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