label.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. /*************************************************************************/
  2. /* label.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 "core/config/project_settings.h"
  32. #include "core/core_string_names.h"
  33. #include "core/string/print_string.h"
  34. #include "core/string/translation.h"
  35. #include "servers/text_server.h"
  36. void Label::set_autowrap_mode(TextServer::AutowrapMode p_mode) {
  37. if (autowrap_mode == p_mode) {
  38. return;
  39. }
  40. autowrap_mode = p_mode;
  41. lines_dirty = true;
  42. queue_redraw();
  43. if (clip || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  44. update_minimum_size();
  45. }
  46. }
  47. TextServer::AutowrapMode Label::get_autowrap_mode() const {
  48. return autowrap_mode;
  49. }
  50. void Label::set_uppercase(bool p_uppercase) {
  51. if (uppercase == p_uppercase) {
  52. return;
  53. }
  54. uppercase = p_uppercase;
  55. dirty = true;
  56. queue_redraw();
  57. }
  58. bool Label::is_uppercase() const {
  59. return uppercase;
  60. }
  61. int Label::get_line_height(int p_line) const {
  62. Ref<Font> font = (settings.is_valid() && settings->get_font().is_valid()) ? settings->get_font() : get_theme_font(SNAME("font"));
  63. if (p_line >= 0 && p_line < lines_rid.size()) {
  64. return TS->shaped_text_get_size(lines_rid[p_line]).y;
  65. } else if (lines_rid.size() > 0) {
  66. int h = 0;
  67. for (int i = 0; i < lines_rid.size(); i++) {
  68. h = MAX(h, TS->shaped_text_get_size(lines_rid[i]).y);
  69. }
  70. return h;
  71. } else {
  72. int font_size = settings.is_valid() ? settings->get_font_size() : get_theme_font_size(SNAME("font_size"));
  73. return font->get_height(font_size);
  74. }
  75. }
  76. void Label::_shape() {
  77. Ref<StyleBox> style = get_theme_stylebox(SNAME("normal"), SNAME("Label"));
  78. int width = (get_size().width - style->get_minimum_size().width);
  79. if (dirty || font_dirty) {
  80. if (dirty) {
  81. TS->shaped_text_clear(text_rid);
  82. }
  83. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  84. TS->shaped_text_set_direction(text_rid, is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  85. } else {
  86. TS->shaped_text_set_direction(text_rid, (TextServer::Direction)text_direction);
  87. }
  88. const Ref<Font> &font = (settings.is_valid() && settings->get_font().is_valid()) ? settings->get_font() : get_theme_font(SNAME("font"));
  89. int font_size = settings.is_valid() ? settings->get_font_size() : get_theme_font_size(SNAME("font_size"));
  90. ERR_FAIL_COND(font.is_null());
  91. String text = (uppercase) ? TS->string_to_upper(xl_text, language) : xl_text;
  92. if (visible_chars >= 0 && visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING) {
  93. text = text.substr(0, visible_chars);
  94. }
  95. if (dirty) {
  96. TS->shaped_text_add_string(text_rid, text, font->get_rids(), font_size, font->get_opentype_features(), language);
  97. } else {
  98. int spans = TS->shaped_get_span_count(text_rid);
  99. for (int i = 0; i < spans; i++) {
  100. TS->shaped_set_span_update_font(text_rid, i, font->get_rids(), font_size, font->get_opentype_features());
  101. }
  102. }
  103. for (int i = 0; i < TextServer::SPACING_MAX; i++) {
  104. TS->shaped_text_set_spacing(text_rid, TextServer::SpacingType(i), font->get_spacing(TextServer::SpacingType(i)));
  105. }
  106. TS->shaped_text_set_bidi_override(text_rid, structured_text_parser(st_parser, st_args, text));
  107. dirty = false;
  108. font_dirty = false;
  109. lines_dirty = true;
  110. }
  111. if (lines_dirty) {
  112. for (int i = 0; i < lines_rid.size(); i++) {
  113. TS->free_rid(lines_rid[i]);
  114. }
  115. lines_rid.clear();
  116. BitField<TextServer::LineBreakFlag> autowrap_flags = TextServer::BREAK_MANDATORY;
  117. switch (autowrap_mode) {
  118. case TextServer::AUTOWRAP_WORD_SMART:
  119. autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_ADAPTIVE | TextServer::BREAK_MANDATORY;
  120. break;
  121. case TextServer::AUTOWRAP_WORD:
  122. autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_MANDATORY;
  123. break;
  124. case TextServer::AUTOWRAP_ARBITRARY:
  125. autowrap_flags = TextServer::BREAK_GRAPHEME_BOUND | TextServer::BREAK_MANDATORY;
  126. break;
  127. case TextServer::AUTOWRAP_OFF:
  128. break;
  129. }
  130. autowrap_flags = autowrap_flags | TextServer::BREAK_TRIM_EDGE_SPACES;
  131. PackedInt32Array line_breaks = TS->shaped_text_get_line_breaks(text_rid, width, 0, autowrap_flags);
  132. for (int i = 0; i < line_breaks.size(); i = i + 2) {
  133. RID line = TS->shaped_text_substr(text_rid, line_breaks[i], line_breaks[i + 1] - line_breaks[i]);
  134. lines_rid.push_back(line);
  135. }
  136. }
  137. if (xl_text.length() == 0) {
  138. minsize = Size2(1, get_line_height());
  139. return;
  140. }
  141. if (autowrap_mode == TextServer::AUTOWRAP_OFF) {
  142. minsize.width = 0.0f;
  143. for (int i = 0; i < lines_rid.size(); i++) {
  144. if (minsize.width < TS->shaped_text_get_size(lines_rid[i]).x) {
  145. minsize.width = TS->shaped_text_get_size(lines_rid[i]).x;
  146. }
  147. }
  148. }
  149. if (lines_dirty) {
  150. BitField<TextServer::TextOverrunFlag> overrun_flags = TextServer::OVERRUN_NO_TRIM;
  151. switch (overrun_behavior) {
  152. case TextServer::OVERRUN_TRIM_WORD_ELLIPSIS:
  153. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  154. overrun_flags.set_flag(TextServer::OVERRUN_TRIM_WORD_ONLY);
  155. overrun_flags.set_flag(TextServer::OVERRUN_ADD_ELLIPSIS);
  156. break;
  157. case TextServer::OVERRUN_TRIM_ELLIPSIS:
  158. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  159. overrun_flags.set_flag(TextServer::OVERRUN_ADD_ELLIPSIS);
  160. break;
  161. case TextServer::OVERRUN_TRIM_WORD:
  162. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  163. overrun_flags.set_flag(TextServer::OVERRUN_TRIM_WORD_ONLY);
  164. break;
  165. case TextServer::OVERRUN_TRIM_CHAR:
  166. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  167. break;
  168. case TextServer::OVERRUN_NO_TRIMMING:
  169. break;
  170. }
  171. // Fill after min_size calculation.
  172. if (autowrap_mode != TextServer::AUTOWRAP_OFF) {
  173. int visible_lines = get_visible_line_count();
  174. bool lines_hidden = visible_lines > 0 && visible_lines < lines_rid.size();
  175. if (lines_hidden) {
  176. overrun_flags.set_flag(TextServer::OVERRUN_ENFORCE_ELLIPSIS);
  177. }
  178. if (horizontal_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  179. for (int i = 0; i < lines_rid.size(); i++) {
  180. if (i < visible_lines - 1 || lines_rid.size() == 1) {
  181. TS->shaped_text_fit_to_width(lines_rid[i], width);
  182. } else if (i == (visible_lines - 1)) {
  183. TS->shaped_text_overrun_trim_to_width(lines_rid[visible_lines - 1], width, overrun_flags);
  184. }
  185. }
  186. } else if (lines_hidden) {
  187. TS->shaped_text_overrun_trim_to_width(lines_rid[visible_lines - 1], width, overrun_flags);
  188. }
  189. } else {
  190. // Autowrap disabled.
  191. for (int i = 0; i < lines_rid.size(); i++) {
  192. if (horizontal_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  193. TS->shaped_text_fit_to_width(lines_rid[i], width);
  194. overrun_flags.set_flag(TextServer::OVERRUN_JUSTIFICATION_AWARE);
  195. TS->shaped_text_overrun_trim_to_width(lines_rid[i], width, overrun_flags);
  196. TS->shaped_text_fit_to_width(lines_rid[i], width, TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_CONSTRAIN_ELLIPSIS);
  197. } else {
  198. TS->shaped_text_overrun_trim_to_width(lines_rid[i], width, overrun_flags);
  199. }
  200. }
  201. }
  202. lines_dirty = false;
  203. }
  204. _update_visible();
  205. if (autowrap_mode == TextServer::AUTOWRAP_OFF || !clip || overrun_behavior == TextServer::OVERRUN_NO_TRIMMING) {
  206. update_minimum_size();
  207. }
  208. }
  209. void Label::_update_visible() {
  210. int line_spacing = settings.is_valid() ? settings->get_line_spacing() : get_theme_constant(SNAME("line_spacing"), SNAME("Label"));
  211. Ref<StyleBox> style = get_theme_stylebox(SNAME("normal"), SNAME("Label"));
  212. int lines_visible = lines_rid.size();
  213. if (max_lines_visible >= 0 && lines_visible > max_lines_visible) {
  214. lines_visible = max_lines_visible;
  215. }
  216. minsize.height = 0;
  217. int last_line = MIN(lines_rid.size(), lines_visible + lines_skipped);
  218. for (int64_t i = lines_skipped; i < last_line; i++) {
  219. minsize.height += TS->shaped_text_get_size(lines_rid[i]).y + line_spacing;
  220. if (minsize.height > (get_size().height - style->get_minimum_size().height + line_spacing)) {
  221. break;
  222. }
  223. }
  224. }
  225. inline void draw_glyph(const Glyph &p_gl, const RID &p_canvas, const Color &p_font_color, const Vector2 &p_ofs) {
  226. if (p_gl.font_rid != RID()) {
  227. 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);
  228. } else {
  229. 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);
  230. }
  231. }
  232. inline void draw_glyph_outline(const Glyph &p_gl, const RID &p_canvas, const Color &p_font_color, const Color &p_font_shadow_color, const Color &p_font_outline_color, const int &p_shadow_outline_size, const int &p_outline_size, const Vector2 &p_ofs, const Vector2 &shadow_ofs) {
  233. if (p_gl.font_rid != RID()) {
  234. if (p_font_shadow_color.a > 0) {
  235. 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);
  236. }
  237. if (p_font_shadow_color.a > 0 && p_shadow_outline_size > 0) {
  238. 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);
  239. }
  240. if (p_font_outline_color.a != 0.0 && p_outline_size > 0) {
  241. 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);
  242. }
  243. }
  244. }
  245. void Label::_notification(int p_what) {
  246. switch (p_what) {
  247. case NOTIFICATION_TRANSLATION_CHANGED: {
  248. String new_text = atr(text);
  249. if (new_text == xl_text) {
  250. return; // Nothing new.
  251. }
  252. xl_text = new_text;
  253. if (visible_ratio < 1) {
  254. visible_chars = get_total_character_count() * visible_ratio;
  255. }
  256. dirty = true;
  257. queue_redraw();
  258. } break;
  259. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  260. queue_redraw();
  261. } break;
  262. case NOTIFICATION_DRAW: {
  263. if (clip) {
  264. RenderingServer::get_singleton()->canvas_item_set_clip(get_canvas_item(), true);
  265. }
  266. if (dirty || font_dirty || lines_dirty) {
  267. _shape();
  268. }
  269. RID ci = get_canvas_item();
  270. bool has_settings = settings.is_valid();
  271. Size2 string_size;
  272. Size2 size = get_size();
  273. Ref<StyleBox> style = get_theme_stylebox(SNAME("normal"));
  274. Ref<Font> font = (has_settings && settings->get_font().is_valid()) ? settings->get_font() : get_theme_font(SNAME("font"));
  275. Color font_color = has_settings ? settings->get_font_color() : get_theme_color(SNAME("font_color"));
  276. Color font_shadow_color = has_settings ? settings->get_shadow_color() : get_theme_color(SNAME("font_shadow_color"));
  277. Point2 shadow_ofs = has_settings ? settings->get_shadow_offset() : Point2(get_theme_constant(SNAME("shadow_offset_x")), get_theme_constant(SNAME("shadow_offset_y")));
  278. int line_spacing = has_settings ? settings->get_line_spacing() : get_theme_constant(SNAME("line_spacing"));
  279. Color font_outline_color = has_settings ? settings->get_outline_color() : get_theme_color(SNAME("font_outline_color"));
  280. int outline_size = has_settings ? settings->get_outline_size() : get_theme_constant(SNAME("outline_size"));
  281. int shadow_outline_size = has_settings ? settings->get_shadow_size() : get_theme_constant(SNAME("shadow_outline_size"));
  282. bool rtl = (TS->shaped_text_get_inferred_direction(text_rid) == TextServer::DIRECTION_RTL);
  283. bool rtl_layout = is_layout_rtl();
  284. style->draw(ci, Rect2(Point2(0, 0), get_size()));
  285. float total_h = 0.0;
  286. int lines_visible = 0;
  287. // Get number of lines to fit to the height.
  288. for (int64_t i = lines_skipped; i < lines_rid.size(); i++) {
  289. total_h += TS->shaped_text_get_size(lines_rid[i]).y + line_spacing;
  290. if (total_h > (get_size().height - style->get_minimum_size().height + line_spacing)) {
  291. break;
  292. }
  293. lines_visible++;
  294. }
  295. if (max_lines_visible >= 0 && lines_visible > max_lines_visible) {
  296. lines_visible = max_lines_visible;
  297. }
  298. int last_line = MIN(lines_rid.size(), lines_visible + lines_skipped);
  299. bool trim_chars = (visible_chars >= 0) && (visible_chars_behavior == TextServer::VC_CHARS_AFTER_SHAPING);
  300. bool trim_glyphs_ltr = (visible_chars >= 0) && ((visible_chars_behavior == TextServer::VC_GLYPHS_LTR) || ((visible_chars_behavior == TextServer::VC_GLYPHS_AUTO) && !rtl_layout));
  301. bool trim_glyphs_rtl = (visible_chars >= 0) && ((visible_chars_behavior == TextServer::VC_GLYPHS_RTL) || ((visible_chars_behavior == TextServer::VC_GLYPHS_AUTO) && rtl_layout));
  302. // Get real total height.
  303. int total_glyphs = 0;
  304. total_h = 0;
  305. for (int64_t i = lines_skipped; i < last_line; i++) {
  306. total_h += TS->shaped_text_get_size(lines_rid[i]).y + line_spacing;
  307. total_glyphs += TS->shaped_text_get_glyph_count(lines_rid[i]) + TS->shaped_text_get_ellipsis_glyph_count(lines_rid[i]);
  308. }
  309. int visible_glyphs = total_glyphs * visible_ratio;
  310. int processed_glyphs = 0;
  311. total_h += style->get_margin(SIDE_TOP) + style->get_margin(SIDE_BOTTOM);
  312. int vbegin = 0, vsep = 0;
  313. if (lines_visible > 0) {
  314. switch (vertical_alignment) {
  315. case VERTICAL_ALIGNMENT_TOP: {
  316. // Nothing.
  317. } break;
  318. case VERTICAL_ALIGNMENT_CENTER: {
  319. vbegin = (size.y - (total_h - line_spacing)) / 2;
  320. vsep = 0;
  321. } break;
  322. case VERTICAL_ALIGNMENT_BOTTOM: {
  323. vbegin = size.y - (total_h - line_spacing);
  324. vsep = 0;
  325. } break;
  326. case VERTICAL_ALIGNMENT_FILL: {
  327. vbegin = 0;
  328. if (lines_visible > 1) {
  329. vsep = (size.y - (total_h - line_spacing)) / (lines_visible - 1);
  330. } else {
  331. vsep = 0;
  332. }
  333. } break;
  334. }
  335. }
  336. Vector2 ofs;
  337. ofs.y = style->get_offset().y + vbegin;
  338. for (int i = lines_skipped; i < last_line; i++) {
  339. Size2 line_size = TS->shaped_text_get_size(lines_rid[i]);
  340. ofs.x = 0;
  341. ofs.y += TS->shaped_text_get_ascent(lines_rid[i]);
  342. switch (horizontal_alignment) {
  343. case HORIZONTAL_ALIGNMENT_FILL:
  344. if (rtl && autowrap_mode != TextServer::AUTOWRAP_OFF) {
  345. ofs.x = int(size.width - style->get_margin(SIDE_RIGHT) - line_size.width);
  346. } else {
  347. ofs.x = style->get_offset().x;
  348. }
  349. break;
  350. case HORIZONTAL_ALIGNMENT_LEFT: {
  351. if (rtl_layout) {
  352. ofs.x = int(size.width - style->get_margin(SIDE_RIGHT) - line_size.width);
  353. } else {
  354. ofs.x = style->get_offset().x;
  355. }
  356. } break;
  357. case HORIZONTAL_ALIGNMENT_CENTER: {
  358. ofs.x = int(size.width - line_size.width) / 2;
  359. } break;
  360. case HORIZONTAL_ALIGNMENT_RIGHT: {
  361. if (rtl_layout) {
  362. ofs.x = style->get_offset().x;
  363. } else {
  364. ofs.x = int(size.width - style->get_margin(SIDE_RIGHT) - line_size.width);
  365. }
  366. } break;
  367. }
  368. const Glyph *glyphs = TS->shaped_text_get_glyphs(lines_rid[i]);
  369. int gl_size = TS->shaped_text_get_glyph_count(lines_rid[i]);
  370. int ellipsis_pos = TS->shaped_text_get_ellipsis_pos(lines_rid[i]);
  371. int trim_pos = TS->shaped_text_get_trim_pos(lines_rid[i]);
  372. const Glyph *ellipsis_glyphs = TS->shaped_text_get_ellipsis_glyphs(lines_rid[i]);
  373. int ellipsis_gl_size = TS->shaped_text_get_ellipsis_glyph_count(lines_rid[i]);
  374. // Draw outline. Note: Do not merge this into the single loop with the main text, to prevent overlaps.
  375. int processed_glyphs_ol = processed_glyphs;
  376. if ((outline_size > 0 && font_outline_color.a != 0) || (font_shadow_color.a != 0)) {
  377. Vector2 offset = ofs;
  378. // Draw RTL ellipsis string when necessary.
  379. if (rtl && ellipsis_pos >= 0) {
  380. for (int gl_idx = ellipsis_gl_size - 1; gl_idx >= 0; gl_idx--) {
  381. for (int j = 0; j < ellipsis_glyphs[gl_idx].repeat; j++) {
  382. bool skip = (trim_chars && ellipsis_glyphs[gl_idx].end > visible_chars) || (trim_glyphs_ltr && (processed_glyphs_ol >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs_ol < total_glyphs - visible_glyphs));
  383. //Draw glyph outlines and shadow.
  384. if (!skip) {
  385. draw_glyph_outline(ellipsis_glyphs[gl_idx], ci, font_color, font_shadow_color, font_outline_color, shadow_outline_size, outline_size, offset, shadow_ofs);
  386. }
  387. processed_glyphs_ol++;
  388. offset.x += ellipsis_glyphs[gl_idx].advance;
  389. }
  390. }
  391. }
  392. // Draw main text.
  393. for (int j = 0; j < gl_size; j++) {
  394. // Trim when necessary.
  395. if (trim_pos >= 0) {
  396. if (rtl) {
  397. if (j < trim_pos) {
  398. continue;
  399. }
  400. } else {
  401. if (j >= trim_pos) {
  402. break;
  403. }
  404. }
  405. }
  406. for (int k = 0; k < glyphs[j].repeat; k++) {
  407. bool skip = (trim_chars && glyphs[j].end > visible_chars) || (trim_glyphs_ltr && (processed_glyphs_ol >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs_ol < total_glyphs - visible_glyphs));
  408. // Draw glyph outlines and shadow.
  409. if (!skip) {
  410. draw_glyph_outline(glyphs[j], ci, font_color, font_shadow_color, font_outline_color, shadow_outline_size, outline_size, offset, shadow_ofs);
  411. }
  412. processed_glyphs_ol++;
  413. offset.x += glyphs[j].advance;
  414. }
  415. }
  416. // Draw LTR ellipsis string when necessary.
  417. if (!rtl && ellipsis_pos >= 0) {
  418. for (int gl_idx = 0; gl_idx < ellipsis_gl_size; gl_idx++) {
  419. for (int j = 0; j < ellipsis_glyphs[gl_idx].repeat; j++) {
  420. bool skip = (trim_chars && ellipsis_glyphs[gl_idx].end > visible_chars) || (trim_glyphs_ltr && (processed_glyphs_ol >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs_ol < total_glyphs - visible_glyphs));
  421. //Draw glyph outlines and shadow.
  422. if (!skip) {
  423. draw_glyph_outline(ellipsis_glyphs[gl_idx], ci, font_color, font_shadow_color, font_outline_color, shadow_outline_size, outline_size, offset, shadow_ofs);
  424. }
  425. processed_glyphs_ol++;
  426. offset.x += ellipsis_glyphs[gl_idx].advance;
  427. }
  428. }
  429. }
  430. }
  431. // Draw main text. Note: Do not merge this into the single loop with the outline, to prevent overlaps.
  432. // Draw RTL ellipsis string when necessary.
  433. if (rtl && ellipsis_pos >= 0) {
  434. for (int gl_idx = ellipsis_gl_size - 1; gl_idx >= 0; gl_idx--) {
  435. for (int j = 0; j < ellipsis_glyphs[gl_idx].repeat; j++) {
  436. bool skip = (trim_chars && ellipsis_glyphs[gl_idx].end > visible_chars) || (trim_glyphs_ltr && (processed_glyphs >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs < total_glyphs - visible_glyphs));
  437. //Draw glyph outlines and shadow.
  438. if (!skip) {
  439. draw_glyph(ellipsis_glyphs[gl_idx], ci, font_color, ofs);
  440. }
  441. processed_glyphs++;
  442. ofs.x += ellipsis_glyphs[gl_idx].advance;
  443. }
  444. }
  445. }
  446. // Draw main text.
  447. for (int j = 0; j < gl_size; j++) {
  448. // Trim when necessary.
  449. if (trim_pos >= 0) {
  450. if (rtl) {
  451. if (j < trim_pos) {
  452. continue;
  453. }
  454. } else {
  455. if (j >= trim_pos) {
  456. break;
  457. }
  458. }
  459. }
  460. for (int k = 0; k < glyphs[j].repeat; k++) {
  461. bool skip = (trim_chars && glyphs[j].end > visible_chars) || (trim_glyphs_ltr && (processed_glyphs >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs < total_glyphs - visible_glyphs));
  462. // Draw glyph outlines and shadow.
  463. if (!skip) {
  464. draw_glyph(glyphs[j], ci, font_color, ofs);
  465. }
  466. processed_glyphs++;
  467. ofs.x += glyphs[j].advance;
  468. }
  469. }
  470. // Draw LTR ellipsis string when necessary.
  471. if (!rtl && ellipsis_pos >= 0) {
  472. for (int gl_idx = 0; gl_idx < ellipsis_gl_size; gl_idx++) {
  473. for (int j = 0; j < ellipsis_glyphs[gl_idx].repeat; j++) {
  474. bool skip = (trim_chars && ellipsis_glyphs[gl_idx].end > visible_chars) || (trim_glyphs_ltr && (processed_glyphs >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs < total_glyphs - visible_glyphs));
  475. //Draw glyph outlines and shadow.
  476. if (!skip) {
  477. draw_glyph(ellipsis_glyphs[gl_idx], ci, font_color, ofs);
  478. }
  479. processed_glyphs++;
  480. ofs.x += ellipsis_glyphs[gl_idx].advance;
  481. }
  482. }
  483. }
  484. ofs.y += TS->shaped_text_get_descent(lines_rid[i]) + vsep + line_spacing;
  485. }
  486. } break;
  487. case NOTIFICATION_THEME_CHANGED: {
  488. font_dirty = true;
  489. queue_redraw();
  490. } break;
  491. case NOTIFICATION_RESIZED: {
  492. lines_dirty = true;
  493. } break;
  494. }
  495. }
  496. Size2 Label::get_minimum_size() const {
  497. // don't want to mutable everything
  498. if (dirty || font_dirty || lines_dirty) {
  499. const_cast<Label *>(this)->_shape();
  500. }
  501. Size2 min_size = minsize;
  502. const Ref<Font> &font = (settings.is_valid() && settings->get_font().is_valid()) ? settings->get_font() : get_theme_font(SNAME("font"));
  503. int font_size = settings.is_valid() ? settings->get_font_size() : get_theme_font_size(SNAME("font_size"));
  504. min_size.height = MAX(min_size.height, font->get_height(font_size) + font->get_spacing(TextServer::SPACING_TOP) + font->get_spacing(TextServer::SPACING_BOTTOM));
  505. Size2 min_style = get_theme_stylebox(SNAME("normal"))->get_minimum_size();
  506. if (autowrap_mode != TextServer::AUTOWRAP_OFF) {
  507. return Size2(1, (clip || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) ? 1 : min_size.height) + min_style;
  508. } else {
  509. if (clip || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  510. min_size.width = 1;
  511. }
  512. return min_size + min_style;
  513. }
  514. }
  515. int Label::get_line_count() const {
  516. if (!is_inside_tree()) {
  517. return 1;
  518. }
  519. if (dirty || font_dirty || lines_dirty) {
  520. const_cast<Label *>(this)->_shape();
  521. }
  522. return lines_rid.size();
  523. }
  524. int Label::get_visible_line_count() const {
  525. Ref<StyleBox> style = get_theme_stylebox(SNAME("normal"));
  526. int line_spacing = settings.is_valid() ? settings->get_line_spacing() : get_theme_constant(SNAME("line_spacing"));
  527. int lines_visible = 0;
  528. float total_h = 0.0;
  529. for (int64_t i = lines_skipped; i < lines_rid.size(); i++) {
  530. total_h += TS->shaped_text_get_size(lines_rid[i]).y + line_spacing;
  531. if (total_h > (get_size().height - style->get_minimum_size().height + line_spacing)) {
  532. break;
  533. }
  534. lines_visible++;
  535. }
  536. if (lines_visible > lines_rid.size()) {
  537. lines_visible = lines_rid.size();
  538. }
  539. if (max_lines_visible >= 0 && lines_visible > max_lines_visible) {
  540. lines_visible = max_lines_visible;
  541. }
  542. return lines_visible;
  543. }
  544. void Label::set_horizontal_alignment(HorizontalAlignment p_alignment) {
  545. ERR_FAIL_INDEX((int)p_alignment, 4);
  546. if (horizontal_alignment == p_alignment) {
  547. return;
  548. }
  549. if (horizontal_alignment == HORIZONTAL_ALIGNMENT_FILL || p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  550. lines_dirty = true; // Reshape lines.
  551. }
  552. horizontal_alignment = p_alignment;
  553. queue_redraw();
  554. }
  555. HorizontalAlignment Label::get_horizontal_alignment() const {
  556. return horizontal_alignment;
  557. }
  558. void Label::set_vertical_alignment(VerticalAlignment p_alignment) {
  559. ERR_FAIL_INDEX((int)p_alignment, 4);
  560. if (vertical_alignment == p_alignment) {
  561. return;
  562. }
  563. vertical_alignment = p_alignment;
  564. queue_redraw();
  565. }
  566. VerticalAlignment Label::get_vertical_alignment() const {
  567. return vertical_alignment;
  568. }
  569. void Label::set_text(const String &p_string) {
  570. if (text == p_string) {
  571. return;
  572. }
  573. text = p_string;
  574. xl_text = atr(p_string);
  575. dirty = true;
  576. if (visible_ratio < 1) {
  577. visible_chars = get_total_character_count() * visible_ratio;
  578. }
  579. queue_redraw();
  580. update_minimum_size();
  581. }
  582. void Label::_invalidate() {
  583. font_dirty = true;
  584. queue_redraw();
  585. }
  586. void Label::set_label_settings(const Ref<LabelSettings> &p_settings) {
  587. if (settings != p_settings) {
  588. if (settings.is_valid()) {
  589. settings->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Label::_invalidate));
  590. }
  591. settings = p_settings;
  592. if (settings.is_valid()) {
  593. settings->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Label::_invalidate), CONNECT_REFERENCE_COUNTED);
  594. }
  595. _invalidate();
  596. }
  597. }
  598. Ref<LabelSettings> Label::get_label_settings() const {
  599. return settings;
  600. }
  601. void Label::set_text_direction(Control::TextDirection p_text_direction) {
  602. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  603. if (text_direction != p_text_direction) {
  604. text_direction = p_text_direction;
  605. font_dirty = true;
  606. queue_redraw();
  607. }
  608. }
  609. void Label::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) {
  610. if (st_parser != p_parser) {
  611. st_parser = p_parser;
  612. dirty = true;
  613. queue_redraw();
  614. }
  615. }
  616. TextServer::StructuredTextParser Label::get_structured_text_bidi_override() const {
  617. return st_parser;
  618. }
  619. void Label::set_structured_text_bidi_override_options(Array p_args) {
  620. if (st_args == p_args) {
  621. return;
  622. }
  623. st_args = p_args;
  624. dirty = true;
  625. queue_redraw();
  626. }
  627. Array Label::get_structured_text_bidi_override_options() const {
  628. return st_args;
  629. }
  630. Control::TextDirection Label::get_text_direction() const {
  631. return text_direction;
  632. }
  633. void Label::set_language(const String &p_language) {
  634. if (language != p_language) {
  635. language = p_language;
  636. dirty = true;
  637. queue_redraw();
  638. }
  639. }
  640. String Label::get_language() const {
  641. return language;
  642. }
  643. void Label::set_clip_text(bool p_clip) {
  644. if (clip == p_clip) {
  645. return;
  646. }
  647. clip = p_clip;
  648. queue_redraw();
  649. update_minimum_size();
  650. }
  651. bool Label::is_clipping_text() const {
  652. return clip;
  653. }
  654. void Label::set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior) {
  655. if (overrun_behavior == p_behavior) {
  656. return;
  657. }
  658. overrun_behavior = p_behavior;
  659. lines_dirty = true;
  660. queue_redraw();
  661. if (clip || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  662. update_minimum_size();
  663. }
  664. }
  665. TextServer::OverrunBehavior Label::get_text_overrun_behavior() const {
  666. return overrun_behavior;
  667. }
  668. String Label::get_text() const {
  669. return text;
  670. }
  671. void Label::set_visible_characters(int p_amount) {
  672. if (visible_chars != p_amount) {
  673. visible_chars = p_amount;
  674. if (get_total_character_count() > 0) {
  675. visible_ratio = (float)p_amount / (float)get_total_character_count();
  676. } else {
  677. visible_ratio = 1.0;
  678. }
  679. if (visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING) {
  680. dirty = true;
  681. }
  682. queue_redraw();
  683. }
  684. }
  685. int Label::get_visible_characters() const {
  686. return visible_chars;
  687. }
  688. void Label::set_visible_ratio(float p_ratio) {
  689. if (visible_ratio != p_ratio) {
  690. if (p_ratio >= 1.0) {
  691. visible_chars = -1;
  692. visible_ratio = 1.0;
  693. } else if (p_ratio < 0.0) {
  694. visible_chars = 0;
  695. visible_ratio = 0.0;
  696. } else {
  697. visible_chars = get_total_character_count() * p_ratio;
  698. visible_ratio = p_ratio;
  699. }
  700. if (visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING) {
  701. dirty = true;
  702. }
  703. queue_redraw();
  704. }
  705. }
  706. float Label::get_visible_ratio() const {
  707. return visible_ratio;
  708. }
  709. TextServer::VisibleCharactersBehavior Label::get_visible_characters_behavior() const {
  710. return visible_chars_behavior;
  711. }
  712. void Label::set_visible_characters_behavior(TextServer::VisibleCharactersBehavior p_behavior) {
  713. if (visible_chars_behavior != p_behavior) {
  714. visible_chars_behavior = p_behavior;
  715. dirty = true;
  716. queue_redraw();
  717. }
  718. }
  719. void Label::set_lines_skipped(int p_lines) {
  720. ERR_FAIL_COND(p_lines < 0);
  721. if (lines_skipped == p_lines) {
  722. return;
  723. }
  724. lines_skipped = p_lines;
  725. _update_visible();
  726. queue_redraw();
  727. }
  728. int Label::get_lines_skipped() const {
  729. return lines_skipped;
  730. }
  731. void Label::set_max_lines_visible(int p_lines) {
  732. if (max_lines_visible == p_lines) {
  733. return;
  734. }
  735. max_lines_visible = p_lines;
  736. _update_visible();
  737. queue_redraw();
  738. }
  739. int Label::get_max_lines_visible() const {
  740. return max_lines_visible;
  741. }
  742. int Label::get_total_character_count() const {
  743. if (dirty || font_dirty || lines_dirty) {
  744. const_cast<Label *>(this)->_shape();
  745. }
  746. return xl_text.length();
  747. }
  748. void Label::_bind_methods() {
  749. ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &Label::set_horizontal_alignment);
  750. ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &Label::get_horizontal_alignment);
  751. ClassDB::bind_method(D_METHOD("set_vertical_alignment", "alignment"), &Label::set_vertical_alignment);
  752. ClassDB::bind_method(D_METHOD("get_vertical_alignment"), &Label::get_vertical_alignment);
  753. ClassDB::bind_method(D_METHOD("set_text", "text"), &Label::set_text);
  754. ClassDB::bind_method(D_METHOD("get_text"), &Label::get_text);
  755. ClassDB::bind_method(D_METHOD("set_label_settings", "settings"), &Label::set_label_settings);
  756. ClassDB::bind_method(D_METHOD("get_label_settings"), &Label::get_label_settings);
  757. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &Label::set_text_direction);
  758. ClassDB::bind_method(D_METHOD("get_text_direction"), &Label::get_text_direction);
  759. ClassDB::bind_method(D_METHOD("set_language", "language"), &Label::set_language);
  760. ClassDB::bind_method(D_METHOD("get_language"), &Label::get_language);
  761. ClassDB::bind_method(D_METHOD("set_autowrap_mode", "autowrap_mode"), &Label::set_autowrap_mode);
  762. ClassDB::bind_method(D_METHOD("get_autowrap_mode"), &Label::get_autowrap_mode);
  763. ClassDB::bind_method(D_METHOD("set_clip_text", "enable"), &Label::set_clip_text);
  764. ClassDB::bind_method(D_METHOD("is_clipping_text"), &Label::is_clipping_text);
  765. ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "overrun_behavior"), &Label::set_text_overrun_behavior);
  766. ClassDB::bind_method(D_METHOD("get_text_overrun_behavior"), &Label::get_text_overrun_behavior);
  767. ClassDB::bind_method(D_METHOD("set_uppercase", "enable"), &Label::set_uppercase);
  768. ClassDB::bind_method(D_METHOD("is_uppercase"), &Label::is_uppercase);
  769. ClassDB::bind_method(D_METHOD("get_line_height", "line"), &Label::get_line_height, DEFVAL(-1));
  770. ClassDB::bind_method(D_METHOD("get_line_count"), &Label::get_line_count);
  771. ClassDB::bind_method(D_METHOD("get_visible_line_count"), &Label::get_visible_line_count);
  772. ClassDB::bind_method(D_METHOD("get_total_character_count"), &Label::get_total_character_count);
  773. ClassDB::bind_method(D_METHOD("set_visible_characters", "amount"), &Label::set_visible_characters);
  774. ClassDB::bind_method(D_METHOD("get_visible_characters"), &Label::get_visible_characters);
  775. ClassDB::bind_method(D_METHOD("get_visible_characters_behavior"), &Label::get_visible_characters_behavior);
  776. ClassDB::bind_method(D_METHOD("set_visible_characters_behavior", "behavior"), &Label::set_visible_characters_behavior);
  777. ClassDB::bind_method(D_METHOD("set_visible_ratio", "ratio"), &Label::set_visible_ratio);
  778. ClassDB::bind_method(D_METHOD("get_visible_ratio"), &Label::get_visible_ratio);
  779. ClassDB::bind_method(D_METHOD("set_lines_skipped", "lines_skipped"), &Label::set_lines_skipped);
  780. ClassDB::bind_method(D_METHOD("get_lines_skipped"), &Label::get_lines_skipped);
  781. ClassDB::bind_method(D_METHOD("set_max_lines_visible", "lines_visible"), &Label::set_max_lines_visible);
  782. ClassDB::bind_method(D_METHOD("get_max_lines_visible"), &Label::get_max_lines_visible);
  783. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &Label::set_structured_text_bidi_override);
  784. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &Label::get_structured_text_bidi_override);
  785. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &Label::set_structured_text_bidi_override_options);
  786. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &Label::get_structured_text_bidi_override_options);
  787. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT, "", PROPERTY_USAGE_DEFAULT_INTL), "set_text", "get_text");
  788. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "label_settings", PROPERTY_HINT_RESOURCE_TYPE, "LabelSettings"), "set_label_settings", "get_label_settings");
  789. ADD_PROPERTY(PropertyInfo(Variant::INT, "horizontal_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_horizontal_alignment", "get_horizontal_alignment");
  790. ADD_PROPERTY(PropertyInfo(Variant::INT, "vertical_alignment", PROPERTY_HINT_ENUM, "Top,Center,Bottom,Fill"), "set_vertical_alignment", "get_vertical_alignment");
  791. ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_mode", PROPERTY_HINT_ENUM, "Off,Arbitrary,Word,Word (Smart)"), "set_autowrap_mode", "get_autowrap_mode");
  792. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_text"), "set_clip_text", "is_clipping_text");
  793. 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");
  794. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "uppercase"), "set_uppercase", "is_uppercase");
  795. ADD_GROUP("Displayed Text", "");
  796. ADD_PROPERTY(PropertyInfo(Variant::INT, "lines_skipped", PROPERTY_HINT_RANGE, "0,999,1"), "set_lines_skipped", "get_lines_skipped");
  797. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_lines_visible", PROPERTY_HINT_RANGE, "-1,999,1"), "set_max_lines_visible", "get_max_lines_visible");
  798. // Note: "visible_characters" and "visible_ratio" should be set after "text" to be correctly applied.
  799. ADD_PROPERTY(PropertyInfo(Variant::INT, "visible_characters", PROPERTY_HINT_RANGE, "-1,128000,1"), "set_visible_characters", "get_visible_characters");
  800. 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");
  801. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "visible_ratio", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_visible_ratio", "get_visible_ratio");
  802. ADD_GROUP("BiDi", "");
  803. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  804. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  805. 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");
  806. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");
  807. }
  808. Label::Label(const String &p_text) {
  809. text_rid = TS->create_shaped_text();
  810. set_mouse_filter(MOUSE_FILTER_IGNORE);
  811. set_text(p_text);
  812. set_v_size_flags(SIZE_SHRINK_CENTER);
  813. }
  814. Label::~Label() {
  815. for (int i = 0; i < lines_rid.size(); i++) {
  816. TS->free_rid(lines_rid[i]);
  817. }
  818. lines_rid.clear();
  819. TS->free_rid(text_rid);
  820. }