label.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  1. /**************************************************************************/
  2. /* label.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 "label.h"
  31. #include "scene/gui/container.h"
  32. #include "scene/theme/theme_db.h"
  33. #include "servers/text_server.h"
  34. void Label::set_autowrap_mode(TextServer::AutowrapMode p_mode) {
  35. if (autowrap_mode == p_mode) {
  36. return;
  37. }
  38. autowrap_mode = p_mode;
  39. for (Paragraph &para : paragraphs) {
  40. para.lines_dirty = true;
  41. }
  42. queue_redraw();
  43. update_configuration_warnings();
  44. if (clip || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  45. update_minimum_size();
  46. }
  47. }
  48. TextServer::AutowrapMode Label::get_autowrap_mode() const {
  49. return autowrap_mode;
  50. }
  51. void Label::set_autowrap_trim_flags(BitField<TextServer::LineBreakFlag> p_flags) {
  52. if (autowrap_flags_trim == (p_flags & TextServer::BREAK_TRIM_MASK)) {
  53. return;
  54. }
  55. autowrap_flags_trim = p_flags & TextServer::BREAK_TRIM_MASK;
  56. for (Paragraph &para : paragraphs) {
  57. para.lines_dirty = true;
  58. }
  59. queue_redraw();
  60. update_configuration_warnings();
  61. if (clip || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  62. update_minimum_size();
  63. }
  64. }
  65. BitField<TextServer::LineBreakFlag> Label::get_autowrap_trim_flags() const {
  66. return autowrap_flags_trim;
  67. }
  68. void Label::set_justification_flags(BitField<TextServer::JustificationFlag> p_flags) {
  69. if (jst_flags == p_flags) {
  70. return;
  71. }
  72. jst_flags = p_flags;
  73. for (Paragraph &para : paragraphs) {
  74. para.lines_dirty = true;
  75. }
  76. queue_redraw();
  77. }
  78. BitField<TextServer::JustificationFlag> Label::get_justification_flags() const {
  79. return jst_flags;
  80. }
  81. void Label::set_uppercase(bool p_uppercase) {
  82. if (uppercase == p_uppercase) {
  83. return;
  84. }
  85. uppercase = p_uppercase;
  86. text_dirty = true;
  87. queue_redraw();
  88. }
  89. bool Label::is_uppercase() const {
  90. return uppercase;
  91. }
  92. int Label::get_line_height(int p_line) const {
  93. Ref<Font> font = (settings.is_valid() && settings->get_font().is_valid()) ? settings->get_font() : theme_cache.font;
  94. int font_size = settings.is_valid() ? settings->get_font_size() : theme_cache.font_size;
  95. int font_h = font->get_height(font_size);
  96. if (p_line >= 0 && p_line < total_line_count) {
  97. RID rid = get_line_rid(p_line);
  98. double asc = TS->shaped_text_get_ascent(rid);
  99. double dsc = TS->shaped_text_get_descent(rid);
  100. if (asc + dsc < font_h) {
  101. double diff = font_h - (asc + dsc);
  102. asc += diff / 2;
  103. dsc += diff - (diff / 2);
  104. }
  105. return asc + dsc;
  106. } else if (total_line_count > 0) {
  107. int h = font_h;
  108. for (const Paragraph &para : paragraphs) {
  109. for (const RID &line_rid : para.lines_rid) {
  110. h = MAX(h, TS->shaped_text_get_size(line_rid).y);
  111. }
  112. }
  113. return h;
  114. } else {
  115. return font->get_height(font_size);
  116. }
  117. }
  118. void Label::_shape() const {
  119. Ref<StyleBox> style = theme_cache.normal_style;
  120. int width = (get_size().width - style->get_minimum_size().width);
  121. if (text_dirty) {
  122. for (Paragraph &para : paragraphs) {
  123. for (const RID &line_rid : para.lines_rid) {
  124. TS->free_rid(line_rid);
  125. }
  126. para.lines_rid.clear();
  127. TS->free_rid(para.text_rid);
  128. }
  129. paragraphs.clear();
  130. String txt = (uppercase) ? TS->string_to_upper(xl_text, language) : xl_text;
  131. if (visible_chars >= 0 && visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING) {
  132. txt = txt.substr(0, visible_chars);
  133. }
  134. String ps = paragraph_separator.c_unescape();
  135. Vector<String> para_text = txt.split(ps);
  136. int start = 0;
  137. for (const String &str : para_text) {
  138. Paragraph para;
  139. para.text_rid = TS->create_shaped_text();
  140. para.text = str;
  141. para.start = start;
  142. start += str.length() + ps.length();
  143. paragraphs.push_back(para);
  144. }
  145. text_dirty = false;
  146. }
  147. total_line_count = 0;
  148. for (Paragraph &para : paragraphs) {
  149. if (para.dirty || font_dirty) {
  150. if (para.dirty) {
  151. TS->shaped_text_clear(para.text_rid);
  152. }
  153. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  154. TS->shaped_text_set_direction(para.text_rid, is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  155. } else {
  156. TS->shaped_text_set_direction(para.text_rid, (TextServer::Direction)text_direction);
  157. }
  158. const Ref<Font> &font = (settings.is_valid() && settings->get_font().is_valid()) ? settings->get_font() : theme_cache.font;
  159. int font_size = settings.is_valid() ? settings->get_font_size() : theme_cache.font_size;
  160. ERR_FAIL_COND(font.is_null());
  161. if (para.dirty) {
  162. TS->shaped_text_add_string(para.text_rid, para.text, font->get_rids(), font_size, font->get_opentype_features(), language);
  163. } else {
  164. int spans = TS->shaped_get_span_count(para.text_rid);
  165. for (int i = 0; i < spans; i++) {
  166. TS->shaped_set_span_update_font(para.text_rid, i, font->get_rids(), font_size, font->get_opentype_features());
  167. }
  168. }
  169. TS->shaped_text_set_bidi_override(para.text_rid, structured_text_parser(st_parser, st_args, para.text));
  170. if (!tab_stops.is_empty()) {
  171. TS->shaped_text_tab_align(para.text_rid, tab_stops);
  172. }
  173. para.dirty = false;
  174. para.lines_dirty = true;
  175. }
  176. if (para.lines_dirty) {
  177. for (const RID &line_rid : para.lines_rid) {
  178. TS->free_rid(line_rid);
  179. }
  180. para.lines_rid.clear();
  181. BitField<TextServer::LineBreakFlag> autowrap_flags = TextServer::BREAK_MANDATORY;
  182. switch (autowrap_mode) {
  183. case TextServer::AUTOWRAP_WORD_SMART:
  184. autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_ADAPTIVE | TextServer::BREAK_MANDATORY;
  185. break;
  186. case TextServer::AUTOWRAP_WORD:
  187. autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_MANDATORY;
  188. break;
  189. case TextServer::AUTOWRAP_ARBITRARY:
  190. autowrap_flags = TextServer::BREAK_GRAPHEME_BOUND | TextServer::BREAK_MANDATORY;
  191. break;
  192. case TextServer::AUTOWRAP_OFF:
  193. break;
  194. }
  195. autowrap_flags = autowrap_flags | autowrap_flags_trim;
  196. PackedInt32Array line_breaks = TS->shaped_text_get_line_breaks(para.text_rid, width, 0, autowrap_flags);
  197. for (int i = 0; i < line_breaks.size(); i = i + 2) {
  198. RID line = TS->shaped_text_substr(para.text_rid, line_breaks[i], line_breaks[i + 1] - line_breaks[i]);
  199. if (!tab_stops.is_empty()) {
  200. TS->shaped_text_tab_align(line, tab_stops);
  201. }
  202. para.lines_rid.push_back(line);
  203. }
  204. }
  205. total_line_count += para.lines_rid.size();
  206. }
  207. dirty = false;
  208. font_dirty = false;
  209. if (xl_text.length() == 0) {
  210. minsize = Size2(1, get_line_height());
  211. return;
  212. }
  213. int visible_lines = get_visible_line_count();
  214. bool lines_hidden = visible_lines > 0 && visible_lines < total_line_count;
  215. int line_index = 0;
  216. if (autowrap_mode == TextServer::AUTOWRAP_OFF) {
  217. minsize.width = 0.0f;
  218. }
  219. for (Paragraph &para : paragraphs) {
  220. if (autowrap_mode == TextServer::AUTOWRAP_OFF) {
  221. for (const RID &line_rid : para.lines_rid) {
  222. if (minsize.width < TS->shaped_text_get_size(line_rid).x) {
  223. minsize.width = TS->shaped_text_get_size(line_rid).x;
  224. }
  225. }
  226. }
  227. if (para.lines_dirty) {
  228. BitField<TextServer::TextOverrunFlag> overrun_flags = TextServer::OVERRUN_NO_TRIM;
  229. switch (overrun_behavior) {
  230. case TextServer::OVERRUN_TRIM_WORD_ELLIPSIS_FORCE: {
  231. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  232. overrun_flags.set_flag(TextServer::OVERRUN_TRIM_WORD_ONLY);
  233. overrun_flags.set_flag(TextServer::OVERRUN_ADD_ELLIPSIS);
  234. overrun_flags.set_flag(TextServer::OVERRUN_ENFORCE_ELLIPSIS);
  235. } break;
  236. case TextServer::OVERRUN_TRIM_ELLIPSIS_FORCE: {
  237. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  238. overrun_flags.set_flag(TextServer::OVERRUN_ADD_ELLIPSIS);
  239. overrun_flags.set_flag(TextServer::OVERRUN_ENFORCE_ELLIPSIS);
  240. } break;
  241. case TextServer::OVERRUN_TRIM_WORD_ELLIPSIS:
  242. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  243. overrun_flags.set_flag(TextServer::OVERRUN_TRIM_WORD_ONLY);
  244. overrun_flags.set_flag(TextServer::OVERRUN_ADD_ELLIPSIS);
  245. break;
  246. case TextServer::OVERRUN_TRIM_ELLIPSIS:
  247. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  248. overrun_flags.set_flag(TextServer::OVERRUN_ADD_ELLIPSIS);
  249. break;
  250. case TextServer::OVERRUN_TRIM_WORD:
  251. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  252. overrun_flags.set_flag(TextServer::OVERRUN_TRIM_WORD_ONLY);
  253. break;
  254. case TextServer::OVERRUN_TRIM_CHAR:
  255. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  256. break;
  257. case TextServer::OVERRUN_NO_TRIMMING:
  258. break;
  259. }
  260. // Fill after min_size calculation.
  261. BitField<TextServer::JustificationFlag> line_jst_flags = jst_flags;
  262. if (!tab_stops.is_empty()) {
  263. line_jst_flags.set_flag(TextServer::JUSTIFICATION_AFTER_LAST_TAB);
  264. }
  265. if (autowrap_mode != TextServer::AUTOWRAP_OFF) {
  266. if (lines_hidden) {
  267. overrun_flags.set_flag(TextServer::OVERRUN_ENFORCE_ELLIPSIS);
  268. }
  269. if (horizontal_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  270. int jst_to_line = para.lines_rid.size();
  271. if (para.lines_rid.size() == 1 && line_jst_flags.has_flag(TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE)) {
  272. jst_to_line = para.lines_rid.size();
  273. } else {
  274. if (line_jst_flags.has_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE)) {
  275. jst_to_line = para.lines_rid.size() - 1;
  276. }
  277. if (line_jst_flags.has_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE_WITH_VISIBLE_CHARS)) {
  278. for (int i = para.lines_rid.size() - 1; i >= 0; i--) {
  279. if (TS->shaped_text_has_visible_chars(para.lines_rid[i])) {
  280. jst_to_line = i;
  281. break;
  282. }
  283. }
  284. }
  285. }
  286. for (int i = 0; i < para.lines_rid.size(); i++) {
  287. if (i < jst_to_line) {
  288. TS->shaped_text_fit_to_width(para.lines_rid[i], width, line_jst_flags);
  289. } else if (i == (visible_lines - line_index - 1)) {
  290. TS->shaped_text_set_custom_ellipsis(para.lines_rid[i], (el_char.length() > 0) ? el_char[0] : 0x2026);
  291. TS->shaped_text_overrun_trim_to_width(para.lines_rid[i], width, overrun_flags);
  292. }
  293. }
  294. } else if (lines_hidden && (visible_lines - line_index - 1 >= 0) && (visible_lines - line_index - 1) < para.lines_rid.size()) {
  295. TS->shaped_text_set_custom_ellipsis(para.lines_rid[visible_lines - line_index - 1], (el_char.length() > 0) ? el_char[0] : 0x2026);
  296. TS->shaped_text_overrun_trim_to_width(para.lines_rid[visible_lines - line_index - 1], width, overrun_flags);
  297. }
  298. } else {
  299. // Autowrap disabled.
  300. int jst_to_line = para.lines_rid.size();
  301. if (para.lines_rid.size() == 1 && line_jst_flags.has_flag(TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE)) {
  302. jst_to_line = para.lines_rid.size();
  303. } else {
  304. if (line_jst_flags.has_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE)) {
  305. jst_to_line = para.lines_rid.size() - 1;
  306. }
  307. if (line_jst_flags.has_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE_WITH_VISIBLE_CHARS)) {
  308. for (int i = para.lines_rid.size() - 1; i >= 0; i--) {
  309. if (TS->shaped_text_has_visible_chars(para.lines_rid[i])) {
  310. jst_to_line = i;
  311. break;
  312. }
  313. }
  314. }
  315. }
  316. for (int i = 0; i < para.lines_rid.size(); i++) {
  317. if (i < jst_to_line && horizontal_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  318. TS->shaped_text_fit_to_width(para.lines_rid[i], width, line_jst_flags);
  319. overrun_flags.set_flag(TextServer::OVERRUN_JUSTIFICATION_AWARE);
  320. TS->shaped_text_set_custom_ellipsis(para.lines_rid[i], (el_char.length() > 0) ? el_char[0] : 0x2026);
  321. TS->shaped_text_overrun_trim_to_width(para.lines_rid[i], width, overrun_flags);
  322. TS->shaped_text_fit_to_width(para.lines_rid[i], width, line_jst_flags | TextServer::JUSTIFICATION_CONSTRAIN_ELLIPSIS);
  323. } else {
  324. TS->shaped_text_set_custom_ellipsis(para.lines_rid[i], (el_char.length() > 0) ? el_char[0] : 0x2026);
  325. TS->shaped_text_overrun_trim_to_width(para.lines_rid[i], width, overrun_flags);
  326. }
  327. }
  328. }
  329. para.lines_dirty = false;
  330. }
  331. line_index += para.lines_rid.size();
  332. }
  333. _update_visible();
  334. if (autowrap_mode == TextServer::AUTOWRAP_OFF || !clip || overrun_behavior == TextServer::OVERRUN_NO_TRIMMING) {
  335. const_cast<Label *>(this)->update_minimum_size();
  336. }
  337. }
  338. void Label::_update_visible() const {
  339. int line_spacing = settings.is_valid() ? settings->get_line_spacing() : theme_cache.line_spacing;
  340. int paragraph_spacing = settings.is_valid() ? settings->get_paragraph_spacing() : theme_cache.paragraph_spacing;
  341. Ref<StyleBox> style = theme_cache.normal_style;
  342. Ref<Font> font = (settings.is_valid() && settings->get_font().is_valid()) ? settings->get_font() : theme_cache.font;
  343. int font_size = settings.is_valid() ? settings->get_font_size() : theme_cache.font_size;
  344. int font_h = font->get_height(font_size);
  345. int lines_visible = total_line_count;
  346. if (max_lines_visible >= 0 && lines_visible > max_lines_visible) {
  347. lines_visible = max_lines_visible;
  348. }
  349. minsize.height = 0;
  350. int last_line = MIN(total_line_count, lines_visible + lines_skipped);
  351. int line_index = 0;
  352. for (const Paragraph &para : paragraphs) {
  353. if (line_index + para.lines_rid.size() <= lines_skipped) {
  354. line_index += para.lines_rid.size();
  355. } else {
  356. int start = (line_index < lines_skipped) ? lines_skipped - line_index : 0;
  357. int end = (line_index + para.lines_rid.size() < last_line) ? para.lines_rid.size() : last_line - line_index;
  358. if (end <= 0) {
  359. break;
  360. }
  361. for (int i = start; i < end; i++) {
  362. double asc = TS->shaped_text_get_ascent(para.lines_rid[i]);
  363. double dsc = TS->shaped_text_get_descent(para.lines_rid[i]);
  364. if (asc + dsc < font_h) {
  365. double diff = font_h - (asc + dsc);
  366. asc += diff / 2;
  367. dsc += diff - (diff / 2);
  368. }
  369. minsize.height += asc + dsc + line_spacing;
  370. }
  371. minsize.height += paragraph_spacing;
  372. line_index += para.lines_rid.size();
  373. }
  374. }
  375. if (minsize.height > 0) {
  376. minsize.height -= (line_spacing + paragraph_spacing);
  377. }
  378. }
  379. inline void draw_glyph(const Glyph &p_gl, const RID &p_canvas, const Color &p_font_color, const Vector2 &p_ofs) {
  380. if (p_gl.font_rid != RID()) {
  381. TS->font_draw_glyph(p_gl.font_rid, p_canvas, p_gl.font_size, p_ofs + Vector2(p_gl.x_off, p_gl.y_off), p_gl.index, p_font_color);
  382. } else if (((p_gl.flags & TextServer::GRAPHEME_IS_VIRTUAL) != TextServer::GRAPHEME_IS_VIRTUAL) && ((p_gl.flags & TextServer::GRAPHEME_IS_EMBEDDED_OBJECT) != TextServer::GRAPHEME_IS_EMBEDDED_OBJECT)) {
  383. TS->draw_hex_code_box(p_canvas, p_gl.font_size, p_ofs + Vector2(p_gl.x_off, p_gl.y_off), p_gl.index, p_font_color);
  384. }
  385. }
  386. inline void draw_glyph_shadow(const Glyph &p_gl, const RID &p_canvas, const Color &p_font_shadow_color, const Vector2 &p_ofs, const Vector2 &shadow_ofs) {
  387. if (p_gl.font_rid != RID()) {
  388. TS->font_draw_glyph(p_gl.font_rid, p_canvas, p_gl.font_size, p_ofs + Vector2(p_gl.x_off, p_gl.y_off) + shadow_ofs, p_gl.index, p_font_shadow_color);
  389. }
  390. }
  391. inline void draw_glyph_shadow_outline(const Glyph &p_gl, const RID &p_canvas, const Color &p_font_shadow_color, int p_shadow_outline_size, const Vector2 &p_ofs, const Vector2 &shadow_ofs) {
  392. if (p_gl.font_rid != RID()) {
  393. TS->font_draw_glyph_outline(p_gl.font_rid, p_canvas, p_gl.font_size, p_shadow_outline_size, p_ofs + Vector2(p_gl.x_off, p_gl.y_off) + shadow_ofs, p_gl.index, p_font_shadow_color);
  394. }
  395. }
  396. inline void draw_glyph_outline(const Glyph &p_gl, const RID &p_canvas, const Color &p_font_outline_color, int p_outline_size, const Vector2 &p_ofs) {
  397. if (p_gl.font_rid != RID()) {
  398. if (p_font_outline_color.a != 0.0 && p_outline_size > 0) {
  399. TS->font_draw_glyph_outline(p_gl.font_rid, p_canvas, p_gl.font_size, p_outline_size, p_ofs + Vector2(p_gl.x_off, p_gl.y_off), p_gl.index, p_font_outline_color);
  400. }
  401. }
  402. }
  403. void Label::_ensure_shaped() const {
  404. if (dirty || font_dirty || text_dirty) {
  405. _shape();
  406. } else {
  407. for (const Paragraph &para : paragraphs) {
  408. if (para.lines_dirty || para.dirty) {
  409. _shape();
  410. return;
  411. }
  412. }
  413. }
  414. }
  415. RID Label::get_line_rid(int p_line) const {
  416. if (p_line < 0 || p_line >= total_line_count) {
  417. return RID();
  418. }
  419. int line_index = 0;
  420. for (const Paragraph &para : paragraphs) {
  421. if (line_index + para.lines_rid.size() <= p_line) {
  422. line_index += para.lines_rid.size();
  423. } else {
  424. return para.lines_rid[p_line - line_index];
  425. }
  426. }
  427. return RID();
  428. }
  429. Rect2 Label::get_line_rect(int p_line) const {
  430. if (p_line < 0 || p_line >= total_line_count) {
  431. return Rect2();
  432. }
  433. int line_index = 0;
  434. for (int p = 0; p < paragraphs.size(); p++) {
  435. const Paragraph &para = paragraphs[p];
  436. if (line_index + para.lines_rid.size() <= p_line) {
  437. line_index += para.lines_rid.size();
  438. } else {
  439. return _get_line_rect(p, p_line - line_index);
  440. }
  441. }
  442. return Rect2();
  443. }
  444. Rect2 Label::_get_line_rect(int p_para, int p_line) const {
  445. // Returns a rect providing the line's horizontal offset and total size. To determine the vertical
  446. // offset, use r_offset and r_line_spacing from get_layout_data.
  447. bool rtl = TS->shaped_text_get_inferred_direction(paragraphs[p_para].text_rid) == TextServer::DIRECTION_RTL;
  448. bool rtl_layout = is_layout_rtl();
  449. Ref<StyleBox> style = theme_cache.normal_style;
  450. Ref<Font> font = (settings.is_valid() && settings->get_font().is_valid()) ? settings->get_font() : theme_cache.font;
  451. int font_size = settings.is_valid() ? settings->get_font_size() : theme_cache.font_size;
  452. int font_h = font->get_height(font_size);
  453. Size2 size = get_size();
  454. RID rid = paragraphs[p_para].lines_rid[p_line];
  455. Size2 line_size = TS->shaped_text_get_size(rid);
  456. double asc = TS->shaped_text_get_ascent(rid);
  457. double dsc = TS->shaped_text_get_descent(rid);
  458. if (asc + dsc < font_h) {
  459. double diff = font_h - (asc + dsc);
  460. asc += diff / 2;
  461. dsc += diff - (diff / 2);
  462. }
  463. line_size.y = asc + dsc;
  464. Vector2 offset;
  465. switch (horizontal_alignment) {
  466. case HORIZONTAL_ALIGNMENT_FILL:
  467. if (rtl && autowrap_mode != TextServer::AUTOWRAP_OFF) {
  468. offset.x = int(size.width - style->get_margin(SIDE_RIGHT) - line_size.width);
  469. } else {
  470. offset.x = style->get_offset().x;
  471. }
  472. break;
  473. case HORIZONTAL_ALIGNMENT_LEFT: {
  474. if (rtl_layout) {
  475. offset.x = int(size.width - style->get_margin(SIDE_RIGHT) - line_size.width);
  476. } else {
  477. offset.x = style->get_offset().x;
  478. }
  479. } break;
  480. case HORIZONTAL_ALIGNMENT_CENTER: {
  481. offset.x = int(size.width - line_size.width) / 2;
  482. } break;
  483. case HORIZONTAL_ALIGNMENT_RIGHT: {
  484. if (rtl_layout) {
  485. offset.x = style->get_offset().x;
  486. } else {
  487. offset.x = int(size.width - style->get_margin(SIDE_RIGHT) - line_size.width);
  488. }
  489. } break;
  490. }
  491. return Rect2(offset, line_size);
  492. }
  493. int Label::get_layout_data(Vector2 &r_offset, int &r_last_line, int &r_line_spacing) const {
  494. // Computes several common parameters involved in laying out and rendering text set to this label.
  495. // Only vertical margin is considered in r_offset: use get_line_rect to get the horizontal offset
  496. // for a given line of text.
  497. Size2 size = get_size();
  498. Ref<StyleBox> style = theme_cache.normal_style;
  499. Ref<Font> font = (settings.is_valid() && settings->get_font().is_valid()) ? settings->get_font() : theme_cache.font;
  500. int font_size = settings.is_valid() ? settings->get_font_size() : theme_cache.font_size;
  501. int font_h = font->get_height(font_size);
  502. int line_spacing = settings.is_valid() ? settings->get_line_spacing() : theme_cache.line_spacing;
  503. int paragraph_spacing = settings.is_valid() ? settings->get_paragraph_spacing() : theme_cache.paragraph_spacing;
  504. float total_h = 0.0;
  505. int lines_visible = 0;
  506. // Get number of lines to fit to the height.
  507. int line_index = 0;
  508. for (const Paragraph &para : paragraphs) {
  509. if (line_index + para.lines_rid.size() <= lines_skipped) {
  510. line_index += para.lines_rid.size();
  511. } else {
  512. int start = (line_index < lines_skipped) ? lines_skipped - line_index : 0;
  513. for (int i = start; i < para.lines_rid.size(); i++) {
  514. double asc = TS->shaped_text_get_ascent(para.lines_rid[i]);
  515. double dsc = TS->shaped_text_get_descent(para.lines_rid[i]);
  516. if (asc + dsc < font_h) {
  517. double diff = font_h - (asc + dsc);
  518. asc += diff / 2;
  519. dsc += diff - (diff / 2);
  520. }
  521. total_h += asc + dsc + line_spacing;
  522. if (total_h > Math::ceil(get_size().height - style->get_minimum_size().height + line_spacing)) {
  523. break;
  524. }
  525. lines_visible++;
  526. }
  527. total_h += paragraph_spacing;
  528. line_index += para.lines_rid.size();
  529. }
  530. }
  531. if (max_lines_visible >= 0 && lines_visible > max_lines_visible) {
  532. lines_visible = max_lines_visible;
  533. }
  534. r_last_line = MIN(total_line_count, lines_visible + lines_skipped);
  535. // Get real total height.
  536. int total_glyphs = 0;
  537. total_h = 0;
  538. line_index = 0;
  539. for (const Paragraph &para : paragraphs) {
  540. if (line_index + para.lines_rid.size() <= lines_skipped) {
  541. line_index += para.lines_rid.size();
  542. } else {
  543. int start = (line_index < lines_skipped) ? lines_skipped - line_index : 0;
  544. int end = (line_index + para.lines_rid.size() < r_last_line) ? para.lines_rid.size() : r_last_line - line_index;
  545. if (end <= 0) {
  546. break;
  547. }
  548. for (int i = start; i < end; i++) {
  549. double asc = TS->shaped_text_get_ascent(para.lines_rid[i]);
  550. double dsc = TS->shaped_text_get_descent(para.lines_rid[i]);
  551. if (asc + dsc < font_h) {
  552. double diff = font_h - (asc + dsc);
  553. asc += diff / 2;
  554. dsc += diff - (diff / 2);
  555. }
  556. total_h += asc + dsc + line_spacing;
  557. total_glyphs += TS->shaped_text_get_glyph_count(para.lines_rid[i]) + TS->shaped_text_get_ellipsis_glyph_count(para.lines_rid[i]);
  558. }
  559. total_h += paragraph_spacing;
  560. line_index += para.lines_rid.size();
  561. }
  562. }
  563. total_h += style->get_margin(SIDE_TOP) + style->get_margin(SIDE_BOTTOM);
  564. int vbegin = 0, vsep = 0;
  565. if (lines_visible > 0) {
  566. switch (vertical_alignment) {
  567. case VERTICAL_ALIGNMENT_TOP: {
  568. // Nothing.
  569. } break;
  570. case VERTICAL_ALIGNMENT_CENTER: {
  571. vbegin = (size.y - (total_h - line_spacing - paragraph_spacing)) / 2;
  572. vsep = 0;
  573. } break;
  574. case VERTICAL_ALIGNMENT_BOTTOM: {
  575. vbegin = size.y - (total_h - line_spacing - paragraph_spacing);
  576. vsep = 0;
  577. } break;
  578. case VERTICAL_ALIGNMENT_FILL: {
  579. vbegin = 0;
  580. if (lines_visible > 1) {
  581. vsep = (size.y - (total_h - line_spacing - paragraph_spacing)) / (lines_visible - 1);
  582. } else {
  583. vsep = 0;
  584. }
  585. } break;
  586. }
  587. }
  588. r_offset = { 0, style->get_offset().y + vbegin };
  589. r_line_spacing = line_spacing + vsep;
  590. return total_glyphs;
  591. }
  592. PackedStringArray Label::get_configuration_warnings() const {
  593. PackedStringArray warnings = Control::get_configuration_warnings();
  594. // FIXME: This is not ideal and the sizing model should be fixed,
  595. // but for now we have to warn about this impossible to resolve combination.
  596. // See GH-83546.
  597. if (is_inside_tree() && get_tree()->get_edited_scene_root() != this) {
  598. // If the Label happens to be the root node of the edited scene, we don't need
  599. // to check what its parent is. It's going to be some node from the editor tree
  600. // and it can be a container, but that makes no difference to the user.
  601. Container *parent_container = Object::cast_to<Container>(get_parent_control());
  602. if (parent_container && autowrap_mode != TextServer::AUTOWRAP_OFF && get_custom_minimum_size() == Size2()) {
  603. warnings.push_back(RTR("Labels with autowrapping enabled must have a custom minimum size configured to work correctly inside a container."));
  604. }
  605. }
  606. // Ensure that the font can render all of the required glyphs.
  607. Ref<Font> font;
  608. if (settings.is_valid()) {
  609. font = settings->get_font();
  610. }
  611. if (font.is_null()) {
  612. font = theme_cache.font;
  613. }
  614. if (font.is_valid()) {
  615. _ensure_shaped();
  616. for (const Paragraph &para : paragraphs) {
  617. const Glyph *glyph = TS->shaped_text_get_glyphs(para.text_rid);
  618. int64_t glyph_count = TS->shaped_text_get_glyph_count(para.text_rid);
  619. for (int64_t i = 0; i < glyph_count; i++) {
  620. if (glyph[i].font_rid == RID()) {
  621. warnings.push_back(RTR("The current font does not support rendering one or more characters used in this Label's text."));
  622. break;
  623. }
  624. }
  625. }
  626. }
  627. return warnings;
  628. }
  629. void Label::_notification(int p_what) {
  630. switch (p_what) {
  631. case NOTIFICATION_TRANSLATION_CHANGED: {
  632. String new_text = atr(text);
  633. if (new_text == xl_text) {
  634. return; // Nothing new.
  635. }
  636. xl_text = new_text;
  637. if (visible_ratio < 1) {
  638. visible_chars = get_total_character_count() * visible_ratio;
  639. }
  640. text_dirty = true;
  641. queue_redraw();
  642. update_configuration_warnings();
  643. } break;
  644. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  645. queue_redraw();
  646. } break;
  647. case NOTIFICATION_DRAW: {
  648. if (clip) {
  649. RenderingServer::get_singleton()->canvas_item_set_clip(get_canvas_item(), true);
  650. }
  651. // When a shaped text is invalidated by an external source, we want to reshape it.
  652. for (Paragraph &para : paragraphs) {
  653. if (!TS->shaped_text_is_ready(para.text_rid)) {
  654. para.dirty = true;
  655. }
  656. for (const RID &line_rid : para.lines_rid) {
  657. if (!TS->shaped_text_is_ready(line_rid)) {
  658. para.lines_dirty = true;
  659. break;
  660. }
  661. }
  662. }
  663. _ensure_shaped();
  664. RID ci = get_canvas_item();
  665. bool has_settings = settings.is_valid();
  666. Ref<StyleBox> style = theme_cache.normal_style;
  667. Ref<Font> font = (settings.is_valid() && settings->get_font().is_valid()) ? settings->get_font() : theme_cache.font;
  668. int font_size = settings.is_valid() ? settings->get_font_size() : theme_cache.font_size;
  669. int font_h = font->get_height(font_size);
  670. Color font_color = has_settings ? settings->get_font_color() : theme_cache.font_color;
  671. Color font_shadow_color = has_settings ? settings->get_shadow_color() : theme_cache.font_shadow_color;
  672. Point2 shadow_ofs = has_settings ? settings->get_shadow_offset() : theme_cache.font_shadow_offset;
  673. int paragraph_spacing = has_settings ? settings->get_paragraph_spacing() : theme_cache.paragraph_spacing;
  674. Color font_outline_color = has_settings ? settings->get_outline_color() : theme_cache.font_outline_color;
  675. int outline_size = has_settings ? settings->get_outline_size() : theme_cache.font_outline_size;
  676. int shadow_outline_size = has_settings ? settings->get_shadow_size() : theme_cache.font_shadow_outline_size;
  677. bool rtl_layout = is_layout_rtl();
  678. style->draw(ci, Rect2(Point2(0, 0), get_size()));
  679. bool trim_chars = (visible_chars >= 0) && (visible_chars_behavior == TextServer::VC_CHARS_AFTER_SHAPING);
  680. bool trim_glyphs_ltr = (visible_chars >= 0) && ((visible_chars_behavior == TextServer::VC_GLYPHS_LTR) || ((visible_chars_behavior == TextServer::VC_GLYPHS_AUTO) && !rtl_layout));
  681. bool trim_glyphs_rtl = (visible_chars >= 0) && ((visible_chars_behavior == TextServer::VC_GLYPHS_RTL) || ((visible_chars_behavior == TextServer::VC_GLYPHS_AUTO) && rtl_layout));
  682. Vector2 ofs;
  683. int line_spacing;
  684. int last_line;
  685. int total_glyphs = get_layout_data(ofs, last_line, line_spacing);
  686. int processed_glyphs = 0;
  687. int visible_glyphs = total_glyphs * visible_ratio;
  688. int line_index = 0;
  689. for (int p = 0; p < paragraphs.size(); p++) {
  690. const Paragraph &para = paragraphs[p];
  691. if (line_index + para.lines_rid.size() <= lines_skipped) {
  692. line_index += para.lines_rid.size();
  693. } else {
  694. int start = (line_index < lines_skipped) ? lines_skipped - line_index : 0;
  695. int end = (line_index + para.lines_rid.size() < last_line) ? para.lines_rid.size() : last_line - line_index;
  696. if (end <= 0) {
  697. break;
  698. }
  699. bool rtl = (TS->shaped_text_get_inferred_direction(para.text_rid) == TextServer::DIRECTION_RTL);
  700. for (int i = start; i < end; i++) {
  701. RID line_rid = para.lines_rid[i];
  702. Vector2 line_offset = _get_line_rect(p, i).position;
  703. ofs.x = line_offset.x;
  704. double asc = TS->shaped_text_get_ascent(line_rid);
  705. double dsc = TS->shaped_text_get_descent(line_rid);
  706. if (asc + dsc < font_h) {
  707. double diff = font_h - (asc + dsc);
  708. asc += diff / 2;
  709. dsc += diff - (diff / 2);
  710. }
  711. const Glyph *glyphs = TS->shaped_text_get_glyphs(line_rid);
  712. int gl_size = TS->shaped_text_get_glyph_count(line_rid);
  713. int ellipsis_pos = TS->shaped_text_get_ellipsis_pos(line_rid);
  714. int trim_pos = TS->shaped_text_get_trim_pos(line_rid);
  715. const Glyph *ellipsis_glyphs = TS->shaped_text_get_ellipsis_glyphs(line_rid);
  716. int ellipsis_gl_size = TS->shaped_text_get_ellipsis_glyph_count(line_rid);
  717. ofs.y += asc;
  718. // Draw shadow, outline and text. Note: Do not merge this into the single loop iteration, to prevent overlaps.
  719. int processed_glyphs_step = 0;
  720. for (int step = DRAW_STEP_SHADOW_OUTLINE; step < DRAW_STEP_MAX; step++) {
  721. if (step == DRAW_STEP_SHADOW_OUTLINE && (font_shadow_color.a == 0 || shadow_outline_size <= 0)) {
  722. continue;
  723. }
  724. if (step == DRAW_STEP_SHADOW && (font_shadow_color.a == 0)) {
  725. continue;
  726. }
  727. if (step == DRAW_STEP_OUTLINE && (outline_size <= 0 || font_outline_color.a == 0)) {
  728. continue;
  729. }
  730. processed_glyphs_step = processed_glyphs;
  731. Vector2 offset_step = ofs;
  732. // Draw RTL ellipsis string when necessary.
  733. if (rtl && ellipsis_pos >= 0) {
  734. for (int gl_idx = ellipsis_gl_size - 1; gl_idx >= 0; gl_idx--) {
  735. for (int j = 0; j < ellipsis_glyphs[gl_idx].repeat; j++) {
  736. bool skip = (trim_chars && ellipsis_glyphs[gl_idx].end + para.start > visible_chars) || (trim_glyphs_ltr && (processed_glyphs_step >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs_step < total_glyphs - visible_glyphs));
  737. if (!skip) {
  738. if (step == DRAW_STEP_SHADOW_OUTLINE) {
  739. draw_glyph_shadow_outline(ellipsis_glyphs[gl_idx], ci, font_shadow_color, shadow_outline_size, offset_step, shadow_ofs);
  740. } else if (step == DRAW_STEP_SHADOW) {
  741. draw_glyph_shadow(ellipsis_glyphs[gl_idx], ci, font_shadow_color, offset_step, shadow_ofs);
  742. } else if (step == DRAW_STEP_OUTLINE) {
  743. draw_glyph_outline(ellipsis_glyphs[gl_idx], ci, font_outline_color, outline_size, offset_step);
  744. } else if (step == DRAW_STEP_TEXT) {
  745. draw_glyph(ellipsis_glyphs[gl_idx], ci, font_color, offset_step);
  746. }
  747. }
  748. processed_glyphs_step++;
  749. offset_step.x += ellipsis_glyphs[gl_idx].advance;
  750. }
  751. }
  752. }
  753. // Draw main text.
  754. for (int j = 0; j < gl_size; j++) {
  755. // Trim when necessary.
  756. if (trim_pos >= 0) {
  757. if (rtl) {
  758. if (j < trim_pos) {
  759. continue;
  760. }
  761. } else {
  762. if (j >= trim_pos) {
  763. break;
  764. }
  765. }
  766. }
  767. for (int k = 0; k < glyphs[j].repeat; k++) {
  768. bool skip = (trim_chars && glyphs[j].end + para.start > visible_chars) || (trim_glyphs_ltr && (processed_glyphs_step >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs_step < total_glyphs - visible_glyphs));
  769. if (!skip) {
  770. if (step == DRAW_STEP_SHADOW_OUTLINE) {
  771. draw_glyph_shadow_outline(glyphs[j], ci, font_shadow_color, shadow_outline_size, offset_step, shadow_ofs);
  772. } else if (step == DRAW_STEP_SHADOW) {
  773. draw_glyph_shadow(glyphs[j], ci, font_shadow_color, offset_step, shadow_ofs);
  774. } else if (step == DRAW_STEP_OUTLINE) {
  775. draw_glyph_outline(glyphs[j], ci, font_outline_color, outline_size, offset_step);
  776. } else if (step == DRAW_STEP_TEXT) {
  777. draw_glyph(glyphs[j], ci, font_color, offset_step);
  778. }
  779. }
  780. processed_glyphs_step++;
  781. offset_step.x += glyphs[j].advance;
  782. }
  783. }
  784. // Draw LTR ellipsis string when necessary.
  785. if (!rtl && ellipsis_pos >= 0) {
  786. for (int gl_idx = 0; gl_idx < ellipsis_gl_size; gl_idx++) {
  787. for (int j = 0; j < ellipsis_glyphs[gl_idx].repeat; j++) {
  788. bool skip = (trim_chars && ellipsis_glyphs[gl_idx].end + para.start > visible_chars) || (trim_glyphs_ltr && (processed_glyphs_step >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs_step < total_glyphs - visible_glyphs));
  789. if (!skip) {
  790. if (step == DRAW_STEP_SHADOW_OUTLINE) {
  791. draw_glyph_shadow_outline(ellipsis_glyphs[gl_idx], ci, font_shadow_color, shadow_outline_size, offset_step, shadow_ofs);
  792. } else if (step == DRAW_STEP_SHADOW) {
  793. draw_glyph_shadow(ellipsis_glyphs[gl_idx], ci, font_shadow_color, offset_step, shadow_ofs);
  794. } else if (step == DRAW_STEP_OUTLINE) {
  795. draw_glyph_outline(ellipsis_glyphs[gl_idx], ci, font_outline_color, outline_size, offset_step);
  796. } else if (step == DRAW_STEP_TEXT) {
  797. draw_glyph(ellipsis_glyphs[gl_idx], ci, font_color, offset_step);
  798. }
  799. }
  800. processed_glyphs_step++;
  801. offset_step.x += ellipsis_glyphs[gl_idx].advance;
  802. }
  803. }
  804. }
  805. }
  806. processed_glyphs = processed_glyphs_step;
  807. ofs.y += dsc + line_spacing;
  808. }
  809. ofs.y += paragraph_spacing;
  810. line_index += para.lines_rid.size();
  811. }
  812. }
  813. } break;
  814. case NOTIFICATION_THEME_CHANGED: {
  815. font_dirty = true;
  816. queue_redraw();
  817. } break;
  818. case NOTIFICATION_RESIZED: {
  819. for (Paragraph &para : paragraphs) {
  820. para.lines_dirty = true;
  821. }
  822. } break;
  823. }
  824. }
  825. Rect2 Label::get_character_bounds(int p_pos) const {
  826. _ensure_shaped();
  827. int paragraph_spacing = settings.is_valid() ? settings->get_paragraph_spacing() : theme_cache.paragraph_spacing;
  828. Ref<Font> font = (settings.is_valid() && settings->get_font().is_valid()) ? settings->get_font() : theme_cache.font;
  829. int font_size = settings.is_valid() ? settings->get_font_size() : theme_cache.font_size;
  830. int font_h = font->get_height(font_size);
  831. Vector2 ofs;
  832. int line_spacing;
  833. int last_line;
  834. get_layout_data(ofs, last_line, line_spacing);
  835. int line_index = 0;
  836. for (int p = 0; p < paragraphs.size(); p++) {
  837. const Paragraph &para = paragraphs[p];
  838. if (line_index + para.lines_rid.size() <= lines_skipped) {
  839. line_index += para.lines_rid.size();
  840. } else {
  841. int start = (line_index < lines_skipped) ? lines_skipped - line_index : 0;
  842. int end = (line_index + para.lines_rid.size() < last_line) ? para.lines_rid.size() : last_line - line_index;
  843. if (end <= 0) {
  844. break;
  845. }
  846. for (int i = start; i < end; i++) {
  847. RID line_rid = para.lines_rid[i];
  848. Rect2 line_rect = _get_line_rect(p, i);
  849. ofs.x = line_rect.position.x;
  850. int v_size = TS->shaped_text_get_glyph_count(line_rid);
  851. const Glyph *glyphs = TS->shaped_text_get_glyphs(line_rid);
  852. float gl_off = 0.0f;
  853. for (int j = 0; j < v_size; j++) {
  854. if ((glyphs[j].count > 0) && ((glyphs[j].index != 0) || ((glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE))) {
  855. if (p_pos >= glyphs[j].start + para.start && p_pos < glyphs[j].end + para.start) {
  856. float advance = 0.f;
  857. for (int k = 0; k < glyphs[j].count; k++) {
  858. advance += glyphs[j + k].advance;
  859. }
  860. Rect2 rect;
  861. rect.position = ofs + Vector2(gl_off, 0);
  862. rect.size = Vector2(advance, line_rect.size.y);
  863. return rect;
  864. }
  865. }
  866. gl_off += glyphs[j].advance * glyphs[j].repeat;
  867. }
  868. double asc = TS->shaped_text_get_ascent(line_rid);
  869. double dsc = TS->shaped_text_get_descent(line_rid);
  870. if (asc + dsc < font_h) {
  871. double diff = font_h - (asc + dsc);
  872. asc += diff / 2;
  873. dsc += diff - (diff / 2);
  874. }
  875. ofs.y += asc + dsc + line_spacing;
  876. }
  877. ofs.y += paragraph_spacing;
  878. line_index += para.lines_rid.size();
  879. }
  880. }
  881. return Rect2();
  882. }
  883. Size2 Label::get_minimum_size() const {
  884. _ensure_shaped();
  885. Size2 min_size = minsize;
  886. const Ref<Font> &font = (settings.is_valid() && settings->get_font().is_valid()) ? settings->get_font() : theme_cache.font;
  887. int font_size = settings.is_valid() ? settings->get_font_size() : theme_cache.font_size;
  888. min_size.height = MAX(min_size.height, font->get_height(font_size) + font->get_spacing(TextServer::SPACING_TOP) + font->get_spacing(TextServer::SPACING_BOTTOM));
  889. Size2 min_style = theme_cache.normal_style->get_minimum_size();
  890. if (autowrap_mode != TextServer::AUTOWRAP_OFF) {
  891. return Size2(1, (clip || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) ? 1 : min_size.height) + min_style;
  892. } else {
  893. if (clip || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  894. min_size.width = 1;
  895. }
  896. return min_size + min_style;
  897. }
  898. }
  899. #ifndef DISABLE_DEPRECATED
  900. bool Label::_set(const StringName &p_name, const Variant &p_value) {
  901. if (p_name == SNAME("valign")) {
  902. set_vertical_alignment((VerticalAlignment)p_value.operator int());
  903. return true;
  904. } else if (p_name == SNAME("align")) {
  905. set_horizontal_alignment((HorizontalAlignment)p_value.operator int());
  906. return true;
  907. }
  908. return false;
  909. }
  910. #endif
  911. int Label::get_line_count() const {
  912. if (!is_inside_tree()) {
  913. return 1;
  914. }
  915. _ensure_shaped();
  916. return total_line_count;
  917. }
  918. int Label::get_visible_line_count() const {
  919. Ref<StyleBox> style = theme_cache.normal_style;
  920. Ref<Font> font = (settings.is_valid() && settings->get_font().is_valid()) ? settings->get_font() : theme_cache.font;
  921. int font_size = settings.is_valid() ? settings->get_font_size() : theme_cache.font_size;
  922. int font_h = font->get_height(font_size);
  923. int line_spacing = settings.is_valid() ? settings->get_line_spacing() : theme_cache.line_spacing;
  924. int paragraph_spacing = settings.is_valid() ? settings->get_paragraph_spacing() : theme_cache.paragraph_spacing;
  925. int lines_visible = 0;
  926. float total_h = 0.0;
  927. int line_index = 0;
  928. for (const Paragraph &para : paragraphs) {
  929. if (line_index + para.lines_rid.size() <= lines_skipped) {
  930. line_index += para.lines_rid.size();
  931. } else {
  932. int start = (line_index < lines_skipped) ? lines_skipped - line_index : 0;
  933. for (int i = start; i < para.lines_rid.size(); i++) {
  934. double asc = TS->shaped_text_get_ascent(para.lines_rid[i]);
  935. double dsc = TS->shaped_text_get_descent(para.lines_rid[i]);
  936. if (asc + dsc < font_h) {
  937. double diff = font_h - (asc + dsc);
  938. asc += diff / 2;
  939. dsc += diff - (diff / 2);
  940. }
  941. total_h += asc + dsc + line_spacing;
  942. if (total_h > Math::ceil(get_size().height - style->get_minimum_size().height + line_spacing)) {
  943. break;
  944. }
  945. lines_visible++;
  946. }
  947. total_h += paragraph_spacing;
  948. line_index += para.lines_rid.size();
  949. }
  950. }
  951. if (max_lines_visible >= 0 && lines_visible > max_lines_visible) {
  952. lines_visible = max_lines_visible;
  953. }
  954. return lines_visible;
  955. }
  956. void Label::set_horizontal_alignment(HorizontalAlignment p_alignment) {
  957. ERR_FAIL_INDEX((int)p_alignment, 4);
  958. if (horizontal_alignment == p_alignment) {
  959. return;
  960. }
  961. if (horizontal_alignment == HORIZONTAL_ALIGNMENT_FILL || p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  962. for (Paragraph &para : paragraphs) {
  963. para.lines_dirty = true; // Reshape lines.
  964. }
  965. }
  966. horizontal_alignment = p_alignment;
  967. queue_redraw();
  968. }
  969. HorizontalAlignment Label::get_horizontal_alignment() const {
  970. return horizontal_alignment;
  971. }
  972. void Label::set_vertical_alignment(VerticalAlignment p_alignment) {
  973. ERR_FAIL_INDEX((int)p_alignment, 4);
  974. if (vertical_alignment == p_alignment) {
  975. return;
  976. }
  977. vertical_alignment = p_alignment;
  978. queue_redraw();
  979. }
  980. VerticalAlignment Label::get_vertical_alignment() const {
  981. return vertical_alignment;
  982. }
  983. void Label::set_text(const String &p_string) {
  984. if (text == p_string) {
  985. return;
  986. }
  987. text = p_string;
  988. xl_text = atr(p_string);
  989. text_dirty = true;
  990. if (visible_ratio < 1) {
  991. visible_chars = get_total_character_count() * visible_ratio;
  992. }
  993. queue_redraw();
  994. update_minimum_size();
  995. update_configuration_warnings();
  996. }
  997. void Label::_invalidate() {
  998. font_dirty = true;
  999. queue_redraw();
  1000. }
  1001. void Label::set_label_settings(const Ref<LabelSettings> &p_settings) {
  1002. if (settings != p_settings) {
  1003. if (settings.is_valid()) {
  1004. settings->disconnect_changed(callable_mp(this, &Label::_invalidate));
  1005. }
  1006. settings = p_settings;
  1007. if (settings.is_valid()) {
  1008. settings->connect_changed(callable_mp(this, &Label::_invalidate), CONNECT_REFERENCE_COUNTED);
  1009. }
  1010. _invalidate();
  1011. }
  1012. }
  1013. Ref<LabelSettings> Label::get_label_settings() const {
  1014. return settings;
  1015. }
  1016. void Label::set_text_direction(Control::TextDirection p_text_direction) {
  1017. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  1018. if (text_direction != p_text_direction) {
  1019. text_direction = p_text_direction;
  1020. for (Paragraph &para : paragraphs) {
  1021. para.dirty = true;
  1022. }
  1023. queue_redraw();
  1024. }
  1025. }
  1026. void Label::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) {
  1027. if (st_parser != p_parser) {
  1028. st_parser = p_parser;
  1029. for (Paragraph &para : paragraphs) {
  1030. para.dirty = true;
  1031. }
  1032. queue_redraw();
  1033. }
  1034. }
  1035. TextServer::StructuredTextParser Label::get_structured_text_bidi_override() const {
  1036. return st_parser;
  1037. }
  1038. void Label::set_structured_text_bidi_override_options(Array p_args) {
  1039. if (st_args == p_args) {
  1040. return;
  1041. }
  1042. st_args = p_args;
  1043. for (Paragraph &para : paragraphs) {
  1044. para.dirty = true;
  1045. }
  1046. queue_redraw();
  1047. }
  1048. Array Label::get_structured_text_bidi_override_options() const {
  1049. return st_args;
  1050. }
  1051. Control::TextDirection Label::get_text_direction() const {
  1052. return text_direction;
  1053. }
  1054. void Label::set_language(const String &p_language) {
  1055. if (language != p_language) {
  1056. language = p_language;
  1057. for (Paragraph &para : paragraphs) {
  1058. para.dirty = true;
  1059. }
  1060. queue_redraw();
  1061. }
  1062. }
  1063. String Label::get_language() const {
  1064. return language;
  1065. }
  1066. void Label::set_paragraph_separator(const String &p_paragraph_separator) {
  1067. if (paragraph_separator != p_paragraph_separator) {
  1068. paragraph_separator = p_paragraph_separator;
  1069. text_dirty = true;
  1070. queue_redraw();
  1071. }
  1072. }
  1073. String Label::get_paragraph_separator() const {
  1074. return paragraph_separator;
  1075. }
  1076. void Label::set_clip_text(bool p_clip) {
  1077. if (clip == p_clip) {
  1078. return;
  1079. }
  1080. clip = p_clip;
  1081. queue_redraw();
  1082. update_minimum_size();
  1083. }
  1084. bool Label::is_clipping_text() const {
  1085. return clip;
  1086. }
  1087. void Label::set_tab_stops(const PackedFloat32Array &p_tab_stops) {
  1088. if (tab_stops != p_tab_stops) {
  1089. tab_stops = p_tab_stops;
  1090. for (Paragraph &para : paragraphs) {
  1091. para.dirty = true;
  1092. }
  1093. queue_redraw();
  1094. }
  1095. }
  1096. PackedFloat32Array Label::get_tab_stops() const {
  1097. return tab_stops;
  1098. }
  1099. void Label::set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior) {
  1100. if (overrun_behavior == p_behavior) {
  1101. return;
  1102. }
  1103. overrun_behavior = p_behavior;
  1104. for (Paragraph &para : paragraphs) {
  1105. para.lines_dirty = true;
  1106. }
  1107. queue_redraw();
  1108. if (clip || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  1109. update_minimum_size();
  1110. }
  1111. }
  1112. TextServer::OverrunBehavior Label::get_text_overrun_behavior() const {
  1113. return overrun_behavior;
  1114. }
  1115. void Label::set_ellipsis_char(const String &p_char) {
  1116. String c = p_char;
  1117. if (c.length() > 1) {
  1118. WARN_PRINT("Ellipsis must be exactly one character long (" + itos(c.length()) + " characters given).");
  1119. c = c.left(1);
  1120. }
  1121. if (el_char == c) {
  1122. return;
  1123. }
  1124. el_char = c;
  1125. for (Paragraph &para : paragraphs) {
  1126. para.lines_dirty = true;
  1127. }
  1128. queue_redraw();
  1129. if (clip || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  1130. update_minimum_size();
  1131. }
  1132. }
  1133. String Label::get_ellipsis_char() const {
  1134. return el_char;
  1135. }
  1136. String Label::get_text() const {
  1137. return text;
  1138. }
  1139. void Label::set_visible_characters(int p_amount) {
  1140. if (visible_chars != p_amount) {
  1141. visible_chars = p_amount;
  1142. if (get_total_character_count() > 0) {
  1143. visible_ratio = (float)p_amount / (float)get_total_character_count();
  1144. } else {
  1145. visible_ratio = 1.0;
  1146. }
  1147. if (visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING) {
  1148. text_dirty = true;
  1149. }
  1150. queue_redraw();
  1151. }
  1152. }
  1153. int Label::get_visible_characters() const {
  1154. return visible_chars;
  1155. }
  1156. void Label::set_visible_ratio(float p_ratio) {
  1157. if (visible_ratio != p_ratio) {
  1158. if (p_ratio >= 1.0) {
  1159. visible_chars = -1;
  1160. visible_ratio = 1.0;
  1161. } else if (p_ratio < 0.0) {
  1162. visible_chars = 0;
  1163. visible_ratio = 0.0;
  1164. } else {
  1165. visible_chars = get_total_character_count() * p_ratio;
  1166. visible_ratio = p_ratio;
  1167. }
  1168. if (visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING) {
  1169. text_dirty = true;
  1170. }
  1171. queue_redraw();
  1172. }
  1173. }
  1174. float Label::get_visible_ratio() const {
  1175. return visible_ratio;
  1176. }
  1177. TextServer::VisibleCharactersBehavior Label::get_visible_characters_behavior() const {
  1178. return visible_chars_behavior;
  1179. }
  1180. void Label::set_visible_characters_behavior(TextServer::VisibleCharactersBehavior p_behavior) {
  1181. if (visible_chars_behavior != p_behavior) {
  1182. if (visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING || p_behavior == TextServer::VC_CHARS_BEFORE_SHAPING) {
  1183. text_dirty = true;
  1184. }
  1185. visible_chars_behavior = p_behavior;
  1186. queue_redraw();
  1187. }
  1188. }
  1189. void Label::set_lines_skipped(int p_lines) {
  1190. ERR_FAIL_COND(p_lines < 0);
  1191. if (lines_skipped == p_lines) {
  1192. return;
  1193. }
  1194. lines_skipped = p_lines;
  1195. _update_visible();
  1196. queue_redraw();
  1197. }
  1198. int Label::get_lines_skipped() const {
  1199. return lines_skipped;
  1200. }
  1201. void Label::set_max_lines_visible(int p_lines) {
  1202. if (max_lines_visible == p_lines) {
  1203. return;
  1204. }
  1205. max_lines_visible = p_lines;
  1206. _update_visible();
  1207. queue_redraw();
  1208. }
  1209. int Label::get_max_lines_visible() const {
  1210. return max_lines_visible;
  1211. }
  1212. int Label::get_total_character_count() const {
  1213. return xl_text.length();
  1214. }
  1215. void Label::_bind_methods() {
  1216. ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &Label::set_horizontal_alignment);
  1217. ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &Label::get_horizontal_alignment);
  1218. ClassDB::bind_method(D_METHOD("set_vertical_alignment", "alignment"), &Label::set_vertical_alignment);
  1219. ClassDB::bind_method(D_METHOD("get_vertical_alignment"), &Label::get_vertical_alignment);
  1220. ClassDB::bind_method(D_METHOD("set_text", "text"), &Label::set_text);
  1221. ClassDB::bind_method(D_METHOD("get_text"), &Label::get_text);
  1222. ClassDB::bind_method(D_METHOD("set_label_settings", "settings"), &Label::set_label_settings);
  1223. ClassDB::bind_method(D_METHOD("get_label_settings"), &Label::get_label_settings);
  1224. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &Label::set_text_direction);
  1225. ClassDB::bind_method(D_METHOD("get_text_direction"), &Label::get_text_direction);
  1226. ClassDB::bind_method(D_METHOD("set_language", "language"), &Label::set_language);
  1227. ClassDB::bind_method(D_METHOD("get_language"), &Label::get_language);
  1228. ClassDB::bind_method(D_METHOD("set_paragraph_separator", "paragraph_separator"), &Label::set_paragraph_separator);
  1229. ClassDB::bind_method(D_METHOD("get_paragraph_separator"), &Label::get_paragraph_separator);
  1230. ClassDB::bind_method(D_METHOD("set_autowrap_mode", "autowrap_mode"), &Label::set_autowrap_mode);
  1231. ClassDB::bind_method(D_METHOD("get_autowrap_mode"), &Label::get_autowrap_mode);
  1232. ClassDB::bind_method(D_METHOD("set_autowrap_trim_flags", "autowrap_trim_flags"), &Label::set_autowrap_trim_flags);
  1233. ClassDB::bind_method(D_METHOD("get_autowrap_trim_flags"), &Label::get_autowrap_trim_flags);
  1234. ClassDB::bind_method(D_METHOD("set_justification_flags", "justification_flags"), &Label::set_justification_flags);
  1235. ClassDB::bind_method(D_METHOD("get_justification_flags"), &Label::get_justification_flags);
  1236. ClassDB::bind_method(D_METHOD("set_clip_text", "enable"), &Label::set_clip_text);
  1237. ClassDB::bind_method(D_METHOD("is_clipping_text"), &Label::is_clipping_text);
  1238. ClassDB::bind_method(D_METHOD("set_tab_stops", "tab_stops"), &Label::set_tab_stops);
  1239. ClassDB::bind_method(D_METHOD("get_tab_stops"), &Label::get_tab_stops);
  1240. ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "overrun_behavior"), &Label::set_text_overrun_behavior);
  1241. ClassDB::bind_method(D_METHOD("get_text_overrun_behavior"), &Label::get_text_overrun_behavior);
  1242. ClassDB::bind_method(D_METHOD("set_ellipsis_char", "char"), &Label::set_ellipsis_char);
  1243. ClassDB::bind_method(D_METHOD("get_ellipsis_char"), &Label::get_ellipsis_char);
  1244. ClassDB::bind_method(D_METHOD("set_uppercase", "enable"), &Label::set_uppercase);
  1245. ClassDB::bind_method(D_METHOD("is_uppercase"), &Label::is_uppercase);
  1246. ClassDB::bind_method(D_METHOD("get_line_height", "line"), &Label::get_line_height, DEFVAL(-1));
  1247. ClassDB::bind_method(D_METHOD("get_line_count"), &Label::get_line_count);
  1248. ClassDB::bind_method(D_METHOD("get_visible_line_count"), &Label::get_visible_line_count);
  1249. ClassDB::bind_method(D_METHOD("get_total_character_count"), &Label::get_total_character_count);
  1250. ClassDB::bind_method(D_METHOD("set_visible_characters", "amount"), &Label::set_visible_characters);
  1251. ClassDB::bind_method(D_METHOD("get_visible_characters"), &Label::get_visible_characters);
  1252. ClassDB::bind_method(D_METHOD("get_visible_characters_behavior"), &Label::get_visible_characters_behavior);
  1253. ClassDB::bind_method(D_METHOD("set_visible_characters_behavior", "behavior"), &Label::set_visible_characters_behavior);
  1254. ClassDB::bind_method(D_METHOD("set_visible_ratio", "ratio"), &Label::set_visible_ratio);
  1255. ClassDB::bind_method(D_METHOD("get_visible_ratio"), &Label::get_visible_ratio);
  1256. ClassDB::bind_method(D_METHOD("set_lines_skipped", "lines_skipped"), &Label::set_lines_skipped);
  1257. ClassDB::bind_method(D_METHOD("get_lines_skipped"), &Label::get_lines_skipped);
  1258. ClassDB::bind_method(D_METHOD("set_max_lines_visible", "lines_visible"), &Label::set_max_lines_visible);
  1259. ClassDB::bind_method(D_METHOD("get_max_lines_visible"), &Label::get_max_lines_visible);
  1260. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &Label::set_structured_text_bidi_override);
  1261. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &Label::get_structured_text_bidi_override);
  1262. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &Label::set_structured_text_bidi_override_options);
  1263. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &Label::get_structured_text_bidi_override_options);
  1264. ClassDB::bind_method(D_METHOD("get_character_bounds", "pos"), &Label::get_character_bounds);
  1265. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
  1266. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "label_settings", PROPERTY_HINT_RESOURCE_TYPE, "LabelSettings"), "set_label_settings", "get_label_settings");
  1267. ADD_PROPERTY(PropertyInfo(Variant::INT, "horizontal_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_horizontal_alignment", "get_horizontal_alignment");
  1268. ADD_PROPERTY(PropertyInfo(Variant::INT, "vertical_alignment", PROPERTY_HINT_ENUM, "Top,Center,Bottom,Fill"), "set_vertical_alignment", "get_vertical_alignment");
  1269. ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_mode", PROPERTY_HINT_ENUM, "Off,Arbitrary,Word,Word (Smart)"), "set_autowrap_mode", "get_autowrap_mode");
  1270. ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_trim_flags", PROPERTY_HINT_FLAGS, vformat("Trim Spaces After Break:%d,Trim Spaces Before Break:%d", TextServer::BREAK_TRIM_START_EDGE_SPACES, TextServer::BREAK_TRIM_END_EDGE_SPACES)), "set_autowrap_trim_flags", "get_autowrap_trim_flags");
  1271. ADD_PROPERTY(PropertyInfo(Variant::INT, "justification_flags", PROPERTY_HINT_FLAGS, "Kashida Justification:1,Word Justification:2,Justify Only After Last Tab:8,Skip Last Line:32,Skip Last Line With Visible Characters:64,Do Not Skip Single Line:128"), "set_justification_flags", "get_justification_flags");
  1272. ADD_PROPERTY(PropertyInfo(Variant::STRING, "paragraph_separator"), "set_paragraph_separator", "get_paragraph_separator");
  1273. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_text"), "set_clip_text", "is_clipping_text");
  1274. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_overrun_behavior", PROPERTY_HINT_ENUM, "Trim Nothing,Trim Characters,Trim Words,Ellipsis (6+ Characters),Word Ellipsis (6+ Characters),Ellipsis (Always),Word Ellipsis (Always)"), "set_text_overrun_behavior", "get_text_overrun_behavior");
  1275. ADD_PROPERTY(PropertyInfo(Variant::STRING, "ellipsis_char"), "set_ellipsis_char", "get_ellipsis_char");
  1276. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "uppercase"), "set_uppercase", "is_uppercase");
  1277. ADD_PROPERTY(PropertyInfo(Variant::PACKED_FLOAT32_ARRAY, "tab_stops"), "set_tab_stops", "get_tab_stops");
  1278. ADD_GROUP("Displayed Text", "");
  1279. ADD_PROPERTY(PropertyInfo(Variant::INT, "lines_skipped", PROPERTY_HINT_RANGE, "0,999,1"), "set_lines_skipped", "get_lines_skipped");
  1280. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_lines_visible", PROPERTY_HINT_RANGE, "-1,999,1"), "set_max_lines_visible", "get_max_lines_visible");
  1281. // Note: "visible_characters" and "visible_ratio" should be set after "text" to be correctly applied.
  1282. ADD_PROPERTY(PropertyInfo(Variant::INT, "visible_characters", PROPERTY_HINT_RANGE, "-1,128000,1"), "set_visible_characters", "get_visible_characters");
  1283. ADD_PROPERTY(PropertyInfo(Variant::INT, "visible_characters_behavior", PROPERTY_HINT_ENUM, "Characters Before Shaping,Characters After Shaping,Glyphs (Layout Direction),Glyphs (Left-to-Right),Glyphs (Right-to-Left)"), "set_visible_characters_behavior", "get_visible_characters_behavior");
  1284. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "visible_ratio", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_visible_ratio", "get_visible_ratio");
  1285. ADD_GROUP("BiDi", "");
  1286. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  1287. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  1288. ADD_PROPERTY(PropertyInfo(Variant::INT, "structured_text_bidi_override", PROPERTY_HINT_ENUM, "Default,URI,File,Email,List,None,Custom"), "set_structured_text_bidi_override", "get_structured_text_bidi_override");
  1289. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");
  1290. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Label, normal_style, "normal");
  1291. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Label, line_spacing);
  1292. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Label, paragraph_spacing);
  1293. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, Label, font);
  1294. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, Label, font_size);
  1295. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Label, font_color);
  1296. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Label, font_shadow_color);
  1297. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, Label, font_shadow_offset.x, "shadow_offset_x");
  1298. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, Label, font_shadow_offset.y, "shadow_offset_y");
  1299. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Label, font_outline_color);
  1300. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, Label, font_outline_size, "outline_size");
  1301. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, Label, font_shadow_outline_size, "shadow_outline_size");
  1302. }
  1303. Label::Label(const String &p_text) {
  1304. set_mouse_filter(MOUSE_FILTER_IGNORE);
  1305. set_text(p_text);
  1306. set_v_size_flags(SIZE_SHRINK_CENTER);
  1307. }
  1308. Label::~Label() {
  1309. for (Paragraph &para : paragraphs) {
  1310. for (const RID &line_rid : para.lines_rid) {
  1311. TS->free_rid(line_rid);
  1312. }
  1313. para.lines_rid.clear();
  1314. TS->free_rid(para.text_rid);
  1315. }
  1316. paragraphs.clear();
  1317. }