RazrFalcon / resvg

An SVG rendering library.
Mozilla Public License 2.0
2.61k stars 215 forks source link

Add optional parameters to limit the maximum dimensions ---max-width, --max-height #647

Open totoestcontent opened 11 months ago

totoestcontent commented 11 months ago

Some svgs have a huge viewbox and I would need to limit the size of the png produced. The current settings (-w and -h) allow this if the image is larger than the given values. If the image is smaller then it will be enlarged. The idea is therefore not to exceed a given dimension (optional).

I made the changes and created a git patch.

Be indulgent, it's the first time I do Rust ;-) So you can do much better, but if it can make your job easier, I'd be happy ;-)

add_max_width_height.patch

RazrFalcon commented 11 months ago

What behavior do you expect? I would assume that images larger than required should simply be rejected, while your patch simply downscales them.

totoestcontent commented 11 months ago

I need to preview all svg images with a maximum dimension of 200x200. If the svgs are smaller than 200x200 then the previews must be the size of the svg. If the svgs are larger, then the preview should fit into a 200x200 rectangle. So I don't want to reject some images because they're too big.

RazrFalcon commented 11 months ago

Isn't this exactly what the current --width + --height attributes are doing?

totoestcontent commented 11 months ago

Not exactly.

With image 900_600.svg if i call: resvg --width 200 900_600.svg 900_600_width_200.png , png has dimension: 200x134 With image 32x32.svg if i call: resvg --width 200 32_32.svg 32_32_width_200.png, png has dimension: 200x200, with --max-width, i want: 200x134 and 32x32 900_600 32_32 900_600_width_200 svg 32_32_width_200 svg

totoestcontent commented 11 months ago

And if i use --width and --height as resvg.exe --width 200 --height 200 32_32.svg 32_32_width_200.svg.png png has dimension: 200x200 and not 32x32.

maybe the problem is elsewhere and the method tiny_skia::size::scale_to doesn't do exactly what it suggests...

RazrFalcon commented 11 months ago

Oh, so you do not what to upscale the SVG, only downscale it. I get it now. Yes, this is not supported. Will think how it could be done. To me, the main issue is naming, since when I see --max-width I think the max limit, a cut off, which is not what you want. I do plan an option that would return an error when the limit is reached. It's more like --width-limit to me. Or maybe even --downscale-only.

PS: you can easily achieve what you want from code, obviously. But it would take some time to add to the CLI.

totoestcontent commented 11 months ago

Thank you for being so responsive.

Indeed, the name of my parameter can be confusing. I suggested this parameter name in reference to the max-width/height CSS property.

You are better placed than me to choose a name :) .

RazrFalcon commented 11 months ago

I suggested this parameter name in reference to the max-width/height CSS property.

Interesting, so those CSS properties do work as you want? I'm not a web developer, so I'm not very familiar with CSS. If so, then we definitely should use them for consistency.

totoestcontent commented 11 months ago

Yes these CSS properties do exactly that. They resize the image to fit within a maximum rectangle. Proportionality is preserved. Image is not enlarged if it is smaller.