font.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*************************************************************************/
  2. /* font.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 FONT_H
  31. #define FONT_H
  32. #include "core/io/resource.h"
  33. #include "core/templates/lru.h"
  34. #include "core/templates/map.h"
  35. #include "scene/resources/texture.h"
  36. #include "servers/text_server.h"
  37. /*************************************************************************/
  38. class FontData : public Resource {
  39. GDCLASS(FontData, Resource);
  40. RID rid;
  41. int base_size = 16;
  42. String path;
  43. protected:
  44. static void _bind_methods();
  45. bool _set(const StringName &p_name, const Variant &p_value);
  46. bool _get(const StringName &p_name, Variant &r_ret) const;
  47. void _get_property_list(List<PropertyInfo> *p_list) const;
  48. virtual void reset_state() override;
  49. public:
  50. virtual RID get_rid() const override;
  51. void load_resource(const String &p_filename, int p_base_size = 16);
  52. void load_memory(const uint8_t *p_data, size_t p_size, const String &p_type, int p_base_size = 16);
  53. void _load_memory(const PackedByteArray &p_data, const String &p_type, int p_base_size = 16);
  54. void set_data_path(const String &p_path);
  55. String get_data_path() const;
  56. float get_height(int p_size) const;
  57. float get_ascent(int p_size) const;
  58. float get_descent(int p_size) const;
  59. Dictionary get_feature_list() const;
  60. Dictionary get_variation_list() const;
  61. void set_variation(const String &p_name, double p_value);
  62. double get_variation(const String &p_name) const;
  63. float get_underline_position(int p_size) const;
  64. float get_underline_thickness(int p_size) const;
  65. void set_antialiased(bool p_antialiased);
  66. bool get_antialiased() const;
  67. void set_distance_field_hint(bool p_distance_field);
  68. bool get_distance_field_hint() const;
  69. void set_force_autohinter(bool p_enabeld);
  70. bool get_force_autohinter() const;
  71. void set_hinting(TextServer::Hinting p_hinting);
  72. TextServer::Hinting get_hinting() const;
  73. bool has_char(char32_t p_char) const;
  74. String get_supported_chars() const;
  75. Vector2 get_glyph_advance(uint32_t p_index, int p_size) const;
  76. Vector2 get_glyph_kerning(uint32_t p_index_a, uint32_t p_index_b, int p_size) const;
  77. bool has_outline() const;
  78. float get_base_size() const;
  79. bool is_language_supported(const String &p_language) const;
  80. void set_language_support_override(const String &p_language, bool p_supported);
  81. bool get_language_support_override(const String &p_language) const;
  82. void remove_language_support_override(const String &p_language);
  83. Vector<String> get_language_support_overrides() const;
  84. bool is_script_supported(const String &p_script) const;
  85. void set_script_support_override(const String &p_script, bool p_supported);
  86. bool get_script_support_override(const String &p_script) const;
  87. void remove_script_support_override(const String &p_script);
  88. Vector<String> get_script_support_overrides() const;
  89. uint32_t get_glyph_index(char32_t p_char, char32_t p_variation_selector = 0x0000) const;
  90. Vector2 draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color = Color(1, 1, 1)) const;
  91. 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 = Color(1, 1, 1)) const;
  92. FontData();
  93. FontData(const String &p_filename, int p_base_size);
  94. FontData(const PackedByteArray &p_data, const String &p_type, int p_base_size);
  95. ~FontData();
  96. };
  97. /*************************************************************************/
  98. class TextLine;
  99. class TextParagraph;
  100. class Font : public Resource {
  101. GDCLASS(Font, Resource);
  102. public:
  103. enum SpacingType {
  104. SPACING_TOP,
  105. SPACING_BOTTOM
  106. };
  107. private:
  108. int spacing_top = 0;
  109. int spacing_bottom = 0;
  110. mutable LRUCache<uint64_t, Ref<TextLine>> cache;
  111. mutable LRUCache<uint64_t, Ref<TextParagraph>> cache_wrap;
  112. Vector<Ref<FontData>> data;
  113. protected:
  114. static void _bind_methods();
  115. bool _set(const StringName &p_name, const Variant &p_value);
  116. bool _get(const StringName &p_name, Variant &r_ret) const;
  117. void _get_property_list(List<PropertyInfo> *p_list) const;
  118. virtual void reset_state() override;
  119. void _data_changed();
  120. public:
  121. Dictionary get_feature_list() const;
  122. // Font data control.
  123. void add_data(const Ref<FontData> &p_data);
  124. void set_data(int p_idx, const Ref<FontData> &p_data);
  125. int get_data_count() const;
  126. Ref<FontData> get_data(int p_idx) const;
  127. void remove_data(int p_idx);
  128. float get_height(int p_size = -1) const;
  129. float get_ascent(int p_size = -1) const;
  130. float get_descent(int p_size = -1) const;
  131. float get_underline_position(int p_size = -1) const;
  132. float get_underline_thickness(int p_size = -1) const;
  133. int get_spacing(int p_type) const;
  134. void set_spacing(int p_type, int p_value);
  135. // Drawing string.
  136. Size2 get_string_size(const String &p_text, int p_size = -1) const;
  137. Size2 get_multiline_string_size(const String &p_text, float p_width = -1, int p_size = -1, uint8_t p_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND) const;
  138. void draw_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HAlign p_align = HALIGN_LEFT, float p_width = -1, int p_size = -1, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0), uint8_t p_flags = TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND) const;
  139. void draw_multiline_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HAlign p_align = HALIGN_LEFT, float p_width = -1, int p_max_lines = -1, int p_size = -1, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0), uint8_t p_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND) const;
  140. // Helper functions.
  141. bool has_char(char32_t p_char) const;
  142. String get_supported_chars() const;
  143. Size2 get_char_size(char32_t p_char, char32_t p_next = 0, int p_size = -1) const;
  144. float draw_char(RID p_canvas_item, const Point2 &p_pos, char32_t p_char, char32_t p_next = 0, int p_size = -1, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0)) const;
  145. Vector<RID> get_rids() const;
  146. void update_changes();
  147. Font();
  148. ~Font();
  149. };
  150. VARIANT_ENUM_CAST(Font::SpacingType);
  151. /*************************************************************************/
  152. class ResourceFormatLoaderFont : public ResourceFormatLoader {
  153. public:
  154. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE);
  155. virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
  156. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  157. virtual bool handles_type(const String &p_type) const;
  158. virtual String get_resource_type(const String &p_path) const;
  159. };
  160. #ifndef DISABLE_DEPRECATED
  161. class ResourceFormatLoaderCompatFont : public ResourceFormatLoader {
  162. public:
  163. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE);
  164. virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
  165. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  166. virtual bool handles_type(const String &p_type) const;
  167. virtual String get_resource_type(const String &p_path) const;
  168. };
  169. #endif /* DISABLE_DEPRECATED */
  170. #endif /* FONT_H */