[librsvg: 1/2] (#762) Support auto width/height for svg element




commit 0292db8bc9822152e9b9c780463194f39f0543f6
Author: Madds H <madds hollandart io>
Date:   Thu Jul 8 23:00:27 2021 -0500

    (#762) Support auto width/height for svg element
    
    Part-of: <https://gitlab.gnome.org/GNOME/librsvg/-/merge_requests/568>

 src/structure.rs       | 33 ++++++++++++++++++++++-----------
 tests/src/reference.rs | 16 ++++++++++++++++
 2 files changed, 38 insertions(+), 11 deletions(-)
---
diff --git a/src/structure.rs b/src/structure.rs
index aafebd23..4189f2ac 100644
--- a/src/structure.rs
+++ b/src/structure.rs
@@ -120,16 +120,26 @@ pub struct Svg {
     preserve_aspect_ratio: AspectRatio,
     x: Option<Length<Horizontal>>,
     y: Option<Length<Vertical>>,
-    width: Option<ULength<Horizontal>>,
-    height: Option<ULength<Vertical>>,
+    width: Option<LengthOrAuto<Horizontal>>,
+    height: Option<LengthOrAuto<Vertical>>,
     vbox: Option<ViewBox>,
 }
 
 impl Svg {
     pub fn get_intrinsic_dimensions(&self) -> IntrinsicDimensions {
+        let w = self.width.map(|length_or_auto| match length_or_auto {
+            LengthOrAuto::Auto => ULength::<Horizontal>::parse_str("100%").unwrap(),
+            LengthOrAuto::Length(l) => l,
+        });
+
+        let h = self.height.map(|length_or_auto| match length_or_auto {
+            LengthOrAuto::Auto => ULength::<Vertical>::parse_str("100%").unwrap(),
+            LengthOrAuto::Length(l) => l,
+        });
+
         IntrinsicDimensions {
-            width: self.width,
-            height: self.height,
+            width: w,
+            height: h,
             vbox: self.vbox,
         }
     }
@@ -148,13 +158,14 @@ impl Svg {
 
     fn get_unnormalized_size(&self) -> (ULength<Horizontal>, ULength<Vertical>) {
         // these defaults are per the spec
-        let w = self
-            .width
-            .unwrap_or_else(|| ULength::<Horizontal>::parse_str("100%").unwrap());
-        let h = self
-            .height
-            .unwrap_or_else(|| ULength::<Vertical>::parse_str("100%").unwrap());
-
+        let w = match self.width {
+            None | Some(LengthOrAuto::Auto) => ULength::<Horizontal>::parse_str("100%").unwrap(),
+            Some(LengthOrAuto::Length(l)) => l,
+        };
+        let h = match self.height {
+            None | Some(LengthOrAuto::Auto) => ULength::<Vertical>::parse_str("100%").unwrap(),
+            Some(LengthOrAuto::Length(l)) => l,
+        };
         (w, h)
     }
 
diff --git a/tests/src/reference.rs b/tests/src/reference.rs
index 658f9ccb..669b239d 100644
--- a/tests/src/reference.rs
+++ b/tests/src/reference.rs
@@ -348,3 +348,19 @@ test_compare_render_output!(
     <svg xmlns="http://www.w3.org/2000/svg"; width="30" height="30">
     </svg>"##
 );
+
+test_compare_render_output!(
+    svg_auto_width_height,
+    30,
+    30,
+    br##"<?xml version="1.0" encoding="UTF-8"?>
+    <svg xmlns="http://www.w3.org/2000/svg"; width="30" height="30">
+      <svg xmlns="http://www.w3.org/2000/svg"; width="auto" height="auto">
+        <rect x="10" y="10" width="100%" height="100%" fill="lime"/>
+      </svg>
+    </svg>"##,
+    br##"<?xml version="1.0" encoding="UTF-8"?>
+    <svg xmlns="http://www.w3.org/2000/svg"; width="30" height="30">
+      <rect x="10" y="10" width="100%" height="100%" fill="lime"/>
+    </svg>"##,
+);


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