/* LICENSE BEGIN This file is part of the SixtyFPS Project -- https://sixtyfps.io Copyright (c) 2020 Olivier Goffart Copyright (c) 2020 Simon Hausmann SPDX-License-Identifier: GPL-3.0-only This file is also available under commercial licensing terms. Please contact info@sixtyfps.io for more information. LICENSE END */ use std::cell::RefCell; use std::collections::HashMap; use std::rc::Rc; #[cfg(not(target_arch = "wasm32"))] mod fontkit; #[cfg(not(target_arch = "wasm32"))] pub use fontkit::*; #[cfg(target_arch = "wasm32")] mod canvasfont; #[cfg(target_arch = "wasm32")] pub use canvasfont::*; struct FontMatch { handle: FontHandle, fonts_per_pixel_size: Vec>, } #[derive(Default)] pub struct FontCache { // index by family name loaded_fonts: RefCell>, } impl FontCache { pub fn find_font(&self, family: &str, pixel_size: f32) -> Rc { assert_ne!(pixel_size, 0.0); let mut loaded_fonts = self.loaded_fonts.borrow_mut(); let font_match = loaded_fonts.entry(family.to_owned()).or_insert_with(|| FontMatch { handle: FontHandle::new_from_match(family), fonts_per_pixel_size: Vec::new(), }); font_match .fonts_per_pixel_size .iter() .find_map(|font| if font.pixel_size == pixel_size { Some(font.clone()) } else { None }) .unwrap_or_else(|| { let fnt = Rc::new(font_match.handle.load(pixel_size).unwrap()); font_match.fonts_per_pixel_size.push(fnt.clone()); fnt }) } } thread_local! { pub static FONT_CACHE: FontCache = Default::default(); }