Skip to content

Commit

Permalink
stylo: System font support for font-weight
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Apr 21, 2017
1 parent b0dcb72 commit 5184f29
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion components/style/properties/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

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]:
Expand Down
5 changes: 1 addition & 4 deletions components/style/properties/gecko.mako.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1530,10 +1530,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) {
Expand Down
38 changes: 37 additions & 1 deletion components/style/properties/longhand/font.mako.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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 {
Expand All @@ -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<SpecifiedValue, ()> {
input.try(|input| {
Expand Down Expand Up @@ -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<SystemFont> {
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<Self, ()> {
Expand All @@ -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!(),
}
}
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
</%self:nongecko_unreachable>
}
}
}

Expand Down Expand Up @@ -2014,9 +2046,13 @@ ${helpers.single_keyword("-moz-math-variant",
quoted: true
})
}).collect::<Vec<_>>();
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
Expand Down
2 changes: 1 addition & 1 deletion components/style/properties/shorthand/font.mako.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5184f29

Please sign in to comment.