text_server_fb.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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. #ifndef TEXT_SERVER_FB_H
  31. #define TEXT_SERVER_FB_H
  32. /*************************************************************************/
  33. /* Fallback Text Server provides simplified TS functionality, without */
  34. /* BiDi, shaping and advanced font features support. */
  35. /*************************************************************************/
  36. #ifdef GDEXTENSION
  37. // Headers for building as GDExtension plug-in.
  38. #include <godot_cpp/godot.hpp>
  39. #include <godot_cpp/core/class_db.hpp>
  40. #include <godot_cpp/core/ext_wrappers.gen.inc>
  41. #include <godot_cpp/core/mutex_lock.hpp>
  42. #include <godot_cpp/variant/array.hpp>
  43. #include <godot_cpp/variant/dictionary.hpp>
  44. #include <godot_cpp/variant/packed_int32_array.hpp>
  45. #include <godot_cpp/variant/packed_string_array.hpp>
  46. #include <godot_cpp/variant/packed_vector2_array.hpp>
  47. #include <godot_cpp/variant/rect2.hpp>
  48. #include <godot_cpp/variant/rid.hpp>
  49. #include <godot_cpp/variant/string.hpp>
  50. #include <godot_cpp/variant/typed_array.hpp>
  51. #include <godot_cpp/variant/vector2.hpp>
  52. #include <godot_cpp/variant/vector2i.hpp>
  53. #include <godot_cpp/classes/text_server.hpp>
  54. #include <godot_cpp/classes/text_server_extension.hpp>
  55. #include <godot_cpp/classes/text_server_manager.hpp>
  56. #include <godot_cpp/classes/caret_info.hpp>
  57. #include <godot_cpp/classes/global_constants_binds.hpp>
  58. #include <godot_cpp/classes/glyph.hpp>
  59. #include <godot_cpp/classes/image.hpp>
  60. #include <godot_cpp/classes/image_texture.hpp>
  61. #include <godot_cpp/classes/ref.hpp>
  62. #include <godot_cpp/classes/worker_thread_pool.hpp>
  63. #include <godot_cpp/templates/hash_map.hpp>
  64. #include <godot_cpp/templates/hash_set.hpp>
  65. #include <godot_cpp/templates/rid_owner.hpp>
  66. #include <godot_cpp/templates/vector.hpp>
  67. using namespace godot;
  68. #else
  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 "scene/resources/image_texture.h"
  75. #include "servers/text/text_server_extension.h"
  76. #include "modules/modules_enabled.gen.h" // For freetype, msdfgen, svg.
  77. #endif
  78. // Thirdparty headers.
  79. #ifdef MODULE_FREETYPE_ENABLED
  80. #include <ft2build.h>
  81. #include FT_FREETYPE_H
  82. #include FT_TRUETYPE_TABLES_H
  83. #include FT_STROKER_H
  84. #include FT_ADVANCES_H
  85. #include FT_MULTIPLE_MASTERS_H
  86. #include FT_BBOX_H
  87. #include FT_MODULE_H
  88. #include FT_CONFIG_OPTIONS_H
  89. #if !defined(FT_CONFIG_OPTION_USE_BROTLI) && !defined(_MSC_VER)
  90. #warning FreeType is configured without Brotli support, built-in fonts will not be available.
  91. #endif
  92. #endif
  93. /*************************************************************************/
  94. class TextServerFallback : public TextServerExtension {
  95. GDCLASS(TextServerFallback, TextServerExtension);
  96. _THREAD_SAFE_CLASS_
  97. HashMap<StringName, int32_t> feature_sets;
  98. HashMap<int32_t, StringName> feature_sets_inv;
  99. void _insert_feature_sets();
  100. _FORCE_INLINE_ void _insert_feature(const StringName &p_name, int32_t p_tag);
  101. // Font cache data.
  102. #ifdef MODULE_FREETYPE_ENABLED
  103. mutable FT_Library ft_library = nullptr;
  104. #endif
  105. const int rect_range = 1;
  106. struct FontTexturePosition {
  107. int32_t index = -1;
  108. int32_t x = 0;
  109. int32_t y = 0;
  110. FontTexturePosition() {}
  111. FontTexturePosition(int32_t p_id, int32_t p_x, int32_t p_y) :
  112. index(p_id), x(p_x), y(p_y) {}
  113. };
  114. struct Shelf {
  115. int32_t x = 0;
  116. int32_t y = 0;
  117. int32_t w = 0;
  118. int32_t h = 0;
  119. FontTexturePosition alloc_shelf(int32_t p_id, int32_t p_w, int32_t p_h) {
  120. if (p_w > w || p_h > h) {
  121. return FontTexturePosition(-1, 0, 0);
  122. }
  123. int32_t xx = x;
  124. x += p_w;
  125. w -= p_w;
  126. return FontTexturePosition(p_id, xx, y);
  127. }
  128. Shelf() {}
  129. Shelf(int32_t p_x, int32_t p_y, int32_t p_w, int32_t p_h) :
  130. x(p_x), y(p_y), w(p_w), h(p_h) {}
  131. };
  132. struct ShelfPackTexture {
  133. int32_t texture_w = 1024;
  134. int32_t texture_h = 1024;
  135. Image::Format format;
  136. PackedByteArray imgdata;
  137. Ref<ImageTexture> texture;
  138. bool dirty = true;
  139. List<Shelf> shelves;
  140. FontTexturePosition pack_rect(int32_t p_id, int32_t p_h, int32_t p_w) {
  141. int32_t y = 0;
  142. int32_t waste = 0;
  143. Shelf *best_shelf = nullptr;
  144. int32_t best_waste = std::numeric_limits<std::int32_t>::max();
  145. for (Shelf &E : shelves) {
  146. y += E.h;
  147. if (p_w > E.w) {
  148. continue;
  149. }
  150. if (p_h == E.h) {
  151. return E.alloc_shelf(p_id, p_w, p_h);
  152. }
  153. if (p_h > E.h) {
  154. continue;
  155. }
  156. if (p_h < E.h) {
  157. waste = (E.h - p_h) * p_w;
  158. if (waste < best_waste) {
  159. best_waste = waste;
  160. best_shelf = &E;
  161. }
  162. }
  163. }
  164. if (best_shelf) {
  165. return best_shelf->alloc_shelf(p_id, p_w, p_h);
  166. }
  167. if (p_h <= (texture_h - y) && p_w <= texture_w) {
  168. List<Shelf>::Element *E = shelves.push_back(Shelf(0, y, texture_w, p_h));
  169. return E->get().alloc_shelf(p_id, p_w, p_h);
  170. }
  171. return FontTexturePosition(-1, 0, 0);
  172. }
  173. ShelfPackTexture() {}
  174. ShelfPackTexture(int32_t p_w, int32_t p_h) :
  175. texture_w(p_w), texture_h(p_h) {}
  176. };
  177. struct FontGlyph {
  178. bool found = false;
  179. int texture_idx = -1;
  180. Rect2 rect;
  181. Rect2 uv_rect;
  182. Vector2 advance;
  183. };
  184. struct FontForSizeFallback {
  185. double ascent = 0.0;
  186. double descent = 0.0;
  187. double underline_position = 0.0;
  188. double underline_thickness = 0.0;
  189. double scale = 1.0;
  190. double oversampling = 1.0;
  191. Vector2i size;
  192. Vector<ShelfPackTexture> textures;
  193. HashMap<int32_t, FontGlyph> glyph_map;
  194. HashMap<Vector2i, Vector2> kerning_map;
  195. #ifdef MODULE_FREETYPE_ENABLED
  196. FT_Face face = nullptr;
  197. FT_StreamRec stream;
  198. #endif
  199. ~FontForSizeFallback() {
  200. #ifdef MODULE_FREETYPE_ENABLED
  201. if (face != nullptr) {
  202. FT_Done_Face(face);
  203. }
  204. #endif
  205. }
  206. };
  207. struct FontFallbackLinkedVariation {
  208. RID base_font;
  209. int extra_spacing[4] = { 0, 0, 0, 0 };
  210. };
  211. struct FontFallback {
  212. Mutex mutex;
  213. TextServer::FontAntialiasing antialiasing = TextServer::FONT_ANTIALIASING_GRAY;
  214. bool mipmaps = false;
  215. bool msdf = false;
  216. FixedSizeScaleMode fixed_size_scale_mode = FIXED_SIZE_SCALE_DISABLE;
  217. int msdf_range = 14;
  218. int msdf_source_size = 48;
  219. int fixed_size = 0;
  220. bool force_autohinter = false;
  221. bool allow_system_fallback = true;
  222. TextServer::Hinting hinting = TextServer::HINTING_LIGHT;
  223. TextServer::SubpixelPositioning subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_AUTO;
  224. Dictionary variation_coordinates;
  225. double oversampling = 0.0;
  226. double embolden = 0.0;
  227. Transform2D transform;
  228. BitField<TextServer::FontStyle> style_flags = 0;
  229. String font_name;
  230. String style_name;
  231. int weight = 400;
  232. int stretch = 100;
  233. int extra_spacing[4] = { 0, 0, 0, 0 };
  234. HashMap<Vector2i, FontForSizeFallback *, VariantHasher, VariantComparator> cache;
  235. bool face_init = false;
  236. Dictionary supported_varaitions;
  237. Dictionary feature_overrides;
  238. // Language/script support override.
  239. HashMap<String, bool> language_support_overrides;
  240. HashMap<String, bool> script_support_overrides;
  241. PackedByteArray data;
  242. const uint8_t *data_ptr;
  243. size_t data_size;
  244. int face_index = 0;
  245. ~FontFallback() {
  246. for (const KeyValue<Vector2i, FontForSizeFallback *> &E : cache) {
  247. memdelete(E.value);
  248. }
  249. cache.clear();
  250. }
  251. };
  252. _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;
  253. #ifdef MODULE_MSDFGEN_ENABLED
  254. _FORCE_INLINE_ FontGlyph rasterize_msdf(FontFallback *p_font_data, FontForSizeFallback *p_data, int p_pixel_range, int p_rect_margin, FT_Outline *outline, const Vector2 &advance) const;
  255. #endif
  256. #ifdef MODULE_FREETYPE_ENABLED
  257. _FORCE_INLINE_ FontGlyph rasterize_bitmap(FontForSizeFallback *p_data, int p_rect_margin, FT_Bitmap bitmap, int yofs, int xofs, const Vector2 &advance, bool p_bgra) const;
  258. #endif
  259. _FORCE_INLINE_ bool _ensure_glyph(FontFallback *p_font_data, const Vector2i &p_size, int32_t p_glyph) const;
  260. _FORCE_INLINE_ bool _ensure_cache_for_size(FontFallback *p_font_data, const Vector2i &p_size) const;
  261. _FORCE_INLINE_ void _font_clear_cache(FontFallback *p_font_data);
  262. static void _generateMTSDF_threaded(void *p_td, uint32_t p_y);
  263. _FORCE_INLINE_ Vector2i _get_size(const FontFallback *p_font_data, int p_size) const {
  264. if (p_font_data->msdf) {
  265. return Vector2i(p_font_data->msdf_source_size, 0);
  266. } else if (p_font_data->fixed_size > 0) {
  267. return Vector2i(p_font_data->fixed_size, 0);
  268. } else {
  269. return Vector2i(p_size, 0);
  270. }
  271. }
  272. _FORCE_INLINE_ Vector2i _get_size_outline(const FontFallback *p_font_data, const Vector2i &p_size) const {
  273. if (p_font_data->msdf) {
  274. return Vector2i(p_font_data->msdf_source_size, 0);
  275. } else if (p_font_data->fixed_size > 0) {
  276. return Vector2i(p_font_data->fixed_size, MIN(p_size.y, 1));
  277. } else {
  278. return p_size;
  279. }
  280. }
  281. _FORCE_INLINE_ int _font_get_weight_by_name(const String &p_sty_name) const {
  282. String sty_name = p_sty_name.replace(" ", "").replace("-", "");
  283. if (sty_name.find("thin") >= 0 || sty_name.find("hairline") >= 0) {
  284. return 100;
  285. } else if (sty_name.find("extralight") >= 0 || sty_name.find("ultralight") >= 0) {
  286. return 200;
  287. } else if (sty_name.find("light") >= 0) {
  288. return 300;
  289. } else if (sty_name.find("semilight") >= 0) {
  290. return 350;
  291. } else if (sty_name.find("regular") >= 0) {
  292. return 400;
  293. } else if (sty_name.find("medium") >= 0) {
  294. return 500;
  295. } else if (sty_name.find("semibold") >= 0 || sty_name.find("demibold") >= 0) {
  296. return 600;
  297. } else if (sty_name.find("bold") >= 0) {
  298. return 700;
  299. } else if (sty_name.find("extrabold") >= 0 || sty_name.find("ultrabold") >= 0) {
  300. return 800;
  301. } else if (sty_name.find("black") >= 0 || sty_name.find("heavy") >= 0) {
  302. return 900;
  303. } else if (sty_name.find("extrablack") >= 0 || sty_name.find("ultrablack") >= 0) {
  304. return 950;
  305. }
  306. return 400;
  307. }
  308. _FORCE_INLINE_ int _font_get_stretch_by_name(const String &p_sty_name) const {
  309. String sty_name = p_sty_name.replace(" ", "").replace("-", "");
  310. if (sty_name.find("ultracondensed") >= 0) {
  311. return 50;
  312. } else if (sty_name.find("extracondensed") >= 0) {
  313. return 63;
  314. } else if (sty_name.find("condensed") >= 0) {
  315. return 75;
  316. } else if (sty_name.find("semicondensed") >= 0) {
  317. return 87;
  318. } else if (sty_name.find("semiexpanded") >= 0) {
  319. return 113;
  320. } else if (sty_name.find("expanded") >= 0) {
  321. return 125;
  322. } else if (sty_name.find("extraexpanded") >= 0) {
  323. return 150;
  324. } else if (sty_name.find("ultraexpanded") >= 0) {
  325. return 200;
  326. }
  327. return 100;
  328. }
  329. _FORCE_INLINE_ bool _is_ital_style(const String &p_sty_name) const {
  330. return (p_sty_name.find("italic") >= 0) || (p_sty_name.find("oblique") >= 0);
  331. }
  332. // Shaped text cache data.
  333. struct TrimData {
  334. int trim_pos = -1;
  335. int ellipsis_pos = -1;
  336. Vector<Glyph> ellipsis_glyph_buf;
  337. };
  338. struct ShapedTextDataFallback {
  339. Mutex mutex;
  340. /* Source data */
  341. RID parent; // Substring parent ShapedTextData.
  342. int start = 0; // Substring start offset in the parent string.
  343. int end = 0; // Substring end offset in the parent string.
  344. String text;
  345. String custom_punct;
  346. TextServer::Direction direction = DIRECTION_LTR; // Desired text direction.
  347. TextServer::Orientation orientation = ORIENTATION_HORIZONTAL;
  348. struct Span {
  349. int start = -1;
  350. int end = -1;
  351. Array fonts;
  352. int font_size = 0;
  353. Variant embedded_key;
  354. String language;
  355. Dictionary features;
  356. Variant meta;
  357. };
  358. Vector<Span> spans;
  359. struct EmbeddedObject {
  360. int pos = 0;
  361. InlineAlignment inline_align = INLINE_ALIGNMENT_CENTER;
  362. Rect2 rect;
  363. double baseline = 0;
  364. };
  365. HashMap<Variant, EmbeddedObject, VariantHasher, VariantComparator> objects;
  366. /* Shaped data */
  367. TextServer::Direction para_direction = DIRECTION_LTR; // Detected text direction.
  368. bool valid = false; // String is shaped.
  369. bool line_breaks_valid = false; // Line and word break flags are populated (and virtual zero width spaces inserted).
  370. bool justification_ops_valid = false; // Virtual elongation glyphs are added to the string.
  371. bool sort_valid = false;
  372. bool text_trimmed = false;
  373. bool preserve_invalid = true; // Draw hex code box instead of missing characters.
  374. bool preserve_control = false; // Draw control characters.
  375. double ascent = 0.0; // Ascent for horizontal layout, 1/2 of width for vertical.
  376. double descent = 0.0; // Descent for horizontal layout, 1/2 of width for vertical.
  377. double width = 0.0; // Width for horizontal layout, height for vertical.
  378. double width_trimmed = 0.0;
  379. int extra_spacing[4] = { 0, 0, 0, 0 };
  380. double upos = 0.0;
  381. double uthk = 0.0;
  382. TrimData overrun_trim_data;
  383. bool fit_width_minimum_reached = false;
  384. Vector<Glyph> glyphs;
  385. Vector<Glyph> glyphs_logical;
  386. };
  387. // Common data.
  388. double oversampling = 1.0;
  389. mutable RID_PtrOwner<FontFallbackLinkedVariation> font_var_owner;
  390. mutable RID_PtrOwner<FontFallback> font_owner;
  391. mutable RID_PtrOwner<ShapedTextDataFallback> shaped_owner;
  392. _FORCE_INLINE_ FontFallback *_get_font_data(const RID &p_font_rid) const {
  393. RID rid = p_font_rid;
  394. FontFallbackLinkedVariation *fdv = font_var_owner.get_or_null(rid);
  395. if (unlikely(fdv)) {
  396. rid = fdv->base_font;
  397. }
  398. return font_owner.get_or_null(rid);
  399. }
  400. struct SystemFontKey {
  401. String font_name;
  402. TextServer::FontAntialiasing antialiasing = TextServer::FONT_ANTIALIASING_GRAY;
  403. bool italic = false;
  404. bool mipmaps = false;
  405. bool msdf = false;
  406. bool force_autohinter = false;
  407. int weight = 400;
  408. int stretch = 100;
  409. int msdf_range = 14;
  410. int msdf_source_size = 48;
  411. int fixed_size = 0;
  412. TextServer::Hinting hinting = TextServer::HINTING_LIGHT;
  413. TextServer::SubpixelPositioning subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_AUTO;
  414. Dictionary variation_coordinates;
  415. double oversampling = 0.0;
  416. double embolden = 0.0;
  417. Transform2D transform;
  418. int extra_spacing[4] = { 0, 0, 0, 0 };
  419. bool operator==(const SystemFontKey &p_b) const {
  420. return (font_name == p_b.font_name) && (antialiasing == p_b.antialiasing) && (italic == p_b.italic) && (mipmaps == p_b.mipmaps) && (msdf == p_b.msdf) && (force_autohinter == p_b.force_autohinter) && (weight == p_b.weight) && (stretch == p_b.stretch) && (msdf_range == p_b.msdf_range) && (msdf_source_size == p_b.msdf_source_size) && (fixed_size == p_b.fixed_size) && (hinting == p_b.hinting) && (subpixel_positioning == p_b.subpixel_positioning) && (variation_coordinates == p_b.variation_coordinates) && (oversampling == p_b.oversampling) && (embolden == p_b.embolden) && (transform == p_b.transform) && (extra_spacing[SPACING_TOP] == p_b.extra_spacing[SPACING_TOP]) && (extra_spacing[SPACING_BOTTOM] == p_b.extra_spacing[SPACING_BOTTOM]) && (extra_spacing[SPACING_SPACE] == p_b.extra_spacing[SPACING_SPACE]) && (extra_spacing[SPACING_GLYPH] == p_b.extra_spacing[SPACING_GLYPH]);
  421. }
  422. SystemFontKey(const String &p_font_name, bool p_italic, int p_weight, int p_stretch, RID p_font, const TextServerFallback *p_fb) {
  423. font_name = p_font_name;
  424. italic = p_italic;
  425. weight = p_weight;
  426. stretch = p_stretch;
  427. antialiasing = p_fb->_font_get_antialiasing(p_font);
  428. mipmaps = p_fb->_font_get_generate_mipmaps(p_font);
  429. msdf = p_fb->_font_is_multichannel_signed_distance_field(p_font);
  430. msdf_range = p_fb->_font_get_msdf_pixel_range(p_font);
  431. msdf_source_size = p_fb->_font_get_msdf_size(p_font);
  432. fixed_size = p_fb->_font_get_fixed_size(p_font);
  433. force_autohinter = p_fb->_font_is_force_autohinter(p_font);
  434. hinting = p_fb->_font_get_hinting(p_font);
  435. subpixel_positioning = p_fb->_font_get_subpixel_positioning(p_font);
  436. variation_coordinates = p_fb->_font_get_variation_coordinates(p_font);
  437. oversampling = p_fb->_font_get_oversampling(p_font);
  438. embolden = p_fb->_font_get_embolden(p_font);
  439. transform = p_fb->_font_get_transform(p_font);
  440. extra_spacing[SPACING_TOP] = p_fb->_font_get_spacing(p_font, SPACING_TOP);
  441. extra_spacing[SPACING_BOTTOM] = p_fb->_font_get_spacing(p_font, SPACING_BOTTOM);
  442. extra_spacing[SPACING_SPACE] = p_fb->_font_get_spacing(p_font, SPACING_SPACE);
  443. extra_spacing[SPACING_GLYPH] = p_fb->_font_get_spacing(p_font, SPACING_GLYPH);
  444. }
  445. };
  446. struct SystemFontCacheRec {
  447. RID rid;
  448. int index = 0;
  449. };
  450. struct SystemFontCache {
  451. Vector<SystemFontCacheRec> var;
  452. int max_var = 0;
  453. };
  454. struct SystemFontKeyHasher {
  455. _FORCE_INLINE_ static uint32_t hash(const SystemFontKey &p_a) {
  456. uint32_t hash = p_a.font_name.hash();
  457. hash = hash_murmur3_one_32(p_a.variation_coordinates.hash(), hash);
  458. hash = hash_murmur3_one_32(p_a.weight, hash);
  459. hash = hash_murmur3_one_32(p_a.stretch, hash);
  460. hash = hash_murmur3_one_32(p_a.msdf_range, hash);
  461. hash = hash_murmur3_one_32(p_a.msdf_source_size, hash);
  462. hash = hash_murmur3_one_32(p_a.fixed_size, hash);
  463. hash = hash_murmur3_one_double(p_a.oversampling, hash);
  464. hash = hash_murmur3_one_double(p_a.embolden, hash);
  465. hash = hash_murmur3_one_real(p_a.transform[0].x, hash);
  466. hash = hash_murmur3_one_real(p_a.transform[0].y, hash);
  467. hash = hash_murmur3_one_real(p_a.transform[1].x, hash);
  468. hash = hash_murmur3_one_real(p_a.transform[1].y, hash);
  469. hash = hash_murmur3_one_32(p_a.extra_spacing[SPACING_TOP], hash);
  470. hash = hash_murmur3_one_32(p_a.extra_spacing[SPACING_BOTTOM], hash);
  471. hash = hash_murmur3_one_32(p_a.extra_spacing[SPACING_SPACE], hash);
  472. hash = hash_murmur3_one_32(p_a.extra_spacing[SPACING_GLYPH], hash);
  473. return hash_fmix32(hash_murmur3_one_32(((int)p_a.mipmaps) | ((int)p_a.msdf << 1) | ((int)p_a.italic << 2) | ((int)p_a.force_autohinter << 3) | ((int)p_a.hinting << 4) | ((int)p_a.subpixel_positioning << 8) | ((int)p_a.antialiasing << 12), hash));
  474. }
  475. };
  476. mutable HashMap<SystemFontKey, SystemFontCache, SystemFontKeyHasher> system_fonts;
  477. mutable HashMap<String, PackedByteArray> system_font_data;
  478. void _realign(ShapedTextDataFallback *p_sd) const;
  479. Mutex ft_mutex;
  480. protected:
  481. static void _bind_methods(){};
  482. void full_copy(ShapedTextDataFallback *p_shaped);
  483. void invalidate(ShapedTextDataFallback *p_shaped);
  484. public:
  485. MODBIND1RC(bool, has_feature, Feature);
  486. MODBIND0RC(String, get_name);
  487. MODBIND0RC(int64_t, get_features);
  488. MODBIND1(free_rid, const RID &);
  489. MODBIND1R(bool, has, const RID &);
  490. MODBIND1R(bool, load_support_data, const String &);
  491. MODBIND0RC(String, get_support_data_filename);
  492. MODBIND0RC(String, get_support_data_info);
  493. MODBIND1RC(bool, save_support_data, const String &);
  494. MODBIND1RC(bool, is_locale_right_to_left, const String &);
  495. MODBIND1RC(int64_t, name_to_tag, const String &);
  496. MODBIND1RC(String, tag_to_name, int64_t);
  497. /* Font interface */
  498. MODBIND0R(RID, create_font);
  499. MODBIND1R(RID, create_font_linked_variation, const RID &);
  500. MODBIND2(font_set_data, const RID &, const PackedByteArray &);
  501. MODBIND3(font_set_data_ptr, const RID &, const uint8_t *, int64_t);
  502. MODBIND2(font_set_face_index, const RID &, int64_t);
  503. MODBIND1RC(int64_t, font_get_face_index, const RID &);
  504. MODBIND1RC(int64_t, font_get_face_count, const RID &);
  505. MODBIND2(font_set_style, const RID &, BitField<FontStyle>);
  506. MODBIND1RC(BitField<FontStyle>, font_get_style, const RID &);
  507. MODBIND2(font_set_style_name, const RID &, const String &);
  508. MODBIND1RC(String, font_get_style_name, const RID &);
  509. MODBIND2(font_set_weight, const RID &, int64_t);
  510. MODBIND1RC(int64_t, font_get_weight, const RID &);
  511. MODBIND2(font_set_stretch, const RID &, int64_t);
  512. MODBIND1RC(int64_t, font_get_stretch, const RID &);
  513. MODBIND2(font_set_name, const RID &, const String &);
  514. MODBIND1RC(String, font_get_name, const RID &);
  515. MODBIND2(font_set_antialiasing, const RID &, TextServer::FontAntialiasing);
  516. MODBIND1RC(TextServer::FontAntialiasing, font_get_antialiasing, const RID &);
  517. MODBIND2(font_set_generate_mipmaps, const RID &, bool);
  518. MODBIND1RC(bool, font_get_generate_mipmaps, const RID &);
  519. MODBIND2(font_set_multichannel_signed_distance_field, const RID &, bool);
  520. MODBIND1RC(bool, font_is_multichannel_signed_distance_field, const RID &);
  521. MODBIND2(font_set_msdf_pixel_range, const RID &, int64_t);
  522. MODBIND1RC(int64_t, font_get_msdf_pixel_range, const RID &);
  523. MODBIND2(font_set_msdf_size, const RID &, int64_t);
  524. MODBIND1RC(int64_t, font_get_msdf_size, const RID &);
  525. MODBIND2(font_set_fixed_size, const RID &, int64_t);
  526. MODBIND1RC(int64_t, font_get_fixed_size, const RID &);
  527. MODBIND2(font_set_fixed_size_scale_mode, const RID &, FixedSizeScaleMode);
  528. MODBIND1RC(FixedSizeScaleMode, font_get_fixed_size_scale_mode, const RID &);
  529. MODBIND2(font_set_allow_system_fallback, const RID &, bool);
  530. MODBIND1RC(bool, font_is_allow_system_fallback, const RID &);
  531. MODBIND2(font_set_force_autohinter, const RID &, bool);
  532. MODBIND1RC(bool, font_is_force_autohinter, const RID &);
  533. MODBIND2(font_set_subpixel_positioning, const RID &, SubpixelPositioning);
  534. MODBIND1RC(SubpixelPositioning, font_get_subpixel_positioning, const RID &);
  535. MODBIND2(font_set_embolden, const RID &, double);
  536. MODBIND1RC(double, font_get_embolden, const RID &);
  537. MODBIND3(font_set_spacing, const RID &, SpacingType, int64_t);
  538. MODBIND2RC(int64_t, font_get_spacing, const RID &, SpacingType);
  539. MODBIND2(font_set_transform, const RID &, const Transform2D &);
  540. MODBIND1RC(Transform2D, font_get_transform, const RID &);
  541. MODBIND2(font_set_variation_coordinates, const RID &, const Dictionary &);
  542. MODBIND1RC(Dictionary, font_get_variation_coordinates, const RID &);
  543. MODBIND2(font_set_hinting, const RID &, TextServer::Hinting);
  544. MODBIND1RC(TextServer::Hinting, font_get_hinting, const RID &);
  545. MODBIND2(font_set_oversampling, const RID &, double);
  546. MODBIND1RC(double, font_get_oversampling, const RID &);
  547. MODBIND1RC(TypedArray<Vector2i>, font_get_size_cache_list, const RID &);
  548. MODBIND1(font_clear_size_cache, const RID &);
  549. MODBIND2(font_remove_size_cache, const RID &, const Vector2i &);
  550. MODBIND3(font_set_ascent, const RID &, int64_t, double);
  551. MODBIND2RC(double, font_get_ascent, const RID &, int64_t);
  552. MODBIND3(font_set_descent, const RID &, int64_t, double);
  553. MODBIND2RC(double, font_get_descent, const RID &, int64_t);
  554. MODBIND3(font_set_underline_position, const RID &, int64_t, double);
  555. MODBIND2RC(double, font_get_underline_position, const RID &, int64_t);
  556. MODBIND3(font_set_underline_thickness, const RID &, int64_t, double);
  557. MODBIND2RC(double, font_get_underline_thickness, const RID &, int64_t);
  558. MODBIND3(font_set_scale, const RID &, int64_t, double);
  559. MODBIND2RC(double, font_get_scale, const RID &, int64_t);
  560. MODBIND2RC(int64_t, font_get_texture_count, const RID &, const Vector2i &);
  561. MODBIND2(font_clear_textures, const RID &, const Vector2i &);
  562. MODBIND3(font_remove_texture, const RID &, const Vector2i &, int64_t);
  563. MODBIND4(font_set_texture_image, const RID &, const Vector2i &, int64_t, const Ref<Image> &);
  564. MODBIND3RC(Ref<Image>, font_get_texture_image, const RID &, const Vector2i &, int64_t);
  565. MODBIND4(font_set_texture_offsets, const RID &, const Vector2i &, int64_t, const PackedInt32Array &);
  566. MODBIND3RC(PackedInt32Array, font_get_texture_offsets, const RID &, const Vector2i &, int64_t);
  567. MODBIND2RC(PackedInt32Array, font_get_glyph_list, const RID &, const Vector2i &);
  568. MODBIND2(font_clear_glyphs, const RID &, const Vector2i &);
  569. MODBIND3(font_remove_glyph, const RID &, const Vector2i &, int64_t);
  570. MODBIND3RC(Vector2, font_get_glyph_advance, const RID &, int64_t, int64_t);
  571. MODBIND4(font_set_glyph_advance, const RID &, int64_t, int64_t, const Vector2 &);
  572. MODBIND3RC(Vector2, font_get_glyph_offset, const RID &, const Vector2i &, int64_t);
  573. MODBIND4(font_set_glyph_offset, const RID &, const Vector2i &, int64_t, const Vector2 &);
  574. MODBIND3RC(Vector2, font_get_glyph_size, const RID &, const Vector2i &, int64_t);
  575. MODBIND4(font_set_glyph_size, const RID &, const Vector2i &, int64_t, const Vector2 &);
  576. MODBIND3RC(Rect2, font_get_glyph_uv_rect, const RID &, const Vector2i &, int64_t);
  577. MODBIND4(font_set_glyph_uv_rect, const RID &, const Vector2i &, int64_t, const Rect2 &);
  578. MODBIND3RC(int64_t, font_get_glyph_texture_idx, const RID &, const Vector2i &, int64_t);
  579. MODBIND4(font_set_glyph_texture_idx, const RID &, const Vector2i &, int64_t, int64_t);
  580. MODBIND3RC(RID, font_get_glyph_texture_rid, const RID &, const Vector2i &, int64_t);
  581. MODBIND3RC(Size2, font_get_glyph_texture_size, const RID &, const Vector2i &, int64_t);
  582. MODBIND3RC(Dictionary, font_get_glyph_contours, const RID &, int64_t, int64_t);
  583. MODBIND2RC(TypedArray<Vector2i>, font_get_kerning_list, const RID &, int64_t);
  584. MODBIND2(font_clear_kerning_map, const RID &, int64_t);
  585. MODBIND3(font_remove_kerning, const RID &, int64_t, const Vector2i &);
  586. MODBIND4(font_set_kerning, const RID &, int64_t, const Vector2i &, const Vector2 &);
  587. MODBIND3RC(Vector2, font_get_kerning, const RID &, int64_t, const Vector2i &);
  588. MODBIND4RC(int64_t, font_get_glyph_index, const RID &, int64_t, int64_t, int64_t);
  589. MODBIND3RC(int64_t, font_get_char_from_glyph_index, const RID &, int64_t, int64_t);
  590. MODBIND2RC(bool, font_has_char, const RID &, int64_t);
  591. MODBIND1RC(String, font_get_supported_chars, const RID &);
  592. MODBIND4(font_render_range, const RID &, const Vector2i &, int64_t, int64_t);
  593. MODBIND3(font_render_glyph, const RID &, const Vector2i &, int64_t);
  594. MODBIND6C(font_draw_glyph, const RID &, const RID &, int64_t, const Vector2 &, int64_t, const Color &);
  595. MODBIND7C(font_draw_glyph_outline, const RID &, const RID &, int64_t, int64_t, const Vector2 &, int64_t, const Color &);
  596. MODBIND2RC(bool, font_is_language_supported, const RID &, const String &);
  597. MODBIND3(font_set_language_support_override, const RID &, const String &, bool);
  598. MODBIND2R(bool, font_get_language_support_override, const RID &, const String &);
  599. MODBIND2(font_remove_language_support_override, const RID &, const String &);
  600. MODBIND1R(PackedStringArray, font_get_language_support_overrides, const RID &);
  601. MODBIND2RC(bool, font_is_script_supported, const RID &, const String &);
  602. MODBIND3(font_set_script_support_override, const RID &, const String &, bool);
  603. MODBIND2R(bool, font_get_script_support_override, const RID &, const String &);
  604. MODBIND2(font_remove_script_support_override, const RID &, const String &);
  605. MODBIND1R(PackedStringArray, font_get_script_support_overrides, const RID &);
  606. MODBIND2(font_set_opentype_feature_overrides, const RID &, const Dictionary &);
  607. MODBIND1RC(Dictionary, font_get_opentype_feature_overrides, const RID &);
  608. MODBIND1RC(Dictionary, font_supported_feature_list, const RID &);
  609. MODBIND1RC(Dictionary, font_supported_variation_list, const RID &);
  610. MODBIND0RC(double, font_get_global_oversampling);
  611. MODBIND1(font_set_global_oversampling, double);
  612. /* Shaped text buffer interface */
  613. MODBIND2R(RID, create_shaped_text, Direction, Orientation);
  614. MODBIND1(shaped_text_clear, const RID &);
  615. MODBIND2(shaped_text_set_direction, const RID &, Direction);
  616. MODBIND1RC(Direction, shaped_text_get_direction, const RID &);
  617. MODBIND1RC(Direction, shaped_text_get_inferred_direction, const RID &);
  618. MODBIND2(shaped_text_set_bidi_override, const RID &, const Array &);
  619. MODBIND2(shaped_text_set_custom_punctuation, const RID &, const String &);
  620. MODBIND1RC(String, shaped_text_get_custom_punctuation, const RID &);
  621. MODBIND2(shaped_text_set_orientation, const RID &, Orientation);
  622. MODBIND1RC(Orientation, shaped_text_get_orientation, const RID &);
  623. MODBIND2(shaped_text_set_preserve_invalid, const RID &, bool);
  624. MODBIND1RC(bool, shaped_text_get_preserve_invalid, const RID &);
  625. MODBIND2(shaped_text_set_preserve_control, const RID &, bool);
  626. MODBIND1RC(bool, shaped_text_get_preserve_control, const RID &);
  627. MODBIND3(shaped_text_set_spacing, const RID &, SpacingType, int64_t);
  628. MODBIND2RC(int64_t, shaped_text_get_spacing, const RID &, SpacingType);
  629. MODBIND7R(bool, shaped_text_add_string, const RID &, const String &, const TypedArray<RID> &, int64_t, const Dictionary &, const String &, const Variant &);
  630. MODBIND6R(bool, shaped_text_add_object, const RID &, const Variant &, const Size2 &, InlineAlignment, int64_t, double);
  631. MODBIND5R(bool, shaped_text_resize_object, const RID &, const Variant &, const Size2 &, InlineAlignment, double);
  632. MODBIND1RC(int64_t, shaped_get_span_count, const RID &);
  633. MODBIND2RC(Variant, shaped_get_span_meta, const RID &, int64_t);
  634. MODBIND5(shaped_set_span_update_font, const RID &, int64_t, const TypedArray<RID> &, int64_t, const Dictionary &);
  635. MODBIND3RC(RID, shaped_text_substr, const RID &, int64_t, int64_t);
  636. MODBIND1RC(RID, shaped_text_get_parent, const RID &);
  637. MODBIND3R(double, shaped_text_fit_to_width, const RID &, double, BitField<TextServer::JustificationFlag>);
  638. MODBIND2R(double, shaped_text_tab_align, const RID &, const PackedFloat32Array &);
  639. MODBIND1R(bool, shaped_text_shape, const RID &);
  640. MODBIND1R(bool, shaped_text_update_breaks, const RID &);
  641. MODBIND1R(bool, shaped_text_update_justification_ops, const RID &);
  642. MODBIND1RC(int64_t, shaped_text_get_trim_pos, const RID &);
  643. MODBIND1RC(int64_t, shaped_text_get_ellipsis_pos, const RID &);
  644. MODBIND1RC(const Glyph *, shaped_text_get_ellipsis_glyphs, const RID &);
  645. MODBIND1RC(int64_t, shaped_text_get_ellipsis_glyph_count, const RID &);
  646. MODBIND3(shaped_text_overrun_trim_to_width, const RID &, double, BitField<TextServer::TextOverrunFlag>);
  647. MODBIND1RC(bool, shaped_text_is_ready, const RID &);
  648. MODBIND1RC(const Glyph *, shaped_text_get_glyphs, const RID &);
  649. MODBIND1R(const Glyph *, shaped_text_sort_logical, const RID &);
  650. MODBIND1RC(int64_t, shaped_text_get_glyph_count, const RID &);
  651. MODBIND1RC(Vector2i, shaped_text_get_range, const RID &);
  652. MODBIND1RC(Array, shaped_text_get_objects, const RID &);
  653. MODBIND2RC(Rect2, shaped_text_get_object_rect, const RID &, const Variant &);
  654. MODBIND1RC(Size2, shaped_text_get_size, const RID &);
  655. MODBIND1RC(double, shaped_text_get_ascent, const RID &);
  656. MODBIND1RC(double, shaped_text_get_descent, const RID &);
  657. MODBIND1RC(double, shaped_text_get_width, const RID &);
  658. MODBIND1RC(double, shaped_text_get_underline_position, const RID &);
  659. MODBIND1RC(double, shaped_text_get_underline_thickness, const RID &);
  660. MODBIND1RC(PackedInt32Array, shaped_text_get_character_breaks, const RID &);
  661. MODBIND3RC(PackedInt32Array, string_get_word_breaks, const String &, const String &, int64_t);
  662. MODBIND2RC(String, string_to_upper, const String &, const String &);
  663. MODBIND2RC(String, string_to_lower, const String &, const String &);
  664. MODBIND0(cleanup);
  665. TextServerFallback();
  666. ~TextServerFallback();
  667. };
  668. #endif // TEXT_SERVER_FB_H