Documentation
¶
Overview ¶
Package text lays out paragraphs on go-text: the font collection, shaping, line breaking, bidi and glyph outlines.
Flutter: libtxt and Skia's paragraph module (engine/src/flutter/third_party/txt).
Index ¶
- Constants
- func IsEmoji(r rune) bool
- func IsGenericFamily(family string) bool
- type Aliaser
- type Collection
- func (c *Collection) AddBundled(family string, data []byte) error
- func (c *Collection) AddFont(family string, data []byte) error
- func (c *Collection) Face(ctx context.Context, f FontFile, variations ...Variation) (*font.Face, error)
- func (c *Collection) Fallback(ctx context.Context, family string, weight int, italic bool, lang string, ...) (FontFile, bool, error)
- func (c *Collection) Families(ctx context.Context) ([]string, error)
- func (c *Collection) Generation() uint64
- func (c *Collection) Match(ctx context.Context, family string, weight int, italic bool, lang string) ([]FontFile, error)
- type CollectionOpts
- type Fallbacker
- type FontFile
- type Lister
- type Matcher
- type Variation
Constants ¶
const BundledPrefix = "bundled:"
BundledPrefix starts the Path of a FontFile that is a bundled font (AddBundled) rather than a file on disk.
Flutter: no counterpart
const LoadedPrefix = "loaded:"
LoadedPrefix starts the Path of a FontFile that is a font the app loaded at run time (AddFont), followed by the number of the font in the collection.
Flutter: no counterpart
Variables ¶
This section is empty.
Functions ¶
func IsEmoji ¶
IsEmoji reports whether the rune is one that a text normally shows as a color emoji: the pictographs of the emoji blocks and the symbols and dingbats that have an emoji presentation. A font that has such a rune in color is preferred over one that has it as a black and white symbol.
Flutter: no counterpart
func IsGenericFamily ¶
IsGenericFamily says whether the family name is a generic one (sans-serif, serif, monospace, cursive, fantasy, emoji or math), which stands for the system's fonts of that kind.
Flutter: no counterpart
Types ¶
type Aliaser ¶
type Aliaser interface {
// Aliases returns the other names of the family, most preferred first; none for a
// family that the configuration says nothing about.
Aliases(ctx context.Context, family string) ([]string, error)
}
Aliaser is a Matcher that also knows which other family names a family stands for, such as Liberation Sans for Arial. A Collection uses it when the Matcher implements it: a family that is not generic gets the system fonts of its own family and of these names, and no other (Collection.Match).
Flutter: no counterpart
type Collection ¶
type Collection struct {
// contains filtered or unexported fields
}
Collection finds and loads fonts: the bundled ones, and the system fonts in the order the Matcher (or the font scan) gives. It caches the loaded faces. It is safe for concurrent use, but a font.Face it returns is not: each variation set has its own Face, which one goroutine at a time may use.
Flutter: engine/src/flutter/third_party/txt/src/txt/font_collection.cc FontCollection
func NewCollection ¶
func NewCollection(opts ...CollectionOpts) *Collection
NewCollection makes an empty font collection. More than one options value panics with MultipleOptions.
Flutter: engine/src/flutter/third_party/txt/src/txt/font_collection.cc FontCollection::FontCollection
func (*Collection) AddBundled ¶
func (c *Collection) AddBundled(family string, data []byte) error
AddBundled registers a font that is part of the program under a family name, such as Roboto or MaterialIcons. A request for that family finds it before any system font. data is the content of a TrueType or OpenType file (a collection is read at index 0). It fails with FontBundledInvalid when the data is not a font.
Flutter: engine/src/flutter/third_party/txt/src/txt/font_collection.cc FontCollection::RegisterFont
func (*Collection) AddFont ¶
func (c *Collection) AddFont(family string, data []byte) error
AddFont registers a font that the app loads at run time under a family name. A family can hold several fonts (regular, bold, italic...): Match puts the closest to the requested weight and slant first, then the others of the family. The weight and the slant come from the font (its OS/2 table; a variable font counts as every weight of its wght axis). When family is empty, the family name of the font is used. data is a TrueType or OpenType file (a collection is read at index 0); the Collection keeps a copy. The same bytes under the same family are added once. The font wins over a bundled font of the same family. It fails with FontDataInvalid when data is not a font, and with FontFamilyMissing when family is empty and the font names none.
Flutter: dart:ui loadFontFromList
func (*Collection) Face ¶
func (c *Collection) Face(ctx context.Context, f FontFile, variations ...Variation) (*font.Face, error)
Face returns the face of the font with the variable axes set. The same file, index and variations give the same Face. It fails with FontLoadFailed when the file cannot be read or is not a font, or has no face at the index, and with FontLoadCanceled when ctx is done. A variation whose tag is not four letters fails with FontVariationInvalid.
Flutter: engine/src/flutter/third_party/txt/src/txt/font_collection.cc FontCollection::CreateMinikinFontCollection
func (*Collection) Fallback ¶
func (c *Collection) Fallback(ctx context.Context, family string, weight int, italic bool, lang string, r rune) (FontFile, bool, error)
Fallback returns a system font that has a glyph for r, when the fonts of Match do not; it is false when there is none (or the Matcher cannot look for a character). It fails like Match.
Flutter: engine/src/flutter/third_party/txt/src/txt/font_collection.cc FontCollection::GetFallbackFontForRune
func (*Collection) Families ¶
func (c *Collection) Families(ctx context.Context) ([]string, error)
Families lists the families of the loaded fonts, of the bundled fonts and of the system's fonts (when the Matcher is a Lister, or the font scan), sorted without regard to case and without duplicates. It fails with FontMatchCanceled when ctx is done (and returns nothing) and with FontScanFailed when the fonts cannot be scanned; the Matcher's own error is returned unchanged, and with it the loaded and bundled families.
Flutter: no counterpart
func (*Collection) Generation ¶
func (c *Collection) Generation() uint64
Generation is a number that grows whenever the loaded or the bundled fonts change. A cache of what Match and Face returned is valid while it stays the same.
Flutter: no counterpart
func (*Collection) Match ¶
func (c *Collection) Match(ctx context.Context, family string, weight int, italic bool, lang string) ([]FontFile, error)
Match returns the fonts for the family, best first: the fonts the app loaded under that family (the closest to the weight and slant first), the bundled font of that family if there is one, then the system fonts in the order of the Matcher or of the font scan (only TrueType and OpenType files, which go-text reads). For a generic family (IsGenericFamily) that is the Matcher's whole list, which goes on with the fonts that follow in the user's configuration. For any other family it is only the system fonts that belong to it: those of the family itself, and those of the names that the Matcher says it stands for (Aliaser); a family that no installed font has gives none, so the caller can go on to the next family instead of getting the system's default font. weight is an OpenType weight (400 regular, 700 bold), lang a BCP 47 tag or "". It fails with FontMatchCanceled when ctx is done, with FontScanFailed when the fonts cannot be scanned; the Matcher's own error is returned unchanged.
Flutter: engine/src/flutter/third_party/txt/src/txt/font_collection.cc FontCollection::GetMinikinFontCollectionForFamilies
type CollectionOpts ¶
type CollectionOpts struct {
// Matcher decides the fallback order. When nil, the Collection scans the system
// font directories with go-text's fontscan, and keeps its index in CacheDir.
Matcher Matcher
// CacheDir is where the fontscan index is kept. It defaults to
// $XDG_CACHE_HOME/glutter/fonts (~/.cache/glutter/fonts).
CacheDir string
// Logger receives the non-fatal problems fontscan finds (fonts it cannot read).
// It defaults to a logger that discards them.
Logger interface {
Printf(format string, args ...any)
}
}
CollectionOpts are the optional parameters of NewCollection.
Flutter: no counterpart
type Fallbacker ¶
type Fallbacker interface {
// Fallback returns a font with a glyph for r, or false when there is none.
Fallback(ctx context.Context, family string, weight int, italic bool, lang string, r rune) (FontFile, bool, error)
}
Fallbacker is a Matcher that can also find a font for one character that the fonts of Match do not have. A Collection uses it when the Matcher implements it.
Flutter: no counterpart
type FontFile ¶
type FontFile struct {
// Path is the font file, BundledPrefix followed by the family of a bundled
// font, or LoadedPrefix followed by the number of a loaded font.
Path string
// Index is the face in a font collection (the low 16 bits). The high 16 bits
// that fontconfig sets for a named instance of a variable font are ignored:
// the caller sets the variable axes (the weight, for one) with Variation.
Index int
// Family is the family name of the font, or "" when unknown.
Family string
}
FontFile is one font of a fallback list: a file on disk (or a bundled font) and the face in it. Its fields are those of the fontconfig package's File.
Flutter: no counterpart
type Lister ¶
type Lister interface {
// Families returns the family names of the installed fonts, in any order.
Families(ctx context.Context) ([]string, error)
}
Lister is a Matcher that can also list the families of the system's fonts. A Collection uses it for Families when the Matcher implements it.
Flutter: no counterpart
type Matcher ¶
type Matcher interface {
// Match returns the fallback order for the family (which can be a generic name
// such as "sans-serif"), the OpenType weight (400 regular), the slant and the
// BCP 47 language tag ("" for the default).
Match(ctx context.Context, family string, weight int, italic bool, lang string) ([]FontFile, error)
}
Matcher says which font files fit a family and style, best first. The fontconfig package implements it on Linux; without it the Collection scans the font directories itself.
Flutter: no counterpart