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
- Variables
- func FeatureDefault(tag string) int
- func FeatureName(tag string) string
- func LangName(tag string) string
- func ScriptName(tag string) string
- type Anchor
- type Attachment
- type Context
- type Feature
- type GDEF
- type LangSys
- type Lookup
- type MarkAnchor
- type Pair
- type Script
- type Subst
- type Table
- func (t *Table) Attachments(i int) []Attachment
- func (t *Table) Context(i int) (c Context, ok bool)
- func (t *Table) Covered(i int) []uint16
- func (t *Table) MarkCounts(i int) (marks, bases int)
- func (t *Table) PairCount(i int) int
- func (t *Table) PairValue(i int, first, second uint16) (v1, v2 Value, ok bool)
- func (t *Table) Pairs(i int, fn func(Pair) bool)
- func (t *Table) ScriptFeatures() [][]string
- func (t *Table) Singles(i int, fn func(g uint16, v Value) bool)
- func (t *Table) Substs(i int, fn func(Subst) bool)
- func (t *Table) Truncated() bool
- func (t *Table) TypeName(typ int) string
- type Value
Constants ¶
const ( GlyphBase = 1 GlyphLigature = 2 GlyphMark = 3 GlyphComponent = 4 )
Glyph classes of the GDEF table.
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).
const ( RightToLeft = 0x0001 IgnoreBaseGlyphs = 0x0002 IgnoreLigatures = 0x0004 IgnoreMarks = 0x0008 UseMarkFilteringSet = 0x0010 MarkAttachmentType = 0xFF00 )
Lookup flags.
const ( SubstSingle = 1 SubstMultiple = 2 SubstAlternate = 3 SubstLigature = 4 SubstContext = 5 SubstChainContext = 6 SubstReverseChain = 8 )
GSUB lookup types.
const ( PosSingle = 1 PosPair = 2 PosCursive = 3 PosMarkToBase = 4 PosMarkToLig = 5 PosMarkToMark = 6 PosContext = 7 PosChainContext = 8 )
GPOS lookup types.
Variables ¶
var ErrNotLayout = errors.New("otlayout: not a layout table")
ErrNotLayout is returned for data that is not a GSUB or GPOS table.
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 ¶
FeatureDefault tells when browsers apply a feature.
func FeatureName ¶
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 ¶
LangName returns the name of a language system tag ("Japanese" for "JAN"), or "" for tags it does not know.
func ScriptName ¶
ScriptName returns the name of a script tag ("Latin" for "latn"), or "" for tags it does not know.
Types ¶
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.
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 ¶
MarkAnchor is the class of a mark and its anchor.
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 (*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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Singles calls fn with the glyphs a single adjustment lookup (GPOS type 1) moves and how, until fn returns false.
func (*Table) Substs ¶
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).