Skip to main content

docling_pdf/
lib.rs

1//! PDF backend for docling.rs.
2//!
3//! A port of docling's standard PDF pipeline: pdfium extracts the text layer
4//! (cells with bounding boxes) and renders page images; a discriminative ONNX
5//! stack (layout detection, table structure, OCR) classifies regions; the cells
6//! are assembled in reading order into a [`DoclingDocument`].
7//!
8//! Current stages: pdfium text-cell extraction + page rendering ([`pdfium_backend`])
9//! and the deterministic text/reading-order assembly ([`assemble`]). The layout,
10//! table-structure and OCR ONNX stages land behind [`Pipeline`] next.
11
12// Without `ml` only the text-layer path runs; the shared assembly/label
13// helpers it doesn't exercise stay compiled for API stability (the full
14// build still flags genuinely dead code).
15#![cfg_attr(not(feature = "ml"), allow(dead_code))]
16
17// Reading-order assembly. Public under `ocr-prep` so the browser pipeline can
18// reuse the geometric table reconstruction and its reliability gate (#157).
19#[cfg(feature = "ocr-prep")]
20pub mod assemble;
21#[cfg(not(feature = "ocr-prep"))]
22mod assemble;
23// Vector checkbox squares from the text parser's path walk (#609).
24pub mod checkbox;
25mod dp_lines;
26#[cfg(feature = "ml")]
27pub mod enrich;
28// Public so sibling crates (e.g. docling-rag's ONNX embedder) can route their
29// own `ort` sessions through the same `DOCLING_RS_EP` selection. Kept as a
30// re-export after the logic moved to the shared `docling-onnx` crate —
31// `docling_pdf::ep::…` remains the stable path downstream crates code against.
32#[cfg(feature = "ml")]
33pub use docling_onnx as ep;
34// Heading-hierarchy stage (#302): PDF font-name style parsing, outline
35// extraction (pure lopdf), and the level-assignment pass. No feature gate on
36// the logic itself — only the glyph style pass needs pdfium (`ml`).
37#[cfg(feature = "ml")]
38pub mod dparse_render;
39mod font_style;
40mod heading_hierarchy;
41pub mod layout;
42#[cfg(feature = "ml")]
43mod mets;
44#[cfg(feature = "ml")]
45mod ocr;
46#[cfg(any(feature = "ml", feature = "ocr-prep"))]
47pub mod ocr_det;
48#[cfg(feature = "ocr-prep")]
49pub mod ocr_prep;
50#[cfg(feature = "ml")]
51mod orient;
52pub mod outline;
53pub mod pdfium_backend;
54#[cfg(feature = "ml")]
55pub mod picture;
56#[cfg(feature = "ml")]
57pub mod quality;
58mod reading_order;
59// Pure-Rust region resampling (page→1024px box-average, crop→448 bilinear) —
60// available to the browser TableFormer path (#157 stage 3), not just `ml`.
61#[cfg(feature = "ocr-prep")]
62pub mod resample;
63#[cfg(feature = "ocr-prep")]
64pub mod scanned;
65// Built-in standard-14 font metrics for the pure-Rust text parser (#187) —
66// no feature gate: the wasm/pdf-text path needs them like the native one.
67pub mod pdf_meta;
68#[cfg(feature = "ml")]
69pub mod raster;
70#[cfg(feature = "ml")]
71pub mod render;
72mod std14;
73#[cfg(feature = "ml")]
74pub mod tableformer;
75#[cfg(feature = "ml")]
76mod tesseract;
77pub mod textparse;
78#[cfg(feature = "ocr-prep")]
79pub mod tf_core;
80// docling's TableFormer cell matcher — pure Rust, shared with the browser
81// TableFormer path (#157 stage 3).
82#[cfg(feature = "ocr-prep")]
83pub mod tf_match;
84pub mod timing;
85
86#[cfg(feature = "ml")]
87use std::collections::BTreeMap;
88use std::fmt;
89#[cfg(feature = "ml")]
90use std::sync::mpsc::{sync_channel, Receiver};
91#[cfg(feature = "ml")]
92use std::sync::{Arc, Mutex};
93
94// An execution provider only exists on its OS, and ort's prebuilt ONNX
95// Runtime binaries follow suit — requesting an impossible pairing otherwise
96// surfaces as a cryptic ort-sys linker error ("no builds available that
97// satisfy the requested feature set"). Catch it at type-check time with an
98// actionable message instead.
99#[cfg(all(feature = "coreml", not(target_vendor = "apple")))]
100compile_error!(
101    "the `coreml` execution provider exists only on Apple targets (macOS/iOS). \
102     On Linux use `--features cuda` or `--features tensorrt` (NVIDIA), on \
103     Windows also `--features directml`, or build without EP features for CPU."
104);
105#[cfg(all(feature = "directml", not(target_os = "windows")))]
106compile_error!(
107    "the `directml` execution provider exists only on Windows. On Linux use \
108     `--features cuda` or `--features tensorrt` (NVIDIA), on macOS \
109     `--features coreml`, or build without EP features for CPU."
110);
111#[cfg(all(any(feature = "cuda", feature = "tensorrt"), target_vendor = "apple"))]
112compile_error!(
113    "the `cuda`/`tensorrt` execution providers have no Apple builds (no NVIDIA \
114     support on macOS). Use `--features coreml` there, or build without EP \
115     features for CPU."
116);
117
118use docling_core::DoclingDocument;
119pub use docling_core::EncryptionError;
120// The env-knob helpers only gate ML-pipeline diagnostics and tuning; the
121// pure text-layer (wasm) build has no call sites.
122#[cfg(feature = "ml")]
123use docling_core::Node;
124#[cfg(feature = "ml")]
125use docling_core::{debug_log, env};
126
127pub use heading_hierarchy::HeadingHierarchyOptions;
128#[cfg(feature = "ml")]
129pub use mets::{convert_mets_gbs, convert_mets_gbs_with_options, convert_mets_gbs_with_pipeline};
130#[cfg(feature = "ml")]
131pub use ocr::{OcrEngine, OcrLang, OcrMode};
132#[cfg(feature = "ml")]
133pub use pdfium_backend::PdfDocument;
134pub use pdfium_backend::{PdfPage, TextCell};
135#[cfg(feature = "ml")]
136pub use tesseract::{lang_arg as tesseract_lang_arg, TesseractOptions};
137// Plain page rasterization (#243) — pdfium only, no models.
138#[cfg(feature = "ml")]
139pub use pdfium_backend::{render_pages, RenderedPage};
140
141/// Errors from the PDF backend. Detailed and surfaced (never silently skipped).
142#[derive(Debug)]
143pub enum PdfError {
144    /// pdfium failed to bind, open, or read the document (and, historically,
145    /// any pipeline error outside the models).
146    Pdfium(String),
147    /// The document itself: an object model lopdf cannot read, a page no
148    /// renderer can draw, a page range outside it.
149    Document(String),
150    /// The layout ONNX model failed to load or run.
151    Layout(String),
152    /// The OCR ONNX model failed to load or run.
153    Ocr(String),
154    /// The document budget ([`Pipeline::document_timeout`]) ran out between
155    /// two pages. Internally the sentinel that stops the page walk; a caller
156    /// never sees it from [`Pipeline::convert`] — the conversion returns the
157    /// pages it finished and reports the cut through [`Completion`].
158    Timeout(String),
159    /// The document is encrypted and the password given (or none) does not
160    /// open it (#636) — the typed value a caller prompts for a password on,
161    /// reachable through [`std::error::Error::source`] too. Its text is the
162    /// one docling raises.
163    Encrypted(EncryptionError),
164}
165
166impl fmt::Display for PdfError {
167    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
168        match self {
169            PdfError::Pdfium(m) => write!(f, "pdf: pdfium error: {m}"),
170            PdfError::Document(m) => write!(f, "pdf: {m}"),
171            PdfError::Layout(m) => write!(f, "pdf: {m}"),
172            PdfError::Ocr(m) => write!(f, "pdf: {m}"),
173            PdfError::Timeout(m) => write!(f, "pdf: {m}"),
174            PdfError::Encrypted(EncryptionError::WrongPassword) => {
175                f.write_str("pdf: the PDF is encrypted and the password is wrong")
176            }
177            PdfError::Encrypted(EncryptionError::NeedPassword) => {
178                f.write_str("pdf: the PDF is encrypted: a password is required")
179            }
180            PdfError::Encrypted(e) => write!(f, "pdf: the PDF is encrypted: {e}"),
181        }
182    }
183}
184
185/// How a conversion ended: every selected page, or the pages that fit the
186/// document budget (docling's `document_timeout`, `PARTIAL_SUCCESS`).
187#[derive(Debug, Clone, Copy, PartialEq, Eq)]
188pub enum Completion {
189    /// Every selected page was processed.
190    Complete,
191    /// The budget ran out: `pages_done` of `pages_selected` pages are in the
192    /// document, the rest were never rendered (or never processed).
193    TimedOut {
194        pages_done: usize,
195        pages_selected: usize,
196        budget: std::time::Duration,
197    },
198}
199
200impl Completion {
201    pub fn timed_out(&self) -> bool {
202        matches!(self, Completion::TimedOut { .. })
203    }
204
205    /// docling's `ErrorItem.error_message` for the cut, `None` when complete.
206    pub fn message(&self) -> Option<String> {
207        match self {
208            Completion::Complete => None,
209            Completion::TimedOut {
210                pages_done,
211                pages_selected,
212                budget,
213            } => Some(format!(
214                "document timeout of {:.3}s exceeded after {pages_done} of {pages_selected} \
215                 pages; the output holds the pages processed",
216                budget.as_secs_f64()
217            )),
218        }
219    }
220}
221
222/// A converted document and how its conversion ended ([`Completion`]).
223#[derive(Debug, Clone)]
224pub struct Converted {
225    pub document: DoclingDocument,
226    pub completion: Completion,
227}
228
229/// Has the document budget run out?
230fn expired(deadline: Option<std::time::Instant>) -> bool {
231    deadline.is_some_and(|d| std::time::Instant::now() >= d)
232}
233
234/// The sentinel the page walks stop on once the budget is gone.
235fn timeout_sentinel() -> PdfError {
236    PdfError::Timeout("document timeout exceeded".into())
237}
238
239impl std::error::Error for PdfError {
240    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
241        match self {
242            PdfError::Encrypted(e) => Some(e),
243            _ => None,
244        }
245    }
246}
247
248#[cfg(feature = "pdfium")]
249impl From<pdfium_render::prelude::PdfiumError> for PdfError {
250    fn from(e: pdfium_render::prelude::PdfiumError) -> Self {
251        // A failed dlopen means the library this build was asked to use
252        // (`pdfium` feature) is not there. Say what to do instead of leaking
253        // the raw loader error.
254        if matches!(e, pdfium_render::prelude::PdfiumError::LoadLibraryError(_)) {
255            // The loader error pretty-prints over several lines; compact it.
256            let detail = e
257                .to_string()
258                .split_whitespace()
259                .collect::<Vec<_>>()
260                .join(" ");
261            return PdfError::Pdfium(format!(
262                "the pdfium library is not installed. This build's `pdfium` feature \
263                 was asked for it (DOCLING_RS_RENDERER=pdfium, or a file the pure-Rust \
264                 object model cannot read): point PDFIUM_DYNAMIC_LIB_PATH at a directory \
265                 containing the pdfium library, or unset DOCLING_RS_RENDERER — the \
266                 default renderer needs no library. Declarative formats (DOCX, HTML, \
267                 Markdown, …) never need it. [{detail}]"
268            ));
269        }
270        PdfError::Pdfium(e.to_string())
271    }
272}
273
274/// Convert a PDF's **embedded text layer only** — no ONNX, no
275/// threads: the pure-Rust content-stream parser ([`textparse`]) feeds the same
276/// orphan-region assembly the `no_ocr` pipeline flag uses, so text-layer PDFs
277/// come out identical to `--text-layer-only` (flat, line-grouped paragraphs in reading
278/// order; no headings/lists/tables/pictures, and no hyperlink recovery).
279///
280/// This is the only conversion entry compiled without the `ml` feature (it is
281/// what a wasm32 build runs). A scanned/image-only PDF (no embedded text
282/// layer) yields an empty document rather than an error, same as `no_ocr` —
283/// callers can detect that and fall back to an OCR-capable build.
284pub fn convert_text_layer(bytes: &[u8], name: &str) -> Result<DoclingDocument, PdfError> {
285    convert_text_layer_pages(bytes, name, None)
286}
287
288/// The error for bytes no reader here can parse as a PDF — not a PDF at
289/// all, or one damaged past the parser's repairs. Said about the file, not
290/// about a missing renderer or text layer: no library would read it either.
291pub(crate) const UNREADABLE: &str =
292    "not a readable PDF: the file is damaged or not a PDF (its structure could not be parsed)";
293
294/// [`convert_text_layer`] restricted to a **1-based inclusive** page window
295/// (issue #80's `--pages`); `None` converts everything. The window is
296/// validated the same way as [`Pipeline::pages`]: `first <= last`, 1-based,
297/// and it must select at least one existing page.
298pub fn convert_text_layer_pages(
299    bytes: &[u8],
300    name: &str,
301    pages: Option<(usize, usize)>,
302) -> Result<DoclingDocument, PdfError> {
303    if let Some((first, last)) = pages {
304        if first == 0 || last < first {
305            return Err(PdfError::Pdfium(format!(
306                "invalid page range {first}-{last} (pages are 1-based, first <= last)"
307            )));
308        }
309    }
310    let mut doc = DoclingDocument::new(name);
311    // A file nothing can open is an error, not a PDF that merely has no
312    // text layer (the caller's "scanned? needs OCR" hint).
313    let Some(mut parser) = textparse::PageTextParser::open(bytes) else {
314        return Err(PdfError::Document(UNREADABLE.into()));
315    };
316    // Pages are parsed, assembled and dropped one at a time: holding every
317    // page's cells until the end made the peak scale with the whole file —
318    // and a page window still paid for every page outside it.
319    let total = parser.page_count();
320    let mut tally = textparse::TextLayerTally::default();
321    for i in 0..total {
322        let selected = pages.is_none_or(|(first, last)| (first..=last).contains(&(i + 1)));
323        // A page outside the window only feeds the vestigial verdict below,
324        // so it is parsed only until that verdict is settled (on any real
325        // text layer, by the first page or two).
326        if !selected && tally.proves_text(total) {
327            continue;
328        }
329        let page = parser.text_page(i);
330        tally.add(&page);
331        if !selected {
332            continue;
333        }
334        let mut regions = Vec::new();
335        assemble::add_orphan_regions(&mut regions, &page.cells);
336        let table_rows = vec![None; regions.len()];
337        let enrich_out = vec![None; regions.len()];
338        let (mut nodes, links) =
339            assemble::assemble_page(&page, regions, &table_rows, &enrich_out, None);
340        assemble::stamp_page_no(&mut nodes, i + 1);
341        doc.nodes.extend(nodes);
342        doc.links.extend(links);
343    }
344    // A vestigial layer (a few typed-in form fields over scanned pages) is not
345    // the document's text: return the empty document, which callers already
346    // report as "no text layer" — so an OCR-capable caller falls back to OCR
347    // instead of proudly extracting thirteen characters.
348    if tally.is_vestigial() {
349        return Ok(DoclingDocument::new(name));
350    }
351    if let Some((first, last)) = pages {
352        if first > total {
353            return Err(PdfError::Pdfium(format!(
354                "page range {first}-{last} is outside the document ({total} page(s))"
355            )));
356        }
357    }
358    assemble::merge_continuations(&mut doc.nodes);
359    // Grown a page at a time, the node vector can be up to half spare
360    // capacity — hundreds of MB on a long document, held for its lifetime.
361    doc.nodes.shrink_to_fit();
362    Ok(doc)
363}
364
365/// Threads ONNX inference may use, capped by `DOCLING_RS_PDF_THREADS` if set.
366/// Defaults to the available parallelism (ort otherwise picks a low number).
367#[cfg(feature = "ml")]
368pub(crate) fn intra_threads() -> usize {
369    if let Some(n) = env::parse::<usize>("DOCLING_RS_PDF_THREADS").filter(|&n| n > 0) {
370        return n;
371    }
372    env::cpu_budget()
373}
374
375#[cfg(feature = "ml")]
376/// TableFormer's intra-op width (#262): `DOCLING_RS_TF_INTRA` explicitly,
377/// else the shared [`intra_threads`] budget. The shared TF session used to
378/// take the raw host width on top of the already-sized worker pools —
379/// under a cgroup CPU limit that oversubscription showed up as constant
380/// throttling and ~66% higher peak memory (each intra thread carries its own
381/// arena slab); the reporter's 4-CPU/8-core case dropped from 2.2 GB to
382/// 1.3 GB peak by capping this pool.
383pub(crate) fn tf_intra() -> usize {
384    if let Some(n) = env::parse::<usize>("DOCLING_RS_TF_INTRA").filter(|&n| n > 0) {
385        return n;
386    }
387    intra_threads()
388}
389
390#[cfg(feature = "ml")]
391/// True when `DOCLING_RS_FP32` forces the full-precision models even where
392/// an INT8 variant sits next to the fp32 default.
393pub(crate) fn fp32_forced() -> bool {
394    env::flag("DOCLING_RS_FP32")
395}
396
397#[cfg(feature = "ml")]
398/// Should the int8 model defaults be skipped in favor of fp32? Either the
399/// user said so (`DOCLING_RS_FP32`), or a GPU execution provider is selected
400/// (#74) — the int8 exports are QDQ graphs calibrated for CPU kernels and
401/// only conformance-validated there. An explicit `DOCLING_*_ONNX` path
402/// override still wins over this at every call site.
403pub(crate) fn prefer_fp32() -> bool {
404    fp32_forced() || docling_onnx::prefers_fp32()
405}
406
407#[cfg(feature = "ml")]
408/// Resolve a default (CWD-relative) asset path — the shared chain in
409/// [`docling_core::assets`]: CWD, then next to the executable and one level
410/// above it (the `scripts/install/install.sh` layout).
411pub(crate) fn resolve_asset(rel: &str) -> String {
412    docling_core::assets::resolve(rel)
413}
414
415/// One resolved runtime asset — which file a stage would load right now,
416/// given the CWD, the env overrides and the int8/fp32 preference.
417#[cfg(feature = "ml")]
418#[derive(Debug, Clone)]
419pub struct ModelEntry {
420    /// Pipeline stage, e.g. `layout`, `tableformer.decoder`, `ocr.rec`.
421    pub stage: &'static str,
422    /// The resolved path (absolute or CWD-relative, as it will be opened).
423    pub path: String,
424    /// Whether the file exists right now.
425    pub found: bool,
426    /// File size in bytes (0 when missing) — enough to tell an int8 quant
427    /// from an fp32 graph, or a stale model from a re-published one, at a
428    /// glance without hashing gigabytes per request.
429    pub bytes: u64,
430}
431
432/// Resolve the whole runtime model set **without loading anything** — the
433/// exact selection each stage performs at load time (layout honors the
434/// int8/fp32 preference, TableFormer its decoder ranking, OCR the language
435/// pair), plus the pdfium library. docling-serve exposes this at
436/// `/v1/config` and logs it at startup, so "the server picked up different
437/// models" is one `curl` away instead of a mystery of dissolved tables.
438/// Resolution is CWD-relative with an exe-dir fallback, so the answer can
439/// legitimately differ between two working directories.
440#[cfg(feature = "ml")]
441pub fn model_inventory() -> Vec<ModelEntry> {
442    fn entry(stage: &'static str, path: String) -> ModelEntry {
443        let meta = std::fs::metadata(&path).ok();
444        ModelEntry {
445            stage,
446            found: meta.is_some(),
447            bytes: meta.map(|m| m.len()).unwrap_or(0),
448            path,
449        }
450    }
451    let (enc, dec, bbx) = tableformer::resolved_paths();
452    let (rec, dict) = ocr::resolve_rec_pair(ocr::OcrLang::from_env());
453    let pdfium =
454        env::nonempty("PDFIUM_DYNAMIC_LIB_PATH").unwrap_or_else(|| resolve_asset(".pdfium/lib"));
455    vec![
456        entry(
457            "layout",
458            model_path(
459                "DOCLING_LAYOUT_ONNX",
460                ".models/layout_heron.onnx",
461                ".models/layout_heron_int8.onnx",
462            ),
463        ),
464        entry("tableformer.encoder", enc),
465        entry("tableformer.decoder", dec),
466        entry("tableformer.bbox", bbx),
467        entry("ocr.rec", rec),
468        entry("ocr.dict", dict),
469        entry("pdfium", pdfium),
470    ]
471}
472
473/// Resolve a model path: an explicit env override always wins; otherwise the
474/// INT8 variant of the default path when it exists on disk (the quantized
475/// models are conformance-validated — see docs/PDF_CONFORMANCE.md — and load/run
476/// markedly faster on CPU), unless `DOCLING_RS_FP32` opts back into full
477/// precision; else the fp32 default.
478#[cfg(feature = "ml")]
479pub(crate) fn model_path(key: &str, fp32_default: &str, int8_default: &str) -> String {
480    if let Some(p) = env::nonempty(key) {
481        return p;
482    }
483    if !prefer_fp32() {
484        let p = resolve_asset(int8_default);
485        if std::path::Path::new(&p).exists() {
486            return p;
487        }
488    }
489    resolve_asset(fp32_default)
490}
491
492/// Decode a standalone image with hard resource limits. A crafted image can
493/// declare enormous dimensions in a few-KB file; `image::load_from_memory`
494/// then tries to allocate the full pixel buffer (e.g. 60000×60000 → ~10 GB),
495/// and allocation failure aborts the whole process, bypassing the per-request
496/// panic catch. The 256 MiB alloc / 30000-px caps below turn that into a
497/// recoverable decode error instead. `DOCLING_RS_MAX_IMAGE_PIXELS` overrides
498/// the per-side pixel cap for the rare legitimately-huge scan.
499///
500/// Gated on `ml`: the only callers (`convert_image`, the METS backend) are
501/// ML-only, and the `image` crate is an `ml`-feature dependency — the
502/// text-layer wasm build has neither.
503#[cfg(feature = "ml")]
504pub(crate) fn decode_image_limited(bytes: &[u8]) -> Result<image::RgbImage, PdfError> {
505    let max_side: u32 = env::parse("DOCLING_RS_MAX_IMAGE_PIXELS").unwrap_or(30_000);
506    decode_image_with_max_side(bytes, max_side)
507}
508
509/// Whether `bytes` is an ISOBMFF HEIF/HEIC container (the `ftyp` brands
510/// iPhones write). Checked by content, not extension — HEIC regularly
511/// arrives misnamed `.jpg`.
512#[cfg(feature = "ml")]
513fn is_heif(bytes: &[u8]) -> bool {
514    bytes.len() >= 12
515        && &bytes[4..8] == b"ftyp"
516        && matches!(
517            &bytes[8..12],
518            b"heic" | b"heix" | b"hevc" | b"heim" | b"heis" | b"hevm" | b"hevs" | b"mif1" | b"msf1"
519        )
520}
521
522/// Decode a HEIF/HEIC primary image via libheif (#211). Behind the opt-in
523/// `heif` feature — libheif is a native dependency the default build (and
524/// wasm) must not carry.
525#[cfg(all(feature = "ml", feature = "heif"))]
526fn decode_heif(bytes: &[u8], max_side: u32) -> Result<image::RgbImage, PdfError> {
527    use libheif_rs::{ColorSpace, HeifContext, LibHeif, RgbChroma};
528    let err = |e: String| PdfError::Pdfium(format!("heif: {e}"));
529    let ctx = HeifContext::read_from_bytes(bytes).map_err(|e| err(e.to_string()))?;
530    let handle = ctx.primary_image_handle().map_err(|e| err(e.to_string()))?;
531    if handle.width() > max_side || handle.height() > max_side {
532        return Err(err(format!(
533            "image dimensions {}x{} exceed the {max_side}px per-side cap \
534             (DOCLING_RS_MAX_IMAGE_PIXELS overrides)",
535            handle.width(),
536            handle.height()
537        )));
538    }
539    let lib = LibHeif::new();
540    let img = lib
541        .decode(&handle, ColorSpace::Rgb(RgbChroma::Rgb), None)
542        .map_err(|e| err(e.to_string()))?;
543    let (w, h) = (img.width(), img.height());
544    let planes = img.planes();
545    let plane = planes
546        .interleaved
547        .ok_or_else(|| err("no RGB plane".into()))?;
548    let stride = plane.stride;
549    let mut out = image::RgbImage::new(w, h);
550    for (y, row) in out.rows_mut().enumerate() {
551        let src = &plane.data[y * stride..y * stride + w as usize * 3];
552        for (x, px) in row.enumerate() {
553            px.0 = [src[x * 3], src[x * 3 + 1], src[x * 3 + 2]];
554        }
555    }
556    Ok(out)
557}
558
559#[cfg(feature = "ml")]
560fn decode_image_with_max_side(bytes: &[u8], max_side: u32) -> Result<image::RgbImage, PdfError> {
561    use image::{ImageDecoder, ImageReader};
562    use std::io::Cursor;
563
564    if is_heif(bytes) {
565        #[cfg(feature = "heif")]
566        return decode_heif(bytes, max_side);
567        #[cfg(not(feature = "heif"))]
568        return Err(PdfError::Pdfium(
569            "HEIC/HEIF input needs a build with the `heif` cargo feature \
570             (rebuild with --features heif; links the system libheif)"
571                .into(),
572        ));
573    }
574
575    let mut limits = image::Limits::default();
576    limits.max_image_width = Some(max_side);
577    limits.max_image_height = Some(max_side);
578    limits.max_alloc = Some(256 * 1024 * 1024);
579
580    let mut reader = ImageReader::new(Cursor::new(bytes))
581        .with_guessed_format()
582        .map_err(|e| PdfError::Pdfium(format!("image: {e}")))?;
583    reader.limits(limits);
584    let mut decoder = reader
585        .into_decoder()
586        .map_err(|e| PdfError::Pdfium(format!("image: {e}")))?;
587    // docling#4247 (2.128, `ImageOps.exif_transpose`): a camera stores the
588    // sensor readout plus an orientation tag rather than rotated pixels, so
589    // a portrait photo would reach layout and OCR on its side unless the tag
590    // is honoured. An unreadable tag is no orientation, not an error.
591    let orientation = decoder
592        .orientation()
593        .unwrap_or(image::metadata::Orientation::NoTransforms);
594    let mut img = image::DynamicImage::from_decoder(decoder)
595        .map_err(|e| PdfError::Pdfium(format!("image: {e}")))?;
596    img.apply_orientation(orientation);
597    Ok(img.into_rgb8())
598}
599
600/// Image outputs of the PDF/image pipeline (#519/#520): the scale picture
601/// crops are delivered at and whether each page's render is kept as the
602/// document's page image. See [`Pipeline::images_scale`] /
603/// [`Pipeline::generate_page_images`].
604#[derive(Debug, Clone, Copy, Default, PartialEq)]
605pub struct ImageOutput {
606    /// Pixels per PDF point for picture crops and page images — docling's
607    /// `images_scale`. `None` keeps the pipeline's own page render (2.0
608    /// px/pt), resampling nothing.
609    pub scale: Option<f32>,
610    /// Keep every page's render as the document's page image — docling's
611    /// `generate_page_images`.
612    pub page_images: bool,
613}
614
615#[cfg(feature = "ml")]
616/// One page's assembled output: typed nodes plus the page's hyperlinks (kept
617/// separate so pages processed out of order can be stitched back in page
618/// order) and its confidence scores (#183).
619type PageOut = (
620    Vec<Node>,
621    Vec<(String, String)>,
622    docling_core::confidence::PageConfidence,
623    // The page image, when page images are requested (#520).
624    Option<docling_core::PictureImage>,
625);
626
627#[cfg(feature = "ml")]
628/// A page between region resolution and TableFormer: what `prepare_page`
629/// produced and `complete_page` still needs, so a pool worker can park the
630/// page while another worker holds the shared TableFormer (see `Staged`).
631struct Prepared {
632    regions: Vec<layout::Region>,
633    ocr_confs: Vec<f32>,
634    parse: Option<f64>,
635}
636
637#[cfg(feature = "ml")]
638/// A pool worker's per-page outcome. `NeedsTables` is a page whose only
639/// remaining stage is the shared TableFormer, which was busy when the worker
640/// got there: rather than block on the mutex — one worker idle for the
641/// whole of another worker's table decode, ~9.5 s of the 60-page .NET slice
642/// on a 2-worker pool — the worker keeps the page aside, pulls the next one
643/// off the render channel, and comes back once the slot is free. Results
644/// are reassembled by page index anyway, so completion order is free.
645enum Staged {
646    Done(PageOut),
647    NeedsTables(Prepared),
648}
649
650#[cfg(feature = "ml")]
651/// How many pages a pool worker keeps parked on the TableFormer before it
652/// falls back to waiting: each carries its ~5 MB bitmaps, so this bounds the
653/// extra residency to two pages per worker on top of the render channel.
654const MAX_DEFERRED_PAGES: usize = 2;
655
656#[cfg(feature = "ml")]
657/// The pool-wide TableFormer slot: one instance shared by every worker, loaded
658/// lazily on the first table region any worker sees. Tables appear on a
659/// minority of pages, so per-worker copies mostly multiplied ~0.4 GB of
660/// weights+arenas by the pool size for nothing; a single shared instance keeps
661/// the peak flat regardless of pool width, and a table's structure prediction
662/// is independent of which worker runs it, so output is byte-identical. The
663/// mutex serialises concurrent tables — the shared instance is loaded with the
664/// full intra-op thread budget to compensate (one wide TableFormer instead of
665/// several narrow ones).
666enum TfSlot {
667    /// Not attempted yet (no table seen so far).
668    Unloaded,
669    /// Load attempted, graphs absent — geometric fallback (warned once).
670    Missing,
671    Ready(tableformer::TableFormer),
672}
673
674#[cfg(feature = "ml")]
675impl TfSlot {
676    /// Load TableFormer on the first call: `Ready`, or `Missing` when its
677    /// graphs are absent (the geometric fallback — degradation, not an
678    /// error). Later calls are no-ops either way.
679    fn load(&mut self) {
680        if matches!(self, TfSlot::Unloaded) {
681            *self = match tableformer::TableFormer::load_with(tf_intra()) {
682                Some(tf) => TfSlot::Ready(tf),
683                None => TfSlot::Missing,
684            };
685        }
686    }
687}
688
689#[cfg(feature = "ml")]
690type SharedTables = Arc<Mutex<TfSlot>>;
691
692#[cfg(feature = "ml")]
693/// The same lazy shared-slot pattern for the (rarer still) enrichment models:
694/// one instance per pipeline, loaded on the first region that needs it.
695enum EnrichSlot<T> {
696    Unloaded,
697    /// Load attempted, model files absent — enrichment skipped (warned once).
698    Missing,
699    Ready(T),
700}
701
702#[cfg(feature = "ml")]
703type SharedClassifier = Arc<Mutex<EnrichSlot<enrich::PictureClassifier>>>;
704#[cfg(feature = "ml")]
705type SharedCodeFormula = Arc<Mutex<EnrichSlot<enrich::CodeFormula>>>;
706
707#[cfg(feature = "ml")]
708/// The opt-in enrichment passes, mirroring docling's `PdfPipelineOptions`
709/// flags (`do_picture_classification`, `do_code_enrichment`,
710/// `do_formula_enrichment`). All off by default.
711#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
712pub struct EnrichmentOptions {
713    /// Classify each picture with DocumentFigureClassifier (26 classes).
714    pub picture_classification: bool,
715    /// Rewrite code blocks (and detect their language) with CodeFormulaV2.
716    pub code: bool,
717    /// Decode display formulas to LaTeX with CodeFormulaV2.
718    pub formula: bool,
719}
720
721#[cfg(feature = "ml")]
722impl EnrichmentOptions {
723    fn any(&self) -> bool {
724        self.picture_classification || self.code || self.formula
725    }
726}
727
728#[cfg(feature = "ml")]
729/// The layout model's input for a page: the pypdfium2-exact scale-1.0 page
730/// image (#478) when the renderer produced one, else the legacy stretch of the 2×
731/// bitmap (browser / METS paths) — see [`layout::LayoutSrc`]. Public so the
732/// diagnostic examples feed [`layout::LayoutModel::predict`] the same input
733/// the pipeline does.
734pub fn layout_src(page: &PdfPage) -> layout::LayoutSrc<'_> {
735    match &page.image_layout {
736        Some(img) => layout::LayoutSrc::PageImage(img),
737        None => layout::LayoutSrc::Raw(&page.image),
738    }
739}
740
741#[cfg(feature = "ml")]
742/// RapidOCR's longest side (`Global.max_side_len`, 2000): docling renders the
743/// OCR input at `OcrOptions.scale` = 3 and RapidOCR then shrinks what exceeds
744/// this before detecting and recognizing, so that is the resolution its
745/// models actually see.
746const RAPIDOCR_MAX_SIDE: f32 = 2000.0;
747
748#[cfg(feature = "ml")]
749/// The px/pt the OCR reads a page at: the caller's `ocr_scale` when set; else,
750/// for a standalone bitmap input — its own scale-1.0 page, a resolution the
751/// pipeline did not choose — docling's rule (#570): three pixels per point
752/// (`OcrOptions.scale`), shrunk so the longer side stays within RapidOCR's
753/// 2000 px (a 754 × 1000 scanned form reads at 2.0; a 3000 px photo at 0.67,
754/// as RapidOCR would downsample it). `None` for a rendered page (PDF, hOCR),
755/// which keeps its own render — the 2.0 px/pt the conformance baselines are
756/// pinned to. Measured on FUNSD at 1.0 / 2.0 / 3.0 px/pt: 0.825 / 0.856 /
757/// 0.836 word recall — the recognizer likes its crops at the resolution
758/// RapidOCR hands it, no more.
759fn page_ocr_scale(ocr_scale: Option<f32>, width: f32, height: f32, scale: f32) -> Option<f32> {
760    if ocr_scale.is_some() || scale != 1.0 {
761        return ocr_scale;
762    }
763    let longest = width.max(height);
764    if longest <= 0.0 {
765        return None;
766    }
767    let s = 3.0f32.min(RAPIDOCR_MAX_SIDE / longest);
768    ((s - 1.0).abs() > 1e-3).then_some(s)
769}
770
771#[cfg(feature = "ml")]
772/// The bitmap + px/pt scale the OCR reads (#254, docling#3877's
773/// `OcrOptions.scale`): the page's own render unless `ocr_scale` asks for a
774/// different resolution, where a PIL-bicubic resample of that render is built
775/// once per page (cached in `cache`) and shared by every OCR pass. Resampling
776/// — rather than a second native pdfium render — keeps one code path across
777/// PDF, image, and hOCR inputs and leaves the layout/TableFormer pixels (and
778/// with them the conformance baseline) untouched; the 144-dpi base render is
779/// itself supersampled down from 216 dpi, so an upscaled OCR view loses
780/// little against a native render.
781fn ocr_input<'a>(
782    cache: &'a mut Option<image::RgbImage>,
783    image: &'a image::RgbImage,
784    scale: f32,
785    ocr_scale: Option<f32>,
786) -> (&'a image::RgbImage, f32) {
787    match ocr_scale {
788        Some(s) if (s - scale).abs() > 1e-3 && image.width() > 1 => {
789            let f = s / scale;
790            let img = cache.get_or_insert_with(|| {
791                let dw = ((image.width() as f32 * f).round() as u32).max(1);
792                let dh = ((image.height() as f32 * f).round() as u32).max(1);
793                resample::pil_resize(image, dw, dh, resample::PilFilter::Bicubic)
794            });
795            (img, s)
796        }
797        _ => (image, scale),
798    }
799}
800
801#[cfg(feature = "ml")]
802/// A self-contained set of the per-page models (layout, OCR). Each parallel
803/// page-worker owns its own `Worker` so inference runs concurrently without
804/// sharing an ONNX session (`ort`'s `Session::run` is `&mut self`); only the
805/// rarely-hit TableFormer is shared (see [`TfSlot`]).
806struct Worker {
807    /// `None` when `no_ocr` skips layout entirely — no model load, no inference.
808    layout: Option<layout::LayoutModel>,
809    ocr: OcrSlot,
810    det: DetSlot,
811    /// Text-detector results computed ahead of a page's OCR pass (#429), keyed
812    /// by page index: the detector reads nothing but the page image, so it
813    /// runs on its own thread while the layout model predicts — see
814    /// [`Self::detect_alongside`] — and the OCR block collects the boxes here
815    /// instead of paying for detection serially.
816    pending_det: BTreeMap<usize, Result<Vec<ocr_det::DetBox>, String>>,
817    /// This worker's intra-op thread budget — also the OCR lane count (see
818    /// [`ocr::OcrModel::load_with`]): a pool worker with two threads runs two
819    /// single-thread recognisers, the primary as many as its cores.
820    intra: usize,
821    /// Shared TableFormer slot; `None` when `no_table_former`/`no_ocr` skip it.
822    tables: Option<SharedTables>,
823    /// Shared enrichment slots; `None` unless the corresponding flag is on.
824    classifier: Option<SharedClassifier>,
825    code_formula: Option<SharedCodeFormula>,
826    enrich: EnrichmentOptions,
827    /// Skip layout, OCR, and TableFormer; reconstruct text purely from the PDF's
828    /// embedded text layer. See [`Pipeline::no_ocr`].
829    no_ocr: bool,
830    /// Discard the embedded text layer and OCR every page. See
831    /// [`Pipeline::force_full_page_ocr`].
832    force_full_page_ocr: bool,
833    /// Keep text-panel pictures as pictures instead of demoting them to
834    /// paragraphs. See [`Pipeline::no_text_panels`].
835    no_text_panels: bool,
836    /// Never run OCR, but keep layout + TableFormer (#244) — docling's
837    /// `do_ocr=False`. See [`Pipeline::skip_ocr`].
838    skip_ocr: bool,
839    /// Which recognition model [`Self::ocr`] loads. See [`Pipeline::ocr_lang`].
840    ocr_lang: ocr::OcrLang,
841    /// Which engine [`Self::ocr`] is (#460). See [`Pipeline::ocr_engine`].
842    ocr_engine: ocr::OcrEngine,
843    /// Tesseract's `-l` argument (#460). See [`Pipeline::tesseract_lang`].
844    tesseract_lang: Option<String>,
845    /// OCR render scale override (px/pt, #254). See [`Pipeline::ocr_scale`].
846    ocr_scale: Option<f32>,
847    /// Picture-crop scale and page images (#519/#520). See [`ImageOutput`].
848    images: ImageOutput,
849}
850
851#[cfg(feature = "ml")]
852/// The worker's lazily-loaded OCR recognition model. `Missing` records a
853/// failed load (#244: degradation over failure — a deployment without the OCR
854/// model still gets layout + TableFormer, and OCR-dependent regions stay
855/// empty) so the load isn't retried per page.
856enum OcrSlot {
857    Unloaded,
858    Ready(Recognizer),
859    Missing,
860}
861
862#[cfg(feature = "ml")]
863/// The loaded OCR engine (#460): PP-OCRv3 via ONNX Runtime or the
864/// `tesseract` binary. Both answer the two questions the page assembly asks
865/// — line cells for text regions, word cells for table regions — in page
866/// points with a 0–1 confidence, so the call sites never know which runs.
867pub(crate) enum Recognizer {
868    PpOcr(ocr::OcrModel),
869    Tesseract(tesseract::TesseractOcr),
870}
871
872#[cfg(feature = "ml")]
873impl Recognizer {
874    /// See [`ocr::OcrModel::ocr_page_with`].
875    fn ocr_page(
876        &mut self,
877        img: &image::RgbImage,
878        regions: &[layout::Region],
879        scale: f32,
880    ) -> Result<Vec<(TextCell, f32)>, String> {
881        self.ocr_page_with(img, regions, scale, None)
882    }
883
884    /// [`Self::ocr_page`] with the detector's boxes (#570). Tesseract segments its
885    /// own lines and ignores the detector's boxes.
886    fn ocr_page_with(
887        &mut self,
888        img: &image::RgbImage,
889        regions: &[layout::Region],
890        scale: f32,
891        detected: Option<&[ocr_det::DetBox]>,
892    ) -> Result<Vec<(TextCell, f32)>, String> {
893        match self {
894            Self::PpOcr(m) => m.ocr_page_with(img, regions, scale, detected),
895            Self::Tesseract(t) => t.ocr_page(img, regions, scale),
896        }
897    }
898
899    /// See [`ocr::OcrModel::ocr_table_words`].
900    fn ocr_table_words(
901        &mut self,
902        img: &image::RgbImage,
903        regions: &[layout::Region],
904        scale: f32,
905        detected: Option<&[ocr_det::DetBox]>,
906    ) -> Result<Vec<(TextCell, f32)>, String> {
907        match self {
908            Self::PpOcr(m) => m.ocr_table_words(img, regions, scale, detected),
909            Self::Tesseract(t) => t.ocr_table_words(img, regions, scale),
910        }
911    }
912}
913
914#[cfg(feature = "ml")]
915/// The text detector (#429), loaded on the first OCR'd page like the
916/// recognizer; `Missing` keeps the pipeline recognition-only.
917enum DetSlot {
918    Unloaded,
919    Ready(ocr_det::DetModel),
920    Missing,
921}
922
923#[cfg(feature = "ml")]
924impl Worker {
925    #[allow(clippy::too_many_arguments)] // mirrors the Pipeline's option set
926    fn load(
927        intra: usize,
928        tables: Option<SharedTables>,
929        enrich_slots: (Option<SharedClassifier>, Option<SharedCodeFormula>),
930        enrich: EnrichmentOptions,
931        no_ocr: bool,
932        skip_ocr: bool,
933        force_full_page_ocr: bool,
934        no_text_panels: bool,
935        ocr_lang: ocr::OcrLang,
936        ocr_engine: ocr::OcrEngine,
937        tesseract_lang: Option<String>,
938        ocr_scale: Option<f32>,
939        images: ImageOutput,
940    ) -> Result<Self, PdfError> {
941        Ok(Self {
942            images,
943            layout: if no_ocr {
944                None
945            } else {
946                Some(layout::LayoutModel::load_with(intra).map_err(PdfError::Layout)?)
947            },
948            ocr: OcrSlot::Unloaded,
949            det: DetSlot::Unloaded,
950            pending_det: BTreeMap::new(),
951            intra,
952            tables,
953            classifier: enrich_slots.0,
954            code_formula: enrich_slots.1,
955            enrich,
956            no_ocr,
957            skip_ocr,
958            force_full_page_ocr,
959            no_text_panels,
960            ocr_lang,
961            ocr_engine,
962            tesseract_lang,
963            ocr_scale,
964        })
965    }
966
967    /// The OCR model, or `None` when this conversion must not (or cannot) OCR:
968    /// `skip_ocr` short-circuits, and a failed model load degrades to `None`
969    /// with a one-time warning instead of failing the conversion (#244) —
970    /// unless `force_full_page_ocr` demanded OCR explicitly, where a missing
971    /// model stays a hard error (the text layer was deliberately discarded, so
972    /// degrading would silently emit an empty document).
973    fn ocr_model(&mut self) -> Result<Option<&mut Recognizer>, PdfError> {
974        if self.skip_ocr {
975            return Ok(None);
976        }
977        if matches!(self.ocr, OcrSlot::Unloaded) {
978            let loaded = match self.ocr_engine {
979                ocr::OcrEngine::PpOcr => {
980                    ocr::OcrModel::load_with(self.ocr_lang, self.intra).map(Recognizer::PpOcr)
981                }
982                ocr::OcrEngine::Tesseract => tesseract::TesseractOcr::load(
983                    tesseract::TesseractOptions::from_env(self.tesseract_lang.clone()),
984                    self.intra,
985                )
986                .map(Recognizer::Tesseract),
987            };
988            match loaded {
989                Ok(model) => self.ocr = OcrSlot::Ready(model),
990                Err(e) if self.force_full_page_ocr => return Err(PdfError::Ocr(e)),
991                Err(e) => {
992                    static WARNED: std::sync::Once = std::sync::Once::new();
993                    let hint = match self.ocr_engine {
994                        ocr::OcrEngine::PpOcr => {
995                            "run scripts/install/download_dependencies.sh for the model"
996                        }
997                        ocr::OcrEngine::Tesseract => {
998                            "install tesseract-ocr with the language pack, or use --ocr-engine ppocr"
999                        }
1000                    };
1001                    WARNED.call_once(|| {
1002                        eprintln!(
1003                            "warning: OCR model unavailable ({e}); continuing without OCR — \
1004                             scanned pages and text inside images will come back empty \
1005                             ({hint})"
1006                        );
1007                    });
1008                    self.ocr = OcrSlot::Missing;
1009                }
1010            }
1011        }
1012        Ok(match &mut self.ocr {
1013            OcrSlot::Ready(model) => Some(model),
1014            _ => None,
1015        })
1016    }
1017
1018    /// The text detector (#429): loaded on first use, `None` with `skip_ocr`
1019    /// or when the model is not installed — recognition stays region-scoped
1020    /// then, exactly the pre-#429 behavior, and `DOCLING_RS_DEBUG` says why.
1021    fn det_model(&mut self) -> Option<&mut ocr_det::DetModel> {
1022        if self.skip_ocr {
1023            return None;
1024        }
1025        if matches!(self.det, DetSlot::Unloaded) {
1026            match ocr_det::DetModel::load(self.intra) {
1027                Ok(model) => self.det = DetSlot::Ready(model),
1028                Err(e) => {
1029                    docling_core::debug_log!(
1030                        "docling-pdf: text detection unavailable ({e}); OCR stays region-scoped"
1031                    );
1032                    self.det = DetSlot::Missing;
1033                }
1034            }
1035        }
1036        match &mut self.det {
1037            DetSlot::Ready(model) => Some(model),
1038            _ => None,
1039        }
1040    }
1041
1042    /// Run layout (+ OCR for cell-less pages) + TableFormer and assemble page `n`
1043    /// into its nodes and links. Pure given the page (mutates only the worker's
1044    /// lazily-loaded OCR model), so it is safe to run concurrently across pages.
1045    fn process(&mut self, n: usize, page: &mut PdfPage) -> Result<PageOut, PdfError> {
1046        if self.no_ocr {
1047            // Fastest path: no layout/OCR/TableFormer inference at all. The PDF's
1048            // embedded text cells (if any) become flat, line-grouped paragraphs in
1049            // reading order via the same orphan-region machinery that normally
1050            // rescues text the detector missed — here it rescues *all* of it.
1051            // Pages with no embedded text layer (scanned/image-only) yield nothing;
1052            // convert those without `no_ocr`.
1053            let parse = quality::parse_score(&page.cells);
1054            let mut regions = Vec::new();
1055            assemble::add_orphan_regions(&mut regions, &page.cells);
1056            let table_rows = vec![None; regions.len()];
1057            let enrich_out = vec![None; regions.len()];
1058            let conf = quality::page_confidence(parse, &regions, &[]);
1059            let (nodes, links) = timing::timed("assemble_page", || {
1060                assemble::assemble_page(page, regions, &table_rows, &enrich_out, self.images.scale)
1061            });
1062            return Ok((nodes, links, conf, self.page_image(n, page)));
1063        }
1064        self.normalize_orientation(n, page)?;
1065        let det_pages = self.det_candidates(std::slice::from_ref(&(n, &*page)))?;
1066        let regions = {
1067            let Self {
1068                layout,
1069                det,
1070                pending_det,
1071                ocr_scale,
1072                ..
1073            } = self;
1074            let layout = layout.as_mut().expect("layout model loaded unless no_ocr");
1075            let page: &PdfPage = &*page;
1076            Self::detect_alongside(det, pending_det, *ocr_scale, &det_pages, || {
1077                timing::timed("layout.predict", || {
1078                    layout.predict(layout_src(page), page.width, page.height)
1079                })
1080            })
1081        }
1082        .map_err(|e| PdfError::Layout(format!("page {}: {e}", n + 1)))?;
1083        self.finish_page(n, page, regions)
1084    }
1085
1086    /// The page's render as docling's `PageItem.image` (#520), at
1087    /// [`ImageOutput::scale`] — `None` unless page images were requested, or
1088    /// when the page has no bitmap (the text-layer-only `no_ocr` path never
1089    /// renders one).
1090    fn page_image(&self, n: usize, page: &PdfPage) -> Option<docling_core::PictureImage> {
1091        if !self.images.page_images {
1092            return None;
1093        }
1094        let image = assemble::page_image(page, self.images.scale);
1095        if image.is_none() {
1096            docling_core::debug_log!("page {}: no page bitmap for the page image", n + 1);
1097        }
1098        image
1099    }
1100
1101    /// The pages of a batch the text detector should sweep (#429): bitmap
1102    /// pages (no text layer) on a run with OCR and the detector available —
1103    /// loading both lazily here so the concurrent run below needs no `self`.
1104    fn det_candidates<'p>(
1105        &mut self,
1106        pages: &[(usize, &'p PdfPage)],
1107    ) -> Result<Vec<(usize, &'p PdfPage)>, PdfError> {
1108        // Pages the orientation pass already detected (#571) are skipped:
1109        // their boxes wait in `pending_det`.
1110        let scanned: Vec<(usize, &PdfPage)> = pages
1111            .iter()
1112            .filter(|(n, p)| {
1113                p.cells.is_empty() && p.image.width() > 1 && !self.pending_det.contains_key(n)
1114            })
1115            .copied()
1116            .collect();
1117        if scanned.is_empty() || self.ocr_model()?.is_none() || self.det_model().is_none() {
1118            return Ok(Vec::new());
1119        }
1120        Ok(scanned)
1121    }
1122
1123    /// Run `layout` on the calling thread while the text detector sweeps
1124    /// `det_pages` on another (#429): the detector only reads each page's
1125    /// image, layout only reads the pages too, and the two sessions are
1126    /// independent, so on a scanned page the ~0.7 s detection hides behind
1127    /// layout instead of adding to it. Results land in `pending_det` for the
1128    /// OCR block; a detector failure is recorded per page and surfaces there.
1129    fn detect_alongside<T>(
1130        det: &mut DetSlot,
1131        pending_det: &mut BTreeMap<usize, Result<Vec<ocr_det::DetBox>, String>>,
1132        ocr_scale: Option<f32>,
1133        det_pages: &[(usize, &PdfPage)],
1134        layout: impl FnOnce() -> T,
1135    ) -> T {
1136        let DetSlot::Ready(det) = det else {
1137            return layout();
1138        };
1139        if det_pages.is_empty() {
1140            return layout();
1141        }
1142        std::thread::scope(|s| {
1143            let handle = s.spawn(move || {
1144                det_pages
1145                    .iter()
1146                    .map(|&(n, page)| {
1147                        let mut view = None;
1148                        let (img, _) = ocr_input(
1149                            &mut view,
1150                            &page.image,
1151                            page.scale,
1152                            page_ocr_scale(ocr_scale, page.width, page.height, page.scale),
1153                        );
1154                        (n, timing::timed("ocr.det", || det.detect(img)))
1155                    })
1156                    .collect::<Vec<_>>()
1157            });
1158            let out = layout();
1159            match handle.join() {
1160                Ok(results) => pending_det.extend(results),
1161                Err(_) => {
1162                    for &(n, _) in det_pages {
1163                        pending_det.insert(n, Err("ocr-det: detection thread panicked".into()));
1164                    }
1165                }
1166            }
1167            out
1168        })
1169    }
1170
1171    /// Content-based orientation normalization (#225), before any inference:
1172    /// a physically rotated scan (sideways phone photo, landscape-fed sheet)
1173    /// has `/Rotate 0`, so the metadata pass in `extract_page` never fires and
1174    /// layout+OCR would read a sideways raster. Only pages with no text layer
1175    /// at all are probed (a digital page's raster is upright by construction,
1176    /// and its cells — not its pixels — carry the text); the detected angle
1177    /// composes with any `/Rotate` normalization through the same
1178    /// [`PdfPage::unrotate`] + display-space assembly mapping. Detection is
1179    /// evidence-gated and degrades to a no-op — see [`orient`].
1180    fn normalize_orientation(&mut self, n: usize, page: &mut PdfPage) -> Result<(), PdfError> {
1181        let scanned =
1182            page.cells.is_empty() && page.word_cells.is_empty() && page.code_cells.is_empty();
1183        if self.no_ocr || self.skip_ocr || !scanned || page.image.width() <= 1 || !orient::enabled()
1184        {
1185            return Ok(());
1186        }
1187        // The probe reads text through the OCR model; without one (missing —
1188        // #244 degradation) the page stays as rendered.
1189        if self.ocr_model()?.is_none() {
1190            return Ok(());
1191        }
1192        // The text detector's lines are the probe's crops (#571). Detected on
1193        // the page image as rendered; an upright page keeps them for the OCR
1194        // pass (`pending_det`) when that pass reads the same bitmap, so the
1195        // detector runs once per page as before — a rotated page detects
1196        // again on the un-rotated image, alongside layout.
1197        let boxes: Vec<ocr_det::DetBox> = match self.det_model() {
1198            Some(det) => timing::timed("orient.det", || det.detect(&page.image)).unwrap_or_else(|e| {
1199                debug_log!("docling-pdf: page {}: text detection failed ({e}); probing projection strips", n + 1);
1200                Vec::new()
1201            }),
1202            None => Vec::new(),
1203        };
1204        let ocr_scale = page_ocr_scale(self.ocr_scale, page.width, page.height, page.scale);
1205        let Some(ocr) = self.ocr_model()? else {
1206            return Ok(());
1207        };
1208        let deg = timing::timed("orient.detect", || {
1209            orient::detect(&page.image, ocr, page.scale, &boxes)
1210        });
1211        if deg != 0 {
1212            debug_log!(
1213                "docling-pdf: page {}: content rotated {deg}° in the raster; \
1214                 un-rotating before layout/OCR",
1215                n + 1
1216            );
1217            page.unrotate(deg);
1218        } else if !boxes.is_empty() && ocr_scale.is_none_or(|s| (s - page.scale).abs() <= 1e-3) {
1219            self.pending_det.insert(n, Ok(boxes));
1220        }
1221        Ok(())
1222    }
1223
1224    /// Layout-detect a whole batch of pages with one inference call (issue #73),
1225    /// then run each page's remaining stages (OCR / TableFormer / enrichment /
1226    /// assembly) per page. Index-aligned with `items`; a layout failure fails
1227    /// every page in the batch (they shared the one inference call).
1228    fn process_batch(&mut self, items: &mut [(usize, PdfPage)]) -> Vec<Result<Staged, PdfError>> {
1229        if self.no_ocr {
1230            // No layout model to batch — the text-layer-only path is per page.
1231            return items
1232                .iter_mut()
1233                .map(|(n, page)| {
1234                    let n = *n;
1235                    self.process(n, page).map(Staged::Done)
1236                })
1237                .collect();
1238        }
1239        // Orientation-normalize every scanned page before the shared layout
1240        // call — the batched inference must see upright bitmaps too (#225).
1241        for (n, page) in items.iter_mut() {
1242            let n = *n;
1243            if let Err(e) = self.normalize_orientation(n, page) {
1244                // Model-load failure — every page in the batch needs the same
1245                // model, so they all fail alike (mirrors the layout-error arm).
1246                let msg = e.to_string();
1247                return items
1248                    .iter()
1249                    .map(|_| Err(PdfError::Ocr(msg.clone())))
1250                    .collect();
1251            }
1252        }
1253        let inputs: Vec<(layout::LayoutSrc<'_>, f32, f32)> = items
1254            .iter()
1255            .map(|(_, page)| (layout_src(page), page.width, page.height))
1256            .collect();
1257        let refs: Vec<(usize, &PdfPage)> = items.iter().map(|(n, page)| (*n, page)).collect();
1258        let det_pages = match self.det_candidates(&refs) {
1259            Ok(p) => p,
1260            Err(e) => {
1261                let msg = e.to_string();
1262                return items
1263                    .iter()
1264                    .map(|_| Err(PdfError::Ocr(msg.clone())))
1265                    .collect();
1266            }
1267        };
1268        let batched = {
1269            let Self {
1270                layout,
1271                det,
1272                pending_det,
1273                ocr_scale,
1274                ..
1275            } = self;
1276            let layout = layout.as_mut().expect("layout model loaded unless no_ocr");
1277            Self::detect_alongside(det, pending_det, *ocr_scale, &det_pages, || {
1278                timing::timed("layout.predict", || layout.predict_batch(&inputs))
1279            })
1280        };
1281        match batched {
1282            Ok(all) => items
1283                .iter_mut()
1284                .zip(all)
1285                .map(|((n, page), regions)| self.stage_page(*n, page, regions))
1286                .collect(),
1287            Err(e) => items
1288                .iter()
1289                .map(|(n, _)| Err(PdfError::Layout(format!("page {}: {e}", n + 1))))
1290                .collect(),
1291        }
1292    }
1293
1294    /// A pool worker's main loop: pull rendered pages off the shared channel
1295    /// (whatever is already there, up to the layout batch size), process them,
1296    /// and hand each finished page — or its error — to `deliver`, which returns
1297    /// `false` to stop early (the streaming consumer went away). Returns when
1298    /// the channel is closed and every page this worker took is delivered.
1299    ///
1300    /// Pages whose TableFormer turn would have to wait are parked (see
1301    /// `Staged`) and retried before every new pull; while any are parked the
1302    /// pull is non-blocking, so an empty channel means the worker waits for
1303    /// the TableFormer rather than for the renderer. The parking budget
1304    /// (`MAX_DEFERRED_PAGES`) bounds resident bitmaps; past it the worker waits
1305    /// like the serial path. Output is independent of completion order — the
1306    /// callers reassemble by page index.
1307    fn run_pool(
1308        &mut self,
1309        work_rx: &Mutex<Receiver<(usize, PdfPage)>>,
1310        layout_batch: usize,
1311        deadline: Option<std::time::Instant>,
1312        mut deliver: impl FnMut(usize, Result<PageOut, PdfError>) -> bool,
1313    ) {
1314        use std::sync::mpsc::TryRecvError;
1315        let mut deferred: std::collections::VecDeque<(usize, PdfPage, Prepared)> =
1316            std::collections::VecDeque::new();
1317        loop {
1318            // The document budget (#497), checked between pages: once it is
1319            // spent, every page still queued or parked is reported as timed
1320            // out instead of processed — the renderer stops feeding at the
1321            // same check, so the queue drains quickly.
1322            if expired(deadline) {
1323                while let Some((idx, _, _)) = deferred.pop_front() {
1324                    if !deliver(idx, Err(timeout_sentinel())) {
1325                        return;
1326                    }
1327                }
1328                let rx = work_rx.lock().unwrap();
1329                loop {
1330                    match rx.recv() {
1331                        Ok((idx, _page)) => {
1332                            if !deliver(idx, Err(timeout_sentinel())) {
1333                                return;
1334                            }
1335                        }
1336                        Err(_) => return,
1337                    }
1338                }
1339            }
1340            // Any parked page the slot has since freed up for, oldest first.
1341            let mut i = 0;
1342            while i < deferred.len() {
1343                let (_, page, prepared) = &deferred[i];
1344                match self.table_rows_try(page, &prepared.regions) {
1345                    Some(rows) => {
1346                        let (idx, mut page, prepared) = deferred.remove(i).expect("index in range");
1347                        if !deliver(idx, self.complete_page(idx, &mut page, prepared, rows)) {
1348                            return;
1349                        }
1350                    }
1351                    None => i += 1,
1352                }
1353            }
1354            // Hold the receiver lock only for the recv (plus a non-blocking drain
1355            // up to the layout batch size); release before the (long) per-page
1356            // work so other workers can pull concurrently.
1357            let mut batch: Vec<(usize, PdfPage)> = Vec::new();
1358            let mut closed = false;
1359            {
1360                let rx = work_rx.lock().unwrap();
1361                let first = if deferred.is_empty() {
1362                    rx.recv().map_err(|_| TryRecvError::Disconnected)
1363                } else {
1364                    rx.try_recv()
1365                };
1366                match first {
1367                    Ok(item) => {
1368                        batch.push(item);
1369                        while batch.len() < layout_batch {
1370                            match rx.try_recv() {
1371                                Ok(item) => batch.push(item),
1372                                Err(_) => break,
1373                            }
1374                        }
1375                    }
1376                    Err(TryRecvError::Empty) => {}
1377                    Err(TryRecvError::Disconnected) => closed = true,
1378                }
1379            }
1380            if batch.is_empty() {
1381                // Nothing new rendered (or the channel is closed): wait our turn
1382                // on the oldest parked page instead.
1383                match deferred.pop_front() {
1384                    Some((idx, mut page, prepared)) => {
1385                        let rows = self.table_rows_blocking(&page, &prepared.regions);
1386                        if !deliver(idx, self.complete_page(idx, &mut page, prepared, rows)) {
1387                            return;
1388                        }
1389                        continue;
1390                    }
1391                    None if closed => return,
1392                    None => continue,
1393                }
1394            }
1395            let outs = self.process_batch(&mut batch);
1396            for ((idx, mut page), out) in batch.into_iter().zip(outs) {
1397                let delivered = match out {
1398                    Ok(Staged::Done(out)) => deliver(idx, Ok(out)),
1399                    Ok(Staged::NeedsTables(prepared)) => {
1400                        if deferred.len() < MAX_DEFERRED_PAGES {
1401                            deferred.push_back((idx, page, prepared));
1402                            true
1403                        } else {
1404                            let rows = self.table_rows_blocking(&page, &prepared.regions);
1405                            deliver(idx, self.complete_page(idx, &mut page, prepared, rows))
1406                        }
1407                    }
1408                    Err(e) => deliver(idx, Err(e)),
1409                };
1410                if !delivered {
1411                    return;
1412                }
1413            }
1414        }
1415    }
1416
1417    /// Everything after layout detection: per-label confidence thresholds,
1418    /// overlap resolution, orphan-text recovery, OCR for cell-less pages,
1419    /// TableFormer, enrichment, and page assembly. The serial path: waits
1420    /// for the shared TableFormer when a table needs it.
1421    fn finish_page(
1422        &mut self,
1423        n: usize,
1424        page: &mut PdfPage,
1425        regions: Vec<layout::Region>,
1426    ) -> Result<PageOut, PdfError> {
1427        let prepared = self.prepare_page(n, page, regions)?;
1428        let table_rows = self.table_rows_blocking(page, &prepared.regions);
1429        self.complete_page(n, page, prepared, table_rows)
1430    }
1431
1432    /// The pool path: like [`finish_page`](Self::finish_page), except that a
1433    /// page whose TableFormer turn would have to wait comes back as
1434    /// [`Staged::NeedsTables`] for the worker loop to park (see `Staged`).
1435    fn stage_page(
1436        &mut self,
1437        n: usize,
1438        page: &mut PdfPage,
1439        regions: Vec<layout::Region>,
1440    ) -> Result<Staged, PdfError> {
1441        let prepared = self.prepare_page(n, page, regions)?;
1442        match self.table_rows_try(page, &prepared.regions) {
1443            Some(rows) => Ok(Staged::Done(self.complete_page(n, page, prepared, rows)?)),
1444            None => Ok(Staged::NeedsTables(prepared)),
1445        }
1446    }
1447
1448    /// Does this page need the shared TableFormer at all? Table-free pages
1449    /// never touch (or load) it.
1450    fn needs_tables(&self, regions: &[layout::Region]) -> bool {
1451        self.tables.is_some() && regions.iter().any(|r| assemble::is_table_like(r.label))
1452    }
1453
1454    /// TableFormer structure for every table region of the page, on an
1455    /// already-locked slot (loading the model on first use). Tables serialise
1456    /// on this mutex, so the one instance gets the shared thread budget
1457    /// (quota-aware, #262) — DOCLING_RS_TF_INTRA narrows it further where the
1458    /// memory-per-thread tradeoff matters more than table latency.
1459    fn predict_tables(
1460        guard: &mut TfSlot,
1461        page: &PdfPage,
1462        regions: &[layout::Region],
1463    ) -> Vec<Option<tf_core::TableGrid>> {
1464        let mut table_rows: Vec<Option<tf_core::TableGrid>> = vec![None; regions.len()];
1465        guard.load();
1466        if let TfSlot::Ready(tf) = guard {
1467            // One 1024-px frame per page, shared by all of its tables, and one
1468            // call for all of them: with the dynamic-batch decoder their
1469            // decode steps are shared (each step costs about the same for B
1470            // tables as for one).
1471            let page1024 = tableformer::TableFormer::page_1024(&page.image);
1472            let (idx, boxes): (Vec<usize>, Vec<[f32; 4]>) = regions
1473                .iter()
1474                .enumerate()
1475                .filter(|(_, r)| assemble::is_table_like(r.label))
1476                .map(|(i, r)| (i, [r.l, r.t, r.r, r.b]))
1477                .unzip();
1478            let rows =
1479                tf.predict_tables_on(page.image.height(), &page1024, &boxes, &page.word_cells);
1480            for (i, grid) in idx.into_iter().zip(rows) {
1481                table_rows[i] = grid;
1482            }
1483        }
1484        table_rows
1485    }
1486
1487    /// Table structure for the page, waiting for the shared slot if another
1488    /// worker holds it (else geometric fallback downstream when there is no
1489    /// TableFormer at all). The `tableformer` timing stage here includes any
1490    /// wait.
1491    fn table_rows_blocking(
1492        &self,
1493        page: &PdfPage,
1494        regions: &[layout::Region],
1495    ) -> Vec<Option<tf_core::TableGrid>> {
1496        match self.tables.as_ref().filter(|_| self.needs_tables(regions)) {
1497            Some(slot) => timing::timed("tableformer", || {
1498                Self::predict_tables(&mut slot.lock().unwrap(), page, regions)
1499            }),
1500            None => vec![None; regions.len()],
1501        }
1502    }
1503
1504    /// Non-blocking variant: `None` when the slot is held by another worker
1505    /// right now — the caller parks the page and tries again later.
1506    fn table_rows_try(
1507        &self,
1508        page: &PdfPage,
1509        regions: &[layout::Region],
1510    ) -> Option<Vec<Option<tf_core::TableGrid>>> {
1511        let Some(slot) = self.tables.as_ref().filter(|_| self.needs_tables(regions)) else {
1512            return Some(vec![None; regions.len()]);
1513        };
1514        match slot.try_lock() {
1515            Ok(mut guard) => Some(timing::timed("tableformer", || {
1516                Self::predict_tables(&mut guard, page, regions)
1517            })),
1518            Err(std::sync::TryLockError::WouldBlock) => None,
1519            Err(std::sync::TryLockError::Poisoned(e)) => panic!("TableFormer slot poisoned: {e}"),
1520        }
1521    }
1522
1523    /// The stages before TableFormer: fp32 escalation, per-label confidence
1524    /// thresholds, overlap resolution, orphan-text recovery, OCR for cell-less
1525    /// pages, in-picture text and table-word recognition.
1526    fn prepare_page(
1527        &mut self,
1528        n: usize,
1529        page: &mut PdfPage,
1530        regions: Vec<layout::Region>,
1531    ) -> Result<Prepared, PdfError> {
1532        // Force-OCR is exactly "pretend the text layer is not there": clear
1533        // every cell kind the extractors produced before anything reads them,
1534        // and the ordinary no-text-layer machinery below — full-page OCR,
1535        // OCR-fed TableFormer matching — takes over unchanged. (`no_ocr` wins
1536        // when both are set, mirroring docling, where `force_full_page_ocr`
1537        // is a sub-option of `do_ocr`; the no-ocr path never reaches here.)
1538        // Done here rather than in `process` so the batched layout path
1539        // (`process_batch` → `finish_page`) honors the flag too.
1540        // Parse quality is scored on the extracted text layer before force-OCR
1541        // discards it (docling's page-preprocessing stage runs before OCR too,
1542        // so its parse_score also reflects the original text layer).
1543        let parse = quality::parse_score(&page.cells);
1544        // Recognition confidences of every OCR'd cell on this page → ocr_score.
1545        let mut ocr_confs: Vec<f32> = Vec::new();
1546        // The bitmap the OCR reads (#254): with `ocr_scale` set, a resample of
1547        // the page render at the requested px/pt, built lazily on the first
1548        // OCR use so non-OCR pages never pay for it. Copied out of `self` up
1549        // front — the OCR sites hold `self.ocr_model()`'s mutable borrow.
1550        let ocr_scale = page_ocr_scale(self.ocr_scale, page.width, page.height, page.scale);
1551        let mut ocr_view: Option<image::RgbImage> = None;
1552        if self.force_full_page_ocr {
1553            page.cells.clear();
1554            page.code_cells.clear();
1555            page.word_cells.clear();
1556        }
1557        // Quant-robustness guard: the default int8 layout graph keeps its
1558        // confidences near the 0.5 label thresholds, and a different CPU's
1559        // quantized kernels can flip a whole page's detections under them —
1560        // tables and paragraphs then dissolve into orphan one-liners while the
1561        // same build converts the page perfectly elsewhere. When a dense
1562        // digital page ends up with detections covering almost none of its
1563        // text cells, re-run that one page on the fp32 graph (lazy-loaded,
1564        // auto-int8 selection only) and keep whichever detections cover more.
1565        let mut regions = regions;
1566        if !page.cells.is_empty() {
1567            let thresholded = |rs: &[layout::Region]| -> Vec<layout::Region> {
1568                rs.iter()
1569                    .filter(|r| r.score >= layout::label_threshold(r.label))
1570                    .cloned()
1571                    .collect()
1572            };
1573            let text_cells = page
1574                .cells
1575                .iter()
1576                .filter(|c| !c.text.trim().is_empty())
1577                .count();
1578            let cov = assemble::layout_cell_coverage(&thresholded(&regions), &page.cells);
1579            if text_cells >= 15 && cov < 0.5 {
1580                let retry = self
1581                    .layout
1582                    .as_mut()
1583                    .expect("layout model loaded unless no_ocr")
1584                    .predict_fp32_fallback(layout_src(page), page.width, page.height)
1585                    .map_err(|e| PdfError::Layout(format!("page {}: {e}", n + 1)))?;
1586                if let Some(retry) = retry {
1587                    let cov2 = assemble::layout_cell_coverage(&thresholded(&retry), &page.cells);
1588                    if cov2 > cov {
1589                        debug_log!(
1590                            "docling-pdf: page {}: int8 layout covered {:.0}% of the text \
1591                             cells; the fp32 retry covers {:.0}% — using it",
1592                            n + 1,
1593                            cov * 100.0,
1594                            cov2 * 100.0
1595                        );
1596                        regions = retry;
1597                    }
1598                }
1599            }
1600        }
1601        // docling's LayoutPostprocessor drops each detection below its label's
1602        // confidence threshold (stricter than the 0.3 base the predictor keeps),
1603        // before any overlap resolution. This removes the low-confidence tables /
1604        // pictures / list-items that otherwise double-emit or mis-classify.
1605        if env::flag("DOCLING_RS_DEBUG_REGIONS") {
1606            for r in &regions {
1607                eprintln!(
1608                    "DBG raw {} {:.2} [{:.0},{:.0},{:.0},{:.0}]",
1609                    r.label, r.score, r.l, r.t, r.r, r.b
1610                );
1611            }
1612        }
1613        regions.retain(|r| r.score >= layout::label_threshold(r.label));
1614        // docling's full-page picture filter and same-label picture dedup run
1615        // on the thresholded detections, before overlap resolution: a picture
1616        // that is the whole page goes (its text reads out as text), and a
1617        // figure proposed both whole and as sub-panels collapses to one box
1618        // (see `dedup_pictures`).
1619        assemble::drop_full_page_pictures(&mut regions, page.width, page.height);
1620        assemble::dedup_pictures(&mut regions);
1621        // Resolve overlapping detections once, before OCR.
1622        let mut regions = assemble::resolve(regions);
1623        // Emit text the detector missed as orphan text regions (docling parity).
1624        assemble::add_orphan_regions(&mut regions, &page.cells);
1625        // Drop phantom empty low-confidence picture boxes (docling parity).
1626        assemble::drop_false_pictures(&mut regions, &page.cells, page.width, page.height);
1627        // A regular region fully inside a surviving table/index/picture is that
1628        // special's child (a cell / in-figure label), not a separate block —
1629        // remove it so it isn't emitted twice (docling parity).
1630        assemble::drop_contained_regulars(&mut regions);
1631        // A one-line paragraph in the bottom margin under a body-less heading
1632        // is that heading's text, not furniture (deliberate deviation).
1633        assemble::reclaim_heading_body_footers(&mut regions, page.width);
1634        // No text layer → recognise text from the page image via OCR.
1635        let ocred = page.cells.is_empty();
1636        // Lines the text detector found outside every layout region (#429).
1637        let mut det_cells: Vec<pdfium_backend::TextCell> = Vec::new();
1638        if ocred {
1639            // Region-scoped OCR recognizes each region's crop, so two regular
1640            // regions over the same ink would read it twice — a low-score
1641            // paragraph box over the high-score line boxes `greedy` keeps.
1642            // Collapse such groups to one region first; a digital
1643            // page resolves the same overlap through cell ownership in
1644            // `fit_regions_to_cells` and needs nothing here.
1645            assemble::merge_overlapping_regulars(&mut regions);
1646            // The text detector's lines (#429), fetched before the region
1647            // pass: with `det` line mode (#570, the default) they are the
1648            // recognizer's crops inside the regions too — RapidOCR's own line
1649            // source — not only the supplement outside them. Usually computed
1650            // already, alongside layout (see `detect_alongside`); a page that
1651            // reached OCR another way detects here. Degradation over failure:
1652            // a detector that loaded but cannot run (a damaged file, an
1653            // allocation failure) costs the page its detected lines, not its
1654            // conversion — the projection segmentation is complete on its own.
1655            let detected: Vec<ocr_det::DetBox> = if self.ocr_model()?.is_some() {
1656                let (img, _) = ocr_input(&mut ocr_view, &page.image, page.scale, ocr_scale);
1657                match self.pending_det.remove(&n) {
1658                    Some(result) => result,
1659                    None => match self.det_model() {
1660                        Some(det) => timing::timed("ocr.det", || det.detect(img)),
1661                        None => Ok(Vec::new()),
1662                    },
1663                }
1664                .unwrap_or_else(|e| {
1665                    docling_core::debug_log!(
1666                        "docling-pdf: page {}: text detection failed ({e}); keeping the \
1667                         region-scoped OCR only",
1668                        n + 1
1669                    );
1670                    Vec::new()
1671                })
1672            } else {
1673                Vec::new()
1674            };
1675            docling_core::debug_log!(
1676                "docling-pdf: page {}: text detector found {} line(s): {:?}",
1677                n + 1,
1678                detected.len(),
1679                detected
1680                    .iter()
1681                    .map(|d| (
1682                        d.l.round(),
1683                        d.t.round(),
1684                        d.r.round(),
1685                        d.b.round(),
1686                        (d.score * 100.0).round() / 100.0
1687                    ))
1688                    .collect::<Vec<_>>()
1689            );
1690            let det_lines =
1691                (ocr::det_lines() && !detected.is_empty()).then_some(detected.as_slice());
1692            // `None` = `skip_ocr` or a missing model (#244): the page keeps
1693            // its layout regions (and TableFormer structure below) with no
1694            // recognized text, instead of failing the conversion.
1695            if let Some(ocr) = self.ocr_model()? {
1696                let (img, scl) = ocr_input(&mut ocr_view, &page.image, page.scale, ocr_scale);
1697                let cells = timing::timed("ocr.page", || {
1698                    ocr.ocr_page_with(img, &regions, scl, det_lines)
1699                })
1700                .map_err(|e| PdfError::Ocr(format!("page {}: {e}", n + 1)))?;
1701                ocr_confs.extend(cells.iter().map(|(_, conf)| conf));
1702                page.cells = cells.into_iter().map(|(cell, _)| cell).collect();
1703                // Table interiors carry no words yet: region-scoped OCR skips
1704                // table labels, and a scanned page has no pdfium text layer — so
1705                // TableFormer's cell matcher got an empty word list and the table
1706                // dissolved (#173). Recognize the table regions' word crops
1707                // (mirroring the browser scanned path): `word_cells` feeds the
1708                // matcher, and the same cells join `cells` so the geometric
1709                // fallback and the table's region text see them too.
1710                if regions.iter().any(|r| assemble::is_table_like(r.label)) {
1711                    let (img, scl) = ocr_input(&mut ocr_view, &page.image, page.scale, ocr_scale);
1712                    let words = timing::timed("ocr.table_words", || {
1713                        ocr.ocr_table_words(img, &regions, scl, det_lines)
1714                    })
1715                    .map_err(|e| PdfError::Ocr(format!("page {}: {e}", n + 1)))?;
1716                    ocr_confs.extend(words.iter().map(|(_, conf)| conf));
1717                    let words: Vec<_> = words.into_iter().map(|(cell, _)| cell).collect();
1718                    page.cells.extend(words.iter().cloned());
1719                    page.word_cells = words;
1720                }
1721            }
1722            // docling's OCR engines detect text lines over the whole bitmap
1723            // and every line becomes a cell, so text the layout model gave no
1724            // region — a diagram's labels, a stamp, a margin note — still
1725            // reads out as orphan text. Region-scoped recognition above stays
1726            // the source inside layout regions; the detector (#429) adds only
1727            // the lines no recognized cell already covers, and the orphan pass
1728            // below places them (those inside a kept picture or table become
1729            // that special's silent children, as upstream).
1730            if self.ocr_model()?.is_some() {
1731                let (img, scl) = ocr_input(&mut ocr_view, &page.image, page.scale, ocr_scale);
1732                let uncovered = ocr_det::uncovered_lines(&detected, scl, &regions, &page.cells);
1733                docling_core::debug_log!(
1734                    "docling-pdf: page {}: {} of {} detected line(s) not covered by the region pass",
1735                    n + 1,
1736                    uncovered.len(),
1737                    detected.len()
1738                );
1739                if let (false, Some(ocr)) = (uncovered.is_empty(), self.ocr_model()?) {
1740                    let scored =
1741                        timing::timed("ocr.det_lines", || ocr.ocr_page(img, &uncovered, scl))
1742                            .map_err(|e| PdfError::Ocr(format!("page {}: {e}", n + 1)))?;
1743                    ocr_confs.extend(scored.iter().map(|(_, conf)| conf));
1744                    det_cells = scored.into_iter().map(|(cell, _)| cell).collect();
1745                    page.cells.extend(det_cells.iter().cloned());
1746                }
1747            }
1748        }
1749        // Region-scoped OCR skips `picture` interiors, and a digital page's
1750        // text layer cannot see into an embedded raster either — so a figure
1751        // that is really a text box (terms-and-conditions exported as an
1752        // image) lost its words on every page kind. Python docling OCRs the
1753        // bitmap-covered areas of *every* page — even digital ones — once they
1754        // exceed `bitmap_area_threshold` (5 % of the page); the browser paths
1755        // already do. Recognize the big text-less crops here too; the panel
1756        // demotion / orphan recovery below place the lines.
1757        let mut pic_cells: Vec<pdfium_backend::TextCell> = Vec::new();
1758        {
1759            let page_area = (page.width * page.height).max(1.0);
1760            let has_text = |r: &layout::Region| {
1761                page.cells.iter().any(|c| {
1762                    let ca = ((c.r - c.l) * (c.b - c.t)).max(1.0);
1763                    let ix = (r.r.min(c.r) - r.l.max(c.l)).max(0.0);
1764                    let iy = (r.b.min(c.b) - r.t.max(c.t)).max(0.0);
1765                    !c.text.trim().is_empty() && ix * iy / ca > 0.5
1766                })
1767            };
1768            // A captioned picture can never demote to a text panel (see
1769            // recover_text_panels), and on digital pages its speculative OCR
1770            // would be discarded anyway — don't pay for it.
1771            let captioned = |r: &layout::Region| {
1772                regions.iter().any(|c| {
1773                    c.label == "caption"
1774                        && c.r.min(r.r) - c.l.max(r.l) > 0.0
1775                        && ((c.t >= r.b && c.t - r.b <= 25.0) || (r.t >= c.b && r.t - c.b <= 25.0))
1776                })
1777            };
1778            let bare: Vec<layout::Region> = regions
1779                .iter()
1780                .filter(|r| {
1781                    r.label == "picture"
1782                        && (r.r - r.l) * (r.b - r.t) / page_area >= 0.05
1783                        && !has_text(r)
1784                        && (ocred || !captioned(r))
1785                })
1786                .map(|r| layout::Region {
1787                    label: "text",
1788                    ..r.clone()
1789                })
1790                .collect();
1791            // Speculative OCR (#244): with `skip_ocr` or no model, big bare
1792            // pictures simply stay pictures.
1793            if let (false, Some(ocr)) = (bare.is_empty(), self.ocr_model()?) {
1794                let (img, scl) = ocr_input(&mut ocr_view, &page.image, page.scale, ocr_scale);
1795                let scored = timing::timed("ocr.pictures", || ocr.ocr_page(img, &bare, scl))
1796                    .map_err(|e| PdfError::Ocr(format!("page {}: {e}", n + 1)))?;
1797                // Speculative in-picture OCR counts toward ocr_score only on
1798                // OCR'd pages, where the recognized lines actually join the
1799                // output; on a digital page they may be discarded below.
1800                if ocred {
1801                    ocr_confs.extend(scored.iter().map(|(_, conf)| conf));
1802                }
1803                pic_cells = scored.into_iter().map(|(cell, _)| cell).collect();
1804                page.cells.extend(pic_cells.iter().cloned());
1805            }
1806        }
1807        let cells_before_pic_ocr = page.cells.len() - pic_cells.len();
1808        // A "picture" that is really a colored text panel — dense, wide,
1809        // multi-line — reads out as paragraphs instead of shipping as pixels;
1810        // sparse in-picture text (a chart's labels) keeps the crop and stays
1811        // inside it as the picture's silent children (docling parity, #200).
1812        // `no_text_panels` (#173) opts out entirely for image-extraction
1813        // workflows.
1814        if !self.no_text_panels {
1815            assemble::recover_text_panels(&mut regions, &page.cells);
1816        }
1817        // On an OCR'd page, in-picture text that did NOT demote its picture
1818        // mostly stays silent, exactly as in docling: its postprocess step
1819        // "Remove regular clusters that are included in wrappers" walks
1820        // SPECIAL_TYPES — which includes PICTURE — so an orphan text cluster
1821        // >80 % contained in a kept picture becomes that picture's child and
1822        // never reaches the serializer. Only border-straddlers (≤80 %
1823        // containment) survive as text. Emitting *everything* here used to
1824        // splice a chart's OCR'd axis ticks into the body text right next to
1825        // the image chunk (#200) — so the orphan pass places the recognized
1826        // lines, the containment drop re-runs for the in-table ones, and
1827        // `assemble_page` nests the in-picture ones under their picture
1828        // (JSON-only children, `assemble::picture_parents`).
1829        if ocred && (!pic_cells.is_empty() || !det_cells.is_empty()) {
1830            // Pictures (and wrappers) no longer count as claimers (#165), so
1831            // the plain orphan pass places the recognized lines directly —
1832            // the detector's lines (#429) the same way.
1833            assemble::add_orphan_regions(&mut regions, &pic_cells);
1834            assemble::add_orphan_regions(&mut regions, &det_cells);
1835            assemble::drop_contained_regulars(&mut regions);
1836        } else if !ocred && !pic_cells.is_empty() {
1837            // Digital page, picture kept: its speculative OCR cells must not
1838            // linger in the text-cell set (they were appended at the tail).
1839            let kept: Vec<layout::Region> = regions
1840                .iter()
1841                .filter(|r| r.label == "picture")
1842                .cloned()
1843                .collect();
1844            let tail = page.cells.split_off(cells_before_pic_ocr);
1845            page.cells.extend(tail.into_iter().filter(|c| {
1846                !kept.iter().any(|r| {
1847                    let ca = ((c.r - c.l) * (c.b - c.t)).max(1.0);
1848                    let ix = (r.r.min(c.r) - r.l.max(c.l)).max(0.0);
1849                    let iy = (r.b.min(c.b) - r.t.max(c.t)).max(0.0);
1850                    ix * iy / ca > 0.5
1851                })
1852            }));
1853        }
1854        // A text-less *table* detected inside a picture on a digital page — a
1855        // screenshot of a table (2203's Figure 10) — has no text layer and no
1856        // scanned-path OCR to feed it, so its grid used to serialize empty and
1857        // the whole element vanished. docling OCRs bitmap-covered areas on
1858        // every page kind and its table cluster collects those cells; mirror
1859        // the scanned path for exactly these tables: recognize word crops and
1860        // feed them to the TableFormer matcher and the cell set.
1861        if !ocred {
1862            let has_text = |t: &layout::Region| {
1863                page.cells.iter().any(|c| {
1864                    let ca = ((c.r - c.l) * (c.b - c.t)).max(1.0);
1865                    let ix = (t.r.min(c.r) - t.l.max(c.l)).max(0.0);
1866                    let iy = (t.b.min(c.b) - t.t.max(c.t)).max(0.0);
1867                    !c.text.trim().is_empty() && ix * iy / ca > 0.5
1868                })
1869            };
1870            let in_picture = |t: &layout::Region| {
1871                regions.iter().any(|r| {
1872                    r.label == "picture" && {
1873                        let ta = ((t.r - t.l) * (t.b - t.t)).max(1.0);
1874                        let ix = (r.r.min(t.r) - r.l.max(t.l)).max(0.0);
1875                        let iy = (r.b.min(t.b) - r.t.max(t.t)).max(0.0);
1876                        ix * iy / ta > 0.5
1877                    }
1878                })
1879            };
1880            let pic_tables: Vec<layout::Region> = regions
1881                .iter()
1882                .filter(|t| assemble::is_table_like(t.label) && !has_text(t) && in_picture(t))
1883                .cloned()
1884                .collect();
1885            // Same degradation as above: without OCR the in-picture table
1886            // keeps its structure (TableFormer is geometry-driven) minus text.
1887            if let (false, Some(ocr)) = (pic_tables.is_empty(), self.ocr_model()?) {
1888                let (img, scl) = ocr_input(&mut ocr_view, &page.image, page.scale, ocr_scale);
1889                let words = timing::timed("ocr.table_words", || {
1890                    ocr.ocr_table_words(img, &pic_tables, scl, None)
1891                })
1892                .map_err(|e| PdfError::Ocr(format!("page {}: {e}", n + 1)))?;
1893                ocr_confs.extend(words.iter().map(|(_, conf)| conf));
1894                let words: Vec<_> = words.into_iter().map(|(cell, _)| cell).collect();
1895                page.cells.extend(words.iter().cloned());
1896                page.word_cells.extend(words);
1897            }
1898        }
1899        // The cells are final: fit every regular region to the cells it
1900        // claims and fold the orphans it now surrounds (#419), before
1901        // TableFormer and the reading order see the boxes.
1902        assemble::fit_regions_to_cells(&mut regions, &page.cells);
1903        Ok(Prepared {
1904            regions,
1905            ocr_confs,
1906            parse,
1907        })
1908    }
1909
1910    /// The stages after TableFormer: enrichment, the page confidence report and
1911    /// assembly into typed nodes.
1912    fn complete_page(
1913        &mut self,
1914        n: usize,
1915        page: &mut PdfPage,
1916        prepared: Prepared,
1917        table_rows: Vec<Option<tf_core::TableGrid>>,
1918    ) -> Result<PageOut, PdfError> {
1919        let Prepared {
1920            regions,
1921            ocr_confs,
1922            parse,
1923        } = prepared;
1924        if env::flag("DOCLING_RS_DEBUG_REGIONS") {
1925            for (i, r) in regions.iter().enumerate() {
1926                eprintln!(
1927                    "DBG final {} {:.2} [{:.0},{:.0},{:.0},{:.0}] rows={:?}",
1928                    r.label,
1929                    r.score,
1930                    r.l,
1931                    r.t,
1932                    r.r,
1933                    r.b,
1934                    table_rows[i]
1935                        .as_ref()
1936                        .map(|t| (t.rows.len(), t.rows.first().map(|r| r.len())))
1937                );
1938            }
1939            eprintln!(
1940                "DBG cells={} words={}",
1941                page.cells.len(),
1942                page.word_cells.len()
1943            );
1944        }
1945        // Enrichment passes (opt-in): DocumentPictureClassifier over picture
1946        // regions, CodeFormulaV2 over code/formula regions. Same shared-slot
1947        // shape as TableFormer — one lazily-loaded instance per pipeline, only
1948        // ever locked when a page actually has a matching region.
1949        let mut enrich_out: Vec<Option<assemble::Enrichment>> = vec![None; regions.len()];
1950        if let Some(slot) = self.classifier.as_ref() {
1951            if regions.iter().any(|r| r.label == "picture") {
1952                timing::timed("picture_classifier", || {
1953                    let mut guard = slot.lock().unwrap();
1954                    if matches!(*guard, EnrichSlot::Unloaded) {
1955                        *guard = match enrich::PictureClassifier::load_with(intra_threads()) {
1956                            Some(m) => EnrichSlot::Ready(m),
1957                            None => EnrichSlot::Missing,
1958                        };
1959                    }
1960                    if let EnrichSlot::Ready(model) = &mut *guard {
1961                        for (i, r) in regions.iter().enumerate() {
1962                            if r.label != "picture" {
1963                                continue;
1964                            }
1965                            let Some(crop) = assemble::crop_region_scaled(
1966                                page,
1967                                [r.l, r.t, r.r, r.b],
1968                                enrich::CLASSIFIER_SCALE,
1969                            ) else {
1970                                continue;
1971                            };
1972                            match model.classify(&crop) {
1973                                Ok(classes) => {
1974                                    enrich_out[i] =
1975                                        Some(assemble::Enrichment::PictureClasses(classes));
1976                                }
1977                                Err(e) => eprintln!("docling-pdf: page {}: {e}", n + 1),
1978                            }
1979                        }
1980                    }
1981                });
1982            }
1983        }
1984        if let Some(slot) = self.code_formula.as_ref() {
1985            let wants = |label: &str| {
1986                (label == "code" && self.enrich.code) || (label == "formula" && self.enrich.formula)
1987            };
1988            if regions.iter().any(|r| wants(r.label)) {
1989                timing::timed("code_formula", || {
1990                    let mut guard = slot.lock().unwrap();
1991                    if matches!(*guard, EnrichSlot::Unloaded) {
1992                        *guard = match enrich::CodeFormula::load_with(intra_threads()) {
1993                            Some(m) => EnrichSlot::Ready(m),
1994                            None => EnrichSlot::Missing,
1995                        };
1996                    }
1997                    if let EnrichSlot::Ready(model) = &mut *guard {
1998                        for (i, r) in regions.iter().enumerate() {
1999                            if !wants(r.label) {
2000                                continue;
2001                            }
2002                            // docling crops the postprocessed cluster box — the
2003                            // union of the region's text cells, not the raw
2004                            // detector box — expanded by 18% per side, at
2005                            // ~120 dpi.
2006                            let [bl, bt, br, bb] = assemble::region_cell_bbox(r, &page.cells)
2007                                .unwrap_or([r.l, r.t, r.r, r.b]);
2008                            let (w, h) = (br - bl, bb - bt);
2009                            let ex = enrich::CODE_FORMULA_EXPANSION;
2010                            let bbox = [bl - w * ex, bt - h * ex, br + w * ex, bb + h * ex];
2011                            let Some(crop) = assemble::crop_region_scaled(
2012                                page,
2013                                bbox,
2014                                enrich::CODE_FORMULA_SCALE,
2015                            ) else {
2016                                continue;
2017                            };
2018                            let kind = if r.label == "code" {
2019                                enrich::CodeFormulaKind::Code
2020                            } else {
2021                                enrich::CodeFormulaKind::Formula
2022                            };
2023                            match model.predict(&crop, kind) {
2024                                Ok(text) => {
2025                                    enrich_out[i] = Some(match kind {
2026                                        enrich::CodeFormulaKind::Code => {
2027                                            let (code, language) =
2028                                                enrich::extract_code_language(&text);
2029                                            assemble::Enrichment::Code {
2030                                                language,
2031                                                text: code,
2032                                            }
2033                                        }
2034                                        enrich::CodeFormulaKind::Formula => {
2035                                            assemble::Enrichment::Formula { latex: text }
2036                                        }
2037                                    });
2038                                }
2039                                Err(e) => eprintln!("docling-pdf: page {}: {e}", n + 1),
2040                            }
2041                        }
2042                    }
2043                });
2044            }
2045        }
2046        // Score the final region set (docling assigns layout_score over the
2047        // postprocessed clusters — the page elements assemble_page emits,
2048        // which leave out the regulars nested in a picture as its children).
2049        let parents = assemble::picture_parents(&regions);
2050        let elements: Vec<layout::Region> = regions
2051            .iter()
2052            .zip(&parents)
2053            .filter(|(_, p)| p.is_none())
2054            .map(|(r, _)| r.clone())
2055            .collect();
2056        let conf = quality::page_confidence(parse, &elements, &ocr_confs);
2057        let (nodes, links) = timing::timed("assemble_page", || {
2058            assemble::assemble_page(page, regions, &table_rows, &enrich_out, self.images.scale)
2059        });
2060        Ok((nodes, links, conf, self.page_image(n, page)))
2061    }
2062}
2063
2064#[cfg(feature = "ml")]
2065/// Per-worker ONNX intra-op threads. The layout model is memory-bandwidth bound,
2066/// so on a typical machine two threads per worker (sharing one in-cache copy of
2067/// the weights) extracts more throughput than one fat model or many single-thread
2068/// workers. `DOCLING_RS_PDF_INTRA` overrides for per-machine tuning.
2069fn pdf_intra() -> usize {
2070    if let Some(n) = env::parse::<usize>("DOCLING_RS_PDF_INTRA").filter(|&n| n > 0) {
2071        return n;
2072    }
2073    if intra_threads() >= 2 {
2074        2
2075    } else {
2076        1
2077    }
2078}
2079
2080#[cfg(feature = "ml")]
2081/// How many page-workers to spin up for a multi-page PDF. `DOCLING_RS_PDF_WORKERS`
2082/// overrides; otherwise size the pool so `workers × intra ≈ cores`.
2083///
2084/// The pool scales with the machine (#324 follow-up testing): the old hard cap
2085/// of 4 left most of a many-core box idle — on a 16-core M4 Max, 10 workers
2086/// measured ~1.2× over the capped pool (10.0 → 8.5 s on a 130-page document,
2087/// byte-identical output). The ceiling of 16 is a memory bound, not a
2088/// performance one: each worker holds its own layout/OCR sessions (~0.4 GB),
2089/// so a worst-case pool stays under ~6.5 GB even on a ≥32-core host — and
2090/// docling-serve's per-request pools sit behind its `DOCLING_RS_MAX_MEMORY_MB`
2091/// admission control besides. Machines with 4 or fewer effective threads keep
2092/// the exact old sizing (`threads / intra`, min 1).
2093fn pdf_worker_count() -> usize {
2094    if let Some(n) = env::parse::<usize>("DOCLING_RS_PDF_WORKERS").filter(|&n| n > 0) {
2095        return n;
2096    }
2097    (intra_threads() / pdf_intra()).clamp(1, 16)
2098}
2099
2100#[cfg(feature = "ml")]
2101/// Max pages a worker layout-detects with one batched inference call (issue
2102/// #73). Workers drain the work channel opportunistically up to this size —
2103/// whatever is already rendered gets batched, so batching never *waits* for
2104/// pages and adds no latency when rendering is the bottleneck.
2105///
2106/// Default: per-page (1) on the CPU provider, 4 when a CUDA-class GPU
2107/// provider is selected (#338) — CoreML stays per-page (#602,
2108/// [`docling_onnx::prefers_batching`]): only the pinned batch=1 graph is
2109/// static, and static partitions are all CoreML is allowed to take. The old
2110/// "4 on 8+ cores" CPU default was a hypothesis — that single-session
2111/// amortization pays off with a wider thread budget —
2112/// and every actual CPU measurement lands the other way: a 4-core x86 box
2113/// runs the 9-page 2206.01062 fixture in 8.5 s/conv at batch=1 vs 9.3 s at
2114/// batch=4 (re-measured for #338; the original 8.1 vs 9.3 agrees), and the
2115/// issue-#338 report measured batch=1 ~2× faster on a 16-core M4 Max at
2116/// every worker count — batching only adds cache pressure once workers
2117/// saturate the cores. On a GPU the per-call dispatch overhead is real and
2118/// batching amortizes it, so the GPU default stays. Output is bit-identical
2119/// at every batch size, so this is purely a throughput knob.
2120/// `DOCLING_RS_PDF_LAYOUT_BATCH` overrides either way; `1` = per-page.
2121pub(crate) fn pdf_layout_batch() -> usize {
2122    env::parse::<usize>("DOCLING_RS_PDF_LAYOUT_BATCH")
2123        .filter(|&n| n > 0)
2124        .unwrap_or_else(|| {
2125            if docling_onnx::prefers_batching() {
2126                4
2127            } else {
2128                1
2129            }
2130        })
2131}
2132
2133#[cfg(feature = "ml")]
2134/// Minimum page count before a PDF is worth the parallel worker pool. Below this,
2135/// the serial primary (running its model on every core) is faster than fanning out
2136/// — the helper pool's one-time model-load cost only pays off once enough pages
2137/// share it. `DOCLING_RS_PDF_PARALLEL_MIN` overrides.
2138fn pdf_parallel_min() -> usize {
2139    env::parse::<usize>("DOCLING_RS_PDF_PARALLEL_MIN")
2140        .filter(|&n| n > 0)
2141        .unwrap_or(6)
2142}
2143
2144#[cfg(feature = "ml")]
2145/// A reusable PDF pipeline. The **primary** worker runs its models on every core,
2146/// so a single-page / small / image / METS input is converted at full intra-op
2147/// speed with no pool to load. A document with enough pages instead fans out
2148/// across a **pool** of narrower workers processed concurrently. Both load lazily
2149/// and are cached for reuse, so a one-shot conversion only pays for what it uses.
2150pub struct Pipeline {
2151    /// Full-intra worker for the serial path; loaded on first serial use.
2152    primary: Option<Worker>,
2153    /// Narrower workers (≈cores/`target_workers` threads each) for the parallel
2154    /// path; loaded on first multi-page use and cached.
2155    pool: Vec<Worker>,
2156    /// The single TableFormer instance every worker shares (see [`TfSlot`]).
2157    tables: SharedTables,
2158    /// The shared enrichment-model slots (same pattern as [`TfSlot`]).
2159    classifier: SharedClassifier,
2160    code_formula: SharedCodeFormula,
2161    /// Desired pool size for multi-page documents.
2162    target_workers: usize,
2163    /// Page count at/above which the parallel pool is worth its load cost.
2164    parallel_min: usize,
2165    /// Skip loading/running TableFormer; table regions fall back to geometric
2166    /// reconstruction. See [`Pipeline::no_table_former`].
2167    no_table_former: bool,
2168    /// Skip layout, OCR, and TableFormer entirely. See [`Pipeline::no_ocr`].
2169    no_ocr: bool,
2170    /// Keep layout + TableFormer, never OCR (#244). See [`Pipeline::skip_ocr`].
2171    skip_ocr: bool,
2172    /// OCR every page even when it carries a text layer. See
2173    /// [`Pipeline::force_full_page_ocr`].
2174    force_full_page_ocr: bool,
2175    /// Never demote text-panel pictures. See [`Pipeline::no_text_panels`].
2176    no_text_panels: bool,
2177    /// Opt-in enrichment passes. See [`Pipeline::enrichments`].
2178    enrich: EnrichmentOptions,
2179    /// 1-based inclusive page window to convert. See [`Pipeline::pages`].
2180    page_range: Option<(usize, usize)>,
2181    /// OCR recognition language. See [`Pipeline::ocr_lang`].
2182    ocr_lang: ocr::OcrLang,
2183    /// Which OCR engine runs (#460). See [`Pipeline::ocr_engine`].
2184    ocr_engine: ocr::OcrEngine,
2185    /// Tesseract's languages (#460). See [`Pipeline::tesseract_lang`].
2186    tesseract_lang: Option<String>,
2187    /// Which regions feed the OCR (#254). See [`Pipeline::ocr_mode`].
2188    ocr_mode: ocr::OcrMode,
2189    /// OCR render scale override in px/pt (#254). See [`Pipeline::ocr_scale`].
2190    ocr_scale: Option<f32>,
2191    /// Picture-crop scale and page images (#519/#520). See
2192    /// [`Pipeline::images_scale`] / [`Pipeline::generate_page_images`].
2193    images: ImageOutput,
2194    /// Heading-level inference (#302). See [`Pipeline::heading_hierarchy`].
2195    heading_hierarchy: HeadingHierarchyOptions,
2196    /// Optional per-page progress hook `(done, selected_total)`, invoked after
2197    /// each page finishes on both the serial and parallel buffered paths. Set
2198    /// by the CLI batch mode for dot-progress; `None` costs nothing.
2199    progress: Option<Arc<dyn Fn(usize, usize) + Send + Sync>>,
2200    /// Per-document wall-clock budget (docling's `document_timeout`, #497).
2201    /// See [`Pipeline::document_timeout`].
2202    document_timeout: Option<std::time::Duration>,
2203}
2204
2205#[cfg(feature = "ml")]
2206impl Pipeline {
2207    /// Construct the pipeline. Models load lazily on first use (full-intra primary
2208    /// for serial inputs, the helper pool for multi-page PDFs), so nothing is
2209    /// loaded that a given document doesn't need.
2210    pub fn new() -> Result<Self, PdfError> {
2211        Ok(Self {
2212            primary: None,
2213            pool: Vec::new(),
2214            tables: Arc::new(Mutex::new(TfSlot::Unloaded)),
2215            classifier: Arc::new(Mutex::new(EnrichSlot::Unloaded)),
2216            code_formula: Arc::new(Mutex::new(EnrichSlot::Unloaded)),
2217            target_workers: pdf_worker_count(),
2218            parallel_min: pdf_parallel_min(),
2219            no_table_former: false,
2220            no_ocr: false,
2221            skip_ocr: false,
2222            force_full_page_ocr: false,
2223            no_text_panels: false,
2224            enrich: EnrichmentOptions::default(),
2225            page_range: None,
2226            ocr_lang: ocr::OcrLang::from_env(),
2227            ocr_engine: ocr::OcrEngine::from_env(),
2228            tesseract_lang: None,
2229            ocr_mode: ocr::OcrMode::from_env(),
2230            ocr_scale: ocr::scale_from_env(),
2231            images: ImageOutput::default(),
2232            heading_hierarchy: HeadingHierarchyOptions::default(),
2233            progress: None,
2234            document_timeout: None,
2235        })
2236    }
2237
2238    /// A wall-clock budget for each document (docling's
2239    /// `PipelineOptions.document_timeout`, #497; `None` = unlimited, the
2240    /// default). The budget starts when a conversion starts and is checked
2241    /// cooperatively between pages: once it is spent, no further page is
2242    /// rendered or processed, the pages already finished are assembled into
2243    /// the document, and the outcome says so (`Completion::TimedOut`, which
2244    /// the converter reports as `PartialSuccess` with a timeout error, the
2245    /// way docling does). A page in flight finishes — the check costs
2246    /// nothing inside a page, and a single-page document (an image) is never
2247    /// cut. For a long-lived pipeline use
2248    /// [`set_document_timeout`](Self::set_document_timeout) before each
2249    /// conversion.
2250    pub fn document_timeout(mut self, timeout: Option<std::time::Duration>) -> Self {
2251        self.document_timeout = timeout;
2252        self
2253    }
2254
2255    /// In-place variant of [`document_timeout`](Self::document_timeout) for a
2256    /// warm pipeline — set it before every conversion so no request inherits
2257    /// a previous one's budget.
2258    pub fn set_document_timeout(&mut self, timeout: Option<std::time::Duration>) {
2259        self.document_timeout = timeout;
2260    }
2261
2262    /// The deadline of a conversion starting now, from the configured budget.
2263    fn deadline(&self) -> Option<std::time::Instant> {
2264        self.document_timeout.map(|t| std::time::Instant::now() + t)
2265    }
2266
2267    /// The [`Completion`] of a walk that processed `done` of `selected` pages.
2268    fn completion(&self, timed_out: bool, done: usize, selected: usize) -> Completion {
2269        match (timed_out, self.document_timeout) {
2270            (true, Some(budget)) => Completion::TimedOut {
2271                pages_done: done,
2272                pages_selected: selected,
2273                budget,
2274            },
2275            _ => Completion::Complete,
2276        }
2277    }
2278
2279    /// Infer section-header levels after assembly (#302, docling's
2280    /// `HeadingHierarchyModel`): PDF bookmarks > legal/outline numbering >
2281    /// font style, off by default — see [`HeadingHierarchyOptions`]. Pure
2282    /// post-processing configuration; for a warm pipeline use
2283    /// [`set_heading_hierarchy`](Self::set_heading_hierarchy).
2284    pub fn heading_hierarchy(mut self, opts: HeadingHierarchyOptions) -> Self {
2285        self.heading_hierarchy = opts;
2286        self
2287    }
2288
2289    /// In-place variant of [`heading_hierarchy`](Self::heading_hierarchy) for
2290    /// a long-lived pipeline (docling-serve's warm instance) — like
2291    /// [`set_pages`](Self::set_pages), set it before every conversion so no
2292    /// request inherits a previous one's choice.
2293    pub fn set_heading_hierarchy(&mut self, opts: HeadingHierarchyOptions) {
2294        self.heading_hierarchy = opts;
2295    }
2296
2297    /// Run the enabled heading-hierarchy stage (#302) on an assembled
2298    /// document: gather the outline (bookmarks) and the per-page glyph styles
2299    /// on demand, then assign levels in place. `bytes` is `None` on paths
2300    /// with no PDF behind them (standalone images, METS) — those degrade to
2301    /// the numbering signal, exactly like docling without parsed pages.
2302    fn apply_heading_hierarchy(&self, nodes: &mut [Node], bytes: Option<&[u8]>) {
2303        let opts = &self.heading_hierarchy;
2304        if !opts.enabled {
2305            return;
2306        }
2307        let outline = match bytes {
2308            Some(bytes) if opts.use_bookmarks => outline::extract_outline(bytes),
2309            _ => Vec::new(),
2310        };
2311        let styles = match bytes {
2312            Some(bytes) if opts.use_style => {
2313                let pages = heading_hierarchy::heading_pages(nodes);
2314                textparse::glyph_styles(bytes, &pages)
2315            }
2316            _ => Default::default(),
2317        };
2318        heading_hierarchy::apply(nodes, &outline, &styles, opts);
2319    }
2320
2321    /// Install (or clear) the per-page progress hook: called with
2322    /// `(pages_done, pages_selected)` after each page completes during
2323    /// [`convert`](Self::convert). Shared with the parallel workers, so the
2324    /// callback must be cheap and thread-safe.
2325    pub fn set_progress(&mut self, cb: Option<Arc<dyn Fn(usize, usize) + Send + Sync>>) {
2326        self.progress = cb;
2327    }
2328
2329    /// Convert only pages `first..=last` (**1-based**, like the page numbers a
2330    /// PDF viewer shows — issue #80's `--pages A-B`). Out-of-range pages are
2331    /// skipped before rasterization, so the cost is proportional to the window,
2332    /// not the document. `last` past the end of the document clamps; a window
2333    /// that selects no pages at all (`first` beyond the last page) is an error
2334    /// at convert time. `None` (the default) converts everything.
2335    pub fn pages(mut self, range: Option<(usize, usize)>) -> Self {
2336        self.page_range = range;
2337        self
2338    }
2339
2340    /// In-place variant of [`pages`](Self::pages) for a long-lived pipeline
2341    /// (e.g. docling-serve's warm instance) that applies a per-request window
2342    /// without rebuilding — unlike the model switches, the window is pure
2343    /// configuration. Set it before every conversion; it stays until changed.
2344    pub fn set_pages(&mut self, range: Option<(usize, usize)>) {
2345        self.page_range = range;
2346    }
2347
2348    /// OCR recognition language (see [`OcrLang`]): English by default, `ch`
2349    /// for the multilingual docling-conformance model. `None` keeps the
2350    /// process default (`DOCLING_RS_OCR_LANG`, else English). Set before the
2351    /// first conversion; for a warm pipeline use
2352    /// [`set_ocr_lang`](Self::set_ocr_lang).
2353    pub fn ocr_lang(mut self, lang: Option<ocr::OcrLang>) -> Self {
2354        self.set_ocr_lang(lang);
2355        self
2356    }
2357
2358    /// In-place variant of [`ocr_lang`](Self::ocr_lang) for a long-lived
2359    /// pipeline (docling-serve's warm instance). Unlike the page window this
2360    /// is a *model* switch: any worker whose cached recognition model was
2361    /// loaded for a different language drops it, to be lazily reloaded on the
2362    /// next OCR-needing page (cheap — the rec models are ~10 MB).
2363    pub fn set_ocr_lang(&mut self, lang: Option<ocr::OcrLang>) {
2364        let lang = lang.unwrap_or_else(ocr::OcrLang::from_env);
2365        self.ocr_lang = lang;
2366        for worker in self.primary.iter_mut().chain(self.pool.iter_mut()) {
2367            if worker.ocr_lang != lang {
2368                worker.ocr_lang = lang;
2369                worker.ocr = OcrSlot::Unloaded;
2370            }
2371        }
2372    }
2373
2374    /// Which OCR engine recognizes text (#460, see [`OcrEngine`]): the
2375    /// built-in PP-OCRv3 recognizer by default, or the system `tesseract`
2376    /// binary (docling's `TesseractCliOcrOptions`) — same layout-region
2377    /// crops, same cells; Tesseract does its own line/word segmentation and
2378    /// reads orientation from its OSD. `None` keeps the process default
2379    /// (`DOCLING_RS_OCR_ENGINE`, else PP-OCR). Set before the first
2380    /// conversion; for a warm pipeline use
2381    /// [`set_ocr_engine`](Self::set_ocr_engine).
2382    pub fn ocr_engine(mut self, engine: Option<ocr::OcrEngine>) -> Self {
2383        self.set_ocr_engine(engine);
2384        self
2385    }
2386
2387    /// In-place variant of [`ocr_engine`](Self::ocr_engine): a model switch
2388    /// like [`set_ocr_lang`](Self::set_ocr_lang) — workers holding the other
2389    /// engine drop it, to be lazily reloaded.
2390    pub fn set_ocr_engine(&mut self, engine: Option<ocr::OcrEngine>) {
2391        let engine = engine.unwrap_or_else(ocr::OcrEngine::from_env);
2392        self.ocr_engine = engine;
2393        for worker in self.primary.iter_mut().chain(self.pool.iter_mut()) {
2394            if worker.ocr_engine != engine {
2395                worker.ocr_engine = engine;
2396                worker.ocr = OcrSlot::Unloaded;
2397            }
2398        }
2399    }
2400
2401    /// Tesseract's languages (#460): the `-l` argument, tessdata stems joined
2402    /// with `+` — build it from an `ocr_lang` value with
2403    /// [`tesseract_lang_arg`], which also maps BCP-47 tags and the PP-OCR
2404    /// codes. `None` runs Tesseract's default (`eng`). Ignored under the
2405    /// PP-OCR engine, whose language is [`ocr_lang`](Self::ocr_lang). For a
2406    /// warm pipeline use [`set_tesseract_lang`](Self::set_tesseract_lang).
2407    pub fn tesseract_lang(mut self, lang: Option<String>) -> Self {
2408        self.set_tesseract_lang(lang);
2409        self
2410    }
2411
2412    /// In-place variant of [`tesseract_lang`](Self::tesseract_lang): a model
2413    /// switch like [`set_ocr_lang`](Self::set_ocr_lang) — a worker whose
2414    /// Tesseract was probed for other languages drops it.
2415    pub fn set_tesseract_lang(&mut self, lang: Option<String>) {
2416        self.tesseract_lang = lang.clone();
2417        for worker in self.primary.iter_mut().chain(self.pool.iter_mut()) {
2418            if worker.tesseract_lang != lang {
2419                worker.tesseract_lang = lang.clone();
2420                if worker.ocr_engine == ocr::OcrEngine::Tesseract {
2421                    worker.ocr = OcrSlot::Unloaded;
2422                }
2423            }
2424        }
2425    }
2426
2427    /// Resolve the configured 1-based window against a page count into the
2428    /// 0-based inclusive form the backend walks, validating it selects at
2429    /// least one existing page.
2430    fn resolve_range(&self, total: usize) -> Result<Option<(usize, usize)>, PdfError> {
2431        let Some((first, last)) = self.page_range else {
2432            return Ok(None);
2433        };
2434        if first == 0 || last < first {
2435            return Err(PdfError::Pdfium(format!(
2436                "invalid page range {first}-{last} (pages are 1-based, first <= last)"
2437            )));
2438        }
2439        if first > total {
2440            return Err(PdfError::Pdfium(format!(
2441                "page range {first}-{last} is outside the document ({total} page(s))"
2442            )));
2443        }
2444        Ok(Some((first - 1, last.min(total) - 1)))
2445    }
2446
2447    /// Enable the opt-in enrichment passes (docling's
2448    /// `do_picture_classification` / `do_code_enrichment` /
2449    /// `do_formula_enrichment`). Each enabled pass lazily loads its model on
2450    /// the first matching region; a missing model warns once and is skipped.
2451    /// Set before the first conversion (no effect on already-loaded workers).
2452    pub fn enrichments(mut self, opts: EnrichmentOptions) -> Self {
2453        self.enrich = opts;
2454        self
2455    }
2456
2457    /// Skip loading and running the TableFormer table-structure model. Table
2458    /// regions still get emitted, but reconstructed geometrically from cell
2459    /// positions instead of via the ONNX model's predicted structure — faster
2460    /// (no model load, no per-table inference) at the cost of table fidelity.
2461    /// No effect if a worker is already loaded; set this before the first
2462    /// conversion.
2463    pub fn no_table_former(mut self, disable: bool) -> Self {
2464        self.no_table_former = disable;
2465        self
2466    }
2467
2468    /// Keep every detected `picture` region as a picture. By default an
2469    /// *uncaptioned* picture that reads like a dense, uniform text panel (a
2470    /// terms-and-conditions box exported as an image) is demoted into
2471    /// paragraphs (#157); a chart the layout mislabels can still trip that
2472    /// heuristic on scanned pages, and image-extraction workflows may simply
2473    /// want every crop — this flag disables the demotion entirely (#173).
2474    /// No effect on already-loaded workers; set before the first conversion.
2475    pub fn no_text_panels(mut self, disable: bool) -> Self {
2476        self.no_text_panels = disable;
2477        self
2478    }
2479
2480    /// Skip layout detection, OCR, and TableFormer entirely — no model load, no
2481    /// inference of any kind. The PDF's embedded text cells are grouped by line
2482    /// and emitted as plain paragraphs in reading order: no headings, lists,
2483    /// tables, code blocks, or pictures, since that structure comes from the
2484    /// layout model. The fastest possible PDF path, but pages with no embedded
2485    /// text layer (scanned/image-only PDFs) yield no text at all — convert those
2486    /// without this flag. Implies `no_table_former`. No effect if a worker is
2487    /// already loaded; set this before the first conversion.
2488    pub fn no_ocr(mut self, disable: bool) -> Self {
2489        self.no_ocr = disable;
2490        self
2491    }
2492
2493    /// Never run OCR, but keep layout detection and TableFormer — docling's
2494    /// independent `do_ocr=False` (#244), the counterpart of
2495    /// [`no_table_former`](Self::no_table_former). Unlike
2496    /// [`no_ocr`](Self::no_ocr) (which skips the whole ML stack), structured
2497    /// output — headings, tables, pictures, reading order — is preserved;
2498    /// only text that exists solely as pixels is lost: scanned pages come
2499    /// back with their regions empty, and the speculative OCR of large
2500    /// embedded images never runs. The OCR model is never loaded. Ignored
2501    /// when `no_ocr` is set (there is no OCR to skip);
2502    /// takes precedence over [`force_full_page_ocr`](Self::force_full_page_ocr),
2503    /// mirroring docling where forcing is a sub-option of `do_ocr`.
2504    pub fn skip_ocr(mut self, disable: bool) -> Self {
2505        self.skip_ocr = disable;
2506        self
2507    }
2508
2509    /// OCR every page from its rendered image even when the page carries an
2510    /// embedded text layer — docling's `force_full_page_ocr`. The escape hatch
2511    /// for text layers that exist but lie: broken encodings, subset fonts with
2512    /// garbage mappings, a scanned form with a few typed-in fields. Ignored
2513    /// when [`no_ocr`](Self::no_ocr) is set, mirroring docling (there
2514    /// `force_full_page_ocr` is a sub-option of `do_ocr`).
2515    pub fn force_full_page_ocr(mut self, force: bool) -> Self {
2516        self.force_full_page_ocr = force;
2517        self
2518    }
2519
2520    /// Which document regions feed the OCR — docling's `OcrMode` (#254). The
2521    /// default (`default` = `pdf_aware_layout_regions`) is the standard
2522    /// text-layer-aware behavior; `full_page`/`layout_regions` discard the
2523    /// text layer like [`force_full_page_ocr`](Self::force_full_page_ocr)
2524    /// (see [`ocr::OcrMode`] for why both map onto it). Whichever of the flag
2525    /// and the mode demands forcing wins, mirroring docling's
2526    /// `force_full_page_ocr` → `mode=full_page` bridge. `None` keeps the
2527    /// process default (`DOCLING_RS_OCR_MODE`, else `default`).
2528    pub fn ocr_mode(mut self, mode: Option<ocr::OcrMode>) -> Self {
2529        self.ocr_mode = mode.unwrap_or_else(ocr::OcrMode::from_env);
2530        self
2531    }
2532
2533    /// In-place variants of [`force_full_page_ocr`](Self::force_full_page_ocr),
2534    /// [`ocr_mode`](Self::ocr_mode) and [`ocr_scale`](Self::ocr_scale) for a
2535    /// long-lived pipeline (docling-serve's warm instance): all three are pure
2536    /// per-worker configuration — no model reloads — so they apply per request
2537    /// like [`set_pages`](Self::set_pages). Set them before every conversion so
2538    /// no request inherits a previous one's choice.
2539    pub fn set_force_full_page_ocr(&mut self, force: bool) {
2540        self.force_full_page_ocr = force;
2541        self.sync_ocr_config();
2542    }
2543
2544    /// See [`set_force_full_page_ocr`](Self::set_force_full_page_ocr).
2545    pub fn set_ocr_mode(&mut self, mode: Option<ocr::OcrMode>) {
2546        self.ocr_mode = mode.unwrap_or_else(ocr::OcrMode::from_env);
2547        self.sync_ocr_config();
2548    }
2549
2550    /// See [`set_force_full_page_ocr`](Self::set_force_full_page_ocr).
2551    pub fn set_ocr_scale(&mut self, scale: Option<f32>) {
2552        self.ocr_scale = scale
2553            .filter(|s| s.is_finite() && *s > 0.0)
2554            .or_else(ocr::scale_from_env);
2555        self.sync_ocr_config();
2556    }
2557
2558    /// Whether page extraction should decode the text layer at all. Forced
2559    /// full-page OCR (the flag or `ocr_mode=full_page|layout_regions`) clears
2560    /// every extracted cell unread, so the decode is skipped outright —
2561    /// docling#4061's `skip_cell_extraction` (2.122). `no_ocr` wins over the
2562    /// forcing, as everywhere else: its fast path *is* the text layer.
2563    fn extract_text_layer(&self) -> bool {
2564        self.no_ocr || !(self.force_full_page_ocr || self.ocr_mode.forces_full_page())
2565    }
2566
2567    /// Push the current OCR forcing/scale choice onto already-loaded workers
2568    /// (new workers read it at [`Worker::load`]).
2569    fn sync_ocr_config(&mut self) {
2570        let force = self.force_full_page_ocr || self.ocr_mode.forces_full_page();
2571        let scale = self.ocr_scale;
2572        for worker in self.primary.iter_mut().chain(self.pool.iter_mut()) {
2573            worker.force_full_page_ocr = force;
2574            worker.ocr_scale = scale;
2575        }
2576    }
2577
2578    /// Pixels per PDF point for picture crops and page images — docling's
2579    /// `images_scale` (#519/#520). `None` (the default) delivers crops at the
2580    /// pipeline's own page render, 2.0 px/pt (144 dpi), untouched. Another
2581    /// value resamples that render (CatmullRom, docling's PIL BICUBIC), so
2582    /// values above 2.0 upsample rather than re-render. Each picture's
2583    /// `dpi` (docling-core's `ImageRef.dpi`) is 72·scale, so consumers mapping
2584    /// pixels back to points stay exact. Layout, OCR and TableFormer inputs
2585    /// are unaffected. Non-positive values are ignored.
2586    pub fn images_scale(mut self, scale: Option<f32>) -> Self {
2587        self.set_images(ImageOutput {
2588            scale,
2589            ..self.images
2590        });
2591        self
2592    }
2593
2594    /// Keep each page's render as the document's page image — docling's
2595    /// `generate_page_images` (#520): the JSON export writes it as the page's
2596    /// `image` (docling-core's `PageItem.image`, at [`Self::images_scale`],
2597    /// `dpi` = 72·scale), which is what docling-core's
2598    /// `TableItem.get_image` / `FormulaItem.get_image` crop from. Off by
2599    /// default: a page image is a full-page PNG per page held in memory.
2600    /// Pages converted with `no_ocr` (text layer only) are never rendered and
2601    /// get none; streaming conversions carry no page map and drop them.
2602    pub fn generate_page_images(mut self, enabled: bool) -> Self {
2603        self.set_images(ImageOutput {
2604            page_images: enabled,
2605            ..self.images
2606        });
2607        self
2608    }
2609
2610    /// Both image outputs at once, also on already-loaded workers — the
2611    /// per-request form a warm pipeline (serve, the Node `Pipeline`) uses,
2612    /// like [`set_ocr_scale`](Self::set_ocr_scale). A non-finite or
2613    /// non-positive scale reads as unset.
2614    pub fn set_images(&mut self, images: ImageOutput) {
2615        self.images = ImageOutput {
2616            scale: images.scale.filter(|s| s.is_finite() && *s > 0.0),
2617            ..images
2618        };
2619        let images = self.images;
2620        for worker in self.primary.iter_mut().chain(self.pool.iter_mut()) {
2621            worker.images = images;
2622        }
2623    }
2624
2625    /// OCR render scale in pixels per PDF point — docling's `OcrOptions.scale`
2626    /// (#254, upstream docling#3877; their default 3 = 216 dpi). `None`
2627    /// (default: `DOCLING_RS_OCR_SCALE`, else unset) feeds the recognizer the
2628    /// pipeline's own page render (2.0 px/pt = 144 dpi) for a rendered page,
2629    /// and for a standalone image input docling's own resolution (#570): 3
2630    /// px/pt shrunk so the longer side stays within RapidOCR's 2000 px — what
2631    /// its models see after RapidOCR's `max_side_len` pass (a 754 × 1000 scan
2632    /// reads at 2.0). A set value resamples the render / image for the OCR
2633    /// input only — layout and TableFormer keep their pinned-resolution
2634    /// pixels, so the conformance baseline never moves. Lower it when the
2635    /// source raster is already high-resolution and upscaling degrades
2636    /// recognition; raise it toward docling's 216 dpi for parity experiments.
2637    /// Non-positive values are ignored.
2638    pub fn ocr_scale(mut self, scale: Option<f32>) -> Self {
2639        self.ocr_scale = scale
2640            .filter(|s| s.is_finite() && *s > 0.0)
2641            .or_else(ocr::scale_from_env);
2642        self
2643    }
2644
2645    /// The shared TableFormer slot handed to each worker, or `None` when the
2646    /// pipeline options skip TableFormer entirely.
2647    fn tables_slot(&self) -> Option<SharedTables> {
2648        if self.no_table_former || self.no_ocr {
2649            None
2650        } else {
2651            Some(Arc::clone(&self.tables))
2652        }
2653    }
2654
2655    /// The shared enrichment slots for a worker (`None` per model unless its
2656    /// flag is on; `no_ocr` skips layout, so there are no regions to enrich).
2657    fn enrich_slots(&self) -> (Option<SharedClassifier>, Option<SharedCodeFormula>) {
2658        if self.no_ocr || !self.enrich.any() {
2659            return (None, None);
2660        }
2661        (
2662            self.enrich
2663                .picture_classification
2664                .then(|| Arc::clone(&self.classifier)),
2665            (self.enrich.code || self.enrich.formula).then(|| Arc::clone(&self.code_formula)),
2666        )
2667    }
2668
2669    /// Eagerly load the models (the full-intra serial worker: layout + OCR, and
2670    /// the shared TableFormer unless disabled) so the first conversion doesn't pay
2671    /// the load cost. Idempotent; respects `no_ocr` / `no_table_former` (with
2672    /// `no_ocr` there is nothing to load). The docling.rs analogue of docling's
2673    /// `DocumentConverter.initialize_pipeline`.
2674    pub fn warm_up(&mut self) -> Result<(), PdfError> {
2675        self.primary()?;
2676        // The shared TableFormer loads on the first table otherwise — a
2677        // document with one would still pay for it (#548).
2678        if let Some(tables) = self.tables_slot() {
2679            tables.lock().unwrap_or_else(|p| p.into_inner()).load();
2680        }
2681        Ok(())
2682    }
2683
2684    /// [`warm_up`](Self::warm_up) plus the parallel page-worker pool, so the
2685    /// first *multi-page* conversion is no slower than later ones either: a
2686    /// document of `DOCLING_RS_PDF_PARALLEL_MIN` pages (6) or more fans out across the pool,
2687    /// whose workers each load their own layout/OCR sessions on first use
2688    /// (#548 — docling-serve's `--warmup` left a 9-page PDF paying that load).
2689    /// Costs the pool's memory up front (~0.4 GB a worker); with a pool of one
2690    /// worker it is exactly `warm_up`.
2691    pub fn warm_up_all(&mut self) -> Result<(), PdfError> {
2692        self.warm_up()?;
2693        if self.target_workers >= 2 {
2694            self.ensure_pool()?;
2695        }
2696        Ok(())
2697    }
2698
2699    /// The full-intra serial worker, loaded on first use.
2700    fn primary(&mut self) -> Result<&mut Worker, PdfError> {
2701        if self.primary.is_none() {
2702            self.primary = Some(Worker::load(
2703                intra_threads(),
2704                self.tables_slot(),
2705                self.enrich_slots(),
2706                self.enrich,
2707                self.no_ocr,
2708                self.skip_ocr,
2709                // The mode-shaped spelling (#254) and the flag are one engine
2710                // truth: whichever demands forcing wins, mirroring docling's
2711                // `force_full_page_ocr` → `mode=full_page` bridge.
2712                self.force_full_page_ocr || self.ocr_mode.forces_full_page(),
2713                self.no_text_panels,
2714                self.ocr_lang,
2715                self.ocr_engine,
2716                self.tesseract_lang.clone(),
2717                self.ocr_scale,
2718                self.images,
2719            )?);
2720        }
2721        Ok(self.primary.as_mut().unwrap())
2722    }
2723
2724    /// Convert a PDF (bytes) to a [`DoclingDocument`]. A document with fewer than
2725    /// `parallel_min` pages (or a pool size of 1) streams through the full-intra
2726    /// primary; a larger one renders on this thread (pdfium is not thread-safe) and
2727    /// fans the pages out across the worker pool, reassembled in page order so the
2728    /// output is byte-identical to the serial path.
2729    pub fn convert(
2730        &mut self,
2731        bytes: &[u8],
2732        password: Option<&str>,
2733        name: &str,
2734    ) -> Result<DoclingDocument, PdfError> {
2735        self.convert_outcome(bytes, password, name)
2736            .map(|c| c.document)
2737    }
2738
2739    /// [`convert`](Self::convert) that also says how the conversion ended —
2740    /// whether the [`document_timeout`](Self::document_timeout) cut it short
2741    /// and how many pages made it (`Completion`).
2742    pub fn convert_outcome(
2743        &mut self,
2744        bytes: &[u8],
2745        password: Option<&str>,
2746        name: &str,
2747    ) -> Result<Converted, PdfError> {
2748        let deadline = self.deadline();
2749        let pages = pdfium_backend::page_count(bytes, password)?;
2750        let range = self.resolve_range(pages)?;
2751        // Serial vs parallel is decided by the pages actually converted: a
2752        // 3-page window over a 500-page PDF should not pay the pool load.
2753        let selected = range.map_or(pages, |(a, b)| b - a + 1);
2754        let (document, done, timed_out) =
2755            if self.target_workers >= 2 && selected >= self.parallel_min {
2756                self.convert_parallel(bytes, password, name, range, selected, deadline)?
2757            } else {
2758                self.convert_serial(bytes, password, name, range, selected, deadline)?
2759            };
2760        timing::report();
2761        Ok(Converted {
2762            document,
2763            completion: self.completion(timed_out, done, selected),
2764        })
2765    }
2766
2767    /// Stream pages one at a time through the primary worker — render → process →
2768    /// drop — so the document holds ~one page bitmap (~5 MB) at a time.
2769    fn convert_serial(
2770        &mut self,
2771        bytes: &[u8],
2772        password: Option<&str>,
2773        name: &str,
2774        range: Option<(usize, usize)>,
2775        selected: usize,
2776        deadline: Option<std::time::Instant>,
2777    ) -> Result<(DoclingDocument, usize, bool), PdfError> {
2778        let mut doc = DoclingDocument::new(name);
2779        let mut confs = std::collections::BTreeMap::new();
2780        let render_image = !self.no_ocr;
2781        let extract_text = self.extract_text_layer();
2782        let progress = self.progress.clone();
2783        let mut done = 0usize;
2784        let worker = self.primary()?;
2785        // The walk renders a page before handing it over, so the budget is
2786        // checked where a page *arrives*: a page the budget had already run
2787        // out on is dropped unprocessed and ends the walk through the
2788        // sentinel (`for_each_page` reads it as an error; here it is the cut).
2789        let walk = pdfium_backend::for_each_page(
2790            bytes,
2791            password,
2792            render_image,
2793            extract_text,
2794            range,
2795            |n, _total, mut page| {
2796                if expired(deadline) {
2797                    return Err(timeout_sentinel());
2798                }
2799                let (mut nodes, links, conf, page_image) = worker.process(n, &mut page)?;
2800                assemble::stamp_page_no(&mut nodes, n + 1);
2801                if let Some(img) = page_image {
2802                    doc.page_images.insert(n + 1, img);
2803                }
2804                doc.nodes.extend(nodes);
2805                doc.links.extend(links);
2806                confs.insert(n + 1, conf);
2807                done += 1;
2808                if let Some(cb) = &progress {
2809                    cb(done, selected);
2810                }
2811                Ok::<(), PdfError>(())
2812            },
2813        );
2814        let timed_out = match walk {
2815            Ok(()) => false,
2816            Err(PdfError::Timeout(_)) => true,
2817            Err(e) => return Err(e),
2818        };
2819        assemble::merge_continuations(&mut doc.nodes);
2820        self.apply_heading_hierarchy(&mut doc.nodes, Some(bytes));
2821        doc.confidence = Some(docling_core::ConfidenceReport::from_pages(confs));
2822        Ok((doc, done, timed_out))
2823    }
2824
2825    /// Render pages serially on this thread (pdfium) and process them in parallel
2826    /// across the worker pool. A bounded channel applies backpressure so only a
2827    /// handful of page bitmaps are resident at once; results carry their page
2828    /// index and are reassembled in order, so the output is byte-identical to the
2829    /// serial path.
2830    fn convert_parallel(
2831        &mut self,
2832        bytes: &[u8],
2833        password: Option<&str>,
2834        name: &str,
2835        range: Option<(usize, usize)>,
2836        selected: usize,
2837        deadline: Option<std::time::Instant>,
2838    ) -> Result<(DoclingDocument, usize, bool), PdfError> {
2839        self.ensure_pool()?;
2840        let progress = self.progress.clone();
2841        let pages_done = std::sync::atomic::AtomicUsize::new(0);
2842        let timed_out = std::sync::atomic::AtomicBool::new(false);
2843        let n_workers = self.pool.len();
2844        let render_image = !self.no_ocr;
2845        let extract_text = self.extract_text_layer();
2846        let layout_batch = pdf_layout_batch();
2847        // Bound sized so every worker can accumulate a full layout batch while
2848        // rendering stays ahead (and never below the pre-#73 render-ahead of
2849        // two pages per worker); still a hard cap on resident page bitmaps.
2850        let (work_tx, work_rx) = sync_channel::<(usize, PdfPage)>(n_workers * layout_batch.max(2));
2851        let work_rx: Arc<Mutex<Receiver<(usize, PdfPage)>>> = Arc::new(Mutex::new(work_rx));
2852        let results: Arc<Mutex<Vec<(usize, PageOut)>>> = Arc::new(Mutex::new(Vec::new()));
2853        let first_err: Arc<Mutex<Option<PdfError>>> = Arc::new(Mutex::new(None));
2854
2855        // Move the pool into the scope so each worker gets an exclusive `&mut`.
2856        let mut workers = std::mem::take(&mut self.pool);
2857        std::thread::scope(|s| {
2858            for worker in workers.iter_mut() {
2859                let work_rx = Arc::clone(&work_rx);
2860                let results = Arc::clone(&results);
2861                let first_err = Arc::clone(&first_err);
2862                let progress = progress.clone();
2863                let pages_done = &pages_done;
2864                let timed_out = &timed_out;
2865                s.spawn(move || {
2866                    worker.run_pool(&work_rx, layout_batch, deadline, |idx, out| {
2867                        match out {
2868                            Ok(out) => {
2869                                results.lock().unwrap().push((idx, out));
2870                                let d = pages_done
2871                                    .fetch_add(1, std::sync::atomic::Ordering::Relaxed)
2872                                    + 1;
2873                                if let Some(cb) = &progress {
2874                                    cb(d, selected);
2875                                }
2876                            }
2877                            // The budget ran out before this page's turn: the
2878                            // page is left out, the document is partial.
2879                            Err(PdfError::Timeout(_)) => {
2880                                timed_out.store(true, std::sync::atomic::Ordering::Relaxed);
2881                            }
2882                            Err(e) => {
2883                                let mut slot = first_err.lock().unwrap();
2884                                if slot.is_none() {
2885                                    *slot = Some(e);
2886                                }
2887                            }
2888                        }
2889                        true
2890                    });
2891                });
2892            }
2893            // Render on this thread and feed the workers; backpressure blocks here
2894            // when the channel is full. Dropping `work_tx` afterwards signals the
2895            // workers (recv → Err) to finish. A spent budget ends the walk before
2896            // the next page is rendered.
2897            let render = pdfium_backend::for_each_page(
2898                bytes,
2899                password,
2900                render_image,
2901                extract_text,
2902                range,
2903                |i, _total, page| {
2904                    if expired(deadline) {
2905                        return Err(timeout_sentinel());
2906                    }
2907                    work_tx
2908                        .send((i, page))
2909                        .map_err(|_| PdfError::Pdfium("page-worker channel closed".into()))
2910                },
2911            );
2912            drop(work_tx);
2913            match render {
2914                Ok(()) => {}
2915                Err(PdfError::Timeout(_)) => {
2916                    timed_out.store(true, std::sync::atomic::Ordering::Relaxed);
2917                }
2918                Err(e) => {
2919                    let mut slot = first_err.lock().unwrap();
2920                    if slot.is_none() {
2921                        *slot = Some(e);
2922                    }
2923                }
2924            }
2925        });
2926        // Threads have joined; restore the pool for the next conversion.
2927        self.pool = workers;
2928
2929        if let Some(e) = first_err.lock().unwrap().take() {
2930            return Err(e);
2931        }
2932        let mut results = Arc::try_unwrap(results)
2933            .unwrap_or_else(|arc| Mutex::new(arc.lock().unwrap().clone()))
2934            .into_inner()
2935            .unwrap();
2936        results.sort_by_key(|(idx, _)| *idx);
2937        let done = results.len();
2938        let mut doc = DoclingDocument::new(name);
2939        let mut confs = std::collections::BTreeMap::new();
2940        for (idx, (mut nodes, links, conf, page_image)) in results {
2941            assemble::stamp_page_no(&mut nodes, idx + 1);
2942            if let Some(img) = page_image {
2943                doc.page_images.insert(idx + 1, img);
2944            }
2945            doc.nodes.extend(nodes);
2946            doc.links.extend(links);
2947            confs.insert(idx + 1, conf);
2948        }
2949        assemble::merge_continuations(&mut doc.nodes);
2950        self.apply_heading_hierarchy(&mut doc.nodes, Some(bytes));
2951        doc.confidence = Some(docling_core::ConfidenceReport::from_pages(confs));
2952        let timed_out = timed_out.load(std::sync::atomic::Ordering::Relaxed);
2953        Ok((doc, done, timed_out))
2954    }
2955
2956    /// Convert a PDF in **streaming** mode: `emit` is called with each finalized,
2957    /// in-document-order batch of nodes (and that span's recovered links) as pages
2958    /// complete, so a caller can serialize Markdown page by page instead of waiting
2959    /// for the whole document. The batches are exactly the buffered [`convert`]'s
2960    /// nodes, split at safe block boundaries by [`assemble::StreamAssembler`] — the
2961    /// parallel path reorders pages back into document order before emitting, so
2962    /// the output is identical regardless of worker scheduling.
2963    ///
2964    /// `emit` runs on the calling thread (never a worker), so it needn't be `Send`
2965    /// and its backpressure throttles the whole pipeline. Returning `Err` from
2966    /// `emit` aborts the conversion with that error.
2967    pub fn convert_streaming<F>(
2968        &mut self,
2969        bytes: &[u8],
2970        password: Option<&str>,
2971        name: &str,
2972        emit: F,
2973    ) -> Result<(), PdfError>
2974    where
2975        F: FnMut(Vec<Node>, Vec<(String, String)>) -> Result<(), PdfError>,
2976    {
2977        self.convert_streaming_outcome(bytes, password, name, emit)
2978            .map(|_| ())
2979    }
2980
2981    /// [`convert_streaming`](Self::convert_streaming) that also says how the
2982    /// conversion ended ([`Completion`]): a spent
2983    /// [`document_timeout`](Self::document_timeout) stops the walk, the pages
2984    /// finished so far are emitted (the tail included) and the outcome is
2985    /// `TimedOut` — never an error.
2986    pub fn convert_streaming_outcome<F>(
2987        &mut self,
2988        bytes: &[u8],
2989        password: Option<&str>,
2990        name: &str,
2991        emit: F,
2992    ) -> Result<Completion, PdfError>
2993    where
2994        F: FnMut(Vec<Node>, Vec<(String, String)>) -> Result<(), PdfError>,
2995    {
2996        let _ = name; // page nodes carry no name; the caller owns the document name.
2997        let deadline = self.deadline();
2998        let pages = pdfium_backend::page_count(bytes, password)?;
2999        let range = self.resolve_range(pages)?;
3000        let selected = range.map_or(pages, |(a, b)| b - a + 1);
3001        let r = if self.target_workers >= 2 && selected >= self.parallel_min {
3002            self.convert_streaming_parallel(bytes, password, range, deadline, emit)
3003        } else {
3004            self.convert_streaming_serial(bytes, password, range, deadline, emit)
3005        };
3006        timing::report();
3007        let (done, timed_out) = r?;
3008        Ok(self.completion(timed_out, done, selected))
3009    }
3010
3011    /// Serial streaming: render → process → emit, one page at a time, holding back
3012    /// only the tail that might still merge into the next page.
3013    fn convert_streaming_serial<F>(
3014        &mut self,
3015        bytes: &[u8],
3016        password: Option<&str>,
3017        range: Option<(usize, usize)>,
3018        deadline: Option<std::time::Instant>,
3019        mut emit: F,
3020    ) -> Result<(usize, bool), PdfError>
3021    where
3022        F: FnMut(Vec<Node>, Vec<(String, String)>) -> Result<(), PdfError>,
3023    {
3024        let mut asm = assemble::StreamAssembler::new();
3025        let render_image = !self.no_ocr;
3026        let extract_text = self.extract_text_layer();
3027        let worker = self.primary()?;
3028        let mut done = 0usize;
3029        let walk = pdfium_backend::for_each_page(
3030            bytes,
3031            password,
3032            render_image,
3033            extract_text,
3034            range,
3035            |n, _total, mut page| {
3036                if expired(deadline) {
3037                    return Err(timeout_sentinel());
3038                }
3039                // Confidence is dropped on the streaming path: the report is
3040                // only complete once every page has run, which defeats
3041                // page-by-page emission — buffered `convert` carries it.
3042                let (nodes, links, _conf, _page_image) = worker.process(n, &mut page)?;
3043                done += 1;
3044                emit(asm.push(nodes), links)
3045            },
3046        );
3047        let timed_out = match walk {
3048            Ok(()) => false,
3049            Err(PdfError::Timeout(_)) => true,
3050            Err(e) => return Err(e),
3051        };
3052        emit(asm.finish(), Vec::new())?;
3053        Ok((done, timed_out))
3054    }
3055
3056    /// Parallel streaming: pages render serially on a dedicated thread (pdfium is
3057    /// not thread-safe) and process across the worker pool; results carry their
3058    /// page index and are reordered on the calling thread into a
3059    /// [`assemble::StreamAssembler`], which emits each page in document order as
3060    /// soon as its predecessors have arrived. Bounded channels keep only a handful
3061    /// of pages resident and let `emit`'s backpressure reach the renderer.
3062    fn convert_streaming_parallel<F>(
3063        &mut self,
3064        bytes: &[u8],
3065        password: Option<&str>,
3066        range: Option<(usize, usize)>,
3067        deadline: Option<std::time::Instant>,
3068        mut emit: F,
3069    ) -> Result<(usize, bool), PdfError>
3070    where
3071        F: FnMut(Vec<Node>, Vec<(String, String)>) -> Result<(), PdfError>,
3072    {
3073        self.ensure_pool()?;
3074        let n_workers = self.pool.len();
3075        let render_image = !self.no_ocr;
3076        let extract_text = self.extract_text_layer();
3077        let layout_batch = pdf_layout_batch();
3078        // Bound sized so every worker can accumulate a full layout batch while
3079        // rendering stays ahead (and never below the pre-#73 render-ahead of
3080        // two pages per worker); still a hard cap on resident page bitmaps.
3081        let (work_tx, work_rx) = sync_channel::<(usize, PdfPage)>(n_workers * layout_batch.max(2));
3082        let work_rx: Arc<Mutex<Receiver<(usize, PdfPage)>>> = Arc::new(Mutex::new(work_rx));
3083        // Workers and the renderer report here; the calling thread drains it in
3084        // page order. Bounded so workers block (bounding resident bitmaps) when the
3085        // consumer falls behind.
3086        let (res_tx, res_rx) = sync_channel::<Result<(usize, PageOut), PdfError>>(n_workers * 2);
3087
3088        let mut workers = std::mem::take(&mut self.pool);
3089        let mut asm = assemble::StreamAssembler::new();
3090        let mut first_err: Option<PdfError> = None;
3091        let mut timed_out = false;
3092        let mut done = 0usize;
3093
3094        std::thread::scope(|s| {
3095            // Workers: pull a batch of pages (whatever is already rendered, up
3096            // to the layout batch size), process it, report (index-tagged)
3097            // results.
3098            for worker in workers.iter_mut() {
3099                let work_rx = Arc::clone(&work_rx);
3100                let res_tx = res_tx.clone();
3101                s.spawn(move || {
3102                    worker.run_pool(&work_rx, layout_batch, deadline, |idx, out| {
3103                        // `false` once the consumer is gone.
3104                        res_tx.send(out.map(|o| (idx, o))).is_ok()
3105                    });
3106                });
3107            }
3108            // Renderer: feed pages to the pool on its own thread (pdfium stays on a
3109            // single thread); report a render error through the same channel. A
3110            // spent budget ends the walk before the next page is rendered.
3111            {
3112                let res_tx = res_tx.clone();
3113                s.spawn(move || {
3114                    let render = pdfium_backend::for_each_page(
3115                        bytes,
3116                        password,
3117                        render_image,
3118                        extract_text,
3119                        range,
3120                        |i, _total, page| {
3121                            if expired(deadline) {
3122                                return Err(timeout_sentinel());
3123                            }
3124                            work_tx
3125                                .send((i, page))
3126                                .map_err(|_| PdfError::Pdfium("page-worker channel closed".into()))
3127                        },
3128                    );
3129                    drop(work_tx); // signal workers to finish
3130                    if let Err(e) = render {
3131                        let _ = res_tx.send(Err(e));
3132                    }
3133                });
3134            }
3135            // Drop our own sender so the channel closes once the threads finish.
3136            drop(res_tx);
3137
3138            // Collector (this thread): reorder into document order and emit.
3139            // With a page window, indices start at the window's first page.
3140            // A timed-out page leaves a hole in the sequence: everything
3141            // before it emits in order, the pages after it stay buffered —
3142            // they are the pages the budget did not cover, and the document
3143            // ends at the cut like the buffered path's.
3144            let mut buffer: BTreeMap<usize, PageOut> = BTreeMap::new();
3145            let mut next = range.map_or(0, |(first, _)| first);
3146            for msg in res_rx.iter() {
3147                match msg {
3148                    Err(PdfError::Timeout(_)) => timed_out = true,
3149                    Err(e) => {
3150                        if first_err.is_none() {
3151                            first_err = Some(e);
3152                        }
3153                    }
3154                    Ok((idx, out)) => {
3155                        buffer.insert(idx, out);
3156                        if first_err.is_some() {
3157                            continue; // keep draining so the threads can exit
3158                        }
3159                        // A stream carries Markdown, not the page map: page
3160                        // images (#520) have nowhere to go and are dropped.
3161                        while let Some((nodes, links, _conf, _page_image)) = buffer.remove(&next) {
3162                            if let Err(e) = emit(asm.push(nodes), links) {
3163                                first_err = Some(e);
3164                                break;
3165                            }
3166                            next += 1;
3167                            done += 1;
3168                        }
3169                    }
3170                }
3171            }
3172            // Pages that finished after a hole (a page the budget skipped
3173            // before them) cannot follow it in document order; count them
3174            // out of the document like the skipped one.
3175            if timed_out && !buffer.is_empty() {
3176                buffer.clear();
3177            }
3178        });
3179        // Threads have joined; restore the pool for the next conversion.
3180        self.pool = workers;
3181
3182        if let Some(e) = first_err {
3183            return Err(e);
3184        }
3185        emit(asm.finish(), Vec::new())?;
3186        Ok((done, timed_out))
3187    }
3188
3189    /// Lazily grow the pool to `target_workers`, loading the new workers
3190    /// concurrently (model load is mostly I/O + mmap, so N loads overlap to roughly
3191    /// one load's wall-time). Cached for reuse across documents.
3192    fn ensure_pool(&mut self) -> Result<(), PdfError> {
3193        let need = self.target_workers.saturating_sub(self.pool.len());
3194        if need == 0 {
3195            return Ok(());
3196        }
3197        let intra = pdf_intra();
3198        let no_ocr = self.no_ocr;
3199        let skip_ocr = self.skip_ocr;
3200        let force = self.force_full_page_ocr || self.ocr_mode.forces_full_page();
3201        let ntp = self.no_text_panels;
3202        let ocr_lang = self.ocr_lang;
3203        let ocr_engine = self.ocr_engine;
3204        let tesseract_lang = self.tesseract_lang.clone();
3205        let ocr_scale = self.ocr_scale;
3206        let images = self.images;
3207        let enrich = self.enrich;
3208        let tables = self.tables_slot();
3209        let enrich_slots = self.enrich_slots();
3210        let loaded: Vec<Result<Worker, PdfError>> = std::thread::scope(|s| {
3211            let handles: Vec<_> = (0..need)
3212                .map(|_| {
3213                    let tables = tables.clone();
3214                    let enrich_slots = enrich_slots.clone();
3215                    let tesseract_lang = tesseract_lang.clone();
3216                    s.spawn(move || {
3217                        Worker::load(
3218                            intra,
3219                            tables,
3220                            enrich_slots,
3221                            enrich,
3222                            no_ocr,
3223                            skip_ocr,
3224                            force,
3225                            ntp,
3226                            ocr_lang,
3227                            ocr_engine,
3228                            tesseract_lang,
3229                            ocr_scale,
3230                            images,
3231                        )
3232                    })
3233                })
3234                .collect();
3235            handles.into_iter().map(|h| h.join().unwrap()).collect()
3236        });
3237        for w in loaded {
3238            self.pool.push(w?);
3239        }
3240        Ok(())
3241    }
3242
3243    /// Convert a standalone image (PNG/JPEG/TIFF/WebP/…) as a single page —
3244    /// docling routes images through the same layout+OCR pipeline as a PDF page.
3245    pub fn convert_image(&mut self, bytes: &[u8], name: &str) -> Result<DoclingDocument, PdfError> {
3246        let image = decode_image_limited(bytes)?;
3247        let (w, h) = image.dimensions();
3248        // The image is its own page rendered at 1 px per "point" (scale 1.0); a
3249        // standalone image has no text layer, so OCR supplies the cells.
3250        let page = PdfPage {
3251            width: w as f32,
3252            height: h as f32,
3253            scale: 1.0,
3254            cells: Vec::new(),
3255            code_cells: Vec::new(),
3256            checkboxes: Vec::new(),
3257            word_cells: Vec::new(),
3258            // A standalone image *is* its own scale-1.0 page image, so the
3259            // layout model sees it through the docling-exact PIL kernel.
3260            image_layout: Some(image.clone()),
3261            image,
3262            links: Vec::new(),
3263            rotation: 0,
3264        };
3265        self.process_pages(vec![page], name)
3266    }
3267
3268    /// Run layout (+ OCR for cell-less pages) and assemble each already-rendered
3269    /// page (image / METS inputs, which are small and already materialised).
3270    /// Public so [`mets::convert_mets_gbs_with_pipeline`] can drive a
3271    /// caller-configured pipeline (#244).
3272    pub fn process_pages(
3273        &mut self,
3274        mut pages: Vec<PdfPage>,
3275        name: &str,
3276    ) -> Result<DoclingDocument, PdfError> {
3277        let mut doc = DoclingDocument::new(name);
3278        let mut confs = std::collections::BTreeMap::new();
3279        let worker = self.primary()?;
3280        for (n, page) in pages.iter_mut().enumerate() {
3281            let (mut nodes, links, conf, page_image) = worker.process(n, page)?;
3282            assemble::stamp_page_no(&mut nodes, n + 1);
3283            if let Some(img) = page_image {
3284                doc.page_images.insert(n + 1, img);
3285            }
3286            doc.nodes.extend(nodes);
3287            doc.links.extend(links);
3288            confs.insert(n + 1, conf);
3289        }
3290        assemble::merge_continuations(&mut doc.nodes);
3291        // No PDF behind these pages (images, METS): the heading-hierarchy
3292        // stage degrades to the numbering signal — exactly docling without
3293        // an outline or parsed pages.
3294        self.apply_heading_hierarchy(&mut doc.nodes, None);
3295        doc.confidence = Some(docling_core::ConfidenceReport::from_pages(confs));
3296        Ok(doc)
3297    }
3298}
3299
3300/// Number of pages in a PDF, without converting anything — what the CLI batch
3301/// mode prints in its per-document start line.
3302#[cfg(feature = "ml")]
3303pub fn page_count(bytes: &[u8], password: Option<&str>) -> Result<usize, PdfError> {
3304    pdfium_backend::page_count(bytes, password)
3305}
3306
3307#[cfg(feature = "ml")]
3308/// Convenience one-shot conversion (loads the pipeline per call). Errors are
3309/// detailed and surfaced (never silently skipped).
3310pub fn convert(
3311    bytes: &[u8],
3312    password: Option<&str>,
3313    name: &str,
3314) -> Result<DoclingDocument, PdfError> {
3315    convert_with_options(
3316        bytes,
3317        password,
3318        name,
3319        false,
3320        false,
3321        false,
3322        false,
3323        EnrichmentOptions::default(),
3324        None,
3325        None,
3326    )
3327}
3328
3329#[cfg(feature = "ml")]
3330/// Like [`convert`], but optionally skips loading/running TableFormer (see
3331/// [`Pipeline::no_table_former`]) and/or layout+OCR+TableFormer entirely (see
3332/// [`Pipeline::no_ocr`]), and/or enables the enrichment passes (see
3333/// [`Pipeline::enrichments`]).
3334// One positional per pipeline switch mirrors the Pipeline builder; growing
3335// past clippy's arity cap is the price of keeping this one-shot signature
3336// stable-ish instead of churning callers into an options struct mid-series.
3337#[allow(clippy::too_many_arguments)]
3338pub fn convert_with_options(
3339    bytes: &[u8],
3340    password: Option<&str>,
3341    name: &str,
3342    no_table_former: bool,
3343    no_ocr: bool,
3344    force_full_page_ocr: bool,
3345    no_text_panels: bool,
3346    enrich: EnrichmentOptions,
3347    pages: Option<(usize, usize)>,
3348    ocr_lang: Option<OcrLang>,
3349) -> Result<DoclingDocument, PdfError> {
3350    Pipeline::new()?
3351        .no_table_former(no_table_former)
3352        .no_ocr(no_ocr)
3353        .force_full_page_ocr(force_full_page_ocr)
3354        .no_text_panels(no_text_panels)
3355        .enrichments(enrich)
3356        .pages(pages)
3357        .ocr_lang(ocr_lang)
3358        .convert(bytes, password, name)
3359}
3360
3361#[cfg(feature = "ml")]
3362/// Convenience one-shot image conversion (loads the pipeline per call).
3363pub fn convert_image(bytes: &[u8], name: &str) -> Result<DoclingDocument, PdfError> {
3364    convert_image_with_options(
3365        bytes,
3366        name,
3367        false,
3368        false,
3369        false,
3370        EnrichmentOptions::default(),
3371        None,
3372    )
3373}
3374
3375#[cfg(feature = "ml")]
3376/// Like [`convert_image`], but optionally skips loading/running TableFormer (see
3377/// [`Pipeline::no_table_former`]) and/or layout+OCR+TableFormer entirely (see
3378/// [`Pipeline::no_ocr`]), and/or enables the enrichment passes.
3379pub fn convert_image_with_options(
3380    bytes: &[u8],
3381    name: &str,
3382    no_table_former: bool,
3383    no_ocr: bool,
3384    no_text_panels: bool,
3385    enrich: EnrichmentOptions,
3386    ocr_lang: Option<OcrLang>,
3387) -> Result<DoclingDocument, PdfError> {
3388    Pipeline::new()?
3389        .no_table_former(no_table_former)
3390        .no_ocr(no_ocr)
3391        .no_text_panels(no_text_panels)
3392        .enrichments(enrich)
3393        .ocr_lang(ocr_lang)
3394        .convert_image(bytes, name)
3395}
3396
3397#[cfg(feature = "ml")]
3398/// Convert pre-segmented pages (image + already-known text cells, e.g. METS/hOCR
3399/// scans) through the shared layout + assembly pipeline.
3400pub fn convert_pages(pages: Vec<PdfPage>, name: &str) -> Result<DoclingDocument, PdfError> {
3401    convert_pages_with_options(
3402        pages,
3403        name,
3404        false,
3405        false,
3406        false,
3407        EnrichmentOptions::default(),
3408    )
3409}
3410
3411#[cfg(feature = "ml")]
3412/// Like [`convert_pages`], but optionally skips loading/running TableFormer (see
3413/// [`Pipeline::no_table_former`]) and/or layout+OCR+TableFormer entirely (see
3414/// [`Pipeline::no_ocr`]), and/or enables the enrichment passes.
3415pub fn convert_pages_with_options(
3416    pages: Vec<PdfPage>,
3417    name: &str,
3418    no_table_former: bool,
3419    no_ocr: bool,
3420    no_text_panels: bool,
3421    enrich: EnrichmentOptions,
3422) -> Result<DoclingDocument, PdfError> {
3423    Pipeline::new()?
3424        .no_table_former(no_table_former)
3425        .no_text_panels(no_text_panels)
3426        .no_ocr(no_ocr)
3427        .enrichments(enrich)
3428        .process_pages(pages, name)
3429}
3430
3431#[cfg(feature = "ml")]
3432#[cfg(all(test, feature = "ml"))]
3433mod image_limit_tests {
3434    use super::decode_image_with_max_side;
3435
3436    /// A small valid PNG encoded via the `image` crate (robust vs. a hand-rolled
3437    /// byte literal).
3438    fn png_bytes(w: u32, h: u32) -> Vec<u8> {
3439        use std::io::Cursor;
3440        let img = image::RgbImage::new(w, h);
3441        let mut out = Vec::new();
3442        img.write_to(&mut Cursor::new(&mut out), image::ImageFormat::Png)
3443            .unwrap();
3444        out
3445    }
3446
3447    #[test]
3448    fn normal_image_decodes_under_the_cap() {
3449        let img = decode_image_with_max_side(&png_bytes(8, 8), 30_000).expect("8x8 decodes");
3450        assert_eq!(img.dimensions(), (8, 8));
3451    }
3452
3453    /// docling#4247 (2.128): the EXIF orientation is applied when a frame is
3454    /// loaded — a 4×2 JPEG tagged "rotate 90° CW" (orientation 6) decodes as
3455    /// 2×4, with the pixels turned.
3456    #[test]
3457    fn exif_orientation_is_applied() {
3458        use std::io::Cursor;
3459        let mut img = image::RgbImage::new(4, 2);
3460        img.put_pixel(0, 0, image::Rgb([255, 0, 0]));
3461        let mut jpeg = Vec::new();
3462        img.write_to(&mut Cursor::new(&mut jpeg), image::ImageFormat::Jpeg)
3463            .unwrap();
3464        // Splice an APP1 Exif segment (little-endian TIFF, one IFD entry:
3465        // Orientation = 6) right after the SOI marker.
3466        let mut tiff = b"II*\0".to_vec();
3467        tiff.extend(8u32.to_le_bytes());
3468        tiff.extend(1u16.to_le_bytes());
3469        tiff.extend(0x0112u16.to_le_bytes());
3470        tiff.extend(3u16.to_le_bytes());
3471        tiff.extend(1u32.to_le_bytes());
3472        tiff.extend(6u16.to_le_bytes());
3473        tiff.extend([0, 0]);
3474        tiff.extend(0u32.to_le_bytes());
3475        let mut app1 = b"Exif\0\0".to_vec();
3476        app1.extend(tiff);
3477        let mut out = jpeg[..2].to_vec();
3478        out.extend([0xff, 0xe1]);
3479        out.extend((app1.len() as u16 + 2).to_be_bytes());
3480        out.extend(app1);
3481        out.extend(&jpeg[2..]);
3482        let plain = decode_image_with_max_side(&jpeg, 30_000).unwrap();
3483        assert_eq!(plain.dimensions(), (4, 2));
3484        let turned = decode_image_with_max_side(&out, 30_000).unwrap();
3485        assert_eq!(turned.dimensions(), (2, 4), "quarter turn swaps the sides");
3486        // Rotating 90° CW moves the top-left pixel to the top-right corner.
3487        assert!(turned.get_pixel(1, 0).0[0] > 128);
3488        assert!(turned.get_pixel(0, 0).0[0] <= 128);
3489    }
3490
3491    #[test]
3492    fn dimensions_over_the_cap_are_rejected_not_aborted() {
3493        // A per-side cap below the image's declared size must yield a
3494        // recoverable Err, never an allocation-abort — the mechanism that stops
3495        // a crafted image declaring 60000×60000 from OOM-killing the process.
3496        let r = decode_image_with_max_side(&png_bytes(8, 8), 4);
3497        assert!(
3498            r.is_err(),
3499            "decode must fail under the pixel cap, not abort"
3500        );
3501    }
3502}
3503
3504#[cfg(all(test, feature = "ml"))]
3505mod ocr_scale_tests {
3506    use super::*;
3507
3508    /// #570: an explicit scale wins everywhere; a rendered page keeps its
3509    /// render; a scale-1.0 image page reads at docling's 3 px/pt, shrunk to
3510    /// RapidOCR's 2000 px longer side (a FUNSD scan at 2.0, a 3000 px photo
3511    /// downsampled to 0.67), and an image that lands at 1.0 is not resampled.
3512    #[test]
3513    fn image_inputs_follow_rapidocrs_resolution() {
3514        assert_eq!(page_ocr_scale(Some(1.5), 754.0, 1000.0, 1.0), Some(1.5));
3515        assert_eq!(page_ocr_scale(None, 612.0, 792.0, 2.0), None);
3516        assert_eq!(page_ocr_scale(None, 754.0, 1000.0, 1.0), Some(2.0));
3517        assert_eq!(page_ocr_scale(None, 400.0, 600.0, 1.0), Some(3.0));
3518        let s = page_ocr_scale(None, 3000.0, 2000.0, 1.0).unwrap();
3519        assert!((s - 2.0 / 3.0).abs() < 1e-6, "{s}");
3520        assert_eq!(page_ocr_scale(None, 2000.0, 1500.0, 1.0), None);
3521    }
3522}
3523
3524#[cfg(test)]
3525mod median_tests {
3526    #[test]
3527    fn median_of_empty_is_zero_not_a_panic() {
3528        // A crafted table can leave a row/column with zero matched cells; the
3529        // even-count branch would index values[0 - 1] and panic (→ remote crash
3530        // via docling-serve) without the empty guard.
3531        assert_eq!(super::tf_match::median_for_test(&mut []), 0.0);
3532        assert_eq!(super::tf_match::median_for_test(&mut [4.0, 2.0]), 3.0);
3533        assert_eq!(super::tf_match::median_for_test(&mut [5.0, 1.0, 3.0]), 3.0);
3534    }
3535}
3536
3537#[cfg(test)]
3538mod unreadable_tests {
3539    /// The text-layer path (the wasm build's only one) tells a file nothing
3540    /// can open apart from a PDF that merely has no text layer: garbage is
3541    /// an error about the file, a real PDF converts.
3542    #[test]
3543    fn text_layer_path_reports_an_unreadable_file() {
3544        for bytes in [b"garbage".as_slice(), b"", b"%PDF-1.7\n%%EOF\n"] {
3545            let err = super::convert_text_layer(bytes, "x.pdf").unwrap_err();
3546            assert!(err.to_string().contains("not a readable PDF"), "{err}");
3547        }
3548        let real = std::fs::read(
3549            std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
3550                .join("../../tests/data/pdf/sources/multi_page.pdf"),
3551        )
3552        .unwrap();
3553        assert!(!super::convert_text_layer(&real, "multi_page.pdf")
3554            .unwrap()
3555            .nodes
3556            .is_empty());
3557    }
3558}
3559
3560#[cfg(test)]
3561mod send_check {
3562    /// The Node bindings (`docling-node`) run a shared [`super::Pipeline`] on
3563    /// libuv worker threads (`Arc<Mutex<Pipeline>>`), which is only sound while
3564    /// `Pipeline: Send` holds — this fails to compile if a non-`Send` field
3565    /// (e.g. an `Rc` or a raw pdfium handle) ever lands in the pipeline.
3566    fn assert_send<T: Send>() {}
3567
3568    #[test]
3569    fn pipeline_is_send() {
3570        assert_send::<super::Pipeline>();
3571    }
3572}
3573
3574#[cfg(all(test, feature = "ml"))]
3575mod ocr_input_tests {
3576    /// #254: without an `ocr_scale` (or with one equal to the render scale)
3577    /// the OCR reads the page render untouched and the cache stays cold; a
3578    /// different scale builds one resampled view, reuses it across calls, and
3579    /// reports the requested px/pt so cell geometry divides back to points.
3580    #[test]
3581    fn ocr_input_resamples_only_on_a_real_scale_change() {
3582        let img = image::RgbImage::new(200, 100);
3583        let mut cache = None;
3584        let (v, s) = super::ocr_input(&mut cache, &img, 2.0, None);
3585        assert!(std::ptr::eq(v, &img) && s == 2.0 && cache.is_none());
3586        let (v, s) = super::ocr_input(&mut cache, &img, 2.0, Some(2.0));
3587        assert!(std::ptr::eq(v, &img) && s == 2.0 && cache.is_none());
3588
3589        let (v, s) = super::ocr_input(&mut cache, &img, 2.0, Some(3.0));
3590        assert_eq!((v.width(), v.height(), s), (300, 150, 3.0));
3591        let first = cache.as_ref().map(|c| c as *const image::RgbImage);
3592        let (v, _) = super::ocr_input(&mut cache, &img, 2.0, Some(3.0));
3593        assert_eq!(
3594            Some(v as *const image::RgbImage),
3595            first,
3596            "cached, not rebuilt"
3597        );
3598
3599        let mut down = None;
3600        let (v, s) = super::ocr_input(&mut down, &img, 2.0, Some(1.0));
3601        assert_eq!((v.width(), v.height(), s), (100, 50, 1.0));
3602    }
3603}