winfonts

package
v1.17.13 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: BSD-3-Clause Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package winfonts finds the system fonts of Windows: it answers a request for a family, a weight, a slant and a language with the fonts in the order Windows would try them, and a request for a character with the first font of the fallback chain that has it (SPEC §25.8).

The package is plain Go: it does not call Windows and has no build tags, so it is tested on every OS with lists of families and files written for the tests. The reading of the system (the font directories and the registry keys that list the installed fonts) is in package internal/platform/windows (fonts_windows.go), which gives the list of files to Scan; Scan reads the family, the weight and the slant of each face from the font file with go-text, as fontscan does. The matcher implements text.Matcher, text.Aliaser, text.Lister and text.Fallbacker.

Flutter: no counterpart (Skia: src/ports/SkFontMgr_win_dw.cpp chooses the fonts through DirectWrite; Glutter does not use DirectWrite)

Index

Constants

View Source
const (
	// TagUnavailable is the tag of New when there are no installed faces.
	//
	// Flutter: no counterpart
	TagUnavailable = "WindowsFontsUnavailable"
)

The tags of the errors that this file makes (rm4n0s/errors, R5).

Variables

This section is empty.

Functions

func SupportedFile

func SupportedFile(path string) bool

SupportedFile says whether go-text can read the file, judging by its extension: TrueType and OpenType fonts and their collections. Windows also lists bitmap fonts (.fon) and Type 1 fonts.

Flutter: no counterpart

Types

type Cache

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

Cache keeps the faces that Scan read, so that the next start of the app does not open every font file of the machine (hundreds on Windows). It is kept in a file in the cache directory of the user (Load and Save); a damaged file is an empty cache. It is not safe for concurrent use; Scan locks it.

Flutter: no counterpart

func LoadCache

func LoadCache(path string) *Cache

LoadCache reads the cache from a file. A file that is missing, damaged or of another version gives an empty cache, with no error: the next Scan reads the fonts and Save writes the file again.

Flutter: no counterpart

func NewCache

func NewCache() *Cache

NewCache makes an empty cache.

Flutter: no counterpart

func (*Cache) Dirty

func (c *Cache) Dirty() bool

Dirty says whether Scan learned a file since the cache was loaded or saved.

Flutter: no counterpart

func (*Cache) Save

func (c *Cache) Save(path string, keep func(path string) bool) error

Save writes the cache to the file, when it learned something. The entries of files that are no longer in the list are dropped first (keep says which files are). It fails with WindowsFontCacheWriteFailed.

Flutter: no counterpart

type Face

type Face struct {
	// Family is the name of the family.
	Family string
	// Path is the font file; Index the face in it (0 for a file with one font).
	Path  string
	Index int
	// Weight is the OpenType weight (400 regular, 700 bold) and Italic whether the face is slanted
	// (italic or oblique).
	Weight int
	Italic bool
}

Face is one face of a font file that is installed: the family it belongs to, as its name table says (the preferred family, so that "Segoe UI Semibold" is a weight of "Segoe UI"), its weight and slant, and where it is. A font collection (.ttc) has a face for each font in it.

Flutter: no counterpart

func Describe

func Describe(path string) ([]Face, error)

Describe reads the faces of a font file: the family, the weight and the slant of each font in it. The file is not kept open. It fails with WindowsFontUnreadable when the file cannot be opened or is not a font.

Flutter: no counterpart

func Scan

func Scan(ctx context.Context, files []string, cache *Cache, onError func(path string, err error)) []Face

Scan reads the faces of the files, several at a time. A file that cannot be read is left out and reported to onError (when it is not nil); the faces keep the order of the files. A cache that has the file with the same stamp (see Stamp) saves reading it, and learns the files it did not have.

Flutter: no counterpart

type Matcher

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

Matcher chooses the system fonts of Windows from the list of installed faces (SPEC §25.8). Build it with New. It is safe for concurrent use.

Flutter: no counterpart

func New

func New(opts Options) (*Matcher, error)

New builds the matcher from the installed faces. It fails with WindowsFontsUnavailable when there are no faces.

Flutter: no counterpart

func (*Matcher) Aliases

func (m *Matcher) Aliases(ctx context.Context, name string) ([]string, error)

Aliases returns the installed families that the name stands for, most preferred first: Arial for Helvetica. A name that is not an alias, or an alias with no installed target, has none. It fails with WindowsFontsCanceled when ctx is done.

Flutter: no counterpart

func (*Matcher) Covers

func (m *Matcher) Covers(_ context.Context, f text.FontFile, r rune) (has, known bool)

Covers implements text.Coverer with the reading of the font's cmap that Fallback uses (Options.HasGlyph), so that the text layout does not parse a font that cannot show a character. It knows only the installed faces, and is unknown for a font it cannot read.

Flutter: no counterpart

func (*Matcher) Fallback

func (m *Matcher) Fallback(ctx context.Context, name string, weight int, italic bool, lang string, r rune) (text.FontFile, bool, error)

Fallback returns the first font that has a glyph for r: the font of the family (when it is installed), then, for an emoji, Segoe UI Emoji, then the families of the fallback chain in the order for the language. For each family only the face nearest to the weight and slant is asked. It is false when no font has the character. It fails with WindowsFontsCanceled when ctx is done.

Flutter: no counterpart

func (*Matcher) Families

func (m *Matcher) Families(ctx context.Context) ([]string, error)

Families returns the names of the installed families, sorted without regard to case. It fails with WindowsFontsCanceled when ctx is done.

Flutter: no counterpart

func (*Matcher) Match

func (m *Matcher) Match(ctx context.Context, name string, weight int, italic bool, lang string) ([]text.FontFile, error)

Match returns the fonts for the family, the OpenType weight (400 regular), the slant and the BCP 47 language, best first: the family itself (its face nearest to the weight and slant), then, for a generic family (sans-serif, serif, monospace, cursive, fantasy, system-ui, math and emoji), the families of the fallback chain, each with its nearest face, in the order for the language. A family that is not generic gives its own face only, or that of the family an alias stands for (Helvetica is Arial); one that is not installed gives none and no error, so the caller can go on to the next family. It fails with WindowsFontsCanceled when ctx is done.

Flutter: no counterpart

type Options

type Options struct {
	// Faces are the installed faces (Scan reads them from the files). New fails when there are none.
	Faces []Face
	// Locale is the BCP 47 tag of the language used when a request names none (the user's first
	// language); "" keeps the order of the chain.
	Locale string
	// HasGlyph says whether the face at index of a font file has a glyph for r. It is how Fallback
	// finds a font for a character. It defaults to the cmap of the font, read with go-text and kept
	// per file and index.
	HasGlyph func(path string, index int, r rune) bool
}

Options are the parameters of New.

Flutter: no counterpart

type Stamp

type Stamp struct {
	Size    int64
	ModTime int64 // Unix nanoseconds
}

Stamp identifies the state of a font file: the cache trusts the faces it holds for a file while the size and the modification time are the same.

Flutter: no counterpart

Jump to

Keyboard shortcuts

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