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

@ -8,7 +8,9 @@ use std::string;
use std::rc::Rc;
use std::cell::RefCell;
use servo_util::cache::{Cache, HashCache};
use style::computed_values::{font_weight, font_style, font_variant};
use servo_util::smallvec::{SmallVec, SmallVec1};
use style::computed_values::{font_variant, font_weight};
use style::style_structs::Font as FontStyle;
use sync::Arc;
use servo_util::geometry::Au;
@ -82,22 +84,6 @@ pub struct FontMetrics {
pub line_gap: Au,
}
// TODO(Issue #179): eventually this will be split into the specified
// and used font styles. specified contains uninterpreted CSS font
// property values, while 'used' is attached to gfx::Font to descript
// the instance's properties.
//
// For now, the cases are differentiated with a typedef
#[deriving(Clone, PartialEq)]
pub struct FontStyle {
pub pt_size: f64,
pub weight: font_weight::T,
pub style: font_style::T,
pub families: Vec<String>,
pub variant: font_variant::T,
// TODO(Issue #198): font-stretch, text-decoration, size-adjust
}
pub type SpecifiedFontStyle = FontStyle;
pub type UsedFontStyle = FontStyle;
@ -174,13 +160,13 @@ impl Font {
}
pub struct FontGroup {
pub fonts: Vec<Rc<RefCell<Font>>>,
pub fonts: SmallVec1<Rc<RefCell<Font>>>,
}
impl FontGroup {
pub fn new(fonts: Vec<Rc<RefCell<Font>>>) -> FontGroup {
pub fn new(fonts: SmallVec1<Rc<RefCell<Font>>>) -> FontGroup {
FontGroup {
fonts: fonts
fonts: fonts,
}
}
@ -188,7 +174,7 @@ impl FontGroup {
assert!(self.fonts.len() > 0);
// TODO(Issue #177): Actually fall back through the FontGroup when a font is unsuitable.
TextRun::new(&mut *self.fonts[0].borrow_mut(), text.clone())
TextRun::new(&mut *self.fonts.get(0).borrow_mut(), text.clone())
}
}

View file

@ -13,6 +13,7 @@ use platform::font_template::FontTemplateData;
use font::FontHandleMethods;
use platform::font::FontHandle;
use servo_util::cache::HashCache;
use servo_util::smallvec::{SmallVec, SmallVec1};
use std::rc::Rc;
use std::cell::RefCell;
@ -122,18 +123,19 @@ impl FontContext {
// TODO: The font context holds a strong ref to the cached fonts
// so they will never be released. Find out a good time to drop them.
let desc = FontTemplateDescriptor::new(style.weight, style.style == font_style::italic);
let mut fonts: Vec<Rc<RefCell<Font>>> = vec!();
let desc = FontTemplateDescriptor::new(style.font_weight,
style.font_style == font_style::italic);
let mut fonts: SmallVec1<Rc<RefCell<Font>>> = SmallVec1::new();
for family in style.families.iter() {
for family in style.font_family.iter() {
// GWTODO: Check on real pages if this is faster as Vec() or HashMap().
let mut cache_hit = false;
for cached_font_entry in self.layout_font_cache.iter() {
if cached_font_entry.family == *family {
if cached_font_entry.family.as_slice() == family.name() {
let cached_font = cached_font_entry.font.borrow();
if cached_font.descriptor == desc &&
cached_font.requested_pt_size == style.pt_size &&
cached_font.variant == style.variant {
cached_font.requested_pt_size == style.font_size.to_subpx() &&
cached_font.variant == style.font_variant {
fonts.push(cached_font_entry.font.clone());
cache_hit = true;
break;
@ -142,16 +144,18 @@ impl FontContext {
}
if !cache_hit {
let font_template = self.font_cache_task.get_font_template(family.clone(), desc.clone());
let font_template = self.font_cache_task.get_font_template(family.name()
.to_string(),
desc.clone());
match font_template {
Some(font_template) => {
let layout_font = self.create_layout_font(font_template,
desc.clone(),
style.pt_size,
style.variant);
style.font_size.to_subpx(),
style.font_variant);
let layout_font = Rc::new(RefCell::new(layout_font));
self.layout_font_cache.push(LayoutFontCacheEntry {
family: family.clone(),
family: family.name().to_string(),
font: layout_font.clone(),
});
fonts.push(layout_font);
@ -168,8 +172,8 @@ impl FontContext {
for cached_font_entry in self.fallback_font_cache.iter() {
let cached_font = cached_font_entry.font.borrow();
if cached_font.descriptor == desc &&
cached_font.requested_pt_size == style.pt_size &&
cached_font.variant == style.variant {
cached_font.requested_pt_size == style.font_size.to_subpx() &&
cached_font.variant == style.font_variant {
fonts.push(cached_font_entry.font.clone());
cache_hit = true;
break;
@ -180,8 +184,8 @@ impl FontContext {
let font_template = self.font_cache_task.get_last_resort_font_template(desc.clone());
let layout_font = self.create_layout_font(font_template,
desc.clone(),
style.pt_size,
style.variant);
style.font_size.to_subpx(),
style.font_variant);
let layout_font = Rc::new(RefCell::new(layout_font));
self.fallback_font_cache.push(FallbackFontCacheEntry {
font: layout_font.clone(),