androidfonts

package
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: BSD-3-Clause Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

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.

The package is plain Go: it does not call the system and has no build tags, so it is tested on every OS with fixture files. The matcher implements text.Matcher, text.Aliaser, text.Lister and text.Fallbacker. The files, the existence of a file and the glyphs of a font come through the functions of Options, so a test does not need real fonts.

Flutter: no counterpart (Skia: src/ports/SkFontMgr_android.cpp)

Index

Constants

View Source
const (
	// SystemFontsXML is the font configuration of Android 5 and later.
	SystemFontsXML = "/system/etc/fonts.xml"
	// SystemFontDir is the folder of the system fonts.
	SystemFontDir = "/system/fonts"
)

The locations of the font configuration on a device (SkFontMgr_android_parser.cpp: LMP_SYSTEM_FONTS_FILE, PRODUCT_CUSTOMIZATION_FILE, and the folder of the fonts, which Skia builds from $ANDROID_ROOT).

View Source
const (
	// TagUnavailable is the tag of New when no font configuration file gave a family or none
	// of the fonts the files name exists.
	//
	// Flutter: no counterpart
	TagUnavailable = "AndroidFontsUnavailable"
)

The tags of the errors that this file makes (rm4n0s/errors, R5).

View Source
const (
	// TagXMLInvalid is the tag of a font configuration file that is not well formed XML, has no
	// element or declares an entity.
	//
	// Flutter: no counterpart
	TagXMLInvalid = "AndroidFontsXMLInvalid"
)

The tags of the errors that this file makes (rm4n0s/errors, R5).

Variables

This section is empty.

Functions

This section is empty.

Types

type Matcher

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

Matcher chooses the system fonts of an Android device as Skia's Android font manager does. Build it with New. It is safe for concurrent use.

Flutter: no counterpart (Skia: src/ports/SkFontMgr_android.cpp SkFontMgr_Android)

func New

func New(opts Options) (*Matcher, error)

New reads the font configuration and builds the matcher. Every file of FontsXML is read in order, then the files of Customization; the fonts whose file does not exist are left out. A file that is missing is skipped. It fails with AndroidFontsXMLInvalid when no file of FontsXML gave a family and one of them was not well formed (the error of the first such file), and with AndroidFontsUnavailable when no file gave a family or none of the fonts they name exists. A customization file with a problem is logged and skipped.

Flutter: no counterpart (Skia: src/ports/SkFontMgr_android.cpp SkFontMgr_Android::SkFontMgr_Android)

func (*Matcher) Aliases

func (m *Matcher) Aliases(ctx context.Context, family string) ([]string, error)

Aliases returns the other names of the family that has the name, its first name first: for arial that is sans-serif and the other names fonts.xml gives it (helvetica, tahoma, verdana...). A name that is not a family, or a family with one name, has none. It fails with AndroidFontsCanceled when ctx is done.

Flutter: no counterpart (Skia: src/ports/SkFontMgr_android_parser.cpp lmpParser::aliasHandler)

func (*Matcher) Covers

func (m *Matcher) Covers(_ context.Context, f text.FontFile, r rune) (has, known bool)

Covers implements text.Coverer with the reading of the font's cmap that Fallback uses (Options.HasGlyph), so that the text layout does not parse a font that cannot show a character. It knows only the fonts of the configuration, and is unknown for a font it cannot read.

Flutter: no counterpart

func (*Matcher) Fallback

func (m *Matcher) Fallback(ctx context.Context, family string, weight int, italic bool, lang string, r rune) (text.FontFile, bool, error)

Fallback returns the first font that has a glyph for r, in the order of Skia's onMatchFamilyStyleCharacter for the family and the language (see Match; unlike Match it does not put the family first: it is the character's turn). For each family only the font nearest to the weight and slant is asked, with Options.HasGlyph. The family may be unknown or "". It is false when no font has the character. It fails with AndroidFontsCanceled when ctx is done.

Flutter: no counterpart (Skia: src/ports/SkFontMgr_android.cpp SkFontMgr_Android::onMatchFamilyStyleCharacter)

func (*Matcher) Families

func (m *Matcher) Families(ctx context.Context) ([]string, error)

Families returns the names of the named families, the aliases included, in the order of the configuration without repeats (Skia: onCountFamilies and onGetFamilyName). It fails with AndroidFontsCanceled when ctx is done.

Flutter: no counterpart (Skia: src/ports/SkFontMgr_android.cpp SkFontMgr_Android::onGetFamilyName)

func (*Matcher) Match

func (m *Matcher) Match(ctx context.Context, family string, weight int, italic bool, lang string) ([]text.FontFile, error)

Match returns the fonts for the family, the OpenType weight (400 regular), the slant and the BCP 47 language, best first: the family itself (its font nearest to the weight and slant, found by its name or by an alias), then the families of the fallback chain, each with its nearest font, in the order Skia's onMatchFamilyStyleCharacter tries them for the language: the families written for the language and its more general tags first (Japanese fonts for ja, Traditional Chinese for zh-TW), then the others in the order of the configuration. A font that is in the list once is not repeated: the fonts of a variable font file that fonts.xml lists once per weight are one font (the caller sets the weight with a variation). The names are those of the configuration (sans-serif, serif, monospace, sans-serif-condensed, casual, arial...), without regard to case; "" is sans-serif. An unknown family gives no fonts and no error: Skia returns no typeface for it either, so that the app can try another family; Fallback finds a font for a character in any case. The generic name emoji gives the color emoji fonts first. The FontFile.Family of a font is the first name of its family, "" for a family with no name. It fails with AndroidFontsCanceled when ctx is done.

Flutter: no counterpart (Skia: src/ports/SkFontMgr_android.cpp SkFontMgr_Android::onMatchFamilyStyle)

type Options

type Options struct {
	// FontsXML are the font configuration files, read in order. Their families are added one
	// after the other, so a later file extends the earlier ones (an alias can name a family of an
	// earlier file). A file that is missing is skipped; New fails when no file gives a family.
	FontsXML []string
	// Customization are the fonts_customization.xml files of the device builder, read after
	// FontsXML. A file that is missing or malformed is skipped (the problem is logged). A relative font
	// file name in /<partition>/etc/fonts_customization.xml is in /<partition>/fonts; in any
	// other path it is in FontDir.
	Customization []string
	// FontDir is where the relative font file names of FontsXML are. It defaults to
	// /system/fonts.
	FontDir string
	// ReadFile reads a configuration file. It defaults to os.ReadFile.
	ReadFile func(path string) ([]byte, error)
	// FileExists says whether a font file exists. A font whose file does not exist is not in its
	// family, and a family with no font is left out, as Skia does with a file it cannot open. It
	// defaults to a check with os.Stat that the path is a regular file.
	FileExists func(path string) bool
	// HasGlyph says whether the face at index of a font file has a glyph for r. It is how Fallback
	// finds a font for a character. It defaults to the cmap of the font, read with go-text and
	// kept per file and index.
	HasGlyph func(path string, index int, r rune) bool

	// Locale is the BCP 47 tag of the language used when a request names none (the user's
	// locale). It is "" by default: the fonts then come in the order of the configuration.
	Locale string
	// Logger receives the problems of the configuration that are not fatal: a file that cannot
	// be read, an unknown element, a value that is not valid. It defaults to a logger that
	// discards them.
	Logger *slog.Logger
	// contains filtered or unexported fields
}

Options are the parameters of New.

Flutter: no counterpart (Skia: src/ports/SkFontMgr_android.h SkFontMgr_Android_CustomFonts)

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns the options for a device: /system/etc/fonts.xml (the configuration of Android 5 and later; the older system_fonts.xml and fallback_fonts.xml are not read, Glutter runs on Android 8 and later), the fonts_customization.xml of /oem, /product and /vendor, and /system/fonts.

Flutter: no counterpart (Skia: src/ports/SkFontMgr_android_parser.cpp SkFontMgr_Android_Parser::GetSystemFontFamilies)

Jump to

Keyboard shortcuts

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