text_server_adv.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. /**************************************************************************/
  2. /* text_server_adv.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef TEXT_SERVER_ADV_H
  31. #define TEXT_SERVER_ADV_H
  32. /*************************************************************************/
  33. /* ICU/HarfBuzz/Graphite backed Text Server implementation with BiDi, */
  34. /* 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 "servers/text/text_server_extension.h"
  71. #include "core/extension/ext_wrappers.gen.inc"
  72. #include "core/object/worker_thread_pool.h"
  73. #include "core/templates/hash_map.h"
  74. #include "core/templates/rid_owner.h"
  75. #include "scene/resources/texture.h"
  76. #include "modules/modules_enabled.gen.h" // For freetype, msdfgen, svg.
  77. #endif
  78. #include "script_iterator.h"
  79. // Thirdparty headers.
  80. #include <unicode/ubidi.h>
  81. #include <unicode/ubrk.h>
  82. #include <unicode/uchar.h>
  83. #include <unicode/uclean.h>
  84. #include <unicode/udata.h>
  85. #include <unicode/uiter.h>
  86. #include <unicode/uloc.h>
  87. #include <unicode/unorm2.h>
  88. #include <unicode/uscript.h>
  89. #include <unicode/uspoof.h>
  90. #include <unicode/ustring.h>
  91. #include <unicode/utypes.h>
  92. #ifdef MODULE_FREETYPE_ENABLED
  93. #include <ft2build.h>
  94. #include FT_FREETYPE_H
  95. #include FT_TRUETYPE_TABLES_H
  96. #include FT_STROKER_H
  97. #include FT_ADVANCES_H
  98. #include FT_MULTIPLE_MASTERS_H
  99. #include FT_BBOX_H
  100. #include FT_MODULE_H
  101. #include FT_CONFIG_OPTIONS_H
  102. #if !defined(FT_CONFIG_OPTION_USE_BROTLI) && !defined(_MSC_VER)
  103. #warning FreeType is configured without Brotli support, built-in fonts will not be available.
  104. #endif
  105. #include <hb-ft.h>
  106. #include <hb-ot.h>
  107. #endif
  108. #include <hb-icu.h>
  109. #include <hb.h>
  110. /*************************************************************************/
  111. class TextServerAdvanced : public TextServerExtension {
  112. GDCLASS(TextServerAdvanced, TextServerExtension);
  113. _THREAD_SAFE_CLASS_
  114. struct NumSystemData {
  115. HashSet<StringName> lang;
  116. String digits;
  117. String percent_sign;
  118. String exp;
  119. };
  120. Vector<NumSystemData> num_systems;
  121. struct FeatureInfo {
  122. StringName name;
  123. Variant::Type vtype = Variant::INT;
  124. bool hidden = false;
  125. };
  126. HashMap<StringName, int32_t> feature_sets;
  127. HashMap<int32_t, FeatureInfo> feature_sets_inv;
  128. void _insert_num_systems_lang();
  129. void _insert_feature_sets();
  130. _FORCE_INLINE_ void _insert_feature(const StringName &p_name, int32_t p_tag, Variant::Type p_vtype = Variant::INT, bool p_hidden = false);
  131. // ICU support data.
  132. static bool icu_data_loaded;
  133. mutable USet *allowed = nullptr;
  134. mutable USpoofChecker *sc_spoof = nullptr;
  135. mutable USpoofChecker *sc_conf = nullptr;
  136. // Font cache data.
  137. #ifdef MODULE_FREETYPE_ENABLED
  138. mutable FT_Library ft_library = nullptr;
  139. #endif
  140. const int rect_range = 1;
  141. struct FontTexturePosition {
  142. int32_t index = -1;
  143. int32_t x = 0;
  144. int32_t y = 0;
  145. FontTexturePosition() {}
  146. FontTexturePosition(int32_t p_id, int32_t p_x, int32_t p_y) :
  147. index(p_id), x(p_x), y(p_y) {}
  148. };
  149. struct Shelf {
  150. int32_t x = 0;
  151. int32_t y = 0;
  152. int32_t w = 0;
  153. int32_t h = 0;
  154. FontTexturePosition alloc_shelf(int32_t p_id, int32_t p_w, int32_t p_h) {
  155. if (p_w > w || p_h > h) {
  156. return FontTexturePosition(-1, 0, 0);
  157. }
  158. int32_t xx = x;
  159. x += p_w;
  160. w -= p_w;
  161. return FontTexturePosition(p_id, xx, y);
  162. }
  163. Shelf() {}
  164. Shelf(int32_t p_x, int32_t p_y, int32_t p_w, int32_t p_h) :
  165. x(p_x), y(p_y), w(p_w), h(p_h) {}
  166. };
  167. struct ShelfPackTexture {
  168. int32_t texture_w = 1024;
  169. int32_t texture_h = 1024;
  170. Image::Format format;
  171. PackedByteArray imgdata;
  172. Ref<ImageTexture> texture;
  173. bool dirty = true;
  174. List<Shelf> shelves;
  175. FontTexturePosition pack_rect(int32_t p_id, int32_t p_h, int32_t p_w) {
  176. int32_t y = 0;
  177. int32_t waste = 0;
  178. Shelf *best_shelf = nullptr;
  179. int32_t best_waste = std::numeric_limits<std::int32_t>::max();
  180. for (Shelf &E : shelves) {
  181. y += E.h;
  182. if (p_w > E.w) {
  183. continue;
  184. }
  185. if (p_h == E.h) {
  186. return E.alloc_shelf(p_id, p_w, p_h);
  187. }
  188. if (p_h > E.h) {
  189. continue;
  190. }
  191. if (p_h < E.h) {
  192. waste = (E.h - p_h) * p_w;
  193. if (waste < best_waste) {
  194. best_waste = waste;
  195. best_shelf = &E;
  196. }
  197. }
  198. }
  199. if (best_shelf) {
  200. return best_shelf->alloc_shelf(p_id, p_w, p_h);
  201. }
  202. if (p_h <= (texture_h - y) && p_w <= texture_w) {
  203. List<Shelf>::Element *E = shelves.push_back(Shelf(0, y, texture_w, p_h));
  204. return E->get().alloc_shelf(p_id, p_w, p_h);
  205. }
  206. return FontTexturePosition(-1, 0, 0);
  207. }
  208. ShelfPackTexture() {}
  209. ShelfPackTexture(int32_t p_w, int32_t p_h) :
  210. texture_w(p_w), texture_h(p_h) {}
  211. };
  212. struct FontGlyph {
  213. bool found = false;
  214. int texture_idx = -1;
  215. Rect2 rect;
  216. Rect2 uv_rect;
  217. Vector2 advance;
  218. };
  219. struct FontForSizeAdvanced {
  220. double ascent = 0.0;
  221. double descent = 0.0;
  222. double underline_position = 0.0;
  223. double underline_thickness = 0.0;
  224. double scale = 1.0;
  225. double oversampling = 1.0;
  226. Vector2i size;
  227. Vector<ShelfPackTexture> textures;
  228. HashMap<int32_t, FontGlyph> glyph_map;
  229. HashMap<Vector2i, Vector2> kerning_map;
  230. hb_font_t *hb_handle = nullptr;
  231. #ifdef MODULE_FREETYPE_ENABLED
  232. FT_Face face = nullptr;
  233. FT_StreamRec stream;
  234. #endif
  235. ~FontForSizeAdvanced() {
  236. if (hb_handle != nullptr) {
  237. hb_font_destroy(hb_handle);
  238. }
  239. #ifdef MODULE_FREETYPE_ENABLED
  240. if (face != nullptr) {
  241. FT_Done_Face(face);
  242. }
  243. #endif
  244. }
  245. };
  246. struct FontAdvanced {
  247. Mutex mutex;
  248. TextServer::FontAntialiasing antialiasing = TextServer::FONT_ANTIALIASING_GRAY;
  249. bool mipmaps = false;
  250. bool msdf = false;
  251. int msdf_range = 14;
  252. int msdf_source_size = 48;
  253. int fixed_size = 0;
  254. bool allow_system_fallback = true;
  255. bool force_autohinter = false;
  256. TextServer::Hinting hinting = TextServer::HINTING_LIGHT;
  257. TextServer::SubpixelPositioning subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_AUTO;
  258. Dictionary variation_coordinates;
  259. double oversampling = 0.0;
  260. double embolden = 0.0;
  261. Transform2D transform;
  262. BitField<TextServer::FontStyle> style_flags = 0;
  263. String font_name;
  264. String style_name;
  265. int weight = 400;
  266. int stretch = 100;
  267. HashMap<Vector2i, FontForSizeAdvanced *, VariantHasher, VariantComparator> cache;
  268. bool face_init = false;
  269. HashSet<uint32_t> supported_scripts;
  270. Dictionary supported_features;
  271. Dictionary supported_varaitions;
  272. Dictionary feature_overrides;
  273. // Language/script support override.
  274. HashMap<String, bool> language_support_overrides;
  275. HashMap<String, bool> script_support_overrides;
  276. PackedByteArray data;
  277. const uint8_t *data_ptr;
  278. size_t data_size;
  279. int face_index = 0;
  280. ~FontAdvanced() {
  281. for (const KeyValue<Vector2i, FontForSizeAdvanced *> &E : cache) {
  282. memdelete(E.value);
  283. }
  284. cache.clear();
  285. }
  286. };
  287. _FORCE_INLINE_ FontTexturePosition find_texture_pos_for_glyph(FontForSizeAdvanced *p_data, int p_color_size, Image::Format p_image_format, int p_width, int p_height, bool p_msdf) const;
  288. #ifdef MODULE_MSDFGEN_ENABLED
  289. _FORCE_INLINE_ FontGlyph rasterize_msdf(FontAdvanced *p_font_data, FontForSizeAdvanced *p_data, int p_pixel_range, int p_rect_margin, FT_Outline *outline, const Vector2 &advance) const;
  290. #endif
  291. #ifdef MODULE_FREETYPE_ENABLED
  292. _FORCE_INLINE_ FontGlyph rasterize_bitmap(FontForSizeAdvanced *p_data, int p_rect_margin, FT_Bitmap bitmap, int yofs, int xofs, const Vector2 &advance, bool p_bgra) const;
  293. #endif
  294. _FORCE_INLINE_ bool _ensure_glyph(FontAdvanced *p_font_data, const Vector2i &p_size, int32_t p_glyph) const;
  295. _FORCE_INLINE_ bool _ensure_cache_for_size(FontAdvanced *p_font_data, const Vector2i &p_size) const;
  296. _FORCE_INLINE_ void _font_clear_cache(FontAdvanced *p_font_data);
  297. static void _generateMTSDF_threaded(void *p_td, uint32_t p_y);
  298. _FORCE_INLINE_ Vector2i _get_size(const FontAdvanced *p_font_data, int p_size) const {
  299. if (p_font_data->msdf) {
  300. return Vector2i(p_font_data->msdf_source_size, 0);
  301. } else if (p_font_data->fixed_size > 0) {
  302. return Vector2i(p_font_data->fixed_size, 0);
  303. } else {
  304. return Vector2i(p_size, 0);
  305. }
  306. }
  307. _FORCE_INLINE_ Vector2i _get_size_outline(const FontAdvanced *p_font_data, const Vector2i &p_size) const {
  308. if (p_font_data->msdf) {
  309. return Vector2i(p_font_data->msdf_source_size, 0);
  310. } else if (p_font_data->fixed_size > 0) {
  311. return Vector2i(p_font_data->fixed_size, MIN(p_size.y, 1));
  312. } else {
  313. return p_size;
  314. }
  315. }
  316. _FORCE_INLINE_ double _get_extra_advance(RID p_font_rid, int p_font_size) const;
  317. _FORCE_INLINE_ Variant::Type _get_tag_type(int64_t p_tag) const;
  318. _FORCE_INLINE_ bool _get_tag_hidden(int64_t p_tag) const;
  319. _FORCE_INLINE_ int _font_get_weight_by_name(const String &p_sty_name) const {
  320. String sty_name = p_sty_name.replace(" ", "").replace("-", "");
  321. if (sty_name.find("thin") >= 0 || sty_name.find("hairline") >= 0) {
  322. return 100;
  323. } else if (sty_name.find("extralight") >= 0 || sty_name.find("ultralight") >= 0) {
  324. return 200;
  325. } else if (sty_name.find("light") >= 0) {
  326. return 300;
  327. } else if (sty_name.find("semilight") >= 0) {
  328. return 350;
  329. } else if (sty_name.find("regular") >= 0) {
  330. return 400;
  331. } else if (sty_name.find("medium") >= 0) {
  332. return 500;
  333. } else if (sty_name.find("semibold") >= 0 || sty_name.find("demibold") >= 0) {
  334. return 600;
  335. } else if (sty_name.find("bold") >= 0) {
  336. return 700;
  337. } else if (sty_name.find("extrabold") >= 0 || sty_name.find("ultrabold") >= 0) {
  338. return 800;
  339. } else if (sty_name.find("black") >= 0 || sty_name.find("heavy") >= 0) {
  340. return 900;
  341. } else if (sty_name.find("extrablack") >= 0 || sty_name.find("ultrablack") >= 0) {
  342. return 950;
  343. }
  344. return 400;
  345. }
  346. _FORCE_INLINE_ int _font_get_stretch_by_name(const String &p_sty_name) const {
  347. String sty_name = p_sty_name.replace(" ", "").replace("-", "");
  348. if (sty_name.find("ultracondensed") >= 0) {
  349. return 50;
  350. } else if (sty_name.find("extracondensed") >= 0) {
  351. return 63;
  352. } else if (sty_name.find("condensed") >= 0) {
  353. return 75;
  354. } else if (sty_name.find("semicondensed") >= 0) {
  355. return 87;
  356. } else if (sty_name.find("semiexpanded") >= 0) {
  357. return 113;
  358. } else if (sty_name.find("expanded") >= 0) {
  359. return 125;
  360. } else if (sty_name.find("extraexpanded") >= 0) {
  361. return 150;
  362. } else if (sty_name.find("ultraexpanded") >= 0) {
  363. return 200;
  364. }
  365. return 100;
  366. }
  367. _FORCE_INLINE_ bool _is_ital_style(const String &p_sty_name) const {
  368. return (p_sty_name.find("italic") >= 0) || (p_sty_name.find("oblique") >= 0);
  369. }
  370. // Shaped text cache data.
  371. struct TrimData {
  372. int trim_pos = -1;
  373. int ellipsis_pos = -1;
  374. Vector<Glyph> ellipsis_glyph_buf;
  375. };
  376. struct ShapedTextDataAdvanced {
  377. Mutex mutex;
  378. /* Source data */
  379. RID parent; // Substring parent ShapedTextData.
  380. int start = 0; // Substring start offset in the parent string.
  381. int end = 0; // Substring end offset in the parent string.
  382. String text;
  383. String custom_punct;
  384. TextServer::Direction direction = DIRECTION_LTR; // Desired text direction.
  385. TextServer::Orientation orientation = ORIENTATION_HORIZONTAL;
  386. struct Span {
  387. int start = -1;
  388. int end = -1;
  389. Array fonts;
  390. int font_size = 0;
  391. Variant embedded_key;
  392. String language;
  393. Dictionary features;
  394. Variant meta;
  395. };
  396. Vector<Span> spans;
  397. struct EmbeddedObject {
  398. int pos = 0;
  399. InlineAlignment inline_align = INLINE_ALIGNMENT_CENTER;
  400. Rect2 rect;
  401. double baseline = 0;
  402. };
  403. HashMap<Variant, EmbeddedObject, VariantHasher, VariantComparator> objects;
  404. /* Shaped data */
  405. TextServer::Direction para_direction = DIRECTION_LTR; // Detected text direction.
  406. bool valid = false; // String is shaped.
  407. bool line_breaks_valid = false; // Line and word break flags are populated (and virtual zero width spaces inserted).
  408. bool justification_ops_valid = false; // Virtual elongation glyphs are added to the string.
  409. bool sort_valid = false;
  410. bool text_trimmed = false;
  411. bool preserve_invalid = true; // Draw hex code box instead of missing characters.
  412. bool preserve_control = false; // Draw control characters.
  413. double ascent = 0.0; // Ascent for horizontal layout, 1/2 of width for vertical.
  414. double descent = 0.0; // Descent for horizontal layout, 1/2 of width for vertical.
  415. double width = 0.0; // Width for horizontal layout, height for vertical.
  416. double width_trimmed = 0.0;
  417. int extra_spacing[4] = { 0, 0, 0, 0 };
  418. double upos = 0.0;
  419. double uthk = 0.0;
  420. TrimData overrun_trim_data;
  421. bool fit_width_minimum_reached = false;
  422. Vector<Glyph> glyphs;
  423. Vector<Glyph> glyphs_logical;
  424. /* Intermediate data */
  425. Char16String utf16;
  426. Vector<UBiDi *> bidi_iter;
  427. Vector<Vector3i> bidi_override;
  428. ScriptIterator *script_iter = nullptr;
  429. hb_buffer_t *hb_buffer = nullptr;
  430. HashMap<int, bool> jstops;
  431. HashMap<int, bool> breaks;
  432. int break_inserts = 0;
  433. bool break_ops_valid = false;
  434. bool js_ops_valid = false;
  435. ~ShapedTextDataAdvanced() {
  436. for (int i = 0; i < bidi_iter.size(); i++) {
  437. ubidi_close(bidi_iter[i]);
  438. }
  439. if (script_iter) {
  440. memdelete(script_iter);
  441. }
  442. if (hb_buffer) {
  443. hb_buffer_destroy(hb_buffer);
  444. }
  445. }
  446. };
  447. // Common data.
  448. double oversampling = 1.0;
  449. mutable RID_PtrOwner<FontAdvanced> font_owner;
  450. mutable RID_PtrOwner<ShapedTextDataAdvanced> shaped_owner;
  451. struct SystemFontKey {
  452. String font_name;
  453. TextServer::FontAntialiasing antialiasing = TextServer::FONT_ANTIALIASING_GRAY;
  454. bool italic = false;
  455. bool mipmaps = false;
  456. bool msdf = false;
  457. bool force_autohinter = false;
  458. int weight = 400;
  459. int stretch = 100;
  460. int msdf_range = 14;
  461. int msdf_source_size = 48;
  462. int fixed_size = 0;
  463. TextServer::Hinting hinting = TextServer::HINTING_LIGHT;
  464. TextServer::SubpixelPositioning subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_AUTO;
  465. Dictionary variation_coordinates;
  466. double oversampling = 0.0;
  467. double embolden = 0.0;
  468. Transform2D transform;
  469. bool operator==(const SystemFontKey &p_b) const {
  470. 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);
  471. }
  472. SystemFontKey(const String &p_font_name, bool p_italic, int p_weight, int p_stretch, RID p_font, const TextServerAdvanced *p_fb) {
  473. font_name = p_font_name;
  474. italic = p_italic;
  475. weight = p_weight;
  476. stretch = p_stretch;
  477. antialiasing = p_fb->_font_get_antialiasing(p_font);
  478. mipmaps = p_fb->_font_get_generate_mipmaps(p_font);
  479. msdf = p_fb->_font_is_multichannel_signed_distance_field(p_font);
  480. msdf_range = p_fb->_font_get_msdf_pixel_range(p_font);
  481. msdf_source_size = p_fb->_font_get_msdf_size(p_font);
  482. fixed_size = p_fb->_font_get_fixed_size(p_font);
  483. force_autohinter = p_fb->_font_is_force_autohinter(p_font);
  484. hinting = p_fb->_font_get_hinting(p_font);
  485. subpixel_positioning = p_fb->_font_get_subpixel_positioning(p_font);
  486. variation_coordinates = p_fb->_font_get_variation_coordinates(p_font);
  487. oversampling = p_fb->_font_get_oversampling(p_font);
  488. embolden = p_fb->_font_get_embolden(p_font);
  489. transform = p_fb->_font_get_transform(p_font);
  490. }
  491. };
  492. struct SystemFontCacheRec {
  493. RID rid;
  494. int index = 0;
  495. };
  496. struct SystemFontCache {
  497. Vector<SystemFontCacheRec> var;
  498. int max_var = 0;
  499. };
  500. struct SystemFontKeyHasher {
  501. _FORCE_INLINE_ static uint32_t hash(const SystemFontKey &p_a) {
  502. uint32_t hash = p_a.font_name.hash();
  503. hash = hash_murmur3_one_32(p_a.variation_coordinates.hash(), hash);
  504. hash = hash_murmur3_one_32(p_a.weight, hash);
  505. hash = hash_murmur3_one_32(p_a.stretch, hash);
  506. hash = hash_murmur3_one_32(p_a.msdf_range, hash);
  507. hash = hash_murmur3_one_32(p_a.msdf_source_size, hash);
  508. hash = hash_murmur3_one_32(p_a.fixed_size, hash);
  509. hash = hash_murmur3_one_double(p_a.oversampling, hash);
  510. hash = hash_murmur3_one_double(p_a.embolden, hash);
  511. hash = hash_murmur3_one_real(p_a.transform[0].x, hash);
  512. hash = hash_murmur3_one_real(p_a.transform[0].y, hash);
  513. hash = hash_murmur3_one_real(p_a.transform[1].x, hash);
  514. hash = hash_murmur3_one_real(p_a.transform[1].y, hash);
  515. 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));
  516. }
  517. };
  518. mutable HashMap<SystemFontKey, SystemFontCache, SystemFontKeyHasher> system_fonts;
  519. mutable HashMap<String, PackedByteArray> system_font_data;
  520. void _realign(ShapedTextDataAdvanced *p_sd) const;
  521. int64_t _convert_pos(const String &p_utf32, const Char16String &p_utf16, int64_t p_pos) const;
  522. int64_t _convert_pos(const ShapedTextDataAdvanced *p_sd, int64_t p_pos) const;
  523. int64_t _convert_pos_inv(const ShapedTextDataAdvanced *p_sd, int64_t p_pos) const;
  524. bool _shape_substr(ShapedTextDataAdvanced *p_new_sd, const ShapedTextDataAdvanced *p_sd, int64_t p_start, int64_t p_length) const;
  525. void _shape_run(ShapedTextDataAdvanced *p_sd, int64_t p_start, int64_t p_end, hb_script_t p_script, hb_direction_t p_direction, TypedArray<RID> p_fonts, int64_t p_span, int64_t p_fb_index, int64_t p_prev_start, int64_t p_prev_end);
  526. Glyph _shape_single_glyph(ShapedTextDataAdvanced *p_sd, char32_t p_char, hb_script_t p_script, hb_direction_t p_direction, const RID &p_font, int64_t p_font_size);
  527. _FORCE_INLINE_ void _add_featuers(const Dictionary &p_source, Vector<hb_feature_t> &r_ftrs);
  528. Mutex ft_mutex;
  529. // HarfBuzz bitmap font interface.
  530. static hb_font_funcs_t *funcs;
  531. struct bmp_font_t {
  532. TextServerAdvanced::FontForSizeAdvanced *face = nullptr;
  533. bool unref = false; /* Whether to destroy bm_face when done. */
  534. };
  535. static bmp_font_t *_bmp_font_create(TextServerAdvanced::FontForSizeAdvanced *p_face, bool p_unref);
  536. static void _bmp_font_destroy(void *p_data);
  537. static hb_bool_t _bmp_get_nominal_glyph(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_unicode, hb_codepoint_t *r_glyph, void *p_user_data);
  538. static hb_position_t _bmp_get_glyph_h_advance(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_glyph, void *p_user_data);
  539. static hb_position_t _bmp_get_glyph_v_advance(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_glyph, void *p_user_data);
  540. static hb_position_t _bmp_get_glyph_h_kerning(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_left_glyph, hb_codepoint_t p_right_glyph, void *p_user_data);
  541. static hb_bool_t _bmp_get_glyph_v_origin(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_glyph, hb_position_t *r_x, hb_position_t *r_y, void *p_user_data);
  542. static hb_bool_t _bmp_get_glyph_extents(hb_font_t *p_font, void *p_font_data, hb_codepoint_t p_glyph, hb_glyph_extents_t *r_extents, void *p_user_data);
  543. static hb_bool_t _bmp_get_font_h_extents(hb_font_t *p_font, void *p_font_data, hb_font_extents_t *r_metrics, void *p_user_data);
  544. static void _bmp_create_font_funcs();
  545. static void _bmp_free_font_funcs();
  546. static void _bmp_font_set_funcs(hb_font_t *p_font, TextServerAdvanced::FontForSizeAdvanced *p_face, bool p_unref);
  547. static hb_font_t *_bmp_font_create(TextServerAdvanced::FontForSizeAdvanced *p_face, hb_destroy_func_t p_destroy);
  548. hb_font_t *_font_get_hb_handle(const RID &p_font, int64_t p_font_size) const;
  549. struct GlyphCompare { // For line breaking reordering.
  550. _FORCE_INLINE_ bool operator()(const Glyph &l, const Glyph &r) const {
  551. if (l.start == r.start) {
  552. if (l.count == r.count) {
  553. if ((l.flags & TextServer::GRAPHEME_IS_VIRTUAL) == TextServer::GRAPHEME_IS_VIRTUAL) {
  554. return false;
  555. } else {
  556. return true;
  557. }
  558. }
  559. return l.count > r.count; // Sort first glyph with count & flags, order of the rest are irrelevant.
  560. } else {
  561. return l.start < r.start;
  562. }
  563. }
  564. };
  565. protected:
  566. static void _bind_methods(){};
  567. void full_copy(ShapedTextDataAdvanced *p_shaped);
  568. void invalidate(ShapedTextDataAdvanced *p_shaped, bool p_text = false);
  569. public:
  570. MODBIND1RC(bool, has_feature, Feature);
  571. MODBIND0RC(String, get_name);
  572. MODBIND0RC(int64_t, get_features);
  573. MODBIND1(free_rid, const RID &);
  574. MODBIND1R(bool, has, const RID &);
  575. MODBIND1R(bool, load_support_data, const String &);
  576. MODBIND0RC(String, get_support_data_filename);
  577. MODBIND0RC(String, get_support_data_info);
  578. MODBIND1RC(bool, save_support_data, const String &);
  579. MODBIND1RC(bool, is_locale_right_to_left, const String &);
  580. MODBIND1RC(int64_t, name_to_tag, const String &);
  581. MODBIND1RC(String, tag_to_name, int64_t);
  582. /* Font interface */
  583. MODBIND0R(RID, create_font);
  584. MODBIND2(font_set_data, const RID &, const PackedByteArray &);
  585. MODBIND3(font_set_data_ptr, const RID &, const uint8_t *, int64_t);
  586. MODBIND2(font_set_face_index, const RID &, int64_t);
  587. MODBIND1RC(int64_t, font_get_face_index, const RID &);
  588. MODBIND1RC(int64_t, font_get_face_count, const RID &);
  589. MODBIND2(font_set_style, const RID &, BitField<FontStyle>);
  590. MODBIND1RC(BitField<FontStyle>, font_get_style, const RID &);
  591. MODBIND2(font_set_style_name, const RID &, const String &);
  592. MODBIND1RC(String, font_get_style_name, const RID &);
  593. MODBIND2(font_set_weight, const RID &, int64_t);
  594. MODBIND1RC(int64_t, font_get_weight, const RID &);
  595. MODBIND2(font_set_stretch, const RID &, int64_t);
  596. MODBIND1RC(int64_t, font_get_stretch, const RID &);
  597. MODBIND2(font_set_name, const RID &, const String &);
  598. MODBIND1RC(String, font_get_name, const RID &);
  599. MODBIND2(font_set_antialiasing, const RID &, TextServer::FontAntialiasing);
  600. MODBIND1RC(TextServer::FontAntialiasing, font_get_antialiasing, const RID &);
  601. MODBIND2(font_set_generate_mipmaps, const RID &, bool);
  602. MODBIND1RC(bool, font_get_generate_mipmaps, const RID &);
  603. MODBIND2(font_set_multichannel_signed_distance_field, const RID &, bool);
  604. MODBIND1RC(bool, font_is_multichannel_signed_distance_field, const RID &);
  605. MODBIND2(font_set_msdf_pixel_range, const RID &, int64_t);
  606. MODBIND1RC(int64_t, font_get_msdf_pixel_range, const RID &);
  607. MODBIND2(font_set_msdf_size, const RID &, int64_t);
  608. MODBIND1RC(int64_t, font_get_msdf_size, const RID &);
  609. MODBIND2(font_set_fixed_size, const RID &, int64_t);
  610. MODBIND1RC(int64_t, font_get_fixed_size, const RID &);
  611. MODBIND2(font_set_allow_system_fallback, const RID &, bool);
  612. MODBIND1RC(bool, font_is_allow_system_fallback, const RID &);
  613. MODBIND2(font_set_force_autohinter, const RID &, bool);
  614. MODBIND1RC(bool, font_is_force_autohinter, const RID &);
  615. MODBIND2(font_set_subpixel_positioning, const RID &, SubpixelPositioning);
  616. MODBIND1RC(SubpixelPositioning, font_get_subpixel_positioning, const RID &);
  617. MODBIND2(font_set_embolden, const RID &, double);
  618. MODBIND1RC(double, font_get_embolden, const RID &);
  619. MODBIND2(font_set_transform, const RID &, const Transform2D &);
  620. MODBIND1RC(Transform2D, font_get_transform, const RID &);
  621. MODBIND2(font_set_variation_coordinates, const RID &, const Dictionary &);
  622. MODBIND1RC(Dictionary, font_get_variation_coordinates, const RID &);
  623. MODBIND2(font_set_hinting, const RID &, TextServer::Hinting);
  624. MODBIND1RC(TextServer::Hinting, font_get_hinting, const RID &);
  625. MODBIND2(font_set_oversampling, const RID &, double);
  626. MODBIND1RC(double, font_get_oversampling, const RID &);
  627. MODBIND1RC(TypedArray<Vector2i>, font_get_size_cache_list, const RID &);
  628. MODBIND1(font_clear_size_cache, const RID &);
  629. MODBIND2(font_remove_size_cache, const RID &, const Vector2i &);
  630. MODBIND3(font_set_ascent, const RID &, int64_t, double);
  631. MODBIND2RC(double, font_get_ascent, const RID &, int64_t);
  632. MODBIND3(font_set_descent, const RID &, int64_t, double);
  633. MODBIND2RC(double, font_get_descent, const RID &, int64_t);
  634. MODBIND3(font_set_underline_position, const RID &, int64_t, double);
  635. MODBIND2RC(double, font_get_underline_position, const RID &, int64_t);
  636. MODBIND3(font_set_underline_thickness, const RID &, int64_t, double);
  637. MODBIND2RC(double, font_get_underline_thickness, const RID &, int64_t);
  638. MODBIND3(font_set_scale, const RID &, int64_t, double);
  639. MODBIND2RC(double, font_get_scale, const RID &, int64_t);
  640. MODBIND2RC(int64_t, font_get_texture_count, const RID &, const Vector2i &);
  641. MODBIND2(font_clear_textures, const RID &, const Vector2i &);
  642. MODBIND3(font_remove_texture, const RID &, const Vector2i &, int64_t);
  643. MODBIND4(font_set_texture_image, const RID &, const Vector2i &, int64_t, const Ref<Image> &);
  644. MODBIND3RC(Ref<Image>, font_get_texture_image, const RID &, const Vector2i &, int64_t);
  645. MODBIND4(font_set_texture_offsets, const RID &, const Vector2i &, int64_t, const PackedInt32Array &);
  646. MODBIND3RC(PackedInt32Array, font_get_texture_offsets, const RID &, const Vector2i &, int64_t);
  647. MODBIND2RC(PackedInt32Array, font_get_glyph_list, const RID &, const Vector2i &);
  648. MODBIND2(font_clear_glyphs, const RID &, const Vector2i &);
  649. MODBIND3(font_remove_glyph, const RID &, const Vector2i &, int64_t);
  650. MODBIND3RC(Vector2, font_get_glyph_advance, const RID &, int64_t, int64_t);
  651. MODBIND4(font_set_glyph_advance, const RID &, int64_t, int64_t, const Vector2 &);
  652. MODBIND3RC(Vector2, font_get_glyph_offset, const RID &, const Vector2i &, int64_t);
  653. MODBIND4(font_set_glyph_offset, const RID &, const Vector2i &, int64_t, const Vector2 &);
  654. MODBIND3RC(Vector2, font_get_glyph_size, const RID &, const Vector2i &, int64_t);
  655. MODBIND4(font_set_glyph_size, const RID &, const Vector2i &, int64_t, const Vector2 &);
  656. MODBIND3RC(Rect2, font_get_glyph_uv_rect, const RID &, const Vector2i &, int64_t);
  657. MODBIND4(font_set_glyph_uv_rect, const RID &, const Vector2i &, int64_t, const Rect2 &);
  658. MODBIND3RC(int64_t, font_get_glyph_texture_idx, const RID &, const Vector2i &, int64_t);
  659. MODBIND4(font_set_glyph_texture_idx, const RID &, const Vector2i &, int64_t, int64_t);
  660. MODBIND3RC(RID, font_get_glyph_texture_rid, const RID &, const Vector2i &, int64_t);
  661. MODBIND3RC(Size2, font_get_glyph_texture_size, const RID &, const Vector2i &, int64_t);
  662. MODBIND3RC(Dictionary, font_get_glyph_contours, const RID &, int64_t, int64_t);
  663. MODBIND2RC(TypedArray<Vector2i>, font_get_kerning_list, const RID &, int64_t);
  664. MODBIND2(font_clear_kerning_map, const RID &, int64_t);
  665. MODBIND3(font_remove_kerning, const RID &, int64_t, const Vector2i &);
  666. MODBIND4(font_set_kerning, const RID &, int64_t, const Vector2i &, const Vector2 &);
  667. MODBIND3RC(Vector2, font_get_kerning, const RID &, int64_t, const Vector2i &);
  668. MODBIND4RC(int64_t, font_get_glyph_index, const RID &, int64_t, int64_t, int64_t);
  669. MODBIND2RC(bool, font_has_char, const RID &, int64_t);
  670. MODBIND1RC(String, font_get_supported_chars, const RID &);
  671. MODBIND4(font_render_range, const RID &, const Vector2i &, int64_t, int64_t);
  672. MODBIND3(font_render_glyph, const RID &, const Vector2i &, int64_t);
  673. MODBIND6C(font_draw_glyph, const RID &, const RID &, int64_t, const Vector2 &, int64_t, const Color &);
  674. MODBIND7C(font_draw_glyph_outline, const RID &, const RID &, int64_t, int64_t, const Vector2 &, int64_t, const Color &);
  675. MODBIND2RC(bool, font_is_language_supported, const RID &, const String &);
  676. MODBIND3(font_set_language_support_override, const RID &, const String &, bool);
  677. MODBIND2R(bool, font_get_language_support_override, const RID &, const String &);
  678. MODBIND2(font_remove_language_support_override, const RID &, const String &);
  679. MODBIND1R(PackedStringArray, font_get_language_support_overrides, const RID &);
  680. MODBIND2RC(bool, font_is_script_supported, const RID &, const String &);
  681. MODBIND3(font_set_script_support_override, const RID &, const String &, bool);
  682. MODBIND2R(bool, font_get_script_support_override, const RID &, const String &);
  683. MODBIND2(font_remove_script_support_override, const RID &, const String &);
  684. MODBIND1R(PackedStringArray, font_get_script_support_overrides, const RID &);
  685. MODBIND2(font_set_opentype_feature_overrides, const RID &, const Dictionary &);
  686. MODBIND1RC(Dictionary, font_get_opentype_feature_overrides, const RID &);
  687. MODBIND1RC(Dictionary, font_supported_feature_list, const RID &);
  688. MODBIND1RC(Dictionary, font_supported_variation_list, const RID &);
  689. MODBIND0RC(double, font_get_global_oversampling);
  690. MODBIND1(font_set_global_oversampling, double);
  691. /* Shaped text buffer interface */
  692. MODBIND2R(RID, create_shaped_text, Direction, Orientation);
  693. MODBIND1(shaped_text_clear, const RID &);
  694. MODBIND2(shaped_text_set_direction, const RID &, Direction);
  695. MODBIND1RC(Direction, shaped_text_get_direction, const RID &);
  696. MODBIND1RC(Direction, shaped_text_get_inferred_direction, const RID &);
  697. MODBIND2(shaped_text_set_bidi_override, const RID &, const Array &);
  698. MODBIND2(shaped_text_set_custom_punctuation, const RID &, const String &);
  699. MODBIND1RC(String, shaped_text_get_custom_punctuation, const RID &);
  700. MODBIND2(shaped_text_set_orientation, const RID &, Orientation);
  701. MODBIND1RC(Orientation, shaped_text_get_orientation, const RID &);
  702. MODBIND2(shaped_text_set_preserve_invalid, const RID &, bool);
  703. MODBIND1RC(bool, shaped_text_get_preserve_invalid, const RID &);
  704. MODBIND2(shaped_text_set_preserve_control, const RID &, bool);
  705. MODBIND1RC(bool, shaped_text_get_preserve_control, const RID &);
  706. MODBIND3(shaped_text_set_spacing, const RID &, SpacingType, int64_t);
  707. MODBIND2RC(int64_t, shaped_text_get_spacing, const RID &, SpacingType);
  708. MODBIND7R(bool, shaped_text_add_string, const RID &, const String &, const TypedArray<RID> &, int64_t, const Dictionary &, const String &, const Variant &);
  709. MODBIND6R(bool, shaped_text_add_object, const RID &, const Variant &, const Size2 &, InlineAlignment, int64_t, double);
  710. MODBIND5R(bool, shaped_text_resize_object, const RID &, const Variant &, const Size2 &, InlineAlignment, double);
  711. MODBIND1RC(int64_t, shaped_get_span_count, const RID &);
  712. MODBIND2RC(Variant, shaped_get_span_meta, const RID &, int64_t);
  713. MODBIND5(shaped_set_span_update_font, const RID &, int64_t, const TypedArray<RID> &, int64_t, const Dictionary &);
  714. MODBIND3RC(RID, shaped_text_substr, const RID &, int64_t, int64_t);
  715. MODBIND1RC(RID, shaped_text_get_parent, const RID &);
  716. MODBIND3R(double, shaped_text_fit_to_width, const RID &, double, BitField<TextServer::JustificationFlag>);
  717. MODBIND2R(double, shaped_text_tab_align, const RID &, const PackedFloat32Array &);
  718. MODBIND1R(bool, shaped_text_shape, const RID &);
  719. MODBIND1R(bool, shaped_text_update_breaks, const RID &);
  720. MODBIND1R(bool, shaped_text_update_justification_ops, const RID &);
  721. MODBIND1RC(int64_t, shaped_text_get_trim_pos, const RID &);
  722. MODBIND1RC(int64_t, shaped_text_get_ellipsis_pos, const RID &);
  723. MODBIND1RC(const Glyph *, shaped_text_get_ellipsis_glyphs, const RID &);
  724. MODBIND1RC(int64_t, shaped_text_get_ellipsis_glyph_count, const RID &);
  725. MODBIND3(shaped_text_overrun_trim_to_width, const RID &, double, BitField<TextServer::TextOverrunFlag>);
  726. MODBIND1RC(bool, shaped_text_is_ready, const RID &);
  727. MODBIND1RC(const Glyph *, shaped_text_get_glyphs, const RID &);
  728. MODBIND1R(const Glyph *, shaped_text_sort_logical, const RID &);
  729. MODBIND1RC(int64_t, shaped_text_get_glyph_count, const RID &);
  730. MODBIND1RC(Vector2i, shaped_text_get_range, const RID &);
  731. MODBIND1RC(Array, shaped_text_get_objects, const RID &);
  732. MODBIND2RC(Rect2, shaped_text_get_object_rect, const RID &, const Variant &);
  733. MODBIND1RC(Size2, shaped_text_get_size, const RID &);
  734. MODBIND1RC(double, shaped_text_get_ascent, const RID &);
  735. MODBIND1RC(double, shaped_text_get_descent, const RID &);
  736. MODBIND1RC(double, shaped_text_get_width, const RID &);
  737. MODBIND1RC(double, shaped_text_get_underline_position, const RID &);
  738. MODBIND1RC(double, shaped_text_get_underline_thickness, const RID &);
  739. MODBIND2RC(String, format_number, const String &, const String &);
  740. MODBIND2RC(String, parse_number, const String &, const String &);
  741. MODBIND1RC(String, percent_sign, const String &);
  742. MODBIND3RC(PackedInt32Array, string_get_word_breaks, const String &, const String &, int64_t);
  743. MODBIND2RC(int64_t, is_confusable, const String &, const PackedStringArray &);
  744. MODBIND1RC(bool, spoof_check, const String &);
  745. MODBIND1RC(String, strip_diacritics, const String &);
  746. MODBIND1RC(bool, is_valid_identifier, const String &);
  747. MODBIND2RC(String, string_to_upper, const String &, const String &);
  748. MODBIND2RC(String, string_to_lower, const String &, const String &);
  749. MODBIND0(cleanup);
  750. TextServerAdvanced();
  751. ~TextServerAdvanced();
  752. };
  753. #endif // TEXT_SERVER_ADV_H