font.md 2.3 KB

Fonts

SMGUI supports any kind of fonts, and ships two reference implementations, ui_psf2.h (default, simple bitmap fonts) and ui_ssfn.h (vector fonts, scaled bitmap and pixelmap fonts), but you can also add your own.

Setting up a Custom Implementation

int ui_fonthook(ui_t *ctx, ui_font_bbox bbox, ui_font_draw draw);

For a custom font format you'll have to pass two hooks to the context.

Parameter Description
ctx Pointer to UI context
bbox Bounding box function
draw Font renderer function

Returns 0 on success, an error code otherwise.

The hooks are:

typedef int (*ui_font_bbox)(void *fnt, char *str, char *end,
    int *w, int *h, int *l, int *t);

Measures the string and returns its width in w, height in h in pixels. If there's a left bearing, l is set. Baseline is set from the top in t (both l and t can be NULL if not interested). The string is a zero terminated UTF-8 string in str, but if end is not NULL, then it must stop at end.

typedef int (*ui_font_draw)(void *fnt, char *str, char *end,
    uint8_t *dst, uint32_t color, int x, int y, int l, int t, int p,
    int cx0, int cy0, int cx1, int cy1);

Renders the string at x, y (with left bearing l and baseline from top t) into a pixel buffer dst which has p bytes in a line (pitch). It is very important that this function must not modify pixels outside of the cx0, cy0 to cx1, cy1 crop region. The implementation specific font is passed in fnt, the font's color in color, the zero terminated UTF-8 string itself in str, but if end is not NULL, then it must stop at end.

Loading a Font

WARNING: This function does not initialize the font, it just stores the pointer and passes it to the hooks. Font initialization and releasing is platform specific and up to the user (PSF2 needs none).

int ui_font(ui_t *ctx, void *fnt);

Sets the current font to be used.

Parameter Description
ctx Pointer to UI context
fnt Font to be used

Returns 0 on success, an error code otherwise.