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
SVG dimensions get (wrongly) rounded #64
Comments
Running into this same issue when getting SVG dimensions from the width/height and viewbox. For SVGs it seems like you'd definitely not want to round the values at all. |
@Pomax @mdmoreau could you comment SVG use cases? I'm designing SVG support API, and not sure that mixing floats & ints is good idea nodeca/probe-image-size#4.
Mixin everything into existing properties can require additional checks after getting result for many cases where SVG is not expected). Solution could be:
What do you think? |
If you work with SVG, you will always run into this. Tons of SVG tools like inkscape, Sketch, etc. will throw SVG exports at you with decimal values.
As for the proposed solutions:
I filed a PR that fixes this problem months ago, just have a look at that. |
I've seen your commit, and asked questions after that.
|
I think the distinction is what @Pomax mentioned initially - bitmap images will always be integers while SVGs are designed to use integers and/or decimals. Not supporting decimals ignores a pretty core feature of the SVG format in my opinion, especially since rounding will negatively impact most SVGs. My primary use case involves using the dimensions to build a responsive container for an SVG image. Part of a mustache template I have uses the following snippet to generate some CSS: .svgstore {
position: relative;
pointer-events: none;
svg {
position: absolute;
width: 100%;
height: 100%;
}
}
.svgstore--{{ name }} {
padding-bottom: {{ height }} / {{ width }} * 100%;
} I then have a nunjucks macro that outputs the code which allows the SVG to scale correctly without needing to have the inline viewbox attributes: <div class="svgstore svgstore--{{ svg }}">
<svg>
<use xlink:href="/img/svgstore.svg#{{ svg }}"></use>
</svg>
</div> When the numbers are rounded, this ends up throwing off the padding-bottom value that scales the container, messing up the display of the SVG. |
@mdmoreau I agree, if use-case is "keep aspect ratio", then units are not needed. Need to understand if there are other cases, where lack of units can become a problem. My case is post-processing images at forum posts, to define width/height and avoid reflows on page load. But we don't care much about SVG insert. It's even better to ignore "not pixels" for our case. |
@puzrin the change does not affect processing of integer values, as As for 2, losing the unit is not going to ruin anyone's day nearly as much as losing the fractions currently does. Losing the unit just means you might have to apply a uniform scale, and as developer you know how to best solve that (in app, CSS, etc). While it might be a bother, you can easily fix it. On the other hand, losing the fractions means you've lost all image aspect integrity, and that's something you cannot fix without uninstalling this library and using something else entirely (like manual XML parsing of the SVG source). So: land it, and you've reduced the current bug's surface to a much, much smaller set of cases, which unlike before do have a workable solution even when users run into them. And then for good process, you file a follow-up issue to investigate whether the loss of units is a genuine problem, while in the mean time you've vastly improved the library for everyone, and maintaining backward compatibility. |
@Pomax if we speak about existing If we speak about What do you think about adding property |
I don't use that library, so my main concern is to fix this issue, for this library, in a way that is backward compatible. Given that it is, any library that relies on this one will not be impacted and any additional work that happens specifically in dependents is honestly something to discuss on their repos, not something that should hold up progress in this library. |
SVG support is now available in https://github.com/nodeca/probe-image-size. With floats, units info etc. |
That's awesome, @puzrin 👏 You might want to update the GitHub repo description to mention SVG: |
@zeke thanks, updated :) . I hope code quality is high, but still interested in massive feedback. |
nice @puzrin! |
I tried using
image-size
with (autogenerated) SVG files that specify their dimensions inex
units, such as:but the dimensions get reported as being width
69
and height2
, with the rather important decimal fraction part removed. For bitmap images this makes sense, but for SVG this means the dimensions are very wrong and cannot be relied upon.Worth not rounding dims for vector format images?
The text was updated successfully, but these errors were encountered: