Path: blob/master/modules/text_server_adv/text_server_adv.h
10277 views
/**************************************************************************/1/* text_server_adv.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132/*************************************************************************/33/* ICU/HarfBuzz/Graphite backed Text Server implementation with BiDi, */34/* shaping and advanced font features support. */35/*************************************************************************/3637#include "script_iterator.h"3839#ifdef GDEXTENSION40// Headers for building as GDExtension plug-in.4142#include <godot_cpp/godot.hpp>4344#include <godot_cpp/core/class_db.hpp>45#include <godot_cpp/core/ext_wrappers.gen.inc>46#include <godot_cpp/core/mutex_lock.hpp>4748#include <godot_cpp/variant/array.hpp>49#include <godot_cpp/variant/dictionary.hpp>50#include <godot_cpp/variant/packed_int32_array.hpp>51#include <godot_cpp/variant/packed_string_array.hpp>52#include <godot_cpp/variant/packed_vector2_array.hpp>53#include <godot_cpp/variant/rect2.hpp>54#include <godot_cpp/variant/rid.hpp>55#include <godot_cpp/variant/string.hpp>56#include <godot_cpp/variant/typed_array.hpp>57#include <godot_cpp/variant/vector2.hpp>58#include <godot_cpp/variant/vector2i.hpp>5960#include <godot_cpp/classes/text_server.hpp>61#include <godot_cpp/classes/text_server_extension.hpp>62#include <godot_cpp/classes/text_server_manager.hpp>6364#include <godot_cpp/classes/caret_info.hpp>65#include <godot_cpp/classes/global_constants_binds.hpp>66#include <godot_cpp/classes/glyph.hpp>67#include <godot_cpp/classes/image.hpp>68#include <godot_cpp/classes/image_texture.hpp>69#include <godot_cpp/classes/ref.hpp>70#include <godot_cpp/classes/worker_thread_pool.hpp>7172#include <godot_cpp/templates/hash_map.hpp>73#include <godot_cpp/templates/hash_set.hpp>74#include <godot_cpp/templates/rid_owner.hpp>75#include <godot_cpp/templates/safe_refcount.hpp>76#include <godot_cpp/templates/vector.hpp>7778using namespace godot;7980#elif defined(GODOT_MODULE)81// Headers for building as built-in module.8283#include "core/extension/ext_wrappers.gen.inc"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"8990#include "modules/modules_enabled.gen.h" // For freetype, msdfgen, svg.9192#endif9394// Thirdparty headers.9596#include <unicode/ubidi.h>97#include <unicode/ubrk.h>98#include <unicode/uchar.h>99#include <unicode/uclean.h>100#include <unicode/udata.h>101#include <unicode/uiter.h>102#include <unicode/uloc.h>103#include <unicode/unorm2.h>104#include <unicode/uscript.h>105#include <unicode/uspoof.h>106#include <unicode/ustring.h>107#include <unicode/utypes.h>108109#ifdef MODULE_FREETYPE_ENABLED110#include <ft2build.h>111#include FT_FREETYPE_H112#include FT_TRUETYPE_TABLES_H113#include FT_STROKER_H114#include FT_ADVANCES_H115#include FT_MULTIPLE_MASTERS_H116#include FT_BBOX_H117#include FT_MODULE_H118#include FT_CONFIG_OPTIONS_H119#if !defined(FT_CONFIG_OPTION_USE_BROTLI) && !defined(_MSC_VER)120#warning FreeType is configured without Brotli support, built-in fonts will not be available.121#endif122#include <hb-ft.h>123#include <hb-ot.h>124#endif125126#include <hb-icu.h>127#include <hb.h>128129/*************************************************************************/130131class TextServerAdvanced : public TextServerExtension {132GDCLASS(TextServerAdvanced, TextServerExtension);133_THREAD_SAFE_CLASS_134135struct NumSystemData {136HashSet<StringName> lang;137String digits;138String percent_sign;139String exp_l;140String exp_u;141};142143Vector<NumSystemData> num_systems;144145struct FeatureInfo {146StringName name;147Variant::Type vtype = Variant::INT;148bool hidden = false;149};150151HashMap<StringName, int32_t> feature_sets;152HashMap<int32_t, FeatureInfo> feature_sets_inv;153154enum LineBreakStrictness {155LB_AUTO,156LB_LOOSE,157LB_NORMAL,158LB_STRICT,159};160161SafeNumeric<TextServer::FontLCDSubpixelLayout> lcd_subpixel_layout{ TextServer::FontLCDSubpixelLayout::FONT_LCD_SUBPIXEL_LAYOUT_NONE };162LineBreakStrictness lb_strictness = LB_AUTO;163void _update_settings();164165void _insert_num_systems_lang();166void _insert_feature_sets();167_FORCE_INLINE_ void _insert_feature(const StringName &p_name, int32_t p_tag, Variant::Type p_vtype = Variant::INT, bool p_hidden = false);168169// ICU support data.170171static bool icu_data_loaded;172static PackedByteArray icu_data;173mutable USet *allowed = nullptr;174mutable USpoofChecker *sc_spoof = nullptr;175mutable USpoofChecker *sc_conf = nullptr;176177mutable HashMap<String, UBreakIterator *> line_break_iterators_per_language;178179UBreakIterator *_create_line_break_iterator_for_locale(const String &p_language, UErrorCode *r_err) const;180181// Font cache data.182183#ifdef MODULE_FREETYPE_ENABLED184mutable FT_Library ft_library = nullptr;185#endif186187const int rect_range = 1;188189struct FontTexturePosition {190int32_t index = -1;191int32_t x = 0;192int32_t y = 0;193194FontTexturePosition() {}195FontTexturePosition(int32_t p_id, int32_t p_x, int32_t p_y) :196index(p_id), x(p_x), y(p_y) {}197};198199struct Shelf {200int32_t x = 0;201int32_t y = 0;202int32_t w = 0;203int32_t h = 0;204205FontTexturePosition alloc_shelf(int32_t p_id, int32_t p_w, int32_t p_h) {206if (p_w > w || p_h > h) {207return FontTexturePosition(-1, 0, 0);208}209int32_t xx = x;210x += p_w;211w -= p_w;212return FontTexturePosition(p_id, xx, y);213}214215Shelf() {}216Shelf(int32_t p_x, int32_t p_y, int32_t p_w, int32_t p_h) :217x(p_x), y(p_y), w(p_w), h(p_h) {}218};219220struct ShelfPackTexture {221int32_t texture_w = 1024;222int32_t texture_h = 1024;223224Ref<Image> image;225Ref<ImageTexture> texture;226bool dirty = true;227228List<Shelf> shelves;229230FontTexturePosition pack_rect(int32_t p_id, int32_t p_h, int32_t p_w) {231int32_t y = 0;232int32_t waste = 0;233Shelf *best_shelf = nullptr;234int32_t best_waste = std::numeric_limits<std::int32_t>::max();235236for (Shelf &E : shelves) {237y += E.h;238if (p_w > E.w) {239continue;240}241if (p_h == E.h) {242return E.alloc_shelf(p_id, p_w, p_h);243}244if (p_h > E.h) {245continue;246}247if (p_h < E.h) {248waste = (E.h - p_h) * p_w;249if (waste < best_waste) {250best_waste = waste;251best_shelf = &E;252}253}254}255if (best_shelf) {256return best_shelf->alloc_shelf(p_id, p_w, p_h);257}258if (p_h <= (texture_h - y) && p_w <= texture_w) {259List<Shelf>::Element *E = shelves.push_back(Shelf(0, y, texture_w, p_h));260return E->get().alloc_shelf(p_id, p_w, p_h);261}262return FontTexturePosition(-1, 0, 0);263}264265ShelfPackTexture() {}266ShelfPackTexture(int32_t p_w, int32_t p_h) :267texture_w(p_w), texture_h(p_h) {}268};269270struct FontGlyph {271bool found = false;272int texture_idx = -1;273Rect2 rect;274Rect2 uv_rect;275Vector2 advance;276bool from_svg = false;277};278279struct FontAdvanced;280struct FontForSizeAdvanced {281double ascent = 0.0;282double descent = 0.0;283double underline_position = 0.0;284double underline_thickness = 0.0;285double scale = 1.0;286287FontAdvanced *owner = nullptr;288uint32_t viewport_oversampling = 0;289290Vector2i size;291292Vector<ShelfPackTexture> textures;293HashMap<int64_t, int64_t> inv_glyph_map;294HashMap<int32_t, FontGlyph> glyph_map;295HashMap<Vector2i, Vector2> kerning_map;296hb_font_t *hb_handle = nullptr;297298#ifdef MODULE_FREETYPE_ENABLED299FT_Face face = nullptr;300FT_StreamRec stream;301#endif302303~FontForSizeAdvanced() {304if (hb_handle != nullptr) {305hb_font_destroy(hb_handle);306}307#ifdef MODULE_FREETYPE_ENABLED308if (face != nullptr) {309FT_Done_Face(face);310}311#endif312}313};314315struct OversamplingLevel {316HashSet<FontForSizeAdvanced *> fonts;317int32_t refcount = 1;318};319320mutable HashMap<uint32_t, OversamplingLevel> oversampling_levels;321322struct FontAdvancedLinkedVariation {323RID base_font;324int extra_spacing[4] = { 0, 0, 0, 0 };325double baseline_offset = 0.0;326};327328struct FontAdvanced {329Mutex mutex;330331TextServer::FontAntialiasing antialiasing = TextServer::FONT_ANTIALIASING_GRAY;332bool disable_embedded_bitmaps = true;333bool mipmaps = false;334bool msdf = false;335int msdf_range = 14;336FixedSizeScaleMode fixed_size_scale_mode = FIXED_SIZE_SCALE_DISABLE;337int msdf_source_size = 48;338int fixed_size = 0;339bool allow_system_fallback = true;340bool force_autohinter = false;341bool modulate_color_glyphs = false;342TextServer::Hinting hinting = TextServer::HINTING_LIGHT;343TextServer::SubpixelPositioning subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_AUTO;344bool keep_rounding_remainders = true;345Dictionary variation_coordinates;346double oversampling_override = 0.0;347double embolden = 0.0;348Transform2D transform;349350BitField<TextServer::FontStyle> style_flags = 0;351String font_name;352String style_name;353int weight = 400;354int stretch = 100;355int extra_spacing[4] = { 0, 0, 0, 0 };356double baseline_offset = 0.0;357358HashMap<Vector2i, FontForSizeAdvanced *> cache;359360bool face_init = false;361HashSet<uint32_t> supported_scripts;362Dictionary supported_features;363Dictionary supported_varaitions;364Dictionary feature_overrides;365366// Language/script support override.367HashMap<String, bool> language_support_overrides;368HashMap<String, bool> script_support_overrides;369370PackedByteArray data;371const uint8_t *data_ptr;372size_t data_size;373int face_index = 0;374375~FontAdvanced() {376for (const KeyValue<Vector2i, FontForSizeAdvanced *> &E : cache) {377memdelete(E.value);378}379cache.clear();380}381};382383_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;384#ifdef MODULE_MSDFGEN_ENABLED385_FORCE_INLINE_ FontGlyph rasterize_msdf(FontAdvanced *p_font_data, FontForSizeAdvanced *p_data, int p_pixel_range, int p_rect_margin, FT_Outline *p_outline, const Vector2 &p_advance) const;386#endif387#ifdef MODULE_FREETYPE_ENABLED388_FORCE_INLINE_ FontGlyph rasterize_bitmap(FontForSizeAdvanced *p_data, int p_rect_margin, FT_Bitmap p_bitmap, int p_yofs, int p_xofs, const Vector2 &p_advance, bool p_bgra) const;389#endif390bool _ensure_glyph(FontAdvanced *p_font_data, const Vector2i &p_size, int32_t p_glyph, FontGlyph &r_glyph, uint32_t p_oversampling = 0) const;391bool _ensure_cache_for_size(FontAdvanced *p_font_data, const Vector2i &p_size, FontForSizeAdvanced *&r_cache_for_size, bool p_silent = false, uint32_t p_oversampling = 0) const;392_FORCE_INLINE_ bool _font_validate(const RID &p_font_rid) const;393_FORCE_INLINE_ void _font_clear_cache(FontAdvanced *p_font_data);394static void _generateMTSDF_threaded(void *p_td, uint32_t p_y);395396_FORCE_INLINE_ Vector2i _get_size(const FontAdvanced *p_font_data, int p_size) const {397if (p_font_data->msdf) {398return Vector2i(p_font_data->msdf_source_size * 64, 0);399} else if (p_font_data->fixed_size > 0) {400return Vector2i(p_font_data->fixed_size * 64, 0);401} else {402return Vector2i(p_size * 64, 0);403}404}405406_FORCE_INLINE_ Vector2i _get_size_outline(const FontAdvanced *p_font_data, const Vector2i &p_size) const {407if (p_font_data->msdf) {408return Vector2i(p_font_data->msdf_source_size * 64, 0);409} else if (p_font_data->fixed_size > 0) {410return Vector2i(p_font_data->fixed_size * 64, MIN(p_size.y, 1));411} else {412return Vector2i(p_size.x * 64, p_size.y);413}414}415416_FORCE_INLINE_ double _get_extra_advance(RID p_font_rid, int p_font_size) const;417_FORCE_INLINE_ Variant::Type _get_tag_type(int64_t p_tag) const;418_FORCE_INLINE_ bool _get_tag_hidden(int64_t p_tag) const;419_FORCE_INLINE_ int _font_get_weight_by_name(const String &p_sty_name) const {420String sty_name = p_sty_name.remove_chars(" -");421if (sty_name.contains("thin") || sty_name.contains("hairline")) {422return 100;423} else if (sty_name.contains("extralight") || sty_name.contains("ultralight")) {424return 200;425} else if (sty_name.contains("light")) {426return 300;427} else if (sty_name.contains("semilight")) {428return 350;429} else if (sty_name.contains("regular")) {430return 400;431} else if (sty_name.contains("medium")) {432return 500;433} else if (sty_name.contains("semibold") || sty_name.contains("demibold")) {434return 600;435} else if (sty_name.contains("bold")) {436return 700;437} else if (sty_name.contains("extrabold") || sty_name.contains("ultrabold")) {438return 800;439} else if (sty_name.contains("black") || sty_name.contains("heavy")) {440return 900;441} else if (sty_name.contains("extrablack") || sty_name.contains("ultrablack")) {442return 950;443}444return 400;445}446_FORCE_INLINE_ int _font_get_stretch_by_name(const String &p_sty_name) const {447String sty_name = p_sty_name.remove_chars(" -");448if (sty_name.contains("ultracondensed")) {449return 50;450} else if (sty_name.contains("extracondensed")) {451return 63;452} else if (sty_name.contains("condensed")) {453return 75;454} else if (sty_name.contains("semicondensed")) {455return 87;456} else if (sty_name.contains("semiexpanded")) {457return 113;458} else if (sty_name.contains("expanded")) {459return 125;460} else if (sty_name.contains("extraexpanded")) {461return 150;462} else if (sty_name.contains("ultraexpanded")) {463return 200;464}465return 100;466}467_FORCE_INLINE_ bool _is_ital_style(const String &p_sty_name) const {468return p_sty_name.contains("italic") || p_sty_name.contains("oblique");469}470471// Shaped text cache data.472struct TrimData {473int trim_pos = -1;474int ellipsis_pos = -1;475Vector<Glyph> ellipsis_glyph_buf;476};477478struct TextRun {479Vector2i range;480RID font_rid;481int font_size = 0;482bool rtl = false;483int64_t span_index = -1;484};485486struct ShapedTextDataAdvanced {487Mutex mutex;488489/* Source data */490RID parent; // Substring parent ShapedTextData.491492int start = 0; // Substring start offset in the parent string.493int end = 0; // Substring end offset in the parent string.494495String text;496String custom_punct;497TextServer::Direction direction = DIRECTION_LTR; // Desired text direction.498TextServer::Orientation orientation = ORIENTATION_HORIZONTAL;499500struct Span {501int start = -1;502int end = -1;503504Array fonts;505int font_size = 0;506507Variant embedded_key;508509String language;510Dictionary features;511Variant meta;512};513Vector<Span> spans;514int first_span = 0; // First span in the parent ShapedTextData.515int last_span = 0;516517Vector<TextRun> runs;518bool runs_dirty = true;519520struct EmbeddedObject {521int start = -1;522int end = -1;523InlineAlignment inline_align = INLINE_ALIGNMENT_CENTER;524Rect2 rect;525double baseline = 0;526};527HashMap<Variant, EmbeddedObject, VariantHasher, VariantComparator> objects;528529/* Shaped data */530TextServer::Direction para_direction = DIRECTION_LTR; // Detected text direction.531int base_para_direction = UBIDI_DEFAULT_LTR;532SafeFlag valid{ false }; // String is shaped.533bool line_breaks_valid = false; // Line and word break flags are populated (and virtual zero width spaces inserted).534bool justification_ops_valid = false; // Virtual elongation glyphs are added to the string.535bool sort_valid = false;536bool text_trimmed = false;537538bool preserve_invalid = true; // Draw hex code box instead of missing characters.539bool preserve_control = false; // Draw control characters.540541double ascent = 0.0; // Ascent for horizontal layout, 1/2 of width for vertical.542double descent = 0.0; // Descent for horizontal layout, 1/2 of width for vertical.543double width = 0.0; // Width for horizontal layout, height for vertical.544double width_trimmed = 0.0;545int extra_spacing[4] = { 0, 0, 0, 0 };546547double upos = 0.0;548double uthk = 0.0;549550char32_t el_char = 0x2026;551TrimData overrun_trim_data;552bool fit_width_minimum_reached = false;553554LocalVector<Glyph> glyphs;555LocalVector<Glyph> glyphs_logical;556557/* Intermediate data */558Char16String utf16;559Vector<UBiDi *> bidi_iter;560Vector<Vector3i> bidi_override;561ScriptIterator *script_iter = nullptr;562hb_buffer_t *hb_buffer = nullptr;563564HashMap<int, bool> jstops;565HashMap<int, bool> breaks;566PackedInt32Array chars;567int break_inserts = 0;568bool break_ops_valid = false;569bool js_ops_valid = false;570bool chars_valid = false;571572~ShapedTextDataAdvanced() {573for (int i = 0; i < bidi_iter.size(); i++) {574if (bidi_iter[i]) {575ubidi_close(bidi_iter[i]);576}577}578if (script_iter) {579memdelete(script_iter);580}581if (hb_buffer) {582hb_buffer_destroy(hb_buffer);583}584}585};586587// Common data.588589mutable RID_PtrOwner<FontAdvancedLinkedVariation> font_var_owner;590mutable RID_PtrOwner<FontAdvanced> font_owner;591mutable RID_PtrOwner<ShapedTextDataAdvanced> shaped_owner;592593_FORCE_INLINE_ FontAdvanced *_get_font_data(const RID &p_font_rid) const {594RID rid = p_font_rid;595FontAdvancedLinkedVariation *fdv = font_var_owner.get_or_null(rid);596if (unlikely(fdv)) {597rid = fdv->base_font;598}599return font_owner.get_or_null(rid);600}601602struct SystemFontKey {603String font_name;604TextServer::FontAntialiasing antialiasing = TextServer::FONT_ANTIALIASING_GRAY;605bool disable_embedded_bitmaps = true;606bool italic = false;607bool mipmaps = false;608bool msdf = false;609bool force_autohinter = false;610int weight = 400;611int stretch = 100;612int msdf_range = 14;613int msdf_source_size = 48;614int fixed_size = 0;615TextServer::Hinting hinting = TextServer::HINTING_LIGHT;616TextServer::SubpixelPositioning subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_AUTO;617bool keep_rounding_remainders = true;618Dictionary variation_coordinates;619double embolden = 0.0;620Transform2D transform;621int extra_spacing[4] = { 0, 0, 0, 0 };622double baseline_offset = 0.0;623624bool operator==(const SystemFontKey &p_b) const {625return (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);626}627628SystemFontKey(const String &p_font_name, bool p_italic, int p_weight, int p_stretch, RID p_font, const TextServerAdvanced *p_fb) {629font_name = p_font_name;630italic = p_italic;631weight = p_weight;632stretch = p_stretch;633antialiasing = p_fb->_font_get_antialiasing(p_font);634disable_embedded_bitmaps = p_fb->_font_get_disable_embedded_bitmaps(p_font);635mipmaps = p_fb->_font_get_generate_mipmaps(p_font);636msdf = p_fb->_font_is_multichannel_signed_distance_field(p_font);637msdf_range = p_fb->_font_get_msdf_pixel_range(p_font);638msdf_source_size = p_fb->_font_get_msdf_size(p_font);639fixed_size = p_fb->_font_get_fixed_size(p_font);640force_autohinter = p_fb->_font_is_force_autohinter(p_font);641hinting = p_fb->_font_get_hinting(p_font);642subpixel_positioning = p_fb->_font_get_subpixel_positioning(p_font);643keep_rounding_remainders = p_fb->_font_get_keep_rounding_remainders(p_font);644variation_coordinates = p_fb->_font_get_variation_coordinates(p_font);645embolden = p_fb->_font_get_embolden(p_font);646transform = p_fb->_font_get_transform(p_font);647extra_spacing[SPACING_TOP] = p_fb->_font_get_spacing(p_font, SPACING_TOP);648extra_spacing[SPACING_BOTTOM] = p_fb->_font_get_spacing(p_font, SPACING_BOTTOM);649extra_spacing[SPACING_SPACE] = p_fb->_font_get_spacing(p_font, SPACING_SPACE);650extra_spacing[SPACING_GLYPH] = p_fb->_font_get_spacing(p_font, SPACING_GLYPH);651baseline_offset = p_fb->_font_get_baseline_offset(p_font);652}653};654655struct SystemFontCacheRec {656RID rid;657int index = 0;658};659660struct SystemFontCache {661Vector<SystemFontCacheRec> var;662int max_var = 0;663};664665struct SystemFontKeyHasher {666_FORCE_INLINE_ static uint32_t hash(const SystemFontKey &p_a) {667uint32_t hash = p_a.font_name.hash();668hash = hash_murmur3_one_32(p_a.variation_coordinates.hash(), hash);669hash = hash_murmur3_one_32(p_a.weight, hash);670hash = hash_murmur3_one_32(p_a.stretch, hash);671hash = hash_murmur3_one_32(p_a.msdf_range, hash);672hash = hash_murmur3_one_32(p_a.msdf_source_size, hash);673hash = hash_murmur3_one_32(p_a.fixed_size, hash);674hash = hash_murmur3_one_double(p_a.embolden, hash);675hash = hash_murmur3_one_real(p_a.transform[0].x, hash);676hash = hash_murmur3_one_real(p_a.transform[0].y, hash);677hash = hash_murmur3_one_real(p_a.transform[1].x, hash);678hash = hash_murmur3_one_real(p_a.transform[1].y, hash);679hash = hash_murmur3_one_32(p_a.extra_spacing[SPACING_TOP], hash);680hash = hash_murmur3_one_32(p_a.extra_spacing[SPACING_BOTTOM], hash);681hash = hash_murmur3_one_32(p_a.extra_spacing[SPACING_SPACE], hash);682hash = hash_murmur3_one_32(p_a.extra_spacing[SPACING_GLYPH], hash);683hash = hash_murmur3_one_double(p_a.baseline_offset, hash);684return 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));685}686};687mutable HashMap<SystemFontKey, SystemFontCache, SystemFontKeyHasher> system_fonts;688mutable HashMap<String, PackedByteArray> system_font_data;689690void _update_chars(ShapedTextDataAdvanced *p_sd) const;691void _generate_runs(ShapedTextDataAdvanced *p_sd) const;692void _realign(ShapedTextDataAdvanced *p_sd) const;693int64_t _convert_pos(const String &p_utf32, const Char16String &p_utf16, int64_t p_pos) const;694int64_t _convert_pos(const ShapedTextDataAdvanced *p_sd, int64_t p_pos) const;695int64_t _convert_pos_inv(const ShapedTextDataAdvanced *p_sd, int64_t p_pos) const;696bool _shape_substr(ShapedTextDataAdvanced *p_new_sd, const ShapedTextDataAdvanced *p_sd, int64_t p_start, int64_t p_length) const;697void _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, RID p_prev_font);698Glyph _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);699_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);700701_FORCE_INLINE_ void _add_features(const Dictionary &p_source, Vector<hb_feature_t> &r_ftrs);702703Mutex ft_mutex;704705// HarfBuzz bitmap font interface.706707static hb_font_funcs_t *funcs;708709struct bmp_font_t {710TextServerAdvanced::FontForSizeAdvanced *face = nullptr;711bool unref = false; /* Whether to destroy bm_face when done. */712};713714static bmp_font_t *_bmp_font_create(TextServerAdvanced::FontForSizeAdvanced *p_face, bool p_unref);715static void _bmp_font_destroy(void *p_data);716static 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);717static 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);718static 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);719static 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);720static 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);721static 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);722static 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);723static void _bmp_create_font_funcs();724static void _bmp_free_font_funcs();725static void _bmp_font_set_funcs(hb_font_t *p_font, TextServerAdvanced::FontForSizeAdvanced *p_face, bool p_unref);726static hb_font_t *_bmp_font_create(TextServerAdvanced::FontForSizeAdvanced *p_face, hb_destroy_func_t p_destroy);727728hb_font_t *_font_get_hb_handle(const RID &p_font, int64_t p_font_size) const;729730struct GlyphCompare { // For line breaking reordering.731_FORCE_INLINE_ bool operator()(const Glyph &l, const Glyph &r) const {732if (l.start == r.start) {733if (l.count == r.count) {734return (l.flags & TextServer::GRAPHEME_IS_VIRTUAL) < (r.flags & TextServer::GRAPHEME_IS_VIRTUAL);735}736return l.count > r.count; // Sort first glyph with count & flags, order of the rest are irrelevant.737} else {738return l.start < r.start;739}740}741};742743protected:744static void _bind_methods() {}745746void full_copy(ShapedTextDataAdvanced *p_shaped);747void invalidate(ShapedTextDataAdvanced *p_shaped, bool p_text = false);748749public:750MODBIND1RC(bool, has_feature, Feature);751MODBIND0RC(String, get_name);752MODBIND0RC(int64_t, get_features);753754MODBIND1(free_rid, const RID &);755MODBIND1R(bool, has, const RID &);756MODBIND1R(bool, load_support_data, const String &);757758MODBIND0RC(String, get_support_data_filename);759MODBIND0RC(String, get_support_data_info);760MODBIND1RC(bool, save_support_data, const String &);761MODBIND0RC(PackedByteArray, get_support_data);762763MODBIND1RC(bool, is_locale_right_to_left, const String &);764765MODBIND1RC(int64_t, name_to_tag, const String &);766MODBIND1RC(String, tag_to_name, int64_t);767768/* Font interface */769770MODBIND0R(RID, create_font);771MODBIND1R(RID, create_font_linked_variation, const RID &);772773MODBIND2(font_set_data, const RID &, const PackedByteArray &);774MODBIND3(font_set_data_ptr, const RID &, const uint8_t *, int64_t);775776MODBIND2(font_set_face_index, const RID &, int64_t);777MODBIND1RC(int64_t, font_get_face_index, const RID &);778779MODBIND1RC(int64_t, font_get_face_count, const RID &);780781MODBIND2(font_set_style, const RID &, BitField<FontStyle>);782MODBIND1RC(BitField<FontStyle>, font_get_style, const RID &);783784MODBIND2(font_set_style_name, const RID &, const String &);785MODBIND1RC(String, font_get_style_name, const RID &);786787MODBIND2(font_set_weight, const RID &, int64_t);788MODBIND1RC(int64_t, font_get_weight, const RID &);789790MODBIND2(font_set_stretch, const RID &, int64_t);791MODBIND1RC(int64_t, font_get_stretch, const RID &);792793MODBIND2(font_set_name, const RID &, const String &);794MODBIND1RC(String, font_get_name, const RID &);795MODBIND1RC(Dictionary, font_get_ot_name_strings, const RID &);796797MODBIND2(font_set_antialiasing, const RID &, TextServer::FontAntialiasing);798MODBIND1RC(TextServer::FontAntialiasing, font_get_antialiasing, const RID &);799800MODBIND2(font_set_disable_embedded_bitmaps, const RID &, bool);801MODBIND1RC(bool, font_get_disable_embedded_bitmaps, const RID &);802803MODBIND2(font_set_generate_mipmaps, const RID &, bool);804MODBIND1RC(bool, font_get_generate_mipmaps, const RID &);805806MODBIND2(font_set_multichannel_signed_distance_field, const RID &, bool);807MODBIND1RC(bool, font_is_multichannel_signed_distance_field, const RID &);808809MODBIND2(font_set_msdf_pixel_range, const RID &, int64_t);810MODBIND1RC(int64_t, font_get_msdf_pixel_range, const RID &);811812MODBIND2(font_set_msdf_size, const RID &, int64_t);813MODBIND1RC(int64_t, font_get_msdf_size, const RID &);814815MODBIND2(font_set_fixed_size, const RID &, int64_t);816MODBIND1RC(int64_t, font_get_fixed_size, const RID &);817818MODBIND2(font_set_fixed_size_scale_mode, const RID &, FixedSizeScaleMode);819MODBIND1RC(FixedSizeScaleMode, font_get_fixed_size_scale_mode, const RID &);820821MODBIND2(font_set_allow_system_fallback, const RID &, bool);822MODBIND1RC(bool, font_is_allow_system_fallback, const RID &);823MODBIND0(font_clear_system_fallback_cache);824825MODBIND2(font_set_force_autohinter, const RID &, bool);826MODBIND1RC(bool, font_is_force_autohinter, const RID &);827828MODBIND2(font_set_modulate_color_glyphs, const RID &, bool);829MODBIND1RC(bool, font_is_modulate_color_glyphs, const RID &);830831MODBIND2(font_set_subpixel_positioning, const RID &, SubpixelPositioning);832MODBIND1RC(SubpixelPositioning, font_get_subpixel_positioning, const RID &);833834MODBIND2(font_set_keep_rounding_remainders, const RID &, bool);835MODBIND1RC(bool, font_get_keep_rounding_remainders, const RID &);836837MODBIND2(font_set_embolden, const RID &, double);838MODBIND1RC(double, font_get_embolden, const RID &);839840MODBIND3(font_set_spacing, const RID &, SpacingType, int64_t);841MODBIND2RC(int64_t, font_get_spacing, const RID &, SpacingType);842843MODBIND2(font_set_baseline_offset, const RID &, double);844MODBIND1RC(double, font_get_baseline_offset, const RID &);845846MODBIND2(font_set_transform, const RID &, const Transform2D &);847MODBIND1RC(Transform2D, font_get_transform, const RID &);848849MODBIND2(font_set_variation_coordinates, const RID &, const Dictionary &);850MODBIND1RC(Dictionary, font_get_variation_coordinates, const RID &);851852MODBIND2(font_set_oversampling, const RID &, double);853MODBIND1RC(double, font_get_oversampling, const RID &);854855MODBIND2(font_set_hinting, const RID &, TextServer::Hinting);856MODBIND1RC(TextServer::Hinting, font_get_hinting, const RID &);857858MODBIND1RC(TypedArray<Vector2i>, font_get_size_cache_list, const RID &);859MODBIND1(font_clear_size_cache, const RID &);860MODBIND2(font_remove_size_cache, const RID &, const Vector2i &);861MODBIND1RC(TypedArray<Dictionary>, font_get_size_cache_info, const RID &);862863MODBIND3(font_set_ascent, const RID &, int64_t, double);864MODBIND2RC(double, font_get_ascent, const RID &, int64_t);865866MODBIND3(font_set_descent, const RID &, int64_t, double);867MODBIND2RC(double, font_get_descent, const RID &, int64_t);868869MODBIND3(font_set_underline_position, const RID &, int64_t, double);870MODBIND2RC(double, font_get_underline_position, const RID &, int64_t);871872MODBIND3(font_set_underline_thickness, const RID &, int64_t, double);873MODBIND2RC(double, font_get_underline_thickness, const RID &, int64_t);874875MODBIND3(font_set_scale, const RID &, int64_t, double);876MODBIND2RC(double, font_get_scale, const RID &, int64_t);877878MODBIND2RC(int64_t, font_get_texture_count, const RID &, const Vector2i &);879MODBIND2(font_clear_textures, const RID &, const Vector2i &);880MODBIND3(font_remove_texture, const RID &, const Vector2i &, int64_t);881882MODBIND4(font_set_texture_image, const RID &, const Vector2i &, int64_t, const Ref<Image> &);883MODBIND3RC(Ref<Image>, font_get_texture_image, const RID &, const Vector2i &, int64_t);884885MODBIND4(font_set_texture_offsets, const RID &, const Vector2i &, int64_t, const PackedInt32Array &);886MODBIND3RC(PackedInt32Array, font_get_texture_offsets, const RID &, const Vector2i &, int64_t);887888MODBIND2RC(PackedInt32Array, font_get_glyph_list, const RID &, const Vector2i &);889MODBIND2(font_clear_glyphs, const RID &, const Vector2i &);890MODBIND3(font_remove_glyph, const RID &, const Vector2i &, int64_t);891892MODBIND3RC(Vector2, font_get_glyph_advance, const RID &, int64_t, int64_t);893MODBIND4(font_set_glyph_advance, const RID &, int64_t, int64_t, const Vector2 &);894895MODBIND3RC(Vector2, font_get_glyph_offset, const RID &, const Vector2i &, int64_t);896MODBIND4(font_set_glyph_offset, const RID &, const Vector2i &, int64_t, const Vector2 &);897898MODBIND3RC(Vector2, font_get_glyph_size, const RID &, const Vector2i &, int64_t);899MODBIND4(font_set_glyph_size, const RID &, const Vector2i &, int64_t, const Vector2 &);900901MODBIND3RC(Rect2, font_get_glyph_uv_rect, const RID &, const Vector2i &, int64_t);902MODBIND4(font_set_glyph_uv_rect, const RID &, const Vector2i &, int64_t, const Rect2 &);903904MODBIND3RC(int64_t, font_get_glyph_texture_idx, const RID &, const Vector2i &, int64_t);905MODBIND4(font_set_glyph_texture_idx, const RID &, const Vector2i &, int64_t, int64_t);906907MODBIND3RC(RID, font_get_glyph_texture_rid, const RID &, const Vector2i &, int64_t);908MODBIND3RC(Size2, font_get_glyph_texture_size, const RID &, const Vector2i &, int64_t);909910MODBIND3RC(Dictionary, font_get_glyph_contours, const RID &, int64_t, int64_t);911912MODBIND2RC(TypedArray<Vector2i>, font_get_kerning_list, const RID &, int64_t);913MODBIND2(font_clear_kerning_map, const RID &, int64_t);914MODBIND3(font_remove_kerning, const RID &, int64_t, const Vector2i &);915916MODBIND4(font_set_kerning, const RID &, int64_t, const Vector2i &, const Vector2 &);917MODBIND3RC(Vector2, font_get_kerning, const RID &, int64_t, const Vector2i &);918919MODBIND4RC(int64_t, font_get_glyph_index, const RID &, int64_t, int64_t, int64_t);920MODBIND3RC(int64_t, font_get_char_from_glyph_index, const RID &, int64_t, int64_t);921922MODBIND2RC(bool, font_has_char, const RID &, int64_t);923MODBIND1RC(String, font_get_supported_chars, const RID &);924MODBIND1RC(PackedInt32Array, font_get_supported_glyphs, const RID &);925926MODBIND4(font_render_range, const RID &, const Vector2i &, int64_t, int64_t);927MODBIND3(font_render_glyph, const RID &, const Vector2i &, int64_t);928929MODBIND7C(font_draw_glyph, const RID &, const RID &, int64_t, const Vector2 &, int64_t, const Color &, float);930MODBIND8C(font_draw_glyph_outline, const RID &, const RID &, int64_t, int64_t, const Vector2 &, int64_t, const Color &, float);931932MODBIND2RC(bool, font_is_language_supported, const RID &, const String &);933MODBIND3(font_set_language_support_override, const RID &, const String &, bool);934MODBIND2R(bool, font_get_language_support_override, const RID &, const String &);935MODBIND2(font_remove_language_support_override, const RID &, const String &);936MODBIND1R(PackedStringArray, font_get_language_support_overrides, const RID &);937938MODBIND2RC(bool, font_is_script_supported, const RID &, const String &);939MODBIND3(font_set_script_support_override, const RID &, const String &, bool);940MODBIND2R(bool, font_get_script_support_override, const RID &, const String &);941MODBIND2(font_remove_script_support_override, const RID &, const String &);942MODBIND1R(PackedStringArray, font_get_script_support_overrides, const RID &);943944MODBIND2(font_set_opentype_feature_overrides, const RID &, const Dictionary &);945MODBIND1RC(Dictionary, font_get_opentype_feature_overrides, const RID &);946947MODBIND1RC(Dictionary, font_supported_feature_list, const RID &);948MODBIND1RC(Dictionary, font_supported_variation_list, const RID &);949950MODBIND1(reference_oversampling_level, double);951MODBIND1(unreference_oversampling_level, double);952953/* Shaped text buffer interface */954955MODBIND2R(RID, create_shaped_text, Direction, Orientation);956957MODBIND1(shaped_text_clear, const RID &);958959MODBIND2(shaped_text_set_direction, const RID &, Direction);960MODBIND1RC(Direction, shaped_text_get_direction, const RID &);961MODBIND1RC(Direction, shaped_text_get_inferred_direction, const RID &);962963MODBIND2(shaped_text_set_bidi_override, const RID &, const Array &);964965MODBIND2(shaped_text_set_custom_punctuation, const RID &, const String &);966MODBIND1RC(String, shaped_text_get_custom_punctuation, const RID &);967968MODBIND2(shaped_text_set_custom_ellipsis, const RID &, int64_t);969MODBIND1RC(int64_t, shaped_text_get_custom_ellipsis, const RID &);970971MODBIND2(shaped_text_set_orientation, const RID &, Orientation);972MODBIND1RC(Orientation, shaped_text_get_orientation, const RID &);973974MODBIND2(shaped_text_set_preserve_invalid, const RID &, bool);975MODBIND1RC(bool, shaped_text_get_preserve_invalid, const RID &);976977MODBIND2(shaped_text_set_preserve_control, const RID &, bool);978MODBIND1RC(bool, shaped_text_get_preserve_control, const RID &);979980MODBIND3(shaped_text_set_spacing, const RID &, SpacingType, int64_t);981MODBIND2RC(int64_t, shaped_text_get_spacing, const RID &, SpacingType);982983MODBIND7R(bool, shaped_text_add_string, const RID &, const String &, const TypedArray<RID> &, int64_t, const Dictionary &, const String &, const Variant &);984MODBIND6R(bool, shaped_text_add_object, const RID &, const Variant &, const Size2 &, InlineAlignment, int64_t, double);985MODBIND5R(bool, shaped_text_resize_object, const RID &, const Variant &, const Size2 &, InlineAlignment, double);986MODBIND1RC(String, shaped_get_text, const RID &);987988MODBIND1RC(int64_t, shaped_get_span_count, const RID &);989MODBIND2RC(Variant, shaped_get_span_meta, const RID &, int64_t);990MODBIND2RC(Variant, shaped_get_span_embedded_object, const RID &, int64_t);991MODBIND2RC(String, shaped_get_span_text, const RID &, int64_t);992MODBIND2RC(Variant, shaped_get_span_object, const RID &, int64_t);993MODBIND5(shaped_set_span_update_font, const RID &, int64_t, const TypedArray<RID> &, int64_t, const Dictionary &);994995MODBIND1RC(int64_t, shaped_get_run_count, const RID &);996MODBIND2RC(String, shaped_get_run_text, const RID &, int64_t);997MODBIND2RC(Vector2i, shaped_get_run_range, const RID &, int64_t);998MODBIND2RC(RID, shaped_get_run_font_rid, const RID &, int64_t);999MODBIND2RC(int, shaped_get_run_font_size, const RID &, int64_t);1000MODBIND2RC(String, shaped_get_run_language, const RID &, int64_t);1001MODBIND2RC(Direction, shaped_get_run_direction, const RID &, int64_t);1002MODBIND2RC(Variant, shaped_get_run_object, const RID &, int64_t);10031004MODBIND3RC(RID, shaped_text_substr, const RID &, int64_t, int64_t);1005MODBIND1RC(RID, shaped_text_get_parent, const RID &);10061007MODBIND3R(double, shaped_text_fit_to_width, const RID &, double, BitField<TextServer::JustificationFlag>);1008MODBIND2R(double, shaped_text_tab_align, const RID &, const PackedFloat32Array &);10091010MODBIND1R(bool, shaped_text_shape, const RID &);1011MODBIND1R(bool, shaped_text_update_breaks, const RID &);1012MODBIND1R(bool, shaped_text_update_justification_ops, const RID &);10131014MODBIND1RC(int64_t, shaped_text_get_trim_pos, const RID &);1015MODBIND1RC(int64_t, shaped_text_get_ellipsis_pos, const RID &);1016MODBIND1RC(const Glyph *, shaped_text_get_ellipsis_glyphs, const RID &);1017MODBIND1RC(int64_t, shaped_text_get_ellipsis_glyph_count, const RID &);10181019MODBIND3(shaped_text_overrun_trim_to_width, const RID &, double, BitField<TextServer::TextOverrunFlag>);10201021MODBIND1RC(bool, shaped_text_is_ready, const RID &);10221023MODBIND1RC(const Glyph *, shaped_text_get_glyphs, const RID &);1024MODBIND1R(const Glyph *, shaped_text_sort_logical, const RID &);1025MODBIND1RC(int64_t, shaped_text_get_glyph_count, const RID &);10261027MODBIND1RC(Vector2i, shaped_text_get_range, const RID &);10281029MODBIND1RC(Array, shaped_text_get_objects, const RID &);1030MODBIND2RC(Rect2, shaped_text_get_object_rect, const RID &, const Variant &);1031MODBIND2RC(Vector2i, shaped_text_get_object_range, const RID &, const Variant &);1032MODBIND2RC(int64_t, shaped_text_get_object_glyph, const RID &, const Variant &);10331034MODBIND1RC(Size2, shaped_text_get_size, const RID &);1035MODBIND1RC(double, shaped_text_get_ascent, const RID &);1036MODBIND1RC(double, shaped_text_get_descent, const RID &);1037MODBIND1RC(double, shaped_text_get_width, const RID &);1038MODBIND1RC(double, shaped_text_get_underline_position, const RID &);1039MODBIND1RC(double, shaped_text_get_underline_thickness, const RID &);10401041MODBIND1RC(PackedInt32Array, shaped_text_get_character_breaks, const RID &);10421043MODBIND2RC(String, format_number, const String &, const String &);1044MODBIND2RC(String, parse_number, const String &, const String &);1045MODBIND1RC(String, percent_sign, const String &);10461047MODBIND3RC(PackedInt32Array, string_get_word_breaks, const String &, const String &, int64_t);1048MODBIND2RC(PackedInt32Array, string_get_character_breaks, const String &, const String &);10491050MODBIND2RC(int64_t, is_confusable, const String &, const PackedStringArray &);1051MODBIND1RC(bool, spoof_check, const String &);10521053MODBIND1RC(String, strip_diacritics, const String &);1054MODBIND1RC(bool, is_valid_identifier, const String &);1055MODBIND1RC(bool, is_valid_letter, uint64_t);10561057MODBIND2RC(String, string_to_upper, const String &, const String &);1058MODBIND2RC(String, string_to_lower, const String &, const String &);1059MODBIND2RC(String, string_to_title, const String &, const String &);10601061MODBIND0(cleanup);10621063TextServerAdvanced();1064~TextServerAdvanced();1065};106610671068