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