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

@ -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)?))
}
}