ttfbuild

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ttfbuild assembles a TrueType font from nothing, for tests.

It exists because every real font is someone's copyright and the corpus's are inside gitignored ISO documents, so a test that needs a font either redistributes one it may not, skips in a clone, or builds what it needs. Two packages need the same bytes — font, to check that its parser reads the outlines that were written here, and render/native, to check that a page set in this font rasterizes like the borrowed engine says — and a font builder duplicated in two test files is the same defect as arithmetic duplicated in two packages: a fix to one is invisible to the other.

Not in a _test.go file, because an internal package cannot export from one. Internal so it cannot become part of the module's surface by accident.

On the overflow annotations below

Every function here writes chosen constants into big-endian font fields, and a font's signed fields — a coordinate, an ascender, a side bearing — are written by reinterpreting the value's two's-complement bits, which is what the format specifies. A scanner reads each of those as a possible overflow, thirty-two times in one file. The annotations are at the function level with this one explanation, rather than at each site, because a suppression repeated thirty-two times stops being read — and because every input here is a literal in this file.

Index

Constants

View Source
const (
	GIDSquare    = 1
	GIDDiamond   = 2
	GIDComposite = 3
	// GIDRing is two squares, one inside the other and wound the *same* direction, which is the
	// only shape that tells the two fill rules apart. Nonzero fills the middle, because both
	// contours wind the same way and the winding number there is two; even-odd hollows it,
	// because two crossings is even. A real font winds a counter the opposite way to its outer
	// contour, where both rules agree and neither can be caught being the other — so a glyph
	// wound this way is the fixture, and same-direction nested contours do occur, in overlapping
	// composite components and in fonts built by hand.
	GIDRing = 4
	// GIDSpace is a glyph with no ink, mapped from U+0020 the way every real font maps it.
	//
	// It is here because the first version of this font stopped at 'C', and a page reading
	// "A A" was then refused for having no glyph for code 32 — correctly, by the contract that
	// a code the page draws and this backend cannot resolve stops the page. The defect was the
	// font: a real one carries a space glyph with an empty outline, so a fixture without one
	// tests a font shape no producer emits. Not glyph 0, which is notdef: a cmap entry
	// resolving to 0 means "this font has no glyph for that character", which is the one value
	// a lookup has to read as absence rather than as an answer.
	GIDSpace  = 5
	NumGlyphs = 6
)

Variables

This section is empty.

Functions

func AssembleTables

func AssembleTables(tables map[string][]byte) []byte

assembleTables writes a table directory and the tables, four-byte aligned. #nosec G115 -- font fields are written by reinterpretation; see the package comment

func AssembleTablesOrdered

func AssembleTablesOrdered(entries []Entry) []byte

AssembleTablesOrdered writes a table directory in exactly the given order, with no sort and no duplicate-tag merge. #nosec G115 -- font fields are written by reinterpretation; see the package comment

Types

type Builder

type Builder struct {
	UnitsPerEm uint16
	LongLoca   bool
	// NoCmap omits the cmap table, which is what a subsetted symbolic font looks like: there is
	// no character map to read it with, so a consumer's only route to a glyph is the code itself.
	NoCmap bool
	// SymbolicCmap adds a (3,0) subtable keyed by code, which is what a symbolic font carries
	// alongside or instead of the (3,1) one keyed by character.
	SymbolicCmap bool
	// Cmaps replaces the default subtables with these, in this order, for the fixtures whose
	// question is which subtable a reader consults.
	Cmaps []Cmap
	// PostNames, when non-nil, replaces the default format 3.0 `post` table (which the TrueType
	// spec's own header comment calls "no PostScript name information") with a format 2.0 one
	// naming glyph i PostNames[i], for the fixtures whose question is the charmap FreeType
	// synthesizes from those names when no cmap subtable survives. An empty string leaves that
	// glyph unnamed.
	PostNames []string
}

func (Builder) Build

func (tb Builder) Build() []byte

build assembles the font: head, maxp, loca, glyf, cmap. #nosec G115 -- font fields are written by reinterpretation; see the package comment

type Cmap

type Cmap struct {
	Platform, Encoding uint16
	Map                map[uint16]uint16
}

Cmap is one cmap subtable: the platform and encoding its record names, and the codes it maps to glyphs. It is written as format 4 with a segment per code.

type Entry

type Entry struct {
	Tag  string
	Data []byte
}

Entry is one table-directory record: a tag and its bytes, written exactly in the order given.

AssembleTables' map can hold only one entry per tag and always writes it sorted, which is right for a well-formed font but cannot express the shapes ParseTrueType's directory rules are about: a repeated tag, so which one wins is the question; a zero-length entry ahead of a real one; or records in an order a real subsetter would not choose. AssembleTablesOrdered writes exactly the list given, so a test can put those shapes in the file rather than describe them.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL