From 4d187524c67be18cf9dcb243e819276e2b8561d0 Mon Sep 17 00:00:00 2001 From: Brian Birtles Date: Tue, 23 May 2017 15:14:18 +0900 Subject: [PATCH] Support addition of font-stretch Although there are no specific tests for this yet if any code does try to perform addition or accumulation with a font-stretch value it will likely fail the bounds check in the Into trait. To avoid that this patch provides an basic implementation of add_weighted for font-stretch that should work correctly with portions whose sum is outside the [0.0, 1.0] range. --- .../helpers/animated_properties.mako.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/components/style/properties/helpers/animated_properties.mako.rs b/components/style/properties/helpers/animated_properties.mako.rs index 11d769a1d74..3b9c2f69823 100644 --- a/components/style/properties/helpers/animated_properties.mako.rs +++ b/components/style/properties/helpers/animated_properties.mako.rs @@ -1373,11 +1373,16 @@ impl Animatable for FontWeight { /// https://drafts.csswg.org/css-fonts/#font-stretch-prop impl Animatable for FontStretch { #[inline] - fn add_weighted(&self, other: &Self, self_portion: f64, other_portion: f64) -> Result { + fn add_weighted(&self, other: &Self, self_portion: f64, other_portion: f64) + -> Result + { let from = f64::from(*self); - let to = f64::from(*other); - let interpolated_mapped_index = ((from * self_portion + to * other_portion) + 0.5).floor(); - Ok(interpolated_mapped_index.into()) + let to = f64::from(*other); + // FIXME: When `const fn` is available in release rust, make |normal|, below, const. + let normal = f64::from(FontStretch::normal); + let result = (from - normal) * self_portion + (to - normal) * other_portion + normal; + + Ok(result.into()) } #[inline] @@ -1410,11 +1415,11 @@ impl From for f64 { impl Into for f64 { fn into(self) -> FontStretch { use properties::longhands::font_stretch::computed_value::T::*; - debug_assert!(self >= 1.0 && self <= 9.0); + let index = (self + 0.5).floor().min(9.0).max(1.0); static FONT_STRETCH_ENUM_MAP: [FontStretch; 9] = [ ultra_condensed, extra_condensed, condensed, semi_condensed, normal, semi_expanded, expanded, extra_expanded, ultra_expanded ]; - FONT_STRETCH_ENUM_MAP[(self - 1.0) as usize] + FONT_STRETCH_ENUM_MAP[(index - 1.0) as usize] } }