font

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: Apache-2.0 Imports: 17 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package font reads font programs and returns glyph outlines and metrics. It knows nothing about PDF: a font is bytes in, outlines out.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalid means the font program is damaged past use.
	ErrInvalid = errors.New("font: invalid font")
	// ErrUnsupported means the format is known but not handled.
	ErrUnsupported = errors.New("font: unsupported font")
)

Errors returned by this package.

Functions

func AddFontDir

func AddFontDir(dir string)

AddFontDir adds a directory to the system font search. It must be called before the first look-up.

func BidiLevels

func BidiLevels(text []rune, base int) []byte

BidiLevels is the UAX #9 embedding level of every character of one paragraph. base is 0 for left to right, 1 for right to left, negative to take it from the text.

func BidiMirror

func BidiMirror(r rune) rune

BidiMirror is the mirrored form rule L4 draws for r, or r when it has none.

func BidiOrder

func BidiOrder(levels []byte) []int

BidiOrder is the order a line of the given levels draws in, rule L2.

func Encoding

func Encoding(name string) *[256]string

Encoding returns one of the predefined simple font encodings, or nil.

func IndexForName

func IndexForName(name string) int

IndexForName reads the gXX, GXX, glyphXX and cidXX conventions, which name a glyph by its number rather than by what it looks like.

func MacGlyphName

func MacGlyphName(i int) string

MacGlyphName is the name of the glyph at index i of the standard Macintosh ordering.

func MacRomanCode

func MacRomanCode(r rune) int

MacRomanCode returns the MacRoman code of a character, or -1.

func NeedsBidi

func NeedsBidi(s string) bool

NeedsBidi reports text with a character that does not run left to right.

func NeedsShaping

func NeedsShaping(s string) bool

NeedsShaping reports text a font has to lay out itself.

func RuneForName

func RuneForName(name string) rune

RuneForName resolves a glyph name to a character through the Adobe Glyph List and the conventions that stand in for it: uniXXXX, uXXXX, a suffix after a dot, and a single character name.

func StandardEncoding

func StandardEncoding() *[256]string

StandardEncoding is the encoding a simple font falls back to.

func StandardName

func StandardName(name string, serif, fixed, symbolic, bold, italic bool) string

StandardName maps a font name onto one of the fourteen, using the flags of the font descriptor to choose when the name says nothing. It returns "" for a name that cannot be resolved, which is never: Helvetica is last.

func StandardWidth

func StandardWidth(font, glyph string) float64

StandardWidth returns the advance a base-14 font gives a glyph name, in units of a thousandth of an em, or -1.

func Upright

func Upright(r rune) bool

Upright reports a character drawn as it is written in vertical text rather than turned a quarter with the line, UAX #50.

Types

type Font

type Font struct {
	Kind Kind
	// Name is the name the program gives itself.
	Name string
	// Family is the family the program belongs to.
	Family string
	// Weight is the usWeightClass, 400 by default, and Italic whether it is slanted.
	Weight int
	Italic bool
	// UnitsPerEm is the glyph space of a TrueType font.
	UnitsPerEm int
	// Matrix maps glyph space to text space, where one unit is the font size.
	Matrix raster.Matrix
	// Ascent and Descent are the em box extents above and below the baseline, in text space.
	Ascent, Descent float32
	// XHeight, SubOffset and SuperOffset are SFNT metrics in text space, zero when absent.
	XHeight, SubOffset, SuperOffset float32
	// CID is true for a CID keyed CFF font.
	CID bool
	// contains filtered or unexported fields
}

Font is a font program.

func Fallback

func Fallback(r rune, bold, italic bool) *Font

Fallback returns a face the machine has that can draw r, or nil.

func Parse

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

Parse reads a font program, sniffing the format from its first bytes.

func Standard

func Standard(name string) *Font

Standard returns one of the fourteen fonts by its canonical name, or nil. One Font is kept per name and shared by every document that asks for it, which is what makes the base fourteen cost their glyph outlines once for the life of the process rather than once a document.

func SystemFont

func SystemFont(family string, bold, italic bool) *Font

SystemFont returns the face nearest a weight and slant the machine has for a family, or nil.

func (*Font) Advance

func (f *Font) Advance(gid int) float32

Advance returns the horizontal advance of a glyph in glyph space.

func (*Font) BuiltinEncoding

func (f *Font) BuiltinEncoding() *[256]string

BuiltinEncoding returns the code to glyph name table the program carries, or nil.

func (*Font) DebugLayout

func (f *Font) DebugLayout() string

DebugLayout reports what the layout tables of a font hold.

func (*Font) GIDForMacCode

func (f *Font) GIDForMacCode(code uint32) int

GIDForMacCode looks a byte code up in a (1,0) Mac Roman character map.

func (*Font) GIDForName

func (f *Font) GIDForName(name string) int

GIDForName returns the glyph with a name, or -1.

func (*Font) GIDForRune

func (f *Font) GIDForRune(r rune) int

GIDForRune looks a Unicode value up in the font's character map, or -1.

func (*Font) GIDForSymbolCode

func (f *Font) GIDForSymbolCode(code uint32) int

GIDForSymbolCode looks a byte code up in a (3,0) symbol character map.

func (*Font) GlyphName

func (f *Font) GlyphName(gid int) string

GlyphName is the name a glyph has in the program, "" when it has none.

func (*Font) GlyphPath

func (f *Font) GlyphPath(gid int) *raster.Path

GlyphPath returns the outline of a glyph in glyph space, owned by the font. A CID keyed CFF is addressed by CID.

func (*Font) HasCmap

func (f *Font) HasCmap() bool

HasCmap reports whether the program carries a character map.

func (*Font) HasGlyphNames

func (f *Font) HasGlyphNames() bool

HasGlyphNames reports whether the program names its glyphs.

func (*Font) NumGlyphs

func (f *Font) NumGlyphs() int

NumGlyphs returns how many glyphs the program has.

func (*Font) Shape

func (f *Font) Shape(text []rune, rtl bool) []Glyph

Shape turns a run of one script and one direction into the glyphs that draw it, in drawing order.

func (*Font) Shaped

func (f *Font) Shaped() bool

Shaped reports a font that carries the tables shaping reads.

type Glyph

type Glyph struct {
	GID                int
	Cluster            int
	XAdvance, YAdvance int
	XOffset, YOffset   int
}

Glyph is one glyph of a shaped run, with its cluster, and its advance and offset in font units.

type Kind

type Kind int

Kind is the format a font program is in.

const (
	KindTrueType Kind = iota
	KindCFF
	KindType1
)

Font program formats.

Jump to

Keyboard shortcuts

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