Debian Codegraph @

scene/resources/text_line.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* text_line.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_line.h"
32 #include "text_line.compat.inc"
33
34 void TextLine::_bind_methods() {
35 ClassDB::bind_method(D_METHOD("clear"), &TextLine::clear);
36 ClassDB::bind_method(D_METHOD("duplicate"), &TextLine::duplicate);
37
38 ClassDB::bind_method(D_METHOD("set_direction", "direction"), &TextLine::set_direction);
39 ClassDB::bind_method(D_METHOD("get_direction"), &TextLine::get_direction);
40 ClassDB::bind_method(D_METHOD("get_inferred_direction"), &TextLine::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_orientation", "orientation"), &TextLine::set_orientation);
49 ClassDB::bind_method(D_METHOD("get_orientation"), &TextLine::get_orientation);
50
51 ADD_PROPERTY(PropertyInfo(Variant::INT, "orientation", PROPERTY_HINT_ENUM, "Horizontal,Orientation"), "set_orientation", "get_orientation");
52
53 ClassDB::bind_method(D_METHOD("set_preserve_invalid", "enabled"), &TextLine::set_preserve_invalid);
54 ClassDB::bind_method(D_METHOD("get_preserve_invalid"), &TextLine::get_preserve_invalid);
55
56 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "preserve_invalid"), "set_preserve_invalid", "get_preserve_invalid");
57
58 ClassDB::bind_method(D_METHOD("set_preserve_control", "enabled"), &TextLine::set_preserve_control);
59 ClassDB::bind_method(D_METHOD("get_preserve_control"), &TextLine::get_preserve_control);
60
61 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "preserve_control"), "set_preserve_control", "get_preserve_control");
62
63 ClassDB::bind_method(D_METHOD("set_bidi_override", "override"), &TextLine::set_bidi_override);
64
65 ClassDB::bind_method(D_METHOD("add_string", "text", "font", "font_size", "language", "meta"), &TextLine::add_string, DEFVAL(""), DEFVAL(Variant()));
66 ClassDB::bind_method(D_METHOD("add_object", "key", "size", "inline_align", "length", "baseline"), &TextLine::add_object, DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(1), DEFVAL(0.0));
67 ClassDB::bind_method(D_METHOD("resize_object", "key", "size", "inline_align", "baseline"), &TextLine::resize_object, DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(0.0));
68 ClassDB::bind_method(D_METHOD("has_object", "key"), &TextLine::has_object);
69
70 ClassDB::bind_method(D_METHOD("set_width", "width"), &TextLine::set_width);
71 ClassDB::bind_method(D_METHOD("get_width"), &TextLine::get_width);
72
73 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "width"), "set_width", "get_width");
74
75 ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &TextLine::set_horizontal_alignment);
76 ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &TextLine::get_horizontal_alignment);
77
78 ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_horizontal_alignment", "get_horizontal_alignment");
79
80 ClassDB::bind_method(D_METHOD("tab_align", "tab_stops"), &TextLine::tab_align);
81
82 ClassDB::bind_method(D_METHOD("set_flags", "flags"), &TextLine::set_flags);
83 ClassDB::bind_method(D_METHOD("get_flags"), &TextLine::get_flags);
84
85 ADD_PROPERTY(PropertyInfo(Variant::INT, "flags", PROPERTY_HINT_FLAGS, "Kashida Justification,Word Justification,Trim Edge Spaces After Justification,Justify Only After Last Tab,Constrain Ellipsis"), "set_flags", "get_flags");
86
87 ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "overrun_behavior"), &TextLine::set_text_overrun_behavior);
88 ClassDB::bind_method(D_METHOD("get_text_overrun_behavior"), &TextLine::get_text_overrun_behavior);
89
90 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");
91
92 ClassDB::bind_method(D_METHOD("set_ellipsis_char", "char"), &TextLine::set_ellipsis_char);
93 ClassDB::bind_method(D_METHOD("get_ellipsis_char"), &TextLine::get_ellipsis_char);
94 ADD_PROPERTY(PropertyInfo(Variant::STRING, "ellipsis_char"), "set_ellipsis_char", "get_ellipsis_char");
95
96 ClassDB::bind_method(D_METHOD("get_objects"), &TextLine::get_objects);
97 ClassDB::bind_method(D_METHOD("get_object_rect", "key"), &TextLine::get_object_rect);
98
99 ClassDB::bind_method(D_METHOD("get_size"), &TextLine::get_size);
100
101 ClassDB::bind_method(D_METHOD("get_rid"), &TextLine::get_rid);
102
103 ClassDB::bind_method(D_METHOD("get_line_ascent"), &TextLine::get_line_ascent);
104 ClassDB::bind_method(D_METHOD("get_line_descent"), &TextLine::get_line_descent);
105 ClassDB::bind_method(D_METHOD("get_line_width"), &TextLine::get_line_width);
106 ClassDB::bind_method(D_METHOD("get_line_underline_position"), &TextLine::get_line_underline_position);
107 ClassDB::bind_method(D_METHOD("get_line_underline_thickness"), &TextLine::get_line_underline_thickness);
108
109 ClassDB::bind_method(D_METHOD("draw", "canvas", "pos", "color", "oversampling"), &TextLine::draw, DEFVAL(Color(1, 1, 1)), DEFVAL(0.0));
110 ClassDB::bind_method(D_METHOD("draw_outline", "canvas", "pos", "outline_size", "color", "oversampling"), &TextLine::draw_outline, DEFVAL(1), DEFVAL(Color(1, 1, 1)), DEFVAL(0.0));
111
112 ClassDB::bind_method(D_METHOD("hit_test", "coords"), &TextLine::hit_test);
113 }
114
115 void TextLine::_shape() const {
116 // When a shaped text is invalidated by an external source, we want to reshape it.
117 if (!TS->shaped_text_is_ready(rid)) {
118 dirty = true;
119 }
120
121 if (dirty) {
122 if (!tab_stops.is_empty()) {
123 TS->shaped_text_tab_align(rid, tab_stops);
124 }
125
126 BitField<TextServer::TextOverrunFlag> overrun_flags = TextServer::OVERRUN_NO_TRIM;
127 if (overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
128 overrun_flags = TextServer::get_overrun_flags_from_behavior(overrun_behavior);
129
130 if (alignment == HORIZONTAL_ALIGNMENT_FILL) {
131 TS->shaped_text_fit_to_width(rid, width, flags);
132 overrun_flags.set_flag(TextServer::OVERRUN_JUSTIFICATION_AWARE);
133 TS->shaped_text_set_custom_ellipsis(rid, (el_char.length() > 0) ? el_char[0] : 0x2026);
134 TS->shaped_text_overrun_trim_to_width(rid, width, overrun_flags);
135 } else {
136 TS->shaped_text_set_custom_ellipsis(rid, (el_char.length() > 0) ? el_char[0] : 0x2026);
137 TS->shaped_text_overrun_trim_to_width(rid, width, overrun_flags);
138 }
139 } else if (alignment == HORIZONTAL_ALIGNMENT_FILL) {
140 TS->shaped_text_fit_to_width(rid, width, flags);
141 }
142 dirty = false;
143 }
144 }
145
146 RID TextLine::get_rid() const {
147 return rid;
148 }
149
150 void TextLine::clear() {
151 TS->shaped_text_clear(rid);
152 }
153
154 Ref<TextLine> TextLine::duplicate() const {
155 Ref<TextLine> copy;
156 copy.instantiate();
157 if (rid.is_valid()) {
158 TS->free_rid(copy->rid);
159 copy->rid = TS->shaped_text_duplicate(rid);
160 }
161 copy->dirty = true;
162 copy->width = width;
163 copy->flags = flags;
164 copy->alignment = alignment;
165 copy->el_char = el_char;
166 copy->overrun_behavior = overrun_behavior;
167 copy->tab_stops = tab_stops;
168
169 return copy;
170 }
171
172 void TextLine::set_preserve_invalid(bool p_enabled) {
173 TS->shaped_text_set_preserve_invalid(rid, p_enabled);
174 dirty = true;
175 }
176
177 bool TextLine::get_preserve_invalid() const {
178 return TS->shaped_text_get_preserve_invalid(rid);
179 }
180
181 void TextLine::set_preserve_control(bool p_enabled) {
182 TS->shaped_text_set_preserve_control(rid, p_enabled);
183 dirty = true;
184 }
185
186 bool TextLine::get_preserve_control() const {
187 return TS->shaped_text_get_preserve_control(rid);
188 }
189
190 void TextLine::set_direction(TextServer::Direction p_direction) {
191 TS->shaped_text_set_direction(rid, p_direction);
192 dirty = true;
193 }
194
195 TextServer::Direction TextLine::get_direction() const {
196 return TS->shaped_text_get_direction(rid);
197 }
198
199 TextServer::Direction TextLine::get_inferred_direction() const {
200 return TS->shaped_text_get_inferred_direction(rid);
201 }
202
203 void TextLine::set_orientation(TextServer::Orientation p_orientation) {
204 TS->shaped_text_set_orientation(rid, p_orientation);
205 dirty = true;
206 }
207
208 TextServer::Orientation TextLine::get_orientation() const {
209 return TS->shaped_text_get_orientation(rid);
210 }
211
212 void TextLine::set_bidi_override(const Array &p_override) {
213 TS->shaped_text_set_bidi_override(rid, p_override);
214 dirty = true;
215 }
216
217 bool TextLine::add_string(const String &p_text, const Ref<Font> &p_font, int p_font_size, const String &p_language, const Variant &p_meta) {
218 ERR_FAIL_COND_V(p_font.is_null(), false);
219 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);
220 dirty = true;
221 return res;
222 }
223
224 bool TextLine::add_object(Variant p_key, const Size2 &p_size, InlineAlignment p_inline_align, int p_length, float p_baseline) {
225 bool res = TS->shaped_text_add_object(rid, p_key, p_size, p_inline_align, p_length, p_baseline);
226 dirty = true;
227 return res;
228 }
229
230 bool TextLine::resize_object(Variant p_key, const Size2 &p_size, InlineAlignment p_inline_align, float p_baseline) {
231 _shape();
232 return TS->shaped_text_resize_object(rid, p_key, p_size, p_inline_align, p_baseline);
233 }
234
235 bool TextLine::has_object(Variant p_key) const {
236 _shape();
237 return TS->shaped_text_has_object(rid, p_key);
238 }
239
240 Array TextLine::get_objects() const {
241 return TS->shaped_text_get_objects(rid);
242 }
243
244 Rect2 TextLine::get_object_rect(Variant p_key) const {
245 Vector2 ofs;
246
247 float length = TS->shaped_text_get_width(rid);
248 if (width > 0) {
249 switch (alignment) {
250 case HORIZONTAL_ALIGNMENT_FILL:
251 case HORIZONTAL_ALIGNMENT_LEFT:
252 break;
253 case HORIZONTAL_ALIGNMENT_CENTER: {
254 if (length <= width) {
255 if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
256 ofs.x += Math::floor((width - length) / 2.0);
257 } else {
258 ofs.y += Math::floor((width - length) / 2.0);
259 }
260 } else if (TS->shaped_text_get_inferred_direction(rid) == TextServer::DIRECTION_RTL) {
261 if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
262 ofs.x += width - length;
263 } else {
264 ofs.y += width - length;
265 }
266 }
267 } break;
268 case HORIZONTAL_ALIGNMENT_RIGHT: {
269 if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
270 ofs.x += width - length;
271 } else {
272 ofs.y += width - length;
273 }
274 } break;
275 }
276 }
277 if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
278 ofs.y += TS->shaped_text_get_ascent(rid);
279 } else {
280 ofs.x += TS->shaped_text_get_ascent(rid);
281 }
282
283 Rect2 rect = TS->shaped_text_get_object_rect(rid, p_key);
284 rect.position += ofs;
285
286 return rect;
287 }
288
289 void TextLine::set_horizontal_alignment(HorizontalAlignment p_alignment) {
290 if (alignment != p_alignment) {
291 if (alignment == HORIZONTAL_ALIGNMENT_FILL || p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
292 alignment = p_alignment;
293 dirty = true;
294 } else {
295 alignment = p_alignment;
296 }
297 }
298 }
299
300 HorizontalAlignment TextLine::get_horizontal_alignment() const {
301 return alignment;
302 }
303
304 void TextLine::tab_align(const Vector<float> &p_tab_stops) {
305 tab_stops = p_tab_stops;
306 dirty = true;
307 }
308
309 void TextLine::set_flags(BitField<TextServer::JustificationFlag> p_flags) {
310 if (flags != p_flags) {
311 flags = p_flags;
312 dirty = true;
313 }
314 }
315
316 BitField<TextServer::JustificationFlag> TextLine::get_flags() const {
317 return flags;
318 }
319
320 void TextLine::set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior) {
321 if (overrun_behavior != p_behavior) {
322 overrun_behavior = p_behavior;
323 dirty = true;
324 }
325 }
326
327 TextServer::OverrunBehavior TextLine::get_text_overrun_behavior() const {
328 return overrun_behavior;
329 }
330
331 void TextLine::set_ellipsis_char(const String &p_char) {
332 String c = p_char;
333 if (c.length() > 1) {
334 WARN_PRINT("Ellipsis must be exactly one character long (" + itos(c.length()) + " characters given).");
335 c = c.left(1);
336 }
337 if (el_char == c) {
338 return;
339 }
340 el_char = c;
341 dirty = true;
342 }
343
344 String TextLine::get_ellipsis_char() const {
345 return el_char;
346 }
347
348 void TextLine::set_width(float p_width) {
349 if (width == p_width) {
350 return;
351 }
352 width = p_width;
353 if (alignment == HORIZONTAL_ALIGNMENT_FILL || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING) {
354 dirty = true;
355 }
356 }
357
358 float TextLine::get_width() const {
359 return width;
360 }
361
362 Size2 TextLine::get_size() const {
363 _shape();
364 return TS->shaped_text_get_size(rid);
365 }
366
367 float TextLine::get_line_ascent() const {
368 _shape();
369 return TS->shaped_text_get_ascent(rid);
370 }
371
372 float TextLine::get_line_descent() const {
373 _shape();
374 return TS->shaped_text_get_descent(rid);
375 }
376
377 float TextLine::get_line_width() const {
378 _shape();
379 return TS->shaped_text_get_width(rid);
380 }
381
382 float TextLine::get_line_underline_position() const {
383 _shape();
384 return TS->shaped_text_get_underline_position(rid);
385 }
386
387 float TextLine::get_line_underline_thickness() const {
388 _shape();
389 return TS->shaped_text_get_underline_thickness(rid);
390 }
391
392 void TextLine::draw(RID p_canvas, const Vector2 &p_pos, const Color &p_color, float p_oversampling) const {
393 _shape();
394
395 Vector2 ofs = p_pos;
396
397 float length = TS->shaped_text_get_width(rid);
398 if (width > 0) {
399 switch (alignment) {
400 case HORIZONTAL_ALIGNMENT_FILL:
401 case HORIZONTAL_ALIGNMENT_LEFT:
402 break;
403 case HORIZONTAL_ALIGNMENT_CENTER: {
404 if (length <= width) {
405 if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
406 ofs.x += Math::floor((width - length) / 2.0);
407 } else {
408 ofs.y += Math::floor((width - length) / 2.0);
409 }
410 } else if (TS->shaped_text_get_inferred_direction(rid) == TextServer::DIRECTION_RTL) {
411 if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
412 ofs.x += width - length;
413 } else {
414 ofs.y += width - length;
415 }
416 }
417 } break;
418 case HORIZONTAL_ALIGNMENT_RIGHT: {
419 if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
420 ofs.x += width - length;
421 } else {
422 ofs.y += width - length;
423 }
424 } break;
425 }
426 }
427
428 float clip_l;
429 if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
430 ofs.y += TS->shaped_text_get_ascent(rid);
431 clip_l = MAX(0, p_pos.x - ofs.x);
432 } else {
433 ofs.x += TS->shaped_text_get_ascent(rid);
434 clip_l = MAX(0, p_pos.y - ofs.y);
435 }
436 return TS->shaped_text_draw(rid, p_canvas, ofs, clip_l, clip_l + width, p_color, p_oversampling);
437 }
438
439 void TextLine::draw_outline(RID p_canvas, const Vector2 &p_pos, int p_outline_size, const Color &p_color, float p_oversampling) const {
440 _shape();
441
442 Vector2 ofs = p_pos;
443
444 float length = TS->shaped_text_get_width(rid);
445 if (width > 0) {
446 switch (alignment) {
447 case HORIZONTAL_ALIGNMENT_FILL:
448 case HORIZONTAL_ALIGNMENT_LEFT:
449 break;
450 case HORIZONTAL_ALIGNMENT_CENTER: {
451 if (length <= width) {
452 if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
453 ofs.x += Math::floor((width - length) / 2.0);
454 } else {
455 ofs.y += Math::floor((width - length) / 2.0);
456 }
457 } else if (TS->shaped_text_get_inferred_direction(rid) == TextServer::DIRECTION_RTL) {
458 if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
459 ofs.x += width - length;
460 } else {
461 ofs.y += width - length;
462 }
463 }
464 } break;
465 case HORIZONTAL_ALIGNMENT_RIGHT: {
466 if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
467 ofs.x += width - length;
468 } else {
469 ofs.y += width - length;
470 }
471 } break;
472 }
473 }
474
475 float clip_l;
476 if (TS->shaped_text_get_orientation(rid) == TextServer::ORIENTATION_HORIZONTAL) {
477 ofs.y += TS->shaped_text_get_ascent(rid);
478 clip_l = MAX(0, p_pos.x - ofs.x);
479 } else {
480 ofs.x += TS->shaped_text_get_ascent(rid);
481 clip_l = MAX(0, p_pos.y - ofs.y);
482 }
483 return TS->shaped_text_draw_outline(rid, p_canvas, ofs, clip_l, clip_l + width, p_outline_size, p_color, p_oversampling);
484 }
485
486 int TextLine::hit_test(float p_coords) const {
487 _shape();
488
489 return TS->shaped_text_hit_test_position(rid, p_coords);
490 }
491
492 TextLine::TextLine(const String &p_text, const Ref<Font> &p_font, int p_font_size, const String &p_language, TextServer::Direction p_direction, TextServer::Orientation p_orientation) {
493 rid = TS->create_shaped_text(p_direction, p_orientation);
494 if (p_font.is_valid()) {
495 TS->shaped_text_add_string(rid, p_text, p_font->get_rids(), p_font_size, p_font->get_opentype_features(), p_language);
496 }
497 }
498
499 TextLine::TextLine() {
500 rid = TS->create_shaped_text();
501 }
502
503 TextLine::~TextLine() {
504 TS->free_rid(rid);
505 }