fix(ocr): a scanned table page with an unmapped text layer loses its table to block mode
Description
Automatic OCR routing gives Tesseract block mode (PSM 6) to every page whose text layer has no usable character map (#1896). The page list comes from the fabricated-character-map detector, which looks only at the text layer. A scanned page often carries such a layer: an invisible OCR sidecar drawn in a Type 3 font with no /ToUnicode. That page is a scan, but it is routed like a page of unmapped vector text.
On a scanned table, block mode breaks the table reconstruction. The scan's own mode (the whole-image mode, PSM 11) reconstructs the table and keeps each value on its row. Block mode returns a few fragments instead, so most row labels and their values do not come out together. #1896 already notes that block mode is worse on ordinary scans; the page selection does not exclude them.
Pages of unmapped vector text still gain from block mode, as #1896 intends. The regression shipped in 1.3.0.
Steps to reproduce
- Make a one-page Letter PDF with one RGB raster of a table. Paint it inset, about a third of the page. Give the table a dark title bar, a coloured header band, light alternate rows and a dark row with white text every fifth row. Use neutral labels (
Item 1..Item 14,Column A..Column F) and invented amounts. - Draw an invisible text layer (
3 Tr) over the raster in a Type 3 font with no/ToUnicodeand non-standard glyph names, so the page is listed infabricated_text_pages. - Run
xberg extract page.pdf --format json --extract-pages true --ocr true --ocr-backend tesseract(or add--ocr-scanned-pages). - Count the values that come out on the line of their row label. Before #1896 all 84 do, and the page has one 15-row table. After it, 6 do and the table breaks into fragments. Pinning
tesseract_config.psmto 11 restores all 84; pinning it to 6 on the older build gives the same 6.
Relevant files and configuration
crates/xberg/src/extractors/pdf/mod.rs: the automatic routes handfabricated_text_pagesto OCR as the block-mode page list.crates/xberg/src/extractors/pdf/ocr/pipeline.rs: the mixed route and the whole-document route turn a listed page into PSM 6, and block mode takes precedence over the whole-image mode for a scan.crates/xberg/src/pdf/scan_detect.rs: the raster coverage that marks a page as a scan.- Default configuration with OCR on, Tesseract backend, no explicit
psm.
How it was found
Measuring a real-world document (not shareable) whose scanned table pages carry an unreadable Type 3 text layer. One scanned table page lost most of its values after #1896 on both --ocr-scanned-pages and plain OCR, while its unmapped vector-text pages gained.
Fix
Give block mode only to a listed page that carries no scan raster. A page whose images cover at least a quarter of it, the least a scan covers in scan detection, keeps the mode a scan gets. Do not change the page list itself: it also drives OCR routing.
Source: xberg-io/xberg