pub struct Mask { /* private fields */ }Expand description
A mask.
During drawing over Pixmap, mask’s black (0) “pixels” would block rendering
and white (255) will allow it.
Anything in between is used for gradual masking and anti-aliasing.
Unlike Skia, we’re using just a simple 8bit alpha mask. It’s way slower, but easier to implement.
Implementations§
source§impl Mask
 
impl Mask
sourcepub fn new(width: u32, height: u32) -> Option<Self>
 
pub fn new(width: u32, height: u32) -> Option<Self>
Creates a new mask by taking ownership over a mask buffer.
The size needs to match the data provided.
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
    let clip_path = {
        let mut pb = PathBuilder::new();
        pb.push_circle(250.0, 250.0, 200.0);
        pb.push_circle(250.0, 250.0, 100.0);
        pb.finish().unwrap()
    };
    let clip_path = clip_path
        .transform(Transform::from_row(1.0, -0.3, 0.0, 1.0, 0.0, 75.0))
        .unwrap();
    let mut mask = Mask::new(500, 500).unwrap();
    mask.fill_path(&clip_path, FillRule::EvenOdd, true, Transform::default());
    let mut paint = Paint::default();
    paint.anti_alias = false;
    paint.set_color_rgba8(50, 127, 150, 200);
    let mut pixmap = Pixmap::new(500, 500).unwrap();
    pixmap.fill_rect(
        Rect::from_xywh(0.0, 0.0, 500.0, 500.0).unwrap(),
        &paint,
        Transform::identity(),
        Some(&mask),
    );
    pixmap.save_png("image.png").unwrap();
}More examples
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
fn main() {
    let path1 = {
        let mut pb = PathBuilder::new();
        pb.move_to(1200.0, 1200.0);
        pb.line_to(3200.0, 18800.0);
        pb.cubic_to(7600.0, 16800.0, 13200.0, 16000.0, 18800.0, 16000.0);
        pb.cubic_to(14800.0, 9200.0, 8800.0, 3200.0, 1200.0, 1200.0);
        pb.close();
        pb.finish().unwrap()
    };
    let path2 = {
        let mut pb = PathBuilder::new();
        pb.move_to(18800.0, 1200.0);
        pb.line_to(16800.0, 18800.0);
        pb.cubic_to(12400.0, 16800.0, 6800.0, 16000.0, 1200.0, 16000.0);
        pb.cubic_to(5200.0, 9200.0, 11200.0, 3200.0, 18800.0, 1200.0);
        pb.close();
        pb.finish().unwrap()
    };
    let mut pixmap = Pixmap::new(20000, 20000).unwrap();
    let clip_path = {
        let mut pb = PathBuilder::new();
        pb.push_circle(10000.0, 10000.0, 7000.0);
        pb.finish().unwrap()
    };
    let mut mask = Mask::new(20000, 20000).unwrap();
    mask.fill_path(&clip_path, FillRule::Winding, true, Transform::default());
    let mut paint = Paint::default();
    paint.set_color_rgba8(90, 175, 100, 150);
    paint.anti_alias = true;
    let large_rect = Rect::from_xywh(500.0, 500.0, 19000.0, 19000.0).unwrap();
    pixmap.fill_rect(large_rect, &paint, Transform::identity(), None);
    paint.set_color_rgba8(50, 127, 150, 200);
    paint.anti_alias = true;
    pixmap.fill_path(
        &path1,
        &paint,
        FillRule::Winding,
        Transform::default(),
        Some(&mask),
    );
    paint.set_color_rgba8(220, 140, 75, 180);
    paint.anti_alias = false;
    pixmap.fill_path(
        &path2,
        &paint,
        FillRule::Winding,
        Transform::default(),
        None,
    );
    paint.set_color_rgba8(255, 10, 15, 180);
    paint.anti_alias = true;
    let mut stroke = Stroke::default();
    stroke.width = 0.8; // hairline
    pixmap.stroke_path(&path2, &paint, &stroke, Transform::default(), None);
    pixmap.save_png("image.png").unwrap();
}sourcepub fn from_pixmap(pixmap: PixmapRef<'_>, mask_type: MaskType) -> Self
 
pub fn from_pixmap(pixmap: PixmapRef<'_>, mask_type: MaskType) -> Self
Creates a new mask from a PixmapRef.
sourcepub fn from_vec(data: Vec<u8>, size: IntSize) -> Option<Self>
 
pub fn from_vec(data: Vec<u8>, size: IntSize) -> Option<Self>
Creates a new mask by taking ownership over a mask buffer.
The size needs to match the data provided.
sourcepub fn decode_png(data: &[u8]) -> Result<Self, DecodingError>
 
pub fn decode_png(data: &[u8]) -> Result<Self, DecodingError>
Loads a PNG file into a Mask.
Only grayscale images are supported.
sourcepub fn load_png<P: AsRef<Path>>(path: P) -> Result<Self, DecodingError>
 
pub fn load_png<P: AsRef<Path>>(path: P) -> Result<Self, DecodingError>
Loads a PNG file into a Mask.
Only grayscale images are supported.
sourcepub fn encode_png(&self) -> Result<Vec<u8>, EncodingError>
 
pub fn encode_png(&self) -> Result<Vec<u8>, EncodingError>
Encodes mask into a PNG data.
sourcepub fn save_png<P: AsRef<Path>>(&self, path: P) -> Result<(), EncodingError>
 
pub fn save_png<P: AsRef<Path>>(&self, path: P) -> Result<(), EncodingError>
Saves mask as a PNG file.
sourcepub fn fill_path(
    &mut self,
    path: &Path,
    fill_rule: FillRule,
    anti_alias: bool,
    transform: Transform
)
 
pub fn fill_path( &mut self, path: &Path, fill_rule: FillRule, anti_alias: bool, transform: Transform )
Draws a filled path onto the mask.
In terms of RGB (no alpha) image, draws a white path on top of black mask.
Doesn’t reset the existing mask content and draws the path on top of existing data.
If the above behavior is undesired, [clear()] should be called first.
This method is intended to be used for simple cases. For more complex masks
prefer [from_pixmap()].
Examples found in repository?
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
    let clip_path = {
        let mut pb = PathBuilder::new();
        pb.push_circle(250.0, 250.0, 200.0);
        pb.push_circle(250.0, 250.0, 100.0);
        pb.finish().unwrap()
    };
    let clip_path = clip_path
        .transform(Transform::from_row(1.0, -0.3, 0.0, 1.0, 0.0, 75.0))
        .unwrap();
    let mut mask = Mask::new(500, 500).unwrap();
    mask.fill_path(&clip_path, FillRule::EvenOdd, true, Transform::default());
    let mut paint = Paint::default();
    paint.anti_alias = false;
    paint.set_color_rgba8(50, 127, 150, 200);
    let mut pixmap = Pixmap::new(500, 500).unwrap();
    pixmap.fill_rect(
        Rect::from_xywh(0.0, 0.0, 500.0, 500.0).unwrap(),
        &paint,
        Transform::identity(),
        Some(&mask),
    );
    pixmap.save_png("image.png").unwrap();
}More examples
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
fn main() {
    let path1 = {
        let mut pb = PathBuilder::new();
        pb.move_to(1200.0, 1200.0);
        pb.line_to(3200.0, 18800.0);
        pb.cubic_to(7600.0, 16800.0, 13200.0, 16000.0, 18800.0, 16000.0);
        pb.cubic_to(14800.0, 9200.0, 8800.0, 3200.0, 1200.0, 1200.0);
        pb.close();
        pb.finish().unwrap()
    };
    let path2 = {
        let mut pb = PathBuilder::new();
        pb.move_to(18800.0, 1200.0);
        pb.line_to(16800.0, 18800.0);
        pb.cubic_to(12400.0, 16800.0, 6800.0, 16000.0, 1200.0, 16000.0);
        pb.cubic_to(5200.0, 9200.0, 11200.0, 3200.0, 18800.0, 1200.0);
        pb.close();
        pb.finish().unwrap()
    };
    let mut pixmap = Pixmap::new(20000, 20000).unwrap();
    let clip_path = {
        let mut pb = PathBuilder::new();
        pb.push_circle(10000.0, 10000.0, 7000.0);
        pb.finish().unwrap()
    };
    let mut mask = Mask::new(20000, 20000).unwrap();
    mask.fill_path(&clip_path, FillRule::Winding, true, Transform::default());
    let mut paint = Paint::default();
    paint.set_color_rgba8(90, 175, 100, 150);
    paint.anti_alias = true;
    let large_rect = Rect::from_xywh(500.0, 500.0, 19000.0, 19000.0).unwrap();
    pixmap.fill_rect(large_rect, &paint, Transform::identity(), None);
    paint.set_color_rgba8(50, 127, 150, 200);
    paint.anti_alias = true;
    pixmap.fill_path(
        &path1,
        &paint,
        FillRule::Winding,
        Transform::default(),
        Some(&mask),
    );
    paint.set_color_rgba8(220, 140, 75, 180);
    paint.anti_alias = false;
    pixmap.fill_path(
        &path2,
        &paint,
        FillRule::Winding,
        Transform::default(),
        None,
    );
    paint.set_color_rgba8(255, 10, 15, 180);
    paint.anti_alias = true;
    let mut stroke = Stroke::default();
    stroke.width = 0.8; // hairline
    pixmap.stroke_path(&path2, &paint, &stroke, Transform::default(), None);
    pixmap.save_png("image.png").unwrap();
}