xsrf / easyeda-svg-import

Simple SVG Importer for EasyEDA PCB that doesn't convert everything to Comic Sans 😄
85 stars 8 forks source link

Incorrect handling of S/s command #24

Closed somewhatlurker closed 2 years ago

somewhatlurker commented 2 years ago

I have an SVG that renders incorrectly. After some investigation, I found that it often uses the "s" command without a preceding cubic Bezier. According to MDN docs (https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths), "If the S command doesn't follow another S or C command, then the current position of the cursor is used as the first control point.", however this extension currently appears to reflect the last used Bezier's control point regardless of preceding command.

 

With a small code change to the S and s cases, the image is rendered correctly:

k1x = cx;
k1y = cy;
if (cubicCmds.indexOf(lastLastCmd) >= 0) {
    k1x += cx-k2x;
    k1y += cy-k2y;
}

(cubicCmds is a constant set to ['C', 'c', 'S', 's'], lastLastCmd is copied from lastCmd at the start of the while loop)

 

Here's a minimal sample SVG that demonstrates the issue, along with before and after screenshots: s_command image image

 

I haven't opened this as a PR because you might have a better way of implementing a fix. This is just enough to demonstrate the issue.

somewhatlurker commented 2 years ago

Actually, on closer inspection the issue I reproduced is slightly different to the one I first noticed, since there is no previous Bezier curve in the example SVG's second path (so it just doubles the cursor position). Regardless, the incorrect behaviour is the same in both cases (trying to reflect a point when not following another Bezier).

somewhatlurker commented 2 years ago

Working perfectly now, thanks!