rich_text_label.h 29 KB

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