summaryrefslogtreecommitdiff
path: root/common/vgui_surfacelib/linuxfont.h
blob: 33cf4f8d775fc2f483115da0ce671f152fb8089f (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#ifndef LINUXFONT_H
#define LINUXFONT_H

#include "utlrbtree.h"
#include <tier0/memdbgoff.h>
#include <tier0/memdbgon.h>
#include "tier1/strtools.h"
#include "tier1/utlstring.h"

#include <ft2build.h>
#include FT_FREETYPE_H


//-----------------------------------------------------------------------------
// Purpose: encapsulates a OSX font
//-----------------------------------------------------------------------------
class CLinuxFont
{
public:
	CLinuxFont();
	~CLinuxFont();

	// creates the font from windows.  returns false if font does not exist in the OS.
	virtual bool CreateFromMemory(const char *windowsFontName, void *data, int size, int tall, int weight, int blur, int scanlines, int flags);

	// writes the char into the specified 32bpp texture
	virtual void GetCharRGBA( wchar_t ch, int rgbaWide, int rgbaTall, unsigned char *rgba);

	// returns true if the font is equivalent to that specified
	virtual bool IsEqualTo(const char *windowsFontName, int tall, int weight, int blur, int scanlines, int flags);

	// returns true only if this font is valid for use
	virtual bool IsValid();

	// gets the abc widths for a character
	//  A spacing is the distance to add to the current position before drawing the character glyph.
	//  B spacing is the width of the drawn portion of the glyph.
	//  C spacing is the distance to add to the current position to provide white space to the right of the glyph.
	virtual void GetCharABCWidths(int ch, int &a, int &b, int &c);

	// set the font to be the one to currently draw with in the gdi
	void *SetAsActiveFont( void *glContext );

	// returns the height of the font, in pixels
	virtual int GetHeight();

	// returns the requested height of the font.
	virtual int GetHeightRequested();

	// returns the ascent of the font, in pixels (ascent=units above the base line)
	virtual int GetAscent();

	// returns the maximum width of a character, in pixels
	virtual int GetMaxCharWidth();

	// returns the flags used to make this font
	virtual int GetFlags();

	// returns true if this font is underlined
	virtual bool GetUnderlined() { return m_bUnderlined; }
	
	// gets the name of this font
	const char *GetName() { return m_szName.String(); }
	const char *GetFamilyName() { return m_face ? m_face->family_name : NULL; }

	// gets the weight of the font
	virtual int GetWeight() { return m_iWeight; }
	
	bool HasChar(wchar_t wch);

	// gets the width of ch given its position around before and after chars
	virtual void GetKernedCharWidth( wchar_t ch, wchar_t chBefore, wchar_t chAfter, float &wide, float &abcA, float &abcC );

#ifdef DBGFLAG_VALIDATE
	void Validate( CValidator &validator, char *pchName );
#endif

	// Given a font name from windows, match it to the filename and return that.
	static char *GetFontFileName(const char *windowsFontName, int flags);


protected:
	CUtlString m_szName;
	int m_iTall;
	int m_iWeight;
	int m_iFlags;
	bool m_bAntiAliased;
	bool m_bRotary;
	bool m_bAdditive;
	int m_iDropShadowOffset;
	bool m_bUnderlined;
	int m_iOutlineSize;

	int m_iHeight;
	int m_iHeightRequested;
	int m_iMaxCharWidth;
	int m_iAscent;

	int m_iScanLines;
	int m_iBlur;

private:
	static void CreateFontList();
	// abc widths
	struct abc_t
	{
		short b;
		char a;
		char c;
	};

	// cache for storing asian abc widths (since it's too big too just store them all)
	struct abc_cache_t
	{
		wchar_t wch;
		abc_t abc;
	};

	
	CUtlRBTree<abc_cache_t, unsigned short> m_ExtendedABCWidthsCache;
	static bool ExtendedABCWidthsCacheLessFunc(const abc_cache_t &lhs, const abc_cache_t &rhs);

	// cache for storing asian abc widths (since it's too big too just store them all)
	struct kernedSize
	{
		float wide;
	};
	
	struct kerned_abc_cache_t
	{
		wchar_t wch;
		wchar_t wchBefore;
		wchar_t wchAfter; 
		kernedSize abc;
	};
	
	CUtlRBTree<kerned_abc_cache_t, unsigned short> m_ExtendedKernedABCWidthsCache;
	static bool ExtendedKernedABCWidthsCacheLessFunc(const kerned_abc_cache_t &lhs, const kerned_abc_cache_t &rhs);

	bool m_faceValid;
	FT_Face m_face;

	struct font_name_entry
	{
		font_name_entry()
		{
			m_pchFile = NULL;
			m_pchFriendlyName = NULL;
		}
		
		char *m_pchFile;
		char *m_pchFriendlyName;
		bool operator<( const font_name_entry &rhs ) const
		{
			return V_stricmp( rhs.m_pchFriendlyName, m_pchFriendlyName ) > 0;
		}
	};
	static CUtlRBTree< font_name_entry > m_FriendlyNameCache;
	static bool ms_bSetFriendlyNameCacheLessFunc;
};

#endif // LINUXFONT_H