[librsvg: 13/17] Tests for width-only ResizeStrategy::ScaleWithMaxSize




commit b68f29dd061e405853da8b6b473ff070ddcd0580
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Feb 23 18:39:52 2022 -0600

    Tests for width-only ResizeStrategy::ScaleWithMaxSize
    
    Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/669>

 src/bin/rsvg-convert.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 49 insertions(+), 2 deletions(-)
---
diff --git a/src/bin/rsvg-convert.rs b/src/bin/rsvg-convert.rs
index 2bb590bf2..692418ab1 100644
--- a/src/bin/rsvg-convert.rs
+++ b/src/bin/rsvg-convert.rs
@@ -156,7 +156,7 @@ impl ResizeStrategy {
                         } else {
                             Size::new(max_width, max_height)
                         }
-                    },
+                    }
 
                     (Some(max_width), Some(max_height), true) => {
                         if scaled.w <= max_width && scaled.h <= max_height {
@@ -172,7 +172,24 @@ impl ResizeStrategy {
                         }
                     }
 
-                    _ => unimplemented!()
+                    (Some(max_width), None, false) => {
+                        if scaled.w <= max_width {
+                            scaled
+                        } else {
+                            Size::new(max_width, scaled.h)
+                        }
+                    }
+
+                    (Some(max_width), None, true) => {
+                        if scaled.w <= max_width {
+                            scaled
+                        } else {
+                            let factor = max_width / scaled.w;
+                            Size::new(max_width, scaled.h * factor)
+                        }
+                    }
+
+                    _ => unimplemented!(),
                 }
             }
         };
@@ -1319,4 +1336,34 @@ mod sizing_tests {
             Size::new(5.0, 15.0)
         );
     }
+
+    #[test]
+    fn scale_with_max_width_doesnt_fit_non_proportional() {
+        let strategy = ResizeStrategy::ScaleWithMaxSize {
+            scale: Scale { x: 10.0, y: 20.0 },
+            max_width: Some(10.0),
+            max_height: None,
+            keep_aspect_ratio: false,
+        };
+
+        assert_eq!(
+            strategy.apply(&Size::new(5.0, 10.0)).unwrap(),
+            Size::new(10.0, 200.0),
+        );
+    }
+
+    #[test]
+    fn scale_with_max_width_doesnt_fit_proportional() {
+        let strategy = ResizeStrategy::ScaleWithMaxSize {
+            scale: Scale { x: 10.0, y: 20.0 },
+            max_width: Some(10.0),
+            max_height: None,
+            keep_aspect_ratio: true,
+        };
+
+        assert_eq!(
+            strategy.apply(&Size::new(5.0, 10.0)).unwrap(),
+            Size::new(10.0, 40.0),
+        );
+    }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]