-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGFont.h
367 lines (299 loc) · 8.81 KB
/
GFont.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
Copyright (C) 2011 MoSync AB
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License,
version 2, as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
#ifndef GFONT_H_
#define GFONT_H_
// Filename: BMFont.h
//
// Handles font format from the tool BMFont (windows)
//
// BMFont Class contain all info necessary for a Font.
// Where as each letters offset position texture coordinates spacing (kerning) etc towards other characters
// Input : Parses the .fnt (textfile) containing with and height of each letter and its kerning towards other characters etc.
// Input : Texture that contains the alphabet setup N
// Font can generate vertex data with texture coordinate.
// NOTE this class is independent from the Graph system and can be use generically.
// THIS CLASS DOESN'T RENDER THE TEXT, IT CONTAINS INFORMATION ABOUT THE FONT
// SEE RenderText class for the text rendering part, that uses BMFont as input.
//---------------------------------------------------------------------------------
#include <glm/glm.hpp>
#include <string>
#include <vector>
#include <hash_map>
#include <GLES2/gl2.h>
#include "IFont.h"
class ParseBMFont;
struct BMChar;
struct BMKerning;
struct VertexType;
/**
* \brief helper typedef using pairs for hash_map<>
*/
typedef std::pair <std::string, ParseBMFont> String_ParseBMFont_Pair;
typedef std::pair <int,BMChar> Int_BMChar_Pair;
typedef std::pair <int, std::vector<BMKerning> > Int_Vector_BMKerning_Pair;
/**
* \brief TextureStore
* stores texture index for the shader to use.
* TODO add more texture info in here in the future.
*/
struct TextureStore
{
GLuint m_texture;
};
/**
* \brief Struct name: BMFont
* used for parsing BMInfo (header part of .fnt file)
*/
struct BMInfo
{
std::string m_face;
int m_size;
bool m_bold;
bool m_italic;
std::string m_charset;
int m_unicode;
int m_stretchH;
int m_smooth;
int m_aa;
int m_padding[4];
int m_spacing[2];
int m_outline;
/**
* \brief BMInfo::parse function
* @param line, contains string contains whole line of text from .fnt file
* chopped up in string array from the space delimited data.
*/
void parse(std::vector<std::string> &line);
};
/**
* \brief Struct name: BMCommon
* used for parsing BMCommon information from .fnt file
* contains information that is common for this font
*/
struct BMCommon
{
int m_lineHeight;
int m_base;
int m_scaleW;
int m_scaleH;
int m_pages;
int m_packed;
int m_alphaChnl;
int m_redChnl;
int m_greenChnl;
int m_blueChnl;
/**
* \brief BMCommon::parse function
* @param line, contains string contains whole line of text from .fnt file
* chopped up in string array from the space delimited data.
*/
void parse(std::vector<std::string> &line);
};
/**
* Struct name: BMChar
* used for parsing the BMChar entry from .fnt file
* contains information for one character.
*/
struct BMChar
{
int m_id;
int m_x;
int m_y;
int m_width;
int m_height;
int m_xoffset;
int m_yoffset;
int m_xadvance;
int m_page;
int m_chnl;
/**
* \brief BMChar::parse function
* @param line, contains string contains whole line of text from .fnt file
* chopped up in string array from the space delimited data.
*/
void parse(std::vector<std::string> &line);
};
/**
* \brief Struct name: BMKerning
* used for parsing the BMKerning entry from .fnt file
* contains kerning information, spacing between one character to another character.
*/
struct BMKerning
{
int m_first;
int m_second;
int m_amount;
/**
* \brief BMKerning::parse function
* @param line, contains string contains whole line of text from .fnt file
* chopped up in string array from the space delimited data.
*/
void parse(std::vector<std::string> &line);
};
/**
* \brief Struct name: BMPage
* used for parsing the BMPage entry from .fnt file
* contains texture index to be used, one font could contain several texture
*/
struct BMPage
{
int m_id;
std::string m_file;
/**
* \brief BMPage::parse function
* @param line, contains string contains whole line of text from .fnt file
* chopped up in string array from the space delimited data.
*/
void parse(std::vector<std::string> &line);
};
/**
* \brief FontType
*/
struct FontType
{
float left, right;
int size;
};
/**
* \brief VertexType
* contains vertex info, position and texture coordinate
*/
struct VertexType
{
glm::vec2 position;
glm::vec2 texture;
};
/**
* \brief Class name: BMFont (main class)
* main Font class that holds all information about the specific font.
* Texture and each character info.
* also provides with building a vertex table
*/
class BMFont : public IFont
{
private:
// ParseBMFont enumeration for each different element .fnt file contains.
enum ParseBMFont
{
PBM_IDLE = 0,
PBM_INFO,
PBM_COMMON,
PBM_PAGE,
PBM_CHARS,
PBM_CHAR,
PBM_KERNINGS,
PBM_KERNING,
};
public:
/**
* \brief BMFont
* Constructor
*/
BMFont();
/**
* \brief BMFont
* Copy Constructor
*/
BMFont(const BMFont&);
/**
* \brief ~BMFont
* Destructor
*/
virtual ~BMFont();
/**
* \brief Init(), initiates from resource data .fnt file and texture.
* @param fontResource, resource of a .fnt contains textual information about this font.
* @param textureResources, array of resource of one or multiple font textures that represents a Font.
* @return bool true/false true = success, false = failed.
*/
virtual bool Init(MAHandle fontResource, std::vector<MAHandle> &textureResources);
/**
* \brief Clear
* Release Texture and Font DB creation
*/
virtual void Clear();
/**
* \brief GetTexture get texture after its index from array
* @param i, index in array to lookup the texture
* @return GLuint shader index.
*/
virtual GLuint GetTexture(int i=0);
/**
* \brief BuildVertexArray buids the vertex array
* from any given string containing this BMFont.
* @param vertexPtr vertex, pointer where the user provides to be filled,
* user also needs to have sufficient allocated space for this
* @param sentence char* string, to build a vertex table from
* @param drawX, position in X
* @param drawY, position in Y
* @param scaleX, scale in X
* @param scaleY, scale in Y
* @return string width
*/
virtual float BuildVertexArray(glm::vec4 * vertexPtr, const char * str, float dX, float dY, float scaleX, float scaleY);
/**
* \brief getTextProperties calculates the vertex array from any given string containing this BMFont.
* @param sentence char* string, to build a vertex table from
* @param drawX, position in X
* @param drawY, position in Y
* @param scaleX, scale in X
* @param scaleY, scale in Y
* @param property, output vec2 => width , height of text.
* @return string width
*/
virtual float getTextProperties(const char* sentence, float drawX, float drawY, float scaleX, float scaleY,TextProperty *property);
/**
* \brief getCommon information for the font
* @return struct of BMCommon
*/
virtual const BMCommon *getCommon() {return &m_common;}
/**
* \brief getInfo information
* @return struct BMInfo
*/
virtual const BMInfo *getInfo() {return &m_info;}
private:
// Helper functions (internal use)
/**
* \brief LoadFontData.. load a .fnt file and parse
* @param resource of an .fnt file
* @return bool true/false, true = success, false = failed
*/
bool LoadFontData(MAHandle resource);
/**
* \brief ReleaseFontData...
*/
void ReleaseFontData();
/**
* \brief ReleaseTexture...
*/
void ReleaseTexture();
/**
* \brief getBMType
* @param header lookup enum from string hence the name
* @return ParseBMFont enum
*/
ParseBMFont getBMType(std::string &header);
std::hash_map<std::string, ParseBMFont> m_string2enum; // Parsing setup should be static! or singleton
BMInfo m_info; // Header info of current Font
BMCommon m_common; // Common data for current Font
std::hash_map<int,BMChar> m_chars; // Contains table for each letter info texture coord width height offset position etc.
std::hash_map<int, std::vector<BMKerning> > m_kernings; // Contains table of letters kerning toward other letters (spacing)
std::vector<TextureStore> m_texStores; // Contains all textures that this alphabet has (but renderer uses only one)
std::vector<BMPage> m_pages; // Page info, not in use! parses data for future ref.
int m_nchars; // amount of characters in font
int m_nkernings; // amount of kerning information in font
};
#endif /* GFONT_H_ */