Struct ab_glyph_rasterizer::Rasterizer
source · pub struct Rasterizer { /* private fields */ }
Expand description
Coverage rasterizer for lines, quadratic & cubic beziers.
Implementations§
source§impl Rasterizer
impl Rasterizer
sourcepub fn new(width: usize, height: usize) -> Self
pub fn new(width: usize, height: usize) -> Self
Allocates a new rasterizer that can draw onto a width
x height
alpha grid.
use ab_glyph_rasterizer::Rasterizer;
let mut rasterizer = Rasterizer::new(14, 38);
sourcepub fn reset(&mut self, width: usize, height: usize)
pub fn reset(&mut self, width: usize, height: usize)
Resets the rasterizer to an empty width
x height
alpha grid. This method behaves as if
the Rasterizer were re-created, with the advantage of not allocating if the total number of
pixels of the grid does not increase.
rasterizer.reset(12, 24);
assert_eq!(rasterizer.dimensions(), (12, 24));
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the rasterizer. This method behaves as if the Rasterizer were re-created with the same dimensions, but does not perform an allocation.
rasterizer.clear();
sourcepub fn dimensions(&self) -> (usize, usize)
pub fn dimensions(&self) -> (usize, usize)
Returns the dimensions the rasterizer was built to draw to.
let rasterizer = Rasterizer::new(9, 8);
assert_eq!((9, 8), rasterizer.dimensions());
sourcepub fn draw_line(&mut self, p0: Point, p1: Point)
pub fn draw_line(&mut self, p0: Point, p1: Point)
Adds a straight line from p0
to p1
to the outline.
rasterizer.draw_line(point(0.0, 0.48), point(1.22, 0.48));
sourcepub fn draw_quad(&mut self, p0: Point, p1: Point, p2: Point)
pub fn draw_quad(&mut self, p0: Point, p1: Point, p2: Point)
Adds a quadratic Bézier curve from p0
to p2
to the outline using p1
as the control.
rasterizer.draw_quad(point(6.2, 34.5), point(7.2, 34.5), point(9.2, 34.0));
sourcepub fn draw_cubic(&mut self, p0: Point, p1: Point, p2: Point, p3: Point)
pub fn draw_cubic(&mut self, p0: Point, p1: Point, p2: Point, p3: Point)
Adds a cubic Bézier curve from p0
to p3
to the outline using p1
as the control
at the beginning of the curve and p2
at the end of the curve.
rasterizer.draw_cubic(
point(10.3, 16.4),
point(8.6, 16.9),
point(7.7, 16.5),
point(8.2, 15.2),
);
sourcepub fn for_each_pixel<O: FnMut(usize, f32)>(&self, px_fn: O)
pub fn for_each_pixel<O: FnMut(usize, f32)>(&self, px_fn: O)
Run a callback for each pixel index
& alpha
, with indices in 0..width * height
.
An alpha
coverage value of 0.0
means the pixel is not covered at all by the glyph,
whereas a value of 1.0
(or greater) means the pixel is totally covered.
let mut pixels = vec![0u8; width * height];
rasterizer.for_each_pixel(|index, alpha| {
pixels[index] = (alpha * 255.0) as u8;
});
sourcepub fn for_each_pixel_2d<O: FnMut(u32, u32, f32)>(&self, px_fn: O)
pub fn for_each_pixel_2d<O: FnMut(u32, u32, f32)>(&self, px_fn: O)
Run a callback for each pixel x position, y position & alpha.
Convenience wrapper for Rasterizer::for_each_pixel
.
rasterizer.for_each_pixel_2d(|x, y, alpha| {
image.set_pixel(x, y, (alpha * 255.0) as u8);
});