Skip to content

Support floating point viewbox in resvg binary - #875

Merged
LaurenzV merged 2 commits into
linebender:mainfrom
jermy:float_support
Oct 8, 2026
Merged

LaurenzV merged 2 commits into
linebender:mainfrom
jermy:float_support

Conversation

@jermy

@jermy jermy commented Dec 27, 2024 •

Copy link
Copy Markdown
Contributor

Avoid rounding sizes for images until creating the pixmap, and always use the ceil for that to avoid truncating images. This fixes #810.

This requires a corresponding change to tiny-skia to add scale_by/scale_to_width/scale_to_height functions to tiny_skia_path::Size to match the implementations in IntSize.

This won't compile as-is, since it depends on linebender/tiny-skia#146 or a similar change.

Comment thread crates/resvg/src/main.rs
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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let's ignore this for now, if you want to render such big images you are going to have other problems. 😄

@RiedleroD

Copy link
Copy Markdown

don't want to be annoying, but - ping! I'm still waiting on this so I can stop having inkscape as a dependency in my software =w=

@RiedleroD

Copy link
Copy Markdown

a-another ping 😓 sorry… impatient

@LaurenzV

LaurenzV commented Jul 7, 2025

Copy link
Copy Markdown
Collaborator

What’s missing for this? Just a bump in tiny skia?

@jermy

jermy commented Jul 7, 2025

Copy link
Copy Markdown
Contributor Author

That's my understanding, yes. Once that's available then we can bump dependencies here (and consider a resvg release?).

The only question is if it's useful to have width and height directly parsed from the command line as f32 as @DJMcNab has pondered. If you used --width 33.5 --height 27.6 you'd still end up with a 34x28 PNG, but maybe with content outside of the bounding box rather than truncating it?

@LaurenzV

LaurenzV commented Jul 7, 2025

Copy link
Copy Markdown
Collaborator

Thanks, will do my best to take a closer soon. Sorry for the delay.

@LaurenzV

Copy link
Copy Markdown
Collaborator

Had to update two test cases, but only with very minimal pixel differences. I added a git dependency now because I think it'll still be a while until the next release.

I think we can merge this as is, @DJMcNab wdyt?

@DJMcNab

DJMcNab commented Jul 11, 2025 •

Copy link
Copy Markdown
Member

I'd be hesistant to land a git dependency here - I'm not sure when we would otherwise made a Tiny Skia release, so using this to force that is probably worthwhile.

@jermy, would you be willing to help here? The way to unblock a Tiny Skia release is to go through the changes since the last release, and check if any of them are breaking. At the same time, it would be great to add them to the Unreleased section of the changelog. If they aren't, we should be able to make a bump fairly easily.

Unfortunately, if there are breaking changes, the path forward is less clear.

@LaurenzV

LaurenzV commented Jul 11, 2025 •

Copy link
Copy Markdown
Collaborator

It is a breaking change I think, because new methods were added in the PR to tiny-skia.

@RiedleroD

Copy link
Copy Markdown

hey, hate to bump, but what is the course of action from here? is there any way I can help get this through faster? I would really like to have this so I can replace inkscape with resvg as my primary SVG rasterizer…

@LaurenzV

Copy link
Copy Markdown
Collaborator

Sorry about that. :( The reason nothing is happening is basically 1) No one's really been actively developing resvg in recent months (I guess mostly because of being busy with other stuff) and 2) tiny-skia is basically unmaintained at this point (we have our own new CPU renderer that we are developing), and therefore there's little motivation to add new features and updates to the crate... So things are basically not moving at all at the moment. 😅 Hopefully this can change soon, but hard to say. :/

@LaurenzV

Copy link
Copy Markdown
Collaborator

I'm wondering though, is that really such a critical thing to have? To me it seems like a pretty small edge case, no?

@RiedleroD

Copy link
Copy Markdown

in my use-case, a lot of the images I use a rasterizer on are small enough (in SVG units) to notice discrepancies at larger scales (in pixels)

the only reason I even noticed is because it really fucked with a bunch of my images. for example:

resvg inkscape
blobcat_thinkage_sceptic blobcat_thinkage_sceptic_is

↑ one of the less bad examples. I don't have time to look for a worse one rn, but notice how the bottom and right sides are cut off a bit in the resvg render

@luisbg

luisbg commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

This is unblocked now. The reason it stalled (needing Size::scale_by/scale_to_width/scale_to_height from tiny-skia) no longer applies: those all shipped in the released tiny-skia-path 0.12.0 that resvg already depends on.

I confirmed the bug is still live on main. Red extends to pixel 60, i.e. 1.2 ÷ 2.0, so the 1.5 viewBox is being rounded up.

I rebased locally to check it works: dropped the git pins, resolved the conflicts, builds clean and all 1,793 tests pass. Afterwards red extends to pixel 80 (1.2 ÷ 1.5), the corner is blue, and there's no green anywhere.

Two things worth noting from the rebase:

  1. The two golden PNG updates are no longer needed. main moved on and the current references now match what this branch renders. I kept main's versions and the suite is green.
  2. The main.rs conflict isn't mechanical. main gained graceful handling for the i32::MAX/4 pixmap limit while this PR uses .unwrap() with "Unwrap is safe, because size is already valid." That reasoning no longer holds once ceil() can push a size over the limit. I resolved it by keeping the float sizing and main's error handling.

@jermy are you still up for finishing this? Happy to push the rebase if it's easier. @RiedleroD , sorry it took so long.

@RiedleroD

Copy link
Copy Markdown

all that matters is that it's getting done eventually ^^ I'm just happy it's not being dropped

@jermy

jermy commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

@jermy are you still up for finishing this? Happy to push the rebase if it's easier. @RiedleroD , sorry it took so long.

I'm fairly busy at the moment - I don't have any issue with you taking this and doing that rebase and rework as you see fit. Thank you.

@quantum-incongruity

Copy link
Copy Markdown

This is unblocked now. The reason it stalled (needing Size::scale_by/scale_to_width/scale_to_height from tiny-skia) no longer applies: those all shipped in the released tiny-skia-path 0.12.0 that resvg already depends on.

Hey @luisbg are you a new maintainer to resvg?
Do you know when a new version will be released?

@luisbg

luisbg commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

I am just helping, not a maintainer. Let me ask when a new version will be released.

@luisbg

luisbg commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

No release scheduled, unless there is something important.

jermy and others added 2 commits October 8, 2026 20:45
Avoid rounding sizes for images until creating the pixmap, and always
use the ceil for that to avoid truncating images.

This requires a corresponding change to tiny-skia to add
scale_by/scale_to_width/scale_to_height functions to
tiny_skia_path::Size to match the implementations in IntSize.
@LaurenzV

LaurenzV commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

@jermy Thank you again, and very sorry that this took so long. :(

@LaurenzV
LaurenzV merged commit 617ceba into linebender:main Oct 8, 2026
10 checks passed
@RiedleroD

Copy link
Copy Markdown

thanks everyone for making this happen ^^ once this lands in a release I can finally go back to using resvg for my image converter script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

viewbox values get rounded to nearest int

6 participants