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) Covers(ctx context.Context, f FontFile, r rune) (bool, 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) LastResort() (FontFile, bool)
- func (c *Collection) Match(ctx context.Context, family string, weight int, italic bool, lang string) ([]FontFile, error)
- func (c *Collection) ParsedFonts() int
- type CollectionOpts
- type Coverer
- 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) Covers ¶
Covers says whether the font has a glyph for r, without parsing its outlines. The answer comes from the Matcher when it is a Coverer and knows the font (fontconfig's character set, the footprint of the font scan); otherwise from the font's cmap table alone, which is read once and kept. A font the app loaded or bundled is read from its data. A font whose cmap cannot be read has no glyph, and the call that finds out fails with FontLoadFailed; the later calls for that font are false with no error. It fails with FontLoadCanceled when ctx is done. A true answer is a promise only of the character map: Face(f).NominalGlyph(r) is the last word.
Flutter: no counterpart
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) LastResort ¶
func (c *Collection) LastResort() (FontFile, bool)
LastResort returns the font that draws text when neither the families of a style nor the system give one: the first font registered with AddBundled (Roboto, in an app and in gluttertest), or false when the program bundles none. Match does not list it for another family: a family that nothing has still gives no font of its own, and only the layout falls back to this one, as Flutter's engine falls back to the default font of its font manager.
Flutter: engine/src/flutter/third_party/txt/src/txt/font_collection.cc FontCollection::GetMinikinFontCollectionForFamilies (the default font manager's fallback)
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
func (*Collection) ParsedFonts ¶
func (c *Collection) ParsedFonts() int
ParsedFonts is how many fonts the Collection has tried to read in full so far (the calls of Face that had to parse a font, a font that fails to load among them; a font read for its character map only, by Covers, does not count). The tests and the benchmarks of the text layout use it to see that a style does not parse fonts that cannot help.
Flutter: no counterpart
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 glutter/fonts in the user's cache
// directory (os.UserCacheDir: $XDG_CACHE_HOME or ~/.cache on Linux, ~/Library/Caches on macOS).
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 Coverer ¶
type Coverer interface {
// Covers says whether the font f has a glyph for r. known is false when the Matcher cannot tell
// (it never listed the font, or could not read it); has is then meaningless.
Covers(ctx context.Context, f FontFile, r rune) (has, known bool)
}
Coverer is a Matcher that already knows which characters its fonts have, from data it keeps (fontconfig's character sets, the footprints of the font scan, the readings of a cache), so that a Collection can rule a font out for a character without opening the font. A Collection uses it when the Matcher implements it, for the system fonts only; for a font the Matcher knows nothing about it reads the font's cmap table (Collection.Covers).
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
Directories
¶
| Path | Synopsis |
|---|---|
|
Package androidfonts finds the system fonts of an Android device the way Skia's Android font manager does (src/ports/SkFontMgr_android.cpp and SkFontMgr_android_parser.cpp): it reads the font configuration of the system (/system/etc/fonts.xml, and the fonts_customization.xml of the vendor), builds the families with their names, aliases, weights, styles, languages and variants, and answers a request for a family, a weight, a slant and a language with the fonts in the order the system would try them, and a request for a character with the first font that has it.
|
Package androidfonts finds the system fonts of an Android device the way Skia's Android font manager does (src/ports/SkFontMgr_android.cpp and SkFontMgr_android_parser.cpp): it reads the font configuration of the system (/system/etc/fonts.xml, and the fonts_customization.xml of the vendor), builds the families with their names, aliases, weights, styles, languages and variants, and answers a request for a family, a weight, a slant and a language with the fonts in the order the system would try them, and a request for a character with the first font that has it. |
|
Package darwinfonts finds the system fonts of macOS: it answers a request for a family, a weight, a slant and a language with the fonts in the order a Mac would try them, and a request for a character with the font that Core Text chooses for it, or the first font of a fallback chain that has it (SPEC §29.8).
|
Package darwinfonts finds the system fonts of macOS: it answers a request for a family, a weight, a slant and a language with the fonts in the order a Mac would try them, and a request for a character with the font that Core Text chooses for it, or the first font of a fallback chain that has it (SPEC §29.8). |
|
Package fonttest makes test fonts from the fonts that the module bundles: the same outlines under another character map, so that a test can have many fonts that cover different characters, and a font whose character map cannot be read, without keeping font files in the repository.
|
Package fonttest makes test fonts from the fonts that the module bundles: the same outlines under another character map, so that a test can have many fonts that cover different characters, and a font whose character map cannot be read, without keeping font files in the repository. |
|
Package layout lays out paragraphs: it turns the styled text of a ui.ParagraphBuilder into lines of shaped glyphs with go-text.
|
Package layout lays out paragraphs: it turns the styled text of a ui.ParagraphBuilder into lines of shaped glyphs with go-text. |
|
Package winfonts finds the system fonts of Windows: it answers a request for a family, a weight, a slant and a language with the fonts in the order Windows would try them, and a request for a character with the first font of the fallback chain that has it (SPEC §25.8).
|
Package winfonts finds the system fonts of Windows: it answers a request for a family, a weight, a slant and a language with the fonts in the order Windows would try them, and a request for a character with the first font of the fallback chain that has it (SPEC §25.8). |