subset

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2026 License: MIT Imports: 5 Imported by: 1

Documentation

Overview

Package subset provides font subsetting functionality.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoTables is returned when building a font with no tables.
	ErrNoTables = errors.New("subset: no tables to build")

	// ErrMissingTable is returned when a required table is missing.
	ErrMissingTable = errors.New("subset: required table missing")

	// ErrInvalidGlyph is returned for invalid glyph references.
	ErrInvalidGlyph = errors.New("subset: invalid glyph reference")
)

Functions

func Subset

func Subset(font *ot.Font, codepoints []rune) ([]byte, error)

Subset is a convenience function that subsets a font for given codepoints.

func SubsetString

func SubsetString(font *ot.Font, text string) ([]byte, error)

SubsetString is a convenience function that subsets a font for a string.

Types

type Flags

type Flags uint32

Flags controls various subsetting options.

const (
	// FlagNoHinting removes hinting instructions.
	FlagNoHinting Flags = 1 << iota

	// FlagRetainGIDs keeps original glyph IDs (pads with empty glyphs).
	FlagRetainGIDs

	// FlagGlyphNames retains PostScript glyph names.
	FlagGlyphNames

	// FlagNotdefOutline retains the .notdef glyph outline.
	FlagNotdefOutline

	// FlagNoLayoutClosure skips GSUB/GPOS glyph closure.
	FlagNoLayoutClosure

	// FlagPassUnrecognized keeps unrecognized tables.
	FlagPassUnrecognized

	// FlagDropLayoutTables excludes GSUB/GPOS/GDEF tables from output.
	// Use this for PDF embedding where shaping is already done.
	FlagDropLayoutTables
)

type FontBuilder

type FontBuilder struct {
	// contains filtered or unexported fields
}

FontBuilder builds a new font from subset tables.

func NewFontBuilder

func NewFontBuilder() *FontBuilder

NewFontBuilder creates a new FontBuilder.

func (*FontBuilder) AddTable

func (b *FontBuilder) AddTable(tag ot.Tag, data []byte)

AddTable adds or replaces a table in the font.

func (*FontBuilder) Build

func (b *FontBuilder) Build() ([]byte, error)

Build produces the final font binary.

func (*FontBuilder) HasTable

func (b *FontBuilder) HasTable(tag ot.Tag) bool

HasTable returns true if the table exists.

type Input

type Input struct {

	// Flags controls subsetting behavior.
	Flags Flags
	// contains filtered or unexported fields
}

Input configures which glyphs and tables to include in the subset.

func NewInput

func NewInput() *Input

NewInput creates a new subset input configuration.

func (*Input) AddGlyph

func (i *Input) AddGlyph(gid ot.GlyphID)

AddGlyph adds a glyph ID to retain.

func (*Input) AddGlyphs

func (i *Input) AddGlyphs(gids ...ot.GlyphID)

AddGlyphs adds multiple glyph IDs.

func (*Input) AddString

func (i *Input) AddString(s string)

AddString adds all codepoints from a string.

func (*Input) AddUnicode

func (i *Input) AddUnicode(cp rune)

AddUnicode adds a Unicode codepoint to retain.

func (*Input) AddUnicodeRange

func (i *Input) AddUnicodeRange(start, end rune)

AddUnicodeRange adds a range of Unicode codepoints [start, end].

func (*Input) AddUnicodes

func (i *Input) AddUnicodes(cps ...rune)

AddUnicodes adds multiple Unicode codepoints.

func (*Input) DropTable

func (i *Input) DropTable(tag ot.Tag)

DropTable marks a table to be excluded from output.

func (*Input) Glyphs

func (i *Input) Glyphs() map[ot.GlyphID]bool

Glyphs returns the set of glyph IDs to retain.

func (*Input) HasLayoutFeatures

func (i *Input) HasLayoutFeatures() bool

HasLayoutFeatures returns true if specific features were requested.

func (*Input) HasPinnedAxes added in v0.0.3

func (i *Input) HasPinnedAxes() bool

HasPinnedAxes returns true if any axes have been pinned.

func (*Input) IsFullyInstanced added in v0.0.3

func (i *Input) IsFullyInstanced(font *ot.Font) bool

IsFullyInstanced returns true if all axes in the font have been pinned.

func (*Input) KeepFeature

func (i *Input) KeepFeature(tag ot.Tag)

KeepFeature marks an OpenType feature to retain. If no features are specified, all features are retained.

func (*Input) PassThroughTable

func (i *Input) PassThroughTable(tag ot.Tag)

PassThroughTable marks a table to be copied unchanged.

func (*Input) PinAllAxesToDefault added in v0.0.3

func (i *Input) PinAllAxesToDefault(font *ot.Font) bool

PinAllAxesToDefault pins all variation axes to their default values. This creates a static font at the default instance. This is similar to HarfBuzz's hb_subset_input_pin_all_axes_to_default().

func (*Input) PinAxisLocation added in v0.0.3

func (i *Input) PinAxisLocation(axisTag ot.Tag, value float32)

PinAxisLocation pins a variation axis to a specific value. When all axes are pinned, the font is "instanced" to a static font. The value should be in design-space coordinates (e.g., 700 for Bold weight). This is similar to HarfBuzz's hb_subset_input_pin_axis_location().

func (*Input) PinAxisToDefault added in v0.0.3

func (i *Input) PinAxisToDefault(font *ot.Font, axisTag ot.Tag) bool

PinAxisToDefault pins a variation axis to its default value. The font's fvar table is needed to determine the default. This is similar to HarfBuzz's hb_subset_input_pin_axis_to_default().

func (*Input) PinnedAxes added in v0.0.3

func (i *Input) PinnedAxes() map[ot.Tag]float32

PinnedAxes returns the map of pinned axis tags to values.

func (*Input) ShouldDropTable

func (i *Input) ShouldDropTable(tag ot.Tag) bool

ShouldDropTable returns true if the table should be excluded.

func (*Input) ShouldKeepFeature

func (i *Input) ShouldKeepFeature(tag ot.Tag) bool

ShouldKeepFeature returns true if the feature should be retained.

func (*Input) ShouldPassThrough

func (i *Input) ShouldPassThrough(tag ot.Tag) bool

ShouldPassThrough returns true if the table should be copied unchanged.

func (*Input) Unicodes

func (i *Input) Unicodes() map[rune]bool

Unicodes returns the set of Unicode codepoints to retain.

type Plan

type Plan struct {
	// contains filtered or unexported fields
}

Plan holds the computed glyph mapping and metadata for subsetting.

func CreatePlan

func CreatePlan(font *ot.Font, input *Input) (*Plan, error)

CreatePlan creates a subset plan from a font and input configuration.

func (*Plan) CFF

func (p *Plan) CFF() *ot.CFF

CFF returns the parsed CFF table.

func (*Plan) Cmap

func (p *Plan) Cmap() *ot.Cmap

Cmap returns the parsed cmap table.

func (*Plan) Execute

func (p *Plan) Execute() ([]byte, error)

Execute performs the subsetting operation and returns the new font data.

func (*Plan) Fvar added in v0.0.3

func (p *Plan) Fvar() *ot.Fvar

Fvar returns the parsed fvar table.

func (*Plan) GetGlyphDeltas added in v0.0.3

func (p *Plan) GetGlyphDeltas(gid ot.GlyphID, numPoints int) (xDeltas, yDeltas []int16)

GetGlyphDeltas returns the gvar deltas for a glyph at the pinned coordinates. numPoints is the number of outline points in the glyph. This version doesn't support proper IUP interpolation.

func (*Plan) GetGlyphDeltasWithCoords added in v0.0.3

func (p *Plan) GetGlyphDeltasWithCoords(gid ot.GlyphID, numPoints int, origPoints []ot.SimpleGlyphPoint) (xDeltas, yDeltas []int16)

GetGlyphDeltasWithCoords returns the gvar deltas for a glyph at the pinned coordinates with proper IUP interpolation using the original point coordinates.

func (*Plan) GetInstancedAdvance added in v0.0.3

func (p *Plan) GetInstancedAdvance(oldGID ot.GlyphID) uint16

GetInstancedAdvance returns the instanced advance width for a glyph. If not instanced, returns the base advance from hmtx.

func (*Plan) Glyf

func (p *Plan) Glyf() *ot.Glyf

Glyf returns the parsed glyf table.

func (*Plan) GlyphMap

func (p *Plan) GlyphMap() map[ot.GlyphID]ot.GlyphID

GlyphMap returns the old->new glyph ID mapping.

func (*Plan) GlyphSet

func (p *Plan) GlyphSet() map[ot.GlyphID]bool

GlyphSet returns the set of old glyph IDs to retain.

func (*Plan) Hmtx

func (p *Plan) Hmtx() *ot.Hmtx

Hmtx returns the parsed hmtx table.

func (*Plan) Input

func (p *Plan) Input() *Input

Input returns the input configuration.

func (*Plan) IsInstanced added in v0.0.3

func (p *Plan) IsInstanced() bool

IsInstanced returns true if the plan will produce an instanced (static) font.

func (*Plan) MapGlyph

func (p *Plan) MapGlyph(oldGID ot.GlyphID) (ot.GlyphID, bool)

MapGlyph maps an old glyph ID to a new glyph ID. Returns (0, false) if the glyph is not in the subset.

func (*Plan) NumOutputGlyphs

func (p *Plan) NumOutputGlyphs() int

NumOutputGlyphs returns the number of glyphs in the output font.

func (*Plan) OldGlyph

func (p *Plan) OldGlyph(newGID ot.GlyphID) (ot.GlyphID, bool)

OldGlyph returns the old glyph ID for a new glyph ID.

func (*Plan) Source

func (p *Plan) Source() *ot.Font

Source returns the source font.

Jump to

Keyboard shortcuts

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