text_line.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /**************************************************************************/
  2. /* text_line.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "text_line.h"
  31. #include "text_line.compat.inc"
  32. void TextLine::_bind_methods() {
  33. ClassDB::bind_method(D_METHOD("clear"), &TextLine::clear);
  34. ClassDB::bind_method(D_METHOD("set_direction", "direction"), &TextLine::set_direction);
  35. ClassDB::bind_method(D_METHOD("get_direction"), &TextLine::get_direction);
  36. ClassDB::bind_method(D_METHOD("get_inferred_direction"), &TextLine::get_inferred_direction);
  37. ADD_PROPERTY(PropertyInfo(Variant::INT, "direction", PROPERTY_HINT_ENUM, "Auto,Left-to-right,Right-to-left"), "set_direction", "get_direction");
  38. // If compiling the editor with TextServerFallback only,
  39. // `--doctool` would change the default value to `TextServer::DIRECTION_LTR`.
  40. // Force it so that it's consistent regardless of the backend.
  41. ADD_PROPERTY_DEFAULT("direction", TextServer::DIRECTION_AUTO);
  42. ClassDB::bind_method(D_METHOD("set_orientation", "orientation"), &TextLine::set_orientation);
  43. ClassDB::bind_method(D_METHOD("get_orientation"), &TextLine::get_orientation);
  44. ADD_PROPERTY(PropertyInfo(Variant::INT, "orientation", PROPERTY_HINT_ENUM, "Horizontal,Orientation"), "set_orientation", "get_orientation");
  45. ClassDB::bind_method(D_METHOD("set_preserve_invalid", "enabled"), &TextLine::set_preserve_invalid);
  46. ClassDB::bind_method(D_METHOD("get_preserve_invalid"), &TextLine::get_preserve_invalid);
  47. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "preserve_invalid"), "set_preserve_invalid", "get_preserve_invalid");
  48. ClassDB::bind_method(D_METHOD("set_preserve_control", "enabled"), &TextLine::set_preserve_control);
  49. ClassDB::bind_method(D_METHOD("get_preserve_control"), &TextLine::get_preserve_control);
  50. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "preserve_control"), "set_preserve_control", "get_preserve_control");
  51. ClassDB::bind_method(D_METHOD("set_bidi_override", "override"), &TextLine::set_bidi_override);
  52. ClassDB::bind_method(D_METHOD("add_string", "text", "font", "font_size", "language", "meta"), &TextLine::add_string, DEFVAL(""), DEFVAL(Variant()));
  53. ClassDB::bind_method(D_METHOD("add_object", "key", "size", "inline_align", "length", "baseline"), &TextLine::add_object, DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(1), DEFVAL(0.0));
  54. ClassDB::bind_method(D_METHOD("resize_object", "key", "size", "inline_align", "baseline"), &TextLine::resize_object, DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(0.0));
  55. ClassDB::bind_method(D_METHOD("set_width", "width"), &TextLine::set_width);
  56. ClassDB::bind_method(D_METHOD("get_width"), &TextLine::get_width);
  57. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "width"), "set_width", "get_width");
  58. ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &TextLine::set_horizontal_alignment);
  59. ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &TextLine::get_horizontal_alignment);
  60. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_horizontal_alignment", "get_horizontal_alignment");
  61. ClassDB::bind_method(D_METHOD("tab_align", "tab_stops"), &TextLine::tab_align);
  62. ClassDB::bind_method(D_METHOD("set_flags", "flags"), &TextLine::set_flags);
  63. ClassDB::bind_method(D_METHOD("get_flags"), &TextLine::get_flags);
  64. ADD_PROPERTY(PropertyInfo(Variant::INT, "flags", PROPERTY_HINT_FLAGS, "Kashida Justification,Word Justification,Trim Edge Spaces After Justification,Justify Only After Last Tab,Constrain Ellipsis"), "set_flags", "get_flags");
  65. ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "overrun_behavior"), &TextLine::set_text_overrun_behavior);
  66. ClassDB::bind_method(D_METHOD("get_text_overrun_behavior"), &TextLine::get_text_overrun_behavior);
  67. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_overrun_behavior", PROPERTY_HINT_ENUM, "Trim Nothing,Trim Characters,Trim Words,Ellipsis (6+ Characters),Word Ellipsis (6+ Characters),Ellipsis (Always),Word Ellipsis (Always)"), "set_text_overrun_behavior", "get_text_overrun_behavior");
  68. ClassDB::bind_method(D_METHOD("set_ellipsis_char", "char"), &TextLine::set_ellipsis_char);
  69. ClassDB::bind_method(D_METHOD("get_ellipsis_char"), &TextLine::get_ellipsis_char);
  70. ADD_PROPERTY(PropertyInfo(Variant::STRING, "ellipsis_char"), "set_ellipsis_char", "get_ellipsis_char");
  71. ClassDB::bind_method(D_METHOD("get_objects"), &TextLine::get_objects);
  72. ClassDB::bind_method(D_METHOD("get_object_rect", "key"), &TextLine::get_object_rect);
  73. ClassDB::bind_method(D_METHOD("get_size"), &TextLine::get_size);
  74. ClassDB::bind_method(D_METHOD("get_rid"), &TextLine::get_rid);
  75. ClassDB::bind_method(D_METHOD("get_line_ascent"), &TextLine::get_line_ascent);
  76. ClassDB::bind_method(D_METHOD("get_line_descent"), &TextLine::get_line_descent);
  77. ClassDB::bind_method(D_METHOD("get_line_width"), &TextLine::get_line_width);
  78. ClassDB::bind_method(D_METHOD("get_line_underline_position"), &TextLine::get_line_underline_position);
  79. ClassDB::bind_method(D_METHOD("get_line_underline_thickness"), &TextLine::get_line_underline_thickness);
  80. ClassDB::bind_method(D_METHOD("draw", "canvas", "pos", "color", "oversampling"), &TextLine::draw, DEFVAL(Color(1, 1, 1)), DEFVAL(0.0));
  81. ClassDB::bind_method(D_METHOD("draw_outline", "canvas", "pos", "outline_size", "color", "oversampling"), &TextLine::draw_outline, DEFVAL(1), DEFVAL(Color(1, 1, 1)), DEFVAL(0.0));
  82. ClassDB::bind_method(D_METHOD("hit_test", "coords"), &TextLine::hit_test);
  83. }
  84. void TextLine::_shape() const {
  85. // When a shaped text is invalidated by an external source, we want to reshape it.
  86. if (!TS->shaped_text_is_ready(rid)) {
  87. dirty = true;
  88. }
  89. if (dirty) {
  90. if (!tab_stops.is_empty()) {
  91. TS->shaped_text_tab_align(rid, tab_stops);
  92. }
  93. BitField<TextServer::TextOverrunFlag> overrun_flags = TextServer::OVERRUN_NO_TRIM;
  94. if (overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  95. switch (overrun_behavior) {
  96. case TextServer::OVERRUN_TRIM_WORD_ELLIPSIS_FORCE: {
  97. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  98. overrun_flags.set_flag(TextServer::OVERRUN_TRIM_WORD_ONLY);
  99. overrun_flags.set_flag(TextServer::OVERRUN_ADD_ELLIPSIS);
  100. overrun_flags.set_flag(TextServer::OVERRUN_ENFORCE_ELLIPSIS);
  101. } break;
  102. case TextServer::OVERRUN_TRIM_ELLIPSIS_FORCE: {
  103. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  104. overrun_flags.set_flag(TextServer::OVERRUN_ADD_ELLIPSIS);
  105. overrun_flags.set_flag(TextServer::OVERRUN_ENFORCE_ELLIPSIS);
  106. } break;
  107. case TextServer::OVERRUN_TRIM_WORD_ELLIPSIS:
  108. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  109. overrun_flags.set_flag(TextServer::OVERRUN_TRIM_WORD_ONLY);
  110. overrun_flags.set_flag(TextServer::OVERRUN_ADD_ELLIPSIS);
  111. break;
  112. case TextServer::OVERRUN_TRIM_ELLIPSIS:
  113. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  114. overrun_flags.set_flag(TextServer::OVERRUN_ADD_ELLIPSIS);
  115. break;
  116. case TextServer::OVERRUN_TRIM_WORD:
  117. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  118. overrun_flags.set_flag(TextServer::OVERRUN_TRIM_WORD_ONLY);
  119. break;
  120. case TextServer::OVERRUN_TRIM_CHAR:
  121. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  122. break;
  123. case TextServer::OVERRUN_NO_TRIMMING:
  124. break;
  125. }
  126. if (alignment == HORIZONTAL_ALIGNMENT_FILL) {
  127. TS->shaped_text_fit_to_width(rid, width, flags);
  128. overrun_flags.set_flag(TextServer::OVERRUN_JUSTIFICATION_AWARE);
  129. TS->shaped_text_set_custom_ellipsis(rid, (el_char.length() > 0) ? el_char[0] : 0x2026);
  130. TS->shaped_text_overrun_trim_to_width(rid, width, overrun_flags);
  131. } else {
  132. TS->shaped_text_set_custom_ellipsis(rid, (el_char.length() > 0) ? el_char[0] : 0x2026);
  133. TS->shaped_text_overrun_trim_to_width(rid, width, overrun_flags);
  134. }
  135. } else if (alignment == HORIZONTAL_ALIGNMENT_FILL) {
  136. TS->shaped_text_fit_to_width(rid, width, flags);
  137. }
  138. dirty = false;
  139. }
  140. }
  141. RID TextLine::get_rid() const {
  142. return rid;
  143. }
  144. void TextLine::clear() {
  145. TS->shaped_text_clear(rid);
  146. }
  147. void TextLine::set_preserve_invalid(bool p_enabled) {
  148. TS->shaped_text_set_preserve_invalid(rid, p_enabled);
  149. dirty = true;
  150. }
  151. bool TextLine::get_preserve_invalid() const {
  152. return TS->shaped_text_get_preserve_invalid(rid);
  153. }
  154. void TextLine::set_preserve_control(bool p_enabled) {
  155. TS->shaped_text_set_preserve_control(rid, p_enabled);
  156. dirty = true;
  157. }
  158. bool TextLine::get_preserve_control() const {
  159. return TS->shaped_text_get_preserve_control(rid);
  160. }
  161. void TextLine::set_direction(TextServer::Direction p_direction) {
  162. TS->shaped_text_set_direction(rid, p_direction);
  163. dirty = true;
  164. }
  165. TextServer::Direction TextLine::get_direction() const {
  166. return TS->shaped_text_get_direction(rid);
  167. }
  168. TextServer::Direction TextLine::get_inferred_direction() const {
  169. return TS->shaped_text_get_inferred_direction(rid);
  170. }
  171. void TextLine::set_orientation(TextServer::Orientation p_orientation) {
  172. TS->shaped_text_set_orientation(rid, p_orientation);
  173. dirty = true;
  174. }
  175. TextServer::Orientation TextLine::get_orientation() const {
  176. return TS->shaped_text_get_orientation(rid);
  177. }
  178. void TextLine::set_bidi_override(const Array &p_override) {
  179. TS->shaped_text_set_bidi_override(rid, p_override);
  180. dirty = true;
  181. }
  182. bool TextLine::add_string(const String &p_text, const Ref<Font> &p_font, int p_font_size, const String &p_language, const Variant &p_meta) {
  183. ERR_FAIL_COND_V(p_font.is_null(), false);
  184. bool res = TS->shaped_text_add_string(rid, p_text, p_font->get_rids(), p_font_size, p_font->get_opentype_features(), p_language, p_meta);
  185. dirty = true;
  186. return res;
  187. }
  188. bool TextLine::add_object(Variant p_key, const Size2 &p_size, InlineAlignment p_inline_align, int p_length, float p_baseline) {
  189. bool res = TS->shaped_text_add_object(rid, p_key, p_size, p_inline_align, p_length, p_baseline);
  190. dirty = true;
  191. return res;
  192. }
  193. bool TextLine::resize_object(Variant p_key, const Size2 &p_size, InlineAlignment p_inline_align, float p_baseline) {
  194. _shape();
  195. return TS->shaped_text_resize_object(rid, p_key, p_size, p_inline_align, p_baseline);
  196. }
  197. Array TextLine::get_objects() const {
  198. return TS->shaped_text_get_objects(rid);
  199. }
  200. Rect2 TextLine::get_object_rect(Variant p_key) const {
  201. Vector2 ofs;
  202. float length = TS->shaped_text_get_width(rid);
  203. if (width > 0) {
  204. switch (alignment) {
  205. case HORIZONTAL_ALIGNMENT_FILL:
  206. case HORIZONTAL_ALIGNMENT_LEFT:
  207. break;
  208. case HORIZONTAL_ALIGNMENT_CENTER: {
  209. if (length <= width) {
  210. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  211. ofs.x += Math::floor((width - length) / 2.0);
  212. } else {
  213. ofs.y += Math::floor((width - length) / 2.0);
  214. }
  215. } else if (TS->shaped_text_get_inferred_direction(rid) == TextServer::DIRECTION_RTL) {
  216. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  217. ofs.x += width - length;
  218. } else {
  219. ofs.y += width - length;
  220. }
  221. }
  222. } break;
  223. case HORIZONTAL_ALIGNMENT_RIGHT: {
  224. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  225. ofs.x += width - length;
  226. } else {
  227. ofs.y += width - length;
  228. }
  229. } break;
  230. }
  231. }
  232. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  233. ofs.y += TS->shaped_text_get_ascent(rid);
  234. } else {
  235. ofs.x += TS->shaped_text_get_ascent(rid);
  236. }
  237. Rect2 rect = TS->shaped_text_get_object_rect(rid, p_key);
  238. rect.position += ofs;
  239. return rect;
  240. }
  241. void TextLine::set_horizontal_alignment(HorizontalAlignment p_alignment) {
  242. if (alignment != p_alignment) {
  243. if (alignment == HORIZONTAL_ALIGNMENT_FILL || p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  244. alignment = p_alignment;
  245. dirty = true;
  246. } else {
  247. alignment = p_alignment;
  248. }
  249. }
  250. }
  251. HorizontalAlignment TextLine::get_horizontal_alignment() const {
  252. return alignment;
  253. }
  254. void TextLine::tab_align(const Vector<float> &p_tab_stops) {
  255. tab_stops = p_tab_stops;
  256. dirty = true;
  257. }
  258. void TextLine::set_flags(BitField<TextServer::JustificationFlag> p_flags) {
  259. if (flags != p_flags) {
  260. flags = p_flags;
  261. dirty = true;
  262. }
  263. }
  264. BitField<TextServer::JustificationFlag> TextLine::get_flags() const {
  265. return flags;
  266. }
  267. void TextLine::set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior) {
  268. if (overrun_behavior != p_behavior) {
  269. overrun_behavior = p_behavior;
  270. dirty = true;
  271. }
  272. }
  273. TextServer::OverrunBehavior TextLine::get_text_overrun_behavior() const {
  274. return overrun_behavior;
  275. }
  276. void TextLine::set_ellipsis_char(const String &p_char) {
  277. String c = p_char;
  278. if (c.length() > 1) {
  279. WARN_PRINT("Ellipsis must be exactly one character long (" + itos(c.length()) + " characters given).");
  280. c = c.left(1);
  281. }
  282. if (el_char == c) {
  283. return;
  284. }
  285. el_char = c;
  286. dirty = true;
  287. }
  288. String TextLine::get_ellipsis_char() const {
  289. return el_char;
  290. }
  291. void TextLine::set_width(float p_width) {
  292. width = p_width;
  293. if (alignment == HORIZONTAL_ALIGNMENT_FILL || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  294. dirty = true;
  295. }
  296. }
  297. float TextLine::get_width() const {
  298. return width;
  299. }
  300. Size2 TextLine::get_size() const {
  301. _shape();
  302. return TS->shaped_text_get_size(rid);
  303. }
  304. float TextLine::get_line_ascent() const {
  305. _shape();
  306. return TS->shaped_text_get_ascent(rid);
  307. }
  308. float TextLine::get_line_descent() const {
  309. _shape();
  310. return TS->shaped_text_get_descent(rid);
  311. }
  312. float TextLine::get_line_width() const {
  313. _shape();
  314. return TS->shaped_text_get_width(rid);
  315. }
  316. float TextLine::get_line_underline_position() const {
  317. _shape();
  318. return TS->shaped_text_get_underline_position(rid);
  319. }
  320. float TextLine::get_line_underline_thickness() const {
  321. _shape();
  322. return TS->shaped_text_get_underline_thickness(rid);
  323. }
  324. void TextLine::draw(RID p_canvas, const Vector2 &p_pos, const Color &p_color, float p_oversampling) const {
  325. _shape();
  326. Vector2 ofs = p_pos;
  327. float length = TS->shaped_text_get_width(rid);
  328. if (width > 0) {
  329. switch (alignment) {
  330. case HORIZONTAL_ALIGNMENT_FILL:
  331. case HORIZONTAL_ALIGNMENT_LEFT:
  332. break;
  333. case HORIZONTAL_ALIGNMENT_CENTER: {
  334. if (length <= width) {
  335. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  336. ofs.x += Math::floor((width - length) / 2.0);
  337. } else {
  338. ofs.y += Math::floor((width - length) / 2.0);
  339. }
  340. } else if (TS->shaped_text_get_inferred_direction(rid) == TextServer::DIRECTION_RTL) {
  341. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  342. ofs.x += width - length;
  343. } else {
  344. ofs.y += width - length;
  345. }
  346. }
  347. } break;
  348. case HORIZONTAL_ALIGNMENT_RIGHT: {
  349. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  350. ofs.x += width - length;
  351. } else {
  352. ofs.y += width - length;
  353. }
  354. } break;
  355. }
  356. }
  357. float clip_l;
  358. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  359. ofs.y += TS->shaped_text_get_ascent(rid);
  360. clip_l = MAX(0, p_pos.x - ofs.x);
  361. } else {
  362. ofs.x += TS->shaped_text_get_ascent(rid);
  363. clip_l = MAX(0, p_pos.y - ofs.y);
  364. }
  365. return TS->shaped_text_draw(rid, p_canvas, ofs, clip_l, clip_l + width, p_color, p_oversampling);
  366. }
  367. void TextLine::draw_outline(RID p_canvas, const Vector2 &p_pos, int p_outline_size, const Color &p_color, float p_oversampling) const {
  368. _shape();
  369. Vector2 ofs = p_pos;
  370. float length = TS->shaped_text_get_width(rid);
  371. if (width > 0) {
  372. switch (alignment) {
  373. case HORIZONTAL_ALIGNMENT_FILL:
  374. case HORIZONTAL_ALIGNMENT_LEFT:
  375. break;
  376. case HORIZONTAL_ALIGNMENT_CENTER: {
  377. if (length <= width) {
  378. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  379. ofs.x += Math::floor((width - length) / 2.0);
  380. } else {
  381. ofs.y += Math::floor((width - length) / 2.0);
  382. }
  383. } else if (TS->shaped_text_get_inferred_direction(rid) == TextServer::DIRECTION_RTL) {
  384. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  385. ofs.x += width - length;
  386. } else {
  387. ofs.y += width - length;
  388. }
  389. }
  390. } break;
  391. case HORIZONTAL_ALIGNMENT_RIGHT: {
  392. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  393. ofs.x += width - length;
  394. } else {
  395. ofs.y += width - length;
  396. }
  397. } break;
  398. }
  399. }
  400. float clip_l;
  401. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  402. ofs.y += TS->shaped_text_get_ascent(rid);
  403. clip_l = MAX(0, p_pos.x - ofs.x);
  404. } else {
  405. ofs.x += TS->shaped_text_get_ascent(rid);
  406. clip_l = MAX(0, p_pos.y - ofs.y);
  407. }
  408. return TS->shaped_text_draw_outline(rid, p_canvas, ofs, clip_l, clip_l + width, p_outline_size, p_color, p_oversampling);
  409. }
  410. int TextLine::hit_test(float p_coords) const {
  411. _shape();
  412. return TS->shaped_text_hit_test_position(rid, p_coords);
  413. }
  414. TextLine::TextLine(const String &p_text, const Ref<Font> &p_font, int p_font_size, const String &p_language, TextServer::Direction p_direction, TextServer::Orientation p_orientation) {
  415. rid = TS->create_shaped_text(p_direction, p_orientation);
  416. if (p_font.is_valid()) {
  417. TS->shaped_text_add_string(rid, p_text, p_font->get_rids(), p_font_size, p_font->get_opentype_features(), p_language);
  418. }
  419. }
  420. TextLine::TextLine() {
  421. rid = TS->create_shaped_text();
  422. }
  423. TextLine::~TextLine() {
  424. TS->free_rid(rid);
  425. }