From cd6f68c5450451a78b8dc8a5755f178c20122629 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Mon, 18 Sep 2017 14:12:10 -0700 Subject: [PATCH] stylo: Clean up keyword values --- components/style/properties/gecko.mako.rs | 6 ++-- .../style/properties/longhand/font.mako.rs | 22 +++++++------- components/style/values/computed/font.rs | 25 ++++++++++++---- components/style/values/specified/font.rs | 29 +++++++++---------- 4 files changed, 48 insertions(+), 34 deletions(-) diff --git a/components/style/properties/gecko.mako.rs b/components/style/properties/gecko.mako.rs index a0490db4e1d2..33fe7cd5af0c 100644 --- a/components/style/properties/gecko.mako.rs +++ b/components/style/properties/gecko.mako.rs @@ -2164,7 +2164,7 @@ fn static_assert() { use values::specified::font::KeywordSize; self.gecko.mSize = v.size().0; self.gecko.mScriptUnconstrainedSize = v.size().0; - if let Some(info) = v.info { + if let Some(info) = v.keyword_info { self.gecko.mFontSizeKeyword = match info.kw { KeywordSize::XXSmall => structs::NS_STYLE_FONT_SIZE_XXSMALL, KeywordSize::XSmall => structs::NS_STYLE_FONT_SIZE_XSMALL, @@ -2385,14 +2385,14 @@ fn static_assert() { structs::NS_STYLE_FONT_SIZE_NO_KEYWORD => { return longhands::font_size::computed_value::T { size: size, - info: None, + keyword_info: None, } } _ => unreachable!("mFontSizeKeyword should be an absolute keyword or NO_KEYWORD") }; longhands::font_size::computed_value::T { size: size, - info: Some(KeywordInfo { + keyword_info: Some(KeywordInfo { kw: kw, factor: self.gecko.mFontSizeFactor, offset: Au(self.gecko.mFontSizeOffset).into() diff --git a/components/style/properties/longhand/font.mako.rs b/components/style/properties/longhand/font.mako.rs index 455ea5dd14b4..5b7b62be04a0 100644 --- a/components/style/properties/longhand/font.mako.rs +++ b/components/style/properties/longhand/font.mako.rs @@ -603,6 +603,7 @@ ${helpers.single_keyword_system("font-variant-caps", use values::specified::AllowQuirks; use values::specified::length::FontBaseSize; use values::specified::font::{FONT_MEDIUM_PX, KeywordSize}; + use values::computed::font::{KeywordInfo}; pub mod computed_value { use values::computed::font; @@ -616,17 +617,13 @@ ${helpers.single_keyword_system("font-variant-caps", pub fn get_initial_value() -> computed_value::T { computed_value::T { size: Au::from_px(FONT_MEDIUM_PX).into(), - info: Some(::values::computed::font::KeywordInfo { - kw: KeywordSize::Medium, - factor: 1., - offset: Au(0).into(), - }) + keyword_info: Some(KeywordInfo::medium()) } } #[inline] pub fn get_initial_specified_value() -> SpecifiedValue { - SpecifiedValue::Keyword(KeywordSize::Medium, 1., Au(0).into()) + SpecifiedValue::Keyword(KeywordInfo::medium()) } @@ -647,7 +644,7 @@ ${helpers.single_keyword_system("font-variant-caps", } if let Ok(kw) = input.try(KeywordSize::parse) { - return Ok(SpecifiedValue::Keyword(kw, 1., Au(0).into())) + return Ok(SpecifiedValue::Keyword(kw.into())) } try_match_ident_ignore_ascii_case! { input.expect_ident()?, @@ -670,7 +667,7 @@ ${helpers.single_keyword_system("font-variant-caps", context.builder.get_parent_font().gecko().mLanguage.raw::() || context.builder.get_font().gecko().mGenericID != context.builder.get_parent_font().gecko().mGenericID { - if let Some(info) = computed.info { + if let Some(info) = computed.keyword_info { computed.size = context.maybe_zoom_text(info.kw.to_computed_value(context) .scale_by(info.factor) + info.offset) } @@ -703,8 +700,8 @@ ${helpers.single_keyword_system("font-variant-caps", // handle mathml scriptlevel changes let kw_inherited_size = context.builder.get_parent_font() .clone_font_size() - .info.map(|info| { - context.maybe_zoom_text(SpecifiedValue::Keyword(info.kw, info.factor, info.offset) + .keyword_info.map(|info| { + context.maybe_zoom_text(SpecifiedValue::Keyword(info) .to_computed_value(context).size) }); let mut font = context.builder.take_font(); @@ -2205,7 +2202,10 @@ ${helpers.single_keyword("-moz-math-variant", let weight = longhands::font_weight::computed_value::T::from_gecko_weight(system.weight); let ret = ComputedSystemFont { font_family: longhands::font_family::computed_value::T(family), - font_size: longhands::font_size::computed_value::T { size: Au(system.size).into(), info: None }, + font_size: longhands::font_size::computed_value::T { + size: Au(system.size).into(), + keyword_info: None + }, font_weight: weight, font_size_adjust: longhands::font_size_adjust::computed_value ::T::from_gecko_adjust(system.sizeAdjust), diff --git a/components/style/values/computed/font.rs b/components/style/values/computed/font.rs index 62110779d617..5ba0a0e4953d 100644 --- a/components/style/values/computed/font.rs +++ b/components/style/values/computed/font.rs @@ -10,8 +10,8 @@ use style_traits::ToCss; use values::computed::NonNegativeLength; use values::specified::font as specified; -#[derive(Copy, Clone, PartialEq, Debug)] -#[derive(ToAnimatedValue, Animate, ToAnimatedZero, ComputeSquaredDistance)] +#[derive(Animate, ComputeSquaredDistance, ToAnimatedValue, ToAnimatedZero)] +#[derive(Clone, Copy, Debug, PartialEq)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "gecko", derive(MallocSizeOf))] /// The computed value of font-size @@ -19,11 +19,11 @@ pub struct FontSize { /// The size. pub size: NonNegativeLength, /// If derived from a keyword, the keyword and additional transformations applied to it - pub info: Option, + pub keyword_info: Option, } -#[derive(Copy, Clone, PartialEq, Debug)] -#[derive(ToAnimatedValue, Animate, ToAnimatedZero, ComputeSquaredDistance)] +#[derive(Animate, ComputeSquaredDistance, ToAnimatedValue, ToAnimatedZero)] +#[derive(Clone, Copy, Debug, PartialEq)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "gecko", derive(MallocSizeOf))] /// Additional information for keyword-derived font sizes. @@ -45,6 +45,21 @@ impl KeywordInfo { offset: self.offset.scale_by(factor) + offset, } } + + /// KeywordInfo value for font-size: medium + pub fn medium() -> Self { + specified::KeywordSize::Medium.into() + } +} + +impl From for KeywordInfo { + fn from(x: specified::KeywordSize) -> Self { + KeywordInfo { + kw: x, + factor: 1., + offset: Au(0).into(), + } + } } impl FontSize { diff --git a/components/style/values/specified/font.rs b/components/style/values/specified/font.rs index af0d5df149b6..417d29094132 100644 --- a/components/style/values/specified/font.rs +++ b/components/style/values/specified/font.rs @@ -32,7 +32,7 @@ pub enum FontSize { /// go into the ratio, and the remaining units all computed together /// will go into the offset. /// See bug 1355707. - Keyword(KeywordSize, f32, NonNegativeLength), + Keyword(computed::KeywordInfo), /// font-size: smaller Smaller, /// font-size: larger @@ -45,7 +45,7 @@ impl ToCss for FontSize { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { match *self { FontSize::Length(ref lop) => lop.to_css(dest), - FontSize::Keyword(kw, _, _) => kw.to_css(dest), + FontSize::Keyword(info) => info.kw.to_css(dest), FontSize::Smaller => dest.write_str("smaller"), FontSize::Larger => dest.write_str("larger"), FontSize::System(sys) => sys.to_css(dest), @@ -60,8 +60,8 @@ impl From for FontSize { } /// CSS font keywords +#[derive(Animate, ComputeSquaredDistance, ToAnimatedValue, ToAnimatedZero)] #[derive(Clone, Copy, Debug, PartialEq)] -#[derive(ToAnimatedValue, Animate, ToAnimatedZero, ComputeSquaredDistance)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "gecko", derive(MallocSizeOf))] #[allow(missing_docs)] @@ -226,7 +226,7 @@ impl FontSize { 6 => KeywordSize::XXLarge, // If value is greater than 7, let it be 7. _ => KeywordSize::XXXLarge, - }, 1., Au(0).into()) + }.into()) } /// Compute it against a given base font size @@ -239,7 +239,7 @@ impl FontSize { let compose_keyword = |factor| { context.style().get_parent_font() - .clone_font_size().info + .clone_font_size().keyword_info .map(|i| i.compose(factor, Au(0).into())) }; let mut info = None; @@ -274,7 +274,7 @@ impl FontSize { let parent = context.style().get_parent_font().clone_font_size(); // if we contain em/% units and the parent was keyword derived, this is too // Extract the ratio/offset and compose it - if (calc.em.is_some() || calc.percentage.is_some()) && parent.info.is_some() { + if (calc.em.is_some() || calc.percentage.is_some()) && parent.keyword_info.is_some() { let ratio = calc.em.unwrap_or(0.) + calc.percentage.map_or(0., |pc| pc.0); // Compute it, but shave off the font-relative part (em, %) // This will mean that other font-relative units like ex and ch will be computed against @@ -284,19 +284,15 @@ impl FontSize { // recomputes new ones. This is enough of an edge case to not really matter. let abs = calc.to_computed_value_zoomed(context, FontBaseSize::Custom(Au(0).into())) .length_component().into(); - info = parent.info.map(|i| i.compose(ratio, abs)); + info = parent.keyword_info.map(|i| i.compose(ratio, abs)); } let calc = calc.to_computed_value_zoomed(context, base_size); calc.to_used_value(Some(base_size.resolve(context))).unwrap().into() } - FontSize::Keyword(key, fraction, offset) => { + FontSize::Keyword(i) => { // As a specified keyword, this is keyword derived - info = Some(computed::KeywordInfo { - kw: key, - factor: fraction, - offset: offset, - }); - context.maybe_zoom_text(key.to_computed_value(context).scale_by(fraction) + offset) + info = Some(i); + context.maybe_zoom_text(i.kw.to_computed_value(context).scale_by(i.factor) + i.offset) } FontSize::Smaller => { info = compose_keyword(1. / LARGER_FONT_SIZE_RATIO); @@ -318,7 +314,10 @@ impl FontSize { } } }; - computed::FontSize { size, info } + computed::FontSize { + size: size, + keyword_info: info, + } } }