fontstore

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package fontstore registers and resolves fonts and measures text. It parses embedded fonts with go-text/typesetting and falls back to the 14 standard PDF fonts (via the afm package) when an unregistered standard family is requested.

Measurement here uses nominal glyph advances (no shaping); OpenType shaping with kerning and ligatures arrives with the text engine (textkit).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StandardBaseFont

func StandardBaseFont(family string, weight int, style Style) (string, bool)

StandardBaseFont maps a family name plus weight/style to one of the 14 standard PDF base fonts, or reports false if the family is not a standard one.

Types

type Descriptor

type Descriptor struct {
	Ascent, Descent, CapHeight float64
	BBox                       [4]float64 // xMin, yMin, xMax, yMax
	ItalicAngle                float64
	Flags                      int
	StemV                      float64
}

Descriptor holds the metrics a PDF FontDescriptor needs for an embedded font, in 1000-unit text space (font units scaled by 1000/upem).

type Face

type Face struct {
	Family string
	Weight int
	Style  Style
	// contains filtered or unexported fields
}

Face is one registered font face.

A Face is shared across concurrent renders via the template asset cache, but its parsed go-text/typesetting Face is not safe for concurrent use: glyph lookups lazily populate internal caches. mu serializes every access to parsed, and the render-invariant Descriptor is memoized so the hot path never touches parsed after warmup.

func (*Face) Data

func (f *Face) Data() []byte

Data returns the original font bytes (for embedding).

func (*Face) Descriptor

func (f *Face) Descriptor() Descriptor

Descriptor returns the FontDescriptor metrics for embedding this face. The result is render-invariant, so it is computed once and memoized; concurrent callers share the cached value and never race on the underlying go-text Face.

func (*Face) EmbeddedFont

func (f *Face) EmbeddedFont() *pdf.EmbeddedFont

EmbeddedFont returns the pdf.EmbeddedFont for this face (program bytes + descriptor + WinAnsi widths), ready to embed as a simple TrueType PDF font. The value is built once and cached: returning the same pointer on every call lets the PDF writer dedupe the font program to a single FontFile2 stream no matter how many text boxes use the face.

func (*Face) FontName

func (f *Face) FontName() string

FontName returns a PDF-safe /BaseFont name derived from the family and weight/style. Embedders may prefix a subset tag (e.g. "ABCDEF+").

func (*Face) StringWidth

func (f *Face) StringWidth(text string, size float64) float64

StringWidth measures text using nominal glyph advances. It is safe for concurrent use: access to the shared go-text Face is serialized by f.mu.

func (*Face) WinAnsiWidths

func (f *Face) WinAnsiWidths() []int

WinAnsiWidths returns advance widths in 1000-unit space for WinAnsi codes 0..255 (0 for unmapped codes), for the PDF simple-font /Widths array. It is safe for concurrent use: access to the shared go-text Face is serialized by f.mu.

type FaceSpec

type FaceSpec struct {
	Data   []byte
	Weight int // 0 ⇒ 400
	Style  Style
}

FaceSpec describes a face to register.

type Font

type Font interface {
	// StringWidth returns the advance width of text in points at the given size.
	StringWidth(text string, size float64) float64
}

Font is anything that can measure text; both a registered Face and a standard font's metrics satisfy it.

type Store

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

Store is a concurrency-safe font registry.

func New

func New() *Store

New returns an empty Store.

func (*Store) Register

func (s *Store) Register(family string, specs ...FaceSpec) error

Register parses and registers one or more faces for a family.

func (*Store) Resolve

func (s *Store) Resolve(family string, weight int, style Style) (Font, bool)

Resolve returns a Font for the requested family/weight/style. Registered families are checked first; otherwise a standard PDF font matching the family name is used. weight 0 is treated as 400.

func (*Store) ResolveFace

func (s *Store) ResolveFace(family string, weight int, style Style) (*Face, bool)

ResolveFace returns a registered face for family/weight/style, or false if the family has no registered faces. Unlike Resolve it never falls back to a standard PDF font — the caller handles standard fonts separately (they don't need embedding). weight 0 is treated as 400.

type Style

type Style int

Style is a font style.

const (
	StyleNormal Style = iota
	StyleItalic
)

func ParseStyle

func ParseStyle(s string) Style

ParseStyle maps a CSS font-style string to a Style ("oblique" folds to italic).

Jump to

Keyboard shortcuts

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