Documentation
¶
Overview ¶
Package sysfont is a small package that makes it easy to identify installed fonts. It is useful for listing installed fonts or for matching fonts based on user queries. The matching process also suggests viable font alternatives.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Finder ¶
type Finder struct {
// contains filtered or unexported fields
}
Finder is used to identify installed fonts. It can match fonts based on user queries and suggest alternative fonts if the requested fonts are not found.
func NewFinder ¶
func NewFinder(opts *FinderOpts) *Finder
NewFinder returns a new font finder. If the opts parameter is nil, default options are used.
Default options:
Extensions: []string{".ttf", ".ttc", ".otf"}
Example ¶
package main import ( "fmt" "github.com/adrg/sysfont" ) func main() { // Create a new font finder using the default options. finder := sysfont.NewFinder(nil) // Create a new finder which only searches for TTF files. finder = sysfont.NewFinder(&sysfont.FinderOpts{ Extensions: []string{".ttf"}, }) // List detected fonts. for _, font := range finder.List() { fmt.Println(font.Family, font.Name, font.Filename) } }
Output:
func (*Finder) List ¶
List returns the list of installed fonts. The finder attempts to identify the name and family of the returned fonts. If identification is not possible, only the filename field will be filled.
Example ¶
package main import ( "fmt" "github.com/adrg/sysfont" ) func main() { finder := sysfont.NewFinder(nil) for _, font := range finder.List() { fmt.Println(font.Family, font.Name, font.Filename) } }
Output:
func (*Finder) Match ¶
Match attempts to identify the best matching installed font based on the specified query. If no close match is found, alternative fonts are searched. If no alternative font is found, a suitable default font is returned.
Example ¶
package main import ( "fmt" "github.com/adrg/sysfont" ) func main() { finder := sysfont.NewFinder(nil) terms := []string{ "AmericanTypewriter", "AmericanTypewriter-Bold", "Andale", "Arial", "Arial Bold", "Arial-BoldItalicMT", "ArialMT", "Baskerville", "Candara", "Corbel", "Gill Sans", "Hoefler Text Bold", "Impact", "Palatino", "Symbol", "Tahoma", "Times", "Times Bold", "Times BoldItalic", "Times Italic Bold", "Times Roman", "Verdana", "Verdana-Italic", "Webdings", "ZapfDingbats", } for _, term := range terms { font := finder.Match(term) if font == nil { // Match should always return a font. However, it is safer to check. continue } fmt.Printf("%-30s -> %-30s (%s)\n", term, font.Name, font.Filename) } }
Output:
type FinderOpts ¶
type FinderOpts struct { // Extensions controls which types of font files the finder reports. Extensions []string }
FinderOpts contains options for configuring a font finder.