Skip to content

Commit

Permalink
Add unicode-range descriptor to @font-face
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Feb 2, 2017
1 parent 0f351cb commit 4701850
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
62 changes: 55 additions & 7 deletions components/style/font_face.rs
Expand Up @@ -8,7 +8,8 @@

#![deny(missing_docs)]

#[cfg(feature = "gecko")] use computed_values::{font_style, font_weight, font_stretch};
#[cfg(feature = "gecko")]
use computed_values::{font_style, font_weight, font_stretch};
use computed_values::font_family::FamilyName;
use cssparser::{AtRuleParser, DeclarationListParser, DeclarationParser, Parser};
use parser::{ParserContext, log_css_error, Parse};
Expand Down Expand Up @@ -141,12 +142,6 @@ impl<'a, 'b> AtRuleParser for FontFaceRuleParser<'a, 'b> {
type AtRule = ();
}

impl Parse for Vec<Source> {
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
input.parse_comma_separated(|input| Source::parse(context, input))
}
}

impl Parse for Source {
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Source, ()> {
if input.try(|input| input.expect_function_matching("local")).is_ok() {
Expand Down Expand Up @@ -301,6 +296,11 @@ font_face_descriptors! {

/// The stretch of this font face
"font-stretch" stretch: font_stretch::T = font_stretch::T::normal,

/// The ranges of code points outside of which this font face should not be used.
"unicode-range" unicode_range: Vec<unicode_range::Range> = vec![
unicode_range::Range { start: 0, end: unicode_range::MAX }
],
]
}

Expand All @@ -316,3 +316,51 @@ font_face_descriptors! {
optional descriptors = [
]
}

/// https://drafts.csswg.org/css-fonts/#unicode-range-desc
#[cfg(feature = "gecko")]
pub mod unicode_range {
use cssparser::{Parser, Token};
use parser::{ParserContext, Parse};
use std::fmt;
use style_traits::{ToCss, CommaSeparated};

/// Maximum value of the end of a range
pub const MAX: u32 = ::std::char::MAX as u32;

/// A single range: https://drafts.csswg.org/css-fonts/#urange-value
#[derive(Debug, PartialEq, Eq)]
pub struct Range {
/// Start of the range, inclusive
pub start: u32,

/// End of the range, inclusive
pub end: u32,
}

impl CommaSeparated for Range {}

impl Parse for Range {
fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
// FIXME: The unicode-range token has been removed from the CSS Syntax spec,
// cssparser should be updated accordingly
// and implement https://drafts.csswg.org/css-syntax/#urange instead
match input.next() {
Ok(Token::UnicodeRange(start, end)) => {
if end <= MAX && start <= end {
Ok(Range { start: start, end: end })
} else {
Err(())
}
}
_ => Err(())
}
}
}

impl ToCss for Range {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
Token::UnicodeRange(self.start, self.end).to_css(dest)
}
}
}
7 changes: 7 additions & 0 deletions components/style/parser.rs
Expand Up @@ -11,6 +11,7 @@ use error_reporting::ParseErrorReporter;
#[cfg(feature = "gecko")]
use gecko_bindings::sugar::refptr::{GeckoArcPrincipal, GeckoArcURI};
use servo_url::ServoUrl;
use style_traits::CommaSeparated;
use stylesheets::{MemoryHoleReporter, Origin};

/// Extra data that the style backend may need to parse stylesheets.
Expand Down Expand Up @@ -102,3 +103,9 @@ pub trait Parse : Sized {
/// Returns an error on failure.
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()>;
}

impl<T> Parse for Vec<T> where T: Parse + CommaSeparated {
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
input.parse_comma_separated(|input| T::parse(context, input))
}
}

0 comments on commit 4701850

Please sign in to comment.