fonts

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: May 24, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package fonts provides a cross-platform, pure-Go typography subsystem.

This package handles OS-level font discovery, logical SFNT metadata extraction, query matching, regressional regression caching, and font fallback routines. It is intended as a low-level registry for downstream plotting mechanics.

Index

Constants

This section is empty.

Variables

View Source
var ErrFontNotFound = errors.New("font not found")

ErrFontNotFound is returned when no suitable font or fallback can be matched.

View Source
var ErrInvalidFontData = errors.New("invalid font data")

ErrInvalidFontData is returned when a font file cannot be parsed.

Functions

func DefaultDirs

func DefaultDirs() []string

DefaultDirs provides the universal logical starting OS-specific standard hierarchies.

Types

type Extents

type Extents struct {
	Width      float64
	Height     float64
	Ascent     float64
	Descent    float64
	LineHeight float64
}

Extents describes the geometric bounding layout for a specific rendered string returning drawing metrics mappings generically.

type FaceCache

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

FaceCache specifically memoizes concrete renderer facing bounds ensuring concurrent drawing maps over exact DPI/Size bounds.

func (*FaceCache) Get

func (c *FaceCache) Get(r FaceRequest) (*FaceHandle, bool)

Get handles sizing array lookups executing native thread mapping.

func (*FaceCache) Set

func (c *FaceCache) Set(r FaceRequest, f *FaceHandle)

Set locks mapping array metrics executing size bounds.

type FaceHandle

type FaceHandle struct {
	Font *Font
	Size float64
	DPI  float64
	// contains filtered or unexported fields
}

FaceHandle acts as the renderer-agnostic mapping pointer exposing pure file physical physics bounded to mapped exact sizes.

func (*FaceHandle) FontSource added in v0.0.5

func (h *FaceHandle) FontSource() *text.FontSource

FontSource returns the underlying text.FontSource for this handle, enabling callers to create faces with custom options (e.g., OpenType features). Returns nil if the font cannot be loaded.

func (*FaceHandle) MeasureExtents

func (h *FaceHandle) MeasureExtents(s string) (Extents, error)

MeasureExtents calculates exact plotting dimensions converting abstract handles onto scaled layout math algorithms.

func (*FaceHandle) TextFace

func (h *FaceHandle) TextFace() text.Face

TextFace returns the mapped graphics explicit bounding context allowing external Ebiten / GG layers structural drawing commands globally mapping.

type FaceRequest

type FaceRequest struct {
	Family          string
	Weight          Weight
	Style           Style
	PreferMonospace bool
	AllowFallback   bool

	Size float64 // Represents requested structural point sizing.
	DPI  float64 // Scales the mapped size metrics mapping precisely.
}

FaceRequest defines sized layout dependencies external graphing engines pass directly matching native configurations.

type FallbackConfig

type FallbackConfig struct {
	// Aliases maps specific query families to nearest approximations BEFORE cascading generic overrides.
	Aliases map[string][]string

	// Core platform agnostic fallback trees mapping general generic categories arrays.
	SansSerif []string
	Serif     []string
	Monospace []string
	Emoji     []string
}

FallbackConfig defines explicit string map overrides generating strict deterministic cascade trees.

func DefaultFallbackConfig

func DefaultFallbackConfig() FallbackConfig

DefaultFallbackConfig returns sane universal fallbacks spanning Linux, Windows, and macOS mappings.

type Font

type Font struct {
	Path        string
	Index       int
	Family      string
	Subfamily   string
	FullName    string
	Weight      Weight
	Style       Style
	Stretch     string // "condensed", "expanded", "normal"
	IsMonospace bool
	IsSymbol    bool
}

Font represents a resolvable physical font file face location.

func DiscoverFonts

func DiscoverFonts(dirs []string) ([]Font, error)

DiscoverFonts iteratively paths recursively searching for supported binary format schemas parsing.

type FontCache

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

FontCache provides thread-safe heuristic memoization for resolved system fonts.

func (*FontCache) Get

func (c *FontCache) Get(q Query) (*Font, bool)

Get retrieves a resolved font from the cache map mapping across RLock read barriers.

func (*FontCache) Set

func (c *FontCache) Set(q Query, f *Font)

Set stores a newly evaluated pointer locking globally executing exact write limits.

type Query

type Query struct {
	Family          string
	Weight          Weight
	Style           Style
	PreferMonospace bool
	AllowFallback   bool
}

Query defines the desired typographic traits requested.

type Registry

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

Registry indexes system fonts discovered by scanning standard OS font directories. Each entry records the font's file path, family name, weight, and style.

func NewRegistry

func NewRegistry() (*Registry, error)

NewRegistry discovers and indexes all fonts in the standard OS font directories. On Linux this scans /usr/share/fonts; on macOS /Library/Fonts; on Windows C:\Windows\Fonts.

func (*Registry) Fonts

func (r *Registry) Fonts() []Font

Fonts returns all discovered fonts in the registry.

func (*Registry) Match

func (r *Registry) Match(q Query) *Font

Match finds the best font in the registry for the given family, weight, and style, using a scoring heuristic that prefers exact matches over partial ones.

type Resolver

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

Resolver maps font requests to loaded font faces, applying a CSS-like cascade: exact match → family alias → weight fallback → system default.

func NewResolver

func NewResolver(registry *Registry, config FallbackConfig) *Resolver

NewResolver creates a Resolver backed by the given font registry and fallback configuration.

func (*Resolver) LoadFace

func (r *Resolver) LoadFace(req FaceRequest) (*FaceHandle, error)

LoadFace returns a font.Face for the given family, size, weight, and style. Results are cached; concurrent calls for the same parameters share one Face.

func (*Resolver) Resolve

func (r *Resolver) Resolve(q Query) *Font

Resolve finds the best font for the given query, using the cached result if available.

type SourceCache

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

SourceCache specifically memoizes parsed physical bytes mapping resolving massive TTF files.

func (*SourceCache) Get

func (c *SourceCache) Get(path string) (*text.FontSource, bool)

Get handles string lookup loads ensuring physical limits match threaded arrays.

func (*SourceCache) Set

func (c *SourceCache) Set(path string, f *text.FontSource)

Set commits caching heavy physical file blocks executing directly over map arrays.

type Style

type Style int

Style represents the slant or italicization.

const (
	StyleNormal Style = iota
	StyleItalic
	StyleOblique
)

StyleNormal represents an upright font style.

type Weight

type Weight int

Weight represents the stroke thickness of a physical font face.

const (
	WeightThin       Weight = 100
	WeightExtraLight Weight = 200
	WeightLight      Weight = 300
	WeightNormal     Weight = 400
	WeightMedium     Weight = 500
	WeightSemiBold   Weight = 600
	WeightBold       Weight = 700
	WeightExtraBold  Weight = 800
	WeightBlack      Weight = 900
)

WeightThin is the thinnest font weight.

Jump to

Keyboard shortcuts

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