Expand description
Reading-order layout pass for Tagged PDFs (round 29).
Plain raster (content-stream) order does not give logical reading
order for multi-column / multi-block layouts: the painter would
lay column 1’s first row, column 2’s first row, then column 1’s
second row, etc., as it raster-scanned the page from top to bottom.
Tagged PDF (ISO 32000-1 §14.8) factors logical structure out of
visual layout: the catalog’s /StructTreeRoot carries a tree of
/StructElems (sections, paragraphs, list items, table rows…)
whose leaves are MarkedContentReferences (MCIDs) — integers that
cross-reference the page’s /Span <</MCID n>> BDC … EMC-bracketed
content-stream slices. Walking the tree in document order and
resolving each MCID to its painted text run gives us the
author-intended reading order, regardless of where the runs
actually appear on paper.
This module’s read_in_logical_order performs that walk:
- Open the catalog → find
/StructTreeRoot(if absent, return aRaster-tagged result that delegates tocrate::reader::extract_text). - Walk every page in document order, run the round-22 text walker
with MCID tracking enabled (round-29 addition), and bucket each
crate::reader::text::MarkedTextRunby(page_obj_num, mcid). - Recurse the StructTreeRoot’s
/Ktree. For every leaf that’s either a bare integer (MCID into the parent’s/Pgpage) or a<</Type /MCR /Pg n /MCID m>>dict (MCID into the named page), look up the corresponding bucket and emit its runs in accumulation order. For every kid that’s a<</Type /StructElem …>>(or an indirect ref to one), recurse into its/K.
The walker is permissive — unknown /S (structure-type) names are
recursed into anyway (they’re decorative — the spec encourages
user-defined types — and any text under them still belongs in
logical order). /OBJR (object reference) leaves are skipped:
they reference annotations, not content, so they carry no text.
§Provenance
ISO 32000-1:2008 §14.6 (Marked Content), §14.7 (Logical Structure), §14.8 (Tagged PDF). No third-party PDF library was consulted.
Structs§
- Reading
Order Text - Output of
read_in_logical_order: the run sequence plus the flag that tells the caller which path produced them.
Enums§
- Layout
Mode - Which path produced the
ReadingOrderTextruns.
Functions§
- read_
in_ logical_ order - Walk the document’s logical structure tree and emit text runs in
reading order. Falls back to raster order when no
/StructTreeRootis present.