Add caching of swash rendering, significantly improves layout speed

This commit is contained in:
Jeremy Soller 2022-10-12 13:42:30 -06:00
parent 0b5050e601
commit a4959bbe7b
No known key found for this signature in database
GPG key ID: 87F211AF2BE4C2FE
5 changed files with 243 additions and 92 deletions

View file

@ -1,3 +1,11 @@
use std::{
collections::HashMap,
sync::Mutex,
};
pub use self::cache::*;
mod cache;
pub use self::layout::*;
mod layout;
@ -10,6 +18,12 @@ mod shape;
pub use self::system::*;
mod system;
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct FontCacheKey {
glyph_id: u16,
}
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
pub struct FontLineIndex(usize);
@ -32,6 +46,9 @@ pub struct Font<'a> {
pub rusttype: rusttype::Font<'a>,
#[cfg(feature = "swash")]
pub swash: swash::FontRef<'a>,
#[cfg(feature = "swash")]
pub scale_context: Mutex<swash::scale::ScaleContext>,
pub cache: Mutex<HashMap<CacheKey, CacheItem>>,
}
impl<'a> Font<'a> {
@ -45,6 +62,9 @@ impl<'a> Font<'a> {
rusttype: rusttype::Font::try_from_bytes_and_index(data, index)?,
#[cfg(feature = "swash")]
swash: swash::FontRef::from_index(data, index as usize)?,
#[cfg(feature = "swash")]
scale_context: Mutex::new(swash::scale::ScaleContext::new()),
cache: Mutex::new(HashMap::new()),
})
}
}