text_server_adv.h 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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. #pragma once
  31. /*************************************************************************/
  32. /* ICU/HarfBuzz/Graphite backed Text Server implementation with BiDi, */
  33. /* shaping and advanced font features support. */
  34. /*************************************************************************/
  35. #include "script_iterator.h"
  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/safe_refcount.hpp>
  67. #include <godot_cpp/templates/vector.hpp>
  68. using namespace godot;
  69. #elif defined(GODOT_MODULE)
  70. // Headers for building as built-in module.
  71. #include "core/extension/ext_wrappers.gen.inc"
  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. GODOT_GCC_WARNING_PUSH_AND_IGNORE("-Wshadow")
  81. #if defined(__EMSCRIPTEN__) || (defined(__MINGW32__) && __clang_major__ >= 21)
  82. GODOT_CLANG_WARNING_PUSH_AND_IGNORE("-Wunnecessary-virtual-specifier")
  83. #endif
  84. #include <unicode/ubidi.h>
  85. #include <unicode/ubrk.h>
  86. #include <unicode/uchar.h>
  87. #include <unicode/uclean.h>
  88. #include <unicode/udata.h>
  89. #include <unicode/uiter.h>
  90. #include <unicode/uloc.h>
  91. #include <unicode/unorm2.h>
  92. #include <unicode/uscript.h>
  93. #include <unicode/uspoof.h>
  94. #include <unicode/ustring.h>
  95. #include <unicode/utypes.h>
  96. GODOT_GCC_WARNING_POP
  97. #if defined(__EMSCRIPTEN__) || (defined(__MINGW32__) && __clang_major__ >= 21)
  98. GODOT_CLANG_WARNING_POP
  99. #endif
  100. #ifdef MODULE_FREETYPE_ENABLED
  101. #include <ft2build.h>
  102. #include FT_FREETYPE_H
  103. #include FT_TRUETYPE_TABLES_H
  104. #include FT_STROKER_H
  105. #include FT_ADVANCES_H
  106. #include FT_MULTIPLE_MASTERS_H
  107. #include FT_BBOX_H
  108. #include FT_MODULE_H
  109. #include FT_CONFIG_OPTIONS_H
  110. #if !defined(FT_CONFIG_OPTION_USE_BROTLI) && !defined(_MSC_VER)
  111. #warning FreeType is configured without Brotli support, built-in fonts will not be available.
  112. #endif
  113. #include <hb-ft.h>
  114. #include <hb-ot.h>
  115. #endif
  116. #include <hb-icu.h>
  117. #include <hb.h>
  118. /*************************************************************************/
  119. class TextServerAdvanced : public TextServerExtension {
  120. GDCLASS(TextServerAdvanced, TextServerExtension);
  121. _THREAD_SAFE_CLASS_
  122. struct FeatureInfo {
  123. StringName name;
  124. Variant::Type vtype = Variant::INT;
  125. bool hidden = false;
  126. };
  127. HashMap<StringName, int32_t> feature_sets;
  128. HashMap<int32_t, FeatureInfo> feature_sets_inv;
  129. enum LineBreakStrictness {
  130. LB_AUTO,
  131. LB_LOOSE,
  132. LB_NORMAL,
  133. LB_STRICT,
  134. };
  135. SafeNumeric<TextServer::FontLCDSubpixelLayout> lcd_subpixel_layout{ TextServer::FontLCDSubpixelLayout::FONT_LCD_SUBPIXEL_LAYOUT_NONE };
  136. LineBreakStrictness lb_strictness = LB_AUTO;
  137. void _update_settings();
  138. void _insert_feature_sets();
  139. _FORCE_INLINE_ void _insert_feature(const StringName &p_name, int32_t p_tag, Variant::Type p_vtype = Variant::INT, bool p_hidden = false);
  140. // ICU support data.
  141. static bool icu_data_loaded;
  142. static PackedByteArray icu_data;
  143. mutable USet *allowed = nullptr;
  144. mutable USpoofChecker *sc_spoof = nullptr;
  145. mutable USpoofChecker *sc_conf = nullptr;
  146. mutable HashMap<String, UBreakIterator *> line_break_iterators_per_language;
  147. UBreakIterator *_create_line_break_iterator_for_locale(const String &p_language, UErrorCode *r_err) const;
  148. // Font cache data.
  149. #ifdef MODULE_FREETYPE_ENABLED
  150. mutable FT_Library ft_library = nullptr;
  151. #endif
  152. const int rect_range = 1;
  153. struct FontTexturePosition {
  154. int32_t index = -1;
  155. int32_t x = 0;
  156. int32_t y = 0;
  157. FontTexturePosition() {}
  158. FontTexturePosition(int32_t p_id, int32_t p_x, int32_t p_y) :
  159. index(p_id), x(p_x), y(p_y) {}
  160. };
  161. struct Shelf {
  162. int32_t x = 0;
  163. int32_t y = 0;
  164. int32_t w = 0;
  165. int32_t h = 0;
  166. FontTexturePosition alloc_shelf(int32_t p_id, int32_t p_w, int32_t p_h) {
  167. if (p_w > w || p_h > h) {
  168. return FontTexturePosition(-1, 0, 0);
  169. }
  170. int32_t xx = x;
  171. x += p_w;
  172. w -= p_w;
  173. return FontTexturePosition(p_id, xx, y);
  174. }
  175. Shelf() {}
  176. Shelf(int32_t p_x, int32_t p_y, int32_t p_w, int32_t p_h) :
  177. x(p_x), y(p_y), w(p_w), h(p_h) {}
  178. };
  179. struct ShelfPackTexture {
  180. int32_t texture_w = 1024;
  181. int32_t texture_h = 1024;
  182. Ref<Image> image;
  183. Ref<ImageTexture> texture;
  184. bool dirty = true;
  185. List<Shelf> shelves;
  186. FontTexturePosition pack_rect(int32_t p_id, int32_t p_h, int32_t p_w) {
  187. int32_t y = 0;
  188. int32_t waste = 0;
  189. Shelf *best_shelf = nullptr;
  190. int32_t best_waste = std::numeric_limits<std::int32_t>::max();
  191. for (Shelf &E : shelves) {
  192. y += E.h;
  193. if (p_w > E.w) {
  194. continue;
  195. }
  196. if (p_h == E.h) {
  197. return E.alloc_shelf(p_id, p_w, p_h);
  198. }
  199. if (p_h > E.h) {
  200. continue;
  201. }
  202. if (p_h < E.h) {
  203. waste = (E.h - p_h) * p_w;
  204. if (waste < best_waste) {
  205. best_waste = waste;
  206. best_shelf = &E;
  207. }
  208. }
  209. }
  210. if (best_shelf) {
  211. return best_shelf->alloc_shelf(p_id, p_w, p_h);
  212. }
  213. if (p_h <= (texture_h - y) && p_w <= texture_w) {
  214. List<Shelf>::Element *E = shelves.push_back(Shelf(0, y, texture_w, p_h));
  215. return E->get().alloc_shelf(p_id, p_w, p_h);
  216. }
  217. return FontTexturePosition(-1, 0, 0);
  218. }
  219. ShelfPackTexture() {}
  220. ShelfPackTexture(int32_t p_w, int32_t p_h) :
  221. texture_w(p_w), texture_h(p_h) {}
  222. };
  223. struct FontGlyph {
  224. bool found = false;
  225. int texture_idx = -1;
  226. Rect2 rect;
  227. Rect2 uv_rect;
  228. Vector2 advance;
  229. bool from_svg = false;
  230. };
  231. struct FontAdvanced;
  232. struct FontForSizeAdvanced {
  233. double ascent = 0.0;
  234. double descent = 0.0;
  235. double underline_position = 0.0;
  236. double underline_thickness = 0.0;
  237. double scale = 1.0;
  238. FontAdvanced *owner = nullptr;
  239. uint32_t viewport_oversampling = 0;
  240. Vector2i size;
  241. Vector<ShelfPackTexture> textures;
  242. HashMap<int64_t, int64_t> inv_glyph_map;
  243. HashMap<int32_t, FontGlyph> glyph_map;
  244. HashMap<Vector2i, Vector2> kerning_map;
  245. hb_font_t *hb_handle = nullptr;
  246. #ifdef MODULE_FREETYPE_ENABLED
  247. FT_Face face = nullptr;
  248. FT_StreamRec stream;
  249. #endif
  250. ~FontForSizeAdvanced() {
  251. if (hb_handle != nullptr) {
  252. hb_font_destroy(hb_handle);
  253. }
  254. #ifdef MODULE_FREETYPE_ENABLED
  255. if (face != nullptr) {
  256. FT_Done_Face(face);
  257. }
  258. #endif
  259. }
  260. };
  261. struct OversamplingLevel {
  262. HashSet<FontForSizeAdvanced *> fonts;
  263. int32_t refcount = 1;
  264. };
  265. mutable HashMap<uint32_t, OversamplingLevel> oversampling_levels;
  266. struct FontAdvancedLinkedVariation {
  267. RID base_font;
  268. int extra_spacing[4] = { 0, 0, 0, 0 };
  269. double baseline_offset = 0.0;
  270. };
  271. struct FontAdvanced {
  272. Mutex mutex;
  273. TextServer::FontAntialiasing antialiasing = TextServer::FONT_ANTIALIASING_GRAY;
  274. bool disable_embedded_bitmaps = true;
  275. bool mipmaps = false;
  276. bool msdf = false;
  277. int msdf_range = 14;
  278. FixedSizeScaleMode fixed_size_scale_mode = FIXED_SIZE_SCALE_DISABLE;
  279. int msdf_source_size = 48;
  280. int fixed_size = 0;
  281. bool allow_system_fallback = true;
  282. bool force_autohinter = false;
  283. bool modulate_color_glyphs = false;
  284. TextServer::Hinting hinting = TextServer::HINTING_LIGHT;
  285. TextServer::SubpixelPositioning subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_AUTO;
  286. bool keep_rounding_remainders = true;
  287. Dictionary variation_coordinates;
  288. double oversampling_override = 0.0;
  289. double embolden = 0.0;
  290. Transform2D transform;
  291. BitField<TextServer::FontStyle> style_flags = 0;
  292. String font_name;
  293. String style_name;
  294. int weight = 400;
  295. int stretch = 100;
  296. int extra_spacing[4] = { 0, 0, 0, 0 };
  297. double baseline_offset = 0.0;
  298. HashMap<Vector2i, FontForSizeAdvanced *> cache;
  299. bool face_init = false;
  300. HashSet<uint32_t> supported_scripts;
  301. Dictionary supported_features;
  302. Dictionary supported_varaitions;
  303. Dictionary feature_overrides;
  304. // Language/script support override.
  305. HashMap<String, bool> language_support_overrides;
  306. HashMap<String, bool> script_support_overrides;
  307. PackedByteArray data;
  308. const uint8_t *data_ptr = nullptr;
  309. size_t data_size;
  310. int face_index = 0;
  311. ~FontAdvanced() {
  312. for (const KeyValue<Vector2i, FontForSizeAdvanced *> &E : cache) {
  313. memdelete(E.value);
  314. }
  315. cache.clear();
  316. }
  317. };
  318. _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;
  319. #ifdef MODULE_MSDFGEN_ENABLED
  320. _FORCE_INLINE_ FontGlyph rasterize_msdf(FontAdvanced *p_font_data, FontForSizeAdvanced *p_data, int p_pixel_range, int p_rect_margin, FT_Outline *p_outline, const Vector2 &p_advance) const;
  321. #endif
  322. #ifdef MODULE_FREETYPE_ENABLED
  323. _FORCE_INLINE_ FontGlyph rasterize_bitmap(FontForSizeAdvanced *p_data, int p_rect_margin, FT_Bitmap p_bitmap, int p_yofs, int p_xofs, const Vector2 &p_advance, bool p_bgra) const;
  324. #endif
  325. bool _ensure_glyph(FontAdvanced *p_font_data, const Vector2i &p_size, int32_t p_glyph, FontGlyph &r_glyph, uint32_t p_oversampling = 0) const;
  326. bool _ensure_cache_for_size(FontAdvanced *p_font_data, const Vector2i &p_size, FontForSizeAdvanced *&r_cache_for_size, bool p_silent = false, uint32_t p_oversampling = 0) const;
  327. _FORCE_INLINE_ bool _font_validate(const RID &p_font_rid) const;
  328. _FORCE_INLINE_ void _font_clear_cache(FontAdvanced *p_font_data);
  329. static void _generateMTSDF_threaded(void *p_td, uint32_t p_y);
  330. _FORCE_INLINE_ Vector2i _get_size(const FontAdvanced *p_font_data, int p_size) const {
  331. if (p_font_data->msdf) {
  332. return Vector2i(p_font_data->msdf_source_size * 64, 0);
  333. } else if (p_font_data->fixed_size > 0) {
  334. return Vector2i(p_font_data->fixed_size * 64, 0);
  335. } else {
  336. return Vector2i(p_size * 64, 0);
  337. }
  338. }
  339. _FORCE_INLINE_ Vector2i _get_size_outline(const FontAdvanced *p_font_data, const Vector2i &p_size) const {
  340. if (p_font_data->msdf) {
  341. return Vector2i(p_font_data->msdf_source_size * 64, 0);
  342. } else if (p_font_data->fixed_size > 0) {
  343. return Vector2i(p_font_data->fixed_size * 64, MIN(p_size.y, 1));
  344. } else {
  345. return Vector2i(p_size.x * 64, p_size.y);
  346. }
  347. }
  348. _FORCE_INLINE_ double _get_extra_advance(RID p_font_rid, int p_font_size) const;
  349. _FORCE_INLINE_ Variant::Type _get_tag_type(int64_t p_tag) const;
  350. _FORCE_INLINE_ bool _get_tag_hidden(int64_t p_tag) const;
  351. _FORCE_INLINE_ int _font_get_weight_by_name(const String &p_sty_name) const {
  352. String sty_name = p_sty_name.remove_chars(" -");
  353. if (sty_name.contains("thin") || sty_name.contains("hairline")) {
  354. return 100;
  355. } else if (sty_name.contains("extralight") || sty_name.contains("ultralight")) {
  356. return 200;
  357. } else if (sty_name.contains("light")) {
  358. return 300;
  359. } else if (sty_name.contains("semilight")) {
  360. return 350;
  361. } else if (sty_name.contains("regular")) {
  362. return 400;
  363. } else if (sty_name.contains("medium")) {
  364. return 500;
  365. } else if (sty_name.contains("semibold") || sty_name.contains("demibold")) {
  366. return 600;
  367. } else if (sty_name.contains("bold")) {
  368. return 700;
  369. } else if (sty_name.contains("extrabold") || sty_name.contains("ultrabold")) {
  370. return 800;
  371. } else if (sty_name.contains("black") || sty_name.contains("heavy")) {
  372. return 900;
  373. } else if (sty_name.contains("extrablack") || sty_name.contains("ultrablack")) {
  374. return 950;
  375. }
  376. return 400;
  377. }
  378. _FORCE_INLINE_ int _font_get_stretch_by_name(const String &p_sty_name) const {
  379. String sty_name = p_sty_name.remove_chars(" -");
  380. if (sty_name.contains("ultracondensed")) {
  381. return 50;
  382. } else if (sty_name.contains("extracondensed")) {
  383. return 63;
  384. } else if (sty_name.contains("condensed")) {
  385. return 75;
  386. } else if (sty_name.contains("semicondensed")) {
  387. return 87;
  388. } else if (sty_name.contains("semiexpanded")) {
  389. return 113;
  390. } else if (sty_name.contains("expanded")) {
  391. return 125;
  392. } else if (sty_name.contains("extraexpanded")) {
  393. return 150;
  394. } else if (sty_name.contains("ultraexpanded")) {
  395. return 200;
  396. }
  397. return 100;
  398. }
  399. _FORCE_INLINE_ bool _is_ital_style(const String &p_sty_name) const {
  400. return p_sty_name.contains("italic") || p_sty_name.contains("oblique");
  401. }
  402. // Shaped text cache data.
  403. struct TrimData {
  404. int trim_pos = -1;
  405. int ellipsis_pos = -1;
  406. Vector<Glyph> ellipsis_glyph_buf;
  407. };
  408. struct TextRun {
  409. Vector2i range;
  410. RID font_rid;
  411. int font_size = 0;
  412. bool rtl = false;
  413. int64_t span_index = -1;
  414. };
  415. struct ShapedTextDataAdvanced {
  416. Mutex mutex;
  417. /* Source data */
  418. RID parent; // Substring parent ShapedTextData.
  419. int start = 0; // Substring start offset in the parent string.
  420. int end = 0; // Substring end offset in the parent string.
  421. String text;
  422. String custom_punct;
  423. TextServer::Direction direction = DIRECTION_LTR; // Desired text direction.
  424. TextServer::Orientation orientation = ORIENTATION_HORIZONTAL;
  425. struct Span {
  426. int start = -1;
  427. int end = -1;
  428. Array fonts;
  429. int font_size = 0;
  430. Variant embedded_key;
  431. String language;
  432. Dictionary features;
  433. Variant meta;
  434. };
  435. Vector<Span> spans;
  436. int first_span = 0; // First span in the parent ShapedTextData.
  437. int last_span = 0;
  438. Vector<TextRun> runs;
  439. bool runs_dirty = true;
  440. struct EmbeddedObject {
  441. int start = -1;
  442. int end = -1;
  443. InlineAlignment inline_align = INLINE_ALIGNMENT_CENTER;
  444. Rect2 rect;
  445. double baseline = 0;
  446. };
  447. HashMap<Variant, EmbeddedObject> objects;
  448. /* Shaped data */
  449. TextServer::Direction para_direction = DIRECTION_LTR; // Detected text direction.
  450. int base_para_direction = UBIDI_DEFAULT_LTR;
  451. SafeFlag valid{ false }; // String is shaped.
  452. bool line_breaks_valid = false; // Line and word break flags are populated (and virtual zero width spaces inserted).
  453. bool justification_ops_valid = false; // Virtual elongation glyphs are added to the string.
  454. bool sort_valid = false;
  455. bool text_trimmed = false;
  456. bool preserve_invalid = true; // Draw hex code box instead of missing characters.
  457. bool preserve_control = false; // Draw control characters.
  458. double ascent = 0.0; // Ascent for horizontal layout, 1/2 of width for vertical.
  459. double descent = 0.0; // Descent for horizontal layout, 1/2 of width for vertical.
  460. double width = 0.0; // Width for horizontal layout, height for vertical.
  461. double width_trimmed = 0.0;
  462. int extra_spacing[4] = { 0, 0, 0, 0 };
  463. double upos = 0.0;
  464. double uthk = 0.0;
  465. char32_t el_char = 0x2026;
  466. TrimData overrun_trim_data;
  467. bool fit_width_minimum_reached = false;
  468. LocalVector<Glyph> glyphs;
  469. LocalVector<Glyph> glyphs_logical;
  470. /* Intermediate data */
  471. Char16String utf16;
  472. Vector<UBiDi *> bidi_iter;
  473. Vector<Vector3i> bidi_override;
  474. ScriptIterator *script_iter = nullptr;
  475. hb_buffer_t *hb_buffer = nullptr;
  476. HashMap<int, bool> jstops;
  477. HashMap<int, bool> breaks;
  478. PackedInt32Array chars;
  479. int break_inserts = 0;
  480. bool break_ops_valid = false;
  481. bool js_ops_valid = false;
  482. bool chars_valid = false;
  483. ~ShapedTextDataAdvanced() {
  484. for (int i = 0; i < bidi_iter.size(); i++) {
  485. if (bidi_iter[i]) {
  486. ubidi_close(bidi_iter[i]);
  487. }
  488. }
  489. if (script_iter) {
  490. memdelete(script_iter);
  491. }
  492. if (hb_buffer) {
  493. hb_buffer_destroy(hb_buffer);
  494. }
  495. }
  496. };
  497. // Common data.
  498. mutable RID_PtrOwner<FontAdvancedLinkedVariation> font_var_owner;
  499. mutable RID_PtrOwner<FontAdvanced> font_owner;
  500. mutable RID_PtrOwner<ShapedTextDataAdvanced> shaped_owner;
  501. _FORCE_INLINE_ FontAdvanced *_get_font_data(const RID &p_font_rid) const {
  502. RID rid = p_font_rid;
  503. FontAdvancedLinkedVariation *fdv = font_var_owner.get_or_null(rid);
  504. if (unlikely(fdv)) {
  505. rid = fdv->base_font;
  506. }
  507. return font_owner.get_or_null(rid);
  508. }
  509. struct SystemFontKey {
  510. String font_name;
  511. TextServer::FontAntialiasing antialiasing = TextServer::FONT_ANTIALIASING_GRAY;
  512. bool disable_embedded_bitmaps = true;
  513. bool italic = false;
  514. bool mipmaps = false;
  515. bool msdf = false;
  516. bool force_autohinter = false;
  517. int weight = 400;
  518. int stretch = 100;
  519. int msdf_range = 14;
  520. int msdf_source_size = 48;
  521. int fixed_size = 0;
  522. TextServer::Hinting hinting = TextServer::HINTING_LIGHT;
  523. TextServer::SubpixelPositioning subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_AUTO;
  524. bool keep_rounding_remainders = true;
  525. Dictionary variation_coordinates;
  526. double embolden = 0.0;
  527. Transform2D transform;
  528. int extra_spacing[4] = { 0, 0, 0, 0 };
  529. double baseline_offset = 0.0;
  530. bool operator==(const SystemFontKey &p_b) const {
  531. 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);
  532. }
  533. SystemFontKey(const String &p_font_name, bool p_italic, int p_weight, int p_stretch, RID p_font, const TextServerAdvanced *p_fb) {
  534. font_name = p_font_name;
  535. italic = p_italic;
  536. weight = p_weight;
  537. stretch = p_stretch;
  538. antialiasing = p_fb->_font_get_antialiasing(p_font);
  539. disable_embedded_bitmaps = p_fb->_font_get_disable_embedded_bitmaps(p_font);
  540. mipmaps = p_fb->_font_get_generate_mipmaps(p_font);
  541. msdf = p_fb->_font_is_multichannel_signed_distance_field(p_font);
  542. msdf_range = p_fb->_font_get_msdf_pixel_range(p_font);
  543. msdf_source_size = p_fb->_font_get_msdf_size(p_font);
  544. fixed_size = p_fb->_font_get_fixed_size(p_font);
  545. force_autohinter = p_fb->_font_is_force_autohinter(p_font);
  546. hinting = p_fb->_font_get_hinting(p_font);
  547. subpixel_positioning = p_fb->_font_get_subpixel_positioning(p_font);
  548. keep_rounding_remainders = p_fb->_font_get_keep_rounding_remainders(p_font);
  549. variation_coordinates = p_fb->_font_get_variation_coordinates(p_font);
  550. embolden = p_fb->_font_get_embolden(p_font);
  551. transform = p_fb->_font_get_transform(p_font);
  552. extra_spacing[SPACING_TOP] = p_fb->_font_get_spacing(p_font, SPACING_TOP);
  553. extra_spacing[SPACING_BOTTOM] = p_fb->_font_get_spacing(p_font, SPACING_BOTTOM);
  554. extra_spacing[SPACING_SPACE] = p_fb->_font_get_spacing(p_font, SPACING_SPACE);
  555. extra_spacing[SPACING_GLYPH] = p_fb->_font_get_spacing(p_font, SPACING_GLYPH);
  556. baseline_offset = p_fb->_font_get_baseline_offset(p_font);
  557. }
  558. };
  559. struct SystemFontCacheRec {
  560. RID rid;
  561. int index = 0;
  562. };
  563. struct SystemFontCache {
  564. Vector<SystemFontCacheRec> var;
  565. int max_var = 0;
  566. };
  567. struct SystemFontKeyHasher {
  568. _FORCE_INLINE_ static uint32_t hash(const SystemFontKey &p_a) {
  569. uint32_t hash = p_a.font_name.hash();
  570. hash = hash_murmur3_one_32(p_a.variation_coordinates.hash(), hash);
  571. hash = hash_murmur3_one_32(p_a.weight, hash);
  572. hash = hash_murmur3_one_32(p_a.stretch, hash);
  573. hash = hash_murmur3_one_32(p_a.msdf_range, hash);
  574. hash = hash_murmur3_one_32(p_a.msdf_source_size, hash);
  575. hash = hash_murmur3_one_32(p_a.fixed_size, hash);
  576. hash = hash_murmur3_one_double(p_a.embolden, hash);
  577. hash = hash_murmur3_one_real(p_a.transform[0].x, hash);
  578. hash = hash_murmur3_one_real(p_a.transform[0].y, hash);
  579. hash = hash_murmur3_one_real(p_a.transform[1].x, hash);
  580. hash = hash_murmur3_one_real(p_a.transform[1].y, hash);
  581. hash = hash_murmur3_one_32(p_a.extra_spacing[SPACING_TOP], hash);
  582. hash = hash_murmur3_one_32(p_a.extra_spacing[SPACING_BOTTOM], hash);
  583. hash = hash_murmur3_one_32(p_a.extra_spacing[SPACING_SPACE], hash);
  584. hash = hash_murmur3_one_32(p_a.extra_spacing[SPACING_GLYPH], hash);
  585. hash = hash_murmur3_one_double(p_a.baseline_offset, hash);
  586. 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));
  587. }
  588. };
  589. mutable HashMap<SystemFontKey, SystemFontCache, SystemFontKeyHasher> system_fonts;
  590. mutable HashMap<String, PackedByteArray> system_font_data;
  591. void _update_chars(ShapedTextDataAdvanced *p_sd) const;
  592. void _generate_runs(ShapedTextDataAdvanced *p_sd) const;
  593. void _realign(ShapedTextDataAdvanced *p_sd) const;
  594. int64_t _convert_pos(const String &p_utf32, const Char16String &p_utf16, int64_t p_pos) const;
  595. int64_t _convert_pos(const ShapedTextDataAdvanced *p_sd, int64_t p_pos) const;
  596. int64_t _convert_pos_inv(const ShapedTextDataAdvanced *p_sd, int64_t p_pos) const;
  597. bool _shape_substr(ShapedTextDataAdvanced *p_new_sd, const ShapedTextDataAdvanced *p_sd, int64_t p_start, int64_t p_length) const;
  598. struct FontPriorityList {
  599. friend class TextServerAdvanced;
  600. const int PRIORITY_SKIP = 100; // Font already used.
  601. const int PRIORITY_MAX = 2;
  602. int current_priority = 0;
  603. uint32_t current_index = 0;
  604. uint32_t font_count = 0;
  605. String language;
  606. String script_code;
  607. bool color = false;
  608. LocalVector<Pair<RID, int>> unprocessed_fonts;
  609. LocalVector<RID> fonts;
  610. const TextServerAdvanced *text_server;
  611. FontPriorityList(const TextServerAdvanced *p_text_server, const Array &p_fonts, const String &p_language, const String &p_script_code, bool p_color) {
  612. text_server = p_text_server;
  613. language = p_language;
  614. script_code = p_script_code;
  615. font_count = p_fonts.size();
  616. color = p_color;
  617. unprocessed_fonts.reserve(font_count);
  618. for (uint32_t i = 0; i < font_count; i++) {
  619. unprocessed_fonts.push_back(Pair<RID, int>(p_fonts[i], -1));
  620. }
  621. fonts.reserve(font_count);
  622. if (font_count > 0) {
  623. fonts.push_back(p_fonts[0]);
  624. unprocessed_fonts[0].second = PRIORITY_SKIP;
  625. current_index++;
  626. }
  627. }
  628. _FORCE_INLINE_ uint32_t size() const {
  629. return font_count;
  630. }
  631. _FORCE_INLINE_ int _get_priority(const RID &p_font) {
  632. if (color && text_server->_font_is_color(p_font)) {
  633. return 0;
  634. }
  635. return text_server->_font_is_script_supported(p_font, script_code) ? (text_server->_font_is_language_supported(p_font, language) ? 0 : 1) : 2;
  636. }
  637. RID operator[](uint32_t p_index) {
  638. if (p_index < fonts.size()) {
  639. return fonts[p_index];
  640. }
  641. while (current_priority < PRIORITY_MAX || current_index < font_count) {
  642. if (current_index >= font_count) {
  643. current_priority++;
  644. current_index = 0;
  645. }
  646. const RID &font = unprocessed_fonts[current_index].first;
  647. int &priority = unprocessed_fonts[current_index].second;
  648. if (priority < 0) {
  649. priority = _get_priority(font);
  650. }
  651. if (priority == current_priority) {
  652. unprocessed_fonts[current_index].second = PRIORITY_SKIP;
  653. fonts.push_back(font);
  654. if (p_index < fonts.size()) {
  655. return fonts[p_index];
  656. }
  657. }
  658. current_index++;
  659. }
  660. return RID();
  661. }
  662. };
  663. void _shape_run(ShapedTextDataAdvanced *p_sd, int64_t p_start, int64_t p_end, const String &p_language, hb_script_t p_script, hb_direction_t p_direction, FontPriorityList &p_fonts, int64_t p_span, int64_t p_fb_index, int64_t p_prev_start, int64_t p_prev_end, RID p_prev_font);
  664. 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);
  665. _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);
  666. _FORCE_INLINE_ void _add_features(const Dictionary &p_source, Vector<hb_feature_t> &r_ftrs);
  667. String os_locale;
  668. Mutex ft_mutex;
  669. // HarfBuzz bitmap font interface.
  670. static hb_font_funcs_t *funcs;
  671. struct bmp_font_t {
  672. TextServerAdvanced::FontForSizeAdvanced *face = nullptr;
  673. bool unref = false; /* Whether to destroy bm_face when done. */
  674. };
  675. static bmp_font_t *_bmp_font_create(TextServerAdvanced::FontForSizeAdvanced *p_face, bool p_unref);
  676. static void _bmp_font_destroy(void *p_data);
  677. 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);
  678. 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);
  679. 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);
  680. 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);
  681. 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);
  682. 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);
  683. 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);
  684. static void _bmp_create_font_funcs();
  685. static void _bmp_free_font_funcs();
  686. static void _bmp_font_set_funcs(hb_font_t *p_font, TextServerAdvanced::FontForSizeAdvanced *p_face, bool p_unref);
  687. static hb_font_t *_bmp_font_create(TextServerAdvanced::FontForSizeAdvanced *p_face, hb_destroy_func_t p_destroy);
  688. hb_font_t *_font_get_hb_handle(const RID &p_font, int64_t p_font_size, bool &r_is_color) const;
  689. bool _font_is_color(const RID &p_font) const;
  690. struct GlyphCompare { // For line breaking reordering.
  691. _FORCE_INLINE_ bool operator()(const Glyph &l, const Glyph &r) const {
  692. if (l.start == r.start) {
  693. if (l.count == r.count) {
  694. return (l.flags & TextServer::GRAPHEME_IS_VIRTUAL) < (r.flags & TextServer::GRAPHEME_IS_VIRTUAL);
  695. }
  696. return l.count > r.count; // Sort first glyph with count & flags, order of the rest are irrelevant.
  697. } else {
  698. return l.start < r.start;
  699. }
  700. }
  701. };
  702. protected:
  703. static void _bind_methods() {}
  704. void full_copy(ShapedTextDataAdvanced *p_shaped);
  705. void invalidate(ShapedTextDataAdvanced *p_shaped, bool p_text = false);
  706. public:
  707. MODBIND1RC(bool, has_feature, Feature);
  708. MODBIND0RC(String, get_name);
  709. MODBIND0RC(int64_t, get_features);
  710. MODBIND1(free_rid, const RID &);
  711. MODBIND1R(bool, has, const RID &);
  712. MODBIND1R(bool, load_support_data, const String &);
  713. MODBIND0RC(String, get_support_data_filename);
  714. MODBIND0RC(String, get_support_data_info);
  715. MODBIND1RC(bool, save_support_data, const String &);
  716. MODBIND0RC(PackedByteArray, get_support_data);
  717. MODBIND1RC(bool, is_locale_using_support_data, const String &);
  718. MODBIND1RC(bool, is_locale_right_to_left, const String &);
  719. MODBIND1RC(int64_t, name_to_tag, const String &);
  720. MODBIND1RC(String, tag_to_name, int64_t);
  721. /* Font interface */
  722. MODBIND0R(RID, create_font);
  723. MODBIND1R(RID, create_font_linked_variation, const RID &);
  724. MODBIND2(font_set_data, const RID &, const PackedByteArray &);
  725. MODBIND3(font_set_data_ptr, const RID &, const uint8_t *, int64_t);
  726. MODBIND2(font_set_face_index, const RID &, int64_t);
  727. MODBIND1RC(int64_t, font_get_face_index, const RID &);
  728. MODBIND1RC(int64_t, font_get_face_count, const RID &);
  729. MODBIND2(font_set_style, const RID &, BitField<FontStyle>);
  730. MODBIND1RC(BitField<FontStyle>, font_get_style, const RID &);
  731. MODBIND2(font_set_style_name, const RID &, const String &);
  732. MODBIND1RC(String, font_get_style_name, const RID &);
  733. MODBIND2(font_set_weight, const RID &, int64_t);
  734. MODBIND1RC(int64_t, font_get_weight, const RID &);
  735. MODBIND2(font_set_stretch, const RID &, int64_t);
  736. MODBIND1RC(int64_t, font_get_stretch, const RID &);
  737. MODBIND2(font_set_name, const RID &, const String &);
  738. MODBIND1RC(String, font_get_name, const RID &);
  739. MODBIND1RC(Dictionary, font_get_ot_name_strings, const RID &);
  740. MODBIND2(font_set_antialiasing, const RID &, TextServer::FontAntialiasing);
  741. MODBIND1RC(TextServer::FontAntialiasing, font_get_antialiasing, const RID &);
  742. MODBIND2(font_set_disable_embedded_bitmaps, const RID &, bool);
  743. MODBIND1RC(bool, font_get_disable_embedded_bitmaps, const RID &);
  744. MODBIND2(font_set_generate_mipmaps, const RID &, bool);
  745. MODBIND1RC(bool, font_get_generate_mipmaps, const RID &);
  746. MODBIND2(font_set_multichannel_signed_distance_field, const RID &, bool);
  747. MODBIND1RC(bool, font_is_multichannel_signed_distance_field, const RID &);
  748. MODBIND2(font_set_msdf_pixel_range, const RID &, int64_t);
  749. MODBIND1RC(int64_t, font_get_msdf_pixel_range, const RID &);
  750. MODBIND2(font_set_msdf_size, const RID &, int64_t);
  751. MODBIND1RC(int64_t, font_get_msdf_size, const RID &);
  752. MODBIND2(font_set_fixed_size, const RID &, int64_t);
  753. MODBIND1RC(int64_t, font_get_fixed_size, const RID &);
  754. MODBIND2(font_set_fixed_size_scale_mode, const RID &, FixedSizeScaleMode);
  755. MODBIND1RC(FixedSizeScaleMode, font_get_fixed_size_scale_mode, const RID &);
  756. MODBIND2(font_set_allow_system_fallback, const RID &, bool);
  757. MODBIND1RC(bool, font_is_allow_system_fallback, const RID &);
  758. MODBIND0(font_clear_system_fallback_cache);
  759. MODBIND2(font_set_force_autohinter, const RID &, bool);
  760. MODBIND1RC(bool, font_is_force_autohinter, const RID &);
  761. MODBIND2(font_set_modulate_color_glyphs, const RID &, bool);
  762. MODBIND1RC(bool, font_is_modulate_color_glyphs, const RID &);
  763. MODBIND2(font_set_subpixel_positioning, const RID &, SubpixelPositioning);
  764. MODBIND1RC(SubpixelPositioning, font_get_subpixel_positioning, const RID &);
  765. MODBIND2(font_set_keep_rounding_remainders, const RID &, bool);
  766. MODBIND1RC(bool, font_get_keep_rounding_remainders, const RID &);
  767. MODBIND2(font_set_embolden, const RID &, double);
  768. MODBIND1RC(double, font_get_embolden, const RID &);
  769. MODBIND3(font_set_spacing, const RID &, SpacingType, int64_t);
  770. MODBIND2RC(int64_t, font_get_spacing, const RID &, SpacingType);
  771. MODBIND2(font_set_baseline_offset, const RID &, double);
  772. MODBIND1RC(double, font_get_baseline_offset, const RID &);
  773. MODBIND2(font_set_transform, const RID &, const Transform2D &);
  774. MODBIND1RC(Transform2D, font_get_transform, const RID &);
  775. MODBIND2(font_set_variation_coordinates, const RID &, const Dictionary &);
  776. MODBIND1RC(Dictionary, font_get_variation_coordinates, const RID &);
  777. MODBIND2(font_set_oversampling, const RID &, double);
  778. MODBIND1RC(double, font_get_oversampling, const RID &);
  779. MODBIND2(font_set_hinting, const RID &, TextServer::Hinting);
  780. MODBIND1RC(TextServer::Hinting, font_get_hinting, const RID &);
  781. MODBIND1RC(TypedArray<Vector2i>, font_get_size_cache_list, const RID &);
  782. MODBIND1(font_clear_size_cache, const RID &);
  783. MODBIND2(font_remove_size_cache, const RID &, const Vector2i &);
  784. MODBIND1RC(TypedArray<Dictionary>, font_get_size_cache_info, const RID &);
  785. MODBIND3(font_set_ascent, const RID &, int64_t, double);
  786. MODBIND2RC(double, font_get_ascent, const RID &, int64_t);
  787. MODBIND3(font_set_descent, const RID &, int64_t, double);
  788. MODBIND2RC(double, font_get_descent, const RID &, int64_t);
  789. MODBIND3(font_set_underline_position, const RID &, int64_t, double);
  790. MODBIND2RC(double, font_get_underline_position, const RID &, int64_t);
  791. MODBIND3(font_set_underline_thickness, const RID &, int64_t, double);
  792. MODBIND2RC(double, font_get_underline_thickness, const RID &, int64_t);
  793. MODBIND3(font_set_scale, const RID &, int64_t, double);
  794. MODBIND2RC(double, font_get_scale, const RID &, int64_t);
  795. MODBIND2RC(int64_t, font_get_texture_count, const RID &, const Vector2i &);
  796. MODBIND2(font_clear_textures, const RID &, const Vector2i &);
  797. MODBIND3(font_remove_texture, const RID &, const Vector2i &, int64_t);
  798. MODBIND4(font_set_texture_image, const RID &, const Vector2i &, int64_t, const Ref<Image> &);
  799. MODBIND3RC(Ref<Image>, font_get_texture_image, const RID &, const Vector2i &, int64_t);
  800. MODBIND4(font_set_texture_offsets, const RID &, const Vector2i &, int64_t, const PackedInt32Array &);
  801. MODBIND3RC(PackedInt32Array, font_get_texture_offsets, const RID &, const Vector2i &, int64_t);
  802. MODBIND2RC(PackedInt32Array, font_get_glyph_list, const RID &, const Vector2i &);
  803. MODBIND2(font_clear_glyphs, const RID &, const Vector2i &);
  804. MODBIND3(font_remove_glyph, const RID &, const Vector2i &, int64_t);
  805. MODBIND3RC(Vector2, font_get_glyph_advance, const RID &, int64_t, int64_t);
  806. MODBIND4(font_set_glyph_advance, const RID &, int64_t, int64_t, const Vector2 &);
  807. MODBIND3RC(Vector2, font_get_glyph_offset, const RID &, const Vector2i &, int64_t);
  808. MODBIND4(font_set_glyph_offset, const RID &, const Vector2i &, int64_t, const Vector2 &);
  809. MODBIND3RC(Vector2, font_get_glyph_size, const RID &, const Vector2i &, int64_t);
  810. MODBIND4(font_set_glyph_size, const RID &, const Vector2i &, int64_t, const Vector2 &);
  811. MODBIND3RC(Rect2, font_get_glyph_uv_rect, const RID &, const Vector2i &, int64_t);
  812. MODBIND4(font_set_glyph_uv_rect, const RID &, const Vector2i &, int64_t, const Rect2 &);
  813. MODBIND3RC(int64_t, font_get_glyph_texture_idx, const RID &, const Vector2i &, int64_t);
  814. MODBIND4(font_set_glyph_texture_idx, const RID &, const Vector2i &, int64_t, int64_t);
  815. MODBIND3RC(RID, font_get_glyph_texture_rid, const RID &, const Vector2i &, int64_t);
  816. MODBIND3RC(Size2, font_get_glyph_texture_size, const RID &, const Vector2i &, int64_t);
  817. MODBIND3RC(Dictionary, font_get_glyph_contours, const RID &, int64_t, int64_t);
  818. MODBIND2RC(TypedArray<Vector2i>, font_get_kerning_list, const RID &, int64_t);
  819. MODBIND2(font_clear_kerning_map, const RID &, int64_t);
  820. MODBIND3(font_remove_kerning, const RID &, int64_t, const Vector2i &);
  821. MODBIND4(font_set_kerning, const RID &, int64_t, const Vector2i &, const Vector2 &);
  822. MODBIND3RC(Vector2, font_get_kerning, const RID &, int64_t, const Vector2i &);
  823. MODBIND4RC(int64_t, font_get_glyph_index, const RID &, int64_t, int64_t, int64_t);
  824. MODBIND3RC(int64_t, font_get_char_from_glyph_index, const RID &, int64_t, int64_t);
  825. MODBIND2RC(bool, font_has_char, const RID &, int64_t);
  826. MODBIND1RC(String, font_get_supported_chars, const RID &);
  827. MODBIND1RC(PackedInt32Array, font_get_supported_glyphs, const RID &);
  828. MODBIND4(font_render_range, const RID &, const Vector2i &, int64_t, int64_t);
  829. MODBIND3(font_render_glyph, const RID &, const Vector2i &, int64_t);
  830. MODBIND7C(font_draw_glyph, const RID &, const RID &, int64_t, const Vector2 &, int64_t, const Color &, float);
  831. MODBIND8C(font_draw_glyph_outline, const RID &, const RID &, int64_t, int64_t, const Vector2 &, int64_t, const Color &, float);
  832. MODBIND2RC(bool, font_is_language_supported, const RID &, const String &);
  833. MODBIND3(font_set_language_support_override, const RID &, const String &, bool);
  834. MODBIND2R(bool, font_get_language_support_override, const RID &, const String &);
  835. MODBIND2(font_remove_language_support_override, const RID &, const String &);
  836. MODBIND1R(PackedStringArray, font_get_language_support_overrides, const RID &);
  837. MODBIND2RC(bool, font_is_script_supported, const RID &, const String &);
  838. MODBIND3(font_set_script_support_override, const RID &, const String &, bool);
  839. MODBIND2R(bool, font_get_script_support_override, const RID &, const String &);
  840. MODBIND2(font_remove_script_support_override, const RID &, const String &);
  841. MODBIND1R(PackedStringArray, font_get_script_support_overrides, const RID &);
  842. MODBIND2(font_set_opentype_feature_overrides, const RID &, const Dictionary &);
  843. MODBIND1RC(Dictionary, font_get_opentype_feature_overrides, const RID &);
  844. MODBIND1RC(Dictionary, font_supported_feature_list, const RID &);
  845. MODBIND1RC(Dictionary, font_supported_variation_list, const RID &);
  846. MODBIND1(reference_oversampling_level, double);
  847. MODBIND1(unreference_oversampling_level, double);
  848. /* Shaped text buffer interface */
  849. MODBIND2R(RID, create_shaped_text, Direction, Orientation);
  850. MODBIND1(shaped_text_clear, const RID &);
  851. MODBIND1R(RID, shaped_text_duplicate, const RID &);
  852. MODBIND2(shaped_text_set_direction, const RID &, Direction);
  853. MODBIND1RC(Direction, shaped_text_get_direction, const RID &);
  854. MODBIND1RC(Direction, shaped_text_get_inferred_direction, const RID &);
  855. MODBIND2(shaped_text_set_bidi_override, const RID &, const Array &);
  856. MODBIND2(shaped_text_set_custom_punctuation, const RID &, const String &);
  857. MODBIND1RC(String, shaped_text_get_custom_punctuation, const RID &);
  858. MODBIND2(shaped_text_set_custom_ellipsis, const RID &, int64_t);
  859. MODBIND1RC(int64_t, shaped_text_get_custom_ellipsis, const RID &);
  860. MODBIND2(shaped_text_set_orientation, const RID &, Orientation);
  861. MODBIND1RC(Orientation, shaped_text_get_orientation, const RID &);
  862. MODBIND2(shaped_text_set_preserve_invalid, const RID &, bool);
  863. MODBIND1RC(bool, shaped_text_get_preserve_invalid, const RID &);
  864. MODBIND2(shaped_text_set_preserve_control, const RID &, bool);
  865. MODBIND1RC(bool, shaped_text_get_preserve_control, const RID &);
  866. MODBIND3(shaped_text_set_spacing, const RID &, SpacingType, int64_t);
  867. MODBIND2RC(int64_t, shaped_text_get_spacing, const RID &, SpacingType);
  868. MODBIND7R(bool, shaped_text_add_string, const RID &, const String &, const TypedArray<RID> &, int64_t, const Dictionary &, const String &, const Variant &);
  869. MODBIND6R(bool, shaped_text_add_object, const RID &, const Variant &, const Size2 &, InlineAlignment, int64_t, double);
  870. MODBIND5R(bool, shaped_text_resize_object, const RID &, const Variant &, const Size2 &, InlineAlignment, double);
  871. MODBIND2RC(bool, shaped_text_has_object, const RID &, const Variant &);
  872. MODBIND1RC(String, shaped_get_text, const RID &);
  873. MODBIND1RC(int64_t, shaped_get_span_count, const RID &);
  874. MODBIND2RC(Variant, shaped_get_span_meta, const RID &, int64_t);
  875. MODBIND2RC(Variant, shaped_get_span_embedded_object, const RID &, int64_t);
  876. MODBIND2RC(String, shaped_get_span_text, const RID &, int64_t);
  877. MODBIND2RC(Variant, shaped_get_span_object, const RID &, int64_t);
  878. MODBIND5(shaped_set_span_update_font, const RID &, int64_t, const TypedArray<RID> &, int64_t, const Dictionary &);
  879. MODBIND1RC(int64_t, shaped_get_run_count, const RID &);
  880. MODBIND2RC(String, shaped_get_run_text, const RID &, int64_t);
  881. MODBIND2RC(Vector2i, shaped_get_run_range, const RID &, int64_t);
  882. MODBIND2RC(RID, shaped_get_run_font_rid, const RID &, int64_t);
  883. MODBIND2RC(int, shaped_get_run_font_size, const RID &, int64_t);
  884. MODBIND2RC(String, shaped_get_run_language, const RID &, int64_t);
  885. MODBIND2RC(Direction, shaped_get_run_direction, const RID &, int64_t);
  886. MODBIND2RC(Variant, shaped_get_run_object, const RID &, int64_t);
  887. MODBIND3RC(RID, shaped_text_substr, const RID &, int64_t, int64_t);
  888. MODBIND1RC(RID, shaped_text_get_parent, const RID &);
  889. MODBIND3R(double, shaped_text_fit_to_width, const RID &, double, BitField<TextServer::JustificationFlag>);
  890. MODBIND2R(double, shaped_text_tab_align, const RID &, const PackedFloat32Array &);
  891. MODBIND1R(bool, shaped_text_shape, const RID &);
  892. MODBIND1R(bool, shaped_text_update_breaks, const RID &);
  893. MODBIND1R(bool, shaped_text_update_justification_ops, const RID &);
  894. MODBIND1RC(int64_t, shaped_text_get_trim_pos, const RID &);
  895. MODBIND1RC(int64_t, shaped_text_get_ellipsis_pos, const RID &);
  896. MODBIND1RC(const Glyph *, shaped_text_get_ellipsis_glyphs, const RID &);
  897. MODBIND1RC(int64_t, shaped_text_get_ellipsis_glyph_count, const RID &);
  898. MODBIND3(shaped_text_overrun_trim_to_width, const RID &, double, BitField<TextServer::TextOverrunFlag>);
  899. MODBIND1RC(bool, shaped_text_is_ready, const RID &);
  900. MODBIND1RC(const Glyph *, shaped_text_get_glyphs, const RID &);
  901. MODBIND1R(const Glyph *, shaped_text_sort_logical, const RID &);
  902. MODBIND1RC(int64_t, shaped_text_get_glyph_count, const RID &);
  903. MODBIND1RC(Vector2i, shaped_text_get_range, const RID &);
  904. MODBIND1RC(Array, shaped_text_get_objects, const RID &);
  905. MODBIND2RC(Rect2, shaped_text_get_object_rect, const RID &, const Variant &);
  906. MODBIND2RC(Vector2i, shaped_text_get_object_range, const RID &, const Variant &);
  907. MODBIND2RC(int64_t, shaped_text_get_object_glyph, const RID &, const Variant &);
  908. MODBIND1RC(Size2, shaped_text_get_size, const RID &);
  909. MODBIND1RC(double, shaped_text_get_ascent, const RID &);
  910. MODBIND1RC(double, shaped_text_get_descent, const RID &);
  911. MODBIND1RC(double, shaped_text_get_width, const RID &);
  912. MODBIND1RC(double, shaped_text_get_underline_position, const RID &);
  913. MODBIND1RC(double, shaped_text_get_underline_thickness, const RID &);
  914. MODBIND1RC(PackedInt32Array, shaped_text_get_character_breaks, const RID &);
  915. MODBIND3RC(PackedInt32Array, string_get_word_breaks, const String &, const String &, int64_t);
  916. MODBIND2RC(PackedInt32Array, string_get_character_breaks, const String &, const String &);
  917. MODBIND2RC(int64_t, is_confusable, const String &, const PackedStringArray &);
  918. MODBIND1RC(bool, spoof_check, const String &);
  919. MODBIND1RC(String, strip_diacritics, const String &);
  920. MODBIND1RC(bool, is_valid_identifier, const String &);
  921. MODBIND1RC(bool, is_valid_letter, uint64_t);
  922. MODBIND2RC(String, string_to_upper, const String &, const String &);
  923. MODBIND2RC(String, string_to_lower, const String &, const String &);
  924. MODBIND2RC(String, string_to_title, const String &, const String &);
  925. MODBIND0(cleanup);
  926. TextServerAdvanced();
  927. ~TextServerAdvanced();
  928. };