font.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /**************************************************************************/
  2. /* font.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 FONT_H
  31. #define FONT_H
  32. #include "core/io/resource.h"
  33. #include "core/templates/lru.h"
  34. #include "core/templates/rb_map.h"
  35. #include "scene/resources/texture.h"
  36. #include "servers/text_server.h"
  37. class TextLine;
  38. class TextParagraph;
  39. /*************************************************************************/
  40. /* Font */
  41. /*************************************************************************/
  42. class Font : public Resource {
  43. GDCLASS(Font, Resource);
  44. struct ShapedTextKey {
  45. String text;
  46. int font_size = 14;
  47. float width = 0.f;
  48. BitField<TextServer::JustificationFlag> jst_flags = TextServer::JUSTIFICATION_NONE;
  49. BitField<TextServer::LineBreakFlag> brk_flags = TextServer::BREAK_MANDATORY;
  50. TextServer::Direction direction = TextServer::DIRECTION_AUTO;
  51. TextServer::Orientation orientation = TextServer::ORIENTATION_HORIZONTAL;
  52. bool operator==(const ShapedTextKey &p_b) const {
  53. return (font_size == p_b.font_size) && (width == p_b.width) && (jst_flags == p_b.jst_flags) && (brk_flags == p_b.brk_flags) && (direction == p_b.direction) && (orientation == p_b.orientation) && (text == p_b.text);
  54. }
  55. ShapedTextKey() {}
  56. ShapedTextKey(const String &p_text, int p_font_size, float p_width, BitField<TextServer::JustificationFlag> p_jst_flags, BitField<TextServer::LineBreakFlag> p_brk_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation) {
  57. text = p_text;
  58. font_size = p_font_size;
  59. width = p_width;
  60. jst_flags = p_jst_flags;
  61. brk_flags = p_brk_flags;
  62. direction = p_direction;
  63. orientation = p_orientation;
  64. }
  65. };
  66. struct ShapedTextKeyHasher {
  67. _FORCE_INLINE_ static uint32_t hash(const ShapedTextKey &p_a) {
  68. uint32_t hash = p_a.text.hash();
  69. hash = hash_murmur3_one_32(p_a.font_size, hash);
  70. hash = hash_murmur3_one_float(p_a.width, hash);
  71. hash = hash_murmur3_one_32(p_a.brk_flags | (p_a.jst_flags << 6) | (p_a.direction << 12) | (p_a.orientation << 15), hash);
  72. return hash_fmix32(hash);
  73. }
  74. };
  75. // Shaped string cache.
  76. mutable LRUCache<ShapedTextKey, Ref<TextLine>, ShapedTextKeyHasher> cache;
  77. mutable LRUCache<ShapedTextKey, Ref<TextParagraph>, ShapedTextKeyHasher> cache_wrap;
  78. protected:
  79. // Output.
  80. mutable TypedArray<RID> rids;
  81. mutable bool dirty_rids = true;
  82. // Fallbacks.
  83. static constexpr int MAX_FALLBACK_DEPTH = 64;
  84. TypedArray<Font> fallbacks;
  85. static void _bind_methods();
  86. virtual void _update_rids_fb(const Ref<Font> &p_f, int p_depth) const;
  87. virtual void _update_rids() const;
  88. virtual void reset_state() override;
  89. #ifndef DISABLE_DEPRECATED
  90. RID _find_variation_compat_80954(const Dictionary &p_variation_coordinates, int p_face_index = 0, float p_strength = 0.0, Transform2D p_transform = Transform2D()) const;
  91. RID _find_variation_compat_87668(const Dictionary &p_variation_coordinates, int p_face_index = 0, float p_strength = 0.0, Transform2D p_transform = Transform2D(), int p_spacing_top = 0, int p_spacing_bottom = 0, int p_spacing_space = 0, int p_spacing_glyph = 0) const;
  92. static void _bind_compatibility_methods();
  93. #endif
  94. public:
  95. virtual bool _is_cyclic(const Ref<Font> &p_f, int p_depth) const;
  96. virtual bool _is_base_cyclic(const Ref<Font> &p_f, int p_depth) const;
  97. virtual void _invalidate_rids();
  98. static constexpr int DEFAULT_FONT_SIZE = 16;
  99. // Fallbacks.
  100. virtual void set_fallbacks(const TypedArray<Font> &p_fallbacks);
  101. virtual TypedArray<Font> get_fallbacks() const;
  102. // Output.
  103. virtual RID find_variation(const Dictionary &p_variation_coordinates, int p_face_index = 0, float p_strength = 0.0, Transform2D p_transform = Transform2D(), int p_spacing_top = 0, int p_spacing_bottom = 0, int p_spacing_space = 0, int p_spacing_glyph = 0, float p_baseline_offset = 0.0) const { return RID(); }
  104. virtual RID _get_rid() const { return RID(); }
  105. virtual TypedArray<RID> get_rids() const;
  106. // Font metrics.
  107. virtual real_t get_height(int p_font_size) const;
  108. virtual real_t get_ascent(int p_font_size) const;
  109. virtual real_t get_descent(int p_font_size) const;
  110. virtual real_t get_underline_position(int p_font_size) const;
  111. virtual real_t get_underline_thickness(int p_font_size) const;
  112. virtual String get_font_name() const;
  113. virtual String get_font_style_name() const;
  114. virtual Dictionary get_ot_name_strings() const;
  115. virtual BitField<TextServer::FontStyle> get_font_style() const;
  116. virtual int get_font_weight() const;
  117. virtual int get_font_stretch() const;
  118. virtual int get_spacing(TextServer::SpacingType p_spacing) const { return 0; }
  119. virtual Dictionary get_opentype_features() const;
  120. // Drawing string.
  121. virtual void set_cache_capacity(int p_single_line, int p_multi_line);
  122. virtual Size2 get_string_size(const String &p_text, HorizontalAlignment p_alignment = HORIZONTAL_ALIGNMENT_LEFT, float p_width = -1, int p_font_size = DEFAULT_FONT_SIZE, BitField<TextServer::JustificationFlag> p_jst_flags = TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND, TextServer::Direction p_direction = TextServer::DIRECTION_AUTO, TextServer::Orientation p_orientation = TextServer::ORIENTATION_HORIZONTAL) const;
  123. virtual Size2 get_multiline_string_size(const String &p_text, HorizontalAlignment p_alignment = HORIZONTAL_ALIGNMENT_LEFT, float p_width = -1, int p_font_size = DEFAULT_FONT_SIZE, int p_max_lines = -1, BitField<TextServer::LineBreakFlag> p_brk_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND, BitField<TextServer::JustificationFlag> p_jst_flags = TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND, TextServer::Direction p_direction = TextServer::DIRECTION_AUTO, TextServer::Orientation p_orientation = TextServer::ORIENTATION_HORIZONTAL) const;
  124. virtual void draw_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment = HORIZONTAL_ALIGNMENT_LEFT, float p_width = -1, int p_font_size = DEFAULT_FONT_SIZE, const Color &p_modulate = Color(1.0, 1.0, 1.0), BitField<TextServer::JustificationFlag> p_jst_flags = TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND, TextServer::Direction p_direction = TextServer::DIRECTION_AUTO, TextServer::Orientation p_orientation = TextServer::ORIENTATION_HORIZONTAL) const;
  125. virtual void draw_multiline_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment = HORIZONTAL_ALIGNMENT_LEFT, float p_width = -1, int p_font_size = DEFAULT_FONT_SIZE, int p_max_lines = -1, const Color &p_modulate = Color(1.0, 1.0, 1.0), BitField<TextServer::LineBreakFlag> p_brk_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND, BitField<TextServer::JustificationFlag> p_jst_flags = TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND, TextServer::Direction p_direction = TextServer::DIRECTION_AUTO, TextServer::Orientation p_orientation = TextServer::ORIENTATION_HORIZONTAL) const;
  126. virtual void draw_string_outline(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment = HORIZONTAL_ALIGNMENT_LEFT, float p_width = -1, int p_font_size = DEFAULT_FONT_SIZE, int p_size = 1, const Color &p_modulate = Color(1.0, 1.0, 1.0), BitField<TextServer::JustificationFlag> p_jst_flags = TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND, TextServer::Direction p_direction = TextServer::DIRECTION_AUTO, TextServer::Orientation p_orientation = TextServer::ORIENTATION_HORIZONTAL) const;
  127. virtual void draw_multiline_string_outline(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment = HORIZONTAL_ALIGNMENT_LEFT, float p_width = -1, int p_font_size = DEFAULT_FONT_SIZE, int p_max_lines = -1, int p_size = 1, const Color &p_modulate = Color(1.0, 1.0, 1.0), BitField<TextServer::LineBreakFlag> p_brk_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND, BitField<TextServer::JustificationFlag> p_jst_flags = TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND, TextServer::Direction p_direction = TextServer::DIRECTION_AUTO, TextServer::Orientation p_orientation = TextServer::ORIENTATION_HORIZONTAL) const;
  128. // Drawing char.
  129. virtual Size2 get_char_size(char32_t p_char, int p_font_size = DEFAULT_FONT_SIZE) const;
  130. virtual real_t draw_char(RID p_canvas_item, const Point2 &p_pos, char32_t p_char, int p_font_size = DEFAULT_FONT_SIZE, const Color &p_modulate = Color(1.0, 1.0, 1.0)) const;
  131. virtual real_t draw_char_outline(RID p_canvas_item, const Point2 &p_pos, char32_t p_char, int p_font_size = DEFAULT_FONT_SIZE, int p_size = 1, const Color &p_modulate = Color(1.0, 1.0, 1.0)) const;
  132. // Helper functions.
  133. virtual bool has_char(char32_t p_char) const;
  134. virtual String get_supported_chars() const;
  135. virtual bool is_language_supported(const String &p_language) const;
  136. virtual bool is_script_supported(const String &p_script) const;
  137. virtual Dictionary get_supported_feature_list() const;
  138. virtual Dictionary get_supported_variation_list() const;
  139. virtual int64_t get_face_count() const;
  140. Font();
  141. ~Font();
  142. };
  143. /*************************************************************************/
  144. /* FontFile */
  145. /*************************************************************************/
  146. class FontFile : public Font {
  147. GDCLASS(FontFile, Font);
  148. RES_BASE_EXTENSION("fontdata");
  149. // Font source data.
  150. const uint8_t *data_ptr = nullptr;
  151. size_t data_size = 0;
  152. PackedByteArray data;
  153. TextServer::FontAntialiasing antialiasing = TextServer::FONT_ANTIALIASING_GRAY;
  154. bool mipmaps = false;
  155. bool disable_embedded_bitmaps = true;
  156. bool msdf = false;
  157. int msdf_pixel_range = 16;
  158. int msdf_size = 48;
  159. int fixed_size = 0;
  160. TextServer::FixedSizeScaleMode fixed_size_scale_mode = TextServer::FIXED_SIZE_SCALE_DISABLE;
  161. bool force_autohinter = false;
  162. bool allow_system_fallback = true;
  163. TextServer::Hinting hinting = TextServer::HINTING_LIGHT;
  164. TextServer::SubpixelPositioning subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_AUTO;
  165. real_t oversampling = 0.f;
  166. #ifndef DISABLE_DEPRECATED
  167. real_t bmp_height = 0.0;
  168. real_t bmp_ascent = 0.0;
  169. #endif
  170. // Cache.
  171. mutable Vector<RID> cache;
  172. _FORCE_INLINE_ void _clear_cache();
  173. _FORCE_INLINE_ void _ensure_rid(int p_cache_index, int p_make_linked_from = -1) const;
  174. void _convert_packed_8bit(Ref<Image> &p_source, int p_page, int p_sz);
  175. void _convert_packed_4bit(Ref<Image> &p_source, int p_page, int p_sz);
  176. void _convert_rgba_4bit(Ref<Image> &p_source, int p_page, int p_sz);
  177. void _convert_mono_8bit(Ref<Image> &p_source, int p_page, int p_ch, int p_sz, int p_ol);
  178. void _convert_mono_4bit(Ref<Image> &p_source, int p_page, int p_ch, int p_sz, int p_ol);
  179. protected:
  180. static void _bind_methods();
  181. void _validate_property(PropertyInfo &p_property) const;
  182. bool _set(const StringName &p_name, const Variant &p_value);
  183. bool _get(const StringName &p_name, Variant &r_ret) const;
  184. void _get_property_list(List<PropertyInfo> *p_list) const;
  185. virtual void reset_state() override;
  186. public:
  187. Error _load_bitmap_font(const String &p_path, List<String> *r_image_files);
  188. Error load_bitmap_font(const String &p_path);
  189. Error load_dynamic_font(const String &p_path);
  190. // Font source data.
  191. virtual void set_data_ptr(const uint8_t *p_data, size_t p_size);
  192. virtual void set_data(const PackedByteArray &p_data);
  193. virtual PackedByteArray get_data() const;
  194. // Common properties.
  195. virtual void set_font_name(const String &p_name);
  196. virtual void set_font_style_name(const String &p_name);
  197. virtual void set_font_style(BitField<TextServer::FontStyle> p_style);
  198. virtual void set_font_weight(int p_weight);
  199. virtual void set_font_stretch(int p_stretch);
  200. virtual void set_antialiasing(TextServer::FontAntialiasing p_antialiasing);
  201. virtual TextServer::FontAntialiasing get_antialiasing() const;
  202. virtual void set_disable_embedded_bitmaps(bool p_disable_embedded_bitmaps);
  203. virtual bool get_disable_embedded_bitmaps() const;
  204. virtual void set_generate_mipmaps(bool p_generate_mipmaps);
  205. virtual bool get_generate_mipmaps() const;
  206. virtual void set_multichannel_signed_distance_field(bool p_msdf);
  207. virtual bool is_multichannel_signed_distance_field() const;
  208. virtual void set_msdf_pixel_range(int p_msdf_pixel_range);
  209. virtual int get_msdf_pixel_range() const;
  210. virtual void set_msdf_size(int p_msdf_size);
  211. virtual int get_msdf_size() const;
  212. virtual void set_fixed_size(int p_fixed_size);
  213. virtual int get_fixed_size() const;
  214. virtual void set_fixed_size_scale_mode(TextServer::FixedSizeScaleMode p_fixed_size_scale_mode);
  215. virtual TextServer::FixedSizeScaleMode get_fixed_size_scale_mode() const;
  216. virtual void set_allow_system_fallback(bool p_allow_system_fallback);
  217. virtual bool is_allow_system_fallback() const;
  218. virtual void set_force_autohinter(bool p_force_autohinter);
  219. virtual bool is_force_autohinter() const;
  220. virtual void set_hinting(TextServer::Hinting p_hinting);
  221. virtual TextServer::Hinting get_hinting() const;
  222. virtual void set_subpixel_positioning(TextServer::SubpixelPositioning p_subpixel);
  223. virtual TextServer::SubpixelPositioning get_subpixel_positioning() const;
  224. virtual void set_oversampling(real_t p_oversampling);
  225. virtual real_t get_oversampling() const;
  226. // Cache.
  227. virtual RID find_variation(const Dictionary &p_variation_coordinates, int p_face_index = 0, float p_strength = 0.0, Transform2D p_transform = Transform2D(), int p_spacing_top = 0, int p_spacing_bottom = 0, int p_spacing_space = 0, int p_spacing_glyph = 0, float p_baseline_offset = 0.0) const override;
  228. virtual RID _get_rid() const override;
  229. virtual int get_cache_count() const;
  230. virtual void clear_cache();
  231. virtual void remove_cache(int p_cache_index);
  232. virtual TypedArray<Vector2i> get_size_cache_list(int p_cache_index) const;
  233. virtual void clear_size_cache(int p_cache_index);
  234. virtual void remove_size_cache(int p_cache_index, const Vector2i &p_size);
  235. virtual void set_variation_coordinates(int p_cache_index, const Dictionary &p_variation_coordinates);
  236. virtual Dictionary get_variation_coordinates(int p_cache_index) const;
  237. virtual void set_embolden(int p_cache_index, float p_strength);
  238. virtual float get_embolden(int p_cache_index) const;
  239. virtual void set_transform(int p_cache_index, Transform2D p_transform);
  240. virtual Transform2D get_transform(int p_cache_index) const;
  241. virtual void set_extra_spacing(int p_cache_index, TextServer::SpacingType p_spacing, int64_t p_value);
  242. virtual int64_t get_extra_spacing(int p_cache_index, TextServer::SpacingType p_spacing) const;
  243. virtual float get_extra_baseline_offset(int p_cache_index) const;
  244. virtual void set_extra_baseline_offset(int p_cache_index, float p_baseline_offset);
  245. virtual void set_face_index(int p_cache_index, int64_t p_index);
  246. virtual int64_t get_face_index(int p_cache_index) const;
  247. virtual void set_cache_ascent(int p_cache_index, int p_size, real_t p_ascent);
  248. virtual real_t get_cache_ascent(int p_cache_index, int p_size) const;
  249. virtual void set_cache_descent(int p_cache_index, int p_size, real_t p_descent);
  250. virtual real_t get_cache_descent(int p_cache_index, int p_size) const;
  251. virtual void set_cache_underline_position(int p_cache_index, int p_size, real_t p_underline_position);
  252. virtual real_t get_cache_underline_position(int p_cache_index, int p_size) const;
  253. virtual void set_cache_underline_thickness(int p_cache_index, int p_size, real_t p_underline_thickness);
  254. virtual real_t get_cache_underline_thickness(int p_cache_index, int p_size) const;
  255. virtual void set_cache_scale(int p_cache_index, int p_size, real_t p_scale); // Rendering scale for bitmap fonts (e.g. emoji fonts).
  256. virtual real_t get_cache_scale(int p_cache_index, int p_size) const;
  257. virtual int get_texture_count(int p_cache_index, const Vector2i &p_size) const;
  258. virtual void clear_textures(int p_cache_index, const Vector2i &p_size);
  259. virtual void remove_texture(int p_cache_index, const Vector2i &p_size, int p_texture_index);
  260. virtual void set_texture_image(int p_cache_index, const Vector2i &p_size, int p_texture_index, const Ref<Image> &p_image);
  261. virtual Ref<Image> get_texture_image(int p_cache_index, const Vector2i &p_size, int p_texture_index) const;
  262. virtual void set_texture_offsets(int p_cache_index, const Vector2i &p_size, int p_texture_index, const PackedInt32Array &p_offset);
  263. virtual PackedInt32Array get_texture_offsets(int p_cache_index, const Vector2i &p_size, int p_texture_index) const;
  264. virtual PackedInt32Array get_glyph_list(int p_cache_index, const Vector2i &p_size) const;
  265. virtual void clear_glyphs(int p_cache_index, const Vector2i &p_size);
  266. virtual void remove_glyph(int p_cache_index, const Vector2i &p_size, int32_t p_glyph);
  267. virtual void set_glyph_advance(int p_cache_index, int p_size, int32_t p_glyph, const Vector2 &p_advance);
  268. virtual Vector2 get_glyph_advance(int p_cache_index, int p_size, int32_t p_glyph) const;
  269. virtual void set_glyph_offset(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_offset);
  270. virtual Vector2 get_glyph_offset(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const;
  271. virtual void set_glyph_size(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_gl_size);
  272. virtual Vector2 get_glyph_size(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const;
  273. virtual void set_glyph_uv_rect(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, const Rect2 &p_uv_rect);
  274. virtual Rect2 get_glyph_uv_rect(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const;
  275. virtual void set_glyph_texture_idx(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, int p_texture_idx);
  276. virtual int get_glyph_texture_idx(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const;
  277. virtual TypedArray<Vector2i> get_kerning_list(int p_cache_index, int p_size) const;
  278. virtual void clear_kerning_map(int p_cache_index, int p_size);
  279. virtual void remove_kerning(int p_cache_index, int p_size, const Vector2i &p_glyph_pair);
  280. virtual void set_kerning(int p_cache_index, int p_size, const Vector2i &p_glyph_pair, const Vector2 &p_kerning);
  281. virtual Vector2 get_kerning(int p_cache_index, int p_size, const Vector2i &p_glyph_pair) const;
  282. virtual void render_range(int p_cache_index, const Vector2i &p_size, char32_t p_start, char32_t p_end);
  283. virtual void render_glyph(int p_cache_index, const Vector2i &p_size, int32_t p_index);
  284. // Language/script support override.
  285. virtual void set_language_support_override(const String &p_language, bool p_supported);
  286. virtual bool get_language_support_override(const String &p_language) const;
  287. virtual void remove_language_support_override(const String &p_language);
  288. virtual Vector<String> get_language_support_overrides() const;
  289. virtual void set_script_support_override(const String &p_script, bool p_supported);
  290. virtual bool get_script_support_override(const String &p_script) const;
  291. virtual void remove_script_support_override(const String &p_script);
  292. virtual Vector<String> get_script_support_overrides() const;
  293. virtual void set_opentype_feature_overrides(const Dictionary &p_overrides);
  294. virtual Dictionary get_opentype_feature_overrides() const;
  295. // Base font properties.
  296. virtual int32_t get_glyph_index(int p_size, char32_t p_char, char32_t p_variation_selector = 0x0000) const;
  297. virtual char32_t get_char_from_glyph_index(int p_size, int32_t p_glyph_index) const;
  298. FontFile();
  299. ~FontFile();
  300. };
  301. /*************************************************************************/
  302. /* FontVariation */
  303. /*************************************************************************/
  304. class FontVariation : public Font {
  305. GDCLASS(FontVariation, Font);
  306. struct Variation {
  307. Dictionary opentype;
  308. real_t embolden = 0.f;
  309. int face_index = 0;
  310. Transform2D transform;
  311. };
  312. mutable Ref<Font> theme_font;
  313. Ref<Font> base_font;
  314. Variation variation;
  315. Dictionary opentype_features;
  316. int extra_spacing[TextServer::SPACING_MAX];
  317. float baseline_offset = 0.0;
  318. protected:
  319. static void _bind_methods();
  320. virtual void _update_rids() const override;
  321. virtual void reset_state() override;
  322. public:
  323. virtual void set_base_font(const Ref<Font> &p_font);
  324. virtual Ref<Font> get_base_font() const;
  325. virtual Ref<Font> _get_base_font_or_default() const;
  326. virtual void set_variation_opentype(const Dictionary &p_coords);
  327. virtual Dictionary get_variation_opentype() const;
  328. virtual void set_variation_embolden(float p_strength);
  329. virtual float get_variation_embolden() const;
  330. virtual void set_variation_transform(Transform2D p_transform);
  331. virtual Transform2D get_variation_transform() const;
  332. virtual void set_variation_face_index(int p_face_index);
  333. virtual int get_variation_face_index() const;
  334. virtual void set_opentype_features(const Dictionary &p_features);
  335. virtual Dictionary get_opentype_features() const override;
  336. virtual void set_spacing(TextServer::SpacingType p_spacing, int p_value);
  337. virtual int get_spacing(TextServer::SpacingType p_spacing) const override;
  338. virtual float get_baseline_offset() const;
  339. virtual void set_baseline_offset(float p_baseline_offset);
  340. // Output.
  341. virtual RID find_variation(const Dictionary &p_variation_coordinates, int p_face_index = 0, float p_strength = 0.0, Transform2D p_transform = Transform2D(), int p_spacing_top = 0, int p_spacing_bottom = 0, int p_spacing_space = 0, int p_spacing_glyph = 0, float p_baseline_offset = 0.0) const override;
  342. virtual RID _get_rid() const override;
  343. FontVariation();
  344. ~FontVariation();
  345. };
  346. /*************************************************************************/
  347. /* SystemFont */
  348. /*************************************************************************/
  349. class SystemFont : public Font {
  350. GDCLASS(SystemFont, Font);
  351. PackedStringArray names;
  352. bool italic = false;
  353. int weight = 400;
  354. int stretch = 100;
  355. mutable Ref<Font> theme_font;
  356. Ref<FontFile> base_font;
  357. Vector<int> face_indeces;
  358. int ftr_weight = 0;
  359. int ftr_stretch = 0;
  360. int ftr_italic = 0;
  361. TextServer::FontAntialiasing antialiasing = TextServer::FONT_ANTIALIASING_GRAY;
  362. bool mipmaps = false;
  363. bool disable_embedded_bitmaps = true;
  364. bool force_autohinter = false;
  365. bool allow_system_fallback = true;
  366. TextServer::Hinting hinting = TextServer::HINTING_LIGHT;
  367. TextServer::SubpixelPositioning subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_AUTO;
  368. real_t oversampling = 0.f;
  369. bool msdf = false;
  370. int msdf_pixel_range = 16;
  371. int msdf_size = 48;
  372. protected:
  373. static void _bind_methods();
  374. virtual void _update_base_font();
  375. virtual void _update_rids() const override;
  376. virtual void reset_state() override;
  377. public:
  378. virtual Ref<Font> get_base_font() const { return base_font; }
  379. virtual Ref<Font> _get_base_font_or_default() const;
  380. virtual void set_antialiasing(TextServer::FontAntialiasing p_antialiasing);
  381. virtual TextServer::FontAntialiasing get_antialiasing() const;
  382. virtual void set_disable_embedded_bitmaps(bool p_disable_embedded_bitmaps);
  383. virtual bool get_disable_embedded_bitmaps() const;
  384. virtual void set_generate_mipmaps(bool p_generate_mipmaps);
  385. virtual bool get_generate_mipmaps() const;
  386. virtual void set_allow_system_fallback(bool p_allow_system_fallback);
  387. virtual bool is_allow_system_fallback() const;
  388. virtual void set_force_autohinter(bool p_force_autohinter);
  389. virtual bool is_force_autohinter() const;
  390. virtual void set_hinting(TextServer::Hinting p_hinting);
  391. virtual TextServer::Hinting get_hinting() const;
  392. virtual void set_subpixel_positioning(TextServer::SubpixelPositioning p_subpixel);
  393. virtual TextServer::SubpixelPositioning get_subpixel_positioning() const;
  394. virtual void set_oversampling(real_t p_oversampling);
  395. virtual real_t get_oversampling() const;
  396. virtual void set_multichannel_signed_distance_field(bool p_msdf);
  397. virtual bool is_multichannel_signed_distance_field() const;
  398. virtual void set_msdf_pixel_range(int p_msdf_pixel_range);
  399. virtual int get_msdf_pixel_range() const;
  400. virtual void set_msdf_size(int p_msdf_size);
  401. virtual int get_msdf_size() const;
  402. virtual void set_font_names(const PackedStringArray &p_names);
  403. virtual PackedStringArray get_font_names() const;
  404. virtual void set_font_italic(bool p_italic);
  405. virtual bool get_font_italic() const;
  406. virtual void set_font_weight(int p_weight);
  407. virtual int get_font_weight() const override;
  408. virtual void set_font_stretch(int p_stretch);
  409. virtual int get_font_stretch() const override;
  410. virtual int get_spacing(TextServer::SpacingType p_spacing) const override;
  411. virtual RID find_variation(const Dictionary &p_variation_coordinates, int p_face_index = 0, float p_strength = 0.0, Transform2D p_transform = Transform2D(), int p_spacing_top = 0, int p_spacing_bottom = 0, int p_spacing_space = 0, int p_spacing_glyph = 0, float p_baseline_offset = 0.0) const override;
  412. virtual RID _get_rid() const override;
  413. int64_t get_face_count() const override;
  414. SystemFont();
  415. ~SystemFont();
  416. };
  417. #endif // FONT_H