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
impl FaceSet
Sourcepub fn register(&mut self, family: &str, bytes: Vec<u8>) -> Option<FontId>
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?
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}Sourcepub fn register_with(
&mut self,
family: &str,
attrs: FontAttrs,
bytes: Vec<u8>,
) -> Option<FontId>
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.
Sourcepub fn add(&mut self, font: Font) -> FontId
pub fn add(&mut self, font: Font) -> FontId
add registers an already parsed font under its embedded names and attributes.
Sourcepub fn with_font(&self, font: Font) -> (FaceSet, FontId)
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.
Sourcepub fn with_fallbacks(&self, fallbacks: Vec<FontId>) -> FaceSet
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.
Sourcepub fn add_fallback(&mut self, id: FontId)
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.
Sourcepub fn grown_by(
&self,
source: &mut dyn FontSource,
demand: &FontDemand,
) -> Option<FaceSet>
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.
Sourcepub fn get_arc(&self, id: FontId) -> Arc<Font> ⓘ
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.
Sourcepub fn get(&self, id: FontId) -> &Font
pub fn get(&self, id: FontId) -> &Font
Examples found in repository?
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}Sourcepub fn family(&self, name: &str) -> Option<FontId>
pub fn family(&self, name: &str) -> Option<FontId>
family returns the first registered font matching a family name or alias.
Sourcepub fn faces<'a>(&'a self, name: &'a str) -> impl Iterator<Item = FontId> + 'a
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.
Sourcepub fn family_variant(&self, name: &str, attrs: FontAttrs) -> Option<FontId>
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.
Sourcepub fn resolve(&self, families: &[String], attrs: FontAttrs, ch: char) -> FontId
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.