Revert "Revert "Revert dependency updates in #9836 (#10089)""

This reverts commit c8b14ee2cb.
This commit is contained in:
Conrad Irwin 2024-04-02 13:12:38 -06:00
parent c8b14ee2cb
commit 9317fe46af
9 changed files with 267 additions and 496 deletions

View file

@ -1,10 +1,7 @@
use crate::{AssetSource, DevicePixels, IsZero, Result, SharedString, Size};
use anyhow::anyhow;
use resvg::tiny_skia::Pixmap;
use std::{
hash::Hash,
sync::{Arc, OnceLock},
};
use std::{hash::Hash, sync::Arc};
use tiny_skia::Pixmap;
#[derive(Clone, PartialEq, Hash, Eq)]
pub(crate) struct RenderSvgParams {
@ -46,40 +43,28 @@ impl SvgRenderer {
Ok(alpha_mask)
}
pub fn render_pixmap(&self, bytes: &[u8], size: SvgSize) -> Result<Pixmap, resvg::usvg::Error> {
let tree =
resvg::usvg::Tree::from_data(&bytes, &resvg::usvg::Options::default(), svg_fontdb())?;
pub fn render_pixmap(&self, bytes: &[u8], size: SvgSize) -> Result<Pixmap, usvg::Error> {
let tree = usvg::Tree::from_data(&bytes, &usvg::Options::default())?;
let tree_size = tree.svg_node().size;
let size = match size {
SvgSize::Size(size) => size,
SvgSize::ScaleFactor(scale) => crate::size(
DevicePixels((tree.size().width() * scale) as i32),
DevicePixels((tree.size().height() * scale) as i32),
DevicePixels((tree_size.width() * scale as f64) as i32),
DevicePixels((tree_size.height() * scale as f64) as i32),
),
};
// Render the SVG to a pixmap with the specified width and height.
let mut pixmap =
resvg::tiny_skia::Pixmap::new(size.width.into(), size.height.into()).unwrap();
let ratio = size.width.0 as f32 / tree.size().width();
let mut pixmap = tiny_skia::Pixmap::new(size.width.into(), size.height.into()).unwrap();
resvg::render(
&tree,
resvg::tiny_skia::Transform::from_scale(ratio, ratio),
&mut pixmap.as_mut(),
usvg::FitTo::Width(size.width.into()),
pixmap.as_mut(),
);
Ok(pixmap)
}
}
/// Returns the global font database used for SVG rendering.
pub(crate) fn svg_fontdb() -> &'static resvg::usvg::fontdb::Database {
static FONTDB: OnceLock<resvg::usvg::fontdb::Database> = OnceLock::new();
FONTDB.get_or_init(|| {
let mut fontdb = resvg::usvg::fontdb::Database::new();
fontdb.load_system_fonts();
fontdb
})
}