font.h
1 /*************************************************************************/
2 /* font.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 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 
32 #include "resource.h"
33 #include "scene/resources/texture.h"
34 #include "map.h"
38 class Font : public Resource {
39 
40  OBJ_TYPE( Font, Resource );
41  RES_BASE_EXTENSION("fnt");
42 
43  Vector< Ref<Texture> > textures;
44 
45 public:
46  struct Character {
47 
48  int texture_idx;
49  Rect2 rect;
50  float v_align;
51  float h_align;
52  float advance;
53 
54  Character() { texture_idx=0; v_align=0; }
55  };
56 
57  struct KerningPairKey {
58 
59  union {
60  struct {
61  uint32_t A,B;
62  };
63 
64  uint64_t pair;
65  };
66 
67  _FORCE_INLINE_ bool operator<(const KerningPairKey& p_r) const { return pair<p_r.pair; }
68  };
69 
70 private:
71 
72 
74  Map<KerningPairKey,int> kerning_map;
75 
76  float height;
77  float ascent;
78  bool distance_field_hint;
79 
80  void _set_chars(const DVector<int>& p_chars);
81  DVector<int> _get_chars() const;
82  void _set_kernings(const DVector<int>& p_kernings);
83  DVector<int> _get_kernings() const;
84  void _set_textures(const Vector<Variant> & p_textures);
85  Vector<Variant> _get_textures() const;
86 
87  Ref<Font> fallback;
88 protected:
89 
90  static void _bind_methods();
91 
92 public:
93 
94  Error create_from_fnt(const String& p_file);
95 
96  void set_height(float p_height);
97  float get_height() const;
98 
99  void set_ascent(float p_ascent);
100  float get_ascent() const;
101  float get_descent() const;
102 
103  void add_texture(const Ref<Texture>& p_texture);
104  void add_char(CharType p_char, int p_texture_idx, const Rect2& p_rect, const Size2& p_align, float p_advance=-1);
105 
106  int get_character_count() const;
107  Vector<CharType> get_char_keys() const;
108  Character get_character(CharType p_char) const;
109 
110  int get_texture_count() const;
111  Ref<Texture> get_texture(int p_idx) const;
112 
113  void add_kerning_pair(CharType p_A,CharType p_B,int p_kerning);
114  int get_kerning_pair(CharType p_A,CharType p_B) const;
115  Vector<KerningPairKey> get_kerning_pair_keys() const;
116 
117  inline Size2 get_char_size(CharType p_char,CharType p_next=0) const;
118  Size2 get_string_size(const String& p_string) const;
119 
120 
121  void set_fallback(const Ref<Font> &p_fallback);
122  Ref<Font> get_fallback() const;
123 
124  void clear();
125 
126  void set_distance_field_hint(bool p_distance_field);
127  bool is_distance_field_hint() const;
128 
129  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;
130  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;
131  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;
132 
133  Font();
134  ~Font();
135 };
136 
137 
138 Size2 Font::get_char_size(CharType p_char,CharType p_next) const {
139 
140  const Character * c = char_map.getptr(p_char);
141 
142  if (!c) {
143  if (fallback.is_valid())
144  return fallback->get_char_size(p_char,p_next);
145  return Size2();
146  }
147 
148  Size2 ret(c->advance,c->rect.size.y);
149 
150  if (p_next) {
151 
152  KerningPairKey kpk;
153  kpk.A=p_char;
154  kpk.B=p_next;
155 
156  const Map<KerningPairKey,int>::Element *E=kerning_map.find(kpk);
157  if (E) {
158 
159  ret.width-=E->get();
160  }
161  }
162 
163  return ret;
164 }
165 
166 
167 
168 #endif
Definition: font.h:38
Definition: color.h:37
Definition: math_2d.h:204
Definition: map.h:50
Definition: hash_map.h:84
Definition: resource.h:89
Definition: rid.h:47
Definition: map.h:41
Definition: math_2d.h:65
Definition: font.h:46
Definition: ustring.h:64
Definition: vector.h:43
Definition: font.h:57