Debian Codegraph @

scene/resources/text_paragraph.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* text_paragraph.cpp */
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 #include "text_paragraph.h"
32 #include "text_paragraph.compat.inc"
33
34 void TextParagraph::_bind_methods() {
35 ClassDB::bind_method(D_METHOD("clear"), &TextParagraph::clear);
36 ClassDB::bind_method(D_METHOD("duplicate"), &TextParagraph::duplicate);
37
38 ClassDB::bind_method(D_METHOD("set_direction", "direction"), &TextParagraph::set_direction);
39 ClassDB::bind_method(D_METHOD("get_direction"), &TextParagraph::get_direction);
40 ClassDB::bind_method(D_METHOD("get_inferred_direction"), &TextParagraph::get_inferred_direction);
41
42 ADD_PROPERTY(PropertyInfo(Variant::INT, "direction", PROPERTY_HINT_ENUM, "Auto,Left-to-right,Right-to-left"), "set_direction", "get_direction");
43 // If compiling the editor with TextServerFallback only,
44 // `--doctool` would change the default value to `TextServer::DIRECTION_LTR`.
45 // Force it so that it's consistent regardless of the backend.
46 ADD_PROPERTY_DEFAULT("direction", TextServer::DIRECTION_AUTO);
47
48 ClassDB::bind_method(D_METHOD("set_custom_punctuation", "custom_punctuation"), &TextParagraph::set_custom_punctuation);
49 ClassDB::bind_method(D_METHOD("get_custom_punctuation"), &TextParagraph::get_custom_punctuation);
50
51 ADD_PROPERTY(PropertyInfo(Variant::STRING, "custom_punctuation"), "set_custom_punctuation", "get_custom_punctuation");
52
53 ClassDB::bind_method(D_METHOD("set_orientation", "orientation"), &TextParagraph::set_orientation);
54 ClassDB::bind_method(D_METHOD("get_orientation"), &TextParagraph::get_orientation);
55
56 ADD_PROPERTY(PropertyInfo(Variant::INT, "orientation", PROPERTY_HINT_ENUM, "Horizontal,Orientation"), "set_orientation", "get_orientation");
57
58 ClassDB::bind_method(D_METHOD("set_preserve_invalid", "enabled"), &TextParagraph::set_preserve_invalid);
59 ClassDB::bind_method(D_METHOD("get_preserve_invalid"), &TextParagraph::get_preserve_invalid);
60
61 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "preserve_invalid"), "set_preserve_invalid", "get_preserve_invalid");
62
63 ClassDB::bind_method(D_METHOD("set_preserve_control", "enabled"), &TextParagraph::set_preserve_control);
64 ClassDB::bind_method(D_METHOD("get_preserve_control"), &TextParagraph::get_preserve_control);
65
66 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "preserve_control"), "set_preserve_control", "get_preserve_control");
67
68 ClassDB::bind_method(D_METHOD("set_bidi_override", "override"), &TextParagraph::set_bidi_override);
69
70 ClassDB::bind_method(D_METHOD("set_dropcap", "text", "font", "font_size", "dropcap_margins", "language"), &TextParagraph::set_dropcap, DEFVAL(Rect2()), DEFVAL(""));
71 ClassDB::bind_method(D_METHOD("clear_dropcap"), &TextParagraph::clear_dropcap);
72
73 ClassDB::bind_method(D_METHOD("add_string", "text", "font", "font_size", "language", "meta"), &TextParagraph::add_string, DEFVAL(""), DEFVAL(Variant()));
74 ClassDB::bind_method(D_METHOD("add_object", "key", "size", "inline_align", "length", "baseline"), &TextParagraph::add_object, DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(1), DEFVAL(0.0));
75 ClassDB::bind_method(D_METHOD("resize_object", "key", "size", "inline_align", "baseline"), &TextParagraph::resize_object, DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(0.0));
76 ClassDB::bind_method(D_METHOD("has_object", "key"), &TextParagraph::has_object);
77
78 ClassDB::bind_method(D_METHOD("set_alignment", "alignment"), &TextParagraph::set_alignment);
79 ClassDB::bind_method(D_METHOD("get_alignment"), &TextParagraph::get_alignment);
80
81 ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_alignment", "get_alignment");
82
83 ClassDB::bind_method(D_METHOD("tab_align", "tab_stops"), &TextParagraph::tab_align);
84
85 ClassDB::bind_method(D_METHOD("set_break_flags", "flags"), &TextParagraph::set_break_flags);
86 ClassDB::bind_method(D_METHOD("get_break_flags"), &TextParagraph::get_break_flags);
87
88 ADD_PROPERTY(PropertyInfo(Variant::INT, "break_flags", PROPERTY_HINT_FLAGS, "Mandatory,Word Bound,Grapheme Bound,Adaptive,Trim Spaces"), "set_break_flags", "get_break_flags");
89
90 ClassDB::bind_method(D_METHOD("set_justification_flags", "flags"), &TextParagraph::set_justification_flags);
91 ClassDB::bind_method(D_METHOD("get_justification_flags"), &TextParagraph::get_justification_flags);
92
93 ADD_PROPERTY(PropertyInfo(Variant::INT, "justification_flags", PROPERTY_HINT_FLAGS, "Kashida Justification:1,Word Justification:2,Trim Edge Spaces After Justification:4,Justify Only After Last Tab:8,Constrain Ellipsis:16,Skip Last Line:32,Skip Last Line With Visible Characters:64,Do Not Skip Single Line:128"), "set_justification_flags", "get_justification_flags");
94
95 ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "overrun_behavior"), &TextParagraph::set_text_overrun_behavior);
96 ClassDB::bind_method(D_METHOD("get_text_overrun_behavior"), &TextParagraph::get_text_overrun_behavior);
97
98 ADD_PROPERTY(PropertyInfo(Variant::INT, "text_overrun_behavior", PROPERTY_HINT_ENUM, "Trim Nothing,Trim Characters,Trim Words,Ellipsis (6+ Characters),Word Ellipsis (6+ Characters),Ellipsis (Always),Word Ellipsis (Always)"), "set_text_overrun_behavior", "get_text_overrun_behavior");
99
100 ClassDB::bind_method(D_METHOD("set_ellipsis_char", "char"), &TextParagraph::set_ellipsis_char);
101 ClassDB::bind_method(D_METHOD("get_ellipsis_char"), &TextParagraph::get_ellipsis_char);
102 ADD_PROPERTY(PropertyInfo(Variant::STRING, "ellipsis_char"), "set_ellipsis_char", "get_ellipsis_char");
103
104 ClassDB::bind_method(D_METHOD("set_width", "width"), &TextParagraph::set_width);
105 ClassDB::bind_method(D_METHOD("get_width"), &TextParagraph::get_width);
106
107 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "width"), "set_width", "get_width");
108
109 ClassDB::bind_method(D_METHOD("get_non_wrapped_size"), &TextParagraph::get_non_wrapped_size);
110 ClassDB::bind_method(D_METHOD("get_size"), &TextParagraph::get_size);
111
112 ClassDB::bind_method(D_METHOD("get_rid"), &TextParagraph::get_rid);
113 ClassDB::bind_method(D_METHOD("get_line_rid", "line"), &TextParagraph::get_line_rid);
114 ClassDB::bind_method(D_METHOD("get_dropcap_rid"), &TextParagraph::get_dropcap_rid);
115
116 ClassDB::bind_method(D_METHOD("get_range"), &TextParagraph::get_range);
117
118 ClassDB::bind_method(D_METHOD("get_line_count"), &TextParagraph::get_line_count);
119
120 ClassDB::bind_method(D_METHOD("set_max_lines_visible", "max_lines_visible"), &TextParagraph::set_max_lines_visible);
121 ClassDB::bind_method(D_METHOD("get_max_lines_visible"), &TextParagraph::get_max_lines_visible);
122
123 ADD_PROPERTY(PropertyInfo(Variant::INT, "max_lines_visible"), "set_max_lines_visible", "get_max_lines_visible");
124
125 ClassDB::bind_method(D_METHOD("set_line_spacing", "line_spacing"), &TextParagraph::set_line_spacing);
126 ClassDB::bind_method(D_METHOD("get_line_spacing"), &TextParagraph::get_line_spacing);
127
128 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "line_spacing"), "set_line_spacing", "get_line_spacing");
129
130 ClassDB::bind_method(D_METHOD("get_line_objects", "line"), &TextParagraph::get_line_objects);
131 ClassDB::bind_method(D_METHOD("get_line_object_rect", "line", "key"), &TextParagraph::get_line_object_rect);
132 ClassDB::bind_method(D_METHOD("get_line_size", "line"), &TextParagraph::get_line_size);
133 ClassDB::bind_method(D_METHOD("get_line_range", "line"), &TextParagraph::get_line_range);
134 ClassDB::bind_method(D_METHOD("get_line_ascent", "line"), &TextParagraph::get_line_ascent);
135 ClassDB::bind_method(D_METHOD("get_line_descent", "line"), &TextParagraph::get_line_descent);
136 ClassDB::bind_method(D_METHOD("get_line_width", "line"), &TextParagraph::get_line_width);
137 ClassDB::bind_method(D_METHOD("get_line_underline_position", "line"), &TextParagraph::get_line_underline_position);
138 ClassDB::bind_method(D_METHOD("get_line_underline_thickness", "line"), &TextParagraph::get_line_underline_thickness);
139
140 ClassDB::bind_method(D_METHOD("get_dropcap_size"), &TextParagraph::get_dropcap_size);
141 ClassDB::bind_method(D_METHOD("get_dropcap_lines"), &TextParagraph::get_dropcap_lines);
142
143 ClassDB::bind_method(D_METHOD("draw", "canvas", "pos", "color", "dc_color", "oversampling"), &TextParagraph::draw, DEFVAL(Color(1, 1, 1)), DEFVAL(Color(1, 1, 1)), DEFVAL(0.0));
144 ClassDB::bind_method(D_METHOD("draw_outline", "canvas", "pos", "outline_size", "color", "dc_color", "oversampling"), &TextParagraph::draw_outline, DEFVAL(1), DEFVAL(Color(1, 1, 1)), DEFVAL(Color(1, 1, 1)), DEFVAL(0.0));
145
146 ClassDB::bind_method(D_METHOD("draw_line", "canvas", "pos", "line", "color", "oversampling"), &TextParagraph::draw_line, DEFVAL(Color(1, 1, 1)), DEFVAL(0.0));
147 ClassDB::bind_method(D_METHOD("draw_line_outline", "canvas", "pos", "line", "outline_size", "color", "oversampling"), &TextParagraph::draw_line_outline, DEFVAL(1), DEFVAL(Color(1, 1, 1)), DEFVAL(0.0));
148
149 ClassDB::bind_method(D_METHOD("draw_dropcap", "canvas", "pos", "color", "oversampling"), &TextParagraph::draw_dropcap, DEFVAL(Color(1, 1, 1)), DEFVAL(0.0));
150 ClassDB::bind_method(D_METHOD("draw_dropcap_outline", "canvas", "pos", "outline_size", "color", "oversampling"), &TextParagraph::draw_dropcap_outline, DEFVAL(1), DEFVAL(Color(1, 1, 1)), DEFVAL(0.0));
151
152 ClassDB::bind_method(D_METHOD("hit_test", "coords"), &TextParagraph::hit_test);
153 }
154
155 void TextParagraph::_shape_lines() const {
156 // When a shaped text is invalidated by an external source, we want to reshape it.
157 if (!TS->shaped_text_is_ready(rid) || !TS->shaped_text_is_ready(dropcap_rid)) {
158 lines_dirty = true;
159 }
160
161 for (const RID &line_rid : lines_rid) {
162 if (!TS->shaped_text_is_ready(line_rid)) {
163 lines_dirty = true;
164 break;
165 }
166 }
167
168 if (lines_dirty) {
169 for (const RID &line_rid : lines_rid) {
170 TS->free_rid(line_rid);
171 }
172 lines_rid.clear();
173
174 if (!tab_stops.is_empty()) {
175 TS->shaped_text_tab_align(rid, tab_stops);
176 }
177
178 float h_offset = 0.f;
179 float v_offset = 0.f;
180 int start = 0;
181 dropcap_lines = 0;
182
183 if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
184 h_offset = TS->shaped_text_get_size(dropcap_rid).x + dropcap_margins.size.x + dropcap_margins.position.x;
185 v_offset = TS->shaped_text_get_size(dropcap_rid).y + dropcap_margins.size.y + dropcap_margins.position.y;
186 } else {
187 h_offset = TS->shaped_text_get_size(dropcap_rid).y + dropcap_margins.size.y + dropcap_margins.position.y;
188 v_offset = TS->shaped_text_get_size(dropcap_rid).x + dropcap_margins.size.x + dropcap_margins.position.x;
189 }
190
191 Size2i range = TS->shaped_text_get_range(rid);
192 if (h_offset > 0) {
193 // Dropcap, flow around.
194 PackedInt32Array line_breaks = TS->shaped_text_get_line_breaks(rid, width - h_offset, 0, brk_flags);
195 for (int i = 0; i < line_breaks.size(); i = i + 2) {
196 RID line = TS->shaped_text_substr(rid, line_breaks[i], line_breaks[i + 1] - line_breaks[i]);
197 float h = (TS->shaped_text_get_orientation(line) == TextServer::ORIENTATION_HORIZONTAL) ? TS->shaped_text_get_size(line).y : TS->shaped_text_get_size(line).x;
198 h += line_spacing;
199 if (!tab_stops.is_empty()) {
200 TS->shaped_text_tab_align(line, tab_stops);
201 }
202 start = (i < line_breaks.size() - 2) ? line_breaks[i + 2] : range.y;
203 lines_rid.push_back(line);
204 if (v_offset < h) {
205 break;
206 }
207 dropcap_lines++;
208 v_offset -= h;
209 }
210 }
211 // Use fixed for the rest of lines.
212 if (start == 0 || start < range.y) {
213 PackedInt32Array line_breaks = TS->shaped_text_get_line_breaks(rid, width, start, brk_flags);
214 for (int i = 0; i < line_breaks.size(); i = i + 2) {
215 RID line = TS->shaped_text_substr(rid, line_breaks[i], line_breaks[i + 1] - line_breaks[i]);
216 if (!tab_stops.is_empty()) {
217 TS->shaped_text_tab_align(line, tab_stops);
218 }
219 lines_rid.push_back(line);
220 }
221 }
222
223 BitField<TextServer::TextOverrunFlag> overrun_flags = TextServer::get_overrun_flags_from_behavior(overrun_behavior);
224
225 bool autowrap_enabled = brk_flags.has_flag(TextServer::BREAK_WORD_BOUND) || brk_flags.has_flag(TextServer::BREAK_GRAPHEME_BOUND);
226
227 // Fill after min_size calculation.
228 if (autowrap_enabled) {
229 int visible_lines = (max_lines_visible >= 0) ? MIN(max_lines_visible, (int)lines_rid.size()) : (int)lines_rid.size();
230 bool lines_hidden = visible_lines > 0 && visible_lines < (int)lines_rid.size();
231 if (lines_hidden) {
232 overrun_flags.set_flag(TextServer::OVERRUN_ENFORCE_ELLIPSIS);
233 }
234 if (alignment == HORIZONTAL_ALIGNMENT_FILL) {
235 int jst_to_line = visible_lines;
236 if (lines_rid.size() == 1 && jst_flags.has_flag(TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE)) {
237 jst_to_line = lines_rid.size();
238 } else {
239 if (jst_flags.has_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE)) {
240 jst_to_line = visible_lines - 1;
241 }
242 if (jst_flags.has_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE_WITH_VISIBLE_CHARS)) {
243 for (int i = visible_lines - 1; i >= 0; i--) {
244 if (TS->shaped_text_has_visible_chars(lines_rid[i])) {
245 jst_to_line = i;
246 break;
247 }
248 }
249 }
250 }
251 for (int i = 0; i < (int)lines_rid.size(); i++) {
252 float line_w = (i <= dropcap_lines) ? (width - h_offset) : width;
253 if (i < jst_to_line) {
254 TS->shaped_text_fit_to_width(lines_rid[i], line_w, jst_flags);
255 } else if (i == (visible_lines - 1)) {
256 TS->shaped_text_set_custom_ellipsis(lines_rid[visible_lines - 1], (el_char.length() > 0) ? el_char[0] : 0x2026);
257 TS->shaped_text_overrun_trim_to_width(lines_rid[visible_lines - 1], line_w, overrun_flags);
258 }
259 }
260 } else if (lines_hidden) {
261 TS->shaped_text_set_custom_ellipsis(lines_rid[visible_lines - 1], (el_char.length() > 0) ? el_char[0] : 0x2026);
262 TS->shaped_text_overrun_trim_to_width(lines_rid[visible_lines - 1], (visible_lines - 1 <= dropcap_lines) ? (width - h_offset) : width, overrun_flags);
263 }
264 } else {
265 // Autowrap disabled.
266 int jst_to_line = lines_rid.size();
267 if (lines_rid.size() == 1 && jst_flags.has_flag(TextServer::JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE)) {
268 jst_to_line = lines_rid.size();
269 } else {
270 if (jst_flags.has_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE)) {
271 jst_to_line = lines_rid.size() - 1;
272 }
273 if (jst_flags.has_flag(TextServer::JUSTIFICATION_SKIP_LAST_LINE_WITH_VISIBLE_CHARS)) {
274 for (int i = lines_rid.size() - 1; i >= 0; i--) {
275 if (TS->shaped_text_has_visible_chars(lines_rid[i])) {
276 jst_to_line = i;
277 break;
278 }
279 }
280 }
281 }
282 for (int i = 0; i < (int)lines_rid.size(); i++) {
283 float line_w = (i <= dropcap_lines) ? (width - h_offset) : width;
284 if (i < jst_to_line && alignment == HORIZONTAL_ALIGNMENT_FILL) {
285 TS->shaped_text_fit_to_width(lines_rid[i], line_w, jst_flags);
286 overrun_flags.set_flag(TextServer::OVERRUN_JUSTIFICATION_AWARE);
287 TS->shaped_text_set_custom_ellipsis(lines_rid[i], (el_char.length() > 0) ? el_char[0] : 0x2026);
288 TS->shaped_text_overrun_trim_to_width(lines_rid[i], line_w, overrun_flags);
289 TS->shaped_text_fit_to_width(lines_rid[i], line_w, jst_flags | TextServer::JUSTIFICATION_CONSTRAIN_ELLIPSIS);
290 } else {
291 TS->shaped_text_set_custom_ellipsis(lines_rid[i], (el_char.length() > 0) ? el_char[0] : 0x2026);
292 TS->shaped_text_overrun_trim_to_width(lines_rid[i], line_w, overrun_flags);
293 }
294 }
295 }
296 lines_dirty = false;
297 }
298 }
299
300 RID TextParagraph::get_rid() const {
301 return rid;
302 }
303
304 RID TextParagraph::get_line_rid(int p_line) const {
305 _THREAD_SAFE_METHOD_
306
307 _shape_lines();
308 ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)lines_rid.size(), RID());
309 return lines_rid[p_line];
310 }
311
312 RID TextParagraph::get_dropcap_rid() const {
313 return dropcap_rid;
314 }
315
316 void TextParagraph::clear() {
317 _THREAD_SAFE_METHOD_
318
319 for (const RID &line_rid : lines_rid) {
320 TS->free_rid(line_rid);
321 }
322 lines_rid.clear();
323 TS->shaped_text_clear(rid);
324 TS->shaped_text_clear(dropcap_rid);
325 }
326
327 Ref<TextParagraph> TextParagraph::duplicate() const {
328 Ref<TextParagraph> copy;
329 copy.instantiate();
330 if (dropcap_rid.is_valid()) {
331 TS->free_rid(copy->dropcap_rid);
332 copy->dropcap_rid = TS->shaped_text_duplicate(dropcap_rid);
333 }
334 copy->dropcap_lines = dropcap_lines;
335 copy->dropcap_margins = dropcap_margins;
336 if (rid.is_valid()) {
337 TS->free_rid(copy->rid);
338 copy->rid = TS->shaped_text_duplicate(rid);
339 }
340 copy->lines_dirty = true;
341 copy->line_spacing = line_spacing;
342 copy->width = width;
343 copy->max_lines_visible = max_lines_visible;
344 copy->brk_flags = brk_flags;
345 copy->jst_flags = jst_flags;
346 copy->el_char = el_char;
347 copy->overrun_behavior = overrun_behavior;
348 copy->alignment = alignment;
349 copy->tab_stops = tab_stops;
350
351 return copy;
352 }
353
354 void TextParagraph::set_preserve_invalid(bool p_enabled) {
355 _THREAD_SAFE_METHOD_
356
357 TS->shaped_text_set_preserve_invalid(rid, p_enabled);
358 TS->shaped_text_set_preserve_invalid(dropcap_rid, p_enabled);
359 lines_dirty = true;
360 }
361
362 bool TextParagraph::get_preserve_invalid() const {
363 _THREAD_SAFE_METHOD_
364
365 return TS->shaped_text_get_preserve_invalid(rid);
366 }
367
368 void TextParagraph::set_preserve_control(bool p_enabled) {
369 _THREAD_SAFE_METHOD_
370
371 TS->shaped_text_set_preserve_control(rid, p_enabled);
372 TS->shaped_text_set_preserve_control(dropcap_rid, p_enabled);
373 lines_dirty = true;
374 }
375
376 bool TextParagraph::get_preserve_control() const {
377 _THREAD_SAFE_METHOD_
378
379 return TS->shaped_text_get_preserve_control(rid);
380 }
381
382 void TextParagraph::set_direction(TextServer::Direction p_direction) {
383 _THREAD_SAFE_METHOD_
384
385 TS->shaped_text_set_direction(rid, p_direction);
386 TS->shaped_text_set_direction(dropcap_rid, p_direction);
387 lines_dirty = true;
388 }
389
390 TextServer::Direction TextParagraph::get_direction() const {
391 _THREAD_SAFE_METHOD_
392
393 _shape_lines();
394 return TS->shaped_text_get_direction(rid);
395 }
396
397 TextServer::Direction TextParagraph::get_inferred_direction() const {
398 _THREAD_SAFE_METHOD_
399
400 const_cast<TextParagraph *>(this)->_shape_lines();
401 return TS->shaped_text_get_inferred_direction(rid);
402 }
403
404 void TextParagraph::set_custom_punctuation(const String &p_punct) {
405 _THREAD_SAFE_METHOD_
406
407 TS->shaped_text_set_custom_punctuation(rid, p_punct);
408 lines_dirty = true;
409 }
410
411 String TextParagraph::get_custom_punctuation() const {
412 _THREAD_SAFE_METHOD_
413
414 return TS->shaped_text_get_custom_punctuation(rid);
415 }
416
417 void TextParagraph::set_orientation(TextServer::Orientation p_orientation) {
418 _THREAD_SAFE_METHOD_
419
420 TS->shaped_text_set_orientation(rid, p_orientation);
421 TS->shaped_text_set_orientation(dropcap_rid, p_orientation);
422 lines_dirty = true;
423 }
424
425 TextServer::Orientation TextParagraph::get_orientation() const {
426 _THREAD_SAFE_METHOD_
427
428 _shape_lines();
429 return TS->shaped_text_get_orientation(rid);
430 }
431
432 bool TextParagraph::set_dropcap(const String &p_text, const Ref<Font> &p_font, int p_font_size, const Rect2 &p_dropcap_margins, const String &p_language) {
433 _THREAD_SAFE_METHOD_
434 ERR_FAIL_COND_V(p_font.is_null(), false);
435 TS->shaped_text_clear(dropcap_rid);
436 dropcap_margins = p_dropcap_margins;
437 bool res = TS->shaped_text_add_string(dropcap_rid, p_text, p_font->get_rids(), p_font_size, p_font->get_opentype_features(), p_language);
438 lines_dirty = true;
439 return res;
440 }
441
442 void TextParagraph::clear_dropcap() {
443 _THREAD_SAFE_METHOD_
444 dropcap_margins = Rect2();
445 TS->shaped_text_clear(dropcap_rid);
446 lines_dirty = true;
447 }
448
449 bool TextParagraph::add_string(const String &p_text, const Ref<Font> &p_font, int p_font_size, const String &p_language, const Variant &p_meta) {
450 _THREAD_SAFE_METHOD_
451 ERR_FAIL_COND_V(p_font.is_null(), false);
452 bool res = TS->shaped_text_add_string(rid, p_text, p_font->get_rids(), p_font_size, p_font->get_opentype_features(), p_language, p_meta);
453 lines_dirty = true;
454 return res;
455 }
456
457 void TextParagraph::set_bidi_override(const Array &p_override) {
458 _THREAD_SAFE_METHOD_
459
460 TS->shaped_text_set_bidi_override(rid, p_override);
461 lines_dirty = true;
462 }
463
464 bool TextParagraph::add_object(Variant p_key, const Size2 &p_size, InlineAlignment p_inline_align, int p_length, float p_baseline) {
465 _THREAD_SAFE_METHOD_
466
467 bool res = TS->shaped_text_add_object(rid, p_key, p_size, p_inline_align, p_length, p_baseline);
468 lines_dirty = true;
469 return res;
470 }
471
472 bool TextParagraph::resize_object(Variant p_key, const Size2 &p_size, InlineAlignment p_inline_align, float p_baseline) {
473 _THREAD_SAFE_METHOD_
474
475 bool res = TS->shaped_text_resize_object(rid, p_key, p_size, p_inline_align, p_baseline);
476 lines_dirty = true;
477 return res;
478 }
479
480 bool TextParagraph::has_object(Variant p_key) const {
481 _THREAD_SAFE_METHOD_
482
483 return TS->shaped_text_has_object(rid, p_key);
484 }
485
486 void TextParagraph::set_alignment(HorizontalAlignment p_alignment) {
487 _THREAD_SAFE_METHOD_
488
489 if (alignment != p_alignment) {
490 if (alignment == HORIZONTAL_ALIGNMENT_FILL || p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
491 alignment = p_alignment;
492 lines_dirty = true;
493 } else {
494 alignment = p_alignment;
495 }
496 }
497 }
498
499 HorizontalAlignment TextParagraph::get_alignment() const {
500 return alignment;
501 }
502
503 void TextParagraph::tab_align(const Vector<float> &p_tab_stops) {
504 _THREAD_SAFE_METHOD_
505
506 tab_stops = p_tab_stops;
507 lines_dirty = true;
508 }
509
510 void TextParagraph::set_justification_flags(BitField<TextServer::JustificationFlag> p_flags) {
511 _THREAD_SAFE_METHOD_
512
513 if (jst_flags != p_flags) {
514 jst_flags = p_flags;
515 lines_dirty = true;
516 }
517 }
518
519 BitField<TextServer::JustificationFlag> TextParagraph::get_justification_flags() const {
520 return jst_flags;
521 }
522
523 void TextParagraph::set_break_flags(BitField<TextServer::LineBreakFlag> p_flags) {
524 _THREAD_SAFE_METHOD_
525
526 if (brk_flags != p_flags) {
527 brk_flags = p_flags;
528 lines_dirty = true;
529 }
530 }
531
532 BitField<TextServer::LineBreakFlag> TextParagraph::get_break_flags() const {
533 return brk_flags;
534 }
535
536 void TextParagraph::set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior) {
537 _THREAD_SAFE_METHOD_
538
539 if (overrun_behavior != p_behavior) {
540 overrun_behavior = p_behavior;
541 lines_dirty = true;
542 }
543 }
544
545 TextServer::OverrunBehavior TextParagraph::get_text_overrun_behavior() const {
546 return overrun_behavior;
547 }
548
549 void TextParagraph::set_ellipsis_char(const String &p_char) {
550 String c = p_char;
551 if (c.length() > 1) {
552 WARN_PRINT("Ellipsis must be exactly one character long (" + itos(c.length()) + " characters given).");
553 c = c.left(1);
554 }
555 if (el_char == c) {
556 return;
557 }
558 el_char = c;
559 lines_dirty = true;
560 }
561
562 String TextParagraph::get_ellipsis_char() const {
563 return el_char;
564 }
565
566 void TextParagraph::set_width(float p_width) {
567 _THREAD_SAFE_METHOD_
568
569 if (width != p_width) {
570 width = p_width;
571 lines_dirty = true;
572 }
573 }
574
575 float TextParagraph::get_width() const {
576 return width;
577 }
578
579 Size2 TextParagraph::get_non_wrapped_size() const {
580 _THREAD_SAFE_METHOD_
581
582 _shape_lines();
583 return TS->shaped_text_get_size(rid);
584 }
585
586 Size2 TextParagraph::get_size() const {
587 _THREAD_SAFE_METHOD_
588
589 _shape_lines();
590
591 float h_offset = 0.f;
592 float v_offset = 0.f;
593 if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
594 h_offset = TS->shaped_text_get_size(dropcap_rid).x + dropcap_margins.size.x + dropcap_margins.position.x;
595 v_offset = TS->shaped_text_get_size(dropcap_rid).y + dropcap_margins.size.y + dropcap_margins.position.y;
596 } else {
597 h_offset = TS->shaped_text_get_size(dropcap_rid).y + dropcap_margins.size.y + dropcap_margins.position.y;
598 v_offset = TS->shaped_text_get_size(dropcap_rid).x + dropcap_margins.size.x + dropcap_margins.position.x;
599 }
600
601 Size2 size;
602 int visible_lines = (max_lines_visible >= 0) ? MIN(max_lines_visible, (int)lines_rid.size()) : (int)lines_rid.size();
603 for (int i = 0; i < visible_lines; i++) {
604 Size2 lsize = TS->shaped_text_get_size(lines_rid[i]);
605 if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
606 if (h_offset > 0 && i <= dropcap_lines) {
607 lsize.x += h_offset;
608 }
609 size.x = MAX(size.x, lsize.x);
610 size.y += lsize.y;
611 if (i != visible_lines - 1) {
612 size.y += line_spacing;
613 }
614 } else {
615 if (h_offset > 0 && i <= dropcap_lines) {
616 lsize.y += h_offset;
617 }
618 size.x += lsize.x;
619 size.y = MAX(size.y, lsize.y);
620 if (i != visible_lines - 1) {
621 size.x += line_spacing;
622 }
623 }
624 }
625 if (h_offset > 0) {
626 if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
627 size.y = MAX(size.y, v_offset);
628 } else {
629 size.x = MAX(size.x, v_offset);
630 }
631 }
632 return size;
633 }
634
635 Vector2i TextParagraph::get_range() const {
636 _THREAD_SAFE_METHOD_
637
638 return TS->shaped_text_get_range(rid);
639 }
640
641 int TextParagraph::get_line_count() const {
642 _THREAD_SAFE_METHOD_
643
644 _shape_lines();
645 return (int)lines_rid.size();
646 }
647
648 void TextParagraph::set_max_lines_visible(int p_lines) {
649 _THREAD_SAFE_METHOD_
650
651 if (p_lines != max_lines_visible) {
652 max_lines_visible = p_lines;
653 lines_dirty = true;
654 }
655 }
656
657 int TextParagraph::get_max_lines_visible() const {
658 return max_lines_visible;
659 }
660
661 void TextParagraph::set_line_spacing(float p_spacing) {
662 _THREAD_SAFE_METHOD_
663
664 if (line_spacing != p_spacing) {
665 line_spacing = p_spacing;
666 lines_dirty = true;
667 }
668 }
669
670 float TextParagraph::get_line_spacing() const {
671 return line_spacing;
672 }
673
674 Array TextParagraph::get_line_objects(int p_line) const {
675 _THREAD_SAFE_METHOD_
676
677 _shape_lines();
678 ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)lines_rid.size(), Array());
679 return TS->shaped_text_get_objects(lines_rid[p_line]);
680 }
681
682 Rect2 TextParagraph::get_line_object_rect(int p_line, Variant p_key) const {
683 _THREAD_SAFE_METHOD_
684
685 _shape_lines();
686 ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)lines_rid.size(), Rect2());
687
688 Vector2 ofs;
689
690 float h_offset = 0.f;
691 if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
692 h_offset = TS->shaped_text_get_size(dropcap_rid).x + dropcap_margins.size.x + dropcap_margins.position.x;
693 } else {
694 h_offset = TS->shaped_text_get_size(dropcap_rid).y + dropcap_margins.size.y + dropcap_margins.position.y;
695 }
696
697 for (int i = 0; i <= p_line; i++) {
698 float l_width = width;
699 if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
700 ofs.x = 0.f;
701 ofs.y += TS->shaped_text_get_ascent(lines_rid[i]);
702 if (i <= dropcap_lines) {
703 if (TS->shaped_text_get_inferred_direction(dropcap_rid) == TextServer::DIRECTION_LTR) {
704 ofs.x -= h_offset;
705 }
706 l_width -= h_offset;
707 }
708 } else {
709 ofs.y = 0.f;
710 ofs.x += TS->shaped_text_get_ascent(lines_rid[i]);
711 if (i <= dropcap_lines) {
712 if (TS->shaped_text_get_inferred_direction(dropcap_rid) == TextServer::DIRECTION_LTR) {
713 ofs.y -= h_offset;
714 }
715 l_width -= h_offset;
716 }
717 }
718 float length = TS->shaped_text_get_width(lines_rid[i]);
719 if (width > 0) {
720 switch (alignment) {
721 case HORIZONTAL_ALIGNMENT_FILL:
722 if (TS->shaped_text_get_inferred_direction(lines_rid[i]) == TextServer::DIRECTION_RTL) {
723 if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
724 ofs.x += l_width - length;
725 } else {
726 ofs.y += l_width - length;
727 }
728 }
729 break;
730 case HORIZONTAL_ALIGNMENT_LEFT:
731 break;
732 case HORIZONTAL_ALIGNMENT_CENTER: {
733 if (length <= l_width) {
734 if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
735 ofs.x += Math::floor((l_width - length) / 2.0);
736 } else {
737 ofs.y += Math::floor((l_width - length) / 2.0);
738 }
739 } else if (TS->shaped_text_get_inferred_direction(lines_rid[i]) == TextServer::DIRECTION_RTL) {
740 if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
741 ofs.x += l_width - length;
742 } else {
743 ofs.y += l_width - length;
744 }
745 }
746 } break;
747 case HORIZONTAL_ALIGNMENT_RIGHT: {
748 if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
749 ofs.x += l_width - length;
750 } else {
751 ofs.y += l_width - length;
752 }
753 } break;
754 }
755 }
756 if (i != p_line) {
757 if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
758 ofs.x = 0.f;
759 ofs.y += TS->shaped_text_get_descent(lines_rid[i]) + line_spacing;
760 } else {
761 ofs.y = 0.f;
762 ofs.x += TS->shaped_text_get_descent(lines_rid[i]) + line_spacing;
763 }
764 }
765 }
766
767 Rect2 rect = TS->shaped_text_get_object_rect(lines_rid[p_line], p_key);
768 rect.position += ofs;
769
770 return rect;
771 }
772
773 Size2 TextParagraph::get_line_size(int p_line) const {
774 _THREAD_SAFE_METHOD_
775
776 _shape_lines();
777 ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)lines_rid.size(), Size2());
778 return TS->shaped_text_get_size(lines_rid[p_line]);
779 }
780
781 Vector2i TextParagraph::get_line_range(int p_line) const {
782 _THREAD_SAFE_METHOD_
783
784 _shape_lines();
785 ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)lines_rid.size(), Vector2i());
786 return TS->shaped_text_get_range(lines_rid[p_line]);
787 }
788
789 float TextParagraph::get_line_ascent(int p_line) const {
790 _THREAD_SAFE_METHOD_
791
792 _shape_lines();
793 ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)lines_rid.size(), 0.f);
794 return TS->shaped_text_get_ascent(lines_rid[p_line]);
795 }
796
797 float TextParagraph::get_line_descent(int p_line) const {
798 _THREAD_SAFE_METHOD_
799
800 _shape_lines();
801 ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)lines_rid.size(), 0.f);
802 return TS->shaped_text_get_descent(lines_rid[p_line]);
803 }
804
805 float TextParagraph::get_line_width(int p_line) const {
806 _THREAD_SAFE_METHOD_
807
808 _shape_lines();
809 ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)lines_rid.size(), 0.f);
810 return TS->shaped_text_get_width(lines_rid[p_line]);
811 }
812
813 float TextParagraph::get_line_underline_position(int p_line) const {
814 _THREAD_SAFE_METHOD_
815
816 _shape_lines();
817 ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)lines_rid.size(), 0.f);
818 return TS->shaped_text_get_underline_position(lines_rid[p_line]);
819 }
820
821 float TextParagraph::get_line_underline_thickness(int p_line) const {
822 _THREAD_SAFE_METHOD_
823
824 _shape_lines();
825 ERR_FAIL_COND_V(p_line < 0 || p_line >= (int)lines_rid.size(), 0.f);
826 return TS->shaped_text_get_underline_thickness(lines_rid[p_line]);
827 }
828
829 Size2 TextParagraph::get_dropcap_size() const {
830 _THREAD_SAFE_METHOD_
831
832 return TS->shaped_text_get_size(dropcap_rid) + dropcap_margins.size + dropcap_margins.position;
833 }
834
835 int TextParagraph::get_dropcap_lines() const {
836 return dropcap_lines;
837 }
838
839 void TextParagraph::draw(RID p_canvas, const Vector2 &p_pos, const Color &p_color, const Color &p_dc_color, float p_oversampling) const {
840 _THREAD_SAFE_METHOD_
841
842 _shape_lines();
843 Vector2 ofs = p_pos;
844 float h_offset = 0.f;
845 if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
846 h_offset = TS->shaped_text_get_size(dropcap_rid).x + dropcap_margins.size.x + dropcap_margins.position.x;
847 } else {
848 h_offset = TS->shaped_text_get_size(dropcap_rid).y + dropcap_margins.size.y + dropcap_margins.position.y;
849 }
850
851 if (h_offset > 0) {
852 // Draw dropcap.
853 Vector2 dc_off = ofs;
854 if (TS->shaped_text_get_inferred_direction(dropcap_rid) == TextServer::DIRECTION_RTL) {
855 if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
856 dc_off.x += width - h_offset;
857 } else {
858 dc_off.y += width - h_offset;
859 }
860 }
861 TS->shaped_text_draw(dropcap_rid, p_canvas, dc_off + Vector2(0, TS->shaped_text_get_ascent(dropcap_rid) + dropcap_margins.size.y + dropcap_margins.position.y / 2), -1, -1, p_dc_color, p_oversampling);
862 }
863
864 int lines_visible = (max_lines_visible >= 0) ? MIN(max_lines_visible, (int)lines_rid.size()) : (int)lines_rid.size();
865
866 for (int i = 0; i < lines_visible; i++) {
867 float l_width = width;
868 if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
869 ofs.x = p_pos.x;
870 ofs.y += TS->shaped_text_get_ascent(lines_rid[i]);
871 if (i <= dropcap_lines) {
872 if (TS->shaped_text_get_inferred_direction(dropcap_rid) == TextServer::DIRECTION_LTR) {
873 ofs.x -= h_offset;
874 }
875 l_width -= h_offset;
876 }
877 } else {
878 ofs.y = p_pos.y;
879 ofs.x += TS->shaped_text_get_ascent(lines_rid[i]);
880 if (i <= dropcap_lines) {
881 if (TS->shaped_text_get_inferred_direction(dropcap_rid) == TextServer::DIRECTION_LTR) {
882 ofs.y -= h_offset;
883 }
884 l_width -= h_offset;
885 }
886 }
887 float line_width = TS->shaped_text_get_width(lines_rid[i]);
888 if (width > 0) {
889 switch (alignment) {
890 case HORIZONTAL_ALIGNMENT_FILL:
891 if (TS->shaped_text_get_inferred_direction(lines_rid[i]) == TextServer::DIRECTION_RTL) {
892 if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
893 ofs.x += l_width - line_width;
894 } else {
895 ofs.y += l_width - line_width;
896 }
897 }
898 break;
899 case HORIZONTAL_ALIGNMENT_LEFT:
900 break;
901 case HORIZONTAL_ALIGNMENT_CENTER: {
902 if (line_width <= l_width) {
903 if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
904 ofs.x += Math::floor((l_width - line_width) / 2.0);
905 } else {
906 ofs.y += Math::floor((l_width - line_width) / 2.0);
907 }
908 } else if (TS->shaped_text_get_inferred_direction(lines_rid[i]) == TextServer::DIRECTION_RTL) {
909 if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
910 ofs.x += l_width - line_width;
911 } else {
912 ofs.y += l_width - line_width;
913 }
914 }
915 } break;
916 case HORIZONTAL_ALIGNMENT_RIGHT: {
917 if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
918 ofs.x += l_width - line_width;
919 } else {
920 ofs.y += l_width - line_width;
921 }
922 } break;
923 }
924 }
925 float clip_l;
926 if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
927 clip_l = MAX(0, p_pos.x - ofs.x);
928 } else {
929 clip_l = MAX(0, p_pos.y - ofs.y);
930 }
931 TS->shaped_text_draw(lines_rid[i], p_canvas, ofs, clip_l, clip_l + l_width, p_color, p_oversampling);
932 if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
933 ofs.x = p_pos.x;
934 ofs.y += TS->shaped_text_get_descent(lines_rid[i]) + line_spacing;
935 } else {
936 ofs.y = p_pos.y;
937 ofs.x += TS->shaped_text_get_descent(lines_rid[i]) + line_spacing;
938 }
939 }
940 }
941
942 void TextParagraph::draw_outline(RID p_canvas, const Vector2 &p_pos, int p_outline_size, const Color &p_color, const Color &p_dc_color, float p_oversampling) const {
943 _THREAD_SAFE_METHOD_
944
945 _shape_lines();
946 Vector2 ofs = p_pos;
947
948 float h_offset = 0.f;
949 if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
950 h_offset = TS->shaped_text_get_size(dropcap_rid).x + dropcap_margins.size.x + dropcap_margins.position.x;
951 } else {
952 h_offset = TS->shaped_text_get_size(dropcap_rid).y + dropcap_margins.size.y + dropcap_margins.position.y;
953 }
954
955 if (h_offset > 0) {
956 // Draw dropcap.
957 Vector2 dc_off = ofs;
958 if (TS->shaped_text_get_inferred_direction(dropcap_rid) == TextServer::DIRECTION_RTL) {
959 if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
960 dc_off.x += width - h_offset;
961 } else {
962 dc_off.y += width - h_offset;
963 }
964 }
965 TS->shaped_text_draw_outline(dropcap_rid, p_canvas, dc_off + Vector2(dropcap_margins.position.x, TS->shaped_text_get_ascent(dropcap_rid) + dropcap_margins.position.y), -1, -1, p_outline_size, p_dc_color, p_oversampling);
966 }
967
968 for (int i = 0; i < (int)lines_rid.size(); i++) {
969 float l_width = width;
970 if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
971 ofs.x = p_pos.x;
972 ofs.y += TS->shaped_text_get_ascent(lines_rid[i]);
973 if (i <= dropcap_lines) {
974 if (TS->shaped_text_get_inferred_direction(dropcap_rid) == TextServer::DIRECTION_LTR) {
975 ofs.x -= h_offset;
976 }
977 l_width -= h_offset;
978 }
979 } else {
980 ofs.y = p_pos.y;
981 ofs.x += TS->shaped_text_get_ascent(lines_rid[i]);
982 if (i <= dropcap_lines) {
983 if (TS->shaped_text_get_inferred_direction(dropcap_rid) == TextServer::DIRECTION_LTR) {
984 ofs.y -= h_offset;
985 }
986 l_width -= h_offset;
987 }
988 }
989 float length = TS->shaped_text_get_width(lines_rid[i]);
990 if (width > 0) {
991 switch (alignment) {
992 case HORIZONTAL_ALIGNMENT_FILL:
993 if (TS->shaped_text_get_inferred_direction(lines_rid[i]) == TextServer::DIRECTION_RTL) {
994 if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
995 ofs.x += l_width - length;
996 } else {
997 ofs.y += l_width - length;
998 }
999 }
1000 break;
1001 case HORIZONTAL_ALIGNMENT_LEFT:
1002 break;
1003 case HORIZONTAL_ALIGNMENT_CENTER: {
1004 if (length <= l_width) {
1005 if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
1006 ofs.x += Math::floor((l_width - length) / 2.0);
1007 } else {
1008 ofs.y += Math::floor((l_width - length) / 2.0);
1009 }
1010 } else if (TS->shaped_text_get_inferred_direction(lines_rid[i]) == TextServer::DIRECTION_RTL) {
1011 if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
1012 ofs.x += l_width - length;
1013 } else {
1014 ofs.y += l_width - length;
1015 }
1016 }
1017 } break;
1018 case HORIZONTAL_ALIGNMENT_RIGHT: {
1019 if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
1020 ofs.x += l_width - length;
1021 } else {
1022 ofs.y += l_width - length;
1023 }
1024 } break;
1025 }
1026 }
1027 float clip_l;
1028 if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
1029 clip_l = MAX(0, p_pos.x - ofs.x);
1030 } else {
1031 clip_l = MAX(0, p_pos.y - ofs.y);
1032 }
1033 TS->shaped_text_draw_outline(lines_rid[i], p_canvas, ofs, clip_l, clip_l + l_width, p_outline_size, p_color, p_oversampling);
1034 if (TS->shaped_text_get_orientation(lines_rid[i]) == TextServer::ORIENTATION_HORIZONTAL) {
1035 ofs.x = p_pos.x;
1036 ofs.y += TS->shaped_text_get_descent(lines_rid[i]) + line_spacing;
1037 } else {
1038 ofs.y = p_pos.y;
1039 ofs.x += TS->shaped_text_get_descent(lines_rid[i]) + line_spacing;
1040 }
1041 }
1042 }
1043
1044 int TextParagraph::hit_test(const Point2 &p_coords) const {
1045 _THREAD_SAFE_METHOD_
1046
1047 _shape_lines();
1048 Vector2 ofs;
1049 if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
1050 if (ofs.y < 0) {
1051 return 0;
1052 }
1053 } else {
1054 if (ofs.x < 0) {
1055 return 0;
1056 }
1057 }
1058 for (const RID &line_rid : lines_rid) {
1059 if (TS->shaped_text_get_orientation(line_rid) == TextServer::ORIENTATION_HORIZONTAL) {
1060 if ((p_coords.y >= ofs.y) && (p_coords.y <= ofs.y + TS->shaped_text_get_size(line_rid).y)) {
1061 return TS->shaped_text_hit_test_position(line_rid, p_coords.x);
1062 }
1063 ofs.y += TS->shaped_text_get_size(line_rid).y + line_spacing;
1064 } else {
1065 if ((p_coords.x >= ofs.x) && (p_coords.x <= ofs.x + TS->shaped_text_get_size(line_rid).x)) {
1066 return TS->shaped_text_hit_test_position(line_rid, p_coords.y);
1067 }
1068 ofs.y += TS->shaped_text_get_size(line_rid).x + line_spacing;
1069 }
1070 }
1071 return TS->shaped_text_get_range(rid).y;
1072 }
1073
1074 bool TextParagraph::is_dirty() {
1075 return lines_dirty;
1076 }
1077
1078 void TextParagraph::draw_dropcap(RID p_canvas, const Vector2 &p_pos, const Color &p_color, float p_oversampling) const {
1079 _THREAD_SAFE_METHOD_
1080
1081 Vector2 ofs = p_pos;
1082 float h_offset = 0.f;
1083 if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
1084 h_offset = TS->shaped_text_get_size(dropcap_rid).x + dropcap_margins.size.x + dropcap_margins.position.x;
1085 } else {
1086 h_offset = TS->shaped_text_get_size(dropcap_rid).y + dropcap_margins.size.y + dropcap_margins.position.y;
1087 }
1088
1089 if (h_offset > 0) {
1090 // Draw dropcap.
1091 if (TS->shaped_text_get_inferred_direction(dropcap_rid) == TextServer::DIRECTION_RTL) {
1092 if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
1093 ofs.x += width - h_offset;
1094 } else {
1095 ofs.y += width - h_offset;
1096 }
1097 }
1098 TS->shaped_text_draw(dropcap_rid, p_canvas, ofs + Vector2(dropcap_margins.position.x, TS->shaped_text_get_ascent(dropcap_rid) + dropcap_margins.position.y), -1, -1, p_color, p_oversampling);
1099 }
1100 }
1101
1102 void TextParagraph::draw_dropcap_outline(RID p_canvas, const Vector2 &p_pos, int p_outline_size, const Color &p_color, float p_oversampling) const {
1103 _THREAD_SAFE_METHOD_
1104
1105 Vector2 ofs = p_pos;
1106 float h_offset = 0.f;
1107 if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
1108 h_offset = TS->shaped_text_get_size(dropcap_rid).x + dropcap_margins.size.x + dropcap_margins.position.x;
1109 } else {
1110 h_offset = TS->shaped_text_get_size(dropcap_rid).y + dropcap_margins.size.y + dropcap_margins.position.y;
1111 }
1112
1113 if (h_offset > 0) {
1114 // Draw dropcap.
1115 if (TS->shaped_text_get_inferred_direction(dropcap_rid) == TextServer::DIRECTION_RTL) {
1116 if (TS->shaped_text_get_orientation(dropcap_rid) == TextServer::ORIENTATION_HORIZONTAL) {
1117 ofs.x += width - h_offset;
1118 } else {
1119 ofs.y += width - h_offset;
1120 }
1121 }
1122 TS->shaped_text_draw_outline(dropcap_rid, p_canvas, ofs + Vector2(dropcap_margins.position.x, TS->shaped_text_get_ascent(dropcap_rid) + dropcap_margins.position.y), -1, -1, p_outline_size, p_color, p_oversampling);
1123 }
1124 }
1125
1126 void TextParagraph::draw_line(RID p_canvas, const Vector2 &p_pos, int p_line, const Color &p_color, float p_oversampling) const {
1127 _THREAD_SAFE_METHOD_
1128
1129 _shape_lines();
1130 ERR_FAIL_COND(p_line < 0 || p_line >= (int)lines_rid.size());
1131
1132 Vector2 ofs = p_pos;
1133
1134 if (TS->shaped_text_get_orientation(lines_rid[p_line]) == TextServer::ORIENTATION_HORIZONTAL) {
1135 ofs.y += TS->shaped_text_get_ascent(lines_rid[p_line]);
1136 } else {
1137 ofs.x += TS->shaped_text_get_ascent(lines_rid[p_line]);
1138 }
1139 return TS->shaped_text_draw(lines_rid[p_line], p_canvas, ofs, -1, -1, p_color, p_oversampling);
1140 }
1141
1142 void TextParagraph::draw_line_outline(RID p_canvas, const Vector2 &p_pos, int p_line, int p_outline_size, const Color &p_color, float p_oversampling) const {
1143 _THREAD_SAFE_METHOD_
1144
1145 _shape_lines();
1146 ERR_FAIL_COND(p_line < 0 || p_line >= (int)lines_rid.size());
1147
1148 Vector2 ofs = p_pos;
1149 if (TS->shaped_text_get_orientation(lines_rid[p_line]) == TextServer::ORIENTATION_HORIZONTAL) {
1150 ofs.y += TS->shaped_text_get_ascent(lines_rid[p_line]);
1151 } else {
1152 ofs.x += TS->shaped_text_get_ascent(lines_rid[p_line]);
1153 }
1154 return TS->shaped_text_draw_outline(lines_rid[p_line], p_canvas, ofs, -1, -1, p_outline_size, p_color, p_oversampling);
1155 }
1156
1157 TextParagraph::TextParagraph(const String &p_text, const Ref<Font> &p_font, int p_font_size, const String &p_language, float p_width, TextServer::Direction p_direction, TextServer::Orientation p_orientation) {
1158 rid = TS->create_shaped_text(p_direction, p_orientation);
1159 if (p_font.is_valid()) {
1160 TS->shaped_text_add_string(rid, p_text, p_font->get_rids(), p_font_size, p_font->get_opentype_features(), p_language);
1161 }
1162 width = p_width;
1163 }
1164
1165 TextParagraph::TextParagraph() {
1166 rid = TS->create_shaped_text();
1167 dropcap_rid = TS->create_shaped_text();
1168 }
1169
1170 TextParagraph::~TextParagraph() {
1171 for (const RID &line_rid : lines_rid) {
1172 TS->free_rid(line_rid);
1173 }
1174 lines_rid.clear();
1175 TS->free_rid(rid);
1176 TS->free_rid(dropcap_rid);
1177 }