From b4a81e84d5207a77a3beb76f674335cb3acdc574 Mon Sep 17 00:00:00 2001 From: Zimond Date: Mon, 24 Oct 2022 15:48:36 +0800 Subject: [PATCH] chore: upgrade to resvg/usvg 0.24 --- Cargo.toml | 16 +++----- __test__/index.spec.ts | 2 +- src/error.rs | 2 +- src/fonts.rs | 2 +- src/lib.rs | 92 ++++++++++++++---------------------------- src/options.rs | 2 +- 6 files changed, 41 insertions(+), 75 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b645edc9..97ba1c9c 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,10 +12,6 @@ crate-type = ["cdylib"] env_logger = "0.9.0" fontdb = "0.9.1" log = "0.4" -resvg = { version = "0.23.0", default-features = false, features = [ - "filter", - "dump-svg", -] } serde = { version = "1", features = ["derive"] } serde_json = "1" @@ -34,17 +30,17 @@ mimalloc-rust = { version = "0.2" } [target.'cfg(target_arch = "wasm32")'.dependencies] wasm-bindgen = "0.2.81" js-sys = "0.3.58" -usvg = { version = "0.23.0", default-features = false, features = [ - "export", +resvg = { version = "0.24.0", default-features = false, features = [ "filter", + "dump-svg", ] } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] napi = { version = "2.7.0", features = ["serde-json", "async"] } napi-derive = "2.6.0" -usvg = { version = "0.23.0", default-features = false, features = [ - "export", +resvg = { version = "0.24.0", default-features = false, features = [ "filter", + "dump-svg", "text", ] } @@ -59,5 +55,5 @@ opt-level = 3 codegen-units = 1 [patch.crates-io] -resvg = { git = "https://github.com/zimond/resvg", rev = "dc405c4" } -usvg = { git = "https://github.com/zimond/resvg", rev = "dc405c4" } +resvg = { git = "https://github.com/zimond/resvg", rev = "468b1bb" } +usvg = { git = "https://github.com/zimond/resvg", rev = "468b1bb" } diff --git a/__test__/index.spec.ts b/__test__/index.spec.ts index a81721a1..f664f1cd 100755 --- a/__test__/index.spec.ts +++ b/__test__/index.spec.ts @@ -449,7 +449,7 @@ test('should throw (no input parameters)', (t) => { ) t.is(error.code, 'InvalidArg') - t.is(error.message, 'Value is not either String or Vec') + t.is(error.message, 'Expect type String or Object, but got Undefined') }) test('should throw (SVG string is empty)', (t) => { diff --git a/src/error.rs b/src/error.rs index 0e54a2c3..309a0d7f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -9,7 +9,7 @@ pub enum Error { #[error(transparent)] SVG(#[from] svgtypes::Error), #[error(transparent)] - USvg(#[from] usvg::Error), + USvg(#[from] resvg::usvg::Error), #[error(transparent)] Encoding(#[from] png::EncodingError), #[error(transparent)] diff --git a/src/fonts.rs b/src/fonts.rs index dd1e801a..918b1dcd 100644 --- a/src/fonts.rs +++ b/src/fonts.rs @@ -9,7 +9,7 @@ use crate::options::*; use log::{debug, warn}; #[cfg(not(target_arch = "wasm32"))] -use usvg::fontdb::Database; +use resvg::usvg::fontdb::Database; /// Loads fonts. #[cfg(not(target_arch = "wasm32"))] diff --git a/src/lib.rs b/src/lib.rs index 8f7a207f..b0b2331d 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,7 @@ use pathfinder_geometry::vector::Vector2F; use napi_derive::napi; use options::JsOptions; use resvg::tiny_skia::Pixmap; -use usvg::{ImageKind, NodeKind}; +use resvg::usvg::{self, ImageKind, NodeKind}; #[cfg(target_arch = "wasm32")] use wasm_bindgen::{ prelude::{wasm_bindgen, JsValue}, @@ -173,13 +173,13 @@ impl Resvg { // Either depends on napi 2.4.3 // https://github.com/napi-rs/napi-rs/releases/tag/napi@2.4.3 pub fn inner_bbox(&self) -> Either { - let rect = self.tree.svg_node().view_box.rect; + let rect = self.tree.view_box.rect; let rect = points_to_rect( usvg::Point::new(rect.x(), rect.y()), usvg::Point::new(rect.right(), rect.bottom()), ); let mut v = None; - for child in self.tree.root().children().skip(1) { + for child in self.tree.root.children().skip(1) { let child_viewbox = match self.node_bbox(child).and_then(|v| v.intersection(rect)) { Some(v) => v, None => continue, @@ -209,8 +209,7 @@ impl Resvg { // Either depends on napi 2.4.3 // https://github.com/napi-rs/napi-rs/releases/tag/napi@2.4.3 pub fn get_bbox(&self) -> Either { - let node = self.tree.root(); - match node.calculate_bbox() { + match self.tree.root.calculate_bbox() { Some(bbox) => Either::A(BBox { x: bbox.x(), y: bbox.y(), @@ -228,14 +227,10 @@ impl Resvg { if !bbox.width.is_finite() || !bbox.height.is_finite() { return; } - let mut node = self.tree.root(); - let mut node = node.borrow_mut(); - if let usvg::NodeKind::Svg(svg) = &mut *node { - let width = bbox.width; - let height = bbox.height; - svg.view_box.rect = usvg::Rect::new(bbox.x, bbox.y, width, height).unwrap(); - svg.size = usvg::Size::new(width, height).unwrap(); - } + let width = bbox.width; + let height = bbox.height; + self.tree.view_box.rect = usvg::Rect::new(bbox.x, bbox.y, width, height).unwrap(); + self.tree.size = usvg::Size::new(width, height).unwrap(); } #[napi] @@ -252,13 +247,13 @@ impl Resvg { /// Get the SVG width #[napi(getter)] pub fn width(&self) -> f64 { - self.tree.svg_node().size.width().round() + self.tree.size.width().round() } /// Get the SVG height #[napi(getter)] pub fn height(&self) -> f64 { - self.tree.svg_node().size.height().round() + self.tree.size.height().round() } } @@ -289,13 +284,13 @@ impl Resvg { /// Get the SVG width #[wasm_bindgen(getter)] pub fn width(&self) -> f64 { - self.tree.svg_node().size.width().round() + self.tree.size.width().round() } /// Get the SVG height #[wasm_bindgen(getter)] pub fn height(&self) -> f64 { - self.tree.svg_node().size.height().round() + self.tree.size.height().round() } /// Renders an SVG in Wasm @@ -314,13 +309,13 @@ impl Resvg { /// Note: path bounding box are approx values. #[wasm_bindgen(js_name = innerBBox)] pub fn inner_bbox(&self) -> Option { - let rect = self.tree.svg_node().view_box.rect; + let rect = self.tree.view_box.rect; let rect = points_to_rect( usvg::Point::new(rect.x(), rect.y()), usvg::Point::new(rect.right(), rect.bottom()), ); let mut v = None; - for child in self.tree.root().children().skip(1) { + for child in self.tree.root.children() { let child_viewbox = match self.node_bbox(child).and_then(|v| v.intersection(rect)) { Some(v) => v, None => continue, @@ -345,8 +340,7 @@ impl Resvg { /// This will first apply transform. /// Similar to `SVGGraphicsElement.getBBox()` DOM API. pub fn get_bbox(&self) -> Option { - let node = self.tree.root(); - let bbox = node.calculate_bbox()?; + let bbox = self.tree.root.calculate_bbox()?; Some(BBox { x: bbox.x(), y: bbox.y(), @@ -362,14 +356,10 @@ impl Resvg { if !bbox.width.is_finite() || !bbox.height.is_finite() { return; } - let mut node = self.tree.root(); - let mut node = node.borrow_mut(); - if let usvg::NodeKind::Svg(svg) = &mut *node { - let width = bbox.width; - let height = bbox.height; - svg.view_box.rect = usvg::Rect::new(bbox.x, bbox.y, width, height).unwrap(); - svg.size = usvg::Size::new(width, height).unwrap(); - } + let width = bbox.width; + let height = bbox.height; + self.tree.view_box.rect = usvg::Rect::new(bbox.x, bbox.y, width, height).unwrap(); + self.tree.size = usvg::Size::new(width, height).unwrap(); } #[wasm_bindgen(js_name = imagesToResolve)] @@ -391,15 +381,6 @@ impl Resvg { } impl Resvg { - fn node_by_id(&self, id: &str) -> Option { - for node in self.tree.root().descendants() { - if id == node.borrow().id() { - return Some(node); - } - } - None - } - fn node_bbox(&self, node: usvg::Node) -> Option { let transform = node.borrow().transform(); let bbox = match &*node.borrow() { @@ -419,7 +400,7 @@ impl Resvg { } let mut outline = Outline::new(); let mut contour = Contour::new(); - let mut iter = p.data.0.iter().peekable(); + let mut iter = p.data.segments().peekable(); while let Some(seg) = iter.next() { match seg { usvg::PathSegment::MoveTo { x, y } => { @@ -427,10 +408,10 @@ impl Resvg { outline .push_contour(std::mem::replace(&mut contour, Contour::new())); } - contour.push_endpoint(Vector2F::new(*x as f32, *y as f32)); + contour.push_endpoint(Vector2F::new(x as f32, y as f32)); } usvg::PathSegment::LineTo { x, y } => { - let v = Vector2F::new(*x as f32, *y as f32); + let v = Vector2F::new(x as f32, y as f32); if let Some(usvg::PathSegment::ClosePath) = iter.peek() { let first = contour.position_of(0); if (first - v).square_length() < 1.0 { @@ -448,9 +429,9 @@ impl Resvg { y, } => { contour.push_cubic( - Vector2F::new(*x1 as f32, *y1 as f32), - Vector2F::new(*x2 as f32, *y2 as f32), - Vector2F::new(*x as f32, *y as f32), + Vector2F::new(x1 as f32, y1 as f32), + Vector2F::new(x2 as f32, y2 as f32), + Vector2F::new(x as f32, y as f32), ); } usvg::PathSegment::ClosePath => { @@ -480,14 +461,11 @@ impl Resvg { Some(outline.bounds()) } usvg::NodeKind::Group(g) => { - let clippath = if let Some(clippath) = g - .clip_path - .as_ref() - .and_then(|cp| self.node_by_id(cp)) - .and_then(|n| n.first_child()) + let clippath = if let Some(clippath) = + g.clip_path.as_ref().and_then(|n| n.root.first_child()) { self.node_bbox(clippath) - } else if let Some(mask) = g.mask.as_ref().and_then(|cp| self.node_by_id(cp)) { + } else if let Some(mask) = g.mask.as_ref().and_then(|n| n.root.first_child()) { self.node_bbox(mask) } else { Some(self.viewbox()) @@ -514,14 +492,6 @@ impl Resvg { usvg::Point::new(rect.right(), rect.bottom()), )) } - usvg::NodeKind::ClipPath(_) | usvg::NodeKind::Mask(_) => { - if let Some(child) = node.first_child() { - self.node_bbox(child) - } else { - None - } - } - _ => None, }?; let (x1, y1) = transform.apply(bbox.min_x() as f64, bbox.min_y() as f64); let (x2, y2) = transform.apply(bbox.max_x() as f64, bbox.max_y() as f64); @@ -549,7 +519,7 @@ impl Resvg { let pixmap_size = self .js_options .fit_to - .fit_to(self.tree.svg_node().size.to_screen_size()) + .fit_to(self.tree.size.to_screen_size()) .ok_or_else(|| Error::ZeroSized)?; let mut pixmap = self.js_options.create_pixmap(pixmap_size)?; // Render the tree @@ -583,7 +553,7 @@ impl Resvg { fn images_to_resolve_inner(&self) -> Result, Error> { let mut data = vec![]; - for mut node in self.tree.root().descendants() { + for node in self.tree.root.descendants() { if let NodeKind::Image(i) = &mut *node.borrow_mut() { if let ImageKind::RAW(_, _, buffer) = &mut i.kind { let s = String::from_utf8(buffer.clone())?; @@ -600,7 +570,7 @@ impl Resvg { let mime = infer::get(&buffer) .ok_or_else(|| Error::UnrecognizedBuffer)? .to_string(); - for mut node in self.tree.root().descendants() { + for node in self.tree.root.descendants() { if let NodeKind::Image(i) = &mut *node.borrow_mut() { let matched = if let ImageKind::RAW(_, _, data) = &mut i.kind { let s = String::from_utf8(data.clone()).map_err(Error::from)?; diff --git a/src/options.rs b/src/options.rs index fdebd101..318da63d 100644 --- a/src/options.rs +++ b/src/options.rs @@ -9,8 +9,8 @@ use fontdb::Database; use napi::{bindgen_prelude::Buffer, Either}; use resvg::tiny_skia::Pixmap; use resvg::usvg::ScreenSize; +use resvg::usvg::{self, ImageHrefResolver, ImageKind, OptionsRef}; use serde::{Deserialize, Deserializer}; -use usvg::{ImageHrefResolver, ImageKind, OptionsRef}; /// Image fit options. /// This provides the deserializer for `usvg::FitTo`.