font.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*************************************************************************/
  2. /* font.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 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. virtual float get_height() const = 0;
  41. virtual float get_ascent() const = 0;
  42. virtual float get_descent() const = 0;
  43. virtual Size2 get_char_size(CharType p_char, CharType p_next = 0) const = 0;
  44. Size2 get_string_size(const String &p_string) const;
  45. Size2 get_wordwrap_string_size(const String &p_string, float p_width) const;
  46. virtual bool is_distance_field_hint() const = 0;
  47. 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;
  48. 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;
  49. virtual bool has_outline() const { return false; }
  50. 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;
  51. void update_changes();
  52. Font();
  53. };
  54. // Helper class to that draws outlines immediately and draws characters in its destructor.
  55. class FontDrawer {
  56. const Ref<Font> &font;
  57. Color outline_color;
  58. bool has_outline;
  59. struct PendingDraw {
  60. RID canvas_item;
  61. Point2 pos;
  62. CharType chr;
  63. CharType next;
  64. Color modulate;
  65. };
  66. Vector<PendingDraw> pending_draws;
  67. public:
  68. FontDrawer(const Ref<Font> &p_font, const Color &p_outline_color) :
  69. font(p_font),
  70. outline_color(p_outline_color) {
  71. has_outline = p_font->has_outline();
  72. }
  73. 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)) {
  74. if (has_outline) {
  75. PendingDraw draw = { p_canvas_item, p_pos, p_char, p_next, p_modulate };
  76. pending_draws.push_back(draw);
  77. }
  78. return font->draw_char(p_canvas_item, p_pos, p_char, p_next, has_outline ? outline_color : p_modulate, has_outline);
  79. }
  80. ~FontDrawer() {
  81. for (int i = 0; i < pending_draws.size(); ++i) {
  82. const PendingDraw &draw = pending_draws[i];
  83. font->draw_char(draw.canvas_item, draw.pos, draw.chr, draw.next, draw.modulate, false);
  84. }
  85. }
  86. };
  87. class BitmapFont : public Font {
  88. GDCLASS(BitmapFont, Font);
  89. RES_BASE_EXTENSION("font");
  90. Vector<Ref<Texture> > textures;
  91. public:
  92. struct Character {
  93. int texture_idx;
  94. Rect2 rect;
  95. float v_align;
  96. float h_align;
  97. float advance;
  98. Character() {
  99. texture_idx = 0;
  100. v_align = 0;
  101. }
  102. };
  103. struct KerningPairKey {
  104. union {
  105. struct {
  106. uint32_t A, B;
  107. };
  108. uint64_t pair;
  109. };
  110. _FORCE_INLINE_ bool operator<(const KerningPairKey &p_r) const { return pair < p_r.pair; }
  111. };
  112. private:
  113. HashMap<CharType, Character> char_map;
  114. Map<KerningPairKey, int> kerning_map;
  115. float height;
  116. float ascent;
  117. bool distance_field_hint;
  118. void _set_chars(const PoolVector<int> &p_chars);
  119. PoolVector<int> _get_chars() const;
  120. void _set_kernings(const PoolVector<int> &p_kernings);
  121. PoolVector<int> _get_kernings() const;
  122. void _set_textures(const Vector<Variant> &p_textures);
  123. Vector<Variant> _get_textures() const;
  124. Ref<BitmapFont> fallback;
  125. protected:
  126. static void _bind_methods();
  127. public:
  128. Error create_from_fnt(const String &p_file);
  129. void set_height(float p_height);
  130. float get_height() const;
  131. void set_ascent(float p_ascent);
  132. float get_ascent() const;
  133. float get_descent() const;
  134. void add_texture(const Ref<Texture> &p_texture);
  135. void add_char(CharType p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance = -1);
  136. int get_character_count() const;
  137. Vector<CharType> get_char_keys() const;
  138. Character get_character(CharType p_char) const;
  139. int get_texture_count() const;
  140. Ref<Texture> get_texture(int p_idx) const;
  141. void add_kerning_pair(CharType p_A, CharType p_B, int p_kerning);
  142. int get_kerning_pair(CharType p_A, CharType p_B) const;
  143. Vector<KerningPairKey> get_kerning_pair_keys() const;
  144. Size2 get_char_size(CharType p_char, CharType p_next = 0) const;
  145. void set_fallback(const Ref<BitmapFont> &p_fallback);
  146. Ref<BitmapFont> get_fallback() const;
  147. void clear();
  148. void set_distance_field_hint(bool p_distance_field);
  149. bool is_distance_field_hint() const;
  150. 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;
  151. BitmapFont();
  152. ~BitmapFont();
  153. };
  154. class ResourceFormatLoaderBMFont : public ResourceFormatLoader {
  155. public:
  156. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  157. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  158. virtual bool handles_type(const String &p_type) const;
  159. virtual String get_resource_type(const String &p_path) const;
  160. };
  161. #endif