diff --git a/examples/image_on_image.rs b/examples/image_on_image.rs index ef284cd..152cb7e 100644 --- a/examples/image_on_image.rs +++ b/examples/image_on_image.rs @@ -10,14 +10,16 @@ fn main() { let mut paint = PixmapPaint::default(); paint.quality = FilterQuality::Bicubic; - pixmap.draw_pixmap( - 20, - 20, - triangle.as_ref(), - &paint, - Transform::from_row(1.2, 0.5, 0.5, 1.2, 0.0, 0.0), - None, - ); + pixmap + .draw_pixmap( + 20, + 20, + triangle.as_ref(), + &paint, + Transform::from_row(1.2, 0.5, 0.5, 1.2, 0.0, 0.0), + None, + ) + .unwrap(); println!( "Rendered in {:.2}ms", diff --git a/path/src/size.rs b/path/src/size.rs index b314cae..e4a0430 100644 --- a/path/src/size.rs +++ b/path/src/size.rs @@ -84,8 +84,8 @@ impl IntSize { } /// Converts into [`IntRect`] at the provided position. - pub fn to_int_rect(&self, x: i32, y: i32) -> IntRect { - IntRect::from_xywh(x, y, self.width(), self.height()).unwrap() + pub fn to_int_rect(&self, x: i32, y: i32) -> Option { + IntRect::from_xywh(x, y, self.width(), self.height()) } } @@ -117,7 +117,7 @@ mod tests { let size = IntSize::from_wh(3, 4).unwrap(); assert_eq!( - size.to_int_rect(1, 2), + size.to_int_rect(1, 2).unwrap(), IntRect::from_xywh(1, 2, 3, 4).unwrap() ); } diff --git a/src/mask.rs b/src/mask.rs index 420a813..f4d6364 100644 --- a/src/mask.rs +++ b/src/mask.rs @@ -146,7 +146,7 @@ impl Mask { } pub(crate) fn submask(&self, rect: IntRect) -> Option> { - let rect = self.size.to_int_rect(0, 0).intersect(&rect)?; + let rect = self.size.to_int_rect(0, 0)?.intersect(&rect)?; let row_bytes = self.width() as usize; let offset = rect.top() as usize * row_bytes + rect.left() as usize; @@ -166,7 +166,7 @@ impl Mask { } pub(crate) fn subpixmap(&mut self, rect: IntRect) -> Option> { - let rect = self.size.to_int_rect(0, 0).intersect(&rect)?; + let rect = self.size.to_int_rect(0, 0)?.intersect(&rect)?; let row_bytes = self.width() as usize; let offset = rect.top() as usize * row_bytes + rect.left() as usize; diff --git a/src/painter.rs b/src/painter.rs index 624e00a..ca835e5 100644 --- a/src/painter.rs +++ b/src/painter.rs @@ -28,6 +28,10 @@ pub enum FillRule { EvenOdd, } +/// Invalid Size +#[derive(Debug, Copy, Clone)] +pub struct InvalidSize; + /// Controls how a shape should be painted. #[derive(Clone, PartialEq, Debug)] pub struct Paint<'a> { @@ -160,9 +164,9 @@ impl Pixmap { paint: &PixmapPaint, transform: Transform, mask: Option<&Mask>, - ) { + ) -> Result<(), InvalidSize> { self.as_mut() - .draw_pixmap(x, y, pixmap, paint, transform, mask); + .draw_pixmap(x, y, pixmap, paint, transform, mask) } /// Applies a masks. @@ -477,8 +481,12 @@ impl PixmapMut<'_> { paint: &PixmapPaint, transform: Transform, mask: Option<&Mask>, - ) { - let rect = pixmap.size().to_int_rect(x, y).to_rect(); + ) -> Result<(), InvalidSize> { + let rect = pixmap + .size() + .to_int_rect(x, y) + .ok_or(InvalidSize)? + .to_rect(); // TODO: SkSpriteBlitter // TODO: partially clipped @@ -502,6 +510,7 @@ impl PixmapMut<'_> { }; self.fill_rect(rect, &paint, transform, mask); + Ok(()) } /// Applies a masks. diff --git a/src/pixmap.rs b/src/pixmap.rs index e576ef2..22d014f 100644 --- a/src/pixmap.rs +++ b/src/pixmap.rs @@ -542,7 +542,7 @@ impl<'a> PixmapMut<'a> { /// /// Returns `None` when `Pixmap`'s rect doesn't contain `rect`. pub(crate) fn subpixmap(&mut self, rect: IntRect) -> Option> { - let rect = self.size.to_int_rect(0, 0).intersect(&rect)?; + let rect = self.size.to_int_rect(0, 0)?.intersect(&rect)?; let row_bytes = self.width() as usize * BYTES_PER_PIXEL; let offset = rect.top() as usize * row_bytes + rect.left() as usize * BYTES_PER_PIXEL; diff --git a/tests/integration/mask.rs b/tests/integration/mask.rs index 6a48ae2..2ebd1cd 100644 --- a/tests/integration/mask.rs +++ b/tests/integration/mask.rs @@ -129,7 +129,7 @@ fn skip_dest() { mask.fill_path(&clip_path, FillRule::Winding, true, Transform::default()); pixmap.draw_pixmap(0, 0, pixmap2.as_ref(), &PixmapPaint::default(), - Transform::identity(), Some(&mask)); + Transform::identity(), Some(&mask)).unwrap(); let expected = Pixmap::load_png("tests/images/mask/skip-dest.png").unwrap(); assert_eq!(pixmap, expected); diff --git a/tests/integration/pixmap.rs b/tests/integration/pixmap.rs index 1dda753..6b46feb 100644 --- a/tests/integration/pixmap.rs +++ b/tests/integration/pixmap.rs @@ -95,7 +95,7 @@ fn draw_pixmap() { paint.quality = FilterQuality::Bicubic; let mut pixmap = Pixmap::new(200, 200).unwrap(); - pixmap.draw_pixmap(20, 20, sub_pixmap.as_ref(), &paint, Transform::identity(), None); + pixmap.draw_pixmap(20, 20, sub_pixmap.as_ref(), &paint, Transform::identity(), None).unwrap(); let expected = Pixmap::load_png("tests/images/canvas/draw-pixmap.png").unwrap(); assert_eq!(pixmap, expected); @@ -130,7 +130,7 @@ fn draw_pixmap_ts() { &paint, Transform::from_row(1.2, 0.5, 0.5, 1.2, 0.0, 0.0), None, - ); + ).unwrap(); let expected = Pixmap::load_png("tests/images/canvas/draw-pixmap-ts.png").unwrap(); assert_eq!(pixmap, expected); @@ -166,7 +166,7 @@ fn draw_pixmap_opacity() { &paint, Transform::from_row(1.2, 0.5, 0.5, 1.2, 0.0, 0.0), None, - ); + ).unwrap(); let expected = Pixmap::load_png("tests/images/canvas/draw-pixmap-opacity.png").unwrap(); assert_eq!(pixmap, expected);