swhitty / SwiftDraw

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

Stroke gradient not supported? #17

Closed gbuela closed 1 year ago

gbuela commented 1 year ago

Hello, I'm trying to set some shape's stroke to a gradient and it's not getting rendered. Is that not supported? Is it something that you could add?

swhitty commented 1 year ago

HI @gbuela do you have an SVG sample file I can check.

There is basic support for stroking with a gradient on Apple platforms. There are limitations to the technique SwiftDraw uses that can cause the gradient to be rendered differently to the SVG standard.

See this sample: https://github.com/swhitty/SwiftDraw/blob/main/Samples/angry.svg

gbuela commented 1 year ago

Here's a sample

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns="http://www.w3.org/2000/svg"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:krita="http://krita.org/namespaces/svg/krita"
    xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
    width="150"
    height="150">
<defs>
  <linearGradient id="grad1" x1="0" y1="0" x2="0" y2="1" spreadMethod="pad">
    <stop stop-color="#ff0000" offset="0%" stop-opacity="1"/>
    <stop stop-color="#ffff00" offset="34%" stop-opacity="1"/>
    <stop stop-color="#00ff00" offset="50%" stop-opacity="1"/>
    <stop stop-color="#00ffff" offset="66%" stop-opacity="1"/>
    <stop stop-color="#0000ff" offset="100%" stop-opacity="1"/>
  </linearGradient>
</defs>
<circle cx="50" cy="50" r="40" stroke="url(#grad1)" stroke-width="10" fill="none"/>
</svg>
swhitty commented 1 year ago

I have extended the logic for determining the gradient start, end points to shapes and polygons. Try it out on the main branch

Screen Shot 2022-12-23 at 6 42 46 am
gbuela commented 1 year ago

Yup it seems to work fine!

swhitty commented 1 year ago

Included in 0.13.3 release