Skip to main content

GoogleFonts

Struct GoogleFonts 

Source
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

Source

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.

Source

pub fn tags(&self) -> Result<&[Tagging], &Error>

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.

Source

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).

Source

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.

Source

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.

Source

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.

Source

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.

Source

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:

  1. If the family declares a primary_language that maps to a known language, return that.
  2. Otherwise if the family declares a primary_script, pick the most populous language using that script.
  3. Fall back to en_Latn if 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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.