font

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2024 License: MIT Imports: 7 Imported by: 21

Documentation

Overview

The font subpackage contains helper methods to parse fonts and obtain information from them (id, name, family, etc.), alongisde a Library type to assist with their management if necessary.

Using a Library is actually rather uncommon, as most small games do not use more than a couple fonts and will generally be better off avoiding the abstraction.

Index

Constants

This section is empty.

Variables

View Source
var ErrAlreadyPresent = errors.New("font already present in the library")

An error that can be returned by Library.AddFont(), Library.ParseFromPath() and Library.ParseFromBytes() when a font is not added due to its name already being present in the Library.

View Source
var ErrBreakEach = errors.New("EachFont() early break")

Special error that can be used with Library.EachFont() to break early. When used, the function will return early but still return a nil error.

View Source
var ErrNotFound = errors.New("font property not found or empty")

A common error returned by font property getter functions.

Functions

func GetFamily

func GetFamily(font *sfnt.Font) (string, error)

Returns the family name of the given font. If the information is missing, ErrNotFound will be returned. Other errors are also possible (e.g., if the font naming table is invalid).

func GetIdentifier

func GetIdentifier(font *sfnt.Font) (string, error)

Returns the identifier of the given font. If the information is missing, ErrNotFound will be returned. Other errors are also possible (e.g., if the font naming table is invalid).

func GetMissingRunes

func GetMissingRunes(font *sfnt.Font, text string) ([]rune, error)

Returns the runes in the given text that can't be represented by the font. If runes are repeated in the input text, the returned slice may contain them multiple times too.

If you load fonts dynamically, it is good practice to use this function or the simpler IsMissingRunes() to make sure that the fonts include all the glyphs that you require.

func GetName

func GetName(font *sfnt.Font) (string, error)

Returns the name of the given font. If the information is missing, ErrNotFound will be returned. Other errors are also possible (e.g., if the font naming table is invalid).

func GetProperty

func GetProperty(font *sfnt.Font, property sfnt.NameID) (string, error)

Returns the requested font property for the given font. The returned property string might be empty even when error is nil. If the property is missing, ErrNotFound will be returned.

func GetSubfamily

func GetSubfamily(font *sfnt.Font) (string, error)

Returns the subfamily name of the given font. If the information is missing, ErrNotFound will be returned. Other errors are also possible (e.g., if the font naming table is invalid).

In most cases, the subfamily value will be one of:

  • "Regular", "Italic", "Bold", "Bold Italic"

func IsMissingRunes

func IsMissingRunes(font *sfnt.Font, text string) (bool, error)

Returns true iff the font is missing any of the glyphs required to render the given text. More casual version of GetMissingRunes().

func ParseFromBytes

func ParseFromBytes(fontBytes []byte) (*sfnt.Font, string, error)

Similar to sfnt.Parse(), but also including the font name in the returned values. The bytes must not be modified while the font is in use.

This is a low level function; you may prefer to use a Library instead.

func ParseFromFS

func ParseFromFS(filesys fs.FS, path string) (*sfnt.Font, string, error)

Same as ParseFromPath(), but for embedded filesystems.

This is a low level function; you may prefer to use a Library instead.

func ParseFromPath

func ParseFromPath(path string) (*sfnt.Font, string, error)

Attempts to parse a font located the given filepath and returns it along its name and any possible error. Supported formats are .ttf and .otf.

This is a low level function; you may prefer to use a Library instead.

Types

type Library

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

A collection of fonts accessible by name.

The goal of a library is to make it easy to parse fonts in bulk and keep them all in a single place.

A library doesn't know about system fonts, but there are other packages out there that help you with that if you need it.

func NewLibrary

func NewLibrary() *Library

Creates a new, empty font Library.

func (*Library) AddFont

func (self *Library) AddFont(font *sfnt.Font) (string, error)

Adds the given font into the library and returns its name and any possible error. If the given font is nil, the method will panic. If another font with the same name was already present in the library, ErrAlreadyPresent will be returned.

This method is rarely necessary unless the font parsing is done by an external package. In general, using the built-in parsing functions (e.g. Library.ParseFromBytes()) would be preferable.

func (*Library) EachFont

func (self *Library) EachFont(fontFunc func(string, *sfnt.Font) error) error

Calls the given function for each font in the library, passing their names and content as arguments, in pseudo-random order.

If the given function returns a non-nil error, the method will immediately stop and return that error, with the only exception of ErrBreakEach. Otherwise, Library.EachFont() will always return nil.

Example code to print the names of all the fonts in the library:

library.EachFont(func(name string, _ *sfnt.Font) error {
    fmt.Println(name)
    return nil
})

func (*Library) GetFont

func (self *Library) GetFont(name string) *sfnt.Font

Returns the font with the given name, or nil if not found.

If you don't know what are the names of your fonts, there are a few ways to figure it out:

  • Add the fonts into the font library and print their names with Library.EachFont().
  • Use the GetName() function directly on a font.
  • Open a font with the OS's default font viewer; the name is usually on the title and/or first line of text.

func (*Library) HasFont

func (self *Library) HasFont(name string) bool

Finds out whether a font with the given name exists in the library.

func (*Library) ParseAllFromFS

func (self *Library) ParseAllFromFS(filesys fs.FS, dirName string) (added, skipped int, err error)

The equivalent of Library.ParseAllFromPath() for filesystems. This is mainly provided to support embed.FS and embedded fonts.

func (*Library) ParseAllFromPath

func (self *Library) ParseAllFromPath(dirName string) (added, skipped int, err error)

Walks the given directory non-recursively and adds all the .ttf and .otf fonts in it. Returns the number of fonts added, the number of fonts skipped (when a font with the same name already exists in the Library) and any error that might happen during the process.

func (*Library) ParseFromBytes

func (self *Library) ParseFromBytes(fontBytes []byte) (string, error)

The equivalent of Library.ParseFromPath() for raw font bytes. The bytes must not be modified while the font is in use. When in doubt, pass a copy (e.g. ParseFromBytes(append([]byte(nil), data))).

func (*Library) ParseFromFS

func (self *Library) ParseFromFS(filesys fs.FS, path string) (string, error)

The equivalent of Library.ParseFromPath() for filesystems. This is mainly provided to support embed.FS and embedded fonts.

func (*Library) ParseFromPath

func (self *Library) ParseFromPath(path string) (string, error)

Returns the name of the added font and any possible error. If error == nil, the font name will be non-empty.

If a font with the same name has already been parsed or added, ErrAlreadyPresent will be returned.

func (*Library) RemoveFont

func (self *Library) RemoveFont(name string) bool

Returns false if the font can't be removed due to not being found.

This function is rarely necessary unless your program exposes font management directly to the user.

The given font name must match the name returned by the original font parsing function. Font names can also be recovered through Library.EachFont().

func (*Library) Size

func (self *Library) Size() int

Returns the current number of fonts in the library.

Jump to

Keyboard shortcuts

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