style: Use a generic type in preparation to fix animation.

Apart from a bit more code reuse.

Bug: 1455358
Reviewed-by: xidorn
MozReview-Commit-ID: 2BNOK6v30lX
This commit is contained in:
Emilio Cobos Álvarez 2018-04-19 21:23:31 +02:00
parent 32d4da8a99
commit 737501153b
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
9 changed files with 158 additions and 120 deletions

View file

@ -25,7 +25,8 @@ use values::computed::font::FamilyName;
#[cfg(feature = "gecko")] #[cfg(feature = "gecko")]
use values::specified::font::{SpecifiedFontFeatureSettings, SpecifiedFontVariationSettings}; use values::specified::font::{SpecifiedFontFeatureSettings, SpecifiedFontVariationSettings};
use values::specified::font::{AbsoluteFontWeight, FontStretch as SpecifiedFontStretch}; use values::specified::font::{AbsoluteFontWeight, FontStretch as SpecifiedFontStretch};
use values::specified::font::FontStyle as SpecifiedFontStyle; use values::specified::font::SpecifiedFontStyle;
use values::generics::font::FontStyle as GenericFontStyle;
use values::specified::Angle; use values::specified::Angle;
use values::specified::url::SpecifiedUrl; use values::specified::url::SpecifiedUrl;
@ -145,16 +146,15 @@ impl Parse for FontStyle {
) -> Result<Self, ParseError<'i>> { ) -> Result<Self, ParseError<'i>> {
let style = SpecifiedFontStyle::parse(context, input)?; let style = SpecifiedFontStyle::parse(context, input)?;
Ok(match style { Ok(match style {
SpecifiedFontStyle::Normal => FontStyle::Normal, GenericFontStyle::Normal => FontStyle::Normal,
SpecifiedFontStyle::Italic => FontStyle::Italic, GenericFontStyle::Italic => FontStyle::Italic,
SpecifiedFontStyle::Oblique(angle) => { GenericFontStyle::Oblique(angle) => {
let second_angle = input.try(|input| { let second_angle = input.try(|input| {
SpecifiedFontStyle::parse_angle(context, input) SpecifiedFontStyle::parse_angle(context, input)
}).unwrap_or_else(|_| angle.clone()); }).unwrap_or_else(|_| angle.clone());
FontStyle::Oblique(angle, second_angle) FontStyle::Oblique(angle, second_angle)
} }
SpecifiedFontStyle::System(..) => unreachable!(),
}) })
} }
} }

View file

@ -16,7 +16,7 @@ use values::computed::font::FamilyName;
use values::generics::font::FontTag; use values::generics::font::FontTag;
use values::specified::font::{SpecifiedFontFeatureSettings, SpecifiedFontVariationSettings}; use values::specified::font::{SpecifiedFontFeatureSettings, SpecifiedFontVariationSettings};
use values::specified::font::{AbsoluteFontWeight, FontStretch as SpecifiedFontStretch}; use values::specified::font::{AbsoluteFontWeight, FontStretch as SpecifiedFontStretch};
use values::specified::font::FontStyle as SpecifiedFontStyle; use values::specified::font::SpecifiedFontStyle;
impl<'a> ToNsCssValue for &'a FamilyName { impl<'a> ToNsCssValue for &'a FamilyName {
fn convert(self, nscssvalue: &mut nsCSSValue) { fn convert(self, nscssvalue: &mut nsCSSValue) {

View file

@ -2625,7 +2625,7 @@ fn static_assert() {
} }
pub fn set_font_style(&mut self, v: longhands::font_style::computed_value::T) { pub fn set_font_style(&mut self, v: longhands::font_style::computed_value::T) {
use values::computed::font::FontStyle; use values::generics::font::FontStyle;
self.gecko.mFont.style = match v { self.gecko.mFont.style = match v {
FontStyle::Normal => structs::NS_STYLE_FONT_STYLE_NORMAL, FontStyle::Normal => structs::NS_STYLE_FONT_STYLE_NORMAL,
FontStyle::Italic => structs::NS_STYLE_FONT_STYLE_ITALIC, FontStyle::Italic => structs::NS_STYLE_FONT_STYLE_ITALIC,

View file

@ -18,8 +18,8 @@ ${helpers.predefined_type("font-family",
${helpers.predefined_type( ${helpers.predefined_type(
"font-style", "font-style",
"FontStyle", "FontStyle",
initial_value="computed::FontStyle::Normal", initial_value="computed::FontStyle::normal()",
initial_specified_value="specified::FontStyle::Normal", initial_specified_value="specified::FontStyle::normal()",
# FIXME(emilio): This won't handle clamping correctly. # FIXME(emilio): This won't handle clamping correctly.
animation_value_type="ComputedValue", animation_value_type="ComputedValue",
flags="APPLIES_TO_FIRST_LETTER APPLIES_TO_FIRST_LINE APPLIES_TO_PLACEHOLDER", flags="APPLIES_TO_FIRST_LETTER APPLIES_TO_FIRST_LINE APPLIES_TO_PLACEHOLDER",

View file

@ -359,8 +359,8 @@ impl<'a, 'b: 'a> StyleAdjuster<'a, 'b> {
#[cfg(feature = "gecko")] #[cfg(feature = "gecko")]
fn adjust_for_mathvariant(&mut self) { fn adjust_for_mathvariant(&mut self) {
use properties::longhands::_moz_math_variant::computed_value::T as MozMathVariant; use properties::longhands::_moz_math_variant::computed_value::T as MozMathVariant;
use properties::longhands::font_style::computed_value::T as FontStyle;
use properties::longhands::font_weight::computed_value::T as FontWeight; use properties::longhands::font_weight::computed_value::T as FontWeight;
use values::generics::font::FontStyle;
if self.style.get_font().clone__moz_math_variant() != MozMathVariant::None { if self.style.get_font().clone__moz_math_variant() != MozMathVariant::None {
let font_style = self.style.mutate_font(); let font_style = self.style.mutate_font();
font_style.set_font_weight(FontWeight::normal()); font_style.set_font_weight(FontWeight::normal());

View file

@ -22,8 +22,7 @@ use style_traits::{CssWriter, ParseError, ToCss};
use values::CSSFloat; use values::CSSFloat;
use values::animated::{ToAnimatedValue, ToAnimatedZero}; use values::animated::{ToAnimatedValue, ToAnimatedZero};
use values::computed::{Angle, Context, Integer, NonNegativeLength, Number, ToComputedValue}; use values::computed::{Angle, Context, Integer, NonNegativeLength, Number, ToComputedValue};
use values::generics::font::{FeatureTagValue, FontSettings}; use values::generics::font::{self as generics, FeatureTagValue, FontSettings, VariationValue};
use values::generics::font::{KeywordInfo as GenericKeywordInfo, VariationValue};
use values::specified::font::{self as specified, MIN_FONT_WEIGHT, MAX_FONT_WEIGHT}; use values::specified::font::{self as specified, MIN_FONT_WEIGHT, MAX_FONT_WEIGHT};
use values::specified::length::{FontBaseSize, NoCalcLength}; use values::specified::length::{FontBaseSize, NoCalcLength};
@ -67,7 +66,7 @@ pub struct FontSize {
} }
/// Additional information for computed keyword-derived font sizes. /// Additional information for computed keyword-derived font sizes.
pub type KeywordInfo = GenericKeywordInfo<NonNegativeLength>; pub type KeywordInfo = generics::KeywordInfo<NonNegativeLength>;
impl FontWeight { impl FontWeight {
/// Value for normal /// Value for normal
@ -840,30 +839,22 @@ impl ToComputedValue for specified::MozScriptLevel {
} }
/// The computed value of `font-style`. /// The computed value of `font-style`.
#[derive(Animate, Clone, ComputeSquaredDistance, Copy, Debug, MallocSizeOf, ///
PartialEq)] /// FIXME(emilio): Angle should be a custom type to handle clamping during
#[allow(missing_docs)] /// animation.
pub enum FontStyle { pub type FontStyle = generics::FontStyle<Angle>;
#[animation(error)]
Normal,
#[animation(error)]
Italic,
// FIXME(emilio): This needs to clamp really.
Oblique(Angle),
}
impl ToAnimatedZero for FontStyle {
#[inline]
fn to_animated_zero(&self) -> Result<Self, ()> {
use num_traits::Zero;
Ok(FontStyle::Oblique(Angle::zero()))
}
}
impl FontStyle { impl FontStyle {
/// The `normal` value.
#[inline]
pub fn normal() -> Self {
generics::FontStyle::Normal
}
/// The default angle for font-style: oblique. This is 20deg per spec: /// The default angle for font-style: oblique. This is 20deg per spec:
/// ///
/// https://drafts.csswg.org/css-fonts-4/#valdef-font-style-oblique-angle /// https://drafts.csswg.org/css-fonts-4/#valdef-font-style-oblique-angle
#[inline]
pub fn default_angle() -> Angle { pub fn default_angle() -> Angle {
Angle::Deg(specified::DEFAULT_FONT_STYLE_OBLIQUE_ANGLE_DEGREES) Angle::Deg(specified::DEFAULT_FONT_STYLE_OBLIQUE_ANGLE_DEGREES)
} }
@ -875,10 +866,10 @@ impl FontStyle {
use gecko_bindings::structs; use gecko_bindings::structs;
match kw as u32 { match kw as u32 {
structs::NS_STYLE_FONT_STYLE_NORMAL => FontStyle::Normal, structs::NS_STYLE_FONT_STYLE_NORMAL => generics::FontStyle::Normal,
structs::NS_STYLE_FONT_STYLE_ITALIC => FontStyle::Italic, structs::NS_STYLE_FONT_STYLE_ITALIC => generics::FontStyle::Italic,
// FIXME(emilio): Grab the angle when we honor it :) // FIXME(emilio): Grab the angle when we honor it :)
structs::NS_STYLE_FONT_STYLE_OBLIQUE => FontStyle::Oblique(Self::default_angle()), structs::NS_STYLE_FONT_STYLE_OBLIQUE => generics::FontStyle::Oblique(Self::default_angle()),
_ => unreachable!("Unknown font style"), _ => unreachable!("Unknown font style"),
} }
} }
@ -890,9 +881,9 @@ impl ToCss for FontStyle {
W: fmt::Write, W: fmt::Write,
{ {
match *self { match *self {
FontStyle::Normal => dest.write_str("normal"), generics::FontStyle::Normal => dest.write_str("normal"),
FontStyle::Italic => dest.write_str("italic"), generics::FontStyle::Italic => dest.write_str("italic"),
FontStyle::Oblique(ref angle) => { generics::FontStyle::Oblique(ref angle) => {
dest.write_str("oblique")?; dest.write_str("oblique")?;
if *angle != Self::default_angle() { if *angle != Self::default_angle() {
dest.write_char(' ')?; dest.write_char(' ')?;

View file

@ -231,3 +231,17 @@ impl ToCss for KeywordSize {
}) })
} }
} }
/// A generic value for the `font-style` property.
///
/// https://drafts.csswg.org/css-fonts-4/#font-style-prop
#[allow(missing_docs)]
#[derive(Animate, Clone, ComputeSquaredDistance, Copy, Debug, MallocSizeOf,
PartialEq, ToAnimatedValue, ToAnimatedZero)]
pub enum FontStyle<Angle> {
#[animation(error)]
Normal,
#[animation(error)]
Italic,
Oblique(Angle),
}

View file

@ -21,7 +21,7 @@ use values::computed::{Angle as ComputedAngle, Percentage as ComputedPercentage}
use values::computed::{font as computed, Context, Length, NonNegativeLength, ToComputedValue}; use values::computed::{font as computed, Context, Length, NonNegativeLength, ToComputedValue};
use values::computed::font::{FamilyName, FontFamilyList, SingleFontFamily}; use values::computed::font::{FamilyName, FontFamilyList, SingleFontFamily};
use values::generics::NonNegative; use values::generics::NonNegative;
use values::generics::font::{FeatureTagValue, FontSettings, FontTag}; use values::generics::font::{self as generics, FeatureTagValue, FontSettings, FontTag};
use values::generics::font::{KeywordInfo as GenericKeywordInfo, KeywordSize, VariationValue}; use values::generics::font::{KeywordInfo as GenericKeywordInfo, KeywordSize, VariationValue};
use values::specified::{AllowQuirks, Angle, Integer, LengthOrPercentage, NoCalcLength, Number, Percentage}; use values::specified::{AllowQuirks, Angle, Integer, LengthOrPercentage, NoCalcLength, Number, Percentage};
use values::specified::length::{FontBaseSize, AU_PER_PT, AU_PER_PX}; use values::specified::length::{FontBaseSize, AU_PER_PT, AU_PER_PX};
@ -191,54 +191,73 @@ impl Parse for AbsoluteFontWeight {
} }
} }
/// A specified value for the `font-style` property. /// The specified value of the `font-style` property, without the system font
/// /// crap.
/// https://drafts.csswg.org/css-fonts-4/#font-style-prop pub type SpecifiedFontStyle = generics::FontStyle<Angle>;
///
/// FIXME(emilio): It'd be nice to share more code with computed::FontStyle, impl ToCss for SpecifiedFontStyle {
/// except the system font stuff makes it kind of a pain. fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
#[allow(missing_docs)] where
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq)] W: Write,
pub enum FontStyle { {
Normal, match *self {
Italic, generics::FontStyle::Normal => dest.write_str("normal"),
Oblique(Angle), generics::FontStyle::Italic => dest.write_str("italic"),
System(SystemFont), generics::FontStyle::Oblique(ref angle) => {
dest.write_str("oblique")?;
if *angle != Self::default_angle() {
dest.write_char(' ')?;
angle.to_css(dest)?;
}
Ok(())
}
}
}
} }
impl ToComputedValue for FontStyle { impl Parse for SpecifiedFontStyle {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
Ok(try_match_ident_ignore_ascii_case! { input,
"normal" => generics::FontStyle::Normal,
"italic" => generics::FontStyle::Italic,
"oblique" => {
let angle = input.try(|input| Self::parse_angle(context, input))
.unwrap_or_else(|_| Self::default_angle());
generics::FontStyle::Oblique(angle)
}
})
}
}
impl ToComputedValue for SpecifiedFontStyle {
type ComputedValue = computed::FontStyle; type ComputedValue = computed::FontStyle;
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue { fn to_computed_value(&self, _: &Context) -> Self::ComputedValue {
match *self { match *self {
FontStyle::Normal => computed::FontStyle::Normal, generics::FontStyle::Normal => generics::FontStyle::Normal,
FontStyle::Italic => computed::FontStyle::Italic, generics::FontStyle::Italic => generics::FontStyle::Italic,
FontStyle::Oblique(ref angle) => { generics::FontStyle::Oblique(ref angle) => {
computed::FontStyle::Oblique(angle.to_computed_value(context)) generics::FontStyle::Oblique(Self::compute_angle(angle))
} }
#[cfg(feature = "gecko")]
FontStyle::System(..) => context
.cached_system_font
.as_ref()
.unwrap()
.font_style
.clone(),
#[cfg(not(feature = "gecko"))]
FontStyle::System(_) => unreachable!(),
} }
} }
fn from_computed_value(computed: &Self::ComputedValue) -> Self { fn from_computed_value(computed: &Self::ComputedValue) -> Self {
match *computed { match *computed {
computed::FontStyle::Normal => FontStyle::Normal, generics::FontStyle::Normal => generics::FontStyle::Normal,
computed::FontStyle::Italic => FontStyle::Italic, generics::FontStyle::Italic => generics::FontStyle::Italic,
computed::FontStyle::Oblique(ref angle) => { generics::FontStyle::Oblique(ref angle) => {
FontStyle::Oblique(Angle::from_computed_value(angle)) generics::FontStyle::Oblique(Angle::from_computed_value(angle))
} }
} }
} }
} }
/// The default angle for `font-style: oblique`. /// The default angle for `font-style: oblique`.
/// ///
/// NOTE(emilio): As of right now this diverges from the spec, which specifies /// NOTE(emilio): As of right now this diverges from the spec, which specifies
@ -258,21 +277,7 @@ pub const FONT_STYLE_OBLIQUE_MAX_ANGLE_DEGREES: f32 = 90.;
/// The minimum angle value that `font-style: oblique` should compute to. /// The minimum angle value that `font-style: oblique` should compute to.
pub const FONT_STYLE_OBLIQUE_MIN_ANGLE_DEGREES: f32 = -90.; pub const FONT_STYLE_OBLIQUE_MIN_ANGLE_DEGREES: f32 = -90.;
impl FontStyle { impl SpecifiedFontStyle {
/// More system font copy-pasta.
pub fn system_font(f: SystemFont) -> Self {
FontStyle::System(f)
}
/// Retreive a SystemFont from FontStyle.
pub fn get_system(&self) -> Option<SystemFont> {
if let FontStyle::System(s) = *self {
Some(s)
} else {
None
}
}
/// Gets a clamped angle from a specified Angle. /// Gets a clamped angle from a specified Angle.
pub fn compute_angle(angle: &Angle) -> ComputedAngle { pub fn compute_angle(angle: &Angle) -> ComputedAngle {
ComputedAngle::Deg( ComputedAngle::Deg(
@ -312,42 +317,65 @@ impl FontStyle {
} }
} }
impl ToCss for FontStyle { /// The specified value of the `font-style` property.
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result #[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToCss)]
where #[allow(missing_docs)]
W: Write, pub enum FontStyle {
{ Specified(SpecifiedFontStyle),
match *self { System(SystemFont),
FontStyle::Normal => dest.write_str("normal"), }
FontStyle::Italic => dest.write_str("italic"),
FontStyle::Oblique(ref angle) => { impl FontStyle {
dest.write_str("oblique")?; /// Return the `normal` value.
if *angle != Self::default_angle() { #[inline]
dest.write_char(' ')?; pub fn normal() -> Self {
angle.to_css(dest)?; FontStyle::Specified(generics::FontStyle::Normal)
} }
Ok(())
} /// More system font copy-pasta.
FontStyle::System(ref s) => s.to_css(dest), pub fn system_font(f: SystemFont) -> Self {
FontStyle::System(f)
}
/// Retreive a SystemFont from FontStyle.
pub fn get_system(&self) -> Option<SystemFont> {
if let FontStyle::System(s) = *self {
Some(s)
} else {
None
} }
} }
} }
impl ToComputedValue for FontStyle {
type ComputedValue = computed::FontStyle;
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
match *self {
FontStyle::Specified(ref specified) => specified.to_computed_value(context),
#[cfg(feature = "gecko")]
FontStyle::System(..) => context
.cached_system_font
.as_ref()
.unwrap()
.font_style
.clone(),
#[cfg(not(feature = "gecko"))]
FontStyle::System(_) => unreachable!(),
}
}
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
FontStyle::Specified(SpecifiedFontStyle::from_computed_value(computed))
}
}
impl Parse for FontStyle { impl Parse for FontStyle {
fn parse<'i, 't>( fn parse<'i, 't>(
context: &ParserContext, context: &ParserContext,
input: &mut Parser<'i, 't>, input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> { ) -> Result<Self, ParseError<'i>> {
Ok(try_match_ident_ignore_ascii_case! { input, Ok(FontStyle::Specified(SpecifiedFontStyle::parse(context, input)?))
"normal" => FontStyle::Normal,
"italic" => FontStyle::Italic,
"oblique" => {
let angle = input.try(|input| Self::parse_angle(context, input))
.unwrap_or_else(|_| Self::default_angle());
FontStyle::Oblique(angle)
}
})
} }
} }

View file

@ -5408,7 +5408,8 @@ pub extern "C" fn Servo_ParseFontShorthandForMatching(
weight: nsCSSValueBorrowedMut weight: nsCSSValueBorrowedMut
) -> bool { ) -> bool {
use style::properties::shorthands::font; use style::properties::shorthands::font;
use style::values::specified::font::{FontFamily, FontWeight, FontStyle}; use style::values::generics::font::FontStyle as GenericFontStyle;
use style::values::specified::font::{FontFamily, FontWeight, FontStyle, SpecifiedFontStyle};
let string = unsafe { (*value).to_string() }; let string = unsafe { (*value).to_string() };
let mut input = ParserInput::new(&string); let mut input = ParserInput::new(&string);
@ -5433,15 +5434,19 @@ pub extern "C" fn Servo_ParseFontShorthandForMatching(
FontFamily::Values(list) => family.set_move(list.0), FontFamily::Values(list) => family.set_move(list.0),
FontFamily::System(_) => return false, FontFamily::System(_) => return false,
} }
match font.font_style { let specified_font_style = match font.font_style {
FontStyle::Normal => style.set_normal(), FontStyle::Specified(ref s) => s,
FontStyle::Italic => style.set_enum(structs::NS_FONT_STYLE_ITALIC as i32),
FontStyle::Oblique(ref angle) => {
style.set_angle(FontStyle::compute_angle(angle))
}
FontStyle::System(_) => return false, FontStyle::System(_) => return false,
}; };
match *specified_font_style {
GenericFontStyle::Normal => style.set_normal(),
GenericFontStyle::Italic => style.set_enum(structs::NS_FONT_STYLE_ITALIC as i32),
GenericFontStyle::Oblique(ref angle) => {
style.set_angle(SpecifiedFontStyle::compute_angle(angle))
}
}
if font.font_stretch.get_system().is_some() { if font.font_stretch.get_system().is_some() {
return false; return false;
} }