Skip to content
Permalink
Browse files

stylo: Clean up keyword values

  • Loading branch information
Manishearth committed Sep 23, 2017
1 parent df9d7cd commit cd6f68c5450451a78b8dc8a5755f178c20122629
@@ -2164,7 +2164,7 @@ fn static_assert() {
use values::specified::font::KeywordSize;
self.gecko.mSize = v.size().0;
self.gecko.mScriptUnconstrainedSize = v.size().0;
if let Some(info) = v.info {
if let Some(info) = v.keyword_info {
self.gecko.mFontSizeKeyword = match info.kw {
KeywordSize::XXSmall => structs::NS_STYLE_FONT_SIZE_XXSMALL,
KeywordSize::XSmall => structs::NS_STYLE_FONT_SIZE_XSMALL,
@@ -2385,14 +2385,14 @@ fn static_assert() {
structs::NS_STYLE_FONT_SIZE_NO_KEYWORD => {
return longhands::font_size::computed_value::T {
size: size,
info: None,
keyword_info: None,
}
}
_ => unreachable!("mFontSizeKeyword should be an absolute keyword or NO_KEYWORD")
};
longhands::font_size::computed_value::T {
size: size,
info: Some(KeywordInfo {
keyword_info: Some(KeywordInfo {
kw: kw,
factor: self.gecko.mFontSizeFactor,
offset: Au(self.gecko.mFontSizeOffset).into()
@@ -603,6 +603,7 @@ ${helpers.single_keyword_system("font-variant-caps",
use values::specified::AllowQuirks;
use values::specified::length::FontBaseSize;
use values::specified::font::{FONT_MEDIUM_PX, KeywordSize};
use values::computed::font::{KeywordInfo};

pub mod computed_value {
use values::computed::font;
@@ -616,17 +617,13 @@ ${helpers.single_keyword_system("font-variant-caps",
pub fn get_initial_value() -> computed_value::T {
computed_value::T {
size: Au::from_px(FONT_MEDIUM_PX).into(),
info: Some(::values::computed::font::KeywordInfo {
kw: KeywordSize::Medium,
factor: 1.,
offset: Au(0).into(),
})
keyword_info: Some(KeywordInfo::medium())
}
}

#[inline]
pub fn get_initial_specified_value() -> SpecifiedValue {
SpecifiedValue::Keyword(KeywordSize::Medium, 1., Au(0).into())
SpecifiedValue::Keyword(KeywordInfo::medium())
}


@@ -647,7 +644,7 @@ ${helpers.single_keyword_system("font-variant-caps",
}

if let Ok(kw) = input.try(KeywordSize::parse) {
return Ok(SpecifiedValue::Keyword(kw, 1., Au(0).into()))
return Ok(SpecifiedValue::Keyword(kw.into()))
}

try_match_ident_ignore_ascii_case! { input.expect_ident()?,
@@ -670,7 +667,7 @@ ${helpers.single_keyword_system("font-variant-caps",
context.builder.get_parent_font().gecko().mLanguage.raw::<nsIAtom>() ||
context.builder.get_font().gecko().mGenericID !=
context.builder.get_parent_font().gecko().mGenericID {
if let Some(info) = computed.info {
if let Some(info) = computed.keyword_info {
computed.size = context.maybe_zoom_text(info.kw.to_computed_value(context)
.scale_by(info.factor) + info.offset)
}
@@ -703,8 +700,8 @@ ${helpers.single_keyword_system("font-variant-caps",
// handle mathml scriptlevel changes
let kw_inherited_size = context.builder.get_parent_font()
.clone_font_size()
.info.map(|info| {
context.maybe_zoom_text(SpecifiedValue::Keyword(info.kw, info.factor, info.offset)
.keyword_info.map(|info| {
context.maybe_zoom_text(SpecifiedValue::Keyword(info)
.to_computed_value(context).size)
});
let mut font = context.builder.take_font();
@@ -2205,7 +2202,10 @@ ${helpers.single_keyword("-moz-math-variant",
let weight = 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: longhands::font_size::computed_value::T { size: Au(system.size).into(), info: None },
font_size: longhands::font_size::computed_value::T {
size: Au(system.size).into(),
keyword_info: None
},
font_weight: weight,
font_size_adjust: longhands::font_size_adjust::computed_value
::T::from_gecko_adjust(system.sizeAdjust),
@@ -10,20 +10,20 @@ use style_traits::ToCss;
use values::computed::NonNegativeLength;
use values::specified::font as specified;

#[derive(Copy, Clone, PartialEq, Debug)]
#[derive(ToAnimatedValue, Animate, ToAnimatedZero, ComputeSquaredDistance)]
#[derive(Animate, ComputeSquaredDistance, ToAnimatedValue, ToAnimatedZero)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[cfg_attr(feature = "gecko", derive(MallocSizeOf))]
/// The computed value of font-size
pub struct FontSize {
/// The size.
pub size: NonNegativeLength,
/// If derived from a keyword, the keyword and additional transformations applied to it
pub info: Option<KeywordInfo>,
pub keyword_info: Option<KeywordInfo>,
}

#[derive(Copy, Clone, PartialEq, Debug)]
#[derive(ToAnimatedValue, Animate, ToAnimatedZero, ComputeSquaredDistance)]
#[derive(Animate, ComputeSquaredDistance, ToAnimatedValue, ToAnimatedZero)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[cfg_attr(feature = "gecko", derive(MallocSizeOf))]
/// Additional information for keyword-derived font sizes.
@@ -45,6 +45,21 @@ impl KeywordInfo {
offset: self.offset.scale_by(factor) + offset,
}
}

/// KeywordInfo value for font-size: medium
pub fn medium() -> Self {
specified::KeywordSize::Medium.into()
}
}

impl From<specified::KeywordSize> for KeywordInfo {
fn from(x: specified::KeywordSize) -> Self {
KeywordInfo {
kw: x,
factor: 1.,
offset: Au(0).into(),
}
}
}

impl FontSize {
@@ -32,7 +32,7 @@ pub enum FontSize {
/// go into the ratio, and the remaining units all computed together
/// will go into the offset.
/// See bug 1355707.
Keyword(KeywordSize, f32, NonNegativeLength),
Keyword(computed::KeywordInfo),
/// font-size: smaller
Smaller,
/// font-size: larger
@@ -45,7 +45,7 @@ impl ToCss for FontSize {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
FontSize::Length(ref lop) => lop.to_css(dest),
FontSize::Keyword(kw, _, _) => kw.to_css(dest),
FontSize::Keyword(info) => info.kw.to_css(dest),
FontSize::Smaller => dest.write_str("smaller"),
FontSize::Larger => dest.write_str("larger"),
FontSize::System(sys) => sys.to_css(dest),
@@ -60,8 +60,8 @@ impl From<LengthOrPercentage> for FontSize {
}

/// CSS font keywords
#[derive(Animate, ComputeSquaredDistance, ToAnimatedValue, ToAnimatedZero)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(ToAnimatedValue, Animate, ToAnimatedZero, ComputeSquaredDistance)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[cfg_attr(feature = "gecko", derive(MallocSizeOf))]
#[allow(missing_docs)]
@@ -226,7 +226,7 @@ impl FontSize {
6 => KeywordSize::XXLarge,
// If value is greater than 7, let it be 7.
_ => KeywordSize::XXXLarge,
}, 1., Au(0).into())
}.into())
}

/// Compute it against a given base font size
@@ -239,7 +239,7 @@ impl FontSize {

let compose_keyword = |factor| {
context.style().get_parent_font()
.clone_font_size().info
.clone_font_size().keyword_info
.map(|i| i.compose(factor, Au(0).into()))
};
let mut info = None;
@@ -274,7 +274,7 @@ impl FontSize {
let parent = context.style().get_parent_font().clone_font_size();
// if we contain em/% units and the parent was keyword derived, this is too
// Extract the ratio/offset and compose it
if (calc.em.is_some() || calc.percentage.is_some()) && parent.info.is_some() {
if (calc.em.is_some() || calc.percentage.is_some()) && parent.keyword_info.is_some() {
let ratio = calc.em.unwrap_or(0.) + calc.percentage.map_or(0., |pc| pc.0);
// Compute it, but shave off the font-relative part (em, %)
// This will mean that other font-relative units like ex and ch will be computed against
@@ -284,19 +284,15 @@ impl FontSize {
// recomputes new ones. This is enough of an edge case to not really matter.
let abs = calc.to_computed_value_zoomed(context, FontBaseSize::Custom(Au(0).into()))
.length_component().into();
info = parent.info.map(|i| i.compose(ratio, abs));
info = parent.keyword_info.map(|i| i.compose(ratio, abs));
}
let calc = calc.to_computed_value_zoomed(context, base_size);
calc.to_used_value(Some(base_size.resolve(context))).unwrap().into()
}
FontSize::Keyword(key, fraction, offset) => {
FontSize::Keyword(i) => {
// As a specified keyword, this is keyword derived
info = Some(computed::KeywordInfo {
kw: key,
factor: fraction,
offset: offset,
});
context.maybe_zoom_text(key.to_computed_value(context).scale_by(fraction) + offset)
info = Some(i);
context.maybe_zoom_text(i.kw.to_computed_value(context).scale_by(i.factor) + i.offset)
}
FontSize::Smaller => {
info = compose_keyword(1. / LARGER_FONT_SIZE_RATIO);
@@ -318,7 +314,10 @@ impl FontSize {
}
}
};
computed::FontSize { size, info }
computed::FontSize {
size: size,
keyword_info: info,
}
}
}

0 comments on commit cd6f68c

Please sign in to comment.
You can’t perform that action at this time.