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. Off by default: the
90/// parser's space-glyph-only code grouping drops the inter-token spaces pdfium
91/// recovers in monospace listings (`function add` → `functionadd`), a regression
92/// vs the docling groundtruth. Opt in with `DOCLING_PARSER_CODE` once the parser
93/// emits faithful monospace spacing. `DOCLING_PDFIUM_TEXT` always wins (pdfium).
94pub(crate) fn use_parser_code() -> bool {
95 std::env::var("DOCLING_PARSER_CODE").is_ok() && std::env::var("DOCLING_PDFIUM_TEXT").is_err()
96}
97
98fn bind() -> Result<Pdfium, PdfiumError> {
99 if let Ok(path) = std::env::var("PDFIUM_DYNAMIC_LIB_PATH") {
100 let name = Pdfium::pdfium_platform_library_name_at_path(&path);
101 if let Ok(b) = Pdfium::bind_to_library(&name) {
102 return Ok(Pdfium::new(b));
103 }
104 if let Ok(b) = Pdfium::bind_to_library(&path) {
105 return Ok(Pdfium::new(b));
106 }
107 }
108 Pdfium::bind_to_system_library().map(Pdfium::new)
109}
110
111impl PdfDocument {
112 /// Parse a PDF from bytes, optionally decrypting with `password`.
113 ///
114 /// Note: this materialises **every** page's rendered bitmap in memory at
115 /// once. For large documents prefer [`for_each_page`], which streams.
116 pub fn open(bytes: &[u8], password: Option<&str>) -> Result<Self, PdfiumError> {
117 let pdfium = bind()?;
118 let ffi = FfiText::load(pdfium.bindings(), bytes, password);
119 let doc = pdfium.load_pdf_from_byte_slice(bytes, password)?;
120 let mut rust = rust_parser_cells(bytes);
121 let mut pages = Vec::new();
122 for (i, page) in doc.pages().iter().enumerate() {
123 let rc = rust.as_mut().and_then(|v| v.get_mut(i).map(std::mem::take));
124 pages.push(extract_page(&page, &ffi, i as i32, rc)?);
125 }
126 Ok(PdfDocument { pages })
127 }
128}
129
130/// Per-page prose line cells from the pure-Rust text parser. This is the
131/// **default** text layer (it matches docling-parse's char geometry and is a
132/// strict improvement on byte-conformance — e.g. it recovers the Arabic
133/// sentence-period attachment in `right_to_left_01`). Set `DOCLING_PDFIUM_TEXT`
134/// to fall back to pdfium's text layer. The parser returns an empty page when a
135/// PDF (or a page) has no parseable text layer; the caller keeps pdfium's cells
136/// in that case, so scanned/edge-case pages are unaffected.
137fn rust_parser_cells(bytes: &[u8]) -> Option<Vec<crate::textparse::PageParserCells>> {
138 if std::env::var("DOCLING_PDFIUM_TEXT").is_ok() {
139 return None;
140 }
141 Some(crate::timing::timed("textparse", || {
142 crate::textparse::pdf_all_cells(bytes)
143 }))
144}
145
146/// Number of pages in a PDF, without rendering any of them — used to decide
147/// whether a document is worth spinning up the parallel worker pool.
148pub fn page_count(bytes: &[u8], password: Option<&str>) -> Result<usize, PdfiumError> {
149 let pdfium = bind()?;
150 let doc = pdfium.load_pdf_from_byte_slice(bytes, password)?;
151 Ok(doc.pages().len() as usize)
152}
153
154/// Render + extract pages one at a time, handing each (owned) [`PdfPage`] to `f`.
155/// Only one page bitmap is resident at a time — a rendered page is ~5 MB, so a
156/// large PDF would otherwise hold gigabytes of bitmaps at once. `f` receives the
157/// zero-based page index and the total page count.
158///
159/// `E` is the caller's error type; pdfium errors convert into it via `From`.
160pub fn for_each_page<E, F>(bytes: &[u8], password: Option<&str>, mut f: F) -> Result<(), E>
161where
162 E: From<PdfiumError>,
163 F: FnMut(usize, usize, PdfPage) -> Result<(), E>,
164{
165 let pdfium = bind()?;
166 let ffi = FfiText::load(pdfium.bindings(), bytes, password);
167 let doc = pdfium.load_pdf_from_byte_slice(bytes, password)?;
168 let mut rust = rust_parser_cells(bytes);
169 let pages = doc.pages();
170 let total = pages.len() as usize;
171 for (i, page) in pages.iter().enumerate() {
172 let rc = rust.as_mut().and_then(|v| v.get_mut(i).map(std::mem::take));
173 let extracted = extract_page(&page, &ffi, i as i32, rc)?;
174 f(i, total, extracted)?;
175 }
176 Ok(())
177}
178
179fn extract_page(
180 page: &pdfium_render::prelude::PdfPage<'_>,
181 ffi: &FfiText<'_>,
182 index: i32,
183 rust_cells: Option<crate::textparse::PageParserCells>,
184) -> Result<PdfPage, PdfiumError> {
185 let width = page.width().value;
186 let height = page.height().value;
187
188 let (mut cells, mut code_cells, mut word_cells) =
189 crate::timing::timed("ffi.page_cells", || ffi.page_cells(index, height));
190 if cells.is_empty() {
191 cells = segment_cells(&page.text()?, height);
192 }
193 // Default: use the pure-Rust text parser instead of pdfium's text layer
194 // (override with `DOCLING_PDFIUM_TEXT`). Prose line cells always come from the
195 // parser; word and code cells do too unless `DOCLING_PDFIUM_WORDS` keeps them
196 // on pdfium (the parser's word grouping reproduces docling-parse's, which
197 // TableFormer matches against — roadmap item 6). A page the parser couldn't
198 // read (no text layer) keeps pdfium's cells.
199 if let Some(rc) = rust_cells {
200 if !rc.prose.is_empty() {
201 cells = rc.prose;
202 }
203 if use_parser_words() && !rc.words.is_empty() {
204 word_cells = rc.words;
205 }
206 if use_parser_code() && !rc.code.is_empty() {
207 code_cells = rc.code;
208 }
209 }
210
211 // docling renders at 1.5× the target scale and downsamples "to make it
212 // sharper" (pypdfium2 → PIL BICUBIC). Replicate exactly: the TableFormer
213 // model is pixel-sensitive, so the page bitmap must match byte-for-byte.
214 // `CatmullRom` is the same a=-0.5 cubic kernel as PIL's BICUBIC.
215 const SUPERSAMPLE: f32 = 1.5;
216 let tw = (width * RENDER_SCALE * SUPERSAMPLE).round().max(1.0) as i32;
217 let th = (height * RENDER_SCALE * SUPERSAMPLE).round().max(1.0) as i32;
218 let cfg = PdfRenderConfig::new()
219 .set_target_width(tw)
220 .set_target_height(th);
221 let big = crate::timing::timed("pdfium.render", || {
222 page.render_with_config(&cfg)
223 .map(|b| b.as_image().into_rgb8())
224 })?;
225 let dw = (width * RENDER_SCALE).round().max(1.0) as u32;
226 let dh = (height * RENDER_SCALE).round().max(1.0) as u32;
227 let image = crate::timing::timed("image.resize", || {
228 image::imageops::resize(&big, dw, dh, image::imageops::FilterType::CatmullRom)
229 });
230
231 Ok(PdfPage {
232 width,
233 height,
234 scale: RENDER_SCALE,
235 cells,
236 code_cells,
237 word_cells,
238 image,
239 links: extract_links(page, height),
240 })
241}
242
243/// Collect web/mail/tel hyperlink annotations on a page, mapping each link's
244/// rectangle into top-left page coordinates (like [`TextCell`]). `file://` and
245/// in-document destinations are skipped — only externally meaningful targets are
246/// rendered. pdfium occasionally lists a link twice; rects are kept as-is and the
247/// caller dedupes by resolved anchor text.
248fn extract_links(page: &pdfium_render::prelude::PdfPage<'_>, page_h: f32) -> Vec<LinkAnnot> {
249 let mut out = Vec::new();
250 for link in page.links().iter() {
251 let Some(uri) = link
252 .action()
253 .and_then(|a| a.as_uri_action().and_then(|u| u.uri().ok()))
254 else {
255 continue;
256 };
257 let scheme_ok = ["http://", "https://", "mailto:", "tel:"]
258 .iter()
259 .any(|s| uri.starts_with(s));
260 if !scheme_ok {
261 continue;
262 }
263 if let Ok(rect) = link.rect() {
264 out.push(LinkAnnot {
265 l: rect.left().value,
266 t: page_h - rect.top().value,
267 r: rect.right().value,
268 b: page_h - rect.bottom().value,
269 uri,
270 });
271 }
272 }
273 out
274}
275
276/// Fallback line cells from pdfium-render's style segments (one cell per
277/// segment). Used only when the raw-FFI text page can't be loaded.
278fn segment_cells(text: &PdfPageText, page_h: f32) -> Vec<TextCell> {
279 text.segments()
280 .iter()
281 .filter_map(|seg| {
282 let s = seg.text();
283 if s.trim().is_empty() {
284 return None;
285 }
286 let r = seg.bounds();
287 Some(TextCell {
288 text: s,
289 l: r.left().value,
290 t: page_h - r.top().value,
291 r: r.right().value,
292 b: page_h - r.bottom().value,
293 })
294 })
295 .collect()
296}
297
298/// A second, raw-FFI handle on the same PDF used to drive the character loop
299/// (`FPDFText_GetUnicode`/`GetCharBox`) that pdfium-render's safe API doesn't
300/// expose. Closes the document on drop.
301struct FfiText<'a> {
302 bindings: &'a dyn PdfiumLibraryBindings,
303 doc: FPDF_DOCUMENT,
304}
305
306/// One glyph: codepoint + native (y-up) box edges. `l/b/r/t` is pdfium's *tight*
307/// ink box (used by the legacy `lines_from_glyphs`); `ll/lb/lr/lt` is the *loose*
308/// box (font ascent/descent + advance — uniform per font/size), which the
309/// docling-parse-style sanitizer needs so adjacent glyphs share a top edge.
310pub(crate) struct Glyph {
311 pub(crate) ch: char,
312 pub(crate) l: f32,
313 pub(crate) b: f32,
314 pub(crate) r: f32,
315 pub(crate) t: f32,
316 pub(crate) ll: f32,
317 pub(crate) lb: f32,
318 pub(crate) lr: f32,
319 pub(crate) lt: f32,
320 /// Hash of the PDF font name + flags (0 when not fetched). The sanitizer uses
321 /// it for docling-parse's `enforce_same_font` (keeps a bold label and regular
322 /// value as separate line cells, e.g. `LABEL : value`).
323 pub(crate) font: u64,
324}
325
326impl<'a> FfiText<'a> {
327 fn load(bindings: &'a dyn PdfiumLibraryBindings, bytes: &[u8], password: Option<&str>) -> Self {
328 let doc = bindings.FPDF_LoadMemDocument(bytes, password);
329 FfiText { bindings, doc }
330 }
331
332 /// Reconstruct line cells for page `index` (zero-based) via the
333 /// chars→words→lines grouping. Returns `(prose_cells, code_cells)` — the same
334 /// glyphs grouped two ways (gap-heuristic for prose, space-glyph-only for
335 /// code). Both empty on any failure (caller falls back).
336 fn page_cells(&self, index: i32, page_h: f32) -> (Vec<TextCell>, Vec<TextCell>, Vec<TextCell>) {
337 let empty = || (Vec::new(), Vec::new(), Vec::new());
338 if self.doc.is_null() {
339 return empty();
340 }
341 let b = self.bindings;
342 let page = b.FPDF_LoadPage(self.doc, index);
343 if page.is_null() {
344 return empty();
345 }
346 let tp = b.FPDFText_LoadPage(page);
347 let out = if tp.is_null() {
348 empty()
349 } else {
350 let dp = use_dp_lines();
351 let g = glyphs(b, tp, dp);
352 b.FPDFText_ClosePage(tp);
353 // Prose line cells: the docling-parse-style sanitizer (behind a flag
354 // while it's validated) or the legacy gap-heuristic reconstruction.
355 let prose = if dp {
356 crate::dp_lines::line_cells(&g, page_h, false)
357 } else {
358 lines_from_glyphs(&g, page_h, false)
359 };
360 (
361 prose,
362 lines_from_glyphs(&g, page_h, true),
363 words_from_glyphs(&g, page_h),
364 )
365 };
366 b.FPDF_ClosePage(page);
367 out
368 }
369}
370
371impl Drop for FfiText<'_> {
372 fn drop(&mut self) {
373 if !self.doc.is_null() {
374 self.bindings.FPDF_CloseDocument(self.doc);
375 }
376 }
377}
378
379/// Read every glyph (codepoint + native box) from the text page, in document
380/// order. A space glyph is kept as a word-boundary marker (NaN box, char `' '`);
381/// pdfium emits these on most lines and they pin word splits exactly. Hard line
382/// breaks are dropped (line structure comes from geometry); the gap heuristic in
383/// [`lines_from_glyphs`] is the fallback for the lines pdfium leaves space-less.
384/// Debug helper: the raw pdfium glyph stream (codepoint + native bottom-left
385/// box) for a page, in pdfium's character order. For comparing against
386/// docling-parse's char cells.
387pub fn debug_glyphs(bytes: &[u8], index: i32) -> Vec<(char, f32, f32)> {
388 let Ok(pdfium) = bind() else {
389 return Vec::new();
390 };
391 let ffi = FfiText::load(pdfium.bindings(), bytes, None);
392 if ffi.doc.is_null() {
393 return Vec::new();
394 }
395 let b = ffi.bindings;
396 let page = b.FPDF_LoadPage(ffi.doc, index);
397 if page.is_null() {
398 return Vec::new();
399 }
400 let tp = b.FPDFText_LoadPage(page);
401 let mut out = Vec::new();
402 if !tp.is_null() {
403 for g in glyphs(b, tp, true) {
404 out.push((g.ch, g.ll, g.lr));
405 }
406 b.FPDFText_ClosePage(tp);
407 }
408 b.FPDF_ClosePage(page);
409 out
410}
411
412/// Hash a glyph's PDF font name + flags, for `enforce_same_font`. 0 if unavailable.
413fn font_hash(b: &dyn PdfiumLibraryBindings, tp: FPDF_TEXTPAGE, i: i32) -> u64 {
414 use std::hash::{Hash, Hasher};
415 let mut flags: std::os::raw::c_int = 0;
416 let len = b.FPDFText_GetFontInfo(tp, i, std::ptr::null_mut(), 0, &mut flags);
417 if len == 0 {
418 return 0;
419 }
420 let mut buf = vec![0u8; len as usize];
421 b.FPDFText_GetFontInfo(
422 tp,
423 i,
424 buf.as_mut_ptr() as *mut std::os::raw::c_void,
425 len,
426 &mut flags,
427 );
428 let mut h = std::collections::hash_map::DefaultHasher::new();
429 buf.hash(&mut h);
430 flags.hash(&mut h);
431 h.finish()
432}
433
434fn glyphs(b: &dyn PdfiumLibraryBindings, tp: FPDF_TEXTPAGE, fetch_font: bool) -> Vec<Glyph> {
435 let n = b.FPDFText_CountChars(tp);
436 let mut out = Vec::with_capacity(n.max(0) as usize);
437 for i in 0..n {
438 let ch = match char::from_u32(b.FPDFText_GetUnicode(tp, i)) {
439 Some(c) => c,
440 None => continue,
441 };
442 if ch == '\r' || ch == '\n' {
443 continue;
444 }
445 // Spaces are font-neutral (0): pdfium's generated spaces carry a default
446 // font that would otherwise block every word↔space merge under
447 // enforce_same_font; docling-parse's spaces inherit the run's font.
448 let font = if fetch_font && !ch.is_whitespace() {
449 font_hash(b, tp, i)
450 } else {
451 0
452 };
453 let (mut l, mut r, mut bot, mut top) = (0f64, 0f64, 0f64, 0f64);
454 let has_box = b.FPDFText_GetCharBox(tp, i, &mut l, &mut r, &mut bot, &mut top) != 0;
455 // Loose box: font ascent/descent + glyph advance, uniform per font/size.
456 let mut lr = FS_RECTF {
457 left: 0.0,
458 top: 0.0,
459 right: 0.0,
460 bottom: 0.0,
461 };
462 let (ll, lb, lrt, ltop) = if b.FPDFText_GetLooseCharBox(tp, i, &mut lr) != 0 {
463 (lr.left, lr.bottom, lr.right, lr.top)
464 } else if has_box {
465 (l as f32, bot as f32, r as f32, top as f32)
466 } else {
467 (f32::NAN, 0.0, 0.0, 0.0)
468 };
469 if ch.is_whitespace() {
470 // Keep the space *with its box* (the docling-parse-style line sanitizer
471 // needs literal space glyphs); NaN `l` if pdfium reports no box (the
472 // legacy `lines_from_glyphs` ignores the box and only flags a space).
473 out.push(Glyph {
474 ch: ' ',
475 l: if has_box { l as f32 } else { f32::NAN },
476 b: if has_box { bot as f32 } else { 0.0 },
477 r: if has_box { r as f32 } else { 0.0 },
478 t: if has_box { top as f32 } else { 0.0 },
479 ll,
480 lb,
481 lr: lrt,
482 lt: ltop,
483 font,
484 });
485 continue;
486 }
487 if !has_box {
488 continue;
489 }
490 out.push(Glyph {
491 ch,
492 l: l as f32,
493 b: bot as f32,
494 r: r as f32,
495 t: top as f32,
496 ll,
497 lb,
498 lr: lrt,
499 lt: ltop,
500 font,
501 });
502 }
503 // pdfium splits the Arabic lam-alef ligature into two chars at the *same* x
504 // (it's one glyph) in visual order — `alef-variant, lam`. docling-parse and
505 // logical order are `lam, alef-variant`. Detect the ligature by the shared x
506 // and swap. The shared-x test reliably distinguishes a true ligature from a
507 // genuine `alef + lam` sequence (the article `ال`, or `فعالة`), whose two
508 // glyphs sit at different x and must NOT be reordered.
509 for i in 0..out.len().saturating_sub(1) {
510 let same_x = out[i].l.is_finite()
511 && out[i + 1].l.is_finite()
512 && (out[i].l - out[i + 1].l).abs() < 1.0;
513 if same_x
514 && matches!(out[i].ch, '\u{0622}' | '\u{0623}' | '\u{0625}' | '\u{0627}')
515 && out[i + 1].ch == '\u{0644}'
516 {
517 out.swap(i, i + 1);
518 }
519 }
520 // Reconstruct degenerate (zero-width) loose space boxes by spanning the gap to
521 // the next glyph on the same line, so the sanitizer keeps them as word
522 // separators rather than dropping them (which would merge `Information systems`
523 // → `Informationsystems`). pdfium gives generated spaces a zero-width box at a
524 // wrong baseline; a wrap (different baseline) or a touching gap is left alone.
525 for i in 0..out.len() {
526 if out[i].ch != ' ' || (out[i].lr - out[i].ll).abs() >= 0.5 {
527 continue;
528 }
529 let prev = out[..i]
530 .iter()
531 .rev()
532 .find(|g| g.ch != ' ' && g.ll.is_finite())
533 .map(|g| (g.lr, g.lb, g.lt));
534 let next = out[i + 1..]
535 .iter()
536 .find(|g| g.ch != ' ' && g.ll.is_finite())
537 .map(|g| (g.ll, g.lb));
538 if let (Some((plr, plb, plt)), Some((nll, nlb))) = (prev, next) {
539 let line_h = (plt - plb).abs().max(1.0);
540 if (plb - nlb).abs() < line_h * 0.5 && nll > plr + 0.5 {
541 out[i].ll = plr;
542 out[i].lr = nll;
543 out[i].lb = plb;
544 out[i].lt = plt;
545 }
546 }
547 }
548 out
549}
550
551/// Group glyphs (document order) into words then lines, the way docling-parse
552/// does: a new **word** starts where the horizontal gap to the previous glyph
553/// exceeds ~0.2 × the font height (a real space is ~0.3 × height; letter
554/// tracking is smaller, so titles don't shatter); a new **line** starts where
555/// the baseline drops by ~half the font height (a superscript rises without
556/// dropping, so it stays on its line). Coordinates are flipped to top-left.
557/// `code` mode splits words **only** at pdfium's own space glyphs and never glues
558/// punctuation — monospace code has wide inter-glyph advances that the prose
559/// gap heuristic mistakes for spaces (`f un c t i o n`), but pdfium emits a real
560/// space glyph at every true gap, so honoring just those reproduces the source
561/// spacing (`function add(a, b)`).
562fn lines_from_glyphs(gs: &[Glyph], page_h: f32, code: bool) -> Vec<TextCell> {
563 let mut cells: Vec<TextCell> = Vec::new();
564 let mut words: Vec<String> = Vec::new(); // words on the current line
565 let mut word = String::new();
566 // current line bounding box, native
567 let (mut ll, mut lb, mut lr, mut lt) = (
568 f32::INFINITY,
569 f32::INFINITY,
570 f32::NEG_INFINITY,
571 f32::NEG_INFINITY,
572 );
573 // Tallest glyph seen on the current line: the word-gap threshold is relative
574 // to it, so a small-font run on the line (a superscript citation) isn't split
575 // at its tight digit gaps, while a big display title isn't split at its wider
576 // letter tracking. A real inter-word space is ~0.3× the font height.
577 let mut line_h: f32 = 0.0;
578 let mut prev: Option<&Glyph> = None;
579 // A space glyph between non-space glyphs pins a word split the gap heuristic
580 // can miss (tight justified spacing); it carries no geometry.
581 let mut pending_space = false;
582
583 for g in gs {
584 if g.ch == ' ' {
585 pending_space = true;
586 continue;
587 }
588 let h = (g.t - g.b).abs().max(1.0);
589 let (mut new_word, mut new_line) = (false, false);
590 if let Some(p) = prev {
591 // A new line drops the baseline *and* resets x leftward; requiring the
592 // x-reset avoids a descending comma/semicolon faking a line break. A
593 // *large* drop (≥1.5× the line height — a skipped line, e.g. a centered
594 // page-number footer below a short last word) is always a new line,
595 // even without the x-reset.
596 // LTR wraps reset x leftward (`g.l < p.r`); RTL (Arabic) wraps reset
597 // rightward (the new line begins at the far right). A large drop
598 // (≥1.5× line height) is a new line regardless of x.
599 let x_reset = if is_arabic(g.ch) || is_arabic(p.ch) {
600 g.l > p.r
601 } else {
602 g.l < p.r
603 };
604 new_line = (p.b - g.b > h * 0.5 && x_reset) || (p.b - g.b > line_h.max(h) * 1.5);
605 // Don't split before closing punctuation, after opening punctuation, or
606 // after a period that runs into a digit/lowercase letter — docling
607 // keeps `engines,` / `[37` / `i.e.` / `98.5` together even across a
608 // space or gap.
609 let glued = is_close_punct(g.ch)
610 || is_open_punct(p.ch)
611 || (p.ch.is_ascii_digit() && g.ch.is_ascii_digit())
612 || (p.ch == '.'
613 && !pending_space
614 && (g.ch.is_ascii_digit() || g.ch.is_ascii_lowercase()));
615 let word_gap = line_h.max(h) * 0.25;
616 new_word = if code {
617 new_line || pending_space
618 } else if is_arabic(g.ch) || is_arabic(p.ch) {
619 // RTL runs right-to-left, so the inter-word gap is `p.l - g.r`. A
620 // real word space has a gap; pdfium also emits spurious zero-gap
621 // space glyphs inside words (`التي`), so require the gap rather
622 // than trusting a bare space glyph.
623 new_line || (p.l - g.r > word_gap && !glued)
624 } else {
625 new_line || ((pending_space || g.l - p.r > word_gap) && !glued)
626 };
627 }
628 pending_space = false;
629 if new_line {
630 push_word(&mut word, &mut words);
631 push_line(&mut words, (ll, lb, lr, lt), page_h, &mut cells);
632 (ll, lb, lr, lt) = (
633 f32::INFINITY,
634 f32::INFINITY,
635 f32::NEG_INFINITY,
636 f32::NEG_INFINITY,
637 );
638 line_h = 0.0;
639 } else if new_word {
640 push_word(&mut word, &mut words);
641 }
642 word.push(g.ch);
643 ll = ll.min(g.l);
644 lb = lb.min(g.b);
645 lr = lr.max(g.r);
646 lt = lt.max(g.t);
647 line_h = line_h.max(h);
648 prev = Some(g);
649 }
650 push_word(&mut word, &mut words);
651 push_line(&mut words, (ll, lb, lr, lt), page_h, &mut cells);
652 cells
653}
654
655/// Code line cells from a glyph stream (parser or pdfium): split only at space
656/// glyphs so monospace runs keep their source spacing. Thin wrapper over
657/// [`lines_from_glyphs`] with `code = true`, for the parser text path.
658pub(crate) fn code_cells_from_glyphs(gs: &[Glyph], page_h: f32) -> Vec<TextCell> {
659 lines_from_glyphs(gs, page_h, true)
660}
661
662/// Per-word cells (each word's text + top-left bbox), using the same word/line
663/// splitting as [`lines_from_glyphs`] but emitting one cell per word instead of
664/// joining into lines — the legacy gap-heuristic word grouping, kept for the
665/// pdfium word path (`DOCLING_PDFIUM_WORDS`). The default parser path uses
666/// [`crate::dp_lines::word_cells`] instead.
667pub(crate) fn words_from_glyphs(gs: &[Glyph], page_h: f32) -> Vec<TextCell> {
668 let mut cells = Vec::new();
669 let mut word = String::new();
670 let inf = (
671 f32::INFINITY,
672 f32::INFINITY,
673 f32::NEG_INFINITY,
674 f32::NEG_INFINITY,
675 );
676 let (mut wl, mut wb, mut wr, mut wt) = inf;
677 let mut line_h: f32 = 0.0;
678 let mut prev: Option<&Glyph> = None;
679 let mut pending_space = false;
680 for g in gs {
681 if g.ch == ' ' {
682 pending_space = true;
683 continue;
684 }
685 let h = (g.t - g.b).abs().max(1.0);
686 let mut new_line = false;
687 let mut new_word = false;
688 if let Some(p) = prev {
689 // LTR wraps reset x leftward (`g.l < p.r`); RTL (Arabic) wraps reset
690 // rightward (the new line begins at the far right). A large drop
691 // (≥1.5× line height) is a new line regardless of x.
692 let x_reset = if is_arabic(g.ch) || is_arabic(p.ch) {
693 g.l > p.r
694 } else {
695 g.l < p.r
696 };
697 new_line = (p.b - g.b > h * 0.5 && x_reset) || (p.b - g.b > line_h.max(h) * 1.5);
698 // No digit-digit glue here (unlike the prose grouping): table cells in
699 // adjacent columns are numeric and a column gap must still split them
700 // (`0.965` `0.934`, not `0.9650.934`). Intra-number digits have no gap
701 // so they stay together regardless.
702 let glued = is_close_punct(g.ch)
703 || is_open_punct(p.ch)
704 || (p.ch == '.'
705 && !pending_space
706 && (g.ch.is_ascii_digit() || g.ch.is_ascii_lowercase()));
707 let word_gap = line_h.max(h) * 0.25;
708 new_word = new_line || ((pending_space || g.l - p.r > word_gap) && !glued);
709 }
710 pending_space = false;
711 if new_word && !word.is_empty() {
712 cells.push(TextCell {
713 text: std::mem::take(&mut word),
714 l: wl,
715 t: page_h - wt,
716 r: wr,
717 b: page_h - wb,
718 });
719 (wl, wb, wr, wt) = inf;
720 }
721 if new_line {
722 line_h = 0.0;
723 }
724 word.push(g.ch);
725 wl = wl.min(g.l);
726 wb = wb.min(g.b);
727 wr = wr.max(g.r);
728 wt = wt.max(g.t);
729 line_h = line_h.max(h);
730 prev = Some(g);
731 }
732 if !word.is_empty() {
733 cells.push(TextCell {
734 text: word,
735 l: wl,
736 t: page_h - wt,
737 r: wr,
738 b: page_h - wb,
739 });
740 }
741 cells
742}
743
744fn is_arabic(c: char) -> bool {
745 ('\u{0600}'..='\u{06FF}').contains(&c)
746}
747
748fn is_close_punct(c: char) -> bool {
749 matches!(
750 c,
751 ',' | '.' | ';' | '!' | '?' | ')' | ']' | '}' | '%' | '\'' | '\u{2019}' | '\u{2018}'
752 )
753}
754
755fn is_open_punct(c: char) -> bool {
756 // `@` glues to what follows (`mAP @0.5`, `bpf@zurich`, `@decorator`).
757 matches!(c, '(' | '[' | '{' | '@')
758}
759
760fn push_word(word: &mut String, words: &mut Vec<String>) {
761 if !word.is_empty() {
762 words.push(std::mem::take(word));
763 }
764}
765
766fn push_line(
767 words: &mut Vec<String>,
768 bbox: (f32, f32, f32, f32),
769 page_h: f32,
770 cells: &mut Vec<TextCell>,
771) {
772 if words.is_empty() {
773 return;
774 }
775 let text = std::mem::take(words).join(" ");
776 let (l, b, r, t) = bbox;
777 cells.push(TextCell {
778 text,
779 l,
780 t: page_h - t,
781 r,
782 b: page_h - b,
783 });
784}