layout: Remove FontStyle in favor of using the font style struct

directly, and optimize `get_layout_font_group()` to use a small vector.

Seems to be a 38% layout win on a site I tested with a lot of text.
This commit is contained in:
Patrick Walton 2014-10-15 22:09:15 -07:00
parent f3066c70da
commit a6fcec468f
6 changed files with 52 additions and 87 deletions

View file

@ -32,7 +32,6 @@ use gfx::display_list::{LineDisplayItemClass, OpaqueNode, PseudoDisplayItemClass
use gfx::display_list::{SidewaysLeft, SidewaysRight, SolidColorDisplayItem};
use gfx::display_list::{SolidColorDisplayItemClass, StackingLevel, TextDisplayItem};
use gfx::display_list::{TextDisplayItemClass, Upright};
use gfx::font::FontStyle;
use gfx::text::glyph::CharIndex;
use gfx::text::text_run::TextRun;
use script_traits::UntrustedNodeAddress;
@ -707,8 +706,8 @@ impl Fragment {
}
pub fn calculate_line_height(&self, layout_context: &LayoutContext) -> Au {
let font_style = text::computed_style_to_font_style(&*self.style);
let font_metrics = text::font_metrics_for_style(layout_context.font_context(), &font_style);
let font_style = self.style.get_font();
let font_metrics = text::font_metrics_for_style(layout_context.font_context(), font_style);
text::line_height_from_style(&*self.style, &font_metrics)
}
@ -862,11 +861,6 @@ impl Fragment {
}
}
/// Converts this fragment's computed style to a font style used for rendering.
pub fn font_style(&self) -> FontStyle {
text::computed_style_to_font_style(self.style())
}
#[inline(always)]
pub fn style<'a>(&'a self) -> &'a ComputedValues {
&*self.style
@ -1834,9 +1828,9 @@ impl Fragment {
InlineBlockFragment(ref info) => {
// See CSS 2.1 § 10.8.1.
let block_flow = info.flow_ref.deref().as_immutable_block();
let font_style = text::computed_style_to_font_style(&*self.style);
let font_style = self.style.get_font();
let font_metrics = text::font_metrics_for_style(layout_context.font_context(),
&font_style);
font_style);
InlineMetrics::from_block_height(&font_metrics,
block_flow.base.position.size.block +
block_flow.fragment.margin.block_start_end())
@ -1872,7 +1866,7 @@ impl Fragment {
match (&self.specific, &other.specific) {
(&UnscannedTextFragment(_), &UnscannedTextFragment(_)) => {
// FIXME: Should probably use a whitelist of styles that can safely differ (#3165)
self.font_style() == other.font_style() &&
self.style().get_font() == other.style().get_font() &&
self.text_decoration() == other.text_decoration() &&
self.white_space() == other.white_space()
}

View file

@ -930,8 +930,8 @@ impl InlineFlow {
return (Au(0), Au(0))
}
let font_style = text::computed_style_to_font_style(style);
let font_metrics = text::font_metrics_for_style(font_context, &font_style);
let font_style = style.get_font();
let font_metrics = text::font_metrics_for_style(font_context, font_style);
let line_height = text::line_height_from_style(style, &font_metrics);
let inline_metrics = InlineMetrics::from_font_metrics(&font_metrics, line_height);
@ -944,8 +944,8 @@ impl InlineFlow {
match frag.inline_context {
Some(ref inline_context) => {
for style in inline_context.styles.iter() {
let font_style = text::computed_style_to_font_style(&**style);
let font_metrics = text::font_metrics_for_style(font_context, &font_style);
let font_style = style.get_font();
let font_metrics = text::font_metrics_for_style(font_context, font_style);
let line_height = text::line_height_from_style(&**style, &font_metrics);
let inline_metrics = InlineMetrics::from_font_metrics(&font_metrics,
line_height);

View file

@ -9,7 +9,7 @@
use flow::Flow;
use fragment::{Fragment, ScannedTextFragment, ScannedTextFragmentInfo, UnscannedTextFragment};
use gfx::font::{FontMetrics, FontStyle, RunMetrics};
use gfx::font::{FontMetrics,RunMetrics};
use gfx::font_context::FontContext;
use gfx::text::glyph::CharIndex;
use gfx::text::text_run::TextRun;
@ -17,8 +17,10 @@ use gfx::text::util::{CompressWhitespaceNewline, transform_text, CompressNone};
use servo_util::geometry::Au;
use servo_util::logical_geometry::{LogicalSize, WritingMode};
use servo_util::range::Range;
use servo_util::smallvec::SmallVec;
use style::ComputedValues;
use style::computed_values::{font_family, line_height, text_orientation, white_space};
use style::computed_values::{line_height, text_orientation, white_space};
use style::style_structs::Font as FontStyle;
use sync::Arc;
struct NewLinePositions {
@ -121,7 +123,7 @@ impl TextRunScanner {
_ => fail!("Expected an unscanned text fragment!"),
};
let font_style = old_fragment.font_style();
let font_style = old_fragment.style().get_font();
let compression = match old_fragment.white_space() {
white_space::normal | white_space::nowrap => CompressWhitespaceNewline,
@ -141,7 +143,7 @@ impl TextRunScanner {
// TODO(#177): Text run creation must account for the renderability of text by
// font group fonts. This is probably achieved by creating the font group above
// and then letting `FontGroup` decide which `Font` to stick into the text run.
let fontgroup = font_context.get_layout_font_group_for_style(&font_style);
let fontgroup = font_context.get_layout_font_group_for_style(font_style);
let run = box fontgroup.create_textrun(
transformed_text.clone());
@ -164,8 +166,8 @@ impl TextRunScanner {
// font group fonts. This is probably achieved by creating the font group above
// and then letting `FontGroup` decide which `Font` to stick into the text run.
let in_fragment = &in_fragments[self.clump.begin().to_uint()];
let font_style = in_fragment.font_style();
let fontgroup = font_context.get_layout_font_group_for_style(&font_style);
let font_style = in_fragment.style().get_font();
let fontgroup = font_context.get_layout_font_group_for_style(font_style);
let compression = match in_fragment.white_space() {
white_space::normal | white_space::nowrap => CompressWhitespaceNewline,
@ -216,9 +218,8 @@ impl TextRunScanner {
// sequence. If no clump takes ownership, however, it will leak.
let clump = self.clump;
let run = if clump.length() != CharIndex(0) && run_str.len() > 0 {
Some(Arc::new(box TextRun::new(
&mut *fontgroup.fonts[0].borrow_mut(),
run_str.to_string())))
Some(Arc::new(box TextRun::new(&mut *fontgroup.fonts.get(0).borrow_mut(),
run_str.to_string())))
} else {
None
};
@ -287,34 +288,7 @@ fn bounding_box_for_run_metrics(metrics: &RunMetrics, writing_mode: WritingMode)
pub fn font_metrics_for_style(font_context: &mut FontContext, font_style: &FontStyle)
-> FontMetrics {
let fontgroup = font_context.get_layout_font_group_for_style(font_style);
fontgroup.fonts[0].borrow().metrics.clone()
}
/// Converts a computed style to a font style used for rendering.
///
/// FIXME(pcwalton): This should not be necessary; just make the font part of the style sharable
/// with the display list somehow. (Perhaps we should use an ARC.)
pub fn computed_style_to_font_style(style: &ComputedValues) -> FontStyle {
debug!("(font style) start");
// FIXME: Too much allocation here.
let mut font_families = style.get_font().font_family.iter().map(|family| {
match *family {
font_family::FamilyName(ref name) => (*name).clone(),
}
});
debug!("(font style) font families: `{:?}`", font_families);
let font_size = style.get_font().font_size.to_f64().unwrap() / 60.0;
debug!("(font style) font size: `{:f}px`", font_size);
FontStyle {
pt_size: font_size,
weight: style.get_font().font_weight,
style: style.get_font().font_style,
variant: style.get_font().font_variant,
families: font_families.collect(),
}
fontgroup.fonts.get(0).borrow().metrics.clone()
}
/// Returns the line block-size needed by the given computed style and font size.