Closed zhanghai closed 1 year ago
Sorry for the long delay on this! I've avoided this issue for a while since I don't think I have enough understanding of the SVG spec to say what the correct behaviour should be here. It's possible Coil is implementing logic out of line with the spec, however overall I lean towards keeping the behaviour as is since there haven't been any follow up issue reports related to SVG rendering and this behaviour.
Additionally, I think changing the default behaviour at this point would be more detrimental to end users even if the behaviour is incorrect. Also it's possible to avoid the default behaviour by setting useViewBoundsAsIntrinsicSize = false
.
Describe the bug
(Copying discussion from #688 becuase it's missing follow-up probably because it's a PR instead of an issue.)
I see, thanks for the clarification about using view box for fitting the target size. However, it still seems to me the original behavior is correct.
This doesn't sound correct according to my understanding of SVG.
Although the view box may be smaller, it's still perfectly valid for content to be drawn outside the view box and be shown in the final SVG, because:
the
viewBox
attribute is essentially just a shorthand for a transformation matrix:the default value for
preserveAspectRatio
(whenviewBox
is present) isxMidYMid
instead ofnone
, so that uniform scaling is performed and the view box is centered, leaving additional space for drawing.(One potential source of confusion is that for Android VectorDrawable XML files, there is no
preserveAspectRatio
equivalent andandroid:viewportWidth
&android:viewportHeight
behaves as ifpreserveAspectRatio
isnone
. But Android VectorDrawable XML isn't SVG.)Even if no content is drawn outside of the view box, it is still perfectly valid for an SVG file to utilize the view box for transformation, and it should be handled as if the transformation were applied at a root group instead of as the view box.
(i.e. I didn't find anything in the specification that says the
viewBox
attribute should be the viewport of the user agent, and on the contrary, the specification seems to suggest theviewBox
attribute is just another way to specify a transformation. The value forviewBox
are the coordinates pre-tranform, but it's the post-tranform coordinates that should be rendered and respected.)Take this SVG file for an ellipse as an example:
It should be handled exactly like the following equivalent SVG file:
I've confirmed that both EOG and Inkscape treats the two SVG files in exactly the same way.
This is actually also covered similarly in the specification's discussion of how the user agent may implement
viewBox
withtransform
:Even if some non-standard handling of
viewBox
for fitting the target size is desired for some reason, it should actually respect the value ofpreserveAspectRatio
to be a correct implementation.The current implementation would return an incorrect result if
preserveAspectRatio
is set tonone
, in which case the aspect ratio from thewidth
andheight
attributes should definitely be used instead of the one fromviewBox
.I still don't see why we need non-standard handling of
viewBox
for fitting the target size though.Expected behavior
SVG view box should not be used for its aspect ratio.
To Reproduce
Read changelog or #688.
Logs/Screenshots
N/A
Version
1.2.0