From 70033edccd2d3acec00bbef0446aba483bdc0288 Mon Sep 17 00:00:00 2001 From: CYBAI Date: Mon, 20 Nov 2017 11:26:09 +0800 Subject: [PATCH] style: Move font-feature-settings outside of mako --- .../style/properties/longhand/font.mako.rs | 83 +++---------------- components/style/values/computed/font.rs | 12 +++ components/style/values/computed/mod.rs | 3 +- components/style/values/specified/font.rs | 65 +++++++++++++++ components/style/values/specified/mod.rs | 3 +- 5 files changed, 91 insertions(+), 75 deletions(-) diff --git a/components/style/properties/longhand/font.mako.rs b/components/style/properties/longhand/font.mako.rs index c0dda84c28b..585d6510aef 100644 --- a/components/style/properties/longhand/font.mako.rs +++ b/components/style/properties/longhand/font.mako.rs @@ -15,42 +15,6 @@ %endif -// Define ToComputedValue, ToCss, and other boilerplate for a specified value -// which is of the form `enum SpecifiedValue {Value(..), System(SystemFont)}` -<%def name="simple_system_boilerplate(name)"> - impl SpecifiedValue { - pub fn system_font(f: SystemFont) -> Self { - SpecifiedValue::System(f) - } - pub fn get_system(&self) -> Option { - if let SpecifiedValue::System(s) = *self { - Some(s) - } else { - None - } - } - } - - impl ToComputedValue for SpecifiedValue { - type ComputedValue = computed_value::T; - - fn to_computed_value(&self, _context: &Context) -> computed_value::T { - match *self { - SpecifiedValue::Value(ref v) => v.clone(), - SpecifiedValue::System(_) => { - <%self:nongecko_unreachable> - _context.cached_system_font.as_ref().unwrap().${name}.clone() - - } - } - } - - fn from_computed_value(other: &computed_value::T) -> Self { - SpecifiedValue::Value(other.clone()) - } - } - - <%helpers:longhand name="font-family" animation_value_type="discrete" flags="APPLIES_TO_FIRST_LETTER APPLIES_TO_FIRST_LINE APPLIES_TO_PLACEHOLDER" spec="https://drafts.csswg.org/css-fonts/#propdef-font-family"> @@ -698,43 +662,16 @@ ${helpers.single_keyword_system("font-variant-position", flags="APPLIES_TO_FIRST_LETTER APPLIES_TO_FIRST_LINE APPLIES_TO_PLACEHOLDER", animation_value_type="discrete")} -<%helpers:longhand name="font-feature-settings" products="gecko" animation_value_type="discrete" - extra_prefixes="moz" boxed="True" - flags="APPLIES_TO_FIRST_LETTER APPLIES_TO_FIRST_LINE APPLIES_TO_PLACEHOLDER" - spec="https://drafts.csswg.org/css-fonts/#propdef-font-feature-settings"> - use properties::longhands::system_font::SystemFont; - use values::generics::FontSettings; - - #[cfg_attr(feature = "gecko", derive(MallocSizeOf))] - #[derive(Clone, Debug, PartialEq, ToCss)] - pub enum SpecifiedValue { - Value(computed_value::T), - System(SystemFont) - } - - <%self:simple_system_boilerplate name="font_feature_settings"> - - pub mod computed_value { - use values::generics::{FontSettings, FontSettingTagInt}; - pub type T = FontSettings; - } - - #[inline] - pub fn get_initial_value() -> computed_value::T { - FontSettings::Normal - } - - #[inline] - pub fn get_initial_specified_value() -> SpecifiedValue { - SpecifiedValue::Value(FontSettings::Normal) - } - - /// normal | # - pub fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) - -> Result> { - computed_value::T::parse(context, input).map(SpecifiedValue::Value) - } - +${helpers.predefined_type("font-feature-settings", + "FontFeatureSettings", + products="gecko", + initial_value="computed::FontFeatureSettings::normal()", + initial_specified_value="specified::FontFeatureSettings::normal()", + extra_prefixes="moz", + boxed=True, + animation_value_type="discrete", + flags="APPLIES_TO_FIRST_LETTER APPLIES_TO_FIRST_LINE APPLIES_TO_PLACEHOLDER", + spec="https://drafts.csswg.org/css-fonts/#propdef-font-feature-settings")} <% # This spec link is too long to fit elsewhere diff --git a/components/style/values/computed/font.rs b/components/style/values/computed/font.rs index 5ea93352a75..ed2b7c9f881 100644 --- a/components/style/values/computed/font.rs +++ b/components/style/values/computed/font.rs @@ -11,6 +11,7 @@ use style_traits::ToCss; use values::CSSFloat; use values::animated::{ToAnimatedValue, ToAnimatedZero}; use values::computed::{Context, NonNegativeLength, ToComputedValue}; +use values::generics::{FontSettings, FontSettingTagInt}; use values::specified::font as specified; use values::specified::length::{FontBaseSize, NoCalcLength}; @@ -286,6 +287,17 @@ pub type FontVariantLigatures = specified::VariantLigatures; /// Use VariantNumeric as computed type of FontVariantNumeric pub type FontVariantNumeric = specified::VariantNumeric; +/// Use FontSettings as computed type of FontFeatureSettings +pub type FontFeatureSettings = FontSettings; + +impl FontFeatureSettings { + #[inline] + /// Default value of `font-feature-settings` as `normal` + pub fn normal() -> FontFeatureSettings { + FontSettings::Normal + } +} + /// font-language-override can only have a single three-letter /// OpenType "language system" tag, so we should be able to compute /// it and store it as a 32-bit integer diff --git a/components/style/values/computed/mod.rs b/components/style/values/computed/mod.rs index 95e03c8b724..941755700b9 100644 --- a/components/style/values/computed/mod.rs +++ b/components/style/values/computed/mod.rs @@ -38,7 +38,8 @@ pub use self::border::{BorderImageSlice, BorderImageWidth, BorderImageSideWidth} pub use self::border::{BorderRadius, BorderCornerRadius, BorderSpacing}; pub use self::font::{FontSize, FontSizeAdjust, FontSynthesis, FontWeight, FontVariantAlternates}; pub use self::font::{FontLanguageOverride, FontVariantSettings, FontVariantEastAsian}; -pub use self::font::{FontVariantLigatures, FontVariantNumeric, MozScriptLevel, MozScriptMinSize, XTextZoom}; +pub use self::font::{FontVariantLigatures, FontVariantNumeric, FontFeatureSettings}; +pub use self::font::{MozScriptLevel, MozScriptMinSize, XTextZoom}; pub use self::box_::{AnimationIterationCount, AnimationName, ScrollSnapType, VerticalAlign}; pub use self::color::{Color, ColorPropertyValue, RGBAColor}; pub use self::effects::{BoxShadow, Filter, SimpleShadow}; diff --git a/components/style/values/specified/font.rs b/components/style/values/specified/font.rs index 56e4b88b17e..e0720298595 100644 --- a/components/style/values/specified/font.rs +++ b/components/style/values/specified/font.rs @@ -1573,6 +1573,71 @@ impl Parse for FontVariantNumeric { } } +#[cfg_attr(feature = "gecko", derive(MallocSizeOf))] +#[derive(Clone, Debug, PartialEq, ToCss)] +/// Define initial settings that apply when the font defined +/// by an @font-face rule is rendered. +pub enum FontFeatureSettings { + /// Value of `FontSettings` + Value(computed::FontFeatureSettings), + /// System font + System(SystemFont) +} + +impl FontFeatureSettings { + #[inline] + /// Get default value of `font-feature-settings` as normal + pub fn normal() -> FontFeatureSettings { + FontFeatureSettings::Value(FontSettings::Normal) + } + + /// Get `font-feature-settings` with system font + pub fn system_font(f: SystemFont) -> Self { + FontFeatureSettings::System(f) + } + + /// Get system font + pub fn get_system(&self) -> Option { + if let FontFeatureSettings::System(s) = *self { + Some(s) + } else { + None + } + } +} + +impl ToComputedValue for FontFeatureSettings { + type ComputedValue = computed::FontFeatureSettings; + + fn to_computed_value(&self, _context: &Context) -> computed::FontFeatureSettings { + match *self { + FontFeatureSettings::Value(ref v) => v.clone(), + FontFeatureSettings::System(_) => { + #[cfg(feature = "gecko")] { + _context.cached_system_font.as_ref().unwrap().font_feature_settings.clone() + } + #[cfg(feature = "servo")] { + unreachable!() + } + } + } + } + + fn from_computed_value(other: &computed::FontFeatureSettings) -> Self { + FontFeatureSettings::Value(other.clone()) + } +} + +impl Parse for FontFeatureSettings { + /// normal | # + fn parse<'i, 't>( + context: &ParserContext, + input: &mut Parser<'i, 't> + ) -> Result> { + computed::FontFeatureSettings::parse(context, input).map(FontFeatureSettings::Value) + } +} + #[derive(Clone, Debug, MallocSizeOf, PartialEq, ToComputedValue)] /// Whether user agents are allowed to synthesize bold or oblique font faces /// when a font family lacks bold or italic faces diff --git a/components/style/values/specified/mod.rs b/components/style/values/specified/mod.rs index f9d700f7391..ffc70bef2a1 100644 --- a/components/style/values/specified/mod.rs +++ b/components/style/values/specified/mod.rs @@ -32,7 +32,8 @@ pub use self::border::{BorderCornerRadius, BorderImageSlice, BorderImageWidth}; pub use self::border::{BorderImageSideWidth, BorderRadius, BorderSideWidth, BorderSpacing}; pub use self::font::{FontSize, FontSizeAdjust, FontSynthesis, FontWeight, FontVariantAlternates}; pub use self::font::{FontLanguageOverride, FontVariantSettings, FontVariantEastAsian}; -pub use self::font::{FontVariantLigatures, FontVariantNumeric, MozScriptLevel, MozScriptMinSize, XTextZoom}; +pub use self::font::{FontVariantLigatures, FontVariantNumeric, FontFeatureSettings}; +pub use self::font::{MozScriptLevel, MozScriptMinSize, XTextZoom}; pub use self::box_::{AnimationIterationCount, AnimationName, ScrollSnapType, VerticalAlign}; pub use self::color::{Color, ColorPropertyValue, RGBAColor}; pub use self::effects::{BoxShadow, Filter, SimpleShadow};