otlayout

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package otlayout reads the OpenType layout tables GSUB, GPOS and GDEF: the scripts and language systems a font declares, its features and the lookups they apply, and what each lookup substitutes or positions. It lists what a font can do, as a font viewer shows it; it does not shape text.

Fonts are untrusted input: offsets and counts are checked against the table, and what a table expands to (the glyphs of a coverage or class definition, the pairs of a class-based kerning lookup) is bounded. Records may share what they point at, so that a small table says it holds millions of features or subtables: a table is read into maxElements of them at most (ErrTooLarge), and its lookups list so many entries in all the calls made of them (maxSteps), then no more (Truncated).

Index

Constants

View Source
const (
	GlyphBase      = 1
	GlyphLigature  = 2
	GlyphMark      = 3
	GlyphComponent = 4
)

Glyph classes of the GDEF table.

View Source
const (
	// Optional features apply only when the text asks for them
	// (font-feature-settings, font-variant).
	Optional = iota
	// Default features apply to all horizontal text.
	Default
	// ScriptDefault features apply where the shaper of a script calls for
	// them (the joining forms of Arabic, the conjuncts of Indic scripts).
	ScriptDefault
	// Vertical features apply to vertical text.
	Vertical
)

Feature defaults: when browsers apply a feature to text without being asked to (with HarfBuzz, as Chrome and Firefox do).

View Source
const (
	RightToLeft         = 0x0001
	IgnoreBaseGlyphs    = 0x0002
	IgnoreLigatures     = 0x0004
	IgnoreMarks         = 0x0008
	UseMarkFilteringSet = 0x0010
	MarkAttachmentType  = 0xFF00
)

Lookup flags.

View Source
const (
	SubstSingle       = 1
	SubstMultiple     = 2
	SubstAlternate    = 3
	SubstLigature     = 4
	SubstContext      = 5
	SubstChainContext = 6

	SubstReverseChain = 8
)

GSUB lookup types.

View Source
const (
	PosSingle       = 1
	PosPair         = 2
	PosCursive      = 3
	PosMarkToBase   = 4
	PosMarkToLig    = 5
	PosMarkToMark   = 6
	PosContext      = 7
	PosChainContext = 8
)

GPOS lookup types.

Variables

View Source
var ErrNotLayout = errors.New("otlayout: not a layout table")

ErrNotLayout is returned for data that is not a GSUB or GPOS table.

View Source
var ErrTooLarge = errors.New("otlayout: the table holds too many features or lookups")

ErrTooLarge is returned for a table that holds more scripts, features, lookups and subtables than a table is read into.

Functions

func FeatureDefault

func FeatureDefault(tag string) int

FeatureDefault tells when browsers apply a feature.

func FeatureName

func FeatureName(tag string) string

FeatureName returns the name of a registered feature tag ("Standard Ligatures" for "liga"), or "" for tags the OpenType feature registry does not list.

func LangName

func LangName(tag string) string

LangName returns the name of a language system tag ("Japanese" for "JAN"), or "" for tags it does not know.

func ScriptName

func ScriptName(tag string) string

ScriptName returns the name of a script tag ("Latin" for "latn"), or "" for tags it does not know.

Types

type Anchor

type Anchor struct{ X, Y int }

Anchor is a point a glyph attaches to, in font units.

type Attachment

type Attachment struct {
	Marks map[uint16]MarkAnchor
	Bases map[uint16][]*Anchor // indexed by mark class; nil when the base has none for the class
}

Attachment is a subtable of a mark to base or mark to mark lookup: the class and anchor of each mark, and for each glyph the marks attach to, its anchor for each class.

type Context

type Context struct {
	Rules int
	// Nested are the lookups the rules apply, in the order they first
	// appear, each once.
	Nested []int
}

Context is what a contextual lookup does: how many rules it has and the lookups its rules apply to the glyphs they match.

type Feature

type Feature struct {
	Tag     string
	Lookups []int
	// UIName is the name ID of the name a stylistic set (ss01–ss20) or a
	// character variant (cv01–cv99) gives itself, 0 when it has none.
	UIName uint16
	// Chars are the characters a character variant applies to.
	Chars []rune
}

Feature is a feature of the table.

type GDEF

type GDEF struct {
	Classes    map[uint16]int // glyph → GlyphBase, GlyphLigature, GlyphMark or GlyphComponent
	MarkAttach map[uint16]int // mark → mark attachment class
	MarkSets   [][]uint16     // the first sets, when they hold more than a million glyphs together
}

GDEF is the glyph definition table: which glyphs are bases, ligatures, marks and components, the classes marks attach by, and the glyph sets lookups can restrict themselves to.

func ParseGDEF

func ParseGDEF(b []byte) *GDEF

ParseGDEF reads a GDEF table; it returns nil when b is not one.

type LangSys

type LangSys struct {
	Tag string // "" for a script's default
	// Required is the index of the feature that always applies, -1 when
	// there is none.
	Required int
	Features []int
}

LangSys is a language system: the features that apply to text of a script in a language.

type Lookup

type Lookup struct {
	// Type is the lookup type. An extension lookup has the type of the
	// subtables it points to.
	Type int
	Flag uint16
	// MarkSet is the index of the mark glyph set the lookup is restricted
	// to (see GDEF.MarkSets), -1 when there is none.
	MarkSet int
	// contains filtered or unexported fields
}

Lookup is a lookup of the table.

type MarkAnchor

type MarkAnchor struct {
	Class  int
	Anchor Anchor
}

MarkAnchor is the class of a mark and its anchor.

type Pair

type Pair struct {
	First, Second uint16
	V1, V2        Value
}

Pair is two glyphs a pair adjustment lookup moves when they meet.

type Script

type Script struct {
	Tag string
	// Default is the language system used for languages the script does
	// not list; nil when there is none.
	Default *LangSys
	Langs   []LangSys
}

Script is a script the table has features for.

type Subst

type Subst struct {
	// In is the glyph replaced, or the components of a ligature.
	In []uint16
	// Out is what replaces it: a glyph, a sequence (a multiple substitution;
	// empty deletes the glyph), or the glyphs an alternate substitution
	// chooses from.
	Out []uint16
}

Subst is a substitution of a GSUB lookup.

type Table

type Table struct {
	Scripts  []Script
	Features []Feature
	Lookups  []Lookup
	// FeatureVariations is the number of feature variation records: the
	// regions of a variable font's design space where other lookups apply.
	FeatureVariations int
	// contains filtered or unexported fields
}

Table is a GSUB or GPOS table.

func ParseGPOS

func ParseGPOS(b []byte) (*Table, error)

ParseGPOS reads a GPOS table.

func ParseGSUB

func ParseGSUB(b []byte) (*Table, error)

ParseGSUB reads a GSUB table.

func (*Table) Attachments

func (t *Table) Attachments(i int) []Attachment

Attachments returns the subtables of a mark to base (GPOS type 4) or mark to mark (type 6) lookup.

func (*Table) Context

func (t *Table) Context(i int) (c Context, ok bool)

Context returns the rules of a contextual lookup (GSUB types 5, 6 and 8, GPOS types 7 and 8); ok is false for other lookups. A reverse chaining lookup has a rule per subtable and applies no other lookup.

func (*Table) Covered

func (t *Table) Covered(i int) []uint16

Covered returns the glyphs lookup i applies to: those its subtables cover (for a contextual lookup, the first glyph of its rules).

func (*Table) MarkCounts

func (t *Table) MarkCounts(i int) (marks, bases int)

MarkCounts returns how many marks a mark attachment lookup (GPOS types 4, 5 and 6) attaches, and to how many glyphs; for a cursive attachment lookup (type 3), the glyphs it joins.

func (*Table) PairCount

func (t *Table) PairCount(i int) int

PairCount returns about how many pairs lookup i adjusts: the pairs of each subtable with an adjustment that is not zero, counted per subtable (a pair two subtables list counts twice). For class-based subtables it is the product of the sizes of the classes, without the class of second glyphs no class lists.

func (*Table) PairValue

func (t *Table) PairValue(i int, first, second uint16) (v1, v2 Value, ok bool)

PairValue returns how pair adjustment lookup i moves first and second when they meet: the first subtable that has the pair decides.

func (*Table) Pairs

func (t *Table) Pairs(i int, fn func(Pair) bool)

Pairs calls fn with pairs of lookup i whose adjustment is not zero, subtable by subtable, until fn returns false. Class-based subtables are expanded to every pair of their classes (without the class of second glyphs no class lists), so there may be very many.

func (*Table) ScriptFeatures

func (t *Table) ScriptFeatures() [][]string

ScriptFeatures returns, for each feature, the scripts and language systems that use it, as "script" (the script's default) or "script/LANG" in the order of the script list.

func (*Table) Singles

func (t *Table) Singles(i int, fn func(g uint16, v Value) bool)

Singles calls fn with the glyphs a single adjustment lookup (GPOS type 1) moves and how, until fn returns false.

func (*Table) Substs

func (t *Table) Substs(i int, fn func(Subst) bool)

Substs calls fn with the substitutions of GSUB lookup i (a single, multiple, alternate, ligature or reverse chaining lookup) in the order of its subtables and their coverage, until fn returns false. What an earlier subtable substitutes is not listed again from a later one, since the first subtable that applies wins; the substitutions of a reverse chaining lookup depend on their context and are all listed. The list is cut short when the table has listed all it may (see Truncated).

func (*Table) Truncated

func (t *Table) Truncated() bool

Truncated reports whether lookups were cut short: the table lists more than any font does, and what was read of it is not all it says.

func (*Table) TypeName

func (t *Table) TypeName(typ int) string

TypeName names a lookup type of the table.

type Value

type Value struct {
	XPlacement, YPlacement, XAdvance, YAdvance int
}

Value is how a positioning lookup moves a glyph, in font units.

func (Value) Zero

func (v Value) Zero() bool

Zero reports whether the value moves nothing.

Jump to

Keyboard shortcuts

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