Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/text/text_server.cpp
20938 views
1
/**************************************************************************/
2
/* text_server.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_server.h"
32
#include "text_server.compat.inc"
33
34
#include "core/config/project_settings.h"
35
#include "core/os/main_loop.h"
36
#include "core/variant/typed_array.h"
37
#include "servers/rendering/rendering_server.h"
38
39
#ifndef DISABLE_DEPRECATED
40
#include "core/string/translation_server.h"
41
#endif // DISABLE_DEPRECATED
42
43
TextServerManager *TextServerManager::singleton = nullptr;
44
45
void TextServerManager::_bind_methods() {
46
ClassDB::bind_method(D_METHOD("add_interface", "interface"), &TextServerManager::add_interface);
47
ClassDB::bind_method(D_METHOD("get_interface_count"), &TextServerManager::get_interface_count);
48
ClassDB::bind_method(D_METHOD("remove_interface", "interface"), &TextServerManager::remove_interface);
49
ClassDB::bind_method(D_METHOD("get_interface", "idx"), &TextServerManager::get_interface);
50
ClassDB::bind_method(D_METHOD("get_interfaces"), &TextServerManager::get_interfaces);
51
ClassDB::bind_method(D_METHOD("find_interface", "name"), &TextServerManager::find_interface);
52
53
ClassDB::bind_method(D_METHOD("set_primary_interface", "index"), &TextServerManager::set_primary_interface);
54
ClassDB::bind_method(D_METHOD("get_primary_interface"), &TextServerManager::get_primary_interface);
55
56
ADD_SIGNAL(MethodInfo("interface_added", PropertyInfo(Variant::STRING_NAME, "interface_name")));
57
ADD_SIGNAL(MethodInfo("interface_removed", PropertyInfo(Variant::STRING_NAME, "interface_name")));
58
}
59
60
void TextServerManager::add_interface(const Ref<TextServer> &p_interface) {
61
ERR_FAIL_COND(p_interface.is_null());
62
63
for (int i = 0; i < interfaces.size(); i++) {
64
if (interfaces[i] == p_interface) {
65
ERR_PRINT("TextServer: Interface was already added.");
66
return;
67
};
68
};
69
70
interfaces.push_back(p_interface);
71
print_verbose("TextServer: Added interface \"" + p_interface->get_name() + "\"");
72
emit_signal(SNAME("interface_added"), p_interface->get_name());
73
}
74
75
void TextServerManager::remove_interface(const Ref<TextServer> &p_interface) {
76
ERR_FAIL_COND(p_interface.is_null());
77
ERR_FAIL_COND_MSG(p_interface == primary_interface, "TextServer: Can't remove primary interface.");
78
79
int idx = -1;
80
for (int i = 0; i < interfaces.size(); i++) {
81
if (interfaces[i] == p_interface) {
82
idx = i;
83
break;
84
};
85
};
86
87
ERR_FAIL_COND_MSG(idx == -1, "Interface not found.");
88
print_verbose("TextServer: Removed interface \"" + p_interface->get_name() + "\"");
89
emit_signal(SNAME("interface_removed"), p_interface->get_name());
90
interfaces.remove_at(idx);
91
}
92
93
int TextServerManager::get_interface_count() const {
94
return interfaces.size();
95
}
96
97
Ref<TextServer> TextServerManager::get_interface(int p_index) const {
98
ERR_FAIL_INDEX_V(p_index, interfaces.size(), nullptr);
99
return interfaces[p_index];
100
}
101
102
Ref<TextServer> TextServerManager::find_interface(const String &p_name) const {
103
int idx = -1;
104
for (int i = 0; i < interfaces.size(); i++) {
105
if (interfaces[i]->get_name() == p_name) {
106
idx = i;
107
break;
108
};
109
};
110
111
ERR_FAIL_COND_V_MSG(idx == -1, nullptr, "Interface not found.");
112
return interfaces[idx];
113
}
114
115
TypedArray<Dictionary> TextServerManager::get_interfaces() const {
116
TypedArray<Dictionary> ret;
117
118
for (int i = 0; i < interfaces.size(); i++) {
119
Dictionary iface_info;
120
121
iface_info["id"] = i;
122
iface_info["name"] = interfaces[i]->get_name();
123
124
ret.push_back(iface_info);
125
};
126
127
return ret;
128
}
129
130
void TextServerManager::set_primary_interface(const Ref<TextServer> &p_primary_interface) {
131
if (p_primary_interface.is_null()) {
132
print_verbose("TextServer: Clearing primary interface");
133
primary_interface.unref();
134
} else {
135
primary_interface = p_primary_interface;
136
print_verbose("TextServer: Primary interface set to: \"" + primary_interface->get_name() + "\".");
137
138
if (OS::get_singleton()->get_main_loop()) {
139
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_TEXT_SERVER_CHANGED);
140
}
141
}
142
}
143
144
TextServerManager::TextServerManager() {
145
singleton = this;
146
}
147
148
TextServerManager::~TextServerManager() {
149
if (primary_interface.is_valid()) {
150
primary_interface.unref();
151
}
152
while (interfaces.size() > 0) {
153
interfaces.remove_at(0);
154
}
155
singleton = nullptr;
156
}
157
158
/*************************************************************************/
159
160
bool Glyph::operator==(const Glyph &p_a) const {
161
return (p_a.index == index) && (p_a.font_rid == font_rid) && (p_a.font_size == font_size) && (p_a.start == start);
162
}
163
164
bool Glyph::operator!=(const Glyph &p_a) const {
165
return (p_a.index != index) || (p_a.font_rid != font_rid) || (p_a.font_size != font_size) || (p_a.start != start);
166
}
167
168
bool Glyph::operator<(const Glyph &p_a) const {
169
if (p_a.start == start) {
170
if (p_a.count == count) {
171
if ((p_a.flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL) {
172
return true;
173
} else {
174
return false;
175
}
176
}
177
return p_a.count > count;
178
}
179
return p_a.start < start;
180
}
181
182
bool Glyph::operator>(const Glyph &p_a) const {
183
if (p_a.start == start) {
184
if (p_a.count == count) {
185
if ((p_a.flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL) {
186
return false;
187
} else {
188
return true;
189
}
190
}
191
return p_a.count < count;
192
}
193
return p_a.start > start;
194
}
195
196
double TextServer::vp_oversampling = 0.0;
197
198
void TextServer::_bind_methods() {
199
ClassDB::bind_method(D_METHOD("has_feature", "feature"), &TextServer::has_feature);
200
ClassDB::bind_method(D_METHOD("get_name"), &TextServer::get_name);
201
ClassDB::bind_method(D_METHOD("get_features"), &TextServer::get_features);
202
ClassDB::bind_method(D_METHOD("load_support_data", "filename"), &TextServer::load_support_data);
203
204
ClassDB::bind_method(D_METHOD("get_support_data_filename"), &TextServer::get_support_data_filename);
205
ClassDB::bind_method(D_METHOD("get_support_data_info"), &TextServer::get_support_data_info);
206
ClassDB::bind_method(D_METHOD("save_support_data", "filename"), &TextServer::save_support_data);
207
ClassDB::bind_method(D_METHOD("get_support_data"), &TextServer::get_support_data);
208
ClassDB::bind_method(D_METHOD("is_locale_using_support_data", "locale"), &TextServer::is_locale_using_support_data);
209
210
ClassDB::bind_method(D_METHOD("is_locale_right_to_left", "locale"), &TextServer::is_locale_right_to_left);
211
212
ClassDB::bind_method(D_METHOD("name_to_tag", "name"), &TextServer::name_to_tag);
213
ClassDB::bind_method(D_METHOD("tag_to_name", "tag"), &TextServer::tag_to_name);
214
215
ClassDB::bind_method(D_METHOD("has", "rid"), &TextServer::has);
216
ClassDB::bind_method(D_METHOD("free_rid", "rid"), &TextServer::free_rid);
217
218
/* Font Interface */
219
220
ClassDB::bind_method(D_METHOD("create_font"), &TextServer::create_font);
221
ClassDB::bind_method(D_METHOD("create_font_linked_variation", "font_rid"), &TextServer::create_font_linked_variation);
222
223
ClassDB::bind_method(D_METHOD("font_set_data", "font_rid", "data"), &TextServer::font_set_data);
224
225
ClassDB::bind_method(D_METHOD("font_set_face_index", "font_rid", "face_index"), &TextServer::font_set_face_index);
226
ClassDB::bind_method(D_METHOD("font_get_face_index", "font_rid"), &TextServer::font_get_face_index);
227
228
ClassDB::bind_method(D_METHOD("font_get_face_count", "font_rid"), &TextServer::font_get_face_count);
229
230
ClassDB::bind_method(D_METHOD("font_set_style", "font_rid", "style"), &TextServer::font_set_style);
231
ClassDB::bind_method(D_METHOD("font_get_style", "font_rid"), &TextServer::font_get_style);
232
233
ClassDB::bind_method(D_METHOD("font_set_name", "font_rid", "name"), &TextServer::font_set_name);
234
ClassDB::bind_method(D_METHOD("font_get_name", "font_rid"), &TextServer::font_get_name);
235
ClassDB::bind_method(D_METHOD("font_get_ot_name_strings", "font_rid"), &TextServer::font_get_ot_name_strings);
236
237
ClassDB::bind_method(D_METHOD("font_set_style_name", "font_rid", "name"), &TextServer::font_set_style_name);
238
ClassDB::bind_method(D_METHOD("font_get_style_name", "font_rid"), &TextServer::font_get_style_name);
239
240
ClassDB::bind_method(D_METHOD("font_set_weight", "font_rid", "weight"), &TextServer::font_set_weight);
241
ClassDB::bind_method(D_METHOD("font_get_weight", "font_rid"), &TextServer::font_get_weight);
242
243
ClassDB::bind_method(D_METHOD("font_set_stretch", "font_rid", "weight"), &TextServer::font_set_stretch);
244
ClassDB::bind_method(D_METHOD("font_get_stretch", "font_rid"), &TextServer::font_get_stretch);
245
246
ClassDB::bind_method(D_METHOD("font_set_antialiasing", "font_rid", "antialiasing"), &TextServer::font_set_antialiasing);
247
ClassDB::bind_method(D_METHOD("font_get_antialiasing", "font_rid"), &TextServer::font_get_antialiasing);
248
249
ClassDB::bind_method(D_METHOD("font_set_disable_embedded_bitmaps", "font_rid", "disable_embedded_bitmaps"), &TextServer::font_set_disable_embedded_bitmaps);
250
ClassDB::bind_method(D_METHOD("font_get_disable_embedded_bitmaps", "font_rid"), &TextServer::font_get_disable_embedded_bitmaps);
251
252
ClassDB::bind_method(D_METHOD("font_set_generate_mipmaps", "font_rid", "generate_mipmaps"), &TextServer::font_set_generate_mipmaps);
253
ClassDB::bind_method(D_METHOD("font_get_generate_mipmaps", "font_rid"), &TextServer::font_get_generate_mipmaps);
254
255
ClassDB::bind_method(D_METHOD("font_set_multichannel_signed_distance_field", "font_rid", "msdf"), &TextServer::font_set_multichannel_signed_distance_field);
256
ClassDB::bind_method(D_METHOD("font_is_multichannel_signed_distance_field", "font_rid"), &TextServer::font_is_multichannel_signed_distance_field);
257
258
ClassDB::bind_method(D_METHOD("font_set_msdf_pixel_range", "font_rid", "msdf_pixel_range"), &TextServer::font_set_msdf_pixel_range);
259
ClassDB::bind_method(D_METHOD("font_get_msdf_pixel_range", "font_rid"), &TextServer::font_get_msdf_pixel_range);
260
261
ClassDB::bind_method(D_METHOD("font_set_msdf_size", "font_rid", "msdf_size"), &TextServer::font_set_msdf_size);
262
ClassDB::bind_method(D_METHOD("font_get_msdf_size", "font_rid"), &TextServer::font_get_msdf_size);
263
264
ClassDB::bind_method(D_METHOD("font_set_fixed_size", "font_rid", "fixed_size"), &TextServer::font_set_fixed_size);
265
ClassDB::bind_method(D_METHOD("font_get_fixed_size", "font_rid"), &TextServer::font_get_fixed_size);
266
267
ClassDB::bind_method(D_METHOD("font_set_fixed_size_scale_mode", "font_rid", "fixed_size_scale_mode"), &TextServer::font_set_fixed_size_scale_mode);
268
ClassDB::bind_method(D_METHOD("font_get_fixed_size_scale_mode", "font_rid"), &TextServer::font_get_fixed_size_scale_mode);
269
270
ClassDB::bind_method(D_METHOD("font_set_allow_system_fallback", "font_rid", "allow_system_fallback"), &TextServer::font_set_allow_system_fallback);
271
ClassDB::bind_method(D_METHOD("font_is_allow_system_fallback", "font_rid"), &TextServer::font_is_allow_system_fallback);
272
ClassDB::bind_method(D_METHOD("font_clear_system_fallback_cache"), &TextServer::font_clear_system_fallback_cache);
273
274
ClassDB::bind_method(D_METHOD("font_set_force_autohinter", "font_rid", "force_autohinter"), &TextServer::font_set_force_autohinter);
275
ClassDB::bind_method(D_METHOD("font_is_force_autohinter", "font_rid"), &TextServer::font_is_force_autohinter);
276
277
ClassDB::bind_method(D_METHOD("font_set_modulate_color_glyphs", "font_rid", "force_autohinter"), &TextServer::font_set_modulate_color_glyphs);
278
ClassDB::bind_method(D_METHOD("font_is_modulate_color_glyphs", "font_rid"), &TextServer::font_is_modulate_color_glyphs);
279
280
ClassDB::bind_method(D_METHOD("font_set_hinting", "font_rid", "hinting"), &TextServer::font_set_hinting);
281
ClassDB::bind_method(D_METHOD("font_get_hinting", "font_rid"), &TextServer::font_get_hinting);
282
283
ClassDB::bind_method(D_METHOD("font_set_subpixel_positioning", "font_rid", "subpixel_positioning"), &TextServer::font_set_subpixel_positioning);
284
ClassDB::bind_method(D_METHOD("font_get_subpixel_positioning", "font_rid"), &TextServer::font_get_subpixel_positioning);
285
286
ClassDB::bind_method(D_METHOD("font_set_keep_rounding_remainders", "font_rid", "keep_rounding_remainders"), &TextServer::font_set_keep_rounding_remainders);
287
ClassDB::bind_method(D_METHOD("font_get_keep_rounding_remainders", "font_rid"), &TextServer::font_get_keep_rounding_remainders);
288
289
ClassDB::bind_method(D_METHOD("font_set_embolden", "font_rid", "strength"), &TextServer::font_set_embolden);
290
ClassDB::bind_method(D_METHOD("font_get_embolden", "font_rid"), &TextServer::font_get_embolden);
291
292
ClassDB::bind_method(D_METHOD("font_set_spacing", "font_rid", "spacing", "value"), &TextServer::font_set_spacing);
293
ClassDB::bind_method(D_METHOD("font_get_spacing", "font_rid", "spacing"), &TextServer::font_get_spacing);
294
295
ClassDB::bind_method(D_METHOD("font_set_baseline_offset", "font_rid", "baseline_offset"), &TextServer::font_set_baseline_offset);
296
ClassDB::bind_method(D_METHOD("font_get_baseline_offset", "font_rid"), &TextServer::font_get_baseline_offset);
297
298
ClassDB::bind_method(D_METHOD("font_set_transform", "font_rid", "transform"), &TextServer::font_set_transform);
299
ClassDB::bind_method(D_METHOD("font_get_transform", "font_rid"), &TextServer::font_get_transform);
300
301
ClassDB::bind_method(D_METHOD("font_set_variation_coordinates", "font_rid", "variation_coordinates"), &TextServer::font_set_variation_coordinates);
302
ClassDB::bind_method(D_METHOD("font_get_variation_coordinates", "font_rid"), &TextServer::font_get_variation_coordinates);
303
304
ClassDB::bind_method(D_METHOD("font_set_oversampling", "font_rid", "oversampling"), &TextServer::font_set_oversampling);
305
ClassDB::bind_method(D_METHOD("font_get_oversampling", "font_rid"), &TextServer::font_get_oversampling);
306
307
ClassDB::bind_method(D_METHOD("font_get_size_cache_list", "font_rid"), &TextServer::font_get_size_cache_list);
308
ClassDB::bind_method(D_METHOD("font_clear_size_cache", "font_rid"), &TextServer::font_clear_size_cache);
309
ClassDB::bind_method(D_METHOD("font_remove_size_cache", "font_rid", "size"), &TextServer::font_remove_size_cache);
310
ClassDB::bind_method(D_METHOD("font_get_size_cache_info", "font_rid"), &TextServer::font_get_size_cache_info);
311
312
ClassDB::bind_method(D_METHOD("font_set_ascent", "font_rid", "size", "ascent"), &TextServer::font_set_ascent);
313
ClassDB::bind_method(D_METHOD("font_get_ascent", "font_rid", "size"), &TextServer::font_get_ascent);
314
315
ClassDB::bind_method(D_METHOD("font_set_descent", "font_rid", "size", "descent"), &TextServer::font_set_descent);
316
ClassDB::bind_method(D_METHOD("font_get_descent", "font_rid", "size"), &TextServer::font_get_descent);
317
318
ClassDB::bind_method(D_METHOD("font_set_underline_position", "font_rid", "size", "underline_position"), &TextServer::font_set_underline_position);
319
ClassDB::bind_method(D_METHOD("font_get_underline_position", "font_rid", "size"), &TextServer::font_get_underline_position);
320
321
ClassDB::bind_method(D_METHOD("font_set_underline_thickness", "font_rid", "size", "underline_thickness"), &TextServer::font_set_underline_thickness);
322
ClassDB::bind_method(D_METHOD("font_get_underline_thickness", "font_rid", "size"), &TextServer::font_get_underline_thickness);
323
324
ClassDB::bind_method(D_METHOD("font_set_scale", "font_rid", "size", "scale"), &TextServer::font_set_scale);
325
ClassDB::bind_method(D_METHOD("font_get_scale", "font_rid", "size"), &TextServer::font_get_scale);
326
327
ClassDB::bind_method(D_METHOD("font_get_texture_count", "font_rid", "size"), &TextServer::font_get_texture_count);
328
ClassDB::bind_method(D_METHOD("font_clear_textures", "font_rid", "size"), &TextServer::font_clear_textures);
329
ClassDB::bind_method(D_METHOD("font_remove_texture", "font_rid", "size", "texture_index"), &TextServer::font_remove_texture);
330
331
ClassDB::bind_method(D_METHOD("font_set_texture_image", "font_rid", "size", "texture_index", "image"), &TextServer::font_set_texture_image);
332
ClassDB::bind_method(D_METHOD("font_get_texture_image", "font_rid", "size", "texture_index"), &TextServer::font_get_texture_image);
333
334
ClassDB::bind_method(D_METHOD("font_set_texture_offsets", "font_rid", "size", "texture_index", "offset"), &TextServer::font_set_texture_offsets);
335
ClassDB::bind_method(D_METHOD("font_get_texture_offsets", "font_rid", "size", "texture_index"), &TextServer::font_get_texture_offsets);
336
337
ClassDB::bind_method(D_METHOD("font_get_glyph_list", "font_rid", "size"), &TextServer::font_get_glyph_list);
338
ClassDB::bind_method(D_METHOD("font_clear_glyphs", "font_rid", "size"), &TextServer::font_clear_glyphs);
339
ClassDB::bind_method(D_METHOD("font_remove_glyph", "font_rid", "size", "glyph"), &TextServer::font_remove_glyph);
340
341
ClassDB::bind_method(D_METHOD("font_get_glyph_advance", "font_rid", "size", "glyph"), &TextServer::font_get_glyph_advance);
342
ClassDB::bind_method(D_METHOD("font_set_glyph_advance", "font_rid", "size", "glyph", "advance"), &TextServer::font_set_glyph_advance);
343
344
ClassDB::bind_method(D_METHOD("font_get_glyph_offset", "font_rid", "size", "glyph"), &TextServer::font_get_glyph_offset);
345
ClassDB::bind_method(D_METHOD("font_set_glyph_offset", "font_rid", "size", "glyph", "offset"), &TextServer::font_set_glyph_offset);
346
347
ClassDB::bind_method(D_METHOD("font_get_glyph_size", "font_rid", "size", "glyph"), &TextServer::font_get_glyph_size);
348
ClassDB::bind_method(D_METHOD("font_set_glyph_size", "font_rid", "size", "glyph", "gl_size"), &TextServer::font_set_glyph_size);
349
350
ClassDB::bind_method(D_METHOD("font_get_glyph_uv_rect", "font_rid", "size", "glyph"), &TextServer::font_get_glyph_uv_rect);
351
ClassDB::bind_method(D_METHOD("font_set_glyph_uv_rect", "font_rid", "size", "glyph", "uv_rect"), &TextServer::font_set_glyph_uv_rect);
352
353
ClassDB::bind_method(D_METHOD("font_get_glyph_texture_idx", "font_rid", "size", "glyph"), &TextServer::font_get_glyph_texture_idx);
354
ClassDB::bind_method(D_METHOD("font_set_glyph_texture_idx", "font_rid", "size", "glyph", "texture_idx"), &TextServer::font_set_glyph_texture_idx);
355
356
ClassDB::bind_method(D_METHOD("font_get_glyph_texture_rid", "font_rid", "size", "glyph"), &TextServer::font_get_glyph_texture_rid);
357
ClassDB::bind_method(D_METHOD("font_get_glyph_texture_size", "font_rid", "size", "glyph"), &TextServer::font_get_glyph_texture_size);
358
359
ClassDB::bind_method(D_METHOD("font_get_glyph_contours", "font", "size", "index"), &TextServer::font_get_glyph_contours);
360
361
ClassDB::bind_method(D_METHOD("font_get_kerning_list", "font_rid", "size"), &TextServer::font_get_kerning_list);
362
ClassDB::bind_method(D_METHOD("font_clear_kerning_map", "font_rid", "size"), &TextServer::font_clear_kerning_map);
363
ClassDB::bind_method(D_METHOD("font_remove_kerning", "font_rid", "size", "glyph_pair"), &TextServer::font_remove_kerning);
364
365
ClassDB::bind_method(D_METHOD("font_set_kerning", "font_rid", "size", "glyph_pair", "kerning"), &TextServer::font_set_kerning);
366
ClassDB::bind_method(D_METHOD("font_get_kerning", "font_rid", "size", "glyph_pair"), &TextServer::font_get_kerning);
367
368
ClassDB::bind_method(D_METHOD("font_get_glyph_index", "font_rid", "size", "char", "variation_selector"), &TextServer::font_get_glyph_index);
369
ClassDB::bind_method(D_METHOD("font_get_char_from_glyph_index", "font_rid", "size", "glyph_index"), &TextServer::font_get_char_from_glyph_index);
370
371
ClassDB::bind_method(D_METHOD("font_has_char", "font_rid", "char"), &TextServer::font_has_char);
372
ClassDB::bind_method(D_METHOD("font_get_supported_chars", "font_rid"), &TextServer::font_get_supported_chars);
373
ClassDB::bind_method(D_METHOD("font_get_supported_glyphs", "font_rid"), &TextServer::font_get_supported_glyphs);
374
375
ClassDB::bind_method(D_METHOD("font_render_range", "font_rid", "size", "start", "end"), &TextServer::font_render_range);
376
ClassDB::bind_method(D_METHOD("font_render_glyph", "font_rid", "size", "index"), &TextServer::font_render_glyph);
377
378
ClassDB::bind_method(D_METHOD("font_draw_glyph", "font_rid", "canvas", "size", "pos", "index", "color", "oversampling"), &TextServer::font_draw_glyph, DEFVAL(Color(1, 1, 1)), DEFVAL(0.0));
379
ClassDB::bind_method(D_METHOD("font_draw_glyph_outline", "font_rid", "canvas", "size", "outline_size", "pos", "index", "color", "oversampling"), &TextServer::font_draw_glyph_outline, DEFVAL(Color(1, 1, 1)), DEFVAL(0.0));
380
381
ClassDB::bind_method(D_METHOD("font_is_language_supported", "font_rid", "language"), &TextServer::font_is_language_supported);
382
ClassDB::bind_method(D_METHOD("font_set_language_support_override", "font_rid", "language", "supported"), &TextServer::font_set_language_support_override);
383
ClassDB::bind_method(D_METHOD("font_get_language_support_override", "font_rid", "language"), &TextServer::font_get_language_support_override);
384
ClassDB::bind_method(D_METHOD("font_remove_language_support_override", "font_rid", "language"), &TextServer::font_remove_language_support_override);
385
ClassDB::bind_method(D_METHOD("font_get_language_support_overrides", "font_rid"), &TextServer::font_get_language_support_overrides);
386
387
ClassDB::bind_method(D_METHOD("font_is_script_supported", "font_rid", "script"), &TextServer::font_is_script_supported);
388
ClassDB::bind_method(D_METHOD("font_set_script_support_override", "font_rid", "script", "supported"), &TextServer::font_set_script_support_override);
389
ClassDB::bind_method(D_METHOD("font_get_script_support_override", "font_rid", "script"), &TextServer::font_get_script_support_override);
390
ClassDB::bind_method(D_METHOD("font_remove_script_support_override", "font_rid", "script"), &TextServer::font_remove_script_support_override);
391
ClassDB::bind_method(D_METHOD("font_get_script_support_overrides", "font_rid"), &TextServer::font_get_script_support_overrides);
392
393
ClassDB::bind_method(D_METHOD("font_set_opentype_feature_overrides", "font_rid", "overrides"), &TextServer::font_set_opentype_feature_overrides);
394
ClassDB::bind_method(D_METHOD("font_get_opentype_feature_overrides", "font_rid"), &TextServer::font_get_opentype_feature_overrides);
395
396
ClassDB::bind_method(D_METHOD("font_supported_feature_list", "font_rid"), &TextServer::font_supported_feature_list);
397
ClassDB::bind_method(D_METHOD("font_supported_variation_list", "font_rid"), &TextServer::font_supported_variation_list);
398
399
#ifndef DISABLE_DEPRECATED
400
ClassDB::bind_method(D_METHOD("font_get_global_oversampling"), &TextServer::font_get_global_oversampling);
401
ClassDB::bind_method(D_METHOD("font_set_global_oversampling", "oversampling"), &TextServer::font_set_global_oversampling);
402
#endif
403
404
ClassDB::bind_method(D_METHOD("get_hex_code_box_size", "size", "index"), &TextServer::get_hex_code_box_size);
405
ClassDB::bind_method(D_METHOD("draw_hex_code_box", "canvas", "size", "pos", "index", "color"), &TextServer::draw_hex_code_box);
406
407
/* Shaped text buffer interface */
408
409
ClassDB::bind_method(D_METHOD("create_shaped_text", "direction", "orientation"), &TextServer::create_shaped_text, DEFVAL(DIRECTION_AUTO), DEFVAL(ORIENTATION_HORIZONTAL));
410
411
ClassDB::bind_method(D_METHOD("shaped_text_clear", "rid"), &TextServer::shaped_text_clear);
412
ClassDB::bind_method(D_METHOD("shaped_text_duplicate", "rid"), &TextServer::shaped_text_duplicate);
413
414
ClassDB::bind_method(D_METHOD("shaped_text_set_direction", "shaped", "direction"), &TextServer::shaped_text_set_direction, DEFVAL(DIRECTION_AUTO));
415
ClassDB::bind_method(D_METHOD("shaped_text_get_direction", "shaped"), &TextServer::shaped_text_get_direction);
416
ClassDB::bind_method(D_METHOD("shaped_text_get_inferred_direction", "shaped"), &TextServer::shaped_text_get_inferred_direction);
417
418
ClassDB::bind_method(D_METHOD("shaped_text_set_bidi_override", "shaped", "override"), &TextServer::shaped_text_set_bidi_override);
419
420
ClassDB::bind_method(D_METHOD("shaped_text_set_custom_punctuation", "shaped", "punct"), &TextServer::shaped_text_set_custom_punctuation);
421
ClassDB::bind_method(D_METHOD("shaped_text_get_custom_punctuation", "shaped"), &TextServer::shaped_text_get_custom_punctuation);
422
423
ClassDB::bind_method(D_METHOD("shaped_text_set_custom_ellipsis", "shaped", "char"), &TextServer::shaped_text_set_custom_ellipsis);
424
ClassDB::bind_method(D_METHOD("shaped_text_get_custom_ellipsis", "shaped"), &TextServer::shaped_text_get_custom_ellipsis);
425
426
ClassDB::bind_method(D_METHOD("shaped_text_set_orientation", "shaped", "orientation"), &TextServer::shaped_text_set_orientation, DEFVAL(ORIENTATION_HORIZONTAL));
427
ClassDB::bind_method(D_METHOD("shaped_text_get_orientation", "shaped"), &TextServer::shaped_text_get_orientation);
428
429
ClassDB::bind_method(D_METHOD("shaped_text_set_preserve_invalid", "shaped", "enabled"), &TextServer::shaped_text_set_preserve_invalid);
430
ClassDB::bind_method(D_METHOD("shaped_text_get_preserve_invalid", "shaped"), &TextServer::shaped_text_get_preserve_invalid);
431
432
ClassDB::bind_method(D_METHOD("shaped_text_set_preserve_control", "shaped", "enabled"), &TextServer::shaped_text_set_preserve_control);
433
ClassDB::bind_method(D_METHOD("shaped_text_get_preserve_control", "shaped"), &TextServer::shaped_text_get_preserve_control);
434
435
ClassDB::bind_method(D_METHOD("shaped_text_set_spacing", "shaped", "spacing", "value"), &TextServer::shaped_text_set_spacing);
436
ClassDB::bind_method(D_METHOD("shaped_text_get_spacing", "shaped", "spacing"), &TextServer::shaped_text_get_spacing);
437
438
ClassDB::bind_method(D_METHOD("shaped_text_add_string", "shaped", "text", "fonts", "size", "opentype_features", "language", "meta"), &TextServer::shaped_text_add_string, DEFVAL(Dictionary()), DEFVAL(""), DEFVAL(Variant()));
439
ClassDB::bind_method(D_METHOD("shaped_text_add_object", "shaped", "key", "size", "inline_align", "length", "baseline"), &TextServer::shaped_text_add_object, DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(1), DEFVAL(0.0));
440
ClassDB::bind_method(D_METHOD("shaped_text_resize_object", "shaped", "key", "size", "inline_align", "baseline"), &TextServer::shaped_text_resize_object, DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(0.0));
441
ClassDB::bind_method(D_METHOD("shaped_text_has_object", "shaped", "key"), &TextServer::shaped_text_has_object);
442
ClassDB::bind_method(D_METHOD("shaped_get_text", "shaped"), &TextServer::shaped_get_text);
443
444
ClassDB::bind_method(D_METHOD("shaped_get_span_count", "shaped"), &TextServer::shaped_get_span_count);
445
ClassDB::bind_method(D_METHOD("shaped_get_span_meta", "shaped", "index"), &TextServer::shaped_get_span_meta);
446
ClassDB::bind_method(D_METHOD("shaped_get_span_embedded_object", "shaped", "index"), &TextServer::shaped_get_span_embedded_object);
447
ClassDB::bind_method(D_METHOD("shaped_get_span_text", "shaped", "index"), &TextServer::shaped_get_span_text);
448
ClassDB::bind_method(D_METHOD("shaped_get_span_object", "shaped", "index"), &TextServer::shaped_get_span_object);
449
ClassDB::bind_method(D_METHOD("shaped_set_span_update_font", "shaped", "index", "fonts", "size", "opentype_features"), &TextServer::shaped_set_span_update_font, DEFVAL(Dictionary()));
450
451
ClassDB::bind_method(D_METHOD("shaped_get_run_count", "shaped"), &TextServer::shaped_get_run_count);
452
ClassDB::bind_method(D_METHOD("shaped_get_run_text", "shaped", "index"), &TextServer::shaped_get_run_text);
453
ClassDB::bind_method(D_METHOD("shaped_get_run_range", "shaped", "index"), &TextServer::shaped_get_run_range);
454
ClassDB::bind_method(D_METHOD("shaped_get_run_font_rid", "shaped", "index"), &TextServer::shaped_get_run_font_rid);
455
ClassDB::bind_method(D_METHOD("shaped_get_run_font_size", "shaped", "index"), &TextServer::shaped_get_run_font_size);
456
ClassDB::bind_method(D_METHOD("shaped_get_run_language", "shaped", "index"), &TextServer::shaped_get_run_language);
457
ClassDB::bind_method(D_METHOD("shaped_get_run_direction", "shaped", "index"), &TextServer::shaped_get_run_direction);
458
ClassDB::bind_method(D_METHOD("shaped_get_run_object", "shaped", "index"), &TextServer::shaped_get_run_object);
459
460
ClassDB::bind_method(D_METHOD("shaped_text_substr", "shaped", "start", "length"), &TextServer::shaped_text_substr);
461
ClassDB::bind_method(D_METHOD("shaped_text_get_parent", "shaped"), &TextServer::shaped_text_get_parent);
462
ClassDB::bind_method(D_METHOD("shaped_text_fit_to_width", "shaped", "width", "justification_flags"), &TextServer::shaped_text_fit_to_width, DEFVAL(JUSTIFICATION_WORD_BOUND | JUSTIFICATION_KASHIDA));
463
ClassDB::bind_method(D_METHOD("shaped_text_tab_align", "shaped", "tab_stops"), &TextServer::shaped_text_tab_align);
464
465
ClassDB::bind_method(D_METHOD("shaped_text_shape", "shaped"), &TextServer::shaped_text_shape);
466
ClassDB::bind_method(D_METHOD("shaped_text_is_ready", "shaped"), &TextServer::shaped_text_is_ready);
467
ClassDB::bind_method(D_METHOD("shaped_text_has_visible_chars", "shaped"), &TextServer::shaped_text_has_visible_chars);
468
469
ClassDB::bind_method(D_METHOD("shaped_text_get_glyphs", "shaped"), &TextServer::_shaped_text_get_glyphs_wrapper);
470
ClassDB::bind_method(D_METHOD("shaped_text_sort_logical", "shaped"), &TextServer::_shaped_text_sort_logical_wrapper);
471
ClassDB::bind_method(D_METHOD("shaped_text_get_glyph_count", "shaped"), &TextServer::shaped_text_get_glyph_count);
472
473
ClassDB::bind_method(D_METHOD("shaped_text_get_range", "shaped"), &TextServer::shaped_text_get_range);
474
ClassDB::bind_method(D_METHOD("shaped_text_get_line_breaks_adv", "shaped", "width", "start", "once", "break_flags"), &TextServer::shaped_text_get_line_breaks_adv, DEFVAL(0), DEFVAL(true), DEFVAL(BREAK_MANDATORY | BREAK_WORD_BOUND));
475
ClassDB::bind_method(D_METHOD("shaped_text_get_line_breaks", "shaped", "width", "start", "break_flags"), &TextServer::shaped_text_get_line_breaks, DEFVAL(0), DEFVAL(BREAK_MANDATORY | BREAK_WORD_BOUND));
476
ClassDB::bind_method(D_METHOD("shaped_text_get_word_breaks", "shaped", "grapheme_flags", "skip_grapheme_flags"), &TextServer::shaped_text_get_word_breaks, DEFVAL(GRAPHEME_IS_SPACE | GRAPHEME_IS_PUNCTUATION), DEFVAL(GRAPHEME_IS_VIRTUAL));
477
478
ClassDB::bind_method(D_METHOD("shaped_text_get_trim_pos", "shaped"), &TextServer::shaped_text_get_trim_pos);
479
ClassDB::bind_method(D_METHOD("shaped_text_get_ellipsis_pos", "shaped"), &TextServer::shaped_text_get_ellipsis_pos);
480
ClassDB::bind_method(D_METHOD("shaped_text_get_ellipsis_glyphs", "shaped"), &TextServer::_shaped_text_get_ellipsis_glyphs_wrapper);
481
ClassDB::bind_method(D_METHOD("shaped_text_get_ellipsis_glyph_count", "shaped"), &TextServer::shaped_text_get_ellipsis_glyph_count);
482
483
ClassDB::bind_method(D_METHOD("shaped_text_overrun_trim_to_width", "shaped", "width", "overrun_trim_flags"), &TextServer::shaped_text_overrun_trim_to_width, DEFVAL(0), DEFVAL(OVERRUN_NO_TRIM));
484
485
ClassDB::bind_method(D_METHOD("shaped_text_get_objects", "shaped"), &TextServer::shaped_text_get_objects);
486
ClassDB::bind_method(D_METHOD("shaped_text_get_object_rect", "shaped", "key"), &TextServer::shaped_text_get_object_rect);
487
ClassDB::bind_method(D_METHOD("shaped_text_get_object_range", "shaped", "key"), &TextServer::shaped_text_get_object_range);
488
ClassDB::bind_method(D_METHOD("shaped_text_get_object_glyph", "shaped", "key"), &TextServer::shaped_text_get_object_glyph);
489
490
ClassDB::bind_method(D_METHOD("shaped_text_get_size", "shaped"), &TextServer::shaped_text_get_size);
491
ClassDB::bind_method(D_METHOD("shaped_text_get_ascent", "shaped"), &TextServer::shaped_text_get_ascent);
492
ClassDB::bind_method(D_METHOD("shaped_text_get_descent", "shaped"), &TextServer::shaped_text_get_descent);
493
ClassDB::bind_method(D_METHOD("shaped_text_get_width", "shaped"), &TextServer::shaped_text_get_width);
494
ClassDB::bind_method(D_METHOD("shaped_text_get_underline_position", "shaped"), &TextServer::shaped_text_get_underline_position);
495
ClassDB::bind_method(D_METHOD("shaped_text_get_underline_thickness", "shaped"), &TextServer::shaped_text_get_underline_thickness);
496
497
ClassDB::bind_method(D_METHOD("shaped_text_get_carets", "shaped", "position"), &TextServer::_shaped_text_get_carets_wrapper);
498
ClassDB::bind_method(D_METHOD("shaped_text_get_selection", "shaped", "start", "end"), &TextServer::shaped_text_get_selection);
499
500
ClassDB::bind_method(D_METHOD("shaped_text_hit_test_grapheme", "shaped", "coords"), &TextServer::shaped_text_hit_test_grapheme);
501
ClassDB::bind_method(D_METHOD("shaped_text_hit_test_position", "shaped", "coords"), &TextServer::shaped_text_hit_test_position);
502
503
ClassDB::bind_method(D_METHOD("shaped_text_get_grapheme_bounds", "shaped", "pos"), &TextServer::shaped_text_get_grapheme_bounds);
504
ClassDB::bind_method(D_METHOD("shaped_text_next_grapheme_pos", "shaped", "pos"), &TextServer::shaped_text_next_grapheme_pos);
505
ClassDB::bind_method(D_METHOD("shaped_text_prev_grapheme_pos", "shaped", "pos"), &TextServer::shaped_text_prev_grapheme_pos);
506
507
ClassDB::bind_method(D_METHOD("shaped_text_get_character_breaks", "shaped"), &TextServer::shaped_text_get_character_breaks);
508
ClassDB::bind_method(D_METHOD("shaped_text_next_character_pos", "shaped", "pos"), &TextServer::shaped_text_next_character_pos);
509
ClassDB::bind_method(D_METHOD("shaped_text_prev_character_pos", "shaped", "pos"), &TextServer::shaped_text_prev_character_pos);
510
ClassDB::bind_method(D_METHOD("shaped_text_closest_character_pos", "shaped", "pos"), &TextServer::shaped_text_closest_character_pos);
511
512
ClassDB::bind_method(D_METHOD("shaped_text_draw", "shaped", "canvas", "pos", "clip_l", "clip_r", "color", "oversampling"), &TextServer::shaped_text_draw, DEFVAL(-1), DEFVAL(-1), DEFVAL(Color(1, 1, 1)), DEFVAL(0.0));
513
ClassDB::bind_method(D_METHOD("shaped_text_draw_outline", "shaped", "canvas", "pos", "clip_l", "clip_r", "outline_size", "color", "oversampling"), &TextServer::shaped_text_draw_outline, DEFVAL(-1), DEFVAL(-1), DEFVAL(1), DEFVAL(Color(1, 1, 1)), DEFVAL(0.0));
514
515
ClassDB::bind_method(D_METHOD("shaped_text_get_dominant_direction_in_range", "shaped", "start", "end"), &TextServer::shaped_text_get_dominant_direction_in_range);
516
517
#ifndef DISABLE_DEPRECATED
518
ClassDB::bind_method(D_METHOD("format_number", "number", "language"), &TextServer::format_number, DEFVAL(""));
519
ClassDB::bind_method(D_METHOD("parse_number", "number", "language"), &TextServer::parse_number, DEFVAL(""));
520
ClassDB::bind_method(D_METHOD("percent_sign", "language"), &TextServer::percent_sign, DEFVAL(""));
521
#endif // DISABLE_DEPRECATED
522
523
ClassDB::bind_method(D_METHOD("string_get_word_breaks", "string", "language", "chars_per_line"), &TextServer::string_get_word_breaks, DEFVAL(""), DEFVAL(0));
524
ClassDB::bind_method(D_METHOD("string_get_character_breaks", "string", "language"), &TextServer::string_get_character_breaks, DEFVAL(""));
525
526
ClassDB::bind_method(D_METHOD("is_confusable", "string", "dict"), &TextServer::is_confusable);
527
ClassDB::bind_method(D_METHOD("spoof_check", "string"), &TextServer::spoof_check);
528
529
ClassDB::bind_method(D_METHOD("strip_diacritics", "string"), &TextServer::strip_diacritics);
530
ClassDB::bind_method(D_METHOD("is_valid_identifier", "string"), &TextServer::is_valid_identifier);
531
ClassDB::bind_method(D_METHOD("is_valid_letter", "unicode"), &TextServer::is_valid_letter);
532
533
ClassDB::bind_method(D_METHOD("string_to_upper", "string", "language"), &TextServer::string_to_upper, DEFVAL(""));
534
ClassDB::bind_method(D_METHOD("string_to_lower", "string", "language"), &TextServer::string_to_lower, DEFVAL(""));
535
ClassDB::bind_method(D_METHOD("string_to_title", "string", "language"), &TextServer::string_to_title, DEFVAL(""));
536
537
ClassDB::bind_method(D_METHOD("parse_structured_text", "parser_type", "args", "text"), &TextServer::parse_structured_text);
538
539
/* Font AA */
540
BIND_ENUM_CONSTANT(FONT_ANTIALIASING_NONE);
541
BIND_ENUM_CONSTANT(FONT_ANTIALIASING_GRAY);
542
BIND_ENUM_CONSTANT(FONT_ANTIALIASING_LCD);
543
544
BIND_ENUM_CONSTANT(FONT_LCD_SUBPIXEL_LAYOUT_NONE);
545
BIND_ENUM_CONSTANT(FONT_LCD_SUBPIXEL_LAYOUT_HRGB);
546
BIND_ENUM_CONSTANT(FONT_LCD_SUBPIXEL_LAYOUT_HBGR);
547
BIND_ENUM_CONSTANT(FONT_LCD_SUBPIXEL_LAYOUT_VRGB);
548
BIND_ENUM_CONSTANT(FONT_LCD_SUBPIXEL_LAYOUT_VBGR);
549
BIND_ENUM_CONSTANT(FONT_LCD_SUBPIXEL_LAYOUT_MAX);
550
551
/* Direction */
552
BIND_ENUM_CONSTANT(DIRECTION_AUTO);
553
BIND_ENUM_CONSTANT(DIRECTION_LTR);
554
BIND_ENUM_CONSTANT(DIRECTION_RTL);
555
BIND_ENUM_CONSTANT(DIRECTION_INHERITED);
556
557
/* Orientation */
558
BIND_ENUM_CONSTANT(ORIENTATION_HORIZONTAL);
559
BIND_ENUM_CONSTANT(ORIENTATION_VERTICAL);
560
561
/* JustificationFlag */
562
BIND_BITFIELD_FLAG(JUSTIFICATION_NONE);
563
BIND_BITFIELD_FLAG(JUSTIFICATION_KASHIDA);
564
BIND_BITFIELD_FLAG(JUSTIFICATION_WORD_BOUND);
565
BIND_BITFIELD_FLAG(JUSTIFICATION_TRIM_EDGE_SPACES);
566
BIND_BITFIELD_FLAG(JUSTIFICATION_AFTER_LAST_TAB);
567
BIND_BITFIELD_FLAG(JUSTIFICATION_CONSTRAIN_ELLIPSIS);
568
BIND_BITFIELD_FLAG(JUSTIFICATION_SKIP_LAST_LINE);
569
BIND_BITFIELD_FLAG(JUSTIFICATION_SKIP_LAST_LINE_WITH_VISIBLE_CHARS);
570
BIND_BITFIELD_FLAG(JUSTIFICATION_DO_NOT_SKIP_SINGLE_LINE);
571
572
/* AutowrapMode */
573
BIND_ENUM_CONSTANT(AUTOWRAP_OFF);
574
BIND_ENUM_CONSTANT(AUTOWRAP_ARBITRARY);
575
BIND_ENUM_CONSTANT(AUTOWRAP_WORD);
576
BIND_ENUM_CONSTANT(AUTOWRAP_WORD_SMART);
577
578
/* LineBreakFlag */
579
BIND_BITFIELD_FLAG(BREAK_NONE);
580
BIND_BITFIELD_FLAG(BREAK_MANDATORY);
581
BIND_BITFIELD_FLAG(BREAK_WORD_BOUND);
582
BIND_BITFIELD_FLAG(BREAK_GRAPHEME_BOUND);
583
BIND_BITFIELD_FLAG(BREAK_ADAPTIVE);
584
#ifndef DISABLE_DEPRECATED
585
BIND_BITFIELD_FLAG(BREAK_TRIM_EDGE_SPACES);
586
#endif
587
BIND_BITFIELD_FLAG(BREAK_TRIM_INDENT);
588
BIND_BITFIELD_FLAG(BREAK_TRIM_START_EDGE_SPACES);
589
BIND_BITFIELD_FLAG(BREAK_TRIM_END_EDGE_SPACES);
590
591
/* VisibleCharactersBehavior */
592
BIND_ENUM_CONSTANT(VC_CHARS_BEFORE_SHAPING);
593
BIND_ENUM_CONSTANT(VC_CHARS_AFTER_SHAPING);
594
BIND_ENUM_CONSTANT(VC_GLYPHS_AUTO);
595
BIND_ENUM_CONSTANT(VC_GLYPHS_LTR);
596
BIND_ENUM_CONSTANT(VC_GLYPHS_RTL);
597
598
/* OverrunBehavior */
599
BIND_ENUM_CONSTANT(OVERRUN_NO_TRIMMING);
600
BIND_ENUM_CONSTANT(OVERRUN_TRIM_CHAR);
601
BIND_ENUM_CONSTANT(OVERRUN_TRIM_WORD);
602
BIND_ENUM_CONSTANT(OVERRUN_TRIM_ELLIPSIS);
603
BIND_ENUM_CONSTANT(OVERRUN_TRIM_WORD_ELLIPSIS);
604
BIND_ENUM_CONSTANT(OVERRUN_TRIM_ELLIPSIS_FORCE);
605
BIND_ENUM_CONSTANT(OVERRUN_TRIM_WORD_ELLIPSIS_FORCE);
606
607
/* TextOverrunFlag */
608
BIND_BITFIELD_FLAG(OVERRUN_NO_TRIM);
609
BIND_BITFIELD_FLAG(OVERRUN_TRIM);
610
BIND_BITFIELD_FLAG(OVERRUN_TRIM_WORD_ONLY);
611
BIND_BITFIELD_FLAG(OVERRUN_ADD_ELLIPSIS);
612
BIND_BITFIELD_FLAG(OVERRUN_ENFORCE_ELLIPSIS);
613
BIND_BITFIELD_FLAG(OVERRUN_JUSTIFICATION_AWARE);
614
BIND_BITFIELD_FLAG(OVERRUN_SHORT_STRING_ELLIPSIS);
615
616
/* GraphemeFlag */
617
BIND_BITFIELD_FLAG(GRAPHEME_IS_VALID);
618
BIND_BITFIELD_FLAG(GRAPHEME_IS_RTL);
619
BIND_BITFIELD_FLAG(GRAPHEME_IS_VIRTUAL);
620
BIND_BITFIELD_FLAG(GRAPHEME_IS_SPACE);
621
BIND_BITFIELD_FLAG(GRAPHEME_IS_BREAK_HARD);
622
BIND_BITFIELD_FLAG(GRAPHEME_IS_BREAK_SOFT);
623
BIND_BITFIELD_FLAG(GRAPHEME_IS_TAB);
624
BIND_BITFIELD_FLAG(GRAPHEME_IS_ELONGATION);
625
BIND_BITFIELD_FLAG(GRAPHEME_IS_PUNCTUATION);
626
BIND_BITFIELD_FLAG(GRAPHEME_IS_UNDERSCORE);
627
BIND_BITFIELD_FLAG(GRAPHEME_IS_CONNECTED);
628
BIND_BITFIELD_FLAG(GRAPHEME_IS_SAFE_TO_INSERT_TATWEEL);
629
BIND_BITFIELD_FLAG(GRAPHEME_IS_EMBEDDED_OBJECT);
630
BIND_BITFIELD_FLAG(GRAPHEME_IS_SOFT_HYPHEN);
631
632
/* Hinting */
633
BIND_ENUM_CONSTANT(HINTING_NONE);
634
BIND_ENUM_CONSTANT(HINTING_LIGHT);
635
BIND_ENUM_CONSTANT(HINTING_NORMAL);
636
637
/* SubpixelPositioning */
638
BIND_ENUM_CONSTANT(SUBPIXEL_POSITIONING_DISABLED);
639
BIND_ENUM_CONSTANT(SUBPIXEL_POSITIONING_AUTO);
640
BIND_ENUM_CONSTANT(SUBPIXEL_POSITIONING_ONE_HALF);
641
BIND_ENUM_CONSTANT(SUBPIXEL_POSITIONING_ONE_QUARTER);
642
BIND_ENUM_CONSTANT(SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE);
643
BIND_ENUM_CONSTANT(SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE);
644
645
/* Feature */
646
BIND_ENUM_CONSTANT(FEATURE_SIMPLE_LAYOUT);
647
BIND_ENUM_CONSTANT(FEATURE_BIDI_LAYOUT);
648
BIND_ENUM_CONSTANT(FEATURE_VERTICAL_LAYOUT);
649
BIND_ENUM_CONSTANT(FEATURE_SHAPING);
650
BIND_ENUM_CONSTANT(FEATURE_KASHIDA_JUSTIFICATION);
651
BIND_ENUM_CONSTANT(FEATURE_BREAK_ITERATORS);
652
BIND_ENUM_CONSTANT(FEATURE_FONT_BITMAP);
653
BIND_ENUM_CONSTANT(FEATURE_FONT_DYNAMIC);
654
BIND_ENUM_CONSTANT(FEATURE_FONT_MSDF);
655
BIND_ENUM_CONSTANT(FEATURE_FONT_SYSTEM);
656
BIND_ENUM_CONSTANT(FEATURE_FONT_VARIABLE);
657
BIND_ENUM_CONSTANT(FEATURE_CONTEXT_SENSITIVE_CASE_CONVERSION);
658
BIND_ENUM_CONSTANT(FEATURE_USE_SUPPORT_DATA);
659
BIND_ENUM_CONSTANT(FEATURE_UNICODE_IDENTIFIERS);
660
BIND_ENUM_CONSTANT(FEATURE_UNICODE_SECURITY);
661
662
/* FT Contour Point Types */
663
BIND_ENUM_CONSTANT(CONTOUR_CURVE_TAG_ON);
664
BIND_ENUM_CONSTANT(CONTOUR_CURVE_TAG_OFF_CONIC);
665
BIND_ENUM_CONSTANT(CONTOUR_CURVE_TAG_OFF_CUBIC);
666
667
/* Font Spacing */
668
BIND_ENUM_CONSTANT(SPACING_GLYPH);
669
BIND_ENUM_CONSTANT(SPACING_SPACE);
670
BIND_ENUM_CONSTANT(SPACING_TOP);
671
BIND_ENUM_CONSTANT(SPACING_BOTTOM);
672
BIND_ENUM_CONSTANT(SPACING_MAX);
673
674
/* Font Style */
675
BIND_BITFIELD_FLAG(FONT_BOLD);
676
BIND_BITFIELD_FLAG(FONT_ITALIC);
677
BIND_BITFIELD_FLAG(FONT_FIXED_WIDTH);
678
679
/* Structured text parser */
680
BIND_ENUM_CONSTANT(STRUCTURED_TEXT_DEFAULT);
681
BIND_ENUM_CONSTANT(STRUCTURED_TEXT_URI);
682
BIND_ENUM_CONSTANT(STRUCTURED_TEXT_FILE);
683
BIND_ENUM_CONSTANT(STRUCTURED_TEXT_EMAIL);
684
BIND_ENUM_CONSTANT(STRUCTURED_TEXT_LIST);
685
BIND_ENUM_CONSTANT(STRUCTURED_TEXT_GDSCRIPT);
686
BIND_ENUM_CONSTANT(STRUCTURED_TEXT_CUSTOM);
687
688
/* Fixed size scale mode */
689
BIND_ENUM_CONSTANT(FIXED_SIZE_SCALE_DISABLE);
690
BIND_ENUM_CONSTANT(FIXED_SIZE_SCALE_INTEGER_ONLY);
691
BIND_ENUM_CONSTANT(FIXED_SIZE_SCALE_ENABLED);
692
}
693
694
_FORCE_INLINE_ int32_t ot_tag_from_string(const char *p_str, int p_len) {
695
char tag[4];
696
uint32_t i;
697
698
if (!p_str || !p_len || !*p_str) {
699
return OT_TAG(0, 0, 0, 0);
700
}
701
702
if (p_len < 0 || p_len > 4) {
703
p_len = 4;
704
}
705
for (i = 0; i < (uint32_t)p_len && p_str[i]; i++) {
706
tag[i] = p_str[i];
707
}
708
709
for (; i < 4; i++) {
710
tag[i] = ' ';
711
}
712
713
return OT_TAG(tag[0], tag[1], tag[2], tag[3]);
714
}
715
716
int64_t TextServer::name_to_tag(const String &p_name) const {
717
// No readable name, use tag string.
718
return ot_tag_from_string(p_name.replace("custom_", "").ascii().get_data(), -1);
719
}
720
721
_FORCE_INLINE_ void ot_tag_to_string(int32_t p_tag, char *p_buf) {
722
p_buf[0] = (char)(uint8_t)(p_tag >> 24);
723
p_buf[1] = (char)(uint8_t)(p_tag >> 16);
724
p_buf[2] = (char)(uint8_t)(p_tag >> 8);
725
p_buf[3] = (char)(uint8_t)(p_tag >> 0);
726
}
727
728
String TextServer::tag_to_name(int64_t p_tag) const {
729
// No readable name, use tag string.
730
char name[5];
731
memset(name, 0, 5);
732
ot_tag_to_string(p_tag, name);
733
return String("custom_") + String(name);
734
}
735
736
Vector2 TextServer::get_hex_code_box_size(int64_t p_size, int64_t p_index) const {
737
int w = ((p_index <= 0xFF) ? 1 : ((p_index <= 0xFFFF) ? 2 : 3));
738
int sp = MAX(0, w - 1);
739
int sz = MAX(1, Math::round(p_size / 15.f));
740
741
return Vector2(4 + 3 * w + sp + 1, 15) * sz;
742
}
743
744
void TextServer::_draw_hex_code_box_number(const RID &p_canvas, int64_t p_size, const Vector2 &p_pos, uint8_t p_index, const Color &p_color) const {
745
static uint8_t chars[] = { 0x7E, 0x30, 0x6D, 0x79, 0x33, 0x5B, 0x5F, 0x70, 0x7F, 0x7B, 0x77, 0x1F, 0x4E, 0x3D, 0x4F, 0x47, 0x00 };
746
uint8_t x = chars[p_index];
747
if (x & (1 << 6)) {
748
RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(p_pos, Size2(3, 1) * p_size), p_color);
749
}
750
if (x & (1 << 5)) {
751
RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(p_pos + Point2(2, 0) * p_size, Size2(1, 3) * p_size), p_color);
752
}
753
if (x & (1 << 4)) {
754
RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(p_pos + Point2(2, 2) * p_size, Size2(1, 3) * p_size), p_color);
755
}
756
if (x & (1 << 3)) {
757
RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(p_pos + Point2(0, 4) * p_size, Size2(3, 1) * p_size), p_color);
758
}
759
if (x & (1 << 2)) {
760
RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(p_pos + Point2(0, 2) * p_size, Size2(1, 3) * p_size), p_color);
761
}
762
if (x & (1 << 1)) {
763
RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(p_pos, Size2(1, 3) * p_size), p_color);
764
}
765
if (x & (1 << 0)) {
766
RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(p_pos + Point2(0, 2) * p_size, Size2(3, 1) * p_size), p_color);
767
}
768
}
769
770
void TextServer::draw_hex_code_box(const RID &p_canvas, int64_t p_size, const Vector2 &p_pos, int64_t p_index, const Color &p_color) const {
771
if (p_index == 0) {
772
return;
773
}
774
775
int w = ((p_index <= 0xFF) ? 1 : ((p_index <= 0xFFFF) ? 2 : 3));
776
int sp = MAX(0, w - 1);
777
int sz = MAX(1, Math::round(p_size / 15.f));
778
779
Size2 size = Vector2(4 + 3 * w + sp, 15) * sz;
780
Point2 pos = p_pos - Point2i(0, size.y * 0.85);
781
782
// Draw frame.
783
RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(pos + Point2(0, 0), Size2(sz, size.y)), p_color);
784
RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(pos + Point2(size.x - sz, 0), Size2(sz, size.y)), p_color);
785
RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(pos + Point2(0, 0), Size2(size.x, sz)), p_color);
786
RenderingServer::get_singleton()->canvas_item_add_rect(p_canvas, Rect2(pos + Point2(0, size.y - sz), Size2(size.x, sz)), p_color);
787
788
uint8_t a = p_index & 0x0F;
789
uint8_t b = (p_index >> 4) & 0x0F;
790
uint8_t c = (p_index >> 8) & 0x0F;
791
uint8_t d = (p_index >> 12) & 0x0F;
792
uint8_t e = (p_index >> 16) & 0x0F;
793
uint8_t f = (p_index >> 20) & 0x0F;
794
795
// Draw hex code.
796
if (p_index <= 0xFF) {
797
_draw_hex_code_box_number(p_canvas, sz, pos + Point2(2, 2) * sz, b, p_color);
798
_draw_hex_code_box_number(p_canvas, sz, pos + Point2(2, 8) * sz, a, p_color);
799
} else if (p_index <= 0xFFFF) {
800
_draw_hex_code_box_number(p_canvas, sz, pos + Point2(2, 2) * sz, d, p_color);
801
_draw_hex_code_box_number(p_canvas, sz, pos + Point2(6, 2) * sz, c, p_color);
802
_draw_hex_code_box_number(p_canvas, sz, pos + Point2(2, 8) * sz, b, p_color);
803
_draw_hex_code_box_number(p_canvas, sz, pos + Point2(6, 8) * sz, a, p_color);
804
} else {
805
_draw_hex_code_box_number(p_canvas, sz, pos + Point2(2, 2) * sz, f, p_color);
806
_draw_hex_code_box_number(p_canvas, sz, pos + Point2(6, 2) * sz, e, p_color);
807
_draw_hex_code_box_number(p_canvas, sz, pos + Point2(10, 2) * sz, d, p_color);
808
_draw_hex_code_box_number(p_canvas, sz, pos + Point2(2, 8) * sz, c, p_color);
809
_draw_hex_code_box_number(p_canvas, sz, pos + Point2(6, 8) * sz, b, p_color);
810
_draw_hex_code_box_number(p_canvas, sz, pos + Point2(10, 8) * sz, a, p_color);
811
}
812
}
813
814
bool TextServer::shaped_text_has_visible_chars(const RID &p_shaped) const {
815
int v_size = shaped_text_get_glyph_count(p_shaped);
816
if (v_size == 0) {
817
return false;
818
}
819
820
const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
821
for (int i = 0; i < v_size; i++) {
822
if (glyphs[i].index != 0 && (glyphs[i].flags & GRAPHEME_IS_VIRTUAL) != GRAPHEME_IS_VIRTUAL) {
823
return true;
824
}
825
}
826
return false;
827
}
828
829
PackedInt32Array TextServer::shaped_text_get_line_breaks_adv(const RID &p_shaped, const PackedFloat32Array &p_width, int64_t p_start, bool p_once, BitField<TextServer::LineBreakFlag> p_break_flags) const {
830
PackedInt32Array lines;
831
832
ERR_FAIL_COND_V(p_width.is_empty(), lines);
833
834
TextServer::Orientation orientation = shaped_text_get_orientation(p_shaped);
835
const_cast<TextServer *>(this)->shaped_text_update_breaks(p_shaped);
836
const Vector2i &range = shaped_text_get_range(p_shaped);
837
838
real_t width = 0.f;
839
int line_start = MAX(p_start, range.x);
840
int last_end = line_start;
841
int prev_safe_break = 0;
842
int last_safe_break = -1;
843
int word_count = 0;
844
int chunk = 0;
845
int prev_chunk = -1;
846
bool trim_next = false;
847
848
#ifndef DISABLE_DEPRECATED
849
if (p_break_flags.has_flag(BREAK_TRIM_EDGE_SPACES)) {
850
p_break_flags = p_break_flags | BREAK_TRIM_START_EDGE_SPACES | BREAK_TRIM_END_EDGE_SPACES;
851
}
852
#endif
853
854
int l_size = shaped_text_get_glyph_count(p_shaped);
855
const Glyph *l_gl = const_cast<TextServer *>(this)->shaped_text_sort_logical(p_shaped);
856
857
int indent_end = 0;
858
double indent = 0.0;
859
860
for (int i = 0; i < l_size; i++) {
861
double l_width = p_width[chunk];
862
863
if (p_break_flags.has_flag(BREAK_TRIM_INDENT) && chunk != prev_chunk) {
864
indent = 0.0;
865
for (int j = indent_end; j < l_size; j++) {
866
if ((l_gl[j].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB || (l_gl[j].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE) {
867
if (indent + l_gl[j].advance * l_gl[j].repeat > l_width) {
868
indent = 0.0;
869
}
870
indent += l_gl[j].advance * l_gl[j].repeat;
871
indent_end = l_gl[j].end;
872
} else {
873
break;
874
}
875
}
876
indent = MIN(indent, 0.6 * l_width);
877
prev_chunk = chunk;
878
}
879
880
if (l_width > indent && i > indent_end) {
881
l_width -= indent;
882
}
883
if (l_gl[i].start < p_start) {
884
prev_safe_break = i + 1;
885
continue;
886
}
887
if (l_gl[i].count > 0) {
888
float adv = 0.0;
889
for (int j = i; j < l_size && l_gl[i].end == l_gl[j].end && l_gl[i].start == l_gl[j].start; j++) {
890
adv += l_gl[j].advance * l_gl[j].repeat;
891
}
892
if ((l_width > 0) && (width + adv > l_width) && (last_safe_break >= 0)) {
893
int cur_safe_brk = last_safe_break;
894
if (p_break_flags.has_flag(BREAK_TRIM_START_EDGE_SPACES) || p_break_flags.has_flag(BREAK_TRIM_END_EDGE_SPACES)) {
895
int start_pos = prev_safe_break;
896
int end_pos = last_safe_break;
897
while (p_break_flags.has_flag(BREAK_TRIM_START_EDGE_SPACES) && trim_next && (start_pos < end_pos) && ((l_gl[start_pos].flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN) && ((l_gl[start_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (l_gl[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (l_gl[start_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
898
start_pos += l_gl[start_pos].count;
899
}
900
while (p_break_flags.has_flag(BREAK_TRIM_END_EDGE_SPACES) && (start_pos <= end_pos) && (end_pos > 0) && ((l_gl[end_pos].flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN) && ((l_gl[end_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (l_gl[end_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (l_gl[end_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
901
end_pos -= l_gl[end_pos].count;
902
}
903
if (last_end <= l_gl[start_pos].start && l_gl[start_pos].start != l_gl[end_pos].end) {
904
lines.push_back(l_gl[start_pos].start);
905
lines.push_back(l_gl[end_pos].end);
906
cur_safe_brk = last_safe_break;
907
last_end = l_gl[end_pos].end;
908
}
909
trim_next = true;
910
} else {
911
if (last_end <= line_start) {
912
lines.push_back(line_start);
913
lines.push_back(l_gl[last_safe_break].end);
914
last_end = l_gl[last_safe_break].end;
915
}
916
}
917
line_start = l_gl[cur_safe_brk].end;
918
prev_safe_break = cur_safe_brk + 1;
919
while (prev_safe_break < l_size && l_gl[prev_safe_break].end == line_start) {
920
prev_safe_break++;
921
}
922
i = cur_safe_brk;
923
last_safe_break = -1;
924
width = 0;
925
word_count = 0;
926
chunk++;
927
if (chunk >= p_width.size()) {
928
chunk = 0;
929
if (p_once) {
930
return lines;
931
}
932
}
933
continue;
934
}
935
if (p_break_flags.has_flag(BREAK_MANDATORY)) {
936
if ((l_gl[i].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD) {
937
int cur_safe_brk = i;
938
if (p_break_flags.has_flag(BREAK_TRIM_START_EDGE_SPACES) || p_break_flags.has_flag(BREAK_TRIM_END_EDGE_SPACES)) {
939
int start_pos = prev_safe_break;
940
int end_pos = i;
941
while (p_break_flags.has_flag(BREAK_TRIM_START_EDGE_SPACES) && trim_next && (start_pos < end_pos) && ((l_gl[start_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (l_gl[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (l_gl[start_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
942
start_pos += l_gl[start_pos].count;
943
}
944
while (p_break_flags.has_flag(BREAK_TRIM_END_EDGE_SPACES) && (start_pos <= end_pos) && (end_pos > 0) && ((l_gl[end_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (l_gl[end_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (l_gl[end_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
945
end_pos -= l_gl[end_pos].count;
946
}
947
if (last_end <= l_gl[start_pos].start && l_gl[start_pos].start != l_gl[end_pos].end) {
948
lines.push_back(l_gl[start_pos].start);
949
lines.push_back(l_gl[end_pos].end);
950
last_end = l_gl[i].end;
951
cur_safe_brk = i;
952
}
953
trim_next = true;
954
} else {
955
if (last_end <= line_start) {
956
lines.push_back(line_start);
957
lines.push_back(l_gl[i].end);
958
last_end = l_gl[i].end;
959
}
960
}
961
line_start = l_gl[cur_safe_brk].end;
962
prev_safe_break = cur_safe_brk + 1;
963
while (prev_safe_break < l_size && l_gl[prev_safe_break].end == line_start) {
964
prev_safe_break++;
965
}
966
last_safe_break = -1;
967
width = 0;
968
chunk = 0;
969
if (p_once) {
970
return lines;
971
}
972
continue;
973
}
974
}
975
if (p_break_flags.has_flag(BREAK_WORD_BOUND)) {
976
if ((l_gl[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
977
if ((l_gl[i].flags & GRAPHEME_IS_SOFT_HYPHEN) == GRAPHEME_IS_SOFT_HYPHEN) {
978
uint32_t gl = font_get_glyph_index(l_gl[i].font_rid, l_gl[i].font_size, 0x00ad, 0);
979
float w = font_get_glyph_advance(l_gl[i].font_rid, l_gl[i].font_size, gl)[(orientation == ORIENTATION_HORIZONTAL) ? 0 : 1];
980
if (width + adv + w <= p_width[chunk]) {
981
last_safe_break = i;
982
word_count++;
983
}
984
} else if (i >= indent_end) {
985
last_safe_break = i;
986
word_count++;
987
}
988
}
989
}
990
if (p_break_flags.has_flag(BREAK_GRAPHEME_BOUND) && word_count == 0) {
991
last_safe_break = i;
992
}
993
}
994
width += l_gl[i].advance * l_gl[i].repeat;
995
}
996
997
if (l_size > 0) {
998
if (lines.is_empty() || (lines[lines.size() - 1] < range.y && prev_safe_break < l_size)) {
999
if (p_break_flags.has_flag(BREAK_TRIM_START_EDGE_SPACES)) {
1000
int start_pos = (prev_safe_break < l_size) ? prev_safe_break : l_size - 1;
1001
if (last_end <= l_gl[start_pos].start) {
1002
int end_pos = l_size - 1;
1003
while (trim_next && (start_pos < end_pos) && ((l_gl[start_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (l_gl[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (l_gl[start_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
1004
start_pos += l_gl[start_pos].count;
1005
}
1006
lines.push_back(l_gl[start_pos].start);
1007
} else {
1008
lines.push_back(last_end);
1009
}
1010
} else {
1011
lines.push_back(MAX(last_end, line_start));
1012
}
1013
lines.push_back(range.y);
1014
}
1015
} else {
1016
lines.push_back(0);
1017
lines.push_back(0);
1018
}
1019
1020
return lines;
1021
}
1022
1023
PackedInt32Array TextServer::shaped_text_get_line_breaks(const RID &p_shaped, double p_width, int64_t p_start, BitField<TextServer::LineBreakFlag> p_break_flags) const {
1024
PackedInt32Array lines;
1025
1026
const_cast<TextServer *>(this)->shaped_text_update_breaks(p_shaped);
1027
const Vector2i &range = shaped_text_get_range(p_shaped);
1028
1029
double width = 0.f;
1030
int line_start = MAX(p_start, range.x);
1031
int last_end = line_start;
1032
int prev_safe_break = 0;
1033
int last_safe_break = -1;
1034
int word_count = 0;
1035
bool trim_next = false;
1036
1037
#ifndef DISABLE_DEPRECATED
1038
if (p_break_flags.has_flag(BREAK_TRIM_EDGE_SPACES)) {
1039
p_break_flags = p_break_flags | BREAK_TRIM_START_EDGE_SPACES | BREAK_TRIM_END_EDGE_SPACES;
1040
}
1041
#endif
1042
1043
TextServer::Orientation orientation = shaped_text_get_orientation(p_shaped);
1044
int l_size = shaped_text_get_glyph_count(p_shaped);
1045
const Glyph *l_gl = const_cast<TextServer *>(this)->shaped_text_sort_logical(p_shaped);
1046
1047
int indent_end = 0;
1048
double indent = 0.0;
1049
if (p_break_flags.has_flag(BREAK_TRIM_INDENT)) {
1050
for (int i = 0; i < l_size; i++) {
1051
if ((l_gl[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB || (l_gl[i].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE) {
1052
if (indent + l_gl[i].advance * l_gl[i].repeat > p_width) {
1053
indent = 0.0;
1054
}
1055
indent += l_gl[i].advance * l_gl[i].repeat;
1056
indent_end = l_gl[i].end;
1057
} else {
1058
break;
1059
}
1060
}
1061
indent = MIN(indent, 0.6 * p_width);
1062
}
1063
1064
double l_width = p_width;
1065
for (int i = 0; i < l_size; i++) {
1066
if (l_gl[i].start < p_start) {
1067
prev_safe_break = i + 1;
1068
continue;
1069
}
1070
if (l_gl[i].count > 0) {
1071
float adv = 0.0;
1072
for (int j = i; j < l_size && l_gl[i].end == l_gl[j].end && l_gl[i].start == l_gl[j].start; j++) {
1073
adv += l_gl[j].advance * l_gl[j].repeat;
1074
}
1075
if ((l_width > 0) && (width + adv > l_width) && (last_safe_break >= 0)) {
1076
int cur_safe_brk = last_safe_break;
1077
if (p_break_flags.has_flag(BREAK_TRIM_START_EDGE_SPACES) || p_break_flags.has_flag(BREAK_TRIM_END_EDGE_SPACES)) {
1078
int start_pos = prev_safe_break;
1079
int end_pos = last_safe_break;
1080
while (p_break_flags.has_flag(BREAK_TRIM_START_EDGE_SPACES) && trim_next && (start_pos < end_pos) && ((l_gl[start_pos].flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN) && ((l_gl[start_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (l_gl[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (l_gl[start_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
1081
start_pos += l_gl[start_pos].count;
1082
}
1083
while (p_break_flags.has_flag(BREAK_TRIM_END_EDGE_SPACES) && (start_pos <= end_pos) && (end_pos > 0) && ((l_gl[end_pos].flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN) && ((l_gl[end_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (l_gl[end_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (l_gl[end_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
1084
end_pos -= l_gl[end_pos].count;
1085
}
1086
if (last_end <= l_gl[start_pos].start && l_gl[start_pos].start != l_gl[end_pos].end) {
1087
lines.push_back(l_gl[start_pos].start);
1088
lines.push_back(l_gl[end_pos].end);
1089
if (p_width > indent && i > indent_end) {
1090
l_width = p_width - indent;
1091
}
1092
cur_safe_brk = last_safe_break;
1093
last_end = l_gl[end_pos].end;
1094
}
1095
trim_next = true;
1096
} else {
1097
if (last_end <= line_start) {
1098
lines.push_back(line_start);
1099
lines.push_back(l_gl[last_safe_break].end);
1100
if (p_width > indent && i > indent_end) {
1101
l_width = p_width - indent;
1102
}
1103
last_end = l_gl[last_safe_break].end;
1104
}
1105
}
1106
line_start = l_gl[cur_safe_brk].end;
1107
prev_safe_break = cur_safe_brk + 1;
1108
while (prev_safe_break < l_size && l_gl[prev_safe_break].end == line_start) {
1109
prev_safe_break++;
1110
}
1111
i = cur_safe_brk;
1112
last_safe_break = -1;
1113
width = 0;
1114
word_count = 0;
1115
continue;
1116
}
1117
if (p_break_flags.has_flag(BREAK_MANDATORY)) {
1118
if ((l_gl[i].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD) {
1119
int cur_safe_brk = i;
1120
if (p_break_flags.has_flag(BREAK_TRIM_START_EDGE_SPACES) || p_break_flags.has_flag(BREAK_TRIM_END_EDGE_SPACES)) {
1121
int start_pos = prev_safe_break;
1122
int end_pos = i;
1123
while (p_break_flags.has_flag(BREAK_TRIM_START_EDGE_SPACES) && trim_next && (start_pos < end_pos) && ((l_gl[start_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (l_gl[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (l_gl[start_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
1124
start_pos += l_gl[start_pos].count;
1125
}
1126
while (p_break_flags.has_flag(BREAK_TRIM_END_EDGE_SPACES) && (start_pos <= end_pos) && (end_pos > 0) && ((l_gl[end_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (l_gl[end_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (l_gl[end_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
1127
end_pos -= l_gl[end_pos].count;
1128
}
1129
trim_next = true;
1130
if (last_end <= l_gl[start_pos].start && l_gl[start_pos].start != l_gl[end_pos].end) {
1131
lines.push_back(l_gl[start_pos].start);
1132
lines.push_back(l_gl[end_pos].end);
1133
if (p_width > indent && i > indent_end) {
1134
l_width = p_width - indent;
1135
}
1136
last_end = l_gl[i].end;
1137
cur_safe_brk = i;
1138
}
1139
} else {
1140
if (last_end <= line_start) {
1141
lines.push_back(line_start);
1142
lines.push_back(l_gl[i].end);
1143
if (p_width > indent && i > indent_end) {
1144
l_width = p_width - indent;
1145
}
1146
last_end = l_gl[i].end;
1147
}
1148
}
1149
line_start = l_gl[cur_safe_brk].end;
1150
prev_safe_break = cur_safe_brk + 1;
1151
while (prev_safe_break < l_size && l_gl[prev_safe_break].end == line_start) {
1152
prev_safe_break++;
1153
}
1154
last_safe_break = -1;
1155
width = 0;
1156
continue;
1157
}
1158
}
1159
if (p_break_flags.has_flag(BREAK_WORD_BOUND)) {
1160
if ((l_gl[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
1161
if ((l_gl[i].flags & GRAPHEME_IS_SOFT_HYPHEN) == GRAPHEME_IS_SOFT_HYPHEN) {
1162
uint32_t gl = font_get_glyph_index(l_gl[i].font_rid, l_gl[i].font_size, 0x00AD, 0);
1163
float w = font_get_glyph_advance(l_gl[i].font_rid, l_gl[i].font_size, gl)[(orientation == ORIENTATION_HORIZONTAL) ? 0 : 1];
1164
if (width + adv + w <= p_width) {
1165
last_safe_break = i;
1166
word_count++;
1167
}
1168
} else if (i >= indent_end) {
1169
last_safe_break = i;
1170
word_count++;
1171
}
1172
}
1173
if (p_break_flags.has_flag(BREAK_ADAPTIVE) && word_count == 0) {
1174
last_safe_break = i;
1175
}
1176
}
1177
if (p_break_flags.has_flag(BREAK_GRAPHEME_BOUND)) {
1178
last_safe_break = i;
1179
}
1180
}
1181
width += l_gl[i].advance * l_gl[i].repeat;
1182
}
1183
1184
if (l_size > 0) {
1185
if (lines.is_empty() || (lines[lines.size() - 1] < range.y && prev_safe_break < l_size)) {
1186
if (p_break_flags.has_flag(BREAK_TRIM_START_EDGE_SPACES)) {
1187
int start_pos = (prev_safe_break < l_size) ? prev_safe_break : l_size - 1;
1188
if (last_end <= l_gl[start_pos].start) {
1189
int end_pos = l_size - 1;
1190
while (trim_next && (start_pos < end_pos) && ((l_gl[start_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (l_gl[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (l_gl[start_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
1191
start_pos += l_gl[start_pos].count;
1192
}
1193
lines.push_back(l_gl[start_pos].start);
1194
} else {
1195
lines.push_back(last_end);
1196
}
1197
} else {
1198
lines.push_back(MAX(last_end, line_start));
1199
}
1200
lines.push_back(range.y);
1201
}
1202
} else {
1203
lines.push_back(0);
1204
lines.push_back(0);
1205
}
1206
1207
return lines;
1208
}
1209
1210
PackedInt32Array TextServer::shaped_text_get_word_breaks(const RID &p_shaped, BitField<TextServer::GraphemeFlag> p_grapheme_flags, BitField<TextServer::GraphemeFlag> p_skip_grapheme_flags) const {
1211
PackedInt32Array words;
1212
1213
const_cast<TextServer *>(this)->shaped_text_update_justification_ops(p_shaped);
1214
const Vector2i &range = shaped_text_get_range(p_shaped);
1215
1216
int word_start = range.x;
1217
1218
const int l_size = shaped_text_get_glyph_count(p_shaped);
1219
const Glyph *l_gl = const_cast<TextServer *>(this)->shaped_text_sort_logical(p_shaped);
1220
1221
for (int i = 0; i < l_size; i++) {
1222
if (l_gl[i].count > 0) {
1223
if ((l_gl[i].flags & p_grapheme_flags) != 0 && (l_gl[i].flags & p_skip_grapheme_flags) == 0) {
1224
int next = (i == 0) ? l_gl[i].start : l_gl[i - 1].end;
1225
if (word_start < next) {
1226
words.push_back(word_start);
1227
words.push_back(next);
1228
}
1229
word_start = l_gl[i].end;
1230
}
1231
}
1232
}
1233
if (l_size > 0) {
1234
if (word_start != range.y) {
1235
words.push_back(word_start);
1236
words.push_back(range.y);
1237
}
1238
}
1239
1240
return words;
1241
}
1242
1243
CaretInfo TextServer::shaped_text_get_carets(const RID &p_shaped, int64_t p_position) const {
1244
Vector<Rect2> carets;
1245
1246
TextServer::Orientation orientation = shaped_text_get_orientation(p_shaped);
1247
const Vector2 &range = shaped_text_get_range(p_shaped);
1248
real_t ascent = shaped_text_get_ascent(p_shaped);
1249
real_t descent = shaped_text_get_descent(p_shaped);
1250
real_t height = (ascent + descent) / 2;
1251
1252
real_t off = 0.0f;
1253
real_t obj_off = -1.0f;
1254
CaretInfo caret;
1255
caret.l_dir = DIRECTION_AUTO;
1256
caret.t_dir = DIRECTION_AUTO;
1257
1258
int v_size = shaped_text_get_glyph_count(p_shaped);
1259
const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
1260
1261
for (int i = 0; i < v_size; i++) {
1262
if (glyphs[i].count > 0) {
1263
// Skip inline objects.
1264
if ((glyphs[i].flags & GRAPHEME_IS_EMBEDDED_OBJECT) == GRAPHEME_IS_EMBEDDED_OBJECT && glyphs[i].start == glyphs[i].end) {
1265
obj_off = glyphs[i].advance;
1266
continue;
1267
}
1268
// Caret before grapheme (top / left).
1269
if (p_position == glyphs[i].start && ((glyphs[i].flags & GRAPHEME_IS_VIRTUAL) != GRAPHEME_IS_VIRTUAL)) {
1270
real_t advance = 0.f;
1271
for (int j = 0; j < glyphs[i].count; j++) {
1272
advance += glyphs[i + j].advance * glyphs[i + j].repeat;
1273
}
1274
real_t char_adv = advance / (real_t)(glyphs[i].end - glyphs[i].start);
1275
Rect2 cr;
1276
if (orientation == ORIENTATION_HORIZONTAL) {
1277
if (glyphs[i].start == range.x) {
1278
cr.size.y = height * 2;
1279
} else {
1280
cr.size.y = height;
1281
}
1282
cr.position.y = -ascent;
1283
cr.position.x = off;
1284
if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
1285
caret.t_dir = DIRECTION_RTL;
1286
cr.position.x += advance;
1287
cr.size.x = -char_adv;
1288
} else {
1289
caret.t_dir = DIRECTION_LTR;
1290
cr.size.x = char_adv;
1291
}
1292
} else {
1293
if (glyphs[i].start == range.x) {
1294
cr.size.x = height * 2;
1295
} else {
1296
cr.size.x = height;
1297
}
1298
cr.position.x = -ascent;
1299
cr.position.y = off;
1300
if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
1301
caret.t_dir = DIRECTION_RTL;
1302
cr.position.y += advance;
1303
cr.size.y = -char_adv;
1304
} else {
1305
caret.t_dir = DIRECTION_LTR;
1306
cr.size.y = char_adv;
1307
}
1308
}
1309
caret.t_caret = cr;
1310
}
1311
// Caret after grapheme (bottom / right).
1312
if (p_position == glyphs[i].end && ((glyphs[i].flags & GRAPHEME_IS_VIRTUAL) != GRAPHEME_IS_VIRTUAL)) {
1313
real_t advance = 0.f;
1314
for (int j = 0; j < glyphs[i].count; j++) {
1315
advance += glyphs[i + j].advance * glyphs[i + j].repeat;
1316
}
1317
real_t char_adv = advance / (real_t)(glyphs[i].end - glyphs[i].start);
1318
Rect2 cr;
1319
if (orientation == ORIENTATION_HORIZONTAL) {
1320
if (glyphs[i].end == range.y) {
1321
cr.size.y = height * 2;
1322
cr.position.y = -ascent;
1323
} else {
1324
cr.size.y = height;
1325
cr.position.y = -ascent + height;
1326
}
1327
cr.position.x = off;
1328
if ((glyphs[i].flags & GRAPHEME_IS_RTL) != GRAPHEME_IS_RTL) {
1329
caret.l_dir = DIRECTION_LTR;
1330
cr.position.x += advance;
1331
cr.size.x = -char_adv;
1332
} else {
1333
caret.l_dir = DIRECTION_RTL;
1334
cr.size.x = char_adv;
1335
}
1336
} else {
1337
cr.size.y = 1.0f;
1338
if (glyphs[i].end == range.y) {
1339
cr.size.x = height * 2;
1340
cr.position.x = -ascent;
1341
} else {
1342
cr.size.x = height;
1343
cr.position.x = -ascent + height;
1344
}
1345
cr.position.y = off;
1346
if ((glyphs[i].flags & GRAPHEME_IS_RTL) != GRAPHEME_IS_RTL) {
1347
caret.l_dir = DIRECTION_LTR;
1348
cr.position.y += advance;
1349
cr.size.y = -char_adv;
1350
} else {
1351
caret.l_dir = DIRECTION_RTL;
1352
cr.position.x += advance;
1353
cr.size.y = char_adv;
1354
}
1355
}
1356
cr.position.x += MAX(0.0, obj_off); // Prevent split caret when on an inline object.
1357
caret.l_caret = cr;
1358
}
1359
// Caret inside grapheme (middle).
1360
if (p_position > glyphs[i].start && p_position < glyphs[i].end && (glyphs[i].flags & GRAPHEME_IS_VIRTUAL) != GRAPHEME_IS_VIRTUAL) {
1361
real_t advance = 0.f;
1362
for (int j = 0; j < glyphs[i].count; j++) {
1363
advance += glyphs[i + j].advance * glyphs[i + j].repeat;
1364
}
1365
real_t char_adv = advance / (real_t)(glyphs[i].end - glyphs[i].start);
1366
Rect2 cr;
1367
if (orientation == ORIENTATION_HORIZONTAL) {
1368
cr.size.y = height * 2;
1369
cr.position.y = -ascent;
1370
if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
1371
cr.position.x = off + char_adv * (glyphs[i].end - p_position);
1372
cr.size.x = -char_adv;
1373
} else {
1374
cr.position.x = off + char_adv * (p_position - glyphs[i].start);
1375
cr.size.x = char_adv;
1376
}
1377
} else {
1378
cr.size.x = height * 2;
1379
cr.position.x = -ascent;
1380
if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
1381
cr.position.y = off + char_adv * (glyphs[i].end - p_position);
1382
cr.size.y = -char_adv;
1383
} else {
1384
cr.position.y = off + char_adv * (p_position - glyphs[i].start);
1385
cr.size.y = char_adv;
1386
}
1387
}
1388
caret.t_caret = cr;
1389
caret.l_caret = cr;
1390
}
1391
}
1392
off += glyphs[i].advance * glyphs[i].repeat;
1393
if (obj_off >= 0.0) {
1394
off += obj_off;
1395
obj_off = -1.0;
1396
}
1397
}
1398
return caret;
1399
}
1400
1401
Dictionary TextServer::_shaped_text_get_carets_wrapper(const RID &p_shaped, int64_t p_position) const {
1402
Dictionary ret;
1403
1404
CaretInfo caret = shaped_text_get_carets(p_shaped, p_position);
1405
1406
ret["leading_rect"] = caret.l_caret;
1407
ret["leading_direction"] = caret.l_dir;
1408
ret["trailing_rect"] = caret.t_caret;
1409
ret["trailing_direction"] = caret.t_dir;
1410
1411
return ret;
1412
}
1413
1414
TextServer::Direction TextServer::shaped_text_get_dominant_direction_in_range(const RID &p_shaped, int64_t p_start, int64_t p_end) const {
1415
if (p_start == p_end) {
1416
return DIRECTION_AUTO;
1417
}
1418
1419
int start = MIN(p_start, p_end);
1420
int end = MAX(p_start, p_end);
1421
1422
int rtl = 0;
1423
int ltr = 0;
1424
1425
int v_size = shaped_text_get_glyph_count(p_shaped);
1426
const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
1427
1428
for (int i = 0; i < v_size; i++) {
1429
if ((glyphs[i].end > start) && (glyphs[i].start < end)) {
1430
if (glyphs[i].count > 0) {
1431
if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
1432
rtl++;
1433
} else {
1434
ltr++;
1435
}
1436
}
1437
}
1438
}
1439
if (ltr == rtl) {
1440
return DIRECTION_AUTO;
1441
} else if (ltr > rtl) {
1442
return DIRECTION_LTR;
1443
} else {
1444
return DIRECTION_RTL;
1445
}
1446
}
1447
1448
_FORCE_INLINE_ void _push_range(Vector<Vector2> &r_vector, real_t p_start, real_t p_end) {
1449
if (!r_vector.is_empty() && Math::is_equal_approx(r_vector[r_vector.size() - 1].y, p_start, (real_t)UNIT_EPSILON)) {
1450
r_vector.write[r_vector.size() - 1].y = p_end;
1451
} else {
1452
r_vector.push_back(Vector2(p_start, p_end));
1453
}
1454
}
1455
1456
Vector<Vector2> TextServer::shaped_text_get_selection(const RID &p_shaped, int64_t p_start, int64_t p_end) const {
1457
Vector<Vector2> ranges;
1458
1459
if (p_start == p_end) {
1460
return ranges;
1461
}
1462
1463
int start = MIN(p_start, p_end);
1464
int end = MAX(p_start, p_end);
1465
1466
int v_size = shaped_text_get_glyph_count(p_shaped);
1467
const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
1468
1469
real_t off = 0.0f;
1470
for (int i = 0; i < v_size; i++) {
1471
for (int k = 0; k < glyphs[i].repeat; k++) {
1472
if ((glyphs[i].count > 0) && ((glyphs[i].index != 0) || ((glyphs[i].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE))) {
1473
if (glyphs[i].start < end && glyphs[i].end > start) {
1474
// Grapheme fully in selection range.
1475
if (glyphs[i].start >= start && glyphs[i].end <= end) {
1476
real_t advance = 0.f;
1477
for (int j = 0; j < glyphs[i].count; j++) {
1478
advance += glyphs[i + j].advance;
1479
}
1480
_push_range(ranges, off, off + advance);
1481
}
1482
// Only start of grapheme is in selection range.
1483
if (glyphs[i].start >= start && glyphs[i].end > end) {
1484
real_t advance = 0.f;
1485
for (int j = 0; j < glyphs[i].count; j++) {
1486
advance += glyphs[i + j].advance;
1487
}
1488
real_t char_adv = advance / (real_t)(glyphs[i].end - glyphs[i].start);
1489
if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
1490
_push_range(ranges, off + char_adv * (glyphs[i].end - end), off + advance);
1491
} else {
1492
_push_range(ranges, off, off + char_adv * (end - glyphs[i].start));
1493
}
1494
}
1495
// Only end of grapheme is in selection range.
1496
if (glyphs[i].start < start && glyphs[i].end <= end) {
1497
real_t advance = 0.f;
1498
for (int j = 0; j < glyphs[i].count; j++) {
1499
advance += glyphs[i + j].advance;
1500
}
1501
real_t char_adv = advance / (real_t)(glyphs[i].end - glyphs[i].start);
1502
if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
1503
_push_range(ranges, off, off + char_adv * (glyphs[i].end - start));
1504
} else {
1505
_push_range(ranges, off + char_adv * (start - glyphs[i].start), off + advance);
1506
}
1507
}
1508
// Selection range is within grapheme.
1509
if (glyphs[i].start < start && glyphs[i].end > end) {
1510
real_t advance = 0.f;
1511
for (int j = 0; j < glyphs[i].count; j++) {
1512
advance += glyphs[i + j].advance;
1513
}
1514
real_t char_adv = advance / (real_t)(glyphs[i].end - glyphs[i].start);
1515
if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
1516
_push_range(ranges, off + char_adv * (glyphs[i].end - end), off + char_adv * (glyphs[i].end - start));
1517
} else {
1518
_push_range(ranges, off + char_adv * (start - glyphs[i].start), off + char_adv * (end - glyphs[i].start));
1519
}
1520
}
1521
}
1522
}
1523
off += glyphs[i].advance;
1524
}
1525
}
1526
1527
return ranges;
1528
}
1529
1530
int64_t TextServer::shaped_text_hit_test_grapheme(const RID &p_shaped, double p_coords) const {
1531
// Exact grapheme hit test, return -1 if missed.
1532
double off = 0.0f;
1533
1534
int v_size = shaped_text_get_glyph_count(p_shaped);
1535
const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
1536
1537
for (int i = 0; i < v_size; i++) {
1538
for (int j = 0; j < glyphs[i].repeat; j++) {
1539
if (p_coords >= off && p_coords < off + glyphs[i].advance) {
1540
return i;
1541
}
1542
off += glyphs[i].advance;
1543
}
1544
}
1545
return -1;
1546
}
1547
1548
int64_t TextServer::shaped_text_hit_test_position(const RID &p_shaped, double p_coords) const {
1549
int v_size = shaped_text_get_glyph_count(p_shaped);
1550
const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
1551
1552
// Cursor placement hit test.
1553
1554
// Place caret to the left of the leftmost grapheme, or to position 0 if string is empty.
1555
if (Math::floor(p_coords) <= 0) {
1556
if (v_size > 0) {
1557
if ((glyphs[0].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
1558
return glyphs[0].end;
1559
} else {
1560
return glyphs[0].start;
1561
}
1562
} else {
1563
return 0;
1564
}
1565
}
1566
1567
// Place caret to the right of the rightmost grapheme, or to position 0 if string is empty.
1568
if (Math::ceil(p_coords) >= shaped_text_get_width(p_shaped)) {
1569
if (v_size > 0) {
1570
if ((glyphs[v_size - 1].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
1571
return glyphs[v_size - 1].start;
1572
} else {
1573
return glyphs[v_size - 1].end;
1574
}
1575
} else {
1576
return 0;
1577
}
1578
}
1579
1580
real_t off = 0.0f;
1581
for (int i = 0; i < v_size; i++) {
1582
if (glyphs[i].count > 0) {
1583
real_t advance = 0.f;
1584
for (int j = 0; j < glyphs[i].count; j++) {
1585
advance += glyphs[i + j].advance * glyphs[i + j].repeat;
1586
}
1587
if (((glyphs[i].flags & GRAPHEME_IS_VIRTUAL) == GRAPHEME_IS_VIRTUAL) && (p_coords >= off && p_coords < off + advance)) {
1588
if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
1589
return glyphs[i].end;
1590
} else {
1591
return glyphs[i].start;
1592
}
1593
}
1594
// Ligature, handle mid-grapheme hit.
1595
if (p_coords >= off && p_coords < off + advance && glyphs[i].end > glyphs[i].start + 1) {
1596
int cnt = glyphs[i].end - glyphs[i].start;
1597
real_t char_adv = advance / (real_t)(cnt);
1598
real_t sub_off = off;
1599
for (int j = 0; j < cnt; j++) {
1600
// Place caret to the left of clicked sub-grapheme.
1601
if (p_coords >= sub_off && p_coords < sub_off + char_adv / 2) {
1602
if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
1603
return glyphs[i].end - j;
1604
} else {
1605
return glyphs[i].start + j;
1606
}
1607
}
1608
// Place caret to the right of clicked sub-grapheme.
1609
if (p_coords >= sub_off + char_adv / 2 && p_coords < sub_off + char_adv) {
1610
if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
1611
return glyphs[i].start + (cnt - 1) - j;
1612
} else {
1613
return glyphs[i].end - (cnt - 1) + j;
1614
}
1615
}
1616
sub_off += char_adv;
1617
}
1618
}
1619
// Place caret to the left of clicked grapheme.
1620
if (p_coords >= off && p_coords < off + advance / 2) {
1621
if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
1622
return glyphs[i].end;
1623
} else {
1624
return glyphs[i].start;
1625
}
1626
}
1627
// Place caret to the right of clicked grapheme.
1628
if (p_coords >= off + advance / 2 && p_coords < off + advance) {
1629
if ((glyphs[i].flags & GRAPHEME_IS_RTL) == GRAPHEME_IS_RTL) {
1630
return glyphs[i].start;
1631
} else {
1632
return glyphs[i].end;
1633
}
1634
}
1635
}
1636
off += glyphs[i].advance * glyphs[i].repeat;
1637
}
1638
1639
return -1;
1640
}
1641
1642
Vector2 TextServer::shaped_text_get_grapheme_bounds(const RID &p_shaped, int64_t p_pos) const {
1643
int v_size = shaped_text_get_glyph_count(p_shaped);
1644
const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
1645
1646
real_t off = 0.0f;
1647
for (int i = 0; i < v_size; i++) {
1648
if ((glyphs[i].count > 0) && ((glyphs[i].index != 0) || ((glyphs[i].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE))) {
1649
if (glyphs[i].start <= p_pos && glyphs[i].end >= p_pos) {
1650
real_t advance = 0.f;
1651
for (int j = 0; j < glyphs[i].count; j++) {
1652
advance += glyphs[i + j].advance;
1653
}
1654
return Vector2(off, off + advance);
1655
}
1656
}
1657
off += glyphs[i].advance * glyphs[i].repeat;
1658
}
1659
1660
return Vector2();
1661
}
1662
1663
int64_t TextServer::shaped_text_next_grapheme_pos(const RID &p_shaped, int64_t p_pos) const {
1664
int v_size = shaped_text_get_glyph_count(p_shaped);
1665
const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
1666
for (int i = 0; i < v_size; i++) {
1667
if (p_pos >= glyphs[i].start && p_pos < glyphs[i].end) {
1668
return glyphs[i].end;
1669
}
1670
}
1671
return p_pos;
1672
}
1673
1674
int64_t TextServer::shaped_text_prev_grapheme_pos(const RID &p_shaped, int64_t p_pos) const {
1675
int v_size = shaped_text_get_glyph_count(p_shaped);
1676
const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
1677
for (int i = 0; i < v_size; i++) {
1678
if (p_pos > glyphs[i].start && p_pos <= glyphs[i].end) {
1679
return glyphs[i].start;
1680
}
1681
}
1682
1683
return p_pos;
1684
}
1685
1686
int64_t TextServer::shaped_text_prev_character_pos(const RID &p_shaped, int64_t p_pos) const {
1687
const PackedInt32Array &chars = shaped_text_get_character_breaks(p_shaped);
1688
int64_t prev = shaped_text_get_range(p_shaped).x;
1689
for (const int32_t &E : chars) {
1690
if (E >= p_pos) {
1691
return prev;
1692
}
1693
prev = E;
1694
}
1695
return prev;
1696
}
1697
1698
int64_t TextServer::shaped_text_next_character_pos(const RID &p_shaped, int64_t p_pos) const {
1699
const PackedInt32Array &chars = shaped_text_get_character_breaks(p_shaped);
1700
int64_t prev = shaped_text_get_range(p_shaped).x;
1701
for (const int32_t &E : chars) {
1702
if (E > p_pos) {
1703
return E;
1704
}
1705
prev = E;
1706
}
1707
return prev;
1708
}
1709
1710
int64_t TextServer::shaped_text_closest_character_pos(const RID &p_shaped, int64_t p_pos) const {
1711
const PackedInt32Array &chars = shaped_text_get_character_breaks(p_shaped);
1712
int64_t prev = shaped_text_get_range(p_shaped).x;
1713
for (const int32_t &E : chars) {
1714
if (E == p_pos) {
1715
return E;
1716
} else if (E > p_pos) {
1717
if ((E - p_pos) < (p_pos - prev)) {
1718
return E;
1719
} else {
1720
return prev;
1721
}
1722
}
1723
prev = E;
1724
}
1725
return prev;
1726
}
1727
1728
PackedInt32Array TextServer::string_get_character_breaks(const String &p_string, const String &p_language) const {
1729
PackedInt32Array ret;
1730
if (!p_string.is_empty()) {
1731
ret.resize(p_string.size() - 1);
1732
for (int i = 0; i < p_string.size() - 1; i++) {
1733
ret.write[i] = i + 1;
1734
}
1735
}
1736
return ret;
1737
}
1738
1739
void TextServer::shaped_text_draw(const RID &p_shaped, const RID &p_canvas, const Vector2 &p_pos, double p_clip_l, double p_clip_r, const Color &p_color, float p_oversampling) const {
1740
TextServer::Orientation orientation = shaped_text_get_orientation(p_shaped);
1741
bool hex_codes = shaped_text_get_preserve_control(p_shaped) || shaped_text_get_preserve_invalid(p_shaped);
1742
1743
bool rtl = shaped_text_get_direction(p_shaped) == DIRECTION_RTL;
1744
1745
int ellipsis_pos = shaped_text_get_ellipsis_pos(p_shaped);
1746
int trim_pos = shaped_text_get_trim_pos(p_shaped);
1747
1748
const Glyph *ellipsis_glyphs = shaped_text_get_ellipsis_glyphs(p_shaped);
1749
int ellipsis_gl_size = shaped_text_get_ellipsis_glyph_count(p_shaped);
1750
1751
int v_size = shaped_text_get_glyph_count(p_shaped);
1752
const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
1753
1754
Vector2 ofs;
1755
// Draw RTL ellipsis string when needed.
1756
if (rtl && ellipsis_pos >= 0) {
1757
for (int i = ellipsis_gl_size - 1; i >= 0; i--) {
1758
for (int j = 0; j < ellipsis_glyphs[i].repeat; j++) {
1759
font_draw_glyph(ellipsis_glyphs[i].font_rid, p_canvas, ellipsis_glyphs[i].font_size, ofs + p_pos + Vector2(ellipsis_glyphs[i].x_off, ellipsis_glyphs[i].y_off), ellipsis_glyphs[i].index, p_color, p_oversampling);
1760
if (orientation == ORIENTATION_HORIZONTAL) {
1761
ofs.x += ellipsis_glyphs[i].advance;
1762
} else {
1763
ofs.y += ellipsis_glyphs[i].advance;
1764
}
1765
}
1766
}
1767
}
1768
// Draw at the baseline.
1769
for (int i = 0; i < v_size; i++) {
1770
if (trim_pos >= 0) {
1771
if (rtl) {
1772
if (i < trim_pos) {
1773
continue;
1774
}
1775
} else {
1776
if (i >= trim_pos) {
1777
break;
1778
}
1779
}
1780
}
1781
for (int j = 0; j < glyphs[i].repeat; j++) {
1782
if (p_clip_r > 0) {
1783
// Clip right / bottom.
1784
if (orientation == ORIENTATION_HORIZONTAL) {
1785
if (ofs.x + glyphs[i].advance > p_clip_r) {
1786
return;
1787
}
1788
} else {
1789
if (ofs.y + glyphs[i].advance > p_clip_r) {
1790
return;
1791
}
1792
}
1793
}
1794
if (p_clip_l > 0) {
1795
// Clip left / top.
1796
if (orientation == ORIENTATION_HORIZONTAL) {
1797
if (ofs.x < p_clip_l) {
1798
ofs.x += glyphs[i].advance;
1799
continue;
1800
}
1801
} else {
1802
if (ofs.y < p_clip_l) {
1803
ofs.y += glyphs[i].advance;
1804
continue;
1805
}
1806
}
1807
}
1808
1809
if (glyphs[i].font_rid != RID()) {
1810
font_draw_glyph(glyphs[i].font_rid, p_canvas, glyphs[i].font_size, ofs + p_pos + Vector2(glyphs[i].x_off, glyphs[i].y_off), glyphs[i].index, p_color, p_oversampling);
1811
} else if (hex_codes && ((glyphs[i].flags & GRAPHEME_IS_VIRTUAL) != GRAPHEME_IS_VIRTUAL) && ((glyphs[i].flags & GRAPHEME_IS_EMBEDDED_OBJECT) != GRAPHEME_IS_EMBEDDED_OBJECT)) {
1812
TextServer::draw_hex_code_box(p_canvas, glyphs[i].font_size, ofs + p_pos + Vector2(glyphs[i].x_off, glyphs[i].y_off), glyphs[i].index, p_color);
1813
}
1814
if (orientation == ORIENTATION_HORIZONTAL) {
1815
ofs.x += glyphs[i].advance;
1816
} else {
1817
ofs.y += glyphs[i].advance;
1818
}
1819
}
1820
}
1821
// Draw LTR ellipsis string when needed.
1822
if (!rtl && ellipsis_pos >= 0) {
1823
for (int i = 0; i < ellipsis_gl_size; i++) {
1824
for (int j = 0; j < ellipsis_glyphs[i].repeat; j++) {
1825
font_draw_glyph(ellipsis_glyphs[i].font_rid, p_canvas, ellipsis_glyphs[i].font_size, ofs + p_pos + Vector2(ellipsis_glyphs[i].x_off, ellipsis_glyphs[i].y_off), ellipsis_glyphs[i].index, p_color, p_oversampling);
1826
if (orientation == ORIENTATION_HORIZONTAL) {
1827
ofs.x += ellipsis_glyphs[i].advance;
1828
} else {
1829
ofs.y += ellipsis_glyphs[i].advance;
1830
}
1831
}
1832
}
1833
}
1834
}
1835
1836
void TextServer::shaped_text_draw_outline(const RID &p_shaped, const RID &p_canvas, const Vector2 &p_pos, double p_clip_l, double p_clip_r, int64_t p_outline_size, const Color &p_color, float p_oversampling) const {
1837
TextServer::Orientation orientation = shaped_text_get_orientation(p_shaped);
1838
1839
bool rtl = (shaped_text_get_inferred_direction(p_shaped) == DIRECTION_RTL);
1840
1841
int ellipsis_pos = shaped_text_get_ellipsis_pos(p_shaped);
1842
int trim_pos = shaped_text_get_trim_pos(p_shaped);
1843
1844
const Glyph *ellipsis_glyphs = shaped_text_get_ellipsis_glyphs(p_shaped);
1845
int ellipsis_gl_size = shaped_text_get_ellipsis_glyph_count(p_shaped);
1846
1847
int v_size = shaped_text_get_glyph_count(p_shaped);
1848
const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
1849
1850
Vector2 ofs;
1851
// Draw RTL ellipsis string when needed.
1852
if (rtl && ellipsis_pos >= 0) {
1853
for (int i = ellipsis_gl_size - 1; i >= 0; i--) {
1854
for (int j = 0; j < ellipsis_glyphs[i].repeat; j++) {
1855
font_draw_glyph_outline(ellipsis_glyphs[i].font_rid, p_canvas, ellipsis_glyphs[i].font_size, p_outline_size, ofs + p_pos + Vector2(ellipsis_glyphs[i].x_off, ellipsis_glyphs[i].y_off), ellipsis_glyphs[i].index, p_color, p_oversampling);
1856
if (orientation == ORIENTATION_HORIZONTAL) {
1857
ofs.x += ellipsis_glyphs[i].advance;
1858
} else {
1859
ofs.y += ellipsis_glyphs[i].advance;
1860
}
1861
}
1862
}
1863
}
1864
// Draw at the baseline.
1865
for (int i = 0; i < v_size; i++) {
1866
if (trim_pos >= 0) {
1867
if (rtl) {
1868
if (i < trim_pos) {
1869
continue;
1870
}
1871
} else {
1872
if (i >= trim_pos) {
1873
break;
1874
}
1875
}
1876
}
1877
for (int j = 0; j < glyphs[i].repeat; j++) {
1878
if (p_clip_r > 0) {
1879
// Clip right / bottom.
1880
if (orientation == ORIENTATION_HORIZONTAL) {
1881
if (ofs.x + glyphs[i].advance > p_clip_r) {
1882
return;
1883
}
1884
} else {
1885
if (ofs.y + glyphs[i].advance > p_clip_r) {
1886
return;
1887
}
1888
}
1889
}
1890
if (p_clip_l > 0) {
1891
// Clip left / top.
1892
if (orientation == ORIENTATION_HORIZONTAL) {
1893
if (ofs.x < p_clip_l) {
1894
ofs.x += glyphs[i].advance;
1895
continue;
1896
}
1897
} else {
1898
if (ofs.y < p_clip_l) {
1899
ofs.y += glyphs[i].advance;
1900
continue;
1901
}
1902
}
1903
}
1904
if (glyphs[i].font_rid != RID()) {
1905
font_draw_glyph_outline(glyphs[i].font_rid, p_canvas, glyphs[i].font_size, p_outline_size, ofs + p_pos + Vector2(glyphs[i].x_off, glyphs[i].y_off), glyphs[i].index, p_color, p_oversampling);
1906
}
1907
if (orientation == ORIENTATION_HORIZONTAL) {
1908
ofs.x += glyphs[i].advance;
1909
} else {
1910
ofs.y += glyphs[i].advance;
1911
}
1912
}
1913
}
1914
// Draw LTR ellipsis string when needed.
1915
if (!rtl && ellipsis_pos >= 0) {
1916
for (int i = 0; i < ellipsis_gl_size; i++) {
1917
for (int j = 0; j < ellipsis_glyphs[i].repeat; j++) {
1918
font_draw_glyph_outline(ellipsis_glyphs[i].font_rid, p_canvas, ellipsis_glyphs[i].font_size, p_outline_size, ofs + p_pos + Vector2(ellipsis_glyphs[i].x_off, ellipsis_glyphs[i].y_off), ellipsis_glyphs[i].index, p_color, p_oversampling);
1919
if (orientation == ORIENTATION_HORIZONTAL) {
1920
ofs.x += ellipsis_glyphs[i].advance;
1921
} else {
1922
ofs.y += ellipsis_glyphs[i].advance;
1923
}
1924
}
1925
}
1926
}
1927
}
1928
1929
#ifdef DEBUG_ENABLED
1930
1931
void TextServer::debug_print_glyph(int p_idx, const Glyph &p_glyph) const {
1932
String flags;
1933
if (p_glyph.flags & GRAPHEME_IS_VALID) {
1934
flags += "v";
1935
}
1936
if (p_glyph.flags & GRAPHEME_IS_RTL) {
1937
flags += "R";
1938
}
1939
if (p_glyph.flags & GRAPHEME_IS_VIRTUAL) {
1940
flags += "V";
1941
}
1942
if (p_glyph.flags & GRAPHEME_IS_SPACE) {
1943
flags += "w";
1944
}
1945
if (p_glyph.flags & GRAPHEME_IS_BREAK_HARD) {
1946
flags += "h";
1947
}
1948
if (p_glyph.flags & GRAPHEME_IS_BREAK_SOFT) {
1949
flags += "s";
1950
}
1951
if (p_glyph.flags & GRAPHEME_IS_TAB) {
1952
flags += "t";
1953
}
1954
if (p_glyph.flags & GRAPHEME_IS_ELONGATION) {
1955
flags += "e";
1956
}
1957
if (p_glyph.flags & GRAPHEME_IS_PUNCTUATION) {
1958
flags += "p";
1959
}
1960
if (p_glyph.flags & GRAPHEME_IS_UNDERSCORE) {
1961
flags += "u";
1962
}
1963
if (p_glyph.flags & GRAPHEME_IS_CONNECTED) {
1964
flags += "C";
1965
}
1966
if (p_glyph.flags & GRAPHEME_IS_SAFE_TO_INSERT_TATWEEL) {
1967
flags += "S";
1968
}
1969
if (p_glyph.flags & GRAPHEME_IS_EMBEDDED_OBJECT) {
1970
flags += "E";
1971
}
1972
if (p_glyph.flags & GRAPHEME_IS_SOFT_HYPHEN) {
1973
flags += "h";
1974
}
1975
print_line(vformat(" %d => range: %d-%d cnt:%d index:%x font:%x(%d) offset:%fx%f adv:%f rep:%d flags:%s", p_idx, p_glyph.start, p_glyph.end, p_glyph.count, p_glyph.index, p_glyph.font_rid.get_id(), p_glyph.font_size, p_glyph.x_off, p_glyph.y_off, p_glyph.advance, p_glyph.repeat, flags));
1976
}
1977
1978
void TextServer::shaped_text_debug_print(const RID &p_shaped) const {
1979
int ellipsis_pos = shaped_text_get_ellipsis_pos(p_shaped);
1980
int trim_pos = shaped_text_get_trim_pos(p_shaped);
1981
const Vector2i &range = shaped_text_get_range(p_shaped);
1982
int v_size = shaped_text_get_glyph_count(p_shaped);
1983
const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
1984
1985
print_line(vformat("%x: range: %d-%d glyps: %d trim: %d ellipsis: %d", p_shaped.get_id(), range.x, range.y, v_size, trim_pos, ellipsis_pos));
1986
1987
for (int i = 0; i < v_size; i++) {
1988
debug_print_glyph(i, glyphs[i]);
1989
}
1990
}
1991
1992
#endif // DEBUG_ENABLED
1993
1994
void TextServer::_diacritics_map_add(const String &p_from, char32_t p_to) {
1995
for (int i = 0; i < p_from.size(); i++) {
1996
diacritics_map[p_from[i]] = p_to;
1997
}
1998
}
1999
2000
void TextServer::_init_diacritics_map() {
2001
diacritics_map.clear();
2002
2003
// Latin.
2004
_diacritics_map_add(U"ÀÁÂÃÄÅĀĂĄǍǞǠǺȀȂȦḀẠẢẤẦẨẪẬẮẰẲẴẶ", U'A');
2005
_diacritics_map_add(U"àáâãäåāăąǎǟǡǻȁȃȧḁẚạảấầẩẫậắằẳẵặ", U'a');
2006
_diacritics_map_add(U"ǢǼ", U'Æ');
2007
_diacritics_map_add(U"ǣǽ", U'æ');
2008
_diacritics_map_add(U"ḂḄḆ", U'B');
2009
_diacritics_map_add(U"ḃḅḇ", U'b');
2010
_diacritics_map_add(U"ÇĆĈĊČḈ", U'C');
2011
_diacritics_map_add(U"çćĉċčḉ", U'c');
2012
_diacritics_map_add(U"ĎḊḌḎḐḒ", U'D');
2013
_diacritics_map_add(U"ďḋḍḏḑḓ", U'd');
2014
_diacritics_map_add(U"ÈÉÊËĒĔĖĘĚȆȨḔḖḘḚḜẸẺẼẾỀỂỄỆ", U'E');
2015
_diacritics_map_add(U"èéêëēĕėęěȇȩḕḗḙḛḝẹẻẽếềểễệ", U'e');
2016
_diacritics_map_add(U"Ḟ", U'F');
2017
_diacritics_map_add(U"ḟ", U'f');
2018
_diacritics_map_add(U"ĜĞĠĢǦǴḠ", U'G');
2019
_diacritics_map_add(U"ĝğġģǧǵḡ", U'g');
2020
_diacritics_map_add(U"ĤȞḢḤḦḨḪ", U'H');
2021
_diacritics_map_add(U"ĥȟḣḥḧḩḫẖ", U'h');
2022
_diacritics_map_add(U"ÌÍÎÏĨĪĬĮİǏȈȊḬḮỈỊ", U'I');
2023
_diacritics_map_add(U"ìíîïĩīĭįıǐȉȋḭḯỉị", U'i');
2024
_diacritics_map_add(U"Ĵ", U'J');
2025
_diacritics_map_add(U"ĵ", U'j');
2026
_diacritics_map_add(U"ĶǨḰḲḴ", U'K');
2027
_diacritics_map_add(U"ķĸǩḱḳḵ", U'k');
2028
_diacritics_map_add(U"ĹĻĽĿḶḸḺḼ", U'L');
2029
_diacritics_map_add(U"ĺļľŀḷḹḻḽ", U'l');
2030
_diacritics_map_add(U"ḾṀṂ", U'M');
2031
_diacritics_map_add(U"ḿṁṃ", U'm');
2032
_diacritics_map_add(U"ÑŃŅŇǸṄṆṈṊ", U'N');
2033
_diacritics_map_add(U"ñńņňʼnǹṅṇṉṋ", U'n');
2034
_diacritics_map_add(U"ÒÓÔÕÖŌŎŐƠǑǪǬȌȎȪȬȮȰṌṎṐṒỌỎỐỒỔỖỘỚỜỞỠỢ", U'O');
2035
_diacritics_map_add(U"òóôõöōŏőơǒǫǭȍȏȫȭȯȱṍṏṑṓọỏốồổỗộớờởỡợ", U'o');
2036
_diacritics_map_add(U"ṔṖ", U'P');
2037
_diacritics_map_add(U"ṗṕ", U'p');
2038
_diacritics_map_add(U"ŔŖŘȐȒṘṚṜṞ", U'R');
2039
_diacritics_map_add(U"ŕŗřȑȓṙṛṝṟ", U'r');
2040
_diacritics_map_add(U"ŚŜŞŠȘṠṢṤṦṨ", U'S');
2041
_diacritics_map_add(U"śŝşšſșṡṣṥṧṩẛẜẝ", U's');
2042
_diacritics_map_add(U"ŢŤȚṪṬṮṰ", U'T');
2043
_diacritics_map_add(U"ţťțṫṭṯṱẗ", U't');
2044
_diacritics_map_add(U"ÙÚÛÜŨŪŬŮŰŲƯǓǕǗǙǛȔȖṲṴṶṸṺỤỦỨỪỬỮỰ", U'U');
2045
_diacritics_map_add(U"ùúûüũūŭůűųưǔǖǘǚǜȕȗṳṵṷṹṻụủứừửữự", U'u');
2046
_diacritics_map_add(U"ṼṾ", U'V');
2047
_diacritics_map_add(U"ṽṿ", U'v');
2048
_diacritics_map_add(U"ŴẀẂẄẆẈ", U'W');
2049
_diacritics_map_add(U"ŵẁẃẅẇẉẘ", U'w');
2050
_diacritics_map_add(U"ẊẌ", U'X');
2051
_diacritics_map_add(U"ẋẍ", U'x');
2052
_diacritics_map_add(U"ÝŶẎỲỴỶỸỾ", U'Y');
2053
_diacritics_map_add(U"ýÿŷẏẙỳỵỷỹỿ", U'y');
2054
_diacritics_map_add(U"ŹŻŽẐẒẔ", U'Z');
2055
_diacritics_map_add(U"źżžẑẓẕ", U'z');
2056
2057
// Greek.
2058
_diacritics_map_add(U"ΆἈἉἊἋἌἍἎἏᾈᾉᾊᾋᾌᾍᾎᾏᾸᾹᾺΆᾼ", U'Α');
2059
_diacritics_map_add(U"άἀἁἂἃἄἅἆἇὰάᾀᾁᾂᾃᾄᾅᾆᾇᾰᾱᾲᾳᾴᾶᾷ", U'α');
2060
_diacritics_map_add(U"ΈἘἙἚἛἜἝῈΈ", U'Ε');
2061
_diacritics_map_add(U"έἐἑἒἓἔἕὲέ", U'ε');
2062
_diacritics_map_add(U"ΉἨἩἪἫἬἭἮἯᾘᾙᾚᾛᾜᾝᾞᾟῊΉῌ", U'Η');
2063
_diacritics_map_add(U"ήἠἡἢἣἤἥἦἧὴήᾐᾑᾒᾓᾔᾕᾖᾗῂῃῄῆῇ", U'η');
2064
_diacritics_map_add(U"ΊΪἸἹἺἻἼἽἾἿῘῙῚΊ", U'Ι');
2065
_diacritics_map_add(U"ίΐϊἰἱἲἳἴἵἶἷὶίῐῑῒΐῖῗ", U'ι');
2066
_diacritics_map_add(U"ΌὈὉὊὋὌὍῸΌ", U'Ο');
2067
_diacritics_map_add(U"όὀὁὂὃὄὅὸό", U'ο');
2068
_diacritics_map_add(U"Ῥ", U'Ρ');
2069
_diacritics_map_add(U"ῤῥ", U'ρ');
2070
_diacritics_map_add(U"ΎΫϓϔὙὛὝὟῨῩῪΎ", U'Υ');
2071
_diacritics_map_add(U"ΰϋύὐὑὒὓὔὕὖὗὺύῠῡῢΰῦῧ", U'υ');
2072
_diacritics_map_add(U"ΏὨὩὪὫὬὭὮὯᾨᾩᾪᾫᾬᾭᾮᾯῺΏῼ", U'Ω');
2073
_diacritics_map_add(U"ώὠὡὢὣὤὥὦὧὼώᾠᾡᾢᾣᾤᾥᾦᾧῲῳῴῶῷ", U'ω');
2074
2075
// Cyrillic.
2076
_diacritics_map_add(U"ӐӒ", U'А');
2077
_diacritics_map_add(U"ӑӓ", U'а');
2078
_diacritics_map_add(U"ЀЁӖ", U'Е');
2079
_diacritics_map_add(U"ѐёӗ", U'е');
2080
_diacritics_map_add(U"Ӛ", U'Ә');
2081
_diacritics_map_add(U"ӛ", U'ә');
2082
_diacritics_map_add(U"Ӝ", U'Ж');
2083
_diacritics_map_add(U"ӝ", U'ж');
2084
_diacritics_map_add(U"Ӟ", U'З');
2085
_diacritics_map_add(U"ӟ", U'з');
2086
_diacritics_map_add(U"Ѓ", U'Г');
2087
_diacritics_map_add(U"ѓ", U'г');
2088
_diacritics_map_add(U"Ї", U'І');
2089
_diacritics_map_add(U"ї", U'і');
2090
_diacritics_map_add(U"ЍӢӤЙ", U'И');
2091
_diacritics_map_add(U"ѝӣӥй", U'и');
2092
_diacritics_map_add(U"Ќ", U'К');
2093
_diacritics_map_add(U"ќ", U'к');
2094
_diacritics_map_add(U"Ӧ", U'О');
2095
_diacritics_map_add(U"ӧ", U'о');
2096
_diacritics_map_add(U"Ӫ", U'Ө');
2097
_diacritics_map_add(U"ӫ", U'ө');
2098
_diacritics_map_add(U"Ӭ", U'Э');
2099
_diacritics_map_add(U"ӭ", U'э');
2100
_diacritics_map_add(U"ЎӮӰӲ", U'У');
2101
_diacritics_map_add(U"ўӯӱӳ", U'у');
2102
_diacritics_map_add(U"Ӵ", U'Ч');
2103
_diacritics_map_add(U"ӵ", U'ч');
2104
_diacritics_map_add(U"Ӹ", U'Ы');
2105
_diacritics_map_add(U"ӹ", U'ы');
2106
}
2107
2108
String TextServer::strip_diacritics(const String &p_string) const {
2109
String result;
2110
for (int i = 0; i < p_string.length(); i++) {
2111
if (p_string[i] < 0x02B0 || p_string[i] > 0x036F) { // Skip combining diacritics.
2112
if (diacritics_map.has(p_string[i])) {
2113
result += diacritics_map[p_string[i]];
2114
} else {
2115
result += p_string[i];
2116
}
2117
}
2118
}
2119
return result;
2120
}
2121
2122
#ifndef DISABLE_DEPRECATED
2123
String TextServer::format_number(const String &p_string, const String &p_language) const {
2124
const StringName lang = p_language.is_empty() ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
2125
return TranslationServer::get_singleton()->format_number(p_string, lang);
2126
}
2127
2128
String TextServer::parse_number(const String &p_string, const String &p_language) const {
2129
const StringName lang = p_language.is_empty() ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
2130
return TranslationServer::get_singleton()->parse_number(p_string, lang);
2131
}
2132
2133
String TextServer::percent_sign(const String &p_language) const {
2134
const StringName lang = p_language.is_empty() ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
2135
return TranslationServer::get_singleton()->get_percent_sign(lang);
2136
}
2137
#endif // DISABLE_DEPRECATED
2138
2139
TypedArray<Vector3i> TextServer::parse_structured_text(StructuredTextParser p_parser_type, const Array &p_args, const String &p_text) const {
2140
TypedArray<Vector3i> ret;
2141
switch (p_parser_type) {
2142
case STRUCTURED_TEXT_URI: {
2143
int prev = 0;
2144
for (int i = 0; i < p_text.length(); i++) {
2145
if ((p_text[i] == '\\') || (p_text[i] == '/') || (p_text[i] == '.') || (p_text[i] == ':') || (p_text[i] == '&') || (p_text[i] == '=') || (p_text[i] == '@') || (p_text[i] == '?') || (p_text[i] == '#')) {
2146
if (prev != i) {
2147
ret.push_back(Vector3i(prev, i, TextServer::DIRECTION_AUTO));
2148
}
2149
ret.push_back(Vector3i(i, i + 1, TextServer::DIRECTION_LTR));
2150
prev = i + 1;
2151
}
2152
}
2153
if (prev != p_text.length()) {
2154
ret.push_back(Vector3i(prev, p_text.length(), TextServer::DIRECTION_AUTO));
2155
}
2156
} break;
2157
case STRUCTURED_TEXT_FILE: {
2158
int prev = 0;
2159
for (int i = 0; i < p_text.length(); i++) {
2160
if ((p_text[i] == '\\') || (p_text[i] == '/') || (p_text[i] == ':')) {
2161
if (prev != i) {
2162
ret.push_back(Vector3i(prev, i, TextServer::DIRECTION_AUTO));
2163
}
2164
ret.push_back(Vector3i(i, i + 1, TextServer::DIRECTION_LTR));
2165
prev = i + 1;
2166
}
2167
}
2168
if (prev != p_text.length()) {
2169
ret.push_back(Vector3i(prev, p_text.length(), TextServer::DIRECTION_AUTO));
2170
}
2171
} break;
2172
case STRUCTURED_TEXT_EMAIL: {
2173
bool local = true;
2174
int prev = 0;
2175
for (int i = 0; i < p_text.length(); i++) {
2176
if ((p_text[i] == '@') && local) { // Add full "local" as single context.
2177
local = false;
2178
ret.push_back(Vector3i(prev, i, TextServer::DIRECTION_AUTO));
2179
ret.push_back(Vector3i(i, i + 1, TextServer::DIRECTION_LTR));
2180
prev = i + 1;
2181
} else if (!local && (p_text[i] == '.')) { // Add each dot separated "domain" part as context.
2182
if (prev != i) {
2183
ret.push_back(Vector3i(prev, i, TextServer::DIRECTION_AUTO));
2184
}
2185
ret.push_back(Vector3i(i, i + 1, TextServer::DIRECTION_LTR));
2186
prev = i + 1;
2187
}
2188
}
2189
if (prev != p_text.length()) {
2190
ret.push_back(Vector3i(prev, p_text.length(), TextServer::DIRECTION_AUTO));
2191
}
2192
} break;
2193
case STRUCTURED_TEXT_LIST: {
2194
if (p_args.size() == 1 && p_args[0].is_string()) {
2195
Vector<String> tags = p_text.split(String(p_args[0]));
2196
int prev = 0;
2197
for (int i = 0; i < tags.size(); i++) {
2198
if (prev != i) {
2199
ret.push_back(Vector3i(prev, prev + tags[i].length(), TextServer::DIRECTION_INHERITED));
2200
}
2201
ret.push_back(Vector3i(prev + tags[i].length(), prev + tags[i].length() + 1, TextServer::DIRECTION_INHERITED));
2202
prev = prev + tags[i].length() + 1;
2203
}
2204
}
2205
} break;
2206
case STRUCTURED_TEXT_GDSCRIPT: {
2207
bool in_string_literal = false;
2208
bool in_string_literal_single = false;
2209
bool in_id = false;
2210
2211
int prev = 0;
2212
for (int i = 0; i < p_text.length(); i++) {
2213
char32_t c = p_text[i];
2214
if (in_string_literal) {
2215
if (c == '\\') {
2216
i++;
2217
continue; // Skip escaped chars.
2218
} else if (c == '\"') {
2219
// String literal end, push string and ".
2220
if (prev != i) {
2221
ret.push_back(Vector3i(prev, i, TextServer::DIRECTION_AUTO));
2222
}
2223
prev = i + 1;
2224
ret.push_back(Vector3i(i, i + 1, TextServer::DIRECTION_LTR));
2225
in_string_literal = false;
2226
}
2227
} else if (in_string_literal_single) {
2228
if (c == '\\') {
2229
i++;
2230
continue; // Skip escaped chars.
2231
} else if (c == '\'') {
2232
// String literal end, push string and '.
2233
if (prev != i) {
2234
ret.push_back(Vector3i(prev, i, TextServer::DIRECTION_AUTO));
2235
}
2236
prev = i + 1;
2237
ret.push_back(Vector3i(i, i + 1, TextServer::DIRECTION_LTR));
2238
in_string_literal_single = false;
2239
}
2240
} else if (in_id) {
2241
if (!is_unicode_identifier_continue(c)) {
2242
// End of id, push id.
2243
if (prev != i) {
2244
ret.push_back(Vector3i(prev, i, TextServer::DIRECTION_AUTO));
2245
}
2246
prev = i;
2247
in_id = false;
2248
}
2249
} else if (is_unicode_identifier_start(c)) {
2250
// Start of new id, push prev element.
2251
if (prev != i) {
2252
ret.push_back(Vector3i(prev, i, TextServer::DIRECTION_AUTO));
2253
}
2254
prev = i;
2255
in_id = true;
2256
} else if (c == '\"') {
2257
// String literal start, push prev element and ".
2258
if (prev != i) {
2259
ret.push_back(Vector3i(prev, i, TextServer::DIRECTION_AUTO));
2260
}
2261
prev = i + 1;
2262
ret.push_back(Vector3i(i, i + 1, TextServer::DIRECTION_LTR));
2263
in_string_literal = true;
2264
} else if (c == '\'') {
2265
// String literal start, push prev element and '.
2266
if (prev != i) {
2267
ret.push_back(Vector3i(prev, i, TextServer::DIRECTION_AUTO));
2268
}
2269
prev = i + 1;
2270
ret.push_back(Vector3i(i, i + 1, TextServer::DIRECTION_LTR));
2271
in_string_literal_single = true;
2272
} else if (c == '#') {
2273
// Start of comment, push prev element and #, skip the rest of the text.
2274
if (prev != i) {
2275
ret.push_back(Vector3i(prev, i, TextServer::DIRECTION_AUTO));
2276
}
2277
prev = p_text.length();
2278
ret.push_back(Vector3i(i, p_text.length(), TextServer::DIRECTION_AUTO));
2279
break;
2280
}
2281
}
2282
if (prev < p_text.length()) {
2283
ret.push_back(Vector3i(prev, p_text.length(), TextServer::DIRECTION_AUTO));
2284
}
2285
} break;
2286
case STRUCTURED_TEXT_CUSTOM:
2287
case STRUCTURED_TEXT_DEFAULT:
2288
default: {
2289
ret.push_back(Vector3i(0, p_text.length(), TextServer::DIRECTION_INHERITED));
2290
}
2291
}
2292
return ret;
2293
}
2294
2295
TypedArray<Dictionary> TextServer::_shaped_text_get_glyphs_wrapper(const RID &p_shaped) const {
2296
TypedArray<Dictionary> ret;
2297
2298
const Glyph *glyphs = shaped_text_get_glyphs(p_shaped);
2299
int gl_size = shaped_text_get_glyph_count(p_shaped);
2300
for (int i = 0; i < gl_size; i++) {
2301
Dictionary glyph;
2302
2303
glyph["start"] = glyphs[i].start;
2304
glyph["end"] = glyphs[i].end;
2305
glyph["repeat"] = glyphs[i].repeat;
2306
glyph["count"] = glyphs[i].count;
2307
glyph["flags"] = glyphs[i].flags;
2308
glyph["offset"] = Vector2(glyphs[i].x_off, glyphs[i].y_off);
2309
glyph["advance"] = glyphs[i].advance;
2310
glyph["font_rid"] = glyphs[i].font_rid;
2311
glyph["font_size"] = glyphs[i].font_size;
2312
glyph["index"] = glyphs[i].index;
2313
glyph["span_index"] = glyphs[i].span_index;
2314
2315
ret.push_back(glyph);
2316
}
2317
2318
return ret;
2319
}
2320
2321
TypedArray<Dictionary> TextServer::_shaped_text_sort_logical_wrapper(const RID &p_shaped) {
2322
Array ret;
2323
2324
const Glyph *glyphs = shaped_text_sort_logical(p_shaped);
2325
int gl_size = shaped_text_get_glyph_count(p_shaped);
2326
for (int i = 0; i < gl_size; i++) {
2327
Dictionary glyph;
2328
2329
glyph["start"] = glyphs[i].start;
2330
glyph["end"] = glyphs[i].end;
2331
glyph["repeat"] = glyphs[i].repeat;
2332
glyph["count"] = glyphs[i].count;
2333
glyph["flags"] = glyphs[i].flags;
2334
glyph["offset"] = Vector2(glyphs[i].x_off, glyphs[i].y_off);
2335
glyph["advance"] = glyphs[i].advance;
2336
glyph["font_rid"] = glyphs[i].font_rid;
2337
glyph["font_size"] = glyphs[i].font_size;
2338
glyph["index"] = glyphs[i].index;
2339
glyph["span_index"] = glyphs[i].span_index;
2340
2341
ret.push_back(glyph);
2342
}
2343
2344
return ret;
2345
}
2346
2347
TypedArray<Dictionary> TextServer::_shaped_text_get_ellipsis_glyphs_wrapper(const RID &p_shaped) const {
2348
TypedArray<Dictionary> ret;
2349
2350
const Glyph *glyphs = shaped_text_get_ellipsis_glyphs(p_shaped);
2351
int gl_size = shaped_text_get_ellipsis_glyph_count(p_shaped);
2352
for (int i = 0; i < gl_size; i++) {
2353
Dictionary glyph;
2354
2355
glyph["start"] = glyphs[i].start;
2356
glyph["end"] = glyphs[i].end;
2357
glyph["repeat"] = glyphs[i].repeat;
2358
glyph["count"] = glyphs[i].count;
2359
glyph["flags"] = glyphs[i].flags;
2360
glyph["offset"] = Vector2(glyphs[i].x_off, glyphs[i].y_off);
2361
glyph["advance"] = glyphs[i].advance;
2362
glyph["font_rid"] = glyphs[i].font_rid;
2363
glyph["font_size"] = glyphs[i].font_size;
2364
glyph["index"] = glyphs[i].index;
2365
glyph["span_index"] = glyphs[i].span_index;
2366
2367
ret.push_back(glyph);
2368
}
2369
2370
return ret;
2371
}
2372
2373
bool TextServer::is_valid_identifier(const String &p_string) const {
2374
return p_string.is_valid_unicode_identifier();
2375
}
2376
2377
bool TextServer::is_valid_letter(uint64_t p_unicode) const {
2378
return is_unicode_letter(p_unicode);
2379
}
2380
2381
TextServer::TextServer() {
2382
// Default font rendering related project settings.
2383
2384
GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "gui/theme/default_font_antialiasing", PROPERTY_HINT_ENUM, "None,Grayscale,LCD Subpixel", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED), 1);
2385
GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "gui/theme/default_font_hinting", PROPERTY_HINT_ENUM, "None,Light,Normal", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED), TextServer::HINTING_LIGHT);
2386
GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "gui/theme/default_font_subpixel_positioning", PROPERTY_HINT_ENUM, "Disabled,Auto,One Half of a Pixel,One Quarter of a Pixel", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_RESTART_IF_CHANGED), TextServer::SUBPIXEL_POSITIONING_AUTO);
2387
2388
GLOBAL_DEF_RST("gui/theme/default_font_multichannel_signed_distance_field", false);
2389
GLOBAL_DEF_RST("gui/theme/default_font_generate_mipmaps", false);
2390
2391
GLOBAL_DEF(PropertyInfo(Variant::INT, "gui/theme/lcd_subpixel_layout", PROPERTY_HINT_ENUM, "Disabled,Horizontal RGB,Horizontal BGR,Vertical RGB,Vertical BGR"), 1);
2392
GLOBAL_DEF_BASIC("internationalization/locale/include_text_server_data", false);
2393
GLOBAL_DEF_BASIC(PropertyInfo(Variant::INT, "internationalization/locale/line_breaking_strictness", PROPERTY_HINT_ENUM, "Auto,Loose,Normal,Strict"), 0);
2394
2395
_init_diacritics_map();
2396
}
2397
2398
TextServer::~TextServer() {
2399
}
2400
2401
BitField<TextServer::TextOverrunFlag> TextServer::get_overrun_flags_from_behavior(TextServer::OverrunBehavior p_behavior) {
2402
BitField<TextOverrunFlag> overrun_flags = OVERRUN_NO_TRIM;
2403
switch (p_behavior) {
2404
case OVERRUN_TRIM_WORD_ELLIPSIS_FORCE: {
2405
overrun_flags.set_flag(OVERRUN_TRIM);
2406
overrun_flags.set_flag(OVERRUN_TRIM_WORD_ONLY);
2407
overrun_flags.set_flag(OVERRUN_ADD_ELLIPSIS);
2408
overrun_flags.set_flag(OVERRUN_SHORT_STRING_ELLIPSIS);
2409
} break;
2410
case OVERRUN_TRIM_ELLIPSIS_FORCE: {
2411
overrun_flags.set_flag(OVERRUN_TRIM);
2412
overrun_flags.set_flag(OVERRUN_ADD_ELLIPSIS);
2413
overrun_flags.set_flag(OVERRUN_SHORT_STRING_ELLIPSIS);
2414
} break;
2415
case OVERRUN_TRIM_WORD_ELLIPSIS:
2416
overrun_flags.set_flag(OVERRUN_TRIM);
2417
overrun_flags.set_flag(OVERRUN_TRIM_WORD_ONLY);
2418
overrun_flags.set_flag(OVERRUN_ADD_ELLIPSIS);
2419
break;
2420
case OVERRUN_TRIM_ELLIPSIS:
2421
overrun_flags.set_flag(OVERRUN_TRIM);
2422
overrun_flags.set_flag(OVERRUN_ADD_ELLIPSIS);
2423
break;
2424
case OVERRUN_TRIM_WORD:
2425
overrun_flags.set_flag(OVERRUN_TRIM);
2426
overrun_flags.set_flag(OVERRUN_TRIM_WORD_ONLY);
2427
break;
2428
case OVERRUN_TRIM_CHAR:
2429
overrun_flags.set_flag(OVERRUN_TRIM);
2430
break;
2431
case OVERRUN_NO_TRIMMING:
2432
break;
2433
}
2434
return overrun_flags;
2435
}
2436
2437