@@ -95,31 +95,19 @@ pub struct Rect {
9595}
9696
9797impl Rect {
98− /// Creates a new rect, with width and height limited to keep the area under max u16.
99− /// If clipped, aspect ratio will be preserved.
98+ /// Creates a new rect, with width and height
10099 pub fn new(x: u16, y: u16, width: u16, height: u16) -> Rect {
101− let max_area = u16::max_value();
102− let (clipped_width, clipped_height) =
103− if u32::from(width) * u32::from(height) > u32::from(max_area) {
104− let aspect_ratio = f64::from(width) / f64::from(height);
105− let max_area_f = f64::from(max_area);
106− let height_f = (max_area_f / aspect_ratio).sqrt();
107− let width_f = height_f * aspect_ratio;
108− (width_f as u16, height_f as u16)
109− } else {
110− (width, height)
111− };
112100 Rect {
113101 x,
114102 y,
115− width: clipped_width,
116− height: clipped_height,
103+ width,
104+ height,
117105 }
118106 }
119107
120108 #[inline]
121− pub fn area(self) -> u16 {
122− self.width * self.height
109+ pub fn area(self) -> usize {
110+ (self.width as usize) * (self.height as usize)
123111 }
124112
125113 #[inline]
@@ -630,33 +618,6 @@ impl Style {
630618mod tests {
631619 use super::*;
632620
633− #[test]
634− fn test_rect_size_truncation() {
635− for width in 256u16..300u16 {
636− for height in 256u16..300u16 {
637− let rect = Rect::new(0, 0, width, height);
638− rect.area(); // Should not panic.
639− assert!(rect.width < width || rect.height < height);
640− // The target dimensions are rounded down so the math will not be too precise
641− // but let's make sure the ratios don't diverge crazily.
642− assert!(
643− (f64::from(rect.width) / f64::from(rect.height)
644− - f64::from(width) / f64::from(height))
645− .abs()
646− < 1.0
647− )
648− }
649− }
650−
651− // One dimension below 255, one above. Area above max u16.
652− let width = 900;
653− let height = 100;
654− let rect = Rect::new(0, 0, width, height);
655− assert_ne!(rect.width, 900);
656− assert_ne!(rect.height, 100);
657− assert!(rect.width < width || rect.height < height);
658− }
659−
660621 #[test]
661622 fn test_rect_size_preservation() {
662623 for width in 0..256u16 {