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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "vgui_surfacelib/linuxfont.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <malloc.h>
#include <tier0/dbg.h>
#include <vgui/ISurface.h>
#include <utlbuffer.h>
#include <fontconfig/fontconfig.h>
#include <freetype/ftbitmap.h>
#include "materialsystem/imaterialsystem.h"
#include "vgui_surfacelib/FontManager.h"
#include "FontEffects.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#define FT_LOAD_FLAGS 0 //$ (FT_LOAD_TARGET_LIGHT)
namespace {
// Freetype uses a lot of fixed float values that are 26.6 splits of a 32 bit word.
// to make it an int, shift down the 6 bits and round up if the high bit of the 6
// bits was set.
inline int32_t FIXED6_2INT(int32_t x) { return ( (x>>6) + ( (x&0x20) ? (x<0 ? -1 : 1) : 0) ); }
inline float FIXED6_2FLOAT(int32_t x) { return (float)x / 64.0f; }
inline int32_t INT_2FIXED6(int32_t x) { return x << 6; }
}
bool CLinuxFont::ms_bSetFriendlyNameCacheLessFunc = false;
CUtlRBTree< CLinuxFont::font_name_entry > CLinuxFont::m_FriendlyNameCache;
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CLinuxFont::CLinuxFont() :
m_ExtendedABCWidthsCache(256, 0, &ExtendedABCWidthsCacheLessFunc),
m_ExtendedKernedABCWidthsCache( 256, 0, &ExtendedKernedABCWidthsCacheLessFunc )
{
m_face = NULL;
m_faceValid = false;
m_iTall = 0;
m_iHeight = 0;
m_iHeightRequested = 0;
m_iWeight = 0;
m_iFlags = 0;
m_iMaxCharWidth = 0;
m_bAntiAliased = false;
m_bUnderlined = false;
m_iBlur = 0;
m_iScanLines = 0;
m_bRotary = false;
m_bAdditive = false;
if ( !ms_bSetFriendlyNameCacheLessFunc )
{
ms_bSetFriendlyNameCacheLessFunc = true;
SetDefLessFunc( m_FriendlyNameCache );
}
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
CLinuxFont::~CLinuxFont()
{
if( m_faceValid )
{
FT_Done_Face( m_face );
m_face = NULL;
m_faceValid = false;
}
}
//-----------------------------------------------------------------------------
// Purpose: build a map of friendly (char *) name to crazy ATSU bytestream, so we can ask for "Tahoma" and actually load it
//-----------------------------------------------------------------------------
void CLinuxFont::CreateFontList()
{
if ( m_FriendlyNameCache.Count() > 0 )
return;
if(!FcInit())
return;
FcConfig *config;
FcPattern *pat;
FcObjectSet *os;
FcFontSet *fontset;
int i;
char *file;
const char *name;
config = FcConfigGetCurrent();
pat = FcPatternCreate();
os = FcObjectSetCreate();
FcObjectSetAdd(os, FC_FILE);
FcObjectSetAdd(os, FC_FULLNAME);
FcObjectSetAdd(os, FC_FAMILY);
FcObjectSetAdd(os, FC_SCALABLE);
fontset = FcFontList(config, pat, os);
if(!fontset)
return;
for(i = 0; i < fontset->nfont; i++)
{
FcBool scalable;
if ( FcPatternGetBool(fontset->fonts[i], FC_SCALABLE, 0, &scalable) == FcResultMatch && !scalable )
continue;
if ( FcPatternGetString(fontset->fonts[i], FC_FAMILY, 0, (FcChar8**)&name) != FcResultMatch )
continue;
if ( FcPatternGetString(fontset->fonts[i], FC_FILE, 0, (FcChar8**)&file) != FcResultMatch )
continue;
font_name_entry entry;
entry.m_pchFile = (char *)malloc( Q_strlen(file) + 1 );
entry.m_pchFriendlyName = (char *)malloc( Q_strlen(name) +1);
Q_memcpy( entry.m_pchFile, file, Q_strlen(file) + 1 );
Q_memcpy( entry.m_pchFriendlyName, name, Q_strlen(name) +1);
m_FriendlyNameCache.Insert( entry );
// substitute Vera Sans for Tahoma on X
if ( !V_stricmp( name, "Bitstream Vera Sans" ) )
{
name = "Tahoma";
entry.m_pchFile = (char *)malloc( Q_strlen(file) + 1 );
entry.m_pchFriendlyName = (char *)malloc( Q_strlen(name) +1);
Q_memcpy( entry.m_pchFile, file, Q_strlen(file) + 1 );
Q_memcpy( entry.m_pchFriendlyName, name, Q_strlen(name) +1);
m_FriendlyNameCache.Insert( entry );
name = "Verdana";
entry.m_pchFile = (char *)malloc( Q_strlen(file) + 1 );
entry.m_pchFriendlyName = (char *)malloc( Q_strlen(name) +1);
Q_memcpy( entry.m_pchFile, file, Q_strlen(file) + 1 );
Q_memcpy( entry.m_pchFriendlyName, name, Q_strlen(name) +1);
m_FriendlyNameCache.Insert( entry );
name = "Lucidia Console";
entry.m_pchFile = (char *)malloc( Q_strlen(file) + 1 );
entry.m_pchFriendlyName = (char *)malloc( Q_strlen(name) +1);
Q_memcpy( entry.m_pchFile, file, Q_strlen(file) + 1 );
Q_memcpy( entry.m_pchFriendlyName, name, Q_strlen(name) +1);
m_FriendlyNameCache.Insert( entry );
}
}
FcFontSetDestroy(fontset);
FcObjectSetDestroy(os);
FcPatternDestroy(pat);
}
static FcPattern* FontMatch(const char* type, FcType vtype, const void* value,
...)
{
va_list ap;
va_start(ap, value);
FcPattern* pattern = FcPatternCreate();
for (;;)
{
FcValue fcvalue;
fcvalue.type = vtype;
switch (vtype) {
case FcTypeString:
fcvalue.u.s = (FcChar8*) value;
break;
case FcTypeInteger:
fcvalue.u.i = (int) value;
break;
default:
Assert(!"FontMatch unhandled type");
}
FcPatternAdd(pattern, type, fcvalue, 0);
type = va_arg(ap, const char *);
if (!type)
break;
// FcType is promoted to int when passed through ...
vtype = static_cast<FcType>(va_arg(ap, int));
value = va_arg(ap, const void *);
};
va_end(ap);
FcConfigSubstitute(0, pattern, FcMatchPattern);
FcDefaultSubstitute(pattern);
FcResult result;
FcPattern* match = FcFontMatch(0, pattern, &result);
FcPatternDestroy(pattern);
return match;
}
bool CLinuxFont::CreateFromMemory(const char *windowsFontName, void *data, int datasize, int tall, int weight, int blur, int scanlines, int flags)
{
// setup font properties
m_szName = windowsFontName;
m_iTall = tall;
m_iWeight = weight;
m_iFlags = flags;
m_bAntiAliased = flags & vgui::ISurface::FONTFLAG_ANTIALIAS;
m_bUnderlined = flags & vgui::ISurface::FONTFLAG_UNDERLINE;
m_iDropShadowOffset = (flags & vgui::ISurface::FONTFLAG_DROPSHADOW) ? 1 : 0;
m_iOutlineSize = (flags & vgui::ISurface::FONTFLAG_OUTLINE) ? 1 : 0;
m_iBlur = blur;
m_iScanLines = scanlines;
m_bRotary = flags & vgui::ISurface::FONTFLAG_ROTARY;
m_bAdditive = flags & vgui::ISurface::FONTFLAG_ADDITIVE;
if ( !HushAsserts() )
{
// These flags are NYI in Linux right now.
Assert( !m_bAntiAliased );
Assert( !m_bUnderlined );
Assert( !m_bAdditive );
}
Assert( !m_faceValid );
FT_Error error = FT_New_Memory_Face( FontManager().GetFontLibraryHandle(), (FT_Byte *)data, datasize, 0, &m_face );
if ( error )
{
// FT_Err_Unknown_File_Format?
Msg( "FT_New_Memory_Face failed. font:%s error:%d\n", windowsFontName, error );
return false;
}
if ( m_face->charmap == NULL )
{
FT_Error error = FT_Select_Charmap( m_face, FT_ENCODING_APPLE_ROMAN );
if ( error )
{
FT_Done_Face( m_face );
m_face = NULL;
Msg( "Font %s has no valid charmap\n", windowsFontName );
return false;
}
}
m_iHeightRequested = m_iTall;
// Loop through until we get a height that is less than or equal to the requested height.
// We tried using the BBOX ascender / descender, but it was overly large compared to Windows.
// We also tried using the size metrics, but the ascender wasn't high enough and accents were cut off.
// Descender from size metrics was too low for fonts with no lower case characters.
// Compromise: Use ascent from O' and descent from bbox. Diffs on textures indicate this is best choice.
// Used these command lines vars and convars to help beyond compare linux and windows:
// -precachefontintlchars / -enable_font_bounding_boxes / vgui_spew_fonts / mat_texture_save_fonts
bool bFirstTimeThrough = true;
int IncAmount = -1;
for ( ;; )
{
bool SetPixelSizesFailed = false;
FT_Error error = FT_Set_Pixel_Sizes( m_face, 0, m_iHeightRequested );
if ( error )
{
SetPixelSizesFailed = true;
// If FT_Set_Pixel_Sizes fails, it should be because we've got a fixed-size font.
Assert( m_face->face_flags & FT_FACE_FLAG_FIXED_SIZES );
Assert( !( m_face->face_flags & FT_FACE_FLAG_SCALABLE ) );
if ( m_face->num_fixed_sizes )
{
// Pick width/height of first size.
int width = m_face->available_sizes[ 0 ].width;
m_iHeightRequested = m_face->available_sizes[ 0 ].height;
// Loop through all the other available sizes and find the closest match.
for ( int i = 1; i < m_face->num_fixed_sizes; i++ )
{
if ( ( m_face->available_sizes[ i ].height <= m_iTall ) &&
( m_face->available_sizes[ i ].height > m_iHeightRequested ) )
{
width = m_face->available_sizes[ i ].width;
m_iHeightRequested = m_face->available_sizes[ i ].height;
}
}
FT_Size_RequestRec req;
Q_memset( &req, 0, sizeof( req ) );
req.type = FT_SIZE_REQUEST_TYPE_REAL_DIM;
req.width = INT_2FIXED6( width );
req.height = INT_2FIXED6( m_iHeightRequested );
req.horiResolution = 0;
req.vertResolution = 0;
error = FT_Request_Size( m_face, &req );
if ( error )
{
Msg( "FT_Request_Size failed on %s / %s\n",
m_face->family_name ? m_face->family_name : "??",
m_face->style_name ? m_face->style_name : "??" );
}
}
}
FT_Pos ascender, descender;
if( SetPixelSizesFailed )
{
// If SetPixelSizesFailed failed, then we've hopefully got a fixed size
// font, and we can just use the metrics.
ascender = m_face->size->metrics.ascender;
descender = m_face->size->metrics.descender;
}
else
{
// Full bounding box ascent and descent:
// ( a * b ) / 0x10000. y_scale is 16.16.
//$ ascender = FT_MulFix( m_face->bbox.yMax, m_face->size->metrics.y_scale );
descender = FT_MulFix( m_face->bbox.yMin, m_face->size->metrics.y_scale );
// Metrics ascent and descent
ascender = m_face->size->metrics.ascender;
//$ descender = m_face->size->metrics.descender;
// While running with Spanish, the m_face->size->metrics.ascender is less
// than the bitmap_top for the character. This makes GetCharRGBA() chop off
// the top of the O and the accent is skipped. Complete hack here, but we
// check for the tallest character we know about (O') and bump up the ascender
// value if it is greater than what we've currently got.
wchar_t ch = 0xd3;
error = FT_Load_Char( m_face, ch, FT_LOAD_RENDER | FT_LOAD_TARGET_NORMAL);
if ( !error )
{
int glyph_index = FT_Get_Char_Index( m_face, ch );
error = FT_Load_Glyph( m_face, glyph_index, FT_LOAD_RENDER );
if ( !error )
{
FT_GlyphSlot slot = m_face->glyph;
FT_Pos ascenderTop = INT_2FIXED6( slot->bitmap_top );
if( ascenderTop > ascender )
{
ascender = ascenderTop;
}
else if ( !slot->bitmap.rows || !slot->bitmap.width )
{
// We didn't find an O' character in this font: use the full BBox.
ascender = FT_MulFix( m_face->bbox.yMax, m_face->size->metrics.y_scale );
}
}
}
}
m_iAscent = FIXED6_2INT( ascender );
m_iMaxCharWidth = FIXED6_2INT( m_face->size->metrics.max_advance );
const int fxpHeight = ascender + -descender + INT_2FIXED6( m_iDropShadowOffset + 2 * m_iOutlineSize );
m_iHeight = FIXED6_2INT( fxpHeight );
// If we're exact or we got too small, bail.
if ( SetPixelSizesFailed || ( m_iHeight == m_iTall ) || ( m_iHeight < 7 ) || ( m_iHeightRequested <= 1 ) )
break;
if( bFirstTimeThrough )
{
bFirstTimeThrough = false;
// If we're smaller than requested, start searching up.
if ( m_iHeight < m_iTall )
IncAmount = +1;
}
else if( IncAmount > 0 )
{
// If we're searching up and went too far, drop IncAmount and run down.
if( m_iHeight > m_iTall )
IncAmount = -1;
}
else if ( m_iHeight <= m_iTall )
{
// If the height is less than tall, we're done.
break;
}
m_iHeightRequested += IncAmount;
}
m_faceValid = true;
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Given a font name from windows, match it to the filename and return that.
//-----------------------------------------------------------------------------
char *CLinuxFont::GetFontFileName( const char *windowsFontName, int flags )
{
bool bBold = false;
const char *pchFontName = windowsFontName;
if ( !Q_stricmp( pchFontName, "Tahoma" ) )
pchFontName = "Bitstream Vera Sans";
else if ( !Q_stricmp( pchFontName, "Arial Black" ) || Q_stristr( pchFontName, "bold" ) )
bBold = true;
const int italic = ( flags & vgui::ISurface::FONTFLAG_ITALIC ) ? FC_SLANT_ITALIC : FC_SLANT_ROMAN;
const int nFcWeight = bBold ? FC_WEIGHT_BOLD : FC_WEIGHT_NORMAL;
FcPattern *match = FontMatch( FC_FAMILY, FcTypeString, pchFontName,
FC_WEIGHT, FcTypeInteger, nFcWeight,
FC_SLANT, FcTypeInteger, italic,
NULL);
if ( !match )
{
AssertMsg1( false, "Unable to find font named %s\n", windowsFontName );
return NULL;
}
else
{
char *filenameret = NULL;
FcChar8* filename = NULL;
if ( FcPatternGetString(match, FC_FILE, 0, &filename) != FcResultMatch )
{
AssertMsg1( false, "Unable to find font named %s\n", windowsFontName );
}
else
{
filenameret = strdup( ( char * )filename );
}
FcPatternDestroy( match );
return filenameret;
}
}
//-----------------------------------------------------------------------------
// Purpose: writes the char into the specified 32bpp texture
//-----------------------------------------------------------------------------
void CLinuxFont::GetCharRGBA( wchar_t ch, int rgbaWide, int rgbaTall, unsigned char *prgba )
{
bool bShouldAntialias = m_bAntiAliased;
// filter out
if ( ( ch > 0x00FF ) && !( m_iFlags & vgui::ISurface::FONTFLAG_CUSTOM ) )
{
bShouldAntialias = false;
}
FT_Error error = FT_Load_Char( m_face, ch, FT_LOAD_RENDER | FT_LOAD_TARGET_NORMAL );
if ( error )
{
Msg( "Error in FT_Load_Char: ch:%x error:%x\n", (int)ch, error );
return;
}
int glyph_index = FT_Get_Char_Index( m_face, ch );
error = FT_Load_Glyph( m_face, glyph_index, FT_LOAD_RENDER | FT_LOAD_FLAGS );
if ( error )
{
Msg( "Error in FL_Load_Glyph: glyph_index:%d error:%x\n", glyph_index, error );
return;
}
int yBitmapStart = 0;
FT_GlyphSlot slot = m_face->glyph;
int nSkipRows = ( m_iAscent - slot->bitmap_top );
if( nSkipRows < 0 )
{
yBitmapStart = -nSkipRows;
nSkipRows = 0;
}
if ( nSkipRows >= rgbaTall )
{
Msg( "nSkipRows(%d) > rgbaTall(%d) ch:%d\n", nSkipRows, rgbaTall, (int)ch );
return;
}
if ( m_face->glyph->bitmap.width == 0 )
{
Msg( "m_face->glyph->bitmap.width is 0 for ch:%d %s\n", (int)ch, m_face->family_name ? m_face->family_name : "??" );
return;
}
FT_Bitmap bitmap;
FT_Library ftLibrary = FontManager().GetFontLibraryHandle();
FT_Bitmap_New( &bitmap );
error = FT_Bitmap_Convert( ftLibrary, &m_face->glyph->bitmap, &bitmap, 1 );
if( error == 0 )
{
uint32 alpha_scale = 1;
int Width = min( rgbaWide, bitmap.width );
unsigned char *rgba = prgba + ( nSkipRows * rgbaWide * 4 );
switch( m_face->glyph->bitmap.pixel_mode )
{
case FT_PIXEL_MODE_MONO: // 8-bit per pixel bitmap
alpha_scale *= 256;
break;
case FT_PIXEL_MODE_GRAY2: // 2-bit per pixel bitmap
alpha_scale *= 64;
break;
case FT_PIXEL_MODE_GRAY4: // 4-bit per pixel bitmap
alpha_scale *= 16;
break;
}
/* now draw to our target surface */
for ( int y = yBitmapStart; y < MIN( bitmap.rows, rgbaTall - nSkipRows ); y++ )
{
for ( int x = 0; x < Width; x++ )
{
int rgbaOffset = 4 * ( x + m_iBlur ); // +(rgbaTall-y-1)*rgbaWide*4
uint32 alpha = Min( 255U, alpha_scale * bitmap.buffer[ x + y * bitmap.pitch ] );
rgba[ rgbaOffset + 0 ] = 255;
rgba[ rgbaOffset + 1 ] = 255;
rgba[ rgbaOffset + 2 ] = 255;
rgba[ rgbaOffset + 3 ] = alpha;
}
rgba += ( rgbaWide * 4 );
}
// apply requested effects in specified order
ApplyDropShadowToTexture( rgbaWide, rgbaTall, prgba, m_iDropShadowOffset );
ApplyOutlineToTexture( rgbaWide, rgbaTall, prgba, m_iOutlineSize );
ApplyGaussianBlurToTexture( rgbaWide, rgbaTall, prgba, m_iBlur );
ApplyScanlineEffectToTexture( rgbaWide, rgbaTall, prgba, m_iScanLines );
ApplyRotaryEffectToTexture( rgbaWide, rgbaTall, prgba, m_bRotary );
}
else
{
Msg( "FT_Bitmap_Convert failed: %d on %s\n", error, m_face->family_name ? m_face->family_name : "??" );
}
FT_Bitmap_Done( ftLibrary, &bitmap );
}
void CLinuxFont::GetKernedCharWidth( wchar_t ch, wchar_t chBefore, wchar_t chAfter, float &wide, float &abcA, float &abcC )
{
abcA = abcC = wide = 0.0f;
// look for it in the cache
kerned_abc_cache_t finder = { ch, chBefore, chAfter };
unsigned short iKerned = m_ExtendedKernedABCWidthsCache.Find(finder);
if (m_ExtendedKernedABCWidthsCache.IsValidIndex(iKerned))
{
abcA = 0; //$ NYI. m_ExtendedKernedABCWidthsCache[iKerned].abc.abcA;
abcC = 0; //$ NYI. m_ExtendedKernedABCWidthsCache[iKerned].abc.abcC;
wide = m_ExtendedKernedABCWidthsCache[iKerned].abc.wide;
return;
}
FT_UInt glyph_index;
FT_Bool use_kerning;
FT_UInt previous;
int32_t iFxpPenX;
iFxpPenX = 0;
wide = 0;
use_kerning = FT_HAS_KERNING( m_face );
previous = chBefore;
/* convert character code to glyph index */
glyph_index = FT_Get_Char_Index( m_face, ch );
/* retrieve kerning distance and move pen position */
if ( use_kerning && previous && glyph_index )
{
FT_Vector delta;
FT_Get_Kerning( m_face, previous, glyph_index,
FT_KERNING_DEFAULT, &delta );
iFxpPenX += delta.x;
}
/* load glyph image into the slot (erase previous one) */
int error = FT_Load_Glyph( m_face, glyph_index, FT_LOAD_DEFAULT | FT_LOAD_FLAGS );
if ( error )
{
Error( "Error in FL_Load_Glyph: glyph_index:%d ch:%x error:%x\n", glyph_index, (int)ch, error );
}
FT_GlyphSlot slot = m_face->glyph;
iFxpPenX += slot->advance.x;
if ( FIXED6_2INT(iFxpPenX) > wide )
wide = FIXED6_2INT(iFxpPenX);
//$ NYI: finder.abc.abcA = abcA;
//$ NYI: finder.abc.abcC = abcC;
finder.abc.wide = wide;
m_ExtendedKernedABCWidthsCache.Insert(finder);
}
//-----------------------------------------------------------------------------
// Purpose: gets the abc widths for a character
//-----------------------------------------------------------------------------
void CLinuxFont::GetCharABCWidths(int ch, int &a, int &b, int &c)
{
Assert(IsValid());
// look for it in the cache
abc_cache_t finder = { (wchar_t)ch };
unsigned short i = m_ExtendedABCWidthsCache.Find(finder);
if (m_ExtendedABCWidthsCache.IsValidIndex(i))
{
a = m_ExtendedABCWidthsCache[i].abc.a;
b = m_ExtendedABCWidthsCache[i].abc.b;
c = m_ExtendedABCWidthsCache[i].abc.c;
return;
}
a = b = c = 0;
FT_Error error = FT_Load_Char( m_face, ch, 0 );
if ( error )
{
Msg( "Error in FT_Load_Char: ch:%x error:%x\n", ch, error );
return;
}
// width: The glyph's width.
// horiBearingX: Left side bearing for horizontal layout.
// horiAdvance: Advance width for horizontal layout.
FT_Glyph_Metrics metrics = m_face->glyph->metrics;
finder.abc.a = metrics.horiBearingX / 64 - m_iBlur - m_iOutlineSize;
finder.abc.b = metrics.width / 64 + ( ( m_iBlur + m_iOutlineSize ) * 2 ) + m_iDropShadowOffset;
finder.abc.c = ( metrics.horiAdvance - metrics.horiBearingX - metrics.width ) / 64 - m_iBlur - m_iDropShadowOffset - m_iOutlineSize;
m_ExtendedABCWidthsCache.Insert( finder );
a = finder.abc.a;
b = finder.abc.b;
c = finder.abc.c;
}
//-----------------------------------------------------------------------------
// Purpose: returns true if the font is equivalent to that specified
//-----------------------------------------------------------------------------
bool CLinuxFont::IsEqualTo(const char *windowsFontName, int tall, int weight, int blur, int scanlines, int flags)
{
if (!Q_stricmp(windowsFontName, m_szName.String() )
&& m_iTall == tall
&& m_iWeight == weight
&& m_iBlur == blur
&& m_iFlags == flags)
return true;
return false;
}
//-----------------------------------------------------------------------------
// Purpose: returns true only if this font is valid for use
//-----------------------------------------------------------------------------
bool CLinuxFont::IsValid()
{
if ( !m_szName.IsEmpty() )
return true;
return false;
}
//-----------------------------------------------------------------------------
// Purpose: returns the height of the font, in pixels
//-----------------------------------------------------------------------------
int CLinuxFont::GetHeight()
{
assert(IsValid());
return m_iHeight;
}
//-----------------------------------------------------------------------------
// Purpose: returns the requested height of the font
//-----------------------------------------------------------------------------
int CLinuxFont::GetHeightRequested()
{
assert(IsValid());
return m_iHeightRequested;
}
//-----------------------------------------------------------------------------
// Purpose: returns the ascent of the font, in pixels (ascent=units above the base line)
//-----------------------------------------------------------------------------
int CLinuxFont::GetAscent()
{
assert(IsValid());
return m_iAscent;
}
//-----------------------------------------------------------------------------
// Purpose: returns the maximum width of a character, in pixels
//-----------------------------------------------------------------------------
int CLinuxFont::GetMaxCharWidth()
{
assert(IsValid());
return m_iMaxCharWidth;
}
//-----------------------------------------------------------------------------
// Purpose: returns the flags used to make this font, used by the dynamic resizing code
//-----------------------------------------------------------------------------
int CLinuxFont::GetFlags()
{
return m_iFlags;
}
//-----------------------------------------------------------------------------
// Purpose: Comparison function for abc widths storage
//-----------------------------------------------------------------------------
bool CLinuxFont::ExtendedABCWidthsCacheLessFunc(const abc_cache_t &lhs, const abc_cache_t &rhs)
{
return lhs.wch < rhs.wch;
}
//-----------------------------------------------------------------------------
// Purpose: Comparison function for abc widths storage
//-----------------------------------------------------------------------------
bool CLinuxFont::ExtendedKernedABCWidthsCacheLessFunc(const kerned_abc_cache_t &lhs, const kerned_abc_cache_t &rhs)
{
return ( lhs.wch < rhs.wch ) ||
( lhs.wch == rhs.wch && lhs.wchBefore < rhs.wchBefore ) ||
( lhs.wch == rhs.wch && lhs.wchBefore == rhs.wchBefore && lhs.wchAfter < rhs.wchAfter );
}
void *CLinuxFont::SetAsActiveFont( void *cglContext )
{
Assert( false );
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose: returns true if this font has a glyph for the code point.
//-----------------------------------------------------------------------------
bool CLinuxFont::HasChar(wchar_t wch)
{
return FT_Get_Char_Index( m_face, wch ) != 0;
}
#ifdef DBGFLAG_VALIDATE
//-----------------------------------------------------------------------------
// Purpose: Ensure that all of our internal structures are consistent, and
// account for all memory that we've allocated.
// Input: validator - Our global validator object
// pchName - Our name (typically a member var in our container)
//-----------------------------------------------------------------------------
void CLinuxFont::Validate( CValidator &validator, const char *pchName )
{
validator.Push( "CLinuxFont", this, pchName );
m_ExtendedABCWidthsCache.Validate( validator, "m_ExtendedABCWidthsCache" );
m_ExtendedKernedABCWidthsCache.Validate( validator, "m_ExtendedKernedABCWidthsCache" );
validator.Pop();
}
#endif // DBGFLAG_VALIDATE
|