[librsvg: 1/2] (#718): Negative rx/ry in rect element should be ignored




commit 73567dc824b3213e114111a331a85f080074dd07
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Apr 28 20:55:26 2021 -0500

    (#718): Negative rx/ry in rect element should be ignored
    
    We were setting the whole element in error; negative values for rx/ry
    should resolve to "auto", or in our case, to None.
    
    Fixes https://gitlab.gnome.org/GNOME/librsvg/-/issues/718

 src/shapes.rs                                      |  38 +++++++++++++--------
 .../reftests/bugs/718-rect-negative-rx-ry-ref.png  | Bin 0 -> 658 bytes
 .../reftests/bugs/718-rect-negative-rx-ry.svg      |  11 ++++++
 3 files changed, 35 insertions(+), 14 deletions(-)
---
diff --git a/src/shapes.rs b/src/shapes.rs
index dfc5c831..a5a16688 100644
--- a/src/shapes.rs
+++ b/src/shapes.rs
@@ -305,8 +305,8 @@ pub struct Rect {
     height: ULength<Vertical>,
 
     // Radiuses for rounded corners
-    rx: Option<ULength<Horizontal>>,
-    ry: Option<ULength<Vertical>>,
+    rx: Option<Length<Horizontal>>,
+    ry: Option<Length<Vertical>>,
 }
 
 impl_draw!(Rect);
@@ -330,6 +330,7 @@ impl SetAttributes for Rect {
 }
 
 impl BasicShape for Rect {
+    #[allow(clippy::many_single_char_names)]
     fn make_shape(&self, values: &ComputedValues, draw_ctx: &DrawingCtx) -> Shape {
         let params = draw_ctx.get_view_params();
 
@@ -338,28 +339,42 @@ impl BasicShape for Rect {
         let w = self.width.normalize(values, &params);
         let h = self.height.normalize(values, &params);
 
+        let specified_rx = self.rx.map(|l| l.normalize(values, &params));
+        let specified_ry = self.ry.map(|l| l.normalize(values, &params));
+
+        fn nonnegative_or_none(l: f64) -> Option<f64> {
+            if l < 0.0 {
+                None
+            } else {
+                Some(l)
+            }
+        }
+
+        let norm_rx = specified_rx.and_then(nonnegative_or_none);
+        let norm_ry = specified_ry.and_then(nonnegative_or_none);
+
         let mut rx;
         let mut ry;
 
-        match (self.rx, self.ry) {
+        match (norm_rx, norm_ry) {
             (None, None) => {
                 rx = 0.0;
                 ry = 0.0;
             }
 
             (Some(_rx), None) => {
-                rx = _rx.normalize(values, &params);
-                ry = _rx.normalize(values, &params);
+                rx = _rx;
+                ry = _rx;
             }
 
             (None, Some(_ry)) => {
-                rx = _ry.normalize(values, &params);
-                ry = _ry.normalize(values, &params);
+                rx = _ry;
+                ry = _ry;
             }
 
             (Some(_rx), Some(_ry)) => {
-                rx = _rx.normalize(values, &params);
-                ry = _ry.normalize(values, &params);
+                rx = _rx;
+                ry = _ry;
             }
         }
 
@@ -370,11 +385,6 @@ impl BasicShape for Rect {
             return Shape::new(Rc::new(builder.into_path()), Markers::No);
         }
 
-        // ... and rx,ry must be nonnegative
-        if rx < 0.0 || ry < 0.0 {
-            return Shape::new(Rc::new(builder.into_path()), Markers::No);
-        }
-
         let half_w = w / 2.0;
         let half_h = h / 2.0;
 
diff --git a/tests/fixtures/reftests/bugs/718-rect-negative-rx-ry-ref.png 
b/tests/fixtures/reftests/bugs/718-rect-negative-rx-ry-ref.png
new file mode 100644
index 00000000..5fd1b8cf
Binary files /dev/null and b/tests/fixtures/reftests/bugs/718-rect-negative-rx-ry-ref.png differ
diff --git a/tests/fixtures/reftests/bugs/718-rect-negative-rx-ry.svg 
b/tests/fixtures/reftests/bugs/718-rect-negative-rx-ry.svg
new file mode 100644
index 00000000..1f526d16
--- /dev/null
+++ b/tests/fixtures/reftests/bugs/718-rect-negative-rx-ry.svg
@@ -0,0 +1,11 @@
+<svg xmlns="http://www.w3.org/2000/svg"; width="300" height="100">
+<!-- Negative values for rx/ry should be ignored -->
+  <rect x="20" y="20" width="60" height="60" fill="red"/>
+  <rect x="10" y="10" width="80" height="80" rx="-10" ry="5" fill="lime"/>
+
+  <rect x="120" y="20" width="60" height="60" fill="red"/>
+  <rect x="110" y="10" width="80" height="80" rx="5" ry="-10" fill="lime"/>
+
+  <rect x="220" y="20" width="60" height="60" fill="red"/>
+  <rect x="210" y="10" width="80" height="80" rx="-5" ry="-10" fill="lime"/>
+</svg>


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