font

package
v0.1.16 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package font provides a font registry for managing font families, weights, and styles in the gogpu/ui toolkit.

The registry maps family+weight+style combinations to embedded font data, enabling resolution of the best available font face for a given request. Weight resolution follows the CSS font-matching algorithm (https://www.w3.org/TR/css-fonts-4/#font-style-matching).

Usage

Create a registry and register font faces:

reg := font.NewRegistry()
reg.RegisterFamily(font.Family{
    Name: "Inter",
    Faces: []font.Face{
        {Weight: font.Regular, Style: font.Normal, Data: interRegularTTF},
        {Weight: font.Bold, Style: font.Normal, Data: interBoldTTF},
    },
})

Resolve the best matching face:

data, ok := reg.Resolve("Inter", font.Medium, font.Normal)
if ok {
    // Use font data for rendering
}

The registry is safe for concurrent use.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Face

type Face struct {
	// Weight is the font weight for this face.
	Weight Weight

	// Style is the font style for this face.
	Style Style

	// Data is the raw font file data (TTF or OTF).
	Data []byte
}

Face represents a single font variant within a family.

A face combines a specific weight and style with the raw font data (typically TTF or OTF bytes).

type Family

type Family struct {
	// Name is the family name (e.g., "Inter", "Roboto").
	Name string

	// Faces is the list of available weight+style variants.
	Faces []Face
}

Family describes a font family with all its available faces.

A family groups multiple Face entries under a single name (e.g., "Inter"). Each face represents a specific weight+style combination with the corresponding font data.

type Registry

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

Registry manages font families and resolves font face requests to font data.

The registry maps family+weight+style combinations to raw font data. When an exact match is not available, it uses the CSS font-matching algorithm to find the closest available weight within the requested style.

Registry is safe for concurrent use. It uses a read-write mutex optimized for read-heavy workloads (font resolution is far more frequent than registration).

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates an empty font registry.

func (*Registry) FaceCount

func (r *Registry) FaceCount(familyName string) int

FaceCount returns the number of registered faces for the given family, or 0 if the family is not registered.

func (*Registry) FamilyNames

func (r *Registry) FamilyNames() []string

FamilyNames returns a sorted list of all registered family names.

func (*Registry) HasFamily

func (r *Registry) HasFamily(familyName string) bool

HasFamily reports whether the given family name is registered.

func (*Registry) RegisterFamily

func (r *Registry) RegisterFamily(family Family)

RegisterFamily registers all faces of a font family.

If the family already exists, new faces are appended. If a face with the same weight and style already exists, it is replaced.

func (*Registry) Resolve

func (r *Registry) Resolve(familyName string, weight Weight, style Style) ([]byte, bool)

Resolve finds the best matching font data for the given family, weight, and style.

Resolution follows the CSS font-matching algorithm (https://www.w3.org/TR/css-fonts-4/#font-style-matching):

  1. Look for an exact weight+style match.
  2. If the requested style is Italic and no italic face exists, fall back to Normal style.
  3. Apply CSS weight resolution: for weights <= 500, try lighter first then heavier; for weights > 500, try heavier first then lighter.

Returns the font data and true if a match was found, or nil and false if the family does not exist or has no faces.

type Style

type Style uint8

Style represents the style (posture) of a font.

const (
	// Normal is the standard upright style.
	Normal Style = iota

	// Italic is the italic style with modified letterforms.
	Italic
)

func (Style) String

func (s Style) String() string

String returns the human-readable name for the font style.

type Weight

type Weight int

Weight represents the weight (boldness) of a font.

Font weights follow the CSS/OpenType standard where values range from 100 (thinnest) to 900 (heaviest). Not all fonts support all weights; the registry uses a CSS-standard matching algorithm to find the closest available weight.

const (
	// Thin is the thinnest available weight (100).
	Thin Weight = 100

	// ExtraLight is extra-light weight (200).
	ExtraLight Weight = 200

	// Light is light weight (300).
	Light Weight = 300

	// Regular is the standard weight (400).
	Regular Weight = 400

	// Medium is medium weight (500).
	Medium Weight = 500

	// SemiBold is semi-bold weight (600).
	SemiBold Weight = 600

	// Bold is bold weight (700).
	Bold Weight = 700

	// ExtraBold is extra-bold weight (800).
	ExtraBold Weight = 800

	// Black is the heaviest weight (900).
	Black Weight = 900
)

func (Weight) IsBold

func (w Weight) IsBold() bool

IsBold returns true if this weight is considered bold (>= 700).

func (Weight) IsLight

func (w Weight) IsLight() bool

IsLight returns true if this weight is considered light (<= 300).

func (Weight) String

func (w Weight) String() string

String returns the human-readable name for the font weight. For non-standard values, it returns the numeric representation.

Jump to

Keyboard shortcuts

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