diff options
author | Federico Mena Quintero <federico.mena@gmail.com> | 2021-07-07 01:21:55 +0000 |
---|---|---|
committer | Federico Mena Quintero <federico.mena@gmail.com> | 2021-07-07 01:21:55 +0000 |
commit | ed61932b7cd9b0338743ca57cf1db942f895999f (patch) | |
tree | 43a4c9a77157ffc7010de84964de0a639260a13d | |
parent | 4d6d8f00aac54c6c3a4c292dffc5be2dcab79a03 (diff) | |
parent | d8f3bc229f0962bc7a86cde43733822ed7bfbfae (diff) | |
download | librsvg-ed61932b7cd9b0338743ca57cf1db942f895999f.tar.gz |
Merge branch 'madds-image-auto-length' into 'master'
(#747) Add "auto" for image element width/height
See merge request GNOME/librsvg!561
-rw-r--r-- | src/image.rs | 15 | ||||
-rw-r--r-- | src/length.rs | 25 | ||||
-rw-r--r-- | 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(¶ms); let y = self.y.to_user(¶ms); - let w = self.width.to_user(¶ms); - let h = self.height.to_user(¶ms); + + let w = match self.width { + LengthOrAuto::Length(l) => l.to_user(¶ms), + LengthOrAuto::Auto => surface.width() as f64, + }; + let h = match self.height { + LengthOrAuto::Length(l) => l.to_user(¶ms), + 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>"##, +); |