[librsvg: 1/2] (#747) Add "auto" for image element width/height




commit d8f3bc229f0962bc7a86cde43733822ed7bfbfae
Author: Madds H <madds hollandart io>
Date:   Tue Jul 6 16:23:38 2021 -0500

    (#747) Add "auto" for image element width/height
    
    Includes test
    
    Fixes https://gitlab.gnome.org/GNOME/librsvg/-/issues/747

 src/image.rs           | 15 +++++++++++----
 src/length.rs          | 25 +++++++++++++++++++++++++
 tests/src/reference.rs | 16 ++++++++++++++++
 3 files changed, 52 insertions(+), 4 deletions(-)
---
diff --git a/src/image.rs b/src/image.rs
index 67cc564c..39834417 100644
--- a/src/image.rs
+++ b/src/image.rs
@@ -20,8 +20,8 @@ use crate::xml::Attributes;
 pub struct Image {
     x: Length<Horizontal>,
     y: Length<Vertical>,
-    width: ULength<Horizontal>,
-    height: ULength<Vertical>,
+    width: LengthOrAuto<Horizontal>,
+    height: LengthOrAuto<Vertical>,
     aspect: AspectRatio,
     href: Option<String>,
 }
@@ -76,8 +76,15 @@ impl Draw for Image {
 
         let x = self.x.to_user(&params);
         let y = self.y.to_user(&params);
-        let w = self.width.to_user(&params);
-        let h = self.height.to_user(&params);
+
+        let w = match self.width {
+            LengthOrAuto::Length(l) => l.to_user(&params),
+            LengthOrAuto::Auto => surface.width() as f64,
+        };
+        let h = match self.height {
+            LengthOrAuto::Length(l) => l.to_user(&params),
+            LengthOrAuto::Auto => surface.height() as f64,
+        };
 
         let is_visible = values.is_visible();
 
diff --git a/src/length.rs b/src/length.rs
index 5f42ec70..803af877 100644
--- a/src/length.rs
+++ b/src/length.rs
@@ -470,6 +470,31 @@ pub type Length<N> = CssLength<N, Signed>;
 /// Alias for `CssLength` types that are non negative
 pub type ULength<N> = CssLength<N, Unsigned>;
 
+#[derive(Debug, PartialEq, Copy, Clone)]
+pub enum LengthOrAuto<N: Normalize> {
+    Length(CssLength<N, Unsigned>),
+    Auto,
+}
+
+impl<N: Normalize> Default for LengthOrAuto<N> {
+    fn default() -> Self {
+        LengthOrAuto::Auto
+    }
+}
+
+impl<N: Normalize> Parse for LengthOrAuto<N> {
+    fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<LengthOrAuto<N>, ParseError<'i>> {
+        if parser
+            .try_parse(|i| i.expect_ident_matching("auto"))
+            .is_ok()
+        {
+            Ok(LengthOrAuto::Auto)
+        } else {
+            Ok(LengthOrAuto::Length(CssLength::parse(parser)?))
+        }
+    }
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;
diff --git a/tests/src/reference.rs b/tests/src/reference.rs
index 17e011cc..3efa3db5 100644
--- a/tests/src/reference.rs
+++ b/tests/src/reference.rs
@@ -319,3 +319,19 @@ test_compare_render_output!(
     </svg>
     "##,
 );
+
+test_compare_render_output!(
+    image_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">
+      <image
+        
href="data:;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAFElEQVQY02Nk+M+ABzAxMIxKYwIAQC0BEwZFOw4AAAAASUVORK5CYII="
+        x="10" y="10"/>
+    </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="10" height="10" fill="lime"/>
+    </svg>"##,
+);


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