|
RMGui 0.15.0
C11 retained-mode UI library: layout kernel, style schema, widget painters, fonts, software framebuffer
|
font.h — the runtime-rasterized interface font. More...
Data Structures | |
| union | rmgui_font_t |
| One font: an installed face and its glyph cache. More... | |
| struct | rmgui_font_metrics_t |
| Mutable measurement/drawing context, owned by one thread at a time. More... | |
| struct | rmgui_font_provider_t |
| Callback table for a host-supplied font rasterizer (for example, browser Canvas 2D or a native OS text stack) used with rmgui_font_init_provider. More... | |
Macros | |
| #define | RMGUI_FONT_BYTES |
| Bytes of caller storage one font occupies, including reserved capacity for private state; unchanged across patch releases within this ABI. | |
| #define | RMGUI_FONT_METRICS_PAIRS 1024u |
| Caller-owned shaping caches: pairs use at most PROBES consecutive slots and evict their home slot when that window is full; tokens are direct mapped. | |
| #define | RMGUI_FONT_METRICS_PROBES 16u |
| #define | RMGUI_FONT_METRICS_TOKENS 256u |
Typedefs | |
| typedef const rmgui_fb_t *(*) | rmgui_font_image_fn(void *user, uint32_t codepoint, int font_px) |
| The colour image a host has for one codepoint at one pixel size: straight- alpha RGBA8 pixels (width * height * 4 bytes, rows packed), or NULL when the codepoint is drawn as the face's glyph. | |
Functions | |
| void | rmgui_font_metrics_init (rmgui_font_metrics_t *metrics, const rmgui_font_t *font) |
| Initializes or clears metrics for the borrowed font; NULL is ignored. | |
| int | rmgui_font_metrics_measure_layout (const void *metrics, const char *text, int font_px, int max_w, int *out_w, int *out_h) |
| rmgui_text_measure_fn with the output and failure contract of rmgui_font_measure_layout. | |
| int | rmgui_font_metrics_measure_prefix (const void *metrics, const char *text, size_t length, size_t start, size_t end, int font_px, void *cursor, int *out_w, int *out_h) |
| rmgui_text_prefix_fn with the output and failure contract of rmgui_font_measure_prefix, filling the mutable metrics context's cache. | |
| int | rmgui_font_init (rmgui_font_t *font, const unsigned char *ttf, size_t length) |
| Installs the face in ttf[0, length) into font, with an empty glyph cache. | |
| int | rmgui_font_init_provider (rmgui_font_t *font, const rmgui_font_provider_t *provider, void *user) |
| Installs an external font provider into font, with an empty glyph cache. | |
| void | rmgui_font_fini (rmgui_font_t *font) |
| Frees everything drawing allocated for font and zero-fills it, so it holds no face and may be initialized again. | |
| uint64_t | rmgui_font_dropped_glyphs (const rmgui_font_t *font) |
| Monotonic count of failed glyph-cache requests since initialization, during CPU drawing or explicit-target preparation. | |
| void | rmgui_font_set_images (rmgui_font_t *font, rmgui_font_image_fn image, void *user) |
| Draws every non-ASCII codepoint for which image returns an image as that image instead of its glyph, in every painter that draws text with font: rmgui_font_draw, the CPU and record _to forms and the label, button, field and layout painters built on them. | |
| int | rmgui_font_covers (const rmgui_font_t *font, uint32_t codepoint, int font_px) |
| 1 for an installed cmap glyph or a non-ASCII registered colour image at font_px; 0 for missing coverage, invalid scalar, NULL/uninstalled font or font_px < 1. | |
| int | rmgui_font_measure (const rmgui_font_t *font, const char *text, size_t length, int font_px) |
| Width in pixels of text[0, length) at font_px, saturating at INT_MAX; the distance rmgui_font_draw advances the pen over the same bytes. | |
| int | rmgui_font_line_height (int font_px) |
| Line advance of one text line: font_px + 2, saturating at INT_MAX; 0 for font_px < 1. | |
| size_t | rmgui_font_fit (const rmgui_font_t *font, const char *text, size_t length, int font_px, int max_w) |
| Byte length of the longest prefix of text[0, length) whose width is at most max_w. | |
| int | rmgui_font_measure_layout (const void *font, const char *text, int font_px, int max_w, int *out_w, int *out_h) |
| rmgui_text_measure_fn over the rmgui_font_t font points to (the node's text_measure_userdata, or the kernel's userdata when NULL). | |
| int | rmgui_font_measure_prefix (const void *font, const char *text, size_t length, size_t start, size_t end, int font_px, void *cursor, int *out_w, int *out_h) |
| rmgui_text_prefix_fn over the rmgui_font_t font points to. | |
| void | rmgui_font_draw (rmgui_font_t *font, rmgui_fb_t *fb, rmgui_rect_t clip, int x, int y, const char *text, size_t length, int font_px, rmgui_color_t color) |
| Paints text[0, length) at font_px with its line box's top-left at (x, y) in color, coverage scaling color.a, blended over the framebuffer. | |
| void | rmgui_font_draw_with_metrics (rmgui_font_t *font, rmgui_font_metrics_t *metrics, rmgui_fb_t *fb, rmgui_rect_t clip, int x, int y, const char *text, size_t length, int font_px, rmgui_color_t color) |
| Same output and failure contract as rmgui_font_draw, sharing the optional mutable metrics cache with measurement. | |
font.h — the runtime-rasterized interface font.
Bytes-in: the library performs no file I/O and searches no paths. Hosts obtain their face themselves and install the bytes into a caller-owned rmgui_font_t with rmgui_font_init before any measuring or painting.
Ownership. A font is plain caller storage of RMGUI_FONT_BYTES: the face table and the glyph-cache index live inside it. The cache holds at most 1536 resident (glyph, pixel size) pairs across all sizes, not 1536 glyphs over the font's lifetime. Atlas space can force earlier eviction, depending on glyph and cell dimensions. Recency-based eviction reuses glyph records and atlas cells, frees empty bands for reuse and frees evicted standalone bitmaps. The lazily allocated glyph-record array and atlas pages (128 KiB each, at most 4) remain allocated until rmgui_font_fini; standalone bitmaps for glyphs too large for an atlas cell share a 16 MiB resident budget. rmgui_font_fini releases all remaining cache allocations. CPU drawing allocates only on cache misses; rasterizer scratch is freed before return. Measuring never allocates, and CPU drawing already-cached glyphs never allocates. Explicit RECORD targets have separate preparation and storage contracts (target_font.h). A zero-filled rmgui_font_t (static storage, or one released by rmgui_font_fini) holds no face: measures answer 0 (-1 where they return a status) and draws paint nothing.
Threads. A const rmgui_font_t * parameter only reads the font; any number of threads may measure one font at once while no thread draws with it, initializes it or finalizes it. rmgui_font_draw fills the glyph cache (its rmgui_font_t * parameter): a font being drawn with belongs to one thread at a time, so give each drawing thread its own font.
Carrier. Every library function that measures or draws text receives its font explicitly: painters take rmgui_font_t * as the parameter after the framebuffer, measure functions take const rmgui_font_t * first. State that measures between calls borrows a pointer that must outlive it: the layout kernel through rmgui_layout_set_font or a node's text_measure_userdata override, and the field through rmgui_field_metrics_t.font.
Sizes (font_px) are integer pixel heights; coordinates and widths are framebuffer pixels. A size below 1 measures 0 and draws nothing. Sizes below 12 px rasterize at 2x and box-downfilter once; 12 px and above rasterize directly.
Text is length bytes of strict UTF-8 (no terminator is read); each ill-formed maximal subpart counts as one U+FFFD. The repertoire is printable ASCII, U+0080..U+017F, U+2010..U+2015, U+2018..U+201F, U+2022, U+2026, U+2190..U+21FF, U+2300..U+23FF, U+2600..U+27BF, U+2B00..U+2BFF, U+1F000..U+1FAFF and U+FFFD. U+200D, U+FE0E and U+FE0F advance zero pixels and draw nothing, regardless of the face. Codepoints are independent: no clustering or ligatures, and the face has no colour glyphs; colour comes from images the host registers with rmgui_font_set_images. A codepoint outside the repertoire, or absent from the face, is drawn and measured as the face's U+FFFD when the face has one, otherwise as a box the width of the face's '?'. Measurements, fits and painting use the same advances; kerning is applied between fixed UI repertoire glyphs, including missing-glyph replacements, but not pairs involving a present glyph from the added ranges or a zero-width codepoint. The width of a run is the distance its drawing advances the pen.
| #define RMGUI_FONT_BYTES |
Bytes of caller storage one font occupies, including reserved capacity for private state; unchanged across patch releases within this ABI.
| #define RMGUI_FONT_METRICS_PAIRS 1024u |
Caller-owned shaping caches: pairs use at most PROBES consecutive slots and evict their home slot when that window is full; tokens are direct mapped.
Eviction changes work, never output. No cache lookup is unbounded.
| #define RMGUI_FONT_METRICS_PROBES 16u |
| #define RMGUI_FONT_METRICS_TOKENS 256u |
| typedef const rmgui_fb_t *(*) rmgui_font_image_fn(void *user, uint32_t codepoint, int font_px) |
The colour image a host has for one codepoint at one pixel size: straight- alpha RGBA8 pixels (width * height * 4 bytes, rows packed), or NULL when the codepoint is drawn as the face's glyph.
The face never sees the image: measuring, fitting and every advance are the glyph's. The callback is not called for ASCII, an ill-formed subpart or the zero-width U+200D, U+FE0E and U+FE0F, and must not draw or measure with the font that calls it.
| void rmgui_font_metrics_init | ( | rmgui_font_metrics_t * | metrics, |
| const rmgui_font_t * | font ) |
Initializes or clears metrics for the borrowed font; NULL is ignored.
| int rmgui_font_metrics_measure_layout | ( | const void * | metrics, |
| const char * | text, | ||
| int | font_px, | ||
| int | max_w, | ||
| int * | out_w, | ||
| int * | out_h ) |
rmgui_text_measure_fn with the output and failure contract of rmgui_font_measure_layout.
metrics points to a mutable rmgui_font_metrics_t, not a font; fills its bounded pair cache.
| int rmgui_font_metrics_measure_prefix | ( | const void * | metrics, |
| const char * | text, | ||
| size_t | length, | ||
| size_t | start, | ||
| size_t | end, | ||
| int | font_px, | ||
| void * | cursor, | ||
| int * | out_w, | ||
| int * | out_h ) |
rmgui_text_prefix_fn with the output and failure contract of rmgui_font_measure_prefix, filling the mutable metrics context's cache.
| int rmgui_font_init | ( | rmgui_font_t * | font, |
| const unsigned char * | ttf, | ||
| size_t | length ) |
Installs the face in ttf[0, length) into font, with an empty glyph cache.
The bytes MUST be a trusted, complete font the caller has already validated: the rasterizer is stb_truetype, which is not hardened against malformed or hostile font data and reads offsets taken from the font's own tables. length does NOT bound those reads — it only caps the accepted size — so a truncated or crafted face can make later measuring or painting read outside ttf. ttf is borrowed: it must stay valid and unchanged until rmgui_font_fini. font must not hold a live face (fresh storage, or released by rmgui_font_fini): initializing over a live face leaks its glyph cache. Returns 0, or -1 with font unchanged for a NULL font or ttf, a length of 0 or above 16 MiB, or bytes whose header stb_truetype cannot parse (a best-effort check, not validation).
| int rmgui_font_init_provider | ( | rmgui_font_t * | font, |
| const rmgui_font_provider_t * | provider, | ||
| void * | user ) |
Installs an external font provider into font, with an empty glyph cache.
provider and user are borrowed and must remain valid until rmgui_font_fini. font must not hold a live face (initializing over one leaks its glyph cache). Codepoints in U+0020..U+FFFF map directly to provider glyph queries; supplementary codepoints require host image callbacks or fallback faces. Pass a distinct user pointer per face when sharing a static provider table so rmgui_font_metrics_t distinguishes faces. Returns 0, or -1 with font unchanged if font, provider, or any required callback is NULL, or if provider->ascent <= provider->descent.
| void rmgui_font_fini | ( | rmgui_font_t * | font | ) |
Frees everything drawing allocated for font and zero-fills it, so it holds no face and may be initialized again.
Cannot fail; NULL is ignored.
| uint64_t rmgui_font_dropped_glyphs | ( | const rmgui_font_t * | font | ) |
Monotonic count of failed glyph-cache requests since initialization, during CPU drawing or explicit-target preparation.
Counts cache storage allocation failures, unrepresentable raster bounds, oversize bitmaps beyond the 16 MiB budget and reported raster preparation failures. Each counted failure occurs before that glyph is blended. An unchanged count means no such failure was reported, not that every glyph was visible. This is not a complete rasterizer or explicit-target error status. 0 for NULL.
| void rmgui_font_set_images | ( | rmgui_font_t * | font, |
| rmgui_font_image_fn | image, | ||
| void * | user ) |
Draws every non-ASCII codepoint for which image returns an image as that image instead of its glyph, in every painter that draws text with font: rmgui_font_draw, the CPU and record _to forms and the label, button, field and layout painters built on them.
The image is scaled to fit, aspect kept and centred, inside the codepoint's advance by the face's ascent-to-descent height, with bilinear/area sampling, and the text colour's alpha is its opacity; its colour is not used. A codepoint outside the repertoire has the advance of the face's replacement glyph, and a face without the glyph needs no raster for it. The image must stay unchanged until the draw call returns, and for a record target until the stream is submitted or discarded (a RECORD target pins it as any composite source; rmgui_target_source_stamped may declare it first). Call after rmgui_font_init; NULL image restores glyph drawing; rmgui_font_fini clears it. font and user are borrowed, and like the glyph cache the registration belongs to one thread at a time. A NULL font is ignored.
| int rmgui_font_covers | ( | const rmgui_font_t * | font, |
| uint32_t | codepoint, | ||
| int | font_px ) |
1 for an installed cmap glyph or a non-ASCII registered colour image at font_px; 0 for missing coverage, invalid scalar, NULL/uninstalled font or font_px < 1.
Joiners, selectors, paragraph controls and bidi formatting controls count as covered without a glyph. Does not substitute replacement glyphs or fill raster caches. Image callbacks must remain deterministic and read-only throughout a fallback request.
| int rmgui_font_measure | ( | const rmgui_font_t * | font, |
| const char * | text, | ||
| size_t | length, | ||
| int | font_px ) |
Width in pixels of text[0, length) at font_px, saturating at INT_MAX; the distance rmgui_font_draw advances the pen over the same bytes.
0 for a NULL font or text, an empty text, font_px < 1 or a font holding no face.
| int rmgui_font_line_height | ( | int | font_px | ) |
Line advance of one text line: font_px + 2, saturating at INT_MAX; 0 for font_px < 1.
The same for every face, so it takes no font.
| size_t rmgui_font_fit | ( | const rmgui_font_t * | font, |
| const char * | text, | ||
| size_t | length, | ||
| int | font_px, | ||
| int | max_w ) |
Byte length of the longest prefix of text[0, length) whose width is at most max_w.
Never divides a UTF-8 sequence or an ill-formed maximal subpart. 0 for a NULL font or text, an empty text, a negative max_w, font_px < 1 or a font holding no face.
| int rmgui_font_measure_layout | ( | const void * | font, |
| const char * | text, | ||
| int | font_px, | ||
| int | max_w, | ||
| int * | out_w, | ||
| int * | out_h ) |
rmgui_text_measure_fn over the rmgui_font_t font points to (the node's text_measure_userdata, or the kernel's userdata when NULL).
With max_w <= 0 it measures the NUL-terminated text as one line: *out_w = width, *out_h = line height. With max_w > 0 it wraps text exactly as the painters do, with rmgui_text_flow_line_prefix over rmgui_font_measure_prefix: *out_w is the widest line clamped to max_w, *out_h the sum of the line heights (one line for empty text). Returns 0, or -1 with the outputs unchanged for a NULL font, text or output, font_px < 1, a font holding no face, or a wrapped paragraph whose height exceeds INT_MAX. No text length cap.
| int rmgui_font_measure_prefix | ( | const void * | font, |
| const char * | text, | ||
| size_t | length, | ||
| size_t | start, | ||
| size_t | end, | ||
| int | font_px, | ||
| void * | cursor, | ||
| int * | out_w, | ||
| int * | out_h ) |
rmgui_text_prefix_fn over the rmgui_font_t font points to.
Writes the width of text[start, end) and the line height. cursor is a zeroed rmgui_text_prefix_cursor_t (text.h); successive calls with one cursor must pass the same font, text, length, start and font_px and a non-decreasing end, and cost only the bytes end advanced. Returns 0, or -1 for NULL arguments, start > end, end > length, a cursor reused against different arguments or a retreating end, font_px < 1 or a font holding no face. text must stay unchanged while the cursor is in use.
| void rmgui_font_draw | ( | rmgui_font_t * | font, |
| rmgui_fb_t * | fb, | ||
| rmgui_rect_t | clip, | ||
| int | x, | ||
| int | y, | ||
| const char * | text, | ||
| size_t | length, | ||
| int | font_px, | ||
| rmgui_color_t | color ) |
Paints text[0, length) at font_px with its line box's top-left at (x, y) in color, coverage scaling color.a, blended over the framebuffer.
Writes only pixels inside clip and the framebuffer. A glyph whose cache storage or raster preparation reports failure is skipped as a whole before blending and counted by rmgui_font_dropped_glyphs; its normal pen advance remains and later glyphs are still attempted. This is not whole-text atomicity. Nothing is drawn for a NULL font, framebuffer or text, color.a == 0, an empty clip, font_px < 1 or a font holding no face.
| void rmgui_font_draw_with_metrics | ( | rmgui_font_t * | font, |
| rmgui_font_metrics_t * | metrics, | ||
| rmgui_fb_t * | fb, | ||
| rmgui_rect_t | clip, | ||
| int | x, | ||
| int | y, | ||
| const char * | text, | ||
| size_t | length, | ||
| int | font_px, | ||
| rmgui_color_t | color ) |
Same output and failure contract as rmgui_font_draw, sharing the optional mutable metrics cache with measurement.
NULL metrics uses the uncached path. The explicit drawing font binds the cache for this call without changing metrics->font (the font borrowed by measurement).