-
Notifications
You must be signed in to change notification settings - Fork 9
/
font.c
638 lines (527 loc) · 18.6 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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/* Font-drawing engine
Version: 1.22
Last updated: 2018/12/08 */
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <kernel.h>
#include <wchar.h>
#include <libgs.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_ADVANCES_H
#include "graphics.h"
#include "font.h"
/* Draw the glyphs as close as possible to each other, to save VRAM.
C: character
X: Horizontal frontier
Y: Vertical frontier
I: Initial state, no frontier.
Initial state:
IIII
IIII
IIII
IIII
After one character is added to an empty atlas, initialize the X and Y frontier.
CXXX
YXXX
YXXX
YXXX
After one more character is added:
CCXX
YYXX
YYXX
YYXX
After one more character is added:
CCCX
YYYX
YYYX
YYYX
After one more character is added:
CCCC
YYYY
YYYY
YYYY
After one more character is added:
CCCC
CXXX
YXXX
YXXX */
struct FontGlyphSlotInfo
{
struct FontGlyphSlot *slot;
u32 vram; //Address of the atlas buffer in VRAM
};
struct FontGlyphSlot
{
wint_t character;
unsigned short int VramPageX, VramPageY;
short int top, left; //Offsets to place the glyph at. Refer to the FreeType documentation.
short int advance_x, advance_y;
unsigned short int width, height;
};
struct FontFrontier
{
short int x, y;
short int width, height;
};
struct FontAtlas
{
u32 vram; //Address of the buffer in VRAM
void *buffer; //Address of the buffer in local memory
unsigned int NumGlyphs;
struct FontGlyphSlot *GlyphSlot;
struct FontFrontier frontier[2];
};
typedef struct Font
{
FT_Face FTFace;
GS_IMAGE Texture;
GS_IMAGE Clut;
void *ClutMem;
unsigned short int IsLoaded;
unsigned short int IsInit;
struct FontAtlas atlas[FNT_MAX_ATLASES];
} Font_t;
static FT_Library FTLibrary;
static Font_t GS_FTFont;
static Font_t GS_sub_FTFont;
static int ResetThisFont(struct UIDrawGlobal *gsGlobal, Font_t *font)
{
struct FontAtlas *atlas;
unsigned short int i;
int result, vram;
result = 0;
font->Clut.x = 0;
font->Clut.y = 0;
font->Clut.width = 16;
font->Clut.height = 16;
font->Clut.vram_width = 1; //width (16) / 64 = 1
vram = GsVramAllocTextureBuffer(font->Clut.width, font->Clut.height, font->Clut.psm);
if (vram >= 0)
{
font->Clut.vram_addr = (u16)vram;
UploadClut(gsGlobal, &font->Clut, font->ClutMem);
font->Texture.x = 0;
font->Texture.y = 0;
for (i = 0, atlas = font->atlas; i < FNT_MAX_ATLASES; i++, atlas++)
{
atlas->frontier[0].width = 0;
atlas->frontier[0].height = 0;
atlas->frontier[1].width = 0;
atlas->frontier[1].height = 0;
atlas->vram = -1;
if (atlas->GlyphSlot != NULL)
{
free(atlas->GlyphSlot);
atlas->GlyphSlot = NULL;
}
if (atlas->buffer != NULL)
{
free(atlas->buffer);
atlas->buffer = NULL;
}
atlas->NumGlyphs = 0;
}
}
else
{
printf("Font: error - unable to allocate VRAM for CLUT.\n");
result = -1;
}
return result;
}
int FontReset(struct UIDrawGlobal *gsGlobal)
{
int result;
if ((result = ResetThisFont(gsGlobal, &GS_FTFont)) == 0)
result = ResetThisFont(gsGlobal, &GS_sub_FTFont);
return 0;
}
static int InitFontSupportCommon(struct UIDrawGlobal *gsGlobal, Font_t *font)
{
int result;
unsigned short int i;
if ((result = FT_Set_Pixel_Sizes(font->FTFace, FNT_CHAR_WIDTH, FNT_CHAR_HEIGHT)) == 0)
{
font->Texture.width = FNT_ATLAS_WIDTH;
font->Texture.height = FNT_ATLAS_HEIGHT;
font->Texture.psm = GS_TEX_8;
font->Clut.psm = GS_CLUT_32;
// generate the clut table
u32 *clut = memalign(64, 256 * 4);
font->ClutMem = clut;
for (i = 0; i < 256; ++i)
clut[i] = (i > 0x80 ? 0x80 : i) << 24 | i << 16 | i << 8 | i;
SyncDCache(clut, (void *)((unsigned int)clut + 256 * 4));
if (!font->IsInit)
ResetThisFont(gsGlobal, font);
font->IsLoaded = 1;
font->IsInit = 1;
}
return result;
}
int FontInit(struct UIDrawGlobal *gsGlobal, const char *FontFile)
{
int result;
if ((result = FT_Init_FreeType(&FTLibrary)) == 0)
{
if ((result = FT_New_Face(FTLibrary, FontFile, 0, &GS_FTFont.FTFace)) == 0)
result = InitFontSupportCommon(gsGlobal, &GS_FTFont);
if (result != 0)
FontDeinit();
}
return result;
}
int FontInitWithBuffer(struct UIDrawGlobal *gsGlobal, void *buffer, unsigned int size)
{
int result;
if ((result = FT_Init_FreeType(&FTLibrary)) == 0)
{
if ((result = FT_New_Memory_Face(FTLibrary, buffer, size, 0, &GS_FTFont.FTFace)) == 0)
result = InitFontSupportCommon(gsGlobal, &GS_FTFont);
if (result != 0)
FontDeinit();
}
return result;
}
static void UnloadFont(Font_t *font)
{
struct FontAtlas *atlas;
unsigned int i;
if (font->IsLoaded)
FT_Done_Face(font->FTFace);
for (i = 0, atlas = font->atlas; i < FNT_MAX_ATLASES; i++, atlas++)
{
if (atlas->buffer != NULL)
{
free(atlas->buffer);
atlas->buffer = NULL;
}
if (atlas->GlyphSlot != NULL)
{
free(atlas->GlyphSlot);
atlas->GlyphSlot = NULL;
}
}
font->atlas->NumGlyphs = 0;
if (font->ClutMem != NULL)
{
free(font->ClutMem);
font->ClutMem = NULL;
}
font->IsLoaded = 0;
}
int AddSubFont(struct UIDrawGlobal *gsGlobal, const char *FontFile)
{
int result;
if ((result = FT_New_Face(FTLibrary, FontFile, 0, &GS_sub_FTFont.FTFace)) == 0)
result = InitFontSupportCommon(gsGlobal, &GS_sub_FTFont);
if (result != 0)
UnloadFont(&GS_sub_FTFont);
return result;
}
int AddSubFontWithBuffer(struct UIDrawGlobal *gsGlobal, void *buffer, unsigned int size)
{
int result;
if ((result = FT_New_Memory_Face(FTLibrary, buffer, size, 0, &GS_sub_FTFont.FTFace)) == 0)
result = InitFontSupportCommon(gsGlobal, &GS_sub_FTFont);
if (result != 0)
UnloadFont(&GS_sub_FTFont);
return result;
}
//VRAM stays allocated.
void FontDeinit(void)
{
UnloadFont(&GS_sub_FTFont);
}
static int AtlasInit(Font_t *font, struct FontAtlas *atlas)
{
unsigned int TextureSizeEE, i;
short int width_aligned, height_aligned;
int result;
result = 0;
width_aligned = (font->Texture.width + 127) & ~127;
height_aligned = (font->Texture.height + 127) & ~127;
font->Texture.vram_width = width_aligned / 64;
if ((atlas->vram = GsVramAllocTextureBuffer(width_aligned, height_aligned, font->Texture.psm)) >= 0)
{
TextureSizeEE = width_aligned * height_aligned;
if ((atlas->buffer = memalign(64, TextureSizeEE)) == NULL)
{
printf("Font: error - unable to allocate memory for atlas.\n");
result = -ENOMEM;
}
memset(atlas->buffer, 0, TextureSizeEE);
SyncDCache(atlas->buffer, atlas->buffer + TextureSizeEE);
}
else
{
printf("Font: error - unable to allocate VRAM for atlas.\n");
result = -ENOMEM;
}
return result;
}
static struct FontGlyphSlot *AtlasAlloc(Font_t *font, struct FontAtlas *atlas, short int width, short int height)
{
struct FontGlyphSlot *GlyphSlot;
GlyphSlot = NULL;
if (atlas->buffer == NULL)
{ //No frontier (empty atlas)
if (AtlasInit(font, atlas) != 0)
return NULL;
//Give the glyph 1px more, for texel rendering.
atlas->frontier[0].width = FNT_ATLAS_WIDTH - (width + 1);
atlas->frontier[0].height = FNT_ATLAS_HEIGHT;
atlas->frontier[0].x = width + 1;
atlas->frontier[0].y = 0;
atlas->frontier[1].width = FNT_ATLAS_WIDTH;
atlas->frontier[1].height = FNT_ATLAS_HEIGHT - (height + 1);
atlas->frontier[1].x = 0;
atlas->frontier[1].y = height + 1;
atlas->NumGlyphs++;
if ((atlas->GlyphSlot = realloc(atlas->GlyphSlot, atlas->NumGlyphs * sizeof(struct FontGlyphSlot))) != NULL)
{
GlyphSlot = &atlas->GlyphSlot[atlas->NumGlyphs - 1];
GlyphSlot->VramPageX = 0;
GlyphSlot->VramPageY = 0;
}
else
{
printf("Font: error - unable to allocate a new glyph slot.\n");
atlas->NumGlyphs = 0;
}
return GlyphSlot;
}
else
{ //We have the frontiers
//Try to allocate from the horizontal frontier first.
if ((atlas->frontier[0].width >= width + 1) && (atlas->frontier[0].height >= height + 1))
{
atlas->NumGlyphs++;
if ((atlas->GlyphSlot = realloc(atlas->GlyphSlot, atlas->NumGlyphs * sizeof(struct FontGlyphSlot))) != NULL)
{
GlyphSlot = &atlas->GlyphSlot[atlas->NumGlyphs - 1];
GlyphSlot->VramPageX = atlas->frontier[0].x;
GlyphSlot->VramPageY = atlas->frontier[0].y;
//Give the glyph 1px more, for texel rendering.
//Update frontier.
atlas->frontier[0].width -= width + 1;
atlas->frontier[0].x += width + 1;
//If the new glyph is a little taller than the glyphs under the horizontal frontier, move the vertical frontier.
if (atlas->frontier[0].y + height + 1 > atlas->frontier[1].y)
atlas->frontier[1].y = atlas->frontier[0].y + height + 1;
}
else
{
printf("Font: error - unable to allocate a new glyph slot.\n");
atlas->NumGlyphs = 0;
}
//Now try the vertical frontier.
}
else if ((atlas->frontier[1].width >= width + 1) && (atlas->frontier[1].height >= height + 1))
{
atlas->NumGlyphs++;
if ((atlas->GlyphSlot = realloc(atlas->GlyphSlot, atlas->NumGlyphs * sizeof(struct FontGlyphSlot))) != NULL)
{
GlyphSlot = &atlas->GlyphSlot[atlas->NumGlyphs - 1];
GlyphSlot->VramPageX = atlas->frontier[1].x;
GlyphSlot->VramPageY = atlas->frontier[1].y;
//Give the glyph 1px more, for texel rendering.
/* Update frontier.
If we got here, it means that the horizontal frontier is very close the edge of VRAM.
Give a large portion of the space recorded under this frontier to the horizontal frontier.
Before: After one more character is added:
CCCC CCCC
YYYY CXXX
YYYY YXXX
YYYY YXXX */
atlas->frontier[0].x = width + 1;
atlas->frontier[0].y = atlas->frontier[1].y;
atlas->frontier[0].width = FNT_ATLAS_WIDTH - (width + 1);
atlas->frontier[0].height = atlas->frontier[1].height;
atlas->frontier[1].height -= height + 1;
atlas->frontier[1].y += height + 1;
}
else
{
printf("Font: error - unable to allocate a new glyph slot.\n");
atlas->NumGlyphs = 0;
}
}
}
return GlyphSlot;
}
static void AtlasCopyFT(Font_t *font, struct FontAtlas *atlas, struct FontGlyphSlot *GlyphSlot, FT_GlyphSlot FT_GlyphSlot)
{
short int yOffset;
unsigned char *FTCharRow;
for (yOffset = 0; yOffset < FT_GlyphSlot->bitmap.rows; yOffset++)
{
FTCharRow = (void *)UNCACHED_SEG(((unsigned int)atlas->buffer + GlyphSlot->VramPageX + (GlyphSlot->VramPageY + yOffset) * font->Texture.width));
memcpy(FTCharRow, &((unsigned char *)FT_GlyphSlot->bitmap.buffer)[yOffset * FT_GlyphSlot->bitmap.width], FT_GlyphSlot->bitmap.width);
}
}
static struct FontGlyphSlot *UploadGlyph(struct UIDrawGlobal *gsGlobal, Font_t *font, wint_t character, FT_GlyphSlot FT_GlyphSlot, struct FontAtlas **AtlasOut)
{
struct FontAtlas *atlas;
struct FontGlyphSlot *GlyphSlot;
unsigned short int i;
*AtlasOut = NULL;
GlyphSlot = NULL;
for (i = 0, atlas = font->atlas; i < FNT_MAX_ATLASES; i++, atlas++)
{
if ((GlyphSlot = AtlasAlloc(font, atlas, FT_GlyphSlot->bitmap.width, FT_GlyphSlot->bitmap.rows)) != NULL)
break;
}
if (GlyphSlot != NULL)
{
GlyphSlot->character = character;
GlyphSlot->left = FT_GlyphSlot->bitmap_left;
GlyphSlot->top = FT_GlyphSlot->bitmap_top;
GlyphSlot->width = FT_GlyphSlot->bitmap.width;
GlyphSlot->height = FT_GlyphSlot->bitmap.rows;
GlyphSlot->advance_x = FT_GlyphSlot->advance.x >> 6;
GlyphSlot->advance_y = FT_GlyphSlot->advance.y >> 6;
*AtlasOut = atlas;
//Initiate a texture flush before reusing the VRAM page, if the slot was just used earlier.
GsTextureFlush();
AtlasCopyFT(font, atlas, GlyphSlot, FT_GlyphSlot);
font->Texture.vram_addr = atlas->vram;
GsLoadImage(atlas->buffer, &font->Texture);
}
else
printf("Font: error - all atlas are full.\n");
return GlyphSlot;
}
static int GetGlyph(struct UIDrawGlobal *gsGlobal, Font_t *font, wint_t character, int DrawMissingGlyph, struct FontGlyphSlotInfo *glyphInfo)
{
int i, slot;
struct FontAtlas *atlas;
struct FontGlyphSlot *glyphSlot;
FT_UInt glyphIndex;
//Scan through all uploaded glyph slots.
for (i = 0, atlas = font->atlas; i < FNT_MAX_ATLASES; i++, atlas++)
{
for (slot = 0; slot < atlas->NumGlyphs; slot++)
{
if (atlas->GlyphSlot[slot].character == character)
{
glyphInfo->slot = &atlas->GlyphSlot[slot];
glyphInfo->vram = atlas->vram;
return 0;
}
}
}
//Not in VRAM? Upload it.
if ((glyphIndex = FT_Get_Char_Index(font->FTFace, character)) != 0 || DrawMissingGlyph)
{
if (FT_Load_Glyph(font->FTFace, glyphIndex, FT_LOAD_RENDER))
return -1;
if ((glyphSlot = UploadGlyph(gsGlobal, font, character, font->FTFace->glyph, &atlas)) == NULL)
return -1;
// printf("Uploading %c, %u, %u\n", character, GlyphSlot->VramPageX, GlyphSlot->VramPageY);
glyphInfo->slot = glyphSlot;
glyphInfo->vram = atlas->vram;
return 0;
}
else //Otherwise, the glyph is missing from font
return 1;
return -1;
}
static int DrawGlyph(struct UIDrawGlobal *gsGlobal, Font_t *font, wint_t character, short int x, short int y, short int z, float scale, GS_RGBAQ colour, int DrawMissingGlyph, short int *width)
{
struct FontGlyphSlot *glyphSlot;
struct FontGlyphSlotInfo glyphInfo;
struct FontAtlas *atlas;
unsigned short int i, slot;
short int XCoordinates, YCoordinates;
int result;
if (font->IsLoaded)
{
if ((result = GetGlyph(gsGlobal, font, character, DrawMissingGlyph, &glyphInfo)) != 0)
return result;
glyphSlot = glyphInfo.slot;
font->Texture.vram_addr = glyphInfo.vram;
YCoordinates = y + FNT_CHAR_HEIGHT * scale - glyphSlot->top * scale;
XCoordinates = x + glyphSlot->left * scale;
DrawSpriteTexturedClut(gsGlobal, &font->Texture, &font->Clut,
XCoordinates, YCoordinates, //x1, y1
glyphSlot->VramPageX, glyphSlot->VramPageY, //u1, v1
XCoordinates + glyphSlot->width * scale, YCoordinates + glyphSlot->height * scale, //x2, y2
glyphSlot->VramPageX + glyphSlot->width, glyphSlot->VramPageY + glyphSlot->height, //u2, v2
z, colour);
*width = glyphSlot->advance_x * scale;
}
else //Not loaded
return -1;
return 0;
}
void FontPrintfWithFeedback(struct UIDrawGlobal *gsGlobal, short x, short int y, short int z, float scale, GS_RGBAQ colour, const char *string, short int *xRel, short int *yRel)
{
wchar_t wchar;
short int StartX, StartY, width;
int charsize, bufmax;
StartX = x;
StartY = y;
for (bufmax = strlen(string) + 1; *string != '\0'; string += charsize, bufmax -= charsize)
{
//Up to MB_CUR_MAX
charsize = mbtowc(&wchar, string, bufmax);
switch (wchar)
{
case '\r':
x = StartX;
break;
case '\n':
y += (short int)(FNT_CHAR_HEIGHT * scale);
x = StartX;
break;
case '\t':
x += (short int)(FNT_TAB_STOPS * FNT_CHAR_WIDTH * scale) - (unsigned int)x % (unsigned int)(FNT_TAB_STOPS * FNT_CHAR_WIDTH * scale);
break;
default:
width = 0;
if (DrawGlyph(gsGlobal, &GS_FTFont, wchar, x, y, z, scale, colour, 0, &width) != 0)
{
if (DrawGlyph(gsGlobal, &GS_sub_FTFont, wchar, x, y, z, scale, colour, 0, &width) != 0)
{ //Cannot locate the glyph, so draw the missing glyph character.
DrawGlyph(gsGlobal, &GS_FTFont, wchar, x, y, z, scale, colour, 1, &width);
}
}
x += width;
}
}
if (xRel != NULL)
*xRel = x - StartX;
if (yRel != NULL)
*yRel = y - StartY;
}
void FontPrintf(struct UIDrawGlobal *gsGlobal, short int x, short int y, short int z, float scale, GS_RGBAQ colour, const char *string)
{
return FontPrintfWithFeedback(gsGlobal, x, y, z, scale, colour, string, NULL, NULL);
}
static int GetGlyphWidth(struct UIDrawGlobal *gsGlobal, Font_t *font, wint_t character, int DrawMissingGlyph)
{
struct FontGlyphSlotInfo glyphInfo;
int result;
if (font->IsLoaded)
{ //Calling FT_Get_Advance is slow when I/O is slow, hence a cache is required. Here, the atlas is used as a cache.
if ((result = GetGlyph(gsGlobal, font, character, DrawMissingGlyph, &glyphInfo)) != 0)
return result;
return glyphInfo.slot->advance_x;
}
return 0;
}
int FontGetGlyphWidth(struct UIDrawGlobal *gsGlobal, wint_t character)
{
int width;
if ((width = GetGlyphWidth(gsGlobal, &GS_FTFont, character, 0)) == 0)
{
if ((width = GetGlyphWidth(gsGlobal, &GS_sub_FTFont, character, 0)) == 0)
{
width = GetGlyphWidth(gsGlobal, &GS_FTFont, character, 1);
}
}
}