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