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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Xbox 360 support for TrueType Fonts. The only cuurent solution is to use XUI
// to mount the TTF, and rasterize glyph into a render target. XUI does not support
// rasterization directly to a system memory region.
//
//=====================================================================================//
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <malloc.h>
#include <tier0/dbg.h>
#include <vgui/ISurface.h>
#include <tier0/mem.h>
#include <utlbuffer.h>
#include "filesystem.h"
#include "materialsystem/imaterialsystem.h"
#include "FontEffects.h"
#include "vgui_surfacelib/Win32Font.h"
#include "vgui_surfacelib/FontManager.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
bool s_bSupportsUnicode = true;
//-----------------------------------------------------------------------------
// Determine possible style from parameters.
//-----------------------------------------------------------------------------
int GetStyleFromParameters( int iFlags, int iWeight )
{
// Available xbox TTF styles are very restricted.
int style = XUI_FONT_STYLE_NORMAL;
if ( iFlags & vgui::ISurface::FONTFLAG_ITALIC )
style |= XUI_FONT_STYLE_ITALIC;
if ( iFlags & vgui::ISurface::FONTFLAG_UNDERLINE )
style |= XUI_FONT_STYLE_UNDERLINE;
if ( iWeight > 400 )
style |= XUI_FONT_STYLE_BOLD;
return style;
}
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CWin32Font::CWin32Font()
{
m_szName = UTL_INVAL_SYMBOL;
m_iTall = 0;
m_iWeight = 0;
m_iHeight = 0;
m_iAscent = 0;
m_iFlags = 0;
m_iMaxCharWidth = 0;
m_hFont = NULL;
m_hDC = NULL;
m_bAntiAliased = false;
m_iBlur = 0;
m_iScanLines = 0;
m_bRotary = false;
m_bAdditive = false;
m_rgiBitmapSize[0] = 0;
m_rgiBitmapSize[1] = 0;
Q_memset( m_ABCWidthsCache, 0, sizeof( m_ABCWidthsCache ) );
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
CWin32Font::~CWin32Font()
{
CloseResource();
}
//-----------------------------------------------------------------------------
// Purpose: Creates the font.
//-----------------------------------------------------------------------------
bool CWin32Font::Create( const char *windowsFontName, int tall, int weight, int blur, int scanlines, int flags )
{
// setup font properties
m_iTall = tall;
m_iWeight = weight;
m_iFlags = flags;
m_bAntiAliased = (flags & vgui::ISurface::FONTFLAG_ANTIALIAS) ? 1 : 0;
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) ? 1 : 0;
m_bAdditive = (flags & vgui::ISurface::FONTFLAG_ADDITIVE) ? 1 : 0;
int style = GetStyleFromParameters( flags, weight );
// must support > 128, there are characters in this range in the custom fonts
COMPILE_TIME_ASSERT( ABCWIDTHS_CACHE_SIZE == 256 );
XUIFontMetrics fontMetrics;
XUICharMetrics charMetrics[256];
// many redundant requests are made that are actually the same font metrics
// find it in the metric cache first based on the true specific keys
if ( !FontManager().GetCachedXUIMetrics( windowsFontName, tall, style, &fontMetrics, charMetrics ) )
{
m_hFont = FontManager().MaterialSystem()->OpenTrueTypeFont( windowsFontName, tall, style );
if ( !m_hFont )
{
return false;
}
// getting the metrics is an expensive i/o operation, cache results
FontManager().MaterialSystem()->GetTrueTypeFontMetrics( m_hFont, &fontMetrics, charMetrics );
FontManager().SetCachedXUIMetrics( windowsFontName, tall, style, &fontMetrics, charMetrics );
}
m_szName = windowsFontName;
m_iHeight = fontMetrics.fMaxHeight + m_iDropShadowOffset + 2 * m_iOutlineSize;
m_iMaxCharWidth = fontMetrics.fMaxWidth;
m_iAscent = fontMetrics.fMaxAscent;
// determine cell bounds
m_rgiBitmapSize[0] = m_iMaxCharWidth + m_iOutlineSize * 2;
m_rgiBitmapSize[1] = m_iHeight;
// get char spacing
// a is space before character (can be negative)
// b is the width of the character
// c is the space after the character
Assert( ABCWIDTHS_CACHE_SIZE <= 256 );
Q_memset( m_ABCWidthsCache, 0, sizeof( m_ABCWidthsCache ) );
for ( int i = 1; i < ABCWIDTHS_CACHE_SIZE; i++ )
{
int a,b,c;
// Determine real a,b,c mapping from XUI Character Metrics
a = charMetrics[i].fMinX - 1; // Add one column of padding to make up for font rendering blurring into left column (and adjust in b)
b = charMetrics[i].fMaxX - charMetrics[i].fMinX + 1;
c = charMetrics[i].fAdvance - charMetrics[i].fMaxX; // NOTE: We probably should add a column here, but it's rarely needed in our current fonts so we're opting to save memory instead
// Widen for blur, outline, and shadow. Need to widen b and reduce a and c.
m_ABCWidthsCache[i].a = a - m_iBlur - m_iOutlineSize;
m_ABCWidthsCache[i].b = b + ( ( m_iBlur + m_iOutlineSize ) * 2 ) + m_iDropShadowOffset;
m_ABCWidthsCache[i].c = c - m_iBlur - m_iDropShadowOffset - m_iOutlineSize;
}
return true;
}
//-----------------------------------------------------------------------------
// Purpose: generates texture data (written into appropriate font page subrects) for multiple chars
//-----------------------------------------------------------------------------
void CWin32Font::GetCharsRGBA( newChar_t *newChars, int numNewChars, unsigned char *pRGBA )
{
if ( !m_hFont )
{
// demand request for font glyph, re-create font
int style = GetStyleFromParameters( m_iFlags, m_iWeight );
m_hFont = FontManager().MaterialSystem()->OpenTrueTypeFont( GetName(), m_iTall, style );
}
wchar_t *pWch = (wchar_t *)_alloca( numNewChars*sizeof(wchar_t) );
int *pOffsetX = (int *)_alloca( numNewChars*sizeof(int) );
int *pOffsetY = (int *)_alloca( numNewChars*sizeof(int) );
int *pWidth = (int *)_alloca( numNewChars*sizeof(int) );
int *pHeight = (int *)_alloca( numNewChars*sizeof(int) );
int *pRGBAOffset = (int *)_alloca( numNewChars*sizeof(int) );
for ( int i = 0; i < numNewChars; i++ )
{
int a, c, wide;
GetCharABCWidths( newChars[i].wch, a, wide, c );
pWch[i] = newChars[i].wch;
pOffsetX[i] = -a;
pOffsetY[i] = 0;
pWidth[i] = newChars[i].fontWide;
pHeight[i] = newChars[i].fontTall;
pRGBAOffset[i] = newChars[i].offset;
}
if ( !FontManager().MaterialSystem()->GetTrueTypeGlyphs( m_hFont, numNewChars, pWch, pOffsetX, pOffsetY, pWidth, pHeight, pRGBA, pRGBAOffset ) )
{
// failure
return;
}
for ( int i = 0; i < numNewChars; i++ )
{
// apply requested effects in specified order
unsigned char *pCharRGBA = pRGBA + newChars[i].offset;
ApplyDropShadowToTexture( newChars[i].fontWide, newChars[i].fontTall, pCharRGBA, m_iDropShadowOffset );
ApplyOutlineToTexture( newChars[i].fontWide, newChars[i].fontTall, pCharRGBA, m_iOutlineSize );
ApplyGaussianBlurToTexture( newChars[i].fontWide, newChars[i].fontTall, pCharRGBA, m_iBlur );
ApplyScanlineEffectToTexture( newChars[i].fontWide, newChars[i].fontTall, pCharRGBA, m_iScanLines );
ApplyRotaryEffectToTexture( newChars[i].fontWide, newChars[i].fontTall, pCharRGBA, m_bRotary );
}
}
//-----------------------------------------------------------------------------
// Purpose: writes the char into the specified 32bpp texture at specified rect
//-----------------------------------------------------------------------------
void CWin32Font::GetCharRGBA( wchar_t ch, int rgbaWide, int rgbaTall, unsigned char *pRGBA )
{
newChar_t newChar;
newChar.wch = ch;
newChar.fontWide = rgbaWide;
newChar.fontTall = rgbaTall;
newChar.offset = 0;
GetCharsRGBA( &newChar, 1, pRGBA );
}
//-----------------------------------------------------------------------------
// Purpose: returns true if the font is equivalent to that specified
//-----------------------------------------------------------------------------
bool CWin32Font::IsEqualTo(const char *windowsFontName, int tall, int weight, int blur, int scanlines, int flags)
{
// do an true comparison that accounts for non-supported behaviors that gets remapped
// avoids creating fonts that are graphically equivalent, though specified differently
if ( !stricmp( windowsFontName, m_szName.String() ) &&
m_iTall == tall &&
m_iBlur == blur &&
m_iScanLines == scanlines )
{
// only these flags affect the font glyphs
int validFlags = vgui::ISurface::FONTFLAG_DROPSHADOW |
vgui::ISurface::FONTFLAG_OUTLINE |
vgui::ISurface::FONTFLAG_ROTARY |
vgui::ISurface::FONTFLAG_ITALIC |
vgui::ISurface::FONTFLAG_UNDERLINE;
if ( ( m_iFlags & validFlags ) == ( flags & validFlags ) )
{
if ( GetStyleFromParameters( m_iFlags, m_iWeight ) == GetStyleFromParameters( flags, weight ) )
{
// the font is equivalent
return true;
}
}
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose: returns true only if this font is valid for use
//-----------------------------------------------------------------------------
bool CWin32Font::IsValid()
{
if ( m_szName != UTL_INVAL_SYMBOL )
return true;
return false;
}
//-----------------------------------------------------------------------------
// Purpose: set the font to be the one to currently draw with in the gdi
//-----------------------------------------------------------------------------
void CWin32Font::SetAsActiveFont( HDC hdc )
{
}
//-----------------------------------------------------------------------------
// Purpose: gets the abc widths for a character
//-----------------------------------------------------------------------------
void CWin32Font::GetCharABCWidths( int ch, int &a, int &b, int &c )
{
Assert( IsValid() );
if ( ch < ABCWIDTHS_CACHE_SIZE )
{
// use the cache entry
a = m_ABCWidthsCache[ch].a;
b = m_ABCWidthsCache[ch].b;
c = m_ABCWidthsCache[ch].c;
}
else
{
// cannot support getting character metrics outside of the font initialization
DevMsg( "CWin32Font: Cannot resolve character %d in font %s\n", ch, m_szName.String() );
Assert( 0 );
a = 0;
b = 0;
c = 0;
}
}
//-----------------------------------------------------------------------------
// Purpose: returns the height of the font, in pixels
//-----------------------------------------------------------------------------
int CWin32Font::GetHeight()
{
Assert( IsValid() );
return m_iHeight;
}
//-----------------------------------------------------------------------------
// Purpose: returns the ascent of the font, in pixels (ascent=units above the base line)
//-----------------------------------------------------------------------------
int CWin32Font::GetAscent()
{
Assert( IsValid() );
return m_iAscent;
}
//-----------------------------------------------------------------------------
// Purpose: returns the maximum width of a character, in pixels
//-----------------------------------------------------------------------------
int CWin32Font::GetMaxCharWidth()
{
Assert( IsValid() );
return m_iMaxCharWidth;
}
//-----------------------------------------------------------------------------
// Purpose: returns the flags used to make this font, used by the dynamic resizing code
//-----------------------------------------------------------------------------
int CWin32Font::GetFlags()
{
Assert( IsValid() );
return m_iFlags;
}
void CWin32Font::CloseResource()
{
if ( !m_hFont )
{
return;
}
// many fonts are blindly precached by vgui and never used
// save memory and don't hold font open, re-open if glyph actually requested used during draw
FontManager().MaterialSystem()->CloseTrueTypeFont( m_hFont );
m_hFont = NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Get the kerned size of a char, for win32 just pass thru for now
//-----------------------------------------------------------------------------
void CWin32Font::GetKernedCharWidth( wchar_t ch, wchar_t chBefore, wchar_t chAfter, float &wide, float &abcA )
{
int a,b,c;
GetCharABCWidths(ch, a, b, c );
wide = ( a + b + c);
abcA = a;
}
|