style: Move font-weight outside of mako

This commit is contained in:
Connor Brewster 2017-11-01 11:06:40 -05:00
parent b23131abf1
commit 285d8bb58b
5 changed files with 168 additions and 167 deletions

View file

@ -18,6 +18,90 @@ use values::specified::length::{AU_PER_PT, AU_PER_PX, FontBaseSize};
const DEFAULT_SCRIPT_MIN_SIZE_PT: u32 = 8;
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq, ToCss)]
/// A specified font-weight value
pub enum FontWeight {
/// Normal variant
Normal,
/// Bold variant
Bold,
/// Bolder variant
Bolder,
/// Lighter variant
Lighter,
/// Computed weight variant
Weight(computed::FontWeight),
/// System font varaint
System(SystemFont),
}
impl FontWeight {
/// Get a specified FontWeight from a gecko keyword
pub fn from_gecko_keyword(kw: u32) -> Self {
computed::FontWeight::from_int(kw as i32).map(FontWeight::Weight)
.expect("Found unexpected value in style struct for font-weight property")
}
/// Get a specified FontWeight from a SystemFont
pub fn system_font(f: SystemFont) -> Self {
FontWeight::System(f)
}
/// Retreive a SystemFont from FontWeight
pub fn get_system(&self) -> Option<SystemFont> {
if let FontWeight::System(s) = *self {
Some(s)
} else {
None
}
}
}
impl Parse for FontWeight {
fn parse<'i, 't>(context: &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),
"bolder" => Ok(FontWeight::Bolder),
"lighter" => Ok(FontWeight::Lighter),
_ => Err(())
}
});
result.or_else(|_| computed::FontWeight::parse(context, input).map(FontWeight::Weight))
}
}
impl ToComputedValue for FontWeight {
type ComputedValue = computed::FontWeight;
#[inline]
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
match *self {
FontWeight::Weight(weight) => weight,
FontWeight::Normal => computed::FontWeight::normal(),
FontWeight::Bold => computed::FontWeight::bold(),
FontWeight::Bolder =>
context.builder.get_parent_font().clone_font_weight().bolder(),
FontWeight::Lighter =>
context.builder.get_parent_font().clone_font_weight().lighter(),
#[cfg(feature = "gecko")]
FontWeight::System(_) =>
context.cached_system_font.as_ref().unwrap().font_weight.clone(),
#[cfg(not(feature = "gecko"))]
FontWeight::System(_) =>
unreachable!(),
}
}
#[inline]
fn from_computed_value(computed: &computed::FontWeight) -> Self {
FontWeight::Weight(*computed)
}
}
#[derive(Clone, Debug, MallocSizeOf, PartialEq)]
/// A specified font-size value
pub enum FontSize {