font

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

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 ones the system look-up searches. It has to be called before the first look-up to have any effect.

func BidiLevels

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

BidiLevels is the embedding level of every character of one paragraph, the bidirectional algorithm of UAX #9 run end to end. base is 0 for a paragraph that runs left to right, 1 for one that runs right to left, and negative to take the direction from the text itself.

func BidiMirror

func BidiMirror(r rune) rune

BidiMirror is what rule L4 draws in place of a character that runs right to left, and the character itself where there is no other form.

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 a glyph has at index i of the standard Macintosh ordering, which is the order a TrueType font that has not rearranged its glyphs is in and what a version 1 post table means.

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 in it that does not run left to right, which is what the algorithm has anything to say about.

func NeedsShaping

func NeedsShaping(s string) bool

NeedsShaping reports text a font has to lay out itself: anything with a combining mark in it, and the scripts whose letters join, reorder or carry their vowels above and below. Everything else, Han and Kana among it, is one glyph a character and takes the shorter path.

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 what the program calls itself, which is not the name the PDF
	// font dictionary uses.
	Name string
	// Family is the family the program belongs to, which is the name a
	// stylesheet asks for a font by.
	Family string
	// Weight is the usWeightClass of the program, 400 when it declares none,
	// and Italic whether it is slanted.
	Weight int
	Italic bool
	// UnitsPerEm is the glyph space of a TrueType font; a CFF or Type1 font
	// carries a matrix instead.
	UnitsPerEm int
	// Matrix maps glyph space to text space, where one unit is the font size.
	Matrix raster.Matrix
	// Ascent and Descent are how far the em box reaches above and below the
	// baseline, in text space, and default when the program declares neither.
	Ascent, Descent float32
	// XHeight is how tall a lower case x is, and SubOffset and SuperOffset
	// how far the program puts a subscript below the baseline and a
	// superscript above it, all in text space. Each is zero when the program
	// says nothing, which is every program that is not an SFNT.
	XHeight, SubOffset, SuperOffset float32
	// CID is true for a CFF font keyed by CID rather than by name.
	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, nil when it has none. It is what a document in a script the base fourteen cannot draw needs.

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 a face the machine has for a family, and nil when it has none. A family with several weights and slants gives the nearest.

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. Type1 fonts always have one and CFF fonts usually do.

func (*Font) DebugLayout

func (f *Font) DebugLayout() string

DebugLayout reports what the layout tables of a font hold, for a comparison against another shaper.

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, which addresses the private use area with and without the F0 prefix.

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. The path is owned by the font and must not be modified.

A CID keyed CFF is addressed by CID rather than by glyph index, the convention FreeType uses; the charset maps it to the glyph inside.

func (*Font) HasCmap

func (f *Font) HasCmap() bool

HasCmap reports whether the program carries a character map at all. One that does not is addressed by glyph index.

func (*Font) HasGlyphNames

func (f *Font) HasGlyphNames() bool

HasGlyphNames reports whether the program names its glyphs, which decides whether an encoding can be resolved through them.

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 text into the glyphs that draw it, through the OpenType layout tables the font carries. The run is one script and one direction, and the glyphs come back in the order they are drawn.

func (*Font) Shaped

func (f *Font) Shaped() bool

Shaped reports a font that carries the tables shaping reads. One that does not is drawn from its character map alone.

type Glyph

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

Glyph is one glyph of a shaped run: the glyph, the character it came from, how far the pen moves after it and how far it is drawn from the pen. The last four are 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