|
| pf_atlas_t * | pf_create_atlas (int page_width, int max_page_height) |
| | Allocates and initializes a font atlas.
|
| |
| void | pf_destroy_atlas (pf_atlas_t *atlas) |
| | Destroys a font atlas and frees all associated pages and glyphs.
|
| |
| pf_face_t * | pf_create_face (pf_atlas_t *atlas, const unsigned char *ttf_data, size_t ttf_data_size, float pixel_height) |
| | Create a font face at a given pixel height.
|
| |
| void | pf_destroy_face (pf_face_t *face) |
| | Destroys a font face and frees the TTF data it owns.
|
| |
| const pf_glyph_t * | pf_get_glyph (pf_face_t *face, uint32_t codepoint) |
| | Get (or rasterize) a single glyph.
|
| |
| void | pf_draw_text (pf_face_t *face, const char *text, float *x, float *y, pf_draw_callback_fn cb, void *user) |
| | Lay out and emit quads for a UTF-8 string.
|
| |
| void | pf_upload_atlas (pf_atlas_t *atlas, pf_upload_callback_fn cb, void *user) |
| | Iterate over dirty pages and invoke the callback for each one.
|
| |
| void | pf_measure_text (pf_face_t *face, const char *text, float *out_width, float *out_height) |
| | Measure a UTF-8 string without drawing.
|
| |
| void | pf_get_metrics (const pf_face_t *face, pf_metrics_t *metrics) |
| | Retrieve vertical font metrics for a face.
|
| |
| float | pf_get_kerning (const pf_face_t *face, uint32_t cp1, uint32_t cp2) |
| | Get the horizontal kerning adjustment between two codepoints.
|
| |
A simple, dynamic (online) font atlas.
Licensing information at end of header
Introduction:
A single-header library that rasterizes and caches glyphs on demand into a multi-page texture atlas. Each page is a single-channel (alpha) bitmap with a fixed width and a height that grows on demand up to a configurable maximum. When a page is full, a new page is appended automatically. Glyph placement uses row-based shelf packing. The library also supports basic, single line drawing, but can be customized using the available metrics and measurement functions.
Features:
- Written in C99
- On-demand glyph rasterization via stb_truetype.h
- Multi-page atlas with automatic page growth and creation
- Hashtable glyph cache with automatic rehashing
- UTF-8 text layout with kerning (rendering API agnostic)
- Text measurement and font metrics
- Dirty-page tracking for efficient GPU uploads
Revision History:
- 0.1 (2026/04/11):
- On-demand glyph rasterization and storage
- Simple text rendering
Usage:
#define PICO_FONT_IMPLEMENTATION
#include "pico_font.h"
You must also have stb_truetype.h available. Define STB_TRUETYPE_IMPLEMENTATION exactly once in your project after including this file.
Quick Start:
float x = 10, y = 50;
pf_draw_text(face,
"Hello!", &x, &y, my_quad_callback, user_ptr);
void pf_upload_atlas(pf_atlas_t *atlas, pf_upload_callback_fn cb, void *user)
Iterate over dirty pages and invoke the callback for each one.
struct pf_atlas_t pf_atlas_t
Opaque atlas handle.
Definition pico_font.h:106
struct pf_face_t pf_face_t
Opaque font-face handle.
Definition pico_font.h:111
void pf_destroy_face(pf_face_t *face)
Destroys a font face and frees the TTF data it owns.
void pf_destroy_atlas(pf_atlas_t *atlas)
Destroys a font atlas and frees all associated pages and glyphs.
void pf_draw_text(pf_face_t *face, const char *text, float *x, float *y, pf_draw_callback_fn cb, void *user)
Lay out and emit quads for a UTF-8 string.
pf_face_t * pf_create_face(pf_atlas_t *atlas, const unsigned char *ttf_data, size_t ttf_data_size, float pixel_height)
Create a font face at a given pixel height.
pf_atlas_t * pf_create_atlas(int page_width, int max_page_height)
Allocates and initializes a font atlas.
Full Example:
A more or less complete example can be found at: examples_pico_gfx/text.c
Macro Overrides:
- PICO_FONT_ASSERT (default: assert)
- PICO_FONT_MALLOC (default: malloc)
- PICO_FONT_CALLOC (default: calloc)
- PICO_FONT_REALLOC (default: realloc)
- PICO_FONT_FREE (default: free)
- PICO_FONT_MEMSET (default: memset)
- PICO_FONT_MEMCPY (default: memcpy)
Constant Overrides:
- PICO_FONT_GLYPH_PADDING (default: 1)
- PICO_FONT_CACHE_INIT_SIZE (default: 256)
- PICO_FONT_INIT_PAGE_HEIGHT (default: 64)