[librsvg: 3/22] Add tests for the computed value of line-height



commit f9af71ef16defe5f796967a92e351e58b554d957
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Jun 24 17:52:03 2020 -0500

    Add tests for the computed value of line-height

 rsvg_internals/src/font_props.rs | 59 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 58 insertions(+), 1 deletion(-)
---
diff --git a/rsvg_internals/src/font_props.rs b/rsvg_internals/src/font_props.rs
index 65095fa6..434a44f4 100644
--- a/rsvg_internals/src/font_props.rs
+++ b/rsvg_internals/src/font_props.rs
@@ -1,6 +1,6 @@
 //! CSS font properties.
 
-use cast::u16;
+use cast::{f64, u16};
 use cssparser::{Parser, Token};
 
 use crate::drawing_ctx::ViewParams;
@@ -245,6 +245,32 @@ pub enum LineHeightSpec {
     Percentage(f32),
 }
 
+impl LineHeightSpec {
+    pub fn value(&self) -> Length<Both> {
+        match self {
+            LineHeightSpec::Length(l) => *l,
+            _ => unreachable!(),
+        }
+    }
+
+    pub fn compute(&self, values: &ComputedValues) -> Self {
+        let font_size = values.font_size().0.value();
+
+        match *self {
+            LineHeightSpec::Normal => LineHeightSpec::Length(font_size),
+
+            LineHeightSpec::Number(f) |
+            LineHeightSpec::Percentage(f) => LineHeightSpec::Length(Length::new(font_size.length * f64(f), 
font_size.unit)),
+
+            LineHeightSpec::Length(l) => LineHeightSpec::Length(l),
+        }
+    }
+
+    pub fn normalize(&self, values: &ComputedValues, params: &ViewParams) -> f64 {
+        self.value().normalize(values, params)
+    }
+}
+
 impl Parse for LineHeightSpec {
     fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<LineHeightSpec, ParseError<'i>> {
         let state = parser.state();
@@ -503,4 +529,35 @@ mod tests {
         assert!(<LineHeightSpec as Parse>::parse_str("florp").is_err());
         assert!(<LineHeightSpec as Parse>::parse_str("3florp").is_err());
     }
+
+    #[test]
+    fn computes_line_height() {
+        let mut specified = SpecifiedValues::default();
+        specified.set_parsed_property(&ParsedProperty::FontSize(SpecifiedValue::Specified(
+            FontSize::parse_str("10px").unwrap(),
+        )));
+
+        let mut values = ComputedValues::default();
+        specified.to_computed_values(&mut values);
+
+        assert_eq!(
+            LineHeightSpec::Normal.compute(&values),
+            LineHeightSpec::Length(Length::new(10.0, LengthUnit::Px)),
+        );
+
+        assert_eq!(
+            LineHeightSpec::Number(2.0).compute(&values),
+            LineHeightSpec::Length(Length::new(20.0, LengthUnit::Px)),
+        );
+
+        assert_eq!(
+            LineHeightSpec::Length(Length::new(3.0, LengthUnit::Cm)).compute(&values),
+            LineHeightSpec::Length(Length::new(3.0, LengthUnit::Cm)),
+        );
+
+        assert_eq!(
+            LineHeightSpec::parse_str("150%").unwrap().compute(&values),
+            LineHeightSpec::Length(Length::new(15.0, LengthUnit::Px)),
+        );
+    }
 }


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