text_paragraph.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**************************************************************************/
  2. /* text_paragraph.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/templates/local_vector.h"
  32. #include "scene/resources/font.h"
  33. #include "servers/text_server.h"
  34. /*************************************************************************/
  35. class TextParagraph : public RefCounted {
  36. GDCLASS(TextParagraph, RefCounted);
  37. _THREAD_SAFE_CLASS_
  38. private:
  39. mutable RID dropcap_rid;
  40. mutable int dropcap_lines = 0;
  41. Rect2 dropcap_margins;
  42. RID rid;
  43. mutable LocalVector<RID> lines_rid;
  44. mutable bool lines_dirty = true;
  45. float line_spacing = 0.0;
  46. float width = -1.0;
  47. int max_lines_visible = -1;
  48. BitField<TextServer::LineBreakFlag> brk_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND;
  49. BitField<TextServer::JustificationFlag> jst_flags = TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_SKIP_LAST_LINE | TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE;
  50. String el_char = U"…";
  51. TextServer::OverrunBehavior overrun_behavior = TextServer::OVERRUN_NO_TRIMMING;
  52. HorizontalAlignment alignment = HORIZONTAL_ALIGNMENT_LEFT;
  53. Vector<float> tab_stops;
  54. protected:
  55. #ifndef DISABLE_DEPRECATED
  56. void _draw_bind_compat_104872(RID p_canvas, const Vector2 &p_pos, const Color &p_color = Color(1, 1, 1), const Color &p_dc_color = Color(1, 1, 1)) const;
  57. void _draw_outline_bind_compat_104872(RID p_canvas, const Vector2 &p_pos, int p_outline_size = 1, const Color &p_color = Color(1, 1, 1), const Color &p_dc_color = Color(1, 1, 1)) const;
  58. void _draw_line_bind_compat_104872(RID p_canvas, const Vector2 &p_pos, int p_line, const Color &p_color = Color(1, 1, 1)) const;
  59. void _draw_line_outline_bind_compat_104872(RID p_canvas, const Vector2 &p_pos, int p_line, int p_outline_size = 1, const Color &p_color = Color(1, 1, 1)) const;
  60. void _draw_dropcap_bind_compat_104872(RID p_canvas, const Vector2 &p_pos, const Color &p_color = Color(1, 1, 1)) const;
  61. void _draw_dropcap_outline_bind_compat_104872(RID p_canvas, const Vector2 &p_pos, int p_outline_size = 1, const Color &p_color = Color(1, 1, 1)) const;
  62. static void _bind_compatibility_methods();
  63. #endif
  64. static void _bind_methods();
  65. void _shape_lines() const;
  66. public:
  67. RID get_rid() const;
  68. RID get_line_rid(int p_line) const;
  69. RID get_dropcap_rid() const;
  70. void clear();
  71. void set_direction(TextServer::Direction p_direction);
  72. TextServer::Direction get_direction() const;
  73. TextServer::Direction get_inferred_direction() const;
  74. void set_orientation(TextServer::Orientation p_orientation);
  75. TextServer::Orientation get_orientation() const;
  76. void set_preserve_invalid(bool p_enabled);
  77. bool get_preserve_invalid() const;
  78. void set_preserve_control(bool p_enabled);
  79. bool get_preserve_control() const;
  80. void set_bidi_override(const Array &p_override);
  81. void set_custom_punctuation(const String &p_punct);
  82. String get_custom_punctuation() const;
  83. bool set_dropcap(const String &p_text, const Ref<Font> &p_font, int p_font_size, const Rect2 &p_dropcap_margins = Rect2(), const String &p_language = "");
  84. void clear_dropcap();
  85. bool add_string(const String &p_text, const Ref<Font> &p_font, int p_font_size, const String &p_language = "", const Variant &p_meta = Variant());
  86. bool add_object(Variant p_key, const Size2 &p_size, InlineAlignment p_inline_align = INLINE_ALIGNMENT_CENTER, int p_length = 1, float p_baseline = 0.0);
  87. bool resize_object(Variant p_key, const Size2 &p_size, InlineAlignment p_inline_align = INLINE_ALIGNMENT_CENTER, float p_baseline = 0.0);
  88. void set_alignment(HorizontalAlignment p_alignment);
  89. HorizontalAlignment get_alignment() const;
  90. void tab_align(const Vector<float> &p_tab_stops);
  91. void set_justification_flags(BitField<TextServer::JustificationFlag> p_flags);
  92. BitField<TextServer::JustificationFlag> get_justification_flags() const;
  93. void set_break_flags(BitField<TextServer::LineBreakFlag> p_flags);
  94. BitField<TextServer::LineBreakFlag> get_break_flags() const;
  95. void set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior);
  96. TextServer::OverrunBehavior get_text_overrun_behavior() const;
  97. void set_ellipsis_char(const String &p_char);
  98. String get_ellipsis_char() const;
  99. void set_width(float p_width);
  100. float get_width() const;
  101. void set_max_lines_visible(int p_lines);
  102. int get_max_lines_visible() const;
  103. void set_line_spacing(float p_spacing);
  104. float get_line_spacing() const;
  105. Size2 get_non_wrapped_size() const;
  106. Size2 get_size() const;
  107. Vector2i get_range() const;
  108. int get_line_count() const;
  109. Array get_line_objects(int p_line) const;
  110. Rect2 get_line_object_rect(int p_line, Variant p_key) const;
  111. Size2 get_line_size(int p_line) const;
  112. float get_line_ascent(int p_line) const;
  113. float get_line_descent(int p_line) const;
  114. float get_line_width(int p_line) const;
  115. Vector2i get_line_range(int p_line) const;
  116. float get_line_underline_position(int p_line) const;
  117. float get_line_underline_thickness(int p_line) const;
  118. Size2 get_dropcap_size() const;
  119. int get_dropcap_lines() const;
  120. void draw(RID p_canvas, const Vector2 &p_pos, const Color &p_color = Color(1, 1, 1), const Color &p_dc_color = Color(1, 1, 1), float p_oversampling = 0.0) const;
  121. void draw_outline(RID p_canvas, const Vector2 &p_pos, int p_outline_size = 1, const Color &p_color = Color(1, 1, 1), const Color &p_dc_color = Color(1, 1, 1), float p_oversampling = 0.0) const;
  122. void draw_line(RID p_canvas, const Vector2 &p_pos, int p_line, const Color &p_color = Color(1, 1, 1), float p_oversampling = 0.0) const;
  123. void draw_line_outline(RID p_canvas, const Vector2 &p_pos, int p_line, int p_outline_size = 1, const Color &p_color = Color(1, 1, 1), float p_oversampling = 0.0) const;
  124. void draw_dropcap(RID p_canvas, const Vector2 &p_pos, const Color &p_color = Color(1, 1, 1), float p_oversampling = 0.0) const;
  125. void draw_dropcap_outline(RID p_canvas, const Vector2 &p_pos, int p_outline_size = 1, const Color &p_color = Color(1, 1, 1), float p_oversampling = 0.0) const;
  126. int hit_test(const Point2 &p_coords) const;
  127. bool is_dirty();
  128. Mutex &get_mutex() const { return _thread_safe_; }
  129. TextParagraph(const String &p_text, const Ref<Font> &p_font, int p_font_size, const String &p_language = "", float p_width = -1.f, TextServer::Direction p_direction = TextServer::DIRECTION_AUTO, TextServer::Orientation p_orientation = TextServer::ORIENTATION_HORIZONTAL);
  130. TextParagraph();
  131. ~TextParagraph();
  132. };