text_paragraph.h 7.2 KB

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