Skip to content

Commit

Permalink
feat: use ImageKind::RAW to store urls
Browse files Browse the repository at this point in the history
  • Loading branch information
zimond committed May 5, 2022
1 parent 24ceb2c commit 61b0cc2
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,14 @@ impl Resvg {

let mut opts = js_options.to_usvg_options();
opts.image_href_resolver = ImageHrefResolver::default();
opts.image_href_resolver.resolve_string =
Box::new(move |data: &str, _: &OptionsRef| Some(ImageKind::SVG(data.as_bytes().to_vec())));
opts.image_href_resolver.resolve_string = Box::new(move |data: &str, opts: &OptionsRef| {
if data.starts_with("https://") || data.starts_with("http://") {
Some(ImageKind::RAW(0, 0, data.as_bytes().to_vec()))
} else {
let resolver = ImageHrefResolver::default().resolve_string;
(resolver)(data, opts)
}
});
let opts_ref = opts.to_ref();
// Parse the SVG string into a tree.
let tree = match svg {
Expand Down Expand Up @@ -149,7 +155,7 @@ impl Resvg {
let mut data = vec![];
for mut node in self.tree.root().descendants() {
if let NodeKind::Image(i) = &mut *node.borrow_mut() {
if let ImageKind::SVG(buffer) = &mut i.kind {
if let ImageKind::RAW(_, _, buffer) = &mut i.kind {
let s = String::from_utf8(buffer.clone()).map_err(Error::from)?;
data.push(s);
}
Expand All @@ -164,7 +170,7 @@ impl Resvg {
let options = self.js_options.to_usvg_options();
for mut node in self.tree.root().descendants() {
if let NodeKind::Image(i) = &mut *node.borrow_mut() {
let matched = if let ImageKind::SVG(data) = &mut i.kind {
let matched = if let ImageKind::RAW(_, _, data) = &mut i.kind {
let s = String::from_utf8(data.clone()).map_err(Error::from)?;
s == href
} else {
Expand Down

0 comments on commit 61b0cc2

Please sign in to comment.