Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/resvg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ log = "0.4"
pico-args = { version = "0.5", features = ["eq-separator"] }
rgb = "0.8"
svgtypes = "0.15.3"
tiny-skia = "0.11.4"
tiny-skia = { git = "https://github.com/linebender/tiny-skia", rev = "68b198a" }
usvg = { path = "../usvg", version = "0.45.1", default-features = false }
zune-jpeg = { version = "0.4", optional = true }

Expand Down
45 changes: 25 additions & 20 deletions crates/resvg/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,30 +389,29 @@ enum FitTo {
/// Keep original size.
Original,
/// Scale to width.
Width(u32),
Width(f32),
/// Scale to height.
Height(u32),
Height(f32),
/// Scale to size.
Size(u32, u32),
Size(f32, f32),
/// Zoom by factor.
Zoom(f32),
}

impl FitTo {
fn fit_to_size(&self, size: tiny_skia::IntSize) -> Option<tiny_skia::IntSize> {
fn fit_to_size(&self, size: tiny_skia::Size) -> Option<tiny_skia::Size> {
match *self {
FitTo::Original => Some(size),
FitTo::Width(w) => size.scale_to_width(w),
FitTo::Height(h) => size.scale_to_height(h),
FitTo::Size(w, h) => tiny_skia::IntSize::from_wh(w, h).map(|s| size.scale_to(s)),
FitTo::Size(w, h) => tiny_skia::Size::from_wh(w, h).map(|s| size.scale_to(s)),
FitTo::Zoom(z) => size.scale_by(z),
}
}

fn fit_to_transform(&self, size: tiny_skia::IntSize) -> tiny_skia::Transform {
let size1 = size.to_size();
let size2 = match self.fit_to_size(size) {
Some(v) => v.to_size(),
fn fit_to_transform(&self, size1: tiny_skia::Size) -> tiny_skia::Transform {
let size2 = match self.fit_to_size(size1) {
Some(v) => v,
None => return tiny_skia::Transform::default(),
};
tiny_skia::Transform::from_scale(
Expand Down Expand Up @@ -526,13 +525,13 @@ fn parse_args() -> Result<Args, String> {
let mut default_size = usvg::Size::from_wh(100.0, 100.0).unwrap();
if let (Some(w), Some(h)) = (args.width, args.height) {
default_size = usvg::Size::from_wh(w as f32, h as f32).unwrap();
fit_to = FitTo::Size(w, h);
fit_to = FitTo::Size(w as f32, h as f32);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense for these arguments to themselves be floats? I think probably not, because they need to be positive and finite. But creating a massive image can still cause issues.

} else if let Some(w) = args.width {
default_size = usvg::Size::from_wh(w as f32, 100.0).unwrap();
fit_to = FitTo::Width(w);
fit_to = FitTo::Width(w as f32);
} else if let Some(h) = args.height {
default_size = usvg::Size::from_wh(100.0, h as f32).unwrap();
fit_to = FitTo::Height(h);
fit_to = FitTo::Height(h as f32);
} else if let Some(z) = args.zoom {
fit_to = FitTo::Zoom(z);
}
Expand Down Expand Up @@ -681,19 +680,21 @@ fn render_svg(args: &Args, tree: &usvg::Tree) -> Result<tiny_skia::Pixmap, Strin

let size = args
.fit_to
.fit_to_size(bbox.size().to_int_size())
.fit_to_size(bbox.size())
.ok_or_else(|| "target size is zero".to_string())?;

// Unwrap is safe, because `size` is already valid.
let mut pixmap = tiny_skia::Pixmap::new(size.width(), size.height()).unwrap();
let mut pixmap =
tiny_skia::Pixmap::new(size.width().ceil() as u32, size.height().ceil() as u32)
.unwrap();

if !args.export_area_page {
if let Some(background) = args.background {
pixmap.fill(svg_to_skia_color(background));
}
}

let ts = args.fit_to.fit_to_transform(tree.size().to_int_size());
let ts = args.fit_to.fit_to_transform(tree.size());

resvg::render_node(node, ts, &mut pixmap.as_mut());

Expand All @@ -702,11 +703,13 @@ fn render_svg(args: &Args, tree: &usvg::Tree) -> Result<tiny_skia::Pixmap, Strin

let size = args
.fit_to
.fit_to_size(tree.size().to_int_size())
.fit_to_size(tree.size())
.ok_or_else(|| "target size is zero".to_string())?;

// Unwrap is safe, because `size` is already valid.
let mut page_pixmap = tiny_skia::Pixmap::new(size.width(), size.height()).unwrap();
let mut page_pixmap =
tiny_skia::Pixmap::new(size.width().ceil() as u32, size.height().ceil() as u32)
.unwrap();

if let Some(background) = args.background {
page_pixmap.fill(svg_to_skia_color(background));
Expand All @@ -727,17 +730,19 @@ fn render_svg(args: &Args, tree: &usvg::Tree) -> Result<tiny_skia::Pixmap, Strin
} else {
let size = args
.fit_to
.fit_to_size(tree.size().to_int_size())
.fit_to_size(tree.size())
.ok_or_else(|| "target size is zero".to_string())?;

// Unwrap is safe, because `size` is already valid.
let mut pixmap = tiny_skia::Pixmap::new(size.width(), size.height()).unwrap();
let mut pixmap =
tiny_skia::Pixmap::new(size.width().ceil() as u32, size.height().ceil() as u32)
.unwrap();

if let Some(background) = args.background {
pixmap.fill(svg_to_skia_color(background));
}

let ts = args.fit_to.fit_to_transform(tree.size().to_int_size());
let ts = args.fit_to.fit_to_transform(tree.size());

resvg::render(tree, ts, &mut pixmap.as_mut());

Expand Down
Binary file modified crates/resvg/tests/tests/text/text-rendering/optimizeSpeed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified crates/resvg/tests/tests/text/text-rendering/with-underline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion crates/usvg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ log = "0.4"
pico-args = { version = "0.5", features = ["eq-separator"] }
strict-num = "0.1.1"
svgtypes = "0.15.3"
tiny-skia-path = "0.11.4"
tiny-skia-path = { git = "https://github.com/linebender/tiny-skia", rev = "68b198a" }
xmlwriter = "0.1"

# parser
Expand Down