Skip to content
Merged
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
37 changes: 16 additions & 21 deletions path/src/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,23 +311,31 @@ impl Rect {
///
/// Width and height are guarantee to be >= 1.
pub fn round(&self) -> Option<IntRect> {
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,
)
}

/// Converts into an `IntRect` rounding outwards.
///
/// Width and height are guarantee to be >= 1.
pub fn round_out(&self) -> Option<IntRect> {
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,
)
}

Expand Down Expand Up @@ -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
Expand Down
Binary file modified tests/images/fill/float-rect.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.