Fonts - Bitmap Font Manager for PSP

Fonts is an unimaginatively-named bitmap font manager for the PlayStation portable. Fonts uses a custom file format based on TXR, the texture format used by Tex. You must initialize Tex before using Fonts.

A Brief Introduction to Font Metrics

Font metrics describe the dimensions of individual characters and the font as a whole. There are several terms used to describe a font metrics, but Fonts is designed to be easy to use, so you only need to understand a couple of the terms.

The baseline is an imaginary line on which characters are drawn. You might think of characters as "sitting" on the baseline. A descender is a portion of a character that descends below the baseline. Examples of characters with descenders are g, q, and p. Line height is the amount of space required between two baselines. Line height includes space for descenders and a little bit of whitespace between lines.

If you know the coordinates of the top-left corner of where you'd like to draw some text, you can calculate the position of the baseline like so: top + lineHeight - descender

There's quite a bit more to font metrics, but that's all you need to know to use Fonts.

Types

FntCharacter

typedef struct { ...

Structure describing metrics for a single character. Normally, your program never needs to access structures of this type.

FieldTypeDescription
srcXs32X-coordinate of the character's bitmap image in the font's texture.
srcYs32Y-coordinate of the character's bitmap image in the font's texture.
srcWidths32Width of the character's bitmap image in the font's texture.
srcHeights32Height of the character's bitmap image in the font's texture.
dstXs32Amount to offset the X-coordinate of this character in the destination image.
dstYs32Amount to offset the Y-coordinate of this character in the destination image.
advancements32Amount to advance the drawing position after drawing this character.

FntFont

typedef struct { ...

Structure describing a font.

FieldTypeDescription
characterCountu32Number of characters in the font.
lineHeightu32Height in pixels of one line in the font.
descenderu32Size in pixels of the font's largest descender.
textureTexTexture*Texture containing the font's character bitmaps.
charactersFntCharacter*Array of character metrics.

Functions

fntDraw

int fntDraw(FntFont *font, u32 color, int x, int y, const char *fmt, ...)

Draws fmt at the specified coordinates using font. The baseline is positioned at y. Before drawing, fmt is processed with sprintf, so you can include formatting codes and corresponding extra arguments. The text will be drawn with the color specified by color, which must be in 8888 format.

This function adds commands to the current GU display list. It must be called between sceGuStart and sceGuFinish.

Returns x plus the total width of the characters drawn.

fntFree

void fntFree(FntFont *font)

Frees all memory occupied by font.

fntLoad

FntFont *fntLoad(const char *file)

Loads a font into memory. file must be a font file in FNT format.

Font uses Tex to load font textures. If Tex wasn't initialized with a cacheless pointer, you must flush the CPU's cache before using the loaded font.

FNT Format

FNT is a simple font format based on the TXR texture format. FNT files consists of two parts: texture and metrics. All data in a FNT file is little-endian.

Texture

The beginning of a FNT file is a TXR. The texture contains images of all characters in the font.

Metrics

Immediately following the texture are the font metrics. This section has a short header followed by an array of FntCharacter structures. The header, which is identical to the first six bytes of a FntFont structure, consists of the following fields:

Byte CountDescription
2Number of characters in the font.
2Line height.
2Size in pixels of the font's largest descender.

The first FntCharacter structure describes the space character (ASCII 32). Subsequenct FntCharacter structures describe the remainder of the ASCII character space in sequence.

Creating FNT Files

At the moment there are no publicly available tools for creating FNT files. Sorry. In the meantime, you can download some free FNT files based on the Bitstream Vera fonts.