label.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  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/string/print_string.h"
  33. #include "core/string/translation.h"
  34. #include "scene/gui/container.h"
  35. #include "scene/theme/theme_db.h"
  36. #include "servers/text_server.h"
  37. void Label::set_autowrap_mode(TextServer::AutowrapMode p_mode) {
  38. if (autowrap_mode == p_mode) {
  39. return;
  40. }
  41. autowrap_mode = p_mode;
  42. lines_dirty = true;
  43. queue_redraw();
  44. update_configuration_warnings();
  45. if (clip || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  46. update_minimum_size();
  47. }
  48. }
  49. TextServer::AutowrapMode Label::get_autowrap_mode() const {
  50. return autowrap_mode;
  51. }
  52. void Label::set_justification_flags(BitField<TextServer::JustificationFlag> p_flags) {
  53. if (jst_flags == p_flags) {
  54. return;
  55. }
  56. jst_flags = p_flags;
  57. lines_dirty = true;
  58. queue_redraw();
  59. }
  60. BitField<TextServer::JustificationFlag> Label::get_justification_flags() const {
  61. return jst_flags;
  62. }
  63. void Label::set_uppercase(bool p_uppercase) {
  64. if (uppercase == p_uppercase) {
  65. return;
  66. }
  67. uppercase = p_uppercase;
  68. dirty = true;
  69. queue_redraw();
  70. }
  71. bool Label::is_uppercase() const {
  72. return uppercase;
  73. }
  74. int Label::get_line_height(int p_line) const {
  75. Ref<Font> font = (settings.is_valid() && settings->get_font().is_valid()) ? settings->get_font() : theme_cache.font;
  76. if (p_line >= 0 && p_line < lines_rid.size()) {
  77. return TS->shaped_text_get_size(lines_rid[p_line]).y;
  78. } else if (lines_rid.size() > 0) {
  79. int h = 0;
  80. for (int i = 0; i < lines_rid.size(); i++) {
  81. h = MAX(h, TS->shaped_text_get_size(lines_rid[i]).y);
  82. }
  83. return h;
  84. } else {
  85. int font_size = settings.is_valid() ? settings->get_font_size() : theme_cache.font_size;
  86. return font->get_height(font_size);
  87. }
  88. }
  89. void Label::_shape() {
  90. Ref<StyleBox> style = theme_cache.normal_style;
  91. int width = (get_size().width - style->get_minimum_size().width);
  92. if (dirty || font_dirty) {
  93. if (dirty) {
  94. TS->shaped_text_clear(text_rid);
  95. }
  96. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  97. TS->shaped_text_set_direction(text_rid, is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  98. } else {
  99. TS->shaped_text_set_direction(text_rid, (TextServer::Direction)text_direction);
  100. }
  101. const Ref<Font> &font = (settings.is_valid() && settings->get_font().is_valid()) ? settings->get_font() : theme_cache.font;
  102. int font_size = settings.is_valid() ? settings->get_font_size() : theme_cache.font_size;
  103. ERR_FAIL_COND(font.is_null());
  104. String txt = (uppercase) ? TS->string_to_upper(xl_text, language) : xl_text;
  105. if (visible_chars >= 0 && visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING) {
  106. txt = txt.substr(0, visible_chars);
  107. }
  108. if (dirty) {
  109. TS->shaped_text_add_string(text_rid, txt, font->get_rids(), font_size, font->get_opentype_features(), language);
  110. } else {
  111. int spans = TS->shaped_get_span_count(text_rid);
  112. for (int i = 0; i < spans; i++) {
  113. TS->shaped_set_span_update_font(text_rid, i, font->get_rids(), font_size, font->get_opentype_features());
  114. }
  115. }
  116. TS->shaped_text_set_bidi_override(text_rid, structured_text_parser(st_parser, st_args, txt));
  117. if (!tab_stops.is_empty()) {
  118. TS->shaped_text_tab_align(text_rid, tab_stops);
  119. }
  120. dirty = false;
  121. font_dirty = false;
  122. lines_dirty = true;
  123. }
  124. if (lines_dirty) {
  125. for (int i = 0; i < lines_rid.size(); i++) {
  126. TS->free_rid(lines_rid[i]);
  127. }
  128. lines_rid.clear();
  129. BitField<TextServer::LineBreakFlag> autowrap_flags = TextServer::BREAK_MANDATORY;
  130. switch (autowrap_mode) {
  131. case TextServer::AUTOWRAP_WORD_SMART:
  132. autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_ADAPTIVE | TextServer::BREAK_MANDATORY;
  133. break;
  134. case TextServer::AUTOWRAP_WORD:
  135. autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_MANDATORY;
  136. break;
  137. case TextServer::AUTOWRAP_ARBITRARY:
  138. autowrap_flags = TextServer::BREAK_GRAPHEME_BOUND | TextServer::BREAK_MANDATORY;
  139. break;
  140. case TextServer::AUTOWRAP_OFF:
  141. break;
  142. }
  143. autowrap_flags = autowrap_flags | TextServer::BREAK_TRIM_EDGE_SPACES;
  144. PackedInt32Array line_breaks = TS->shaped_text_get_line_breaks(text_rid, width, 0, autowrap_flags);
  145. for (int i = 0; i < line_breaks.size(); i = i + 2) {
  146. RID line = TS->shaped_text_substr(text_rid, line_breaks[i], line_breaks[i + 1] - line_breaks[i]);
  147. if (!tab_stops.is_empty()) {
  148. TS->shaped_text_tab_align(line, tab_stops);
  149. }
  150. lines_rid.push_back(line);
  151. }
  152. }
  153. if (xl_text.length() == 0) {
  154. minsize = Size2(1, get_line_height());
  155. return;
  156. }
  157. if (autowrap_mode == TextServer::AUTOWRAP_OFF) {
  158. minsize.width = 0.0f;
  159. for (int i = 0; i < lines_rid.size(); i++) {
  160. if (minsize.width < TS->shaped_text_get_size(lines_rid[i]).x) {
  161. minsize.width = TS->shaped_text_get_size(lines_rid[i]).x;
  162. }
  163. }
  164. }
  165. if (lines_dirty) {
  166. BitField<TextServer::TextOverrunFlag> overrun_flags = TextServer::OVERRUN_NO_TRIM;
  167. switch (overrun_behavior) {
  168. case TextServer::OVERRUN_TRIM_WORD_ELLIPSIS:
  169. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  170. overrun_flags.set_flag(TextServer::OVERRUN_TRIM_WORD_ONLY);
  171. overrun_flags.set_flag(TextServer::OVERRUN_ADD_ELLIPSIS);
  172. break;
  173. case TextServer::OVERRUN_TRIM_ELLIPSIS:
  174. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  175. overrun_flags.set_flag(TextServer::OVERRUN_ADD_ELLIPSIS);
  176. break;
  177. case TextServer::OVERRUN_TRIM_WORD:
  178. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  179. overrun_flags.set_flag(TextServer::OVERRUN_TRIM_WORD_ONLY);
  180. break;
  181. case TextServer::OVERRUN_TRIM_CHAR:
  182. overrun_flags.set_flag(TextServer::OVERRUN_TRIM);
  183. break;
  184. case TextServer::OVERRUN_NO_TRIMMING:
  185. break;
  186. }
  187. // Fill after min_size calculation.
  188. BitField<TextServer::JustificationFlag> line_jst_flags = jst_flags;
  189. if (!tab_stops.is_empty()) {
  190. line_jst_flags.set_flag(TextServer::JUSTIFICATION_AFTER_LAST_TAB);
  191. }
  192. if (autowrap_mode != TextServer::AUTOWRAP_OFF) {
  193. int visible_lines = get_visible_line_count();
  194. bool lines_hidden = visible_lines > 0 && visible_lines < lines_rid.size();
  195. if (lines_hidden) {
  196. overrun_flags.set_flag(TextServer::OVERRUN_ENFORCE_ELLIPSIS);
  197. }
  198. if (horizontal_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  199. int jst_to_line = visible_lines;
  200. if (lines_rid.size() == 1 && line_jst_flags.has_flag(TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE)) {
  201. jst_to_line = lines_rid.size();
  202. } else {
  203. if (line_jst_flags.has_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE)) {
  204. jst_to_line = visible_lines - 1;
  205. }
  206. if (line_jst_flags.has_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE_WITH_VISIBLE_CHARS)) {
  207. for (int i = visible_lines - 1; i >= 0; i--) {
  208. if (TS->shaped_text_has_visible_chars(lines_rid[i])) {
  209. jst_to_line = i;
  210. break;
  211. }
  212. }
  213. }
  214. }
  215. for (int i = 0; i < lines_rid.size(); i++) {
  216. if (i < jst_to_line) {
  217. TS->shaped_text_fit_to_width(lines_rid[i], width, line_jst_flags);
  218. } else if (i == (visible_lines - 1)) {
  219. TS->shaped_text_overrun_trim_to_width(lines_rid[i], width, overrun_flags);
  220. }
  221. }
  222. } else if (lines_hidden) {
  223. TS->shaped_text_overrun_trim_to_width(lines_rid[visible_lines - 1], width, overrun_flags);
  224. }
  225. } else {
  226. // Autowrap disabled.
  227. int jst_to_line = lines_rid.size();
  228. if (lines_rid.size() == 1 && line_jst_flags.has_flag(TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE)) {
  229. jst_to_line = lines_rid.size();
  230. } else {
  231. if (line_jst_flags.has_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE)) {
  232. jst_to_line = lines_rid.size() - 1;
  233. }
  234. if (line_jst_flags.has_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE_WITH_VISIBLE_CHARS)) {
  235. for (int i = lines_rid.size() - 1; i >= 0; i--) {
  236. if (TS->shaped_text_has_visible_chars(lines_rid[i])) {
  237. jst_to_line = i;
  238. break;
  239. }
  240. }
  241. }
  242. }
  243. for (int i = 0; i < lines_rid.size(); i++) {
  244. if (i < jst_to_line && horizontal_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  245. TS->shaped_text_fit_to_width(lines_rid[i], width, line_jst_flags);
  246. overrun_flags.set_flag(TextServer::OVERRUN_JUSTIFICATION_AWARE);
  247. TS->shaped_text_overrun_trim_to_width(lines_rid[i], width, overrun_flags);
  248. TS->shaped_text_fit_to_width(lines_rid[i], width, line_jst_flags | TextServer::JUSTIFICATION_CONSTRAIN_ELLIPSIS);
  249. } else {
  250. TS->shaped_text_overrun_trim_to_width(lines_rid[i], width, overrun_flags);
  251. }
  252. }
  253. }
  254. lines_dirty = false;
  255. }
  256. _update_visible();
  257. if (autowrap_mode == TextServer::AUTOWRAP_OFF || !clip || overrun_behavior == TextServer::OVERRUN_NO_TRIMMING) {
  258. update_minimum_size();
  259. }
  260. }
  261. void Label::_update_visible() {
  262. int line_spacing = settings.is_valid() ? settings->get_line_spacing() : theme_cache.line_spacing;
  263. Ref<StyleBox> style = theme_cache.normal_style;
  264. int lines_visible = lines_rid.size();
  265. if (max_lines_visible >= 0 && lines_visible > max_lines_visible) {
  266. lines_visible = max_lines_visible;
  267. }
  268. minsize.height = 0;
  269. int last_line = MIN(lines_rid.size(), lines_visible + lines_skipped);
  270. for (int64_t i = lines_skipped; i < last_line; i++) {
  271. minsize.height += TS->shaped_text_get_size(lines_rid[i]).y + line_spacing;
  272. }
  273. if (minsize.height > 0) {
  274. minsize.height -= line_spacing;
  275. }
  276. }
  277. inline void draw_glyph(const Glyph &p_gl, const RID &p_canvas, const Color &p_font_color, const Vector2 &p_ofs) {
  278. if (p_gl.font_rid != RID()) {
  279. 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);
  280. } else {
  281. 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);
  282. }
  283. }
  284. 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) {
  285. if (p_gl.font_rid != RID()) {
  286. if (p_font_shadow_color.a > 0) {
  287. 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);
  288. }
  289. if (p_font_shadow_color.a > 0 && p_shadow_outline_size > 0) {
  290. 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);
  291. }
  292. if (p_font_outline_color.a != 0.0 && p_outline_size > 0) {
  293. 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);
  294. }
  295. }
  296. }
  297. PackedStringArray Label::get_configuration_warnings() const {
  298. PackedStringArray warnings = Control::get_configuration_warnings();
  299. // FIXME: This is not ideal and the sizing model should be fixed,
  300. // but for now we have to warn about this impossible to resolve combination.
  301. // See GH-83546.
  302. if (is_inside_tree() && get_tree()->get_edited_scene_root() != this) {
  303. // If the Label happens to be the root node of the edited scene, we don't need
  304. // to check what its parent is. It's going to be some node from the editor tree
  305. // and it can be a container, but that makes no difference to the user.
  306. Container *parent_container = Object::cast_to<Container>(get_parent_control());
  307. if (parent_container && autowrap_mode != TextServer::AUTOWRAP_OFF && get_custom_minimum_size() == Size2()) {
  308. warnings.push_back(RTR("Labels with autowrapping enabled must have a custom minimum size configured to work correctly inside a container."));
  309. }
  310. }
  311. // Ensure that the font can render all of the required glyphs.
  312. Ref<Font> font;
  313. if (settings.is_valid()) {
  314. font = settings->get_font();
  315. }
  316. if (font.is_null()) {
  317. font = theme_cache.font;
  318. }
  319. if (font.is_valid()) {
  320. if (dirty || font_dirty || lines_dirty) {
  321. const_cast<Label *>(this)->_shape();
  322. }
  323. const Glyph *glyph = TS->shaped_text_get_glyphs(text_rid);
  324. int64_t glyph_count = TS->shaped_text_get_glyph_count(text_rid);
  325. for (int64_t i = 0; i < glyph_count; i++) {
  326. if (glyph[i].font_rid == RID()) {
  327. warnings.push_back(RTR("The current font does not support rendering one or more characters used in this Label's text."));
  328. break;
  329. }
  330. }
  331. }
  332. return warnings;
  333. }
  334. void Label::_notification(int p_what) {
  335. switch (p_what) {
  336. case NOTIFICATION_TRANSLATION_CHANGED: {
  337. String new_text = atr(text);
  338. if (new_text == xl_text) {
  339. return; // Nothing new.
  340. }
  341. xl_text = new_text;
  342. if (visible_ratio < 1) {
  343. visible_chars = get_total_character_count() * visible_ratio;
  344. }
  345. dirty = true;
  346. queue_redraw();
  347. update_configuration_warnings();
  348. } break;
  349. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  350. queue_redraw();
  351. } break;
  352. case NOTIFICATION_DRAW: {
  353. if (clip) {
  354. RenderingServer::get_singleton()->canvas_item_set_clip(get_canvas_item(), true);
  355. }
  356. // When a shaped text is invalidated by an external source, we want to reshape it.
  357. if (!TS->shaped_text_is_ready(text_rid)) {
  358. dirty = true;
  359. }
  360. for (const RID &line_rid : lines_rid) {
  361. if (!TS->shaped_text_is_ready(line_rid)) {
  362. lines_dirty = true;
  363. break;
  364. }
  365. }
  366. if (dirty || font_dirty || lines_dirty) {
  367. _shape();
  368. }
  369. RID ci = get_canvas_item();
  370. bool has_settings = settings.is_valid();
  371. Size2 string_size;
  372. Size2 size = get_size();
  373. Ref<StyleBox> style = theme_cache.normal_style;
  374. Ref<Font> font = (has_settings && settings->get_font().is_valid()) ? settings->get_font() : theme_cache.font;
  375. Color font_color = has_settings ? settings->get_font_color() : theme_cache.font_color;
  376. Color font_shadow_color = has_settings ? settings->get_shadow_color() : theme_cache.font_shadow_color;
  377. Point2 shadow_ofs = has_settings ? settings->get_shadow_offset() : theme_cache.font_shadow_offset;
  378. int line_spacing = has_settings ? settings->get_line_spacing() : theme_cache.line_spacing;
  379. Color font_outline_color = has_settings ? settings->get_outline_color() : theme_cache.font_outline_color;
  380. int outline_size = has_settings ? settings->get_outline_size() : theme_cache.font_outline_size;
  381. int shadow_outline_size = has_settings ? settings->get_shadow_size() : theme_cache.font_shadow_outline_size;
  382. bool rtl = (TS->shaped_text_get_inferred_direction(text_rid) == TextServer::DIRECTION_RTL);
  383. bool rtl_layout = is_layout_rtl();
  384. style->draw(ci, Rect2(Point2(0, 0), get_size()));
  385. float total_h = 0.0;
  386. int lines_visible = 0;
  387. // Get number of lines to fit to the height.
  388. for (int64_t i = lines_skipped; i < lines_rid.size(); i++) {
  389. total_h += TS->shaped_text_get_size(lines_rid[i]).y + line_spacing;
  390. if (total_h > (get_size().height - style->get_minimum_size().height + line_spacing)) {
  391. break;
  392. }
  393. lines_visible++;
  394. }
  395. if (max_lines_visible >= 0 && lines_visible > max_lines_visible) {
  396. lines_visible = max_lines_visible;
  397. }
  398. int last_line = MIN(lines_rid.size(), lines_visible + lines_skipped);
  399. bool trim_chars = (visible_chars >= 0) && (visible_chars_behavior == TextServer::VC_CHARS_AFTER_SHAPING);
  400. bool trim_glyphs_ltr = (visible_chars >= 0) && ((visible_chars_behavior == TextServer::VC_GLYPHS_LTR) || ((visible_chars_behavior == TextServer::VC_GLYPHS_AUTO) && !rtl_layout));
  401. bool trim_glyphs_rtl = (visible_chars >= 0) && ((visible_chars_behavior == TextServer::VC_GLYPHS_RTL) || ((visible_chars_behavior == TextServer::VC_GLYPHS_AUTO) && rtl_layout));
  402. // Get real total height.
  403. int total_glyphs = 0;
  404. total_h = 0;
  405. for (int64_t i = lines_skipped; i < last_line; i++) {
  406. total_h += TS->shaped_text_get_size(lines_rid[i]).y + line_spacing;
  407. total_glyphs += TS->shaped_text_get_glyph_count(lines_rid[i]) + TS->shaped_text_get_ellipsis_glyph_count(lines_rid[i]);
  408. }
  409. int visible_glyphs = total_glyphs * visible_ratio;
  410. int processed_glyphs = 0;
  411. total_h += style->get_margin(SIDE_TOP) + style->get_margin(SIDE_BOTTOM);
  412. int vbegin = 0, vsep = 0;
  413. if (lines_visible > 0) {
  414. switch (vertical_alignment) {
  415. case VERTICAL_ALIGNMENT_TOP: {
  416. // Nothing.
  417. } break;
  418. case VERTICAL_ALIGNMENT_CENTER: {
  419. vbegin = (size.y - (total_h - line_spacing)) / 2;
  420. vsep = 0;
  421. } break;
  422. case VERTICAL_ALIGNMENT_BOTTOM: {
  423. vbegin = size.y - (total_h - line_spacing);
  424. vsep = 0;
  425. } break;
  426. case VERTICAL_ALIGNMENT_FILL: {
  427. vbegin = 0;
  428. if (lines_visible > 1) {
  429. vsep = (size.y - (total_h - line_spacing)) / (lines_visible - 1);
  430. } else {
  431. vsep = 0;
  432. }
  433. } break;
  434. }
  435. }
  436. Vector2 ofs;
  437. ofs.y = style->get_offset().y + vbegin;
  438. for (int i = lines_skipped; i < last_line; i++) {
  439. Size2 line_size = TS->shaped_text_get_size(lines_rid[i]);
  440. ofs.x = 0;
  441. ofs.y += TS->shaped_text_get_ascent(lines_rid[i]);
  442. switch (horizontal_alignment) {
  443. case HORIZONTAL_ALIGNMENT_FILL:
  444. if (rtl && autowrap_mode != TextServer::AUTOWRAP_OFF) {
  445. ofs.x = int(size.width - style->get_margin(SIDE_RIGHT) - line_size.width);
  446. } else {
  447. ofs.x = style->get_offset().x;
  448. }
  449. break;
  450. case HORIZONTAL_ALIGNMENT_LEFT: {
  451. if (rtl_layout) {
  452. ofs.x = int(size.width - style->get_margin(SIDE_RIGHT) - line_size.width);
  453. } else {
  454. ofs.x = style->get_offset().x;
  455. }
  456. } break;
  457. case HORIZONTAL_ALIGNMENT_CENTER: {
  458. ofs.x = int(size.width - line_size.width) / 2;
  459. } break;
  460. case HORIZONTAL_ALIGNMENT_RIGHT: {
  461. if (rtl_layout) {
  462. ofs.x = style->get_offset().x;
  463. } else {
  464. ofs.x = int(size.width - style->get_margin(SIDE_RIGHT) - line_size.width);
  465. }
  466. } break;
  467. }
  468. const Glyph *glyphs = TS->shaped_text_get_glyphs(lines_rid[i]);
  469. int gl_size = TS->shaped_text_get_glyph_count(lines_rid[i]);
  470. int ellipsis_pos = TS->shaped_text_get_ellipsis_pos(lines_rid[i]);
  471. int trim_pos = TS->shaped_text_get_trim_pos(lines_rid[i]);
  472. const Glyph *ellipsis_glyphs = TS->shaped_text_get_ellipsis_glyphs(lines_rid[i]);
  473. int ellipsis_gl_size = TS->shaped_text_get_ellipsis_glyph_count(lines_rid[i]);
  474. // Draw outline. Note: Do not merge this into the single loop with the main text, to prevent overlaps.
  475. int processed_glyphs_ol = processed_glyphs;
  476. if ((outline_size > 0 && font_outline_color.a != 0) || (font_shadow_color.a != 0)) {
  477. Vector2 offset = ofs;
  478. // Draw RTL ellipsis string when necessary.
  479. if (rtl && ellipsis_pos >= 0) {
  480. for (int gl_idx = ellipsis_gl_size - 1; gl_idx >= 0; gl_idx--) {
  481. for (int j = 0; j < ellipsis_glyphs[gl_idx].repeat; j++) {
  482. 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));
  483. //Draw glyph outlines and shadow.
  484. if (!skip) {
  485. draw_glyph_outline(ellipsis_glyphs[gl_idx], ci, font_color, font_shadow_color, font_outline_color, shadow_outline_size, outline_size, offset, shadow_ofs);
  486. }
  487. processed_glyphs_ol++;
  488. offset.x += ellipsis_glyphs[gl_idx].advance;
  489. }
  490. }
  491. }
  492. // Draw main text.
  493. for (int j = 0; j < gl_size; j++) {
  494. // Trim when necessary.
  495. if (trim_pos >= 0) {
  496. if (rtl) {
  497. if (j < trim_pos) {
  498. continue;
  499. }
  500. } else {
  501. if (j >= trim_pos) {
  502. break;
  503. }
  504. }
  505. }
  506. for (int k = 0; k < glyphs[j].repeat; k++) {
  507. 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));
  508. // Draw glyph outlines and shadow.
  509. if (!skip) {
  510. draw_glyph_outline(glyphs[j], ci, font_color, font_shadow_color, font_outline_color, shadow_outline_size, outline_size, offset, shadow_ofs);
  511. }
  512. processed_glyphs_ol++;
  513. offset.x += glyphs[j].advance;
  514. }
  515. }
  516. // Draw LTR ellipsis string when necessary.
  517. if (!rtl && ellipsis_pos >= 0) {
  518. for (int gl_idx = 0; gl_idx < ellipsis_gl_size; gl_idx++) {
  519. for (int j = 0; j < ellipsis_glyphs[gl_idx].repeat; j++) {
  520. 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));
  521. //Draw glyph outlines and shadow.
  522. if (!skip) {
  523. draw_glyph_outline(ellipsis_glyphs[gl_idx], ci, font_color, font_shadow_color, font_outline_color, shadow_outline_size, outline_size, offset, shadow_ofs);
  524. }
  525. processed_glyphs_ol++;
  526. offset.x += ellipsis_glyphs[gl_idx].advance;
  527. }
  528. }
  529. }
  530. }
  531. // Draw main text. Note: Do not merge this into the single loop with the outline, to prevent overlaps.
  532. // Draw RTL ellipsis string when necessary.
  533. if (rtl && ellipsis_pos >= 0) {
  534. for (int gl_idx = ellipsis_gl_size - 1; gl_idx >= 0; gl_idx--) {
  535. for (int j = 0; j < ellipsis_glyphs[gl_idx].repeat; j++) {
  536. 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));
  537. //Draw glyph outlines and shadow.
  538. if (!skip) {
  539. draw_glyph(ellipsis_glyphs[gl_idx], ci, font_color, ofs);
  540. }
  541. processed_glyphs++;
  542. ofs.x += ellipsis_glyphs[gl_idx].advance;
  543. }
  544. }
  545. }
  546. // Draw main text.
  547. for (int j = 0; j < gl_size; j++) {
  548. // Trim when necessary.
  549. if (trim_pos >= 0) {
  550. if (rtl) {
  551. if (j < trim_pos) {
  552. continue;
  553. }
  554. } else {
  555. if (j >= trim_pos) {
  556. break;
  557. }
  558. }
  559. }
  560. for (int k = 0; k < glyphs[j].repeat; k++) {
  561. 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));
  562. // Draw glyph outlines and shadow.
  563. if (!skip) {
  564. draw_glyph(glyphs[j], ci, font_color, ofs);
  565. }
  566. processed_glyphs++;
  567. ofs.x += glyphs[j].advance;
  568. }
  569. }
  570. // Draw LTR ellipsis string when necessary.
  571. if (!rtl && ellipsis_pos >= 0) {
  572. for (int gl_idx = 0; gl_idx < ellipsis_gl_size; gl_idx++) {
  573. for (int j = 0; j < ellipsis_glyphs[gl_idx].repeat; j++) {
  574. 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));
  575. //Draw glyph outlines and shadow.
  576. if (!skip) {
  577. draw_glyph(ellipsis_glyphs[gl_idx], ci, font_color, ofs);
  578. }
  579. processed_glyphs++;
  580. ofs.x += ellipsis_glyphs[gl_idx].advance;
  581. }
  582. }
  583. }
  584. ofs.y += TS->shaped_text_get_descent(lines_rid[i]) + vsep + line_spacing;
  585. }
  586. } break;
  587. case NOTIFICATION_THEME_CHANGED: {
  588. font_dirty = true;
  589. queue_redraw();
  590. } break;
  591. case NOTIFICATION_RESIZED: {
  592. lines_dirty = true;
  593. } break;
  594. }
  595. }
  596. Size2 Label::get_minimum_size() const {
  597. // don't want to mutable everything
  598. if (dirty || font_dirty || lines_dirty) {
  599. const_cast<Label *>(this)->_shape();
  600. }
  601. Size2 min_size = minsize;
  602. const Ref<Font> &font = (settings.is_valid() && settings->get_font().is_valid()) ? settings->get_font() : theme_cache.font;
  603. int font_size = settings.is_valid() ? settings->get_font_size() : theme_cache.font_size;
  604. min_size.height = MAX(min_size.height, font->get_height(font_size) + font->get_spacing(TextServer::SPACING_TOP) + font->get_spacing(TextServer::SPACING_BOTTOM));
  605. Size2 min_style = theme_cache.normal_style->get_minimum_size();
  606. if (autowrap_mode != TextServer::AUTOWRAP_OFF) {
  607. return Size2(1, (clip || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) ? 1 : min_size.height) + min_style;
  608. } else {
  609. if (clip || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  610. min_size.width = 1;
  611. }
  612. return min_size + min_style;
  613. }
  614. }
  615. int Label::get_line_count() const {
  616. if (!is_inside_tree()) {
  617. return 1;
  618. }
  619. if (dirty || font_dirty || lines_dirty) {
  620. const_cast<Label *>(this)->_shape();
  621. }
  622. return lines_rid.size();
  623. }
  624. int Label::get_visible_line_count() const {
  625. Ref<StyleBox> style = theme_cache.normal_style;
  626. int line_spacing = settings.is_valid() ? settings->get_line_spacing() : theme_cache.line_spacing;
  627. int lines_visible = 0;
  628. float total_h = 0.0;
  629. for (int64_t i = lines_skipped; i < lines_rid.size(); i++) {
  630. total_h += TS->shaped_text_get_size(lines_rid[i]).y + line_spacing;
  631. if (total_h > (get_size().height - style->get_minimum_size().height + line_spacing)) {
  632. break;
  633. }
  634. lines_visible++;
  635. }
  636. if (lines_visible > lines_rid.size()) {
  637. lines_visible = lines_rid.size();
  638. }
  639. if (max_lines_visible >= 0 && lines_visible > max_lines_visible) {
  640. lines_visible = max_lines_visible;
  641. }
  642. return lines_visible;
  643. }
  644. void Label::set_horizontal_alignment(HorizontalAlignment p_alignment) {
  645. ERR_FAIL_INDEX((int)p_alignment, 4);
  646. if (horizontal_alignment == p_alignment) {
  647. return;
  648. }
  649. if (horizontal_alignment == HORIZONTAL_ALIGNMENT_FILL || p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
  650. lines_dirty = true; // Reshape lines.
  651. }
  652. horizontal_alignment = p_alignment;
  653. queue_redraw();
  654. }
  655. HorizontalAlignment Label::get_horizontal_alignment() const {
  656. return horizontal_alignment;
  657. }
  658. void Label::set_vertical_alignment(VerticalAlignment p_alignment) {
  659. ERR_FAIL_INDEX((int)p_alignment, 4);
  660. if (vertical_alignment == p_alignment) {
  661. return;
  662. }
  663. vertical_alignment = p_alignment;
  664. queue_redraw();
  665. }
  666. VerticalAlignment Label::get_vertical_alignment() const {
  667. return vertical_alignment;
  668. }
  669. void Label::set_text(const String &p_string) {
  670. if (text == p_string) {
  671. return;
  672. }
  673. text = p_string;
  674. xl_text = atr(p_string);
  675. dirty = true;
  676. if (visible_ratio < 1) {
  677. visible_chars = get_total_character_count() * visible_ratio;
  678. }
  679. queue_redraw();
  680. update_minimum_size();
  681. update_configuration_warnings();
  682. }
  683. void Label::_invalidate() {
  684. font_dirty = true;
  685. queue_redraw();
  686. }
  687. void Label::set_label_settings(const Ref<LabelSettings> &p_settings) {
  688. if (settings != p_settings) {
  689. if (settings.is_valid()) {
  690. settings->disconnect_changed(callable_mp(this, &Label::_invalidate));
  691. }
  692. settings = p_settings;
  693. if (settings.is_valid()) {
  694. settings->connect_changed(callable_mp(this, &Label::_invalidate), CONNECT_REFERENCE_COUNTED);
  695. }
  696. _invalidate();
  697. }
  698. }
  699. Ref<LabelSettings> Label::get_label_settings() const {
  700. return settings;
  701. }
  702. void Label::set_text_direction(Control::TextDirection p_text_direction) {
  703. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  704. if (text_direction != p_text_direction) {
  705. text_direction = p_text_direction;
  706. font_dirty = true;
  707. queue_redraw();
  708. }
  709. }
  710. void Label::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) {
  711. if (st_parser != p_parser) {
  712. st_parser = p_parser;
  713. dirty = true;
  714. queue_redraw();
  715. }
  716. }
  717. TextServer::StructuredTextParser Label::get_structured_text_bidi_override() const {
  718. return st_parser;
  719. }
  720. void Label::set_structured_text_bidi_override_options(Array p_args) {
  721. if (st_args == p_args) {
  722. return;
  723. }
  724. st_args = p_args;
  725. dirty = true;
  726. queue_redraw();
  727. }
  728. Array Label::get_structured_text_bidi_override_options() const {
  729. return st_args;
  730. }
  731. Control::TextDirection Label::get_text_direction() const {
  732. return text_direction;
  733. }
  734. void Label::set_language(const String &p_language) {
  735. if (language != p_language) {
  736. language = p_language;
  737. dirty = true;
  738. queue_redraw();
  739. }
  740. }
  741. String Label::get_language() const {
  742. return language;
  743. }
  744. void Label::set_clip_text(bool p_clip) {
  745. if (clip == p_clip) {
  746. return;
  747. }
  748. clip = p_clip;
  749. queue_redraw();
  750. update_minimum_size();
  751. }
  752. bool Label::is_clipping_text() const {
  753. return clip;
  754. }
  755. void Label::set_tab_stops(const PackedFloat32Array &p_tab_stops) {
  756. if (tab_stops != p_tab_stops) {
  757. tab_stops = p_tab_stops;
  758. dirty = true;
  759. queue_redraw();
  760. }
  761. }
  762. PackedFloat32Array Label::get_tab_stops() const {
  763. return tab_stops;
  764. }
  765. void Label::set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior) {
  766. if (overrun_behavior == p_behavior) {
  767. return;
  768. }
  769. overrun_behavior = p_behavior;
  770. lines_dirty = true;
  771. queue_redraw();
  772. if (clip || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
  773. update_minimum_size();
  774. }
  775. }
  776. TextServer::OverrunBehavior Label::get_text_overrun_behavior() const {
  777. return overrun_behavior;
  778. }
  779. String Label::get_text() const {
  780. return text;
  781. }
  782. void Label::set_visible_characters(int p_amount) {
  783. if (visible_chars != p_amount) {
  784. visible_chars = p_amount;
  785. if (get_total_character_count() > 0) {
  786. visible_ratio = (float)p_amount / (float)get_total_character_count();
  787. } else {
  788. visible_ratio = 1.0;
  789. }
  790. if (visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING) {
  791. dirty = true;
  792. }
  793. queue_redraw();
  794. }
  795. }
  796. int Label::get_visible_characters() const {
  797. return visible_chars;
  798. }
  799. void Label::set_visible_ratio(float p_ratio) {
  800. if (visible_ratio != p_ratio) {
  801. if (p_ratio >= 1.0) {
  802. visible_chars = -1;
  803. visible_ratio = 1.0;
  804. } else if (p_ratio < 0.0) {
  805. visible_chars = 0;
  806. visible_ratio = 0.0;
  807. } else {
  808. visible_chars = get_total_character_count() * p_ratio;
  809. visible_ratio = p_ratio;
  810. }
  811. if (visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING) {
  812. dirty = true;
  813. }
  814. queue_redraw();
  815. }
  816. }
  817. float Label::get_visible_ratio() const {
  818. return visible_ratio;
  819. }
  820. TextServer::VisibleCharactersBehavior Label::get_visible_characters_behavior() const {
  821. return visible_chars_behavior;
  822. }
  823. void Label::set_visible_characters_behavior(TextServer::VisibleCharactersBehavior p_behavior) {
  824. if (visible_chars_behavior != p_behavior) {
  825. visible_chars_behavior = p_behavior;
  826. dirty = true;
  827. queue_redraw();
  828. }
  829. }
  830. void Label::set_lines_skipped(int p_lines) {
  831. ERR_FAIL_COND(p_lines < 0);
  832. if (lines_skipped == p_lines) {
  833. return;
  834. }
  835. lines_skipped = p_lines;
  836. _update_visible();
  837. queue_redraw();
  838. }
  839. int Label::get_lines_skipped() const {
  840. return lines_skipped;
  841. }
  842. void Label::set_max_lines_visible(int p_lines) {
  843. if (max_lines_visible == p_lines) {
  844. return;
  845. }
  846. max_lines_visible = p_lines;
  847. _update_visible();
  848. queue_redraw();
  849. }
  850. int Label::get_max_lines_visible() const {
  851. return max_lines_visible;
  852. }
  853. int Label::get_total_character_count() const {
  854. if (dirty || font_dirty || lines_dirty) {
  855. const_cast<Label *>(this)->_shape();
  856. }
  857. return xl_text.length();
  858. }
  859. void Label::_bind_methods() {
  860. ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &Label::set_horizontal_alignment);
  861. ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &Label::get_horizontal_alignment);
  862. ClassDB::bind_method(D_METHOD("set_vertical_alignment", "alignment"), &Label::set_vertical_alignment);
  863. ClassDB::bind_method(D_METHOD("get_vertical_alignment"), &Label::get_vertical_alignment);
  864. ClassDB::bind_method(D_METHOD("set_text", "text"), &Label::set_text);
  865. ClassDB::bind_method(D_METHOD("get_text"), &Label::get_text);
  866. ClassDB::bind_method(D_METHOD("set_label_settings", "settings"), &Label::set_label_settings);
  867. ClassDB::bind_method(D_METHOD("get_label_settings"), &Label::get_label_settings);
  868. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &Label::set_text_direction);
  869. ClassDB::bind_method(D_METHOD("get_text_direction"), &Label::get_text_direction);
  870. ClassDB::bind_method(D_METHOD("set_language", "language"), &Label::set_language);
  871. ClassDB::bind_method(D_METHOD("get_language"), &Label::get_language);
  872. ClassDB::bind_method(D_METHOD("set_autowrap_mode", "autowrap_mode"), &Label::set_autowrap_mode);
  873. ClassDB::bind_method(D_METHOD("get_autowrap_mode"), &Label::get_autowrap_mode);
  874. ClassDB::bind_method(D_METHOD("set_justification_flags", "justification_flags"), &Label::set_justification_flags);
  875. ClassDB::bind_method(D_METHOD("get_justification_flags"), &Label::get_justification_flags);
  876. ClassDB::bind_method(D_METHOD("set_clip_text", "enable"), &Label::set_clip_text);
  877. ClassDB::bind_method(D_METHOD("is_clipping_text"), &Label::is_clipping_text);
  878. ClassDB::bind_method(D_METHOD("set_tab_stops", "tab_stops"), &Label::set_tab_stops);
  879. ClassDB::bind_method(D_METHOD("get_tab_stops"), &Label::get_tab_stops);
  880. ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "overrun_behavior"), &Label::set_text_overrun_behavior);
  881. ClassDB::bind_method(D_METHOD("get_text_overrun_behavior"), &Label::get_text_overrun_behavior);
  882. ClassDB::bind_method(D_METHOD("set_uppercase", "enable"), &Label::set_uppercase);
  883. ClassDB::bind_method(D_METHOD("is_uppercase"), &Label::is_uppercase);
  884. ClassDB::bind_method(D_METHOD("get_line_height", "line"), &Label::get_line_height, DEFVAL(-1));
  885. ClassDB::bind_method(D_METHOD("get_line_count"), &Label::get_line_count);
  886. ClassDB::bind_method(D_METHOD("get_visible_line_count"), &Label::get_visible_line_count);
  887. ClassDB::bind_method(D_METHOD("get_total_character_count"), &Label::get_total_character_count);
  888. ClassDB::bind_method(D_METHOD("set_visible_characters", "amount"), &Label::set_visible_characters);
  889. ClassDB::bind_method(D_METHOD("get_visible_characters"), &Label::get_visible_characters);
  890. ClassDB::bind_method(D_METHOD("get_visible_characters_behavior"), &Label::get_visible_characters_behavior);
  891. ClassDB::bind_method(D_METHOD("set_visible_characters_behavior", "behavior"), &Label::set_visible_characters_behavior);
  892. ClassDB::bind_method(D_METHOD("set_visible_ratio", "ratio"), &Label::set_visible_ratio);
  893. ClassDB::bind_method(D_METHOD("get_visible_ratio"), &Label::get_visible_ratio);
  894. ClassDB::bind_method(D_METHOD("set_lines_skipped", "lines_skipped"), &Label::set_lines_skipped);
  895. ClassDB::bind_method(D_METHOD("get_lines_skipped"), &Label::get_lines_skipped);
  896. ClassDB::bind_method(D_METHOD("set_max_lines_visible", "lines_visible"), &Label::set_max_lines_visible);
  897. ClassDB::bind_method(D_METHOD("get_max_lines_visible"), &Label::get_max_lines_visible);
  898. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &Label::set_structured_text_bidi_override);
  899. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &Label::get_structured_text_bidi_override);
  900. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &Label::set_structured_text_bidi_override_options);
  901. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &Label::get_structured_text_bidi_override_options);
  902. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
  903. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "label_settings", PROPERTY_HINT_RESOURCE_TYPE, "LabelSettings"), "set_label_settings", "get_label_settings");
  904. ADD_PROPERTY(PropertyInfo(Variant::INT, "horizontal_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_horizontal_alignment", "get_horizontal_alignment");
  905. ADD_PROPERTY(PropertyInfo(Variant::INT, "vertical_alignment", PROPERTY_HINT_ENUM, "Top,Center,Bottom,Fill"), "set_vertical_alignment", "get_vertical_alignment");
  906. ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_mode", PROPERTY_HINT_ENUM, "Off,Arbitrary,Word,Word (Smart)"), "set_autowrap_mode", "get_autowrap_mode");
  907. ADD_PROPERTY(PropertyInfo(Variant::INT, "justification_flags", PROPERTY_HINT_FLAGS, "Kashida Justification:1,Word Justification:2,Justify Only After Last Tab:8,Skip Last Line:32,Skip Last Line With Visible Characters:64,Do Not Skip Single Line:128"), "set_justification_flags", "get_justification_flags");
  908. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_text"), "set_clip_text", "is_clipping_text");
  909. 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");
  910. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "uppercase"), "set_uppercase", "is_uppercase");
  911. ADD_PROPERTY(PropertyInfo(Variant::PACKED_FLOAT32_ARRAY, "tab_stops"), "set_tab_stops", "get_tab_stops");
  912. ADD_GROUP("Displayed Text", "");
  913. ADD_PROPERTY(PropertyInfo(Variant::INT, "lines_skipped", PROPERTY_HINT_RANGE, "0,999,1"), "set_lines_skipped", "get_lines_skipped");
  914. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_lines_visible", PROPERTY_HINT_RANGE, "-1,999,1"), "set_max_lines_visible", "get_max_lines_visible");
  915. // Note: "visible_characters" and "visible_ratio" should be set after "text" to be correctly applied.
  916. ADD_PROPERTY(PropertyInfo(Variant::INT, "visible_characters", PROPERTY_HINT_RANGE, "-1,128000,1"), "set_visible_characters", "get_visible_characters");
  917. 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");
  918. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "visible_ratio", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_visible_ratio", "get_visible_ratio");
  919. ADD_GROUP("BiDi", "");
  920. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  921. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  922. 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");
  923. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");
  924. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, Label, normal_style, "normal");
  925. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Label, line_spacing);
  926. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, Label, font);
  927. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, Label, font_size);
  928. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Label, font_color);
  929. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Label, font_shadow_color);
  930. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, Label, font_shadow_offset.x, "shadow_offset_x");
  931. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, Label, font_shadow_offset.y, "shadow_offset_y");
  932. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Label, font_outline_color);
  933. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, Label, font_outline_size, "outline_size");
  934. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, Label, font_shadow_outline_size, "shadow_outline_size");
  935. }
  936. Label::Label(const String &p_text) {
  937. text_rid = TS->create_shaped_text();
  938. set_mouse_filter(MOUSE_FILTER_IGNORE);
  939. set_text(p_text);
  940. set_v_size_flags(SIZE_SHRINK_CENTER);
  941. }
  942. Label::~Label() {
  943. for (int i = 0; i < lines_rid.size(); i++) {
  944. TS->free_rid(lines_rid[i]);
  945. }
  946. lines_rid.clear();
  947. TS->free_rid(text_rid);
  948. }