From aa52e644fe69fcd59b43f77dd5db4bd0f5678182 Mon Sep 17 00:00:00 2001 From: Boris Chiou Date: Tue, 5 Dec 2017 13:50:50 +0800 Subject: [PATCH] Implement the Servo parser for FontFaceSet Web API. --- components/style/gecko/generated/bindings.rs | 3 + ports/geckolib/glue.rs | 58 ++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/components/style/gecko/generated/bindings.rs b/components/style/gecko/generated/bindings.rs index 0d6bc333b561..a6aa8c2bf967 100644 --- a/components/style/gecko/generated/bindings.rs +++ b/components/style/gecko/generated/bindings.rs @@ -2162,6 +2162,9 @@ extern "C" { } extern "C" { # [ link_name = "\u{1}_Servo_ParseFontDescriptor" ] pub fn Servo_ParseFontDescriptor ( desc_id : nsCSSFontDesc , value : * const nsAString , data : * mut RawGeckoURLExtraData , arg1 : nsCSSValueBorrowedMut , ) -> bool ; +} extern "C" { + # [ link_name = "\u{1}_Servo_ParseFontShorthandForMatching" ] + pub fn Servo_ParseFontShorthandForMatching ( value : * const nsAString , data : * mut RawGeckoURLExtraData , family : * mut RefPtr < SharedFontList > , style : nsCSSValueBorrowedMut , stretch : nsCSSValueBorrowedMut , weight : nsCSSValueBorrowedMut , ) -> bool ; } extern "C" { # [ link_name = "\u{1}_Gecko_CreateCSSErrorReporter" ] pub fn Gecko_CreateCSSErrorReporter ( sheet : * mut ServoStyleSheet , loader : * mut Loader , uri : * mut nsIURI , ) -> * mut ErrorReporter ; diff --git a/ports/geckolib/glue.rs b/ports/geckolib/glue.rs index 46b0dbb5607c..fdd491985f1a 100644 --- a/ports/geckolib/glue.rs +++ b/ports/geckolib/glue.rs @@ -4810,6 +4810,64 @@ pub extern "C" fn Servo_ParseFontDescriptor( true } +#[no_mangle] +pub extern "C" fn Servo_ParseFontShorthandForMatching( + value: *const nsAString, + data: *mut URLExtraData, + family: *mut structs::RefPtr, + style: nsCSSValueBorrowedMut, + stretch: nsCSSValueBorrowedMut, + weight: nsCSSValueBorrowedMut +) -> bool { + use style::properties::longhands::{font_stretch, font_style}; + use style::properties::shorthands::font; + use style::values::specified::font::{FontFamily, FontWeight}; + + let string = unsafe { (*value).to_string() }; + let mut input = ParserInput::new(&string); + let mut parser = Parser::new(&mut input); + let url_data = unsafe { RefPtr::from_ptr_ref(&data) }; + let context = ParserContext::new( + Origin::Author, + url_data, + Some(CssRuleType::FontFace), + ParsingMode::DEFAULT, + QuirksMode::NoQuirks, + ); + + let font = match parser.parse_entirely(|f| font::parse_value(&context, f)) { + Ok(f) => f, + Err(..) => return false, + }; + + // The system font is not acceptable, so we return false. + let family = unsafe { &mut *family }; + match font.font_family { + FontFamily::Values(list) => family.set_move(list.0), + FontFamily::System(_) => return false, + } + style.set_from(match font.font_style { + font_style::SpecifiedValue::Keyword(kw) => kw, + font_style::SpecifiedValue::System(_) => return false, + }); + stretch.set_from(match font.font_stretch { + font_stretch::SpecifiedValue::Keyword(kw) => kw, + font_stretch::SpecifiedValue::System(_) => return false, + }); + match font.font_weight { + FontWeight::Weight(w) => weight.set_from(w), + FontWeight::Normal => weight.set_enum(structs::NS_STYLE_FONT_WEIGHT_NORMAL as i32), + FontWeight::Bold => weight.set_enum(structs::NS_STYLE_FONT_WEIGHT_BOLD as i32), + // Resolve relative font weights against the initial of font-weight + // (normal, which is equivalent to 400). + FontWeight::Bolder => weight.set_enum(structs::NS_FONT_WEIGHT_BOLD as i32), + FontWeight::Lighter => weight.set_enum(structs::NS_FONT_WEIGHT_THIN as i32), + FontWeight::System(_) => return false, + } + + true +} + #[no_mangle] pub unsafe extern "C" fn Servo_SourceSizeList_Parse( value: *const nsACString,