Skip to main content

Rasterizer

Struct Rasterizer 

Source
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

Source

pub fn new() -> Self

new creates an empty CPU glyph rasterizer.

Examples found in repository?
examples/raster_bench.rs (line 45)
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 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?
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}
Source

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.

Source

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?
examples/raster_bench.rs (line 53)
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 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.

Trait Implementations§

Source§

impl Default for Rasterizer

Source§

fn default() -> Rasterizer

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