svg_renderer.rs

 1use crate::{AssetSource, DevicePixels, IsZero, Result, SharedString, Size};
 2use anyhow::anyhow;
 3use std::{hash::Hash, sync::Arc};
 4use tiny_skia::Pixmap;
 5
 6#[derive(Clone, PartialEq, Hash, Eq)]
 7pub(crate) struct RenderSvgParams {
 8    pub(crate) path: SharedString,
 9    pub(crate) size: Size<DevicePixels>,
10}
11
12#[derive(Clone)]
13pub(crate) struct SvgRenderer {
14    asset_source: Arc<dyn AssetSource>,
15}
16
17pub enum SvgSize {
18    Size(Size<DevicePixels>),
19    ScaleFactor(f32),
20}
21
22impl SvgRenderer {
23    pub fn new(asset_source: Arc<dyn AssetSource>) -> Self {
24        Self { asset_source }
25    }
26
27    pub fn render(&self, params: &RenderSvgParams) -> Result<Vec<u8>> {
28        if params.size.is_zero() {
29            return Err(anyhow!("can't render at a zero size"));
30        }
31
32        // Load the tree.
33        let bytes = self.asset_source.load(&params.path)?;
34
35        let pixmap = self.render_pixmap(&bytes, SvgSize::Size(params.size))?;
36
37        // Convert the pixmap's pixels into an alpha mask.
38        let alpha_mask = pixmap
39            .pixels()
40            .iter()
41            .map(|p| p.alpha())
42            .collect::<Vec<_>>();
43        Ok(alpha_mask)
44    }
45
46    pub fn render_pixmap(&self, bytes: &[u8], size: SvgSize) -> Result<Pixmap, usvg::Error> {
47        let tree = usvg::Tree::from_data(&bytes, &usvg::Options::default())?;
48
49        let tree_size = tree.svg_node().size;
50
51        let size = match size {
52            SvgSize::Size(size) => size,
53            SvgSize::ScaleFactor(scale) => crate::size(
54                DevicePixels((tree_size.width() * scale as f64) as i32),
55                DevicePixels((tree_size.height() * scale as f64) as i32),
56            ),
57        };
58
59        // Render the SVG to a pixmap with the specified width and height.
60        let mut pixmap = tiny_skia::Pixmap::new(size.width.into(), size.height.into()).unwrap();
61
62        resvg::render(
63            &tree,
64            usvg::FitTo::Width(size.width.into()),
65            pixmap.as_mut(),
66        );
67
68        Ok(pixmap)
69    }
70}