From e73f2377cf13f007126643d61c4497dbecc3bb35 Mon Sep 17 00:00:00 2001 From: CYBAI Date: Wed, 8 Nov 2017 23:08:00 +0800 Subject: [PATCH] style: Move font-size-adjust outside of mako --- .../style/properties/longhand/font.mako.rs | 125 ++---------------- components/style/values/computed/font.rs | 55 +++++++- components/style/values/computed/mod.rs | 2 +- components/style/values/specified/font.rs | 72 +++++++++- components/style/values/specified/mod.rs | 2 +- 5 files changed, 135 insertions(+), 121 deletions(-) diff --git a/components/style/properties/longhand/font.mako.rs b/components/style/properties/longhand/font.mako.rs index c9e2a4fff48..63f38521418 100644 --- a/components/style/properties/longhand/font.mako.rs +++ b/components/style/properties/longhand/font.mako.rs @@ -633,123 +633,14 @@ ${helpers.predefined_type("font-size", flags="APPLIES_TO_FIRST_LETTER APPLIES_TO_FIRST_LINE APPLIES_TO_PLACEHOLDER", spec="https://drafts.csswg.org/css-fonts/#propdef-font-size")} -<%helpers:longhand products="gecko" name="font-size-adjust" - animation_value_type="longhands::font_size_adjust::computed_value::T" - flags="APPLIES_TO_FIRST_LETTER APPLIES_TO_FIRST_LINE APPLIES_TO_PLACEHOLDER" - spec="https://drafts.csswg.org/css-fonts/#propdef-font-size-adjust"> - use properties::longhands::system_font::SystemFont; - - - #[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToCss)] - pub enum SpecifiedValue { - None, - Number(specified::Number), - System(SystemFont), - } - - impl ToComputedValue for SpecifiedValue { - type ComputedValue = computed_value::T; - - fn to_computed_value(&self, context: &Context) -> Self::ComputedValue { - match *self { - SpecifiedValue::None => computed_value::T::None, - SpecifiedValue::Number(ref n) => computed_value::T::Number(n.to_computed_value(context)), - SpecifiedValue::System(_) => { - <%self:nongecko_unreachable> - context.cached_system_font.as_ref().unwrap().font_size_adjust - - } - } - } - - fn from_computed_value(computed: &computed_value::T) -> Self { - match *computed { - computed_value::T::None => SpecifiedValue::None, - computed_value::T::Number(ref v) => SpecifiedValue::Number(specified::Number::from_computed_value(v)), - } - } - } - - 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 - } - } - } - - pub mod computed_value { - use values::CSSFloat; - use values::animated::{ToAnimatedValue, ToAnimatedZero}; - - #[derive(Animate, Clone, ComputeSquaredDistance, Copy, Debug, MallocSizeOf, PartialEq, - ToCss)] - pub enum T { - #[animation(error)] - None, - Number(CSSFloat), - } - - impl T { - pub fn from_gecko_adjust(gecko: f32) -> Self { - if gecko == -1.0 { - T::None - } else { - T::Number(gecko) - } - } - } - - impl ToAnimatedZero for T { - #[inline] - fn to_animated_zero(&self) -> Result { Err(()) } - } - - impl ToAnimatedValue for T { - type AnimatedValue = Self; - - #[inline] - fn to_animated_value(self) -> Self { - self - } - - #[inline] - fn from_animated_value(animated: Self::AnimatedValue) -> Self { - match animated { - T::Number(number) => T::Number(number.max(0.)), - _ => animated - } - } - } - } - - #[inline] - pub fn get_initial_value() -> computed_value::T { - computed_value::T::None - } - - #[inline] - pub fn get_initial_specified_value() -> SpecifiedValue { - SpecifiedValue::None - } - - /// none | - pub fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) - -> Result> { - use values::specified::Number; - - if input.try(|input| input.expect_ident_matching("none")).is_ok() { - return Ok(SpecifiedValue::None); - } - - Ok(SpecifiedValue::Number(Number::parse_non_negative(context, input)?)) - } - +${helpers.predefined_type("font-size-adjust", + "FontSizeAdjust", + products="gecko", + initial_value="computed::FontSizeAdjust::none()", + initial_specified_value="specified::FontSizeAdjust::none()", + animation_value_type="ComputedValue", + flags="APPLIES_TO_FIRST_LETTER APPLIES_TO_FIRST_LINE APPLIES_TO_PLACEHOLDER", + spec="https://drafts.csswg.org/css-fonts/#propdef-font-size-adjust")} <%helpers:longhand products="gecko" name="font-synthesis" animation_value_type="discrete" flags="APPLIES_TO_FIRST_LETTER APPLIES_TO_FIRST_LINE APPLIES_TO_PLACEHOLDER" diff --git a/components/style/values/computed/font.rs b/components/style/values/computed/font.rs index 7758ab9ab50..37ae58b8eb0 100644 --- a/components/style/values/computed/font.rs +++ b/components/style/values/computed/font.rs @@ -7,7 +7,8 @@ use app_units::Au; use std::fmt; use style_traits::ToCss; -use values::animated::ToAnimatedValue; +use values::CSSFloat; +use values::animated::{ToAnimatedValue, ToAnimatedZero}; use values::computed::{Context, NonNegativeLength, ToComputedValue}; use values::specified::font as specified; use values::specified::length::{FontBaseSize, NoCalcLength}; @@ -212,6 +213,58 @@ impl ToAnimatedValue for FontSize { } } +#[derive(Animate, Clone, ComputeSquaredDistance, Copy, Debug, MallocSizeOf, PartialEq, ToCss)] +/// Preserve the readability of text when font fallback occurs +pub enum FontSizeAdjust { + #[animation(error)] + /// None variant + None, + /// Number variant + Number(CSSFloat), +} + +impl FontSizeAdjust { + #[inline] + /// Default value of font-size-adjust + pub fn none() -> Self { + FontSizeAdjust::None + } + + /// Get font-size-adjust with float number + pub fn from_gecko_adjust(gecko: f32) -> Self { + if gecko == -1.0 { + FontSizeAdjust::None + } else { + FontSizeAdjust::Number(gecko) + } + } +} + +impl ToAnimatedZero for FontSizeAdjust { + #[inline] + // FIXME(emilio): why? + fn to_animated_zero(&self) -> Result { + Err(()) + } +} + +impl ToAnimatedValue for FontSizeAdjust { + type AnimatedValue = Self; + + #[inline] + fn to_animated_value(self) -> Self { + self + } + + #[inline] + fn from_animated_value(animated: Self::AnimatedValue) -> Self { + match animated { + FontSizeAdjust::Number(number) => FontSizeAdjust::Number(number.max(0.)), + _ => animated + } + } +} + impl ToComputedValue for specified::MozScriptMinSize { type ComputedValue = MozScriptMinSize; diff --git a/components/style/values/computed/mod.rs b/components/style/values/computed/mod.rs index 1f46f7791a1..65eb27eea0c 100644 --- a/components/style/values/computed/mod.rs +++ b/components/style/values/computed/mod.rs @@ -36,7 +36,7 @@ pub use self::angle::Angle; pub use self::background::{BackgroundSize, BackgroundRepeat}; pub use self::border::{BorderImageSlice, BorderImageWidth, BorderImageSideWidth}; pub use self::border::{BorderRadius, BorderCornerRadius, BorderSpacing}; -pub use self::font::{FontSize, FontWeight, MozScriptLevel, MozScriptMinSize, XTextZoom}; +pub use self::font::{FontSize, FontSizeAdjust, FontWeight, 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 45b97ee9a5d..571a1841c58 100644 --- a/components/style/values/specified/font.rs +++ b/components/style/values/specified/font.rs @@ -13,7 +13,7 @@ use properties::longhands::system_font::SystemFont; use std::fmt; use style_traits::{ToCss, StyleParseErrorKind, ParseError}; use values::computed::{font as computed, Context, Length, NonNegativeLength, ToComputedValue}; -use values::specified::{AllowQuirks, LengthOrPercentage, NoCalcLength}; +use values::specified::{AllowQuirks, LengthOrPercentage, NoCalcLength, Number}; use values::specified::length::{AU_PER_PT, AU_PER_PX, FontBaseSize}; const DEFAULT_SCRIPT_MIN_SIZE_PT: u32 = 8; @@ -151,6 +151,76 @@ impl From for FontSize { } } +#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToCss)] +/// Preserve the readability of text when font fallback occurs +pub enum FontSizeAdjust { + /// None variant + None, + /// Number variant + Number(Number), + /// system font + System(SystemFont), +} + +impl FontSizeAdjust { + #[inline] + /// Default value of font-size-adjust + pub fn none() -> Self { + FontSizeAdjust::None + } + + /// Get font-size-adjust with SystemFont + pub fn system_font(f: SystemFont) -> Self { + FontSizeAdjust::System(f) + } + + /// Get SystemFont variant + pub fn get_system(&self) -> Option { + if let FontSizeAdjust::System(s) = *self { + Some(s) + } else { + None + } + } +} + +impl ToComputedValue for FontSizeAdjust { + type ComputedValue = computed::FontSizeAdjust; + + fn to_computed_value(&self, context: &Context) -> Self::ComputedValue { + match *self { + FontSizeAdjust::None => computed::FontSizeAdjust::None, + FontSizeAdjust::Number(ref n) => computed::FontSizeAdjust::Number(n.to_computed_value(context)), + FontSizeAdjust::System(_) => { + #[cfg(feature = "gecko")] { + context.cached_system_font.as_ref().unwrap().font_size_adjust + } + #[cfg(feature = "servo")] { + unreachable!() + } + } + } + } + + fn from_computed_value(computed: &computed::FontSizeAdjust) -> Self { + match *computed { + computed::FontSizeAdjust::None => FontSizeAdjust::None, + computed::FontSizeAdjust::Number(ref v) => FontSizeAdjust::Number(Number::from_computed_value(v)), + } + } +} + +impl Parse for FontSizeAdjust { + /// none | + fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result> { + if input.try(|input| input.expect_ident_matching("none")).is_ok() { + return Ok(FontSizeAdjust::None); + } + + Ok(FontSizeAdjust::Number(Number::parse_non_negative(context, input)?)) + } +} + /// CSS font keywords #[derive(Animate, ComputeSquaredDistance, MallocSizeOf, ToAnimatedValue, ToAnimatedZero)] #[derive(Clone, Copy, Debug, PartialEq)] diff --git a/components/style/values/specified/mod.rs b/components/style/values/specified/mod.rs index 7cab5c17248..e6d1d2f9d56 100644 --- a/components/style/values/specified/mod.rs +++ b/components/style/values/specified/mod.rs @@ -30,7 +30,7 @@ pub use self::align::{AlignItems, AlignJustifyContent, AlignJustifySelf, Justify pub use self::background::{BackgroundRepeat, BackgroundSize}; pub use self::border::{BorderCornerRadius, BorderImageSlice, BorderImageWidth}; pub use self::border::{BorderImageSideWidth, BorderRadius, BorderSideWidth, BorderSpacing}; -pub use self::font::{FontSize, FontWeight, MozScriptLevel, MozScriptMinSize, XTextZoom}; +pub use self::font::{FontSize, FontSizeAdjust, FontWeight, 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};