Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/text_server_fb/text_server_fb.h
20960 views
1
/**************************************************************************/
2
/* text_server_fb.h */
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
#pragma once
32
33
/*************************************************************************/
34
/* Fallback Text Server provides simplified TS functionality, without */
35
/* BiDi, shaping and advanced font features support. */
36
/*************************************************************************/
37
38
#ifdef GDEXTENSION
39
// Headers for building as GDExtension plug-in.
40
41
#include <godot_cpp/godot.hpp>
42
43
#include <godot_cpp/core/class_db.hpp>
44
#include <godot_cpp/core/ext_wrappers.gen.inc>
45
#include <godot_cpp/core/mutex_lock.hpp>
46
47
#include <godot_cpp/variant/array.hpp>
48
#include <godot_cpp/variant/dictionary.hpp>
49
#include <godot_cpp/variant/packed_int32_array.hpp>
50
#include <godot_cpp/variant/packed_string_array.hpp>
51
#include <godot_cpp/variant/packed_vector2_array.hpp>
52
#include <godot_cpp/variant/rect2.hpp>
53
#include <godot_cpp/variant/rid.hpp>
54
#include <godot_cpp/variant/string.hpp>
55
#include <godot_cpp/variant/typed_array.hpp>
56
#include <godot_cpp/variant/vector2.hpp>
57
#include <godot_cpp/variant/vector2i.hpp>
58
59
#include <godot_cpp/classes/text_server.hpp>
60
#include <godot_cpp/classes/text_server_extension.hpp>
61
#include <godot_cpp/classes/text_server_manager.hpp>
62
63
#include <godot_cpp/classes/caret_info.hpp>
64
#include <godot_cpp/classes/global_constants_binds.hpp>
65
#include <godot_cpp/classes/glyph.hpp>
66
#include <godot_cpp/classes/image.hpp>
67
#include <godot_cpp/classes/image_texture.hpp>
68
#include <godot_cpp/classes/ref.hpp>
69
#include <godot_cpp/classes/worker_thread_pool.hpp>
70
71
#include <godot_cpp/templates/hash_map.hpp>
72
#include <godot_cpp/templates/hash_set.hpp>
73
#include <godot_cpp/templates/rid_owner.hpp>
74
#include <godot_cpp/templates/safe_refcount.hpp>
75
#include <godot_cpp/templates/vector.hpp>
76
77
using namespace godot;
78
79
#elif defined(GODOT_MODULE)
80
// Headers for building as built-in module.
81
82
#include "core/extension/ext_wrappers.gen.h"
83
#include "core/object/worker_thread_pool.h"
84
#include "core/templates/hash_map.h"
85
#include "core/templates/rid_owner.h"
86
#include "core/templates/safe_refcount.h"
87
#include "scene/resources/image_texture.h"
88
#include "servers/text/text_server_extension.h"
89
90
#include "modules/modules_enabled.gen.h" // For freetype, msdfgen, svg.
91
92
#endif
93
94
// Thirdparty headers.
95
96
#ifdef MODULE_FREETYPE_ENABLED
97
#include <ft2build.h>
98
#include FT_FREETYPE_H
99
#include FT_TRUETYPE_TABLES_H
100
#include FT_STROKER_H
101
#include FT_ADVANCES_H
102
#include FT_MULTIPLE_MASTERS_H
103
#include FT_BBOX_H
104
#include FT_SIZES_H
105
#include FT_MODULE_H
106
#include FT_CONFIG_OPTIONS_H
107
#if !defined(FT_CONFIG_OPTION_USE_BROTLI) && !defined(_MSC_VER)
108
#warning FreeType is configured without Brotli support, built-in fonts will not be available.
109
#endif
110
#endif
111
112
/*************************************************************************/
113
114
class TextServerFallback : public TextServerExtension {
115
GDCLASS(TextServerFallback, TextServerExtension);
116
_THREAD_SAFE_CLASS_
117
118
HashMap<StringName, int32_t> feature_sets;
119
HashMap<int32_t, StringName> feature_sets_inv;
120
121
SafeNumeric<TextServer::FontLCDSubpixelLayout> lcd_subpixel_layout{ TextServer::FontLCDSubpixelLayout::FONT_LCD_SUBPIXEL_LAYOUT_NONE };
122
void _update_settings();
123
124
void _insert_feature_sets();
125
_FORCE_INLINE_ void _insert_feature(const StringName &p_name, int32_t p_tag);
126
127
// Font cache data.
128
129
#ifdef MODULE_FREETYPE_ENABLED
130
mutable FT_Library ft_library = nullptr;
131
#endif
132
133
const int rect_range = 1;
134
135
struct FontTexturePosition {
136
int32_t index = -1;
137
int32_t x = 0;
138
int32_t y = 0;
139
140
FontTexturePosition() {}
141
FontTexturePosition(int32_t p_id, int32_t p_x, int32_t p_y) :
142
index(p_id), x(p_x), y(p_y) {}
143
};
144
145
struct Shelf {
146
int32_t x = 0;
147
int32_t y = 0;
148
int32_t w = 0;
149
int32_t h = 0;
150
151
FontTexturePosition alloc_shelf(int32_t p_id, int32_t p_w, int32_t p_h) {
152
if (p_w > w || p_h > h) {
153
return FontTexturePosition(-1, 0, 0);
154
}
155
int32_t xx = x;
156
x += p_w;
157
w -= p_w;
158
return FontTexturePosition(p_id, xx, y);
159
}
160
161
Shelf() {}
162
Shelf(int32_t p_x, int32_t p_y, int32_t p_w, int32_t p_h) :
163
x(p_x), y(p_y), w(p_w), h(p_h) {}
164
};
165
166
struct ShelfPackTexture {
167
int32_t texture_w = 1024;
168
int32_t texture_h = 1024;
169
170
Ref<Image> image;
171
Ref<ImageTexture> texture;
172
bool dirty = true;
173
174
List<Shelf> shelves;
175
176
FontTexturePosition pack_rect(int32_t p_id, int32_t p_h, int32_t p_w) {
177
int32_t y = 0;
178
int32_t waste = 0;
179
Shelf *best_shelf = nullptr;
180
int32_t best_waste = std::numeric_limits<std::int32_t>::max();
181
182
for (Shelf &E : shelves) {
183
y += E.h;
184
if (p_w > E.w) {
185
continue;
186
}
187
if (p_h == E.h) {
188
return E.alloc_shelf(p_id, p_w, p_h);
189
}
190
if (p_h > E.h) {
191
continue;
192
}
193
if (p_h < E.h) {
194
waste = (E.h - p_h) * p_w;
195
if (waste < best_waste) {
196
best_waste = waste;
197
best_shelf = &E;
198
}
199
}
200
}
201
if (best_shelf) {
202
return best_shelf->alloc_shelf(p_id, p_w, p_h);
203
}
204
if (p_h <= (texture_h - y) && p_w <= texture_w) {
205
List<Shelf>::Element *E = shelves.push_back(Shelf(0, y, texture_w, p_h));
206
return E->get().alloc_shelf(p_id, p_w, p_h);
207
}
208
return FontTexturePosition(-1, 0, 0);
209
}
210
211
ShelfPackTexture() {}
212
ShelfPackTexture(int32_t p_w, int32_t p_h) :
213
texture_w(p_w), texture_h(p_h) {}
214
};
215
216
struct FontGlyph {
217
bool found = false;
218
int texture_idx = -1;
219
Rect2 rect;
220
Rect2 uv_rect;
221
Vector2 advance;
222
bool from_svg = false;
223
};
224
225
struct FontFallback;
226
struct FontForSizeFallback {
227
double ascent = 0.0;
228
double descent = 0.0;
229
double underline_position = 0.0;
230
double underline_thickness = 0.0;
231
double scale = 1.0;
232
233
FontFallback *owner = nullptr;
234
uint32_t viewport_oversampling = 0;
235
236
Vector2i size;
237
238
Vector<ShelfPackTexture> textures;
239
HashMap<int32_t, FontGlyph> glyph_map;
240
HashMap<Vector2i, Vector2> kerning_map;
241
242
#ifdef MODULE_FREETYPE_ENABLED
243
FT_Size fsize = nullptr;
244
#endif
245
246
~FontForSizeFallback() {
247
#ifdef MODULE_FREETYPE_ENABLED
248
if (fsize != nullptr) {
249
FT_Done_Size(fsize);
250
}
251
#endif
252
}
253
};
254
255
struct OversamplingLevel {
256
HashSet<FontForSizeFallback *> fonts;
257
int32_t refcount = 1;
258
};
259
260
mutable HashMap<uint32_t, OversamplingLevel> oversampling_levels;
261
262
struct FontFallbackLinkedVariation {
263
RID base_font;
264
int extra_spacing[4] = { 0, 0, 0, 0 };
265
double baseline_offset = 0.0;
266
};
267
268
struct FontFallback {
269
Mutex mutex;
270
271
TextServer::FontAntialiasing antialiasing = TextServer::FONT_ANTIALIASING_GRAY;
272
bool disable_embedded_bitmaps = true;
273
bool mipmaps = false;
274
bool msdf = false;
275
FixedSizeScaleMode fixed_size_scale_mode = FIXED_SIZE_SCALE_DISABLE;
276
int msdf_range = 14;
277
int msdf_source_size = 48;
278
int fixed_size = 0;
279
bool force_autohinter = false;
280
bool allow_system_fallback = true;
281
bool modulate_color_glyphs = false;
282
TextServer::Hinting hinting = TextServer::HINTING_LIGHT;
283
TextServer::SubpixelPositioning subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_AUTO;
284
bool keep_rounding_remainders = true;
285
Dictionary variation_coordinates;
286
double oversampling_override = 0.0;
287
double embolden = 0.0;
288
Transform2D transform;
289
290
BitField<TextServer::FontStyle> style_flags = 0;
291
String font_name;
292
String style_name;
293
int weight = 400;
294
int stretch = 100;
295
int extra_spacing[4] = { 0, 0, 0, 0 };
296
double baseline_offset = 0.0;
297
298
HashMap<Vector2i, FontForSizeFallback *> cache;
299
300
bool face_init = false;
301
Dictionary supported_varaitions;
302
Dictionary feature_overrides;
303
304
// Language/script support override.
305
HashMap<String, bool> language_support_overrides;
306
HashMap<String, bool> script_support_overrides;
307
308
PackedByteArray data;
309
const uint8_t *data_ptr = nullptr;
310
size_t data_size;
311
int face_index = 0;
312
313
#ifdef MODULE_FREETYPE_ENABLED
314
FT_Face face = nullptr;
315
FT_StreamRec stream;
316
#endif
317
318
~FontFallback() {
319
for (const KeyValue<Vector2i, FontForSizeFallback *> &E : cache) {
320
memdelete(E.value);
321
}
322
cache.clear();
323
#ifdef MODULE_FREETYPE_ENABLED
324
if (face != nullptr) {
325
FT_Done_Face(face);
326
}
327
#endif
328
}
329
};
330
331
_FORCE_INLINE_ FontTexturePosition 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;
332
#ifdef MODULE_MSDFGEN_ENABLED
333
_FORCE_INLINE_ FontGlyph 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;
334
#endif
335
#ifdef MODULE_FREETYPE_ENABLED
336
_FORCE_INLINE_ FontGlyph 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;
337
#endif
338
bool _ensure_glyph(FontFallback *p_font_data, const Vector2i &p_size, int32_t p_glyph, FontGlyph &r_glyph, uint32_t p_oversampling = 0) const;
339
bool _ensure_cache_for_size(FontFallback *p_font_data, const Vector2i &p_size, FontForSizeFallback *&r_cache_for_size, bool p_silent = false, uint32_t p_oversampling = 0) const;
340
_FORCE_INLINE_ bool _font_validate(const RID &p_font_rid) const;
341
_FORCE_INLINE_ void _font_clear_cache(FontFallback *p_font_data);
342
static void _generateMTSDF_threaded(void *p_td, uint32_t p_y);
343
344
_FORCE_INLINE_ Vector2i _get_size(const FontFallback *p_font_data, int p_size) const {
345
if (p_font_data->msdf) {
346
return Vector2i(p_font_data->msdf_source_size * 64, 0);
347
} else if (p_font_data->fixed_size > 0) {
348
return Vector2i(p_font_data->fixed_size * 64, 0);
349
} else {
350
return Vector2i(p_size * 64, 0);
351
}
352
}
353
354
_FORCE_INLINE_ Vector2i _get_size_outline(const FontFallback *p_font_data, const Vector2i &p_size) const {
355
if (p_font_data->msdf) {
356
return Vector2i(p_font_data->msdf_source_size * 64, 0);
357
} else if (p_font_data->fixed_size > 0) {
358
return Vector2i(p_font_data->fixed_size * 64, MIN(p_size.y, 1));
359
} else {
360
return Vector2i(p_size.x * 64, p_size.y);
361
}
362
}
363
364
_FORCE_INLINE_ int _font_get_weight_by_name(const String &p_sty_name) const {
365
String sty_name = p_sty_name.remove_chars(" -");
366
if (sty_name.contains("thin") || sty_name.contains("hairline")) {
367
return 100;
368
} else if (sty_name.contains("extralight") || sty_name.contains("ultralight")) {
369
return 200;
370
} else if (sty_name.contains("light")) {
371
return 300;
372
} else if (sty_name.contains("semilight")) {
373
return 350;
374
} else if (sty_name.contains("regular")) {
375
return 400;
376
} else if (sty_name.contains("medium")) {
377
return 500;
378
} else if (sty_name.contains("semibold") || sty_name.contains("demibold")) {
379
return 600;
380
} else if (sty_name.contains("bold")) {
381
return 700;
382
} else if (sty_name.contains("extrabold") || sty_name.contains("ultrabold")) {
383
return 800;
384
} else if (sty_name.contains("black") || sty_name.contains("heavy")) {
385
return 900;
386
} else if (sty_name.contains("extrablack") || sty_name.contains("ultrablack")) {
387
return 950;
388
}
389
return 400;
390
}
391
_FORCE_INLINE_ int _font_get_stretch_by_name(const String &p_sty_name) const {
392
String sty_name = p_sty_name.remove_chars(" -");
393
if (sty_name.contains("ultracondensed")) {
394
return 50;
395
} else if (sty_name.contains("extracondensed")) {
396
return 63;
397
} else if (sty_name.contains("condensed")) {
398
return 75;
399
} else if (sty_name.contains("semicondensed")) {
400
return 87;
401
} else if (sty_name.contains("semiexpanded")) {
402
return 113;
403
} else if (sty_name.contains("expanded")) {
404
return 125;
405
} else if (sty_name.contains("extraexpanded")) {
406
return 150;
407
} else if (sty_name.contains("ultraexpanded")) {
408
return 200;
409
}
410
return 100;
411
}
412
_FORCE_INLINE_ bool _is_ital_style(const String &p_sty_name) const {
413
return p_sty_name.contains("italic") || p_sty_name.contains("oblique");
414
}
415
416
// Shaped text cache data.
417
struct TrimData {
418
int trim_pos = -1;
419
int ellipsis_pos = -1;
420
Vector<Glyph> ellipsis_glyph_buf;
421
};
422
423
struct TextRun {
424
Vector2i range;
425
RID font_rid;
426
int font_size = 0;
427
int64_t span_index = -1;
428
};
429
430
struct ShapedTextDataFallback {
431
Mutex mutex;
432
433
/* Source data */
434
RID parent; // Substring parent ShapedTextData.
435
436
int start = 0; // Substring start offset in the parent string.
437
int end = 0; // Substring end offset in the parent string.
438
439
String text;
440
String custom_punct;
441
TextServer::Direction direction = DIRECTION_LTR; // Desired text direction.
442
TextServer::Orientation orientation = ORIENTATION_HORIZONTAL;
443
444
struct Span {
445
int start = -1;
446
int end = -1;
447
448
Array fonts;
449
int font_size = 0;
450
451
Variant embedded_key;
452
453
String language;
454
Dictionary features;
455
Variant meta;
456
};
457
Vector<Span> spans;
458
int first_span = 0; // First span in the parent ShapedTextData.
459
int last_span = 0;
460
461
Vector<TextRun> runs;
462
bool runs_dirty = true;
463
464
struct EmbeddedObject {
465
int start = -1;
466
int end = -1;
467
InlineAlignment inline_align = INLINE_ALIGNMENT_CENTER;
468
Rect2 rect;
469
double baseline = 0;
470
};
471
HashMap<Variant, EmbeddedObject> objects;
472
473
/* Shaped data */
474
TextServer::Direction para_direction = DIRECTION_LTR; // Detected text direction.
475
SafeFlag valid{ false }; // String is shaped.
476
bool line_breaks_valid = false; // Line and word break flags are populated (and virtual zero width spaces inserted).
477
bool justification_ops_valid = false; // Virtual elongation glyphs are added to the string.
478
bool sort_valid = false;
479
bool text_trimmed = false;
480
481
bool preserve_invalid = true; // Draw hex code box instead of missing characters.
482
bool preserve_control = false; // Draw control characters.
483
484
double ascent = 0.0; // Ascent for horizontal layout, 1/2 of width for vertical.
485
double descent = 0.0; // Descent for horizontal layout, 1/2 of width for vertical.
486
double width = 0.0; // Width for horizontal layout, height for vertical.
487
double width_trimmed = 0.0;
488
int extra_spacing[4] = { 0, 0, 0, 0 };
489
490
double upos = 0.0;
491
double uthk = 0.0;
492
493
char32_t el_char = 0x2026;
494
TrimData overrun_trim_data;
495
bool fit_width_minimum_reached = false;
496
497
Vector<Glyph> glyphs;
498
Vector<Glyph> glyphs_logical;
499
};
500
501
// Common data.
502
503
mutable RID_PtrOwner<FontFallbackLinkedVariation> font_var_owner;
504
mutable RID_PtrOwner<FontFallback> font_owner;
505
mutable RID_PtrOwner<ShapedTextDataFallback> shaped_owner;
506
507
_FORCE_INLINE_ FontFallback *_get_font_data(const RID &p_font_rid) const {
508
RID rid = p_font_rid;
509
FontFallbackLinkedVariation *fdv = font_var_owner.get_or_null(rid);
510
if (unlikely(fdv)) {
511
rid = fdv->base_font;
512
}
513
return font_owner.get_or_null(rid);
514
}
515
516
struct SystemFontKey {
517
String font_name;
518
TextServer::FontAntialiasing antialiasing = TextServer::FONT_ANTIALIASING_GRAY;
519
bool disable_embedded_bitmaps = true;
520
bool italic = false;
521
bool mipmaps = false;
522
bool msdf = false;
523
bool force_autohinter = false;
524
int weight = 400;
525
int stretch = 100;
526
int msdf_range = 14;
527
int msdf_source_size = 48;
528
int fixed_size = 0;
529
TextServer::Hinting hinting = TextServer::HINTING_LIGHT;
530
TextServer::SubpixelPositioning subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_AUTO;
531
bool keep_rounding_remainders = true;
532
Dictionary variation_coordinates;
533
double embolden = 0.0;
534
Transform2D transform;
535
int extra_spacing[4] = { 0, 0, 0, 0 };
536
double baseline_offset = 0.0;
537
538
bool operator==(const SystemFontKey &p_b) const {
539
return (font_name == p_b.font_name) && (antialiasing == p_b.antialiasing) && (italic == p_b.italic) && (disable_embedded_bitmaps == p_b.disable_embedded_bitmaps) && (mipmaps == p_b.mipmaps) && (msdf == p_b.msdf) && (force_autohinter == p_b.force_autohinter) && (weight == p_b.weight) && (stretch == p_b.stretch) && (msdf_range == p_b.msdf_range) && (msdf_source_size == p_b.msdf_source_size) && (fixed_size == p_b.fixed_size) && (hinting == p_b.hinting) && (subpixel_positioning == p_b.subpixel_positioning) && (keep_rounding_remainders == p_b.keep_rounding_remainders) && (variation_coordinates == p_b.variation_coordinates) && (embolden == p_b.embolden) && (transform == p_b.transform) && (extra_spacing[SPACING_TOP] == p_b.extra_spacing[SPACING_TOP]) && (extra_spacing[SPACING_BOTTOM] == p_b.extra_spacing[SPACING_BOTTOM]) && (extra_spacing[SPACING_SPACE] == p_b.extra_spacing[SPACING_SPACE]) && (extra_spacing[SPACING_GLYPH] == p_b.extra_spacing[SPACING_GLYPH]) && (baseline_offset == p_b.baseline_offset);
540
}
541
542
SystemFontKey(const String &p_font_name, bool p_italic, int p_weight, int p_stretch, RID p_font, const TextServerFallback *p_fb) {
543
font_name = p_font_name;
544
italic = p_italic;
545
weight = p_weight;
546
stretch = p_stretch;
547
antialiasing = p_fb->_font_get_antialiasing(p_font);
548
disable_embedded_bitmaps = p_fb->_font_get_disable_embedded_bitmaps(p_font);
549
mipmaps = p_fb->_font_get_generate_mipmaps(p_font);
550
msdf = p_fb->_font_is_multichannel_signed_distance_field(p_font);
551
msdf_range = p_fb->_font_get_msdf_pixel_range(p_font);
552
msdf_source_size = p_fb->_font_get_msdf_size(p_font);
553
fixed_size = p_fb->_font_get_fixed_size(p_font);
554
force_autohinter = p_fb->_font_is_force_autohinter(p_font);
555
hinting = p_fb->_font_get_hinting(p_font);
556
subpixel_positioning = p_fb->_font_get_subpixel_positioning(p_font);
557
keep_rounding_remainders = p_fb->_font_get_keep_rounding_remainders(p_font);
558
variation_coordinates = p_fb->_font_get_variation_coordinates(p_font);
559
embolden = p_fb->_font_get_embolden(p_font);
560
transform = p_fb->_font_get_transform(p_font);
561
extra_spacing[SPACING_TOP] = p_fb->_font_get_spacing(p_font, SPACING_TOP);
562
extra_spacing[SPACING_BOTTOM] = p_fb->_font_get_spacing(p_font, SPACING_BOTTOM);
563
extra_spacing[SPACING_SPACE] = p_fb->_font_get_spacing(p_font, SPACING_SPACE);
564
extra_spacing[SPACING_GLYPH] = p_fb->_font_get_spacing(p_font, SPACING_GLYPH);
565
baseline_offset = p_fb->_font_get_baseline_offset(p_font);
566
}
567
};
568
569
struct SystemFontCacheRec {
570
RID rid;
571
int index = 0;
572
};
573
574
struct SystemFontCache {
575
Vector<SystemFontCacheRec> var;
576
int max_var = 0;
577
};
578
579
struct SystemFontKeyHasher {
580
_FORCE_INLINE_ static uint32_t hash(const SystemFontKey &p_a) {
581
uint32_t hash = p_a.font_name.hash();
582
hash = hash_murmur3_one_32(p_a.variation_coordinates.hash(), hash);
583
hash = hash_murmur3_one_32(p_a.weight, hash);
584
hash = hash_murmur3_one_32(p_a.stretch, hash);
585
hash = hash_murmur3_one_32(p_a.msdf_range, hash);
586
hash = hash_murmur3_one_32(p_a.msdf_source_size, hash);
587
hash = hash_murmur3_one_32(p_a.fixed_size, hash);
588
hash = hash_murmur3_one_double(p_a.embolden, hash);
589
hash = hash_murmur3_one_real(p_a.transform[0].x, hash);
590
hash = hash_murmur3_one_real(p_a.transform[0].y, hash);
591
hash = hash_murmur3_one_real(p_a.transform[1].x, hash);
592
hash = hash_murmur3_one_real(p_a.transform[1].y, hash);
593
hash = hash_murmur3_one_32(p_a.extra_spacing[SPACING_TOP], hash);
594
hash = hash_murmur3_one_32(p_a.extra_spacing[SPACING_BOTTOM], hash);
595
hash = hash_murmur3_one_32(p_a.extra_spacing[SPACING_SPACE], hash);
596
hash = hash_murmur3_one_32(p_a.extra_spacing[SPACING_GLYPH], hash);
597
hash = hash_murmur3_one_double(p_a.baseline_offset, hash);
598
return hash_fmix32(hash_murmur3_one_32(((int)p_a.mipmaps) | ((int)p_a.msdf << 1) | ((int)p_a.italic << 2) | ((int)p_a.force_autohinter << 3) | ((int)p_a.hinting << 4) | ((int)p_a.subpixel_positioning << 8) | ((int)p_a.antialiasing << 12) | ((int)p_a.disable_embedded_bitmaps << 14) | ((int)p_a.keep_rounding_remainders << 15), hash));
599
}
600
};
601
mutable HashMap<SystemFontKey, SystemFontCache, SystemFontKeyHasher> system_fonts;
602
mutable HashMap<String, PackedByteArray> system_font_data;
603
604
void _generate_runs(ShapedTextDataFallback *p_sd) const;
605
void _realign(ShapedTextDataFallback *p_sd) const;
606
_FORCE_INLINE_ RID _find_sys_font_for_text(const RID &p_fdef, const String &p_script_code, const String &p_language, const String &p_text);
607
608
Mutex ft_mutex;
609
610
protected:
611
static void _bind_methods() {}
612
613
void full_copy(ShapedTextDataFallback *p_shaped);
614
void invalidate(ShapedTextDataFallback *p_shaped);
615
616
public:
617
MODBIND1RC(bool, has_feature, Feature);
618
MODBIND0RC(String, get_name);
619
MODBIND0RC(int64_t, get_features);
620
621
MODBIND1(free_rid, const RID &);
622
MODBIND1R(bool, has, const RID &);
623
MODBIND1R(bool, load_support_data, const String &);
624
625
MODBIND0RC(String, get_support_data_filename);
626
MODBIND0RC(String, get_support_data_info);
627
MODBIND1RC(bool, save_support_data, const String &);
628
MODBIND0RC(PackedByteArray, get_support_data);
629
MODBIND1RC(bool, is_locale_using_support_data, const String &);
630
631
MODBIND1RC(bool, is_locale_right_to_left, const String &);
632
633
MODBIND1RC(int64_t, name_to_tag, const String &);
634
MODBIND1RC(String, tag_to_name, int64_t);
635
636
/* Font interface */
637
638
MODBIND0R(RID, create_font);
639
MODBIND1R(RID, create_font_linked_variation, const RID &);
640
641
MODBIND2(font_set_data, const RID &, const PackedByteArray &);
642
MODBIND3(font_set_data_ptr, const RID &, const uint8_t *, int64_t);
643
644
MODBIND2(font_set_face_index, const RID &, int64_t);
645
MODBIND1RC(int64_t, font_get_face_index, const RID &);
646
647
MODBIND1RC(int64_t, font_get_face_count, const RID &);
648
649
MODBIND2(font_set_style, const RID &, BitField<FontStyle>);
650
MODBIND1RC(BitField<FontStyle>, font_get_style, const RID &);
651
652
MODBIND2(font_set_style_name, const RID &, const String &);
653
MODBIND1RC(String, font_get_style_name, const RID &);
654
655
MODBIND2(font_set_weight, const RID &, int64_t);
656
MODBIND1RC(int64_t, font_get_weight, const RID &);
657
658
MODBIND2(font_set_stretch, const RID &, int64_t);
659
MODBIND1RC(int64_t, font_get_stretch, const RID &);
660
661
MODBIND2(font_set_name, const RID &, const String &);
662
MODBIND1RC(String, font_get_name, const RID &);
663
664
MODBIND2(font_set_antialiasing, const RID &, TextServer::FontAntialiasing);
665
MODBIND1RC(TextServer::FontAntialiasing, font_get_antialiasing, const RID &);
666
667
MODBIND2(font_set_disable_embedded_bitmaps, const RID &, bool);
668
MODBIND1RC(bool, font_get_disable_embedded_bitmaps, const RID &);
669
670
MODBIND2(font_set_generate_mipmaps, const RID &, bool);
671
MODBIND1RC(bool, font_get_generate_mipmaps, const RID &);
672
673
MODBIND2(font_set_multichannel_signed_distance_field, const RID &, bool);
674
MODBIND1RC(bool, font_is_multichannel_signed_distance_field, const RID &);
675
676
MODBIND2(font_set_msdf_pixel_range, const RID &, int64_t);
677
MODBIND1RC(int64_t, font_get_msdf_pixel_range, const RID &);
678
679
MODBIND2(font_set_msdf_size, const RID &, int64_t);
680
MODBIND1RC(int64_t, font_get_msdf_size, const RID &);
681
682
MODBIND2(font_set_fixed_size, const RID &, int64_t);
683
MODBIND1RC(int64_t, font_get_fixed_size, const RID &);
684
685
MODBIND2(font_set_fixed_size_scale_mode, const RID &, FixedSizeScaleMode);
686
MODBIND1RC(FixedSizeScaleMode, font_get_fixed_size_scale_mode, const RID &);
687
688
MODBIND2(font_set_allow_system_fallback, const RID &, bool);
689
MODBIND1RC(bool, font_is_allow_system_fallback, const RID &);
690
MODBIND0(font_clear_system_fallback_cache);
691
692
MODBIND2(font_set_force_autohinter, const RID &, bool);
693
MODBIND1RC(bool, font_is_force_autohinter, const RID &);
694
695
MODBIND2(font_set_modulate_color_glyphs, const RID &, bool);
696
MODBIND1RC(bool, font_is_modulate_color_glyphs, const RID &);
697
698
MODBIND2(font_set_subpixel_positioning, const RID &, SubpixelPositioning);
699
MODBIND1RC(SubpixelPositioning, font_get_subpixel_positioning, const RID &);
700
701
MODBIND2(font_set_keep_rounding_remainders, const RID &, bool);
702
MODBIND1RC(bool, font_get_keep_rounding_remainders, const RID &);
703
704
MODBIND2(font_set_embolden, const RID &, double);
705
MODBIND1RC(double, font_get_embolden, const RID &);
706
707
MODBIND3(font_set_spacing, const RID &, SpacingType, int64_t);
708
MODBIND2RC(int64_t, font_get_spacing, const RID &, SpacingType);
709
710
MODBIND2(font_set_baseline_offset, const RID &, double);
711
MODBIND1RC(double, font_get_baseline_offset, const RID &);
712
713
MODBIND2(font_set_transform, const RID &, const Transform2D &);
714
MODBIND1RC(Transform2D, font_get_transform, const RID &);
715
716
MODBIND2(font_set_variation_coordinates, const RID &, const Dictionary &);
717
MODBIND1RC(Dictionary, font_get_variation_coordinates, const RID &);
718
719
MODBIND2(font_set_oversampling, const RID &, double);
720
MODBIND1RC(double, font_get_oversampling, const RID &);
721
722
MODBIND2(font_set_hinting, const RID &, TextServer::Hinting);
723
MODBIND1RC(TextServer::Hinting, font_get_hinting, const RID &);
724
725
MODBIND1RC(TypedArray<Vector2i>, font_get_size_cache_list, const RID &);
726
MODBIND1(font_clear_size_cache, const RID &);
727
MODBIND2(font_remove_size_cache, const RID &, const Vector2i &);
728
MODBIND1RC(TypedArray<Dictionary>, font_get_size_cache_info, const RID &);
729
730
MODBIND3(font_set_ascent, const RID &, int64_t, double);
731
MODBIND2RC(double, font_get_ascent, const RID &, int64_t);
732
733
MODBIND3(font_set_descent, const RID &, int64_t, double);
734
MODBIND2RC(double, font_get_descent, const RID &, int64_t);
735
736
MODBIND3(font_set_underline_position, const RID &, int64_t, double);
737
MODBIND2RC(double, font_get_underline_position, const RID &, int64_t);
738
739
MODBIND3(font_set_underline_thickness, const RID &, int64_t, double);
740
MODBIND2RC(double, font_get_underline_thickness, const RID &, int64_t);
741
742
MODBIND3(font_set_scale, const RID &, int64_t, double);
743
MODBIND2RC(double, font_get_scale, const RID &, int64_t);
744
745
MODBIND2RC(int64_t, font_get_texture_count, const RID &, const Vector2i &);
746
MODBIND2(font_clear_textures, const RID &, const Vector2i &);
747
MODBIND3(font_remove_texture, const RID &, const Vector2i &, int64_t);
748
749
MODBIND4(font_set_texture_image, const RID &, const Vector2i &, int64_t, const Ref<Image> &);
750
MODBIND3RC(Ref<Image>, font_get_texture_image, const RID &, const Vector2i &, int64_t);
751
752
MODBIND4(font_set_texture_offsets, const RID &, const Vector2i &, int64_t, const PackedInt32Array &);
753
MODBIND3RC(PackedInt32Array, font_get_texture_offsets, const RID &, const Vector2i &, int64_t);
754
755
MODBIND2RC(PackedInt32Array, font_get_glyph_list, const RID &, const Vector2i &);
756
MODBIND2(font_clear_glyphs, const RID &, const Vector2i &);
757
MODBIND3(font_remove_glyph, const RID &, const Vector2i &, int64_t);
758
759
MODBIND3RC(Vector2, font_get_glyph_advance, const RID &, int64_t, int64_t);
760
MODBIND4(font_set_glyph_advance, const RID &, int64_t, int64_t, const Vector2 &);
761
762
MODBIND3RC(Vector2, font_get_glyph_offset, const RID &, const Vector2i &, int64_t);
763
MODBIND4(font_set_glyph_offset, const RID &, const Vector2i &, int64_t, const Vector2 &);
764
765
MODBIND3RC(Vector2, font_get_glyph_size, const RID &, const Vector2i &, int64_t);
766
MODBIND4(font_set_glyph_size, const RID &, const Vector2i &, int64_t, const Vector2 &);
767
768
MODBIND3RC(Rect2, font_get_glyph_uv_rect, const RID &, const Vector2i &, int64_t);
769
MODBIND4(font_set_glyph_uv_rect, const RID &, const Vector2i &, int64_t, const Rect2 &);
770
771
MODBIND3RC(int64_t, font_get_glyph_texture_idx, const RID &, const Vector2i &, int64_t);
772
MODBIND4(font_set_glyph_texture_idx, const RID &, const Vector2i &, int64_t, int64_t);
773
774
MODBIND3RC(RID, font_get_glyph_texture_rid, const RID &, const Vector2i &, int64_t);
775
MODBIND3RC(Size2, font_get_glyph_texture_size, const RID &, const Vector2i &, int64_t);
776
777
MODBIND3RC(Dictionary, font_get_glyph_contours, const RID &, int64_t, int64_t);
778
779
MODBIND2RC(TypedArray<Vector2i>, font_get_kerning_list, const RID &, int64_t);
780
MODBIND2(font_clear_kerning_map, const RID &, int64_t);
781
MODBIND3(font_remove_kerning, const RID &, int64_t, const Vector2i &);
782
783
MODBIND4(font_set_kerning, const RID &, int64_t, const Vector2i &, const Vector2 &);
784
MODBIND3RC(Vector2, font_get_kerning, const RID &, int64_t, const Vector2i &);
785
786
MODBIND4RC(int64_t, font_get_glyph_index, const RID &, int64_t, int64_t, int64_t);
787
MODBIND3RC(int64_t, font_get_char_from_glyph_index, const RID &, int64_t, int64_t);
788
789
MODBIND2RC(bool, font_has_char, const RID &, int64_t);
790
MODBIND1RC(String, font_get_supported_chars, const RID &);
791
MODBIND1RC(PackedInt32Array, font_get_supported_glyphs, const RID &);
792
793
MODBIND4(font_render_range, const RID &, const Vector2i &, int64_t, int64_t);
794
MODBIND3(font_render_glyph, const RID &, const Vector2i &, int64_t);
795
796
MODBIND7C(font_draw_glyph, const RID &, const RID &, int64_t, const Vector2 &, int64_t, const Color &, float);
797
MODBIND8C(font_draw_glyph_outline, const RID &, const RID &, int64_t, int64_t, const Vector2 &, int64_t, const Color &, float);
798
799
MODBIND2RC(bool, font_is_language_supported, const RID &, const String &);
800
MODBIND3(font_set_language_support_override, const RID &, const String &, bool);
801
MODBIND2R(bool, font_get_language_support_override, const RID &, const String &);
802
MODBIND2(font_remove_language_support_override, const RID &, const String &);
803
MODBIND1R(PackedStringArray, font_get_language_support_overrides, const RID &);
804
805
MODBIND2RC(bool, font_is_script_supported, const RID &, const String &);
806
MODBIND3(font_set_script_support_override, const RID &, const String &, bool);
807
MODBIND2R(bool, font_get_script_support_override, const RID &, const String &);
808
MODBIND2(font_remove_script_support_override, const RID &, const String &);
809
MODBIND1R(PackedStringArray, font_get_script_support_overrides, const RID &);
810
811
MODBIND2(font_set_opentype_feature_overrides, const RID &, const Dictionary &);
812
MODBIND1RC(Dictionary, font_get_opentype_feature_overrides, const RID &);
813
814
MODBIND1RC(Dictionary, font_supported_feature_list, const RID &);
815
MODBIND1RC(Dictionary, font_supported_variation_list, const RID &);
816
817
MODBIND1(reference_oversampling_level, double);
818
MODBIND1(unreference_oversampling_level, double);
819
820
/* Shaped text buffer interface */
821
822
MODBIND2R(RID, create_shaped_text, Direction, Orientation);
823
824
MODBIND1(shaped_text_clear, const RID &);
825
MODBIND1R(RID, shaped_text_duplicate, const RID &);
826
827
MODBIND2(shaped_text_set_direction, const RID &, Direction);
828
MODBIND1RC(Direction, shaped_text_get_direction, const RID &);
829
MODBIND1RC(Direction, shaped_text_get_inferred_direction, const RID &);
830
831
MODBIND2(shaped_text_set_bidi_override, const RID &, const Array &);
832
833
MODBIND2(shaped_text_set_custom_punctuation, const RID &, const String &);
834
MODBIND1RC(String, shaped_text_get_custom_punctuation, const RID &);
835
836
MODBIND2(shaped_text_set_custom_ellipsis, const RID &, int64_t);
837
MODBIND1RC(int64_t, shaped_text_get_custom_ellipsis, const RID &);
838
839
MODBIND2(shaped_text_set_orientation, const RID &, Orientation);
840
MODBIND1RC(Orientation, shaped_text_get_orientation, const RID &);
841
842
MODBIND2(shaped_text_set_preserve_invalid, const RID &, bool);
843
MODBIND1RC(bool, shaped_text_get_preserve_invalid, const RID &);
844
845
MODBIND2(shaped_text_set_preserve_control, const RID &, bool);
846
MODBIND1RC(bool, shaped_text_get_preserve_control, const RID &);
847
848
MODBIND3(shaped_text_set_spacing, const RID &, SpacingType, int64_t);
849
MODBIND2RC(int64_t, shaped_text_get_spacing, const RID &, SpacingType);
850
851
MODBIND7R(bool, shaped_text_add_string, const RID &, const String &, const TypedArray<RID> &, int64_t, const Dictionary &, const String &, const Variant &);
852
MODBIND6R(bool, shaped_text_add_object, const RID &, const Variant &, const Size2 &, InlineAlignment, int64_t, double);
853
MODBIND5R(bool, shaped_text_resize_object, const RID &, const Variant &, const Size2 &, InlineAlignment, double);
854
MODBIND2RC(bool, shaped_text_has_object, const RID &, const Variant &);
855
MODBIND1RC(String, shaped_get_text, const RID &);
856
857
MODBIND1RC(int64_t, shaped_get_span_count, const RID &);
858
MODBIND2RC(Variant, shaped_get_span_meta, const RID &, int64_t);
859
MODBIND2RC(Variant, shaped_get_span_embedded_object, const RID &, int64_t);
860
MODBIND2RC(String, shaped_get_span_text, const RID &, int64_t);
861
MODBIND2RC(Variant, shaped_get_span_object, const RID &, int64_t);
862
MODBIND5(shaped_set_span_update_font, const RID &, int64_t, const TypedArray<RID> &, int64_t, const Dictionary &);
863
864
MODBIND1RC(int64_t, shaped_get_run_count, const RID &);
865
MODBIND2RC(String, shaped_get_run_text, const RID &, int64_t);
866
MODBIND2RC(Vector2i, shaped_get_run_range, const RID &, int64_t);
867
MODBIND2RC(RID, shaped_get_run_font_rid, const RID &, int64_t);
868
MODBIND2RC(int, shaped_get_run_font_size, const RID &, int64_t);
869
MODBIND2RC(String, shaped_get_run_language, const RID &, int64_t);
870
MODBIND2RC(Direction, shaped_get_run_direction, const RID &, int64_t);
871
MODBIND2RC(Variant, shaped_get_run_object, const RID &, int64_t);
872
873
MODBIND3RC(RID, shaped_text_substr, const RID &, int64_t, int64_t);
874
MODBIND1RC(RID, shaped_text_get_parent, const RID &);
875
876
MODBIND3R(double, shaped_text_fit_to_width, const RID &, double, BitField<TextServer::JustificationFlag>);
877
MODBIND2R(double, shaped_text_tab_align, const RID &, const PackedFloat32Array &);
878
879
MODBIND1R(bool, shaped_text_shape, const RID &);
880
MODBIND1R(bool, shaped_text_update_breaks, const RID &);
881
MODBIND1R(bool, shaped_text_update_justification_ops, const RID &);
882
883
MODBIND1RC(int64_t, shaped_text_get_trim_pos, const RID &);
884
MODBIND1RC(int64_t, shaped_text_get_ellipsis_pos, const RID &);
885
MODBIND1RC(const Glyph *, shaped_text_get_ellipsis_glyphs, const RID &);
886
MODBIND1RC(int64_t, shaped_text_get_ellipsis_glyph_count, const RID &);
887
888
MODBIND3(shaped_text_overrun_trim_to_width, const RID &, double, BitField<TextServer::TextOverrunFlag>);
889
890
MODBIND1RC(bool, shaped_text_is_ready, const RID &);
891
892
MODBIND1RC(const Glyph *, shaped_text_get_glyphs, const RID &);
893
MODBIND1R(const Glyph *, shaped_text_sort_logical, const RID &);
894
MODBIND1RC(int64_t, shaped_text_get_glyph_count, const RID &);
895
896
MODBIND1RC(Vector2i, shaped_text_get_range, const RID &);
897
898
MODBIND1RC(Array, shaped_text_get_objects, const RID &);
899
MODBIND2RC(Rect2, shaped_text_get_object_rect, const RID &, const Variant &);
900
MODBIND2RC(Vector2i, shaped_text_get_object_range, const RID &, const Variant &);
901
MODBIND2RC(int64_t, shaped_text_get_object_glyph, const RID &, const Variant &);
902
903
MODBIND1RC(Size2, shaped_text_get_size, const RID &);
904
MODBIND1RC(double, shaped_text_get_ascent, const RID &);
905
MODBIND1RC(double, shaped_text_get_descent, const RID &);
906
MODBIND1RC(double, shaped_text_get_width, const RID &);
907
MODBIND1RC(double, shaped_text_get_underline_position, const RID &);
908
MODBIND1RC(double, shaped_text_get_underline_thickness, const RID &);
909
910
MODBIND1RC(PackedInt32Array, shaped_text_get_character_breaks, const RID &);
911
912
MODBIND3RC(PackedInt32Array, string_get_word_breaks, const String &, const String &, int64_t);
913
914
MODBIND2RC(String, string_to_upper, const String &, const String &);
915
MODBIND2RC(String, string_to_lower, const String &, const String &);
916
MODBIND2RC(String, string_to_title, const String &, const String &);
917
918
MODBIND0(cleanup);
919
920
TextServerFallback();
921
~TextServerFallback();
922
};
923
924