gfx.outline

index · gfx

Overview

Outline text: glyphs drawn at any size from an outline table.

The table holds a monospaced font as contours: up to four faces, and a pool of glyphs every face draws the same. src/gfx/outline_convert.lua writes it from TrueType fonts at development time, and a program hands its bytes to outline_font. The runtime parses no font file.

A glyph is filled once a size into a white mask whose alpha is the exact area the outline covers in each pixel. The mask is a gfx.sprite, so canvas:blit(mask, x, y, { tint = color }) draws it in any colour.

A size is the em in pixels. The font snaps it so the x-height is a whole count of pixels, and the cell and the baseline are whole pixels at that scale. Text set a cell a glyph then lands every glyph on the same rows.

A table may carry a face's contextual rules, a font's ligatures among them. font:shape runs them over a run of codepoints and answers a glyph a codepoint, and font:glyph_mask draws a glyph by its id.

Functions

NameSignature
outline_fontoutline_font(table, opts) -> font, err

outline_font(table, opts) -> font, err

Create an outline font over a table

table is an outline table's bytes. opts.cache_bytes bounds the masks the font keeps, 16 MB when left out. Nil and an error for bytes that are no outline table, or one cut short.

font.cfg holds the table's metrics in font units, as font:metrics() answers them, and cache_bytes.

font:metrics() answers a table:

fielddescription
faces, glyphsthe counts
emfont units to the em
ascender, descenderfrom the baseline; the descender is negative
advanceone cell's width
x_height, cap_heightfrom the baseline; 0 where the font names none
underline_pos, underline_thicknessthe underline's top from the baseline, and its height
strike_pos, strike_thicknessthe strikeout's the same

font:notice() answers the text the table carries: the copyright lines and the licence of the fonts it was made from.

font:cell(px) answers w, h, baseline, scale. px is the em in pixels and takes a fraction. scale is pixels a font unit, snapped so the x-height is a whole count of pixels, 1 at the least; a table with no x-height is not snapped. w and h are the cell at that scale, rounded, and baseline is the rows from the cell's top down to the baseline. A line n units above the baseline sits baseline - n * scale rows down.

font:mask(codepoint, face, px) answers mask, x, y, cells. face counts from 1: regular, bold, italic, bold italic, as many as the table holds; a face out of that range raises. cells is 1 or 2, the cells the glyph was made for. mask is a sprite to blit x, y pixels right of and below the top-left of the glyph's first cell; a glyph that overhangs its cell has a negative x or y. It answers false in place of the mask for a glyph with no ink, a space, and nil alone for a codepoint the table lacks. Nil and an error when the mask cannot be made.

A glyph's advance box is centred in its cells, so the part of a pixel a cell has over or under the advance is split between its two sides.

font:shape(codepoints, face, i, j) shapes codepoints[i] to codepoints[j] as one run. i and j default to 1 and #codepoints, so a row's codepoints shape a run at a time with no copy. It answers a list as long as the run, its first entry for codepoints[i]: the glyph id each codepoint takes in the face once the face's rules have run, or false for a codepoint the table lacks. A false matches no rule. A j below i answers an empty list. An entry of the run that is not a number raises. The list is read raw: a metatable's __index is not asked.

Each lookup walks the run from left to right and substitutes in place, so a rule sees this lookup's own work behind it and the run as it stands ahead of it. The first rule that fits a glyph ends the lookup there, even one that substitutes nothing. The run's ends are the edge of the context: a rule that looks past them does not fit, whatever codepoints holds outside the run. A face with no rules answers each codepoint's own glyph.

font:glyph_mask(id, px) answers what font:mask does, for a glyph id that shape answered. An id names one glyph in every face. An id that is not one of the table's raises.

Masks are kept by size and glyph, and a glyph the faces share is drawn once. Past cache_bytes the size used longest ago is dropped whole; a size that outgrows the bound alone starts again. A mask a program still holds stays valid.