Skip to content

Allow zero dash lengths when not every length is zero - #1823

Open
ybai08 wants to merge 1 commit into
foliojs:masterfrom
ybai08:fix-dash-zero-length
Open

ybai08 wants to merge 1 commit into
foliojs:masterfrom
ybai08:fix-dash-zero-length

Conversation

@ybai08

@ybai08 ybai08 commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Bug fix. Fixes #1060.

dash() throws for any length of 0, so the usual way to draw a dotted line, doc.lineCap('round').dash(0, { space: 2 }) (dash array [0 2]), fails with lengths must be numeric and greater than zero. PDF 32000-1 §8.4.3.6 only requires the dash array's numbers to be non-negative and not all zero. The check was added in #969 for #949, where dash(0) wrote [0 0] 0 d, an all-zero array that Edge and Adobe Reader refused. As discussed in #1060, the check went further than that case needed.

This change:

  • accepts lengths that are >= 0 as long as at least one is greater than zero, so dash(0, { space: 2 }) writes [0 2] 0 d and dash([2, 0, 3]) is accepted;
  • keeps throwing for dash(0) / dash([0, 0]) (the Use of dash(0) emits PDF that some readers interpret as corrupt #949 case), negative numbers, null and other non-finite values;
  • keeps accepting an empty array as before (dash([]) writes [] 0 d, which is a solid line);
  • updates the error message to lengths must be numeric and non-negative, and not all zero, and updates docs/vector.md.

Checklist:

  • Unit Tests
  • Documentation
  • Update CHANGELOG.md
  • Ready to be merged

Tests in tests/unit/vector.spec.js:

  • new: dash(0, { space: 2 }) writes [0 2] 0 d
  • dash([2, 0, 3]) changed from "throws" to "does not throw"
  • new: dash([0, 0]) throws
  • existing 0, -1 and null cases still throw, with the updated message

On master the new and changed tests fail (6 failed, 11 passed in vector.spec.js, including the message updates). With the fix, all 17 pass. Ran yarn test (62 files, 552 tests, unit and visual), yarn lint and yarn prettier, all passing. The output was not opened in Adobe Reader or Edge; the [0 2] content stream was only checked as text against the spec.

This branch has not been deployed

No deployments
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.

Error when trying to create a dotted line

1 participant