rich_text_label.h 28 KB

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