From 3139a9038644ce57ecfc7dd22a701e39bf61e19c Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Fri, 17 Mar 2017 19:48:20 -0700 Subject: [PATCH] stylo: Special-case initial-computation of font-size MozReview-Commit-ID: Ff6kt8RLChI --- components/style/properties/gecko.mako.rs | 5 +++ components/style/properties/helpers.mako.rs | 33 +++++++++++++---- .../style/properties/longhand/font.mako.rs | 6 ++++ .../style/properties/properties.mako.rs | 36 +++++++++++++++---- 4 files changed, 67 insertions(+), 13 deletions(-) diff --git a/components/style/properties/gecko.mako.rs b/components/style/properties/gecko.mako.rs index f8f25da8ed59..3bd5e6b31207 100644 --- a/components/style/properties/gecko.mako.rs +++ b/components/style/properties/gecko.mako.rs @@ -80,6 +80,7 @@ pub struct ComputedValues { shareable: bool, pub writing_mode: WritingMode, pub root_font_size: Au, + pub font_size_keyword: Option, } impl ComputedValues { @@ -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(), @@ -103,6 +105,7 @@ impl ComputedValues { shareable: bool, writing_mode: WritingMode, root_font_size: Au, + font_size_keyword: Option, % for style_struct in data.style_structs: ${style_struct.ident}: Arc, % endfor @@ -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 @@ -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 diff --git a/components/style/properties/helpers.mako.rs b/components/style/properties/helpers.mako.rs index f6c5dd299946..37f8b8f1184e 100644 --- a/components/style/properties/helpers.mako.rs +++ b/components/style/properties/helpers.mako.rs @@ -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}); @@ -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 | @@ -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 } } } diff --git a/components/style/properties/longhand/font.mako.rs b/components/style/properties/longhand/font.mako.rs index 7b1e829eaf0e..55550f2d7d82 100644 --- a/components/style/properties/longhand/font.mako.rs +++ b/components/style/properties/longhand/font.mako.rs @@ -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(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { dest.write_str(match *self { diff --git a/components/style/properties/properties.mako.rs b/components/style/properties/properties.mako.rs index 7cba960dc145..310f5dfe9f3e 100644 --- a/components/style/properties/properties.mako.rs +++ b/components/style/properties/properties.mako.rs @@ -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, } #[cfg(feature = "servo")] @@ -1511,6 +1513,7 @@ impl ComputedValues { shareable: bool, writing_mode: WritingMode, root_font_size: Au, + font_size_keyword: Option, % for style_struct in data.active_style_structs(): ${style_struct.ident}: Arc, % endfor @@ -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 @@ -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()), }; } } @@ -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}(), @@ -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 @@ -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); @@ -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, @@ -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