diff --git a/crates/resvg/src/image.rs b/crates/resvg/src/image.rs index 19ab0bf69..d01e9ad3b 100644 --- a/crates/resvg/src/image.rs +++ b/crates/resvg/src/image.rs @@ -2,6 +2,8 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +use usvg::load_sub_svg; + use crate::render::TinySkiaPixmapMutExt; use crate::tree::{BBoxes, Node, Tree}; @@ -36,7 +38,9 @@ pub fn convert(image: &usvg::Image, children: &mut Vec) -> Option } let kind = match image.kind { - usvg::ImageKind::SVG(_) => return None, + usvg::ImageKind::SVG(ref data) => ImageKind::Vector(Tree::from_usvg( + &load_sub_svg(&data, &usvg::Options::default()).unwrap(), + )), #[cfg(feature = "raster-images")] _ => ImageKind::Raster(raster_images::decode_raster(image)?), #[cfg(not(feature = "raster-images"))] @@ -108,8 +112,6 @@ fn render_vector( #[cfg(feature = "raster-images")] mod raster_images { - use usvg::fontdb::Database; - use super::Image; use crate::render::TinySkiaPixmapMutExt; use crate::tree::OptionLog; diff --git a/crates/usvg-parser/src/image.rs b/crates/usvg-parser/src/image.rs index 18254f6c2..55a2cc897 100644 --- a/crates/usvg-parser/src/image.rs +++ b/crates/usvg-parser/src/image.rs @@ -69,12 +69,12 @@ impl ImageHrefResolver { let height: u32 = u32::from_be_bytes(height_vec); Some(ImageKind::RAW(width, height, Arc::new(buf.to_vec()))) } - "image/svg+xml" => load_sub_svg(&data, opts), + "image/svg+xml" => Some(ImageKind::SVG(Arc::new(data.to_vec()))), "text/plain" => match get_image_data_format(&data) { Some(ImageFormat::JPEG) => Some(ImageKind::JPEG(data)), Some(ImageFormat::PNG) => Some(ImageKind::PNG(data)), Some(ImageFormat::GIF) => Some(ImageKind::GIF(data)), - _ => load_sub_svg(&data, opts), + _ => Some(ImageKind::SVG(Arc::new(data.to_vec()))), }, _ => None, }, @@ -105,7 +105,7 @@ impl ImageHrefResolver { Some(ImageFormat::JPEG) => Some(ImageKind::JPEG(Arc::new(data))), Some(ImageFormat::PNG) => Some(ImageKind::PNG(Arc::new(data))), Some(ImageFormat::GIF) => Some(ImageKind::GIF(Arc::new(data))), - Some(ImageFormat::SVG) => load_sub_svg(&data, opts), + Some(ImageFormat::SVG) => Some(ImageKind::SVG(Arc::new(data))), _ => { log::warn!("'{}' is not a PNG, JPEG, GIF or SVG(Z) image.", href); None @@ -250,27 +250,27 @@ fn get_image_data_format(data: &[u8]) -> Option { /// /// Unlike `Tree::from_*` methods, this one will also remove all `image` elements /// from the loaded SVG, as required by the spec. -pub(crate) fn load_sub_svg(data: &[u8], _: &Options) -> Option { - // let mut sub_opt = Options::default(); - // sub_opt.resources_dir = None; - // sub_opt.dpi = opt.dpi; - // sub_opt.font_size = opt.font_size; - // sub_opt.languages = opt.languages.clone(); - // sub_opt.shape_rendering = opt.shape_rendering; - // sub_opt.text_rendering = opt.text_rendering; - // sub_opt.image_rendering = opt.image_rendering; - // sub_opt.default_size = opt.default_size; - - // let tree = match Tree::from_data(data, &sub_opt) { - // Ok(tree) => tree, - // Err(_) => { - // log::warn!("Failed to load subsvg image."); - // return None; - // } - // }; - - // sanitize_sub_svg(&tree); - Some(ImageKind::SVG(Arc::new(data.to_vec()))) +pub fn load_sub_svg(data: &[u8], opt: &Options) -> Option { + let mut sub_opt = Options::default(); + sub_opt.resources_dir = None; + sub_opt.dpi = opt.dpi; + sub_opt.font_size = opt.font_size; + sub_opt.languages = opt.languages.clone(); + sub_opt.shape_rendering = opt.shape_rendering; + sub_opt.text_rendering = opt.text_rendering; + sub_opt.image_rendering = opt.image_rendering; + sub_opt.default_size = opt.default_size; + + let tree = match Tree::from_data(data, &sub_opt) { + Ok(tree) => tree, + Err(_) => { + log::warn!("Failed to load subsvg image."); + return None; + } + }; + + sanitize_sub_svg(&tree); + Some(tree) } // TODO: technically can simply override Options::image_href_resolver? diff --git a/crates/usvg-parser/src/lib.rs b/crates/usvg-parser/src/lib.rs index 64a789617..d5220bca4 100644 --- a/crates/usvg-parser/src/lib.rs +++ b/crates/usvg-parser/src/lib.rs @@ -38,7 +38,7 @@ mod units; mod use_node; pub use crate::options::*; -pub use image::ImageHrefResolver; +pub use image::{load_sub_svg, ImageHrefResolver}; pub use roxmltree; pub use svgtree::{AId, EId}; diff --git a/crates/usvg-tree/src/lib.rs b/crates/usvg-tree/src/lib.rs index 0fe49948f..ba8f5f4b6 100644 --- a/crates/usvg-tree/src/lib.rs +++ b/crates/usvg-tree/src/lib.rs @@ -914,7 +914,7 @@ pub enum ImageKind { /// A preprocessed SVG tree. Can be rendered as is. SVG(Arc>), /// RAW image data - RAW(u32, u32, Arc>) + RAW(u32, u32, Arc>), } impl std::fmt::Debug for ImageKind {