Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions examples/image_on_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions path/src/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
IntRect::from_xywh(x, y, self.width(), self.height())
}
}

Expand Down Expand Up @@ -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()
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/mask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl Mask {
}

pub(crate) fn submask(&self, rect: IntRect) -> Option<SubMaskRef<'_>> {
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;

Expand All @@ -166,7 +166,7 @@ impl Mask {
}

pub(crate) fn subpixmap(&mut self, rect: IntRect) -> Option<SubPixmapMut<'_>> {
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;

Expand Down
17 changes: 13 additions & 4 deletions src/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -502,6 +510,7 @@ impl PixmapMut<'_> {
};

self.fill_rect(rect, &paint, transform, mask);
Ok(())
}

/// Applies a masks.
Expand Down
2 changes: 1 addition & 1 deletion src/pixmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SubPixmapMut<'_>> {
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;

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/mask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/pixmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Loading