Repository navigation
Write two hex digits for standard font character codes below 0x10 - #1822
Merged
Merged
Conversation
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 #1425, also fixes #606 (and probably #1354, which uses the default Helvetica and a template literal with Windows line endings).
With a standard (AFM) font,
AFMFont#encodeTextwrites each character code withchar.toString(16), so a code below0x10comes out as a single hex digit. In the<...>string the following digits then pair up wrongly. Per PDF 32000-1 §7.3.4.3 a reader takes the digits two at a time, and assumes a0after a trailing odd digit.doc.text('line one\r\nline two'): the first line is written as<6c696e65206f6e65d>. The finaldis read as0xD0, which isÐin WinAnsiEncoding. That is the strayÐreported in Extra "Ð" at the end of a line when using text with carriage returns (\r) #1425 and Strange character "Ð" every time I press "intro" #606.doc.text('a\tb', { lineBreak: false })is written as<61962>, i.e. bytes61 96 20, so thebis lost.doc.text('Tab\there', { lineBreak: false })is read back by pdf.js asTab–†W&P.The fix pads each code to two digits (
padStart(2, '0')), as the embedded font path already does with its 4-digit glyph IDs. Codes0x10and above are unchanged, so normal text produces the same bytes (the visual snapshots still pass).Text extracted with
pdfjs-dist(already a dev dependency),doc.text(s, 50, 50, { lineBreak: false }):'a\tb'a–a b'Tab\there'Tab–†W&PTab here'line\r'lineÐline'x\u0001yz'x —x yzNot part of this PR: the line wrapper still keeps the
\rof a\r\nat the end of the line (now as an invisible0x0Dcode instead ofÐ). Stripping it the way\nis stripped would be a separate change.Checklist:
Tests added:
tests/unit/afm.spec.js:encodeText('a\tb\r\x01')returns['61', '09', '62', '0d', '01']tests/unit/text.spec.js:doc.text('a\tb\rc', { lineBreak: false })keeps the texta\tb\rcin the content streamBoth fail on
masterand pass with the fix. Ranyarn test(62 files, 552 tests, unit and visual),yarn lintandyarn prettier, all passing. Did not runtest:toolsortest:package.