Skip to main content

FaceSet

Struct FaceSet 

Source
pub struct FaceSet { /* private fields */ }
Expand description

FaceSet stores registered fonts and their global fallback order.

Cloning a face set shares parsed fonts, allowing callers to grow a snapshot without modifying existing holders.

Implementations§

Source§

impl FaceSet

Source

pub fn new() -> Self

new creates an empty face set.

Source

pub fn register(&mut self, family: &str, bytes: Vec<u8>) -> Option<FontId>

register adds font bytes under a family using default attributes.

It returns None when face zero cannot be parsed.

Examples found in repository?
examples/raster_bench.rs (lines 23-26)
16fn main() {
17    let dir = concat!(env!("CARGO_MANIFEST_DIR"), "/../../assets/fonts");
18    // A CJK face is 20k+ glyphs and too big to vendor: point VALO_CJK_FONT
19    // at one to get the CJK column, otherwise the bench runs latin only.
20    let cjk_file = std::env::var("VALO_CJK_FONT").unwrap_or_default();
21    let mut fonts = FaceSet::default();
22    let latin = fonts
23        .register(
24            "Latin",
25            std::fs::read(format!("{dir}/fira_sans.ttf")).unwrap(),
26        )
27        .unwrap();
28    let cjk = std::fs::read(cjk_file)
29        .ok()
30        .and_then(|bytes| fonts.register("CJK", bytes));
31
32    let mut sets = vec![("latin", latin, glyphs(&fonts, latin, LATIN))];
33    if let Some(id) = cjk {
34        sets.push(("cjk", id, glyphs(&fonts, id, CJK)));
35    }
36
37    println!(
38        "{:>6} {:>6}  {:>12} {:>12}  (µs/glyph, n={})",
39        "set",
40        "px",
41        "alpha",
42        "sdf",
43        LATIN.len()
44    );
45    let mut raster = Rasterizer::new();
46    for (label, font, ids) in &sets {
47        for px in SIZES {
48            let alpha = time_per_glyph(ids, |g| {
49                raster.alpha(fonts.get(*font), g, px, 0.0);
50            });
51            let mut raster2 = Rasterizer::new();
52            let sdf = time_per_glyph(ids, |g| {
53                raster2.sdf(fonts.get(*font), g, px);
54            });
55            println!("{label:>6} {px:>6.0}  {alpha:>10.1}µs {sdf:>10.1}µs");
56        }
57    }
58}
Source

pub fn register_with( &mut self, family: &str, attrs: FontAttrs, bytes: Vec<u8>, ) -> Option<FontId>

register_with adds font bytes under a family with explicit attributes.

It returns None when face zero cannot be parsed.

Source

pub fn add(&mut self, font: Font) -> FontId

add registers an already parsed font under its embedded names and attributes.

Source

pub fn with_font(&self, font: Font) -> (FaceSet, FontId)

with_font returns a cloned face set containing one additional font.

Existing fonts remain shared and are not reparsed.

Source

pub fn with_fallbacks(&self, fallbacks: Vec<FontId>) -> FaceSet

with_fallbacks returns a clone with its global fallback order replaced.

Every identifier must belong to this face set.

Source

pub fn add_fallback(&mut self, id: FontId)

add_fallback appends a registered font to the global fallback order.

Requested families are searched before this chain. id must belong to this face set.

Source

pub fn grown_by( &self, source: &mut dyn FontSource, demand: &FontDemand, ) -> Option<FaceSet>

grown_by returns a clone extended with answers from a font source.

Requested families are also registered under the requested name. Uncovered codepoints add matching faces to the fallback chain. It returns None when the source supplies nothing new.

Source

pub fn is_empty(&self) -> bool

is_empty reports whether no fonts are registered.

Source

pub fn len(&self) -> usize

len returns the number of registered fonts.

Source

pub fn get_arc(&self, id: FontId) -> Arc<Font>

get_arc returns a shared handle to a registered font.

§Panics

Panics if id does not belong to this face set.

Source

pub fn get(&self, id: FontId) -> &Font

get returns a registered font by identifier.

§Panics

Panics if id does not belong to this face set.

Examples found in repository?
examples/raster_bench.rs (line 49)
16fn main() {
17    let dir = concat!(env!("CARGO_MANIFEST_DIR"), "/../../assets/fonts");
18    // A CJK face is 20k+ glyphs and too big to vendor: point VALO_CJK_FONT
19    // at one to get the CJK column, otherwise the bench runs latin only.
20    let cjk_file = std::env::var("VALO_CJK_FONT").unwrap_or_default();
21    let mut fonts = FaceSet::default();
22    let latin = fonts
23        .register(
24            "Latin",
25            std::fs::read(format!("{dir}/fira_sans.ttf")).unwrap(),
26        )
27        .unwrap();
28    let cjk = std::fs::read(cjk_file)
29        .ok()
30        .and_then(|bytes| fonts.register("CJK", bytes));
31
32    let mut sets = vec![("latin", latin, glyphs(&fonts, latin, LATIN))];
33    if let Some(id) = cjk {
34        sets.push(("cjk", id, glyphs(&fonts, id, CJK)));
35    }
36
37    println!(
38        "{:>6} {:>6}  {:>12} {:>12}  (µs/glyph, n={})",
39        "set",
40        "px",
41        "alpha",
42        "sdf",
43        LATIN.len()
44    );
45    let mut raster = Rasterizer::new();
46    for (label, font, ids) in &sets {
47        for px in SIZES {
48            let alpha = time_per_glyph(ids, |g| {
49                raster.alpha(fonts.get(*font), g, px, 0.0);
50            });
51            let mut raster2 = Rasterizer::new();
52            let sdf = time_per_glyph(ids, |g| {
53                raster2.sdf(fonts.get(*font), g, px);
54            });
55            println!("{label:>6} {px:>6.0}  {alpha:>10.1}µs {sdf:>10.1}µs");
56        }
57    }
58}
59
60fn glyphs(fonts: &FaceSet, id: FontId, text: &str) -> Vec<u32> {
61    let font = fonts.get(id);
62    text.chars().filter_map(|ch| font.glyph_for(ch)).collect()
63}
Source

pub fn family(&self, name: &str) -> Option<FontId>

family returns the first registered font matching a family name or alias.

Source

pub fn faces<'a>(&'a self, name: &'a str) -> impl Iterator<Item = FontId> + 'a

faces iterates over every font matching a family name or alias.

Results follow registration order and include separate subset faces.

Source

pub fn family_variant(&self, name: &str, attrs: FontAttrs) -> Option<FontId>

family_variant returns the family face nearest to requested attributes.

Width is matched first, then italic style and weight. Registration order breaks ties.

Source

pub fn resolve(&self, families: &[String], attrs: FontAttrs, ch: char) -> FontId

resolve selects a font for a character and requested style.

It searches requested families in order, then global fallbacks. If no font covers the character, it returns a font suitable for rendering .notdef.

§Panics

Panics when the face set is empty.

Source

pub fn resolve_covered( &self, families: &[String], attrs: FontAttrs, ch: char, ) -> (FontId, bool)

resolve_covered selects a font and reports whether it covers the character.

A false flag means the returned font will render .notdef.

§Panics

Panics when the face set is empty.

Trait Implementations§

Source§

impl Clone for FaceSet

Source§

fn clone(&self) -> FaceSet

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for FaceSet

Source§

fn default() -> FaceSet

Returns the “default value” for a type. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.