dynamic_font_fb.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*************************************************************************/
  2. /* dynamic_font_fb.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_FALLBACK_H
  31. #define DYNAMIC_FONT_FALLBACK_H
  32. #include "font_fb.h"
  33. #include "modules/modules_enabled.gen.h"
  34. #ifdef MODULE_FREETYPE_ENABLED
  35. #include <ft2build.h>
  36. #include FT_FREETYPE_H
  37. struct DynamicFontDataFallback : public FontDataFallback {
  38. _THREAD_SAFE_CLASS_
  39. private:
  40. struct CharTexture {
  41. Vector<uint8_t> imgdata;
  42. int texture_size = 0;
  43. Vector<int> offsets;
  44. Ref<ImageTexture> texture;
  45. };
  46. struct Character {
  47. bool found = false;
  48. int texture_idx = 0;
  49. Rect2 rect;
  50. Rect2 rect_uv;
  51. Vector2 align;
  52. Vector2 advance = Vector2(-1, -1);
  53. static Character not_found();
  54. };
  55. struct TexturePosition {
  56. int index = 0;
  57. int x = 0;
  58. int y = 0;
  59. };
  60. struct CacheID {
  61. union {
  62. struct {
  63. uint32_t size : 16;
  64. uint32_t outline_size : 16;
  65. };
  66. uint32_t key = 0;
  67. };
  68. bool operator<(CacheID right) const {
  69. return key < right.key;
  70. }
  71. };
  72. struct DataAtSize {
  73. FT_Face face = nullptr;
  74. FT_StreamRec stream;
  75. int size = 0;
  76. float scale_color_font = 1.f;
  77. float ascent = 0.0;
  78. float descent = 0.0;
  79. float underline_position = 0.0;
  80. float underline_thickness = 0.0;
  81. Vector<CharTexture> textures;
  82. HashMap<char32_t, Character> char_map;
  83. ~DataAtSize() {
  84. if (face != nullptr) {
  85. FT_Done_Face(face);
  86. }
  87. }
  88. };
  89. FT_Library library = nullptr;
  90. // Source data.
  91. const uint8_t *font_mem = nullptr;
  92. int font_mem_size = 0;
  93. String font_path;
  94. Vector<uint8_t> font_mem_cache;
  95. float rect_margin = 1.f;
  96. int base_size = 16;
  97. float oversampling = 1.f;
  98. bool antialiased = true;
  99. bool force_autohinter = false;
  100. TextServer::Hinting hinting = TextServer::HINTING_LIGHT;
  101. Map<CacheID, DataAtSize *> size_cache;
  102. Map<CacheID, DataAtSize *> size_cache_outline;
  103. DataAtSize *get_data_for_size(int p_size, int p_outline_size = 0);
  104. TexturePosition find_texture_pos_for_glyph(DataAtSize *p_data, int p_color_size, Image::Format p_image_format, int p_width, int p_height);
  105. Character bitmap_to_character(DataAtSize *p_data, FT_Bitmap bitmap, int yofs, int xofs, const Vector2 &advance);
  106. _FORCE_INLINE_ void update_char(int p_size, char32_t p_char);
  107. _FORCE_INLINE_ void update_char_outline(int p_size, int p_outline_size, char32_t p_char);
  108. public:
  109. virtual void clear_cache() override;
  110. virtual Error load_from_file(const String &p_filename, int p_base_size) override;
  111. virtual Error load_from_memory(const uint8_t *p_data, size_t p_size, int p_base_size) override;
  112. virtual float get_height(int p_size) const override;
  113. virtual float get_ascent(int p_size) const override;
  114. virtual float get_descent(int p_size) const override;
  115. virtual float get_underline_position(int p_size) const override;
  116. virtual float get_underline_thickness(int p_size) const override;
  117. virtual void set_antialiased(bool p_antialiased) override;
  118. virtual bool get_antialiased() const override;
  119. virtual void set_hinting(TextServer::Hinting p_hinting) override;
  120. virtual TextServer::Hinting get_hinting() const override;
  121. virtual void set_force_autohinter(bool p_enabeld) override;
  122. virtual bool get_force_autohinter() const override;
  123. virtual void set_distance_field_hint(bool p_distance_field) override{};
  124. virtual bool get_distance_field_hint() const override { return false; };
  125. virtual bool has_outline() const override;
  126. virtual float get_base_size() const override;
  127. virtual bool has_char(char32_t p_char) const override;
  128. virtual String get_supported_chars() const override;
  129. virtual Vector2 get_advance(char32_t p_char, int p_size) const override;
  130. virtual Vector2 get_kerning(char32_t p_char, char32_t p_next, int p_size) const override;
  131. virtual Vector2 draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color) const override;
  132. 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;
  133. virtual ~DynamicFontDataFallback() override;
  134. };
  135. #endif // MODULE_FREETYPE_ENABLED
  136. #endif // DYNAMIC_FONT_FALLBACK_H