dynamic_font.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /**************************************************************************/
  2. /* dynamic_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 DYNAMIC_FONT_H
  31. #define DYNAMIC_FONT_H
  32. #include "modules/modules_enabled.gen.h" // For freetype.
  33. #ifdef MODULE_FREETYPE_ENABLED
  34. #include "core/io/resource_loader.h"
  35. #include "core/os/mutex.h"
  36. #include "core/os/thread_safe.h"
  37. #include "core/pair.h"
  38. #include "scene/resources/font.h"
  39. #include <ft2build.h>
  40. #include FT_FREETYPE_H
  41. class DynamicFontAtSize;
  42. class DynamicFont;
  43. class DynamicFontData : public Resource {
  44. GDCLASS(DynamicFontData, Resource);
  45. public:
  46. struct CacheID {
  47. union {
  48. struct {
  49. uint32_t size : 16;
  50. uint32_t outline_size : 8;
  51. uint32_t mipmaps : 1;
  52. uint32_t filter : 1;
  53. uint32_t unused : 6;
  54. };
  55. uint32_t key;
  56. };
  57. bool operator<(CacheID right) const;
  58. CacheID() {
  59. key = 0;
  60. }
  61. };
  62. enum Hinting {
  63. HINTING_NONE,
  64. HINTING_LIGHT,
  65. HINTING_NORMAL
  66. };
  67. bool is_antialiased() const;
  68. void set_antialiased(bool p_antialiased);
  69. Hinting get_hinting() const;
  70. void set_hinting(Hinting p_hinting);
  71. private:
  72. const uint8_t *font_mem;
  73. int font_mem_size;
  74. bool antialiased;
  75. bool force_autohinter;
  76. Hinting hinting;
  77. Vector<uint8_t> _fontdata;
  78. float override_oversampling;
  79. String font_path;
  80. Map<CacheID, DynamicFontAtSize *> size_cache;
  81. friend class DynamicFontAtSize;
  82. friend class DynamicFont;
  83. Ref<DynamicFontAtSize> _get_dynamic_font_at_size(CacheID p_cache_id);
  84. protected:
  85. static void _bind_methods();
  86. public:
  87. void set_font_ptr(const uint8_t *p_font_mem, int p_font_mem_size);
  88. void set_font_path(const String &p_path);
  89. String get_font_path() const;
  90. void set_force_autohinter(bool p_force);
  91. float get_override_oversampling() const;
  92. void set_override_oversampling(float p_oversampling);
  93. DynamicFontData();
  94. ~DynamicFontData();
  95. };
  96. VARIANT_ENUM_CAST(DynamicFontData::Hinting);
  97. class DynamicFontAtSize : public Reference {
  98. GDCLASS(DynamicFontAtSize, Reference);
  99. _THREAD_SAFE_CLASS_
  100. FT_Library library; /* handle to library */
  101. FT_Face face; /* handle to face object */
  102. FT_StreamRec stream;
  103. float ascent;
  104. float descent;
  105. float linegap;
  106. float rect_margin;
  107. float oversampling;
  108. float scale_color_font;
  109. uint32_t texture_flags;
  110. bool valid;
  111. struct CharTexture {
  112. PoolVector<uint8_t> imgdata;
  113. int texture_size;
  114. Vector<int> offsets;
  115. Ref<ImageTexture> texture;
  116. };
  117. Vector<CharTexture> textures;
  118. struct Character {
  119. bool found;
  120. int texture_idx;
  121. Rect2 rect;
  122. Rect2 rect_uv;
  123. float v_align;
  124. float h_align;
  125. float advance;
  126. Character() {
  127. texture_idx = 0;
  128. v_align = 0;
  129. }
  130. static Character not_found();
  131. };
  132. struct TexturePosition {
  133. int index;
  134. int x;
  135. int y;
  136. };
  137. const Pair<const Character *, DynamicFontAtSize *> _find_char_with_font(int32_t p_char, const Vector<Ref<DynamicFontAtSize>> &p_fallbacks) const;
  138. Character _make_outline_char(int32_t p_char);
  139. float _get_kerning_advance(const DynamicFontAtSize *font, int32_t p_char, int32_t p_next) const;
  140. TexturePosition _find_texture_pos_for_glyph(int p_color_size, Image::Format p_image_format, int p_width, int p_height);
  141. Character _bitmap_to_character(FT_Bitmap bitmap, int yofs, int xofs, float advance);
  142. HashMap<int32_t, Character> char_map;
  143. _FORCE_INLINE_ void _update_char(int32_t p_char);
  144. friend class DynamicFontData;
  145. Ref<DynamicFontData> font;
  146. DynamicFontData::CacheID id;
  147. Error _load();
  148. public:
  149. static float font_oversampling;
  150. float get_height() const;
  151. float get_ascent() const;
  152. float get_descent() const;
  153. Size2 get_char_size(CharType p_char, CharType p_next, const Vector<Ref<DynamicFontAtSize>> &p_fallbacks) const;
  154. String get_available_chars() const;
  155. float draw_char(RID p_canvas_item, const Point2 &p_pos, CharType p_char, CharType p_next, const Color &p_modulate, const Vector<Ref<DynamicFontAtSize>> &p_fallbacks, bool p_advance_only = false, bool p_outline = false) const;
  156. RID get_char_texture(CharType p_char, CharType p_next, const Vector<Ref<DynamicFontAtSize>> &p_fallbacks) const;
  157. Size2 get_char_texture_size(CharType p_char, CharType p_next, const Vector<Ref<DynamicFontAtSize>> &p_fallbacks) const;
  158. Vector2 get_char_tx_offset(CharType p_char, CharType p_next, const Vector<Ref<DynamicFontAtSize>> &p_fallbacks) const;
  159. Size2 get_char_tx_size(CharType p_char, CharType p_next, const Vector<Ref<DynamicFontAtSize>> &p_fallbacks) const;
  160. Rect2 get_char_tx_uv_rect(CharType p_char, CharType p_next, const Vector<Ref<DynamicFontAtSize>> &p_fallbacks) const;
  161. void set_texture_flags(uint32_t p_flags);
  162. void update_oversampling();
  163. Dictionary get_char_contours(CharType p_char, CharType p_next, const Vector<Ref<DynamicFontAtSize>> &p_fallbacks) const;
  164. DynamicFontAtSize();
  165. ~DynamicFontAtSize();
  166. };
  167. ///////////////
  168. class DynamicFont : public Font {
  169. GDCLASS(DynamicFont, Font);
  170. public:
  171. enum SpacingType {
  172. SPACING_TOP,
  173. SPACING_BOTTOM,
  174. SPACING_CHAR,
  175. SPACING_SPACE
  176. };
  177. private:
  178. Ref<DynamicFontData> data;
  179. Ref<DynamicFontAtSize> data_at_size;
  180. Ref<DynamicFontAtSize> outline_data_at_size;
  181. Vector<Ref<DynamicFontData>> fallbacks;
  182. Vector<Ref<DynamicFontAtSize>> fallback_data_at_size;
  183. Vector<Ref<DynamicFontAtSize>> fallback_outline_data_at_size;
  184. DynamicFontData::CacheID cache_id;
  185. DynamicFontData::CacheID outline_cache_id;
  186. bool valid;
  187. int spacing_top;
  188. int spacing_bottom;
  189. int spacing_char;
  190. int spacing_space;
  191. Color outline_color;
  192. protected:
  193. void _reload_cache(const char *p_triggering_property = "");
  194. bool _set(const StringName &p_name, const Variant &p_value);
  195. bool _get(const StringName &p_name, Variant &r_ret) const;
  196. void _get_property_list(List<PropertyInfo> *p_list) const;
  197. static void _bind_methods();
  198. public:
  199. void set_font_data(const Ref<DynamicFontData> &p_data);
  200. Ref<DynamicFontData> get_font_data() const;
  201. void set_size(int p_size);
  202. int get_size() const;
  203. void set_outline_size(int p_size);
  204. int get_outline_size() const;
  205. void set_outline_color(Color p_color);
  206. Color get_outline_color() const;
  207. bool get_use_mipmaps() const;
  208. void set_use_mipmaps(bool p_enable);
  209. bool get_use_filter() const;
  210. void set_use_filter(bool p_enable);
  211. int get_spacing(int p_type) const;
  212. void set_spacing(int p_type, int p_value);
  213. void add_fallback(const Ref<DynamicFontData> &p_data);
  214. void set_fallback(int p_idx, const Ref<DynamicFontData> &p_data);
  215. int get_fallback_count() const;
  216. Ref<DynamicFontData> get_fallback(int p_idx) const;
  217. void remove_fallback(int p_idx);
  218. virtual float get_height() const;
  219. virtual float get_ascent() const;
  220. virtual float get_descent() const;
  221. virtual Size2 get_char_size(CharType p_char, CharType p_next = 0) const;
  222. String get_available_chars() const;
  223. virtual bool is_distance_field_hint() const;
  224. virtual bool has_outline() const;
  225. virtual float draw_char(RID p_canvas_item, const Point2 &p_pos, CharType p_char, CharType p_next = 0, const Color &p_modulate = Color(1, 1, 1), bool p_outline = false) const;
  226. RID get_char_texture(CharType p_char, CharType p_next, bool p_outline) const;
  227. Size2 get_char_texture_size(CharType p_char, CharType p_next, bool p_outline) const;
  228. Vector2 get_char_tx_offset(CharType p_char, CharType p_next, bool p_outline) const;
  229. Size2 get_char_tx_size(CharType p_char, CharType p_next, bool p_outline) const;
  230. Rect2 get_char_tx_uv_rect(CharType p_char, CharType p_next, bool p_outline) const;
  231. Dictionary get_char_contours(CharType p_char, CharType p_next) const;
  232. SelfList<DynamicFont> font_list;
  233. static Mutex dynamic_font_mutex;
  234. static SelfList<DynamicFont>::List *dynamic_fonts;
  235. static void initialize_dynamic_fonts();
  236. static void finish_dynamic_fonts();
  237. static void update_oversampling();
  238. DynamicFont();
  239. ~DynamicFont();
  240. };
  241. VARIANT_ENUM_CAST(DynamicFont::SpacingType);
  242. /////////////
  243. class ResourceFormatLoaderDynamicFont : public ResourceFormatLoader {
  244. public:
  245. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr);
  246. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  247. virtual bool handles_type(const String &p_type) const;
  248. virtual String get_resource_type(const String &p_path) const;
  249. };
  250. #endif // MODULE_FREETYPE_ENABLED
  251. #endif // DYNAMIC_FONT_H