pub struct Rasterizer { /* private fields */ }
Expand description

Coverage rasterizer for lines, quadratic & cubic beziers.

Implementations§

source§

impl Rasterizer

source

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);
source

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));
source

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();
source

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());
source

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));
source

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));
source

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),
);
source

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;
});
source

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);
});

Trait Implementations§

source§

impl Debug for Rasterizer

let rasterizer = ab_glyph_rasterizer::Rasterizer::new(3, 4);
assert_eq!(
    &format!("{:?}", rasterizer),
    "Rasterizer { width: 3, height: 4 }"
);
source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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>,

§

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

§

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.