[librsvg: 6/8] Check for zero-sized viewBox




commit 85b613d62fbf66da12406742a7f4bd7eddae1d70
Author: Federico Mena Quintero <federico gnome org>
Date:   Fri Oct 1 13:00:32 2021 -0500

    Check for zero-sized viewBox
    
    Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/595>

 src/c_api/sizing.rs        |  8 ++++++
 tests/src/legacy_sizing.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+)
---
diff --git a/src/c_api/sizing.rs b/src/c_api/sizing.rs
index 7095fc45..adc22334 100644
--- a/src/c_api/sizing.rs
+++ b/src/c_api/sizing.rs
@@ -10,6 +10,8 @@
 //! viewport, which indicates how big the result should be.  This matches the expectations
 //! of the web platform, which assumes that all embedded content goes into a viewport.
 
+use float_cmp::approx_eq;
+
 use crate::api::{CairoRenderer, IntrinsicDimensions, Length, RenderingError};
 use crate::dpi::Dpi;
 use crate::handle::Handle;
@@ -103,6 +105,12 @@ fn size_in_pixels_from_percentage_width_and_height(
     let width = width.unwrap_or_else(|| Length::new(1.0, Percent));
     let height = height.unwrap_or_else(|| Length::new(1.0, Percent));
 
+    // Avoid division by zero below.  If the viewBox is zero-sized, there's
+    // not much we can do.
+    if approx_eq!(f64, vbox.width, 0.0) || approx_eq!(f64, vbox.height, 0.0) {
+        return Some((0.0, 0.0));
+    }
+
     match (width.unit, height.unit) {
         (Percent, Percent) => Some((vbox.width, vbox.height)),
         (_, Percent) => Some((w, w * vbox.height / vbox.width)),
diff --git a/tests/src/legacy_sizing.rs b/tests/src/legacy_sizing.rs
index 3f33f7d1..3446aba3 100644
--- a/tests/src/legacy_sizing.rs
+++ b/tests/src/legacy_sizing.rs
@@ -203,3 +203,67 @@ fn height_and_viewbox_preserves_aspect_ratio() {
         )
     );
 }
+
+#[test]
+fn zero_width_vbox() {
+    let svg = load_svg(
+        br#"<?xml version="1.0" encoding="UTF-8"?>
+<svg xmlns="http://www.w3.org/2000/svg"; width="80" viewBox="0 0 0 40">
+  <rect x="10" y="20" width="30" height="40" fill="black"/>
+</svg>
+"#,
+    )
+    .unwrap();
+
+    assert_eq!(
+        CairoRenderer::new(&svg)
+            .legacy_layer_geometry(None)
+            .unwrap(),
+        (
+            cairo::Rectangle {
+                x: 0.0,
+                y: 0.0,
+                width: 0.0,
+                height: 0.0,
+            },
+            cairo::Rectangle {
+                x: 0.0,
+                y: 0.0,
+                width: 0.0,
+                height: 0.0,
+            }
+        )
+    );
+}
+
+#[test]
+fn zero_height_vbox() {
+    let svg = load_svg(
+        br#"<?xml version="1.0" encoding="UTF-8"?>
+<svg xmlns="http://www.w3.org/2000/svg"; width="80" viewBox="0 0 30 0">
+  <rect x="10" y="20" width="30" height="40" fill="black"/>
+</svg>
+"#,
+    )
+    .unwrap();
+
+    assert_eq!(
+        CairoRenderer::new(&svg)
+            .legacy_layer_geometry(None)
+            .unwrap(),
+        (
+            cairo::Rectangle {
+                x: 0.0,
+                y: 0.0,
+                width: 0.0,
+                height: 0.0,
+            },
+            cairo::Rectangle {
+                x: 0.0,
+                y: 0.0,
+                width: 0.0,
+                height: 0.0,
+            }
+        )
+    );
+}


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