diff options
Diffstat (limited to 'src/renderer/mod.rs')
-rw-r--r-- | src/renderer/mod.rs | 28 |
1 files changed, 13 insertions, 15 deletions
diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 48354018..60d83287 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -157,6 +157,8 @@ pub struct GlyphCache { /// glyph offset glyph_offset: Delta, + + metrics: ::font::Metrics, } impl GlyphCache { @@ -220,6 +222,12 @@ impl GlyphCache { .unwrap_or_else(|_| regular) }; + // Need to load at least one glyph for the face before calling metrics. + // The glyph requested here ('m' at the time of writing) has no special + // meaning. + rasterizer.get_glyph(&GlyphKey { font_key: regular, c: 'm' as char, size: font.size() })?; + let metrics = rasterizer.metrics(regular)?; + let mut cache = GlyphCache { cache: HashMap::default(), rasterizer: rasterizer, @@ -228,12 +236,13 @@ impl GlyphCache { bold_key: bold, italic_key: italic, glyph_offset: glyph_offset, + metrics: metrics }; macro_rules! load_glyphs_for_font { ($font:expr) => { for i in RangeInclusive::new(32u8, 128u8) { - cache.load_and_cache_glyph(GlyphKey { + cache.get(&GlyphKey { font_key: $font, c: i as char, size: font.size() @@ -255,33 +264,22 @@ impl GlyphCache { .expect("metrics load since font is loaded at glyph cache creation") } - fn load_and_cache_glyph<L>(&mut self, glyph_key: GlyphKey, loader: &mut L) - where L: LoadGlyph - { - let mut rasterized = self.rasterizer.get_glyph(&glyph_key) - .unwrap_or_else(|_| Default::default()); - - rasterized.left += self.glyph_offset.x as i32; - rasterized.top += self.glyph_offset.y as i32; - - let glyph = loader.load_glyph(&rasterized); - self.cache.insert(glyph_key, glyph); - } - pub fn get<'a, L>(&'a mut self, glyph_key: &GlyphKey, loader: &mut L) -> &'a Glyph where L: LoadGlyph { let glyph_offset = self.glyph_offset; let rasterizer = &mut self.rasterizer; + let metrics = &self.metrics; self.cache .entry(*glyph_key) .or_insert_with(|| { let mut rasterized = rasterizer.get_glyph(&glyph_key) .unwrap_or_else(|_| Default::default()); - // We need to apply the offset to glyphs that didn't get cached initially + // Apply offsets so they aren't computed every draw rasterized.left += glyph_offset.x as i32; rasterized.top += glyph_offset.y as i32; + rasterized.top -= metrics.descent as i32; loader.load_glyph(&rasterized) }) |