label.cpp 52 KB

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