fleischwolf_pdf/pdfium_backend.rs
1//! pdfium-based text extraction and page rendering.
2//!
3//! Text is reconstructed the way docling's `docling-parse` does it, so the
4//! output spacing matches the groundtruth: the page's **character** stream is
5//! grouped into **words** (split at a horizontal gap wider than a fraction of
6//! the font height — font-relative, so letter-tracking in display titles does
7//! not split a word) and words into **lines** (by baseline). pdfium-render's
8//! safe API only exposes whole style runs / `GetBoundedText`, so the character
9//! loop is driven through the raw `PdfiumLibraryBindings` FFI on a second handle
10//! to the same bytes (no fork; stays publishable).
11
12use image::RgbImage;
13use pdfium_render::prelude::*;
14
15/// A run of text with its bounding box, in PDF points with a **top-left** origin
16/// (pdfium's native origin is bottom-left; we flip it to match docling's
17/// `BoundingBox(..., origin=TOPLEFT)`).
18#[derive(Debug, Clone)]
19pub struct TextCell {
20 pub text: String,
21 pub l: f32,
22 pub t: f32,
23 pub r: f32,
24 pub b: f32,
25}
26
27/// Pixels-per-point used to render page images. Layout is scale-invariant (it
28/// scales normalized boxes by the page point size), but OCR benefits from the
29/// extra resolution.
30pub const RENDER_SCALE: f32 = 2.0;
31
32/// One page's geometry, extracted text cells, and a rendered RGB image. The
33/// image is rendered at [`RENDER_SCALE`] pixels per PDF point; `image px =
34/// page point × scale`.
35#[derive(Clone)]
36pub struct PdfPage {
37 pub width: f32,
38 pub height: f32,
39 pub scale: f32,
40 pub cells: Vec<TextCell>,
41 /// Same text grouped for code regions: split only at pdfium space glyphs, so
42 /// monospace runs keep their source spacing instead of the prose heuristic's.
43 pub code_cells: Vec<TextCell>,
44 /// Per-word cells (one per word, not joined into lines) for TableFormer cell
45 /// matching.
46 pub word_cells: Vec<TextCell>,
47 pub image: RgbImage,
48 /// Hyperlink annotations on the page (rect in top-left page coords + target
49 /// URI), restricted to web/mail/tel schemes. Used only by strict Markdown.
50 pub links: Vec<LinkAnnot>,
51}
52
53/// A PDF link annotation: its rectangle (top-left page coordinates, matching
54/// [`TextCell`]) and target URI.
55#[derive(Debug, Clone)]
56pub struct LinkAnnot {
57 pub l: f32,
58 pub t: f32,
59 pub r: f32,
60 pub b: f32,
61 pub uri: String,
62}
63
64/// A parsed PDF: per-page text cells and page images.
65pub struct PdfDocument {
66 pub pages: Vec<PdfPage>,
67}
68
69/// Bind to the pdfium dynamic library. Honors `PDFIUM_DYNAMIC_LIB_PATH` (a
70/// directory or file), else the directory of the current exe, else the system
71/// library — mirroring how a deployment ships `libpdfium` alongside the binary.
72/// Whether to use the docling-parse line sanitizer ([`crate::dp_lines`]) for prose
73/// reconstruction — the default. Set `DOCLING_LEGACY_LINES` to fall back to the
74/// older gap-heuristic `lines_from_glyphs`.
75pub(crate) fn use_dp_lines() -> bool {
76 std::env::var("DOCLING_LEGACY_LINES").is_err()
77}
78
79/// Whether to source **word** cells from the pure-Rust parser (roadmap item 6),
80/// the default. The parser's `word_cells` reproduce docling-parse's word grouping
81/// byte-for-byte — the per-word tokens TableFormer matches table-grid cells
82/// against — which moves table extraction closer to docling on the heavy
83/// multi-column fixtures. Set `DOCLING_PDFIUM_WORDS` to keep pdfium's word cells,
84/// or `DOCLING_PDFIUM_TEXT` to fall back to pdfium for all text.
85pub(crate) fn use_parser_words() -> bool {
86 std::env::var("DOCLING_PDFIUM_WORDS").is_err() && std::env::var("DOCLING_PDFIUM_TEXT").is_err()
87}
88
89/// Whether to source **code** cells from the parser too (the default) — the last
90/// text layer to leave pdfium, fully retiring its text path. The parser's
91/// gap-based code grouping ([`code_cells_from_glyphs`]) reconstructs monospace
92/// spacing from positioning gaps (`function add(a, b) { … }`), so it no longer
93/// drops the inter-token spaces the old space-glyph-only grouping lost
94/// (`functionadd`). Reverts to pdfium with `DOCLING_PDFIUM_WORDS` (alongside word
95/// cells) or `DOCLING_PDFIUM_TEXT` (all text).
96pub(crate) fn use_parser_code() -> bool {
97 std::env::var("DOCLING_PDFIUM_WORDS").is_err() && std::env::var("DOCLING_PDFIUM_TEXT").is_err()
98}
99
100fn bind() -> Result<Pdfium, PdfiumError> {
101 if let Ok(path) = std::env::var("PDFIUM_DYNAMIC_LIB_PATH") {
102 let name = Pdfium::pdfium_platform_library_name_at_path(&path);
103 if let Ok(b) = Pdfium::bind_to_library(&name) {
104 return Ok(Pdfium::new(b));
105 }
106 if let Ok(b) = Pdfium::bind_to_library(&path) {
107 return Ok(Pdfium::new(b));
108 }
109 }
110 Pdfium::bind_to_system_library().map(Pdfium::new)
111}
112
113impl PdfDocument {
114 /// Parse a PDF from bytes, optionally decrypting with `password`.
115 ///
116 /// Note: this materialises **every** page's rendered bitmap in memory at
117 /// once. For large documents prefer [`for_each_page`], which streams.
118 pub fn open(bytes: &[u8], password: Option<&str>) -> Result<Self, PdfiumError> {
119 let pdfium = bind()?;
120 let ffi = FfiText::load(pdfium.bindings(), bytes, password);
121 let doc = pdfium.load_pdf_from_byte_slice(bytes, password)?;
122 let mut rust = rust_parser_cells(bytes);
123 let mut pages = Vec::new();
124 for (i, page) in doc.pages().iter().enumerate() {
125 let rc = rust.as_mut().and_then(|v| v.get_mut(i).map(std::mem::take));
126 pages.push(extract_page(&page, &ffi, i as i32, rc)?);
127 }
128 Ok(PdfDocument { pages })
129 }
130}
131
132/// Per-page prose line cells from the pure-Rust text parser. This is the
133/// **default** text layer (it matches docling-parse's char geometry and is a
134/// strict improvement on byte-conformance — e.g. it recovers the Arabic
135/// sentence-period attachment in `right_to_left_01`). Set `DOCLING_PDFIUM_TEXT`
136/// to fall back to pdfium's text layer. The parser returns an empty page when a
137/// PDF (or a page) has no parseable text layer; the caller keeps pdfium's cells
138/// in that case, so scanned/edge-case pages are unaffected.
139fn rust_parser_cells(bytes: &[u8]) -> Option<Vec<crate::textparse::PageParserCells>> {
140 if std::env::var("DOCLING_PDFIUM_TEXT").is_ok() {
141 return None;
142 }
143 Some(crate::timing::timed("textparse", || {
144 crate::textparse::pdf_all_cells(bytes)
145 }))
146}
147
148/// Number of pages in a PDF, without rendering any of them — used to decide
149/// whether a document is worth spinning up the parallel worker pool.
150pub fn page_count(bytes: &[u8], password: Option<&str>) -> Result<usize, PdfiumError> {
151 let pdfium = bind()?;
152 let doc = pdfium.load_pdf_from_byte_slice(bytes, password)?;
153 Ok(doc.pages().len() as usize)
154}
155
156/// Render + extract pages one at a time, handing each (owned) [`PdfPage`] to `f`.
157/// Only one page bitmap is resident at a time — a rendered page is ~5 MB, so a
158/// large PDF would otherwise hold gigabytes of bitmaps at once. `f` receives the
159/// zero-based page index and the total page count.
160///
161/// `E` is the caller's error type; pdfium errors convert into it via `From`.
162pub fn for_each_page<E, F>(bytes: &[u8], password: Option<&str>, mut f: F) -> Result<(), E>
163where
164 E: From<PdfiumError>,
165 F: FnMut(usize, usize, PdfPage) -> Result<(), E>,
166{
167 let pdfium = bind()?;
168 let ffi = FfiText::load(pdfium.bindings(), bytes, password);
169 let doc = pdfium.load_pdf_from_byte_slice(bytes, password)?;
170 let mut rust = rust_parser_cells(bytes);
171 let pages = doc.pages();
172 let total = pages.len() as usize;
173 for (i, page) in pages.iter().enumerate() {
174 let rc = rust.as_mut().and_then(|v| v.get_mut(i).map(std::mem::take));
175 let extracted = extract_page(&page, &ffi, i as i32, rc)?;
176 f(i, total, extracted)?;
177 }
178 Ok(())
179}
180
181fn extract_page(
182 page: &pdfium_render::prelude::PdfPage<'_>,
183 ffi: &FfiText<'_>,
184 index: i32,
185 rust_cells: Option<crate::textparse::PageParserCells>,
186) -> Result<PdfPage, PdfiumError> {
187 let width = page.width().value;
188 let height = page.height().value;
189
190 let (mut cells, mut code_cells, mut word_cells) =
191 crate::timing::timed("ffi.page_cells", || ffi.page_cells(index, height));
192 if cells.is_empty() {
193 cells = segment_cells(&page.text()?, height);
194 }
195 // Default: use the pure-Rust text parser instead of pdfium's text layer
196 // (override with `DOCLING_PDFIUM_TEXT`). Prose line cells always come from the
197 // parser; word and code cells do too unless `DOCLING_PDFIUM_WORDS` keeps them
198 // on pdfium (the parser's word grouping reproduces docling-parse's, which
199 // TableFormer matches against — roadmap item 6). A page the parser couldn't
200 // read (no text layer) keeps pdfium's cells.
201 if let Some(rc) = rust_cells {
202 if !rc.prose.is_empty() {
203 cells = rc.prose;
204 }
205 if use_parser_words() && !rc.words.is_empty() {
206 word_cells = rc.words;
207 }
208 if use_parser_code() && !rc.code.is_empty() {
209 code_cells = rc.code;
210 }
211 }
212
213 // docling renders at 1.5× the target scale and downsamples "to make it
214 // sharper" (pypdfium2 → PIL BICUBIC). Replicate exactly: the TableFormer
215 // model is pixel-sensitive, so the page bitmap must match byte-for-byte.
216 // `CatmullRom` is the same a=-0.5 cubic kernel as PIL's BICUBIC.
217 const SUPERSAMPLE: f32 = 1.5;
218 let tw = (width * RENDER_SCALE * SUPERSAMPLE).round().max(1.0) as i32;
219 let th = (height * RENDER_SCALE * SUPERSAMPLE).round().max(1.0) as i32;
220 let cfg = PdfRenderConfig::new()
221 .set_target_width(tw)
222 .set_target_height(th);
223 let big = crate::timing::timed("pdfium.render", || {
224 page.render_with_config(&cfg)
225 .map(|b| b.as_image().into_rgb8())
226 })?;
227 let dw = (width * RENDER_SCALE).round().max(1.0) as u32;
228 let dh = (height * RENDER_SCALE).round().max(1.0) as u32;
229 let image = crate::timing::timed("image.resize", || {
230 image::imageops::resize(&big, dw, dh, image::imageops::FilterType::CatmullRom)
231 });
232
233 Ok(PdfPage {
234 width,
235 height,
236 scale: RENDER_SCALE,
237 cells,
238 code_cells,
239 word_cells,
240 image,
241 links: extract_links(page, height),
242 })
243}
244
245/// Collect web/mail/tel hyperlink annotations on a page, mapping each link's
246/// rectangle into top-left page coordinates (like [`TextCell`]). `file://` and
247/// in-document destinations are skipped — only externally meaningful targets are
248/// rendered. pdfium occasionally lists a link twice; rects are kept as-is and the
249/// caller dedupes by resolved anchor text.
250fn extract_links(page: &pdfium_render::prelude::PdfPage<'_>, page_h: f32) -> Vec<LinkAnnot> {
251 let mut out = Vec::new();
252 for link in page.links().iter() {
253 let Some(uri) = link
254 .action()
255 .and_then(|a| a.as_uri_action().and_then(|u| u.uri().ok()))
256 else {
257 continue;
258 };
259 let scheme_ok = ["http://", "https://", "mailto:", "tel:"]
260 .iter()
261 .any(|s| uri.starts_with(s));
262 if !scheme_ok {
263 continue;
264 }
265 if let Ok(rect) = link.rect() {
266 out.push(LinkAnnot {
267 l: rect.left().value,
268 t: page_h - rect.top().value,
269 r: rect.right().value,
270 b: page_h - rect.bottom().value,
271 uri,
272 });
273 }
274 }
275 out
276}
277
278/// Fallback line cells from pdfium-render's style segments (one cell per
279/// segment). Used only when the raw-FFI text page can't be loaded.
280fn segment_cells(text: &PdfPageText, page_h: f32) -> Vec<TextCell> {
281 text.segments()
282 .iter()
283 .filter_map(|seg| {
284 let s = seg.text();
285 if s.trim().is_empty() {
286 return None;
287 }
288 let r = seg.bounds();
289 Some(TextCell {
290 text: s,
291 l: r.left().value,
292 t: page_h - r.top().value,
293 r: r.right().value,
294 b: page_h - r.bottom().value,
295 })
296 })
297 .collect()
298}
299
300/// A second, raw-FFI handle on the same PDF used to drive the character loop
301/// (`FPDFText_GetUnicode`/`GetCharBox`) that pdfium-render's safe API doesn't
302/// expose. Closes the document on drop.
303struct FfiText<'a> {
304 bindings: &'a dyn PdfiumLibraryBindings,
305 doc: FPDF_DOCUMENT,
306}
307
308/// One glyph: codepoint + native (y-up) box edges. `l/b/r/t` is pdfium's *tight*
309/// ink box (used by the legacy `lines_from_glyphs`); `ll/lb/lr/lt` is the *loose*
310/// box (font ascent/descent + advance — uniform per font/size), which the
311/// docling-parse-style sanitizer needs so adjacent glyphs share a top edge.
312pub(crate) struct Glyph {
313 pub(crate) ch: char,
314 pub(crate) l: f32,
315 pub(crate) b: f32,
316 pub(crate) r: f32,
317 pub(crate) t: f32,
318 pub(crate) ll: f32,
319 pub(crate) lb: f32,
320 pub(crate) lr: f32,
321 pub(crate) lt: f32,
322 /// Hash of the PDF font name + flags (0 when not fetched). The sanitizer uses
323 /// it for docling-parse's `enforce_same_font` (keeps a bold label and regular
324 /// value as separate line cells, e.g. `LABEL : value`).
325 pub(crate) font: u64,
326}
327
328impl<'a> FfiText<'a> {
329 fn load(bindings: &'a dyn PdfiumLibraryBindings, bytes: &[u8], password: Option<&str>) -> Self {
330 let doc = bindings.FPDF_LoadMemDocument(bytes, password);
331 FfiText { bindings, doc }
332 }
333
334 /// Reconstruct line cells for page `index` (zero-based) via the
335 /// chars→words→lines grouping. Returns `(prose_cells, code_cells)` — the same
336 /// glyphs grouped two ways (gap-heuristic for prose, space-glyph-only for
337 /// code). Both empty on any failure (caller falls back).
338 fn page_cells(&self, index: i32, page_h: f32) -> (Vec<TextCell>, Vec<TextCell>, Vec<TextCell>) {
339 let empty = || (Vec::new(), Vec::new(), Vec::new());
340 if self.doc.is_null() {
341 return empty();
342 }
343 let b = self.bindings;
344 let page = b.FPDF_LoadPage(self.doc, index);
345 if page.is_null() {
346 return empty();
347 }
348 let tp = b.FPDFText_LoadPage(page);
349 let out = if tp.is_null() {
350 empty()
351 } else {
352 let dp = use_dp_lines();
353 let g = glyphs(b, tp, dp);
354 b.FPDFText_ClosePage(tp);
355 // Prose line cells: the docling-parse-style sanitizer (behind a flag
356 // while it's validated) or the legacy gap-heuristic reconstruction.
357 let prose = if dp {
358 crate::dp_lines::line_cells(&g, page_h, false)
359 } else {
360 lines_from_glyphs(&g, page_h, Grouping::Prose)
361 };
362 (
363 prose,
364 lines_from_glyphs(&g, page_h, Grouping::CodeSpaceOnly),
365 words_from_glyphs(&g, page_h),
366 )
367 };
368 b.FPDF_ClosePage(page);
369 out
370 }
371}
372
373impl Drop for FfiText<'_> {
374 fn drop(&mut self) {
375 if !self.doc.is_null() {
376 self.bindings.FPDF_CloseDocument(self.doc);
377 }
378 }
379}
380
381/// Read every glyph (codepoint + native box) from the text page, in document
382/// order. A space glyph is kept as a word-boundary marker (NaN box, char `' '`);
383/// pdfium emits these on most lines and they pin word splits exactly. Hard line
384/// breaks are dropped (line structure comes from geometry); the gap heuristic in
385/// [`lines_from_glyphs`] is the fallback for the lines pdfium leaves space-less.
386/// Debug helper: the raw pdfium glyph stream (codepoint + native bottom-left
387/// box) for a page, in pdfium's character order. For comparing against
388/// docling-parse's char cells.
389pub fn debug_glyphs(bytes: &[u8], index: i32) -> Vec<(char, f32, f32)> {
390 let Ok(pdfium) = bind() else {
391 return Vec::new();
392 };
393 let ffi = FfiText::load(pdfium.bindings(), bytes, None);
394 if ffi.doc.is_null() {
395 return Vec::new();
396 }
397 let b = ffi.bindings;
398 let page = b.FPDF_LoadPage(ffi.doc, index);
399 if page.is_null() {
400 return Vec::new();
401 }
402 let tp = b.FPDFText_LoadPage(page);
403 let mut out = Vec::new();
404 if !tp.is_null() {
405 for g in glyphs(b, tp, true) {
406 out.push((g.ch, g.ll, g.lr));
407 }
408 b.FPDFText_ClosePage(tp);
409 }
410 b.FPDF_ClosePage(page);
411 out
412}
413
414/// One text object on a page, for the hidden-layer diagnostic.
415#[derive(Debug, Clone)]
416pub struct DebugTextObject {
417 /// True when the object is drawn invisibly (text render mode 3) — the marker of
418 /// a hidden duplicate text layer.
419 pub invisible: bool,
420 /// Bounding box in native PDF points (bottom-left origin).
421 pub l: f32,
422 pub b: f32,
423 pub r: f32,
424 pub t: f32,
425 /// The object's text (best-effort; empty if it could not be read).
426 pub text: String,
427}
428
429/// Diagnostic: every text object on page `index`, each tagged visible/invisible
430/// (via the object-level [`FPDFTextObj_GetTextRenderMode`], which — unlike the
431/// per-character render-mode API — is available on the default pdfium binding).
432/// A hidden duplicate text layer shows up as invisible objects repeating the
433/// visible text. Used by the `dump_render_modes` example.
434///
435/// [`FPDFTextObj_GetTextRenderMode`]: pdfium_render::prelude::PdfiumLibraryBindings::FPDFTextObj_GetTextRenderMode
436pub fn debug_text_objects(bytes: &[u8], index: i32) -> Vec<DebugTextObject> {
437 let Ok(pdfium) = bind() else {
438 return Vec::new();
439 };
440 let ffi = FfiText::load(pdfium.bindings(), bytes, None);
441 if ffi.doc.is_null() {
442 return Vec::new();
443 }
444 let b = ffi.bindings;
445 let page = b.FPDF_LoadPage(ffi.doc, index);
446 if page.is_null() {
447 return Vec::new();
448 }
449 let tp = b.FPDFText_LoadPage(page);
450 let mut out = Vec::new();
451 let n = b.FPDFPage_CountObjects(page);
452 for i in 0..n {
453 let obj = b.FPDFPage_GetObject(page, i);
454 if obj.is_null() || b.FPDFPageObj_GetType(obj) != FPDF_PAGEOBJ_TEXT as i32 {
455 continue;
456 }
457 let (mut l, mut bot, mut r, mut top) = (0f32, 0f32, 0f32, 0f32);
458 if b.FPDFPageObj_GetBounds(obj, &mut l, &mut bot, &mut r, &mut top) == 0 {
459 continue;
460 }
461 let invisible = b.FPDFTextObj_GetTextRenderMode(obj) == INVISIBLE_RENDER_MODE;
462 let text = if tp.is_null() {
463 String::new()
464 } else {
465 // FPDFTextObj_GetText returns the count of UTF-16 code units, including
466 // the trailing NUL; call once for the size, once to fill.
467 let need = b.FPDFTextObj_GetText(obj, tp, std::ptr::null_mut(), 0);
468 if need <= 1 {
469 String::new()
470 } else {
471 let mut buf = vec![0u16; need as usize];
472 b.FPDFTextObj_GetText(obj, tp, buf.as_mut_ptr(), need);
473 if let Some(&0) = buf.last() {
474 buf.pop();
475 }
476 String::from_utf16_lossy(&buf)
477 }
478 };
479 out.push(DebugTextObject {
480 invisible,
481 l,
482 b: bot,
483 r,
484 t: top,
485 text,
486 });
487 }
488 if !tp.is_null() {
489 b.FPDFText_ClosePage(tp);
490 }
491 b.FPDF_ClosePage(page);
492 out
493}
494
495/// Hash a glyph's PDF font name + flags, for `enforce_same_font`. 0 if unavailable.
496fn font_hash(b: &dyn PdfiumLibraryBindings, tp: FPDF_TEXTPAGE, i: i32) -> u64 {
497 use std::hash::{Hash, Hasher};
498 let mut flags: std::os::raw::c_int = 0;
499 let len = b.FPDFText_GetFontInfo(tp, i, std::ptr::null_mut(), 0, &mut flags);
500 if len == 0 {
501 return 0;
502 }
503 let mut buf = vec![0u8; len as usize];
504 b.FPDFText_GetFontInfo(
505 tp,
506 i,
507 buf.as_mut_ptr() as *mut std::os::raw::c_void,
508 len,
509 &mut flags,
510 );
511 let mut h = std::collections::hash_map::DefaultHasher::new();
512 buf.hash(&mut h);
513 flags.hash(&mut h);
514 h.finish()
515}
516
517/// pdfium text render mode 3: the glyph is drawn with neither fill nor stroke —
518/// an invisible glyph. Web-to-PDF exporters put a hidden plain-text copy of
519/// syntax-highlighted code (and other "copy"/accessibility layers) in this mode,
520/// which the char-level text API then extracts as a duplicate of the visible text.
521const INVISIBLE_RENDER_MODE: i32 = 3;
522
523fn glyphs(b: &dyn PdfiumLibraryBindings, tp: FPDF_TEXTPAGE, fetch_font: bool) -> Vec<Glyph> {
524 let n = b.FPDFText_CountChars(tp);
525 let mut out = Vec::with_capacity(n.max(0) as usize);
526 for i in 0..n {
527 let ch = match char::from_u32(b.FPDFText_GetUnicode(tp, i)) {
528 Some(c) => c,
529 None => continue,
530 };
531 if ch == '\r' || ch == '\n' {
532 continue;
533 }
534 // Spaces are font-neutral (0): pdfium's generated spaces carry a default
535 // font that would otherwise block every word↔space merge under
536 // enforce_same_font; docling-parse's spaces inherit the run's font.
537 let font = if fetch_font && !ch.is_whitespace() {
538 font_hash(b, tp, i)
539 } else {
540 0
541 };
542 let (mut l, mut r, mut bot, mut top) = (0f64, 0f64, 0f64, 0f64);
543 let has_box = b.FPDFText_GetCharBox(tp, i, &mut l, &mut r, &mut bot, &mut top) != 0;
544 // Loose box: font ascent/descent + glyph advance, uniform per font/size.
545 let mut lr = FS_RECTF {
546 left: 0.0,
547 top: 0.0,
548 right: 0.0,
549 bottom: 0.0,
550 };
551 let (ll, lb, lrt, ltop) = if b.FPDFText_GetLooseCharBox(tp, i, &mut lr) != 0 {
552 (lr.left, lr.bottom, lr.right, lr.top)
553 } else if has_box {
554 (l as f32, bot as f32, r as f32, top as f32)
555 } else {
556 (f32::NAN, 0.0, 0.0, 0.0)
557 };
558 if ch.is_whitespace() {
559 // Keep the space *with its box* (the docling-parse-style line sanitizer
560 // needs literal space glyphs); NaN `l` if pdfium reports no box (the
561 // legacy `lines_from_glyphs` ignores the box and only flags a space).
562 out.push(Glyph {
563 ch: ' ',
564 l: if has_box { l as f32 } else { f32::NAN },
565 b: if has_box { bot as f32 } else { 0.0 },
566 r: if has_box { r as f32 } else { 0.0 },
567 t: if has_box { top as f32 } else { 0.0 },
568 ll,
569 lb,
570 lr: lrt,
571 lt: ltop,
572 font,
573 });
574 continue;
575 }
576 if !has_box {
577 continue;
578 }
579 out.push(Glyph {
580 ch,
581 l: l as f32,
582 b: bot as f32,
583 r: r as f32,
584 t: top as f32,
585 ll,
586 lb,
587 lr: lrt,
588 lt: ltop,
589 font,
590 });
591 }
592 // pdfium splits the Arabic lam-alef ligature into two chars at the *same* x
593 // (it's one glyph) in visual order — `alef-variant, lam`. docling-parse and
594 // logical order are `lam, alef-variant`. Detect the ligature by the shared x
595 // and swap. The shared-x test reliably distinguishes a true ligature from a
596 // genuine `alef + lam` sequence (the article `ال`, or `فعالة`), whose two
597 // glyphs sit at different x and must NOT be reordered.
598 for i in 0..out.len().saturating_sub(1) {
599 let same_x = out[i].l.is_finite()
600 && out[i + 1].l.is_finite()
601 && (out[i].l - out[i + 1].l).abs() < 1.0;
602 if same_x
603 && matches!(out[i].ch, '\u{0622}' | '\u{0623}' | '\u{0625}' | '\u{0627}')
604 && out[i + 1].ch == '\u{0644}'
605 {
606 out.swap(i, i + 1);
607 }
608 }
609 // Reconstruct degenerate (zero-width) loose space boxes by spanning the gap to
610 // the next glyph on the same line, so the sanitizer keeps them as word
611 // separators rather than dropping them (which would merge `Information systems`
612 // → `Informationsystems`). pdfium gives generated spaces a zero-width box at a
613 // wrong baseline; a wrap (different baseline) or a touching gap is left alone.
614 for i in 0..out.len() {
615 if out[i].ch != ' ' || (out[i].lr - out[i].ll).abs() >= 0.5 {
616 continue;
617 }
618 let prev = out[..i]
619 .iter()
620 .rev()
621 .find(|g| g.ch != ' ' && g.ll.is_finite())
622 .map(|g| (g.lr, g.lb, g.lt));
623 let next = out[i + 1..]
624 .iter()
625 .find(|g| g.ch != ' ' && g.ll.is_finite())
626 .map(|g| (g.ll, g.lb));
627 if let (Some((plr, plb, plt)), Some((nll, nlb))) = (prev, next) {
628 let line_h = (plt - plb).abs().max(1.0);
629 if (plb - nlb).abs() < line_h * 0.5 && nll > plr + 0.5 {
630 out[i].ll = plr;
631 out[i].lr = nll;
632 out[i].lb = plb;
633 out[i].lt = plt;
634 }
635 }
636 }
637 out
638}
639
640/// How [`lines_from_glyphs`] splits a line into words.
641#[derive(Clone, Copy, PartialEq)]
642enum Grouping {
643 /// Gap heuristic + punctuation glue (`engines,`, `[37`, `98.5`) — prose.
644 Prose,
645 /// Split only at literal space glyphs, never glue — pdfium code cells.
646 /// pdfium's monospace listings carry a real space glyph at every source space,
647 /// and its overhanging loose boxes would make the gap heuristic over-split
648 /// (`f un c t i o n`), so honouring just the spaces reproduces the spacing.
649 CodeSpaceOnly,
650 /// Split on the inter-glyph **gap** (or a space glyph), but never glue — for
651 /// the parser's code cells: the parser emits no space glyphs (a source space
652 /// is a positioning gap), and its clean advance boxes make the gap reliable.
653 /// Unlike [`Grouping::Prose`] there is no punctuation glue, so a real gap
654 /// always splits (`et al. 2000`, not `et al.2000`) while genuinely touching
655 /// tokens stay joined (`add(a,` / `b)`).
656 CodeGap,
657}
658
659/// Group glyphs (document order) into words then lines, the way docling-parse
660/// does: a new **word** starts where the horizontal gap to the previous glyph
661/// exceeds ~0.2 × the font height (a real space is ~0.3 × height; letter
662/// tracking is smaller, so titles don't shatter); a new **line** starts where
663/// the baseline drops by ~half the font height (a superscript rises without
664/// dropping, so it stays on its line). Coordinates are flipped to top-left.
665/// See [`Grouping`] for how each mode decides word boundaries.
666fn lines_from_glyphs(gs: &[Glyph], page_h: f32, mode: Grouping) -> Vec<TextCell> {
667 let mut cells: Vec<TextCell> = Vec::new();
668 let mut words: Vec<String> = Vec::new(); // words on the current line
669 let mut word = String::new();
670 // current line bounding box, native
671 let (mut ll, mut lb, mut lr, mut lt) = (
672 f32::INFINITY,
673 f32::INFINITY,
674 f32::NEG_INFINITY,
675 f32::NEG_INFINITY,
676 );
677 // Tallest glyph seen on the current line: the word-gap threshold is relative
678 // to it, so a small-font run on the line (a superscript citation) isn't split
679 // at its tight digit gaps, while a big display title isn't split at its wider
680 // letter tracking. A real inter-word space is ~0.3× the font height.
681 let mut line_h: f32 = 0.0;
682 let mut prev: Option<&Glyph> = None;
683 // A space glyph between non-space glyphs pins a word split the gap heuristic
684 // can miss (tight justified spacing); it carries no geometry.
685 let mut pending_space = false;
686
687 for g in gs {
688 if g.ch == ' ' {
689 pending_space = true;
690 continue;
691 }
692 let h = (g.t - g.b).abs().max(1.0);
693 let (mut new_word, mut new_line) = (false, false);
694 if let Some(p) = prev {
695 // A new line drops the baseline *and* resets x leftward; requiring the
696 // x-reset avoids a descending comma/semicolon faking a line break. A
697 // *large* drop (≥1.5× the line height — a skipped line, e.g. a centered
698 // page-number footer below a short last word) is always a new line,
699 // even without the x-reset.
700 // LTR wraps reset x leftward (`g.l < p.r`); RTL (Arabic) wraps reset
701 // rightward (the new line begins at the far right). A large drop
702 // (≥1.5× line height) is a new line regardless of x.
703 let x_reset = if is_arabic(g.ch) || is_arabic(p.ch) {
704 g.l > p.r
705 } else {
706 g.l < p.r
707 };
708 new_line = (p.b - g.b > h * 0.5 && x_reset) || (p.b - g.b > line_h.max(h) * 1.5);
709 // Don't split before closing punctuation, after opening punctuation, or
710 // after a period that runs into a digit/lowercase letter — docling
711 // keeps `engines,` / `[37` / `i.e.` / `98.5` together even across a
712 // space or gap.
713 let glued = is_close_punct(g.ch)
714 || is_open_punct(p.ch)
715 || (p.ch.is_ascii_digit() && g.ch.is_ascii_digit())
716 || (p.ch == '.'
717 && !pending_space
718 && (g.ch.is_ascii_digit() || g.ch.is_ascii_lowercase()));
719 let word_gap = line_h.max(h) * 0.25;
720 new_word = if mode == Grouping::CodeSpaceOnly {
721 new_line || pending_space
722 } else if mode == Grouping::CodeGap {
723 // Gap-based, no glue: a real gap always splits, touching tokens join.
724 new_line || pending_space || g.l - p.r > word_gap
725 } else if is_arabic(g.ch) || is_arabic(p.ch) {
726 // RTL runs right-to-left, so the inter-word gap is `p.l - g.r`. A
727 // real word space has a gap; pdfium also emits spurious zero-gap
728 // space glyphs inside words (`التي`), so require the gap rather
729 // than trusting a bare space glyph.
730 new_line || (p.l - g.r > word_gap && !glued)
731 } else {
732 new_line || ((pending_space || g.l - p.r > word_gap) && !glued)
733 };
734 }
735 pending_space = false;
736 if new_line {
737 push_word(&mut word, &mut words);
738 push_line(&mut words, (ll, lb, lr, lt), page_h, &mut cells);
739 (ll, lb, lr, lt) = (
740 f32::INFINITY,
741 f32::INFINITY,
742 f32::NEG_INFINITY,
743 f32::NEG_INFINITY,
744 );
745 line_h = 0.0;
746 } else if new_word {
747 push_word(&mut word, &mut words);
748 }
749 word.push(g.ch);
750 ll = ll.min(g.l);
751 lb = lb.min(g.b);
752 lr = lr.max(g.r);
753 lt = lt.max(g.t);
754 line_h = line_h.max(h);
755 prev = Some(g);
756 }
757 push_word(&mut word, &mut words);
758 push_line(&mut words, (ll, lb, lr, lt), page_h, &mut cells);
759 cells
760}
761
762/// Code line cells from the **parser**'s glyph stream. Unlike pdfium — whose
763/// monospace listings carry explicit space glyphs (so [`Grouping::CodeSpaceOnly`]
764/// keeps their spacing) — the parser emits no space glyphs: a source space is a
765/// positioning gap. So code cells use [`Grouping::CodeGap`], which splits on the
766/// inter-glyph gap (a space wherever it exceeds ~0.25× the line height) but never
767/// glues punctuation, so `et al. 2000` keeps its space while `add(a,` / `b)` stay
768/// joined. The parser's clean advance boxes make the gap heuristic reliable here,
769/// where pdfium's overhanging loose boxes would over-split (`f un c t i o n`).
770pub(crate) fn code_cells_from_glyphs(gs: &[Glyph], page_h: f32) -> Vec<TextCell> {
771 lines_from_glyphs(gs, page_h, Grouping::CodeGap)
772}
773
774/// Per-word cells (each word's text + top-left bbox), using the same word/line
775/// splitting as [`lines_from_glyphs`] but emitting one cell per word instead of
776/// joining into lines — the legacy gap-heuristic word grouping, kept for the
777/// pdfium word path (`DOCLING_PDFIUM_WORDS`). The default parser path uses
778/// [`crate::dp_lines::word_cells`] instead.
779pub(crate) fn words_from_glyphs(gs: &[Glyph], page_h: f32) -> Vec<TextCell> {
780 let mut cells = Vec::new();
781 let mut word = String::new();
782 let inf = (
783 f32::INFINITY,
784 f32::INFINITY,
785 f32::NEG_INFINITY,
786 f32::NEG_INFINITY,
787 );
788 let (mut wl, mut wb, mut wr, mut wt) = inf;
789 let mut line_h: f32 = 0.0;
790 let mut prev: Option<&Glyph> = None;
791 let mut pending_space = false;
792 for g in gs {
793 if g.ch == ' ' {
794 pending_space = true;
795 continue;
796 }
797 let h = (g.t - g.b).abs().max(1.0);
798 let mut new_line = false;
799 let mut new_word = false;
800 if let Some(p) = prev {
801 // LTR wraps reset x leftward (`g.l < p.r`); RTL (Arabic) wraps reset
802 // rightward (the new line begins at the far right). A large drop
803 // (≥1.5× line height) is a new line regardless of x.
804 let x_reset = if is_arabic(g.ch) || is_arabic(p.ch) {
805 g.l > p.r
806 } else {
807 g.l < p.r
808 };
809 new_line = (p.b - g.b > h * 0.5 && x_reset) || (p.b - g.b > line_h.max(h) * 1.5);
810 // No digit-digit glue here (unlike the prose grouping): table cells in
811 // adjacent columns are numeric and a column gap must still split them
812 // (`0.965` `0.934`, not `0.9650.934`). Intra-number digits have no gap
813 // so they stay together regardless.
814 let glued = is_close_punct(g.ch)
815 || is_open_punct(p.ch)
816 || (p.ch == '.'
817 && !pending_space
818 && (g.ch.is_ascii_digit() || g.ch.is_ascii_lowercase()));
819 let word_gap = line_h.max(h) * 0.25;
820 new_word = new_line || ((pending_space || g.l - p.r > word_gap) && !glued);
821 }
822 pending_space = false;
823 if new_word && !word.is_empty() {
824 cells.push(TextCell {
825 text: std::mem::take(&mut word),
826 l: wl,
827 t: page_h - wt,
828 r: wr,
829 b: page_h - wb,
830 });
831 (wl, wb, wr, wt) = inf;
832 }
833 if new_line {
834 line_h = 0.0;
835 }
836 word.push(g.ch);
837 wl = wl.min(g.l);
838 wb = wb.min(g.b);
839 wr = wr.max(g.r);
840 wt = wt.max(g.t);
841 line_h = line_h.max(h);
842 prev = Some(g);
843 }
844 if !word.is_empty() {
845 cells.push(TextCell {
846 text: word,
847 l: wl,
848 t: page_h - wt,
849 r: wr,
850 b: page_h - wb,
851 });
852 }
853 cells
854}
855
856fn is_arabic(c: char) -> bool {
857 ('\u{0600}'..='\u{06FF}').contains(&c)
858}
859
860fn is_close_punct(c: char) -> bool {
861 matches!(
862 c,
863 ',' | '.' | ';' | '!' | '?' | ')' | ']' | '}' | '%' | '\'' | '\u{2019}' | '\u{2018}'
864 )
865}
866
867fn is_open_punct(c: char) -> bool {
868 // `@` glues to what follows (`mAP @0.5`, `bpf@zurich`, `@decorator`).
869 matches!(c, '(' | '[' | '{' | '@')
870}
871
872fn push_word(word: &mut String, words: &mut Vec<String>) {
873 if !word.is_empty() {
874 words.push(std::mem::take(word));
875 }
876}
877
878fn push_line(
879 words: &mut Vec<String>,
880 bbox: (f32, f32, f32, f32),
881 page_h: f32,
882 cells: &mut Vec<TextCell>,
883) {
884 if words.is_empty() {
885 return;
886 }
887 let text = std::mem::take(words).join(" ");
888 let (l, b, r, t) = bbox;
889 cells.push(TextCell {
890 text,
891 l,
892 t: page_h - t,
893 r,
894 b: page_h - b,
895 });
896}