pub struct GoogleFonts { /* private fields */ }Expand description
A view into the Google Fonts library.
This struct holds a path to a local checkout of the Google Fonts repo and
provides cached, read-only accessors for families, tags and language
metadata. All accessors return borrowed references where possible so callers
should hold the GoogleFonts value for as long as they need the returned
references.
Implementations§
Source§impl GoogleFonts
impl GoogleFonts
Sourcepub fn new(p: PathBuf, family_filter: Option<Regex>) -> Self
pub fn new(p: PathBuf, family_filter: Option<Regex>) -> Self
Create a new GoogleFonts view.
p should be the path to the root of a local Google Fonts repository
checkout (the directory containing METADATA.pb files and the
tags/ directory). family_filter, if present, is a regular
expression used to filter which families are exposed by the
families() iterator.
This constructor does not perform I/O; metadata is read lazily when the corresponding accessor is called.
Return the parsed tag entries for the repository.
On first call this will read and parse the CSV files from the repo’s
tags/all directory. Returns Ok(&[Tag]) when parsing succeeded, or
Err(&Error) if an I/O or parse error occurred. The returned slice is
borrowed from internal storage and remains valid for the lifetime of
self.
Sourcepub fn tag_metadata(&self) -> Result<&[TagMetadata], &Error>
pub fn tag_metadata(&self) -> Result<&[TagMetadata], &Error>
Return tag metadata (min/max and prompt names) for tags defined in the repository.
This reads tags/tags_metadata.csv on first access and returns a
borrowed slice on success. Errors are returned as Err(&Error).
Sourcepub fn families(&self) -> &[(PathBuf, Result<FamilyProto, ParseError>)]
pub fn families(&self) -> &[(PathBuf, Result<FamilyProto, ParseError>)]
Return a list of discovered families and their parsed metadata.
Each entry is a tuple (PathBuf, Result<FamilyProto, ParseError>).
The PathBuf is the path to the METADATA.pb file for the family.
The Result contains the parsed FamilyProto on success or a
ParseError if the metadata could not be parsed. Families are
discovered lazily by scanning the repository and applying the
family_filter provided at construction (if any).
The returned slice is borrowed from internal storage and stays valid
for the lifetime of self.
Sourcepub fn language(&self, lang_id: &str) -> Option<&LanguageProto>
pub fn language(&self, lang_id: &str) -> Option<&LanguageProto>
Lookup a language by its identifier.
The lang_id should be the language identifier used by the
google-fonts-languages crate (for example “en_Latn”). Returns
Some(&LanguageProto) if the language is known, otherwise None.
This is a simple passthrough to the bundled LANGUAGES map.
Sourcepub fn family(&self, font: &FontProto) -> Option<(&Path, &FamilyProto)>
pub fn family(&self, font: &FontProto) -> Option<(&Path, &FamilyProto)>
Given a FontProto, return the family it belongs to.
If the provided font is known (by filename) this returns Some((path, family))
where path is the path to the family’s METADATA.pb and family is
a borrowed FamilyProto. Returns None if the font is not present in
the discovered families.
Sourcepub fn find_font_binary(&self, font: &FontProto) -> Option<PathBuf>
pub fn find_font_binary(&self, font: &FontProto) -> Option<PathBuf>
Find the path to the font binary for a FontProto.
This resolves the font’s family, then constructs the filesystem path
to the font file (sibling to the family’s METADATA.pb). If the
resulting file exists its PathBuf is returned. If the file cannot
be found None is returned. A diagnostic is printed to stderr when
the expected file is missing.
Sourcepub fn primary_language(&self, family: &FamilyProto) -> &LanguageProto
pub fn primary_language(&self, family: &FamilyProto) -> &LanguageProto
Our best guess at the primary language for this family
Meant to be a good choice for things like rendering a sample string Guess the primary language for a family.
The heuristic is:
- If the family declares a
primary_languagethat maps to a known language, return that. - Otherwise if the family declares a
primary_script, pick the most populous language using that script. - Fall back to
en_Latnif nothing else matches.
This is intended as a best-effort choice to select a reasonable language for rendering sample text, not as an authoritative mapping.