use crate::traits::rasterizer::Rasterizer;
use crate::types::glyph::RasterizedGlyph;
use crate::types::shaped_glyph::ShapedGlyph;
use cosmic_text::{SwashCache, FontSystem};
use std::sync::{Arc, Mutex};
pub struct SwashRasterizer {
cache: SwashCache,
font_system: Arc<Mutex<FontSystem>>,
}
impl SwashRasterizer {
pub fn new(font_system: Arc<Mutex<FontSystem>>) -> Self {
Self {
cache: SwashCache::new(),
font_system,
}
}
pub fn font_system(&self) -> Arc<Mutex<FontSystem>> {
self.font_system.clone()
}
}
impl Rasterizer for SwashRasterizer {
fn rasterize(&mut self, glyph: &ShapedGlyph) -> Option<RasterizedGlyph> {
let mut fs = self.font_system.lock().unwrap();
let image = self.cache.get_image(&mut fs, glyph.key).as_ref()?;
let is_color = matches!(image.content, cosmic_text::SwashContent::Color);
Some(RasterizedGlyph {
width: image.placement.width,
height: image.placement.height,
left: image.placement.left,
top: image.placement.top,
data: image.data.clone(),
is_color,
})
}
}