style: Only parse font-stretch keywords in the font shorthand.

Bug: 1454883
Reviewed-by: xidorn
MozReview-Commit-ID: 4BqnOSz83w4
This commit is contained in:
Emilio Cobos Álvarez 2018-04-20 19:57:36 +02:00
parent 36cef8ec68
commit b1fbb28b8a
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
2 changed files with 57 additions and 5 deletions

View file

@ -27,6 +27,7 @@
use properties::longhands::system_font::SystemFont;
use values::specified::text::LineHeight;
use values::specified::FontSize;
use values::specified::font::{FontStretch, FontStretchKeyword};
<%
gecko_sub_properties = "kerning language_override size_adjust \
@ -94,8 +95,8 @@
}
}
if stretch.is_none() {
if let Ok(value) = input.try(|input| font_stretch::parse(context, input)) {
stretch = Some(value);
if let Ok(value) = input.try(FontStretchKeyword::parse) {
stretch = Some(FontStretch::Keyword(value));
continue
}
}
@ -180,15 +181,31 @@
% endfor
% endif
// In case of serialization for canvas font, we need to drop
// initial values of properties other than size and family.
% for name in "style variant_caps weight stretch".split():
// Only font-stretch keywords are allowed as part as the font
// shorthand.
let font_stretch = match *self.font_stretch {
FontStretch::Keyword(kw) => kw,
FontStretch::Stretch(percentage) => {
match FontStretchKeyword::from_percentage(percentage.get()) {
Some(kw) => kw,
None => return Ok(()),
}
}
FontStretch::System(..) => return Ok(()),
};
% for name in "style variant_caps weight".split():
if self.font_${name} != &font_${name}::get_initial_specified_value() {
self.font_${name}.to_css(dest)?;
dest.write_str(" ")?;
}
% endfor
if font_stretch != FontStretchKeyword::Normal {
font_stretch.to_css(dest)?;
dest.write_str(" ")?;
}
self.font_size.to_css(dest)?;
if *self.line_height != LineHeight::normal() {

View file

@ -418,6 +418,41 @@ impl FontStretchKeyword {
UltraExpanded => 2.,
})
}
/// Does the opposite operation to `compute`, in order to serialize keywords
/// if possible.
pub fn from_percentage(percentage: f32) -> Option<Self> {
use self::FontStretchKeyword::*;
// NOTE(emilio): Can't use `match` because of rust-lang/rust#41620.
if percentage == 0.5 {
return Some(UltraCondensed);
}
if percentage == 0.625 {
return Some(ExtraCondensed);
}
if percentage == 0.75 {
return Some(Condensed);
}
if percentage == 0.875 {
return Some(SemiCondensed);
}
if percentage == 1. {
return Some(Normal);
}
if percentage == 1.125 {
return Some(SemiExpanded);
}
if percentage == 1.25 {
return Some(Expanded);
}
if percentage == 1.5 {
return Some(ExtraExpanded);
}
if percentage == 2. {
return Some(UltraExpanded);
}
None
}
}
impl FontStretch {