Skip to content

Fix smooth curve control points and zero-radius arcs in SVG paths - #1868

Merged
blikblum merged 3 commits into
foliojs:masterfrom
SwastikTripathi:fix/svg-path-smooth-control-points
Nov 20, 2026
Merged

blikblum merged 3 commits into
foliojs:masterfrom
SwastikTripathi:fix/svg-path-smooth-control-points

Conversation

@SwastikTripathi

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Bug fix in the SVG path parser (doc.path()), three cases where the drawn curve differed from the SVG spec:

doc.path('M0 0 A10 10 0 0 1 20 0 S40 20 60 0')  // S started from control point (50, 0), expected (20, 0)
doc.path('M0 0 Q10 20 20 0 S40 20 60 0')        // S reflected the Q control point: (30, -20), expected (20, 0)
doc.path('M0 0 C0 20 20 20 20 0 T60 0')         // T reflected the C control point, expected the current point
doc.path('M0 0 A0 10 0 0 1 20 20')              // drew nothing, expected a line to (20, 20)

Causes, in lib/path.js:

  • arcToSegments() assigned to px and py without declaring them, so it overwrote the module's last control point with its own intermediate values. An S or T right after an arc reflected those.
  • S and T decided whether to reflect from px === null, which only lines, moves and closes reset. The spec reflects only after a curve of the same kind (C/S for S, Q/T for T) and uses the current point otherwise. The runners now check the previous command instead, which apply() records.
  • An arc with rx or ry of 0 divided by zero and produced no segments. The spec draws a straight line to the end point, which solveArc() now does. An arc that ends where it starts is still left out, as before and as the spec says.

Checklist:

  • Unit Tests: new smooth curves and arcs with a zero radius cases in tests/unit/path.spec.js. The 6 new cases fail before the change; yarn test passes (661 tests), and eslint and prettier are clean.
  • Documentation N/A
  • Update CHANGELOG.md
  • Ready to be merged

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The zero-radius fallback emits a line for coincident arc endpoints instead of omitting the arc as SVG requires, potentially producing a visible dot with round stroke caps.

1 open issue
What changed in this PR

This PR improves SVG path rendering in doc.path() by correcting smooth-curve control points and drawing zero-radius arcs as straight lines.

Changes:

  • Track the previous command so smooth curves reflect control points only after compatible curve commands.
  • Prevent arc calculations from overwriting curve state and add zero-radius handling.
  • Add regression tests and document the fixes.
File Description
tests/​unit/​path.spec.js Adds cases for smooth-curve transitions and zero-radius arcs.
lib/​path.js Corrects control-point reflection, isolates arc calculations, and handles zero radii.
CHANGELOG.md Records the SVG path fixes under Unreleased.

Review effort: Balanced

Comment thread lib/path.js
Comment on lines +410 to +411
// an arc with a zero radius is a straight line
if (rx === 0 || ry === 0) {
SVG spec section 9.5.1 says a zero-length arc (start == end) is a no-op.
The zero-radius fallback introduced in the smooth-curve fix would emit a
lineTo to the same point instead, which produces a visible dot with round
stroke caps.

Add the coincident endpoint check before the zero-radius check and add a
regression test.
@SwastikTripathi

Copy link
Copy Markdown
Contributor Author

Done in 968dc00: added a coincident endpoint guard before the zero-radius check in solveArc(). When start and end are the same point the arc is now a no-op as SVG 9.5.1 requires, so a zero-radius arc like A0 5 0 0 1 10 10 from (10,10) no longer emits a lineTo. Added a regression test; all 609 unit tests pass.

@blikblum
blikblum merged commit 6b5d17e into foliojs:master Nov 20, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants