Debian Codegraph @

scene/gui/label.h

raw symbols SCIP
1 /**************************************************************************/
2 /* 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
31 #pragma once
32
33 #include "scene/gui/control.h"
34 #include "scene/resources/label_settings.h"
35
36 class Label : public Control {
37 GDCLASS(Label, Control);
38
39 private:
40 HorizontalAlignment horizontal_alignment = HORIZONTAL_ALIGNMENT_LEFT;
41 VerticalAlignment vertical_alignment = VERTICAL_ALIGNMENT_TOP;
42 String text;
43 String xl_text;
44 TextServer::AutowrapMode autowrap_mode = TextServer::AUTOWRAP_OFF;
45 BitField<TextServer::LineBreakFlag> autowrap_flags_trim = TextServer::BREAK_TRIM_START_EDGE_SPACES | TextServer::BREAK_TRIM_END_EDGE_SPACES;
46 BitField<TextServer::JustificationFlag> jst_flags = TextServer::JUSTIFICATION_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_SKIP_LAST_LINE | TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE;
47 bool clip = false;
48 String el_char = U"…";
49 TextServer::OverrunBehavior overrun_behavior = TextServer::OVERRUN_NO_TRIMMING;
50 mutable Size2 minsize;
51 bool uppercase = false;
52
53 struct Paragraph {
54 bool lines_dirty = true;
55 bool dirty = true;
56 int start = 0;
57
58 String text;
59 RID text_rid;
60 Vector<RID> lines_rid;
61 };
62 mutable bool dirty = true;
63 mutable bool font_dirty = true;
64 mutable bool text_dirty = true;
65 mutable Vector<Paragraph> paragraphs;
66 mutable int total_line_count = 0;
67 String paragraph_separator = "\\n";
68
69 String language;
70 TextDirection text_direction = TEXT_DIRECTION_AUTO;
71 TextServer::StructuredTextParser st_parser = TextServer::STRUCTURED_TEXT_DEFAULT;
72 Array st_args;
73
74 TextServer::VisibleCharactersBehavior visible_chars_behavior = TextServer::VC_CHARS_BEFORE_SHAPING;
75 int visible_chars = -1;
76 float visible_ratio = 1.0;
77 int lines_skipped = 0;
78 int max_lines_visible = -1;
79 PackedFloat32Array tab_stops;
80
81 Ref<LabelSettings> settings;
82
83 struct ThemeCache {
84 Ref<StyleBox> normal_style;
85 Ref<StyleBox> focus_style;
86 Ref<Font> font;
87
88 int font_size = 0;
89 int line_spacing = 0;
90 int paragraph_spacing = 0;
91 Color font_color;
92 Color font_shadow_color;
93 Point2 font_shadow_offset;
94 Color font_outline_color;
95 int font_outline_size;
96 int font_shadow_outline_size;
97 } theme_cache;
98
99 Rect2 _get_line_rect(int p_para, int p_line) const;
100 void _ensure_shaped() const;
101 void _update_visible() const;
102 void _shape() const;
103 void _invalidate();
104
105 protected:
106 RID get_line_rid(int p_line) const;
107 Rect2 get_line_rect(int p_line) const;
108 int get_layout_data(Vector2 &r_offset, int &r_last_line, int &r_line_spacing) const;
109
110 void _notification(int p_what);
111 static void _bind_methods();
112 #ifndef DISABLE_DEPRECATED
113 bool _set(const StringName &p_name, const Variant &p_value);
114 #endif
115
116 public:
117 virtual Size2 get_minimum_size() const override;
118 virtual PackedStringArray get_configuration_warnings() const override;
119
120 void set_horizontal_alignment(HorizontalAlignment p_alignment);
121 HorizontalAlignment get_horizontal_alignment() const;
122
123 void set_vertical_alignment(VerticalAlignment p_alignment);
124 VerticalAlignment get_vertical_alignment() const;
125
126 void set_text(const String &p_string);
127 String get_text() const;
128
129 void set_label_settings(const Ref<LabelSettings> &p_settings);
130 Ref<LabelSettings> get_label_settings() const;
131
132 void set_text_direction(TextDirection p_text_direction);
133 TextDirection get_text_direction() const;
134
135 void set_language(const String &p_language);
136 String get_language() const;
137
138 void set_paragraph_separator(const String &p_paragraph_separator);
139 String get_paragraph_separator() const;
140
141 void set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser);
142 TextServer::StructuredTextParser get_structured_text_bidi_override() const;
143
144 void set_structured_text_bidi_override_options(const Array &p_args);
145 Array get_structured_text_bidi_override_options() const;
146
147 void set_autowrap_mode(TextServer::AutowrapMode p_mode);
148 TextServer::AutowrapMode get_autowrap_mode() const;
149
150 void set_autowrap_trim_flags(BitField<TextServer::LineBreakFlag> p_flags);
151 BitField<TextServer::LineBreakFlag> get_autowrap_trim_flags() const;
152
153 void set_justification_flags(BitField<TextServer::JustificationFlag> p_flags);
154 BitField<TextServer::JustificationFlag> get_justification_flags() const;
155
156 void set_uppercase(bool p_uppercase);
157 bool is_uppercase() const;
158
159 void set_visible_characters_behavior(TextServer::VisibleCharactersBehavior p_behavior);
160 TextServer::VisibleCharactersBehavior get_visible_characters_behavior() const;
161
162 void set_visible_characters(int p_amount);
163 int get_visible_characters() const;
164 int get_total_character_count() const;
165
166 void set_visible_ratio(float p_ratio);
167 float get_visible_ratio() const;
168
169 void set_clip_text(bool p_clip);
170 bool is_clipping_text() const;
171
172 void set_tab_stops(const PackedFloat32Array &p_tab_stops);
173 PackedFloat32Array get_tab_stops() const;
174
175 void set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior);
176 TextServer::OverrunBehavior get_text_overrun_behavior() const;
177
178 void set_ellipsis_char(const String &p_char);
179 String get_ellipsis_char() const;
180
181 void set_lines_skipped(int p_lines);
182 int get_lines_skipped() const;
183
184 void set_max_lines_visible(int p_lines);
185 int get_max_lines_visible() const;
186
187 int get_line_height(int p_line = -1) const;
188 int get_line_count() const;
189 int get_visible_line_count() const;
190
191 Rect2 get_character_bounds(int p_pos) const;
192
193 Label(const String &p_text = String());
194 ~Label();
195
196 template <typename... VarArgsFunc, typename... VarArgs>
197 void draw_text(bool p_rtl, int p_ellipsis_pos, int p_ellipsis_gl_size, const Glyph *p_ellipsis_glyphs, bool p_trim_chars, int p_para_start, int p_visible_chars, bool p_trim_glyphs_ltr, int &p_processed_glyphs_step, int p_processed_glyphs, int p_visible_glyphs, bool p_trim_glyphs_rtl, int p_total_glyphs, const RID &p_ci, const Vector2 &p_ofs, int p_gl_size, int p_trim_pos, const Glyph *p_glyphs, const Color &p_color, void (*p_draw_func)(const Glyph &p_gl, const RID &p_canvas, const Color &p_font_outline_color, const Vector2 &p_ofs, VarArgsFunc... p_args), VarArgs &&...p_args) {
198 p_processed_glyphs_step = p_processed_glyphs;
199 Vector2 offset_step = p_ofs; /* Draw RTL ellipsis string when necessary. */
200 if (p_rtl && p_ellipsis_pos >= 0) {
201 for (int gl_idx = p_ellipsis_gl_size - 1; gl_idx >= 0; gl_idx--) {
202 for (int j = 0; j < p_ellipsis_glyphs[gl_idx].repeat; j++) {
203 bool skip = (p_trim_chars && p_ellipsis_glyphs[gl_idx].end + p_para_start > p_visible_chars) || (p_trim_glyphs_ltr && (p_processed_glyphs_step >= p_visible_glyphs)) || (p_trim_glyphs_rtl && (p_processed_glyphs_step < p_total_glyphs - p_visible_glyphs));
204 if (!skip) {
205 p_draw_func(p_ellipsis_glyphs[gl_idx], p_ci, p_color, offset_step, std::forward<VarArgs>(p_args)...);
206 }
207 p_processed_glyphs_step++;
208 offset_step.x += p_ellipsis_glyphs[gl_idx].advance;
209 }
210 }
211 } /* Draw main text. */
212 for (int j = 0; j < p_gl_size; j++) { /* Trim when necessary. */
213 if (p_trim_pos >= 0) {
214 if (p_rtl) {
215 if (j < p_trim_pos) {
216 continue;
217 }
218 } else {
219 if (j >= p_trim_pos) {
220 break;
221 }
222 }
223 }
224 for (int k = 0; k < p_glyphs[j].repeat; k++) {
225 bool skip = (p_trim_chars && p_glyphs[j].end + p_para_start > p_visible_chars) || (p_trim_glyphs_ltr && (p_processed_glyphs_step >= p_visible_glyphs)) || (p_trim_glyphs_rtl && (p_processed_glyphs_step < p_total_glyphs - p_visible_glyphs));
226 if (!skip) {
227 p_draw_func(p_glyphs[j], p_ci, p_color, offset_step, std::forward<VarArgs>(p_args)...);
228 }
229 p_processed_glyphs_step++;
230 offset_step.x += p_glyphs[j].advance;
231 }
232 } /* Draw LTR ellipsis string when necessary. */
233 if (!p_rtl && p_ellipsis_pos >= 0) {
234 for (int gl_idx = 0; gl_idx < p_ellipsis_gl_size; gl_idx++) {
235 for (int j = 0; j < p_ellipsis_glyphs[gl_idx].repeat; j++) {
236 bool skip = (p_trim_chars && p_ellipsis_glyphs[gl_idx].end + p_para_start > p_visible_chars) || (p_trim_glyphs_ltr && (p_processed_glyphs_step >= p_visible_glyphs)) || (p_trim_glyphs_rtl && (p_processed_glyphs_step < p_total_glyphs - p_visible_glyphs));
237 if (!skip) {
238 p_draw_func(p_ellipsis_glyphs[gl_idx], p_ci, p_color, offset_step, std::forward<VarArgs>(p_args)...);
239 }
240 p_processed_glyphs_step++;
241 offset_step.x += p_ellipsis_glyphs[gl_idx].advance;
242 }
243 }
244 }
245 }
246 };