rich_text_label.h 28 KB

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