FontStore caching + returns Arcs

This commit is contained in:
marceline-cramer 2022-07-18 16:10:58 -06:00
parent 6b78d51e64
commit 96eb8c2daa
1 changed files with 19 additions and 3 deletions

View File

@ -14,6 +14,8 @@ use canary_types::GlyphPosition;
use lyon::path::Path;
use ouroboros::self_referencing;
use parking_lot::{Mutex, RwLock};
use std::collections::HashMap;
use std::sync::Arc;
const TEXT_SCALE: f32 = 0.075;
@ -256,16 +258,27 @@ impl FontData {
pub struct FontStore {
source: Box<dyn font_kit::source::Source>,
loaded: Mutex<HashMap<String, Arc<Font>>>,
}
impl FontStore {
pub fn new() -> Self {
let source = font_kit::source::SystemSource::new();
let source = Box::new(source);
Self { source }
Self {
source,
loaded: Default::default(),
}
}
pub fn load_font(&self, title: &str) -> Font {
pub fn load_font(&self, title: &str) -> Arc<Font> {
let mut loaded = self.loaded.lock();
if let Some(loaded) = loaded.get(title) {
return loaded.to_owned();
}
use font_kit::family_name::FamilyName;
use font_kit::handle::Handle;
use font_kit::properties::Properties;
@ -293,7 +306,10 @@ impl FontStore {
let face_index = 0;
let direction = TextDirection::LeftToRight;
let vertical = false;
Font::load(face_index, script, direction, vertical, font_data.clone())
let font = Font::load(face_index, script, direction, vertical, font_data.clone());
let font = Arc::new(font);
loaded.insert(title.to_string(), font.to_owned());
font
}
}