Skip to content

Commit

Permalink
chore: upgrade to resvg/usvg 0.24
Browse files Browse the repository at this point in the history
  • Loading branch information
zimond committed Oct 31, 2022
1 parent bc1ab90 commit c3441b6
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 75 deletions.
16 changes: 6 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.25.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.25.0", default-features = false, features = [
"filter",
"dump-svg",
"text",
] }

Expand All @@ -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" }
2 changes: 1 addition & 1 deletion __test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8>')
t.is(error.message, 'Expect type String or Object, but got Undefined')
})

test('should throw (SVG string is empty)', (t) => {
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion src/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand Down
92 changes: 31 additions & 61 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -173,13 +173,13 @@ impl Resvg {
// Either<T, Undefined> depends on napi 2.4.3
// https://github.com/napi-rs/napi-rs/releases/tag/[email protected]
pub fn inner_bbox(&self) -> Either<BBox, Undefined> {
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,
Expand Down Expand Up @@ -209,8 +209,7 @@ impl Resvg {
// Either<T, Undefined> depends on napi 2.4.3
// https://github.com/napi-rs/napi-rs/releases/tag/[email protected]
pub fn get_bbox(&self) -> Either<BBox, Undefined> {
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(),
Expand All @@ -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]
Expand All @@ -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()
}
}

Expand Down Expand Up @@ -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
Expand All @@ -314,13 +309,13 @@ impl Resvg {
/// Note: path bounding box are approx values.
#[wasm_bindgen(js_name = innerBBox)]
pub fn inner_bbox(&self) -> Option<BBox> {
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,
Expand All @@ -345,8 +340,7 @@ impl Resvg {
/// This will first apply transform.
/// Similar to `SVGGraphicsElement.getBBox()` DOM API.
pub fn get_bbox(&self) -> Option<BBox> {
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(),
Expand All @@ -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)]
Expand All @@ -391,15 +381,6 @@ impl Resvg {
}

impl Resvg {
fn node_by_id(&self, id: &str) -> Option<usvg::Node> {
for node in self.tree.root().descendants() {
if id == node.borrow().id() {
return Some(node);
}
}
None
}

fn node_bbox(&self, node: usvg::Node) -> Option<RectF> {
let transform = node.borrow().transform();
let bbox = match &*node.borrow() {
Expand All @@ -419,18 +400,18 @@ 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 } => {
if !contour.is_empty() {
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 {
Expand All @@ -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 => {
Expand Down Expand Up @@ -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())
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -583,7 +553,7 @@ impl Resvg {

fn images_to_resolve_inner(&self) -> Result<Vec<String>, 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())?;
Expand All @@ -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)?;
Expand Down
2 changes: 1 addition & 1 deletion src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down

0 comments on commit c3441b6

Please sign in to comment.