cff

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package cff reads and subsets CFF font programs: the Type 2 outlines of OpenType fonts with a "CFF " table, and those PDF files embed.

Index

Constants

This section is empty.

Variables

View Source
var ErrTooLong = errors.New("cff: charstrings run too long")

ErrTooLong is returned by Subset and Prune for a font whose charstrings run too long to follow, or leave too many operands on the stack.

View Source
var StandardEncoding = [256]string{}/* 256 elements not displayed */

StandardEncoding is Adobe's standard encoding (PDF 32000-1 Annex D): glyph name per code.

View Source
var StandardStrings = []string{} /* 391 elements not displayed */

StandardStrings are the 391 predefined CFF strings (CFF spec Appendix A).

Functions

func EncodeDict

func EncodeDict(entries []DictEntry, replace func(DictEntry) ([]int, bool)) []byte

cffEncodeDict writes entries back; replace returns new integer operands for an entry (written as 5-byte integers) or false to keep it as it was.

func Index

func Index(items [][]byte) []byte

cffIndex writes an INDEX.

func ParseDict

func ParseDict(b []byte) map[int][]float64

cffParseDict parses a CFF DICT into operator → operands. ParseDict reads a DICT into operator → operands (12 x is 1200+x).

func Prune

func Prune(data []byte, keep map[int]bool) ([]byte, error)

Prune is Subset that keeps the glyph numbers: the glyphs not in keep (nor glyph 0 or a component of a kept seac accent) stay in the font, empty, so that tables which refer to glyphs by number (GSUB, GPOS, hmtx) stay valid. The subroutines only they call become a bare return.

func ReadIndex

func ReadIndex(b []byte, pos int) ([][]byte, int, error)

ReadIndex reads the INDEX at pos and returns its items and the position after it.

func Subset

func Subset(data []byte, keep map[int]bool) (out []byte, order []int, err error)

Subset returns a CFF font program that holds only the glyphs in keep, glyph 0 and the components of seac accents, renumbered in their original order; order[newGID] is the old GID. Subroutines the kept glyphs do not call become a bare return (their numbers must not change). The charset keeps each glyph's name or CID, FDSelect is rebuilt, and a custom encoding is replaced by StandardEncoding: its codes point at old GIDs, and browsers map characters through the cmap anyway.

Dropping glyphs instead of emptying them matters for CFF: its CharStrings INDEX has an offset per glyph, and in a CJK font those offsets alone are far larger than the few glyphs a document uses (TrueType's loca has no such cost in WOFF2, which rebuilds it).

When a charstring cannot be followed (arithmetic operators, a subroutine number out of range) every subroutine is kept and only the glyphs are dropped. Charstrings that run longer than a font's size explains (subroutines that call subroutines many times over) are an error: ErrTooLong.

Types

type DictEntry

type DictEntry struct {
	Op   int // 12 x is 1200+x
	Args []float64
	Raw  []byte // operand bytes
}

DictEntry is one operator of a DICT with its operands, kept as written.

func DictEntries

func DictEntries(b []byte) ([]DictEntry, error)

type Font

type Font struct {
	Data       []byte
	NumGlyphs  int
	IsCID      bool
	Charset    []int // gid → SID (or CID)
	NameToGID  map[string]int
	CIDToGID   map[int]int
	Encoding   map[int]int // code → gid (built-in encoding)
	FontMatrix [6]float64
	Strings    []string
	// Notice and Copyright strings of the Top DICT (the only license text a
	// bare CFF program carries).
	Notice, Copyright string
}

Font holds what the converters need from a CFF font program (bare, as PDF embeds it, or the CFF table of an OpenType font): glyph count, glyph names (or CIDs) per GID, the built-in encoding and the font matrix.

func Parse

func Parse(data []byte) (*Font, error)

Parse reads a CFF font program.

func (*Font) SIDName

func (f *Font) SIDName(sid int) string

SIDName returns the string of a string ID.

Jump to

Keyboard shortcuts

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