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 (*Registry) FaceCount ¶
FaceCount returns the number of registered faces for the given family, or 0 if the family is not registered.
func (*Registry) FamilyNames ¶
FamilyNames returns a sorted list of all registered family names.
func (*Registry) RegisterFamily ¶
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 ¶
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):
- Look for an exact weight+style match.
- If the requested style is Italic and no italic face exists, fall back to Normal style.
- 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 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 )