text_paragraph.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. /**************************************************************************/
  2. /* text_paragraph.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 "scene/resources/text_paragraph.h"
  31. void TextParagraph::_bind_methods() {
  32. ClassDB::bind_method(D_METHOD("clear"), &TextParagraph::clear);
  33. ClassDB::bind_method(D_METHOD("set_direction", "direction"), &TextParagraph::set_direction);
  34. ClassDB::bind_method(D_METHOD("get_direction"), &TextParagraph::get_direction);
  35. ADD_PROPERTY(PropertyInfo(Variant::INT, "direction", PROPERTY_HINT_ENUM, "Auto,Light-to-right,Right-to-left"), "set_direction", "get_direction");
  36. ClassDB::bind_method(D_METHOD("set_custom_punctuation", "custom_punctuation"), &TextParagraph::set_custom_punctuation);
  37. ClassDB::bind_method(D_METHOD("get_custom_punctuation"), &TextParagraph::get_custom_punctuation);
  38. ADD_PROPERTY(PropertyInfo(Variant::STRING, "custom_punctuation"), "set_custom_punctuation", "get_custom_punctuation");
  39. ClassDB::bind_method(D_METHOD("set_orientation", "orientation"), &TextParagraph::set_orientation);
  40. ClassDB::bind_method(D_METHOD("get_orientation"), &TextParagraph::get_orientation);
  41. ADD_PROPERTY(PropertyInfo(Variant::INT, "orientation", PROPERTY_HINT_ENUM, "Horizontal,Orientation"), "set_orientation", "get_orientation");
  42. ClassDB::bind_method(D_METHOD("set_preserve_invalid", "enabled"), &TextParagraph::set_preserve_invalid);
  43. ClassDB::bind_method(D_METHOD("get_preserve_invalid"), &TextParagraph::get_preserve_invalid);
  44. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "preserve_invalid"), "set_preserve_invalid", "get_preserve_invalid");
  45. ClassDB::bind_method(D_METHOD("set_preserve_control", "enabled"), &TextParagraph::set_preserve_control);
  46. ClassDB::bind_method(D_METHOD("get_preserve_control"), &TextParagraph::get_preserve_control);
  47. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "preserve_control"), "set_preserve_control", "get_preserve_control");
  48. ClassDB::bind_method(D_METHOD("set_bidi_override", "override"), &TextParagraph::set_bidi_override);
  49. ClassDB::bind_method(D_METHOD("set_dropcap", "text", "font", "font_size", "dropcap_margins", "language"), &TextParagraph::set_dropcap, DEFVAL(Rect2()), DEFVAL(""));
  50. ClassDB::bind_method(D_METHOD("clear_dropcap"), &TextParagraph::clear_dropcap);
  51. ClassDB::bind_method(D_METHOD("add_string", "text", "font", "font_size", "language", "meta"), &TextParagraph::add_string, DEFVAL(""), DEFVAL(Variant()));
  52. ClassDB::bind_method(D_METHOD("add_object", "key", "size", "inline_align", "length", "baseline"), &TextParagraph::add_object, DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(1), DEFVAL(0.0));
  53. ClassDB::bind_method(D_METHOD("resize_object", "key", "size", "inline_align", "baseline"), &TextParagraph::resize_object, DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(0.0));
  54. ClassDB::bind_method(D_METHOD("set_alignment", "alignment"), &TextParagraph::set_alignment);
  55. ClassDB::bind_method(D_METHOD("get_alignment"), &TextParagraph::get_alignment);
  56. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_alignment", "get_alignment");
  57. ClassDB::bind_method(D_METHOD("tab_align", "tab_stops"), &TextParagraph::tab_align);
  58. ClassDB::bind_method(D_METHOD("set_break_flags", "flags"), &TextParagraph::set_break_flags);
  59. ClassDB::bind_method(D_METHOD("get_break_flags"), &TextParagraph::get_break_flags);
  60. ADD_PROPERTY(PropertyInfo(Variant::INT, "break_flags", PROPERTY_HINT_FLAGS, "Mandatory,Word Bound,Grapheme Bound,Adaptive,Trim Spaces"), "set_break_flags", "get_break_flags");
  61. ClassDB::bind_method(D_METHOD("set_justification_flags", "flags"), &TextParagraph::set_justification_flags);
  62. ClassDB::bind_method(D_METHOD("get_justification_flags"), &TextParagraph::get_justification_flags);
  63. ADD_PROPERTY(PropertyInfo(Variant::INT, "justification_flags", PROPERTY_HINT_FLAGS, "Kashida Justification:1,Word Justification:2,Trim Edge Spaces After Justification:4,Justify Only After Last Tab:8,Constrain Ellipsis:16,Skip Last Line:32,Skip Last Line With Visible Characters:64,Do Not Skip Single Line:128"), "set_justification_flags", "get_justification_flags");
  64. ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "overrun_behavior"), &TextParagraph::set_text_overrun_behavior);
  65. ClassDB::bind_method(D_METHOD("get_text_overrun_behavior"), &TextParagraph::get_text_overrun_behavior);
  66. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_overrun_behavior", PROPERTY_HINT_ENUM, "Trim Nothing,Trim Characters,Trim Words,Ellipsis,Word Ellipsis"), "set_text_overrun_behavior", "get_text_overrun_behavior");
  67. ClassDB::bind_method(D_METHOD("set_ellipsis_char", "char"), &TextParagraph::set_ellipsis_char);
  68. ClassDB::bind_method(D_METHOD("get_ellipsis_char"), &TextParagraph::get_ellipsis_char);
  69. ADD_PROPERTY(PropertyInfo(Variant::STRING, "ellipsis_char"), "set_ellipsis_char", "get_ellipsis_char");
  70. ClassDB::bind_method(D_METHOD("set_width", "width"), &TextParagraph::set_width);
  71. ClassDB::bind_method(D_METHOD("get_width"), &TextParagraph::get_width);
  72. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "width"), "set_width", "get_width");
  73. ClassDB::bind_method(D_METHOD("get_non_wrapped_size"), &TextParagraph::get_non_wrapped_size);
  74. ClassDB::bind_method(D_METHOD("get_size"), &TextParagraph::get_size);
  75. ClassDB::bind_method(D_METHOD("get_rid"), &TextParagraph::get_rid);
  76. ClassDB::bind_method(D_METHOD("get_line_rid", "line"), &TextParagraph::get_line_rid);
  77. ClassDB::bind_method(D_METHOD("get_dropcap_rid"), &TextParagraph::get_dropcap_rid);
  78. ClassDB::bind_method(D_METHOD("get_line_count"), &TextParagraph::get_line_count);
  79. ClassDB::bind_method(D_METHOD("set_max_lines_visible", "max_lines_visible"), &TextParagraph::set_max_lines_visible);
  80. ClassDB::bind_method(D_METHOD("get_max_lines_visible"), &TextParagraph::get_max_lines_visible);
  81. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_lines_visible"), "set_max_lines_visible", "get_max_lines_visible");
  82. ClassDB::bind_method(D_METHOD("set_line_spacing", "line_spacing"), &TextParagraph::set_line_spacing);
  83. ClassDB::bind_method(D_METHOD("get_line_spacing"), &TextParagraph::get_line_spacing);
  84. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "line_spacing"), "set_line_spacing", "get_line_spacing");
  85. ClassDB::bind_method(D_METHOD("get_line_objects", "line"), &TextParagraph::get_line_objects);
  86. ClassDB::bind_method(D_METHOD("get_line_object_rect", "line", "key"), &TextParagraph::get_line_object_rect);
  87. ClassDB::bind_method(D_METHOD("get_line_size", "line"), &TextParagraph::get_line_size);
  88. ClassDB::bind_method(D_METHOD("get_line_range", "line"), &TextParagraph::get_line_range);
  89. ClassDB::bind_method(D_METHOD("get_line_ascent", "line"), &TextParagraph::get_line_ascent);
  90. ClassDB::bind_method(D_METHOD("get_line_descent", "line"), &TextParagraph::get_line_descent);
  91. ClassDB::bind_method(D_METHOD("get_line_width", "line"), &TextParagraph::get_line_width);
  92. ClassDB::bind_method(D_METHOD("get_line_underline_position", "line"), &TextParagraph::get_line_underline_position);
  93. ClassDB::bind_method(D_METHOD("get_line_underline_thickness", "line"), &TextParagraph::get_line_underline_thickness);
  94. ClassDB::bind_method(D_METHOD("get_dropcap_size"), &TextParagraph::get_dropcap_size);
  95. ClassDB::bind_method(D_METHOD("get_dropcap_lines"), &TextParagraph::get_dropcap_lines);
  96. ClassDB::bind_method(D_METHOD("draw", "canvas", "pos", "color", "dc_color"), &TextParagraph::draw, DEFVAL(Color(1, 1, 1)), DEFVAL(Color(1, 1, 1)));
  97. ClassDB::bind_method(D_METHOD("draw_outline", "canvas", "pos", "outline_size", "color", "dc_color"), &TextParagraph::draw_outline, DEFVAL(1), DEFVAL(Color(1, 1, 1)), DEFVAL(Color(1, 1, 1)));
  98. ClassDB::bind_method(D_METHOD("draw_line", "canvas", "pos", "line", "color"), &TextParagraph::draw_line, DEFVAL(Color(1, 1, 1)));
  99. ClassDB::bind_method(D_METHOD("draw_line_outline", "canvas", "pos", "line", "outline_size", "color"), &TextParagraph::draw_line_outline, DEFVAL(1), DEFVAL(Color(1, 1, 1)));
  100. ClassDB::bind_method(D_METHOD("draw_dropcap", "canvas", "pos", "color"), &TextParagraph::draw_dropcap, DEFVAL(Color(1, 1, 1)));
  101. ClassDB::bind_method(D_METHOD("draw_dropcap_outline", "canvas", "pos", "outline_size", "color"), &TextParagraph::draw_dropcap_outline, DEFVAL(1), DEFVAL(Color(1, 1, 1)));
  102. ClassDB::bind_method(D_METHOD("hit_test", "coords"), &TextParagraph::hit_test);
  103. }
  104. void TextParagraph::_shape_lines() {
  105. // When a shaped text is invalidated by an external source, we want to reshape it.
  106. if (!TS->shaped_text_is_ready(rid) || !TS->shaped_text_is_ready(dropcap_rid)) {
  107. lines_dirty = true;
  108. }
  109. for (const RID &line_rid : lines_rid) {
  110. if (!TS->shaped_text_is_ready(line_rid)) {
  111. lines_dirty = true;
  112. break;
  113. }
  114. }
  115. if (lines_dirty) {
  116. for (const RID &line_rid : lines_rid) {
  117. TS->free_rid(line_rid);
  118. }
  119. lines_rid.clear();
  120. if (!tab_stops.is_empty()) {
  121. TS->shaped_text_tab_align(rid, tab_stops);
  122. }
  123. float h_offset = 0.f;
  124. float v_offset = 0.f;
  125. int start = 0;
  126. dropcap_lines = 0;
  127. if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
  128. h_offset = TS->shaped_text_get_size(dropcap_rid).x + dropcap_margins.size.x + dropcap_margins.position.x;
  129. v_offset = TS->shaped_text_get_size(dropcap_rid).y + dropcap_margins.size.y + dropcap_margins.position.y;
  130. } else {
  131. h_offset = TS->shaped_text_get_size(dropcap_rid).y + dropcap_margins.size.y + dropcap_margins.position.y;
  132. v_offset = TS->shaped_text_get_size(dropcap_rid).x + dropcap_margins.size.x + dropcap_margins.position.x;
  133. }
  134. Size2i range = TS->shaped_text_get_range(rid);
  135. if (h_offset > 0) {
  136. // Dropcap, flow around.
  137. PackedInt32Array line_breaks = TS->shaped_text_get_line_breaks(rid, width - h_offset, 0, brk_flags);
  138. for (int i = 0; i < line_breaks.size(); i = i + 2) {
  139. RID line = TS->shaped_text_substr(rid, line_breaks[i], line_breaks[i + 1] - line_breaks[i]);
  140. float h = (TS->shaped_text_get_orientation(line) == TextServer::ORIENTATION_HORIZONTAL) ? TS->shaped_text_get_size(line).y : TS->shaped_text_get_size(line).x;
  141. h += line_spacing;
  142. if (!tab_stops.is_empty()) {
  143. TS->shaped_text_tab_align(line, tab_stops);
  144. }
  145. start = (i < line_breaks.size() - 2) ? line_breaks[i + 2] : range.y;
  146. lines_rid.push_back(line);
  147. if (v_offset < h) {
  148. break;
  149. }
  150. dropcap_lines++;
  151. v_offset -= h;
  152. }
  153. }
  154. // Use fixed for the rest of lines.
  155. if (start == 0 || start < range.y) {
  156. PackedInt32Array line_breaks = TS->shaped_text_get_line_breaks(rid, width, start, brk_flags);
  157. for (int i = 0; i < line_breaks.size(); i = i + 2) {
  158. RID line = TS->shaped_text_substr(rid, line_breaks[i], line_breaks[i + 1] - line_breaks[i]);
  159. if (!tab_stops.is_empty()) {
  160. TS->shaped_text_tab_align(line, tab_stops);
  161. }
  162. lines_rid.push_back(line);
  163. }
  164. }
  165. BitField<TextServer::TextOverrunFlag> overrun_flags = TextServer::OVERRUN_NO_TRIM;
  166. if (overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  167. switch (overrun_behavior) {
  168. case TextServer::OVERRUN_TRIM_WORD_ELLIPSIS:
  169. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  170. overrun_flags.set_flag(TextServer::OVERRUN_TRIM_WORD_ONLY);
  171. overrun_flags.set_flag(TextServer::OVERRUN_ADD_ELLIPSIS);
  172. break;
  173. case TextServer::OVERRUN_TRIM_ELLIPSIS:
  174. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  175. overrun_flags.set_flag(TextServer::OVERRUN_ADD_ELLIPSIS);
  176. break;
  177. case TextServer::OVERRUN_TRIM_WORD:
  178. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  179. overrun_flags.set_flag(TextServer::OVERRUN_TRIM_WORD_ONLY);
  180. break;
  181. case TextServer::OVERRUN_TRIM_CHAR:
  182. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  183. break;
  184. case TextServer::OVERRUN_NO_TRIMMING:
  185. break;
  186. }
  187. }
  188. bool autowrap_enabled = brk_flags.has_flag(TextServer::BREAK_WORD_BOUND) || brk_flags.has_flag(TextServer::BREAK_GRAPHEME_BOUND);
  189. // Fill after min_size calculation.
  190. if (autowrap_enabled) {
  191. int visible_lines = (max_lines_visible >= 0) ? MIN(max_lines_visible, (int)lines_rid.size()) : (int)lines_rid.size();
  192. bool lines_hidden = visible_lines > 0 && visible_lines < (int)lines_rid.size();
  193. if (lines_hidden) {
  194. overrun_flags.set_flag(TextServer::OVERRUN_ENFORCE_ELLIPSIS);
  195. }
  196. if (alignment == HORIZONTAL_ALIGNMENT_FILL) {
  197. int jst_to_line = visible_lines;
  198. if (lines_rid.size() == 1 && jst_flags.has_flag(TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE)) {
  199. jst_to_line = lines_rid.size();
  200. } else {
  201. if (jst_flags.has_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE)) {
  202. jst_to_line = visible_lines - 1;
  203. }
  204. if (jst_flags.has_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE_WITH_VISIBLE_CHARS)) {
  205. for (int i = visible_lines - 1; i >= 0; i--) {
  206. if (TS->shaped_text_has_visible_chars(lines_rid[i])) {
  207. jst_to_line = i;
  208. break;
  209. }
  210. }
  211. }
  212. }
  213. for (int i = 0; i < (int)lines_rid.size(); i++) {
  214. float line_w = (i <= dropcap_lines) ? (width - h_offset) : width;
  215. if (i < jst_to_line) {
  216. TS->shaped_text_fit_to_width(lines_rid[i], line_w, jst_flags);
  217. } else if (i == (visible_lines - 1)) {
  218. TS->shaped_text_set_custom_ellipsis(lines_rid[visible_lines - 1], (el_char.length() > 0) ? el_char[0] : 0x2026);
  219. TS->shaped_text_overrun_trim_to_width(lines_rid[visible_lines - 1], line_w, overrun_flags);
  220. }
  221. }
  222. } else if (lines_hidden) {
  223. TS->shaped_text_set_custom_ellipsis(lines_rid[visible_lines - 1], (el_char.length() > 0) ? el_char[0] : 0x2026);
  224. TS->shaped_text_overrun_trim_to_width(lines_rid[visible_lines - 1], (visible_lines - 1 <= dropcap_lines) ? (width - h_offset) : width, overrun_flags);
  225. }
  226. } else {
  227. // Autowrap disabled.
  228. int jst_to_line = lines_rid.size();
  229. if (lines_rid.size() == 1 && jst_flags.has_flag(TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE)) {
  230. jst_to_line = lines_rid.size();
  231. } else {
  232. if (jst_flags.has_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE)) {
  233. jst_to_line = lines_rid.size() - 1;
  234. }
  235. if (jst_flags.has_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE_WITH_VISIBLE_CHARS)) {
  236. for (int i = lines_rid.size() - 1; i >= 0; i--) {
  237. if (TS->shaped_text_has_visible_chars(lines_rid[i])) {
  238. jst_to_line = i;
  239. break;
  240. }
  241. }
  242. }
  243. }
  244. for (int i = 0; i < (int)lines_rid.size(); i++) {
  245. float line_w = (i <= dropcap_lines) ? (width - h_offset) : width;
  246. if (i < jst_to_line && alignment == HORIZONTAL_ALIGNMENT_FILL) {
  247. TS->shaped_text_fit_to_width(lines_rid[i], line_w, jst_flags);
  248. overrun_flags.set_flag(TextServer::OVERRUN_JUSTIFICATION_AWARE);
  249. TS->shaped_text_set_custom_ellipsis(lines_rid[i], (el_char.length() > 0) ? el_char[0] : 0x2026);
  250. TS->shaped_text_overrun_trim_to_width(lines_rid[i], line_w, overrun_flags);
  251. TS->shaped_text_fit_to_width(lines_rid[i], line_w, jst_flags | TextServer::JUSTIFICATION_CONSTRAIN_ELLIPSIS);
  252. } else {
  253. TS->shaped_text_set_custom_ellipsis(lines_rid[i], (el_char.length() > 0) ? el_char[0] : 0x2026);
  254. TS->shaped_text_overrun_trim_to_width(lines_rid[i], line_w, overrun_flags);
  255. }
  256. }
  257. }
  258. lines_dirty = false;
  259. }
  260. }
  261. RID TextParagraph::get_rid() const {
  262. return rid;
  263. }
  264. RID TextParagraph::get_line_rid(int p_line) const {
  265. _THREAD_SAFE_METHOD_
  266. const_cast<TextParagraph *>(this)->_shape_lines();
  267. ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)lines_rid.size(), RID());
  268. return lines_rid[p_line];
  269. }
  270. RID TextParagraph::get_dropcap_rid() const {
  271. return dropcap_rid;
  272. }
  273. void TextParagraph::clear() {
  274. _THREAD_SAFE_METHOD_
  275. for (const RID &line_rid : lines_rid) {
  276. TS->free_rid(line_rid);
  277. }
  278. lines_rid.clear();
  279. TS->shaped_text_clear(rid);
  280. TS->shaped_text_clear(dropcap_rid);
  281. }
  282. void TextParagraph::set_preserve_invalid(bool p_enabled) {
  283. _THREAD_SAFE_METHOD_
  284. TS->shaped_text_set_preserve_invalid(rid, p_enabled);
  285. TS->shaped_text_set_preserve_invalid(dropcap_rid, p_enabled);
  286. lines_dirty = true;
  287. }
  288. bool TextParagraph::get_preserve_invalid() const {
  289. _THREAD_SAFE_METHOD_
  290. return TS->shaped_text_get_preserve_invalid(rid);
  291. }
  292. void TextParagraph::set_preserve_control(bool p_enabled) {
  293. _THREAD_SAFE_METHOD_
  294. TS->shaped_text_set_preserve_control(rid, p_enabled);
  295. TS->shaped_text_set_preserve_control(dropcap_rid, p_enabled);
  296. lines_dirty = true;
  297. }
  298. bool TextParagraph::get_preserve_control() const {
  299. _THREAD_SAFE_METHOD_
  300. return TS->shaped_text_get_preserve_control(rid);
  301. }
  302. void TextParagraph::set_direction(TextServer::Direction p_direction) {
  303. _THREAD_SAFE_METHOD_
  304. TS->shaped_text_set_direction(rid, p_direction);
  305. TS->shaped_text_set_direction(dropcap_rid, p_direction);
  306. lines_dirty = true;
  307. }
  308. TextServer::Direction TextParagraph::get_direction() const {
  309. _THREAD_SAFE_METHOD_
  310. const_cast<TextParagraph *>(this)->_shape_lines();
  311. return TS->shaped_text_get_direction(rid);
  312. }
  313. void TextParagraph::set_custom_punctuation(const String &p_punct) {
  314. _THREAD_SAFE_METHOD_
  315. TS->shaped_text_set_custom_punctuation(rid, p_punct);
  316. lines_dirty = true;
  317. }
  318. String TextParagraph::get_custom_punctuation() const {
  319. _THREAD_SAFE_METHOD_
  320. return TS->shaped_text_get_custom_punctuation(rid);
  321. }
  322. void TextParagraph::set_orientation(TextServer::Orientation p_orientation) {
  323. _THREAD_SAFE_METHOD_
  324. TS->shaped_text_set_orientation(rid, p_orientation);
  325. TS->shaped_text_set_orientation(dropcap_rid, p_orientation);
  326. lines_dirty = true;
  327. }
  328. TextServer::Orientation TextParagraph::get_orientation() const {
  329. _THREAD_SAFE_METHOD_
  330. const_cast<TextParagraph *>(this)->_shape_lines();
  331. return TS->shaped_text_get_orientation(rid);
  332. }
  333. bool TextParagraph::set_dropcap(const String &p_text, const Ref<Font> &p_font, int p_font_size, const Rect2 &p_dropcap_margins, const String &p_language) {
  334. _THREAD_SAFE_METHOD_
  335. ERR_FAIL_COND_V(p_font.is_null(), false);
  336. TS->shaped_text_clear(dropcap_rid);
  337. dropcap_margins = p_dropcap_margins;
  338. bool res = TS->shaped_text_add_string(dropcap_rid, p_text, p_font->get_rids(), p_font_size, p_font->get_opentype_features(), p_language);
  339. lines_dirty = true;
  340. return res;
  341. }
  342. void TextParagraph::clear_dropcap() {
  343. _THREAD_SAFE_METHOD_
  344. dropcap_margins = Rect2();
  345. TS->shaped_text_clear(dropcap_rid);
  346. lines_dirty = true;
  347. }
  348. bool TextParagraph::add_string(const String &p_text, const Ref<Font> &p_font, int p_font_size, const String &p_language, const Variant &p_meta) {
  349. _THREAD_SAFE_METHOD_
  350. ERR_FAIL_COND_V(p_font.is_null(), false);
  351. 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);
  352. lines_dirty = true;
  353. return res;
  354. }
  355. void TextParagraph::set_bidi_override(const Array &p_override) {
  356. _THREAD_SAFE_METHOD_
  357. TS->shaped_text_set_bidi_override(rid, p_override);
  358. lines_dirty = true;
  359. }
  360. bool TextParagraph::add_object(Variant p_key, const Size2 &p_size, InlineAlignment p_inline_align, int p_length, float p_baseline) {
  361. _THREAD_SAFE_METHOD_
  362. bool res = TS->shaped_text_add_object(rid, p_key, p_size, p_inline_align, p_length, p_baseline);
  363. lines_dirty = true;
  364. return res;
  365. }
  366. bool TextParagraph::resize_object(Variant p_key, const Size2 &p_size, InlineAlignment p_inline_align, float p_baseline) {
  367. _THREAD_SAFE_METHOD_
  368. bool res = TS->shaped_text_resize_object(rid, p_key, p_size, p_inline_align, p_baseline);
  369. lines_dirty = true;
  370. return res;
  371. }
  372. void TextParagraph::set_alignment(HorizontalAlignment p_alignment) {
  373. _THREAD_SAFE_METHOD_
  374. if (alignment != p_alignment) {
  375. if (alignment == HORIZONTAL_ALIGNMENT_FILL || p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  376. alignment = p_alignment;
  377. lines_dirty = true;
  378. } else {
  379. alignment = p_alignment;
  380. }
  381. }
  382. }
  383. HorizontalAlignment TextParagraph::get_alignment() const {
  384. return alignment;
  385. }
  386. void TextParagraph::tab_align(const Vector<float> &p_tab_stops) {
  387. _THREAD_SAFE_METHOD_
  388. tab_stops = p_tab_stops;
  389. lines_dirty = true;
  390. }
  391. void TextParagraph::set_justification_flags(BitField<TextServer::JustificationFlag> p_flags) {
  392. _THREAD_SAFE_METHOD_
  393. if (jst_flags != p_flags) {
  394. jst_flags = p_flags;
  395. lines_dirty = true;
  396. }
  397. }
  398. BitField<TextServer::JustificationFlag> TextParagraph::get_justification_flags() const {
  399. return jst_flags;
  400. }
  401. void TextParagraph::set_break_flags(BitField<TextServer::LineBreakFlag> p_flags) {
  402. _THREAD_SAFE_METHOD_
  403. if (brk_flags != p_flags) {
  404. brk_flags = p_flags;
  405. lines_dirty = true;
  406. }
  407. }
  408. BitField<TextServer::LineBreakFlag> TextParagraph::get_break_flags() const {
  409. return brk_flags;
  410. }
  411. void TextParagraph::set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior) {
  412. _THREAD_SAFE_METHOD_
  413. if (overrun_behavior != p_behavior) {
  414. overrun_behavior = p_behavior;
  415. lines_dirty = true;
  416. }
  417. }
  418. TextServer::OverrunBehavior TextParagraph::get_text_overrun_behavior() const {
  419. return overrun_behavior;
  420. }
  421. void TextParagraph::set_ellipsis_char(const String &p_char) {
  422. String c = p_char;
  423. if (c.length() > 1) {
  424. WARN_PRINT("Ellipsis must be exactly one character long (" + itos(c.length()) + " characters given).");
  425. c = c.left(1);
  426. }
  427. if (el_char == c) {
  428. return;
  429. }
  430. el_char = c;
  431. lines_dirty = true;
  432. }
  433. String TextParagraph::get_ellipsis_char() const {
  434. return el_char;
  435. }
  436. void TextParagraph::set_width(float p_width) {
  437. _THREAD_SAFE_METHOD_
  438. if (width != p_width) {
  439. width = p_width;
  440. lines_dirty = true;
  441. }
  442. }
  443. float TextParagraph::get_width() const {
  444. return width;
  445. }
  446. Size2 TextParagraph::get_non_wrapped_size() const {
  447. _THREAD_SAFE_METHOD_
  448. const_cast<TextParagraph *>(this)->_shape_lines();
  449. return TS->shaped_text_get_size(rid);
  450. }
  451. Size2 TextParagraph::get_size() const {
  452. _THREAD_SAFE_METHOD_
  453. const_cast<TextParagraph *>(this)->_shape_lines();
  454. float h_offset = 0.f;
  455. float v_offset = 0.f;
  456. if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
  457. h_offset = TS->shaped_text_get_size(dropcap_rid).x + dropcap_margins.size.x + dropcap_margins.position.x;
  458. v_offset = TS->shaped_text_get_size(dropcap_rid).y + dropcap_margins.size.y + dropcap_margins.position.y;
  459. } else {
  460. h_offset = TS->shaped_text_get_size(dropcap_rid).y + dropcap_margins.size.y + dropcap_margins.position.y;
  461. v_offset = TS->shaped_text_get_size(dropcap_rid).x + dropcap_margins.size.x + dropcap_margins.position.x;
  462. }
  463. Size2 size;
  464. int visible_lines = (max_lines_visible >= 0) ? MIN(max_lines_visible, (int)lines_rid.size()) : (int)lines_rid.size();
  465. for (int i = 0; i < visible_lines; i++) {
  466. Size2 lsize = TS->shaped_text_get_size(lines_rid[i]);
  467. if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
  468. if (h_offset > 0 && i <= dropcap_lines) {
  469. lsize.x += h_offset;
  470. }
  471. size.x = MAX(size.x, lsize.x);
  472. size.y += lsize.y;
  473. if (i != visible_lines - 1) {
  474. size.y += line_spacing;
  475. }
  476. } else {
  477. if (h_offset > 0 && i <= dropcap_lines) {
  478. lsize.y += h_offset;
  479. }
  480. size.x += lsize.x;
  481. size.y = MAX(size.y, lsize.y);
  482. if (i != visible_lines - 1) {
  483. size.x += line_spacing;
  484. }
  485. }
  486. }
  487. if (h_offset > 0) {
  488. if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
  489. size.y = MAX(size.y, v_offset);
  490. } else {
  491. size.x = MAX(size.x, v_offset);
  492. }
  493. }
  494. return size;
  495. }
  496. int TextParagraph::get_line_count() const {
  497. _THREAD_SAFE_METHOD_
  498. const_cast<TextParagraph *>(this)->_shape_lines();
  499. return (int)lines_rid.size();
  500. }
  501. void TextParagraph::set_max_lines_visible(int p_lines) {
  502. _THREAD_SAFE_METHOD_
  503. if (p_lines != max_lines_visible) {
  504. max_lines_visible = p_lines;
  505. lines_dirty = true;
  506. }
  507. }
  508. int TextParagraph::get_max_lines_visible() const {
  509. return max_lines_visible;
  510. }
  511. void TextParagraph::set_line_spacing(float p_spacing) {
  512. _THREAD_SAFE_METHOD_
  513. if (line_spacing != p_spacing) {
  514. line_spacing = p_spacing;
  515. lines_dirty = true;
  516. }
  517. }
  518. float TextParagraph::get_line_spacing() const {
  519. return line_spacing;
  520. }
  521. Array TextParagraph::get_line_objects(int p_line) const {
  522. _THREAD_SAFE_METHOD_
  523. const_cast<TextParagraph *>(this)->_shape_lines();
  524. ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)lines_rid.size(), Array());
  525. return TS->shaped_text_get_objects(lines_rid[p_line]);
  526. }
  527. Rect2 TextParagraph::get_line_object_rect(int p_line, Variant p_key) const {
  528. _THREAD_SAFE_METHOD_
  529. const_cast<TextParagraph *>(this)->_shape_lines();
  530. ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)lines_rid.size(), Rect2());
  531. Vector2 ofs;
  532. float h_offset = 0.f;
  533. if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
  534. h_offset = TS->shaped_text_get_size(dropcap_rid).x + dropcap_margins.size.x + dropcap_margins.position.x;
  535. } else {
  536. h_offset = TS->shaped_text_get_size(dropcap_rid).y + dropcap_margins.size.y + dropcap_margins.position.y;
  537. }
  538. for (int i = 0; i <= p_line; i++) {
  539. float l_width = width;
  540. if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
  541. ofs.x = 0.f;
  542. ofs.y += TS->shaped_text_get_ascent(lines_rid[i]);
  543. if (i <= dropcap_lines) {
  544. if (TS->shaped_text_get_inferred_direction(dropcap_rid) == TextServer::DIRECTION_LTR) {
  545. ofs.x -= h_offset;
  546. }
  547. l_width -= h_offset;
  548. }
  549. } else {
  550. ofs.y = 0.f;
  551. ofs.x += TS->shaped_text_get_ascent(lines_rid[i]);
  552. if (i <= dropcap_lines) {
  553. if (TS->shaped_text_get_inferred_direction(dropcap_rid) == TextServer::DIRECTION_LTR) {
  554. ofs.y -= h_offset;
  555. }
  556. l_width -= h_offset;
  557. }
  558. }
  559. float length = TS->shaped_text_get_width(lines_rid[i]);
  560. if (width > 0) {
  561. switch (alignment) {
  562. case HORIZONTAL_ALIGNMENT_FILL:
  563. if (TS->shaped_text_get_inferred_direction(lines_rid[i]) == TextServer::DIRECTION_RTL) {
  564. if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
  565. ofs.x += l_width - length;
  566. } else {
  567. ofs.y += l_width - length;
  568. }
  569. }
  570. break;
  571. case HORIZONTAL_ALIGNMENT_LEFT:
  572. break;
  573. case HORIZONTAL_ALIGNMENT_CENTER: {
  574. if (length <= l_width) {
  575. if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
  576. ofs.x += Math::floor((l_width - length) / 2.0);
  577. } else {
  578. ofs.y += Math::floor((l_width - length) / 2.0);
  579. }
  580. } else if (TS->shaped_text_get_inferred_direction(lines_rid[i]) == TextServer::DIRECTION_RTL) {
  581. if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
  582. ofs.x += l_width - length;
  583. } else {
  584. ofs.y += l_width - length;
  585. }
  586. }
  587. } break;
  588. case HORIZONTAL_ALIGNMENT_RIGHT: {
  589. if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
  590. ofs.x += l_width - length;
  591. } else {
  592. ofs.y += l_width - length;
  593. }
  594. } break;
  595. }
  596. }
  597. if (i != p_line) {
  598. if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
  599. ofs.x = 0.f;
  600. ofs.y += TS->shaped_text_get_descent(lines_rid[i]) + line_spacing;
  601. } else {
  602. ofs.y = 0.f;
  603. ofs.x += TS->shaped_text_get_descent(lines_rid[i]) + line_spacing;
  604. }
  605. }
  606. }
  607. Rect2 rect = TS->shaped_text_get_object_rect(lines_rid[p_line], p_key);
  608. rect.position += ofs;
  609. return rect;
  610. }
  611. Size2 TextParagraph::get_line_size(int p_line) const {
  612. _THREAD_SAFE_METHOD_
  613. const_cast<TextParagraph *>(this)->_shape_lines();
  614. ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)lines_rid.size(), Size2());
  615. return TS->shaped_text_get_size(lines_rid[p_line]);
  616. }
  617. Vector2i TextParagraph::get_line_range(int p_line) const {
  618. _THREAD_SAFE_METHOD_
  619. const_cast<TextParagraph *>(this)->_shape_lines();
  620. ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)lines_rid.size(), Vector2i());
  621. return TS->shaped_text_get_range(lines_rid[p_line]);
  622. }
  623. float TextParagraph::get_line_ascent(int p_line) const {
  624. _THREAD_SAFE_METHOD_
  625. const_cast<TextParagraph *>(this)->_shape_lines();
  626. ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)lines_rid.size(), 0.f);
  627. return TS->shaped_text_get_ascent(lines_rid[p_line]);
  628. }
  629. float TextParagraph::get_line_descent(int p_line) const {
  630. _THREAD_SAFE_METHOD_
  631. const_cast<TextParagraph *>(this)->_shape_lines();
  632. ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)lines_rid.size(), 0.f);
  633. return TS->shaped_text_get_descent(lines_rid[p_line]);
  634. }
  635. float TextParagraph::get_line_width(int p_line) const {
  636. _THREAD_SAFE_METHOD_
  637. const_cast<TextParagraph *>(this)->_shape_lines();
  638. ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)lines_rid.size(), 0.f);
  639. return TS->shaped_text_get_width(lines_rid[p_line]);
  640. }
  641. float TextParagraph::get_line_underline_position(int p_line) const {
  642. _THREAD_SAFE_METHOD_
  643. const_cast<TextParagraph *>(this)->_shape_lines();
  644. ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)lines_rid.size(), 0.f);
  645. return TS->shaped_text_get_underline_position(lines_rid[p_line]);
  646. }
  647. float TextParagraph::get_line_underline_thickness(int p_line) const {
  648. _THREAD_SAFE_METHOD_
  649. const_cast<TextParagraph *>(this)->_shape_lines();
  650. ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)lines_rid.size(), 0.f);
  651. return TS->shaped_text_get_underline_thickness(lines_rid[p_line]);
  652. }
  653. Size2 TextParagraph::get_dropcap_size() const {
  654. _THREAD_SAFE_METHOD_
  655. return TS->shaped_text_get_size(dropcap_rid) + dropcap_margins.size + dropcap_margins.position;
  656. }
  657. int TextParagraph::get_dropcap_lines() const {
  658. return dropcap_lines;
  659. }
  660. void TextParagraph::draw(RID p_canvas, const Vector2 &p_pos, const Color &p_color, const Color &p_dc_color) const {
  661. _THREAD_SAFE_METHOD_
  662. const_cast<TextParagraph *>(this)->_shape_lines();
  663. Vector2 ofs = p_pos;
  664. float h_offset = 0.f;
  665. if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
  666. h_offset = TS->shaped_text_get_size(dropcap_rid).x + dropcap_margins.size.x + dropcap_margins.position.x;
  667. } else {
  668. h_offset = TS->shaped_text_get_size(dropcap_rid).y + dropcap_margins.size.y + dropcap_margins.position.y;
  669. }
  670. if (h_offset > 0) {
  671. // Draw dropcap.
  672. Vector2 dc_off = ofs;
  673. if (TS->shaped_text_get_inferred_direction(dropcap_rid) == TextServer::DIRECTION_RTL) {
  674. if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
  675. dc_off.x += width - h_offset;
  676. } else {
  677. dc_off.y += width - h_offset;
  678. }
  679. }
  680. TS->shaped_text_draw(dropcap_rid, p_canvas, dc_off + Vector2(0, TS->shaped_text_get_ascent(dropcap_rid) + dropcap_margins.size.y + dropcap_margins.position.y / 2), -1, -1, p_dc_color);
  681. }
  682. int lines_visible = (max_lines_visible >= 0) ? MIN(max_lines_visible, (int)lines_rid.size()) : (int)lines_rid.size();
  683. for (int i = 0; i < lines_visible; i++) {
  684. float l_width = width;
  685. if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
  686. ofs.x = p_pos.x;
  687. ofs.y += TS->shaped_text_get_ascent(lines_rid[i]);
  688. if (i <= dropcap_lines) {
  689. if (TS->shaped_text_get_inferred_direction(dropcap_rid) == TextServer::DIRECTION_LTR) {
  690. ofs.x -= h_offset;
  691. }
  692. l_width -= h_offset;
  693. }
  694. } else {
  695. ofs.y = p_pos.y;
  696. ofs.x += TS->shaped_text_get_ascent(lines_rid[i]);
  697. if (i <= dropcap_lines) {
  698. if (TS->shaped_text_get_inferred_direction(dropcap_rid) == TextServer::DIRECTION_LTR) {
  699. ofs.y -= h_offset;
  700. }
  701. l_width -= h_offset;
  702. }
  703. }
  704. float line_width = TS->shaped_text_get_width(lines_rid[i]);
  705. if (width > 0) {
  706. switch (alignment) {
  707. case HORIZONTAL_ALIGNMENT_FILL:
  708. if (TS->shaped_text_get_inferred_direction(lines_rid[i]) == TextServer::DIRECTION_RTL) {
  709. if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
  710. ofs.x += l_width - line_width;
  711. } else {
  712. ofs.y += l_width - line_width;
  713. }
  714. }
  715. break;
  716. case HORIZONTAL_ALIGNMENT_LEFT:
  717. break;
  718. case HORIZONTAL_ALIGNMENT_CENTER: {
  719. if (line_width <= l_width) {
  720. if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
  721. ofs.x += Math::floor((l_width - line_width) / 2.0);
  722. } else {
  723. ofs.y += Math::floor((l_width - line_width) / 2.0);
  724. }
  725. } else if (TS->shaped_text_get_inferred_direction(lines_rid[i]) == TextServer::DIRECTION_RTL) {
  726. if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
  727. ofs.x += l_width - line_width;
  728. } else {
  729. ofs.y += l_width - line_width;
  730. }
  731. }
  732. } break;
  733. case HORIZONTAL_ALIGNMENT_RIGHT: {
  734. if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
  735. ofs.x += l_width - line_width;
  736. } else {
  737. ofs.y += l_width - line_width;
  738. }
  739. } break;
  740. }
  741. }
  742. float clip_l;
  743. if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
  744. clip_l = MAX(0, p_pos.x - ofs.x);
  745. } else {
  746. clip_l = MAX(0, p_pos.y - ofs.y);
  747. }
  748. TS->shaped_text_draw(lines_rid[i], p_canvas, ofs, clip_l, clip_l + l_width, p_color);
  749. if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
  750. ofs.x = p_pos.x;
  751. ofs.y += TS->shaped_text_get_descent(lines_rid[i]) + line_spacing;
  752. } else {
  753. ofs.y = p_pos.y;
  754. ofs.x += TS->shaped_text_get_descent(lines_rid[i]) + line_spacing;
  755. }
  756. }
  757. }
  758. void TextParagraph::draw_outline(RID p_canvas, const Vector2 &p_pos, int p_outline_size, const Color &p_color, const Color &p_dc_color) const {
  759. _THREAD_SAFE_METHOD_
  760. const_cast<TextParagraph *>(this)->_shape_lines();
  761. Vector2 ofs = p_pos;
  762. float h_offset = 0.f;
  763. if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
  764. h_offset = TS->shaped_text_get_size(dropcap_rid).x + dropcap_margins.size.x + dropcap_margins.position.x;
  765. } else {
  766. h_offset = TS->shaped_text_get_size(dropcap_rid).y + dropcap_margins.size.y + dropcap_margins.position.y;
  767. }
  768. if (h_offset > 0) {
  769. // Draw dropcap.
  770. Vector2 dc_off = ofs;
  771. if (TS->shaped_text_get_inferred_direction(dropcap_rid) == TextServer::DIRECTION_RTL) {
  772. if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
  773. dc_off.x += width - h_offset;
  774. } else {
  775. dc_off.y += width - h_offset;
  776. }
  777. }
  778. TS->shaped_text_draw_outline(dropcap_rid, p_canvas, dc_off + Vector2(dropcap_margins.position.x, TS->shaped_text_get_ascent(dropcap_rid) + dropcap_margins.position.y), -1, -1, p_outline_size, p_dc_color);
  779. }
  780. for (int i = 0; i < (int)lines_rid.size(); i++) {
  781. float l_width = width;
  782. if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
  783. ofs.x = p_pos.x;
  784. ofs.y += TS->shaped_text_get_ascent(lines_rid[i]);
  785. if (i <= dropcap_lines) {
  786. if (TS->shaped_text_get_inferred_direction(dropcap_rid) == TextServer::DIRECTION_LTR) {
  787. ofs.x -= h_offset;
  788. }
  789. l_width -= h_offset;
  790. }
  791. } else {
  792. ofs.y = p_pos.y;
  793. ofs.x += TS->shaped_text_get_ascent(lines_rid[i]);
  794. if (i <= dropcap_lines) {
  795. if (TS->shaped_text_get_inferred_direction(dropcap_rid) == TextServer::DIRECTION_LTR) {
  796. ofs.y -= h_offset;
  797. }
  798. l_width -= h_offset;
  799. }
  800. }
  801. float length = TS->shaped_text_get_width(lines_rid[i]);
  802. if (width > 0) {
  803. switch (alignment) {
  804. case HORIZONTAL_ALIGNMENT_FILL:
  805. if (TS->shaped_text_get_inferred_direction(lines_rid[i]) == TextServer::DIRECTION_RTL) {
  806. if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
  807. ofs.x += l_width - length;
  808. } else {
  809. ofs.y += l_width - length;
  810. }
  811. }
  812. break;
  813. case HORIZONTAL_ALIGNMENT_LEFT:
  814. break;
  815. case HORIZONTAL_ALIGNMENT_CENTER: {
  816. if (length <= l_width) {
  817. if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
  818. ofs.x += Math::floor((l_width - length) / 2.0);
  819. } else {
  820. ofs.y += Math::floor((l_width - length) / 2.0);
  821. }
  822. } else if (TS->shaped_text_get_inferred_direction(lines_rid[i]) == TextServer::DIRECTION_RTL) {
  823. if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
  824. ofs.x += l_width - length;
  825. } else {
  826. ofs.y += l_width - length;
  827. }
  828. }
  829. } break;
  830. case HORIZONTAL_ALIGNMENT_RIGHT: {
  831. if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
  832. ofs.x += l_width - length;
  833. } else {
  834. ofs.y += l_width - length;
  835. }
  836. } break;
  837. }
  838. }
  839. float clip_l;
  840. if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
  841. clip_l = MAX(0, p_pos.x - ofs.x);
  842. } else {
  843. clip_l = MAX(0, p_pos.y - ofs.y);
  844. }
  845. TS->shaped_text_draw_outline(lines_rid[i], p_canvas, ofs, clip_l, clip_l + l_width, p_outline_size, p_color);
  846. if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
  847. ofs.x = p_pos.x;
  848. ofs.y += TS->shaped_text_get_descent(lines_rid[i]) + line_spacing;
  849. } else {
  850. ofs.y = p_pos.y;
  851. ofs.x += TS->shaped_text_get_descent(lines_rid[i]) + line_spacing;
  852. }
  853. }
  854. }
  855. int TextParagraph::hit_test(const Point2 &p_coords) const {
  856. _THREAD_SAFE_METHOD_
  857. const_cast<TextParagraph *>(this)->_shape_lines();
  858. Vector2 ofs;
  859. if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
  860. if (ofs.y < 0) {
  861. return 0;
  862. }
  863. } else {
  864. if (ofs.x < 0) {
  865. return 0;
  866. }
  867. }
  868. for (const RID &line_rid : lines_rid) {
  869. if (TS->shaped_text_get_orientation(line_rid) == TextServer::ORIENTATION_HORIZONTAL) {
  870. if ((p_coords.y >= ofs.y) && (p_coords.y <= ofs.y + TS->shaped_text_get_size(line_rid).y)) {
  871. return TS->shaped_text_hit_test_position(line_rid, p_coords.x);
  872. }
  873. ofs.y += TS->shaped_text_get_size(line_rid).y + line_spacing;
  874. } else {
  875. if ((p_coords.x >= ofs.x) && (p_coords.x <= ofs.x + TS->shaped_text_get_size(line_rid).x)) {
  876. return TS->shaped_text_hit_test_position(line_rid, p_coords.y);
  877. }
  878. ofs.y += TS->shaped_text_get_size(line_rid).x + line_spacing;
  879. }
  880. }
  881. return TS->shaped_text_get_range(rid).y;
  882. }
  883. bool TextParagraph::is_dirty() {
  884. return lines_dirty;
  885. }
  886. void TextParagraph::draw_dropcap(RID p_canvas, const Vector2 &p_pos, const Color &p_color) const {
  887. _THREAD_SAFE_METHOD_
  888. Vector2 ofs = p_pos;
  889. float h_offset = 0.f;
  890. if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
  891. h_offset = TS->shaped_text_get_size(dropcap_rid).x + dropcap_margins.size.x + dropcap_margins.position.x;
  892. } else {
  893. h_offset = TS->shaped_text_get_size(dropcap_rid).y + dropcap_margins.size.y + dropcap_margins.position.y;
  894. }
  895. if (h_offset > 0) {
  896. // Draw dropcap.
  897. if (TS->shaped_text_get_inferred_direction(dropcap_rid) == TextServer::DIRECTION_RTL) {
  898. if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
  899. ofs.x += width - h_offset;
  900. } else {
  901. ofs.y += width - h_offset;
  902. }
  903. }
  904. TS->shaped_text_draw(dropcap_rid, p_canvas, ofs + Vector2(dropcap_margins.position.x, TS->shaped_text_get_ascent(dropcap_rid) + dropcap_margins.position.y), -1, -1, p_color);
  905. }
  906. }
  907. void TextParagraph::draw_dropcap_outline(RID p_canvas, const Vector2 &p_pos, int p_outline_size, const Color &p_color) const {
  908. _THREAD_SAFE_METHOD_
  909. Vector2 ofs = p_pos;
  910. float h_offset = 0.f;
  911. if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
  912. h_offset = TS->shaped_text_get_size(dropcap_rid).x + dropcap_margins.size.x + dropcap_margins.position.x;
  913. } else {
  914. h_offset = TS->shaped_text_get_size(dropcap_rid).y + dropcap_margins.size.y + dropcap_margins.position.y;
  915. }
  916. if (h_offset > 0) {
  917. // Draw dropcap.
  918. if (TS->shaped_text_get_inferred_direction(dropcap_rid) == TextServer::DIRECTION_RTL) {
  919. if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
  920. ofs.x += width - h_offset;
  921. } else {
  922. ofs.y += width - h_offset;
  923. }
  924. }
  925. TS->shaped_text_draw_outline(dropcap_rid, p_canvas, ofs + Vector2(dropcap_margins.position.x, TS->shaped_text_get_ascent(dropcap_rid) + dropcap_margins.position.y), -1, -1, p_outline_size, p_color);
  926. }
  927. }
  928. void TextParagraph::draw_line(RID p_canvas, const Vector2 &p_pos, int p_line, const Color &p_color) const {
  929. _THREAD_SAFE_METHOD_
  930. const_cast<TextParagraph *>(this)->_shape_lines();
  931. ERR_FAIL_COND(p_line < 0 || p_line >= (int)lines_rid.size());
  932. Vector2 ofs = p_pos;
  933. if (TS->shaped_text_get_orientation(lines_rid[p_line]) == TextServer::ORIENTATION_HORIZONTAL) {
  934. ofs.y += TS->shaped_text_get_ascent(lines_rid[p_line]);
  935. } else {
  936. ofs.x += TS->shaped_text_get_ascent(lines_rid[p_line]);
  937. }
  938. return TS->shaped_text_draw(lines_rid[p_line], p_canvas, ofs, -1, -1, p_color);
  939. }
  940. void TextParagraph::draw_line_outline(RID p_canvas, const Vector2 &p_pos, int p_line, int p_outline_size, const Color &p_color) const {
  941. _THREAD_SAFE_METHOD_
  942. const_cast<TextParagraph *>(this)->_shape_lines();
  943. ERR_FAIL_COND(p_line < 0 || p_line >= (int)lines_rid.size());
  944. Vector2 ofs = p_pos;
  945. if (TS->shaped_text_get_orientation(lines_rid[p_line]) == TextServer::ORIENTATION_HORIZONTAL) {
  946. ofs.y += TS->shaped_text_get_ascent(lines_rid[p_line]);
  947. } else {
  948. ofs.x += TS->shaped_text_get_ascent(lines_rid[p_line]);
  949. }
  950. return TS->shaped_text_draw_outline(lines_rid[p_line], p_canvas, ofs, -1, -1, p_outline_size, p_color);
  951. }
  952. TextParagraph::TextParagraph(const String &p_text, const Ref<Font> &p_font, int p_font_size, const String &p_language, float p_width, TextServer::Direction p_direction, TextServer::Orientation p_orientation) {
  953. rid = TS->create_shaped_text(p_direction, p_orientation);
  954. if (p_font.is_valid()) {
  955. TS->shaped_text_add_string(rid, p_text, p_font->get_rids(), p_font_size, p_font->get_opentype_features(), p_language);
  956. }
  957. width = p_width;
  958. }
  959. TextParagraph::TextParagraph() {
  960. rid = TS->create_shaped_text();
  961. dropcap_rid = TS->create_shaped_text();
  962. }
  963. TextParagraph::~TextParagraph() {
  964. for (const RID &line_rid : lines_rid) {
  965. TS->free_rid(line_rid);
  966. }
  967. lines_rid.clear();
  968. TS->free_rid(rid);
  969. TS->free_rid(dropcap_rid);
  970. }