Skip to content

Commit

Permalink
load sub svg in resvg
Browse files Browse the repository at this point in the history
  • Loading branch information
zimond committed Sep 25, 2023
1 parent 9a44a3c commit 6dbd057
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
8 changes: 5 additions & 3 deletions crates/resvg/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -36,7 +38,9 @@ pub fn convert(image: &usvg::Image, children: &mut Vec<Node>) -> Option<BBoxes>
}

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"))]
Expand Down Expand Up @@ -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;
Expand Down
48 changes: 24 additions & 24 deletions crates/usvg-parser/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -250,27 +250,27 @@ fn get_image_data_format(data: &[u8]) -> Option<ImageFormat> {
///
/// 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<ImageKind> {
// 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<Tree> {
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?
Expand Down
2 changes: 1 addition & 1 deletion crates/usvg-parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
2 changes: 1 addition & 1 deletion crates/usvg-tree/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ pub enum ImageKind {
/// A preprocessed SVG tree. Can be rendered as is.
SVG(Arc<Vec<u8>>),
/// RAW image data
RAW(u32, u32, Arc<Vec<u8>>)
RAW(u32, u32, Arc<Vec<u8>>),
}

impl std::fmt::Debug for ImageKind {
Expand Down

0 comments on commit 6dbd057

Please sign in to comment.