Skip to content

Commit e9e65db

Browse files
committed
Avoid keeping several copies of bitmap glyph for each scale
1 parent 733dd52 commit e9e65db

6 files changed

Lines changed: 215 additions & 132 deletions

File tree

sbr-rasterize/src/rasterizer.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,24 @@ impl TextureInner {
4343
pub struct Texture(TextureInner);
4444

4545
impl Texture {
46-
pub(crate) fn memory_footprint(&self) -> usize {
46+
pub fn memory_footprint(&self) -> usize {
4747
match &self.0 {
4848
TextureInner::Software(sw) => sw.memory_footprint(),
4949
}
5050
}
5151

52+
pub fn width(&self) -> u32 {
53+
match &self.0 {
54+
TextureInner::Software(sw) => sw.width(),
55+
}
56+
}
57+
58+
pub fn height(&self) -> u32 {
59+
match &self.0 {
60+
TextureInner::Software(sw) => sw.height(),
61+
}
62+
}
63+
5264
pub(crate) fn is_mono(&self) -> bool {
5365
match &self.0 {
5466
TextureInner::Software(sw) => matches!(sw.format(), PixelFormat::Mono),

sbr-util/src/cache.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,11 @@ impl<K: Hash + PartialEq + Eq + 'static> Cache<K> {
290290
Ok(value) => value,
291291
}
292292
}
293+
294+
#[must_use]
295+
pub fn get<V: CacheValue>(&self, key: K) -> Option<&V> {
296+
self.get_or_try_insert_with(key, || Err(())).ok()
297+
}
293298
}
294299

295300
#[cfg(test)]

src/text/face.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use util::{
1111
};
1212

1313
use super::FreeTypeError;
14-
use crate::text::{FontSizeCacheKey, GlyphCache, OpenTypeTag};
14+
use crate::text::{FontSizeKey, GlyphCache, GlyphKey, OpenTypeTag};
1515

1616
pub mod freetype;
1717
pub use freetype::GlyphDisplayError;
@@ -191,7 +191,7 @@ impl Font {
191191
forward_methods!(
192192
variants = Font::[FreeType, Tofu];
193193

194-
fn size_cache_key[&]() -> FontSizeCacheKey;
194+
fn size_cache_key[&]() -> FontSizeKey;
195195
pub fn metrics[&]() -> &FontMetrics;
196196
pub fn point_size[&]() -> I26Dot6;
197197
pub fn harfbuzz_scale_factor_for[&](glyph: u32) -> I26Dot6;
@@ -224,15 +224,20 @@ impl Font {
224224
rasterizer: &mut dyn Rasterizer,
225225
) -> Result<&'c GlyphSubscene, GlyphDisplayError> {
226226
let (render_offset, subpixel_bucket) =
227-
FontSizeCacheKey::get_subpixel_bucket(offset_value, offset_axis_is_y);
228-
let key = self
229-
.size_cache_key()
230-
.for_glyph(self.face(), glyph, subpixel_bucket);
231-
232-
cache.get_or_try_insert_with(key, || match self {
233-
Self::FreeType(font) => font.glyph_subscene_uncached(glyph, render_offset, rasterizer),
234-
Self::Tofu(font) => Ok(font.glyph_subscene_uncached(glyph, render_offset, rasterizer)),
235-
})
227+
GlyphKey::get_subpixel_bucket(offset_value, offset_axis_is_y);
228+
let key = self.size_cache_key().for_glyph(self.face(), glyph);
229+
230+
cache.get_or_try_insert_with(
231+
key.clone().for_subpixel_bucket(subpixel_bucket),
232+
|| match self {
233+
Self::FreeType(font) => {
234+
font.glyph_subscene_uncached(glyph, render_offset, rasterizer, cache, key)
235+
}
236+
Self::Tofu(font) => {
237+
Ok(font.glyph_subscene_uncached(glyph, render_offset, rasterizer))
238+
}
239+
},
240+
)
236241
}
237242
}
238243

0 commit comments

Comments
 (0)