diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fe1c3f1..86d0a233 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ### Unreleased +- Fix smooth curves (`S`, `T`) in SVG paths reflecting the control point of an arc or of a curve of the other kind. As the SVG spec says, they now start from the current point there. An arc with a zero radius is now drawn as a straight line instead of nothing + ### [v0.21.0] - 2026-11-19 - Fix gradients whose last stop comes before 1 stretching the stops over the whole gradient. The last color now stays solid from that stop to the end diff --git a/lib/path.js b/lib/path.js index 59a274c2..d7041255 100644 --- a/lib/path.js +++ b/lib/path.js @@ -1,4 +1,4 @@ -let cx, cy, px, py, sx, sy; +let cx, cy, px, py, sx, sy, prev; cx = cy = px = py = sx = sy = 0; @@ -213,8 +213,9 @@ const parse = function (path) { }; const apply = function (commands, doc) { - // current point, control point, and subpath starting point + // current point, control point, subpath starting point and previous command cx = cy = px = py = sx = sy = 0; + prev = null; // run the commands for (let i = 0; i < commands.length; i++) { @@ -222,6 +223,7 @@ const apply = function (commands, doc) { if (typeof runners[c.command] === 'function') { runners[c.command](doc, c.args); } + prev = c.command.toUpperCase(); } }; @@ -268,7 +270,8 @@ const runners = { }, S(doc, a) { - if (px === null) { + // the control point is only reflected after another cubic curve + if (prev !== 'C' && prev !== 'S') { px = cx; py = cy; } @@ -281,7 +284,7 @@ const runners = { }, s(doc, a) { - if (px === null) { + if (prev !== 'C' && prev !== 'S') { px = cx; py = cy; } @@ -321,7 +324,8 @@ const runners = { }, T(doc, a) { - if (px === null) { + // the control point is only reflected after another quadratic curve + if (prev !== 'Q' && prev !== 'T') { px = cx; py = cy; } else { @@ -403,6 +407,15 @@ const runners = { const solveArc = function (doc, x, y, coords) { const [rx, ry, rot, large, sweep, ex, ey] = coords; + // an arc from and to the same point is a no-op (SVG spec 9.5.1) + if (x === ex && y === ey) { + return; + } + // an arc with a zero radius is a straight line + if (rx === 0 || ry === 0) { + doc.lineTo(ex, ey); + return; + } const segs = arcToSegments(ex, ey, rx, ry, large, sweep, rot, x, y); for (let seg of segs) { @@ -418,9 +431,10 @@ const arcToSegments = function (x, y, rx, ry, large, sweep, rotateX, ox, oy) { const cos_th = Math.cos(th); rx = Math.abs(rx); ry = Math.abs(ry); - px = cos_th * (ox - x) * 0.5 + sin_th * (oy - y) * 0.5; - py = cos_th * (oy - y) * 0.5 - sin_th * (ox - x) * 0.5; - let pl = (px * px) / (rx * rx) + (py * py) / (ry * ry); + // x1' and y1' of the SVG arc notes; px and py are the path's control point + const x1p = cos_th * (ox - x) * 0.5 + sin_th * (oy - y) * 0.5; + const y1p = cos_th * (oy - y) * 0.5 - sin_th * (ox - x) * 0.5; + let pl = (x1p * x1p) / (rx * rx) + (y1p * y1p) / (ry * ry); if (pl > 1) { pl = Math.sqrt(pl); rx *= pl; diff --git a/tests/unit/path.spec.js b/tests/unit/path.spec.js index 3d9e9262..fac43b0a 100644 --- a/tests/unit/path.spec.js +++ b/tests/unit/path.spec.js @@ -30,6 +30,51 @@ describe('SVG Path', () => { }); }); + describe('smooth curves', () => { + test.each([ + ['M0 0 L20 0 S40 20 60 0', '20 0 40 20 60 0 c'], + ['M0 0 C0 20 20 20 20 0 S40 20 60 0', '20 -20 40 20 60 0 c'], + // after an arc or a quadratic curve, the first control point is the current point + ['M0 0 A10 10 0 0 1 20 0 S40 20 60 0', '20 0 40 20 60 0 c'], + ['M0 0 Q10 20 20 0 S40 20 60 0', '20 0 40 20 60 0 c'], + ['M0 0 Q10 20 20 0 s20 20 40 0', '20 0 40 20 60 0 c'], + // after a cubic curve, the control point of T is the current point + ['M0 0 C0 20 20 20 20 0 T60 0', '20 0 33.333333 0 60 0 c'], + ])('draws %s with the right control points', (path, curve) => { + const docData = logData(document); + + document.path(path).stroke(); + document.end(); + + expect(docData.join('')).toContain(curve); + }); + }); + + describe('arcs with a zero radius', () => { + test.each(['M0 0 A0 10 0 0 1 20 20', 'M0 0 a10 0 0 0 1 20 20'])( + 'draws %s as a straight line', + (path) => { + const docData = logData(document); + + document.path(path).stroke(); + document.end(); + + expect(docData.join('')).toContain('20 20 l'); + }, + ); + + test('omits arc with coincident start and end points', () => { + const docData = logData(document); + + // A0 5 0 0 1 10 10 starts and ends at (10, 10) - must be omitted per SVG spec + document.path('M10 10 A0 5 0 0 1 10 10').stroke(); + document.end(); + + // No lineTo should have been emitted for the degenerate arc + expect(docData.join('')).not.toContain('10 10 l'); + }); + }); + describe('arc command flag parsing', () => { // Arc command flags (large-arc-flag and sweep-flag) can be chained without separator // This is valid per SVG spec and commonly produced by SVGO (SVG optimizer)