pub struct Rasterizer { /* private fields */ }Expand description
Rasterizer converts font glyphs into CPU bitmap or distance-field images.
Valo’s renderer owns one internally. Hosts need this type only when building a custom glyph cache or text renderer. Reuse an instance to retain scaling and stroking scratch state; its methods require mutable access.
Implementations§
Source§impl Rasterizer
impl Rasterizer
Sourcepub fn new() -> Self
pub fn new() -> Self
new creates an empty CPU glyph rasterizer.
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 alpha(
&mut self,
font: &Font,
glyph: u32,
px: f32,
dx: f32,
) -> Option<GlyphImage>
pub fn alpha( &mut self, font: &Font, glyph: u32, px: f32, dx: f32, ) -> Option<GlyphImage>
alpha rasterizes a glyph into one-byte alpha coverage.
px is the font size in raster pixels. dx shifts the outline
horizontally for subpixel positioning and is usually one of
0.0, 0.25, 0.5, or 0.75. It returns None when the glyph has no
rasterizable monochrome outline.
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 stroked(
&mut self,
font: &Font,
glyph: u32,
px: f32,
dx: f32,
stroke: &GlyphStroke,
) -> Option<GlyphImage>
pub fn stroked( &mut self, font: &Font, glyph: u32, px: f32, dx: f32, stroke: &GlyphStroke, ) -> Option<GlyphImage>
stroked rasterizes a stroked glyph outline into one-byte alpha coverage.
px, dx, and stroke dimensions are in raster pixels. It returns
None when the glyph has no outline or the stroked path cannot be built.
Sourcepub fn sdf(&mut self, font: &Font, glyph: u32, px: f32) -> Option<GlyphImage>
pub fn sdf(&mut self, font: &Font, glyph: u32, px: f32) -> Option<GlyphImage>
sdf rasterizes a glyph into a one-byte signed distance field.
px is the font size in raster pixels. A value near 128 marks the edge;
larger values are inside and smaller values are outside. The image is
padded by SDF_PAD pixels. It returns None when no monochrome
outline can be rasterized.
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 color(&mut self, font: &Font, glyph: u32, px: f32) -> Option<GlyphImage>
pub fn color(&mut self, font: &Font, glyph: u32, px: f32) -> Option<GlyphImage>
color rasterizes a color glyph into premultiplied RGBA8 pixels.
px is the font size in raster pixels. It supports color outlines and
embedded color bitmaps. It returns None when the glyph has no supported
color representation, allowing callers to fall back to alpha rendering.