diff --git a/path/src/rect.rs b/path/src/rect.rs index d9c2799..f8d6d8c 100644 --- a/path/src/rect.rs +++ b/path/src/rect.rs @@ -311,11 +311,15 @@ impl Rect { /// /// Width and height are guarantee to be >= 1. pub fn round(&self) -> Option { + let left = i32::saturate_round(self.left()); + let top = i32::saturate_round(self.top()); + let right = i32::saturate_round(self.right()); + let bottom = i32::saturate_round(self.bottom()); IntRect::from_xywh( - i32::saturate_round(self.x()), - i32::saturate_round(self.y()), - core::cmp::max(1, i32::saturate_round(self.width()) as u32), - core::cmp::max(1, i32::saturate_round(self.height()) as u32), + left, + top, + core::cmp::max(1, right.saturating_sub(left)) as u32, + core::cmp::max(1, bottom.saturating_sub(top)) as u32, ) } @@ -323,11 +327,15 @@ impl Rect { /// /// Width and height are guarantee to be >= 1. pub fn round_out(&self) -> Option { + let left = i32::saturate_floor(self.left()); + let top = i32::saturate_floor(self.top()); + let right = i32::saturate_ceil(self.right()); + let bottom = i32::saturate_ceil(self.bottom()); IntRect::from_xywh( - i32::saturate_floor(self.x()), - i32::saturate_floor(self.y()), - core::cmp::max(1, i32::saturate_ceil(self.width()) as u32), - core::cmp::max(1, i32::saturate_ceil(self.height()) as u32), + left, + top, + core::cmp::max(1, right.saturating_sub(left)) as u32, + core::cmp::max(1, bottom.saturating_sub(top)) as u32, ) } @@ -527,19 +535,6 @@ mod rect_tests { assert_eq!(rect.height(), 20.0); } - #[test] - fn round_overflow() { - // minimum value that cause overflow - // because i32::MAX has no exact conversion to f32 - let x = 128.0; - // maximum width - let width = i32::MAX as f32; - - let rect = Rect::from_xywh(x, 0.0, width, 1.0).unwrap(); - assert_eq!(rect.round(), None); - assert_eq!(rect.round_out(), None); - } - #[test] fn transform() { // Tests based on 2x2 rectangle diff --git a/tests/images/fill/float-rect.png b/tests/images/fill/float-rect.png index ef0d551..a39fc85 100644 Binary files a/tests/images/fill/float-rect.png and b/tests/images/fill/float-rect.png differ