style: Refactor font-feature-settings and font-variation-settings.

This fixes all known issues with serialization and parsing of these two
properties, and in particular calc handling and such:

  https://bugzilla.mozilla.org/show_bug.cgi?id=1434692
  https://bugzilla.mozilla.org/show_bug.cgi?id=1434724

Also does a fair amount of cleanup and all that, which was needed.
This commit is contained in:
Emilio Cobos Álvarez 2018-02-01 02:14:26 +01:00
parent 09398d42af
commit 3b34d734e6
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
11 changed files with 270 additions and 261 deletions

View file

@ -21,8 +21,8 @@ use style_traits::{CssWriter, ParseError, StyleParseErrorKind, ToCss};
use values::CustomIdent;
use values::computed::{font as computed, Context, Length, NonNegativeLength, ToComputedValue};
use values::computed::font::{SingleFontFamily, FontFamilyList, FamilyName};
use values::generics::font::{FontSettings, FontSettingTagFloat};
use values::specified::{AllowQuirks, LengthOrPercentage, NoCalcLength, Number};
use values::generics::font::{FontSettings, FeatureTagValue, VariationValue};
use values::specified::{AllowQuirks, Integer, LengthOrPercentage, NoCalcLength, Number};
use values::specified::length::{AU_PER_PT, AU_PER_PX, FontBaseSize};
const DEFAULT_SCRIPT_MIN_SIZE_PT: u32 = 8;
@ -164,8 +164,8 @@ impl From<LengthOrPercentage> for FontSize {
}
}
/// Specifies a prioritized list of font family names or generic family names.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
/// Specifies a prioritized list of font family names or generic family names
pub enum FontFamily {
/// List of `font-family`
Values(FontFamilyList),
@ -1709,13 +1709,15 @@ 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.
/// This property provides low-level control over OpenType or TrueType font variations.
pub type SpecifiedFontFeatureSettings = FontSettings<FeatureTagValue<Integer>>;
/// Define initial settings that apply when the font defined by an @font-face
/// rule is rendered.
#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToCss)]
pub enum FontFeatureSettings {
/// Value of `FontSettings`
Value(computed::FontFeatureSettings),
Value(SpecifiedFontFeatureSettings),
/// System font
System(SystemFont)
}
@ -1724,7 +1726,7 @@ impl FontFeatureSettings {
#[inline]
/// Get default value of `font-feature-settings` as normal
pub fn normal() -> FontFeatureSettings {
FontFeatureSettings::Value(FontSettings::Normal)
FontFeatureSettings::Value(FontSettings::normal())
}
/// Get `font-feature-settings` with system font
@ -1745,12 +1747,12 @@ impl FontFeatureSettings {
impl ToComputedValue for FontFeatureSettings {
type ComputedValue = computed::FontFeatureSettings;
fn to_computed_value(&self, _context: &Context) -> computed::FontFeatureSettings {
fn to_computed_value(&self, context: &Context) -> computed::FontFeatureSettings {
match *self {
FontFeatureSettings::Value(ref v) => v.clone(),
FontFeatureSettings::Value(ref v) => v.to_computed_value(context),
FontFeatureSettings::System(_) => {
#[cfg(feature = "gecko")] {
_context.cached_system_font.as_ref().unwrap().font_feature_settings.clone()
context.cached_system_font.as_ref().unwrap().font_feature_settings.clone()
}
#[cfg(feature = "servo")] {
unreachable!()
@ -1760,7 +1762,7 @@ impl ToComputedValue for FontFeatureSettings {
}
fn from_computed_value(other: &computed::FontFeatureSettings) -> Self {
FontFeatureSettings::Value(other.clone())
FontFeatureSettings::Value(ToComputedValue::from_computed_value(other))
}
}
@ -1770,7 +1772,7 @@ impl Parse for FontFeatureSettings {
context: &ParserContext,
input: &mut Parser<'i, 't>
) -> Result<FontFeatureSettings, ParseError<'i>> {
computed::FontFeatureSettings::parse(context, input).map(FontFeatureSettings::Value)
SpecifiedFontFeatureSettings::parse(context, input).map(FontFeatureSettings::Value)
}
}
@ -2006,9 +2008,51 @@ impl ToCss for FontTag {
}
}
/// This property provides low-level control over OpenType or TrueType font
/// variations.
pub type FontVariationSettings = FontSettings<VariationValue<Number>>;
fn parse_one_feature_value<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Integer, ParseError<'i>> {
if let Ok(integer) = input.try(|i| Integer::parse_non_negative(context, i)) {
return Ok(integer)
}
try_match_ident_ignore_ascii_case! { input,
"on" => Ok(Integer::new(1)),
"off" => Ok(Integer::new(0)),
}
}
impl Parse for FeatureTagValue<Integer> {
/// https://drafts.csswg.org/css-fonts-4/#feature-tag-value
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
let tag = FontTag::parse(context, input)?;
let value = input.try(|i| parse_one_feature_value(context, i))
.unwrap_or_else(|_| Integer::new(1));
Ok(Self { tag, value })
}
}
impl Parse for VariationValue<Number> {
/// This is the `<string> <number>` part of the font-variation-settings
/// syntax.
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
let tag = FontTag::parse(context, input)?;
let value = Number::parse(context, input)?;
Ok(Self { tag, value })
}
}
/// This property provides low-level control over OpenType or TrueType font variations.
pub type FontVariantSettings = FontSettings<FontSettingTagFloat>;
#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToComputedValue)]
/// text-zoom. Enable if true, disable if false