font.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*************************************************************************/
  2. /* font.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef FONT_H
  30. #define FONT_H
  31. #include "resource.h"
  32. #include "scene/resources/texture.h"
  33. #include "map.h"
  34. /**
  35. @author Juan Linietsky <[email protected]>
  36. */
  37. class Font : public Resource {
  38. OBJ_TYPE( Font, Resource );
  39. RES_BASE_EXTENSION("fnt");
  40. Vector< Ref<Texture> > textures;
  41. public:
  42. struct Character {
  43. int texture_idx;
  44. Rect2 rect;
  45. float v_align;
  46. float h_align;
  47. float advance;
  48. Character() { texture_idx=0; v_align=0; }
  49. };
  50. struct KerningPairKey {
  51. union {
  52. struct {
  53. uint32_t A,B;
  54. };
  55. uint64_t pair;
  56. };
  57. _FORCE_INLINE_ bool operator<(const KerningPairKey& p_r) const { return pair<p_r.pair; }
  58. };
  59. private:
  60. HashMap< CharType, Character > char_map;
  61. Map<KerningPairKey,int> kerning_map;
  62. float height;
  63. float ascent;
  64. void _set_chars(const DVector<int>& p_chars);
  65. DVector<int> _get_chars() const;
  66. void _set_kernings(const DVector<int>& p_kernings);
  67. DVector<int> _get_kernings() const;
  68. void _set_textures(const Vector<Variant> & p_textures);
  69. Vector<Variant> _get_textures() const;
  70. protected:
  71. static void _bind_methods();
  72. public:
  73. Error create_from_fnt(const String& p_file);
  74. void set_height(float p_height);
  75. float get_height() const;
  76. void set_ascent(float p_ascent);
  77. float get_ascent() const;
  78. float get_descent() const;
  79. void add_texture(const Ref<Texture>& p_texture);
  80. void add_char(CharType p_char, int p_texture_idx, const Rect2& p_rect, const Size2& p_align, float p_advance=-1);
  81. int get_character_count() const;
  82. Vector<CharType> get_char_keys() const;
  83. Character get_character(CharType p_char) const;
  84. int get_texture_count() const;
  85. Ref<Texture> get_texture(int p_idx) const;
  86. void add_kerning_pair(CharType p_A,CharType p_B,int p_kerning);
  87. int get_kerning_pair(CharType p_A,CharType p_B) const;
  88. Vector<KerningPairKey> get_kerning_pair_keys() const;
  89. _FORCE_INLINE_ Size2 get_char_size(CharType p_char,CharType p_next=0) const;
  90. Size2 get_string_size(const String& p_string) const;
  91. void clear();
  92. 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;
  93. 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;
  94. float draw_char(RID p_canvas_item, const Point2& p_pos, const CharType& p_char,const CharType& p_next=0,const Color& p_modulate=Color(1,1,1)) const;
  95. Font();
  96. ~Font();
  97. };
  98. Size2 Font::get_char_size(CharType p_char,CharType p_next) const {
  99. const Character * c = char_map.getptr(p_char);
  100. if (!c)
  101. return Size2();
  102. Size2 ret(c->advance,c->rect.size.y);
  103. if (p_next) {
  104. KerningPairKey kpk;
  105. kpk.A=p_char;
  106. kpk.B=p_next;
  107. const Map<KerningPairKey,int>::Element *E=kerning_map.find(kpk);
  108. if (E) {
  109. ret.width-=E->get();
  110. }
  111. }
  112. return ret;
  113. }
  114. #endif