sysfont

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2021 License: MIT Imports: 7 Imported by: 54

README

sysfont

Build Status pkg.go.dev documentation MIT license Go report card GitHub issues Buy me a coffee

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.

Installation

go get github.com/adrg/sysfont

Usage

List fonts
finder := sysfont.NewFinder(nil)

for _, font := range finder.List() {
    fmt.Println(font.Family, font.Name, font.Filename)
}
Match fonts

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)
}

Output: sysfont test output minimal

A more comprehensive test made on Ubuntu: sysfont test output full

References

For more information see:

Contributing

Contributions in the form of pull requests, issues or just general feedback, are always welcome.
See CONTRIBUTING.MD.

License

Copyright (c) 2019 Adrian-George Bostan.

This project is licensed under the MIT license. See LICENSE for more details.

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

func (f *Finder) List() []*Font

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

func (f *Finder) Match(query string) *Font

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.

type Font

type Font struct {
	// Family contains name of the font family.
	Family string

	// Name contains the full name of the font.
	Name string

	// Filename contains the path of the font file.
	Filename string
}

Font represents a system font.

Jump to

Keyboard shortcuts

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