dynamic_font_adv.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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-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_ADV_H
  31. #define DYNAMIC_FONT_ADV_H
  32. #include "font_adv.h"
  33. #include <ft2build.h>
  34. #include FT_FREETYPE_H
  35. #include FT_TRUETYPE_TABLES_H
  36. #include <hb-ft.h>
  37. #include <hb-ot.h>
  38. struct DynamicFontDataAdvanced : public FontDataAdvanced {
  39. _THREAD_SAFE_CLASS_
  40. private:
  41. struct CharTexture {
  42. Vector<uint8_t> imgdata;
  43. int texture_size = 0;
  44. Vector<int> offsets;
  45. Ref<ImageTexture> texture;
  46. };
  47. struct Character {
  48. bool found = false;
  49. int texture_idx = 0;
  50. Rect2 rect;
  51. Rect2 rect_uv;
  52. Vector2 align;
  53. Vector2 advance = Vector2(-1, -1);
  54. static Character not_found();
  55. };
  56. struct TexturePosition {
  57. int index = 0;
  58. int x = 0;
  59. int y = 0;
  60. };
  61. struct CacheID {
  62. union {
  63. struct {
  64. uint32_t size : 16;
  65. uint32_t outline_size : 16;
  66. };
  67. uint32_t key;
  68. };
  69. bool operator<(CacheID right) const {
  70. return key < right.key;
  71. }
  72. CacheID() {
  73. key = 0;
  74. }
  75. };
  76. struct DataAtSize {
  77. FT_Face face = nullptr;
  78. TT_OS2 *os2 = nullptr;
  79. FT_StreamRec stream;
  80. int size = 0;
  81. float scale_color_font = 1.f;
  82. float ascent = 0;
  83. float descent = 0;
  84. float underline_position = 0;
  85. float underline_thickness = 0;
  86. Vector<CharTexture> textures;
  87. HashMap<uint32_t, Character> glyph_map;
  88. hb_font_t *hb_handle = nullptr;
  89. ~DataAtSize() {
  90. if (hb_handle != nullptr) {
  91. hb_font_destroy(hb_handle);
  92. }
  93. if (face != nullptr) {
  94. FT_Done_Face(face);
  95. }
  96. }
  97. };
  98. FT_Library library = nullptr;
  99. // Source data.
  100. const uint8_t *font_mem = nullptr;
  101. int font_mem_size = 0;
  102. String font_path;
  103. Vector<uint8_t> font_mem_cache;
  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 float get_underline_position(int p_size) const override;
  126. virtual float get_underline_thickness(int p_size) const override;
  127. virtual void set_antialiased(bool p_antialiased) override;
  128. virtual bool get_antialiased() const override;
  129. virtual void set_hinting(TextServer::Hinting p_hinting) override;
  130. virtual TextServer::Hinting get_hinting() const override;
  131. virtual void set_force_autohinter(bool p_enabled) override;
  132. virtual bool get_force_autohinter() const override;
  133. virtual void set_distance_field_hint(bool p_distance_field) override{};
  134. virtual bool get_distance_field_hint() const override { return false; };
  135. virtual bool has_outline() const override;
  136. virtual float get_base_size() const override;
  137. virtual bool is_script_supported(uint32_t p_script) const override;
  138. virtual bool has_char(char32_t p_char) const override;
  139. virtual String get_supported_chars() const override;
  140. virtual float get_font_scale(int p_size) const override;
  141. virtual hb_font_t *get_hb_handle(int p_size) override;
  142. virtual uint32_t get_glyph_index(char32_t p_char, char32_t p_variation_selector) const override;
  143. virtual Vector2 get_advance(uint32_t p_index, int p_size) const override;
  144. virtual Vector2 get_kerning(uint32_t p_char, uint32_t p_next, int p_size) const override;
  145. virtual Vector2 draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const override;
  146. 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;
  147. virtual ~DynamicFontDataAdvanced() override;
  148. };
  149. #endif // DYNAMIC_FONT_ADV_H