This reverts commit 55c897d993
.
This commit is contained in:
parent
55c897d993
commit
c8b14ee2cb
9 changed files with 495 additions and 266 deletions
|
@ -1,7 +1,10 @@
|
|||
use crate::{AssetSource, DevicePixels, IsZero, Result, SharedString, Size};
|
||||
use anyhow::anyhow;
|
||||
use std::{hash::Hash, sync::Arc};
|
||||
use tiny_skia::Pixmap;
|
||||
use resvg::tiny_skia::Pixmap;
|
||||
use std::{
|
||||
hash::Hash,
|
||||
sync::{Arc, OnceLock},
|
||||
};
|
||||
|
||||
#[derive(Clone, PartialEq, Hash, Eq)]
|
||||
pub(crate) struct RenderSvgParams {
|
||||
|
@ -43,28 +46,40 @@ impl SvgRenderer {
|
|||
Ok(alpha_mask)
|
||||
}
|
||||
|
||||
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;
|
||||
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())?;
|
||||
|
||||
let size = match size {
|
||||
SvgSize::Size(size) => size,
|
||||
SvgSize::ScaleFactor(scale) => crate::size(
|
||||
DevicePixels((tree_size.width() * scale as f64) as i32),
|
||||
DevicePixels((tree_size.height() * scale as f64) as i32),
|
||||
DevicePixels((tree.size().width() * scale) as i32),
|
||||
DevicePixels((tree.size().height() * scale) as i32),
|
||||
),
|
||||
};
|
||||
|
||||
// Render the SVG to a pixmap with the specified width and height.
|
||||
let mut pixmap = tiny_skia::Pixmap::new(size.width.into(), size.height.into()).unwrap();
|
||||
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();
|
||||
|
||||
resvg::render(
|
||||
&tree,
|
||||
usvg::FitTo::Width(size.width.into()),
|
||||
pixmap.as_mut(),
|
||||
resvg::tiny_skia::Transform::from_scale(ratio, ratio),
|
||||
&mut 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
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue