2
0

label.cpp 32 KB

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