label.cpp 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  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, const Vector2 &p_ofs, int p_shadow_outline_size, 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, const Vector2 &p_ofs, int p_outline_size) {
  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. Vector<LabelSettings::StackedOutlineData> stacked_outline_datas = has_settings ? settings->get_stacked_outline_data() : Vector<LabelSettings::StackedOutlineData>();
  678. Vector<LabelSettings::StackedShadowData> stacked_shadow_datas = has_settings ? settings->get_stacked_shadow_data() : Vector<LabelSettings::StackedShadowData>();
  679. bool rtl_layout = is_layout_rtl();
  680. style->draw(ci, Rect2(Point2(0, 0), get_size()));
  681. bool trim_chars = (visible_chars >= 0) && (visible_chars_behavior == TextServer::VC_CHARS_AFTER_SHAPING);
  682. bool trim_glyphs_ltr = (visible_chars >= 0) && ((visible_chars_behavior == TextServer::VC_GLYPHS_LTR) || ((visible_chars_behavior == TextServer::VC_GLYPHS_AUTO) && !rtl_layout));
  683. bool trim_glyphs_rtl = (visible_chars >= 0) && ((visible_chars_behavior == TextServer::VC_GLYPHS_RTL) || ((visible_chars_behavior == TextServer::VC_GLYPHS_AUTO) && rtl_layout));
  684. Vector2 ofs;
  685. int line_spacing;
  686. int last_line;
  687. int total_glyphs = get_layout_data(ofs, last_line, line_spacing);
  688. int processed_glyphs = 0;
  689. int visible_glyphs = total_glyphs * visible_ratio;
  690. int line_index = 0;
  691. for (int p = 0; p < paragraphs.size(); p++) {
  692. const Paragraph &para = paragraphs[p];
  693. if (line_index + para.lines_rid.size() <= lines_skipped) {
  694. line_index += para.lines_rid.size();
  695. } else {
  696. int start = (line_index < lines_skipped) ? lines_skipped - line_index : 0;
  697. int end = (line_index + para.lines_rid.size() < last_line) ? para.lines_rid.size() : last_line - line_index;
  698. if (end <= 0) {
  699. break;
  700. }
  701. bool rtl = (TS->shaped_text_get_inferred_direction(para.text_rid) == TextServer::DIRECTION_RTL);
  702. for (int i = start; i < end; i++) {
  703. RID line_rid = para.lines_rid[i];
  704. Vector2 line_offset = _get_line_rect(p, i).position;
  705. ofs.x = line_offset.x;
  706. double asc = TS->shaped_text_get_ascent(line_rid);
  707. double dsc = TS->shaped_text_get_descent(line_rid);
  708. if (asc + dsc < font_h) {
  709. double diff = font_h - (asc + dsc);
  710. asc += diff / 2;
  711. dsc += diff - (diff / 2);
  712. }
  713. const Glyph *glyphs = TS->shaped_text_get_glyphs(line_rid);
  714. int gl_size = TS->shaped_text_get_glyph_count(line_rid);
  715. int ellipsis_pos = TS->shaped_text_get_ellipsis_pos(line_rid);
  716. int trim_pos = TS->shaped_text_get_trim_pos(line_rid);
  717. const Glyph *ellipsis_glyphs = TS->shaped_text_get_ellipsis_glyphs(line_rid);
  718. int ellipsis_gl_size = TS->shaped_text_get_ellipsis_glyph_count(line_rid);
  719. ofs.y += asc;
  720. // Draw text effects and main texts. Note: Do not merge this into the single loop iteration, to prevent overlaps.
  721. int processed_glyphs_step = 0;
  722. // Draw shadow outline.
  723. if (font_shadow_color.a != 0 && shadow_outline_size > 0) {
  724. 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);
  725. }
  726. // Draw shadow.
  727. if (font_shadow_color.a > 0) {
  728. 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);
  729. }
  730. // Draw stacked shadow.
  731. if (stacked_shadow_datas.size() != 0) {
  732. int draw_iterations = stacked_shadow_datas.size();
  733. for (int draw_iteration_index = draw_iterations - 1; draw_iteration_index >= 0; --draw_iteration_index) {
  734. LabelSettings::StackedShadowData stacked_shadow_data = stacked_shadow_datas[draw_iteration_index];
  735. if (stacked_shadow_data.outline_size > 0) {
  736. 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);
  737. }
  738. 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);
  739. }
  740. }
  741. // Draw stacked outline.
  742. if (stacked_outline_datas.size() != 0) {
  743. int stacked_outline_draw_size = outline_size;
  744. int draw_iterations = stacked_outline_datas.size();
  745. for (int j = 0; j < draw_iterations; j++) {
  746. int stacked_outline_size = stacked_outline_datas[j].size;
  747. if (stacked_outline_size <= 0) {
  748. continue;
  749. }
  750. stacked_outline_draw_size += stacked_outline_size;
  751. }
  752. for (int draw_iteration_index = draw_iterations - 1; draw_iteration_index >= 0; --draw_iteration_index) {
  753. LabelSettings::StackedOutlineData stacked_outline_data = stacked_outline_datas[draw_iteration_index];
  754. if (stacked_outline_data.size <= 0) {
  755. continue;
  756. }
  757. 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);
  758. stacked_outline_draw_size -= stacked_outline_data.size;
  759. }
  760. }
  761. // Draw outline.
  762. if (outline_size > 0 && font_outline_color.a != 0) {
  763. 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);
  764. }
  765. // Draw text.
  766. {
  767. 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);
  768. }
  769. processed_glyphs = processed_glyphs_step;
  770. ofs.y += dsc + line_spacing;
  771. }
  772. ofs.y += paragraph_spacing;
  773. line_index += para.lines_rid.size();
  774. }
  775. }
  776. } break;
  777. case NOTIFICATION_THEME_CHANGED: {
  778. font_dirty = true;
  779. queue_redraw();
  780. } break;
  781. case NOTIFICATION_RESIZED: {
  782. for (Paragraph &para : paragraphs) {
  783. para.lines_dirty = true;
  784. }
  785. } break;
  786. }
  787. }
  788. Rect2 Label::get_character_bounds(int p_pos) const {
  789. _ensure_shaped();
  790. int paragraph_spacing = settings.is_valid() ? settings->get_paragraph_spacing() : theme_cache.paragraph_spacing;
  791. Ref<Font> font = (settings.is_valid() && settings->get_font().is_valid()) ? settings->get_font() : theme_cache.font;
  792. int font_size = settings.is_valid() ? settings->get_font_size() : theme_cache.font_size;
  793. int font_h = font->get_height(font_size);
  794. Vector2 ofs;
  795. int line_spacing;
  796. int last_line;
  797. get_layout_data(ofs, last_line, line_spacing);
  798. int line_index = 0;
  799. for (int p = 0; p < paragraphs.size(); p++) {
  800. const Paragraph &para = paragraphs[p];
  801. if (line_index + para.lines_rid.size() <= lines_skipped) {
  802. line_index += para.lines_rid.size();
  803. } else {
  804. int start = (line_index < lines_skipped) ? lines_skipped - line_index : 0;
  805. int end = (line_index + para.lines_rid.size() < last_line) ? para.lines_rid.size() : last_line - line_index;
  806. if (end <= 0) {
  807. break;
  808. }
  809. for (int i = start; i < end; i++) {
  810. RID line_rid = para.lines_rid[i];
  811. Rect2 line_rect = _get_line_rect(p, i);
  812. ofs.x = line_rect.position.x;
  813. int v_size = TS->shaped_text_get_glyph_count(line_rid);
  814. const Glyph *glyphs = TS->shaped_text_get_glyphs(line_rid);
  815. float gl_off = 0.0f;
  816. for (int j = 0; j < v_size; j++) {
  817. if ((glyphs[j].count > 0) && ((glyphs[j].index != 0) || ((glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE))) {
  818. if (p_pos >= glyphs[j].start + para.start && p_pos < glyphs[j].end + para.start) {
  819. float advance = 0.f;
  820. for (int k = 0; k < glyphs[j].count; k++) {
  821. advance += glyphs[j + k].advance;
  822. }
  823. Rect2 rect;
  824. rect.position = ofs + Vector2(gl_off, 0);
  825. rect.size = Vector2(advance, line_rect.size.y);
  826. return rect;
  827. }
  828. }
  829. gl_off += glyphs[j].advance * glyphs[j].repeat;
  830. }
  831. double asc = TS->shaped_text_get_ascent(line_rid);
  832. double dsc = TS->shaped_text_get_descent(line_rid);
  833. if (asc + dsc < font_h) {
  834. double diff = font_h - (asc + dsc);
  835. asc += diff / 2;
  836. dsc += diff - (diff / 2);
  837. }
  838. ofs.y += asc + dsc + line_spacing;
  839. }
  840. ofs.y += paragraph_spacing;
  841. line_index += para.lines_rid.size();
  842. }
  843. }
  844. return Rect2();
  845. }
  846. Size2 Label::get_minimum_size() const {
  847. _ensure_shaped();
  848. Size2 min_size = minsize;
  849. const Ref<Font> &font = (settings.is_valid() && settings->get_font().is_valid()) ? settings->get_font() : theme_cache.font;
  850. int font_size = settings.is_valid() ? settings->get_font_size() : theme_cache.font_size;
  851. min_size.height = MAX(min_size.height, font->get_height(font_size));
  852. Size2 min_style = theme_cache.normal_style->get_minimum_size();
  853. if (autowrap_mode != TextServer::AUTOWRAP_OFF) {
  854. return Size2(1, (clip || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) ? 1 : min_size.height) + min_style;
  855. } else {
  856. if (clip || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  857. min_size.width = 1;
  858. }
  859. return min_size + min_style;
  860. }
  861. }
  862. #ifndef DISABLE_DEPRECATED
  863. bool Label::_set(const StringName &p_name, const Variant &p_value) {
  864. if (p_name == SNAME("valign")) {
  865. set_vertical_alignment((VerticalAlignment)p_value.operator int());
  866. return true;
  867. } else if (p_name == SNAME("align")) {
  868. set_horizontal_alignment((HorizontalAlignment)p_value.operator int());
  869. return true;
  870. }
  871. return false;
  872. }
  873. #endif
  874. int Label::get_line_count() const {
  875. if (!is_inside_tree()) {
  876. return 1;
  877. }
  878. _ensure_shaped();
  879. return total_line_count;
  880. }
  881. int Label::get_visible_line_count() const {
  882. Ref<StyleBox> style = theme_cache.normal_style;
  883. Ref<Font> font = (settings.is_valid() && settings->get_font().is_valid()) ? settings->get_font() : theme_cache.font;
  884. int font_size = settings.is_valid() ? settings->get_font_size() : theme_cache.font_size;
  885. int font_h = font->get_height(font_size);
  886. int line_spacing = settings.is_valid() ? settings->get_line_spacing() : theme_cache.line_spacing;
  887. int paragraph_spacing = settings.is_valid() ? settings->get_paragraph_spacing() : theme_cache.paragraph_spacing;
  888. int lines_visible = 0;
  889. float total_h = 0.0;
  890. int line_index = 0;
  891. for (const Paragraph &para : paragraphs) {
  892. if (line_index + para.lines_rid.size() <= lines_skipped) {
  893. line_index += para.lines_rid.size();
  894. } else {
  895. int start = (line_index < lines_skipped) ? lines_skipped - line_index : 0;
  896. for (int i = start; i < para.lines_rid.size(); i++) {
  897. double asc = TS->shaped_text_get_ascent(para.lines_rid[i]);
  898. double dsc = TS->shaped_text_get_descent(para.lines_rid[i]);
  899. if (asc + dsc < font_h) {
  900. double diff = font_h - (asc + dsc);
  901. asc += diff / 2;
  902. dsc += diff - (diff / 2);
  903. }
  904. total_h += asc + dsc + line_spacing;
  905. if (total_h > Math::ceil(get_size().height - style->get_minimum_size().height + line_spacing)) {
  906. break;
  907. }
  908. lines_visible++;
  909. }
  910. total_h += paragraph_spacing;
  911. line_index += para.lines_rid.size();
  912. }
  913. }
  914. if (max_lines_visible >= 0 && lines_visible > max_lines_visible) {
  915. lines_visible = max_lines_visible;
  916. }
  917. return lines_visible;
  918. }
  919. void Label::set_horizontal_alignment(HorizontalAlignment p_alignment) {
  920. ERR_FAIL_INDEX((int)p_alignment, 4);
  921. if (horizontal_alignment == p_alignment) {
  922. return;
  923. }
  924. if (horizontal_alignment == HORIZONTAL_ALIGNMENT_FILL || p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  925. for (Paragraph &para : paragraphs) {
  926. para.lines_dirty = true; // Reshape lines.
  927. }
  928. }
  929. horizontal_alignment = p_alignment;
  930. queue_redraw();
  931. }
  932. HorizontalAlignment Label::get_horizontal_alignment() const {
  933. return horizontal_alignment;
  934. }
  935. void Label::set_vertical_alignment(VerticalAlignment p_alignment) {
  936. ERR_FAIL_INDEX((int)p_alignment, 4);
  937. if (vertical_alignment == p_alignment) {
  938. return;
  939. }
  940. vertical_alignment = p_alignment;
  941. queue_redraw();
  942. }
  943. VerticalAlignment Label::get_vertical_alignment() const {
  944. return vertical_alignment;
  945. }
  946. void Label::set_text(const String &p_string) {
  947. if (text == p_string) {
  948. return;
  949. }
  950. text = p_string;
  951. xl_text = atr(p_string);
  952. text_dirty = true;
  953. if (visible_ratio < 1) {
  954. visible_chars = get_total_character_count() * visible_ratio;
  955. }
  956. queue_redraw();
  957. update_minimum_size();
  958. update_configuration_warnings();
  959. }
  960. void Label::_invalidate() {
  961. font_dirty = true;
  962. queue_redraw();
  963. }
  964. void Label::set_label_settings(const Ref<LabelSettings> &p_settings) {
  965. if (settings != p_settings) {
  966. if (settings.is_valid()) {
  967. settings->disconnect_changed(callable_mp(this, &Label::_invalidate));
  968. }
  969. settings = p_settings;
  970. if (settings.is_valid()) {
  971. settings->connect_changed(callable_mp(this, &Label::_invalidate), CONNECT_REFERENCE_COUNTED);
  972. }
  973. _invalidate();
  974. }
  975. }
  976. Ref<LabelSettings> Label::get_label_settings() const {
  977. return settings;
  978. }
  979. void Label::set_text_direction(Control::TextDirection p_text_direction) {
  980. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  981. if (text_direction != p_text_direction) {
  982. text_direction = p_text_direction;
  983. for (Paragraph &para : paragraphs) {
  984. para.dirty = true;
  985. }
  986. queue_redraw();
  987. }
  988. }
  989. void Label::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) {
  990. if (st_parser != p_parser) {
  991. st_parser = p_parser;
  992. for (Paragraph &para : paragraphs) {
  993. para.dirty = true;
  994. }
  995. queue_redraw();
  996. }
  997. }
  998. TextServer::StructuredTextParser Label::get_structured_text_bidi_override() const {
  999. return st_parser;
  1000. }
  1001. void Label::set_structured_text_bidi_override_options(Array p_args) {
  1002. if (st_args == p_args) {
  1003. return;
  1004. }
  1005. st_args = p_args;
  1006. for (Paragraph &para : paragraphs) {
  1007. para.dirty = true;
  1008. }
  1009. queue_redraw();
  1010. }
  1011. Array Label::get_structured_text_bidi_override_options() const {
  1012. return st_args;
  1013. }
  1014. Control::TextDirection Label::get_text_direction() const {
  1015. return text_direction;
  1016. }
  1017. void Label::set_language(const String &p_language) {
  1018. if (language != p_language) {
  1019. language = p_language;
  1020. for (Paragraph &para : paragraphs) {
  1021. para.dirty = true;
  1022. }
  1023. queue_redraw();
  1024. }
  1025. }
  1026. String Label::get_language() const {
  1027. return language;
  1028. }
  1029. void Label::set_paragraph_separator(const String &p_paragraph_separator) {
  1030. if (paragraph_separator != p_paragraph_separator) {
  1031. paragraph_separator = p_paragraph_separator;
  1032. text_dirty = true;
  1033. queue_redraw();
  1034. }
  1035. }
  1036. String Label::get_paragraph_separator() const {
  1037. return paragraph_separator;
  1038. }
  1039. void Label::set_clip_text(bool p_clip) {
  1040. if (clip == p_clip) {
  1041. return;
  1042. }
  1043. clip = p_clip;
  1044. queue_redraw();
  1045. update_minimum_size();
  1046. }
  1047. bool Label::is_clipping_text() const {
  1048. return clip;
  1049. }
  1050. void Label::set_tab_stops(const PackedFloat32Array &p_tab_stops) {
  1051. if (tab_stops != p_tab_stops) {
  1052. tab_stops = p_tab_stops;
  1053. for (Paragraph &para : paragraphs) {
  1054. para.dirty = true;
  1055. }
  1056. queue_redraw();
  1057. }
  1058. }
  1059. PackedFloat32Array Label::get_tab_stops() const {
  1060. return tab_stops;
  1061. }
  1062. void Label::set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior) {
  1063. if (overrun_behavior == p_behavior) {
  1064. return;
  1065. }
  1066. overrun_behavior = p_behavior;
  1067. for (Paragraph &para : paragraphs) {
  1068. para.lines_dirty = true;
  1069. }
  1070. queue_redraw();
  1071. if (clip || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  1072. update_minimum_size();
  1073. }
  1074. }
  1075. TextServer::OverrunBehavior Label::get_text_overrun_behavior() const {
  1076. return overrun_behavior;
  1077. }
  1078. void Label::set_ellipsis_char(const String &p_char) {
  1079. String c = p_char;
  1080. if (c.length() > 1) {
  1081. WARN_PRINT("Ellipsis must be exactly one character long (" + itos(c.length()) + " characters given).");
  1082. c = c.left(1);
  1083. }
  1084. if (el_char == c) {
  1085. return;
  1086. }
  1087. el_char = c;
  1088. for (Paragraph &para : paragraphs) {
  1089. para.lines_dirty = true;
  1090. }
  1091. queue_redraw();
  1092. if (clip || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  1093. update_minimum_size();
  1094. }
  1095. }
  1096. String Label::get_ellipsis_char() const {
  1097. return el_char;
  1098. }
  1099. String Label::get_text() const {
  1100. return text;
  1101. }
  1102. void Label::set_visible_characters(int p_amount) {
  1103. if (visible_chars != p_amount) {
  1104. visible_chars = p_amount;
  1105. if (get_total_character_count() > 0) {
  1106. visible_ratio = (float)p_amount / (float)get_total_character_count();
  1107. } else {
  1108. visible_ratio = 1.0;
  1109. }
  1110. if (visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING) {
  1111. text_dirty = true;
  1112. }
  1113. queue_redraw();
  1114. }
  1115. }
  1116. int Label::get_visible_characters() const {
  1117. return visible_chars;
  1118. }
  1119. void Label::set_visible_ratio(float p_ratio) {
  1120. if (visible_ratio != p_ratio) {
  1121. if (p_ratio >= 1.0) {
  1122. visible_chars = -1;
  1123. visible_ratio = 1.0;
  1124. } else if (p_ratio < 0.0) {
  1125. visible_chars = 0;
  1126. visible_ratio = 0.0;
  1127. } else {
  1128. visible_chars = get_total_character_count() * p_ratio;
  1129. visible_ratio = p_ratio;
  1130. }
  1131. if (visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING) {
  1132. text_dirty = true;
  1133. }
  1134. queue_redraw();
  1135. }
  1136. }
  1137. float Label::get_visible_ratio() const {
  1138. return visible_ratio;
  1139. }
  1140. TextServer::VisibleCharactersBehavior Label::get_visible_characters_behavior() const {
  1141. return visible_chars_behavior;
  1142. }
  1143. void Label::set_visible_characters_behavior(TextServer::VisibleCharactersBehavior p_behavior) {
  1144. if (visible_chars_behavior != p_behavior) {
  1145. if (visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING || p_behavior == TextServer::VC_CHARS_BEFORE_SHAPING) {
  1146. text_dirty = true;
  1147. }
  1148. visible_chars_behavior = p_behavior;
  1149. queue_redraw();
  1150. }
  1151. }
  1152. void Label::set_lines_skipped(int p_lines) {
  1153. ERR_FAIL_COND(p_lines < 0);
  1154. if (lines_skipped == p_lines) {
  1155. return;
  1156. }
  1157. lines_skipped = p_lines;
  1158. _update_visible();
  1159. queue_redraw();
  1160. }
  1161. int Label::get_lines_skipped() const {
  1162. return lines_skipped;
  1163. }
  1164. void Label::set_max_lines_visible(int p_lines) {
  1165. if (max_lines_visible == p_lines) {
  1166. return;
  1167. }
  1168. max_lines_visible = p_lines;
  1169. _update_visible();
  1170. queue_redraw();
  1171. }
  1172. int Label::get_max_lines_visible() const {
  1173. return max_lines_visible;
  1174. }
  1175. int Label::get_total_character_count() const {
  1176. return xl_text.length();
  1177. }
  1178. void Label::_bind_methods() {
  1179. ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &Label::set_horizontal_alignment);
  1180. ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &Label::get_horizontal_alignment);
  1181. ClassDB::bind_method(D_METHOD("set_vertical_alignment", "alignment"), &Label::set_vertical_alignment);
  1182. ClassDB::bind_method(D_METHOD("get_vertical_alignment"), &Label::get_vertical_alignment);
  1183. ClassDB::bind_method(D_METHOD("set_text", "text"), &Label::set_text);
  1184. ClassDB::bind_method(D_METHOD("get_text"), &Label::get_text);
  1185. ClassDB::bind_method(D_METHOD("set_label_settings", "settings"), &Label::set_label_settings);
  1186. ClassDB::bind_method(D_METHOD("get_label_settings"), &Label::get_label_settings);
  1187. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &Label::set_text_direction);
  1188. ClassDB::bind_method(D_METHOD("get_text_direction"), &Label::get_text_direction);
  1189. ClassDB::bind_method(D_METHOD("set_language", "language"), &Label::set_language);
  1190. ClassDB::bind_method(D_METHOD("get_language"), &Label::get_language);
  1191. ClassDB::bind_method(D_METHOD("set_paragraph_separator", "paragraph_separator"), &Label::set_paragraph_separator);
  1192. ClassDB::bind_method(D_METHOD("get_paragraph_separator"), &Label::get_paragraph_separator);
  1193. ClassDB::bind_method(D_METHOD("set_autowrap_mode", "autowrap_mode"), &Label::set_autowrap_mode);
  1194. ClassDB::bind_method(D_METHOD("get_autowrap_mode"), &Label::get_autowrap_mode);
  1195. ClassDB::bind_method(D_METHOD("set_autowrap_trim_flags", "autowrap_trim_flags"), &Label::set_autowrap_trim_flags);
  1196. ClassDB::bind_method(D_METHOD("get_autowrap_trim_flags"), &Label::get_autowrap_trim_flags);
  1197. ClassDB::bind_method(D_METHOD("set_justification_flags", "justification_flags"), &Label::set_justification_flags);
  1198. ClassDB::bind_method(D_METHOD("get_justification_flags"), &Label::get_justification_flags);
  1199. ClassDB::bind_method(D_METHOD("set_clip_text", "enable"), &Label::set_clip_text);
  1200. ClassDB::bind_method(D_METHOD("is_clipping_text"), &Label::is_clipping_text);
  1201. ClassDB::bind_method(D_METHOD("set_tab_stops", "tab_stops"), &Label::set_tab_stops);
  1202. ClassDB::bind_method(D_METHOD("get_tab_stops"), &Label::get_tab_stops);
  1203. ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "overrun_behavior"), &Label::set_text_overrun_behavior);
  1204. ClassDB::bind_method(D_METHOD("get_text_overrun_behavior"), &Label::get_text_overrun_behavior);
  1205. ClassDB::bind_method(D_METHOD("set_ellipsis_char", "char"), &Label::set_ellipsis_char);
  1206. ClassDB::bind_method(D_METHOD("get_ellipsis_char"), &Label::get_ellipsis_char);
  1207. ClassDB::bind_method(D_METHOD("set_uppercase", "enable"), &Label::set_uppercase);
  1208. ClassDB::bind_method(D_METHOD("is_uppercase"), &Label::is_uppercase);
  1209. ClassDB::bind_method(D_METHOD("get_line_height", "line"), &Label::get_line_height, DEFVAL(-1));
  1210. ClassDB::bind_method(D_METHOD("get_line_count"), &Label::get_line_count);
  1211. ClassDB::bind_method(D_METHOD("get_visible_line_count"), &Label::get_visible_line_count);
  1212. ClassDB::bind_method(D_METHOD("get_total_character_count"), &Label::get_total_character_count);
  1213. ClassDB::bind_method(D_METHOD("set_visible_characters", "amount"), &Label::set_visible_characters);
  1214. ClassDB::bind_method(D_METHOD("get_visible_characters"), &Label::get_visible_characters);
  1215. ClassDB::bind_method(D_METHOD("get_visible_characters_behavior"), &Label::get_visible_characters_behavior);
  1216. ClassDB::bind_method(D_METHOD("set_visible_characters_behavior", "behavior"), &Label::set_visible_characters_behavior);
  1217. ClassDB::bind_method(D_METHOD("set_visible_ratio", "ratio"), &Label::set_visible_ratio);
  1218. ClassDB::bind_method(D_METHOD("get_visible_ratio"), &Label::get_visible_ratio);
  1219. ClassDB::bind_method(D_METHOD("set_lines_skipped", "lines_skipped"), &Label::set_lines_skipped);
  1220. ClassDB::bind_method(D_METHOD("get_lines_skipped"), &Label::get_lines_skipped);
  1221. ClassDB::bind_method(D_METHOD("set_max_lines_visible", "lines_visible"), &Label::set_max_lines_visible);
  1222. ClassDB::bind_method(D_METHOD("get_max_lines_visible"), &Label::get_max_lines_visible);
  1223. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &Label::set_structured_text_bidi_override);
  1224. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &Label::get_structured_text_bidi_override);
  1225. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &Label::set_structured_text_bidi_override_options);
  1226. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &Label::get_structured_text_bidi_override_options);
  1227. ClassDB::bind_method(D_METHOD("get_character_bounds", "pos"), &Label::get_character_bounds);
  1228. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
  1229. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "label_settings", PROPERTY_HINT_RESOURCE_TYPE, "LabelSettings"), "set_label_settings", "get_label_settings");
  1230. ADD_PROPERTY(PropertyInfo(Variant::INT, "horizontal_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_horizontal_alignment", "get_horizontal_alignment");
  1231. ADD_PROPERTY(PropertyInfo(Variant::INT, "vertical_alignment", PROPERTY_HINT_ENUM, "Top,Center,Bottom,Fill"), "set_vertical_alignment", "get_vertical_alignment");
  1232. ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_mode", PROPERTY_HINT_ENUM, "Off,Arbitrary,Word,Word (Smart)"), "set_autowrap_mode", "get_autowrap_mode");
  1233. 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");
  1234. 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");
  1235. ADD_PROPERTY(PropertyInfo(Variant::STRING, "paragraph_separator"), "set_paragraph_separator", "get_paragraph_separator");
  1236. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_text"), "set_clip_text", "is_clipping_text");
  1237. 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");
  1238. ADD_PROPERTY(PropertyInfo(Variant::STRING, "ellipsis_char"), "set_ellipsis_char", "get_ellipsis_char");
  1239. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "uppercase"), "set_uppercase", "is_uppercase");
  1240. ADD_PROPERTY(PropertyInfo(Variant::PACKED_FLOAT32_ARRAY, "tab_stops"), "set_tab_stops", "get_tab_stops");
  1241. ADD_GROUP("Displayed Text", "");
  1242. ADD_PROPERTY(PropertyInfo(Variant::INT, "lines_skipped", PROPERTY_HINT_RANGE, "0,999,1"), "set_lines_skipped", "get_lines_skipped");
  1243. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_lines_visible", PROPERTY_HINT_RANGE, "-1,999,1"), "set_max_lines_visible", "get_max_lines_visible");
  1244. // Note: "visible_characters" and "visible_ratio" should be set after "text" to be correctly applied.
  1245. ADD_PROPERTY(PropertyInfo(Variant::INT, "visible_characters", PROPERTY_HINT_RANGE, "-1,128000,1"), "set_visible_characters", "get_visible_characters");
  1246. 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");
  1247. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "visible_ratio", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_visible_ratio", "get_visible_ratio");
  1248. ADD_GROUP("BiDi", "");
  1249. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  1250. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  1251. 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");
  1252. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");
  1253. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Label, normal_style, "normal");
  1254. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Label, line_spacing);
  1255. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Label, paragraph_spacing);
  1256. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, Label, font);
  1257. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, Label, font_size);
  1258. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Label, font_color);
  1259. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Label, font_shadow_color);
  1260. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, Label, font_shadow_offset.x, "shadow_offset_x");
  1261. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, Label, font_shadow_offset.y, "shadow_offset_y");
  1262. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Label, font_outline_color);
  1263. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, Label, font_outline_size, "outline_size");
  1264. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, Label, font_shadow_outline_size, "shadow_outline_size");
  1265. }
  1266. Label::Label(const String &p_text) {
  1267. set_mouse_filter(MOUSE_FILTER_IGNORE);
  1268. set_text(p_text);
  1269. set_v_size_flags(SIZE_SHRINK_CENTER);
  1270. }
  1271. Label::~Label() {
  1272. for (Paragraph &para : paragraphs) {
  1273. for (const RID &line_rid : para.lines_rid) {
  1274. TS->free_rid(line_rid);
  1275. }
  1276. para.lines_rid.clear();
  1277. TS->free_rid(para.text_rid);
  1278. }
  1279. paragraphs.clear();
  1280. }