dynamic_font.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 "io/resource_loader.h"
  34. #include "os/thread_safe.h"
  35. #include "scene/resources/font.h"
  36. #include <ft2build.h>
  37. #include FT_FREETYPE_H
  38. class DynamicFontAtSize;
  39. class DynamicFont;
  40. class DynamicFontData : public Resource {
  41. OBJ_TYPE(DynamicFontData, Resource);
  42. public:
  43. struct CacheID {
  44. union {
  45. struct {
  46. uint32_t size : 16;
  47. bool mipmaps : 1;
  48. bool filter : 1;
  49. };
  50. uint32_t key;
  51. };
  52. bool operator<(CacheID right) const;
  53. CacheID() {
  54. key = 0;
  55. }
  56. };
  57. private:
  58. const uint8_t *font_mem;
  59. int font_mem_size;
  60. bool force_autohinter;
  61. String font_path;
  62. Map<CacheID, DynamicFontAtSize *> size_cache;
  63. friend class DynamicFontAtSize;
  64. friend class DynamicFont;
  65. Ref<DynamicFontAtSize> _get_dynamic_font_at_size(CacheID p_cache);
  66. protected:
  67. static void _bind_methods();
  68. public:
  69. void set_font_ptr(const uint8_t *p_font_mem, int p_font_mem_size);
  70. void set_font_path(const String &p_path);
  71. String get_font_path() const;
  72. void set_force_autohinter(bool p_force);
  73. DynamicFontData();
  74. ~DynamicFontData();
  75. };
  76. class DynamicFontAtSize : public Reference {
  77. OBJ_TYPE(DynamicFontAtSize, Reference)
  78. _THREAD_SAFE_CLASS_
  79. FT_Library library; /* handle to library */
  80. FT_Face face; /* handle to face object */
  81. FT_StreamRec stream;
  82. int ascent;
  83. int descent;
  84. int linegap;
  85. int rect_margin;
  86. uint32_t texture_flags;
  87. bool valid;
  88. struct CharTexture {
  89. DVector<uint8_t> imgdata;
  90. int texture_size;
  91. Vector<int> offsets;
  92. Ref<ImageTexture> texture;
  93. };
  94. Vector<CharTexture> textures;
  95. struct Character {
  96. bool found;
  97. int texture_idx;
  98. Rect2 rect;
  99. float v_align;
  100. float h_align;
  101. float advance;
  102. Character() {
  103. texture_idx = 0;
  104. v_align = 0;
  105. }
  106. };
  107. static unsigned long _ft_stream_io(FT_Stream stream, unsigned long offset, unsigned char *buffer, unsigned long count);
  108. static void _ft_stream_close(FT_Stream stream);
  109. HashMap<CharType, Character> char_map;
  110. _FORCE_INLINE_ void _update_char(CharType p_char);
  111. friend class DynamicFontData;
  112. Ref<DynamicFontData> font;
  113. DynamicFontData::CacheID id;
  114. static HashMap<String, Vector<uint8_t> > _fontdata;
  115. Error _load();
  116. protected:
  117. public:
  118. float get_height() const;
  119. float get_ascent() const;
  120. float get_descent() const;
  121. Size2 get_char_size(CharType p_char, CharType p_next, const Vector<Ref<DynamicFontAtSize> > &p_fallbacks) const;
  122. 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) const;
  123. void set_texture_flags(uint32_t p_flags);
  124. DynamicFontAtSize();
  125. ~DynamicFontAtSize();
  126. };
  127. ///////////////
  128. class DynamicFont : public Font {
  129. OBJ_TYPE(DynamicFont, Font);
  130. public:
  131. enum SpacingType {
  132. SPACING_TOP,
  133. SPACING_BOTTOM,
  134. SPACING_CHAR,
  135. SPACING_SPACE
  136. };
  137. private:
  138. Ref<DynamicFontData> data;
  139. Ref<DynamicFontAtSize> data_at_size;
  140. Vector<Ref<DynamicFontData> > fallbacks;
  141. Vector<Ref<DynamicFontAtSize> > fallback_data_at_size;
  142. DynamicFontData::CacheID cache_id;
  143. bool valid;
  144. int spacing_top;
  145. int spacing_bottom;
  146. int spacing_char;
  147. int spacing_space;
  148. protected:
  149. void _reload_cache();
  150. bool _set(const StringName &p_name, const Variant &p_value);
  151. bool _get(const StringName &p_name, Variant &r_ret) const;
  152. void _get_property_list(List<PropertyInfo> *p_list) const;
  153. static void _bind_methods();
  154. public:
  155. void set_font_data(const Ref<DynamicFontData> &p_data);
  156. Ref<DynamicFontData> get_font_data() const;
  157. void set_size(int p_size);
  158. int get_size() const;
  159. bool get_use_mipmaps() const;
  160. void set_use_mipmaps(bool p_enable);
  161. bool get_use_filter() const;
  162. void set_use_filter(bool p_enable);
  163. int get_spacing(int p_type) const;
  164. void set_spacing(int p_type, int p_value);
  165. void add_fallback(const Ref<DynamicFontData> &p_data);
  166. void set_fallback(int p_idx, const Ref<DynamicFontData> &p_data);
  167. int get_fallback_count() const;
  168. Ref<DynamicFontData> get_fallback(int p_idx) const;
  169. void remove_fallback(int p_idx);
  170. virtual float get_height() const;
  171. virtual float get_ascent() const;
  172. virtual float get_descent() const;
  173. virtual Size2 get_char_size(CharType p_char, CharType p_next = 0) const;
  174. virtual bool is_distance_field_hint() const;
  175. 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)) const;
  176. DynamicFont();
  177. ~DynamicFont();
  178. };
  179. /////////////
  180. class ResourceFormatLoaderDynamicFont : public ResourceFormatLoader {
  181. public:
  182. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  183. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  184. virtual bool handles_type(const String &p_type) const;
  185. virtual String get_resource_type(const String &p_path) const;
  186. };
  187. #endif
  188. #endif