label.cpp 57 KB

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