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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
|
/*
* Copyright 2013 Jeremie Roy. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#define USE_EDTAA3 0
#include <bx/macros.h>
#if BX_COMPILER_MSVC
# define generic GenericFromFreeType // WinRT language extensions see "generic" as a keyword... this is stupid
#endif // BX_COMPILER_MSVC
BX_PRAGMA_DIAGNOSTIC_PUSH();
BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4245) // error C4245: '=' : conversion from 'int' to 'FT_UInt', signed/unsigned mismatch
#if BX_COMPILER_MSVC || BX_COMPILER_GCC >= 40300
#pragma push_macro("interface")
#endif
#undef interface
#include <freetype/freetype.h>
#if BX_COMPILER_MSVC || BX_COMPILER_GCC >= 40300
#pragma pop_macro("interface")
#endif
BX_PRAGMA_DIAGNOSTIC_POP();
#include "../common.h"
#include <bgfx/bgfx.h>
#include <math.h>
#if USE_EDTAA3
# include <edtaa3/edtaa3func.cpp>
#else
# define SDF_IMPLEMENTATION
# include <sdf/sdf.h>
#endif // USE_EDTAA3
#include <wchar.h> // wcslen
#include <tinystl/allocator.h>
#include <tinystl/unordered_map.h>
namespace stl = tinystl;
#include "font_manager.h"
#include "../cube_atlas.h"
struct FTHolder
{
FT_Library library;
FT_Face face;
};
class TrueTypeFont
{
public:
TrueTypeFont();
~TrueTypeFont();
/// Initialize from an external buffer
/// @remark The ownership of the buffer is external, and you must ensure it stays valid up to this object lifetime
/// @return true if the initialization succeed
bool init(const uint8_t* _buffer, uint32_t _bufferSize, int32_t _fontIndex, uint32_t _pixelHeight);
/// return the font descriptor of the current font
FontInfo getFontInfo();
/// raster a glyph as 8bit alpha to a memory buffer
/// update the GlyphInfo according to the raster strategy
/// @ remark buffer min size: glyphInfo.m_width * glyphInfo * height * sizeof(char)
bool bakeGlyphAlpha(CodePoint _codePoint, GlyphInfo& _outGlyphInfo, uint8_t* _outBuffer);
/// raster a glyph as 32bit subpixel rgba to a memory buffer
/// update the GlyphInfo according to the raster strategy
/// @ remark buffer min size: glyphInfo.m_width * glyphInfo * height * sizeof(uint32_t)
bool bakeGlyphSubpixel(CodePoint _codePoint, GlyphInfo& _outGlyphInfo, uint8_t* _outBuffer);
/// raster a glyph as 8bit signed distance to a memory buffer
/// update the GlyphInfo according to the raster strategy
/// @ remark buffer min size: glyphInfo.m_width * glyphInfo * height * sizeof(char)
bool bakeGlyphDistance(CodePoint _codePoint, GlyphInfo& _outGlyphInfo, uint8_t* _outBuffer);
private:
FTHolder* m_font;
};
TrueTypeFont::TrueTypeFont() : m_font(NULL)
{
}
TrueTypeFont::~TrueTypeFont()
{
if (NULL != m_font)
{
FT_Done_Face(m_font->face);
FT_Done_FreeType(m_font->library);
delete m_font;
m_font = NULL;
}
}
bool TrueTypeFont::init(const uint8_t* _buffer, uint32_t _bufferSize, int32_t _fontIndex, uint32_t _pixelHeight)
{
BX_CHECK(m_font == NULL, "TrueTypeFont already initialized");
BX_CHECK( (_bufferSize > 256 && _bufferSize < 100000000), "TrueType buffer size is suspicious");
BX_CHECK( (_pixelHeight > 4 && _pixelHeight < 128), "TrueType buffer size is suspicious");
FTHolder* holder = new FTHolder;
FT_Error error = FT_Init_FreeType(&holder->library);
BX_WARN(!error, "FT_Init_FreeType failed.");
if (error)
{
goto err0;
}
error = FT_New_Memory_Face(holder->library, _buffer, _bufferSize, _fontIndex, &holder->face);
BX_WARN(!error, "FT_Init_FreeType failed.");
if (error)
{
if (FT_Err_Unknown_File_Format == error)
{
goto err0;
}
goto err1;
}
error = FT_Select_Charmap(holder->face, FT_ENCODING_UNICODE);
BX_WARN(!error, "FT_Init_FreeType failed.");
if (error)
{
goto err2;
}
error = FT_Set_Pixel_Sizes(holder->face, 0, _pixelHeight);
BX_WARN(!error, "FT_Init_FreeType failed.");
if (error)
{
goto err2;
}
m_font = holder;
return true;
err2:
FT_Done_Face(holder->face);
err1:
FT_Done_FreeType(holder->library);
err0:
delete holder;
return false;
}
FontInfo TrueTypeFont::getFontInfo()
{
BX_CHECK(m_font != NULL, "TrueTypeFont not initialized");
BX_CHECK(FT_IS_SCALABLE(m_font->face), "Font is unscalable");
FT_Size_Metrics metrics = m_font->face->size->metrics;
FontInfo outFontInfo;
outFontInfo.scale = 1.0f;
outFontInfo.ascender = metrics.ascender / 64.0f;
outFontInfo.descender = metrics.descender / 64.0f;
outFontInfo.lineGap = (metrics.height - metrics.ascender + metrics.descender) / 64.0f;
outFontInfo.maxAdvanceWidth = metrics.max_advance/ 64.0f;
outFontInfo.underlinePosition = FT_MulFix(m_font->face->underline_position, metrics.y_scale) / 64.0f;
outFontInfo.underlineThickness = FT_MulFix(m_font->face->underline_thickness, metrics.y_scale) / 64.0f;
return outFontInfo;
}
static void glyphInfoInit(GlyphInfo& _glyphInfo, FT_BitmapGlyph _bitmap, FT_GlyphSlot _slot, uint8_t* _dst, uint32_t _bpp)
{
int32_t xx = _bitmap->left;
int32_t yy = -_bitmap->top;
int32_t ww = _bitmap->bitmap.width;
int32_t hh = _bitmap->bitmap.rows;
_glyphInfo.offset_x = (float)xx;
_glyphInfo.offset_y = (float)yy;
_glyphInfo.width = (float)ww;
_glyphInfo.height = (float)hh;
_glyphInfo.advance_x = (float)_slot->advance.x / 64.0f;
_glyphInfo.advance_y = (float)_slot->advance.y / 64.0f;
uint32_t dstPitch = ww * _bpp;
uint8_t* src = _bitmap->bitmap.buffer;
uint32_t srcPitch = _bitmap->bitmap.pitch;
for (int32_t ii = 0; ii < hh; ++ii)
{
memcpy(_dst, src, dstPitch);
_dst += dstPitch;
src += srcPitch;
}
}
bool TrueTypeFont::bakeGlyphAlpha(CodePoint _codePoint, GlyphInfo& _glyphInfo, uint8_t* _outBuffer)
{
BX_CHECK(m_font != NULL, "TrueTypeFont not initialized");
_glyphInfo.glyphIndex = FT_Get_Char_Index(m_font->face, _codePoint);
FT_GlyphSlot slot = m_font->face->glyph;
FT_Error error = FT_Load_Glyph(m_font->face, _glyphInfo.glyphIndex, FT_LOAD_DEFAULT);
if (error)
{
return false;
}
FT_Glyph glyph;
error = FT_Get_Glyph(slot, &glyph);
if (error)
{
return false;
}
error = FT_Glyph_To_Bitmap(&glyph, FT_RENDER_MODE_NORMAL, 0, 1);
if (error)
{
return false;
}
FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyph;
glyphInfoInit(_glyphInfo, bitmap, slot, _outBuffer, 1);
FT_Done_Glyph(glyph);
return true;
}
bool TrueTypeFont::bakeGlyphSubpixel(CodePoint _codePoint, GlyphInfo& _glyphInfo, uint8_t* _outBuffer)
{
BX_CHECK(m_font != NULL, "TrueTypeFont not initialized");
_glyphInfo.glyphIndex = FT_Get_Char_Index(m_font->face, _codePoint);
FT_GlyphSlot slot = m_font->face->glyph;
FT_Error error = FT_Load_Glyph(m_font->face, _glyphInfo.glyphIndex, FT_LOAD_DEFAULT);
if (error)
{
return false;
}
FT_Glyph glyph;
error = FT_Get_Glyph(slot, &glyph);
if (error)
{
return false;
}
error = FT_Glyph_To_Bitmap(&glyph, FT_RENDER_MODE_LCD, 0, 1);
if (error)
{
return false;
}
FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyph;
glyphInfoInit(_glyphInfo, bitmap, slot, _outBuffer, 3);
FT_Done_Glyph(glyph);
return true;
}
static void makeDistanceMap(const uint8_t* _img, uint8_t* _outImg, uint32_t _width, uint32_t _height)
{
#if USE_EDTAA3
int16_t* xdist = (int16_t*)malloc(_width * _height * sizeof(int16_t) );
int16_t* ydist = (int16_t*)malloc(_width * _height * sizeof(int16_t) );
double* gx = (double*)calloc(_width * _height, sizeof(double) );
double* gy = (double*)calloc(_width * _height, sizeof(double) );
double* data = (double*)calloc(_width * _height, sizeof(double) );
double* outside = (double*)calloc(_width * _height, sizeof(double) );
double* inside = (double*)calloc(_width * _height, sizeof(double) );
uint32_t ii;
// Convert img into double (data)
double img_min = 255, img_max = -255;
for (ii = 0; ii < _width * _height; ++ii)
{
double v = _img[ii];
data[ii] = v;
if (v > img_max)
{
img_max = v;
}
if (v < img_min)
{
img_min = v;
}
}
// Rescale image levels between 0 and 1
for (ii = 0; ii < _width * _height; ++ii)
{
data[ii] = (_img[ii] - img_min) / (img_max - img_min);
}
// Compute outside = edtaa3(bitmap); % Transform background (0's)
computegradient(data, _width, _height, gx, gy);
edtaa3(data, gx, gy, _width, _height, xdist, ydist, outside);
for (ii = 0; ii < _width * _height; ++ii)
{
if (outside[ii] < 0)
{
outside[ii] = 0.0;
}
}
// Compute inside = edtaa3(1-bitmap); % Transform foreground (1's)
memset(gx, 0, sizeof(double) * _width * _height);
memset(gy, 0, sizeof(double) * _width * _height);
for (ii = 0; ii < _width * _height; ++ii)
{
data[ii] = 1.0 - data[ii];
}
computegradient(data, _width, _height, gx, gy);
edtaa3(data, gx, gy, _width, _height, xdist, ydist, inside);
for (ii = 0; ii < _width * _height; ++ii)
{
if (inside[ii] < 0)
{
inside[ii] = 0.0;
}
}
// distmap = outside - inside; % Bipolar distance field
uint8_t* out = _outImg;
for (ii = 0; ii < _width * _height; ++ii)
{
outside[ii] -= inside[ii];
outside[ii] = 128 + outside[ii] * 16;
if (outside[ii] < 0)
{
outside[ii] = 0;
}
if (outside[ii] > 255)
{
outside[ii] = 255;
}
out[ii] = 255 - (uint8_t) outside[ii];
}
free(xdist);
free(ydist);
free(gx);
free(gy);
free(data);
free(outside);
free(inside);
#else
sdfBuild(_outImg, _width, 8.0f, _img, _width, _height, _width);
#endif // USE_EDTAA3
}
bool TrueTypeFont::bakeGlyphDistance(CodePoint _codePoint, GlyphInfo& _glyphInfo, uint8_t* _outBuffer)
{
BX_CHECK(m_font != NULL, "TrueTypeFont not initialized");
_glyphInfo.glyphIndex = FT_Get_Char_Index(m_font->face, _codePoint);
FT_Int32 loadMode = FT_LOAD_DEFAULT | FT_LOAD_NO_HINTING;
FT_Render_Mode renderMode = FT_RENDER_MODE_NORMAL;
FT_GlyphSlot slot = m_font->face->glyph;
FT_Error error = FT_Load_Glyph(m_font->face, _glyphInfo.glyphIndex, loadMode);
if (error)
{
return false;
}
FT_Glyph glyph;
error = FT_Get_Glyph(slot, &glyph);
if (error)
{
return false;
}
error = FT_Glyph_To_Bitmap(&glyph, renderMode, 0, 1);
if (error)
{
return false;
}
FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyph;
int32_t ww = bitmap->bitmap.width;
int32_t hh = bitmap->bitmap.rows;
glyphInfoInit(_glyphInfo, bitmap, slot, _outBuffer, 1);
FT_Done_Glyph(glyph);
if (ww * hh > 0)
{
uint32_t dw = 6;
uint32_t dh = 6;
uint32_t nw = ww + dw * 2;
uint32_t nh = hh + dh * 2;
BX_CHECK(nw * nh < 128 * 128, "Buffer overflow (size %d)", nw * nh);
uint32_t buffSize = nw * nh * sizeof(uint8_t);
uint8_t* alphaImg = (uint8_t*)malloc(buffSize);
memset(alphaImg, 0, nw * nh * sizeof(uint8_t) );
//copy the original buffer to the temp one
for (uint32_t ii = dh; ii < nh - dh; ++ii)
{
memcpy(alphaImg + ii * nw + dw, _outBuffer + (ii - dh) * ww, ww);
}
makeDistanceMap(alphaImg, _outBuffer, nw, nh);
free(alphaImg);
_glyphInfo.offset_x -= (float)dw;
_glyphInfo.offset_y -= (float)dh;
_glyphInfo.width = (float)nw;
_glyphInfo.height = (float)nh;
}
return true;
}
typedef stl::unordered_map<CodePoint, GlyphInfo> GlyphHashMap;
// cache font data
struct FontManager::CachedFont
{
CachedFont()
: trueTypeFont(NULL)
{
masterFontHandle.idx = bx::HandleAlloc::invalid;
}
FontInfo fontInfo;
GlyphHashMap cachedGlyphs;
TrueTypeFont* trueTypeFont;
// an handle to a master font in case of sub distance field font
FontHandle masterFontHandle;
int16_t padding;
};
#define MAX_FONT_BUFFER_SIZE (512 * 512 * 4)
FontManager::FontManager(Atlas* _atlas)
: m_ownAtlas(false)
, m_atlas(_atlas)
{
init();
}
FontManager::FontManager(uint16_t _textureSideWidth)
: m_ownAtlas(true)
, m_atlas(new Atlas(_textureSideWidth) )
{
init();
}
void FontManager::init()
{
m_cachedFiles = new CachedFile[MAX_OPENED_FILES];
m_cachedFonts = new CachedFont[MAX_OPENED_FONT];
m_buffer = new uint8_t[MAX_FONT_BUFFER_SIZE];
const uint32_t W = 3;
// Create filler rectangle
uint8_t buffer[W * W * 4];
memset(buffer, 255, W * W * 4);
m_blackGlyph.width = W;
m_blackGlyph.height = W;
///make sure the black glyph doesn't bleed by using a one pixel inner outline
m_blackGlyph.regionIndex = m_atlas->addRegion(W, W, buffer, AtlasRegion::TYPE_GRAY, 1);
}
FontManager::~FontManager()
{
BX_CHECK(m_fontHandles.getNumHandles() == 0, "All the fonts must be destroyed before destroying the manager");
delete [] m_cachedFonts;
BX_CHECK(m_filesHandles.getNumHandles() == 0, "All the font files must be destroyed before destroying the manager");
delete [] m_cachedFiles;
delete [] m_buffer;
if (m_ownAtlas)
{
delete m_atlas;
}
}
TrueTypeHandle FontManager::createTtf(const uint8_t* _buffer, uint32_t _size)
{
uint16_t id = m_filesHandles.alloc();
BX_CHECK(id != bx::HandleAlloc::invalid, "Invalid handle used");
m_cachedFiles[id].buffer = new uint8_t[_size];
m_cachedFiles[id].bufferSize = _size;
memcpy(m_cachedFiles[id].buffer, _buffer, _size);
TrueTypeHandle ret = { id };
return ret;
}
void FontManager::destroyTtf(TrueTypeHandle _handle)
{
BX_CHECK(bgfx::isValid(_handle), "Invalid handle used");
delete m_cachedFiles[_handle.idx].buffer;
m_cachedFiles[_handle.idx].bufferSize = 0;
m_cachedFiles[_handle.idx].buffer = NULL;
m_filesHandles.free(_handle.idx);
}
FontHandle FontManager::createFontByPixelSize(TrueTypeHandle _ttfHandle, uint32_t _typefaceIndex, uint32_t _pixelSize, uint32_t _fontType)
{
BX_CHECK(bgfx::isValid(_ttfHandle), "Invalid handle used");
TrueTypeFont* ttf = new TrueTypeFont();
if (!ttf->init(m_cachedFiles[_ttfHandle.idx].buffer, m_cachedFiles[_ttfHandle.idx].bufferSize, _typefaceIndex, _pixelSize) )
{
delete ttf;
FontHandle invalid = { bx::HandleAlloc::invalid };
return invalid;
}
uint16_t fontIdx = m_fontHandles.alloc();
BX_CHECK(fontIdx != bx::HandleAlloc::invalid, "Invalid handle used");
CachedFont& font = m_cachedFonts[fontIdx];
font.trueTypeFont = ttf;
font.fontInfo = ttf->getFontInfo();
font.fontInfo.fontType = int16_t(_fontType);
font.fontInfo.pixelSize = uint16_t(_pixelSize);
font.cachedGlyphs.clear();
font.masterFontHandle.idx = bx::HandleAlloc::invalid;
FontHandle handle = { fontIdx };
return handle;
}
FontHandle FontManager::createScaledFontToPixelSize(FontHandle _baseFontHandle, uint32_t _pixelSize)
{
BX_CHECK(bgfx::isValid(_baseFontHandle), "Invalid handle used");
CachedFont& baseFont = m_cachedFonts[_baseFontHandle.idx];
FontInfo& fontInfo = baseFont.fontInfo;
FontInfo newFontInfo = fontInfo;
newFontInfo.pixelSize = uint16_t(_pixelSize);
newFontInfo.scale = (float)_pixelSize / (float) fontInfo.pixelSize;
newFontInfo.ascender = (newFontInfo.ascender * newFontInfo.scale);
newFontInfo.descender = (newFontInfo.descender * newFontInfo.scale);
newFontInfo.lineGap = (newFontInfo.lineGap * newFontInfo.scale);
newFontInfo.maxAdvanceWidth = (newFontInfo.maxAdvanceWidth * newFontInfo.scale);
newFontInfo.underlineThickness = (newFontInfo.underlineThickness * newFontInfo.scale);
newFontInfo.underlinePosition = (newFontInfo.underlinePosition * newFontInfo.scale);
uint16_t fontIdx = m_fontHandles.alloc();
BX_CHECK(fontIdx != bx::HandleAlloc::invalid, "Invalid handle used");
CachedFont& font = m_cachedFonts[fontIdx];
font.cachedGlyphs.clear();
font.fontInfo = newFontInfo;
font.trueTypeFont = NULL;
font.masterFontHandle = _baseFontHandle;
FontHandle handle = { fontIdx };
return handle;
}
void FontManager::destroyFont(FontHandle _handle)
{
BX_CHECK(bgfx::isValid(_handle), "Invalid handle used");
CachedFont& font = m_cachedFonts[_handle.idx];
if (font.trueTypeFont != NULL)
{
delete font.trueTypeFont;
font.trueTypeFont = NULL;
}
font.cachedGlyphs.clear();
m_fontHandles.free(_handle.idx);
}
bool FontManager::preloadGlyph(FontHandle _handle, const wchar_t* _string)
{
BX_CHECK(bgfx::isValid(_handle), "Invalid handle used");
CachedFont& font = m_cachedFonts[_handle.idx];
if (NULL == font.trueTypeFont)
{
return false;
}
for (uint32_t ii = 0, end = (uint32_t)wcslen(_string); ii < end; ++ii)
{
CodePoint codePoint = _string[ii];
if (!preloadGlyph(_handle, codePoint) )
{
return false;
}
}
return true;
}
bool FontManager::preloadGlyph(FontHandle _handle, CodePoint _codePoint)
{
BX_CHECK(bgfx::isValid(_handle), "Invalid handle used");
CachedFont& font = m_cachedFonts[_handle.idx];
FontInfo& fontInfo = font.fontInfo;
GlyphHashMap::iterator iter = font.cachedGlyphs.find(_codePoint);
if (iter != font.cachedGlyphs.end() )
{
return true;
}
if (NULL != font.trueTypeFont)
{
GlyphInfo glyphInfo;
switch (font.fontInfo.fontType)
{
case FONT_TYPE_ALPHA:
font.trueTypeFont->bakeGlyphAlpha(_codePoint, glyphInfo, m_buffer);
break;
case FONT_TYPE_DISTANCE:
font.trueTypeFont->bakeGlyphDistance(_codePoint, glyphInfo, m_buffer);
break;
case FONT_TYPE_DISTANCE_SUBPIXEL:
font.trueTypeFont->bakeGlyphDistance(_codePoint, glyphInfo, m_buffer);
break;
default:
BX_CHECK(false, "TextureType not supported yet");
}
if (!addBitmap(glyphInfo, m_buffer) )
{
return false;
}
glyphInfo.advance_x = (glyphInfo.advance_x * fontInfo.scale);
glyphInfo.advance_y = (glyphInfo.advance_y * fontInfo.scale);
glyphInfo.offset_x = (glyphInfo.offset_x * fontInfo.scale);
glyphInfo.offset_y = (glyphInfo.offset_y * fontInfo.scale);
glyphInfo.height = (glyphInfo.height * fontInfo.scale);
glyphInfo.width = (glyphInfo.width * fontInfo.scale);
font.cachedGlyphs[_codePoint] = glyphInfo;
return true;
}
if (isValid(font.masterFontHandle)
&& preloadGlyph(font.masterFontHandle, _codePoint) )
{
const GlyphInfo* glyph = getGlyphInfo(font.masterFontHandle, _codePoint);
GlyphInfo glyphInfo = *glyph;
glyphInfo.advance_x = (glyphInfo.advance_x * fontInfo.scale);
glyphInfo.advance_y = (glyphInfo.advance_y * fontInfo.scale);
glyphInfo.offset_x = (glyphInfo.offset_x * fontInfo.scale);
glyphInfo.offset_y = (glyphInfo.offset_y * fontInfo.scale);
glyphInfo.height = (glyphInfo.height * fontInfo.scale);
glyphInfo.width = (glyphInfo.width * fontInfo.scale);
font.cachedGlyphs[_codePoint] = glyphInfo;
return true;
}
return false;
}
const FontInfo& FontManager::getFontInfo(FontHandle _handle) const
{
BX_CHECK(bgfx::isValid(_handle), "Invalid handle used");
return m_cachedFonts[_handle.idx].fontInfo;
}
const GlyphInfo* FontManager::getGlyphInfo(FontHandle _handle, CodePoint _codePoint)
{
const GlyphHashMap& cachedGlyphs = m_cachedFonts[_handle.idx].cachedGlyphs;
GlyphHashMap::const_iterator it = cachedGlyphs.find(_codePoint);
if (it == cachedGlyphs.end() )
{
if (!preloadGlyph(_handle, _codePoint) )
{
return NULL;
}
it = cachedGlyphs.find(_codePoint);
}
BX_CHECK(it != cachedGlyphs.end(), "Failed to preload glyph.");
return &it->second;
}
bool FontManager::addBitmap(GlyphInfo& _glyphInfo, const uint8_t* _data)
{
_glyphInfo.regionIndex = m_atlas->addRegion( (uint16_t) ceil(_glyphInfo.width), (uint16_t) ceil(_glyphInfo.height), _data, AtlasRegion::TYPE_GRAY);
return true;
}
|