Debian Codegraph @

modules/text_server_fb/text_server_fb.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* text_server_fb.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "text_server_fb.h"
32
33 #ifdef GDEXTENSION
34 // Headers for building as GDExtension plug-in.
35
36 #include <godot_cpp/classes/file_access.hpp>
37 #include <godot_cpp/classes/os.hpp>
38 #include <godot_cpp/classes/project_settings.hpp>
39 #include <godot_cpp/classes/rendering_server.hpp>
40 #include <godot_cpp/classes/translation_server.hpp>
41 #include <godot_cpp/core/error_macros.hpp>
42
43 #define OT_TAG(m_c1, m_c2, m_c3, m_c4) ((int32_t)((((uint32_t)(m_c1) & 0xff) << 24) | (((uint32_t)(m_c2) & 0xff) << 16) | (((uint32_t)(m_c3) & 0xff) << 8) | ((uint32_t)(m_c4) & 0xff)))
44
45 using namespace godot;
46
47 #define GLOBAL_GET(m_var) ProjectSettings::get_singleton()->get_setting_with_override(m_var)
48
49 #elif defined(GODOT_MODULE)
50 // Headers for building as built-in module.
51
52 #include "core/config/project_settings.h"
53 #include "core/error/error_macros.h"
54 #include "core/io/file_access.h"
55 #include "core/string/print_string.h"
56 #include "core/string/translation_server.h"
57 #include "servers/rendering/rendering_server.h"
58
59 #include "modules/modules_enabled.gen.h" // For freetype, msdfgen, svg.
60
61 #endif
62
63 // Thirdparty headers.
64
65 #ifdef MODULE_MSDFGEN_ENABLED
66 GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Wshadow")
67 GODOT_MSVC_WARNING_PUSH_AND_IGNORE(4458) // "Declaration of 'identifier' hides class member".
68
69 #include <core/EdgeHolder.h>
70 #include <core/ShapeDistanceFinder.h>
71 #include <core/contour-combiners.h>
72 #include <core/edge-selectors.h>
73 #include <msdfgen.h>
74
75 GODOT_GCC_WARNING_POP
76 GODOT_MSVC_WARNING_POP
77 #endif
78
79 #ifdef MODULE_FREETYPE_ENABLED
80 #include FT_SFNT_NAMES_H
81 #include FT_TRUETYPE_IDS_H
82 #ifdef MODULE_SVG_ENABLED
83 #include "thorvg_svg_in_ot.h"
84 #endif
85 #endif
86
87 /*************************************************************************/
88
89 bool TextServerFallback::_has_feature(Feature p_feature) const {
90 switch (p_feature) {
91 case FEATURE_SIMPLE_LAYOUT:
92 case FEATURE_FONT_BITMAP:
93 #ifdef MODULE_FREETYPE_ENABLED
94 case FEATURE_FONT_DYNAMIC:
95 #endif
96 #ifdef MODULE_MSDFGEN_ENABLED
97 case FEATURE_FONT_MSDF:
98 #endif
99 return true;
100 default: {
101 }
102 }
103 return false;
104 }
105
106 String TextServerFallback::_get_name() const {
107 #ifdef GDEXTENSION
108 return "Fallback (GDExtension)";
109 #elif defined(GODOT_MODULE)
110 return "Fallback (Built-in)";
111 #endif
112 }
113
114 int64_t TextServerFallback::_get_features() const {
115 int64_t interface_features = FEATURE_SIMPLE_LAYOUT | FEATURE_FONT_BITMAP;
116 #ifdef MODULE_FREETYPE_ENABLED
117 interface_features |= FEATURE_FONT_DYNAMIC;
118 #endif
119 #ifdef MODULE_MSDFGEN_ENABLED
120 interface_features |= FEATURE_FONT_MSDF;
121 #endif
122
123 return interface_features;
124 }
125
126 void TextServerFallback::_free_rid(const RID &p_rid) {
127 _THREAD_SAFE_METHOD_
128 if (font_owner.owns(p_rid)) {
129 MutexLock ftlock(ft_mutex);
130
131 FontFallback *fd = font_owner.get_or_null(p_rid);
132 for (const KeyValue<Vector2i, FontForSizeFallback *> &ffsd : fd->cache) {
133 OversamplingLevel *ol = oversampling_levels.getptr(ffsd.value->viewport_oversampling);
134 if (ol != nullptr) {
135 ol->fonts.erase(ffsd.value);
136 }
137 }
138 {
139 MutexLock lock(fd->mutex);
140 font_owner.free(p_rid);
141 }
142 memdelete(fd);
143 } else if (font_var_owner.owns(p_rid)) {
144 MutexLock ftlock(ft_mutex);
145
146 FontFallbackLinkedVariation *fdv = font_var_owner.get_or_null(p_rid);
147 {
148 font_var_owner.free(p_rid);
149 }
150 memdelete(fdv);
151 } else if (shaped_owner.owns(p_rid)) {
152 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_rid);
153 {
154 MutexLock lock(sd->mutex);
155 shaped_owner.free(p_rid);
156 }
157 memdelete(sd);
158 }
159 }
160
161 bool TextServerFallback::_has(const RID &p_rid) {
162 _THREAD_SAFE_METHOD_
163 return font_owner.owns(p_rid) || shaped_owner.owns(p_rid);
164 }
165
166 String TextServerFallback::_get_support_data_filename() const {
167 return "";
168 }
169
170 String TextServerFallback::_get_support_data_info() const {
171 return "Not supported";
172 }
173
174 bool TextServerFallback::_load_support_data(const String &p_filename) {
175 return false; // No extra data used.
176 }
177
178 bool TextServerFallback::_save_support_data(const String &p_filename) const {
179 return false; // No extra data used.
180 }
181
182 PackedByteArray TextServerFallback::_get_support_data() const {
183 return PackedByteArray(); // No extra data used.
184 }
185
186 bool TextServerFallback::_is_locale_using_support_data(const String &p_locale) const {
187 return false; // No data support.
188 }
189
190 bool TextServerFallback::_is_locale_right_to_left(const String &p_locale) const {
191 return false; // No RTL support.
192 }
193
194 _FORCE_INLINE_ void TextServerFallback::_insert_feature(const StringName &p_name, int32_t p_tag) {
195 feature_sets.insert(p_name, p_tag);
196 feature_sets_inv.insert(p_tag, p_name);
197 }
198
199 void TextServerFallback::_insert_feature_sets() {
200 // Registered OpenType variation tag.
201 _insert_feature("italic", OT_TAG('i', 't', 'a', 'l'));
202 _insert_feature("optical_size", OT_TAG('o', 'p', 's', 'z'));
203 _insert_feature("slant", OT_TAG('s', 'l', 'n', 't'));
204 _insert_feature("width", OT_TAG('w', 'd', 't', 'h'));
205 _insert_feature("weight", OT_TAG('w', 'g', 'h', 't'));
206 }
207
208 _FORCE_INLINE_ int32_t ot_tag_from_string(const char *p_str, int p_len) {
209 char tag[4];
210 uint32_t i;
211
212 if (!p_str || !p_len || !*p_str) {
213 return OT_TAG(0, 0, 0, 0);
214 }
215
216 if (p_len < 0 || p_len > 4) {
217 p_len = 4;
218 }
219 for (i = 0; i < (uint32_t)p_len && p_str[i]; i++) {
220 tag[i] = p_str[i];
221 }
222
223 for (; i < 4; i++) {
224 tag[i] = ' ';
225 }
226
227 return OT_TAG(tag[0], tag[1], tag[2], tag[3]);
228 }
229
230 int64_t TextServerFallback::_name_to_tag(const String &p_name) const {
231 if (feature_sets.has(p_name)) {
232 return feature_sets[p_name];
233 }
234
235 // No readable name, use tag string.
236 return ot_tag_from_string(p_name.replace("custom_", "").ascii().get_data(), -1);
237 }
238
239 _FORCE_INLINE_ void ot_tag_to_string(int32_t p_tag, char *p_buf) {
240 p_buf[0] = (char)(uint8_t)(p_tag >> 24);
241 p_buf[1] = (char)(uint8_t)(p_tag >> 16);
242 p_buf[2] = (char)(uint8_t)(p_tag >> 8);
243 p_buf[3] = (char)(uint8_t)(p_tag >> 0);
244 }
245
246 String TextServerFallback::_tag_to_name(int64_t p_tag) const {
247 if (feature_sets_inv.has(p_tag)) {
248 return feature_sets_inv[p_tag];
249 }
250
251 // No readable name, use tag string.
252 char name[5];
253 memset(name, 0, 5);
254 ot_tag_to_string(p_tag, name);
255 return String("custom_") + String(name);
256 }
257
258 /*************************************************************************/
259 /* Font Glyph Rendering */
260 /*************************************************************************/
261
262 _FORCE_INLINE_ TextServerFallback::FontTexturePosition TextServerFallback::find_texture_pos_for_glyph(FontForSizeFallback *p_data, int p_color_size, Image::Format p_image_format, int p_width, int p_height, bool p_msdf) const {
263 FontTexturePosition ret;
264
265 int mw = p_width;
266 int mh = p_height;
267
268 ShelfPackTexture *ct = p_data->textures.ptrw();
269 for (int32_t i = 0; i < p_data->textures.size(); i++) {
270 if (ct[i].image.is_null()) {
271 continue;
272 }
273 if (p_image_format != ct[i].image->get_format()) {
274 continue;
275 }
276 if (mw > ct[i].texture_w || mh > ct[i].texture_h) { // Too big for this texture.
277 continue;
278 }
279
280 ret = ct[i].pack_rect(i, mh, mw);
281 if (ret.index != -1) {
282 break;
283 }
284 }
285
286 if (ret.index == -1) {
287 // Could not find texture to fit, create one.
288 int texsize = MAX(p_data->size.x * 0.125, 256);
289
290 texsize = next_power_of_2((uint32_t)texsize);
291
292 if (p_msdf) {
293 texsize = MIN(texsize, 2048);
294 } else {
295 texsize = MIN(texsize, 1024);
296 }
297 if (mw > texsize) { // Special case, adapt to it?
298 texsize = next_power_of_2((uint32_t)mw);
299 }
300 if (mh > texsize) { // Special case, adapt to it?
301 texsize = next_power_of_2((uint32_t)mh);
302 }
303
304 ShelfPackTexture tex = ShelfPackTexture(texsize, texsize);
305 tex.image = Image::create_empty(texsize, texsize, false, p_image_format);
306 {
307 // Zero texture.
308 uint8_t *w = tex.image->ptrw();
309 ERR_FAIL_COND_V(texsize * texsize * p_color_size > tex.image->get_data_size(), ret);
310 // Initialize the texture to all-white pixels to prevent artifacts when the
311 // font is displayed at a non-default scale with filtering enabled.
312 if (p_color_size == 2) {
313 for (int i = 0; i < texsize * texsize * p_color_size; i += 2) { // FORMAT_LA8, BW font.
314 w[i + 0] = 255;
315 w[i + 1] = 0;
316 }
317 } else if (p_color_size == 4) {
318 for (int i = 0; i < texsize * texsize * p_color_size; i += 4) { // FORMAT_RGBA8, Color font, Multichannel(+True) SDF.
319 if (p_msdf) {
320 w[i + 0] = 0;
321 w[i + 1] = 0;
322 w[i + 2] = 0;
323 } else {
324 w[i + 0] = 255;
325 w[i + 1] = 255;
326 w[i + 2] = 255;
327 }
328 w[i + 3] = 0;
329 }
330 } else {
331 ERR_FAIL_V(ret);
332 }
333 }
334 p_data->textures.push_back(tex);
335
336 int32_t idx = p_data->textures.size() - 1;
337 ret = p_data->textures.write[idx].pack_rect(idx, mh, mw);
338 }
339
340 return ret;
341 }
342
343 #ifdef MODULE_MSDFGEN_ENABLED
344
345 struct MSContext {
346 msdfgen::Point2 position;
347 msdfgen::Shape *shape = nullptr;
348 msdfgen::Contour *contour = nullptr;
349 };
350
351 class DistancePixelConversion {
352 double invRange;
353
354 public:
355 _FORCE_INLINE_ explicit DistancePixelConversion(double range) :
356 invRange(1 / range) {}
357 _FORCE_INLINE_ void operator()(float *pixels, const msdfgen::MultiAndTrueDistance &distance) const {
358 pixels[0] = float(invRange * distance.r + .5);
359 pixels[1] = float(invRange * distance.g + .5);
360 pixels[2] = float(invRange * distance.b + .5);
361 pixels[3] = float(invRange * distance.a + .5);
362 }
363 };
364
365 struct MSDFThreadData {
366 msdfgen::Bitmap<float, 4> *output;
367 msdfgen::Shape *shape;
368 msdfgen::Projection *projection;
369 DistancePixelConversion *distancePixelConversion;
370 };
371
372 static msdfgen::Point2 ft_point2(const FT_Vector &vector) {
373 return msdfgen::Point2(vector.x / 60.0f, vector.y / 60.0f);
374 }
375
376 static int ft_move_to(const FT_Vector *to, void *user) {
377 MSContext *context = static_cast<MSContext *>(user);
378 if (!(context->contour && context->contour->edges.empty())) {
379 context->contour = &context->shape->addContour();
380 }
381 context->position = ft_point2(*to);
382 return 0;
383 }
384
385 static int ft_line_to(const FT_Vector *to, void *user) {
386 MSContext *context = static_cast<MSContext *>(user);
387 msdfgen::Point2 endpoint = ft_point2(*to);
388 if (endpoint != context->position) {
389 context->contour->addEdge(new msdfgen::LinearSegment(context->position, endpoint));
390 context->position = endpoint;
391 }
392 return 0;
393 }
394
395 static int ft_conic_to(const FT_Vector *control, const FT_Vector *to, void *user) {
396 MSContext *context = static_cast<MSContext *>(user);
397 context->contour->addEdge(new msdfgen::QuadraticSegment(context->position, ft_point2(*control), ft_point2(*to)));
398 context->position = ft_point2(*to);
399 return 0;
400 }
401
402 static int ft_cubic_to(const FT_Vector *control1, const FT_Vector *control2, const FT_Vector *to, void *user) {
403 MSContext *context = static_cast<MSContext *>(user);
404 context->contour->addEdge(new msdfgen::CubicSegment(context->position, ft_point2(*control1), ft_point2(*control2), ft_point2(*to)));
405 context->position = ft_point2(*to);
406 return 0;
407 }
408
409 void TextServerFallback::_generateMTSDF_threaded(void *p_td, uint32_t p_y) {
410 MSDFThreadData *td = static_cast<MSDFThreadData *>(p_td);
411
412 msdfgen::ShapeDistanceFinder<msdfgen::OverlappingContourCombiner<msdfgen::MultiAndTrueDistanceSelector>> distanceFinder(*td->shape);
413 int row = td->shape->inverseYAxis ? td->output->height() - p_y - 1 : p_y;
414 for (int col = 0; col < td->output->width(); ++col) {
415 int x = (p_y % 2) ? td->output->width() - col - 1 : col;
416 msdfgen::Point2 p = td->projection->unproject(msdfgen::Point2(x + .5, p_y + .5));
417 msdfgen::MultiAndTrueDistance distance = distanceFinder.distance(p);
418 td->distancePixelConversion->operator()(td->output->operator()(x, row), distance);
419 }
420 }
421
422 _FORCE_INLINE_ TextServerFallback::FontGlyph TextServerFallback::rasterize_msdf(FontFallback *p_font_data, FontForSizeFallback *p_data, int p_pixel_range, int p_rect_margin, FT_Outline *p_outline, const Vector2 &p_advance) const {
423 msdfgen::Shape shape;
424
425 shape.contours.clear();
426 shape.inverseYAxis = false;
427
428 MSContext context = {};
429 context.shape = &shape;
430 FT_Outline_Funcs ft_functions;
431 ft_functions.move_to = &ft_move_to;
432 ft_functions.line_to = &ft_line_to;
433 ft_functions.conic_to = &ft_conic_to;
434 ft_functions.cubic_to = &ft_cubic_to;
435 ft_functions.shift = 0;
436 ft_functions.delta = 0;
437
438 int error = FT_Outline_Decompose(p_outline, &ft_functions, &context);
439 ERR_FAIL_COND_V_MSG(error, FontGlyph(), "FreeType: Outline decomposition error: '" + String(FT_Error_String(error)) + "'.");
440 if (!shape.contours.empty() && shape.contours.back().edges.empty()) {
441 shape.contours.pop_back();
442 }
443
444 if (FT_Outline_Get_Orientation(p_outline) == 1) {
445 for (int i = 0; i < (int)shape.contours.size(); ++i) {
446 shape.contours[i].reverse();
447 }
448 }
449
450 shape.inverseYAxis = true;
451 shape.normalize();
452
453 msdfgen::Shape::Bounds bounds = shape.getBounds(p_pixel_range);
454
455 FontGlyph chr;
456 chr.found = true;
457 chr.advance = p_advance;
458
459 if (shape.validate() && shape.contours.size() > 0) {
460 int w = (bounds.r - bounds.l);
461 int h = (bounds.t - bounds.b);
462
463 if (w == 0 || h == 0) {
464 chr.texture_idx = -1;
465 chr.uv_rect = Rect2();
466 chr.rect = Rect2();
467 return chr;
468 }
469 int mw = w + p_rect_margin * 4;
470 int mh = h + p_rect_margin * 4;
471
472 ERR_FAIL_COND_V(mw > 4096, FontGlyph());
473 ERR_FAIL_COND_V(mh > 4096, FontGlyph());
474
475 FontTexturePosition tex_pos = find_texture_pos_for_glyph(p_data, 4, Image::FORMAT_RGBA8, mw, mh, true);
476 ERR_FAIL_COND_V(tex_pos.index < 0, FontGlyph());
477 ShelfPackTexture &tex = p_data->textures.write[tex_pos.index];
478
479 edgeColoringSimple(shape, 3.0); // Max. angle.
480 msdfgen::Bitmap<float, 4> image(w, h); // Texture size.
481
482 DistancePixelConversion distancePixelConversion(p_pixel_range);
483 msdfgen::Projection projection(msdfgen::Vector2(1.0, 1.0), msdfgen::Vector2(-bounds.l, -bounds.b));
484 msdfgen::MSDFGeneratorConfig config(true, msdfgen::ErrorCorrectionConfig());
485
486 MSDFThreadData td;
487 td.output = &image;
488 td.shape = &shape;
489 td.projection = &projection;
490 td.distancePixelConversion = &distancePixelConversion;
491
492 WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_native_group_task(&TextServerFallback::_generateMTSDF_threaded, &td, h, -1, true, String("TextServerFBRenderMSDF"));
493 WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
494
495 msdfgen::msdfErrorCorrection(image, shape, projection, p_pixel_range, config);
496
497 {
498 uint8_t *wr = tex.image->ptrw();
499
500 for (int i = 0; i < h; i++) {
501 for (int j = 0; j < w; j++) {
502 int ofs = ((i + tex_pos.y + p_rect_margin * 2) * tex.texture_w + j + tex_pos.x + p_rect_margin * 2) * 4;
503 ERR_FAIL_COND_V(ofs >= tex.image->get_data_size(), FontGlyph());
504 wr[ofs + 0] = (uint8_t)(CLAMP(image(j, i)[0] * 256.f, 0.f, 255.f));
505 wr[ofs + 1] = (uint8_t)(CLAMP(image(j, i)[1] * 256.f, 0.f, 255.f));
506 wr[ofs + 2] = (uint8_t)(CLAMP(image(j, i)[2] * 256.f, 0.f, 255.f));
507 wr[ofs + 3] = (uint8_t)(CLAMP(image(j, i)[3] * 256.f, 0.f, 255.f));
508 }
509 }
510 }
511
512 tex.dirty = true;
513
514 chr.texture_idx = tex_pos.index;
515
516 chr.uv_rect = Rect2(tex_pos.x + p_rect_margin, tex_pos.y + p_rect_margin, w + p_rect_margin * 2, h + p_rect_margin * 2);
517 chr.rect.position = Vector2(bounds.l - p_rect_margin, -bounds.t - p_rect_margin);
518 chr.rect.size = chr.uv_rect.size;
519 }
520 return chr;
521 }
522 #endif
523
524 #ifdef MODULE_FREETYPE_ENABLED
525 _FORCE_INLINE_ TextServerFallback::FontGlyph TextServerFallback::rasterize_bitmap(FontForSizeFallback *p_data, int p_rect_margin, FT_Bitmap p_bitmap, int p_yofs, int p_xofs, const Vector2 &p_advance, bool p_bgra) const {
526 FontGlyph chr;
527 chr.advance = p_advance * p_data->scale;
528 chr.found = true;
529
530 int w = p_bitmap.width;
531 int h = p_bitmap.rows;
532
533 if (w == 0 || h == 0) {
534 chr.texture_idx = -1;
535 chr.uv_rect = Rect2();
536 chr.rect = Rect2();
537 return chr;
538 }
539
540 int color_size = 2;
541
542 switch (p_bitmap.pixel_mode) {
543 case FT_PIXEL_MODE_MONO:
544 case FT_PIXEL_MODE_GRAY: {
545 color_size = 2;
546 } break;
547 case FT_PIXEL_MODE_BGRA: {
548 color_size = 4;
549 } break;
550 case FT_PIXEL_MODE_LCD: {
551 color_size = 4;
552 w /= 3;
553 } break;
554 case FT_PIXEL_MODE_LCD_V: {
555 color_size = 4;
556 h /= 3;
557 } break;
558 }
559
560 int mw = w + p_rect_margin * 4;
561 int mh = h + p_rect_margin * 4;
562
563 ERR_FAIL_COND_V(mw > 4096, FontGlyph());
564 ERR_FAIL_COND_V(mh > 4096, FontGlyph());
565
566 Image::Format require_format = color_size == 4 ? Image::FORMAT_RGBA8 : Image::FORMAT_LA8;
567
568 FontTexturePosition tex_pos = find_texture_pos_for_glyph(p_data, color_size, require_format, mw, mh, false);
569 ERR_FAIL_COND_V(tex_pos.index < 0, FontGlyph());
570
571 // Fit character in char texture.
572 ShelfPackTexture &tex = p_data->textures.write[tex_pos.index];
573
574 {
575 uint8_t *wr = tex.image->ptrw();
576
577 for (int i = 0; i < h; i++) {
578 for (int j = 0; j < w; j++) {
579 int ofs = ((i + tex_pos.y + p_rect_margin * 2) * tex.texture_w + j + tex_pos.x + p_rect_margin * 2) * color_size;
580 ERR_FAIL_COND_V(ofs >= tex.image->get_data_size(), FontGlyph());
581 switch (p_bitmap.pixel_mode) {
582 case FT_PIXEL_MODE_MONO: {
583 int byte = i * p_bitmap.pitch + (j >> 3);
584 int bit = 1 << (7 - (j % 8));
585 wr[ofs + 0] = 255; // grayscale as 1
586 wr[ofs + 1] = (p_bitmap.buffer[byte] & bit) ? 255 : 0;
587 } break;
588 case FT_PIXEL_MODE_GRAY:
589 wr[ofs + 0] = 255; // grayscale as 1
590 wr[ofs + 1] = p_bitmap.buffer[i * p_bitmap.pitch + j];
591 break;
592 case FT_PIXEL_MODE_BGRA: {
593 int ofs_color = i * p_bitmap.pitch + (j << 2);
594 wr[ofs + 2] = p_bitmap.buffer[ofs_color + 0];
595 wr[ofs + 1] = p_bitmap.buffer[ofs_color + 1];
596 wr[ofs + 0] = p_bitmap.buffer[ofs_color + 2];
597 wr[ofs + 3] = p_bitmap.buffer[ofs_color + 3];
598 } break;
599 case FT_PIXEL_MODE_LCD: {
600 int ofs_color = i * p_bitmap.pitch + (j * 3);
601 if (p_bgra) {
602 wr[ofs + 0] = p_bitmap.buffer[ofs_color + 2];
603 wr[ofs + 1] = p_bitmap.buffer[ofs_color + 1];
604 wr[ofs + 2] = p_bitmap.buffer[ofs_color + 0];
605 wr[ofs + 3] = 255;
606 } else {
607 wr[ofs + 0] = p_bitmap.buffer[ofs_color + 0];
608 wr[ofs + 1] = p_bitmap.buffer[ofs_color + 1];
609 wr[ofs + 2] = p_bitmap.buffer[ofs_color + 2];
610 wr[ofs + 3] = 255;
611 }
612 } break;
613 case FT_PIXEL_MODE_LCD_V: {
614 int ofs_color = i * p_bitmap.pitch * 3 + j;
615 if (p_bgra) {
616 wr[ofs + 0] = p_bitmap.buffer[ofs_color + p_bitmap.pitch * 2];
617 wr[ofs + 1] = p_bitmap.buffer[ofs_color + p_bitmap.pitch];
618 wr[ofs + 2] = p_bitmap.buffer[ofs_color + 0];
619 wr[ofs + 3] = 255;
620 } else {
621 wr[ofs + 0] = p_bitmap.buffer[ofs_color + 0];
622 wr[ofs + 1] = p_bitmap.buffer[ofs_color + p_bitmap.pitch];
623 wr[ofs + 2] = p_bitmap.buffer[ofs_color + p_bitmap.pitch * 2];
624 wr[ofs + 3] = 255;
625 }
626 } break;
627 default:
628 ERR_FAIL_V_MSG(FontGlyph(), "Font uses unsupported pixel format: " + String::num_int64(p_bitmap.pixel_mode) + ".");
629 break;
630 }
631 }
632 }
633 }
634
635 tex.dirty = true;
636
637 chr.texture_idx = tex_pos.index;
638
639 chr.uv_rect = Rect2(tex_pos.x + p_rect_margin, tex_pos.y + p_rect_margin, w + p_rect_margin * 2, h + p_rect_margin * 2);
640 chr.rect.position = Vector2(p_xofs - p_rect_margin, -p_yofs - p_rect_margin) * p_data->scale;
641 chr.rect.size = chr.uv_rect.size * p_data->scale;
642 return chr;
643 }
644 #endif
645
646 /*************************************************************************/
647 /* Font Cache */
648 /*************************************************************************/
649
650 bool TextServerFallback::_ensure_glyph(FontFallback *p_font_data, const Vector2i &p_size, int32_t p_glyph, FontGlyph &r_glyph, uint32_t p_oversampling) const {
651 FontForSizeFallback *fd = nullptr;
652 ERR_FAIL_COND_V(!_ensure_cache_for_size(p_font_data, p_size, fd, false, p_oversampling), false);
653
654 int32_t glyph_index = p_glyph & 0xffffff; // Remove subpixel shifts.
655
656 HashMap<int32_t, FontGlyph>::Iterator E = fd->glyph_map.find(p_glyph);
657 if (E) {
658 bool tx_valid = true;
659 if (E->value.texture_idx >= 0) {
660 if (E->value.texture_idx < fd->textures.size()) {
661 tx_valid = fd->textures[E->value.texture_idx].image.is_valid();
662 } else {
663 tx_valid = false;
664 }
665 }
666 if (tx_valid) {
667 r_glyph = E->value;
668 return E->value.found;
669 #ifdef DEBUG_ENABLED
670 } else {
671 WARN_PRINT(vformat("Invalid texture cache for glyph %x in font %s, glyph will be re-rendered. Re-import this font to regenerate textures.", glyph_index, p_font_data->font_name));
672 #endif
673 }
674 }
675
676 if (glyph_index == 0) { // Non graphical or invalid glyph, do not render.
677 E = fd->glyph_map.insert(p_glyph, FontGlyph());
678 r_glyph = E->value;
679 return true;
680 }
681
682 #ifdef MODULE_FREETYPE_ENABLED
683 FontGlyph gl;
684 if (fd->face) {
685 FT_Int32 flags = FT_LOAD_DEFAULT;
686
687 bool outline = p_size.y > 0;
688 switch (p_font_data->hinting) {
689 case TextServer::HINTING_NONE:
690 flags |= FT_LOAD_NO_HINTING;
691 break;
692 case TextServer::HINTING_LIGHT:
693 flags |= FT_LOAD_TARGET_LIGHT;
694 break;
695 default:
696 flags |= FT_LOAD_TARGET_NORMAL;
697 break;
698 }
699 if (p_font_data->force_autohinter) {
700 flags |= FT_LOAD_FORCE_AUTOHINT;
701 }
702 if (outline || (p_font_data->disable_embedded_bitmaps && !FT_HAS_COLOR(fd->face))) {
703 flags |= FT_LOAD_NO_BITMAP;
704 } else if (FT_HAS_COLOR(fd->face)) {
705 flags |= FT_LOAD_COLOR;
706 }
707
708 glyph_index = FT_Get_Char_Index(fd->face, glyph_index);
709
710 FT_Fixed v, h;
711 FT_Get_Advance(fd->face, glyph_index, flags, &h);
712 FT_Get_Advance(fd->face, glyph_index, flags | FT_LOAD_VERTICAL_LAYOUT, &v);
713
714 int error = FT_Load_Glyph(fd->face, glyph_index, flags);
715 if (error) {
716 E = fd->glyph_map.insert(p_glyph, FontGlyph());
717 r_glyph = E->value;
718 return false;
719 }
720
721 if (!p_font_data->msdf) {
722 if ((p_font_data->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (p_font_data->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && p_size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE * 64)) {
723 FT_Pos xshift = (int)((p_glyph >> 27) & 3) << 4;
724 FT_Outline_Translate(&fd->face->glyph->outline, xshift, 0);
725 } else if ((p_font_data->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (p_font_data->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && p_size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE * 64)) {
726 FT_Pos xshift = (int)((p_glyph >> 27) & 3) << 5;
727 FT_Outline_Translate(&fd->face->glyph->outline, xshift, 0);
728 }
729 }
730
731 if (p_font_data->embolden != 0.f) {
732 FT_Pos strength = p_font_data->embolden * p_size.x / 16; // 26.6 fractional units (1 / 64).
733 FT_Outline_Embolden(&fd->face->glyph->outline, strength);
734 }
735
736 if (p_font_data->transform != Transform2D()) {
737 FT_Matrix mat = { FT_Fixed(p_font_data->transform[0][0] * 65536), FT_Fixed(p_font_data->transform[0][1] * 65536), FT_Fixed(p_font_data->transform[1][0] * 65536), FT_Fixed(p_font_data->transform[1][1] * 65536) }; // 16.16 fractional units (1 / 65536).
738 FT_Outline_Transform(&fd->face->glyph->outline, &mat);
739 }
740
741 FT_Render_Mode aa_mode = FT_RENDER_MODE_NORMAL;
742 bool bgra = false;
743 switch (p_font_data->antialiasing) {
744 case FONT_ANTIALIASING_NONE: {
745 aa_mode = FT_RENDER_MODE_MONO;
746 } break;
747 case FONT_ANTIALIASING_GRAY: {
748 aa_mode = FT_RENDER_MODE_NORMAL;
749 } break;
750 case FONT_ANTIALIASING_LCD: {
751 int aa_layout = (int)((p_glyph >> 24) & 7);
752 switch (aa_layout) {
753 case FONT_LCD_SUBPIXEL_LAYOUT_HRGB: {
754 aa_mode = FT_RENDER_MODE_LCD;
755 bgra = false;
756 } break;
757 case FONT_LCD_SUBPIXEL_LAYOUT_HBGR: {
758 aa_mode = FT_RENDER_MODE_LCD;
759 bgra = true;
760 } break;
761 case FONT_LCD_SUBPIXEL_LAYOUT_VRGB: {
762 aa_mode = FT_RENDER_MODE_LCD_V;
763 bgra = false;
764 } break;
765 case FONT_LCD_SUBPIXEL_LAYOUT_VBGR: {
766 aa_mode = FT_RENDER_MODE_LCD_V;
767 bgra = true;
768 } break;
769 default: {
770 aa_mode = FT_RENDER_MODE_NORMAL;
771 } break;
772 }
773 } break;
774 }
775
776 FT_GlyphSlot slot = fd->face->glyph;
777 bool from_svg = (slot->format == FT_GLYPH_FORMAT_SVG); // Need to check before FT_Render_Glyph as it will change format to bitmap.
778 if (!outline) {
779 if (!p_font_data->msdf) {
780 error = FT_Render_Glyph(slot, aa_mode);
781 }
782 if (!error) {
783 if (p_font_data->msdf) {
784 #ifdef MODULE_MSDFGEN_ENABLED
785 gl = rasterize_msdf(p_font_data, fd, p_font_data->msdf_range, rect_range, &slot->outline, Vector2((h + (1 << 9)) >> 10, (v + (1 << 9)) >> 10) / 64.0);
786 #else
787 fd->glyph_map[p_glyph] = FontGlyph();
788 ERR_FAIL_V_MSG(false, "Compiled without MSDFGEN support!");
789 #endif
790 } else {
791 gl = rasterize_bitmap(fd, rect_range, slot->bitmap, slot->bitmap_top, slot->bitmap_left, Vector2((h + (1 << 9)) >> 10, (v + (1 << 9)) >> 10) / 64.0, bgra);
792 }
793 }
794 } else {
795 FT_Stroker stroker;
796 if (FT_Stroker_New(ft_library, &stroker) != 0) {
797 fd->glyph_map[p_glyph] = FontGlyph();
798 ERR_FAIL_V_MSG(false, "FreeType: Failed to load glyph stroker.");
799 }
800
801 FT_Stroker_Set(stroker, (int)(fd->size.y * 16.0), FT_STROKER_LINECAP_BUTT, FT_STROKER_LINEJOIN_ROUND, 0);
802 FT_Glyph glyph;
803 FT_BitmapGlyph glyph_bitmap;
804
805 if (FT_Get_Glyph(fd->face->glyph, &glyph) != 0) {
806 goto cleanup_stroker;
807 }
808 if (FT_Glyph_Stroke(&glyph, stroker, 1) != 0) {
809 goto cleanup_glyph;
810 }
811 if (FT_Glyph_To_Bitmap(&glyph, aa_mode, nullptr, 1) != 0) {
812 goto cleanup_glyph;
813 }
814 glyph_bitmap = (FT_BitmapGlyph)glyph;
815 gl = rasterize_bitmap(fd, rect_range, glyph_bitmap->bitmap, glyph_bitmap->top, glyph_bitmap->left, Vector2(), bgra);
816
817 cleanup_glyph:
818 FT_Done_Glyph(glyph);
819 cleanup_stroker:
820 FT_Stroker_Done(stroker);
821 }
822 gl.from_svg = from_svg;
823 E = fd->glyph_map.insert(p_glyph, gl);
824 r_glyph = E->value;
825 return gl.found;
826 }
827 #endif
828 E = fd->glyph_map.insert(p_glyph, FontGlyph());
829 r_glyph = E->value;
830 return false;
831 }
832
833 bool TextServerFallback::_ensure_cache_for_size(FontFallback *p_font_data, const Vector2i &p_size, FontForSizeFallback *&r_cache_for_size, bool p_silent, uint32_t p_oversampling) const {
834 ERR_FAIL_COND_V(p_size.x <= 0, false);
835
836 HashMap<Vector2i, FontForSizeFallback *>::Iterator E = p_font_data->cache.find(p_size);
837 if (E) {
838 r_cache_for_size = E->value;
839 // Size used directly, remove from oversampling list.
840 if (p_oversampling == 0 && E->value->viewport_oversampling != 0) {
841 OversamplingLevel *ol = oversampling_levels.getptr(E->value->viewport_oversampling);
842 if (ol) {
843 ol->fonts.erase(E->value);
844 }
845 }
846 return true;
847 }
848
849 r_cache_for_size = nullptr;
850 FontForSizeFallback *fd = memnew(FontForSizeFallback);
851 fd->size = p_size;
852 if (p_font_data->data_ptr && (p_font_data->data_size > 0)) {
853 // Init dynamic font.
854 #ifdef MODULE_FREETYPE_ENABLED
855 int error = 0;
856 {
857 MutexLock ftlock(ft_mutex);
858 if (!ft_library) {
859 error = FT_Init_FreeType(&ft_library);
860 if (error != 0) {
861 memdelete(fd);
862 if (p_silent) {
863 return false;
864 } else {
865 ERR_FAIL_V_MSG(false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
866 }
867 }
868 #ifdef MODULE_SVG_ENABLED
869 FT_Property_Set(ft_library, "ot-svg", "svg-hooks", get_tvg_svg_in_ot_hooks());
870 #endif
871 }
872
873 memset(&fd->stream, 0, sizeof(FT_StreamRec));
874 fd->stream.base = (unsigned char *)p_font_data->data_ptr;
875 fd->stream.size = p_font_data->data_size;
876 fd->stream.pos = 0;
877
878 FT_Open_Args fargs;
879 memset(&fargs, 0, sizeof(FT_Open_Args));
880 fargs.memory_base = (unsigned char *)p_font_data->data_ptr;
881 fargs.memory_size = p_font_data->data_size;
882 fargs.flags = FT_OPEN_MEMORY;
883 fargs.stream = &fd->stream;
884
885 int max_index = 0;
886 FT_Face tmp_face = nullptr;
887 error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face);
888 if (tmp_face && error == 0) {
889 max_index = tmp_face->num_faces - 1;
890 }
891 if (tmp_face) {
892 FT_Done_Face(tmp_face);
893 }
894
895 error = FT_Open_Face(ft_library, &fargs, CLAMP(p_font_data->face_index, 0, max_index), &fd->face);
896 if (error) {
897 FT_Done_Face(fd->face);
898 fd->face = nullptr;
899 memdelete(fd);
900 if (p_silent) {
901 return false;
902 } else {
903 ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "'.");
904 }
905 }
906 }
907
908 double sz = double(fd->size.x) / 64.0;
909 if (p_font_data->msdf) {
910 sz = p_font_data->msdf_source_size;
911 }
912
913 if (FT_HAS_COLOR(fd->face) && fd->face->num_fixed_sizes > 0) {
914 int best_match = 0;
915 int diff = Math::abs(sz - ((int64_t)fd->face->available_sizes[0].width));
916 fd->scale = sz / fd->face->available_sizes[0].width;
917 for (int i = 1; i < fd->face->num_fixed_sizes; i++) {
918 int ndiff = Math::abs(sz - ((int64_t)fd->face->available_sizes[i].width));
919 if (ndiff < diff) {
920 best_match = i;
921 diff = ndiff;
922 fd->scale = sz / fd->face->available_sizes[i].width;
923 }
924 }
925 FT_Select_Size(fd->face, best_match);
926 } else {
927 FT_Size_RequestRec req;
928 req.type = FT_SIZE_REQUEST_TYPE_NOMINAL;
929 req.width = sz * 64.0;
930 req.height = sz * 64.0;
931 req.horiResolution = 0;
932 req.vertResolution = 0;
933
934 FT_Request_Size(fd->face, &req);
935 if (fd->face->size->metrics.y_ppem != 0) {
936 fd->scale = sz / (double)fd->face->size->metrics.y_ppem;
937 }
938 }
939
940 fd->ascent = (fd->face->size->metrics.ascender / 64.0) * fd->scale;
941 fd->descent = (-fd->face->size->metrics.descender / 64.0) * fd->scale;
942 fd->underline_position = (-FT_MulFix(fd->face->underline_position, fd->face->size->metrics.y_scale) / 64.0) * fd->scale;
943 fd->underline_thickness = (FT_MulFix(fd->face->underline_thickness, fd->face->size->metrics.y_scale) / 64.0) * fd->scale;
944
945 if (!p_font_data->face_init) {
946 // When a font does not provide a `family_name`, FreeType tries to synthesize one based on other names.
947 // FreeType automatically converts non-ASCII characters to "?" in the synthesized name.
948 // To avoid that behavior, use the format-specific name directly if available.
949 if (FT_IS_SFNT(fd->face)) {
950 int name_count = FT_Get_Sfnt_Name_Count(fd->face);
951 for (int i = 0; i < name_count; i++) {
952 FT_SfntName sfnt_name;
953 if (FT_Get_Sfnt_Name(fd->face, i, &sfnt_name) != 0) {
954 continue;
955 }
956 if (sfnt_name.name_id != TT_NAME_ID_FONT_FAMILY && sfnt_name.name_id != TT_NAME_ID_TYPOGRAPHIC_FAMILY) {
957 continue;
958 }
959 if (!p_font_data->font_name.is_empty() && sfnt_name.language_id != TT_MS_LANGID_ENGLISH_UNITED_STATES) {
960 continue;
961 }
962
963 switch (sfnt_name.platform_id) {
964 case TT_PLATFORM_APPLE_UNICODE: {
965 p_font_data->font_name.clear();
966 p_font_data->font_name.append_utf16((const char16_t *)sfnt_name.string, sfnt_name.string_len / 2, false);
967 } break;
968
969 case TT_PLATFORM_MICROSOFT: {
970 if (sfnt_name.encoding_id == TT_MS_ID_UNICODE_CS || sfnt_name.encoding_id == TT_MS_ID_UCS_4) {
971 p_font_data->font_name.clear();
972 p_font_data->font_name.append_utf16((const char16_t *)sfnt_name.string, sfnt_name.string_len / 2, false);
973 }
974 } break;
975 }
976 }
977 }
978 if (p_font_data->font_name.is_empty() && fd->face->family_name != nullptr) {
979 p_font_data->font_name = String::utf8((const char *)fd->face->family_name);
980 }
981 if (fd->face->style_name != nullptr) {
982 p_font_data->style_name = String::utf8((const char *)fd->face->style_name);
983 }
984 p_font_data->weight = _font_get_weight_by_name(p_font_data->style_name.to_lower());
985 p_font_data->stretch = _font_get_stretch_by_name(p_font_data->style_name.to_lower());
986 p_font_data->style_flags = 0;
987 if ((fd->face->style_flags & FT_STYLE_FLAG_BOLD) || p_font_data->weight >= 700) {
988 p_font_data->style_flags.set_flag(FONT_BOLD);
989 }
990 if ((fd->face->style_flags & FT_STYLE_FLAG_ITALIC) || _is_ital_style(p_font_data->style_name.to_lower())) {
991 p_font_data->style_flags.set_flag(FONT_ITALIC);
992 }
993 if (fd->face->face_flags & FT_FACE_FLAG_FIXED_WIDTH) {
994 p_font_data->style_flags.set_flag(FONT_FIXED_WIDTH);
995 }
996 // Read OpenType variations.
997 p_font_data->supported_varaitions.clear();
998 if (fd->face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
999 FT_MM_Var *amaster;
1000 FT_Get_MM_Var(fd->face, &amaster);
1001 for (FT_UInt i = 0; i < amaster->num_axis; i++) {
1002 p_font_data->supported_varaitions[(int32_t)amaster->axis[i].tag] = Vector3i(amaster->axis[i].minimum / 65536, amaster->axis[i].maximum / 65536, amaster->axis[i].def / 65536);
1003 }
1004 FT_Done_MM_Var(ft_library, amaster);
1005 }
1006 p_font_data->face_init = true;
1007 }
1008
1009 #if defined(MACOS_ENABLED) || defined(IOS_ENABLED)
1010 if (p_font_data->font_name == ".Apple Color Emoji UI" || p_font_data->font_name == "Apple Color Emoji") {
1011 // The baseline offset is missing from the Apple Color Emoji UI font data, so add it manually.
1012 // This issue doesn't occur with other system emoji fonts.
1013 if (!FT_Load_Glyph(fd->face, FT_Get_Char_Index(fd->face, 0x1F92E), FT_LOAD_DEFAULT | FT_LOAD_COLOR)) {
1014 if (fd->face->glyph->metrics.horiBearingY == fd->face->glyph->metrics.height) {
1015 p_font_data->baseline_offset = 0.15;
1016 }
1017 }
1018 }
1019 #endif
1020
1021 // Write variations.
1022 if (fd->face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) {
1023 FT_MM_Var *amaster;
1024
1025 FT_Get_MM_Var(fd->face, &amaster);
1026
1027 Vector<FT_Fixed> coords;
1028 coords.resize(amaster->num_axis);
1029
1030 FT_Get_Var_Design_Coordinates(fd->face, coords.size(), coords.ptrw());
1031
1032 for (FT_UInt i = 0; i < amaster->num_axis; i++) {
1033 // Reset to default.
1034 int32_t var_tag = amaster->axis[i].tag;
1035 double var_value = (double)amaster->axis[i].def / 65536.0;
1036 coords.write[i] = amaster->axis[i].def;
1037
1038 if (p_font_data->variation_coordinates.has(var_tag)) {
1039 var_value = p_font_data->variation_coordinates[var_tag];
1040 coords.write[i] = CLAMP(var_value * 65536.0, amaster->axis[i].minimum, amaster->axis[i].maximum);
1041 }
1042
1043 if (p_font_data->variation_coordinates.has(tag_to_name(var_tag))) {
1044 var_value = p_font_data->variation_coordinates[tag_to_name(var_tag)];
1045 coords.write[i] = CLAMP(var_value * 65536.0, amaster->axis[i].minimum, amaster->axis[i].maximum);
1046 }
1047 }
1048
1049 FT_Set_Var_Design_Coordinates(fd->face, coords.size(), coords.ptrw());
1050 FT_Done_MM_Var(ft_library, amaster);
1051 }
1052 #else
1053 memdelete(fd);
1054 if (p_silent) {
1055 return false;
1056 } else {
1057 ERR_FAIL_V_MSG(false, "FreeType: Can't load dynamic font, engine is compiled without FreeType support!");
1058 }
1059 #endif
1060 }
1061
1062 fd->owner = p_font_data;
1063 p_font_data->cache.insert(p_size, fd);
1064 r_cache_for_size = fd;
1065 if (p_oversampling != 0) {
1066 OversamplingLevel *ol = oversampling_levels.getptr(p_oversampling);
1067 if (ol) {
1068 fd->viewport_oversampling = p_oversampling;
1069 ol->fonts.insert(fd);
1070 }
1071 }
1072 return true;
1073 }
1074
1075 void TextServerFallback::_reference_oversampling_level(double p_oversampling) {
1076 uint32_t oversampling = CLAMP(p_oversampling, 0.1, 100.0) * 64;
1077 if (oversampling == 64) {
1078 return;
1079 }
1080 OversamplingLevel *ol = oversampling_levels.getptr(oversampling);
1081 if (ol) {
1082 ol->refcount++;
1083 } else {
1084 OversamplingLevel new_ol;
1085 oversampling_levels.insert(oversampling, new_ol);
1086 }
1087 }
1088
1089 void TextServerFallback::_unreference_oversampling_level(double p_oversampling) {
1090 uint32_t oversampling = CLAMP(p_oversampling, 0.1, 100.0) * 64;
1091 if (oversampling == 64) {
1092 return;
1093 }
1094 OversamplingLevel *ol = oversampling_levels.getptr(oversampling);
1095 if (ol) {
1096 ol->refcount--;
1097 if (ol->refcount == 0) {
1098 for (FontForSizeFallback *fd : ol->fonts) {
1099 fd->owner->cache.erase(fd->size);
1100 memdelete(fd);
1101 }
1102 ol->fonts.clear();
1103 oversampling_levels.erase(oversampling);
1104 }
1105 }
1106 }
1107
1108 _FORCE_INLINE_ bool TextServerFallback::_font_validate(const RID &p_font_rid) const {
1109 FontFallback *fd = _get_font_data(p_font_rid);
1110 ERR_FAIL_NULL_V(fd, false);
1111
1112 MutexLock lock(fd->mutex);
1113 Vector2i size = _get_size(fd, 16);
1114 FontForSizeFallback *ffsd = nullptr;
1115 return _ensure_cache_for_size(fd, size, ffsd, true);
1116 }
1117
1118 _FORCE_INLINE_ void TextServerFallback::_font_clear_cache(FontFallback *p_font_data) {
1119 MutexLock ftlock(ft_mutex);
1120
1121 for (const KeyValue<Vector2i, FontForSizeFallback *> &E : p_font_data->cache) {
1122 if (E.value->viewport_oversampling != 0) {
1123 OversamplingLevel *ol = oversampling_levels.getptr(E.value->viewport_oversampling);
1124 if (ol) {
1125 ol->fonts.erase(E.value);
1126 }
1127 }
1128 memdelete(E.value);
1129 }
1130
1131 p_font_data->cache.clear();
1132 p_font_data->face_init = false;
1133 p_font_data->supported_varaitions.clear();
1134 }
1135
1136 RID TextServerFallback::_create_font() {
1137 _THREAD_SAFE_METHOD_
1138
1139 FontFallback *fd = memnew(FontFallback);
1140
1141 return font_owner.make_rid(fd);
1142 }
1143
1144 RID TextServerFallback::_create_font_linked_variation(const RID &p_font_rid) {
1145 _THREAD_SAFE_METHOD_
1146
1147 RID rid = p_font_rid;
1148 FontFallbackLinkedVariation *fdv = font_var_owner.get_or_null(rid);
1149 if (unlikely(fdv)) {
1150 rid = fdv->base_font;
1151 }
1152 ERR_FAIL_COND_V(!font_owner.owns(rid), RID());
1153
1154 FontFallbackLinkedVariation *new_fdv = memnew(FontFallbackLinkedVariation);
1155 new_fdv->base_font = rid;
1156
1157 return font_var_owner.make_rid(new_fdv);
1158 }
1159
1160 void TextServerFallback::_font_set_data(const RID &p_font_rid, const PackedByteArray &p_data) {
1161 FontFallback *fd = _get_font_data(p_font_rid);
1162 ERR_FAIL_NULL(fd);
1163
1164 MutexLock lock(fd->mutex);
1165 _font_clear_cache(fd);
1166 fd->data = p_data;
1167 fd->data_ptr = fd->data.ptr();
1168 fd->data_size = fd->data.size();
1169 }
1170
1171 void TextServerFallback::_font_set_data_ptr(const RID &p_font_rid, const uint8_t *p_data_ptr, int64_t p_data_size) {
1172 FontFallback *fd = _get_font_data(p_font_rid);
1173 ERR_FAIL_NULL(fd);
1174
1175 MutexLock lock(fd->mutex);
1176 _font_clear_cache(fd);
1177 fd->data.resize(0);
1178 fd->data_ptr = p_data_ptr;
1179 fd->data_size = p_data_size;
1180 }
1181
1182 void TextServerFallback::_font_set_style(const RID &p_font_rid, BitField<FontStyle> p_style) {
1183 FontFallback *fd = _get_font_data(p_font_rid);
1184 ERR_FAIL_NULL(fd);
1185
1186 MutexLock lock(fd->mutex);
1187 Vector2i size = _get_size(fd, 16);
1188 FontForSizeFallback *ffsd = nullptr;
1189 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
1190 fd->style_flags = p_style;
1191 }
1192
1193 void TextServerFallback::_font_set_face_index(const RID &p_font_rid, int64_t p_face_index) {
1194 ERR_FAIL_COND(p_face_index < 0);
1195 ERR_FAIL_COND(p_face_index >= 0x7FFF);
1196
1197 FontFallback *fd = _get_font_data(p_font_rid);
1198 ERR_FAIL_NULL(fd);
1199
1200 MutexLock lock(fd->mutex);
1201 if (fd->face_index != p_face_index) {
1202 fd->face_index = p_face_index;
1203 _font_clear_cache(fd);
1204 }
1205 }
1206
1207 int64_t TextServerFallback::_font_get_face_index(const RID &p_font_rid) const {
1208 FontFallback *fd = _get_font_data(p_font_rid);
1209 ERR_FAIL_NULL_V(fd, 0);
1210
1211 MutexLock lock(fd->mutex);
1212 return fd->face_index;
1213 }
1214
1215 int64_t TextServerFallback::_font_get_face_count(const RID &p_font_rid) const {
1216 FontFallback *fd = _get_font_data(p_font_rid);
1217 ERR_FAIL_NULL_V(fd, 0);
1218
1219 MutexLock lock(fd->mutex);
1220 int face_count = 0;
1221
1222 if (fd->data_ptr && (fd->data_size > 0)) {
1223 // Init dynamic font.
1224 #ifdef MODULE_FREETYPE_ENABLED
1225 int error = 0;
1226 if (!ft_library) {
1227 error = FT_Init_FreeType(&ft_library);
1228 ERR_FAIL_COND_V_MSG(error != 0, false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'.");
1229 #ifdef MODULE_SVG_ENABLED
1230 FT_Property_Set(ft_library, "ot-svg", "svg-hooks", get_tvg_svg_in_ot_hooks());
1231 #endif
1232 }
1233
1234 FT_StreamRec stream;
1235 memset(&stream, 0, sizeof(FT_StreamRec));
1236 stream.base = (unsigned char *)fd->data_ptr;
1237 stream.size = fd->data_size;
1238 stream.pos = 0;
1239
1240 FT_Open_Args fargs;
1241 memset(&fargs, 0, sizeof(FT_Open_Args));
1242 fargs.memory_base = (unsigned char *)fd->data_ptr;
1243 fargs.memory_size = fd->data_size;
1244 fargs.flags = FT_OPEN_MEMORY;
1245 fargs.stream = &stream;
1246
1247 MutexLock ftlock(ft_mutex);
1248
1249 FT_Face tmp_face = nullptr;
1250 error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face);
1251 if (error == 0) {
1252 face_count = tmp_face->num_faces;
1253 FT_Done_Face(tmp_face);
1254 }
1255 #endif
1256 }
1257
1258 return face_count;
1259 }
1260
1261 BitField<TextServer::FontStyle> TextServerFallback::_font_get_style(const RID &p_font_rid) const {
1262 FontFallback *fd = _get_font_data(p_font_rid);
1263 ERR_FAIL_NULL_V(fd, 0);
1264
1265 MutexLock lock(fd->mutex);
1266 Vector2i size = _get_size(fd, 16);
1267 FontForSizeFallback *ffsd = nullptr;
1268 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 0);
1269 return fd->style_flags;
1270 }
1271
1272 void TextServerFallback::_font_set_style_name(const RID &p_font_rid, const String &p_name) {
1273 FontFallback *fd = _get_font_data(p_font_rid);
1274 ERR_FAIL_NULL(fd);
1275
1276 MutexLock lock(fd->mutex);
1277 Vector2i size = _get_size(fd, 16);
1278 FontForSizeFallback *ffsd = nullptr;
1279 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
1280 fd->style_name = p_name;
1281 }
1282
1283 String TextServerFallback::_font_get_style_name(const RID &p_font_rid) const {
1284 FontFallback *fd = _get_font_data(p_font_rid);
1285 ERR_FAIL_NULL_V(fd, String());
1286
1287 MutexLock lock(fd->mutex);
1288 Vector2i size = _get_size(fd, 16);
1289 FontForSizeFallback *ffsd = nullptr;
1290 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), String());
1291 return fd->style_name;
1292 }
1293
1294 void TextServerFallback::_font_set_weight(const RID &p_font_rid, int64_t p_weight) {
1295 FontFallback *fd = _get_font_data(p_font_rid);
1296 ERR_FAIL_NULL(fd);
1297
1298 MutexLock lock(fd->mutex);
1299 Vector2i size = _get_size(fd, 16);
1300 FontForSizeFallback *ffsd = nullptr;
1301 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
1302 fd->weight = CLAMP(p_weight, 100, 999);
1303 }
1304
1305 int64_t TextServerFallback::_font_get_weight(const RID &p_font_rid) const {
1306 FontFallback *fd = _get_font_data(p_font_rid);
1307 ERR_FAIL_NULL_V(fd, 400);
1308
1309 MutexLock lock(fd->mutex);
1310 Vector2i size = _get_size(fd, 16);
1311 FontForSizeFallback *ffsd = nullptr;
1312 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 400);
1313 return fd->weight;
1314 }
1315
1316 void TextServerFallback::_font_set_stretch(const RID &p_font_rid, int64_t p_stretch) {
1317 FontFallback *fd = _get_font_data(p_font_rid);
1318 ERR_FAIL_NULL(fd);
1319
1320 MutexLock lock(fd->mutex);
1321 Vector2i size = _get_size(fd, 16);
1322 FontForSizeFallback *ffsd = nullptr;
1323 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
1324 fd->stretch = CLAMP(p_stretch, 50, 200);
1325 }
1326
1327 int64_t TextServerFallback::_font_get_stretch(const RID &p_font_rid) const {
1328 FontFallback *fd = _get_font_data(p_font_rid);
1329 ERR_FAIL_NULL_V(fd, 100);
1330
1331 MutexLock lock(fd->mutex);
1332 Vector2i size = _get_size(fd, 16);
1333 FontForSizeFallback *ffsd = nullptr;
1334 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 100);
1335 return fd->stretch;
1336 }
1337
1338 void TextServerFallback::_font_set_name(const RID &p_font_rid, const String &p_name) {
1339 FontFallback *fd = _get_font_data(p_font_rid);
1340 ERR_FAIL_NULL(fd);
1341
1342 MutexLock lock(fd->mutex);
1343 Vector2i size = _get_size(fd, 16);
1344 FontForSizeFallback *ffsd = nullptr;
1345 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
1346 fd->font_name = p_name;
1347 }
1348
1349 String TextServerFallback::_font_get_name(const RID &p_font_rid) const {
1350 FontFallback *fd = _get_font_data(p_font_rid);
1351 ERR_FAIL_NULL_V(fd, String());
1352
1353 MutexLock lock(fd->mutex);
1354 Vector2i size = _get_size(fd, 16);
1355 FontForSizeFallback *ffsd = nullptr;
1356 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), String());
1357 return fd->font_name;
1358 }
1359
1360 void TextServerFallback::_font_set_antialiasing(const RID &p_font_rid, TextServer::FontAntialiasing p_antialiasing) {
1361 FontFallback *fd = _get_font_data(p_font_rid);
1362 ERR_FAIL_NULL(fd);
1363
1364 MutexLock lock(fd->mutex);
1365 if (fd->antialiasing != p_antialiasing) {
1366 _font_clear_cache(fd);
1367 fd->antialiasing = p_antialiasing;
1368 }
1369 }
1370
1371 TextServer::FontAntialiasing TextServerFallback::_font_get_antialiasing(const RID &p_font_rid) const {
1372 FontFallback *fd = _get_font_data(p_font_rid);
1373 ERR_FAIL_NULL_V(fd, TextServer::FONT_ANTIALIASING_NONE);
1374
1375 MutexLock lock(fd->mutex);
1376 return fd->antialiasing;
1377 }
1378
1379 void TextServerFallback::_font_set_disable_embedded_bitmaps(const RID &p_font_rid, bool p_disable_embedded_bitmaps) {
1380 FontFallback *fd = _get_font_data(p_font_rid);
1381 ERR_FAIL_NULL(fd);
1382
1383 MutexLock lock(fd->mutex);
1384 if (fd->disable_embedded_bitmaps != p_disable_embedded_bitmaps) {
1385 _font_clear_cache(fd);
1386 fd->disable_embedded_bitmaps = p_disable_embedded_bitmaps;
1387 }
1388 }
1389
1390 bool TextServerFallback::_font_get_disable_embedded_bitmaps(const RID &p_font_rid) const {
1391 FontFallback *fd = _get_font_data(p_font_rid);
1392 ERR_FAIL_NULL_V(fd, false);
1393
1394 MutexLock lock(fd->mutex);
1395 return fd->disable_embedded_bitmaps;
1396 }
1397
1398 void TextServerFallback::_font_set_generate_mipmaps(const RID &p_font_rid, bool p_generate_mipmaps) {
1399 FontFallback *fd = _get_font_data(p_font_rid);
1400 ERR_FAIL_NULL(fd);
1401
1402 MutexLock lock(fd->mutex);
1403 if (fd->mipmaps != p_generate_mipmaps) {
1404 for (KeyValue<Vector2i, FontForSizeFallback *> &E : fd->cache) {
1405 for (int i = 0; i < E.value->textures.size(); i++) {
1406 E.value->textures.write[i].dirty = true;
1407 E.value->textures.write[i].texture = Ref<ImageTexture>();
1408 }
1409 }
1410 fd->mipmaps = p_generate_mipmaps;
1411 }
1412 }
1413
1414 bool TextServerFallback::_font_get_generate_mipmaps(const RID &p_font_rid) const {
1415 FontFallback *fd = _get_font_data(p_font_rid);
1416 ERR_FAIL_NULL_V(fd, false);
1417
1418 MutexLock lock(fd->mutex);
1419 return fd->mipmaps;
1420 }
1421
1422 void TextServerFallback::_font_set_multichannel_signed_distance_field(const RID &p_font_rid, bool p_msdf) {
1423 FontFallback *fd = _get_font_data(p_font_rid);
1424 ERR_FAIL_NULL(fd);
1425
1426 MutexLock lock(fd->mutex);
1427 if (fd->msdf != p_msdf) {
1428 _font_clear_cache(fd);
1429 fd->msdf = p_msdf;
1430 }
1431 }
1432
1433 bool TextServerFallback::_font_is_multichannel_signed_distance_field(const RID &p_font_rid) const {
1434 FontFallback *fd = _get_font_data(p_font_rid);
1435 ERR_FAIL_NULL_V(fd, false);
1436
1437 MutexLock lock(fd->mutex);
1438 return fd->msdf;
1439 }
1440
1441 void TextServerFallback::_font_set_msdf_pixel_range(const RID &p_font_rid, int64_t p_msdf_pixel_range) {
1442 FontFallback *fd = _get_font_data(p_font_rid);
1443 ERR_FAIL_NULL(fd);
1444
1445 MutexLock lock(fd->mutex);
1446 if (fd->msdf_range != p_msdf_pixel_range) {
1447 _font_clear_cache(fd);
1448 fd->msdf_range = p_msdf_pixel_range;
1449 }
1450 }
1451
1452 int64_t TextServerFallback::_font_get_msdf_pixel_range(const RID &p_font_rid) const {
1453 FontFallback *fd = _get_font_data(p_font_rid);
1454 ERR_FAIL_NULL_V(fd, false);
1455
1456 MutexLock lock(fd->mutex);
1457 return fd->msdf_range;
1458 }
1459
1460 void TextServerFallback::_font_set_msdf_size(const RID &p_font_rid, int64_t p_msdf_size) {
1461 FontFallback *fd = _get_font_data(p_font_rid);
1462 ERR_FAIL_NULL(fd);
1463
1464 MutexLock lock(fd->mutex);
1465 if (fd->msdf_source_size != p_msdf_size) {
1466 _font_clear_cache(fd);
1467 fd->msdf_source_size = p_msdf_size;
1468 }
1469 }
1470
1471 int64_t TextServerFallback::_font_get_msdf_size(const RID &p_font_rid) const {
1472 FontFallback *fd = _get_font_data(p_font_rid);
1473 ERR_FAIL_NULL_V(fd, 0);
1474
1475 MutexLock lock(fd->mutex);
1476 return fd->msdf_source_size;
1477 }
1478
1479 void TextServerFallback::_font_set_fixed_size(const RID &p_font_rid, int64_t p_fixed_size) {
1480 FontFallback *fd = _get_font_data(p_font_rid);
1481 ERR_FAIL_NULL(fd);
1482
1483 MutexLock lock(fd->mutex);
1484 fd->fixed_size = p_fixed_size;
1485 }
1486
1487 int64_t TextServerFallback::_font_get_fixed_size(const RID &p_font_rid) const {
1488 FontFallback *fd = _get_font_data(p_font_rid);
1489 ERR_FAIL_NULL_V(fd, 0);
1490
1491 MutexLock lock(fd->mutex);
1492 return fd->fixed_size;
1493 }
1494
1495 void TextServerFallback::_font_set_fixed_size_scale_mode(const RID &p_font_rid, TextServer::FixedSizeScaleMode p_fixed_size_scale_mode) {
1496 FontFallback *fd = _get_font_data(p_font_rid);
1497 ERR_FAIL_NULL(fd);
1498
1499 MutexLock lock(fd->mutex);
1500 fd->fixed_size_scale_mode = p_fixed_size_scale_mode;
1501 }
1502
1503 TextServer::FixedSizeScaleMode TextServerFallback::_font_get_fixed_size_scale_mode(const RID &p_font_rid) const {
1504 FontFallback *fd = _get_font_data(p_font_rid);
1505 ERR_FAIL_NULL_V(fd, FIXED_SIZE_SCALE_DISABLE);
1506
1507 MutexLock lock(fd->mutex);
1508 return fd->fixed_size_scale_mode;
1509 }
1510
1511 void TextServerFallback::_font_set_allow_system_fallback(const RID &p_font_rid, bool p_allow_system_fallback) {
1512 FontFallback *fd = _get_font_data(p_font_rid);
1513 ERR_FAIL_NULL(fd);
1514
1515 MutexLock lock(fd->mutex);
1516 fd->allow_system_fallback = p_allow_system_fallback;
1517 }
1518
1519 bool TextServerFallback::_font_is_allow_system_fallback(const RID &p_font_rid) const {
1520 FontFallback *fd = _get_font_data(p_font_rid);
1521 ERR_FAIL_NULL_V(fd, false);
1522
1523 MutexLock lock(fd->mutex);
1524 return fd->allow_system_fallback;
1525 }
1526
1527 void TextServerFallback::_font_set_force_autohinter(const RID &p_font_rid, bool p_force_autohinter) {
1528 FontFallback *fd = _get_font_data(p_font_rid);
1529 ERR_FAIL_NULL(fd);
1530
1531 MutexLock lock(fd->mutex);
1532 if (fd->force_autohinter != p_force_autohinter) {
1533 _font_clear_cache(fd);
1534 fd->force_autohinter = p_force_autohinter;
1535 }
1536 }
1537
1538 bool TextServerFallback::_font_is_force_autohinter(const RID &p_font_rid) const {
1539 FontFallback *fd = _get_font_data(p_font_rid);
1540 ERR_FAIL_NULL_V(fd, false);
1541
1542 MutexLock lock(fd->mutex);
1543 return fd->force_autohinter;
1544 }
1545
1546 void TextServerFallback::_font_set_modulate_color_glyphs(const RID &p_font_rid, bool p_modulate) {
1547 FontFallback *fd = _get_font_data(p_font_rid);
1548 ERR_FAIL_NULL(fd);
1549
1550 MutexLock lock(fd->mutex);
1551 if (fd->modulate_color_glyphs != p_modulate) {
1552 fd->modulate_color_glyphs = p_modulate;
1553 }
1554 }
1555
1556 bool TextServerFallback::_font_is_modulate_color_glyphs(const RID &p_font_rid) const {
1557 FontFallback *fd = _get_font_data(p_font_rid);
1558 ERR_FAIL_NULL_V(fd, false);
1559
1560 MutexLock lock(fd->mutex);
1561 return fd->modulate_color_glyphs;
1562 }
1563
1564 void TextServerFallback::_font_set_hinting(const RID &p_font_rid, TextServer::Hinting p_hinting) {
1565 FontFallback *fd = _get_font_data(p_font_rid);
1566 ERR_FAIL_NULL(fd);
1567
1568 MutexLock lock(fd->mutex);
1569 if (fd->hinting != p_hinting) {
1570 _font_clear_cache(fd);
1571 fd->hinting = p_hinting;
1572 }
1573 }
1574
1575 TextServer::Hinting TextServerFallback::_font_get_hinting(const RID &p_font_rid) const {
1576 FontFallback *fd = _get_font_data(p_font_rid);
1577 ERR_FAIL_NULL_V(fd, HINTING_NONE);
1578
1579 MutexLock lock(fd->mutex);
1580 return fd->hinting;
1581 }
1582
1583 void TextServerFallback::_font_set_subpixel_positioning(const RID &p_font_rid, TextServer::SubpixelPositioning p_subpixel) {
1584 FontFallback *fd = _get_font_data(p_font_rid);
1585 ERR_FAIL_NULL(fd);
1586
1587 MutexLock lock(fd->mutex);
1588 fd->subpixel_positioning = p_subpixel;
1589 }
1590
1591 TextServer::SubpixelPositioning TextServerFallback::_font_get_subpixel_positioning(const RID &p_font_rid) const {
1592 FontFallback *fd = _get_font_data(p_font_rid);
1593 ERR_FAIL_NULL_V(fd, SUBPIXEL_POSITIONING_DISABLED);
1594
1595 MutexLock lock(fd->mutex);
1596 return fd->subpixel_positioning;
1597 }
1598
1599 void TextServerFallback::_font_set_keep_rounding_remainders(const RID &p_font_rid, bool p_keep_rounding_remainders) {
1600 FontFallback *fd = _get_font_data(p_font_rid);
1601 ERR_FAIL_NULL(fd);
1602
1603 MutexLock lock(fd->mutex);
1604 fd->keep_rounding_remainders = p_keep_rounding_remainders;
1605 }
1606
1607 bool TextServerFallback::_font_get_keep_rounding_remainders(const RID &p_font_rid) const {
1608 FontFallback *fd = _get_font_data(p_font_rid);
1609 ERR_FAIL_NULL_V(fd, false);
1610
1611 MutexLock lock(fd->mutex);
1612 return fd->keep_rounding_remainders;
1613 }
1614
1615 void TextServerFallback::_font_set_embolden(const RID &p_font_rid, double p_strength) {
1616 FontFallback *fd = _get_font_data(p_font_rid);
1617 ERR_FAIL_NULL(fd);
1618
1619 MutexLock lock(fd->mutex);
1620 if (fd->embolden != p_strength) {
1621 _font_clear_cache(fd);
1622 fd->embolden = p_strength;
1623 }
1624 }
1625
1626 double TextServerFallback::_font_get_embolden(const RID &p_font_rid) const {
1627 FontFallback *fd = _get_font_data(p_font_rid);
1628 ERR_FAIL_NULL_V(fd, 0.0);
1629
1630 MutexLock lock(fd->mutex);
1631 return fd->embolden;
1632 }
1633
1634 void TextServerFallback::_font_set_spacing(const RID &p_font_rid, SpacingType p_spacing, int64_t p_value) {
1635 ERR_FAIL_INDEX((int)p_spacing, 4);
1636 FontFallbackLinkedVariation *fdv = font_var_owner.get_or_null(p_font_rid);
1637 if (fdv) {
1638 if (fdv->extra_spacing[p_spacing] != p_value) {
1639 fdv->extra_spacing[p_spacing] = p_value;
1640 }
1641 } else {
1642 FontFallback *fd = font_owner.get_or_null(p_font_rid);
1643 ERR_FAIL_NULL(fd);
1644
1645 MutexLock lock(fd->mutex);
1646 if (fd->extra_spacing[p_spacing] != p_value) {
1647 _font_clear_cache(fd);
1648 fd->extra_spacing[p_spacing] = p_value;
1649 }
1650 }
1651 }
1652
1653 int64_t TextServerFallback::_font_get_spacing(const RID &p_font_rid, SpacingType p_spacing) const {
1654 ERR_FAIL_INDEX_V((int)p_spacing, 4, 0);
1655 FontFallbackLinkedVariation *fdv = font_var_owner.get_or_null(p_font_rid);
1656 if (fdv) {
1657 return fdv->extra_spacing[p_spacing];
1658 } else {
1659 FontFallback *fd = font_owner.get_or_null(p_font_rid);
1660 ERR_FAIL_NULL_V(fd, 0);
1661
1662 MutexLock lock(fd->mutex);
1663 return fd->extra_spacing[p_spacing];
1664 }
1665 }
1666
1667 void TextServerFallback::_font_set_baseline_offset(const RID &p_font_rid, double p_baseline_offset) {
1668 FontFallbackLinkedVariation *fdv = font_var_owner.get_or_null(p_font_rid);
1669 if (fdv) {
1670 if (fdv->baseline_offset != p_baseline_offset) {
1671 fdv->baseline_offset = p_baseline_offset;
1672 }
1673 } else {
1674 FontFallback *fd = font_owner.get_or_null(p_font_rid);
1675 ERR_FAIL_NULL(fd);
1676
1677 MutexLock lock(fd->mutex);
1678 if (fd->baseline_offset != p_baseline_offset) {
1679 _font_clear_cache(fd);
1680 fd->baseline_offset = p_baseline_offset;
1681 }
1682 }
1683 }
1684
1685 double TextServerFallback::_font_get_baseline_offset(const RID &p_font_rid) const {
1686 FontFallbackLinkedVariation *fdv = font_var_owner.get_or_null(p_font_rid);
1687 if (fdv) {
1688 return fdv->baseline_offset;
1689 } else {
1690 FontFallback *fd = font_owner.get_or_null(p_font_rid);
1691 ERR_FAIL_NULL_V(fd, 0.0);
1692
1693 MutexLock lock(fd->mutex);
1694 return fd->baseline_offset;
1695 }
1696 }
1697
1698 void TextServerFallback::_font_set_transform(const RID &p_font_rid, const Transform2D &p_transform) {
1699 FontFallback *fd = _get_font_data(p_font_rid);
1700 ERR_FAIL_NULL(fd);
1701
1702 MutexLock lock(fd->mutex);
1703 if (fd->transform != p_transform) {
1704 _font_clear_cache(fd);
1705 fd->transform = p_transform;
1706 }
1707 }
1708
1709 Transform2D TextServerFallback::_font_get_transform(const RID &p_font_rid) const {
1710 FontFallback *fd = _get_font_data(p_font_rid);
1711 ERR_FAIL_NULL_V(fd, Transform2D());
1712
1713 MutexLock lock(fd->mutex);
1714 return fd->transform;
1715 }
1716
1717 void TextServerFallback::_font_set_variation_coordinates(const RID &p_font_rid, const Dictionary &p_variation_coordinates) {
1718 FontFallback *fd = _get_font_data(p_font_rid);
1719 ERR_FAIL_NULL(fd);
1720
1721 MutexLock lock(fd->mutex);
1722 if (!fd->variation_coordinates.recursive_equal(p_variation_coordinates, 1)) {
1723 _font_clear_cache(fd);
1724 fd->variation_coordinates = p_variation_coordinates.duplicate();
1725 }
1726 }
1727
1728 double TextServerFallback::_font_get_oversampling(const RID &p_font_rid) const {
1729 FontFallback *fd = _get_font_data(p_font_rid);
1730 ERR_FAIL_NULL_V(fd, -1.0);
1731
1732 MutexLock lock(fd->mutex);
1733 return fd->oversampling_override;
1734 }
1735
1736 void TextServerFallback::_font_set_oversampling(const RID &p_font_rid, double p_oversampling) {
1737 FontFallback *fd = _get_font_data(p_font_rid);
1738 ERR_FAIL_NULL(fd);
1739
1740 MutexLock lock(fd->mutex);
1741 if (fd->oversampling_override != p_oversampling) {
1742 _font_clear_cache(fd);
1743 fd->oversampling_override = p_oversampling;
1744 }
1745 }
1746
1747 Dictionary TextServerFallback::_font_get_variation_coordinates(const RID &p_font_rid) const {
1748 FontFallback *fd = _get_font_data(p_font_rid);
1749 ERR_FAIL_NULL_V(fd, Dictionary());
1750
1751 MutexLock lock(fd->mutex);
1752 return fd->variation_coordinates;
1753 }
1754
1755 TypedArray<Vector2i> TextServerFallback::_font_get_size_cache_list(const RID &p_font_rid) const {
1756 FontFallback *fd = _get_font_data(p_font_rid);
1757 ERR_FAIL_NULL_V(fd, TypedArray<Vector2i>());
1758
1759 MutexLock lock(fd->mutex);
1760 TypedArray<Vector2i> ret;
1761 for (const KeyValue<Vector2i, FontForSizeFallback *> &E : fd->cache) {
1762 if ((E.key.x % 64 == 0) && (E.value->viewport_oversampling == 0)) {
1763 ret.push_back(Vector2i(E.key.x / 64, E.key.y));
1764 }
1765 }
1766 return ret;
1767 }
1768
1769 TypedArray<Dictionary> TextServerFallback::_font_get_size_cache_info(const RID &p_font_rid) const {
1770 FontFallback *fd = _get_font_data(p_font_rid);
1771 ERR_FAIL_NULL_V(fd, TypedArray<Dictionary>());
1772
1773 MutexLock lock(fd->mutex);
1774 TypedArray<Dictionary> ret;
1775 for (const KeyValue<Vector2i, FontForSizeFallback *> &E : fd->cache) {
1776 Dictionary size_info;
1777 size_info["size_px"] = Vector2i(E.key.x / 64, E.key.y);
1778 if (E.value->viewport_oversampling) {
1779 size_info["viewport_oversampling"] = double(E.value->viewport_oversampling) / 64.0;
1780 }
1781 size_info["glyphs"] = E.value->glyph_map.size();
1782 size_info["textures"] = E.value->textures.size();
1783 uint64_t sz = 0;
1784 for (const ShelfPackTexture &tx : E.value->textures) {
1785 sz += tx.image->get_data_size() * 2;
1786 }
1787 size_info["textures_size"] = sz;
1788 ret.push_back(size_info);
1789 }
1790
1791 return ret;
1792 }
1793
1794 void TextServerFallback::_font_clear_size_cache(const RID &p_font_rid) {
1795 FontFallback *fd = _get_font_data(p_font_rid);
1796 ERR_FAIL_NULL(fd);
1797
1798 MutexLock lock(fd->mutex);
1799 MutexLock ftlock(ft_mutex);
1800 for (const KeyValue<Vector2i, FontForSizeFallback *> &E : fd->cache) {
1801 if (E.value->viewport_oversampling != 0) {
1802 OversamplingLevel *ol = oversampling_levels.getptr(E.value->viewport_oversampling);
1803 if (ol) {
1804 ol->fonts.erase(E.value);
1805 }
1806 }
1807 memdelete(E.value);
1808 }
1809 fd->cache.clear();
1810 }
1811
1812 void TextServerFallback::_font_remove_size_cache(const RID &p_font_rid, const Vector2i &p_size) {
1813 FontFallback *fd = _get_font_data(p_font_rid);
1814 ERR_FAIL_NULL(fd);
1815
1816 MutexLock lock(fd->mutex);
1817 MutexLock ftlock(ft_mutex);
1818 Vector2i size = Vector2i(p_size.x * 64, p_size.y);
1819 if (fd->cache.has(size)) {
1820 if (fd->cache[size]->viewport_oversampling != 0) {
1821 OversamplingLevel *ol = oversampling_levels.getptr(fd->cache[size]->viewport_oversampling);
1822 if (ol) {
1823 ol->fonts.erase(fd->cache[size]);
1824 }
1825 }
1826 memdelete(fd->cache[size]);
1827 fd->cache.erase(size);
1828 }
1829 }
1830
1831 void TextServerFallback::_font_set_ascent(const RID &p_font_rid, int64_t p_size, double p_ascent) {
1832 FontFallback *fd = _get_font_data(p_font_rid);
1833 ERR_FAIL_NULL(fd);
1834
1835 MutexLock lock(fd->mutex);
1836 Vector2i size = _get_size(fd, p_size);
1837
1838 FontForSizeFallback *ffsd = nullptr;
1839 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
1840 ffsd->ascent = p_ascent;
1841 }
1842
1843 double TextServerFallback::_font_get_ascent(const RID &p_font_rid, int64_t p_size) const {
1844 FontFallback *fd = _get_font_data(p_font_rid);
1845 ERR_FAIL_NULL_V(fd, 0.0);
1846
1847 MutexLock lock(fd->mutex);
1848 Vector2i size = _get_size(fd, p_size);
1849
1850 FontForSizeFallback *ffsd = nullptr;
1851 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 0.0);
1852
1853 if (fd->msdf) {
1854 return ffsd->ascent * (double)p_size / (double)fd->msdf_source_size;
1855 } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) {
1856 if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
1857 return ffsd->ascent * (double)p_size / (double)fd->fixed_size;
1858 } else {
1859 return ffsd->ascent * Math::round((double)p_size / (double)fd->fixed_size);
1860 }
1861 } else {
1862 return ffsd->ascent;
1863 }
1864 }
1865
1866 void TextServerFallback::_font_set_descent(const RID &p_font_rid, int64_t p_size, double p_descent) {
1867 FontFallback *fd = _get_font_data(p_font_rid);
1868 ERR_FAIL_NULL(fd);
1869
1870 Vector2i size = _get_size(fd, p_size);
1871
1872 FontForSizeFallback *ffsd = nullptr;
1873 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
1874 ffsd->descent = p_descent;
1875 }
1876
1877 double TextServerFallback::_font_get_descent(const RID &p_font_rid, int64_t p_size) const {
1878 FontFallback *fd = _get_font_data(p_font_rid);
1879 ERR_FAIL_NULL_V(fd, 0.0);
1880
1881 MutexLock lock(fd->mutex);
1882 Vector2i size = _get_size(fd, p_size);
1883
1884 FontForSizeFallback *ffsd = nullptr;
1885 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 0.0);
1886
1887 if (fd->msdf) {
1888 return ffsd->descent * (double)p_size / (double)fd->msdf_source_size;
1889 } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) {
1890 if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
1891 return ffsd->descent * (double)p_size / (double)fd->fixed_size;
1892 } else {
1893 return ffsd->descent * Math::round((double)p_size / (double)fd->fixed_size);
1894 }
1895 } else {
1896 return ffsd->descent;
1897 }
1898 }
1899
1900 void TextServerFallback::_font_set_underline_position(const RID &p_font_rid, int64_t p_size, double p_underline_position) {
1901 FontFallback *fd = _get_font_data(p_font_rid);
1902 ERR_FAIL_NULL(fd);
1903
1904 MutexLock lock(fd->mutex);
1905 Vector2i size = _get_size(fd, p_size);
1906
1907 FontForSizeFallback *ffsd = nullptr;
1908 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
1909 ffsd->underline_position = p_underline_position;
1910 }
1911
1912 double TextServerFallback::_font_get_underline_position(const RID &p_font_rid, int64_t p_size) const {
1913 FontFallback *fd = _get_font_data(p_font_rid);
1914 ERR_FAIL_NULL_V(fd, 0.0);
1915
1916 MutexLock lock(fd->mutex);
1917 Vector2i size = _get_size(fd, p_size);
1918
1919 FontForSizeFallback *ffsd = nullptr;
1920 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 0.0);
1921
1922 if (fd->msdf) {
1923 return ffsd->underline_position * (double)p_size / (double)fd->msdf_source_size;
1924 } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) {
1925 if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
1926 return ffsd->underline_position * (double)p_size / (double)fd->fixed_size;
1927 } else {
1928 return ffsd->underline_position * Math::round((double)p_size / (double)fd->fixed_size);
1929 }
1930 } else {
1931 return ffsd->underline_position;
1932 }
1933 }
1934
1935 void TextServerFallback::_font_set_underline_thickness(const RID &p_font_rid, int64_t p_size, double p_underline_thickness) {
1936 FontFallback *fd = _get_font_data(p_font_rid);
1937 ERR_FAIL_NULL(fd);
1938
1939 MutexLock lock(fd->mutex);
1940 Vector2i size = _get_size(fd, p_size);
1941
1942 FontForSizeFallback *ffsd = nullptr;
1943 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
1944 ffsd->underline_thickness = p_underline_thickness;
1945 }
1946
1947 double TextServerFallback::_font_get_underline_thickness(const RID &p_font_rid, int64_t p_size) const {
1948 FontFallback *fd = _get_font_data(p_font_rid);
1949 ERR_FAIL_NULL_V(fd, 0.0);
1950
1951 MutexLock lock(fd->mutex);
1952 Vector2i size = _get_size(fd, p_size);
1953
1954 FontForSizeFallback *ffsd = nullptr;
1955 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 0.0);
1956
1957 if (fd->msdf) {
1958 return ffsd->underline_thickness * (double)p_size / (double)fd->msdf_source_size;
1959 } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) {
1960 if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
1961 return ffsd->underline_thickness * (double)p_size / (double)fd->fixed_size;
1962 } else {
1963 return ffsd->underline_thickness * Math::round((double)p_size / (double)fd->fixed_size);
1964 }
1965 } else {
1966 return ffsd->underline_thickness;
1967 }
1968 }
1969
1970 void TextServerFallback::_font_set_scale(const RID &p_font_rid, int64_t p_size, double p_scale) {
1971 FontFallback *fd = _get_font_data(p_font_rid);
1972 ERR_FAIL_NULL(fd);
1973
1974 MutexLock lock(fd->mutex);
1975 Vector2i size = _get_size(fd, p_size);
1976
1977 FontForSizeFallback *ffsd = nullptr;
1978 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
1979 #ifdef MODULE_FREETYPE_ENABLED
1980 if (ffsd->face) {
1981 return; // Do not override scale for dynamic fonts, it's calculated automatically.
1982 }
1983 #endif
1984 ffsd->scale = p_scale;
1985 }
1986
1987 double TextServerFallback::_font_get_scale(const RID &p_font_rid, int64_t p_size) const {
1988 FontFallback *fd = _get_font_data(p_font_rid);
1989 ERR_FAIL_NULL_V(fd, 0.0);
1990
1991 MutexLock lock(fd->mutex);
1992 Vector2i size = _get_size(fd, p_size);
1993
1994 FontForSizeFallback *ffsd = nullptr;
1995 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 0.0);
1996
1997 if (fd->msdf) {
1998 return ffsd->scale * (double)p_size / (double)fd->msdf_source_size;
1999 } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) {
2000 if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
2001 return ffsd->scale * (double)p_size / (double)fd->fixed_size;
2002 } else {
2003 return ffsd->scale * Math::round((double)p_size / (double)fd->fixed_size);
2004 }
2005 } else {
2006 return ffsd->scale;
2007 }
2008 }
2009
2010 int64_t TextServerFallback::_font_get_texture_count(const RID &p_font_rid, const Vector2i &p_size) const {
2011 FontFallback *fd = _get_font_data(p_font_rid);
2012 ERR_FAIL_NULL_V(fd, 0);
2013
2014 MutexLock lock(fd->mutex);
2015 Vector2i size = _get_size_outline(fd, p_size);
2016
2017 FontForSizeFallback *ffsd = nullptr;
2018 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 0);
2019
2020 return ffsd->textures.size();
2021 }
2022
2023 void TextServerFallback::_font_clear_textures(const RID &p_font_rid, const Vector2i &p_size) {
2024 FontFallback *fd = _get_font_data(p_font_rid);
2025 ERR_FAIL_NULL(fd);
2026 MutexLock lock(fd->mutex);
2027 Vector2i size = _get_size_outline(fd, p_size);
2028
2029 FontForSizeFallback *ffsd = nullptr;
2030 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
2031 ffsd->textures.clear();
2032 }
2033
2034 void TextServerFallback::_font_remove_texture(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) {
2035 FontFallback *fd = _get_font_data(p_font_rid);
2036 ERR_FAIL_NULL(fd);
2037
2038 MutexLock lock(fd->mutex);
2039 Vector2i size = _get_size_outline(fd, p_size);
2040 FontForSizeFallback *ffsd = nullptr;
2041 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
2042 ERR_FAIL_INDEX(p_texture_index, ffsd->textures.size());
2043
2044 ffsd->textures.remove_at(p_texture_index);
2045 }
2046
2047 void TextServerFallback::_font_set_texture_image(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index, const Ref<Image> &p_image) {
2048 FontFallback *fd = _get_font_data(p_font_rid);
2049 ERR_FAIL_NULL(fd);
2050 ERR_FAIL_COND(p_image.is_null());
2051
2052 MutexLock lock(fd->mutex);
2053 Vector2i size = _get_size_outline(fd, p_size);
2054 FontForSizeFallback *ffsd = nullptr;
2055 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
2056 ERR_FAIL_COND(p_texture_index < 0);
2057 if (p_texture_index >= ffsd->textures.size()) {
2058 ffsd->textures.resize(p_texture_index + 1);
2059 }
2060
2061 ShelfPackTexture &tex = ffsd->textures.write[p_texture_index];
2062
2063 tex.image = p_image;
2064 tex.texture_w = p_image->get_width();
2065 tex.texture_h = p_image->get_height();
2066
2067 Ref<Image> img = p_image;
2068 if (fd->mipmaps && !img->has_mipmaps()) {
2069 img = p_image->duplicate();
2070 img->generate_mipmaps();
2071 }
2072 tex.texture = ImageTexture::create_from_image(img);
2073 tex.dirty = false;
2074 }
2075
2076 Ref<Image> TextServerFallback::_font_get_texture_image(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) const {
2077 FontFallback *fd = _get_font_data(p_font_rid);
2078 ERR_FAIL_NULL_V(fd, Ref<Image>());
2079
2080 MutexLock lock(fd->mutex);
2081 Vector2i size = _get_size_outline(fd, p_size);
2082 FontForSizeFallback *ffsd = nullptr;
2083 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Ref<Image>());
2084 ERR_FAIL_INDEX_V(p_texture_index, ffsd->textures.size(), Ref<Image>());
2085
2086 const ShelfPackTexture &tex = ffsd->textures[p_texture_index];
2087 return tex.image;
2088 }
2089
2090 void TextServerFallback::_font_set_texture_offsets(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index, const PackedInt32Array &p_offsets) {
2091 ERR_FAIL_COND(p_offsets.size() % 4 != 0);
2092 FontFallback *fd = _get_font_data(p_font_rid);
2093 ERR_FAIL_NULL(fd);
2094
2095 MutexLock lock(fd->mutex);
2096 Vector2i size = _get_size_outline(fd, p_size);
2097 FontForSizeFallback *ffsd = nullptr;
2098 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
2099 ERR_FAIL_COND(p_texture_index < 0);
2100 if (p_texture_index >= ffsd->textures.size()) {
2101 ffsd->textures.resize(p_texture_index + 1);
2102 }
2103
2104 ShelfPackTexture &tex = ffsd->textures.write[p_texture_index];
2105 tex.shelves.clear();
2106 for (int32_t i = 0; i < p_offsets.size(); i += 4) {
2107 tex.shelves.push_back(Shelf(p_offsets[i], p_offsets[i + 1], p_offsets[i + 2], p_offsets[i + 3]));
2108 }
2109 }
2110
2111 PackedInt32Array TextServerFallback::_font_get_texture_offsets(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) const {
2112 FontFallback *fd = _get_font_data(p_font_rid);
2113 ERR_FAIL_NULL_V(fd, PackedInt32Array());
2114
2115 MutexLock lock(fd->mutex);
2116 Vector2i size = _get_size_outline(fd, p_size);
2117 FontForSizeFallback *ffsd = nullptr;
2118 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), PackedInt32Array());
2119 ERR_FAIL_INDEX_V(p_texture_index, ffsd->textures.size(), PackedInt32Array());
2120
2121 const ShelfPackTexture &tex = ffsd->textures[p_texture_index];
2122 PackedInt32Array ret;
2123 ret.resize(tex.shelves.size() * 4);
2124
2125 int32_t *wr = ret.ptrw();
2126 int32_t i = 0;
2127 for (const Shelf &E : tex.shelves) {
2128 wr[i * 4] = E.x;
2129 wr[i * 4 + 1] = E.y;
2130 wr[i * 4 + 2] = E.w;
2131 wr[i * 4 + 3] = E.h;
2132 i++;
2133 }
2134 return ret;
2135 }
2136
2137 PackedInt32Array TextServerFallback::_font_get_glyph_list(const RID &p_font_rid, const Vector2i &p_size) const {
2138 FontFallback *fd = _get_font_data(p_font_rid);
2139 ERR_FAIL_NULL_V(fd, PackedInt32Array());
2140
2141 MutexLock lock(fd->mutex);
2142 Vector2i size = _get_size_outline(fd, p_size);
2143 FontForSizeFallback *ffsd = nullptr;
2144 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), PackedInt32Array());
2145
2146 PackedInt32Array ret;
2147 const HashMap<int32_t, FontGlyph> &gl = ffsd->glyph_map;
2148 for (const KeyValue<int32_t, FontGlyph> &E : gl) {
2149 ret.push_back(E.key);
2150 }
2151 return ret;
2152 }
2153
2154 void TextServerFallback::_font_clear_glyphs(const RID &p_font_rid, const Vector2i &p_size) {
2155 FontFallback *fd = _get_font_data(p_font_rid);
2156 ERR_FAIL_NULL(fd);
2157
2158 MutexLock lock(fd->mutex);
2159 Vector2i size = _get_size_outline(fd, p_size);
2160 FontForSizeFallback *ffsd = nullptr;
2161 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
2162
2163 ffsd->glyph_map.clear();
2164 }
2165
2166 void TextServerFallback::_font_remove_glyph(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) {
2167 FontFallback *fd = _get_font_data(p_font_rid);
2168 ERR_FAIL_NULL(fd);
2169
2170 MutexLock lock(fd->mutex);
2171 Vector2i size = _get_size_outline(fd, p_size);
2172 FontForSizeFallback *ffsd = nullptr;
2173 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
2174
2175 ffsd->glyph_map.erase(p_glyph);
2176 }
2177
2178 Vector2 TextServerFallback::_font_get_glyph_advance(const RID &p_font_rid, int64_t p_size, int64_t p_glyph) const {
2179 FontFallback *fd = _get_font_data(p_font_rid);
2180 ERR_FAIL_NULL_V(fd, Vector2());
2181
2182 MutexLock lock(fd->mutex);
2183 Vector2i size = _get_size(fd, p_size);
2184
2185 FontForSizeFallback *ffsd = nullptr;
2186 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Vector2());
2187
2188 int mod = 0;
2189 if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
2190 TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get();
2191 if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
2192 mod = (layout << 24);
2193 }
2194 }
2195
2196 FontGlyph fgl;
2197 if (!_ensure_glyph(fd, size, p_glyph | mod, fgl)) {
2198 return Vector2(); // Invalid or non graphicl glyph, do not display errors.
2199 }
2200
2201 Vector2 ea;
2202 if (fd->embolden != 0.0) {
2203 ea.x = fd->embolden * double(size.x) / 4096.0;
2204 }
2205
2206 double scale = _font_get_scale(p_font_rid, p_size);
2207 if (fd->msdf) {
2208 return (fgl.advance + ea) * (double)p_size / (double)fd->msdf_source_size;
2209 } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) {
2210 if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
2211 return (fgl.advance + ea) * (double)p_size / (double)fd->fixed_size;
2212 } else {
2213 return (fgl.advance + ea) * Math::round((double)p_size / (double)fd->fixed_size);
2214 }
2215 } else if ((scale == 1.0) && ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_DISABLED) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x > SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE * 64))) {
2216 return (fgl.advance + ea).round();
2217 } else {
2218 return fgl.advance + ea;
2219 }
2220 }
2221
2222 void TextServerFallback::_font_set_glyph_advance(const RID &p_font_rid, int64_t p_size, int64_t p_glyph, const Vector2 &p_advance) {
2223 FontFallback *fd = _get_font_data(p_font_rid);
2224 ERR_FAIL_NULL(fd);
2225
2226 MutexLock lock(fd->mutex);
2227 Vector2i size = _get_size(fd, p_size);
2228
2229 FontForSizeFallback *ffsd = nullptr;
2230 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
2231
2232 FontGlyph &fgl = ffsd->glyph_map[p_glyph];
2233
2234 fgl.advance = p_advance;
2235 fgl.found = true;
2236 }
2237
2238 Vector2 TextServerFallback::_font_get_glyph_offset(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
2239 FontFallback *fd = _get_font_data(p_font_rid);
2240 ERR_FAIL_NULL_V(fd, Vector2());
2241
2242 MutexLock lock(fd->mutex);
2243 Vector2i size = _get_size_outline(fd, p_size);
2244
2245 FontForSizeFallback *ffsd = nullptr;
2246 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Vector2());
2247
2248 int mod = 0;
2249 if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
2250 TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get();
2251 if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
2252 mod = (layout << 24);
2253 }
2254 }
2255
2256 FontGlyph fgl;
2257 if (!_ensure_glyph(fd, size, p_glyph | mod, fgl)) {
2258 return Vector2(); // Invalid or non graphicl glyph, do not display errors.
2259 }
2260
2261 if (fd->msdf) {
2262 return fgl.rect.position * (double)p_size.x / (double)fd->msdf_source_size;
2263 } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size.x * 64) {
2264 if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
2265 return fgl.rect.position * (double)p_size.x / (double)fd->fixed_size;
2266 } else {
2267 return fgl.rect.position * Math::round((double)p_size.x / (double)fd->fixed_size);
2268 }
2269 } else {
2270 return fgl.rect.position;
2271 }
2272 }
2273
2274 void TextServerFallback::_font_set_glyph_offset(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Vector2 &p_offset) {
2275 FontFallback *fd = _get_font_data(p_font_rid);
2276 ERR_FAIL_NULL(fd);
2277
2278 MutexLock lock(fd->mutex);
2279 Vector2i size = _get_size_outline(fd, p_size);
2280
2281 FontForSizeFallback *ffsd = nullptr;
2282 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
2283
2284 FontGlyph &fgl = ffsd->glyph_map[p_glyph];
2285
2286 fgl.rect.position = p_offset;
2287 fgl.found = true;
2288 }
2289
2290 Vector2 TextServerFallback::_font_get_glyph_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
2291 FontFallback *fd = _get_font_data(p_font_rid);
2292 ERR_FAIL_NULL_V(fd, Vector2());
2293
2294 MutexLock lock(fd->mutex);
2295 Vector2i size = _get_size_outline(fd, p_size);
2296
2297 FontForSizeFallback *ffsd = nullptr;
2298 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Vector2());
2299
2300 int mod = 0;
2301 if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
2302 TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get();
2303 if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
2304 mod = (layout << 24);
2305 }
2306 }
2307
2308 FontGlyph fgl;
2309 if (!_ensure_glyph(fd, size, p_glyph | mod, fgl)) {
2310 return Vector2(); // Invalid or non graphicl glyph, do not display errors.
2311 }
2312
2313 if (fd->msdf) {
2314 return fgl.rect.size * (double)p_size.x / (double)fd->msdf_source_size;
2315 } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size.x * 64) {
2316 if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
2317 return fgl.rect.size * (double)p_size.x / (double)fd->fixed_size;
2318 } else {
2319 return fgl.rect.size * Math::round((double)p_size.x / (double)fd->fixed_size);
2320 }
2321 } else {
2322 return fgl.rect.size;
2323 }
2324 }
2325
2326 void TextServerFallback::_font_set_glyph_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Vector2 &p_gl_size) {
2327 FontFallback *fd = _get_font_data(p_font_rid);
2328 ERR_FAIL_NULL(fd);
2329
2330 MutexLock lock(fd->mutex);
2331 Vector2i size = _get_size_outline(fd, p_size);
2332
2333 FontForSizeFallback *ffsd = nullptr;
2334 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
2335
2336 FontGlyph &fgl = ffsd->glyph_map[p_glyph];
2337
2338 fgl.rect.size = p_gl_size;
2339 fgl.found = true;
2340 }
2341
2342 Rect2 TextServerFallback::_font_get_glyph_uv_rect(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
2343 FontFallback *fd = _get_font_data(p_font_rid);
2344 ERR_FAIL_NULL_V(fd, Rect2());
2345
2346 MutexLock lock(fd->mutex);
2347 Vector2i size = _get_size_outline(fd, p_size);
2348
2349 FontForSizeFallback *ffsd = nullptr;
2350 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Rect2());
2351
2352 int mod = 0;
2353 if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
2354 TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get();
2355 if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
2356 mod = (layout << 24);
2357 }
2358 }
2359
2360 FontGlyph fgl;
2361 if (!_ensure_glyph(fd, size, p_glyph | mod, fgl)) {
2362 return Rect2(); // Invalid or non graphicl glyph, do not display errors.
2363 }
2364
2365 return fgl.uv_rect;
2366 }
2367
2368 void TextServerFallback::_font_set_glyph_uv_rect(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Rect2 &p_uv_rect) {
2369 FontFallback *fd = _get_font_data(p_font_rid);
2370 ERR_FAIL_NULL(fd);
2371
2372 MutexLock lock(fd->mutex);
2373 Vector2i size = _get_size_outline(fd, p_size);
2374
2375 FontForSizeFallback *ffsd = nullptr;
2376 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
2377
2378 FontGlyph &fgl = ffsd->glyph_map[p_glyph];
2379
2380 fgl.uv_rect = p_uv_rect;
2381 fgl.found = true;
2382 }
2383
2384 int64_t TextServerFallback::_font_get_glyph_texture_idx(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
2385 FontFallback *fd = _get_font_data(p_font_rid);
2386 ERR_FAIL_NULL_V(fd, -1);
2387
2388 MutexLock lock(fd->mutex);
2389 Vector2i size = _get_size_outline(fd, p_size);
2390
2391 FontForSizeFallback *ffsd = nullptr;
2392 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), -1);
2393
2394 int mod = 0;
2395 if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
2396 TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get();
2397 if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
2398 mod = (layout << 24);
2399 }
2400 }
2401
2402 FontGlyph fgl;
2403 if (!_ensure_glyph(fd, size, p_glyph | mod, fgl)) {
2404 return -1; // Invalid or non graphicl glyph, do not display errors.
2405 }
2406
2407 return fgl.texture_idx;
2408 }
2409
2410 void TextServerFallback::_font_set_glyph_texture_idx(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, int64_t p_texture_idx) {
2411 FontFallback *fd = _get_font_data(p_font_rid);
2412 ERR_FAIL_NULL(fd);
2413
2414 MutexLock lock(fd->mutex);
2415 Vector2i size = _get_size_outline(fd, p_size);
2416
2417 FontForSizeFallback *ffsd = nullptr;
2418 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
2419
2420 FontGlyph &fgl = ffsd->glyph_map[p_glyph];
2421
2422 fgl.texture_idx = p_texture_idx;
2423 fgl.found = true;
2424 }
2425
2426 RID TextServerFallback::_font_get_glyph_texture_rid(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
2427 FontFallback *fd = _get_font_data(p_font_rid);
2428 ERR_FAIL_NULL_V(fd, RID());
2429
2430 MutexLock lock(fd->mutex);
2431 Vector2i size = _get_size_outline(fd, p_size);
2432
2433 FontForSizeFallback *ffsd = nullptr;
2434 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), RID());
2435
2436 int mod = 0;
2437 if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
2438 TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get();
2439 if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
2440 mod = (layout << 24);
2441 }
2442 }
2443
2444 FontGlyph fgl;
2445 if (!_ensure_glyph(fd, size, p_glyph | mod, fgl)) {
2446 return RID(); // Invalid or non graphicl glyph, do not display errors.
2447 }
2448
2449 ERR_FAIL_COND_V(fgl.texture_idx < -1 || fgl.texture_idx >= ffsd->textures.size(), RID());
2450
2451 if (RenderingServer::get_singleton() != nullptr) {
2452 if (fgl.texture_idx != -1) {
2453 if (ffsd->textures[fgl.texture_idx].dirty) {
2454 ShelfPackTexture &tex = ffsd->textures.write[fgl.texture_idx];
2455 Ref<Image> img = tex.image;
2456 if (fgl.from_svg) {
2457 // Same as the "fix alpha border" process option when importing SVGs
2458 img->fix_alpha_edges();
2459 }
2460 if (fd->mipmaps && !img->has_mipmaps()) {
2461 img = tex.image->duplicate();
2462 img->generate_mipmaps();
2463 }
2464 if (tex.texture.is_null()) {
2465 tex.texture = ImageTexture::create_from_image(img);
2466 } else {
2467 tex.texture->update(img);
2468 }
2469 tex.dirty = false;
2470 }
2471 return ffsd->textures[fgl.texture_idx].texture->get_rid();
2472 }
2473 }
2474
2475 return RID();
2476 }
2477
2478 Size2 TextServerFallback::_font_get_glyph_texture_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const {
2479 FontFallback *fd = _get_font_data(p_font_rid);
2480 ERR_FAIL_NULL_V(fd, Size2());
2481
2482 MutexLock lock(fd->mutex);
2483 Vector2i size = _get_size_outline(fd, p_size);
2484
2485 FontForSizeFallback *ffsd = nullptr;
2486 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Size2());
2487
2488 int mod = 0;
2489 if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
2490 TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get();
2491 if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
2492 mod = (layout << 24);
2493 }
2494 }
2495
2496 FontGlyph fgl;
2497 if (!_ensure_glyph(fd, size, p_glyph | mod, fgl)) {
2498 return Size2(); // Invalid or non graphicl glyph, do not display errors.
2499 }
2500
2501 ERR_FAIL_COND_V(fgl.texture_idx < -1 || fgl.texture_idx >= ffsd->textures.size(), Size2());
2502
2503 if (RenderingServer::get_singleton() != nullptr) {
2504 if (fgl.texture_idx != -1) {
2505 if (ffsd->textures[fgl.texture_idx].dirty) {
2506 ShelfPackTexture &tex = ffsd->textures.write[fgl.texture_idx];
2507 Ref<Image> img = tex.image;
2508 if (fgl.from_svg) {
2509 // Same as the "fix alpha border" process option when importing SVGs
2510 img->fix_alpha_edges();
2511 }
2512 if (fd->mipmaps && !img->has_mipmaps()) {
2513 img = tex.image->duplicate();
2514 img->generate_mipmaps();
2515 }
2516 if (tex.texture.is_null()) {
2517 tex.texture = ImageTexture::create_from_image(img);
2518 } else {
2519 tex.texture->update(img);
2520 }
2521 tex.dirty = false;
2522 }
2523 return ffsd->textures[fgl.texture_idx].texture->get_size();
2524 }
2525 }
2526
2527 return Size2();
2528 }
2529
2530 Dictionary TextServerFallback::_font_get_glyph_contours(const RID &p_font_rid, int64_t p_size, int64_t p_index) const {
2531 FontFallback *fd = _get_font_data(p_font_rid);
2532 ERR_FAIL_NULL_V(fd, Dictionary());
2533
2534 MutexLock lock(fd->mutex);
2535 Vector2i size = _get_size(fd, p_size);
2536
2537 FontForSizeFallback *ffsd = nullptr;
2538 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Dictionary());
2539
2540 #ifdef MODULE_FREETYPE_ENABLED
2541 PackedVector3Array points;
2542 PackedInt32Array contours;
2543
2544 int32_t index = p_index & 0xffffff; // Remove subpixel shifts.
2545
2546 int error = FT_Load_Glyph(ffsd->face, FT_Get_Char_Index(ffsd->face, index), FT_LOAD_NO_BITMAP | (fd->force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0));
2547 ERR_FAIL_COND_V(error, Dictionary());
2548
2549 if (fd->embolden != 0.f) {
2550 FT_Pos strength = fd->embolden * size.x / 16; // 26.6 fractional units (1 / 64).
2551 FT_Outline_Embolden(&ffsd->face->glyph->outline, strength);
2552 }
2553
2554 if (fd->transform != Transform2D()) {
2555 FT_Matrix mat = { FT_Fixed(fd->transform[0][0] * 65536), FT_Fixed(fd->transform[0][1] * 65536), FT_Fixed(fd->transform[1][0] * 65536), FT_Fixed(fd->transform[1][1] * 65536) }; // 16.16 fractional units (1 / 65536).
2556 FT_Outline_Transform(&ffsd->face->glyph->outline, &mat);
2557 }
2558
2559 double scale = (1.0 / 64.0) * ffsd->scale;
2560 if (fd->msdf) {
2561 scale = scale * (double)p_size / (double)fd->msdf_source_size;
2562 } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) {
2563 if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
2564 scale = scale * (double)p_size / (double)fd->fixed_size;
2565 } else {
2566 scale = scale * Math::round((double)p_size / (double)fd->fixed_size);
2567 }
2568 }
2569 for (short i = 0; i < ffsd->face->glyph->outline.n_points; i++) {
2570 points.push_back(Vector3(ffsd->face->glyph->outline.points[i].x * scale, -ffsd->face->glyph->outline.points[i].y * scale, FT_CURVE_TAG(ffsd->face->glyph->outline.tags[i])));
2571 }
2572 for (short i = 0; i < ffsd->face->glyph->outline.n_contours; i++) {
2573 contours.push_back(ffsd->face->glyph->outline.contours[i]);
2574 }
2575 bool orientation = (FT_Outline_Get_Orientation(&ffsd->face->glyph->outline) == FT_ORIENTATION_FILL_RIGHT);
2576
2577 Dictionary out;
2578 out["points"] = points;
2579 out["contours"] = contours;
2580 out["orientation"] = orientation;
2581 return out;
2582 #else
2583 return Dictionary();
2584 #endif
2585 }
2586
2587 TypedArray<Vector2i> TextServerFallback::_font_get_kerning_list(const RID &p_font_rid, int64_t p_size) const {
2588 FontFallback *fd = _get_font_data(p_font_rid);
2589 ERR_FAIL_NULL_V(fd, TypedArray<Vector2i>());
2590
2591 MutexLock lock(fd->mutex);
2592 Vector2i size = _get_size(fd, p_size);
2593
2594 FontForSizeFallback *ffsd = nullptr;
2595 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), TypedArray<Vector2i>());
2596
2597 TypedArray<Vector2i> ret;
2598 for (const KeyValue<Vector2i, Vector2> &E : ffsd->kerning_map) {
2599 ret.push_back(E.key);
2600 }
2601 return ret;
2602 }
2603
2604 void TextServerFallback::_font_clear_kerning_map(const RID &p_font_rid, int64_t p_size) {
2605 FontFallback *fd = _get_font_data(p_font_rid);
2606 ERR_FAIL_NULL(fd);
2607
2608 MutexLock lock(fd->mutex);
2609 Vector2i size = _get_size(fd, p_size);
2610
2611 FontForSizeFallback *ffsd = nullptr;
2612 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
2613 ffsd->kerning_map.clear();
2614 }
2615
2616 void TextServerFallback::_font_remove_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair) {
2617 FontFallback *fd = _get_font_data(p_font_rid);
2618 ERR_FAIL_NULL(fd);
2619
2620 MutexLock lock(fd->mutex);
2621 Vector2i size = _get_size(fd, p_size);
2622
2623 FontForSizeFallback *ffsd = nullptr;
2624 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
2625 ffsd->kerning_map.erase(p_glyph_pair);
2626 }
2627
2628 void TextServerFallback::_font_set_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair, const Vector2 &p_kerning) {
2629 FontFallback *fd = _get_font_data(p_font_rid);
2630 ERR_FAIL_NULL(fd);
2631
2632 MutexLock lock(fd->mutex);
2633 Vector2i size = _get_size(fd, p_size);
2634
2635 FontForSizeFallback *ffsd = nullptr;
2636 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
2637 ffsd->kerning_map[p_glyph_pair] = p_kerning;
2638 }
2639
2640 Vector2 TextServerFallback::_font_get_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair) const {
2641 FontFallback *fd = _get_font_data(p_font_rid);
2642 ERR_FAIL_NULL_V(fd, Vector2());
2643
2644 MutexLock lock(fd->mutex);
2645 Vector2i size = _get_size(fd, p_size);
2646
2647 FontForSizeFallback *ffsd = nullptr;
2648 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Vector2());
2649
2650 const HashMap<Vector2i, Vector2> &kern = ffsd->kerning_map;
2651
2652 if (kern.has(p_glyph_pair)) {
2653 if (fd->msdf) {
2654 return kern[p_glyph_pair] * (double)p_size / (double)fd->msdf_source_size;
2655 } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) {
2656 if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
2657 return kern[p_glyph_pair] * (double)p_size / (double)fd->fixed_size;
2658 } else {
2659 return kern[p_glyph_pair] * Math::round((double)p_size / (double)fd->fixed_size);
2660 }
2661 } else {
2662 return kern[p_glyph_pair];
2663 }
2664 } else {
2665 #ifdef MODULE_FREETYPE_ENABLED
2666 if (ffsd->face) {
2667 FT_Vector delta;
2668 int32_t glyph_a = FT_Get_Char_Index(ffsd->face, p_glyph_pair.x);
2669 int32_t glyph_b = FT_Get_Char_Index(ffsd->face, p_glyph_pair.y);
2670 FT_Get_Kerning(ffsd->face, glyph_a, glyph_b, FT_KERNING_DEFAULT, &delta);
2671 if (fd->msdf) {
2672 return Vector2(delta.x, delta.y) * (double)p_size / (double)fd->msdf_source_size;
2673 } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) {
2674 if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
2675 return Vector2(delta.x, delta.y) * (double)p_size / (double)fd->fixed_size;
2676 } else {
2677 return Vector2(delta.x, delta.y) * Math::round((double)p_size / (double)fd->fixed_size);
2678 }
2679 } else {
2680 return Vector2(delta.x, delta.y);
2681 }
2682 }
2683 #endif
2684 }
2685 return Vector2();
2686 }
2687
2688 int64_t TextServerFallback::_font_get_glyph_index(const RID &p_font_rid, int64_t p_size, int64_t p_char, int64_t p_variation_selector) const {
2689 ERR_FAIL_COND_V_MSG((p_char >= 0xd800 && p_char <= 0xdfff) || (p_char > 0x10ffff), 0, "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_char, 16) + ".");
2690 return (int64_t)p_char;
2691 }
2692
2693 int64_t TextServerFallback::_font_get_char_from_glyph_index(const RID &p_font_rid, int64_t p_size, int64_t p_glyph_index) const {
2694 return p_glyph_index;
2695 }
2696
2697 bool TextServerFallback::_font_has_char(const RID &p_font_rid, int64_t p_char) const {
2698 FontFallback *fd = _get_font_data(p_font_rid);
2699 ERR_FAIL_COND_V_MSG((p_char >= 0xd800 && p_char <= 0xdfff) || (p_char > 0x10ffff), false, "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_char, 16) + ".");
2700 if (!fd) {
2701 return false;
2702 }
2703
2704 MutexLock lock(fd->mutex);
2705 FontForSizeFallback *ffsd = nullptr;
2706 if (fd->cache.is_empty()) {
2707 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, fd->msdf ? Vector2i(fd->msdf_source_size * 64, 0) : Vector2i(16 * 64, 0), ffsd), false);
2708 } else {
2709 ffsd = fd->cache.begin()->value;
2710 }
2711
2712 #ifdef MODULE_FREETYPE_ENABLED
2713 if (ffsd->face) {
2714 return FT_Get_Char_Index(ffsd->face, p_char) != 0;
2715 }
2716 #endif
2717 return ffsd->glyph_map.has((int32_t)p_char);
2718 }
2719
2720 String TextServerFallback::_font_get_supported_chars(const RID &p_font_rid) const {
2721 FontFallback *fd = _get_font_data(p_font_rid);
2722 ERR_FAIL_NULL_V(fd, String());
2723
2724 MutexLock lock(fd->mutex);
2725 FontForSizeFallback *ffsd = nullptr;
2726 if (fd->cache.is_empty()) {
2727 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, fd->msdf ? Vector2i(fd->msdf_source_size * 64, 0) : Vector2i(16 * 64, 0), ffsd), String());
2728 } else {
2729 ffsd = fd->cache.begin()->value;
2730 }
2731
2732 String chars;
2733 #ifdef MODULE_FREETYPE_ENABLED
2734 if (ffsd->face) {
2735 FT_UInt gindex;
2736 FT_ULong charcode = FT_Get_First_Char(ffsd->face, &gindex);
2737 while (gindex != 0) {
2738 if (charcode != 0) {
2739 chars = chars + String::chr(charcode);
2740 }
2741 charcode = FT_Get_Next_Char(ffsd->face, charcode, &gindex);
2742 }
2743 return chars;
2744 }
2745 #endif
2746 const HashMap<int32_t, FontGlyph> &gl = ffsd->glyph_map;
2747 for (const KeyValue<int32_t, FontGlyph> &E : gl) {
2748 chars = chars + String::chr(E.key);
2749 }
2750 return chars;
2751 }
2752
2753 PackedInt32Array TextServerFallback::_font_get_supported_glyphs(const RID &p_font_rid) const {
2754 FontFallback *fd = _get_font_data(p_font_rid);
2755 ERR_FAIL_NULL_V(fd, PackedInt32Array());
2756
2757 MutexLock lock(fd->mutex);
2758 FontForSizeFallback *at_size = nullptr;
2759 if (fd->cache.is_empty()) {
2760 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, fd->msdf ? Vector2i(fd->msdf_source_size * 64, 0) : Vector2i(16 * 64, 0), at_size), PackedInt32Array());
2761 } else {
2762 at_size = fd->cache.begin()->value;
2763 }
2764
2765 PackedInt32Array glyphs;
2766 #ifdef MODULE_FREETYPE_ENABLED
2767 if (at_size && at_size->face) {
2768 FT_UInt gindex;
2769 FT_ULong charcode = FT_Get_First_Char(at_size->face, &gindex);
2770 while (gindex != 0) {
2771 glyphs.push_back(gindex);
2772 charcode = FT_Get_Next_Char(at_size->face, charcode, &gindex);
2773 }
2774 return glyphs;
2775 }
2776 #endif
2777 if (at_size) {
2778 const HashMap<int32_t, FontGlyph> &gl = at_size->glyph_map;
2779 for (const KeyValue<int32_t, FontGlyph> &E : gl) {
2780 glyphs.push_back(E.key);
2781 }
2782 }
2783 return glyphs;
2784 }
2785
2786 void TextServerFallback::_font_render_range(const RID &p_font_rid, const Vector2i &p_size, int64_t p_start, int64_t p_end) {
2787 FontFallback *fd = _get_font_data(p_font_rid);
2788 ERR_FAIL_NULL(fd);
2789 ERR_FAIL_COND_MSG((p_start >= 0xd800 && p_start <= 0xdfff) || (p_start > 0x10ffff), "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_start, 16) + ".");
2790 ERR_FAIL_COND_MSG((p_end >= 0xd800 && p_end <= 0xdfff) || (p_end > 0x10ffff), "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_end, 16) + ".");
2791
2792 MutexLock lock(fd->mutex);
2793 Vector2i size = _get_size_outline(fd, p_size);
2794 FontForSizeFallback *ffsd = nullptr;
2795 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
2796 for (int64_t i = p_start; i <= p_end; i++) {
2797 #ifdef MODULE_FREETYPE_ENABLED
2798 int32_t idx = i;
2799 if (ffsd->face) {
2800 FontGlyph fgl;
2801 if (fd->msdf) {
2802 _ensure_glyph(fd, size, (int32_t)idx, fgl);
2803 } else {
2804 for (int aa = 0; aa < ((fd->antialiasing == FONT_ANTIALIASING_LCD) ? FONT_LCD_SUBPIXEL_LAYOUT_MAX : 1); aa++) {
2805 if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE * 64)) {
2806 _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24), fgl);
2807 _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24), fgl);
2808 _ensure_glyph(fd, size, (int32_t)idx | (2 << 27) | (aa << 24), fgl);
2809 _ensure_glyph(fd, size, (int32_t)idx | (3 << 27) | (aa << 24), fgl);
2810 } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE * 64)) {
2811 _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24), fgl);
2812 _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24), fgl);
2813 } else {
2814 _ensure_glyph(fd, size, (int32_t)idx | (aa << 24), fgl);
2815 }
2816 }
2817 }
2818 }
2819 #endif
2820 }
2821 }
2822
2823 void TextServerFallback::_font_render_glyph(const RID &p_font_rid, const Vector2i &p_size, int64_t p_index) {
2824 FontFallback *fd = _get_font_data(p_font_rid);
2825 ERR_FAIL_NULL(fd);
2826
2827 MutexLock lock(fd->mutex);
2828 Vector2i size = _get_size_outline(fd, p_size);
2829 FontForSizeFallback *ffsd = nullptr;
2830 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
2831 #ifdef MODULE_FREETYPE_ENABLED
2832 int32_t idx = p_index & 0xffffff; // Remove subpixel shifts.
2833 if (ffsd->face) {
2834 FontGlyph fgl;
2835 if (fd->msdf) {
2836 _ensure_glyph(fd, size, (int32_t)idx, fgl);
2837 } else {
2838 for (int aa = 0; aa < ((fd->antialiasing == FONT_ANTIALIASING_LCD) ? FONT_LCD_SUBPIXEL_LAYOUT_MAX : 1); aa++) {
2839 if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE * 64)) {
2840 _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24), fgl);
2841 _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24), fgl);
2842 _ensure_glyph(fd, size, (int32_t)idx | (2 << 27) | (aa << 24), fgl);
2843 _ensure_glyph(fd, size, (int32_t)idx | (3 << 27) | (aa << 24), fgl);
2844 } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE * 64)) {
2845 _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24), fgl);
2846 _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24), fgl);
2847 } else {
2848 _ensure_glyph(fd, size, (int32_t)idx | (aa << 24), fgl);
2849 }
2850 }
2851 }
2852 }
2853 #endif
2854 }
2855
2856 void TextServerFallback::_font_draw_glyph(const RID &p_font_rid, const RID &p_canvas, int64_t p_size, const Vector2 &p_pos, int64_t p_index, const Color &p_color, float p_oversampling) const {
2857 if (p_index == 0) {
2858 return; // Non visual character, skip.
2859 }
2860 FontFallback *fd = _get_font_data(p_font_rid);
2861 ERR_FAIL_NULL(fd);
2862
2863 MutexLock lock(fd->mutex);
2864
2865 // Oversampling.
2866 bool viewport_oversampling = false;
2867 float oversampling_factor = p_oversampling;
2868 if (p_oversampling <= 0.0) {
2869 if (fd->oversampling_override > 0.0) {
2870 oversampling_factor = fd->oversampling_override;
2871 } else if (vp_oversampling > 0.0) {
2872 oversampling_factor = vp_oversampling;
2873 viewport_oversampling = true;
2874 } else {
2875 oversampling_factor = 1.0;
2876 }
2877 }
2878 bool skip_oversampling = fd->msdf || fd->fixed_size > 0;
2879 if (skip_oversampling) {
2880 oversampling_factor = 1.0;
2881 } else {
2882 uint64_t oversampling_level = CLAMP(oversampling_factor, 0.1, 100.0) * 64;
2883 oversampling_factor = double(oversampling_level) / 64.0;
2884 }
2885
2886 Vector2i size;
2887 if (skip_oversampling) {
2888 size = _get_size(fd, p_size);
2889 } else {
2890 size = Vector2i(p_size * 64 * oversampling_factor, 0);
2891 }
2892
2893 FontForSizeFallback *ffsd = nullptr;
2894 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd, false, viewport_oversampling ? 64 * oversampling_factor : 0));
2895
2896 int32_t index = p_index & 0xffffff; // Remove subpixel shifts.
2897 bool lcd_aa = false;
2898
2899 #ifdef MODULE_FREETYPE_ENABLED
2900 if (!fd->msdf && ffsd->face) {
2901 // LCD layout, bits 24, 25, 26
2902 if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
2903 TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get();
2904 if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
2905 lcd_aa = true;
2906 index = index | (layout << 24);
2907 }
2908 }
2909 // Subpixel X-shift, bits 27, 28
2910 if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE * 64)) {
2911 int xshift = (int)(Math::floor(4 * (p_pos.x + 0.125)) - 4 * Math::floor(p_pos.x + 0.125));
2912 index = index | (xshift << 27);
2913 } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE * 64)) {
2914 int xshift = (int)(Math::floor(2 * (p_pos.x + 0.25)) - 2 * Math::floor(p_pos.x + 0.25));
2915 index = index | (xshift << 27);
2916 }
2917 }
2918 #endif
2919
2920 FontGlyph fgl;
2921 if (!_ensure_glyph(fd, size, index, fgl, viewport_oversampling ? 64 * oversampling_factor : 0)) {
2922 return; // Invalid or non-graphical glyph, do not display errors, nothing to draw.
2923 }
2924
2925 if (fgl.found) {
2926 ERR_FAIL_COND(fgl.texture_idx < -1 || fgl.texture_idx >= ffsd->textures.size());
2927
2928 if (fgl.texture_idx != -1) {
2929 Color modulate = p_color;
2930 #ifdef MODULE_FREETYPE_ENABLED
2931 if (!fd->modulate_color_glyphs && ffsd->face && ffsd->textures[fgl.texture_idx].image.is_valid() && (ffsd->textures[fgl.texture_idx].image->get_format() == Image::FORMAT_RGBA8) && !lcd_aa && !fd->msdf) {
2932 modulate.r = modulate.g = modulate.b = 1.0;
2933 }
2934 #endif
2935 if (RenderingServer::get_singleton() != nullptr) {
2936 if (ffsd->textures[fgl.texture_idx].dirty) {
2937 ShelfPackTexture &tex = ffsd->textures.write[fgl.texture_idx];
2938 Ref<Image> img = tex.image;
2939 if (fgl.from_svg) {
2940 // Same as the "fix alpha border" process option when importing SVGs
2941 img->fix_alpha_edges();
2942 }
2943 if (fd->mipmaps && !img->has_mipmaps()) {
2944 img = tex.image->duplicate();
2945 img->generate_mipmaps();
2946 }
2947 if (tex.texture.is_null()) {
2948 tex.texture = ImageTexture::create_from_image(img);
2949 } else {
2950 tex.texture->update(img);
2951 }
2952 tex.dirty = false;
2953 }
2954 RID texture = ffsd->textures[fgl.texture_idx].texture->get_rid();
2955 if (fd->msdf) {
2956 Point2 cpos = p_pos;
2957 cpos += fgl.rect.position * (double)p_size / (double)fd->msdf_source_size;
2958 Size2 csize = fgl.rect.size * (double)p_size / (double)fd->msdf_source_size;
2959 RenderingServer::get_singleton()->canvas_item_add_msdf_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, fgl.uv_rect, modulate, 0, fd->msdf_range, (double)p_size / (double)fd->msdf_source_size);
2960 } else {
2961 Point2 cpos = p_pos;
2962 double scale = _font_get_scale(p_font_rid, p_size) / oversampling_factor;
2963 if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
2964 cpos.x = cpos.x + 0.125;
2965 } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
2966 cpos.x = cpos.x + 0.25;
2967 }
2968 if (scale == 1.0) {
2969 cpos.y = Math::floor(cpos.y);
2970 cpos.x = Math::floor(cpos.x);
2971 }
2972 Vector2 gpos = fgl.rect.position;
2973 Size2 csize = fgl.rect.size;
2974 if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE) {
2975 if (size.x != p_size * 64) {
2976 if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
2977 double gl_scale = (double)p_size / (double)fd->fixed_size;
2978 gpos *= gl_scale;
2979 csize *= gl_scale;
2980 } else {
2981 double gl_scale = Math::round((double)p_size / (double)fd->fixed_size);
2982 gpos *= gl_scale;
2983 csize *= gl_scale;
2984 }
2985 }
2986 } else {
2987 gpos /= oversampling_factor;
2988 csize /= oversampling_factor;
2989 }
2990 cpos += gpos;
2991 if (lcd_aa) {
2992 RenderingServer::get_singleton()->canvas_item_add_lcd_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, fgl.uv_rect, modulate);
2993 } else {
2994 RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, fgl.uv_rect, modulate, false, false);
2995 }
2996 }
2997 }
2998 }
2999 }
3000 }
3001
3002 void TextServerFallback::_font_draw_glyph_outline(const RID &p_font_rid, const RID &p_canvas, int64_t p_size, int64_t p_outline_size, const Vector2 &p_pos, int64_t p_index, const Color &p_color, float p_oversampling) const {
3003 if (p_index == 0) {
3004 return; // Non visual character, skip.
3005 }
3006 FontFallback *fd = _get_font_data(p_font_rid);
3007 ERR_FAIL_NULL(fd);
3008
3009 MutexLock lock(fd->mutex);
3010
3011 // Oversampling.
3012 bool viewport_oversampling = false;
3013 float oversampling_factor = p_oversampling;
3014 if (p_oversampling <= 0.0) {
3015 if (fd->oversampling_override > 0.0) {
3016 oversampling_factor = fd->oversampling_override;
3017 } else if (vp_oversampling > 0.0) {
3018 oversampling_factor = vp_oversampling;
3019 viewport_oversampling = true;
3020 } else {
3021 oversampling_factor = 1.0;
3022 }
3023 }
3024 bool skip_oversampling = fd->msdf || fd->fixed_size > 0;
3025 if (skip_oversampling) {
3026 oversampling_factor = 1.0;
3027 } else {
3028 uint64_t oversampling_level = CLAMP(oversampling_factor, 0.1, 100.0) * 64;
3029 oversampling_factor = double(oversampling_level) / 64.0;
3030 }
3031
3032 Vector2i size;
3033 if (skip_oversampling) {
3034 size = _get_size_outline(fd, Vector2i(p_size, p_outline_size));
3035 } else {
3036 size = Vector2i(p_size * 64 * oversampling_factor, p_outline_size * oversampling_factor);
3037 }
3038
3039 FontForSizeFallback *ffsd = nullptr;
3040 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd, false, viewport_oversampling ? 64 * oversampling_factor : 0));
3041
3042 int32_t index = p_index & 0xffffff; // Remove subpixel shifts.
3043 bool lcd_aa = false;
3044
3045 #ifdef MODULE_FREETYPE_ENABLED
3046 if (!fd->msdf && ffsd->face) {
3047 // LCD layout, bits 24, 25, 26
3048 if (fd->antialiasing == FONT_ANTIALIASING_LCD) {
3049 TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get();
3050 if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) {
3051 lcd_aa = true;
3052 index = index | (layout << 24);
3053 }
3054 }
3055 // Subpixel X-shift, bits 27, 28
3056 if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE * 64)) {
3057 int xshift = (int)(Math::floor(4 * (p_pos.x + 0.125)) - 4 * Math::floor(p_pos.x + 0.125));
3058 index = index | (xshift << 27);
3059 } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE * 64)) {
3060 int xshift = (int)(Math::floor(2 * (p_pos.x + 0.25)) - 2 * Math::floor(p_pos.x + 0.25));
3061 index = index | (xshift << 27);
3062 }
3063 }
3064 #endif
3065
3066 FontGlyph fgl;
3067 if (!_ensure_glyph(fd, size, index, fgl, viewport_oversampling ? 64 * oversampling_factor : 0)) {
3068 return; // Invalid or non-graphical glyph, do not display errors, nothing to draw.
3069 }
3070
3071 if (fgl.found) {
3072 ERR_FAIL_COND(fgl.texture_idx < -1 || fgl.texture_idx >= ffsd->textures.size());
3073
3074 if (fgl.texture_idx != -1) {
3075 Color modulate = p_color;
3076 #ifdef MODULE_FREETYPE_ENABLED
3077 if (ffsd->face && ffsd->textures[fgl.texture_idx].image.is_valid() && (ffsd->textures[fgl.texture_idx].image->get_format() == Image::FORMAT_RGBA8) && !lcd_aa && !fd->msdf) {
3078 modulate.r = modulate.g = modulate.b = 1.0;
3079 }
3080 #endif
3081 if (RenderingServer::get_singleton() != nullptr) {
3082 if (ffsd->textures[fgl.texture_idx].dirty) {
3083 ShelfPackTexture &tex = ffsd->textures.write[fgl.texture_idx];
3084 Ref<Image> img = tex.image;
3085 if (fd->mipmaps && !img->has_mipmaps()) {
3086 img = tex.image->duplicate();
3087 img->generate_mipmaps();
3088 }
3089 if (tex.texture.is_null()) {
3090 tex.texture = ImageTexture::create_from_image(img);
3091 } else {
3092 tex.texture->update(img);
3093 }
3094 tex.dirty = false;
3095 }
3096 RID texture = ffsd->textures[fgl.texture_idx].texture->get_rid();
3097 if (fd->msdf) {
3098 Point2 cpos = p_pos;
3099 cpos += fgl.rect.position * (double)p_size / (double)fd->msdf_source_size;
3100 Size2 csize = fgl.rect.size * (double)p_size / (double)fd->msdf_source_size;
3101 RenderingServer::get_singleton()->canvas_item_add_msdf_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, fgl.uv_rect, modulate, p_outline_size, fd->msdf_range, (double)p_size / (double)fd->msdf_source_size);
3102 } else {
3103 Point2 cpos = p_pos;
3104 double scale = _font_get_scale(p_font_rid, p_size) / oversampling_factor;
3105 if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE)) {
3106 cpos.x = cpos.x + 0.125;
3107 } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE)) {
3108 cpos.x = cpos.x + 0.25;
3109 }
3110 if (scale == 1.0) {
3111 cpos.y = Math::floor(cpos.y);
3112 cpos.x = Math::floor(cpos.x);
3113 }
3114 Vector2 gpos = fgl.rect.position;
3115 Size2 csize = fgl.rect.size;
3116 if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE) {
3117 if (size.x != p_size * 64) {
3118 if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) {
3119 double gl_scale = (double)p_size / (double)fd->fixed_size;
3120 gpos *= gl_scale;
3121 csize *= gl_scale;
3122 } else {
3123 double gl_scale = Math::round((double)p_size / (double)fd->fixed_size);
3124 gpos *= gl_scale;
3125 csize *= gl_scale;
3126 }
3127 }
3128 } else {
3129 gpos /= oversampling_factor;
3130 csize /= oversampling_factor;
3131 }
3132 cpos += gpos;
3133 if (lcd_aa) {
3134 RenderingServer::get_singleton()->canvas_item_add_lcd_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, fgl.uv_rect, modulate);
3135 } else {
3136 RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, fgl.uv_rect, modulate, false, false);
3137 }
3138 }
3139 }
3140 }
3141 }
3142 }
3143
3144 bool TextServerFallback::_font_is_language_supported(const RID &p_font_rid, const String &p_language) const {
3145 FontFallback *fd = _get_font_data(p_font_rid);
3146 ERR_FAIL_NULL_V(fd, false);
3147
3148 MutexLock lock(fd->mutex);
3149 if (fd->language_support_overrides.has(p_language)) {
3150 return fd->language_support_overrides[p_language];
3151 } else {
3152 if (fd->language_support_overrides.has("*")) {
3153 return fd->language_support_overrides["*"];
3154 }
3155 return true;
3156 }
3157 }
3158
3159 void TextServerFallback::_font_set_language_support_override(const RID &p_font_rid, const String &p_language, bool p_supported) {
3160 FontFallback *fd = _get_font_data(p_font_rid);
3161 ERR_FAIL_NULL(fd);
3162
3163 MutexLock lock(fd->mutex);
3164 fd->language_support_overrides[p_language] = p_supported;
3165 }
3166
3167 bool TextServerFallback::_font_get_language_support_override(const RID &p_font_rid, const String &p_language) {
3168 FontFallback *fd = _get_font_data(p_font_rid);
3169 ERR_FAIL_NULL_V(fd, false);
3170
3171 MutexLock lock(fd->mutex);
3172 return fd->language_support_overrides[p_language];
3173 }
3174
3175 void TextServerFallback::_font_remove_language_support_override(const RID &p_font_rid, const String &p_language) {
3176 FontFallback *fd = _get_font_data(p_font_rid);
3177 ERR_FAIL_NULL(fd);
3178
3179 MutexLock lock(fd->mutex);
3180 fd->language_support_overrides.erase(p_language);
3181 }
3182
3183 PackedStringArray TextServerFallback::_font_get_language_support_overrides(const RID &p_font_rid) {
3184 FontFallback *fd = _get_font_data(p_font_rid);
3185 ERR_FAIL_NULL_V(fd, PackedStringArray());
3186
3187 MutexLock lock(fd->mutex);
3188 PackedStringArray out;
3189 for (const KeyValue<String, bool> &E : fd->language_support_overrides) {
3190 out.push_back(E.key);
3191 }
3192 return out;
3193 }
3194
3195 bool TextServerFallback::_font_is_script_supported(const RID &p_font_rid, const String &p_script) const {
3196 FontFallback *fd = _get_font_data(p_font_rid);
3197 ERR_FAIL_NULL_V(fd, false);
3198
3199 MutexLock lock(fd->mutex);
3200 if (fd->script_support_overrides.has(p_script)) {
3201 return fd->script_support_overrides[p_script];
3202 } else {
3203 if (fd->script_support_overrides.has("*")) {
3204 return fd->script_support_overrides["*"];
3205 }
3206 return true;
3207 }
3208 }
3209
3210 void TextServerFallback::_font_set_script_support_override(const RID &p_font_rid, const String &p_script, bool p_supported) {
3211 FontFallback *fd = _get_font_data(p_font_rid);
3212 ERR_FAIL_NULL(fd);
3213
3214 MutexLock lock(fd->mutex);
3215 fd->script_support_overrides[p_script] = p_supported;
3216 }
3217
3218 bool TextServerFallback::_font_get_script_support_override(const RID &p_font_rid, const String &p_script) {
3219 FontFallback *fd = _get_font_data(p_font_rid);
3220 ERR_FAIL_NULL_V(fd, false);
3221
3222 MutexLock lock(fd->mutex);
3223 return fd->script_support_overrides[p_script];
3224 }
3225
3226 void TextServerFallback::_font_remove_script_support_override(const RID &p_font_rid, const String &p_script) {
3227 FontFallback *fd = _get_font_data(p_font_rid);
3228 ERR_FAIL_NULL(fd);
3229
3230 MutexLock lock(fd->mutex);
3231 Vector2i size = _get_size(fd, 16);
3232 FontForSizeFallback *ffsd = nullptr;
3233 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
3234 fd->script_support_overrides.erase(p_script);
3235 }
3236
3237 PackedStringArray TextServerFallback::_font_get_script_support_overrides(const RID &p_font_rid) {
3238 FontFallback *fd = _get_font_data(p_font_rid);
3239 ERR_FAIL_NULL_V(fd, PackedStringArray());
3240
3241 MutexLock lock(fd->mutex);
3242 PackedStringArray out;
3243 for (const KeyValue<String, bool> &E : fd->script_support_overrides) {
3244 out.push_back(E.key);
3245 }
3246 return out;
3247 }
3248
3249 void TextServerFallback::_font_set_opentype_feature_overrides(const RID &p_font_rid, const Dictionary &p_overrides) {
3250 FontFallback *fd = _get_font_data(p_font_rid);
3251 ERR_FAIL_NULL(fd);
3252
3253 MutexLock lock(fd->mutex);
3254 Vector2i size = _get_size(fd, 16);
3255 FontForSizeFallback *ffsd = nullptr;
3256 ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd));
3257 fd->feature_overrides = p_overrides;
3258 }
3259
3260 Dictionary TextServerFallback::_font_get_opentype_feature_overrides(const RID &p_font_rid) const {
3261 FontFallback *fd = _get_font_data(p_font_rid);
3262 ERR_FAIL_NULL_V(fd, Dictionary());
3263
3264 MutexLock lock(fd->mutex);
3265 return fd->feature_overrides;
3266 }
3267
3268 Dictionary TextServerFallback::_font_supported_feature_list(const RID &p_font_rid) const {
3269 return Dictionary();
3270 }
3271
3272 Dictionary TextServerFallback::_font_supported_variation_list(const RID &p_font_rid) const {
3273 FontFallback *fd = _get_font_data(p_font_rid);
3274 ERR_FAIL_NULL_V(fd, Dictionary());
3275
3276 MutexLock lock(fd->mutex);
3277 Vector2i size = _get_size(fd, 16);
3278 FontForSizeFallback *ffsd = nullptr;
3279 ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Dictionary());
3280 return fd->supported_varaitions;
3281 }
3282
3283 /*************************************************************************/
3284 /* Shaped text buffer interface */
3285 /*************************************************************************/
3286
3287 void TextServerFallback::invalidate(ShapedTextDataFallback *p_shaped) {
3288 p_shaped->valid.clear();
3289 p_shaped->sort_valid = false;
3290 p_shaped->line_breaks_valid = false;
3291 p_shaped->justification_ops_valid = false;
3292 p_shaped->ascent = 0.0;
3293 p_shaped->descent = 0.0;
3294 p_shaped->width = 0.0;
3295 p_shaped->upos = 0.0;
3296 p_shaped->uthk = 0.0;
3297 p_shaped->glyphs.clear();
3298 p_shaped->glyphs_logical.clear();
3299 p_shaped->runs.clear();
3300 p_shaped->runs_dirty = true;
3301 }
3302
3303 void TextServerFallback::full_copy(ShapedTextDataFallback *p_shaped) {
3304 ShapedTextDataFallback *parent = shaped_owner.get_or_null(p_shaped->parent);
3305
3306 for (const KeyValue<Variant, ShapedTextDataFallback::EmbeddedObject> &E : parent->objects) {
3307 if (E.value.start >= p_shaped->start && E.value.start < p_shaped->end) {
3308 p_shaped->objects[E.key] = E.value;
3309 }
3310 }
3311
3312 for (int i = MAX(0, p_shaped->first_span); i <= MIN(p_shaped->last_span, parent->spans.size() - 1); i++) {
3313 ShapedTextDataFallback::Span span = parent->spans[i];
3314 span.start = MAX(p_shaped->start, span.start);
3315 span.end = MIN(p_shaped->end, span.end);
3316 p_shaped->spans.push_back(span);
3317 }
3318 p_shaped->first_span = 0;
3319 p_shaped->last_span = 0;
3320
3321 p_shaped->parent = RID();
3322 }
3323
3324 RID TextServerFallback::_create_shaped_text(TextServer::Direction p_direction, TextServer::Orientation p_orientation) {
3325 _THREAD_SAFE_METHOD_
3326 ERR_FAIL_COND_V_MSG(p_direction == DIRECTION_INHERITED, RID(), "Invalid text direction.");
3327
3328 ShapedTextDataFallback *sd = memnew(ShapedTextDataFallback);
3329 sd->direction = p_direction;
3330 sd->orientation = p_orientation;
3331
3332 return shaped_owner.make_rid(sd);
3333 }
3334
3335 void TextServerFallback::_shaped_text_clear(const RID &p_shaped) {
3336 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3337 ERR_FAIL_NULL(sd);
3338
3339 MutexLock lock(sd->mutex);
3340 sd->parent = RID();
3341 sd->start = 0;
3342 sd->end = 0;
3343 sd->text = String();
3344 sd->spans.clear();
3345 sd->first_span = 0;
3346 sd->last_span = 0;
3347 sd->objects.clear();
3348 invalidate(sd);
3349 }
3350
3351 RID TextServerFallback::_shaped_text_duplicate(const RID &p_shaped) {
3352 _THREAD_SAFE_METHOD_
3353
3354 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3355 ERR_FAIL_NULL_V(sd, RID());
3356
3357 MutexLock lock(sd->mutex);
3358
3359 ShapedTextDataFallback *new_sd = memnew(ShapedTextDataFallback);
3360 new_sd->parent = p_shaped;
3361 new_sd->start = sd->start;
3362 new_sd->end = sd->end;
3363 new_sd->first_span = sd->first_span;
3364 new_sd->last_span = sd->last_span;
3365 new_sd->text = sd->text;
3366 new_sd->orientation = sd->orientation;
3367 new_sd->direction = sd->direction;
3368 new_sd->custom_punct = sd->custom_punct;
3369 new_sd->para_direction = sd->para_direction;
3370 new_sd->line_breaks_valid = sd->line_breaks_valid;
3371 new_sd->justification_ops_valid = sd->justification_ops_valid;
3372 new_sd->sort_valid = false;
3373 new_sd->upos = sd->upos;
3374 new_sd->uthk = sd->uthk;
3375 new_sd->runs.clear();
3376 new_sd->runs_dirty = true;
3377 for (int i = 0; i < TextServer::SPACING_MAX; i++) {
3378 new_sd->extra_spacing[i] = sd->extra_spacing[i];
3379 }
3380 for (const KeyValue<Variant, ShapedTextDataFallback::EmbeddedObject> &E : sd->objects) {
3381 new_sd->objects[E.key] = E.value;
3382 }
3383 for (int i = 0; i < sd->spans.size(); i++) {
3384 new_sd->spans.push_back(sd->spans[i]);
3385 }
3386 new_sd->valid.clear();
3387
3388 return shaped_owner.make_rid(new_sd);
3389 }
3390
3391 void TextServerFallback::_shaped_text_set_direction(const RID &p_shaped, TextServer::Direction p_direction) {
3392 ERR_FAIL_COND_MSG(p_direction == DIRECTION_INHERITED, "Invalid text direction.");
3393 if (p_direction == DIRECTION_RTL) {
3394 ERR_PRINT_ONCE("Right-to-left layout is not supported by this text server.");
3395 }
3396 }
3397
3398 TextServer::Direction TextServerFallback::_shaped_text_get_direction(const RID &p_shaped) const {
3399 return TextServer::DIRECTION_LTR;
3400 }
3401
3402 TextServer::Direction TextServerFallback::_shaped_text_get_inferred_direction(const RID &p_shaped) const {
3403 return TextServer::DIRECTION_LTR;
3404 }
3405
3406 void TextServerFallback::_shaped_text_set_custom_punctuation(const RID &p_shaped, const String &p_punct) {
3407 _THREAD_SAFE_METHOD_
3408 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3409 ERR_FAIL_NULL(sd);
3410
3411 if (sd->custom_punct != p_punct) {
3412 if (sd->parent != RID()) {
3413 full_copy(sd);
3414 }
3415 sd->custom_punct = p_punct;
3416 invalidate(sd);
3417 }
3418 }
3419
3420 String TextServerFallback::_shaped_text_get_custom_punctuation(const RID &p_shaped) const {
3421 _THREAD_SAFE_METHOD_
3422 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3423 ERR_FAIL_NULL_V(sd, String());
3424 return sd->custom_punct;
3425 }
3426
3427 void TextServerFallback::_shaped_text_set_custom_ellipsis(const RID &p_shaped, int64_t p_char) {
3428 _THREAD_SAFE_METHOD_
3429 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3430 ERR_FAIL_NULL(sd);
3431 sd->el_char = p_char;
3432 }
3433
3434 int64_t TextServerFallback::_shaped_text_get_custom_ellipsis(const RID &p_shaped) const {
3435 _THREAD_SAFE_METHOD_
3436 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3437 ERR_FAIL_NULL_V(sd, 0);
3438 return sd->el_char;
3439 }
3440
3441 void TextServerFallback::_shaped_text_set_orientation(const RID &p_shaped, TextServer::Orientation p_orientation) {
3442 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3443 ERR_FAIL_NULL(sd);
3444
3445 MutexLock lock(sd->mutex);
3446 if (sd->orientation != p_orientation) {
3447 if (sd->parent != RID()) {
3448 full_copy(sd);
3449 }
3450 sd->orientation = p_orientation;
3451 invalidate(sd);
3452 }
3453 }
3454
3455 void TextServerFallback::_shaped_text_set_bidi_override(const RID &p_shaped, const Array &p_override) {
3456 // No BiDi support, ignore.
3457 }
3458
3459 TextServer::Orientation TextServerFallback::_shaped_text_get_orientation(const RID &p_shaped) const {
3460 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3461 ERR_FAIL_NULL_V(sd, TextServer::ORIENTATION_HORIZONTAL);
3462
3463 MutexLock lock(sd->mutex);
3464 return sd->orientation;
3465 }
3466
3467 void TextServerFallback::_shaped_text_set_preserve_invalid(const RID &p_shaped, bool p_enabled) {
3468 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3469
3470 MutexLock lock(sd->mutex);
3471 ERR_FAIL_NULL(sd);
3472 if (sd->preserve_invalid != p_enabled) {
3473 if (sd->parent != RID()) {
3474 full_copy(sd);
3475 }
3476 sd->preserve_invalid = p_enabled;
3477 invalidate(sd);
3478 }
3479 }
3480
3481 bool TextServerFallback::_shaped_text_get_preserve_invalid(const RID &p_shaped) const {
3482 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3483 ERR_FAIL_NULL_V(sd, false);
3484
3485 MutexLock lock(sd->mutex);
3486 return sd->preserve_invalid;
3487 }
3488
3489 void TextServerFallback::_shaped_text_set_preserve_control(const RID &p_shaped, bool p_enabled) {
3490 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3491 ERR_FAIL_NULL(sd);
3492
3493 MutexLock lock(sd->mutex);
3494 if (sd->preserve_control != p_enabled) {
3495 if (sd->parent != RID()) {
3496 full_copy(sd);
3497 }
3498 sd->preserve_control = p_enabled;
3499 invalidate(sd);
3500 }
3501 }
3502
3503 bool TextServerFallback::_shaped_text_get_preserve_control(const RID &p_shaped) const {
3504 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3505 ERR_FAIL_NULL_V(sd, false);
3506
3507 MutexLock lock(sd->mutex);
3508 return sd->preserve_control;
3509 }
3510
3511 void TextServerFallback::_shaped_text_set_spacing(const RID &p_shaped, SpacingType p_spacing, int64_t p_value) {
3512 ERR_FAIL_INDEX((int)p_spacing, 4);
3513 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3514 ERR_FAIL_NULL(sd);
3515
3516 MutexLock lock(sd->mutex);
3517 if (sd->extra_spacing[p_spacing] != p_value) {
3518 if (sd->parent != RID()) {
3519 full_copy(sd);
3520 }
3521 sd->extra_spacing[p_spacing] = p_value;
3522 invalidate(sd);
3523 }
3524 }
3525
3526 int64_t TextServerFallback::_shaped_text_get_spacing(const RID &p_shaped, SpacingType p_spacing) const {
3527 ERR_FAIL_INDEX_V((int)p_spacing, 4, 0);
3528
3529 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3530 ERR_FAIL_NULL_V(sd, 0);
3531
3532 MutexLock lock(sd->mutex);
3533 return sd->extra_spacing[p_spacing];
3534 }
3535
3536 int64_t TextServerFallback::_shaped_get_span_count(const RID &p_shaped) const {
3537 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3538 ERR_FAIL_NULL_V(sd, 0);
3539
3540 if (sd->parent != RID()) {
3541 return sd->last_span - sd->first_span + 1;
3542 } else {
3543 return sd->spans.size();
3544 }
3545 }
3546
3547 Variant TextServerFallback::_shaped_get_span_meta(const RID &p_shaped, int64_t p_index) const {
3548 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3549 ERR_FAIL_NULL_V(sd, Variant());
3550 if (sd->parent != RID()) {
3551 ShapedTextDataFallback *parent_sd = shaped_owner.get_or_null(sd->parent);
3552 ERR_FAIL_COND_V(!parent_sd->valid.is_set(), Variant());
3553 ERR_FAIL_INDEX_V(p_index + sd->first_span, parent_sd->spans.size(), Variant());
3554 return parent_sd->spans[p_index + sd->first_span].meta;
3555 } else {
3556 ERR_FAIL_INDEX_V(p_index, sd->spans.size(), Variant());
3557 return sd->spans[p_index].meta;
3558 }
3559 }
3560
3561 Variant TextServerFallback::_shaped_get_span_embedded_object(const RID &p_shaped, int64_t p_index) const {
3562 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3563 ERR_FAIL_NULL_V(sd, Variant());
3564 if (sd->parent != RID()) {
3565 ShapedTextDataFallback *parent_sd = shaped_owner.get_or_null(sd->parent);
3566 ERR_FAIL_COND_V(!parent_sd->valid.is_set(), Variant());
3567 ERR_FAIL_INDEX_V(p_index + sd->first_span, parent_sd->spans.size(), Variant());
3568 return parent_sd->spans[p_index + sd->first_span].embedded_key;
3569 } else {
3570 ERR_FAIL_INDEX_V(p_index, sd->spans.size(), Variant());
3571 return sd->spans[p_index].embedded_key;
3572 }
3573 }
3574
3575 String TextServerFallback::_shaped_get_span_text(const RID &p_shaped, int64_t p_index) const {
3576 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3577 ERR_FAIL_NULL_V(sd, String());
3578 ShapedTextDataFallback *span_sd = sd;
3579 if (sd->parent.is_valid()) {
3580 span_sd = shaped_owner.get_or_null(sd->parent);
3581 ERR_FAIL_NULL_V(span_sd, String());
3582 }
3583 ERR_FAIL_INDEX_V(p_index, span_sd->spans.size(), String());
3584 return span_sd->text.substr(span_sd->spans[p_index].start, span_sd->spans[p_index].end - span_sd->spans[p_index].start);
3585 }
3586
3587 Variant TextServerFallback::_shaped_get_span_object(const RID &p_shaped, int64_t p_index) const {
3588 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3589 ERR_FAIL_NULL_V(sd, Variant());
3590 ShapedTextDataFallback *span_sd = sd;
3591 if (sd->parent.is_valid()) {
3592 span_sd = shaped_owner.get_or_null(sd->parent);
3593 ERR_FAIL_NULL_V(span_sd, Variant());
3594 }
3595 ERR_FAIL_INDEX_V(p_index, span_sd->spans.size(), Variant());
3596 return span_sd->spans[p_index].embedded_key;
3597 }
3598
3599 void TextServerFallback::_generate_runs(ShapedTextDataFallback *p_sd) const {
3600 ERR_FAIL_NULL(p_sd);
3601 p_sd->runs.clear();
3602
3603 ShapedTextDataFallback *span_sd = p_sd;
3604 if (p_sd->parent.is_valid()) {
3605 span_sd = shaped_owner.get_or_null(p_sd->parent);
3606 ERR_FAIL_NULL(span_sd);
3607 }
3608
3609 int sd_size = p_sd->glyphs.size();
3610 Glyph *sd_gl = p_sd->glyphs.ptrw();
3611
3612 int span_count = span_sd->spans.size();
3613 int span = -1;
3614 int span_start = -1;
3615 int span_end = -1;
3616
3617 TextRun run;
3618 for (int i = 0; i < sd_size; i += sd_gl[i].count) {
3619 const Glyph &gl = sd_gl[i];
3620 if (gl.start < 0 || gl.end < 0) {
3621 continue;
3622 }
3623 if (gl.start < span_start || gl.start >= span_end) {
3624 span = -1;
3625 span_start = -1;
3626 span_end = -1;
3627 for (int j = 0; j < span_count; j++) {
3628 if (gl.start >= span_sd->spans[j].start && gl.end <= span_sd->spans[j].end) {
3629 span = j;
3630 span_start = span_sd->spans[j].start;
3631 span_end = span_sd->spans[j].end;
3632 break;
3633 }
3634 }
3635 }
3636 if (run.font_rid != gl.font_rid || run.font_size != gl.font_size || run.span_index != span) {
3637 if (run.span_index >= 0) {
3638 p_sd->runs.push_back(run);
3639 }
3640 run.range = Vector2i(gl.start, gl.end);
3641 run.gl_range = Vector2i(i, i);
3642 run.font_rid = gl.font_rid;
3643 run.font_size = gl.font_size;
3644 run.span_index = span;
3645 }
3646 run.range.x = MIN(run.range.x, gl.start);
3647 run.range.y = MAX(run.range.y, gl.end);
3648 run.gl_range.x = MIN(run.gl_range.x, i);
3649 run.gl_range.y = MAX(run.gl_range.y, i);
3650 }
3651 if (run.span_index >= 0) {
3652 p_sd->runs.push_back(run);
3653 }
3654 p_sd->runs_dirty = false;
3655 }
3656
3657 int64_t TextServerFallback::_shaped_get_run_count(const RID &p_shaped) const {
3658 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3659 ERR_FAIL_NULL_V(sd, 0);
3660 MutexLock lock(sd->mutex);
3661 if (!sd->valid.is_set()) {
3662 const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
3663 }
3664 if (sd->runs_dirty) {
3665 _generate_runs(sd);
3666 }
3667 return sd->runs.size();
3668 }
3669
3670 String TextServerFallback::_shaped_get_run_text(const RID &p_shaped, int64_t p_index) const {
3671 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3672 ERR_FAIL_NULL_V(sd, String());
3673 MutexLock lock(sd->mutex);
3674 if (!sd->valid.is_set()) {
3675 const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
3676 }
3677 if (sd->runs_dirty) {
3678 _generate_runs(sd);
3679 }
3680 ERR_FAIL_INDEX_V(p_index, sd->runs.size(), String());
3681 return sd->text.substr(sd->runs[p_index].range.x - sd->start, sd->runs[p_index].range.y - sd->runs[p_index].range.x);
3682 }
3683
3684 Vector2i TextServerFallback::_shaped_get_run_range(const RID &p_shaped, int64_t p_index) const {
3685 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3686 ERR_FAIL_NULL_V(sd, Vector2i());
3687 MutexLock lock(sd->mutex);
3688 if (!sd->valid.is_set()) {
3689 const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
3690 }
3691 if (sd->runs_dirty) {
3692 _generate_runs(sd);
3693 }
3694 ERR_FAIL_INDEX_V(p_index, sd->runs.size(), Vector2i());
3695 return sd->runs[p_index].range;
3696 }
3697
3698 Vector2i TextServerFallback::_shaped_get_run_glyph_range(const RID &p_shaped, int64_t p_index) const {
3699 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3700 ERR_FAIL_NULL_V(sd, Vector2i());
3701 MutexLock lock(sd->mutex);
3702 if (!sd->valid.is_set()) {
3703 const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
3704 }
3705 if (sd->runs_dirty) {
3706 _generate_runs(sd);
3707 }
3708 ERR_FAIL_INDEX_V(p_index, sd->runs.size(), Vector2i());
3709 return sd->runs[p_index].gl_range;
3710 }
3711
3712 RID TextServerFallback::_shaped_get_run_font_rid(const RID &p_shaped, int64_t p_index) const {
3713 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3714 ERR_FAIL_NULL_V(sd, RID());
3715 MutexLock lock(sd->mutex);
3716 if (!sd->valid.is_set()) {
3717 const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
3718 }
3719 if (sd->runs_dirty) {
3720 _generate_runs(sd);
3721 }
3722 ERR_FAIL_INDEX_V(p_index, sd->runs.size(), RID());
3723 return sd->runs[p_index].font_rid;
3724 }
3725
3726 int TextServerFallback::_shaped_get_run_font_size(const RID &p_shaped, int64_t p_index) const {
3727 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3728 ERR_FAIL_NULL_V(sd, 0);
3729 MutexLock lock(sd->mutex);
3730 if (!sd->valid.is_set()) {
3731 const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
3732 }
3733 if (sd->runs_dirty) {
3734 _generate_runs(sd);
3735 }
3736 ERR_FAIL_INDEX_V(p_index, sd->runs.size(), 0);
3737 return sd->runs[p_index].font_size;
3738 }
3739
3740 String TextServerFallback::_shaped_get_run_language(const RID &p_shaped, int64_t p_index) const {
3741 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3742 ERR_FAIL_NULL_V(sd, String());
3743 MutexLock lock(sd->mutex);
3744 if (!sd->valid.is_set()) {
3745 const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
3746 }
3747 if (sd->runs_dirty) {
3748 _generate_runs(sd);
3749 }
3750 ERR_FAIL_INDEX_V(p_index, sd->runs.size(), String());
3751
3752 int span_idx = sd->runs[p_index].span_index;
3753 ShapedTextDataFallback *span_sd = sd;
3754 if (sd->parent.is_valid()) {
3755 span_sd = shaped_owner.get_or_null(sd->parent);
3756 ERR_FAIL_NULL_V(span_sd, String());
3757 }
3758 ERR_FAIL_INDEX_V(span_idx, span_sd->spans.size(), String());
3759 return span_sd->spans[span_idx].language;
3760 }
3761
3762 TextServer::Direction TextServerFallback::_shaped_get_run_direction(const RID &p_shaped, int64_t p_index) const {
3763 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3764 ERR_FAIL_NULL_V(sd, TextServer::DIRECTION_LTR);
3765 MutexLock lock(sd->mutex);
3766 if (!sd->valid.is_set()) {
3767 const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
3768 }
3769 if (sd->runs_dirty) {
3770 _generate_runs(sd);
3771 }
3772 ERR_FAIL_INDEX_V(p_index, sd->runs.size(), TextServer::DIRECTION_LTR);
3773 return TextServer::DIRECTION_LTR;
3774 }
3775
3776 Variant TextServerFallback::_shaped_get_run_object(const RID &p_shaped, int64_t p_index) const {
3777 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3778 ERR_FAIL_NULL_V(sd, Variant());
3779 MutexLock lock(sd->mutex);
3780 if (!sd->valid.is_set()) {
3781 const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
3782 }
3783 if (sd->runs_dirty) {
3784 _generate_runs(sd);
3785 }
3786 ERR_FAIL_INDEX_V(p_index, sd->runs.size(), Variant());
3787
3788 int span_idx = sd->runs[p_index].span_index;
3789 ShapedTextDataFallback *span_sd = sd;
3790 if (sd->parent.is_valid()) {
3791 span_sd = shaped_owner.get_or_null(sd->parent);
3792 ERR_FAIL_NULL_V(span_sd, Variant());
3793 }
3794 ERR_FAIL_INDEX_V(span_idx, span_sd->spans.size(), Variant());
3795 return span_sd->spans[span_idx].embedded_key;
3796 }
3797
3798 void TextServerFallback::_shaped_set_span_update_font(const RID &p_shaped, int64_t p_index, const TypedArray<RID> &p_fonts, int64_t p_size, const Dictionary &p_opentype_features) {
3799 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3800 ERR_FAIL_NULL(sd);
3801 if (sd->parent != RID()) {
3802 full_copy(sd);
3803 }
3804 ERR_FAIL_INDEX(p_index, sd->spans.size());
3805
3806 ShapedTextDataFallback::Span &span = sd->spans.ptrw()[p_index];
3807 span.fonts.clear();
3808 // Pre-sort fonts, push fonts with the language support first.
3809 Array fonts_no_match;
3810 int font_count = p_fonts.size();
3811 for (int i = 0; i < font_count; i++) {
3812 if (_font_is_language_supported(p_fonts[i], span.language)) {
3813 span.fonts.push_back(p_fonts[i]);
3814 } else {
3815 fonts_no_match.push_back(p_fonts[i]);
3816 }
3817 }
3818 span.fonts.append_array(fonts_no_match);
3819 span.font_size = p_size;
3820 span.features = p_opentype_features;
3821
3822 sd->valid.clear();
3823 }
3824
3825 bool TextServerFallback::_shaped_text_add_string(const RID &p_shaped, const String &p_text, const TypedArray<RID> &p_fonts, int64_t p_size, const Dictionary &p_opentype_features, const String &p_language, const Variant &p_meta) {
3826 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3827 ERR_FAIL_NULL_V(sd, false);
3828
3829 MutexLock lock(sd->mutex);
3830 ERR_FAIL_COND_V(p_size <= 0, false);
3831
3832 for (int i = 0; i < p_fonts.size(); i++) {
3833 ERR_FAIL_NULL_V(_get_font_data(p_fonts[i]), false);
3834 }
3835
3836 if (p_text.is_empty()) {
3837 return true;
3838 }
3839
3840 if (sd->parent != RID()) {
3841 full_copy(sd);
3842 }
3843
3844 ShapedTextDataFallback::Span span;
3845 span.start = sd->text.length();
3846 span.end = span.start + p_text.length();
3847
3848 // Pre-sort fonts, push fonts with the language support first.
3849 Array fonts_no_match;
3850 int font_count = p_fonts.size();
3851 if (font_count > 0) {
3852 span.fonts.push_back(p_fonts[0]);
3853 }
3854 for (int i = 1; i < font_count; i++) {
3855 if (_font_is_language_supported(p_fonts[i], p_language)) {
3856 span.fonts.push_back(p_fonts[i]);
3857 } else {
3858 fonts_no_match.push_back(p_fonts[i]);
3859 }
3860 }
3861 span.fonts.append_array(fonts_no_match);
3862
3863 ERR_FAIL_COND_V(span.fonts.is_empty(), false);
3864 span.font_size = p_size;
3865 span.language = p_language;
3866 span.meta = p_meta;
3867
3868 sd->spans.push_back(span);
3869 sd->text = sd->text + p_text;
3870 sd->end += p_text.length();
3871 invalidate(sd);
3872
3873 return true;
3874 }
3875
3876 bool TextServerFallback::_shaped_text_add_object(const RID &p_shaped, const Variant &p_key, const Size2 &p_size, InlineAlignment p_inline_align, int64_t p_length, double p_baseline) {
3877 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3878 ERR_FAIL_NULL_V(sd, false);
3879
3880 MutexLock lock(sd->mutex);
3881 ERR_FAIL_COND_V(p_key == Variant(), false);
3882 ERR_FAIL_COND_V(sd->objects.has(p_key), false);
3883
3884 if (sd->parent != RID()) {
3885 full_copy(sd);
3886 }
3887
3888 ShapedTextDataFallback::Span span;
3889 span.start = sd->start + sd->text.length();
3890 span.end = span.start + p_length;
3891 span.embedded_key = p_key;
3892
3893 ShapedTextDataFallback::EmbeddedObject obj;
3894 obj.inline_align = p_inline_align;
3895 obj.rect.size = p_size;
3896 obj.start = span.start;
3897 obj.end = span.end;
3898 obj.baseline = p_baseline;
3899
3900 sd->spans.push_back(span);
3901 sd->text = sd->text + String::chr(0xfffc).repeat(p_length);
3902 sd->end += p_length;
3903 sd->objects[p_key] = obj;
3904 invalidate(sd);
3905
3906 return true;
3907 }
3908
3909 String TextServerFallback::_shaped_get_text(const RID &p_shaped) const {
3910 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3911 ERR_FAIL_NULL_V(sd, String());
3912
3913 return sd->text;
3914 }
3915
3916 bool TextServerFallback::_shaped_text_has_object(const RID &p_shaped, const Variant &p_key) const {
3917 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3918 ERR_FAIL_NULL_V(sd, false);
3919
3920 MutexLock lock(sd->mutex);
3921 return sd->objects.has(p_key);
3922 }
3923
3924 bool TextServerFallback::_shaped_text_resize_object(const RID &p_shaped, const Variant &p_key, const Size2 &p_size, InlineAlignment p_inline_align, double p_baseline) {
3925 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
3926 ERR_FAIL_NULL_V(sd, false);
3927
3928 MutexLock lock(sd->mutex);
3929 ERR_FAIL_COND_V(!sd->objects.has(p_key), false);
3930 sd->objects[p_key].rect.size = p_size;
3931 sd->objects[p_key].inline_align = p_inline_align;
3932 sd->objects[p_key].baseline = p_baseline;
3933 if (sd->valid.is_set()) {
3934 // Recalc string metrics.
3935 sd->ascent = 0;
3936 sd->descent = 0;
3937 sd->width = 0;
3938 sd->upos = 0;
3939 sd->uthk = 0;
3940
3941 Vector<ShapedTextDataFallback::Span> &spans = sd->spans;
3942 if (sd->parent != RID()) {
3943 ShapedTextDataFallback *parent_sd = shaped_owner.get_or_null(sd->parent);
3944 ERR_FAIL_COND_V(!parent_sd->valid.is_set(), false);
3945 spans = parent_sd->spans;
3946 }
3947
3948 int sd_size = sd->glyphs.size();
3949 int span_size = spans.size();
3950
3951 for (int i = 0; i < sd_size; i++) {
3952 Glyph gl = sd->glyphs[i];
3953 Variant key;
3954 if ((gl.flags & GRAPHEME_IS_EMBEDDED_OBJECT) == GRAPHEME_IS_EMBEDDED_OBJECT && gl.span_index + sd->first_span >= 0 && gl.span_index + sd->first_span < span_size) {
3955 key = spans[gl.span_index + sd->first_span].embedded_key;
3956 }
3957 if (key != Variant()) {
3958 if (sd->orientation == ORIENTATION_HORIZONTAL) {
3959 sd->objects[key].rect.position.x = sd->width;
3960 sd->width += sd->objects[key].rect.size.x;
3961 sd->glyphs.write[i].advance = sd->objects[key].rect.size.x;
3962 } else {
3963 sd->objects[key].rect.position.y = sd->width;
3964 sd->width += sd->objects[key].rect.size.y;
3965 sd->glyphs.write[i].advance = sd->objects[key].rect.size.y;
3966 }
3967 } else {
3968 if (gl.font_rid.is_valid()) {
3969 if (sd->orientation == ORIENTATION_HORIZONTAL) {
3970 sd->ascent = MAX(sd->ascent, _font_get_ascent(gl.font_rid, gl.font_size) + _font_get_spacing(gl.font_rid, SPACING_TOP));
3971 sd->descent = MAX(sd->descent, _font_get_descent(gl.font_rid, gl.font_size) + _font_get_spacing(gl.font_rid, SPACING_BOTTOM));
3972 } else {
3973 sd->ascent = MAX(sd->ascent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
3974 sd->descent = MAX(sd->descent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
3975 }
3976 sd->upos = MAX(sd->upos, _font_get_underline_position(gl.font_rid, gl.font_size));
3977 sd->uthk = MAX(sd->uthk, _font_get_underline_thickness(gl.font_rid, gl.font_size));
3978 } else if (sd->preserve_invalid || (sd->preserve_control && is_control(gl.index))) {
3979 // Glyph not found, replace with hex code box.
3980 if (sd->orientation == ORIENTATION_HORIZONTAL) {
3981 sd->ascent = MAX(sd->ascent, get_hex_code_box_size(gl.font_size, gl.index).y * 0.85);
3982 sd->descent = MAX(sd->descent, get_hex_code_box_size(gl.font_size, gl.index).y * 0.15);
3983 } else {
3984 sd->ascent = MAX(sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
3985 sd->descent = MAX(sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
3986 }
3987 }
3988 sd->width += gl.advance * gl.repeat;
3989 }
3990 }
3991 _realign(sd);
3992 }
3993 return true;
3994 }
3995
3996 void TextServerFallback::_realign(ShapedTextDataFallback *p_sd) const {
3997 // Align embedded objects to baseline.
3998 double full_ascent = p_sd->ascent;
3999 double full_descent = p_sd->descent;
4000 for (KeyValue<Variant, ShapedTextDataFallback::EmbeddedObject> &E : p_sd->objects) {
4001 if ((E.value.start >= p_sd->start) && (E.value.start < p_sd->end)) {
4002 if (p_sd->orientation == ORIENTATION_HORIZONTAL) {
4003 switch (E.value.inline_align & INLINE_ALIGNMENT_TEXT_MASK) {
4004 case INLINE_ALIGNMENT_TO_TOP: {
4005 E.value.rect.position.y = -p_sd->ascent;
4006 } break;
4007 case INLINE_ALIGNMENT_TO_CENTER: {
4008 E.value.rect.position.y = (-p_sd->ascent + p_sd->descent) / 2;
4009 } break;
4010 case INLINE_ALIGNMENT_TO_BASELINE: {
4011 E.value.rect.position.y = 0;
4012 } break;
4013 case INLINE_ALIGNMENT_TO_BOTTOM: {
4014 E.value.rect.position.y = p_sd->descent;
4015 } break;
4016 }
4017 switch (E.value.inline_align & INLINE_ALIGNMENT_IMAGE_MASK) {
4018 case INLINE_ALIGNMENT_BOTTOM_TO: {
4019 E.value.rect.position.y -= E.value.rect.size.y;
4020 } break;
4021 case INLINE_ALIGNMENT_CENTER_TO: {
4022 E.value.rect.position.y -= E.value.rect.size.y / 2;
4023 } break;
4024 case INLINE_ALIGNMENT_BASELINE_TO: {
4025 E.value.rect.position.y -= E.value.baseline;
4026 } break;
4027 case INLINE_ALIGNMENT_TOP_TO: {
4028 // NOP
4029 } break;
4030 }
4031 full_ascent = MAX(full_ascent, -E.value.rect.position.y);
4032 full_descent = MAX(full_descent, E.value.rect.position.y + E.value.rect.size.y);
4033 } else {
4034 switch (E.value.inline_align & INLINE_ALIGNMENT_TEXT_MASK) {
4035 case INLINE_ALIGNMENT_TO_TOP: {
4036 E.value.rect.position.x = -p_sd->ascent;
4037 } break;
4038 case INLINE_ALIGNMENT_TO_CENTER: {
4039 E.value.rect.position.x = (-p_sd->ascent + p_sd->descent) / 2;
4040 } break;
4041 case INLINE_ALIGNMENT_TO_BASELINE: {
4042 E.value.rect.position.x = 0;
4043 } break;
4044 case INLINE_ALIGNMENT_TO_BOTTOM: {
4045 E.value.rect.position.x = p_sd->descent;
4046 } break;
4047 }
4048 switch (E.value.inline_align & INLINE_ALIGNMENT_IMAGE_MASK) {
4049 case INLINE_ALIGNMENT_BOTTOM_TO: {
4050 E.value.rect.position.x -= E.value.rect.size.x;
4051 } break;
4052 case INLINE_ALIGNMENT_CENTER_TO: {
4053 E.value.rect.position.x -= E.value.rect.size.x / 2;
4054 } break;
4055 case INLINE_ALIGNMENT_BASELINE_TO: {
4056 E.value.rect.position.x -= E.value.baseline;
4057 } break;
4058 case INLINE_ALIGNMENT_TOP_TO: {
4059 // NOP
4060 } break;
4061 }
4062 full_ascent = MAX(full_ascent, -E.value.rect.position.x);
4063 full_descent = MAX(full_descent, E.value.rect.position.x + E.value.rect.size.x);
4064 }
4065 }
4066 }
4067 p_sd->ascent = full_ascent;
4068 p_sd->descent = full_descent;
4069 }
4070
4071 RID TextServerFallback::_shaped_text_substr(const RID &p_shaped, int64_t p_start, int64_t p_length) const {
4072 _THREAD_SAFE_METHOD_
4073
4074 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
4075 ERR_FAIL_NULL_V(sd, RID());
4076
4077 MutexLock lock(sd->mutex);
4078 if (sd->parent != RID()) {
4079 return _shaped_text_substr(sd->parent, p_start, p_length);
4080 }
4081 if (!sd->valid.is_set()) {
4082 const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
4083 }
4084 ERR_FAIL_COND_V(p_start < 0 || p_length < 0, RID());
4085 ERR_FAIL_COND_V(sd->start > p_start || sd->end < p_start, RID());
4086 ERR_FAIL_COND_V(sd->end < p_start + p_length, RID());
4087
4088 ShapedTextDataFallback *new_sd = memnew(ShapedTextDataFallback);
4089 new_sd->parent = p_shaped;
4090 new_sd->start = p_start;
4091 new_sd->end = p_start + p_length;
4092
4093 new_sd->orientation = sd->orientation;
4094 new_sd->direction = sd->direction;
4095 new_sd->custom_punct = sd->custom_punct;
4096 new_sd->para_direction = sd->para_direction;
4097 new_sd->line_breaks_valid = sd->line_breaks_valid;
4098 new_sd->justification_ops_valid = sd->justification_ops_valid;
4099 new_sd->sort_valid = false;
4100 new_sd->upos = sd->upos;
4101 new_sd->uthk = sd->uthk;
4102 for (int i = 0; i < TextServer::SPACING_MAX; i++) {
4103 new_sd->extra_spacing[i] = sd->extra_spacing[i];
4104 }
4105
4106 if (p_length > 0) {
4107 new_sd->text = sd->text.substr(p_start - sd->start, p_length);
4108
4109 int span_size = sd->spans.size();
4110
4111 new_sd->first_span = 0;
4112 new_sd->last_span = span_size - 1;
4113 for (int i = 0; i < span_size; i++) {
4114 const ShapedTextDataFallback::Span &span = sd->spans[i];
4115 if (span.end <= p_start) {
4116 new_sd->first_span = i + 1;
4117 } else if (span.start >= p_start + p_length) {
4118 new_sd->last_span = i - 1;
4119 break;
4120 }
4121 }
4122
4123 int sd_size = sd->glyphs.size();
4124 const Glyph *sd_glyphs = sd->glyphs.ptr();
4125
4126 for (int i = 0; i < sd_size; i++) {
4127 if ((sd_glyphs[i].start >= new_sd->start) && (sd_glyphs[i].end <= new_sd->end)) {
4128 Glyph gl = sd_glyphs[i];
4129 if (gl.span_index >= 0) {
4130 gl.span_index -= new_sd->first_span;
4131 }
4132 if (gl.end == p_start + p_length && ((gl.flags & GRAPHEME_IS_SOFT_HYPHEN) == GRAPHEME_IS_SOFT_HYPHEN)) {
4133 gl.index = 0x00ad;
4134 gl.advance = font_get_glyph_advance(gl.font_rid, gl.font_size, 0x00ad).x;
4135 }
4136 if ((gl.flags & GRAPHEME_IS_EMBEDDED_OBJECT) == GRAPHEME_IS_EMBEDDED_OBJECT && gl.span_index + new_sd->first_span >= 0 && gl.span_index + new_sd->first_span < span_size) {
4137 Variant key = sd->spans[gl.span_index + new_sd->first_span].embedded_key;
4138 if (key != Variant()) {
4139 ShapedTextDataFallback::EmbeddedObject obj = sd->objects[key];
4140 if (new_sd->orientation == ORIENTATION_HORIZONTAL) {
4141 obj.rect.position.x = new_sd->width;
4142 new_sd->width += obj.rect.size.x;
4143 } else {
4144 obj.rect.position.y = new_sd->width;
4145 new_sd->width += obj.rect.size.y;
4146 }
4147 new_sd->objects[key] = obj;
4148 }
4149 } else {
4150 if (gl.font_rid.is_valid()) {
4151 if (new_sd->orientation == ORIENTATION_HORIZONTAL) {
4152 new_sd->ascent = MAX(new_sd->ascent, _font_get_ascent(gl.font_rid, gl.font_size) + _font_get_spacing(gl.font_rid, SPACING_TOP));
4153 new_sd->descent = MAX(new_sd->descent, _font_get_descent(gl.font_rid, gl.font_size) + _font_get_spacing(gl.font_rid, SPACING_BOTTOM));
4154 } else {
4155 new_sd->ascent = MAX(new_sd->ascent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
4156 new_sd->descent = MAX(new_sd->descent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
4157 }
4158 } else if (new_sd->preserve_invalid || (new_sd->preserve_control && is_control(gl.index))) {
4159 // Glyph not found, replace with hex code box.
4160 if (new_sd->orientation == ORIENTATION_HORIZONTAL) {
4161 new_sd->ascent = MAX(new_sd->ascent, get_hex_code_box_size(gl.font_size, gl.index).y * 0.85);
4162 new_sd->descent = MAX(new_sd->descent, get_hex_code_box_size(gl.font_size, gl.index).y * 0.15);
4163 } else {
4164 new_sd->ascent = MAX(new_sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
4165 new_sd->descent = MAX(new_sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
4166 }
4167 }
4168 if (new_sd->glyphs.is_empty() && gl.x_off < 0.0) {
4169 gl.advance += -gl.x_off;
4170 gl.x_off = 0.0;
4171 }
4172 new_sd->width += gl.advance * gl.repeat;
4173 }
4174 new_sd->glyphs.push_back(gl);
4175 }
4176 }
4177
4178 _realign(new_sd);
4179 }
4180 new_sd->valid.set();
4181
4182 return shaped_owner.make_rid(new_sd);
4183 }
4184
4185 RID TextServerFallback::_shaped_text_get_parent(const RID &p_shaped) const {
4186 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
4187 ERR_FAIL_NULL_V(sd, RID());
4188
4189 MutexLock lock(sd->mutex);
4190 return sd->parent;
4191 }
4192
4193 double TextServerFallback::_shaped_text_fit_to_width(const RID &p_shaped, double p_width, BitField<JustificationFlag> p_jst_flags) {
4194 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
4195 ERR_FAIL_NULL_V(sd, 0.0);
4196
4197 MutexLock lock(sd->mutex);
4198 if (!sd->valid.is_set()) {
4199 _shaped_text_shape(p_shaped);
4200 }
4201 if (!sd->justification_ops_valid) {
4202 _shaped_text_update_justification_ops(p_shaped);
4203 }
4204
4205 int start_pos = 0;
4206 int end_pos = sd->glyphs.size() - 1;
4207
4208 if (p_jst_flags.has_flag(JUSTIFICATION_AFTER_LAST_TAB)) {
4209 int start, end, delta;
4210 if (sd->para_direction == DIRECTION_LTR) {
4211 start = sd->glyphs.size() - 1;
4212 end = -1;
4213 delta = -1;
4214 } else {
4215 start = 0;
4216 end = sd->glyphs.size();
4217 delta = +1;
4218 }
4219
4220 for (int i = start; i != end; i += delta) {
4221 if ((sd->glyphs[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB) {
4222 if (sd->para_direction == DIRECTION_LTR) {
4223 start_pos = i;
4224 break;
4225 } else {
4226 end_pos = i;
4227 break;
4228 }
4229 }
4230 }
4231 }
4232
4233 double justification_width;
4234 if (p_jst_flags.has_flag(JUSTIFICATION_CONSTRAIN_ELLIPSIS)) {
4235 if (sd->overrun_trim_data.trim_pos >= 0) {
4236 end_pos = sd->overrun_trim_data.trim_pos;
4237 justification_width = sd->width_trimmed;
4238 } else {
4239 return Math::ceil(sd->width);
4240 }
4241 } else {
4242 justification_width = sd->width;
4243 }
4244
4245 if (p_jst_flags.has_flag(JUSTIFICATION_TRIM_EDGE_SPACES)) {
4246 // Trim spaces.
4247 while ((start_pos < end_pos) && ((sd->glyphs[start_pos].flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN) && ((sd->glyphs[start_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
4248 justification_width -= sd->glyphs[start_pos].advance * sd->glyphs[start_pos].repeat;
4249 sd->glyphs.write[start_pos].advance = 0;
4250 start_pos += sd->glyphs[start_pos].count;
4251 }
4252 while ((start_pos < end_pos) && ((sd->glyphs[end_pos].flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN) && ((sd->glyphs[end_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
4253 justification_width -= sd->glyphs[end_pos].advance * sd->glyphs[end_pos].repeat;
4254 sd->glyphs.write[end_pos].advance = 0;
4255 end_pos -= sd->glyphs[end_pos].count;
4256 }
4257 } else {
4258 // Skip breaks, but do not reset size.
4259 while ((start_pos < end_pos) && ((sd->glyphs[start_pos].flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN) && ((sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
4260 start_pos += sd->glyphs[start_pos].count;
4261 }
4262 while ((start_pos < end_pos) && ((sd->glyphs[end_pos].flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN) && ((sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
4263 end_pos -= sd->glyphs[end_pos].count;
4264 }
4265 }
4266
4267 int space_count = 0;
4268 for (int i = start_pos; i <= end_pos; i++) {
4269 const Glyph &gl = sd->glyphs[i];
4270 if (gl.count > 0) {
4271 if ((gl.flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN && (gl.flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE && (gl.flags & GRAPHEME_IS_PUNCTUATION) != GRAPHEME_IS_PUNCTUATION) {
4272 space_count++;
4273 }
4274 }
4275 }
4276
4277 if ((space_count > 0) && p_jst_flags.has_flag(JUSTIFICATION_WORD_BOUND)) {
4278 double delta_width_per_space = (p_width - justification_width) / space_count;
4279 for (int i = start_pos; i <= end_pos; i++) {
4280 Glyph &gl = sd->glyphs.write[i];
4281 if (gl.count > 0) {
4282 if ((gl.flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN && (gl.flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE && (gl.flags & GRAPHEME_IS_PUNCTUATION) != GRAPHEME_IS_PUNCTUATION) {
4283 double old_adv = gl.advance;
4284 gl.advance = MAX(gl.advance + delta_width_per_space, Math::round(0.1 * gl.font_size));
4285 justification_width += (gl.advance - old_adv);
4286 }
4287 }
4288 }
4289 }
4290
4291 if (Math::floor(p_width) < Math::floor(justification_width)) {
4292 sd->fit_width_minimum_reached = true;
4293 }
4294
4295 if (!p_jst_flags.has_flag(JUSTIFICATION_CONSTRAIN_ELLIPSIS)) {
4296 sd->width = justification_width;
4297 }
4298
4299 return Math::ceil(justification_width);
4300 }
4301
4302 double TextServerFallback::_shaped_text_tab_align(const RID &p_shaped, const PackedFloat32Array &p_tab_stops) {
4303 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
4304 ERR_FAIL_NULL_V(sd, 0.0);
4305
4306 MutexLock lock(sd->mutex);
4307 if (!sd->valid.is_set()) {
4308 _shaped_text_shape(p_shaped);
4309 }
4310 if (!sd->line_breaks_valid) {
4311 _shaped_text_update_breaks(p_shaped);
4312 }
4313
4314 for (int i = 0; i < p_tab_stops.size(); i++) {
4315 if (p_tab_stops[i] <= 0) {
4316 return 0.0;
4317 }
4318 }
4319
4320 int tab_index = 0;
4321 double off = 0.0;
4322
4323 int start, end, delta;
4324 if (sd->para_direction == DIRECTION_LTR) {
4325 start = 0;
4326 end = sd->glyphs.size();
4327 delta = +1;
4328 } else {
4329 start = sd->glyphs.size() - 1;
4330 end = -1;
4331 delta = -1;
4332 }
4333
4334 Glyph *gl = sd->glyphs.ptrw();
4335
4336 for (int i = start; i != end; i += delta) {
4337 if ((gl[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB) {
4338 double tab_off = 0.0;
4339 while (tab_off <= off) {
4340 tab_off += p_tab_stops[tab_index];
4341 tab_index++;
4342 if (tab_index >= p_tab_stops.size()) {
4343 tab_index = 0;
4344 }
4345 }
4346 double old_adv = gl[i].advance;
4347 gl[i].advance = tab_off - off;
4348 sd->width += gl[i].advance - old_adv;
4349 off = 0;
4350 continue;
4351 }
4352 off += gl[i].advance * gl[i].repeat;
4353 }
4354
4355 return 0.0;
4356 }
4357
4358 bool TextServerFallback::_shaped_text_update_breaks(const RID &p_shaped) {
4359 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
4360 ERR_FAIL_NULL_V(sd, false);
4361
4362 MutexLock lock(sd->mutex);
4363 if (!sd->valid.is_set()) {
4364 _shaped_text_shape(p_shaped);
4365 }
4366
4367 if (sd->line_breaks_valid) {
4368 return true; // Nothing to do.
4369 }
4370
4371 int sd_size = sd->glyphs.size();
4372 Glyph *sd_glyphs = sd->glyphs.ptrw();
4373
4374 int c_punct_size = sd->custom_punct.length();
4375 const char32_t *c_punct = sd->custom_punct.ptr();
4376
4377 for (int i = 0; i < sd_size; i++) {
4378 if (sd_glyphs[i].count > 0) {
4379 char32_t c = sd->text[sd_glyphs[i].start - sd->start];
4380 char32_t c_next = i < sd_size ? sd->text[sd_glyphs[i].start - sd->start + 1] : 0x0000;
4381 if (c_punct_size == 0) {
4382 if (is_punct(c) && c != 0x005F && c != ' ') {
4383 sd_glyphs[i].flags |= GRAPHEME_IS_PUNCTUATION;
4384 }
4385 } else {
4386 for (int j = 0; j < c_punct_size; j++) {
4387 if (c_punct[j] == c) {
4388 sd_glyphs[i].flags |= GRAPHEME_IS_PUNCTUATION;
4389 break;
4390 }
4391 }
4392 }
4393 if (is_underscore(c)) {
4394 sd->glyphs.write[i].flags |= GRAPHEME_IS_UNDERSCORE;
4395 }
4396 if (is_whitespace(c) && !is_linebreak(c)) {
4397 sd_glyphs[i].flags |= GRAPHEME_IS_SPACE;
4398 if (c != 0x00A0 && c != 0x202F && c != 0x2060 && c != 0x2007) { // Skip for non-breaking space variants.
4399 sd_glyphs[i].flags |= GRAPHEME_IS_BREAK_SOFT;
4400 }
4401 }
4402 if (is_linebreak(c)) {
4403 sd_glyphs[i].flags |= GRAPHEME_IS_SPACE;
4404 if (c != 0x000D || c_next != 0x000A) { // Skip first hard break in CR-LF pair.
4405 sd_glyphs[i].flags |= GRAPHEME_IS_BREAK_HARD;
4406 }
4407 }
4408 if (c == 0x0009 || c == 0x000b) {
4409 sd_glyphs[i].flags |= GRAPHEME_IS_TAB;
4410 }
4411 if (c == 0x00ad) {
4412 sd_glyphs[i].flags |= GRAPHEME_IS_SOFT_HYPHEN;
4413 }
4414
4415 i += (sd_glyphs[i].count - 1);
4416 }
4417 }
4418 sd->line_breaks_valid = true;
4419 return sd->line_breaks_valid;
4420 }
4421
4422 bool TextServerFallback::_shaped_text_update_justification_ops(const RID &p_shaped) {
4423 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
4424 ERR_FAIL_NULL_V(sd, false);
4425
4426 MutexLock lock(sd->mutex);
4427 if (!sd->valid.is_set()) {
4428 _shaped_text_shape(p_shaped);
4429 }
4430 if (!sd->line_breaks_valid) {
4431 _shaped_text_update_breaks(p_shaped);
4432 }
4433
4434 sd->justification_ops_valid = true; // Not supported by fallback server.
4435 return true;
4436 }
4437
4438 RID TextServerFallback::_find_sys_font_for_text(const RID &p_fdef, const String &p_script_code, const String &p_language, const String &p_text) {
4439 RID f;
4440 // Try system fallback.
4441 if (_font_is_allow_system_fallback(p_fdef)) {
4442 String font_name = _font_get_name(p_fdef);
4443 BitField<FontStyle> font_style = _font_get_style(p_fdef);
4444 int font_weight = _font_get_weight(p_fdef);
4445 int font_stretch = _font_get_stretch(p_fdef);
4446 Dictionary dvar = _font_get_variation_coordinates(p_fdef);
4447 static int64_t wgth_tag = name_to_tag("weight");
4448 static int64_t wdth_tag = name_to_tag("width");
4449 static int64_t ital_tag = name_to_tag("italic");
4450 if (dvar.has(wgth_tag)) {
4451 font_weight = dvar[wgth_tag].operator int();
4452 }
4453 if (dvar.has(wdth_tag)) {
4454 font_stretch = dvar[wdth_tag].operator int();
4455 }
4456 if (dvar.has(ital_tag) && dvar[ital_tag].operator int() == 1) {
4457 font_style.set_flag(TextServer::FONT_ITALIC);
4458 }
4459
4460 String locale = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language;
4461 PackedStringArray fallback_font_name = OS::get_singleton()->get_system_font_path_for_text(font_name, p_text, locale, p_script_code, font_weight, font_stretch, font_style & TextServer::FONT_ITALIC);
4462 #ifdef GDEXTENSION
4463 for (int fb = 0; fb < fallback_font_name.size(); fb++) {
4464 const String &E = fallback_font_name[fb];
4465 #elif defined(GODOT_MODULE)
4466 for (const String &E : fallback_font_name) {
4467 #endif
4468 SystemFontKey key = SystemFontKey(E, font_style & TextServer::FONT_ITALIC, font_weight, font_stretch, p_fdef, this);
4469 if (system_fonts.has(key)) {
4470 const SystemFontCache &sysf_cache = system_fonts[key];
4471 int best_score = 0;
4472 int best_match = -1;
4473 for (int face_idx = 0; face_idx < sysf_cache.var.size(); face_idx++) {
4474 const SystemFontCacheRec &F = sysf_cache.var[face_idx];
4475 if (unlikely(!_font_has_char(F.rid, p_text[0]))) {
4476 continue;
4477 }
4478 BitField<FontStyle> style = _font_get_style(F.rid);
4479 int weight = _font_get_weight(F.rid);
4480 int stretch = _font_get_stretch(F.rid);
4481 int score = (20 - Math::abs(weight - font_weight) / 50);
4482 score += (20 - Math::abs(stretch - font_stretch) / 10);
4483 if (bool(style & TextServer::FONT_ITALIC) == bool(font_style & TextServer::FONT_ITALIC)) {
4484 score += 30;
4485 }
4486 if (score >= best_score) {
4487 best_score = score;
4488 best_match = face_idx;
4489 }
4490 if (best_score == 70) {
4491 break;
4492 }
4493 }
4494 if (best_match != -1) {
4495 f = sysf_cache.var[best_match].rid;
4496 }
4497 }
4498 if (!f.is_valid()) {
4499 if (system_fonts.has(key)) {
4500 const SystemFontCache &sysf_cache = system_fonts[key];
4501 if (sysf_cache.max_var == sysf_cache.var.size()) {
4502 // All subfonts already tested, skip.
4503 continue;
4504 }
4505 }
4506
4507 if (!system_font_data.has(E)) {
4508 system_font_data[E] = FileAccess::get_file_as_bytes(E);
4509 }
4510
4511 const PackedByteArray &font_data = system_font_data[E];
4512
4513 SystemFontCacheRec sysf;
4514 sysf.rid = _create_font();
4515 _font_set_data_ptr(sysf.rid, font_data.ptr(), font_data.size());
4516 if (!_font_validate(sysf.rid)) {
4517 _free_rid(sysf.rid);
4518 continue;
4519 }
4520
4521 Dictionary var = dvar;
4522 // Select matching style from collection.
4523 int best_score = 0;
4524 int best_match = -1;
4525 for (int face_idx = 0; face_idx < _font_get_face_count(sysf.rid); face_idx++) {
4526 _font_set_face_index(sysf.rid, face_idx);
4527 if (unlikely(!_font_has_char(sysf.rid, p_text[0]))) {
4528 continue;
4529 }
4530 BitField<FontStyle> style = _font_get_style(sysf.rid);
4531 int weight = _font_get_weight(sysf.rid);
4532 int stretch = _font_get_stretch(sysf.rid);
4533 int score = (20 - Math::abs(weight - font_weight) / 50);
4534 score += (20 - Math::abs(stretch - font_stretch) / 10);
4535 if (bool(style & TextServer::FONT_ITALIC) == bool(font_style & TextServer::FONT_ITALIC)) {
4536 score += 30;
4537 }
4538 if (score >= best_score) {
4539 best_score = score;
4540 best_match = face_idx;
4541 }
4542 if (best_score == 70) {
4543 break;
4544 }
4545 }
4546 if (best_match == -1) {
4547 _free_rid(sysf.rid);
4548 continue;
4549 } else {
4550 _font_set_face_index(sysf.rid, best_match);
4551 }
4552 sysf.index = best_match;
4553
4554 // If it's a variable font, apply weight, stretch and italic coordinates to match requested style.
4555 if (best_score != 70) {
4556 Dictionary ftr = _font_supported_variation_list(sysf.rid);
4557 if (ftr.has(wdth_tag)) {
4558 var[wdth_tag] = font_stretch;
4559 _font_set_stretch(sysf.rid, font_stretch);
4560 }
4561 if (ftr.has(wgth_tag)) {
4562 var[wgth_tag] = font_weight;
4563 _font_set_weight(sysf.rid, font_weight);
4564 }
4565 if ((font_style & TextServer::FONT_ITALIC) && ftr.has(ital_tag)) {
4566 var[ital_tag] = 1;
4567 _font_set_style(sysf.rid, _font_get_style(sysf.rid) | TextServer::FONT_ITALIC);
4568 }
4569 }
4570
4571 bool fb_use_msdf = key.msdf;
4572 #ifdef MODULE_FREETYPE_ENABLED
4573 if (fb_use_msdf) {
4574 FontFallback *fd = _get_font_data(sysf.rid);
4575 if (fd) {
4576 MutexLock lock(fd->mutex);
4577 Vector2i size = _get_size(fd, 16);
4578 FontForSizeFallback *ffsd = nullptr;
4579 if (_ensure_cache_for_size(fd, size, ffsd)) {
4580 if (ffsd && (FT_HAS_COLOR(ffsd->face) || !FT_IS_SCALABLE(ffsd->face))) {
4581 fb_use_msdf = false;
4582 }
4583 }
4584 }
4585 }
4586 #endif
4587
4588 _font_set_antialiasing(sysf.rid, key.antialiasing);
4589 _font_set_disable_embedded_bitmaps(sysf.rid, key.disable_embedded_bitmaps);
4590 _font_set_generate_mipmaps(sysf.rid, key.mipmaps);
4591 _font_set_multichannel_signed_distance_field(sysf.rid, fb_use_msdf);
4592 _font_set_msdf_pixel_range(sysf.rid, key.msdf_range);
4593 _font_set_msdf_size(sysf.rid, key.msdf_source_size);
4594 _font_set_fixed_size(sysf.rid, key.fixed_size);
4595 _font_set_force_autohinter(sysf.rid, key.force_autohinter);
4596 _font_set_hinting(sysf.rid, key.hinting);
4597 _font_set_subpixel_positioning(sysf.rid, key.subpixel_positioning);
4598 _font_set_keep_rounding_remainders(sysf.rid, key.keep_rounding_remainders);
4599 _font_set_variation_coordinates(sysf.rid, var);
4600 _font_set_embolden(sysf.rid, key.embolden);
4601 _font_set_transform(sysf.rid, key.transform);
4602 _font_set_spacing(sysf.rid, SPACING_TOP, key.extra_spacing[SPACING_TOP]);
4603 _font_set_spacing(sysf.rid, SPACING_BOTTOM, key.extra_spacing[SPACING_BOTTOM]);
4604 _font_set_spacing(sysf.rid, SPACING_SPACE, key.extra_spacing[SPACING_SPACE]);
4605 _font_set_spacing(sysf.rid, SPACING_GLYPH, key.extra_spacing[SPACING_GLYPH]);
4606
4607 if (system_fonts.has(key)) {
4608 system_fonts[key].var.push_back(sysf);
4609 } else {
4610 SystemFontCache &sysf_cache = system_fonts[key];
4611 sysf_cache.max_var = _font_get_face_count(sysf.rid);
4612 sysf_cache.var.push_back(sysf);
4613 }
4614 f = sysf.rid;
4615 }
4616 break;
4617 }
4618 }
4619 return f;
4620 }
4621
4622 void TextServerFallback::_shaped_text_overrun_trim_to_width(const RID &p_shaped_line, double p_width, BitField<TextServer::TextOverrunFlag> p_trim_flags) {
4623 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped_line);
4624 ERR_FAIL_NULL_MSG(sd, "ShapedTextDataFallback invalid.");
4625
4626 MutexLock lock(sd->mutex);
4627 if (!sd->valid.is_set()) {
4628 _shaped_text_shape(p_shaped_line);
4629 }
4630
4631 sd->text_trimmed = false;
4632 sd->overrun_trim_data.ellipsis_glyph_buf.clear();
4633
4634 bool add_ellipsis = p_trim_flags.has_flag(OVERRUN_ADD_ELLIPSIS);
4635 bool cut_per_word = p_trim_flags.has_flag(OVERRUN_TRIM_WORD_ONLY);
4636 bool enforce_ellipsis = p_trim_flags.has_flag(OVERRUN_ENFORCE_ELLIPSIS);
4637 bool short_string_ellipsis = p_trim_flags.has_flag(OVERRUN_SHORT_STRING_ELLIPSIS);
4638 bool justification_aware = p_trim_flags.has_flag(OVERRUN_JUSTIFICATION_AWARE);
4639
4640 Glyph *sd_glyphs = sd->glyphs.ptrw();
4641
4642 if ((p_trim_flags & OVERRUN_TRIM) == OVERRUN_NO_TRIM || sd_glyphs == nullptr || p_width <= 0 || !(sd->width > p_width || enforce_ellipsis)) {
4643 sd->overrun_trim_data.trim_pos = -1;
4644 sd->overrun_trim_data.ellipsis_pos = -1;
4645 return;
4646 }
4647
4648 if (justification_aware && !sd->fit_width_minimum_reached) {
4649 return;
4650 }
4651
4652 Vector<ShapedTextDataFallback::Span> &spans = sd->spans;
4653 if (sd->parent != RID()) {
4654 ShapedTextDataFallback *parent_sd = shaped_owner.get_or_null(sd->parent);
4655 ERR_FAIL_COND(!parent_sd->valid.is_set());
4656 spans = parent_sd->spans;
4657 }
4658
4659 int span_size = spans.size();
4660 if (span_size == 0) {
4661 return;
4662 }
4663
4664 int sd_size = sd->glyphs.size();
4665 int last_gl_font_size = sd_glyphs[sd_size - 1].font_size;
4666 bool found_el_char = false;
4667
4668 // Find usable fonts, if fonts from the last glyph do not have required chars.
4669 RID dot_gl_font_rid = sd_glyphs[sd_size - 1].font_rid;
4670 if (add_ellipsis || enforce_ellipsis || short_string_ellipsis) {
4671 if (!_font_has_char(dot_gl_font_rid, sd->el_char)) {
4672 const Array &fonts = spans[span_size - 1].fonts;
4673 for (int i = 0; i < fonts.size(); i++) {
4674 if (_font_has_char(fonts[i], sd->el_char)) {
4675 dot_gl_font_rid = fonts[i];
4676 found_el_char = true;
4677 break;
4678 }
4679 }
4680 if (!found_el_char && OS::get_singleton()->has_feature("system_fonts") && fonts.size() > 0 && _font_is_allow_system_fallback(fonts[0])) {
4681 const char32_t u32str[] = { sd->el_char, 0 };
4682 RID rid = _find_sys_font_for_text(fonts[0], String(), spans[span_size - 1].language, u32str);
4683 if (rid.is_valid()) {
4684 dot_gl_font_rid = rid;
4685 found_el_char = true;
4686 }
4687 }
4688 } else {
4689 found_el_char = true;
4690 }
4691 if (!found_el_char) {
4692 bool found_dot_char = false;
4693 dot_gl_font_rid = sd_glyphs[sd_size - 1].font_rid;
4694 if (!_font_has_char(dot_gl_font_rid, '.')) {
4695 const Array &fonts = spans[span_size - 1].fonts;
4696 for (int i = 0; i < fonts.size(); i++) {
4697 if (_font_has_char(fonts[i], '.')) {
4698 dot_gl_font_rid = fonts[i];
4699 found_dot_char = true;
4700 break;
4701 }
4702 }
4703 if (!found_dot_char && OS::get_singleton()->has_feature("system_fonts") && fonts.size() > 0 && _font_is_allow_system_fallback(fonts[0])) {
4704 RID rid = _find_sys_font_for_text(fonts[0], String(), spans[span_size - 1].language, ".");
4705 if (rid.is_valid()) {
4706 dot_gl_font_rid = rid;
4707 }
4708 }
4709 }
4710 }
4711 }
4712 RID whitespace_gl_font_rid = sd_glyphs[sd_size - 1].font_rid;
4713 if (!_font_has_char(whitespace_gl_font_rid, ' ')) {
4714 const Array &fonts = spans[span_size - 1].fonts;
4715 for (int i = 0; i < fonts.size(); i++) {
4716 if (_font_has_char(fonts[i], ' ')) {
4717 whitespace_gl_font_rid = fonts[i];
4718 break;
4719 }
4720 }
4721 }
4722
4723 int32_t dot_gl_idx = ((add_ellipsis || enforce_ellipsis || short_string_ellipsis) && dot_gl_font_rid.is_valid()) ? _font_get_glyph_index(dot_gl_font_rid, last_gl_font_size, (found_el_char ? sd->el_char : '.'), 0) : -1;
4724 Vector2 dot_adv = ((add_ellipsis || enforce_ellipsis || short_string_ellipsis) && dot_gl_font_rid.is_valid()) ? _font_get_glyph_advance(dot_gl_font_rid, last_gl_font_size, dot_gl_idx) : Vector2();
4725 int32_t whitespace_gl_idx = whitespace_gl_font_rid.is_valid() ? _font_get_glyph_index(whitespace_gl_font_rid, last_gl_font_size, ' ', 0) : -1;
4726 Vector2 whitespace_adv = whitespace_gl_font_rid.is_valid() ? _font_get_glyph_advance(whitespace_gl_font_rid, last_gl_font_size, whitespace_gl_idx) : Vector2();
4727
4728 int ellipsis_width = 0;
4729 if (add_ellipsis && whitespace_gl_font_rid.is_valid()) {
4730 ellipsis_width = (found_el_char ? 1 : 3) * dot_adv.x + sd->extra_spacing[SPACING_GLYPH] + _font_get_spacing(dot_gl_font_rid, SPACING_GLYPH) + (cut_per_word ? whitespace_adv.x : 0);
4731 }
4732
4733 int ell_min_characters = 6;
4734 double width = sd->width;
4735 double width_without_el = width;
4736
4737 int trim_pos = 0;
4738 int ellipsis_pos = (enforce_ellipsis || short_string_ellipsis) ? 0 : -1;
4739
4740 int last_valid_cut = -1;
4741 int last_valid_cut_witout_el = -1;
4742
4743 if ((enforce_ellipsis || short_string_ellipsis) && (width + ellipsis_width <= p_width)) {
4744 trim_pos = -1;
4745 ellipsis_pos = sd_size;
4746 } else {
4747 for (int i = sd_size - 1; i != -1; i--) {
4748 width -= sd_glyphs[i].advance * sd_glyphs[i].repeat;
4749
4750 if (sd_glyphs[i].count > 0) {
4751 bool above_min_char_threshold = (i >= ell_min_characters);
4752 if (!above_min_char_threshold && last_valid_cut_witout_el != -1) {
4753 trim_pos = last_valid_cut_witout_el;
4754 ellipsis_pos = -1;
4755 width = width_without_el;
4756 break;
4757 }
4758 if (!(enforce_ellipsis || short_string_ellipsis) && width <= p_width && last_valid_cut_witout_el == -1) {
4759 if (cut_per_word && above_min_char_threshold) {
4760 if ((sd_glyphs[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
4761 last_valid_cut_witout_el = i;
4762 width_without_el = width;
4763 }
4764 } else {
4765 last_valid_cut_witout_el = i;
4766 width_without_el = width;
4767 }
4768 }
4769 if (width + (((above_min_char_threshold && add_ellipsis) || enforce_ellipsis || short_string_ellipsis) ? ellipsis_width : 0) <= p_width) {
4770 if (cut_per_word && above_min_char_threshold) {
4771 if ((sd_glyphs[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) {
4772 last_valid_cut = i;
4773 }
4774 } else {
4775 last_valid_cut = i;
4776 }
4777 if (last_valid_cut != -1) {
4778 trim_pos = last_valid_cut;
4779
4780 if (add_ellipsis && (above_min_char_threshold || enforce_ellipsis || short_string_ellipsis) && width - ellipsis_width <= p_width) {
4781 ellipsis_pos = trim_pos;
4782 }
4783 break;
4784 }
4785 }
4786 }
4787 }
4788 }
4789
4790 sd->overrun_trim_data.trim_pos = trim_pos;
4791 sd->overrun_trim_data.ellipsis_pos = ellipsis_pos;
4792 if (trim_pos == 0 && (enforce_ellipsis || short_string_ellipsis) && add_ellipsis) {
4793 sd->overrun_trim_data.ellipsis_pos = 0;
4794 }
4795
4796 if ((trim_pos >= 0 && sd->width > p_width) || enforce_ellipsis || short_string_ellipsis) {
4797 if (add_ellipsis && (ellipsis_pos > 0 || enforce_ellipsis || short_string_ellipsis)) {
4798 // Insert an additional space when cutting word bound for aesthetics.
4799 if (cut_per_word && (ellipsis_pos > 0)) {
4800 Glyph gl;
4801 gl.count = 1;
4802 gl.advance = whitespace_adv.x;
4803 gl.index = whitespace_gl_idx;
4804 gl.font_rid = whitespace_gl_font_rid;
4805 gl.font_size = last_gl_font_size;
4806 gl.flags = GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT | GRAPHEME_IS_VIRTUAL;
4807
4808 sd->overrun_trim_data.ellipsis_glyph_buf.append(gl);
4809 }
4810 // Add ellipsis dots.
4811 if (dot_gl_idx != 0) {
4812 Glyph gl;
4813 gl.count = 1;
4814 gl.repeat = (found_el_char ? 1 : 3);
4815 gl.advance = dot_adv.x;
4816 gl.index = dot_gl_idx;
4817 gl.font_rid = dot_gl_font_rid;
4818 gl.font_size = last_gl_font_size;
4819 gl.flags = GRAPHEME_IS_PUNCTUATION | GRAPHEME_IS_VIRTUAL;
4820
4821 sd->overrun_trim_data.ellipsis_glyph_buf.append(gl);
4822 }
4823 }
4824
4825 sd->text_trimmed = true;
4826 sd->width_trimmed = width + ((ellipsis_pos != -1) ? ellipsis_width : 0);
4827 }
4828 }
4829
4830 int64_t TextServerFallback::_shaped_text_get_trim_pos(const RID &p_shaped) const {
4831 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
4832 ERR_FAIL_NULL_V_MSG(sd, -1, "ShapedTextDataFallback invalid.");
4833
4834 MutexLock lock(sd->mutex);
4835 return sd->overrun_trim_data.trim_pos;
4836 }
4837
4838 int64_t TextServerFallback::_shaped_text_get_ellipsis_pos(const RID &p_shaped) const {
4839 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
4840 ERR_FAIL_NULL_V_MSG(sd, -1, "ShapedTextDataFallback invalid.");
4841
4842 MutexLock lock(sd->mutex);
4843 return sd->overrun_trim_data.ellipsis_pos;
4844 }
4845
4846 const Glyph *TextServerFallback::_shaped_text_get_ellipsis_glyphs(const RID &p_shaped) const {
4847 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
4848 ERR_FAIL_NULL_V_MSG(sd, nullptr, "ShapedTextDataFallback invalid.");
4849
4850 MutexLock lock(sd->mutex);
4851 return sd->overrun_trim_data.ellipsis_glyph_buf.ptr();
4852 }
4853
4854 int64_t TextServerFallback::_shaped_text_get_ellipsis_glyph_count(const RID &p_shaped) const {
4855 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
4856 ERR_FAIL_NULL_V_MSG(sd, 0, "ShapedTextDataFallback invalid.");
4857
4858 MutexLock lock(sd->mutex);
4859 return sd->overrun_trim_data.ellipsis_glyph_buf.size();
4860 }
4861
4862 bool TextServerFallback::_shaped_text_shape(const RID &p_shaped) {
4863 ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
4864 ERR_FAIL_NULL_V(sd, false);
4865
4866 MutexLock lock(sd->mutex);
4867 if (sd->valid.is_set()) {
4868 return true;
4869 }
4870
4871 if (sd->parent != RID()) {
4872 full_copy(sd);
4873 }
4874
4875 // Cleanup.
4876 sd->justification_ops_valid = false;
4877 sd->line_breaks_valid = false;
4878 sd->ascent = 0.0;
4879 sd->descent = 0.0;
4880 sd->width = 0.0;
4881 sd->glyphs.clear();
4882 sd->runs.clear();
4883 sd->runs_dirty = true;
4884
4885 if (sd->text.length() == 0) {
4886 sd->valid.set();
4887 return true;
4888 }
4889
4890 // "Shape" string.
4891 for (int i = 0; i < sd->spans.size(); i++) {
4892 const ShapedTextDataFallback::Span &span = sd->spans[i];
4893 if (span.embedded_key != Variant()) {
4894 // Embedded object.
4895 if (sd->orientation == ORIENTATION_HORIZONTAL) {
4896 sd->objects[span.embedded_key].rect.position.x = sd->width;
4897 sd->width += sd->objects[span.embedded_key].rect.size.x;
4898 } else {
4899 sd->objects[span.embedded_key].rect.position.y = sd->width;
4900 sd->width += sd->objects[span.embedded_key].rect.size.y;
4901 }
4902 Glyph gl;
4903 gl.span_index = i;
4904 gl.start = span.start;
4905 gl.end = span.end;
4906 gl.count = 1;
4907 gl.index = 0;
4908 gl.flags = GRAPHEME_IS_VALID | GRAPHEME_IS_EMBEDDED_OBJECT;
4909 if (sd->orientation == ORIENTATION_HORIZONTAL) {
4910 gl.advance = sd->objects[span.embedded_key].rect.size.x;
4911 } else {
4912 gl.advance = sd->objects[span.embedded_key].rect.size.y;
4913 }
4914 sd->glyphs.push_back(gl);
4915 } else {
4916 // Text span.
4917 int last_non_zero_w = sd->end - 1;
4918 if (i == sd->spans.size() - 1) {
4919 for (int j = span.end - 1; j >= span.start; j--) {
4920 last_non_zero_w = j;
4921 uint32_t idx = (int32_t)sd->text[j - sd->start];
4922 if (!is_control(idx) && !(idx >= 0x200B && idx <= 0x200D)) {
4923 break;
4924 }
4925 }
4926 }
4927
4928 RID prev_font;
4929 for (int j = span.start; j < span.end; j++) {
4930 Glyph gl;
4931 gl.span_index = i;
4932 gl.start = j;
4933 gl.end = j + 1;
4934 gl.count = 1;
4935 gl.font_size = span.font_size;
4936 gl.index = (int32_t)sd->text[j - sd->start]; // Use codepoint.
4937 bool zw = (gl.index >= 0x200b && gl.index <= 0x200d);
4938 if (gl.index == 0x0009 || gl.index == 0x000b || zw) {
4939 gl.index = 0x0020;
4940 }
4941 if (!sd->preserve_control && is_control(gl.index)) {
4942 gl.index = 0x0020;
4943 }
4944 // Select first font which has character (font are already sorted by span language).
4945 for (int k = 0; k < span.fonts.size(); k++) {
4946 if (_font_has_char(span.fonts[k], gl.index)) {
4947 gl.font_rid = span.fonts[k];
4948 break;
4949 }
4950 }
4951 if (!gl.font_rid.is_valid() && prev_font.is_valid()) {
4952 if (_font_has_char(prev_font, gl.index)) {
4953 gl.font_rid = prev_font;
4954 }
4955 }
4956 if (!gl.font_rid.is_valid() && OS::get_singleton()->has_feature("system_fonts") && span.fonts.size() > 0) {
4957 // Try system fallback.
4958 RID fdef = span.fonts[0];
4959 if (_font_is_allow_system_fallback(fdef)) {
4960 String text = sd->text.substr(j, 1);
4961 gl.font_rid = _find_sys_font_for_text(fdef, String(), span.language, text);
4962 }
4963 }
4964 prev_font = gl.font_rid;
4965
4966 if (gl.font_rid.is_valid()) {
4967 double scale = _font_get_scale(gl.font_rid, gl.font_size);
4968 bool subpos = (scale != 1.0) || (_font_get_subpixel_positioning(gl.font_rid) == SUBPIXEL_POSITIONING_ONE_HALF) || (_font_get_subpixel_positioning(gl.font_rid) == SUBPIXEL_POSITIONING_ONE_QUARTER) || (_font_get_subpixel_positioning(gl.font_rid) == SUBPIXEL_POSITIONING_AUTO && gl.font_size <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE);
4969 if (sd->text[j - sd->start] != 0 && !is_linebreak(sd->text[j - sd->start])) {
4970 if (sd->orientation == ORIENTATION_HORIZONTAL) {
4971 gl.advance = _font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x;
4972 gl.x_off = 0;
4973 gl.y_off = _font_get_baseline_offset(gl.font_rid) * (double)(_font_get_ascent(gl.font_rid, gl.font_size) + _font_get_descent(gl.font_rid, gl.font_size));
4974 sd->ascent = MAX(sd->ascent, _font_get_ascent(gl.font_rid, gl.font_size) + _font_get_spacing(gl.font_rid, SPACING_TOP));
4975 sd->descent = MAX(sd->descent, _font_get_descent(gl.font_rid, gl.font_size) + _font_get_spacing(gl.font_rid, SPACING_BOTTOM));
4976 } else {
4977 gl.advance = _font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).y;
4978 gl.x_off = -Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5) + _font_get_baseline_offset(gl.font_rid) * (double)(_font_get_ascent(gl.font_rid, gl.font_size) + _font_get_descent(gl.font_rid, gl.font_size));
4979 gl.y_off = _font_get_ascent(gl.font_rid, gl.font_size);
4980 sd->ascent = MAX(sd->ascent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
4981 sd->descent = MAX(sd->descent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5));
4982 }
4983 }
4984 if (zw) {
4985 gl.advance = 0.0;
4986 }
4987 if ((j < last_non_zero_w) && !Math::is_zero_approx(gl.advance)) {
4988 // Do not add extra spacing to the last glyph of the string and zero width glyphs.
4989 if (is_whitespace(sd->text[j - sd->start])) {
4990 gl.advance += sd->extra_spacing[SPACING_SPACE] + _font_get_spacing(gl.font_rid, SPACING_SPACE);
4991 } else {
4992 gl.advance += sd->extra_spacing[SPACING_GLYPH] + _font_get_spacing(gl.font_rid, SPACING_GLYPH);
4993 }
4994 }
4995 sd->upos = MAX(sd->upos, _font_get_underline_position(gl.font_rid, gl.font_size));
4996 sd->uthk = MAX(sd->uthk, _font_get_underline_thickness(gl.font_rid, gl.font_size));
4997
4998 // Add kerning to previous glyph.
4999 if (sd->glyphs.size() > 0) {
5000 Glyph &prev_gl = sd->glyphs.write[sd->glyphs.size() - 1];
5001 if (prev_gl.font_rid == gl.font_rid && prev_gl.font_size == gl.font_size) {
5002 if (sd->orientation == ORIENTATION_HORIZONTAL) {
5003 prev_gl.advance += _font_get_kerning(gl.font_rid, gl.font_size, Vector2i(prev_gl.index, gl.index)).x;
5004 } else {
5005 prev_gl.advance += _font_get_kerning(gl.font_rid, gl.font_size, Vector2i(prev_gl.index, gl.index)).y;
5006 }
5007 }
5008 }
5009 if (sd->orientation == ORIENTATION_HORIZONTAL && !subpos) {
5010 gl.advance = Math::round(gl.advance);
5011 }
5012 } else if (sd->preserve_invalid || (sd->preserve_control && is_control(gl.index))) {
5013 // Glyph not found, replace with hex code box.
5014 if (sd->orientation == ORIENTATION_HORIZONTAL) {
5015 gl.advance = get_hex_code_box_size(gl.font_size, gl.index).x;
5016 sd->ascent = MAX(sd->ascent, get_hex_code_box_size(gl.font_size, gl.index).y * 0.85);
5017 sd->descent = MAX(sd->descent, get_hex_code_box_size(gl.font_size, gl.index).y * 0.15);
5018 } else {
5019 gl.advance = get_hex_code_box_size(gl.font_size, gl.index).y;
5020 sd->ascent = MAX(sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
5021 sd->descent = MAX(sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5));
5022 }
5023 }
5024 sd->width += gl.advance;
5025 sd->glyphs.push_back(gl);
5026 }
5027 }
5028 }
5029
5030 // Align embedded objects to baseline.
5031 _realign(sd);
5032
5033 sd->valid.set();
5034 return sd->valid.is_set();
5035 }
5036
5037 bool TextServerFallback::_shaped_text_is_ready(const RID &p_shaped) const {
5038 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
5039 ERR_FAIL_NULL_V(sd, false);
5040
5041 return sd->valid.is_set();
5042 }
5043
5044 const Glyph *TextServerFallback::_shaped_text_get_glyphs(const RID &p_shaped) const {
5045 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
5046 ERR_FAIL_NULL_V(sd, nullptr);
5047
5048 MutexLock lock(sd->mutex);
5049 if (!sd->valid.is_set()) {
5050 const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
5051 }
5052 return sd->glyphs.ptr();
5053 }
5054
5055 int64_t TextServerFallback::_shaped_text_get_glyph_count(const RID &p_shaped) const {
5056 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
5057 ERR_FAIL_NULL_V(sd, 0);
5058
5059 MutexLock lock(sd->mutex);
5060 if (!sd->valid.is_set()) {
5061 const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
5062 }
5063 return sd->glyphs.size();
5064 }
5065
5066 const Glyph *TextServerFallback::_shaped_text_sort_logical(const RID &p_shaped) {
5067 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
5068 ERR_FAIL_NULL_V(sd, nullptr);
5069
5070 MutexLock lock(sd->mutex);
5071 if (!sd->valid.is_set()) {
5072 _shaped_text_shape(p_shaped);
5073 }
5074
5075 return sd->glyphs.ptr(); // Already in the logical order, return as is.
5076 }
5077
5078 Vector2i TextServerFallback::_shaped_text_get_range(const RID &p_shaped) const {
5079 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
5080 ERR_FAIL_NULL_V(sd, Vector2i());
5081
5082 MutexLock lock(sd->mutex);
5083 return Vector2(sd->start, sd->end);
5084 }
5085
5086 Array TextServerFallback::_shaped_text_get_objects(const RID &p_shaped) const {
5087 Array ret;
5088 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
5089 ERR_FAIL_NULL_V(sd, ret);
5090
5091 MutexLock lock(sd->mutex);
5092 for (const KeyValue<Variant, ShapedTextDataFallback::EmbeddedObject> &E : sd->objects) {
5093 ret.push_back(E.key);
5094 }
5095
5096 return ret;
5097 }
5098
5099 Rect2 TextServerFallback::_shaped_text_get_object_rect(const RID &p_shaped, const Variant &p_key) const {
5100 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
5101 ERR_FAIL_NULL_V(sd, Rect2());
5102
5103 MutexLock lock(sd->mutex);
5104 ERR_FAIL_COND_V(!sd->objects.has(p_key), Rect2());
5105 if (!sd->valid.is_set()) {
5106 const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
5107 }
5108 return sd->objects[p_key].rect;
5109 }
5110
5111 Vector2i TextServerFallback::_shaped_text_get_object_range(const RID &p_shaped, const Variant &p_key) const {
5112 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
5113 ERR_FAIL_NULL_V(sd, Vector2i());
5114
5115 MutexLock lock(sd->mutex);
5116 ERR_FAIL_COND_V(!sd->objects.has(p_key), Vector2i());
5117 return Vector2i(sd->objects[p_key].start, sd->objects[p_key].end);
5118 }
5119
5120 int64_t TextServerFallback::_shaped_text_get_object_glyph(const RID &p_shaped, const Variant &p_key) const {
5121 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
5122 ERR_FAIL_NULL_V(sd, -1);
5123
5124 MutexLock lock(sd->mutex);
5125 ERR_FAIL_COND_V(!sd->objects.has(p_key), -1);
5126 const ShapedTextDataFallback::EmbeddedObject &obj = sd->objects[p_key];
5127 int sd_size = sd->glyphs.size();
5128 const Glyph *sd_glyphs = sd->glyphs.ptr();
5129 for (int i = 0; i < sd_size; i++) {
5130 if (obj.start == sd_glyphs[i].start) {
5131 return i;
5132 }
5133 }
5134 return -1;
5135 }
5136
5137 Size2 TextServerFallback::_shaped_text_get_size(const RID &p_shaped) const {
5138 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
5139 ERR_FAIL_NULL_V(sd, Size2());
5140
5141 MutexLock lock(sd->mutex);
5142 if (!sd->valid.is_set()) {
5143 const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
5144 }
5145 if (sd->orientation == TextServer::ORIENTATION_HORIZONTAL) {
5146 return Size2(sd->width, sd->ascent + sd->descent + sd->extra_spacing[SPACING_TOP] + sd->extra_spacing[SPACING_BOTTOM]).ceil();
5147 } else {
5148 return Size2(sd->ascent + sd->descent + sd->extra_spacing[SPACING_TOP] + sd->extra_spacing[SPACING_BOTTOM], sd->width).ceil();
5149 }
5150 }
5151
5152 double TextServerFallback::_shaped_text_get_ascent(const RID &p_shaped) const {
5153 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
5154 ERR_FAIL_NULL_V(sd, 0.0);
5155
5156 MutexLock lock(sd->mutex);
5157 if (!sd->valid.is_set()) {
5158 const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
5159 }
5160 return sd->ascent + sd->extra_spacing[SPACING_TOP];
5161 }
5162
5163 double TextServerFallback::_shaped_text_get_descent(const RID &p_shaped) const {
5164 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
5165 ERR_FAIL_NULL_V(sd, 0.0);
5166
5167 MutexLock lock(sd->mutex);
5168 if (!sd->valid.is_set()) {
5169 const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
5170 }
5171 return sd->descent + sd->extra_spacing[SPACING_BOTTOM];
5172 }
5173
5174 double TextServerFallback::_shaped_text_get_width(const RID &p_shaped) const {
5175 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
5176 ERR_FAIL_NULL_V(sd, 0.0);
5177
5178 MutexLock lock(sd->mutex);
5179 if (!sd->valid.is_set()) {
5180 const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
5181 }
5182 return Math::ceil(sd->width);
5183 }
5184
5185 double TextServerFallback::_shaped_text_get_underline_position(const RID &p_shaped) const {
5186 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
5187 ERR_FAIL_NULL_V(sd, 0.0);
5188
5189 MutexLock lock(sd->mutex);
5190 if (!sd->valid.is_set()) {
5191 const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
5192 }
5193
5194 return sd->upos;
5195 }
5196
5197 double TextServerFallback::_shaped_text_get_underline_thickness(const RID &p_shaped) const {
5198 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
5199 ERR_FAIL_NULL_V(sd, 0.0);
5200
5201 MutexLock lock(sd->mutex);
5202 if (!sd->valid.is_set()) {
5203 const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
5204 }
5205
5206 return sd->uthk;
5207 }
5208
5209 PackedInt32Array TextServerFallback::_shaped_text_get_character_breaks(const RID &p_shaped) const {
5210 const ShapedTextDataFallback *sd = shaped_owner.get_or_null(p_shaped);
5211 ERR_FAIL_NULL_V(sd, PackedInt32Array());
5212
5213 MutexLock lock(sd->mutex);
5214 if (!sd->valid.is_set()) {
5215 const_cast<TextServerFallback *>(this)->_shaped_text_shape(p_shaped);
5216 }
5217
5218 PackedInt32Array ret;
5219 int size = sd->end - sd->start;
5220 if (size > 0) {
5221 ret.resize(size);
5222 for (int i = 0; i < size; i++) {
5223 #ifdef GDEXTENSION
5224 ret[i] = i + 1 + sd->start;
5225 #else
5226 ret.write[i] = i + 1 + sd->start;
5227 #endif
5228 }
5229 }
5230 return ret;
5231 }
5232
5233 String TextServerFallback::_string_to_upper(const String &p_string, const String &p_language) const {
5234 return p_string.to_upper();
5235 }
5236
5237 String TextServerFallback::_string_to_lower(const String &p_string, const String &p_language) const {
5238 return p_string.to_lower();
5239 }
5240
5241 String TextServerFallback::_string_to_title(const String &p_string, const String &p_language) const {
5242 return p_string.capitalize();
5243 }
5244
5245 PackedInt32Array TextServerFallback::_string_get_word_breaks(const String &p_string, const String &p_language, int64_t p_chars_per_line) const {
5246 PackedInt32Array ret;
5247
5248 if (p_chars_per_line > 0) {
5249 int line_start = 0;
5250 int last_break = -1;
5251 int line_length = 0;
5252
5253 for (int i = 0; i < p_string.length(); i++) {
5254 const char32_t c = p_string[i];
5255
5256 bool is_lb = is_linebreak(c);
5257 bool is_ws = is_whitespace(c);
5258 bool is_p = (is_punct(c) && c != 0x005F) || is_underscore(c) || c == '\t' || c == 0xfffc;
5259
5260 if (is_lb) {
5261 if (line_length > 0) {
5262 ret.push_back(line_start);
5263 ret.push_back(i);
5264 }
5265 line_start = i;
5266 line_length = 0;
5267 last_break = -1;
5268 continue;
5269 } else if (is_ws || is_p) {
5270 last_break = i;
5271 }
5272
5273 if (line_length == p_chars_per_line) {
5274 if (last_break != -1) {
5275 int last_break_w_spaces = last_break;
5276 while (last_break > line_start && is_whitespace(p_string[last_break - 1])) {
5277 last_break--;
5278 }
5279 if (line_start != last_break) {
5280 ret.push_back(line_start);
5281 ret.push_back(last_break);
5282 }
5283 while (last_break_w_spaces < p_string.length() && is_whitespace(p_string[last_break_w_spaces])) {
5284 last_break_w_spaces++;
5285 }
5286 line_start = last_break_w_spaces;
5287 if (last_break_w_spaces < i) {
5288 line_length = i - last_break_w_spaces;
5289 } else {
5290 i = last_break_w_spaces;
5291 line_length = 0;
5292 }
5293 } else {
5294 ret.push_back(line_start);
5295 ret.push_back(i);
5296 line_start = i;
5297 line_length = 0;
5298 }
5299 last_break = -1;
5300 }
5301 line_length++;
5302 }
5303 if (line_length > 0) {
5304 ret.push_back(line_start);
5305 ret.push_back(p_string.length());
5306 }
5307 } else {
5308 int word_start = 0; // -1 if no word encountered. Leading spaces are part of a word.
5309 int word_length = 0;
5310
5311 for (int i = 0; i < p_string.length(); i++) {
5312 const char32_t c = p_string[i];
5313
5314 bool is_lb = is_linebreak(c);
5315 bool is_ws = is_whitespace(c);
5316 bool is_p = (is_punct(c) && c != 0x005F) || is_underscore(c) || c == '\t' || c == 0xfffc;
5317
5318 if (word_start == -1) {
5319 if (!is_lb && !is_ws && !is_p) {
5320 word_start = i;
5321 }
5322 continue;
5323 }
5324
5325 if (is_lb) {
5326 if (word_start != -1 && word_length > 0) {
5327 ret.push_back(word_start);
5328 ret.push_back(i);
5329 }
5330 word_start = -1;
5331 word_length = 0;
5332 } else if (is_ws || is_p) {
5333 if (word_start != -1 && word_length > 0) {
5334 ret.push_back(word_start);
5335 ret.push_back(i);
5336 }
5337 word_start = -1;
5338 word_length = 0;
5339 }
5340
5341 word_length++;
5342 }
5343 if (word_start != -1 && word_length > 0) {
5344 ret.push_back(word_start);
5345 ret.push_back(p_string.length());
5346 }
5347 }
5348 return ret;
5349 }
5350
5351 void TextServerFallback::_update_settings() {
5352 lcd_subpixel_layout.set((TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout"));
5353 }
5354
5355 TextServerFallback::TextServerFallback() {
5356 _insert_feature_sets();
5357 ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &TextServerFallback::_update_settings));
5358 }
5359
5360 void TextServerFallback::_font_clear_system_fallback_cache() {
5361 for (const KeyValue<SystemFontKey, SystemFontCache> &E : system_fonts) {
5362 const Vector<SystemFontCacheRec> &sysf_cache = E.value.var;
5363 for (const SystemFontCacheRec &F : sysf_cache) {
5364 _free_rid(F.rid);
5365 }
5366 }
5367 system_fonts.clear();
5368 system_font_data.clear();
5369 }
5370
5371 void TextServerFallback::_cleanup() {
5372 font_clear_system_fallback_cache();
5373 }
5374
5375 TextServerFallback::~TextServerFallback() {
5376 #ifdef MODULE_FREETYPE_ENABLED
5377 if (ft_library != nullptr) {
5378 FT_Done_FreeType(ft_library);
5379 }
5380 #endif
5381 }