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. Kept as a
28// re-export after the logic moved to the shared `docling-onnx` crate —
29// `docling_pdf::ep::…` remains the stable path downstream crates code against.
30#[cfg(feature = "ml")]
31pub use docling_onnx as ep;
32// Heading-hierarchy stage (#302): PDF font-name style parsing, outline
33// extraction (pure lopdf), and the level-assignment pass. No feature gate on
34// the logic itself — only the glyph style pass needs pdfium (`ml`).
35mod font_style;
36mod heading_hierarchy;
37pub mod layout;
38#[cfg(feature = "ml")]
39mod mets;
40#[cfg(feature = "ml")]
41mod ocr;
42#[cfg(feature = "ocr-prep")]
43pub mod ocr_prep;
44#[cfg(feature = "ml")]
45mod orient;
46pub mod outline;
47pub mod pdfium_backend;
48#[cfg(feature = "ml")]
49pub mod quality;
50mod reading_order;
51// Pure-Rust region resampling (page→1024px box-average, crop→448 bilinear) —
52// available to the browser TableFormer path (#157 stage 3), not just `ml`.
53#[cfg(feature = "ocr-prep")]
54pub mod resample;
55#[cfg(feature = "ocr-prep")]
56pub mod scanned;
57// Built-in standard-14 font metrics for the pure-Rust text parser (#187) —
58// no feature gate: the wasm/pdf-text path needs them like the native one.
59mod std14;
60#[cfg(feature = "ml")]
61pub mod tableformer;
62pub mod textparse;
63#[cfg(feature = "ocr-prep")]
64pub mod tf_core;
65// docling's TableFormer cell matcher — pure Rust, shared with the browser
66// TableFormer path (#157 stage 3).
67#[cfg(feature = "ocr-prep")]
68pub mod tf_match;
69pub mod timing;
70
71#[cfg(feature = "ml")]
72use std::collections::BTreeMap;
73use std::fmt;
74#[cfg(feature = "ml")]
75use std::sync::mpsc::{sync_channel, Receiver};
76#[cfg(feature = "ml")]
77use std::sync::{Arc, Mutex};
78
79// An execution provider only exists on its OS, and ort's prebuilt ONNX
80// Runtime binaries follow suit — requesting an impossible pairing otherwise
81// surfaces as a cryptic ort-sys linker error ("no builds available that
82// satisfy the requested feature set"). Catch it at type-check time with an
83// actionable message instead.
84#[cfg(all(feature = "coreml", not(target_vendor = "apple")))]
85compile_error!(
86 "the `coreml` execution provider exists only on Apple targets (macOS/iOS). \
87 On Linux use `--features cuda` or `--features tensorrt` (NVIDIA), on \
88 Windows also `--features directml`, or build without EP features for CPU."
89);
90#[cfg(all(feature = "directml", not(target_os = "windows")))]
91compile_error!(
92 "the `directml` execution provider exists only on Windows. On Linux use \
93 `--features cuda` or `--features tensorrt` (NVIDIA), on macOS \
94 `--features coreml`, or build without EP features for CPU."
95);
96#[cfg(all(any(feature = "cuda", feature = "tensorrt"), target_vendor = "apple"))]
97compile_error!(
98 "the `cuda`/`tensorrt` execution providers have no Apple builds (no NVIDIA \
99 support on macOS). Use `--features coreml` there, or build without EP \
100 features for CPU."
101);
102
103use docling_core::DoclingDocument;
104// The env-knob helpers only gate ML-pipeline diagnostics and tuning; the
105// pure text-layer (wasm) build has no call sites.
106#[cfg(feature = "ml")]
107use docling_core::Node;
108#[cfg(feature = "ml")]
109use docling_core::{debug_log, env};
110
111pub use heading_hierarchy::HeadingHierarchyOptions;
112#[cfg(feature = "ml")]
113pub use mets::{convert_mets_gbs, convert_mets_gbs_with_options, convert_mets_gbs_with_pipeline};
114#[cfg(feature = "ml")]
115pub use ocr::{OcrLang, OcrMode};
116#[cfg(feature = "ml")]
117pub use pdfium_backend::PdfDocument;
118pub use pdfium_backend::{PdfPage, TextCell};
119// Plain page rasterization (#243) — pdfium only, no models.
120#[cfg(feature = "ml")]
121pub use pdfium_backend::{render_pages, RenderedPage};
122
123/// Errors from the PDF backend. Detailed and surfaced (never silently skipped).
124#[derive(Debug)]
125pub enum PdfError {
126 /// pdfium failed to bind, open, or read the document.
127 Pdfium(String),
128 /// The layout ONNX model failed to load or run.
129 Layout(String),
130 /// The OCR ONNX model failed to load or run.
131 Ocr(String),
132}
133
134impl fmt::Display for PdfError {
135 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
136 match self {
137 PdfError::Pdfium(m) => write!(f, "pdf: pdfium error: {m}"),
138 PdfError::Layout(m) => write!(f, "pdf: {m}"),
139 PdfError::Ocr(m) => write!(f, "pdf: {m}"),
140 }
141 }
142}
143
144impl std::error::Error for PdfError {}
145
146#[cfg(feature = "ml")]
147impl From<pdfium_render::prelude::PdfiumError> for PdfError {
148 fn from(e: pdfium_render::prelude::PdfiumError) -> Self {
149 // A failed dlopen means pdfium was never installed — the #1 first-run
150 // failure after a bare `cargo install` (which ships no runtime
151 // assets). Say what to do instead of leaking the raw loader error.
152 if matches!(e, pdfium_render::prelude::PdfiumError::LoadLibraryError(_)) {
153 // The loader error pretty-prints over several lines; compact it.
154 let detail = e
155 .to_string()
156 .split_whitespace()
157 .collect::<Vec<_>>()
158 .join(" ");
159 return PdfError::Pdfium(format!(
160 "the pdfium library is not installed. PDF/image conversion needs \
161 pdfium + the ONNX models: fetch both with \
162 scripts/install/download_dependencies.sh from a docling.rs \
163 checkout (https://github.com/docling-project/docling.rs), or \
164 point PDFIUM_DYNAMIC_LIB_PATH at a directory containing the \
165 pdfium library. A digital PDF's embedded text layer converts \
166 without either in no-OCR mode (CLI: --no-ocr). Declarative \
167 formats (DOCX, HTML, Markdown, …) never need them. [{detail}]"
168 ));
169 }
170 PdfError::Pdfium(e.to_string())
171 }
172}
173
174/// Convert a PDF's **embedded text layer only** — no pdfium, no ONNX, no
175/// threads: the pure-Rust content-stream parser ([`textparse`]) feeds the same
176/// orphan-region assembly the `no_ocr` pipeline flag uses, so text-layer PDFs
177/// come out identical to `--no-ocr` (flat, line-grouped paragraphs in reading
178/// order; no headings/lists/tables/pictures, and no hyperlink recovery).
179///
180/// This is the only conversion entry compiled without the `ml` feature (it is
181/// what a wasm32 build runs). A scanned/image-only PDF (no embedded text
182/// layer) yields an empty document rather than an error, same as `no_ocr` —
183/// callers can detect that and fall back to an OCR-capable build.
184pub fn convert_text_layer(bytes: &[u8], name: &str) -> Result<DoclingDocument, PdfError> {
185 convert_text_layer_pages(bytes, name, None)
186}
187
188/// [`convert_text_layer`] restricted to a **1-based inclusive** page window
189/// (issue #80's `--pages`); `None` converts everything. The window is
190/// validated the same way as [`Pipeline::pages`]: `first <= last`, 1-based,
191/// and it must select at least one existing page.
192pub fn convert_text_layer_pages(
193 bytes: &[u8],
194 name: &str,
195 pages: Option<(usize, usize)>,
196) -> Result<DoclingDocument, PdfError> {
197 if let Some((first, last)) = pages {
198 if first == 0 || last < first {
199 return Err(PdfError::Pdfium(format!(
200 "invalid page range {first}-{last} (pages are 1-based, first <= last)"
201 )));
202 }
203 }
204 let mut doc = DoclingDocument::new(name);
205 let mut total = 0usize;
206 let parsed = textparse::pdf_text_pages(bytes);
207 // A vestigial layer (a few typed-in form fields over scanned pages) is not
208 // the document's text: return the empty document, which callers already
209 // report as "no text layer" — so an OCR-capable caller falls back to OCR
210 // instead of proudly extracting thirteen characters.
211 if textparse::text_layer_is_vestigial(&parsed) {
212 return Ok(doc);
213 }
214 for (i, page) in parsed.into_iter().enumerate() {
215 total += 1;
216 if let Some((first, last)) = pages {
217 if i + 1 < first || i + 1 > last {
218 continue;
219 }
220 }
221 let mut regions = Vec::new();
222 assemble::add_orphan_regions(&mut regions, &page.cells);
223 let table_rows = vec![None; regions.len()];
224 let enrich_out = vec![None; regions.len()];
225 let (mut nodes, links) = assemble::assemble_page(&page, regions, &table_rows, &enrich_out);
226 assemble::stamp_page_no(&mut nodes, i + 1);
227 doc.nodes.extend(nodes);
228 doc.links.extend(links);
229 }
230 if let Some((first, last)) = pages {
231 if first > total {
232 return Err(PdfError::Pdfium(format!(
233 "page range {first}-{last} is outside the document ({total} page(s))"
234 )));
235 }
236 }
237 assemble::merge_continuations(&mut doc.nodes);
238 Ok(doc)
239}
240
241/// Threads ONNX inference may use, capped by `DOCLING_RS_PDF_THREADS` if set.
242/// Defaults to the available parallelism (ort otherwise picks a low number).
243#[cfg(feature = "ml")]
244pub(crate) fn intra_threads() -> usize {
245 if let Some(n) = env::parse::<usize>("DOCLING_RS_PDF_THREADS").filter(|&n| n > 0) {
246 return n;
247 }
248 env::cpu_budget()
249}
250
251#[cfg(feature = "ml")]
252/// TableFormer's intra-op width (#262): `DOCLING_RS_TF_INTRA` explicitly,
253/// else the shared [`intra_threads`] budget. The shared TF session used to
254/// take the raw host width on top of the already-sized worker pools —
255/// under a cgroup CPU limit that oversubscription showed up as constant
256/// throttling and ~66% higher peak memory (each intra thread carries its own
257/// arena slab); the reporter's 4-CPU/8-core case dropped from 2.2 GB to
258/// 1.3 GB peak by capping this pool.
259pub(crate) fn tf_intra() -> usize {
260 if let Some(n) = env::parse::<usize>("DOCLING_RS_TF_INTRA").filter(|&n| n > 0) {
261 return n;
262 }
263 intra_threads()
264}
265
266#[cfg(feature = "ml")]
267/// True when `DOCLING_RS_FP32` forces the full-precision models even where
268/// an INT8 variant sits next to the fp32 default.
269pub(crate) fn fp32_forced() -> bool {
270 env::flag("DOCLING_RS_FP32")
271}
272
273#[cfg(feature = "ml")]
274/// Should the int8 model defaults be skipped in favor of fp32? Either the
275/// user said so (`DOCLING_RS_FP32`), or a GPU execution provider is selected
276/// (#74) — the int8 exports are QDQ graphs calibrated for CPU kernels and
277/// only conformance-validated there. An explicit `DOCLING_*_ONNX` path
278/// override still wins over this at every call site.
279pub(crate) fn prefer_fp32() -> bool {
280 fp32_forced() || docling_onnx::prefers_fp32()
281}
282
283#[cfg(feature = "ml")]
284/// Resolve a default (CWD-relative) asset path — the shared chain in
285/// [`docling_core::assets`]: CWD, then next to the executable and one level
286/// above it (the `scripts/install/install.sh` layout).
287pub(crate) fn resolve_asset(rel: &str) -> String {
288 docling_core::assets::resolve(rel)
289}
290
291/// One resolved runtime asset — which file a stage would load right now,
292/// given the CWD, the env overrides and the int8/fp32 preference.
293#[cfg(feature = "ml")]
294#[derive(Debug, Clone)]
295pub struct ModelEntry {
296 /// Pipeline stage, e.g. `layout`, `tableformer.decoder`, `ocr.rec`.
297 pub stage: &'static str,
298 /// The resolved path (absolute or CWD-relative, as it will be opened).
299 pub path: String,
300 /// Whether the file exists right now.
301 pub found: bool,
302 /// File size in bytes (0 when missing) — enough to tell an int8 quant
303 /// from an fp32 graph, or a stale model from a re-published one, at a
304 /// glance without hashing gigabytes per request.
305 pub bytes: u64,
306}
307
308/// Resolve the whole runtime model set **without loading anything** — the
309/// exact selection each stage performs at load time (layout honors the
310/// int8/fp32 preference, TableFormer its decoder ranking, OCR the language
311/// pair), plus the pdfium library. docling-serve exposes this at
312/// `/v1/config` and logs it at startup, so "the server picked up different
313/// models" is one `curl` away instead of a mystery of dissolved tables.
314/// Resolution is CWD-relative with an exe-dir fallback, so the answer can
315/// legitimately differ between two working directories.
316#[cfg(feature = "ml")]
317pub fn model_inventory() -> Vec<ModelEntry> {
318 fn entry(stage: &'static str, path: String) -> ModelEntry {
319 let meta = std::fs::metadata(&path).ok();
320 ModelEntry {
321 stage,
322 found: meta.is_some(),
323 bytes: meta.map(|m| m.len()).unwrap_or(0),
324 path,
325 }
326 }
327 let (enc, dec, bbx) = tableformer::resolved_paths();
328 let (rec, dict) = ocr::resolve_rec_pair(ocr::OcrLang::from_env());
329 let pdfium =
330 env::nonempty("PDFIUM_DYNAMIC_LIB_PATH").unwrap_or_else(|| resolve_asset(".pdfium/lib"));
331 vec![
332 entry(
333 "layout",
334 model_path(
335 "DOCLING_LAYOUT_ONNX",
336 ".models/layout_heron.onnx",
337 ".models/layout_heron_int8.onnx",
338 ),
339 ),
340 entry("tableformer.encoder", enc),
341 entry("tableformer.decoder", dec),
342 entry("tableformer.bbox", bbx),
343 entry("ocr.rec", rec),
344 entry("ocr.dict", dict),
345 entry("pdfium", pdfium),
346 ]
347}
348
349/// Resolve a model path: an explicit env override always wins; otherwise the
350/// INT8 variant of the default path when it exists on disk (the quantized
351/// models are conformance-validated — see docs/PDF_CONFORMANCE.md — and load/run
352/// markedly faster on CPU), unless `DOCLING_RS_FP32` opts back into full
353/// precision; else the fp32 default.
354#[cfg(feature = "ml")]
355pub(crate) fn model_path(key: &str, fp32_default: &str, int8_default: &str) -> String {
356 if let Some(p) = env::nonempty(key) {
357 return p;
358 }
359 if !prefer_fp32() {
360 let p = resolve_asset(int8_default);
361 if std::path::Path::new(&p).exists() {
362 return p;
363 }
364 }
365 resolve_asset(fp32_default)
366}
367
368/// Decode a standalone image with hard resource limits. A crafted image can
369/// declare enormous dimensions in a few-KB file; `image::load_from_memory`
370/// then tries to allocate the full pixel buffer (e.g. 60000×60000 → ~10 GB),
371/// and allocation failure aborts the whole process, bypassing the per-request
372/// panic catch. The 256 MiB alloc / 30000-px caps below turn that into a
373/// recoverable decode error instead. `DOCLING_RS_MAX_IMAGE_PIXELS` overrides
374/// the per-side pixel cap for the rare legitimately-huge scan.
375///
376/// Gated on `ml`: the only callers (`convert_image`, the METS backend) are
377/// ML-only, and the `image` crate is an `ml`-feature dependency — the
378/// text-layer wasm build has neither.
379#[cfg(feature = "ml")]
380pub(crate) fn decode_image_limited(bytes: &[u8]) -> Result<image::RgbImage, PdfError> {
381 let max_side: u32 = env::parse("DOCLING_RS_MAX_IMAGE_PIXELS").unwrap_or(30_000);
382 decode_image_with_max_side(bytes, max_side)
383}
384
385/// Whether `bytes` is an ISOBMFF HEIF/HEIC container (the `ftyp` brands
386/// iPhones write). Checked by content, not extension — HEIC regularly
387/// arrives misnamed `.jpg`.
388#[cfg(feature = "ml")]
389fn is_heif(bytes: &[u8]) -> bool {
390 bytes.len() >= 12
391 && &bytes[4..8] == b"ftyp"
392 && matches!(
393 &bytes[8..12],
394 b"heic" | b"heix" | b"hevc" | b"heim" | b"heis" | b"hevm" | b"hevs" | b"mif1" | b"msf1"
395 )
396}
397
398/// Decode a HEIF/HEIC primary image via libheif (#211). Behind the opt-in
399/// `heif` feature — libheif is a native dependency the default build (and
400/// wasm) must not carry.
401#[cfg(all(feature = "ml", feature = "heif"))]
402fn decode_heif(bytes: &[u8], max_side: u32) -> Result<image::RgbImage, PdfError> {
403 use libheif_rs::{ColorSpace, HeifContext, LibHeif, RgbChroma};
404 let err = |e: String| PdfError::Pdfium(format!("heif: {e}"));
405 let ctx = HeifContext::read_from_bytes(bytes).map_err(|e| err(e.to_string()))?;
406 let handle = ctx.primary_image_handle().map_err(|e| err(e.to_string()))?;
407 if handle.width() > max_side || handle.height() > max_side {
408 return Err(err(format!(
409 "image dimensions {}x{} exceed the {max_side}px per-side cap \
410 (DOCLING_RS_MAX_IMAGE_PIXELS overrides)",
411 handle.width(),
412 handle.height()
413 )));
414 }
415 let lib = LibHeif::new();
416 let img = lib
417 .decode(&handle, ColorSpace::Rgb(RgbChroma::Rgb), None)
418 .map_err(|e| err(e.to_string()))?;
419 let (w, h) = (img.width(), img.height());
420 let planes = img.planes();
421 let plane = planes
422 .interleaved
423 .ok_or_else(|| err("no RGB plane".into()))?;
424 let stride = plane.stride;
425 let mut out = image::RgbImage::new(w, h);
426 for (y, row) in out.rows_mut().enumerate() {
427 let src = &plane.data[y * stride..y * stride + w as usize * 3];
428 for (x, px) in row.enumerate() {
429 px.0 = [src[x * 3], src[x * 3 + 1], src[x * 3 + 2]];
430 }
431 }
432 Ok(out)
433}
434
435#[cfg(feature = "ml")]
436fn decode_image_with_max_side(bytes: &[u8], max_side: u32) -> Result<image::RgbImage, PdfError> {
437 use image::ImageReader;
438 use std::io::Cursor;
439
440 if is_heif(bytes) {
441 #[cfg(feature = "heif")]
442 return decode_heif(bytes, max_side);
443 #[cfg(not(feature = "heif"))]
444 return Err(PdfError::Pdfium(
445 "HEIC/HEIF input needs a build with the `heif` cargo feature \
446 (rebuild with --features heif; links the system libheif)"
447 .into(),
448 ));
449 }
450
451 let mut limits = image::Limits::default();
452 limits.max_image_width = Some(max_side);
453 limits.max_image_height = Some(max_side);
454 limits.max_alloc = Some(256 * 1024 * 1024);
455
456 let mut reader = ImageReader::new(Cursor::new(bytes))
457 .with_guessed_format()
458 .map_err(|e| PdfError::Pdfium(format!("image: {e}")))?;
459 reader.limits(limits);
460 Ok(reader
461 .decode()
462 .map_err(|e| PdfError::Pdfium(format!("image: {e}")))?
463 .into_rgb8())
464}
465
466#[cfg(feature = "ml")]
467/// One page's assembled output: typed nodes plus the page's hyperlinks (kept
468/// separate so pages processed out of order can be stitched back in page
469/// order) and its confidence scores (#183).
470type PageOut = (
471 Vec<Node>,
472 Vec<(String, String)>,
473 docling_core::confidence::PageConfidence,
474);
475
476#[cfg(feature = "ml")]
477/// The pool-wide TableFormer slot: one instance shared by every worker, loaded
478/// lazily on the first table region any worker sees. Tables appear on a
479/// minority of pages, so per-worker copies mostly multiplied ~0.4 GB of
480/// weights+arenas by the pool size for nothing; a single shared instance keeps
481/// the peak flat regardless of pool width, and a table's structure prediction
482/// is independent of which worker runs it, so output is byte-identical. The
483/// mutex serialises concurrent tables — the shared instance is loaded with the
484/// full intra-op thread budget to compensate (one wide TableFormer instead of
485/// several narrow ones).
486enum TfSlot {
487 /// Not attempted yet (no table seen so far).
488 Unloaded,
489 /// Load attempted, graphs absent — geometric fallback (warned once).
490 Missing,
491 Ready(tableformer::TableFormer),
492}
493
494#[cfg(feature = "ml")]
495type SharedTables = Arc<Mutex<TfSlot>>;
496
497#[cfg(feature = "ml")]
498/// The same lazy shared-slot pattern for the (rarer still) enrichment models:
499/// one instance per pipeline, loaded on the first region that needs it.
500enum EnrichSlot<T> {
501 Unloaded,
502 /// Load attempted, model files absent — enrichment skipped (warned once).
503 Missing,
504 Ready(T),
505}
506
507#[cfg(feature = "ml")]
508type SharedClassifier = Arc<Mutex<EnrichSlot<enrich::PictureClassifier>>>;
509#[cfg(feature = "ml")]
510type SharedCodeFormula = Arc<Mutex<EnrichSlot<enrich::CodeFormula>>>;
511
512#[cfg(feature = "ml")]
513/// The opt-in enrichment passes, mirroring docling's `PdfPipelineOptions`
514/// flags (`do_picture_classification`, `do_code_enrichment`,
515/// `do_formula_enrichment`). All off by default.
516#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
517pub struct EnrichmentOptions {
518 /// Classify each picture with DocumentFigureClassifier (26 classes).
519 pub picture_classification: bool,
520 /// Rewrite code blocks (and detect their language) with CodeFormulaV2.
521 pub code: bool,
522 /// Decode display formulas to LaTeX with CodeFormulaV2.
523 pub formula: bool,
524}
525
526#[cfg(feature = "ml")]
527impl EnrichmentOptions {
528 fn any(&self) -> bool {
529 self.picture_classification || self.code || self.formula
530 }
531}
532
533#[cfg(feature = "ml")]
534/// The layout model's input for a page: the docling-exact scale-1.0 page
535/// image when the renderer produced one, else the legacy stretch of the 2×
536/// bitmap (browser / METS paths) — see [`layout::LayoutSrc`]. Public so the
537/// diagnostic examples feed [`layout::LayoutModel::predict`] the same input
538/// the pipeline does.
539pub fn layout_src(page: &PdfPage) -> layout::LayoutSrc<'_> {
540 match &page.image_layout {
541 Some(img) => layout::LayoutSrc::PageImage(img),
542 None => layout::LayoutSrc::Raw(&page.image),
543 }
544}
545
546#[cfg(feature = "ml")]
547/// The bitmap + px/pt scale the OCR reads (#254, docling#3877's
548/// `OcrOptions.scale`): the page's own render unless `ocr_scale` asks for a
549/// different resolution, where a PIL-bicubic resample of that render is built
550/// once per page (cached in `cache`) and shared by every OCR pass. Resampling
551/// — rather than a second native pdfium render — keeps one code path across
552/// PDF, image, and hOCR inputs and leaves the layout/TableFormer pixels (and
553/// with them the conformance baseline) untouched; the 144-dpi base render is
554/// itself supersampled down from 216 dpi, so an upscaled OCR view loses
555/// little against a native render.
556fn ocr_input<'a>(
557 cache: &'a mut Option<image::RgbImage>,
558 image: &'a image::RgbImage,
559 scale: f32,
560 ocr_scale: Option<f32>,
561) -> (&'a image::RgbImage, f32) {
562 match ocr_scale {
563 Some(s) if (s - scale).abs() > 1e-3 && image.width() > 1 => {
564 let f = s / scale;
565 let img = cache.get_or_insert_with(|| {
566 let dw = ((image.width() as f32 * f).round() as u32).max(1);
567 let dh = ((image.height() as f32 * f).round() as u32).max(1);
568 resample::pil_resize(image, dw, dh, resample::PilFilter::Bicubic)
569 });
570 (img, s)
571 }
572 _ => (image, scale),
573 }
574}
575
576#[cfg(feature = "ml")]
577/// A self-contained set of the per-page models (layout, OCR). Each parallel
578/// page-worker owns its own `Worker` so inference runs concurrently without
579/// sharing an ONNX session (`ort`'s `Session::run` is `&mut self`); only the
580/// rarely-hit TableFormer is shared (see [`TfSlot`]).
581struct Worker {
582 /// `None` when `no_ocr` skips layout entirely — no model load, no inference.
583 layout: Option<layout::LayoutModel>,
584 ocr: OcrSlot,
585 /// Shared TableFormer slot; `None` when `no_table_former`/`no_ocr` skip it.
586 tables: Option<SharedTables>,
587 /// Shared enrichment slots; `None` unless the corresponding flag is on.
588 classifier: Option<SharedClassifier>,
589 code_formula: Option<SharedCodeFormula>,
590 enrich: EnrichmentOptions,
591 /// Skip layout, OCR, and TableFormer; reconstruct text purely from the PDF's
592 /// embedded text layer. See [`Pipeline::no_ocr`].
593 no_ocr: bool,
594 /// Discard the embedded text layer and OCR every page. See
595 /// [`Pipeline::force_full_page_ocr`].
596 force_full_page_ocr: bool,
597 /// Keep text-panel pictures as pictures instead of demoting them to
598 /// paragraphs. See [`Pipeline::no_text_panels`].
599 no_text_panels: bool,
600 /// Never run OCR, but keep layout + TableFormer (#244) — docling's
601 /// `do_ocr=False`. See [`Pipeline::skip_ocr`].
602 skip_ocr: bool,
603 /// Which recognition model [`Self::ocr`] loads. See [`Pipeline::ocr_lang`].
604 ocr_lang: ocr::OcrLang,
605 /// OCR render scale override (px/pt, #254). See [`Pipeline::ocr_scale`].
606 ocr_scale: Option<f32>,
607}
608
609#[cfg(feature = "ml")]
610/// The worker's lazily-loaded OCR recognition model. `Missing` records a
611/// failed load (#244: degradation over failure — a deployment without the OCR
612/// model still gets layout + TableFormer, and OCR-dependent regions stay
613/// empty) so the load isn't retried per page.
614enum OcrSlot {
615 Unloaded,
616 Ready(ocr::OcrModel),
617 Missing,
618}
619
620#[cfg(feature = "ml")]
621impl Worker {
622 #[allow(clippy::too_many_arguments)] // mirrors the Pipeline's option set
623 fn load(
624 intra: usize,
625 tables: Option<SharedTables>,
626 enrich_slots: (Option<SharedClassifier>, Option<SharedCodeFormula>),
627 enrich: EnrichmentOptions,
628 no_ocr: bool,
629 skip_ocr: bool,
630 force_full_page_ocr: bool,
631 no_text_panels: bool,
632 ocr_lang: ocr::OcrLang,
633 ocr_scale: Option<f32>,
634 ) -> Result<Self, PdfError> {
635 Ok(Self {
636 layout: if no_ocr {
637 None
638 } else {
639 Some(layout::LayoutModel::load_with(intra).map_err(PdfError::Layout)?)
640 },
641 ocr: OcrSlot::Unloaded,
642 tables,
643 classifier: enrich_slots.0,
644 code_formula: enrich_slots.1,
645 enrich,
646 no_ocr,
647 skip_ocr,
648 force_full_page_ocr,
649 no_text_panels,
650 ocr_lang,
651 ocr_scale,
652 })
653 }
654
655 /// The OCR model, or `None` when this conversion must not (or cannot) OCR:
656 /// `skip_ocr` short-circuits, and a failed model load degrades to `None`
657 /// with a one-time warning instead of failing the conversion (#244) —
658 /// unless `force_full_page_ocr` demanded OCR explicitly, where a missing
659 /// model stays a hard error (the text layer was deliberately discarded, so
660 /// degrading would silently emit an empty document).
661 fn ocr_model(&mut self) -> Result<Option<&mut ocr::OcrModel>, PdfError> {
662 if self.skip_ocr {
663 return Ok(None);
664 }
665 if matches!(self.ocr, OcrSlot::Unloaded) {
666 match ocr::OcrModel::load(self.ocr_lang) {
667 Ok(model) => self.ocr = OcrSlot::Ready(model),
668 Err(e) if self.force_full_page_ocr => return Err(PdfError::Ocr(e)),
669 Err(e) => {
670 static WARNED: std::sync::Once = std::sync::Once::new();
671 WARNED.call_once(|| {
672 eprintln!(
673 "warning: OCR model unavailable ({e}); continuing without OCR — \
674 scanned pages and text inside images will come back empty \
675 (run scripts/install/download_dependencies.sh for the model)"
676 );
677 });
678 self.ocr = OcrSlot::Missing;
679 }
680 }
681 }
682 Ok(match &mut self.ocr {
683 OcrSlot::Ready(model) => Some(model),
684 _ => None,
685 })
686 }
687
688 /// Run layout (+ OCR for cell-less pages) + TableFormer and assemble page `n`
689 /// into its nodes and links. Pure given the page (mutates only the worker's
690 /// lazily-loaded OCR model), so it is safe to run concurrently across pages.
691 fn process(&mut self, n: usize, page: &mut PdfPage) -> Result<PageOut, PdfError> {
692 if self.no_ocr {
693 // Fastest path: no layout/OCR/TableFormer inference at all. The PDF's
694 // embedded text cells (if any) become flat, line-grouped paragraphs in
695 // reading order via the same orphan-region machinery that normally
696 // rescues text the detector missed — here it rescues *all* of it.
697 // Pages with no embedded text layer (scanned/image-only) yield nothing;
698 // convert those without `no_ocr`.
699 let parse = quality::parse_score(&page.cells);
700 let mut regions = Vec::new();
701 assemble::add_orphan_regions(&mut regions, &page.cells);
702 let table_rows = vec![None; regions.len()];
703 let enrich_out = vec![None; regions.len()];
704 let conf = quality::page_confidence(parse, ®ions, &[]);
705 let (nodes, links) = timing::timed("assemble_page", || {
706 assemble::assemble_page(page, regions, &table_rows, &enrich_out)
707 });
708 return Ok((nodes, links, conf));
709 }
710 self.normalize_orientation(n, page)?;
711 let regions = timing::timed("layout.predict", || {
712 self.layout
713 .as_mut()
714 .expect("layout model loaded unless no_ocr")
715 .predict(layout_src(page), page.width, page.height)
716 })
717 .map_err(|e| PdfError::Layout(format!("page {}: {e}", n + 1)))?;
718 self.finish_page(n, page, regions)
719 }
720
721 /// Content-based orientation normalization (#225), before any inference:
722 /// a physically rotated scan (sideways phone photo, landscape-fed sheet)
723 /// has `/Rotate 0`, so the metadata pass in `extract_page` never fires and
724 /// layout+OCR would read a sideways raster. Only pages with no text layer
725 /// at all are probed (a digital page's raster is upright by construction,
726 /// and its cells — not its pixels — carry the text); the detected angle
727 /// composes with any `/Rotate` normalization through the same
728 /// [`PdfPage::unrotate`] + display-space assembly mapping. Detection is
729 /// evidence-gated and degrades to a no-op — see [`orient`].
730 fn normalize_orientation(&mut self, n: usize, page: &mut PdfPage) -> Result<(), PdfError> {
731 let scanned =
732 page.cells.is_empty() && page.word_cells.is_empty() && page.code_cells.is_empty();
733 if self.no_ocr || self.skip_ocr || !scanned || page.image.width() <= 1 || !orient::enabled()
734 {
735 return Ok(());
736 }
737 // The probe reads text through the OCR model; without one (missing —
738 // #244 degradation) the page stays as rendered.
739 let Some(ocr) = self.ocr_model()? else {
740 return Ok(());
741 };
742 let deg = timing::timed("orient.detect", || orient::detect(&page.image, ocr));
743 if deg != 0 {
744 debug_log!(
745 "docling-pdf: page {}: content rotated {deg}° in the raster; \
746 un-rotating before layout/OCR",
747 n + 1
748 );
749 page.unrotate(deg);
750 }
751 Ok(())
752 }
753
754 /// Layout-detect a whole batch of pages with one inference call (issue #73),
755 /// then run each page's remaining stages (OCR / TableFormer / enrichment /
756 /// assembly) per page. Index-aligned with `items`; a layout failure fails
757 /// every page in the batch (they shared the one inference call).
758 fn process_batch(&mut self, items: &mut [(usize, PdfPage)]) -> Vec<Result<PageOut, PdfError>> {
759 if self.no_ocr {
760 // No layout model to batch — the text-layer-only path is per page.
761 return items
762 .iter_mut()
763 .map(|(n, page)| {
764 let n = *n;
765 self.process(n, page)
766 })
767 .collect();
768 }
769 // Orientation-normalize every scanned page before the shared layout
770 // call — the batched inference must see upright bitmaps too (#225).
771 for (n, page) in items.iter_mut() {
772 let n = *n;
773 if let Err(e) = self.normalize_orientation(n, page) {
774 // Model-load failure — every page in the batch needs the same
775 // model, so they all fail alike (mirrors the layout-error arm).
776 let msg = e.to_string();
777 return items
778 .iter()
779 .map(|_| Err(PdfError::Ocr(msg.clone())))
780 .collect();
781 }
782 }
783 let inputs: Vec<(layout::LayoutSrc<'_>, f32, f32)> = items
784 .iter()
785 .map(|(_, page)| (layout_src(page), page.width, page.height))
786 .collect();
787 let batched = timing::timed("layout.predict", || {
788 self.layout
789 .as_mut()
790 .expect("layout model loaded unless no_ocr")
791 .predict_batch(&inputs)
792 });
793 match batched {
794 Ok(all) => items
795 .iter_mut()
796 .zip(all)
797 .map(|((n, page), regions)| self.finish_page(*n, page, regions))
798 .collect(),
799 Err(e) => items
800 .iter()
801 .map(|(n, _)| Err(PdfError::Layout(format!("page {}: {e}", n + 1))))
802 .collect(),
803 }
804 }
805
806 /// Everything after layout detection: per-label confidence thresholds,
807 /// overlap resolution, orphan-text recovery, OCR for cell-less pages,
808 /// TableFormer, enrichment, and page assembly.
809 fn finish_page(
810 &mut self,
811 n: usize,
812 page: &mut PdfPage,
813 regions: Vec<layout::Region>,
814 ) -> Result<PageOut, PdfError> {
815 // Force-OCR is exactly "pretend the text layer is not there": clear
816 // every cell kind the extractors produced before anything reads them,
817 // and the ordinary no-text-layer machinery below — full-page OCR,
818 // OCR-fed TableFormer matching — takes over unchanged. (`no_ocr` wins
819 // when both are set, mirroring docling, where `force_full_page_ocr`
820 // is a sub-option of `do_ocr`; the no-ocr path never reaches here.)
821 // Done here rather than in `process` so the batched layout path
822 // (`process_batch` → `finish_page`) honors the flag too.
823 // Parse quality is scored on the extracted text layer before force-OCR
824 // discards it (docling's page-preprocessing stage runs before OCR too,
825 // so its parse_score also reflects the original text layer).
826 let parse = quality::parse_score(&page.cells);
827 // Recognition confidences of every OCR'd cell on this page → ocr_score.
828 let mut ocr_confs: Vec<f32> = Vec::new();
829 // The bitmap the OCR reads (#254): with `ocr_scale` set, a resample of
830 // the page render at the requested px/pt, built lazily on the first
831 // OCR use so non-OCR pages never pay for it. Copied out of `self` up
832 // front — the OCR sites hold `self.ocr_model()`'s mutable borrow.
833 let ocr_scale = self.ocr_scale;
834 let mut ocr_view: Option<image::RgbImage> = None;
835 if self.force_full_page_ocr {
836 page.cells.clear();
837 page.code_cells.clear();
838 page.word_cells.clear();
839 }
840 // Quant-robustness guard: the default int8 layout graph keeps its
841 // confidences near the 0.5 label thresholds, and a different CPU's
842 // quantized kernels can flip a whole page's detections under them —
843 // tables and paragraphs then dissolve into orphan one-liners while the
844 // same build converts the page perfectly elsewhere. When a dense
845 // digital page ends up with detections covering almost none of its
846 // text cells, re-run that one page on the fp32 graph (lazy-loaded,
847 // auto-int8 selection only) and keep whichever detections cover more.
848 let mut regions = regions;
849 if !page.cells.is_empty() {
850 let thresholded = |rs: &[layout::Region]| -> Vec<layout::Region> {
851 rs.iter()
852 .filter(|r| r.score >= layout::label_threshold(r.label))
853 .cloned()
854 .collect()
855 };
856 let text_cells = page
857 .cells
858 .iter()
859 .filter(|c| !c.text.trim().is_empty())
860 .count();
861 let cov = assemble::layout_cell_coverage(&thresholded(®ions), &page.cells);
862 if text_cells >= 15 && cov < 0.5 {
863 let retry = self
864 .layout
865 .as_mut()
866 .expect("layout model loaded unless no_ocr")
867 .predict_fp32_fallback(layout_src(page), page.width, page.height)
868 .map_err(|e| PdfError::Layout(format!("page {}: {e}", n + 1)))?;
869 if let Some(retry) = retry {
870 let cov2 = assemble::layout_cell_coverage(&thresholded(&retry), &page.cells);
871 if cov2 > cov {
872 debug_log!(
873 "docling-pdf: page {}: int8 layout covered {:.0}% of the text \
874 cells; the fp32 retry covers {:.0}% — using it",
875 n + 1,
876 cov * 100.0,
877 cov2 * 100.0
878 );
879 regions = retry;
880 }
881 }
882 }
883 }
884 // docling's LayoutPostprocessor drops each detection below its label's
885 // confidence threshold (stricter than the 0.3 base the predictor keeps),
886 // before any overlap resolution. This removes the low-confidence tables /
887 // pictures / list-items that otherwise double-emit or mis-classify.
888 if env::flag("DOCLING_RS_DEBUG_REGIONS") {
889 for r in ®ions {
890 eprintln!(
891 "DBG raw {} {:.2} [{:.0},{:.0},{:.0},{:.0}]",
892 r.label, r.score, r.l, r.t, r.r, r.b
893 );
894 }
895 }
896 regions.retain(|r| r.score >= layout::label_threshold(r.label));
897 // docling's same-label picture dedup runs on the thresholded
898 // detections, before overlap resolution: a figure proposed both whole
899 // and as sub-panels collapses to one box (see `dedup_pictures`).
900 assemble::dedup_pictures(&mut regions);
901 // Resolve overlapping detections once, before OCR.
902 let mut regions = assemble::resolve(regions);
903 // Emit text the detector missed as orphan text regions (docling parity).
904 assemble::add_orphan_regions(&mut regions, &page.cells);
905 // Drop phantom empty low-confidence picture boxes (docling parity).
906 assemble::drop_false_pictures(&mut regions, &page.cells, page.width, page.height);
907 // A regular region fully inside a surviving table/index/picture is that
908 // special's child (a cell / in-figure label), not a separate block —
909 // remove it so it isn't emitted twice (docling parity).
910 assemble::drop_contained_regulars(&mut regions);
911 // No text layer → recognise text from the page image via OCR.
912 let ocred = page.cells.is_empty();
913 if ocred {
914 // `None` = `skip_ocr` or a missing model (#244): the page keeps
915 // its layout regions (and TableFormer structure below) with no
916 // recognized text, instead of failing the conversion.
917 if let Some(ocr) = self.ocr_model()? {
918 let (img, scl) = ocr_input(&mut ocr_view, &page.image, page.scale, ocr_scale);
919 let cells = timing::timed("ocr.page", || ocr.ocr_page(img, ®ions, scl))
920 .map_err(|e| PdfError::Ocr(format!("page {}: {e}", n + 1)))?;
921 ocr_confs.extend(cells.iter().map(|(_, conf)| conf));
922 page.cells = cells.into_iter().map(|(cell, _)| cell).collect();
923 // Table interiors carry no words yet: region-scoped OCR skips
924 // table labels, and a scanned page has no pdfium text layer — so
925 // TableFormer's cell matcher got an empty word list and the table
926 // dissolved (#173). Recognize the table regions' word crops
927 // (mirroring the browser scanned path): `word_cells` feeds the
928 // matcher, and the same cells join `cells` so the geometric
929 // fallback and the table's region text see them too.
930 if regions.iter().any(|r| assemble::is_table_like(r.label)) {
931 let (img, scl) = ocr_input(&mut ocr_view, &page.image, page.scale, ocr_scale);
932 let words = timing::timed("ocr.table_words", || {
933 ocr.ocr_table_words(img, ®ions, scl)
934 })
935 .map_err(|e| PdfError::Ocr(format!("page {}: {e}", n + 1)))?;
936 ocr_confs.extend(words.iter().map(|(_, conf)| conf));
937 let words: Vec<_> = words.into_iter().map(|(cell, _)| cell).collect();
938 page.cells.extend(words.iter().cloned());
939 page.word_cells = words;
940 }
941 }
942 }
943 // Region-scoped OCR skips `picture` interiors, and a digital page's
944 // text layer cannot see into an embedded raster either — so a figure
945 // that is really a text box (terms-and-conditions exported as an
946 // image) lost its words on every page kind. Python docling OCRs the
947 // bitmap-covered areas of *every* page — even digital ones — once they
948 // exceed `bitmap_area_threshold` (5 % of the page); the browser paths
949 // already do. Recognize the big text-less crops here too; the panel
950 // demotion / orphan recovery below place the lines.
951 let mut pic_cells: Vec<pdfium_backend::TextCell> = Vec::new();
952 {
953 let page_area = (page.width * page.height).max(1.0);
954 let has_text = |r: &layout::Region| {
955 page.cells.iter().any(|c| {
956 let ca = ((c.r - c.l) * (c.b - c.t)).max(1.0);
957 let ix = (r.r.min(c.r) - r.l.max(c.l)).max(0.0);
958 let iy = (r.b.min(c.b) - r.t.max(c.t)).max(0.0);
959 !c.text.trim().is_empty() && ix * iy / ca > 0.5
960 })
961 };
962 // A captioned picture can never demote to a text panel (see
963 // recover_text_panels), and on digital pages its speculative OCR
964 // would be discarded anyway — don't pay for it.
965 let captioned = |r: &layout::Region| {
966 regions.iter().any(|c| {
967 c.label == "caption"
968 && c.r.min(r.r) - c.l.max(r.l) > 0.0
969 && ((c.t >= r.b && c.t - r.b <= 25.0) || (r.t >= c.b && r.t - c.b <= 25.0))
970 })
971 };
972 let bare: Vec<layout::Region> = regions
973 .iter()
974 .filter(|r| {
975 r.label == "picture"
976 && (r.r - r.l) * (r.b - r.t) / page_area >= 0.05
977 && !has_text(r)
978 && (ocred || !captioned(r))
979 })
980 .map(|r| layout::Region {
981 label: "text",
982 ..r.clone()
983 })
984 .collect();
985 // Speculative OCR (#244): with `skip_ocr` or no model, big bare
986 // pictures simply stay pictures.
987 if let (false, Some(ocr)) = (bare.is_empty(), self.ocr_model()?) {
988 let (img, scl) = ocr_input(&mut ocr_view, &page.image, page.scale, ocr_scale);
989 let scored = timing::timed("ocr.pictures", || ocr.ocr_page(img, &bare, scl))
990 .map_err(|e| PdfError::Ocr(format!("page {}: {e}", n + 1)))?;
991 // Speculative in-picture OCR counts toward ocr_score only on
992 // OCR'd pages, where the recognized lines actually join the
993 // output; on a digital page they may be discarded below.
994 if ocred {
995 ocr_confs.extend(scored.iter().map(|(_, conf)| conf));
996 }
997 pic_cells = scored.into_iter().map(|(cell, _)| cell).collect();
998 page.cells.extend(pic_cells.iter().cloned());
999 }
1000 }
1001 let cells_before_pic_ocr = page.cells.len() - pic_cells.len();
1002 // A "picture" that is really a colored text panel — dense, wide,
1003 // multi-line — reads out as paragraphs instead of shipping as pixels;
1004 // sparse in-picture text (a chart's labels) keeps the crop and stays
1005 // inside it as the picture's silent children (docling parity, #200).
1006 // `no_text_panels` (#173) opts out entirely for image-extraction
1007 // workflows.
1008 if !self.no_text_panels {
1009 assemble::recover_text_panels(&mut regions, &page.cells);
1010 }
1011 // On an OCR'd page, in-picture text that did NOT demote its picture
1012 // mostly stays silent, exactly as in docling: its postprocess step
1013 // "Remove regular clusters that are included in wrappers" walks
1014 // SPECIAL_TYPES — which includes PICTURE — so an orphan text cluster
1015 // >80 % contained in a kept picture becomes that picture's child and
1016 // never reaches the serializer. Only border-straddlers (≤80 %
1017 // containment) survive as text. Emitting *everything* here used to
1018 // splice a chart's OCR'd axis ticks into the body text right next to
1019 // the image chunk (#200) — so the orphan pass places the recognized
1020 // lines, then the same containment drop that handled the first wave
1021 // re-runs to swallow the in-picture ones.
1022 if ocred && !pic_cells.is_empty() {
1023 // Pictures (and wrappers) no longer count as claimers (#165), so
1024 // the plain orphan pass places the recognized lines directly.
1025 assemble::add_orphan_regions(&mut regions, &pic_cells);
1026 assemble::drop_contained_regulars(&mut regions);
1027 } else if !ocred && !pic_cells.is_empty() {
1028 // Digital page, picture kept: its speculative OCR cells must not
1029 // linger in the text-cell set (they were appended at the tail).
1030 let kept: Vec<layout::Region> = regions
1031 .iter()
1032 .filter(|r| r.label == "picture")
1033 .cloned()
1034 .collect();
1035 let tail = page.cells.split_off(cells_before_pic_ocr);
1036 page.cells.extend(tail.into_iter().filter(|c| {
1037 !kept.iter().any(|r| {
1038 let ca = ((c.r - c.l) * (c.b - c.t)).max(1.0);
1039 let ix = (r.r.min(c.r) - r.l.max(c.l)).max(0.0);
1040 let iy = (r.b.min(c.b) - r.t.max(c.t)).max(0.0);
1041 ix * iy / ca > 0.5
1042 })
1043 }));
1044 }
1045 // A text-less *table* detected inside a picture on a digital page — a
1046 // screenshot of a table (2203's Figure 10) — has no text layer and no
1047 // scanned-path OCR to feed it, so its grid used to serialize empty and
1048 // the whole element vanished. docling OCRs bitmap-covered areas on
1049 // every page kind and its table cluster collects those cells; mirror
1050 // the scanned path for exactly these tables: recognize word crops and
1051 // feed them to the TableFormer matcher and the cell set.
1052 if !ocred {
1053 let has_text = |t: &layout::Region| {
1054 page.cells.iter().any(|c| {
1055 let ca = ((c.r - c.l) * (c.b - c.t)).max(1.0);
1056 let ix = (t.r.min(c.r) - t.l.max(c.l)).max(0.0);
1057 let iy = (t.b.min(c.b) - t.t.max(c.t)).max(0.0);
1058 !c.text.trim().is_empty() && ix * iy / ca > 0.5
1059 })
1060 };
1061 let in_picture = |t: &layout::Region| {
1062 regions.iter().any(|r| {
1063 r.label == "picture" && {
1064 let ta = ((t.r - t.l) * (t.b - t.t)).max(1.0);
1065 let ix = (r.r.min(t.r) - r.l.max(t.l)).max(0.0);
1066 let iy = (r.b.min(t.b) - r.t.max(t.t)).max(0.0);
1067 ix * iy / ta > 0.5
1068 }
1069 })
1070 };
1071 let pic_tables: Vec<layout::Region> = regions
1072 .iter()
1073 .filter(|t| assemble::is_table_like(t.label) && !has_text(t) && in_picture(t))
1074 .cloned()
1075 .collect();
1076 // Same degradation as above: without OCR the in-picture table
1077 // keeps its structure (TableFormer is geometry-driven) minus text.
1078 if let (false, Some(ocr)) = (pic_tables.is_empty(), self.ocr_model()?) {
1079 let (img, scl) = ocr_input(&mut ocr_view, &page.image, page.scale, ocr_scale);
1080 let words = timing::timed("ocr.table_words", || {
1081 ocr.ocr_table_words(img, &pic_tables, scl)
1082 })
1083 .map_err(|e| PdfError::Ocr(format!("page {}: {e}", n + 1)))?;
1084 ocr_confs.extend(words.iter().map(|(_, conf)| conf));
1085 let words: Vec<_> = words.into_iter().map(|(cell, _)| cell).collect();
1086 page.cells.extend(words.iter().cloned());
1087 page.word_cells.extend(words);
1088 }
1089 }
1090 // TableFormer structure per table region (else geometric fallback). The
1091 // shared slot is only locked (and lazily loaded) when the page actually
1092 // has a table, so table-free documents never pay for TableFormer at all.
1093 let mut table_rows: Vec<Option<tf_core::TableGrid>> = vec![None; regions.len()];
1094 if let Some(slot) = self.tables.as_ref() {
1095 if regions.iter().any(|r| assemble::is_table_like(r.label)) {
1096 timing::timed("tableformer", || {
1097 let mut guard = slot.lock().unwrap();
1098 if matches!(*guard, TfSlot::Unloaded) {
1099 // Tables serialise on this mutex, so the one instance
1100 // gets the shared thread budget (quota-aware, #262) —
1101 // DOCLING_RS_TF_INTRA narrows it further where the
1102 // memory-per-thread tradeoff matters more than table
1103 // latency.
1104 *guard = match tableformer::TableFormer::load_with(tf_intra()) {
1105 Some(tf) => TfSlot::Ready(tf),
1106 None => TfSlot::Missing,
1107 };
1108 }
1109 if let TfSlot::Ready(tf) = &mut *guard {
1110 for (i, r) in regions.iter().enumerate() {
1111 if assemble::is_table_like(r.label) {
1112 table_rows[i] = tf.predict_table_rows(
1113 &page.image,
1114 [r.l, r.t, r.r, r.b],
1115 &page.word_cells,
1116 );
1117 }
1118 }
1119 }
1120 });
1121 }
1122 }
1123 if env::flag("DOCLING_RS_DEBUG_REGIONS") {
1124 for (i, r) in regions.iter().enumerate() {
1125 eprintln!(
1126 "DBG final {} {:.2} [{:.0},{:.0},{:.0},{:.0}] rows={:?}",
1127 r.label,
1128 r.score,
1129 r.l,
1130 r.t,
1131 r.r,
1132 r.b,
1133 table_rows[i]
1134 .as_ref()
1135 .map(|t| (t.rows.len(), t.rows.first().map(|r| r.len())))
1136 );
1137 }
1138 eprintln!(
1139 "DBG cells={} words={}",
1140 page.cells.len(),
1141 page.word_cells.len()
1142 );
1143 }
1144 // Enrichment passes (opt-in): DocumentPictureClassifier over picture
1145 // regions, CodeFormulaV2 over code/formula regions. Same shared-slot
1146 // shape as TableFormer — one lazily-loaded instance per pipeline, only
1147 // ever locked when a page actually has a matching region.
1148 let mut enrich_out: Vec<Option<assemble::Enrichment>> = vec![None; regions.len()];
1149 if let Some(slot) = self.classifier.as_ref() {
1150 if regions.iter().any(|r| r.label == "picture") {
1151 timing::timed("picture_classifier", || {
1152 let mut guard = slot.lock().unwrap();
1153 if matches!(*guard, EnrichSlot::Unloaded) {
1154 *guard = match enrich::PictureClassifier::load_with(intra_threads()) {
1155 Some(m) => EnrichSlot::Ready(m),
1156 None => EnrichSlot::Missing,
1157 };
1158 }
1159 if let EnrichSlot::Ready(model) = &mut *guard {
1160 for (i, r) in regions.iter().enumerate() {
1161 if r.label != "picture" {
1162 continue;
1163 }
1164 let Some(crop) = assemble::crop_region_scaled(
1165 page,
1166 [r.l, r.t, r.r, r.b],
1167 enrich::CLASSIFIER_SCALE,
1168 ) else {
1169 continue;
1170 };
1171 match model.classify(&crop) {
1172 Ok(classes) => {
1173 enrich_out[i] =
1174 Some(assemble::Enrichment::PictureClasses(classes));
1175 }
1176 Err(e) => eprintln!("docling-pdf: page {}: {e}", n + 1),
1177 }
1178 }
1179 }
1180 });
1181 }
1182 }
1183 if let Some(slot) = self.code_formula.as_ref() {
1184 let wants = |label: &str| {
1185 (label == "code" && self.enrich.code) || (label == "formula" && self.enrich.formula)
1186 };
1187 if regions.iter().any(|r| wants(r.label)) {
1188 timing::timed("code_formula", || {
1189 let mut guard = slot.lock().unwrap();
1190 if matches!(*guard, EnrichSlot::Unloaded) {
1191 *guard = match enrich::CodeFormula::load_with(intra_threads()) {
1192 Some(m) => EnrichSlot::Ready(m),
1193 None => EnrichSlot::Missing,
1194 };
1195 }
1196 if let EnrichSlot::Ready(model) = &mut *guard {
1197 for (i, r) in regions.iter().enumerate() {
1198 if !wants(r.label) {
1199 continue;
1200 }
1201 // docling crops the postprocessed cluster box — the
1202 // union of the region's text cells, not the raw
1203 // detector box — expanded by 18% per side, at
1204 // ~120 dpi.
1205 let [bl, bt, br, bb] = assemble::region_cell_bbox(r, &page.cells)
1206 .unwrap_or([r.l, r.t, r.r, r.b]);
1207 let (w, h) = (br - bl, bb - bt);
1208 let ex = enrich::CODE_FORMULA_EXPANSION;
1209 let bbox = [bl - w * ex, bt - h * ex, br + w * ex, bb + h * ex];
1210 let Some(crop) = assemble::crop_region_scaled(
1211 page,
1212 bbox,
1213 enrich::CODE_FORMULA_SCALE,
1214 ) else {
1215 continue;
1216 };
1217 let kind = if r.label == "code" {
1218 enrich::CodeFormulaKind::Code
1219 } else {
1220 enrich::CodeFormulaKind::Formula
1221 };
1222 match model.predict(&crop, kind) {
1223 Ok(text) => {
1224 enrich_out[i] = Some(match kind {
1225 enrich::CodeFormulaKind::Code => {
1226 let (code, language) =
1227 enrich::extract_code_language(&text);
1228 assemble::Enrichment::Code {
1229 language,
1230 text: code,
1231 }
1232 }
1233 enrich::CodeFormulaKind::Formula => {
1234 assemble::Enrichment::Formula { latex: text }
1235 }
1236 });
1237 }
1238 Err(e) => eprintln!("docling-pdf: page {}: {e}", n + 1),
1239 }
1240 }
1241 }
1242 });
1243 }
1244 }
1245 // Score the final region set (docling assigns layout_score over the
1246 // postprocessed clusters — the same set assemble_page consumes).
1247 let conf = quality::page_confidence(parse, ®ions, &ocr_confs);
1248 let (nodes, links) = timing::timed("assemble_page", || {
1249 assemble::assemble_page(page, regions, &table_rows, &enrich_out)
1250 });
1251 Ok((nodes, links, conf))
1252 }
1253}
1254
1255#[cfg(feature = "ml")]
1256/// Per-worker ONNX intra-op threads. The layout model is memory-bandwidth bound,
1257/// so on a typical machine two threads per worker (sharing one in-cache copy of
1258/// the weights) extracts more throughput than one fat model or many single-thread
1259/// workers. `DOCLING_RS_PDF_INTRA` overrides for per-machine tuning.
1260fn pdf_intra() -> usize {
1261 if let Some(n) = env::parse::<usize>("DOCLING_RS_PDF_INTRA").filter(|&n| n > 0) {
1262 return n;
1263 }
1264 if intra_threads() >= 2 {
1265 2
1266 } else {
1267 1
1268 }
1269}
1270
1271#[cfg(feature = "ml")]
1272/// How many page-workers to spin up for a multi-page PDF. `DOCLING_RS_PDF_WORKERS`
1273/// overrides; otherwise size the pool so `workers × intra ≈ cores`, capped at 4 so
1274/// a worst-case pool holds a bounded amount of model memory (~0.4 GB per worker)
1275/// and does not oversaturate the memory bus with model-weight traffic.
1276fn pdf_worker_count() -> usize {
1277 if let Some(n) = env::parse::<usize>("DOCLING_RS_PDF_WORKERS").filter(|&n| n > 0) {
1278 return n;
1279 }
1280 (intra_threads() / pdf_intra()).clamp(1, 4)
1281}
1282
1283#[cfg(feature = "ml")]
1284/// Max pages a worker layout-detects with one batched inference call (issue
1285/// #73). Workers drain the work channel opportunistically up to this size —
1286/// whatever is already rendered gets batched, so batching never *waits* for
1287/// pages and adds no latency when rendering is the bottleneck.
1288///
1289/// Default: 4 on 8+ cores, 1 (per-page) below. Measured on a 4-core box the
1290/// batch only adds cache pressure and costs pipeline overlap (2 workers × 2
1291/// threads: 8.1 s/conv at batch=1 vs 9.3 s at batch=4 on the 9-page
1292/// 2206.01062 fixture); the single-session amortization it buys needs the
1293/// wider thread budget of a many-core machine. Output is bit-identical at
1294/// every batch size, so this is purely a throughput knob.
1295/// `DOCLING_RS_PDF_LAYOUT_BATCH` overrides; `1` restores per-page inference.
1296fn pdf_layout_batch() -> usize {
1297 env::parse::<usize>("DOCLING_RS_PDF_LAYOUT_BATCH")
1298 .filter(|&n| n > 0)
1299 .unwrap_or_else(|| if intra_threads() >= 8 { 4 } else { 1 })
1300}
1301
1302#[cfg(feature = "ml")]
1303/// Minimum page count before a PDF is worth the parallel worker pool. Below this,
1304/// the serial primary (running its model on every core) is faster than fanning out
1305/// — the helper pool's one-time model-load cost only pays off once enough pages
1306/// share it. `DOCLING_RS_PDF_PARALLEL_MIN` overrides.
1307fn pdf_parallel_min() -> usize {
1308 env::parse::<usize>("DOCLING_RS_PDF_PARALLEL_MIN")
1309 .filter(|&n| n > 0)
1310 .unwrap_or(6)
1311}
1312
1313#[cfg(feature = "ml")]
1314/// A reusable PDF pipeline. The **primary** worker runs its models on every core,
1315/// so a single-page / small / image / METS input is converted at full intra-op
1316/// speed with no pool to load. A document with enough pages instead fans out
1317/// across a **pool** of narrower workers processed concurrently. Both load lazily
1318/// and are cached for reuse, so a one-shot conversion only pays for what it uses.
1319pub struct Pipeline {
1320 /// Full-intra worker for the serial path; loaded on first serial use.
1321 primary: Option<Worker>,
1322 /// Narrower workers (≈cores/`target_workers` threads each) for the parallel
1323 /// path; loaded on first multi-page use and cached.
1324 pool: Vec<Worker>,
1325 /// The single TableFormer instance every worker shares (see [`TfSlot`]).
1326 tables: SharedTables,
1327 /// The shared enrichment-model slots (same pattern as [`TfSlot`]).
1328 classifier: SharedClassifier,
1329 code_formula: SharedCodeFormula,
1330 /// Desired pool size for multi-page documents.
1331 target_workers: usize,
1332 /// Page count at/above which the parallel pool is worth its load cost.
1333 parallel_min: usize,
1334 /// Skip loading/running TableFormer; table regions fall back to geometric
1335 /// reconstruction. See [`Pipeline::no_table_former`].
1336 no_table_former: bool,
1337 /// Skip layout, OCR, and TableFormer entirely. See [`Pipeline::no_ocr`].
1338 no_ocr: bool,
1339 /// Keep layout + TableFormer, never OCR (#244). See [`Pipeline::skip_ocr`].
1340 skip_ocr: bool,
1341 /// OCR every page even when it carries a text layer. See
1342 /// [`Pipeline::force_full_page_ocr`].
1343 force_full_page_ocr: bool,
1344 /// Never demote text-panel pictures. See [`Pipeline::no_text_panels`].
1345 no_text_panels: bool,
1346 /// Opt-in enrichment passes. See [`Pipeline::enrichments`].
1347 enrich: EnrichmentOptions,
1348 /// 1-based inclusive page window to convert. See [`Pipeline::pages`].
1349 page_range: Option<(usize, usize)>,
1350 /// OCR recognition language. See [`Pipeline::ocr_lang`].
1351 ocr_lang: ocr::OcrLang,
1352 /// Which regions feed the OCR (#254). See [`Pipeline::ocr_mode`].
1353 ocr_mode: ocr::OcrMode,
1354 /// OCR render scale override in px/pt (#254). See [`Pipeline::ocr_scale`].
1355 ocr_scale: Option<f32>,
1356 /// Heading-level inference (#302). See [`Pipeline::heading_hierarchy`].
1357 heading_hierarchy: HeadingHierarchyOptions,
1358 /// Optional per-page progress hook `(done, selected_total)`, invoked after
1359 /// each page finishes on both the serial and parallel buffered paths. Set
1360 /// by the CLI batch mode for dot-progress; `None` costs nothing.
1361 progress: Option<Arc<dyn Fn(usize, usize) + Send + Sync>>,
1362}
1363
1364#[cfg(feature = "ml")]
1365impl Pipeline {
1366 /// Construct the pipeline. Models load lazily on first use (full-intra primary
1367 /// for serial inputs, the helper pool for multi-page PDFs), so nothing is
1368 /// loaded that a given document doesn't need.
1369 pub fn new() -> Result<Self, PdfError> {
1370 Ok(Self {
1371 primary: None,
1372 pool: Vec::new(),
1373 tables: Arc::new(Mutex::new(TfSlot::Unloaded)),
1374 classifier: Arc::new(Mutex::new(EnrichSlot::Unloaded)),
1375 code_formula: Arc::new(Mutex::new(EnrichSlot::Unloaded)),
1376 target_workers: pdf_worker_count(),
1377 parallel_min: pdf_parallel_min(),
1378 no_table_former: false,
1379 no_ocr: false,
1380 skip_ocr: false,
1381 force_full_page_ocr: false,
1382 no_text_panels: false,
1383 enrich: EnrichmentOptions::default(),
1384 page_range: None,
1385 ocr_lang: ocr::OcrLang::from_env(),
1386 ocr_mode: ocr::OcrMode::from_env(),
1387 ocr_scale: ocr::scale_from_env(),
1388 heading_hierarchy: HeadingHierarchyOptions::default(),
1389 progress: None,
1390 })
1391 }
1392
1393 /// Infer section-header levels after assembly (#302, docling's
1394 /// `HeadingHierarchyModel`): PDF bookmarks > legal/outline numbering >
1395 /// font style, off by default — see [`HeadingHierarchyOptions`]. Pure
1396 /// post-processing configuration; for a warm pipeline use
1397 /// [`set_heading_hierarchy`](Self::set_heading_hierarchy).
1398 pub fn heading_hierarchy(mut self, opts: HeadingHierarchyOptions) -> Self {
1399 self.heading_hierarchy = opts;
1400 self
1401 }
1402
1403 /// In-place variant of [`heading_hierarchy`](Self::heading_hierarchy) for
1404 /// a long-lived pipeline (docling-serve's warm instance) — like
1405 /// [`set_pages`](Self::set_pages), set it before every conversion so no
1406 /// request inherits a previous one's choice.
1407 pub fn set_heading_hierarchy(&mut self, opts: HeadingHierarchyOptions) {
1408 self.heading_hierarchy = opts;
1409 }
1410
1411 /// Run the enabled heading-hierarchy stage (#302) on an assembled
1412 /// document: gather the outline (bookmarks) and the per-page glyph styles
1413 /// on demand, then assign levels in place. `bytes` is `None` on paths
1414 /// with no PDF behind them (standalone images, METS) — those degrade to
1415 /// the numbering signal, exactly like docling without parsed pages.
1416 fn apply_heading_hierarchy(
1417 &self,
1418 nodes: &mut [Node],
1419 bytes: Option<&[u8]>,
1420 password: Option<&str>,
1421 ) {
1422 let opts = &self.heading_hierarchy;
1423 if !opts.enabled {
1424 return;
1425 }
1426 let outline = match bytes {
1427 Some(bytes) if opts.use_bookmarks => outline::extract_outline(bytes),
1428 _ => Vec::new(),
1429 };
1430 let styles = match bytes {
1431 Some(bytes) if opts.use_style => {
1432 let pages = heading_hierarchy::heading_pages(nodes);
1433 pdfium_backend::glyph_styles(bytes, password, &pages)
1434 }
1435 _ => Default::default(),
1436 };
1437 heading_hierarchy::apply(nodes, &outline, &styles, opts);
1438 }
1439
1440 /// Install (or clear) the per-page progress hook: called with
1441 /// `(pages_done, pages_selected)` after each page completes during
1442 /// [`convert`](Self::convert). Shared with the parallel workers, so the
1443 /// callback must be cheap and thread-safe.
1444 pub fn set_progress(&mut self, cb: Option<Arc<dyn Fn(usize, usize) + Send + Sync>>) {
1445 self.progress = cb;
1446 }
1447
1448 /// Convert only pages `first..=last` (**1-based**, like the page numbers a
1449 /// PDF viewer shows — issue #80's `--pages A-B`). Out-of-range pages are
1450 /// skipped before rasterization, so the cost is proportional to the window,
1451 /// not the document. `last` past the end of the document clamps; a window
1452 /// that selects no pages at all (`first` beyond the last page) is an error
1453 /// at convert time. `None` (the default) converts everything.
1454 pub fn pages(mut self, range: Option<(usize, usize)>) -> Self {
1455 self.page_range = range;
1456 self
1457 }
1458
1459 /// In-place variant of [`pages`](Self::pages) for a long-lived pipeline
1460 /// (e.g. docling-serve's warm instance) that applies a per-request window
1461 /// without rebuilding — unlike the model switches, the window is pure
1462 /// configuration. Set it before every conversion; it stays until changed.
1463 pub fn set_pages(&mut self, range: Option<(usize, usize)>) {
1464 self.page_range = range;
1465 }
1466
1467 /// OCR recognition language (see [`OcrLang`]): English by default, `ch`
1468 /// for the multilingual docling-conformance model. `None` keeps the
1469 /// process default (`DOCLING_RS_OCR_LANG`, else English). Set before the
1470 /// first conversion; for a warm pipeline use
1471 /// [`set_ocr_lang`](Self::set_ocr_lang).
1472 pub fn ocr_lang(mut self, lang: Option<ocr::OcrLang>) -> Self {
1473 self.set_ocr_lang(lang);
1474 self
1475 }
1476
1477 /// In-place variant of [`ocr_lang`](Self::ocr_lang) for a long-lived
1478 /// pipeline (docling-serve's warm instance). Unlike the page window this
1479 /// is a *model* switch: any worker whose cached recognition model was
1480 /// loaded for a different language drops it, to be lazily reloaded on the
1481 /// next OCR-needing page (cheap — the rec models are ~10 MB).
1482 pub fn set_ocr_lang(&mut self, lang: Option<ocr::OcrLang>) {
1483 let lang = lang.unwrap_or_else(ocr::OcrLang::from_env);
1484 self.ocr_lang = lang;
1485 for worker in self.primary.iter_mut().chain(self.pool.iter_mut()) {
1486 if worker.ocr_lang != lang {
1487 worker.ocr_lang = lang;
1488 worker.ocr = OcrSlot::Unloaded;
1489 }
1490 }
1491 }
1492
1493 /// Resolve the configured 1-based window against a page count into the
1494 /// 0-based inclusive form the backend walks, validating it selects at
1495 /// least one existing page.
1496 fn resolve_range(&self, total: usize) -> Result<Option<(usize, usize)>, PdfError> {
1497 let Some((first, last)) = self.page_range else {
1498 return Ok(None);
1499 };
1500 if first == 0 || last < first {
1501 return Err(PdfError::Pdfium(format!(
1502 "invalid page range {first}-{last} (pages are 1-based, first <= last)"
1503 )));
1504 }
1505 if first > total {
1506 return Err(PdfError::Pdfium(format!(
1507 "page range {first}-{last} is outside the document ({total} page(s))"
1508 )));
1509 }
1510 Ok(Some((first - 1, last.min(total) - 1)))
1511 }
1512
1513 /// Enable the opt-in enrichment passes (docling's
1514 /// `do_picture_classification` / `do_code_enrichment` /
1515 /// `do_formula_enrichment`). Each enabled pass lazily loads its model on
1516 /// the first matching region; a missing model warns once and is skipped.
1517 /// Set before the first conversion (no effect on already-loaded workers).
1518 pub fn enrichments(mut self, opts: EnrichmentOptions) -> Self {
1519 self.enrich = opts;
1520 self
1521 }
1522
1523 /// Skip loading and running the TableFormer table-structure model. Table
1524 /// regions still get emitted, but reconstructed geometrically from cell
1525 /// positions instead of via the ONNX model's predicted structure — faster
1526 /// (no model load, no per-table inference) at the cost of table fidelity.
1527 /// No effect if a worker is already loaded; set this before the first
1528 /// conversion.
1529 pub fn no_table_former(mut self, disable: bool) -> Self {
1530 self.no_table_former = disable;
1531 self
1532 }
1533
1534 /// Keep every detected `picture` region as a picture. By default an
1535 /// *uncaptioned* picture that reads like a dense, uniform text panel (a
1536 /// terms-and-conditions box exported as an image) is demoted into
1537 /// paragraphs (#157); a chart the layout mislabels can still trip that
1538 /// heuristic on scanned pages, and image-extraction workflows may simply
1539 /// want every crop — this flag disables the demotion entirely (#173).
1540 /// No effect on already-loaded workers; set before the first conversion.
1541 pub fn no_text_panels(mut self, disable: bool) -> Self {
1542 self.no_text_panels = disable;
1543 self
1544 }
1545
1546 /// Skip layout detection, OCR, and TableFormer entirely — no model load, no
1547 /// inference of any kind. The PDF's embedded text cells are grouped by line
1548 /// and emitted as plain paragraphs in reading order: no headings, lists,
1549 /// tables, code blocks, or pictures, since that structure comes from the
1550 /// layout model. The fastest possible PDF path, but pages with no embedded
1551 /// text layer (scanned/image-only PDFs) yield no text at all — convert those
1552 /// without this flag. Implies `no_table_former`. No effect if a worker is
1553 /// already loaded; set this before the first conversion.
1554 pub fn no_ocr(mut self, disable: bool) -> Self {
1555 self.no_ocr = disable;
1556 self
1557 }
1558
1559 /// Never run OCR, but keep layout detection and TableFormer — docling's
1560 /// independent `do_ocr=False` (#244), the counterpart of
1561 /// [`no_table_former`](Self::no_table_former). Unlike
1562 /// [`no_ocr`](Self::no_ocr) (which skips the whole ML stack), structured
1563 /// output — headings, tables, pictures, reading order — is preserved;
1564 /// only text that exists solely as pixels is lost: scanned pages come
1565 /// back with their regions empty, and the speculative OCR of large
1566 /// embedded images never runs. The OCR model is never loaded. Ignored
1567 /// when `no_ocr` is set (there is no OCR to skip);
1568 /// takes precedence over [`force_full_page_ocr`](Self::force_full_page_ocr),
1569 /// mirroring docling where forcing is a sub-option of `do_ocr`.
1570 pub fn skip_ocr(mut self, disable: bool) -> Self {
1571 self.skip_ocr = disable;
1572 self
1573 }
1574
1575 /// OCR every page from its rendered image even when the page carries an
1576 /// embedded text layer — docling's `force_full_page_ocr`. The escape hatch
1577 /// for text layers that exist but lie: broken encodings, subset fonts with
1578 /// garbage mappings, a scanned form with a few typed-in fields. Ignored
1579 /// when [`no_ocr`](Self::no_ocr) is set, mirroring docling (there
1580 /// `force_full_page_ocr` is a sub-option of `do_ocr`).
1581 pub fn force_full_page_ocr(mut self, force: bool) -> Self {
1582 self.force_full_page_ocr = force;
1583 self
1584 }
1585
1586 /// Which document regions feed the OCR — docling's `OcrMode` (#254). The
1587 /// default (`default` = `pdf_aware_layout_regions`) is the standard
1588 /// text-layer-aware behavior; `full_page`/`layout_regions` discard the
1589 /// text layer like [`force_full_page_ocr`](Self::force_full_page_ocr)
1590 /// (see [`ocr::OcrMode`] for why both map onto it). Whichever of the flag
1591 /// and the mode demands forcing wins, mirroring docling's
1592 /// `force_full_page_ocr` → `mode=full_page` bridge. `None` keeps the
1593 /// process default (`DOCLING_RS_OCR_MODE`, else `default`).
1594 pub fn ocr_mode(mut self, mode: Option<ocr::OcrMode>) -> Self {
1595 self.ocr_mode = mode.unwrap_or_else(ocr::OcrMode::from_env);
1596 self
1597 }
1598
1599 /// In-place variants of [`force_full_page_ocr`](Self::force_full_page_ocr),
1600 /// [`ocr_mode`](Self::ocr_mode) and [`ocr_scale`](Self::ocr_scale) for a
1601 /// long-lived pipeline (docling-serve's warm instance): all three are pure
1602 /// per-worker configuration — no model reloads — so they apply per request
1603 /// like [`set_pages`](Self::set_pages). Set them before every conversion so
1604 /// no request inherits a previous one's choice.
1605 pub fn set_force_full_page_ocr(&mut self, force: bool) {
1606 self.force_full_page_ocr = force;
1607 self.sync_ocr_config();
1608 }
1609
1610 /// See [`set_force_full_page_ocr`](Self::set_force_full_page_ocr).
1611 pub fn set_ocr_mode(&mut self, mode: Option<ocr::OcrMode>) {
1612 self.ocr_mode = mode.unwrap_or_else(ocr::OcrMode::from_env);
1613 self.sync_ocr_config();
1614 }
1615
1616 /// See [`set_force_full_page_ocr`](Self::set_force_full_page_ocr).
1617 pub fn set_ocr_scale(&mut self, scale: Option<f32>) {
1618 self.ocr_scale = scale
1619 .filter(|s| s.is_finite() && *s > 0.0)
1620 .or_else(ocr::scale_from_env);
1621 self.sync_ocr_config();
1622 }
1623
1624 /// Push the current OCR forcing/scale choice onto already-loaded workers
1625 /// (new workers read it at [`Worker::load`]).
1626 fn sync_ocr_config(&mut self) {
1627 let force = self.force_full_page_ocr || self.ocr_mode.forces_full_page();
1628 let scale = self.ocr_scale;
1629 for worker in self.primary.iter_mut().chain(self.pool.iter_mut()) {
1630 worker.force_full_page_ocr = force;
1631 worker.ocr_scale = scale;
1632 }
1633 }
1634
1635 /// OCR render scale in pixels per PDF point — docling's `OcrOptions.scale`
1636 /// (#254, upstream docling#3877; their default 3 = 216 dpi). `None`
1637 /// (default: `DOCLING_RS_OCR_SCALE`, else unset) feeds the recognizer the
1638 /// pipeline's own page render (2.0 px/pt = 144 dpi); a different value
1639 /// resamples that render for the OCR input only — layout and TableFormer
1640 /// keep their pinned-resolution pixels, so the conformance baseline never
1641 /// moves. Lower it when the source raster is already high-resolution and
1642 /// upscaling degrades recognition; raise it toward docling's 216 dpi for
1643 /// parity experiments. Non-positive values are ignored.
1644 pub fn ocr_scale(mut self, scale: Option<f32>) -> Self {
1645 self.ocr_scale = scale
1646 .filter(|s| s.is_finite() && *s > 0.0)
1647 .or_else(ocr::scale_from_env);
1648 self
1649 }
1650
1651 /// The shared TableFormer slot handed to each worker, or `None` when the
1652 /// pipeline options skip TableFormer entirely.
1653 fn tables_slot(&self) -> Option<SharedTables> {
1654 if self.no_table_former || self.no_ocr {
1655 None
1656 } else {
1657 Some(Arc::clone(&self.tables))
1658 }
1659 }
1660
1661 /// The shared enrichment slots for a worker (`None` per model unless its
1662 /// flag is on; `no_ocr` skips layout, so there are no regions to enrich).
1663 fn enrich_slots(&self) -> (Option<SharedClassifier>, Option<SharedCodeFormula>) {
1664 if self.no_ocr || !self.enrich.any() {
1665 return (None, None);
1666 }
1667 (
1668 self.enrich
1669 .picture_classification
1670 .then(|| Arc::clone(&self.classifier)),
1671 (self.enrich.code || self.enrich.formula).then(|| Arc::clone(&self.code_formula)),
1672 )
1673 }
1674
1675 /// Eagerly load the models (the full-intra serial worker: layout + OCR, and
1676 /// the shared TableFormer unless disabled) so the first conversion doesn't pay
1677 /// the load cost. Idempotent; respects `no_ocr` / `no_table_former` (with
1678 /// `no_ocr` there is nothing to load). The docling.rs analogue of docling's
1679 /// `DocumentConverter.initialize_pipeline`.
1680 pub fn warm_up(&mut self) -> Result<(), PdfError> {
1681 self.primary()?;
1682 Ok(())
1683 }
1684
1685 /// The full-intra serial worker, loaded on first use.
1686 fn primary(&mut self) -> Result<&mut Worker, PdfError> {
1687 if self.primary.is_none() {
1688 self.primary = Some(Worker::load(
1689 intra_threads(),
1690 self.tables_slot(),
1691 self.enrich_slots(),
1692 self.enrich,
1693 self.no_ocr,
1694 self.skip_ocr,
1695 // The mode-shaped spelling (#254) and the flag are one engine
1696 // truth: whichever demands forcing wins, mirroring docling's
1697 // `force_full_page_ocr` → `mode=full_page` bridge.
1698 self.force_full_page_ocr || self.ocr_mode.forces_full_page(),
1699 self.no_text_panels,
1700 self.ocr_lang,
1701 self.ocr_scale,
1702 )?);
1703 }
1704 Ok(self.primary.as_mut().unwrap())
1705 }
1706
1707 /// Convert a PDF (bytes) to a [`DoclingDocument`]. A document with fewer than
1708 /// `parallel_min` pages (or a pool size of 1) streams through the full-intra
1709 /// primary; a larger one renders on this thread (pdfium is not thread-safe) and
1710 /// fans the pages out across the worker pool, reassembled in page order so the
1711 /// output is byte-identical to the serial path.
1712 pub fn convert(
1713 &mut self,
1714 bytes: &[u8],
1715 password: Option<&str>,
1716 name: &str,
1717 ) -> Result<DoclingDocument, PdfError> {
1718 let pages = pdfium_backend::page_count(bytes, password)?;
1719 let range = self.resolve_range(pages)?;
1720 // Serial vs parallel is decided by the pages actually converted: a
1721 // 3-page window over a 500-page PDF should not pay the pool load.
1722 let selected = range.map_or(pages, |(a, b)| b - a + 1);
1723 let doc = if self.target_workers >= 2 && selected >= self.parallel_min {
1724 self.convert_parallel(bytes, password, name, range, selected)?
1725 } else {
1726 self.convert_serial(bytes, password, name, range, selected)?
1727 };
1728 timing::report();
1729 Ok(doc)
1730 }
1731
1732 /// Stream pages one at a time through the primary worker — render → process →
1733 /// drop — so the document holds ~one page bitmap (~5 MB) at a time.
1734 fn convert_serial(
1735 &mut self,
1736 bytes: &[u8],
1737 password: Option<&str>,
1738 name: &str,
1739 range: Option<(usize, usize)>,
1740 selected: usize,
1741 ) -> Result<DoclingDocument, PdfError> {
1742 let mut doc = DoclingDocument::new(name);
1743 let mut confs = std::collections::BTreeMap::new();
1744 let render_image = !self.no_ocr;
1745 let progress = self.progress.clone();
1746 let mut done = 0usize;
1747 let worker = self.primary()?;
1748 pdfium_backend::for_each_page(
1749 bytes,
1750 password,
1751 render_image,
1752 range,
1753 |n, _total, mut page| {
1754 let (mut nodes, links, conf) = worker.process(n, &mut page)?;
1755 assemble::stamp_page_no(&mut nodes, n + 1);
1756 doc.nodes.extend(nodes);
1757 doc.links.extend(links);
1758 confs.insert(n + 1, conf);
1759 if let Some(cb) = &progress {
1760 done += 1;
1761 cb(done, selected);
1762 }
1763 Ok::<(), PdfError>(())
1764 },
1765 )?;
1766 assemble::merge_continuations(&mut doc.nodes);
1767 self.apply_heading_hierarchy(&mut doc.nodes, Some(bytes), password);
1768 doc.confidence = Some(docling_core::ConfidenceReport::from_pages(confs));
1769 Ok(doc)
1770 }
1771
1772 /// Render pages serially on this thread (pdfium) and process them in parallel
1773 /// across the worker pool. A bounded channel applies backpressure so only a
1774 /// handful of page bitmaps are resident at once; results carry their page
1775 /// index and are reassembled in order, so the output is byte-identical to the
1776 /// serial path.
1777 fn convert_parallel(
1778 &mut self,
1779 bytes: &[u8],
1780 password: Option<&str>,
1781 name: &str,
1782 range: Option<(usize, usize)>,
1783 selected: usize,
1784 ) -> Result<DoclingDocument, PdfError> {
1785 self.ensure_pool()?;
1786 let progress = self.progress.clone();
1787 let pages_done = std::sync::atomic::AtomicUsize::new(0);
1788 let n_workers = self.pool.len();
1789 let render_image = !self.no_ocr;
1790 let layout_batch = pdf_layout_batch();
1791 // Bound sized so every worker can accumulate a full layout batch while
1792 // rendering stays ahead (and never below the pre-#73 render-ahead of
1793 // two pages per worker); still a hard cap on resident page bitmaps.
1794 let (work_tx, work_rx) = sync_channel::<(usize, PdfPage)>(n_workers * layout_batch.max(2));
1795 let work_rx: Arc<Mutex<Receiver<(usize, PdfPage)>>> = Arc::new(Mutex::new(work_rx));
1796 let results: Arc<Mutex<Vec<(usize, PageOut)>>> = Arc::new(Mutex::new(Vec::new()));
1797 let first_err: Arc<Mutex<Option<PdfError>>> = Arc::new(Mutex::new(None));
1798
1799 // Move the pool into the scope so each worker gets an exclusive `&mut`.
1800 let mut workers = std::mem::take(&mut self.pool);
1801 std::thread::scope(|s| {
1802 for worker in workers.iter_mut() {
1803 let work_rx = Arc::clone(&work_rx);
1804 let results = Arc::clone(&results);
1805 let first_err = Arc::clone(&first_err);
1806 let progress = progress.clone();
1807 let pages_done = &pages_done;
1808 s.spawn(move || loop {
1809 // Hold the receiver lock only for the recv (plus a non-blocking
1810 // drain up to the layout batch size); release before the (long)
1811 // per-page work so other workers can pull concurrently.
1812 let mut batch = Vec::new();
1813 {
1814 let rx = work_rx.lock().unwrap();
1815 match rx.recv() {
1816 Ok(item) => {
1817 batch.push(item);
1818 while batch.len() < layout_batch {
1819 match rx.try_recv() {
1820 Ok(item) => batch.push(item),
1821 Err(_) => break,
1822 }
1823 }
1824 }
1825 Err(_) => break,
1826 }
1827 }
1828 let outs = worker.process_batch(&mut batch);
1829 for ((idx, _), out) in batch.iter().zip(outs) {
1830 match out {
1831 Ok(out) => {
1832 results.lock().unwrap().push((*idx, out));
1833 if let Some(cb) = &progress {
1834 let d = pages_done
1835 .fetch_add(1, std::sync::atomic::Ordering::Relaxed)
1836 + 1;
1837 cb(d, selected);
1838 }
1839 }
1840 Err(e) => {
1841 let mut slot = first_err.lock().unwrap();
1842 if slot.is_none() {
1843 *slot = Some(e);
1844 }
1845 }
1846 }
1847 }
1848 });
1849 }
1850 // Render on this thread and feed the workers; backpressure blocks here
1851 // when the channel is full. Dropping `work_tx` afterwards signals the
1852 // workers (recv → Err) to finish.
1853 let render = pdfium_backend::for_each_page(
1854 bytes,
1855 password,
1856 render_image,
1857 range,
1858 |i, _total, page| {
1859 work_tx
1860 .send((i, page))
1861 .map_err(|_| PdfError::Pdfium("page-worker channel closed".into()))
1862 },
1863 );
1864 drop(work_tx);
1865 if let Err(e) = render {
1866 let mut slot = first_err.lock().unwrap();
1867 if slot.is_none() {
1868 *slot = Some(e);
1869 }
1870 }
1871 });
1872 // Threads have joined; restore the pool for the next conversion.
1873 self.pool = workers;
1874
1875 if let Some(e) = first_err.lock().unwrap().take() {
1876 return Err(e);
1877 }
1878 let mut results = Arc::try_unwrap(results)
1879 .unwrap_or_else(|arc| Mutex::new(arc.lock().unwrap().clone()))
1880 .into_inner()
1881 .unwrap();
1882 results.sort_by_key(|(idx, _)| *idx);
1883 let mut doc = DoclingDocument::new(name);
1884 let mut confs = std::collections::BTreeMap::new();
1885 for (idx, (mut nodes, links, conf)) in results {
1886 assemble::stamp_page_no(&mut nodes, idx + 1);
1887 doc.nodes.extend(nodes);
1888 doc.links.extend(links);
1889 confs.insert(idx + 1, conf);
1890 }
1891 assemble::merge_continuations(&mut doc.nodes);
1892 self.apply_heading_hierarchy(&mut doc.nodes, Some(bytes), password);
1893 doc.confidence = Some(docling_core::ConfidenceReport::from_pages(confs));
1894 Ok(doc)
1895 }
1896
1897 /// Convert a PDF in **streaming** mode: `emit` is called with each finalized,
1898 /// in-document-order batch of nodes (and that span's recovered links) as pages
1899 /// complete, so a caller can serialize Markdown page by page instead of waiting
1900 /// for the whole document. The batches are exactly the buffered [`convert`]'s
1901 /// nodes, split at safe block boundaries by [`assemble::StreamAssembler`] — the
1902 /// parallel path reorders pages back into document order before emitting, so
1903 /// the output is identical regardless of worker scheduling.
1904 ///
1905 /// `emit` runs on the calling thread (never a worker), so it needn't be `Send`
1906 /// and its backpressure throttles the whole pipeline. Returning `Err` from
1907 /// `emit` aborts the conversion with that error.
1908 pub fn convert_streaming<F>(
1909 &mut self,
1910 bytes: &[u8],
1911 password: Option<&str>,
1912 name: &str,
1913 emit: F,
1914 ) -> Result<(), PdfError>
1915 where
1916 F: FnMut(Vec<Node>, Vec<(String, String)>) -> Result<(), PdfError>,
1917 {
1918 let _ = name; // page nodes carry no name; the caller owns the document name.
1919 let pages = pdfium_backend::page_count(bytes, password)?;
1920 let range = self.resolve_range(pages)?;
1921 let selected = range.map_or(pages, |(a, b)| b - a + 1);
1922 let r = if self.target_workers >= 2 && selected >= self.parallel_min {
1923 self.convert_streaming_parallel(bytes, password, range, emit)
1924 } else {
1925 self.convert_streaming_serial(bytes, password, range, emit)
1926 };
1927 timing::report();
1928 r
1929 }
1930
1931 /// Serial streaming: render → process → emit, one page at a time, holding back
1932 /// only the tail that might still merge into the next page.
1933 fn convert_streaming_serial<F>(
1934 &mut self,
1935 bytes: &[u8],
1936 password: Option<&str>,
1937 range: Option<(usize, usize)>,
1938 mut emit: F,
1939 ) -> Result<(), PdfError>
1940 where
1941 F: FnMut(Vec<Node>, Vec<(String, String)>) -> Result<(), PdfError>,
1942 {
1943 let mut asm = assemble::StreamAssembler::new();
1944 let render_image = !self.no_ocr;
1945 let worker = self.primary()?;
1946 pdfium_backend::for_each_page(
1947 bytes,
1948 password,
1949 render_image,
1950 range,
1951 |n, _total, mut page| {
1952 // Confidence is dropped on the streaming path: the report is
1953 // only complete once every page has run, which defeats
1954 // page-by-page emission — buffered `convert` carries it.
1955 let (nodes, links, _conf) = worker.process(n, &mut page)?;
1956 emit(asm.push(nodes), links)
1957 },
1958 )?;
1959 emit(asm.finish(), Vec::new())
1960 }
1961
1962 /// Parallel streaming: pages render serially on a dedicated thread (pdfium is
1963 /// not thread-safe) and process across the worker pool; results carry their
1964 /// page index and are reordered on the calling thread into a
1965 /// [`assemble::StreamAssembler`], which emits each page in document order as
1966 /// soon as its predecessors have arrived. Bounded channels keep only a handful
1967 /// of pages resident and let `emit`'s backpressure reach the renderer.
1968 fn convert_streaming_parallel<F>(
1969 &mut self,
1970 bytes: &[u8],
1971 password: Option<&str>,
1972 range: Option<(usize, usize)>,
1973 mut emit: F,
1974 ) -> Result<(), PdfError>
1975 where
1976 F: FnMut(Vec<Node>, Vec<(String, String)>) -> Result<(), PdfError>,
1977 {
1978 self.ensure_pool()?;
1979 let n_workers = self.pool.len();
1980 let render_image = !self.no_ocr;
1981 let layout_batch = pdf_layout_batch();
1982 // Bound sized so every worker can accumulate a full layout batch while
1983 // rendering stays ahead (and never below the pre-#73 render-ahead of
1984 // two pages per worker); still a hard cap on resident page bitmaps.
1985 let (work_tx, work_rx) = sync_channel::<(usize, PdfPage)>(n_workers * layout_batch.max(2));
1986 let work_rx: Arc<Mutex<Receiver<(usize, PdfPage)>>> = Arc::new(Mutex::new(work_rx));
1987 // Workers and the renderer report here; the calling thread drains it in
1988 // page order. Bounded so workers block (bounding resident bitmaps) when the
1989 // consumer falls behind.
1990 let (res_tx, res_rx) = sync_channel::<Result<(usize, PageOut), PdfError>>(n_workers * 2);
1991
1992 let mut workers = std::mem::take(&mut self.pool);
1993 let mut asm = assemble::StreamAssembler::new();
1994 let mut first_err: Option<PdfError> = None;
1995
1996 std::thread::scope(|s| {
1997 // Workers: pull a batch of pages (whatever is already rendered, up
1998 // to the layout batch size), process it, report (index-tagged)
1999 // results.
2000 for worker in workers.iter_mut() {
2001 let work_rx = Arc::clone(&work_rx);
2002 let res_tx = res_tx.clone();
2003 s.spawn(move || 'outer: loop {
2004 let mut batch = Vec::new();
2005 {
2006 let rx = work_rx.lock().unwrap();
2007 match rx.recv() {
2008 Ok(item) => {
2009 batch.push(item);
2010 while batch.len() < layout_batch {
2011 match rx.try_recv() {
2012 Ok(item) => batch.push(item),
2013 Err(_) => break,
2014 }
2015 }
2016 }
2017 Err(_) => break,
2018 }
2019 }
2020 let outs = worker.process_batch(&mut batch);
2021 for ((idx, _), out) in batch.iter().zip(outs) {
2022 if res_tx.send(out.map(|o| (*idx, o))).is_err() {
2023 break 'outer; // consumer gone
2024 }
2025 }
2026 });
2027 }
2028 // Renderer: feed pages to the pool on its own thread (pdfium stays on a
2029 // single thread); report a render error through the same channel.
2030 {
2031 let res_tx = res_tx.clone();
2032 s.spawn(move || {
2033 let render = pdfium_backend::for_each_page(
2034 bytes,
2035 password,
2036 render_image,
2037 range,
2038 |i, _total, page| {
2039 work_tx
2040 .send((i, page))
2041 .map_err(|_| PdfError::Pdfium("page-worker channel closed".into()))
2042 },
2043 );
2044 drop(work_tx); // signal workers to finish
2045 if let Err(e) = render {
2046 let _ = res_tx.send(Err(e));
2047 }
2048 });
2049 }
2050 // Drop our own sender so the channel closes once the threads finish.
2051 drop(res_tx);
2052
2053 // Collector (this thread): reorder into document order and emit.
2054 // With a page window, indices start at the window's first page.
2055 let mut buffer: BTreeMap<usize, PageOut> = BTreeMap::new();
2056 let mut next = range.map_or(0, |(first, _)| first);
2057 for msg in res_rx.iter() {
2058 match msg {
2059 Err(e) => {
2060 if first_err.is_none() {
2061 first_err = Some(e);
2062 }
2063 }
2064 Ok((idx, out)) => {
2065 buffer.insert(idx, out);
2066 if first_err.is_some() {
2067 continue; // keep draining so the threads can exit
2068 }
2069 while let Some((nodes, links, _conf)) = buffer.remove(&next) {
2070 if let Err(e) = emit(asm.push(nodes), links) {
2071 first_err = Some(e);
2072 break;
2073 }
2074 next += 1;
2075 }
2076 }
2077 }
2078 }
2079 });
2080 // Threads have joined; restore the pool for the next conversion.
2081 self.pool = workers;
2082
2083 if let Some(e) = first_err {
2084 return Err(e);
2085 }
2086 emit(asm.finish(), Vec::new())
2087 }
2088
2089 /// Lazily grow the pool to `target_workers`, loading the new workers
2090 /// concurrently (model load is mostly I/O + mmap, so N loads overlap to roughly
2091 /// one load's wall-time). Cached for reuse across documents.
2092 fn ensure_pool(&mut self) -> Result<(), PdfError> {
2093 let need = self.target_workers.saturating_sub(self.pool.len());
2094 if need == 0 {
2095 return Ok(());
2096 }
2097 let intra = pdf_intra();
2098 let no_ocr = self.no_ocr;
2099 let skip_ocr = self.skip_ocr;
2100 let force = self.force_full_page_ocr || self.ocr_mode.forces_full_page();
2101 let ntp = self.no_text_panels;
2102 let ocr_lang = self.ocr_lang;
2103 let ocr_scale = self.ocr_scale;
2104 let enrich = self.enrich;
2105 let tables = self.tables_slot();
2106 let enrich_slots = self.enrich_slots();
2107 let loaded: Vec<Result<Worker, PdfError>> = std::thread::scope(|s| {
2108 let handles: Vec<_> = (0..need)
2109 .map(|_| {
2110 let tables = tables.clone();
2111 let enrich_slots = enrich_slots.clone();
2112 s.spawn(move || {
2113 Worker::load(
2114 intra,
2115 tables,
2116 enrich_slots,
2117 enrich,
2118 no_ocr,
2119 skip_ocr,
2120 force,
2121 ntp,
2122 ocr_lang,
2123 ocr_scale,
2124 )
2125 })
2126 })
2127 .collect();
2128 handles.into_iter().map(|h| h.join().unwrap()).collect()
2129 });
2130 for w in loaded {
2131 self.pool.push(w?);
2132 }
2133 Ok(())
2134 }
2135
2136 /// Convert a standalone image (PNG/JPEG/TIFF/WebP/…) as a single page —
2137 /// docling routes images through the same layout+OCR pipeline as a PDF page.
2138 pub fn convert_image(&mut self, bytes: &[u8], name: &str) -> Result<DoclingDocument, PdfError> {
2139 let image = decode_image_limited(bytes)?;
2140 let (w, h) = image.dimensions();
2141 // The image is its own page rendered at 1 px per "point" (scale 1.0); a
2142 // standalone image has no text layer, so OCR supplies the cells.
2143 let page = PdfPage {
2144 width: w as f32,
2145 height: h as f32,
2146 scale: 1.0,
2147 cells: Vec::new(),
2148 code_cells: Vec::new(),
2149 word_cells: Vec::new(),
2150 // A standalone image *is* its own scale-1.0 page image, so the
2151 // layout model sees it through the docling-exact PIL kernel.
2152 image_layout: Some(image.clone()),
2153 image,
2154 links: Vec::new(),
2155 rotation: 0,
2156 };
2157 self.process_pages(vec![page], name)
2158 }
2159
2160 /// Run layout (+ OCR for cell-less pages) and assemble each already-rendered
2161 /// page (image / METS inputs, which are small and already materialised).
2162 /// Public so [`mets::convert_mets_gbs_with_pipeline`] can drive a
2163 /// caller-configured pipeline (#244).
2164 pub fn process_pages(
2165 &mut self,
2166 mut pages: Vec<PdfPage>,
2167 name: &str,
2168 ) -> Result<DoclingDocument, PdfError> {
2169 let mut doc = DoclingDocument::new(name);
2170 let mut confs = std::collections::BTreeMap::new();
2171 let worker = self.primary()?;
2172 for (n, page) in pages.iter_mut().enumerate() {
2173 let (mut nodes, links, conf) = worker.process(n, page)?;
2174 assemble::stamp_page_no(&mut nodes, n + 1);
2175 doc.nodes.extend(nodes);
2176 doc.links.extend(links);
2177 confs.insert(n + 1, conf);
2178 }
2179 assemble::merge_continuations(&mut doc.nodes);
2180 // No PDF behind these pages (images, METS): the heading-hierarchy
2181 // stage degrades to the numbering signal — exactly docling without
2182 // an outline or parsed pages.
2183 self.apply_heading_hierarchy(&mut doc.nodes, None, None);
2184 doc.confidence = Some(docling_core::ConfidenceReport::from_pages(confs));
2185 Ok(doc)
2186 }
2187}
2188
2189/// Number of pages in a PDF, without converting anything — what the CLI batch
2190/// mode prints in its per-document start line.
2191#[cfg(feature = "ml")]
2192pub fn page_count(bytes: &[u8], password: Option<&str>) -> Result<usize, PdfError> {
2193 Ok(pdfium_backend::page_count(bytes, password)?)
2194}
2195
2196#[cfg(feature = "ml")]
2197/// Convenience one-shot conversion (loads the pipeline per call). Errors are
2198/// detailed and surfaced (never silently skipped).
2199pub fn convert(
2200 bytes: &[u8],
2201 password: Option<&str>,
2202 name: &str,
2203) -> Result<DoclingDocument, PdfError> {
2204 convert_with_options(
2205 bytes,
2206 password,
2207 name,
2208 false,
2209 false,
2210 false,
2211 false,
2212 EnrichmentOptions::default(),
2213 None,
2214 None,
2215 )
2216}
2217
2218#[cfg(feature = "ml")]
2219/// Like [`convert`], but optionally skips loading/running TableFormer (see
2220/// [`Pipeline::no_table_former`]) and/or layout+OCR+TableFormer entirely (see
2221/// [`Pipeline::no_ocr`]), and/or enables the enrichment passes (see
2222/// [`Pipeline::enrichments`]).
2223// One positional per pipeline switch mirrors the Pipeline builder; growing
2224// past clippy's arity cap is the price of keeping this one-shot signature
2225// stable-ish instead of churning callers into an options struct mid-series.
2226#[allow(clippy::too_many_arguments)]
2227pub fn convert_with_options(
2228 bytes: &[u8],
2229 password: Option<&str>,
2230 name: &str,
2231 no_table_former: bool,
2232 no_ocr: bool,
2233 force_full_page_ocr: bool,
2234 no_text_panels: bool,
2235 enrich: EnrichmentOptions,
2236 pages: Option<(usize, usize)>,
2237 ocr_lang: Option<OcrLang>,
2238) -> Result<DoclingDocument, PdfError> {
2239 Pipeline::new()?
2240 .no_table_former(no_table_former)
2241 .no_ocr(no_ocr)
2242 .force_full_page_ocr(force_full_page_ocr)
2243 .no_text_panels(no_text_panels)
2244 .enrichments(enrich)
2245 .pages(pages)
2246 .ocr_lang(ocr_lang)
2247 .convert(bytes, password, name)
2248}
2249
2250#[cfg(feature = "ml")]
2251/// Convenience one-shot image conversion (loads the pipeline per call).
2252pub fn convert_image(bytes: &[u8], name: &str) -> Result<DoclingDocument, PdfError> {
2253 convert_image_with_options(
2254 bytes,
2255 name,
2256 false,
2257 false,
2258 false,
2259 EnrichmentOptions::default(),
2260 None,
2261 )
2262}
2263
2264#[cfg(feature = "ml")]
2265/// Like [`convert_image`], but optionally skips loading/running TableFormer (see
2266/// [`Pipeline::no_table_former`]) and/or layout+OCR+TableFormer entirely (see
2267/// [`Pipeline::no_ocr`]), and/or enables the enrichment passes.
2268pub fn convert_image_with_options(
2269 bytes: &[u8],
2270 name: &str,
2271 no_table_former: bool,
2272 no_ocr: bool,
2273 no_text_panels: bool,
2274 enrich: EnrichmentOptions,
2275 ocr_lang: Option<OcrLang>,
2276) -> Result<DoclingDocument, PdfError> {
2277 Pipeline::new()?
2278 .no_table_former(no_table_former)
2279 .no_ocr(no_ocr)
2280 .no_text_panels(no_text_panels)
2281 .enrichments(enrich)
2282 .ocr_lang(ocr_lang)
2283 .convert_image(bytes, name)
2284}
2285
2286#[cfg(feature = "ml")]
2287/// Convert pre-segmented pages (image + already-known text cells, e.g. METS/hOCR
2288/// scans) through the shared layout + assembly pipeline.
2289pub fn convert_pages(pages: Vec<PdfPage>, name: &str) -> Result<DoclingDocument, PdfError> {
2290 convert_pages_with_options(
2291 pages,
2292 name,
2293 false,
2294 false,
2295 false,
2296 EnrichmentOptions::default(),
2297 )
2298}
2299
2300#[cfg(feature = "ml")]
2301/// Like [`convert_pages`], but optionally skips loading/running TableFormer (see
2302/// [`Pipeline::no_table_former`]) and/or layout+OCR+TableFormer entirely (see
2303/// [`Pipeline::no_ocr`]), and/or enables the enrichment passes.
2304pub fn convert_pages_with_options(
2305 pages: Vec<PdfPage>,
2306 name: &str,
2307 no_table_former: bool,
2308 no_ocr: bool,
2309 no_text_panels: bool,
2310 enrich: EnrichmentOptions,
2311) -> Result<DoclingDocument, PdfError> {
2312 Pipeline::new()?
2313 .no_table_former(no_table_former)
2314 .no_text_panels(no_text_panels)
2315 .no_ocr(no_ocr)
2316 .enrichments(enrich)
2317 .process_pages(pages, name)
2318}
2319
2320#[cfg(feature = "ml")]
2321#[cfg(all(test, feature = "ml"))]
2322mod image_limit_tests {
2323 use super::decode_image_with_max_side;
2324
2325 /// A small valid PNG encoded via the `image` crate (robust vs. a hand-rolled
2326 /// byte literal).
2327 fn png_bytes(w: u32, h: u32) -> Vec<u8> {
2328 use std::io::Cursor;
2329 let img = image::RgbImage::new(w, h);
2330 let mut out = Vec::new();
2331 img.write_to(&mut Cursor::new(&mut out), image::ImageFormat::Png)
2332 .unwrap();
2333 out
2334 }
2335
2336 #[test]
2337 fn normal_image_decodes_under_the_cap() {
2338 let img = decode_image_with_max_side(&png_bytes(8, 8), 30_000).expect("8x8 decodes");
2339 assert_eq!(img.dimensions(), (8, 8));
2340 }
2341
2342 #[test]
2343 fn dimensions_over_the_cap_are_rejected_not_aborted() {
2344 // A per-side cap below the image's declared size must yield a
2345 // recoverable Err, never an allocation-abort — the mechanism that stops
2346 // a crafted image declaring 60000×60000 from OOM-killing the process.
2347 let r = decode_image_with_max_side(&png_bytes(8, 8), 4);
2348 assert!(
2349 r.is_err(),
2350 "decode must fail under the pixel cap, not abort"
2351 );
2352 }
2353}
2354
2355#[cfg(test)]
2356mod median_tests {
2357 #[test]
2358 fn median_of_empty_is_zero_not_a_panic() {
2359 // A crafted table can leave a row/column with zero matched cells; the
2360 // even-count branch would index values[0 - 1] and panic (→ remote crash
2361 // via docling-serve) without the empty guard.
2362 assert_eq!(super::tf_match::median_for_test(&mut []), 0.0);
2363 assert_eq!(super::tf_match::median_for_test(&mut [4.0, 2.0]), 3.0);
2364 assert_eq!(super::tf_match::median_for_test(&mut [5.0, 1.0, 3.0]), 3.0);
2365 }
2366}
2367
2368#[cfg(test)]
2369mod send_check {
2370 /// The Node bindings (`docling-node`) run a shared [`super::Pipeline`] on
2371 /// libuv worker threads (`Arc<Mutex<Pipeline>>`), which is only sound while
2372 /// `Pipeline: Send` holds — this fails to compile if a non-`Send` field
2373 /// (e.g. an `Rc` or a raw pdfium handle) ever lands in the pipeline.
2374 fn assert_send<T: Send>() {}
2375
2376 #[test]
2377 fn pipeline_is_send() {
2378 assert_send::<super::Pipeline>();
2379 }
2380}
2381
2382#[cfg(all(test, feature = "ml"))]
2383mod ocr_input_tests {
2384 /// #254: without an `ocr_scale` (or with one equal to the render scale)
2385 /// the OCR reads the page render untouched and the cache stays cold; a
2386 /// different scale builds one resampled view, reuses it across calls, and
2387 /// reports the requested px/pt so cell geometry divides back to points.
2388 #[test]
2389 fn ocr_input_resamples_only_on_a_real_scale_change() {
2390 let img = image::RgbImage::new(200, 100);
2391 let mut cache = None;
2392 let (v, s) = super::ocr_input(&mut cache, &img, 2.0, None);
2393 assert!(std::ptr::eq(v, &img) && s == 2.0 && cache.is_none());
2394 let (v, s) = super::ocr_input(&mut cache, &img, 2.0, Some(2.0));
2395 assert!(std::ptr::eq(v, &img) && s == 2.0 && cache.is_none());
2396
2397 let (v, s) = super::ocr_input(&mut cache, &img, 2.0, Some(3.0));
2398 assert_eq!((v.width(), v.height(), s), (300, 150, 3.0));
2399 let first = cache.as_ref().map(|c| c as *const image::RgbImage);
2400 let (v, _) = super::ocr_input(&mut cache, &img, 2.0, Some(3.0));
2401 assert_eq!(
2402 Some(v as *const image::RgbImage),
2403 first,
2404 "cached, not rebuilt"
2405 );
2406
2407 let mut down = None;
2408 let (v, s) = super::ocr_input(&mut down, &img, 2.0, Some(1.0));
2409 assert_eq!((v.width(), v.height(), s), (100, 50, 1.0));
2410 }
2411}