dynamic_font.h 8.7 KB

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