The Font library provides comprehensive text rendering capabilities for PlayStation 3 applications, supporting TrueType fonts, system fonts, and advanced text layout features.
Overview
The font library supports:
System font sets (Japanese, Latin, Korean, Chinese)
TrueType/FreeType font loading
Glyph rendering and caching
Horizontal and vertical text layout
Kerning and advanced typography
Custom memory management
Multiple font renderers
Initialization
fontInit
Initialize the font library.
static inline s32 fontInit (fontConfig * config );
Pointer to font configuration structure
Returns 0 on success, nonzero on error
typedef struct _font_config {
struct {
u32 * buffer; // File cache buffer
u32 size; // Cache size
} fileCache;
u32 userFontEntryMax; // Max user font entries
fontEntry * userFontEntries; // User font entry array
u32 flags; // Configuration flags
} fontConfig;
fontConfig config;
fontConfig_initialize ( & config );
// Optional: Set up file cache
config.fileCache.buffer = malloc ( 1024 * 1024 );
config.fileCache.size = 1024 * 1024 ;
if ( fontInit ( & config ) != 0 ) {
// Handle error
}
fontConfig_initialize
Initialize a font configuration structure with defaults.
static inline void fontConfig_initialize (fontConfig * config );
Pointer to configuration structure to initialize
Sets all fields to default values (NULL pointers, zero sizes).
fontEnd
Shutdown the font library.
Returns 0 on success, nonzero on error
Call this before exiting to free all font resources.
Font Loading
fontOpenFontset
Open a system fontset.
s32 fontOpenFontset ( const fontLibrary * lib , fontType * type , font * f );
lib
const fontLibrary*
required
Pointer to font library
Pointer to font type specification
Pointer to receive opened font handle
Returns 0 on success, nonzero on error
typedef struct _font_type {
u32 type; // Font type (FONT_TYPE_RODIN_SANS_SERIF_LATIN, etc.)
u32 map; // Font map (FONT_MAP_FONT or FONT_MAP_UNICODE)
} fontType;
// Open Latin sans-serif system font
fontLibraryConfigFT ftConfig;
fontLibraryConfigFT_initialize ( & ftConfig );
const fontLibrary * lib;
fontInitLibraryFreeType ( & ftConfig , & lib );
fontType type;
type.type = FONT_TYPE_RODIN_SANS_SERIF_LATIN;
type.map = FONT_MAP_UNICODE;
font f;
fontOpenFontset (lib, & type , & f );
fontOpenFontFile
Open a font from file.
s32 fontOpenFontFile ( const fontLibrary * lib , const char * fontPath , u32 subNum ,
s32 uniqueID , font * f );
lib
const fontLibrary*
required
Pointer to font library
Path to font file (TTF, OTF, etc.)
Sub-font index (0 for single fonts)
Unique identifier for this font
Pointer to receive opened font handle
Returns 0 on success, nonzero on error
font customFont;
fontOpenFontFile (lib, "/dev_hdd0/game/fonts/custom.ttf" , 0 , 1 , & customFont );
fontOpenFontMemory
Open a font from memory.
s32 fontOpenFontMemory ( const fontLibrary * lib , void * fontAddr , u32 fontSize ,
u32 subNum , s32 uniqueID , font * f );
lib
const fontLibrary*
required
Pointer to font library
Pointer to font data in memory
Size of font data in bytes
Sub-font index (0 for single fonts)
Unique identifier for this font
Pointer to receive opened font handle
Returns 0 on success, nonzero on error
fontCloseFont
Close an opened font.
s32 fontCloseFont (font * cf );
Pointer to font handle to close
Returns 0 on success, nonzero on error
Font Configuration
fontSetScalePixel
Set font scale in pixels.
s32 fontSetScalePixel (font * f , f32 w , f32 h );
Returns 0 on success, nonzero on error
// Set font to 24 pixel height
fontSetScalePixel ( & myFont , 24.0 f , 24.0 f );
fontSetScalePoint
Set font scale in points.
s32 fontSetScalePoint (font * f , f32 w , f32 h );
Returns 0 on success, nonzero on error
fontSetResolutionDpi
Set font resolution in DPI.
s32 fontSetResolutionDpi (font * f , u32 hDpi , u32 vDpi );
Returns 0 on success, nonzero on error
// Set to 96 DPI (standard screen resolution)
fontSetResolutionDpi ( & myFont , 96 , 96 );
fontSetEffectWeight
Set font weight effect (boldness).
s32 fontSetEffectWeight (font * f , f32 effectWeight );
Weight value (0.0 = normal, positive = bolder)
Returns 0 on success, nonzero on error
fontSetEffectSlant
Set font slant effect (italic).
s32 fontSetEffectSlant (font * f , f32 effectSlant );
Slant value (0.0 = normal, positive = italic)
Returns 0 on success, nonzero on error
Glyph Metrics
fontGetCharGlyphMetrics
Get metrics for a character glyph.
s32 fontGetCharGlyphMetrics (font * f , u32 code , fontGlyphMetrics * metrics );
metrics
fontGlyphMetrics*
required
Pointer to receive glyph metrics
Returns 0 on success, nonzero on error
fontGlyphMetrics Structure
typedef struct _font_glyph_metrics {
f32 width; // Glyph width
f32 height; // Glyph height
struct {
f32 bearingX; // Horizontal bearing X
f32 bearingY; // Horizontal bearing Y
f32 advance; // Horizontal advance
} horizontal;
struct {
f32 bearingX; // Vertical bearing X
f32 bearingY; // Vertical bearing Y
f32 advance; // Vertical advance
} vertical;
} fontGlyphMetrics;
fontGlyphMetrics metrics;
fontGetCharGlyphMetrics ( & myFont , 'A' , & metrics );
printf ( "Character width: %.2f \n " , metrics.width);
printf ( "Advance: %.2f \n " , metrics.horizontal.advance);
fontGetKerning
Get kerning adjustment between two characters.
s32 fontGetKerning (font * f , u32 preCode , u32 code , fontKerning * kerning );
Pointer to receive kerning values
Returns 0 on success, nonzero on error
typedef struct _font_kerning {
f32 offsetX; // X offset adjustment
f32 offsetY; // Y offset adjustment
} fontKerning;
fontGetHorizontalLayout
Get horizontal layout metrics.
s32 fontGetHorizontalLayout (font * f , fontHorizontalLayout * layout );
layout
fontHorizontalLayout*
required
Pointer to receive layout metrics
Returns 0 on success, nonzero on error
fontHorizontalLayout Structure
typedef struct _font_horizontal_layout {
f32 baseLineY; // Baseline Y position
f32 lineHeight; // Total line height
f32 effectHeight; // Effective height
} fontHorizontalLayout;
fontGetVerticalLayout
Get vertical layout metrics.
s32 fontGetVerticalLayout (font * f , fontVerticalLayout * layout );
layout
fontVerticalLayout*
required
Pointer to receive layout metrics
Returns 0 on success, nonzero on error
Font Renderer
fontCreateRenderer
Create a font renderer.
s32 fontCreateRenderer ( const fontLibrary * lib , fontRendererConfig * config ,
fontRenderer * renderer );
lib
const fontLibrary*
required
Pointer to font library
config
fontRendererConfig*
required
Pointer to renderer configuration
Pointer to receive renderer handle
Returns 0 on success, nonzero on error
fontRendererConfig Structure
typedef struct _font_renderer_config {
struct {
void * buffer; // Buffer for glyph cache
u32 initSize; // Initial buffer size
u32 maxSize; // Maximum buffer size
u32 expandSize; // Size to expand by
u32 resetSize; // Reset threshold size
} bufferingPolicy;
} fontRendererConfig;
fontRenderer renderer;
fontRendererConfig config;
config.bufferingPolicy.buffer = NULL ;
config.bufferingPolicy.initSize = 1024 * 1024 ;
config.bufferingPolicy.maxSize = 4 * 1024 * 1024 ;
config.bufferingPolicy.expandSize = 512 * 1024 ;
config.bufferingPolicy.resetSize = 2 * 1024 * 1024 ;
fontCreateRenderer (lib, & config , & renderer );
fontBindRenderer
Bind a renderer to a font.
s32 fontBindRenderer (font * f , fontRenderer * renderer );
Pointer to renderer to bind
Returns 0 on success, nonzero on error
fontUnbindRenderer
Unbind a renderer from a font.
s32 fontUnbindRenderer (font * f );
Returns 0 on success, nonzero on error
fontDestroyRenderer
Destroy a font renderer.
s32 fontDestroyRenderer (fontRenderer * renderer );
Pointer to renderer to destroy
Returns 0 on success, nonzero on error
Glyph Rendering
fontRenderSurfaceInit
Initialize a render surface.
void fontRenderSurfaceInit (fontRenderSurface * surface , void * buffer ,
s32 bufWidthByte , s32 pixelSizeByte , s32 w , s32 h );
surface
fontRenderSurface*
required
Pointer to surface structure to initialize
Buffer width in bytes (pitch)
Bytes per pixel (1, 2, or 4)
fontRenderSurfaceSetScissor
Set scissor rectangle for rendering.
void fontRenderSurfaceSetScissor (fontRenderSurface * surface , s32 x0 , s32 y0 ,
u32 w , u32 h );
surface
fontRenderSurface*
required
Pointer to render surface
fontRenderCharGlyphImage
Render a character glyph to a surface.
s32 fontRenderCharGlyphImage (font * f , u32 code , fontRenderSurface * surface ,
f32 x , f32 y , fontGlyphMetrics * metrics ,
fontImageTransInfo * transInfo );
surface
fontRenderSurface*
required
Pointer to render surface
metrics
fontGlyphMetrics*
required
Pointer to receive glyph metrics (can be NULL)
transInfo
fontImageTransInfo*
required
Pointer to image transfer info (can be NULL)
Returns 0 on success, nonzero on error
// Initialize render surface
u32 * framebuffer = get_framebuffer ();
fontRenderSurface surface;
fontRenderSurfaceInit ( & surface , framebuffer, 1920 * 4 , 4 , 1920 , 1080 );
// Render text
f32 x = 100.0 f , y = 100.0 f ;
const char * text = "Hello, PS3!" ;
for ( int i = 0 ; text [i]; i ++ ) {
fontGlyphMetrics metrics;
fontRenderCharGlyphImage ( & myFont, text [i], & surface, x, y, & metrics, NULL );
x += metrics . horizontal . advance ;
}
FreeType Library
fontInitLibraryFreeType
Initialize FreeType font library.
s32 fontInitLibraryFreeType (fontLibraryConfigFT * config , const fontLibrary ** lib );
config
fontLibraryConfigFT*
required
Pointer to FreeType configuration
lib
const fontLibrary**
required
Pointer to receive library handle
Returns 0 on success, nonzero on error
fontLibraryConfigFT Structure
typedef struct _font_library_configFT {
void * library; // FreeType library handle
fontMemoryInterface memoryIF; // Memory interface
} fontLibraryConfigFT;
fontLibraryConfigFT ftConfig;
fontLibraryConfigFT_initialize ( & ftConfig );
const fontLibrary * lib;
if ( fontInitLibraryFreeType ( & ftConfig , & lib ) != 0 ) {
// Handle error
}
fontLibraryConfigFT_initialize
Initialize FreeType configuration with defaults.
static inline void fontLibraryConfigFT_initialize (fontLibraryConfigFT * config );
config
fontLibraryConfigFT*
required
Pointer to configuration to initialize
System Font Types
Latin Fonts
FONT_TYPE_RODIN_SANS_SERIF_LATIN (0x00000000) - Sans-serif Latin
FONT_TYPE_MATISSE_SERIF_LATIN (0x00000020) - Serif Latin
Japanese Fonts
FONT_TYPE_NEWRODIN_GOTHIC_JAPANESE (0x00000008) - Gothic Japanese
FONT_TYPE_NEWRODIN_GOTHIC_JP_SET (0x00000100) - Japanese character set
FONT_TYPE_NEWRODIN_GOTHIC_LATIN_SET (0x00000101) - Japanese + Latin
Korean Fonts
FONT_TYPE_YD_GOTHIC_KOREAN (0x0000000c) - Gothic Korean
CJK Font Sets
FONT_TYPE_GOTHIC_JAPANESE_CJK_JP_SET - Japanese CJK
FONT_TYPE_GOTHIC_TCHINESE_CJK_JP_SET - Traditional Chinese CJK
FONT_TYPE_GOTHIC_SCHINESE_CJK_JP_SET - Simplified Chinese CJK
Default Fonts
FONT_TYPE_DEFAULT_GOTHIC_SET - Default gothic (Latin)
FONT_TYPE_DEFAULT_SANS_SERIF - Default sans-serif
FONT_TYPE_DEFAULT_SERIF - Default serif
Font Maps
FONT_MAP_FONT (0x0) - Font-specific character mapping
FONT_MAP_UNICODE (0x1) - Unicode character mapping
Complete Example
Full Font Rendering Example
#include <font/font.h>
#include <font/fontFT.h>
// Initialize font system
fontConfig config;
fontConfig_initialize ( & config );
fontInit ( & config );
// Initialize FreeType library
fontLibraryConfigFT ftConfig;
fontLibraryConfigFT_initialize ( & ftConfig );
const fontLibrary * lib;
fontInitLibraryFreeType ( & ftConfig , & lib );
// Open system font
fontType type;
type.type = FONT_TYPE_RODIN_SANS_SERIF_LATIN;
type.map = FONT_MAP_UNICODE;
font myFont;
fontOpenFontset (lib, & type , & myFont );
// Configure font
fontSetScalePixel ( & myFont , 32.0 f , 32.0 f );
fontSetResolutionDpi ( & myFont , 96 , 96 );
// Create renderer
fontRenderer renderer;
fontRendererConfig rendererConfig;
rendererConfig.bufferingPolicy.buffer = NULL ;
rendererConfig.bufferingPolicy.initSize = 1024 * 1024 ;
rendererConfig.bufferingPolicy.maxSize = 4 * 1024 * 1024 ;
rendererConfig.bufferingPolicy.expandSize = 512 * 1024 ;
rendererConfig.bufferingPolicy.resetSize = 2 * 1024 * 1024 ;
fontCreateRenderer (lib, & rendererConfig , & renderer );
fontBindRenderer ( & myFont , & renderer );
// Setup render surface
u32 * framebuffer = get_framebuffer ();
fontRenderSurface surface;
fontRenderSurfaceInit ( & surface , framebuffer, 1920 * 4 , 4 , 1920 , 1080 );
// Render text
const char * text = "Hello, PlayStation 3!" ;
f32 x = 100.0 f , y = 100.0 f ;
for ( int i = 0 ; text [i]; i ++ ) {
fontGlyphMetrics metrics;
fontRenderCharGlyphImage ( & myFont, text [i], & surface, x, y, & metrics, NULL );
// Apply kerning for next character
if ( text [i + 1 ]) {
fontKerning kerning;
fontGetKerning ( & myFont, text [i], text [i + 1 ], & kerning);
x += metrics . horizontal . advance + kerning . offsetX ;
} else {
x += metrics . horizontal . advance ;
}
}
// Cleanup
fontUnbindRenderer ( & myFont );
fontDestroyRenderer ( & renderer );
fontCloseFont ( & myFont );
fontEnd ();
See Also
RSX - Graphics rendering system
Video Configuration - Display setup