CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/Font/PGF.h
Views: 1401
1
// Copyright (c) 2012- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
// Thanks to the JPCSP project! This sceFont implementation is basically a C++ take on JPCSP's font code.
19
20
#pragma once
21
22
#include <string>
23
#include <vector>
24
25
#include "Common/CommonTypes.h"
26
27
class PointerWrap;
28
29
enum {
30
FONT_FILETYPE_PGF = 0x00,
31
FONT_FILETYPE_BWFON = 0x01,
32
};
33
34
enum {
35
FONT_PGF_BMP_H_ROWS = 0x01,
36
FONT_PGF_BMP_V_ROWS = 0x02,
37
FONT_PGF_BMP_OVERLAY = 0x03,
38
// Metric names according to JPCSP findings
39
FONT_PGF_METRIC_DIMENSION_INDEX = 0x04,
40
FONT_PGF_METRIC_BEARING_X_INDEX = 0x08,
41
FONT_PGF_METRIC_BEARING_Y_INDEX = 0x10,
42
FONT_PGF_METRIC_ADVANCE_INDEX = 0x20,
43
FONT_PGF_CHARGLYPH = 0x20,
44
FONT_PGF_SHADOWGLYPH = 0x40,
45
};
46
47
enum Family {
48
FONT_FAMILY_SANS_SERIF = 1,
49
FONT_FAMILY_SERIF = 2,
50
};
51
52
enum Style {
53
FONT_STYLE_REGULAR = 1,
54
FONT_STYLE_ITALIC = 2,
55
FONT_STYLE_BOLD = 5,
56
FONT_STYLE_BOLD_ITALIC = 6,
57
FONT_STYLE_DB = 103, // Demi-Bold / semi-bold
58
};
59
60
enum Language {
61
FONT_LANGUAGE_JAPANESE = 1,
62
FONT_LANGUAGE_LATIN = 2,
63
FONT_LANGUAGE_KOREAN = 3,
64
FONT_LANGUAGE_CHINESE = 4,
65
};
66
67
enum FontPixelFormat {
68
PSP_FONT_PIXELFORMAT_4 = 0, // 2 pixels packed in 1 byte (natural order)
69
PSP_FONT_PIXELFORMAT_4_REV = 1, // 2 pixels packed in 1 byte (reversed order)
70
PSP_FONT_PIXELFORMAT_8 = 2, // 1 pixel in 1 byte
71
PSP_FONT_PIXELFORMAT_24 = 3, // 1 pixel in 3 bytes (RGB)
72
PSP_FONT_PIXELFORMAT_32 = 4, // 1 pixel in 4 bytes (RGBA)
73
};
74
75
76
struct PGFFontStyle {
77
float_le fontH;
78
float_le fontV;
79
float_le fontHRes;
80
float_le fontVRes;
81
float_le fontWeight;
82
u16_le fontFamily;
83
u16_le fontStyle;
84
// Check.
85
u16_le fontStyleSub;
86
u16_le fontLanguage;
87
u16_le fontRegion;
88
u16_le fontCountry;
89
char fontName[64];
90
char fontFileName[64];
91
u32_le fontAttributes;
92
u32_le fontExpire;
93
};
94
95
96
struct Glyph {
97
int w;
98
int h;
99
int left;
100
int top;
101
int flags;
102
int shadowFlags;
103
int shadowID;
104
int advanceH;
105
int advanceV;
106
int dimensionWidth, dimensionHeight;
107
int xAdjustH, xAdjustV;
108
int yAdjustH, yAdjustV;
109
u32 ptr;
110
};
111
112
113
#if COMMON_LITTLE_ENDIAN
114
typedef FontPixelFormat FontPixelFormat_le;
115
#else
116
typedef swap_struct_t<FontPixelFormat, swap_32_t<FontPixelFormat> > FontPixelFormat_le;
117
#endif
118
119
struct GlyphImage {
120
FontPixelFormat_le pixelFormat;
121
s32_le xPos64;
122
s32_le yPos64;
123
u16_le bufWidth;
124
u16_le bufHeight;
125
u16_le bytesPerLine;
126
u16_le pad;
127
u32_le bufferPtr;
128
};
129
130
#pragma pack(push,1)
131
struct PGFHeader
132
{
133
u16_le headerOffset;
134
u16_le headerSize;
135
136
char PGFMagic[4];
137
s32_le revision;
138
s32_le version;
139
140
s32_le charMapLength;
141
s32_le charPointerLength;
142
s32_le charMapBpe;
143
s32_le charPointerBpe;
144
145
// TODO: This has values in it (0404)...
146
u8 pad1[2];
147
u8 bpp;
148
u8 pad2[1];
149
150
s32_le hSize;
151
s32_le vSize;
152
s32_le hResolution;
153
s32_le vResolution;
154
155
u8 pad3[1];
156
char fontName[64];
157
char fontType[64];
158
u8 pad4[1];
159
160
u16_le firstGlyph;
161
u16_le lastGlyph;
162
163
// TODO: This has a few 01s in it in the official fonts.
164
u8 pad5[26];
165
166
s32_le maxAscender;
167
s32_le maxDescender;
168
s32_le maxLeftXAdjust;
169
s32_le maxBaseYAdjust;
170
s32_le minCenterXAdjust;
171
s32_le maxTopYAdjust;
172
173
s32_le maxAdvance[2];
174
s32_le maxSize[2];
175
u16_le maxGlyphWidth;
176
u16_le maxGlyphHeight;
177
u8 pad6[2];
178
179
u8 dimTableLength;
180
u8 xAdjustTableLength;
181
u8 yAdjustTableLength;
182
u8 advanceTableLength;
183
u8 pad7[102];
184
185
s32_le shadowMapLength;
186
s32_le shadowMapBpe;
187
float_le unknown1;
188
s32_le shadowScale[2];
189
u8 pad8[8];
190
};
191
192
struct PGFHeaderRev3Extra {
193
s32_le compCharMapBpe1;
194
s32_le compCharMapLength1;
195
s32_le compCharMapBpe2;
196
s32_le compCharMapLength2;
197
u32_le unknown;
198
};
199
200
struct PGFCharInfo {
201
u32_le bitmapWidth;
202
u32_le bitmapHeight;
203
u32_le bitmapLeft;
204
u32_le bitmapTop;
205
// Glyph metrics (in 26.6 signed fixed-point).
206
u32_le sfp26Width;
207
u32_le sfp26Height;
208
s32_le sfp26Ascender;
209
s32_le sfp26Descender;
210
s32_le sfp26BearingHX;
211
s32_le sfp26BearingHY;
212
s32_le sfp26BearingVX;
213
s32_le sfp26BearingVY;
214
s32_le sfp26AdvanceH;
215
s32_le sfp26AdvanceV;
216
s16_le shadowFlags;
217
s16_le shadowId;
218
};
219
220
struct PGFFontInfo {
221
// Glyph metrics (in 26.6 signed fixed-point).
222
s32_le maxGlyphWidthI;
223
s32_le maxGlyphHeightI;
224
s32_le maxGlyphAscenderI;
225
s32_le maxGlyphDescenderI;
226
s32_le maxGlyphLeftXI;
227
s32_le maxGlyphBaseYI;
228
s32_le minGlyphCenterXI;
229
s32_le maxGlyphTopYI;
230
s32_le maxGlyphAdvanceXI;
231
s32_le maxGlyphAdvanceYI;
232
233
// Glyph metrics (replicated as float).
234
float_le maxGlyphWidthF;
235
float_le maxGlyphHeightF;
236
float_le maxGlyphAscenderF;
237
float_le maxGlyphDescenderF;
238
float_le maxGlyphLeftXF;
239
float_le maxGlyphBaseYF;
240
float_le minGlyphCenterXF;
241
float_le maxGlyphTopYF;
242
float_le maxGlyphAdvanceXF;
243
float_le maxGlyphAdvanceYF;
244
245
// Bitmap dimensions.
246
s16_le maxGlyphWidth;
247
s16_le maxGlyphHeight;
248
s32_le numGlyphs;
249
s32_le shadowMapLength; // Number of elements in the font's shadow charmap.
250
251
// Font style (used by font comparison functions).
252
PGFFontStyle fontStyle;
253
254
u8 BPP; // Font's BPP.
255
u8 pad[3];
256
};
257
258
#pragma pack(pop)
259
260
class PGF {
261
public:
262
PGF();
263
~PGF();
264
265
bool ReadPtr(const u8 *ptr, size_t dataSize);
266
267
bool GetCharInfo(int charCode, PGFCharInfo *ci, int altCharCode, int glyphType = FONT_PGF_CHARGLYPH) const;
268
void GetFontInfo(PGFFontInfo *fi) const;
269
void DrawCharacter(const GlyphImage *image, int clipX, int clipY, int clipWidth, int clipHeight, int charCode, int altCharCode, int glyphType) const;
270
271
void DoState(PointerWrap &p);
272
273
PGFHeader header;
274
275
private:
276
bool ReadCharGlyph(const u8 *fontdata, size_t charPtr, Glyph &glyph);
277
bool ReadShadowGlyph(const u8 *fontdata, size_t charPtr, Glyph &glyph);
278
bool GetCharGlyph(int charCode, int glyphType, Glyph &glyph) const;
279
280
// Unused
281
int GetCharIndex(int charCode, const std::vector<int> &charmapCompressed);
282
283
void SetFontPixel(u32 base, int bpl, int bufWidth, int bufHeight, int x, int y, u8 pixelColor, FontPixelFormat pixelformat) const;
284
285
PGFHeaderRev3Extra rev3extra;
286
287
// Font character image data
288
u8 *fontData;
289
size_t fontDataSize;
290
291
std::string fileName;
292
293
std::vector<int> dimensionTable[2];
294
std::vector<int> xAdjustTable[2];
295
std::vector<int> yAdjustTable[2];
296
std::vector<int> advanceTable[2];
297
298
// Unused
299
std::vector<int> charmapCompressionTable1[2];
300
std::vector<int> charmapCompressionTable2[2];
301
302
std::vector<int> charmap_compr;
303
std::vector<int> charmap;
304
305
std::vector<Glyph> glyphs;
306
std::vector<Glyph> shadowGlyphs;
307
int firstGlyph;
308
};
309
310