I'm playing with SVG loading via `svgResource` and...
# compose-desktop
d
I'm playing with SVG loading via
svgResource
and finding that it plainly does not resize as expected. Currently only trying on Retina MacBook Pro so wondering if screen DPI is playing a part? Anyone tried this? I set a background colour via Modifier to see the layout of the Image element and that is correct; but the SVG itself never scales within, regardless of
ContentScale
passed in ๐Ÿคท
i
Screen DPI should affect the size of SVG. What Compose version do you use? After 0.4.0 we changed SVG resizing logic, you can switch to the latest 0.5.0-buildXXX
d
I'm using
0.5.0-build225
i
Can you show the code where you load SVG? You want SVG to fit the all available space in the Box?
d
So as a sanity check I'm down to this:
Copy code
Image(
            painter = svgResource("MyImage.svg"),
            contentDescription = "",
            modifier = Modifier
                .requiredWidth(442.dp).requiredHeight(64.dp).background(Color.Red),
            contentScale = ContentScale.FillBounds,
            alignment = Alignment.Center
        )
Even without showing you the image, I would expect to see no Red
Because FillBounds should cover all the background.
(White block is part of SVG image)
I'm aware that to scale to a precise width or height myself, I'll need to manage some aspect ratio calculations. So I was doing that originally; and suspected I had a problem in my calculations. In the end, I did this more hard-coded PoC and it seems the
ContentScale
is having no effect.
Now I'll try 0.4.0 ๐Ÿ™‚
i
I tried this code with this SVG. And it looks ok. Can you show the SVG? Maybe internally it uses some other units.
d
I'll DM you the SVG file
๐Ÿ†— 1
i
I'm aware that to scale to a precise width or height myself
to preserve aspect ratio, you can use the default
ContentScale.Fit
d
Understand that will maintain the aspect ratio, but not guarantee overall size. To get the image in the original aspect ratio, on screen, for a given, fixed height or width, I must calculate the other dimension.
Changing some other settings like 'Relative Coordinates' had no effect, but the DPI did.
Somehow it looks like the way this SVG file is expressing its coordinates, gets overlooked by the Compose scaling code(?)
i
Adding
viewBox="0 0 615 89
to the root fixes the issue. To my shame, we didn't fix this issue. Reopened it. Trying to fix this now.
d
Alright; glad to have highlighted an issue and thanks to you for being so responsive ๐Ÿ‘
i
Ok, fixed it. Will be fixed in the next build, after review. Thanks for letting us now about this bug ๐Ÿ™‚
jetpack compose 1
๐ŸŽ‰ 2
d
Thank you @Igor Demin ๐Ÿ™ Since its 23:50 in my time-zone; you have just saved me the torture of lying in bed, wondering what I did wrong in my code ๐Ÿ˜†.
๐Ÿ˜„ 3