-
Notifications
You must be signed in to change notification settings - Fork 2
/
ft_font.cpp
779 lines (639 loc) · 22.5 KB
/
ft_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
/* --------------------------------------------------------------------
Extreme Tuxracer
Copyright (c) 2001-2004 Henry Maddocks (FTGL)
Copyright (C) 2010 Extreme Tuxracer Team (modification)
The FTGL library from H. Maddocks is published under the terms
of the Lesser General Publish License (LGPL). You can find a
copy of the LGPL on the gnu website:
http://www.gnu.org/copyleft/lesser.html
Hint: almost all comments are removed from the code to make it
shorter. So all modules could put together in a single module.
To read the comments of the author you should download the
original FTGL library. Most functions are the same as in this
module.
--------------------------------------------------------------------- */
#include "ft_font.h"
// --------------------------------------------------------------------
// FTFont
// --------------------------------------------------------------------
FTFont::FTFont (const char* fontFilePath)
: face (fontFilePath), glyphList(0) {
err = face.Error();
if (err == 0) glyphList = new FTGlyphContainer (&face);
}
FTFont::FTFont (const unsigned char *pBufferBytes, size_t bufferSizeInBytes)
: face (pBufferBytes, bufferSizeInBytes), glyphList(0) {
err = face.Error();
if (err == 0) glyphList = new FTGlyphContainer (&face);
}
FTFont::~FTFont() {delete glyphList;}
bool FTFont::Attach (const char* fontFilePath) {
if (face.Attach (fontFilePath)) {
err = 0;
return true;
} else {
err = face.Error();
return false;
}
}
bool FTFont::Attach (const unsigned char *pBufferBytes, size_t bufferSizeInBytes) {
if (face.Attach (pBufferBytes, bufferSizeInBytes)) {
err = 0;
return true;
} else {
err = face.Error();
return false;
}
}
bool FTFont::FaceSize (const unsigned int size, const unsigned int res) {
charSize = face.Size (size, res);
err = face.Error();
if (err != 0) return false;
if (glyphList != NULL) delete glyphList;
glyphList = new FTGlyphContainer (&face);
return true;
}
unsigned int FTFont::FaceSize() const {return charSize.CharSize();}
bool FTFont::CharMap (FT_Encoding encoding) {
bool result = glyphList->CharMap (encoding);
err = glyphList->Error();
return result;
}
unsigned int FTFont::CharMapCount() {return face.CharMapCount();}
FT_Encoding *FTFont::CharMapList() {return face.CharMapList();}
float FTFont::Ascender() const {return charSize.Ascender();}
float FTFont::Descender() const {return charSize.Descender();}
float FTFont::LineHeight() const {return charSize.Height();}
void FTFont::BBox (const char* string, float& llx, float& lly, float& llz,
float& urx, float& ury, float& urz) {
FTBBox totalBBox;
if((NULL != string) && ('\0' != *string)) {
const unsigned char* c = (unsigned char*)string;
float advance = 0;
if(CheckGlyph (*c)) {
totalBBox = glyphList->BBox (*c);
advance = glyphList->Advance (*c, *(c + 1));
}
while (*++c) {
if(CheckGlyph (*c)) {
FTBBox tempBBox = glyphList->BBox (*c);
tempBBox.Move (FTPoint (advance, 0.0f, 0.0f));
totalBBox += tempBBox;
advance += glyphList->Advance (*c, *(c + 1));
}
}
}
llx = totalBBox.lowerX;
lly = totalBBox.lowerY;
llz = totalBBox.lowerZ;
urx = totalBBox.upperX;
ury = totalBBox.upperY;
urz = totalBBox.upperZ;
}
void FTFont::BBox (const wchar_t* string, float& llx, float& lly, float& llz,
float& urx, float& ury, float& urz) {
FTBBox totalBBox;
if((NULL != string) && ('\0' != *string)) {
const wchar_t* c = string;
float advance = 0;
if(CheckGlyph (*c)) {
totalBBox = glyphList->BBox (*c);
advance = glyphList->Advance (*c, *(c + 1));
}
while (*++c) {
if(CheckGlyph (*c)) {
FTBBox tempBBox = glyphList->BBox (*c);
tempBBox.Move (FTPoint (advance, 0.0f, 0.0f));
totalBBox += tempBBox;
advance += glyphList->Advance (*c, *(c + 1));
}
}
}
llx = totalBBox.lowerX;
lly = totalBBox.lowerY;
llz = totalBBox.lowerZ;
urx = totalBBox.upperX;
ury = totalBBox.upperY;
urz = totalBBox.upperZ;
}
float FTFont::Advance (const wchar_t* string) {
const wchar_t* c = string;
float width = 0.0f;
while (*c) {
if(CheckGlyph (*c)) width += glyphList->Advance (*c, *(c + 1));
++c;
}
return width;
}
float FTFont::Advance (const char* string) {
const unsigned char* c = (unsigned char*)string;
float width = 0.0f;
while (*c) {
if(CheckGlyph (*c)) width += glyphList->Advance (*c, *(c + 1));
++c;
}
return width;
}
void FTFont::Render (const char* string) {
const unsigned char* c = (unsigned char*)string;
pen.X(0); pen.Y(0);
while (*c) {
if(CheckGlyph (*c)) pen = glyphList->Render (*c, *(c + 1), pen);
++c;
}
}
void FTFont::Render (const wchar_t* string){
const wchar_t* c = string;
pen.X(0); pen.Y(0);
while (*c) {
if(CheckGlyph (*c)) pen = glyphList->Render (*c, *(c + 1), pen);
++c;
}
}
bool FTFont::CheckGlyph (const unsigned int characterCode) {
if (NULL == glyphList->Glyph (characterCode)) {
unsigned int glyphIndex = glyphList->FontIndex (characterCode);
FTGlyph* tempGlyph = MakeGlyph (glyphIndex);
if (NULL == tempGlyph) {
if (0 == err) err = 0x13;
return false;
}
glyphList->Add (tempGlyph, characterCode);
}
return true;
}
// --------------------------------------------------------------------
// FTLibrary
// --------------------------------------------------------------------
const FTLibrary &FTLibrary::Instance () {
static FTLibrary ftlib;
return ftlib;
}
FTLibrary::~FTLibrary () {
// PrintStr ("desctructor FTLibrary");
if (library != 0) {
FT_Done_FreeType (*library);
delete library;
library= 0;
}
}
FTLibrary::FTLibrary () : library(0), err(0) {Initialise();}
bool FTLibrary::Initialise () {
if (library != 0) return true;
library = new FT_Library;
err = FT_Init_FreeType (library);
if (err) {
delete library;
library = 0;
return false;
}
return true;
}
// --------------------------------------------------------------------
// FTFace
// --------------------------------------------------------------------
FTFace::FTFace (const char* fontFilePath)
: numGlyphs(0), fontEncodingList(0), err(0) {
const FT_Long DEFAULT_FACE_INDEX = 0;
ftFace = new FT_Face;
err = FT_New_Face (*FTLibrary::Instance().GetLibrary(), fontFilePath, DEFAULT_FACE_INDEX, ftFace);
if (err) {
Message ("error FT_New_Face");
delete ftFace;
ftFace = 0;
} else {
numGlyphs = (*ftFace)->num_glyphs;
hasKerningTable = FT_HAS_KERNING((*ftFace));
}
}
FTFace::FTFace (const unsigned char *pBufferBytes, size_t bufferSizeInBytes)
: numGlyphs(0), err(0) {
const FT_Long DEFAULT_FACE_INDEX = 0;
ftFace = new FT_Face;
err = FT_New_Memory_Face (
*FTLibrary::Instance().GetLibrary(),
(FT_Byte *)pBufferBytes,
bufferSizeInBytes, DEFAULT_FACE_INDEX, ftFace);
if (err) {
delete ftFace;
ftFace = 0;
} else numGlyphs = (*ftFace)->num_glyphs;
}
FTFace::~FTFace() {
if (ftFace) {
FT_Done_Face (*ftFace);
delete ftFace;
ftFace = 0;
}
}
bool FTFace::Attach (const char* fontFilePath) {
err = FT_Attach_File (*ftFace, fontFilePath);
return !err;
}
bool FTFace::Attach (const unsigned char *pBufferBytes, size_t bufferSizeInBytes) {
FT_Open_Args open;
open.flags = FT_OPEN_MEMORY;
open.memory_base = (FT_Byte *)pBufferBytes;
open.memory_size = bufferSizeInBytes;
err = FT_Attach_Stream (*ftFace, &open);
return !err;
}
const FTSize& FTFace::Size (const unsigned int size, const unsigned int res) {
charSize.CharSize (ftFace, size, res, res);
err = charSize.Error();
return charSize;
}
unsigned int FTFace::CharMapCount() {
return (*ftFace)->num_charmaps;
}
FT_Encoding* FTFace::CharMapList() {
if (0 == fontEncodingList) {
fontEncodingList = new FT_Encoding[CharMapCount()];
for (size_t encodingIndex = 0; encodingIndex < CharMapCount(); ++encodingIndex) {
fontEncodingList[encodingIndex] = (*ftFace)->charmaps[encodingIndex]->encoding;
}
}
return fontEncodingList;
}
FTPoint FTFace::KernAdvance (unsigned int index1, unsigned int index2) {
float x, y;
x = y = 0.0f;
if (hasKerningTable && index1 && index2) {
FT_Vector kernAdvance;
kernAdvance.x = kernAdvance.y = 0;
err = FT_Get_Kerning (*ftFace, index1, index2, ft_kerning_unfitted, &kernAdvance);
if (!err) {
x = static_cast<float> (kernAdvance.x) / 64.0f;
y = static_cast<float> (kernAdvance.y) / 64.0f;
}
}
return FTPoint (x, y, 0.0);
}
FT_GlyphSlot FTFace::Glyph (unsigned int index, FT_Int load_flags) {
err = FT_Load_Glyph (*ftFace, index, load_flags);
if (err) return NULL;
return (*ftFace)->glyph;
}
// --------------------------------------------------------------------
// FTPoint
// --------------------------------------------------------------------
bool operator == (const FTPoint &a, const FTPoint &b) {
return((a.values[0] == b.values[0]) && (a.values[1] ==
b.values[1]) && (a.values[2] == b.values[2]));
}
bool operator != (const FTPoint &a, const FTPoint &b) {
return((a.values[0] != b.values[0]) || (a.values[1]
!= b.values[1]) || (a.values[2] != b.values[2]));
}
FTPoint operator * (double multiplier, FTPoint& point){
return point * multiplier;
}
// --------------------------------------------------------------------
// FTSize
// --------------------------------------------------------------------
FTSize::FTSize()
: ftFace(0),
ftSize(0),
size(0),
xResolution(0),
yResolution(0),
err(0)
{}
FTSize::~FTSize() {}
bool FTSize::CharSize (FT_Face* face, unsigned int pointSize,
unsigned int xRes, unsigned int yRes) {
if (size != pointSize || xResolution != xRes || yResolution != yRes) {
err = FT_Set_Char_Size (*face, 0L, pointSize * 64, xResolution, yResolution);
if (!err) {
ftFace = face;
size = pointSize;
xResolution = xRes;
yResolution = yRes;
ftSize = (*ftFace)->size;
} else {
ftFace = 0;
size = 0;
xResolution = 0;
yResolution = 0;
ftSize = 0;
}
}
return !err;
}
unsigned int FTSize::CharSize() const {
return size;
}
float FTSize::Ascender() const {
return ftSize == 0 ? 0.0f : static_cast<float> (ftSize->metrics.ascender) / 64.0f;
}
float FTSize::Descender() const {
return ftSize == 0 ? 0.0f : static_cast<float> (ftSize->metrics.descender) / 64.0f;
}
float FTSize::Height() const {
if (0 == ftSize) return 0.0f;
if (FT_IS_SCALABLE((*ftFace))) {
return ((*ftFace)->bbox.yMax - (*ftFace)->bbox.yMin) *
((float)ftSize->metrics.y_ppem / (float)(*ftFace)->units_per_EM);
} else return static_cast<float> (ftSize->metrics.height) / 64.0f;
}
float FTSize::Width() const {
if (0 == ftSize) return 0.0f;
if (FT_IS_SCALABLE((*ftFace))) {
return ((*ftFace)->bbox.xMax - (*ftFace)->bbox.xMin) *
(static_cast<float>(ftSize->metrics.x_ppem) /
static_cast<float>((*ftFace)->units_per_EM));
} else return static_cast<float> (ftSize->metrics.max_advance) / 64.0f;
}
float FTSize::Underline() const {return 0.0f;}
// --------------------------------------------------------------------
// FTGlyph
// --------------------------------------------------------------------
FTGlyph::FTGlyph (FT_GlyphSlot glyph)
: err(0) {
if (glyph) {
bBox = FTBBox (glyph);
advance = FTPoint (glyph->advance.x / 64.0f, glyph->advance.y / 64.0f, 0.0f);
}
}
FTGlyph::~FTGlyph() {}
FTGlyphContainer::FTGlyphContainer (FTFace* f)
: face(f), err(0) {
glyphs.push_back (NULL);
charMap = new FTCharmap (face);
}
// --------------------------------------------------------------------
// FTGlyphContainer
// --------------------------------------------------------------------
FTGlyphContainer::~FTGlyphContainer() {
GlyphVector::iterator glyphIterator;
for (glyphIterator = glyphs.begin(); glyphIterator != glyphs.end(); ++glyphIterator) {
delete *glyphIterator;
}
glyphs.clear();
delete charMap;
}
bool FTGlyphContainer::CharMap (FT_Encoding encoding) {
bool result = charMap->CharMap (encoding);
err = charMap->Error();
return result;
}
unsigned int FTGlyphContainer::FontIndex (const unsigned int characterCode) const {
return charMap->FontIndex (characterCode);
}
void FTGlyphContainer::Add (FTGlyph* tempGlyph, const unsigned int characterCode) {
charMap->InsertIndex (characterCode, glyphs.size());
glyphs.push_back (tempGlyph);
}
const FTGlyph* FTGlyphContainer::Glyph (const unsigned int characterCode) const {
signed int index = charMap->GlyphListIndex (characterCode);
return glyphs[index];
}
FTBBox FTGlyphContainer::BBox (const unsigned int characterCode) const {
return glyphs[charMap->GlyphListIndex (characterCode)]->BBox();
}
float FTGlyphContainer::Advance (const unsigned int characterCode,
const unsigned int nextCharacterCode) {
unsigned int left = charMap->FontIndex (characterCode);
unsigned int right = charMap->FontIndex (nextCharacterCode);
float width = face->KernAdvance (left, right).X();
width += glyphs[charMap->GlyphListIndex (characterCode)]->Advance().X();
return width;
}
FTPoint FTGlyphContainer::Render (const unsigned int characterCode,
const unsigned int nextCharacterCode, FTPoint penPosition) {
FTPoint kernAdvance, advance;
unsigned int left = charMap->FontIndex (characterCode);
unsigned int right = charMap->FontIndex (nextCharacterCode);
kernAdvance = face->KernAdvance (left, right);
if (!face->Error()) {
advance = glyphs[charMap->GlyphListIndex (characterCode)]->Render (penPosition);
}
kernAdvance += advance;
return kernAdvance;
}
// --------------------------------------------------------------------
// FTTextureGlyph
// --------------------------------------------------------------------
GLint FTTextureGlyph::activeTextureID = 0;
FTTextureGlyph::FTTextureGlyph (FT_GlyphSlot glyph, int id, int xOffset,
int yOffset, GLsizei width, GLsizei height)
: FTGlyph (glyph), destWidth(0), destHeight(0), glTextureID(id) {
err = FT_Render_Glyph (glyph, FT_RENDER_MODE_NORMAL);
if (err || glyph->format != ft_glyph_format_bitmap) return;
FT_Bitmap bitmap = glyph->bitmap;
destWidth = bitmap.width;
destHeight = bitmap.rows;
if (destWidth && destHeight) {
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
glBindTexture (GL_TEXTURE_2D, glTextureID);
glTexSubImage2D (GL_TEXTURE_2D, 0, xOffset, yOffset,
destWidth, destHeight, GL_ALPHA, GL_UNSIGNED_BYTE, bitmap.buffer);
}
uv[0].X (static_cast<float>(xOffset) / static_cast<float>(width));
uv[0].Y (static_cast<float>(yOffset) / static_cast<float>(height));
uv[1].X (static_cast<float> (xOffset + destWidth) / static_cast<float>(width));
uv[1].Y (static_cast<float> (yOffset + destHeight) / static_cast<float>(height));
pos.X (glyph->bitmap_left);
pos.Y (glyph->bitmap_top);
}
FTTextureGlyph::~FTTextureGlyph() {}
const FTPoint& FTTextureGlyph::Render (const FTPoint& pen) {
if (activeTextureID != glTextureID) {
glBindTexture (GL_TEXTURE_2D, (GLuint)glTextureID);
activeTextureID = glTextureID;
}
glTranslatef (pen.X(), pen.Y(), 0.0f);
const GLfloat tex[] = {
uv[0].X(), uv[0].Y(),
uv[0].X(), uv[1].Y(),
uv[1].X(), uv[1].Y(),
uv[1].X(), uv[0].Y()
};
const GLfloat vtx[] = {
pos.X(), pos.Y(),
pos.X(), pos.Y() - destHeight,
destWidth + pos.X(), pos.Y() - destHeight,
destWidth + pos.X(), pos.Y()
};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, vtx);
glTexCoordPointer(2, GL_FLOAT, 0, tex);
glDrawArrays(GL_TRIANGLE_FAN,0,4);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
return advance;
}
inline GLuint NextPowerOf2 (GLuint in) {
in -= 1;
in |= in >> 16;
in |= in >> 8;
in |= in >> 4;
in |= in >> 2;
in |= in >> 1;
return in + 1;
}
// --------------------------------------------------------------------
// FTGLTextureFont
// --------------------------------------------------------------------
FTGLTextureFont::FTGLTextureFont (const char* fontFilePath)
: FTFont (fontFilePath),
maximumGLTextureSize(0),
textureWidth(0),
textureHeight(0),
glyphHeight(0),
glyphWidth(0),
padding(3),
xOffset(0),
yOffset(0)
{
remGlyphs = numGlyphs = face.GlyphCount();
}
FTGLTextureFont::FTGLTextureFont (const unsigned char *pBufferBytes, size_t bufferSizeInBytes)
: FTFont (pBufferBytes, bufferSizeInBytes),
maximumGLTextureSize(0),
textureWidth(0),
textureHeight(0),
glyphHeight(0),
glyphWidth(0),
padding(3),
xOffset(0),
yOffset(0)
{
remGlyphs = numGlyphs = face.GlyphCount();
}
FTGLTextureFont::~FTGLTextureFont() {
glDeleteTextures (textureIDList.size(), (const GLuint*)&textureIDList[0]);
}
FTGlyph* FTGLTextureFont::MakeGlyph (unsigned int glyphIndex) {
FT_GlyphSlot ftGlyph = face.Glyph (glyphIndex, FT_LOAD_NO_HINTING);
if (ftGlyph) {
glyphHeight = static_cast<int> (charSize.Height());
glyphWidth = static_cast<int> (charSize.Width());
if (textureIDList.empty()) {
textureIDList.push_back (CreateTexture());
xOffset = yOffset = padding;
}
if (xOffset > (textureWidth - glyphWidth)) {
xOffset = padding;
yOffset += glyphHeight;
if (yOffset > (textureHeight - glyphHeight)) {
textureIDList.push_back (CreateTexture());
yOffset = padding;
}
}
FTTextureGlyph* tempGlyph =
new FTTextureGlyph (ftGlyph, textureIDList[textureIDList.size() - 1],
xOffset, yOffset, textureWidth, textureHeight);
xOffset += static_cast<int> (tempGlyph->BBox().upperX - tempGlyph->BBox().lowerX + padding);
--remGlyphs;
return tempGlyph;
}
err = face.Error();
return NULL;
}
void FTGLTextureFont::CalculateTextureSize() {
if (!maximumGLTextureSize) {
glGetIntegerv (GL_MAX_TEXTURE_SIZE, (GLint*)&maximumGLTextureSize);
}
textureWidth = NextPowerOf2 ((remGlyphs * glyphWidth) + (padding * 2));
textureWidth = textureWidth > maximumGLTextureSize ? maximumGLTextureSize : textureWidth;
int h = static_cast<int> ((textureWidth - (padding * 2)) / glyphWidth);
textureHeight = NextPowerOf2 (( (numGlyphs / h) + 1) * glyphHeight);
textureHeight = textureHeight > maximumGLTextureSize ? maximumGLTextureSize : textureHeight;
}
GLuint FTGLTextureFont::CreateTexture() {
CalculateTextureSize();
int totalMemory = textureWidth * textureHeight;
unsigned char* textureMemory = new unsigned char[totalMemory];
memset (textureMemory, 0, totalMemory);
GLuint textID;
glGenTextures (1, (GLuint*)&textID);
glBindTexture (GL_TEXTURE_2D, textID);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D (GL_TEXTURE_2D, 0, GL_ALPHA, textureWidth, textureHeight, 0,
GL_ALPHA, GL_UNSIGNED_BYTE, textureMemory);
delete [] textureMemory;
return textID;
}
bool FTGLTextureFont::FaceSize (const unsigned int size, const unsigned int res) {
if (!textureIDList.empty()) {
glDeleteTextures (textureIDList.size(), (const GLuint*)&textureIDList[0]);
textureIDList.clear();
remGlyphs = numGlyphs = face.GlyphCount();
}
return FTFont::FaceSize (size, res);
}
void FTGLTextureFont::Render (const char* string) {
// pre render
GLint blend_src = 0;
GLint blend_dst = 0;
bool blend_wasEnabled = glIsEnabled(GL_BLEND);
if (blend_wasEnabled) {
glGetIntegerv(GL_BLEND_SRC, &blend_src);
glGetIntegerv(GL_BLEND_DST, &blend_dst);
} else {
glEnable(GL_BLEND);
}
// render
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // GL_ONE
FTTextureGlyph::ResetActiveTexture();
FTFont::Render (string);
// post render
if (blend_wasEnabled) {
glBlendFunc (blend_src, blend_dst);
} else {
glDisable(GL_BLEND);
}
}
void FTGLTextureFont::Render (const wchar_t* string) {
// pre render
GLint blend_src = 0;
GLint blend_dst = 0;
bool blend_wasEnabled = glIsEnabled(GL_BLEND);
if (blend_wasEnabled) {
glGetIntegerv(GL_BLEND_SRC, &blend_src);
glGetIntegerv(GL_BLEND_DST, &blend_dst);
} else {
glEnable(GL_BLEND);
}
// render
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // GL_ONE
FTTextureGlyph::ResetActiveTexture();
FTFont::Render (string);
// post render
if (blend_wasEnabled) {
glBlendFunc (blend_src, blend_dst);
} else {
glDisable(GL_BLEND);
}
}
// --------------------------------------------------------------------
// FTCharmap (character map)
// --------------------------------------------------------------------
FTCharmap::FTCharmap (FTFace* face)
: ftFace (*(face->Face())), err(0) {
if (!ftFace->charmap) err = FT_Set_Charmap (ftFace, ftFace->charmaps[0]);
ftEncoding = ftFace->charmap->encoding;
}
FTCharmap::~FTCharmap() {charMap.clear();}
bool FTCharmap::CharMap (FT_Encoding encoding) {
if (ftEncoding == encoding) return true;
err = FT_Select_Charmap (ftFace, encoding);
if (!err) ftEncoding = encoding;
else ftEncoding = ft_encoding_none;
charMap.clear();
return !err;
}
unsigned int FTCharmap::GlyphListIndex (unsigned int characterCode) {
return charMap.find (characterCode);
}
unsigned int FTCharmap::FontIndex (unsigned int characterCode) {
return FT_Get_Char_Index (ftFace, characterCode);
}
void FTCharmap::InsertIndex (const unsigned int characterCode, const unsigned int containerIndex) {
charMap.insert (characterCode, containerIndex);
}