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
368
369
|
#include "../jin_configuration.h"
#if LIBJIN_MODULES_RENDER
#include "OpenGL.h"
#include "font.h"
#include <stdio.h>
#include "color.h"
#include "Shader.h"
#include "../Common/Array.hpp"
namespace jin
{
namespace graphics
{
#include "Shaders/font.shader.h"
using namespace std;
using namespace jin::math;
const int Font::TEXTURE_WIDTHS[] = { 128, 256, 256, 512, 512, 1024, 1024 };
const int Font::TEXTURE_HEIGHTS[] = { 128, 128, 256, 256, 512, 512, 1024 };
/* utf8 byte string to unicode codepoint */
static const char* utf8toCodepoint(const char *p, unsigned *res) {
unsigned x, mask, shift;
switch (*p & 0xf0) {
case 0xf0: mask = 0x07; shift = 18; break;
case 0xe0: mask = 0x0f; shift = 12; break;
case 0xc0:
case 0xd0: mask = 0x1f; shift = 6; break;
default:
*res = *p;
return p + 1;
}
x = (*p & mask) << shift;
do {
if (*(++p) == '\0') {
*res = x;
return p;
}
shift -= 6;
x |= (*p & 0x3f) << shift;
} while (shift);
*res = x;
return p + 1;
}
/* little endian unicode */
static const char* unicodeLittleEndian(const char* p, unsigned* res)
{
}
/*static*/ Font* Font::createFont(FontData* fontData, unsigned int fontSzie)
{
Font* font;
try
{
font = new Font(fontData, fontSzie);
}
catch (...)
{
return nullptr;
}
return font;
}
void Font::destroyFont(Font* font)
{
if (font != nullptr)
delete font;
}
Font::Font(FontData* f, unsigned int fontSize)
: cursor(0, 0)
, font(f)
, fontsize(fontSize)
{
font->pushFontsize(fontsize);
font->getVMetrics(&baseline, &descent);
estimateSize();
font->popFontsize();
/* create a default texture */
createAtlas();
}
/* estimate the size of atlas texture */
void Font::estimateSize()
{
for (int level = 0; level <= TEXTURE_SIZE_LEVEL_MAX; ++level)
{
if (descent * (descent*0.8) * 96 <= TEXTURE_WIDTHS[level] * TEXTURE_HEIGHTS[level])
{
textureWidth = TEXTURE_WIDTHS[level];
textureHeight = TEXTURE_HEIGHTS[level];
break;
}
}
}
Font::~Font()
{
map<unsigned int, Glyph*>::iterator it = glyphs.begin();
for (; it != glyphs.end(); ++it)
{
delete it->second;
}
}
GLuint Font::createAtlas()
{
GLuint t;
gl.flushError();
t = gl.genTexture();
gl.bindTexture(t);
gl.setTexParameter(GL_TEXTURE_MAG_FILTER, GL_LINEAR);
gl.setTexParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR);
gl.setTexParameter(GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
gl.setTexParameter(GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
gl.texImage(GL_RGBA8, textureWidth, textureHeight, GL_RGBA, GL_UNSIGNED_BYTE);
if (glGetError() != GL_NO_ERROR)
{
glDeleteTextures(1, &t);
gl.bindTexture(0);
return 0;
}
atlases.push_back(t);
gl.bindTexture(0);
return t;
}
void Font::print(const char* t, int x, int y, int lineheight, int spacing)
{
Page* page = typeset(t, lineheight, spacing);
print(page, x, y);
delete page;
}
Page* Font::typeset(const char* text, int lineheight, int spacing)
{
// typesetting, for reducing draw call
const char* t = text;
Page* page = new Page();
vector<GlyphArrayDrawInfo>& glyphinfolist = page->glyphinfolist;
vector<GlyphVertex>& glyphvertices = page->glyphvertices;
Vector2<int> p(0, 0);
Codepoint c;
int texture = -1;
Glyph* glyph = nullptr;
GlyphVertex vertex;
for (int i = 0; *t != NULL; i += 4)
{
t = utf8toCodepoint(t, &c);
if (c == 0x0D)
{
i -= 4;
continue;
}
/* new line */
if (c == 0x0A)
{
p.y += lineheight;
p.x = 0;
i -= 4;
continue;
}
glyph = findGlyph(c);
if (texture != glyph->atlas)
{
GlyphArrayDrawInfo info;
info.start = i;
info.count = 0;
info.texture = glyph->atlas;
texture = glyph->atlas;
glyphinfolist.push_back(info);
}
glyphinfolist[glyphinfolist.size() - 1].count += 4;
Glyph::Bbox& bbox = glyph->bbox;
// 1
vertex.x = p.x; vertex.y = p.y;
vertex.u = bbox.x; vertex.v = bbox.y;
glyphvertices.push_back(vertex);
// 2
vertex.x = p.x; vertex.y = p.y + glyph->height;
vertex.u = bbox.x; vertex.v = bbox.y + bbox.height;
glyphvertices.push_back(vertex);
// 3
vertex.x = p.x + glyph->width; vertex.y = p.y + glyph->height;
vertex.u = bbox.x + bbox.width; vertex.v = bbox.y + bbox.height;
glyphvertices.push_back(vertex);
// 4
vertex.x = p.x + glyph->width; vertex.y = p.y;
vertex.u = bbox.x + bbox.width; vertex.v = bbox.y;
glyphvertices.push_back(vertex);
p.x += glyph->width + spacing;
}
getTextBox(text, &page->width, &page->height, lineheight, spacing);
return page;
}
void Font::print(const Page* page, int x, int y)
{
Shader* shader = Shader::getCurrentShader();
const vector<GlyphArrayDrawInfo>& glyphinfolist = page->glyphinfolist;
const vector<GlyphVertex>& glyphvertices = page->glyphvertices;
gl.ModelMatrix.setTransformation(x, y, 0, 1, 1, 0, 0);
shader->sendMatrix4(Shader::SHADER_MODEL_MATRIX, &gl.ModelMatrix);
shader->sendMatrix4(Shader::SHADER_PROJECTION_MATRIX, &gl.ProjectionMatrix);
for (int i = 0; i < glyphinfolist.size(); ++i)
{
const GlyphArrayDrawInfo& info = glyphinfolist[i];
shader->bindVertexPointer(2, GL_INT, sizeof(GlyphVertex), &glyphvertices[info.start].x);
shader->bindUVPointer(2, GL_FLOAT, sizeof(GlyphVertex), &glyphvertices[info.start].u);
gl.bindTexture(info.texture);
gl.drawArrays(GL_QUADS, 0, info.count);
gl.bindTexture(0);
}
}
int Font::getCharWidth(int c)
{
int adw, lsb;
font->pushFontsize(fontsize);
font->getHMetrics(c, &adw, &lsb);
font->popFontsize();
return adw;
}
int Font::getCharHeight(int c)
{
return descent;
}
int Font::getTextWidth(const char* t, int spacing)
{
font->pushFontsize(fontsize);
int res = 0;
int tmp = 0;
const char *p = t;
while (*p) {
unsigned c;
p = utf8toCodepoint(p, &c);
if (*p == 0x0D)
continue;
if (*p == 0x0A)
{
tmp = 0;
continue;
}
tmp += getCharWidth(c) + spacing;
if (tmp > res) res = tmp;
}
font->popFontsize();
return res;
}
int Font::getTextHeight(const char* t, int lineheight)
{
font->pushFontsize(fontsize);
int res = 0;
bool newline = true;
while (*t)
{
unsigned c;
t = utf8toCodepoint(t, &c);
if (*t == 0x0A)
newline = true;
else if (*t == 0x0D);
else if (newline)
{
newline = false;
res += lineheight;
}
}
font->popFontsize();
return res;
}
void Font::getTextBox(const char* text, int* w, int* h, int lineheight, int spacing)
{
font->pushFontsize(fontsize);
*w = 0;
*h = 0;
int tmp = 0;
const char* p = text;
const char* pt = nullptr;
bool nl = true;
while (*p) {
unsigned c;
pt = p;
p = utf8toCodepoint(p, &c);
if (*pt == 0x0D)
continue;
if (*pt == 0x0A)
{
tmp = 0;
nl = true;
continue;
}
else if(nl)
{
nl = false;
*h += lineheight;
}
tmp += getCharWidth(c) + spacing;
if (tmp > *w) *w = tmp;
}
font->popFontsize();
}
Glyph* Font::bakeGlyph(unsigned int character)
{
Glyph* glyph = (Glyph*)malloc(sizeof(Glyph));
int w, h, xoff, yoff;
font->pushFontsize(fontsize);
GLuint atlas = atlases.back();
const Color* bitmap = font->getCodepointBitmap(character, &w, &h, &xoff, &yoff);
int adw, lsb;
{
font->getHMetrics(character, &adw, &lsb);
font->popFontsize();
if (cursor.x + adw > textureWidth )
{
cursor.x = 0;
cursor.y += descent;
if (cursor.y + descent * 2 > textureHeight)
{
/* create new atlas */
atlas = createAtlas();
cursor.y = 0;
}
}
gl.bindTexture(atlas);
gl.texSubImage(cursor.x + xoff, cursor.y + yoff + baseline, w, h, GL_RGBA, GL_UNSIGNED_BYTE, bitmap);
gl.bindTexture();
delete[] bitmap;
}
glyph->atlas = atlas;
glyph->bbox.x = cursor.x / (float)textureWidth;
glyph->bbox.y = cursor.y / (float)textureHeight;
glyph->bbox.width = adw / (float)textureWidth;
glyph->bbox.height = descent / (float)textureHeight;
glyph->width = adw;
glyph->height = descent;
glyphs.insert(std::pair<unsigned int, Glyph*>(character, glyph));
cursor.x += adw;
return glyph;
}
Glyph* Font::findGlyph(unsigned int character)
{
map<unsigned int, Glyph*>::iterator it = glyphs.find(character);
if (it != glyphs.end())
{
return it->second;
}
else
{
Glyph* glyph = bakeGlyph(character);
return glyph;
}
}
} // graphics
} // jin
#endif // LIBJIN_MODULES_RENDER
|