Skip to content

Commit

Permalink
stylo: Special-case initial-computation of font-size
Browse files Browse the repository at this point in the history
MozReview-Commit-ID: Ff6kt8RLChI
  • Loading branch information
Manishearth committed Mar 24, 2017
1 parent fddddc9 commit 3139a90
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 13 deletions.
5 changes: 5 additions & 0 deletions components/style/properties/gecko.mako.rs
Expand Up @@ -80,6 +80,7 @@ pub struct ComputedValues {
shareable: bool,
pub writing_mode: WritingMode,
pub root_font_size: Au,
pub font_size_keyword: Option<longhands::font_size::KeywordSize>,
}

impl ComputedValues {
Expand All @@ -89,6 +90,7 @@ impl ComputedValues {
shareable: parent.shareable,
writing_mode: parent.writing_mode,
root_font_size: parent.root_font_size,
font_size_keyword: parent.font_size_keyword,
% for style_struct in data.style_structs:
% if style_struct.inherited:
${style_struct.ident}: parent.${style_struct.ident}.clone(),
Expand All @@ -103,6 +105,7 @@ impl ComputedValues {
shareable: bool,
writing_mode: WritingMode,
root_font_size: Au,
font_size_keyword: Option<longhands::font_size::KeywordSize>,
% for style_struct in data.style_structs:
${style_struct.ident}: Arc<style_structs::${style_struct.name}>,
% endfor
Expand All @@ -112,6 +115,7 @@ impl ComputedValues {
shareable: shareable,
writing_mode: writing_mode,
root_font_size: root_font_size,
font_size_keyword: font_size_keyword,
% for style_struct in data.style_structs:
${style_struct.ident}: ${style_struct.ident},
% endfor
Expand All @@ -124,6 +128,7 @@ impl ComputedValues {
shareable: true,
writing_mode: WritingMode::empty(), // FIXME(bz): This seems dubious
root_font_size: longhands::font_size::get_initial_value(), // FIXME(bz): Also seems dubious?
font_size_keyword: Some(Default::default()),
% for style_struct in data.style_structs:
${style_struct.ident}: style_structs::${style_struct.name}::default(pres_context),
% endfor
Expand Down
33 changes: 27 additions & 6 deletions components/style/properties/helpers.mako.rs
Expand Up @@ -266,6 +266,13 @@
match *value {
DeclaredValue::Value(ref specified_value) => {
let computed = specified_value.to_computed_value(context);
% if property.ident == "font_size":
if let longhands::font_size::SpecifiedValue::Keyword(kw) = **specified_value {
context.mutate_style().font_size_keyword = Some(kw);
} else {
context.mutate_style().font_size_keyword = None;
}
% endif
% if property.has_uncacheable_values:
context.mutate_style().mutate_${data.current_style_struct.name_lower}()
.set_${property.ident}(computed, cacheable ${maybe_wm});
Expand All @@ -280,12 +287,22 @@
CSSWideKeyword::Unset |
% endif
CSSWideKeyword::Initial => {
// We assume that it's faster to use copy_*_from rather than
// set_*(get_initial_value());
let initial_struct = default_style
.get_${data.current_style_struct.name_lower}();
context.mutate_style().mutate_${data.current_style_struct.name_lower}()
.copy_${property.ident}_from(initial_struct ${maybe_wm});
% if property.ident == "font_size":
// font-size's default ("medium") does not always
// compute to the same value and depends on the font
let computed = longhands::font_size::get_initial_specified_value()
.to_computed_value(context);
context.mutate_style().mutate_${data.current_style_struct.name_lower}()
.set_font_size(computed);
context.mutate_style().font_size_keyword = Some(Default::default());
% else:
// We assume that it's faster to use copy_*_from rather than
// set_*(get_initial_value());
let initial_struct = default_style
.get_${data.current_style_struct.name_lower}();
context.mutate_style().mutate_${data.current_style_struct.name_lower}()
.copy_${property.ident}_from(initial_struct ${maybe_wm});
% endif
},
% if data.current_style_struct.inherited:
CSSWideKeyword::Unset |
Expand All @@ -300,6 +317,10 @@
inherited_style.get_${data.current_style_struct.name_lower}();
context.mutate_style().mutate_${data.current_style_struct.name_lower}()
.copy_${property.ident}_from(inherited_struct ${maybe_wm});
% if property.ident == "font_size":
context.mutate_style().font_size_keyword =
context.inherited_style.font_size_keyword;
% endif
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions components/style/properties/longhand/font.mako.rs
Expand Up @@ -465,6 +465,12 @@ ${helpers.single_keyword("font-variant-caps",
}
}

impl Default for KeywordSize {
fn default() -> Self {
Medium
}
}

impl ToCss for KeywordSize {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
dest.write_str(match *self {
Expand Down
36 changes: 29 additions & 7 deletions components/style/properties/properties.mako.rs
Expand Up @@ -1502,6 +1502,8 @@ pub struct ComputedValues {
pub writing_mode: WritingMode,
/// The root element's computed font size.
pub root_font_size: Au,
/// The keyword behind the current font-size property, if any
pub font_size_keyword: Option<longhands::font_size::KeywordSize>,
}

#[cfg(feature = "servo")]
Expand All @@ -1511,6 +1513,7 @@ impl ComputedValues {
shareable: bool,
writing_mode: WritingMode,
root_font_size: Au,
font_size_keyword: Option<longhands::font_size::KeywordSize>,
% for style_struct in data.active_style_structs():
${style_struct.ident}: Arc<style_structs::${style_struct.name}>,
% endfor
Expand All @@ -1520,6 +1523,7 @@ impl ComputedValues {
shareable: shareable,
writing_mode: writing_mode,
root_font_size: root_font_size,
font_size_keyword: font_size_keyword,
% for style_struct in data.active_style_structs():
${style_struct.ident}: ${style_struct.ident},
% endfor
Expand Down Expand Up @@ -1843,6 +1847,7 @@ mod lazy_static_module {
shareable: true,
writing_mode: WritingMode::empty(),
root_font_size: longhands::font_size::get_initial_value(),
font_size_keyword: Some(Default::default()),
};
}
}
Expand Down Expand Up @@ -1985,6 +1990,7 @@ pub fn apply_declarations<'a, F, I>(device: &Device,
flags.contains(SHAREABLE),
WritingMode::empty(),
inherited_style.root_font_size,
inherited_style.font_size_keyword,
% for style_struct in data.active_style_structs():
% if style_struct.inherited:
inherited_style.clone_${style_struct.name_lower}(),
Expand All @@ -1998,6 +2004,7 @@ pub fn apply_declarations<'a, F, I>(device: &Device,
flags.contains(SHAREABLE),
WritingMode::empty(),
inherited_style.root_font_size,
inherited_style.font_size_keyword,
% for style_struct in data.active_style_structs():
inherited_style.clone_${style_struct.name_lower}(),
% endfor
Expand Down Expand Up @@ -2075,6 +2082,14 @@ pub fn apply_declarations<'a, F, I>(device: &Device,
{
continue
}

<% maybe_to_physical = ".to_physical(writing_mode)" if category_to_cascade_now != "early" else "" %>
let physical_longhand_id = longhand_id ${maybe_to_physical};
if seen.contains(physical_longhand_id) {
continue
}
seen.insert(physical_longhand_id);

% if category_to_cascade_now == "early":
if LonghandId::FontSize == longhand_id {
font_size = Some(declaration);
Expand All @@ -2086,13 +2101,6 @@ pub fn apply_declarations<'a, F, I>(device: &Device,
}
% endif

<% maybe_to_physical = ".to_physical(writing_mode)" if category_to_cascade_now != "early" else "" %>
let physical_longhand_id = longhand_id ${maybe_to_physical};
if seen.contains(physical_longhand_id) {
continue
}
seen.insert(physical_longhand_id);

let discriminant = longhand_id as usize;
(CASCADE_PROPERTY[discriminant])(declaration,
inherited_style,
Expand Down Expand Up @@ -2135,6 +2143,20 @@ pub fn apply_declarations<'a, F, I>(device: &Device,
&mut cacheable,
&mut cascade_info,
error_reporter);
} else if let Some(kw) = inherited_style.font_size_keyword {
// Font size keywords will inherit as keywords and be recomputed
// each time.
let discriminant = LonghandId::FontSize as usize;
let size = PropertyDeclaration::FontSize(
longhands::font_size::SpecifiedValue::Keyword(kw)
);
(CASCADE_PROPERTY[discriminant])(&size,
inherited_style,
default_style,
&mut context,
&mut cacheable,
&mut cascade_info,
error_reporter);
}
% endif
% endfor
Expand Down

0 comments on commit 3139a90

Please sign in to comment.