outline_convert

index

Overview

The outline font converter: TrueType fonts in, one outline table out. The table is what gfx.outline_font draws from; docs/lilgame/OUTLINE.md holds its layout.

This is a development script, run by hand, and no binary carries it:

lilgame src/gfx/outline_convert.lua <manifest.lua> [--out <file>] [--module <file.lua>] [--report]

--out writes the table's bytes, --module writes them as a Lua module that returns { bytes = "..." }, and --report prints the sizes and the holders of the codepoints the manifest wants. A run with neither output reports. It exits 1 on an error, and on a wanted codepoint no source holds.

A manifest is a Lua file that returns a table. Paths in it count from the manifest's directory.

fielddescription
sourcesthe fonts, in the order they are asked for a codepoint
dropranges { first, last } of codepoints no source gives
noticea text file: the copyright lines and the licence the table carries
advancethe cell's advance in font units; the first face's space otherwise
wantcodepoints the report looks up, each with its holder
headercomment lines the module opens with
featuresGSUB feature tags whose rules the table takes, such as calt
source fielddescription
namethe name the report prints
filesone file for every face, or a file a face
sha256a hex digest a file; a file that differs is refused
cellsthe cells its glyphs take, 1 or 2; 1 when left out
rangesranges { first, last } it gives; everything when left out

The first source names the faces, the em and the metrics. A face takes a codepoint from the first source that holds it. A later source's glyph is scaled to the table's em, shrunk further when its advance overruns its cells, and centred in them.

It reads cmap in formats 4 and 12, loca, glyf, hmtx, head, hhea, maxp, OS/2 and post. A composite glyph is flattened to contours. It reads no CFF, and no composite placed by point matching. It takes no hinting.

With features named, it reads the first source's GSUB: the features with those tags in the DFLT script's default language, and their lookups in the order of the lookup list. A lookup is a chaining context, type 6 in format 1 or 3, and a rule runs one single substitution, type 1, on its one input glyph, or none; type 7 may wrap either. Any other lookup, a lookup with flags, a rule of more than one input glyph or substitution, and a GSUB with no DFLT stop the run with the lookup's number. Each face takes its own file's rules.

A glyph no codepoint holds joins the table when a rule can make it. A glyph that can never occur leaves the rules' sets, and a rule left with an empty set can never fit: it is dropped, and the report counts it. A glyph a rule names keeps its own id, apart from a twin of the same outline, so the table shapes as the font does.

The same sources give the same bytes.

Functions

NameSignature
ttfttf(bytes) -> font, err
encodeencode(desc) -> bytes, stats
convertconvert(manifest, read) -> bytes, stats
modulemodule(bytes, comment) -> source

ttf(bytes) -> font, err

Read a TrueType font

font.cfg holds the metrics in the font's units: em, ascender, descender, x_height, cap_height, underline_pos, underline_thickness, strike_pos and strike_thickness. A font without OS/2 or post reads 0 for what those tables hold.

font:glyph(codepoint) answers the glyph's index, or nil. font:codepoints() answers the whole map, codepoint to index. font:advance(index) answers the advance. font:outline(index) answers the contours, each a list of points { x, y, on }, with a composite flattened; or nil and an error. font:rules(tags) answers the GSUB lookups the module's block describes, each { index, rules }, a rule { input, behind, ahead, map } with its sets as index -> true and its map as index -> index or nil; or nil and an error naming the lookup it cannot express. A font with no GSUB, or no tags, answers an empty list.

Nil and an error for a file that is no TrueType font, holds CFF outlines, or ends before a table it names.

encode(desc) -> bytes, stats

Write an outline table

desc fielddescription
metricsem, ascender, descender, advance, x_height, cap_height, underline_pos, underline_thickness, strike_pos, strike_thickness, in font units
faces1 to 4 maps of codepoint to glyph, in the order regular, bold, italic, bold italic
rulesa list of lookups a face, each a list of rules; none when left out
noticethe text the table carries, empty when left out

A glyph is { cells = 1 or 2, contours = { { { x, y, on }, ... }, ... } } with integer points in font units; tag names its source for the stats. Glyphs with the same record share one glyph in the table, unless a gid tells them apart. A codepoint whose record is the same in every face goes to the pool.

A rule is { input, behind, ahead, map } over glyphs. input is a list of glyphs, and behind and ahead are lists of such lists, behind nearest first. map is a list of { from, to } pairs, or nil for a rule that substitutes nothing. A glyph a rule names that no codepoint holds is stored after the others.

The stats count glyphs, codepoints and pooled codepoints, the glyph_bytes, range_bytes and index_bytes, and under tags the glyphs and bytes a tag. They count the lookups, the rules and their rule_bytes, and the extra_glyphs only rules name with their extra_glyph_bytes.

Nil and an error for a point or a metric that does not fit 16 bits, a glyph of other than 1 or 2 cells, a count of faces out of range, or a rule that maps one glyph to two.

convert(manifest, read) -> bytes, stats

Merge a manifest's fonts into an outline table

read(path) answers a file's bytes, or nil and an error; the manifest's paths reach it as written. The module's own block describes the manifest.

The stats are encode's, with four fields added: holders, a map of codepoint to the name of the source the first face took it from; notice, the notice's text; dead_rules, the rules dropped as never fitting; and gids, a map a face of the first source's glyph indices to the table's ids, for the glyphs the table holds.

Nil and an error for a file read does not give, a digest that differs, a font ttf refuses, a glyph that cannot be read, a GSUB lookup it cannot express, or what encode refuses.

module(bytes, comment) -> source

Write a table's bytes as a Lua module

The module returns { bytes = "..." }. Every byte is a decimal escape, 64 a line, each line closed by \z, so the source diffs by line and the string needs no decode at load. comment opens the file, each of its lines behind -- .