servo: Merge #13910 - Font feature settings (from rwakulszowa:font-feature-settings); r=emilio

<!-- Please describe your changes on the following line: -->
Implement parsing/serialization for font-feature-settings

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #13852 (github issue number if applicable).

<!-- Either: -->
- [x] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

Source-Repo: https://github.com/servo/servo
Source-Revision: a1dfa0fd85fa3071c90b868f4594b9ee59d08744
This commit is contained in:
rwakulszowa 2016-10-27 10:48:41 -05:00
Родитель dff6bf022d
Коммит a00b44e235
3 изменённых файлов: 186 добавлений и 0 удалений

Просмотреть файл

@ -368,3 +368,110 @@ ${helpers.single_keyword("font-variant-position",
gecko_constant_prefix="NS_FONT_VARIANT_POSITION",
animatable=False)}
<%helpers:longhand name="font-feature-settings" products="none" animatable="False">
use cssparser::ToCss;
use std::fmt;
use values::NoViewportPercentage;
use values::computed::ComputedValueAsSpecified;
pub use self::computed_value::T as SpecifiedValue;
impl ComputedValueAsSpecified for SpecifiedValue {}
impl NoViewportPercentage for SpecifiedValue {}
pub mod computed_value {
use cssparser::ToCss;
use cssparser::Parser;
use std::fmt;
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum T {
Normal,
Tag(Vec<FeatureTagValue>)
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct FeatureTagValue {
pub tag: String,
pub value: i32
}
impl ToCss for T {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
T::Normal => dest.write_str("normal"),
T::Tag(ref ftvs) => {
let mut iter = ftvs.iter();
// handle head element
try!(iter.next().unwrap().to_css(dest));
// handle tail, precede each with a delimiter
for ftv in iter {
try!(dest.write_str(", "));
try!(ftv.to_css(dest));
}
Ok(())
}
}
}
}
impl ToCss for FeatureTagValue {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match self.value {
1 => write!(dest, "\"{}\"", self.tag),
0 => write!(dest, "\"{}\" off", self.tag),
x => write!(dest, "\"{}\" {}", self.tag, x)
}
}
}
impl FeatureTagValue {
/// https://www.w3.org/TR/css-fonts-3/#propdef-font-feature-settings
/// <string> [ on | off | <integer> ]
pub fn parse(input: &mut Parser) -> Result<FeatureTagValue, ()> {
let tag = try!(input.expect_string());
// allowed strings of length 4 containing chars: <U+20, U+7E>
if tag.len() != 4 ||
tag.chars().any(|c| c < ' ' || c > '~')
{
return Err(())
}
if let Ok(value) = input.try(|input| input.expect_integer()) {
// handle integer, throw if it is negative
if value >= 0 {
Ok(FeatureTagValue { tag: tag.into_owned(), value: value })
} else {
Err(())
}
} else if let Ok(_) = input.try(|input| input.expect_ident_matching("on")) {
// on is an alias for '1'
Ok(FeatureTagValue { tag: tag.into_owned(), value: 1 })
} else if let Ok(_) = input.try(|input| input.expect_ident_matching("off")) {
// off is an alias for '0'
Ok(FeatureTagValue { tag: tag.into_owned(), value: 0 })
} else {
// empty value is an alias for '1'
Ok(FeatureTagValue { tag:tag.into_owned(), value: 1 })
}
}
}
}
#[inline]
pub fn get_initial_value() -> computed_value::T {
computed_value::T::Normal
}
/// normal | <feature-tag-value>#
pub fn parse(_context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
if input.try(|input| input.expect_ident_matching("normal")).is_ok() {
Ok(computed_value::T::Normal)
} else {
input.parse_comma_separated(computed_value::FeatureTagValue::parse)
.map(computed_value::T::Tag)
}
}
</%helpers:longhand>

Просмотреть файл

@ -0,0 +1,78 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use cssparser::Parser;
use media_queries::CSSErrorReporterTest;
use style::parser::ParserContext;
use style::properties::longhands::font_feature_settings;
use style::properties::longhands::font_feature_settings::computed_value;
use style::properties::longhands::font_feature_settings::computed_value::FeatureTagValue;
use style::stylesheets::Origin;
use url::Url;
#[test]
fn font_feature_settings_should_parse_properly() {
let normal = parse_longhand!(font_feature_settings, "normal");
let normal_computed = computed_value::T::Normal;
assert_eq!(normal, normal_computed);
let on = parse_longhand!(font_feature_settings, "\"abcd\" on");
let on_computed = computed_value::T::Tag(vec![
FeatureTagValue { tag: String::from("abcd"), value: 1 }
]);
assert_eq!(on, on_computed);
let off = parse_longhand!(font_feature_settings, "\"abcd\" off");
let off_computed = computed_value::T::Tag(vec![
FeatureTagValue { tag: String::from("abcd"), value: 0 }
]);
assert_eq!(off, off_computed);
let no_value = parse_longhand!(font_feature_settings, "\"abcd\"");
let no_value_computed = computed_value::T::Tag(vec![
FeatureTagValue { tag: String::from("abcd"), value: 1 }
]);
assert_eq!(no_value, no_value_computed);
let pos_integer = parse_longhand!(font_feature_settings, "\"abcd\" 100");
let pos_integer_computed = computed_value::T::Tag(vec![
FeatureTagValue { tag: String::from("abcd"), value: 100 }
]);
assert_eq!(pos_integer, pos_integer_computed);
let multiple = parse_longhand!(font_feature_settings, "\"abcd\" off, \"efgh\"");
let multiple_computed = computed_value::T::Tag(vec![
FeatureTagValue { tag: String::from("abcd"), value: 0 },
FeatureTagValue { tag: String::from("efgh"), value: 1 }
]);
assert_eq!(multiple, multiple_computed);
}
#[test]
fn font_feature_settings_should_throw_on_bad_input() {
let url = Url::parse("http://localhost").unwrap();
let context = ParserContext::new(Origin::Author, &url, Box::new(CSSErrorReporterTest));
let mut empty = Parser::new("");
assert!(font_feature_settings::parse(&context, &mut empty).is_err());
let mut negative = Parser::new("\"abcd\" -1");
assert!(font_feature_settings::parse(&context, &mut negative).is_err());
let mut short_tag = Parser::new("\"abc\"");
assert!(font_feature_settings::parse(&context, &mut short_tag).is_err());
let mut illegal_tag = Parser::new("\"abcó\"");
assert!(font_feature_settings::parse(&context, &mut illegal_tag).is_err());
}
#[test]
fn font_feature_settings_to_css() {
assert_roundtrip_with_context!(font_feature_settings::parse, "normal");
assert_roundtrip_with_context!(font_feature_settings::parse, "\"abcd\"");
assert_roundtrip_with_context!(font_feature_settings::parse, "\"abcd\" on", "\"abcd\"");
assert_roundtrip_with_context!(font_feature_settings::parse, "\"abcd\" off");
assert_roundtrip_with_context!(font_feature_settings::parse, "\"abcd\" 4");
assert_roundtrip_with_context!(font_feature_settings::parse, "\"abcd\", \"efgh\"");
}

Просмотреть файл

@ -62,6 +62,7 @@ macro_rules! parse_longhand {
}
mod basic_shape;
mod font;
mod image;
mod inherited_text;
mod mask;