text_server_fb.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. /**************************************************************************/
  2. /* text_server_fb.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. /*************************************************************************/
  32. /* Fallback Text Server provides simplified TS functionality, without */
  33. /* BiDi, shaping and advanced font features support. */
  34. /*************************************************************************/
  35. #ifdef GDEXTENSION
  36. // Headers for building as GDExtension plug-in.
  37. #include <godot_cpp/godot.hpp>
  38. #include <godot_cpp/core/class_db.hpp>
  39. #include <godot_cpp/core/ext_wrappers.gen.inc>
  40. #include <godot_cpp/core/mutex_lock.hpp>
  41. #include <godot_cpp/variant/array.hpp>
  42. #include <godot_cpp/variant/dictionary.hpp>
  43. #include <godot_cpp/variant/packed_int32_array.hpp>
  44. #include <godot_cpp/variant/packed_string_array.hpp>
  45. #include <godot_cpp/variant/packed_vector2_array.hpp>
  46. #include <godot_cpp/variant/rect2.hpp>
  47. #include <godot_cpp/variant/rid.hpp>
  48. #include <godot_cpp/variant/string.hpp>
  49. #include <godot_cpp/variant/typed_array.hpp>
  50. #include <godot_cpp/variant/vector2.hpp>
  51. #include <godot_cpp/variant/vector2i.hpp>
  52. #include <godot_cpp/classes/text_server.hpp>
  53. #include <godot_cpp/classes/text_server_extension.hpp>
  54. #include <godot_cpp/classes/text_server_manager.hpp>
  55. #include <godot_cpp/classes/caret_info.hpp>
  56. #include <godot_cpp/classes/global_constants_binds.hpp>
  57. #include <godot_cpp/classes/glyph.hpp>
  58. #include <godot_cpp/classes/image.hpp>
  59. #include <godot_cpp/classes/image_texture.hpp>
  60. #include <godot_cpp/classes/ref.hpp>
  61. #include <godot_cpp/classes/worker_thread_pool.hpp>
  62. #include <godot_cpp/templates/hash_map.hpp>
  63. #include <godot_cpp/templates/hash_set.hpp>
  64. #include <godot_cpp/templates/rid_owner.hpp>
  65. #include <godot_cpp/templates/safe_refcount.hpp>
  66. #include <godot_cpp/templates/vector.hpp>
  67. using namespace godot;
  68. #elif defined(GODOT_MODULE)
  69. // Headers for building as built-in module.
  70. #include "core/extension/ext_wrappers.gen.inc"
  71. #include "core/object/worker_thread_pool.h"
  72. #include "core/templates/hash_map.h"
  73. #include "core/templates/rid_owner.h"
  74. #include "core/templates/safe_refcount.h"
  75. #include "scene/resources/image_texture.h"
  76. #include "servers/text/text_server_extension.h"
  77. #include "modules/modules_enabled.gen.h" // For freetype, msdfgen, svg.
  78. #endif
  79. // Thirdparty headers.
  80. #ifdef MODULE_FREETYPE_ENABLED
  81. #include <ft2build.h>
  82. #include FT_FREETYPE_H
  83. #include FT_TRUETYPE_TABLES_H
  84. #include FT_STROKER_H
  85. #include FT_ADVANCES_H
  86. #include FT_MULTIPLE_MASTERS_H
  87. #include FT_BBOX_H
  88. #include FT_MODULE_H
  89. #include FT_CONFIG_OPTIONS_H
  90. #if !defined(FT_CONFIG_OPTION_USE_BROTLI) && !defined(_MSC_VER)
  91. #warning FreeType is configured without Brotli support, built-in fonts will not be available.
  92. #endif
  93. #endif
  94. /*************************************************************************/
  95. class TextServerFallback : public TextServerExtension {
  96. GDCLASS(TextServerFallback, TextServerExtension);
  97. _THREAD_SAFE_CLASS_
  98. HashMap<StringName, int32_t> feature_sets;
  99. HashMap<int32_t, StringName> feature_sets_inv;
  100. SafeNumeric<TextServer::FontLCDSubpixelLayout> lcd_subpixel_layout{ TextServer::FontLCDSubpixelLayout::FONT_LCD_SUBPIXEL_LAYOUT_NONE };
  101. void _update_settings();
  102. void _insert_feature_sets();
  103. _FORCE_INLINE_ void _insert_feature(const StringName &p_name, int32_t p_tag);
  104. // Font cache data.
  105. #ifdef MODULE_FREETYPE_ENABLED
  106. mutable FT_Library ft_library = nullptr;
  107. #endif
  108. const int rect_range = 1;
  109. struct FontTexturePosition {
  110. int32_t index = -1;
  111. int32_t x = 0;
  112. int32_t y = 0;
  113. FontTexturePosition() {}
  114. FontTexturePosition(int32_t p_id, int32_t p_x, int32_t p_y) :
  115. index(p_id), x(p_x), y(p_y) {}
  116. };
  117. struct Shelf {
  118. int32_t x = 0;
  119. int32_t y = 0;
  120. int32_t w = 0;
  121. int32_t h = 0;
  122. FontTexturePosition alloc_shelf(int32_t p_id, int32_t p_w, int32_t p_h) {
  123. if (p_w > w || p_h > h) {
  124. return FontTexturePosition(-1, 0, 0);
  125. }
  126. int32_t xx = x;
  127. x += p_w;
  128. w -= p_w;
  129. return FontTexturePosition(p_id, xx, y);
  130. }
  131. Shelf() {}
  132. Shelf(int32_t p_x, int32_t p_y, int32_t p_w, int32_t p_h) :
  133. x(p_x), y(p_y), w(p_w), h(p_h) {}
  134. };
  135. struct ShelfPackTexture {
  136. int32_t texture_w = 1024;
  137. int32_t texture_h = 1024;
  138. Ref<Image> image;
  139. Ref<ImageTexture> texture;
  140. bool dirty = true;
  141. List<Shelf> shelves;
  142. FontTexturePosition pack_rect(int32_t p_id, int32_t p_h, int32_t p_w) {
  143. int32_t y = 0;
  144. int32_t waste = 0;
  145. Shelf *best_shelf = nullptr;
  146. int32_t best_waste = std::numeric_limits<std::int32_t>::max();
  147. for (Shelf &E : shelves) {
  148. y += E.h;
  149. if (p_w > E.w) {
  150. continue;
  151. }
  152. if (p_h == E.h) {
  153. return E.alloc_shelf(p_id, p_w, p_h);
  154. }
  155. if (p_h > E.h) {
  156. continue;
  157. }
  158. if (p_h < E.h) {
  159. waste = (E.h - p_h) * p_w;
  160. if (waste < best_waste) {
  161. best_waste = waste;
  162. best_shelf = &E;
  163. }
  164. }
  165. }
  166. if (best_shelf) {
  167. return best_shelf->alloc_shelf(p_id, p_w, p_h);
  168. }
  169. if (p_h <= (texture_h - y) && p_w <= texture_w) {
  170. List<Shelf>::Element *E = shelves.push_back(Shelf(0, y, texture_w, p_h));
  171. return E->get().alloc_shelf(p_id, p_w, p_h);
  172. }
  173. return FontTexturePosition(-1, 0, 0);
  174. }
  175. ShelfPackTexture() {}
  176. ShelfPackTexture(int32_t p_w, int32_t p_h) :
  177. texture_w(p_w), texture_h(p_h) {}
  178. };
  179. struct FontGlyph {
  180. bool found = false;
  181. int texture_idx = -1;
  182. Rect2 rect;
  183. Rect2 uv_rect;
  184. Vector2 advance;
  185. bool from_svg = false;
  186. };
  187. struct FontFallback;
  188. struct FontForSizeFallback {
  189. double ascent = 0.0;
  190. double descent = 0.0;
  191. double underline_position = 0.0;
  192. double underline_thickness = 0.0;
  193. double scale = 1.0;
  194. FontFallback *owner = nullptr;
  195. uint32_t viewport_oversampling = 0;
  196. Vector2i size;
  197. Vector<ShelfPackTexture> textures;
  198. HashMap<int32_t, FontGlyph> glyph_map;
  199. HashMap<Vector2i, Vector2> kerning_map;
  200. #ifdef MODULE_FREETYPE_ENABLED
  201. FT_Face face = nullptr;
  202. FT_StreamRec stream;
  203. #endif
  204. ~FontForSizeFallback() {
  205. #ifdef MODULE_FREETYPE_ENABLED
  206. if (face != nullptr) {
  207. FT_Done_Face(face);
  208. }
  209. #endif
  210. }
  211. };
  212. struct OversamplingLevel {
  213. HashSet<FontForSizeFallback *> fonts;
  214. int32_t refcount = 1;
  215. };
  216. mutable HashMap<uint32_t, OversamplingLevel> oversampling_levels;
  217. struct FontFallbackLinkedVariation {
  218. RID base_font;
  219. int extra_spacing[4] = { 0, 0, 0, 0 };
  220. double baseline_offset = 0.0;
  221. };
  222. struct FontFallback {
  223. Mutex mutex;
  224. TextServer::FontAntialiasing antialiasing = TextServer::FONT_ANTIALIASING_GRAY;
  225. bool disable_embedded_bitmaps = true;
  226. bool mipmaps = false;
  227. bool msdf = false;
  228. FixedSizeScaleMode fixed_size_scale_mode = FIXED_SIZE_SCALE_DISABLE;
  229. int msdf_range = 14;
  230. int msdf_source_size = 48;
  231. int fixed_size = 0;
  232. bool force_autohinter = false;
  233. bool allow_system_fallback = true;
  234. bool modulate_color_glyphs = false;
  235. TextServer::Hinting hinting = TextServer::HINTING_LIGHT;
  236. TextServer::SubpixelPositioning subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_AUTO;
  237. bool keep_rounding_remainders = true;
  238. Dictionary variation_coordinates;
  239. double embolden = 0.0;
  240. Transform2D transform;
  241. BitField<TextServer::FontStyle> style_flags = 0;
  242. String font_name;
  243. String style_name;
  244. int weight = 400;
  245. int stretch = 100;
  246. int extra_spacing[4] = { 0, 0, 0, 0 };
  247. double baseline_offset = 0.0;
  248. HashMap<Vector2i, FontForSizeFallback *> cache;
  249. bool face_init = false;
  250. Dictionary supported_varaitions;
  251. Dictionary feature_overrides;
  252. // Language/script support override.
  253. HashMap<String, bool> language_support_overrides;
  254. HashMap<String, bool> script_support_overrides;
  255. PackedByteArray data;
  256. const uint8_t *data_ptr;
  257. size_t data_size;
  258. int face_index = 0;
  259. ~FontFallback() {
  260. for (const KeyValue<Vector2i, FontForSizeFallback *> &E : cache) {
  261. memdelete(E.value);
  262. }
  263. cache.clear();
  264. }
  265. };
  266. _FORCE_INLINE_ FontTexturePosition find_texture_pos_for_glyph(FontForSizeFallback *p_data, int p_color_size, Image::Format p_image_format, int p_width, int p_height, bool p_msdf) const;
  267. #ifdef MODULE_MSDFGEN_ENABLED
  268. _FORCE_INLINE_ FontGlyph rasterize_msdf(FontFallback *p_font_data, FontForSizeFallback *p_data, int p_pixel_range, int p_rect_margin, FT_Outline *p_outline, const Vector2 &p_advance) const;
  269. #endif
  270. #ifdef MODULE_FREETYPE_ENABLED
  271. _FORCE_INLINE_ FontGlyph rasterize_bitmap(FontForSizeFallback *p_data, int p_rect_margin, FT_Bitmap p_bitmap, int p_yofs, int p_xofs, const Vector2 &p_advance, bool p_bgra) const;
  272. #endif
  273. bool _ensure_glyph(FontFallback *p_font_data, const Vector2i &p_size, int32_t p_glyph, FontGlyph &r_glyph, uint32_t p_oversampling = 0) const;
  274. bool _ensure_cache_for_size(FontFallback *p_font_data, const Vector2i &p_size, FontForSizeFallback *&r_cache_for_size, bool p_silent = false, uint32_t p_oversampling = 0) const;
  275. _FORCE_INLINE_ bool _font_validate(const RID &p_font_rid) const;
  276. _FORCE_INLINE_ void _font_clear_cache(FontFallback *p_font_data);
  277. static void _generateMTSDF_threaded(void *p_td, uint32_t p_y);
  278. _FORCE_INLINE_ Vector2i _get_size(const FontFallback *p_font_data, int p_size) const {
  279. if (p_font_data->msdf) {
  280. return Vector2i(p_font_data->msdf_source_size * 64, 0);
  281. } else if (p_font_data->fixed_size > 0) {
  282. return Vector2i(p_font_data->fixed_size * 64, 0);
  283. } else {
  284. return Vector2i(p_size * 64, 0);
  285. }
  286. }
  287. _FORCE_INLINE_ Vector2i _get_size_outline(const FontFallback *p_font_data, const Vector2i &p_size) const {
  288. if (p_font_data->msdf) {
  289. return Vector2i(p_font_data->msdf_source_size * 64, 0);
  290. } else if (p_font_data->fixed_size > 0) {
  291. return Vector2i(p_font_data->fixed_size * 64, MIN(p_size.y, 1));
  292. } else {
  293. return Vector2i(p_size.x * 64, p_size.y);
  294. }
  295. }
  296. _FORCE_INLINE_ int _font_get_weight_by_name(const String &p_sty_name) const {
  297. String sty_name = p_sty_name.remove_chars(" -");
  298. if (sty_name.contains("thin") || sty_name.contains("hairline")) {
  299. return 100;
  300. } else if (sty_name.contains("extralight") || sty_name.contains("ultralight")) {
  301. return 200;
  302. } else if (sty_name.contains("light")) {
  303. return 300;
  304. } else if (sty_name.contains("semilight")) {
  305. return 350;
  306. } else if (sty_name.contains("regular")) {
  307. return 400;
  308. } else if (sty_name.contains("medium")) {
  309. return 500;
  310. } else if (sty_name.contains("semibold") || sty_name.contains("demibold")) {
  311. return 600;
  312. } else if (sty_name.contains("bold")) {
  313. return 700;
  314. } else if (sty_name.contains("extrabold") || sty_name.contains("ultrabold")) {
  315. return 800;
  316. } else if (sty_name.contains("black") || sty_name.contains("heavy")) {
  317. return 900;
  318. } else if (sty_name.contains("extrablack") || sty_name.contains("ultrablack")) {
  319. return 950;
  320. }
  321. return 400;
  322. }
  323. _FORCE_INLINE_ int _font_get_stretch_by_name(const String &p_sty_name) const {
  324. String sty_name = p_sty_name.remove_chars(" -");
  325. if (sty_name.contains("ultracondensed")) {
  326. return 50;
  327. } else if (sty_name.contains("extracondensed")) {
  328. return 63;
  329. } else if (sty_name.contains("condensed")) {
  330. return 75;
  331. } else if (sty_name.contains("semicondensed")) {
  332. return 87;
  333. } else if (sty_name.contains("semiexpanded")) {
  334. return 113;
  335. } else if (sty_name.contains("expanded")) {
  336. return 125;
  337. } else if (sty_name.contains("extraexpanded")) {
  338. return 150;
  339. } else if (sty_name.contains("ultraexpanded")) {
  340. return 200;
  341. }
  342. return 100;
  343. }
  344. _FORCE_INLINE_ bool _is_ital_style(const String &p_sty_name) const {
  345. return p_sty_name.contains("italic") || p_sty_name.contains("oblique");
  346. }
  347. // Shaped text cache data.
  348. struct TrimData {
  349. int trim_pos = -1;
  350. int ellipsis_pos = -1;
  351. Vector<Glyph> ellipsis_glyph_buf;
  352. };
  353. struct TextRun {
  354. Vector2i range;
  355. RID font_rid;
  356. int font_size = 0;
  357. int64_t span_index = -1;
  358. };
  359. struct ShapedTextDataFallback {
  360. Mutex mutex;
  361. /* Source data */
  362. RID parent; // Substring parent ShapedTextData.
  363. int start = 0; // Substring start offset in the parent string.
  364. int end = 0; // Substring end offset in the parent string.
  365. String text;
  366. String custom_punct;
  367. TextServer::Direction direction = DIRECTION_LTR; // Desired text direction.
  368. TextServer::Orientation orientation = ORIENTATION_HORIZONTAL;
  369. struct Span {
  370. int start = -1;
  371. int end = -1;
  372. Array fonts;
  373. int font_size = 0;
  374. Variant embedded_key;
  375. String language;
  376. Dictionary features;
  377. Variant meta;
  378. };
  379. Vector<Span> spans;
  380. int first_span = 0; // First span in the parent ShapedTextData.
  381. int last_span = 0;
  382. Vector<TextRun> runs;
  383. bool runs_dirty = true;
  384. struct EmbeddedObject {
  385. int start = -1;
  386. int end = -1;
  387. InlineAlignment inline_align = INLINE_ALIGNMENT_CENTER;
  388. Rect2 rect;
  389. double baseline = 0;
  390. };
  391. HashMap<Variant, EmbeddedObject, VariantHasher, VariantComparator> objects;
  392. /* Shaped data */
  393. TextServer::Direction para_direction = DIRECTION_LTR; // Detected text direction.
  394. SafeFlag valid{ false }; // String is shaped.
  395. bool line_breaks_valid = false; // Line and word break flags are populated (and virtual zero width spaces inserted).
  396. bool justification_ops_valid = false; // Virtual elongation glyphs are added to the string.
  397. bool sort_valid = false;
  398. bool text_trimmed = false;
  399. bool preserve_invalid = true; // Draw hex code box instead of missing characters.
  400. bool preserve_control = false; // Draw control characters.
  401. double ascent = 0.0; // Ascent for horizontal layout, 1/2 of width for vertical.
  402. double descent = 0.0; // Descent for horizontal layout, 1/2 of width for vertical.
  403. double width = 0.0; // Width for horizontal layout, height for vertical.
  404. double width_trimmed = 0.0;
  405. int extra_spacing[4] = { 0, 0, 0, 0 };
  406. double upos = 0.0;
  407. double uthk = 0.0;
  408. char32_t el_char = 0x2026;
  409. TrimData overrun_trim_data;
  410. bool fit_width_minimum_reached = false;
  411. Vector<Glyph> glyphs;
  412. Vector<Glyph> glyphs_logical;
  413. };
  414. // Common data.
  415. mutable RID_PtrOwner<FontFallbackLinkedVariation> font_var_owner;
  416. mutable RID_PtrOwner<FontFallback> font_owner;
  417. mutable RID_PtrOwner<ShapedTextDataFallback> shaped_owner;
  418. _FORCE_INLINE_ FontFallback *_get_font_data(const RID &p_font_rid) const {
  419. RID rid = p_font_rid;
  420. FontFallbackLinkedVariation *fdv = font_var_owner.get_or_null(rid);
  421. if (unlikely(fdv)) {
  422. rid = fdv->base_font;
  423. }
  424. return font_owner.get_or_null(rid);
  425. }
  426. struct SystemFontKey {
  427. String font_name;
  428. TextServer::FontAntialiasing antialiasing = TextServer::FONT_ANTIALIASING_GRAY;
  429. bool disable_embedded_bitmaps = true;
  430. bool italic = false;
  431. bool mipmaps = false;
  432. bool msdf = false;
  433. bool force_autohinter = false;
  434. int weight = 400;
  435. int stretch = 100;
  436. int msdf_range = 14;
  437. int msdf_source_size = 48;
  438. int fixed_size = 0;
  439. TextServer::Hinting hinting = TextServer::HINTING_LIGHT;
  440. TextServer::SubpixelPositioning subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_AUTO;
  441. bool keep_rounding_remainders = true;
  442. Dictionary variation_coordinates;
  443. double embolden = 0.0;
  444. Transform2D transform;
  445. int extra_spacing[4] = { 0, 0, 0, 0 };
  446. double baseline_offset = 0.0;
  447. bool operator==(const SystemFontKey &p_b) const {
  448. return (font_name == p_b.font_name) && (antialiasing == p_b.antialiasing) && (italic == p_b.italic) && (disable_embedded_bitmaps == p_b.disable_embedded_bitmaps) && (mipmaps == p_b.mipmaps) && (msdf == p_b.msdf) && (force_autohinter == p_b.force_autohinter) && (weight == p_b.weight) && (stretch == p_b.stretch) && (msdf_range == p_b.msdf_range) && (msdf_source_size == p_b.msdf_source_size) && (fixed_size == p_b.fixed_size) && (hinting == p_b.hinting) && (subpixel_positioning == p_b.subpixel_positioning) && (keep_rounding_remainders == p_b.keep_rounding_remainders) && (variation_coordinates == p_b.variation_coordinates) && (embolden == p_b.embolden) && (transform == p_b.transform) && (extra_spacing[SPACING_TOP] == p_b.extra_spacing[SPACING_TOP]) && (extra_spacing[SPACING_BOTTOM] == p_b.extra_spacing[SPACING_BOTTOM]) && (extra_spacing[SPACING_SPACE] == p_b.extra_spacing[SPACING_SPACE]) && (extra_spacing[SPACING_GLYPH] == p_b.extra_spacing[SPACING_GLYPH]) && (baseline_offset == p_b.baseline_offset);
  449. }
  450. SystemFontKey(const String &p_font_name, bool p_italic, int p_weight, int p_stretch, RID p_font, const TextServerFallback *p_fb) {
  451. font_name = p_font_name;
  452. italic = p_italic;
  453. weight = p_weight;
  454. stretch = p_stretch;
  455. antialiasing = p_fb->_font_get_antialiasing(p_font);
  456. disable_embedded_bitmaps = p_fb->_font_get_disable_embedded_bitmaps(p_font);
  457. mipmaps = p_fb->_font_get_generate_mipmaps(p_font);
  458. msdf = p_fb->_font_is_multichannel_signed_distance_field(p_font);
  459. msdf_range = p_fb->_font_get_msdf_pixel_range(p_font);
  460. msdf_source_size = p_fb->_font_get_msdf_size(p_font);
  461. fixed_size = p_fb->_font_get_fixed_size(p_font);
  462. force_autohinter = p_fb->_font_is_force_autohinter(p_font);
  463. hinting = p_fb->_font_get_hinting(p_font);
  464. subpixel_positioning = p_fb->_font_get_subpixel_positioning(p_font);
  465. keep_rounding_remainders = p_fb->_font_get_keep_rounding_remainders(p_font);
  466. variation_coordinates = p_fb->_font_get_variation_coordinates(p_font);
  467. embolden = p_fb->_font_get_embolden(p_font);
  468. transform = p_fb->_font_get_transform(p_font);
  469. extra_spacing[SPACING_TOP] = p_fb->_font_get_spacing(p_font, SPACING_TOP);
  470. extra_spacing[SPACING_BOTTOM] = p_fb->_font_get_spacing(p_font, SPACING_BOTTOM);
  471. extra_spacing[SPACING_SPACE] = p_fb->_font_get_spacing(p_font, SPACING_SPACE);
  472. extra_spacing[SPACING_GLYPH] = p_fb->_font_get_spacing(p_font, SPACING_GLYPH);
  473. baseline_offset = p_fb->_font_get_baseline_offset(p_font);
  474. }
  475. };
  476. struct SystemFontCacheRec {
  477. RID rid;
  478. int index = 0;
  479. };
  480. struct SystemFontCache {
  481. Vector<SystemFontCacheRec> var;
  482. int max_var = 0;
  483. };
  484. struct SystemFontKeyHasher {
  485. _FORCE_INLINE_ static uint32_t hash(const SystemFontKey &p_a) {
  486. uint32_t hash = p_a.font_name.hash();
  487. hash = hash_murmur3_one_32(p_a.variation_coordinates.hash(), hash);
  488. hash = hash_murmur3_one_32(p_a.weight, hash);
  489. hash = hash_murmur3_one_32(p_a.stretch, hash);
  490. hash = hash_murmur3_one_32(p_a.msdf_range, hash);
  491. hash = hash_murmur3_one_32(p_a.msdf_source_size, hash);
  492. hash = hash_murmur3_one_32(p_a.fixed_size, hash);
  493. hash = hash_murmur3_one_double(p_a.embolden, hash);
  494. hash = hash_murmur3_one_real(p_a.transform[0].x, hash);
  495. hash = hash_murmur3_one_real(p_a.transform[0].y, hash);
  496. hash = hash_murmur3_one_real(p_a.transform[1].x, hash);
  497. hash = hash_murmur3_one_real(p_a.transform[1].y, hash);
  498. hash = hash_murmur3_one_32(p_a.extra_spacing[SPACING_TOP], hash);
  499. hash = hash_murmur3_one_32(p_a.extra_spacing[SPACING_BOTTOM], hash);
  500. hash = hash_murmur3_one_32(p_a.extra_spacing[SPACING_SPACE], hash);
  501. hash = hash_murmur3_one_32(p_a.extra_spacing[SPACING_GLYPH], hash);
  502. hash = hash_murmur3_one_double(p_a.baseline_offset, hash);
  503. return hash_fmix32(hash_murmur3_one_32(((int)p_a.mipmaps) | ((int)p_a.msdf << 1) | ((int)p_a.italic << 2) | ((int)p_a.force_autohinter << 3) | ((int)p_a.hinting << 4) | ((int)p_a.subpixel_positioning << 8) | ((int)p_a.antialiasing << 12) | ((int)p_a.disable_embedded_bitmaps << 14) | ((int)p_a.keep_rounding_remainders << 15), hash));
  504. }
  505. };
  506. mutable HashMap<SystemFontKey, SystemFontCache, SystemFontKeyHasher> system_fonts;
  507. mutable HashMap<String, PackedByteArray> system_font_data;
  508. void _generate_runs(ShapedTextDataFallback *p_sd) const;
  509. void _realign(ShapedTextDataFallback *p_sd) const;
  510. _FORCE_INLINE_ RID _find_sys_font_for_text(const RID &p_fdef, const String &p_script_code, const String &p_language, const String &p_text);
  511. Mutex ft_mutex;
  512. protected:
  513. static void _bind_methods() {}
  514. void full_copy(ShapedTextDataFallback *p_shaped);
  515. void invalidate(ShapedTextDataFallback *p_shaped);
  516. public:
  517. MODBIND1RC(bool, has_feature, Feature);
  518. MODBIND0RC(String, get_name);
  519. MODBIND0RC(int64_t, get_features);
  520. MODBIND1(free_rid, const RID &);
  521. MODBIND1R(bool, has, const RID &);
  522. MODBIND1R(bool, load_support_data, const String &);
  523. MODBIND0RC(String, get_support_data_filename);
  524. MODBIND0RC(String, get_support_data_info);
  525. MODBIND1RC(bool, save_support_data, const String &);
  526. MODBIND0RC(PackedByteArray, get_support_data);
  527. MODBIND1RC(bool, is_locale_right_to_left, const String &);
  528. MODBIND1RC(int64_t, name_to_tag, const String &);
  529. MODBIND1RC(String, tag_to_name, int64_t);
  530. /* Font interface */
  531. MODBIND0R(RID, create_font);
  532. MODBIND1R(RID, create_font_linked_variation, const RID &);
  533. MODBIND2(font_set_data, const RID &, const PackedByteArray &);
  534. MODBIND3(font_set_data_ptr, const RID &, const uint8_t *, int64_t);
  535. MODBIND2(font_set_face_index, const RID &, int64_t);
  536. MODBIND1RC(int64_t, font_get_face_index, const RID &);
  537. MODBIND1RC(int64_t, font_get_face_count, const RID &);
  538. MODBIND2(font_set_style, const RID &, BitField<FontStyle>);
  539. MODBIND1RC(BitField<FontStyle>, font_get_style, const RID &);
  540. MODBIND2(font_set_style_name, const RID &, const String &);
  541. MODBIND1RC(String, font_get_style_name, const RID &);
  542. MODBIND2(font_set_weight, const RID &, int64_t);
  543. MODBIND1RC(int64_t, font_get_weight, const RID &);
  544. MODBIND2(font_set_stretch, const RID &, int64_t);
  545. MODBIND1RC(int64_t, font_get_stretch, const RID &);
  546. MODBIND2(font_set_name, const RID &, const String &);
  547. MODBIND1RC(String, font_get_name, const RID &);
  548. MODBIND2(font_set_antialiasing, const RID &, TextServer::FontAntialiasing);
  549. MODBIND1RC(TextServer::FontAntialiasing, font_get_antialiasing, const RID &);
  550. MODBIND2(font_set_disable_embedded_bitmaps, const RID &, bool);
  551. MODBIND1RC(bool, font_get_disable_embedded_bitmaps, const RID &);
  552. MODBIND2(font_set_generate_mipmaps, const RID &, bool);
  553. MODBIND1RC(bool, font_get_generate_mipmaps, const RID &);
  554. MODBIND2(font_set_multichannel_signed_distance_field, const RID &, bool);
  555. MODBIND1RC(bool, font_is_multichannel_signed_distance_field, const RID &);
  556. MODBIND2(font_set_msdf_pixel_range, const RID &, int64_t);
  557. MODBIND1RC(int64_t, font_get_msdf_pixel_range, const RID &);
  558. MODBIND2(font_set_msdf_size, const RID &, int64_t);
  559. MODBIND1RC(int64_t, font_get_msdf_size, const RID &);
  560. MODBIND2(font_set_fixed_size, const RID &, int64_t);
  561. MODBIND1RC(int64_t, font_get_fixed_size, const RID &);
  562. MODBIND2(font_set_fixed_size_scale_mode, const RID &, FixedSizeScaleMode);
  563. MODBIND1RC(FixedSizeScaleMode, font_get_fixed_size_scale_mode, const RID &);
  564. MODBIND2(font_set_allow_system_fallback, const RID &, bool);
  565. MODBIND1RC(bool, font_is_allow_system_fallback, const RID &);
  566. MODBIND0(font_clear_system_fallback_cache);
  567. MODBIND2(font_set_force_autohinter, const RID &, bool);
  568. MODBIND1RC(bool, font_is_force_autohinter, const RID &);
  569. MODBIND2(font_set_modulate_color_glyphs, const RID &, bool);
  570. MODBIND1RC(bool, font_is_modulate_color_glyphs, const RID &);
  571. MODBIND2(font_set_subpixel_positioning, const RID &, SubpixelPositioning);
  572. MODBIND1RC(SubpixelPositioning, font_get_subpixel_positioning, const RID &);
  573. MODBIND2(font_set_keep_rounding_remainders, const RID &, bool);
  574. MODBIND1RC(bool, font_get_keep_rounding_remainders, const RID &);
  575. MODBIND2(font_set_embolden, const RID &, double);
  576. MODBIND1RC(double, font_get_embolden, const RID &);
  577. MODBIND3(font_set_spacing, const RID &, SpacingType, int64_t);
  578. MODBIND2RC(int64_t, font_get_spacing, const RID &, SpacingType);
  579. MODBIND2(font_set_baseline_offset, const RID &, double);
  580. MODBIND1RC(double, font_get_baseline_offset, const RID &);
  581. MODBIND2(font_set_transform, const RID &, const Transform2D &);
  582. MODBIND1RC(Transform2D, font_get_transform, const RID &);
  583. MODBIND2(font_set_variation_coordinates, const RID &, const Dictionary &);
  584. MODBIND1RC(Dictionary, font_get_variation_coordinates, const RID &);
  585. MODBIND2(font_set_hinting, const RID &, TextServer::Hinting);
  586. MODBIND1RC(TextServer::Hinting, font_get_hinting, const RID &);
  587. MODBIND1RC(TypedArray<Vector2i>, font_get_size_cache_list, const RID &);
  588. MODBIND1(font_clear_size_cache, const RID &);
  589. MODBIND2(font_remove_size_cache, const RID &, const Vector2i &);
  590. MODBIND1RC(TypedArray<Dictionary>, font_get_size_cache_info, const RID &);
  591. MODBIND3(font_set_ascent, const RID &, int64_t, double);
  592. MODBIND2RC(double, font_get_ascent, const RID &, int64_t);
  593. MODBIND3(font_set_descent, const RID &, int64_t, double);
  594. MODBIND2RC(double, font_get_descent, const RID &, int64_t);
  595. MODBIND3(font_set_underline_position, const RID &, int64_t, double);
  596. MODBIND2RC(double, font_get_underline_position, const RID &, int64_t);
  597. MODBIND3(font_set_underline_thickness, const RID &, int64_t, double);
  598. MODBIND2RC(double, font_get_underline_thickness, const RID &, int64_t);
  599. MODBIND3(font_set_scale, const RID &, int64_t, double);
  600. MODBIND2RC(double, font_get_scale, const RID &, int64_t);
  601. MODBIND2RC(int64_t, font_get_texture_count, const RID &, const Vector2i &);
  602. MODBIND2(font_clear_textures, const RID &, const Vector2i &);
  603. MODBIND3(font_remove_texture, const RID &, const Vector2i &, int64_t);
  604. MODBIND4(font_set_texture_image, const RID &, const Vector2i &, int64_t, const Ref<Image> &);
  605. MODBIND3RC(Ref<Image>, font_get_texture_image, const RID &, const Vector2i &, int64_t);
  606. MODBIND4(font_set_texture_offsets, const RID &, const Vector2i &, int64_t, const PackedInt32Array &);
  607. MODBIND3RC(PackedInt32Array, font_get_texture_offsets, const RID &, const Vector2i &, int64_t);
  608. MODBIND2RC(PackedInt32Array, font_get_glyph_list, const RID &, const Vector2i &);
  609. MODBIND2(font_clear_glyphs, const RID &, const Vector2i &);
  610. MODBIND3(font_remove_glyph, const RID &, const Vector2i &, int64_t);
  611. MODBIND3RC(Vector2, font_get_glyph_advance, const RID &, int64_t, int64_t);
  612. MODBIND4(font_set_glyph_advance, const RID &, int64_t, int64_t, const Vector2 &);
  613. MODBIND3RC(Vector2, font_get_glyph_offset, const RID &, const Vector2i &, int64_t);
  614. MODBIND4(font_set_glyph_offset, const RID &, const Vector2i &, int64_t, const Vector2 &);
  615. MODBIND3RC(Vector2, font_get_glyph_size, const RID &, const Vector2i &, int64_t);
  616. MODBIND4(font_set_glyph_size, const RID &, const Vector2i &, int64_t, const Vector2 &);
  617. MODBIND3RC(Rect2, font_get_glyph_uv_rect, const RID &, const Vector2i &, int64_t);
  618. MODBIND4(font_set_glyph_uv_rect, const RID &, const Vector2i &, int64_t, const Rect2 &);
  619. MODBIND3RC(int64_t, font_get_glyph_texture_idx, const RID &, const Vector2i &, int64_t);
  620. MODBIND4(font_set_glyph_texture_idx, const RID &, const Vector2i &, int64_t, int64_t);
  621. MODBIND3RC(RID, font_get_glyph_texture_rid, const RID &, const Vector2i &, int64_t);
  622. MODBIND3RC(Size2, font_get_glyph_texture_size, const RID &, const Vector2i &, int64_t);
  623. MODBIND3RC(Dictionary, font_get_glyph_contours, const RID &, int64_t, int64_t);
  624. MODBIND2RC(TypedArray<Vector2i>, font_get_kerning_list, const RID &, int64_t);
  625. MODBIND2(font_clear_kerning_map, const RID &, int64_t);
  626. MODBIND3(font_remove_kerning, const RID &, int64_t, const Vector2i &);
  627. MODBIND4(font_set_kerning, const RID &, int64_t, const Vector2i &, const Vector2 &);
  628. MODBIND3RC(Vector2, font_get_kerning, const RID &, int64_t, const Vector2i &);
  629. MODBIND4RC(int64_t, font_get_glyph_index, const RID &, int64_t, int64_t, int64_t);
  630. MODBIND3RC(int64_t, font_get_char_from_glyph_index, const RID &, int64_t, int64_t);
  631. MODBIND2RC(bool, font_has_char, const RID &, int64_t);
  632. MODBIND1RC(String, font_get_supported_chars, const RID &);
  633. MODBIND1RC(PackedInt32Array, font_get_supported_glyphs, const RID &);
  634. MODBIND4(font_render_range, const RID &, const Vector2i &, int64_t, int64_t);
  635. MODBIND3(font_render_glyph, const RID &, const Vector2i &, int64_t);
  636. MODBIND7C(font_draw_glyph, const RID &, const RID &, int64_t, const Vector2 &, int64_t, const Color &, float);
  637. MODBIND8C(font_draw_glyph_outline, const RID &, const RID &, int64_t, int64_t, const Vector2 &, int64_t, const Color &, float);
  638. MODBIND2RC(bool, font_is_language_supported, const RID &, const String &);
  639. MODBIND3(font_set_language_support_override, const RID &, const String &, bool);
  640. MODBIND2R(bool, font_get_language_support_override, const RID &, const String &);
  641. MODBIND2(font_remove_language_support_override, const RID &, const String &);
  642. MODBIND1R(PackedStringArray, font_get_language_support_overrides, const RID &);
  643. MODBIND2RC(bool, font_is_script_supported, const RID &, const String &);
  644. MODBIND3(font_set_script_support_override, const RID &, const String &, bool);
  645. MODBIND2R(bool, font_get_script_support_override, const RID &, const String &);
  646. MODBIND2(font_remove_script_support_override, const RID &, const String &);
  647. MODBIND1R(PackedStringArray, font_get_script_support_overrides, const RID &);
  648. MODBIND2(font_set_opentype_feature_overrides, const RID &, const Dictionary &);
  649. MODBIND1RC(Dictionary, font_get_opentype_feature_overrides, const RID &);
  650. MODBIND1RC(Dictionary, font_supported_feature_list, const RID &);
  651. MODBIND1RC(Dictionary, font_supported_variation_list, const RID &);
  652. MODBIND1(reference_oversampling_level, double);
  653. MODBIND1(unreference_oversampling_level, double);
  654. /* Shaped text buffer interface */
  655. MODBIND2R(RID, create_shaped_text, Direction, Orientation);
  656. MODBIND1(shaped_text_clear, const RID &);
  657. MODBIND2(shaped_text_set_direction, const RID &, Direction);
  658. MODBIND1RC(Direction, shaped_text_get_direction, const RID &);
  659. MODBIND1RC(Direction, shaped_text_get_inferred_direction, const RID &);
  660. MODBIND2(shaped_text_set_bidi_override, const RID &, const Array &);
  661. MODBIND2(shaped_text_set_custom_punctuation, const RID &, const String &);
  662. MODBIND1RC(String, shaped_text_get_custom_punctuation, const RID &);
  663. MODBIND2(shaped_text_set_custom_ellipsis, const RID &, int64_t);
  664. MODBIND1RC(int64_t, shaped_text_get_custom_ellipsis, const RID &);
  665. MODBIND2(shaped_text_set_orientation, const RID &, Orientation);
  666. MODBIND1RC(Orientation, shaped_text_get_orientation, const RID &);
  667. MODBIND2(shaped_text_set_preserve_invalid, const RID &, bool);
  668. MODBIND1RC(bool, shaped_text_get_preserve_invalid, const RID &);
  669. MODBIND2(shaped_text_set_preserve_control, const RID &, bool);
  670. MODBIND1RC(bool, shaped_text_get_preserve_control, const RID &);
  671. MODBIND3(shaped_text_set_spacing, const RID &, SpacingType, int64_t);
  672. MODBIND2RC(int64_t, shaped_text_get_spacing, const RID &, SpacingType);
  673. MODBIND7R(bool, shaped_text_add_string, const RID &, const String &, const TypedArray<RID> &, int64_t, const Dictionary &, const String &, const Variant &);
  674. MODBIND6R(bool, shaped_text_add_object, const RID &, const Variant &, const Size2 &, InlineAlignment, int64_t, double);
  675. MODBIND5R(bool, shaped_text_resize_object, const RID &, const Variant &, const Size2 &, InlineAlignment, double);
  676. MODBIND1RC(String, shaped_get_text, const RID &);
  677. MODBIND1RC(int64_t, shaped_get_span_count, const RID &);
  678. MODBIND2RC(Variant, shaped_get_span_meta, const RID &, int64_t);
  679. MODBIND2RC(Variant, shaped_get_span_embedded_object, const RID &, int64_t);
  680. MODBIND2RC(String, shaped_get_span_text, const RID &, int64_t);
  681. MODBIND2RC(Variant, shaped_get_span_object, const RID &, int64_t);
  682. MODBIND5(shaped_set_span_update_font, const RID &, int64_t, const TypedArray<RID> &, int64_t, const Dictionary &);
  683. MODBIND1RC(int64_t, shaped_get_run_count, const RID &);
  684. MODBIND2RC(String, shaped_get_run_text, const RID &, int64_t);
  685. MODBIND2RC(Vector2i, shaped_get_run_range, const RID &, int64_t);
  686. MODBIND2RC(RID, shaped_get_run_font_rid, const RID &, int64_t);
  687. MODBIND2RC(int, shaped_get_run_font_size, const RID &, int64_t);
  688. MODBIND2RC(String, shaped_get_run_language, const RID &, int64_t);
  689. MODBIND2RC(Direction, shaped_get_run_direction, const RID &, int64_t);
  690. MODBIND2RC(Variant, shaped_get_run_object, const RID &, int64_t);
  691. MODBIND3RC(RID, shaped_text_substr, const RID &, int64_t, int64_t);
  692. MODBIND1RC(RID, shaped_text_get_parent, const RID &);
  693. MODBIND3R(double, shaped_text_fit_to_width, const RID &, double, BitField<TextServer::JustificationFlag>);
  694. MODBIND2R(double, shaped_text_tab_align, const RID &, const PackedFloat32Array &);
  695. MODBIND1R(bool, shaped_text_shape, const RID &);
  696. MODBIND1R(bool, shaped_text_update_breaks, const RID &);
  697. MODBIND1R(bool, shaped_text_update_justification_ops, const RID &);
  698. MODBIND1RC(int64_t, shaped_text_get_trim_pos, const RID &);
  699. MODBIND1RC(int64_t, shaped_text_get_ellipsis_pos, const RID &);
  700. MODBIND1RC(const Glyph *, shaped_text_get_ellipsis_glyphs, const RID &);
  701. MODBIND1RC(int64_t, shaped_text_get_ellipsis_glyph_count, const RID &);
  702. MODBIND3(shaped_text_overrun_trim_to_width, const RID &, double, BitField<TextServer::TextOverrunFlag>);
  703. MODBIND1RC(bool, shaped_text_is_ready, const RID &);
  704. MODBIND1RC(const Glyph *, shaped_text_get_glyphs, const RID &);
  705. MODBIND1R(const Glyph *, shaped_text_sort_logical, const RID &);
  706. MODBIND1RC(int64_t, shaped_text_get_glyph_count, const RID &);
  707. MODBIND1RC(Vector2i, shaped_text_get_range, const RID &);
  708. MODBIND1RC(Array, shaped_text_get_objects, const RID &);
  709. MODBIND2RC(Rect2, shaped_text_get_object_rect, const RID &, const Variant &);
  710. MODBIND2RC(Vector2i, shaped_text_get_object_range, const RID &, const Variant &);
  711. MODBIND2RC(int64_t, shaped_text_get_object_glyph, const RID &, const Variant &);
  712. MODBIND1RC(Size2, shaped_text_get_size, const RID &);
  713. MODBIND1RC(double, shaped_text_get_ascent, const RID &);
  714. MODBIND1RC(double, shaped_text_get_descent, const RID &);
  715. MODBIND1RC(double, shaped_text_get_width, const RID &);
  716. MODBIND1RC(double, shaped_text_get_underline_position, const RID &);
  717. MODBIND1RC(double, shaped_text_get_underline_thickness, const RID &);
  718. MODBIND1RC(PackedInt32Array, shaped_text_get_character_breaks, const RID &);
  719. MODBIND3RC(PackedInt32Array, string_get_word_breaks, const String &, const String &, int64_t);
  720. MODBIND2RC(String, string_to_upper, const String &, const String &);
  721. MODBIND2RC(String, string_to_lower, const String &, const String &);
  722. MODBIND2RC(String, string_to_title, const String &, const String &);
  723. MODBIND0(cleanup);
  724. TextServerFallback();
  725. ~TextServerFallback();
  726. };