Remove lifetime from SwashCache

This commit is contained in:
Edgar Geier 2023-03-01 22:41:59 +01:00
parent 2b991129e3
commit 4e93853765
No known key found for this signature in database
GPG key ID: 7A65B51FD6B75EF5
10 changed files with 53 additions and 32 deletions

View file

@ -705,7 +705,7 @@ impl<'a> Buffer<'a> {
None => color,
};
cache.with_pixels(cache_key, glyph_color, |x, y, color| {
cache.with_pixels(self.font_system, cache_key, glyph_color, |x, y, color| {
f(x_int + x, run.line_y as i32 + y_int + y, 1, 1, color);
});
}

View file

@ -826,9 +826,14 @@ impl<'a> Edit<'a> for Editor<'a> {
None => color,
};
cache.with_pixels(cache_key, glyph_color, |x, y, color| {
f(x_int + x, line_y as i32 + y_int + y, 1, 1, color);
});
cache.with_pixels(
self.buffer.font_system(),
cache_key,
glyph_color,
|x, y, color| {
f(x_int + x, line_y as i32 + y_int + y, 1, 1, color);
},
);
}
}
}

View file

@ -18,7 +18,7 @@
//! let font_system = FontSystem::new();
//!
//! // A SwashCache stores rasterized glyphs, create one per application
//! let mut swash_cache = SwashCache::new(&font_system);
//! let mut swash_cache = SwashCache::new();
//!
//! // Text metrics indicate the font size and line height of a buffer
//! let metrics = Metrics::new(14.0, 20.0);

View file

@ -90,18 +90,16 @@ fn swash_outline_commands(
}
/// Cache for rasterizing with the swash scaler
pub struct SwashCache<'a> {
font_system: &'a FontSystem,
pub struct SwashCache {
context: ScaleContext,
pub image_cache: Map<CacheKey, Option<SwashImage>>,
pub outline_command_cache: Map<CacheKey, Option<Vec<swash::zeno::Command>>>,
}
impl<'a> SwashCache<'a> {
impl SwashCache {
/// Create a new swash cache
pub fn new(font_system: &'a FontSystem) -> Self {
pub fn new() -> Self {
Self {
font_system,
context: ScaleContext::new(),
image_cache: Map::new(),
outline_command_cache: Map::new(),
@ -109,34 +107,45 @@ impl<'a> SwashCache<'a> {
}
/// Create a swash Image from a cache key, without caching results
pub fn get_image_uncached(&mut self, cache_key: CacheKey) -> Option<SwashImage> {
swash_image(self.font_system, &mut self.context, cache_key)
pub fn get_image_uncached(
&mut self,
font_system: &FontSystem,
cache_key: CacheKey,
) -> Option<SwashImage> {
swash_image(font_system, &mut self.context, cache_key)
}
/// Create a swash Image from a cache key, caching results
pub fn get_image(&mut self, cache_key: CacheKey) -> &Option<SwashImage> {
pub fn get_image(
&mut self,
font_system: &FontSystem,
cache_key: CacheKey,
) -> &Option<SwashImage> {
self.image_cache
.entry(cache_key)
.or_insert_with(|| swash_image(self.font_system, &mut self.context, cache_key))
.or_insert_with(|| swash_image(font_system, &mut self.context, cache_key))
}
pub fn get_outline_commands(&mut self, cache_key: CacheKey) -> Option<&[swash::zeno::Command]> {
pub fn get_outline_commands(
&mut self,
font_system: &FontSystem,
cache_key: CacheKey,
) -> Option<&[swash::zeno::Command]> {
self.outline_command_cache
.entry(cache_key)
.or_insert_with(|| {
swash_outline_commands(self.font_system, &mut self.context, cache_key)
})
.or_insert_with(|| swash_outline_commands(font_system, &mut self.context, cache_key))
.as_deref()
}
/// Enumerate pixels in an Image, use `with_image` for better performance
pub fn with_pixels<F: FnMut(i32, i32, Color)>(
&mut self,
font_system: &FontSystem,
cache_key: CacheKey,
base: Color,
mut f: F,
) {
if let Some(image) = self.get_image(cache_key) {
if let Some(image) = self.get_image(font_system, cache_key) {
let x = image.placement.left;
let y = -image.placement.top;