diff options
author | bors-servo <lbergstrom+bors@mozilla.com> | 2017-06-21 10:40:35 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-21 10:40:35 -0700 |
commit | 3e698340ac704921cb7bd98e623c8eaf349dfff4 (patch) | |
tree | 98eb408a4612abc4f58be8d0433935faea1cc4d0 | |
parent | b211664e877a3e4d712314fa7f3002fd19cff549 (diff) | |
parent | 632c5571a9382d77222dc49e14b5f3ad458fbec8 (diff) | |
download | servo-3e698340ac704921cb7bd98e623c8eaf349dfff4.tar.gz servo-3e698340ac704921cb7bd98e623c8eaf349dfff4.zip |
Auto merge of #17343 - ferjm:bug1355368.fontweight.keyword, r=upsuper
stylo: make font-weight descriptor in @font-face preserve keyword values
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix https://bugzilla.mozilla.org/show_bug.cgi?id=1355368
<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/17343)
<!-- Reviewable:end -->
-rw-r--r-- | components/style/font_face.rs | 62 | ||||
-rw-r--r-- | components/style/gecko/rules.rs | 14 |
2 files changed, 74 insertions, 2 deletions
diff --git a/components/style/font_face.rs b/components/style/font_face.rs index e8801b5a125..5e1b145287b 100644 --- a/components/style/font_face.rs +++ b/components/style/font_face.rs @@ -70,6 +70,66 @@ define_css_keyword_enum!(FontDisplay: "optional" => Optional); add_impls_for_keyword_enum!(FontDisplay); +/// A font-weight value for a @font-face rule. +/// The font-weight CSS property specifies the weight or boldness of the font. +#[cfg(feature = "gecko")] +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum FontWeight { + /// Numeric font weights for fonts that provide more than just normal and bold. + Weight(font_weight::T), + /// Normal font weight. Same as 400. + Normal, + /// Bold font weight. Same as 700. + Bold, +} + +#[cfg(feature = "gecko")] +impl ToCss for FontWeight { + fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + match *self { + FontWeight::Normal => dest.write_str("normal"), + FontWeight::Bold => dest.write_str("bold"), + FontWeight::Weight(ref weight) => weight.to_css(dest), + } + } +} + +#[cfg(feature = "gecko")] +impl Parse for FontWeight { + fn parse<'i, 't>(_: &ParserContext, input: &mut Parser<'i, 't>) + -> Result<FontWeight, ParseError<'i>> { + let result = input.try(|input| { + let ident = input.expect_ident().map_err(|_| ())?; + match_ignore_ascii_case! { &ident, + "normal" => Ok(FontWeight::Normal), + "bold" => Ok(FontWeight::Bold), + _ => Err(()) + } + }); + result.or_else(|_| { + FontWeight::from_int(input.expect_integer()?) + .map_err(|()| StyleParseError::UnspecifiedError.into()) + }) + } +} + +#[cfg(feature = "gecko")] +impl FontWeight { + fn from_int(kw: i32) -> Result<Self, ()> { + match kw { + 100 => Ok(FontWeight::Weight(font_weight::T::Weight100)), + 200 => Ok(FontWeight::Weight(font_weight::T::Weight200)), + 300 => Ok(FontWeight::Weight(font_weight::T::Weight300)), + 400 => Ok(FontWeight::Weight(font_weight::T::Weight400)), + 500 => Ok(FontWeight::Weight(font_weight::T::Weight500)), + 600 => Ok(FontWeight::Weight(font_weight::T::Weight600)), + 700 => Ok(FontWeight::Weight(font_weight::T::Weight700)), + 800 => Ok(FontWeight::Weight(font_weight::T::Weight800)), + 900 => Ok(FontWeight::Weight(font_weight::T::Weight900)), + _ => Err(()) + } + } +} /// Parse the block inside a `@font-face` rule. /// /// Note that the prelude parsing code lives in the `stylesheets` module. @@ -329,7 +389,7 @@ font_face_descriptors! { "font-style" style / mStyle: font_style::T = font_style::T::normal, /// The weight of this font face - "font-weight" weight / mWeight: font_weight::T = font_weight::T::Weight400 /* normal */, + "font-weight" weight / mWeight: FontWeight = FontWeight::Normal, /// The stretch of this font face "font-stretch" stretch / mStretch: font_stretch::T = font_stretch::T::normal, diff --git a/components/style/gecko/rules.rs b/components/style/gecko/rules.rs index f29da894e86..6a4a29ed97d 100644 --- a/components/style/gecko/rules.rs +++ b/components/style/gecko/rules.rs @@ -9,7 +9,7 @@ use computed_values::{font_feature_settings, font_stretch, font_style, font_weig use computed_values::font_family::FamilyName; use counter_style; use cssparser::UnicodeRange; -use font_face::{FontFaceRuleData, Source, FontDisplay}; +use font_face::{FontFaceRuleData, Source, FontDisplay, FontWeight}; use gecko_bindings::bindings; use gecko_bindings::structs::{self, nsCSSFontFaceRule, nsCSSValue}; use gecko_bindings::structs::{nsCSSCounterDesc, nsCSSCounterStyleRule}; @@ -34,6 +34,18 @@ impl ToNsCssValue for font_weight::T { } } +impl ToNsCssValue for FontWeight { + fn convert(self, nscssvalue: &mut nsCSSValue) { + match self { + FontWeight::Normal => + nscssvalue.set_enum(structs::NS_STYLE_FONT_WEIGHT_NORMAL as i32), + FontWeight::Bold => + nscssvalue.set_enum(structs::NS_STYLE_FONT_WEIGHT_BOLD as i32), + FontWeight::Weight(weight) => nscssvalue.set_integer(weight as i32), + } + } +} + impl ToNsCssValue for font_feature_settings::T { fn convert(self, nscssvalue: &mut nsCSSValue) { match self { |