fontdb

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package fontdb finds font files, resolves the family names documents ask for to faces that are available (directly, through metric-compatible substitutes, or through generic fallbacks), measures glyphs for text layout, and builds the subset font programs that get embedded.

Index

Constants

View Source
const (
	Sans  = "sans-serif"
	Serif = "serif"
	Mono  = "monospace"
)

Generic families.

Variables

View Source
var ErrNoGlyphs = errors.New("fontdb: font has no usable glyph data")

ErrNoGlyphs is returned for faces whose glyph data cannot be read.

Functions

func Classify

func Classify(name string) string

Classify guesses the generic family of a family name.

func IsCJK

func IsCJK(r rune) bool

IsCJK reports whether r belongs to the East Asian scripts that Office lays out with the "ea" font: Han, kana, Hangul, Bopomofo, CJK symbols and punctuation, and the full-width forms.

func Normalize

func Normalize(name string) string

Normalize folds a family name for comparison: NFKC (full-width letters become ASCII), lower case, without spaces, hyphens and underscores.

func SystemDirs

func SystemDirs() []string

SystemDirs returns the usual font directories of the platform.

Types

type DB

type DB struct {
	Faces []*Face
	// contains filtered or unexported fields
}

DB is a set of faces indexed by normalized family name.

func New

func New(fsys fs.FS, dirs []string, system bool) *DB

New scans the fonts in fsys (when it is not nil), then those under dirs and, when system is true, the system directories. Scans of a directory are cached for the life of the process; fsys is scanned on every call.

func (*DB) Fallbacks

func (db *DB) Fallbacks(generic string, bold, italic bool) []Resolved

Fallbacks returns faces to try, in order, for characters the resolved face lacks: CJK chains, the generic family, symbol fonts, then every other face so that a small private font directory is used completely.

func (*DB) Family

func (db *DB) Family(name string) []*Face

Family returns the faces of a family, or nil.

func (*DB) Resolve

func (db *DB) Resolve(family string, bold, italic, cjk bool) Resolved

Resolve maps a family request to a face: the family itself, a known substitute, a generic family of the same kind (serif, sans-serif, monospace; CJK chains when cjk is set), and finally any face at all.

func (*DB) ResolveMath

func (db *DB) ResolveMath(family string) *Face

ResolveMath maps a request for a formula font to a face with a MATH table: the family itself, then the other math fonts, then any face that has one. It returns nil when no available font has a MATH table.

type Face

type Face struct {
	Path   string
	Index  int    // index in a collection
	Family string // English family name (typographic family when present)
	Style  string // English subfamily name
	Weight int    // OS/2 usWeightClass
	Italic bool
	Mono   bool
	CFF    bool
	Math   bool // has a MATH table (a font for formulas)
	// contains filtered or unexported fields
}

Face is one font in a file (a collection holds several).

func Match

func Match(faces []*Face, bold, italic bool) (best *Face, synthBold, synthItalic bool)

Match picks the face of a family closest to the requested weight and slant. synthBold / synthItalic report what the renderer has to fake.

func (*Face) HasRune

func (f *Face) HasRune(r rune) bool

HasRune checks the face's Unicode cmap without loading its glyph data. This lets fallback search skip fonts that cannot draw a character.

func (*Face) Load

func (f *Face) Load() (*Loaded, error)

Load reads and parses the face's font program (once).

type Loaded

type Loaded struct {
	Face *Face
	Font *sfnt.Font
	// Data is the whole file, mapped into memory where the platform allows
	// (contrib/otf); the font's tables point into it.
	Data []byte

	// vertical metrics in em
	Ascent, Descent       float64
	UnderlinePos, UnderTh float64
	// CapHeight is the height of capital letters in em (estimated when
	// the font does not say).
	CapHeight float64
	// contains filtered or unexported fields
}

Loaded is a face with its glyph data.

func (*Loaded) Advance

func (l *Loaded) Advance(g uint16) float64

Advance returns the advance of glyph g in em.

func (*Loaded) Bounds

func (l *Loaded) Bounds(g uint16) (r sfnt.Rect, ok bool)

Bounds returns the bounding box of glyph g's outline in em (y up); ok is false for glyphs without an outline.

func (*Loaded) CanSubset

func (l *Loaded) CanSubset(ignoreFSType bool) bool

CanSubset reports whether Program can drop unused glyphs (ignoreFSType as for Program).

func (*Loaded) Embeddable

func (l *Loaded) Embeddable() (embed, subset bool)

Embeddable reports whether the license bits allow embedding, and subsetting.

func (*Loaded) Glyph

func (l *Loaded) Glyph(r rune) (uint16, bool)

Glyph returns the glyph index for r.

func (*Loaded) Has

func (l *Loaded) Has(r rune) bool

Has reports whether the font maps r to a glyph.

func (*Loaded) Math

func (l *Loaded) Math() *sfnt.Math

Math returns the font's MATH table (read once), or nil.

func (*Loaded) Outline added in v0.1.1

func (l *Loaded) Outline(g uint16, pen sfnt.Pen) (ok bool)

Outline draws the outline of glyph g with pen, in font units (y up); ok is false when the glyph cannot be read.

func (*Loaded) Program

func (l *Loaded) Program(runes []rune, glyphs map[rune]uint16, noSubset, ignoreFSType bool) (data []byte, ok bool)

Program builds the font program to embed for the given characters: a subset holding just their glyphs, or the whole font (fonts whose license forbids subsetting or whose outlines cannot be subset, or all when noSubset is set), with a cmap for exactly those characters. glyphs maps more characters (private use ones) to glyphs that no character maps to, such as the larger variants of a math font's delimiters. The OS/2 fsType and the copyright and license strings of the font carry over. ok is false when the license forbids embedding; ignoreFSType embeds (and subsets) such fonts anyway, for callers that hold the rights to.

func (*Loaded) Size

func (l *Loaded) Size() int

Size is the size of the font file the face comes from.

func (*Loaded) UnitsPerEm

func (l *Loaded) UnitsPerEm() float64

UnitsPerEm returns the size of the em in font units.

type Resolved

type Resolved struct {
	Face        *Face // nil when no font is available at all
	SynthBold   bool
	SynthItalic bool
	Exact       bool   // the requested family itself was found
	Generic     string // generic family of the request
}

Resolved is a family request mapped to an available face.

Jump to

Keyboard shortcuts

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