Bug 1394302 - stylo: Compute font-size calcs against appropriate base size; r?birtles draft
authorManish Goregaokar <manishearth@gmail.com>
Tue, 29 Aug 2017 13:18:24 -0700
changeset 655304 474c1711bd796f9ec0988018ec4213f844da3391
parent 654351 e2efa420beb1a578c7350ba925c82230da6b1267
child 657032 b8e5eab00ea6d2cebdb339de27a1d9b1fe6c7d91
push id76821
push userbmo:manishearth@gmail.com
push dateTue, 29 Aug 2017 20:18:42 +0000
reviewersbirtles
bugs1394302
milestone57.0a1
Bug 1394302 - stylo: Compute font-size calcs against appropriate base size; r?birtles MozReview-Commit-ID: JFC9HcA6e24
servo/components/style/properties/longhand/font.mako.rs
servo/components/style/values/computed/length.rs
servo/components/style/values/specified/length.rs
servo/components/style/values/specified/text.rs
--- a/servo/components/style/properties/longhand/font.mako.rs
+++ b/servo/components/style/properties/longhand/font.mako.rs
@@ -865,17 +865,17 @@ macro_rules! impl_gecko_keyword_conversi
                 }
                 SpecifiedValue::Length(LengthOrPercentage::Length(ref l)) => {
                     l.to_computed_value(context).into()
                 }
                 SpecifiedValue::Length(LengthOrPercentage::Percentage(pc)) => {
                     base_size.resolve(context).scale_by(pc.0).into()
                 }
                 SpecifiedValue::Length(LengthOrPercentage::Calc(ref calc)) => {
-                    let calc = calc.to_computed_value_zoomed(context);
+                    let calc = calc.to_computed_value_zoomed(context, base_size);
                     calc.to_used_value(Some(base_size.resolve(context))).unwrap().into()
                 }
                 SpecifiedValue::Keyword(ref key, fraction) => {
                     context.maybe_zoom_text(key.to_computed_value(context).scale_by(fraction))
                 }
                 SpecifiedValue::Smaller => {
                     FontRelativeLength::Em(1. / LARGER_FONT_SIZE_RATIO)
                         .to_computed_value(context, base_size).into()
--- a/servo/components/style/values/computed/length.rs
+++ b/servo/components/style/values/computed/length.rs
@@ -222,17 +222,17 @@ impl ToCss for CalcLengthOrPercentage {
         length.abs().to_css(dest)?;
 
         dest.write_str(")")
     }
 }
 
 impl specified::CalcLengthOrPercentage {
     /// Compute the value, zooming any absolute units by the zoom function.
-    fn to_computed_value_with_zoom<F>(&self, context: &Context, zoom_fn: F) -> CalcLengthOrPercentage
+    fn to_computed_value_with_zoom<F>(&self, context: &Context, zoom_fn: F, base_size: FontBaseSize) -> CalcLengthOrPercentage
         where F: Fn(Au) -> Au {
         let mut length = Au(0);
 
         if let Some(absolute) = self.absolute {
             length += zoom_fn(absolute);
         }
 
         for val in &[self.vw.map(ViewportPercentageLength::Vw),
@@ -244,38 +244,39 @@ impl specified::CalcLengthOrPercentage {
             }
         }
 
         for val in &[self.ch.map(FontRelativeLength::Ch),
                      self.em.map(FontRelativeLength::Em),
                      self.ex.map(FontRelativeLength::Ex),
                      self.rem.map(FontRelativeLength::Rem)] {
             if let Some(val) = *val {
-                length += val.to_computed_value(context, FontBaseSize::CurrentStyle);
+                length += val.to_computed_value(context, base_size);
             }
         }
 
         CalcLengthOrPercentage {
             clamping_mode: self.clamping_mode,
             length: length,
             percentage: self.percentage,
         }
     }
 
     /// Compute font-size or line-height taking into account text-zoom if necessary.
-    pub fn to_computed_value_zoomed(&self, context: &Context) -> CalcLengthOrPercentage {
-        self.to_computed_value_with_zoom(context, |abs| context.maybe_zoom_text(abs.into()).0)
+    pub fn to_computed_value_zoomed(&self, context: &Context, base_size: FontBaseSize) -> CalcLengthOrPercentage {
+        self.to_computed_value_with_zoom(context, |abs| context.maybe_zoom_text(abs.into()).0, base_size)
     }
 }
 
 impl ToComputedValue for specified::CalcLengthOrPercentage {
     type ComputedValue = CalcLengthOrPercentage;
 
     fn to_computed_value(&self, context: &Context) -> CalcLengthOrPercentage {
-        self.to_computed_value_with_zoom(context, |abs| abs)
+        // normal properties don't zoom, and compute em units against the current style's font-size
+        self.to_computed_value_with_zoom(context, |abs| abs, FontBaseSize::CurrentStyle)
     }
 
     #[inline]
     fn from_computed_value(computed: &CalcLengthOrPercentage) -> Self {
         specified::CalcLengthOrPercentage {
             clamping_mode: computed.clamping_mode,
             absolute: Some(computed.length),
             percentage: computed.percentage,
--- a/servo/components/style/values/specified/length.rs
+++ b/servo/components/style/values/specified/length.rs
@@ -75,16 +75,17 @@ impl ToCss for FontRelativeLength {
             FontRelativeLength::Ex(length) => serialize_dimension(length, "ex", dest),
             FontRelativeLength::Ch(length) => serialize_dimension(length, "ch", dest),
             FontRelativeLength::Rem(length) => serialize_dimension(length, "rem", dest)
         }
     }
 }
 
 /// A source to resolve font-relative units against
+#[derive(Clone, Copy, Debug, PartialEq)]
 pub enum FontBaseSize {
     /// Use the font-size of the current element
     CurrentStyle,
     /// Use the inherited font-size
     InheritedStyle,
     /// Use a custom base size
     Custom(Au),
 }
--- a/servo/components/style/values/specified/text.rs
+++ b/servo/components/style/values/specified/text.rs
@@ -107,17 +107,17 @@ impl ToComputedValue for LineHeight {
                     LengthOrPercentage::Percentage(ref p) => {
                         FontRelativeLength::Em(p.0)
                             .to_computed_value(
                                 context,
                                 FontBaseSize::CurrentStyle,
                             ).into()
                     }
                     LengthOrPercentage::Calc(ref calc) => {
-                        let computed_calc = calc.to_computed_value_zoomed(context);
+                        let computed_calc = calc.to_computed_value_zoomed(context, FontBaseSize::CurrentStyle);
                         let font_relative_length =
                             FontRelativeLength::Em(computed_calc.percentage())
                                 .to_computed_value(
                                     context,
                                     FontBaseSize::CurrentStyle,
                                 );
 
                         let absolute_length = computed_calc.unclamped_length();