Skip to content

PDF reader: right-to-left brackets, presentation forms and text drawn forward - #1129

Merged
KnifeLemon merged 1 commit into
mainfrom
fix/pdf-rtl-reading
Nov 4, 2026
Merged

KnifeLemon merged 1 commit into
mainfrom
fix/pdf-rtl-reading

Conversation

@KnifeLemon

Copy link
Copy Markdown
Owner

Refs #979

All PDF text routes (TXT, Markdown, DOCX, HWPX, slides, tables) read through PdfDocumentReader, so these fixes apply to all of them.

Bugs fixed

  • Brackets around numbers. (النسخة 3) came out as (النسخة )3. Punctuation at the edges of a number or Latin run belongs to the text around the run, so it is drawn on the far side. It now goes back to the side it is read on: )1( → (1), .2024 → 2024., also over several Latin words. A sign that belongs to a number stays with it (50%). Two numbers no longer form a run, so 10, 20 keeps its order. This matches bidi rules N0 and N1.
  • Mirrored brackets. A PDF whose ToUnicode entries name the glyph drawn reads )text(. Brackets that only pair up the other way round are turned around.
  • Presentation forms. Arabic and Hebrew positional forms and ligatures (ﻻ, ﺔ) become letters.
  • Text drawn left to right in reading order. An engine without right-to-left layout (rhwp) draws البطولة from left to right, and reading it from the right gave ةلوطبلا. The words now tell: on a page where more words start with a letter that only ends words (ة, ى, Hebrew final forms) or are common words backwards than fit the normal reading, the page is read from the left. Pages drawn visually keep their reading. A short page with no such evidence stays as it was, for example the single word ریال.
  • Tabbed right-to-left rows. They kept their words in left-to-right drawing order. Their parts now read from the right.

Not reproduced

The issue's own PDF isn't attached, and its screenshot shows a visually correct PDF. PDFs that I could produce and read here were Chrome (Skia) and LibreOffice 26.2.6. Both already read in the right letter order on main; only the brackets were wrong. So I can't say this fixes the reporter's reversed letters, which is why this is Refs. The reader will flip a page whose words come out backwards, whatever made them so.

The route to PowerPoint → PDF is in a separate PR.

How this was checked

  • ComplexScriptTests: new tests with real Arabic, Persian and Hebrew strings, written glyph by glyph as the shaping engines leave them. 11 of them fail on main. Also PDF → DOCX.
  • Real PDFs: Chrome and LibreOffice output read back with the CLI, including mixed Arabic, Latin and digits.

No new dependency. .NET has no public bidi API and SkiaSharp/HarfBuzz only shape, so this uses the reader's existing word and cluster ordering.

🤖 Generated with Claude Code

… forward

Reading Arabic, Persian and Hebrew PDFs into TXT, Markdown, DOCX and the other
text routes, which all share PdfDocumentReader:

- Punctuation at the edges of a number or a Latin run in right-to-left text goes
  to the side the text around it puts it on: "(3)" no longer comes out as ")3".
  Numbers alone no longer form a run ("10, 20" stays in its order), and a bracket
  drawn mirrored ends a Latin run.
- Brackets that only pair up the other way round are turned around, for PDFs
  whose ToUnicode entries name the glyph drawn rather than the character.
- Arabic and Hebrew presentation forms become letters, ligatures included.
- A page that draws such text left to right in reading order (no right-to-left
  layout, like rhwp) is no longer read backwards: words that start with a letter
  that only ends words, and common words backwards, tell it apart.
- The parts of a tabbed right-to-left row read from the right.

Refs #979

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@KnifeLemon
KnifeLemon merged commit 5bdaf04 into main Nov 4, 2026
10 checks passed
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.

1 participant