Add function for getting swash image without caching
This commit is contained in:
parent
22036a0f9e
commit
f652dd1a99
1 changed files with 45 additions and 36 deletions
45
src/swash.rs
45
src/swash.rs
|
|
@ -9,23 +9,7 @@ use crate::{CacheKey, FontMatches};
|
|||
|
||||
pub use swash::scale::image::Image as SwashImage;
|
||||
|
||||
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, caching results
|
||||
pub fn get_image(&mut self, matches: &FontMatches<'_>, cache_key: CacheKey) -> &Option<SwashImage> {
|
||||
self.image_cache.entry(cache_key).or_insert_with(|| {
|
||||
fn swash_image(context: &mut ScaleContext, matches: &FontMatches, cache_key: CacheKey) -> Option<SwashImage> {
|
||||
let font = match matches.get_font(&cache_key.font_id) {
|
||||
Some(some) => some,
|
||||
None => {
|
||||
|
|
@ -35,7 +19,7 @@ impl SwashCache {
|
|||
};
|
||||
|
||||
// Build the scaler
|
||||
let mut scaler = self.context
|
||||
let mut scaler = context
|
||||
.builder(font.as_swash())
|
||||
.size(cache_key.font_size as f32)
|
||||
.hint(true)
|
||||
|
|
@ -61,6 +45,31 @@ impl SwashCache {
|
|||
.offset(offset)
|
||||
// Render the image
|
||||
.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)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue