dynamic_font_stb.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*************************************************************************/
  2. /* dynamic_font_stb.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 DYNAMICFONT_STB_H
  31. #define DYNAMICFONT_STB_H
  32. #ifndef FREETYPE_ENABLED
  33. #include "core/io/resource_loader.h"
  34. #include "font.h"
  35. #include "thirdparty/misc/stb_truetype.h"
  36. class DynamicFontAtSize;
  37. class DynamicFont;
  38. class DynamicFontData : public Resource {
  39. GDCLASS(DynamicFontData, Resource);
  40. bool valid;
  41. PoolVector<uint8_t> font_data;
  42. PoolVector<uint8_t>::Read fr;
  43. const uint8_t *last_data_ptr;
  44. struct KerningPairKey {
  45. union {
  46. struct {
  47. uint32_t A, B;
  48. };
  49. uint64_t pair;
  50. };
  51. _FORCE_INLINE_ bool operator<(const KerningPairKey &p_r) const { return pair < p_r.pair; }
  52. };
  53. Map<KerningPairKey, int> kerning_map;
  54. Map<int, DynamicFontAtSize *> size_cache;
  55. friend class DynamicFontAtSize;
  56. stbtt_fontinfo info;
  57. int ascent;
  58. int descent;
  59. int linegap;
  60. void lock();
  61. void unlock();
  62. friend class DynamicFont;
  63. Ref<DynamicFontAtSize> _get_dynamic_font_at_size(int p_size);
  64. public:
  65. void set_font_data(const PoolVector<uint8_t> &p_font);
  66. DynamicFontData();
  67. ~DynamicFontData();
  68. };
  69. class DynamicFontAtSize : public Reference {
  70. GDCLASS(DynamicFontAtSize, Reference);
  71. int rect_margin;
  72. struct CharTexture {
  73. PoolVector<uint8_t> imgdata;
  74. int texture_size;
  75. Vector<int> offsets;
  76. Ref<ImageTexture> texture;
  77. };
  78. Vector<CharTexture> textures;
  79. struct Character {
  80. int texture_idx;
  81. Rect2 rect;
  82. float v_align;
  83. float h_align;
  84. float advance;
  85. Character() {
  86. texture_idx = 0;
  87. v_align = 0;
  88. }
  89. };
  90. HashMap<CharType, Character> char_map;
  91. _FORCE_INLINE_ void _update_char(CharType p_char);
  92. friend class DynamicFontData;
  93. Ref<DynamicFontData> font;
  94. float scale;
  95. int size;
  96. protected:
  97. public:
  98. float get_height() const;
  99. float get_ascent() const;
  100. float get_descent() const;
  101. Size2 get_char_size(CharType p_char, CharType p_next = 0) const;
  102. 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;
  103. DynamicFontAtSize();
  104. ~DynamicFontAtSize();
  105. };
  106. ///////////////
  107. class DynamicFont : public Font {
  108. GDCLASS(DynamicFont, Font);
  109. Ref<DynamicFontData> data;
  110. Ref<DynamicFontAtSize> data_at_size;
  111. int size;
  112. protected:
  113. static void _bind_methods();
  114. public:
  115. void set_font_data(const Ref<DynamicFontData> &p_data);
  116. Ref<DynamicFontData> get_font_data() const;
  117. void set_size(int p_size);
  118. int get_size() const;
  119. virtual float get_height() const;
  120. virtual float get_ascent() const;
  121. virtual float get_descent() const;
  122. virtual Size2 get_char_size(CharType p_char, CharType p_next = 0) const;
  123. virtual bool is_distance_field_hint() const;
  124. 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;
  125. DynamicFont();
  126. ~DynamicFont();
  127. };
  128. /////////////
  129. class ResourceFormatLoaderDynamicFont : public ResourceFormatLoader {
  130. GDCLASS(ResourceFormatLoaderDynamicFont, ResourceFormatLoader)
  131. public:
  132. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  133. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  134. virtual bool handles_type(const String &p_type) const;
  135. virtual String get_resource_type(const String &p_path) const;
  136. };
  137. #endif
  138. #endif // DYNAMICFONT_H