forked from pioneerspacesim/pioneer
/
TextureFont.cpp
473 lines (379 loc) · 12.6 KB
/
TextureFont.cpp
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#include "TextureFont.h"
#include "libs.h"
#include "graphics/Renderer.h"
#include "graphics/VertexArray.h"
#include "TextSupport.h"
#include "utils.h"
#include <algorithm>
#include FT_GLYPH_H
#include FT_STROKER_H
namespace Text {
int TextureFont::s_glyphCount = 0;
void TextureFont::AddGlyphGeometry(Graphics::VertexArray *va, Uint32 chr, float x, float y, const Color &c)
{
glfglyph_t *glyph = &m_glyphs[chr];
const float offx = x + float(glyph->offx);
const float offy = y + GetHeight() - float(glyph->offy);
const float offU = glyph->offU;
const float offV = glyph->offV;
va->Add(vector3f(offx, offy, 0.0f), c, vector2f(offU, offV));
va->Add(vector3f(offx, offy+glyph->texHeight, 0.0f), c, vector2f(offU, offV + glyph->height));
va->Add(vector3f(offx+glyph->texWidth, offy, 0.0f), c, vector2f(offU + glyph->width, offV));
va->Add(vector3f(offx+glyph->texWidth, offy, 0.0f), c, vector2f(offU + glyph->width, offV));
va->Add(vector3f(offx, offy+glyph->texHeight, 0.0f), c, vector2f(offU, offV + glyph->height));
va->Add(vector3f(offx+glyph->texWidth, offy+glyph->texHeight, 0.0f), c, vector2f(offU + glyph->width, offV + glyph->height));
s_glyphCount++;
}
void TextureFont::MeasureString(const char *str, float &w, float &h)
{
w = h = 0.0f;
float line_width = 0.0f;
int i = 0;
while (str[i]) {
if (str[i] == '\n') {
if (line_width > w) w = line_width;
line_width = 0.0f;
h += GetHeight();
i++;
}
else {
Uint32 chr;
int n = utf8_decode_char(&chr, &str[i]);
assert(n);
i += n;
const glfglyph_t &glyph = m_glyphs[chr];
line_width += glyph.advx;
if (str[i]) {
Uint32 chr2;
n = utf8_decode_char(&chr2, &str[i]);
assert(n);
FT_UInt a = FT_Get_Char_Index(m_face, chr);
FT_UInt b = FT_Get_Char_Index(m_face, chr2);
FT_Vector kern;
FT_Get_Kerning(m_face, a, b, FT_KERNING_UNFITTED, &kern);
line_width += float(kern.x) / 64.0;
}
}
}
if (line_width > w) w = line_width;
h += GetHeight() + GetDescender();
}
void TextureFont::MeasureCharacterPos(const char *str, int charIndex, float &charX, float &charY) const
{
assert(str && (charIndex >= 0));
float x = 0.0f, y = GetHeight();
int i = 0;
Uint32 chr;
int len = utf8_decode_char(&chr, &str[i]);
while (str[i] && (i < charIndex)) {
Uint32 nextChar;
i += len;
len = utf8_decode_char(&nextChar, &str[i]);
assert(!str[i] || len); // assert valid encoding
if (chr == '\n') {
x = 0.0f;
y += GetHeight();
} else {
std::map<Uint32,glfglyph_t>::const_iterator it = m_glyphs.find(chr);
assert(it != m_glyphs.end());
float advance = it->second.advx;
if (nextChar != '\n' && nextChar != '\0') {
FT_UInt a = FT_Get_Char_Index(m_face, chr);
FT_UInt b = FT_Get_Char_Index(m_face, nextChar);
FT_Vector kern;
FT_Get_Kerning(m_face, a, b, FT_KERNING_UNFITTED, &kern);
advance += float(kern.x) / 64.0f;
}
x += advance;
}
chr = nextChar;
}
charX = x;
charY = y;
}
int TextureFont::PickCharacter(const char *str, float mouseX, float mouseY) const
{
assert(str && mouseX >= 0.0f && mouseY >= 0.0f);
// at the point of the mouse in-box test, the vars have the following values:
// i1: the index of the character being tested
// i2: the index of the next character
// charBytes: the number of bytes used to encode the next character
// right: the right edge of the box being tested
// bottom: the bottom of the box being tested
// x: the x-coordinate of the next character
// chr1: the Unicode value of the character being tested
// chr2: the Unicode value of the next character
Uint32 chr2 = '\n'; // pretend we've just had a new line
float bottom = 0.0f, x = 0.0f;
int i2 = 0, charBytes = 0;
do {
int i1 = i2;
Uint32 chr1 = chr2;
// read the next character
i2 += charBytes;
charBytes = utf8_decode_char(&chr2, &str[i2]);
assert(!str[i2] || charBytes); // assert valid encoding
float right;
if (chr1 == '\n') {
right = std::numeric_limits<float>::max();
x = 0.0f;
} else {
std::map<Uint32,glfglyph_t>::const_iterator it = m_glyphs.find(chr1);
assert(it != m_glyphs.end());
float advance = it->second.advx;
if (chr2 != '\n' && chr2 != '\0') {
FT_UInt a = FT_Get_Char_Index(m_face, chr1);
FT_UInt b = FT_Get_Char_Index(m_face, chr2);
FT_Vector kern;
FT_Get_Kerning(m_face, a, b, FT_KERNING_UNFITTED, &kern);
advance += float(kern.x) / 64.0f;
}
right = x + (advance / 2.0f);
x += advance;
}
if ((mouseY < bottom) && (mouseX < right))
return i1;
if (chr1 == '\n')
bottom += GetHeight();
} while (charBytes);
return i2;
}
void TextureFont::RenderString(const char *str, float x, float y, const Color &color)
{
m_renderer->SetBlendMode(Graphics::BLEND_ALPHA);
Graphics::VertexArray va(Graphics::ATTRIB_POSITION | Graphics::ATTRIB_DIFFUSE | Graphics::ATTRIB_UV0);
float px = x;
float py = y;
int i = 0;
while (str[i]) {
if (str[i] == '\n') {
px = x;
py += GetHeight();
i++;
}
else {
Uint32 chr;
int n = utf8_decode_char(&chr, &str[i]);
assert(n);
i += n;
glfglyph_t *glyph = &m_glyphs[chr];
AddGlyphGeometry(&va, chr, roundf(px), py, color);
if (str[i]) {
Uint32 chr2;
n = utf8_decode_char(&chr2, &str[i]);
assert(n);
FT_UInt a = FT_Get_Char_Index(m_face, chr);
FT_UInt b = FT_Get_Char_Index(m_face, chr2);
FT_Vector kern;
FT_Get_Kerning(m_face, a, b, FT_KERNING_UNFITTED, &kern);
px += float(kern.x) / 64.0;
}
px += glyph->advx;
}
}
m_renderer->DrawTriangles(&va, &m_mat);
}
Color TextureFont::RenderMarkup(const char *str, float x, float y, const Color &color)
{
m_renderer->SetBlendMode(Graphics::BLEND_ALPHA);
Graphics::VertexArray va(Graphics::ATTRIB_POSITION | Graphics::ATTRIB_DIFFUSE | Graphics::ATTRIB_UV0);
float px = x;
float py = y;
Color c = color;
int i = 0;
while (str[i]) {
if (str[i] == '#') {
int hexcol;
if (sscanf(str+i, "#%3x", &hexcol)==1) {
c.r = float((hexcol&0xf00)>>4)/255.0f;
c.g = float((hexcol&0xf0))/255.0f;
c.b = float((hexcol&0xf)<<4)/255.0f;
i+=4;
continue;
}
}
if (str[i] == '\n') {
px = x;
py += GetHeight();
i++;
}
else {
Uint32 chr;
int n = utf8_decode_char(&chr, &str[i]);
assert(n);
i += n;
glfglyph_t *glyph = &m_glyphs[chr];
AddGlyphGeometry(&va, chr, roundf(px), py, c);
// XXX kerning doesn't skip markup
if (str[i]) {
Uint32 chr2;
n = utf8_decode_char(&chr2, &str[i]);
assert(n);
FT_UInt a = FT_Get_Char_Index(m_face, chr);
FT_UInt b = FT_Get_Char_Index(m_face, chr2);
FT_Vector kern;
FT_Get_Kerning(m_face, a, b, FT_KERNING_UNFITTED, &kern);
px += float(kern.x) / 64.0;
}
px += glyph->advx;
}
}
m_renderer->DrawTriangles(&va, &m_mat);
return c;
}
TextureFont::TextureFont(const FontDescriptor &descriptor, Graphics::Renderer *renderer) : Font(descriptor), m_renderer(renderer)
{
int err; // used to store freetype error return codes
const int a_width = GetDescriptor().pixelWidth;
const int a_height = GetDescriptor().pixelHeight;
const float advx_adjust = GetDescriptor().advanceXAdjustment;
FT_Set_Pixel_Sizes(m_face, a_width, a_height);
const int sz = 512; //current fonts use maybe 1/4 of this...
m_texSize = sz;
//UV offsets for glyphs
int atlasU = 0;
int atlasV = 0;
int atlasVIncrement = 0;
//temporary RGBA pixel buffer for the glyph atlas
std::vector<unsigned char> pixBuf(4*sz*sz);
std::fill(pixBuf.begin(), pixBuf.end(), 0);
Graphics::TextureDescriptor textureDescriptor(Graphics::TEXTURE_RGBA, vector2f(sz,sz), Graphics::NEAREST_CLAMP);
m_texture.Reset(m_renderer->CreateTexture(textureDescriptor));
m_mat.texture0 = m_texture.Get();
m_mat.unlit = true;
m_mat.vertexColors = true; //to allow per-character colors
bool outline = GetDescriptor().outline;
FT_Stroker stroker(0);
if (outline) {
if (FT_Stroker_New(GetFreeTypeLibrary(), &stroker)) {
fprintf(stderr, "Freetype stroker init error\n");
abort();
}
//1*64 = stroke width
FT_Stroker_Set(stroker, 1*64, FT_STROKER_LINECAP_ROUND, FT_STROKER_LINEJOIN_ROUND, 0);
}
//load 479 characters
for (Uint32 chr=s_firstCharacter; chr<s_lastCharacter; chr++) {
glfglyph_t glfglyph;
FT_Glyph glyph;
err = FT_Load_Char(m_face, chr, FT_LOAD_FORCE_AUTOHINT);
if (err) {
fprintf(stderr, "Error %d loading glyph\n", err);
continue;
}
//get base glyph again
err = FT_Get_Glyph(m_face->glyph, &glyph);
if (err) {
fprintf(stderr, "Glyph get error %d\n", err);
continue;
}
//convert to bitmap
if (glyph->format != FT_GLYPH_FORMAT_BITMAP) {
err = FT_Glyph_To_Bitmap(&glyph, FT_RENDER_MODE_NORMAL, 0, 1);
if (err) {
fprintf(stderr, "Couldn't convert glyph to bitmap, error %d\n", err);
continue;
}
}
const FT_BitmapGlyph bmGlyph = FT_BitmapGlyph(glyph);
if (outline) {
FT_Glyph strokeGlyph;
err = FT_Get_Glyph(m_face->glyph, &strokeGlyph);
if (err) {
fprintf(stderr, "Glyph get error %d\n", err);
continue;
}
err = FT_Glyph_Stroke(&strokeGlyph, stroker, 1);
if (err) {
fprintf(stderr, "Glyph stroke error %d\n", err);
continue;
}
//convert to bitmap
if (strokeGlyph->format != FT_GLYPH_FORMAT_BITMAP) {
err = FT_Glyph_To_Bitmap(&strokeGlyph, FT_RENDER_MODE_NORMAL, 0, 1);
if (err) {
fprintf(stderr, "Couldn't convert glyph to bitmap, error %d\n", err);
continue;
}
}
const FT_BitmapGlyph bmStrokeGlyph = FT_BitmapGlyph(strokeGlyph);
//don't run off atlas borders
atlasVIncrement = std::max(atlasVIncrement, bmStrokeGlyph->bitmap.rows);
if (atlasU + bmStrokeGlyph->bitmap.width > sz) {
atlasU = 0;
atlasV += atlasVIncrement;
}
//copy to the atlas texture
//stroke first
int pitch = bmStrokeGlyph->bitmap.pitch;
for (int row=0; row < bmStrokeGlyph->bitmap.rows; row++) {
for (int col=0; col < bmStrokeGlyph->bitmap.width; col++) {
//assume black outline
const int d = 4*sz*(row+atlasV) + 4*(col+atlasU);
const int s = pitch*row + col;
pixBuf[d] = pixBuf[d+1] = pixBuf[d+2] = 0; //lum
pixBuf[d+3] = bmStrokeGlyph->bitmap.buffer[s]; //alpha
}
}
//overlay normal glyph (luminance only)
int xoff = (bmStrokeGlyph->bitmap.width - bmGlyph->bitmap.width) / 2;
int yoff = (bmStrokeGlyph->bitmap.rows - bmGlyph->bitmap.rows) / 2;
pitch = bmGlyph->bitmap.pitch;
for (int row=0; row < bmGlyph->bitmap.rows; row++) {
for (int col=0; col < bmGlyph->bitmap.width; col++) {
const int d = 4*sz*(row+atlasV+xoff) + 4*(col+atlasU+yoff);
const int s = pitch*row + col;
pixBuf[d] = pixBuf[d+1] = pixBuf[d+2] = bmGlyph->bitmap.buffer[s];
}
}
glfglyph.width = bmStrokeGlyph->bitmap.width / float(sz);
glfglyph.height = bmStrokeGlyph->bitmap.rows / float(sz);
glfglyph.offx = bmStrokeGlyph->left;
glfglyph.offy = bmStrokeGlyph->top;
glfglyph.offU = atlasU / float(sz);
glfglyph.offV = atlasV / float(sz);
atlasU += bmStrokeGlyph->bitmap.width;
FT_Done_Glyph(strokeGlyph);
}
else {
//don't run off atlas borders
atlasVIncrement = std::max(atlasVIncrement, bmGlyph->bitmap.rows);
if (atlasU + bmGlyph->bitmap.width >= sz) {
atlasU = 0;
atlasV += atlasVIncrement;
}
//copy glyph bitmap to the atlas texture
//the glyphs are upside down in the texture due to how freetype stores them
//but it's just a matter of adjusting the texcoords
const int pitch = bmGlyph->bitmap.pitch;
const int rows = bmGlyph->bitmap.rows;
for (int row=0; row < rows; row++) {
for (int col=0; col < bmGlyph->bitmap.width; col++) {
const int d = 4*sz*(row+atlasV) + 4*(col+atlasU);
const int s = pitch*row + col;
pixBuf[d] = pixBuf[d+1] = pixBuf[d+2] = pixBuf[d+3] = bmGlyph->bitmap.buffer[s];
}
}
glfglyph.width = bmGlyph->bitmap.width / float(sz);
glfglyph.height = bmGlyph->bitmap.rows / float(sz);
glfglyph.offx = bmGlyph->left;
glfglyph.offy = bmGlyph->top;
glfglyph.offU = atlasU / float(sz);
glfglyph.offV = atlasV / float(sz);
atlasU += bmGlyph->bitmap.width;
}
FT_Done_Glyph(glyph);
glfglyph.texWidth = m_texSize*glfglyph.width;
glfglyph.texHeight = m_texSize*glfglyph.height;
glfglyph.advx = float(m_face->glyph->advance.x) / 64.f + advx_adjust;
glfglyph.advy = float(m_face->glyph->advance.y) / 64.f;
m_glyphs[chr] = glfglyph;
}
//upload atlas
m_texture->Update(&pixBuf[0], vector2f(sz,sz), Graphics::IMAGE_RGBA, Graphics::IMAGE_UNSIGNED_BYTE);
if (outline)
FT_Stroker_Done(stroker);
m_height = float(m_face->height) / 64.f * float(m_face->size->metrics.y_scale) / 65536.f;
m_descender = -float(m_face->descender) / 64.f * float(m_face->size->metrics.y_scale) / 65536.f;
}
}