Skip to content

BUG: Keep the text shown before a change of direction - #4155

Merged
stefan6419846 merged 2 commits into
py-pdf:mainfrom
RavSinghChandan:rtl-switch-keeps-text
Oct 6, 2026
Merged

stefan6419846 merged 2 commits into
py-pdf:mainfrom
RavSinghChandan:rtl-switch-keeps-text

Conversation

@RavSinghChandan

Copy link
Copy Markdown
Contributor

Closes #4142.

Since 6.19.0, extract_text() drops an Arabic-Indic or Persian number when
it ends a right-to-left line, e.g. "بتاريخ ٢٠٢٦" comes back as "بتاريخ".

The cause is in get_display_str. When the direction switches, the text
collected so far is handed to the visitor and then reset, so it never
reaches the output. #4077 made these digits neutral, so they now collect
before the first Arabic letter of the line and are dropped at the switch.
The same path also dropped Latin text shown before Arabic text in one
operator.

get_display_str now also returns the runs completed by a change of
direction, and the extractor adds them to the output. The visitor still
receives each run exactly once.

This does not reorder runs: the number comes out before the word, in
display order. Logical ordering is #1629, which #4077 also left out.

The new test covers Arabic-Indic and Persian digits before Arabic text,
and Latin text before and after it. The cases fail before the change and
pass after it, and the full suite passes locally.

Used Claude Code (Claude Opus 5.5) to help find and patch this and to draft this description; I reviewed and tested the change.

@codecov

codecov Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.12%. Comparing base (2509a05) to head (36be90c).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4155   +/-   ##
=======================================
  Coverage   98.12%   98.12%           
=======================================
  Files          59       59           
  Lines       11572    11579    +7     
  Branches     2161     2162    +1     
=======================================
+ Hits        11355    11362    +7     
  Misses        123      123           
  Partials       94       94           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread pypdf/_text_extraction/__init__.py Resolved
Comment thread pypdf/_text_extraction/__init__.py Resolved
Comment thread tests/test_text_extraction.py Resolved
@RavSinghChandan

Copy link
Copy Markdown
Contributor Author

Done, it's now completed_text: list[str] with appends, joined once on return so the caller still gets a string.

@RavSinghChandan

Copy link
Copy Markdown
Contributor Author

Done, I used your wording.

@RavSinghChandan

Copy link
Copy Markdown
Contributor Author

Yes, that's only how GitHub renders it. The line is "١٢٣٤ ابحرم", "١٢٣٤ مرحبا" with a plain ", " between the two strings and no direction marks; the browser's bidi algorithm moves the comma when RTL text surrounds it. Running repr() on the line shows the order as written.

@stefan6419846
stefan6419846 merged commit 11458e7 into py-pdf:main Oct 6, 2026
21 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.

extract_text drops Arabic-Indic/Persian digits ending the first right-to-left line (regression in 6.19.0)

2 participants