Skip to content

Instantly share code, notes, and snippets.

@jdm
Created May 31, 2013 20:07
Show Gist options
  • Save jdm/5687626 to your computer and use it in GitHub Desktop.
Save jdm/5687626 to your computer and use it in GitHub Desktop.
diff --git a/src/components/servo-gfx/platform/linux/font.rs b/src/components/servo-gfx/platform/linux/font.rs
index 1eddd68..b6e7bc9 100644
--- a/src/components/servo-gfx/platform/linux/font.rs
+++ b/src/components/servo-gfx/platform/linux/font.rs
@@ -25,6 +25,8 @@ use freetype::freetype::{FT_SizeRec, FT_UInt, FT_Size_Metrics};
use freetype::freetype::{ft_sfnt_os2};
use freetype::tt_os2::TT_OS2;
+use core::hashmap::HashMap;
+
fn float_to_fixed_ft(f: float) -> i32 {
float_to_fixed(6, f)
}
@@ -53,7 +55,8 @@ pub struct FontHandle {
// if the font is created using FT_Memory_Face.
source: FontSource,
face: FT_Face,
- handle: FontContextHandle
+ handle: FontContextHandle,
+ advance_cache: @mut HashMap<FT_UInt, FractionalPixel>
}
#[unsafe_destructor]
@@ -86,7 +89,8 @@ impl FontHandleMethods for FontHandle {
let handle = FontHandle {
face: face,
source: FontSourceMem(buf),
- handle: *fctx
+ handle: *fctx,
+ advance_cache: @mut HashMap::new()
};
Ok(handle)
}
@@ -182,6 +186,11 @@ impl FontHandleMethods for FontHandle {
pub fn glyph_h_advance(&self,
glyph: GlyphIndex) -> Option<FractionalPixel> {
+ match self.advance_cache.find(&(glyph as FT_UInt)) {
+ Some(advance) => return Some(*advance),
+ None => ()
+ }
+
assert!(self.face.is_not_null());
let res = FT_Load_Glyph(self.face, glyph as FT_UInt, 0);
if res.succeeded() {
@@ -193,7 +202,9 @@ impl FontHandleMethods for FontHandle {
let advance = (*slot).metrics.horiAdvance;
debug!("h_advance for %? is %?", glyph, advance);
let advance = advance as i32;
- return Some(fixed_to_float_ft(advance) as FractionalPixel);
+ let advance = fixed_to_float_ft(advance) as FractionalPixel;
+ self.advance_cache.insert(glyph as FT_UInt, advance);
+ return Some(advance);
}
} else {
debug!("Unable to load glyph %?. reason: %?", glyph, res);
@@ -258,7 +269,8 @@ pub impl<'self> FontHandle {
Ok(FontHandle {
source: FontSourceFile(file),
face: face,
- handle: *fctx
+ handle: *fctx,
+ advance_cache: @mut HashMap::new()
})
} else {
Err(())
@@ -283,7 +295,8 @@ pub impl<'self> FontHandle {
Ok(FontHandle {
source: FontSourceFile(file),
face: face,
- handle: *fctx
+ handle: *fctx,
+ advance_cache: @mut HashMap::new()
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment