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")]
use values::specified::font::{SpecifiedFontFeatureSettings, SpecifiedFontVariationSettings};
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::url::SpecifiedUrl;
@ -145,16 +146,15 @@ impl Parse for FontStyle {
) -> Result<Self, ParseError<'i>> {
let style = SpecifiedFontStyle::parse(context, input)?;
Ok(match style {
SpecifiedFontStyle::Normal => FontStyle::Normal,
SpecifiedFontStyle::Italic => FontStyle::Italic,
SpecifiedFontStyle::Oblique(angle) => {
GenericFontStyle::Normal => FontStyle::Normal,
GenericFontStyle::Italic => FontStyle::Italic,
GenericFontStyle::Oblique(angle) => {
let second_angle = input.try(|input| {
SpecifiedFontStyle::parse_angle(context, input)
}).unwrap_or_else(|_| angle.clone());
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::specified::font::{SpecifiedFontFeatureSettings, SpecifiedFontVariationSettings};
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 {
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) {
use values::computed::font::FontStyle;
use values::generics::font::FontStyle;
self.gecko.mFont.style = match v {
FontStyle::Normal => structs::NS_STYLE_FONT_STYLE_NORMAL,
FontStyle::Italic => structs::NS_STYLE_FONT_STYLE_ITALIC,

View file

@ -18,8 +18,8 @@ ${helpers.predefined_type("font-family",
${helpers.predefined_type(
"font-style",
"FontStyle",
initial_value="computed::FontStyle::Normal",
initial_specified_value="specified::FontStyle::Normal",
initial_value="computed::FontStyle::normal()",
initial_specified_value="specified::FontStyle::normal()",
# FIXME(emilio): This won't handle clamping correctly.
animation_value_type="ComputedValue",
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")]
fn adjust_for_mathvariant(&mut self) {
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 values::generics::font::FontStyle;
if self.style.get_font().clone__moz_math_variant() != MozMathVariant::None {
let font_style = self.style.mutate_font();
font_style.set_font_weight(FontWeight::normal());

View file

@ -22,8 +22,7 @@ use style_traits::{CssWriter, ParseError, ToCss};
use values::CSSFloat;
use values::animated::{ToAnimatedValue, ToAnimatedZero};
use values::computed::{Angle, Context, Integer, NonNegativeLength, Number, ToComputedValue};
use values::generics::font::{FeatureTagValue, FontSettings};
use values::generics::font::{KeywordInfo as GenericKeywordInfo, VariationValue};
use values::generics::font::{self as generics, FeatureTagValue, FontSettings, VariationValue};
use values::specified::font::{self as specified, MIN_FONT_WEIGHT, MAX_FONT_WEIGHT};
use values::specified::length::{FontBaseSize, NoCalcLength};
@ -67,7 +66,7 @@ pub struct FontSize {
}
/// Additional information for computed keyword-derived font sizes.
pub type KeywordInfo = GenericKeywordInfo<NonNegativeLength>;
pub type KeywordInfo = generics::KeywordInfo<NonNegativeLength>;
impl FontWeight {
/// Value for normal
@ -840,30 +839,22 @@ impl ToComputedValue for specified::MozScriptLevel {
}
/// The computed value of `font-style`.
#[derive(Animate, Clone, ComputeSquaredDistance, Copy, Debug, MallocSizeOf,
PartialEq)]
#[allow(missing_docs)]
pub enum FontStyle {
#[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()))
}
}
///
/// FIXME(emilio): Angle should be a custom type to handle clamping during
/// animation.
pub type FontStyle = generics::FontStyle<Angle>;
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:
///
/// https://drafts.csswg.org/css-fonts-4/#valdef-font-style-oblique-angle
#[inline]
pub fn default_angle() -> Angle {
Angle::Deg(specified::DEFAULT_FONT_STYLE_OBLIQUE_ANGLE_DEGREES)
}
@ -875,10 +866,10 @@ impl FontStyle {
use gecko_bindings::structs;
match kw as u32 {
structs::NS_STYLE_FONT_STYLE_NORMAL => FontStyle::Normal,
structs::NS_STYLE_FONT_STYLE_ITALIC => FontStyle::Italic,
structs::NS_STYLE_FONT_STYLE_NORMAL => generics::FontStyle::Normal,
structs::NS_STYLE_FONT_STYLE_ITALIC => generics::FontStyle::Italic,
// 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"),
}
}
@ -890,9 +881,9 @@ impl ToCss for FontStyle {
W: fmt::Write,
{
match *self {
FontStyle::Normal => dest.write_str("normal"),
FontStyle::Italic => dest.write_str("italic"),
FontStyle::Oblique(ref angle) => {
generics::FontStyle::Normal => dest.write_str("normal"),
generics::FontStyle::Italic => dest.write_str("italic"),
generics::FontStyle::Oblique(ref angle) => {
dest.write_str("oblique")?;
if *angle != Self::default_angle() {
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::{FamilyName, FontFamilyList, SingleFontFamily};
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::specified::{AllowQuirks, Angle, Integer, LengthOrPercentage, NoCalcLength, Number, Percentage};
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.
///
/// https://drafts.csswg.org/css-fonts-4/#font-style-prop
///
/// FIXME(emilio): It'd be nice to share more code with computed::FontStyle,
/// except the system font stuff makes it kind of a pain.
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq)]
pub enum FontStyle {
Normal,
Italic,
Oblique(Angle),
System(SystemFont),
/// The specified value of the `font-style` property, without the system font
/// crap.
pub type SpecifiedFontStyle = generics::FontStyle<Angle>;
impl ToCss for SpecifiedFontStyle {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
match *self {
generics::FontStyle::Normal => dest.write_str("normal"),
generics::FontStyle::Italic => dest.write_str("italic"),
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;
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
fn to_computed_value(&self, _: &Context) -> Self::ComputedValue {
match *self {
FontStyle::Normal => computed::FontStyle::Normal,
FontStyle::Italic => computed::FontStyle::Italic,
FontStyle::Oblique(ref angle) => {
computed::FontStyle::Oblique(angle.to_computed_value(context))
generics::FontStyle::Normal => generics::FontStyle::Normal,
generics::FontStyle::Italic => generics::FontStyle::Italic,
generics::FontStyle::Oblique(ref angle) => {
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 {
match *computed {
computed::FontStyle::Normal => FontStyle::Normal,
computed::FontStyle::Italic => FontStyle::Italic,
computed::FontStyle::Oblique(ref angle) => {
FontStyle::Oblique(Angle::from_computed_value(angle))
generics::FontStyle::Normal => generics::FontStyle::Normal,
generics::FontStyle::Italic => generics::FontStyle::Italic,
generics::FontStyle::Oblique(ref angle) => {
generics::FontStyle::Oblique(Angle::from_computed_value(angle))
}
}
}
}
/// The default angle for `font-style: oblique`.
///
/// 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.
pub const FONT_STYLE_OBLIQUE_MIN_ANGLE_DEGREES: f32 = -90.;
impl FontStyle {
/// 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
}
}
impl SpecifiedFontStyle {
/// Gets a clamped angle from a specified Angle.
pub fn compute_angle(angle: &Angle) -> ComputedAngle {
ComputedAngle::Deg(
@ -312,42 +317,65 @@ impl FontStyle {
}
}
impl ToCss for FontStyle {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
match *self {
FontStyle::Normal => dest.write_str("normal"),
FontStyle::Italic => dest.write_str("italic"),
FontStyle::Oblique(ref angle) => {
dest.write_str("oblique")?;
if *angle != Self::default_angle() {
dest.write_char(' ')?;
angle.to_css(dest)?;
}
Ok(())
}
FontStyle::System(ref s) => s.to_css(dest),
/// The specified value of the `font-style` property.
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToCss)]
#[allow(missing_docs)]
pub enum FontStyle {
Specified(SpecifiedFontStyle),
System(SystemFont),
}
impl FontStyle {
/// Return the `normal` value.
#[inline]
pub fn normal() -> Self {
FontStyle::Specified(generics::FontStyle::Normal)
}
/// 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
}
}
}
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 {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
Ok(try_match_ident_ignore_ascii_case! { 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)
}
})
Ok(FontStyle::Specified(SpecifiedFontStyle::parse(context, input)?))
}
}

View file

@ -5408,7 +5408,8 @@ pub extern "C" fn Servo_ParseFontShorthandForMatching(
weight: nsCSSValueBorrowedMut
) -> bool {
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 mut input = ParserInput::new(&string);
@ -5433,15 +5434,19 @@ pub extern "C" fn Servo_ParseFontShorthandForMatching(
FontFamily::Values(list) => family.set_move(list.0),
FontFamily::System(_) => return false,
}
match font.font_style {
FontStyle::Normal => style.set_normal(),
FontStyle::Italic => style.set_enum(structs::NS_FONT_STYLE_ITALIC as i32),
FontStyle::Oblique(ref angle) => {
style.set_angle(FontStyle::compute_angle(angle))
}
let specified_font_style = match font.font_style {
FontStyle::Specified(ref s) => s,
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() {
return false;
}