label.cpp 55 KB

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