Skip to content

STY: Simplify warnings & debugging in layout mode text extraction#3271

Merged
stefan6419846 merged 3 commits intopy-pdf:mainfrom
hackowitz-af:simplify-rotated-text-warnings-in-layout-mode
May 6, 2025
Merged

STY: Simplify warnings & debugging in layout mode text extraction#3271
stefan6419846 merged 3 commits intopy-pdf:mainfrom
hackowitz-af:simplify-rotated-text-warnings-in-layout-mode

Conversation

@hackowitz-af
Copy link
Contributor

@hackowitz-af hackowitz-af commented Apr 30, 2025

The existing code for warning if there is rotated text or uninterpretable fonts is slightly overcomplicated. It follows two nested loops to decide if there is text to warn, and needs local variables to flag if a warning is already emitted. It also comingles debug-only logic with the operational code by re-checking for each group of operators if they will be debugged later.

This PR clarifies both by waiting until all operators are collected before searching them for warn-able data, or deciding if to debug them. This removes the need for several variables, nested loops, and several conditional expressions within the loop.

@codecov
Copy link

codecov bot commented Apr 30, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.61%. Comparing base (d9ba201) to head (03def72).
⚠️ Report is 78 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3271      +/-   ##
==========================================
- Coverage   96.62%   96.61%   -0.01%     
==========================================
  Files          53       53              
  Lines        8966     8963       -3     
  Branches     1661     1661              
==========================================
- Hits         8663     8660       -3     
  Misses        181      181              
  Partials      122      122              

☔ View full report in Codecov by Sentry.
📢 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.

Copy link
Collaborator

@stefan6419846 stefan6419846 left a comment

Choose a reason for hiding this comment

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

Thanks.

@stefan6419846 stefan6419846 merged commit c42e3d8 into py-pdf:main May 6, 2025
12 of 13 checks passed
stefan6419846 added a commit that referenced this pull request May 11, 2025
## What's new

### New Features (ENH)
- Add support for IndirectObject.__iter__ (#3228) by @bryan-brancotte
- Allow filtering by font when removing text (#3216) by @samuelbradshaw

### Bug Fixes (BUG)
- Add missing named destinations being ByteStringObjects (#3282) by @stefan6419846
- Get font information more reliably when removing text (#3252) by @samuelbradshaw
- T* 2D Translation consistent with PDF 1.7 Spec (#3250) by @hackowitz-af
- Add font stack to q/Q operations in layout mode (#3225) by @hackowitz-af
- Avoid completely hiding image loading issues like exceeding image size limits (#3221) by @stefan6419846
- Using compress_identical_objects on transformed content duplicates differing content (#3197) by @danio
- Consider BlackIs1 parameter for CCITTFaxDecode filter (#3196) by @stefan6419846

### Robustness (ROB)
- Deal with insufficient cm matrix during text extraction (#3283) by @stefan6419846
- Allow merging when annotations miss D entry (#3281) by @stefan6419846
- Fix merging documents if there are no Dests (#3280) by @stefan6419846
- Fix crash on malformed action in outline (#3278) by @larsga
- Fix compression issues for removed images which might be None (#3246) by @stefan6419846
- Attempt to deal with non-rectangular FlateDecode streams (#3245) by @stefan6419846
- Handle some None values for broken PDF files (#3230) by @stefan6419846

### Developer Experience (DEV)
- Multiple style improvements by @j-t-1
- Update ruff to 0.11.0 by @stefan6419846

### Maintenance (MAINT)
- Conform ASCIIHexDecode implementation to specification (#3274) by @j-t-1
- Modify comments of filters that do not use decode_parms (#3260) by @j-t-1

### Code Style (STY)
- Simplify warnings & debugging in layout mode text extraction (#3271) by @hackowitz-af
- Standardize mypy assert statements (#3276) by @j-t-1

[Full Changelog](5.4.0...5.5.0)
@shartzog
Copy link
Contributor

shartzog commented May 20, 2025

Nice work! Much cleaner...

My thought process at the time was to save memory in the 'non-debug' use case by letting tjs and all its member TextStateParams instances fall out of scope after every iteration of the loop (since no references to them persist between iterations if they're not added to tj_debug). I never actually did any profiling to confirm this intuition, and judging by the debug output file sizes for tjs.json vs bts.json on a typical text heavy page (~1MB and ~50KB respectively), the improved readability trumps the minor theoretical reduction. I.e. a temporary ~1MB memory increase released after every page extraction isn't going to break the bank unless something else is already broken (a la #3082 which could still use a follow up to raise an exception and/or otherwise prevent the ballooning spaces issue at a lower level).

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.

3 participants