Skip to content

ROB: Keep the text state when a text state operator has no operand in layout mode - #4232

Merged
stefan6419846 merged 1 commit into
py-pdf:mainfrom
RavSinghChandan:layout-text-state-no-operands
Oct 27, 2026
Merged

stefan6419846 merged 1 commit into
py-pdf:mainfrom
RavSinghChandan:layout-text-state-no-operands

Conversation

@RavSinghChandan

@RavSinghChandan RavSinghChandan commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

In layout mode, a Tc, Tw, Tz, TL or Ts operator without any operand made extract_text() raise IndexError, because set_state_param() read value[0] from an empty list. Plain mode already ignores these.

A missing operand is now handled like a non-numeric one (#4214): _as_float() gets None and the parameter keeps its current value, so the text around it is still extracted. set_font_operation() from #4228 now uses _as_float() for the size too, so there is a single float conversion in layout mode.

@RavSinghChandan RavSinghChandan changed the title ROB: Keep the text state when a text state operator has no operand in… ROB: Keep the text state when a text state operator has no operand in layout mode Oct 20, 2026
@codecov

codecov Bot commented Oct 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.19%. Comparing base (cd3e646) to head (a23c36b).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4232      +/-   ##
==========================================
- Coverage   98.19%   98.19%   -0.01%     
==========================================
  Files          59       59              
  Lines       11850    11847       -3     
  Branches     2228     2228              
==========================================
- Hits        11636    11633       -3     
  Misses        118      118              
  Partials       96       96              

☔ 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.

@RavSinghChandan

Copy link
Copy Markdown
Contributor Author

I tried it, but it is not quite equivalent: for /F1 Tf (a font name without a size, the case from #4228), operands is not empty, so operands[1] raises IndexError. test_layout_mode_tf_with_a_missing_size_keeps_the_previous_size fails with the suggestion. That is why it checks len(operands) > 1. I am happy to write it differently if you prefer, for example with a small helper.

@stefan6419846
stefan6419846 merged commit 945216f into py-pdf:main Oct 27, 2026
21 of 23 checks passed
stefan6419846 pushed a commit that referenced this pull request Oct 31, 2026
* ROB: Tolerate extra operands before Tm/cm in layout mode

A matrix operator consumes the 6 operands nearest to it; values
left on the operand stack before those (malformed content streams)
overflowed TextStateManager.new_transform() and raised TypeError
in layout mode text extraction.

Complete the matrix with only the last 6 operands in
_complete_matrix and add_cm, mirroring the tolerance already in
place for missing/non-numeric TD and Tz operands (#4232, #4233).

Fixes #4283

Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>

* Add layout-mode tests for extra operands before Tm/cm

Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>

---------

Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>
stefan6419846 added a commit that referenced this pull request Oct 31, 2026
* ROB: Tolerate extra operands before Tm/cm in layout mode

A matrix operator consumes the 6 operands nearest to it; values
left on the operand stack before those (malformed content streams)
overflowed TextStateManager.new_transform() and raised TypeError
in layout mode text extraction.

Complete the matrix with only the last 6 operands in
_complete_matrix and add_cm, mirroring the tolerance already in
place for missing/non-numeric TD and Tz operands (#4232, #4233).

Fixes #4283

Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>

* Fix set_page_label TypeError when start=None

The start parameter is annotated Optional[int] and its validation
accepts None as "not set", but _set_page_label used ,
which treats None as a value to write and crashes in NumberObject.

Only write /St when a concrete non-zero start was provided.

Fixes #4285

Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>

* Add tests for set_page_label with start=None

Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>

* Update tests/test_writer.py

* Update tests/test_writer.py

---------

Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>
Co-authored-by: Stefan <96178532+stefan6419846@users.noreply.github.com>
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