font

package
v0.1.93 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Aliases for standard weights
	FontWeightThin       = FontWeightW100
	FontWeightExtraLight = FontWeightW200
	FontWeightLight      = FontWeightW300
	FontWeightNormal     = FontWeightW400
	FontWeightMedium     = FontWeightW500
	FontWeightSemiBold   = FontWeightW600
	FontWeightBold       = FontWeightW700
	FontWeightExtraBold  = FontWeightW800
	FontWeightBlack      = FontWeightW900
)
View Source
const MaximumAsyncTimeoutMillis = 15_000

MaximumAsyncTimeoutMillis is the global timeout for fetching an async font. After this timeout, a font load may no longer trigger text reflow.

Variables

View Source
var (
	FontSynthesisUnspecified = &FontSynthesis{value: -1}
	// FontSynthesisNone turns off font synthesis.
	// Neither bold nor slanted faces are synthesized.
	FontSynthesisNone = &FontSynthesis{value: 0}

	// FontSynthesisWeight synthesizes only bold font if not available.
	// Slanted fonts will not be synthesized.
	FontSynthesisWeight = &FontSynthesis{value: synthesisWeightFlag}

	// FontSynthesisStyle synthesizes only slanted font if not available.
	// Bold fonts will not be synthesized.
	FontSynthesisStyle = &FontSynthesis{value: synthesisStyleFlag}

	// FontSynthesisAll synthesizes both bold and slanted fonts if either
	// is not available in the FontFamily.
	FontSynthesisAll = &FontSynthesis{value: synthesisAllFlags}
)

Functions

func EqualFontFamily

func EqualFontFamily(a, b FontFamily) bool

func EqualFontSynthesis

func EqualFontSynthesis(a, b *FontSynthesis) bool

func IsFontSynthesis

func IsFontSynthesis(f *FontSynthesis) bool

func IsSpecifiedFontFamily

func IsSpecifiedFontFamily(f FontFamily) bool

func ResolveGioTypeface

func ResolveGioTypeface(f FontFamily) string

ResolveGioTypeface resolves a FontFamily to a gio Typeface string.

func SameFontSynthesis

func SameFontSynthesis(a, b *FontSynthesis) bool

Identity (2 ns)

func SemanticEqualFontSynthesis

func SemanticEqualFontSynthesis(a, b *FontSynthesis) bool

Semantic equality (field-by-field, 20 ns)

func StringFont

func StringFont(f Font) string

func StringFontFamily

func StringFontFamily(f FontFamily) string

func StringFontSynthesis

func StringFontSynthesis(f *FontSynthesis) string

func ToGioFont

func ToGioFont(f FontFamily, w FontWeight, s FontStyle) giofont.Font

ToGioFont converts go-compose font attributes to a gio font.Font.

func ToGioStyle

func ToGioStyle(s FontStyle) giofont.Style

ToGioStyle converts a go-compose FontStyle to a gio font.Style.

func ToGioWeight

func ToGioWeight(w FontWeight) giofont.Weight

ToGioWeight converts a go-compose FontWeight to a gio font.Weight. Gio weights are offset from 0 (Normal/400). 100 -> -300 (Thin) 400 -> 0 (Normal) 700 -> 300 (Bold)

Types

type CacheManager

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

CacheManager handles local caching of font files.

func NewCacheManager

func NewCacheManager(cacheDir string) (*CacheManager, error)

NewCacheManager creates a new CacheManager. It ensures the cache directory exists.

func (*CacheManager) Get

func (c *CacheManager) Get(url string) (string, bool)

Get returns the path to the cached file if it exists.

func (*CacheManager) Put

func (c *CacheManager) Put(url string, data []byte) (string, error)

Put saves the data to the cache and returns the path.

type DefaultFontFamily

type DefaultFontFamily struct{}

DefaultFontFamily is the platform default font family.

func (*DefaultFontFamily) Equals

func (d *DefaultFontFamily) Equals(other FontFamily) bool

func (*DefaultFontFamily) String

func (d *DefaultFontFamily) String() string

String returns the font family display name.

type DefaultFontFamilyResolver

type DefaultFontFamilyResolver struct{}

DefaultFontFamilyResolver is a basic FontFamilyResolver implementation that provides system-default font handling. It does not do any actual font resolution but provides a no-op implementation suitable for basic use cases.

func NewDefaultFontFamilyResolver

func NewDefaultFontFamilyResolver() *DefaultFontFamilyResolver

NewDefaultFontFamilyResolver creates a new DefaultFontFamilyResolver.

func (*DefaultFontFamilyResolver) Preload

func (r *DefaultFontFamilyResolver) Preload(fontFamily FontFamily)

Preload is a no-op in the default resolver.

func (*DefaultFontFamilyResolver) Resolve

func (r *DefaultFontFamilyResolver) Resolve(
	fontFamily FontFamily,
	fontWeight FontWeight,
	fontStyle FontStyle,
	fontSynthesis FontSynthesis,
) state.Value

Resolve returns a state.Value containing the resolved typeface. The default resolver returns a static value representing the default system font.

type FileBasedFontFamily

type FileBasedFontFamily interface {
	FontFamily
	// contains filtered or unexported methods
}

FileBasedFontFamily is a base type for FontFamilies created from file sources.

type Font

type Font interface {
	// Weight returns the weight of the font.
	// The system uses this to match a font to a font request.
	Weight() FontWeight

	// Style returns the style of the font, normal or italic.
	// The system uses this to match a font to a font request.
	Style() FontStyle

	// LoadingStrategy returns the loading strategy for this font.
	LoadingStrategy() FontLoadingStrategy
}

Font is the interface of the font resource.

type FontFamily

type FontFamily interface {
	Equals(other FontFamily) bool
	// contains filtered or unexported methods
}

FontFamily is the primary typography interface for Compose applications.

var (
	// FontFamilyDefault is the platform default font.
	FontFamilyDefault FontFamily = &DefaultFontFamily{}

	// FontFamilySansSerif is a font family with low contrast and plain stroke endings.
	FontFamilySansSerif FontFamily = NewGenericFontFamily("sans-serif", "FontFamily.SansSerif")

	// FontFamilySerif is the formal text style for scripts.
	FontFamilySerif FontFamily = NewGenericFontFamily("serif", "FontFamily.Serif")

	// FontFamilyMonospace is a font family where glyphs have the same fixed width.
	FontFamilyMonospace FontFamily = NewGenericFontFamily("monospace", "FontFamily.Monospace")

	// FontFamilyCursive is a cursive, hand-written like font family.
	FontFamilyCursive FontFamily = NewGenericFontFamily("cursive", "FontFamily.Cursive")
)

Standard font family constants

func CoalesceFontFamily

func CoalesceFontFamily(ptr, def FontFamily) FontFamily

func FontFamilyFromFonts

func FontFamilyFromFonts(fonts ...Font) FontFamily

FontFamilyFromFonts creates a FontFamily from a list of fonts.

func FontFamilyFromTypeface

func FontFamilyFromTypeface(typeface Typeface) FontFamily

FontFamilyFromTypeface creates a FontFamily from a loaded typeface.

func TakeOrElseFontFamily

func TakeOrElseFontFamily(a, b FontFamily) FontFamily

func ToFontFamily

func ToFontFamily(f Font) FontFamily

ToFontFamily creates a FontFamily from a single Font.

type FontFamilyResolver

type FontFamilyResolver interface {
	// Preload resolves and caches all fonts reachable in a FontFamily.
	Preload(fontFamily FontFamily)

	// Resolve resolves a typeface using any appropriate logic for the FontFamily.
	// Returns a state.Value that contains the platform-specific Typeface.
	Resolve(
		fontFamily FontFamily,
		fontWeight FontWeight,
		fontStyle FontStyle,
		fontSynthesis FontSynthesis,
	) state.Value
}

FontFamilyResolver is the main interface for resolving FontFamily into a platform-specific typeface.

type FontListFontFamily

type FontListFontFamily struct {
	// Fonts is the fallback list of fonts used for resolving typefaces.
	Fonts []Font
}

FontListFontFamily defines a font family with a list of Fonts.

func NewFontListFontFamily

func NewFontListFontFamily(fonts []Font) *FontListFontFamily

NewFontListFontFamily creates a FontListFontFamily from a list of fonts. Panics if the fonts list is empty.

func (*FontListFontFamily) Equals

func (f *FontListFontFamily) Equals(other FontFamily) bool

func (*FontListFontFamily) String

func (f *FontListFontFamily) String() string

String returns a string representation of the FontListFontFamily.

type FontLoader

type FontLoader interface {
	// Load loads the font.
	// context can be used for cancellation of async loads.
	Load(ctx context.Context, font Font) (Typeface, error)
}

FontLoader loads a Font and returns a Typeface.

type FontLoadingStrategy

type FontLoadingStrategy int

FontLoadingStrategy controls how font loading resolves when displaying text.

const (
	// FontLoadingStrategyBlocking resolves the font by blocking until loaded.
	// This means the first frame that uses this font will always display using
	// the desired font, and text will never reflow.
	// Should be used for fonts stored on-device.
	FontLoadingStrategyBlocking FontLoadingStrategy = 0

	// FontLoadingStrategyOptionalLocal is best-effort loading from a local resource
	// that MAY be available. Resolution is expected to fail when the resource is
	// not available, which will fallback to the next font in the FontFamily.
	FontLoadingStrategyOptionalLocal FontLoadingStrategy = 1

	// FontLoadingStrategyAsync loads the font in the background without blocking.
	// During loading, the app will use a fallback font. When the font finishes
	// loading, text will reflow with the resolved typeface.
	// Should be used for fonts fetched from a remote source.
	FontLoadingStrategyAsync FontLoadingStrategy = 2
)

func (FontLoadingStrategy) String

func (s FontLoadingStrategy) String() string

String returns a string representation of the FontLoadingStrategy.

func (FontLoadingStrategy) Value

func (s FontLoadingStrategy) Value() int

Value returns the underlying integer value.

type FontMatcher

type FontMatcher struct{}

FontMatcher matches fonts from a FontFamily to a weight/style request. It applies the rules at CSS 4 Font Matching: https://www.w3.org/TR/css-fonts-4/#font-style-matching

func NewFontMatcher

func NewFontMatcher() *FontMatcher

NewFontMatcher creates a new FontMatcher.

func (*FontMatcher) MatchFont

func (m *FontMatcher) MatchFont(fonts []Font, weight FontWeight, style FontStyle) []Font

MatchFont returns fonts matching the requested weight and style. If there is not a font that exactly satisfies the given constraints, the best match will be returned following CSS 4 Font Matching rules. Returns an empty slice if no fonts match.

func (*FontMatcher) MatchFontFromFamily

func (m *FontMatcher) MatchFontFromFamily(family *FontListFontFamily, weight FontWeight, style FontStyle) []Font

MatchFontFromFamily matches fonts from a FontListFontFamily.

type FontStyle

type FontStyle int

FontStyle defines whether the font is Italic or Normal.

const (
	FontStyleUnspecified = -1
	// FontStyleNormal uses the upright glyphs
	FontStyleNormal FontStyle = 0
	// FontStyleItalic uses glyphs designed for slanting
	FontStyleItalic FontStyle = 1
)

func FontStyleValues

func FontStyleValues() []FontStyle

FontStyleValues returns a list of possible FontStyle values.

func (FontStyle) Equals

func (s FontStyle) Equals(other FontStyle) bool

Equals checks if two FontStyles are equal.

func (FontStyle) IsSpecified

func (s FontStyle) IsSpecified() bool

func (FontStyle) String

func (s FontStyle) String() string

String returns a string representation of the FontStyle.

func (FontStyle) TakeOrElse

func (s FontStyle) TakeOrElse(block FontStyle) FontStyle

func (FontStyle) Value

func (s FontStyle) Value() int

Value returns the underlying integer value.

type FontSynthesis

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

FontSynthesis specifies whether the system should fake bold or slanted glyphs when the FontFamily used does not contain bold or oblique Fonts.

func CoalesceFontSynthesis

func CoalesceFontSynthesis(ptr, def *FontSynthesis) *FontSynthesis

func FontSynthesisValueOf

func FontSynthesisValueOf(value int) (*FontSynthesis, error)

FontSynthesisValueOf creates a FontSynthesis from an integer value. Returns an error if the value is not recognized.

func TakeOrElseFontSynthesis

func TakeOrElseFontSynthesis(f, defaultFontSynthesis *FontSynthesis) *FontSynthesis

func (FontSynthesis) IsStyleOn

func (f FontSynthesis) IsStyleOn() bool

IsStyleOn returns true if style synthesis is enabled.

func (FontSynthesis) IsWeightOn

func (f FontSynthesis) IsWeightOn() bool

IsWeightOn returns true if weight synthesis is enabled.

func (FontSynthesis) Value

func (f FontSynthesis) Value() int

Value returns the underlying integer value.

type FontVariationSetting

type FontVariationSetting interface {
	// AxisName returns the font variation axis name (e.g., "wght", "ital").
	AxisName() string

	// ToVariationValue converts the setting to a final value for use as a font variation setting.
	// The fontScale parameter is used for settings that need density (like optical sizing).
	ToVariationValue(fontScale float32) float32

	// NeedsDensity returns true if this setting requires density to resolve.
	NeedsDensity() bool
}

FontVariationSetting represents a single point in a font variation axis.

func FontVariationGrade

func FontVariationGrade(value int) FontVariationSetting

FontVariationGrade creates a grade setting ('GRAD'). Value must be in range [-1000, 1000].

func FontVariationItalic

func FontVariationItalic(value float32) FontVariationSetting

FontVariationItalic creates an italic setting ('ital'). Value should be in range [0.0, 1.0] where 0.0 is upright and 1.0 is italic.

func FontVariationOpticalSizing

func FontVariationOpticalSizing(valueSp float32) FontVariationSetting

FontVariationOpticalSizing creates an optical sizing setting ('opsz'). Value is the font size in sp (scaled pixels).

func FontVariationSlant

func FontVariationSlant(value float32) FontVariationSetting

FontVariationSlant creates a slant setting ('slnt'). Value is an angle in range [-90, 90] where 0 is upright.

func FontVariationWeight

func FontVariationWeight(value int) FontVariationSetting

FontVariationWeight creates a weight setting ('wght'). Value must be in range [1, 1000].

func FontVariationWidth

func FontVariationWidth(value float32) FontVariationSetting

FontVariationWidth creates a width setting ('wdth'). Value must be > 0.0.

func NewFontVariationSetting

func NewFontVariationSetting(name string, value float32) FontVariationSetting

NewFontVariationSetting creates a font variation setting for any axis. The name must be exactly 4 characters.

type FontVariationSettings

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

FontVariationSettings is a collection of settings to apply to a single font. Settings must be unique on AxisName.

func FontVariationSettingsEmpty

func FontVariationSettingsEmpty() *FontVariationSettings

FontVariationSettingsEmpty returns an empty FontVariationSettings.

func FontVariationSettingsFromWeightStyle

func FontVariationSettingsFromWeightStyle(weight FontWeight, style FontStyle, additional ...FontVariationSetting) *FontVariationSettings

FontVariationSettingsFromWeightStyle creates settings that configure FontWeight and FontStyle.

func NewFontVariationSettings

func NewFontVariationSettings(settings ...FontVariationSetting) *FontVariationSettings

NewFontVariationSettings creates a FontVariationSettings from a list of settings. Panics if duplicate axis names are provided.

func (*FontVariationSettings) Equals

Equals checks if two FontVariationSettings are equal.

func (*FontVariationSettings) IsEmpty

func (s *FontVariationSettings) IsEmpty() bool

IsEmpty returns true if there are no settings.

func (*FontVariationSettings) NeedsDensity

func (s *FontVariationSettings) NeedsDensity() bool

NeedsDensity returns true if any setting requires density to resolve.

func (*FontVariationSettings) Settings

Settings returns all settings in this collection.

type FontWeight

type FontWeight int

FontWeight represents the thickness of the glyphs, in a range of [1, 1000].

const (
	// FontWeightW100 is the thinnest font weight (Thin)
	FontWeightW100 FontWeight = 100
	// FontWeightW200 is extra light weight
	FontWeightW200 FontWeight = 200
	// FontWeightW300 is light weight
	FontWeightW300 FontWeight = 300
	// FontWeightW400 is normal/regular weight
	FontWeightW400 FontWeight = 400
	// FontWeightW500 is medium weight
	FontWeightW500 FontWeight = 500
	// FontWeightW600 is semi-bold weight
	FontWeightW600 FontWeight = 600
	// FontWeightW700 is bold weight
	FontWeightW700 FontWeight = 700
	// FontWeightW800 is extra-bold weight
	FontWeightW800 FontWeight = 800
	// FontWeightW900 is black (heaviest) weight
	FontWeightW900 FontWeight = 900
)

Standard font weight constants

const (
	FontWeightUnspecified FontWeight = weightUnspecified
)

func FontWeightValues

func FontWeightValues() []FontWeight

FontWeightValues returns a list of all standard font weights.

func LerpFontWeight

func LerpFontWeight(start, stop FontWeight, fraction float32) FontWeight

LerpFontWeight linearly interpolates between two FontWeights. The fraction represents position on the timeline: 0.0 returns start, 1.0 returns stop.

func NewFontWeight

func NewFontWeight(weight int) FontWeight

NewFontWeight creates a FontWeight with validation. Panics if weight is not in range [1, 1000].

func (FontWeight) Compare

func (w FontWeight) Compare(other FontWeight) int

Compare compares two FontWeights. Returns -1 if w < other, 0 if equal, 1 if w > other.

func (FontWeight) Equals

func (w FontWeight) Equals(other FontWeight) bool

Equals checks if two FontWeights are equal.

func (FontWeight) IsFontWeight

func (w FontWeight) IsFontWeight() bool

func (FontWeight) String

func (w FontWeight) String() string

String returns a string representation of the FontWeight.

func (FontWeight) TakeOrElse

func (w FontWeight) TakeOrElse(other FontWeight) FontWeight

func (FontWeight) Weight

func (w FontWeight) Weight() int

Weight returns the underlying weight value.

type GenericFontFamily

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

GenericFontFamily represents a font family with a generic font family name. If the platform cannot find the passed generic font family, it uses the platform default.

func NewGenericFontFamily

func NewGenericFontFamily(name, fontFamilyName string) *GenericFontFamily

NewGenericFontFamily creates a new GenericFontFamily.

func (*GenericFontFamily) Equals

func (g *GenericFontFamily) Equals(other FontFamily) bool

func (*GenericFontFamily) Name

func (g *GenericFontFamily) Name() string

Name returns the generic font family name (e.g., "sans-serif", "serif").

func (*GenericFontFamily) String

func (g *GenericFontFamily) String() string

String returns the font family display name.

type LoadedFontFamily

type LoadedFontFamily struct {
	Typeface Typeface
}

LoadedFontFamily defines a font family that is already a loaded Typeface.

func NewLoadedFontFamily

func NewLoadedFontFamily(typeface Typeface) *LoadedFontFamily

NewLoadedFontFamily creates a LoadedFontFamily from a typeface.

func (*LoadedFontFamily) Equals

func (l *LoadedFontFamily) Equals(other FontFamily) bool

Equals checks if two LoadedFontFamilies are equal.

func (*LoadedFontFamily) String

func (l *LoadedFontFamily) String() string

String returns a string representation of the LoadedFontFamily.

type LoadedTypeface

type LoadedTypeface struct {
	Source string
}

LoadedTypeface is a placeholder implementation of Typeface.

func (*LoadedTypeface) FontFamily

func (l *LoadedTypeface) FontFamily() FontFamily

type PlatformFontLoader

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

PlatformFontLoader implements FontLoader to load fonts from URLs or files.

func NewPlatformFontLoader

func NewPlatformFontLoader(cacheManager *CacheManager, httpClient *http.Client) *PlatformFontLoader

NewPlatformFontLoader creates a new PlatformFontLoader.

func (*PlatformFontLoader) Load

func (l *PlatformFontLoader) Load(ctx context.Context, font Font) (Typeface, error)

Load loads the font.

type ResolvedTypeface

type ResolvedTypeface struct {
	FontFamily FontFamily
	FontWeight FontWeight
	FontStyle  FontStyle
}

ResolvedTypeface represents a resolved font configuration.

type ResourceLoader

type ResourceLoader interface {
	// Load loads the font data.
	// It returns the loaded font/typeface or an error.
	Load(font Font) (interface{}, error)
}

ResourceLoader allows loading fonts from various sources. This interface separates the font definition from the mechanism to load the actual bytes.

type SystemFontFamily

type SystemFontFamily interface {
	FontFamily
	// contains filtered or unexported methods
}

SystemFontFamily is a base type for FontFamilies installed on the system.

type Typeface

type Typeface interface {
	// FontFamily returns the font family used for creating this Typeface.
	// If a platform Typeface was used, it will return nil.
	FontFamily() FontFamily
}

Typeface is a class that can be used for changing the font used in text.

type UrlFont

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

UrlFont represents a font that is loaded from a URL.

func NewUrlFont

func NewUrlFont(url string, weight FontWeight, style FontStyle, loadingStrategy FontLoadingStrategy) *UrlFont

NewUrlFont creates a new UrlFont.

func (*UrlFont) LoadingStrategy

func (u *UrlFont) LoadingStrategy() FontLoadingStrategy

LoadingStrategy returns the loading strategy of the font.

func (*UrlFont) String

func (u *UrlFont) String() string

String returns a string representation of the UrlFont.

func (*UrlFont) Style

func (u *UrlFont) Style() FontStyle

Style returns the style of the font.

func (*UrlFont) Url

func (u *UrlFont) Url() string

Url returns the URL of the font.

func (*UrlFont) Weight

func (u *UrlFont) Weight() FontWeight

Weight returns the weight of the font.

Jump to

Keyboard shortcuts

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