Skip to content

Commit c92a06e

Browse files
committed
font: save font backend name into texture name
1 parent b68887b commit c92a06e

6 files changed

+48
-54
lines changed

font/BaseFontBackend.cpp

+40-54
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ CBaseFont::CBaseFont()
2525
m_iEllipsisWide( 0 ),
2626
m_glyphs(0, 0), m_ABCCache( 0, 0 )
2727
{
28-
m_szName[0] = 0;
28+
m_szTextureName[0] = m_szName[0] = 0;
2929
SetDefLessFunc( m_glyphs );
3030
SetDefLessFunc( m_ABCCache );
3131
}
@@ -38,7 +38,7 @@ CBaseFont::GetTextureName
3838
Mangle texture name, so using same font names with different attributes will not confuse engine or font renderer
3939
=========================
4040
+*/
41-
void CBaseFont::GetTextureName(char *dst, size_t len) const
41+
void CBaseFont::GetTextureName( char *dst, size_t len ) const
4242
{
4343
char attribs[256];
4444
int i = 0;
@@ -64,13 +64,13 @@ void CBaseFont::GetTextureName(char *dst, size_t len) const
6464
// faster loading: don't query filesystem, tell engine to skip everything and load only from buffer
6565
if( i == 0 )
6666
{
67-
snprintf( dst, len - 1, "#%s_%i_%i_font.bmp", GetName(), GetTall(), GetWeight() );
67+
snprintf( dst, len - 1, "#%s_%i_%i_%s_font.bmp", GetName(), GetTall(), GetWeight(), GetBackendName( ));
6868
dst[len - 1] = 0;
6969
}
7070
else
7171
{
7272
attribs[i] = 0;
73-
snprintf( dst, len - 1, "#%s_%i_%i_%s_font.bmp", GetName(), GetTall(), GetWeight(), attribs );
73+
snprintf( dst, len - 1, "#%s_%i_%i_%s_%s_font.bmp", GetName(), GetTall(), GetWeight(), attribs, GetBackendName( ));
7474
dst[len - 1] = 0;
7575
}
7676
}
@@ -83,11 +83,10 @@ void CBaseFont::UploadGlyphsForRanges(charRange_t *range, int rangeSize)
8383
const int height = GetHeight();
8484
const int tempSize = maxWidth * height * 4; // allocate temporary buffer for max possible glyph size
8585
const Point nullPt( 0, 0 );
86-
char name[256];
8786

88-
GetTextureName( name, sizeof( name ));
87+
GetTextureName( m_szTextureName, sizeof( m_szTextureName ));
8988

90-
if( ReadFromCache( name, range, rangeSize ))
89+
if( ReadFromCache( m_szTextureName, range, rangeSize ))
9190
{
9291
int dotWideA, dotWideB, dotWideC;
9392
GetCharABCWidths( '.', dotWideA, dotWideB, dotWideC );
@@ -207,9 +206,9 @@ void CBaseFont::UploadGlyphsForRanges(charRange_t *range, int rangeSize)
207206
}
208207
}
209208

210-
HIMAGE hImage = EngFuncs::PIC_Load( name, bmp.GetBitmap(), bmp.GetBitmapHdr()->fileSize, 0 );
209+
HIMAGE hImage = EngFuncs::PIC_Load( m_szTextureName, bmp.GetBitmap(), bmp.GetBitmapHdr()->fileSize, 0 );
211210

212-
SaveToCache( name, range, rangeSize, &bmp );
211+
SaveToCache( m_szTextureName, range, rangeSize, &bmp );
213212

214213
delete[] temp;
215214

@@ -228,9 +227,8 @@ void CBaseFont::UploadGlyphsForRanges(charRange_t *range, int rangeSize)
228227

229228
CBaseFont::~CBaseFont()
230229
{
231-
char name[256];
232-
GetTextureName( name, sizeof( name ) );
233-
EngFuncs::PIC_Free( name );
230+
if( m_szTextureName[0] != 0 )
231+
EngFuncs::PIC_Free( m_szTextureName );
234232
}
235233

236234
void CBaseFont::GetCharABCWidths( int ch, int &a, int &b, int &c )
@@ -283,53 +281,41 @@ bool CBaseFont::IsEqualTo(const char *name, int tall, int weight, int blur, int
283281

284282
void CBaseFont::DebugDraw()
285283
{
286-
HIMAGE hImage;
287-
char name[256];
284+
HIMAGE const hImage = EngFuncs::PIC_Load( m_szTextureName );
285+
const int w = EngFuncs::PIC_Width( hImage );
286+
const int h = EngFuncs::PIC_Height( hImage );
288287

289-
{
290-
GetTextureName( name, sizeof( name ) );
291-
292-
hImage = EngFuncs::PIC_Load( name );
293-
int w, h;
294-
w = EngFuncs::PIC_Width( hImage );
295-
h = EngFuncs::PIC_Height( hImage );
296-
int x = 0;
297-
EngFuncs::PIC_Set( hImage, 255, 255, 255 );
298-
EngFuncs::PIC_DrawTrans( Point(x, 0), Size( w, h ) );
288+
int x = 0;
289+
EngFuncs::PIC_Set( hImage, 255, 255, 255 );
290+
EngFuncs::PIC_DrawTrans( Point( x, 0 ), Size( w, h ));
299291

300-
for( int i = m_glyphs.FirstInorder();; i = m_glyphs.NextInorder( i ) )
292+
for( int i = m_glyphs.FirstInorder();; i = m_glyphs.NextInorder( i ) )
293+
{
294+
if( m_glyphs[i].texture == hImage )
301295
{
302-
if( m_glyphs[i].texture == hImage )
303-
{
304-
Point pt;
305-
Size sz;
306-
pt.x = x + m_glyphs[i].rect.left;
307-
pt.y = m_glyphs[i].rect.top;
308-
309-
sz.w = m_glyphs[i].rect.right - m_glyphs[i].rect.left;
310-
sz.h = m_glyphs[i].rect.bottom - pt.y;
311-
312-
UI_DrawRectangleExt( pt, sz, PackRGBA( 255, 0, 0, 255 ), 1 );
313-
314-
int a, b, c;
315-
GetCharABCWidths( m_glyphs[i].ch, a, b, c );
316-
317-
pt.x -= a;
318-
sz.w += c + a;
319-
320-
UI_DrawRectangleExt( pt, sz, PackRGBA( 0, 255, 0, 255 ), 1, QM_LEFT | QM_RIGHT );
321-
322-
int ascender = GetAscent();
323-
324-
pt.y += ascender;
325-
UI_DrawRectangleExt( pt, sz, PackRGBA( 0, 0, 255, 255 ), 1, QM_TOP );
326-
}
327-
328-
if( i == m_glyphs.LastInorder() )
329-
break;
296+
Point pt;
297+
Size sz;
298+
pt.x = x + m_glyphs[i].rect.left;
299+
pt.y = m_glyphs[i].rect.top;
300+
sz.w = m_glyphs[i].rect.right - m_glyphs[i].rect.left;
301+
sz.h = m_glyphs[i].rect.bottom - pt.y;
302+
UI_DrawRectangleExt( pt, sz, PackRGBA( 255, 0, 0, 255 ), 1 );
303+
304+
int a, b, c;
305+
GetCharABCWidths( m_glyphs[i].ch, a, b, c );
306+
307+
pt.x -= a;
308+
sz.w += c + a;
309+
UI_DrawRectangleExt( pt, sz, PackRGBA( 0, 255, 0, 255 ), 1, QM_LEFT | QM_RIGHT );
310+
311+
const int ascender = GetAscent();
312+
pt.y += ascender;
313+
UI_DrawRectangleExt( pt, sz, PackRGBA( 0, 0, 255, 255 ), 1, QM_TOP );
330314
}
331-
}
332315

316+
if( i == m_glyphs.LastInorder() )
317+
break;
318+
}
333319
}
334320

335321
void CBaseFont::ApplyBlur(Size rgbaSz, byte *rgba)

font/BaseFontBackend.h

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class CBaseFont
6363
virtual void GetCharRGBA( int ch, Point pt, Size sz, byte *rgba, Size &drawSize ) = 0;
6464
virtual void GetCharABCWidthsNoCache( int ch, int &a, int &b, int &c ) = 0;
6565
virtual bool HasChar( int ch ) const = 0;
66+
virtual const char *GetBackendName() const = 0;
6667
virtual void GetCharABCWidths( int ch, int &a, int &b, int &c );
6768
virtual void UploadGlyphsForRanges( charRange_t *range, int rangeSize );
6869
virtual int DrawCharacter(int ch, Point pt, int charH, const unsigned int color, bool forceAdditive = false);
@@ -140,6 +141,8 @@ class CBaseFont
140141

141142
CUtlRBTree<glyph_t, int> m_glyphs;
142143
CUtlRBTree<abc_t, int> m_ABCCache;
144+
145+
char m_szTextureName[256];
143146
friend class CFontManager;
144147
};
145148

font/BitmapFont.h

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class CBitmapFont : public CBaseFont
3434
void GetCharABCWidthsNoCache( int ch, int &a, int &b, int &c ) override;
3535
void GetCharABCWidths( int ch, int &a, int &b, int &c ) override;
3636
bool HasChar( int ch ) const override;
37+
const char *GetBackendName() const override { return "bitmap"; }
38+
3739
void UploadGlyphsForRanges( charRange_t *range, int rangeSize ) override;
3840
int DrawCharacter(int ch, Point pt, int charH, const unsigned int color, bool forceAdditive = false) override;
3941
private:

font/FreeTypeFont.h

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class CFreeTypeFont : public CBaseFont
4444
void GetCharRGBA(int ch, Point pt, Size sz, unsigned char *rgba, Size &drawSize) override;
4545
void GetCharABCWidthsNoCache( int ch, int &a, int &b, int &c ) override;
4646
bool HasChar( int ch ) const override;
47+
const char *GetBackendName() const override { return "ft2"; }
4748
private:
4849
FT_Face face;
4950
static FT_Library m_Library;

font/StbFont.h

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class CStbFont : public CBaseFont
3636
void GetCharRGBA(int ch, Point pt, Size sz, unsigned char *rgba, Size &drawSize) override;
3737
void GetCharABCWidthsNoCache( int ch, int &a, int &b, int &c ) override;
3838
bool HasChar( int ch ) const override;
39+
const char *GetBackendName() const override { return "stb"; }
3940

4041
private:
4142
byte *m_pFontData;

font/WinAPIFont.h

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class CWinAPIFont : public CBaseFont
4242
void GetCharRGBA( int ch, Point pt, Size sz, unsigned char *rgba, Size &drawSize ) override;
4343
void GetCharABCWidthsNoCache( int ch, int &a, int &b, int &c ) override;
4444
bool HasChar( int ch ) const override;
45+
const char *GetBackendName() const override { return "win32"; }
4546

4647
bool m_bFound;
4748

0 commit comments

Comments
 (0)