font.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*************************************************************************/
  2. /* font.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "map.h"
  33. #include "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;
  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;
  51. 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)) const = 0;
  52. void update_changes();
  53. Font();
  54. };
  55. class BitmapFont : public Font {
  56. GDCLASS(BitmapFont, Font);
  57. RES_BASE_EXTENSION("font");
  58. Vector<Ref<Texture> > textures;
  59. public:
  60. struct Character {
  61. int texture_idx;
  62. Rect2 rect;
  63. float v_align;
  64. float h_align;
  65. float advance;
  66. Character() {
  67. texture_idx = 0;
  68. v_align = 0;
  69. }
  70. };
  71. struct KerningPairKey {
  72. union {
  73. struct {
  74. uint32_t A, B;
  75. };
  76. uint64_t pair;
  77. };
  78. _FORCE_INLINE_ bool operator<(const KerningPairKey &p_r) const { return pair < p_r.pair; }
  79. };
  80. private:
  81. HashMap<CharType, Character> char_map;
  82. Map<KerningPairKey, int> kerning_map;
  83. float height;
  84. float ascent;
  85. bool distance_field_hint;
  86. void _set_chars(const PoolVector<int> &p_chars);
  87. PoolVector<int> _get_chars() const;
  88. void _set_kernings(const PoolVector<int> &p_kernings);
  89. PoolVector<int> _get_kernings() const;
  90. void _set_textures(const Vector<Variant> &p_textures);
  91. Vector<Variant> _get_textures() const;
  92. Ref<BitmapFont> fallback;
  93. protected:
  94. static void _bind_methods();
  95. public:
  96. Error create_from_fnt(const String &p_file);
  97. void set_height(float p_height);
  98. float get_height() const;
  99. void set_ascent(float p_ascent);
  100. float get_ascent() const;
  101. float get_descent() const;
  102. void add_texture(const Ref<Texture> &p_texture);
  103. void add_char(CharType p_char, int p_texture_idx, const Rect2 &p_rect, const Size2 &p_align, float p_advance = -1);
  104. int get_character_count() const;
  105. Vector<CharType> get_char_keys() const;
  106. Character get_character(CharType p_char) const;
  107. int get_texture_count() const;
  108. Ref<Texture> get_texture(int p_idx) const;
  109. void add_kerning_pair(CharType p_A, CharType p_B, int p_kerning);
  110. int get_kerning_pair(CharType p_A, CharType p_B) const;
  111. Vector<KerningPairKey> get_kerning_pair_keys() const;
  112. Size2 get_char_size(CharType p_char, CharType p_next = 0) const;
  113. void set_fallback(const Ref<BitmapFont> &p_fallback);
  114. Ref<BitmapFont> get_fallback() const;
  115. void clear();
  116. void set_distance_field_hint(bool p_distance_field);
  117. bool is_distance_field_hint() const;
  118. 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)) const;
  119. BitmapFont();
  120. ~BitmapFont();
  121. };
  122. #endif