Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 22 additions & 8 deletions lib/path.js
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -213,15 +213,17 @@ 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++) {
const c = commands[i];
if (typeof runners[c.command] === 'function') {
runners[c.command](doc, c.args);
}
prev = c.command.toUpperCase();
}
};

Expand Down Expand Up @@ -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;
}
Expand All @@ -281,7 +284,7 @@ const runners = {
},

s(doc, a) {
if (px === null) {
if (prev !== 'C' && prev !== 'S') {
px = cx;
py = cy;
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
45 changes: 45 additions & 0 deletions tests/unit/path.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading