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