| 1 | /**************************************************************************/ |
| 2 | /* text_server_adv.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "text_server_adv.h" |
| 32 | |
| 33 | #ifdef GDEXTENSION |
| 34 | // Headers for building as GDExtension plug-in. |
| 35 | |
| 36 | #include <godot_cpp/classes/file_access.hpp> |
| 37 | #include <godot_cpp/classes/os.hpp> |
| 38 | #include <godot_cpp/classes/project_settings.hpp> |
| 39 | #include <godot_cpp/classes/rendering_server.hpp> |
| 40 | #include <godot_cpp/classes/translation_server.hpp> |
| 41 | #include <godot_cpp/core/error_macros.hpp> |
| 42 | |
| 43 | using namespace godot; |
| 44 | |
| 45 | #define GLOBAL_GET(m_var) ProjectSettings::get_singleton()->get_setting_with_override(m_var) |
| 46 | |
| 47 | #elif defined(GODOT_MODULE) |
| 48 | // Headers for building as built-in module. |
| 49 | |
| 50 | #include "core/config/project_settings.h" |
| 51 | #include "core/error/error_macros.h" |
| 52 | #include "core/io/file_access.h" |
| 53 | #include "core/object/worker_thread_pool.h" |
| 54 | #include "core/string/translation_server.h" |
| 55 | #include "scene/resources/image_texture.h" |
| 56 | #include "servers/rendering/rendering_server.h" |
| 57 | |
| 58 | #include "modules/modules_enabled.gen.h" // For freetype, msdfgen, svg. |
| 59 | |
| 60 | #endif |
| 61 | |
| 62 | // Built-in ICU data. |
| 63 | |
| 64 | #ifdef ICU_STATIC_DATA |
| 65 | #include <icudata.gen.h> |
| 66 | #endif |
| 67 | |
| 68 | // Thirdparty headers. |
| 69 | |
| 70 | #ifdef MODULE_MSDFGEN_ENABLED |
| 71 | GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Wshadow") |
| 72 | GODOT_MSVC_WARNING_PUSH_AND_IGNORE(4458) // "Declaration of 'identifier' hides class member". |
| 73 | |
| 74 | #include <core/EdgeHolder.h> |
| 75 | #include <core/ShapeDistanceFinder.h> |
| 76 | #include <core/contour-combiners.h> |
| 77 | #include <core/edge-selectors.h> |
| 78 | #include <msdfgen.h> |
| 79 | |
| 80 | GODOT_GCC_WARNING_POP |
| 81 | GODOT_MSVC_WARNING_POP |
| 82 | #endif |
| 83 | |
| 84 | #ifdef MODULE_SVG_ENABLED |
| 85 | #ifdef MODULE_FREETYPE_ENABLED |
| 86 | #include "thorvg_svg_in_ot.h" |
| 87 | #endif |
| 88 | #endif |
| 89 | |
| 90 | /*************************************************************************/ |
| 91 | /* bmp_font_t HarfBuzz Bitmap font interface */ |
| 92 | /*************************************************************************/ |
| 93 | |
| 94 | hb_font_funcs_t *TextServerAdvanced::funcs = nullptr; |
| 95 | |
| 96 | TextServerAdvanced::bmp_font_t *TextServerAdvanced::_bmp_font_create(TextServerAdvanced::FontForSizeAdvanced *p_face, bool p_unref) { |
| 97 | bmp_font_t *bm_font = memnew(bmp_font_t); |
| 98 | |
| 99 | if (!bm_font) { |
| 100 | return nullptr; |
| 101 | } |
| 102 | |
| 103 | bm_font->face = p_face; |
| 104 | bm_font->unref = p_unref; |
| 105 | |
| 106 | return bm_font; |
| 107 | } |
| 108 | |
| 109 | void TextServerAdvanced::_bmp_font_destroy(void *p_data) { |
| 110 | bmp_font_t *bm_font = static_cast<bmp_font_t *>(p_data); |
| 111 | memdelete(bm_font); |
| 112 | } |
| 113 | |
| 114 | hb_bool_t TextServerAdvanced::_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) { |
| 115 | const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data); |
| 116 | |
| 117 | if (!bm_font->face) { |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | if (!bm_font->face->glyph_map.has(p_unicode)) { |
| 122 | if (bm_font->face->glyph_map.has(0xf000u + p_unicode)) { |
| 123 | *r_glyph = 0xf000u + p_unicode; |
| 124 | return true; |
| 125 | } else { |
| 126 | return false; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | *r_glyph = p_unicode; |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | hb_position_t TextServerAdvanced::_bmp_get_glyph_h_advance(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_glyph, void *p_user_data) { |
| 135 | const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data); |
| 136 | |
| 137 | if (!bm_font->face) { |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | HashMap<int32_t, FontGlyph>::Iterator E = bm_font->face->glyph_map.find(p_glyph); |
| 142 | if (!E) { |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | return E->value.advance.x * 64; |
| 147 | } |
| 148 | |
| 149 | hb_position_t TextServerAdvanced::_bmp_get_glyph_v_advance(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_glyph, void *p_user_data) { |
| 150 | const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data); |
| 151 | |
| 152 | if (!bm_font->face) { |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | HashMap<int32_t, FontGlyph>::Iterator E = bm_font->face->glyph_map.find(p_glyph); |
| 157 | if (!E) { |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | return -E->value.advance.y * 64; |
| 162 | } |
| 163 | |
| 164 | hb_position_t TextServerAdvanced::_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) { |
| 165 | const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data); |
| 166 | |
| 167 | if (!bm_font->face) { |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | if (!bm_font->face->kerning_map.has(Vector2i(p_left_glyph, p_right_glyph))) { |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | return bm_font->face->kerning_map[Vector2i(p_left_glyph, p_right_glyph)].x * 64; |
| 176 | } |
| 177 | |
| 178 | hb_bool_t TextServerAdvanced::_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) { |
| 179 | const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data); |
| 180 | |
| 181 | if (!bm_font->face) { |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | HashMap<int32_t, FontGlyph>::Iterator E = bm_font->face->glyph_map.find(p_glyph); |
| 186 | if (!E) { |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | *r_x = E->value.advance.x * 32; |
| 191 | *r_y = -bm_font->face->ascent * 64; |
| 192 | |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | hb_bool_t TextServerAdvanced::_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) { |
| 197 | const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data); |
| 198 | |
| 199 | if (!bm_font->face) { |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | HashMap<int32_t, FontGlyph>::Iterator E = bm_font->face->glyph_map.find(p_glyph); |
| 204 | if (!E) { |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | r_extents->x_bearing = 0; |
| 209 | r_extents->y_bearing = 0; |
| 210 | r_extents->width = E->value.rect.size.x * 64; |
| 211 | r_extents->height = E->value.rect.size.y * 64; |
| 212 | |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | hb_bool_t TextServerAdvanced::_bmp_get_font_h_extents(hb_font_t *p_font, void *p_font_data, hb_font_extents_t *r_metrics, void *p_user_data) { |
| 217 | const bmp_font_t *bm_font = static_cast<const bmp_font_t *>(p_font_data); |
| 218 | |
| 219 | if (!bm_font->face) { |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | r_metrics->ascender = bm_font->face->ascent; |
| 224 | r_metrics->descender = bm_font->face->descent; |
| 225 | r_metrics->line_gap = 0; |
| 226 | |
| 227 | return true; |
| 228 | } |
| 229 | |
| 230 | void TextServerAdvanced::_bmp_create_font_funcs() { |
| 231 | if (funcs == nullptr) { |
| 232 | funcs = hb_font_funcs_create(); |
| 233 | |
| 234 | hb_font_funcs_set_font_h_extents_func(funcs, _bmp_get_font_h_extents, nullptr, nullptr); |
| 235 | hb_font_funcs_set_nominal_glyph_func(funcs, _bmp_get_nominal_glyph, nullptr, nullptr); |
| 236 | hb_font_funcs_set_glyph_h_advance_func(funcs, _bmp_get_glyph_h_advance, nullptr, nullptr); |
| 237 | hb_font_funcs_set_glyph_v_advance_func(funcs, _bmp_get_glyph_v_advance, nullptr, nullptr); |
| 238 | hb_font_funcs_set_glyph_v_origin_func(funcs, _bmp_get_glyph_v_origin, nullptr, nullptr); |
| 239 | hb_font_funcs_set_glyph_h_kerning_func(funcs, _bmp_get_glyph_h_kerning, nullptr, nullptr); |
| 240 | hb_font_funcs_set_glyph_extents_func(funcs, _bmp_get_glyph_extents, nullptr, nullptr); |
| 241 | |
| 242 | hb_font_funcs_make_immutable(funcs); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | void TextServerAdvanced::_bmp_free_font_funcs() { |
| 247 | if (funcs != nullptr) { |
| 248 | hb_font_funcs_destroy(funcs); |
| 249 | funcs = nullptr; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | void TextServerAdvanced::_bmp_font_set_funcs(hb_font_t *p_font, TextServerAdvanced::FontForSizeAdvanced *p_face, bool p_unref) { |
| 254 | hb_font_set_funcs(p_font, funcs, _bmp_font_create(p_face, p_unref), _bmp_font_destroy); |
| 255 | } |
| 256 | |
| 257 | hb_font_t *TextServerAdvanced::_bmp_font_create(TextServerAdvanced::FontForSizeAdvanced *p_face, hb_destroy_func_t p_destroy) { |
| 258 | hb_font_t *font; |
| 259 | hb_face_t *face = hb_face_create(nullptr, 0); |
| 260 | |
| 261 | font = hb_font_create(face); |
| 262 | hb_face_destroy(face); |
| 263 | _bmp_font_set_funcs(font, p_face, false); |
| 264 | return font; |
| 265 | } |
| 266 | |
| 267 | /*************************************************************************/ |
| 268 | /* Character properties. */ |
| 269 | /*************************************************************************/ |
| 270 | |
| 271 | _FORCE_INLINE_ bool is_ain(char32_t p_chr) { |
| 272 | return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_AIN; |
| 273 | } |
| 274 | |
| 275 | _FORCE_INLINE_ bool is_alef(char32_t p_chr) { |
| 276 | return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_ALEF; |
| 277 | } |
| 278 | |
| 279 | _FORCE_INLINE_ bool is_beh(char32_t p_chr) { |
| 280 | int32_t prop = u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP); |
| 281 | return (prop == U_JG_BEH) || (prop == U_JG_NOON) || (prop == U_JG_AFRICAN_NOON) || (prop == U_JG_NYA) || (prop == U_JG_YEH) || (prop == U_JG_FARSI_YEH); |
| 282 | } |
| 283 | |
| 284 | _FORCE_INLINE_ bool is_dal(char32_t p_chr) { |
| 285 | return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_DAL; |
| 286 | } |
| 287 | |
| 288 | _FORCE_INLINE_ bool is_feh(char32_t p_chr) { |
| 289 | return (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_FEH) || (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_AFRICAN_FEH); |
| 290 | } |
| 291 | |
| 292 | _FORCE_INLINE_ bool is_gaf(char32_t p_chr) { |
| 293 | return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_GAF; |
| 294 | } |
| 295 | |
| 296 | _FORCE_INLINE_ bool is_heh(char32_t p_chr) { |
| 297 | return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_HEH; |
| 298 | } |
| 299 | |
| 300 | _FORCE_INLINE_ bool is_kaf(char32_t p_chr) { |
| 301 | return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_KAF; |
| 302 | } |
| 303 | |
| 304 | _FORCE_INLINE_ bool is_lam(char32_t p_chr) { |
| 305 | return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_LAM; |
| 306 | } |
| 307 | |
| 308 | _FORCE_INLINE_ bool is_qaf(char32_t p_chr) { |
| 309 | return (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_QAF) || (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_AFRICAN_QAF); |
| 310 | } |
| 311 | |
| 312 | _FORCE_INLINE_ bool is_reh(char32_t p_chr) { |
| 313 | return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_REH; |
| 314 | } |
| 315 | |
| 316 | _FORCE_INLINE_ bool is_seen_sad(char32_t p_chr) { |
| 317 | return (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_SAD) || (u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_SEEN); |
| 318 | } |
| 319 | |
| 320 | _FORCE_INLINE_ bool is_tah(char32_t p_chr) { |
| 321 | return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_TAH; |
| 322 | } |
| 323 | |
| 324 | _FORCE_INLINE_ bool is_teh_marbuta(char32_t p_chr) { |
| 325 | return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_TEH_MARBUTA; |
| 326 | } |
| 327 | |
| 328 | _FORCE_INLINE_ bool is_yeh(char32_t p_chr) { |
| 329 | int32_t prop = u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP); |
| 330 | return (prop == U_JG_YEH) || (prop == U_JG_FARSI_YEH) || (prop == U_JG_YEH_BARREE) || (prop == U_JG_BURUSHASKI_YEH_BARREE) || (prop == U_JG_YEH_WITH_TAIL); |
| 331 | } |
| 332 | |
| 333 | _FORCE_INLINE_ bool is_waw(char32_t p_chr) { |
| 334 | return u_getIntPropertyValue(p_chr, UCHAR_JOINING_GROUP) == U_JG_WAW; |
| 335 | } |
| 336 | |
| 337 | _FORCE_INLINE_ bool is_transparent(char32_t p_chr) { |
| 338 | return u_getIntPropertyValue(p_chr, UCHAR_JOINING_TYPE) == U_JT_TRANSPARENT; |
| 339 | } |
| 340 | |
| 341 | _FORCE_INLINE_ bool is_ligature(char32_t p_chr, char32_t p_nchr) { |
| 342 | return (is_lam(p_chr) && is_alef(p_nchr)); |
| 343 | } |
| 344 | |
| 345 | _FORCE_INLINE_ bool is_connected_to_prev(char32_t p_chr, char32_t p_pchr) { |
| 346 | int32_t prop = u_getIntPropertyValue(p_pchr, UCHAR_JOINING_TYPE); |
| 347 | return (prop != U_JT_RIGHT_JOINING) && (prop != U_JT_NON_JOINING) ? !is_ligature(p_pchr, p_chr) : false; |
| 348 | } |
| 349 | |
| 350 | /*************************************************************************/ |
| 351 | |
| 352 | bool TextServerAdvanced::icu_data_loaded = false; |
| 353 | PackedByteArray TextServerAdvanced::icu_data; |
| 354 | |
| 355 | bool TextServerAdvanced::_has_feature(Feature p_feature) const { |
| 356 | switch (p_feature) { |
| 357 | case FEATURE_SIMPLE_LAYOUT: |
| 358 | case FEATURE_BIDI_LAYOUT: |
| 359 | case FEATURE_VERTICAL_LAYOUT: |
| 360 | case FEATURE_SHAPING: |
| 361 | case FEATURE_KASHIDA_JUSTIFICATION: |
| 362 | case FEATURE_BREAK_ITERATORS: |
| 363 | case FEATURE_FONT_BITMAP: |
| 364 | #ifdef MODULE_FREETYPE_ENABLED |
| 365 | case FEATURE_FONT_DYNAMIC: |
| 366 | #endif |
| 367 | #ifdef MODULE_MSDFGEN_ENABLED |
| 368 | case FEATURE_FONT_MSDF: |
| 369 | #endif |
| 370 | case FEATURE_FONT_VARIABLE: |
| 371 | case FEATURE_CONTEXT_SENSITIVE_CASE_CONVERSION: |
| 372 | case FEATURE_USE_SUPPORT_DATA: |
| 373 | case FEATURE_UNICODE_IDENTIFIERS: |
| 374 | case FEATURE_UNICODE_SECURITY: |
| 375 | return true; |
| 376 | default: { |
| 377 | } |
| 378 | } |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | String TextServerAdvanced::_get_name() const { |
| 383 | #ifdef GDEXTENSION |
| 384 | return "ICU / HarfBuzz / Graphite (GDExtension)"; |
| 385 | #elif defined(GODOT_MODULE) |
| 386 | return "ICU / HarfBuzz / Graphite (Built-in)"; |
| 387 | #endif |
| 388 | } |
| 389 | |
| 390 | int64_t TextServerAdvanced::_get_features() const { |
| 391 | int64_t interface_features = FEATURE_SIMPLE_LAYOUT | FEATURE_BIDI_LAYOUT | FEATURE_VERTICAL_LAYOUT | FEATURE_SHAPING | FEATURE_KASHIDA_JUSTIFICATION | FEATURE_BREAK_ITERATORS | FEATURE_FONT_BITMAP | FEATURE_FONT_VARIABLE | FEATURE_CONTEXT_SENSITIVE_CASE_CONVERSION | FEATURE_USE_SUPPORT_DATA; |
| 392 | #ifdef MODULE_FREETYPE_ENABLED |
| 393 | interface_features |= FEATURE_FONT_DYNAMIC; |
| 394 | #endif |
| 395 | #ifdef MODULE_MSDFGEN_ENABLED |
| 396 | interface_features |= FEATURE_FONT_MSDF; |
| 397 | #endif |
| 398 | |
| 399 | return interface_features; |
| 400 | } |
| 401 | |
| 402 | void TextServerAdvanced::_free_rid(const RID &p_rid) { |
| 403 | _THREAD_SAFE_METHOD_ |
| 404 | if (font_owner.owns(p_rid)) { |
| 405 | MutexLock ftlock(ft_mutex); |
| 406 | |
| 407 | FontAdvanced *fd = font_owner.get_or_null(p_rid); |
| 408 | for (const KeyValue<Vector2i, FontForSizeAdvanced *> &ffsd : fd->cache) { |
| 409 | OversamplingLevel *ol = oversampling_levels.getptr(ffsd.value->viewport_oversampling); |
| 410 | if (ol != nullptr) { |
| 411 | ol->fonts.erase(ffsd.value); |
| 412 | } |
| 413 | } |
| 414 | { |
| 415 | MutexLock lock(fd->mutex); |
| 416 | font_owner.free(p_rid); |
| 417 | } |
| 418 | memdelete(fd); |
| 419 | } else if (font_var_owner.owns(p_rid)) { |
| 420 | MutexLock ftlock(ft_mutex); |
| 421 | |
| 422 | FontAdvancedLinkedVariation *fdv = font_var_owner.get_or_null(p_rid); |
| 423 | { |
| 424 | font_var_owner.free(p_rid); |
| 425 | } |
| 426 | memdelete(fdv); |
| 427 | } else if (shaped_owner.owns(p_rid)) { |
| 428 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_rid); |
| 429 | { |
| 430 | MutexLock lock(sd->mutex); |
| 431 | shaped_owner.free(p_rid); |
| 432 | } |
| 433 | memdelete(sd); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | bool TextServerAdvanced::_has(const RID &p_rid) { |
| 438 | _THREAD_SAFE_METHOD_ |
| 439 | return font_owner.owns(p_rid) || font_var_owner.owns(p_rid) || shaped_owner.owns(p_rid); |
| 440 | } |
| 441 | |
| 442 | bool TextServerAdvanced::_load_support_data(const String &p_filename) { |
| 443 | _THREAD_SAFE_METHOD_ |
| 444 | |
| 445 | #if defined(ICU_STATIC_DATA) || !defined(HAVE_ICU_BUILTIN) |
| 446 | if (!icu_data_loaded) { |
| 447 | UErrorCode err = U_ZERO_ERROR; |
| 448 | u_init(&err); // Do not check for errors, since we only load part of the data. |
| 449 | icu_data_loaded = true; |
| 450 | } |
| 451 | #else |
| 452 | if (!icu_data_loaded) { |
| 453 | UErrorCode err = U_ZERO_ERROR; |
| 454 | String filename = (p_filename.is_empty()) ? String("res://icudt_godot.dat") : p_filename; |
| 455 | if (FileAccess::exists(filename)) { |
| 456 | Ref<FileAccess> f = FileAccess::open(filename, FileAccess::READ); |
| 457 | if (f.is_null()) { |
| 458 | return false; |
| 459 | } |
| 460 | uint64_t len = f->get_length(); |
| 461 | icu_data = f->get_buffer(len); |
| 462 | |
| 463 | udata_setCommonData(icu_data.ptr(), &err); |
| 464 | if (U_FAILURE(err)) { |
| 465 | ERR_FAIL_V_MSG(false, u_errorName(err)); |
| 466 | } |
| 467 | |
| 468 | err = U_ZERO_ERROR; |
| 469 | icu_data_loaded = true; |
| 470 | } |
| 471 | |
| 472 | u_init(&err); |
| 473 | if (U_FAILURE(err)) { |
| 474 | ERR_FAIL_V_MSG(false, u_errorName(err)); |
| 475 | } |
| 476 | } |
| 477 | #endif |
| 478 | return true; |
| 479 | } |
| 480 | |
| 481 | String TextServerAdvanced::_get_support_data_filename() const { |
| 482 | return String("icudt_godot.dat"); |
| 483 | } |
| 484 | |
| 485 | String TextServerAdvanced::_get_support_data_info() const { |
| 486 | return String("ICU break iteration data (\"icudt_godot.dat\")."); |
| 487 | } |
| 488 | |
| 489 | bool TextServerAdvanced::_save_support_data(const String &p_filename) const { |
| 490 | _THREAD_SAFE_METHOD_ |
| 491 | #ifdef ICU_STATIC_DATA |
| 492 | |
| 493 | // Store data to the res file if it's available. |
| 494 | |
| 495 | Ref<FileAccess> f = FileAccess::open(p_filename, FileAccess::WRITE); |
| 496 | if (f.is_null()) { |
| 497 | return false; |
| 498 | } |
| 499 | |
| 500 | PackedByteArray icu_data_static; |
| 501 | icu_data_static.resize(U_ICUDATA_SIZE); |
| 502 | memcpy(icu_data_static.ptrw(), U_ICUDATA_ENTRY_POINT, U_ICUDATA_SIZE); |
| 503 | f->store_buffer(icu_data_static); |
| 504 | |
| 505 | return true; |
| 506 | #else |
| 507 | return false; |
| 508 | #endif |
| 509 | } |
| 510 | |
| 511 | PackedByteArray TextServerAdvanced::_get_support_data() const { |
| 512 | _THREAD_SAFE_METHOD_ |
| 513 | #ifdef ICU_STATIC_DATA |
| 514 | |
| 515 | PackedByteArray icu_data_static; |
| 516 | icu_data_static.resize(U_ICUDATA_SIZE); |
| 517 | memcpy(icu_data_static.ptrw(), U_ICUDATA_ENTRY_POINT, U_ICUDATA_SIZE); |
| 518 | |
| 519 | return icu_data_static; |
| 520 | #else |
| 521 | return icu_data; |
| 522 | #endif |
| 523 | } |
| 524 | |
| 525 | bool TextServerAdvanced::_is_locale_using_support_data(const String &p_locale) const { |
| 526 | String l = p_locale.get_slicec('_', 0); |
| 527 | if ((l == "my") || (l == "zh") || (l == "ja") || (l == "ko") || (l == "km") || (l == "lo") || (l == "th")) { |
| 528 | return true; |
| 529 | } else { |
| 530 | return false; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | bool TextServerAdvanced::_is_locale_right_to_left(const String &p_locale) const { |
| 535 | String l = p_locale.get_slicec('_', 0); |
| 536 | if ((l == "ar") || (l == "dv") || (l == "he") || (l == "fa") || (l == "ff") || (l == "ku") || (l == "ur")) { |
| 537 | return true; |
| 538 | } else { |
| 539 | return false; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | _FORCE_INLINE_ void TextServerAdvanced::_insert_feature(const StringName &p_name, int32_t p_tag, Variant::Type p_vtype, bool p_hidden) { |
| 544 | FeatureInfo fi; |
| 545 | fi.name = p_name; |
| 546 | fi.vtype = p_vtype; |
| 547 | fi.hidden = p_hidden; |
| 548 | |
| 549 | feature_sets.insert(p_name, p_tag); |
| 550 | feature_sets_inv.insert(p_tag, fi); |
| 551 | } |
| 552 | |
| 553 | void TextServerAdvanced::_insert_feature_sets() { |
| 554 | // Registered OpenType feature tags. |
| 555 | // Name, Tag, Data Type, Hidden |
| 556 | _insert_feature("access_all_alternates", HB_TAG('a', 'a', 'l', 't'), Variant::Type::INT, false); |
| 557 | _insert_feature("above_base_forms", HB_TAG('a', 'b', 'v', 'f'), Variant::Type::INT, true); |
| 558 | _insert_feature("above_base_mark_positioning", HB_TAG('a', 'b', 'v', 'm'), Variant::Type::INT, true); |
| 559 | _insert_feature("above_base_substitutions", HB_TAG('a', 'b', 'v', 's'), Variant::Type::INT, true); |
| 560 | _insert_feature("alternative_fractions", HB_TAG('a', 'f', 'r', 'c'), Variant::Type::INT, false); |
| 561 | _insert_feature("akhands", HB_TAG('a', 'k', 'h', 'n'), Variant::Type::INT, true); |
| 562 | _insert_feature("below_base_forms", HB_TAG('b', 'l', 'w', 'f'), Variant::Type::INT, true); |
| 563 | _insert_feature("below_base_mark_positioning", HB_TAG('b', 'l', 'w', 'm'), Variant::Type::INT, true); |
| 564 | _insert_feature("below_base_substitutions", HB_TAG('b', 'l', 'w', 's'), Variant::Type::INT, true); |
| 565 | _insert_feature("contextual_alternates", HB_TAG('c', 'a', 'l', 't'), Variant::Type::BOOL, false); |
| 566 | _insert_feature("case_sensitive_forms", HB_TAG('c', 'a', 's', 'e'), Variant::Type::BOOL, false); |
| 567 | _insert_feature("glyph_composition", HB_TAG('c', 'c', 'm', 'p'), Variant::Type::INT, true); |
| 568 | _insert_feature("conjunct_form_after_ro", HB_TAG('c', 'f', 'a', 'r'), Variant::Type::INT, true); |
| 569 | _insert_feature("contextual_half_width_spacing", HB_TAG('c', 'h', 'w', 's'), Variant::Type::INT, true); |
| 570 | _insert_feature("conjunct_forms", HB_TAG('c', 'j', 'c', 't'), Variant::Type::INT, true); |
| 571 | _insert_feature("contextual_ligatures", HB_TAG('c', 'l', 'i', 'g'), Variant::Type::BOOL, false); |
| 572 | _insert_feature("centered_cjk_punctuation", HB_TAG('c', 'p', 'c', 't'), Variant::Type::BOOL, false); |
| 573 | _insert_feature("capital_spacing", HB_TAG('c', 'p', 's', 'p'), Variant::Type::BOOL, false); |
| 574 | _insert_feature("contextual_swash", HB_TAG('c', 's', 'w', 'h'), Variant::Type::INT, false); |
| 575 | _insert_feature("cursive_positioning", HB_TAG('c', 'u', 'r', 's'), Variant::Type::INT, true); |
| 576 | _insert_feature("character_variant_01", HB_TAG('c', 'v', '0', '1'), Variant::Type::BOOL, false); |
| 577 | _insert_feature("character_variant_02", HB_TAG('c', 'v', '0', '2'), Variant::Type::BOOL, false); |
| 578 | _insert_feature("character_variant_03", HB_TAG('c', 'v', '0', '3'), Variant::Type::BOOL, false); |
| 579 | _insert_feature("character_variant_04", HB_TAG('c', 'v', '0', '4'), Variant::Type::BOOL, false); |
| 580 | _insert_feature("character_variant_05", HB_TAG('c', 'v', '0', '5'), Variant::Type::BOOL, false); |
| 581 | _insert_feature("character_variant_06", HB_TAG('c', 'v', '0', '6'), Variant::Type::BOOL, false); |
| 582 | _insert_feature("character_variant_07", HB_TAG('c', 'v', '0', '7'), Variant::Type::BOOL, false); |
| 583 | _insert_feature("character_variant_08", HB_TAG('c', 'v', '0', '8'), Variant::Type::BOOL, false); |
| 584 | _insert_feature("character_variant_09", HB_TAG('c', 'v', '0', '9'), Variant::Type::BOOL, false); |
| 585 | _insert_feature("character_variant_10", HB_TAG('c', 'v', '1', '0'), Variant::Type::BOOL, false); |
| 586 | _insert_feature("character_variant_11", HB_TAG('c', 'v', '1', '1'), Variant::Type::BOOL, false); |
| 587 | _insert_feature("character_variant_12", HB_TAG('c', 'v', '1', '2'), Variant::Type::BOOL, false); |
| 588 | _insert_feature("character_variant_13", HB_TAG('c', 'v', '1', '3'), Variant::Type::BOOL, false); |
| 589 | _insert_feature("character_variant_14", HB_TAG('c', 'v', '1', '4'), Variant::Type::BOOL, false); |
| 590 | _insert_feature("character_variant_15", HB_TAG('c', 'v', '1', '5'), Variant::Type::BOOL, false); |
| 591 | _insert_feature("character_variant_16", HB_TAG('c', 'v', '1', '6'), Variant::Type::BOOL, false); |
| 592 | _insert_feature("character_variant_17", HB_TAG('c', 'v', '1', '7'), Variant::Type::BOOL, false); |
| 593 | _insert_feature("character_variant_18", HB_TAG('c', 'v', '1', '8'), Variant::Type::BOOL, false); |
| 594 | _insert_feature("character_variant_19", HB_TAG('c', 'v', '1', '9'), Variant::Type::BOOL, false); |
| 595 | _insert_feature("character_variant_20", HB_TAG('c', 'v', '2', '0'), Variant::Type::BOOL, false); |
| 596 | _insert_feature("character_variant_21", HB_TAG('c', 'v', '2', '1'), Variant::Type::BOOL, false); |
| 597 | _insert_feature("character_variant_22", HB_TAG('c', 'v', '2', '2'), Variant::Type::BOOL, false); |
| 598 | _insert_feature("character_variant_23", HB_TAG('c', 'v', '2', '3'), Variant::Type::BOOL, false); |
| 599 | _insert_feature("character_variant_24", HB_TAG('c', 'v', '2', '4'), Variant::Type::BOOL, false); |
| 600 | _insert_feature("character_variant_25", HB_TAG('c', 'v', '2', '5'), Variant::Type::BOOL, false); |
| 601 | _insert_feature("character_variant_26", HB_TAG('c', 'v', '2', '6'), Variant::Type::BOOL, false); |
| 602 | _insert_feature("character_variant_27", HB_TAG('c', 'v', '2', '7'), Variant::Type::BOOL, false); |
| 603 | _insert_feature("character_variant_28", HB_TAG('c', 'v', '2', '8'), Variant::Type::BOOL, false); |
| 604 | _insert_feature("character_variant_29", HB_TAG('c', 'v', '2', '9'), Variant::Type::BOOL, false); |
| 605 | _insert_feature("character_variant_30", HB_TAG('c', 'v', '3', '0'), Variant::Type::BOOL, false); |
| 606 | _insert_feature("character_variant_31", HB_TAG('c', 'v', '3', '1'), Variant::Type::BOOL, false); |
| 607 | _insert_feature("character_variant_32", HB_TAG('c', 'v', '3', '2'), Variant::Type::BOOL, false); |
| 608 | _insert_feature("character_variant_33", HB_TAG('c', 'v', '3', '3'), Variant::Type::BOOL, false); |
| 609 | _insert_feature("character_variant_34", HB_TAG('c', 'v', '3', '4'), Variant::Type::BOOL, false); |
| 610 | _insert_feature("character_variant_35", HB_TAG('c', 'v', '3', '5'), Variant::Type::BOOL, false); |
| 611 | _insert_feature("character_variant_36", HB_TAG('c', 'v', '3', '6'), Variant::Type::BOOL, false); |
| 612 | _insert_feature("character_variant_37", HB_TAG('c', 'v', '3', '7'), Variant::Type::BOOL, false); |
| 613 | _insert_feature("character_variant_38", HB_TAG('c', 'v', '3', '8'), Variant::Type::BOOL, false); |
| 614 | _insert_feature("character_variant_39", HB_TAG('c', 'v', '3', '9'), Variant::Type::BOOL, false); |
| 615 | _insert_feature("character_variant_40", HB_TAG('c', 'v', '4', '0'), Variant::Type::BOOL, false); |
| 616 | _insert_feature("character_variant_41", HB_TAG('c', 'v', '4', '1'), Variant::Type::BOOL, false); |
| 617 | _insert_feature("character_variant_42", HB_TAG('c', 'v', '4', '2'), Variant::Type::BOOL, false); |
| 618 | _insert_feature("character_variant_43", HB_TAG('c', 'v', '4', '3'), Variant::Type::BOOL, false); |
| 619 | _insert_feature("character_variant_44", HB_TAG('c', 'v', '4', '4'), Variant::Type::BOOL, false); |
| 620 | _insert_feature("character_variant_45", HB_TAG('c', 'v', '4', '5'), Variant::Type::BOOL, false); |
| 621 | _insert_feature("character_variant_46", HB_TAG('c', 'v', '4', '6'), Variant::Type::BOOL, false); |
| 622 | _insert_feature("character_variant_47", HB_TAG('c', 'v', '4', '7'), Variant::Type::BOOL, false); |
| 623 | _insert_feature("character_variant_48", HB_TAG('c', 'v', '4', '8'), Variant::Type::BOOL, false); |
| 624 | _insert_feature("character_variant_49", HB_TAG('c', 'v', '4', '9'), Variant::Type::BOOL, false); |
| 625 | _insert_feature("character_variant_50", HB_TAG('c', 'v', '5', '0'), Variant::Type::BOOL, false); |
| 626 | _insert_feature("character_variant_51", HB_TAG('c', 'v', '5', '1'), Variant::Type::BOOL, false); |
| 627 | _insert_feature("character_variant_52", HB_TAG('c', 'v', '5', '2'), Variant::Type::BOOL, false); |
| 628 | _insert_feature("character_variant_53", HB_TAG('c', 'v', '5', '3'), Variant::Type::BOOL, false); |
| 629 | _insert_feature("character_variant_54", HB_TAG('c', 'v', '5', '4'), Variant::Type::BOOL, false); |
| 630 | _insert_feature("character_variant_55", HB_TAG('c', 'v', '5', '5'), Variant::Type::BOOL, false); |
| 631 | _insert_feature("character_variant_56", HB_TAG('c', 'v', '5', '6'), Variant::Type::BOOL, false); |
| 632 | _insert_feature("character_variant_57", HB_TAG('c', 'v', '5', '7'), Variant::Type::BOOL, false); |
| 633 | _insert_feature("character_variant_58", HB_TAG('c', 'v', '5', '8'), Variant::Type::BOOL, false); |
| 634 | _insert_feature("character_variant_59", HB_TAG('c', 'v', '5', '9'), Variant::Type::BOOL, false); |
| 635 | _insert_feature("character_variant_60", HB_TAG('c', 'v', '6', '0'), Variant::Type::BOOL, false); |
| 636 | _insert_feature("character_variant_61", HB_TAG('c', 'v', '6', '1'), Variant::Type::BOOL, false); |
| 637 | _insert_feature("character_variant_62", HB_TAG('c', 'v', '6', '2'), Variant::Type::BOOL, false); |
| 638 | _insert_feature("character_variant_63", HB_TAG('c', 'v', '6', '3'), Variant::Type::BOOL, false); |
| 639 | _insert_feature("character_variant_64", HB_TAG('c', 'v', '6', '4'), Variant::Type::BOOL, false); |
| 640 | _insert_feature("character_variant_65", HB_TAG('c', 'v', '6', '5'), Variant::Type::BOOL, false); |
| 641 | _insert_feature("character_variant_66", HB_TAG('c', 'v', '6', '6'), Variant::Type::BOOL, false); |
| 642 | _insert_feature("character_variant_67", HB_TAG('c', 'v', '6', '7'), Variant::Type::BOOL, false); |
| 643 | _insert_feature("character_variant_68", HB_TAG('c', 'v', '6', '8'), Variant::Type::BOOL, false); |
| 644 | _insert_feature("character_variant_69", HB_TAG('c', 'v', '6', '9'), Variant::Type::BOOL, false); |
| 645 | _insert_feature("character_variant_70", HB_TAG('c', 'v', '7', '0'), Variant::Type::BOOL, false); |
| 646 | _insert_feature("character_variant_71", HB_TAG('c', 'v', '7', '1'), Variant::Type::BOOL, false); |
| 647 | _insert_feature("character_variant_72", HB_TAG('c', 'v', '7', '2'), Variant::Type::BOOL, false); |
| 648 | _insert_feature("character_variant_73", HB_TAG('c', 'v', '7', '3'), Variant::Type::BOOL, false); |
| 649 | _insert_feature("character_variant_74", HB_TAG('c', 'v', '7', '4'), Variant::Type::BOOL, false); |
| 650 | _insert_feature("character_variant_75", HB_TAG('c', 'v', '7', '5'), Variant::Type::BOOL, false); |
| 651 | _insert_feature("character_variant_76", HB_TAG('c', 'v', '7', '6'), Variant::Type::BOOL, false); |
| 652 | _insert_feature("character_variant_77", HB_TAG('c', 'v', '7', '7'), Variant::Type::BOOL, false); |
| 653 | _insert_feature("character_variant_78", HB_TAG('c', 'v', '7', '8'), Variant::Type::BOOL, false); |
| 654 | _insert_feature("character_variant_79", HB_TAG('c', 'v', '7', '9'), Variant::Type::BOOL, false); |
| 655 | _insert_feature("character_variant_80", HB_TAG('c', 'v', '8', '0'), Variant::Type::BOOL, false); |
| 656 | _insert_feature("character_variant_81", HB_TAG('c', 'v', '8', '1'), Variant::Type::BOOL, false); |
| 657 | _insert_feature("character_variant_82", HB_TAG('c', 'v', '8', '2'), Variant::Type::BOOL, false); |
| 658 | _insert_feature("character_variant_83", HB_TAG('c', 'v', '8', '3'), Variant::Type::BOOL, false); |
| 659 | _insert_feature("character_variant_84", HB_TAG('c', 'v', '8', '4'), Variant::Type::BOOL, false); |
| 660 | _insert_feature("character_variant_85", HB_TAG('c', 'v', '8', '5'), Variant::Type::BOOL, false); |
| 661 | _insert_feature("character_variant_86", HB_TAG('c', 'v', '8', '6'), Variant::Type::BOOL, false); |
| 662 | _insert_feature("character_variant_87", HB_TAG('c', 'v', '8', '7'), Variant::Type::BOOL, false); |
| 663 | _insert_feature("character_variant_88", HB_TAG('c', 'v', '8', '8'), Variant::Type::BOOL, false); |
| 664 | _insert_feature("character_variant_89", HB_TAG('c', 'v', '8', '9'), Variant::Type::BOOL, false); |
| 665 | _insert_feature("character_variant_90", HB_TAG('c', 'v', '9', '0'), Variant::Type::BOOL, false); |
| 666 | _insert_feature("character_variant_91", HB_TAG('c', 'v', '9', '1'), Variant::Type::BOOL, false); |
| 667 | _insert_feature("character_variant_92", HB_TAG('c', 'v', '9', '2'), Variant::Type::BOOL, false); |
| 668 | _insert_feature("character_variant_93", HB_TAG('c', 'v', '9', '3'), Variant::Type::BOOL, false); |
| 669 | _insert_feature("character_variant_94", HB_TAG('c', 'v', '9', '4'), Variant::Type::BOOL, false); |
| 670 | _insert_feature("character_variant_95", HB_TAG('c', 'v', '9', '5'), Variant::Type::BOOL, false); |
| 671 | _insert_feature("character_variant_96", HB_TAG('c', 'v', '9', '6'), Variant::Type::BOOL, false); |
| 672 | _insert_feature("character_variant_97", HB_TAG('c', 'v', '9', '7'), Variant::Type::BOOL, false); |
| 673 | _insert_feature("character_variant_98", HB_TAG('c', 'v', '9', '8'), Variant::Type::BOOL, false); |
| 674 | _insert_feature("character_variant_99", HB_TAG('c', 'v', '9', '9'), Variant::Type::BOOL, false); |
| 675 | _insert_feature("petite_capitals_from_capitals", HB_TAG('c', '2', 'p', 'c'), Variant::Type::BOOL, false); |
| 676 | _insert_feature("small_capitals_from_capitals", HB_TAG('c', '2', 's', 'c'), Variant::Type::BOOL, false); |
| 677 | _insert_feature("distances", HB_TAG('d', 'i', 's', 't'), Variant::Type::INT, true); |
| 678 | _insert_feature("discretionary_ligatures", HB_TAG('d', 'l', 'i', 'g'), Variant::Type::BOOL, false); |
| 679 | _insert_feature("denominators", HB_TAG('d', 'n', 'o', 'm'), Variant::Type::BOOL, false); |
| 680 | _insert_feature("dotless_forms", HB_TAG('d', 't', 'l', 's'), Variant::Type::INT, true); |
| 681 | _insert_feature("expert_forms", HB_TAG('e', 'x', 'p', 't'), Variant::Type::BOOL, true); |
| 682 | _insert_feature("final_glyph_on_line_alternates", HB_TAG('f', 'a', 'l', 't'), Variant::Type::INT, false); |
| 683 | _insert_feature("terminal_forms_2", HB_TAG('f', 'i', 'n', '2'), Variant::Type::INT, true); |
| 684 | _insert_feature("terminal_forms_3", HB_TAG('f', 'i', 'n', '3'), Variant::Type::INT, true); |
| 685 | _insert_feature("terminal_forms", HB_TAG('f', 'i', 'n', 'a'), Variant::Type::INT, true); |
| 686 | _insert_feature("flattened_accent_forms", HB_TAG('f', 'l', 'a', 'c'), Variant::Type::INT, true); |
| 687 | _insert_feature("fractions", HB_TAG('f', 'r', 'a', 'c'), Variant::Type::BOOL, false); |
| 688 | _insert_feature("full_widths", HB_TAG('f', 'w', 'i', 'd'), Variant::Type::BOOL, false); |
| 689 | _insert_feature("half_forms", HB_TAG('h', 'a', 'l', 'f'), Variant::Type::INT, true); |
| 690 | _insert_feature("halant_forms", HB_TAG('h', 'a', 'l', 'n'), Variant::Type::INT, true); |
| 691 | _insert_feature("alternate_half_widths", HB_TAG('h', 'a', 'l', 't'), Variant::Type::BOOL, false); |
| 692 | _insert_feature("historical_forms", HB_TAG('h', 'i', 's', 't'), Variant::Type::INT, false); |
| 693 | _insert_feature("horizontal_kana_alternates", HB_TAG('h', 'k', 'n', 'a'), Variant::Type::BOOL, false); |
| 694 | _insert_feature("historical_ligatures", HB_TAG('h', 'l', 'i', 'g'), Variant::Type::BOOL, false); |
| 695 | _insert_feature("hangul", HB_TAG('h', 'n', 'g', 'l'), Variant::Type::INT, false); |
| 696 | _insert_feature("hojo_kanji_forms", HB_TAG('h', 'o', 'j', 'o'), Variant::Type::INT, false); |
| 697 | _insert_feature("half_widths", HB_TAG('h', 'w', 'i', 'd'), Variant::Type::BOOL, false); |
| 698 | _insert_feature("initial_forms", HB_TAG('i', 'n', 'i', 't'), Variant::Type::INT, true); |
| 699 | _insert_feature("isolated_forms", HB_TAG('i', 's', 'o', 'l'), Variant::Type::INT, true); |
| 700 | _insert_feature("italics", HB_TAG('i', 't', 'a', 'l'), Variant::Type::INT, false); |
| 701 | _insert_feature("justification_alternates", HB_TAG('j', 'a', 'l', 't'), Variant::Type::INT, false); |
| 702 | _insert_feature("jis78_forms", HB_TAG('j', 'p', '7', '8'), Variant::Type::INT, false); |
| 703 | _insert_feature("jis83_forms", HB_TAG('j', 'p', '8', '3'), Variant::Type::INT, false); |
| 704 | _insert_feature("jis90_forms", HB_TAG('j', 'p', '9', '0'), Variant::Type::INT, false); |
| 705 | _insert_feature("jis2004_forms", HB_TAG('j', 'p', '0', '4'), Variant::Type::INT, false); |
| 706 | _insert_feature("kerning", HB_TAG('k', 'e', 'r', 'n'), Variant::Type::BOOL, false); |
| 707 | _insert_feature("left_bounds", HB_TAG('l', 'f', 'b', 'd'), Variant::Type::INT, false); |
| 708 | _insert_feature("standard_ligatures", HB_TAG('l', 'i', 'g', 'a'), Variant::Type::BOOL, false); |
| 709 | _insert_feature("leading_jamo_forms", HB_TAG('l', 'j', 'm', 'o'), Variant::Type::INT, true); |
| 710 | _insert_feature("lining_figures", HB_TAG('l', 'n', 'u', 'm'), Variant::Type::INT, false); |
| 711 | _insert_feature("localized_forms", HB_TAG('l', 'o', 'c', 'l'), Variant::Type::INT, true); |
| 712 | _insert_feature("left_to_right_alternates", HB_TAG('l', 't', 'r', 'a'), Variant::Type::INT, true); |
| 713 | _insert_feature("left_to_right_mirrored_forms", HB_TAG('l', 't', 'r', 'm'), Variant::Type::INT, true); |
| 714 | _insert_feature("mark_positioning", HB_TAG('m', 'a', 'r', 'k'), Variant::Type::INT, true); |
| 715 | _insert_feature("medial_forms_2", HB_TAG('m', 'e', 'd', '2'), Variant::Type::INT, true); |
| 716 | _insert_feature("medial_forms", HB_TAG('m', 'e', 'd', 'i'), Variant::Type::INT, true); |
| 717 | _insert_feature("mathematical_greek", HB_TAG('m', 'g', 'r', 'k'), Variant::Type::BOOL, false); |
| 718 | _insert_feature("mark_to_mark_positioning", HB_TAG('m', 'k', 'm', 'k'), Variant::Type::INT, true); |
| 719 | _insert_feature("mark_positioning_via_substitution", HB_TAG('m', 's', 'e', 't'), Variant::Type::INT, true); |
| 720 | _insert_feature("alternate_annotation_forms", HB_TAG('n', 'a', 'l', 't'), Variant::Type::INT, false); |
| 721 | _insert_feature("nlc_kanji_forms", HB_TAG('n', 'l', 'c', 'k'), Variant::Type::INT, false); |
| 722 | _insert_feature("nukta_forms", HB_TAG('n', 'u', 'k', 't'), Variant::Type::INT, true); |
| 723 | _insert_feature("numerators", HB_TAG('n', 'u', 'm', 'r'), Variant::Type::BOOL, false); |
| 724 | _insert_feature("oldstyle_figures", HB_TAG('o', 'n', 'u', 'm'), Variant::Type::INT, false); |
| 725 | _insert_feature("optical_bounds", HB_TAG('o', 'p', 'b', 'd'), Variant::Type::INT, true); |
| 726 | _insert_feature("ordinals", HB_TAG('o', 'r', 'd', 'n'), Variant::Type::BOOL, false); |
| 727 | _insert_feature("ornaments", HB_TAG('o', 'r', 'n', 'm'), Variant::Type::INT, false); |
| 728 | _insert_feature("proportional_alternate_widths", HB_TAG('p', 'a', 'l', 't'), Variant::Type::BOOL, false); |
| 729 | _insert_feature("petite_capitals", HB_TAG('p', 'c', 'a', 'p'), Variant::Type::BOOL, false); |
| 730 | _insert_feature("proportional_kana", HB_TAG('p', 'k', 'n', 'a'), Variant::Type::BOOL, false); |
| 731 | _insert_feature("proportional_figures", HB_TAG('p', 'n', 'u', 'm'), Variant::Type::BOOL, false); |
| 732 | _insert_feature("pre_base_forms", HB_TAG('p', 'r', 'e', 'f'), Variant::Type::INT, true); |
| 733 | _insert_feature("pre_base_substitutions", HB_TAG('p', 'r', 'e', 's'), Variant::Type::INT, true); |
| 734 | _insert_feature("post_base_forms", HB_TAG('p', 's', 't', 'f'), Variant::Type::INT, true); |
| 735 | _insert_feature("post_base_substitutions", HB_TAG('p', 's', 't', 's'), Variant::Type::INT, true); |
| 736 | _insert_feature("proportional_widths", HB_TAG('p', 'w', 'i', 'd'), Variant::Type::BOOL, false); |
| 737 | _insert_feature("quarter_widths", HB_TAG('q', 'w', 'i', 'd'), Variant::Type::BOOL, false); |
| 738 | _insert_feature("randomize", HB_TAG('r', 'a', 'n', 'd'), Variant::Type::INT, false); |
| 739 | _insert_feature("required_contextual_alternates", HB_TAG('r', 'c', 'l', 't'), Variant::Type::BOOL, true); |
| 740 | _insert_feature("rakar_forms", HB_TAG('r', 'k', 'r', 'f'), Variant::Type::INT, true); |
| 741 | _insert_feature("required_ligatures", HB_TAG('r', 'l', 'i', 'g'), Variant::Type::BOOL, true); |
| 742 | _insert_feature("reph_forms", HB_TAG('r', 'p', 'h', 'f'), Variant::Type::INT, true); |
| 743 | _insert_feature("right_bounds", HB_TAG('r', 't', 'b', 'd'), Variant::Type::INT, false); |
| 744 | _insert_feature("right_to_left_alternates", HB_TAG('r', 't', 'l', 'a'), Variant::Type::INT, true); |
| 745 | _insert_feature("right_to_left_mirrored_forms", HB_TAG('r', 't', 'l', 'm'), Variant::Type::INT, true); |
| 746 | _insert_feature("ruby_notation_forms", HB_TAG('r', 'u', 'b', 'y'), Variant::Type::INT, false); |
| 747 | _insert_feature("required_variation_alternates", HB_TAG('r', 'v', 'r', 'n'), Variant::Type::INT, true); |
| 748 | _insert_feature("stylistic_alternates", HB_TAG('s', 'a', 'l', 't'), Variant::Type::INT, false); |
| 749 | _insert_feature("scientific_inferiors", HB_TAG('s', 'i', 'n', 'f'), Variant::Type::BOOL, false); |
| 750 | _insert_feature("optical_size", HB_TAG('s', 'i', 'z', 'e'), Variant::Type::INT, false); |
| 751 | _insert_feature("small_capitals", HB_TAG('s', 'm', 'c', 'p'), Variant::Type::BOOL, false); |
| 752 | _insert_feature("simplified_forms", HB_TAG('s', 'm', 'p', 'l'), Variant::Type::INT, false); |
| 753 | _insert_feature("stylistic_set_01", HB_TAG('s', 's', '0', '1'), Variant::Type::BOOL, false); |
| 754 | _insert_feature("stylistic_set_02", HB_TAG('s', 's', '0', '2'), Variant::Type::BOOL, false); |
| 755 | _insert_feature("stylistic_set_03", HB_TAG('s', 's', '0', '3'), Variant::Type::BOOL, false); |
| 756 | _insert_feature("stylistic_set_04", HB_TAG('s', 's', '0', '4'), Variant::Type::BOOL, false); |
| 757 | _insert_feature("stylistic_set_05", HB_TAG('s', 's', '0', '5'), Variant::Type::BOOL, false); |
| 758 | _insert_feature("stylistic_set_06", HB_TAG('s', 's', '0', '6'), Variant::Type::BOOL, false); |
| 759 | _insert_feature("stylistic_set_07", HB_TAG('s', 's', '0', '7'), Variant::Type::BOOL, false); |
| 760 | _insert_feature("stylistic_set_08", HB_TAG('s', 's', '0', '8'), Variant::Type::BOOL, false); |
| 761 | _insert_feature("stylistic_set_09", HB_TAG('s', 's', '0', '9'), Variant::Type::BOOL, false); |
| 762 | _insert_feature("stylistic_set_10", HB_TAG('s', 's', '1', '0'), Variant::Type::BOOL, false); |
| 763 | _insert_feature("stylistic_set_11", HB_TAG('s', 's', '1', '1'), Variant::Type::BOOL, false); |
| 764 | _insert_feature("stylistic_set_12", HB_TAG('s', 's', '1', '2'), Variant::Type::BOOL, false); |
| 765 | _insert_feature("stylistic_set_13", HB_TAG('s', 's', '1', '3'), Variant::Type::BOOL, false); |
| 766 | _insert_feature("stylistic_set_14", HB_TAG('s', 's', '1', '4'), Variant::Type::BOOL, false); |
| 767 | _insert_feature("stylistic_set_15", HB_TAG('s', 's', '1', '5'), Variant::Type::BOOL, false); |
| 768 | _insert_feature("stylistic_set_16", HB_TAG('s', 's', '1', '6'), Variant::Type::BOOL, false); |
| 769 | _insert_feature("stylistic_set_17", HB_TAG('s', 's', '1', '7'), Variant::Type::BOOL, false); |
| 770 | _insert_feature("stylistic_set_18", HB_TAG('s', 's', '1', '8'), Variant::Type::BOOL, false); |
| 771 | _insert_feature("stylistic_set_19", HB_TAG('s', 's', '1', '9'), Variant::Type::BOOL, false); |
| 772 | _insert_feature("stylistic_set_20", HB_TAG('s', 's', '2', '0'), Variant::Type::BOOL, false); |
| 773 | _insert_feature("math_script_style_alternates", HB_TAG('s', 's', 't', 'y'), Variant::Type::INT, true); |
| 774 | _insert_feature("stretching_glyph_decomposition", HB_TAG('s', 't', 'c', 'h'), Variant::Type::INT, true); |
| 775 | _insert_feature("subscript", HB_TAG('s', 'u', 'b', 's'), Variant::Type::BOOL, false); |
| 776 | _insert_feature("superscript", HB_TAG('s', 'u', 'p', 's'), Variant::Type::BOOL, false); |
| 777 | _insert_feature("swash", HB_TAG('s', 'w', 's', 'h'), Variant::Type::INT, false); |
| 778 | _insert_feature("titling", HB_TAG('t', 'i', 't', 'l'), Variant::Type::BOOL, false); |
| 779 | _insert_feature("trailing_jamo_forms", HB_TAG('t', 'j', 'm', 'o'), Variant::Type::INT, true); |
| 780 | _insert_feature("traditional_name_forms", HB_TAG('t', 'n', 'a', 'm'), Variant::Type::INT, false); |
| 781 | _insert_feature("tabular_figures", HB_TAG('t', 'n', 'u', 'm'), Variant::Type::BOOL, false); |
| 782 | _insert_feature("traditional_forms", HB_TAG('t', 'r', 'a', 'd'), Variant::Type::INT, false); |
| 783 | _insert_feature("third_widths", HB_TAG('t', 'w', 'i', 'd'), Variant::Type::BOOL, false); |
| 784 | _insert_feature("unicase", HB_TAG('u', 'n', 'i', 'c'), Variant::Type::BOOL, false); |
| 785 | _insert_feature("alternate_vertical_metrics", HB_TAG('v', 'a', 'l', 't'), Variant::Type::INT, false); |
| 786 | _insert_feature("vattu_variants", HB_TAG('v', 'a', 't', 'u'), Variant::Type::INT, true); |
| 787 | _insert_feature("vertical_contextual_half_width_spacing", HB_TAG('v', 'c', 'h', 'w'), Variant::Type::BOOL, false); |
| 788 | _insert_feature("vertical_alternates", HB_TAG('v', 'e', 'r', 't'), Variant::Type::INT, false); |
| 789 | _insert_feature("alternate_vertical_half_metrics", HB_TAG('v', 'h', 'a', 'l'), Variant::Type::BOOL, false); |
| 790 | _insert_feature("vowel_jamo_forms", HB_TAG('v', 'j', 'm', 'o'), Variant::Type::INT, true); |
| 791 | _insert_feature("vertical_kana_alternates", HB_TAG('v', 'k', 'n', 'a'), Variant::Type::INT, false); |
| 792 | _insert_feature("vertical_kerning", HB_TAG('v', 'k', 'r', 'n'), Variant::Type::BOOL, false); |
| 793 | _insert_feature("proportional_alternate_vertical_metrics", HB_TAG('v', 'p', 'a', 'l'), Variant::Type::BOOL, false); |
| 794 | _insert_feature("vertical_alternates_and_rotation", HB_TAG('v', 'r', 't', '2'), Variant::Type::INT, false); |
| 795 | _insert_feature("vertical_alternates_for_rotation", HB_TAG('v', 'r', 't', 'r'), Variant::Type::INT, false); |
| 796 | _insert_feature("slashed_zero", HB_TAG('z', 'e', 'r', 'o'), Variant::Type::BOOL, false); |
| 797 | |
| 798 | // Registered OpenType variation tag. |
| 799 | _insert_feature("italic", HB_TAG('i', 't', 'a', 'l'), Variant::Type::INT, false); |
| 800 | _insert_feature("optical_size", HB_TAG('o', 'p', 's', 'z'), Variant::Type::INT, false); |
| 801 | _insert_feature("slant", HB_TAG('s', 'l', 'n', 't'), Variant::Type::INT, false); |
| 802 | _insert_feature("width", HB_TAG('w', 'd', 't', 'h'), Variant::Type::INT, false); |
| 803 | _insert_feature("weight", HB_TAG('w', 'g', 'h', 't'), Variant::Type::INT, false); |
| 804 | } |
| 805 | |
| 806 | int64_t TextServerAdvanced::_name_to_tag(const String &p_name) const { |
| 807 | if (feature_sets.has(p_name)) { |
| 808 | return feature_sets[p_name]; |
| 809 | } |
| 810 | |
| 811 | // No readable name, use tag string. |
| 812 | return hb_tag_from_string(p_name.replace("custom_", "").ascii().get_data(), -1); |
| 813 | } |
| 814 | |
| 815 | Variant::Type TextServerAdvanced::_get_tag_type(int64_t p_tag) const { |
| 816 | if (feature_sets_inv.has(p_tag)) { |
| 817 | return feature_sets_inv[p_tag].vtype; |
| 818 | } |
| 819 | return Variant::Type::INT; |
| 820 | } |
| 821 | |
| 822 | bool TextServerAdvanced::_get_tag_hidden(int64_t p_tag) const { |
| 823 | if (feature_sets_inv.has(p_tag)) { |
| 824 | return feature_sets_inv[p_tag].hidden; |
| 825 | } |
| 826 | return false; |
| 827 | } |
| 828 | |
| 829 | String TextServerAdvanced::_tag_to_name(int64_t p_tag) const { |
| 830 | if (feature_sets_inv.has(p_tag)) { |
| 831 | return feature_sets_inv[p_tag].name; |
| 832 | } |
| 833 | |
| 834 | // No readable name, use tag string. |
| 835 | char name[5]; |
| 836 | memset(name, 0, 5); |
| 837 | hb_tag_to_string(p_tag, name); |
| 838 | return String("custom_") + String(name); |
| 839 | } |
| 840 | |
| 841 | /*************************************************************************/ |
| 842 | /* Font Glyph Rendering */ |
| 843 | /*************************************************************************/ |
| 844 | |
| 845 | _FORCE_INLINE_ TextServerAdvanced::FontTexturePosition TextServerAdvanced::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 { |
| 846 | FontTexturePosition ret; |
| 847 | |
| 848 | int mw = p_width; |
| 849 | int mh = p_height; |
| 850 | |
| 851 | ShelfPackTexture *ct = p_data->textures.ptrw(); |
| 852 | for (int32_t i = 0; i < p_data->textures.size(); i++) { |
| 853 | if (ct[i].image.is_null()) { |
| 854 | continue; |
| 855 | } |
| 856 | if (p_image_format != ct[i].image->get_format()) { |
| 857 | continue; |
| 858 | } |
| 859 | if (mw > ct[i].texture_w || mh > ct[i].texture_h) { // Too big for this texture. |
| 860 | continue; |
| 861 | } |
| 862 | |
| 863 | ret = ct[i].pack_rect(i, mh, mw); |
| 864 | if (ret.index != -1) { |
| 865 | break; |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | if (ret.index == -1) { |
| 870 | // Could not find texture to fit, create one. |
| 871 | int texsize = MAX(p_data->size.x * 0.125, 256); |
| 872 | |
| 873 | texsize = next_power_of_2((uint32_t)texsize); |
| 874 | if (p_msdf) { |
| 875 | texsize = MIN(texsize, 2048); |
| 876 | } else { |
| 877 | texsize = MIN(texsize, 1024); |
| 878 | } |
| 879 | if (mw > texsize) { // Special case, adapt to it? |
| 880 | texsize = next_power_of_2((uint32_t)mw); |
| 881 | } |
| 882 | if (mh > texsize) { // Special case, adapt to it? |
| 883 | texsize = next_power_of_2((uint32_t)mh); |
| 884 | } |
| 885 | |
| 886 | ShelfPackTexture tex = ShelfPackTexture(texsize, texsize); |
| 887 | tex.image = Image::create_empty(texsize, texsize, false, p_image_format); |
| 888 | { |
| 889 | // Zero texture. |
| 890 | uint8_t *w = tex.image->ptrw(); |
| 891 | ERR_FAIL_COND_V(texsize * texsize * p_color_size > tex.image->get_data_size(), ret); |
| 892 | // Initialize the texture to all-white pixels to prevent artifacts when the |
| 893 | // font is displayed at a non-default scale with filtering enabled. |
| 894 | if (p_color_size == 2) { |
| 895 | for (int i = 0; i < texsize * texsize * p_color_size; i += 2) { // FORMAT_LA8, BW font. |
| 896 | w[i + 0] = 255; |
| 897 | w[i + 1] = 0; |
| 898 | } |
| 899 | } else if (p_color_size == 4) { |
| 900 | for (int i = 0; i < texsize * texsize * p_color_size; i += 4) { // FORMAT_RGBA8, Color font, Multichannel(+True) SDF. |
| 901 | if (p_msdf) { |
| 902 | w[i + 0] = 0; |
| 903 | w[i + 1] = 0; |
| 904 | w[i + 2] = 0; |
| 905 | } else { |
| 906 | w[i + 0] = 255; |
| 907 | w[i + 1] = 255; |
| 908 | w[i + 2] = 255; |
| 909 | } |
| 910 | w[i + 3] = 0; |
| 911 | } |
| 912 | } else { |
| 913 | ERR_FAIL_V(ret); |
| 914 | } |
| 915 | } |
| 916 | p_data->textures.push_back(tex); |
| 917 | |
| 918 | int32_t idx = p_data->textures.size() - 1; |
| 919 | ret = p_data->textures.write[idx].pack_rect(idx, mh, mw); |
| 920 | } |
| 921 | |
| 922 | return ret; |
| 923 | } |
| 924 | |
| 925 | #ifdef MODULE_MSDFGEN_ENABLED |
| 926 | |
| 927 | struct MSContext { |
| 928 | msdfgen::Point2 position; |
| 929 | msdfgen::Shape *shape = nullptr; |
| 930 | msdfgen::Contour *contour = nullptr; |
| 931 | }; |
| 932 | |
| 933 | class DistancePixelConversion { |
| 934 | double invRange; |
| 935 | |
| 936 | public: |
| 937 | _FORCE_INLINE_ explicit DistancePixelConversion(double range) : |
| 938 | invRange(1 / range) {} |
| 939 | _FORCE_INLINE_ void operator()(float *pixels, const msdfgen::MultiAndTrueDistance &distance) const { |
| 940 | pixels[0] = float(invRange * distance.r + .5); |
| 941 | pixels[1] = float(invRange * distance.g + .5); |
| 942 | pixels[2] = float(invRange * distance.b + .5); |
| 943 | pixels[3] = float(invRange * distance.a + .5); |
| 944 | } |
| 945 | }; |
| 946 | |
| 947 | struct MSDFThreadData { |
| 948 | msdfgen::Bitmap<float, 4> *output; |
| 949 | msdfgen::Shape *shape; |
| 950 | msdfgen::Projection *projection; |
| 951 | DistancePixelConversion *distancePixelConversion; |
| 952 | }; |
| 953 | |
| 954 | static msdfgen::Point2 ft_point2(const FT_Vector &vector) { |
| 955 | return msdfgen::Point2(vector.x / 60.0f, vector.y / 60.0f); |
| 956 | } |
| 957 | |
| 958 | static int ft_move_to(const FT_Vector *to, void *user) { |
| 959 | MSContext *context = static_cast<MSContext *>(user); |
| 960 | if (!(context->contour && context->contour->edges.empty())) { |
| 961 | context->contour = &context->shape->addContour(); |
| 962 | } |
| 963 | context->position = ft_point2(*to); |
| 964 | return 0; |
| 965 | } |
| 966 | |
| 967 | static int ft_line_to(const FT_Vector *to, void *user) { |
| 968 | MSContext *context = static_cast<MSContext *>(user); |
| 969 | msdfgen::Point2 endpoint = ft_point2(*to); |
| 970 | if (endpoint != context->position) { |
| 971 | context->contour->addEdge(new msdfgen::LinearSegment(context->position, endpoint)); |
| 972 | context->position = endpoint; |
| 973 | } |
| 974 | return 0; |
| 975 | } |
| 976 | |
| 977 | static int ft_conic_to(const FT_Vector *control, const FT_Vector *to, void *user) { |
| 978 | MSContext *context = static_cast<MSContext *>(user); |
| 979 | context->contour->addEdge(new msdfgen::QuadraticSegment(context->position, ft_point2(*control), ft_point2(*to))); |
| 980 | context->position = ft_point2(*to); |
| 981 | return 0; |
| 982 | } |
| 983 | |
| 984 | static int ft_cubic_to(const FT_Vector *control1, const FT_Vector *control2, const FT_Vector *to, void *user) { |
| 985 | MSContext *context = static_cast<MSContext *>(user); |
| 986 | context->contour->addEdge(new msdfgen::CubicSegment(context->position, ft_point2(*control1), ft_point2(*control2), ft_point2(*to))); |
| 987 | context->position = ft_point2(*to); |
| 988 | return 0; |
| 989 | } |
| 990 | |
| 991 | void TextServerAdvanced::_generateMTSDF_threaded(void *p_td, uint32_t p_y) { |
| 992 | MSDFThreadData *td = static_cast<MSDFThreadData *>(p_td); |
| 993 | |
| 994 | msdfgen::ShapeDistanceFinder<msdfgen::OverlappingContourCombiner<msdfgen::MultiAndTrueDistanceSelector>> distanceFinder(*td->shape); |
| 995 | int row = td->shape->inverseYAxis ? td->output->height() - p_y - 1 : p_y; |
| 996 | for (int col = 0; col < td->output->width(); ++col) { |
| 997 | int x = (p_y % 2) ? td->output->width() - col - 1 : col; |
| 998 | msdfgen::Point2 p = td->projection->unproject(msdfgen::Point2(x + .5, p_y + .5)); |
| 999 | msdfgen::MultiAndTrueDistance distance = distanceFinder.distance(p); |
| 1000 | td->distancePixelConversion->operator()(td->output->operator()(x, row), distance); |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | _FORCE_INLINE_ TextServerAdvanced::FontGlyph TextServerAdvanced::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 { |
| 1005 | msdfgen::Shape shape; |
| 1006 | |
| 1007 | shape.contours.clear(); |
| 1008 | shape.inverseYAxis = false; |
| 1009 | |
| 1010 | MSContext context = {}; |
| 1011 | context.shape = &shape; |
| 1012 | FT_Outline_Funcs ft_functions; |
| 1013 | ft_functions.move_to = &ft_move_to; |
| 1014 | ft_functions.line_to = &ft_line_to; |
| 1015 | ft_functions.conic_to = &ft_conic_to; |
| 1016 | ft_functions.cubic_to = &ft_cubic_to; |
| 1017 | ft_functions.shift = 0; |
| 1018 | ft_functions.delta = 0; |
| 1019 | |
| 1020 | int error = FT_Outline_Decompose(p_outline, &ft_functions, &context); |
| 1021 | ERR_FAIL_COND_V_MSG(error, FontGlyph(), "FreeType: Outline decomposition error: '" + String(FT_Error_String(error)) + "'."); |
| 1022 | if (!shape.contours.empty() && shape.contours.back().edges.empty()) { |
| 1023 | shape.contours.pop_back(); |
| 1024 | } |
| 1025 | |
| 1026 | if (FT_Outline_Get_Orientation(p_outline) == 1) { |
| 1027 | for (int i = 0; i < (int)shape.contours.size(); ++i) { |
| 1028 | shape.contours[i].reverse(); |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | shape.inverseYAxis = true; |
| 1033 | shape.normalize(); |
| 1034 | |
| 1035 | msdfgen::Shape::Bounds bounds = shape.getBounds(p_pixel_range); |
| 1036 | |
| 1037 | FontGlyph chr; |
| 1038 | chr.found = true; |
| 1039 | chr.advance = p_advance; |
| 1040 | |
| 1041 | if (shape.validate() && shape.contours.size() > 0) { |
| 1042 | int w = (bounds.r - bounds.l); |
| 1043 | int h = (bounds.t - bounds.b); |
| 1044 | |
| 1045 | if (w == 0 || h == 0) { |
| 1046 | chr.texture_idx = -1; |
| 1047 | chr.uv_rect = Rect2(); |
| 1048 | chr.rect = Rect2(); |
| 1049 | return chr; |
| 1050 | } |
| 1051 | |
| 1052 | int mw = w + p_rect_margin * 4; |
| 1053 | int mh = h + p_rect_margin * 4; |
| 1054 | |
| 1055 | ERR_FAIL_COND_V(mw > 4096, FontGlyph()); |
| 1056 | ERR_FAIL_COND_V(mh > 4096, FontGlyph()); |
| 1057 | |
| 1058 | FontTexturePosition tex_pos = find_texture_pos_for_glyph(p_data, 4, Image::FORMAT_RGBA8, mw, mh, true); |
| 1059 | ERR_FAIL_COND_V(tex_pos.index < 0, FontGlyph()); |
| 1060 | ShelfPackTexture &tex = p_data->textures.write[tex_pos.index]; |
| 1061 | |
| 1062 | edgeColoringSimple(shape, 3.0); // Max. angle. |
| 1063 | msdfgen::Bitmap<float, 4> image(w, h); // Texture size. |
| 1064 | |
| 1065 | DistancePixelConversion distancePixelConversion(p_pixel_range); |
| 1066 | msdfgen::Projection projection(msdfgen::Vector2(1.0, 1.0), msdfgen::Vector2(-bounds.l, -bounds.b)); |
| 1067 | msdfgen::MSDFGeneratorConfig config(true, msdfgen::ErrorCorrectionConfig()); |
| 1068 | |
| 1069 | MSDFThreadData td; |
| 1070 | td.output = ℑ |
| 1071 | td.shape = &shape; |
| 1072 | td.projection = &projection; |
| 1073 | td.distancePixelConversion = &distancePixelConversion; |
| 1074 | |
| 1075 | WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_native_group_task(&TextServerAdvanced::_generateMTSDF_threaded, &td, h, -1, true, String("FontServerRasterizeMSDF")); |
| 1076 | WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task); |
| 1077 | |
| 1078 | msdfgen::msdfErrorCorrection(image, shape, projection, p_pixel_range, config); |
| 1079 | |
| 1080 | { |
| 1081 | uint8_t *wr = tex.image->ptrw(); |
| 1082 | |
| 1083 | for (int i = 0; i < h; i++) { |
| 1084 | for (int j = 0; j < w; j++) { |
| 1085 | int ofs = ((i + tex_pos.y + p_rect_margin * 2) * tex.texture_w + j + tex_pos.x + p_rect_margin * 2) * 4; |
| 1086 | ERR_FAIL_COND_V(ofs >= tex.image->get_data_size(), FontGlyph()); |
| 1087 | wr[ofs + 0] = (uint8_t)(CLAMP(image(j, i)[0] * 256.f, 0.f, 255.f)); |
| 1088 | wr[ofs + 1] = (uint8_t)(CLAMP(image(j, i)[1] * 256.f, 0.f, 255.f)); |
| 1089 | wr[ofs + 2] = (uint8_t)(CLAMP(image(j, i)[2] * 256.f, 0.f, 255.f)); |
| 1090 | wr[ofs + 3] = (uint8_t)(CLAMP(image(j, i)[3] * 256.f, 0.f, 255.f)); |
| 1091 | } |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | tex.dirty = true; |
| 1096 | |
| 1097 | chr.texture_idx = tex_pos.index; |
| 1098 | |
| 1099 | chr.uv_rect = Rect2(tex_pos.x + p_rect_margin, tex_pos.y + p_rect_margin, w + p_rect_margin * 2, h + p_rect_margin * 2); |
| 1100 | chr.rect.position = Vector2(bounds.l - p_rect_margin, -bounds.t - p_rect_margin); |
| 1101 | |
| 1102 | chr.rect.size = chr.uv_rect.size; |
| 1103 | } |
| 1104 | return chr; |
| 1105 | } |
| 1106 | #endif |
| 1107 | |
| 1108 | #ifdef MODULE_FREETYPE_ENABLED |
| 1109 | _FORCE_INLINE_ TextServerAdvanced::FontGlyph TextServerAdvanced::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 { |
| 1110 | FontGlyph chr; |
| 1111 | chr.advance = p_advance * p_data->scale; |
| 1112 | chr.found = true; |
| 1113 | |
| 1114 | int w = p_bitmap.width; |
| 1115 | int h = p_bitmap.rows; |
| 1116 | |
| 1117 | if (w == 0 || h == 0) { |
| 1118 | chr.texture_idx = -1; |
| 1119 | chr.uv_rect = Rect2(); |
| 1120 | chr.rect = Rect2(); |
| 1121 | return chr; |
| 1122 | } |
| 1123 | |
| 1124 | int color_size = 2; |
| 1125 | |
| 1126 | switch (p_bitmap.pixel_mode) { |
| 1127 | case FT_PIXEL_MODE_MONO: |
| 1128 | case FT_PIXEL_MODE_GRAY: { |
| 1129 | color_size = 2; |
| 1130 | } break; |
| 1131 | case FT_PIXEL_MODE_BGRA: { |
| 1132 | color_size = 4; |
| 1133 | } break; |
| 1134 | case FT_PIXEL_MODE_LCD: { |
| 1135 | color_size = 4; |
| 1136 | w /= 3; |
| 1137 | } break; |
| 1138 | case FT_PIXEL_MODE_LCD_V: { |
| 1139 | color_size = 4; |
| 1140 | h /= 3; |
| 1141 | } break; |
| 1142 | } |
| 1143 | |
| 1144 | int mw = w + p_rect_margin * 4; |
| 1145 | int mh = h + p_rect_margin * 4; |
| 1146 | |
| 1147 | ERR_FAIL_COND_V(mw > 4096, FontGlyph()); |
| 1148 | ERR_FAIL_COND_V(mh > 4096, FontGlyph()); |
| 1149 | |
| 1150 | Image::Format require_format = color_size == 4 ? Image::FORMAT_RGBA8 : Image::FORMAT_LA8; |
| 1151 | |
| 1152 | FontTexturePosition tex_pos = find_texture_pos_for_glyph(p_data, color_size, require_format, mw, mh, false); |
| 1153 | ERR_FAIL_COND_V(tex_pos.index < 0, FontGlyph()); |
| 1154 | |
| 1155 | // Fit character in char texture. |
| 1156 | ShelfPackTexture &tex = p_data->textures.write[tex_pos.index]; |
| 1157 | |
| 1158 | { |
| 1159 | uint8_t *wr = tex.image->ptrw(); |
| 1160 | |
| 1161 | for (int i = 0; i < h; i++) { |
| 1162 | for (int j = 0; j < w; j++) { |
| 1163 | int ofs = ((i + tex_pos.y + p_rect_margin * 2) * tex.texture_w + j + tex_pos.x + p_rect_margin * 2) * color_size; |
| 1164 | ERR_FAIL_COND_V(ofs >= tex.image->get_data_size(), FontGlyph()); |
| 1165 | switch (p_bitmap.pixel_mode) { |
| 1166 | case FT_PIXEL_MODE_MONO: { |
| 1167 | int byte = i * p_bitmap.pitch + (j >> 3); |
| 1168 | int bit = 1 << (7 - (j % 8)); |
| 1169 | wr[ofs + 0] = 255; // grayscale as 1 |
| 1170 | wr[ofs + 1] = (p_bitmap.buffer[byte] & bit) ? 255 : 0; |
| 1171 | } break; |
| 1172 | case FT_PIXEL_MODE_GRAY: |
| 1173 | wr[ofs + 0] = 255; // grayscale as 1 |
| 1174 | wr[ofs + 1] = p_bitmap.buffer[i * p_bitmap.pitch + j]; |
| 1175 | break; |
| 1176 | case FT_PIXEL_MODE_BGRA: { |
| 1177 | int ofs_color = i * p_bitmap.pitch + (j << 2); |
| 1178 | wr[ofs + 2] = p_bitmap.buffer[ofs_color + 0]; |
| 1179 | wr[ofs + 1] = p_bitmap.buffer[ofs_color + 1]; |
| 1180 | wr[ofs + 0] = p_bitmap.buffer[ofs_color + 2]; |
| 1181 | wr[ofs + 3] = p_bitmap.buffer[ofs_color + 3]; |
| 1182 | } break; |
| 1183 | case FT_PIXEL_MODE_LCD: { |
| 1184 | int ofs_color = i * p_bitmap.pitch + (j * 3); |
| 1185 | if (p_bgra) { |
| 1186 | wr[ofs + 0] = p_bitmap.buffer[ofs_color + 2]; |
| 1187 | wr[ofs + 1] = p_bitmap.buffer[ofs_color + 1]; |
| 1188 | wr[ofs + 2] = p_bitmap.buffer[ofs_color + 0]; |
| 1189 | wr[ofs + 3] = 255; |
| 1190 | } else { |
| 1191 | wr[ofs + 0] = p_bitmap.buffer[ofs_color + 0]; |
| 1192 | wr[ofs + 1] = p_bitmap.buffer[ofs_color + 1]; |
| 1193 | wr[ofs + 2] = p_bitmap.buffer[ofs_color + 2]; |
| 1194 | wr[ofs + 3] = 255; |
| 1195 | } |
| 1196 | } break; |
| 1197 | case FT_PIXEL_MODE_LCD_V: { |
| 1198 | int ofs_color = i * p_bitmap.pitch * 3 + j; |
| 1199 | if (p_bgra) { |
| 1200 | wr[ofs + 0] = p_bitmap.buffer[ofs_color + p_bitmap.pitch * 2]; |
| 1201 | wr[ofs + 1] = p_bitmap.buffer[ofs_color + p_bitmap.pitch]; |
| 1202 | wr[ofs + 2] = p_bitmap.buffer[ofs_color + 0]; |
| 1203 | wr[ofs + 3] = 255; |
| 1204 | } else { |
| 1205 | wr[ofs + 0] = p_bitmap.buffer[ofs_color + 0]; |
| 1206 | wr[ofs + 1] = p_bitmap.buffer[ofs_color + p_bitmap.pitch]; |
| 1207 | wr[ofs + 2] = p_bitmap.buffer[ofs_color + p_bitmap.pitch * 2]; |
| 1208 | wr[ofs + 3] = 255; |
| 1209 | } |
| 1210 | } break; |
| 1211 | default: |
| 1212 | ERR_FAIL_V_MSG(FontGlyph(), "Font uses unsupported pixel format: " + String::num_int64(p_bitmap.pixel_mode) + "."); |
| 1213 | break; |
| 1214 | } |
| 1215 | } |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | tex.dirty = true; |
| 1220 | |
| 1221 | chr.texture_idx = tex_pos.index; |
| 1222 | |
| 1223 | chr.uv_rect = Rect2(tex_pos.x + p_rect_margin, tex_pos.y + p_rect_margin, w + p_rect_margin * 2, h + p_rect_margin * 2); |
| 1224 | chr.rect.position = Vector2(p_xofs - p_rect_margin, -p_yofs - p_rect_margin) * p_data->scale; |
| 1225 | chr.rect.size = chr.uv_rect.size * p_data->scale; |
| 1226 | return chr; |
| 1227 | } |
| 1228 | #endif |
| 1229 | |
| 1230 | /*************************************************************************/ |
| 1231 | /* Font Cache */ |
| 1232 | /*************************************************************************/ |
| 1233 | |
| 1234 | bool TextServerAdvanced::_ensure_glyph(FontAdvanced *p_font_data, const Vector2i &p_size, int32_t p_glyph, FontGlyph &r_glyph, uint32_t p_oversampling) const { |
| 1235 | FontForSizeAdvanced *fd = nullptr; |
| 1236 | ERR_FAIL_COND_V(!_ensure_cache_for_size(p_font_data, p_size, fd, false, p_oversampling), false); |
| 1237 | |
| 1238 | int32_t glyph_index = p_glyph & 0xffffff; // Remove subpixel shifts. |
| 1239 | |
| 1240 | HashMap<int32_t, FontGlyph>::Iterator E = fd->glyph_map.find(p_glyph); |
| 1241 | if (E) { |
| 1242 | bool tx_valid = true; |
| 1243 | if (E->value.texture_idx >= 0) { |
| 1244 | if (E->value.texture_idx < fd->textures.size()) { |
| 1245 | tx_valid = fd->textures[E->value.texture_idx].image.is_valid(); |
| 1246 | } else { |
| 1247 | tx_valid = false; |
| 1248 | } |
| 1249 | } |
| 1250 | if (tx_valid) { |
| 1251 | r_glyph = E->value; |
| 1252 | return E->value.found; |
| 1253 | #ifdef DEBUG_ENABLED |
| 1254 | } else { |
| 1255 | WARN_PRINT(vformat("Invalid texture cache for glyph %x in font %s, glyph will be re-rendered. Re-import this font to regenerate textures.", glyph_index, p_font_data->font_name)); |
| 1256 | #endif |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | if (glyph_index == 0) { // Non graphical or invalid glyph, do not render. |
| 1261 | E = fd->glyph_map.insert(p_glyph, FontGlyph()); |
| 1262 | r_glyph = E->value; |
| 1263 | return true; |
| 1264 | } |
| 1265 | |
| 1266 | #ifdef MODULE_FREETYPE_ENABLED |
| 1267 | FontGlyph gl; |
| 1268 | if (fd->face) { |
| 1269 | FT_Int32 flags = FT_LOAD_DEFAULT; |
| 1270 | |
| 1271 | bool outline = p_size.y > 0; |
| 1272 | switch (p_font_data->hinting) { |
| 1273 | case TextServer::HINTING_NONE: |
| 1274 | flags |= FT_LOAD_NO_HINTING; |
| 1275 | break; |
| 1276 | case TextServer::HINTING_LIGHT: |
| 1277 | flags |= FT_LOAD_TARGET_LIGHT; |
| 1278 | break; |
| 1279 | default: |
| 1280 | flags |= FT_LOAD_TARGET_NORMAL; |
| 1281 | break; |
| 1282 | } |
| 1283 | if (p_font_data->force_autohinter) { |
| 1284 | flags |= FT_LOAD_FORCE_AUTOHINT; |
| 1285 | } |
| 1286 | if (outline || (p_font_data->disable_embedded_bitmaps && !FT_HAS_COLOR(fd->face))) { |
| 1287 | flags |= FT_LOAD_NO_BITMAP; |
| 1288 | } else if (FT_HAS_COLOR(fd->face)) { |
| 1289 | flags |= FT_LOAD_COLOR; |
| 1290 | } |
| 1291 | |
| 1292 | FT_Fixed v, h; |
| 1293 | FT_Get_Advance(fd->face, glyph_index, flags, &h); |
| 1294 | FT_Get_Advance(fd->face, glyph_index, flags | FT_LOAD_VERTICAL_LAYOUT, &v); |
| 1295 | |
| 1296 | int error = FT_Load_Glyph(fd->face, glyph_index, flags); |
| 1297 | if (error) { |
| 1298 | E = fd->glyph_map.insert(p_glyph, FontGlyph()); |
| 1299 | r_glyph = E->value; |
| 1300 | return false; |
| 1301 | } |
| 1302 | |
| 1303 | if (!p_font_data->msdf) { |
| 1304 | if ((p_font_data->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (p_font_data->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && p_size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE * 64)) { |
| 1305 | FT_Pos xshift = (int)((p_glyph >> 27) & 3) << 4; |
| 1306 | FT_Outline_Translate(&fd->face->glyph->outline, xshift, 0); |
| 1307 | } else if ((p_font_data->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (p_font_data->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && p_size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE * 64)) { |
| 1308 | FT_Pos xshift = (int)((p_glyph >> 27) & 3) << 5; |
| 1309 | FT_Outline_Translate(&fd->face->glyph->outline, xshift, 0); |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | if (p_font_data->embolden != 0.f) { |
| 1314 | FT_Pos strength = p_font_data->embolden * p_size.x / 16; // 26.6 fractional units (1 / 64). |
| 1315 | FT_Outline_Embolden(&fd->face->glyph->outline, strength); |
| 1316 | } |
| 1317 | |
| 1318 | if (p_font_data->transform != Transform2D()) { |
| 1319 | FT_Matrix mat = { FT_Fixed(p_font_data->transform[0][0] * 65536), FT_Fixed(p_font_data->transform[0][1] * 65536), FT_Fixed(p_font_data->transform[1][0] * 65536), FT_Fixed(p_font_data->transform[1][1] * 65536) }; // 16.16 fractional units (1 / 65536). |
| 1320 | FT_Outline_Transform(&fd->face->glyph->outline, &mat); |
| 1321 | } |
| 1322 | |
| 1323 | FT_Render_Mode aa_mode = FT_RENDER_MODE_NORMAL; |
| 1324 | bool bgra = false; |
| 1325 | switch (p_font_data->antialiasing) { |
| 1326 | case FONT_ANTIALIASING_NONE: { |
| 1327 | aa_mode = FT_RENDER_MODE_MONO; |
| 1328 | } break; |
| 1329 | case FONT_ANTIALIASING_GRAY: { |
| 1330 | aa_mode = FT_RENDER_MODE_NORMAL; |
| 1331 | } break; |
| 1332 | case FONT_ANTIALIASING_LCD: { |
| 1333 | int aa_layout = (int)((p_glyph >> 24) & 7); |
| 1334 | switch (aa_layout) { |
| 1335 | case FONT_LCD_SUBPIXEL_LAYOUT_HRGB: { |
| 1336 | aa_mode = FT_RENDER_MODE_LCD; |
| 1337 | bgra = false; |
| 1338 | } break; |
| 1339 | case FONT_LCD_SUBPIXEL_LAYOUT_HBGR: { |
| 1340 | aa_mode = FT_RENDER_MODE_LCD; |
| 1341 | bgra = true; |
| 1342 | } break; |
| 1343 | case FONT_LCD_SUBPIXEL_LAYOUT_VRGB: { |
| 1344 | aa_mode = FT_RENDER_MODE_LCD_V; |
| 1345 | bgra = false; |
| 1346 | } break; |
| 1347 | case FONT_LCD_SUBPIXEL_LAYOUT_VBGR: { |
| 1348 | aa_mode = FT_RENDER_MODE_LCD_V; |
| 1349 | bgra = true; |
| 1350 | } break; |
| 1351 | default: { |
| 1352 | aa_mode = FT_RENDER_MODE_NORMAL; |
| 1353 | } break; |
| 1354 | } |
| 1355 | } break; |
| 1356 | } |
| 1357 | |
| 1358 | FT_GlyphSlot slot = fd->face->glyph; |
| 1359 | bool from_svg = (slot->format == FT_GLYPH_FORMAT_SVG); // Need to check before FT_Render_Glyph as it will change format to bitmap. |
| 1360 | if (!outline) { |
| 1361 | if (!p_font_data->msdf) { |
| 1362 | error = FT_Render_Glyph(slot, aa_mode); |
| 1363 | } |
| 1364 | if (!error) { |
| 1365 | if (p_font_data->msdf) { |
| 1366 | #ifdef MODULE_MSDFGEN_ENABLED |
| 1367 | gl = rasterize_msdf(p_font_data, fd, p_font_data->msdf_range, rect_range, &slot->outline, Vector2((h + (1 << 9)) >> 10, (v + (1 << 9)) >> 10) / 64.0); |
| 1368 | #else |
| 1369 | fd->glyph_map[p_glyph] = FontGlyph(); |
| 1370 | ERR_FAIL_V_MSG(false, "Compiled without MSDFGEN support!"); |
| 1371 | #endif |
| 1372 | } else { |
| 1373 | gl = rasterize_bitmap(fd, rect_range, slot->bitmap, slot->bitmap_top, slot->bitmap_left, Vector2((h + (1 << 9)) >> 10, (v + (1 << 9)) >> 10) / 64.0, bgra); |
| 1374 | } |
| 1375 | } |
| 1376 | } else { |
| 1377 | FT_Stroker stroker; |
| 1378 | if (FT_Stroker_New(ft_library, &stroker) != 0) { |
| 1379 | fd->glyph_map[p_glyph] = FontGlyph(); |
| 1380 | ERR_FAIL_V_MSG(false, "FreeType: Failed to load glyph stroker."); |
| 1381 | } |
| 1382 | |
| 1383 | FT_Stroker_Set(stroker, (int)(fd->size.y * 16.0), FT_STROKER_LINECAP_BUTT, FT_STROKER_LINEJOIN_ROUND, 0); |
| 1384 | FT_Glyph glyph; |
| 1385 | FT_BitmapGlyph glyph_bitmap; |
| 1386 | |
| 1387 | if (FT_Get_Glyph(fd->face->glyph, &glyph) != 0) { |
| 1388 | goto cleanup_stroker; |
| 1389 | } |
| 1390 | if (FT_Glyph_Stroke(&glyph, stroker, 1) != 0) { |
| 1391 | goto cleanup_glyph; |
| 1392 | } |
| 1393 | if (FT_Glyph_To_Bitmap(&glyph, aa_mode, nullptr, 1) != 0) { |
| 1394 | goto cleanup_glyph; |
| 1395 | } |
| 1396 | glyph_bitmap = (FT_BitmapGlyph)glyph; |
| 1397 | gl = rasterize_bitmap(fd, rect_range, glyph_bitmap->bitmap, glyph_bitmap->top, glyph_bitmap->left, Vector2(), bgra); |
| 1398 | |
| 1399 | cleanup_glyph: |
| 1400 | FT_Done_Glyph(glyph); |
| 1401 | cleanup_stroker: |
| 1402 | FT_Stroker_Done(stroker); |
| 1403 | } |
| 1404 | gl.from_svg = from_svg; |
| 1405 | E = fd->glyph_map.insert(p_glyph, gl); |
| 1406 | r_glyph = E->value; |
| 1407 | return gl.found; |
| 1408 | } |
| 1409 | #endif |
| 1410 | E = fd->glyph_map.insert(p_glyph, FontGlyph()); |
| 1411 | r_glyph = E->value; |
| 1412 | return false; |
| 1413 | } |
| 1414 | |
| 1415 | bool TextServerAdvanced::_ensure_cache_for_size(FontAdvanced *p_font_data, const Vector2i &p_size, FontForSizeAdvanced *&r_cache_for_size, bool p_silent, uint32_t p_oversampling) const { |
| 1416 | ERR_FAIL_COND_V(p_size.x <= 0, false); |
| 1417 | |
| 1418 | HashMap<Vector2i, FontForSizeAdvanced *>::Iterator E = p_font_data->cache.find(p_size); |
| 1419 | if (E) { |
| 1420 | r_cache_for_size = E->value; |
| 1421 | // Size used directly, remove from oversampling list. |
| 1422 | if (p_oversampling == 0 && E->value->viewport_oversampling != 0) { |
| 1423 | OversamplingLevel *ol = oversampling_levels.getptr(E->value->viewport_oversampling); |
| 1424 | if (ol) { |
| 1425 | ol->fonts.erase(E->value); |
| 1426 | } |
| 1427 | } |
| 1428 | return true; |
| 1429 | } |
| 1430 | |
| 1431 | FontForSizeAdvanced *fd = memnew(FontForSizeAdvanced); |
| 1432 | fd->size = p_size; |
| 1433 | if (p_font_data->data_ptr && (p_font_data->data_size > 0)) { |
| 1434 | // Init dynamic font. |
| 1435 | #ifdef MODULE_FREETYPE_ENABLED |
| 1436 | int error = 0; |
| 1437 | { |
| 1438 | MutexLock ftlock(ft_mutex); |
| 1439 | if (!ft_library) { |
| 1440 | error = FT_Init_FreeType(&ft_library); |
| 1441 | if (error != 0) { |
| 1442 | memdelete(fd); |
| 1443 | if (p_silent) { |
| 1444 | return false; |
| 1445 | } else { |
| 1446 | ERR_FAIL_V_MSG(false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'."); |
| 1447 | } |
| 1448 | } |
| 1449 | #ifdef MODULE_SVG_ENABLED |
| 1450 | FT_Property_Set(ft_library, "ot-svg", "svg-hooks", get_tvg_svg_in_ot_hooks()); |
| 1451 | #endif |
| 1452 | } |
| 1453 | |
| 1454 | memset(&fd->stream, 0, sizeof(FT_StreamRec)); |
| 1455 | fd->stream.base = (unsigned char *)p_font_data->data_ptr; |
| 1456 | fd->stream.size = p_font_data->data_size; |
| 1457 | fd->stream.pos = 0; |
| 1458 | |
| 1459 | FT_Open_Args fargs; |
| 1460 | memset(&fargs, 0, sizeof(FT_Open_Args)); |
| 1461 | fargs.memory_base = (unsigned char *)p_font_data->data_ptr; |
| 1462 | fargs.memory_size = p_font_data->data_size; |
| 1463 | fargs.flags = FT_OPEN_MEMORY; |
| 1464 | fargs.stream = &fd->stream; |
| 1465 | |
| 1466 | error = FT_Open_Face(ft_library, &fargs, p_font_data->face_index, &fd->face); |
| 1467 | if (error) { |
| 1468 | if (fd->face) { |
| 1469 | FT_Done_Face(fd->face); |
| 1470 | fd->face = nullptr; |
| 1471 | } |
| 1472 | memdelete(fd); |
| 1473 | if (p_silent) { |
| 1474 | return false; |
| 1475 | } else { |
| 1476 | ERR_FAIL_V_MSG(false, "FreeType: Error loading font: '" + String(FT_Error_String(error)) + "' (face_index=" + String::num_int64(p_font_data->face_index) + ")."); |
| 1477 | } |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | double sz = double(fd->size.x) / 64.0; |
| 1482 | if (p_font_data->msdf) { |
| 1483 | sz = p_font_data->msdf_source_size; |
| 1484 | } |
| 1485 | |
| 1486 | if (FT_HAS_COLOR(fd->face) && fd->face->num_fixed_sizes > 0) { |
| 1487 | int best_match = 0; |
| 1488 | int diff = Math::abs(sz - ((int64_t)fd->face->available_sizes[0].width)); |
| 1489 | fd->scale = sz / fd->face->available_sizes[0].width; |
| 1490 | for (int i = 1; i < fd->face->num_fixed_sizes; i++) { |
| 1491 | int ndiff = Math::abs(sz - ((int64_t)fd->face->available_sizes[i].width)); |
| 1492 | if (ndiff < diff) { |
| 1493 | best_match = i; |
| 1494 | diff = ndiff; |
| 1495 | fd->scale = sz / fd->face->available_sizes[i].width; |
| 1496 | } |
| 1497 | } |
| 1498 | FT_Select_Size(fd->face, best_match); |
| 1499 | } else { |
| 1500 | FT_Size_RequestRec req; |
| 1501 | req.type = FT_SIZE_REQUEST_TYPE_NOMINAL; |
| 1502 | req.width = sz * 64.0; |
| 1503 | req.height = sz * 64.0; |
| 1504 | req.horiResolution = 0; |
| 1505 | req.vertResolution = 0; |
| 1506 | |
| 1507 | FT_Request_Size(fd->face, &req); |
| 1508 | if (fd->face->size->metrics.y_ppem != 0) { |
| 1509 | fd->scale = sz / (double)fd->face->size->metrics.y_ppem; |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | fd->hb_handle = hb_ft_font_create(fd->face, nullptr); |
| 1514 | |
| 1515 | fd->ascent = (fd->face->size->metrics.ascender / 64.0) * fd->scale; |
| 1516 | fd->descent = (-fd->face->size->metrics.descender / 64.0) * fd->scale; |
| 1517 | fd->underline_position = (-FT_MulFix(fd->face->underline_position, fd->face->size->metrics.y_scale) / 64.0) * fd->scale; |
| 1518 | fd->underline_thickness = (FT_MulFix(fd->face->underline_thickness, fd->face->size->metrics.y_scale) / 64.0) * fd->scale; |
| 1519 | |
| 1520 | #if HB_VERSION_ATLEAST(3, 3, 0) |
| 1521 | hb_font_set_synthetic_slant(fd->hb_handle, p_font_data->transform[0][1]); |
| 1522 | #else |
| 1523 | #ifndef _MSC_VER |
| 1524 | #warning Building with HarfBuzz < 3.3.0, synthetic slant offset correction disabled. |
| 1525 | #endif |
| 1526 | #endif |
| 1527 | |
| 1528 | if (!p_font_data->face_init) { |
| 1529 | // When a font does not provide a `family_name`, FreeType tries to synthesize one based on other names. |
| 1530 | // FreeType automatically converts non-ASCII characters to "?" in the synthesized name. |
| 1531 | // To avoid that behavior, use the format-specific name directly if available. |
| 1532 | hb_face_t *hb_face = hb_font_get_face(fd->hb_handle); |
| 1533 | unsigned int num_entries = 0; |
| 1534 | const hb_ot_name_entry_t *names = hb_ot_name_list_names(hb_face, &num_entries); |
| 1535 | const hb_language_t english = hb_language_from_string("en", -1); |
| 1536 | for (unsigned int i = 0; i < num_entries; i++) { |
| 1537 | if (names[i].name_id != HB_OT_NAME_ID_FONT_FAMILY) { |
| 1538 | continue; |
| 1539 | } |
| 1540 | if (!p_font_data->font_name.is_empty() && names[i].language != english) { |
| 1541 | continue; |
| 1542 | } |
| 1543 | unsigned int text_size = hb_ot_name_get_utf32(hb_face, names[i].name_id, names[i].language, nullptr, nullptr) + 1; |
| 1544 | p_font_data->font_name.resize_uninitialized(text_size); |
| 1545 | hb_ot_name_get_utf32(hb_face, names[i].name_id, names[i].language, &text_size, (uint32_t *)p_font_data->font_name.ptrw()); |
| 1546 | } |
| 1547 | if (p_font_data->font_name.is_empty() && fd->face->family_name != nullptr) { |
| 1548 | p_font_data->font_name = String::utf8((const char *)fd->face->family_name); |
| 1549 | } |
| 1550 | if (fd->face->style_name != nullptr) { |
| 1551 | p_font_data->style_name = String::utf8((const char *)fd->face->style_name); |
| 1552 | } |
| 1553 | p_font_data->weight = _font_get_weight_by_name(p_font_data->style_name.to_lower()); |
| 1554 | p_font_data->stretch = _font_get_stretch_by_name(p_font_data->style_name.to_lower()); |
| 1555 | p_font_data->style_flags = 0; |
| 1556 | if ((fd->face->style_flags & FT_STYLE_FLAG_BOLD) || p_font_data->weight >= 700) { |
| 1557 | p_font_data->style_flags.set_flag(FONT_BOLD); |
| 1558 | } |
| 1559 | if ((fd->face->style_flags & FT_STYLE_FLAG_ITALIC) || _is_ital_style(p_font_data->style_name.to_lower())) { |
| 1560 | p_font_data->style_flags.set_flag(FONT_ITALIC); |
| 1561 | } |
| 1562 | if (fd->face->face_flags & FT_FACE_FLAG_FIXED_WIDTH) { |
| 1563 | p_font_data->style_flags.set_flag(FONT_FIXED_WIDTH); |
| 1564 | } |
| 1565 | |
| 1566 | // Get supported scripts from OpenType font data. |
| 1567 | p_font_data->supported_scripts.clear(); |
| 1568 | unsigned int count = hb_ot_layout_table_get_script_tags(hb_face, HB_OT_TAG_GSUB, 0, nullptr, nullptr); |
| 1569 | if (count != 0) { |
| 1570 | hb_tag_t *script_tags = (hb_tag_t *)memalloc(count * sizeof(hb_tag_t)); |
| 1571 | hb_ot_layout_table_get_script_tags(hb_face, HB_OT_TAG_GSUB, 0, &count, script_tags); |
| 1572 | for (unsigned int i = 0; i < count; i++) { |
| 1573 | p_font_data->supported_scripts.insert(script_tags[i]); |
| 1574 | } |
| 1575 | memfree(script_tags); |
| 1576 | } |
| 1577 | count = hb_ot_layout_table_get_script_tags(hb_face, HB_OT_TAG_GPOS, 0, nullptr, nullptr); |
| 1578 | if (count != 0) { |
| 1579 | hb_tag_t *script_tags = (hb_tag_t *)memalloc(count * sizeof(hb_tag_t)); |
| 1580 | hb_ot_layout_table_get_script_tags(hb_face, HB_OT_TAG_GPOS, 0, &count, script_tags); |
| 1581 | for (unsigned int i = 0; i < count; i++) { |
| 1582 | p_font_data->supported_scripts.insert(script_tags[i]); |
| 1583 | } |
| 1584 | memfree(script_tags); |
| 1585 | } |
| 1586 | |
| 1587 | // Get supported scripts from OS2 table. |
| 1588 | TT_OS2 *os2 = (TT_OS2 *)FT_Get_Sfnt_Table(fd->face, FT_SFNT_OS2); |
| 1589 | if (os2) { |
| 1590 | if ((os2->ulUnicodeRange1 & 1L << 4) || (os2->ulUnicodeRange1 & 1L << 5) || (os2->ulUnicodeRange1 & 1L << 6) || (os2->ulUnicodeRange1 & 1L << 31) || (os2->ulUnicodeRange2 & 1L << 0) || (os2->ulUnicodeRange2 & 1L << 1) || (os2->ulUnicodeRange2 & 1L << 2) || (os2->ulUnicodeRange2 & 1L << 3) || (os2->ulUnicodeRange2 & 1L << 4) || (os2->ulUnicodeRange2 & 1L << 5) || (os2->ulUnicodeRange2 & 1L << 6) || (os2->ulUnicodeRange2 & 1L << 7) || (os2->ulUnicodeRange2 & 1L << 8) || (os2->ulUnicodeRange2 & 1L << 9) || (os2->ulUnicodeRange2 & 1L << 10) || (os2->ulUnicodeRange2 & 1L << 11) || (os2->ulUnicodeRange2 & 1L << 12) || (os2->ulUnicodeRange2 & 1L << 13) || (os2->ulUnicodeRange2 & 1L << 14) || (os2->ulUnicodeRange2 & 1L << 15) || (os2->ulUnicodeRange2 & 1L << 30) || (os2->ulUnicodeRange3 & 1L << 0) || (os2->ulUnicodeRange3 & 1L << 1) || (os2->ulUnicodeRange3 & 1L << 2) || (os2->ulUnicodeRange3 & 1L << 4) || (os2->ulUnicodeRange3 & 1L << 5) || (os2->ulUnicodeRange3 & 1L << 18) || (os2->ulUnicodeRange3 & 1L << 24) || (os2->ulUnicodeRange3 & 1L << 25) || (os2->ulUnicodeRange3 & 1L << 26) || (os2->ulUnicodeRange3 & 1L << 27) || (os2->ulUnicodeRange3 & 1L << 28) || (os2->ulUnicodeRange4 & 1L << 3) || (os2->ulUnicodeRange4 & 1L << 6) || (os2->ulUnicodeRange4 & 1L << 15) || (os2->ulUnicodeRange4 & 1L << 23) || (os2->ulUnicodeRange4 & 1L << 24) || (os2->ulUnicodeRange4 & 1L << 26)) { |
| 1591 | p_font_data->supported_scripts.insert(HB_SCRIPT_COMMON); |
| 1592 | } |
| 1593 | if ((os2->ulUnicodeRange1 & 1L << 0) || (os2->ulUnicodeRange1 & 1L << 1) || (os2->ulUnicodeRange1 & 1L << 2) || (os2->ulUnicodeRange1 & 1L << 3) || (os2->ulUnicodeRange1 & 1L << 29)) { |
| 1594 | p_font_data->supported_scripts.insert(HB_SCRIPT_LATIN); |
| 1595 | } |
| 1596 | if ((os2->ulUnicodeRange1 & 1L << 7) || (os2->ulUnicodeRange1 & 1L << 30)) { |
| 1597 | p_font_data->supported_scripts.insert(HB_SCRIPT_GREEK); |
| 1598 | } |
| 1599 | if (os2->ulUnicodeRange1 & 1L << 8) { |
| 1600 | p_font_data->supported_scripts.insert(HB_SCRIPT_COPTIC); |
| 1601 | } |
| 1602 | if (os2->ulUnicodeRange1 & 1L << 9) { |
| 1603 | p_font_data->supported_scripts.insert(HB_SCRIPT_CYRILLIC); |
| 1604 | } |
| 1605 | if (os2->ulUnicodeRange1 & 1L << 10) { |
| 1606 | p_font_data->supported_scripts.insert(HB_SCRIPT_ARMENIAN); |
| 1607 | } |
| 1608 | if (os2->ulUnicodeRange1 & 1L << 11) { |
| 1609 | p_font_data->supported_scripts.insert(HB_SCRIPT_HEBREW); |
| 1610 | } |
| 1611 | if (os2->ulUnicodeRange1 & 1L << 12) { |
| 1612 | p_font_data->supported_scripts.insert(HB_SCRIPT_VAI); |
| 1613 | } |
| 1614 | if ((os2->ulUnicodeRange1 & 1L << 13) || (os2->ulUnicodeRange2 & 1L << 31) || (os2->ulUnicodeRange3 & 1L << 3)) { |
| 1615 | p_font_data->supported_scripts.insert(HB_SCRIPT_ARABIC); |
| 1616 | } |
| 1617 | if (os2->ulUnicodeRange1 & 1L << 14) { |
| 1618 | p_font_data->supported_scripts.insert(HB_SCRIPT_NKO); |
| 1619 | } |
| 1620 | if (os2->ulUnicodeRange1 & 1L << 15) { |
| 1621 | p_font_data->supported_scripts.insert(HB_SCRIPT_DEVANAGARI); |
| 1622 | } |
| 1623 | if (os2->ulUnicodeRange1 & 1L << 16) { |
| 1624 | p_font_data->supported_scripts.insert(HB_SCRIPT_BENGALI); |
| 1625 | } |
| 1626 | if (os2->ulUnicodeRange1 & 1L << 17) { |
| 1627 | p_font_data->supported_scripts.insert(HB_SCRIPT_GURMUKHI); |
| 1628 | } |
| 1629 | if (os2->ulUnicodeRange1 & 1L << 18) { |
| 1630 | p_font_data->supported_scripts.insert(HB_SCRIPT_GUJARATI); |
| 1631 | } |
| 1632 | if (os2->ulUnicodeRange1 & 1L << 19) { |
| 1633 | p_font_data->supported_scripts.insert(HB_SCRIPT_ORIYA); |
| 1634 | } |
| 1635 | if (os2->ulUnicodeRange1 & 1L << 20) { |
| 1636 | p_font_data->supported_scripts.insert(HB_SCRIPT_TAMIL); |
| 1637 | } |
| 1638 | if (os2->ulUnicodeRange1 & 1L << 21) { |
| 1639 | p_font_data->supported_scripts.insert(HB_SCRIPT_TELUGU); |
| 1640 | } |
| 1641 | if (os2->ulUnicodeRange1 & 1L << 22) { |
| 1642 | p_font_data->supported_scripts.insert(HB_SCRIPT_KANNADA); |
| 1643 | } |
| 1644 | if (os2->ulUnicodeRange1 & 1L << 23) { |
| 1645 | p_font_data->supported_scripts.insert(HB_SCRIPT_MALAYALAM); |
| 1646 | } |
| 1647 | if (os2->ulUnicodeRange1 & 1L << 24) { |
| 1648 | p_font_data->supported_scripts.insert(HB_SCRIPT_THAI); |
| 1649 | } |
| 1650 | if (os2->ulUnicodeRange1 & 1L << 25) { |
| 1651 | p_font_data->supported_scripts.insert(HB_SCRIPT_LAO); |
| 1652 | } |
| 1653 | if (os2->ulUnicodeRange1 & 1L << 26) { |
| 1654 | p_font_data->supported_scripts.insert(HB_SCRIPT_GEORGIAN); |
| 1655 | } |
| 1656 | if (os2->ulUnicodeRange1 & 1L << 27) { |
| 1657 | p_font_data->supported_scripts.insert(HB_SCRIPT_BALINESE); |
| 1658 | } |
| 1659 | if ((os2->ulUnicodeRange1 & 1L << 28) || (os2->ulUnicodeRange2 & 1L << 20) || (os2->ulUnicodeRange2 & 1L << 24)) { |
| 1660 | p_font_data->supported_scripts.insert(HB_SCRIPT_HANGUL); |
| 1661 | } |
| 1662 | if ((os2->ulUnicodeRange2 & 1L << 21) || (os2->ulUnicodeRange2 & 1L << 22) || (os2->ulUnicodeRange2 & 1L << 23) || (os2->ulUnicodeRange2 & 1L << 26) || (os2->ulUnicodeRange2 & 1L << 27) || (os2->ulUnicodeRange2 & 1L << 29)) { |
| 1663 | p_font_data->supported_scripts.insert(HB_SCRIPT_HAN); |
| 1664 | } |
| 1665 | if (os2->ulUnicodeRange2 & 1L << 17) { |
| 1666 | p_font_data->supported_scripts.insert(HB_SCRIPT_HIRAGANA); |
| 1667 | } |
| 1668 | if (os2->ulUnicodeRange2 & 1L << 18) { |
| 1669 | p_font_data->supported_scripts.insert(HB_SCRIPT_KATAKANA); |
| 1670 | } |
| 1671 | if (os2->ulUnicodeRange2 & 1L << 19) { |
| 1672 | p_font_data->supported_scripts.insert(HB_SCRIPT_BOPOMOFO); |
| 1673 | } |
| 1674 | if (os2->ulUnicodeRange3 & 1L << 6) { |
| 1675 | p_font_data->supported_scripts.insert(HB_SCRIPT_TIBETAN); |
| 1676 | } |
| 1677 | if (os2->ulUnicodeRange3 & 1L << 7) { |
| 1678 | p_font_data->supported_scripts.insert(HB_SCRIPT_SYRIAC); |
| 1679 | } |
| 1680 | if (os2->ulUnicodeRange3 & 1L << 8) { |
| 1681 | p_font_data->supported_scripts.insert(HB_SCRIPT_THAANA); |
| 1682 | } |
| 1683 | if (os2->ulUnicodeRange3 & 1L << 9) { |
| 1684 | p_font_data->supported_scripts.insert(HB_SCRIPT_SINHALA); |
| 1685 | } |
| 1686 | if (os2->ulUnicodeRange3 & 1L << 10) { |
| 1687 | p_font_data->supported_scripts.insert(HB_SCRIPT_MYANMAR); |
| 1688 | } |
| 1689 | if (os2->ulUnicodeRange3 & 1L << 11) { |
| 1690 | p_font_data->supported_scripts.insert(HB_SCRIPT_ETHIOPIC); |
| 1691 | } |
| 1692 | if (os2->ulUnicodeRange3 & 1L << 12) { |
| 1693 | p_font_data->supported_scripts.insert(HB_SCRIPT_CHEROKEE); |
| 1694 | } |
| 1695 | if (os2->ulUnicodeRange3 & 1L << 13) { |
| 1696 | p_font_data->supported_scripts.insert(HB_SCRIPT_CANADIAN_SYLLABICS); |
| 1697 | } |
| 1698 | if (os2->ulUnicodeRange3 & 1L << 14) { |
| 1699 | p_font_data->supported_scripts.insert(HB_SCRIPT_OGHAM); |
| 1700 | } |
| 1701 | if (os2->ulUnicodeRange3 & 1L << 15) { |
| 1702 | p_font_data->supported_scripts.insert(HB_SCRIPT_RUNIC); |
| 1703 | } |
| 1704 | if (os2->ulUnicodeRange3 & 1L << 16) { |
| 1705 | p_font_data->supported_scripts.insert(HB_SCRIPT_KHMER); |
| 1706 | } |
| 1707 | if (os2->ulUnicodeRange3 & 1L << 17) { |
| 1708 | p_font_data->supported_scripts.insert(HB_SCRIPT_MONGOLIAN); |
| 1709 | } |
| 1710 | if (os2->ulUnicodeRange3 & 1L << 19) { |
| 1711 | p_font_data->supported_scripts.insert(HB_SCRIPT_YI); |
| 1712 | } |
| 1713 | if (os2->ulUnicodeRange3 & 1L << 20) { |
| 1714 | p_font_data->supported_scripts.insert(HB_SCRIPT_HANUNOO); |
| 1715 | p_font_data->supported_scripts.insert(HB_SCRIPT_TAGBANWA); |
| 1716 | p_font_data->supported_scripts.insert(HB_SCRIPT_BUHID); |
| 1717 | p_font_data->supported_scripts.insert(HB_SCRIPT_TAGALOG); |
| 1718 | } |
| 1719 | if (os2->ulUnicodeRange3 & 1L << 21) { |
| 1720 | p_font_data->supported_scripts.insert(HB_SCRIPT_OLD_ITALIC); |
| 1721 | } |
| 1722 | if (os2->ulUnicodeRange3 & 1L << 22) { |
| 1723 | p_font_data->supported_scripts.insert(HB_SCRIPT_GOTHIC); |
| 1724 | } |
| 1725 | if (os2->ulUnicodeRange3 & 1L << 23) { |
| 1726 | p_font_data->supported_scripts.insert(HB_SCRIPT_DESERET); |
| 1727 | } |
| 1728 | if (os2->ulUnicodeRange3 & 1L << 29) { |
| 1729 | p_font_data->supported_scripts.insert(HB_SCRIPT_LIMBU); |
| 1730 | } |
| 1731 | if (os2->ulUnicodeRange3 & 1L << 30) { |
| 1732 | p_font_data->supported_scripts.insert(HB_SCRIPT_TAI_LE); |
| 1733 | } |
| 1734 | if (os2->ulUnicodeRange3 & 1L << 31) { |
| 1735 | p_font_data->supported_scripts.insert(HB_SCRIPT_NEW_TAI_LUE); |
| 1736 | } |
| 1737 | if (os2->ulUnicodeRange4 & 1L << 0) { |
| 1738 | p_font_data->supported_scripts.insert(HB_SCRIPT_BUGINESE); |
| 1739 | } |
| 1740 | if (os2->ulUnicodeRange4 & 1L << 1) { |
| 1741 | p_font_data->supported_scripts.insert(HB_SCRIPT_GLAGOLITIC); |
| 1742 | } |
| 1743 | if (os2->ulUnicodeRange4 & 1L << 2) { |
| 1744 | p_font_data->supported_scripts.insert(HB_SCRIPT_TIFINAGH); |
| 1745 | } |
| 1746 | if (os2->ulUnicodeRange4 & 1L << 4) { |
| 1747 | p_font_data->supported_scripts.insert(HB_SCRIPT_SYLOTI_NAGRI); |
| 1748 | } |
| 1749 | if (os2->ulUnicodeRange4 & 1L << 5) { |
| 1750 | p_font_data->supported_scripts.insert(HB_SCRIPT_LINEAR_B); |
| 1751 | } |
| 1752 | if (os2->ulUnicodeRange4 & 1L << 7) { |
| 1753 | p_font_data->supported_scripts.insert(HB_SCRIPT_UGARITIC); |
| 1754 | } |
| 1755 | if (os2->ulUnicodeRange4 & 1L << 8) { |
| 1756 | p_font_data->supported_scripts.insert(HB_SCRIPT_OLD_PERSIAN); |
| 1757 | } |
| 1758 | if (os2->ulUnicodeRange4 & 1L << 9) { |
| 1759 | p_font_data->supported_scripts.insert(HB_SCRIPT_SHAVIAN); |
| 1760 | } |
| 1761 | if (os2->ulUnicodeRange4 & 1L << 10) { |
| 1762 | p_font_data->supported_scripts.insert(HB_SCRIPT_OSMANYA); |
| 1763 | } |
| 1764 | if (os2->ulUnicodeRange4 & 1L << 11) { |
| 1765 | p_font_data->supported_scripts.insert(HB_SCRIPT_CYPRIOT); |
| 1766 | } |
| 1767 | if (os2->ulUnicodeRange4 & 1L << 12) { |
| 1768 | p_font_data->supported_scripts.insert(HB_SCRIPT_KHAROSHTHI); |
| 1769 | } |
| 1770 | if (os2->ulUnicodeRange4 & 1L << 13) { |
| 1771 | p_font_data->supported_scripts.insert(HB_SCRIPT_TAI_VIET); |
| 1772 | } |
| 1773 | if (os2->ulUnicodeRange4 & 1L << 14) { |
| 1774 | p_font_data->supported_scripts.insert(HB_SCRIPT_CUNEIFORM); |
| 1775 | } |
| 1776 | if (os2->ulUnicodeRange4 & 1L << 16) { |
| 1777 | p_font_data->supported_scripts.insert(HB_SCRIPT_SUNDANESE); |
| 1778 | } |
| 1779 | if (os2->ulUnicodeRange4 & 1L << 17) { |
| 1780 | p_font_data->supported_scripts.insert(HB_SCRIPT_LEPCHA); |
| 1781 | } |
| 1782 | if (os2->ulUnicodeRange4 & 1L << 18) { |
| 1783 | p_font_data->supported_scripts.insert(HB_SCRIPT_OL_CHIKI); |
| 1784 | } |
| 1785 | if (os2->ulUnicodeRange4 & 1L << 19) { |
| 1786 | p_font_data->supported_scripts.insert(HB_SCRIPT_SAURASHTRA); |
| 1787 | } |
| 1788 | if (os2->ulUnicodeRange4 & 1L << 20) { |
| 1789 | p_font_data->supported_scripts.insert(HB_SCRIPT_KAYAH_LI); |
| 1790 | } |
| 1791 | if (os2->ulUnicodeRange4 & 1L << 21) { |
| 1792 | p_font_data->supported_scripts.insert(HB_SCRIPT_REJANG); |
| 1793 | } |
| 1794 | if (os2->ulUnicodeRange4 & 1L << 22) { |
| 1795 | p_font_data->supported_scripts.insert(HB_SCRIPT_CHAM); |
| 1796 | } |
| 1797 | if (os2->ulUnicodeRange4 & 1L << 25) { |
| 1798 | p_font_data->supported_scripts.insert(HB_SCRIPT_ANATOLIAN_HIEROGLYPHS); |
| 1799 | } |
| 1800 | } |
| 1801 | |
| 1802 | // Validate script sample strings. |
| 1803 | { |
| 1804 | LocalVector<uint32_t> failed_scripts; |
| 1805 | |
| 1806 | Vector<UChar> sample_buf; |
| 1807 | sample_buf.resize(255); |
| 1808 | for (const uint32_t &scr_tag : p_font_data->supported_scripts) { |
| 1809 | if ((hb_script_t)scr_tag == HB_SCRIPT_COMMON) { |
| 1810 | continue; |
| 1811 | } |
| 1812 | UErrorCode icu_err = U_ZERO_ERROR; |
| 1813 | int32_t len = uscript_getSampleString(hb_icu_script_from_script((hb_script_t)scr_tag), sample_buf.ptrw(), 255, &icu_err); |
| 1814 | if (U_SUCCESS(icu_err) && len > 0) { |
| 1815 | String sample = String::utf16(sample_buf.ptr(), len); |
| 1816 | for (int ch = 0; ch < sample.length(); ch++) { |
| 1817 | if (FT_Get_Char_Index(fd->face, sample[ch]) == 0) { |
| 1818 | failed_scripts.push_back(scr_tag); |
| 1819 | break; |
| 1820 | } |
| 1821 | } |
| 1822 | } |
| 1823 | } |
| 1824 | for (const uint32_t &scr_tag : failed_scripts) { |
| 1825 | p_font_data->supported_scripts.erase(scr_tag); |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | // Read OpenType feature tags. |
| 1830 | p_font_data->supported_features.clear(); |
| 1831 | count = hb_ot_layout_table_get_feature_tags(hb_face, HB_OT_TAG_GSUB, 0, nullptr, nullptr); |
| 1832 | if (count != 0) { |
| 1833 | hb_tag_t *feature_tags = (hb_tag_t *)memalloc(count * sizeof(hb_tag_t)); |
| 1834 | hb_ot_layout_table_get_feature_tags(hb_face, HB_OT_TAG_GSUB, 0, &count, feature_tags); |
| 1835 | for (unsigned int i = 0; i < count; i++) { |
| 1836 | Dictionary ftr; |
| 1837 | |
| 1838 | #if HB_VERSION_ATLEAST(2, 1, 0) |
| 1839 | hb_ot_name_id_t lbl_id; |
| 1840 | if (hb_ot_layout_feature_get_name_ids(hb_face, HB_OT_TAG_GSUB, i, &lbl_id, nullptr, nullptr, nullptr, nullptr)) { |
| 1841 | PackedInt32Array lbl; |
| 1842 | unsigned int text_size = hb_ot_name_get_utf32(hb_face, lbl_id, hb_language_from_string(TranslationServer::get_singleton()->get_tool_locale().ascii().get_data(), -1), nullptr, nullptr) + 1; |
| 1843 | lbl.resize(text_size); |
| 1844 | memset((uint32_t *)lbl.ptrw(), 0, sizeof(uint32_t) * text_size); |
| 1845 | hb_ot_name_get_utf32(hb_face, lbl_id, hb_language_from_string(TranslationServer::get_singleton()->get_tool_locale().ascii().get_data(), -1), &text_size, (uint32_t *)lbl.ptrw()); |
| 1846 | ftr["label"] = String((const char32_t *)lbl.ptr()); |
| 1847 | } |
| 1848 | #else |
| 1849 | #ifndef _MSC_VER |
| 1850 | #warning Building with HarfBuzz < 2.1.0, readable OpenType feature names disabled. |
| 1851 | #endif |
| 1852 | #endif |
| 1853 | ftr["type"] = _get_tag_type(feature_tags[i]); |
| 1854 | ftr["hidden"] = _get_tag_hidden(feature_tags[i]); |
| 1855 | |
| 1856 | p_font_data->supported_features[feature_tags[i]] = ftr; |
| 1857 | } |
| 1858 | memfree(feature_tags); |
| 1859 | } |
| 1860 | count = hb_ot_layout_table_get_feature_tags(hb_face, HB_OT_TAG_GPOS, 0, nullptr, nullptr); |
| 1861 | if (count != 0) { |
| 1862 | hb_tag_t *feature_tags = (hb_tag_t *)memalloc(count * sizeof(hb_tag_t)); |
| 1863 | hb_ot_layout_table_get_feature_tags(hb_face, HB_OT_TAG_GPOS, 0, &count, feature_tags); |
| 1864 | for (unsigned int i = 0; i < count; i++) { |
| 1865 | Dictionary ftr; |
| 1866 | |
| 1867 | #if HB_VERSION_ATLEAST(2, 1, 0) |
| 1868 | hb_ot_name_id_t lbl_id; |
| 1869 | if (hb_ot_layout_feature_get_name_ids(hb_face, HB_OT_TAG_GPOS, i, &lbl_id, nullptr, nullptr, nullptr, nullptr)) { |
| 1870 | PackedInt32Array lbl; |
| 1871 | unsigned int text_size = hb_ot_name_get_utf32(hb_face, lbl_id, hb_language_from_string(TranslationServer::get_singleton()->get_tool_locale().ascii().get_data(), -1), nullptr, nullptr) + 1; |
| 1872 | lbl.resize(text_size); |
| 1873 | memset((uint32_t *)lbl.ptrw(), 0, sizeof(uint32_t) * text_size); |
| 1874 | hb_ot_name_get_utf32(hb_face, lbl_id, hb_language_from_string(TranslationServer::get_singleton()->get_tool_locale().ascii().get_data(), -1), &text_size, (uint32_t *)lbl.ptrw()); |
| 1875 | ftr["label"] = String((const char32_t *)lbl.ptr()); |
| 1876 | } |
| 1877 | #else |
| 1878 | #ifndef _MSC_VER |
| 1879 | #warning Building with HarfBuzz < 2.1.0, readable OpenType feature names disabled. |
| 1880 | #endif |
| 1881 | #endif |
| 1882 | ftr["type"] = _get_tag_type(feature_tags[i]); |
| 1883 | ftr["hidden"] = _get_tag_hidden(feature_tags[i]); |
| 1884 | |
| 1885 | p_font_data->supported_features[feature_tags[i]] = ftr; |
| 1886 | } |
| 1887 | memfree(feature_tags); |
| 1888 | } |
| 1889 | |
| 1890 | // Read OpenType variations. |
| 1891 | p_font_data->supported_varaitions.clear(); |
| 1892 | if (fd->face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) { |
| 1893 | FT_MM_Var *amaster; |
| 1894 | FT_Get_MM_Var(fd->face, &amaster); |
| 1895 | for (FT_UInt i = 0; i < amaster->num_axis; i++) { |
| 1896 | p_font_data->supported_varaitions[(int32_t)amaster->axis[i].tag] = Vector3i(amaster->axis[i].minimum / 65536, amaster->axis[i].maximum / 65536, amaster->axis[i].def / 65536); |
| 1897 | } |
| 1898 | FT_Done_MM_Var(ft_library, amaster); |
| 1899 | } |
| 1900 | p_font_data->face_init = true; |
| 1901 | } |
| 1902 | |
| 1903 | #if defined(MACOS_ENABLED) || defined(IOS_ENABLED) |
| 1904 | if (p_font_data->font_name == ".Apple Color Emoji UI" || p_font_data->font_name == "Apple Color Emoji") { |
| 1905 | // The baseline offset is missing from the Apple Color Emoji UI font data, so add it manually. |
| 1906 | // This issue doesn't occur with other system emoji fonts. |
| 1907 | if (!FT_Load_Glyph(fd->face, FT_Get_Char_Index(fd->face, 0x1F92E), FT_LOAD_DEFAULT | FT_LOAD_COLOR)) { |
| 1908 | if (fd->face->glyph->metrics.horiBearingY == fd->face->glyph->metrics.height) { |
| 1909 | p_font_data->baseline_offset = 0.15; |
| 1910 | } |
| 1911 | } |
| 1912 | } |
| 1913 | #endif |
| 1914 | |
| 1915 | // Write variations. |
| 1916 | if (fd->face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS) { |
| 1917 | FT_MM_Var *amaster; |
| 1918 | |
| 1919 | FT_Get_MM_Var(fd->face, &amaster); |
| 1920 | |
| 1921 | Vector<hb_variation_t> hb_vars; |
| 1922 | Vector<FT_Fixed> coords; |
| 1923 | coords.resize(amaster->num_axis); |
| 1924 | |
| 1925 | FT_Get_Var_Design_Coordinates(fd->face, coords.size(), coords.ptrw()); |
| 1926 | |
| 1927 | for (FT_UInt i = 0; i < amaster->num_axis; i++) { |
| 1928 | hb_variation_t var; |
| 1929 | |
| 1930 | // Reset to default. |
| 1931 | var.tag = amaster->axis[i].tag; |
| 1932 | var.value = (double)amaster->axis[i].def / 65536.0; |
| 1933 | coords.write[i] = amaster->axis[i].def; |
| 1934 | |
| 1935 | if (p_font_data->variation_coordinates.has(var.tag)) { |
| 1936 | var.value = p_font_data->variation_coordinates[var.tag]; |
| 1937 | coords.write[i] = CLAMP(var.value * 65536.0, amaster->axis[i].minimum, amaster->axis[i].maximum); |
| 1938 | } |
| 1939 | |
| 1940 | if (p_font_data->variation_coordinates.has(_tag_to_name(var.tag))) { |
| 1941 | var.value = p_font_data->variation_coordinates[_tag_to_name(var.tag)]; |
| 1942 | coords.write[i] = CLAMP(var.value * 65536.0, amaster->axis[i].minimum, amaster->axis[i].maximum); |
| 1943 | } |
| 1944 | |
| 1945 | hb_vars.push_back(var); |
| 1946 | } |
| 1947 | |
| 1948 | FT_Set_Var_Design_Coordinates(fd->face, coords.size(), coords.ptrw()); |
| 1949 | hb_font_set_variations(fd->hb_handle, hb_vars.is_empty() ? nullptr : &hb_vars[0], hb_vars.size()); |
| 1950 | FT_Done_MM_Var(ft_library, amaster); |
| 1951 | } |
| 1952 | #else |
| 1953 | memdelete(fd); |
| 1954 | if (p_silent) { |
| 1955 | return false; |
| 1956 | } else { |
| 1957 | ERR_FAIL_V_MSG(false, "FreeType: Can't load dynamic font, engine is compiled without FreeType support!"); |
| 1958 | } |
| 1959 | #endif |
| 1960 | } else { |
| 1961 | // Init bitmap font. |
| 1962 | fd->hb_handle = _bmp_font_create(fd, nullptr); |
| 1963 | } |
| 1964 | |
| 1965 | fd->owner = p_font_data; |
| 1966 | p_font_data->cache.insert(p_size, fd); |
| 1967 | r_cache_for_size = fd; |
| 1968 | if (p_oversampling != 0) { |
| 1969 | OversamplingLevel *ol = oversampling_levels.getptr(p_oversampling); |
| 1970 | if (ol) { |
| 1971 | fd->viewport_oversampling = p_oversampling; |
| 1972 | ol->fonts.insert(fd); |
| 1973 | } |
| 1974 | } |
| 1975 | return true; |
| 1976 | } |
| 1977 | |
| 1978 | void TextServerAdvanced::_reference_oversampling_level(double p_oversampling) { |
| 1979 | uint32_t oversampling = CLAMP(p_oversampling, 0.1, 100.0) * 64; |
| 1980 | if (oversampling == 64) { |
| 1981 | return; |
| 1982 | } |
| 1983 | OversamplingLevel *ol = oversampling_levels.getptr(oversampling); |
| 1984 | if (ol) { |
| 1985 | ol->refcount++; |
| 1986 | } else { |
| 1987 | OversamplingLevel new_ol; |
| 1988 | oversampling_levels.insert(oversampling, new_ol); |
| 1989 | } |
| 1990 | } |
| 1991 | |
| 1992 | void TextServerAdvanced::_unreference_oversampling_level(double p_oversampling) { |
| 1993 | uint32_t oversampling = CLAMP(p_oversampling, 0.1, 100.0) * 64; |
| 1994 | if (oversampling == 64) { |
| 1995 | return; |
| 1996 | } |
| 1997 | OversamplingLevel *ol = oversampling_levels.getptr(oversampling); |
| 1998 | if (ol) { |
| 1999 | ol->refcount--; |
| 2000 | if (ol->refcount == 0) { |
| 2001 | for (FontForSizeAdvanced *fd : ol->fonts) { |
| 2002 | fd->owner->cache.erase(fd->size); |
| 2003 | memdelete(fd); |
| 2004 | } |
| 2005 | ol->fonts.clear(); |
| 2006 | oversampling_levels.erase(oversampling); |
| 2007 | } |
| 2008 | } |
| 2009 | } |
| 2010 | |
| 2011 | _FORCE_INLINE_ bool TextServerAdvanced::_font_validate(const RID &p_font_rid) const { |
| 2012 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2013 | ERR_FAIL_NULL_V(fd, false); |
| 2014 | |
| 2015 | MutexLock lock(fd->mutex); |
| 2016 | Vector2i size = _get_size(fd, 16); |
| 2017 | FontForSizeAdvanced *ffsd = nullptr; |
| 2018 | return _ensure_cache_for_size(fd, size, ffsd, true); |
| 2019 | } |
| 2020 | |
| 2021 | _FORCE_INLINE_ void TextServerAdvanced::_font_clear_cache(FontAdvanced *p_font_data) { |
| 2022 | MutexLock ftlock(ft_mutex); |
| 2023 | |
| 2024 | for (const KeyValue<Vector2i, FontForSizeAdvanced *> &E : p_font_data->cache) { |
| 2025 | if (E.value->viewport_oversampling != 0) { |
| 2026 | OversamplingLevel *ol = oversampling_levels.getptr(E.value->viewport_oversampling); |
| 2027 | if (ol) { |
| 2028 | ol->fonts.erase(E.value); |
| 2029 | } |
| 2030 | } |
| 2031 | memdelete(E.value); |
| 2032 | } |
| 2033 | p_font_data->cache.clear(); |
| 2034 | p_font_data->face_init = false; |
| 2035 | p_font_data->supported_features.clear(); |
| 2036 | p_font_data->supported_varaitions.clear(); |
| 2037 | p_font_data->supported_scripts.clear(); |
| 2038 | } |
| 2039 | |
| 2040 | bool TextServerAdvanced::_font_is_color(const RID &p_font_rid) const { |
| 2041 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2042 | ERR_FAIL_NULL_V(fd, false); |
| 2043 | |
| 2044 | MutexLock lock(fd->mutex); |
| 2045 | Vector2i size = _get_size(fd, 16); |
| 2046 | |
| 2047 | FontForSizeAdvanced *ffsd = nullptr; |
| 2048 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), false); |
| 2049 | #ifdef MODULE_FREETYPE_ENABLED |
| 2050 | return ffsd->face && FT_HAS_COLOR(ffsd->face); |
| 2051 | #else |
| 2052 | return false; |
| 2053 | #endif |
| 2054 | } |
| 2055 | |
| 2056 | hb_font_t *TextServerAdvanced::_font_get_hb_handle(const RID &p_font_rid, int64_t p_size, bool &r_is_color) const { |
| 2057 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2058 | ERR_FAIL_NULL_V(fd, nullptr); |
| 2059 | |
| 2060 | MutexLock lock(fd->mutex); |
| 2061 | Vector2i size = _get_size(fd, p_size); |
| 2062 | |
| 2063 | FontForSizeAdvanced *ffsd = nullptr; |
| 2064 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), nullptr); |
| 2065 | #ifdef MODULE_FREETYPE_ENABLED |
| 2066 | r_is_color = ffsd->face && FT_HAS_COLOR(ffsd->face); |
| 2067 | #else |
| 2068 | r_is_color = false; |
| 2069 | #endif |
| 2070 | |
| 2071 | return ffsd->hb_handle; |
| 2072 | } |
| 2073 | |
| 2074 | RID TextServerAdvanced::_create_font() { |
| 2075 | _THREAD_SAFE_METHOD_ |
| 2076 | |
| 2077 | FontAdvanced *fd = memnew(FontAdvanced); |
| 2078 | |
| 2079 | return font_owner.make_rid(fd); |
| 2080 | } |
| 2081 | |
| 2082 | RID TextServerAdvanced::_create_font_linked_variation(const RID &p_font_rid) { |
| 2083 | _THREAD_SAFE_METHOD_ |
| 2084 | |
| 2085 | RID rid = p_font_rid; |
| 2086 | FontAdvancedLinkedVariation *fdv = font_var_owner.get_or_null(rid); |
| 2087 | if (unlikely(fdv)) { |
| 2088 | rid = fdv->base_font; |
| 2089 | } |
| 2090 | ERR_FAIL_COND_V(!font_owner.owns(rid), RID()); |
| 2091 | |
| 2092 | FontAdvancedLinkedVariation *new_fdv = memnew(FontAdvancedLinkedVariation); |
| 2093 | new_fdv->base_font = rid; |
| 2094 | |
| 2095 | return font_var_owner.make_rid(new_fdv); |
| 2096 | } |
| 2097 | |
| 2098 | void TextServerAdvanced::_font_set_data(const RID &p_font_rid, const PackedByteArray &p_data) { |
| 2099 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2100 | ERR_FAIL_NULL(fd); |
| 2101 | |
| 2102 | MutexLock lock(fd->mutex); |
| 2103 | _font_clear_cache(fd); |
| 2104 | fd->data = p_data; |
| 2105 | fd->data_ptr = fd->data.ptr(); |
| 2106 | fd->data_size = fd->data.size(); |
| 2107 | } |
| 2108 | |
| 2109 | void TextServerAdvanced::_font_set_data_ptr(const RID &p_font_rid, const uint8_t *p_data_ptr, int64_t p_data_size) { |
| 2110 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2111 | ERR_FAIL_NULL(fd); |
| 2112 | |
| 2113 | MutexLock lock(fd->mutex); |
| 2114 | _font_clear_cache(fd); |
| 2115 | fd->data.resize(0); |
| 2116 | fd->data_ptr = p_data_ptr; |
| 2117 | fd->data_size = p_data_size; |
| 2118 | } |
| 2119 | |
| 2120 | void TextServerAdvanced::_font_set_face_index(const RID &p_font_rid, int64_t p_face_index) { |
| 2121 | ERR_FAIL_COND(p_face_index < 0); |
| 2122 | ERR_FAIL_COND(p_face_index >= 0x7FFF); |
| 2123 | |
| 2124 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2125 | ERR_FAIL_NULL(fd); |
| 2126 | |
| 2127 | MutexLock lock(fd->mutex); |
| 2128 | if (fd->face_index != p_face_index) { |
| 2129 | fd->face_index = p_face_index; |
| 2130 | _font_clear_cache(fd); |
| 2131 | } |
| 2132 | } |
| 2133 | |
| 2134 | int64_t TextServerAdvanced::_font_get_face_index(const RID &p_font_rid) const { |
| 2135 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2136 | ERR_FAIL_NULL_V(fd, 0); |
| 2137 | |
| 2138 | MutexLock lock(fd->mutex); |
| 2139 | return fd->face_index; |
| 2140 | } |
| 2141 | |
| 2142 | int64_t TextServerAdvanced::_font_get_face_count(const RID &p_font_rid) const { |
| 2143 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2144 | ERR_FAIL_NULL_V(fd, 0); |
| 2145 | |
| 2146 | MutexLock lock(fd->mutex); |
| 2147 | int face_count = 0; |
| 2148 | |
| 2149 | if (fd->data_ptr && (fd->data_size > 0)) { |
| 2150 | // Init dynamic font. |
| 2151 | #ifdef MODULE_FREETYPE_ENABLED |
| 2152 | int error = 0; |
| 2153 | if (!ft_library) { |
| 2154 | error = FT_Init_FreeType(&ft_library); |
| 2155 | ERR_FAIL_COND_V_MSG(error != 0, false, "FreeType: Error initializing library: '" + String(FT_Error_String(error)) + "'."); |
| 2156 | #ifdef MODULE_SVG_ENABLED |
| 2157 | FT_Property_Set(ft_library, "ot-svg", "svg-hooks", get_tvg_svg_in_ot_hooks()); |
| 2158 | #endif |
| 2159 | } |
| 2160 | |
| 2161 | FT_StreamRec stream; |
| 2162 | memset(&stream, 0, sizeof(FT_StreamRec)); |
| 2163 | stream.base = (unsigned char *)fd->data_ptr; |
| 2164 | stream.size = fd->data_size; |
| 2165 | stream.pos = 0; |
| 2166 | |
| 2167 | FT_Open_Args fargs; |
| 2168 | memset(&fargs, 0, sizeof(FT_Open_Args)); |
| 2169 | fargs.memory_base = (unsigned char *)fd->data_ptr; |
| 2170 | fargs.memory_size = fd->data_size; |
| 2171 | fargs.flags = FT_OPEN_MEMORY; |
| 2172 | fargs.stream = &stream; |
| 2173 | |
| 2174 | MutexLock ftlock(ft_mutex); |
| 2175 | |
| 2176 | FT_Face tmp_face = nullptr; |
| 2177 | error = FT_Open_Face(ft_library, &fargs, -1, &tmp_face); |
| 2178 | if (error == 0) { |
| 2179 | face_count = tmp_face->num_faces; |
| 2180 | FT_Done_Face(tmp_face); |
| 2181 | } |
| 2182 | #endif |
| 2183 | } |
| 2184 | |
| 2185 | return face_count; |
| 2186 | } |
| 2187 | |
| 2188 | void TextServerAdvanced::_font_set_style(const RID &p_font_rid, BitField<FontStyle> p_style) { |
| 2189 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2190 | ERR_FAIL_NULL(fd); |
| 2191 | |
| 2192 | MutexLock lock(fd->mutex); |
| 2193 | Vector2i size = _get_size(fd, 16); |
| 2194 | FontForSizeAdvanced *ffsd = nullptr; |
| 2195 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 2196 | fd->style_flags = p_style; |
| 2197 | } |
| 2198 | |
| 2199 | BitField<TextServer::FontStyle> TextServerAdvanced::_font_get_style(const RID &p_font_rid) const { |
| 2200 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2201 | ERR_FAIL_NULL_V(fd, 0); |
| 2202 | |
| 2203 | MutexLock lock(fd->mutex); |
| 2204 | Vector2i size = _get_size(fd, 16); |
| 2205 | FontForSizeAdvanced *ffsd = nullptr; |
| 2206 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 0); |
| 2207 | return fd->style_flags; |
| 2208 | } |
| 2209 | |
| 2210 | void TextServerAdvanced::_font_set_style_name(const RID &p_font_rid, const String &p_name) { |
| 2211 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2212 | ERR_FAIL_NULL(fd); |
| 2213 | |
| 2214 | MutexLock lock(fd->mutex); |
| 2215 | Vector2i size = _get_size(fd, 16); |
| 2216 | FontForSizeAdvanced *ffsd = nullptr; |
| 2217 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 2218 | fd->style_name = p_name; |
| 2219 | } |
| 2220 | |
| 2221 | String TextServerAdvanced::_font_get_style_name(const RID &p_font_rid) const { |
| 2222 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2223 | ERR_FAIL_NULL_V(fd, String()); |
| 2224 | |
| 2225 | MutexLock lock(fd->mutex); |
| 2226 | Vector2i size = _get_size(fd, 16); |
| 2227 | FontForSizeAdvanced *ffsd = nullptr; |
| 2228 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), String()); |
| 2229 | return fd->style_name; |
| 2230 | } |
| 2231 | |
| 2232 | void TextServerAdvanced::_font_set_weight(const RID &p_font_rid, int64_t p_weight) { |
| 2233 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2234 | ERR_FAIL_NULL(fd); |
| 2235 | |
| 2236 | MutexLock lock(fd->mutex); |
| 2237 | Vector2i size = _get_size(fd, 16); |
| 2238 | FontForSizeAdvanced *ffsd = nullptr; |
| 2239 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 2240 | fd->weight = CLAMP(p_weight, 100, 999); |
| 2241 | } |
| 2242 | |
| 2243 | int64_t TextServerAdvanced::_font_get_weight(const RID &p_font_rid) const { |
| 2244 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2245 | ERR_FAIL_NULL_V(fd, 400); |
| 2246 | |
| 2247 | MutexLock lock(fd->mutex); |
| 2248 | Vector2i size = _get_size(fd, 16); |
| 2249 | FontForSizeAdvanced *ffsd = nullptr; |
| 2250 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 400); |
| 2251 | return fd->weight; |
| 2252 | } |
| 2253 | |
| 2254 | void TextServerAdvanced::_font_set_stretch(const RID &p_font_rid, int64_t p_stretch) { |
| 2255 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2256 | ERR_FAIL_NULL(fd); |
| 2257 | |
| 2258 | MutexLock lock(fd->mutex); |
| 2259 | Vector2i size = _get_size(fd, 16); |
| 2260 | FontForSizeAdvanced *ffsd = nullptr; |
| 2261 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 2262 | fd->stretch = CLAMP(p_stretch, 50, 200); |
| 2263 | } |
| 2264 | |
| 2265 | int64_t TextServerAdvanced::_font_get_stretch(const RID &p_font_rid) const { |
| 2266 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2267 | ERR_FAIL_NULL_V(fd, 100); |
| 2268 | |
| 2269 | MutexLock lock(fd->mutex); |
| 2270 | Vector2i size = _get_size(fd, 16); |
| 2271 | FontForSizeAdvanced *ffsd = nullptr; |
| 2272 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 100); |
| 2273 | return fd->stretch; |
| 2274 | } |
| 2275 | |
| 2276 | void TextServerAdvanced::_font_set_name(const RID &p_font_rid, const String &p_name) { |
| 2277 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2278 | ERR_FAIL_NULL(fd); |
| 2279 | |
| 2280 | MutexLock lock(fd->mutex); |
| 2281 | Vector2i size = _get_size(fd, 16); |
| 2282 | FontForSizeAdvanced *ffsd = nullptr; |
| 2283 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 2284 | fd->font_name = p_name; |
| 2285 | } |
| 2286 | |
| 2287 | String TextServerAdvanced::_font_get_name(const RID &p_font_rid) const { |
| 2288 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2289 | ERR_FAIL_NULL_V(fd, String()); |
| 2290 | |
| 2291 | MutexLock lock(fd->mutex); |
| 2292 | Vector2i size = _get_size(fd, 16); |
| 2293 | FontForSizeAdvanced *ffsd = nullptr; |
| 2294 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), String()); |
| 2295 | return fd->font_name; |
| 2296 | } |
| 2297 | |
| 2298 | Dictionary TextServerAdvanced::_font_get_ot_name_strings(const RID &p_font_rid) const { |
| 2299 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2300 | ERR_FAIL_NULL_V(fd, Dictionary()); |
| 2301 | |
| 2302 | Dictionary out; |
| 2303 | #ifdef MODULE_FREETYPE_ENABLED |
| 2304 | MutexLock lock(fd->mutex); |
| 2305 | Vector2i size = _get_size(fd, 16); |
| 2306 | FontForSizeAdvanced *ffsd = nullptr; |
| 2307 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Dictionary()); |
| 2308 | |
| 2309 | hb_face_t *hb_face = hb_font_get_face(ffsd->hb_handle); |
| 2310 | |
| 2311 | unsigned int num_entries = 0; |
| 2312 | const hb_ot_name_entry_t *names = hb_ot_name_list_names(hb_face, &num_entries); |
| 2313 | HashMap<String, Dictionary> names_for_lang; |
| 2314 | for (unsigned int i = 0; i < num_entries; i++) { |
| 2315 | String name; |
| 2316 | switch (names[i].name_id) { |
| 2317 | case HB_OT_NAME_ID_COPYRIGHT: { |
| 2318 | name = "copyright"; |
| 2319 | } break; |
| 2320 | case HB_OT_NAME_ID_FONT_FAMILY: { |
| 2321 | name = "family_name"; |
| 2322 | } break; |
| 2323 | case HB_OT_NAME_ID_FONT_SUBFAMILY: { |
| 2324 | name = "subfamily_name"; |
| 2325 | } break; |
| 2326 | case HB_OT_NAME_ID_UNIQUE_ID: { |
| 2327 | name = "unique_identifier"; |
| 2328 | } break; |
| 2329 | case HB_OT_NAME_ID_FULL_NAME: { |
| 2330 | name = "full_name"; |
| 2331 | } break; |
| 2332 | case HB_OT_NAME_ID_VERSION_STRING: { |
| 2333 | name = "version"; |
| 2334 | } break; |
| 2335 | case HB_OT_NAME_ID_POSTSCRIPT_NAME: { |
| 2336 | name = "postscript_name"; |
| 2337 | } break; |
| 2338 | case HB_OT_NAME_ID_TRADEMARK: { |
| 2339 | name = "trademark"; |
| 2340 | } break; |
| 2341 | case HB_OT_NAME_ID_MANUFACTURER: { |
| 2342 | name = "manufacturer"; |
| 2343 | } break; |
| 2344 | case HB_OT_NAME_ID_DESIGNER: { |
| 2345 | name = "designer"; |
| 2346 | } break; |
| 2347 | case HB_OT_NAME_ID_DESCRIPTION: { |
| 2348 | name = "description"; |
| 2349 | } break; |
| 2350 | case HB_OT_NAME_ID_VENDOR_URL: { |
| 2351 | name = "vendor_url"; |
| 2352 | } break; |
| 2353 | case HB_OT_NAME_ID_DESIGNER_URL: { |
| 2354 | name = "designer_url"; |
| 2355 | } break; |
| 2356 | case HB_OT_NAME_ID_LICENSE: { |
| 2357 | name = "license"; |
| 2358 | } break; |
| 2359 | case HB_OT_NAME_ID_LICENSE_URL: { |
| 2360 | name = "license_url"; |
| 2361 | } break; |
| 2362 | case HB_OT_NAME_ID_TYPOGRAPHIC_FAMILY: { |
| 2363 | name = "typographic_family_name"; |
| 2364 | } break; |
| 2365 | case HB_OT_NAME_ID_TYPOGRAPHIC_SUBFAMILY: { |
| 2366 | name = "typographic_subfamily_name"; |
| 2367 | } break; |
| 2368 | case HB_OT_NAME_ID_MAC_FULL_NAME: { |
| 2369 | name = "full_name_macos"; |
| 2370 | } break; |
| 2371 | case HB_OT_NAME_ID_SAMPLE_TEXT: { |
| 2372 | name = "sample_text"; |
| 2373 | } break; |
| 2374 | case HB_OT_NAME_ID_CID_FINDFONT_NAME: { |
| 2375 | name = "cid_findfont_name"; |
| 2376 | } break; |
| 2377 | case HB_OT_NAME_ID_WWS_FAMILY: { |
| 2378 | name = "weight_width_slope_family_name"; |
| 2379 | } break; |
| 2380 | case HB_OT_NAME_ID_WWS_SUBFAMILY: { |
| 2381 | name = "weight_width_slope_subfamily_name"; |
| 2382 | } break; |
| 2383 | case HB_OT_NAME_ID_LIGHT_BACKGROUND: { |
| 2384 | name = "light_background_palette"; |
| 2385 | } break; |
| 2386 | case HB_OT_NAME_ID_DARK_BACKGROUND: { |
| 2387 | name = "dark_background_palette"; |
| 2388 | } break; |
| 2389 | case HB_OT_NAME_ID_VARIATIONS_PS_PREFIX: { |
| 2390 | name = "postscript_name_prefix"; |
| 2391 | } break; |
| 2392 | default: { |
| 2393 | name = vformat("unknown_%d", names[i].name_id); |
| 2394 | } break; |
| 2395 | } |
| 2396 | String text; |
| 2397 | unsigned int text_size = hb_ot_name_get_utf32(hb_face, names[i].name_id, names[i].language, nullptr, nullptr) + 1; |
| 2398 | text.resize_uninitialized(text_size); |
| 2399 | hb_ot_name_get_utf32(hb_face, names[i].name_id, names[i].language, &text_size, (uint32_t *)text.ptrw()); |
| 2400 | if (!text.is_empty()) { |
| 2401 | Dictionary &id_string = names_for_lang[String(hb_language_to_string(names[i].language))]; |
| 2402 | id_string[name] = text; |
| 2403 | } |
| 2404 | } |
| 2405 | |
| 2406 | for (const KeyValue<String, Dictionary> &E : names_for_lang) { |
| 2407 | out[E.key] = E.value; |
| 2408 | } |
| 2409 | #endif |
| 2410 | return out; |
| 2411 | } |
| 2412 | |
| 2413 | void TextServerAdvanced::_font_set_antialiasing(const RID &p_font_rid, TextServer::FontAntialiasing p_antialiasing) { |
| 2414 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2415 | ERR_FAIL_NULL(fd); |
| 2416 | |
| 2417 | MutexLock lock(fd->mutex); |
| 2418 | if (fd->antialiasing != p_antialiasing) { |
| 2419 | _font_clear_cache(fd); |
| 2420 | fd->antialiasing = p_antialiasing; |
| 2421 | } |
| 2422 | } |
| 2423 | |
| 2424 | TextServer::FontAntialiasing TextServerAdvanced::_font_get_antialiasing(const RID &p_font_rid) const { |
| 2425 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2426 | ERR_FAIL_NULL_V(fd, TextServer::FONT_ANTIALIASING_NONE); |
| 2427 | |
| 2428 | MutexLock lock(fd->mutex); |
| 2429 | return fd->antialiasing; |
| 2430 | } |
| 2431 | |
| 2432 | void TextServerAdvanced::_font_set_disable_embedded_bitmaps(const RID &p_font_rid, bool p_disable_embedded_bitmaps) { |
| 2433 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2434 | ERR_FAIL_NULL(fd); |
| 2435 | |
| 2436 | MutexLock lock(fd->mutex); |
| 2437 | if (fd->disable_embedded_bitmaps != p_disable_embedded_bitmaps) { |
| 2438 | _font_clear_cache(fd); |
| 2439 | fd->disable_embedded_bitmaps = p_disable_embedded_bitmaps; |
| 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | bool TextServerAdvanced::_font_get_disable_embedded_bitmaps(const RID &p_font_rid) const { |
| 2444 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2445 | ERR_FAIL_NULL_V(fd, false); |
| 2446 | |
| 2447 | MutexLock lock(fd->mutex); |
| 2448 | return fd->disable_embedded_bitmaps; |
| 2449 | } |
| 2450 | |
| 2451 | void TextServerAdvanced::_font_set_generate_mipmaps(const RID &p_font_rid, bool p_generate_mipmaps) { |
| 2452 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2453 | ERR_FAIL_NULL(fd); |
| 2454 | |
| 2455 | MutexLock lock(fd->mutex); |
| 2456 | if (fd->mipmaps != p_generate_mipmaps) { |
| 2457 | for (KeyValue<Vector2i, FontForSizeAdvanced *> &E : fd->cache) { |
| 2458 | for (int i = 0; i < E.value->textures.size(); i++) { |
| 2459 | E.value->textures.write[i].dirty = true; |
| 2460 | E.value->textures.write[i].texture = Ref<ImageTexture>(); |
| 2461 | } |
| 2462 | } |
| 2463 | fd->mipmaps = p_generate_mipmaps; |
| 2464 | } |
| 2465 | } |
| 2466 | |
| 2467 | bool TextServerAdvanced::_font_get_generate_mipmaps(const RID &p_font_rid) const { |
| 2468 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2469 | ERR_FAIL_NULL_V(fd, false); |
| 2470 | |
| 2471 | MutexLock lock(fd->mutex); |
| 2472 | return fd->mipmaps; |
| 2473 | } |
| 2474 | |
| 2475 | void TextServerAdvanced::_font_set_multichannel_signed_distance_field(const RID &p_font_rid, bool p_msdf) { |
| 2476 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2477 | ERR_FAIL_NULL(fd); |
| 2478 | |
| 2479 | MutexLock lock(fd->mutex); |
| 2480 | if (fd->msdf != p_msdf) { |
| 2481 | _font_clear_cache(fd); |
| 2482 | fd->msdf = p_msdf; |
| 2483 | } |
| 2484 | } |
| 2485 | |
| 2486 | bool TextServerAdvanced::_font_is_multichannel_signed_distance_field(const RID &p_font_rid) const { |
| 2487 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2488 | ERR_FAIL_NULL_V(fd, false); |
| 2489 | |
| 2490 | MutexLock lock(fd->mutex); |
| 2491 | return fd->msdf; |
| 2492 | } |
| 2493 | |
| 2494 | void TextServerAdvanced::_font_set_msdf_pixel_range(const RID &p_font_rid, int64_t p_msdf_pixel_range) { |
| 2495 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2496 | ERR_FAIL_NULL(fd); |
| 2497 | |
| 2498 | MutexLock lock(fd->mutex); |
| 2499 | if (fd->msdf_range != p_msdf_pixel_range) { |
| 2500 | _font_clear_cache(fd); |
| 2501 | fd->msdf_range = p_msdf_pixel_range; |
| 2502 | } |
| 2503 | } |
| 2504 | |
| 2505 | int64_t TextServerAdvanced::_font_get_msdf_pixel_range(const RID &p_font_rid) const { |
| 2506 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2507 | ERR_FAIL_NULL_V(fd, false); |
| 2508 | |
| 2509 | MutexLock lock(fd->mutex); |
| 2510 | return fd->msdf_range; |
| 2511 | } |
| 2512 | |
| 2513 | void TextServerAdvanced::_font_set_msdf_size(const RID &p_font_rid, int64_t p_msdf_size) { |
| 2514 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2515 | ERR_FAIL_NULL(fd); |
| 2516 | |
| 2517 | MutexLock lock(fd->mutex); |
| 2518 | if (fd->msdf_source_size != p_msdf_size) { |
| 2519 | _font_clear_cache(fd); |
| 2520 | fd->msdf_source_size = p_msdf_size; |
| 2521 | } |
| 2522 | } |
| 2523 | |
| 2524 | int64_t TextServerAdvanced::_font_get_msdf_size(const RID &p_font_rid) const { |
| 2525 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2526 | ERR_FAIL_NULL_V(fd, 0); |
| 2527 | |
| 2528 | MutexLock lock(fd->mutex); |
| 2529 | return fd->msdf_source_size; |
| 2530 | } |
| 2531 | |
| 2532 | void TextServerAdvanced::_font_set_fixed_size(const RID &p_font_rid, int64_t p_fixed_size) { |
| 2533 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2534 | ERR_FAIL_NULL(fd); |
| 2535 | |
| 2536 | MutexLock lock(fd->mutex); |
| 2537 | fd->fixed_size = p_fixed_size; |
| 2538 | } |
| 2539 | |
| 2540 | int64_t TextServerAdvanced::_font_get_fixed_size(const RID &p_font_rid) const { |
| 2541 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2542 | ERR_FAIL_NULL_V(fd, 0); |
| 2543 | |
| 2544 | MutexLock lock(fd->mutex); |
| 2545 | return fd->fixed_size; |
| 2546 | } |
| 2547 | |
| 2548 | void TextServerAdvanced::_font_set_fixed_size_scale_mode(const RID &p_font_rid, TextServer::FixedSizeScaleMode p_fixed_size_scale_mode) { |
| 2549 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2550 | ERR_FAIL_NULL(fd); |
| 2551 | |
| 2552 | MutexLock lock(fd->mutex); |
| 2553 | fd->fixed_size_scale_mode = p_fixed_size_scale_mode; |
| 2554 | } |
| 2555 | |
| 2556 | TextServer::FixedSizeScaleMode TextServerAdvanced::_font_get_fixed_size_scale_mode(const RID &p_font_rid) const { |
| 2557 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2558 | ERR_FAIL_NULL_V(fd, FIXED_SIZE_SCALE_DISABLE); |
| 2559 | |
| 2560 | MutexLock lock(fd->mutex); |
| 2561 | return fd->fixed_size_scale_mode; |
| 2562 | } |
| 2563 | |
| 2564 | void TextServerAdvanced::_font_set_allow_system_fallback(const RID &p_font_rid, bool p_allow_system_fallback) { |
| 2565 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2566 | ERR_FAIL_NULL(fd); |
| 2567 | |
| 2568 | MutexLock lock(fd->mutex); |
| 2569 | fd->allow_system_fallback = p_allow_system_fallback; |
| 2570 | } |
| 2571 | |
| 2572 | bool TextServerAdvanced::_font_is_allow_system_fallback(const RID &p_font_rid) const { |
| 2573 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2574 | ERR_FAIL_NULL_V(fd, false); |
| 2575 | |
| 2576 | MutexLock lock(fd->mutex); |
| 2577 | return fd->allow_system_fallback; |
| 2578 | } |
| 2579 | |
| 2580 | void TextServerAdvanced::_font_set_force_autohinter(const RID &p_font_rid, bool p_force_autohinter) { |
| 2581 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2582 | ERR_FAIL_NULL(fd); |
| 2583 | |
| 2584 | MutexLock lock(fd->mutex); |
| 2585 | if (fd->force_autohinter != p_force_autohinter) { |
| 2586 | _font_clear_cache(fd); |
| 2587 | fd->force_autohinter = p_force_autohinter; |
| 2588 | } |
| 2589 | } |
| 2590 | |
| 2591 | bool TextServerAdvanced::_font_is_force_autohinter(const RID &p_font_rid) const { |
| 2592 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2593 | ERR_FAIL_NULL_V(fd, false); |
| 2594 | |
| 2595 | MutexLock lock(fd->mutex); |
| 2596 | return fd->force_autohinter; |
| 2597 | } |
| 2598 | |
| 2599 | void TextServerAdvanced::_font_set_modulate_color_glyphs(const RID &p_font_rid, bool p_modulate) { |
| 2600 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2601 | ERR_FAIL_NULL(fd); |
| 2602 | |
| 2603 | MutexLock lock(fd->mutex); |
| 2604 | if (fd->modulate_color_glyphs != p_modulate) { |
| 2605 | fd->modulate_color_glyphs = p_modulate; |
| 2606 | } |
| 2607 | } |
| 2608 | |
| 2609 | bool TextServerAdvanced::_font_is_modulate_color_glyphs(const RID &p_font_rid) const { |
| 2610 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2611 | ERR_FAIL_NULL_V(fd, false); |
| 2612 | |
| 2613 | MutexLock lock(fd->mutex); |
| 2614 | return fd->modulate_color_glyphs; |
| 2615 | } |
| 2616 | |
| 2617 | void TextServerAdvanced::_font_set_hinting(const RID &p_font_rid, TextServer::Hinting p_hinting) { |
| 2618 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2619 | ERR_FAIL_NULL(fd); |
| 2620 | |
| 2621 | MutexLock lock(fd->mutex); |
| 2622 | if (fd->hinting != p_hinting) { |
| 2623 | _font_clear_cache(fd); |
| 2624 | fd->hinting = p_hinting; |
| 2625 | } |
| 2626 | } |
| 2627 | |
| 2628 | TextServer::Hinting TextServerAdvanced::_font_get_hinting(const RID &p_font_rid) const { |
| 2629 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2630 | ERR_FAIL_NULL_V(fd, HINTING_NONE); |
| 2631 | |
| 2632 | MutexLock lock(fd->mutex); |
| 2633 | return fd->hinting; |
| 2634 | } |
| 2635 | |
| 2636 | void TextServerAdvanced::_font_set_subpixel_positioning(const RID &p_font_rid, TextServer::SubpixelPositioning p_subpixel) { |
| 2637 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2638 | ERR_FAIL_NULL(fd); |
| 2639 | |
| 2640 | MutexLock lock(fd->mutex); |
| 2641 | fd->subpixel_positioning = p_subpixel; |
| 2642 | } |
| 2643 | |
| 2644 | TextServer::SubpixelPositioning TextServerAdvanced::_font_get_subpixel_positioning(const RID &p_font_rid) const { |
| 2645 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2646 | ERR_FAIL_NULL_V(fd, SUBPIXEL_POSITIONING_DISABLED); |
| 2647 | |
| 2648 | MutexLock lock(fd->mutex); |
| 2649 | return fd->subpixel_positioning; |
| 2650 | } |
| 2651 | |
| 2652 | void TextServerAdvanced::_font_set_keep_rounding_remainders(const RID &p_font_rid, bool p_keep_rounding_remainders) { |
| 2653 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2654 | ERR_FAIL_NULL(fd); |
| 2655 | |
| 2656 | MutexLock lock(fd->mutex); |
| 2657 | fd->keep_rounding_remainders = p_keep_rounding_remainders; |
| 2658 | } |
| 2659 | |
| 2660 | bool TextServerAdvanced::_font_get_keep_rounding_remainders(const RID &p_font_rid) const { |
| 2661 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2662 | ERR_FAIL_NULL_V(fd, false); |
| 2663 | |
| 2664 | MutexLock lock(fd->mutex); |
| 2665 | return fd->keep_rounding_remainders; |
| 2666 | } |
| 2667 | |
| 2668 | void TextServerAdvanced::_font_set_embolden(const RID &p_font_rid, double p_strength) { |
| 2669 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2670 | ERR_FAIL_NULL(fd); |
| 2671 | |
| 2672 | MutexLock lock(fd->mutex); |
| 2673 | if (fd->embolden != p_strength) { |
| 2674 | _font_clear_cache(fd); |
| 2675 | fd->embolden = p_strength; |
| 2676 | } |
| 2677 | } |
| 2678 | |
| 2679 | double TextServerAdvanced::_font_get_embolden(const RID &p_font_rid) const { |
| 2680 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2681 | ERR_FAIL_NULL_V(fd, 0.0); |
| 2682 | |
| 2683 | MutexLock lock(fd->mutex); |
| 2684 | return fd->embolden; |
| 2685 | } |
| 2686 | |
| 2687 | void TextServerAdvanced::_font_set_spacing(const RID &p_font_rid, SpacingType p_spacing, int64_t p_value) { |
| 2688 | ERR_FAIL_INDEX((int)p_spacing, 4); |
| 2689 | FontAdvancedLinkedVariation *fdv = font_var_owner.get_or_null(p_font_rid); |
| 2690 | if (fdv) { |
| 2691 | if (fdv->extra_spacing[p_spacing] != p_value) { |
| 2692 | fdv->extra_spacing[p_spacing] = p_value; |
| 2693 | } |
| 2694 | } else { |
| 2695 | FontAdvanced *fd = font_owner.get_or_null(p_font_rid); |
| 2696 | ERR_FAIL_NULL(fd); |
| 2697 | |
| 2698 | MutexLock lock(fd->mutex); |
| 2699 | if (fd->extra_spacing[p_spacing] != p_value) { |
| 2700 | fd->extra_spacing[p_spacing] = p_value; |
| 2701 | } |
| 2702 | } |
| 2703 | } |
| 2704 | |
| 2705 | int64_t TextServerAdvanced::_font_get_spacing(const RID &p_font_rid, SpacingType p_spacing) const { |
| 2706 | ERR_FAIL_INDEX_V((int)p_spacing, 4, 0); |
| 2707 | FontAdvancedLinkedVariation *fdv = font_var_owner.get_or_null(p_font_rid); |
| 2708 | if (fdv) { |
| 2709 | return fdv->extra_spacing[p_spacing]; |
| 2710 | } else { |
| 2711 | FontAdvanced *fd = font_owner.get_or_null(p_font_rid); |
| 2712 | ERR_FAIL_NULL_V(fd, 0); |
| 2713 | |
| 2714 | MutexLock lock(fd->mutex); |
| 2715 | return fd->extra_spacing[p_spacing]; |
| 2716 | } |
| 2717 | } |
| 2718 | |
| 2719 | void TextServerAdvanced::_font_set_baseline_offset(const RID &p_font_rid, double p_baseline_offset) { |
| 2720 | FontAdvancedLinkedVariation *fdv = font_var_owner.get_or_null(p_font_rid); |
| 2721 | if (fdv) { |
| 2722 | if (fdv->baseline_offset != p_baseline_offset) { |
| 2723 | fdv->baseline_offset = p_baseline_offset; |
| 2724 | } |
| 2725 | } else { |
| 2726 | FontAdvanced *fd = font_owner.get_or_null(p_font_rid); |
| 2727 | ERR_FAIL_NULL(fd); |
| 2728 | |
| 2729 | MutexLock lock(fd->mutex); |
| 2730 | if (fd->baseline_offset != p_baseline_offset) { |
| 2731 | _font_clear_cache(fd); |
| 2732 | fd->baseline_offset = p_baseline_offset; |
| 2733 | } |
| 2734 | } |
| 2735 | } |
| 2736 | |
| 2737 | double TextServerAdvanced::_font_get_baseline_offset(const RID &p_font_rid) const { |
| 2738 | FontAdvancedLinkedVariation *fdv = font_var_owner.get_or_null(p_font_rid); |
| 2739 | if (fdv) { |
| 2740 | return fdv->baseline_offset; |
| 2741 | } else { |
| 2742 | FontAdvanced *fd = font_owner.get_or_null(p_font_rid); |
| 2743 | ERR_FAIL_NULL_V(fd, 0.0); |
| 2744 | |
| 2745 | MutexLock lock(fd->mutex); |
| 2746 | return fd->baseline_offset; |
| 2747 | } |
| 2748 | } |
| 2749 | |
| 2750 | void TextServerAdvanced::_font_set_transform(const RID &p_font_rid, const Transform2D &p_transform) { |
| 2751 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2752 | ERR_FAIL_NULL(fd); |
| 2753 | |
| 2754 | MutexLock lock(fd->mutex); |
| 2755 | if (fd->transform != p_transform) { |
| 2756 | _font_clear_cache(fd); |
| 2757 | fd->transform = p_transform; |
| 2758 | } |
| 2759 | } |
| 2760 | |
| 2761 | Transform2D TextServerAdvanced::_font_get_transform(const RID &p_font_rid) const { |
| 2762 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2763 | ERR_FAIL_NULL_V(fd, Transform2D()); |
| 2764 | |
| 2765 | MutexLock lock(fd->mutex); |
| 2766 | return fd->transform; |
| 2767 | } |
| 2768 | |
| 2769 | void TextServerAdvanced::_font_set_variation_coordinates(const RID &p_font_rid, const Dictionary &p_variation_coordinates) { |
| 2770 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2771 | ERR_FAIL_NULL(fd); |
| 2772 | |
| 2773 | MutexLock lock(fd->mutex); |
| 2774 | if (!fd->variation_coordinates.recursive_equal(p_variation_coordinates, 1)) { |
| 2775 | _font_clear_cache(fd); |
| 2776 | fd->variation_coordinates = p_variation_coordinates.duplicate(); |
| 2777 | } |
| 2778 | } |
| 2779 | |
| 2780 | double TextServerAdvanced::_font_get_oversampling(const RID &p_font_rid) const { |
| 2781 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2782 | ERR_FAIL_NULL_V(fd, -1.0); |
| 2783 | |
| 2784 | MutexLock lock(fd->mutex); |
| 2785 | return fd->oversampling_override; |
| 2786 | } |
| 2787 | |
| 2788 | void TextServerAdvanced::_font_set_oversampling(const RID &p_font_rid, double p_oversampling) { |
| 2789 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2790 | ERR_FAIL_NULL(fd); |
| 2791 | |
| 2792 | MutexLock lock(fd->mutex); |
| 2793 | if (fd->oversampling_override != p_oversampling) { |
| 2794 | _font_clear_cache(fd); |
| 2795 | fd->oversampling_override = p_oversampling; |
| 2796 | } |
| 2797 | } |
| 2798 | |
| 2799 | Dictionary TextServerAdvanced::_font_get_variation_coordinates(const RID &p_font_rid) const { |
| 2800 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2801 | ERR_FAIL_NULL_V(fd, Dictionary()); |
| 2802 | |
| 2803 | MutexLock lock(fd->mutex); |
| 2804 | return fd->variation_coordinates; |
| 2805 | } |
| 2806 | |
| 2807 | TypedArray<Vector2i> TextServerAdvanced::_font_get_size_cache_list(const RID &p_font_rid) const { |
| 2808 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2809 | ERR_FAIL_NULL_V(fd, TypedArray<Vector2i>()); |
| 2810 | |
| 2811 | MutexLock lock(fd->mutex); |
| 2812 | TypedArray<Vector2i> ret; |
| 2813 | for (const KeyValue<Vector2i, FontForSizeAdvanced *> &E : fd->cache) { |
| 2814 | if ((E.key.x % 64 == 0) && (E.value->viewport_oversampling == 0)) { |
| 2815 | ret.push_back(Vector2i(E.key.x / 64, E.key.y)); |
| 2816 | } |
| 2817 | } |
| 2818 | return ret; |
| 2819 | } |
| 2820 | |
| 2821 | TypedArray<Dictionary> TextServerAdvanced::_font_get_size_cache_info(const RID &p_font_rid) const { |
| 2822 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2823 | ERR_FAIL_NULL_V(fd, TypedArray<Dictionary>()); |
| 2824 | |
| 2825 | MutexLock lock(fd->mutex); |
| 2826 | TypedArray<Dictionary> ret; |
| 2827 | for (const KeyValue<Vector2i, FontForSizeAdvanced *> &E : fd->cache) { |
| 2828 | Dictionary size_info; |
| 2829 | size_info["size_px"] = Vector2i(E.key.x / 64, E.key.y); |
| 2830 | if (E.value->viewport_oversampling) { |
| 2831 | size_info["viewport_oversampling"] = double(E.value->viewport_oversampling) / 64.0; |
| 2832 | } |
| 2833 | size_info["glyphs"] = E.value->glyph_map.size(); |
| 2834 | size_info["textures"] = E.value->textures.size(); |
| 2835 | uint64_t sz = 0; |
| 2836 | for (const ShelfPackTexture &tx : E.value->textures) { |
| 2837 | sz += tx.image->get_data_size() * 2; |
| 2838 | } |
| 2839 | size_info["textures_size"] = sz; |
| 2840 | ret.push_back(size_info); |
| 2841 | } |
| 2842 | |
| 2843 | return ret; |
| 2844 | } |
| 2845 | |
| 2846 | void TextServerAdvanced::_font_clear_size_cache(const RID &p_font_rid) { |
| 2847 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2848 | ERR_FAIL_NULL(fd); |
| 2849 | |
| 2850 | MutexLock lock(fd->mutex); |
| 2851 | MutexLock ftlock(ft_mutex); |
| 2852 | for (const KeyValue<Vector2i, FontForSizeAdvanced *> &E : fd->cache) { |
| 2853 | if (E.value->viewport_oversampling != 0) { |
| 2854 | OversamplingLevel *ol = oversampling_levels.getptr(E.value->viewport_oversampling); |
| 2855 | if (ol) { |
| 2856 | ol->fonts.erase(E.value); |
| 2857 | } |
| 2858 | } |
| 2859 | memdelete(E.value); |
| 2860 | } |
| 2861 | fd->cache.clear(); |
| 2862 | } |
| 2863 | |
| 2864 | void TextServerAdvanced::_font_remove_size_cache(const RID &p_font_rid, const Vector2i &p_size) { |
| 2865 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2866 | ERR_FAIL_NULL(fd); |
| 2867 | |
| 2868 | MutexLock lock(fd->mutex); |
| 2869 | MutexLock ftlock(ft_mutex); |
| 2870 | Vector2i size = Vector2i(p_size.x * 64, p_size.y); |
| 2871 | if (fd->cache.has(size)) { |
| 2872 | if (fd->cache[size]->viewport_oversampling != 0) { |
| 2873 | OversamplingLevel *ol = oversampling_levels.getptr(fd->cache[size]->viewport_oversampling); |
| 2874 | if (ol) { |
| 2875 | ol->fonts.erase(fd->cache[size]); |
| 2876 | } |
| 2877 | } |
| 2878 | memdelete(fd->cache[size]); |
| 2879 | fd->cache.erase(size); |
| 2880 | } |
| 2881 | } |
| 2882 | |
| 2883 | void TextServerAdvanced::_font_set_ascent(const RID &p_font_rid, int64_t p_size, double p_ascent) { |
| 2884 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2885 | ERR_FAIL_NULL(fd); |
| 2886 | |
| 2887 | MutexLock lock(fd->mutex); |
| 2888 | Vector2i size = _get_size(fd, p_size); |
| 2889 | |
| 2890 | FontForSizeAdvanced *ffsd = nullptr; |
| 2891 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 2892 | ffsd->ascent = p_ascent; |
| 2893 | } |
| 2894 | |
| 2895 | double TextServerAdvanced::_font_get_ascent(const RID &p_font_rid, int64_t p_size) const { |
| 2896 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2897 | ERR_FAIL_NULL_V(fd, 0.0); |
| 2898 | |
| 2899 | MutexLock lock(fd->mutex); |
| 2900 | Vector2i size = _get_size(fd, p_size); |
| 2901 | |
| 2902 | FontForSizeAdvanced *ffsd = nullptr; |
| 2903 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 0.0); |
| 2904 | |
| 2905 | if (fd->msdf) { |
| 2906 | return ffsd->ascent * (double)p_size / (double)fd->msdf_source_size; |
| 2907 | } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) { |
| 2908 | if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) { |
| 2909 | return ffsd->ascent * (double)p_size / (double)fd->fixed_size; |
| 2910 | } else { |
| 2911 | return ffsd->ascent * Math::round((double)p_size / (double)fd->fixed_size); |
| 2912 | } |
| 2913 | } else { |
| 2914 | return ffsd->ascent; |
| 2915 | } |
| 2916 | } |
| 2917 | |
| 2918 | void TextServerAdvanced::_font_set_descent(const RID &p_font_rid, int64_t p_size, double p_descent) { |
| 2919 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2920 | ERR_FAIL_NULL(fd); |
| 2921 | |
| 2922 | Vector2i size = _get_size(fd, p_size); |
| 2923 | |
| 2924 | FontForSizeAdvanced *ffsd = nullptr; |
| 2925 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 2926 | ffsd->descent = p_descent; |
| 2927 | } |
| 2928 | |
| 2929 | double TextServerAdvanced::_font_get_descent(const RID &p_font_rid, int64_t p_size) const { |
| 2930 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2931 | ERR_FAIL_NULL_V(fd, 0.0); |
| 2932 | |
| 2933 | MutexLock lock(fd->mutex); |
| 2934 | Vector2i size = _get_size(fd, p_size); |
| 2935 | |
| 2936 | FontForSizeAdvanced *ffsd = nullptr; |
| 2937 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 0.0); |
| 2938 | |
| 2939 | if (fd->msdf) { |
| 2940 | return ffsd->descent * (double)p_size / (double)fd->msdf_source_size; |
| 2941 | } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) { |
| 2942 | if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) { |
| 2943 | return ffsd->descent * (double)p_size / (double)fd->fixed_size; |
| 2944 | } else { |
| 2945 | return ffsd->descent * Math::round((double)p_size / (double)fd->fixed_size); |
| 2946 | } |
| 2947 | } else { |
| 2948 | return ffsd->descent; |
| 2949 | } |
| 2950 | } |
| 2951 | |
| 2952 | void TextServerAdvanced::_font_set_underline_position(const RID &p_font_rid, int64_t p_size, double p_underline_position) { |
| 2953 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2954 | ERR_FAIL_NULL(fd); |
| 2955 | |
| 2956 | MutexLock lock(fd->mutex); |
| 2957 | Vector2i size = _get_size(fd, p_size); |
| 2958 | |
| 2959 | FontForSizeAdvanced *ffsd = nullptr; |
| 2960 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 2961 | ffsd->underline_position = p_underline_position; |
| 2962 | } |
| 2963 | |
| 2964 | double TextServerAdvanced::_font_get_underline_position(const RID &p_font_rid, int64_t p_size) const { |
| 2965 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2966 | ERR_FAIL_NULL_V(fd, 0.0); |
| 2967 | |
| 2968 | MutexLock lock(fd->mutex); |
| 2969 | Vector2i size = _get_size(fd, p_size); |
| 2970 | |
| 2971 | FontForSizeAdvanced *ffsd = nullptr; |
| 2972 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 0.0); |
| 2973 | |
| 2974 | if (fd->msdf) { |
| 2975 | return ffsd->underline_position * (double)p_size / (double)fd->msdf_source_size; |
| 2976 | } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) { |
| 2977 | if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) { |
| 2978 | return ffsd->underline_position * (double)p_size / (double)fd->fixed_size; |
| 2979 | } else { |
| 2980 | return ffsd->underline_position * Math::round((double)p_size / (double)fd->fixed_size); |
| 2981 | } |
| 2982 | } else { |
| 2983 | return ffsd->underline_position; |
| 2984 | } |
| 2985 | } |
| 2986 | |
| 2987 | void TextServerAdvanced::_font_set_underline_thickness(const RID &p_font_rid, int64_t p_size, double p_underline_thickness) { |
| 2988 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 2989 | ERR_FAIL_NULL(fd); |
| 2990 | |
| 2991 | MutexLock lock(fd->mutex); |
| 2992 | Vector2i size = _get_size(fd, p_size); |
| 2993 | |
| 2994 | FontForSizeAdvanced *ffsd = nullptr; |
| 2995 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 2996 | ffsd->underline_thickness = p_underline_thickness; |
| 2997 | } |
| 2998 | |
| 2999 | double TextServerAdvanced::_font_get_underline_thickness(const RID &p_font_rid, int64_t p_size) const { |
| 3000 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3001 | ERR_FAIL_NULL_V(fd, 0.0); |
| 3002 | |
| 3003 | MutexLock lock(fd->mutex); |
| 3004 | Vector2i size = _get_size(fd, p_size); |
| 3005 | |
| 3006 | FontForSizeAdvanced *ffsd = nullptr; |
| 3007 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 0.0); |
| 3008 | |
| 3009 | if (fd->msdf) { |
| 3010 | return ffsd->underline_thickness * (double)p_size / (double)fd->msdf_source_size; |
| 3011 | } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) { |
| 3012 | if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) { |
| 3013 | return ffsd->underline_thickness * (double)p_size / (double)fd->fixed_size; |
| 3014 | } else { |
| 3015 | return ffsd->underline_thickness * Math::round((double)p_size / (double)fd->fixed_size); |
| 3016 | } |
| 3017 | } else { |
| 3018 | return ffsd->underline_thickness; |
| 3019 | } |
| 3020 | } |
| 3021 | |
| 3022 | void TextServerAdvanced::_font_set_scale(const RID &p_font_rid, int64_t p_size, double p_scale) { |
| 3023 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3024 | ERR_FAIL_NULL(fd); |
| 3025 | |
| 3026 | MutexLock lock(fd->mutex); |
| 3027 | Vector2i size = _get_size(fd, p_size); |
| 3028 | |
| 3029 | FontForSizeAdvanced *ffsd = nullptr; |
| 3030 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 3031 | |
| 3032 | #ifdef MODULE_FREETYPE_ENABLED |
| 3033 | if (ffsd->face) { |
| 3034 | return; // Do not override scale for dynamic fonts, it's calculated automatically. |
| 3035 | } |
| 3036 | #endif |
| 3037 | ffsd->scale = p_scale; |
| 3038 | } |
| 3039 | |
| 3040 | double TextServerAdvanced::_font_get_scale(const RID &p_font_rid, int64_t p_size) const { |
| 3041 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3042 | ERR_FAIL_NULL_V(fd, 0.0); |
| 3043 | |
| 3044 | MutexLock lock(fd->mutex); |
| 3045 | Vector2i size = _get_size(fd, p_size); |
| 3046 | |
| 3047 | FontForSizeAdvanced *ffsd = nullptr; |
| 3048 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 0.0); |
| 3049 | |
| 3050 | if (fd->msdf) { |
| 3051 | return ffsd->scale * (double)p_size / (double)fd->msdf_source_size; |
| 3052 | } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) { |
| 3053 | if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) { |
| 3054 | return ffsd->scale * (double)p_size / (double)fd->fixed_size; |
| 3055 | } else { |
| 3056 | return ffsd->scale * Math::round((double)p_size / (double)fd->fixed_size); |
| 3057 | } |
| 3058 | } else { |
| 3059 | return ffsd->scale; |
| 3060 | } |
| 3061 | } |
| 3062 | |
| 3063 | int64_t TextServerAdvanced::_font_get_texture_count(const RID &p_font_rid, const Vector2i &p_size) const { |
| 3064 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3065 | ERR_FAIL_NULL_V(fd, 0); |
| 3066 | |
| 3067 | MutexLock lock(fd->mutex); |
| 3068 | Vector2i size = _get_size_outline(fd, p_size); |
| 3069 | |
| 3070 | FontForSizeAdvanced *ffsd = nullptr; |
| 3071 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 0); |
| 3072 | |
| 3073 | return ffsd->textures.size(); |
| 3074 | } |
| 3075 | |
| 3076 | void TextServerAdvanced::_font_clear_textures(const RID &p_font_rid, const Vector2i &p_size) { |
| 3077 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3078 | ERR_FAIL_NULL(fd); |
| 3079 | MutexLock lock(fd->mutex); |
| 3080 | Vector2i size = _get_size_outline(fd, p_size); |
| 3081 | |
| 3082 | FontForSizeAdvanced *ffsd = nullptr; |
| 3083 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 3084 | ffsd->textures.clear(); |
| 3085 | } |
| 3086 | |
| 3087 | void TextServerAdvanced::_font_remove_texture(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) { |
| 3088 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3089 | ERR_FAIL_NULL(fd); |
| 3090 | |
| 3091 | MutexLock lock(fd->mutex); |
| 3092 | Vector2i size = _get_size_outline(fd, p_size); |
| 3093 | FontForSizeAdvanced *ffsd = nullptr; |
| 3094 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 3095 | ERR_FAIL_INDEX(p_texture_index, ffsd->textures.size()); |
| 3096 | |
| 3097 | ffsd->textures.remove_at(p_texture_index); |
| 3098 | } |
| 3099 | |
| 3100 | void TextServerAdvanced::_font_set_texture_image(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index, const Ref<Image> &p_image) { |
| 3101 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3102 | ERR_FAIL_NULL(fd); |
| 3103 | ERR_FAIL_COND(p_image.is_null()); |
| 3104 | |
| 3105 | MutexLock lock(fd->mutex); |
| 3106 | Vector2i size = _get_size_outline(fd, p_size); |
| 3107 | FontForSizeAdvanced *ffsd = nullptr; |
| 3108 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 3109 | ERR_FAIL_COND(p_texture_index < 0); |
| 3110 | if (p_texture_index >= ffsd->textures.size()) { |
| 3111 | ffsd->textures.resize(p_texture_index + 1); |
| 3112 | } |
| 3113 | |
| 3114 | ShelfPackTexture &tex = ffsd->textures.write[p_texture_index]; |
| 3115 | |
| 3116 | tex.image = p_image; |
| 3117 | tex.texture_w = p_image->get_width(); |
| 3118 | tex.texture_h = p_image->get_height(); |
| 3119 | |
| 3120 | Ref<Image> img = p_image; |
| 3121 | if (fd->mipmaps && !img->has_mipmaps()) { |
| 3122 | img = p_image->duplicate(); |
| 3123 | img->generate_mipmaps(); |
| 3124 | } |
| 3125 | tex.texture = ImageTexture::create_from_image(img); |
| 3126 | tex.dirty = false; |
| 3127 | } |
| 3128 | |
| 3129 | Ref<Image> TextServerAdvanced::_font_get_texture_image(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) const { |
| 3130 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3131 | ERR_FAIL_NULL_V(fd, Ref<Image>()); |
| 3132 | |
| 3133 | MutexLock lock(fd->mutex); |
| 3134 | Vector2i size = _get_size_outline(fd, p_size); |
| 3135 | FontForSizeAdvanced *ffsd = nullptr; |
| 3136 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Ref<Image>()); |
| 3137 | ERR_FAIL_INDEX_V(p_texture_index, ffsd->textures.size(), Ref<Image>()); |
| 3138 | |
| 3139 | const ShelfPackTexture &tex = ffsd->textures[p_texture_index]; |
| 3140 | return tex.image; |
| 3141 | } |
| 3142 | |
| 3143 | void TextServerAdvanced::_font_set_texture_offsets(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index, const PackedInt32Array &p_offsets) { |
| 3144 | ERR_FAIL_COND(p_offsets.size() % 4 != 0); |
| 3145 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3146 | ERR_FAIL_NULL(fd); |
| 3147 | |
| 3148 | MutexLock lock(fd->mutex); |
| 3149 | Vector2i size = _get_size_outline(fd, p_size); |
| 3150 | FontForSizeAdvanced *ffsd = nullptr; |
| 3151 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 3152 | ERR_FAIL_COND(p_texture_index < 0); |
| 3153 | if (p_texture_index >= ffsd->textures.size()) { |
| 3154 | ffsd->textures.resize(p_texture_index + 1); |
| 3155 | } |
| 3156 | |
| 3157 | ShelfPackTexture &tex = ffsd->textures.write[p_texture_index]; |
| 3158 | tex.shelves.clear(); |
| 3159 | for (int32_t i = 0; i < p_offsets.size(); i += 4) { |
| 3160 | tex.shelves.push_back(Shelf(p_offsets[i], p_offsets[i + 1], p_offsets[i + 2], p_offsets[i + 3])); |
| 3161 | } |
| 3162 | } |
| 3163 | |
| 3164 | PackedInt32Array TextServerAdvanced::_font_get_texture_offsets(const RID &p_font_rid, const Vector2i &p_size, int64_t p_texture_index) const { |
| 3165 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3166 | ERR_FAIL_NULL_V(fd, PackedInt32Array()); |
| 3167 | |
| 3168 | MutexLock lock(fd->mutex); |
| 3169 | Vector2i size = _get_size_outline(fd, p_size); |
| 3170 | FontForSizeAdvanced *ffsd = nullptr; |
| 3171 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), PackedInt32Array()); |
| 3172 | ERR_FAIL_INDEX_V(p_texture_index, ffsd->textures.size(), PackedInt32Array()); |
| 3173 | |
| 3174 | const ShelfPackTexture &tex = ffsd->textures[p_texture_index]; |
| 3175 | PackedInt32Array ret; |
| 3176 | ret.resize(tex.shelves.size() * 4); |
| 3177 | |
| 3178 | int32_t *wr = ret.ptrw(); |
| 3179 | int32_t i = 0; |
| 3180 | for (const Shelf &E : tex.shelves) { |
| 3181 | wr[i * 4] = E.x; |
| 3182 | wr[i * 4 + 1] = E.y; |
| 3183 | wr[i * 4 + 2] = E.w; |
| 3184 | wr[i * 4 + 3] = E.h; |
| 3185 | i++; |
| 3186 | } |
| 3187 | return ret; |
| 3188 | } |
| 3189 | |
| 3190 | PackedInt32Array TextServerAdvanced::_font_get_glyph_list(const RID &p_font_rid, const Vector2i &p_size) const { |
| 3191 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3192 | ERR_FAIL_NULL_V(fd, PackedInt32Array()); |
| 3193 | |
| 3194 | MutexLock lock(fd->mutex); |
| 3195 | Vector2i size = _get_size_outline(fd, p_size); |
| 3196 | FontForSizeAdvanced *ffsd = nullptr; |
| 3197 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), PackedInt32Array()); |
| 3198 | |
| 3199 | PackedInt32Array ret; |
| 3200 | const HashMap<int32_t, FontGlyph> &gl = ffsd->glyph_map; |
| 3201 | for (const KeyValue<int32_t, FontGlyph> &E : gl) { |
| 3202 | ret.push_back(E.key); |
| 3203 | } |
| 3204 | return ret; |
| 3205 | } |
| 3206 | |
| 3207 | void TextServerAdvanced::_font_clear_glyphs(const RID &p_font_rid, const Vector2i &p_size) { |
| 3208 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3209 | ERR_FAIL_NULL(fd); |
| 3210 | |
| 3211 | MutexLock lock(fd->mutex); |
| 3212 | Vector2i size = _get_size_outline(fd, p_size); |
| 3213 | FontForSizeAdvanced *ffsd = nullptr; |
| 3214 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 3215 | |
| 3216 | ffsd->glyph_map.clear(); |
| 3217 | } |
| 3218 | |
| 3219 | void TextServerAdvanced::_font_remove_glyph(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) { |
| 3220 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3221 | ERR_FAIL_NULL(fd); |
| 3222 | |
| 3223 | MutexLock lock(fd->mutex); |
| 3224 | Vector2i size = _get_size_outline(fd, p_size); |
| 3225 | FontForSizeAdvanced *ffsd = nullptr; |
| 3226 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 3227 | |
| 3228 | ffsd->glyph_map.erase(p_glyph); |
| 3229 | } |
| 3230 | |
| 3231 | double TextServerAdvanced::_get_extra_advance(RID p_font_rid, int p_font_size) const { |
| 3232 | const FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3233 | ERR_FAIL_NULL_V(fd, 0.0); |
| 3234 | |
| 3235 | MutexLock lock(fd->mutex); |
| 3236 | Vector2i size = _get_size(fd, p_font_size); |
| 3237 | |
| 3238 | if (fd->embolden != 0.0) { |
| 3239 | return fd->embolden * double(size.x) / 4096.0; |
| 3240 | } else { |
| 3241 | return 0.0; |
| 3242 | } |
| 3243 | } |
| 3244 | |
| 3245 | Vector2 TextServerAdvanced::_font_get_glyph_advance(const RID &p_font_rid, int64_t p_size, int64_t p_glyph) const { |
| 3246 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3247 | ERR_FAIL_NULL_V(fd, Vector2()); |
| 3248 | |
| 3249 | MutexLock lock(fd->mutex); |
| 3250 | Vector2i size = _get_size(fd, p_size); |
| 3251 | |
| 3252 | FontForSizeAdvanced *ffsd = nullptr; |
| 3253 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Vector2()); |
| 3254 | |
| 3255 | int mod = 0; |
| 3256 | if (fd->antialiasing == FONT_ANTIALIASING_LCD) { |
| 3257 | TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get(); |
| 3258 | if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) { |
| 3259 | mod = (layout << 24); |
| 3260 | } |
| 3261 | } |
| 3262 | |
| 3263 | FontGlyph fgl; |
| 3264 | if (!_ensure_glyph(fd, size, p_glyph | mod, fgl)) { |
| 3265 | return Vector2(); // Invalid or non graphicl glyph, do not display errors. |
| 3266 | } |
| 3267 | |
| 3268 | Vector2 ea; |
| 3269 | if (fd->embolden != 0.0) { |
| 3270 | ea.x = fd->embolden * double(size.x) / 4096.0; |
| 3271 | } |
| 3272 | |
| 3273 | double scale = _font_get_scale(p_font_rid, p_size); |
| 3274 | if (fd->msdf) { |
| 3275 | return (fgl.advance + ea) * (double)p_size / (double)fd->msdf_source_size; |
| 3276 | } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) { |
| 3277 | if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) { |
| 3278 | return (fgl.advance + ea) * (double)p_size / (double)fd->fixed_size; |
| 3279 | } else { |
| 3280 | return (fgl.advance + ea) * Math::round((double)p_size / (double)fd->fixed_size); |
| 3281 | } |
| 3282 | } else if ((scale == 1.0) && ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_DISABLED) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x > SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE * 64))) { |
| 3283 | return (fgl.advance + ea).round(); |
| 3284 | } else { |
| 3285 | return fgl.advance + ea; |
| 3286 | } |
| 3287 | } |
| 3288 | |
| 3289 | void TextServerAdvanced::_font_set_glyph_advance(const RID &p_font_rid, int64_t p_size, int64_t p_glyph, const Vector2 &p_advance) { |
| 3290 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3291 | ERR_FAIL_NULL(fd); |
| 3292 | |
| 3293 | MutexLock lock(fd->mutex); |
| 3294 | Vector2i size = _get_size(fd, p_size); |
| 3295 | |
| 3296 | FontForSizeAdvanced *ffsd = nullptr; |
| 3297 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 3298 | |
| 3299 | FontGlyph &fgl = ffsd->glyph_map[p_glyph]; |
| 3300 | |
| 3301 | fgl.advance = p_advance; |
| 3302 | fgl.found = true; |
| 3303 | } |
| 3304 | |
| 3305 | Vector2 TextServerAdvanced::_font_get_glyph_offset(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const { |
| 3306 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3307 | ERR_FAIL_NULL_V(fd, Vector2()); |
| 3308 | |
| 3309 | MutexLock lock(fd->mutex); |
| 3310 | Vector2i size = _get_size_outline(fd, p_size); |
| 3311 | |
| 3312 | FontForSizeAdvanced *ffsd = nullptr; |
| 3313 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Vector2()); |
| 3314 | |
| 3315 | int mod = 0; |
| 3316 | if (fd->antialiasing == FONT_ANTIALIASING_LCD) { |
| 3317 | TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get(); |
| 3318 | if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) { |
| 3319 | mod = (layout << 24); |
| 3320 | } |
| 3321 | } |
| 3322 | |
| 3323 | FontGlyph fgl; |
| 3324 | if (!_ensure_glyph(fd, size, p_glyph | mod, fgl)) { |
| 3325 | return Vector2(); // Invalid or non graphicl glyph, do not display errors. |
| 3326 | } |
| 3327 | |
| 3328 | if (fd->msdf) { |
| 3329 | return fgl.rect.position * (double)p_size.x / (double)fd->msdf_source_size; |
| 3330 | } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size.x * 64) { |
| 3331 | if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) { |
| 3332 | return fgl.rect.position * (double)p_size.x / (double)fd->fixed_size; |
| 3333 | } else { |
| 3334 | return fgl.rect.position * Math::round((double)p_size.x / (double)fd->fixed_size); |
| 3335 | } |
| 3336 | } else { |
| 3337 | return fgl.rect.position; |
| 3338 | } |
| 3339 | } |
| 3340 | |
| 3341 | void TextServerAdvanced::_font_set_glyph_offset(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Vector2 &p_offset) { |
| 3342 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3343 | ERR_FAIL_NULL(fd); |
| 3344 | |
| 3345 | MutexLock lock(fd->mutex); |
| 3346 | Vector2i size = _get_size_outline(fd, p_size); |
| 3347 | |
| 3348 | FontForSizeAdvanced *ffsd = nullptr; |
| 3349 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 3350 | |
| 3351 | FontGlyph &fgl = ffsd->glyph_map[p_glyph]; |
| 3352 | |
| 3353 | fgl.rect.position = p_offset; |
| 3354 | fgl.found = true; |
| 3355 | } |
| 3356 | |
| 3357 | Vector2 TextServerAdvanced::_font_get_glyph_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const { |
| 3358 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3359 | ERR_FAIL_NULL_V(fd, Vector2()); |
| 3360 | |
| 3361 | MutexLock lock(fd->mutex); |
| 3362 | Vector2i size = _get_size_outline(fd, p_size); |
| 3363 | |
| 3364 | FontForSizeAdvanced *ffsd = nullptr; |
| 3365 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Vector2()); |
| 3366 | |
| 3367 | int mod = 0; |
| 3368 | if (fd->antialiasing == FONT_ANTIALIASING_LCD) { |
| 3369 | TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get(); |
| 3370 | if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) { |
| 3371 | mod = (layout << 24); |
| 3372 | } |
| 3373 | } |
| 3374 | |
| 3375 | FontGlyph fgl; |
| 3376 | if (!_ensure_glyph(fd, size, p_glyph | mod, fgl)) { |
| 3377 | return Vector2(); // Invalid or non graphicl glyph, do not display errors. |
| 3378 | } |
| 3379 | |
| 3380 | if (fd->msdf) { |
| 3381 | return fgl.rect.size * (double)p_size.x / (double)fd->msdf_source_size; |
| 3382 | } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size.x * 64) { |
| 3383 | if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) { |
| 3384 | return fgl.rect.size * (double)p_size.x / (double)fd->fixed_size; |
| 3385 | } else { |
| 3386 | return fgl.rect.size * Math::round((double)p_size.x / (double)fd->fixed_size); |
| 3387 | } |
| 3388 | } else { |
| 3389 | return fgl.rect.size; |
| 3390 | } |
| 3391 | } |
| 3392 | |
| 3393 | void TextServerAdvanced::_font_set_glyph_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Vector2 &p_gl_size) { |
| 3394 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3395 | ERR_FAIL_NULL(fd); |
| 3396 | |
| 3397 | MutexLock lock(fd->mutex); |
| 3398 | Vector2i size = _get_size_outline(fd, p_size); |
| 3399 | |
| 3400 | FontForSizeAdvanced *ffsd = nullptr; |
| 3401 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 3402 | |
| 3403 | FontGlyph &fgl = ffsd->glyph_map[p_glyph]; |
| 3404 | |
| 3405 | fgl.rect.size = p_gl_size; |
| 3406 | fgl.found = true; |
| 3407 | } |
| 3408 | |
| 3409 | Rect2 TextServerAdvanced::_font_get_glyph_uv_rect(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const { |
| 3410 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3411 | ERR_FAIL_NULL_V(fd, Rect2()); |
| 3412 | |
| 3413 | MutexLock lock(fd->mutex); |
| 3414 | Vector2i size = _get_size_outline(fd, p_size); |
| 3415 | |
| 3416 | FontForSizeAdvanced *ffsd = nullptr; |
| 3417 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Rect2()); |
| 3418 | |
| 3419 | int mod = 0; |
| 3420 | if (fd->antialiasing == FONT_ANTIALIASING_LCD) { |
| 3421 | TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get(); |
| 3422 | if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) { |
| 3423 | mod = (layout << 24); |
| 3424 | } |
| 3425 | } |
| 3426 | |
| 3427 | FontGlyph fgl; |
| 3428 | if (!_ensure_glyph(fd, size, p_glyph | mod, fgl)) { |
| 3429 | return Rect2(); // Invalid or non graphicl glyph, do not display errors. |
| 3430 | } |
| 3431 | |
| 3432 | return fgl.uv_rect; |
| 3433 | } |
| 3434 | |
| 3435 | void TextServerAdvanced::_font_set_glyph_uv_rect(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, const Rect2 &p_uv_rect) { |
| 3436 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3437 | ERR_FAIL_NULL(fd); |
| 3438 | |
| 3439 | MutexLock lock(fd->mutex); |
| 3440 | Vector2i size = _get_size_outline(fd, p_size); |
| 3441 | |
| 3442 | FontForSizeAdvanced *ffsd = nullptr; |
| 3443 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 3444 | |
| 3445 | FontGlyph &fgl = ffsd->glyph_map[p_glyph]; |
| 3446 | |
| 3447 | fgl.uv_rect = p_uv_rect; |
| 3448 | fgl.found = true; |
| 3449 | } |
| 3450 | |
| 3451 | int64_t TextServerAdvanced::_font_get_glyph_texture_idx(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const { |
| 3452 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3453 | ERR_FAIL_NULL_V(fd, -1); |
| 3454 | |
| 3455 | MutexLock lock(fd->mutex); |
| 3456 | Vector2i size = _get_size_outline(fd, p_size); |
| 3457 | |
| 3458 | FontForSizeAdvanced *ffsd = nullptr; |
| 3459 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), -1); |
| 3460 | |
| 3461 | int mod = 0; |
| 3462 | if (fd->antialiasing == FONT_ANTIALIASING_LCD) { |
| 3463 | TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get(); |
| 3464 | if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) { |
| 3465 | mod = (layout << 24); |
| 3466 | } |
| 3467 | } |
| 3468 | |
| 3469 | FontGlyph fgl; |
| 3470 | if (!_ensure_glyph(fd, size, p_glyph | mod, fgl)) { |
| 3471 | return -1; // Invalid or non graphicl glyph, do not display errors. |
| 3472 | } |
| 3473 | |
| 3474 | return fgl.texture_idx; |
| 3475 | } |
| 3476 | |
| 3477 | void TextServerAdvanced::_font_set_glyph_texture_idx(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph, int64_t p_texture_idx) { |
| 3478 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3479 | ERR_FAIL_NULL(fd); |
| 3480 | |
| 3481 | MutexLock lock(fd->mutex); |
| 3482 | Vector2i size = _get_size_outline(fd, p_size); |
| 3483 | |
| 3484 | FontForSizeAdvanced *ffsd = nullptr; |
| 3485 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 3486 | |
| 3487 | FontGlyph &fgl = ffsd->glyph_map[p_glyph]; |
| 3488 | |
| 3489 | fgl.texture_idx = p_texture_idx; |
| 3490 | fgl.found = true; |
| 3491 | } |
| 3492 | |
| 3493 | RID TextServerAdvanced::_font_get_glyph_texture_rid(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const { |
| 3494 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3495 | ERR_FAIL_NULL_V(fd, RID()); |
| 3496 | |
| 3497 | MutexLock lock(fd->mutex); |
| 3498 | Vector2i size = _get_size_outline(fd, p_size); |
| 3499 | |
| 3500 | FontForSizeAdvanced *ffsd = nullptr; |
| 3501 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), RID()); |
| 3502 | |
| 3503 | int mod = 0; |
| 3504 | if (fd->antialiasing == FONT_ANTIALIASING_LCD) { |
| 3505 | TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get(); |
| 3506 | if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) { |
| 3507 | mod = (layout << 24); |
| 3508 | } |
| 3509 | } |
| 3510 | |
| 3511 | FontGlyph fgl; |
| 3512 | if (!_ensure_glyph(fd, size, p_glyph | mod, fgl)) { |
| 3513 | return RID(); // Invalid or non graphicl glyph, do not display errors. |
| 3514 | } |
| 3515 | |
| 3516 | ERR_FAIL_COND_V(fgl.texture_idx < -1 || fgl.texture_idx >= ffsd->textures.size(), RID()); |
| 3517 | |
| 3518 | if (RenderingServer::get_singleton() != nullptr) { |
| 3519 | if (fgl.texture_idx != -1) { |
| 3520 | if (ffsd->textures[fgl.texture_idx].dirty) { |
| 3521 | ShelfPackTexture &tex = ffsd->textures.write[fgl.texture_idx]; |
| 3522 | Ref<Image> img = tex.image; |
| 3523 | if (fgl.from_svg) { |
| 3524 | // Same as the "fix alpha border" process option when importing SVGs |
| 3525 | img->fix_alpha_edges(); |
| 3526 | } |
| 3527 | if (fd->mipmaps && !img->has_mipmaps()) { |
| 3528 | img = tex.image->duplicate(); |
| 3529 | img->generate_mipmaps(); |
| 3530 | } |
| 3531 | if (tex.texture.is_null()) { |
| 3532 | tex.texture = ImageTexture::create_from_image(img); |
| 3533 | } else { |
| 3534 | tex.texture->update(img); |
| 3535 | } |
| 3536 | tex.dirty = false; |
| 3537 | } |
| 3538 | return ffsd->textures[fgl.texture_idx].texture->get_rid(); |
| 3539 | } |
| 3540 | } |
| 3541 | |
| 3542 | return RID(); |
| 3543 | } |
| 3544 | |
| 3545 | Size2 TextServerAdvanced::_font_get_glyph_texture_size(const RID &p_font_rid, const Vector2i &p_size, int64_t p_glyph) const { |
| 3546 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3547 | ERR_FAIL_NULL_V(fd, Size2()); |
| 3548 | |
| 3549 | MutexLock lock(fd->mutex); |
| 3550 | Vector2i size = _get_size_outline(fd, p_size); |
| 3551 | |
| 3552 | FontForSizeAdvanced *ffsd = nullptr; |
| 3553 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Size2()); |
| 3554 | |
| 3555 | int mod = 0; |
| 3556 | if (fd->antialiasing == FONT_ANTIALIASING_LCD) { |
| 3557 | TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get(); |
| 3558 | if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) { |
| 3559 | mod = (layout << 24); |
| 3560 | } |
| 3561 | } |
| 3562 | |
| 3563 | FontGlyph fgl; |
| 3564 | if (!_ensure_glyph(fd, size, p_glyph | mod, fgl)) { |
| 3565 | return Size2(); // Invalid or non graphicl glyph, do not display errors. |
| 3566 | } |
| 3567 | |
| 3568 | ERR_FAIL_COND_V(fgl.texture_idx < -1 || fgl.texture_idx >= ffsd->textures.size(), Size2()); |
| 3569 | |
| 3570 | if (RenderingServer::get_singleton() != nullptr) { |
| 3571 | if (fgl.texture_idx != -1) { |
| 3572 | if (ffsd->textures[fgl.texture_idx].dirty) { |
| 3573 | ShelfPackTexture &tex = ffsd->textures.write[fgl.texture_idx]; |
| 3574 | Ref<Image> img = tex.image; |
| 3575 | if (fgl.from_svg) { |
| 3576 | // Same as the "fix alpha border" process option when importing SVGs |
| 3577 | img->fix_alpha_edges(); |
| 3578 | } |
| 3579 | if (fd->mipmaps && !img->has_mipmaps()) { |
| 3580 | img = tex.image->duplicate(); |
| 3581 | img->generate_mipmaps(); |
| 3582 | } |
| 3583 | if (tex.texture.is_null()) { |
| 3584 | tex.texture = ImageTexture::create_from_image(img); |
| 3585 | } else { |
| 3586 | tex.texture->update(img); |
| 3587 | } |
| 3588 | tex.dirty = false; |
| 3589 | } |
| 3590 | return ffsd->textures[fgl.texture_idx].texture->get_size(); |
| 3591 | } |
| 3592 | } |
| 3593 | |
| 3594 | return Size2(); |
| 3595 | } |
| 3596 | |
| 3597 | Dictionary TextServerAdvanced::_font_get_glyph_contours(const RID &p_font_rid, int64_t p_size, int64_t p_index) const { |
| 3598 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3599 | ERR_FAIL_NULL_V(fd, Dictionary()); |
| 3600 | |
| 3601 | MutexLock lock(fd->mutex); |
| 3602 | Vector2i size = _get_size(fd, p_size); |
| 3603 | |
| 3604 | FontForSizeAdvanced *ffsd = nullptr; |
| 3605 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Dictionary()); |
| 3606 | |
| 3607 | #ifdef MODULE_FREETYPE_ENABLED |
| 3608 | PackedVector3Array points; |
| 3609 | PackedInt32Array contours; |
| 3610 | |
| 3611 | int32_t index = p_index & 0xffffff; // Remove subpixel shifts. |
| 3612 | |
| 3613 | int error = FT_Load_Glyph(ffsd->face, index, FT_LOAD_NO_BITMAP | (fd->force_autohinter ? FT_LOAD_FORCE_AUTOHINT : 0)); |
| 3614 | ERR_FAIL_COND_V(error, Dictionary()); |
| 3615 | |
| 3616 | if (fd->embolden != 0.f) { |
| 3617 | FT_Pos strength = fd->embolden * size.x / 16; // 26.6 fractional units (1 / 64). |
| 3618 | FT_Outline_Embolden(&ffsd->face->glyph->outline, strength); |
| 3619 | } |
| 3620 | |
| 3621 | if (fd->transform != Transform2D()) { |
| 3622 | FT_Matrix mat = { FT_Fixed(fd->transform[0][0] * 65536), FT_Fixed(fd->transform[0][1] * 65536), FT_Fixed(fd->transform[1][0] * 65536), FT_Fixed(fd->transform[1][1] * 65536) }; // 16.16 fractional units (1 / 65536). |
| 3623 | FT_Outline_Transform(&ffsd->face->glyph->outline, &mat); |
| 3624 | } |
| 3625 | |
| 3626 | double scale = (1.0 / 64.0) * ffsd->scale; |
| 3627 | if (fd->msdf) { |
| 3628 | scale = scale * (double)p_size / (double)fd->msdf_source_size; |
| 3629 | } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) { |
| 3630 | if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) { |
| 3631 | scale = scale * (double)p_size / (double)fd->fixed_size; |
| 3632 | } else { |
| 3633 | scale = scale * Math::round((double)p_size / (double)fd->fixed_size); |
| 3634 | } |
| 3635 | } |
| 3636 | for (short i = 0; i < ffsd->face->glyph->outline.n_points; i++) { |
| 3637 | points.push_back(Vector3(ffsd->face->glyph->outline.points[i].x * scale, -ffsd->face->glyph->outline.points[i].y * scale, FT_CURVE_TAG(ffsd->face->glyph->outline.tags[i]))); |
| 3638 | } |
| 3639 | for (short i = 0; i < ffsd->face->glyph->outline.n_contours; i++) { |
| 3640 | contours.push_back(ffsd->face->glyph->outline.contours[i]); |
| 3641 | } |
| 3642 | bool orientation = (FT_Outline_Get_Orientation(&ffsd->face->glyph->outline) == FT_ORIENTATION_FILL_RIGHT); |
| 3643 | |
| 3644 | Dictionary out; |
| 3645 | out["points"] = points; |
| 3646 | out["contours"] = contours; |
| 3647 | out["orientation"] = orientation; |
| 3648 | return out; |
| 3649 | #else |
| 3650 | return Dictionary(); |
| 3651 | #endif |
| 3652 | } |
| 3653 | |
| 3654 | TypedArray<Vector2i> TextServerAdvanced::_font_get_kerning_list(const RID &p_font_rid, int64_t p_size) const { |
| 3655 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3656 | ERR_FAIL_NULL_V(fd, TypedArray<Vector2i>()); |
| 3657 | |
| 3658 | MutexLock lock(fd->mutex); |
| 3659 | Vector2i size = _get_size(fd, p_size); |
| 3660 | |
| 3661 | FontForSizeAdvanced *ffsd = nullptr; |
| 3662 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), TypedArray<Vector2i>()); |
| 3663 | |
| 3664 | TypedArray<Vector2i> ret; |
| 3665 | for (const KeyValue<Vector2i, Vector2> &E : fd->cache[size]->kerning_map) { |
| 3666 | ret.push_back(E.key); |
| 3667 | } |
| 3668 | return ret; |
| 3669 | } |
| 3670 | |
| 3671 | void TextServerAdvanced::_font_clear_kerning_map(const RID &p_font_rid, int64_t p_size) { |
| 3672 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3673 | ERR_FAIL_NULL(fd); |
| 3674 | |
| 3675 | MutexLock lock(fd->mutex); |
| 3676 | Vector2i size = _get_size(fd, p_size); |
| 3677 | |
| 3678 | FontForSizeAdvanced *ffsd = nullptr; |
| 3679 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 3680 | ffsd->kerning_map.clear(); |
| 3681 | } |
| 3682 | |
| 3683 | void TextServerAdvanced::_font_remove_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair) { |
| 3684 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3685 | ERR_FAIL_NULL(fd); |
| 3686 | |
| 3687 | MutexLock lock(fd->mutex); |
| 3688 | Vector2i size = _get_size(fd, p_size); |
| 3689 | |
| 3690 | FontForSizeAdvanced *ffsd = nullptr; |
| 3691 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 3692 | ffsd->kerning_map.erase(p_glyph_pair); |
| 3693 | } |
| 3694 | |
| 3695 | void TextServerAdvanced::_font_set_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair, const Vector2 &p_kerning) { |
| 3696 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3697 | ERR_FAIL_NULL(fd); |
| 3698 | |
| 3699 | MutexLock lock(fd->mutex); |
| 3700 | Vector2i size = _get_size(fd, p_size); |
| 3701 | |
| 3702 | FontForSizeAdvanced *ffsd = nullptr; |
| 3703 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 3704 | ffsd->kerning_map[p_glyph_pair] = p_kerning; |
| 3705 | } |
| 3706 | |
| 3707 | Vector2 TextServerAdvanced::_font_get_kerning(const RID &p_font_rid, int64_t p_size, const Vector2i &p_glyph_pair) const { |
| 3708 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3709 | ERR_FAIL_NULL_V(fd, Vector2()); |
| 3710 | |
| 3711 | MutexLock lock(fd->mutex); |
| 3712 | Vector2i size = _get_size(fd, p_size); |
| 3713 | |
| 3714 | FontForSizeAdvanced *ffsd = nullptr; |
| 3715 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Vector2()); |
| 3716 | |
| 3717 | const HashMap<Vector2i, Vector2> &kern = ffsd->kerning_map; |
| 3718 | |
| 3719 | if (kern.has(p_glyph_pair)) { |
| 3720 | if (fd->msdf) { |
| 3721 | return kern[p_glyph_pair] * (double)p_size / (double)fd->msdf_source_size; |
| 3722 | } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) { |
| 3723 | if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) { |
| 3724 | return kern[p_glyph_pair] * (double)p_size / (double)fd->fixed_size; |
| 3725 | } else { |
| 3726 | return kern[p_glyph_pair] * Math::round((double)p_size / (double)fd->fixed_size); |
| 3727 | } |
| 3728 | } else { |
| 3729 | return kern[p_glyph_pair]; |
| 3730 | } |
| 3731 | } else { |
| 3732 | #ifdef MODULE_FREETYPE_ENABLED |
| 3733 | if (ffsd->face) { |
| 3734 | FT_Vector delta; |
| 3735 | FT_Get_Kerning(ffsd->face, p_glyph_pair.x, p_glyph_pair.y, FT_KERNING_DEFAULT, &delta); |
| 3736 | if (fd->msdf) { |
| 3737 | return Vector2(delta.x, delta.y) * (double)p_size / (double)fd->msdf_source_size; |
| 3738 | } else if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE && size.x != p_size * 64) { |
| 3739 | if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) { |
| 3740 | return Vector2(delta.x, delta.y) * (double)p_size / (double)fd->fixed_size; |
| 3741 | } else { |
| 3742 | return Vector2(delta.x, delta.y) * Math::round((double)p_size / (double)fd->fixed_size); |
| 3743 | } |
| 3744 | } else { |
| 3745 | return Vector2(delta.x, delta.y); |
| 3746 | } |
| 3747 | } |
| 3748 | #endif |
| 3749 | } |
| 3750 | return Vector2(); |
| 3751 | } |
| 3752 | |
| 3753 | int64_t TextServerAdvanced::_font_get_glyph_index(const RID &p_font_rid, int64_t p_size, int64_t p_char, int64_t p_variation_selector) const { |
| 3754 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3755 | ERR_FAIL_NULL_V(fd, 0); |
| 3756 | ERR_FAIL_COND_V_MSG((p_char >= 0xd800 && p_char <= 0xdfff) || (p_char > 0x10ffff), 0, "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_char, 16) + "."); |
| 3757 | ERR_FAIL_COND_V_MSG((p_variation_selector >= 0xd800 && p_variation_selector <= 0xdfff) || (p_variation_selector > 0x10ffff), 0, "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_variation_selector, 16) + "."); |
| 3758 | |
| 3759 | MutexLock lock(fd->mutex); |
| 3760 | Vector2i size = _get_size(fd, p_size); |
| 3761 | FontForSizeAdvanced *ffsd = nullptr; |
| 3762 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 0); |
| 3763 | |
| 3764 | #ifdef MODULE_FREETYPE_ENABLED |
| 3765 | if (ffsd->face) { |
| 3766 | if (p_variation_selector) { |
| 3767 | return FT_Face_GetCharVariantIndex(ffsd->face, p_char, p_variation_selector); |
| 3768 | } else { |
| 3769 | return FT_Get_Char_Index(ffsd->face, p_char); |
| 3770 | } |
| 3771 | } else { |
| 3772 | return (int64_t)p_char; |
| 3773 | } |
| 3774 | #else |
| 3775 | return (int64_t)p_char; |
| 3776 | #endif |
| 3777 | } |
| 3778 | |
| 3779 | int64_t TextServerAdvanced::_font_get_char_from_glyph_index(const RID &p_font_rid, int64_t p_size, int64_t p_glyph_index) const { |
| 3780 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3781 | ERR_FAIL_NULL_V(fd, 0); |
| 3782 | |
| 3783 | MutexLock lock(fd->mutex); |
| 3784 | Vector2i size = _get_size(fd, p_size); |
| 3785 | FontForSizeAdvanced *ffsd = nullptr; |
| 3786 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), 0); |
| 3787 | |
| 3788 | #ifdef MODULE_FREETYPE_ENABLED |
| 3789 | if (ffsd->inv_glyph_map.is_empty()) { |
| 3790 | FT_Face face = ffsd->face; |
| 3791 | FT_UInt gindex; |
| 3792 | FT_ULong charcode = FT_Get_First_Char(face, &gindex); |
| 3793 | while (gindex != 0) { |
| 3794 | if (charcode != 0) { |
| 3795 | ffsd->inv_glyph_map[gindex] = charcode; |
| 3796 | } |
| 3797 | charcode = FT_Get_Next_Char(face, charcode, &gindex); |
| 3798 | } |
| 3799 | } |
| 3800 | |
| 3801 | if (ffsd->inv_glyph_map.has(p_glyph_index)) { |
| 3802 | return ffsd->inv_glyph_map[p_glyph_index]; |
| 3803 | } else { |
| 3804 | return 0; |
| 3805 | } |
| 3806 | #else |
| 3807 | return p_glyph_index; |
| 3808 | #endif |
| 3809 | } |
| 3810 | |
| 3811 | bool TextServerAdvanced::_font_has_char(const RID &p_font_rid, int64_t p_char) const { |
| 3812 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3813 | ERR_FAIL_COND_V_MSG((p_char >= 0xd800 && p_char <= 0xdfff) || (p_char > 0x10ffff), false, "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_char, 16) + "."); |
| 3814 | if (!fd) { |
| 3815 | return false; |
| 3816 | } |
| 3817 | |
| 3818 | MutexLock lock(fd->mutex); |
| 3819 | FontForSizeAdvanced *ffsd = nullptr; |
| 3820 | if (fd->cache.is_empty()) { |
| 3821 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, fd->msdf ? Vector2i(fd->msdf_source_size * 64, 0) : Vector2i(16 * 64, 0), ffsd), false); |
| 3822 | } else { |
| 3823 | ffsd = fd->cache.begin()->value; |
| 3824 | } |
| 3825 | |
| 3826 | #ifdef MODULE_FREETYPE_ENABLED |
| 3827 | if (ffsd->face) { |
| 3828 | return FT_Get_Char_Index(ffsd->face, p_char) != 0; |
| 3829 | } |
| 3830 | #endif |
| 3831 | return ffsd->glyph_map.has((int32_t)p_char); |
| 3832 | } |
| 3833 | |
| 3834 | String TextServerAdvanced::_font_get_supported_chars(const RID &p_font_rid) const { |
| 3835 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3836 | ERR_FAIL_NULL_V(fd, String()); |
| 3837 | |
| 3838 | MutexLock lock(fd->mutex); |
| 3839 | FontForSizeAdvanced *ffsd = nullptr; |
| 3840 | if (fd->cache.is_empty()) { |
| 3841 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, fd->msdf ? Vector2i(fd->msdf_source_size * 64, 0) : Vector2i(16 * 64, 0), ffsd), String()); |
| 3842 | } else { |
| 3843 | ffsd = fd->cache.begin()->value; |
| 3844 | } |
| 3845 | |
| 3846 | String chars; |
| 3847 | #ifdef MODULE_FREETYPE_ENABLED |
| 3848 | if (ffsd->face) { |
| 3849 | FT_UInt gindex; |
| 3850 | FT_ULong charcode = FT_Get_First_Char(ffsd->face, &gindex); |
| 3851 | while (gindex != 0) { |
| 3852 | if (charcode != 0) { |
| 3853 | chars = chars + String::chr(charcode); |
| 3854 | } |
| 3855 | charcode = FT_Get_Next_Char(ffsd->face, charcode, &gindex); |
| 3856 | } |
| 3857 | return chars; |
| 3858 | } |
| 3859 | #endif |
| 3860 | const HashMap<int32_t, FontGlyph> &gl = ffsd->glyph_map; |
| 3861 | for (const KeyValue<int32_t, FontGlyph> &E : gl) { |
| 3862 | chars = chars + String::chr(E.key); |
| 3863 | } |
| 3864 | return chars; |
| 3865 | } |
| 3866 | |
| 3867 | PackedInt32Array TextServerAdvanced::_font_get_supported_glyphs(const RID &p_font_rid) const { |
| 3868 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3869 | ERR_FAIL_NULL_V(fd, PackedInt32Array()); |
| 3870 | |
| 3871 | MutexLock lock(fd->mutex); |
| 3872 | FontForSizeAdvanced *at_size = nullptr; |
| 3873 | if (fd->cache.is_empty()) { |
| 3874 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, fd->msdf ? Vector2i(fd->msdf_source_size * 64, 0) : Vector2i(16 * 64, 0), at_size), PackedInt32Array()); |
| 3875 | } else { |
| 3876 | at_size = fd->cache.begin()->value; |
| 3877 | } |
| 3878 | |
| 3879 | PackedInt32Array glyphs; |
| 3880 | #ifdef MODULE_FREETYPE_ENABLED |
| 3881 | if (at_size && at_size->face) { |
| 3882 | FT_UInt gindex; |
| 3883 | FT_ULong charcode = FT_Get_First_Char(at_size->face, &gindex); |
| 3884 | while (gindex != 0) { |
| 3885 | glyphs.push_back(gindex); |
| 3886 | charcode = FT_Get_Next_Char(at_size->face, charcode, &gindex); |
| 3887 | } |
| 3888 | return glyphs; |
| 3889 | } |
| 3890 | #endif |
| 3891 | if (at_size) { |
| 3892 | const HashMap<int32_t, FontGlyph> &gl = at_size->glyph_map; |
| 3893 | for (const KeyValue<int32_t, FontGlyph> &E : gl) { |
| 3894 | glyphs.push_back(E.key); |
| 3895 | } |
| 3896 | } |
| 3897 | return glyphs; |
| 3898 | } |
| 3899 | |
| 3900 | void TextServerAdvanced::_font_render_range(const RID &p_font_rid, const Vector2i &p_size, int64_t p_start, int64_t p_end) { |
| 3901 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3902 | ERR_FAIL_NULL(fd); |
| 3903 | ERR_FAIL_COND_MSG((p_start >= 0xd800 && p_start <= 0xdfff) || (p_start > 0x10ffff), "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_start, 16) + "."); |
| 3904 | ERR_FAIL_COND_MSG((p_end >= 0xd800 && p_end <= 0xdfff) || (p_end > 0x10ffff), "Unicode parsing error: Invalid unicode codepoint " + String::num_int64(p_end, 16) + "."); |
| 3905 | |
| 3906 | MutexLock lock(fd->mutex); |
| 3907 | Vector2i size = _get_size_outline(fd, p_size); |
| 3908 | FontForSizeAdvanced *ffsd = nullptr; |
| 3909 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 3910 | for (int64_t i = p_start; i <= p_end; i++) { |
| 3911 | #ifdef MODULE_FREETYPE_ENABLED |
| 3912 | int32_t idx = FT_Get_Char_Index(ffsd->face, i); |
| 3913 | if (ffsd->face) { |
| 3914 | FontGlyph fgl; |
| 3915 | if (fd->msdf) { |
| 3916 | _ensure_glyph(fd, size, (int32_t)idx, fgl); |
| 3917 | } else { |
| 3918 | for (int aa = 0; aa < ((fd->antialiasing == FONT_ANTIALIASING_LCD) ? FONT_LCD_SUBPIXEL_LAYOUT_MAX : 1); aa++) { |
| 3919 | if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE * 64)) { |
| 3920 | _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24), fgl); |
| 3921 | _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24), fgl); |
| 3922 | _ensure_glyph(fd, size, (int32_t)idx | (2 << 27) | (aa << 24), fgl); |
| 3923 | _ensure_glyph(fd, size, (int32_t)idx | (3 << 27) | (aa << 24), fgl); |
| 3924 | } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE * 64)) { |
| 3925 | _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24), fgl); |
| 3926 | _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24), fgl); |
| 3927 | } else { |
| 3928 | _ensure_glyph(fd, size, (int32_t)idx | (aa << 24), fgl); |
| 3929 | } |
| 3930 | } |
| 3931 | } |
| 3932 | } |
| 3933 | #endif |
| 3934 | } |
| 3935 | } |
| 3936 | |
| 3937 | void TextServerAdvanced::_font_render_glyph(const RID &p_font_rid, const Vector2i &p_size, int64_t p_index) { |
| 3938 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3939 | ERR_FAIL_NULL(fd); |
| 3940 | |
| 3941 | MutexLock lock(fd->mutex); |
| 3942 | Vector2i size = _get_size_outline(fd, p_size); |
| 3943 | FontForSizeAdvanced *ffsd = nullptr; |
| 3944 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 3945 | #ifdef MODULE_FREETYPE_ENABLED |
| 3946 | int32_t idx = p_index & 0xffffff; // Remove subpixel shifts. |
| 3947 | if (ffsd->face) { |
| 3948 | FontGlyph fgl; |
| 3949 | if (fd->msdf) { |
| 3950 | _ensure_glyph(fd, size, (int32_t)idx, fgl); |
| 3951 | } else { |
| 3952 | for (int aa = 0; aa < ((fd->antialiasing == FONT_ANTIALIASING_LCD) ? FONT_LCD_SUBPIXEL_LAYOUT_MAX : 1); aa++) { |
| 3953 | if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE * 64)) { |
| 3954 | _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24), fgl); |
| 3955 | _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24), fgl); |
| 3956 | _ensure_glyph(fd, size, (int32_t)idx | (2 << 27) | (aa << 24), fgl); |
| 3957 | _ensure_glyph(fd, size, (int32_t)idx | (3 << 27) | (aa << 24), fgl); |
| 3958 | } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE * 64)) { |
| 3959 | _ensure_glyph(fd, size, (int32_t)idx | (1 << 27) | (aa << 24), fgl); |
| 3960 | _ensure_glyph(fd, size, (int32_t)idx | (0 << 27) | (aa << 24), fgl); |
| 3961 | } else { |
| 3962 | _ensure_glyph(fd, size, (int32_t)idx | (aa << 24), fgl); |
| 3963 | } |
| 3964 | } |
| 3965 | } |
| 3966 | } |
| 3967 | #endif |
| 3968 | } |
| 3969 | |
| 3970 | void TextServerAdvanced::_font_draw_glyph(const RID &p_font_rid, const RID &p_canvas, int64_t p_size, const Vector2 &p_pos, int64_t p_index, const Color &p_color, float p_oversampling) const { |
| 3971 | if (p_index == 0) { |
| 3972 | return; // Non visual character, skip. |
| 3973 | } |
| 3974 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 3975 | ERR_FAIL_NULL(fd); |
| 3976 | |
| 3977 | MutexLock lock(fd->mutex); |
| 3978 | |
| 3979 | // Oversampling. |
| 3980 | bool viewport_oversampling = false; |
| 3981 | float oversampling_factor = p_oversampling; |
| 3982 | if (p_oversampling <= 0.0) { |
| 3983 | if (fd->oversampling_override > 0.0) { |
| 3984 | oversampling_factor = fd->oversampling_override; |
| 3985 | } else if (vp_oversampling > 0.0) { |
| 3986 | oversampling_factor = vp_oversampling; |
| 3987 | viewport_oversampling = true; |
| 3988 | } else { |
| 3989 | oversampling_factor = 1.0; |
| 3990 | } |
| 3991 | } |
| 3992 | bool skip_oversampling = fd->msdf || fd->fixed_size > 0; |
| 3993 | if (skip_oversampling) { |
| 3994 | oversampling_factor = 1.0; |
| 3995 | } else { |
| 3996 | uint64_t oversampling_level = CLAMP(oversampling_factor, 0.1, 100.0) * 64; |
| 3997 | oversampling_factor = double(oversampling_level) / 64.0; |
| 3998 | } |
| 3999 | |
| 4000 | Vector2i size; |
| 4001 | if (skip_oversampling) { |
| 4002 | size = _get_size(fd, p_size); |
| 4003 | } else { |
| 4004 | size = Vector2i(p_size * 64 * oversampling_factor, 0); |
| 4005 | } |
| 4006 | |
| 4007 | FontForSizeAdvanced *ffsd = nullptr; |
| 4008 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd, false, viewport_oversampling ? 64 * oversampling_factor : 0)); |
| 4009 | |
| 4010 | int32_t index = p_index & 0xffffff; // Remove subpixel shifts. |
| 4011 | bool lcd_aa = false; |
| 4012 | |
| 4013 | #ifdef MODULE_FREETYPE_ENABLED |
| 4014 | if (!fd->msdf && ffsd->face) { |
| 4015 | // LCD layout, bits 24, 25, 26 |
| 4016 | if (fd->antialiasing == FONT_ANTIALIASING_LCD) { |
| 4017 | TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get(); |
| 4018 | if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) { |
| 4019 | lcd_aa = true; |
| 4020 | index = index | (layout << 24); |
| 4021 | } |
| 4022 | } |
| 4023 | // Subpixel X-shift, bits 27, 28 |
| 4024 | if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE * 64)) { |
| 4025 | int xshift = (int)(Math::floor(4 * (p_pos.x + 0.125)) - 4 * Math::floor(p_pos.x + 0.125)); |
| 4026 | index = index | (xshift << 27); |
| 4027 | } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE * 64)) { |
| 4028 | int xshift = (int)(Math::floor(2 * (p_pos.x + 0.25)) - 2 * Math::floor(p_pos.x + 0.25)); |
| 4029 | index = index | (xshift << 27); |
| 4030 | } |
| 4031 | } |
| 4032 | #endif |
| 4033 | |
| 4034 | FontGlyph fgl; |
| 4035 | if (!_ensure_glyph(fd, size, index, fgl, viewport_oversampling ? 64 * oversampling_factor : 0)) { |
| 4036 | return; // Invalid or non-graphical glyph, do not display errors, nothing to draw. |
| 4037 | } |
| 4038 | |
| 4039 | if (fgl.found) { |
| 4040 | ERR_FAIL_COND(fgl.texture_idx < -1 || fgl.texture_idx >= ffsd->textures.size()); |
| 4041 | |
| 4042 | if (fgl.texture_idx != -1) { |
| 4043 | Color modulate = p_color; |
| 4044 | #ifdef MODULE_FREETYPE_ENABLED |
| 4045 | if (!fd->modulate_color_glyphs && ffsd->face && ffsd->textures[fgl.texture_idx].image.is_valid() && (ffsd->textures[fgl.texture_idx].image->get_format() == Image::FORMAT_RGBA8) && !lcd_aa && !fd->msdf) { |
| 4046 | modulate.r = modulate.g = modulate.b = 1.0; |
| 4047 | } |
| 4048 | #endif |
| 4049 | if (RenderingServer::get_singleton() != nullptr) { |
| 4050 | if (ffsd->textures[fgl.texture_idx].dirty) { |
| 4051 | ShelfPackTexture &tex = ffsd->textures.write[fgl.texture_idx]; |
| 4052 | Ref<Image> img = tex.image; |
| 4053 | if (fgl.from_svg) { |
| 4054 | // Same as the "fix alpha border" process option when importing SVGs |
| 4055 | img->fix_alpha_edges(); |
| 4056 | } |
| 4057 | if (fd->mipmaps && !img->has_mipmaps()) { |
| 4058 | img = tex.image->duplicate(); |
| 4059 | img->generate_mipmaps(); |
| 4060 | } |
| 4061 | if (tex.texture.is_null()) { |
| 4062 | tex.texture = ImageTexture::create_from_image(img); |
| 4063 | } else { |
| 4064 | tex.texture->update(img); |
| 4065 | } |
| 4066 | tex.dirty = false; |
| 4067 | } |
| 4068 | RID texture = ffsd->textures[fgl.texture_idx].texture->get_rid(); |
| 4069 | if (fd->msdf) { |
| 4070 | Point2 cpos = p_pos; |
| 4071 | cpos += fgl.rect.position * (double)p_size / (double)fd->msdf_source_size; |
| 4072 | Size2 csize = fgl.rect.size * (double)p_size / (double)fd->msdf_source_size; |
| 4073 | RenderingServer::get_singleton()->canvas_item_add_msdf_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, fgl.uv_rect, modulate, 0, fd->msdf_range, (double)p_size / (double)fd->msdf_source_size); |
| 4074 | } else { |
| 4075 | Point2 cpos = p_pos; |
| 4076 | double scale = _font_get_scale(p_font_rid, p_size) / oversampling_factor; |
| 4077 | if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE * 64)) { |
| 4078 | cpos.x = cpos.x + 0.125; |
| 4079 | } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE * 64)) { |
| 4080 | cpos.x = cpos.x + 0.25; |
| 4081 | } |
| 4082 | if (scale == 1.0) { |
| 4083 | cpos.y = Math::floor(cpos.y); |
| 4084 | cpos.x = Math::floor(cpos.x); |
| 4085 | } |
| 4086 | Vector2 gpos = fgl.rect.position; |
| 4087 | Size2 csize = fgl.rect.size; |
| 4088 | if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE) { |
| 4089 | if (size.x != p_size * 64) { |
| 4090 | if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) { |
| 4091 | double gl_scale = (double)p_size / (double)fd->fixed_size; |
| 4092 | gpos *= gl_scale; |
| 4093 | csize *= gl_scale; |
| 4094 | } else { |
| 4095 | double gl_scale = Math::round((double)p_size / (double)fd->fixed_size); |
| 4096 | gpos *= gl_scale; |
| 4097 | csize *= gl_scale; |
| 4098 | } |
| 4099 | } |
| 4100 | } else { |
| 4101 | gpos /= oversampling_factor; |
| 4102 | csize /= oversampling_factor; |
| 4103 | } |
| 4104 | cpos += gpos; |
| 4105 | if (lcd_aa) { |
| 4106 | RenderingServer::get_singleton()->canvas_item_add_lcd_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, fgl.uv_rect, modulate); |
| 4107 | } else { |
| 4108 | RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, fgl.uv_rect, modulate, false, false); |
| 4109 | } |
| 4110 | } |
| 4111 | } |
| 4112 | } |
| 4113 | } |
| 4114 | } |
| 4115 | |
| 4116 | void TextServerAdvanced::_font_draw_glyph_outline(const RID &p_font_rid, const RID &p_canvas, int64_t p_size, int64_t p_outline_size, const Vector2 &p_pos, int64_t p_index, const Color &p_color, float p_oversampling) const { |
| 4117 | if (p_index == 0) { |
| 4118 | return; // Non visual character, skip. |
| 4119 | } |
| 4120 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 4121 | ERR_FAIL_NULL(fd); |
| 4122 | |
| 4123 | MutexLock lock(fd->mutex); |
| 4124 | |
| 4125 | // Oversampling. |
| 4126 | bool viewport_oversampling = false; |
| 4127 | float oversampling_factor = p_oversampling; |
| 4128 | if (p_oversampling <= 0.0) { |
| 4129 | if (fd->oversampling_override > 0.0) { |
| 4130 | oversampling_factor = fd->oversampling_override; |
| 4131 | } else if (vp_oversampling > 0.0) { |
| 4132 | oversampling_factor = vp_oversampling; |
| 4133 | viewport_oversampling = true; |
| 4134 | } else { |
| 4135 | oversampling_factor = 1.0; |
| 4136 | } |
| 4137 | } |
| 4138 | bool skip_oversampling = fd->msdf || fd->fixed_size > 0; |
| 4139 | if (skip_oversampling) { |
| 4140 | oversampling_factor = 1.0; |
| 4141 | } else { |
| 4142 | uint64_t oversampling_level = CLAMP(oversampling_factor, 0.1, 100.0) * 64; |
| 4143 | oversampling_factor = double(oversampling_level) / 64.0; |
| 4144 | } |
| 4145 | |
| 4146 | Vector2i size; |
| 4147 | if (skip_oversampling) { |
| 4148 | size = _get_size_outline(fd, Vector2i(p_size, p_outline_size)); |
| 4149 | } else { |
| 4150 | size = Vector2i(p_size * 64 * oversampling_factor, p_outline_size * oversampling_factor); |
| 4151 | } |
| 4152 | |
| 4153 | FontForSizeAdvanced *ffsd = nullptr; |
| 4154 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd, false, viewport_oversampling ? 64 * oversampling_factor : 0)); |
| 4155 | |
| 4156 | int32_t index = p_index & 0xffffff; // Remove subpixel shifts. |
| 4157 | bool lcd_aa = false; |
| 4158 | |
| 4159 | #ifdef MODULE_FREETYPE_ENABLED |
| 4160 | if (!fd->msdf && ffsd->face) { |
| 4161 | // LCD layout, bits 24, 25, 26 |
| 4162 | if (fd->antialiasing == FONT_ANTIALIASING_LCD) { |
| 4163 | TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get(); |
| 4164 | if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) { |
| 4165 | lcd_aa = true; |
| 4166 | index = index | (layout << 24); |
| 4167 | } |
| 4168 | } |
| 4169 | // Subpixel X-shift, bits 27, 28 |
| 4170 | if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE * 64)) { |
| 4171 | int xshift = (int)(Math::floor(4 * (p_pos.x + 0.125)) - 4 * Math::floor(p_pos.x + 0.125)); |
| 4172 | index = index | (xshift << 27); |
| 4173 | } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE * 64)) { |
| 4174 | int xshift = (int)(Math::floor(2 * (p_pos.x + 0.25)) - 2 * Math::floor(p_pos.x + 0.25)); |
| 4175 | index = index | (xshift << 27); |
| 4176 | } |
| 4177 | } |
| 4178 | #endif |
| 4179 | |
| 4180 | FontGlyph fgl; |
| 4181 | if (!_ensure_glyph(fd, size, index, fgl, viewport_oversampling ? 64 * oversampling_factor : 0)) { |
| 4182 | return; // Invalid or non-graphical glyph, do not display errors, nothing to draw. |
| 4183 | } |
| 4184 | |
| 4185 | if (fgl.found) { |
| 4186 | ERR_FAIL_COND(fgl.texture_idx < -1 || fgl.texture_idx >= ffsd->textures.size()); |
| 4187 | |
| 4188 | if (fgl.texture_idx != -1) { |
| 4189 | Color modulate = p_color; |
| 4190 | #ifdef MODULE_FREETYPE_ENABLED |
| 4191 | if (ffsd->face && fd->cache[size]->textures[fgl.texture_idx].image.is_valid() && (ffsd->textures[fgl.texture_idx].image->get_format() == Image::FORMAT_RGBA8) && !lcd_aa && !fd->msdf) { |
| 4192 | modulate.r = modulate.g = modulate.b = 1.0; |
| 4193 | } |
| 4194 | #endif |
| 4195 | if (RenderingServer::get_singleton() != nullptr) { |
| 4196 | if (ffsd->textures[fgl.texture_idx].dirty) { |
| 4197 | ShelfPackTexture &tex = ffsd->textures.write[fgl.texture_idx]; |
| 4198 | Ref<Image> img = tex.image; |
| 4199 | if (fd->mipmaps && !img->has_mipmaps()) { |
| 4200 | img = tex.image->duplicate(); |
| 4201 | img->generate_mipmaps(); |
| 4202 | } |
| 4203 | if (tex.texture.is_null()) { |
| 4204 | tex.texture = ImageTexture::create_from_image(img); |
| 4205 | } else { |
| 4206 | tex.texture->update(img); |
| 4207 | } |
| 4208 | tex.dirty = false; |
| 4209 | } |
| 4210 | RID texture = ffsd->textures[fgl.texture_idx].texture->get_rid(); |
| 4211 | if (fd->msdf) { |
| 4212 | Point2 cpos = p_pos; |
| 4213 | cpos += fgl.rect.position * (double)p_size / (double)fd->msdf_source_size; |
| 4214 | Size2 csize = fgl.rect.size * (double)p_size / (double)fd->msdf_source_size; |
| 4215 | RenderingServer::get_singleton()->canvas_item_add_msdf_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, fgl.uv_rect, modulate, p_outline_size, fd->msdf_range, (double)p_size / (double)fd->msdf_source_size); |
| 4216 | } else { |
| 4217 | Point2 cpos = p_pos; |
| 4218 | double scale = _font_get_scale(p_font_rid, p_size) / oversampling_factor; |
| 4219 | if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_QUARTER) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_QUARTER_MAX_SIZE * 64)) { |
| 4220 | cpos.x = cpos.x + 0.125; |
| 4221 | } else if ((fd->subpixel_positioning == SUBPIXEL_POSITIONING_ONE_HALF) || (fd->subpixel_positioning == SUBPIXEL_POSITIONING_AUTO && size.x <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE * 64)) { |
| 4222 | cpos.x = cpos.x + 0.25; |
| 4223 | } |
| 4224 | if (scale == 1.0) { |
| 4225 | cpos.y = Math::floor(cpos.y); |
| 4226 | cpos.x = Math::floor(cpos.x); |
| 4227 | } |
| 4228 | Vector2 gpos = fgl.rect.position; |
| 4229 | Size2 csize = fgl.rect.size; |
| 4230 | if (fd->fixed_size > 0 && fd->fixed_size_scale_mode != FIXED_SIZE_SCALE_DISABLE) { |
| 4231 | if (size.x != p_size * 64) { |
| 4232 | if (fd->fixed_size_scale_mode == FIXED_SIZE_SCALE_ENABLED) { |
| 4233 | double gl_scale = (double)p_size / (double)fd->fixed_size; |
| 4234 | gpos *= gl_scale; |
| 4235 | csize *= gl_scale; |
| 4236 | } else { |
| 4237 | double gl_scale = Math::round((double)p_size / (double)fd->fixed_size); |
| 4238 | gpos *= gl_scale; |
| 4239 | csize *= gl_scale; |
| 4240 | } |
| 4241 | } |
| 4242 | } else { |
| 4243 | gpos /= oversampling_factor; |
| 4244 | csize /= oversampling_factor; |
| 4245 | } |
| 4246 | cpos += gpos; |
| 4247 | if (lcd_aa) { |
| 4248 | RenderingServer::get_singleton()->canvas_item_add_lcd_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, fgl.uv_rect, modulate); |
| 4249 | } else { |
| 4250 | RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas, Rect2(cpos, csize), texture, fgl.uv_rect, modulate, false, false); |
| 4251 | } |
| 4252 | } |
| 4253 | } |
| 4254 | } |
| 4255 | } |
| 4256 | } |
| 4257 | |
| 4258 | bool TextServerAdvanced::_font_is_language_supported(const RID &p_font_rid, const String &p_language) const { |
| 4259 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 4260 | ERR_FAIL_NULL_V(fd, false); |
| 4261 | |
| 4262 | MutexLock lock(fd->mutex); |
| 4263 | if (fd->language_support_overrides.has(p_language)) { |
| 4264 | return fd->language_support_overrides[p_language]; |
| 4265 | } else { |
| 4266 | if (fd->language_support_overrides.has("*")) { |
| 4267 | return fd->language_support_overrides["*"]; |
| 4268 | } |
| 4269 | return true; |
| 4270 | } |
| 4271 | } |
| 4272 | |
| 4273 | void TextServerAdvanced::_font_set_language_support_override(const RID &p_font_rid, const String &p_language, bool p_supported) { |
| 4274 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 4275 | ERR_FAIL_NULL(fd); |
| 4276 | |
| 4277 | MutexLock lock(fd->mutex); |
| 4278 | fd->language_support_overrides[p_language] = p_supported; |
| 4279 | } |
| 4280 | |
| 4281 | bool TextServerAdvanced::_font_get_language_support_override(const RID &p_font_rid, const String &p_language) { |
| 4282 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 4283 | ERR_FAIL_NULL_V(fd, false); |
| 4284 | |
| 4285 | MutexLock lock(fd->mutex); |
| 4286 | return fd->language_support_overrides[p_language]; |
| 4287 | } |
| 4288 | |
| 4289 | void TextServerAdvanced::_font_remove_language_support_override(const RID &p_font_rid, const String &p_language) { |
| 4290 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 4291 | ERR_FAIL_NULL(fd); |
| 4292 | |
| 4293 | MutexLock lock(fd->mutex); |
| 4294 | fd->language_support_overrides.erase(p_language); |
| 4295 | } |
| 4296 | |
| 4297 | PackedStringArray TextServerAdvanced::_font_get_language_support_overrides(const RID &p_font_rid) { |
| 4298 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 4299 | ERR_FAIL_NULL_V(fd, PackedStringArray()); |
| 4300 | |
| 4301 | MutexLock lock(fd->mutex); |
| 4302 | PackedStringArray out; |
| 4303 | for (const KeyValue<String, bool> &E : fd->language_support_overrides) { |
| 4304 | out.push_back(E.key); |
| 4305 | } |
| 4306 | return out; |
| 4307 | } |
| 4308 | |
| 4309 | bool TextServerAdvanced::_font_is_script_supported(const RID &p_font_rid, const String &p_script) const { |
| 4310 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 4311 | ERR_FAIL_NULL_V(fd, false); |
| 4312 | |
| 4313 | MutexLock lock(fd->mutex); |
| 4314 | if (fd->script_support_overrides.has(p_script)) { |
| 4315 | return fd->script_support_overrides[p_script]; |
| 4316 | } else { |
| 4317 | if (fd->script_support_overrides.has("*")) { |
| 4318 | return fd->script_support_overrides["*"]; |
| 4319 | } |
| 4320 | Vector2i size = _get_size(fd, 16); |
| 4321 | FontForSizeAdvanced *ffsd = nullptr; |
| 4322 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), false); |
| 4323 | char ascii_script[] = { ' ', ' ', ' ', ' ' }; |
| 4324 | for (int i = 0; i < MIN(4, p_script.size()); i++) { |
| 4325 | if (p_script[i] <= 0x7f) { |
| 4326 | ascii_script[i] = p_script[i]; |
| 4327 | } |
| 4328 | } |
| 4329 | return fd->supported_scripts.has(hb_tag_from_string(ascii_script, -1)); |
| 4330 | } |
| 4331 | } |
| 4332 | |
| 4333 | void TextServerAdvanced::_font_set_script_support_override(const RID &p_font_rid, const String &p_script, bool p_supported) { |
| 4334 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 4335 | ERR_FAIL_NULL(fd); |
| 4336 | |
| 4337 | MutexLock lock(fd->mutex); |
| 4338 | fd->script_support_overrides[p_script] = p_supported; |
| 4339 | } |
| 4340 | |
| 4341 | bool TextServerAdvanced::_font_get_script_support_override(const RID &p_font_rid, const String &p_script) { |
| 4342 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 4343 | ERR_FAIL_NULL_V(fd, false); |
| 4344 | |
| 4345 | MutexLock lock(fd->mutex); |
| 4346 | return fd->script_support_overrides[p_script]; |
| 4347 | } |
| 4348 | |
| 4349 | void TextServerAdvanced::_font_remove_script_support_override(const RID &p_font_rid, const String &p_script) { |
| 4350 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 4351 | ERR_FAIL_NULL(fd); |
| 4352 | |
| 4353 | MutexLock lock(fd->mutex); |
| 4354 | fd->script_support_overrides.erase(p_script); |
| 4355 | } |
| 4356 | |
| 4357 | PackedStringArray TextServerAdvanced::_font_get_script_support_overrides(const RID &p_font_rid) { |
| 4358 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 4359 | ERR_FAIL_NULL_V(fd, PackedStringArray()); |
| 4360 | |
| 4361 | MutexLock lock(fd->mutex); |
| 4362 | PackedStringArray out; |
| 4363 | for (const KeyValue<String, bool> &E : fd->script_support_overrides) { |
| 4364 | out.push_back(E.key); |
| 4365 | } |
| 4366 | return out; |
| 4367 | } |
| 4368 | |
| 4369 | void TextServerAdvanced::_font_set_opentype_feature_overrides(const RID &p_font_rid, const Dictionary &p_overrides) { |
| 4370 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 4371 | ERR_FAIL_NULL(fd); |
| 4372 | |
| 4373 | MutexLock lock(fd->mutex); |
| 4374 | Vector2i size = _get_size(fd, 16); |
| 4375 | FontForSizeAdvanced *ffsd = nullptr; |
| 4376 | ERR_FAIL_COND(!_ensure_cache_for_size(fd, size, ffsd)); |
| 4377 | fd->feature_overrides = p_overrides; |
| 4378 | } |
| 4379 | |
| 4380 | Dictionary TextServerAdvanced::_font_get_opentype_feature_overrides(const RID &p_font_rid) const { |
| 4381 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 4382 | ERR_FAIL_NULL_V(fd, Dictionary()); |
| 4383 | |
| 4384 | MutexLock lock(fd->mutex); |
| 4385 | return fd->feature_overrides; |
| 4386 | } |
| 4387 | |
| 4388 | Dictionary TextServerAdvanced::_font_supported_feature_list(const RID &p_font_rid) const { |
| 4389 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 4390 | ERR_FAIL_NULL_V(fd, Dictionary()); |
| 4391 | |
| 4392 | MutexLock lock(fd->mutex); |
| 4393 | Vector2i size = _get_size(fd, 16); |
| 4394 | FontForSizeAdvanced *ffsd = nullptr; |
| 4395 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Dictionary()); |
| 4396 | return fd->supported_features; |
| 4397 | } |
| 4398 | |
| 4399 | Dictionary TextServerAdvanced::_font_supported_variation_list(const RID &p_font_rid) const { |
| 4400 | FontAdvanced *fd = _get_font_data(p_font_rid); |
| 4401 | ERR_FAIL_NULL_V(fd, Dictionary()); |
| 4402 | |
| 4403 | MutexLock lock(fd->mutex); |
| 4404 | Vector2i size = _get_size(fd, 16); |
| 4405 | FontForSizeAdvanced *ffsd = nullptr; |
| 4406 | ERR_FAIL_COND_V(!_ensure_cache_for_size(fd, size, ffsd), Dictionary()); |
| 4407 | return fd->supported_varaitions; |
| 4408 | } |
| 4409 | |
| 4410 | /*************************************************************************/ |
| 4411 | /* Shaped text buffer interface */ |
| 4412 | /*************************************************************************/ |
| 4413 | |
| 4414 | int64_t TextServerAdvanced::_convert_pos(const String &p_utf32, const Char16String &p_utf16, int64_t p_pos) const { |
| 4415 | int64_t limit = p_pos; |
| 4416 | if (p_utf32.length() != p_utf16.length()) { |
| 4417 | const UChar *data = p_utf16.get_data(); |
| 4418 | for (int i = 0; i < p_pos; i++) { |
| 4419 | if (U16_IS_LEAD(data[i])) { |
| 4420 | limit--; |
| 4421 | } |
| 4422 | } |
| 4423 | } |
| 4424 | return limit; |
| 4425 | } |
| 4426 | |
| 4427 | int64_t TextServerAdvanced::_convert_pos(const ShapedTextDataAdvanced *p_sd, int64_t p_pos) const { |
| 4428 | int64_t limit = p_pos; |
| 4429 | if (p_sd->text.length() != p_sd->utf16.length()) { |
| 4430 | const UChar *data = p_sd->utf16.get_data(); |
| 4431 | for (int i = 0; i < p_pos; i++) { |
| 4432 | if (U16_IS_LEAD(data[i])) { |
| 4433 | limit--; |
| 4434 | } |
| 4435 | } |
| 4436 | } |
| 4437 | return limit; |
| 4438 | } |
| 4439 | |
| 4440 | int64_t TextServerAdvanced::_convert_pos_inv(const ShapedTextDataAdvanced *p_sd, int64_t p_pos) const { |
| 4441 | int64_t limit = p_pos; |
| 4442 | if (p_sd->text.length() != p_sd->utf16.length()) { |
| 4443 | for (int i = 0; i < p_pos; i++) { |
| 4444 | if (p_sd->text[i] > 0xffff) { |
| 4445 | limit++; |
| 4446 | } |
| 4447 | } |
| 4448 | } |
| 4449 | return limit; |
| 4450 | } |
| 4451 | |
| 4452 | void TextServerAdvanced::invalidate(TextServerAdvanced::ShapedTextDataAdvanced *p_shaped, bool p_text) { |
| 4453 | p_shaped->valid.clear(); |
| 4454 | p_shaped->sort_valid = false; |
| 4455 | p_shaped->line_breaks_valid = false; |
| 4456 | p_shaped->justification_ops_valid = false; |
| 4457 | p_shaped->text_trimmed = false; |
| 4458 | p_shaped->ascent = 0.0; |
| 4459 | p_shaped->descent = 0.0; |
| 4460 | p_shaped->width = 0.0; |
| 4461 | p_shaped->upos = 0.0; |
| 4462 | p_shaped->uthk = 0.0; |
| 4463 | p_shaped->glyphs.clear(); |
| 4464 | p_shaped->glyphs_logical.clear(); |
| 4465 | p_shaped->runs.clear(); |
| 4466 | p_shaped->runs_dirty = true; |
| 4467 | p_shaped->overrun_trim_data = TrimData(); |
| 4468 | p_shaped->utf16 = Char16String(); |
| 4469 | for (int i = 0; i < p_shaped->bidi_iter.size(); i++) { |
| 4470 | ubidi_close(p_shaped->bidi_iter[i]); |
| 4471 | } |
| 4472 | p_shaped->bidi_iter.clear(); |
| 4473 | |
| 4474 | if (p_text) { |
| 4475 | if (p_shaped->script_iter != nullptr) { |
| 4476 | memdelete(p_shaped->script_iter); |
| 4477 | p_shaped->script_iter = nullptr; |
| 4478 | } |
| 4479 | p_shaped->break_ops_valid = false; |
| 4480 | p_shaped->chars_valid = false; |
| 4481 | p_shaped->js_ops_valid = false; |
| 4482 | } |
| 4483 | } |
| 4484 | |
| 4485 | void TextServerAdvanced::full_copy(ShapedTextDataAdvanced *p_shaped) { |
| 4486 | ShapedTextDataAdvanced *parent = shaped_owner.get_or_null(p_shaped->parent); |
| 4487 | |
| 4488 | for (const KeyValue<Variant, ShapedTextDataAdvanced::EmbeddedObject> &E : parent->objects) { |
| 4489 | if (E.value.start >= p_shaped->start && E.value.start < p_shaped->end) { |
| 4490 | p_shaped->objects[E.key] = E.value; |
| 4491 | } |
| 4492 | } |
| 4493 | |
| 4494 | for (int i = MAX(0, p_shaped->first_span); i <= MIN(p_shaped->last_span, parent->spans.size() - 1); i++) { |
| 4495 | ShapedTextDataAdvanced::Span span = parent->spans[i]; |
| 4496 | span.start = MAX(p_shaped->start, span.start); |
| 4497 | span.end = MIN(p_shaped->end, span.end); |
| 4498 | p_shaped->spans.push_back(span); |
| 4499 | } |
| 4500 | p_shaped->first_span = 0; |
| 4501 | p_shaped->last_span = 0; |
| 4502 | |
| 4503 | p_shaped->parent = RID(); |
| 4504 | } |
| 4505 | |
| 4506 | RID TextServerAdvanced::_create_shaped_text(TextServer::Direction p_direction, TextServer::Orientation p_orientation) { |
| 4507 | _THREAD_SAFE_METHOD_ |
| 4508 | ERR_FAIL_COND_V_MSG(p_direction == DIRECTION_INHERITED, RID(), "Invalid text direction."); |
| 4509 | |
| 4510 | ShapedTextDataAdvanced *sd = memnew(ShapedTextDataAdvanced); |
| 4511 | sd->hb_buffer = hb_buffer_create(); |
| 4512 | sd->direction = p_direction; |
| 4513 | sd->orientation = p_orientation; |
| 4514 | return shaped_owner.make_rid(sd); |
| 4515 | } |
| 4516 | |
| 4517 | void TextServerAdvanced::_shaped_text_clear(const RID &p_shaped) { |
| 4518 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4519 | ERR_FAIL_NULL(sd); |
| 4520 | |
| 4521 | MutexLock lock(sd->mutex); |
| 4522 | sd->parent = RID(); |
| 4523 | sd->start = 0; |
| 4524 | sd->end = 0; |
| 4525 | sd->text = String(); |
| 4526 | sd->spans.clear(); |
| 4527 | sd->first_span = 0; |
| 4528 | sd->last_span = 0; |
| 4529 | sd->objects.clear(); |
| 4530 | sd->bidi_override.clear(); |
| 4531 | invalidate(sd, true); |
| 4532 | } |
| 4533 | |
| 4534 | RID TextServerAdvanced::_shaped_text_duplicate(const RID &p_shaped) { |
| 4535 | _THREAD_SAFE_METHOD_ |
| 4536 | |
| 4537 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4538 | ERR_FAIL_NULL_V(sd, RID()); |
| 4539 | |
| 4540 | MutexLock lock(sd->mutex); |
| 4541 | |
| 4542 | ShapedTextDataAdvanced *new_sd = memnew(ShapedTextDataAdvanced); |
| 4543 | new_sd->start = sd->start; |
| 4544 | new_sd->end = sd->end; |
| 4545 | new_sd->first_span = sd->first_span; |
| 4546 | new_sd->last_span = sd->last_span; |
| 4547 | new_sd->text = sd->text; |
| 4548 | new_sd->hb_buffer = hb_buffer_create(); |
| 4549 | new_sd->utf16 = new_sd->text.utf16(); |
| 4550 | new_sd->script_iter = memnew(ScriptIterator(new_sd->text, 0, new_sd->text.length())); |
| 4551 | new_sd->orientation = sd->orientation; |
| 4552 | new_sd->direction = sd->direction; |
| 4553 | new_sd->custom_punct = sd->custom_punct; |
| 4554 | new_sd->para_direction = sd->para_direction; |
| 4555 | new_sd->base_para_direction = sd->base_para_direction; |
| 4556 | new_sd->line_breaks_valid = sd->line_breaks_valid; |
| 4557 | new_sd->justification_ops_valid = sd->justification_ops_valid; |
| 4558 | new_sd->sort_valid = false; |
| 4559 | new_sd->upos = sd->upos; |
| 4560 | new_sd->uthk = sd->uthk; |
| 4561 | new_sd->runs.clear(); |
| 4562 | new_sd->runs_dirty = true; |
| 4563 | for (int i = 0; i < TextServer::SPACING_MAX; i++) { |
| 4564 | new_sd->extra_spacing[i] = sd->extra_spacing[i]; |
| 4565 | } |
| 4566 | for (const KeyValue<Variant, ShapedTextDataAdvanced::EmbeddedObject> &E : sd->objects) { |
| 4567 | new_sd->objects[E.key] = E.value; |
| 4568 | } |
| 4569 | for (int i = 0; i < sd->spans.size(); i++) { |
| 4570 | new_sd->spans.push_back(sd->spans[i]); |
| 4571 | } |
| 4572 | new_sd->valid.clear(); |
| 4573 | |
| 4574 | return shaped_owner.make_rid(new_sd); |
| 4575 | } |
| 4576 | |
| 4577 | void TextServerAdvanced::_shaped_text_set_direction(const RID &p_shaped, TextServer::Direction p_direction) { |
| 4578 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4579 | ERR_FAIL_COND_MSG(p_direction == DIRECTION_INHERITED, "Invalid text direction."); |
| 4580 | ERR_FAIL_NULL(sd); |
| 4581 | |
| 4582 | MutexLock lock(sd->mutex); |
| 4583 | if (sd->direction != p_direction) { |
| 4584 | if (sd->parent != RID()) { |
| 4585 | full_copy(sd); |
| 4586 | } |
| 4587 | sd->direction = p_direction; |
| 4588 | invalidate(sd, false); |
| 4589 | } |
| 4590 | } |
| 4591 | |
| 4592 | TextServer::Direction TextServerAdvanced::_shaped_text_get_direction(const RID &p_shaped) const { |
| 4593 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4594 | ERR_FAIL_NULL_V(sd, TextServer::DIRECTION_LTR); |
| 4595 | |
| 4596 | MutexLock lock(sd->mutex); |
| 4597 | return sd->direction; |
| 4598 | } |
| 4599 | |
| 4600 | TextServer::Direction TextServerAdvanced::_shaped_text_get_inferred_direction(const RID &p_shaped) const { |
| 4601 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4602 | ERR_FAIL_NULL_V(sd, TextServer::DIRECTION_LTR); |
| 4603 | |
| 4604 | MutexLock lock(sd->mutex); |
| 4605 | return sd->para_direction; |
| 4606 | } |
| 4607 | |
| 4608 | void TextServerAdvanced::_shaped_text_set_custom_punctuation(const RID &p_shaped, const String &p_punct) { |
| 4609 | _THREAD_SAFE_METHOD_ |
| 4610 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4611 | ERR_FAIL_NULL(sd); |
| 4612 | |
| 4613 | if (sd->custom_punct != p_punct) { |
| 4614 | if (sd->parent != RID()) { |
| 4615 | full_copy(sd); |
| 4616 | } |
| 4617 | sd->custom_punct = p_punct; |
| 4618 | invalidate(sd, false); |
| 4619 | } |
| 4620 | } |
| 4621 | |
| 4622 | String TextServerAdvanced::_shaped_text_get_custom_punctuation(const RID &p_shaped) const { |
| 4623 | _THREAD_SAFE_METHOD_ |
| 4624 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4625 | ERR_FAIL_NULL_V(sd, String()); |
| 4626 | return sd->custom_punct; |
| 4627 | } |
| 4628 | |
| 4629 | void TextServerAdvanced::_shaped_text_set_custom_ellipsis(const RID &p_shaped, int64_t p_char) { |
| 4630 | _THREAD_SAFE_METHOD_ |
| 4631 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4632 | ERR_FAIL_NULL(sd); |
| 4633 | sd->el_char = p_char; |
| 4634 | } |
| 4635 | |
| 4636 | int64_t TextServerAdvanced::_shaped_text_get_custom_ellipsis(const RID &p_shaped) const { |
| 4637 | _THREAD_SAFE_METHOD_ |
| 4638 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4639 | ERR_FAIL_NULL_V(sd, 0); |
| 4640 | return sd->el_char; |
| 4641 | } |
| 4642 | |
| 4643 | void TextServerAdvanced::_shaped_text_set_bidi_override(const RID &p_shaped, const Array &p_override) { |
| 4644 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4645 | ERR_FAIL_NULL(sd); |
| 4646 | |
| 4647 | MutexLock lock(sd->mutex); |
| 4648 | if (sd->parent != RID()) { |
| 4649 | full_copy(sd); |
| 4650 | } |
| 4651 | sd->bidi_override.clear(); |
| 4652 | for (int i = 0; i < p_override.size(); i++) { |
| 4653 | if (p_override[i].get_type() == Variant::VECTOR3I) { |
| 4654 | const Vector3i &r = p_override[i]; |
| 4655 | sd->bidi_override.push_back(r); |
| 4656 | } else if (p_override[i].get_type() == Variant::VECTOR2I) { |
| 4657 | const Vector2i &r = p_override[i]; |
| 4658 | sd->bidi_override.push_back(Vector3i(r.x, r.y, DIRECTION_INHERITED)); |
| 4659 | } |
| 4660 | } |
| 4661 | invalidate(sd, false); |
| 4662 | } |
| 4663 | |
| 4664 | void TextServerAdvanced::_shaped_text_set_orientation(const RID &p_shaped, TextServer::Orientation p_orientation) { |
| 4665 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4666 | ERR_FAIL_NULL(sd); |
| 4667 | |
| 4668 | MutexLock lock(sd->mutex); |
| 4669 | if (sd->orientation != p_orientation) { |
| 4670 | if (sd->parent != RID()) { |
| 4671 | full_copy(sd); |
| 4672 | } |
| 4673 | sd->orientation = p_orientation; |
| 4674 | invalidate(sd, false); |
| 4675 | } |
| 4676 | } |
| 4677 | |
| 4678 | void TextServerAdvanced::_shaped_text_set_preserve_invalid(const RID &p_shaped, bool p_enabled) { |
| 4679 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4680 | ERR_FAIL_NULL(sd); |
| 4681 | |
| 4682 | MutexLock lock(sd->mutex); |
| 4683 | ERR_FAIL_COND(sd->parent != RID()); |
| 4684 | if (sd->preserve_invalid != p_enabled) { |
| 4685 | sd->preserve_invalid = p_enabled; |
| 4686 | invalidate(sd, false); |
| 4687 | } |
| 4688 | } |
| 4689 | |
| 4690 | bool TextServerAdvanced::_shaped_text_get_preserve_invalid(const RID &p_shaped) const { |
| 4691 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4692 | ERR_FAIL_NULL_V(sd, false); |
| 4693 | |
| 4694 | MutexLock lock(sd->mutex); |
| 4695 | return sd->preserve_invalid; |
| 4696 | } |
| 4697 | |
| 4698 | void TextServerAdvanced::_shaped_text_set_preserve_control(const RID &p_shaped, bool p_enabled) { |
| 4699 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4700 | ERR_FAIL_NULL(sd); |
| 4701 | |
| 4702 | MutexLock lock(sd->mutex); |
| 4703 | if (sd->preserve_control != p_enabled) { |
| 4704 | if (sd->parent != RID()) { |
| 4705 | full_copy(sd); |
| 4706 | } |
| 4707 | sd->preserve_control = p_enabled; |
| 4708 | invalidate(sd, false); |
| 4709 | } |
| 4710 | } |
| 4711 | |
| 4712 | bool TextServerAdvanced::_shaped_text_get_preserve_control(const RID &p_shaped) const { |
| 4713 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4714 | ERR_FAIL_NULL_V(sd, false); |
| 4715 | |
| 4716 | MutexLock lock(sd->mutex); |
| 4717 | return sd->preserve_control; |
| 4718 | } |
| 4719 | |
| 4720 | void TextServerAdvanced::_shaped_text_set_spacing(const RID &p_shaped, SpacingType p_spacing, int64_t p_value) { |
| 4721 | ERR_FAIL_INDEX((int)p_spacing, 4); |
| 4722 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4723 | ERR_FAIL_NULL(sd); |
| 4724 | |
| 4725 | MutexLock lock(sd->mutex); |
| 4726 | if (sd->extra_spacing[p_spacing] != p_value) { |
| 4727 | if (sd->parent != RID()) { |
| 4728 | full_copy(sd); |
| 4729 | } |
| 4730 | sd->extra_spacing[p_spacing] = p_value; |
| 4731 | invalidate(sd, false); |
| 4732 | } |
| 4733 | } |
| 4734 | |
| 4735 | int64_t TextServerAdvanced::_shaped_text_get_spacing(const RID &p_shaped, SpacingType p_spacing) const { |
| 4736 | ERR_FAIL_INDEX_V((int)p_spacing, 4, 0); |
| 4737 | |
| 4738 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4739 | ERR_FAIL_NULL_V(sd, 0); |
| 4740 | |
| 4741 | MutexLock lock(sd->mutex); |
| 4742 | return sd->extra_spacing[p_spacing]; |
| 4743 | } |
| 4744 | |
| 4745 | TextServer::Orientation TextServerAdvanced::_shaped_text_get_orientation(const RID &p_shaped) const { |
| 4746 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4747 | ERR_FAIL_NULL_V(sd, TextServer::ORIENTATION_HORIZONTAL); |
| 4748 | |
| 4749 | MutexLock lock(sd->mutex); |
| 4750 | return sd->orientation; |
| 4751 | } |
| 4752 | |
| 4753 | int64_t TextServerAdvanced::_shaped_get_span_count(const RID &p_shaped) const { |
| 4754 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4755 | ERR_FAIL_NULL_V(sd, 0); |
| 4756 | |
| 4757 | if (sd->parent != RID()) { |
| 4758 | return sd->last_span - sd->first_span + 1; |
| 4759 | } else { |
| 4760 | return sd->spans.size(); |
| 4761 | } |
| 4762 | } |
| 4763 | |
| 4764 | Variant TextServerAdvanced::_shaped_get_span_meta(const RID &p_shaped, int64_t p_index) const { |
| 4765 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4766 | ERR_FAIL_NULL_V(sd, Variant()); |
| 4767 | if (sd->parent != RID()) { |
| 4768 | ShapedTextDataAdvanced *parent_sd = shaped_owner.get_or_null(sd->parent); |
| 4769 | ERR_FAIL_COND_V(!parent_sd->valid.is_set(), Variant()); |
| 4770 | ERR_FAIL_INDEX_V(p_index + sd->first_span, parent_sd->spans.size(), Variant()); |
| 4771 | return parent_sd->spans[p_index + sd->first_span].meta; |
| 4772 | } else { |
| 4773 | ERR_FAIL_INDEX_V(p_index, sd->spans.size(), Variant()); |
| 4774 | return sd->spans[p_index].meta; |
| 4775 | } |
| 4776 | } |
| 4777 | |
| 4778 | Variant TextServerAdvanced::_shaped_get_span_embedded_object(const RID &p_shaped, int64_t p_index) const { |
| 4779 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4780 | ERR_FAIL_NULL_V(sd, Variant()); |
| 4781 | if (sd->parent != RID()) { |
| 4782 | ShapedTextDataAdvanced *parent_sd = shaped_owner.get_or_null(sd->parent); |
| 4783 | ERR_FAIL_COND_V(!parent_sd->valid.is_set(), Variant()); |
| 4784 | ERR_FAIL_INDEX_V(p_index + sd->first_span, parent_sd->spans.size(), Variant()); |
| 4785 | return parent_sd->spans[p_index + sd->first_span].embedded_key; |
| 4786 | } else { |
| 4787 | ERR_FAIL_INDEX_V(p_index, sd->spans.size(), Variant()); |
| 4788 | return sd->spans[p_index].embedded_key; |
| 4789 | } |
| 4790 | } |
| 4791 | |
| 4792 | String TextServerAdvanced::_shaped_get_span_text(const RID &p_shaped, int64_t p_index) const { |
| 4793 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4794 | ERR_FAIL_NULL_V(sd, String()); |
| 4795 | ShapedTextDataAdvanced *span_sd = sd; |
| 4796 | if (sd->parent.is_valid()) { |
| 4797 | span_sd = shaped_owner.get_or_null(sd->parent); |
| 4798 | ERR_FAIL_NULL_V(span_sd, String()); |
| 4799 | } |
| 4800 | ERR_FAIL_INDEX_V(p_index, span_sd->spans.size(), String()); |
| 4801 | return span_sd->text.substr(span_sd->spans[p_index].start, span_sd->spans[p_index].end - span_sd->spans[p_index].start); |
| 4802 | } |
| 4803 | |
| 4804 | Variant TextServerAdvanced::_shaped_get_span_object(const RID &p_shaped, int64_t p_index) const { |
| 4805 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4806 | ERR_FAIL_NULL_V(sd, Variant()); |
| 4807 | ShapedTextDataAdvanced *span_sd = sd; |
| 4808 | if (sd->parent.is_valid()) { |
| 4809 | span_sd = shaped_owner.get_or_null(sd->parent); |
| 4810 | ERR_FAIL_NULL_V(span_sd, Variant()); |
| 4811 | } |
| 4812 | ERR_FAIL_INDEX_V(p_index, span_sd->spans.size(), Variant()); |
| 4813 | return span_sd->spans[p_index].embedded_key; |
| 4814 | } |
| 4815 | |
| 4816 | void TextServerAdvanced::_generate_runs(ShapedTextDataAdvanced *p_sd) const { |
| 4817 | ERR_FAIL_NULL(p_sd); |
| 4818 | p_sd->runs.clear(); |
| 4819 | |
| 4820 | ShapedTextDataAdvanced *span_sd = p_sd; |
| 4821 | if (p_sd->parent.is_valid()) { |
| 4822 | span_sd = shaped_owner.get_or_null(p_sd->parent); |
| 4823 | ERR_FAIL_NULL(span_sd); |
| 4824 | } |
| 4825 | |
| 4826 | int sd_size = p_sd->glyphs.size(); |
| 4827 | const Glyph *sd_gl = p_sd->glyphs.ptr(); |
| 4828 | |
| 4829 | int span_count = span_sd->spans.size(); |
| 4830 | int span = -1; |
| 4831 | int span_start = -1; |
| 4832 | int span_end = -1; |
| 4833 | |
| 4834 | TextRun run; |
| 4835 | for (int i = 0; i < sd_size; i += sd_gl[i].count) { |
| 4836 | const Glyph &gl = sd_gl[i]; |
| 4837 | if (gl.start < 0 || gl.end < 0) { |
| 4838 | continue; |
| 4839 | } |
| 4840 | if (gl.start < span_start || gl.start >= span_end) { |
| 4841 | span = -1; |
| 4842 | span_start = -1; |
| 4843 | span_end = -1; |
| 4844 | for (int j = 0; j < span_count; j++) { |
| 4845 | if (gl.start >= span_sd->spans[j].start && gl.end <= span_sd->spans[j].end) { |
| 4846 | span = j; |
| 4847 | span_start = span_sd->spans[j].start; |
| 4848 | span_end = span_sd->spans[j].end; |
| 4849 | break; |
| 4850 | } |
| 4851 | } |
| 4852 | } |
| 4853 | if (run.font_rid != gl.font_rid || run.font_size != gl.font_size || run.span_index != span || run.rtl != bool(gl.flags & GRAPHEME_IS_RTL)) { |
| 4854 | if (run.span_index >= 0) { |
| 4855 | p_sd->runs.push_back(run); |
| 4856 | } |
| 4857 | run.range = Vector2i(gl.start, gl.end); |
| 4858 | run.gl_range = Vector2i(i, i); |
| 4859 | run.font_rid = gl.font_rid; |
| 4860 | run.font_size = gl.font_size; |
| 4861 | run.rtl = bool(gl.flags & GRAPHEME_IS_RTL); |
| 4862 | run.span_index = span; |
| 4863 | } |
| 4864 | run.range.x = MIN(run.range.x, gl.start); |
| 4865 | run.range.y = MAX(run.range.y, gl.end); |
| 4866 | run.gl_range.x = MIN(run.gl_range.x, i); |
| 4867 | run.gl_range.y = MAX(run.gl_range.y, i); |
| 4868 | } |
| 4869 | if (run.span_index >= 0) { |
| 4870 | p_sd->runs.push_back(run); |
| 4871 | } |
| 4872 | p_sd->runs_dirty = false; |
| 4873 | } |
| 4874 | |
| 4875 | int64_t TextServerAdvanced::_shaped_get_run_count(const RID &p_shaped) const { |
| 4876 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4877 | ERR_FAIL_NULL_V(sd, 0); |
| 4878 | MutexLock lock(sd->mutex); |
| 4879 | if (!sd->valid.is_set()) { |
| 4880 | const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped); |
| 4881 | } |
| 4882 | if (sd->runs_dirty) { |
| 4883 | _generate_runs(sd); |
| 4884 | } |
| 4885 | return sd->runs.size(); |
| 4886 | } |
| 4887 | |
| 4888 | String TextServerAdvanced::_shaped_get_run_text(const RID &p_shaped, int64_t p_index) const { |
| 4889 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4890 | ERR_FAIL_NULL_V(sd, String()); |
| 4891 | MutexLock lock(sd->mutex); |
| 4892 | if (!sd->valid.is_set()) { |
| 4893 | const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped); |
| 4894 | } |
| 4895 | if (sd->runs_dirty) { |
| 4896 | _generate_runs(sd); |
| 4897 | } |
| 4898 | ERR_FAIL_INDEX_V(p_index, sd->runs.size(), String()); |
| 4899 | return sd->text.substr(sd->runs[p_index].range.x - sd->start, sd->runs[p_index].range.y - sd->runs[p_index].range.x); |
| 4900 | } |
| 4901 | |
| 4902 | Vector2i TextServerAdvanced::_shaped_get_run_range(const RID &p_shaped, int64_t p_index) const { |
| 4903 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4904 | ERR_FAIL_NULL_V(sd, Vector2i()); |
| 4905 | MutexLock lock(sd->mutex); |
| 4906 | if (!sd->valid.is_set()) { |
| 4907 | const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped); |
| 4908 | } |
| 4909 | if (sd->runs_dirty) { |
| 4910 | _generate_runs(sd); |
| 4911 | } |
| 4912 | ERR_FAIL_INDEX_V(p_index, sd->runs.size(), Vector2i()); |
| 4913 | return sd->runs[p_index].range; |
| 4914 | } |
| 4915 | |
| 4916 | Vector2i TextServerAdvanced::_shaped_get_run_glyph_range(const RID &p_shaped, int64_t p_index) const { |
| 4917 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4918 | ERR_FAIL_NULL_V(sd, Vector2i()); |
| 4919 | MutexLock lock(sd->mutex); |
| 4920 | if (!sd->valid.is_set()) { |
| 4921 | const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped); |
| 4922 | } |
| 4923 | if (sd->runs_dirty) { |
| 4924 | _generate_runs(sd); |
| 4925 | } |
| 4926 | ERR_FAIL_INDEX_V(p_index, sd->runs.size(), Vector2i()); |
| 4927 | return sd->runs[p_index].gl_range; |
| 4928 | } |
| 4929 | |
| 4930 | RID TextServerAdvanced::_shaped_get_run_font_rid(const RID &p_shaped, int64_t p_index) const { |
| 4931 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4932 | ERR_FAIL_NULL_V(sd, RID()); |
| 4933 | MutexLock lock(sd->mutex); |
| 4934 | if (!sd->valid.is_set()) { |
| 4935 | const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped); |
| 4936 | } |
| 4937 | if (sd->runs_dirty) { |
| 4938 | _generate_runs(sd); |
| 4939 | } |
| 4940 | ERR_FAIL_INDEX_V(p_index, sd->runs.size(), RID()); |
| 4941 | return sd->runs[p_index].font_rid; |
| 4942 | } |
| 4943 | |
| 4944 | int TextServerAdvanced::_shaped_get_run_font_size(const RID &p_shaped, int64_t p_index) const { |
| 4945 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4946 | ERR_FAIL_NULL_V(sd, 0); |
| 4947 | MutexLock lock(sd->mutex); |
| 4948 | if (!sd->valid.is_set()) { |
| 4949 | const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped); |
| 4950 | } |
| 4951 | if (sd->runs_dirty) { |
| 4952 | _generate_runs(sd); |
| 4953 | } |
| 4954 | ERR_FAIL_INDEX_V(p_index, sd->runs.size(), 0); |
| 4955 | return sd->runs[p_index].font_size; |
| 4956 | } |
| 4957 | |
| 4958 | String TextServerAdvanced::_shaped_get_run_language(const RID &p_shaped, int64_t p_index) const { |
| 4959 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4960 | ERR_FAIL_NULL_V(sd, String()); |
| 4961 | MutexLock lock(sd->mutex); |
| 4962 | if (!sd->valid.is_set()) { |
| 4963 | const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped); |
| 4964 | } |
| 4965 | if (sd->runs_dirty) { |
| 4966 | _generate_runs(sd); |
| 4967 | } |
| 4968 | ERR_FAIL_INDEX_V(p_index, sd->runs.size(), String()); |
| 4969 | |
| 4970 | int span_idx = sd->runs[p_index].span_index; |
| 4971 | ShapedTextDataAdvanced *span_sd = sd; |
| 4972 | if (sd->parent.is_valid()) { |
| 4973 | span_sd = shaped_owner.get_or_null(sd->parent); |
| 4974 | ERR_FAIL_NULL_V(span_sd, String()); |
| 4975 | } |
| 4976 | ERR_FAIL_INDEX_V(span_idx, span_sd->spans.size(), String()); |
| 4977 | return span_sd->spans[span_idx].language; |
| 4978 | } |
| 4979 | |
| 4980 | TextServer::Direction TextServerAdvanced::_shaped_get_run_direction(const RID &p_shaped, int64_t p_index) const { |
| 4981 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4982 | ERR_FAIL_NULL_V(sd, TextServer::DIRECTION_LTR); |
| 4983 | MutexLock lock(sd->mutex); |
| 4984 | if (!sd->valid.is_set()) { |
| 4985 | const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped); |
| 4986 | } |
| 4987 | if (sd->runs_dirty) { |
| 4988 | _generate_runs(sd); |
| 4989 | } |
| 4990 | ERR_FAIL_INDEX_V(p_index, sd->runs.size(), TextServer::DIRECTION_LTR); |
| 4991 | return sd->runs[p_index].rtl ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR; |
| 4992 | } |
| 4993 | |
| 4994 | Variant TextServerAdvanced::_shaped_get_run_object(const RID &p_shaped, int64_t p_index) const { |
| 4995 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 4996 | ERR_FAIL_NULL_V(sd, Variant()); |
| 4997 | MutexLock lock(sd->mutex); |
| 4998 | if (!sd->valid.is_set()) { |
| 4999 | const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped); |
| 5000 | } |
| 5001 | if (sd->runs_dirty) { |
| 5002 | _generate_runs(sd); |
| 5003 | } |
| 5004 | ERR_FAIL_INDEX_V(p_index, sd->runs.size(), Variant()); |
| 5005 | |
| 5006 | int span_idx = sd->runs[p_index].span_index; |
| 5007 | ShapedTextDataAdvanced *span_sd = sd; |
| 5008 | if (sd->parent.is_valid()) { |
| 5009 | span_sd = shaped_owner.get_or_null(sd->parent); |
| 5010 | ERR_FAIL_NULL_V(span_sd, Variant()); |
| 5011 | } |
| 5012 | ERR_FAIL_INDEX_V(span_idx, span_sd->spans.size(), Variant()); |
| 5013 | return span_sd->spans[span_idx].embedded_key; |
| 5014 | } |
| 5015 | |
| 5016 | void TextServerAdvanced::_shaped_set_span_update_font(const RID &p_shaped, int64_t p_index, const TypedArray<RID> &p_fonts, int64_t p_size, const Dictionary &p_opentype_features) { |
| 5017 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 5018 | ERR_FAIL_NULL(sd); |
| 5019 | if (sd->parent != RID()) { |
| 5020 | full_copy(sd); |
| 5021 | } |
| 5022 | ERR_FAIL_INDEX(p_index, sd->spans.size()); |
| 5023 | |
| 5024 | ShapedTextDataAdvanced::Span &span = sd->spans.ptrw()[p_index]; |
| 5025 | span.fonts = p_fonts; |
| 5026 | span.font_size = p_size; |
| 5027 | span.features = p_opentype_features; |
| 5028 | |
| 5029 | invalidate(sd, false); |
| 5030 | } |
| 5031 | |
| 5032 | bool TextServerAdvanced::_shaped_text_add_string(const RID &p_shaped, const String &p_text, const TypedArray<RID> &p_fonts, int64_t p_size, const Dictionary &p_opentype_features, const String &p_language, const Variant &p_meta) { |
| 5033 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 5034 | ERR_FAIL_NULL_V(sd, false); |
| 5035 | ERR_FAIL_COND_V(p_size <= 0, false); |
| 5036 | |
| 5037 | MutexLock lock(sd->mutex); |
| 5038 | for (int i = 0; i < p_fonts.size(); i++) { |
| 5039 | ERR_FAIL_NULL_V(_get_font_data(p_fonts[i]), false); |
| 5040 | } |
| 5041 | |
| 5042 | if (p_text.is_empty()) { |
| 5043 | return true; |
| 5044 | } |
| 5045 | |
| 5046 | if (sd->parent != RID()) { |
| 5047 | full_copy(sd); |
| 5048 | } |
| 5049 | |
| 5050 | ShapedTextDataAdvanced::Span span; |
| 5051 | span.start = sd->text.length(); |
| 5052 | span.end = span.start + p_text.length(); |
| 5053 | span.fonts = p_fonts; // Do not pre-sort, spans will be divided to subruns later. |
| 5054 | span.font_size = p_size; |
| 5055 | span.language = p_language; |
| 5056 | span.features = p_opentype_features; |
| 5057 | span.meta = p_meta; |
| 5058 | |
| 5059 | sd->spans.push_back(span); |
| 5060 | sd->text = sd->text + p_text; |
| 5061 | sd->end += p_text.length(); |
| 5062 | invalidate(sd, true); |
| 5063 | |
| 5064 | return true; |
| 5065 | } |
| 5066 | |
| 5067 | bool TextServerAdvanced::_shaped_text_add_object(const RID &p_shaped, const Variant &p_key, const Size2 &p_size, InlineAlignment p_inline_align, int64_t p_length, double p_baseline) { |
| 5068 | _THREAD_SAFE_METHOD_ |
| 5069 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 5070 | ERR_FAIL_NULL_V(sd, false); |
| 5071 | ERR_FAIL_COND_V(p_key == Variant(), false); |
| 5072 | ERR_FAIL_COND_V(sd->objects.has(p_key), false); |
| 5073 | |
| 5074 | if (sd->parent != RID()) { |
| 5075 | full_copy(sd); |
| 5076 | } |
| 5077 | |
| 5078 | ShapedTextDataAdvanced::Span span; |
| 5079 | span.start = sd->start + sd->text.length(); |
| 5080 | span.end = span.start + p_length; |
| 5081 | span.embedded_key = p_key; |
| 5082 | |
| 5083 | ShapedTextDataAdvanced::EmbeddedObject obj; |
| 5084 | obj.inline_align = p_inline_align; |
| 5085 | obj.rect.size = p_size; |
| 5086 | obj.start = span.start; |
| 5087 | obj.end = span.end; |
| 5088 | obj.baseline = p_baseline; |
| 5089 | |
| 5090 | sd->spans.push_back(span); |
| 5091 | sd->text = sd->text + String::chr(0xfffc).repeat(p_length); |
| 5092 | sd->end += p_length; |
| 5093 | sd->objects[p_key] = obj; |
| 5094 | invalidate(sd, true); |
| 5095 | |
| 5096 | return true; |
| 5097 | } |
| 5098 | |
| 5099 | String TextServerAdvanced::_shaped_get_text(const RID &p_shaped) const { |
| 5100 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 5101 | ERR_FAIL_NULL_V(sd, String()); |
| 5102 | |
| 5103 | return sd->text; |
| 5104 | } |
| 5105 | |
| 5106 | bool TextServerAdvanced::_shaped_text_has_object(const RID &p_shaped, const Variant &p_key) const { |
| 5107 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 5108 | ERR_FAIL_NULL_V(sd, false); |
| 5109 | |
| 5110 | MutexLock lock(sd->mutex); |
| 5111 | return sd->objects.has(p_key); |
| 5112 | } |
| 5113 | |
| 5114 | bool TextServerAdvanced::_shaped_text_resize_object(const RID &p_shaped, const Variant &p_key, const Size2 &p_size, InlineAlignment p_inline_align, double p_baseline) { |
| 5115 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 5116 | ERR_FAIL_NULL_V(sd, false); |
| 5117 | |
| 5118 | MutexLock lock(sd->mutex); |
| 5119 | ERR_FAIL_COND_V(!sd->objects.has(p_key), false); |
| 5120 | sd->objects[p_key].rect.size = p_size; |
| 5121 | sd->objects[p_key].inline_align = p_inline_align; |
| 5122 | sd->objects[p_key].baseline = p_baseline; |
| 5123 | if (sd->valid.is_set()) { |
| 5124 | // Recalc string metrics. |
| 5125 | sd->ascent = 0; |
| 5126 | sd->descent = 0; |
| 5127 | sd->width = 0; |
| 5128 | sd->upos = 0; |
| 5129 | sd->uthk = 0; |
| 5130 | |
| 5131 | Vector<ShapedTextDataAdvanced::Span> &spans = sd->spans; |
| 5132 | if (sd->parent != RID()) { |
| 5133 | ShapedTextDataAdvanced *parent_sd = shaped_owner.get_or_null(sd->parent); |
| 5134 | ERR_FAIL_COND_V(!parent_sd->valid.is_set(), false); |
| 5135 | spans = parent_sd->spans; |
| 5136 | } |
| 5137 | |
| 5138 | int sd_size = sd->glyphs.size(); |
| 5139 | int span_size = spans.size(); |
| 5140 | const char32_t *ch = sd->text.ptr(); |
| 5141 | |
| 5142 | for (int i = 0; i < sd_size; i++) { |
| 5143 | Glyph gl = sd->glyphs[i]; |
| 5144 | Variant key; |
| 5145 | if ((gl.flags & GRAPHEME_IS_EMBEDDED_OBJECT) == GRAPHEME_IS_EMBEDDED_OBJECT && gl.span_index + sd->first_span >= 0 && gl.span_index + sd->first_span < span_size) { |
| 5146 | key = spans[gl.span_index + sd->first_span].embedded_key; |
| 5147 | } |
| 5148 | if (key != Variant()) { |
| 5149 | if (sd->orientation == ORIENTATION_HORIZONTAL) { |
| 5150 | sd->objects[key].rect.position.x = sd->width; |
| 5151 | sd->width += sd->objects[key].rect.size.x; |
| 5152 | sd->glyphs[i].advance = sd->objects[key].rect.size.x; |
| 5153 | } else { |
| 5154 | sd->objects[key].rect.position.y = sd->width; |
| 5155 | sd->width += sd->objects[key].rect.size.y; |
| 5156 | sd->glyphs[i].advance = sd->objects[key].rect.size.y; |
| 5157 | } |
| 5158 | } else { |
| 5159 | if (gl.font_rid.is_valid()) { |
| 5160 | if (sd->orientation == ORIENTATION_HORIZONTAL) { |
| 5161 | sd->ascent = MAX(sd->ascent, MAX(_font_get_ascent(gl.font_rid, gl.font_size) + _font_get_spacing(gl.font_rid, SPACING_TOP), -gl.y_off)); |
| 5162 | sd->descent = MAX(sd->descent, MAX(_font_get_descent(gl.font_rid, gl.font_size) + _font_get_spacing(gl.font_rid, SPACING_BOTTOM), gl.y_off)); |
| 5163 | } else { |
| 5164 | sd->ascent = MAX(sd->ascent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5)); |
| 5165 | sd->descent = MAX(sd->descent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5)); |
| 5166 | } |
| 5167 | sd->upos = MAX(sd->upos, _font_get_underline_position(gl.font_rid, gl.font_size)); |
| 5168 | sd->uthk = MAX(sd->uthk, _font_get_underline_thickness(gl.font_rid, gl.font_size)); |
| 5169 | } else if (sd->preserve_invalid || (sd->preserve_control && is_control(ch[gl.start - sd->start]))) { |
| 5170 | // Glyph not found, replace with hex code box. |
| 5171 | if (sd->orientation == ORIENTATION_HORIZONTAL) { |
| 5172 | sd->ascent = MAX(sd->ascent, get_hex_code_box_size(gl.font_size, gl.index).y * 0.85); |
| 5173 | sd->descent = MAX(sd->descent, get_hex_code_box_size(gl.font_size, gl.index).y * 0.15); |
| 5174 | } else { |
| 5175 | sd->ascent = MAX(sd->ascent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5)); |
| 5176 | sd->descent = MAX(sd->descent, Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5)); |
| 5177 | } |
| 5178 | } |
| 5179 | sd->width += gl.advance * gl.repeat; |
| 5180 | } |
| 5181 | } |
| 5182 | sd->sort_valid = false; |
| 5183 | sd->glyphs_logical.clear(); |
| 5184 | _realign(sd); |
| 5185 | } |
| 5186 | return true; |
| 5187 | } |
| 5188 | |
| 5189 | void TextServerAdvanced::_realign(ShapedTextDataAdvanced *p_sd) const { |
| 5190 | // Align embedded objects to baseline. |
| 5191 | double full_ascent = p_sd->ascent; |
| 5192 | double full_descent = p_sd->descent; |
| 5193 | for (KeyValue<Variant, ShapedTextDataAdvanced::EmbeddedObject> &E : p_sd->objects) { |
| 5194 | if ((E.value.start >= p_sd->start) && (E.value.start < p_sd->end)) { |
| 5195 | if (p_sd->orientation == ORIENTATION_HORIZONTAL) { |
| 5196 | switch (E.value.inline_align & INLINE_ALIGNMENT_TEXT_MASK) { |
| 5197 | case INLINE_ALIGNMENT_TO_TOP: { |
| 5198 | E.value.rect.position.y = -p_sd->ascent; |
| 5199 | } break; |
| 5200 | case INLINE_ALIGNMENT_TO_CENTER: { |
| 5201 | E.value.rect.position.y = (-p_sd->ascent + p_sd->descent) / 2; |
| 5202 | } break; |
| 5203 | case INLINE_ALIGNMENT_TO_BASELINE: { |
| 5204 | E.value.rect.position.y = 0; |
| 5205 | } break; |
| 5206 | case INLINE_ALIGNMENT_TO_BOTTOM: { |
| 5207 | E.value.rect.position.y = p_sd->descent; |
| 5208 | } break; |
| 5209 | } |
| 5210 | switch (E.value.inline_align & INLINE_ALIGNMENT_IMAGE_MASK) { |
| 5211 | case INLINE_ALIGNMENT_BOTTOM_TO: { |
| 5212 | E.value.rect.position.y -= E.value.rect.size.y; |
| 5213 | } break; |
| 5214 | case INLINE_ALIGNMENT_CENTER_TO: { |
| 5215 | E.value.rect.position.y -= E.value.rect.size.y / 2; |
| 5216 | } break; |
| 5217 | case INLINE_ALIGNMENT_BASELINE_TO: { |
| 5218 | E.value.rect.position.y -= E.value.baseline; |
| 5219 | } break; |
| 5220 | case INLINE_ALIGNMENT_TOP_TO: { |
| 5221 | // NOP |
| 5222 | } break; |
| 5223 | } |
| 5224 | full_ascent = MAX(full_ascent, -E.value.rect.position.y); |
| 5225 | full_descent = MAX(full_descent, E.value.rect.position.y + E.value.rect.size.y); |
| 5226 | } else { |
| 5227 | switch (E.value.inline_align & INLINE_ALIGNMENT_TEXT_MASK) { |
| 5228 | case INLINE_ALIGNMENT_TO_TOP: { |
| 5229 | E.value.rect.position.x = -p_sd->ascent; |
| 5230 | } break; |
| 5231 | case INLINE_ALIGNMENT_TO_CENTER: { |
| 5232 | E.value.rect.position.x = (-p_sd->ascent + p_sd->descent) / 2; |
| 5233 | } break; |
| 5234 | case INLINE_ALIGNMENT_TO_BASELINE: { |
| 5235 | E.value.rect.position.x = 0; |
| 5236 | } break; |
| 5237 | case INLINE_ALIGNMENT_TO_BOTTOM: { |
| 5238 | E.value.rect.position.x = p_sd->descent; |
| 5239 | } break; |
| 5240 | } |
| 5241 | switch (E.value.inline_align & INLINE_ALIGNMENT_IMAGE_MASK) { |
| 5242 | case INLINE_ALIGNMENT_BOTTOM_TO: { |
| 5243 | E.value.rect.position.x -= E.value.rect.size.x; |
| 5244 | } break; |
| 5245 | case INLINE_ALIGNMENT_CENTER_TO: { |
| 5246 | E.value.rect.position.x -= E.value.rect.size.x / 2; |
| 5247 | } break; |
| 5248 | case INLINE_ALIGNMENT_BASELINE_TO: { |
| 5249 | E.value.rect.position.x -= E.value.baseline; |
| 5250 | } break; |
| 5251 | case INLINE_ALIGNMENT_TOP_TO: { |
| 5252 | // NOP |
| 5253 | } break; |
| 5254 | } |
| 5255 | full_ascent = MAX(full_ascent, -E.value.rect.position.x); |
| 5256 | full_descent = MAX(full_descent, E.value.rect.position.x + E.value.rect.size.x); |
| 5257 | } |
| 5258 | } |
| 5259 | } |
| 5260 | p_sd->ascent = full_ascent; |
| 5261 | p_sd->descent = full_descent; |
| 5262 | } |
| 5263 | |
| 5264 | RID TextServerAdvanced::_shaped_text_substr(const RID &p_shaped, int64_t p_start, int64_t p_length) const { |
| 5265 | _THREAD_SAFE_METHOD_ |
| 5266 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 5267 | ERR_FAIL_NULL_V(sd, RID()); |
| 5268 | |
| 5269 | MutexLock lock(sd->mutex); |
| 5270 | if (sd->parent != RID()) { |
| 5271 | return _shaped_text_substr(sd->parent, p_start, p_length); |
| 5272 | } |
| 5273 | if (!sd->valid.is_set()) { |
| 5274 | const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped); |
| 5275 | } |
| 5276 | ERR_FAIL_COND_V(p_start < 0 || p_length < 0, RID()); |
| 5277 | ERR_FAIL_COND_V(sd->start > p_start || sd->end < p_start, RID()); |
| 5278 | ERR_FAIL_COND_V(sd->end < p_start + p_length, RID()); |
| 5279 | |
| 5280 | ShapedTextDataAdvanced *new_sd = memnew(ShapedTextDataAdvanced); |
| 5281 | new_sd->parent = p_shaped; |
| 5282 | new_sd->start = p_start; |
| 5283 | new_sd->end = p_start + p_length; |
| 5284 | new_sd->orientation = sd->orientation; |
| 5285 | new_sd->direction = sd->direction; |
| 5286 | new_sd->custom_punct = sd->custom_punct; |
| 5287 | new_sd->para_direction = sd->para_direction; |
| 5288 | new_sd->base_para_direction = sd->base_para_direction; |
| 5289 | for (int i = 0; i < TextServer::SPACING_MAX; i++) { |
| 5290 | new_sd->extra_spacing[i] = sd->extra_spacing[i]; |
| 5291 | } |
| 5292 | |
| 5293 | if (!_shape_substr(new_sd, sd, p_start, p_length)) { |
| 5294 | memdelete(new_sd); |
| 5295 | return RID(); |
| 5296 | } |
| 5297 | return shaped_owner.make_rid(new_sd); |
| 5298 | } |
| 5299 | |
| 5300 | bool TextServerAdvanced::_shape_substr(ShapedTextDataAdvanced *p_new_sd, const ShapedTextDataAdvanced *p_sd, int64_t p_start, int64_t p_length) const { |
| 5301 | if (p_new_sd->valid.is_set()) { |
| 5302 | return true; |
| 5303 | } |
| 5304 | |
| 5305 | p_new_sd->hb_buffer = hb_buffer_create(); |
| 5306 | |
| 5307 | p_new_sd->line_breaks_valid = p_sd->line_breaks_valid; |
| 5308 | p_new_sd->justification_ops_valid = p_sd->justification_ops_valid; |
| 5309 | p_new_sd->sort_valid = false; |
| 5310 | p_new_sd->upos = p_sd->upos; |
| 5311 | p_new_sd->uthk = p_sd->uthk; |
| 5312 | p_new_sd->runs.clear(); |
| 5313 | p_new_sd->runs_dirty = true; |
| 5314 | |
| 5315 | if (p_length > 0) { |
| 5316 | p_new_sd->text = p_sd->text.substr(p_start - p_sd->start, p_length); |
| 5317 | p_new_sd->utf16 = p_new_sd->text.utf16(); |
| 5318 | p_new_sd->script_iter = memnew(ScriptIterator(p_new_sd->text, 0, p_new_sd->text.length())); |
| 5319 | |
| 5320 | int span_size = p_sd->spans.size(); |
| 5321 | |
| 5322 | p_new_sd->first_span = 0; |
| 5323 | p_new_sd->last_span = span_size - 1; |
| 5324 | for (int i = 0; i < span_size; i++) { |
| 5325 | const ShapedTextDataAdvanced::Span &span = p_sd->spans[i]; |
| 5326 | if (span.end <= p_start) { |
| 5327 | p_new_sd->first_span = i + 1; |
| 5328 | } else if (span.start >= p_start + p_length) { |
| 5329 | p_new_sd->last_span = i - 1; |
| 5330 | break; |
| 5331 | } |
| 5332 | } |
| 5333 | |
| 5334 | Vector<Vector3i> bidi_ranges; |
| 5335 | if (p_sd->bidi_override.is_empty()) { |
| 5336 | bidi_ranges.push_back(Vector3i(p_sd->start, p_sd->end, DIRECTION_INHERITED)); |
| 5337 | } else { |
| 5338 | bidi_ranges = p_sd->bidi_override; |
| 5339 | } |
| 5340 | |
| 5341 | int sd_size = p_sd->glyphs.size(); |
| 5342 | const Glyph *sd_glyphs = p_sd->glyphs.ptr(); |
| 5343 | const char32_t *ch = p_sd->text.ptr(); |
| 5344 | for (int ov = 0; ov < bidi_ranges.size(); ov++) { |
| 5345 | UErrorCode err = U_ZERO_ERROR; |
| 5346 | |
| 5347 | if (bidi_ranges[ov].x >= p_start + p_length || bidi_ranges[ov].y <= p_start) { |
| 5348 | continue; |
| 5349 | } |
| 5350 | int ov_start = _convert_pos_inv(p_sd, bidi_ranges[ov].x); |
| 5351 | int start = MAX(0, _convert_pos_inv(p_sd, p_start) - ov_start); |
| 5352 | int end = MIN(_convert_pos_inv(p_sd, p_start + p_length), _convert_pos_inv(p_sd, bidi_ranges[ov].y)) - ov_start; |
| 5353 | |
| 5354 | ERR_FAIL_COND_V_MSG((start < 0 || end - start > p_new_sd->utf16.length()), false, "Invalid BiDi override range."); |
| 5355 | |
| 5356 | // Create temporary line bidi & shape. |
| 5357 | UBiDi *bidi_iter = nullptr; |
| 5358 | if (p_sd->bidi_iter[ov]) { |
| 5359 | bidi_iter = ubidi_openSized(end - start, 0, &err); |
| 5360 | if (U_SUCCESS(err)) { |
| 5361 | ubidi_setLine(p_sd->bidi_iter[ov], start, end, bidi_iter, &err); |
| 5362 | if (U_FAILURE(err)) { |
| 5363 | // Line BiDi failed (string contains incompatible control characters), try full paragraph BiDi instead. |
| 5364 | err = U_ZERO_ERROR; |
| 5365 | const UChar *data = p_sd->utf16.get_data(); |
| 5366 | switch (static_cast<TextServer::Direction>(bidi_ranges[ov].z)) { |
| 5367 | case DIRECTION_LTR: { |
| 5368 | ubidi_setPara(bidi_iter, data + start, end - start, UBIDI_LTR, nullptr, &err); |
| 5369 | } break; |
| 5370 | case DIRECTION_RTL: { |
| 5371 | ubidi_setPara(bidi_iter, data + start, end - start, UBIDI_RTL, nullptr, &err); |
| 5372 | } break; |
| 5373 | case DIRECTION_INHERITED: { |
| 5374 | ubidi_setPara(bidi_iter, data + start, end - start, p_sd->base_para_direction, nullptr, &err); |
| 5375 | } break; |
| 5376 | case DIRECTION_AUTO: { |
| 5377 | UBiDiDirection direction = ubidi_getBaseDirection(data + start, end - start); |
| 5378 | if (direction != UBIDI_NEUTRAL) { |
| 5379 | ubidi_setPara(bidi_iter, data + start, end - start, direction, nullptr, &err); |
| 5380 | } else { |
| 5381 | ubidi_setPara(bidi_iter, data + start, end - start, p_sd->base_para_direction, nullptr, &err); |
| 5382 | } |
| 5383 | } break; |
| 5384 | } |
| 5385 | if (U_FAILURE(err)) { |
| 5386 | ubidi_close(bidi_iter); |
| 5387 | bidi_iter = nullptr; |
| 5388 | ERR_PRINT(vformat("BiDi reordering for the line failed: %s", u_errorName(err))); |
| 5389 | } |
| 5390 | } |
| 5391 | } else { |
| 5392 | bidi_iter = nullptr; |
| 5393 | ERR_PRINT(vformat("BiDi iterator allocation for the line failed: %s", u_errorName(err))); |
| 5394 | } |
| 5395 | } |
| 5396 | p_new_sd->bidi_iter.push_back(bidi_iter); |
| 5397 | |
| 5398 | err = U_ZERO_ERROR; |
| 5399 | int bidi_run_count = 1; |
| 5400 | if (bidi_iter) { |
| 5401 | bidi_run_count = ubidi_countRuns(bidi_iter, &err); |
| 5402 | if (U_FAILURE(err)) { |
| 5403 | ERR_PRINT(u_errorName(err)); |
| 5404 | } |
| 5405 | } |
| 5406 | for (int i = 0; i < bidi_run_count; i++) { |
| 5407 | int32_t _bidi_run_start = 0; |
| 5408 | int32_t _bidi_run_length = end - start; |
| 5409 | if (bidi_iter) { |
| 5410 | ubidi_getVisualRun(bidi_iter, i, &_bidi_run_start, &_bidi_run_length); |
| 5411 | } |
| 5412 | |
| 5413 | int32_t bidi_run_start = _convert_pos(p_sd, ov_start + start + _bidi_run_start); |
| 5414 | int32_t bidi_run_end = _convert_pos(p_sd, ov_start + start + _bidi_run_start + _bidi_run_length); |
| 5415 | |
| 5416 | bool cache_valid = false; |
| 5417 | int cached_font_size = -1; |
| 5418 | RID cached_font_rid = RID(); |
| 5419 | double cached_font_ascent = 0; |
| 5420 | double cached_font_descent = 0; |
| 5421 | double cached_font_top_spacing = 0; |
| 5422 | double cached_font_bottom_spacing = 0; |
| 5423 | p_new_sd->glyphs.reserve(p_new_sd->glyphs.size() + MIN(sd_size, bidi_run_end - bidi_run_start)); |
| 5424 | for (int j = 0; j < sd_size; j++) { |
| 5425 | int col_key_off = (sd_glyphs[j].start == sd_glyphs[j].end) ? 1 : 0; |
| 5426 | if ((sd_glyphs[j].start >= bidi_run_start) && (sd_glyphs[j].end <= bidi_run_end - col_key_off)) { |
| 5427 | // Copy glyphs. |
| 5428 | Glyph gl = sd_glyphs[j]; |
| 5429 | if (gl.span_index >= 0) { |
| 5430 | gl.span_index -= p_new_sd->first_span; |
| 5431 | } |
| 5432 | if (gl.end == p_start + p_length && ((gl.flags & GRAPHEME_IS_SOFT_HYPHEN) == GRAPHEME_IS_SOFT_HYPHEN)) { |
| 5433 | uint32_t index = font_get_glyph_index(gl.font_rid, gl.font_size, 0x00ad, 0); |
| 5434 | if (index == 0) { // Try other fonts in the span. |
| 5435 | const ShapedTextDataAdvanced::Span &span = p_sd->spans[gl.span_index + p_new_sd->first_span]; |
| 5436 | for (int k = 0; k < span.fonts.size(); k++) { |
| 5437 | if (span.fonts[k] != gl.font_rid) { |
| 5438 | index = font_get_glyph_index(span.fonts[k], gl.font_size, 0x00ad, 0); |
| 5439 | if (index != 0) { |
| 5440 | gl.font_rid = span.fonts[k]; |
| 5441 | break; |
| 5442 | } |
| 5443 | } |
| 5444 | } |
| 5445 | } |
| 5446 | if (index == 0 && gl.font_rid.is_valid() && OS::get_singleton()->has_feature("system_fonts") && _font_is_allow_system_fallback(gl.font_rid)) { // Try system font fallback. |
| 5447 | const char32_t u32str[] = { 0x00ad, 0 }; |
| 5448 | RID rid = const_cast<TextServerAdvanced *>(this)->_find_sys_font_for_text(gl.font_rid, String(), String(), u32str); |
| 5449 | if (rid.is_valid()) { |
| 5450 | index = font_get_glyph_index(rid, gl.font_size, 0x00ad, 0); |
| 5451 | if (index != 0) { |
| 5452 | gl.font_rid = rid; |
| 5453 | } |
| 5454 | } |
| 5455 | } |
| 5456 | float w = font_get_glyph_advance(gl.font_rid, gl.font_size, index)[(p_new_sd->orientation == ORIENTATION_HORIZONTAL) ? 0 : 1]; |
| 5457 | gl.index = index; |
| 5458 | gl.advance = w; |
| 5459 | } |
| 5460 | if ((gl.flags & GRAPHEME_IS_EMBEDDED_OBJECT) == GRAPHEME_IS_EMBEDDED_OBJECT && gl.span_index + p_new_sd->first_span >= 0 && gl.span_index + p_new_sd->first_span < span_size) { |
| 5461 | Variant key = p_sd->spans[gl.span_index + p_new_sd->first_span].embedded_key; |
| 5462 | if (key != Variant()) { |
| 5463 | ShapedTextDataAdvanced::EmbeddedObject obj = p_sd->objects[key]; |
| 5464 | if (p_new_sd->orientation == ORIENTATION_HORIZONTAL) { |
| 5465 | obj.rect.position.x = p_new_sd->width; |
| 5466 | p_new_sd->width += obj.rect.size.x; |
| 5467 | } else { |
| 5468 | obj.rect.position.y = p_new_sd->width; |
| 5469 | p_new_sd->width += obj.rect.size.y; |
| 5470 | } |
| 5471 | p_new_sd->objects[key] = obj; |
| 5472 | } |
| 5473 | } else { |
| 5474 | if (gl.font_rid.is_valid()) { |
| 5475 | if (p_new_sd->orientation == ORIENTATION_HORIZONTAL) { |
| 5476 | if (!cache_valid || cached_font_rid != gl.font_rid || cached_font_size != gl.font_size) { |
| 5477 | cache_valid = true; |
| 5478 | cached_font_rid = gl.font_rid; |
| 5479 | cached_font_size = gl.font_size; |
| 5480 | cached_font_ascent = _font_get_ascent(gl.font_rid, gl.font_size); |
| 5481 | cached_font_descent = _font_get_descent(gl.font_rid, gl.font_size); |
| 5482 | cached_font_top_spacing = _font_get_spacing(gl.font_rid, SPACING_TOP); |
| 5483 | cached_font_bottom_spacing = _font_get_spacing(gl.font_rid, SPACING_BOTTOM); |
| 5484 | } |
| 5485 | p_new_sd->ascent = MAX(p_new_sd->ascent, MAX(cached_font_ascent + cached_font_top_spacing, -gl.y_off)); |
| 5486 | p_new_sd->descent = MAX(p_new_sd->descent, MAX(cached_font_descent + cached_font_bottom_spacing, gl.y_off)); |
| 5487 | } else { |
| 5488 | double glyph_advance = Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5); |
| 5489 | p_new_sd->ascent = MAX(p_new_sd->ascent, glyph_advance); |
| 5490 | p_new_sd->descent = MAX(p_new_sd->descent, glyph_advance); |
| 5491 | } |
| 5492 | } else if (p_new_sd->preserve_invalid || (p_new_sd->preserve_control && is_control(ch[gl.start - p_sd->start]))) { |
| 5493 | // Glyph not found, replace with hex code box. |
| 5494 | if (p_new_sd->orientation == ORIENTATION_HORIZONTAL) { |
| 5495 | double box_size = get_hex_code_box_size(gl.font_size, gl.index).y; |
| 5496 | p_new_sd->ascent = MAX(p_new_sd->ascent, box_size * 0.85); |
| 5497 | p_new_sd->descent = MAX(p_new_sd->descent, box_size * 0.15); |
| 5498 | } else { |
| 5499 | double box_size = Math::round(get_hex_code_box_size(gl.font_size, gl.index).x * 0.5); |
| 5500 | p_new_sd->ascent = MAX(p_new_sd->ascent, box_size); |
| 5501 | p_new_sd->descent = MAX(p_new_sd->descent, box_size); |
| 5502 | } |
| 5503 | } |
| 5504 | p_new_sd->width += gl.advance * gl.repeat; |
| 5505 | } |
| 5506 | if (p_new_sd->glyphs.is_empty() && gl.x_off < 0.0) { |
| 5507 | gl.advance += -gl.x_off; |
| 5508 | gl.x_off = 0.0; |
| 5509 | } |
| 5510 | p_new_sd->glyphs.push_back(gl); |
| 5511 | } |
| 5512 | } |
| 5513 | } |
| 5514 | } |
| 5515 | |
| 5516 | _realign(p_new_sd); |
| 5517 | } |
| 5518 | p_new_sd->valid.set(); |
| 5519 | |
| 5520 | return true; |
| 5521 | } |
| 5522 | |
| 5523 | RID TextServerAdvanced::_shaped_text_get_parent(const RID &p_shaped) const { |
| 5524 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 5525 | ERR_FAIL_NULL_V(sd, RID()); |
| 5526 | |
| 5527 | MutexLock lock(sd->mutex); |
| 5528 | return sd->parent; |
| 5529 | } |
| 5530 | |
| 5531 | double TextServerAdvanced::_shaped_text_fit_to_width(const RID &p_shaped, double p_width, BitField<TextServer::JustificationFlag> p_jst_flags) { |
| 5532 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 5533 | ERR_FAIL_NULL_V(sd, 0.0); |
| 5534 | |
| 5535 | MutexLock lock(sd->mutex); |
| 5536 | if (!sd->valid.is_set()) { |
| 5537 | _shaped_text_shape(p_shaped); |
| 5538 | } |
| 5539 | if (!sd->justification_ops_valid) { |
| 5540 | _shaped_text_update_justification_ops(p_shaped); |
| 5541 | } |
| 5542 | |
| 5543 | sd->fit_width_minimum_reached = false; |
| 5544 | int start_pos = 0; |
| 5545 | int end_pos = sd->glyphs.size() - 1; |
| 5546 | |
| 5547 | if (p_jst_flags.has_flag(JUSTIFICATION_AFTER_LAST_TAB)) { |
| 5548 | int start, end, delta; |
| 5549 | if (sd->para_direction == DIRECTION_LTR) { |
| 5550 | start = sd->glyphs.size() - 1; |
| 5551 | end = -1; |
| 5552 | delta = -1; |
| 5553 | } else { |
| 5554 | start = 0; |
| 5555 | end = sd->glyphs.size(); |
| 5556 | delta = +1; |
| 5557 | } |
| 5558 | |
| 5559 | for (int i = start; i != end; i += delta) { |
| 5560 | if ((sd->glyphs[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB) { |
| 5561 | if (sd->para_direction == DIRECTION_LTR) { |
| 5562 | start_pos = i; |
| 5563 | break; |
| 5564 | } else { |
| 5565 | end_pos = i; |
| 5566 | break; |
| 5567 | } |
| 5568 | } |
| 5569 | } |
| 5570 | } |
| 5571 | |
| 5572 | double justification_width; |
| 5573 | if (p_jst_flags.has_flag(JUSTIFICATION_CONSTRAIN_ELLIPSIS)) { |
| 5574 | if (sd->overrun_trim_data.trim_pos >= 0) { |
| 5575 | if (sd->para_direction == DIRECTION_RTL) { |
| 5576 | start_pos = sd->overrun_trim_data.trim_pos; |
| 5577 | } else { |
| 5578 | end_pos = sd->overrun_trim_data.trim_pos; |
| 5579 | } |
| 5580 | justification_width = sd->width_trimmed; |
| 5581 | } else { |
| 5582 | return Math::ceil(sd->width); |
| 5583 | } |
| 5584 | } else { |
| 5585 | justification_width = sd->width; |
| 5586 | } |
| 5587 | |
| 5588 | if (p_jst_flags.has_flag(JUSTIFICATION_TRIM_EDGE_SPACES)) { |
| 5589 | // Trim spaces. |
| 5590 | while ((start_pos < end_pos) && ((sd->glyphs[start_pos].flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN) && ((sd->glyphs[start_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) { |
| 5591 | justification_width -= sd->glyphs[start_pos].advance * sd->glyphs[start_pos].repeat; |
| 5592 | sd->glyphs[start_pos].advance = 0; |
| 5593 | start_pos += sd->glyphs[start_pos].count; |
| 5594 | } |
| 5595 | while ((start_pos < end_pos) && ((sd->glyphs[end_pos].flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN) && ((sd->glyphs[end_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) { |
| 5596 | justification_width -= sd->glyphs[end_pos].advance * sd->glyphs[end_pos].repeat; |
| 5597 | sd->glyphs[end_pos].advance = 0; |
| 5598 | end_pos -= sd->glyphs[end_pos].count; |
| 5599 | } |
| 5600 | } else { |
| 5601 | // Skip breaks, but do not reset size. |
| 5602 | while ((start_pos < end_pos) && ((sd->glyphs[start_pos].flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN) && ((sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) { |
| 5603 | start_pos += sd->glyphs[start_pos].count; |
| 5604 | } |
| 5605 | while ((start_pos < end_pos) && ((sd->glyphs[end_pos].flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN) && ((sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) { |
| 5606 | end_pos -= sd->glyphs[end_pos].count; |
| 5607 | } |
| 5608 | } |
| 5609 | |
| 5610 | int space_count = 0; |
| 5611 | int elongation_count = 0; |
| 5612 | for (int i = start_pos; i <= end_pos; i++) { |
| 5613 | const Glyph &gl = sd->glyphs[i]; |
| 5614 | if (gl.count > 0) { |
| 5615 | if ((gl.flags & GRAPHEME_IS_ELONGATION) == GRAPHEME_IS_ELONGATION) { |
| 5616 | if ((i > 0) && ((sd->glyphs[i - 1].flags & GRAPHEME_IS_ELONGATION) != GRAPHEME_IS_ELONGATION)) { |
| 5617 | // Expand once per elongation sequence. |
| 5618 | elongation_count++; |
| 5619 | } |
| 5620 | } |
| 5621 | if ((gl.flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN && (gl.flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE && (gl.flags & GRAPHEME_IS_PUNCTUATION) != GRAPHEME_IS_PUNCTUATION) { |
| 5622 | space_count++; |
| 5623 | } |
| 5624 | } |
| 5625 | } |
| 5626 | |
| 5627 | if ((elongation_count > 0) && p_jst_flags.has_flag(JUSTIFICATION_KASHIDA)) { |
| 5628 | double delta_width_per_kashida = (p_width - justification_width) / elongation_count; |
| 5629 | for (int i = start_pos; i <= end_pos; i++) { |
| 5630 | Glyph &gl = sd->glyphs[i]; |
| 5631 | if (gl.count > 0) { |
| 5632 | if (((gl.flags & GRAPHEME_IS_ELONGATION) == GRAPHEME_IS_ELONGATION) && (gl.advance > 0)) { |
| 5633 | if ((i > 0) && ((sd->glyphs[i - 1].flags & GRAPHEME_IS_ELONGATION) != GRAPHEME_IS_ELONGATION)) { |
| 5634 | // Expand once per elongation sequence. |
| 5635 | int count = delta_width_per_kashida / gl.advance; |
| 5636 | int prev_count = gl.repeat; |
| 5637 | if ((gl.flags & GRAPHEME_IS_VIRTUAL) == GRAPHEME_IS_VIRTUAL) { |
| 5638 | gl.repeat = CLAMP(count, 0, 255); |
| 5639 | } else { |
| 5640 | gl.repeat = CLAMP(count + 1, 1, 255); |
| 5641 | } |
| 5642 | justification_width += (gl.repeat - prev_count) * gl.advance; |
| 5643 | } |
| 5644 | } |
| 5645 | } |
| 5646 | } |
| 5647 | } |
| 5648 | if ((space_count > 0) && p_jst_flags.has_flag(JUSTIFICATION_WORD_BOUND)) { |
| 5649 | double delta_width_per_space = (p_width - justification_width) / space_count; |
| 5650 | double adv_remain = 0; |
| 5651 | for (int i = start_pos; i <= end_pos; i++) { |
| 5652 | Glyph &gl = sd->glyphs[i]; |
| 5653 | if (gl.count > 0) { |
| 5654 | if ((gl.flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN && (gl.flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE && (gl.flags & GRAPHEME_IS_PUNCTUATION) != GRAPHEME_IS_PUNCTUATION) { |
| 5655 | double old_adv = gl.advance; |
| 5656 | double new_advance; |
| 5657 | if ((gl.flags & GRAPHEME_IS_VIRTUAL) == GRAPHEME_IS_VIRTUAL) { |
| 5658 | new_advance = MAX(gl.advance + delta_width_per_space, 0.0); |
| 5659 | } else { |
| 5660 | new_advance = MAX(gl.advance + delta_width_per_space, 0.1 * gl.font_size); |
| 5661 | } |
| 5662 | gl.advance = new_advance; |
| 5663 | adv_remain += (new_advance - gl.advance); |
| 5664 | if (adv_remain >= 1.0) { |
| 5665 | gl.advance++; |
| 5666 | adv_remain -= 1.0; |
| 5667 | } else if (adv_remain <= -1.0) { |
| 5668 | gl.advance = MAX(gl.advance - 1, 0); |
| 5669 | adv_remain -= 1.0; |
| 5670 | } |
| 5671 | justification_width += (gl.advance - old_adv); |
| 5672 | } |
| 5673 | } |
| 5674 | } |
| 5675 | } |
| 5676 | |
| 5677 | if (Math::floor(p_width) < Math::floor(justification_width)) { |
| 5678 | sd->fit_width_minimum_reached = true; |
| 5679 | } |
| 5680 | |
| 5681 | if (!p_jst_flags.has_flag(JUSTIFICATION_CONSTRAIN_ELLIPSIS)) { |
| 5682 | sd->width = justification_width; |
| 5683 | } |
| 5684 | |
| 5685 | return Math::ceil(justification_width); |
| 5686 | } |
| 5687 | |
| 5688 | double TextServerAdvanced::_shaped_text_tab_align(const RID &p_shaped, const PackedFloat32Array &p_tab_stops) { |
| 5689 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 5690 | ERR_FAIL_NULL_V(sd, 0.0); |
| 5691 | |
| 5692 | MutexLock lock(sd->mutex); |
| 5693 | if (!sd->valid.is_set()) { |
| 5694 | _shaped_text_shape(p_shaped); |
| 5695 | } |
| 5696 | if (!sd->line_breaks_valid) { |
| 5697 | _shaped_text_update_breaks(p_shaped); |
| 5698 | } |
| 5699 | |
| 5700 | for (int i = 0; i < p_tab_stops.size(); i++) { |
| 5701 | if (p_tab_stops[i] <= 0) { |
| 5702 | return 0.0; |
| 5703 | } |
| 5704 | } |
| 5705 | |
| 5706 | int tab_index = 0; |
| 5707 | double off = 0.0; |
| 5708 | |
| 5709 | int start, end, delta; |
| 5710 | if (sd->para_direction == DIRECTION_LTR) { |
| 5711 | start = 0; |
| 5712 | end = sd->glyphs.size(); |
| 5713 | delta = +1; |
| 5714 | } else { |
| 5715 | start = sd->glyphs.size() - 1; |
| 5716 | end = -1; |
| 5717 | delta = -1; |
| 5718 | } |
| 5719 | |
| 5720 | Glyph *gl = sd->glyphs.ptr(); |
| 5721 | |
| 5722 | for (int i = start; i != end; i += delta) { |
| 5723 | if ((gl[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB) { |
| 5724 | double tab_off = 0.0; |
| 5725 | while (tab_off <= off) { |
| 5726 | tab_off += p_tab_stops[tab_index]; |
| 5727 | tab_index++; |
| 5728 | if (tab_index >= p_tab_stops.size()) { |
| 5729 | tab_index = 0; |
| 5730 | } |
| 5731 | } |
| 5732 | double old_adv = gl[i].advance; |
| 5733 | gl[i].advance = tab_off - off; |
| 5734 | sd->width += gl[i].advance - old_adv; |
| 5735 | off = 0; |
| 5736 | continue; |
| 5737 | } |
| 5738 | off += gl[i].advance * gl[i].repeat; |
| 5739 | } |
| 5740 | |
| 5741 | return 0.0; |
| 5742 | } |
| 5743 | |
| 5744 | RID TextServerAdvanced::_find_sys_font_for_text(const RID &p_fdef, const String &p_script_code, const String &p_language, const String &p_text) { |
| 5745 | RID f; |
| 5746 | // Try system fallback. |
| 5747 | String font_name = _font_get_name(p_fdef); |
| 5748 | BitField<FontStyle> font_style = _font_get_style(p_fdef); |
| 5749 | int font_weight = _font_get_weight(p_fdef); |
| 5750 | int font_stretch = _font_get_stretch(p_fdef); |
| 5751 | Dictionary dvar = _font_get_variation_coordinates(p_fdef); |
| 5752 | static int64_t wgth_tag = _name_to_tag("weight"); |
| 5753 | static int64_t wdth_tag = _name_to_tag("width"); |
| 5754 | static int64_t ital_tag = _name_to_tag("italic"); |
| 5755 | if (dvar.has(wgth_tag)) { |
| 5756 | font_weight = dvar[wgth_tag].operator int(); |
| 5757 | } |
| 5758 | if (dvar.has(wdth_tag)) { |
| 5759 | font_stretch = dvar[wdth_tag].operator int(); |
| 5760 | } |
| 5761 | if (dvar.has(ital_tag) && dvar[ital_tag].operator int() == 1) { |
| 5762 | font_style.set_flag(TextServer::FONT_ITALIC); |
| 5763 | } |
| 5764 | if (p_script_code == "Zsye") { |
| 5765 | #if defined(MACOS_ENABLED) || defined(APPLE_EMBEDDED_ENABLED) |
| 5766 | font_name = "Apple Color Emoji"; |
| 5767 | #elif defined(WINDOWS_ENABLED) |
| 5768 | font_name = "Segoe UI Emoji"; |
| 5769 | #else |
| 5770 | font_name = "Noto Color Emoji"; |
| 5771 | #endif |
| 5772 | } |
| 5773 | |
| 5774 | String locale = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language; |
| 5775 | PackedStringArray fallback_font_name = OS::get_singleton()->get_system_font_path_for_text(font_name, p_text, locale, p_script_code, font_weight, font_stretch, font_style & TextServer::FONT_ITALIC); |
| 5776 | #ifdef GDEXTENSION |
| 5777 | for (int fb = 0; fb < fallback_font_name.size(); fb++) { |
| 5778 | const String &E = fallback_font_name[fb]; |
| 5779 | #elif defined(GODOT_MODULE) |
| 5780 | for (const String &E : fallback_font_name) { |
| 5781 | #endif |
| 5782 | SystemFontKey key = SystemFontKey(E, font_style & TextServer::FONT_ITALIC, font_weight, font_stretch, p_fdef, this); |
| 5783 | if (system_fonts.has(key)) { |
| 5784 | const SystemFontCache &sysf_cache = system_fonts[key]; |
| 5785 | int best_score = 0; |
| 5786 | int best_match = -1; |
| 5787 | for (int face_idx = 0; face_idx < sysf_cache.var.size(); face_idx++) { |
| 5788 | const SystemFontCacheRec &F = sysf_cache.var[face_idx]; |
| 5789 | if (unlikely(!_font_has_char(F.rid, p_text[0]))) { |
| 5790 | continue; |
| 5791 | } |
| 5792 | BitField<FontStyle> style = _font_get_style(F.rid); |
| 5793 | int weight = _font_get_weight(F.rid); |
| 5794 | int stretch = _font_get_stretch(F.rid); |
| 5795 | int score = (20 - Math::abs(weight - font_weight) / 50); |
| 5796 | score += (20 - Math::abs(stretch - font_stretch) / 10); |
| 5797 | if (bool(style & TextServer::FONT_ITALIC) == bool(font_style & TextServer::FONT_ITALIC)) { |
| 5798 | score += 30; |
| 5799 | } |
| 5800 | if (score >= best_score) { |
| 5801 | best_score = score; |
| 5802 | best_match = face_idx; |
| 5803 | } |
| 5804 | if (best_score == 70) { |
| 5805 | break; |
| 5806 | } |
| 5807 | } |
| 5808 | if (best_match != -1) { |
| 5809 | f = sysf_cache.var[best_match].rid; |
| 5810 | } |
| 5811 | } |
| 5812 | if (!f.is_valid()) { |
| 5813 | if (system_fonts.has(key)) { |
| 5814 | const SystemFontCache &sysf_cache = system_fonts[key]; |
| 5815 | if (sysf_cache.max_var == sysf_cache.var.size()) { |
| 5816 | // All subfonts already tested, skip. |
| 5817 | continue; |
| 5818 | } |
| 5819 | } |
| 5820 | |
| 5821 | if (!system_font_data.has(E)) { |
| 5822 | system_font_data[E] = FileAccess::get_file_as_bytes(E); |
| 5823 | } |
| 5824 | |
| 5825 | const PackedByteArray &font_data = system_font_data[E]; |
| 5826 | |
| 5827 | SystemFontCacheRec sysf; |
| 5828 | sysf.rid = _create_font(); |
| 5829 | _font_set_data_ptr(sysf.rid, font_data.ptr(), font_data.size()); |
| 5830 | if (!_font_validate(sysf.rid)) { |
| 5831 | _free_rid(sysf.rid); |
| 5832 | continue; |
| 5833 | } |
| 5834 | |
| 5835 | Dictionary var = dvar; |
| 5836 | // Select matching style from collection. |
| 5837 | int best_score = 0; |
| 5838 | int best_match = -1; |
| 5839 | for (int face_idx = 0; face_idx < _font_get_face_count(sysf.rid); face_idx++) { |
| 5840 | _font_set_face_index(sysf.rid, face_idx); |
| 5841 | if (unlikely(!_font_has_char(sysf.rid, p_text[0]))) { |
| 5842 | continue; |
| 5843 | } |
| 5844 | BitField<FontStyle> style = _font_get_style(sysf.rid); |
| 5845 | int weight = _font_get_weight(sysf.rid); |
| 5846 | int stretch = _font_get_stretch(sysf.rid); |
| 5847 | int score = (20 - Math::abs(weight - font_weight) / 50); |
| 5848 | score += (20 - Math::abs(stretch - font_stretch) / 10); |
| 5849 | if (bool(style & TextServer::FONT_ITALIC) == bool(font_style & TextServer::FONT_ITALIC)) { |
| 5850 | score += 30; |
| 5851 | } |
| 5852 | if (score >= best_score) { |
| 5853 | best_score = score; |
| 5854 | best_match = face_idx; |
| 5855 | } |
| 5856 | if (best_score == 70) { |
| 5857 | break; |
| 5858 | } |
| 5859 | } |
| 5860 | if (best_match == -1) { |
| 5861 | _free_rid(sysf.rid); |
| 5862 | continue; |
| 5863 | } else { |
| 5864 | _font_set_face_index(sysf.rid, best_match); |
| 5865 | } |
| 5866 | sysf.index = best_match; |
| 5867 | |
| 5868 | // If it's a variable font, apply weight, stretch and italic coordinates to match requested style. |
| 5869 | if (best_score != 70) { |
| 5870 | Dictionary ftr = _font_supported_variation_list(sysf.rid); |
| 5871 | if (ftr.has(wdth_tag)) { |
| 5872 | var[wdth_tag] = font_stretch; |
| 5873 | _font_set_stretch(sysf.rid, font_stretch); |
| 5874 | } |
| 5875 | if (ftr.has(wgth_tag)) { |
| 5876 | var[wgth_tag] = font_weight; |
| 5877 | _font_set_weight(sysf.rid, font_weight); |
| 5878 | } |
| 5879 | if ((font_style & TextServer::FONT_ITALIC) && ftr.has(ital_tag)) { |
| 5880 | var[ital_tag] = 1; |
| 5881 | _font_set_style(sysf.rid, _font_get_style(sysf.rid) | TextServer::FONT_ITALIC); |
| 5882 | } |
| 5883 | } |
| 5884 | |
| 5885 | bool fb_use_msdf = key.msdf; |
| 5886 | #ifdef MODULE_FREETYPE_ENABLED |
| 5887 | if (fb_use_msdf) { |
| 5888 | FontAdvanced *fd = _get_font_data(sysf.rid); |
| 5889 | if (fd) { |
| 5890 | MutexLock lock(fd->mutex); |
| 5891 | Vector2i size = _get_size(fd, 16); |
| 5892 | FontForSizeAdvanced *ffsd = nullptr; |
| 5893 | if (_ensure_cache_for_size(fd, size, ffsd)) { |
| 5894 | if (ffsd && (FT_HAS_COLOR(ffsd->face) || !FT_IS_SCALABLE(ffsd->face))) { |
| 5895 | fb_use_msdf = false; |
| 5896 | } |
| 5897 | } |
| 5898 | } |
| 5899 | } |
| 5900 | #endif |
| 5901 | |
| 5902 | _font_set_antialiasing(sysf.rid, key.antialiasing); |
| 5903 | _font_set_disable_embedded_bitmaps(sysf.rid, key.disable_embedded_bitmaps); |
| 5904 | _font_set_generate_mipmaps(sysf.rid, key.mipmaps); |
| 5905 | _font_set_multichannel_signed_distance_field(sysf.rid, fb_use_msdf); |
| 5906 | _font_set_msdf_pixel_range(sysf.rid, key.msdf_range); |
| 5907 | _font_set_msdf_size(sysf.rid, key.msdf_source_size); |
| 5908 | _font_set_fixed_size(sysf.rid, key.fixed_size); |
| 5909 | _font_set_force_autohinter(sysf.rid, key.force_autohinter); |
| 5910 | _font_set_hinting(sysf.rid, key.hinting); |
| 5911 | _font_set_subpixel_positioning(sysf.rid, key.subpixel_positioning); |
| 5912 | _font_set_keep_rounding_remainders(sysf.rid, key.keep_rounding_remainders); |
| 5913 | _font_set_variation_coordinates(sysf.rid, var); |
| 5914 | _font_set_embolden(sysf.rid, key.embolden); |
| 5915 | _font_set_transform(sysf.rid, key.transform); |
| 5916 | _font_set_spacing(sysf.rid, SPACING_TOP, key.extra_spacing[SPACING_TOP]); |
| 5917 | _font_set_spacing(sysf.rid, SPACING_BOTTOM, key.extra_spacing[SPACING_BOTTOM]); |
| 5918 | _font_set_spacing(sysf.rid, SPACING_SPACE, key.extra_spacing[SPACING_SPACE]); |
| 5919 | _font_set_spacing(sysf.rid, SPACING_GLYPH, key.extra_spacing[SPACING_GLYPH]); |
| 5920 | |
| 5921 | if (system_fonts.has(key)) { |
| 5922 | system_fonts[key].var.push_back(sysf); |
| 5923 | } else { |
| 5924 | SystemFontCache &sysf_cache = system_fonts[key]; |
| 5925 | sysf_cache.max_var = _font_get_face_count(sysf.rid); |
| 5926 | sysf_cache.var.push_back(sysf); |
| 5927 | } |
| 5928 | f = sysf.rid; |
| 5929 | } |
| 5930 | break; |
| 5931 | } |
| 5932 | return f; |
| 5933 | } |
| 5934 | |
| 5935 | void TextServerAdvanced::_shaped_text_overrun_trim_to_width(const RID &p_shaped_line, double p_width, BitField<TextServer::TextOverrunFlag> p_trim_flags) { |
| 5936 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped_line); |
| 5937 | ERR_FAIL_NULL_MSG(sd, "ShapedTextDataAdvanced invalid."); |
| 5938 | |
| 5939 | MutexLock lock(sd->mutex); |
| 5940 | if (!sd->valid.is_set()) { |
| 5941 | _shaped_text_shape(p_shaped_line); |
| 5942 | } |
| 5943 | if (!sd->line_breaks_valid) { |
| 5944 | _shaped_text_update_breaks(p_shaped_line); |
| 5945 | } |
| 5946 | |
| 5947 | sd->text_trimmed = false; |
| 5948 | sd->overrun_trim_data.ellipsis_glyph_buf.clear(); |
| 5949 | |
| 5950 | bool add_ellipsis = p_trim_flags.has_flag(OVERRUN_ADD_ELLIPSIS); |
| 5951 | bool cut_per_word = p_trim_flags.has_flag(OVERRUN_TRIM_WORD_ONLY); |
| 5952 | bool enforce_ellipsis = p_trim_flags.has_flag(OVERRUN_ENFORCE_ELLIPSIS); |
| 5953 | bool short_string_ellipsis = p_trim_flags.has_flag(OVERRUN_SHORT_STRING_ELLIPSIS); |
| 5954 | bool justification_aware = p_trim_flags.has_flag(OVERRUN_JUSTIFICATION_AWARE); |
| 5955 | |
| 5956 | Glyph *sd_glyphs = sd->glyphs.ptr(); |
| 5957 | |
| 5958 | if ((p_trim_flags & OVERRUN_TRIM) == OVERRUN_NO_TRIM || sd_glyphs == nullptr || p_width <= 0 || !(sd->width > p_width || enforce_ellipsis)) { |
| 5959 | sd->overrun_trim_data.trim_pos = -1; |
| 5960 | sd->overrun_trim_data.ellipsis_pos = -1; |
| 5961 | return; |
| 5962 | } |
| 5963 | |
| 5964 | if (justification_aware && !sd->fit_width_minimum_reached) { |
| 5965 | return; |
| 5966 | } |
| 5967 | |
| 5968 | Vector<ShapedTextDataAdvanced::Span> &spans = sd->spans; |
| 5969 | if (sd->parent != RID()) { |
| 5970 | ShapedTextDataAdvanced *parent_sd = shaped_owner.get_or_null(sd->parent); |
| 5971 | ERR_FAIL_COND(!parent_sd->valid.is_set()); |
| 5972 | spans = parent_sd->spans; |
| 5973 | } |
| 5974 | |
| 5975 | int span_size = spans.size(); |
| 5976 | if (span_size == 0) { |
| 5977 | return; |
| 5978 | } |
| 5979 | |
| 5980 | int sd_size = sd->glyphs.size(); |
| 5981 | int last_gl_font_size = sd_glyphs[sd_size - 1].font_size; |
| 5982 | bool found_el_char = false; |
| 5983 | |
| 5984 | // Find usable fonts, if fonts from the last glyph do not have required chars. |
| 5985 | RID dot_gl_font_rid = sd_glyphs[sd_size - 1].font_rid; |
| 5986 | if (add_ellipsis || enforce_ellipsis || short_string_ellipsis) { |
| 5987 | if (!_font_has_char(dot_gl_font_rid, sd->el_char)) { |
| 5988 | const Array &fonts = spans[span_size - 1].fonts; |
| 5989 | for (int i = 0; i < fonts.size(); i++) { |
| 5990 | if (_font_has_char(fonts[i], sd->el_char)) { |
| 5991 | dot_gl_font_rid = fonts[i]; |
| 5992 | found_el_char = true; |
| 5993 | break; |
| 5994 | } |
| 5995 | } |
| 5996 | if (!found_el_char && OS::get_singleton()->has_feature("system_fonts") && fonts.size() > 0 && _font_is_allow_system_fallback(fonts[0])) { |
| 5997 | const char32_t u32str[] = { sd->el_char, 0 }; |
| 5998 | RID rid = _find_sys_font_for_text(fonts[0], String(), spans[span_size - 1].language, u32str); |
| 5999 | if (rid.is_valid()) { |
| 6000 | dot_gl_font_rid = rid; |
| 6001 | found_el_char = true; |
| 6002 | } |
| 6003 | } |
| 6004 | } else { |
| 6005 | found_el_char = true; |
| 6006 | } |
| 6007 | if (!found_el_char) { |
| 6008 | bool found_dot_char = false; |
| 6009 | dot_gl_font_rid = sd_glyphs[sd_size - 1].font_rid; |
| 6010 | if (!_font_has_char(dot_gl_font_rid, '.')) { |
| 6011 | const Array &fonts = spans[span_size - 1].fonts; |
| 6012 | for (int i = 0; i < fonts.size(); i++) { |
| 6013 | if (_font_has_char(fonts[i], '.')) { |
| 6014 | dot_gl_font_rid = fonts[i]; |
| 6015 | found_dot_char = true; |
| 6016 | break; |
| 6017 | } |
| 6018 | } |
| 6019 | if (!found_dot_char && OS::get_singleton()->has_feature("system_fonts") && fonts.size() > 0 && _font_is_allow_system_fallback(fonts[0])) { |
| 6020 | RID rid = _find_sys_font_for_text(fonts[0], String(), spans[span_size - 1].language, "."); |
| 6021 | if (rid.is_valid()) { |
| 6022 | dot_gl_font_rid = rid; |
| 6023 | } |
| 6024 | } |
| 6025 | } |
| 6026 | } |
| 6027 | } |
| 6028 | RID whitespace_gl_font_rid = sd_glyphs[sd_size - 1].font_rid; |
| 6029 | if (!_font_has_char(whitespace_gl_font_rid, ' ')) { |
| 6030 | const Array &fonts = spans[span_size - 1].fonts; |
| 6031 | for (int i = 0; i < fonts.size(); i++) { |
| 6032 | if (_font_has_char(fonts[i], ' ')) { |
| 6033 | whitespace_gl_font_rid = fonts[i]; |
| 6034 | break; |
| 6035 | } |
| 6036 | } |
| 6037 | } |
| 6038 | |
| 6039 | int32_t dot_gl_idx = ((add_ellipsis || enforce_ellipsis || short_string_ellipsis) && dot_gl_font_rid.is_valid()) ? _font_get_glyph_index(dot_gl_font_rid, last_gl_font_size, (found_el_char ? sd->el_char : '.'), 0) : -1; |
| 6040 | Vector2 dot_adv = ((add_ellipsis || enforce_ellipsis || short_string_ellipsis) && dot_gl_font_rid.is_valid()) ? _font_get_glyph_advance(dot_gl_font_rid, last_gl_font_size, dot_gl_idx) : Vector2(); |
| 6041 | int32_t whitespace_gl_idx = whitespace_gl_font_rid.is_valid() ? _font_get_glyph_index(whitespace_gl_font_rid, last_gl_font_size, ' ', 0) : -1; |
| 6042 | Vector2 whitespace_adv = whitespace_gl_font_rid.is_valid() ? _font_get_glyph_advance(whitespace_gl_font_rid, last_gl_font_size, whitespace_gl_idx) : Vector2(); |
| 6043 | |
| 6044 | int ellipsis_width = 0; |
| 6045 | if (add_ellipsis && whitespace_gl_font_rid.is_valid()) { |
| 6046 | ellipsis_width = (found_el_char ? 1 : 3) * dot_adv.x + sd->extra_spacing[SPACING_GLYPH] + _font_get_spacing(dot_gl_font_rid, SPACING_GLYPH) + (cut_per_word ? whitespace_adv.x : 0); |
| 6047 | } |
| 6048 | |
| 6049 | int ell_min_characters = 6; |
| 6050 | double width = sd->width; |
| 6051 | double width_without_el = width; |
| 6052 | |
| 6053 | bool is_rtl = sd->para_direction == DIRECTION_RTL; |
| 6054 | |
| 6055 | int trim_pos = (is_rtl) ? sd_size : 0; |
| 6056 | int ellipsis_pos = (enforce_ellipsis || short_string_ellipsis) ? 0 : -1; |
| 6057 | |
| 6058 | int last_valid_cut = -1; |
| 6059 | int last_valid_cut_witout_el = -1; |
| 6060 | |
| 6061 | int glyphs_from = (is_rtl) ? 0 : sd_size - 1; |
| 6062 | int glyphs_to = (is_rtl) ? sd_size - 1 : -1; |
| 6063 | int glyphs_delta = (is_rtl) ? +1 : -1; |
| 6064 | |
| 6065 | if ((enforce_ellipsis || short_string_ellipsis) && (width + ellipsis_width <= p_width)) { |
| 6066 | trim_pos = -1; |
| 6067 | ellipsis_pos = (is_rtl) ? 0 : sd_size; |
| 6068 | } else { |
| 6069 | for (int i = glyphs_from; i != glyphs_to; i += glyphs_delta) { |
| 6070 | if (!is_rtl) { |
| 6071 | width -= sd_glyphs[i].advance * sd_glyphs[i].repeat; |
| 6072 | } |
| 6073 | if (sd_glyphs[i].count > 0) { |
| 6074 | bool above_min_char_threshold = ((is_rtl) ? sd_size - 1 - i : i) >= ell_min_characters; |
| 6075 | if (!above_min_char_threshold && last_valid_cut_witout_el != -1) { |
| 6076 | trim_pos = last_valid_cut_witout_el; |
| 6077 | ellipsis_pos = -1; |
| 6078 | width = width_without_el; |
| 6079 | break; |
| 6080 | } |
| 6081 | if (!(enforce_ellipsis || short_string_ellipsis) && width <= p_width && last_valid_cut_witout_el == -1) { |
| 6082 | if (cut_per_word && above_min_char_threshold) { |
| 6083 | if ((sd_glyphs[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) { |
| 6084 | last_valid_cut_witout_el = i; |
| 6085 | width_without_el = width; |
| 6086 | } |
| 6087 | } else { |
| 6088 | last_valid_cut_witout_el = i; |
| 6089 | width_without_el = width; |
| 6090 | } |
| 6091 | } |
| 6092 | if (width + (((above_min_char_threshold && add_ellipsis) || enforce_ellipsis || short_string_ellipsis) ? ellipsis_width : 0) <= p_width) { |
| 6093 | if (cut_per_word && above_min_char_threshold) { |
| 6094 | if ((sd_glyphs[i].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT) { |
| 6095 | last_valid_cut = i; |
| 6096 | } |
| 6097 | } else { |
| 6098 | last_valid_cut = i; |
| 6099 | } |
| 6100 | if (last_valid_cut != -1) { |
| 6101 | trim_pos = last_valid_cut; |
| 6102 | |
| 6103 | if (add_ellipsis && (above_min_char_threshold || enforce_ellipsis || short_string_ellipsis) && width - ellipsis_width <= p_width) { |
| 6104 | ellipsis_pos = trim_pos; |
| 6105 | } |
| 6106 | break; |
| 6107 | } |
| 6108 | } |
| 6109 | } |
| 6110 | if (is_rtl) { |
| 6111 | width -= sd_glyphs[i].advance * sd_glyphs[i].repeat; |
| 6112 | } |
| 6113 | } |
| 6114 | } |
| 6115 | |
| 6116 | sd->overrun_trim_data.trim_pos = trim_pos; |
| 6117 | sd->overrun_trim_data.ellipsis_pos = ellipsis_pos; |
| 6118 | if (trim_pos == 0 && (enforce_ellipsis || short_string_ellipsis) && add_ellipsis) { |
| 6119 | sd->overrun_trim_data.ellipsis_pos = 0; |
| 6120 | } |
| 6121 | |
| 6122 | if ((trim_pos >= 0 && sd->width > p_width) || enforce_ellipsis || short_string_ellipsis) { |
| 6123 | if (add_ellipsis && (ellipsis_pos > 0 || enforce_ellipsis || short_string_ellipsis)) { |
| 6124 | // Insert an additional space when cutting word bound for aesthetics. |
| 6125 | if (cut_per_word && (ellipsis_pos > 0)) { |
| 6126 | Glyph gl; |
| 6127 | gl.count = 1; |
| 6128 | gl.advance = whitespace_adv.x; |
| 6129 | gl.index = whitespace_gl_idx; |
| 6130 | gl.font_rid = whitespace_gl_font_rid; |
| 6131 | gl.font_size = last_gl_font_size; |
| 6132 | gl.flags = GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT | GRAPHEME_IS_VIRTUAL | (is_rtl ? GRAPHEME_IS_RTL : 0); |
| 6133 | |
| 6134 | sd->overrun_trim_data.ellipsis_glyph_buf.append(gl); |
| 6135 | } |
| 6136 | // Add ellipsis dots. |
| 6137 | if (dot_gl_idx != 0) { |
| 6138 | Glyph gl; |
| 6139 | gl.count = 1; |
| 6140 | gl.repeat = (found_el_char ? 1 : 3); |
| 6141 | gl.advance = dot_adv.x; |
| 6142 | gl.index = dot_gl_idx; |
| 6143 | gl.font_rid = dot_gl_font_rid; |
| 6144 | gl.font_size = last_gl_font_size; |
| 6145 | gl.flags = GRAPHEME_IS_PUNCTUATION | GRAPHEME_IS_VIRTUAL | (is_rtl ? GRAPHEME_IS_RTL : 0); |
| 6146 | |
| 6147 | sd->overrun_trim_data.ellipsis_glyph_buf.append(gl); |
| 6148 | } |
| 6149 | } |
| 6150 | |
| 6151 | sd->text_trimmed = true; |
| 6152 | sd->width_trimmed = width + ((ellipsis_pos != -1) ? ellipsis_width : 0); |
| 6153 | } |
| 6154 | } |
| 6155 | |
| 6156 | int64_t TextServerAdvanced::_shaped_text_get_trim_pos(const RID &p_shaped) const { |
| 6157 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 6158 | ERR_FAIL_NULL_V_MSG(sd, -1, "ShapedTextDataAdvanced invalid."); |
| 6159 | |
| 6160 | MutexLock lock(sd->mutex); |
| 6161 | return sd->overrun_trim_data.trim_pos; |
| 6162 | } |
| 6163 | |
| 6164 | int64_t TextServerAdvanced::_shaped_text_get_ellipsis_pos(const RID &p_shaped) const { |
| 6165 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 6166 | ERR_FAIL_NULL_V_MSG(sd, -1, "ShapedTextDataAdvanced invalid."); |
| 6167 | |
| 6168 | MutexLock lock(sd->mutex); |
| 6169 | return sd->overrun_trim_data.ellipsis_pos; |
| 6170 | } |
| 6171 | |
| 6172 | const Glyph *TextServerAdvanced::_shaped_text_get_ellipsis_glyphs(const RID &p_shaped) const { |
| 6173 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 6174 | ERR_FAIL_NULL_V_MSG(sd, nullptr, "ShapedTextDataAdvanced invalid."); |
| 6175 | |
| 6176 | MutexLock lock(sd->mutex); |
| 6177 | return sd->overrun_trim_data.ellipsis_glyph_buf.ptr(); |
| 6178 | } |
| 6179 | |
| 6180 | int64_t TextServerAdvanced::_shaped_text_get_ellipsis_glyph_count(const RID &p_shaped) const { |
| 6181 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 6182 | ERR_FAIL_NULL_V_MSG(sd, 0, "ShapedTextDataAdvanced invalid."); |
| 6183 | |
| 6184 | MutexLock lock(sd->mutex); |
| 6185 | return sd->overrun_trim_data.ellipsis_glyph_buf.size(); |
| 6186 | } |
| 6187 | |
| 6188 | void TextServerAdvanced::_update_chars(ShapedTextDataAdvanced *p_sd) const { |
| 6189 | if (!p_sd->chars_valid) { |
| 6190 | p_sd->chars.clear(); |
| 6191 | |
| 6192 | const UChar *data = p_sd->utf16.get_data(); |
| 6193 | UErrorCode err = U_ZERO_ERROR; |
| 6194 | int prev = -1; |
| 6195 | int i = 0; |
| 6196 | |
| 6197 | Vector<ShapedTextDataAdvanced::Span> &spans = p_sd->spans; |
| 6198 | if (p_sd->parent != RID()) { |
| 6199 | ShapedTextDataAdvanced *parent_sd = shaped_owner.get_or_null(p_sd->parent); |
| 6200 | ERR_FAIL_COND(!parent_sd->valid.is_set()); |
| 6201 | spans = parent_sd->spans; |
| 6202 | } |
| 6203 | |
| 6204 | int span_size = spans.size(); |
| 6205 | while (i < span_size) { |
| 6206 | if (spans[i].start > p_sd->end) { |
| 6207 | break; |
| 6208 | } |
| 6209 | if (spans[i].end < p_sd->start) { |
| 6210 | i++; |
| 6211 | continue; |
| 6212 | } |
| 6213 | |
| 6214 | int r_start = MAX(0, spans[i].start - p_sd->start); |
| 6215 | String language = spans[i].language; |
| 6216 | while (i + 1 < span_size && language == spans[i + 1].language) { |
| 6217 | i++; |
| 6218 | } |
| 6219 | int r_end = MIN(spans[i].end - p_sd->start, p_sd->text.length()); |
| 6220 | UBreakIterator *bi = ubrk_open(UBRK_CHARACTER, (language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale().ascii().get_data() : language.ascii().get_data(), data + _convert_pos_inv(p_sd, r_start), _convert_pos_inv(p_sd, r_end - r_start), &err); |
| 6221 | if (U_SUCCESS(err)) { |
| 6222 | while (ubrk_next(bi) != UBRK_DONE) { |
| 6223 | int pos = _convert_pos(p_sd, ubrk_current(bi)) + r_start + p_sd->start; |
| 6224 | if (prev != pos) { |
| 6225 | p_sd->chars.push_back(pos); |
| 6226 | } |
| 6227 | prev = pos; |
| 6228 | } |
| 6229 | ubrk_close(bi); |
| 6230 | } else { |
| 6231 | for (int j = r_start; j < r_end; j++) { |
| 6232 | if (prev != j) { |
| 6233 | p_sd->chars.push_back(j + 1 + p_sd->start); |
| 6234 | } |
| 6235 | prev = j; |
| 6236 | } |
| 6237 | } |
| 6238 | i++; |
| 6239 | } |
| 6240 | p_sd->chars_valid = true; |
| 6241 | } |
| 6242 | } |
| 6243 | |
| 6244 | PackedInt32Array TextServerAdvanced::_shaped_text_get_character_breaks(const RID &p_shaped) const { |
| 6245 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 6246 | ERR_FAIL_NULL_V(sd, PackedInt32Array()); |
| 6247 | |
| 6248 | MutexLock lock(sd->mutex); |
| 6249 | if (!sd->valid.is_set()) { |
| 6250 | const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped); |
| 6251 | } |
| 6252 | |
| 6253 | _update_chars(sd); |
| 6254 | |
| 6255 | return sd->chars; |
| 6256 | } |
| 6257 | |
| 6258 | bool TextServerAdvanced::_shaped_text_update_breaks(const RID &p_shaped) { |
| 6259 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 6260 | ERR_FAIL_NULL_V(sd, false); |
| 6261 | |
| 6262 | MutexLock lock(sd->mutex); |
| 6263 | if (!sd->valid.is_set()) { |
| 6264 | _shaped_text_shape(p_shaped); |
| 6265 | } |
| 6266 | |
| 6267 | if (sd->line_breaks_valid) { |
| 6268 | return true; // Nothing to do. |
| 6269 | } |
| 6270 | |
| 6271 | const UChar *data = sd->utf16.get_data(); |
| 6272 | |
| 6273 | if (!sd->break_ops_valid) { |
| 6274 | sd->breaks.clear(); |
| 6275 | sd->break_inserts = 0; |
| 6276 | UErrorCode err = U_ZERO_ERROR; |
| 6277 | int i = 0; |
| 6278 | int span_size = sd->spans.size(); |
| 6279 | while (i < span_size) { |
| 6280 | bool start_with_obj = sd->spans[i].embedded_key != Variant(); |
| 6281 | String language = sd->spans[i].language; |
| 6282 | int r_start = sd->spans[i].start; |
| 6283 | if (r_start == sd->spans[i].end) { |
| 6284 | i++; |
| 6285 | continue; |
| 6286 | } |
| 6287 | while (i + 1 < span_size && (language == sd->spans[i + 1].language || start_with_obj || sd->spans[i + 1].embedded_key != Variant() || sd->spans[i + 1].start == sd->spans[i + 1].end)) { |
| 6288 | if (start_with_obj && sd->spans[i + 1].embedded_key == Variant()) { |
| 6289 | language = sd->spans[i + 1].language; |
| 6290 | start_with_obj = false; |
| 6291 | } |
| 6292 | i++; |
| 6293 | } |
| 6294 | int r_end = sd->spans[i].end; |
| 6295 | UBreakIterator *bi = _create_line_break_iterator_for_locale(language, &err); |
| 6296 | |
| 6297 | if (!U_FAILURE(err) && bi) { |
| 6298 | ubrk_setText(bi, data + _convert_pos_inv(sd, r_start), _convert_pos_inv(sd, r_end - r_start), &err); |
| 6299 | } |
| 6300 | |
| 6301 | if (U_FAILURE(err) || !bi) { |
| 6302 | // No data loaded - use fallback. |
| 6303 | for (int j = r_start; j < r_end; j++) { |
| 6304 | char32_t c = sd->text[j - sd->start]; |
| 6305 | char32_t c_next = (j < r_end) ? sd->text[j - sd->start + 1] : 0x0000; |
| 6306 | if (is_whitespace(c)) { |
| 6307 | sd->breaks[j + 1] = false; |
| 6308 | } |
| 6309 | if (is_linebreak(c)) { |
| 6310 | if (c != 0x000D || c_next != 0x000A) { // Skip first hard break in CR-LF pair. |
| 6311 | sd->breaks[j + 1] = true; |
| 6312 | } |
| 6313 | } |
| 6314 | } |
| 6315 | } else { |
| 6316 | while (ubrk_next(bi) != UBRK_DONE) { |
| 6317 | int pos = _convert_pos(sd, ubrk_current(bi)) + r_start; |
| 6318 | if ((ubrk_getRuleStatus(bi) >= UBRK_LINE_HARD) && (ubrk_getRuleStatus(bi) < UBRK_LINE_HARD_LIMIT)) { |
| 6319 | sd->breaks[pos] = true; |
| 6320 | } else if ((ubrk_getRuleStatus(bi) >= UBRK_LINE_SOFT) && (ubrk_getRuleStatus(bi) < UBRK_LINE_SOFT_LIMIT)) { |
| 6321 | sd->breaks[pos] = false; |
| 6322 | } |
| 6323 | int pos_p = pos - 1 - sd->start; |
| 6324 | char32_t c = sd->text[pos_p]; |
| 6325 | if (pos - sd->start != sd->end && !is_whitespace(c) && (c != 0xfffc)) { |
| 6326 | sd->break_inserts++; |
| 6327 | } |
| 6328 | } |
| 6329 | ubrk_close(bi); |
| 6330 | } |
| 6331 | i++; |
| 6332 | } |
| 6333 | sd->break_ops_valid = true; |
| 6334 | } |
| 6335 | |
| 6336 | LocalVector<Glyph> glyphs_new; |
| 6337 | |
| 6338 | bool rewrite = false; |
| 6339 | int sd_shift = 0; |
| 6340 | int sd_size = sd->glyphs.size(); |
| 6341 | Glyph *sd_glyphs = sd->glyphs.ptr(); |
| 6342 | Glyph *sd_glyphs_new = nullptr; |
| 6343 | |
| 6344 | if (sd->break_inserts > 0) { |
| 6345 | glyphs_new.resize(sd->glyphs.size() + sd->break_inserts); |
| 6346 | sd_glyphs_new = glyphs_new.ptr(); |
| 6347 | rewrite = true; |
| 6348 | } else { |
| 6349 | sd_glyphs_new = sd_glyphs; |
| 6350 | } |
| 6351 | |
| 6352 | sd->sort_valid = false; |
| 6353 | sd->glyphs_logical.clear(); |
| 6354 | const char32_t *ch = sd->text.ptr(); |
| 6355 | |
| 6356 | int c_punct_size = sd->custom_punct.length(); |
| 6357 | const char32_t *c_punct = sd->custom_punct.ptr(); |
| 6358 | |
| 6359 | for (int i = 0; i < sd_size; i++) { |
| 6360 | if (rewrite) { |
| 6361 | for (int j = 0; j < sd_glyphs[i].count; j++) { |
| 6362 | sd_glyphs_new[sd_shift + i + j] = sd_glyphs[i + j]; |
| 6363 | } |
| 6364 | } |
| 6365 | if (sd_glyphs[i].count > 0) { |
| 6366 | char32_t c = ch[sd_glyphs[i].start - sd->start]; |
| 6367 | if (c == 0xfffc) { |
| 6368 | i += (sd_glyphs[i].count - 1); |
| 6369 | continue; |
| 6370 | } |
| 6371 | if (c == 0x0009 || c == 0x000b) { |
| 6372 | sd_glyphs_new[sd_shift + i].flags |= GRAPHEME_IS_TAB; |
| 6373 | } |
| 6374 | if (c == 0x00ad) { |
| 6375 | sd_glyphs_new[sd_shift + i].flags |= GRAPHEME_IS_SOFT_HYPHEN; |
| 6376 | } |
| 6377 | if (is_whitespace(c)) { |
| 6378 | sd_glyphs_new[sd_shift + i].flags |= GRAPHEME_IS_SPACE; |
| 6379 | } |
| 6380 | if (c_punct_size == 0) { |
| 6381 | if (u_ispunct(c) && c != 0x005f) { |
| 6382 | sd_glyphs_new[sd_shift + i].flags |= GRAPHEME_IS_PUNCTUATION; |
| 6383 | } |
| 6384 | } else { |
| 6385 | for (int j = 0; j < c_punct_size; j++) { |
| 6386 | if (c_punct[j] == c) { |
| 6387 | sd_glyphs_new[sd_shift + i].flags |= GRAPHEME_IS_PUNCTUATION; |
| 6388 | break; |
| 6389 | } |
| 6390 | } |
| 6391 | } |
| 6392 | if (is_underscore(c)) { |
| 6393 | sd_glyphs_new[sd_shift + i].flags |= GRAPHEME_IS_UNDERSCORE; |
| 6394 | } |
| 6395 | if (sd->breaks.has(sd_glyphs[i].end)) { |
| 6396 | if (sd->breaks[sd_glyphs[i].end] && (is_linebreak(c))) { |
| 6397 | sd_glyphs_new[sd_shift + i].flags |= GRAPHEME_IS_BREAK_HARD; |
| 6398 | } else if (is_whitespace(c) || c == 0x00ad) { |
| 6399 | sd_glyphs_new[sd_shift + i].flags |= GRAPHEME_IS_BREAK_SOFT; |
| 6400 | } else { |
| 6401 | int count = sd_glyphs[i].count; |
| 6402 | // Do not add extra space at the end of the line. |
| 6403 | if (sd_glyphs[i].end == sd->end) { |
| 6404 | i += (sd_glyphs[i].count - 1); |
| 6405 | continue; |
| 6406 | } |
| 6407 | // Do not add extra space after existing space. |
| 6408 | if (sd_glyphs[i].flags & GRAPHEME_IS_RTL) { |
| 6409 | if ((i + count < sd_size - 1) && ((sd_glyphs[i + count].flags & (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT)) == (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT))) { |
| 6410 | i += (sd_glyphs[i].count - 1); |
| 6411 | continue; |
| 6412 | } |
| 6413 | } else { |
| 6414 | if ((sd_glyphs[i].flags & (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT)) == (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT)) { |
| 6415 | i += (sd_glyphs[i].count - 1); |
| 6416 | continue; |
| 6417 | } |
| 6418 | } |
| 6419 | // Do not add extra space for color picker object. |
| 6420 | if (((sd_glyphs[i].flags & GRAPHEME_IS_EMBEDDED_OBJECT) == GRAPHEME_IS_EMBEDDED_OBJECT && sd_glyphs[i].start == sd_glyphs[i].end) || (uint32_t(i + 1) < sd->glyphs.size() && (sd_glyphs[i + 1].flags & GRAPHEME_IS_EMBEDDED_OBJECT) == GRAPHEME_IS_EMBEDDED_OBJECT && sd_glyphs[i + 1].start == sd_glyphs[i + 1].end)) { |
| 6421 | i += (sd_glyphs[i].count - 1); |
| 6422 | continue; |
| 6423 | } |
| 6424 | Glyph gl; |
| 6425 | gl.span_index = sd_glyphs[i].span_index; |
| 6426 | gl.start = sd_glyphs[i].start; |
| 6427 | gl.end = sd_glyphs[i].end; |
| 6428 | gl.count = 1; |
| 6429 | gl.font_rid = sd_glyphs[i].font_rid; |
| 6430 | gl.font_size = sd_glyphs[i].font_size; |
| 6431 | gl.flags = GRAPHEME_IS_BREAK_SOFT | GRAPHEME_IS_VIRTUAL | GRAPHEME_IS_SPACE; |
| 6432 | // Mark virtual space after punctuation as punctuation to avoid justification at this point. |
| 6433 | if (c_punct_size == 0) { |
| 6434 | if (u_ispunct(c) && c != 0x005f) { |
| 6435 | gl.flags |= GRAPHEME_IS_PUNCTUATION; |
| 6436 | } |
| 6437 | } else { |
| 6438 | for (int j = 0; j < c_punct_size; j++) { |
| 6439 | if (c_punct[j] == c) { |
| 6440 | gl.flags |= GRAPHEME_IS_PUNCTUATION; |
| 6441 | break; |
| 6442 | } |
| 6443 | } |
| 6444 | } |
| 6445 | if (sd_glyphs[i].flags & GRAPHEME_IS_RTL) { |
| 6446 | gl.flags |= GRAPHEME_IS_RTL; |
| 6447 | for (int j = sd_glyphs[i].count - 1; j >= 0; j--) { |
| 6448 | sd_glyphs_new[sd_shift + i + j + 1] = sd_glyphs_new[sd_shift + i + j]; |
| 6449 | } |
| 6450 | sd_glyphs_new[sd_shift + i] = gl; |
| 6451 | } else { |
| 6452 | sd_glyphs_new[sd_shift + i + count] = gl; |
| 6453 | } |
| 6454 | sd_shift++; |
| 6455 | ERR_FAIL_COND_V_MSG(sd_shift > sd->break_inserts, false, "Invalid break insert count!"); |
| 6456 | } |
| 6457 | } |
| 6458 | i += (sd_glyphs[i].count - 1); |
| 6459 | } |
| 6460 | } |
| 6461 | if (sd_shift < sd->break_inserts) { |
| 6462 | // Note: should not happen with a normal text, but might be a case with special fonts that substitute a long string (with breaks opportunities in it) with a single glyph (like Font Awesome). |
| 6463 | glyphs_new.resize(sd->glyphs.size() + sd_shift); |
| 6464 | } |
| 6465 | |
| 6466 | if (sd->break_inserts > 0) { |
| 6467 | sd->glyphs = std::move(glyphs_new); |
| 6468 | } |
| 6469 | |
| 6470 | sd->line_breaks_valid = true; |
| 6471 | |
| 6472 | return sd->line_breaks_valid; |
| 6473 | } |
| 6474 | |
| 6475 | _FORCE_INLINE_ int64_t _generate_kashida_justification_opportunities(const String &p_data, int64_t p_start, int64_t p_end) { |
| 6476 | int64_t kashida_pos = -1; |
| 6477 | int8_t priority = 100; |
| 6478 | int64_t i = p_start; |
| 6479 | |
| 6480 | char32_t pc = 0; |
| 6481 | |
| 6482 | while ((p_end > p_start) && is_transparent(p_data[p_end - 1])) { |
| 6483 | p_end--; |
| 6484 | } |
| 6485 | |
| 6486 | while (i < p_end) { |
| 6487 | uint32_t c = p_data[i]; |
| 6488 | |
| 6489 | if (c == 0x0640) { |
| 6490 | kashida_pos = i; |
| 6491 | priority = 0; |
| 6492 | } |
| 6493 | if (priority >= 1 && i < p_end - 1) { |
| 6494 | if (is_seen_sad(c) && (p_data[i + 1] != 0x200c)) { |
| 6495 | kashida_pos = i; |
| 6496 | priority = 1; |
| 6497 | } |
| 6498 | } |
| 6499 | if (priority >= 2 && i > p_start) { |
| 6500 | if (is_teh_marbuta(c) || is_dal(c) || (is_heh(c) && i == p_end - 1)) { |
| 6501 | if (is_connected_to_prev(c, pc)) { |
| 6502 | kashida_pos = i - 1; |
| 6503 | priority = 2; |
| 6504 | } |
| 6505 | } |
| 6506 | } |
| 6507 | if (priority >= 3 && i > p_start) { |
| 6508 | if (is_alef(c) || ((is_lam(c) || is_tah(c) || is_kaf(c) || is_gaf(c)) && i == p_end - 1)) { |
| 6509 | if (is_connected_to_prev(c, pc)) { |
| 6510 | kashida_pos = i - 1; |
| 6511 | priority = 3; |
| 6512 | } |
| 6513 | } |
| 6514 | } |
| 6515 | if (priority >= 4 && i > p_start && i < p_end - 1) { |
| 6516 | if (is_beh(c)) { |
| 6517 | if (is_reh(p_data[i + 1]) || is_yeh(p_data[i + 1])) { |
| 6518 | if (is_connected_to_prev(c, pc)) { |
| 6519 | kashida_pos = i - 1; |
| 6520 | priority = 4; |
| 6521 | } |
| 6522 | } |
| 6523 | } |
| 6524 | } |
| 6525 | if (priority >= 5 && i > p_start) { |
| 6526 | if (is_waw(c) || ((is_ain(c) || is_qaf(c) || is_feh(c)) && i == p_end - 1)) { |
| 6527 | if (is_connected_to_prev(c, pc)) { |
| 6528 | kashida_pos = i - 1; |
| 6529 | priority = 5; |
| 6530 | } |
| 6531 | } |
| 6532 | } |
| 6533 | if (priority >= 6 && i > p_start) { |
| 6534 | if (is_reh(c)) { |
| 6535 | if (is_connected_to_prev(c, pc)) { |
| 6536 | kashida_pos = i - 1; |
| 6537 | priority = 6; |
| 6538 | } |
| 6539 | } |
| 6540 | } |
| 6541 | if (!is_transparent(c)) { |
| 6542 | pc = c; |
| 6543 | } |
| 6544 | i++; |
| 6545 | } |
| 6546 | |
| 6547 | return kashida_pos; |
| 6548 | } |
| 6549 | |
| 6550 | bool TextServerAdvanced::_shaped_text_update_justification_ops(const RID &p_shaped) { |
| 6551 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 6552 | ERR_FAIL_NULL_V(sd, false); |
| 6553 | |
| 6554 | MutexLock lock(sd->mutex); |
| 6555 | if (!sd->valid.is_set()) { |
| 6556 | _shaped_text_shape(p_shaped); |
| 6557 | } |
| 6558 | if (!sd->line_breaks_valid) { |
| 6559 | _shaped_text_update_breaks(p_shaped); |
| 6560 | } |
| 6561 | |
| 6562 | if (sd->justification_ops_valid) { |
| 6563 | return true; // Nothing to do. |
| 6564 | } |
| 6565 | |
| 6566 | const UChar *data = sd->utf16.get_data(); |
| 6567 | int data_size = sd->utf16.length(); |
| 6568 | |
| 6569 | if (!sd->js_ops_valid) { |
| 6570 | sd->jstops.clear(); |
| 6571 | |
| 6572 | // Use ICU word iterator and custom kashida detection. |
| 6573 | UErrorCode err = U_ZERO_ERROR; |
| 6574 | UBreakIterator *bi = ubrk_open(UBRK_WORD, "", data, data_size, &err); |
| 6575 | if (U_FAILURE(err)) { |
| 6576 | // No data - use fallback. |
| 6577 | int limit = 0; |
| 6578 | for (int i = 0; i < sd->text.length(); i++) { |
| 6579 | if (is_whitespace(sd->text[i])) { |
| 6580 | int ks = _generate_kashida_justification_opportunities(sd->text, limit, i) + sd->start; |
| 6581 | if (ks != -1) { |
| 6582 | sd->jstops[ks] = true; |
| 6583 | } |
| 6584 | limit = i + 1; |
| 6585 | } |
| 6586 | } |
| 6587 | int ks = _generate_kashida_justification_opportunities(sd->text, limit, sd->text.length()) + sd->start; |
| 6588 | if (ks != -1) { |
| 6589 | sd->jstops[ks] = true; |
| 6590 | } |
| 6591 | } else { |
| 6592 | int limit = 0; |
| 6593 | while (ubrk_next(bi) != UBRK_DONE) { |
| 6594 | if (ubrk_getRuleStatus(bi) != UBRK_WORD_NONE) { |
| 6595 | int i = _convert_pos(sd, ubrk_current(bi)); |
| 6596 | sd->jstops[i + sd->start] = false; |
| 6597 | int ks = _generate_kashida_justification_opportunities(sd->text, limit, i); |
| 6598 | if (ks != -1) { |
| 6599 | sd->jstops[ks + sd->start] = true; |
| 6600 | } |
| 6601 | limit = i; |
| 6602 | } |
| 6603 | } |
| 6604 | ubrk_close(bi); |
| 6605 | } |
| 6606 | |
| 6607 | sd->js_ops_valid = true; |
| 6608 | } |
| 6609 | |
| 6610 | sd->sort_valid = false; |
| 6611 | sd->glyphs_logical.clear(); |
| 6612 | |
| 6613 | Glyph *sd_glyphs = sd->glyphs.ptr(); |
| 6614 | int sd_size = sd->glyphs.size(); |
| 6615 | if (!sd->jstops.is_empty()) { |
| 6616 | for (int i = 0; i < sd_size; i++) { |
| 6617 | if (sd_glyphs[i].count > 0) { |
| 6618 | char32_t c = sd->text[sd_glyphs[i].start - sd->start]; |
| 6619 | if (c == 0x0640 && sd_glyphs[i].start == sd_glyphs[i].end - 1) { |
| 6620 | sd_glyphs[i].flags |= GRAPHEME_IS_ELONGATION; |
| 6621 | } |
| 6622 | if (sd->jstops.has(sd_glyphs[i].start)) { |
| 6623 | if (c == 0xfffc || c == 0x00ad) { |
| 6624 | continue; |
| 6625 | } |
| 6626 | if (sd->jstops[sd_glyphs[i].start]) { |
| 6627 | if (c != 0x0640) { |
| 6628 | if (sd_glyphs[i].font_rid != RID()) { |
| 6629 | Glyph gl = _shape_single_glyph(sd, 0x0640, HB_SCRIPT_ARABIC, HB_DIRECTION_RTL, sd->glyphs[i].font_rid, sd->glyphs[i].font_size); |
| 6630 | if ((sd_glyphs[i].flags & GRAPHEME_IS_VALID) == GRAPHEME_IS_VALID) { |
| 6631 | #if HB_VERSION_ATLEAST(5, 1, 0) |
| 6632 | if ((i > 0) && ((sd_glyphs[i - 1].flags & GRAPHEME_IS_SAFE_TO_INSERT_TATWEEL) != GRAPHEME_IS_SAFE_TO_INSERT_TATWEEL)) { |
| 6633 | continue; |
| 6634 | } |
| 6635 | #endif |
| 6636 | gl.start = sd_glyphs[i].start; |
| 6637 | gl.end = sd_glyphs[i].end; |
| 6638 | gl.repeat = 0; |
| 6639 | gl.count = 1; |
| 6640 | if (sd->orientation == ORIENTATION_HORIZONTAL) { |
| 6641 | gl.y_off = sd_glyphs[i].y_off; |
| 6642 | } else { |
| 6643 | gl.x_off = sd_glyphs[i].x_off; |
| 6644 | } |
| 6645 | gl.flags |= GRAPHEME_IS_ELONGATION | GRAPHEME_IS_VIRTUAL; |
| 6646 | sd->glyphs.insert(i, gl); |
| 6647 | i++; |
| 6648 | |
| 6649 | // Update write pointer and size. |
| 6650 | sd_size = sd->glyphs.size(); |
| 6651 | sd_glyphs = sd->glyphs.ptr(); |
| 6652 | continue; |
| 6653 | } |
| 6654 | } |
| 6655 | } |
| 6656 | } else if ((sd_glyphs[i].flags & GRAPHEME_IS_SPACE) != GRAPHEME_IS_SPACE && (sd_glyphs[i].flags & GRAPHEME_IS_PUNCTUATION) != GRAPHEME_IS_PUNCTUATION) { |
| 6657 | int count = sd_glyphs[i].count; |
| 6658 | // Do not add extra spaces at the end of the line. |
| 6659 | if (sd_glyphs[i].end == sd->end) { |
| 6660 | continue; |
| 6661 | } |
| 6662 | // Do not add extra space after existing space. |
| 6663 | if (sd_glyphs[i].flags & GRAPHEME_IS_RTL) { |
| 6664 | if ((i + count < sd_size - 1) && ((sd_glyphs[i + count].flags & (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT)) == (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT))) { |
| 6665 | continue; |
| 6666 | } |
| 6667 | } else { |
| 6668 | if ((i > 0) && ((sd_glyphs[i - 1].flags & (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT)) == (GRAPHEME_IS_SPACE | GRAPHEME_IS_BREAK_SOFT))) { |
| 6669 | continue; |
| 6670 | } |
| 6671 | } |
| 6672 | // Inject virtual space for alignment. |
| 6673 | Glyph gl; |
| 6674 | gl.span_index = sd_glyphs[i].span_index; |
| 6675 | gl.start = sd_glyphs[i].start; |
| 6676 | gl.end = sd_glyphs[i].end; |
| 6677 | gl.count = 1; |
| 6678 | gl.font_rid = sd_glyphs[i].font_rid; |
| 6679 | gl.font_size = sd_glyphs[i].font_size; |
| 6680 | gl.flags = GRAPHEME_IS_SPACE | GRAPHEME_IS_VIRTUAL; |
| 6681 | if (sd_glyphs[i].flags & GRAPHEME_IS_RTL) { |
| 6682 | gl.flags |= GRAPHEME_IS_RTL; |
| 6683 | sd->glyphs.insert(i, gl); // Insert before. |
| 6684 | } else { |
| 6685 | sd->glyphs.insert(i + count, gl); // Insert after. |
| 6686 | } |
| 6687 | i += count; |
| 6688 | |
| 6689 | // Update write pointer and size. |
| 6690 | sd_size = sd->glyphs.size(); |
| 6691 | sd_glyphs = sd->glyphs.ptr(); |
| 6692 | continue; |
| 6693 | } |
| 6694 | } |
| 6695 | } |
| 6696 | } |
| 6697 | } |
| 6698 | |
| 6699 | sd->justification_ops_valid = true; |
| 6700 | return sd->justification_ops_valid; |
| 6701 | } |
| 6702 | |
| 6703 | Glyph TextServerAdvanced::_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) { |
| 6704 | bool color = false; |
| 6705 | hb_font_t *hb_font = _font_get_hb_handle(p_font, p_font_size, color); |
| 6706 | double scale = _font_get_scale(p_font, p_font_size); |
| 6707 | bool subpos = (scale != 1.0) || (_font_get_subpixel_positioning(p_font) == SUBPIXEL_POSITIONING_ONE_HALF) || (_font_get_subpixel_positioning(p_font) == SUBPIXEL_POSITIONING_ONE_QUARTER) || (_font_get_subpixel_positioning(p_font) == SUBPIXEL_POSITIONING_AUTO && p_font_size <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE); |
| 6708 | ERR_FAIL_NULL_V(hb_font, Glyph()); |
| 6709 | |
| 6710 | hb_buffer_clear_contents(p_sd->hb_buffer); |
| 6711 | hb_buffer_set_direction(p_sd->hb_buffer, p_direction); |
| 6712 | hb_buffer_set_flags(p_sd->hb_buffer, (hb_buffer_flags_t)(HB_BUFFER_FLAG_DEFAULT)); |
| 6713 | hb_buffer_set_script(p_sd->hb_buffer, (p_script == HB_TAG('Z', 's', 'y', 'e')) ? HB_SCRIPT_COMMON : p_script); |
| 6714 | hb_buffer_add_utf32(p_sd->hb_buffer, (const uint32_t *)&p_char, 1, 0, 1); |
| 6715 | |
| 6716 | hb_shape(hb_font, p_sd->hb_buffer, nullptr, 0); |
| 6717 | |
| 6718 | unsigned int glyph_count = 0; |
| 6719 | hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(p_sd->hb_buffer, &glyph_count); |
| 6720 | hb_glyph_position_t *glyph_pos = hb_buffer_get_glyph_positions(p_sd->hb_buffer, &glyph_count); |
| 6721 | |
| 6722 | // Process glyphs. |
| 6723 | Glyph gl; |
| 6724 | |
| 6725 | if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) { |
| 6726 | gl.flags |= TextServer::GRAPHEME_IS_RTL; |
| 6727 | } |
| 6728 | |
| 6729 | gl.font_rid = p_font; |
| 6730 | gl.font_size = p_font_size; |
| 6731 | |
| 6732 | if (glyph_count > 0) { |
| 6733 | if (p_sd->orientation == ORIENTATION_HORIZONTAL) { |
| 6734 | if (subpos) { |
| 6735 | gl.advance = (double)glyph_pos[0].x_advance / (64.0 / scale) + _get_extra_advance(p_font, p_font_size); |
| 6736 | } else { |
| 6737 | gl.advance = Math::round((double)glyph_pos[0].x_advance / (64.0 / scale) + _get_extra_advance(p_font, p_font_size)); |
| 6738 | } |
| 6739 | } else { |
| 6740 | gl.advance = -Math::round((double)glyph_pos[0].y_advance / (64.0 / scale)); |
| 6741 | } |
| 6742 | gl.count = 1; |
| 6743 | |
| 6744 | gl.index = glyph_info[0].codepoint; |
| 6745 | if (subpos) { |
| 6746 | gl.x_off = (double)glyph_pos[0].x_offset / (64.0 / scale); |
| 6747 | } else { |
| 6748 | gl.x_off = Math::round((double)glyph_pos[0].x_offset / (64.0 / scale)); |
| 6749 | } |
| 6750 | gl.y_off = -Math::round((double)glyph_pos[0].y_offset / (64.0 / scale)); |
| 6751 | if (p_sd->orientation == ORIENTATION_HORIZONTAL) { |
| 6752 | gl.y_off += _font_get_baseline_offset(gl.font_rid) * (double)(_font_get_ascent(gl.font_rid, gl.font_size) + _font_get_descent(gl.font_rid, gl.font_size)); |
| 6753 | } else { |
| 6754 | gl.x_off += _font_get_baseline_offset(gl.font_rid) * (double)(_font_get_ascent(gl.font_rid, gl.font_size) + _font_get_descent(gl.font_rid, gl.font_size)); |
| 6755 | } |
| 6756 | |
| 6757 | if ((glyph_info[0].codepoint != 0) || !u_isgraph(p_char)) { |
| 6758 | gl.flags |= GRAPHEME_IS_VALID; |
| 6759 | } |
| 6760 | } |
| 6761 | return gl; |
| 6762 | } |
| 6763 | |
| 6764 | _FORCE_INLINE_ void TextServerAdvanced::_add_features(const Dictionary &p_source, Vector<hb_feature_t> &r_ftrs) { |
| 6765 | for (const KeyValue<Variant, Variant> &key_value : p_source) { |
| 6766 | int32_t value = key_value.value; |
| 6767 | if (value >= 0) { |
| 6768 | hb_feature_t feature; |
| 6769 | if (key_value.key.is_string()) { |
| 6770 | feature.tag = _name_to_tag(key_value.key); |
| 6771 | } else { |
| 6772 | feature.tag = key_value.key; |
| 6773 | } |
| 6774 | feature.value = value; |
| 6775 | feature.start = 0; |
| 6776 | feature.end = -1; |
| 6777 | r_ftrs.push_back(feature); |
| 6778 | } |
| 6779 | } |
| 6780 | } |
| 6781 | |
| 6782 | UBreakIterator *TextServerAdvanced::_create_line_break_iterator_for_locale(const String &p_language, UErrorCode *r_err) const { |
| 6783 | // Creating UBreakIterator (ubrk_open) is surprisingly costly. |
| 6784 | // However, cloning (ubrk_clone) is cheaper, so we keep around blueprints to accelerate creating new ones. |
| 6785 | |
| 6786 | String language = p_language.is_empty() ? TranslationServer::get_singleton()->get_tool_locale() : p_language; |
| 6787 | if (!language.contains("@")) { |
| 6788 | if (lb_strictness == LB_LOOSE) { |
| 6789 | language += "@lb=loose"; |
| 6790 | } else if (lb_strictness == LB_NORMAL) { |
| 6791 | language += "@lb=normal"; |
| 6792 | } else if (lb_strictness == LB_STRICT) { |
| 6793 | language += "@lb=strict"; |
| 6794 | } |
| 6795 | } |
| 6796 | |
| 6797 | _THREAD_SAFE_METHOD_ |
| 6798 | const HashMap<String, UBreakIterator *>::Iterator key_value = line_break_iterators_per_language.find(language); |
| 6799 | if (key_value) { |
| 6800 | return ubrk_clone(key_value->value, r_err); |
| 6801 | } |
| 6802 | UBreakIterator *bi = ubrk_open(UBRK_LINE, language.ascii().get_data(), nullptr, 0, r_err); |
| 6803 | if (U_FAILURE(*r_err) || !bi) { |
| 6804 | return nullptr; |
| 6805 | } |
| 6806 | line_break_iterators_per_language.insert(language, bi); |
| 6807 | return ubrk_clone(bi, r_err); |
| 6808 | } |
| 6809 | |
| 6810 | void TextServerAdvanced::_shape_run(ShapedTextDataAdvanced *p_sd, int64_t p_start, int64_t p_end, const String &p_language, hb_script_t p_script, hb_direction_t p_direction, FontPriorityList &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) { |
| 6811 | RID f; |
| 6812 | int fs = p_sd->spans[p_span].font_size; |
| 6813 | if (p_fb_index >= 0 && p_fb_index < p_fonts.size()) { |
| 6814 | // Try font from list. |
| 6815 | f = p_fonts[p_fb_index]; |
| 6816 | } else if (OS::get_singleton()->has_feature("system_fonts") && p_fonts.size() > 0 && ((p_fb_index == p_fonts.size()) || (p_fb_index > p_fonts.size() && p_start != p_prev_start))) { |
| 6817 | // Try system fallback. |
| 6818 | if (_font_is_allow_system_fallback(p_fonts[0])) { |
| 6819 | _update_chars(p_sd); |
| 6820 | |
| 6821 | int64_t next = p_end; |
| 6822 | for (const int32_t &E : p_sd->chars) { |
| 6823 | if (E > p_start) { |
| 6824 | next = E; |
| 6825 | break; |
| 6826 | } |
| 6827 | } |
| 6828 | char scr_buffer[5] = { 0, 0, 0, 0, 0 }; |
| 6829 | hb_tag_to_string(hb_script_to_iso15924_tag(p_script), scr_buffer); |
| 6830 | String script_code = String(scr_buffer); |
| 6831 | |
| 6832 | String text = p_sd->text.substr(p_start, next - p_start); |
| 6833 | f = _find_sys_font_for_text(p_fonts[0], script_code, p_language, text); |
| 6834 | } |
| 6835 | } |
| 6836 | |
| 6837 | if (!f.is_valid()) { |
| 6838 | // Shaping failed, try looking up raw characters or use fallback hex code boxes. |
| 6839 | int fb_from = (p_direction != HB_DIRECTION_RTL) ? p_start : p_end - 1; |
| 6840 | int fb_to = (p_direction != HB_DIRECTION_RTL) ? p_end : p_start - 1; |
| 6841 | int fb_delta = (p_direction != HB_DIRECTION_RTL) ? +1 : -1; |
| 6842 | |
| 6843 | for (int i = fb_from; i != fb_to; i += fb_delta) { |
| 6844 | if (p_sd->preserve_invalid || (p_sd->preserve_control && is_control(p_sd->text[i]))) { |
| 6845 | Glyph gl; |
| 6846 | gl.span_index = p_span; |
| 6847 | gl.start = i + p_sd->start; |
| 6848 | gl.end = i + 1 + p_sd->start; |
| 6849 | gl.count = 1; |
| 6850 | gl.font_size = fs; |
| 6851 | if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) { |
| 6852 | gl.flags |= TextServer::GRAPHEME_IS_RTL; |
| 6853 | } |
| 6854 | |
| 6855 | bool found = false; |
| 6856 | for (uint32_t j = 0; j <= p_fonts.size(); j++) { |
| 6857 | RID f_rid; |
| 6858 | if (j == p_fonts.size()) { |
| 6859 | f_rid = p_prev_font; |
| 6860 | } else { |
| 6861 | f_rid = p_fonts[j]; |
| 6862 | } |
| 6863 | if (f_rid.is_valid() && _font_has_char(f_rid, p_sd->text[i])) { |
| 6864 | gl.font_rid = f_rid; |
| 6865 | gl.index = _font_get_glyph_index(gl.font_rid, fs, p_sd->text[i], 0); |
| 6866 | if (p_sd->orientation == ORIENTATION_HORIZONTAL) { |
| 6867 | gl.advance = _font_get_glyph_advance(gl.font_rid, fs, gl.index).x; |
| 6868 | gl.x_off = 0; |
| 6869 | gl.y_off = _font_get_baseline_offset(gl.font_rid) * (double)(_font_get_ascent(gl.font_rid, gl.font_size) + _font_get_descent(gl.font_rid, gl.font_size)); |
| 6870 | p_sd->ascent = MAX(p_sd->ascent, _font_get_ascent(gl.font_rid, gl.font_size) + _font_get_spacing(gl.font_rid, SPACING_TOP)); |
| 6871 | p_sd->descent = MAX(p_sd->descent, _font_get_descent(gl.font_rid, gl.font_size) + _font_get_spacing(gl.font_rid, SPACING_BOTTOM)); |
| 6872 | } else { |
| 6873 | gl.advance = _font_get_glyph_advance(gl.font_rid, fs, gl.index).y; |
| 6874 | gl.x_off = -Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5) + _font_get_baseline_offset(gl.font_rid) * (double)(_font_get_ascent(gl.font_rid, gl.font_size) + _font_get_descent(gl.font_rid, gl.font_size)); |
| 6875 | gl.y_off = _font_get_ascent(gl.font_rid, gl.font_size); |
| 6876 | p_sd->ascent = MAX(p_sd->ascent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5)); |
| 6877 | p_sd->descent = MAX(p_sd->descent, Math::round(_font_get_glyph_advance(gl.font_rid, gl.font_size, gl.index).x * 0.5)); |
| 6878 | } |
| 6879 | double scale = _font_get_scale(gl.font_rid, fs); |
| 6880 | bool subpos = (scale != 1.0) || (_font_get_subpixel_positioning(gl.font_rid) == SUBPIXEL_POSITIONING_ONE_HALF) || (_font_get_subpixel_positioning(gl.font_rid) == SUBPIXEL_POSITIONING_ONE_QUARTER) || (_font_get_subpixel_positioning(gl.font_rid) == SUBPIXEL_POSITIONING_AUTO && fs <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE); |
| 6881 | if (!subpos) { |
| 6882 | gl.advance = Math::round(gl.advance); |
| 6883 | gl.x_off = Math::round(gl.x_off); |
| 6884 | } |
| 6885 | found = true; |
| 6886 | break; |
| 6887 | } |
| 6888 | } |
| 6889 | if (!found) { |
| 6890 | gl.font_rid = RID(); |
| 6891 | gl.index = p_sd->text[i]; |
| 6892 | if (p_sd->orientation == ORIENTATION_HORIZONTAL) { |
| 6893 | gl.advance = get_hex_code_box_size(fs, gl.index).x; |
| 6894 | p_sd->ascent = MAX(p_sd->ascent, get_hex_code_box_size(fs, gl.index).y * 0.85); |
| 6895 | p_sd->descent = MAX(p_sd->descent, get_hex_code_box_size(fs, gl.index).y * 0.15); |
| 6896 | } else { |
| 6897 | gl.advance = get_hex_code_box_size(fs, gl.index).y; |
| 6898 | gl.y_off = get_hex_code_box_size(fs, gl.index).y; |
| 6899 | gl.x_off = -Math::round(get_hex_code_box_size(fs, gl.index).x * 0.5); |
| 6900 | p_sd->ascent = MAX(p_sd->ascent, Math::round(get_hex_code_box_size(fs, gl.index).x * 0.5)); |
| 6901 | p_sd->descent = MAX(p_sd->descent, Math::round(get_hex_code_box_size(fs, gl.index).x * 0.5)); |
| 6902 | } |
| 6903 | } |
| 6904 | bool zero_w = (p_sd->preserve_control) ? (p_sd->text[i] == 0x200B || p_sd->text[i] == 0xFEFF) : ((p_sd->text[i] >= 0x200B && p_sd->text[i] <= 0x200D) || p_sd->text[i] == 0x2060 || p_sd->text[i] == 0xFEFF); |
| 6905 | if (zero_w) { |
| 6906 | gl.index = 0; |
| 6907 | gl.advance = 0.0; |
| 6908 | } |
| 6909 | |
| 6910 | p_sd->width += gl.advance; |
| 6911 | |
| 6912 | p_sd->glyphs.push_back(gl); |
| 6913 | } |
| 6914 | } |
| 6915 | return; |
| 6916 | } |
| 6917 | |
| 6918 | FontAdvanced *fd = _get_font_data(f); |
| 6919 | ERR_FAIL_NULL(fd); |
| 6920 | MutexLock lock(fd->mutex); |
| 6921 | bool color = false; |
| 6922 | |
| 6923 | Vector2i fss = _get_size(fd, fs); |
| 6924 | hb_font_t *hb_font = _font_get_hb_handle(f, fs, color); |
| 6925 | |
| 6926 | if (p_script == HB_TAG('Z', 's', 'y', 'e') && !color && _font_is_allow_system_fallback(p_fonts[0])) { |
| 6927 | // Color emoji is requested, skip non-color font. |
| 6928 | _shape_run(p_sd, p_start, p_end, p_language, p_script, p_direction, p_fonts, p_span, p_fb_index + 1, p_start, p_end, f); |
| 6929 | return; |
| 6930 | } |
| 6931 | |
| 6932 | double scale = _font_get_scale(f, fs); |
| 6933 | double sp_sp = p_sd->extra_spacing[SPACING_SPACE] + _font_get_spacing(f, SPACING_SPACE); |
| 6934 | double sp_gl = p_sd->extra_spacing[SPACING_GLYPH] + _font_get_spacing(f, SPACING_GLYPH); |
| 6935 | bool last_run = (p_sd->end == p_end); |
| 6936 | double ea = _get_extra_advance(f, fs); |
| 6937 | bool subpos = (scale != 1.0) || (_font_get_subpixel_positioning(f) == SUBPIXEL_POSITIONING_ONE_HALF) || (_font_get_subpixel_positioning(f) == SUBPIXEL_POSITIONING_ONE_QUARTER) || (_font_get_subpixel_positioning(f) == SUBPIXEL_POSITIONING_AUTO && fs <= SUBPIXEL_POSITIONING_ONE_HALF_MAX_SIZE); |
| 6938 | ERR_FAIL_NULL(hb_font); |
| 6939 | |
| 6940 | hb_buffer_clear_contents(p_sd->hb_buffer); |
| 6941 | hb_buffer_set_direction(p_sd->hb_buffer, p_direction); |
| 6942 | int flags = (p_start == 0 ? HB_BUFFER_FLAG_BOT : 0) | (p_end == p_sd->text.length() ? HB_BUFFER_FLAG_EOT : 0); |
| 6943 | if (p_sd->preserve_control) { |
| 6944 | flags |= HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES; |
| 6945 | } else { |
| 6946 | flags |= HB_BUFFER_FLAG_DEFAULT; |
| 6947 | } |
| 6948 | #if HB_VERSION_ATLEAST(5, 1, 0) |
| 6949 | flags |= HB_BUFFER_FLAG_PRODUCE_SAFE_TO_INSERT_TATWEEL; |
| 6950 | #endif |
| 6951 | hb_buffer_set_flags(p_sd->hb_buffer, (hb_buffer_flags_t)flags); |
| 6952 | hb_buffer_set_script(p_sd->hb_buffer, (p_script == HB_TAG('Z', 's', 'y', 'e')) ? HB_SCRIPT_COMMON : p_script); |
| 6953 | |
| 6954 | hb_language_t lang = hb_language_from_string(p_language.ascii().get_data(), -1); |
| 6955 | hb_buffer_set_language(p_sd->hb_buffer, lang); |
| 6956 | |
| 6957 | hb_buffer_add_utf32(p_sd->hb_buffer, (const uint32_t *)p_sd->text.ptr(), p_sd->text.length(), p_start, p_end - p_start); |
| 6958 | |
| 6959 | Vector<hb_feature_t> ftrs; |
| 6960 | _add_features(_font_get_opentype_feature_overrides(f), ftrs); |
| 6961 | _add_features(p_sd->spans[p_span].features, ftrs); |
| 6962 | |
| 6963 | hb_shape(hb_font, p_sd->hb_buffer, ftrs.is_empty() ? nullptr : &ftrs[0], ftrs.size()); |
| 6964 | |
| 6965 | unsigned int glyph_count = 0; |
| 6966 | hb_glyph_info_t *glyph_info = hb_buffer_get_glyph_infos(p_sd->hb_buffer, &glyph_count); |
| 6967 | hb_glyph_position_t *glyph_pos = hb_buffer_get_glyph_positions(p_sd->hb_buffer, &glyph_count); |
| 6968 | |
| 6969 | int mod = 0; |
| 6970 | if (fd->antialiasing == FONT_ANTIALIASING_LCD) { |
| 6971 | TextServer::FontLCDSubpixelLayout layout = lcd_subpixel_layout.get(); |
| 6972 | if (layout != FONT_LCD_SUBPIXEL_LAYOUT_NONE) { |
| 6973 | mod = (layout << 24); |
| 6974 | } |
| 6975 | } |
| 6976 | |
| 6977 | // Process glyphs. |
| 6978 | if (glyph_count > 0) { |
| 6979 | Glyph *w = (Glyph *)memalloc(glyph_count * sizeof(Glyph)); |
| 6980 | |
| 6981 | int end = (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) ? p_end : 0; |
| 6982 | uint32_t last_cluster_id = UINT32_MAX; |
| 6983 | unsigned int last_cluster_index = 0; |
| 6984 | bool last_cluster_valid = true; |
| 6985 | |
| 6986 | unsigned int last_non_zero_w = glyph_count - 1; |
| 6987 | if (last_run) { |
| 6988 | for (int64_t i = glyph_count - 1; i >= 0; i--) { |
| 6989 | last_non_zero_w = (unsigned int)i; |
| 6990 | if (p_sd->orientation == ORIENTATION_HORIZONTAL) { |
| 6991 | if (glyph_pos[i].x_advance != 0) { |
| 6992 | break; |
| 6993 | } |
| 6994 | } else { |
| 6995 | if (glyph_pos[i].y_advance != 0) { |
| 6996 | break; |
| 6997 | } |
| 6998 | } |
| 6999 | } |
| 7000 | } |
| 7001 | |
| 7002 | bool cache_valid = false; |
| 7003 | RID cached_font_rid = RID(); |
| 7004 | int cached_font_size = 0; |
| 7005 | float cached_offset = 0; |
| 7006 | |
| 7007 | double adv_rem = 0.0; |
| 7008 | for (unsigned int i = 0; i < glyph_count; i++) { |
| 7009 | if ((i > 0) && (last_cluster_id != glyph_info[i].cluster)) { |
| 7010 | if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) { |
| 7011 | end = w[last_cluster_index].start; |
| 7012 | } else { |
| 7013 | for (unsigned int j = last_cluster_index; j < i; j++) { |
| 7014 | w[j].end = glyph_info[i].cluster; |
| 7015 | } |
| 7016 | } |
| 7017 | if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) { |
| 7018 | w[last_cluster_index].flags |= GRAPHEME_IS_RTL; |
| 7019 | } |
| 7020 | if (last_cluster_valid) { |
| 7021 | w[last_cluster_index].flags |= GRAPHEME_IS_VALID; |
| 7022 | } |
| 7023 | w[last_cluster_index].count = i - last_cluster_index; |
| 7024 | last_cluster_index = i; |
| 7025 | last_cluster_valid = true; |
| 7026 | } |
| 7027 | |
| 7028 | last_cluster_id = glyph_info[i].cluster; |
| 7029 | |
| 7030 | Glyph &gl = w[i]; |
| 7031 | gl = Glyph(); |
| 7032 | |
| 7033 | gl.span_index = p_span; |
| 7034 | gl.start = glyph_info[i].cluster; |
| 7035 | gl.end = end; |
| 7036 | gl.count = 0; |
| 7037 | |
| 7038 | gl.font_rid = f; |
| 7039 | gl.font_size = fs; |
| 7040 | |
| 7041 | if (glyph_info[i].mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK) { |
| 7042 | gl.flags |= GRAPHEME_IS_CONNECTED; |
| 7043 | } |
| 7044 | |
| 7045 | #if HB_VERSION_ATLEAST(5, 1, 0) |
| 7046 | if (glyph_info[i].mask & HB_GLYPH_FLAG_SAFE_TO_INSERT_TATWEEL) { |
| 7047 | gl.flags |= GRAPHEME_IS_SAFE_TO_INSERT_TATWEEL; |
| 7048 | } |
| 7049 | #endif |
| 7050 | |
| 7051 | gl.index = glyph_info[i].codepoint; |
| 7052 | bool zero_w = (p_sd->preserve_control) ? (p_sd->text[glyph_info[i].cluster] == 0x200B || p_sd->text[glyph_info[i].cluster] == 0xFEFF) : ((p_sd->text[glyph_info[i].cluster] >= 0x200B && p_sd->text[glyph_info[i].cluster] <= 0x200D) || p_sd->text[glyph_info[i].cluster] == 0x2060 || p_sd->text[glyph_info[i].cluster] == 0xFEFF); |
| 7053 | if (zero_w) { |
| 7054 | gl.index = 0; |
| 7055 | gl.advance = 0.0; |
| 7056 | } |
| 7057 | if ((p_sd->text[glyph_info[i].cluster] == 0x0009) || u_isblank(p_sd->text[glyph_info[i].cluster]) || is_linebreak(p_sd->text[glyph_info[i].cluster])) { |
| 7058 | adv_rem = 0.0; // Reset on blank. |
| 7059 | } |
| 7060 | if (gl.index != 0) { |
| 7061 | FontGlyph fgl; |
| 7062 | _ensure_glyph(fd, fss, gl.index | mod, fgl); |
| 7063 | if (subpos) { |
| 7064 | gl.x_off = (double)glyph_pos[i].x_offset / (64.0 / scale); |
| 7065 | } else if (p_sd->orientation == ORIENTATION_HORIZONTAL) { |
| 7066 | gl.x_off = Math::round(adv_rem + ((double)glyph_pos[i].x_offset / (64.0 / scale))); |
| 7067 | } else { |
| 7068 | gl.x_off = Math::round((double)glyph_pos[i].x_offset / (64.0 / scale)); |
| 7069 | } |
| 7070 | if (p_sd->orientation == ORIENTATION_HORIZONTAL) { |
| 7071 | gl.y_off = -Math::round((double)glyph_pos[i].y_offset / (64.0 / scale)); |
| 7072 | } else { |
| 7073 | gl.y_off = -Math::round(adv_rem + ((double)glyph_pos[i].y_offset / (64.0 / scale))); |
| 7074 | } |
| 7075 | if (p_sd->orientation == ORIENTATION_HORIZONTAL) { |
| 7076 | if (subpos) { |
| 7077 | gl.advance = (double)glyph_pos[i].x_advance / (64.0 / scale) + ea; |
| 7078 | } else { |
| 7079 | double full_adv = adv_rem + ((double)glyph_pos[i].x_advance / (64.0 / scale) + ea); |
| 7080 | gl.advance = Math::round(full_adv); |
| 7081 | if (fd->keep_rounding_remainders) { |
| 7082 | adv_rem = full_adv - gl.advance; |
| 7083 | } |
| 7084 | } |
| 7085 | } else { |
| 7086 | double full_adv = adv_rem + ((double)glyph_pos[i].y_advance / (64.0 / scale)); |
| 7087 | gl.advance = -Math::round(full_adv); |
| 7088 | if (fd->keep_rounding_remainders) { |
| 7089 | adv_rem = full_adv + gl.advance; |
| 7090 | } |
| 7091 | } |
| 7092 | if (!cache_valid || cached_font_rid != gl.font_rid || cached_font_size != gl.font_size) { |
| 7093 | cache_valid = true; |
| 7094 | cached_font_size = gl.font_size; |
| 7095 | cached_font_rid = gl.font_rid; |
| 7096 | cached_offset = _font_get_baseline_offset(gl.font_rid) * (double)(_font_get_ascent(gl.font_rid, gl.font_size) + _font_get_descent(gl.font_rid, gl.font_size)); |
| 7097 | } |
| 7098 | if (p_sd->orientation == ORIENTATION_HORIZONTAL) { |
| 7099 | gl.y_off += cached_offset; |
| 7100 | } else { |
| 7101 | gl.x_off += cached_offset; |
| 7102 | } |
| 7103 | } |
| 7104 | if ((!last_run || i < last_non_zero_w) && !Math::is_zero_approx(gl.advance)) { |
| 7105 | // Do not add extra spacing to the last glyph of the string and zero width glyphs. |
| 7106 | if (sp_sp && is_whitespace(p_sd->text[glyph_info[i].cluster])) { |
| 7107 | gl.advance += sp_sp; |
| 7108 | } else { |
| 7109 | gl.advance += sp_gl; |
| 7110 | } |
| 7111 | } |
| 7112 | |
| 7113 | if (p_sd->preserve_control) { |
| 7114 | last_cluster_valid = last_cluster_valid && ((glyph_info[i].codepoint != 0) || zero_w || (p_sd->text[glyph_info[i].cluster] == 0x0009) || (u_isblank(p_sd->text[glyph_info[i].cluster]) && (gl.advance != 0)) || (!u_isblank(p_sd->text[glyph_info[i].cluster]) && is_linebreak(p_sd->text[glyph_info[i].cluster]))); |
| 7115 | } else { |
| 7116 | last_cluster_valid = last_cluster_valid && ((glyph_info[i].codepoint != 0) || zero_w || (p_sd->text[glyph_info[i].cluster] == 0x0009) || (u_isblank(p_sd->text[glyph_info[i].cluster]) && (gl.advance != 0)) || (!u_isblank(p_sd->text[glyph_info[i].cluster]) && !u_isgraph(p_sd->text[glyph_info[i].cluster]))); |
| 7117 | } |
| 7118 | } |
| 7119 | if (p_direction == HB_DIRECTION_LTR || p_direction == HB_DIRECTION_TTB) { |
| 7120 | for (unsigned int j = last_cluster_index; j < glyph_count; j++) { |
| 7121 | w[j].end = p_end; |
| 7122 | } |
| 7123 | } |
| 7124 | w[last_cluster_index].count = glyph_count - last_cluster_index; |
| 7125 | if (p_direction == HB_DIRECTION_RTL || p_direction == HB_DIRECTION_BTT) { |
| 7126 | w[last_cluster_index].flags |= GRAPHEME_IS_RTL; |
| 7127 | } |
| 7128 | if (last_cluster_valid) { |
| 7129 | w[last_cluster_index].flags |= GRAPHEME_IS_VALID; |
| 7130 | } |
| 7131 | |
| 7132 | // Fallback. |
| 7133 | int failed_subrun_start = p_end + 1; |
| 7134 | int failed_subrun_end = p_start; |
| 7135 | |
| 7136 | for (unsigned int i = 0; i < glyph_count; i++) { |
| 7137 | if ((w[i].flags & GRAPHEME_IS_VALID) == GRAPHEME_IS_VALID) { |
| 7138 | if (failed_subrun_start != p_end + 1) { |
| 7139 | _shape_run(p_sd, failed_subrun_start, failed_subrun_end, p_language, p_script, p_direction, p_fonts, p_span, p_fb_index + 1, p_start, p_end, (p_fb_index >= p_fonts.size()) ? f : RID()); |
| 7140 | failed_subrun_start = p_end + 1; |
| 7141 | failed_subrun_end = p_start; |
| 7142 | } |
| 7143 | p_sd->glyphs.reserve(p_sd->glyphs.size() + w[i].count); |
| 7144 | for (int j = 0; j < w[i].count; j++) { |
| 7145 | if (p_sd->orientation == ORIENTATION_HORIZONTAL) { |
| 7146 | p_sd->ascent = MAX(p_sd->ascent, -w[i + j].y_off); |
| 7147 | p_sd->descent = MAX(p_sd->descent, w[i + j].y_off); |
| 7148 | } else { |
| 7149 | double gla = Math::round(_font_get_glyph_advance(f, fs, w[i + j].index).x * 0.5); |
| 7150 | p_sd->ascent = MAX(p_sd->ascent, gla); |
| 7151 | p_sd->descent = MAX(p_sd->descent, gla); |
| 7152 | } |
| 7153 | p_sd->width += w[i + j].advance; |
| 7154 | w[i + j].start += p_sd->start; |
| 7155 | w[i + j].end += p_sd->start; |
| 7156 | p_sd->glyphs.push_back(w[i + j]); |
| 7157 | } |
| 7158 | } else { |
| 7159 | if (failed_subrun_start >= w[i].start) { |
| 7160 | failed_subrun_start = w[i].start; |
| 7161 | } |
| 7162 | if (failed_subrun_end <= w[i].end) { |
| 7163 | failed_subrun_end = w[i].end; |
| 7164 | } |
| 7165 | } |
| 7166 | i += w[i].count - 1; |
| 7167 | } |
| 7168 | memfree(w); |
| 7169 | if (failed_subrun_start != p_end + 1) { |
| 7170 | _shape_run(p_sd, failed_subrun_start, failed_subrun_end, p_language, p_script, p_direction, p_fonts, p_span, p_fb_index + 1, p_start, p_end, (p_fb_index >= p_fonts.size()) ? f : RID()); |
| 7171 | } |
| 7172 | p_sd->ascent = MAX(p_sd->ascent, _font_get_ascent(f, fs) + _font_get_spacing(f, SPACING_TOP)); |
| 7173 | p_sd->descent = MAX(p_sd->descent, _font_get_descent(f, fs) + _font_get_spacing(f, SPACING_BOTTOM)); |
| 7174 | p_sd->upos = MAX(p_sd->upos, _font_get_underline_position(f, fs)); |
| 7175 | p_sd->uthk = MAX(p_sd->uthk, _font_get_underline_thickness(f, fs)); |
| 7176 | } else if (p_start != p_end) { |
| 7177 | if (p_fb_index >= p_fonts.size()) { |
| 7178 | Glyph gl; |
| 7179 | gl.start = p_start; |
| 7180 | gl.end = p_end; |
| 7181 | gl.span_index = p_span; |
| 7182 | gl.font_rid = f; |
| 7183 | gl.font_size = fs; |
| 7184 | gl.flags = GRAPHEME_IS_VALID; |
| 7185 | p_sd->glyphs.push_back(gl); |
| 7186 | |
| 7187 | p_sd->ascent = MAX(p_sd->ascent, _font_get_ascent(f, fs) + _font_get_spacing(f, SPACING_TOP)); |
| 7188 | p_sd->descent = MAX(p_sd->descent, _font_get_descent(f, fs) + _font_get_spacing(f, SPACING_BOTTOM)); |
| 7189 | p_sd->upos = MAX(p_sd->upos, _font_get_underline_position(f, fs)); |
| 7190 | p_sd->uthk = MAX(p_sd->uthk, _font_get_underline_thickness(f, fs)); |
| 7191 | } else { |
| 7192 | _shape_run(p_sd, p_start, p_end, p_language, p_script, p_direction, p_fonts, p_span, p_fb_index + 1, p_start, p_end, f); |
| 7193 | } |
| 7194 | } |
| 7195 | } |
| 7196 | |
| 7197 | bool TextServerAdvanced::_shaped_text_shape(const RID &p_shaped) { |
| 7198 | _THREAD_SAFE_METHOD_ |
| 7199 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 7200 | ERR_FAIL_NULL_V(sd, false); |
| 7201 | |
| 7202 | MutexLock lock(sd->mutex); |
| 7203 | if (sd->valid.is_set()) { |
| 7204 | return true; |
| 7205 | } |
| 7206 | |
| 7207 | invalidate(sd, false); |
| 7208 | if (sd->parent != RID()) { |
| 7209 | _shaped_text_shape(sd->parent); |
| 7210 | ShapedTextDataAdvanced *parent_sd = shaped_owner.get_or_null(sd->parent); |
| 7211 | ERR_FAIL_COND_V(!parent_sd->valid.is_set(), false); |
| 7212 | ERR_FAIL_COND_V(!_shape_substr(sd, parent_sd, sd->start, sd->end - sd->start), false); |
| 7213 | return true; |
| 7214 | } |
| 7215 | |
| 7216 | if (sd->text.length() == 0) { |
| 7217 | sd->valid.set(); |
| 7218 | return true; |
| 7219 | } |
| 7220 | |
| 7221 | const String &project_locale = TranslationServer::get_singleton()->get_tool_locale(); |
| 7222 | |
| 7223 | sd->utf16 = sd->text.utf16(); |
| 7224 | const UChar *data = sd->utf16.get_data(); |
| 7225 | |
| 7226 | // Create script iterator. |
| 7227 | if (sd->script_iter == nullptr) { |
| 7228 | sd->script_iter = memnew(ScriptIterator(sd->text, 0, sd->text.length())); |
| 7229 | } |
| 7230 | |
| 7231 | sd->base_para_direction = UBIDI_DEFAULT_LTR; |
| 7232 | switch (sd->direction) { |
| 7233 | case DIRECTION_LTR: { |
| 7234 | sd->para_direction = DIRECTION_LTR; |
| 7235 | sd->base_para_direction = UBIDI_LTR; |
| 7236 | } break; |
| 7237 | case DIRECTION_RTL: { |
| 7238 | sd->para_direction = DIRECTION_RTL; |
| 7239 | sd->base_para_direction = UBIDI_RTL; |
| 7240 | } break; |
| 7241 | case DIRECTION_INHERITED: |
| 7242 | case DIRECTION_AUTO: { |
| 7243 | UBiDiDirection direction = ubidi_getBaseDirection(data, sd->utf16.length()); |
| 7244 | if (direction != UBIDI_NEUTRAL) { |
| 7245 | sd->para_direction = (direction == UBIDI_RTL) ? DIRECTION_RTL : DIRECTION_LTR; |
| 7246 | sd->base_para_direction = direction; |
| 7247 | } else { |
| 7248 | const String &lang = (sd->spans.is_empty() || sd->spans[0].language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : sd->spans[0].language; |
| 7249 | bool lang_rtl = _is_locale_right_to_left(lang); |
| 7250 | |
| 7251 | sd->para_direction = lang_rtl ? DIRECTION_RTL : DIRECTION_LTR; |
| 7252 | sd->base_para_direction = lang_rtl ? UBIDI_DEFAULT_RTL : UBIDI_DEFAULT_LTR; |
| 7253 | } |
| 7254 | } break; |
| 7255 | } |
| 7256 | |
| 7257 | Vector<Vector3i> bidi_ranges; |
| 7258 | if (sd->bidi_override.is_empty()) { |
| 7259 | bidi_ranges.push_back(Vector3i(sd->start, sd->end, DIRECTION_INHERITED)); |
| 7260 | } else { |
| 7261 | bidi_ranges = sd->bidi_override; |
| 7262 | } |
| 7263 | sd->runs.clear(); |
| 7264 | sd->runs_dirty = true; |
| 7265 | |
| 7266 | for (int ov = 0; ov < bidi_ranges.size(); ov++) { |
| 7267 | // Create BiDi iterator. |
| 7268 | int start = _convert_pos_inv(sd, bidi_ranges[ov].x - sd->start); |
| 7269 | int end = _convert_pos_inv(sd, bidi_ranges[ov].y - sd->start); |
| 7270 | |
| 7271 | if (start < 0 || end - start > sd->utf16.length()) { |
| 7272 | continue; |
| 7273 | } |
| 7274 | |
| 7275 | UErrorCode err = U_ZERO_ERROR; |
| 7276 | UBiDi *bidi_iter = ubidi_openSized(end - start, 0, &err); |
| 7277 | if (U_SUCCESS(err)) { |
| 7278 | switch (static_cast<TextServer::Direction>(bidi_ranges[ov].z)) { |
| 7279 | case DIRECTION_LTR: { |
| 7280 | ubidi_setPara(bidi_iter, data + start, end - start, UBIDI_LTR, nullptr, &err); |
| 7281 | } break; |
| 7282 | case DIRECTION_RTL: { |
| 7283 | ubidi_setPara(bidi_iter, data + start, end - start, UBIDI_RTL, nullptr, &err); |
| 7284 | } break; |
| 7285 | case DIRECTION_INHERITED: { |
| 7286 | ubidi_setPara(bidi_iter, data + start, end - start, sd->base_para_direction, nullptr, &err); |
| 7287 | } break; |
| 7288 | case DIRECTION_AUTO: { |
| 7289 | UBiDiDirection direction = ubidi_getBaseDirection(data + start, end - start); |
| 7290 | if (direction != UBIDI_NEUTRAL) { |
| 7291 | ubidi_setPara(bidi_iter, data + start, end - start, direction, nullptr, &err); |
| 7292 | } else { |
| 7293 | ubidi_setPara(bidi_iter, data + start, end - start, sd->base_para_direction, nullptr, &err); |
| 7294 | } |
| 7295 | } break; |
| 7296 | } |
| 7297 | if (U_FAILURE(err)) { |
| 7298 | ubidi_close(bidi_iter); |
| 7299 | bidi_iter = nullptr; |
| 7300 | ERR_PRINT(vformat("BiDi reordering for the paragraph failed: %s", u_errorName(err))); |
| 7301 | } |
| 7302 | } else { |
| 7303 | bidi_iter = nullptr; |
| 7304 | ERR_PRINT(vformat("BiDi iterator allocation for the paragraph failed: %s", u_errorName(err))); |
| 7305 | } |
| 7306 | sd->bidi_iter.push_back(bidi_iter); |
| 7307 | |
| 7308 | err = U_ZERO_ERROR; |
| 7309 | int bidi_run_count = 1; |
| 7310 | if (bidi_iter) { |
| 7311 | bidi_run_count = ubidi_countRuns(bidi_iter, &err); |
| 7312 | if (U_FAILURE(err)) { |
| 7313 | ERR_PRINT(u_errorName(err)); |
| 7314 | } |
| 7315 | } |
| 7316 | for (int i = 0; i < bidi_run_count; i++) { |
| 7317 | int32_t _bidi_run_start = 0; |
| 7318 | int32_t _bidi_run_length = end - start; |
| 7319 | bool is_ltr = false; |
| 7320 | hb_direction_t bidi_run_direction = HB_DIRECTION_INVALID; |
| 7321 | if (bidi_iter) { |
| 7322 | is_ltr = (ubidi_getVisualRun(bidi_iter, i, &_bidi_run_start, &_bidi_run_length) == UBIDI_LTR); |
| 7323 | } |
| 7324 | switch (sd->orientation) { |
| 7325 | case ORIENTATION_HORIZONTAL: { |
| 7326 | if (is_ltr) { |
| 7327 | bidi_run_direction = HB_DIRECTION_LTR; |
| 7328 | } else { |
| 7329 | bidi_run_direction = HB_DIRECTION_RTL; |
| 7330 | } |
| 7331 | } break; |
| 7332 | case ORIENTATION_VERTICAL: { |
| 7333 | if (is_ltr) { |
| 7334 | bidi_run_direction = HB_DIRECTION_TTB; |
| 7335 | } else { |
| 7336 | bidi_run_direction = HB_DIRECTION_BTT; |
| 7337 | } |
| 7338 | } |
| 7339 | } |
| 7340 | |
| 7341 | int32_t bidi_run_start = _convert_pos(sd, start + _bidi_run_start); |
| 7342 | int32_t bidi_run_end = _convert_pos(sd, start + _bidi_run_start + _bidi_run_length); |
| 7343 | |
| 7344 | // Shape runs. |
| 7345 | |
| 7346 | int scr_from = (is_ltr) ? 0 : sd->script_iter->script_ranges.size() - 1; |
| 7347 | int scr_to = (is_ltr) ? sd->script_iter->script_ranges.size() : -1; |
| 7348 | int scr_delta = (is_ltr) ? +1 : -1; |
| 7349 | |
| 7350 | for (int j = scr_from; j != scr_to; j += scr_delta) { |
| 7351 | if ((sd->script_iter->script_ranges[j].start < bidi_run_end) && (sd->script_iter->script_ranges[j].end > bidi_run_start)) { |
| 7352 | int32_t script_run_start = MAX(sd->script_iter->script_ranges[j].start, bidi_run_start); |
| 7353 | int32_t script_run_end = MIN(sd->script_iter->script_ranges[j].end, bidi_run_end); |
| 7354 | char scr_buffer[5] = { 0, 0, 0, 0, 0 }; |
| 7355 | hb_tag_to_string(hb_script_to_iso15924_tag(sd->script_iter->script_ranges[j].script), scr_buffer); |
| 7356 | String script_code = String(scr_buffer); |
| 7357 | |
| 7358 | int spn_from = (is_ltr) ? 0 : sd->spans.size() - 1; |
| 7359 | int spn_to = (is_ltr) ? sd->spans.size() : -1; |
| 7360 | int spn_delta = (is_ltr) ? +1 : -1; |
| 7361 | |
| 7362 | for (int k = spn_from; k != spn_to; k += spn_delta) { |
| 7363 | const ShapedTextDataAdvanced::Span &span = sd->spans[k]; |
| 7364 | int col_key_off = (span.start == span.end) ? 1 : 0; |
| 7365 | if (span.start - sd->start >= script_run_end || span.end - sd->start <= script_run_start - col_key_off) { |
| 7366 | continue; |
| 7367 | } |
| 7368 | if (span.embedded_key != Variant()) { |
| 7369 | // Embedded object. |
| 7370 | if (sd->orientation == ORIENTATION_HORIZONTAL) { |
| 7371 | sd->objects[span.embedded_key].rect.position.x = sd->width; |
| 7372 | sd->width += sd->objects[span.embedded_key].rect.size.x; |
| 7373 | } else { |
| 7374 | sd->objects[span.embedded_key].rect.position.y = sd->width; |
| 7375 | sd->width += sd->objects[span.embedded_key].rect.size.y; |
| 7376 | } |
| 7377 | Glyph gl; |
| 7378 | gl.start = span.start; |
| 7379 | gl.end = span.end; |
| 7380 | gl.count = 1; |
| 7381 | gl.span_index = k; |
| 7382 | gl.flags = GRAPHEME_IS_VALID | GRAPHEME_IS_EMBEDDED_OBJECT; |
| 7383 | if (sd->orientation == ORIENTATION_HORIZONTAL) { |
| 7384 | gl.advance = sd->objects[span.embedded_key].rect.size.x; |
| 7385 | } else { |
| 7386 | gl.advance = sd->objects[span.embedded_key].rect.size.y; |
| 7387 | } |
| 7388 | sd->glyphs.push_back(gl); |
| 7389 | } else { |
| 7390 | // Select best matching language for the run. |
| 7391 | String language = span.language; |
| 7392 | if (!language.contains("force")) { |
| 7393 | if (language.is_empty() || !TranslationServer::get_singleton()->is_script_suppored_by_locale(language, script_code)) { |
| 7394 | language = project_locale; |
| 7395 | if (language.is_empty() || !TranslationServer::get_singleton()->is_script_suppored_by_locale(language, script_code)) { |
| 7396 | language = os_locale; |
| 7397 | } |
| 7398 | } |
| 7399 | } |
| 7400 | FontPriorityList fonts(this, span.fonts, language.left(3).remove_char('_'), script_code, sd->script_iter->script_ranges[j].script == HB_TAG('Z', 's', 'y', 'e')); |
| 7401 | _shape_run(sd, MAX(span.start - sd->start, script_run_start), MIN(span.end - sd->start, script_run_end), language, sd->script_iter->script_ranges[j].script, bidi_run_direction, fonts, k, 0, 0, 0, RID()); |
| 7402 | } |
| 7403 | } |
| 7404 | } |
| 7405 | } |
| 7406 | } |
| 7407 | } |
| 7408 | |
| 7409 | _realign(sd); |
| 7410 | sd->valid.set(); |
| 7411 | return sd->valid.is_set(); |
| 7412 | } |
| 7413 | |
| 7414 | bool TextServerAdvanced::_shaped_text_is_ready(const RID &p_shaped) const { |
| 7415 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 7416 | ERR_FAIL_NULL_V(sd, false); |
| 7417 | |
| 7418 | // Atomic read is safe and faster. |
| 7419 | return sd->valid.is_set(); |
| 7420 | } |
| 7421 | |
| 7422 | const Glyph *TextServerAdvanced::_shaped_text_get_glyphs(const RID &p_shaped) const { |
| 7423 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 7424 | ERR_FAIL_NULL_V(sd, nullptr); |
| 7425 | |
| 7426 | MutexLock lock(sd->mutex); |
| 7427 | if (!sd->valid.is_set()) { |
| 7428 | const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped); |
| 7429 | } |
| 7430 | return sd->glyphs.ptr(); |
| 7431 | } |
| 7432 | |
| 7433 | int64_t TextServerAdvanced::_shaped_text_get_glyph_count(const RID &p_shaped) const { |
| 7434 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 7435 | ERR_FAIL_NULL_V(sd, 0); |
| 7436 | |
| 7437 | MutexLock lock(sd->mutex); |
| 7438 | if (!sd->valid.is_set()) { |
| 7439 | const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped); |
| 7440 | } |
| 7441 | return sd->glyphs.size(); |
| 7442 | } |
| 7443 | |
| 7444 | const Glyph *TextServerAdvanced::_shaped_text_sort_logical(const RID &p_shaped) { |
| 7445 | ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 7446 | ERR_FAIL_NULL_V(sd, nullptr); |
| 7447 | |
| 7448 | MutexLock lock(sd->mutex); |
| 7449 | if (!sd->valid.is_set()) { |
| 7450 | _shaped_text_shape(p_shaped); |
| 7451 | } |
| 7452 | |
| 7453 | if (!sd->sort_valid) { |
| 7454 | sd->glyphs_logical = sd->glyphs; |
| 7455 | sd->glyphs_logical.sort_custom<GlyphCompare>(); |
| 7456 | sd->sort_valid = true; |
| 7457 | } |
| 7458 | |
| 7459 | return sd->glyphs_logical.ptr(); |
| 7460 | } |
| 7461 | |
| 7462 | Vector2i TextServerAdvanced::_shaped_text_get_range(const RID &p_shaped) const { |
| 7463 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 7464 | ERR_FAIL_NULL_V(sd, Vector2i()); |
| 7465 | |
| 7466 | MutexLock lock(sd->mutex); |
| 7467 | return Vector2(sd->start, sd->end); |
| 7468 | } |
| 7469 | |
| 7470 | Array TextServerAdvanced::_shaped_text_get_objects(const RID &p_shaped) const { |
| 7471 | Array ret; |
| 7472 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 7473 | ERR_FAIL_NULL_V(sd, ret); |
| 7474 | |
| 7475 | MutexLock lock(sd->mutex); |
| 7476 | for (const KeyValue<Variant, ShapedTextDataAdvanced::EmbeddedObject> &E : sd->objects) { |
| 7477 | ret.push_back(E.key); |
| 7478 | } |
| 7479 | |
| 7480 | return ret; |
| 7481 | } |
| 7482 | |
| 7483 | Rect2 TextServerAdvanced::_shaped_text_get_object_rect(const RID &p_shaped, const Variant &p_key) const { |
| 7484 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 7485 | ERR_FAIL_NULL_V(sd, Rect2()); |
| 7486 | |
| 7487 | MutexLock lock(sd->mutex); |
| 7488 | ERR_FAIL_COND_V(!sd->objects.has(p_key), Rect2()); |
| 7489 | if (!sd->valid.is_set()) { |
| 7490 | const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped); |
| 7491 | } |
| 7492 | return sd->objects[p_key].rect; |
| 7493 | } |
| 7494 | |
| 7495 | Vector2i TextServerAdvanced::_shaped_text_get_object_range(const RID &p_shaped, const Variant &p_key) const { |
| 7496 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 7497 | ERR_FAIL_NULL_V(sd, Vector2i()); |
| 7498 | |
| 7499 | MutexLock lock(sd->mutex); |
| 7500 | ERR_FAIL_COND_V(!sd->objects.has(p_key), Vector2i()); |
| 7501 | return Vector2i(sd->objects[p_key].start, sd->objects[p_key].end); |
| 7502 | } |
| 7503 | |
| 7504 | int64_t TextServerAdvanced::_shaped_text_get_object_glyph(const RID &p_shaped, const Variant &p_key) const { |
| 7505 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 7506 | ERR_FAIL_NULL_V(sd, -1); |
| 7507 | |
| 7508 | MutexLock lock(sd->mutex); |
| 7509 | ERR_FAIL_COND_V(!sd->objects.has(p_key), -1); |
| 7510 | if (!sd->valid.is_set()) { |
| 7511 | const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped); |
| 7512 | } |
| 7513 | const ShapedTextDataAdvanced::EmbeddedObject &obj = sd->objects[p_key]; |
| 7514 | int sd_size = sd->glyphs.size(); |
| 7515 | const Glyph *sd_glyphs = sd->glyphs.ptr(); |
| 7516 | for (int i = 0; i < sd_size; i++) { |
| 7517 | if (obj.start == sd_glyphs[i].start) { |
| 7518 | return i; |
| 7519 | } |
| 7520 | } |
| 7521 | return -1; |
| 7522 | } |
| 7523 | |
| 7524 | Size2 TextServerAdvanced::_shaped_text_get_size(const RID &p_shaped) const { |
| 7525 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 7526 | ERR_FAIL_NULL_V(sd, Size2()); |
| 7527 | |
| 7528 | MutexLock lock(sd->mutex); |
| 7529 | if (!sd->valid.is_set()) { |
| 7530 | const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped); |
| 7531 | } |
| 7532 | if (sd->orientation == TextServer::ORIENTATION_HORIZONTAL) { |
| 7533 | return Size2((sd->text_trimmed ? sd->width_trimmed : sd->width), sd->ascent + sd->descent + sd->extra_spacing[SPACING_TOP] + sd->extra_spacing[SPACING_BOTTOM]).ceil(); |
| 7534 | } else { |
| 7535 | return Size2(sd->ascent + sd->descent + sd->extra_spacing[SPACING_TOP] + sd->extra_spacing[SPACING_BOTTOM], (sd->text_trimmed ? sd->width_trimmed : sd->width)).ceil(); |
| 7536 | } |
| 7537 | } |
| 7538 | |
| 7539 | double TextServerAdvanced::_shaped_text_get_ascent(const RID &p_shaped) const { |
| 7540 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 7541 | ERR_FAIL_NULL_V(sd, 0.0); |
| 7542 | |
| 7543 | MutexLock lock(sd->mutex); |
| 7544 | if (!sd->valid.is_set()) { |
| 7545 | const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped); |
| 7546 | } |
| 7547 | return sd->ascent + sd->extra_spacing[SPACING_TOP]; |
| 7548 | } |
| 7549 | |
| 7550 | double TextServerAdvanced::_shaped_text_get_descent(const RID &p_shaped) const { |
| 7551 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 7552 | ERR_FAIL_NULL_V(sd, 0.0); |
| 7553 | |
| 7554 | MutexLock lock(sd->mutex); |
| 7555 | if (!sd->valid.is_set()) { |
| 7556 | const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped); |
| 7557 | } |
| 7558 | return sd->descent + sd->extra_spacing[SPACING_BOTTOM]; |
| 7559 | } |
| 7560 | |
| 7561 | double TextServerAdvanced::_shaped_text_get_width(const RID &p_shaped) const { |
| 7562 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 7563 | ERR_FAIL_NULL_V(sd, 0.0); |
| 7564 | |
| 7565 | MutexLock lock(sd->mutex); |
| 7566 | if (!sd->valid.is_set()) { |
| 7567 | const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped); |
| 7568 | } |
| 7569 | return Math::ceil(sd->text_trimmed ? sd->width_trimmed : sd->width); |
| 7570 | } |
| 7571 | |
| 7572 | double TextServerAdvanced::_shaped_text_get_underline_position(const RID &p_shaped) const { |
| 7573 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 7574 | ERR_FAIL_NULL_V(sd, 0.0); |
| 7575 | |
| 7576 | MutexLock lock(sd->mutex); |
| 7577 | if (!sd->valid.is_set()) { |
| 7578 | const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped); |
| 7579 | } |
| 7580 | |
| 7581 | return sd->upos; |
| 7582 | } |
| 7583 | |
| 7584 | double TextServerAdvanced::_shaped_text_get_underline_thickness(const RID &p_shaped) const { |
| 7585 | const ShapedTextDataAdvanced *sd = shaped_owner.get_or_null(p_shaped); |
| 7586 | ERR_FAIL_NULL_V(sd, 0.0); |
| 7587 | |
| 7588 | MutexLock lock(sd->mutex); |
| 7589 | if (!sd->valid.is_set()) { |
| 7590 | const_cast<TextServerAdvanced *>(this)->_shaped_text_shape(p_shaped); |
| 7591 | } |
| 7592 | |
| 7593 | return sd->uthk; |
| 7594 | } |
| 7595 | |
| 7596 | int64_t TextServerAdvanced::_is_confusable(const String &p_string, const PackedStringArray &p_dict) const { |
| 7597 | #ifndef ICU_STATIC_DATA |
| 7598 | if (!icu_data_loaded) { |
| 7599 | return -1; |
| 7600 | } |
| 7601 | #endif |
| 7602 | UErrorCode status = U_ZERO_ERROR; |
| 7603 | int64_t match_index = -1; |
| 7604 | |
| 7605 | Char16String utf16 = p_string.utf16(); |
| 7606 | Vector<UChar *> skeletons; |
| 7607 | skeletons.resize(p_dict.size()); |
| 7608 | |
| 7609 | if (sc_conf == nullptr) { |
| 7610 | sc_conf = uspoof_open(&status); |
| 7611 | uspoof_setChecks(sc_conf, USPOOF_CONFUSABLE, &status); |
| 7612 | } |
| 7613 | for (int i = 0; i < p_dict.size(); i++) { |
| 7614 | Char16String word = p_dict[i].utf16(); |
| 7615 | int32_t len = uspoof_getSkeleton(sc_conf, 0, word.get_data(), -1, nullptr, 0, &status); |
| 7616 | skeletons.write[i] = (UChar *)memalloc(++len * sizeof(UChar)); |
| 7617 | status = U_ZERO_ERROR; |
| 7618 | uspoof_getSkeleton(sc_conf, 0, word.get_data(), -1, skeletons.write[i], len, &status); |
| 7619 | } |
| 7620 | |
| 7621 | int32_t len = uspoof_getSkeleton(sc_conf, 0, utf16.get_data(), -1, nullptr, 0, &status); |
| 7622 | UChar *skel = (UChar *)memalloc(++len * sizeof(UChar)); |
| 7623 | status = U_ZERO_ERROR; |
| 7624 | uspoof_getSkeleton(sc_conf, 0, utf16.get_data(), -1, skel, len, &status); |
| 7625 | for (int i = 0; i < skeletons.size(); i++) { |
| 7626 | if (u_strcmp(skel, skeletons[i]) == 0) { |
| 7627 | match_index = i; |
| 7628 | break; |
| 7629 | } |
| 7630 | } |
| 7631 | memfree(skel); |
| 7632 | |
| 7633 | for (int i = 0; i < skeletons.size(); i++) { |
| 7634 | memfree(skeletons.write[i]); |
| 7635 | } |
| 7636 | |
| 7637 | ERR_FAIL_COND_V_MSG(U_FAILURE(status), -1, u_errorName(status)); |
| 7638 | |
| 7639 | return match_index; |
| 7640 | } |
| 7641 | |
| 7642 | bool TextServerAdvanced::_spoof_check(const String &p_string) const { |
| 7643 | #ifndef ICU_STATIC_DATA |
| 7644 | if (!icu_data_loaded) { |
| 7645 | return false; |
| 7646 | } |
| 7647 | #endif |
| 7648 | UErrorCode status = U_ZERO_ERROR; |
| 7649 | Char16String utf16 = p_string.utf16(); |
| 7650 | |
| 7651 | if (allowed == nullptr) { |
| 7652 | allowed = uset_openEmpty(); |
| 7653 | uset_addAll(allowed, uspoof_getRecommendedSet(&status)); |
| 7654 | uset_addAll(allowed, uspoof_getInclusionSet(&status)); |
| 7655 | } |
| 7656 | if (sc_spoof == nullptr) { |
| 7657 | sc_spoof = uspoof_open(&status); |
| 7658 | uspoof_setAllowedChars(sc_spoof, allowed, &status); |
| 7659 | uspoof_setRestrictionLevel(sc_spoof, USPOOF_MODERATELY_RESTRICTIVE); |
| 7660 | } |
| 7661 | |
| 7662 | int32_t bitmask = uspoof_check(sc_spoof, utf16.get_data(), -1, nullptr, &status); |
| 7663 | ERR_FAIL_COND_V_MSG(U_FAILURE(status), false, u_errorName(status)); |
| 7664 | |
| 7665 | return (bitmask != 0); |
| 7666 | } |
| 7667 | |
| 7668 | String TextServerAdvanced::_strip_diacritics(const String &p_string) const { |
| 7669 | #ifndef ICU_STATIC_DATA |
| 7670 | if (!icu_data_loaded) { |
| 7671 | return TextServer::strip_diacritics(p_string); |
| 7672 | } |
| 7673 | #endif |
| 7674 | UErrorCode err = U_ZERO_ERROR; |
| 7675 | |
| 7676 | // Get NFKD normalizer singleton. |
| 7677 | const UNormalizer2 *unorm = unorm2_getNFKDInstance(&err); |
| 7678 | ERR_FAIL_COND_V_MSG(U_FAILURE(err), TextServer::strip_diacritics(p_string), u_errorName(err)); |
| 7679 | |
| 7680 | // Convert to UTF-16. |
| 7681 | Char16String utf16 = p_string.utf16(); |
| 7682 | |
| 7683 | // Normalize. |
| 7684 | Vector<char16_t> normalized; |
| 7685 | err = U_ZERO_ERROR; |
| 7686 | int32_t len = unorm2_normalize(unorm, utf16.get_data(), -1, nullptr, 0, &err); |
| 7687 | ERR_FAIL_COND_V_MSG(err != U_BUFFER_OVERFLOW_ERROR, TextServer::strip_diacritics(p_string), u_errorName(err)); |
| 7688 | normalized.resize(len); |
| 7689 | err = U_ZERO_ERROR; |
| 7690 | unorm2_normalize(unorm, utf16.get_data(), -1, normalized.ptrw(), len, &err); |
| 7691 | ERR_FAIL_COND_V_MSG(U_FAILURE(err), TextServer::strip_diacritics(p_string), u_errorName(err)); |
| 7692 | |
| 7693 | // Convert back to UTF-32. |
| 7694 | String normalized_string = String::utf16(normalized.ptr(), len); |
| 7695 | |
| 7696 | // Strip combining characters. |
| 7697 | String result; |
| 7698 | for (int i = 0; i < normalized_string.length(); i++) { |
| 7699 | if (u_getCombiningClass(normalized_string[i]) == 0) { |
| 7700 | #ifdef GDEXTENSION |
| 7701 | result = result + String::chr(normalized_string[i]); |
| 7702 | #elif defined(GODOT_MODULE) |
| 7703 | result = result + normalized_string[i]; |
| 7704 | #endif |
| 7705 | } |
| 7706 | } |
| 7707 | return result; |
| 7708 | } |
| 7709 | |
| 7710 | String TextServerAdvanced::_string_to_upper(const String &p_string, const String &p_language) const { |
| 7711 | #ifndef ICU_STATIC_DATA |
| 7712 | if (!icu_data_loaded) { |
| 7713 | return p_string.to_upper(); |
| 7714 | } |
| 7715 | #endif |
| 7716 | |
| 7717 | if (p_string.is_empty()) { |
| 7718 | return p_string; |
| 7719 | } |
| 7720 | const String lang = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language; |
| 7721 | |
| 7722 | // Convert to UTF-16. |
| 7723 | Char16String utf16 = p_string.utf16(); |
| 7724 | |
| 7725 | Vector<char16_t> upper; |
| 7726 | UErrorCode err = U_ZERO_ERROR; |
| 7727 | int32_t len = u_strToUpper(nullptr, 0, utf16.get_data(), -1, lang.ascii().get_data(), &err); |
| 7728 | ERR_FAIL_COND_V_MSG(err != U_BUFFER_OVERFLOW_ERROR, p_string, u_errorName(err)); |
| 7729 | upper.resize(len); |
| 7730 | err = U_ZERO_ERROR; |
| 7731 | u_strToUpper(upper.ptrw(), len, utf16.get_data(), -1, lang.ascii().get_data(), &err); |
| 7732 | ERR_FAIL_COND_V_MSG(U_FAILURE(err), p_string, u_errorName(err)); |
| 7733 | |
| 7734 | // Convert back to UTF-32. |
| 7735 | return String::utf16(upper.ptr(), len); |
| 7736 | } |
| 7737 | |
| 7738 | String TextServerAdvanced::_string_to_lower(const String &p_string, const String &p_language) const { |
| 7739 | #ifndef ICU_STATIC_DATA |
| 7740 | if (!icu_data_loaded) { |
| 7741 | return p_string.to_lower(); |
| 7742 | } |
| 7743 | #endif |
| 7744 | |
| 7745 | if (p_string.is_empty()) { |
| 7746 | return p_string; |
| 7747 | } |
| 7748 | const String lang = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language; |
| 7749 | // Convert to UTF-16. |
| 7750 | Char16String utf16 = p_string.utf16(); |
| 7751 | |
| 7752 | Vector<char16_t> lower; |
| 7753 | UErrorCode err = U_ZERO_ERROR; |
| 7754 | int32_t len = u_strToLower(nullptr, 0, utf16.get_data(), -1, lang.ascii().get_data(), &err); |
| 7755 | ERR_FAIL_COND_V_MSG(err != U_BUFFER_OVERFLOW_ERROR, p_string, u_errorName(err)); |
| 7756 | lower.resize(len); |
| 7757 | err = U_ZERO_ERROR; |
| 7758 | u_strToLower(lower.ptrw(), len, utf16.get_data(), -1, lang.ascii().get_data(), &err); |
| 7759 | ERR_FAIL_COND_V_MSG(U_FAILURE(err), p_string, u_errorName(err)); |
| 7760 | |
| 7761 | // Convert back to UTF-32. |
| 7762 | return String::utf16(lower.ptr(), len); |
| 7763 | } |
| 7764 | |
| 7765 | String TextServerAdvanced::_string_to_title(const String &p_string, const String &p_language) const { |
| 7766 | #ifndef ICU_STATIC_DATA |
| 7767 | if (!icu_data_loaded) { |
| 7768 | return p_string.capitalize(); |
| 7769 | } |
| 7770 | #endif |
| 7771 | |
| 7772 | if (p_string.is_empty()) { |
| 7773 | return p_string; |
| 7774 | } |
| 7775 | const String lang = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language; |
| 7776 | |
| 7777 | // Convert to UTF-16. |
| 7778 | Char16String utf16 = p_string.utf16(); |
| 7779 | |
| 7780 | Vector<char16_t> upper; |
| 7781 | UErrorCode err = U_ZERO_ERROR; |
| 7782 | int32_t len = u_strToTitle(nullptr, 0, utf16.get_data(), -1, nullptr, lang.ascii().get_data(), &err); |
| 7783 | ERR_FAIL_COND_V_MSG(err != U_BUFFER_OVERFLOW_ERROR, p_string, u_errorName(err)); |
| 7784 | upper.resize(len); |
| 7785 | err = U_ZERO_ERROR; |
| 7786 | u_strToTitle(upper.ptrw(), len, utf16.get_data(), -1, nullptr, lang.ascii().get_data(), &err); |
| 7787 | ERR_FAIL_COND_V_MSG(U_FAILURE(err), p_string, u_errorName(err)); |
| 7788 | |
| 7789 | // Convert back to UTF-32. |
| 7790 | return String::utf16(upper.ptr(), len); |
| 7791 | } |
| 7792 | |
| 7793 | PackedInt32Array TextServerAdvanced::_string_get_word_breaks(const String &p_string, const String &p_language, int64_t p_chars_per_line) const { |
| 7794 | const String lang = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language; |
| 7795 | // Convert to UTF-16. |
| 7796 | Char16String utf16 = p_string.utf16(); |
| 7797 | |
| 7798 | HashSet<int> breaks; |
| 7799 | UErrorCode err = U_ZERO_ERROR; |
| 7800 | UBreakIterator *bi = ubrk_open(UBRK_WORD, lang.ascii().get_data(), (const UChar *)utf16.get_data(), utf16.length(), &err); |
| 7801 | if (U_SUCCESS(err)) { |
| 7802 | while (ubrk_next(bi) != UBRK_DONE) { |
| 7803 | int pos = _convert_pos(p_string, utf16, ubrk_current(bi)); |
| 7804 | if (pos != p_string.length() - 1) { |
| 7805 | breaks.insert(pos); |
| 7806 | } |
| 7807 | } |
| 7808 | ubrk_close(bi); |
| 7809 | } |
| 7810 | |
| 7811 | PackedInt32Array ret; |
| 7812 | |
| 7813 | if (p_chars_per_line > 0) { |
| 7814 | int line_start = 0; |
| 7815 | int last_break = -1; |
| 7816 | int line_length = 0; |
| 7817 | |
| 7818 | for (int i = 0; i < p_string.length(); i++) { |
| 7819 | const char32_t c = p_string[i]; |
| 7820 | |
| 7821 | bool is_lb = is_linebreak(c); |
| 7822 | bool is_ws = is_whitespace(c); |
| 7823 | bool is_p = (u_ispunct(c) && c != 0x005F) || is_underscore(c) || c == '\t' || c == 0xfffc; |
| 7824 | |
| 7825 | if (is_lb) { |
| 7826 | if (line_length > 0) { |
| 7827 | ret.push_back(line_start); |
| 7828 | ret.push_back(i); |
| 7829 | } |
| 7830 | line_start = i; |
| 7831 | line_length = 0; |
| 7832 | last_break = -1; |
| 7833 | continue; |
| 7834 | } else if (breaks.has(i) || is_ws || is_p) { |
| 7835 | last_break = i; |
| 7836 | } |
| 7837 | |
| 7838 | if (line_length == p_chars_per_line) { |
| 7839 | if (last_break != -1) { |
| 7840 | int last_break_w_spaces = last_break; |
| 7841 | while (last_break > line_start && is_whitespace(p_string[last_break - 1])) { |
| 7842 | last_break--; |
| 7843 | } |
| 7844 | if (line_start != last_break) { |
| 7845 | ret.push_back(line_start); |
| 7846 | ret.push_back(last_break); |
| 7847 | } |
| 7848 | while (last_break_w_spaces < p_string.length() && is_whitespace(p_string[last_break_w_spaces])) { |
| 7849 | last_break_w_spaces++; |
| 7850 | } |
| 7851 | line_start = last_break_w_spaces; |
| 7852 | if (last_break_w_spaces < i) { |
| 7853 | line_length = i - last_break_w_spaces; |
| 7854 | } else { |
| 7855 | i = last_break_w_spaces; |
| 7856 | line_length = 0; |
| 7857 | } |
| 7858 | } else { |
| 7859 | ret.push_back(line_start); |
| 7860 | ret.push_back(i); |
| 7861 | line_start = i; |
| 7862 | line_length = 0; |
| 7863 | } |
| 7864 | last_break = -1; |
| 7865 | } |
| 7866 | line_length++; |
| 7867 | } |
| 7868 | if (line_length > 0) { |
| 7869 | ret.push_back(line_start); |
| 7870 | ret.push_back(p_string.length()); |
| 7871 | } |
| 7872 | } else { |
| 7873 | int word_start = 0; // -1 if no word encountered. Leading spaces are part of a word. |
| 7874 | int word_length = 0; |
| 7875 | |
| 7876 | for (int i = 0; i < p_string.length(); i++) { |
| 7877 | const char32_t c = p_string[i]; |
| 7878 | |
| 7879 | bool is_lb = is_linebreak(c); |
| 7880 | bool is_ws = is_whitespace(c); |
| 7881 | bool is_p = (u_ispunct(c) && c != 0x005F) || is_underscore(c) || c == '\t' || c == 0xfffc; |
| 7882 | |
| 7883 | if (word_start == -1) { |
| 7884 | if (!is_lb && !is_ws && !is_p) { |
| 7885 | word_start = i; |
| 7886 | } |
| 7887 | continue; |
| 7888 | } |
| 7889 | |
| 7890 | if (is_lb) { |
| 7891 | if (word_start != -1 && word_length > 0) { |
| 7892 | ret.push_back(word_start); |
| 7893 | ret.push_back(i); |
| 7894 | } |
| 7895 | word_start = -1; |
| 7896 | word_length = 0; |
| 7897 | } else if (breaks.has(i) || is_ws || is_p) { |
| 7898 | if (word_start != -1 && word_length > 0) { |
| 7899 | ret.push_back(word_start); |
| 7900 | ret.push_back(i); |
| 7901 | } |
| 7902 | if (is_ws || is_p) { |
| 7903 | word_start = -1; |
| 7904 | } else { |
| 7905 | word_start = i; |
| 7906 | } |
| 7907 | word_length = 0; |
| 7908 | } |
| 7909 | |
| 7910 | word_length++; |
| 7911 | } |
| 7912 | if (word_start != -1 && word_length > 0) { |
| 7913 | ret.push_back(word_start); |
| 7914 | ret.push_back(p_string.length()); |
| 7915 | } |
| 7916 | } |
| 7917 | |
| 7918 | return ret; |
| 7919 | } |
| 7920 | |
| 7921 | PackedInt32Array TextServerAdvanced::_string_get_character_breaks(const String &p_string, const String &p_language) const { |
| 7922 | const String lang = (p_language.is_empty()) ? TranslationServer::get_singleton()->get_tool_locale() : p_language; |
| 7923 | // Convert to UTF-16. |
| 7924 | Char16String utf16 = p_string.utf16(); |
| 7925 | |
| 7926 | PackedInt32Array ret; |
| 7927 | |
| 7928 | UErrorCode err = U_ZERO_ERROR; |
| 7929 | UBreakIterator *bi = ubrk_open(UBRK_CHARACTER, lang.ascii().get_data(), (const UChar *)utf16.get_data(), utf16.length(), &err); |
| 7930 | if (U_SUCCESS(err)) { |
| 7931 | while (ubrk_next(bi) != UBRK_DONE) { |
| 7932 | int pos = _convert_pos(p_string, utf16, ubrk_current(bi)); |
| 7933 | ret.push_back(pos); |
| 7934 | } |
| 7935 | ubrk_close(bi); |
| 7936 | } else { |
| 7937 | return TextServer::string_get_character_breaks(p_string, p_language); |
| 7938 | } |
| 7939 | |
| 7940 | return ret; |
| 7941 | } |
| 7942 | |
| 7943 | bool TextServerAdvanced::_is_valid_identifier(const String &p_string) const { |
| 7944 | #ifndef ICU_STATIC_DATA |
| 7945 | if (!icu_data_loaded) { |
| 7946 | WARN_PRINT_ONCE("ICU data is not loaded, Unicode security and spoofing detection disabled."); |
| 7947 | return TextServer::is_valid_identifier(p_string); |
| 7948 | } |
| 7949 | #endif |
| 7950 | |
| 7951 | enum UAX31SequenceStatus { |
| 7952 | SEQ_NOT_STARTED, |
| 7953 | SEQ_STARTED, |
| 7954 | SEQ_STARTED_VIR, |
| 7955 | SEQ_NEAR_END, |
| 7956 | }; |
| 7957 | |
| 7958 | const char32_t *str = p_string.ptr(); |
| 7959 | int len = p_string.length(); |
| 7960 | |
| 7961 | if (len == 0) { |
| 7962 | return false; // Empty string. |
| 7963 | } |
| 7964 | |
| 7965 | UErrorCode err = U_ZERO_ERROR; |
| 7966 | Char16String utf16 = p_string.utf16(); |
| 7967 | const UNormalizer2 *norm_c = unorm2_getNFCInstance(&err); |
| 7968 | if (U_FAILURE(err)) { |
| 7969 | return false; // Failed to load normalizer. |
| 7970 | } |
| 7971 | bool isnurom = unorm2_isNormalized(norm_c, utf16.get_data(), utf16.length(), &err); |
| 7972 | if (U_FAILURE(err) || !isnurom) { |
| 7973 | return false; // Do not conform to Normalization Form C. |
| 7974 | } |
| 7975 | |
| 7976 | UAX31SequenceStatus A1_sequence_status = SEQ_NOT_STARTED; |
| 7977 | UScriptCode A1_scr = USCRIPT_INHERITED; |
| 7978 | UAX31SequenceStatus A2_sequence_status = SEQ_NOT_STARTED; |
| 7979 | UScriptCode A2_scr = USCRIPT_INHERITED; |
| 7980 | UAX31SequenceStatus B_sequence_status = SEQ_NOT_STARTED; |
| 7981 | UScriptCode B_scr = USCRIPT_INHERITED; |
| 7982 | |
| 7983 | for (int i = 0; i < len; i++) { |
| 7984 | err = U_ZERO_ERROR; |
| 7985 | UScriptCode scr = uscript_getScript(str[i], &err); |
| 7986 | if (U_FAILURE(err)) { |
| 7987 | return false; // Invalid script. |
| 7988 | } |
| 7989 | if (uscript_getUsage(scr) != USCRIPT_USAGE_RECOMMENDED) { |
| 7990 | return false; // Not a recommended script. |
| 7991 | } |
| 7992 | uint8_t cat = u_charType(str[i]); |
| 7993 | int32_t jt = u_getIntPropertyValue(str[i], UCHAR_JOINING_TYPE); |
| 7994 | |
| 7995 | // UAX #31 section 2.3 subsections A1, A2 and B, check ZWNJ and ZWJ usage. |
| 7996 | switch (A1_sequence_status) { |
| 7997 | case SEQ_NEAR_END: { |
| 7998 | if ((A1_scr > USCRIPT_INHERITED) && (scr > USCRIPT_INHERITED) && (scr != A1_scr)) { |
| 7999 | return false; // Mixed script. |
| 8000 | } |
| 8001 | if (jt == U_JT_RIGHT_JOINING || jt == U_JT_DUAL_JOINING) { |
| 8002 | A1_sequence_status = SEQ_NOT_STARTED; // Valid end of sequence, reset. |
| 8003 | } else if (jt != U_JT_TRANSPARENT) { |
| 8004 | return false; // Invalid end of sequence. |
| 8005 | } |
| 8006 | } break; |
| 8007 | case SEQ_STARTED: { |
| 8008 | if ((A1_scr > USCRIPT_INHERITED) && (scr > USCRIPT_INHERITED) && (scr != A1_scr)) { |
| 8009 | A1_sequence_status = SEQ_NOT_STARTED; // Reset. |
| 8010 | } else { |
| 8011 | if (jt != U_JT_TRANSPARENT) { |
| 8012 | if (str[i] == 0x200C /*ZWNJ*/) { |
| 8013 | A1_sequence_status = SEQ_NEAR_END; |
| 8014 | continue; |
| 8015 | } else { |
| 8016 | A1_sequence_status = SEQ_NOT_STARTED; // Reset. |
| 8017 | } |
| 8018 | } |
| 8019 | } |
| 8020 | } break; |
| 8021 | default: |
| 8022 | break; |
| 8023 | } |
| 8024 | if (A1_sequence_status == SEQ_NOT_STARTED) { |
| 8025 | if (jt == U_JT_LEFT_JOINING || jt == U_JT_DUAL_JOINING) { |
| 8026 | A1_sequence_status = SEQ_STARTED; |
| 8027 | A1_scr = scr; |
| 8028 | } |
| 8029 | }; |
| 8030 | |
| 8031 | switch (A2_sequence_status) { |
| 8032 | case SEQ_NEAR_END: { |
| 8033 | if ((A2_scr > USCRIPT_INHERITED) && (scr > USCRIPT_INHERITED) && (scr != A2_scr)) { |
| 8034 | return false; // Mixed script. |
| 8035 | } |
| 8036 | if (cat == U_UPPERCASE_LETTER || cat == U_LOWERCASE_LETTER || cat == U_TITLECASE_LETTER || cat == U_MODIFIER_LETTER || cat == U_OTHER_LETTER) { |
| 8037 | A2_sequence_status = SEQ_NOT_STARTED; // Valid end of sequence, reset. |
| 8038 | } else if (cat != U_MODIFIER_LETTER || u_getCombiningClass(str[i]) == 0) { |
| 8039 | return false; // Invalid end of sequence. |
| 8040 | } |
| 8041 | } break; |
| 8042 | case SEQ_STARTED_VIR: { |
| 8043 | if ((A2_scr > USCRIPT_INHERITED) && (scr > USCRIPT_INHERITED) && (scr != A2_scr)) { |
| 8044 | A2_sequence_status = SEQ_NOT_STARTED; // Reset. |
| 8045 | } else { |
| 8046 | if (str[i] == 0x200C /*ZWNJ*/) { |
| 8047 | A2_sequence_status = SEQ_NEAR_END; |
| 8048 | continue; |
| 8049 | } else if (cat != U_MODIFIER_LETTER || u_getCombiningClass(str[i]) == 0) { |
| 8050 | A2_sequence_status = SEQ_NOT_STARTED; // Reset. |
| 8051 | } |
| 8052 | } |
| 8053 | } break; |
| 8054 | case SEQ_STARTED: { |
| 8055 | if ((A2_scr > USCRIPT_INHERITED) && (scr > USCRIPT_INHERITED) && (scr != A2_scr)) { |
| 8056 | A2_sequence_status = SEQ_NOT_STARTED; // Reset. |
| 8057 | } else { |
| 8058 | if (u_getCombiningClass(str[i]) == 9 /*Virama Combining Class*/) { |
| 8059 | A2_sequence_status = SEQ_STARTED_VIR; |
| 8060 | } else if (cat != U_MODIFIER_LETTER) { |
| 8061 | A2_sequence_status = SEQ_NOT_STARTED; // Reset. |
| 8062 | } |
| 8063 | } |
| 8064 | } break; |
| 8065 | default: |
| 8066 | break; |
| 8067 | } |
| 8068 | if (A2_sequence_status == SEQ_NOT_STARTED) { |
| 8069 | if (cat == U_UPPERCASE_LETTER || cat == U_LOWERCASE_LETTER || cat == U_TITLECASE_LETTER || cat == U_MODIFIER_LETTER || cat == U_OTHER_LETTER) { |
| 8070 | A2_sequence_status = SEQ_STARTED; |
| 8071 | A2_scr = scr; |
| 8072 | } |
| 8073 | } |
| 8074 | |
| 8075 | switch (B_sequence_status) { |
| 8076 | case SEQ_NEAR_END: { |
| 8077 | if ((B_scr > USCRIPT_INHERITED) && (scr > USCRIPT_INHERITED) && (scr != B_scr)) { |
| 8078 | return false; // Mixed script. |
| 8079 | } |
| 8080 | if (u_getIntPropertyValue(str[i], UCHAR_INDIC_SYLLABIC_CATEGORY) != U_INSC_VOWEL_DEPENDENT) { |
| 8081 | B_sequence_status = SEQ_NOT_STARTED; // Valid end of sequence, reset. |
| 8082 | } else { |
| 8083 | return false; // Invalid end of sequence. |
| 8084 | } |
| 8085 | } break; |
| 8086 | case SEQ_STARTED_VIR: { |
| 8087 | if ((B_scr > USCRIPT_INHERITED) && (scr > USCRIPT_INHERITED) && (scr != B_scr)) { |
| 8088 | B_sequence_status = SEQ_NOT_STARTED; // Reset. |
| 8089 | } else { |
| 8090 | if (str[i] == 0x200D /*ZWJ*/) { |
| 8091 | B_sequence_status = SEQ_NEAR_END; |
| 8092 | continue; |
| 8093 | } else if (cat != U_MODIFIER_LETTER || u_getCombiningClass(str[i]) == 0) { |
| 8094 | B_sequence_status = SEQ_NOT_STARTED; // Reset. |
| 8095 | } |
| 8096 | } |
| 8097 | } break; |
| 8098 | case SEQ_STARTED: { |
| 8099 | if ((B_scr > USCRIPT_INHERITED) && (scr > USCRIPT_INHERITED) && (scr != B_scr)) { |
| 8100 | B_sequence_status = SEQ_NOT_STARTED; // Reset. |
| 8101 | } else { |
| 8102 | if (u_getCombiningClass(str[i]) == 9 /*Virama Combining Class*/) { |
| 8103 | B_sequence_status = SEQ_STARTED_VIR; |
| 8104 | } else if (cat != U_MODIFIER_LETTER) { |
| 8105 | B_sequence_status = SEQ_NOT_STARTED; // Reset. |
| 8106 | } |
| 8107 | } |
| 8108 | } break; |
| 8109 | default: |
| 8110 | break; |
| 8111 | } |
| 8112 | if (B_sequence_status == SEQ_NOT_STARTED) { |
| 8113 | if (cat == U_UPPERCASE_LETTER || cat == U_LOWERCASE_LETTER || cat == U_TITLECASE_LETTER || cat == U_MODIFIER_LETTER || cat == U_OTHER_LETTER) { |
| 8114 | B_sequence_status = SEQ_STARTED; |
| 8115 | B_scr = scr; |
| 8116 | } |
| 8117 | } |
| 8118 | |
| 8119 | if (u_hasBinaryProperty(str[i], UCHAR_PATTERN_SYNTAX) || u_hasBinaryProperty(str[i], UCHAR_PATTERN_WHITE_SPACE) || u_hasBinaryProperty(str[i], UCHAR_NONCHARACTER_CODE_POINT)) { |
| 8120 | return false; // Not a XID_Start or XID_Continue character. |
| 8121 | } |
| 8122 | if (i == 0) { |
| 8123 | if (!(cat == U_LOWERCASE_LETTER || cat == U_UPPERCASE_LETTER || cat == U_TITLECASE_LETTER || cat == U_OTHER_LETTER || cat == U_MODIFIER_LETTER || cat == U_LETTER_NUMBER || str[0] == 0x2118 || str[0] == 0x212E || str[0] == 0x309B || str[0] == 0x309C || str[0] == 0x005F)) { |
| 8124 | return false; // Not a XID_Start character. |
| 8125 | } |
| 8126 | } else { |
| 8127 | if (!(cat == U_LOWERCASE_LETTER || cat == U_UPPERCASE_LETTER || cat == U_TITLECASE_LETTER || cat == U_OTHER_LETTER || cat == U_MODIFIER_LETTER || cat == U_LETTER_NUMBER || cat == U_NON_SPACING_MARK || cat == U_COMBINING_SPACING_MARK || cat == U_DECIMAL_DIGIT_NUMBER || cat == U_CONNECTOR_PUNCTUATION || str[i] == 0x2118 || str[i] == 0x212E || str[i] == 0x309B || str[i] == 0x309C || str[i] == 0x1369 || str[i] == 0x1371 || str[i] == 0x00B7 || str[i] == 0x0387 || str[i] == 0x19DA || str[i] == 0x0E33 || str[i] == 0x0EB3 || str[i] == 0xFF9E || str[i] == 0xFF9F)) { |
| 8128 | return false; // Not a XID_Continue character. |
| 8129 | } |
| 8130 | } |
| 8131 | } |
| 8132 | return true; |
| 8133 | } |
| 8134 | |
| 8135 | bool TextServerAdvanced::_is_valid_letter(uint64_t p_unicode) const { |
| 8136 | #ifndef ICU_STATIC_DATA |
| 8137 | if (!icu_data_loaded) { |
| 8138 | return TextServer::is_valid_letter(p_unicode); |
| 8139 | } |
| 8140 | #endif |
| 8141 | |
| 8142 | return u_isalpha(p_unicode); |
| 8143 | } |
| 8144 | |
| 8145 | void TextServerAdvanced::_update_settings() { |
| 8146 | lcd_subpixel_layout.set((TextServer::FontLCDSubpixelLayout)(int)GLOBAL_GET("gui/theme/lcd_subpixel_layout")); |
| 8147 | lb_strictness = (LineBreakStrictness)(int)GLOBAL_GET("internationalization/locale/line_breaking_strictness"); |
| 8148 | } |
| 8149 | |
| 8150 | TextServerAdvanced::TextServerAdvanced() { |
| 8151 | os_locale = OS::get_singleton()->get_locale(); |
| 8152 | |
| 8153 | _insert_feature_sets(); |
| 8154 | _bmp_create_font_funcs(); |
| 8155 | _update_settings(); |
| 8156 | ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &TextServerAdvanced::_update_settings)); |
| 8157 | } |
| 8158 | |
| 8159 | void TextServerAdvanced::_font_clear_system_fallback_cache() { |
| 8160 | _THREAD_SAFE_METHOD_ |
| 8161 | for (const KeyValue<SystemFontKey, SystemFontCache> &E : system_fonts) { |
| 8162 | const Vector<SystemFontCacheRec> &sysf_cache = E.value.var; |
| 8163 | for (const SystemFontCacheRec &F : sysf_cache) { |
| 8164 | _free_rid(F.rid); |
| 8165 | } |
| 8166 | } |
| 8167 | system_fonts.clear(); |
| 8168 | system_font_data.clear(); |
| 8169 | } |
| 8170 | |
| 8171 | void TextServerAdvanced::_cleanup() { |
| 8172 | font_clear_system_fallback_cache(); |
| 8173 | } |
| 8174 | |
| 8175 | TextServerAdvanced::~TextServerAdvanced() { |
| 8176 | _bmp_free_font_funcs(); |
| 8177 | #ifdef MODULE_FREETYPE_ENABLED |
| 8178 | if (ft_library != nullptr) { |
| 8179 | FT_Done_FreeType(ft_library); |
| 8180 | } |
| 8181 | #endif |
| 8182 | if (sc_spoof != nullptr) { |
| 8183 | uspoof_close(sc_spoof); |
| 8184 | sc_spoof = nullptr; |
| 8185 | } |
| 8186 | if (sc_conf != nullptr) { |
| 8187 | uspoof_close(sc_conf); |
| 8188 | sc_conf = nullptr; |
| 8189 | } |
| 8190 | if (allowed != nullptr) { |
| 8191 | uset_close(allowed); |
| 8192 | allowed = nullptr; |
| 8193 | } |
| 8194 | for (const KeyValue<String, UBreakIterator *> &bi : line_break_iterators_per_language) { |
| 8195 | ubrk_close(bi.value); |
| 8196 | } |
| 8197 | |
| 8198 | std::atexit(u_cleanup); |
| 8199 | } |