font.h 7.1 KB

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