wgpu_font_renderer/
store.rs

1use swash::CacheKey;
2use wgpu::{CommandEncoderDescriptor, SurfaceConfiguration};
3
4use std::collections::HashMap;
5
6use crate::{atlas::Atlas, loader::Font, LoadingError};
7
8pub struct FontStore {
9    cache: HashMap<CacheKey, Font>,
10    atlas: Atlas,
11}
12
13impl FontStore {
14    pub fn new(device: &wgpu::Device, surface_config: &SurfaceConfiguration) -> Self {
15        Self {
16            cache: HashMap::new(),
17            atlas: Atlas::new(device, surface_config),  
18        }
19    }
20
21    pub fn load(
22        &mut self,
23        device: &wgpu::Device,
24        queue: &wgpu::Queue,
25        font_file_path: &str,
26        cache_preset: &str
27    ) -> Result<CacheKey, LoadingError>{
28
29        let mut encoder = device.create_command_encoder(&CommandEncoderDescriptor { label: None });
30
31        let font = Font::from_file(device, &mut encoder, queue, font_file_path, 0, cache_preset, &mut self.atlas)?;
32
33        queue.submit(Some(encoder.finish()));
34
35        let cache_key = font.key.clone();
36
37        self.cache.insert(cache_key, font);
38
39        Ok(cache_key)
40    }
41
42    pub fn atlas(&self) -> &Atlas {
43        &self.atlas
44    }
45
46    pub fn get(&self, font_key: CacheKey) -> Option<&Font> {
47        self.cache.get(&font_key)
48    }
49}