Debian Codegraph @

scene/gui/rich_text_label.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* rich_text_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 "rich_text_label.h"
32 #include "rich_text_label.compat.inc"
33
34 #include "core/input/input_map.h"
35 #include "core/io/resource_loader.h"
36 #include "core/math/math_defs.h"
37 #include "core/os/keyboard.h"
38 #include "core/os/os.h"
39 #include "core/string/translation_server.h"
40 #include "scene/gui/label.h"
41 #include "scene/gui/rich_text_effect.h"
42 #include "scene/main/timer.h"
43 #include "scene/resources/atlas_texture.h"
44 #include "scene/theme/theme_db.h"
45 #include "servers/display/display_server.h"
46
47 #include "modules/modules_enabled.gen.h" // For regex.
48 #ifdef MODULE_REGEX_ENABLED
49 #include "modules/regex/regex.h"
50 #endif
51
52 RichTextLabel::ItemCustomFX::ItemCustomFX() {
53 type = ITEM_CUSTOMFX;
54 char_fx_transform.instantiate();
55 }
56
57 RichTextLabel::ItemCustomFX::~ItemCustomFX() {
58 _clear_children();
59
60 char_fx_transform.unref();
61 custom_effect.unref();
62 }
63
64 Rect2i _merge_or_copy_rect(const Rect2i &p_a, const Rect2i &p_b) {
65 if (!p_a.has_area()) {
66 return p_b;
67 } else {
68 return p_a.merge(p_b);
69 }
70 }
71
72 RichTextLabel::Item *RichTextLabel::_get_next_item(Item *p_item, bool p_free) const {
73 if (!p_item) {
74 return nullptr;
75 }
76 if (p_free) {
77 if (p_item->subitems.size()) {
78 return p_item->subitems.front()->get();
79 } else if (!p_item->parent) {
80 return nullptr;
81 } else if (p_item->E->next()) {
82 return p_item->E->next()->get();
83 } else {
84 // Go up until something with a next is found.
85 while (p_item->parent && !p_item->E->next()) {
86 p_item = p_item->parent;
87 }
88
89 if (p_item->parent) {
90 return p_item->E->next()->get();
91 } else {
92 return nullptr;
93 }
94 }
95
96 } else {
97 if (p_item->subitems.size() && p_item->type != ITEM_TABLE) {
98 return p_item->subitems.front()->get();
99 } else if (p_item->type == ITEM_FRAME) {
100 return nullptr;
101 } else if (p_item->E->next()) {
102 return p_item->E->next()->get();
103 } else {
104 // Go up until something with a next is found.
105 while (p_item->parent && p_item->type != ITEM_FRAME && !p_item->E->next()) {
106 p_item = p_item->parent;
107 }
108
109 if (p_item->type != ITEM_FRAME) {
110 return p_item->E->next()->get();
111 } else {
112 return nullptr;
113 }
114 }
115 }
116 }
117
118 RichTextLabel::Item *RichTextLabel::_get_prev_item(Item *p_item, bool p_free) const {
119 if (!p_item) {
120 return nullptr;
121 }
122 if (p_free) {
123 if (!p_item->parent) {
124 return nullptr;
125 } else if (p_item->E->prev()) {
126 p_item = p_item->E->prev()->get();
127 while (p_item->subitems.size()) {
128 p_item = p_item->subitems.back()->get();
129 }
130 return p_item;
131 } else {
132 if (p_item->parent) {
133 return p_item->parent;
134 } else {
135 return nullptr;
136 }
137 }
138
139 } else {
140 if (p_item->type == ITEM_FRAME) {
141 return nullptr;
142 } else if (p_item->E->prev()) {
143 p_item = p_item->E->prev()->get();
144 while (p_item->subitems.size() && p_item->type != ITEM_TABLE) {
145 p_item = p_item->subitems.back()->get();
146 }
147 return p_item;
148 } else {
149 if (p_item->parent && p_item->type != ITEM_FRAME) {
150 return p_item->parent;
151 } else {
152 return nullptr;
153 }
154 }
155 }
156 }
157
158 Rect2 RichTextLabel::_get_text_rect() {
159 return Rect2(theme_cache.normal_style->get_offset(), get_size() - theme_cache.normal_style->get_minimum_size());
160 }
161
162 RichTextLabel::Item *RichTextLabel::_get_item_at_pos(RichTextLabel::Item *p_item_from, RichTextLabel::Item *p_item_to, int p_position) {
163 int offset = 0;
164 for (Item *it = p_item_from; it && it != p_item_to; it = _get_next_item(it)) {
165 switch (it->type) {
166 case ITEM_TEXT: {
167 ItemText *t = static_cast<ItemText *>(it);
168 offset += t->text.length();
169 if (offset > p_position) {
170 return it;
171 }
172 } break;
173 case ITEM_NEWLINE: {
174 offset += 1;
175 if (offset == p_position) {
176 return it;
177 }
178 } break;
179 case ITEM_IMAGE: {
180 offset += 1;
181 if (offset > p_position) {
182 return it;
183 }
184 } break;
185 case ITEM_TABLE: {
186 ItemTable *table = static_cast<ItemTable *>(it);
187 offset += table->char_count;
188 } break;
189 default:
190 break;
191 }
192 }
193 return p_item_from;
194 }
195
196 String RichTextLabel::_roman(int p_num, bool p_capitalize) const {
197 if (p_num > 3999) {
198 return "ERR";
199 };
200 String s;
201 if (p_capitalize) {
202 const String roman_M[] = { "", "M", "MM", "MMM" };
203 const String roman_C[] = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
204 const String roman_X[] = { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" };
205 const String roman_I[] = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
206 s = roman_M[p_num / 1000] + roman_C[(p_num % 1000) / 100] + roman_X[(p_num % 100) / 10] + roman_I[p_num % 10];
207 } else {
208 const String roman_M[] = { "", "m", "mm", "mmm" };
209 const String roman_C[] = { "", "c", "cc", "ccc", "cd", "d", "dc", "dcc", "dccc", "cm" };
210 const String roman_X[] = { "", "x", "xx", "xxx", "xl", "l", "lx", "lxx", "lxxx", "xc" };
211 const String roman_I[] = { "", "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix" };
212 s = roman_M[p_num / 1000] + roman_C[(p_num % 1000) / 100] + roman_X[(p_num % 100) / 10] + roman_I[p_num % 10];
213 }
214 return s;
215 }
216
217 String RichTextLabel::_letters(int p_num, bool p_capitalize) const {
218 int64_t n = p_num;
219
220 int chars = 0;
221 do {
222 n /= 24;
223 chars++;
224 } while (n);
225
226 String s;
227 s.resize_uninitialized(chars + 1);
228 char32_t *c = s.ptrw();
229 c[chars] = 0;
230 n = p_num;
231 do {
232 int mod = Math::abs(n % 24);
233 char a = (p_capitalize ? 'A' : 'a');
234 c[--chars] = a + mod - 1;
235
236 n /= 24;
237 } while (n);
238
239 return s;
240 }
241
242 String RichTextLabel::_get_prefix(Item *p_item, const Vector<int> &p_list_index, const Vector<ItemList *> &p_list_items) {
243 String prefix;
244 int segments = 0;
245 for (int i = 0; i < p_list_index.size(); i++) {
246 String segment;
247 if (p_list_items[i]->list_type == LIST_DOTS) {
248 if (segments == 0) {
249 prefix = p_list_items[i]->bullet;
250 }
251 break;
252 }
253 prefix = "." + prefix;
254 if (p_list_items[i]->list_type == LIST_NUMBERS) {
255 segment = itos(p_list_index[i]);
256 if (is_localizing_numeral_system()) {
257 segment = TranslationServer::get_singleton()->format_number(segment, _find_language(p_item));
258 }
259 segments++;
260 } else if (p_list_items[i]->list_type == LIST_LETTERS) {
261 segment = _letters(p_list_index[i], p_list_items[i]->capitalize);
262 segments++;
263 } else if (p_list_items[i]->list_type == LIST_ROMAN) {
264 segment = _roman(p_list_index[i], p_list_items[i]->capitalize);
265 segments++;
266 }
267 prefix = segment + prefix;
268 }
269 return prefix + " ";
270 }
271
272 void RichTextLabel::_add_list_prefixes(ItemFrame *p_frame, int p_line, Line &r_l) {
273 Vector<int> list_index;
274 Vector<int> list_count;
275 Vector<ItemList *> list_items;
276 _find_list(r_l.from, list_index, list_count, list_items);
277 if (list_items.size() > 0) {
278 ItemList *this_list = list_items[0];
279 if (list_index[0] == 1) {
280 // List level start, shape all prefixes for this level and compute max. prefix width.
281 list_items[0]->max_width = 0;
282 int index = 0;
283 for (int i = p_line; i < (int)p_frame->lines.size(); i++) { // For all the list rows in all lists in this frame.
284 Line &list_row_line = p_frame->lines[i];
285 if (_find_list_item(list_row_line.from) == this_list) { // Is a row inside this list.
286 index++;
287 Ref<Font> font = theme_cache.normal_font;
288 int font_size = theme_cache.normal_font_size;
289 int list_row_char_ofs = list_row_line.from->char_ofs;
290 int item_font_size = -1;
291 ItemFont *found_font_item = nullptr;
292 Vector<Item *> formatting_items_info;
293 ItemText *this_row_text_item = nullptr;
294 Item *it = _get_next_item(this_list);
295 while (it && (this_row_text_item != nullptr || it->char_ofs <= list_row_char_ofs)) { // Find the ItemText for this list row. There is only one per row or none.
296 if (it->type == ITEM_TEXT && it->char_ofs == list_row_char_ofs) {
297 ItemText *text_item = static_cast<ItemText *>(it);
298 this_row_text_item = text_item;
299 // `parent` is the enclosing item tag, if any, which itself can be further enclosed by another tag and so on,
300 // all of which will be applied to the text item. The `parent` is an interval predecessor, not a hierarchical parent.
301 Item *parent = text_item->parent;
302 while (parent && parent != main) {
303 // `formatting_items` is an Array of all ITEM types affecting glyph appearance, like ITEM_FONT, ITEM_COLOR, etc.
304 if (formatting_items.has(parent->type)) {
305 formatting_items_info.push_back(parent);
306 }
307 parent = parent->parent;
308 }
309 }
310 it = _get_next_item(it);
311 }
312 if (this_row_text_item == nullptr) { // If the row doesn't have any text yet.
313 it = _get_next_item(this_list);
314 // All format items at the same char location should be applied to the prefix.
315 // This won't add any earlier tags.
316 while (it && it->char_ofs <= list_row_char_ofs) {
317 if (formatting_items.has(it->type) && it->char_ofs == list_row_char_ofs) {
318 formatting_items_info.push_back(it);
319 }
320 it = _get_next_item(it);
321 }
322 }
323 for (Item *format_item : formatting_items_info) {
324 switch (format_item->type) {
325 case ITEM_FONT: {
326 ItemFont *font_item = static_cast<ItemFont *>(format_item);
327 if (font_item->def_font != RTL_CUSTOM_FONT) {
328 font_item = _find_font(format_item); // Sets `def_font` based on font type.
329 }
330 if (font_item->font.is_valid()) {
331 if (font_item->def_font == RTL_BOLD_ITALICS_FONT) { // Always set bold italic.
332 found_font_item = font_item;
333 } else if (found_font_item == nullptr || found_font_item->def_font != RTL_BOLD_ITALICS_FONT) { // Don't overwrite BOLD_ITALIC with BOLD or ITALIC.
334 found_font_item = font_item;
335 }
336 }
337 if (found_font_item->font_size > 0) {
338 font_size = found_font_item->font_size;
339 }
340 } break;
341 case ITEM_FONT_SIZE: {
342 ItemFontSize *font_size_item = static_cast<ItemFontSize *>(format_item);
343 item_font_size = font_size_item->font_size;
344 } break;
345 case ITEM_COLOR: {
346 ItemColor *color_item = static_cast<ItemColor *>(format_item);
347 list_row_line.prefix_color = color_item->color;
348 } break;
349 case ITEM_OUTLINE_SIZE: {
350 ItemOutlineSize *outline_size_item = static_cast<ItemOutlineSize *>(format_item);
351 list_row_line.prefix_outline_size = outline_size_item->outline_size;
352 } break;
353 case ITEM_OUTLINE_COLOR: {
354 ItemOutlineColor *outline_color_item = static_cast<ItemOutlineColor *>(format_item);
355 list_row_line.prefix_outline_color = outline_color_item->color;
356 } break;
357 default: {
358 } break;
359 }
360 }
361 font = found_font_item != nullptr ? found_font_item->font : font;
362 font_size = item_font_size != -1 ? item_font_size : font_size;
363 list_index.write[0] = index;
364 String prefix = _get_prefix(list_row_line.from, list_index, list_items);
365 list_row_line.text_prefix.instantiate();
366 list_row_line.text_prefix->set_direction(_find_direction(list_row_line.from));
367 list_row_line.text_prefix->add_string(prefix, font, font_size);
368 list_items.write[0]->max_width = MAX(this_list->max_width, list_row_line.text_prefix->get_size().x);
369 }
370 }
371 }
372 r_l.prefix_width = this_list->max_width;
373 }
374 }
375
376 void RichTextLabel::_update_line_font(ItemFrame *p_frame, int p_line, const Ref<Font> &p_base_font, int p_base_font_size) {
377 ERR_FAIL_NULL(p_frame);
378 ERR_FAIL_COND(p_line < 0 || p_line >= (int)p_frame->lines.size());
379
380 Line &l = p_frame->lines[p_line];
381 MutexLock lock(l.text_buf->get_mutex());
382
383 // List.
384 _add_list_prefixes(p_frame, p_line, l);
385
386 {
387 RID t = l.text_buf->get_rid();
388 int spans = TS->shaped_get_span_count(t);
389 for (int i = 0; i < spans; i++) {
390 Item *it_span = items.get_or_null(TS->shaped_get_span_meta(t, i));
391 ItemText *it = reinterpret_cast<ItemText *>(it_span);
392 if (it) {
393 Ref<Font> font = p_base_font;
394 int font_size = p_base_font_size;
395
396 ItemFont *font_it = _find_font(it);
397 if (font_it) {
398 if (font_it->font.is_valid()) {
399 font = font_it->font;
400 }
401 if (font_it->font_size > 0) {
402 font_size = font_it->font_size;
403 }
404 }
405 ItemFontSize *font_size_it = _find_font_size(it);
406 if (font_size_it && font_size_it->font_size > 0) {
407 font_size = font_size_it->font_size;
408 }
409 TS->shaped_set_span_update_font(t, i, font->get_rids(), font_size, font->get_opentype_features());
410 } else {
411 TS->shaped_set_span_update_font(t, i, p_base_font->get_rids(), p_base_font_size, p_base_font->get_opentype_features());
412 }
413 }
414 }
415 if (l.text_buf_disp.is_valid()) {
416 RID t = l.text_buf_disp->get_rid();
417 int spans = TS->shaped_get_span_count(t);
418 for (int i = 0; i < spans; i++) {
419 Item *it_span = items.get_or_null(TS->shaped_get_span_meta(t, i));
420 ItemText *it = reinterpret_cast<ItemText *>(it_span);
421 if (it) {
422 Ref<Font> font = p_base_font;
423 int font_size = p_base_font_size;
424
425 ItemFont *font_it = _find_font(it);
426 if (font_it) {
427 if (font_it->font.is_valid()) {
428 font = font_it->font;
429 }
430 if (font_it->font_size > 0) {
431 font_size = font_it->font_size;
432 }
433 }
434 ItemFontSize *font_size_it = _find_font_size(it);
435 if (font_size_it && font_size_it->font_size > 0) {
436 font_size = font_size_it->font_size;
437 }
438 TS->shaped_set_span_update_font(t, i, font->get_rids(), font_size, font->get_opentype_features());
439 } else {
440 TS->shaped_set_span_update_font(t, i, p_base_font->get_rids(), p_base_font_size, p_base_font->get_opentype_features());
441 }
442 }
443 }
444
445 Item *it_to = (p_line + 1 < (int)p_frame->lines.size()) ? p_frame->lines[p_line + 1].from : nullptr;
446 for (Item *it = l.from; it && it != it_to; it = _get_next_item(it)) {
447 switch (it->type) {
448 case ITEM_TABLE: {
449 ItemTable *table = static_cast<ItemTable *>(it);
450 for (Item *E : table->subitems) {
451 ERR_CONTINUE(E->type != ITEM_FRAME); // Children should all be frames.
452 ItemFrame *frame = static_cast<ItemFrame *>(E);
453 for (int i = 0; i < (int)frame->lines.size(); i++) {
454 _update_line_font(frame, i, p_base_font, p_base_font_size);
455 }
456 }
457 } break;
458 default:
459 break;
460 }
461 }
462 }
463
464 float RichTextLabel::_resize_line(ItemFrame *p_frame, int p_line, const Ref<Font> &p_base_font, int p_base_font_size, int p_width, float p_h) {
465 ERR_FAIL_NULL_V(p_frame, p_h);
466 ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)p_frame->lines.size(), p_h);
467
468 Line &l = p_frame->lines[p_line];
469
470 MutexLock lock(l.text_buf->get_mutex());
471
472 l.indent = _find_margin(l.from, p_base_font, p_base_font_size) + l.prefix_width;
473 l.offset.x = l.indent;
474 l.text_buf->set_width(p_width - l.offset.x);
475
476 PackedFloat32Array tab_stops = _find_tab_stops(l.from);
477 if (!tab_stops.is_empty()) {
478 l.text_buf->tab_align(tab_stops);
479 } else if (tab_size > 0) { // Align inline tabs.
480 Vector<float> tabs;
481 tabs.push_back(MAX(1, tab_size * (p_base_font->get_char_size(' ', p_base_font_size).width + p_base_font->get_spacing(TextServer::SPACING_SPACE))));
482 l.text_buf->tab_align(tabs);
483 }
484
485 if (l.text_buf_disp.is_valid()) {
486 l.text_buf_disp->set_width(p_width - l.offset.x);
487 if (!tab_stops.is_empty()) {
488 l.text_buf_disp->tab_align(tab_stops);
489 } else if (tab_size > 0) { // Align inline tabs.
490 Vector<float> tabs;
491 tabs.push_back(tab_size * p_base_font->get_char_size(' ', p_base_font_size).width);
492 l.text_buf_disp->tab_align(tabs);
493 }
494 }
495
496 Item *it_to = (p_line + 1 < (int)p_frame->lines.size()) ? p_frame->lines[p_line + 1].from : nullptr;
497 for (Item *it = l.from; it && it != it_to; it = _get_next_item(it)) {
498 switch (it->type) {
499 case ITEM_IMAGE: {
500 ItemImage *img = static_cast<ItemImage *>(it);
501 Size2 img_size = img->size;
502 if (img->width_in_percent || img->height_in_percent) {
503 img_size = _get_image_size(img->image, img->width_in_percent ? (p_width * img->rq_size.width / 100.f) : img->rq_size.width, img->height_in_percent ? (p_width * img->rq_size.height / 100.f) : img->rq_size.height, img->region);
504 l.text_buf->resize_object(it->rid, img_size, img->inline_align);
505 if (l.text_buf_disp.is_valid() && l.text_buf_disp->has_object(it->rid)) {
506 l.text_buf_disp->resize_object(it->rid, img_size, img->inline_align);
507 }
508 }
509 } break;
510 case ITEM_TABLE: {
511 ItemTable *table = static_cast<ItemTable *>(it);
512 int col_count = table->columns.size();
513
514 for (int i = 0; i < col_count; i++) {
515 table->columns[i].width = 0;
516 }
517
518 const int available_width = p_width - theme_cache.table_h_separation * (col_count - 1);
519 int base_column_width = available_width / col_count;
520
521 for (Item *E : table->subitems) {
522 ERR_CONTINUE(E->type != ITEM_FRAME); // Children should all be frames.
523 ItemFrame *frame = static_cast<ItemFrame *>(E);
524
525 float prev_h = 0;
526 for (int i = 0; i < (int)frame->lines.size(); i++) {
527 MutexLock sub_lock(frame->lines[i].text_buf->get_mutex());
528 int w = base_column_width - frame->padding.position.x - frame->padding.size.x;
529 w = MAX(w, _find_margin(frame->lines[i].from, p_base_font, p_base_font_size) + 1);
530 prev_h = _resize_line(frame, i, p_base_font, p_base_font_size, w, prev_h);
531 }
532 }
533
534 _set_table_size(table, available_width);
535
536 int row_idx = (table->align_to_row < 0) ? table->rows_baseline.size() - 1 : table->align_to_row;
537 if (table->rows_baseline.size() != 0 && row_idx < (int)table->rows_baseline.size()) {
538 l.text_buf->resize_object(it->rid, Size2(table->total_width, table->total_height), table->inline_align, Math::round(table->rows_baseline[row_idx]));
539 if (l.text_buf_disp.is_valid() && l.text_buf_disp->has_object(it->rid)) {
540 l.text_buf_disp->resize_object(it->rid, Size2(table->total_width, table->total_height), table->inline_align, Math::round(table->rows_baseline[row_idx]));
541 }
542 } else {
543 l.text_buf->resize_object(it->rid, Size2(table->total_width, table->total_height), table->inline_align);
544 if (l.text_buf_disp.is_valid() && l.text_buf_disp->has_object(it->rid)) {
545 l.text_buf_disp->resize_object(it->rid, Size2(table->total_width, table->total_height), table->inline_align);
546 }
547 }
548 } break;
549 default:
550 break;
551 }
552 }
553
554 l.offset.y = p_h;
555 return _calculate_line_vertical_offset(l);
556 }
557
558 float RichTextLabel::_shape_line(ItemFrame *p_frame, int p_line, const Ref<Font> &p_base_font, int p_base_font_size, int p_width, float p_h, int *r_char_offset) {
559 ERR_FAIL_NULL_V(p_frame, p_h);
560 ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)p_frame->lines.size(), p_h);
561
562 Line &l = p_frame->lines[p_line];
563
564 MutexLock lock(l.text_buf->get_mutex());
565
566 BitField<TextServer::LineBreakFlag> autowrap_flags = TextServer::BREAK_MANDATORY;
567 switch (autowrap_mode) {
568 case TextServer::AUTOWRAP_WORD_SMART:
569 autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_ADAPTIVE | TextServer::BREAK_MANDATORY;
570 break;
571 case TextServer::AUTOWRAP_WORD:
572 autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_MANDATORY;
573 break;
574 case TextServer::AUTOWRAP_ARBITRARY:
575 autowrap_flags = TextServer::BREAK_GRAPHEME_BOUND | TextServer::BREAK_MANDATORY;
576 break;
577 case TextServer::AUTOWRAP_OFF:
578 break;
579 }
580 autowrap_flags = autowrap_flags | autowrap_flags_trim;
581
582 // Clear cache.
583 l.dc_item = nullptr;
584 l.text_buf_disp = Ref<TextParagraph>();
585 l.text_buf->clear();
586 l.text_buf->set_break_flags(autowrap_flags);
587 l.text_buf->set_justification_flags(_find_jst_flags(l.from));
588 l.char_offset = *r_char_offset;
589 l.char_count = 0;
590
591 // List.
592 _add_list_prefixes(p_frame, p_line, l);
593
594 // Add indent.
595 l.indent = _find_margin(l.from, p_base_font, p_base_font_size) + l.prefix_width;
596 l.offset.x = l.indent;
597 l.text_buf->set_width(p_width - l.offset.x);
598 l.text_buf->set_alignment(_find_alignment(l.from));
599 l.text_buf->set_direction(_find_direction(l.from));
600
601 PackedFloat32Array tab_stops = _find_tab_stops(l.from);
602 if (!tab_stops.is_empty()) {
603 l.text_buf->tab_align(tab_stops);
604 } else if (tab_size > 0) { // Align inline tabs.
605 Vector<float> tabs;
606 tabs.push_back(MAX(1, tab_size * (p_base_font->get_char_size(' ', p_base_font_size).width + p_base_font->get_spacing(TextServer::SPACING_SPACE))));
607 l.text_buf->tab_align(tabs);
608 }
609
610 // Shape current paragraph.
611 String txt;
612 String txt_sub;
613 Item *it_to = (p_line + 1 < (int)p_frame->lines.size()) ? p_frame->lines[p_line + 1].from : nullptr;
614 int remaining_characters = visible_characters - l.char_offset;
615 for (Item *it = l.from; it && it != it_to; it = _get_next_item(it)) {
616 switch (it->type) {
617 case ITEM_DROPCAP: {
618 // Add dropcap.
619 ItemDropcap *dc = static_cast<ItemDropcap *>(it);
620 l.text_buf->set_dropcap(dc->text, dc->font, dc->font_size, dc->dropcap_margins);
621 l.dc_item = dc;
622 l.dc_color = dc->color;
623 l.dc_ol_size = dc->ol_size;
624 l.dc_ol_color = dc->ol_color;
625 } break;
626 case ITEM_NEWLINE: {
627 Ref<Font> font = p_base_font;
628 int font_size = p_base_font_size;
629
630 ItemFont *font_it = _find_font(it);
631 if (font_it) {
632 if (font_it->font.is_valid()) {
633 font = font_it->font;
634 }
635 if (font_it->font_size > 0) {
636 font_size = font_it->font_size;
637 }
638 }
639 ItemFontSize *font_size_it = _find_font_size(it);
640 if (font_size_it && font_size_it->font_size > 0) {
641 font_size = font_size_it->font_size;
642 }
643 l.text_buf->add_string(String::chr(0x200B), font, font_size, String(), it->rid);
644 txt += "\n";
645 l.char_count++;
646 remaining_characters--;
647 } break;
648 case ITEM_TEXT: {
649 ItemText *t = static_cast<ItemText *>(it);
650 Ref<Font> font = p_base_font;
651 int font_size = p_base_font_size;
652
653 ItemFont *font_it = _find_font(it);
654 if (font_it) {
655 if (font_it->font.is_valid()) {
656 font = font_it->font;
657 }
658 if (font_it->font_size > 0) {
659 font_size = font_it->font_size;
660 }
661 }
662 ItemFontSize *font_size_it = _find_font_size(it);
663 if (font_size_it && font_size_it->font_size > 0) {
664 font_size = font_size_it->font_size;
665 }
666 String lang = _find_language(it);
667 String tx = t->text;
668 if (l.text_buf_disp.is_null() && visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING && visible_characters >= 0 && remaining_characters >= 0 && tx.length() > remaining_characters) {
669 String sub = tx.substr(0, remaining_characters);
670 l.text_buf_disp = l.text_buf->duplicate();
671 l.text_buf_disp->add_string(sub, font, font_size, lang, it->rid);
672 txt_sub = txt + sub;
673 }
674 l.text_buf->add_string(tx, font, font_size, lang, it->rid);
675 remaining_characters -= tx.length();
676
677 txt += tx;
678 l.char_count += tx.length();
679 } break;
680 case ITEM_IMAGE: {
681 ItemImage *img = static_cast<ItemImage *>(it);
682 Size2 img_size = img->size;
683 if (img->width_in_percent || img->height_in_percent) {
684 img_size = _get_image_size(img->image, img->width_in_percent ? (p_width * img->rq_size.width / 100.f) : img->rq_size.width, img->height_in_percent ? (p_width * img->rq_size.height / 100.f) : img->rq_size.height, img->region);
685 }
686 l.text_buf->add_object(it->rid, img_size, img->inline_align, 1);
687 txt += String::chr(0xfffc);
688 l.char_count++;
689 remaining_characters--;
690 } break;
691 case ITEM_TABLE: {
692 ItemTable *table = static_cast<ItemTable *>(it);
693 int col_count = table->columns.size();
694 int t_char_count = 0;
695 // Set minimums to zero.
696 for (int i = 0; i < col_count; i++) {
697 table->columns[i].min_width = 0;
698 table->columns[i].max_width = 0;
699 table->columns[i].width = 0;
700 }
701 // Compute minimum width for each cell.
702 const int available_width = p_width - theme_cache.table_h_separation * (col_count - 1);
703 int base_column_width = available_width / col_count;
704 int idx = 0;
705 for (Item *E : table->subitems) {
706 ERR_CONTINUE(E->type != ITEM_FRAME); // Children should all be frames.
707 ItemFrame *frame = static_cast<ItemFrame *>(E);
708
709 int column = idx % col_count;
710 float prev_h = 0;
711 for (int i = 0; i < (int)frame->lines.size(); i++) {
712 MutexLock sub_lock(frame->lines[i].text_buf->get_mutex());
713
714 int char_offset = l.char_offset + l.char_count;
715 int w = _find_margin(frame->lines[i].from, p_base_font, p_base_font_size) + 1;
716 prev_h = _shape_line(frame, i, p_base_font, p_base_font_size, w, prev_h, &char_offset);
717 int cell_ch = (char_offset - (l.char_offset + l.char_count));
718 l.char_count += cell_ch;
719 t_char_count += cell_ch;
720 remaining_characters -= cell_ch;
721
722 table->columns[column].min_width = MAX(table->columns[column].min_width, frame->lines[i].indent + std::ceil(frame->lines[i].text_buf->get_size().x));
723 table->columns[column].max_width = MAX(table->columns[column].max_width, frame->lines[i].indent + std::ceil(frame->lines[i].text_buf->get_non_wrapped_size().x));
724 }
725 idx++;
726 }
727 table->char_count = t_char_count;
728 for (Item *E : table->subitems) {
729 ERR_CONTINUE(E->type != ITEM_FRAME); // Children should all be frames.
730 ItemFrame *frame = static_cast<ItemFrame *>(E);
731
732 float prev_h = 0;
733 for (int i = 0; i < (int)frame->lines.size(); i++) {
734 int w = base_column_width - frame->padding.position.x - frame->padding.size.x;
735 w = MAX(w, _find_margin(frame->lines[i].from, p_base_font, p_base_font_size) + 1);
736 prev_h = _resize_line(frame, i, p_base_font, p_base_font_size, w, prev_h);
737 }
738 }
739
740 _set_table_size(table, available_width);
741
742 int row_idx = (table->align_to_row < 0) ? table->rows_baseline.size() - 1 : table->align_to_row;
743 if (table->rows_baseline.size() != 0 && row_idx < (int)table->rows_baseline.size()) {
744 l.text_buf->add_object(it->rid, Size2(table->total_width, table->total_height), table->inline_align, t_char_count, Math::round(table->rows_baseline[row_idx]));
745 } else {
746 l.text_buf->add_object(it->rid, Size2(table->total_width, table->total_height), table->inline_align, t_char_count);
747 }
748 txt += String::chr(0xfffc).repeat(t_char_count);
749 } break;
750 default:
751 break;
752 }
753 }
754
755 // Apply BiDi override.
756 TextServer::StructuredTextParser stt = _find_stt(l.from);
757 l.text_buf->set_bidi_override(structured_text_parser(stt, st_args, txt));
758 if (l.text_buf_disp.is_valid()) {
759 l.text_buf_disp->set_bidi_override(structured_text_parser(stt, st_args, txt_sub));
760 }
761
762 *r_char_offset = l.char_offset + l.char_count;
763
764 l.offset.y = p_h;
765 return _calculate_line_vertical_offset(l);
766 }
767
768 void RichTextLabel::_set_table_size(ItemTable *p_table, int p_available_width) {
769 int col_count = p_table->columns.size();
770
771 // Compute available width and total ratio (for expanders).
772 int total_ratio = 0;
773 int remaining_width = p_available_width;
774 p_table->total_width = theme_cache.table_h_separation;
775
776 for (int i = 0; i < col_count; i++) {
777 remaining_width -= p_table->columns[i].min_width;
778 if (p_table->columns[i].max_width > p_table->columns[i].min_width) {
779 p_table->columns[i].expand = true;
780 }
781 if (p_table->columns[i].expand) {
782 total_ratio += p_table->columns[i].expand_ratio;
783 }
784 }
785
786 // Assign actual widths.
787 for (int i = 0; i < col_count; i++) {
788 p_table->columns[i].width = p_table->columns[i].min_width;
789 if (p_table->columns[i].expand && total_ratio > 0 && remaining_width > 0) {
790 p_table->columns[i].width += p_table->columns[i].expand_ratio * remaining_width / total_ratio;
791 }
792 if (i != col_count - 1) {
793 p_table->total_width += p_table->columns[i].width + theme_cache.table_h_separation;
794 } else {
795 p_table->total_width += p_table->columns[i].width;
796 }
797 p_table->columns[i].width_with_padding = p_table->columns[i].width;
798 }
799
800 // Resize to max_width if needed and distribute the remaining space.
801 bool table_need_fit = true;
802 while (table_need_fit) {
803 table_need_fit = false;
804 // Fit slim.
805 for (int i = 0; i < col_count; i++) {
806 if (!p_table->columns[i].expand || !p_table->columns[i].shrink) {
807 continue;
808 }
809 int dif = p_table->columns[i].width - p_table->columns[i].max_width;
810 if (dif > 0) {
811 table_need_fit = true;
812 p_table->columns[i].width = p_table->columns[i].max_width;
813 p_table->total_width -= dif;
814 total_ratio -= p_table->columns[i].expand_ratio;
815 p_table->columns[i].width_with_padding = p_table->columns[i].width;
816 }
817 }
818 // Grow.
819 remaining_width = p_available_width - p_table->total_width;
820 if (remaining_width > 0 && total_ratio > 0) {
821 for (int i = 0; i < col_count; i++) {
822 if (p_table->columns[i].expand) {
823 int dif = p_table->columns[i].max_width - p_table->columns[i].width;
824 if (dif > 0) {
825 int slice = p_table->columns[i].expand_ratio * remaining_width / total_ratio;
826 int incr = MIN(dif, slice);
827 p_table->columns[i].width += incr;
828 p_table->total_width += incr;
829 p_table->columns[i].width_with_padding = p_table->columns[i].width;
830 }
831 }
832 }
833 }
834 }
835
836 // Update line width and get total height.
837 int idx = 0;
838 p_table->total_height = 0;
839 p_table->rows.clear();
840 p_table->rows_no_padding.clear();
841 p_table->rows_baseline.clear();
842
843 Vector2 offset = Vector2(theme_cache.table_h_separation * 0.5, theme_cache.table_v_separation * 0.5).floor();
844 float row_height = 0.0;
845 float row_top_padding = 0.0;
846 float row_bottom_padding = 0.0;
847 const List<Item *>::Element *prev = p_table->subitems.front();
848
849 for (const List<Item *>::Element *E = prev; E; E = E->next()) {
850 ERR_CONTINUE(E->get()->type != ITEM_FRAME); // Children should all be frames.
851 ItemFrame *frame = static_cast<ItemFrame *>(E->get());
852
853 int column = idx % col_count;
854
855 offset.x += frame->padding.position.x;
856 float yofs = 0.0;
857 float prev_h = 0.0;
858 float row_baseline = 0.0;
859 for (int i = 0; i < (int)frame->lines.size(); i++) {
860 MutexLock sub_lock(frame->lines[i].text_buf->get_mutex());
861
862 frame->lines[i].text_buf->set_width(p_table->columns[column].width);
863 p_table->columns[column].width = MAX(p_table->columns[column].width, std::ceil(frame->lines[i].text_buf->get_size().x));
864 p_table->columns[column].width_with_padding = MAX(p_table->columns[column].width_with_padding, std::ceil(frame->lines[i].text_buf->get_size().x + frame->padding.position.x + frame->padding.size.x));
865
866 frame->lines[i].offset.y = prev_h;
867
868 float h = frame->lines[i].text_buf->get_size().y + (frame->lines[i].text_buf->get_line_count() - 1) * theme_cache.line_separation;
869 if (i > 0) {
870 h += theme_cache.paragraph_separation + theme_cache.line_separation;
871 }
872 if (frame->min_size_over.y > 0) {
873 h = MAX(h, frame->min_size_over.y);
874 }
875 if (frame->max_size_over.y > 0) {
876 h = MIN(h, frame->max_size_over.y);
877 }
878 yofs += h;
879 prev_h = frame->lines[i].offset.y + frame->lines[i].text_buf->get_size().y + frame->lines[i].text_buf->get_line_count() * theme_cache.line_separation + theme_cache.paragraph_separation;
880
881 frame->lines[i].offset += offset;
882 row_baseline = MAX(row_baseline, frame->lines[i].text_buf->get_line_ascent(frame->lines[i].text_buf->get_line_count() - 1));
883 }
884 row_top_padding = MAX(row_top_padding, frame->padding.position.y);
885 row_bottom_padding = MAX(row_bottom_padding, frame->padding.size.y);
886 offset.x += p_table->columns[column].width + theme_cache.table_h_separation + frame->padding.size.x;
887
888 row_height = MAX(yofs, row_height);
889 // Add row height after last column of the row or last cell of the table.
890 if (column == col_count - 1 || E->next() == nullptr) {
891 offset.x = Math::floor(theme_cache.table_h_separation * 0.5);
892 float row_contents_height = row_height;
893 row_height += row_top_padding + row_bottom_padding;
894 row_height += theme_cache.table_v_separation;
895 p_table->total_height += row_height;
896 offset.y += row_height;
897 p_table->rows.push_back(row_height);
898 p_table->rows_no_padding.push_back(row_contents_height);
899 p_table->rows_baseline.push_back(p_table->total_height - row_height + row_baseline + Math::floor(theme_cache.table_v_separation * 0.5));
900 for (const List<Item *>::Element *F = prev; F; F = F->next()) {
901 ItemFrame *in_frame = static_cast<ItemFrame *>(F->get());
902 for (int i = 0; i < (int)in_frame->lines.size(); i++) {
903 in_frame->lines[i].offset.y += row_top_padding;
904 }
905 if (in_frame == frame) {
906 break;
907 }
908 }
909 row_height = 0.0;
910 row_top_padding = 0.0;
911 row_bottom_padding = 0.0;
912 prev = E->next();
913 }
914 idx++;
915 }
916
917 // Recalculate total width.
918 p_table->total_width = 0;
919 for (int i = 0; i < col_count; i++) {
920 p_table->total_width += p_table->columns[i].width_with_padding + theme_cache.table_h_separation;
921 }
922 }
923
924 int RichTextLabel::_draw_line(ItemFrame *p_frame, int p_line, const Vector2 &p_ofs, int p_width, float p_vsep, const Color &p_base_color, int p_outline_size, const Color &p_outline_color, const Color &p_font_shadow_color, int p_shadow_outline_size, const Point2 &p_shadow_ofs, int &r_processed_glyphs) {
925 ERR_FAIL_NULL_V(p_frame, 0);
926 ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)p_frame->lines.size(), 0);
927
928 Vector2 off;
929
930 Line &l = p_frame->lines[p_line];
931 MutexLock lock(l.text_buf->get_mutex());
932
933 Item *it_from = l.from;
934
935 if (it_from == nullptr) {
936 return 0;
937 }
938
939 RID ci = get_canvas_item();
940 bool rtl = (l.text_buf->get_direction() == TextServer::DIRECTION_RTL);
941 bool lrtl = is_layout_rtl();
942
943 bool trim_chars = (visible_characters >= 0) && (visible_chars_behavior == TextServer::VC_CHARS_AFTER_SHAPING || visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING);
944 bool trim_glyphs_ltr = (visible_characters >= 0) && ((visible_chars_behavior == TextServer::VC_GLYPHS_LTR) || ((visible_chars_behavior == TextServer::VC_GLYPHS_AUTO) && !lrtl));
945 bool trim_glyphs_rtl = (visible_characters >= 0) && ((visible_chars_behavior == TextServer::VC_GLYPHS_RTL) || ((visible_chars_behavior == TextServer::VC_GLYPHS_AUTO) && lrtl));
946 int total_glyphs = (trim_glyphs_ltr || trim_glyphs_rtl) ? get_total_glyph_count() : 0;
947 int visible_glyphs = total_glyphs * visible_ratio;
948
949 // Draw dropcap.
950 int dc_lines = l.text_buf->get_dropcap_lines();
951 float h_off = l.text_buf->get_dropcap_size().x;
952 bool skip_dc = (trim_chars && l.char_offset > visible_characters) || (trim_glyphs_ltr && (r_processed_glyphs >= visible_glyphs)) || (trim_glyphs_rtl && (r_processed_glyphs < total_glyphs - visible_glyphs));
953 if (!skip_dc) {
954 if (l.dc_ol_size > 0) {
955 l.text_buf->draw_dropcap_outline(ci, p_ofs + ((rtl) ? Vector2() : Vector2(l.offset.x, 0)), l.dc_ol_size, l.dc_ol_color);
956 }
957 l.text_buf->draw_dropcap(ci, p_ofs + ((rtl) ? Vector2() : Vector2(l.offset.x, 0)), l.dc_color);
958 }
959
960 const Ref<TextParagraph> &text_buf = l.text_buf_disp.is_valid() ? l.text_buf_disp : l.text_buf;
961
962 int line_count = 0;
963 bool has_visible_chars = false;
964 // Bottom margin for text clipping.
965 float v_limit = theme_cache.normal_style->get_margin(SIDE_BOTTOM);
966 Size2 ctrl_size = get_size();
967 // Draw text.
968 for (int line = 0; line < text_buf->get_line_count(); line++) {
969 if (line > 0) {
970 off.y += (theme_cache.line_separation + p_vsep);
971 }
972
973 if (p_ofs.y + off.y >= ctrl_size.height - v_limit) {
974 break;
975 }
976
977 double l_height = text_buf->get_line_ascent(line) + text_buf->get_line_descent(line);
978 if (p_ofs.y + off.y + l_height <= 0) {
979 off.y += l_height;
980 continue;
981 }
982
983 float width = text_buf->get_width();
984 float length = text_buf->get_line_size(line).x;
985
986 // Draw line.
987 if (rtl) {
988 off.x = p_width - l.offset.x - width;
989 if (!lrtl && p_frame == main) { // Skip Scrollbar.
990 off.x -= scroll_w;
991 }
992 } else {
993 off.x = l.offset.x;
994 if (lrtl && p_frame == main) { // Skip Scrollbar.
995 off.x += scroll_w;
996 }
997 }
998
999 // Draw text.
1000 switch (text_buf->get_alignment()) {
1001 case HORIZONTAL_ALIGNMENT_FILL:
1002 case HORIZONTAL_ALIGNMENT_LEFT: {
1003 if (rtl) {
1004 off.x += width - length;
1005 }
1006 } break;
1007 case HORIZONTAL_ALIGNMENT_CENTER: {
1008 off.x += Math::floor((width - length) / 2.0);
1009 } break;
1010 case HORIZONTAL_ALIGNMENT_RIGHT: {
1011 if (!rtl) {
1012 off.x += width - length;
1013 }
1014 } break;
1015 }
1016
1017 bool skip_prefix = (trim_chars && l.char_offset > visible_characters) || (trim_glyphs_ltr && (r_processed_glyphs >= visible_glyphs)) || (trim_glyphs_rtl && (r_processed_glyphs < total_glyphs - visible_glyphs));
1018 if (l.text_prefix.is_valid() && line == 0 && !skip_prefix) {
1019 Color font_color = l.prefix_color == Color(0, 0, 0, 0) ? _find_color(l.from, p_base_color) : l.prefix_color;
1020 int outline_size = l.prefix_outline_size == -1 ? _find_outline_size(l.from, p_outline_size) : l.prefix_outline_size;
1021 Color font_outline_color = l.prefix_outline_color == Color(0, 0, 0, 0) ? _find_outline_color(l.from, p_base_color) : l.prefix_outline_color;
1022 Color font_shadow_color = p_font_shadow_color * Color(1, 1, 1, font_color.a);
1023 if (rtl) {
1024 if (p_shadow_outline_size > 0 && font_shadow_color.a != 0.0) {
1025 l.text_prefix->draw_outline(ci, p_ofs + Vector2(off.x + length, 0) + p_shadow_ofs, p_shadow_outline_size, font_shadow_color);
1026 }
1027 if (outline_size > 0 && font_outline_color.a != 0.0) {
1028 l.text_prefix->draw_outline(ci, p_ofs + Vector2(off.x + length, 0), outline_size, font_outline_color);
1029 }
1030 l.text_prefix->draw(ci, p_ofs + Vector2(off.x + length, 0), font_color);
1031 } else {
1032 if (p_shadow_outline_size > 0 && font_shadow_color.a != 0.0) {
1033 l.text_prefix->draw_outline(ci, p_ofs + Vector2(off.x - l.text_prefix->get_size().x, 0) + p_shadow_ofs, p_shadow_outline_size, font_shadow_color);
1034 }
1035 if (outline_size > 0 && font_outline_color.a != 0.0) {
1036 l.text_prefix->draw_outline(ci, p_ofs + Vector2(off.x - l.text_prefix->get_size().x, 0), outline_size, font_outline_color);
1037 }
1038 l.text_prefix->draw(ci, p_ofs + Vector2(off.x - l.text_prefix->get_size().x, 0), font_color);
1039 }
1040 }
1041
1042 if (line <= dc_lines) {
1043 if (rtl) {
1044 off.x -= h_off;
1045 } else {
1046 off.x += h_off;
1047 }
1048 }
1049
1050 RID rid = text_buf->get_line_rid(line);
1051 double l_ascent = TS->shaped_text_get_ascent(rid);
1052 Size2 l_size = TS->shaped_text_get_size(rid);
1053 double upos = TS->shaped_text_get_underline_position(rid);
1054 double uth = TS->shaped_text_get_underline_thickness(rid);
1055
1056 off.y += l_ascent;
1057
1058 const Glyph *glyphs = TS->shaped_text_get_glyphs(rid);
1059 int gl_size = TS->shaped_text_get_glyph_count(rid);
1060 Vector2i chr_range = TS->shaped_text_get_range(rid);
1061
1062 int sel_start = -1;
1063 int sel_end = -1;
1064
1065 if (selection.active && (selection.from_frame->lines[selection.from_line].char_offset + selection.from_char) <= (l.char_offset + chr_range.y) && (selection.to_frame->lines[selection.to_line].char_offset + selection.to_char) >= (l.char_offset + chr_range.x)) {
1066 sel_start = MAX(chr_range.x, (selection.from_frame->lines[selection.from_line].char_offset + selection.from_char) - l.char_offset);
1067 sel_end = MIN(chr_range.y, (selection.to_frame->lines[selection.to_line].char_offset + selection.to_char) - l.char_offset);
1068 }
1069
1070 int processed_glyphs_step = 0;
1071 for (int step = DRAW_STEP_BACKGROUND; step < DRAW_STEP_MAX; step++) {
1072 if (step == DRAW_STEP_TEXT) {
1073 // Draw inlined objects.
1074 Array objects = TS->shaped_text_get_objects(rid);
1075 for (int i = 0; i < objects.size(); i++) {
1076 Item *it = items.get_or_null(objects[i]);
1077 if (it != nullptr) {
1078 Vector2i obj_range = TS->shaped_text_get_object_range(rid, objects[i]);
1079 if (trim_chars && l.char_offset + obj_range.y > visible_characters) {
1080 continue;
1081 }
1082 if (trim_glyphs_ltr || trim_glyphs_rtl) {
1083 int obj_glyph = r_processed_glyphs + TS->shaped_text_get_object_glyph(rid, objects[i]);
1084 if ((trim_glyphs_ltr && (obj_glyph >= visible_glyphs)) || (trim_glyphs_rtl && (obj_glyph < total_glyphs - visible_glyphs))) {
1085 continue;
1086 }
1087 }
1088 Rect2 rect = TS->shaped_text_get_object_rect(rid, objects[i]);
1089 switch (it->type) {
1090 case ITEM_IMAGE: {
1091 ItemImage *img = static_cast<ItemImage *>(it);
1092 if (img->pad) {
1093 Size2 pad_size = rect.size.min(img->image->get_size());
1094 Vector2 pad_off = (rect.size - pad_size) / 2;
1095 img->image->draw_rect(ci, Rect2(p_ofs + rect.position + off + pad_off, pad_size), false, img->color);
1096 visible_rect = _merge_or_copy_rect(visible_rect, Rect2(p_ofs + rect.position + off + pad_off, pad_size));
1097 } else {
1098 img->image->draw_rect(ci, Rect2(p_ofs + rect.position + off, rect.size), false, img->color);
1099 visible_rect = _merge_or_copy_rect(visible_rect, Rect2(p_ofs + rect.position + off, rect.size));
1100 }
1101 } break;
1102 case ITEM_TABLE: {
1103 ItemTable *table = static_cast<ItemTable *>(it);
1104 Color odd_row_bg = theme_cache.table_odd_row_bg;
1105 Color even_row_bg = theme_cache.table_even_row_bg;
1106 Color border = theme_cache.table_border;
1107 float h_separation = theme_cache.table_h_separation;
1108 float v_separation = theme_cache.table_v_separation;
1109
1110 int col_count = table->columns.size();
1111 int row_count = table->rows.size();
1112
1113 int idx = 0;
1114 for (Item *E : table->subitems) {
1115 ItemFrame *frame = static_cast<ItemFrame *>(E);
1116
1117 int col = idx % col_count;
1118 int row = idx / col_count;
1119
1120 if (frame->lines.size() != 0 && row < row_count) {
1121 Vector2 coff = frame->lines[0].offset;
1122 coff.x -= frame->lines[0].indent;
1123 if (rtl) {
1124 coff.x = rect.size.width - table->columns[col].width - coff.x;
1125 }
1126 if (row % 2 == 0) {
1127 Color c = frame->odd_row_bg != Color(0, 0, 0, 0) ? frame->odd_row_bg : odd_row_bg;
1128 if (c.a > 0.0) {
1129 draw_rect(Rect2(p_ofs + rect.position + off + coff - frame->padding.position - Vector2(h_separation * 0.5, v_separation * 0.5).floor(), Size2(table->columns[col].width + h_separation + frame->padding.position.x + frame->padding.size.x, table->rows_no_padding[row] + frame->padding.position.y + frame->padding.size.y)), c, true);
1130 }
1131 } else {
1132 Color c = frame->even_row_bg != Color(0, 0, 0, 0) ? frame->even_row_bg : even_row_bg;
1133 if (c.a > 0.0) {
1134 draw_rect(Rect2(p_ofs + rect.position + off + coff - frame->padding.position - Vector2(h_separation * 0.5, v_separation * 0.5).floor(), Size2(table->columns[col].width + h_separation + frame->padding.position.x + frame->padding.size.x, table->rows_no_padding[row] + frame->padding.position.y + frame->padding.size.y)), c, true);
1135 }
1136 }
1137 Color bc = frame->border != Color(0, 0, 0, 0) ? frame->border : border;
1138 if (bc.a > 0.0) {
1139 draw_rect(Rect2(p_ofs + rect.position + off + coff - frame->padding.position - Vector2(h_separation * 0.5, v_separation * 0.5).floor(), Size2(table->columns[col].width + h_separation + frame->padding.position.x + frame->padding.size.x, table->rows_no_padding[row] + frame->padding.position.y + frame->padding.size.y)), bc, false);
1140 }
1141 }
1142
1143 for (int j = 0; j < (int)frame->lines.size(); j++) {
1144 _draw_line(frame, j, p_ofs + rect.position + off + Vector2(0, frame->lines[j].offset.y), rect.size.x, 0, p_base_color, p_outline_size, p_outline_color, p_font_shadow_color, p_shadow_outline_size, p_shadow_ofs, r_processed_glyphs);
1145 }
1146 idx++;
1147 }
1148 } break;
1149 default:
1150 break;
1151 }
1152 }
1153 }
1154 }
1155 Vector2 off_step = off;
1156 processed_glyphs_step = r_processed_glyphs;
1157
1158 Vector2 ul_start;
1159 bool ul_started = false;
1160 Color ul_color_prev;
1161 Color ul_color;
1162
1163 Vector2 dot_ul_start;
1164 bool dot_ul_started = false;
1165 Color dot_ul_color_prev;
1166 Color dot_ul_color;
1167
1168 Vector2 st_start;
1169 bool st_started = false;
1170 Color st_color_prev;
1171 Color st_color;
1172
1173 float box_start = 0.0;
1174 Color last_color = Color(0, 0, 0, 0);
1175
1176 Item *it = it_from;
1177 int span = -1;
1178 for (int i = 0; i < gl_size; i++) {
1179 bool selected = selection.active && (sel_start != -1) && (glyphs[i].start >= sel_start) && (glyphs[i].end <= sel_end);
1180 if (glyphs[i].span_index != span) {
1181 span = glyphs[i].span_index;
1182 if (span >= 0) {
1183 if ((glyphs[i].flags & TextServer::GRAPHEME_IS_EMBEDDED_OBJECT) == TextServer::GRAPHEME_IS_EMBEDDED_OBJECT) {
1184 Item *new_it = items.get_or_null(TS->shaped_get_span_embedded_object(rid, span));
1185 if (new_it) {
1186 it = new_it;
1187 }
1188 } else {
1189 Item *new_it = items.get_or_null(TS->shaped_get_span_meta(rid, span));
1190 if (new_it) {
1191 it = new_it;
1192 }
1193 }
1194 }
1195 }
1196
1197 Color font_color = (step == DRAW_STEP_SHADOW_OUTLINE || step == DRAW_STEP_SHADOW || step == DRAW_STEP_OUTLINE || step == DRAW_STEP_TEXT) ? _find_color(it, p_base_color) : Color();
1198 int outline_size = (step == DRAW_STEP_OUTLINE) ? _find_outline_size(it, p_outline_size) : 0;
1199 Color font_outline_color = (step == DRAW_STEP_OUTLINE) ? _find_outline_color(it, p_outline_color) : Color();
1200 Color font_shadow_color = p_font_shadow_color;
1201 bool txt_visible = (font_color.a != 0);
1202 if (step == DRAW_STEP_OUTLINE && (outline_size <= 0 || font_outline_color.a == 0)) {
1203 processed_glyphs_step += glyphs[i].repeat;
1204 off_step.x += glyphs[i].advance * glyphs[i].repeat;
1205 continue;
1206 } else if (step == DRAW_STEP_SHADOW_OUTLINE && (font_shadow_color.a == 0 || p_shadow_outline_size <= 0)) {
1207 processed_glyphs_step += glyphs[i].repeat;
1208 off_step.x += glyphs[i].advance * glyphs[i].repeat;
1209 continue;
1210 } else if (step == DRAW_STEP_SHADOW && (font_shadow_color.a == 0)) {
1211 processed_glyphs_step += glyphs[i].repeat;
1212 off_step.x += glyphs[i].advance * glyphs[i].repeat;
1213 continue;
1214 } else if (step == DRAW_STEP_TEXT) {
1215 Color user_ul_color = Color(0, 0, 0, 0);
1216 bool has_ul = _find_underline(it, &user_ul_color);
1217 if (!has_ul && underline_meta) {
1218 ItemMeta *meta = nullptr;
1219 if (_find_meta(it, nullptr, &meta) && meta) {
1220 switch (meta->underline) {
1221 case META_UNDERLINE_ALWAYS: {
1222 has_ul = true;
1223 } break;
1224 case META_UNDERLINE_NEVER: {
1225 has_ul = false;
1226 } break;
1227 case META_UNDERLINE_ON_HOVER: {
1228 has_ul = (meta == meta_hovering);
1229 } break;
1230 }
1231 }
1232 }
1233 if (has_ul) {
1234 Color new_ul_color;
1235 if (user_ul_color.a == 0.0) {
1236 new_ul_color = font_color;
1237 new_ul_color.a *= float(theme_cache.underline_alpha) / 100.0;
1238 } else {
1239 new_ul_color = user_ul_color;
1240 }
1241 if (ul_started && new_ul_color != ul_color_prev) {
1242 float y_off = upos;
1243 float underline_width = MAX(1.0, uth * theme_cache.base_scale);
1244 draw_line(ul_start + Vector2(0, y_off), p_ofs + Vector2(off_step.x, off_step.y + y_off), ul_color, underline_width);
1245 ul_start = p_ofs + Vector2(off_step.x, off_step.y);
1246 ul_color = new_ul_color;
1247 ul_color_prev = new_ul_color;
1248 } else if (!ul_started) {
1249 ul_started = true;
1250 ul_start = p_ofs + Vector2(off_step.x, off_step.y);
1251 ul_color = new_ul_color;
1252 ul_color_prev = new_ul_color;
1253 }
1254 } else if (ul_started) {
1255 ul_started = false;
1256 float y_off = upos;
1257 float underline_width = MAX(1.0, uth * theme_cache.base_scale);
1258 draw_line(ul_start + Vector2(0, y_off), p_ofs + Vector2(off_step.x, off_step.y + y_off), ul_color, underline_width);
1259 }
1260 if (_find_hint(it, nullptr) && underline_hint) {
1261 if (dot_ul_started && font_color != dot_ul_color_prev) {
1262 float y_off = upos;
1263 float underline_width = MAX(1.0, uth * theme_cache.base_scale);
1264 draw_dashed_line(dot_ul_start + Vector2(0, y_off), p_ofs + Vector2(off_step.x, off_step.y + y_off), dot_ul_color, underline_width, MAX(2.0, underline_width * 2));
1265 dot_ul_start = p_ofs + Vector2(off_step.x, off_step.y);
1266 dot_ul_color_prev = font_color;
1267 dot_ul_color = font_color;
1268 dot_ul_color.a *= float(theme_cache.underline_alpha) / 100.0;
1269 } else if (!dot_ul_started) {
1270 dot_ul_started = true;
1271 dot_ul_start = p_ofs + Vector2(off_step.x, off_step.y);
1272 dot_ul_color_prev = font_color;
1273 dot_ul_color = font_color;
1274 dot_ul_color.a *= float(theme_cache.underline_alpha) / 100.0;
1275 }
1276 } else if (dot_ul_started) {
1277 dot_ul_started = false;
1278 float y_off = upos;
1279 float underline_width = MAX(1.0, uth * theme_cache.base_scale);
1280 draw_dashed_line(dot_ul_start + Vector2(0, y_off), p_ofs + Vector2(off_step.x, off_step.y + y_off), dot_ul_color, underline_width, MAX(2.0, underline_width * 2));
1281 }
1282 Color user_st_color = Color(0, 0, 0, 0);
1283 if (_find_strikethrough(it, &user_st_color)) {
1284 Color new_st_color;
1285 if (user_st_color.a == 0.0) {
1286 new_st_color = font_color;
1287 new_st_color.a *= float(theme_cache.strikethrough_alpha) / 100.0;
1288 } else {
1289 new_st_color = user_st_color;
1290 }
1291 if (st_started && new_st_color != st_color_prev) {
1292 float y_off = -l_ascent + l_size.y / 2;
1293 float underline_width = MAX(1.0, uth * theme_cache.base_scale);
1294 draw_line(st_start + Vector2(0, y_off), p_ofs + Vector2(off_step.x, off_step.y + y_off), st_color, underline_width);
1295 st_start = p_ofs + Vector2(off_step.x, off_step.y);
1296 st_color = new_st_color;
1297 st_color_prev = new_st_color;
1298 } else if (!st_started) {
1299 st_started = true;
1300 st_start = p_ofs + Vector2(off_step.x, off_step.y);
1301 st_color = new_st_color;
1302 st_color_prev = new_st_color;
1303 }
1304 } else if (st_started) {
1305 st_started = false;
1306 float y_off = -l_ascent + l_size.y / 2;
1307 float underline_width = MAX(1.0, uth * theme_cache.base_scale);
1308 draw_line(st_start + Vector2(0, y_off), p_ofs + Vector2(off_step.x, off_step.y + y_off), st_color, underline_width);
1309 }
1310 }
1311 if (step == DRAW_STEP_SHADOW_OUTLINE || step == DRAW_STEP_SHADOW || step == DRAW_STEP_OUTLINE || step == DRAW_STEP_TEXT) {
1312 ItemFade *fade = nullptr;
1313 Item *fade_item = it;
1314 while (fade_item) {
1315 if (fade_item->type == ITEM_FADE) {
1316 fade = static_cast<ItemFade *>(fade_item);
1317 break;
1318 }
1319 fade_item = fade_item->parent;
1320 }
1321
1322 Vector<ItemFX *> fx_stack;
1323 _fetch_item_fx_stack(it, fx_stack);
1324 bool custom_fx_ok = true;
1325
1326 Point2 fx_offset = Vector2(glyphs[i].x_off, glyphs[i].y_off);
1327 RID frid = glyphs[i].font_rid;
1328 uint32_t gl = glyphs[i].index;
1329 uint16_t gl_fl = glyphs[i].flags;
1330 uint8_t gl_cn = glyphs[i].count;
1331 bool cprev_cluster = false;
1332 bool cprev_conn = false;
1333 if (gl_cn == 0) { // Parts of the same grapheme cluster, always connected.
1334 cprev_cluster = true;
1335 }
1336 if (gl_fl & TextServer::GRAPHEME_IS_RTL) { // Check if previous grapheme cluster is connected.
1337 if (i > 0 && (glyphs[i - 1].flags & TextServer::GRAPHEME_IS_CONNECTED)) {
1338 cprev_conn = true;
1339 }
1340 } else {
1341 if (glyphs[i].flags & TextServer::GRAPHEME_IS_CONNECTED) {
1342 cprev_conn = true;
1343 }
1344 }
1345
1346 //Apply fx.
1347 if (fade) {
1348 float faded_visibility = 1.0f;
1349 if (l.char_offset + glyphs[i].start >= fade->char_ofs + fade->starting_index) {
1350 faded_visibility -= (float)((l.char_offset + glyphs[i].start) - (fade->char_ofs + fade->starting_index)) / (float)fade->length;
1351 faded_visibility = faded_visibility < 0.0f ? 0.0f : faded_visibility;
1352 }
1353 font_color.a = faded_visibility;
1354 }
1355
1356 Transform2D char_xform;
1357 char_xform.set_origin(p_ofs + off_step);
1358
1359 for (int j = 0; j < fx_stack.size(); j++) {
1360 ItemFX *item_fx = fx_stack[j];
1361 bool cn = cprev_cluster || (cprev_conn && item_fx->connected);
1362
1363 if (item_fx->type == ITEM_CUSTOMFX && custom_fx_ok) {
1364 ItemCustomFX *item_custom = static_cast<ItemCustomFX *>(item_fx);
1365
1366 Ref<CharFXTransform> charfx = item_custom->char_fx_transform;
1367 Ref<RichTextEffect> custom_effect = item_custom->custom_effect;
1368
1369 if (custom_effect.is_valid()) {
1370 charfx->elapsed_time = item_custom->elapsed_time;
1371 charfx->range = Vector2i(l.char_offset + glyphs[i].start, l.char_offset + glyphs[i].end);
1372 charfx->relative_index = l.char_offset + glyphs[i].start - item_fx->char_ofs;
1373 charfx->visibility = txt_visible;
1374 charfx->outline = (step == DRAW_STEP_SHADOW_OUTLINE) || (step == DRAW_STEP_SHADOW) || (step == DRAW_STEP_OUTLINE);
1375 charfx->font = frid;
1376 charfx->glyph_index = gl;
1377 charfx->glyph_flags = gl_fl;
1378 charfx->glyph_count = gl_cn;
1379 charfx->offset = fx_offset;
1380 charfx->color = font_color;
1381 charfx->transform = char_xform;
1382
1383 bool effect_status = custom_effect->_process_effect_impl(charfx);
1384 custom_fx_ok = effect_status;
1385
1386 char_xform = charfx->transform;
1387 fx_offset = charfx->offset;
1388 font_color = charfx->color;
1389 gl = charfx->glyph_index;
1390 txt_visible &= charfx->visibility;
1391 }
1392 } else if (item_fx->type == ITEM_SHAKE) {
1393 ItemShake *item_shake = static_cast<ItemShake *>(item_fx);
1394
1395 if (!cn) {
1396 uint64_t char_current_rand = item_shake->offset_random(glyphs[i].start);
1397 uint64_t char_previous_rand = item_shake->offset_previous_random(glyphs[i].start);
1398 uint64_t max_rand = 2147483647;
1399 double current_offset = Math::remap(char_current_rand % max_rand, 0, max_rand, 0.0f, 2.f * (float)Math::PI);
1400 double previous_offset = Math::remap(char_previous_rand % max_rand, 0, max_rand, 0.0f, 2.f * (float)Math::PI);
1401 double n_time = (double)(item_shake->elapsed_time / (0.5f / item_shake->rate));
1402 n_time = (n_time > 1.0) ? 1.0 : n_time;
1403 item_shake->prev_off = Point2(Math::lerp(Math::sin(previous_offset), Math::sin(current_offset), n_time), Math::lerp(Math::cos(previous_offset), Math::cos(current_offset), n_time)) * (float)item_shake->strength / 10.0f;
1404 }
1405 fx_offset += item_shake->prev_off;
1406 } else if (item_fx->type == ITEM_WAVE) {
1407 ItemWave *item_wave = static_cast<ItemWave *>(item_fx);
1408
1409 if (!cn) {
1410 double value = Math::sin(item_wave->frequency * item_wave->elapsed_time + ((p_ofs.x + off_step.x) / 50)) * (item_wave->amplitude / 10.0f);
1411 item_wave->prev_off = Point2(0, 1) * value;
1412 }
1413 fx_offset += item_wave->prev_off;
1414 } else if (item_fx->type == ITEM_TORNADO) {
1415 ItemTornado *item_tornado = static_cast<ItemTornado *>(item_fx);
1416
1417 if (!cn) {
1418 double torn_x = Math::sin(item_tornado->frequency * item_tornado->elapsed_time + ((p_ofs.x + off_step.x) / 50)) * (item_tornado->radius);
1419 double torn_y = Math::cos(item_tornado->frequency * item_tornado->elapsed_time + ((p_ofs.x + off_step.x) / 50)) * (item_tornado->radius);
1420 item_tornado->prev_off = Point2(torn_x, torn_y);
1421 }
1422 fx_offset += item_tornado->prev_off;
1423 } else if (item_fx->type == ITEM_RAINBOW) {
1424 ItemRainbow *item_rainbow = static_cast<ItemRainbow *>(item_fx);
1425
1426 font_color = font_color.from_ok_hsv(MAX(item_rainbow->frequency, 0) * Math::abs(item_rainbow->elapsed_time * item_rainbow->speed + ((p_ofs.x + off_step.x) / 50)), item_rainbow->saturation, item_rainbow->value, font_color.a);
1427 } else if (item_fx->type == ITEM_PULSE) {
1428 ItemPulse *item_pulse = static_cast<ItemPulse *>(item_fx);
1429
1430 const float sined_time = (Math::ease(Math::pingpong(item_pulse->elapsed_time, 1.0 / item_pulse->frequency) * item_pulse->frequency, item_pulse->ease));
1431 font_color = font_color.lerp(font_color * item_pulse->color, sined_time);
1432 }
1433 }
1434
1435 if (is_inside_tree() && get_viewport()->is_snap_2d_transforms_to_pixel_enabled()) {
1436 fx_offset = (fx_offset + Point2(0.5, 0.5)).floor();
1437 }
1438
1439 Vector2 char_off = char_xform.get_origin();
1440 Transform2D char_reverse_xform;
1441 if (step == DRAW_STEP_TEXT) {
1442 if (selected && use_selected_font_color) {
1443 font_color = theme_cache.font_selected_color;
1444 }
1445
1446 char_reverse_xform.set_origin(-char_off);
1447 Transform2D char_final_xform = char_xform * char_reverse_xform;
1448 draw_set_transform_matrix(char_final_xform);
1449 } else if (step == DRAW_STEP_SHADOW_OUTLINE || step == DRAW_STEP_SHADOW) {
1450 font_color = font_shadow_color * Color(1, 1, 1, font_color.a);
1451
1452 char_reverse_xform.set_origin(-char_off - p_shadow_ofs);
1453 Transform2D char_final_xform = char_xform * char_reverse_xform;
1454 char_final_xform.columns[2] += p_shadow_ofs;
1455 draw_set_transform_matrix(char_final_xform);
1456 } else if (step == DRAW_STEP_OUTLINE) {
1457 font_color = font_outline_color * Color(1, 1, 1, font_color.a);
1458
1459 char_reverse_xform.set_origin(-char_off);
1460 Transform2D char_final_xform = char_xform * char_reverse_xform;
1461 draw_set_transform_matrix(char_final_xform);
1462 }
1463
1464 // Draw glyphs.
1465 for (int j = 0; j < glyphs[i].repeat; j++) {
1466 bool skip = (trim_chars && l.char_offset + glyphs[i].end > visible_characters) || (trim_glyphs_ltr && (processed_glyphs_step >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs_step < total_glyphs - visible_glyphs));
1467 if (!skip) {
1468 if (txt_visible) {
1469 has_visible_chars = true;
1470 visible_rect = _merge_or_copy_rect(visible_rect, Rect2i(fx_offset + char_off - Vector2i(0, l_ascent), Point2i(glyphs[i].advance, l_size.y)));
1471 if (step == DRAW_STEP_TEXT) {
1472 if (frid != RID()) {
1473 TS->font_draw_glyph(frid, ci, glyphs[i].font_size, fx_offset + char_off, gl, font_color);
1474 } else if (((glyphs[i].flags & TextServer::GRAPHEME_IS_VIRTUAL) != TextServer::GRAPHEME_IS_VIRTUAL) && ((glyphs[i].flags & TextServer::GRAPHEME_IS_EMBEDDED_OBJECT) != TextServer::GRAPHEME_IS_EMBEDDED_OBJECT)) {
1475 TS->draw_hex_code_box(ci, glyphs[i].font_size, fx_offset + char_off, gl, font_color);
1476 }
1477 } else if (step == DRAW_STEP_SHADOW_OUTLINE && frid != RID()) {
1478 TS->font_draw_glyph_outline(frid, ci, glyphs[i].font_size, p_shadow_outline_size, fx_offset + char_off + p_shadow_ofs, gl, font_color);
1479 } else if (step == DRAW_STEP_SHADOW && frid != RID()) {
1480 TS->font_draw_glyph(frid, ci, glyphs[i].font_size, fx_offset + char_off + p_shadow_ofs, gl, font_color);
1481 } else if (step == DRAW_STEP_OUTLINE && frid != RID()) {
1482 TS->font_draw_glyph_outline(frid, ci, glyphs[i].font_size, outline_size, fx_offset + char_off, gl, font_color);
1483 }
1484 }
1485 }
1486 processed_glyphs_step++;
1487 if (step == DRAW_STEP_TEXT && skip) {
1488 // Finish underline/overline/strikethrough is previous glyph is skipped.
1489 if (ul_started) {
1490 ul_started = false;
1491 float y_off = upos;
1492 float underline_width = MAX(1.0, uth * theme_cache.base_scale);
1493 draw_line(ul_start + Vector2(0, y_off), p_ofs + Vector2(off_step.x, off_step.y + y_off), ul_color, underline_width);
1494 }
1495 if (dot_ul_started) {
1496 dot_ul_started = false;
1497 float y_off = upos;
1498 float underline_width = MAX(1.0, uth * theme_cache.base_scale);
1499 draw_dashed_line(dot_ul_start + Vector2(0, y_off), p_ofs + Vector2(off_step.x, off_step.y + y_off), dot_ul_color, underline_width, MAX(2.0, underline_width * 2));
1500 }
1501 if (st_started) {
1502 st_started = false;
1503 float y_off = -l_ascent + l_size.y / 2;
1504 float underline_width = MAX(1.0, uth * theme_cache.base_scale);
1505 draw_line(st_start + Vector2(0, y_off), p_ofs + Vector2(off_step.x, off_step.y + y_off), st_color, underline_width);
1506 }
1507 }
1508 off_step.x += glyphs[i].advance;
1509 }
1510 draw_set_transform_matrix(Transform2D());
1511 }
1512 // Draw boxes.
1513 if (step == DRAW_STEP_BACKGROUND || step == DRAW_STEP_FOREGROUND) {
1514 for (int j = 0; j < glyphs[i].repeat; j++) {
1515 bool skip = (trim_chars && l.char_offset + glyphs[i].end > visible_characters) || (trim_glyphs_ltr && (processed_glyphs_step >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs_step < total_glyphs - visible_glyphs));
1516 if (!skip) {
1517 Color color;
1518 if (step == DRAW_STEP_BACKGROUND) {
1519 color = _find_bgcolor(it);
1520 } else if (step == DRAW_STEP_FOREGROUND) {
1521 color = _find_fgcolor(it);
1522 }
1523 if (color != last_color) {
1524 if (last_color.a > 0.0) {
1525 Vector2 rect_off = p_ofs + Vector2(box_start - theme_cache.text_highlight_h_padding, off_step.y - l_ascent - theme_cache.text_highlight_v_padding);
1526 Vector2 rect_size = Vector2(off_step.x - box_start + 2 * theme_cache.text_highlight_h_padding, l_size.y + 2 * theme_cache.text_highlight_v_padding);
1527 RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(rect_off, rect_size), last_color);
1528 }
1529 if (color.a > 0.0) {
1530 box_start = off_step.x;
1531 }
1532 }
1533 last_color = color;
1534 } else {
1535 // Finish box is previous glyph is skipped.
1536 if (last_color.a > 0.0) {
1537 Vector2 rect_off = p_ofs + Vector2(box_start - theme_cache.text_highlight_h_padding, off_step.y - l_ascent - theme_cache.text_highlight_v_padding);
1538 Vector2 rect_size = Vector2(off_step.x - box_start + 2 * theme_cache.text_highlight_h_padding, l_size.y + 2 * theme_cache.text_highlight_v_padding);
1539 RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(rect_off, rect_size), last_color);
1540 }
1541 last_color = Color(0, 0, 0, 0);
1542 }
1543 processed_glyphs_step++;
1544 off_step.x += glyphs[i].advance;
1545 }
1546 }
1547 }
1548 // Finish lines and boxes.
1549 if (step == DRAW_STEP_BACKGROUND || step == DRAW_STEP_FOREGROUND) {
1550 if (last_color.a > 0.0) {
1551 Vector2 rect_off = p_ofs + Vector2(box_start - theme_cache.text_highlight_h_padding, off_step.y - l_ascent - theme_cache.text_highlight_v_padding);
1552 Vector2 rect_size = Vector2(off_step.x - box_start + 2 * theme_cache.text_highlight_h_padding, l_size.y + 2 * theme_cache.text_highlight_v_padding);
1553 RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(rect_off, rect_size), last_color);
1554 }
1555 }
1556 if (step == DRAW_STEP_BACKGROUND) {
1557 if (sel_start != -1) {
1558 Color selection_bg = theme_cache.selection_color;
1559 Vector<Vector2> sel = TS->shaped_text_get_selection(rid, sel_start, sel_end);
1560 for (int i = 0; i < sel.size(); i++) {
1561 Rect2 rect = Rect2(sel[i].x + p_ofs.x + off.x, p_ofs.y + off.y - l_ascent, sel[i].y - sel[i].x, l_size.y); // Note: use "off" not "off_step", selection is relative to the line start.
1562 RenderingServer::get_singleton()->canvas_item_add_rect(ci, rect, selection_bg);
1563 }
1564 }
1565 }
1566 if (step == DRAW_STEP_TEXT) {
1567 if (ul_started) {
1568 ul_started = false;
1569 float y_off = upos;
1570 float underline_width = MAX(1.0, uth * theme_cache.base_scale);
1571 draw_line(ul_start + Vector2(0, y_off), p_ofs + Vector2(off_step.x, off_step.y + y_off), ul_color, underline_width);
1572 }
1573 if (dot_ul_started) {
1574 dot_ul_started = false;
1575 float y_off = upos;
1576 float underline_width = MAX(1.0, uth * theme_cache.base_scale);
1577 draw_dashed_line(dot_ul_start + Vector2(0, y_off), p_ofs + Vector2(off_step.x, off_step.y + y_off), dot_ul_color, underline_width, MAX(2.0, underline_width * 2));
1578 }
1579 if (st_started) {
1580 st_started = false;
1581 float y_off = -l_ascent + l_size.y / 2;
1582 float underline_width = MAX(1.0, uth * theme_cache.base_scale);
1583 draw_line(st_start + Vector2(0, y_off), p_ofs + Vector2(off_step.x, off_step.y + y_off), st_color, underline_width);
1584 }
1585 }
1586 }
1587
1588 r_processed_glyphs = processed_glyphs_step;
1589 off.y += TS->shaped_text_get_descent(rid);
1590 if (has_visible_chars) {
1591 line_count++;
1592 has_visible_chars = false;
1593 }
1594 }
1595
1596 return line_count;
1597 }
1598
1599 void RichTextLabel::_find_click(ItemFrame *p_frame, const Point2i &p_click, ItemFrame **r_click_frame, int *r_click_line, Item **r_click_item, int *r_click_char, bool *r_outside, bool p_meta) {
1600 if (r_click_item) {
1601 *r_click_item = nullptr;
1602 }
1603 if (r_click_char != nullptr) {
1604 *r_click_char = 0;
1605 }
1606 if (r_outside != nullptr) {
1607 *r_outside = true;
1608 }
1609
1610 Size2 size = get_size();
1611 Rect2 text_rect = _get_text_rect();
1612
1613 int vofs = vscroll->get_value();
1614
1615 // Search for the first line.
1616 int to_line = main->first_invalid_line.load();
1617 int from_line = _find_first_line(0, to_line, vofs);
1618
1619 int total_height = INT32_MAX;
1620 if (to_line && vertical_alignment != VERTICAL_ALIGNMENT_TOP) {
1621 MutexLock lock(main->lines[to_line - 1].text_buf->get_mutex());
1622 if (theme_cache.line_separation < 0) {
1623 // Do not apply to the last line to avoid cutting text.
1624 total_height = main->lines[to_line - 1].offset.y + main->lines[to_line - 1].text_buf->get_size().y + (main->lines[to_line - 1].text_buf->get_line_count() - 1) * theme_cache.line_separation;
1625 } else {
1626 total_height = main->lines[to_line - 1].offset.y + main->lines[to_line - 1].text_buf->get_size().y + main->lines[to_line - 1].text_buf->get_line_count() * theme_cache.line_separation + theme_cache.paragraph_separation;
1627 }
1628 }
1629 float vbegin = 0, vsep = 0;
1630 if (text_rect.size.y > total_height) {
1631 switch (vertical_alignment) {
1632 case VERTICAL_ALIGNMENT_TOP: {
1633 // Nothing.
1634 } break;
1635 case VERTICAL_ALIGNMENT_CENTER: {
1636 vbegin = (text_rect.size.y - total_height) / 2;
1637 } break;
1638 case VERTICAL_ALIGNMENT_BOTTOM: {
1639 vbegin = text_rect.size.y - total_height;
1640 } break;
1641 case VERTICAL_ALIGNMENT_FILL: {
1642 int lines = 0;
1643 for (int l = from_line; l < to_line; l++) {
1644 MutexLock lock(main->lines[l].text_buf->get_mutex());
1645 lines += main->lines[l].text_buf->get_line_count();
1646 }
1647 if (lines > 1) {
1648 vsep = (text_rect.size.y - total_height) / (lines - 1);
1649 }
1650 } break;
1651 }
1652 }
1653
1654 Point2 ofs = text_rect.get_position() + Vector2(0, vbegin + main->lines[from_line].offset.y - vofs);
1655 while (ofs.y < size.height && from_line < to_line) {
1656 MutexLock lock(main->lines[from_line].text_buf->get_mutex());
1657 _find_click_in_line(p_frame, from_line, ofs, text_rect.size.x, vsep, p_click, r_click_frame, r_click_line, r_click_item, r_click_char, false, p_meta);
1658 ofs.y += main->lines[from_line].text_buf->get_size().y + main->lines[from_line].text_buf->get_line_count() * (theme_cache.line_separation + vsep) + (theme_cache.paragraph_separation);
1659 if (((r_click_item != nullptr) && ((*r_click_item) != nullptr)) || ((r_click_frame != nullptr) && ((*r_click_frame) != nullptr))) {
1660 if (r_outside != nullptr) {
1661 *r_outside = false;
1662 }
1663 return;
1664 }
1665 from_line++;
1666 }
1667 }
1668
1669 float RichTextLabel::_find_click_in_line(ItemFrame *p_frame, int p_line, const Vector2 &p_ofs, int p_width, float p_vsep, const Point2i &p_click, ItemFrame **r_click_frame, int *r_click_line, Item **r_click_item, int *r_click_char, bool p_table, bool p_meta) {
1670 Vector2 off;
1671
1672 bool line_clicked = false;
1673 float text_rect_begin = 0.0;
1674 int char_pos = -1;
1675 bool char_clicked = false;
1676 Line &l = p_frame->lines[p_line];
1677 MutexLock lock(l.text_buf->get_mutex());
1678
1679 bool rtl = (l.text_buf->get_direction() == TextServer::DIRECTION_RTL);
1680 bool lrtl = is_layout_rtl();
1681
1682 // Table hit test results.
1683 bool table_hit = false;
1684 Vector2i table_range;
1685 float table_offy = 0.f;
1686 ItemFrame *table_click_frame = nullptr;
1687 int table_click_line = -1;
1688 Item *table_click_item = nullptr;
1689 int table_click_char = -1;
1690
1691 const Ref<TextParagraph> &text_buf = l.text_buf_disp.is_valid() ? l.text_buf_disp : l.text_buf;
1692
1693 for (int line = 0; line < text_buf->get_line_count(); line++) {
1694 RID rid = text_buf->get_line_rid(line);
1695
1696 float width = text_buf->get_width();
1697 float length = TS->shaped_text_get_width(rid);
1698
1699 if (rtl) {
1700 off.x = p_width - l.offset.x - width;
1701 if (!lrtl && p_frame == main) { // Skip Scrollbar.
1702 off.x -= scroll_w;
1703 }
1704 } else {
1705 off.x = l.offset.x;
1706 if (lrtl && p_frame == main) { // Skip Scrollbar.
1707 off.x += scroll_w;
1708 }
1709 }
1710
1711 switch (text_buf->get_alignment()) {
1712 case HORIZONTAL_ALIGNMENT_FILL:
1713 case HORIZONTAL_ALIGNMENT_LEFT: {
1714 if (rtl) {
1715 off.x += width - length;
1716 }
1717 } break;
1718 case HORIZONTAL_ALIGNMENT_CENTER: {
1719 off.x += Math::floor((width - length) / 2.0);
1720 } break;
1721 case HORIZONTAL_ALIGNMENT_RIGHT: {
1722 if (!rtl) {
1723 off.x += width - length;
1724 }
1725 } break;
1726 }
1727 // Adjust for dropcap.
1728 int dc_lines = text_buf->get_dropcap_lines();
1729 float h_off = text_buf->get_dropcap_size().x;
1730 if (line <= dc_lines) {
1731 if (rtl) {
1732 off.x -= h_off;
1733 } else {
1734 off.x += h_off;
1735 }
1736 }
1737 off.y += TS->shaped_text_get_ascent(rid);
1738
1739 Array objects = TS->shaped_text_get_objects(rid);
1740 for (int i = 0; i < objects.size(); i++) {
1741 Item *it = items.get_or_null(objects[i]);
1742 if (it != nullptr) {
1743 Rect2 rect = TS->shaped_text_get_object_rect(rid, objects[i]);
1744 rect.position += p_ofs + off;
1745 if (p_click.y >= rect.position.y && p_click.y <= rect.position.y + rect.size.y) {
1746 switch (it->type) {
1747 case ITEM_TABLE: {
1748 ItemTable *table = static_cast<ItemTable *>(it);
1749
1750 int idx = 0;
1751 int col_count = table->columns.size();
1752 int row_count = table->rows.size();
1753
1754 for (Item *E : table->subitems) {
1755 ItemFrame *frame = static_cast<ItemFrame *>(E);
1756
1757 int col = idx % col_count;
1758 int row = idx / col_count;
1759
1760 if (frame->lines.size() != 0 && row < row_count) {
1761 Vector2 coff = frame->lines[0].offset;
1762 if (rtl) {
1763 coff.x = rect.size.width - table->columns[col].width - coff.x;
1764 }
1765 Rect2 crect = Rect2(rect.position + coff - frame->padding.position - Vector2(theme_cache.table_h_separation * 0.5, theme_cache.table_h_separation * 0.5).floor(), Size2(table->columns[col].width + theme_cache.table_h_separation, table->rows[row] + theme_cache.table_v_separation) + frame->padding.position + frame->padding.size);
1766 if (col == col_count - 1) {
1767 if (rtl) {
1768 crect.size.x = crect.position.x + crect.size.x;
1769 crect.position.x = 0;
1770 } else {
1771 crect.size.x = get_size().x;
1772 }
1773 }
1774 if (crect.has_point(p_click)) {
1775 for (int j = 0; j < (int)frame->lines.size(); j++) {
1776 _find_click_in_line(frame, j, rect.position + Vector2(0.0, frame->lines[j].offset.y), rect.size.x, 0, p_click, &table_click_frame, &table_click_line, &table_click_item, &table_click_char, true, p_meta);
1777 if (table_click_frame && table_click_item) {
1778 // Save cell detected cell hit data.
1779 table_range = Vector2i(INT32_MAX, 0);
1780 for (Item *F : table->subitems) {
1781 ItemFrame *sub_frame = static_cast<ItemFrame *>(F);
1782 for (int k = 0; k < (int)sub_frame->lines.size(); k++) {
1783 table_range.x = MIN(table_range.x, sub_frame->lines[k].char_offset);
1784 table_range.y = MAX(table_range.y, sub_frame->lines[k].char_offset + sub_frame->lines[k].char_count);
1785 }
1786 }
1787 table_offy = off.y;
1788 table_hit = true;
1789 }
1790 }
1791 }
1792 }
1793 idx++;
1794 }
1795 } break;
1796 default:
1797 break;
1798 }
1799 }
1800 }
1801 }
1802 Rect2 rect = Rect2(p_ofs + off - Vector2(0, TS->shaped_text_get_ascent(rid)), TS->shaped_text_get_size(rid) + p_frame->padding.size);
1803 if (p_table) {
1804 rect.size.y += theme_cache.table_v_separation;
1805 }
1806
1807 if (p_click.y >= rect.position.y && p_click.y <= rect.position.y + rect.size.y) {
1808 if (!p_meta) {
1809 char_pos = rtl ? TS->shaped_text_get_range(rid).y : TS->shaped_text_get_range(rid).x;
1810 }
1811 if ((!rtl && p_click.x >= rect.position.x) || (rtl && p_click.x <= rect.position.x + rect.size.x)) {
1812 if (p_meta) {
1813 int64_t glyph_idx = TS->shaped_text_hit_test_grapheme(rid, p_click.x - rect.position.x);
1814 if (glyph_idx >= 0) {
1815 float baseline_y = rect.position.y + TS->shaped_text_get_ascent(rid);
1816 const Glyph *glyphs = TS->shaped_text_get_glyphs(rid);
1817 if (glyphs[glyph_idx].flags & TextServer::GRAPHEME_IS_EMBEDDED_OBJECT) {
1818 // Emebedded object.
1819 Vector2 obj_off = p_ofs + off;
1820 for (int i = 0; i < objects.size(); i++) {
1821 if (TS->shaped_text_get_object_glyph(rid, objects[i]) == glyph_idx) {
1822 Rect2 obj_rect = TS->shaped_text_get_object_rect(rid, objects[i]);
1823 obj_rect.position += obj_off;
1824 Item *it = items.get_or_null(objects[i]);
1825 if (it && it->type == ITEM_IMAGE) {
1826 ItemImage *img = reinterpret_cast<ItemImage *>(it);
1827 if (img && img->pad && img->image.is_valid()) {
1828 Size2 pad_size = rect.size.min(img->image->get_size());
1829 Vector2 pad_off = (rect.size - pad_size) / 2;
1830 obj_rect.position += pad_off;
1831 obj_rect.size = pad_size;
1832 }
1833 }
1834 if (p_click.y >= obj_rect.position.y && p_click.y <= obj_rect.position.y + obj_rect.size.y) {
1835 char_pos = glyphs[glyph_idx].start;
1836 char_clicked = true;
1837 }
1838 break;
1839 }
1840 }
1841 } else if (glyphs[glyph_idx].font_rid != RID()) {
1842 // Normal glyph.
1843 float fa = TS->font_get_ascent(glyphs[glyph_idx].font_rid, glyphs[glyph_idx].font_size);
1844 float fd = TS->font_get_descent(glyphs[glyph_idx].font_rid, glyphs[glyph_idx].font_size);
1845 if (p_click.y >= baseline_y - fa && p_click.y <= baseline_y + fd) {
1846 char_pos = glyphs[glyph_idx].start;
1847 char_clicked = true;
1848 }
1849 } else if (!(glyphs[glyph_idx].flags & TextServer::GRAPHEME_IS_VIRTUAL)) {
1850 // Hex code box.
1851 Vector2 gl_size = TS->get_hex_code_box_size(glyphs[glyph_idx].font_size, glyphs[glyph_idx].index);
1852 if (p_click.y >= baseline_y - gl_size.y * 0.85 && p_click.y <= baseline_y + gl_size.y * 0.15) {
1853 char_pos = glyphs[glyph_idx].start;
1854 char_clicked = true;
1855 }
1856 }
1857 }
1858 } else {
1859 int click_char_pos = TS->shaped_text_hit_test_position(rid, p_click.x - rect.position.x);
1860 if (click_char_pos != -1) {
1861 char_pos = TS->shaped_text_closest_character_pos(rid, click_char_pos);
1862 char_clicked = true;
1863 }
1864 }
1865 }
1866 line_clicked = true;
1867 text_rect_begin = rtl ? rect.position.x + rect.size.x : rect.position.x;
1868 }
1869
1870 // If table hit was detected, and line hit is in the table bounds use table hit.
1871 if (table_hit && (((char_pos + p_frame->lines[p_line].char_offset) >= table_range.x && (char_pos + p_frame->lines[p_line].char_offset) <= table_range.y) || !char_clicked)) {
1872 if (r_click_frame != nullptr) {
1873 *r_click_frame = table_click_frame;
1874 }
1875
1876 if (r_click_line != nullptr) {
1877 *r_click_line = table_click_line;
1878 }
1879
1880 if (r_click_item != nullptr) {
1881 *r_click_item = table_click_item;
1882 }
1883
1884 if (r_click_char != nullptr) {
1885 *r_click_char = table_click_char;
1886 }
1887 return table_offy;
1888 }
1889
1890 if (line == text_buf->get_line_count() - 1) {
1891 off.y += TS->shaped_text_get_descent(rid) + theme_cache.paragraph_separation;
1892 }
1893
1894 off.y += TS->shaped_text_get_descent(rid) + theme_cache.line_separation + p_vsep;
1895 }
1896
1897 // Text line hit.
1898 if (line_clicked) {
1899 // Find item.
1900 if (r_click_item != nullptr) {
1901 Item *it = p_frame->lines[p_line].from;
1902 Item *it_to = (p_line + 1 < (int)p_frame->lines.size()) ? p_frame->lines[p_line + 1].from : nullptr;
1903 if (char_pos >= 0) {
1904 *r_click_item = _get_item_at_pos(it, it_to, char_pos);
1905 } else {
1906 int stop = text_rect_begin;
1907 *r_click_item = _find_indentable(it);
1908 while (*r_click_item) {
1909 Ref<Font> font = theme_cache.normal_font;
1910 int font_size = theme_cache.normal_font_size;
1911 ItemFont *font_it = _find_font(*r_click_item);
1912 if (font_it) {
1913 if (font_it->font.is_valid()) {
1914 font = font_it->font;
1915 }
1916 if (font_it->font_size > 0) {
1917 font_size = font_it->font_size;
1918 }
1919 }
1920 ItemFontSize *font_size_it = _find_font_size(*r_click_item);
1921 if (font_size_it && font_size_it->font_size > 0) {
1922 font_size = font_size_it->font_size;
1923 }
1924 if (rtl) {
1925 stop += MAX(1, tab_size * (font->get_char_size(' ', font_size).width + font->get_spacing(TextServer::SPACING_SPACE)));
1926 if (stop > p_click.x) {
1927 break;
1928 }
1929 } else {
1930 stop -= MAX(1, tab_size * (font->get_char_size(' ', font_size).width + font->get_spacing(TextServer::SPACING_SPACE)));
1931 if (stop < p_click.x) {
1932 break;
1933 }
1934 }
1935 *r_click_item = _find_indentable((*r_click_item)->parent);
1936 }
1937 }
1938 }
1939
1940 if (r_click_frame != nullptr) {
1941 *r_click_frame = p_frame;
1942 }
1943
1944 if (r_click_line != nullptr) {
1945 *r_click_line = p_line;
1946 }
1947
1948 if (r_click_char != nullptr) {
1949 *r_click_char = char_pos;
1950 }
1951 }
1952
1953 return off.y;
1954 }
1955
1956 void RichTextLabel::_scroll_changed(double) {
1957 if (updating_scroll) {
1958 return;
1959 }
1960
1961 if (scroll_follow && vscroll->get_value() > (vscroll->get_max() - vscroll->get_page() - 1)) {
1962 scroll_following = true;
1963 } else {
1964 scroll_following = false;
1965 }
1966
1967 scroll_updated = true;
1968
1969 queue_redraw();
1970 }
1971
1972 void RichTextLabel::_update_fx(RichTextLabel::ItemFrame *p_frame, double p_delta_time) {
1973 Item *it = p_frame;
1974 while (it) {
1975 ItemFX *ifx = nullptr;
1976
1977 if (it->type == ITEM_CUSTOMFX || it->type == ITEM_SHAKE || it->type == ITEM_WAVE || it->type == ITEM_TORNADO || it->type == ITEM_RAINBOW || it->type == ITEM_PULSE) {
1978 ifx = static_cast<ItemFX *>(it);
1979 }
1980
1981 if (!ifx) {
1982 it = _get_next_item(it, true);
1983 continue;
1984 }
1985
1986 ifx->elapsed_time += p_delta_time;
1987
1988 ItemShake *shake = nullptr;
1989
1990 if (it->type == ITEM_SHAKE) {
1991 shake = static_cast<ItemShake *>(it);
1992 }
1993
1994 if (shake) {
1995 bool cycle = (shake->elapsed_time > (1.0f / shake->rate));
1996 if (cycle) {
1997 shake->elapsed_time -= (1.0f / shake->rate);
1998 shake->reroll_random();
1999 }
2000 }
2001
2002 it = _get_next_item(it, true);
2003 }
2004 }
2005
2006 int RichTextLabel::_find_first_line(int p_from, int p_to, int p_vofs) const {
2007 int l = p_from;
2008 int r = p_to;
2009 while (l < r) {
2010 int m = Math::floor(double(l + r) / 2.0);
2011 MutexLock lock(main->lines[m].text_buf->get_mutex());
2012 int ofs = _calculate_line_vertical_offset(main->lines[m]);
2013 if (ofs < p_vofs) {
2014 l = m + 1;
2015 } else {
2016 r = m;
2017 }
2018 }
2019 return MIN(l, (int)main->lines.size() - 1);
2020 }
2021
2022 _FORCE_INLINE_ float RichTextLabel::_calculate_line_vertical_offset(const RichTextLabel::Line &line) const {
2023 return line.get_height(theme_cache.line_separation, theme_cache.paragraph_separation);
2024 }
2025
2026 void RichTextLabel::_update_theme_item_cache() {
2027 Control::_update_theme_item_cache();
2028
2029 theme_cache.base_scale = get_theme_default_base_scale();
2030 use_selected_font_color = theme_cache.font_selected_color != Color(0, 0, 0, 0);
2031 }
2032
2033 PackedStringArray RichTextLabel::get_accessibility_configuration_warnings() const {
2034 PackedStringArray warnings = Control::get_accessibility_configuration_warnings();
2035
2036 Item *it = main;
2037 while (it) {
2038 if (it->type == ITEM_IMAGE) {
2039 ItemImage *img = static_cast<ItemImage *>(it);
2040 if (img && img->alt_text.strip_edges().is_empty()) {
2041 warnings.push_back(RTR("Image alternative text must not be empty."));
2042 }
2043 }
2044 it = _get_next_item(it, true);
2045 }
2046
2047 return warnings;
2048 }
2049
2050 void RichTextLabel::_accessibility_update_line(RID p_id, ItemFrame *p_frame, int p_line, const Vector2 &p_ofs, int p_width, float p_vsep) {
2051 ERR_FAIL_NULL(p_frame);
2052 ERR_FAIL_COND(p_line < 0 || p_line >= (int)p_frame->lines.size());
2053
2054 Line &l = p_frame->lines[p_line];
2055
2056 if (l.accessibility_line_element.is_valid()) {
2057 return;
2058 }
2059 l.accessibility_line_element = DisplayServer::get_singleton()->accessibility_create_sub_element(p_id, DisplayServer::AccessibilityRole::ROLE_CONTAINER);
2060
2061 MutexLock lock(l.text_buf->get_mutex());
2062
2063 const RID &line_ae = l.accessibility_line_element;
2064
2065 Rect2 ae_rect = Rect2(p_ofs, Size2(p_width, l.text_buf->get_size().y + l.text_buf->get_line_count() * theme_cache.line_separation));
2066 DisplayServer::get_singleton()->accessibility_update_set_bounds(line_ae, ae_rect);
2067 ac_element_bounds_cache[line_ae] = ae_rect;
2068
2069 Item *it_from = l.from;
2070 if (it_from == nullptr) {
2071 return;
2072 }
2073
2074 bool rtl = (l.text_buf->get_direction() == TextServer::DIRECTION_RTL);
2075 bool lrtl = is_layout_rtl();
2076
2077 // Process dropcap.
2078 int dc_lines = l.text_buf->get_dropcap_lines();
2079 float h_off = l.text_buf->get_dropcap_size().x;
2080
2081 // Process text.
2082 const Ref<TextParagraph> &text_buf = l.text_buf_disp.is_valid() ? l.text_buf_disp : l.text_buf;
2083 const RID &para_rid = text_buf->get_rid();
2084
2085 String l_text = TS->shaped_get_text(para_rid).remove_char(0xfffc).strip_edges();
2086 if (l.dc_item) {
2087 ItemDropcap *dc = static_cast<ItemDropcap *>(l.dc_item);
2088 l_text = dc->text + l_text;
2089 }
2090 if (!l_text.is_empty()) {
2091 Vector2 off;
2092 if (rtl) {
2093 off.x = p_width - l.offset.x - text_buf->get_width();
2094 if (!lrtl && p_frame == main) { // Skip Scrollbar.
2095 off.x -= scroll_w;
2096 }
2097 } else {
2098 off.x = l.offset.x;
2099 if (lrtl && p_frame == main) { // Skip Scrollbar.
2100 off.x += scroll_w;
2101 }
2102 }
2103
2104 l.accessibility_text_element = DisplayServer::get_singleton()->accessibility_create_sub_element(line_ae, DisplayServer::AccessibilityRole::ROLE_STATIC_TEXT);
2105 DisplayServer::get_singleton()->accessibility_update_set_value(l.accessibility_text_element, l_text);
2106 ae_rect = Rect2(p_ofs + off, text_buf->get_size());
2107 DisplayServer::get_singleton()->accessibility_update_set_bounds(l.accessibility_text_element, ae_rect);
2108 ac_element_bounds_cache[l.accessibility_text_element] = ae_rect;
2109
2110 DisplayServer::get_singleton()->accessibility_update_add_action(l.accessibility_text_element, DisplayServer::AccessibilityAction::ACTION_FOCUS, callable_mp(this, &RichTextLabel::_accessibility_focus_item).bind((uint64_t)l.from, true, true));
2111 DisplayServer::get_singleton()->accessibility_update_add_action(l.accessibility_text_element, DisplayServer::AccessibilityAction::ACTION_BLUR, callable_mp(this, &RichTextLabel::_accessibility_focus_item).bind((uint64_t)l.from, true, false));
2112 DisplayServer::get_singleton()->accessibility_update_add_action(l.accessibility_text_element, DisplayServer::AccessibilityAction::ACTION_SCROLL_INTO_VIEW, callable_mp(this, &RichTextLabel::_accessibility_scroll_to_item).bind((uint64_t)l.from));
2113 }
2114
2115 Vector2 off;
2116 for (int line = 0; line < text_buf->get_line_count(); line++) {
2117 if (line > 0) {
2118 off.y += (theme_cache.line_separation + p_vsep);
2119 }
2120
2121 const Size2 line_size = text_buf->get_line_size(line);
2122
2123 float width = text_buf->get_width();
2124 float length = line_size.x;
2125
2126 // Process line.
2127
2128 if (rtl) {
2129 off.x = p_width - l.offset.x - width;
2130 if (!lrtl && p_frame == main) { // Skip Scrollbar.
2131 off.x -= scroll_w;
2132 }
2133 } else {
2134 off.x = l.offset.x;
2135 if (lrtl && p_frame == main) { // Skip Scrollbar.
2136 off.x += scroll_w;
2137 }
2138 }
2139
2140 // Process text.
2141 switch (text_buf->get_alignment()) {
2142 case HORIZONTAL_ALIGNMENT_FILL:
2143 case HORIZONTAL_ALIGNMENT_LEFT: {
2144 if (rtl) {
2145 off.x += width - length;
2146 }
2147 } break;
2148 case HORIZONTAL_ALIGNMENT_CENTER: {
2149 off.x += Math::floor((width - length) / 2.0);
2150 } break;
2151 case HORIZONTAL_ALIGNMENT_RIGHT: {
2152 if (!rtl) {
2153 off.x += width - length;
2154 }
2155 } break;
2156 }
2157
2158 if (line <= dc_lines) {
2159 if (rtl) {
2160 off.x -= h_off;
2161 } else {
2162 off.x += h_off;
2163 }
2164 }
2165
2166 const RID &rid = text_buf->get_line_rid(line);
2167
2168 Array objects = TS->shaped_text_get_objects(rid);
2169 for (int i = 0; i < objects.size(); i++) {
2170 Item *it = reinterpret_cast<Item *>((uint64_t)objects[i]);
2171 if (it != nullptr) {
2172 Rect2 rect = TS->shaped_text_get_object_rect(rid, objects[i]);
2173 switch (it->type) {
2174 case ITEM_IMAGE: {
2175 ItemImage *img = static_cast<ItemImage *>(it);
2176 RID img_ae = DisplayServer::get_singleton()->accessibility_create_sub_element(line_ae, DisplayServer::AccessibilityRole::ROLE_IMAGE);
2177
2178 DisplayServer::get_singleton()->accessibility_update_set_name(img_ae, img->alt_text);
2179 if (img->pad) {
2180 Size2 pad_size = rect.size.min(img->image->get_size());
2181 Vector2 pad_off = (rect.size - pad_size) / 2;
2182 ae_rect = Rect2(p_ofs + rect.position + off + pad_off, pad_size);
2183 } else {
2184 ae_rect = Rect2(p_ofs + rect.position + off, rect.size);
2185 }
2186 DisplayServer::get_singleton()->accessibility_update_set_bounds(img_ae, ae_rect);
2187 ac_element_bounds_cache[img_ae] = ae_rect;
2188
2189 DisplayServer::get_singleton()->accessibility_update_add_action(img_ae, DisplayServer::AccessibilityAction::ACTION_FOCUS, callable_mp(this, &RichTextLabel::_accessibility_focus_item).bind((uint64_t)it, false, true));
2190 DisplayServer::get_singleton()->accessibility_update_add_action(img_ae, DisplayServer::AccessibilityAction::ACTION_BLUR, callable_mp(this, &RichTextLabel::_accessibility_focus_item).bind((uint64_t)it, false, false));
2191 DisplayServer::get_singleton()->accessibility_update_add_action(img_ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_INTO_VIEW, callable_mp(this, &RichTextLabel::_accessibility_scroll_to_item).bind((uint64_t)it));
2192
2193 it->accessibility_item_element = img_ae;
2194 } break;
2195 case ITEM_TABLE: {
2196 ItemTable *table = static_cast<ItemTable *>(it);
2197 float h_separation = theme_cache.table_h_separation;
2198 float v_separation = theme_cache.table_v_separation;
2199
2200 RID table_ae = DisplayServer::get_singleton()->accessibility_create_sub_element(line_ae, DisplayServer::AccessibilityRole::ROLE_TABLE);
2201
2202 int col_count = table->columns.size();
2203 int row_count = table->rows.size();
2204
2205 DisplayServer::get_singleton()->accessibility_update_set_name(table_ae, table->name);
2206 DisplayServer::get_singleton()->accessibility_update_set_role(table_ae, DisplayServer::AccessibilityRole::ROLE_TABLE);
2207 DisplayServer::get_singleton()->accessibility_update_set_table_column_count(table_ae, col_count);
2208 DisplayServer::get_singleton()->accessibility_update_set_table_row_count(table_ae, row_count);
2209 ae_rect = Rect2(p_ofs + rect.position + off + Vector2(0, TS->shaped_text_get_ascent(rid)), rect.size);
2210 DisplayServer::get_singleton()->accessibility_update_set_bounds(table_ae, ae_rect);
2211 ac_element_bounds_cache[table_ae] = ae_rect;
2212
2213 DisplayServer::get_singleton()->accessibility_update_add_action(table_ae, DisplayServer::AccessibilityAction::ACTION_FOCUS, callable_mp(this, &RichTextLabel::_accessibility_focus_item).bind((uint64_t)it, false, true));
2214 DisplayServer::get_singleton()->accessibility_update_add_action(table_ae, DisplayServer::AccessibilityAction::ACTION_BLUR, callable_mp(this, &RichTextLabel::_accessibility_focus_item).bind((uint64_t)it, false, false));
2215 DisplayServer::get_singleton()->accessibility_update_add_action(table_ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_INTO_VIEW, callable_mp(this, &RichTextLabel::_accessibility_scroll_to_item).bind((uint64_t)it));
2216
2217 Vector<RID> row_aes;
2218 Vector2 row_off = Vector2(0, TS->shaped_text_get_ascent(rid));
2219 for (int j = 0; j < row_count; j++) {
2220 RID row_ae = DisplayServer::get_singleton()->accessibility_create_sub_element(table_ae, DisplayServer::AccessibilityRole::ROLE_ROW);
2221
2222 DisplayServer::get_singleton()->accessibility_update_set_table_row_index(row_ae, j);
2223 ae_rect = Rect2(p_ofs + rect.position + off + row_off, Size2(rect.size.x, table->rows[j]));
2224 DisplayServer::get_singleton()->accessibility_update_set_bounds(row_ae, ae_rect);
2225 ac_element_bounds_cache[row_ae] = ae_rect;
2226 row_off.y += table->rows[j];
2227
2228 row_aes.push_back(row_ae);
2229 }
2230
2231 int idx = 0;
2232 for (Item *E : table->subitems) {
2233 ItemFrame *frame = static_cast<ItemFrame *>(E);
2234
2235 int col = idx % col_count;
2236 int row = idx / col_count;
2237
2238 for (int j = 0; j < (int)frame->lines.size(); j++) {
2239 RID cell_ae = DisplayServer::get_singleton()->accessibility_create_sub_element(row_aes[row], DisplayServer::AccessibilityRole::ROLE_CELL);
2240
2241 if (frame->lines.size() != 0 && row < row_count) {
2242 Vector2 coff = frame->lines[0].offset;
2243 coff.x -= frame->lines[0].indent;
2244 if (rtl) {
2245 coff.x = rect.size.width - table->columns[col].width - coff.x;
2246 }
2247 ae_rect = Rect2(p_ofs + rect.position + off + coff - frame->padding.position - Vector2(h_separation * 0.5, v_separation * 0.5).floor(), Size2(table->columns[col].width + h_separation + frame->padding.position.x + frame->padding.size.x, table->rows[row]));
2248 DisplayServer::get_singleton()->accessibility_update_set_bounds(cell_ae, ae_rect);
2249 ac_element_bounds_cache[cell_ae] = ae_rect;
2250 }
2251 DisplayServer::get_singleton()->accessibility_update_set_table_cell_position(cell_ae, row, col);
2252
2253 _accessibility_update_line(cell_ae, frame, j, p_ofs + rect.position + off + Vector2(0, frame->lines[j].offset.y), rect.size.x, p_vsep);
2254 }
2255 idx++;
2256 }
2257
2258 it->accessibility_item_element = table_ae;
2259 } break;
2260 default:
2261 break;
2262 }
2263 }
2264 }
2265
2266 off.y += TS->shaped_text_get_descent(rid) + TS->shaped_text_get_ascent(rid);
2267 }
2268 }
2269
2270 void RichTextLabel::_accessibility_action_menu(const Variant &p_data) {
2271 if (context_menu_enabled) {
2272 _update_context_menu();
2273 menu->set_position(get_screen_position());
2274 menu->reset_size();
2275 menu->popup();
2276 menu->grab_focus();
2277 }
2278 }
2279
2280 void RichTextLabel::_accessibility_scroll_down(const Variant &p_data) {
2281 if ((uint8_t)p_data == 0) {
2282 vscroll->set_value(vscroll->get_value() + vscroll->get_page() / 4);
2283 } else {
2284 vscroll->set_value(vscroll->get_value() + vscroll->get_page());
2285 }
2286 }
2287
2288 void RichTextLabel::_accessibility_scroll_up(const Variant &p_data) {
2289 if ((uint8_t)p_data == 0) {
2290 vscroll->set_value(vscroll->get_value() - vscroll->get_page() / 4);
2291 } else {
2292 vscroll->set_value(vscroll->get_value() - vscroll->get_page());
2293 }
2294 }
2295
2296 void RichTextLabel::_accessibility_scroll_set(const Variant &p_data) {
2297 const Point2 &pos = p_data;
2298 vscroll->set_value(pos.y);
2299 }
2300
2301 void RichTextLabel::_accessibility_focus_item(const Variant &p_data, uint64_t p_item, bool p_line, bool p_foucs) {
2302 Item *it = reinterpret_cast<Item *>(p_item);
2303 if (p_foucs) {
2304 ItemFrame *f = nullptr;
2305 _find_frame(it, &f, nullptr);
2306
2307 if (f && it) {
2308 keyboard_focus_frame = f;
2309 keyboard_focus_line = it->line;
2310 keyboard_focus_item = it;
2311 keyboard_focus_on_text = p_line;
2312 }
2313 } else {
2314 keyboard_focus_frame = nullptr;
2315 keyboard_focus_line = 0;
2316 keyboard_focus_item = nullptr;
2317 keyboard_focus_on_text = true;
2318 }
2319 }
2320
2321 void RichTextLabel::_accessibility_scroll_to_item(const Variant &p_data, uint64_t p_item) {
2322 Item *it = reinterpret_cast<Item *>(p_item);
2323 ItemFrame *f = nullptr;
2324 _find_frame(it, &f, nullptr);
2325
2326 if (f && it) {
2327 vscroll->set_value(f->lines[it->line].offset.y);
2328 }
2329 }
2330
2331 void RichTextLabel::_invalidate_accessibility() {
2332 if (accessibility_scroll_element.is_null()) {
2333 return;
2334 }
2335
2336 Item *it = main;
2337 while (it) {
2338 if (it->type == ITEM_FRAME) {
2339 ItemFrame *fr = static_cast<ItemFrame *>(it);
2340 for (size_t i = 0; i < fr->lines.size(); i++) {
2341 if (fr->lines[i].accessibility_line_element.is_valid()) {
2342 DisplayServer::get_singleton()->accessibility_free_element(fr->lines[i].accessibility_line_element);
2343 }
2344 fr->lines[i].accessibility_line_element = RID();
2345 fr->lines[i].accessibility_text_element = RID();
2346 }
2347 }
2348 it->accessibility_item_element = RID();
2349 it = _get_next_item(it, true);
2350 }
2351 }
2352
2353 RID RichTextLabel::get_focused_accessibility_element() const {
2354 if (keyboard_focus_frame && keyboard_focus_item) {
2355 if (keyboard_focus_on_text) {
2356 return keyboard_focus_frame->lines[keyboard_focus_line].accessibility_text_element;
2357 } else {
2358 if (keyboard_focus_item->accessibility_item_element.is_valid()) {
2359 return keyboard_focus_item->accessibility_item_element;
2360 }
2361 }
2362 } else {
2363 if (!main->lines.is_empty()) {
2364 return main->lines[0].accessibility_text_element;
2365 }
2366 }
2367 return get_accessibility_element();
2368 }
2369
2370 void RichTextLabel::_prepare_scroll_anchor() {
2371 scroll_w = vscroll->get_combined_minimum_size().width;
2372 vscroll->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -scroll_w);
2373 }
2374
2375 void RichTextLabel::_notification(int p_what) {
2376 switch (p_what) {
2377 case NOTIFICATION_ACCESSIBILITY_INVALIDATE: {
2378 accessibility_scroll_element = RID();
2379 Item *it = main;
2380 while (it) {
2381 if (it->type == ITEM_FRAME) {
2382 ItemFrame *fr = static_cast<ItemFrame *>(it);
2383 for (size_t i = 0; i < fr->lines.size(); i++) {
2384 fr->lines[i].accessibility_line_element = RID();
2385 fr->lines[i].accessibility_text_element = RID();
2386 }
2387 }
2388 it->accessibility_item_element = RID();
2389 it = _get_next_item(it, true);
2390 }
2391 } break;
2392
2393 case NOTIFICATION_ACCESSIBILITY_UPDATE: {
2394 RID ae = get_accessibility_element();
2395 ERR_FAIL_COND(ae.is_null());
2396
2397 DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_CONTAINER);
2398
2399 DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SHOW_CONTEXT_MENU, callable_mp(this, &RichTextLabel::_accessibility_action_menu));
2400 DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_DOWN, callable_mp(this, &RichTextLabel::_accessibility_scroll_down));
2401 DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_UP, callable_mp(this, &RichTextLabel::_accessibility_scroll_up));
2402 DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_SCROLL_OFFSET, callable_mp(this, &RichTextLabel::_accessibility_scroll_set));
2403
2404 if (_validate_line_caches()) {
2405 DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_BUSY, false);
2406 } else {
2407 DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_BUSY, true);
2408 return; // Do not update internal elements if threaded procesisng is not done.
2409 }
2410
2411 if (accessibility_scroll_element.is_null()) {
2412 accessibility_scroll_element = DisplayServer::get_singleton()->accessibility_create_sub_element(ae, DisplayServer::AccessibilityRole::ROLE_CONTAINER);
2413 }
2414 Rect2 text_rect = _get_text_rect();
2415
2416 Transform2D scroll_xform;
2417 scroll_xform.set_origin(Vector2i(0, -vscroll->get_value()));
2418 DisplayServer::get_singleton()->accessibility_update_set_transform(accessibility_scroll_element, scroll_xform);
2419 DisplayServer::get_singleton()->accessibility_update_set_bounds(accessibility_scroll_element, text_rect);
2420
2421 MutexLock data_lock(data_mutex);
2422
2423 int to_line = main->first_invalid_line.load();
2424 int from_line = 0;
2425
2426 int total_height = INT32_MAX;
2427 if (to_line && vertical_alignment != VERTICAL_ALIGNMENT_TOP) {
2428 MutexLock lock(main->lines[to_line - 1].text_buf->get_mutex());
2429 if (theme_cache.line_separation < 0) {
2430 // Do not apply to the last line to avoid cutting text.
2431 total_height = main->lines[to_line - 1].offset.y + main->lines[to_line - 1].text_buf->get_size().y + (main->lines[to_line - 1].text_buf->get_line_count() - 1) * theme_cache.line_separation;
2432 } else {
2433 total_height = main->lines[to_line - 1].offset.y + main->lines[to_line - 1].text_buf->get_size().y + main->lines[to_line - 1].text_buf->get_line_count() * theme_cache.line_separation + theme_cache.paragraph_separation;
2434 }
2435 }
2436 float vbegin = 0, vsep = 0;
2437 if (text_rect.size.y > total_height) {
2438 switch (vertical_alignment) {
2439 case VERTICAL_ALIGNMENT_TOP: {
2440 // Nothing.
2441 } break;
2442 case VERTICAL_ALIGNMENT_CENTER: {
2443 vbegin = (text_rect.size.y - total_height) / 2;
2444 } break;
2445 case VERTICAL_ALIGNMENT_BOTTOM: {
2446 vbegin = text_rect.size.y - total_height;
2447 } break;
2448 case VERTICAL_ALIGNMENT_FILL: {
2449 int lines = 0;
2450 for (int l = from_line; l < to_line; l++) {
2451 MutexLock lock(main->lines[l].text_buf->get_mutex());
2452 lines += main->lines[l].text_buf->get_line_count();
2453 }
2454 if (lines > 1) {
2455 vsep = (text_rect.size.y - total_height) / (lines - 1);
2456 }
2457 } break;
2458 }
2459 }
2460
2461 ac_element_bounds_cache.clear();
2462 Point2 ofs = text_rect.get_position() + Vector2(0, vbegin + main->lines[from_line].offset.y);
2463 while (from_line < to_line) {
2464 MutexLock lock(main->lines[from_line].text_buf->get_mutex());
2465
2466 _accessibility_update_line(accessibility_scroll_element, main, from_line, ofs, text_rect.size.x, vsep);
2467 ofs.y += main->lines[from_line].text_buf->get_size().y + main->lines[from_line].text_buf->get_line_count() * (theme_cache.line_separation + vsep);
2468 from_line++;
2469 }
2470 } break;
2471
2472 case NOTIFICATION_MOUSE_EXIT: {
2473 if (meta_hovering) {
2474 meta_hovering = nullptr;
2475 emit_signal(SNAME("meta_hover_ended"), current_meta);
2476 current_meta = false;
2477 queue_redraw();
2478 }
2479 } break;
2480
2481 case NOTIFICATION_RESIZED: {
2482 _stop_thread();
2483 main->first_resized_line.store(0); // Invalidate all lines.
2484 _invalidate_accessibility();
2485 queue_accessibility_update();
2486 queue_redraw();
2487 } break;
2488
2489 case NOTIFICATION_THEME_CHANGED: {
2490 _stop_thread();
2491 main->first_invalid_font_line.store(0); // Invalidate all lines.
2492 for (const RID &E : hr_list) {
2493 Item *it = items.get_or_null(E);
2494 if (it) {
2495 ItemImage *img = static_cast<ItemImage *>(it);
2496 if (img) {
2497 if (img->image.is_valid()) {
2498 img->image->disconnect_changed(callable_mp(this, &RichTextLabel::_texture_changed));
2499 }
2500 img->image = theme_cache.horizontal_rule;
2501 if (img->image.is_valid()) {
2502 img->image->connect_changed(callable_mp(this, &RichTextLabel::_texture_changed).bind(img->rid), CONNECT_REFERENCE_COUNTED);
2503 }
2504 }
2505 }
2506 }
2507 _invalidate_accessibility();
2508 queue_accessibility_update();
2509 queue_redraw();
2510 } break;
2511
2512 case NOTIFICATION_ENTER_TREE: {
2513 _stop_thread();
2514 if (!text.is_empty()) {
2515 set_text(text);
2516 }
2517
2518 main->first_invalid_line.store(0); // Invalidate all lines.
2519 _invalidate_accessibility();
2520 queue_accessibility_update();
2521 queue_redraw();
2522 } break;
2523
2524 case NOTIFICATION_PREDELETE:
2525 case NOTIFICATION_EXIT_TREE: {
2526 _stop_thread();
2527
2528 accessibility_scroll_element = RID();
2529 Item *it = main;
2530 while (it) {
2531 if (it->type == ITEM_FRAME) {
2532 ItemFrame *fr = static_cast<ItemFrame *>(it);
2533 for (size_t i = 0; i < fr->lines.size(); i++) {
2534 fr->lines[i].accessibility_line_element = RID();
2535 fr->lines[i].accessibility_text_element = RID();
2536 }
2537 }
2538 it = _get_next_item(it, true);
2539 }
2540 } break;
2541
2542 case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
2543 case NOTIFICATION_TRANSLATION_CHANGED: {
2544 if (!stack_externally_modified) {
2545 _apply_translation();
2546 }
2547
2548 queue_redraw();
2549 } break;
2550
2551 case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
2552 if (is_visible_in_tree()) {
2553 queue_redraw();
2554 }
2555 } break;
2556
2557 case NOTIFICATION_DRAW: {
2558 RID ci = get_canvas_item();
2559 Size2 size = get_size();
2560
2561 draw_style_box(theme_cache.normal_style, Rect2(Point2(), size));
2562
2563 if (has_focus(true)) {
2564 RenderingServer::get_singleton()->canvas_item_add_clip_ignore(ci, true);
2565 draw_style_box(theme_cache.focus_style, Rect2(Point2(), size));
2566 RenderingServer::get_singleton()->canvas_item_add_clip_ignore(ci, false);
2567 }
2568
2569 // Start text shaping.
2570 if (_validate_line_caches()) {
2571 set_physics_process_internal(false); // Disable auto refresh, if text is fully processed.
2572 } else {
2573 // Draw loading progress bar.
2574 if ((progress_delay > 0) && (OS::get_singleton()->get_ticks_msec() - loading_started >= (uint64_t)progress_delay)) {
2575 Vector2 p_size = Vector2(size.width - (theme_cache.normal_style->get_offset().x + vscroll->get_combined_minimum_size().width) * 2, vscroll->get_combined_minimum_size().width);
2576 Vector2 p_pos = Vector2(theme_cache.normal_style->get_offset().x, size.height - theme_cache.normal_style->get_offset().y - vscroll->get_combined_minimum_size().width);
2577
2578 draw_style_box(theme_cache.progress_bg_style, Rect2(p_pos, p_size));
2579
2580 bool right_to_left = is_layout_rtl();
2581 double r = loaded.load();
2582 int mp = theme_cache.progress_fg_style->get_minimum_size().width;
2583 int p = std::round(r * (p_size.width - mp));
2584 if (right_to_left) {
2585 int p_remaining = std::round((1.0 - r) * (p_size.width - mp));
2586 draw_style_box(theme_cache.progress_fg_style, Rect2(p_pos + Point2(p_remaining, 0), Size2(p + theme_cache.progress_fg_style->get_minimum_size().width, p_size.height)));
2587 } else {
2588 draw_style_box(theme_cache.progress_fg_style, Rect2(p_pos, Size2(p + theme_cache.progress_fg_style->get_minimum_size().width, p_size.height)));
2589 }
2590 }
2591 }
2592
2593 // Draw main text.
2594 Rect2 text_rect = _get_text_rect();
2595 float vofs = vscroll->get_value();
2596
2597 // Search for the first line.
2598 int to_line = main->first_invalid_line.load();
2599 int from_line = _find_first_line(0, to_line, vofs);
2600
2601 // Bottom margin for text clipping.
2602 float v_limit = theme_cache.normal_style->get_margin(SIDE_BOTTOM);
2603
2604 int total_height = INT32_MAX;
2605 if (to_line && vertical_alignment != VERTICAL_ALIGNMENT_TOP) {
2606 MutexLock lock(main->lines[to_line - 1].text_buf->get_mutex());
2607 if (theme_cache.line_separation < 0) {
2608 // Do not apply to the last line to avoid cutting text.
2609 total_height = main->lines[to_line - 1].offset.y + main->lines[to_line - 1].text_buf->get_size().y + (main->lines[to_line - 1].text_buf->get_line_count() - 1) * theme_cache.line_separation;
2610 } else {
2611 total_height = main->lines[to_line - 1].offset.y + main->lines[to_line - 1].text_buf->get_size().y + main->lines[to_line - 1].text_buf->get_line_count() * theme_cache.line_separation + theme_cache.paragraph_separation;
2612 }
2613 }
2614 float vbegin = 0, vsep = 0;
2615 if (text_rect.size.y > total_height) {
2616 switch (vertical_alignment) {
2617 case VERTICAL_ALIGNMENT_TOP: {
2618 // Nothing.
2619 } break;
2620 case VERTICAL_ALIGNMENT_CENTER: {
2621 vbegin = (text_rect.size.y - total_height) / 2;
2622 } break;
2623 case VERTICAL_ALIGNMENT_BOTTOM: {
2624 vbegin = text_rect.size.y - total_height;
2625 } break;
2626 case VERTICAL_ALIGNMENT_FILL: {
2627 int lines = 0;
2628 for (int l = from_line; l < to_line; l++) {
2629 MutexLock lock(main->lines[l].text_buf->get_mutex());
2630 lines += main->lines[l].text_buf->get_line_count();
2631 }
2632 if (lines > 1) {
2633 vsep = (text_rect.size.y - total_height) / (lines - 1);
2634 }
2635 } break;
2636 }
2637 }
2638
2639 Point2 shadow_ofs(theme_cache.shadow_offset_x, theme_cache.shadow_offset_y);
2640
2641 visible_paragraph_count = 0;
2642 visible_line_count = 0;
2643 visible_rect = Rect2i();
2644
2645 // New cache draw.
2646 Point2 ofs = text_rect.get_position() + Vector2(0, vbegin + main->lines[from_line].offset.y - vofs);
2647 int processed_glyphs = 0;
2648 while (ofs.y < size.height - v_limit && from_line < to_line) {
2649 MutexLock lock(main->lines[from_line].text_buf->get_mutex());
2650
2651 int drawn_lines = _draw_line(main, from_line, ofs, text_rect.size.x, vsep, theme_cache.default_color, theme_cache.outline_size, theme_cache.font_outline_color, theme_cache.font_shadow_color, theme_cache.shadow_outline_size, shadow_ofs, processed_glyphs);
2652 visible_line_count += drawn_lines;
2653 if (drawn_lines > 0) {
2654 visible_paragraph_count++;
2655 }
2656 ofs.y += main->lines[from_line].text_buf->get_size().y + main->lines[from_line].text_buf->get_line_count() * (theme_cache.line_separation + vsep) + (theme_cache.paragraph_separation);
2657 from_line++;
2658 }
2659 if (scroll_follow_visible_characters && scroll_active) {
2660 scroll_visible = follow_vc_pos > 0;
2661 if (scroll_visible) {
2662 _prepare_scroll_anchor();
2663 } else {
2664 scroll_w = 0;
2665 }
2666 vscroll->set_visible(scroll_visible);
2667 }
2668 if (has_focus() && get_tree()->is_accessibility_enabled()) {
2669 RID ae;
2670 if (keyboard_focus_frame && keyboard_focus_item) {
2671 if (keyboard_focus_on_text) {
2672 ae = keyboard_focus_frame->lines[keyboard_focus_line].accessibility_text_element;
2673 } else {
2674 if (keyboard_focus_item->accessibility_item_element.is_valid()) {
2675 ae = keyboard_focus_item->accessibility_item_element;
2676 }
2677 }
2678 } else {
2679 if (!main->lines.is_empty()) {
2680 ae = main->lines[0].accessibility_text_element;
2681 }
2682 }
2683 if (ac_element_bounds_cache.has(ae)) {
2684 draw_style_box(theme_cache.focus_style, ac_element_bounds_cache[ae]);
2685 }
2686 }
2687 } break;
2688
2689 case NOTIFICATION_INTERNAL_PROCESS: {
2690 if (is_visible_in_tree()) {
2691 if (!is_finished()) {
2692 return;
2693 }
2694 double dt = get_process_delta_time();
2695 _update_fx(main, dt);
2696 queue_redraw();
2697 }
2698 } break;
2699
2700 case NOTIFICATION_FOCUS_EXIT: {
2701 if (deselect_on_focus_loss_enabled) {
2702 deselect();
2703 }
2704 } break;
2705
2706 case NOTIFICATION_DRAG_END: {
2707 selection.drag_attempt = false;
2708 } break;
2709 }
2710 }
2711
2712 Control::CursorShape RichTextLabel::get_cursor_shape(const Point2 &p_pos) const {
2713 if (selection.click_item) {
2714 return CURSOR_IBEAM;
2715 }
2716
2717 Item *item = nullptr;
2718 bool outside = true;
2719 const_cast<RichTextLabel *>(this)->_find_click(main, p_pos, nullptr, nullptr, &item, nullptr, &outside, true);
2720
2721 if (item && !outside && const_cast<RichTextLabel *>(this)->_find_meta(item, nullptr)) {
2722 return CURSOR_POINTING_HAND;
2723 }
2724 return get_default_cursor_shape();
2725 }
2726
2727 void RichTextLabel::gui_input(const Ref<InputEvent> &p_event) {
2728 ERR_FAIL_COND(p_event.is_null());
2729
2730 Ref<InputEventMouseButton> b = p_event;
2731
2732 if (b.is_valid()) {
2733 if (b->get_button_index() == MouseButton::LEFT) {
2734 if (b->is_pressed() && !b->is_double_click()) {
2735 scroll_updated = false;
2736 ItemFrame *c_frame = nullptr;
2737 int c_line = 0;
2738 Item *c_item = nullptr;
2739 int c_index = 0;
2740 bool outside;
2741
2742 selection.double_click = false;
2743 selection.drag_attempt = false;
2744
2745 _find_click(main, b->get_position(), &c_frame, &c_line, &c_item, &c_index, &outside, false);
2746 if (c_item != nullptr) {
2747 if (selection.enabled) {
2748 selection.click_frame = c_frame;
2749 selection.click_item = c_item;
2750 selection.click_line = c_line;
2751 selection.click_char = c_index;
2752
2753 // Erase previous selection.
2754 if (selection.active) {
2755 if (drag_and_drop_selection_enabled && _is_click_inside_selection()) {
2756 selection.drag_attempt = true;
2757 selection.click_item = nullptr;
2758 } else {
2759 selection.from_frame = nullptr;
2760 selection.from_line = 0;
2761 selection.from_item = nullptr;
2762 selection.from_char = 0;
2763 selection.to_frame = nullptr;
2764 selection.to_line = 0;
2765 selection.to_item = nullptr;
2766 selection.to_char = 0;
2767 deselect();
2768 }
2769 }
2770
2771 if (!selection.drag_attempt) {
2772 is_selecting_text = true;
2773 click_select_held->start();
2774 }
2775 }
2776 }
2777 } else if (b->is_pressed() && b->is_double_click() && selection.enabled) {
2778 //double_click: select word
2779
2780 ItemFrame *c_frame = nullptr;
2781 int c_line = 0;
2782 Item *c_item = nullptr;
2783 int c_index = 0;
2784 bool outside;
2785
2786 selection.drag_attempt = false;
2787
2788 _find_click(main, b->get_position(), &c_frame, &c_line, &c_item, &c_index, &outside, false);
2789
2790 if (c_frame) {
2791 const Line &l = c_frame->lines[c_line];
2792 MutexLock lock(l.text_buf->get_mutex());
2793 PackedInt32Array words = TS->shaped_text_get_word_breaks(l.text_buf->get_rid());
2794 for (int i = 0; i < words.size(); i = i + 2) {
2795 if (c_index >= words[i] && c_index < words[i + 1]) {
2796 selection.from_frame = c_frame;
2797 selection.from_line = c_line;
2798 selection.from_item = c_item;
2799 selection.from_char = words[i];
2800
2801 selection.to_frame = c_frame;
2802 selection.to_line = c_line;
2803 selection.to_item = c_item;
2804 selection.to_char = words[i + 1];
2805
2806 selection.active = true;
2807 if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
2808 DisplayServer::get_singleton()->clipboard_set_primary(get_selected_text());
2809 }
2810 queue_accessibility_update();
2811 queue_redraw();
2812 break;
2813 }
2814 }
2815
2816 selection.click_frame = c_frame;
2817 selection.click_item = c_item;
2818 selection.click_line = c_line;
2819 selection.click_char = c_index;
2820
2821 selection.double_click = true;
2822 is_selecting_text = true;
2823 click_select_held->start();
2824 }
2825 } else if (!b->is_pressed()) {
2826 if (selection.enabled && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
2827 DisplayServer::get_singleton()->clipboard_set_primary(get_selected_text());
2828 }
2829 selection.click_item = nullptr;
2830 if (selection.drag_attempt) {
2831 selection.drag_attempt = false;
2832 if (_is_click_inside_selection()) {
2833 selection.from_frame = nullptr;
2834 selection.from_line = 0;
2835 selection.from_item = nullptr;
2836 selection.from_char = 0;
2837 selection.to_frame = nullptr;
2838 selection.to_line = 0;
2839 selection.to_item = nullptr;
2840 selection.to_char = 0;
2841 deselect();
2842 }
2843 }
2844 if (!b->is_double_click() && !scroll_updated && !selection.active) {
2845 Item *c_item = nullptr;
2846
2847 bool outside = true;
2848 _find_click(main, b->get_position(), nullptr, nullptr, &c_item, nullptr, &outside, true);
2849
2850 if (c_item) {
2851 Variant meta;
2852 if (!outside && _find_meta(c_item, &meta)) {
2853 //meta clicked
2854 emit_signal(SNAME("meta_clicked"), meta);
2855 }
2856 }
2857 }
2858
2859 is_selecting_text = false;
2860 click_select_held->stop();
2861 }
2862 }
2863
2864 bool scroll_value_modified = false;
2865 double prev_scroll = vscroll->get_value();
2866
2867 if (b->get_button_index() == MouseButton::WHEEL_UP) {
2868 if (scroll_active) {
2869 vscroll->scroll(-vscroll->get_page() * b->get_factor() * 0.5 / 8);
2870 scroll_value_modified = true;
2871 }
2872 }
2873 if (b->get_button_index() == MouseButton::WHEEL_DOWN) {
2874 if (scroll_active) {
2875 vscroll->scroll(vscroll->get_page() * b->get_factor() * 0.5 / 8);
2876 scroll_value_modified = true;
2877 }
2878 }
2879
2880 if (scroll_value_modified && vscroll->get_value() != prev_scroll) {
2881 accept_event();
2882 return;
2883 }
2884
2885 if (b->get_button_index() == MouseButton::RIGHT && context_menu_enabled) {
2886 _update_context_menu();
2887 menu->set_position(get_screen_transform().xform(b->get_position()));
2888 menu->reset_size();
2889 menu->popup();
2890 menu->grab_focus();
2891 }
2892 }
2893
2894 Ref<InputEventPanGesture> pan_gesture = p_event;
2895 if (pan_gesture.is_valid()) {
2896 if (scroll_active) {
2897 vscroll->scroll(vscroll->get_page() * pan_gesture->get_delta().y * 0.5 / 8);
2898 queue_accessibility_update();
2899 }
2900
2901 return;
2902 }
2903
2904 Ref<InputEventKey> k = p_event;
2905
2906 if (k.is_valid()) {
2907 if (k->is_pressed()) {
2908 bool handled = false;
2909
2910 if (k->is_action("ui_page_up", true) && vscroll->is_visible_in_tree()) {
2911 vscroll->scroll(-vscroll->get_page());
2912 queue_accessibility_update();
2913 handled = true;
2914 }
2915 if (k->is_action("ui_page_down", true) && vscroll->is_visible_in_tree()) {
2916 vscroll->scroll(vscroll->get_page());
2917 queue_accessibility_update();
2918 handled = true;
2919 }
2920 if (k->is_action("ui_up", true) && vscroll->is_visible_in_tree()) {
2921 vscroll->scroll(-theme_cache.normal_font->get_height(theme_cache.normal_font_size));
2922 queue_accessibility_update();
2923 handled = true;
2924 }
2925 if (k->is_action("ui_down", true) && vscroll->is_visible_in_tree()) {
2926 vscroll->scroll(theme_cache.normal_font->get_height(theme_cache.normal_font_size));
2927 queue_accessibility_update();
2928 handled = true;
2929 }
2930 if (k->is_action("ui_home", true) && vscroll->is_visible_in_tree()) {
2931 vscroll->scroll_to(0);
2932 queue_accessibility_update();
2933 handled = true;
2934 }
2935 if (k->is_action("ui_end", true) && vscroll->is_visible_in_tree()) {
2936 vscroll->scroll_to(vscroll->get_max());
2937 queue_accessibility_update();
2938 handled = true;
2939 }
2940 if (get_tree()->is_accessibility_enabled()) {
2941 if (k->is_action("ui_left", true)) {
2942 if (keyboard_focus_frame != nullptr) {
2943 if (!keyboard_focus_on_text && keyboard_focus_line < (int)keyboard_focus_frame->lines.size() && keyboard_focus_frame->lines[keyboard_focus_line].from == keyboard_focus_item) {
2944 keyboard_focus_on_text = true;
2945 } else {
2946 Item *it = keyboard_focus_item;
2947 while (it) {
2948 it = _get_prev_item(it, true);
2949 if (it) {
2950 ItemFrame *f = nullptr;
2951 _find_frame(it, &f, nullptr);
2952 if (it->type == ITEM_IMAGE || it->type == ITEM_TABLE) {
2953 keyboard_focus_frame = f;
2954 keyboard_focus_line = it->line;
2955 keyboard_focus_item = it;
2956 keyboard_focus_on_text = false;
2957 break;
2958 }
2959 if (f && !f->lines.is_empty()) {
2960 if (f->lines[it->line].from == it) {
2961 keyboard_focus_frame = f;
2962 keyboard_focus_line = it->line;
2963 keyboard_focus_item = it;
2964 keyboard_focus_on_text = true;
2965 break;
2966 }
2967 }
2968 }
2969 }
2970 }
2971 }
2972 queue_accessibility_update();
2973 queue_redraw();
2974 handled = true;
2975 }
2976 if (k->is_action("ui_right", true)) {
2977 if (keyboard_focus_frame == nullptr) {
2978 keyboard_focus_frame = main;
2979 keyboard_focus_line = 0;
2980 keyboard_focus_item = main->lines.is_empty() ? nullptr : main->lines[0].from;
2981 keyboard_focus_on_text = true;
2982 } else {
2983 if (keyboard_focus_on_text && keyboard_focus_item && (keyboard_focus_item->type == ITEM_IMAGE || keyboard_focus_item->type == ITEM_TABLE)) {
2984 keyboard_focus_on_text = false;
2985 } else {
2986 Item *it = keyboard_focus_item;
2987 while (it) {
2988 it = _get_next_item(it, true);
2989 if (it) {
2990 ItemFrame *f = nullptr;
2991 _find_frame(it, &f, nullptr);
2992 if (f && !f->lines.is_empty()) {
2993 if (f->lines[it->line].from == it) {
2994 keyboard_focus_frame = f;
2995 keyboard_focus_line = it->line;
2996 keyboard_focus_item = it;
2997 keyboard_focus_on_text = true;
2998 break;
2999 }
3000 }
3001 if (it->type == ITEM_IMAGE || it->type == ITEM_TABLE) {
3002 keyboard_focus_frame = f;
3003 keyboard_focus_line = it->line;
3004 keyboard_focus_item = it;
3005 keyboard_focus_on_text = false;
3006 break;
3007 }
3008 }
3009 }
3010 }
3011 }
3012 queue_accessibility_update();
3013 queue_redraw();
3014 handled = true;
3015 }
3016 }
3017 if (is_shortcut_keys_enabled()) {
3018 if (k->is_action("ui_text_select_all", true)) {
3019 select_all();
3020 handled = true;
3021 }
3022 if (k->is_action("ui_copy", true)) {
3023 const String txt = get_selected_text();
3024 if (!txt.is_empty()) {
3025 DisplayServer::get_singleton()->clipboard_set(txt);
3026 }
3027 handled = true;
3028 }
3029 }
3030 if (k->is_action("ui_menu", true)) {
3031 if (context_menu_enabled) {
3032 _update_context_menu();
3033 menu->set_position(get_screen_position());
3034 menu->reset_size();
3035 menu->popup();
3036 menu->grab_focus();
3037 }
3038 handled = true;
3039 }
3040
3041 if (handled) {
3042 accept_event();
3043 }
3044 }
3045 }
3046
3047 Ref<InputEventMouseMotion> m = p_event;
3048 if (m.is_valid()) {
3049 local_mouse_pos = get_local_mouse_position();
3050 last_clamped_mouse_pos = local_mouse_pos.clamp(Vector2(), get_size());
3051
3052 Item *c_item = nullptr;
3053 bool outside = false;
3054
3055 // Update meta hovering.
3056 _find_click(main, local_mouse_pos, nullptr, nullptr, &c_item, nullptr, &outside, true);
3057 Variant meta;
3058 ItemMeta *item_meta;
3059 ItemMeta *prev_meta = meta_hovering;
3060 if (c_item && !outside && _find_meta(c_item, &meta, &item_meta)) {
3061 if (meta_hovering != item_meta) {
3062 if (meta_hovering) {
3063 emit_signal(SNAME("meta_hover_ended"), current_meta);
3064 }
3065 meta_hovering = item_meta;
3066 current_meta = meta;
3067 emit_signal(SNAME("meta_hover_started"), meta);
3068 if ((item_meta && item_meta->underline == META_UNDERLINE_ON_HOVER) || (prev_meta && prev_meta->underline == META_UNDERLINE_ON_HOVER)) {
3069 queue_redraw();
3070 }
3071 }
3072 } else if (meta_hovering) {
3073 meta_hovering = nullptr;
3074 emit_signal(SNAME("meta_hover_ended"), current_meta);
3075 current_meta = false;
3076 if (prev_meta->underline == META_UNDERLINE_ON_HOVER) {
3077 queue_redraw();
3078 }
3079 }
3080 }
3081 }
3082
3083 void RichTextLabel::_update_selection() {
3084 ItemFrame *c_frame = nullptr;
3085 int c_line = 0;
3086 Item *c_item = nullptr;
3087 int c_index = 0;
3088 bool outside;
3089
3090 // Handle auto scrolling.
3091 const Size2 size = get_size();
3092 if (!(local_mouse_pos.x >= 0.0 && local_mouse_pos.y >= 0.0 &&
3093 local_mouse_pos.x < size.x && local_mouse_pos.y < size.y)) {
3094 real_t scroll_delta = 0.0;
3095 if (local_mouse_pos.y < 0) {
3096 scroll_delta = -auto_scroll_speed * (1 - (local_mouse_pos.y / 15.0));
3097 } else if (local_mouse_pos.y > size.y) {
3098 scroll_delta = auto_scroll_speed * (1 + (local_mouse_pos.y - size.y) / 15.0);
3099 }
3100
3101 if (scroll_delta != 0.0) {
3102 vscroll->scroll(scroll_delta);
3103 queue_redraw();
3104 }
3105 }
3106
3107 // Update selection area.
3108 _find_click(main, last_clamped_mouse_pos, &c_frame, &c_line, &c_item, &c_index, &outside, false);
3109 if (selection.click_item && c_item) {
3110 selection.from_frame = selection.click_frame;
3111 selection.from_line = selection.click_line;
3112 selection.from_item = selection.click_item;
3113 selection.from_char = selection.click_char;
3114
3115 selection.to_frame = c_frame;
3116 selection.to_line = c_line;
3117 selection.to_item = c_item;
3118 selection.to_char = c_index;
3119
3120 bool swap = false;
3121 if (selection.click_frame && c_frame) {
3122 const Line &l1 = c_frame->lines[c_line];
3123 const Line &l2 = selection.click_frame->lines[selection.click_line];
3124 if (l1.char_offset + c_index < l2.char_offset + selection.click_char) {
3125 swap = true;
3126 } else if (l1.char_offset + c_index == l2.char_offset + selection.click_char && !selection.double_click) {
3127 deselect();
3128 return;
3129 }
3130 }
3131
3132 if (swap) {
3133 SWAP(selection.from_frame, selection.to_frame);
3134 SWAP(selection.from_line, selection.to_line);
3135 SWAP(selection.from_item, selection.to_item);
3136 SWAP(selection.from_char, selection.to_char);
3137 }
3138
3139 if (selection.double_click && c_frame) {
3140 // Expand the selection to word edges.
3141
3142 Line *l = &selection.from_frame->lines[selection.from_line];
3143 MutexLock lock(l->text_buf->get_mutex());
3144 PackedInt32Array words = TS->shaped_text_get_word_breaks(l->text_buf->get_rid());
3145 for (int i = 0; i < words.size(); i = i + 2) {
3146 if (selection.from_char > words[i] && selection.from_char < words[i + 1]) {
3147 selection.from_char = words[i];
3148 break;
3149 }
3150 }
3151 l = &selection.to_frame->lines[selection.to_line];
3152 lock = MutexLock(l->text_buf->get_mutex());
3153 words = TS->shaped_text_get_word_breaks(l->text_buf->get_rid());
3154 for (int i = 0; i < words.size(); i = i + 2) {
3155 if (selection.to_char > words[i] && selection.to_char < words[i + 1]) {
3156 selection.to_char = words[i + 1];
3157 break;
3158 }
3159 }
3160 }
3161
3162 selection.active = true;
3163 queue_accessibility_update();
3164 queue_redraw();
3165 }
3166 }
3167
3168 String RichTextLabel::get_tooltip(const Point2 &p_pos) const {
3169 Item *c_item = nullptr;
3170 bool outside;
3171
3172 const_cast<RichTextLabel *>(this)->_find_click(main, p_pos, nullptr, nullptr, &c_item, nullptr, &outside, true);
3173
3174 String description;
3175 if (c_item && !outside) {
3176 ItemMeta *meta = nullptr;
3177 if (const_cast<RichTextLabel *>(this)->_find_hint(c_item, &description)) {
3178 return description;
3179 } else if (c_item->type == ITEM_IMAGE && !static_cast<ItemImage *>(c_item)->tooltip.is_empty()) {
3180 return static_cast<ItemImage *>(c_item)->tooltip;
3181 } else if (const_cast<RichTextLabel *>(this)->_find_meta(c_item, nullptr, &meta) && meta && !meta->tooltip.is_empty()) {
3182 return meta->tooltip;
3183 }
3184 }
3185
3186 return Control::get_tooltip(p_pos);
3187 }
3188
3189 void RichTextLabel::_find_frame(Item *p_item, ItemFrame **r_frame, int *r_line) {
3190 if (r_frame != nullptr) {
3191 *r_frame = nullptr;
3192 }
3193 if (r_line != nullptr) {
3194 *r_line = 0;
3195 }
3196
3197 Item *item = p_item;
3198
3199 while (item) {
3200 if (item->parent != nullptr && item->parent->type == ITEM_FRAME) {
3201 if (r_frame != nullptr) {
3202 *r_frame = static_cast<ItemFrame *>(item->parent);
3203 }
3204 if (r_line != nullptr) {
3205 *r_line = item->line;
3206 }
3207 return;
3208 }
3209
3210 item = item->parent;
3211 }
3212 }
3213
3214 RichTextLabel::Item *RichTextLabel::_find_indentable(Item *p_item) {
3215 Item *indentable = p_item;
3216
3217 while (indentable) {
3218 if (indentable->type == ITEM_INDENT || indentable->type == ITEM_LIST) {
3219 return indentable;
3220 }
3221 indentable = indentable->parent;
3222 }
3223
3224 return indentable;
3225 }
3226
3227 RichTextLabel::ItemFont *RichTextLabel::_find_font(Item *p_item) {
3228 Item *fontitem = p_item;
3229
3230 while (fontitem) {
3231 if (fontitem->type == ITEM_FONT) {
3232 ItemFont *fi = static_cast<ItemFont *>(fontitem);
3233 switch (fi->def_font) {
3234 case RTL_NORMAL_FONT: {
3235 if (fi->variation) {
3236 Ref<FontVariation> fc = fi->font;
3237 if (fc.is_valid()) {
3238 fc->set_base_font(theme_cache.normal_font);
3239 }
3240 } else {
3241 fi->font = theme_cache.normal_font;
3242 }
3243 if (fi->def_size) {
3244 fi->font_size = theme_cache.normal_font_size;
3245 }
3246 } break;
3247 case RTL_BOLD_FONT: {
3248 if (fi->variation) {
3249 Ref<FontVariation> fc = fi->font;
3250 if (fc.is_valid()) {
3251 fc->set_base_font(theme_cache.bold_font);
3252 }
3253 } else {
3254 fi->font = theme_cache.bold_font;
3255 }
3256 if (fi->def_size) {
3257 fi->font_size = theme_cache.bold_font_size;
3258 }
3259 } break;
3260 case RTL_ITALICS_FONT: {
3261 if (fi->variation) {
3262 Ref<FontVariation> fc = fi->font;
3263 if (fc.is_valid()) {
3264 fc->set_base_font(theme_cache.italics_font);
3265 }
3266 } else {
3267 fi->font = theme_cache.italics_font;
3268 }
3269 if (fi->def_size) {
3270 fi->font_size = theme_cache.italics_font_size;
3271 }
3272 } break;
3273 case RTL_BOLD_ITALICS_FONT: {
3274 if (fi->variation) {
3275 Ref<FontVariation> fc = fi->font;
3276 if (fc.is_valid()) {
3277 fc->set_base_font(theme_cache.bold_italics_font);
3278 }
3279 } else {
3280 fi->font = theme_cache.bold_italics_font;
3281 }
3282 if (fi->def_size) {
3283 fi->font_size = theme_cache.bold_italics_font_size;
3284 }
3285 } break;
3286 case RTL_MONO_FONT: {
3287 if (fi->variation) {
3288 Ref<FontVariation> fc = fi->font;
3289 if (fc.is_valid()) {
3290 fc->set_base_font(theme_cache.mono_font);
3291 }
3292 } else {
3293 fi->font = theme_cache.mono_font;
3294 }
3295 if (fi->def_size) {
3296 fi->font_size = theme_cache.mono_font_size;
3297 }
3298 } break;
3299 default: {
3300 } break;
3301 }
3302 return fi;
3303 }
3304
3305 fontitem = fontitem->parent;
3306 }
3307
3308 return nullptr;
3309 }
3310
3311 RichTextLabel::ItemFontSize *RichTextLabel::_find_font_size(Item *p_item) {
3312 Item *sizeitem = p_item;
3313
3314 while (sizeitem) {
3315 if (sizeitem->type == ITEM_FONT_SIZE) {
3316 ItemFontSize *fi = static_cast<ItemFontSize *>(sizeitem);
3317 return fi;
3318 }
3319
3320 sizeitem = sizeitem->parent;
3321 }
3322
3323 return nullptr;
3324 }
3325
3326 int RichTextLabel::_find_outline_size(Item *p_item, int p_default) {
3327 Item *sizeitem = p_item;
3328
3329 while (sizeitem) {
3330 if (sizeitem->type == ITEM_OUTLINE_SIZE) {
3331 ItemOutlineSize *fi = static_cast<ItemOutlineSize *>(sizeitem);
3332 return fi->outline_size;
3333 }
3334
3335 sizeitem = sizeitem->parent;
3336 }
3337
3338 return p_default;
3339 }
3340
3341 RichTextLabel::ItemDropcap *RichTextLabel::_find_dc_item(Item *p_item) {
3342 Item *item = p_item;
3343
3344 while (item) {
3345 if (item->type == ITEM_DROPCAP) {
3346 return static_cast<ItemDropcap *>(item);
3347 }
3348 item = item->parent;
3349 }
3350
3351 return nullptr;
3352 }
3353
3354 RichTextLabel::ItemList *RichTextLabel::_find_list_item(Item *p_item) {
3355 Item *item = p_item;
3356
3357 while (item) {
3358 if (item->type == ITEM_LIST) {
3359 return static_cast<ItemList *>(item);
3360 }
3361 item = item->parent;
3362 }
3363
3364 return nullptr;
3365 }
3366
3367 int RichTextLabel::_find_list(Item *p_item, Vector<int> &r_index, Vector<int> &r_count, Vector<ItemList *> &r_list) {
3368 Item *item = p_item;
3369 Item *prev_item = p_item;
3370
3371 int level = 0;
3372
3373 while (item) {
3374 if (item->type == ITEM_LIST) {
3375 ItemList *list = static_cast<ItemList *>(item);
3376
3377 ItemFrame *frame = nullptr;
3378 int line = -1;
3379 _find_frame(list, &frame, &line);
3380
3381 int index = 1;
3382 int count = 1;
3383 if (frame != nullptr) {
3384 for (int i = list->line + 1; i < (int)frame->lines.size(); i++) {
3385 if (_find_list_item(frame->lines[i].from) == list) {
3386 if (i <= prev_item->line) {
3387 index++;
3388 }
3389 count++;
3390 }
3391 }
3392 }
3393
3394 r_index.push_back(index);
3395 r_count.push_back(count);
3396 r_list.push_back(list);
3397
3398 prev_item = item;
3399 }
3400 level++;
3401 item = item->parent;
3402 }
3403
3404 return level;
3405 }
3406
3407 int RichTextLabel::_find_margin(Item *p_item, const Ref<Font> &p_base_font, int p_base_font_size) {
3408 Item *item = p_item;
3409
3410 float margin = 0.0;
3411
3412 while (item) {
3413 if (item->type == ITEM_FRAME) {
3414 break;
3415 }
3416
3417 if (item->type == ITEM_INDENT) {
3418 Ref<Font> font = p_base_font;
3419 int font_size = p_base_font_size;
3420
3421 ItemFont *font_it = _find_font(item);
3422 if (font_it) {
3423 if (font_it->font.is_valid()) {
3424 font = font_it->font;
3425 }
3426 if (font_it->font_size > 0) {
3427 font_size = font_it->font_size;
3428 }
3429 }
3430 ItemFontSize *font_size_it = _find_font_size(item);
3431 if (font_size_it && font_size_it->font_size > 0) {
3432 font_size = font_size_it->font_size;
3433 }
3434 margin += MAX(1, tab_size * (font->get_char_size(' ', font_size).width + font->get_spacing(TextServer::SPACING_SPACE)));
3435
3436 } else if (item->type == ITEM_LIST) {
3437 Ref<Font> font = p_base_font;
3438 int font_size = p_base_font_size;
3439
3440 ItemFont *font_it = _find_font(item);
3441 if (font_it) {
3442 if (font_it->font.is_valid()) {
3443 font = font_it->font;
3444 }
3445 if (font_it->font_size > 0) {
3446 font_size = font_it->font_size;
3447 }
3448 }
3449 ItemFontSize *font_size_it = _find_font_size(item);
3450 if (font_size_it && font_size_it->font_size > 0) {
3451 font_size = font_size_it->font_size;
3452 }
3453 margin += MAX(1, tab_size * (font->get_char_size(' ', font_size).width + font->get_spacing(TextServer::SPACING_SPACE)));
3454 }
3455
3456 item = item->parent;
3457 }
3458
3459 return margin;
3460 }
3461
3462 BitField<TextServer::JustificationFlag> RichTextLabel::_find_jst_flags(Item *p_item) {
3463 Item *item = p_item;
3464
3465 while (item) {
3466 if (item->type == ITEM_PARAGRAPH) {
3467 ItemParagraph *p = static_cast<ItemParagraph *>(item);
3468 return p->jst_flags;
3469 }
3470
3471 item = item->parent;
3472 }
3473
3474 return default_jst_flags;
3475 }
3476
3477 PackedFloat32Array RichTextLabel::_find_tab_stops(Item *p_item) {
3478 Item *item = p_item;
3479
3480 while (item) {
3481 if (item->type == ITEM_PARAGRAPH) {
3482 ItemParagraph *p = static_cast<ItemParagraph *>(item);
3483 return p->tab_stops;
3484 }
3485
3486 item = item->parent;
3487 }
3488
3489 return default_tab_stops;
3490 }
3491
3492 HorizontalAlignment RichTextLabel::_find_alignment(Item *p_item) {
3493 Item *item = p_item;
3494
3495 while (item) {
3496 if (item->type == ITEM_PARAGRAPH) {
3497 ItemParagraph *p = static_cast<ItemParagraph *>(item);
3498 return p->alignment;
3499 }
3500
3501 item = item->parent;
3502 }
3503
3504 return default_alignment;
3505 }
3506
3507 TextServer::Direction RichTextLabel::_find_direction(Item *p_item) {
3508 Item *item = p_item;
3509
3510 while (item) {
3511 if (item->type == ITEM_PARAGRAPH) {
3512 ItemParagraph *p = static_cast<ItemParagraph *>(item);
3513 if (p->direction != Control::TEXT_DIRECTION_INHERITED) {
3514 return (TextServer::Direction)p->direction;
3515 }
3516 }
3517
3518 item = item->parent;
3519 }
3520
3521 if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
3522 return is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR;
3523 } else {
3524 return (TextServer::Direction)text_direction;
3525 }
3526 }
3527
3528 TextServer::StructuredTextParser RichTextLabel::_find_stt(Item *p_item) {
3529 Item *item = p_item;
3530
3531 while (item) {
3532 if (item->type == ITEM_PARAGRAPH) {
3533 ItemParagraph *p = static_cast<ItemParagraph *>(item);
3534 return p->st_parser;
3535 }
3536
3537 item = item->parent;
3538 }
3539
3540 return st_parser;
3541 }
3542
3543 String RichTextLabel::_find_language(Item *p_item) {
3544 String lang = language;
3545 for (Item *item = p_item; item; item = item->parent) {
3546 if (item->type == ITEM_LANGUAGE) {
3547 ItemLanguage *p = static_cast<ItemLanguage *>(item);
3548 lang = p->language;
3549 break;
3550 }
3551 if (item->type == ITEM_PARAGRAPH) {
3552 ItemParagraph *p = static_cast<ItemParagraph *>(item);
3553 lang = p->language;
3554 break;
3555 }
3556 }
3557 return lang.is_empty() ? _get_locale() : lang;
3558 }
3559
3560 Color RichTextLabel::_find_color(Item *p_item, const Color &p_default_color) {
3561 Item *item = p_item;
3562
3563 while (item) {
3564 if (item->type == ITEM_COLOR) {
3565 ItemColor *color = static_cast<ItemColor *>(item);
3566 return color->color;
3567 }
3568
3569 item = item->parent;
3570 }
3571
3572 return p_default_color;
3573 }
3574
3575 Color RichTextLabel::_find_outline_color(Item *p_item, const Color &p_default_color) {
3576 Item *item = p_item;
3577
3578 while (item) {
3579 if (item->type == ITEM_OUTLINE_COLOR) {
3580 ItemOutlineColor *color = static_cast<ItemOutlineColor *>(item);
3581 return color->color;
3582 }
3583
3584 item = item->parent;
3585 }
3586
3587 return p_default_color;
3588 }
3589
3590 bool RichTextLabel::_find_underline(Item *p_item, Color *r_color) {
3591 Item *item = p_item;
3592
3593 while (item) {
3594 if (item->type == ITEM_UNDERLINE) {
3595 if (r_color) {
3596 ItemUnderline *ul = static_cast<ItemUnderline *>(item);
3597 *r_color = ul->color;
3598 }
3599 return true;
3600 }
3601
3602 item = item->parent;
3603 }
3604
3605 return false;
3606 }
3607
3608 bool RichTextLabel::_find_strikethrough(Item *p_item, Color *r_color) {
3609 Item *item = p_item;
3610
3611 while (item) {
3612 if (item->type == ITEM_STRIKETHROUGH) {
3613 if (r_color) {
3614 ItemStrikethrough *st = static_cast<ItemStrikethrough *>(item);
3615 *r_color = st->color;
3616 }
3617 return true;
3618 }
3619
3620 item = item->parent;
3621 }
3622
3623 return false;
3624 }
3625
3626 void RichTextLabel::_fetch_item_fx_stack(Item *p_item, Vector<ItemFX *> &r_stack) {
3627 Item *item = p_item;
3628 while (item) {
3629 if (item->type == ITEM_CUSTOMFX || item->type == ITEM_SHAKE || item->type == ITEM_WAVE || item->type == ITEM_TORNADO || item->type == ITEM_RAINBOW || item->type == ITEM_PULSE) {
3630 r_stack.push_back(static_cast<ItemFX *>(item));
3631 }
3632
3633 item = item->parent;
3634 }
3635 }
3636
3637 void RichTextLabel::_normalize_subtags(Vector<String> &subtags) {
3638 for (String &subtag : subtags) {
3639 subtag = subtag.unquote();
3640 }
3641 }
3642
3643 bool RichTextLabel::_find_meta(Item *p_item, Variant *r_meta, ItemMeta **r_item) {
3644 Item *item = p_item;
3645
3646 while (item) {
3647 if (item->type == ITEM_META) {
3648 ItemMeta *meta = static_cast<ItemMeta *>(item);
3649 if (r_meta) {
3650 *r_meta = meta->meta;
3651 }
3652 if (r_item) {
3653 *r_item = meta;
3654 }
3655 return true;
3656 }
3657
3658 item = item->parent;
3659 }
3660
3661 return false;
3662 }
3663
3664 bool RichTextLabel::_find_hint(Item *p_item, String *r_description) {
3665 Item *item = p_item;
3666
3667 while (item) {
3668 if (item->type == ITEM_HINT) {
3669 ItemHint *hint = static_cast<ItemHint *>(item);
3670 if (r_description) {
3671 *r_description = hint->description;
3672 }
3673 return true;
3674 }
3675
3676 item = item->parent;
3677 }
3678
3679 return false;
3680 }
3681
3682 Color RichTextLabel::_find_bgcolor(Item *p_item) {
3683 Item *item = p_item;
3684
3685 while (item) {
3686 if (item->type == ITEM_BGCOLOR) {
3687 ItemBGColor *color = static_cast<ItemBGColor *>(item);
3688 return color->color;
3689 }
3690
3691 item = item->parent;
3692 }
3693
3694 return Color(0, 0, 0, 0);
3695 }
3696
3697 Color RichTextLabel::_find_fgcolor(Item *p_item) {
3698 Item *item = p_item;
3699
3700 while (item) {
3701 if (item->type == ITEM_FGCOLOR) {
3702 ItemFGColor *color = static_cast<ItemFGColor *>(item);
3703 return color->color;
3704 }
3705
3706 item = item->parent;
3707 }
3708
3709 return Color(0, 0, 0, 0);
3710 }
3711
3712 bool RichTextLabel::_find_layout_subitem(Item *from, Item *to) {
3713 if (from && from != to) {
3714 if (from->type != ITEM_FONT && from->type != ITEM_COLOR && from->type != ITEM_UNDERLINE && from->type != ITEM_STRIKETHROUGH) {
3715 return true;
3716 }
3717
3718 for (Item *E : from->subitems) {
3719 bool layout = _find_layout_subitem(E, to);
3720
3721 if (layout) {
3722 return true;
3723 }
3724 }
3725 }
3726
3727 return false;
3728 }
3729
3730 void RichTextLabel::_thread_function(void *p_userdata) {
3731 set_current_thread_safe_for_nodes(true);
3732 _process_line_caches();
3733 callable_mp(this, &RichTextLabel::_thread_end).call_deferred();
3734 }
3735
3736 void RichTextLabel::_thread_end() {
3737 set_physics_process_internal(false);
3738 if (!scroll_visible) {
3739 vscroll->hide();
3740 }
3741 if (is_visible_in_tree()) {
3742 queue_accessibility_update();
3743 queue_redraw();
3744 }
3745 }
3746
3747 void RichTextLabel::_stop_thread() {
3748 if (threaded) {
3749 stop_thread.store(true);
3750 wait_until_finished();
3751 }
3752 }
3753
3754 int RichTextLabel::get_pending_paragraphs() const {
3755 int to_line = main->first_invalid_line.load();
3756 int lines = main->lines.size();
3757
3758 return lines - to_line;
3759 }
3760
3761 bool RichTextLabel::is_finished() const {
3762 const_cast<RichTextLabel *>(this)->_validate_line_caches();
3763
3764 if (updating.load()) {
3765 return false;
3766 }
3767 return (main->first_invalid_line.load() == (int)main->lines.size() && main->first_resized_line.load() == (int)main->lines.size() && main->first_invalid_font_line.load() == (int)main->lines.size());
3768 }
3769
3770 bool RichTextLabel::is_updating() const {
3771 return updating.load() || validating.load();
3772 }
3773
3774 void RichTextLabel::wait_until_finished() {
3775 if (task != WorkerThreadPool::INVALID_TASK_ID) {
3776 WorkerThreadPool::get_singleton()->wait_for_task_completion(task);
3777 task = WorkerThreadPool::INVALID_TASK_ID;
3778 }
3779 }
3780
3781 void RichTextLabel::set_threaded(bool p_threaded) {
3782 if (threaded != p_threaded) {
3783 _stop_thread();
3784 threaded = p_threaded;
3785 queue_redraw();
3786 }
3787 }
3788
3789 bool RichTextLabel::is_threaded() const {
3790 return threaded;
3791 }
3792
3793 void RichTextLabel::set_progress_bar_delay(int p_delay_ms) {
3794 progress_delay = p_delay_ms;
3795 }
3796
3797 int RichTextLabel::get_progress_bar_delay() const {
3798 return progress_delay;
3799 }
3800
3801 _FORCE_INLINE_ float RichTextLabel::_update_scroll_exceeds(float p_total_height, float p_ctrl_height, float p_width, int p_idx, float p_old_scroll, float p_text_rect_height) {
3802 updating_scroll = true;
3803
3804 float total_height = p_total_height;
3805 bool exceeds = p_total_height > p_ctrl_height && scroll_active;
3806 if (exceeds != scroll_visible) {
3807 if (exceeds) {
3808 scroll_visible = true;
3809 _prepare_scroll_anchor();
3810 vscroll->show();
3811 } else {
3812 scroll_visible = false;
3813 scroll_w = 0;
3814 }
3815
3816 main->first_resized_line.store(0);
3817
3818 total_height = 0;
3819 for (int j = 0; j <= p_idx; j++) {
3820 total_height = _resize_line(main, j, theme_cache.normal_font, theme_cache.normal_font_size, p_width - scroll_w, total_height);
3821
3822 main->first_resized_line.store(j);
3823 }
3824 }
3825 vscroll->set_max(total_height);
3826 vscroll->set_page(p_text_rect_height);
3827 if (scroll_follow && scroll_following) {
3828 vscroll->set_value(total_height);
3829 } else {
3830 vscroll->set_value(p_old_scroll);
3831 }
3832 updating_scroll = false;
3833
3834 return total_height;
3835 }
3836
3837 bool RichTextLabel::_validate_line_caches() {
3838 if (updating.load()) {
3839 return false;
3840 }
3841 validating.store(true);
3842 if (main->first_invalid_line.load() == (int)main->lines.size()) {
3843 MutexLock data_lock(data_mutex);
3844 Rect2 text_rect = _get_text_rect();
3845
3846 float ctrl_height = get_size().height;
3847
3848 // Update fonts.
3849 float old_scroll = vscroll->get_value();
3850 if (main->first_invalid_font_line.load() != (int)main->lines.size()) {
3851 for (int i = main->first_invalid_font_line.load(); i < (int)main->lines.size(); i++) {
3852 _update_line_font(main, i, theme_cache.normal_font, theme_cache.normal_font_size);
3853 }
3854 main->first_resized_line.store(main->first_invalid_font_line.load());
3855 main->first_invalid_font_line.store(main->lines.size());
3856 }
3857
3858 if (main->first_resized_line.load() == (int)main->lines.size()) {
3859 vscroll->set_value(old_scroll);
3860 validating.store(false);
3861 if (!scroll_visible) {
3862 vscroll->hide();
3863 }
3864 queue_accessibility_update();
3865 return true;
3866 }
3867
3868 // Resize lines without reshaping.
3869 int fi = main->first_resized_line.load();
3870
3871 float total_height = (fi == 0) ? 0 : _calculate_line_vertical_offset(main->lines[fi - 1]);
3872 for (int i = fi; i < (int)main->lines.size(); i++) {
3873 total_height = _resize_line(main, i, theme_cache.normal_font, theme_cache.normal_font_size, text_rect.get_size().width - scroll_w, total_height);
3874 total_height = _update_scroll_exceeds(total_height, ctrl_height, text_rect.get_size().width, i, old_scroll, text_rect.size.height);
3875 main->first_resized_line.store(i);
3876 }
3877
3878 main->first_resized_line.store(main->lines.size());
3879
3880 if (fit_content) {
3881 update_minimum_size();
3882 }
3883 validating.store(false);
3884 if (!scroll_visible) {
3885 vscroll->hide();
3886 }
3887 queue_accessibility_update();
3888 return true;
3889 }
3890 validating.store(false);
3891 stop_thread.store(false);
3892 if (threaded) {
3893 updating.store(true);
3894 loaded.store(true);
3895 task = WorkerThreadPool::get_singleton()->add_template_task(this, &RichTextLabel::_thread_function, nullptr, true, vformat("RichTextLabelShape:%x", (int64_t)get_instance_id()));
3896 set_physics_process_internal(true);
3897 loading_started = OS::get_singleton()->get_ticks_msec();
3898 queue_accessibility_update();
3899 return false;
3900 } else {
3901 updating.store(true);
3902 _process_line_caches();
3903 if (!scroll_visible) {
3904 vscroll->hide();
3905 }
3906 queue_accessibility_update();
3907 queue_redraw();
3908 return true;
3909 }
3910 }
3911
3912 void RichTextLabel::_process_line_caches() {
3913 // Shape invalid lines.
3914 if (!is_inside_tree()) {
3915 updating.store(false);
3916 return;
3917 }
3918
3919 MutexLock data_lock(data_mutex);
3920 Rect2 text_rect = _get_text_rect();
3921
3922 float ctrl_height = get_size().height;
3923 int fi = main->first_invalid_line.load();
3924 int total_chars = main->lines[fi].char_offset;
3925 float old_scroll = vscroll->get_value();
3926
3927 float total_height = 0;
3928 if (fi != 0) {
3929 int sr = MIN(main->first_invalid_font_line.load(), main->first_resized_line.load());
3930
3931 // Update fonts.
3932 for (int i = main->first_invalid_font_line.load(); i < fi; i++) {
3933 _update_line_font(main, i, theme_cache.normal_font, theme_cache.normal_font_size);
3934
3935 main->first_invalid_font_line.store(i);
3936
3937 if (stop_thread.load()) {
3938 updating.store(false);
3939 return;
3940 }
3941 }
3942
3943 // Resize lines without reshaping.
3944 if (sr != 0) {
3945 total_height = _calculate_line_vertical_offset(main->lines[sr - 1]);
3946 }
3947
3948 for (int i = sr; i < fi; i++) {
3949 total_height = _resize_line(main, i, theme_cache.normal_font, theme_cache.normal_font_size, text_rect.get_size().width - scroll_w, total_height);
3950 total_height = _update_scroll_exceeds(total_height, ctrl_height, text_rect.get_size().width, i, old_scroll, text_rect.size.height);
3951
3952 main->first_resized_line.store(i);
3953
3954 if (stop_thread.load()) {
3955 updating.store(false);
3956 return;
3957 }
3958 }
3959 }
3960
3961 total_height = (fi == 0) ? 0 : _calculate_line_vertical_offset(main->lines[fi - 1]);
3962 for (int i = fi; i < (int)main->lines.size(); i++) {
3963 total_height = _shape_line(main, i, theme_cache.normal_font, theme_cache.normal_font_size, text_rect.get_size().width - scroll_w, total_height, &total_chars);
3964 total_height = _update_scroll_exceeds(total_height, ctrl_height, text_rect.get_size().width, i, old_scroll, text_rect.size.height);
3965
3966 main->first_invalid_line.store(i);
3967 main->first_resized_line.store(i);
3968 main->first_invalid_font_line.store(i);
3969
3970 if (stop_thread.load()) {
3971 updating.store(false);
3972 return;
3973 }
3974 loaded.store(double(i) / double(main->lines.size()));
3975 }
3976
3977 main->first_invalid_line.store(main->lines.size());
3978 main->first_resized_line.store(main->lines.size());
3979 main->first_invalid_font_line.store(main->lines.size());
3980 updating.store(false);
3981
3982 if (fit_content) {
3983 update_minimum_size();
3984 }
3985 emit_signal(SceneStringName(finished));
3986 }
3987
3988 void RichTextLabel::_invalidate_current_line(ItemFrame *p_frame) {
3989 if ((int)p_frame->lines.size() - 1 <= p_frame->first_invalid_line) {
3990 p_frame->first_invalid_line = (int)p_frame->lines.size() - 1;
3991 queue_accessibility_update();
3992 }
3993 }
3994
3995 void RichTextLabel::_texture_changed(RID p_item) {
3996 Item *it = items.get_or_null(p_item);
3997 if (it && it->type == ITEM_IMAGE) {
3998 ItemImage *img = reinterpret_cast<ItemImage *>(it);
3999 Size2 new_size = _get_image_size(img->image, img->rq_size.width, img->rq_size.height, img->region);
4000 if (img->size != new_size) {
4001 main->first_invalid_line.store(0);
4002 img->size = new_size;
4003 }
4004 }
4005 queue_redraw();
4006 }
4007
4008 void RichTextLabel::add_text(const String &p_text) {
4009 _stop_thread();
4010 MutexLock data_lock(data_mutex);
4011
4012 if (current->type == ITEM_TABLE) {
4013 return; //can't add anything here
4014 }
4015
4016 int pos = 0;
4017 String t = p_text.replace("\r\n", "\n");
4018
4019 while (pos < t.length()) {
4020 int end = t.find_char('\n', pos);
4021 String line;
4022 bool eol = false;
4023 if (end == -1) {
4024 end = t.length();
4025 } else {
4026 eol = true;
4027 }
4028
4029 if (pos == 0 && end == t.length()) {
4030 line = t;
4031 } else {
4032 line = t.substr(pos, end - pos);
4033 }
4034
4035 if (line.length() > 0) {
4036 if (current->subitems.size() && current->subitems.back()->get()->type == ITEM_TEXT) {
4037 //append text condition!
4038 ItemText *ti = static_cast<ItemText *>(current->subitems.back()->get());
4039 ti->text += line;
4040 current_char_ofs += line.length();
4041 _invalidate_current_line(main);
4042
4043 } else {
4044 //append item condition
4045 ItemText *item = memnew(ItemText);
4046 item->owner = get_instance_id();
4047 item->rid = items.make_rid(item);
4048 item->text = line;
4049 _add_item(item, false);
4050 }
4051 }
4052
4053 if (eol) {
4054 ItemNewline *item = memnew(ItemNewline); // Sets item->type to ITEM_NEWLINE.
4055 item->owner = get_instance_id();
4056 item->rid = items.make_rid(item);
4057 item->line = current_frame->lines.size();
4058 _add_item(item, false);
4059 current_frame->lines.resize(current_frame->lines.size() + 1);
4060 if (item->type != ITEM_NEWLINE) { // item IS an ITEM_NEWLINE so this will never get called?
4061 current_frame->lines[current_frame->lines.size() - 1].from = item;
4062 }
4063 _invalidate_current_line(current_frame);
4064 }
4065
4066 pos = end + 1;
4067 }
4068 queue_redraw();
4069 }
4070
4071 void RichTextLabel::_add_item(Item *p_item, bool p_enter, bool p_ensure_newline) {
4072 if (!internal_stack_editing) {
4073 stack_externally_modified = true;
4074 }
4075
4076 if (p_enter && !parsing_bbcode.load() && !tag_stack.is_empty()) {
4077 tag_stack.push_back(U"?");
4078 }
4079
4080 p_item->parent = current;
4081 p_item->E = current->subitems.push_back(p_item);
4082 p_item->index = current_idx++;
4083 p_item->char_ofs = current_char_ofs;
4084 if (p_item->type == ITEM_TEXT) {
4085 ItemText *t = static_cast<ItemText *>(p_item);
4086 current_char_ofs += t->text.length();
4087 } else if (p_item->type == ITEM_IMAGE) {
4088 current_char_ofs++;
4089 } else if (p_item->type == ITEM_NEWLINE) {
4090 current_char_ofs++;
4091 }
4092
4093 if (p_enter) {
4094 current = p_item;
4095 }
4096
4097 if (p_ensure_newline) {
4098 Item *from = current_frame->lines[current_frame->lines.size() - 1].from;
4099 // only create a new line for Item types that generate content/layout, ignore those that represent formatting/styling
4100 if (_find_layout_subitem(from, p_item)) {
4101 _invalidate_current_line(current_frame);
4102 current_frame->lines.resize(current_frame->lines.size() + 1);
4103 }
4104 }
4105
4106 if (current_frame->lines[current_frame->lines.size() - 1].from == nullptr) {
4107 current_frame->lines[current_frame->lines.size() - 1].from = p_item;
4108 }
4109 p_item->line = current_frame->lines.size() - 1;
4110
4111 _invalidate_current_line(current_frame);
4112
4113 if (fit_content) {
4114 update_minimum_size();
4115 }
4116 queue_accessibility_update();
4117 queue_redraw();
4118 }
4119
4120 Size2 RichTextLabel::_get_image_size(const Ref<Texture2D> &p_image, int p_width, int p_height, const Rect2 &p_region) {
4121 Size2 ret;
4122 if (p_width > 0) {
4123 // custom width
4124 ret.width = p_width;
4125 if (p_height > 0) {
4126 // custom height
4127 ret.height = p_height;
4128 } else {
4129 // calculate height to keep aspect ratio
4130 if (p_region.has_area()) {
4131 ret.height = p_region.get_size().height * p_width / p_region.get_size().width;
4132 } else {
4133 ret.height = p_image->get_height() * p_width / p_image->get_width();
4134 }
4135 }
4136 } else {
4137 if (p_height > 0) {
4138 // custom height
4139 ret.height = p_height;
4140 // calculate width to keep aspect ratio
4141 if (p_region.has_area()) {
4142 ret.width = p_region.get_size().width * p_height / p_region.get_size().height;
4143 } else {
4144 ret.width = p_image->get_width() * p_height / p_image->get_height();
4145 }
4146 } else {
4147 if (p_region.has_area()) {
4148 // if the image has a region, keep the region size
4149 ret = p_region.get_size();
4150 } else {
4151 // keep original width and height
4152 ret = p_image->get_size();
4153 }
4154 }
4155 }
4156 return ret;
4157 }
4158
4159 void RichTextLabel::add_hr(int p_width, int p_height, const Color &p_color, HorizontalAlignment p_alignment, bool p_width_in_percent, bool p_height_in_percent) {
4160 _stop_thread();
4161 MutexLock data_lock(data_mutex);
4162
4163 if (current->type == ITEM_TABLE) {
4164 return;
4165 }
4166
4167 ERR_FAIL_COND(p_width < 0);
4168 ERR_FAIL_COND(p_height < 0);
4169
4170 ItemParagraph *p_item = memnew(ItemParagraph);
4171 p_item->owner = get_instance_id();
4172 p_item->rid = items.make_rid(p_item);
4173 p_item->alignment = p_alignment;
4174 _add_item(p_item, true, true);
4175
4176 ItemImage *item = memnew(ItemImage);
4177 item->owner = get_instance_id();
4178 item->rid = items.make_rid(item);
4179
4180 item->image = theme_cache.horizontal_rule;
4181 item->color = p_color;
4182 item->inline_align = INLINE_ALIGNMENT_CENTER;
4183 item->rq_size = Size2(p_width, p_height);
4184 item->size = _get_image_size(theme_cache.horizontal_rule, p_width, p_height, Rect2());
4185 item->width_in_percent = p_width_in_percent;
4186 item->height_in_percent = p_height_in_percent;
4187
4188 item->image->connect_changed(callable_mp(this, &RichTextLabel::_texture_changed).bind(item->rid), CONNECT_REFERENCE_COUNTED);
4189
4190 _add_item(item, false);
4191 hr_list.insert(item->rid);
4192
4193 if (current->type == ITEM_FRAME) {
4194 current_frame = static_cast<ItemFrame *>(current)->parent_frame;
4195 }
4196 current = current->parent;
4197 if (!parsing_bbcode.load() && !tag_stack.is_empty()) {
4198 tag_stack.pop_back();
4199 }
4200 }
4201
4202 void RichTextLabel::add_image(const Ref<Texture2D> &p_image, int p_width, int p_height, const Color &p_color, InlineAlignment p_alignment, const Rect2 &p_region, const Variant &p_key, bool p_pad, const String &p_tooltip, bool p_width_in_percent, bool p_height_in_percent, const String &p_alt_text) {
4203 _stop_thread();
4204 MutexLock data_lock(data_mutex);
4205
4206 if (current->type == ITEM_TABLE) {
4207 return;
4208 }
4209
4210 ERR_FAIL_COND(p_image.is_null());
4211 ERR_FAIL_COND(p_image->get_width() == 0);
4212 ERR_FAIL_COND(p_image->get_height() == 0);
4213 ERR_FAIL_COND(p_width < 0);
4214 ERR_FAIL_COND(p_height < 0);
4215
4216 ItemImage *item = memnew(ItemImage);
4217 item->owner = get_instance_id();
4218 item->rid = items.make_rid(item);
4219
4220 if (p_region.has_area()) {
4221 Ref<AtlasTexture> atlas_tex = memnew(AtlasTexture);
4222 atlas_tex->set_atlas(p_image);
4223 atlas_tex->set_region(p_region);
4224 item->image = atlas_tex;
4225 } else {
4226 item->image = p_image;
4227 }
4228 item->color = p_color;
4229 item->inline_align = p_alignment;
4230 item->rq_size = Size2(p_width, p_height);
4231 item->region = p_region;
4232 item->size = _get_image_size(p_image, p_width, p_height, p_region);
4233 item->width_in_percent = p_width_in_percent;
4234 item->height_in_percent = p_height_in_percent;
4235 item->pad = p_pad;
4236 item->key = p_key;
4237 item->tooltip = p_tooltip;
4238 item->alt_text = p_alt_text;
4239
4240 item->image->connect_changed(callable_mp(this, &RichTextLabel::_texture_changed).bind(item->rid), CONNECT_REFERENCE_COUNTED);
4241
4242 _add_item(item, false);
4243 update_configuration_warnings();
4244 }
4245
4246 void RichTextLabel::update_image(const Variant &p_key, BitField<ImageUpdateMask> p_mask, const Ref<Texture2D> &p_image, int p_width, int p_height, const Color &p_color, InlineAlignment p_alignment, const Rect2 &p_region, bool p_pad, const String &p_tooltip, bool p_width_in_percent, bool p_height_in_percent) {
4247 _stop_thread();
4248 MutexLock data_lock(data_mutex);
4249
4250 if (p_mask & UPDATE_TEXTURE) {
4251 ERR_FAIL_COND(p_image.is_null());
4252 ERR_FAIL_COND(p_image->get_width() == 0);
4253 ERR_FAIL_COND(p_image->get_height() == 0);
4254 }
4255
4256 ERR_FAIL_COND(p_width < 0);
4257 ERR_FAIL_COND(p_height < 0);
4258
4259 bool reshape = false;
4260
4261 Item *it = main;
4262 while (it) {
4263 if (it->type == ITEM_IMAGE) {
4264 ItemImage *it_img = static_cast<ItemImage *>(it);
4265 if (it_img->key == p_key) {
4266 ItemImage *item = it_img;
4267 if (p_mask & UPDATE_REGION) {
4268 item->region = p_region;
4269 if (!(p_mask & UPDATE_TEXTURE)) {
4270 // Update existing atlas texture region, if texture is not updated.
4271 Ref<AtlasTexture> atlas_tex = item->image;
4272 if (atlas_tex.is_valid()) {
4273 atlas_tex->set_region(item->region);
4274 }
4275 }
4276 }
4277 if (p_mask & UPDATE_TEXTURE) {
4278 if (item->image.is_valid()) {
4279 item->image->disconnect_changed(callable_mp(this, &RichTextLabel::_texture_changed));
4280 }
4281 if (item->region.has_area()) {
4282 Ref<AtlasTexture> atlas_tex = memnew(AtlasTexture);
4283 atlas_tex->set_atlas(p_image);
4284 atlas_tex->set_region(item->region);
4285 item->image = atlas_tex;
4286 } else {
4287 item->image = p_image;
4288 }
4289 item->image->connect_changed(callable_mp(this, &RichTextLabel::_texture_changed).bind(item->rid), CONNECT_REFERENCE_COUNTED);
4290 }
4291 if (p_mask & UPDATE_COLOR) {
4292 item->color = p_color;
4293 }
4294 if (p_mask & UPDATE_TOOLTIP) {
4295 item->tooltip = p_tooltip;
4296 }
4297 if (p_mask & UPDATE_PAD) {
4298 item->pad = p_pad;
4299 }
4300 if (p_mask & UPDATE_ALIGNMENT) {
4301 if (item->inline_align != p_alignment) {
4302 reshape = true;
4303 item->inline_align = p_alignment;
4304 }
4305 }
4306 if (p_mask & UPDATE_WIDTH_IN_PERCENT) {
4307 if (item->width_in_percent != p_width_in_percent || item->height_in_percent != p_height_in_percent) {
4308 reshape = true;
4309 item->width_in_percent = p_width_in_percent;
4310 item->height_in_percent = p_height_in_percent;
4311 }
4312 }
4313 if (p_mask & UPDATE_SIZE) {
4314 if (p_width > 0) {
4315 item->rq_size.width = p_width;
4316 }
4317 if (p_height > 0) {
4318 item->rq_size.height = p_height;
4319 }
4320 }
4321 if ((p_mask & UPDATE_SIZE) || (p_mask & UPDATE_REGION) || (p_mask & UPDATE_TEXTURE)) {
4322 ERR_FAIL_COND(item->image.is_null());
4323 ERR_FAIL_COND(item->image->get_width() == 0);
4324 ERR_FAIL_COND(item->image->get_height() == 0);
4325 Size2 new_size = _get_image_size(item->image, item->rq_size.width, item->rq_size.height, item->region);
4326 if (item->size != new_size) {
4327 reshape = true;
4328 item->size = new_size;
4329 }
4330 }
4331 }
4332 }
4333 it = _get_next_item(it, true);
4334 }
4335
4336 if (reshape) {
4337 main->first_invalid_line.store(0);
4338 }
4339 queue_redraw();
4340 }
4341
4342 void RichTextLabel::add_newline() {
4343 _stop_thread();
4344 MutexLock data_lock(data_mutex);
4345
4346 if (current->type == ITEM_TABLE) {
4347 return;
4348 }
4349 ItemNewline *item = memnew(ItemNewline);
4350 item->owner = get_instance_id();
4351 item->rid = items.make_rid(item);
4352 item->line = current_frame->lines.size();
4353 _add_item(item, false);
4354 current_frame->lines.resize(current_frame->lines.size() + 1);
4355 _invalidate_current_line(current_frame);
4356 queue_redraw();
4357 }
4358
4359 void RichTextLabel::_remove_frame(HashSet<Item *> &r_erase_list, ItemFrame *p_frame, int p_line, bool p_erase, int p_char_offset, int p_line_offset) {
4360 Line &l = p_frame->lines[p_line];
4361 Item *it_to = (p_line + 1 < (int)p_frame->lines.size()) ? p_frame->lines[p_line + 1].from : nullptr;
4362 if (!p_erase) {
4363 l.char_offset -= p_char_offset;
4364 }
4365
4366 for (Item *it = l.from; it && it != it_to;) {
4367 Item *next_it = _get_next_item(it);
4368 it->line -= p_line_offset;
4369 if (!p_erase) {
4370 while (r_erase_list.has(it->parent)) {
4371 it->E->erase();
4372 it->parent = it->parent->parent;
4373 it->E = it->parent->subitems.push_back(it);
4374 }
4375 }
4376 if (it->type == ITEM_TABLE) {
4377 ItemTable *table = static_cast<ItemTable *>(it);
4378 for (List<Item *>::Element *sub_it = table->subitems.front(); sub_it; sub_it = sub_it->next()) {
4379 ERR_CONTINUE(sub_it->get()->type != ITEM_FRAME); // Children should all be frames.
4380 ItemFrame *frame = static_cast<ItemFrame *>(sub_it->get());
4381 for (int i = 0; i < (int)frame->lines.size(); i++) {
4382 _remove_frame(r_erase_list, frame, i, p_erase, p_char_offset, 0);
4383 }
4384 if (p_erase) {
4385 r_erase_list.insert(frame);
4386 } else {
4387 frame->char_ofs -= p_char_offset;
4388 }
4389 }
4390 }
4391 if (p_erase) {
4392 r_erase_list.insert(it);
4393 } else {
4394 it->char_ofs -= p_char_offset;
4395 }
4396 it = next_it;
4397 }
4398 }
4399
4400 bool RichTextLabel::remove_paragraph(int p_paragraph, bool p_no_invalidate) {
4401 _stop_thread();
4402 MutexLock data_lock(data_mutex);
4403
4404 if (p_paragraph >= (int)main->lines.size() || p_paragraph < 0) {
4405 return false;
4406 }
4407
4408 stack_externally_modified = true;
4409
4410 if (main->lines.size() == 1) {
4411 // Clear all.
4412 main->_clear_children();
4413 current = main;
4414 current_frame = main;
4415 main->lines.clear();
4416 main->lines.resize(1);
4417
4418 current_char_ofs = 0;
4419 } else {
4420 HashSet<Item *> erase_list;
4421 Line &l = main->lines[p_paragraph];
4422 int off = l.char_count;
4423 for (int i = p_paragraph; i < (int)main->lines.size(); i++) {
4424 if (i == p_paragraph) {
4425 _remove_frame(erase_list, main, i, true, off, 0);
4426 } else {
4427 _remove_frame(erase_list, main, i, false, off, 1);
4428 }
4429 }
4430 for (HashSet<Item *>::Iterator E = erase_list.begin(); E; ++E) {
4431 Item *it = *E;
4432 if (current_frame == it) {
4433 current_frame = main;
4434 }
4435 if (current == it) {
4436 current = main;
4437 }
4438 if (!erase_list.has(it->parent)) {
4439 it->E->erase();
4440 }
4441 items.free(it->rid);
4442 it->subitems.clear();
4443 memdelete(it);
4444 }
4445 main->lines.remove_at(p_paragraph);
4446 current_char_ofs -= off;
4447 }
4448
4449 selection.click_frame = nullptr;
4450 selection.click_item = nullptr;
4451 selection.active = false;
4452
4453 if (is_processing_internal()) {
4454 bool process_enabled = false;
4455 Item *it = main;
4456 while (it) {
4457 Vector<ItemFX *> fx_stack;
4458 _fetch_item_fx_stack(it, fx_stack);
4459 if (fx_stack.size()) {
4460 process_enabled = true;
4461 break;
4462 }
4463 it = _get_next_item(it, true);
4464 }
4465 set_process_internal(process_enabled);
4466 }
4467
4468 if (p_no_invalidate) {
4469 // Do not invalidate cache, only update vertical offsets of the paragraphs after deleted one and scrollbar.
4470 int to_line = main->first_invalid_line.load() - 1;
4471 float total_height = (p_paragraph == 0) ? 0 : _calculate_line_vertical_offset(main->lines[p_paragraph - 1]);
4472 for (int i = p_paragraph; i < to_line; i++) {
4473 MutexLock lock(main->lines[to_line - 1].text_buf->get_mutex());
4474 main->lines[i].offset.y = total_height;
4475 total_height = _calculate_line_vertical_offset(main->lines[i]);
4476 }
4477 updating_scroll = true;
4478 vscroll->set_max(total_height);
4479 updating_scroll = false;
4480
4481 main->first_invalid_line.store(MAX(main->first_invalid_line.load() - 1, 0));
4482 main->first_resized_line.store(MAX(main->first_resized_line.load() - 1, 0));
4483 main->first_invalid_font_line.store(MAX(main->first_invalid_font_line.load() - 1, 0));
4484 } else {
4485 // Invalidate cache after the deleted paragraph.
4486 main->first_invalid_line.store(MIN(main->first_invalid_line.load(), p_paragraph));
4487 main->first_resized_line.store(MIN(main->first_resized_line.load(), p_paragraph));
4488 main->first_invalid_font_line.store(MIN(main->first_invalid_font_line.load(), p_paragraph));
4489 }
4490 queue_redraw();
4491
4492 return true;
4493 }
4494
4495 bool RichTextLabel::invalidate_paragraph(int p_paragraph) {
4496 _stop_thread();
4497 MutexLock data_lock(data_mutex);
4498
4499 if (p_paragraph >= (int)main->lines.size() || p_paragraph < 0) {
4500 return false;
4501 }
4502
4503 // Invalidate cache.
4504 main->first_invalid_line.store(MIN(main->first_invalid_line.load(), p_paragraph));
4505 main->first_resized_line.store(MIN(main->first_resized_line.load(), p_paragraph));
4506 main->first_invalid_font_line.store(MIN(main->first_invalid_font_line.load(), p_paragraph));
4507
4508 _invalidate_accessibility();
4509 if (is_inside_tree()) {
4510 queue_accessibility_update();
4511 }
4512 queue_redraw();
4513 update_configuration_warnings();
4514
4515 return true;
4516 }
4517
4518 void RichTextLabel::push_dropcap(const String &p_string, const Ref<Font> &p_font, int p_size, const Rect2 &p_dropcap_margins, const Color &p_color, int p_ol_size, const Color &p_ol_color) {
4519 _stop_thread();
4520 MutexLock data_lock(data_mutex);
4521
4522 ERR_FAIL_COND(current->type == ITEM_TABLE);
4523 ERR_FAIL_COND(p_string.is_empty());
4524 ERR_FAIL_COND(p_font.is_null());
4525 ERR_FAIL_COND(p_size <= 0);
4526
4527 ItemDropcap *item = memnew(ItemDropcap);
4528 item->owner = get_instance_id();
4529 item->rid = items.make_rid(item);
4530 item->text = p_string.replace("\r\n", "\n");
4531 item->font = p_font;
4532 item->font_size = p_size;
4533 item->color = p_color;
4534 item->ol_size = p_ol_size;
4535 item->ol_color = p_ol_color;
4536 item->dropcap_margins = p_dropcap_margins;
4537 p_font->connect_changed(callable_mp(this, &RichTextLabel::_invalidate_fonts), CONNECT_REFERENCE_COUNTED);
4538
4539 _add_item(item, false);
4540 }
4541
4542 void RichTextLabel::_push_def_font_var(DefaultFont p_def_font, const Ref<Font> &p_font, int p_size) {
4543 _stop_thread();
4544 MutexLock data_lock(data_mutex);
4545
4546 ERR_FAIL_COND(current->type == ITEM_TABLE);
4547 ItemFont *item = memnew(ItemFont);
4548 item->owner = get_instance_id();
4549 item->rid = items.make_rid(item);
4550 item->def_font = p_def_font;
4551 item->variation = true;
4552 item->font = p_font;
4553 item->font_size = p_size;
4554 item->def_size = (p_size <= 0);
4555 p_font->connect_changed(callable_mp(this, &RichTextLabel::_invalidate_fonts), CONNECT_REFERENCE_COUNTED);
4556
4557 _add_item(item, true);
4558 }
4559
4560 void RichTextLabel::_push_def_font(DefaultFont p_def_font) {
4561 _stop_thread();
4562 MutexLock data_lock(data_mutex);
4563
4564 ERR_FAIL_COND(current->type == ITEM_TABLE);
4565 ItemFont *item = memnew(ItemFont);
4566 item->owner = get_instance_id();
4567 item->rid = items.make_rid(item);
4568 item->def_font = p_def_font;
4569 item->def_size = true;
4570 _add_item(item, true);
4571 }
4572
4573 void RichTextLabel::push_font(const Ref<Font> &p_font, int p_size) {
4574 _stop_thread();
4575 MutexLock data_lock(data_mutex);
4576
4577 ERR_FAIL_COND(current->type == ITEM_TABLE);
4578 ERR_FAIL_COND(p_font.is_null());
4579 ItemFont *item = memnew(ItemFont);
4580 item->owner = get_instance_id();
4581 item->rid = items.make_rid(item);
4582 item->font = p_font;
4583 item->font_size = p_size;
4584 p_font->connect_changed(callable_mp(this, &RichTextLabel::_invalidate_fonts), CONNECT_REFERENCE_COUNTED);
4585
4586 _add_item(item, true);
4587 }
4588
4589 void RichTextLabel::_invalidate_fonts() {
4590 _stop_thread();
4591 main->first_invalid_font_line.store(0); // Invalidate all lines.
4592 _invalidate_accessibility();
4593 queue_accessibility_update();
4594 queue_redraw();
4595 }
4596
4597 void RichTextLabel::push_normal() {
4598 ERR_FAIL_COND(theme_cache.normal_font.is_null());
4599
4600 _push_def_font(RTL_NORMAL_FONT);
4601 }
4602
4603 void RichTextLabel::push_bold() {
4604 ERR_FAIL_COND(theme_cache.bold_font.is_null());
4605
4606 ItemFont *item_font = _find_font(current);
4607 _push_def_font((item_font && item_font->def_font == RTL_ITALICS_FONT) ? RTL_BOLD_ITALICS_FONT : RTL_BOLD_FONT);
4608 }
4609
4610 void RichTextLabel::push_bold_italics() {
4611 ERR_FAIL_COND(theme_cache.bold_italics_font.is_null());
4612
4613 _push_def_font(RTL_BOLD_ITALICS_FONT);
4614 }
4615
4616 void RichTextLabel::push_italics() {
4617 ERR_FAIL_COND(theme_cache.italics_font.is_null());
4618
4619 ItemFont *item_font = _find_font(current);
4620 _push_def_font((item_font && item_font->def_font == RTL_BOLD_FONT) ? RTL_BOLD_ITALICS_FONT : RTL_ITALICS_FONT);
4621 }
4622
4623 void RichTextLabel::push_mono() {
4624 ERR_FAIL_COND(theme_cache.mono_font.is_null());
4625
4626 _push_def_font(RTL_MONO_FONT);
4627 }
4628
4629 void RichTextLabel::push_font_size(int p_font_size) {
4630 _stop_thread();
4631 MutexLock data_lock(data_mutex);
4632
4633 ERR_FAIL_COND(current->type == ITEM_TABLE);
4634 ItemFontSize *item = memnew(ItemFontSize);
4635 item->owner = get_instance_id();
4636 item->rid = items.make_rid(item);
4637 item->font_size = p_font_size;
4638 _add_item(item, true);
4639 }
4640
4641 void RichTextLabel::push_outline_size(int p_ol_size) {
4642 _stop_thread();
4643 MutexLock data_lock(data_mutex);
4644
4645 ERR_FAIL_COND(current->type == ITEM_TABLE);
4646 ItemOutlineSize *item = memnew(ItemOutlineSize);
4647 item->owner = get_instance_id();
4648 item->rid = items.make_rid(item);
4649 item->outline_size = p_ol_size;
4650 _add_item(item, true);
4651 }
4652
4653 void RichTextLabel::push_color(const Color &p_color) {
4654 _stop_thread();
4655 MutexLock data_lock(data_mutex);
4656
4657 ERR_FAIL_COND(current->type == ITEM_TABLE);
4658 ItemColor *item = memnew(ItemColor);
4659 item->owner = get_instance_id();
4660 item->rid = items.make_rid(item);
4661 item->color = p_color;
4662 _add_item(item, true);
4663 }
4664
4665 void RichTextLabel::push_outline_color(const Color &p_color) {
4666 _stop_thread();
4667 MutexLock data_lock(data_mutex);
4668
4669 ERR_FAIL_COND(current->type == ITEM_TABLE);
4670 ItemOutlineColor *item = memnew(ItemOutlineColor);
4671 item->owner = get_instance_id();
4672 item->rid = items.make_rid(item);
4673 item->color = p_color;
4674 _add_item(item, true);
4675 }
4676
4677 void RichTextLabel::push_underline(const Color &p_color) {
4678 _stop_thread();
4679 MutexLock data_lock(data_mutex);
4680
4681 ERR_FAIL_COND(current->type == ITEM_TABLE);
4682 ItemUnderline *item = memnew(ItemUnderline);
4683 item->color = p_color;
4684 item->owner = get_instance_id();
4685 item->rid = items.make_rid(item);
4686
4687 _add_item(item, true);
4688 }
4689
4690 void RichTextLabel::push_strikethrough(const Color &p_color) {
4691 _stop_thread();
4692 MutexLock data_lock(data_mutex);
4693
4694 ERR_FAIL_COND(current->type == ITEM_TABLE);
4695 ItemStrikethrough *item = memnew(ItemStrikethrough);
4696 item->color = p_color;
4697 item->owner = get_instance_id();
4698 item->rid = items.make_rid(item);
4699
4700 _add_item(item, true);
4701 }
4702
4703 void RichTextLabel::push_paragraph(HorizontalAlignment p_alignment, Control::TextDirection p_direction, const String &p_language, TextServer::StructuredTextParser p_st_parser, BitField<TextServer::JustificationFlag> p_jst_flags, const PackedFloat32Array &p_tab_stops) {
4704 _stop_thread();
4705 MutexLock data_lock(data_mutex);
4706
4707 ERR_FAIL_COND(current->type == ITEM_TABLE);
4708
4709 ItemParagraph *item = memnew(ItemParagraph);
4710 item->owner = get_instance_id();
4711 item->rid = items.make_rid(item);
4712 item->alignment = p_alignment;
4713 item->direction = p_direction;
4714 item->language = p_language;
4715 item->st_parser = p_st_parser;
4716 item->jst_flags = p_jst_flags;
4717 item->tab_stops = p_tab_stops;
4718 _add_item(item, true, true);
4719 }
4720
4721 void RichTextLabel::push_indent(int p_level) {
4722 _stop_thread();
4723 MutexLock data_lock(data_mutex);
4724
4725 ERR_FAIL_COND(current->type == ITEM_TABLE);
4726 ERR_FAIL_COND(p_level < 0);
4727
4728 ItemIndent *item = memnew(ItemIndent);
4729 item->owner = get_instance_id();
4730 item->rid = items.make_rid(item);
4731 item->level = p_level;
4732 _add_item(item, true, true);
4733 }
4734
4735 void RichTextLabel::push_list(int p_level, ListType p_list, bool p_capitalize, const String &p_bullet) {
4736 _stop_thread();
4737 MutexLock data_lock(data_mutex);
4738
4739 ERR_FAIL_COND(current->type == ITEM_TABLE);
4740 ERR_FAIL_COND(p_level < 0);
4741
4742 ItemList *item = memnew(ItemList);
4743 item->owner = get_instance_id();
4744 item->rid = items.make_rid(item);
4745 item->list_type = p_list;
4746 item->level = p_level;
4747 item->capitalize = p_capitalize;
4748 item->bullet = p_bullet;
4749 _add_item(item, true, true);
4750 }
4751
4752 void RichTextLabel::push_meta(const Variant &p_meta, MetaUnderline p_underline_mode, const String &p_tooltip) {
4753 _stop_thread();
4754 MutexLock data_lock(data_mutex);
4755
4756 ERR_FAIL_COND(current->type == ITEM_TABLE);
4757 ItemMeta *item = memnew(ItemMeta);
4758 item->owner = get_instance_id();
4759 item->rid = items.make_rid(item);
4760 item->meta = p_meta;
4761 item->underline = p_underline_mode;
4762 item->tooltip = p_tooltip;
4763 _add_item(item, true);
4764 }
4765
4766 void RichTextLabel::push_language(const String &p_language) {
4767 _stop_thread();
4768 MutexLock data_lock(data_mutex);
4769
4770 ERR_FAIL_COND(current->type == ITEM_TABLE);
4771 ItemLanguage *item = memnew(ItemLanguage);
4772 item->owner = get_instance_id();
4773 item->rid = items.make_rid(item);
4774 item->language = p_language;
4775 _add_item(item, true);
4776 }
4777
4778 void RichTextLabel::push_hint(const String &p_string) {
4779 _stop_thread();
4780 MutexLock data_lock(data_mutex);
4781
4782 ERR_FAIL_COND(current->type == ITEM_TABLE);
4783 ItemHint *item = memnew(ItemHint);
4784 item->owner = get_instance_id();
4785 item->rid = items.make_rid(item);
4786 item->description = p_string;
4787 _add_item(item, true);
4788 }
4789
4790 void RichTextLabel::push_table(int p_columns, InlineAlignment p_alignment, int p_align_to_row, const String &p_alt_text) {
4791 _stop_thread();
4792 MutexLock data_lock(data_mutex);
4793
4794 ERR_FAIL_COND(current->type == ITEM_TABLE);
4795 ERR_FAIL_COND(p_columns < 1);
4796 ItemTable *item = memnew(ItemTable);
4797 item->owner = get_instance_id();
4798 item->rid = items.make_rid(item);
4799 item->name = p_alt_text;
4800 item->columns.resize(p_columns);
4801 item->total_width = 0;
4802 item->inline_align = p_alignment;
4803 item->align_to_row = p_align_to_row;
4804 for (int i = 0; i < (int)item->columns.size(); i++) {
4805 item->columns[i].expand = false;
4806 item->columns[i].shrink = true;
4807 item->columns[i].expand_ratio = 1;
4808 }
4809 _add_item(item, true, false);
4810 }
4811
4812 void RichTextLabel::push_fade(int p_start_index, int p_length) {
4813 _stop_thread();
4814 MutexLock data_lock(data_mutex);
4815
4816 ERR_FAIL_COND(current->type == ITEM_TABLE);
4817 ItemFade *item = memnew(ItemFade);
4818 item->owner = get_instance_id();
4819 item->rid = items.make_rid(item);
4820 item->starting_index = p_start_index;
4821 item->length = p_length;
4822 _add_item(item, true);
4823 }
4824
4825 void RichTextLabel::push_shake(int p_strength = 10, float p_rate = 24.0f, bool p_connected = true) {
4826 _stop_thread();
4827 MutexLock data_lock(data_mutex);
4828
4829 ERR_FAIL_COND(current->type == ITEM_TABLE);
4830 ItemShake *item = memnew(ItemShake);
4831 item->owner = get_instance_id();
4832 item->rid = items.make_rid(item);
4833 item->strength = p_strength;
4834 item->rate = p_rate;
4835 item->connected = p_connected;
4836 _add_item(item, true);
4837 }
4838
4839 void RichTextLabel::push_wave(float p_frequency = 1.0f, float p_amplitude = 10.0f, bool p_connected = true) {
4840 _stop_thread();
4841 MutexLock data_lock(data_mutex);
4842
4843 ERR_FAIL_COND(current->type == ITEM_TABLE);
4844 ItemWave *item = memnew(ItemWave);
4845 item->owner = get_instance_id();
4846 item->rid = items.make_rid(item);
4847 item->frequency = p_frequency;
4848 item->amplitude = p_amplitude;
4849 item->connected = p_connected;
4850 _add_item(item, true);
4851 }
4852
4853 void RichTextLabel::push_tornado(float p_frequency = 1.0f, float p_radius = 10.0f, bool p_connected = true) {
4854 _stop_thread();
4855 MutexLock data_lock(data_mutex);
4856
4857 ERR_FAIL_COND(current->type == ITEM_TABLE);
4858 ItemTornado *item = memnew(ItemTornado);
4859 item->owner = get_instance_id();
4860 item->rid = items.make_rid(item);
4861 item->frequency = p_frequency;
4862 item->radius = p_radius;
4863 item->connected = p_connected;
4864 _add_item(item, true);
4865 }
4866
4867 void RichTextLabel::push_rainbow(float p_saturation, float p_value, float p_frequency, float p_speed) {
4868 _stop_thread();
4869 MutexLock data_lock(data_mutex);
4870
4871 ERR_FAIL_COND(current->type == ITEM_TABLE);
4872 ItemRainbow *item = memnew(ItemRainbow);
4873 item->owner = get_instance_id();
4874 item->rid = items.make_rid(item);
4875 item->speed = p_speed;
4876 item->frequency = p_frequency;
4877 item->saturation = p_saturation;
4878 item->value = p_value;
4879 _add_item(item, true);
4880 }
4881
4882 void RichTextLabel::push_pulse(const Color &p_color, float p_frequency, float p_ease) {
4883 _stop_thread();
4884 MutexLock data_lock(data_mutex);
4885
4886 ItemPulse *item = memnew(ItemPulse);
4887 item->owner = get_instance_id();
4888 item->rid = items.make_rid(item);
4889 item->color = p_color;
4890 item->frequency = p_frequency;
4891 item->ease = p_ease;
4892 _add_item(item, true);
4893 }
4894
4895 void RichTextLabel::push_bgcolor(const Color &p_color) {
4896 _stop_thread();
4897 MutexLock data_lock(data_mutex);
4898
4899 ERR_FAIL_COND(current->type == ITEM_TABLE);
4900 ItemBGColor *item = memnew(ItemBGColor);
4901 item->owner = get_instance_id();
4902 item->rid = items.make_rid(item);
4903 item->color = p_color;
4904 _add_item(item, true);
4905 }
4906
4907 void RichTextLabel::push_fgcolor(const Color &p_color) {
4908 _stop_thread();
4909 MutexLock data_lock(data_mutex);
4910
4911 ERR_FAIL_COND(current->type == ITEM_TABLE);
4912 ItemFGColor *item = memnew(ItemFGColor);
4913 item->owner = get_instance_id();
4914 item->rid = items.make_rid(item);
4915 item->color = p_color;
4916 _add_item(item, true);
4917 }
4918
4919 void RichTextLabel::push_customfx(Ref<RichTextEffect> p_custom_effect, Dictionary p_environment) {
4920 _stop_thread();
4921 MutexLock data_lock(data_mutex);
4922
4923 ERR_FAIL_COND(current->type == ITEM_TABLE);
4924 ItemCustomFX *item = memnew(ItemCustomFX);
4925 item->owner = get_instance_id();
4926 item->rid = items.make_rid(item);
4927 item->custom_effect = p_custom_effect;
4928 item->char_fx_transform->environment = p_environment;
4929 _add_item(item, true);
4930
4931 set_process_internal(true);
4932 }
4933
4934 void RichTextLabel::push_context() {
4935 _stop_thread();
4936 MutexLock data_lock(data_mutex);
4937
4938 ERR_FAIL_COND(current->type == ITEM_TABLE);
4939 ItemContext *item = memnew(ItemContext);
4940 item->owner = get_instance_id();
4941 item->rid = items.make_rid(item);
4942 _add_item(item, true);
4943 }
4944
4945 void RichTextLabel::set_table_column_expand(int p_column, bool p_expand, int p_ratio, bool p_shrink) {
4946 _stop_thread();
4947 MutexLock data_lock(data_mutex);
4948
4949 ERR_FAIL_COND(current->type != ITEM_TABLE);
4950
4951 ItemTable *table = static_cast<ItemTable *>(current);
4952 ERR_FAIL_INDEX(p_column, (int)table->columns.size());
4953 table->columns[p_column].expand = p_expand;
4954 table->columns[p_column].shrink = p_shrink;
4955 table->columns[p_column].expand_ratio = p_ratio;
4956 }
4957
4958 void RichTextLabel::set_table_column_name(int p_column, const String &p_name) {
4959 _stop_thread();
4960 MutexLock data_lock(data_mutex);
4961
4962 ERR_FAIL_COND(current->type != ITEM_TABLE);
4963
4964 ItemTable *table = static_cast<ItemTable *>(current);
4965 ERR_FAIL_INDEX(p_column, (int)table->columns.size());
4966 table->columns[p_column].name = p_name;
4967 }
4968
4969 void RichTextLabel::set_cell_row_background_color(const Color &p_odd_row_bg, const Color &p_even_row_bg) {
4970 _stop_thread();
4971 MutexLock data_lock(data_mutex);
4972
4973 ERR_FAIL_COND(current->type != ITEM_FRAME);
4974
4975 ItemFrame *cell = static_cast<ItemFrame *>(current);
4976 ERR_FAIL_COND(!cell->cell);
4977 cell->odd_row_bg = p_odd_row_bg;
4978 cell->even_row_bg = p_even_row_bg;
4979 }
4980
4981 void RichTextLabel::set_cell_border_color(const Color &p_color) {
4982 _stop_thread();
4983 MutexLock data_lock(data_mutex);
4984
4985 ERR_FAIL_COND(current->type != ITEM_FRAME);
4986
4987 ItemFrame *cell = static_cast<ItemFrame *>(current);
4988 ERR_FAIL_COND(!cell->cell);
4989 cell->border = p_color;
4990 }
4991
4992 void RichTextLabel::set_cell_size_override(const Size2 &p_min_size, const Size2 &p_max_size) {
4993 _stop_thread();
4994 MutexLock data_lock(data_mutex);
4995
4996 ERR_FAIL_COND(current->type != ITEM_FRAME);
4997
4998 ItemFrame *cell = static_cast<ItemFrame *>(current);
4999 ERR_FAIL_COND(!cell->cell);
5000 cell->min_size_over = p_min_size;
5001 cell->max_size_over = p_max_size;
5002 }
5003
5004 void RichTextLabel::set_cell_padding(const Rect2 &p_padding) {
5005 _stop_thread();
5006 MutexLock data_lock(data_mutex);
5007
5008 ERR_FAIL_COND(current->type != ITEM_FRAME);
5009
5010 ItemFrame *cell = static_cast<ItemFrame *>(current);
5011 ERR_FAIL_COND(!cell->cell);
5012 cell->padding = p_padding;
5013 }
5014
5015 void RichTextLabel::push_cell() {
5016 _stop_thread();
5017 MutexLock data_lock(data_mutex);
5018
5019 ERR_FAIL_COND(current->type != ITEM_TABLE);
5020
5021 ItemFrame *item = memnew(ItemFrame);
5022 item->owner = get_instance_id();
5023 item->rid = items.make_rid(item);
5024 item->parent_frame = current_frame;
5025 _add_item(item, true);
5026 current_frame = item;
5027 item->cell = true;
5028 item->lines.resize(1);
5029 item->lines[0].from = nullptr;
5030 item->first_invalid_line.store(0); // parent frame last line ???
5031 queue_accessibility_update();
5032 }
5033
5034 int RichTextLabel::get_current_table_column() const {
5035 ERR_FAIL_COND_V(current->type != ITEM_TABLE, -1);
5036
5037 ItemTable *table = static_cast<ItemTable *>(current);
5038 return table->subitems.size() % table->columns.size();
5039 }
5040
5041 void RichTextLabel::pop() {
5042 _stop_thread();
5043 MutexLock data_lock(data_mutex);
5044
5045 ERR_FAIL_NULL(current->parent);
5046
5047 if (current->type == ITEM_FRAME) {
5048 current_frame = static_cast<ItemFrame *>(current)->parent_frame;
5049 }
5050 current = current->parent;
5051 if (!parsing_bbcode.load() && !tag_stack.is_empty()) {
5052 tag_stack.pop_back();
5053 }
5054 }
5055
5056 void RichTextLabel::pop_context() {
5057 _stop_thread();
5058 MutexLock data_lock(data_mutex);
5059
5060 ERR_FAIL_NULL(current->parent);
5061
5062 while (current->parent && current != main) {
5063 if (current->type == ITEM_FRAME) {
5064 current_frame = static_cast<ItemFrame *>(current)->parent_frame;
5065 } else if (current->type == ITEM_CONTEXT) {
5066 if (!parsing_bbcode.load() && !tag_stack.is_empty()) {
5067 tag_stack.pop_back();
5068 }
5069 current = current->parent;
5070 return;
5071 }
5072 if (!parsing_bbcode.load() && !tag_stack.is_empty()) {
5073 tag_stack.pop_back();
5074 }
5075 current = current->parent;
5076 }
5077 }
5078
5079 void RichTextLabel::pop_all() {
5080 _stop_thread();
5081 MutexLock data_lock(data_mutex);
5082
5083 current = main;
5084 current_frame = main;
5085 }
5086
5087 void RichTextLabel::clear() {
5088 _stop_thread();
5089 set_process_internal(false);
5090 MutexLock data_lock(data_mutex);
5091
5092 stack_externally_modified = false;
5093
5094 tag_stack.clear();
5095 main->_clear_children();
5096 current = main;
5097 current_frame = main;
5098 main->lines.clear();
5099 main->lines.resize(1);
5100 main->first_invalid_line.store(0);
5101 _invalidate_accessibility();
5102
5103 keyboard_focus_frame = nullptr;
5104 keyboard_focus_line = 0;
5105 keyboard_focus_item = nullptr;
5106
5107 selection.click_frame = nullptr;
5108 selection.click_item = nullptr;
5109 deselect();
5110
5111 current_idx = 1;
5112 current_char_ofs = 0;
5113 if (scroll_follow) {
5114 scroll_following = true;
5115 }
5116
5117 if (fit_content) {
5118 update_minimum_size();
5119 }
5120 queue_accessibility_update();
5121 update_configuration_warnings();
5122 }
5123
5124 void RichTextLabel::set_tab_size(int p_spaces) {
5125 if (tab_size == p_spaces) {
5126 return;
5127 }
5128
5129 _stop_thread();
5130
5131 tab_size = p_spaces;
5132 main->first_resized_line.store(0);
5133 _invalidate_accessibility();
5134 queue_accessibility_update();
5135 queue_redraw();
5136 }
5137
5138 int RichTextLabel::get_tab_size() const {
5139 return tab_size;
5140 }
5141
5142 void RichTextLabel::set_fit_content(bool p_enabled) {
5143 if (p_enabled == fit_content) {
5144 return;
5145 }
5146
5147 fit_content = p_enabled;
5148 update_minimum_size();
5149 }
5150
5151 bool RichTextLabel::is_fit_content_enabled() const {
5152 return fit_content;
5153 }
5154
5155 void RichTextLabel::set_meta_underline(bool p_underline) {
5156 if (underline_meta == p_underline) {
5157 return;
5158 }
5159
5160 underline_meta = p_underline;
5161 queue_redraw();
5162 }
5163
5164 bool RichTextLabel::is_meta_underlined() const {
5165 return underline_meta;
5166 }
5167
5168 void RichTextLabel::set_hint_underline(bool p_underline) {
5169 underline_hint = p_underline;
5170 queue_redraw();
5171 }
5172
5173 bool RichTextLabel::is_hint_underlined() const {
5174 return underline_hint;
5175 }
5176
5177 void RichTextLabel::set_offset(int p_pixel) {
5178 vscroll->set_value(p_pixel);
5179 queue_accessibility_update();
5180 }
5181
5182 void RichTextLabel::set_scroll_active(bool p_active) {
5183 if (scroll_active == p_active) {
5184 return;
5185 }
5186
5187 scroll_active = p_active;
5188 vscroll->set_drag_node_enabled(p_active);
5189 vscroll->set_visible(p_active);
5190 _apply_translation(); // without this, RichLabelText is not updated in the editor/game.
5191 queue_redraw();
5192 }
5193
5194 bool RichTextLabel::is_scroll_active() const {
5195 return scroll_active;
5196 }
5197
5198 void RichTextLabel::set_scroll_follow(bool p_follow) {
5199 scroll_follow = p_follow;
5200 if (!vscroll->is_visible_in_tree() || vscroll->get_value() > (vscroll->get_max() - vscroll->get_page() - 1)) {
5201 scroll_following = true;
5202 }
5203 }
5204
5205 bool RichTextLabel::is_scroll_following() const {
5206 return scroll_follow;
5207 }
5208
5209 void RichTextLabel::_update_follow_vc() {
5210 if (!scroll_follow_visible_characters) {
5211 return;
5212 }
5213 int vc = (visible_characters < 0 ? get_total_character_count() : MIN(visible_characters, get_total_character_count())) - 1;
5214 int voff = get_character_line(vc) + 1;
5215 if (voff <= get_line_count() - 1) {
5216 follow_vc_pos = get_line_offset(voff) - _get_text_rect().size.y;
5217 } else {
5218 follow_vc_pos = vscroll->get_max();
5219 }
5220 vscroll->scroll_to(follow_vc_pos);
5221 }
5222
5223 void RichTextLabel::set_scroll_follow_visible_characters(bool p_follow) {
5224 if (scroll_follow_visible_characters != p_follow) {
5225 scroll_follow_visible_characters = p_follow;
5226 _update_follow_vc();
5227 }
5228 }
5229
5230 bool RichTextLabel::is_scroll_following_visible_characters() const {
5231 return scroll_follow_visible_characters;
5232 }
5233
5234 void RichTextLabel::parse_bbcode(const String &p_bbcode) {
5235 clear();
5236 append_text(p_bbcode);
5237 }
5238
5239 String RichTextLabel::_get_tag_value(const String &p_tag) {
5240 return p_tag.substr(p_tag.find_char('=') + 1);
5241 }
5242
5243 int RichTextLabel::_find_unquoted(const String &p_src, char32_t p_chr, int p_from) {
5244 if (p_from < 0) {
5245 return -1;
5246 }
5247
5248 const int len = p_src.length();
5249 if (len == 0) {
5250 return -1;
5251 }
5252
5253 const char32_t *src = p_src.get_data();
5254 bool in_single_quote = false;
5255 bool in_double_quote = false;
5256 for (int i = p_from; i < len; i++) {
5257 if (in_double_quote) {
5258 if (src[i] == '"') {
5259 in_double_quote = false;
5260 }
5261 } else if (in_single_quote) {
5262 if (src[i] == '\'') {
5263 in_single_quote = false;
5264 }
5265 } else {
5266 if (src[i] == '"') {
5267 in_double_quote = true;
5268 } else if (src[i] == '\'') {
5269 in_single_quote = true;
5270 } else if (src[i] == p_chr) {
5271 return i;
5272 }
5273 }
5274 }
5275
5276 return -1;
5277 }
5278
5279 Vector<String> RichTextLabel::_split_unquoted(const String &p_src, char32_t p_splitter) {
5280 Vector<String> ret;
5281
5282 if (p_src.is_empty()) {
5283 return ret;
5284 }
5285
5286 int from = 0;
5287 int len = p_src.length();
5288
5289 while (true) {
5290 int end = _find_unquoted(p_src, p_splitter, from);
5291 if (end < 0) {
5292 end = len;
5293 }
5294 if (end > from) {
5295 ret.push_back(p_src.substr(from, end - from));
5296 }
5297 if (end == len) {
5298 break;
5299 }
5300
5301 from = end + 1;
5302 }
5303
5304 return ret;
5305 }
5306
5307 void RichTextLabel::append_text(const String &p_bbcode) {
5308 _stop_thread();
5309 MutexLock data_lock(data_mutex);
5310
5311 parsing_bbcode.store(true);
5312
5313 int pos = 0;
5314
5315 bool in_bold = false;
5316 bool in_italics = false;
5317 bool after_list_open_tag = false;
5318 bool after_list_close_tag = false;
5319
5320 String bbcode = p_bbcode.replace("\r\n", "\n");
5321
5322 while (pos <= bbcode.length()) {
5323 int brk_pos = bbcode.find_char('[', pos);
5324
5325 if (brk_pos < 0) {
5326 brk_pos = bbcode.length();
5327 }
5328
5329 String txt = brk_pos > pos ? bbcode.substr(pos, brk_pos - pos) : "";
5330
5331 // Trim the first newline character, it may be added later as needed.
5332 if (after_list_close_tag || after_list_open_tag) {
5333 txt = txt.trim_prefix("\n");
5334 }
5335
5336 if (brk_pos == bbcode.length()) {
5337 // For tags that are not properly closed.
5338 if (txt.is_empty() && after_list_open_tag) {
5339 txt = "\n";
5340 }
5341
5342 if (!txt.is_empty()) {
5343 add_text(txt);
5344 }
5345 break; //nothing else to add
5346 }
5347
5348 int brk_end = _find_unquoted(bbcode, ']', brk_pos + 1);
5349
5350 if (brk_end == -1) {
5351 //no close, add the rest
5352 txt += bbcode.substr(brk_pos);
5353 add_text(txt);
5354 break;
5355 }
5356
5357 String tag = bbcode.substr(brk_pos + 1, brk_end - brk_pos - 1);
5358 Vector<String> split_tag_block = _split_unquoted(tag, ' ');
5359
5360 // Find optional parameters.
5361 String bbcode_name;
5362 typedef HashMap<String, String> OptionMap;
5363 OptionMap bbcode_options;
5364 if (!split_tag_block.is_empty()) {
5365 bbcode_name = split_tag_block[0];
5366 for (int i = 1; i < split_tag_block.size(); i++) {
5367 const String &expr = split_tag_block[i];
5368 int value_pos = expr.find_char('=');
5369 if (value_pos > -1) {
5370 bbcode_options[expr.substr(0, value_pos)] = expr.substr(value_pos + 1).unquote();
5371 }
5372 }
5373 } else {
5374 bbcode_name = tag;
5375 }
5376
5377 // Find main parameter.
5378 String bbcode_value;
5379 int main_value_pos = bbcode_name.find_char('=');
5380 if (main_value_pos > -1) {
5381 bbcode_value = bbcode_name.substr(main_value_pos + 1);
5382 bbcode_name = bbcode_name.substr(0, main_value_pos);
5383 }
5384
5385 if (tag.begins_with("/") && tag_stack.size()) {
5386 bool tag_ok = tag_stack.size() && tag_stack.front()->get() == tag.substr(1);
5387
5388 if (tag_stack.front()->get() == "b") {
5389 in_bold = false;
5390 }
5391 if (tag_stack.front()->get() == "i") {
5392 in_italics = false;
5393 }
5394 if ((tag_stack.front()->get() == "indent") || (tag_stack.front()->get() == "ol") || (tag_stack.front()->get() == "ul")) {
5395 current_frame->indent_level--;
5396 }
5397
5398 if (!tag_ok) {
5399 txt += "[" + tag;
5400 add_text(txt);
5401 after_list_open_tag = false;
5402 after_list_close_tag = false;
5403 pos = brk_end;
5404 continue;
5405 }
5406
5407 if (txt.is_empty() && after_list_open_tag) {
5408 txt = "\n"; // Make empty list have at least one item.
5409 }
5410 after_list_open_tag = false;
5411
5412 if (tag == "/ol" || tag == "/ul") {
5413 if (!txt.is_empty()) {
5414 // Make sure text ends with a newline character, that is, the last item
5415 // will wrap at the end of block.
5416 if (!txt.ends_with("\n")) {
5417 txt += "\n";
5418 }
5419 } else if (!after_list_close_tag) {
5420 txt = "\n"; // Make the innermost list item wrap at the end of lists.
5421 }
5422 after_list_close_tag = true;
5423 } else {
5424 after_list_close_tag = false;
5425 }
5426
5427 if (!txt.is_empty()) {
5428 add_text(txt);
5429 }
5430
5431 tag_stack.pop_front();
5432 pos = brk_end + 1;
5433 if (tag != "/img" && tag != "/dropcap") {
5434 pop();
5435 }
5436 continue;
5437 }
5438
5439 if (tag == "ol" || tag.begins_with("ol ") || tag == "ul" || tag.begins_with("ul ")) {
5440 if (txt.is_empty() && after_list_open_tag) {
5441 txt = "\n"; // Make each list have at least one item at the beginning.
5442 }
5443 after_list_open_tag = true;
5444 } else {
5445 after_list_open_tag = false;
5446 }
5447 if (!txt.is_empty()) {
5448 add_text(txt);
5449 }
5450 after_list_close_tag = false;
5451
5452 if (tag == "b") {
5453 //use bold font
5454 in_bold = true;
5455 if (in_italics) {
5456 _push_def_font(RTL_BOLD_ITALICS_FONT);
5457 } else {
5458 _push_def_font(RTL_BOLD_FONT);
5459 }
5460 pos = brk_end + 1;
5461 tag_stack.push_front(tag);
5462 } else if (tag == "i") {
5463 //use italics font
5464 in_italics = true;
5465 if (in_bold) {
5466 _push_def_font(RTL_BOLD_ITALICS_FONT);
5467 } else {
5468 _push_def_font(RTL_ITALICS_FONT);
5469 }
5470 pos = brk_end + 1;
5471 tag_stack.push_front(tag);
5472 } else if (tag == "code") {
5473 //use monospace font
5474 _push_def_font(RTL_MONO_FONT);
5475 pos = brk_end + 1;
5476 tag_stack.push_front(tag);
5477 } else if (tag.begins_with("table=")) {
5478 Vector<String> subtag = _split_unquoted(_get_tag_value(tag), U',');
5479 _normalize_subtags(subtag);
5480
5481 int columns = (subtag.is_empty()) ? 1 : subtag[0].to_int();
5482 if (columns < 1) {
5483 columns = 1;
5484 }
5485
5486 int alignment = INLINE_ALIGNMENT_TOP;
5487 if (subtag.size() > 2) {
5488 if (subtag[1] == "top" || subtag[1] == "t") {
5489 alignment = INLINE_ALIGNMENT_TOP_TO;
5490 } else if (subtag[1] == "center" || subtag[1] == "c") {
5491 alignment = INLINE_ALIGNMENT_CENTER_TO;
5492 } else if (subtag[1] == "baseline" || subtag[1] == "l") {
5493 alignment = INLINE_ALIGNMENT_BASELINE_TO;
5494 } else if (subtag[1] == "bottom" || subtag[1] == "b") {
5495 alignment = INLINE_ALIGNMENT_BOTTOM_TO;
5496 }
5497 if (subtag[2] == "top" || subtag[2] == "t") {
5498 alignment |= INLINE_ALIGNMENT_TO_TOP;
5499 } else if (subtag[2] == "center" || subtag[2] == "c") {
5500 alignment |= INLINE_ALIGNMENT_TO_CENTER;
5501 } else if (subtag[2] == "baseline" || subtag[2] == "l") {
5502 alignment |= INLINE_ALIGNMENT_TO_BASELINE;
5503 } else if (subtag[2] == "bottom" || subtag[2] == "b") {
5504 alignment |= INLINE_ALIGNMENT_TO_BOTTOM;
5505 }
5506 } else if (subtag.size() > 1) {
5507 if (subtag[1] == "top" || subtag[1] == "t") {
5508 alignment = INLINE_ALIGNMENT_TOP;
5509 } else if (subtag[1] == "center" || subtag[1] == "c") {
5510 alignment = INLINE_ALIGNMENT_CENTER;
5511 } else if (subtag[1] == "bottom" || subtag[1] == "b") {
5512 alignment = INLINE_ALIGNMENT_BOTTOM;
5513 }
5514 }
5515 int row = -1;
5516 if (subtag.size() > 3) {
5517 row = subtag[3].to_int();
5518 }
5519
5520 OptionMap::Iterator alt_text_option = bbcode_options.find("name");
5521 String alt_text;
5522 if (alt_text_option) {
5523 alt_text = alt_text_option->value;
5524 }
5525
5526 push_table(columns, (InlineAlignment)alignment, row, alt_text);
5527 pos = brk_end + 1;
5528 tag_stack.push_front("table");
5529 } else if (tag == "cell") {
5530 push_cell();
5531 pos = brk_end + 1;
5532 tag_stack.push_front(tag);
5533 } else if (tag.begins_with("cell=")) {
5534 int ratio = _get_tag_value(tag).to_int();
5535 if (ratio < 1) {
5536 ratio = 1;
5537 }
5538
5539 set_table_column_expand(get_current_table_column(), true, ratio);
5540 push_cell();
5541
5542 pos = brk_end + 1;
5543 tag_stack.push_front("cell");
5544 } else if (tag.begins_with("cell ")) {
5545 bool shrink = true;
5546 OptionMap::Iterator shrink_option = bbcode_options.find("shrink");
5547 if (shrink_option) {
5548 shrink = (shrink_option->value == "true");
5549 }
5550
5551 OptionMap::Iterator expand_option = bbcode_options.find("expand");
5552 if (expand_option) {
5553 int ratio = expand_option->value.to_int();
5554 if (ratio < 1) {
5555 ratio = 1;
5556 }
5557 set_table_column_expand(get_current_table_column(), true, ratio, shrink);
5558 }
5559
5560 push_cell();
5561 const Color fallback_color = Color(0, 0, 0, 0);
5562
5563 OptionMap::Iterator border_option = bbcode_options.find("border");
5564 if (border_option) {
5565 Color color = Color::from_string(border_option->value, fallback_color);
5566 set_cell_border_color(color);
5567 }
5568 OptionMap::Iterator bg_option = bbcode_options.find("bg");
5569 if (bg_option) {
5570 Vector<String> subtag_b = _split_unquoted(bg_option->value, U',');
5571 _normalize_subtags(subtag_b);
5572
5573 if (subtag_b.size() == 2) {
5574 Color color1 = Color::from_string(subtag_b[0], fallback_color);
5575 Color color2 = Color::from_string(subtag_b[1], fallback_color);
5576 set_cell_row_background_color(color1, color2);
5577 }
5578 if (subtag_b.size() == 1) {
5579 Color color1 = Color::from_string(bg_option->value, fallback_color);
5580 set_cell_row_background_color(color1, color1);
5581 }
5582 }
5583 OptionMap::Iterator padding_option = bbcode_options.find("padding");
5584 if (padding_option) {
5585 Vector<String> subtag_b = _split_unquoted(padding_option->value, U',');
5586 _normalize_subtags(subtag_b);
5587
5588 if (subtag_b.size() == 4) {
5589 set_cell_padding(Rect2(subtag_b[0].to_float(), subtag_b[1].to_float(), subtag_b[2].to_float(), subtag_b[3].to_float()));
5590 }
5591 }
5592
5593 pos = brk_end + 1;
5594 tag_stack.push_front("cell");
5595 } else if (tag == "u") {
5596 push_underline();
5597 pos = brk_end + 1;
5598 tag_stack.push_front(tag);
5599 } else if (tag.begins_with("u ")) {
5600 Color color = Color(0, 0, 0, 0);
5601 OptionMap::Iterator color_option = bbcode_options.find("color");
5602 if (color_option) {
5603 color = Color::from_string(color_option->value, color);
5604 }
5605
5606 push_underline(color);
5607 pos = brk_end + 1;
5608 tag_stack.push_front("u");
5609 } else if (tag == "s") {
5610 push_strikethrough();
5611 pos = brk_end + 1;
5612 tag_stack.push_front(tag);
5613 } else if (tag.begins_with("s ")) {
5614 Color color = Color(0, 0, 0, 0);
5615 OptionMap::Iterator color_option = bbcode_options.find("color");
5616 if (color_option) {
5617 color = Color::from_string(color_option->value, color);
5618 }
5619
5620 push_strikethrough(color);
5621 pos = brk_end + 1;
5622 tag_stack.push_front("s");
5623 } else if (tag.begins_with("char=")) {
5624 int32_t char_code = _get_tag_value(tag).hex_to_int();
5625 add_text(String::chr(char_code));
5626 pos = brk_end + 1;
5627 } else if (tag == "lb") {
5628 add_text("[");
5629 pos = brk_end + 1;
5630 } else if (tag == "rb") {
5631 add_text("]");
5632 pos = brk_end + 1;
5633 } else if (tag == "lrm") {
5634 add_text(String::chr(0x200E));
5635 pos = brk_end + 1;
5636 } else if (tag == "rlm") {
5637 add_text(String::chr(0x200F));
5638 pos = brk_end + 1;
5639 } else if (tag == "lre") {
5640 add_text(String::chr(0x202A));
5641 pos = brk_end + 1;
5642 } else if (tag == "rle") {
5643 add_text(String::chr(0x202B));
5644 pos = brk_end + 1;
5645 } else if (tag == "lro") {
5646 add_text(String::chr(0x202D));
5647 pos = brk_end + 1;
5648 } else if (tag == "rlo") {
5649 add_text(String::chr(0x202E));
5650 pos = brk_end + 1;
5651 } else if (tag == "pdf") {
5652 add_text(String::chr(0x202C));
5653 pos = brk_end + 1;
5654 } else if (tag == "alm") {
5655 add_text(String::chr(0x061c));
5656 pos = brk_end + 1;
5657 } else if (tag == "lri") {
5658 add_text(String::chr(0x2066));
5659 pos = brk_end + 1;
5660 } else if (tag == "rli") {
5661 add_text(String::chr(0x2027));
5662 pos = brk_end + 1;
5663 } else if (tag == "fsi") {
5664 add_text(String::chr(0x2068));
5665 pos = brk_end + 1;
5666 } else if (tag == "pdi") {
5667 add_text(String::chr(0x2069));
5668 pos = brk_end + 1;
5669 } else if (tag == "zwj") {
5670 add_text(String::chr(0x200D));
5671 pos = brk_end + 1;
5672 } else if (tag == "zwnj") {
5673 add_text(String::chr(0x200C));
5674 pos = brk_end + 1;
5675 } else if (tag == "wj") {
5676 add_text(String::chr(0x2060));
5677 pos = brk_end + 1;
5678 } else if (tag == "shy") {
5679 add_text(String::chr(0x00AD));
5680 pos = brk_end + 1;
5681 } else if (tag == "center") {
5682 push_paragraph(HORIZONTAL_ALIGNMENT_CENTER, text_direction, language, st_parser, default_jst_flags, default_tab_stops);
5683 pos = brk_end + 1;
5684 tag_stack.push_front(tag);
5685 } else if (tag == "fill") {
5686 push_paragraph(HORIZONTAL_ALIGNMENT_FILL, text_direction, language, st_parser, default_jst_flags, default_tab_stops);
5687 pos = brk_end + 1;
5688 tag_stack.push_front(tag);
5689 } else if (tag == "left") {
5690 push_paragraph(HORIZONTAL_ALIGNMENT_LEFT, text_direction, language, st_parser, default_jst_flags, default_tab_stops);
5691 pos = brk_end + 1;
5692 tag_stack.push_front(tag);
5693 } else if (tag == "right") {
5694 push_paragraph(HORIZONTAL_ALIGNMENT_RIGHT, text_direction, language, st_parser, default_jst_flags, default_tab_stops);
5695 pos = brk_end + 1;
5696 tag_stack.push_front(tag);
5697 } else if (tag == "ul") {
5698 current_frame->indent_level++;
5699 push_list(current_frame->indent_level, LIST_DOTS, false);
5700 pos = brk_end + 1;
5701 tag_stack.push_front(tag);
5702 } else if (tag.begins_with("ul bullet=")) {
5703 String bullet = _get_tag_value(tag);
5704 current_frame->indent_level++;
5705 push_list(current_frame->indent_level, LIST_DOTS, false, bullet);
5706 pos = brk_end + 1;
5707 tag_stack.push_front("ul");
5708 } else if ((tag == "ol") || (tag == "ol type=1")) {
5709 current_frame->indent_level++;
5710 push_list(current_frame->indent_level, LIST_NUMBERS, false);
5711 pos = brk_end + 1;
5712 tag_stack.push_front("ol");
5713 } else if (tag == "ol type=a") {
5714 current_frame->indent_level++;
5715 push_list(current_frame->indent_level, LIST_LETTERS, false);
5716 pos = brk_end + 1;
5717 tag_stack.push_front("ol");
5718 } else if (tag == "ol type=A") {
5719 current_frame->indent_level++;
5720 push_list(current_frame->indent_level, LIST_LETTERS, true);
5721 pos = brk_end + 1;
5722 tag_stack.push_front("ol");
5723 } else if (tag == "ol type=i") {
5724 current_frame->indent_level++;
5725 push_list(current_frame->indent_level, LIST_ROMAN, false);
5726 pos = brk_end + 1;
5727 tag_stack.push_front("ol");
5728 } else if (tag == "ol type=I") {
5729 current_frame->indent_level++;
5730 push_list(current_frame->indent_level, LIST_ROMAN, true);
5731 pos = brk_end + 1;
5732 tag_stack.push_front("ol");
5733 } else if (tag == "indent") {
5734 current_frame->indent_level++;
5735 push_indent(current_frame->indent_level);
5736 pos = brk_end + 1;
5737 tag_stack.push_front(tag);
5738 } else if (tag.begins_with("lang=")) {
5739 String lang = _get_tag_value(tag).unquote();
5740 push_language(lang);
5741 pos = brk_end + 1;
5742 tag_stack.push_front("lang");
5743 } else if (tag == "br") {
5744 add_text("\r");
5745 pos = brk_end + 1;
5746 } else if (tag == "p") {
5747 push_paragraph(HORIZONTAL_ALIGNMENT_LEFT);
5748 pos = brk_end + 1;
5749 tag_stack.push_front("p");
5750 } else if (tag.begins_with("p ")) {
5751 HorizontalAlignment alignment = HORIZONTAL_ALIGNMENT_LEFT;
5752 Control::TextDirection dir = Control::TEXT_DIRECTION_INHERITED;
5753 String lang = language;
5754 PackedFloat32Array tab_stops = default_tab_stops;
5755 TextServer::StructuredTextParser st_parser_type = TextServer::STRUCTURED_TEXT_DEFAULT;
5756 BitField<TextServer::JustificationFlag> jst_flags = default_jst_flags;
5757
5758 OptionMap::Iterator justification_flags_option = bbcode_options.find("justification_flags");
5759 if (!justification_flags_option) {
5760 justification_flags_option = bbcode_options.find("jst");
5761 }
5762 if (justification_flags_option) {
5763 Vector<String> subtag_b = _split_unquoted(justification_flags_option->value, U',');
5764 jst_flags = 0; // Clear flags.
5765 for (const String &E : subtag_b) {
5766 if (E == "kashida" || E == "k") {
5767 jst_flags.set_flag(TextServer::JUSTIFICATION_KASHIDA);
5768 } else if (E == "word" || E == "w") {
5769 jst_flags.set_flag(TextServer::JUSTIFICATION_WORD_BOUND);
5770 } else if (E == "trim" || E == "tr") {
5771 jst_flags.set_flag(TextServer::JUSTIFICATION_TRIM_EDGE_SPACES);
5772 } else if (E == "after_last_tab" || E == "lt") {
5773 jst_flags.set_flag(TextServer::JUSTIFICATION_AFTER_LAST_TAB);
5774 } else if (E == "skip_last" || E == "sl") {
5775 jst_flags.set_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE);
5776 } else if (E == "skip_last_with_chars" || E == "sv") {
5777 jst_flags.set_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE_WITH_VISIBLE_CHARS);
5778 } else if (E == "do_not_skip_single" || E == "ns") {
5779 jst_flags.set_flag(TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE);
5780 }
5781 }
5782 }
5783 OptionMap::Iterator tab_stops_option = bbcode_options.find("tab_stops");
5784 if (tab_stops_option) {
5785 Vector<String> splitters;
5786 splitters.push_back(",");
5787 splitters.push_back(";");
5788 tab_stops = tab_stops_option->value.split_floats_mk(splitters);
5789 }
5790 OptionMap::Iterator align_option = bbcode_options.find("align");
5791 if (align_option) {
5792 if (align_option->value == "l" || align_option->value == "left") {
5793 alignment = HORIZONTAL_ALIGNMENT_LEFT;
5794 } else if (align_option->value == "c" || align_option->value == "center") {
5795 alignment = HORIZONTAL_ALIGNMENT_CENTER;
5796 } else if (align_option->value == "r" || align_option->value == "right") {
5797 alignment = HORIZONTAL_ALIGNMENT_RIGHT;
5798 } else if (align_option->value == "f" || align_option->value == "fill") {
5799 alignment = HORIZONTAL_ALIGNMENT_FILL;
5800 }
5801 }
5802 OptionMap::Iterator direction_option = bbcode_options.find("direction");
5803 if (!direction_option) {
5804 direction_option = bbcode_options.find("dir");
5805 }
5806 if (direction_option) {
5807 if (direction_option->value == "a" || direction_option->value == "auto") {
5808 dir = Control::TEXT_DIRECTION_AUTO;
5809 } else if (direction_option->value == "l" || direction_option->value == "ltr") {
5810 dir = Control::TEXT_DIRECTION_LTR;
5811 } else if (direction_option->value == "r" || direction_option->value == "rtl") {
5812 dir = Control::TEXT_DIRECTION_RTL;
5813 }
5814 }
5815 OptionMap::Iterator language_option = bbcode_options.find("language");
5816 if (!language_option) {
5817 language_option = bbcode_options.find("lang");
5818 }
5819 if (language_option) {
5820 lang = language_option->value;
5821 }
5822 OptionMap::Iterator bidi_override_option = bbcode_options.find("bidi_override");
5823 if (!bidi_override_option) {
5824 bidi_override_option = bbcode_options.find("st");
5825 }
5826 if (bidi_override_option) {
5827 if (bidi_override_option->value == "d" || bidi_override_option->value == "default") {
5828 st_parser_type = TextServer::STRUCTURED_TEXT_DEFAULT;
5829 } else if (bidi_override_option->value == "u" || bidi_override_option->value == "uri") {
5830 st_parser_type = TextServer::STRUCTURED_TEXT_URI;
5831 } else if (bidi_override_option->value == "f" || bidi_override_option->value == "file") {
5832 st_parser_type = TextServer::STRUCTURED_TEXT_FILE;
5833 } else if (bidi_override_option->value == "e" || bidi_override_option->value == "email") {
5834 st_parser_type = TextServer::STRUCTURED_TEXT_EMAIL;
5835 } else if (bidi_override_option->value == "l" || bidi_override_option->value == "list") {
5836 st_parser_type = TextServer::STRUCTURED_TEXT_LIST;
5837 } else if (bidi_override_option->value == "n" || bidi_override_option->value == "gdscript") {
5838 st_parser_type = TextServer::STRUCTURED_TEXT_GDSCRIPT;
5839 } else if (bidi_override_option->value == "c" || bidi_override_option->value == "custom") {
5840 st_parser_type = TextServer::STRUCTURED_TEXT_CUSTOM;
5841 }
5842 }
5843
5844 push_paragraph(alignment, dir, lang, st_parser_type, jst_flags, tab_stops);
5845 pos = brk_end + 1;
5846 tag_stack.push_front("p");
5847 } else if (tag == "url") {
5848 int end = bbcode.find_char('[', brk_end);
5849 if (end == -1) {
5850 end = bbcode.length();
5851 }
5852 String url = bbcode.substr(brk_end + 1, end - brk_end - 1).unquote();
5853 push_meta(url, META_UNDERLINE_ALWAYS);
5854
5855 pos = brk_end + 1;
5856 tag_stack.push_front(tag);
5857
5858 } else if (tag.begins_with("url ")) {
5859 String url;
5860 MetaUnderline underline = META_UNDERLINE_ALWAYS;
5861 String tooltip;
5862
5863 OptionMap::Iterator underline_option = bbcode_options.find("underline");
5864 if (underline_option) {
5865 if (underline_option->value == "never") {
5866 underline = META_UNDERLINE_NEVER;
5867 } else if (underline_option->value == "always") {
5868 underline = META_UNDERLINE_ALWAYS;
5869 } else if (underline_option->value == "hover") {
5870 underline = META_UNDERLINE_ON_HOVER;
5871 }
5872 }
5873 OptionMap::Iterator tooltip_option = bbcode_options.find("tooltip");
5874 if (tooltip_option) {
5875 tooltip = tooltip_option->value;
5876 }
5877 OptionMap::Iterator href_option = bbcode_options.find("href");
5878 if (href_option) {
5879 url = href_option->value;
5880 }
5881
5882 push_meta(url, underline, tooltip);
5883
5884 pos = brk_end + 1;
5885 tag_stack.push_front("url");
5886 } else if (tag.begins_with("url=")) {
5887 String url = _get_tag_value(tag).unquote();
5888 push_meta(url, META_UNDERLINE_ALWAYS);
5889 pos = brk_end + 1;
5890 tag_stack.push_front("url");
5891 } else if (tag.begins_with("hint=")) {
5892 String description = _get_tag_value(tag).unquote();
5893 push_hint(description);
5894 pos = brk_end + 1;
5895 tag_stack.push_front("hint");
5896 } else if (tag.begins_with("dropcap")) {
5897 int fs = theme_cache.normal_font_size * 3;
5898 Ref<Font> f = theme_cache.normal_font;
5899 Color color = theme_cache.default_color;
5900 Color outline_color = theme_cache.font_outline_color;
5901 int outline_size = theme_cache.outline_size;
5902 Rect2 dropcap_margins;
5903
5904 OptionMap::Iterator font_option = bbcode_options.find("font");
5905 if (!font_option) {
5906 font_option = bbcode_options.find("f");
5907 }
5908 if (font_option) {
5909 const String &fnt = font_option->value;
5910 Ref<Font> font = ResourceLoader::load(fnt, "Font");
5911 if (font.is_valid()) {
5912 f = font;
5913 }
5914 }
5915 OptionMap::Iterator font_size_option = bbcode_options.find("font_size");
5916 if (font_size_option) {
5917 fs = font_size_option->value.to_int();
5918 }
5919 OptionMap::Iterator margins_option = bbcode_options.find("margins");
5920 if (margins_option) {
5921 Vector<String> subtag_b = _split_unquoted(margins_option->value, U',');
5922 _normalize_subtags(subtag_b);
5923
5924 if (subtag_b.size() == 4) {
5925 dropcap_margins.position.x = subtag_b[0].to_float();
5926 dropcap_margins.position.y = subtag_b[1].to_float();
5927 dropcap_margins.size.x = subtag_b[2].to_float();
5928 dropcap_margins.size.y = subtag_b[3].to_float();
5929 }
5930 }
5931 OptionMap::Iterator outline_size_option = bbcode_options.find("outline_size");
5932 if (outline_size_option) {
5933 outline_size = outline_size_option->value.to_int();
5934 }
5935 OptionMap::Iterator color_option = bbcode_options.find("color");
5936 if (color_option) {
5937 color = Color::from_string(color_option->value, color);
5938 }
5939 OptionMap::Iterator outline_color_option = bbcode_options.find("outline_color");
5940 if (outline_color_option) {
5941 outline_color = Color::from_string(outline_color_option->value, outline_color);
5942 }
5943
5944 int end = bbcode.find_char('[', brk_end);
5945 if (end == -1) {
5946 end = bbcode.length();
5947 }
5948
5949 String dc_txt = bbcode.substr(brk_end + 1, end - brk_end - 1);
5950
5951 push_dropcap(dc_txt, f, fs, dropcap_margins, color, outline_size, outline_color);
5952
5953 pos = end;
5954 tag_stack.push_front(bbcode_name);
5955 } else if (tag.begins_with("hr")) {
5956 HorizontalAlignment alignment = HORIZONTAL_ALIGNMENT_CENTER;
5957 OptionMap::Iterator align_option = bbcode_options.find("align");
5958 if (align_option) {
5959 if (align_option->value == "l" || align_option->value == "left") {
5960 alignment = HORIZONTAL_ALIGNMENT_LEFT;
5961 } else if (align_option->value == "c" || align_option->value == "center") {
5962 alignment = HORIZONTAL_ALIGNMENT_CENTER;
5963 } else if (align_option->value == "r" || align_option->value == "right") {
5964 alignment = HORIZONTAL_ALIGNMENT_RIGHT;
5965 }
5966 }
5967
5968 Color color = theme_cache.default_color;
5969 OptionMap::Iterator color_option = bbcode_options.find("color");
5970 if (color_option) {
5971 color = Color::from_string(color_option->value, color);
5972 }
5973 int width = 90;
5974 bool width_in_percent = true;
5975 OptionMap::Iterator width_option = bbcode_options.find("width");
5976 if (width_option) {
5977 width = width_option->value.to_int();
5978 width_in_percent = (width_option->value.ends_with("%"));
5979 }
5980
5981 int height = 2;
5982 bool height_in_percent = false;
5983 OptionMap::Iterator height_option = bbcode_options.find("height");
5984 if (height_option) {
5985 height = height_option->value.to_int();
5986 height_in_percent = (height_option->value.ends_with("%"));
5987 }
5988
5989 add_hr(width, height, color, alignment, width_in_percent, height_in_percent);
5990
5991 pos = brk_end + 1;
5992 } else if (tag.begins_with("img")) {
5993 int alignment = INLINE_ALIGNMENT_CENTER;
5994 if (tag.begins_with("img=")) {
5995 Vector<String> subtag = _split_unquoted(_get_tag_value(tag), U',');
5996 _normalize_subtags(subtag);
5997
5998 if (subtag.size() > 1) {
5999 if (subtag[0] == "top" || subtag[0] == "t") {
6000 alignment = INLINE_ALIGNMENT_TOP_TO;
6001 } else if (subtag[0] == "center" || subtag[0] == "c") {
6002 alignment = INLINE_ALIGNMENT_CENTER_TO;
6003 } else if (subtag[0] == "bottom" || subtag[0] == "b") {
6004 alignment = INLINE_ALIGNMENT_BOTTOM_TO;
6005 }
6006 if (subtag[1] == "top" || subtag[1] == "t") {
6007 alignment |= INLINE_ALIGNMENT_TO_TOP;
6008 } else if (subtag[1] == "center" || subtag[1] == "c") {
6009 alignment |= INLINE_ALIGNMENT_TO_CENTER;
6010 } else if (subtag[1] == "baseline" || subtag[1] == "l") {
6011 alignment |= INLINE_ALIGNMENT_TO_BASELINE;
6012 } else if (subtag[1] == "bottom" || subtag[1] == "b") {
6013 alignment |= INLINE_ALIGNMENT_TO_BOTTOM;
6014 }
6015 } else if (!subtag.is_empty()) {
6016 if (subtag[0] == "top" || subtag[0] == "t") {
6017 alignment = INLINE_ALIGNMENT_TOP;
6018 } else if (subtag[0] == "center" || subtag[0] == "c") {
6019 alignment = INLINE_ALIGNMENT_CENTER;
6020 } else if (subtag[0] == "bottom" || subtag[0] == "b") {
6021 alignment = INLINE_ALIGNMENT_BOTTOM;
6022 }
6023 }
6024 }
6025
6026 int end = bbcode.find_char('[', brk_end);
6027 if (end == -1) {
6028 end = bbcode.length();
6029 }
6030
6031 String image = bbcode.substr(brk_end + 1, end - brk_end - 1);
6032 String alt_text;
6033
6034 Ref<Texture2D> texture = ResourceLoader::load(image, "Texture2D");
6035 if (texture.is_valid()) {
6036 Rect2 region;
6037 OptionMap::Iterator region_option = bbcode_options.find("region");
6038 if (region_option) {
6039 Vector<String> region_values = _split_unquoted(region_option->value, U',');
6040 if (region_values.size() == 4) {
6041 region.position.x = region_values[0].to_float();
6042 region.position.y = region_values[1].to_float();
6043 region.size.x = region_values[2].to_float();
6044 region.size.y = region_values[3].to_float();
6045 }
6046 }
6047
6048 Color color = Color(1.0, 1.0, 1.0);
6049 OptionMap::Iterator color_option = bbcode_options.find("color");
6050 if (color_option) {
6051 color = Color::from_string(color_option->value, color);
6052 }
6053
6054 OptionMap::Iterator alt_text_option = bbcode_options.find("alt");
6055 if (alt_text_option) {
6056 alt_text = alt_text_option->value;
6057 }
6058
6059 int width = 0;
6060 int height = 0;
6061 bool pad = false;
6062 String tooltip;
6063 bool width_in_percent = false;
6064 bool height_in_percent = false;
6065 if (!bbcode_value.is_empty()) {
6066 int sep = bbcode_value.find_char('x');
6067 if (sep == -1) {
6068 if (bbcode_value.ends_with("%")) {
6069 width_in_percent = true;
6070 }
6071 width = bbcode_value.to_int();
6072 } else {
6073 if (bbcode_value.substr(0, sep).ends_with("%")) {
6074 width_in_percent = true;
6075 }
6076 width = bbcode_value.substr(0, sep).to_int();
6077 if (bbcode_value.substr(sep + 1).ends_with("%")) {
6078 height_in_percent = true;
6079 }
6080 height = bbcode_value.substr(sep + 1).to_int();
6081 }
6082 } else {
6083 OptionMap::Iterator align_option = bbcode_options.find("align");
6084 if (align_option) {
6085 Vector<String> subtag = _split_unquoted(align_option->value, U',');
6086 _normalize_subtags(subtag);
6087
6088 if (subtag.size() > 1) {
6089 if (subtag[0] == "top" || subtag[0] == "t") {
6090 alignment = INLINE_ALIGNMENT_TOP_TO;
6091 } else if (subtag[0] == "center" || subtag[0] == "c") {
6092 alignment = INLINE_ALIGNMENT_CENTER_TO;
6093 } else if (subtag[0] == "bottom" || subtag[0] == "b") {
6094 alignment = INLINE_ALIGNMENT_BOTTOM_TO;
6095 }
6096 if (subtag[1] == "top" || subtag[1] == "t") {
6097 alignment |= INLINE_ALIGNMENT_TO_TOP;
6098 } else if (subtag[1] == "center" || subtag[1] == "c") {
6099 alignment |= INLINE_ALIGNMENT_TO_CENTER;
6100 } else if (subtag[1] == "baseline" || subtag[1] == "l") {
6101 alignment |= INLINE_ALIGNMENT_TO_BASELINE;
6102 } else if (subtag[1] == "bottom" || subtag[1] == "b") {
6103 alignment |= INLINE_ALIGNMENT_TO_BOTTOM;
6104 }
6105 } else if (!subtag.is_empty()) {
6106 if (subtag[0] == "top" || subtag[0] == "t") {
6107 alignment = INLINE_ALIGNMENT_TOP;
6108 } else if (subtag[0] == "center" || subtag[0] == "c") {
6109 alignment = INLINE_ALIGNMENT_CENTER;
6110 } else if (subtag[0] == "bottom" || subtag[0] == "b") {
6111 alignment = INLINE_ALIGNMENT_BOTTOM;
6112 }
6113 }
6114 }
6115 OptionMap::Iterator width_option = bbcode_options.find("width");
6116 if (width_option) {
6117 width = width_option->value.to_int();
6118 if (width_option->value.ends_with("%")) {
6119 width_in_percent = true;
6120 }
6121 }
6122
6123 OptionMap::Iterator height_option = bbcode_options.find("height");
6124 if (height_option) {
6125 height = height_option->value.to_int();
6126 if (height_option->value.ends_with("%")) {
6127 height_in_percent = true;
6128 }
6129 }
6130
6131 OptionMap::Iterator tooltip_option = bbcode_options.find("tooltip");
6132 if (tooltip_option) {
6133 tooltip = tooltip_option->value;
6134 }
6135
6136 OptionMap::Iterator pad_option = bbcode_options.find("pad");
6137 if (pad_option) {
6138 pad = (pad_option->value == "true");
6139 }
6140 }
6141
6142 add_image(texture, width, height, color, (InlineAlignment)alignment, region, Variant(), pad, tooltip, width_in_percent, height_in_percent, alt_text);
6143 }
6144
6145 pos = end;
6146 tag_stack.push_front(bbcode_name);
6147 } else if (tag.begins_with("color=")) {
6148 String color_str = _get_tag_value(tag).unquote();
6149 Color color = Color::from_string(color_str, theme_cache.default_color);
6150 push_color(color);
6151 pos = brk_end + 1;
6152 tag_stack.push_front("color");
6153
6154 } else if (tag.begins_with("outline_color=")) {
6155 String color_str = _get_tag_value(tag).unquote();
6156 Color color = Color::from_string(color_str, theme_cache.default_color);
6157 push_outline_color(color);
6158 pos = brk_end + 1;
6159 tag_stack.push_front("outline_color");
6160
6161 } else if (tag.begins_with("font_size=")) {
6162 int fnt_size = _get_tag_value(tag).to_int();
6163 push_font_size(fnt_size);
6164 pos = brk_end + 1;
6165 tag_stack.push_front("font_size");
6166
6167 } else if (tag.begins_with("opentype_features=") || tag.begins_with("otf=")) {
6168 int value_pos = tag.find_char('=');
6169 String fnt_ftr = tag.substr(value_pos + 1);
6170 Vector<String> subtag = fnt_ftr.split(",");
6171 _normalize_subtags(subtag);
6172
6173 Ref<Font> font = theme_cache.normal_font;
6174 DefaultFont def_font = RTL_NORMAL_FONT;
6175
6176 ItemFont *font_it = _find_font(current);
6177 if (font_it) {
6178 if (font_it->font.is_valid()) {
6179 font = font_it->font;
6180 def_font = font_it->def_font;
6181 }
6182 }
6183 Dictionary features;
6184 if (!subtag.is_empty()) {
6185 for (int i = 0; i < subtag.size(); i++) {
6186 Vector<String> subtag_a = subtag[i].split("=");
6187 _normalize_subtags(subtag_a);
6188
6189 if (subtag_a.size() == 2) {
6190 features[TS->name_to_tag(subtag_a[0])] = subtag_a[1].to_int();
6191 } else if (subtag_a.size() == 1) {
6192 features[TS->name_to_tag(subtag_a[0])] = 1;
6193 }
6194 }
6195 }
6196 Ref<FontVariation> fc;
6197 fc.instantiate();
6198
6199 fc->set_base_font(font);
6200 fc->set_opentype_features(features);
6201
6202 if (def_font != RTL_CUSTOM_FONT) {
6203 _push_def_font_var(def_font, fc);
6204 } else {
6205 push_font(fc);
6206 }
6207
6208 pos = brk_end + 1;
6209 tag_stack.push_front(tag.substr(0, value_pos));
6210
6211 } else if (tag.begins_with("font=")) {
6212 String fnt = _get_tag_value(tag).unquote();
6213
6214 Ref<Font> fc = ResourceLoader::load(fnt, "Font");
6215 if (fc.is_valid()) {
6216 push_font(fc);
6217 } else {
6218 push_font(theme_cache.normal_font);
6219 }
6220
6221 pos = brk_end + 1;
6222 tag_stack.push_front("font");
6223
6224 } else if (tag.begins_with("font ")) {
6225 Ref<Font> font = theme_cache.normal_font;
6226 DefaultFont def_font = RTL_NORMAL_FONT;
6227 int fnt_size = -1;
6228
6229 ItemFont *font_it = _find_font(current);
6230 if (font_it) {
6231 if (font_it->font.is_valid()) {
6232 font = font_it->font;
6233 def_font = font_it->def_font;
6234 }
6235 }
6236
6237 Ref<FontVariation> fc;
6238 fc.instantiate();
6239
6240 OptionMap::Iterator name_option = bbcode_options.find("name");
6241 if (!name_option) {
6242 name_option = bbcode_options.find("n");
6243 }
6244 if (name_option) {
6245 const String &fnt = name_option->value;
6246 Ref<Font> font_data = ResourceLoader::load(fnt, "Font");
6247 if (font_data.is_valid()) {
6248 font = font_data;
6249 def_font = RTL_CUSTOM_FONT;
6250 }
6251 }
6252 OptionMap::Iterator size_option = bbcode_options.find("size");
6253 if (!size_option) {
6254 size_option = bbcode_options.find("s");
6255 }
6256 if (size_option) {
6257 fnt_size = size_option->value.to_int();
6258 }
6259 OptionMap::Iterator glyph_spacing_option = bbcode_options.find("glyph_spacing");
6260 if (!glyph_spacing_option) {
6261 glyph_spacing_option = bbcode_options.find("gl");
6262 }
6263 if (glyph_spacing_option) {
6264 int spacing = glyph_spacing_option->value.to_int();
6265 fc->set_spacing(TextServer::SPACING_GLYPH, spacing);
6266 }
6267 OptionMap::Iterator space_spacing_option = bbcode_options.find("space_spacing");
6268 if (!space_spacing_option) {
6269 space_spacing_option = bbcode_options.find("sp");
6270 }
6271 if (space_spacing_option) {
6272 int spacing = space_spacing_option->value.to_int();
6273 fc->set_spacing(TextServer::SPACING_SPACE, spacing);
6274 }
6275 OptionMap::Iterator top_spacing_option = bbcode_options.find("top_spacing");
6276 if (!top_spacing_option) {
6277 top_spacing_option = bbcode_options.find("top");
6278 }
6279 if (top_spacing_option) {
6280 int spacing = top_spacing_option->value.to_int();
6281 fc->set_spacing(TextServer::SPACING_TOP, spacing);
6282 }
6283 OptionMap::Iterator bottom_spacing_option = bbcode_options.find("bottom_spacing");
6284 if (!bottom_spacing_option) {
6285 bottom_spacing_option = bbcode_options.find("bt");
6286 }
6287 if (bottom_spacing_option) {
6288 int spacing = bottom_spacing_option->value.to_int();
6289 fc->set_spacing(TextServer::SPACING_BOTTOM, spacing);
6290 }
6291 OptionMap::Iterator embolden_option = bbcode_options.find("embolden");
6292 if (!embolden_option) {
6293 embolden_option = bbcode_options.find("emb");
6294 }
6295 if (embolden_option) {
6296 float emb = embolden_option->value.to_float();
6297 fc->set_variation_embolden(emb);
6298 }
6299 OptionMap::Iterator face_index_option = bbcode_options.find("face_index");
6300 if (!face_index_option) {
6301 face_index_option = bbcode_options.find("fi");
6302 }
6303 if (face_index_option) {
6304 int fi = face_index_option->value.to_int();
6305 fc->set_variation_face_index(fi);
6306 }
6307 OptionMap::Iterator slant_option = bbcode_options.find("slant");
6308 if (!slant_option) {
6309 slant_option = bbcode_options.find("sln");
6310 }
6311 if (slant_option) {
6312 float slant = slant_option->value.to_float();
6313 fc->set_variation_transform(Transform2D(1.0, slant, 0.0, 1.0, 0.0, 0.0));
6314 }
6315 OptionMap::Iterator opentype_variation_option = bbcode_options.find("opentype_variation");
6316 if (!opentype_variation_option) {
6317 opentype_variation_option = bbcode_options.find("otv");
6318 }
6319 if (opentype_variation_option) {
6320 Dictionary variations;
6321 if (!opentype_variation_option->value.is_empty()) {
6322 Vector<String> variation_tags = opentype_variation_option->value.split(",");
6323 for (int j = 0; j < variation_tags.size(); j++) {
6324 Vector<String> subtag_b = variation_tags[j].split("=");
6325 _normalize_subtags(subtag_b);
6326
6327 if (subtag_b.size() == 2) {
6328 variations[TS->name_to_tag(subtag_b[0])] = subtag_b[1].to_float();
6329 }
6330 }
6331 fc->set_variation_opentype(variations);
6332 }
6333 }
6334 OptionMap::Iterator opentype_features_option = bbcode_options.find("opentype_features");
6335 if (!opentype_features_option) {
6336 opentype_features_option = bbcode_options.find("otf");
6337 }
6338 if (opentype_features_option) {
6339 Dictionary features;
6340 if (!opentype_features_option->value.is_empty()) {
6341 Vector<String> feature_tags = opentype_features_option->value.split(",");
6342 for (int j = 0; j < feature_tags.size(); j++) {
6343 Vector<String> subtag_b = feature_tags[j].split("=");
6344 _normalize_subtags(subtag_b);
6345
6346 if (subtag_b.size() == 2) {
6347 features[TS->name_to_tag(subtag_b[0])] = subtag_b[1].to_float();
6348 } else if (subtag_b.size() == 1) {
6349 features[TS->name_to_tag(subtag_b[0])] = 1;
6350 }
6351 }
6352 fc->set_opentype_features(features);
6353 }
6354 }
6355
6356 fc->set_base_font(font);
6357
6358 if (def_font != RTL_CUSTOM_FONT) {
6359 _push_def_font_var(def_font, fc, fnt_size);
6360 } else {
6361 push_font(fc, fnt_size);
6362 }
6363
6364 pos = brk_end + 1;
6365 tag_stack.push_front("font");
6366
6367 } else if (tag.begins_with("outline_size=")) {
6368 int fnt_size = _get_tag_value(tag).to_int();
6369 push_outline_size(MAX(0, fnt_size));
6370 pos = brk_end + 1;
6371 tag_stack.push_front("outline_size");
6372
6373 } else if (bbcode_name == "fade") {
6374 int start_index = 0;
6375 OptionMap::Iterator start_option = bbcode_options.find("start");
6376 if (start_option) {
6377 start_index = start_option->value.to_int();
6378 }
6379
6380 int length = 10;
6381 OptionMap::Iterator length_option = bbcode_options.find("length");
6382 if (length_option) {
6383 length = length_option->value.to_int();
6384 }
6385
6386 push_fade(start_index, length);
6387 pos = brk_end + 1;
6388 tag_stack.push_front("fade");
6389 } else if (bbcode_name == "shake") {
6390 int strength = 5;
6391 OptionMap::Iterator strength_option = bbcode_options.find("level");
6392 if (strength_option) {
6393 strength = strength_option->value.to_int();
6394 }
6395
6396 float rate = 20.0f;
6397 OptionMap::Iterator rate_option = bbcode_options.find("rate");
6398 if (rate_option) {
6399 rate = rate_option->value.to_float();
6400 }
6401
6402 bool connected = true;
6403 OptionMap::Iterator connected_option = bbcode_options.find("connected");
6404 if (connected_option) {
6405 connected = connected_option->value.to_int();
6406 }
6407
6408 push_shake(strength, rate, connected);
6409 pos = brk_end + 1;
6410 tag_stack.push_front("shake");
6411 set_process_internal(true);
6412 } else if (bbcode_name == "wave") {
6413 float amplitude = 20.0f;
6414 OptionMap::Iterator amplitude_option = bbcode_options.find("amp");
6415 if (amplitude_option) {
6416 amplitude = amplitude_option->value.to_float();
6417 }
6418
6419 float period = 5.0f;
6420 OptionMap::Iterator period_option = bbcode_options.find("freq");
6421 if (period_option) {
6422 period = period_option->value.to_float();
6423 }
6424
6425 bool connected = true;
6426 OptionMap::Iterator connected_option = bbcode_options.find("connected");
6427 if (connected_option) {
6428 connected = connected_option->value.to_int();
6429 }
6430
6431 push_wave(period, amplitude, connected);
6432 pos = brk_end + 1;
6433 tag_stack.push_front("wave");
6434 set_process_internal(true);
6435 } else if (bbcode_name == "tornado") {
6436 float radius = 10.0f;
6437 OptionMap::Iterator radius_option = bbcode_options.find("radius");
6438 if (radius_option) {
6439 radius = radius_option->value.to_float();
6440 }
6441
6442 float frequency = 1.0f;
6443 OptionMap::Iterator frequency_option = bbcode_options.find("freq");
6444 if (frequency_option) {
6445 frequency = frequency_option->value.to_float();
6446 }
6447
6448 bool connected = true;
6449 OptionMap::Iterator connected_option = bbcode_options.find("connected");
6450 if (connected_option) {
6451 connected = connected_option->value.to_int();
6452 }
6453
6454 push_tornado(frequency, radius, connected);
6455 pos = brk_end + 1;
6456 tag_stack.push_front("tornado");
6457 set_process_internal(true);
6458 } else if (bbcode_name == "rainbow") {
6459 float saturation = 0.8f;
6460 OptionMap::Iterator saturation_option = bbcode_options.find("sat");
6461 if (saturation_option) {
6462 saturation = saturation_option->value.to_float();
6463 }
6464
6465 float value = 0.8f;
6466 OptionMap::Iterator value_option = bbcode_options.find("val");
6467 if (value_option) {
6468 value = value_option->value.to_float();
6469 }
6470
6471 float frequency = 1.0f;
6472 OptionMap::Iterator frequency_option = bbcode_options.find("freq");
6473 if (frequency_option) {
6474 frequency = frequency_option->value.to_float();
6475 }
6476
6477 float speed = 1.0f;
6478 OptionMap::Iterator speed_option = bbcode_options.find("speed");
6479 if (speed_option) {
6480 speed = speed_option->value.to_float();
6481 }
6482
6483 push_rainbow(saturation, value, frequency, speed);
6484 pos = brk_end + 1;
6485 tag_stack.push_front("rainbow");
6486 set_process_internal(true);
6487 } else if (bbcode_name == "pulse") {
6488 Color color = Color(1, 1, 1, 0.25);
6489 OptionMap::Iterator color_option = bbcode_options.find("color");
6490 if (color_option) {
6491 color = Color::from_string(color_option->value, color);
6492 }
6493
6494 float frequency = 1.0;
6495 OptionMap::Iterator freq_option = bbcode_options.find("freq");
6496 if (freq_option) {
6497 frequency = freq_option->value.to_float();
6498 }
6499
6500 float ease = -2.0;
6501 OptionMap::Iterator ease_option = bbcode_options.find("ease");
6502 if (ease_option) {
6503 ease = ease_option->value.to_float();
6504 }
6505
6506 push_pulse(color, frequency, ease);
6507 pos = brk_end + 1;
6508 tag_stack.push_front("pulse");
6509 set_process_internal(true);
6510 } else if (tag.begins_with("bgcolor=")) {
6511 String color_str = _get_tag_value(tag).unquote();
6512 Color color = Color::from_string(color_str, theme_cache.default_color);
6513
6514 push_bgcolor(color);
6515 pos = brk_end + 1;
6516 tag_stack.push_front("bgcolor");
6517
6518 } else if (tag.begins_with("fgcolor=")) {
6519 String color_str = _get_tag_value(tag).unquote();
6520 Color color = Color::from_string(color_str, theme_cache.default_color);
6521
6522 push_fgcolor(color);
6523 pos = brk_end + 1;
6524 tag_stack.push_front("fgcolor");
6525
6526 } else {
6527 Vector<String> &expr = split_tag_block;
6528 if (expr.is_empty()) {
6529 add_text("[");
6530 pos = brk_pos + 1;
6531 } else {
6532 String identifier = expr[0];
6533 expr.remove_at(0);
6534 Dictionary properties = parse_expressions_for_values(expr);
6535 Ref<RichTextEffect> effect = _get_custom_effect_by_code(identifier);
6536
6537 if (effect.is_valid()) {
6538 push_customfx(effect, properties);
6539 pos = brk_end + 1;
6540 tag_stack.push_front(identifier);
6541 } else {
6542 add_text("["); //ignore
6543 pos = brk_pos + 1;
6544 }
6545 }
6546 }
6547 }
6548
6549 parsing_bbcode.store(false);
6550 }
6551
6552 void RichTextLabel::scroll_to_selection() {
6553 float line_offset = get_selection_line_offset();
6554 if (line_offset != -1.0) {
6555 vscroll->set_value(line_offset);
6556 queue_accessibility_update();
6557 }
6558 }
6559
6560 void RichTextLabel::scroll_to_paragraph(int p_paragraph) {
6561 _validate_line_caches();
6562
6563 if (p_paragraph <= 0) {
6564 vscroll->set_value(0);
6565 } else if (p_paragraph >= main->first_invalid_line.load()) {
6566 vscroll->set_value(vscroll->get_max());
6567 } else {
6568 vscroll->set_value(main->lines[p_paragraph].offset.y);
6569 }
6570 queue_accessibility_update();
6571 }
6572
6573 int RichTextLabel::get_paragraph_count() const {
6574 return main->lines.size();
6575 }
6576
6577 int RichTextLabel::get_visible_paragraph_count() const {
6578 if (!is_visible()) {
6579 return 0;
6580 }
6581
6582 const_cast<RichTextLabel *>(this)->_validate_line_caches();
6583 return visible_paragraph_count;
6584 }
6585
6586 void RichTextLabel::scroll_to_line(int p_line) {
6587 if (p_line <= 0) {
6588 vscroll->set_value(0);
6589 queue_accessibility_update();
6590 return;
6591 }
6592 _validate_line_caches();
6593
6594 int line_count = 0;
6595 int to_line = main->first_invalid_line.load();
6596 for (int i = 0; i < to_line; i++) {
6597 MutexLock lock(main->lines[i].text_buf->get_mutex());
6598 if ((line_count <= p_line) && (line_count + main->lines[i].text_buf->get_line_count() >= p_line)) {
6599 float line_offset = 0.f;
6600 for (int j = 0; j < p_line - line_count; j++) {
6601 line_offset += main->lines[i].text_buf->get_line_ascent(j) + main->lines[i].text_buf->get_line_descent(j) + theme_cache.line_separation;
6602 }
6603 vscroll->set_value(main->lines[i].offset.y + line_offset);
6604 queue_accessibility_update();
6605 return;
6606 }
6607 line_count += main->lines[i].text_buf->get_line_count();
6608 }
6609 vscroll->set_value(vscroll->get_max());
6610 queue_accessibility_update();
6611 }
6612
6613 float RichTextLabel::get_line_offset(int p_line) {
6614 _validate_line_caches();
6615
6616 int line_count = 0;
6617 int to_line = main->first_invalid_line.load();
6618 for (int i = 0; i < to_line; i++) {
6619 MutexLock lock(main->lines[i].text_buf->get_mutex());
6620 if ((line_count <= p_line) && (p_line <= line_count + main->lines[i].text_buf->get_line_count())) {
6621 float line_offset = 0.f;
6622 for (int j = 0; j < p_line - line_count; j++) {
6623 line_offset += main->lines[i].text_buf->get_line_ascent(j) + main->lines[i].text_buf->get_line_descent(j) + theme_cache.line_separation;
6624 }
6625 return main->lines[i].offset.y + line_offset;
6626 }
6627 line_count += main->lines[i].text_buf->get_line_count();
6628 }
6629 return 0;
6630 }
6631
6632 float RichTextLabel::get_paragraph_offset(int p_paragraph) {
6633 _validate_line_caches();
6634
6635 int to_line = main->first_invalid_line.load();
6636 if (0 <= p_paragraph && p_paragraph < to_line) {
6637 return main->lines[p_paragraph].offset.y;
6638 }
6639 return 0;
6640 }
6641
6642 int RichTextLabel::get_line_count() const {
6643 const_cast<RichTextLabel *>(this)->_validate_line_caches();
6644
6645 int line_count = 0;
6646 int to_line = main->first_invalid_line.load();
6647 for (int i = 0; i < to_line; i++) {
6648 MutexLock lock(main->lines[i].text_buf->get_mutex());
6649 line_count += main->lines[i].text_buf->get_line_count();
6650 }
6651 return line_count;
6652 }
6653
6654 Vector2i RichTextLabel::get_line_range(int p_line) {
6655 const_cast<RichTextLabel *>(this)->_validate_line_caches();
6656
6657 int line_count = 0;
6658 int to_line = main->first_invalid_line.load();
6659 for (int i = 0; i < to_line; i++) {
6660 MutexLock lock(main->lines[i].text_buf->get_mutex());
6661 int lc = main->lines[i].text_buf->get_line_count();
6662
6663 if (p_line < line_count + lc) {
6664 Vector2i char_offset = Vector2i(main->lines[i].char_offset, main->lines[i].char_offset);
6665 Vector2i line_range = main->lines[i].text_buf->get_line_range(p_line - line_count);
6666 return char_offset + line_range;
6667 }
6668
6669 line_count += lc;
6670 }
6671 return Vector2i();
6672 }
6673
6674 int RichTextLabel::get_visible_line_count() const {
6675 if (!is_visible()) {
6676 return 0;
6677 }
6678 const_cast<RichTextLabel *>(this)->_validate_line_caches();
6679
6680 return visible_line_count;
6681 }
6682
6683 void RichTextLabel::set_selection_enabled(bool p_enabled) {
6684 if (selection.enabled == p_enabled) {
6685 return;
6686 }
6687
6688 selection.enabled = p_enabled;
6689 if (!p_enabled) {
6690 if (selection.active) {
6691 deselect();
6692 }
6693 set_focus_mode(FOCUS_ACCESSIBILITY);
6694 } else {
6695 set_focus_mode(FOCUS_ALL);
6696 }
6697 queue_accessibility_update();
6698 }
6699
6700 void RichTextLabel::set_deselect_on_focus_loss_enabled(const bool p_enabled) {
6701 if (deselect_on_focus_loss_enabled == p_enabled) {
6702 return;
6703 }
6704
6705 deselect_on_focus_loss_enabled = p_enabled;
6706 if (p_enabled && selection.active && !has_focus()) {
6707 deselect();
6708 }
6709 }
6710
6711 Variant RichTextLabel::get_drag_data(const Point2 &p_point) {
6712 Variant ret = Control::get_drag_data(p_point);
6713 if (ret != Variant()) {
6714 return ret;
6715 }
6716
6717 if (selection.drag_attempt && selection.enabled) {
6718 String t = get_selected_text();
6719 Label *l = memnew(Label);
6720 l->set_text(t);
6721 l->set_focus_mode(FOCUS_ACCESSIBILITY);
6722 l->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); // Text is already translated.
6723 set_drag_preview(l);
6724 return t;
6725 }
6726
6727 return Variant();
6728 }
6729
6730 bool RichTextLabel::_is_click_inside_selection() const {
6731 if (selection.active && selection.enabled && selection.click_frame && selection.from_frame && selection.to_frame) {
6732 const Line &l_click = selection.click_frame->lines[selection.click_line];
6733 const Line &l_from = selection.from_frame->lines[selection.from_line];
6734 const Line &l_to = selection.to_frame->lines[selection.to_line];
6735 return (l_click.char_offset + selection.click_char >= l_from.char_offset + selection.from_char) && (l_click.char_offset + selection.click_char <= l_to.char_offset + selection.to_char);
6736 } else {
6737 return false;
6738 }
6739 }
6740
6741 bool RichTextLabel::_search_table_cell(ItemTable *p_table, List<Item *>::Element *p_cell, const String &p_string, bool p_reverse_search, int p_from_line) {
6742 ERR_FAIL_COND_V(p_cell->get()->type != ITEM_FRAME, false); // Children should all be frames.
6743 ItemFrame *frame = static_cast<ItemFrame *>(p_cell->get());
6744 if (p_from_line < 0) {
6745 p_from_line = (int)frame->lines.size() - 1;
6746 }
6747
6748 if (p_reverse_search) {
6749 for (int i = p_from_line; i >= 0; i--) {
6750 if (_search_line(frame, i, p_string, -1, p_reverse_search)) {
6751 return true;
6752 }
6753 }
6754 } else {
6755 for (int i = p_from_line; i < (int)frame->lines.size(); i++) {
6756 if (_search_line(frame, i, p_string, 0, p_reverse_search)) {
6757 return true;
6758 }
6759 }
6760 }
6761
6762 return false;
6763 }
6764
6765 bool RichTextLabel::_search_table(ItemTable *p_table, List<Item *>::Element *p_from, const String &p_string, bool p_reverse_search) {
6766 List<Item *>::Element *E = p_from;
6767 while (E != nullptr) {
6768 int from_line = p_reverse_search ? -1 : 0;
6769 if (_search_table_cell(p_table, E, p_string, p_reverse_search, from_line)) {
6770 return true;
6771 }
6772 E = p_reverse_search ? E->prev() : E->next();
6773 }
6774 return false;
6775 }
6776
6777 bool RichTextLabel::_search_line(ItemFrame *p_frame, int p_line, const String &p_string, int p_char_idx, bool p_reverse_search) {
6778 ERR_FAIL_NULL_V(p_frame, false);
6779 ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)p_frame->lines.size(), false);
6780
6781 Line &l = p_frame->lines[p_line];
6782
6783 String txt;
6784 Item *it_to = (p_line + 1 < (int)p_frame->lines.size()) ? p_frame->lines[p_line + 1].from : nullptr;
6785 for (Item *it = l.from; it && it != it_to; it = _get_next_item(it)) {
6786 switch (it->type) {
6787 case ITEM_NEWLINE: {
6788 txt += "\n";
6789 } break;
6790 case ITEM_TEXT: {
6791 ItemText *t = static_cast<ItemText *>(it);
6792 txt += t->text;
6793 } break;
6794 case ITEM_IMAGE: {
6795 txt += " ";
6796 } break;
6797 case ITEM_TABLE: {
6798 ItemTable *table = static_cast<ItemTable *>(it);
6799 List<Item *>::Element *E = p_reverse_search ? table->subitems.back() : table->subitems.front();
6800 if (_search_table(table, E, p_string, p_reverse_search)) {
6801 return true;
6802 }
6803 } break;
6804 default:
6805 break;
6806 }
6807 }
6808
6809 int sp = -1;
6810 if (p_reverse_search) {
6811 sp = txt.rfindn(p_string, p_char_idx);
6812 } else {
6813 sp = txt.findn(p_string, p_char_idx);
6814 }
6815
6816 if (sp != -1) {
6817 selection.from_frame = p_frame;
6818 selection.from_line = p_line;
6819 selection.from_item = _get_item_at_pos(l.from, it_to, sp);
6820 selection.from_char = sp;
6821 selection.to_frame = p_frame;
6822 selection.to_line = p_line;
6823 selection.to_item = _get_item_at_pos(l.from, it_to, sp + p_string.length());
6824 selection.to_char = sp + p_string.length();
6825 selection.active = true;
6826 queue_accessibility_update();
6827 return true;
6828 }
6829
6830 return false;
6831 }
6832
6833 bool RichTextLabel::search(const String &p_string, bool p_from_selection, bool p_search_previous) {
6834 ERR_FAIL_COND_V(!selection.enabled, false);
6835
6836 if (p_string.is_empty()) {
6837 selection.active = false;
6838 queue_accessibility_update();
6839 return false;
6840 }
6841
6842 int char_idx = p_search_previous ? -1 : 0;
6843 int current_line = 0;
6844 int to_line = main->first_invalid_line.load();
6845 int ending_line = to_line - 1;
6846 if (p_from_selection && selection.active) {
6847 // First check to see if other results exist in current line
6848 char_idx = p_search_previous ? selection.from_char - 1 : selection.to_char;
6849 if (!(p_search_previous && char_idx < 0) &&
6850 _search_line(selection.from_frame, selection.from_line, p_string, char_idx, p_search_previous)) {
6851 scroll_to_selection();
6852 queue_redraw();
6853 return true;
6854 }
6855 char_idx = p_search_previous ? -1 : 0;
6856
6857 // Next, check to see if the current search result is in a table
6858 bool in_table = selection.from_frame->parent != nullptr && selection.from_frame->parent->type == ITEM_TABLE;
6859 if (in_table) {
6860 // Find last search result in table
6861 ItemTable *parent_table = static_cast<ItemTable *>(selection.from_frame->parent);
6862 List<Item *>::Element *parent_element = p_search_previous ? parent_table->subitems.back() : parent_table->subitems.front();
6863
6864 while (parent_element->get() != selection.from_frame) {
6865 parent_element = p_search_previous ? parent_element->prev() : parent_element->next();
6866 ERR_FAIL_NULL_V(parent_element, false);
6867 }
6868
6869 // Search remainder of current cell
6870 int from_line = p_search_previous ? selection.from_line - 1 : selection.from_line + 1;
6871 if (from_line >= 0 && _search_table_cell(parent_table, parent_element, p_string, p_search_previous, from_line)) {
6872 scroll_to_selection();
6873 queue_redraw();
6874 return true;
6875 }
6876
6877 // Search remainder of table
6878 if (!(p_search_previous && parent_element == parent_table->subitems.front()) &&
6879 !(!p_search_previous && parent_element == parent_table->subitems.back())) {
6880 parent_element = p_search_previous ? parent_element->prev() : parent_element->next(); // Don't want to search current item
6881 ERR_FAIL_NULL_V(parent_element, false);
6882
6883 // Search for next element
6884 if (_search_table(parent_table, parent_element, p_string, p_search_previous)) {
6885 scroll_to_selection();
6886 queue_redraw();
6887 return true;
6888 }
6889 }
6890 }
6891
6892 ending_line = selection.from_frame->line;
6893 if (!in_table) {
6894 ending_line += selection.from_line;
6895 }
6896 current_line = p_search_previous ? ending_line - 1 : ending_line + 1;
6897 } else if (p_search_previous) {
6898 current_line = ending_line;
6899 ending_line = 0;
6900 }
6901
6902 // Search remainder of the file
6903 while (current_line != ending_line) {
6904 // Wrap around
6905 if (current_line < 0) {
6906 current_line = to_line - 1;
6907 } else if (current_line >= to_line) {
6908 current_line = 0;
6909 }
6910
6911 if (_search_line(main, current_line, p_string, char_idx, p_search_previous)) {
6912 scroll_to_selection();
6913 queue_redraw();
6914 return true;
6915 }
6916
6917 if (current_line != ending_line) {
6918 p_search_previous ? current_line-- : current_line++;
6919 }
6920 }
6921
6922 if (p_from_selection && selection.active) {
6923 // Check contents of selection
6924 return _search_line(main, current_line, p_string, char_idx, p_search_previous);
6925 } else {
6926 return false;
6927 }
6928 }
6929
6930 String RichTextLabel::_get_line_text(ItemFrame *p_frame, int p_line, Selection p_selection) const {
6931 String txt;
6932
6933 ERR_FAIL_NULL_V(p_frame, txt);
6934 ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)p_frame->lines.size(), txt);
6935
6936 Line &l = p_frame->lines[p_line];
6937
6938 Item *it_to = (p_line + 1 < (int)p_frame->lines.size()) ? p_frame->lines[p_line + 1].from : nullptr;
6939 int end_idx = 0;
6940 if (it_to != nullptr) {
6941 end_idx = it_to->index;
6942 } else {
6943 for (Item *it = l.from; it; it = _get_next_item(it)) {
6944 end_idx = it->index + 1;
6945 }
6946 }
6947 for (Item *it = l.from; it && it != it_to; it = _get_next_item(it)) {
6948 if (it->type == ITEM_TABLE) {
6949 ItemTable *table = static_cast<ItemTable *>(it);
6950 for (Item *E : table->subitems) {
6951 ERR_CONTINUE(E->type != ITEM_FRAME); // Children should all be frames.
6952 ItemFrame *frame = static_cast<ItemFrame *>(E);
6953 for (int i = 0; i < (int)frame->lines.size(); i++) {
6954 txt += _get_line_text(frame, i, p_selection);
6955 }
6956 }
6957 }
6958 if ((p_selection.to_item != nullptr) && (p_selection.to_item->index < l.from->index)) {
6959 continue;
6960 }
6961 if ((p_selection.from_item != nullptr) && (p_selection.from_item->index >= end_idx)) {
6962 continue;
6963 }
6964 if (it->type == ITEM_DROPCAP) {
6965 const ItemDropcap *dc = static_cast<ItemDropcap *>(it);
6966 txt += dc->text;
6967 } else if (it->type == ITEM_TEXT) {
6968 const ItemText *t = static_cast<ItemText *>(it);
6969 txt += t->text;
6970 } else if (it->type == ITEM_NEWLINE) {
6971 txt += "\n";
6972 } else if (it->type == ITEM_IMAGE) {
6973 txt += " ";
6974 }
6975 }
6976 if ((l.from != nullptr) && (p_frame == p_selection.to_frame) && (p_selection.to_item != nullptr) && (p_selection.to_item->index >= l.from->index) && (p_selection.to_item->index < end_idx)) {
6977 txt = txt.substr(0, p_selection.to_char);
6978 }
6979 if ((l.from != nullptr) && (p_frame == p_selection.from_frame) && (p_selection.from_item != nullptr) && (p_selection.from_item->index >= l.from->index) && (p_selection.from_item->index < end_idx)) {
6980 txt = txt.substr(p_selection.from_char);
6981 }
6982 return txt;
6983 }
6984
6985 void RichTextLabel::set_context_menu_enabled(bool p_enabled) {
6986 context_menu_enabled = p_enabled;
6987 }
6988
6989 bool RichTextLabel::is_context_menu_enabled() const {
6990 return context_menu_enabled;
6991 }
6992
6993 void RichTextLabel::set_shortcut_keys_enabled(bool p_enabled) {
6994 shortcut_keys_enabled = p_enabled;
6995 }
6996
6997 bool RichTextLabel::is_shortcut_keys_enabled() const {
6998 return shortcut_keys_enabled;
6999 }
7000
7001 // Context menu.
7002 PopupMenu *RichTextLabel::get_menu() const {
7003 if (!menu) {
7004 const_cast<RichTextLabel *>(this)->_generate_context_menu();
7005 }
7006 return menu;
7007 }
7008
7009 bool RichTextLabel::is_menu_visible() const {
7010 return menu && menu->is_visible();
7011 }
7012
7013 String RichTextLabel::get_selected_text() const {
7014 if (!selection.active || !selection.enabled) {
7015 return "";
7016 }
7017
7018 String txt;
7019 int to_line = main->first_invalid_line.load();
7020 for (int i = 0; i < to_line; i++) {
7021 txt += _get_line_text(main, i, selection);
7022 }
7023
7024 if (selection_modifier.is_valid()) {
7025 txt = selection_modifier.call(txt);
7026 }
7027
7028 return txt;
7029 }
7030
7031 void RichTextLabel::deselect() {
7032 selection.active = false;
7033 queue_accessibility_update();
7034 queue_redraw();
7035 }
7036
7037 void RichTextLabel::select_all() {
7038 _validate_line_caches();
7039
7040 if (!selection.enabled) {
7041 return;
7042 }
7043
7044 Item *it = main;
7045 Item *from_item = nullptr;
7046 Item *to_item = nullptr;
7047
7048 while (it) {
7049 if (it->type != ITEM_FRAME) {
7050 if (!from_item) {
7051 from_item = it;
7052 }
7053 to_item = it;
7054 }
7055 it = _get_next_item(it, true);
7056 }
7057 if (!from_item) {
7058 return;
7059 }
7060
7061 ItemFrame *from_frame = nullptr;
7062 int from_line = 0;
7063 _find_frame(from_item, &from_frame, &from_line);
7064 if (!from_frame) {
7065 return;
7066 }
7067 ItemFrame *to_frame = nullptr;
7068 int to_line = 0;
7069 _find_frame(to_item, &to_frame, &to_line);
7070 if (!to_frame) {
7071 return;
7072 }
7073 selection.from_line = from_line;
7074 selection.from_frame = from_frame;
7075 selection.from_char = 0;
7076 selection.from_item = from_item;
7077 selection.to_line = to_line;
7078 selection.to_frame = to_frame;
7079 selection.to_char = to_frame->lines[to_line].char_count;
7080 selection.to_item = to_item;
7081 selection.active = true;
7082 queue_accessibility_update();
7083 queue_redraw();
7084 }
7085
7086 bool RichTextLabel::is_selection_enabled() const {
7087 return selection.enabled;
7088 }
7089
7090 bool RichTextLabel::is_deselect_on_focus_loss_enabled() const {
7091 return deselect_on_focus_loss_enabled;
7092 }
7093
7094 void RichTextLabel::set_drag_and_drop_selection_enabled(const bool p_enabled) {
7095 drag_and_drop_selection_enabled = p_enabled;
7096 }
7097
7098 bool RichTextLabel::is_drag_and_drop_selection_enabled() const {
7099 return drag_and_drop_selection_enabled;
7100 }
7101
7102 int RichTextLabel::get_selection_from() const {
7103 if (!selection.active || !selection.enabled) {
7104 return -1;
7105 }
7106
7107 return selection.from_frame->lines[selection.from_line].char_offset + selection.from_char;
7108 }
7109
7110 int RichTextLabel::get_selection_to() const {
7111 if (!selection.active || !selection.enabled) {
7112 return -1;
7113 }
7114
7115 return selection.to_frame->lines[selection.to_line].char_offset + selection.to_char - 1;
7116 }
7117
7118 float RichTextLabel::get_selection_line_offset() const {
7119 if (selection.active && selection.from_frame && selection.from_line >= 0 && selection.from_line < (int)selection.from_frame->lines.size()) {
7120 // Selected frame paragraph offset.
7121 float line_offset = selection.from_frame->lines[selection.from_line].offset.y;
7122
7123 // Add wrapped line offset.
7124 for (int i = 0; i < selection.from_frame->lines[selection.from_line].text_buf->get_line_count(); i++) {
7125 Vector2i range = selection.from_frame->lines[selection.from_line].text_buf->get_line_range(i);
7126 if (range.x <= selection.from_char && range.y >= selection.from_char) {
7127 break;
7128 }
7129 line_offset += selection.from_frame->lines[selection.from_line].text_buf->get_line_ascent(i) + selection.from_frame->lines[selection.from_line].text_buf->get_line_descent(i) + theme_cache.line_separation;
7130 }
7131
7132 // Add nested frame (e.g. table cell) offset.
7133 ItemFrame *it = selection.from_frame;
7134 while (it->parent_frame != nullptr) {
7135 line_offset += it->parent_frame->lines[it->line].offset.y;
7136 it = it->parent_frame;
7137 }
7138 return line_offset;
7139 }
7140
7141 return -1.0;
7142 }
7143
7144 void RichTextLabel::set_text(const String &p_bbcode) {
7145 // Allow clearing the tag stack.
7146 if (!p_bbcode.is_empty() && text == p_bbcode) {
7147 return;
7148 }
7149
7150 stack_externally_modified = false;
7151
7152 text = p_bbcode;
7153 if (text.is_empty()) {
7154 clear();
7155 } else {
7156 _apply_translation();
7157 }
7158 }
7159
7160 void RichTextLabel::_apply_translation() {
7161 if (text.is_empty()) {
7162 return;
7163 }
7164
7165 internal_stack_editing = true;
7166
7167 String xl_text = atr(text);
7168 if (use_bbcode) {
7169 parse_bbcode(xl_text);
7170 } else { // Raw text.
7171 clear();
7172 add_text(xl_text);
7173 }
7174
7175 internal_stack_editing = false;
7176 }
7177
7178 String RichTextLabel::get_text() const {
7179 return text;
7180 }
7181
7182 void RichTextLabel::set_use_bbcode(bool p_enable) {
7183 if (use_bbcode == p_enable) {
7184 return;
7185 }
7186 use_bbcode = p_enable;
7187 notify_property_list_changed();
7188
7189 if (!stack_externally_modified) {
7190 _apply_translation();
7191 }
7192 }
7193
7194 bool RichTextLabel::is_using_bbcode() const {
7195 return use_bbcode;
7196 }
7197
7198 String RichTextLabel::get_parsed_text() const {
7199 String txt;
7200 Item *it = main;
7201 while (it) {
7202 if (it->type == ITEM_DROPCAP) {
7203 ItemDropcap *dc = static_cast<ItemDropcap *>(it);
7204 txt += dc->text;
7205 } else if (it->type == ITEM_TEXT) {
7206 ItemText *t = static_cast<ItemText *>(it);
7207 txt += t->text;
7208 } else if (it->type == ITEM_NEWLINE) {
7209 txt += "\n";
7210 } else if (it->type == ITEM_IMAGE) {
7211 txt += " ";
7212 } else if (it->type == ITEM_INDENT || it->type == ITEM_LIST) {
7213 txt += "\t";
7214 }
7215 it = _get_next_item(it, true);
7216 }
7217 return txt;
7218 }
7219
7220 void RichTextLabel::set_text_direction(Control::TextDirection p_text_direction) {
7221 ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
7222 _stop_thread();
7223
7224 if (text_direction != p_text_direction) {
7225 text_direction = p_text_direction;
7226 if (!stack_externally_modified) {
7227 _apply_translation();
7228 } else {
7229 main->first_invalid_line.store(0); // Invalidate all lines.
7230 _invalidate_accessibility();
7231 _validate_line_caches();
7232 }
7233 queue_redraw();
7234 }
7235 }
7236
7237 Control::TextDirection RichTextLabel::get_text_direction() const {
7238 return text_direction;
7239 }
7240
7241 void RichTextLabel::set_horizontal_alignment(HorizontalAlignment p_alignment) {
7242 ERR_FAIL_INDEX((int)p_alignment, 4);
7243 _stop_thread();
7244
7245 if (default_alignment != p_alignment) {
7246 default_alignment = p_alignment;
7247 if (!stack_externally_modified) {
7248 _apply_translation();
7249 } else {
7250 main->first_invalid_line.store(0); // Invalidate all lines.
7251 _validate_line_caches();
7252 }
7253 queue_redraw();
7254 }
7255 }
7256
7257 HorizontalAlignment RichTextLabel::get_horizontal_alignment() const {
7258 return default_alignment;
7259 }
7260
7261 void RichTextLabel::set_vertical_alignment(VerticalAlignment p_alignment) {
7262 ERR_FAIL_INDEX((int)p_alignment, 4);
7263
7264 if (vertical_alignment == p_alignment) {
7265 return;
7266 }
7267
7268 vertical_alignment = p_alignment;
7269 queue_redraw();
7270 }
7271
7272 VerticalAlignment RichTextLabel::get_vertical_alignment() const {
7273 return vertical_alignment;
7274 }
7275
7276 void RichTextLabel::set_justification_flags(BitField<TextServer::JustificationFlag> p_flags) {
7277 _stop_thread();
7278
7279 if (default_jst_flags != p_flags) {
7280 default_jst_flags = p_flags;
7281 if (!stack_externally_modified) {
7282 _apply_translation();
7283 } else {
7284 main->first_invalid_line.store(0); // Invalidate all lines.
7285 _validate_line_caches();
7286 }
7287 queue_redraw();
7288 }
7289 }
7290
7291 BitField<TextServer::JustificationFlag> RichTextLabel::get_justification_flags() const {
7292 return default_jst_flags;
7293 }
7294
7295 void RichTextLabel::set_tab_stops(const PackedFloat32Array &p_tab_stops) {
7296 _stop_thread();
7297
7298 if (default_tab_stops != p_tab_stops) {
7299 default_tab_stops = p_tab_stops;
7300 if (!stack_externally_modified) {
7301 _apply_translation();
7302 } else {
7303 main->first_invalid_line.store(0); // Invalidate all lines.
7304 _validate_line_caches();
7305 }
7306 queue_redraw();
7307 }
7308 }
7309
7310 PackedFloat32Array RichTextLabel::get_tab_stops() const {
7311 return default_tab_stops;
7312 }
7313
7314 void RichTextLabel::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) {
7315 if (st_parser != p_parser) {
7316 _stop_thread();
7317
7318 st_parser = p_parser;
7319 if (!stack_externally_modified) {
7320 _apply_translation();
7321 } else {
7322 main->first_invalid_line.store(0); // Invalidate all lines.
7323 _invalidate_accessibility();
7324 _validate_line_caches();
7325 }
7326 queue_redraw();
7327 }
7328 }
7329
7330 TextServer::StructuredTextParser RichTextLabel::get_structured_text_bidi_override() const {
7331 return st_parser;
7332 }
7333
7334 void RichTextLabel::set_structured_text_bidi_override_options(const Array &p_args) {
7335 if (st_args != p_args) {
7336 _stop_thread();
7337
7338 st_args = Array(p_args);
7339 main->first_invalid_line.store(0); // Invalidate all lines.
7340 _invalidate_accessibility();
7341 _validate_line_caches();
7342 queue_redraw();
7343 }
7344 }
7345
7346 Array RichTextLabel::get_structured_text_bidi_override_options() const {
7347 return Array(st_args);
7348 }
7349
7350 void RichTextLabel::set_language(const String &p_language) {
7351 if (language != p_language) {
7352 _stop_thread();
7353
7354 language = p_language;
7355 if (!stack_externally_modified) {
7356 _apply_translation();
7357 } else {
7358 main->first_invalid_line.store(0); // Invalidate all lines.
7359 _invalidate_accessibility();
7360 _validate_line_caches();
7361 }
7362 queue_redraw();
7363 }
7364 }
7365
7366 String RichTextLabel::get_language() const {
7367 return language;
7368 }
7369
7370 void RichTextLabel::set_autowrap_mode(TextServer::AutowrapMode p_mode) {
7371 if (autowrap_mode != p_mode) {
7372 _stop_thread();
7373
7374 autowrap_mode = p_mode;
7375 main->first_invalid_line = 0; // Invalidate all lines.
7376 _invalidate_accessibility();
7377 _validate_line_caches();
7378 queue_redraw();
7379 }
7380 }
7381
7382 TextServer::AutowrapMode RichTextLabel::get_autowrap_mode() const {
7383 return autowrap_mode;
7384 }
7385
7386 void RichTextLabel::set_autowrap_trim_flags(BitField<TextServer::LineBreakFlag> p_flags) {
7387 if (autowrap_flags_trim != (p_flags & TextServer::BREAK_TRIM_MASK)) {
7388 _stop_thread();
7389
7390 autowrap_flags_trim = p_flags & TextServer::BREAK_TRIM_MASK;
7391 main->first_invalid_line = 0; // Invalidate all lines.
7392 _validate_line_caches();
7393 queue_redraw();
7394 }
7395 }
7396
7397 BitField<TextServer::LineBreakFlag> RichTextLabel::get_autowrap_trim_flags() const {
7398 return autowrap_flags_trim;
7399 }
7400
7401 void RichTextLabel::set_visible_ratio(float p_ratio) {
7402 if (visible_ratio != p_ratio) {
7403 _stop_thread();
7404
7405 int prev_vc = visible_characters;
7406 if (p_ratio >= 1.0) {
7407 visible_characters = -1;
7408 visible_ratio = 1.0;
7409 } else if (p_ratio < 0.0) {
7410 visible_characters = 0;
7411 visible_ratio = 0.0;
7412 } else {
7413 visible_characters = get_total_character_count() * p_ratio;
7414 visible_ratio = p_ratio;
7415 }
7416
7417 if (visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING && visible_characters != prev_vc) {
7418 int new_vc = (visible_characters < 0) ? get_total_character_count() : visible_characters;
7419 int old_vc = (prev_vc < 0) ? get_total_character_count() : prev_vc;
7420 int to_line = main->first_invalid_line.load();
7421 int old_from_l = to_line;
7422 int new_from_l = to_line;
7423 for (int i = 0; i < to_line; i++) {
7424 const Line &l = main->lines[i];
7425 if (l.char_offset <= old_vc && l.char_offset + l.char_count > old_vc) {
7426 old_from_l = i;
7427 }
7428 if (l.char_offset <= new_vc && l.char_offset + l.char_count > new_vc) {
7429 new_from_l = i;
7430 }
7431 }
7432 Rect2 text_rect = _get_text_rect();
7433 int first_invalid = MIN(new_from_l, old_from_l);
7434 int second_invalid = MAX(new_from_l, old_from_l);
7435
7436 float total_height = (first_invalid == 0) ? 0 : _calculate_line_vertical_offset(main->lines[first_invalid - 1]);
7437 if (first_invalid < to_line) {
7438 int total_chars = main->lines[first_invalid].char_offset;
7439 total_height = _shape_line(main, first_invalid, theme_cache.normal_font, theme_cache.normal_font_size, text_rect.get_size().width - scroll_w, total_height, &total_chars);
7440 }
7441 if (first_invalid != second_invalid) {
7442 for (int i = first_invalid + 1; i < second_invalid; i++) {
7443 main->lines[i].offset.y = total_height;
7444 total_height = _calculate_line_vertical_offset(main->lines[i]);
7445 }
7446 if (second_invalid < to_line) {
7447 int total_chars = main->lines[second_invalid].char_offset;
7448 total_height = _shape_line(main, second_invalid, theme_cache.normal_font, theme_cache.normal_font_size, text_rect.get_size().width - scroll_w, total_height, &total_chars);
7449 }
7450 }
7451 for (int i = second_invalid + 1; i < to_line; i++) {
7452 main->lines[i].offset.y = total_height;
7453 total_height = _calculate_line_vertical_offset(main->lines[i]);
7454 }
7455 }
7456 _update_follow_vc();
7457 queue_redraw();
7458 }
7459 }
7460
7461 float RichTextLabel::get_visible_ratio() const {
7462 return visible_ratio;
7463 }
7464
7465 void RichTextLabel::set_effects(const Array &p_effects) {
7466 custom_effects = Array(p_effects);
7467 reload_effects();
7468 }
7469
7470 Array RichTextLabel::get_effects() {
7471 return Array(custom_effects);
7472 }
7473
7474 void RichTextLabel::install_effect(const Variant effect) {
7475 Ref<RichTextEffect> rteffect;
7476 rteffect = effect;
7477
7478 ERR_FAIL_COND_MSG(rteffect.is_null(), "Invalid RichTextEffect resource.");
7479 custom_effects.push_back(effect);
7480 reload_effects();
7481 }
7482
7483 void RichTextLabel::reload_effects() {
7484 if (!stack_externally_modified && use_bbcode) {
7485 internal_stack_editing = true;
7486 parse_bbcode(atr(text));
7487 internal_stack_editing = false;
7488 }
7489 }
7490
7491 int RichTextLabel::get_content_height() const {
7492 const_cast<RichTextLabel *>(this)->_validate_line_caches();
7493
7494 int total_height = 0;
7495 int to_line = main->first_invalid_line.load();
7496 if (to_line) {
7497 MutexLock lock(main->lines[to_line - 1].text_buf->get_mutex());
7498 if (theme_cache.line_separation < 0) {
7499 // Do not apply to the last line to avoid cutting text.
7500 total_height = main->lines[to_line - 1].offset.y + main->lines[to_line - 1].text_buf->get_size().y + (main->lines[to_line - 1].text_buf->get_line_count() - 1) * theme_cache.line_separation;
7501 } else {
7502 total_height = main->lines[to_line - 1].offset.y + main->lines[to_line - 1].text_buf->get_size().y + main->lines[to_line - 1].text_buf->get_line_count() * theme_cache.line_separation + theme_cache.paragraph_separation;
7503 }
7504 }
7505 return total_height;
7506 }
7507
7508 Rect2i RichTextLabel::get_visible_content_rect() const {
7509 return visible_rect;
7510 }
7511
7512 int RichTextLabel::get_content_width() const {
7513 const_cast<RichTextLabel *>(this)->_validate_line_caches();
7514
7515 int total_width = 0;
7516 int to_line = main->first_invalid_line.load();
7517 for (int i = 0; i < to_line; i++) {
7518 MutexLock lock(main->lines[i].text_buf->get_mutex());
7519 total_width = MAX(total_width, main->lines[i].offset.x + main->lines[i].text_buf->get_size().x);
7520 }
7521 return total_width;
7522 }
7523
7524 int RichTextLabel::get_line_height(int p_line) const {
7525 const_cast<RichTextLabel *>(this)->_validate_line_caches();
7526
7527 int line_count = 0;
7528 int to_line = main->first_invalid_line.load();
7529 for (int i = 0; i < to_line; i++) {
7530 MutexLock lock(main->lines[i].text_buf->get_mutex());
7531 int lc = main->lines[i].text_buf->get_line_count();
7532
7533 if (p_line < line_count + lc) {
7534 const Ref<TextParagraph> text_buf = main->lines[i].text_buf;
7535 return text_buf->get_line_ascent(p_line - line_count) + text_buf->get_line_descent(p_line - line_count) + theme_cache.line_separation;
7536 }
7537 line_count += lc;
7538 }
7539 return 0;
7540 }
7541
7542 int RichTextLabel::get_line_width(int p_line) const {
7543 const_cast<RichTextLabel *>(this)->_validate_line_caches();
7544
7545 int line_count = 0;
7546 int to_line = main->first_invalid_line.load();
7547 for (int i = 0; i < to_line; i++) {
7548 MutexLock lock(main->lines[i].text_buf->get_mutex());
7549 int lc = main->lines[i].text_buf->get_line_count();
7550
7551 if (p_line < line_count + lc) {
7552 return main->lines[i].text_buf->get_line_width(p_line - line_count);
7553 }
7554 line_count += lc;
7555 }
7556 return 0;
7557 }
7558
7559 #ifndef DISABLE_DEPRECATED
7560 // People will be very angry, if their texts get erased, because of #39148. (3.x -> 4.0)
7561 // Although some people may not used bbcode_text, so we only overwrite, if bbcode_text is not empty.
7562 bool RichTextLabel::_set(const StringName &p_name, const Variant &p_value) {
7563 if (p_name == "bbcode_text" && !((String)p_value).is_empty()) {
7564 set_text(p_value);
7565 return true;
7566 }
7567 return false;
7568 }
7569 #endif
7570
7571 void RichTextLabel::_bind_methods() {
7572 ClassDB::bind_method(D_METHOD("get_parsed_text"), &RichTextLabel::get_parsed_text);
7573 ClassDB::bind_method(D_METHOD("add_text", "text"), &RichTextLabel::add_text);
7574 ClassDB::bind_method(D_METHOD("set_text", "text"), &RichTextLabel::set_text);
7575 ClassDB::bind_method(D_METHOD("add_hr", "width", "height", "color", "alignment", "width_in_percent", "height_in_percent"), &RichTextLabel::add_hr, DEFVAL(90), DEFVAL(2), DEFVAL(Color(1, 1, 1, 1)), DEFVAL(HORIZONTAL_ALIGNMENT_CENTER), DEFVAL(true), DEFVAL(false));
7576 ClassDB::bind_method(D_METHOD("add_image", "image", "width", "height", "color", "inline_align", "region", "key", "pad", "tooltip", "width_in_percent", "height_in_percent", "alt_text"), &RichTextLabel::add_image, DEFVAL(0), DEFVAL(0), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(Rect2()), DEFVAL(Variant()), DEFVAL(false), DEFVAL(String()), DEFVAL(false), DEFVAL(false), DEFVAL(String()));
7577 ClassDB::bind_method(D_METHOD("update_image", "key", "mask", "image", "width", "height", "color", "inline_align", "region", "pad", "tooltip", "width_in_percent", "height_in_percent"), &RichTextLabel::update_image, DEFVAL(0), DEFVAL(0), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(Rect2()), DEFVAL(false), DEFVAL(String()), DEFVAL(false), DEFVAL(false));
7578 ClassDB::bind_method(D_METHOD("newline"), &RichTextLabel::add_newline);
7579 ClassDB::bind_method(D_METHOD("remove_paragraph", "paragraph", "no_invalidate"), &RichTextLabel::remove_paragraph, DEFVAL(false));
7580 ClassDB::bind_method(D_METHOD("invalidate_paragraph", "paragraph"), &RichTextLabel::invalidate_paragraph);
7581 ClassDB::bind_method(D_METHOD("push_font", "font", "font_size"), &RichTextLabel::push_font, DEFVAL(0));
7582 ClassDB::bind_method(D_METHOD("push_font_size", "font_size"), &RichTextLabel::push_font_size);
7583 ClassDB::bind_method(D_METHOD("push_normal"), &RichTextLabel::push_normal);
7584 ClassDB::bind_method(D_METHOD("push_bold"), &RichTextLabel::push_bold);
7585 ClassDB::bind_method(D_METHOD("push_bold_italics"), &RichTextLabel::push_bold_italics);
7586 ClassDB::bind_method(D_METHOD("push_italics"), &RichTextLabel::push_italics);
7587 ClassDB::bind_method(D_METHOD("push_mono"), &RichTextLabel::push_mono);
7588 ClassDB::bind_method(D_METHOD("push_color", "color"), &RichTextLabel::push_color);
7589 ClassDB::bind_method(D_METHOD("push_outline_size", "outline_size"), &RichTextLabel::push_outline_size);
7590 ClassDB::bind_method(D_METHOD("push_outline_color", "color"), &RichTextLabel::push_outline_color);
7591 ClassDB::bind_method(D_METHOD("push_paragraph", "alignment", "base_direction", "language", "st_parser", "justification_flags", "tab_stops"), &RichTextLabel::push_paragraph, DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(""), DEFVAL(TextServer::STRUCTURED_TEXT_DEFAULT), DEFVAL(TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_SKIP_LAST_LINE | TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE), DEFVAL(PackedFloat32Array()));
7592 ClassDB::bind_method(D_METHOD("push_indent", "level"), &RichTextLabel::push_indent);
7593 ClassDB::bind_method(D_METHOD("push_list", "level", "type", "capitalize", "bullet"), &RichTextLabel::push_list, DEFVAL(String::utf8("•")));
7594 ClassDB::bind_method(D_METHOD("push_meta", "data", "underline_mode", "tooltip"), &RichTextLabel::push_meta, DEFVAL(META_UNDERLINE_ALWAYS), DEFVAL(String()));
7595 ClassDB::bind_method(D_METHOD("push_hint", "description"), &RichTextLabel::push_hint);
7596 ClassDB::bind_method(D_METHOD("push_language", "language"), &RichTextLabel::push_language);
7597 ClassDB::bind_method(D_METHOD("push_underline", "color"), &RichTextLabel::push_underline, DEFVAL(Color(0, 0, 0, 0)));
7598 ClassDB::bind_method(D_METHOD("push_strikethrough", "color"), &RichTextLabel::push_strikethrough, DEFVAL(Color(0, 0, 0, 0)));
7599 ClassDB::bind_method(D_METHOD("push_table", "columns", "inline_align", "align_to_row", "name"), &RichTextLabel::push_table, DEFVAL(INLINE_ALIGNMENT_TOP), DEFVAL(-1), DEFVAL(String()));
7600 ClassDB::bind_method(D_METHOD("push_dropcap", "string", "font", "size", "dropcap_margins", "color", "outline_size", "outline_color"), &RichTextLabel::push_dropcap, DEFVAL(Rect2()), DEFVAL(Color(1, 1, 1)), DEFVAL(0), DEFVAL(Color(0, 0, 0, 0)));
7601 ClassDB::bind_method(D_METHOD("set_table_column_expand", "column", "expand", "ratio", "shrink"), &RichTextLabel::set_table_column_expand, DEFVAL(1), DEFVAL(true));
7602 ClassDB::bind_method(D_METHOD("set_table_column_name", "column", "name"), &RichTextLabel::set_table_column_name);
7603 ClassDB::bind_method(D_METHOD("set_cell_row_background_color", "odd_row_bg", "even_row_bg"), &RichTextLabel::set_cell_row_background_color);
7604 ClassDB::bind_method(D_METHOD("set_cell_border_color", "color"), &RichTextLabel::set_cell_border_color);
7605 ClassDB::bind_method(D_METHOD("set_cell_size_override", "min_size", "max_size"), &RichTextLabel::set_cell_size_override);
7606 ClassDB::bind_method(D_METHOD("set_cell_padding", "padding"), &RichTextLabel::set_cell_padding);
7607 ClassDB::bind_method(D_METHOD("push_cell"), &RichTextLabel::push_cell);
7608 ClassDB::bind_method(D_METHOD("push_fgcolor", "fgcolor"), &RichTextLabel::push_fgcolor);
7609 ClassDB::bind_method(D_METHOD("push_bgcolor", "bgcolor"), &RichTextLabel::push_bgcolor);
7610 ClassDB::bind_method(D_METHOD("push_customfx", "effect", "env"), &RichTextLabel::push_customfx);
7611 ClassDB::bind_method(D_METHOD("push_context"), &RichTextLabel::push_context);
7612 ClassDB::bind_method(D_METHOD("pop_context"), &RichTextLabel::pop_context);
7613 ClassDB::bind_method(D_METHOD("pop"), &RichTextLabel::pop);
7614 ClassDB::bind_method(D_METHOD("pop_all"), &RichTextLabel::pop_all);
7615
7616 ClassDB::bind_method(D_METHOD("clear"), &RichTextLabel::clear);
7617
7618 ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &RichTextLabel::set_structured_text_bidi_override);
7619 ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &RichTextLabel::get_structured_text_bidi_override);
7620 ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &RichTextLabel::set_structured_text_bidi_override_options);
7621 ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &RichTextLabel::get_structured_text_bidi_override_options);
7622 ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &RichTextLabel::set_text_direction);
7623 ClassDB::bind_method(D_METHOD("get_text_direction"), &RichTextLabel::get_text_direction);
7624 ClassDB::bind_method(D_METHOD("set_language", "language"), &RichTextLabel::set_language);
7625 ClassDB::bind_method(D_METHOD("get_language"), &RichTextLabel::get_language);
7626
7627 ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &RichTextLabel::set_horizontal_alignment);
7628 ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &RichTextLabel::get_horizontal_alignment);
7629 ClassDB::bind_method(D_METHOD("set_vertical_alignment", "alignment"), &RichTextLabel::set_vertical_alignment);
7630 ClassDB::bind_method(D_METHOD("get_vertical_alignment"), &RichTextLabel::get_vertical_alignment);
7631 ClassDB::bind_method(D_METHOD("set_justification_flags", "justification_flags"), &RichTextLabel::set_justification_flags);
7632 ClassDB::bind_method(D_METHOD("get_justification_flags"), &RichTextLabel::get_justification_flags);
7633 ClassDB::bind_method(D_METHOD("set_tab_stops", "tab_stops"), &RichTextLabel::set_tab_stops);
7634 ClassDB::bind_method(D_METHOD("get_tab_stops"), &RichTextLabel::get_tab_stops);
7635
7636 ClassDB::bind_method(D_METHOD("set_autowrap_mode", "autowrap_mode"), &RichTextLabel::set_autowrap_mode);
7637 ClassDB::bind_method(D_METHOD("get_autowrap_mode"), &RichTextLabel::get_autowrap_mode);
7638
7639 ClassDB::bind_method(D_METHOD("set_autowrap_trim_flags", "autowrap_trim_flags"), &RichTextLabel::set_autowrap_trim_flags);
7640 ClassDB::bind_method(D_METHOD("get_autowrap_trim_flags"), &RichTextLabel::get_autowrap_trim_flags);
7641
7642 ClassDB::bind_method(D_METHOD("set_meta_underline", "enable"), &RichTextLabel::set_meta_underline);
7643 ClassDB::bind_method(D_METHOD("is_meta_underlined"), &RichTextLabel::is_meta_underlined);
7644
7645 ClassDB::bind_method(D_METHOD("set_hint_underline", "enable"), &RichTextLabel::set_hint_underline);
7646 ClassDB::bind_method(D_METHOD("is_hint_underlined"), &RichTextLabel::is_hint_underlined);
7647
7648 ClassDB::bind_method(D_METHOD("set_scroll_active", "active"), &RichTextLabel::set_scroll_active);
7649 ClassDB::bind_method(D_METHOD("is_scroll_active"), &RichTextLabel::is_scroll_active);
7650
7651 ClassDB::bind_method(D_METHOD("set_scroll_follow_visible_characters", "follow"), &RichTextLabel::set_scroll_follow_visible_characters);
7652 ClassDB::bind_method(D_METHOD("is_scroll_following_visible_characters"), &RichTextLabel::is_scroll_following_visible_characters);
7653
7654 ClassDB::bind_method(D_METHOD("set_scroll_follow", "follow"), &RichTextLabel::set_scroll_follow);
7655 ClassDB::bind_method(D_METHOD("is_scroll_following"), &RichTextLabel::is_scroll_following);
7656
7657 ClassDB::bind_method(D_METHOD("get_v_scroll_bar"), &RichTextLabel::get_v_scroll_bar);
7658
7659 ClassDB::bind_method(D_METHOD("scroll_to_line", "line"), &RichTextLabel::scroll_to_line);
7660 ClassDB::bind_method(D_METHOD("scroll_to_paragraph", "paragraph"), &RichTextLabel::scroll_to_paragraph);
7661 ClassDB::bind_method(D_METHOD("scroll_to_selection"), &RichTextLabel::scroll_to_selection);
7662
7663 ClassDB::bind_method(D_METHOD("set_tab_size", "spaces"), &RichTextLabel::set_tab_size);
7664 ClassDB::bind_method(D_METHOD("get_tab_size"), &RichTextLabel::get_tab_size);
7665
7666 ClassDB::bind_method(D_METHOD("set_fit_content", "enabled"), &RichTextLabel::set_fit_content);
7667 ClassDB::bind_method(D_METHOD("is_fit_content_enabled"), &RichTextLabel::is_fit_content_enabled);
7668
7669 ClassDB::bind_method(D_METHOD("set_selection_enabled", "enabled"), &RichTextLabel::set_selection_enabled);
7670 ClassDB::bind_method(D_METHOD("is_selection_enabled"), &RichTextLabel::is_selection_enabled);
7671
7672 ClassDB::bind_method(D_METHOD("set_context_menu_enabled", "enabled"), &RichTextLabel::set_context_menu_enabled);
7673 ClassDB::bind_method(D_METHOD("is_context_menu_enabled"), &RichTextLabel::is_context_menu_enabled);
7674
7675 ClassDB::bind_method(D_METHOD("set_shortcut_keys_enabled", "enabled"), &RichTextLabel::set_shortcut_keys_enabled);
7676 ClassDB::bind_method(D_METHOD("is_shortcut_keys_enabled"), &RichTextLabel::is_shortcut_keys_enabled);
7677
7678 ClassDB::bind_method(D_METHOD("set_deselect_on_focus_loss_enabled", "enable"), &RichTextLabel::set_deselect_on_focus_loss_enabled);
7679 ClassDB::bind_method(D_METHOD("is_deselect_on_focus_loss_enabled"), &RichTextLabel::is_deselect_on_focus_loss_enabled);
7680
7681 ClassDB::bind_method(D_METHOD("set_drag_and_drop_selection_enabled", "enable"), &RichTextLabel::set_drag_and_drop_selection_enabled);
7682 ClassDB::bind_method(D_METHOD("is_drag_and_drop_selection_enabled"), &RichTextLabel::is_drag_and_drop_selection_enabled);
7683
7684 ClassDB::bind_method(D_METHOD("get_selection_from"), &RichTextLabel::get_selection_from);
7685 ClassDB::bind_method(D_METHOD("get_selection_to"), &RichTextLabel::get_selection_to);
7686 ClassDB::bind_method(D_METHOD("get_selection_line_offset"), &RichTextLabel::get_selection_line_offset);
7687
7688 ClassDB::bind_method(D_METHOD("select_all"), &RichTextLabel::select_all);
7689 ClassDB::bind_method(D_METHOD("get_selected_text"), &RichTextLabel::get_selected_text);
7690 ClassDB::bind_method(D_METHOD("deselect"), &RichTextLabel::deselect);
7691
7692 ClassDB::bind_method(D_METHOD("parse_bbcode", "bbcode"), &RichTextLabel::parse_bbcode);
7693 ClassDB::bind_method(D_METHOD("append_text", "bbcode"), &RichTextLabel::append_text);
7694
7695 ClassDB::bind_method(D_METHOD("get_text"), &RichTextLabel::get_text);
7696
7697 #ifndef DISABLE_DEPRECATED
7698 ClassDB::bind_method(D_METHOD("is_ready"), &RichTextLabel::is_finished);
7699 #endif // DISABLE_DEPRECATED
7700 ClassDB::bind_method(D_METHOD("is_finished"), &RichTextLabel::is_finished);
7701
7702 ClassDB::bind_method(D_METHOD("set_threaded", "threaded"), &RichTextLabel::set_threaded);
7703 ClassDB::bind_method(D_METHOD("is_threaded"), &RichTextLabel::is_threaded);
7704
7705 ClassDB::bind_method(D_METHOD("set_progress_bar_delay", "delay_ms"), &RichTextLabel::set_progress_bar_delay);
7706 ClassDB::bind_method(D_METHOD("get_progress_bar_delay"), &RichTextLabel::get_progress_bar_delay);
7707
7708 ClassDB::bind_method(D_METHOD("set_visible_characters", "amount"), &RichTextLabel::set_visible_characters);
7709 ClassDB::bind_method(D_METHOD("get_visible_characters"), &RichTextLabel::get_visible_characters);
7710
7711 ClassDB::bind_method(D_METHOD("get_visible_characters_behavior"), &RichTextLabel::get_visible_characters_behavior);
7712 ClassDB::bind_method(D_METHOD("set_visible_characters_behavior", "behavior"), &RichTextLabel::set_visible_characters_behavior);
7713
7714 ClassDB::bind_method(D_METHOD("set_visible_ratio", "ratio"), &RichTextLabel::set_visible_ratio);
7715 ClassDB::bind_method(D_METHOD("get_visible_ratio"), &RichTextLabel::get_visible_ratio);
7716
7717 ClassDB::bind_method(D_METHOD("get_character_line", "character"), &RichTextLabel::get_character_line);
7718 ClassDB::bind_method(D_METHOD("get_character_paragraph", "character"), &RichTextLabel::get_character_paragraph);
7719 ClassDB::bind_method(D_METHOD("get_total_character_count"), &RichTextLabel::get_total_character_count);
7720
7721 ClassDB::bind_method(D_METHOD("set_use_bbcode", "enable"), &RichTextLabel::set_use_bbcode);
7722 ClassDB::bind_method(D_METHOD("is_using_bbcode"), &RichTextLabel::is_using_bbcode);
7723
7724 ClassDB::bind_method(D_METHOD("get_line_count"), &RichTextLabel::get_line_count);
7725 ClassDB::bind_method(D_METHOD("get_line_range", "line"), &RichTextLabel::get_line_range);
7726 ClassDB::bind_method(D_METHOD("get_visible_line_count"), &RichTextLabel::get_visible_line_count);
7727
7728 ClassDB::bind_method(D_METHOD("get_paragraph_count"), &RichTextLabel::get_paragraph_count);
7729 ClassDB::bind_method(D_METHOD("get_visible_paragraph_count"), &RichTextLabel::get_visible_paragraph_count);
7730
7731 ClassDB::bind_method(D_METHOD("get_content_height"), &RichTextLabel::get_content_height);
7732 ClassDB::bind_method(D_METHOD("get_content_width"), &RichTextLabel::get_content_width);
7733
7734 ClassDB::bind_method(D_METHOD("get_line_height", "line"), &RichTextLabel::get_line_height);
7735 ClassDB::bind_method(D_METHOD("get_line_width", "line"), &RichTextLabel::get_line_width);
7736
7737 ClassDB::bind_method(D_METHOD("get_visible_content_rect"), &RichTextLabel::get_visible_content_rect);
7738
7739 ClassDB::bind_method(D_METHOD("get_line_offset", "line"), &RichTextLabel::get_line_offset);
7740 ClassDB::bind_method(D_METHOD("get_paragraph_offset", "paragraph"), &RichTextLabel::get_paragraph_offset);
7741
7742 ClassDB::bind_method(D_METHOD("parse_expressions_for_values", "expressions"), &RichTextLabel::parse_expressions_for_values);
7743
7744 ClassDB::bind_method(D_METHOD("set_effects", "effects"), &RichTextLabel::set_effects);
7745 ClassDB::bind_method(D_METHOD("get_effects"), &RichTextLabel::get_effects);
7746 ClassDB::bind_method(D_METHOD("install_effect", "effect"), &RichTextLabel::install_effect);
7747 ClassDB::bind_method(D_METHOD("reload_effects"), &RichTextLabel::reload_effects);
7748
7749 ClassDB::bind_method(D_METHOD("get_menu"), &RichTextLabel::get_menu);
7750 ClassDB::bind_method(D_METHOD("is_menu_visible"), &RichTextLabel::is_menu_visible);
7751 ClassDB::bind_method(D_METHOD("menu_option", "option"), &RichTextLabel::menu_option);
7752
7753 // Note: set "bbcode_enabled" first, to avoid unnecessary "text" resets.
7754 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bbcode_enabled"), "set_use_bbcode", "is_using_bbcode");
7755 ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
7756
7757 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "fit_content"), "set_fit_content", "is_fit_content_enabled");
7758 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_active"), "set_scroll_active", "is_scroll_active");
7759 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_following"), "set_scroll_follow", "is_scroll_following");
7760 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_following_visible_characters"), "set_scroll_follow_visible_characters", "is_scroll_following_visible_characters");
7761 ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_mode", PROPERTY_HINT_ENUM, "Off,Arbitrary,Word,Word (Smart)"), "set_autowrap_mode", "get_autowrap_mode");
7762 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");
7763 ADD_PROPERTY(PropertyInfo(Variant::INT, "tab_size", PROPERTY_HINT_RANGE, "0,24,1"), "set_tab_size", "get_tab_size");
7764 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "context_menu_enabled"), "set_context_menu_enabled", "is_context_menu_enabled");
7765 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_keys_enabled"), "set_shortcut_keys_enabled", "is_shortcut_keys_enabled");
7766
7767 ADD_PROPERTY(PropertyInfo(Variant::INT, "horizontal_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_horizontal_alignment", "get_horizontal_alignment");
7768 ADD_PROPERTY(PropertyInfo(Variant::INT, "vertical_alignment", PROPERTY_HINT_ENUM, "Top,Center,Bottom,Fill"), "set_vertical_alignment", "get_vertical_alignment");
7769 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");
7770 ADD_PROPERTY(PropertyInfo(Variant::PACKED_FLOAT32_ARRAY, "tab_stops"), "set_tab_stops", "get_tab_stops");
7771
7772 ADD_GROUP("Markup", "");
7773 ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "custom_effects", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("RichTextEffect"), (PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_SCRIPT_VARIABLE)), "set_effects", "get_effects");
7774 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "meta_underlined"), "set_meta_underline", "is_meta_underlined");
7775 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hint_underlined"), "set_hint_underline", "is_hint_underlined");
7776
7777 ADD_GROUP("Threading", "");
7778 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "threaded"), "set_threaded", "is_threaded");
7779 ADD_PROPERTY(PropertyInfo(Variant::INT, "progress_bar_delay", PROPERTY_HINT_NONE, "suffix:ms"), "set_progress_bar_delay", "get_progress_bar_delay");
7780
7781 ADD_GROUP("Text Selection", "");
7782 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selection_enabled", PROPERTY_HINT_GROUP_ENABLE), "set_selection_enabled", "is_selection_enabled");
7783 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deselect_on_focus_loss_enabled"), "set_deselect_on_focus_loss_enabled", "is_deselect_on_focus_loss_enabled");
7784 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_and_drop_selection_enabled"), "set_drag_and_drop_selection_enabled", "is_drag_and_drop_selection_enabled");
7785
7786 ADD_GROUP("Displayed Text", "");
7787 // Note: "visible_characters" and "visible_ratio" should be set after "text" to be correctly applied.
7788 ADD_PROPERTY(PropertyInfo(Variant::INT, "visible_characters", PROPERTY_HINT_RANGE, "-1,128000,1"), "set_visible_characters", "get_visible_characters");
7789 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");
7790 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "visible_ratio", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_visible_ratio", "get_visible_ratio");
7791
7792 ADD_GROUP("BiDi", "");
7793 ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
7794 ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
7795 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");
7796 ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");
7797
7798 ADD_SIGNAL(MethodInfo("meta_clicked", PropertyInfo(Variant::NIL, "meta", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
7799 ADD_SIGNAL(MethodInfo("meta_hover_started", PropertyInfo(Variant::NIL, "meta", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
7800 ADD_SIGNAL(MethodInfo("meta_hover_ended", PropertyInfo(Variant::NIL, "meta", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
7801
7802 ADD_SIGNAL(MethodInfo("finished"));
7803
7804 BIND_ENUM_CONSTANT(LIST_NUMBERS);
7805 BIND_ENUM_CONSTANT(LIST_LETTERS);
7806 BIND_ENUM_CONSTANT(LIST_ROMAN);
7807 BIND_ENUM_CONSTANT(LIST_DOTS);
7808
7809 BIND_ENUM_CONSTANT(MENU_COPY);
7810 BIND_ENUM_CONSTANT(MENU_SELECT_ALL);
7811 BIND_ENUM_CONSTANT(MENU_MAX);
7812
7813 BIND_ENUM_CONSTANT(META_UNDERLINE_NEVER);
7814 BIND_ENUM_CONSTANT(META_UNDERLINE_ALWAYS);
7815 BIND_ENUM_CONSTANT(META_UNDERLINE_ON_HOVER);
7816
7817 BIND_BITFIELD_FLAG(UPDATE_TEXTURE);
7818 BIND_BITFIELD_FLAG(UPDATE_SIZE);
7819 BIND_BITFIELD_FLAG(UPDATE_COLOR);
7820 BIND_BITFIELD_FLAG(UPDATE_ALIGNMENT);
7821 BIND_BITFIELD_FLAG(UPDATE_REGION);
7822 BIND_BITFIELD_FLAG(UPDATE_PAD);
7823 BIND_BITFIELD_FLAG(UPDATE_TOOLTIP);
7824 BIND_BITFIELD_FLAG(UPDATE_WIDTH_IN_PERCENT);
7825
7826 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, RichTextLabel, normal_style, "normal");
7827 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, RichTextLabel, focus_style, "focus");
7828 BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, RichTextLabel, progress_bg_style, "background", "ProgressBar");
7829 BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, RichTextLabel, progress_fg_style, "fill", "ProgressBar");
7830
7831 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, RichTextLabel, horizontal_rule, "horizontal_rule");
7832
7833 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, RichTextLabel, line_separation);
7834 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, RichTextLabel, paragraph_separation);
7835
7836 BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, RichTextLabel, normal_font);
7837 BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, RichTextLabel, normal_font_size);
7838
7839 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, RichTextLabel, default_color);
7840 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, RichTextLabel, font_selected_color);
7841 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, RichTextLabel, selection_color);
7842 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, RichTextLabel, font_outline_color);
7843 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, RichTextLabel, font_shadow_color);
7844 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, RichTextLabel, shadow_outline_size);
7845 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, RichTextLabel, shadow_offset_x);
7846 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, RichTextLabel, shadow_offset_y);
7847 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, RichTextLabel, outline_size);
7848
7849 BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, RichTextLabel, bold_font);
7850 BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, RichTextLabel, bold_font_size);
7851 BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, RichTextLabel, bold_italics_font);
7852 BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, RichTextLabel, bold_italics_font_size);
7853 BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, RichTextLabel, italics_font);
7854 BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, RichTextLabel, italics_font_size);
7855 BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, RichTextLabel, mono_font);
7856 BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, RichTextLabel, mono_font_size);
7857
7858 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, RichTextLabel, text_highlight_h_padding);
7859 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, RichTextLabel, text_highlight_v_padding);
7860
7861 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, RichTextLabel, underline_alpha);
7862 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, RichTextLabel, strikethrough_alpha);
7863
7864 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, RichTextLabel, table_h_separation);
7865 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, RichTextLabel, table_v_separation);
7866 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, RichTextLabel, table_odd_row_bg);
7867 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, RichTextLabel, table_even_row_bg);
7868 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, RichTextLabel, table_border);
7869
7870 ADD_CLASS_DEPENDENCY("PopupMenu");
7871 #ifdef MODULE_REGEX_ENABLED
7872 ADD_CLASS_DEPENDENCY("RegEx");
7873 #endif
7874 }
7875
7876 TextServer::VisibleCharactersBehavior RichTextLabel::get_visible_characters_behavior() const {
7877 return visible_chars_behavior;
7878 }
7879
7880 void RichTextLabel::set_visible_characters_behavior(TextServer::VisibleCharactersBehavior p_behavior) {
7881 if (visible_chars_behavior != p_behavior) {
7882 _stop_thread();
7883
7884 visible_chars_behavior = p_behavior;
7885 main->first_invalid_line.store(0); // Invalidate all lines.
7886 _invalidate_accessibility();
7887 _validate_line_caches();
7888 queue_redraw();
7889 }
7890 }
7891
7892 void RichTextLabel::set_visible_characters(int p_visible) {
7893 if (visible_characters != p_visible) {
7894 _stop_thread();
7895
7896 int prev_vc = visible_characters;
7897 visible_characters = p_visible;
7898 if (p_visible == -1) {
7899 visible_ratio = 1;
7900 } else {
7901 int total_char_count = get_total_character_count();
7902 if (total_char_count > 0) {
7903 visible_ratio = (float)p_visible / (float)total_char_count;
7904 }
7905 }
7906 if (visible_chars_behavior == TextServer::VC_CHARS_BEFORE_SHAPING && visible_characters != prev_vc) {
7907 int new_vc = (visible_characters < 0) ? get_total_character_count() : visible_characters;
7908 int old_vc = (prev_vc < 0) ? get_total_character_count() : prev_vc;
7909 int to_line = main->first_invalid_line.load();
7910 int old_from_l = to_line;
7911 int new_from_l = to_line;
7912 for (int i = 0; i < to_line; i++) {
7913 const Line &l = main->lines[i];
7914 if (l.char_offset <= old_vc && l.char_offset + l.char_count > old_vc) {
7915 old_from_l = i;
7916 }
7917 if (l.char_offset <= new_vc && l.char_offset + l.char_count > new_vc) {
7918 new_from_l = i;
7919 }
7920 }
7921 Rect2 text_rect = _get_text_rect();
7922 int first_invalid = MIN(new_from_l, old_from_l);
7923 int second_invalid = MAX(new_from_l, old_from_l);
7924
7925 float total_height = (first_invalid == 0) ? 0 : _calculate_line_vertical_offset(main->lines[first_invalid - 1]);
7926 if (first_invalid < to_line) {
7927 int total_chars = main->lines[first_invalid].char_offset;
7928 total_height = _shape_line(main, first_invalid, theme_cache.normal_font, theme_cache.normal_font_size, text_rect.get_size().width - scroll_w, total_height, &total_chars);
7929 }
7930 if (first_invalid != second_invalid) {
7931 for (int i = first_invalid + 1; i < second_invalid; i++) {
7932 main->lines[i].offset.y = total_height;
7933 total_height = _calculate_line_vertical_offset(main->lines[i]);
7934 }
7935 if (second_invalid < to_line) {
7936 int total_chars = main->lines[second_invalid].char_offset;
7937 total_height = _shape_line(main, second_invalid, theme_cache.normal_font, theme_cache.normal_font_size, text_rect.get_size().width - scroll_w, total_height, &total_chars);
7938 }
7939 }
7940 for (int i = second_invalid + 1; i < to_line; i++) {
7941 main->lines[i].offset.y = total_height;
7942 total_height = _calculate_line_vertical_offset(main->lines[i]);
7943 }
7944 }
7945 _update_follow_vc();
7946 queue_redraw();
7947 }
7948 }
7949
7950 int RichTextLabel::get_visible_characters() const {
7951 return visible_characters;
7952 }
7953
7954 int RichTextLabel::get_character_line(int p_char) {
7955 _validate_line_caches();
7956
7957 int line_count = 0;
7958 int to_line = main->first_invalid_line.load();
7959 for (int i = 0; i < to_line; i++) {
7960 MutexLock lock(main->lines[i].text_buf->get_mutex());
7961 int char_offset = main->lines[i].char_offset;
7962 int char_count = main->lines[i].char_count;
7963 if (char_offset <= p_char && p_char < char_offset + char_count) {
7964 int lc = main->lines[i].text_buf->get_line_count();
7965 for (int j = 0; j < lc; j++) {
7966 Vector2i range = main->lines[i].text_buf->get_line_range(j);
7967 if (char_offset + range.x <= p_char && p_char < char_offset + range.y) {
7968 break;
7969 }
7970 if (char_offset + range.x > p_char && line_count > 0) {
7971 line_count--; // Character is not rendered and is between the lines (e.g., edge space).
7972 break;
7973 }
7974 if (j != lc - 1) {
7975 line_count++;
7976 }
7977 }
7978 return line_count;
7979 } else {
7980 line_count += main->lines[i].text_buf->get_line_count();
7981 }
7982 }
7983 return -1;
7984 }
7985
7986 int RichTextLabel::get_character_paragraph(int p_char) {
7987 _validate_line_caches();
7988
7989 int to_line = main->first_invalid_line.load();
7990 for (int i = 0; i < to_line; i++) {
7991 int char_offset = main->lines[i].char_offset;
7992 if (char_offset <= p_char && p_char < char_offset + main->lines[i].char_count) {
7993 return i;
7994 }
7995 }
7996 return -1;
7997 }
7998
7999 int RichTextLabel::get_total_character_count() const {
8000 // Note: Do not use line buffer "char_count", it includes only visible characters.
8001 int tc = 0;
8002 Item *it = main;
8003 while (it) {
8004 if (it->type == ITEM_TEXT) {
8005 ItemText *t = static_cast<ItemText *>(it);
8006 tc += t->text.length();
8007 } else if (it->type == ITEM_NEWLINE) {
8008 tc++;
8009 } else if (it->type == ITEM_IMAGE) {
8010 tc++;
8011 }
8012 it = _get_next_item(it, true);
8013 }
8014 return tc;
8015 }
8016
8017 int RichTextLabel::get_total_glyph_count() const {
8018 const_cast<RichTextLabel *>(this)->_validate_line_caches();
8019
8020 int tg = 0;
8021 Item *it = main;
8022 while (it) {
8023 if (it->type == ITEM_FRAME) {
8024 ItemFrame *f = static_cast<ItemFrame *>(it);
8025 for (int i = 0; i < (int)f->lines.size(); i++) {
8026 MutexLock lock(f->lines[i].text_buf->get_mutex());
8027 tg += TS->shaped_text_get_glyph_count(f->lines[i].text_buf->get_rid());
8028 }
8029 }
8030 it = _get_next_item(it, true);
8031 }
8032
8033 return tg;
8034 }
8035
8036 Size2 RichTextLabel::get_minimum_size() const {
8037 Size2 sb_min_size = theme_cache.normal_style->get_minimum_size();
8038 Size2 min_size;
8039
8040 if (fit_content) {
8041 min_size.x = get_content_width();
8042 min_size.y = get_content_height();
8043 }
8044
8045 return sb_min_size +
8046 ((autowrap_mode != TextServer::AUTOWRAP_OFF) ? Size2(1, min_size.height) : min_size);
8047 }
8048
8049 // Context menu.
8050 void RichTextLabel::_generate_context_menu() {
8051 menu = memnew(PopupMenu);
8052 add_child(menu, false, INTERNAL_MODE_FRONT);
8053 menu->connect(SceneStringName(id_pressed), callable_mp(this, &RichTextLabel::menu_option));
8054
8055 menu->add_item(ETR("Copy"), MENU_COPY);
8056 menu->add_item(ETR("Select All"), MENU_SELECT_ALL);
8057 }
8058
8059 void RichTextLabel::_update_context_menu() {
8060 if (!menu) {
8061 _generate_context_menu();
8062 }
8063
8064 int idx = -1;
8065
8066 #define MENU_ITEM_ACTION_DISABLED(m_menu, m_id, m_action, m_disabled) \
8067 idx = m_menu->get_item_index(m_id); \
8068 if (idx >= 0) { \
8069 m_menu->set_item_accelerator(idx, shortcut_keys_enabled ? _get_menu_action_accelerator(m_action) : Key::NONE); \
8070 m_menu->set_item_disabled(idx, m_disabled); \
8071 }
8072
8073 MENU_ITEM_ACTION_DISABLED(menu, MENU_COPY, "ui_copy", !selection.enabled)
8074 MENU_ITEM_ACTION_DISABLED(menu, MENU_SELECT_ALL, "ui_text_select_all", !selection.enabled)
8075
8076 #undef MENU_ITEM_ACTION_DISABLED
8077 }
8078
8079 Key RichTextLabel::_get_menu_action_accelerator(const String &p_action) {
8080 const List<Ref<InputEvent>> *events = InputMap::get_singleton()->action_get_events(p_action);
8081 if (!events) {
8082 return Key::NONE;
8083 }
8084
8085 // Use first event in the list for the accelerator.
8086 const List<Ref<InputEvent>>::Element *first_event = events->front();
8087 if (!first_event) {
8088 return Key::NONE;
8089 }
8090
8091 const Ref<InputEventKey> event = first_event->get();
8092 if (event.is_null()) {
8093 return Key::NONE;
8094 }
8095
8096 // Use physical keycode if non-zero
8097 if (event->get_physical_keycode() != Key::NONE) {
8098 return event->get_physical_keycode_with_modifiers();
8099 } else {
8100 return event->get_keycode_with_modifiers();
8101 }
8102 }
8103
8104 void RichTextLabel::menu_option(int p_option) {
8105 switch (p_option) {
8106 case MENU_COPY: {
8107 String txt = get_selected_text();
8108 if (txt.is_empty()) {
8109 txt = get_parsed_text();
8110 }
8111
8112 if (!txt.is_empty()) {
8113 DisplayServer::get_singleton()->clipboard_set(txt);
8114 }
8115 } break;
8116 case MENU_SELECT_ALL: {
8117 select_all();
8118 } break;
8119 }
8120 }
8121
8122 Ref<RichTextEffect> RichTextLabel::_get_custom_effect_by_code(String p_bbcode_identifier) {
8123 for (int i = 0; i < custom_effects.size(); i++) {
8124 Ref<RichTextEffect> effect = custom_effects[i];
8125 if (effect.is_null()) {
8126 continue;
8127 }
8128
8129 if (effect->get_bbcode() == p_bbcode_identifier) {
8130 return effect;
8131 }
8132 }
8133
8134 return Ref<RichTextEffect>();
8135 }
8136
8137 Dictionary RichTextLabel::parse_expressions_for_values(Vector<String> p_expressions) {
8138 Dictionary d;
8139 for (int i = 0; i < p_expressions.size(); i++) {
8140 Array a;
8141 Vector<String> parts = p_expressions[i].split("=", true);
8142 const String &key = parts[0];
8143 if (parts.size() != 2) {
8144 return d;
8145 }
8146
8147 Vector<String> values = parts[1].split(",", false);
8148
8149 #ifdef MODULE_REGEX_ENABLED
8150 RegEx color = RegEx();
8151 color.compile("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$");
8152 RegEx nodepath = RegEx();
8153 nodepath.compile("^\\$");
8154 RegEx boolean = RegEx();
8155 boolean.compile("^(true|false)$");
8156 RegEx numerical = RegEx();
8157 numerical.compile("^[-+]?\\d+$");
8158 RegEx decimal = RegEx();
8159 decimal.compile("^[+-]?\\d*(\\.\\d*)?([eE][+-]?\\d+)?$");
8160
8161 for (int j = 0; j < values.size(); j++) {
8162 if (color.search(values[j]).is_valid()) {
8163 a.append(Color::html(values[j]));
8164 } else if (nodepath.search(values[j]).is_valid()) {
8165 if (values[j].begins_with("$")) {
8166 String v = values[j].substr(1);
8167 a.append(NodePath(v));
8168 }
8169 } else if (boolean.search(values[j]).is_valid()) {
8170 if (values[j] == "true") {
8171 a.append(true);
8172 } else if (values[j] == "false") {
8173 a.append(false);
8174 }
8175 } else if (numerical.search(values[j]).is_valid()) {
8176 a.append(values[j].to_int());
8177 } else if (decimal.search(values[j]).is_valid()) {
8178 a.append(values[j].to_float());
8179 } else {
8180 a.append(values[j]);
8181 }
8182 }
8183 #endif
8184
8185 if (values.size() > 1) {
8186 d[key] = a;
8187 } else if (values.size() == 1) {
8188 d[key] = a[0];
8189 }
8190 }
8191 return d;
8192 }
8193
8194 RichTextLabel::RichTextLabel(const String &p_text) {
8195 main = memnew(ItemFrame);
8196 main->owner = get_instance_id();
8197 main->rid = items.make_rid(main);
8198 main->index = 0;
8199 current = main;
8200 main->lines.resize(1);
8201 main->lines[0].from = main;
8202 main->first_invalid_line.store(0);
8203 main->first_resized_line.store(0);
8204 main->first_invalid_font_line.store(0);
8205 current_frame = main;
8206
8207 vscroll = memnew(VScrollBar);
8208 add_child(vscroll, false, INTERNAL_MODE_FRONT);
8209 vscroll->set_drag_node(String(".."));
8210 vscroll->set_step(1);
8211 vscroll->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, 0);
8212 vscroll->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
8213 vscroll->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
8214 vscroll->connect(SceneStringName(value_changed), callable_mp(this, &RichTextLabel::_scroll_changed));
8215 vscroll->set_step(1);
8216 vscroll->hide();
8217
8218 set_focus_mode(FOCUS_ACCESSIBILITY);
8219 set_text(p_text);
8220 updating.store(false);
8221 validating.store(false);
8222 stop_thread.store(false);
8223 parsing_bbcode.store(false);
8224
8225 set_clip_contents(true);
8226
8227 click_select_held = memnew(Timer);
8228 add_child(click_select_held, false, INTERNAL_MODE_FRONT);
8229 click_select_held->set_wait_time(0.05);
8230 click_select_held->connect("timeout", callable_mp(this, &RichTextLabel::_update_selection));
8231 }
8232
8233 RichTextLabel::~RichTextLabel() {
8234 _stop_thread();
8235 items.free(main->rid);
8236 memdelete(main);
8237 }