font.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**************************************************************************/
  2. /* font.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/map.h"
  33. #include "core/resource.h"
  34. #include "scene/resources/texture.h"
  35. class Font : public Resource {
  36. GDCLASS(Font, Resource);
  37. protected:
  38. static void _bind_methods();
  39. public:
  40. enum ContourPointTag {
  41. CONTOUR_CURVE_TAG_ON = 0x01,
  42. CONTOUR_CURVE_TAG_OFF_CONIC = 0x00,
  43. CONTOUR_CURVE_TAG_OFF_CUBIC = 0x02
  44. };
  45. virtual float get_height() const = 0;
  46. virtual float get_ascent() const = 0;
  47. virtual float get_descent() const = 0;
  48. virtual Size2 get_char_size(CharType p_char, CharType p_next = 0) const = 0;
  49. Size2 get_string_size(const String &p_string) const;
  50. Size2 get_wordwrap_string_size(const String &p_string, float p_width) const;
  51. virtual bool is_distance_field_hint() const = 0;
  52. void draw(RID p_canvas_item, const Point2 &p_pos, const String &p_text, const Color &p_modulate = Color(1, 1, 1), int p_clip_w = -1, const Color &p_outline_modulate = Color(1, 1, 1)) const;
  53. void draw_halign(RID p_canvas_item, const Point2 &p_pos, HAlign p_align, float p_width, const String &p_text, const Color &p_modulate = Color(1, 1, 1), const Color &p_outline_modulate = Color(1, 1, 1)) const;
  54. virtual bool has_outline() const { return false; }
  55. virtual float draw_char(RID p_canvas_item, const Point2 &p_pos, CharType p_char, CharType p_next = 0, const Color &p_modulate = Color(1, 1, 1), bool p_outline = false) const = 0;
  56. virtual RID get_char_texture(CharType p_char, CharType p_next, bool p_outline) const = 0;
  57. virtual Size2 get_char_texture_size(CharType p_char, CharType p_next, bool p_outline) const = 0;
  58. virtual Vector2 get_char_tx_offset(CharType p_char, CharType p_next, bool p_outline) const = 0;
  59. virtual Size2 get_char_tx_size(CharType p_char, CharType p_next, bool p_outline) const = 0;
  60. virtual Rect2 get_char_tx_uv_rect(CharType p_char, CharType p_next, bool p_outline) const = 0;
  61. virtual Dictionary get_char_contours(CharType p_char, CharType p_next = 0) const { return Dictionary(); }
  62. void update_changes();
  63. Font();
  64. };
  65. // Helper class to that draws outlines immediately and draws characters in its destructor.
  66. class FontDrawer {
  67. const Ref<Font> &font;
  68. Color outline_color;
  69. bool has_outline;
  70. struct PendingDraw {
  71. RID canvas_item;
  72. Point2 pos;
  73. CharType chr;
  74. CharType next;
  75. Color modulate;
  76. };
  77. Vector<PendingDraw> pending_draws;
  78. public:
  79. FontDrawer(const Ref<Font> &p_font, const Color &p_outline_color) :
  80. font(p_font),
  81. outline_color(p_outline_color) {
  82. has_outline = p_font->has_outline();
  83. }
  84. float draw_char(RID p_canvas_item, const Point2 &p_pos, CharType p_char, CharType p_next = 0, const Color &p_modulate = Color(1, 1, 1)) {
  85. if (has_outline) {
  86. PendingDraw draw = { p_canvas_item, p_pos, p_char, p_next, p_modulate };
  87. pending_draws.push_back(draw);
  88. }
  89. return font->draw_char(p_canvas_item, p_pos, p_char, p_next, has_outline ? outline_color : p_modulate, has_outline);
  90. }
  91. ~FontDrawer() {
  92. for (int i = 0; i < pending_draws.size(); ++i) {
  93. const PendingDraw &draw = pending_draws[i];
  94. font->draw_char(draw.canvas_item, draw.pos, draw.chr, draw.next, draw.modulate, false);
  95. }
  96. }
  97. };
  98. class BitmapFont : public Font {
  99. GDCLASS(BitmapFont, Font);
  100. RES_BASE_EXTENSION("font");
  101. Vector<Ref<Texture>> textures;
  102. public:
  103. struct Character {
  104. int texture_idx;
  105. Rect2 rect;
  106. float v_align;
  107. float h_align;
  108. float advance;
  109. Character() {
  110. texture_idx = 0;
  111. v_align = 0;
  112. }
  113. };
  114. struct KerningPairKey {
  115. union {
  116. struct {
  117. uint32_t A, B;
  118. };
  119. uint64_t pair;
  120. };
  121. _FORCE_INLINE_ bool operator<(const KerningPairKey &p_r) const { return pair < p_r.pair; }
  122. };
  123. private:
  124. HashMap<int32_t, Character> char_map;
  125. Map<KerningPairKey, int> kerning_map;
  126. float height;
  127. float ascent;
  128. bool distance_field_hint;
  129. void _set_chars(const PoolVector<int> &p_chars);
  130. PoolVector<int> _get_chars() const;
  131. void _set_kernings(const PoolVector<int> &p_kernings);
  132. PoolVector<int> _get_kernings() const;
  133. void _set_textures(const Vector<Variant> &p_textures);
  134. Vector<Variant> _get_textures() const;
  135. Ref<BitmapFont> fallback;
  136. protected:
  137. static void _bind_methods();
  138. public:
  139. Error create_from_fnt(const String &p_file);
  140. void set_height(float p_height);
  141. float get_height() const;
  142. void set_ascent(float p_ascent);
  143. float get_ascent() const;
  144. float get_descent() const;
  145. void add_texture(const Ref<Texture> &p_texture);
  146. void add_char(int32_t p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance = -1);
  147. int get_character_count() const;
  148. Vector<int32_t> get_char_keys() const;
  149. Character get_character(int32_t p_char) const;
  150. int get_texture_count() const;
  151. Ref<Texture> get_texture(int p_idx) const;
  152. void add_kerning_pair(int32_t p_A, int32_t p_B, int p_kerning);
  153. int get_kerning_pair(int32_t p_A, int32_t p_B) const;
  154. Vector<KerningPairKey> get_kerning_pair_keys() const;
  155. Size2 get_char_size(CharType p_char, CharType p_next = 0) const;
  156. void set_fallback(const Ref<BitmapFont> &p_fallback);
  157. Ref<BitmapFont> get_fallback() const;
  158. void clear();
  159. void set_distance_field_hint(bool p_distance_field);
  160. bool is_distance_field_hint() const;
  161. float draw_char(RID p_canvas_item, const Point2 &p_pos, CharType p_char, CharType p_next = 0, const Color &p_modulate = Color(1, 1, 1), bool p_outline = false) const;
  162. RID get_char_texture(CharType p_char, CharType p_next, bool p_outline) const;
  163. Size2 get_char_texture_size(CharType p_char, CharType p_next, bool p_outline) const;
  164. Vector2 get_char_tx_offset(CharType p_char, CharType p_next, bool p_outline) const;
  165. Size2 get_char_tx_size(CharType p_char, CharType p_next, bool p_outline) const;
  166. Rect2 get_char_tx_uv_rect(CharType p_char, CharType p_next, bool p_outline) const;
  167. BitmapFont();
  168. ~BitmapFont();
  169. };
  170. class ResourceFormatLoaderBMFont : public ResourceFormatLoader {
  171. public:
  172. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr);
  173. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  174. virtual bool handles_type(const String &p_type) const;
  175. virtual String get_resource_type(const String &p_path) const;
  176. };
  177. VARIANT_ENUM_CAST(Font::ContourPointTag);
  178. #endif // FONT_H