dynamic_font_adv.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*************************************************************************/
  2. /* dynamic_font_adv.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_ADV_H
  31. #define DYNAMIC_FONT_ADV_H
  32. #include "font_adv.h"
  33. #include "modules/modules_enabled.gen.h"
  34. #ifdef MODULE_FREETYPE_ENABLED
  35. #include <ft2build.h>
  36. #include FT_FREETYPE_H
  37. #include FT_TRUETYPE_TABLES_H
  38. #include <hb-ft.h>
  39. #include <hb-ot.h>
  40. struct DynamicFontDataAdvanced : public FontDataAdvanced {
  41. _THREAD_SAFE_CLASS_
  42. private:
  43. struct CharTexture {
  44. Vector<uint8_t> imgdata;
  45. int texture_size = 0;
  46. Vector<int> offsets;
  47. Ref<ImageTexture> texture;
  48. };
  49. struct Character {
  50. bool found = false;
  51. int texture_idx = 0;
  52. Rect2 rect;
  53. Rect2 rect_uv;
  54. Vector2 align;
  55. Vector2 advance = Vector2(-1, -1);
  56. static Character not_found();
  57. };
  58. struct TexturePosition {
  59. int index = 0;
  60. int x = 0;
  61. int y = 0;
  62. };
  63. struct CacheID {
  64. union {
  65. struct {
  66. uint32_t size : 16;
  67. uint32_t outline_size : 16;
  68. };
  69. uint32_t key = 0;
  70. };
  71. bool operator<(CacheID right) const {
  72. return key < right.key;
  73. }
  74. };
  75. struct DataAtSize {
  76. FT_Face face = nullptr;
  77. TT_OS2 *os2 = nullptr;
  78. FT_StreamRec stream;
  79. int size = 0;
  80. float scale_color_font = 1.f;
  81. float ascent = 0.0;
  82. float descent = 0.0;
  83. float underline_position = 0.0;
  84. float underline_thickness = 0.0;
  85. Vector<CharTexture> textures;
  86. HashMap<uint32_t, Character> glyph_map;
  87. hb_font_t *hb_handle = nullptr;
  88. ~DataAtSize() {
  89. if (hb_handle != nullptr) {
  90. hb_font_destroy(hb_handle);
  91. }
  92. if (face != nullptr) {
  93. FT_Done_Face(face);
  94. }
  95. }
  96. };
  97. FT_Library library = nullptr;
  98. // Source data.
  99. const uint8_t *font_mem = nullptr;
  100. int font_mem_size = 0;
  101. String font_path;
  102. Vector<uint8_t> font_mem_cache;
  103. Map<int32_t, double> variations;
  104. float rect_margin = 1.f;
  105. int base_size = 16;
  106. float oversampling = 1.f;
  107. bool antialiased = true;
  108. bool force_autohinter = false;
  109. TextServer::Hinting hinting = TextServer::HINTING_LIGHT;
  110. Map<CacheID, DataAtSize *> size_cache;
  111. Map<CacheID, DataAtSize *> size_cache_outline;
  112. DataAtSize *get_data_for_size(int p_size, int p_outline_size = 0);
  113. TexturePosition find_texture_pos_for_glyph(DataAtSize *p_data, int p_color_size, Image::Format p_image_format, int p_width, int p_height);
  114. Character bitmap_to_character(DataAtSize *p_data, FT_Bitmap bitmap, int yofs, int xofs, const Vector2 &advance);
  115. _FORCE_INLINE_ void update_glyph(int p_size, uint32_t p_index);
  116. _FORCE_INLINE_ void update_glyph_outline(int p_size, int p_outline_size, uint32_t p_index);
  117. public:
  118. virtual void clear_cache() override;
  119. virtual Error load_from_file(const String &p_filename, int p_base_size) override;
  120. virtual Error load_from_memory(const uint8_t *p_data, size_t p_size, int p_base_size) override;
  121. virtual float get_height(int p_size) const override;
  122. virtual float get_ascent(int p_size) const override;
  123. virtual float get_descent(int p_size) const override;
  124. virtual Dictionary get_feature_list() const override;
  125. virtual Dictionary get_variation_list() const override;
  126. virtual void set_variation(const String &p_name, double p_value) override;
  127. virtual double get_variation(const String &p_name) const override;
  128. virtual float get_underline_position(int p_size) const override;
  129. virtual float get_underline_thickness(int p_size) const override;
  130. virtual void set_antialiased(bool p_antialiased) override;
  131. virtual bool get_antialiased() const override;
  132. virtual void set_hinting(TextServer::Hinting p_hinting) override;
  133. virtual TextServer::Hinting get_hinting() const override;
  134. virtual void set_force_autohinter(bool p_enabled) override;
  135. virtual bool get_force_autohinter() const override;
  136. virtual void set_distance_field_hint(bool p_distance_field) override{};
  137. virtual bool get_distance_field_hint() const override { return false; };
  138. virtual bool has_outline() const override;
  139. virtual float get_base_size() const override;
  140. virtual bool is_script_supported(uint32_t p_script) const override;
  141. virtual bool has_char(char32_t p_char) const override;
  142. virtual String get_supported_chars() const override;
  143. virtual float get_font_scale(int p_size) const override;
  144. virtual hb_font_t *get_hb_handle(int p_size) override;
  145. virtual uint32_t get_glyph_index(char32_t p_char, char32_t p_variation_selector) const override;
  146. virtual Vector2 get_advance(uint32_t p_index, int p_size) const override;
  147. virtual Vector2 get_kerning(uint32_t p_char, uint32_t p_next, int p_size) const override;
  148. virtual Vector2 draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const override;
  149. virtual Vector2 draw_glyph_outline(RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const override;
  150. virtual ~DynamicFontDataAdvanced() override;
  151. };
  152. #endif // MODULE_FREETYPE_ENABLED
  153. #endif // DYNAMIC_FONT_ADV_H