aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/Common/UI/UIFontData.h
blob: b7e38ffa08a8d8420e29d8f06ce56c2b30ce7ec7 (plain)
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
#pragma once

#include <unordered_map>

using namespace std;

#define _DEBUG_BLOCK_CHARS 0

// For hardcoded font data.
struct SFontData
{
public:
	static const unsigned short FONTCOLS = 23;
	static const unsigned short FONTROWS = 20;

	static const unsigned short FONTSIZE = FONTCOLS * FONTROWS;

public:
	// Font name.
	string				m_strFontName;

	// Filename of the glyph archive.
	wstring				m_wstrFilename;

	// Number of glyphs in the archive.
	unsigned int		m_uiGlyphCount;

	// Unicode values of each glyph.
	unsigned short		*m_arrCodepoints;

	// X resolution of glyph archive.
	unsigned int		m_uiGlyphMapX;

	// Y resolution of glyph archive.
	unsigned int		m_uiGlyphMapY;

	// Number of columns in the glyph archive.
	unsigned int		m_uiGlyphMapCols;

	// Number of rows in the glyph archive.
	unsigned int		m_uiGlyphMapRows;

	// Width of each glyph.
	unsigned int		m_uiGlyphWidth;

	// Height of each glyph.
	unsigned int		m_uiGlyphHeight;

	// Ascent of each glyph above the baseline (units?).
	float				m_fAscent;

	// Descent of each glyph below the baseline (units?).
	float				m_fDescent;

	// How much to advance for each pixel wide the glyph is.
	float				m_fAdvPerPixel;

	// How many pixels wide any whitespace characters are.
	unsigned int		m_uiWhitespaceWidth;

public:
	static unsigned short	Codepoints[FONTSIZE];
	static SFontData		Mojangles_7;
	static SFontData		Mojangles_11;
};

// Provides a common interface for dealing with font data.
class CFontData
{
public:
	CFontData();

	// pbRawImage consumed by constructor.
	CFontData(SFontData &sFontData, int *pbRawImage);

	// Release memory.
	void				release();

protected:

	// Hardcoded font data.
	SFontData			*m_sFontData;

	// Map Unicodepoints to glyph ids.
	unordered_map<unsigned int, unsigned short> m_unicodeMap;

	// Kerning value for each glyph.
	unsigned short		*m_kerningTable;

	// Binary blob of the archive image.
	unsigned char		*m_pbRawImage;

	// Total advance of each character.
	float				*m_pfAdvanceTable;

public:

	// Accessor for the font name in the internal SFontData.
	const string		getFontName();

	// Accessor for the hardcoded internal font data.
	SFontData			*getFontData();

	// Get the glyph id corresponding to a unicode point.
	unsigned short		getGlyphId(unsigned int unicodepoint);

	// Get the unicodepoint corresponding to a glyph id.
	unsigned int		getUnicode(unsigned short glyphId);

	// Get a pointer to the top left pixel of a row/column in the raw image.
	unsigned char		*topLeftPixel(int row, int col);

	// Get the row and column where a glyph appears in the archive.
	void				getPos(unsigned short gyphId, int &row, int &col);

	// Get the advance of this character (units?).
	float				getAdvance(unsigned short glyphId);

	// Get the width (in pixels) of a given character.
	int					getWidth(unsigned short glyphId);

	// Returns true if this glyph is whitespace.
	bool				glyphIsWhitespace(unsigned short glyphId);
	
	// Returns true if this unicodepoint is whitespace
	bool				unicodeIsWhitespace(unsigned int unicodepoint);

private:

	// Move a pointer in an image dx pixels right and dy pixels down, wrap around in either dimension leads to unknown behaviour.
	void				moveCursor(unsigned char *&cursor, unsigned int dx, unsigned int dy);
};