Add function for getting swash image without caching

This commit is contained in:
Jeremy Soller 2022-10-25 19:40:17 -06:00
parent 22036a0f9e
commit f652dd1a99

View file

@ -9,23 +9,7 @@ use crate::{CacheKey, FontMatches};
pub use swash::scale::image::Image as SwashImage; pub use swash::scale::image::Image as SwashImage;
pub struct SwashCache { fn swash_image(context: &mut ScaleContext, matches: &FontMatches, cache_key: CacheKey) -> Option<SwashImage> {
context: ScaleContext,
pub image_cache: HashMap<CacheKey, Option<SwashImage>>,
}
impl SwashCache {
/// Create a new swash cache
pub fn new() -> Self {
Self {
context: ScaleContext::new(),
image_cache: HashMap::new()
}
}
/// Create a swash Image from a cache key, caching results
pub fn get_image(&mut self, matches: &FontMatches<'_>, cache_key: CacheKey) -> &Option<SwashImage> {
self.image_cache.entry(cache_key).or_insert_with(|| {
let font = match matches.get_font(&cache_key.font_id) { let font = match matches.get_font(&cache_key.font_id) {
Some(some) => some, Some(some) => some,
None => { None => {
@ -35,7 +19,7 @@ impl SwashCache {
}; };
// Build the scaler // Build the scaler
let mut scaler = self.context let mut scaler = context
.builder(font.as_swash()) .builder(font.as_swash())
.size(cache_key.font_size as f32) .size(cache_key.font_size as f32)
.hint(true) .hint(true)
@ -61,6 +45,31 @@ impl SwashCache {
.offset(offset) .offset(offset)
// Render the image // Render the image
.render(&mut scaler, cache_key.glyph_id) .render(&mut scaler, cache_key.glyph_id)
}
pub struct SwashCache {
context: ScaleContext,
pub image_cache: HashMap<CacheKey, Option<SwashImage>>,
}
impl SwashCache {
/// Create a new swash cache
pub fn new() -> Self {
Self {
context: ScaleContext::new(),
image_cache: HashMap::new()
}
}
/// Create a swash Image from a cache key, without caching results
pub fn get_image_uncached(&mut self, matches: &FontMatches, cache_key: CacheKey) -> Option<SwashImage> {
swash_image(&mut self.context, matches, cache_key)
}
/// Create a swash Image from a cache key, caching results
pub fn get_image(&mut self, matches: &FontMatches, cache_key: CacheKey) -> &Option<SwashImage> {
self.image_cache.entry(cache_key).or_insert_with(|| {
swash_image(&mut self.context, matches, cache_key)
}) })
} }