label.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  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 "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() : theme_cache.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() : theme_cache.font_size;
  73. return font->get_height(font_size);
  74. }
  75. }
  76. void Label::_shape() {
  77. Ref<StyleBox> style = theme_cache.normal_style;
  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() : theme_cache.font;
  89. int font_size = settings.is_valid() ? settings->get_font_size() : theme_cache.font_size;
  90. ERR_FAIL_COND(font.is_null());
  91. String txt = (uppercase) ? TS->string_to_upper(xl_text, language) : xl_text;
  92. if (visible_chars >= 0 && visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING) {
  93. txt = txt.substr(0, visible_chars);
  94. }
  95. if (dirty) {
  96. TS->shaped_text_add_string(text_rid, txt, 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, txt));
  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() : theme_cache.line_spacing;
  211. Ref<StyleBox> style = theme_cache.normal_style;
  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::_update_theme_item_cache() {
  246. Control::_update_theme_item_cache();
  247. theme_cache.normal_style = get_theme_stylebox(SNAME("normal"));
  248. theme_cache.font = get_theme_font(SNAME("font"));
  249. theme_cache.font_size = get_theme_font_size(SNAME("font_size"));
  250. theme_cache.line_spacing = get_theme_constant(SNAME("line_spacing"));
  251. theme_cache.font_color = get_theme_color(SNAME("font_color"));
  252. theme_cache.font_shadow_color = get_theme_color(SNAME("font_shadow_color"));
  253. theme_cache.font_shadow_offset = Point2(get_theme_constant(SNAME("shadow_offset_x")), get_theme_constant(SNAME("shadow_offset_y")));
  254. theme_cache.font_outline_color = get_theme_color(SNAME("font_outline_color"));
  255. theme_cache.font_outline_size = get_theme_constant(SNAME("outline_size"));
  256. theme_cache.font_shadow_outline_size = get_theme_constant(SNAME("shadow_outline_size"));
  257. }
  258. PackedStringArray Label::get_configuration_warnings() const {
  259. PackedStringArray warnings = Control::get_configuration_warnings();
  260. // Ensure that the font can render all of the required glyphs.
  261. Ref<Font> font;
  262. if (settings.is_valid()) {
  263. font = settings->get_font();
  264. }
  265. if (font.is_null()) {
  266. font = theme_cache.font;
  267. }
  268. if (font.is_valid()) {
  269. if (dirty || font_dirty || lines_dirty) {
  270. const_cast<Label *>(this)->_shape();
  271. }
  272. const Glyph *glyph = TS->shaped_text_get_glyphs(text_rid);
  273. int64_t glyph_count = TS->shaped_text_get_glyph_count(text_rid);
  274. for (int64_t i = 0; i < glyph_count; i++) {
  275. if (glyph[i].font_rid == RID()) {
  276. warnings.push_back(RTR("The current font does not support rendering one or more characters used in this Label's text."));
  277. break;
  278. }
  279. }
  280. }
  281. return warnings;
  282. }
  283. void Label::_notification(int p_what) {
  284. switch (p_what) {
  285. case NOTIFICATION_TRANSLATION_CHANGED: {
  286. String new_text = atr(text);
  287. if (new_text == xl_text) {
  288. return; // Nothing new.
  289. }
  290. xl_text = new_text;
  291. if (visible_ratio < 1) {
  292. visible_chars = get_total_character_count() * visible_ratio;
  293. }
  294. dirty = true;
  295. queue_redraw();
  296. update_configuration_warnings();
  297. } break;
  298. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  299. queue_redraw();
  300. } break;
  301. case NOTIFICATION_DRAW: {
  302. if (clip) {
  303. RenderingServer::get_singleton()->canvas_item_set_clip(get_canvas_item(), true);
  304. }
  305. // When a shaped text is invalidated by an external source, we want to reshape it.
  306. if (!TS->shaped_text_is_ready(text_rid)) {
  307. dirty = true;
  308. }
  309. for (const RID &line_rid : lines_rid) {
  310. if (!TS->shaped_text_is_ready(line_rid)) {
  311. lines_dirty = true;
  312. break;
  313. }
  314. }
  315. if (dirty || font_dirty || lines_dirty) {
  316. _shape();
  317. }
  318. RID ci = get_canvas_item();
  319. bool has_settings = settings.is_valid();
  320. Size2 string_size;
  321. Size2 size = get_size();
  322. Ref<StyleBox> style = theme_cache.normal_style;
  323. Ref<Font> font = (has_settings && settings->get_font().is_valid()) ? settings->get_font() : theme_cache.font;
  324. Color font_color = has_settings ? settings->get_font_color() : theme_cache.font_color;
  325. Color font_shadow_color = has_settings ? settings->get_shadow_color() : theme_cache.font_shadow_color;
  326. Point2 shadow_ofs = has_settings ? settings->get_shadow_offset() : theme_cache.font_shadow_offset;
  327. int line_spacing = has_settings ? settings->get_line_spacing() : theme_cache.line_spacing;
  328. Color font_outline_color = has_settings ? settings->get_outline_color() : theme_cache.font_outline_color;
  329. int outline_size = has_settings ? settings->get_outline_size() : theme_cache.font_outline_size;
  330. int shadow_outline_size = has_settings ? settings->get_shadow_size() : theme_cache.font_shadow_outline_size;
  331. bool rtl = (TS->shaped_text_get_inferred_direction(text_rid) == TextServer::DIRECTION_RTL);
  332. bool rtl_layout = is_layout_rtl();
  333. style->draw(ci, Rect2(Point2(0, 0), get_size()));
  334. float total_h = 0.0;
  335. int lines_visible = 0;
  336. // Get number of lines to fit to the height.
  337. for (int64_t i = lines_skipped; i < lines_rid.size(); i++) {
  338. total_h += TS->shaped_text_get_size(lines_rid[i]).y + line_spacing;
  339. if (total_h > (get_size().height - style->get_minimum_size().height + line_spacing)) {
  340. break;
  341. }
  342. lines_visible++;
  343. }
  344. if (max_lines_visible >= 0 && lines_visible > max_lines_visible) {
  345. lines_visible = max_lines_visible;
  346. }
  347. int last_line = MIN(lines_rid.size(), lines_visible + lines_skipped);
  348. bool trim_chars = (visible_chars >= 0) && (visible_chars_behavior == TextServer::VC_CHARS_AFTER_SHAPING);
  349. bool trim_glyphs_ltr = (visible_chars >= 0) && ((visible_chars_behavior == TextServer::VC_GLYPHS_LTR) || ((visible_chars_behavior == TextServer::VC_GLYPHS_AUTO) && !rtl_layout));
  350. bool trim_glyphs_rtl = (visible_chars >= 0) && ((visible_chars_behavior == TextServer::VC_GLYPHS_RTL) || ((visible_chars_behavior == TextServer::VC_GLYPHS_AUTO) && rtl_layout));
  351. // Get real total height.
  352. int total_glyphs = 0;
  353. total_h = 0;
  354. for (int64_t i = lines_skipped; i < last_line; i++) {
  355. total_h += TS->shaped_text_get_size(lines_rid[i]).y + line_spacing;
  356. total_glyphs += TS->shaped_text_get_glyph_count(lines_rid[i]) + TS->shaped_text_get_ellipsis_glyph_count(lines_rid[i]);
  357. }
  358. int visible_glyphs = total_glyphs * visible_ratio;
  359. int processed_glyphs = 0;
  360. total_h += style->get_margin(SIDE_TOP) + style->get_margin(SIDE_BOTTOM);
  361. int vbegin = 0, vsep = 0;
  362. if (lines_visible > 0) {
  363. switch (vertical_alignment) {
  364. case VERTICAL_ALIGNMENT_TOP: {
  365. // Nothing.
  366. } break;
  367. case VERTICAL_ALIGNMENT_CENTER: {
  368. vbegin = (size.y - (total_h - line_spacing)) / 2;
  369. vsep = 0;
  370. } break;
  371. case VERTICAL_ALIGNMENT_BOTTOM: {
  372. vbegin = size.y - (total_h - line_spacing);
  373. vsep = 0;
  374. } break;
  375. case VERTICAL_ALIGNMENT_FILL: {
  376. vbegin = 0;
  377. if (lines_visible > 1) {
  378. vsep = (size.y - (total_h - line_spacing)) / (lines_visible - 1);
  379. } else {
  380. vsep = 0;
  381. }
  382. } break;
  383. }
  384. }
  385. Vector2 ofs;
  386. ofs.y = style->get_offset().y + vbegin;
  387. for (int i = lines_skipped; i < last_line; i++) {
  388. Size2 line_size = TS->shaped_text_get_size(lines_rid[i]);
  389. ofs.x = 0;
  390. ofs.y += TS->shaped_text_get_ascent(lines_rid[i]);
  391. switch (horizontal_alignment) {
  392. case HORIZONTAL_ALIGNMENT_FILL:
  393. if (rtl && autowrap_mode != TextServer::AUTOWRAP_OFF) {
  394. ofs.x = int(size.width - style->get_margin(SIDE_RIGHT) - line_size.width);
  395. } else {
  396. ofs.x = style->get_offset().x;
  397. }
  398. break;
  399. case HORIZONTAL_ALIGNMENT_LEFT: {
  400. if (rtl_layout) {
  401. ofs.x = int(size.width - style->get_margin(SIDE_RIGHT) - line_size.width);
  402. } else {
  403. ofs.x = style->get_offset().x;
  404. }
  405. } break;
  406. case HORIZONTAL_ALIGNMENT_CENTER: {
  407. ofs.x = int(size.width - line_size.width) / 2;
  408. } break;
  409. case HORIZONTAL_ALIGNMENT_RIGHT: {
  410. if (rtl_layout) {
  411. ofs.x = style->get_offset().x;
  412. } else {
  413. ofs.x = int(size.width - style->get_margin(SIDE_RIGHT) - line_size.width);
  414. }
  415. } break;
  416. }
  417. const Glyph *glyphs = TS->shaped_text_get_glyphs(lines_rid[i]);
  418. int gl_size = TS->shaped_text_get_glyph_count(lines_rid[i]);
  419. int ellipsis_pos = TS->shaped_text_get_ellipsis_pos(lines_rid[i]);
  420. int trim_pos = TS->shaped_text_get_trim_pos(lines_rid[i]);
  421. const Glyph *ellipsis_glyphs = TS->shaped_text_get_ellipsis_glyphs(lines_rid[i]);
  422. int ellipsis_gl_size = TS->shaped_text_get_ellipsis_glyph_count(lines_rid[i]);
  423. // Draw outline. Note: Do not merge this into the single loop with the main text, to prevent overlaps.
  424. int processed_glyphs_ol = processed_glyphs;
  425. if ((outline_size > 0 && font_outline_color.a != 0) || (font_shadow_color.a != 0)) {
  426. Vector2 offset = ofs;
  427. // Draw RTL ellipsis string when necessary.
  428. if (rtl && ellipsis_pos >= 0) {
  429. for (int gl_idx = ellipsis_gl_size - 1; gl_idx >= 0; gl_idx--) {
  430. for (int j = 0; j < ellipsis_glyphs[gl_idx].repeat; j++) {
  431. 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));
  432. //Draw glyph outlines and shadow.
  433. if (!skip) {
  434. draw_glyph_outline(ellipsis_glyphs[gl_idx], ci, font_color, font_shadow_color, font_outline_color, shadow_outline_size, outline_size, offset, shadow_ofs);
  435. }
  436. processed_glyphs_ol++;
  437. offset.x += ellipsis_glyphs[gl_idx].advance;
  438. }
  439. }
  440. }
  441. // Draw main text.
  442. for (int j = 0; j < gl_size; j++) {
  443. // Trim when necessary.
  444. if (trim_pos >= 0) {
  445. if (rtl) {
  446. if (j < trim_pos) {
  447. continue;
  448. }
  449. } else {
  450. if (j >= trim_pos) {
  451. break;
  452. }
  453. }
  454. }
  455. for (int k = 0; k < glyphs[j].repeat; k++) {
  456. 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));
  457. // Draw glyph outlines and shadow.
  458. if (!skip) {
  459. draw_glyph_outline(glyphs[j], ci, font_color, font_shadow_color, font_outline_color, shadow_outline_size, outline_size, offset, shadow_ofs);
  460. }
  461. processed_glyphs_ol++;
  462. offset.x += glyphs[j].advance;
  463. }
  464. }
  465. // Draw LTR ellipsis string when necessary.
  466. if (!rtl && ellipsis_pos >= 0) {
  467. for (int gl_idx = 0; gl_idx < ellipsis_gl_size; gl_idx++) {
  468. for (int j = 0; j < ellipsis_glyphs[gl_idx].repeat; j++) {
  469. 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));
  470. //Draw glyph outlines and shadow.
  471. if (!skip) {
  472. draw_glyph_outline(ellipsis_glyphs[gl_idx], ci, font_color, font_shadow_color, font_outline_color, shadow_outline_size, outline_size, offset, shadow_ofs);
  473. }
  474. processed_glyphs_ol++;
  475. offset.x += ellipsis_glyphs[gl_idx].advance;
  476. }
  477. }
  478. }
  479. }
  480. // Draw main text. Note: Do not merge this into the single loop with the outline, to prevent overlaps.
  481. // Draw RTL ellipsis string when necessary.
  482. if (rtl && ellipsis_pos >= 0) {
  483. for (int gl_idx = ellipsis_gl_size - 1; gl_idx >= 0; gl_idx--) {
  484. for (int j = 0; j < ellipsis_glyphs[gl_idx].repeat; j++) {
  485. 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));
  486. //Draw glyph outlines and shadow.
  487. if (!skip) {
  488. draw_glyph(ellipsis_glyphs[gl_idx], ci, font_color, ofs);
  489. }
  490. processed_glyphs++;
  491. ofs.x += ellipsis_glyphs[gl_idx].advance;
  492. }
  493. }
  494. }
  495. // Draw main text.
  496. for (int j = 0; j < gl_size; j++) {
  497. // Trim when necessary.
  498. if (trim_pos >= 0) {
  499. if (rtl) {
  500. if (j < trim_pos) {
  501. continue;
  502. }
  503. } else {
  504. if (j >= trim_pos) {
  505. break;
  506. }
  507. }
  508. }
  509. for (int k = 0; k < glyphs[j].repeat; k++) {
  510. 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));
  511. // Draw glyph outlines and shadow.
  512. if (!skip) {
  513. draw_glyph(glyphs[j], ci, font_color, ofs);
  514. }
  515. processed_glyphs++;
  516. ofs.x += glyphs[j].advance;
  517. }
  518. }
  519. // Draw LTR ellipsis string when necessary.
  520. if (!rtl && ellipsis_pos >= 0) {
  521. for (int gl_idx = 0; gl_idx < ellipsis_gl_size; gl_idx++) {
  522. for (int j = 0; j < ellipsis_glyphs[gl_idx].repeat; j++) {
  523. 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));
  524. //Draw glyph outlines and shadow.
  525. if (!skip) {
  526. draw_glyph(ellipsis_glyphs[gl_idx], ci, font_color, ofs);
  527. }
  528. processed_glyphs++;
  529. ofs.x += ellipsis_glyphs[gl_idx].advance;
  530. }
  531. }
  532. }
  533. ofs.y += TS->shaped_text_get_descent(lines_rid[i]) + vsep + line_spacing;
  534. }
  535. } break;
  536. case NOTIFICATION_THEME_CHANGED: {
  537. font_dirty = true;
  538. queue_redraw();
  539. } break;
  540. case NOTIFICATION_RESIZED: {
  541. lines_dirty = true;
  542. } break;
  543. }
  544. }
  545. Size2 Label::get_minimum_size() const {
  546. // don't want to mutable everything
  547. if (dirty || font_dirty || lines_dirty) {
  548. const_cast<Label *>(this)->_shape();
  549. }
  550. Size2 min_size = minsize;
  551. const Ref<Font> &font = (settings.is_valid() && settings->get_font().is_valid()) ? settings->get_font() : theme_cache.font;
  552. int font_size = settings.is_valid() ? settings->get_font_size() : theme_cache.font_size;
  553. min_size.height = MAX(min_size.height, font->get_height(font_size) + font->get_spacing(TextServer::SPACING_TOP) + font->get_spacing(TextServer::SPACING_BOTTOM));
  554. Size2 min_style = theme_cache.normal_style->get_minimum_size();
  555. if (autowrap_mode != TextServer::AUTOWRAP_OFF) {
  556. return Size2(1, (clip || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) ? 1 : min_size.height) + min_style;
  557. } else {
  558. if (clip || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  559. min_size.width = 1;
  560. }
  561. return min_size + min_style;
  562. }
  563. }
  564. int Label::get_line_count() const {
  565. if (!is_inside_tree()) {
  566. return 1;
  567. }
  568. if (dirty || font_dirty || lines_dirty) {
  569. const_cast<Label *>(this)->_shape();
  570. }
  571. return lines_rid.size();
  572. }
  573. int Label::get_visible_line_count() const {
  574. Ref<StyleBox> style = theme_cache.normal_style;
  575. int line_spacing = settings.is_valid() ? settings->get_line_spacing() : theme_cache.line_spacing;
  576. int lines_visible = 0;
  577. float total_h = 0.0;
  578. for (int64_t i = lines_skipped; i < lines_rid.size(); i++) {
  579. total_h += TS->shaped_text_get_size(lines_rid[i]).y + line_spacing;
  580. if (total_h > (get_size().height - style->get_minimum_size().height + line_spacing)) {
  581. break;
  582. }
  583. lines_visible++;
  584. }
  585. if (lines_visible > lines_rid.size()) {
  586. lines_visible = lines_rid.size();
  587. }
  588. if (max_lines_visible >= 0 && lines_visible > max_lines_visible) {
  589. lines_visible = max_lines_visible;
  590. }
  591. return lines_visible;
  592. }
  593. void Label::set_horizontal_alignment(HorizontalAlignment p_alignment) {
  594. ERR_FAIL_INDEX((int)p_alignment, 4);
  595. if (horizontal_alignment == p_alignment) {
  596. return;
  597. }
  598. if (horizontal_alignment == HORIZONTAL_ALIGNMENT_FILL || p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  599. lines_dirty = true; // Reshape lines.
  600. }
  601. horizontal_alignment = p_alignment;
  602. queue_redraw();
  603. }
  604. HorizontalAlignment Label::get_horizontal_alignment() const {
  605. return horizontal_alignment;
  606. }
  607. void Label::set_vertical_alignment(VerticalAlignment p_alignment) {
  608. ERR_FAIL_INDEX((int)p_alignment, 4);
  609. if (vertical_alignment == p_alignment) {
  610. return;
  611. }
  612. vertical_alignment = p_alignment;
  613. queue_redraw();
  614. }
  615. VerticalAlignment Label::get_vertical_alignment() const {
  616. return vertical_alignment;
  617. }
  618. void Label::set_text(const String &p_string) {
  619. if (text == p_string) {
  620. return;
  621. }
  622. text = p_string;
  623. xl_text = atr(p_string);
  624. dirty = true;
  625. if (visible_ratio < 1) {
  626. visible_chars = get_total_character_count() * visible_ratio;
  627. }
  628. queue_redraw();
  629. update_minimum_size();
  630. update_configuration_warnings();
  631. }
  632. void Label::_invalidate() {
  633. font_dirty = true;
  634. queue_redraw();
  635. }
  636. void Label::set_label_settings(const Ref<LabelSettings> &p_settings) {
  637. if (settings != p_settings) {
  638. if (settings.is_valid()) {
  639. settings->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Label::_invalidate));
  640. }
  641. settings = p_settings;
  642. if (settings.is_valid()) {
  643. settings->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Label::_invalidate), CONNECT_REFERENCE_COUNTED);
  644. }
  645. _invalidate();
  646. }
  647. }
  648. Ref<LabelSettings> Label::get_label_settings() const {
  649. return settings;
  650. }
  651. void Label::set_text_direction(Control::TextDirection p_text_direction) {
  652. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  653. if (text_direction != p_text_direction) {
  654. text_direction = p_text_direction;
  655. font_dirty = true;
  656. queue_redraw();
  657. }
  658. }
  659. void Label::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) {
  660. if (st_parser != p_parser) {
  661. st_parser = p_parser;
  662. dirty = true;
  663. queue_redraw();
  664. }
  665. }
  666. TextServer::StructuredTextParser Label::get_structured_text_bidi_override() const {
  667. return st_parser;
  668. }
  669. void Label::set_structured_text_bidi_override_options(Array p_args) {
  670. if (st_args == p_args) {
  671. return;
  672. }
  673. st_args = p_args;
  674. dirty = true;
  675. queue_redraw();
  676. }
  677. Array Label::get_structured_text_bidi_override_options() const {
  678. return st_args;
  679. }
  680. Control::TextDirection Label::get_text_direction() const {
  681. return text_direction;
  682. }
  683. void Label::set_language(const String &p_language) {
  684. if (language != p_language) {
  685. language = p_language;
  686. dirty = true;
  687. queue_redraw();
  688. }
  689. }
  690. String Label::get_language() const {
  691. return language;
  692. }
  693. void Label::set_clip_text(bool p_clip) {
  694. if (clip == p_clip) {
  695. return;
  696. }
  697. clip = p_clip;
  698. queue_redraw();
  699. update_minimum_size();
  700. }
  701. bool Label::is_clipping_text() const {
  702. return clip;
  703. }
  704. void Label::set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior) {
  705. if (overrun_behavior == p_behavior) {
  706. return;
  707. }
  708. overrun_behavior = p_behavior;
  709. lines_dirty = true;
  710. queue_redraw();
  711. if (clip || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  712. update_minimum_size();
  713. }
  714. }
  715. TextServer::OverrunBehavior Label::get_text_overrun_behavior() const {
  716. return overrun_behavior;
  717. }
  718. String Label::get_text() const {
  719. return text;
  720. }
  721. void Label::set_visible_characters(int p_amount) {
  722. if (visible_chars != p_amount) {
  723. visible_chars = p_amount;
  724. if (get_total_character_count() > 0) {
  725. visible_ratio = (float)p_amount / (float)get_total_character_count();
  726. } else {
  727. visible_ratio = 1.0;
  728. }
  729. if (visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING) {
  730. dirty = true;
  731. }
  732. queue_redraw();
  733. }
  734. }
  735. int Label::get_visible_characters() const {
  736. return visible_chars;
  737. }
  738. void Label::set_visible_ratio(float p_ratio) {
  739. if (visible_ratio != p_ratio) {
  740. if (p_ratio >= 1.0) {
  741. visible_chars = -1;
  742. visible_ratio = 1.0;
  743. } else if (p_ratio < 0.0) {
  744. visible_chars = 0;
  745. visible_ratio = 0.0;
  746. } else {
  747. visible_chars = get_total_character_count() * p_ratio;
  748. visible_ratio = p_ratio;
  749. }
  750. if (visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING) {
  751. dirty = true;
  752. }
  753. queue_redraw();
  754. }
  755. }
  756. float Label::get_visible_ratio() const {
  757. return visible_ratio;
  758. }
  759. TextServer::VisibleCharactersBehavior Label::get_visible_characters_behavior() const {
  760. return visible_chars_behavior;
  761. }
  762. void Label::set_visible_characters_behavior(TextServer::VisibleCharactersBehavior p_behavior) {
  763. if (visible_chars_behavior != p_behavior) {
  764. visible_chars_behavior = p_behavior;
  765. dirty = true;
  766. queue_redraw();
  767. }
  768. }
  769. void Label::set_lines_skipped(int p_lines) {
  770. ERR_FAIL_COND(p_lines < 0);
  771. if (lines_skipped == p_lines) {
  772. return;
  773. }
  774. lines_skipped = p_lines;
  775. _update_visible();
  776. queue_redraw();
  777. }
  778. int Label::get_lines_skipped() const {
  779. return lines_skipped;
  780. }
  781. void Label::set_max_lines_visible(int p_lines) {
  782. if (max_lines_visible == p_lines) {
  783. return;
  784. }
  785. max_lines_visible = p_lines;
  786. _update_visible();
  787. queue_redraw();
  788. }
  789. int Label::get_max_lines_visible() const {
  790. return max_lines_visible;
  791. }
  792. int Label::get_total_character_count() const {
  793. if (dirty || font_dirty || lines_dirty) {
  794. const_cast<Label *>(this)->_shape();
  795. }
  796. return xl_text.length();
  797. }
  798. void Label::_bind_methods() {
  799. ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &Label::set_horizontal_alignment);
  800. ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &Label::get_horizontal_alignment);
  801. ClassDB::bind_method(D_METHOD("set_vertical_alignment", "alignment"), &Label::set_vertical_alignment);
  802. ClassDB::bind_method(D_METHOD("get_vertical_alignment"), &Label::get_vertical_alignment);
  803. ClassDB::bind_method(D_METHOD("set_text", "text"), &Label::set_text);
  804. ClassDB::bind_method(D_METHOD("get_text"), &Label::get_text);
  805. ClassDB::bind_method(D_METHOD("set_label_settings", "settings"), &Label::set_label_settings);
  806. ClassDB::bind_method(D_METHOD("get_label_settings"), &Label::get_label_settings);
  807. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &Label::set_text_direction);
  808. ClassDB::bind_method(D_METHOD("get_text_direction"), &Label::get_text_direction);
  809. ClassDB::bind_method(D_METHOD("set_language", "language"), &Label::set_language);
  810. ClassDB::bind_method(D_METHOD("get_language"), &Label::get_language);
  811. ClassDB::bind_method(D_METHOD("set_autowrap_mode", "autowrap_mode"), &Label::set_autowrap_mode);
  812. ClassDB::bind_method(D_METHOD("get_autowrap_mode"), &Label::get_autowrap_mode);
  813. ClassDB::bind_method(D_METHOD("set_clip_text", "enable"), &Label::set_clip_text);
  814. ClassDB::bind_method(D_METHOD("is_clipping_text"), &Label::is_clipping_text);
  815. ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "overrun_behavior"), &Label::set_text_overrun_behavior);
  816. ClassDB::bind_method(D_METHOD("get_text_overrun_behavior"), &Label::get_text_overrun_behavior);
  817. ClassDB::bind_method(D_METHOD("set_uppercase", "enable"), &Label::set_uppercase);
  818. ClassDB::bind_method(D_METHOD("is_uppercase"), &Label::is_uppercase);
  819. ClassDB::bind_method(D_METHOD("get_line_height", "line"), &Label::get_line_height, DEFVAL(-1));
  820. ClassDB::bind_method(D_METHOD("get_line_count"), &Label::get_line_count);
  821. ClassDB::bind_method(D_METHOD("get_visible_line_count"), &Label::get_visible_line_count);
  822. ClassDB::bind_method(D_METHOD("get_total_character_count"), &Label::get_total_character_count);
  823. ClassDB::bind_method(D_METHOD("set_visible_characters", "amount"), &Label::set_visible_characters);
  824. ClassDB::bind_method(D_METHOD("get_visible_characters"), &Label::get_visible_characters);
  825. ClassDB::bind_method(D_METHOD("get_visible_characters_behavior"), &Label::get_visible_characters_behavior);
  826. ClassDB::bind_method(D_METHOD("set_visible_characters_behavior", "behavior"), &Label::set_visible_characters_behavior);
  827. ClassDB::bind_method(D_METHOD("set_visible_ratio", "ratio"), &Label::set_visible_ratio);
  828. ClassDB::bind_method(D_METHOD("get_visible_ratio"), &Label::get_visible_ratio);
  829. ClassDB::bind_method(D_METHOD("set_lines_skipped", "lines_skipped"), &Label::set_lines_skipped);
  830. ClassDB::bind_method(D_METHOD("get_lines_skipped"), &Label::get_lines_skipped);
  831. ClassDB::bind_method(D_METHOD("set_max_lines_visible", "lines_visible"), &Label::set_max_lines_visible);
  832. ClassDB::bind_method(D_METHOD("get_max_lines_visible"), &Label::get_max_lines_visible);
  833. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &Label::set_structured_text_bidi_override);
  834. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &Label::get_structured_text_bidi_override);
  835. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &Label::set_structured_text_bidi_override_options);
  836. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &Label::get_structured_text_bidi_override_options);
  837. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
  838. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "label_settings", PROPERTY_HINT_RESOURCE_TYPE, "LabelSettings"), "set_label_settings", "get_label_settings");
  839. ADD_PROPERTY(PropertyInfo(Variant::INT, "horizontal_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_horizontal_alignment", "get_horizontal_alignment");
  840. ADD_PROPERTY(PropertyInfo(Variant::INT, "vertical_alignment", PROPERTY_HINT_ENUM, "Top,Center,Bottom,Fill"), "set_vertical_alignment", "get_vertical_alignment");
  841. ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_mode", PROPERTY_HINT_ENUM, "Off,Arbitrary,Word,Word (Smart)"), "set_autowrap_mode", "get_autowrap_mode");
  842. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_text"), "set_clip_text", "is_clipping_text");
  843. 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");
  844. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "uppercase"), "set_uppercase", "is_uppercase");
  845. ADD_GROUP("Displayed Text", "");
  846. ADD_PROPERTY(PropertyInfo(Variant::INT, "lines_skipped", PROPERTY_HINT_RANGE, "0,999,1"), "set_lines_skipped", "get_lines_skipped");
  847. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_lines_visible", PROPERTY_HINT_RANGE, "-1,999,1"), "set_max_lines_visible", "get_max_lines_visible");
  848. // Note: "visible_characters" and "visible_ratio" should be set after "text" to be correctly applied.
  849. ADD_PROPERTY(PropertyInfo(Variant::INT, "visible_characters", PROPERTY_HINT_RANGE, "-1,128000,1"), "set_visible_characters", "get_visible_characters");
  850. 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");
  851. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "visible_ratio", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_visible_ratio", "get_visible_ratio");
  852. ADD_GROUP("BiDi", "");
  853. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  854. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  855. 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");
  856. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");
  857. }
  858. Label::Label(const String &p_text) {
  859. text_rid = TS->create_shaped_text();
  860. set_mouse_filter(MOUSE_FILTER_IGNORE);
  861. set_text(p_text);
  862. set_v_size_flags(SIZE_SHRINK_CENTER);
  863. }
  864. Label::~Label() {
  865. for (int i = 0; i < lines_rid.size(); i++) {
  866. TS->free_rid(lines_rid[i]);
  867. }
  868. lines_rid.clear();
  869. TS->free_rid(text_rid);
  870. }