swhitty / SwiftDraw

Swift library and command line tool to convert SVGs into SFSymbol, PNG, PDF and Swift source code.
zlib License
419 stars 50 forks source link

Embedded Base64 image isn’t rendered #52

Open WinWurm opened 1 week ago

WinWurm commented 1 week ago

Hi, i have faced with rendering issues with some svg files. The embedded png image doesn’t rendered Here is the file response3

File structure look like this

<svg width="80" height="80" viewBox="0 0 80 80" fill="none" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <rect width="80" height="80" rx="8" fill="white"/>
    <rect x="2" y="10.7491" width="76" height="58.5017" fill="url(#pattern0_373_2050)"/>
    <defs>
        <pattern id="pattern0_373_2050" patternContentUnits="objectBoundingBox" width="1" height="1">
            <use xlink:href="#image0_373_2050" transform="scale(0.00171821 0.00223214)"/>
        </pattern>
        <image id="image0_373_2050" width="582" height="448" xlink:href="data:image/png;base64,<image data>"/>
    </defs>
</svg>

if i change file structure to more simple form (remove defs section) all renders correct

<svg width="80" height="80" viewBox="0 0 80 80" fill="none" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <rect width="80" height="80" rx="8" fill="white"/>
    <image  x="2" y="10.7491" width="76" height="58.5017" xlink:href="data:image/png;base64,<image data>"/>
</svg>

I don’t know exactly, but it’s looks like the issue occurs because image UIGraphicsImageRenderer initiated with svg size (defined at file beginning) and embedded image renders in more bigger rect width="582" height="448" by using CGRenderer.draw(image: CGImage, in rect: CGRect) and only after rendering its scaling

swhitty commented 5 days ago

Thanks for reporting this, it appears to be an issue when using <pattern> and how it calculates the projected coordinates.
I will investigate further but sidestepping pattern and directly using <image> is best for now 👍🏻