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;
23mod dp_lines;
24#[cfg(feature = "ml")]
25pub mod enrich;
26// Public so sibling crates (e.g. docling-rag's ONNX embedder) can route their
27// own `ort` sessions through the same `DOCLING_RS_EP` selection.
28#[cfg(feature = "ml")]
29pub mod ep;
30pub mod layout;
31#[cfg(feature = "ml")]
32mod mets;
33#[cfg(feature = "ml")]
34mod ocr;
35#[cfg(feature = "ocr-prep")]
36pub mod ocr_prep;
37pub mod pdfium_backend;
38#[cfg(feature = "ml")]
39pub mod quality;
40mod reading_order;
41// Pure-Rust region resampling (page→1024px box-average, crop→448 bilinear) —
42// available to the browser TableFormer path (#157 stage 3), not just `ml`.
43#[cfg(feature = "ocr-prep")]
44pub mod resample;
45#[cfg(feature = "ocr-prep")]
46pub mod scanned;
47// Built-in standard-14 font metrics for the pure-Rust text parser (#187) —
48// no feature gate: the wasm/pdf-text path needs them like the native one.
49mod std14;
50#[cfg(feature = "ml")]
51pub mod tableformer;
52pub mod textparse;
53#[cfg(feature = "ocr-prep")]
54pub mod tf_core;
55// docling's TableFormer cell matcher — pure Rust, shared with the browser
56// TableFormer path (#157 stage 3).
57#[cfg(feature = "ocr-prep")]
58pub mod tf_match;
59pub mod timing;
60
61#[cfg(feature = "ml")]
62use std::collections::BTreeMap;
63use std::fmt;
64#[cfg(feature = "ml")]
65use std::sync::mpsc::{sync_channel, Receiver};
66#[cfg(feature = "ml")]
67use std::sync::{Arc, Mutex};
68
69use docling_core::DoclingDocument;
70#[cfg(feature = "ml")]
71use docling_core::Node;
72
73#[cfg(feature = "ml")]
74pub use mets::{convert_mets_gbs, convert_mets_gbs_with_options};
75#[cfg(feature = "ml")]
76pub use ocr::OcrLang;
77#[cfg(feature = "ml")]
78pub use pdfium_backend::PdfDocument;
79pub use pdfium_backend::{PdfPage, TextCell};
80
81/// Errors from the PDF backend. Detailed and surfaced (never silently skipped).
82#[derive(Debug)]
83pub enum PdfError {
84    /// pdfium failed to bind, open, or read the document.
85    Pdfium(String),
86    /// The layout ONNX model failed to load or run.
87    Layout(String),
88    /// The OCR ONNX model failed to load or run.
89    Ocr(String),
90}
91
92impl fmt::Display for PdfError {
93    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94        match self {
95            PdfError::Pdfium(m) => write!(f, "pdf: pdfium error: {m}"),
96            PdfError::Layout(m) => write!(f, "pdf: {m}"),
97            PdfError::Ocr(m) => write!(f, "pdf: {m}"),
98        }
99    }
100}
101
102impl std::error::Error for PdfError {}
103
104#[cfg(feature = "ml")]
105impl From<pdfium_render::prelude::PdfiumError> for PdfError {
106    fn from(e: pdfium_render::prelude::PdfiumError) -> Self {
107        PdfError::Pdfium(e.to_string())
108    }
109}
110
111/// Convert a PDF's **embedded text layer only** — no pdfium, no ONNX, no
112/// threads: the pure-Rust content-stream parser ([`textparse`]) feeds the same
113/// orphan-region assembly the `no_ocr` pipeline flag uses, so text-layer PDFs
114/// come out identical to `--no-ocr` (flat, line-grouped paragraphs in reading
115/// order; no headings/lists/tables/pictures, and no hyperlink recovery).
116///
117/// This is the only conversion entry compiled without the `ml` feature (it is
118/// what a wasm32 build runs). A scanned/image-only PDF (no embedded text
119/// layer) yields an empty document rather than an error, same as `no_ocr` —
120/// callers can detect that and fall back to an OCR-capable build.
121pub fn convert_text_layer(bytes: &[u8], name: &str) -> Result<DoclingDocument, PdfError> {
122    convert_text_layer_pages(bytes, name, None)
123}
124
125/// [`convert_text_layer`] restricted to a **1-based inclusive** page window
126/// (issue #80's `--pages`); `None` converts everything. The window is
127/// validated the same way as [`Pipeline::pages`]: `first <= last`, 1-based,
128/// and it must select at least one existing page.
129pub fn convert_text_layer_pages(
130    bytes: &[u8],
131    name: &str,
132    pages: Option<(usize, usize)>,
133) -> Result<DoclingDocument, PdfError> {
134    if let Some((first, last)) = pages {
135        if first == 0 || last < first {
136            return Err(PdfError::Pdfium(format!(
137                "invalid page range {first}-{last} (pages are 1-based, first <= last)"
138            )));
139        }
140    }
141    let mut doc = DoclingDocument::new(name);
142    let mut total = 0usize;
143    let parsed = textparse::pdf_text_pages(bytes);
144    // A vestigial layer (a few typed-in form fields over scanned pages) is not
145    // the document's text: return the empty document, which callers already
146    // report as "no text layer" — so an OCR-capable caller falls back to OCR
147    // instead of proudly extracting thirteen characters.
148    if textparse::text_layer_is_vestigial(&parsed) {
149        return Ok(doc);
150    }
151    for (i, page) in parsed.into_iter().enumerate() {
152        total += 1;
153        if let Some((first, last)) = pages {
154            if i + 1 < first || i + 1 > last {
155                continue;
156            }
157        }
158        let mut regions = Vec::new();
159        assemble::add_orphan_regions(&mut regions, &page.cells);
160        let table_rows = vec![None; regions.len()];
161        let enrich_out = vec![None; regions.len()];
162        let (mut nodes, links) = assemble::assemble_page(&page, regions, &table_rows, &enrich_out);
163        assemble::stamp_page_no(&mut nodes, i + 1);
164        doc.nodes.extend(nodes);
165        doc.links.extend(links);
166    }
167    if let Some((first, last)) = pages {
168        if first > total {
169            return Err(PdfError::Pdfium(format!(
170                "page range {first}-{last} is outside the document ({total} page(s))"
171            )));
172        }
173    }
174    assemble::merge_continuations(&mut doc.nodes);
175    Ok(doc)
176}
177
178/// Threads ONNX inference may use, capped by `DOCLING_RS_PDF_THREADS` if set.
179/// Defaults to the available parallelism (ort otherwise picks a low number).
180#[cfg(feature = "ml")]
181pub(crate) fn intra_threads() -> usize {
182    if let Some(n) = std::env::var("DOCLING_RS_PDF_THREADS")
183        .ok()
184        .and_then(|v| v.parse::<usize>().ok())
185        .filter(|&n| n > 0)
186    {
187        return n;
188    }
189    std::thread::available_parallelism()
190        .map(|n| n.get())
191        .unwrap_or(1)
192}
193
194#[cfg(feature = "ml")]
195/// True when `DOCLING_RS_FP32` (any value but `0`) forces the full-precision
196/// models even where an INT8 variant sits next to the fp32 default.
197pub(crate) fn fp32_forced() -> bool {
198    std::env::var("DOCLING_RS_FP32")
199        .map(|v| v != "0")
200        .unwrap_or(false)
201}
202
203#[cfg(feature = "ml")]
204/// Should the int8 model defaults be skipped in favor of fp32? Either the
205/// user said so (`DOCLING_RS_FP32`), or a GPU execution provider is selected
206/// (#74) — the int8 exports are QDQ graphs calibrated for CPU kernels and
207/// only conformance-validated there. An explicit `DOCLING_*_ONNX` path
208/// override still wins over this at every call site.
209pub(crate) fn prefer_fp32() -> bool {
210    fp32_forced() || ep::prefers_fp32()
211}
212
213#[cfg(feature = "ml")]
214/// Resolve a default (CWD-relative) asset path. If it doesn't exist relative
215/// to the current directory, try next to the executable and one level above
216/// it (following symlinks — the layout `scripts/install/install.sh` produces:
217/// `/usr/local/bin/docling-rs` → `/usr/local/docling.rs/bin/docling-rs`
218/// with `models/` and `.pdfium/` in `/usr/local/docling.rs`). Lets an
219/// installed binary run from any working directory with no env vars; explicit
220/// env overrides never reach this. Returns `rel` unchanged when nothing
221/// exists anywhere, so callers' error messages keep the familiar path.
222pub(crate) fn resolve_asset(rel: &str) -> String {
223    if std::path::Path::new(rel).exists() {
224        return rel.to_string();
225    }
226    if let Some(dir) = std::env::current_exe()
227        .ok()
228        .and_then(|p| p.canonicalize().ok())
229        .and_then(|p| p.parent().map(std::path::Path::to_path_buf))
230    {
231        for base in [Some(dir.as_path()), dir.parent()].into_iter().flatten() {
232            let p = base.join(rel);
233            if p.exists() {
234                return p.to_string_lossy().into_owned();
235            }
236        }
237    }
238    rel.to_string()
239}
240
241/// One resolved runtime asset — which file a stage would load right now,
242/// given the CWD, the env overrides and the int8/fp32 preference.
243#[cfg(feature = "ml")]
244#[derive(Debug, Clone)]
245pub struct ModelEntry {
246    /// Pipeline stage, e.g. `layout`, `tableformer.decoder`, `ocr.rec`.
247    pub stage: &'static str,
248    /// The resolved path (absolute or CWD-relative, as it will be opened).
249    pub path: String,
250    /// Whether the file exists right now.
251    pub found: bool,
252    /// File size in bytes (0 when missing) — enough to tell an int8 quant
253    /// from an fp32 graph, or a stale model from a re-published one, at a
254    /// glance without hashing gigabytes per request.
255    pub bytes: u64,
256}
257
258/// Resolve the whole runtime model set **without loading anything** — the
259/// exact selection each stage performs at load time (layout honors the
260/// int8/fp32 preference, TableFormer its decoder ranking, OCR the language
261/// pair), plus the pdfium library. docling-serve exposes this at
262/// `/v1/config` and logs it at startup, so "the server picked up different
263/// models" is one `curl` away instead of a mystery of dissolved tables.
264/// Resolution is CWD-relative with an exe-dir fallback, so the answer can
265/// legitimately differ between two working directories.
266#[cfg(feature = "ml")]
267pub fn model_inventory() -> Vec<ModelEntry> {
268    fn entry(stage: &'static str, path: String) -> ModelEntry {
269        let meta = std::fs::metadata(&path).ok();
270        ModelEntry {
271            stage,
272            found: meta.is_some(),
273            bytes: meta.map(|m| m.len()).unwrap_or(0),
274            path,
275        }
276    }
277    let (enc, dec, bbx) = tableformer::resolved_paths();
278    let (rec, dict) = ocr::resolve_rec_pair(ocr::OcrLang::from_env());
279    let pdfium =
280        std::env::var("PDFIUM_DYNAMIC_LIB_PATH").unwrap_or_else(|_| resolve_asset(".pdfium/lib"));
281    vec![
282        entry(
283            "layout",
284            model_path(
285                "DOCLING_LAYOUT_ONNX",
286                "models/layout_heron.onnx",
287                "models/layout_heron_int8.onnx",
288            ),
289        ),
290        entry("tableformer.encoder", enc),
291        entry("tableformer.decoder", dec),
292        entry("tableformer.bbox", bbx),
293        entry("ocr.rec", rec),
294        entry("ocr.dict", dict),
295        entry("pdfium", pdfium),
296    ]
297}
298
299/// Resolve a model path: an explicit env override always wins; otherwise the
300/// INT8 variant of the default path when it exists on disk (the quantized
301/// models are conformance-validated — see docs/PDF_CONFORMANCE.md — and load/run
302/// markedly faster on CPU), unless `DOCLING_RS_FP32` opts back into full
303/// precision; else the fp32 default.
304#[cfg(feature = "ml")]
305pub(crate) fn model_path(env: &str, fp32_default: &str, int8_default: &str) -> String {
306    if let Ok(p) = std::env::var(env) {
307        return p;
308    }
309    if !prefer_fp32() {
310        let p = resolve_asset(int8_default);
311        if std::path::Path::new(&p).exists() {
312            return p;
313        }
314    }
315    resolve_asset(fp32_default)
316}
317
318/// Decode a standalone image with hard resource limits. A crafted image can
319/// declare enormous dimensions in a few-KB file; `image::load_from_memory`
320/// then tries to allocate the full pixel buffer (e.g. 60000×60000 → ~10 GB),
321/// and allocation failure aborts the whole process, bypassing the per-request
322/// panic catch. The 256 MiB alloc / 30000-px caps below turn that into a
323/// recoverable decode error instead. `DOCLING_RS_MAX_IMAGE_PIXELS` overrides
324/// the per-side pixel cap for the rare legitimately-huge scan.
325///
326/// Gated on `ml`: the only callers (`convert_image`, the METS backend) are
327/// ML-only, and the `image` crate is an `ml`-feature dependency — the
328/// text-layer wasm build has neither.
329#[cfg(feature = "ml")]
330pub(crate) fn decode_image_limited(bytes: &[u8]) -> Result<image::RgbImage, PdfError> {
331    let max_side: u32 = std::env::var("DOCLING_RS_MAX_IMAGE_PIXELS")
332        .ok()
333        .and_then(|v| v.parse().ok())
334        .unwrap_or(30_000);
335    decode_image_with_max_side(bytes, max_side)
336}
337
338#[cfg(feature = "ml")]
339fn decode_image_with_max_side(bytes: &[u8], max_side: u32) -> Result<image::RgbImage, PdfError> {
340    use image::ImageReader;
341    use std::io::Cursor;
342
343    let mut limits = image::Limits::default();
344    limits.max_image_width = Some(max_side);
345    limits.max_image_height = Some(max_side);
346    limits.max_alloc = Some(256 * 1024 * 1024);
347
348    let mut reader = ImageReader::new(Cursor::new(bytes))
349        .with_guessed_format()
350        .map_err(|e| PdfError::Pdfium(format!("image: {e}")))?;
351    reader.limits(limits);
352    Ok(reader
353        .decode()
354        .map_err(|e| PdfError::Pdfium(format!("image: {e}")))?
355        .into_rgb8())
356}
357
358#[cfg(feature = "ml")]
359/// One page's assembled output: typed nodes plus the page's hyperlinks (kept
360/// separate so pages processed out of order can be stitched back in page
361/// order) and its confidence scores (#183).
362type PageOut = (
363    Vec<Node>,
364    Vec<(String, String)>,
365    docling_core::confidence::PageConfidence,
366);
367
368#[cfg(feature = "ml")]
369/// The pool-wide TableFormer slot: one instance shared by every worker, loaded
370/// lazily on the first table region any worker sees. Tables appear on a
371/// minority of pages, so per-worker copies mostly multiplied ~0.4 GB of
372/// weights+arenas by the pool size for nothing; a single shared instance keeps
373/// the peak flat regardless of pool width, and a table's structure prediction
374/// is independent of which worker runs it, so output is byte-identical. The
375/// mutex serialises concurrent tables — the shared instance is loaded with the
376/// full intra-op thread budget to compensate (one wide TableFormer instead of
377/// several narrow ones).
378enum TfSlot {
379    /// Not attempted yet (no table seen so far).
380    Unloaded,
381    /// Load attempted, graphs absent — geometric fallback (warned once).
382    Missing,
383    Ready(tableformer::TableFormer),
384}
385
386#[cfg(feature = "ml")]
387type SharedTables = Arc<Mutex<TfSlot>>;
388
389#[cfg(feature = "ml")]
390/// The same lazy shared-slot pattern for the (rarer still) enrichment models:
391/// one instance per pipeline, loaded on the first region that needs it.
392enum EnrichSlot<T> {
393    Unloaded,
394    /// Load attempted, model files absent — enrichment skipped (warned once).
395    Missing,
396    Ready(T),
397}
398
399#[cfg(feature = "ml")]
400type SharedClassifier = Arc<Mutex<EnrichSlot<enrich::PictureClassifier>>>;
401#[cfg(feature = "ml")]
402type SharedCodeFormula = Arc<Mutex<EnrichSlot<enrich::CodeFormula>>>;
403
404#[cfg(feature = "ml")]
405/// The opt-in enrichment passes, mirroring docling's `PdfPipelineOptions`
406/// flags (`do_picture_classification`, `do_code_enrichment`,
407/// `do_formula_enrichment`). All off by default.
408#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
409pub struct EnrichmentOptions {
410    /// Classify each picture with DocumentFigureClassifier (26 classes).
411    pub picture_classification: bool,
412    /// Rewrite code blocks (and detect their language) with CodeFormulaV2.
413    pub code: bool,
414    /// Decode display formulas to LaTeX with CodeFormulaV2.
415    pub formula: bool,
416}
417
418#[cfg(feature = "ml")]
419impl EnrichmentOptions {
420    fn any(&self) -> bool {
421        self.picture_classification || self.code || self.formula
422    }
423}
424
425#[cfg(feature = "ml")]
426/// The layout model's input for a page: the docling-exact scale-1.0 page
427/// image when the renderer produced one, else the legacy stretch of the 2×
428/// bitmap (browser / METS paths) — see [`layout::LayoutSrc`]. Public so the
429/// diagnostic examples feed [`layout::LayoutModel::predict`] the same input
430/// the pipeline does.
431pub fn layout_src(page: &PdfPage) -> layout::LayoutSrc<'_> {
432    match &page.image_layout {
433        Some(img) => layout::LayoutSrc::PageImage(img),
434        None => layout::LayoutSrc::Raw(&page.image),
435    }
436}
437
438#[cfg(feature = "ml")]
439/// A self-contained set of the per-page models (layout, OCR). Each parallel
440/// page-worker owns its own `Worker` so inference runs concurrently without
441/// sharing an ONNX session (`ort`'s `Session::run` is `&mut self`); only the
442/// rarely-hit TableFormer is shared (see [`TfSlot`]).
443struct Worker {
444    /// `None` when `no_ocr` skips layout entirely — no model load, no inference.
445    layout: Option<layout::LayoutModel>,
446    ocr: Option<ocr::OcrModel>,
447    /// Shared TableFormer slot; `None` when `no_table_former`/`no_ocr` skip it.
448    tables: Option<SharedTables>,
449    /// Shared enrichment slots; `None` unless the corresponding flag is on.
450    classifier: Option<SharedClassifier>,
451    code_formula: Option<SharedCodeFormula>,
452    enrich: EnrichmentOptions,
453    /// Skip layout, OCR, and TableFormer; reconstruct text purely from the PDF's
454    /// embedded text layer. See [`Pipeline::no_ocr`].
455    no_ocr: bool,
456    /// Discard the embedded text layer and OCR every page. See
457    /// [`Pipeline::force_full_page_ocr`].
458    force_full_page_ocr: bool,
459    /// Keep text-panel pictures as pictures instead of demoting them to
460    /// paragraphs. See [`Pipeline::no_text_panels`].
461    no_text_panels: bool,
462    /// Which recognition model [`Self::ocr`] loads. See [`Pipeline::ocr_lang`].
463    ocr_lang: ocr::OcrLang,
464}
465
466#[cfg(feature = "ml")]
467impl Worker {
468    #[allow(clippy::too_many_arguments)] // mirrors the Pipeline's option set
469    fn load(
470        intra: usize,
471        tables: Option<SharedTables>,
472        enrich_slots: (Option<SharedClassifier>, Option<SharedCodeFormula>),
473        enrich: EnrichmentOptions,
474        no_ocr: bool,
475        force_full_page_ocr: bool,
476        no_text_panels: bool,
477        ocr_lang: ocr::OcrLang,
478    ) -> Result<Self, PdfError> {
479        Ok(Self {
480            layout: if no_ocr {
481                None
482            } else {
483                Some(layout::LayoutModel::load_with(intra).map_err(PdfError::Layout)?)
484            },
485            ocr: None,
486            tables,
487            classifier: enrich_slots.0,
488            code_formula: enrich_slots.1,
489            enrich,
490            no_ocr,
491            force_full_page_ocr,
492            no_text_panels,
493            ocr_lang,
494        })
495    }
496
497    /// Run layout (+ OCR for cell-less pages) + TableFormer and assemble page `n`
498    /// into its nodes and links. Pure given the page (mutates only the worker's
499    /// lazily-loaded OCR model), so it is safe to run concurrently across pages.
500    fn process(&mut self, n: usize, page: &mut PdfPage) -> Result<PageOut, PdfError> {
501        if self.no_ocr {
502            // Fastest path: no layout/OCR/TableFormer inference at all. The PDF's
503            // embedded text cells (if any) become flat, line-grouped paragraphs in
504            // reading order via the same orphan-region machinery that normally
505            // rescues text the detector missed — here it rescues *all* of it.
506            // Pages with no embedded text layer (scanned/image-only) yield nothing;
507            // convert those without `no_ocr`.
508            let parse = quality::parse_score(&page.cells);
509            let mut regions = Vec::new();
510            assemble::add_orphan_regions(&mut regions, &page.cells);
511            let table_rows = vec![None; regions.len()];
512            let enrich_out = vec![None; regions.len()];
513            let conf = quality::page_confidence(parse, &regions, &[]);
514            let (nodes, links) = timing::timed("assemble_page", || {
515                assemble::assemble_page(page, regions, &table_rows, &enrich_out)
516            });
517            return Ok((nodes, links, conf));
518        }
519        let regions = timing::timed("layout.predict", || {
520            self.layout
521                .as_mut()
522                .expect("layout model loaded unless no_ocr")
523                .predict(layout_src(page), page.width, page.height)
524        })
525        .map_err(|e| PdfError::Layout(format!("page {}: {e}", n + 1)))?;
526        self.finish_page(n, page, regions)
527    }
528
529    /// Layout-detect a whole batch of pages with one inference call (issue #73),
530    /// then run each page's remaining stages (OCR / TableFormer / enrichment /
531    /// assembly) per page. Index-aligned with `items`; a layout failure fails
532    /// every page in the batch (they shared the one inference call).
533    fn process_batch(&mut self, items: &mut [(usize, PdfPage)]) -> Vec<Result<PageOut, PdfError>> {
534        if self.no_ocr {
535            // No layout model to batch — the text-layer-only path is per page.
536            return items
537                .iter_mut()
538                .map(|(n, page)| {
539                    let n = *n;
540                    self.process(n, page)
541                })
542                .collect();
543        }
544        let inputs: Vec<(layout::LayoutSrc<'_>, f32, f32)> = items
545            .iter()
546            .map(|(_, page)| (layout_src(page), page.width, page.height))
547            .collect();
548        let batched = timing::timed("layout.predict", || {
549            self.layout
550                .as_mut()
551                .expect("layout model loaded unless no_ocr")
552                .predict_batch(&inputs)
553        });
554        match batched {
555            Ok(all) => items
556                .iter_mut()
557                .zip(all)
558                .map(|((n, page), regions)| self.finish_page(*n, page, regions))
559                .collect(),
560            Err(e) => items
561                .iter()
562                .map(|(n, _)| Err(PdfError::Layout(format!("page {}: {e}", n + 1))))
563                .collect(),
564        }
565    }
566
567    /// Everything after layout detection: per-label confidence thresholds,
568    /// overlap resolution, orphan-text recovery, OCR for cell-less pages,
569    /// TableFormer, enrichment, and page assembly.
570    fn finish_page(
571        &mut self,
572        n: usize,
573        page: &mut PdfPage,
574        regions: Vec<layout::Region>,
575    ) -> Result<PageOut, PdfError> {
576        // Force-OCR is exactly "pretend the text layer is not there": clear
577        // every cell kind the extractors produced before anything reads them,
578        // and the ordinary no-text-layer machinery below — full-page OCR,
579        // OCR-fed TableFormer matching — takes over unchanged. (`no_ocr` wins
580        // when both are set, mirroring docling, where `force_full_page_ocr`
581        // is a sub-option of `do_ocr`; the no-ocr path never reaches here.)
582        // Done here rather than in `process` so the batched layout path
583        // (`process_batch` → `finish_page`) honors the flag too.
584        // Parse quality is scored on the extracted text layer before force-OCR
585        // discards it (docling's page-preprocessing stage runs before OCR too,
586        // so its parse_score also reflects the original text layer).
587        let parse = quality::parse_score(&page.cells);
588        // Recognition confidences of every OCR'd cell on this page → ocr_score.
589        let mut ocr_confs: Vec<f32> = Vec::new();
590        if self.force_full_page_ocr {
591            page.cells.clear();
592            page.code_cells.clear();
593            page.word_cells.clear();
594        }
595        // Quant-robustness guard: the default int8 layout graph keeps its
596        // confidences near the 0.5 label thresholds, and a different CPU's
597        // quantized kernels can flip a whole page's detections under them —
598        // tables and paragraphs then dissolve into orphan one-liners while the
599        // same build converts the page perfectly elsewhere. When a dense
600        // digital page ends up with detections covering almost none of its
601        // text cells, re-run that one page on the fp32 graph (lazy-loaded,
602        // auto-int8 selection only) and keep whichever detections cover more.
603        let mut regions = regions;
604        if !page.cells.is_empty() {
605            let thresholded = |rs: &[layout::Region]| -> Vec<layout::Region> {
606                rs.iter()
607                    .filter(|r| r.score >= layout::label_threshold(r.label))
608                    .cloned()
609                    .collect()
610            };
611            let text_cells = page
612                .cells
613                .iter()
614                .filter(|c| !c.text.trim().is_empty())
615                .count();
616            let cov = assemble::layout_cell_coverage(&thresholded(&regions), &page.cells);
617            if text_cells >= 15 && cov < 0.5 {
618                let retry = self
619                    .layout
620                    .as_mut()
621                    .expect("layout model loaded unless no_ocr")
622                    .predict_fp32_fallback(layout_src(page), page.width, page.height)
623                    .map_err(|e| PdfError::Layout(format!("page {}: {e}", n + 1)))?;
624                if let Some(retry) = retry {
625                    let cov2 = assemble::layout_cell_coverage(&thresholded(&retry), &page.cells);
626                    if cov2 > cov {
627                        eprintln!(
628                            "docling-pdf: page {}: int8 layout covered {:.0}% of the text \
629                             cells; the fp32 retry covers {:.0}% — using it",
630                            n + 1,
631                            cov * 100.0,
632                            cov2 * 100.0
633                        );
634                        regions = retry;
635                    }
636                }
637            }
638        }
639        // docling's LayoutPostprocessor drops each detection below its label's
640        // confidence threshold (stricter than the 0.3 base the predictor keeps),
641        // before any overlap resolution. This removes the low-confidence tables /
642        // pictures / list-items that otherwise double-emit or mis-classify.
643        if std::env::var("DOCLING_RS_DEBUG_REGIONS").is_ok() {
644            for r in &regions {
645                eprintln!(
646                    "DBG raw {} {:.2} [{:.0},{:.0},{:.0},{:.0}]",
647                    r.label, r.score, r.l, r.t, r.r, r.b
648                );
649            }
650        }
651        regions.retain(|r| r.score >= layout::label_threshold(r.label));
652        // docling's same-label picture dedup runs on the thresholded
653        // detections, before overlap resolution: a figure proposed both whole
654        // and as sub-panels collapses to one box (see `dedup_pictures`).
655        assemble::dedup_pictures(&mut regions);
656        // Resolve overlapping detections once, before OCR.
657        let mut regions = assemble::resolve(regions);
658        // Emit text the detector missed as orphan text regions (docling parity).
659        assemble::add_orphan_regions(&mut regions, &page.cells);
660        // Drop phantom empty low-confidence picture boxes (docling parity).
661        assemble::drop_false_pictures(&mut regions, &page.cells, page.width, page.height);
662        // A regular region fully inside a surviving table/index/picture is that
663        // special's child (a cell / in-figure label), not a separate block —
664        // remove it so it isn't emitted twice (docling parity).
665        assemble::drop_contained_regulars(&mut regions);
666        // No text layer → recognise text from the page image via OCR.
667        let ocred = page.cells.is_empty();
668        if ocred {
669            if self.ocr.is_none() {
670                self.ocr = Some(ocr::OcrModel::load(self.ocr_lang).map_err(PdfError::Ocr)?);
671            }
672            let cells = timing::timed("ocr.page", || {
673                self.ocr
674                    .as_mut()
675                    .unwrap()
676                    .ocr_page(&page.image, &regions, page.scale)
677            })
678            .map_err(|e| PdfError::Ocr(format!("page {}: {e}", n + 1)))?;
679            ocr_confs.extend(cells.iter().map(|(_, conf)| conf));
680            page.cells = cells.into_iter().map(|(cell, _)| cell).collect();
681            // Table interiors carry no words yet: region-scoped OCR skips
682            // table labels, and a scanned page has no pdfium text layer — so
683            // TableFormer's cell matcher got an empty word list and the table
684            // dissolved (#173). Recognize the table regions' word crops
685            // (mirroring the browser scanned path): `word_cells` feeds the
686            // matcher, and the same cells join `cells` so the geometric
687            // fallback and the table's region text see them too.
688            if regions.iter().any(|r| assemble::is_table_like(r.label)) {
689                let words = timing::timed("ocr.table_words", || {
690                    self.ocr
691                        .as_mut()
692                        .unwrap()
693                        .ocr_table_words(&page.image, &regions, page.scale)
694                })
695                .map_err(|e| PdfError::Ocr(format!("page {}: {e}", n + 1)))?;
696                ocr_confs.extend(words.iter().map(|(_, conf)| conf));
697                let words: Vec<_> = words.into_iter().map(|(cell, _)| cell).collect();
698                page.cells.extend(words.iter().cloned());
699                page.word_cells = words;
700            }
701        }
702        // Region-scoped OCR skips `picture` interiors, and a digital page's
703        // text layer cannot see into an embedded raster either — so a figure
704        // that is really a text box (terms-and-conditions exported as an
705        // image) lost its words on every page kind. Python docling OCRs the
706        // bitmap-covered areas of *every* page — even digital ones — once they
707        // exceed `bitmap_area_threshold` (5 % of the page); the browser paths
708        // already do. Recognize the big text-less crops here too; the panel
709        // demotion / orphan recovery below place the lines.
710        let mut pic_cells: Vec<pdfium_backend::TextCell> = Vec::new();
711        {
712            let page_area = (page.width * page.height).max(1.0);
713            let has_text = |r: &layout::Region| {
714                page.cells.iter().any(|c| {
715                    let ca = ((c.r - c.l) * (c.b - c.t)).max(1.0);
716                    let ix = (r.r.min(c.r) - r.l.max(c.l)).max(0.0);
717                    let iy = (r.b.min(c.b) - r.t.max(c.t)).max(0.0);
718                    !c.text.trim().is_empty() && ix * iy / ca > 0.5
719                })
720            };
721            // A captioned picture can never demote to a text panel (see
722            // recover_text_panels), and on digital pages its speculative OCR
723            // would be discarded anyway — don't pay for it.
724            let captioned = |r: &layout::Region| {
725                regions.iter().any(|c| {
726                    c.label == "caption"
727                        && c.r.min(r.r) - c.l.max(r.l) > 0.0
728                        && ((c.t >= r.b && c.t - r.b <= 25.0) || (r.t >= c.b && r.t - c.b <= 25.0))
729                })
730            };
731            let bare: Vec<layout::Region> = regions
732                .iter()
733                .filter(|r| {
734                    r.label == "picture"
735                        && (r.r - r.l) * (r.b - r.t) / page_area >= 0.05
736                        && !has_text(r)
737                        && (ocred || !captioned(r))
738                })
739                .map(|r| layout::Region {
740                    label: "text",
741                    ..r.clone()
742                })
743                .collect();
744            if !bare.is_empty() {
745                if self.ocr.is_none() {
746                    self.ocr = Some(ocr::OcrModel::load(self.ocr_lang).map_err(PdfError::Ocr)?);
747                }
748                let scored = timing::timed("ocr.pictures", || {
749                    self.ocr
750                        .as_mut()
751                        .unwrap()
752                        .ocr_page(&page.image, &bare, page.scale)
753                })
754                .map_err(|e| PdfError::Ocr(format!("page {}: {e}", n + 1)))?;
755                // Speculative in-picture OCR counts toward ocr_score only on
756                // OCR'd pages, where the recognized lines actually join the
757                // output; on a digital page they may be discarded below.
758                if ocred {
759                    ocr_confs.extend(scored.iter().map(|(_, conf)| conf));
760                }
761                pic_cells = scored.into_iter().map(|(cell, _)| cell).collect();
762                page.cells.extend(pic_cells.iter().cloned());
763            }
764        }
765        let cells_before_pic_ocr = page.cells.len() - pic_cells.len();
766        // A "picture" that is really a colored text panel — dense, wide,
767        // multi-line — reads out as paragraphs instead of shipping as pixels;
768        // sparse in-picture text (a chart's labels) keeps the crop and stays
769        // inside it as the picture's silent children (docling parity, #200).
770        // `no_text_panels` (#173) opts out entirely for image-extraction
771        // workflows.
772        if !self.no_text_panels {
773            assemble::recover_text_panels(&mut regions, &page.cells);
774        }
775        // On an OCR'd page, in-picture text that did NOT demote its picture
776        // mostly stays silent, exactly as in docling: its postprocess step
777        // "Remove regular clusters that are included in wrappers" walks
778        // SPECIAL_TYPES — which includes PICTURE — so an orphan text cluster
779        // >80 % contained in a kept picture becomes that picture's child and
780        // never reaches the serializer. Only border-straddlers (≤80 %
781        // containment) survive as text. Emitting *everything* here used to
782        // splice a chart's OCR'd axis ticks into the body text right next to
783        // the image chunk (#200) — so the orphan pass places the recognized
784        // lines, then the same containment drop that handled the first wave
785        // re-runs to swallow the in-picture ones.
786        if ocred && !pic_cells.is_empty() {
787            // Pictures (and wrappers) no longer count as claimers (#165), so
788            // the plain orphan pass places the recognized lines directly.
789            assemble::add_orphan_regions(&mut regions, &pic_cells);
790            assemble::drop_contained_regulars(&mut regions);
791        } else if !ocred && !pic_cells.is_empty() {
792            // Digital page, picture kept: its speculative OCR cells must not
793            // linger in the text-cell set (they were appended at the tail).
794            let kept: Vec<layout::Region> = regions
795                .iter()
796                .filter(|r| r.label == "picture")
797                .cloned()
798                .collect();
799            let tail = page.cells.split_off(cells_before_pic_ocr);
800            page.cells.extend(tail.into_iter().filter(|c| {
801                !kept.iter().any(|r| {
802                    let ca = ((c.r - c.l) * (c.b - c.t)).max(1.0);
803                    let ix = (r.r.min(c.r) - r.l.max(c.l)).max(0.0);
804                    let iy = (r.b.min(c.b) - r.t.max(c.t)).max(0.0);
805                    ix * iy / ca > 0.5
806                })
807            }));
808        }
809        // TableFormer structure per table region (else geometric fallback). The
810        // shared slot is only locked (and lazily loaded) when the page actually
811        // has a table, so table-free documents never pay for TableFormer at all.
812        let mut table_rows: Vec<Option<Vec<Vec<String>>>> = vec![None; regions.len()];
813        if let Some(slot) = self.tables.as_ref() {
814            if regions.iter().any(|r| assemble::is_table_like(r.label)) {
815                timing::timed("tableformer", || {
816                    let mut guard = slot.lock().unwrap();
817                    if matches!(*guard, TfSlot::Unloaded) {
818                        // Full intra-op width: tables serialise on this mutex, so
819                        // the one instance gets the whole thread budget.
820                        *guard = match tableformer::TableFormer::load_with(intra_threads()) {
821                            Some(tf) => TfSlot::Ready(tf),
822                            None => TfSlot::Missing,
823                        };
824                    }
825                    if let TfSlot::Ready(tf) = &mut *guard {
826                        for (i, r) in regions.iter().enumerate() {
827                            if assemble::is_table_like(r.label) {
828                                table_rows[i] = tf.predict_table_rows(
829                                    &page.image,
830                                    [r.l, r.t, r.r, r.b],
831                                    &page.word_cells,
832                                );
833                            }
834                        }
835                    }
836                });
837            }
838        }
839        if std::env::var("DOCLING_RS_DEBUG_REGIONS").is_ok() {
840            for (i, r) in regions.iter().enumerate() {
841                eprintln!(
842                    "DBG final {} {:.2} [{:.0},{:.0},{:.0},{:.0}] rows={:?}",
843                    r.label,
844                    r.score,
845                    r.l,
846                    r.t,
847                    r.r,
848                    r.b,
849                    table_rows[i]
850                        .as_ref()
851                        .map(|t| (t.len(), t.first().map(|r| r.len())))
852                );
853            }
854            eprintln!(
855                "DBG cells={} words={}",
856                page.cells.len(),
857                page.word_cells.len()
858            );
859        }
860        // Enrichment passes (opt-in): DocumentPictureClassifier over picture
861        // regions, CodeFormulaV2 over code/formula regions. Same shared-slot
862        // shape as TableFormer — one lazily-loaded instance per pipeline, only
863        // ever locked when a page actually has a matching region.
864        let mut enrich_out: Vec<Option<assemble::Enrichment>> = vec![None; regions.len()];
865        if let Some(slot) = self.classifier.as_ref() {
866            if regions.iter().any(|r| r.label == "picture") {
867                timing::timed("picture_classifier", || {
868                    let mut guard = slot.lock().unwrap();
869                    if matches!(*guard, EnrichSlot::Unloaded) {
870                        *guard = match enrich::PictureClassifier::load_with(intra_threads()) {
871                            Some(m) => EnrichSlot::Ready(m),
872                            None => EnrichSlot::Missing,
873                        };
874                    }
875                    if let EnrichSlot::Ready(model) = &mut *guard {
876                        for (i, r) in regions.iter().enumerate() {
877                            if r.label != "picture" {
878                                continue;
879                            }
880                            let Some(crop) = assemble::crop_region_scaled(
881                                page,
882                                [r.l, r.t, r.r, r.b],
883                                enrich::CLASSIFIER_SCALE,
884                            ) else {
885                                continue;
886                            };
887                            match model.classify(&crop) {
888                                Ok(classes) => {
889                                    enrich_out[i] =
890                                        Some(assemble::Enrichment::PictureClasses(classes));
891                                }
892                                Err(e) => eprintln!("docling-pdf: page {}: {e}", n + 1),
893                            }
894                        }
895                    }
896                });
897            }
898        }
899        if let Some(slot) = self.code_formula.as_ref() {
900            let wants = |label: &str| {
901                (label == "code" && self.enrich.code) || (label == "formula" && self.enrich.formula)
902            };
903            if regions.iter().any(|r| wants(r.label)) {
904                timing::timed("code_formula", || {
905                    let mut guard = slot.lock().unwrap();
906                    if matches!(*guard, EnrichSlot::Unloaded) {
907                        *guard = match enrich::CodeFormula::load_with(intra_threads()) {
908                            Some(m) => EnrichSlot::Ready(m),
909                            None => EnrichSlot::Missing,
910                        };
911                    }
912                    if let EnrichSlot::Ready(model) = &mut *guard {
913                        for (i, r) in regions.iter().enumerate() {
914                            if !wants(r.label) {
915                                continue;
916                            }
917                            // docling crops the postprocessed cluster box — the
918                            // union of the region's text cells, not the raw
919                            // detector box — expanded by 18% per side, at
920                            // ~120 dpi.
921                            let [bl, bt, br, bb] = assemble::region_cell_bbox(r, &page.cells)
922                                .unwrap_or([r.l, r.t, r.r, r.b]);
923                            let (w, h) = (br - bl, bb - bt);
924                            let ex = enrich::CODE_FORMULA_EXPANSION;
925                            let bbox = [bl - w * ex, bt - h * ex, br + w * ex, bb + h * ex];
926                            let Some(crop) = assemble::crop_region_scaled(
927                                page,
928                                bbox,
929                                enrich::CODE_FORMULA_SCALE,
930                            ) else {
931                                continue;
932                            };
933                            let kind = if r.label == "code" {
934                                enrich::CodeFormulaKind::Code
935                            } else {
936                                enrich::CodeFormulaKind::Formula
937                            };
938                            match model.predict(&crop, kind) {
939                                Ok(text) => {
940                                    enrich_out[i] = Some(match kind {
941                                        enrich::CodeFormulaKind::Code => {
942                                            let (code, language) =
943                                                enrich::extract_code_language(&text);
944                                            assemble::Enrichment::Code {
945                                                language,
946                                                text: code,
947                                            }
948                                        }
949                                        enrich::CodeFormulaKind::Formula => {
950                                            assemble::Enrichment::Formula { latex: text }
951                                        }
952                                    });
953                                }
954                                Err(e) => eprintln!("docling-pdf: page {}: {e}", n + 1),
955                            }
956                        }
957                    }
958                });
959            }
960        }
961        // Score the final region set (docling assigns layout_score over the
962        // postprocessed clusters — the same set assemble_page consumes).
963        let conf = quality::page_confidence(parse, &regions, &ocr_confs);
964        let (nodes, links) = timing::timed("assemble_page", || {
965            assemble::assemble_page(page, regions, &table_rows, &enrich_out)
966        });
967        Ok((nodes, links, conf))
968    }
969}
970
971#[cfg(feature = "ml")]
972/// Per-worker ONNX intra-op threads. The layout model is memory-bandwidth bound,
973/// so on a typical machine two threads per worker (sharing one in-cache copy of
974/// the weights) extracts more throughput than one fat model or many single-thread
975/// workers. `DOCLING_RS_PDF_INTRA` overrides for per-machine tuning.
976fn pdf_intra() -> usize {
977    if let Some(n) = std::env::var("DOCLING_RS_PDF_INTRA")
978        .ok()
979        .and_then(|v| v.parse::<usize>().ok())
980        .filter(|&n| n > 0)
981    {
982        return n;
983    }
984    if intra_threads() >= 2 {
985        2
986    } else {
987        1
988    }
989}
990
991#[cfg(feature = "ml")]
992/// How many page-workers to spin up for a multi-page PDF. `DOCLING_RS_PDF_WORKERS`
993/// overrides; otherwise size the pool so `workers × intra ≈ cores`, capped at 4 so
994/// a worst-case pool holds a bounded amount of model memory (~0.4 GB per worker)
995/// and does not oversaturate the memory bus with model-weight traffic.
996fn pdf_worker_count() -> usize {
997    if let Some(n) = std::env::var("DOCLING_RS_PDF_WORKERS")
998        .ok()
999        .and_then(|v| v.parse::<usize>().ok())
1000        .filter(|&n| n > 0)
1001    {
1002        return n;
1003    }
1004    (intra_threads() / pdf_intra()).clamp(1, 4)
1005}
1006
1007#[cfg(feature = "ml")]
1008/// Max pages a worker layout-detects with one batched inference call (issue
1009/// #73). Workers drain the work channel opportunistically up to this size —
1010/// whatever is already rendered gets batched, so batching never *waits* for
1011/// pages and adds no latency when rendering is the bottleneck.
1012///
1013/// Default: 4 on 8+ cores, 1 (per-page) below. Measured on a 4-core box the
1014/// batch only adds cache pressure and costs pipeline overlap (2 workers × 2
1015/// threads: 8.1 s/conv at batch=1 vs 9.3 s at batch=4 on the 9-page
1016/// 2206.01062 fixture); the single-session amortization it buys needs the
1017/// wider thread budget of a many-core machine. Output is bit-identical at
1018/// every batch size, so this is purely a throughput knob.
1019/// `DOCLING_RS_PDF_LAYOUT_BATCH` overrides; `1` restores per-page inference.
1020fn pdf_layout_batch() -> usize {
1021    std::env::var("DOCLING_RS_PDF_LAYOUT_BATCH")
1022        .ok()
1023        .and_then(|v| v.parse::<usize>().ok())
1024        .filter(|&n| n > 0)
1025        .unwrap_or_else(|| if intra_threads() >= 8 { 4 } else { 1 })
1026}
1027
1028#[cfg(feature = "ml")]
1029/// Minimum page count before a PDF is worth the parallel worker pool. Below this,
1030/// the serial primary (running its model on every core) is faster than fanning out
1031/// — the helper pool's one-time model-load cost only pays off once enough pages
1032/// share it. `DOCLING_RS_PDF_PARALLEL_MIN` overrides.
1033fn pdf_parallel_min() -> usize {
1034    std::env::var("DOCLING_RS_PDF_PARALLEL_MIN")
1035        .ok()
1036        .and_then(|v| v.parse::<usize>().ok())
1037        .filter(|&n| n > 0)
1038        .unwrap_or(6)
1039}
1040
1041#[cfg(feature = "ml")]
1042/// A reusable PDF pipeline. The **primary** worker runs its models on every core,
1043/// so a single-page / small / image / METS input is converted at full intra-op
1044/// speed with no pool to load. A document with enough pages instead fans out
1045/// across a **pool** of narrower workers processed concurrently. Both load lazily
1046/// and are cached for reuse, so a one-shot conversion only pays for what it uses.
1047pub struct Pipeline {
1048    /// Full-intra worker for the serial path; loaded on first serial use.
1049    primary: Option<Worker>,
1050    /// Narrower workers (≈cores/`target_workers` threads each) for the parallel
1051    /// path; loaded on first multi-page use and cached.
1052    pool: Vec<Worker>,
1053    /// The single TableFormer instance every worker shares (see [`TfSlot`]).
1054    tables: SharedTables,
1055    /// The shared enrichment-model slots (same pattern as [`TfSlot`]).
1056    classifier: SharedClassifier,
1057    code_formula: SharedCodeFormula,
1058    /// Desired pool size for multi-page documents.
1059    target_workers: usize,
1060    /// Page count at/above which the parallel pool is worth its load cost.
1061    parallel_min: usize,
1062    /// Skip loading/running TableFormer; table regions fall back to geometric
1063    /// reconstruction. See [`Pipeline::no_table_former`].
1064    no_table_former: bool,
1065    /// Skip layout, OCR, and TableFormer entirely. See [`Pipeline::no_ocr`].
1066    no_ocr: bool,
1067    /// OCR every page even when it carries a text layer. See
1068    /// [`Pipeline::force_full_page_ocr`].
1069    force_full_page_ocr: bool,
1070    /// Never demote text-panel pictures. See [`Pipeline::no_text_panels`].
1071    no_text_panels: bool,
1072    /// Opt-in enrichment passes. See [`Pipeline::enrichments`].
1073    enrich: EnrichmentOptions,
1074    /// 1-based inclusive page window to convert. See [`Pipeline::pages`].
1075    page_range: Option<(usize, usize)>,
1076    /// OCR recognition language. See [`Pipeline::ocr_lang`].
1077    ocr_lang: ocr::OcrLang,
1078}
1079
1080#[cfg(feature = "ml")]
1081impl Pipeline {
1082    /// Construct the pipeline. Models load lazily on first use (full-intra primary
1083    /// for serial inputs, the helper pool for multi-page PDFs), so nothing is
1084    /// loaded that a given document doesn't need.
1085    pub fn new() -> Result<Self, PdfError> {
1086        Ok(Self {
1087            primary: None,
1088            pool: Vec::new(),
1089            tables: Arc::new(Mutex::new(TfSlot::Unloaded)),
1090            classifier: Arc::new(Mutex::new(EnrichSlot::Unloaded)),
1091            code_formula: Arc::new(Mutex::new(EnrichSlot::Unloaded)),
1092            target_workers: pdf_worker_count(),
1093            parallel_min: pdf_parallel_min(),
1094            no_table_former: false,
1095            no_ocr: false,
1096            force_full_page_ocr: false,
1097            no_text_panels: false,
1098            enrich: EnrichmentOptions::default(),
1099            page_range: None,
1100            ocr_lang: ocr::OcrLang::from_env(),
1101        })
1102    }
1103
1104    /// Convert only pages `first..=last` (**1-based**, like the page numbers a
1105    /// PDF viewer shows — issue #80's `--pages A-B`). Out-of-range pages are
1106    /// skipped before rasterization, so the cost is proportional to the window,
1107    /// not the document. `last` past the end of the document clamps; a window
1108    /// that selects no pages at all (`first` beyond the last page) is an error
1109    /// at convert time. `None` (the default) converts everything.
1110    pub fn pages(mut self, range: Option<(usize, usize)>) -> Self {
1111        self.page_range = range;
1112        self
1113    }
1114
1115    /// In-place variant of [`pages`](Self::pages) for a long-lived pipeline
1116    /// (e.g. docling-serve's warm instance) that applies a per-request window
1117    /// without rebuilding — unlike the model switches, the window is pure
1118    /// configuration. Set it before every conversion; it stays until changed.
1119    pub fn set_pages(&mut self, range: Option<(usize, usize)>) {
1120        self.page_range = range;
1121    }
1122
1123    /// OCR recognition language (see [`OcrLang`]): English by default, `ch`
1124    /// for the multilingual docling-conformance model. `None` keeps the
1125    /// process default (`DOCLING_RS_OCR_LANG`, else English). Set before the
1126    /// first conversion; for a warm pipeline use
1127    /// [`set_ocr_lang`](Self::set_ocr_lang).
1128    pub fn ocr_lang(mut self, lang: Option<ocr::OcrLang>) -> Self {
1129        self.set_ocr_lang(lang);
1130        self
1131    }
1132
1133    /// In-place variant of [`ocr_lang`](Self::ocr_lang) for a long-lived
1134    /// pipeline (docling-serve's warm instance). Unlike the page window this
1135    /// is a *model* switch: any worker whose cached recognition model was
1136    /// loaded for a different language drops it, to be lazily reloaded on the
1137    /// next OCR-needing page (cheap — the rec models are ~10 MB).
1138    pub fn set_ocr_lang(&mut self, lang: Option<ocr::OcrLang>) {
1139        let lang = lang.unwrap_or_else(ocr::OcrLang::from_env);
1140        self.ocr_lang = lang;
1141        for worker in self.primary.iter_mut().chain(self.pool.iter_mut()) {
1142            if worker.ocr_lang != lang {
1143                worker.ocr_lang = lang;
1144                worker.ocr = None;
1145            }
1146        }
1147    }
1148
1149    /// Resolve the configured 1-based window against a page count into the
1150    /// 0-based inclusive form the backend walks, validating it selects at
1151    /// least one existing page.
1152    fn resolve_range(&self, total: usize) -> Result<Option<(usize, usize)>, PdfError> {
1153        let Some((first, last)) = self.page_range else {
1154            return Ok(None);
1155        };
1156        if first == 0 || last < first {
1157            return Err(PdfError::Pdfium(format!(
1158                "invalid page range {first}-{last} (pages are 1-based, first <= last)"
1159            )));
1160        }
1161        if first > total {
1162            return Err(PdfError::Pdfium(format!(
1163                "page range {first}-{last} is outside the document ({total} page(s))"
1164            )));
1165        }
1166        Ok(Some((first - 1, last.min(total) - 1)))
1167    }
1168
1169    /// Enable the opt-in enrichment passes (docling's
1170    /// `do_picture_classification` / `do_code_enrichment` /
1171    /// `do_formula_enrichment`). Each enabled pass lazily loads its model on
1172    /// the first matching region; a missing model warns once and is skipped.
1173    /// Set before the first conversion (no effect on already-loaded workers).
1174    pub fn enrichments(mut self, opts: EnrichmentOptions) -> Self {
1175        self.enrich = opts;
1176        self
1177    }
1178
1179    /// Skip loading and running the TableFormer table-structure model. Table
1180    /// regions still get emitted, but reconstructed geometrically from cell
1181    /// positions instead of via the ONNX model's predicted structure — faster
1182    /// (no model load, no per-table inference) at the cost of table fidelity.
1183    /// No effect if a worker is already loaded; set this before the first
1184    /// conversion.
1185    pub fn no_table_former(mut self, disable: bool) -> Self {
1186        self.no_table_former = disable;
1187        self
1188    }
1189
1190    /// Keep every detected `picture` region as a picture. By default an
1191    /// *uncaptioned* picture that reads like a dense, uniform text panel (a
1192    /// terms-and-conditions box exported as an image) is demoted into
1193    /// paragraphs (#157); a chart the layout mislabels can still trip that
1194    /// heuristic on scanned pages, and image-extraction workflows may simply
1195    /// want every crop — this flag disables the demotion entirely (#173).
1196    /// No effect on already-loaded workers; set before the first conversion.
1197    pub fn no_text_panels(mut self, disable: bool) -> Self {
1198        self.no_text_panels = disable;
1199        self
1200    }
1201
1202    /// Skip layout detection, OCR, and TableFormer entirely — no model load, no
1203    /// inference of any kind. The PDF's embedded text cells are grouped by line
1204    /// and emitted as plain paragraphs in reading order: no headings, lists,
1205    /// tables, code blocks, or pictures, since that structure comes from the
1206    /// layout model. The fastest possible PDF path, but pages with no embedded
1207    /// text layer (scanned/image-only PDFs) yield no text at all — convert those
1208    /// without this flag. Implies `no_table_former`. No effect if a worker is
1209    /// already loaded; set this before the first conversion.
1210    pub fn no_ocr(mut self, disable: bool) -> Self {
1211        self.no_ocr = disable;
1212        self
1213    }
1214
1215    /// OCR every page from its rendered image even when the page carries an
1216    /// embedded text layer — docling's `force_full_page_ocr`. The escape hatch
1217    /// for text layers that exist but lie: broken encodings, subset fonts with
1218    /// garbage mappings, a scanned form with a few typed-in fields. Ignored
1219    /// when [`no_ocr`](Self::no_ocr) is set, mirroring docling (there
1220    /// `force_full_page_ocr` is a sub-option of `do_ocr`).
1221    pub fn force_full_page_ocr(mut self, force: bool) -> Self {
1222        self.force_full_page_ocr = force;
1223        self
1224    }
1225
1226    /// The shared TableFormer slot handed to each worker, or `None` when the
1227    /// pipeline options skip TableFormer entirely.
1228    fn tables_slot(&self) -> Option<SharedTables> {
1229        if self.no_table_former || self.no_ocr {
1230            None
1231        } else {
1232            Some(Arc::clone(&self.tables))
1233        }
1234    }
1235
1236    /// The shared enrichment slots for a worker (`None` per model unless its
1237    /// flag is on; `no_ocr` skips layout, so there are no regions to enrich).
1238    fn enrich_slots(&self) -> (Option<SharedClassifier>, Option<SharedCodeFormula>) {
1239        if self.no_ocr || !self.enrich.any() {
1240            return (None, None);
1241        }
1242        (
1243            self.enrich
1244                .picture_classification
1245                .then(|| Arc::clone(&self.classifier)),
1246            (self.enrich.code || self.enrich.formula).then(|| Arc::clone(&self.code_formula)),
1247        )
1248    }
1249
1250    /// Eagerly load the models (the full-intra serial worker: layout + OCR, and
1251    /// the shared TableFormer unless disabled) so the first conversion doesn't pay
1252    /// the load cost. Idempotent; respects `no_ocr` / `no_table_former` (with
1253    /// `no_ocr` there is nothing to load). The docling.rs analogue of docling's
1254    /// `DocumentConverter.initialize_pipeline`.
1255    pub fn warm_up(&mut self) -> Result<(), PdfError> {
1256        self.primary()?;
1257        Ok(())
1258    }
1259
1260    /// The full-intra serial worker, loaded on first use.
1261    fn primary(&mut self) -> Result<&mut Worker, PdfError> {
1262        if self.primary.is_none() {
1263            self.primary = Some(Worker::load(
1264                intra_threads(),
1265                self.tables_slot(),
1266                self.enrich_slots(),
1267                self.enrich,
1268                self.no_ocr,
1269                self.force_full_page_ocr,
1270                self.no_text_panels,
1271                self.ocr_lang,
1272            )?);
1273        }
1274        Ok(self.primary.as_mut().unwrap())
1275    }
1276
1277    /// Convert a PDF (bytes) to a [`DoclingDocument`]. A document with fewer than
1278    /// `parallel_min` pages (or a pool size of 1) streams through the full-intra
1279    /// primary; a larger one renders on this thread (pdfium is not thread-safe) and
1280    /// fans the pages out across the worker pool, reassembled in page order so the
1281    /// output is byte-identical to the serial path.
1282    pub fn convert(
1283        &mut self,
1284        bytes: &[u8],
1285        password: Option<&str>,
1286        name: &str,
1287    ) -> Result<DoclingDocument, PdfError> {
1288        let pages = pdfium_backend::page_count(bytes, password)?;
1289        let range = self.resolve_range(pages)?;
1290        // Serial vs parallel is decided by the pages actually converted: a
1291        // 3-page window over a 500-page PDF should not pay the pool load.
1292        let selected = range.map_or(pages, |(a, b)| b - a + 1);
1293        let doc = if self.target_workers >= 2 && selected >= self.parallel_min {
1294            self.convert_parallel(bytes, password, name, range)?
1295        } else {
1296            self.convert_serial(bytes, password, name, range)?
1297        };
1298        timing::report();
1299        Ok(doc)
1300    }
1301
1302    /// Stream pages one at a time through the primary worker — render → process →
1303    /// drop — so the document holds ~one page bitmap (~5 MB) at a time.
1304    fn convert_serial(
1305        &mut self,
1306        bytes: &[u8],
1307        password: Option<&str>,
1308        name: &str,
1309        range: Option<(usize, usize)>,
1310    ) -> Result<DoclingDocument, PdfError> {
1311        let mut doc = DoclingDocument::new(name);
1312        let mut confs = std::collections::BTreeMap::new();
1313        let render_image = !self.no_ocr;
1314        let worker = self.primary()?;
1315        pdfium_backend::for_each_page(
1316            bytes,
1317            password,
1318            render_image,
1319            range,
1320            |n, _total, mut page| {
1321                let (mut nodes, links, conf) = worker.process(n, &mut page)?;
1322                assemble::stamp_page_no(&mut nodes, n + 1);
1323                doc.nodes.extend(nodes);
1324                doc.links.extend(links);
1325                confs.insert(n + 1, conf);
1326                Ok::<(), PdfError>(())
1327            },
1328        )?;
1329        assemble::merge_continuations(&mut doc.nodes);
1330        doc.confidence = Some(docling_core::ConfidenceReport::from_pages(confs));
1331        Ok(doc)
1332    }
1333
1334    /// Render pages serially on this thread (pdfium) and process them in parallel
1335    /// across the worker pool. A bounded channel applies backpressure so only a
1336    /// handful of page bitmaps are resident at once; results carry their page
1337    /// index and are reassembled in order, so the output is byte-identical to the
1338    /// serial path.
1339    fn convert_parallel(
1340        &mut self,
1341        bytes: &[u8],
1342        password: Option<&str>,
1343        name: &str,
1344        range: Option<(usize, usize)>,
1345    ) -> Result<DoclingDocument, PdfError> {
1346        self.ensure_pool()?;
1347        let n_workers = self.pool.len();
1348        let render_image = !self.no_ocr;
1349        let layout_batch = pdf_layout_batch();
1350        // Bound sized so every worker can accumulate a full layout batch while
1351        // rendering stays ahead (and never below the pre-#73 render-ahead of
1352        // two pages per worker); still a hard cap on resident page bitmaps.
1353        let (work_tx, work_rx) = sync_channel::<(usize, PdfPage)>(n_workers * layout_batch.max(2));
1354        let work_rx: Arc<Mutex<Receiver<(usize, PdfPage)>>> = Arc::new(Mutex::new(work_rx));
1355        let results: Arc<Mutex<Vec<(usize, PageOut)>>> = Arc::new(Mutex::new(Vec::new()));
1356        let first_err: Arc<Mutex<Option<PdfError>>> = Arc::new(Mutex::new(None));
1357
1358        // Move the pool into the scope so each worker gets an exclusive `&mut`.
1359        let mut workers = std::mem::take(&mut self.pool);
1360        std::thread::scope(|s| {
1361            for worker in workers.iter_mut() {
1362                let work_rx = Arc::clone(&work_rx);
1363                let results = Arc::clone(&results);
1364                let first_err = Arc::clone(&first_err);
1365                s.spawn(move || loop {
1366                    // Hold the receiver lock only for the recv (plus a non-blocking
1367                    // drain up to the layout batch size); release before the (long)
1368                    // per-page work so other workers can pull concurrently.
1369                    let mut batch = Vec::new();
1370                    {
1371                        let rx = work_rx.lock().unwrap();
1372                        match rx.recv() {
1373                            Ok(item) => {
1374                                batch.push(item);
1375                                while batch.len() < layout_batch {
1376                                    match rx.try_recv() {
1377                                        Ok(item) => batch.push(item),
1378                                        Err(_) => break,
1379                                    }
1380                                }
1381                            }
1382                            Err(_) => break,
1383                        }
1384                    }
1385                    let outs = worker.process_batch(&mut batch);
1386                    for ((idx, _), out) in batch.iter().zip(outs) {
1387                        match out {
1388                            Ok(out) => results.lock().unwrap().push((*idx, out)),
1389                            Err(e) => {
1390                                let mut slot = first_err.lock().unwrap();
1391                                if slot.is_none() {
1392                                    *slot = Some(e);
1393                                }
1394                            }
1395                        }
1396                    }
1397                });
1398            }
1399            // Render on this thread and feed the workers; backpressure blocks here
1400            // when the channel is full. Dropping `work_tx` afterwards signals the
1401            // workers (recv → Err) to finish.
1402            let render = pdfium_backend::for_each_page(
1403                bytes,
1404                password,
1405                render_image,
1406                range,
1407                |i, _total, page| {
1408                    work_tx
1409                        .send((i, page))
1410                        .map_err(|_| PdfError::Pdfium("page-worker channel closed".into()))
1411                },
1412            );
1413            drop(work_tx);
1414            if let Err(e) = render {
1415                let mut slot = first_err.lock().unwrap();
1416                if slot.is_none() {
1417                    *slot = Some(e);
1418                }
1419            }
1420        });
1421        // Threads have joined; restore the pool for the next conversion.
1422        self.pool = workers;
1423
1424        if let Some(e) = first_err.lock().unwrap().take() {
1425            return Err(e);
1426        }
1427        let mut results = Arc::try_unwrap(results)
1428            .unwrap_or_else(|arc| Mutex::new(arc.lock().unwrap().clone()))
1429            .into_inner()
1430            .unwrap();
1431        results.sort_by_key(|(idx, _)| *idx);
1432        let mut doc = DoclingDocument::new(name);
1433        let mut confs = std::collections::BTreeMap::new();
1434        for (idx, (mut nodes, links, conf)) in results {
1435            assemble::stamp_page_no(&mut nodes, idx + 1);
1436            doc.nodes.extend(nodes);
1437            doc.links.extend(links);
1438            confs.insert(idx + 1, conf);
1439        }
1440        assemble::merge_continuations(&mut doc.nodes);
1441        doc.confidence = Some(docling_core::ConfidenceReport::from_pages(confs));
1442        Ok(doc)
1443    }
1444
1445    /// Convert a PDF in **streaming** mode: `emit` is called with each finalized,
1446    /// in-document-order batch of nodes (and that span's recovered links) as pages
1447    /// complete, so a caller can serialize Markdown page by page instead of waiting
1448    /// for the whole document. The batches are exactly the buffered [`convert`]'s
1449    /// nodes, split at safe block boundaries by [`assemble::StreamAssembler`] — the
1450    /// parallel path reorders pages back into document order before emitting, so
1451    /// the output is identical regardless of worker scheduling.
1452    ///
1453    /// `emit` runs on the calling thread (never a worker), so it needn't be `Send`
1454    /// and its backpressure throttles the whole pipeline. Returning `Err` from
1455    /// `emit` aborts the conversion with that error.
1456    pub fn convert_streaming<F>(
1457        &mut self,
1458        bytes: &[u8],
1459        password: Option<&str>,
1460        name: &str,
1461        emit: F,
1462    ) -> Result<(), PdfError>
1463    where
1464        F: FnMut(Vec<Node>, Vec<(String, String)>) -> Result<(), PdfError>,
1465    {
1466        let _ = name; // page nodes carry no name; the caller owns the document name.
1467        let pages = pdfium_backend::page_count(bytes, password)?;
1468        let range = self.resolve_range(pages)?;
1469        let selected = range.map_or(pages, |(a, b)| b - a + 1);
1470        let r = if self.target_workers >= 2 && selected >= self.parallel_min {
1471            self.convert_streaming_parallel(bytes, password, range, emit)
1472        } else {
1473            self.convert_streaming_serial(bytes, password, range, emit)
1474        };
1475        timing::report();
1476        r
1477    }
1478
1479    /// Serial streaming: render → process → emit, one page at a time, holding back
1480    /// only the tail that might still merge into the next page.
1481    fn convert_streaming_serial<F>(
1482        &mut self,
1483        bytes: &[u8],
1484        password: Option<&str>,
1485        range: Option<(usize, usize)>,
1486        mut emit: F,
1487    ) -> Result<(), PdfError>
1488    where
1489        F: FnMut(Vec<Node>, Vec<(String, String)>) -> Result<(), PdfError>,
1490    {
1491        let mut asm = assemble::StreamAssembler::new();
1492        let render_image = !self.no_ocr;
1493        let worker = self.primary()?;
1494        pdfium_backend::for_each_page(
1495            bytes,
1496            password,
1497            render_image,
1498            range,
1499            |n, _total, mut page| {
1500                // Confidence is dropped on the streaming path: the report is
1501                // only complete once every page has run, which defeats
1502                // page-by-page emission — buffered `convert` carries it.
1503                let (nodes, links, _conf) = worker.process(n, &mut page)?;
1504                emit(asm.push(nodes), links)
1505            },
1506        )?;
1507        emit(asm.finish(), Vec::new())
1508    }
1509
1510    /// Parallel streaming: pages render serially on a dedicated thread (pdfium is
1511    /// not thread-safe) and process across the worker pool; results carry their
1512    /// page index and are reordered on the calling thread into a
1513    /// [`assemble::StreamAssembler`], which emits each page in document order as
1514    /// soon as its predecessors have arrived. Bounded channels keep only a handful
1515    /// of pages resident and let `emit`'s backpressure reach the renderer.
1516    fn convert_streaming_parallel<F>(
1517        &mut self,
1518        bytes: &[u8],
1519        password: Option<&str>,
1520        range: Option<(usize, usize)>,
1521        mut emit: F,
1522    ) -> Result<(), PdfError>
1523    where
1524        F: FnMut(Vec<Node>, Vec<(String, String)>) -> Result<(), PdfError>,
1525    {
1526        self.ensure_pool()?;
1527        let n_workers = self.pool.len();
1528        let render_image = !self.no_ocr;
1529        let layout_batch = pdf_layout_batch();
1530        // Bound sized so every worker can accumulate a full layout batch while
1531        // rendering stays ahead (and never below the pre-#73 render-ahead of
1532        // two pages per worker); still a hard cap on resident page bitmaps.
1533        let (work_tx, work_rx) = sync_channel::<(usize, PdfPage)>(n_workers * layout_batch.max(2));
1534        let work_rx: Arc<Mutex<Receiver<(usize, PdfPage)>>> = Arc::new(Mutex::new(work_rx));
1535        // Workers and the renderer report here; the calling thread drains it in
1536        // page order. Bounded so workers block (bounding resident bitmaps) when the
1537        // consumer falls behind.
1538        let (res_tx, res_rx) = sync_channel::<Result<(usize, PageOut), PdfError>>(n_workers * 2);
1539
1540        let mut workers = std::mem::take(&mut self.pool);
1541        let mut asm = assemble::StreamAssembler::new();
1542        let mut first_err: Option<PdfError> = None;
1543
1544        std::thread::scope(|s| {
1545            // Workers: pull a batch of pages (whatever is already rendered, up
1546            // to the layout batch size), process it, report (index-tagged)
1547            // results.
1548            for worker in workers.iter_mut() {
1549                let work_rx = Arc::clone(&work_rx);
1550                let res_tx = res_tx.clone();
1551                s.spawn(move || 'outer: loop {
1552                    let mut batch = Vec::new();
1553                    {
1554                        let rx = work_rx.lock().unwrap();
1555                        match rx.recv() {
1556                            Ok(item) => {
1557                                batch.push(item);
1558                                while batch.len() < layout_batch {
1559                                    match rx.try_recv() {
1560                                        Ok(item) => batch.push(item),
1561                                        Err(_) => break,
1562                                    }
1563                                }
1564                            }
1565                            Err(_) => break,
1566                        }
1567                    }
1568                    let outs = worker.process_batch(&mut batch);
1569                    for ((idx, _), out) in batch.iter().zip(outs) {
1570                        if res_tx.send(out.map(|o| (*idx, o))).is_err() {
1571                            break 'outer; // consumer gone
1572                        }
1573                    }
1574                });
1575            }
1576            // Renderer: feed pages to the pool on its own thread (pdfium stays on a
1577            // single thread); report a render error through the same channel.
1578            {
1579                let res_tx = res_tx.clone();
1580                s.spawn(move || {
1581                    let render = pdfium_backend::for_each_page(
1582                        bytes,
1583                        password,
1584                        render_image,
1585                        range,
1586                        |i, _total, page| {
1587                            work_tx
1588                                .send((i, page))
1589                                .map_err(|_| PdfError::Pdfium("page-worker channel closed".into()))
1590                        },
1591                    );
1592                    drop(work_tx); // signal workers to finish
1593                    if let Err(e) = render {
1594                        let _ = res_tx.send(Err(e));
1595                    }
1596                });
1597            }
1598            // Drop our own sender so the channel closes once the threads finish.
1599            drop(res_tx);
1600
1601            // Collector (this thread): reorder into document order and emit.
1602            // With a page window, indices start at the window's first page.
1603            let mut buffer: BTreeMap<usize, PageOut> = BTreeMap::new();
1604            let mut next = range.map_or(0, |(first, _)| first);
1605            for msg in res_rx.iter() {
1606                match msg {
1607                    Err(e) => {
1608                        if first_err.is_none() {
1609                            first_err = Some(e);
1610                        }
1611                    }
1612                    Ok((idx, out)) => {
1613                        buffer.insert(idx, out);
1614                        if first_err.is_some() {
1615                            continue; // keep draining so the threads can exit
1616                        }
1617                        while let Some((nodes, links, _conf)) = buffer.remove(&next) {
1618                            if let Err(e) = emit(asm.push(nodes), links) {
1619                                first_err = Some(e);
1620                                break;
1621                            }
1622                            next += 1;
1623                        }
1624                    }
1625                }
1626            }
1627        });
1628        // Threads have joined; restore the pool for the next conversion.
1629        self.pool = workers;
1630
1631        if let Some(e) = first_err {
1632            return Err(e);
1633        }
1634        emit(asm.finish(), Vec::new())
1635    }
1636
1637    /// Lazily grow the pool to `target_workers`, loading the new workers
1638    /// concurrently (model load is mostly I/O + mmap, so N loads overlap to roughly
1639    /// one load's wall-time). Cached for reuse across documents.
1640    fn ensure_pool(&mut self) -> Result<(), PdfError> {
1641        let need = self.target_workers.saturating_sub(self.pool.len());
1642        if need == 0 {
1643            return Ok(());
1644        }
1645        let intra = pdf_intra();
1646        let no_ocr = self.no_ocr;
1647        let force = self.force_full_page_ocr;
1648        let ntp = self.no_text_panels;
1649        let ocr_lang = self.ocr_lang;
1650        let enrich = self.enrich;
1651        let tables = self.tables_slot();
1652        let enrich_slots = self.enrich_slots();
1653        let loaded: Vec<Result<Worker, PdfError>> = std::thread::scope(|s| {
1654            let handles: Vec<_> = (0..need)
1655                .map(|_| {
1656                    let tables = tables.clone();
1657                    let enrich_slots = enrich_slots.clone();
1658                    s.spawn(move || {
1659                        Worker::load(
1660                            intra,
1661                            tables,
1662                            enrich_slots,
1663                            enrich,
1664                            no_ocr,
1665                            force,
1666                            ntp,
1667                            ocr_lang,
1668                        )
1669                    })
1670                })
1671                .collect();
1672            handles.into_iter().map(|h| h.join().unwrap()).collect()
1673        });
1674        for w in loaded {
1675            self.pool.push(w?);
1676        }
1677        Ok(())
1678    }
1679
1680    /// Convert a standalone image (PNG/JPEG/TIFF/WebP/…) as a single page —
1681    /// docling routes images through the same layout+OCR pipeline as a PDF page.
1682    pub fn convert_image(&mut self, bytes: &[u8], name: &str) -> Result<DoclingDocument, PdfError> {
1683        let image = decode_image_limited(bytes)?;
1684        let (w, h) = image.dimensions();
1685        // The image is its own page rendered at 1 px per "point" (scale 1.0); a
1686        // standalone image has no text layer, so OCR supplies the cells.
1687        let page = PdfPage {
1688            width: w as f32,
1689            height: h as f32,
1690            scale: 1.0,
1691            cells: Vec::new(),
1692            code_cells: Vec::new(),
1693            word_cells: Vec::new(),
1694            // A standalone image *is* its own scale-1.0 page image, so the
1695            // layout model sees it through the docling-exact PIL kernel.
1696            image_layout: Some(image.clone()),
1697            image,
1698            links: Vec::new(),
1699        };
1700        self.process_pages(vec![page], name)
1701    }
1702
1703    /// Run layout (+ OCR for cell-less pages) and assemble each already-rendered
1704    /// page (image / METS inputs, which are small and already materialised).
1705    fn process_pages(
1706        &mut self,
1707        mut pages: Vec<PdfPage>,
1708        name: &str,
1709    ) -> Result<DoclingDocument, PdfError> {
1710        let mut doc = DoclingDocument::new(name);
1711        let mut confs = std::collections::BTreeMap::new();
1712        let worker = self.primary()?;
1713        for (n, page) in pages.iter_mut().enumerate() {
1714            let (mut nodes, links, conf) = worker.process(n, page)?;
1715            assemble::stamp_page_no(&mut nodes, n + 1);
1716            doc.nodes.extend(nodes);
1717            doc.links.extend(links);
1718            confs.insert(n + 1, conf);
1719        }
1720        assemble::merge_continuations(&mut doc.nodes);
1721        doc.confidence = Some(docling_core::ConfidenceReport::from_pages(confs));
1722        Ok(doc)
1723    }
1724}
1725
1726#[cfg(feature = "ml")]
1727/// Convenience one-shot conversion (loads the pipeline per call). Errors are
1728/// detailed and surfaced (never silently skipped).
1729pub fn convert(
1730    bytes: &[u8],
1731    password: Option<&str>,
1732    name: &str,
1733) -> Result<DoclingDocument, PdfError> {
1734    convert_with_options(
1735        bytes,
1736        password,
1737        name,
1738        false,
1739        false,
1740        false,
1741        false,
1742        EnrichmentOptions::default(),
1743        None,
1744        None,
1745    )
1746}
1747
1748#[cfg(feature = "ml")]
1749/// Like [`convert`], but optionally skips loading/running TableFormer (see
1750/// [`Pipeline::no_table_former`]) and/or layout+OCR+TableFormer entirely (see
1751/// [`Pipeline::no_ocr`]), and/or enables the enrichment passes (see
1752/// [`Pipeline::enrichments`]).
1753// One positional per pipeline switch mirrors the Pipeline builder; growing
1754// past clippy's arity cap is the price of keeping this one-shot signature
1755// stable-ish instead of churning callers into an options struct mid-series.
1756#[allow(clippy::too_many_arguments)]
1757pub fn convert_with_options(
1758    bytes: &[u8],
1759    password: Option<&str>,
1760    name: &str,
1761    no_table_former: bool,
1762    no_ocr: bool,
1763    force_full_page_ocr: bool,
1764    no_text_panels: bool,
1765    enrich: EnrichmentOptions,
1766    pages: Option<(usize, usize)>,
1767    ocr_lang: Option<OcrLang>,
1768) -> Result<DoclingDocument, PdfError> {
1769    Pipeline::new()?
1770        .no_table_former(no_table_former)
1771        .no_ocr(no_ocr)
1772        .force_full_page_ocr(force_full_page_ocr)
1773        .no_text_panels(no_text_panels)
1774        .enrichments(enrich)
1775        .pages(pages)
1776        .ocr_lang(ocr_lang)
1777        .convert(bytes, password, name)
1778}
1779
1780#[cfg(feature = "ml")]
1781/// Convenience one-shot image conversion (loads the pipeline per call).
1782pub fn convert_image(bytes: &[u8], name: &str) -> Result<DoclingDocument, PdfError> {
1783    convert_image_with_options(
1784        bytes,
1785        name,
1786        false,
1787        false,
1788        false,
1789        EnrichmentOptions::default(),
1790        None,
1791    )
1792}
1793
1794#[cfg(feature = "ml")]
1795/// Like [`convert_image`], but optionally skips loading/running TableFormer (see
1796/// [`Pipeline::no_table_former`]) and/or layout+OCR+TableFormer entirely (see
1797/// [`Pipeline::no_ocr`]), and/or enables the enrichment passes.
1798pub fn convert_image_with_options(
1799    bytes: &[u8],
1800    name: &str,
1801    no_table_former: bool,
1802    no_ocr: bool,
1803    no_text_panels: bool,
1804    enrich: EnrichmentOptions,
1805    ocr_lang: Option<OcrLang>,
1806) -> Result<DoclingDocument, PdfError> {
1807    Pipeline::new()?
1808        .no_table_former(no_table_former)
1809        .no_ocr(no_ocr)
1810        .no_text_panels(no_text_panels)
1811        .enrichments(enrich)
1812        .ocr_lang(ocr_lang)
1813        .convert_image(bytes, name)
1814}
1815
1816#[cfg(feature = "ml")]
1817/// Convert pre-segmented pages (image + already-known text cells, e.g. METS/hOCR
1818/// scans) through the shared layout + assembly pipeline.
1819pub fn convert_pages(pages: Vec<PdfPage>, name: &str) -> Result<DoclingDocument, PdfError> {
1820    convert_pages_with_options(
1821        pages,
1822        name,
1823        false,
1824        false,
1825        false,
1826        EnrichmentOptions::default(),
1827    )
1828}
1829
1830#[cfg(feature = "ml")]
1831/// Like [`convert_pages`], but optionally skips loading/running TableFormer (see
1832/// [`Pipeline::no_table_former`]) and/or layout+OCR+TableFormer entirely (see
1833/// [`Pipeline::no_ocr`]), and/or enables the enrichment passes.
1834pub fn convert_pages_with_options(
1835    pages: Vec<PdfPage>,
1836    name: &str,
1837    no_table_former: bool,
1838    no_ocr: bool,
1839    no_text_panels: bool,
1840    enrich: EnrichmentOptions,
1841) -> Result<DoclingDocument, PdfError> {
1842    Pipeline::new()?
1843        .no_table_former(no_table_former)
1844        .no_text_panels(no_text_panels)
1845        .no_ocr(no_ocr)
1846        .enrichments(enrich)
1847        .process_pages(pages, name)
1848}
1849
1850#[cfg(feature = "ml")]
1851#[cfg(all(test, feature = "ml"))]
1852mod image_limit_tests {
1853    use super::decode_image_with_max_side;
1854
1855    /// A small valid PNG encoded via the `image` crate (robust vs. a hand-rolled
1856    /// byte literal).
1857    fn png_bytes(w: u32, h: u32) -> Vec<u8> {
1858        use std::io::Cursor;
1859        let img = image::RgbImage::new(w, h);
1860        let mut out = Vec::new();
1861        img.write_to(&mut Cursor::new(&mut out), image::ImageFormat::Png)
1862            .unwrap();
1863        out
1864    }
1865
1866    #[test]
1867    fn normal_image_decodes_under_the_cap() {
1868        let img = decode_image_with_max_side(&png_bytes(8, 8), 30_000).expect("8x8 decodes");
1869        assert_eq!(img.dimensions(), (8, 8));
1870    }
1871
1872    #[test]
1873    fn dimensions_over_the_cap_are_rejected_not_aborted() {
1874        // A per-side cap below the image's declared size must yield a
1875        // recoverable Err, never an allocation-abort — the mechanism that stops
1876        // a crafted image declaring 60000×60000 from OOM-killing the process.
1877        let r = decode_image_with_max_side(&png_bytes(8, 8), 4);
1878        assert!(
1879            r.is_err(),
1880            "decode must fail under the pixel cap, not abort"
1881        );
1882    }
1883}
1884
1885#[cfg(test)]
1886mod median_tests {
1887    #[test]
1888    fn median_of_empty_is_zero_not_a_panic() {
1889        // A crafted table can leave a row/column with zero matched cells; the
1890        // even-count branch would index values[0 - 1] and panic (→ remote crash
1891        // via docling-serve) without the empty guard.
1892        assert_eq!(super::tf_match::median_for_test(&mut []), 0.0);
1893        assert_eq!(super::tf_match::median_for_test(&mut [4.0, 2.0]), 3.0);
1894        assert_eq!(super::tf_match::median_for_test(&mut [5.0, 1.0, 3.0]), 3.0);
1895    }
1896}
1897
1898#[cfg(test)]
1899mod send_check {
1900    /// The Node bindings (`docling-node`) run a shared [`super::Pipeline`] on
1901    /// libuv worker threads (`Arc<Mutex<Pipeline>>`), which is only sound while
1902    /// `Pipeline: Send` holds — this fails to compile if a non-`Send` field
1903    /// (e.g. an `Rc` or a raw pdfium handle) ever lands in the pipeline.
1904    fn assert_send<T: Send>() {}
1905
1906    #[test]
1907    fn pipeline_is_send() {
1908        assert_send::<super::Pipeline>();
1909    }
1910}