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.
The package uses a collection of standard fonts compiled from the os-font-list project along with string processing and similarity metrics for scoring font matches, in order to account for partial or inexact input queries.
Full documentation can be found at: https://pkg.go.dev/github.com/adrg/sysfont.
go get github.com/adrg/sysfont
finder := sysfont.NewFinder(nil)
for _, font := range finder.List() {
fmt.Println(font.Family, font.Name, font.Filename)
}
The matching process has three steps. Identification of the best matching installed font, based on the specified query, is attempted first. If no close match is found, alternative fonts are searched. If no alternative font is found, a suitable default font is returned.
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)
fmt.Printf("%-30s -> %-30s (%s)\n", term, font.Name, font.Filename)
}
A more comprehensive test made on Ubuntu:
For more information see:
Contributions in the form of pull requests, issues or just general feedback,
are always welcome.
See CONTRIBUTING.MD.
Copyright (c) 2019 Adrian-George Bostan.
This project is licensed under the MIT license. See LICENSE for more details.