fonttest

package
v0.1.0-alpha Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: Apache-2.0 Imports: 7 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 fonttest builds small fonts for tests: a TrueType writer (so no licensed font binary has to be committed), WOFF and WOFF2 encoders and a few ready-made fonts whose glyphs are plain boxes with distinct widths and heights, which makes rendered text easy to recognise and compare.

Nothing outside tests imports this package, so it adds nothing to the binaries.

Index

Constants

This section is empty.

Variables

View Source
var (
	//go:embed testdata/boxes.woff2
	BoxesWOFF2 []byte
	//go:embed testdata/icons.woff2
	IconsWOFF2 []byte
)

BoxesWOFF2 and IconsWOFF2 are Boxes("Boxy", "Regular", 400, false) and Icons("Icony") as WOFF2 files compressed by a real brotli encoder (see TestGenerateFixtures).

Functions

func Adv

func Adv(c rune) int

Adv is the advance of printable ASCII character c in the Boxes fonts.

func Assemble

func Assemble(flavor uint32, tables map[string][]byte) []byte

Assemble builds an sfnt from raw tables (4-byte aligned, correct checksums).

func Checksum

func Checksum(b []byte) uint32

Checksum is the sfnt table checksum.

func StoredBrotli

func StoredBrotli(b []byte) []byte

StoredBrotli wraps b in a valid brotli stream made of uncompressed meta-blocks.

func TransformGlyf

func TransformGlyf(glyf, loca, head, maxp []byte, overlap bool) (out []byte, xMins []int)

TransformGlyf produces the WOFF2 transformed glyf table for glyf/loca and the xMin of every glyph.

func WOFF

func WOFF(sfnt []byte, compress bool) []byte

WOFF encodes an sfnt as WOFF 1.0. Tables are zlib compressed when that makes them smaller and compress is set.

func WOFF2

func WOFF2(sfnt []byte, o Options) []byte

WOFF2 encodes an sfnt as WOFF 2.0.

Types

type Axis

type Axis struct {
	Tag           string
	Min, Def, Max float64
}

Axis is one fvar axis.

type Component

type Component struct {
	GID    int
	DX, DY int
}

Component places another glyph inside a composite.

type Font

type Font struct {
	Family, Style string
	UPM           int
	Ascent        int // hhea / typo ascender
	Descent       int // positive distance below the baseline
	LineGap       int
	XHeight, Cap  int
	Weight        int
	Italic        bool
	Mono          bool
	Glyphs        []Glyph // index 0 is .notdef
	Cmap          map[rune]int
	Ligatures     []Lig
	// Kern pairs as GPOS-less legacy 'kern' table entries (left gid, right gid, value in font units).
	Kern map[[2]int]int
	// ShortLoca selects the short loca format when the glyph data allows it.
	ShortLoca bool
	// ExtraTables are added verbatim (e.g. fvar and gvar of a variable font).
	ExtraTables map[string][]byte
}

Font describes a TrueType font.

func Boxes

func Boxes(family, style string, weight int, italic bool) *Font

Boxes returns a font with a glyph for every printable ASCII character plus U+00E9 (a composite of 'e' and an accent), U+0416 and U+4E2D (so unicode range tests have distinct scripts). unitsPerEm is 1000, ascent 800, descent 200. Glyph height depends on the character: 'x'-height-like for lowercase, taller for capitals and digits. weight changes the horizontal thickness only through the advance (bold faces are 8% wider) so the face can be told apart.

func Icons

func Icons(family string) *Font

Icons returns an icon font in the style of Material Icons: private use code points (U+E900..) draw icons, and ligatures turn the icon names ("home", "arrow_back", ...) into the same glyphs. The letters themselves draw nothing and have a tiny advance, as in the real font.

func VarBoxes

func VarBoxes(family string, withHVAR bool) *Font

VarBoxes is Boxes with a weight axis (wght 100..900, default 400): at the bold end (normalised +1) every glyph's right side moves right by 160 units and the advance grows by 160; at the light end (-1) the advance shrinks by 100. Deltas are given for all points, so the glyph outlines scale linearly with the coordinate. withHVAR chooses between advance deltas in HVAR and in the phantom points of gvar.

func (*Font) AddVariations

func (f *Font) AddVariations(v VarSpec)

AddVariations turns the font into a variable font.

func (*Font) SFNT

func (f *Font) SFNT() []byte

SFNT serialises the font.

type Glyph

type Glyph struct {
	Contours [][]Pt
	Comps    []Component
	Adv      int
	Instr    []byte // TrueType instructions (opaque bytes) of a simple glyph
}

Glyph is one glyph: simple (Contours) or composite (Comps).

type GlyphVar

type GlyphVar struct {
	HasShared bool  // the glyph has a shared point number set
	SharedPts []int // nil with HasShared: all points
	Tuples    []Tuple
}

GlyphVar is the variation data of one glyph.

type HVAR

type HVAR struct {
	Regions [][][3]float64 // [region][axis] = start, peak, end
	Deltas  [][]int        // [glyph][region]
}

HVAR is a minimal horizontal metrics variation table: one region per entry of Regions (axis triples), advance deltas per glyph and region.

type Lig

type Lig struct {
	Comps []int
	To    int
}

Lig is a GSUB ligature: the glyph sequence Comps (at least two) becomes To.

type Options

type Options struct {
	// Transform applies the glyf/loca transform (and the hmtx transform when
	// the font allows it); otherwise the null transform is used.
	Transform bool
	// Brotli compresses the stream; nil uses uncompressed meta-blocks.
	Brotli func([]byte) []byte
	// Overlap sets the OVERLAP_SIMPLE bit of every simple glyph (option flag 1).
	Overlap bool
}

Options for WOFF2.

type Pt

type Pt struct {
	X, Y int
	Off  bool
}

Pt is one outline point; Off marks an off-curve (quadratic control) point.

func Rect

func Rect(x0, y0, x1, y1 int) []Pt

Rect is a closed box contour (counter-clockwise, all points on curve).

type Table

type Table struct {
	Tag  string
	Data []byte
}

Table is one sfnt table.

func ParseTables

func ParseTables(sfnt []byte) []Table

ParseTables splits an sfnt into its tables in directory order.

type Tuple

type Tuple struct {
	SharedIdx    int       // index into VarSpec.Shared; -1 embeds Peak
	Peak         []float64 // embedded peak (normalised coordinates)
	Start, End   []float64 // intermediate region (both or none)
	Points       []int     // touched points; nil means all points (with the four phantom points)
	DX, DY       []int     // deltas of the listed points (or of all points)
	UseSharedPts bool      // take the point numbers from the glyph's shared point set
}

Tuple is one gvar tuple variation of a glyph.

type VarSpec

type VarSpec struct {
	Axes     []Axis
	Shared   [][]float64
	Glyphs   map[int]GlyphVar
	Avar     [][][2]float64 // per axis (from, to) pairs; nil: no avar table
	HVAR     *HVAR
	LongGvar bool // 32-bit glyph variation data offsets
}

VarSpec describes the variations of a font.

Jump to

Keyboard shortcuts

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