forked from RetroPie/EmulationStation
/
Font.cpp
853 lines (690 loc) · 21.5 KB
/
Font.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
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
#include "resources/Font.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <boost/filesystem.hpp>
#include "Renderer.h"
#include "Log.h"
#include "Util.h"
FT_Library Font::sLibrary = NULL;
int Font::getSize() const { return mSize; }
std::map< std::pair<std::string, int>, std::weak_ptr<Font> > Font::sFontMap;
// utf8 stuff
size_t Font::getNextCursor(const std::string& str, size_t cursor)
{
// compare to character at the cursor
const char& c = str[cursor];
size_t result = cursor;
if((c & 0x80) == 0) // 0xxxxxxx, one byte character
{
result += 1;
}
else if((c & 0xE0) == 0xC0) // 110xxxxx, two bytes left in character
{
result += 2;
}
else if((c & 0xF0) == 0xE0) // 1110xxxx, three bytes left in character
{
result += 3;
}
else if((c & 0xF8) == 0xF0) // 11110xxx, four bytes left in character
{
result += 4;
}
else
{
// error, invalid utf8 string
// if this assert is tripped, the cursor is in the middle of a utf8 code point
assert((c & 0xC0) != 0x80); // character is 10xxxxxx
// if that wasn't it, something crazy happened
assert(false);
}
if(str.length() < result || result < cursor) // don't go beyond the very end of the string, try and catch overflow
return cursor;
return result;
}
// note: will happily accept malformed utf8
size_t Font::getPrevCursor(const std::string& str, size_t cursor)
{
if(cursor == 0)
return 0;
do
{
cursor--;
} while(cursor > 0 &&
(str[cursor] & 0xC0) == 0x80); // character is 10xxxxxx
return cursor;
}
size_t Font::moveCursor(const std::string& str, size_t cursor, int amt)
{
if(amt > 0)
{
for(int i = 0; i < amt; i++)
cursor = Font::getNextCursor(str, cursor);
}
else if(amt < 0)
{
for(int i = amt; i < 0; i++)
cursor = Font::getPrevCursor(str, cursor);
}
return cursor;
}
UnicodeChar Font::readUnicodeChar(const std::string& str, size_t& cursor)
{
const char& c = str[cursor];
if((c & 0x80) == 0) // 0xxxxxxx, one byte character
{
// 0xxxxxxx
cursor++;
return (UnicodeChar)c;
}
else if((c & 0xE0) == 0xC0) // 110xxxxx, two bytes left in character
{
// 110xxxxx 10xxxxxx
UnicodeChar val = ((str[cursor] & 0x1F) << 6) |
(str[cursor + 1] & 0x3F);
cursor += 2;
return val;
}
else if((c & 0xF0) == 0xE0) // 1110xxxx, three bytes left in character
{
// 1110xxxx 10xxxxxx 10xxxxxx
UnicodeChar val = ((str[cursor] & 0x0F) << 12) |
((str[cursor + 1] & 0x3F) << 6) |
(str[cursor + 2] & 0x3F);
cursor += 3;
return val;
}
else if((c & 0xF8) == 0xF0) // 11110xxx, four bytes left in character
{
// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
UnicodeChar val = ((str[cursor] & 0x07) << 18) |
((str[cursor + 1] & 0x3F) << 12) |
((str[cursor + 2] & 0x3F) << 6) |
(str[cursor + 3] & 0x3F);
cursor += 4;
return val;
}
else
{
// error, invalid utf8 string
// if this assert is tripped, the cursor is in the middle of a utf8 code point
assert((c & 0xC0) != 0x80); // character is 10xxxxxx
// if that wasn't it, something crazy happened
assert(false);
}
// error
return 0;
}
Font::FontFace::FontFace(ResourceData&& d, int size) : data(d)
{
int err = FT_New_Memory_Face(sLibrary, data.ptr.get(), data.length, 0, &face);
assert(!err);
if(!err)
FT_Set_Pixel_Sizes(face, 0, size);
}
Font::FontFace::~FontFace()
{
if(face)
FT_Done_Face(face);
}
void Font::initLibrary()
{
assert(sLibrary == NULL);
if(FT_Init_FreeType(&sLibrary))
{
sLibrary = NULL;
LOG(LogError) << "Error initializing FreeType!";
}
}
size_t Font::getMemUsage() const
{
size_t memUsage = 0;
for(auto it = mTextures.begin(); it != mTextures.end(); it++)
memUsage += it->textureSize.x() * it->textureSize.y() * 4;
for(auto it = mFaceCache.begin(); it != mFaceCache.end(); it++)
memUsage += it->second->data.length;
return memUsage;
}
size_t Font::getTotalMemUsage()
{
size_t total = 0;
auto it = sFontMap.begin();
while(it != sFontMap.end())
{
if(it->second.expired())
{
it = sFontMap.erase(it);
continue;
}
total += it->second.lock()->getMemUsage();
it++;
}
return total;
}
Font::Font(int size, const std::string& path) : mSize(size), mPath(path)
{
assert(mSize > 0);
mMaxGlyphHeight = 0;
if(!sLibrary)
initLibrary();
// always initialize ASCII characters
for(UnicodeChar i = 32; i < 128; i++)
getGlyph(i);
clearFaceCache();
}
Font::~Font()
{
unload(ResourceManager::getInstance());
}
void Font::reload(std::shared_ptr<ResourceManager>& rm)
{
rebuildTextures();
}
void Font::unload(std::shared_ptr<ResourceManager>& rm)
{
unloadTextures();
}
std::shared_ptr<Font> Font::get(int size, const std::string& path)
{
const std::string canonicalPath = getCanonicalPath(path);
std::pair<std::string, int> def(canonicalPath.empty() ? getDefaultPath() : canonicalPath, size);
auto foundFont = sFontMap.find(def);
if(foundFont != sFontMap.end())
{
if(!foundFont->second.expired())
return foundFont->second.lock();
}
std::shared_ptr<Font> font = std::shared_ptr<Font>(new Font(def.second, def.first));
sFontMap[def] = std::weak_ptr<Font>(font);
ResourceManager::getInstance()->addReloadable(font);
return font;
}
void Font::unloadTextures()
{
for(auto it = mTextures.begin(); it != mTextures.end(); it++)
{
it->deinitTexture();
}
}
Font::FontTexture::FontTexture()
{
textureId = 0;
textureSize = Vector2i(2048, 512);
writePos = Vector2i::Zero();
rowHeight = 0;
}
Font::FontTexture::~FontTexture()
{
deinitTexture();
}
bool Font::FontTexture::findEmpty(const Vector2i& size, Vector2i& cursor_out)
{
if(size.x() >= textureSize.x() || size.y() >= textureSize.y())
return false;
if(writePos.x() + size.x() >= textureSize.x() &&
writePos.y() + rowHeight + size.y() + 1 < textureSize.y())
{
// row full, but it should fit on the next row
// move cursor to next row
writePos = Vector2i(0, writePos.y() + rowHeight + 1); // leave 1px of space between glyphs
rowHeight = 0;
}
if(writePos.x() + size.x() >= textureSize.x() ||
writePos.y() + size.y() >= textureSize.y())
{
// nope, still won't fit
return false;
}
cursor_out = writePos;
writePos[0] += size.x() + 1; // leave 1px of space between glyphs
if(size.y() > rowHeight)
rowHeight = size.y();
return true;
}
void Font::FontTexture::initTexture()
{
assert(textureId == 0);
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, textureSize.x(), textureSize.y(), 0, GL_ALPHA, GL_UNSIGNED_BYTE, NULL);
}
void Font::FontTexture::deinitTexture()
{
if(textureId != 0)
{
glDeleteTextures(1, &textureId);
textureId = 0;
}
}
void Font::getTextureForNewGlyph(const Vector2i& glyphSize, FontTexture*& tex_out, Vector2i& cursor_out)
{
if(mTextures.size())
{
// check if the most recent texture has space
tex_out = &mTextures.back();
// will this one work?
if(tex_out->findEmpty(glyphSize, cursor_out))
return; // yes
}
// current textures are full,
// make a new one
mTextures.push_back(FontTexture());
tex_out = &mTextures.back();
tex_out->initTexture();
bool ok = tex_out->findEmpty(glyphSize, cursor_out);
if(!ok)
{
LOG(LogError) << "Glyph too big to fit on a new texture (glyph size > " << tex_out->textureSize.x() << ", " << tex_out->textureSize.y() << ")!";
tex_out = NULL;
}
}
std::vector<std::string> getFallbackFontPaths()
{
#ifdef WIN32
// Windows
// get this system's equivalent of "C:\Windows" (might be on a different drive or in a different folder)
// so we can check the Fonts subdirectory for fallback fonts
TCHAR winDir[MAX_PATH];
GetWindowsDirectory(winDir, MAX_PATH);
std::string fontDir = winDir;
fontDir += "\\Fonts\\";
const char* fontNames[] = {
"meiryo.ttc", // japanese
"simhei.ttf", // chinese
"arial.ttf" // latin
};
//prepend to font file names
std::vector<std::string> fontPaths;
fontPaths.reserve(sizeof(fontNames) / sizeof(fontNames[0]));
for(unsigned int i = 0; i < sizeof(fontNames) / sizeof(fontNames[0]); i++)
{
std::string path = fontDir + fontNames[i];
if(ResourceManager::getInstance()->fileExists(path))
fontPaths.push_back(path);
}
fontPaths.shrink_to_fit();
return fontPaths;
#else
// Linux
// TODO
const char* paths[] = {
"/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf" // japanese, chinese, present on Debian
};
std::vector<std::string> fontPaths;
for(unsigned int i = 0; i < sizeof(paths) / sizeof(paths[0]); i++)
{
if(ResourceManager::getInstance()->fileExists(paths[i]))
fontPaths.push_back(paths[i]);
}
fontPaths.shrink_to_fit();
return fontPaths;
#endif
}
FT_Face Font::getFaceForChar(UnicodeChar id)
{
static const std::vector<std::string> fallbackFonts = getFallbackFontPaths();
// look through our current font + fallback fonts to see if any have the glyph we're looking for
for(unsigned int i = 0; i < fallbackFonts.size() + 1; i++)
{
auto fit = mFaceCache.find(i);
if(fit == mFaceCache.end()) // doesn't exist yet
{
// i == 0 -> mPath
// otherwise, take from fallbackFonts
const std::string& path = (i == 0 ? mPath : fallbackFonts.at(i - 1));
ResourceData data = ResourceManager::getInstance()->getFileData(path);
mFaceCache[i] = std::unique_ptr<FontFace>(new FontFace(std::move(data), mSize));
fit = mFaceCache.find(i);
}
if(FT_Get_Char_Index(fit->second->face, id) != 0)
return fit->second->face;
}
// nothing has a valid glyph - return the "real" face so we get a "missing" character
return mFaceCache.begin()->second->face;
}
void Font::clearFaceCache()
{
mFaceCache.clear();
}
Font::Glyph* Font::getGlyph(UnicodeChar id)
{
// is it already loaded?
auto it = mGlyphMap.find(id);
if(it != mGlyphMap.end())
return &it->second;
// nope, need to make a glyph
FT_Face face = getFaceForChar(id);
if(!face)
{
LOG(LogError) << "Could not find appropriate font face for character " << id << " for font " << mPath;
return NULL;
}
FT_GlyphSlot g = face->glyph;
if(FT_Load_Char(face, id, FT_LOAD_RENDER))
{
LOG(LogError) << "Could not find glyph for character " << id << " for font " << mPath << ", size " << mSize << "!";
return NULL;
}
Vector2i glyphSize(g->bitmap.width, g->bitmap.rows);
FontTexture* tex = NULL;
Vector2i cursor;
getTextureForNewGlyph(glyphSize, tex, cursor);
// getTextureForNewGlyph can fail if the glyph is bigger than the max texture size (absurdly large font size)
if(tex == NULL)
{
LOG(LogError) << "Could not create glyph for character " << id << " for font " << mPath << ", size " << mSize << " (no suitable texture found)!";
return NULL;
}
// create glyph
Glyph& glyph = mGlyphMap[id];
glyph.texture = tex;
glyph.texPos = Vector2f(cursor.x() / (float)tex->textureSize.x(), cursor.y() / (float)tex->textureSize.y());
glyph.texSize = Vector2f(glyphSize.x() / (float)tex->textureSize.x(), glyphSize.y() / (float)tex->textureSize.y());
glyph.advance = Vector2f((float)g->metrics.horiAdvance / 64.0f, (float)g->metrics.vertAdvance / 64.0f);
glyph.bearing = Vector2f((float)g->metrics.horiBearingX / 64.0f, (float)g->metrics.horiBearingY / 64.0f);
// upload glyph bitmap to texture
glBindTexture(GL_TEXTURE_2D, tex->textureId);
glTexSubImage2D(GL_TEXTURE_2D, 0, cursor.x(), cursor.y(), glyphSize.x(), glyphSize.y(), GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap.buffer);
glBindTexture(GL_TEXTURE_2D, 0);
// update max glyph height
if(glyphSize.y() > mMaxGlyphHeight)
mMaxGlyphHeight = glyphSize.y();
// done
return &glyph;
}
// completely recreate the texture data for all textures based on mGlyphs information
void Font::rebuildTextures()
{
// recreate OpenGL textures
for(auto it = mTextures.begin(); it != mTextures.end(); it++)
{
it->initTexture();
}
// reupload the texture data
for(auto it = mGlyphMap.begin(); it != mGlyphMap.end(); it++)
{
FT_Face face = getFaceForChar(it->first);
FT_GlyphSlot glyphSlot = face->glyph;
// load the glyph bitmap through FT
FT_Load_Char(face, it->first, FT_LOAD_RENDER);
FontTexture* tex = it->second.texture;
// find the position/size
Vector2i cursor(it->second.texPos.x() * tex->textureSize.x(), it->second.texPos.y() * tex->textureSize.y());
Vector2i glyphSize(it->second.texSize.x() * tex->textureSize.x(), it->second.texSize.y() * tex->textureSize.y());
// upload to texture
glBindTexture(GL_TEXTURE_2D, tex->textureId);
glTexSubImage2D(GL_TEXTURE_2D, 0, cursor.x(), cursor.y(), glyphSize.x(), glyphSize.y(), GL_ALPHA, GL_UNSIGNED_BYTE, glyphSlot->bitmap.buffer);
}
glBindTexture(GL_TEXTURE_2D, 0);
}
void Font::renderTextCache(TextCache* cache)
{
if(cache == NULL)
{
LOG(LogError) << "Attempted to draw NULL TextCache!";
return;
}
for(auto it = cache->vertexLists.begin(); it != cache->vertexLists.end(); it++)
{
assert(*it->textureIdPtr != 0);
auto vertexList = *it;
glBindTexture(GL_TEXTURE_2D, *it->textureIdPtr);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(2, GL_FLOAT, sizeof(TextCache::Vertex), &it->verts[0].pos);
glTexCoordPointer(2, GL_FLOAT, sizeof(TextCache::Vertex), &it->verts[0].tex);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, it->colors.data());
glDrawArrays(GL_TRIANGLES, 0, it->verts.size());
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
}
}
Vector2f Font::sizeText(std::string text, float lineSpacing)
{
float lineWidth = 0.0f;
float highestWidth = 0.0f;
const float lineHeight = getHeight(lineSpacing);
float y = lineHeight;
size_t i = 0;
while(i < text.length())
{
UnicodeChar character = readUnicodeChar(text, i); // advances i
if(character == (UnicodeChar)'\n')
{
if(lineWidth > highestWidth)
highestWidth = lineWidth;
lineWidth = 0.0f;
y += lineHeight;
}
Glyph* glyph = getGlyph(character);
if(glyph)
lineWidth += glyph->advance.x();
}
if(lineWidth > highestWidth)
highestWidth = lineWidth;
return Vector2f(highestWidth, y);
}
float Font::getHeight(float lineSpacing) const
{
return mMaxGlyphHeight * lineSpacing;
}
float Font::getLetterHeight()
{
Glyph* glyph = getGlyph((UnicodeChar)'S');
assert(glyph);
return glyph->texSize.y() * glyph->texture->textureSize.y();
}
// Naive way to find whitespace characters, only includes the common ones in the ASCII range!
bool Font::isWhiteSpace(UnicodeChar c)
{
return (c == (UnicodeChar) ' ' ||
c == (UnicodeChar) '\n' ||
c == (UnicodeChar) '\t' ||
c == (UnicodeChar) '\v' ||
c == (UnicodeChar) '\f' ||
c == (UnicodeChar) '\r');
}
// Breaks up a normal string with newlines to make it fit width (in pixels)
std::string Font::wrapText(std::string text, float maxWidth)
{
std::string out = "";
while (text.length() > 0) // find next cut-point
{
size_t cursor = 0;
float lineWidth = 0.0f;
size_t lastWhiteSpace = 0;
while ((lineWidth < maxWidth) &&
(cursor < text.length()))
{
UnicodeChar c = readUnicodeChar(text, cursor); // also advances cursor!
lineWidth = sizeText(text.substr(0, cursor)).x(); // this takes into account \n already present!
if (isWhiteSpace(c))
{
lastWhiteSpace = cursor;
}
}
if (cursor == text.length()) // arrived at end of text.
{
out += text;
text.erase();
}
else // need to cut at last whitespace or lacking that, the previous cursor.
{
size_t cut = (lastWhiteSpace != 0) ? lastWhiteSpace : getPrevCursor(text, cursor);
out += text.substr(0, cut) + "\n";
text.erase(0, cut);
}
}
return out;
}
Vector2f Font::sizeWrappedText(std::string text, float xLen, float lineSpacing)
{
text = wrapText(text, xLen);
return sizeText(text, lineSpacing);
}
Vector2f Font::getWrappedTextCursorOffset(std::string text, float xLen, size_t stop, float lineSpacing)
{
std::string wrappedText = wrapText(text, xLen);
float lineWidth = 0.0f;
float y = 0.0f;
size_t wrapCursor = 0;
size_t cursor = 0;
while(cursor < stop)
{
UnicodeChar wrappedCharacter = readUnicodeChar(wrappedText, wrapCursor);
UnicodeChar character = readUnicodeChar(text, cursor);
if(wrappedCharacter == (UnicodeChar)'\n' && character != (UnicodeChar)'\n')
{
//this is where the wordwrap inserted a newline
//reset lineWidth and increment y, but don't consume a cursor character
lineWidth = 0.0f;
y += getHeight(lineSpacing);
cursor = getPrevCursor(text, cursor); // unconsume
continue;
}
if(character == (UnicodeChar)'\n')
{
lineWidth = 0.0f;
y += getHeight(lineSpacing);
continue;
}
Glyph* glyph = getGlyph(character);
if(glyph)
lineWidth += glyph->advance.x();
}
return Vector2f(lineWidth, y);
}
//=============================================================================================================
//TextCache
//=============================================================================================================
float Font::getNewlineStartOffset(const std::string& text, const unsigned int& charStart, const float& xLen, const Alignment& alignment)
{
switch(alignment)
{
case ALIGN_LEFT:
return 0;
case ALIGN_CENTER:
{
unsigned int endChar = text.find('\n', charStart);
return (xLen - sizeText(text.substr(charStart, endChar != std::string::npos ? endChar - charStart : endChar)).x()) / 2.0f;
}
case ALIGN_RIGHT:
{
unsigned int endChar = text.find('\n', charStart);
return xLen - (sizeText(text.substr(charStart, endChar != std::string::npos ? endChar - charStart : endChar)).x());
}
default:
return 0;
}
}
inline float font_round(float v)
{
return round(v);
}
TextCache* Font::buildTextCache(const std::string& text, Vector2f offset, unsigned int color, float xLen, Alignment alignment, float lineSpacing)
{
float x = offset[0] + (xLen != 0 ? getNewlineStartOffset(text, 0, xLen, alignment) : 0);
float yTop = getGlyph((UnicodeChar)'S')->bearing.y();
float yBot = getHeight(lineSpacing);
float y = offset[1] + (yBot + yTop)/2.0f;
// vertices by texture
std::map< FontTexture*, std::vector<TextCache::Vertex> > vertMap;
size_t cursor = 0;
UnicodeChar character;
Glyph* glyph;
while(cursor < text.length())
{
character = readUnicodeChar(text, cursor); // also advances cursor
// invalid character
if(character == 0)
continue;
if(character == (UnicodeChar)'\n')
{
y += getHeight(lineSpacing);
x = offset[0] + (xLen != 0 ? getNewlineStartOffset(text, cursor /* cursor is already advanced */, xLen, alignment) : 0);
continue;
}
glyph = getGlyph(character);
if(glyph == NULL)
continue;
std::vector<TextCache::Vertex>& verts = vertMap[glyph->texture];
size_t oldVertSize = verts.size();
verts.resize(oldVertSize + 6);
TextCache::Vertex* tri = verts.data() + oldVertSize;
const float glyphStartX = x + glyph->bearing.x();
const Vector2i& textureSize = glyph->texture->textureSize;
// triangle 1
// round to fix some weird "cut off" text bugs
tri[0].pos = Vector2f(font_round(glyphStartX), font_round(y + (glyph->texSize.y() * textureSize.y() - glyph->bearing.y())));
tri[1].pos = Vector2f(font_round(glyphStartX + glyph->texSize.x() * textureSize.x()), font_round(y - glyph->bearing.y()));
tri[2].pos = Vector2f(tri[0].pos.x(), tri[1].pos.y());
//tri[0].tex = Vector2f(0, 0);
//tri[0].tex = Vector2f(1, 1);
//tri[0].tex = Vector2f(0, 1);
tri[0].tex = Vector2f(glyph->texPos.x(), glyph->texPos.y() + glyph->texSize.y());
tri[1].tex = Vector2f(glyph->texPos.x() + glyph->texSize.x(), glyph->texPos.y());
tri[2].tex = Vector2f(tri[0].tex.x(), tri[1].tex.y());
// triangle 2
tri[3].pos = tri[0].pos;
tri[4].pos = tri[1].pos;
tri[5].pos = Vector2f(tri[1].pos.x(), tri[0].pos.y());
tri[3].tex = tri[0].tex;
tri[4].tex = tri[1].tex;
tri[5].tex = Vector2f(tri[1].tex.x(), tri[0].tex.y());
// advance
x += glyph->advance.x();
}
//TextCache::CacheMetrics metrics = { sizeText(text, lineSpacing) };
TextCache* cache = new TextCache();
cache->vertexLists.resize(vertMap.size());
cache->metrics = { sizeText(text, lineSpacing) };
unsigned int i = 0;
for(auto it = vertMap.begin(); it != vertMap.end(); it++)
{
TextCache::VertexList& vertList = cache->vertexLists.at(i);
vertList.textureIdPtr = &it->first->textureId;
vertList.verts = it->second;
vertList.colors.resize(4 * it->second.size());
Renderer::buildGLColorArray(vertList.colors.data(), color, it->second.size());
}
clearFaceCache();
return cache;
}
TextCache* Font::buildTextCache(const std::string& text, float offsetX, float offsetY, unsigned int color)
{
return buildTextCache(text, Vector2f(offsetX, offsetY), color, 0.0f);
}
void TextCache::setColor(unsigned int color)
{
for(auto it = vertexLists.begin(); it != vertexLists.end(); it++)
Renderer::buildGLColorArray(it->colors.data(), color, it->verts.size());
}
std::shared_ptr<Font> Font::getFromTheme(const ThemeData::ThemeElement* elem, unsigned int properties, const std::shared_ptr<Font>& orig)
{
using namespace ThemeFlags;
if(!(properties & FONT_PATH) && !(properties & FONT_SIZE))
return orig;
std::shared_ptr<Font> font;
int size = (orig ? orig->mSize : FONT_SIZE_MEDIUM);
std::string path = (orig ? orig->mPath : getDefaultPath());
float sh = (float)Renderer::getScreenHeight();
if(properties & FONT_SIZE && elem->has("fontSize"))
size = (int)(sh * elem->get<float>("fontSize"));
if(properties & FONT_PATH && elem->has("fontPath"))
path = elem->get<std::string>("fontPath");
return get(size, path);
}