Fix doc.text() throwing NaN with lineBreak false and underline/strike/link/goTo - #1784
Merged
Merged
Conversation
Member
|
Many thanks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What kind of change does this PR introduce?
Bug fix. Fixes #401.
doc.text(str, x, y, { lineBreak: false, underline: true })throwsError: unsupported number: NaN. The same happens withstrike,linkandgoTo— anything that reaches_fragment's rendered-width calculation.The chain:
_initOptionsonly assignsresult.widthwhenresult.lineBreak !== false(lib/mixins/text.js:421), and_textonly constructs aLineWrapperif (options.width)(lib/mixins/text.js:82). Butoptions.textWidthandoptions.wordCountare written only inside the wrapper (lib/line_wrapper.js:238-239), so withlineBreak: falsethey stayundefinedandevaluates to
NaN. That reacheslineTo()and thenPDFObject.number, whose range guardn > -1e21 && n < 1e21is false forNaN, so it throws (lib/object.js:164-170).The 2015 report describes this as invisible text rather than a crash, because
number()did not throw back then. The title understates what it does today.The fix supplies only the two missing inputs, leaving the wrapper's formula alone.
_line80 lines up already handles the no-wrapper case this way —this.x += this.widthOfString(text, options)(lib/mixins/text.js:449) — as doesboundsOfStringin its non-wrapped branch (lib/mixins/text.js:186). So the library already knows how to measure an unwrapped fragment;_fragmentwas just missing the fallback.The values are local consts and are never assigned back to
options, so continued-text state and_textOptionsinheritance are untouched. I grepped the other readers of that state before changing it —line_wrapper.js:238-239(the writer),line_wrapper.js:356,text.js:178-179(inside a wrapper callback) andtext.js:475(guarded byif (options.width)) — none of them see a different value than before.Verification. Built from
master(c6f57c6) rather than testing the published package, since this area has moved recently.Before the change, four public-API combinations throw and three controls pass:
After it, all seven pass. The four new cases in
tests/unit/text.spec.jsare verified red-green: withlib/mixins/text.jsreverted and the tests kept, all four fail withunsupported number: NaN.Full unit suite: 432 passing on unmodified
master, 436 with this change — the delta is exactly the four new cases, with no existing test changing state.yarn lintis clean.Checklist:
Credit to @ashelley for the report.
Disclosure: I use AI assistance in my work, and I review and verify everything before it goes out. The runs above are my own.