svg_renderer.rs

 1use crate::{AssetSource, DevicePixels, IsZero, Result, SharedString, Size};
 2use anyhow::anyhow;
 3use resvg::tiny_skia::Pixmap;
 4use std::{hash::Hash, sync::Arc};
 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 size = match size {
50            SvgSize::Size(size) => size,
51            SvgSize::ScaleFactor(scale) => crate::size(
52                DevicePixels((tree.size().width() * scale) as i32),
53                DevicePixels((tree.size().height() * scale) as i32),
54            ),
55        };
56
57        // Render the SVG to a pixmap with the specified width and height.
58        let mut pixmap = resvg::tiny_skia::Pixmap::new(size.width.into(), size.height.into())
59            .ok_or(usvg::Error::InvalidSize)?;
60
61        let transform = tree.view_box().to_transform(
62            resvg::tiny_skia::Size::from_wh(size.width.0 as f32, size.height.0 as f32)
63                .ok_or(usvg::Error::InvalidSize)?,
64        );
65
66        resvg::render(&tree, transform, &mut pixmap.as_mut());
67
68        Ok(pixmap)
69    }
70}