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

@ -1426,29 +1426,21 @@ impl Clone for ${style_struct.gecko_struct_name} {
}
</%def>
<%def name="impl_font_settings(ident, tag_type)">
<%def name="impl_font_settings(ident, tag_type, value_type, gecko_value_type)">
<%
gecko_ffi_name = to_camel_case_lower(ident)
%>
pub fn set_${ident}(&mut self, v: longhands::${ident}::computed_value::T) {
use values::generics::font::FontSettings;
let current_settings = &mut self.gecko.mFont.${gecko_ffi_name};
current_settings.clear_pod();
match v {
FontSettings::Normal => {}, // do nothing, length is already 0
unsafe { current_settings.set_len_pod(v.0.len() as u32) };
FontSettings::Tag(other_settings) => {
unsafe { current_settings.set_len_pod(other_settings.len() as u32) };
for (current, other) in current_settings.iter_mut().zip(other_settings) {
current.mTag = other.tag.0;
current.mValue = other.value.0;
}
}
};
for (current, other) in current_settings.iter_mut().zip(v.0.iter()) {
current.mTag = other.tag.0;
current.mValue = other.value as ${gecko_value_type};
}
}
pub fn copy_${ident}_from(&mut self, other: &Self) {
@ -1470,21 +1462,17 @@ impl Clone for ${style_struct.gecko_struct_name} {
}
pub fn clone_${ident}(&self) -> longhands::${ident}::computed_value::T {
use values::generics::font::{FontSettings, FontSettingTag, ${tag_type}};
use values::generics::font::{FontSettings, ${tag_type}};
use values::specified::font::FontTag;
if self.gecko.mFont.${gecko_ffi_name}.len() == 0 {
FontSettings::Normal
} else {
FontSettings::Tag(
self.gecko.mFont.${gecko_ffi_name}.iter().map(|gecko_font_setting| {
FontSettingTag {
tag: FontTag(gecko_font_setting.mTag),
value: ${tag_type}(gecko_font_setting.mValue),
}
}).collect()
)
}
FontSettings(
self.gecko.mFont.${gecko_ffi_name}.iter().map(|gecko_font_setting| {
${tag_type} {
tag: FontTag(gecko_font_setting.mTag),
value: gecko_font_setting.mValue as ${value_type},
}
}).collect::<Vec<_>>().into_boxed_slice()
)
}
</%def>
@ -2338,8 +2326,10 @@ fn static_assert() {
<%self:impl_trait style_struct_name="Font"
skip_longhands="${skip_font_longhands}">
<% impl_font_settings("font_feature_settings", "FontSettingTagInt") %>
<% impl_font_settings("font_variation_settings", "FontSettingTagFloat") %>
// Negative numbers are invalid at parse time, but <integer> is still an
// i32.
<% impl_font_settings("font_feature_settings", "FeatureTagValue", "i32", "u32") %>
<% impl_font_settings("font_variation_settings", "VariationValue", "f32", "f32") %>
pub fn fixup_none_generic(&mut self, device: &Device) {
self.gecko.mFont.systemFont = false;

View file

@ -17,8 +17,6 @@ use properties::{CSSWideKeyword, PropertyDeclaration};
use properties::longhands;
use properties::longhands::font_weight::computed_value::T as FontWeight;
use properties::longhands::font_stretch::computed_value::T as FontStretch;
#[cfg(feature = "gecko")]
use properties::longhands::font_variation_settings::computed_value::T as FontVariationSettings;
use properties::longhands::visibility::computed_value::T as Visibility;
#[cfg(feature = "gecko")]
use properties::PropertyId;
@ -51,15 +49,15 @@ use values::computed::transform::Translate as ComputedTranslate;
use values::computed::transform::Scale as ComputedScale;
use values::generics::transform::{self, Rotate, Translate, Scale, Transform, TransformOperation};
use values::distance::{ComputeSquaredDistance, SquaredDistance};
#[cfg(feature = "gecko")] use values::generics::font::FontSettings as GenericFontSettings;
#[cfg(feature = "gecko")] use values::generics::font::FontSettingTag as GenericFontSettingTag;
#[cfg(feature = "gecko")] use values::generics::font::FontSettingTagFloat;
use values::generics::font::FontSettings as GenericFontSettings;
use values::computed::font::FontVariationSettings;
use values::generics::font::VariationValue;
use values::generics::NonNegative;
use values::generics::effects::Filter;
use values::generics::position as generic_position;
use values::generics::svg::{SVGLength, SvgLengthOrPercentageOrNumber, SVGPaint};
use values::generics::svg::{SVGPaintKind, SVGStrokeDashArray, SVGOpacity};
#[cfg(feature = "gecko")] use values::specified::font::FontTag;
use values::specified::font::FontTag;
/// <https://drafts.csswg.org/css-transitions/#animtype-repeatable-list>
pub trait RepeatableListAnimatable: Animate {}
@ -817,18 +815,16 @@ impl Into<FontStretch> for f64 {
}
/// <https://drafts.csswg.org/css-fonts-4/#font-variation-settings-def>
#[cfg(feature = "gecko")]
impl Animate for FontVariationSettings {
#[inline]
fn animate(&self, other: &Self, procedure: Procedure) -> Result<Self, ()> {
FontSettingTagIter::new(self, other)?
.map(|r| r.and_then(|(st, ot)| st.animate(&ot, procedure)))
.collect::<Result<Vec<FontSettingTag>, ()>>()
.map(GenericFontSettings::Tag)
.collect::<Result<Vec<ComputedVariationValue>, ()>>()
.map(|v| GenericFontSettings(v.into_boxed_slice()))
}
}
#[cfg(feature = "gecko")]
impl ComputeSquaredDistance for FontVariationSettings {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
@ -838,7 +834,6 @@ impl ComputeSquaredDistance for FontVariationSettings {
}
}
#[cfg(feature = "gecko")]
impl ToAnimatedZero for FontVariationSettings {
#[inline]
fn to_animated_zero(&self) -> Result<Self, ()> {
@ -846,45 +841,17 @@ impl ToAnimatedZero for FontVariationSettings {
}
}
#[cfg(feature = "gecko")]
impl Animate for FontSettingTag {
#[inline]
fn animate(&self, other: &Self, procedure: Procedure) -> Result<Self, ()> {
if self.tag != other.tag {
return Err(());
}
let value = self.value.animate(&other.value, procedure)?;
Ok(FontSettingTag {
tag: self.tag,
value,
})
}
}
type ComputedVariationValue = VariationValue<Number>;
#[cfg(feature = "gecko")]
impl ComputeSquaredDistance for FontSettingTag {
#[inline]
fn compute_squared_distance(&self, other: &Self) -> Result<SquaredDistance, ()> {
if self.tag != other.tag {
return Err(());
}
self.value.compute_squared_distance(&other.value)
}
}
#[cfg(feature = "gecko")]
type FontSettingTag = GenericFontSettingTag<FontSettingTagFloat>;
#[cfg(feature = "gecko")]
// FIXME: Could do a rename, this is only used for font variations.
struct FontSettingTagIterState<'a> {
tags: Vec<(&'a FontSettingTag)>,
tags: Vec<(&'a ComputedVariationValue)>,
index: usize,
prev_tag: FontTag,
}
#[cfg(feature = "gecko")]
impl<'a> FontSettingTagIterState<'a> {
fn new(tags: Vec<(&'a FontSettingTag)>) -> FontSettingTagIterState<'a> {
fn new(tags: Vec<<&'a ComputedVariationValue>) -> FontSettingTagIterState<'a> {
FontSettingTagIterState {
index: tags.len(),
tags,
@ -898,12 +865,13 @@ impl<'a> FontSettingTagIterState<'a> {
/// [CSS fonts level 4](https://drafts.csswg.org/css-fonts-4/#descdef-font-face-font-variation-settings)
/// defines the animation of font-variation-settings as follows:
///
/// Two declarations of font-feature-settings[sic] can be animated between if they are "like".
/// "Like" declarations are ones where the same set of properties appear (in any order).
/// Because succesive[sic] duplicate properties are applied instead of prior duplicate
/// properties, two declarations can be "like" even if they have differing number of
/// properties. If two declarations are "like" then animation occurs pairwise between
/// corresponding values in the declarations.
/// Two declarations of font-feature-settings[sic] can be animated between if
/// they are "like". "Like" declarations are ones where the same set of
/// properties appear (in any order). Because succesive[sic] duplicate
/// properties are applied instead of prior duplicate properties, two
/// declarations can be "like" even if they have differing number of
/// properties. If two declarations are "like" then animation occurs pairwise
/// between corresponding values in the declarations.
///
/// In other words if we have the following lists:
///
@ -915,9 +883,10 @@ impl<'a> FontSettingTagIterState<'a> {
/// "wdth" 5, "wght" 2
/// "wght" 4, "wdth" 10
///
/// This iterator supports this by sorting the two lists, then iterating them in reverse,
/// and skipping entries with repeated tag names. It will return Some(Err()) if it reaches the
/// end of one list before the other, or if the tag names do not match.
/// This iterator supports this by sorting the two lists, then iterating them in
/// reverse, and skipping entries with repeated tag names. It will return
/// Some(Err()) if it reaches the end of one list before the other, or if the
/// tag names do not match.
///
/// For the above example, this iterator would return:
///
@ -925,37 +894,33 @@ impl<'a> FontSettingTagIterState<'a> {
/// Some(Ok("wdth" 5, "wdth" 10))
/// None
///
#[cfg(feature = "gecko")]
struct FontSettingTagIter<'a> {
a_state: FontSettingTagIterState<'a>,
b_state: FontSettingTagIterState<'a>,
}
#[cfg(feature = "gecko")]
impl<'a> FontSettingTagIter<'a> {
fn new(
a_settings: &'a FontVariationSettings,
b_settings: &'a FontVariationSettings,
) -> Result<FontSettingTagIter<'a>, ()> {
if let (&GenericFontSettings::Tag(ref a_tags), &GenericFontSettings::Tag(ref b_tags)) = (a_settings, b_settings)
{
fn as_new_sorted_tags(tags: &Vec<FontSettingTag>) -> Vec<(&FontSettingTag)> {
use std::iter::FromIterator;
let mut sorted_tags: Vec<(&FontSettingTag)> = Vec::from_iter(tags.iter());
sorted_tags.sort_by_key(|k| k.tag.0);
sorted_tags
};
Ok(FontSettingTagIter {
a_state: FontSettingTagIterState::new(as_new_sorted_tags(a_tags)),
b_state: FontSettingTagIterState::new(as_new_sorted_tags(b_tags)),
})
} else {
Err(())
if a_settings.0.is_empty() || b_settings.0.is_empty() {
return Err(());
}
fn as_new_sorted_tags(tags: &[ComputedVariationValue]) -> Vec<<&ComputedVariationValue> {
use std::iter::FromIterator;
let mut sorted_tags = Vec::from_iter(tags.iter());
sorted_tags.sort_by_key(|k| k.tag.0);
sorted_tags
};
Ok(FontSettingTagIter {
a_state: FontSettingTagIterState::new(as_new_sorted_tags(&a_settings.0)),
b_state: FontSettingTagIterState::new(as_new_sorted_tags(&b_settings.0)),
})
}
fn next_tag(state: &mut FontSettingTagIterState<'a>) -> Option<(&'a FontSettingTag)> {
fn next_tag(state: &mut FontSettingTagIterState<'a>) -> Option<<&'a ComputedVariationValue> {
if state.index == 0 {
return None;
}
@ -971,11 +936,10 @@ impl<'a> FontSettingTagIter<'a> {
}
}
#[cfg(feature = "gecko")]
impl<'a> Iterator for FontSettingTagIter<'a> {
type Item = Result<(&'a FontSettingTag, &'a FontSettingTag), ()>;
type Item = Result<(&'a ComputedVariationValue, &'a ComputedVariationValue), ()>;
fn next(&mut self) -> Option<Result<(&'a FontSettingTag, &'a FontSettingTag), ()>> {
fn next(&mut self) -> Option<Result<(&'a ComputedVariationValue, &'a ComputedVariationValue), ()>> {
match (
FontSettingTagIter::next_tag(&mut self.a_state),
FontSettingTagIter::next_tag(&mut self.b_state),

View file

@ -144,7 +144,6 @@ ${helpers.predefined_type("font-feature-settings",
initial_value="computed::FontFeatureSettings::normal()",
initial_specified_value="specified::FontFeatureSettings::normal()",
extra_prefixes="moz",
boxed=True,
animation_value_type="discrete",
flags="APPLIES_TO_FIRST_LETTER APPLIES_TO_FIRST_LINE APPLIES_TO_PLACEHOLDER",
spec="https://drafts.csswg.org/css-fonts/#propdef-font-feature-settings")}
@ -157,10 +156,10 @@ https://drafts.csswg.org/css-fonts-4/#low-level-font-variation-settings-control-
%>
${helpers.predefined_type("font-variation-settings",
"FontVariantSettings",
"FontVariationSettings",
products="gecko",
gecko_pref="layout.css.font-variations.enabled",
initial_value="specified::FontVariantSettings::normal()",
initial_value="computed::FontVariationSettings::normal()",
animation_value_type="ComputedValue",
flags="APPLIES_TO_FIRST_LETTER APPLIES_TO_FIRST_LINE APPLIES_TO_PLACEHOLDER",
spec="${variation_spec}")}