From 4a899530be3a32fd2d0f224f7638f54ac85078ad Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Tue, 21 Mar 2017 20:38:12 -0700 Subject: [PATCH] stylo: System font support for font-weight --- components/style/properties/data.py | 2 +- components/style/properties/gecko.mako.rs | 5 +-- .../style/properties/longhand/font.mako.rs | 38 ++++++++++++++++++- .../style/properties/shorthand/font.mako.rs | 2 +- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/components/style/properties/data.py b/components/style/properties/data.py index f19e4b76cd5..f53b73c9a8d 100644 --- a/components/style/properties/data.py +++ b/components/style/properties/data.py @@ -15,7 +15,7 @@ ALL_SIZES = [(size, False) for size in PHYSICAL_SIZES] + [(size, True) for size SYSTEM_FONT_LONGHANDS = """font_family font_size font_style font_variant_caps font_stretch font_kerning - font_variant_position""".split() + font_variant_position font_weight""".split() def maybe_moz_logical_alias(product, side, prop): if product == "gecko" and side[1]: diff --git a/components/style/properties/gecko.mako.rs b/components/style/properties/gecko.mako.rs index 2a75da38d9f..16e7a1d0cf4 100644 --- a/components/style/properties/gecko.mako.rs +++ b/components/style/properties/gecko.mako.rs @@ -1371,10 +1371,7 @@ fn static_assert() { ${impl_simple_copy('font_weight', 'mFont.weight')} pub fn clone_font_weight(&self) -> longhands::font_weight::computed_value::T { - debug_assert!(self.gecko.mFont.weight >= 100); - debug_assert!(self.gecko.mFont.weight <= 900); - debug_assert!(self.gecko.mFont.weight % 10 == 0); - unsafe { transmute(self.gecko.mFont.weight) } + unsafe { longhands::font_weight::computed_value::T::from_gecko_weight(self.gecko.mFont.weight) } } pub fn set_font_synthesis(&mut self, v: longhands::font_synthesis::computed_value::T) { diff --git a/components/style/properties/longhand/font.mako.rs b/components/style/properties/longhand/font.mako.rs index d15be7f76c8..321aa51fae7 100644 --- a/components/style/properties/longhand/font.mako.rs +++ b/components/style/properties/longhand/font.mako.rs @@ -310,6 +310,7 @@ ${helpers.single_keyword_system("font-variant-caps", use std::fmt; use style_traits::ToCss; use values::HasViewportPercentage; + use properties::longhands::system_font::SystemFont; no_viewport_percentage!(SpecifiedValue); @@ -323,6 +324,7 @@ ${helpers.single_keyword_system("font-variant-caps", % for weight in range(100, 901, 100): Weight${weight}, % endfor + System(SystemFont), } impl ToCss for SpecifiedValue { @@ -335,9 +337,11 @@ ${helpers.single_keyword_system("font-variant-caps", % for weight in range(100, 901, 100): SpecifiedValue::Weight${weight} => dest.write_str("${weight}"), % endfor + SpecifiedValue::System(_) => Ok(()) } } } + /// normal | bold | bolder | lighter | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900 pub fn parse(_context: &ParserContext, input: &mut Parser) -> Result { input.try(|input| { @@ -369,6 +373,19 @@ ${helpers.single_keyword_system("font-variant-caps", } } + impl SpecifiedValue { + pub fn system_font(f: SystemFont) -> Self { + SpecifiedValue::System(f) + } + pub fn get_system(&self) -> Option { + if let SpecifiedValue::System(s) = *self { + Some(s) + } else { + None + } + } + } + /// Used in @font-face, where relative keywords are not allowed. impl Parse for computed_value::T { fn parse(context: &ParserContext, input: &mut Parser) -> Result { @@ -379,7 +396,8 @@ ${helpers.single_keyword_system("font-variant-caps", SpecifiedValue::Normal => Ok(computed_value::T::Weight400), SpecifiedValue::Bold => Ok(computed_value::T::Weight700), SpecifiedValue::Bolder | - SpecifiedValue::Lighter => Err(()) + SpecifiedValue::Lighter => Err(()), + SpecifiedValue::System(..) => unreachable!(), } } } @@ -403,6 +421,15 @@ ${helpers.single_keyword_system("font-variant-caps", _ => false } } + + /// Obtain a Servo computed value from a Gecko computed font-weight + pub unsafe fn from_gecko_weight(weight: u16) -> Self { + use std::mem::transmute; + debug_assert!(weight >= 100); + debug_assert!(weight <= 900); + debug_assert!(weight % 10 == 0); + transmute(weight) + } } } impl ToCss for computed_value::T { @@ -457,6 +484,11 @@ ${helpers.single_keyword_system("font-variant-caps", computed_value::T::Weight800 => computed_value::T::Weight700, computed_value::T::Weight900 => computed_value::T::Weight700, }, + SpecifiedValue::System(_) => { + <%self:nongecko_unreachable> + context.style.cached_system_font.as_ref().unwrap().font_weight.clone() + + } } } @@ -1934,9 +1966,13 @@ ${helpers.single_keyword("-moz-math-variant", quoted: true }) }).collect::>(); + let weight = unsafe { + longhands::font_weight::computed_value::T::from_gecko_weight(system.weight) + }; let ret = ComputedSystemFont { font_family: longhands::font_family::computed_value::T(family), font_size: Au(system.size), + font_weight: weight, % for kwprop in kw_font_props: ${kwprop}: longhands::${kwprop}::computed_value::T::from_gecko_keyword( system.${to_camel_case_lower(kwprop.replace('font_', ''))} as u32 diff --git a/components/style/properties/shorthand/font.mako.rs b/components/style/properties/shorthand/font.mako.rs index 5e8c79bf4b0..30b06c524b3 100644 --- a/components/style/properties/shorthand/font.mako.rs +++ b/components/style/properties/shorthand/font.mako.rs @@ -46,7 +46,7 @@ % for name in SYSTEM_FONT_LONGHANDS: ${name}: ${name}::SpecifiedValue::system_font(sys), % endfor - % for name in gecko_sub_properties + "weight variant_caps stretch".split(): + % for name in gecko_sub_properties + "variant_caps stretch".split(): % if "font_" + name not in SYSTEM_FONT_LONGHANDS: font_${name}: font_${name}::get_initial_specified_value(), % endif