mirror of
https://github.com/servo/servo.git
synced 2025-08-07 06:25:32 +01:00
style: Move font-weight outside of mako
This commit is contained in:
parent
b23131abf1
commit
285d8bb58b
5 changed files with 168 additions and 167 deletions
|
@ -616,170 +616,13 @@ ${helpers.single_keyword_system("font-variant-caps",
|
|||
flags="APPLIES_TO_FIRST_LETTER APPLIES_TO_FIRST_LINE APPLIES_TO_PLACEHOLDER",
|
||||
animation_value_type="discrete")}
|
||||
|
||||
<%helpers:longhand name="font-weight" animation_value_type="ComputedValue"
|
||||
flags="APPLIES_TO_FIRST_LETTER APPLIES_TO_FIRST_LINE APPLIES_TO_PLACEHOLDER"
|
||||
spec="https://drafts.csswg.org/css-fonts/#propdef-font-weight">
|
||||
use properties::longhands::system_font::SystemFont;
|
||||
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq, ToCss)]
|
||||
pub enum SpecifiedValue {
|
||||
Normal,
|
||||
Bold,
|
||||
Bolder,
|
||||
Lighter,
|
||||
Weight(computed_value::T),
|
||||
System(SystemFont),
|
||||
}
|
||||
|
||||
/// normal | bold | bolder | lighter | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900
|
||||
pub fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
|
||||
-> Result<SpecifiedValue, ParseError<'i>> {
|
||||
let result = input.try(|input| {
|
||||
let ident = input.expect_ident().map_err(|_| ())?;
|
||||
match_ignore_ascii_case! { &ident,
|
||||
"normal" => Ok(SpecifiedValue::Normal),
|
||||
"bold" => Ok(SpecifiedValue::Bold),
|
||||
"bolder" => Ok(SpecifiedValue::Bolder),
|
||||
"lighter" => Ok(SpecifiedValue::Lighter),
|
||||
_ => Err(())
|
||||
}
|
||||
});
|
||||
result.or_else(|_| computed_value::T::parse(context, input).map(SpecifiedValue::Weight))
|
||||
}
|
||||
|
||||
impl SpecifiedValue {
|
||||
pub fn from_gecko_keyword(kw: u32) -> Self {
|
||||
computed_value::T::from_int(kw as i32).map(SpecifiedValue::Weight)
|
||||
.expect("Found unexpected value in style struct for font-weight property")
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub mod computed_value {
|
||||
/// As of CSS Fonts Module Level 3, only the following values are
|
||||
/// valid: 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900
|
||||
///
|
||||
/// However, system fonts may provide other values. Pango
|
||||
/// may provide 350, 380, and 1000 (on top of the existing values), for example.
|
||||
#[derive(Clone, ComputeSquaredDistance, Copy, Debug, Eq, Hash, MallocSizeOf, PartialEq,
|
||||
ToCss)]
|
||||
#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
|
||||
pub struct T(pub u16);
|
||||
|
||||
impl T {
|
||||
/// Value for normal
|
||||
pub fn normal() -> Self {
|
||||
T(400)
|
||||
}
|
||||
|
||||
/// Value for bold
|
||||
pub fn bold() -> Self {
|
||||
T(700)
|
||||
}
|
||||
|
||||
/// Convert from an integer to Weight
|
||||
pub fn from_int(n: i32) -> Result<Self, ()> {
|
||||
if n >= 100 && n <= 900 && n % 100 == 0 {
|
||||
Ok(T(n as u16))
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert from an Gecko weight
|
||||
pub fn from_gecko_weight(weight: u16) -> Self {
|
||||
// we allow a wider range of weights than is parseable
|
||||
// because system fonts may provide custom values
|
||||
T(weight)
|
||||
}
|
||||
|
||||
/// Weither this weight is bold
|
||||
pub fn is_bold(&self) -> bool {
|
||||
self.0 > 500
|
||||
}
|
||||
|
||||
/// Return the bolder weight
|
||||
pub fn bolder(self) -> Self {
|
||||
if self.0 < 400 {
|
||||
T(400)
|
||||
} else if self.0 < 600 {
|
||||
T(700)
|
||||
} else {
|
||||
T(900)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the lighter weight
|
||||
pub fn lighter(self) -> Self {
|
||||
if self.0 < 600 {
|
||||
T(100)
|
||||
} else if self.0 < 800 {
|
||||
T(400)
|
||||
} else {
|
||||
T(700)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Parse for computed_value::T {
|
||||
fn parse<'i, 't>(_: &ParserContext, input: &mut Parser<'i, 't>)
|
||||
-> Result<Self, ParseError<'i>> {
|
||||
Self::from_int(input.expect_integer()?)
|
||||
.map_err(|_| input.new_custom_error(StyleParseErrorKind::UnspecifiedError))
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_initial_value() -> computed_value::T {
|
||||
computed_value::T::normal()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_initial_specified_value() -> SpecifiedValue {
|
||||
SpecifiedValue::Normal
|
||||
}
|
||||
|
||||
impl ToComputedValue for SpecifiedValue {
|
||||
type ComputedValue = computed_value::T;
|
||||
|
||||
#[inline]
|
||||
fn to_computed_value(&self, context: &Context) -> computed_value::T {
|
||||
match *self {
|
||||
SpecifiedValue::Weight(weight) => weight,
|
||||
SpecifiedValue::Normal => computed_value::T::normal(),
|
||||
SpecifiedValue::Bold => computed_value::T::bold(),
|
||||
SpecifiedValue::Bolder =>
|
||||
context.builder.get_parent_font().clone_font_weight().bolder(),
|
||||
SpecifiedValue::Lighter =>
|
||||
context.builder.get_parent_font().clone_font_weight().lighter(),
|
||||
SpecifiedValue::System(_) => {
|
||||
<%self:nongecko_unreachable>
|
||||
context.cached_system_font.as_ref().unwrap().font_weight.clone()
|
||||
</%self:nongecko_unreachable>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &computed_value::T) -> Self {
|
||||
SpecifiedValue::Weight(*computed)
|
||||
}
|
||||
}
|
||||
</%helpers:longhand>
|
||||
${helpers.predefined_type("font-weight",
|
||||
"FontWeight",
|
||||
initial_value="computed::FontWeight::normal()",
|
||||
initial_specified_value="specified::FontWeight::Normal",
|
||||
animation_value_type="ComputedValue",
|
||||
flags="APPLIES_TO_FIRST_LETTER APPLIES_TO_FIRST_LINE APPLIES_TO_PLACEHOLDER",
|
||||
spec="https://drafts.csswg.org/css-fonts/#propdef-font-weight")}
|
||||
|
||||
<%helpers:longhand name="font-size" animation_value_type="NonNegativeLength"
|
||||
flags="APPLIES_TO_FIRST_LETTER APPLIES_TO_FIRST_LINE APPLIES_TO_PLACEHOLDER"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue