summaryrefslogtreecommitdiff
path: root/font
diff options
context:
space:
mode:
Diffstat (limited to 'font')
-rw-r--r--font/src/darwin/mod.rs21
-rw-r--r--font/src/ft/mod.rs17
2 files changed, 27 insertions, 11 deletions
diff --git a/font/src/darwin/mod.rs b/font/src/darwin/mod.rs
index 4db8dad1..1b295801 100644
--- a/font/src/darwin/mod.rs
+++ b/font/src/darwin/mod.rs
@@ -75,6 +75,7 @@ pub struct Descriptor {
/// Given a fontdesc, can rasterize fonts.
pub struct Rasterizer {
fonts: HashMap<FontKey, Font>,
+ keys: HashMap<(FontDesc, Size), FontKey>,
device_pixel_ratio: f32,
}
@@ -83,6 +84,7 @@ impl Rasterizer {
println!("device_pixel_ratio: {}", device_pixel_ratio);
Rasterizer {
fonts: HashMap::new(),
+ keys: HashMap::new(),
device_pixel_ratio: device_pixel_ratio,
}
}
@@ -100,12 +102,19 @@ impl Rasterizer {
}
pub fn load_font(&mut self, desc: &FontDesc, size: Size) -> Option<FontKey> {
- self.get_font(desc, size)
- .map(|font| {
- let key = FontKey::next();
- self.fonts.insert(key, font);
-
- key
+ self.keys
+ .get(&(desc.to_owned(), size))
+ .map(|k| *k)
+ .or_else(|| {
+ self.get_font(desc, size)
+ .map(|font| {
+ let key = FontKey::next();
+
+ self.fonts.insert(key, font);
+ self.keys.insert((desc.clone(), size), key);
+
+ key
+ })
})
}
diff --git a/font/src/ft/mod.rs b/font/src/ft/mod.rs
index 55829e12..fa4325a0 100644
--- a/font/src/ft/mod.rs
+++ b/font/src/ft/mod.rs
@@ -29,6 +29,7 @@ pub struct Rasterizer {
faces: HashMap<FontKey, Face<'static>>,
library: Library,
system_fonts: HashMap<String, Family>,
+ keys: HashMap<FontDesc, FontKey>,
dpi_x: u32,
dpi_y: u32,
dpr: f32,
@@ -51,6 +52,7 @@ impl Rasterizer {
Rasterizer {
system_fonts: get_font_families(),
faces: HashMap::new(),
+ keys: HashMap::new(),
library: library,
dpi_x: dpi_x as u32,
dpi_y: dpi_y as u32,
@@ -77,11 +79,16 @@ impl Rasterizer {
}
pub fn load_font(&mut self, desc: &FontDesc, _size: Size) -> Option<FontKey> {
- self.get_face(desc)
- .map(|face| {
- let key = FontKey::next();
- self.faces.insert(key, face);
- key
+ self.keys
+ .get(&desc.to_owned())
+ .map(|k| *k)
+ .or_else(|| {
+ self.get_face(desc)
+ .map(|face| {
+ let key = FontKey::next();
+ self.faces.insert(key, face);
+ key
+ })
})
}