Skip to content

Keep the original text in PDF glyph clusters - #2644

Closed
yucheng-du wants to merge 1 commit into
Automattic:masterfrom
yucheng-du:fix/pdf-text-clusters
Closed

yucheng-du wants to merge 1 commit into
Automattic:masterfrom
yucheng-du:fix/pdf-text-clusters

Conversation

@yucheng-du

Copy link
Copy Markdown

Copying कि out of a v4 PDF gave �क in Poppler and PDFium. The glyphs are drawn in visual order, and Cairo has no way to recover the original letters from their IDs alone.

Pass the source text and HarfBuzz cluster boundaries to Cairo for forward runs. Keep a cluster together if it crosses the 1,024-glyph buffer boundary. Backwards runs still use the existing path because changing their drawing order broke Arabic and Hebrew selection in PDF readers. Color fonts also keep their existing path so bitmap emoji still render.

Both new regression tests fail on master. All 310 unit tests pass with the change, as do lint and the type tests. I also tried 23 strings in Devanagari, Bengali, Tamil and Thai. Poppler and MuPDF recovered all 23 exactly, up from two. PDFium recovered 22. All 40 sample PDFs rendered pixel-for-pixel the same as master.

Refs #2611.

  • A coding agent was used to make some of these changes (GPT-6.2 Astra, ultra effort).

@chearon

chearon commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Thanks but I'm already working on this, and want to do it differently. In particular, we don't need to go through a JS string to get a UTF-8 value. It can just be UTF-8 from the beginning, but only when the surface requires it.

@yucheng-du

Copy link
Copy Markdown
Author

Thanks for the heads-up. I'll close this draft. Two cases from testing may help with your implementation. Passing a whole Hebrew run with Cairo's backwards-cluster flag reversed the copied letters in PDFium and macOS PDFKit. Sending a bitmap color emoji through the text-glyph path also removed its image from the PDF. The two Arimo tests in this branch are independent of how the UTF-8 text is obtained.

@yucheng-du yucheng-du closed this Dec 1, 2026
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.

2 participants