rich_text_label.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. /**************************************************************************/
  2. /* rich_text_label.h */
  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. #pragma once
  31. #include "core/object/worker_thread_pool.h"
  32. #include "core/templates/rid_owner.h"
  33. #include "scene/gui/popup_menu.h"
  34. #include "scene/gui/scroll_bar.h"
  35. #include "scene/resources/text_paragraph.h"
  36. class CharFXTransform;
  37. class RichTextEffect;
  38. class RichTextLabel : public Control {
  39. GDCLASS(RichTextLabel, Control);
  40. enum RTLDrawStep {
  41. DRAW_STEP_BACKGROUND,
  42. DRAW_STEP_SHADOW_OUTLINE,
  43. DRAW_STEP_SHADOW,
  44. DRAW_STEP_OUTLINE,
  45. DRAW_STEP_TEXT,
  46. DRAW_STEP_FOREGROUND,
  47. DRAW_STEP_MAX,
  48. };
  49. public:
  50. enum ListType {
  51. LIST_NUMBERS,
  52. LIST_LETTERS,
  53. LIST_ROMAN,
  54. LIST_DOTS
  55. };
  56. enum MetaUnderline {
  57. META_UNDERLINE_NEVER,
  58. META_UNDERLINE_ALWAYS,
  59. META_UNDERLINE_ON_HOVER,
  60. };
  61. enum ItemType {
  62. ITEM_FRAME,
  63. ITEM_TEXT,
  64. ITEM_IMAGE,
  65. ITEM_NEWLINE,
  66. ITEM_FONT,
  67. ITEM_FONT_SIZE,
  68. ITEM_FONT_FEATURES,
  69. ITEM_COLOR,
  70. ITEM_OUTLINE_SIZE,
  71. ITEM_OUTLINE_COLOR,
  72. ITEM_UNDERLINE,
  73. ITEM_STRIKETHROUGH,
  74. ITEM_PARAGRAPH,
  75. ITEM_INDENT,
  76. ITEM_LIST,
  77. ITEM_TABLE,
  78. ITEM_FADE,
  79. ITEM_SHAKE,
  80. ITEM_WAVE,
  81. ITEM_TORNADO,
  82. ITEM_RAINBOW,
  83. ITEM_PULSE,
  84. ITEM_BGCOLOR,
  85. ITEM_FGCOLOR,
  86. ITEM_META,
  87. ITEM_HINT,
  88. ITEM_DROPCAP,
  89. ITEM_CUSTOMFX,
  90. ITEM_CONTEXT,
  91. ITEM_LANGUAGE,
  92. };
  93. enum MenuItems {
  94. MENU_COPY,
  95. MENU_SELECT_ALL,
  96. MENU_MAX
  97. };
  98. enum DefaultFont {
  99. RTL_NORMAL_FONT,
  100. RTL_BOLD_FONT,
  101. RTL_ITALICS_FONT,
  102. RTL_BOLD_ITALICS_FONT,
  103. RTL_MONO_FONT,
  104. RTL_CUSTOM_FONT,
  105. };
  106. enum ImageUpdateMask {
  107. UPDATE_TEXTURE = 1 << 0,
  108. UPDATE_SIZE = 1 << 1,
  109. UPDATE_COLOR = 1 << 2,
  110. UPDATE_ALIGNMENT = 1 << 3,
  111. UPDATE_REGION = 1 << 4,
  112. UPDATE_PAD = 1 << 5,
  113. UPDATE_TOOLTIP = 1 << 6,
  114. UPDATE_WIDTH_IN_PERCENT = 1 << 7,
  115. };
  116. protected:
  117. virtual void _update_theme_item_cache() override;
  118. void _notification(int p_what);
  119. static void _bind_methods();
  120. #ifndef DISABLE_DEPRECATED
  121. void _push_font_bind_compat_79053(const Ref<Font> &p_font, int p_size);
  122. void _set_table_column_expand_bind_compat_79053(int p_column, bool p_expand, int p_ratio);
  123. void _push_meta_bind_compat_99481(const Variant &p_meta, MetaUnderline p_underline_mode);
  124. void _push_meta_bind_compat_89024(const Variant &p_meta);
  125. void _add_image_bind_compat_80410(const Ref<Texture2D> &p_image, const int p_width, const int p_height, const Color &p_color, InlineAlignment p_alignment, const Rect2 &p_region);
  126. void _add_image_bind_compat_76829(const Ref<Texture2D> &p_image, const int p_width, const 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_size_in_percent);
  127. void _push_table_bind_compat_76829(int p_columns, InlineAlignment p_alignment, int p_align_to_row);
  128. bool _remove_paragraph_bind_compat_91098(int p_paragraph);
  129. void _set_table_column_expand_bind_compat_101482(int p_column, bool p_expand, int p_ratio);
  130. static void _bind_compatibility_methods();
  131. #endif
  132. private:
  133. struct Item;
  134. struct Line {
  135. Item *from = nullptr;
  136. Ref<TextLine> text_prefix;
  137. float prefix_width = 0;
  138. Ref<TextParagraph> text_buf;
  139. RID accessibility_line_element;
  140. RID accessibility_text_element;
  141. Item *dc_item = nullptr;
  142. Color dc_color;
  143. int dc_ol_size = 0;
  144. Color dc_ol_color;
  145. Vector2 offset;
  146. float indent = 0.0;
  147. int char_offset = 0;
  148. int char_count = 0;
  149. Line() {
  150. text_buf.instantiate();
  151. }
  152. ~Line() {
  153. if (accessibility_line_element.is_valid()) {
  154. DisplayServer::get_singleton()->accessibility_free_element(accessibility_line_element);
  155. accessibility_line_element = RID();
  156. accessibility_text_element = RID();
  157. }
  158. }
  159. _FORCE_INLINE_ float get_height(float line_separation) const {
  160. return offset.y + text_buf->get_size().y + text_buf->get_line_count() * line_separation;
  161. }
  162. };
  163. struct Item {
  164. int index = 0;
  165. int char_ofs = 0;
  166. Item *parent = nullptr;
  167. ItemType type = ITEM_FRAME;
  168. List<Item *> subitems;
  169. List<Item *>::Element *E = nullptr;
  170. ObjectID owner;
  171. int line = 0;
  172. RID rid;
  173. RID accessibility_item_element;
  174. void _clear_children() {
  175. RichTextLabel *owner_rtl = ObjectDB::get_instance<RichTextLabel>(owner);
  176. while (subitems.size()) {
  177. Item *subitem = subitems.front()->get();
  178. if (subitem && subitem->rid.is_valid() && owner_rtl) {
  179. owner_rtl->items.free(subitem->rid);
  180. }
  181. memdelete(subitem);
  182. subitems.pop_front();
  183. }
  184. }
  185. virtual ~Item() { _clear_children(); }
  186. };
  187. struct ItemFrame : public Item {
  188. bool cell = false;
  189. LocalVector<Line> lines;
  190. std::atomic<int> first_invalid_line;
  191. std::atomic<int> first_invalid_font_line;
  192. std::atomic<int> first_resized_line;
  193. ItemFrame *parent_frame = nullptr;
  194. Color odd_row_bg = Color(0, 0, 0, 0);
  195. Color even_row_bg = Color(0, 0, 0, 0);
  196. Color border = Color(0, 0, 0, 0);
  197. Size2 min_size_over = Size2(-1, -1);
  198. Size2 max_size_over = Size2(-1, -1);
  199. Rect2 padding;
  200. int indent_level = 0;
  201. ItemFrame() {
  202. type = ITEM_FRAME;
  203. first_invalid_line.store(0);
  204. first_invalid_font_line.store(0);
  205. first_resized_line.store(0);
  206. }
  207. };
  208. struct ItemText : public Item {
  209. String text;
  210. ItemText() { type = ITEM_TEXT; }
  211. };
  212. struct ItemDropcap : public Item {
  213. String text;
  214. Ref<Font> font;
  215. int font_size = 0;
  216. Color color;
  217. int ol_size = 0;
  218. Color ol_color;
  219. Rect2 dropcap_margins;
  220. ItemDropcap() { type = ITEM_DROPCAP; }
  221. ~ItemDropcap() {
  222. if (font.is_valid()) {
  223. RichTextLabel *owner_rtl = ObjectDB::get_instance<RichTextLabel>(owner);
  224. if (owner_rtl) {
  225. font->disconnect_changed(callable_mp(owner_rtl, &RichTextLabel::_invalidate_fonts));
  226. }
  227. }
  228. }
  229. };
  230. struct ItemImage : public Item {
  231. Ref<Texture2D> image;
  232. String alt_text;
  233. InlineAlignment inline_align = INLINE_ALIGNMENT_CENTER;
  234. bool pad = false;
  235. bool size_in_percent = false;
  236. Rect2 region;
  237. Size2 size;
  238. Size2 rq_size;
  239. Color color;
  240. Variant key;
  241. String tooltip;
  242. ItemImage() { type = ITEM_IMAGE; }
  243. ~ItemImage() {
  244. if (image.is_valid()) {
  245. RichTextLabel *owner_rtl = ObjectDB::get_instance<RichTextLabel>(owner);
  246. if (owner_rtl) {
  247. image->disconnect_changed(callable_mp(owner_rtl, &RichTextLabel::_texture_changed));
  248. }
  249. }
  250. }
  251. };
  252. struct ItemFont : public Item {
  253. DefaultFont def_font = RTL_CUSTOM_FONT;
  254. Ref<Font> font;
  255. bool variation = false;
  256. bool def_size = false;
  257. int font_size = 0;
  258. ItemFont() { type = ITEM_FONT; }
  259. ~ItemFont() {
  260. if (font.is_valid()) {
  261. RichTextLabel *owner_rtl = ObjectDB::get_instance<RichTextLabel>(owner);
  262. if (owner_rtl) {
  263. font->disconnect_changed(callable_mp(owner_rtl, &RichTextLabel::_invalidate_fonts));
  264. }
  265. }
  266. }
  267. };
  268. struct ItemFontSize : public Item {
  269. int font_size = 16;
  270. ItemFontSize() { type = ITEM_FONT_SIZE; }
  271. };
  272. struct ItemColor : public Item {
  273. Color color;
  274. ItemColor() { type = ITEM_COLOR; }
  275. };
  276. struct ItemOutlineSize : public Item {
  277. int outline_size = 0;
  278. ItemOutlineSize() { type = ITEM_OUTLINE_SIZE; }
  279. };
  280. struct ItemOutlineColor : public Item {
  281. Color color;
  282. ItemOutlineColor() { type = ITEM_OUTLINE_COLOR; }
  283. };
  284. struct ItemUnderline : public Item {
  285. ItemUnderline() { type = ITEM_UNDERLINE; }
  286. };
  287. struct ItemStrikethrough : public Item {
  288. ItemStrikethrough() { type = ITEM_STRIKETHROUGH; }
  289. };
  290. struct ItemMeta : public Item {
  291. Variant meta;
  292. MetaUnderline underline = META_UNDERLINE_ALWAYS;
  293. String tooltip;
  294. ItemMeta() { type = ITEM_META; }
  295. };
  296. struct ItemHint : public Item {
  297. String description;
  298. ItemHint() { type = ITEM_HINT; }
  299. };
  300. struct ItemLanguage : public Item {
  301. String language;
  302. ItemLanguage() { type = ITEM_LANGUAGE; }
  303. };
  304. struct ItemParagraph : public Item {
  305. HorizontalAlignment alignment = HORIZONTAL_ALIGNMENT_LEFT;
  306. String language;
  307. Control::TextDirection direction = Control::TEXT_DIRECTION_AUTO;
  308. TextServer::StructuredTextParser st_parser = TextServer::STRUCTURED_TEXT_DEFAULT;
  309. BitField<TextServer::JustificationFlag> jst_flags = TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_SKIP_LAST_LINE | TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE;
  310. PackedFloat32Array tab_stops;
  311. ItemParagraph() { type = ITEM_PARAGRAPH; }
  312. };
  313. struct ItemIndent : public Item {
  314. int level = 0;
  315. ItemIndent() { type = ITEM_INDENT; }
  316. };
  317. struct ItemList : public Item {
  318. ListType list_type = LIST_DOTS;
  319. bool capitalize = false;
  320. int level = 0;
  321. String bullet = U"•";
  322. float max_width = 0;
  323. ItemList() { type = ITEM_LIST; }
  324. };
  325. struct ItemNewline : public Item {
  326. ItemNewline() { type = ITEM_NEWLINE; }
  327. };
  328. struct ItemTable : public Item {
  329. struct Column {
  330. String name;
  331. bool expand = false;
  332. bool shrink = true;
  333. int expand_ratio = 0;
  334. int min_width = 0;
  335. int max_width = 0;
  336. int width = 0;
  337. int width_with_padding = 0;
  338. };
  339. LocalVector<Column> columns;
  340. LocalVector<float> rows;
  341. LocalVector<float> rows_no_padding;
  342. LocalVector<float> rows_baseline;
  343. String name;
  344. int align_to_row = -1;
  345. int total_width = 0;
  346. int total_height = 0;
  347. InlineAlignment inline_align = INLINE_ALIGNMENT_TOP;
  348. ItemTable() { type = ITEM_TABLE; }
  349. };
  350. struct ItemFade : public Item {
  351. int starting_index = 0;
  352. int length = 0;
  353. ItemFade() { type = ITEM_FADE; }
  354. };
  355. struct ItemFX : public Item {
  356. double elapsed_time = 0.f;
  357. bool connected = true;
  358. };
  359. struct ItemShake : public ItemFX {
  360. int strength = 0;
  361. float rate = 0.0f;
  362. uint64_t _current_rng = 0;
  363. uint64_t _previous_rng = 0;
  364. Vector2 prev_off;
  365. ItemShake() { type = ITEM_SHAKE; }
  366. void reroll_random() {
  367. _previous_rng = _current_rng;
  368. _current_rng = Math::rand();
  369. }
  370. uint64_t offset_random(int p_index) {
  371. return (_current_rng >> (p_index % 64)) |
  372. (_current_rng << (64 - (p_index % 64)));
  373. }
  374. uint64_t offset_previous_random(int p_index) {
  375. return (_previous_rng >> (p_index % 64)) |
  376. (_previous_rng << (64 - (p_index % 64)));
  377. }
  378. };
  379. struct ItemWave : public ItemFX {
  380. float frequency = 1.0f;
  381. float amplitude = 1.0f;
  382. Vector2 prev_off;
  383. ItemWave() { type = ITEM_WAVE; }
  384. };
  385. struct ItemTornado : public ItemFX {
  386. float radius = 1.0f;
  387. float frequency = 1.0f;
  388. Vector2 prev_off;
  389. ItemTornado() { type = ITEM_TORNADO; }
  390. };
  391. struct ItemRainbow : public ItemFX {
  392. float saturation = 0.8f;
  393. float value = 0.8f;
  394. float frequency = 1.0f;
  395. float speed = 1.0f;
  396. ItemRainbow() { type = ITEM_RAINBOW; }
  397. };
  398. struct ItemPulse : public ItemFX {
  399. Color color = Color(1.0, 1.0, 1.0, 0.25);
  400. float frequency = 1.0f;
  401. float ease = -2.0f;
  402. ItemPulse() { type = ITEM_PULSE; }
  403. };
  404. struct ItemBGColor : public Item {
  405. Color color;
  406. ItemBGColor() { type = ITEM_BGCOLOR; }
  407. };
  408. struct ItemFGColor : public Item {
  409. Color color;
  410. ItemFGColor() { type = ITEM_FGCOLOR; }
  411. };
  412. struct ItemCustomFX : public ItemFX {
  413. Ref<CharFXTransform> char_fx_transform;
  414. Ref<RichTextEffect> custom_effect;
  415. ItemCustomFX();
  416. virtual ~ItemCustomFX();
  417. };
  418. struct ItemContext : public Item {
  419. ItemContext() { type = ITEM_CONTEXT; }
  420. };
  421. ItemFrame *main = nullptr;
  422. Item *current = nullptr;
  423. ItemFrame *current_frame = nullptr;
  424. WorkerThreadPool::TaskID task = WorkerThreadPool::INVALID_TASK_ID;
  425. Mutex data_mutex;
  426. bool threaded = false;
  427. std::atomic<bool> stop_thread;
  428. std::atomic<bool> updating;
  429. std::atomic<bool> validating;
  430. std::atomic<double> loaded;
  431. std::atomic<bool> parsing_bbcode;
  432. uint64_t loading_started = 0;
  433. int progress_delay = 1000;
  434. VScrollBar *vscroll = nullptr;
  435. TextServer::AutowrapMode autowrap_mode = TextServer::AUTOWRAP_WORD_SMART;
  436. BitField<TextServer::LineBreakFlag> autowrap_flags_trim = TextServer::BREAK_TRIM_START_EDGE_SPACES | TextServer::BREAK_TRIM_END_EDGE_SPACES;
  437. bool scroll_visible = false;
  438. bool scroll_follow = false;
  439. bool scroll_following = false;
  440. bool scroll_active = true;
  441. int scroll_w = 0;
  442. bool scroll_updated = false;
  443. bool updating_scroll = false;
  444. int current_idx = 1;
  445. int current_char_ofs = 0;
  446. int visible_paragraph_count = 0;
  447. int visible_line_count = 0;
  448. int tab_size = 4;
  449. bool underline_meta = true;
  450. bool underline_hint = true;
  451. bool use_selected_font_color = false;
  452. HorizontalAlignment default_alignment = HORIZONTAL_ALIGNMENT_LEFT;
  453. VerticalAlignment vertical_alignment = VERTICAL_ALIGNMENT_TOP;
  454. BitField<TextServer::JustificationFlag> default_jst_flags = TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_SKIP_LAST_LINE | TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE;
  455. PackedFloat32Array default_tab_stops;
  456. ItemMeta *meta_hovering = nullptr;
  457. Variant current_meta;
  458. Array custom_effects;
  459. HashMap<RID, Rect2> ac_element_bounds_cache;
  460. void _invalidate_accessibility();
  461. void _invalidate_current_line(ItemFrame *p_frame);
  462. void _thread_function(void *p_userdata);
  463. void _thread_end();
  464. void _stop_thread();
  465. bool _validate_line_caches();
  466. void _process_line_caches();
  467. _FORCE_INLINE_ float _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);
  468. void _add_item(Item *p_item, bool p_enter = false, bool p_ensure_newline = false);
  469. void _remove_frame(HashSet<Item *> &r_erase_list, ItemFrame *p_frame, int p_line, bool p_erase, int p_char_offset, int p_line_offset);
  470. void _texture_changed(RID p_item);
  471. RID_PtrOwner<Item> items;
  472. List<String> tag_stack;
  473. String language;
  474. TextDirection text_direction = TEXT_DIRECTION_AUTO;
  475. TextServer::StructuredTextParser st_parser = TextServer::STRUCTURED_TEXT_DEFAULT;
  476. Array st_args;
  477. struct Selection {
  478. ItemFrame *click_frame = nullptr;
  479. int click_line = 0;
  480. Item *click_item = nullptr;
  481. int click_char = 0;
  482. ItemFrame *from_frame = nullptr;
  483. int from_line = 0;
  484. Item *from_item = nullptr;
  485. int from_char = 0;
  486. ItemFrame *to_frame = nullptr;
  487. int to_line = 0;
  488. Item *to_item = nullptr;
  489. int to_char = 0;
  490. bool double_click = false; // Selecting whole words?
  491. bool active = false; // anything selected? i.e. from, to, etc. valid?
  492. bool enabled = false; // allow selections?
  493. bool drag_attempt = false;
  494. };
  495. Selection selection;
  496. Callable selection_modifier;
  497. bool deselect_on_focus_loss_enabled = true;
  498. bool drag_and_drop_selection_enabled = true;
  499. ItemFrame *keyboard_focus_frame = nullptr;
  500. int keyboard_focus_line = 0;
  501. Item *keyboard_focus_item = nullptr;
  502. bool keyboard_focus_on_text = true;
  503. bool context_menu_enabled = false;
  504. bool shortcut_keys_enabled = true;
  505. // Context menu.
  506. PopupMenu *menu = nullptr;
  507. void _generate_context_menu();
  508. void _update_context_menu();
  509. Key _get_menu_action_accelerator(const String &p_action);
  510. int visible_characters = -1;
  511. float visible_ratio = 1.0;
  512. TextServer::VisibleCharactersBehavior visible_chars_behavior = TextServer::VC_CHARS_BEFORE_SHAPING;
  513. bool _is_click_inside_selection() const;
  514. void _find_click(ItemFrame *p_frame, const Point2i &p_click, ItemFrame **r_click_frame = nullptr, int *r_click_line = nullptr, Item **r_click_item = nullptr, int *r_click_char = nullptr, bool *r_outside = nullptr, bool p_meta = false);
  515. String _get_line_text(ItemFrame *p_frame, int p_line, Selection p_sel) const;
  516. bool _search_line(ItemFrame *p_frame, int p_line, const String &p_string, int p_char_idx, bool p_reverse_search);
  517. bool _search_table(ItemTable *p_table, List<Item *>::Element *p_from, const String &p_string, bool p_reverse_search);
  518. float _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);
  519. float _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);
  520. void _set_table_size(ItemTable *p_table, int p_available_width);
  521. void _update_line_font(ItemFrame *p_frame, int p_line, const Ref<Font> &p_base_font, int p_base_font_size);
  522. int _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);
  523. float _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 = nullptr, int *r_click_line = nullptr, Item **r_click_item = nullptr, int *r_click_char = nullptr, bool p_table = false, bool p_meta = false);
  524. void _accessibility_update_line(RID p_id, ItemFrame *p_frame, int p_line, const Vector2 &p_ofs, int p_width, float p_vsep);
  525. String _roman(int p_num, bool p_capitalize) const;
  526. String _letters(int p_num, bool p_capitalize) const;
  527. Item *_find_indentable(Item *p_item);
  528. Item *_get_item_at_pos(Item *p_item_from, Item *p_item_to, int p_position);
  529. void _find_frame(Item *p_item, ItemFrame **r_frame, int *r_line);
  530. ItemFontSize *_find_font_size(Item *p_item);
  531. ItemFont *_find_font(Item *p_item);
  532. int _find_outline_size(Item *p_item, int p_default);
  533. ItemList *_find_list_item(Item *p_item);
  534. ItemDropcap *_find_dc_item(Item *p_item);
  535. int _find_list(Item *p_item, Vector<int> &r_index, Vector<int> &r_count, Vector<ItemList *> &r_list);
  536. int _find_margin(Item *p_item, const Ref<Font> &p_base_font, int p_base_font_size);
  537. PackedFloat32Array _find_tab_stops(Item *p_item);
  538. HorizontalAlignment _find_alignment(Item *p_item);
  539. BitField<TextServer::JustificationFlag> _find_jst_flags(Item *p_item);
  540. TextServer::Direction _find_direction(Item *p_item);
  541. TextServer::StructuredTextParser _find_stt(Item *p_item);
  542. String _find_language(Item *p_item);
  543. Color _find_color(Item *p_item, const Color &p_default_color);
  544. Color _find_outline_color(Item *p_item, const Color &p_default_color);
  545. bool _find_underline(Item *p_item);
  546. bool _find_strikethrough(Item *p_item);
  547. bool _find_meta(Item *p_item, Variant *r_meta, ItemMeta **r_item = nullptr);
  548. bool _find_hint(Item *p_item, String *r_description);
  549. Color _find_bgcolor(Item *p_item);
  550. Color _find_fgcolor(Item *p_item);
  551. bool _find_layout_subitem(Item *from, Item *to);
  552. void _fetch_item_fx_stack(Item *p_item, Vector<ItemFX *> &r_stack);
  553. void _normalize_subtags(Vector<String> &subtags);
  554. void _update_fx(ItemFrame *p_frame, double p_delta_time);
  555. void _scroll_changed(double);
  556. int _find_first_line(int p_from, int p_to, int p_vofs) const;
  557. _FORCE_INLINE_ float _calculate_line_vertical_offset(const Line &line) const;
  558. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  559. virtual String get_tooltip(const Point2 &p_pos) const override;
  560. Item *_get_next_item(Item *p_item, bool p_free = false) const;
  561. Item *_get_prev_item(Item *p_item, bool p_free = false) const;
  562. Rect2 _get_text_rect();
  563. Ref<RichTextEffect> _get_custom_effect_by_code(String p_bbcode_identifier);
  564. virtual Dictionary parse_expressions_for_values(Vector<String> p_expressions);
  565. void _invalidate_fonts();
  566. Size2 _get_image_size(const Ref<Texture2D> &p_image, int p_width = 0, int p_height = 0, const Rect2 &p_region = Rect2());
  567. String _get_prefix(Item *p_item, const Vector<int> &p_list_index, const Vector<ItemList *> &p_list_items);
  568. static int _find_unquoted(const String &p_src, char32_t p_chr, int p_from);
  569. static Vector<String> _split_unquoted(const String &p_src, char32_t p_splitter);
  570. static String _get_tag_value(const String &p_tag);
  571. #ifndef DISABLE_DEPRECATED
  572. // Kept for compatibility from 3.x to 4.0.
  573. bool _set(const StringName &p_name, const Variant &p_value);
  574. #endif
  575. bool use_bbcode = false;
  576. String text;
  577. void _apply_translation();
  578. bool internal_stack_editing = false;
  579. bool stack_externally_modified = false;
  580. void _accessibility_action_menu(const Variant &p_data);
  581. void _accessibility_scroll_down(const Variant &p_data);
  582. void _accessibility_scroll_up(const Variant &p_data);
  583. void _accessibility_scroll_set(const Variant &p_data);
  584. void _accessibility_focus_item(const Variant &p_data, uint64_t p_item, bool p_line, bool p_foucs);
  585. void _accessibility_scroll_to_item(const Variant &p_data, uint64_t p_item);
  586. RID accessibility_scroll_element;
  587. bool fit_content = false;
  588. struct ThemeCache {
  589. Ref<StyleBox> normal_style;
  590. Ref<StyleBox> focus_style;
  591. Ref<StyleBox> progress_bg_style;
  592. Ref<StyleBox> progress_fg_style;
  593. int line_separation;
  594. Ref<Font> normal_font;
  595. int normal_font_size;
  596. Color default_color;
  597. Color font_selected_color;
  598. Color selection_color;
  599. Color font_outline_color;
  600. Color font_shadow_color;
  601. int shadow_outline_size;
  602. int shadow_offset_x;
  603. int shadow_offset_y;
  604. int outline_size;
  605. Color outline_color;
  606. Ref<Font> bold_font;
  607. int bold_font_size;
  608. Ref<Font> bold_italics_font;
  609. int bold_italics_font_size;
  610. Ref<Font> italics_font;
  611. int italics_font_size;
  612. Ref<Font> mono_font;
  613. int mono_font_size;
  614. int text_highlight_h_padding;
  615. int text_highlight_v_padding;
  616. int table_h_separation;
  617. int table_v_separation;
  618. Color table_odd_row_bg;
  619. Color table_even_row_bg;
  620. Color table_border;
  621. float base_scale = 1.0;
  622. } theme_cache;
  623. public:
  624. virtual RID get_focused_accessibility_element() const override;
  625. PackedStringArray get_accessibility_configuration_warnings() const override;
  626. String get_parsed_text() const;
  627. void add_text(const String &p_text);
  628. void add_image(const Ref<Texture2D> &p_image, int p_width = 0, int p_height = 0, const Color &p_color = Color(1.0, 1.0, 1.0), InlineAlignment p_alignment = INLINE_ALIGNMENT_CENTER, const Rect2 &p_region = Rect2(), const Variant &p_key = Variant(), bool p_pad = false, const String &p_tooltip = String(), bool p_size_in_percent = false, const String &p_alt_text = String());
  629. void update_image(const Variant &p_key, BitField<ImageUpdateMask> p_mask, const Ref<Texture2D> &p_image, int p_width = 0, int p_height = 0, const Color &p_color = Color(1.0, 1.0, 1.0), InlineAlignment p_alignment = INLINE_ALIGNMENT_CENTER, const Rect2 &p_region = Rect2(), bool p_pad = false, const String &p_tooltip = String(), bool p_size_in_percent = false);
  630. void add_newline();
  631. bool remove_paragraph(int p_paragraph, bool p_no_invalidate = false);
  632. bool invalidate_paragraph(int p_paragraph);
  633. void push_dropcap(const String &p_string, const Ref<Font> &p_font, int p_size, const Rect2 &p_dropcap_margins = Rect2(), const Color &p_color = Color(1, 1, 1), int p_ol_size = 0, const Color &p_ol_color = Color(0, 0, 0, 0));
  634. void _push_def_font(DefaultFont p_def_font);
  635. void _push_def_font_var(DefaultFont p_def_font, const Ref<Font> &p_font, int p_size = -1);
  636. void push_font(const Ref<Font> &p_font, int p_size = 0);
  637. void push_font_size(int p_font_size);
  638. void push_outline_size(int p_font_size);
  639. void push_normal();
  640. void push_bold();
  641. void push_bold_italics();
  642. void push_italics();
  643. void push_mono();
  644. void push_color(const Color &p_color);
  645. void push_outline_color(const Color &p_color);
  646. void push_underline();
  647. void push_strikethrough();
  648. void push_language(const String &p_language);
  649. void push_paragraph(HorizontalAlignment p_alignment, Control::TextDirection p_direction = Control::TEXT_DIRECTION_INHERITED, const String &p_language = "", TextServer::StructuredTextParser p_st_parser = TextServer::STRUCTURED_TEXT_DEFAULT, BitField<TextServer::JustificationFlag> p_jst_flags = TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_SKIP_LAST_LINE | TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE, const PackedFloat32Array &p_tab_stops = PackedFloat32Array());
  650. void push_indent(int p_level);
  651. void push_list(int p_level, ListType p_list, bool p_capitalize, const String &p_bullet = String::utf8("•"));
  652. void push_meta(const Variant &p_meta, MetaUnderline p_underline_mode = META_UNDERLINE_ALWAYS, const String &p_tooltip = String());
  653. void push_hint(const String &p_string);
  654. void push_table(int p_columns, InlineAlignment p_alignment = INLINE_ALIGNMENT_TOP, int p_align_to_row = -1, const String &p_name = String());
  655. void push_fade(int p_start_index, int p_length);
  656. void push_shake(int p_strength, float p_rate, bool p_connected);
  657. void push_wave(float p_frequency, float p_amplitude, bool p_connected);
  658. void push_tornado(float p_frequency, float p_radius, bool p_connected);
  659. void push_rainbow(float p_saturation, float p_value, float p_frequency, float p_speed);
  660. void push_pulse(const Color &p_color, float p_frequency, float p_ease);
  661. void push_bgcolor(const Color &p_color);
  662. void push_fgcolor(const Color &p_color);
  663. void push_customfx(Ref<RichTextEffect> p_custom_effect, Dictionary p_environment);
  664. void push_context();
  665. void set_table_column_expand(int p_column, bool p_expand, int p_ratio = 1, bool p_shrink = true);
  666. void set_table_column_name(int p_column, const String &p_name);
  667. void set_cell_row_background_color(const Color &p_odd_row_bg, const Color &p_even_row_bg);
  668. void set_cell_border_color(const Color &p_color);
  669. void set_cell_size_override(const Size2 &p_min_size, const Size2 &p_max_size);
  670. void set_cell_padding(const Rect2 &p_padding);
  671. int get_current_table_column() const;
  672. void push_cell();
  673. void pop();
  674. void pop_context();
  675. void pop_all();
  676. void clear();
  677. void set_offset(int p_pixel);
  678. void set_meta_underline(bool p_underline);
  679. bool is_meta_underlined() const;
  680. void set_hint_underline(bool p_underline);
  681. bool is_hint_underlined() const;
  682. void set_scroll_active(bool p_active);
  683. bool is_scroll_active() const;
  684. void set_scroll_follow(bool p_follow);
  685. bool is_scroll_following() const;
  686. void set_tab_size(int p_spaces);
  687. int get_tab_size() const;
  688. void set_context_menu_enabled(bool p_enabled);
  689. bool is_context_menu_enabled() const;
  690. void set_shortcut_keys_enabled(bool p_enabled);
  691. bool is_shortcut_keys_enabled() const;
  692. void set_fit_content(bool p_enabled);
  693. bool is_fit_content_enabled() const;
  694. bool search(const String &p_string, bool p_from_selection = false, bool p_search_previous = false);
  695. void scroll_to_paragraph(int p_paragraph);
  696. int get_paragraph_count() const;
  697. int get_visible_paragraph_count() const;
  698. float get_line_offset(int p_line);
  699. float get_paragraph_offset(int p_paragraph);
  700. void scroll_to_line(int p_line);
  701. int get_line_count() const;
  702. Vector2i get_line_range(int p_line);
  703. int get_visible_line_count() const;
  704. int get_content_height() const;
  705. int get_content_width() const;
  706. int get_line_height(int p_line) const;
  707. int get_line_width(int p_line) const;
  708. void scroll_to_selection();
  709. VScrollBar *get_v_scroll_bar() { return vscroll; }
  710. virtual CursorShape get_cursor_shape(const Point2 &p_pos) const override;
  711. virtual Variant get_drag_data(const Point2 &p_point) override;
  712. void set_selection_enabled(bool p_enabled);
  713. bool is_selection_enabled() const;
  714. int get_selection_from() const;
  715. int get_selection_to() const;
  716. float get_selection_line_offset() const;
  717. String get_selected_text() const;
  718. void select_all();
  719. _FORCE_INLINE_ void set_selection_modifier(const Callable &p_modifier) {
  720. selection_modifier = p_modifier;
  721. }
  722. void set_deselect_on_focus_loss_enabled(const bool p_enabled);
  723. bool is_deselect_on_focus_loss_enabled() const;
  724. void set_drag_and_drop_selection_enabled(const bool p_enabled);
  725. bool is_drag_and_drop_selection_enabled() const;
  726. void deselect();
  727. int get_pending_paragraphs() const;
  728. bool is_finished() const;
  729. bool is_updating() const;
  730. void wait_until_finished();
  731. void set_threaded(bool p_threaded);
  732. bool is_threaded() const;
  733. void set_progress_bar_delay(int p_delay_ms);
  734. int get_progress_bar_delay() const;
  735. // Context menu.
  736. PopupMenu *get_menu() const;
  737. bool is_menu_visible() const;
  738. void menu_option(int p_option);
  739. void parse_bbcode(const String &p_bbcode);
  740. void append_text(const String &p_bbcode);
  741. void set_use_bbcode(bool p_enable);
  742. bool is_using_bbcode() const;
  743. void set_text(const String &p_bbcode);
  744. String get_text() const;
  745. void set_horizontal_alignment(HorizontalAlignment p_alignment);
  746. HorizontalAlignment get_horizontal_alignment() const;
  747. void set_vertical_alignment(VerticalAlignment p_alignment);
  748. VerticalAlignment get_vertical_alignment() const;
  749. void set_justification_flags(BitField<TextServer::JustificationFlag> p_flags);
  750. BitField<TextServer::JustificationFlag> get_justification_flags() const;
  751. void set_tab_stops(const PackedFloat32Array &p_tab_stops);
  752. PackedFloat32Array get_tab_stops() const;
  753. void set_text_direction(TextDirection p_text_direction);
  754. TextDirection get_text_direction() const;
  755. void set_language(const String &p_language);
  756. String get_language() const;
  757. void set_autowrap_mode(TextServer::AutowrapMode p_mode);
  758. TextServer::AutowrapMode get_autowrap_mode() const;
  759. void set_autowrap_trim_flags(BitField<TextServer::LineBreakFlag> p_flags);
  760. BitField<TextServer::LineBreakFlag> get_autowrap_trim_flags() const;
  761. void set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser);
  762. TextServer::StructuredTextParser get_structured_text_bidi_override() const;
  763. void set_structured_text_bidi_override_options(Array p_args);
  764. Array get_structured_text_bidi_override_options() const;
  765. void set_visible_characters(int p_visible);
  766. int get_visible_characters() const;
  767. int get_character_line(int p_char);
  768. int get_character_paragraph(int p_char);
  769. int get_total_character_count() const;
  770. int get_total_glyph_count() const;
  771. void set_visible_ratio(float p_ratio);
  772. float get_visible_ratio() const;
  773. TextServer::VisibleCharactersBehavior get_visible_characters_behavior() const;
  774. void set_visible_characters_behavior(TextServer::VisibleCharactersBehavior p_behavior);
  775. void set_effects(Array p_effects);
  776. Array get_effects();
  777. void install_effect(const Variant effect);
  778. void reload_effects();
  779. virtual Size2 get_minimum_size() const override;
  780. RichTextLabel(const String &p_text = String());
  781. ~RichTextLabel();
  782. };
  783. VARIANT_ENUM_CAST(RichTextLabel::ListType);
  784. VARIANT_ENUM_CAST(RichTextLabel::MenuItems);
  785. VARIANT_ENUM_CAST(RichTextLabel::MetaUnderline);
  786. VARIANT_BITFIELD_CAST(RichTextLabel::ImageUpdateMask);