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