3 Licenced under Academic Free License version 3.0
4 Review OpenUsbLd README & LICENSE files for further details.
7 #include "include/integers.h"
8 #include "include/usbld.h"
9 #include "include/fntsys.h"
10 #include "include/renderman.h"
11 #include "include/ioman.h"
12 #include "include/utf8.h"
13 #include "include/util.h"
14 #include "include/atlas.h"
18 #include FT_FREETYPE_H
20 extern void* freesansfont_raw
;
21 extern int size_freesansfont_raw
;
23 /// Maximal count of atlases per font
25 /// Atlas width in pixels
26 #define ATLAS_WIDTH 128
27 /// Atlas height in pixels
28 #define ATLAS_HEIGHT 128
31 static FT_Library font_library
;
33 static rm_quad_t quad
;
35 static int gCharHeight
;
37 static s32 gFontSemaId
;
38 static ee_sema_t gFontSema
;
40 static GSCLUT fontClut
;
42 /** Single entry in the glyph cache */
45 // size in pixels of the glyph
47 // offsetting of the glyph
49 // advancements in pixels after rendering this glyph
52 // atlas for which the allocation was done
55 // atlas allocation position
56 struct atlas_allocation_t
*allocation
;
58 } fnt_glyph_cache_entry_t
;
60 /** A whole font definition */
62 /** GLYPH CACHE. Every glyph in the ASCII range is cached when first used
63 * this means no additional memory aside from the one needed to render the
64 * character set is used.
66 fnt_glyph_cache_entry_t
**glyphCache
;
68 /// Maximal font cache page index
74 /// Nonzero if font is used
77 /// Nonzero for custom fonts
80 /// Texture atlases (default to NULL)
81 atlas_t
*atlases
[ATLAS_MAX
];
83 /// Pointer to data, if allocation takeover was selected (will be freed)
87 #define FNT_MAX_COUNT (16)
89 /// Array of font definitions
90 static font_t fonts
[FNT_MAX_COUNT
];
92 #define GLYPH_CACHE_PAGE_SIZE 256
94 #define GLYPH_PAGE_OK(font,page) ((pageid <= font->cacheMaxPageID) && (font->glyphCache[page]))
96 static int fntPrepareGlyphCachePage(font_t
*font
, int pageid
) {
97 if (pageid
> font
->cacheMaxPageID
) {
98 fnt_glyph_cache_entry_t
**np
= realloc(font
->glyphCache
, (pageid
+ 1) * sizeof(fnt_glyph_cache_entry_t
*));
103 font
->glyphCache
= np
;
106 for (page
= font
->cacheMaxPageID
+ 1; page
<= pageid
; ++page
)
107 font
->glyphCache
[page
] = NULL
;
109 font
->cacheMaxPageID
= pageid
;
112 // if it already was allocated, skip this
113 if (font
->glyphCache
[pageid
])
117 font
->glyphCache
[pageid
] = malloc(sizeof(fnt_glyph_cache_entry_t
) * GLYPH_CACHE_PAGE_SIZE
);
120 for (i
= 0; i
< GLYPH_CACHE_PAGE_SIZE
; ++i
) {
121 font
->glyphCache
[pageid
][i
].isValid
= 0;
122 font
->glyphCache
[pageid
][i
].atlas
= NULL
;
123 font
->glyphCache
[pageid
][i
].allocation
= NULL
;
129 static void fntResetFontDef(font_t
*fnt
) {
130 LOG("fntResetFontDef\n");
131 fnt
->glyphCache
= NULL
;
132 fnt
->cacheMaxPageID
= -1;
137 for(aid
= 0; aid
< ATLAS_MAX
; ++aid
)
138 fnt
->atlases
[aid
] = NULL
;
143 static void fntPrepareCLUT() {
144 fontClut
.PSM
= GS_PSM_T8
;
145 fontClut
.ClutPSM
= GS_PSM_CT32
;
146 fontClut
.Clut
= memalign(128, 256 * 4);
147 fontClut
.VramClut
= 0;
149 // generate the clut table
151 u32
*clut
= fontClut
.Clut
;
152 for (i
= 0; i
< 256; ++i
) {
153 u8 alpha
= i
> 0x080 ? 0x080 : i
;
155 *clut
= alpha
<< 24 | i
<< 16 | i
<< 8 | i
;
160 static void fntDestroyCLUT() {
162 fontClut
.Clut
= NULL
;
167 int error
= FT_Init_FreeType(&font_library
);
170 // just report over the ps2link
171 LOG("Freetype init failed with %x!\n", error
);
177 gFontSema
.init_count
= 1;
178 gFontSema
.max_count
= 1;
179 gFontSema
.option
= 0;
180 gFontSemaId
= CreateSema(&gFontSema
);
184 for (i
= 0; i
< FNT_MAX_COUNT
; ++i
)
185 fntResetFontDef(&fonts
[i
]);
187 // load the default font (will be id=0)
188 fntLoad(NULL
, -1, 0);
191 static void fntCacheFlushPage(fnt_glyph_cache_entry_t
*page
) {
192 LOG("fntCacheFlushPage\n");
195 for (i
= 0; i
< GLYPH_CACHE_PAGE_SIZE
; ++i
, ++page
) {
197 // we're not doing any atlasFree or such - atlas has to be rebuild
198 page
->allocation
= NULL
;
203 static void fntCacheFlush(font_t
*fnt
) {
204 LOG("fntCacheFlush\n");
208 // Release all the glyphs from the cache
209 for (i
= 0; i
<= fnt
->cacheMaxPageID
; ++i
) {
210 if (fnt
->glyphCache
[i
]) {
211 fntCacheFlushPage(fnt
->glyphCache
[i
]);
212 free(fnt
->glyphCache
[i
]);
213 fnt
->glyphCache
[i
] = NULL
;
217 free(fnt
->glyphCache
);
218 fnt
->glyphCache
= NULL
;
219 fnt
->cacheMaxPageID
= -1;
221 // free all atlasses too, they're invalid now anyway
223 for(aid
= 0; aid
< ATLAS_MAX
; ++aid
) {
224 atlasFree(fnt
->atlases
[aid
]);
225 fnt
->atlases
[aid
] = NULL
;
229 static int fntNewFont() {
232 for (i
= 0; i
< FNT_MAX_COUNT
; ++i
) {
233 if (fonts
[i
].isValid
== 0) {
234 fntResetFontDef(&fonts
[i
]);
242 void fntDeleteFont(font_t
*font
) {
243 LOG("fntDeleteFont\n");
245 // skip already deleted fonts
249 // free the glyph cache, atlases, unload the font
252 FT_Done_Face(font
->face
);
256 font
->dataPtr
= NULL
;
262 int fntLoadSlot(font_t
*fnt
, void* buffer
, int bufferSize
) {
263 LOG("fntLoadSlot\n");
266 buffer
= &freesansfont_raw
;
267 bufferSize
= size_freesansfont_raw
;
271 // load the font via memory handle
272 int error
= FT_New_Memory_Face(font_library
, (FT_Byte
*) buffer
, bufferSize
, 0, &fnt
->face
);
275 // just report over the ps2link
276 LOG("Freetype: Font loading failed with %x!\n", error
);
283 error
= FT_Set_Char_Size(fnt
->face
, 0, gCharHeight
* 16, 300, 300);
284 /*error = FT_Set_Pixel_Sizes( face,
286 gCharHeight ); // pixel_height*/
289 // just report over the ps2link
290 LOG("Freetype: Error setting font pixel size with %x!\n", error
);
299 int fntLoad(void* buffer
, int bufferSize
, int takeover
) {
302 // we need a new slot in the font array
303 int fontID
= fntNewFont();
305 if (fontID
== FNT_ERROR
)
308 font_t
*fnt
= &fonts
[fontID
];
310 if (fntLoadSlot(fnt
, buffer
, bufferSize
) == FNT_ERROR
)
314 fnt
->dataPtr
= buffer
;
319 int fntLoadFile(char* path
) {
320 LOG("fntLoadFile\n");
321 // load the buffer with font
323 void* customFont
= readFile(path
, -1, &size
);
328 int fontID
= fntLoad(customFont
, size
, 1);
333 void fntRelease(int id
) {
335 if (id
< FNT_MAX_COUNT
)
336 fntDeleteFont(&fonts
[id
]);
339 /** Terminates the font rendering system */
342 // release all the fonts
344 for (id
= 0; id
< FNT_MAX_COUNT
; ++id
)
345 fntDeleteFont(&fonts
[id
]);
347 // deinit freetype system
348 FT_Done_FreeType(font_library
);
350 DeleteSema(gFontSemaId
);
355 static atlas_t
*fntNewAtlas() {
356 atlas_t
*atl
= atlasNew(ATLAS_WIDTH
, ATLAS_HEIGHT
, GS_PSM_T8
);
358 atl
->surface
.ClutPSM
= GS_PSM_CT32
;
359 atl
->surface
.Clut
= (u32
*)(&fontClut
);
364 static int fntGlyphAtlasPlace(font_t
*fnt
, fnt_glyph_cache_entry_t
* glyph
) {
365 FT_GlyphSlot slot
= fnt
->face
->glyph
;
367 LOG("fntGlyphAtlasPlace: Placing the glyph... %d x %d\n", slot
->bitmap
.width
, slot
->bitmap
.rows
);
369 if (slot
->bitmap
.width
== 0 || slot
->bitmap
.rows
== 0) {
370 // no bitmap glyph, just skip
376 for (aid
= 0; aid
< ATLAS_MAX
; ++aid
) {
377 LOG(" * Placing aid %d...\n", aid
);
378 atlas_t
**atl
= &fnt
->atlases
[aid
];
379 if (!*atl
) { // atlas slot not yet used
380 LOG(" * aid %d is new...\n", aid
);
381 *atl
= fntNewAtlas();
385 atlasPlace(*atl
, slot
->bitmap
.width
, slot
->bitmap
.rows
, slot
->bitmap
.buffer
);
387 if (glyph
->allocation
) {
388 LOG(" * found placement\n", aid
);
395 LOG(" * ! no atlas free\n", aid
);
399 /** Internal method. Makes sure the bitmap data for particular character are pre-rendered to the glyph cache */
400 static fnt_glyph_cache_entry_t
* fntCacheGlyph(font_t
*fnt
, uint32_t gid
) {
401 // calc page id and in-page index from glyph id
402 int pageid
= gid
/ GLYPH_CACHE_PAGE_SIZE
;
403 int idx
= gid
% GLYPH_CACHE_PAGE_SIZE
;
405 // do not call on every char of every font rendering call
406 if (!GLYPH_PAGE_OK(fnt
,pageid
))
407 if (!fntPrepareGlyphCachePage(fnt
, pageid
)) // failed to prepare the page...
410 fnt_glyph_cache_entry_t
*page
= fnt
->glyphCache
[pageid
];
412 /* Should never happen.
413 if (!page) // safeguard
417 fnt_glyph_cache_entry_t
* glyph
= &page
[idx
];
422 // not cached but valid. Cache
424 LOG("FNT: Face is NULL!\n");
427 int error
= FT_Load_Char(fnt
->face
, gid
, FT_LOAD_RENDER
);
430 LOG("FNT: Error loading glyph - %d\n", error
);
434 // find atlas placement for the glyph
435 if (!fntGlyphAtlasPlace(fnt
, glyph
))
438 FT_GlyphSlot slot
= fnt
->face
->glyph
;
439 glyph
->width
= slot
->bitmap
.width
;
440 glyph
->height
= slot
->bitmap
.rows
;
441 glyph
->shx
= slot
->advance
.x
;
442 glyph
->shy
= slot
->advance
.y
;
443 glyph
->ox
= slot
->bitmap_left
;
444 glyph
->oy
= gCharHeight
- slot
->bitmap_top
;
451 void fntSetAspectRatio(float aw
, float ah
) {
452 LOG("fntSetAspectRatio\n");
453 // flush cache - it will be invalid after the setting
456 for (i
= 0; i
< FNT_MAX_COUNT
; ++i
) {
457 if (fonts
[i
].isValid
)
458 fntCacheFlush(&fonts
[i
]);
461 // set new aspect ratio (Is this correct, I wonder?)
462 // TODO: error = FT_Set_Char_Size(face, 0, gCharHeight*64, ah*300, aw*300);
465 int fntRenderString(int font
, int x
, int y
, short aligned
, const unsigned char* string
, u64 colour
) {
466 // wait for font lock to unlock
467 WaitSema(gFontSemaId
);
468 font_t
*fnt
= &fonts
[font
];
469 SignalSema(gFontSemaId
);
472 x
-= fntCalcDimensions(font
, string
) >> 1;
473 y
-= MENU_ITEM_HEIGHT
>> 1;
476 rmApplyShiftRatio(&y
);
480 FT_Bool use_kerning
= FT_HAS_KERNING(fnt
->face
);
481 FT_UInt glyph_index
, previous
= 0;
484 // cache glyphs and render as we go
485 for (; *string
; ++string
) {
486 if (utf8Decode(&state
, &codepoint
, *string
)) // accumulate the codepoint value
489 fnt_glyph_cache_entry_t
* glyph
= fntCacheGlyph(fnt
, codepoint
);
495 if (use_kerning
&& previous
) {
496 glyph_index
= FT_Get_Char_Index(fnt
->face
, codepoint
);
498 FT_Get_Kerning(fnt
->face
, previous
, glyph_index
, FT_KERNING_DEFAULT
, &delta
);
501 previous
= glyph_index
;
504 // only if glyph has atlas placement
505 if (glyph
->allocation
) {
506 /* TODO: Ineffective on many parts:
507 * 1. Usage of floats for UV - fixed point should suffice (and is used internally by GS for UV)
509 * 2. GS_SETREG_TEX0 for every quad - why? gsKit should only set texture if demanded
510 * We should prepare a special fnt render method that would step over most of the
511 * performance problems under - begining with rmSetupQuad and continuing into gsKit
512 * - this method would handle the preparetion of the quads and GS upload itself,
513 * without the use of prim_quad_texture and rmSetupQuad...
515 * (3. rmSetupQuad is cool for a few quads a frame, but glyphs are too many in numbers
516 * - seriously, all that branching for every letter is a bit much)
518 * 4. We should use clut to keep the memory allocations sane - we're linear in the 32bit buffers
519 * anyway, no reason to waste like we do!
522 quad
.ul
.x
= x
+ glyph
->ox
;
523 quad
.br
.x
= quad
.ul
.x
+ glyph
->width
;
524 quad
.ul
.y
= y
+ glyph
->oy
;
525 quad
.br
.y
= quad
.ul
.y
+ glyph
->height
;
527 // UV is our own, custom thing here
528 quad
.txt
= &glyph
->atlas
->surface
;
529 quad
.ul
.u
= glyph
->allocation
->x
;
530 quad
.br
.u
= quad
.ul
.u
+ glyph
->width
;
531 quad
.ul
.v
= glyph
->allocation
->y
;
532 quad
.br
.v
= quad
.ul
.v
+ glyph
->height
;
537 x
+= glyph
->shx
>> 6;
538 y
+= glyph
->shy
>> 6;
544 void fntRenderText(int font
, int sx
, int sy
, short aligned
, size_t width
, size_t height
, const unsigned char* string
, u64 colour
) {
545 // wait for font lock to unlock
546 WaitSema(gFontSemaId
);
547 font_t
*fnt
= &fonts
[font
];
548 SignalSema(gFontSemaId
);
551 sx
-= min(fntCalcDimensions(font
, string
), width
) >> 1;
552 sy
-= MENU_ITEM_HEIGHT
>> 1;
555 rmApplyShiftRatio(&sy
);
560 int ym
= sy
+ height
;
564 FT_Bool use_kerning
= FT_HAS_KERNING(fnt
->face
);
565 FT_UInt glyph_index
, previous
= 0;
568 // Note: We need to change this so that we'll accumulate whole word before doing a layout with it
569 // for now this method breaks on any character - which is a bit ugly
571 // I don't want to do anything complicated though so I'd say
572 // we should instead have a dynamic layout routine that'll replace spaces with newlines as appropriate
573 // because that'll make the code run only once per N frames, not every frame
575 // cache glyphs and render as we go
576 for (; *string
; ++string
) {
577 if (utf8Decode(&state
, &codepoint
, *string
)) // accumulate the codepoint value
580 fnt_glyph_cache_entry_t
* glyph
= fntCacheGlyph(fnt
, codepoint
);
585 if (use_kerning
&& previous
) {
586 glyph_index
= FT_Get_Char_Index(fnt
->face
, codepoint
);
588 FT_Get_Kerning(fnt
->face
, previous
, glyph_index
, FT_KERNING_DEFAULT
, &delta
);
591 previous
= glyph_index
;
594 if (codepoint
== '\n') {
596 y
+= MENU_ITEM_HEIGHT
; // hmax is too tight and unordered, generally
600 if ((x
+ glyph
->width
> xm
) || (y
> ym
)) // stepped over the max
603 // only if glyph has atlas placement
604 if (glyph
->allocation
) {
606 quad
.ul
.x
= x
+ glyph
->ox
;
607 quad
.br
.x
= quad
.ul
.x
+ glyph
->width
;
608 quad
.ul
.y
= y
+ glyph
->oy
;
609 quad
.br
.y
= quad
.ul
.y
+ glyph
->height
;
611 // UV is our own, custom thing here
612 quad
.txt
= &glyph
->atlas
->surface
;
613 quad
.ul
.u
= glyph
->allocation
->x
;
614 quad
.br
.u
= quad
.ul
.u
+ glyph
->width
;
615 quad
.ul
.v
= glyph
->allocation
->y
;
616 quad
.br
.v
= quad
.ul
.v
+ glyph
->height
;
621 x
+= glyph
->shx
>> 6;
622 y
+= glyph
->shy
>> 6;
626 void fntFitString(int font
, unsigned char *string
, size_t width
) {
628 unsigned char *str
= string
;
629 size_t spacewidth
= fntCalcDimensions(font
, " ");
630 unsigned char *psp
= NULL
;
633 // scan forward to the next whitespace
634 unsigned char *sp
= str
;
635 for (; *sp
&& *sp
!= ' ' && *sp
!= '\n'; ++sp
);
637 // store what was there before
638 unsigned char osp
= *sp
;
640 // newline resets the situation
648 // terminate after the word
651 // Calc the font's width...
652 // NOTE: The word was terminated, so we're seeing a single word
654 size_t ww
= fntCalcDimensions(font
, str
);
656 if (cw
+ ww
> width
) {
658 // we have a prev space to utilise (wrap on it)
664 // no prev. space to hijack, must break after the word
665 // this will mean overflowed text...
680 int fntCalcDimensions(int font
, const unsigned char* str
) {
683 WaitSema(gFontSemaId
);
684 font_t
*fnt
= &fonts
[font
];
685 SignalSema(gFontSemaId
);
689 FT_Bool use_kerning
= FT_HAS_KERNING(fnt
->face
);
690 FT_UInt glyph_index
, previous
= 0;
693 // cache glyphs and render as we go
694 for (; *str
; ++str
) {
695 unsigned char c
= *str
;
697 if (utf8Decode(&state
, &codepoint
, c
)) // accumulate the codepoint value
700 // Could just as well only get the glyph dimensions
701 // but it is probable the glyphs will be needed anyway
702 fnt_glyph_cache_entry_t
* glyph
= fntCacheGlyph(fnt
, codepoint
);
708 if (use_kerning
&& previous
) {
709 glyph_index
= FT_Get_Char_Index(fnt
->face
, codepoint
);
711 FT_Get_Kerning(fnt
->face
, previous
, glyph_index
, FT_KERNING_DEFAULT
, &delta
);
714 previous
= glyph_index
;
717 w
+= glyph
->shx
>> 6;
723 void fntReplace(int id
, void* buffer
, int bufferSize
, int takeover
, int asDefault
) {
724 font_t
*fnt
= &fonts
[id
];
726 font_t ndefault
, old
;
727 fntResetFontDef(&ndefault
);
728 fntLoadSlot(&ndefault
, buffer
, bufferSize
);
729 ndefault
.isDefault
= asDefault
;
731 // copy over the new font definition
732 // we have to lock this phase, as the old font may still be used
733 WaitSema(gFontSemaId
);
734 memcpy(&old
, fnt
, sizeof(font_t
));
735 memcpy(fnt
, &ndefault
, sizeof(font_t
));
738 fnt
->dataPtr
= buffer
;
740 SignalSema(gFontSemaId
);
742 // delete the old font
746 void fntSetDefault(int id
) {
747 font_t
*fnt
= &fonts
[id
];
753 font_t ndefault
, old
;
754 fntResetFontDef(&ndefault
);
755 fntLoadSlot(&ndefault
, NULL
, -1);
757 // copy over the new font definition
758 // we have to lock this phase, as the old font may still be used
759 // Note: No check for concurrency is done here, which is kinda funky!
760 WaitSema(gFontSemaId
);
761 memcpy(&old
, fnt
, sizeof(font_t
));
762 memcpy(fnt
, &ndefault
, sizeof(font_t
));
763 SignalSema(gFontSemaId
);
765 // delete the old font