text_paragraph.cpp 39 KB

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