dynamic_font.h 8.7 KB

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