BUG: layout mode splits words when the font size travels in Tm (space_tx is not transformed into page space)
Summary
In layout mode, TextStateParams.space_tx is computed in text space while tx and displaced_tx are in page space. When a producer sets Tf 1 and carries the real font size in Tm — what Illustrator, InDesign and Figma emit — space_tx comes out smaller than the page-space geometry by the Tm scale factor, and any small kerning adjustment inside a TJ array is then read as a whole space.
CEO written as [(C) -30.5 (EO)] TJ extracts as C EO.
Cause
TextStateParams.word_tx() returns
(font_size * (width - td_offset) / 1000 + len(word) * Tc + word.count(space) * Tw) * (Tz / 100)with no self.transform applied. displaced_tx, by contrast, goes through displaced_transform(), which does apply it. space_tx is then compared against excess_tx = tx - last_displaced_tx, mixing the two spaces.
For the repro below: space_tx = 0.278 while transform[0] = 11.0, so the page-space space width is really 3.058 pt. The kern is ~0.34 pt — a third of a space — but 0.34 // 0.278 == 1, so a space is inserted. Scaled correctly, 0.34 // 3.058 == 0, and no space appears.
The same mismatch turns ordinary wide layout gaps into hundreds of padding spaces.
Reproduction
Self-contained, no external files:
"""Minimal repro: layout mode inserts a space inside a word when the font size
travels in Tm (Tf 1)."""
from io import BytesIO
import pypdf
from pypdf import PdfReader
def build(size: int = 11) -> bytes:
# TJ [(C) -30.5 (EO)] : a ~0.34 pt kern inside the word "CEO"
stream = (
b"BT /F1 1 Tf " + str(size).encode() + b" 0 0 " + str(size).encode()
+ b" 100 700 Tm [(C) -30.5 (EO)] TJ ET\n"
)
objs = {
1: b"<< /Type /Catalog /Pages 2 0 R >>",
2: b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
3: b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] "
b"/Resources << /Font << /F1 5 0 R >> >> /Contents 4 0 R >>",
4: b"<< /Length " + str(len(stream)).encode() + b" >>\nstream\n" + stream + b"endstream",
5: b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>",
}
out = bytearray(b"%PDF-1.7\n")
offs = {}
for n in sorted(objs):
offs[n] = len(out)
out += f"{n} 0 obj\n".encode() + objs[n] + b"\nendobj\n"
xref = len(out)
out += f"xref\n0 {len(objs)+1}\n".encode() + b"0000000000 65535 f \n"
for n in sorted(objs):
out += f"{offs[n]:010d} 00000 n \n".encode()
out += f"trailer\n<< /Size {len(objs)+1} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n".encode()
return bytes(out)
page = PdfReader(BytesIO(build())).pages[0]
print(f"pypdf {pypdf.__version__}")
print(f" layout mode : {page.extract_text(extraction_mode='layout').strip()!r} (expected 'CEO')")
print(f" plain mode : {page.extract_text().strip()!r}")Output, identical on every version tested:
pypdf 6.10.2
layout mode : 'C EO' (expected 'CEO')
plain mode : 'CEO'
pypdf 6.16.1
layout mode : 'C EO' (expected 'CEO')
plain mode : 'CEO'
pypdf 6.19.0
layout mode : 'C EO' (expected 'CEO')
plain mode : 'CEO'Plain mode gets it right; only layout mode is affected. Not a regression — present at least as far back as 6.10.2 and still in 6.19.0.
Suggested direction
Compare like with like: either scale space_tx into page space (multiplying by the horizontal scale of Tm × CTM, i.e. abs(transform[0]) for the unrotated case) before comparing it with excess_tx, or compute excess_tx in text space.
We have been running the first of those downstream for a while — space_tx * abs(transform[0]) — and it holds up across a corpus of PDFs from 14 different producer engines, including flipped-CTM pages from Skia. Happy to open a PR if that direction looks right to you.
Possibly related
#4067 covers synthetic-space insertion in plain mode (crlf_space_check). This one is the layout-mode path and a different mechanism, but the symptom a user sees — a space appearing inside a word — is the same.
Environment
pypdf 6.10.2, 6.16.1, 6.19.0. Python 3.13, macOS.
Source: py-pdf/pypdf