TDME2  1.9.200
GUIFont.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <ft2build.h>
4 #include FT_FREETYPE_H
5 
6 #include <string>
7 #include <unordered_map>
8 
9 #include <tdme/tdme.h>
11 #include <tdme/gui/fwd-tdme.h>
20 
22 
23 using std::string;
24 using std::unordered_map;
25 
35 
36 /**
37  * GUI font class
38  * @author Andreas Drewke
39  */
41 {
42 private:
43  static bool ftInitialized;
44  static FT_Library ftLibrary;
45  string ftPathName;
46  vector<uint8_t> ttfData;
47  FT_Open_Args ftOpenArgs;
48  FT_Face ftFace;
50  int32_t textureId { 0 };
51  unordered_map<uint32_t, GUICharacter*> chars;
52  float lineHeight { 0.0f };
53  float baseLine { 0.0f };
54 
55  // forbid class copy
57 
58  /**
59  * Public constructor
60  * @param pathName font path name
61  * @param fileName font file name
62  * @param size font pixel size
63  */
64  GUIFont(const string& pathName, const string& fileName, int size);
65 
66  /**
67  * Add character with given id to texture atlas
68  * @param charId character id
69  * @return GUI character entity
70  */
71  GUICharacter* addToTextureAtlas(uint32_t charId);
72 
73  /**
74  * Update texture atlas
75  * @param text text chars to be included in atlas
76  */
77  inline void addCharactersToFont(const string& text) {
78  auto updatedTextureAtlas = false;
79  UTF8CharacterIterator u8It(text);
80  while (u8It.hasNext() == true) {
81  auto characterId = u8It.next();
82  if (characterId == -1) continue;
83  if (getCharacter(characterId) == nullptr) {
84  addToTextureAtlas(characterId);
85  updatedTextureAtlas = true;
86  }
87  }
88  if (updatedTextureAtlas == true) updateFontInternal();
89  }
90 
91  /**
92  * Update font texture atlas and character definitions
93  */
94  inline void updateFont() {
96  }
97 
98  /**
99  * Do the update work
100  */
101  void updateFontInternal();
102 
103 public:
104  /**
105  * Parse the font definition file
106  * @param pathName font path name
107  * @param fileName font file name
108  * @param size font pixel size
109  * @throws tdme::os::filesystem::FileSystemException
110  */
111  static GUIFont* parse(const string& pathName, const string& fileName, int size);
112 
113  /**
114  * Destructor
115  */
116  ~GUIFont();
117 
118  /**
119  * Init
120  */
121  void initialize();
122 
123  /**
124  * Dispose
125  */
126  void dispose();
127 
128  /**
129  * @return texture id
130  */
131  inline int32_t getTextureId() {
132  return textureId;
133  }
134 
135  /**
136  * Get character defintion
137  * @param charId character id
138  * @return character definition
139  */
140  inline GUICharacter* getCharacter(uint32_t charId) {
141  // ignore -1 character
142  if (charId == -1) return nullptr;
143  // try to get char and return it
144  auto charIt = chars.find(charId);
145  if (charIt != chars.end()) return charIt->second;
146  // no yet added, add it
147  return addToTextureAtlas(charId);
148  }
149 
150  /**
151  * @return line height
152  */
153  inline float getLineHeight() {
154  return lineHeight;
155  }
156 
157  /**
158  * @return base line
159  */
160  inline float getBaseLine() {
161  return baseLine;
162  }
163 
164  /**
165  * Get text index X of given text and index
166  * @param text text
167  * @param offset offset
168  * @param length length or 0 if full length
169  * @param index index
170  * @return text index x
171  */
172  int getTextIndexX(const MutableString& text, int offset, int length, int index);
173 
174  /**
175  * Get text index by text and X in space of text
176  * @param text text
177  * @param offset offset
178  * @param length length or 0 if full length
179  * @param textX text X
180  * @return text index
181  */
182  int getTextIndexByX(const MutableString& text, int offset, int length, int textX);
183 
184  /**
185  * Text width
186  * @param text text
187  * @return text width
188  */
189  int getTextWidth(const MutableString& text);
190 
191  /**
192  * Get text index X at width
193  * @param text text
194  * @return text width
195  */
196  int getTextIndexXAtWidth(const MutableString& text, int width);
197 
198  /**
199  * Draw character
200  * @param guiRenderer gui renderer
201  * @param character character
202  * @param x x
203  * @param y y
204  * @param color color
205  */
206  void drawCharacter(GUIRenderer* guiRenderer, GUICharacter* character, int x, int y, const GUIColor& color = GUIColor::GUICOLOR_WHITE);
207 
208  /**
209  * Draw background
210  * @param guiRenderer gui renderer
211  * @param character character
212  * @param x x
213  * @param y y
214  * @param lineHeight line height
215  * @param color color
216  */
217  void drawCharacterBackground(GUIRenderer* guiRenderer, GUICharacter* character, int x, int y, int lineHeight, const GUIColor& color);
218 
219  /**
220  * Draw string
221  * @param guiRenderer gui renderer
222  * @param x x
223  * @param y y
224  * @param text text
225  * @param offset offset
226  * @param length length or 0 if full length
227  * @param color color
228  * @param selectionStartIndex selection start index
229  * @param selectionEndIndex selection end index
230  * @param backgroundColor background color
231  */
232  void drawString(GUIRenderer* guiRenderer, int x, int y, const MutableString& text, int offset, int length, const GUIColor& color, int selectionStartIndex = -1, int selectionEndIndex = -1, const GUIColor& backgroundColor = GUIColor::GUICOLOR_TRANSPARENT);
233 
234 };
Texture entity.
Definition: Texture.h:24
GUI screen node that represents a screen that can be rendered via GUI system.
Definition: GUIScreenNode.h:72
GUI font class.
Definition: GUIFont.h:41
void drawString(GUIRenderer *guiRenderer, int x, int y, const MutableString &text, int offset, int length, const GUIColor &color, int selectionStartIndex=-1, int selectionEndIndex=-1, const GUIColor &backgroundColor=GUIColor::GUICOLOR_TRANSPARENT)
Draw string.
Definition: GUIFont.cpp:344
GUICharacter * getCharacter(uint32_t charId)
Get character defintion.
Definition: GUIFont.h:140
unordered_map< uint32_t, GUICharacter * > chars
Definition: GUIFont.h:51
GUICharacter * addToTextureAtlas(uint32_t charId)
Add character with given id to texture atlas.
Definition: GUIFont.cpp:112
void drawCharacter(GUIRenderer *guiRenderer, GUICharacter *character, int x, int y, const GUIColor &color=GUIColor::GUICOLOR_WHITE)
Draw character.
Definition: GUIFont.cpp:275
void drawCharacterBackground(GUIRenderer *guiRenderer, GUICharacter *character, int x, int y, int lineHeight, const GUIColor &color)
Draw background.
Definition: GUIFont.cpp:314
vector< uint8_t > ttfData
Definition: GUIFont.h:46
int getTextIndexX(const MutableString &text, int offset, int length, int index)
Get text index X of given text and index.
Definition: GUIFont.cpp:213
int getTextIndexByX(const MutableString &text, int offset, int length, int textX)
Get text index by text and X in space of text.
Definition: GUIFont.cpp:229
void updateFontInternal()
Do the update work.
Definition: GUIFont.cpp:174
static bool ftInitialized
Definition: GUIFont.h:43
static FT_Library ftLibrary
Definition: GUIFont.h:44
TextureAtlas textureAtlas
Definition: GUIFont.h:49
FT_Open_Args ftOpenArgs
Definition: GUIFont.h:47
GUIFont(const string &pathName, const string &fileName, int size)
Public constructor.
Definition: GUIFont.cpp:57
void addCharactersToFont(const string &text)
Update texture atlas.
Definition: GUIFont.h:77
static GUIFont * parse(const string &pathName, const string &fileName, int size)
Parse the font definition file.
Definition: GUIFont.cpp:93
void updateFont()
Update font texture atlas and character definitions.
Definition: GUIFont.h:94
int getTextIndexXAtWidth(const MutableString &text, int width)
Get text index X at width.
Definition: GUIFont.cpp:262
int getTextWidth(const MutableString &text)
Text width.
Definition: GUIFont.cpp:249
Mutable utf8 aware string class.
Definition: MutableString.h:23
String tools class.
Definition: StringTools.h:22
UTF8 string character iterator.
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6