cffbuild

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: MIT Imports: 3 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 cffbuild assembles a bare CFF font program from nothing, for tests.

It exists for the reason internal/ttfbuild does: a test that needs a font either redistributes one it may not, skips in a clone, or builds what it needs, and font and render/native both need the same bytes — font, to check its parser reads what was written here, and render/native, to check a page set in this font rasterizes like the borrowed engine. 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.

Why every offset is written in the widest form

A Top or Font DICT operand that names an offset — charset, Encoding, CharStrings, Private, Subrs, FDArray, FDSelect — is written here in the fixed 5-byte form TN #5176 §4 calls the "29" integer, even where the value would fit in one byte. That form's size does not depend on its value, so every section's byte length is known before the offsets into later sections are, and Build lays the font out in one pass: write each section in order, and the moment a later section's start is known, go back and fill in the placeholder that was reserved for it. A shortest-form encoder would make a DICT's own length depend on the very offsets it is trying to compute the position of.

On the overflow annotations below

Every function here writes a chosen value into a big-endian binary field, and CFF's fields are written by reinterpreting the value's bits, which is what the format specifies: an offset, an SID, a code, or a count, cast down to the byte or word width its field declares. A scanner reads each of those as a possible overflow, dozens of times in one file. The annotations are at the function level with this one explanation, rather than at each site, because a suppression repeated dozens of times stops being read — and because every input here is a literal or a small computed value from this file or a caller's test data, never a value from outside the program.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CS

func CS(items ...any) []byte

CS encodes a charstring from operands (int or float64), operators (Op), and Raw bytes, in order. Integers use the shortest form of TN #5177 Table 1 (one byte for -107..107, two for ±108..1131, 28+int16 otherwise); a non-integer float64 uses the 255 16.16 fixed form. Panics on any other type.

func Diamond

func Diamond() []byte

Diamond is a closed shape of four rrcurveto cubics through (500,100), (900,500), (500,900) and (100,500) — roughly inscribed in the box (100,100)-(900,900) — so a render test has a glyph that differs from Square everywhere a glyph can.

func DictInt

func DictInt(v int32) []byte

DictInt encodes one DICT integer operand in the 5-byte 29 form, for building TopExtra. #nosec G115 -- see the package comment

func DictReal

func DictReal(v float64) []byte

DictReal encodes one DICT operand in the real (30-form) packed-decimal form, forcing that form even for a value dictNum would otherwise write as an integer, for building a TopExtra or FDArray Extra fixture that needs checkTopOccurrence's own real-operand checks (Private, ROS) to see hadReal set.

func Square

func Square() []byte

Square is a box from (100,100) to (400,400), drawn with rmoveto and one hlineto alternating horizontal and vertical, mirroring ttfbuild's square. Operands precede the operator they belong to, as every Type 2 charstring stack-based operator requires (TN #5177 §3).

Types

type Builder

type Builder struct {
	Glyphs []Glyph
	// Codes is the built-in encoding: Codes[i] is the code of GID i+1. With Codes nil and no
	// Supplements, the Top DICT names the predefined Standard encoding (offset 0) — the default.
	Codes          []byte
	EncodingFormat int // 0 or 1 (ranges of consecutive codes for consecutive GIDs)
	Supplements    []Supplement
	CharsetFormat  int       // 0, 1 or 2 (format 2 has a two-byte nLeft; ranges group consecutive IDs)
	ISOAdobe       bool      // name the predefined ISOAdobe charset (offset 0) and write none
	FontMatrix     []float64 // Top DICT FontMatrix, written when non-nil
	Subrs          [][]byte  // local subroutines of a name-keyed font's Private DICT
	GSubrs         [][]byte
	CID            bool       // CID-keyed: ROS (Adobe/Identity/0, both strings in the String INDEX), FDArray, FDSelect
	FDs            []FontDict // CID-keyed only; one empty FontDict when nil
	FDSelectFormat int        // 0 or 3
	// TopExtra is raw DICT bytes appended to the Top DICT after everything Build writes, so an
	// operator here overrides Build's (parseDict keeps the last); for refusal fixtures.
	TopExtra []byte
	Major    byte // header major version; 0 means 1
	// HeaderSize overrides the header's own length (hdrSize, data[2]): 0 means the default 4. A
	// shorter value trims the header's own trailing fixed field (offSize) before writing hdrSize; a
	// longer one pads with zero bytes after it. Build computes every later offset from the
	// resulting buffer length, so this is the only field a header-guard fixture needs to set; for
	// pinning ParseCFF's hdrSize bound at both a real font's own header size and one byte more.
	HeaderSize int
}

func (Builder) Build

func (b Builder) Build() []byte

Build assembles the font. See the package comment for the layout and why every offset is written in the fixed 5-byte form.

type FontDict

type FontDict struct {
	FontMatrix []float64 // written when non-nil
	Subrs      [][]byte  // local subroutines in its Private DICT
	// Extra is raw DICT bytes appended after everything fontDictBytes writes, the FDArray entry's
	// own analogue of Builder.TopExtra: an operator here overrides fontDictBytes's own (parseDict
	// keeps the last), for a refusal fixture that needs a bad occurrence inside one Font DICT.
	Extra []byte
}

FontDict is one FDArray entry of a CID-keyed font.

type Glyph

type Glyph struct {
	SID        uint16 // charset entry for a name-keyed font when Name is "": a standard string ID
	Name       string // charset entry for a name-keyed font: appended to the String INDEX (SID 391 up)
	CID        uint16 // charset entry for a CID-keyed font
	FD         uint8  // the Font DICT a CID-keyed glyph uses
	CharString []byte
	// EmptyName mints a fresh SID naming an empty String INDEX entry, overriding Name and SID.
	// Name alone cannot write this: an empty Name falls back to SID (see charsetIDsAndStrings),
	// and a font's own custom names are otherwise never empty.
	EmptyName bool
}

Glyph is one CharStrings entry. GID 0 is always .notdef, written by Build as `endchar`; Glyphs are GIDs 1 up.

type Op

type Op int

Op is a Type 2 charstring operator. A two-byte (escaped) operator is 1200 plus its second byte.

const (
	HStem      Op = 1
	VStem      Op = 3
	VMoveTo    Op = 4
	RLineTo    Op = 5
	HLineTo    Op = 6
	VLineTo    Op = 7
	RRCurveTo  Op = 8
	CallSubr   Op = 10
	Return     Op = 11
	EndChar    Op = 14
	HStemHM    Op = 18
	HintMask   Op = 19
	CntrMask   Op = 20
	RMoveTo    Op = 21
	HMoveTo    Op = 22
	VStemHM    Op = 23
	RCurveLine Op = 24
	RLineCurve Op = 25
	VVCurveTo  Op = 26
	HHCurveTo  Op = 27
	CallGSubr  Op = 29
	VHCurveTo  Op = 30
	HVCurveTo  Op = 31

	DotSection Op = 1200
	And        Op = 1203
	Add        Op = 1210
	Random     Op = 1223
	HFlex      Op = 1234
	Flex       Op = 1235
	HFlex1     Op = 1236
	Flex1      Op = 1237
)

type Raw

type Raw []byte

Raw is bytes written into a charstring verbatim: a hintmask's mask bytes, or a malformed tail.

type Supplement

type Supplement struct {
	Code byte
	SID  uint16
}

Supplement is an encoding supplement: a code, and the SID of the glyph it names.

Jump to

Keyboard shortcuts

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