Low level surface
Raw tables and raw records, for code that walks the atlas itself or owns its own instance memory.
| Call | What it does |
|---|---|
text_font_find | The text_font_glyph for a face and codepoint, or null. |
text_font_kerning | The kern between two codepoints, per em. Multiply by the pixel size. |
text_font_utf8_next | Decodes one codepoint and moves the cursor. It returns zero on a broken sequence and still steps one byte. A null end walks to the NUL. |
text_font_glyph_record | One glyph to one gfx_glyph at a baseline. It returns false when the glyph has no ink. |
text_font_emit | A whole run into your array, from *count up to cap. It moves *count, and glyphs past cap are dropped. |
text_font_emit_in | The same, aligned inside a width. A nonzero limit keeps only that many glyphs and aligns what was kept. |
The ASCII fast table
text_font::ascii maps each face and each printable ASCII character, from TEXT_ASCII_LOW up to but not including TEXT_ASCII_HIGH, straight to a glyph index. TEXT_GLYPH_NONE (0xffff) marks a character the atlas does not carry in that face, and lookups outside the table fall back to a binary search.
Emitting into a list
text_font_emit writes wherever you point it. Pointing it at the free tail of a gfx_list and then committing with gfx_glyphs_placed is exactly how text_font_draw is built. Nothing may record into the list between the two calls.
#include "isolenui/text/text_font.h"
#include <string.h>
// walk a run codepoint by codepoint, look each glyph up and kern each pair,
// the same tables text_font_measure reads, advances and kerns are per em so
// they scale by the pixel size directly
static float manual_width(const text_font* f, uint32_t face, const char* utf8,
float px)
{
float pen = 0.0f;
uint32_t prev = 0;
const char* cursor = utf8;
// a null end walks to the nul, zero means a broken sequence
while (*cursor) {
const uint32_t cp = text_font_utf8_next(&cursor, nullptr);
const text_font_glyph* g = cp ? text_font_find(f, face, cp) : nullptr;
if (!g) {
prev = 0;
continue;
}
if (prev) {
pen += text_font_kerning(f, face, prev, cp) * px;
}
pen += g->advance * px;
prev = cp;
}
return pen;
}
// the ascii fast table holds a glyph index per face, TEXT_GLYPH_NONE marks a
// character the atlas does not carry in that face
static bool has_ascii(const text_font* f, uint32_t face, char c)
{
const uint32_t cp = uint32_t(uint8_t(c));
if (face >= TEXT_MAX_FACES || cp < TEXT_ASCII_LOW || cp >= TEXT_ASCII_HIGH) {
return false;
}
return f->ascii[face][cp - TEXT_ASCII_LOW] != TEXT_GLYPH_NONE;
}
// emit writes records into memory the caller owns, here the free tail of a
// draw list, then gfx_glyphs_placed commits them, which is exactly how
// text_font_draw is built
static float draw_into_tail(const text_font* f, gfx_list* l, gfx_font handle,
const char* utf8, float x, float baseline)
{
uint32_t room = 0;
gfx_glyph* tail = gfx_glyphs_tail(l, uint32_t(strlen(utf8)), &room);
uint32_t n = 0;
if (!tail) {
return x;
}
const float pen = text_font_emit(f, 0u, utf8, TEXT_FONT_TERMINATED, x,
baseline, 18.0f, 0.0f, 0xffffffffu, tail,
&n, room);
if (n) {
gfx_glyphs_placed(l, handle, n);
}
return pen;
}
// a typewriter reveal, limit keeps the first glyphs only and emit_in aligns
// just what was kept, into a buffer of the caller's own
static uint32_t typewriter(const text_font* f, const char* line, float t,
gfx_glyph* out, uint32_t cap)
{
uint32_t count = 0;
const uint32_t shown = uint32_t(t * 30.0f) + 1u;
text_font_emit_in(f, 0u, line, TEXT_FONT_TERMINATED, 0.0f, 640.0f, 48.0f,
32.0f, 0.0f, 0xffffffffu, text_font_centre, shown, out,
&count, cap);
return count;
}
// one glyph to one record, false when the glyph has no ink, a space say
static bool single_glyph(const text_font* f, uint32_t cp, gfx_glyph* out)
{
const text_font_glyph* g = text_font_find(f, 0u, cp);
return g && text_font_glyph_record(f, g, 0.0f, 32.0f, 32.0f, 0xffffffffu, out);
}Laying a line to bend
text_font_lay_dir(f, face, utf8, len, px, tracking, dir, out, cap, &width, &level) lays one line the way the direction aware draw would, shaped, kerned and in visual order, but hands the glyphs back instead of drawing them. Each text_font_laid holds the gfx_glyph record rec on a baseline at y zero from x zero, its pen position and advance along the line, and fallback, set when the record draws from the fallback atlas. It returns how many landed, up to cap, and glyphs with no ink are skipped. width gets the line's width and level the paragraph level, odd for right to left.
Move, bend or turn the records, set their colour, then place them. A record with fallback set goes out under f->fallback_handle and the rest under the font's own handle, each change of atlas as its own placed group. Turned glyphs turn about their centre, which is how arc labels follow their circle.
#include "isolenui/gfx/gfx_list.h"
#include "isolenui/text/text_font.h"
#include <math.h>
// lays one line, then rides every glyph along a wave and turns it to follow
// the slope, the same steps arc_label takes around a circle
static void record_wave(gfx_list* l, const text_font* f, gfx_font fh, const char* text,
float x, float y, float px, uint32_t rgba)
{
text_font_laid laid[128];
float width = 0.0f;
uint8_t level = 0u;
const uint32_t n = text_font_lay_dir(f, 0u, text, TEXT_FONT_TERMINATED, px, 0.0f,
text_dir_auto, laid, 128u, &width, &level);
for (uint32_t i = 0; i < n; ++i) {
gfx_glyph& g = laid[i].rec;
const float mid = laid[i].pen + laid[i].advance * 0.5f;
const float lift = 8.0f * sinf(mid * 0.05f);
g.pos[0] += x;
g.pos[1] += y + lift;
g.color = rgba;
gfx_glyph_turn(&g, atanf(0.4f * cosf(mid * 0.05f)));
}
// a fallback glyph draws from the fallback atlas, so each change of atlas
// is its own placed group
uint32_t i = 0u;
while (i < n) {
uint32_t e = i;
while (e < n && laid[e].fallback == laid[i].fallback) {
++e;
}
const gfx_font font = laid[i].fallback ? f->fallback_handle : fh;
uint32_t room = 0u;
gfx_glyph* out = gfx_glyphs_tail(l, e - i, &room);
const uint32_t k = e - i < room ? e - i : room;
for (uint32_t j = 0; j < k; ++j) {
out[j] = laid[i + j].rec;
}
gfx_glyphs_placed(l, font, k);
i = e;
}
(void)width;
(void)level;
}