-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfont.c
425 lines (358 loc) · 11.4 KB
/
font.c
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
File: BMFont.c
Project: Platform Development Tools
Date: Sept. 24, 2014
Author: Matt Slevinsky
Based on http://www.angelcode.com/dev/bmfonts/
Description: Bitmap Font Library that is compatible with angelcode's BMF compiler
*/
#include <png/png.h>
#include "font.h"
#pragma pack(push)
#pragma pack(1)
typedef struct InfoBlock {
uint16 fontSize;
uint8 reserved:4;
uint8 bold:1;
uint8 italic:1;
uint8 unicode:1;
uint8 smooth:1;
uint8 charSet;
uint16 stretchH;
uint8 aa;
uint8 paddingUp;
uint8 paddingRight;
uint8 paddingDown;
uint8 paddingLeft;
uint8 spacingHoriz;
uint8 spacingVert;
uint8 outline; // Added with version 2
char fontName[];
} InfoBlock;
#pragma pack(pop)
#pragma pack(push)
#pragma pack(1)
typedef struct CommonBlock
{
uint16 lineHeight;
uint16 base;
uint16 scaleW;
uint16 scaleH;
uint16 pages;
uint8 packed :1;
uint8 reserved:7;
uint8 alphaChnl;
uint8 redChnl;
uint8 greenChnl;
uint8 blueChnl;
} CommonBlock;
#pragma pack(pop)
#pragma pack(push)
#pragma pack(1)
typedef struct CharInfo
{
uint32 id;
uint16 x;
uint16 y;
uint16 width;
uint16 height;
int16 xoffset;
int16 yoffset;
int16 xadvance;
uint8 page;
uint8 chnl;
} CharInfo;
#pragma pack(pop)
void readInfoBlock(uint32 fd, Font* font, int blockSize, int* outlineThickness) {
InfoBlock* infoBlock = malloc(blockSize);
fs_read(fd, infoBlock, blockSize);
printf("INFO: Loading font %s\n", infoBlock -> fontName);
printf("INFO: Font Size : %i\n", infoBlock -> fontSize);
printf("INFO: Bold : %s\n", infoBlock -> bold ? "true" : "false");
printf("INFO: Italic : %s\n", infoBlock -> italic ? "true" : "false");
printf("INFO: Unicode : %s\n", infoBlock -> unicode ? "true" : "false");
printf("INFO: Smooth : %s\n", infoBlock -> smooth ? "true" : "false");
printf("INFO: Outline : %i\n", infoBlock -> outline);
// We're only interested in the outline thickness
*outlineThickness = infoBlock -> outline;
free(infoBlock);
}
void bmfpage_copy(void *_dst, const void *_src) {
FontPage *dst = (FontPage*)_dst, *src = (FontPage*)_src;
dst->width = src->width;
dst->height = src->height;
dst->texture = src->texture;
//TODO : Reference count and release texture when refcount = 0
}
void bmfpage_dtor(void *_elt) {
//MS - do we need this?
//pvr_mem_free(elt -> texture);
}
UT_icd bmfpage_icd = {sizeof(FontPage), NULL, bmfpage_copy, bmfpage_dtor};
void readCommonBlock(uint32 fd, Font* font, int blockSize, int* outlineThickness) {
CommonBlock* commonBlock = malloc(sizeof(CommonBlock));
fs_read(fd, commonBlock, sizeof(CommonBlock));
font -> fontHeight = commonBlock -> lineHeight;
font -> base = commonBlock -> base;
font -> scaleW = commonBlock -> scaleW;
font -> scaleH = commonBlock -> scaleH;
utarray_new(font -> pages, &bmfpage_icd);
if ((commonBlock -> packed != 0) &&
(*outlineThickness != 0))
font -> hasOutline = 1;
free(commonBlock);
}
void readPage(Font* font, char* pagePath) {
uint32 w,h;
FontPage* page = (FontPage*)malloc(sizeof(FontPage));
png_load_texture(pagePath, &(page -> texture), PNG_FULL_ALPHA, &w, &h);
page -> width = (uint16)w;
page -> height = (uint16)h;
utarray_push_back(font -> pages, page);
}
void readPagesBlock(uint32 fd, Font* font, int blockSize) {
char* pageNames = malloc(blockSize);
char* pagePtr = pageNames;
fs_read(fd, pageNames, blockSize);
int id = 0;
int bytesProcessed = 0;
int temp;
do {
char* pngPath = malloc((int)strlen(pagePtr) + 5);
strcpy(pngPath, "/rd/"); // TODO : strip leading path from font file and look in the same location
strcat(pngPath, pagePtr);
printf("Loading texture %i:%s\n", id, pngPath);
readPage(font, pngPath);
temp = 1 + (int)strlen(pagePtr);
pagePtr += temp;
bytesProcessed += temp;
free(pngPath);
id++;
} while (bytesProcessed < blockSize);
free(pageNames);
}
void addCharacter(Font* font, CharInfo charInfo) {
//printf("Adding character mapping for character %c\n", (char)charInfo.id);
CharDescriptor* bmfChar = malloc(sizeof(CharDescriptor));
bmfChar -> id = charInfo.id;
bmfChar -> srcX = charInfo.x;
bmfChar -> srcY = charInfo.y;
bmfChar -> srcW = charInfo.width;
bmfChar -> srcH = charInfo.height;
bmfChar -> xOff = charInfo.xoffset;
bmfChar -> yOff = charInfo.yoffset;
bmfChar -> xAdv = charInfo.xadvance;
bmfChar -> page = charInfo.page;
bmfChar -> chnl = charInfo.chnl;
utarray_new(bmfChar -> kerningPairs, &ut_int_icd);
if (charInfo.id == -1) {
font -> defChar = bmfChar;
}
CharDescriptor** characters = &(font -> characters);
HASH_ADD_INT(*characters, id, bmfChar);
}
void readCharsBlock(uint32 fd, Font* font, int blockSize) {
CharInfo* charInfos = malloc(blockSize);
fs_read(fd, charInfos, blockSize);
font -> characters = NULL;
int i;
for (i = 0; (i * sizeof(CharInfo)) < blockSize; i++) {
addCharacter(font, charInfos[i]);
}
free(charInfos);
}
void addKerningPair(Font* font, KerningPair kerningPair) {
if (kerningPair.first >= 0 && kerningPair.first < 256) {
CharDescriptor** characters = &(font -> characters);
CharDescriptor* descriptor;
HASH_FIND_INT(*characters, &(kerningPair.first), descriptor);
if (descriptor != NULL) {
//printf("Adding kerning pair for character %c\n", (char)descriptor -> id);
utarray_push_back(descriptor -> kerningPairs, &kerningPair.second);
utarray_push_back(descriptor -> kerningPairs, &kerningPair.amount);
}
}
}
void readKerningPairsBlock(uint32 fd, Font* font, int blockSize) {
KerningPair* kerningPairs = malloc(blockSize);
fs_read(fd, kerningPairs, blockSize);
int i;
for (i = 0; (i * sizeof(KerningPair)) < blockSize; i++) {
addKerningPair(font, kerningPairs[i]);
}
free(kerningPairs);
}
Font* load_font(const char* filepath) {
uint32 fd;
Font* font = NULL;
int outlineThickness;
fd = fs_open(filepath, O_RDONLY);
if (fd > 0) {
printf("Opened file path [%s] successfully for reading\n", filepath);
char magicString[4];
if (fs_read(fd, magicString, 4) == 4) {
if (strncmp(magicString, "BMF\003", 4) != 0) {
printf("Unrecognized format for %s detected. Aborting font load.\n", filepath);
fs_close(fd);
return NULL;
}
font = malloc(sizeof(Font));
char blockType;
int blockSize;
while (fs_read(fd, &blockType, 1)) {
// Read the blockSize
fs_read(fd, &blockSize, 4);
switch( blockType ) {
case 1: // info
printf("Detected info block\n");
readInfoBlock(fd, font, blockSize, &outlineThickness);
break;
case 2: // common
printf("Detected common block\n");
readCommonBlock(fd, font, blockSize, &outlineThickness);
break;
case 3: // pages
printf("Detected pages block\n");
readPagesBlock(fd, font, blockSize);
break;
case 4: // chars
printf("Detected chars block\n");
readCharsBlock(fd, font, blockSize);
break;
case 5: // kerning pairs
printf("Detected kerning pairs block\n");
readKerningPairsBlock(fd, font, blockSize);
break;
default:
printf("Unexpected block type (%d) detected. Aborting font load.\n", blockType);
free(font);
fs_close(fd);
return NULL;
}
}
}
} else {
printf("Unable to open file path [%s] for reading\n", filepath);
}
printf("Font Loaded successfully.\n");
return font;
}
void deleteAllDescriptors(CharDescriptor** root) {
CharDescriptor* currentDescriptor;
CharDescriptor* tmp;
HASH_ITER(hh, *root, currentDescriptor, tmp) {
utarray_free(currentDescriptor->kerningPairs);
HASH_DEL(*root,currentDescriptor); /* delete; users advances to next */
free(currentDescriptor); /* optional- if you want to free */
}
}
void deleteAllPages(Font* font) {
FontPage* page;
for (page=(FontPage*)utarray_front(font -> pages);
page!=NULL;
page=(FontPage*)utarray_next(font -> pages,page)) {
pvr_mem_free(page -> texture);
}
utarray_free(font -> pages);
}
void destroy_font(Font* font) {
font -> defChar = NULL;
deleteAllDescriptors(&(font->characters));
deleteAllPages(font);
}
void measureLine(Font* font, char* string, float scale, float* measuredWidth) {
char* ptr = string;
float accX = 0.0f;
CharDescriptor** descriptors = &(font -> characters);
CharDescriptor* descriptor;
while (((*ptr) != 0) && ((*ptr) != '\n')) {
uint32 id = (uint32)*ptr;
HASH_FIND_INT(*descriptors, &id, descriptor);
if (descriptor != NULL) {
accX += (descriptor -> xAdv * scale);
} else {
accX += (font -> defChar -> xAdv * scale);
}
ptr++;
}
*measuredWidth = accX;
}
void drawChar(CharDescriptor* descriptor, float xPos, float yPos, float zPos, int texWidth, int texHeight, uint32 color, float scale) {
pvr_vertex_t vert;
float u1, v1, u2, v2, yDatum;
yDatum = yPos + (scale * (float)descriptor -> yOff);
u1 = (float)descriptor -> srcX / (float)texWidth;
v1 = (float)descriptor -> srcY / (float)texHeight;
u2 = ((float)descriptor -> srcX + descriptor -> srcW) / (float)texWidth;
v2 = ((float)descriptor -> srcY + descriptor -> srcH) / (float)texHeight;
vert.flags = PVR_CMD_VERTEX;
vert.x = xPos;
vert.y = yDatum + ((float)descriptor -> srcH * scale);
vert.z = zPos;
vert.u = u1;
vert.v = v2;
vert.argb = color;
vert.oargb = 0;
pvr_prim(&vert, sizeof(vert));
vert.y = yDatum;
vert.u = u1;
vert.v = v1;
pvr_prim(&vert, sizeof(vert));
vert.x = xPos + ((float)descriptor -> srcW * scale);
vert.y = yDatum + ((float)descriptor -> srcH * scale);
vert.u = u2;
vert.v = v2;
pvr_prim(&vert, sizeof(vert));
vert.flags = PVR_CMD_VERTEX_EOL;
vert.y = yDatum;
vert.u = u2;
vert.v = v1;
pvr_prim(&vert, sizeof(vert));
}
void draw_string(Font* font, int currentList, char* string,
float xPos, float yPos, float zPos, float width, float height,
LineRenderMode lineRenderMode, TextAlignmentMode textAlignmentMode, uint32 color, float scale) {
pvr_poly_cxt_t cxt;
pvr_poly_hdr_t hdr;
char* ptr = string;
FontPage* page = NULL;
int currentPage = -1;
float drawX = xPos;
if (textAlignmentMode == RIGHT) {
float measuredWidth;
measureLine(font, string, scale, &measuredWidth);
drawX = xPos + width - measuredWidth;
}
CharDescriptor** descriptors = &(font -> characters);
CharDescriptor* descriptor;
while ((*ptr) != 0) {
uint32 id = (uint32)*ptr;
HASH_FIND_INT(*descriptors, &id, descriptor);
if (descriptor != NULL) {
if (descriptor -> page != currentPage) {
//printf("Selected texture at index %i\n", descriptor -> page);
page = (FontPage*)utarray_eltptr(font -> pages, descriptor -> page);
if (page != NULL) {
currentPage = descriptor -> page;
//printf("Load font texture of resolution %i x %i\n", (short)page -> width, (short)page -> height);
pvr_poly_cxt_col(&cxt, currentList);
pvr_poly_cxt_txr(&cxt, currentList, PVR_TXRFMT_ARGB4444 | PVR_TXRFMT_TWIDDLED, page -> width, page -> height, page -> texture, PVR_FILTER_BILINEAR);
pvr_poly_compile(&hdr, &cxt);
pvr_prim(&hdr, sizeof(hdr));
} else {
printf("Failed to load page at index %i\n", descriptor -> page);
}
}
if (currentPage != -1) {
drawChar(descriptor, drawX, yPos, zPos, page -> width, page -> height, color, scale);
}
drawX += ((float)descriptor -> xAdv * scale);
} else {
drawX += ((float)font -> defChar -> xAdv * scale);
}
if (drawX >= xPos + width) {
break;
}
ptr++;
}
}