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