Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update to use resvg 0.33.0 #79

Merged
merged 2 commits into from May 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Expand Up @@ -13,8 +13,8 @@ crate-type = ["cdylib", "rlib"]
default = ["console_error_panic_hook"]

[dependencies]
resvg = { version = "0.23.0", default-features = false, features = ["filter"] }
usvg = { version = "0.23.0", default-features = false, features = [ "filter", "text"] }
resvg = { version = "0.33.0", default-features = false, features = ["text", "raster-images"] }
usvg = { version = "0.33.0", default-features = false, features = ["text"] }
svgtypes = "0.8.0"

tiny-skia = "0.6.1"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -87,7 +87,7 @@
"tslib": "^2.3.1",
"typescript": "^4.5.4",
"vitest": "^0.9.4",
"wasm-pack": "^0.10.2"
"wasm-pack": "^0.11.1"
},
"type": "module",
"packageManager": "pnpm@8.5.1"
Expand Down
73 changes: 39 additions & 34 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 34 additions & 23 deletions src/converter.rs
@@ -1,11 +1,10 @@
use std::collections::HashSet;
use std::str::FromStr;

use tiny_skia::Color;
use tiny_skia::Pixmap;
use tiny_skia::Transform;
use resvg::tiny_skia::{Color, Pixmap, Transform};
use usvg::fontdb::Database;
use usvg::{FitTo, OptionsRef, Size, Tree};
use usvg::{Options, Size, Tree};
use usvg::{TreeParsing, TreeTextToPath};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
Expand Down Expand Up @@ -43,34 +42,35 @@ impl Converter {
self.fantasy_family.as_deref(),
self.monospace_family.as_deref(),
);
let faces = fontdb.faces();
let default_font_family = if faces.is_empty() {
"sans-serif"
let faces = &mut fontdb.faces();
let default_font_family = if fontdb.is_empty() {
"sans-serif".to_string()
} else {
&faces[0].family
faces.next().clone().unwrap().families[0].0.to_string()
};
let svg_options = OptionsRef {
let svg_options = Options {
resources_dir: None,
dpi: 96.0,
font_family: default_font_family,
font_family: default_font_family.clone(),
font_size: 12.0,
languages: &["en".to_string()],
languages: vec!["en".to_string()],
shape_rendering: usvg::ShapeRendering::GeometricPrecision,
text_rendering: usvg::TextRendering::OptimizeLegibility,
image_rendering: usvg::ImageRendering::OptimizeQuality,
keep_named_groups: false,
default_size: Size::new(
width.unwrap_or(100.0).into(),
height.unwrap_or(100.0).into(),
)
.ok_or_else(|| JsValue::from_str("Invalid width or height"))?,
fontdb: &fontdb,
image_href_resolver: &usvg::ImageHrefResolver::default(),
image_href_resolver: usvg::ImageHrefResolver::default(),
};

let scale = scale.unwrap_or(1.0);
let tree =
let mut tree =
Tree::from_str(svg, &svg_options).map_err(|e| JsValue::from_str(&e.to_string()))?;
let svg_size = tree.svg_node().size;
tree.convert_text(&fontdb);

let svg_size = tree.size;
let (width, height) = match (width, height) {
(Some(w), Some(h)) => (w.round() as u32, h.round() as u32),
(Some(w), _) => (
Expand All @@ -94,12 +94,23 @@ impl Converter {
pixmap.fill(parse_color_string(&color));
}

resvg::render(
&tree,
FitTo::Size(width, height),
Transform::default(),
pixmap.as_mut(),
let rtree = resvg::Tree::from_usvg(&tree);

let size = resvg::IntSize::from_usvg(tree.size);
let size1 = size.to_size();
let fit_to_size = resvg::IntSize::new(width, height).map(|s| size.scale_to(s));
let size2 = match fit_to_size {
Some(v) => v.to_size(),
None => size.to_size(),
};

let tx = Transform::from_scale(
(size2.width() / size1.width()) as f32,
(size2.height() / size1.height()) as f32,
);

rtree.render(tx, &mut pixmap.as_mut());

pixmap
.encode_png()
.map_err(|e| JsValue::from_str(&e.to_string()))
Expand All @@ -109,8 +120,8 @@ impl Converter {
pub fn list_fonts(&self) -> Box<[JsValue]> {
load_fonts(&self.fonts, None, None, None, None, None)
.faces()
.iter()
.map(|f| &f.family)
.into_iter()
.map(|f| &f.families[0].0)
.collect::<HashSet<&String>>()
.iter()
.map(|s| JsValue::from_str(s))
Expand Down
Binary file modified test/vrt/expected/edge-circle.50w.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/vrt/expected/fe-turblence.1x.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/vrt/expected/fe-turblence.2x.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/vrt/expected/fe-turblence.50w.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/vrt/expected/fe-turblence.50w30h.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/vrt/expected/fe-turblence.blue.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/vrt/expected/fe-turblence.green.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/vrt/expected/fe-turblence.red.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.