font

package
v0.1.0-experimental.3 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2021 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Overview

Package font is for typeface and font handling.

There is a certain confusion with the nomenclature of typesetting. We will stick to the following definitions:

▪︎ A "typeface" is a family of fonts. An example is "Helvetica". This corresponds to a TrueType "collection" (*.ttc).

▪︎ A "scalable font" is a font, i.e. a variant of a typeface with a certain weight, slant, etc. An example is "Helvetica regular".

▪︎ A "typecase" is a scaled font, i.e. a font in a certain size for a certain script and language. The name is reminiscend on the wooden boxes of typesetters in the era of metal type. An example is "Helvetica regular 11pt, Latin, en_US".

Please note that Go (Golang) does use the terms "font" and "face" differently–actually more or less in an opposite manner.

Status

Does not yet contain methods for font collections (*.ttc), e.g., /System/Library/Fonts/Helvetica.ttc on Mac OS.

License

Governed by a 3-Clause BSD license. License file may be found in the root folder of this module.

Copyright © 2017–2021 Norbert Pillmayer <norbert@pillmayer.com>

Index

Constants

View Source
const (
	StyleNormal = xfont.StyleNormal
	StyleItalic = xfont.StyleItalic
)
View Source
const (
	WeightLight    = xfont.WeightLight
	WeightNormal   = xfont.WeightNormal
	WeightSemiBold = xfont.WeightSemiBold
	WeightBold     = xfont.WeightBold
)

Variables

This section is empty.

Functions

func ClosestMatch

func ClosestMatch(fdescs []Descriptor, pattern string, style xfont.Style,
	weight xfont.Weight) (match Descriptor, variant string, confidence MatchConfidence)

ClosestMatch scans a list of font desriptors and returns the closest match for a given set of parametesrs. If no variant matches, returns `NoConfidence`.

func GuessStyleAndWeight

func GuessStyleAndWeight(fontfilename string) (xfont.Style, xfont.Weight)

GuessStyleAndWeight trys to guess a font's style and weight from the font's file name.

func Matches

func Matches(fontfilename, pattern string, style xfont.Style, weight xfont.Weight) bool

Matches returns true if a font's filename contains pattern and indicators for a given style and weight.

func NormalizeFontname

func NormalizeFontname(fname string, style xfont.Style, weight xfont.Weight) string

Types

type Descriptor

type Descriptor struct {
	Family   string   `json:"family"`
	Variants []string `json:"variants"`
	Path     string   // only used if just a single variant
}

Descriptor represents all the known variants of a font.

type MatchConfidence

type MatchConfidence int

MatchConfidence is a type for expressing the confidence level of font matching.

const (
	NoConfidence      MatchConfidence = 0
	LowConfidence     MatchConfidence = 2
	HighConfidence    MatchConfidence = 3
	PerfectConfidence MatchConfidence = 4
)

func MatchStyle

func MatchStyle(variantName string, style xfont.Style) MatchConfidence

MatchStyle trys to match a font-variant to a given style.

func MatchWeight

func MatchWeight(variantName string, weight xfont.Weight) MatchConfidence

MatchWeight trys to match a font-variant to a given weight.

type Registry

type Registry struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Registry is a type for holding information about loaded fonts for a typesetter.

func GlobalRegistry

func GlobalRegistry() *Registry

GlobalRegistry is an application-wide singleton to hold information about loaded fonts and typecases.

func NewRegistry

func NewRegistry() *Registry

func (*Registry) LogFontList

func (fr *Registry) LogFontList()

LogFontList is a helper function to dump the list of known fonts and typecases in a registry to the trace-file (log-level Info).

func (*Registry) StoreFont

func (fr *Registry) StoreFont(normalizedName string, f *ScalableFont)

StoreFont pushes a font into the registry if it isn't contained yet.

The font will be stored using the normalized font name as a key. If this key is already associated with a font, that font will not be overridden.

func (*Registry) TypeCase

func (fr *Registry) TypeCase(normalizedName string, size float64) (*TypeCase, error)

TypeCase returns a concrete typecase with a given font, style, weight and size. If a suitable typecase has already been cached, TypeCase will return the cached typecase. If a suitable font has previously been stored under key `normalizedName`, a typecase will be derived from this font.

If not typecase can be produced, TypeCase will derive one from a system-wide fallback font and return it, together with an error message.

type ScalableFont

type ScalableFont struct {
	Fontname string
	Filepath string     // file path
	Binary   []byte     // raw data
	SFNT     *sfnt.Font // the font's container // TODO: not threadsafe???
}

ScalableFont is an internal representation of an outline-font of type TTF of OTF.

func FallbackFont

func FallbackFont() *ScalableFont

FallbackFont returns a font to be used if everything else failes. It is always present. Currently we use Go Sans.

func LoadOpenTypeFont

func LoadOpenTypeFont(fontfile string) (*ScalableFont, error)

LoadOpenTypeFont loads an OpenType font (TTF or OTF) from a file.

func ParseOpenTypeFont

func ParseOpenTypeFont(fbytes []byte) (f *ScalableFont, err error)

ParseOpenTypeFont loads an OpenType font (TTF or OTF) from memory.

func (*ScalableFont) PrepareCase

func (sf *ScalableFont) PrepareCase(fontsize float64) (*TypeCase, error)

PrepareCase prepares a typecase in a given point size, e.g. "Helvetica bold 10pt" from an existing font "Helvetiva bold", which has been previously loaded.

type TypeCase

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

TypeCase represents a font at a specific point size, e.g. "Helvetica bold 10pt".

func NullTypeCase

func NullTypeCase() *TypeCase

func (*TypeCase) PtSize

func (tc *TypeCase) PtSize() float64

PtSize returns the point-size of a typecase.

func (*TypeCase) ScalableFontParent

func (tc *TypeCase) ScalableFontParent() *ScalableFont

ScalableFontParent returns the unscaled font a typecase has been derived from.

Directories

Path Synopsis
opentype
ot
Package ot provides access to OpenType font tables and features.
Package ot provides access to OpenType font tables and features.
otlayout
Package otlayout provides access to OpenType font layout features.
Package otlayout provides access to OpenType font layout features.
otquery
Package otquery queries metrics and other information from OpenType fonts.
Package otquery queries metrics and other information from OpenType fonts.
otshape
Package otshape is work in progress.
Package otshape is work in progress.

Jump to

Keyboard shortcuts

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