dynamic_font.h 6.9 KB

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