Skip to content

Instantly share code, notes, and snippets.

@jdm
Created June 3, 2013 21:02
Show Gist options
  • Save jdm/5701387 to your computer and use it in GitHub Desktop.
Save jdm/5701387 to your computer and use it in GitHub Desktop.
diff --git a/src/components/gfx/font.rs b/src/components/gfx/font.rs
index 37a712d..da93a13 100644
--- a/src/components/gfx/font.rs
+++ b/src/components/gfx/font.rs
@@ -18,6 +18,8 @@ use azure::scaled_font::ScaledFont;
use azure::azure_hl::{BackendType, ColorPattern};
use geom::{Point2D, Rect, Size2D};
+use core::hashmap::HashMap;
+
// FontHandle encapsulates access to the platform's font API,
// e.g. quartz, FreeType. It provides access to metrics and tables
// needed by the text shaper as well as access to the underlying font
@@ -166,6 +168,7 @@ pub struct FontGroup {
// used for purposes of calculating text run metrics.
style: UsedFontStyle,
fonts: ~[@mut Font],
+ textrun_cache: HashMap<~str, @TextRun>
}
pub impl FontGroup {
@@ -174,6 +177,7 @@ pub impl FontGroup {
families: families,
style: copy *style,
fonts: fonts,
+ textrun_cache: HashMap::new()
}
}
@@ -181,11 +185,18 @@ pub impl FontGroup {
self.fonts = ~[];
}
- fn create_textrun(&self, text: ~str, underline: bool) -> TextRun {
+ fn create_textrun(&mut self, text: ~str, underline: bool) -> @TextRun {
+ match self.textrun_cache.find(&text) {
+ Some(run) => return *run,
+ None => (),
+ }
+
assert!(self.fonts.len() > 0);
// TODO(Issue #177): Actually fall back through the FontGroup when a font is unsuitable.
- return TextRun::new(self.fonts[0], text, underline);
+ let run = @TextRun::new(self.fonts[0], text.clone(), underline);
+ self.textrun_cache.insert(text, run);
+ run
}
}
diff --git a/src/components/gfx/font_context.rs b/src/components/gfx/font_context.rs
index 880c7d3..b8057e7 100644
--- a/src/components/gfx/font_context.rs
+++ b/src/components/gfx/font_context.rs
@@ -38,7 +38,7 @@ pub trait FontContextHandleMethods {
pub struct FontContext {
instance_cache: MonoCache<FontDescriptor, @mut Font>,
shaper_cache: MonoCache<FontDescriptor, @Shaper>,
- group_cache: MonoCache<SpecifiedFontStyle, @FontGroup>,
+ group_cache: MonoCache<SpecifiedFontStyle, @mut FontGroup>,
font_list: Option<FontList>, // only needed by layout
handle: FontContextHandle,
backend: BackendType,
@@ -71,7 +71,7 @@ pub impl<'self> FontContext {
shaper_cache:
Cache::new::<FontDescriptor,@Shaper,MonoCache<FontDescriptor,@Shaper>>(10),
group_cache:
- Cache::new::<SpecifiedFontStyle,@FontGroup,MonoCache<SpecifiedFontStyle,@FontGroup>>(10),
+ Cache::new::<SpecifiedFontStyle,@mut FontGroup,MonoCache<SpecifiedFontStyle,@mut FontGroup>>(10),
font_list: font_list,
handle: handle,
backend: backend,
@@ -95,7 +95,7 @@ pub impl<'self> FontContext {
self.font_list.get_ref()
}
- fn get_resolved_font_for_style(@mut self, style: &SpecifiedFontStyle) -> @FontGroup {
+ fn get_resolved_font_for_style(@mut self, style: &SpecifiedFontStyle) -> @mut FontGroup {
match self.group_cache.find(style) {
Some(fg) => return fg,
None => ()
@@ -131,7 +131,7 @@ pub impl<'self> FontContext {
}
// TODO:(Issue #196): cache font groups on the font context.
- priv fn create_font_group(@mut self, style: &SpecifiedFontStyle) -> @FontGroup {
+ priv fn create_font_group(@mut self, style: &SpecifiedFontStyle) -> @mut FontGroup {
let mut fonts = ~[];
debug!("(create font group) --- starting ---");
@@ -171,7 +171,7 @@ pub impl<'self> FontContext {
debug!("(create font group) --- finished ---");
- @FontGroup::new(style.families.to_managed(), &used_style, fonts)
+ @mut FontGroup::new(style.families.to_managed(), &used_style, fonts)
}
priv fn create_font_instance(&mut self, desc: &FontDescriptor) -> Result<@mut Font, ()> {
diff --git a/src/components/main/layout/inline.rs b/src/components/main/layout/inline.rs
index 3db7e23..81ade6e 100644
--- a/src/components/main/layout/inline.rs
+++ b/src/components/main/layout/inline.rs
@@ -281,7 +281,7 @@ 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 fontgroup = ctx.font_ctx.get_resolved_font_for_style(&font_style);
- let run = @fontgroup.create_textrun(transformed_text, underline);
+ let run = fontgroup.create_textrun(transformed_text, underline);
debug!("TextRunScanner: pushing single text box in range: %?", self.clump);
let new_box = do old_box.with_imm_base |old_box_base| {
@@ -329,7 +329,7 @@ impl TextRunScanner {
// sequence. If no clump takes ownership, however, it will leak.
let clump = self.clump;
let run = if clump.length() != 0 {
- Some(@TextRun::new(fontgroup.fonts[0], run_str, underline))
+ Some(fontgroup.create_textrun(run_str, underline))
} else {
None
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment