Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle toUnicode cMaps that omit leading zeros in hex encoded UTF-16 (issue 18099) #18390

Merged
merged 1 commit into from
Jul 6, 2024

Conversation

alexcat3
Copy link
Contributor

@alexcat3 alexcat3 commented Jul 4, 2024

Modifies partialEvaluator.readToUnicode() to handle toUnicode cMaps that omit leading zeros in hex encoded UTF-16, as seen in the PDF in [https://github.com//issues/18099](issue 18099).

In the PDF in question, the toUnicode cmap had a line to map the glyph char codes from 00 to 7F to the corresponding unicode code points. The syntax to map a range of char codes to a range of unicode code points is
<start_char_code> <end_char_code> <start_unicode_codepoint>
As the unicode code points are supposed to be given in UTF 16, the PDF's line SHOULD have probably read
<00> <7F> <0000>
Instead it omitted two leading zeros from the UTF 16 like this
<00> <7F> <00>
This confused pdf.js into mapping these character codes to the UTF-16 characters with the corresponding high bytes ( 01 becomes \u0100, 02 becomes \u0200) which ended up turning latin text in the PDF into chinese when it was copied.

I'm not sure if the specification actually allows PDFs to do this, but since there's at least one PDF in the wild that does and other PDF readers read it correctly, pdf.js should probably support this.

@Snuffleupagus Snuffleupagus changed the title Fix Issue 18099 Handle toUnicode cMaps that omit leading zeros in hex encoded UTF-16 (issue 18099) Jul 4, 2024
@timvandermeij timvandermeij linked an issue Jul 4, 2024 that may be closed by this pull request
@calixteman
Copy link
Contributor

Could you add a unit test ? something similar to:

pdf.js/test/unit/api_spec.js

Lines 3202 to 3216 in 790470c

it("gets text content, with no extra spaces (issue 13226)", async function () {
const loadingTask = getDocument(buildGetDocumentParams("issue13226.pdf"));
const pdfDoc = await loadingTask.promise;
const pdfPage = await pdfDoc.getPage(1);
const { items } = await pdfPage.getTextContent({
disableNormalization: true,
});
const text = mergeText(items);
expect(text).toEqual(
"Mitarbeiterinnen und Mitarbeiter arbeiten in über 100 Ländern engagiert im Dienste"
);
await loadingTask.destroy();
});

You can just test that the retrieved text starts with Yýsledková listina přijímacích zkoušek.

@alexcat3
Copy link
Contributor Author

alexcat3 commented Jul 4, 2024

OK, I will.

Copy link
Collaborator

@Snuffleupagus Snuffleupagus left a comment

Choose a reason for hiding this comment

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

When addressing the comments, please remember to squash the commits.

test/pdfs/issue18099_reduced.pdf Outdated Show resolved Hide resolved
test/unit/api_spec.js Show resolved Hide resolved
test/unit/api_spec.js Outdated Show resolved Hide resolved
timvandermeij

This comment was marked as outdated.

@alexcat3
Copy link
Contributor Author

alexcat3 commented Jul 5, 2024

OK, I made the necessary changes and squashed the commits.

@timvandermeij
Copy link
Contributor

The CI failed on linting. You can automatically fix that by running npx gulp lint --fix locally. Other than that this patch LGTM, with passing tests which we'll trigger once the linting issue is fixed, but I'll leave the final sign-off here to @Snuffleupagus given the familiarity with the font code. Thanks!

@calixteman
Copy link
Contributor

/botio test

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Received

Command cmd_test from @calixteman received. Current queue size: 0

Live output at: http://54.241.84.105:8877/c8ea0e506839555/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Windows)


Received

Command cmd_test from @calixteman received. Current queue size: 0

Live output at: http://54.193.163.58:8877/db23d9ada3a63cd/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Failed

Full output at http://54.241.84.105:8877/c8ea0e506839555/output.txt

Total script time: 28.99 mins

  • Unit tests: Passed
  • Integration Tests: Passed
  • Regression tests: FAILED
  different ref/snapshot: 19
  different first/second rendering: 2

Image differences available at: http://54.241.84.105:8877/c8ea0e506839555/reftest-analyzer.html#web=eq.log

Copy link
Collaborator

@Snuffleupagus Snuffleupagus left a comment

Choose a reason for hiding this comment

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

Can you please improve the commit message a bit, such that all relevant information is available on the Git command line as well without having to read GitHub?

Generally the first line of the commit message should contain a summary of the changes, with a reference to the bug/issue, and then any other relevant details below. You included a bunch of nice context in #18390 (comment) that belongs in the commit message too. My suggestion would be something like this:

Handle toUnicode cMaps that omit leading zeros in hex encoded UTF-16 (issue 18099)

In the PDF in question, the toUnicode cmap had a line to map the glyph char codes from 00 to 7F to the corresponding unicode code points. The syntax to map a range of char codes to a range of unicode code points is
<start_char_code> <end_char_code> <start_unicode_codepoint>
As the unicode code points are supposed to be given in UTF 16, the PDF's line SHOULD have probably read
<00> <7F> <0000>
Instead it omitted two leading zeros from the UTF 16 like this
<00> <7F> <00>
This confused PDF.js into mapping these character codes to the UTF-16 characters with the corresponding high bytes ( 01 becomes \u0100, 02 becomes \u0200) which ended up turning latin text in the PDF into chinese when it was copied.

I'm not sure if the specification actually allows PDFs to do this, but since there's at least one PDF in the wild that does and other PDF readers read it correctly, PDF.js should probably support this.

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Windows)


Failed

Full output at http://54.193.163.58:8877/db23d9ada3a63cd/output.txt

Total script time: 42.46 mins

  • Unit tests: Passed
  • Integration Tests: Passed
  • Regression tests: FAILED
  different ref/snapshot: 3

Image differences available at: http://54.193.163.58:8877/db23d9ada3a63cd/reftest-analyzer.html#web=eq.log

…(issue 18099)

Add unit test to check compatability with such cmaps

In the PDF in issue 18099. the toUnicode cmap had a line to map the glyph char codes from 00 to 7F to the corresponding code points. The syntax to map a range of char codes to a range of unicode code points is
<start_char_code> <end_char_code> <start_unicode_codepoint>
As the unicode code points are supposed to be given in UTF-16 BE, the PDF's line SHOULD have probably read
<00> <7F> <0000>
Instead it omitted two leading zeros from the UTF-16 like this
<00> <7F> <00>
This confused PDF.js into mapping these character codes to the UTF-16 characters with the corresponding HIGH bytes (01 became \u0100, 02 became \u0200, et cetera), which ended up turning latin text in the PDF into chinese when it was copied
I'm not sure if the PDF spec actually allows PDFs to do this, but since there's at least one PDF in the wild that does and other PDF readers read it correctly, PDF.js should probably support this
@alexcat3
Copy link
Contributor Author

alexcat3 commented Jul 6, 2024

@Snuffleupagus I changed the commit message

Copy link
Collaborator

@Snuffleupagus Snuffleupagus left a comment

Choose a reason for hiding this comment

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

r=me, thank you!

@Snuffleupagus Snuffleupagus merged commit 5ee6169 into mozilla:master Jul 6, 2024
9 checks passed
@alexcat3
Copy link
Contributor Author

alexcat3 commented Jul 6, 2024

Thank YOU for all your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CJK characters in clipboard after copy of latin text
5 participants