use docling_core::{
CaptionParent, ListItemDclx, ListItemSplit, Node, PictureClass, PictureImage, Table,
};
#[cfg(any(feature = "ml", feature = "ocr-prep"))]
use image::RgbImage;
use crate::layout::Region;
use crate::pdfium_backend::{PdfPage, TextCell};
fn area(l: f32, t: f32, r: f32, b: f32) -> f32 {
((r - l).max(0.0)) * ((b - t).max(0.0))
}
fn inter(a: &Region, l: f32, t: f32, r: f32, b: f32) -> f32 {
let il = a.l.max(l);
let it = a.t.max(t);
let ir = a.r.min(r);
let ib = a.b.min(b);
area(il, it, ir, ib)
}
fn is_wrapper(label: &str) -> bool {
matches!(
label,
"table" | "document_index" | "form" | "key_value_region"
)
}
pub fn is_table_like(label: &str) -> bool {
matches!(label, "table" | "document_index")
}
fn greedy(mut regions: Vec<Region>) -> Vec<Region> {
regions.sort_by(|a, b| b.score.total_cmp(&a.score));
let mut kept: Vec<Region> = Vec::new();
for r in regions {
let ra = area(r.l, r.t, r.r, r.b).max(1.0);
let covered = kept.iter().any(|k| {
let i = inter(&r, k.l, k.t, k.r, k.b);
let ka = area(k.l, k.t, k.r, k.b).max(1.0);
i / ra > 0.7 || i / (ra + ka - i) > 0.5
});
if !covered {
kept.push(r);
}
}
kept
}
pub(crate) fn dedup_pictures(regions: &mut Vec<Region>) {
remove_overlapping_specials(regions, |l| l == "picture", 2.0, 0.3);
}
fn remove_overlapping_specials(
regions: &mut Vec<Region>,
in_bucket: impl Fn(&str) -> bool,
area_threshold: f32,
conf_threshold: f32,
) {
let idx: Vec<usize> = (0..regions.len())
.filter(|&i| in_bucket(regions[i].label))
.collect();
if idx.len() < 2 {
return;
}
let mut parent: Vec<usize> = (0..idx.len()).collect();
fn find(parent: &mut [usize], i: usize) -> usize {
let mut root = i;
while parent[root] != root {
root = parent[root];
}
let mut cur = i;
while parent[cur] != root {
let next = parent[cur];
parent[cur] = root;
cur = next;
}
root
}
let boxed = |r: &Region| (r.l, r.t, r.r, r.b);
for a in 0..idx.len() {
for b in (a + 1)..idx.len() {
let (ra, rb) = (®ions[idx[a]], ®ions[idx[b]]);
let (al, at, ar, ab_) = boxed(ra);
let (bl, bt, br, bb) = boxed(rb);
let ix = (ar.min(br) - al.max(bl)).max(0.0);
let iy = (ab_.min(bb) - at.max(bt)).max(0.0);
let inter = ix * iy;
let aa = area(al, at, ar, ab_).max(f32::EPSILON);
let ba = area(bl, bt, br, bb).max(f32::EPSILON);
let iou = inter / (aa + ba - inter).max(f32::EPSILON);
if iou > 0.8 || inter / aa > 0.8 || inter / ba > 0.8 {
let (pa, pb) = (find(&mut parent, a), find(&mut parent, b));
if pa != pb {
parent[pa] = pb;
}
}
}
}
let mut groups: std::collections::HashMap<usize, Vec<usize>> = std::collections::HashMap::new();
for i in 0..idx.len() {
let root = find(&mut parent, i);
groups.entry(root).or_default().push(i);
}
let mut drop = vec![false; regions.len()];
for group in groups.values() {
if group.len() < 2 {
continue;
}
let area_of = |i: usize| {
let r = ®ions[idx[i]];
area(r.l, r.t, r.r, r.b).max(f32::EPSILON)
};
let mut best: Option<usize> = None;
for &cand in group {
let passes = group.iter().all(|&other| {
if other == cand {
return true;
}
let area_ratio = area_of(cand) / area_of(other);
let conf_diff = regions[idx[other]].score - regions[idx[cand]].score;
!(area_ratio <= area_threshold && conf_diff > conf_threshold)
});
if passes {
best = Some(match best {
None => cand,
Some(cur) => {
if area_of(cand) > area_of(cur)
&& regions[idx[cur]].score - regions[idx[cand]].score <= conf_threshold
{
cand
} else {
cur
}
}
});
}
}
let keep = best.unwrap_or_else(|| {
*group
.iter()
.max_by(|&&a, &&b| regions[idx[a]].score.total_cmp(®ions[idx[b]].score))
.expect("non-empty group")
});
for &i in group {
if i != keep {
drop[idx[i]] = true;
}
}
}
let mut keep_iter = drop.into_iter();
regions.retain(|_| !keep_iter.next().expect("aligned"));
}
pub(crate) fn merge_overlapping_regulars(regions: &mut Vec<Region>) {
let idx: Vec<usize> = (0..regions.len())
.filter(|&i| regions[i].label != "picture" && !is_wrapper(regions[i].label))
.collect();
if idx.len() < 2 {
return;
}
let mut parent: Vec<usize> = (0..idx.len()).collect();
fn find(parent: &mut [usize], i: usize) -> usize {
let mut root = i;
while parent[root] != root {
root = parent[root];
}
let mut cur = i;
while parent[cur] != root {
let next = parent[cur];
parent[cur] = root;
cur = next;
}
root
}
for a in 0..idx.len() {
for b in (a + 1)..idx.len() {
let (ra, rb) = (®ions[idx[a]], ®ions[idx[b]]);
let i = inter(ra, rb.l, rb.t, rb.r, rb.b);
let aa = area(ra.l, ra.t, ra.r, ra.b).max(f32::EPSILON);
let ba = area(rb.l, rb.t, rb.r, rb.b).max(f32::EPSILON);
if i / (aa + ba - i).max(f32::EPSILON) > 0.8 || i / aa > 0.8 || i / ba > 0.8 {
let (pa, pb) = (find(&mut parent, a), find(&mut parent, b));
if pa != pb {
parent[pa] = pb;
}
}
}
}
let mut groups: std::collections::BTreeMap<usize, Vec<usize>> =
std::collections::BTreeMap::new();
for i in 0..idx.len() {
let root = find(&mut parent, i);
groups.entry(root).or_default().push(i);
}
const AREA_THRESHOLD: f32 = 1.3;
const CONF_THRESHOLD: f32 = 0.05;
let area_of = |i: usize| {
let r = ®ions[idx[i]];
area(r.l, r.t, r.r, r.b).max(f32::EPSILON)
};
let prefer = |cand: usize, other: usize| -> bool {
let (c, o) = (®ions[idx[cand]], ®ions[idx[other]]);
let area_ratio = area_of(cand) / area_of(other);
if c.label == "list_item" && o.label == "text" && (1.0 - area_ratio).abs() < 0.2 {
return true;
}
if c.label == "code" && inter(o, c.l, c.t, c.r, c.b) / area_of(other) > 0.8 {
return true;
}
!(area_ratio <= AREA_THRESHOLD && o.score - c.score > CONF_THRESHOLD)
};
let mut drop = vec![false; regions.len()];
let mut unions: Vec<(usize, (f32, f32, f32, f32))> = Vec::new();
for group in groups.values() {
if group.len() < 2 {
continue;
}
let mut best: Option<usize> = None;
for &cand in group {
if group
.iter()
.all(|&other| other == cand || prefer(cand, other))
{
best = Some(match best {
None => cand,
Some(cur)
if area_of(cand) > area_of(cur)
&& regions[idx[cur]].score - regions[idx[cand]].score
<= CONF_THRESHOLD =>
{
cand
}
Some(cur) => cur,
});
}
}
let keep = best.unwrap_or_else(|| {
*group
.iter()
.max_by(|&&a, &&b| regions[idx[a]].score.total_cmp(®ions[idx[b]].score))
.expect("non-empty group")
});
let mut u = (
f32::INFINITY,
f32::INFINITY,
f32::NEG_INFINITY,
f32::NEG_INFINITY,
);
for &i in group {
let r = ®ions[idx[i]];
u = (u.0.min(r.l), u.1.min(r.t), u.2.max(r.r), u.3.max(r.b));
if i != keep {
drop[idx[i]] = true;
}
}
unions.push((idx[keep], u));
}
for (i, (l, t, r, b)) in unions {
let k = &mut regions[i];
(k.l, k.t, k.r, k.b) = (l, t, r, b);
}
let mut keep_iter = drop.into_iter();
regions.retain(|_| !keep_iter.next().expect("aligned"));
}
fn iou(a: &Region, b: &Region) -> f32 {
let i = inter(a, b.l, b.t, b.r, b.b);
let u = area(a.l, a.t, a.r, a.b) + area(b.l, b.t, b.r, b.b) - i;
if u > 0.0 {
i / u
} else {
0.0
}
}
fn coincident_losers(regions: &[Region], losers: &[usize], winners: &[usize]) -> Vec<usize> {
let mut out = Vec::new();
for &li in losers {
for &wi in winners {
if iou(®ions[li], ®ions[wi]) > 0.8 && regions[li].score - regions[wi].score < 0.1
{
out.push(li);
break;
}
}
}
out
}
fn handle_cross_type_overlaps(regions: Vec<Region>) -> Vec<Region> {
let by = |pred: &dyn Fn(&str) -> bool| -> Vec<usize> {
(0..regions.len())
.filter(|&i| pred(regions[i].label))
.collect()
};
let tables = by(&|l| l == "table");
let doc_indices = by(&|l| l == "document_index");
let pictures = by(&|l| l == "picture");
let containers = by(&|l| matches!(l, "form" | "key_value_region"));
let mut drop = vec![false; regions.len()];
for i in coincident_losers(®ions, &tables, &doc_indices) {
drop[i] = true;
}
let table_like: Vec<usize> = tables.iter().chain(&doc_indices).copied().collect();
for i in coincident_losers(®ions, &pictures, &table_like) {
drop[i] = true;
}
let structured: Vec<usize> = table_like
.iter()
.chain(&pictures)
.copied()
.filter(|&i| !drop[i])
.collect();
for i in coincident_losers(®ions, &containers, &structured) {
drop[i] = true;
}
let mut drop = drop.into_iter();
let mut regions = regions;
regions.retain(|_| !drop.next().expect("aligned"));
regions
}
pub fn resolve(regions: Vec<Region>) -> Vec<Region> {
let regions = handle_cross_type_overlaps(regions);
let pictures = greedy(
regions
.iter()
.filter(|r| r.label == "picture")
.cloned()
.collect(),
);
let mut tables = greedy(
regions
.iter()
.filter(|r| is_table_like(r.label))
.cloned()
.collect(),
);
remove_overlapping_specials(&mut tables, |_| true, 2.0, 0.2);
let containers = greedy(
regions
.iter()
.filter(|r| matches!(r.label, "form" | "key_value_region"))
.cloned()
.collect(),
);
let mut kept = greedy(
regions
.iter()
.filter(|r| r.label != "picture" && !is_wrapper(r.label))
.cloned()
.collect(),
);
dedup_nested_code(&mut kept);
kept.extend(pictures);
kept.extend(tables);
kept.extend(containers);
kept
}
pub fn reclaim_heading_body_footers(regions: &mut [Region], page_w: f32) {
let n = regions.len();
let overlap_x = |a: &Region, b: &Region| a.r.min(b.r) - a.l.max(b.l) > 0.0;
for fi in 0..n {
let f = regions[fi].clone();
if f.label != "page_footer" || f.r - f.l < 0.4 * page_w {
continue;
}
let fh = (f.b - f.t).max(1.0);
let heading = (0..n)
.filter(|&j| {
let h = ®ions[j];
j != fi && h.label == "section_header" && h.b <= f.t + 0.5 * fh && overlap_x(h, &f)
})
.min_by(|&a, &b| regions[b].b.total_cmp(®ions[a].b));
let Some(hi) = heading else {
continue;
};
let h = regions[hi].clone();
if f.t - h.b > 2.5 * fh {
continue;
}
let has_body = (0..n).any(|j| {
let r = ®ions[j];
j != fi
&& j != hi
&& !matches!(r.label, "page_footer" | "page_header")
&& r.t >= h.b - 0.5 * fh
&& (overlap_x(r, &h) || overlap_x(r, &f))
});
if has_body {
continue;
}
regions[fi].label = "text";
}
}
pub fn drop_contained_regulars(regions: &mut Vec<Region>) {
let specials: Vec<(f32, f32, f32, f32)> = regions
.iter()
.filter(|r| is_table_like(r.label))
.map(|r| (r.l, r.t, r.r, r.b))
.collect();
if specials.is_empty() {
return;
}
regions.retain(|r| {
if r.label == "picture" || is_wrapper(r.label) {
return true;
}
let ra = area(r.l, r.t, r.r, r.b).max(1.0);
!specials
.iter()
.any(|&(l, t, rr, b)| inter(r, l, t, rr, b) / ra > 0.8)
});
}
pub fn picture_parents(regions: &[Region]) -> Vec<Option<usize>> {
regions
.iter()
.map(|r| {
if !claims_cells(r) {
return None;
}
let ra = area(r.l, r.t, r.r, r.b).max(1.0);
regions
.iter()
.enumerate()
.filter(|(_, p)| p.label == "picture" && inter(r, p.l, p.t, p.r, p.b) / ra > 0.8)
.min_by(|(_, a), (_, b)| {
area(a.l, a.t, a.r, a.b).total_cmp(&area(b.l, b.t, b.r, b.b))
})
.map(|(i, _)| i)
})
.collect()
}
fn is_code_language(t: &str) -> bool {
let t = t.trim();
if t.is_empty() || t.chars().any(char::is_whitespace) || t.chars().count() > 12 {
return false;
}
const LANGS: &[&str] = &[
"xml",
"html",
"xhtml",
"json",
"jsonc",
"yaml",
"yml",
"toml",
"ini",
"c#",
"csharp",
"f#",
"fsharp",
"vb",
"c",
"c++",
"cpp",
"java",
"kotlin",
"scala",
"go",
"golang",
"rust",
"swift",
"javascript",
"js",
"typescript",
"ts",
"jsx",
"tsx",
"python",
"py",
"ruby",
"rb",
"php",
"perl",
"lua",
"r",
"dart",
"bash",
"sh",
"shell",
"powershell",
"zsh",
"batch",
"cmd",
"sql",
"tsql",
"plsql",
"graphql",
"dockerfile",
"makefile",
"css",
"scss",
"sass",
"less",
"markdown",
"md",
"tex",
"latex",
"diff",
"proto",
"razor",
"cshtml",
"xaml",
"aspx",
"http",
];
let lower = t.to_ascii_lowercase();
LANGS.contains(&lower.as_str())
}
fn code_language_labels(regions: &[Region], cells: &[TextCell]) -> Vec<bool> {
let mut drop = vec![false; regions.len()];
for (i, r) in regions.iter().enumerate() {
if matches!(r.label, "code" | "picture" | "table") {
continue;
}
if !is_code_language(®ion_text(r, cells)) {
continue;
}
let line_h = (r.b - r.t).abs().max(1.0);
let window = (line_h * 4.0).max(28.0);
let labels_code = regions.iter().enumerate().any(|(j, c)| {
if j == i || c.label != "code" {
return false;
}
let gap = c.t - r.b; let h_overlap = (r.r.min(c.r) - r.l.max(c.l)).max(0.0);
gap > -line_h * 3.0 && gap < window && h_overlap > 0.0
});
if labels_code {
drop[i] = true;
}
}
drop
}
fn dedup_nested_code(kept: &mut Vec<Region>) {
let mut drop = vec![false; kept.len()];
for i in 0..kept.len() {
if kept[i].label != "code" {
continue;
}
let ai = area(kept[i].l, kept[i].t, kept[i].r, kept[i].b).max(1.0);
for j in 0..kept.len() {
if i == j || drop[j] || kept[j].label != "code" {
continue;
}
let aj = area(kept[j].l, kept[j].t, kept[j].r, kept[j].b).max(1.0);
let overlap = inter(&kept[i], kept[j].l, kept[j].t, kept[j].r, kept[j].b);
if aj > ai && overlap / ai > 0.7 {
drop[i] = true;
break;
}
}
}
let mut keep = drop.iter();
kept.retain(|_| !*keep.next().unwrap());
}
pub fn layout_cell_coverage(regions: &[Region], cells: &[TextCell]) -> f32 {
let mut total = 0usize;
let mut covered = 0usize;
for c in cells {
if c.text.trim().is_empty() {
continue;
}
total += 1;
let ca = area(c.l, c.t, c.r, c.b).max(1.0);
if regions
.iter()
.any(|r| inter(r, c.l, c.t, c.r, c.b) / ca > 0.2)
{
covered += 1;
}
}
if total == 0 {
1.0
} else {
covered as f32 / total as f32
}
}
fn looks_like_note_call(text: &str) -> bool {
let t = text.trim();
let n = t.chars().count();
if n == 0 || n > 16 {
return false;
}
let mark = |c: char| c.is_ascii_digit() || "*†‡§¶#,⁰¹²³⁴⁵⁶⁷⁸⁹".contains(c);
(t.chars().all(mark) && t.chars().any(|c| c != ','))
|| matches!(
(t.chars().next(), t.chars().last()),
(Some('('), Some(')')) | (Some('['), Some(']'))
)
}
fn is_note_of(note: &TextCell, host: &TextCell) -> bool {
if !looks_like_note_call(¬e.text) {
return false;
}
let (nt, nb) = (note.t.min(note.b), note.t.max(note.b));
let (ht, hb) = (host.t.min(host.b), host.t.max(host.b));
let (nh, hh) = ((nb - nt).max(1.0), (hb - ht).max(1.0));
nh <= 0.8 * hh
&& nb.min(hb) - nt.max(ht) >= 0.5 * nh
&& note.l >= host.r - 0.5 * hh
&& note.l <= host.r + hh
}
fn place_raised_notes(sorted: Vec<&TextCell>) -> (Vec<&TextCell>, Vec<bool>) {
let n = sorted.len();
let mut out: Vec<&TextCell> = Vec::with_capacity(n);
let mut flags: Vec<bool> = Vec::with_capacity(n);
let mut held: Vec<&TextCell> = Vec::new();
for (i, &c) in sorted.iter().enumerate() {
let mut k = 0;
while k < held.len() {
if c.t >= held[k].t.max(held[k].b) {
out.push(held.remove(k));
flags.push(false);
} else {
k += 1;
}
}
let waits = sorted[i + 1..]
.iter()
.take_while(|h| h.t < c.t.max(c.b))
.any(|h| is_note_of(c, h));
if waits {
held.push(c);
continue;
}
out.push(c);
flags.push(false);
let mut k = 0;
while k < held.len() {
if is_note_of(held[k], c) {
out.push(held.remove(k));
flags.push(true);
} else {
k += 1;
}
}
}
for c in held {
out.push(c);
flags.push(false);
}
(out, flags)
}
pub fn add_orphan_regions(regions: &mut Vec<Region>, cells: &[TextCell]) {
let assigned = |c: &TextCell| {
let ca = area(c.l, c.t, c.r, c.b).max(1.0);
regions
.iter()
.filter(|r| r.label != "picture" && !is_wrapper(r.label))
.any(|r| inter(r, c.l, c.t, c.r, c.b) / ca > 0.2)
};
let mut orphans: Vec<&TextCell> = cells
.iter()
.filter(|c| !c.text.trim().is_empty() && !assigned(c))
.collect();
if orphans.is_empty() {
return;
}
orphans.sort_by(|a, b| a.t.total_cmp(&b.t).then(a.l.total_cmp(&b.l)));
let (orphans, notes) = place_raised_notes(orphans);
let mut merged: Vec<Region> = Vec::new();
for (k, c) in orphans.into_iter().enumerate() {
let mut h = (c.b - c.t).abs().max(1.0);
if let Some(last) = merged.last_mut() {
if notes[k] {
h = h.max((last.b - last.t).abs());
}
let same_line = (last.t - c.t).abs() < h * 0.5;
let touching = c.l <= last.r + h && c.l >= last.l - h;
let lh = (last.b - last.t).abs().max(1.0);
let comparable = h <= 4.0 * lh && lh <= 4.0 * h;
if same_line && touching && comparable {
last.l = last.l.min(c.l);
last.r = last.r.max(c.r);
last.t = last.t.min(c.t);
last.b = last.b.max(c.b);
continue;
}
}
merged.push(Region {
label: "text",
score: 0.0,
l: c.l,
t: c.t,
r: c.r,
b: c.b,
});
}
regions.extend(merged);
}
pub fn recover_text_panels(regions: &mut Vec<Region>, cells: &[TextCell]) {
let captioned: Vec<bool> = regions
.iter()
.map(|r| {
r.label == "picture"
&& regions.iter().any(|c| {
c.label == "caption" && c.r.min(r.r) - c.l.max(r.l) > 0.0 && {
let gap = if c.t >= r.b {
c.t - r.b
} else if r.t >= c.b {
r.t - c.b
} else {
f32::MAX };
gap <= 25.0
}
})
})
.collect();
let mut out: Vec<Region> = Vec::with_capacity(regions.len());
let mut demoted_paras: Vec<Region> = Vec::new();
let mut demoted_boxes: Vec<(f32, f32, f32, f32)> = Vec::new();
for (i, r) in regions.drain(..).enumerate() {
if r.label != "picture" || captioned[i] {
out.push(r);
continue;
}
let inside: Vec<&TextCell> = cells
.iter()
.filter(|c| {
!c.text.trim().is_empty() && {
let ca = area(c.l, c.t, c.r, c.b).max(1.0);
inter(&r, c.l, c.t, c.r, c.b) / ca > 0.5
}
})
.collect();
let mut lines: Vec<(f32, f32, f32, f32)> = Vec::new(); for c in &inside {
let (ct, cb) = (c.t.min(c.b), c.t.max(c.b));
match lines.iter_mut().find(|(lt, lb, _, _)| {
let ov = cb.min(*lb) - ct.max(*lt);
ov > 0.5 * (cb - ct).min(*lb - *lt).max(1.0)
}) {
Some((lt, lb, ll, lr)) => {
*lt = lt.min(ct);
*lb = lb.max(cb);
*ll = ll.min(c.l);
*lr = lr.max(c.r);
}
None => lines.push((ct, cb, c.l, c.r)),
}
}
if lines.len() < 3 {
out.push(r);
continue;
}
let panel_w = (r.r - r.l).max(1.0);
let coverage = inside.iter().map(|c| area(c.l, c.t, c.r, c.b)).sum::<f32>()
/ area(r.l, r.t, r.r, r.b).max(1.0);
let mut widths: Vec<f32> = lines.iter().map(|(_, _, l, rr)| rr - l).collect();
widths.sort_by(f32::total_cmp);
let mut heights: Vec<f32> = lines.iter().map(|(t, b, _, _)| b - t).collect();
heights.sort_by(f32::total_cmp);
let h_med = heights[heights.len() / 2].max(1.0);
let mut devs: Vec<f32> = heights.iter().map(|h| (h - h_med).abs()).collect();
devs.sort_by(f32::total_cmp);
let uniform = devs[devs.len() / 2] <= 0.35 * h_med;
let text_panel = coverage >= 0.2 && widths[widths.len() / 2] >= 0.45 * panel_w && uniform;
if !text_panel {
out.push(r);
continue;
}
lines.sort_by(|a, b| a.0.total_cmp(&b.0));
let mut heights: Vec<f32> = lines.iter().map(|(t, b, _, _)| b - t).collect();
heights.sort_by(f32::total_cmp);
let h = heights[heights.len() / 2].max(1.0);
let mut gaps: Vec<f32> = lines
.windows(2)
.map(|w| (w[1].0 - w[0].1).max(0.0))
.collect();
gaps.sort_by(f32::total_cmp);
let leading = if gaps.is_empty() {
0.0
} else {
gaps[gaps.len() / 2]
};
let brk = (1.8 * leading).max(0.75 * h);
let mut para: Option<(f32, f32, f32, f32)> = None; for (t, b, l, rr) in &lines {
match &mut para {
Some((pl, _, pr, pb)) if *t - *pb <= brk => {
*pl = pl.min(*l);
*pr = pr.max(*rr);
*pb = pb.max(*b);
}
_ => {
if let Some((pl, pt, pr, pb)) = para.take() {
demoted_paras.push(Region {
label: "text",
score: r.score,
l: pl,
t: pt,
r: pr,
b: pb,
});
}
para = Some((*l, *t, *rr, *b));
}
}
}
if let Some((pl, pt, pr, pb)) = para {
demoted_paras.push(Region {
label: "text",
score: r.score,
l: pl,
t: pt,
r: pr,
b: pb,
});
}
demoted_boxes.push((r.l, r.t, r.r, r.b));
}
if !demoted_boxes.is_empty() {
out.retain(|r| {
r.label == "picture" || is_wrapper(r.label) || {
let ra = area(r.l, r.t, r.r, r.b).max(1.0);
!demoted_boxes
.iter()
.any(|&(l, t, rr, b)| inter(r, l, t, rr, b) / ra > 0.5)
}
});
}
demoted_paras.retain(|p| {
let pa = area(p.l, p.t, p.r, p.b).max(1.0);
!out.iter()
.any(|s| is_table_like(s.label) && inter(p, s.l, s.t, s.r, s.b) / pa > 0.8)
});
out.extend(demoted_paras);
*regions = out;
}
pub fn drop_full_page_pictures(regions: &mut Vec<Region>, page_w: f32, page_h: f32) {
let page_area = (page_w * page_h).max(1.0);
regions.retain(|r| r.label != "picture" || area(r.l, r.t, r.r, r.b) / page_area <= 0.90);
}
pub fn drop_false_pictures(
regions: &mut Vec<Region>,
cells: &[TextCell],
page_w: f32,
page_h: f32,
) {
if cells.iter().all(|c| c.text.trim().is_empty()) {
return; }
let content_regions = regions
.iter()
.filter(|r| r.label != "picture" && !region_text(r, cells).trim().is_empty())
.count();
if content_regions < 2 {
return;
}
let page_area = (page_w * page_h).max(1.0);
regions.retain(|r| {
if r.label != "picture" || r.score >= 0.5 {
return true;
}
if area(r.l, r.t, r.r, r.b) / page_area >= 0.25 {
return true; }
cells.iter().any(|c| {
let ca = area(c.l, c.t, c.r, c.b).max(1.0);
!c.text.trim().is_empty() && inter(r, c.l, c.t, c.r, c.b) / ca > 0.5
})
});
}
fn is_page_number(region: &Region, cells: &[TextCell], page_h: f32) -> bool {
let t = region_text(region, cells);
let t = t.trim();
!t.is_empty()
&& t.chars().all(|c| c.is_ascii_digit())
&& (region.b - region.t).abs() < 30.0
&& (region.t < page_h * 0.12 || region.b > page_h * 0.88)
}
fn order_with_containers<T: Clone>(
items: &mut Vec<T>,
cids: &[usize],
page_w: f32,
page_h: f32,
separators: &[crate::reading_order::Separator],
reg: impl Fn(&T) -> &Region,
) {
let is_container = |r: &Region| matches!(r.label, "form" | "key_value_region");
let containers: Vec<usize> = (0..items.len())
.filter(|&i| is_container(reg(&items[i])))
.collect();
if containers.is_empty() {
order_regions(items, cids, page_w, page_h, separators, reg);
return;
}
let mut parent: Vec<Option<usize>> = vec![None; items.len()];
for i in 0..items.len() {
let r = reg(&items[i]);
if is_container(r) {
continue;
}
let ra = area(r.l, r.t, r.r, r.b).max(1.0);
let mut best: Option<(usize, f32, f32)> = None; for &c in &containers {
let cr = reg(&items[c]);
if inter(r, cr.l, cr.t, cr.r, cr.b) / ra > 0.8 {
let key = (area(cr.l, cr.t, cr.r, cr.b), -cr.score);
if best.is_none_or(|(_, a, s)| key.0 < a || (key.0 == a && key.1 < s)) {
best = Some((c, key.0, key.1));
}
}
}
parent[i] = best.map(|(c, _, _)| c);
}
let mut top: Vec<(usize, Region)> = Vec::new();
for i in 0..items.len() {
if parent[i].is_some() {
continue;
}
let mut r = reg(&items[i]).clone();
if is_container(&r) {
let kids: Vec<&Region> = (0..items.len())
.filter(|&k| parent[k] == Some(i))
.map(|k| reg(&items[k]))
.collect();
if !kids.is_empty() {
r.l = kids.iter().map(|k| k.l).fold(f32::INFINITY, f32::min);
r.t = kids.iter().map(|k| k.t).fold(f32::INFINITY, f32::min);
r.r = kids.iter().map(|k| k.r).fold(f32::NEG_INFINITY, f32::max);
r.b = kids.iter().map(|k| k.b).fold(f32::NEG_INFINITY, f32::max);
}
}
top.push((i, r));
}
let top_cids: Vec<usize> = top.iter().map(|(i, _)| cids[*i]).collect();
order_regions(&mut top, &top_cids, page_w, page_h, separators, |it| &it.1);
let mut out: Vec<T> = Vec::with_capacity(items.len());
for (i, _) in top {
if is_container(reg(&items[i])) {
let kid_idx: Vec<usize> = (0..items.len()).filter(|&k| parent[k] == Some(i)).collect();
let mut kids: Vec<T> = kid_idx.iter().map(|&k| items[k].clone()).collect();
let kid_cids: Vec<usize> = kid_idx.iter().map(|&k| cids[k]).collect();
order_regions(&mut kids, &kid_cids, page_w, page_h, &[], ®);
out.push(items[i].clone());
out.extend(kids);
} else {
out.push(items[i].clone());
}
}
*items = out;
}
fn is_skipped(label: &str) -> bool {
matches!(
label,
"page_header" | "page_footer" | "form" | "key_value_region"
)
}
fn order_regions<T: Clone>(
items: &mut Vec<T>,
cids: &[usize],
page_w: f32,
page_h: f32,
separators: &[crate::reading_order::Separator],
reg: impl Fn(&T) -> &Region,
) {
let boxes: Vec<(f32, f32, f32, f32)> = items
.iter()
.map(|it| {
let r = reg(it);
(r.l, r.t, r.r, r.b)
})
.collect();
let is_header: Vec<bool> = items
.iter()
.map(|it| reg(it).label == "page_header")
.collect();
let is_footer: Vec<bool> = items
.iter()
.map(|it| reg(it).label == "page_footer")
.collect();
let is_graphic: Vec<bool> = items
.iter()
.map(|it| matches!(reg(it).label, "table" | "picture" | "code"))
.collect();
let order = crate::reading_order::order_page_with_separators(
&boxes,
cids,
&is_header,
&is_footer,
&is_graphic,
page_w,
page_h,
separators,
);
*items = order.iter().map(|&i| items[i].clone()).collect();
}
fn page_separators(page: &PdfPage, regions: &[Region]) -> Vec<crate::reading_order::Separator> {
static ENABLED: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
let enabled = *ENABLED.get_or_init(|| {
docling_core::env::nonempty("DOCLING_RS_PDF_ORDER_SEPARATORS").is_none()
|| docling_core::env::flag("DOCLING_RS_PDF_ORDER_SEPARATORS")
});
let shapes = &page.shapes;
if !enabled || (shapes.lines.is_empty() && shapes.boxes.is_empty()) {
return Vec::new();
}
let h = page.height;
let bl = |[l, t, r, b]: [f32; 4]| [l, h - b, r, h - t];
let texts = region_texts_exclusive(regions, &page.cells);
let elements: Vec<crate::reading_order::SeparatorContext> = regions
.iter()
.zip(&texts)
.map(|(r, text)| {
let graphic = matches!(r.label, "table" | "picture" | "code");
let container = matches!(r.label, "form" | "key_value_region");
crate::reading_order::SeparatorContext {
l: r.l,
b: h - r.b,
r: r.r,
t: h - r.t,
has_text: !graphic && !container && !text.trim().is_empty(),
graphic,
}
})
.collect();
let lines: Vec<[f32; 4]> = shapes.lines.iter().map(|&b| bl(b)).collect();
let boxes: Vec<[f32; 4]> = shapes.boxes.iter().map(|&b| bl(b)).collect();
crate::reading_order::build_separators(page.width, h, &elements, &lines, &boxes)
}
pub fn cluster_cids(regions: &[Region], cells: &[TextCell]) -> Vec<usize> {
let owned = assign_cells(regions, cells);
let first_cell: Vec<usize> = regions
.iter()
.enumerate()
.map(|(i, r)| {
if claims_cells(r) {
return owned[i].iter().copied().min().unwrap_or(usize::MAX);
}
let interior = cells
.iter()
.enumerate()
.filter(|(_, c)| {
!c.text.trim().is_empty()
&& inter(r, c.l, c.t, c.r, c.b) / area(c.l, c.t, c.r, c.b).max(1.0) > 0.8
})
.map(|(ci, _)| ci)
.min();
let children = regions
.iter()
.enumerate()
.filter(|(j, child)| {
*j != i && claims_cells(child) && {
let ca = area(child.l, child.t, child.r, child.b).max(1.0);
inter(r, child.l, child.t, child.r, child.b) / ca > 0.8
}
})
.filter_map(|(j, _)| owned[j].iter().copied().min())
.min();
interior
.into_iter()
.chain(children)
.min()
.unwrap_or(usize::MAX)
})
.collect();
let mut by_source: Vec<usize> = (0..regions.len()).collect();
by_source.sort_by(|&a, &b| {
first_cell[a]
.cmp(&first_cell[b])
.then(regions[a].t.total_cmp(®ions[b].t))
.then(regions[a].l.total_cmp(®ions[b].l))
});
let mut cids = vec![0; regions.len()];
for (rank, &i) in by_source.iter().enumerate() {
cids[i] = rank;
}
cids
}
const LIST_BULLET_MARKERS: &str = "\u{2022}\u{2023}\u{25E6}\u{2043}\u{204C}\u{204D}\u{2219}\u{25AA}\u{25AB}\u{25CF}\u{25CB}-*+•·‣⁃►▶▸➤➢✓✔✗✘\u{F0B7}\u{F0A7}\u{F06E}\u{F076}\u{F0D8}\u{F0E0}\u{F0FC}\u{F0A8}";
const PUA_FONT_BULLETS: [char; 8] = [
'\u{F0B7}', '\u{F0A7}', '\u{F06E}', '\u{F076}', '\u{F0D8}', '\u{F0E0}', '\u{F0FC}', '\u{F0A8}',
];
const LIST_NUMBERED_MARKERS: &[fn(&str) -> Vec<usize>] = &[
|s| {
let mut i = digits(s, 0);
if i == 0 {
return Vec::new();
}
let mut groups = 0;
while s[i..].starts_with('.') && digits(s, i + 1) > i + 1 {
i = digits(s, i + 1);
groups += 1;
}
if groups == 0 {
return Vec::new();
}
if s[i..].starts_with('.') {
vec![i + 1, i]
} else {
vec![i]
}
},
|s| digits_dot_letter(s, 0, '.').into_iter().collect(),
|s| digits_dot_letter(s, 0, ')').into_iter().collect(),
|s| {
if !s.starts_with('(') {
return Vec::new();
}
digits_dot_letter(s, 1, ')').into_iter().collect()
},
|s| digits_then(s, 0, '.').into_iter().collect(),
|s| digits_then(s, 0, ')').into_iter().collect(),
|s| {
if !s.starts_with('(') {
return Vec::new();
}
digits_then(s, 1, ')').into_iter().collect()
},
|s| {
if !s.starts_with('[') {
return Vec::new();
}
digits_then(s, 1, ']').into_iter().collect()
},
|s| class_run_then(s, "ivxlcdm", '.').into_iter().collect(),
|s| class_run_then(s, "IVXLCDM", '.').into_iter().collect(),
|s| {
letter_then(s, char::is_ascii_lowercase, '.')
.into_iter()
.collect()
},
|s| {
letter_then(s, char::is_ascii_uppercase, '.')
.into_iter()
.collect()
},
|s| {
letter_then(s, char::is_ascii_lowercase, ')')
.into_iter()
.collect()
},
|s| {
letter_then(s, char::is_ascii_uppercase, ')')
.into_iter()
.collect()
},
];
fn digits(s: &str, from: usize) -> usize {
s[from..]
.char_indices()
.find(|(_, c)| !c.is_numeric())
.map_or(s.len(), |(i, _)| from + i)
}
fn digits_then(s: &str, from: usize, close: char) -> Option<usize> {
let end = digits(s, from);
(end > from && s[end..].starts_with(close)).then(|| end + close.len_utf8())
}
fn digits_dot_letter(s: &str, from: usize, close: char) -> Option<usize> {
let mut i = digits(s, from);
if i == from {
return None;
}
if s[i..].starts_with('.') {
i += 1;
}
let letter = s[i..].chars().next().filter(char::is_ascii_alphabetic)?;
i += letter.len_utf8();
s[i..].starts_with(close).then(|| i + close.len_utf8())
}
fn class_run_then(s: &str, class: &str, close: char) -> Option<usize> {
let end = s
.char_indices()
.find(|(_, c)| !class.contains(*c))
.map_or(s.len(), |(i, _)| i);
(end > 0 && s[end..].starts_with(close)).then(|| end + close.len_utf8())
}
fn letter_then(s: &str, class: fn(&char) -> bool, close: char) -> Option<usize> {
let letter = s.chars().next().filter(class)?;
let i = letter.len_utf8();
s[i..].starts_with(close).then(|| i + close.len_utf8())
}
fn split_list_marker(text: &str) -> Option<(&str, &str, bool)> {
let tail_after = |len: usize| -> Option<&str> {
let ws = text[len..].chars().next()?;
if !ws.is_whitespace() {
return None;
}
let rest = &text[len + ws.len_utf8()..];
(!rest.is_empty()).then_some(rest)
};
let first = text.chars().next()?;
if LIST_BULLET_MARKERS.contains(first) {
if let Some(rest) = tail_after(first.len_utf8()) {
return Some((&text[..first.len_utf8()], rest, false));
}
}
for matcher in LIST_NUMBERED_MARKERS {
for len in matcher(text) {
if let Some(rest) = tail_after(len) {
return Some((&text[..len], rest, true));
}
}
}
None
}
fn list_item_node(text: &str, loc: [u16; 4], first_in_list: bool) -> Node {
let stripped = text
.trim_start_matches(|c| {
matches!(c, '•' | '◦' | '▪' | '·' | '*') || PUA_FONT_BULLETS.contains(&c)
})
.trim_start();
let split = split_list_marker(text).or_else(|| split_list_marker(stripped));
let json = |orig: &str, marker: &str, body: &str, enumerated: bool| {
Some(Box::new(ListItemSplit {
orig: orig.to_string(),
marker: marker.to_string(),
text: body.to_string(),
enumerated,
}))
};
let matched = if split_list_marker(text).is_some() {
text
} else {
stripped
};
let bullet = |text: String, marker: &str, split| Node::ListItem {
ordered: false,
number: 0,
first_in_list,
text: md_escape(&text),
level: 0,
marker: Some(marker.to_string()),
location: Some(loc),
split,
dclx: None,
href: None,
layer: None,
};
let is_number_dot = |m: &str| {
m.strip_suffix('.')
.is_some_and(|d| !d.is_empty() && d.chars().all(char::is_numeric))
};
match split {
Some((marker, body, true)) if is_number_dot(marker) => {
let number = parse_ordered_marker(marker).map_or(0, |(n, _)| n);
Node::ListItem {
ordered: true,
number,
first_in_list,
text: md_escape(body),
level: 0,
marker: Some(marker.to_string()),
location: Some(loc),
split: json(matched, marker, body, true),
dclx: None,
href: None,
layer: None,
}
}
Some((marker, body, true)) => {
let text = md_escape(&format!("{marker} {body}"));
Node::ListItem {
ordered: false,
number: 0,
first_in_list,
text: text.clone(),
level: 0,
marker: None,
location: Some(loc),
split: json(matched, marker, body, true),
dclx: Some(ListItemDclx {
ordered: false,
marker: Some(marker.to_string()),
text,
runs: Vec::new(),
}),
href: None,
layer: None,
}
}
Some((marker, body, false)) => {
bullet(body.to_string(), marker, json(matched, marker, body, false))
}
None => bullet(
stripped.to_string(),
"·",
json(stripped, "", stripped, false),
),
}
}
fn parse_ordered_marker(s: &str) -> Option<(u64, String)> {
let digits: String = s.chars().take_while(|c| c.is_ascii_digit()).collect();
if digits.is_empty() {
return None;
}
let rest = s[digits.len()..].strip_prefix('.')?;
let number = digits.parse().ok()?;
Some((number, rest.trim_start().to_string()))
}
pub(crate) fn md_escape(text: &str) -> String {
text.replace('_', "\\_")
.replace('&', "&")
.replace('<', "<")
.replace('>', ">")
}
fn clean_text(text: &str) -> String {
let mut out = clean_text_keep_wrap(text);
if out.ends_with('\u{ad}') {
out.pop();
}
out
}
fn clean_text_keep_wrap(text: &str) -> String {
let wrap = text.trim_end().ends_with('\u{ad}');
let unwrapped = text
.replace("\u{2} ", "")
.replace("\u{ad} ", "")
.replace(['\u{2}', '\u{ad}'], "");
let replaced = if KEEP_TYPOGRAPHY.with(std::cell::Cell::get) {
unwrapped
} else {
fold_typography(&unwrapped)
};
let out = replaced.replace(['\n', '\r', '\t'], " ").trim().to_string();
let out = fold_presentation_forms(&out);
let mut out = fix_arabic_lam_alef(&out);
if wrap && !out.is_empty() {
out.push('\u{ad}');
}
out
}
thread_local! {
static KEEP_TYPOGRAPHY: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
struct TypographyScope(bool);
impl TypographyScope {
fn set(keep: bool) -> Self {
Self(KEEP_TYPOGRAPHY.with(|k| k.replace(keep)))
}
}
impl Drop for TypographyScope {
fn drop(&mut self) {
KEEP_TYPOGRAPHY.with(|k| k.set(self.0));
}
}
pub fn keep_typography_from_env() -> bool {
docling_core::env::flag("DOCLING_RS_PDF_KEEP_TYPOGRAPHY")
}
fn fold_typography(text: &str) -> String {
text.replace(['\u{2018}', '\u{2019}', '\u{201b}'], "'") .replace(['\u{201c}', '\u{201d}', '\u{201e}', '\u{201f}'], "\"") .replace('\u{201a}', ",") .replace(
[
'\u{2010}', '\u{2011}', '\u{2012}', '\u{2013}', '\u{2014}', '\u{2015}', '\u{2212}',
],
"-",
) .replace('\u{2044}', "/") .replace('\u{2022}', "\u{b7}") .replace('\u{2026}', "...") }
pub(crate) fn fold_presentation_forms(text: &str) -> String {
use unicode_normalization::UnicodeNormalization;
let is_arabic_form = |c: char| matches!(c, '\u{FB50}'..='\u{FDFF}' | '\u{FE70}'..='\u{FEFF}');
let needs = |c: char| {
matches!(
c,
'\u{FB00}'..='\u{FB06}' | '\u{132}' | '\u{133}' | '\u{F0A0}'
) || is_arabic_form(c)
};
if !text.chars().any(needs) {
return text.to_string();
}
let mut out = String::with_capacity(text.len());
let mut run = String::new();
for c in text.chars() {
if is_arabic_form(c) {
run.push(c);
continue;
}
if !run.is_empty() {
out.extend(run.nfkc());
run.clear();
}
match c {
'\u{FB00}' => out.push_str("ff"),
'\u{FB01}' => out.push_str("fi"),
'\u{FB02}' => out.push_str("fl"),
'\u{FB03}' => out.push_str("ffi"),
'\u{FB04}' => out.push_str("ffl"),
'\u{FB05}' | '\u{FB06}' => out.push_str("st"),
'\u{132}' => out.push_str("IJ"),
'\u{133}' => out.push_str("ij"),
'\u{F0A0}' => {}
_ => out.push(c),
}
}
if !run.is_empty() {
out.extend(run.nfkc());
}
out
}
fn normalize_table_text(text: &str) -> String {
let text = if KEEP_TYPOGRAPHY.with(std::cell::Cell::get) {
text.to_string()
} else {
fold_typography(text)
};
fold_presentation_forms(&text)
}
fn normalize_grid(mut grid: TableGrid) -> TableGrid {
for row in &mut grid.rows {
for cell in row.iter_mut() {
*cell = normalize_table_text(cell);
}
}
for cell in &mut grid.cells {
cell.text = normalize_table_text(&cell.text);
}
grid
}
fn fix_arabic_lam_alef(s: &str) -> String {
let is_arabic_letter = |c: char| ('\u{0620}'..='\u{064A}').contains(&c);
let chars: Vec<char> = s.chars().collect();
if !chars.iter().any(|&c| is_arabic_letter(c)) {
return s.to_string(); }
let mut a: Vec<char> = Vec::with_capacity(chars.len());
let mut i = 0;
while i < chars.len() {
let c = chars[i];
if matches!(c, '\u{0622}' | '\u{0623}' | '\u{0625}')
&& chars.get(i + 1) == Some(&'\u{0644}')
&& i > 0
&& is_arabic_letter(chars[i - 1])
&& chars[i - 1] != '\u{0644}'
{
a.push('\u{0644}');
a.push(c);
i += 2;
continue;
}
a.push(c);
i += 1;
}
let mut out: Vec<char> = Vec::with_capacity(a.len());
for (j, &c) in a.iter().enumerate() {
if j > 0 {
let p = a[j - 1];
if (is_arabic_letter(p) && c.is_ascii_alphabetic())
|| (p.is_ascii_alphabetic() && is_arabic_letter(c))
{
out.push(' ');
}
}
out.push(c);
}
out.into_iter().collect()
}
pub(crate) fn region_hyperlink(
region: &Region,
links: &[crate::pdfium_backend::LinkAnnot],
) -> Option<String> {
if links.is_empty() {
return None;
}
let area = (region.r - region.l).max(0.0) * (region.b - region.t).max(0.0);
if area <= 0.0 {
return None;
}
let mut coverage: Vec<(&str, f32)> = Vec::new();
for link in links {
let ix = (region.r.min(link.r) - region.l.max(link.l)).max(0.0);
let iy = (region.b.min(link.b) - region.t.max(link.t)).max(0.0);
let c = ix * iy / area;
match coverage.iter_mut().find(|(uri, _)| *uri == link.uri) {
Some((_, acc)) => *acc += c,
None => coverage.push((&link.uri, c)),
}
}
let mut best: Option<(&str, f32)> = None;
for (uri, c) in coverage {
if best.is_none_or(|(_, bc)| c > bc) {
best = Some((uri, c));
}
}
let (uri, c) = best?;
(c >= 0.5).then(|| normalize_uri(uri))
}
fn normalize_uri(uri: &str) -> String {
if let Some((_, rest)) = uri.split_once("://") {
if !rest.is_empty() && !rest.contains(['/', '?', '#']) {
return format!("{uri}/");
}
}
uri.to_string()
}
pub(crate) fn resolve_link_anchors(page: &PdfPage) -> Vec<(String, String)> {
let mut out: Vec<(String, String)> = Vec::new();
let words = if page.word_cells.is_empty() {
&page.cells
} else {
&page.word_cells
};
for link in &page.links {
let mut inside: Vec<(&TextCell, String)> = words
.iter()
.filter(|c| {
let cy = (c.t + c.b) / 2.0;
cy >= link.t && cy <= link.b && c.r.min(link.r) > c.l.max(link.l)
})
.filter_map(|c| {
let text = cell_text_in_rect(c, link.l, link.r);
(!text.is_empty()).then_some((c, text))
})
.collect();
let band = inside
.iter()
.map(|(c, _)| (c.b - c.t).abs())
.fold(0.0f32, f32::max)
.max(1.0);
inside.sort_by_key(|(c, _)| ((c.t / band).round() as i64, (c.l * 10.0) as i64));
let anchor = clean_text(
&inside
.iter()
.map(|(_, t)| t.trim())
.filter(|t| !t.is_empty())
.collect::<Vec<_>>()
.join(" "),
);
if anchor.is_empty() {
continue;
}
if out
.last()
.is_some_and(|(a, u)| a == &anchor && u == &link.uri)
{
continue;
}
out.push((anchor, link.uri.clone()));
}
out
}
fn cell_text_in_rect(c: &TextCell, l: f32, r: f32) -> String {
let cx = (c.l + c.r) / 2.0;
if cx >= l && cx <= r && c.l >= l - (c.r - c.l) * 0.25 && c.r <= r + (c.r - c.l) * 0.25 {
return c.text.trim().to_string();
}
let chars: Vec<char> = c.text.chars().collect();
let n = chars.len();
if n == 0 || c.r <= c.l {
return String::new();
}
let per = (c.r - c.l) / n as f32;
let mut out: Vec<String> = Vec::new();
let mut token = String::new();
let mut start = 0usize;
for (i, &ch) in chars.iter().enumerate().chain(std::iter::once((n, &' '))) {
if ch.is_whitespace() {
if !token.is_empty() {
let mid = c.l + (start as f32 + (i - start) as f32 / 2.0) * per;
if mid >= l && mid <= r {
out.push(std::mem::take(&mut token));
} else {
token.clear();
}
}
} else {
if token.is_empty() {
start = i;
}
token.push(ch);
}
}
out.join(" ")
}
fn region_text(region: &Region, cells: &[TextCell]) -> String {
let inside: Vec<&TextCell> = cells
.iter()
.filter(|c| {
let ca = area(c.l, c.t, c.r, c.b).max(1.0);
inter(region, c.l, c.t, c.r, c.b) / ca > 0.5
})
.collect();
cells_text(inside)
}
pub fn region_texts_exclusive(regions: &[Region], cells: &[TextCell]) -> Vec<String> {
let mut texts = region_texts_keep_wrap(regions, cells);
for t in &mut texts {
if t.ends_with('\u{ad}') {
t.pop();
}
}
texts
}
fn region_texts_keep_wrap(regions: &[Region], cells: &[TextCell]) -> Vec<String> {
let owned = assign_cells(regions, cells);
regions
.iter()
.zip(owned)
.map(|(r, cs)| {
if claims_cells(r) {
cells_text_keep_wrap(cs.iter().map(|&i| &cells[i]).collect())
} else {
region_text(r, cells)
}
})
.collect()
}
fn claims_cells(r: &Region) -> bool {
r.label != "picture" && !is_wrapper(r.label)
}
fn assign_cells(regions: &[Region], cells: &[TextCell]) -> Vec<Vec<usize>> {
let mut owned: Vec<Vec<usize>> = vec![Vec::new(); regions.len()];
for (ci, c) in cells.iter().enumerate() {
if c.text.trim().is_empty() {
continue;
}
let ca = area(c.l, c.t, c.r, c.b).max(1.0);
let mut best: Option<(usize, f32)> = None;
for (i, r) in regions.iter().enumerate() {
if !claims_cells(r) {
continue;
}
let ov = inter(r, c.l, c.t, c.r, c.b) / ca;
let wins = |(bi, b): (usize, f32)| {
ov > b || (ov == b && r.score == 0.0 && regions[bi].score != 0.0)
};
if ov > 0.2 && best.is_none_or(wins) {
best = Some((i, ov));
}
}
if let Some((i, _)) = best {
owned[i].push(ci);
}
}
owned
}
pub(crate) fn checkbox_glyph(c: char) -> Option<bool> {
match c {
'☐' | '□' | '▢' | '◻' | '❏' | '❐' | '❑' | '❒' => Some(false),
'☑' | '☒' | '⊠' | '⌧' | '▣' | '\u{1F5F9}' | '\u{1F5F7}' | '\u{1F5F5}' => {
Some(true)
}
_ => None,
}
}
pub(crate) fn strip_checkbox_glyph(text: &str) -> &str {
let t = text.trim_start();
match t.chars().next() {
Some(c) if checkbox_glyph(c).is_some() => t[c.len_utf8()..].trim_start(),
_ => text,
}
}
fn frames_text(sq: &crate::checkbox::CheckBox, cells: &[TextCell]) -> bool {
cells.iter().any(|c| {
let (cx, cy) = ((c.l + c.r) / 2.0, (c.t + c.b) / 2.0);
let text = c.text.trim();
cx > sq.l
&& cx < sq.r
&& cy > sq.t
&& cy < sq.b
&& text.chars().any(char::is_alphanumeric)
&& !matches!(text, "x" | "X" | "v" | "V")
})
}
pub fn split_checkbox_lines(
regions: &mut Vec<Region>,
cells: &[TextCell],
boxes: &[crate::checkbox::CheckBox],
) {
let has_glyph = || {
cells
.iter()
.any(|c| c.text.chars().any(|ch| checkbox_glyph(ch).is_some()))
};
if boxes.is_empty() && !has_glyph() {
return;
}
let owned = assign_cells(regions, cells);
let mut used = vec![false; boxes.len()];
let mut frame: Vec<Option<bool>> = vec![None; boxes.len()];
let mut appended: Vec<Region> = Vec::new();
for (i, mine) in owned.iter().enumerate() {
let region = regions[i].clone();
let model_checkbox = matches!(region.label, "checkbox_selected" | "checkbox_unselected");
if !(model_checkbox || matches!(region.label, "text" | "list_item")) || mine.is_empty() {
continue;
}
let mut sorted: Vec<&TextCell> = mine.iter().map(|&c| &cells[c]).collect();
sorted.sort_by(|a, b| a.t.total_cmp(&b.t).then(a.l.total_cmp(&b.l)));
type Row<'c> = ((f32, f32, f32, f32), Vec<&'c TextCell>);
let mut rows: Vec<Row> = Vec::new();
for c in sorted {
let mid = (c.t + c.b) / 2.0;
match rows.last_mut() {
Some((row, members)) if mid >= row.1 && mid <= row.3 => {
*row = (
row.0.min(c.l),
row.1.min(c.t),
row.2.max(c.r),
row.3.max(c.b),
);
members.push(c);
}
_ => rows.push(((c.l, c.t, c.r, c.b), vec![c])),
}
}
enum Mark {
Item {
square: Option<usize>,
checked: bool,
},
Plain,
Continue,
}
let mut marks: Vec<Mark> = Vec::with_capacity(rows.len());
for ((l, t, _, b), members) in &rows {
let square = boxes
.iter()
.enumerate()
.filter(|&(k, sq)| {
let side = sq.r - sq.l;
let mid = (sq.t + sq.b) / 2.0;
let gap = l - sq.r;
!used[k]
&& mid >= t - 2.0
&& mid <= b + 2.0
&& gap >= -1.0
&& gap <= (1.5 * side).max(6.0)
&& !*frame[k].get_or_insert_with(|| frames_text(sq, cells))
})
.min_by(|a, b| (l - a.1.r).total_cmp(&(l - b.1.r)))
.map(|(k, _)| k);
let mark = match square {
Some(k) => {
used[k] = true;
Mark::Item {
square: Some(k),
checked: boxes[k].checked,
}
}
None => {
let mut members = members.clone();
members.sort_by(|a, b| a.l.total_cmp(&b.l));
let text: String = members.iter().map(|c| c.text.as_str()).collect();
let first = text.trim_start().chars().next().and_then(checkbox_glyph);
let glyphs = text
.chars()
.filter(|&c| checkbox_glyph(c).is_some())
.count();
match (first, glyphs) {
(Some(checked), 1) => Mark::Item {
square: None,
checked,
},
(_, 0) => Mark::Continue,
_ => Mark::Plain,
}
}
};
marks.push(mark);
}
if model_checkbox {
if let Some(Mark::Item { checked, .. }) = marks.first() {
regions[i].label = if *checked {
"checkbox_selected"
} else {
"checkbox_unselected"
};
}
continue;
}
if !marks.iter().any(|m| matches!(m, Mark::Item { .. })) {
continue;
}
let mut pieces: Vec<Region> = Vec::new();
for ((row, _), mark) in rows.iter().zip(&marks) {
match mark {
Mark::Item { square, checked } => {
let (mut l, mut t, mut r, mut b) = *row;
if let Some(s) = square.map(|k| &boxes[k]) {
(l, t, r, b) = (l.min(s.l), t.min(s.t), r.max(s.r), b.max(s.b));
}
pieces.push(Region {
label: if *checked {
"checkbox_selected"
} else {
"checkbox_unselected"
},
score: region.score,
l,
t,
r,
b,
});
}
Mark::Plain => pieces.push(Region {
l: row.0,
t: row.1,
r: row.2,
b: row.3,
..region.clone()
}),
Mark::Continue => match pieces.last_mut() {
Some(p) => {
p.l = p.l.min(row.0);
p.t = p.t.min(row.1);
p.r = p.r.max(row.2);
p.b = p.b.max(row.3);
}
None => pieces.push(Region {
l: row.0,
t: row.1,
r: row.2,
b: row.3,
..region.clone()
}),
},
}
}
let mut pieces = pieces.into_iter();
if let Some(first) = pieces.next() {
regions[i] = first;
}
appended.extend(pieces);
}
regions.extend(appended);
}
fn split_text_containers(regions: &mut Vec<Region>, cells: &[TextCell]) {
type Bx = (f32, f32, f32, f32);
let union = |a: Bx, b: Bx| -> Bx { (a.0.min(b.0), a.1.min(b.1), a.2.max(b.2), a.3.max(b.3)) };
let owned = assign_cells(regions, cells);
let lines_of = |own: &[usize]| -> Vec<(Bx, String)> {
let mut own = own.to_vec();
own.sort_by(|&a, &b| cells[a].t.total_cmp(&cells[b].t));
let mut lines: Vec<(Bx, Vec<usize>)> = Vec::new();
for i in own {
let c = &cells[i];
let mid = 0.5 * (c.t + c.b);
match lines.last_mut() {
Some((b, members)) if mid >= b.1 && mid <= b.3 => {
*b = union(*b, (c.l, c.t, c.r, c.b));
members.push(i);
}
_ => lines.push(((c.l, c.t, c.r, c.b), vec![i])),
}
}
lines
.into_iter()
.map(|(b, mut members)| {
members.sort_by(|&a, &b| cells[a].l.total_cmp(&cells[b].l));
let text: Vec<&str> = members.iter().map(|&i| cells[i].text.trim()).collect();
(b, text.join(" "))
})
.collect()
};
let mut drop = vec![false; regions.len()];
let mut grow: Vec<(usize, Bx)> = Vec::new();
let mut added: Vec<Region> = Vec::new();
for ci in 0..regions.len() {
let c = ®ions[ci];
if c.label != "text" || c.score == 0.0 || drop[ci] || owned[ci].is_empty() {
continue;
}
let inner: Vec<(usize, Vec<(Bx, String)>)> = (0..regions.len())
.filter(|&k| k != ci && !drop[k] && claims_cells(®ions[k]) && !owned[k].is_empty())
.filter(|&k| {
let r = ®ions[k];
inter(c, r.l, r.t, r.r, r.b) / area(r.l, r.t, r.r, r.b).max(1.0) > 0.8
})
.map(|k| (k, lines_of(&owned[k])))
.collect();
if inner.is_empty() {
continue;
}
let own_lines = lines_of(&owned[ci]);
let boxes = |ls: &[(Bx, String)]| ls.iter().map(|l| l.0).reduce(union).expect("lines");
let inner_boxes: Vec<Bx> = inner.iter().map(|(_, ls)| boxes(ls)).collect();
let stack = inner_boxes.iter().copied().fold(boxes(&own_lines), union);
let width = (stack.2 - stack.0).max(1.0);
let wrapped_tail = |n: usize| -> bool {
let (ls, b) = (&inner[n].1, inner_boxes[n]);
let h = (ls[0].0 .3 - ls[0].0 .1).max(1.0);
ls.len() == 1
&& b.0 - stack.0 < 2.0 * h
&& own_lines.iter().any(|(lb, _)| {
let gap = b.1 - lb.3;
gap > -0.25 * h
&& gap < 1.5 * h
&& lb.0 - stack.0 < 2.0 * h
&& lb.2 - lb.0 >= 0.75 * width
})
&& !inner_boxes
.iter()
.enumerate()
.any(|(m, o)| m != n && o.1 < b.3 && o.3 > b.1)
};
if (0..inner.len()).any(|n| {
let b = inner_boxes[n];
b.2 - b.0 < 0.5 * width && !wrapped_tail(n)
}) {
continue;
}
let beside = own_lines.iter().any(|(lb, _)| {
let mid = 0.5 * (lb.1 + lb.3);
inner_boxes.iter().any(|b| mid > b.1 && mid < b.3)
});
if beside {
continue;
}
struct Seg {
inner: Option<usize>,
bx: Bx,
label: &'static str,
first: (Bx, String),
last: (Bx, String),
}
let mut segs: Vec<Seg> = inner
.iter()
.enumerate()
.map(|(n, (k, ls))| Seg {
inner: Some(n),
bx: inner_boxes[n],
label: regions[*k].label,
first: ls[0].clone(),
last: ls.last().expect("lines").clone(),
})
.collect();
let mut run: Option<Seg> = None;
for (lb, text) in own_lines {
let crossed = run
.as_ref()
.is_some_and(|r| inner_boxes.iter().any(|b| b.1 >= r.last.0 .1 && b.1 < lb.1));
if crossed {
segs.extend(run.take());
}
match run.as_mut() {
Some(r) => {
r.bx = union(r.bx, lb);
r.last = (lb, text);
}
None => {
run = Some(Seg {
inner: None,
bx: lb,
label: "text",
first: (lb, text.clone()),
last: (lb, text),
})
}
}
}
segs.extend(run);
segs.sort_by(|a, b| a.bx.1.total_cmp(&b.bx.1));
let runs_on = |upper: &Seg, lower: &Seg| -> bool {
let (lb, text) = &upper.last;
let line_h = (lb.3 - lb.1).max(1.0);
let (nb, next) = &lower.first;
let next_chars = next.chars().count().max(1) as f32;
let word = next.split_whitespace().next().unwrap_or("");
let word_w = (nb.2 - nb.0) * (word.chars().count() + 1) as f32 / next_chars;
let flush_left = lb.0 - stack.0 < 2.0 * line_h;
let full = flush_left && (lb.2 - lb.0 >= 0.75 * width || lb.2 + word_w > stack.2);
upper.label == "text"
&& lower.label == "text"
&& lower.bx.1 - lb.3 < 1.5 * line_h
&& full
&& !text.ends_with(['.', ':', ';', '!', '?'])
};
let mut groups: Vec<Vec<usize>> = Vec::new();
for n in 0..segs.len() {
match groups.last_mut() {
Some(g) if runs_on(&segs[*g.last().expect("group")], &segs[n]) => g.push(n),
_ => groups.push(vec![n]),
}
}
drop[ci] = true;
for g in groups {
let gbox = g.iter().map(|&n| segs[n].bx).reduce(union).expect("group");
let mut hosts = g.iter().filter_map(|&n| segs[n].inner.map(|m| inner[m].0));
match hosts.next() {
Some(k) => {
grow.push((k, gbox));
for other in hosts {
drop[other] = true;
}
}
None => added.push(Region {
l: gbox.0,
t: gbox.1,
r: gbox.2,
b: gbox.3,
..regions[ci].clone()
}),
}
}
}
if !drop.iter().any(|&d| d) {
return;
}
for (k, (l, t, r, b)) in grow {
let g = &mut regions[k];
(g.l, g.t, g.r, g.b) = (g.l.min(l), g.t.min(t), g.r.max(r), g.b.max(b));
}
let mut drop = drop.into_iter();
regions.retain(|_| !drop.next().expect("aligned"));
regions.extend(added);
}
pub fn fit_regions_to_cells(regions: &mut Vec<Region>, cells: &[TextCell]) {
if !cells.iter().any(|c| !c.text.trim().is_empty()) {
return;
}
split_text_containers(regions, cells);
let mut raw: Vec<(f32, f32, f32, f32)> = regions.iter().map(|r| (r.l, r.t, r.r, r.b)).collect();
for _ in 0..3 {
let owned = assign_cells(regions, cells);
let mut fitted: Vec<Region> = Vec::with_capacity(regions.len());
let mut fitted_raw: Vec<(f32, f32, f32, f32)> = Vec::with_capacity(regions.len());
for ((r, own), &rb) in regions.iter().zip(&owned).zip(&raw) {
if !claims_cells(r) {
fitted.push(r.clone());
fitted_raw.push(rb);
continue;
}
if own.is_empty() {
if r.label == "formula" {
fitted.push(r.clone());
fitted_raw.push(rb);
}
continue;
}
let mut f = r.clone();
f.l = own
.iter()
.map(|&i| cells[i].l)
.fold(f32::INFINITY, f32::min);
f.t = own
.iter()
.map(|&i| cells[i].t)
.fold(f32::INFINITY, f32::min);
f.r = own
.iter()
.map(|&i| cells[i].r)
.fold(f32::NEG_INFINITY, f32::max);
f.b = own
.iter()
.map(|&i| cells[i].b)
.fold(f32::NEG_INFINITY, f32::max);
fitted.push(f);
fitted_raw.push(rb);
}
let mut changed = fitted.len() != regions.len();
let mut drop = vec![false; fitted.len()];
for i in 0..fitted.len() {
let o = &fitted[i];
if !(o.score == 0.0 && o.label == "text") {
continue;
}
let oa = area(o.l, o.t, o.r, o.b).max(1.0);
let mut best: Option<(usize, f32)> = None;
for (j, r) in fitted.iter().enumerate() {
if j == i || drop[j] || r.score == 0.0 || !claims_cells(r) {
continue;
}
let (rl, rt, rr, rb) = fitted_raw[j];
if rr.min(o.r) < rl.max(o.l) || rb.min(o.b) < rt.max(o.t) {
continue;
}
let ov = inter(r, o.l, o.t, o.r, o.b) / oa;
if ov > 0.8 && best.is_none_or(|(_, b)| ov > b) {
best = Some((j, ov));
}
}
if let Some((j, _)) = best {
let (l, t, r, b) = (o.l, o.t, o.r, o.b);
let host = &mut fitted[j];
host.l = host.l.min(l);
host.t = host.t.min(t);
host.r = host.r.max(r);
host.b = host.b.max(b);
drop[i] = true;
changed = true;
}
}
let mut keep = drop.iter().map(|&d| !d);
fitted_raw.retain(|_| keep.next().expect("aligned"));
let mut drop = drop.into_iter();
fitted.retain(|_| !drop.next().expect("aligned"));
*regions = fitted;
raw = fitted_raw;
if !changed {
break;
}
}
}
fn cells_text(inside: Vec<&TextCell>) -> String {
let mut t = cells_text_keep_wrap(inside);
if t.ends_with('\u{ad}') {
t.pop();
}
t
}
fn cells_text_keep_wrap(inside: Vec<&TextCell>) -> String {
let joined = {
let texts: Vec<&str> = inside
.iter()
.map(|c| c.text.trim())
.filter(|t| !t.is_empty())
.collect();
let last_word_alnum = |s: &str| {
s.split(|c: char| !(c.is_alphanumeric() || c == '_'))
.rfind(|w| !w.is_empty())
.is_some_and(|w| w.chars().all(char::is_alphanumeric))
};
let first_word_alnum = |s: &str| {
s.split(|c: char| !(c.is_alphanumeric() || c == '_'))
.find(|w| !w.is_empty())
.is_some_and(|w| w.chars().all(char::is_alphanumeric))
};
let mut out = String::new();
for (i, t) in texts.iter().enumerate() {
if i > 0 {
let prev = texts[i - 1];
let dashish = matches!(
prev.chars().last(),
Some(
'-' | '\u{2010}'
| '\u{2011}'
| '\u{2012}'
| '\u{2013}'
| '\u{2014}'
| '\u{2015}'
| '\u{2212}'
)
);
let attached = prev.chars().rev().nth(1).is_some_and(char::is_alphanumeric);
if dashish && attached && starts_with_conjunction(t) {
out.push(' ');
} else if dashish && attached {
if last_word_alnum(prev) && first_word_alnum(t) {
out.pop(); }
} else {
out.push(' ');
}
}
out.push_str(t);
}
out
};
clean_text_keep_wrap(&joined)
}
fn tighten_code_punct(s: &str) -> String {
s.replace(" .", ".")
.replace(" ,", ",")
.replace(" ;", ";")
.replace(" )", ")")
.replace(" (", "(")
}
fn code_region_text(region: &Region, cells: &[TextCell]) -> String {
let mut inside: Vec<&TextCell> = cells
.iter()
.filter(|c| {
let ca = area(c.l, c.t, c.r, c.b).max(1.0);
inter(region, c.l, c.t, c.r, c.b) / ca > 0.5
})
.filter(|c| !c.text.trim().is_empty())
.collect();
if inside.is_empty() {
return String::new();
}
let band = inside
.iter()
.map(|c| (c.b - c.t).abs())
.fold(0.0f32, f32::max)
.max(1.0);
let line_of = |c: &TextCell| (c.t / band).round() as i64;
inside.sort_by_key(|c| (line_of(c), (c.l * 10.0) as i64));
let (mut total_w, mut total_chars) = (0.0f32, 0usize);
for c in &inside {
let n = c.text.trim().chars().count();
if n > 0 {
total_w += (c.r - c.l).max(0.0);
total_chars += n;
}
}
let char_w = if total_chars > 0 {
(total_w / total_chars as f32).max(1.0)
} else {
1.0
};
let base_l = inside.iter().map(|c| c.l).fold(f32::INFINITY, f32::min);
let mut lines: Vec<String> = Vec::new();
let mut cur: Option<i64> = None;
for c in &inside {
let text = tighten_code_punct(&clean_text(c.text.trim()));
if Some(line_of(c)) == cur {
if let Some(last) = lines.last_mut() {
last.push(' ');
last.push_str(&text);
}
continue;
}
let indent = ((c.l - base_l) / char_w).round().max(0.0) as usize;
lines.push(format!("{}{}", " ".repeat(indent), text));
cur = Some(line_of(c));
}
lines.join("\n")
}
fn line_height(c: &TextCell) -> f32 {
let box_h = (c.b - c.t).abs();
let line = LINE_PER_ADVANCE * c.mean_advance();
if box_h > INFLATED_BOX * line {
line.max(1.0)
} else {
box_h.max(1.0)
}
}
fn line_top(c: &TextCell) -> f32 {
let lh = line_height(c);
if lh < (c.b - c.t).abs() {
(c.t + c.b) / 2.0 - lh / 2.0
} else {
c.t
}
}
const LINE_PER_ADVANCE: f32 = 2.5;
const INFLATED_BOX: f32 = 2.0;
fn table_line_cells<'a>(region: &Region, cells: &'a [TextCell]) -> Vec<&'a TextCell> {
let mut inside: Vec<&TextCell> = cells
.iter()
.filter(|c| {
let ca = area(c.l, c.t, c.r, c.b).max(1.0);
inter(region, c.l, c.t, c.r, c.b) / ca > 0.5
})
.collect();
inside.sort_by(|a, b| line_top(a).total_cmp(&line_top(b)));
inside
}
fn table_line_rows<'a>(inside: &[&'a TextCell]) -> Vec<(f32, Vec<&'a TextCell>)> {
let mut rows: Vec<(f32, Vec<&TextCell>)> = Vec::new();
for c in inside {
let cyc = (c.t + c.b) / 2.0;
let lh = line_height(c);
if let Some((ryc, row)) = rows.last_mut() {
if (cyc - *ryc).abs() < lh * 0.7 {
row.push(c);
continue;
}
}
rows.push((cyc, vec![c]));
}
rows
}
pub fn geometric_rows_share_lines(region: &Region, cells: &[TextCell]) -> bool {
let inside = table_line_cells(region, cells);
table_line_rows(&inside).iter().all(|(_, row)| {
let centres = || row.iter().map(|c| (c.t + c.b) / 2.0);
let spread =
centres().fold(f32::NEG_INFINITY, f32::max) - centres().fold(f32::INFINITY, f32::min);
let line = row
.iter()
.map(|c| line_height(c))
.fold(f32::INFINITY, f32::min);
spread <= ROW_LINE_SLACK * line
})
}
const ROW_LINE_SLACK: f32 = 0.25;
pub fn reconstruct_table(region: &Region, cells: &[TextCell]) -> Vec<Vec<String>> {
let inside = table_line_cells(region, cells);
if inside.is_empty() {
return Vec::new();
}
let rows = table_line_rows(&inside);
let tol = {
let mut hs: Vec<f32> = inside.iter().map(|c| line_height(c)).collect();
hs.sort_by(f32::total_cmp);
hs[hs.len() / 2].max(4.0) * 1.5
};
let mut lefts: Vec<f32> = inside.iter().map(|c| c.l).collect();
lefts.sort_by(f32::total_cmp);
let mut col_starts: Vec<f32> = Vec::new();
for l in lefts {
if col_starts.last().is_none_or(|&last| l - last > tol) {
col_starts.push(l);
}
}
let ncols = col_starts.len().max(1);
let col_of = |l: f32| -> usize {
col_starts
.iter()
.rposition(|&s| l + tol * 0.5 >= s)
.unwrap_or(0)
.min(ncols - 1)
};
let mut grid = Vec::with_capacity(rows.len());
for (_, mut row) in rows {
row.sort_by(|a, b| a.l.total_cmp(&b.l));
let mut cols = vec![String::new(); ncols];
for c in row {
let ci = col_of(c.l);
let t = c.text.trim().replace(['\u{2}', '\u{ad}'], "");
if cols[ci].is_empty() {
cols[ci] = t;
} else {
cols[ci].push(' ');
cols[ci].push_str(&t);
}
}
grid.push(cols);
}
grid
}
pub fn geometric_table_is_reliable(rows: &[Vec<String>]) -> bool {
let ncols = rows.iter().map(Vec::len).max().unwrap_or(0);
if rows.len() < 2 || ncols < 2 {
return false;
}
let filled = |c: &String| !c.trim().is_empty();
let total = rows.len() * ncols;
let full = rows.iter().flatten().filter(|c| filled(c)).count();
if (full as f32) < MIN_TABLE_FILL * total as f32 {
return false;
}
if rows.len() >= 3 {
for ci in 0..ncols {
let used = rows
.iter()
.filter(|r| r.get(ci).is_some_and(filled))
.count();
if used <= 1 {
return false;
}
}
}
true
}
const MIN_TABLE_FILL: f32 = 0.6;
pub fn region_cell_bbox(region: &Region, cells: &[TextCell]) -> Option<[f32; 4]> {
let mut bbox: Option<[f32; 4]> = None;
for c in cells {
let ca = area(c.l, c.t, c.r, c.b).max(1.0);
if inter(region, c.l, c.t, c.r, c.b) / ca <= 0.5 {
continue;
}
bbox = Some(match bbox {
None => [c.l, c.t, c.r, c.b],
Some([l, t, r, b]) => [l.min(c.l), t.min(c.t), r.max(c.r), b.max(c.b)],
});
}
bbox
}
#[derive(Debug, Clone)]
pub enum Enrichment {
PictureClasses(Vec<PictureClass>),
Code {
language: Option<String>,
text: String,
},
Formula { latex: String },
}
#[cfg(feature = "ml")]
pub fn crop_region_scaled(page: &PdfPage, bbox: [f32; 4], target_scale: f32) -> Option<RgbImage> {
debug_assert!(
!page.image_pending(),
"a crop reached an unrendered page bitmap (#897)"
);
let s = page.scale;
let [l, t, r, b] = bbox;
let (iw, ih) = (page.image.width(), page.image.height());
let x = (l * s).max(0.0) as u32;
let y = (t * s).max(0.0) as u32;
if x >= iw || y >= ih {
return None;
}
let w = (((r - l.max(0.0)) * s) as u32).min(iw - x);
let h = (((b - t.max(0.0)) * s) as u32).min(ih - y);
if w == 0 || h == 0 {
return None;
}
let crop = image::imageops::crop_imm(&page.image, x, y, w, h).to_image();
let tw = ((w as f32 / s) * target_scale).round().max(1.0) as u32;
let th = ((h as f32 / s) * target_scale).round().max(1.0) as u32;
if (tw, th) == (w, h) {
return Some(crop);
}
Some(image::imageops::resize(
&crop,
tw,
th,
image::imageops::FilterType::CatmullRom,
))
}
#[cfg(feature = "ocr-prep")]
fn rescale(img: RgbImage, w_pt: f32, h_pt: f32, to: f32) -> RgbImage {
let tw = (w_pt * to).round().max(1.0) as u32;
let th = (h_pt * to).round().max(1.0) as u32;
if (tw, th) == img.dimensions() {
return img;
}
image::imageops::resize(&img, tw, th, image::imageops::FilterType::CatmullRom)
}
#[cfg(feature = "ocr-prep")]
fn png_image(img: &RgbImage, scale: f32) -> Option<PictureImage> {
let mut buf = std::io::Cursor::new(Vec::new());
img.write_to(&mut buf, image::ImageFormat::Png).ok()?;
Some(PictureImage {
mimetype: "image/png".into(),
width: img.width(),
height: img.height(),
data: buf.into_inner(),
dpi: PictureImage::dpi_for_scale(scale),
display_size: None,
})
}
#[cfg(feature = "ocr-prep")]
pub fn page_image(page: &PdfPage, scale: Option<f32>) -> Option<PictureImage> {
#[cfg(feature = "ml")]
debug_assert!(
!page.image_pending(),
"a crop reached an unrendered page bitmap (#897)"
);
if page.image.width() == 0 || page.image.height() == 0 || page.scale <= 0.0 {
return None;
}
let scale = scale.unwrap_or(page.scale);
let img = rescale(page.image.clone(), page.width, page.height, scale);
png_image(&img, scale)
}
#[cfg(feature = "ocr-prep")]
fn crop_region(page: &PdfPage, region: &Region, scale: Option<f32>) -> Option<PictureImage> {
#[cfg(feature = "render")]
if let Some(img) = crate::embedded::match_region(
&page.embedded_images,
&page.cells,
[region.l, region.t, region.r, region.b],
) {
return png_image(img, 1.0);
}
#[cfg(feature = "ml")]
debug_assert!(
!page.image_pending(),
"a crop reached an unrendered page bitmap (#897)"
);
let s = page.scale;
let (iw, ih) = (page.image.width(), page.image.height());
let x = (region.l * s).max(0.0) as u32;
let y = (region.t * s).max(0.0) as u32;
if x >= iw || y >= ih {
return None;
}
let w = (((region.r - region.l) * s) as u32).min(iw - x);
let h = (((region.b - region.t) * s) as u32).min(ih - y);
if w == 0 || h == 0 {
return None;
}
let sub = image::imageops::crop_imm(&page.image, x, y, w, h).to_image();
match scale {
Some(to) if (to - s).abs() > f32::EPSILON => {
let img = rescale(sub, w as f32 / s, h as f32 / s, to);
png_image(&img, to)
}
_ => png_image(&sub, s),
}
}
fn promote_labelled_captions(regions: &mut [Region], cells: &[TextCell]) {
let body: Vec<usize> = (0..regions.len())
.filter(|&i| !is_skipped(regions[i].label))
.collect();
let mut promote = Vec::new();
for (pos, &i) in body.iter().enumerate() {
let r = ®ions[i];
if r.label != "text" || !is_caption_label(®ion_text(r, cells)) {
continue;
}
let beside = |g: &Region| {
is_graphic(g.label) && g.r.min(r.r) - g.l.max(r.l) > 0.0 && {
let gap = if r.t >= g.b {
r.t - g.b
} else if g.t >= r.b {
g.t - r.b
} else {
f32::MAX
};
gap <= 25.0
}
};
let prev = pos.checked_sub(1).map(|p| ®ions[body[p]]);
let next = body.get(pos + 1).map(|&n| ®ions[n]);
if prev.is_some_and(beside) || next.is_some_and(beside) {
promote.push(i);
}
}
for i in promote {
regions[i].label = "caption";
}
}
fn is_caption_label(text: &str) -> bool {
let t = text.trim_start();
let Some(rest) = ["Table", "Figure", "Fig.", "Listing"]
.iter()
.find_map(|w| t.strip_prefix(w))
else {
return false;
};
let rest = rest.trim_start();
if !rest.starts_with(|c: char| c.is_ascii_digit()) {
return false;
}
let num_end = rest
.find(|c: char| !(c.is_ascii_alphanumeric() || c == '.'))
.unwrap_or(rest.len());
let num = rest[..num_end].trim_end_matches('.');
let after = &rest[num.len()..];
after.starts_with(':') || after.starts_with('.')
}
fn is_graphic(label: &str) -> bool {
is_table_like(label) || matches!(label, "picture" | "code")
}
fn shortest_box_gap(a: &Region, b: &Region) -> f32 {
let dx = ((a.r.max(b.r) - a.l.min(b.l)) - (a.r - a.l) - (b.r - b.l)).max(0.0);
let dy = ((a.b.max(b.b) - a.t.min(b.t)) - (a.b - a.t) - (b.b - b.t)).max(0.0);
dx.hypot(dy)
}
fn pair_graphic_captions(regions: &[Region]) -> Vec<Option<usize>> {
let body: Vec<usize> = (0..regions.len())
.filter(|&i| !is_skipped(regions[i].label))
.collect();
let mut captions: Vec<usize> = Vec::new();
let mut preferred: Vec<Vec<usize>> = vec![Vec::new(); regions.len()];
for (pos, &ci) in body.iter().enumerate() {
if regions[ci].label != "caption" {
continue;
}
let graphic = |&&i: &&usize| is_graphic(regions[i].label);
let preceding = body[..pos].iter().rev().take_while(graphic);
let following = body[pos + 1..].iter().take_while(graphic);
let mut ranked: Vec<(f32, u8, usize)> = preceding
.map(|&g| (shortest_box_gap(®ions[ci], ®ions[g]), 0, g))
.chain(following.map(|&g| (shortest_box_gap(®ions[ci], ®ions[g]), 1, g)))
.collect();
ranked.sort_by(|a, b| a.0.total_cmp(&b.0).then(a.1.cmp(&b.1)));
preferred[ci] = ranked.into_iter().map(|(_, _, g)| g).collect();
captions.push(ci);
}
let mut matched: Vec<Option<usize>> = vec![None; regions.len()];
for &caption in &captions {
if let Some(next) = augment_caption_match(caption, &preferred, &matched) {
matched = next;
}
}
for (pos, &ci) in body.iter().enumerate() {
let is_caption = |i: &usize| regions[*i].label == "caption";
if !is_caption(&ci) || matched.contains(&Some(ci)) {
continue;
}
let c = ®ions[ci];
let line_h = (c.b - c.t).abs().max(1.0);
let graphic = |i: &usize| is_graphic(regions[*i].label);
let before = body[..pos]
.iter()
.rev()
.skip_while(|i| is_caption(i))
.take_while(|i| graphic(i));
let after = body[pos + 1..]
.iter()
.skip_while(|i| is_caption(i))
.take_while(|i| graphic(i));
let best = before
.chain(after)
.filter(|&&g| matched[g].is_none())
.filter_map(|&g| {
let p = ®ions[g];
let h_overlap = p.r.min(c.r) - p.l.max(c.l);
let gap = (c.t - p.b).max(p.t - c.b); (h_overlap > 0.0 && gap > -line_h && gap < line_h * 3.0).then_some((g, gap))
})
.min_by(|a, b| a.1.total_cmp(&b.1));
if let Some((g, _)) = best {
matched[g] = Some(ci);
}
}
matched
}
fn augment_caption_match(
caption: usize,
preferred: &[Vec<usize>],
matched: &[Option<usize>],
) -> Option<Vec<Option<usize>>> {
let mut seen = vec![false; matched.len()];
let mut stack: Vec<(usize, usize)> = vec![(caption, 0)];
let mut chain: Vec<(usize, usize)> = Vec::new();
while let Some(frame) = stack.last_mut() {
let (claimant, cursor) = *frame;
let candidates = &preferred[claimant];
let Some(off) = candidates[cursor..].iter().position(|&g| !seen[g]) else {
stack.pop();
chain.pop();
continue;
};
let graphic = candidates[cursor + off];
frame.1 = cursor + off + 1;
seen[graphic] = true;
chain.push((graphic, claimant));
match matched[graphic] {
None => {
let mut next = matched.to_vec();
for &(g, c) in &chain {
next[g] = Some(c);
}
return Some(next);
}
Some(holder) => stack.push((holder, 0)),
}
}
None
}
fn norm_loc(region: &Region, page_w: f32, page_h: f32) -> [u16; 4] {
let q = |v: f32, dim: f32| -> u16 {
if dim <= 0.0 {
return 0;
}
let g = (512.0 * (v as f64) / (dim as f64)).round() as i64;
g.clamp(0, 511) as u16
};
[
q(region.l, page_w),
q(region.t, page_h),
q(region.r, page_w),
q(region.b, page_h),
]
}
fn located(loc: [u16; 4], inner: Node) -> Node {
Node::Located {
location: loc,
inner: Box::new(inner),
}
}
pub fn stamp_page_no(nodes: &mut [Node], page_no: usize) {
if let Some(Node::PageInfo { page_no: p, .. }) = nodes.first_mut() {
*p = page_no;
}
}
pub fn float_running_head(nodes: &mut [Node], cells: &[TextCell]) {
let mut lines = cells.iter().filter(|c| !c.text.trim().is_empty());
let Some(first) = lines.next() else { return };
let mut top = first;
let mut rest_t = f32::INFINITY;
for c in lines {
if c.t < top.t {
rest_t = rest_t.min(top.t);
top = c;
} else {
rest_t = rest_t.min(c.t);
}
}
if top.b > rest_t {
return;
}
let key = |t: &str| -> String { t.chars().filter(|c| c.is_alphanumeric()).collect() };
let want = key(&clean_text(&top.text));
if want.is_empty() {
return;
}
let body = usize::from(matches!(nodes.first(), Some(Node::PageInfo { .. })));
let Some(at) = nodes
.iter()
.position(|n| as_paragraph(n).is_some_and(|t| key(t) == want))
else {
return;
};
let numbered = nodes.get(body).and_then(as_paragraph).is_some_and(|t| {
let t = t.trim();
!t.is_empty() && t.chars().all(|c| c.is_ascii_digit())
});
if !numbered && at > body && !nodes[body..at].iter().all(is_merge_trailer) {
nodes[body..=at].rotate_right(1);
}
}
#[derive(Clone, Debug)]
pub struct TableGrid {
pub rows: Vec<Vec<String>>,
pub cells: Vec<docling_core::TableCell>,
}
const RICH_CELL_PICTURE_COVERAGE: f32 = 0.8;
fn match_table_pictures(
regions: &[Region],
table_rows: &[Option<TableGrid>],
caption_for: &[Option<usize>],
) -> std::collections::HashMap<usize, Vec<(usize, Vec<usize>)>> {
let mut out: std::collections::HashMap<usize, Vec<(usize, Vec<usize>)>> =
std::collections::HashMap::new();
for (p, pic) in regions.iter().enumerate() {
if pic.label != "picture" || caption_for.get(p).is_some_and(Option::is_some) {
continue;
}
let pa = area(pic.l, pic.t, pic.r, pic.b).max(1.0);
let mut best: Option<(f32, usize, usize)> = None; for (t, tbl) in regions.iter().enumerate() {
if !is_table_like(tbl.label) {
continue;
}
let Some(grid) = table_rows.get(t).and_then(Option::as_ref) else {
continue;
};
if inter(pic, tbl.l, tbl.t, tbl.r, tbl.b) / pa < RICH_CELL_PICTURE_COVERAGE {
continue;
}
if let Some((cov, cell)) = match_picture_to_cell(pic, &grid.cells) {
if best.is_none_or(|(b, _, _)| cov > b) {
best = Some((cov, t, cell));
}
}
}
if let Some((_, t, cell)) = best {
let entry = out.entry(t).or_default();
match entry.iter_mut().find(|(c, _)| *c == cell) {
Some((_, pics)) => pics.push(p),
None => entry.push((cell, vec![p])),
}
}
}
out
}
fn match_picture_to_cell(pic: &Region, cells: &[docling_core::TableCell]) -> Option<(f32, usize)> {
let pa = area(pic.l, pic.t, pic.r, pic.b).max(1.0);
let cover = |b: &[f32; 4]| inter(pic, b[0], b[1], b[2], b[3]) / pa;
let eligible: Vec<(f32, usize)> = cells
.iter()
.enumerate()
.filter_map(|(i, c)| {
let b = c.bbox.as_ref()?;
let cov = cover(b);
(cov >= RICH_CELL_PICTURE_COVERAGE).then_some((cov, i))
})
.collect();
if eligible.is_empty() {
return None;
}
let mut row_centers: std::collections::BTreeMap<usize, Vec<f32>> = Default::default();
let mut col_centers: std::collections::BTreeMap<usize, Vec<f32>> = Default::default();
for c in cells {
let Some(b) = c.bbox.as_ref() else { continue };
for r in c.start_row..c.start_row + c.row_span {
row_centers.entry(r).or_default().push((b[1] + b[3]) / 2.0);
}
for k in c.start_col..c.start_col + c.col_span {
col_centers.entry(k).or_default().push((b[0] + b[2]) / 2.0);
}
}
let median = |v: &mut Vec<f32>| -> f32 {
v.sort_by(f32::total_cmp);
let n = v.len();
if n % 2 == 1 {
v[n / 2]
} else {
(v[n / 2 - 1] + v[n / 2]) / 2.0
}
};
let (px, py) = ((pic.l + pic.r) / 2.0, (pic.t + pic.b) / 2.0);
let nearest = |centers: &mut std::collections::BTreeMap<usize, Vec<f32>>, target: f32| {
centers
.iter_mut()
.map(|(&i, v)| (i, (median(v) - target).abs()))
.min_by(|a, b| a.1.total_cmp(&b.1))
.map(|(i, _)| i)
};
let row = nearest(&mut row_centers, py);
let col = nearest(&mut col_centers, px);
let logical: Vec<(f32, usize)> = eligible
.iter()
.copied()
.filter(|&(_, i)| {
let c = &cells[i];
row.is_some_and(|r| c.start_row <= r && r < c.start_row + c.row_span)
&& col.is_some_and(|k| c.start_col <= k && k < c.start_col + c.col_span)
})
.collect();
let pool = if logical.is_empty() {
&eligible
} else {
&logical
};
pool.iter()
.copied()
.max_by(|a, b| a.0.total_cmp(&b.0).then(a.1.cmp(&b.1)))
}
fn structure_from_cells(
cells: &[docling_core::TableCell],
nrows: usize,
ncols: usize,
) -> docling_core::TableStructure {
let grid = || vec![vec![false; ncols]; nrows];
let mut col_cont = grid();
let mut row_cont = grid();
let mut row_header = grid();
let mut col_header = grid();
for c in cells {
for r in c.start_row..(c.start_row + c.row_span).min(nrows) {
for k in c.start_col..(c.start_col + c.col_span).min(ncols) {
col_cont[r][k] = k > c.start_col;
row_cont[r][k] = r > c.start_row;
row_header[r][k] = c.row_header;
col_header[r][k] = c.column_header;
}
}
}
docling_core::TableStructure {
header_row: Vec::new(),
col_continuation: col_cont,
row_continuation: row_cont,
row_header,
col_header,
}
}
pub fn assemble_page(
page: &PdfPage,
mut regions: Vec<Region>,
table_rows: &[Option<TableGrid>],
enrichments: &[Option<Enrichment>],
picture_scale: Option<f32>,
) -> (Vec<Node>, Vec<(String, String)>) {
#[cfg(not(feature = "ocr-prep"))]
let _ = picture_scale;
let _typography = TypographyScope::set(page.keep_typography);
let mut nodes: Vec<Node> = Vec::new();
nodes.push(Node::PageInfo {
page_no: 0,
width: page.width,
height: page.height,
});
let mut links = resolve_link_anchors(page);
split_checkbox_lines(&mut regions, &page.cells, &page.checkboxes);
let parents = picture_parents(®ions);
let mut kids: Vec<Vec<Region>> = vec![Vec::new(); regions.len()];
let mut top: Vec<(usize, Region)> = Vec::with_capacity(regions.len());
for (i, (r, parent)) in regions.into_iter().zip(parents).enumerate() {
match parent {
Some(p) => kids[p].push(r),
None => top.push((i, r)),
}
}
let top_regions: Vec<Region> = top.iter().map(|(_, r)| r.clone()).collect();
let cids = cluster_cids(&top_regions, &page.cells);
type RegionItem = (Region, Option<TableGrid>, Option<Enrichment>, Vec<Region>);
let mut items: Vec<RegionItem> = top
.into_iter()
.map(|(i, r)| {
(
r,
table_rows.get(i).cloned().flatten(),
enrichments.get(i).cloned().flatten(),
std::mem::take(&mut kids[i]),
)
})
.collect();
let separators = page_separators(page, &top_regions);
order_with_containers(
&mut items,
&cids,
page.width,
page.height,
&separators,
|it| &it.0,
);
let page_h = page.height;
items.sort_by_key(|(r, _, _, _)| !is_page_number(r, &page.cells, page_h));
let table_rows: Vec<Option<TableGrid>> = items.iter().map(|(_, t, _, _)| t.clone()).collect();
let enrichments: Vec<Option<Enrichment>> = items.iter().map(|(_, _, e, _)| e.clone()).collect();
let mut picture_children: Vec<Vec<Region>> = items
.iter_mut()
.map(|it| std::mem::take(&mut it.3))
.collect();
let mut regions: Vec<Region> = items.into_iter().map(|(r, _, _, _)| r).collect();
promote_labelled_captions(&mut regions, &page.cells);
for kids in picture_children.iter_mut().filter(|k| k.len() > 1) {
let rank = cluster_cids(kids, &page.cells);
let mut ranked: Vec<(usize, Region)> = rank.into_iter().zip(kids.drain(..)).collect();
ranked.sort_by_key(|(k, _)| *k);
kids.extend(ranked.into_iter().map(|(_, r)| r));
}
let graphic_caption_for = pair_graphic_captions(®ions);
let only = |pred: fn(&str) -> bool| -> Vec<Option<usize>> {
regions
.iter()
.zip(&graphic_caption_for)
.map(|(r, c)| c.filter(|_| pred(r.label)))
.collect()
};
let caption_for = only(|l| l == "picture");
let code_caption_for = only(|l| l == "code");
let table_caption_for = only(is_table_like);
let mut consumed = vec![false; regions.len()];
for ci in graphic_caption_for.iter().flatten() {
consumed[*ci] = true;
}
let rich_cell_pictures = match_table_pictures(®ions, &table_rows, &caption_for);
for (_, pics) in rich_cell_pictures.values().flatten() {
for &p in pics {
consumed[p] = true;
}
}
for (i, is_label) in code_language_labels(®ions, &page.cells)
.into_iter()
.enumerate()
{
if is_label {
consumed[i] = true;
}
}
let with_children: Vec<Region> = regions
.iter()
.chain(picture_children.iter().flatten())
.cloned()
.collect();
let mut region_texts: Vec<String> = region_texts_keep_wrap(&with_children, &page.cells);
let wraps: Vec<bool> = region_texts
.iter_mut()
.map(|t| t.ends_with('\u{ad}') && t.pop().is_some())
.collect();
let mut kid_texts = region_texts.split_off(regions.len()).into_iter();
let child_texts: Vec<Vec<String>> = picture_children
.iter()
.map(|k| kid_texts.by_ref().take(k.len()).collect())
.collect();
let is_text: Vec<bool> = regions
.iter()
.enumerate()
.map(|(i, r)| r.label == "text" && !consumed[i])
.collect();
let is_skip: Vec<bool> = regions
.iter()
.enumerate()
.map(|(i, r)| {
consumed[i]
|| matches!(
r.label,
"page_header" | "page_footer" | "table" | "picture" | "caption" | "footnote"
)
})
.collect();
let boxes: Vec<(f32, f32, f32, f32)> = regions.iter().map(|r| (r.l, r.t, r.r, r.b)).collect();
if docling_core::env::flag("DOCLING_RS_DEBUG_MERGES") {
for (i, r) in regions.iter().enumerate() {
eprintln!(
"MRG {i:2} {} text={} skip={} [{:.0},{:.0},{:.0},{:.0}] {:?}",
r.label,
is_text[i],
is_skip[i],
r.l,
r.t,
r.r,
r.b,
region_texts[i].chars().take(40).collect::<String>()
);
}
}
let mut merge_suffix: Vec<String> = vec![String::new(); regions.len()];
for (head, children) in
crate::reading_order::predict_merges(&boxes, ®ion_texts, &is_text, &is_skip)
.into_iter()
.enumerate()
{
for c in children {
let t = region_texts[c].trim();
if !t.is_empty() {
merge_suffix[head].push(' ');
merge_suffix[head].push_str(t);
}
consumed[c] = true;
}
}
let in_container: Vec<bool> = regions
.iter()
.map(|r| {
let ra = area(r.l, r.t, r.r, r.b).max(1.0);
!matches!(r.label, "form" | "key_value_region")
&& regions.iter().any(|c| {
matches!(c.label, "form" | "key_value_region")
&& inter(r, c.l, c.t, c.r, c.b) / ra > 0.8
})
})
.collect();
let mut node_in_container: Vec<bool> = Vec::new();
let mut last_in_container = false;
for (i, region) in regions.iter().enumerate() {
node_in_container.resize(nodes.len(), last_in_container);
last_in_container = in_container[i];
if consumed[i] {
continue;
}
if matches!(region.label, "page_header" | "page_footer") {
let text = region_texts[i].clone();
if !text.is_empty() {
nodes.push(Node::PageFurniture {
footer: region.label == "page_footer",
location: norm_loc(region, page.width, page_h),
text: md_escape(&text),
});
}
continue;
}
if is_skipped(region.label) {
continue;
}
let loc = norm_loc(region, page.width, page_h);
if region.label == "picture" {
let caption = caption_for[i]
.map(|ci| md_escape(®ion_texts[ci]))
.filter(|t| !t.is_empty());
let caption_location = caption_for[i]
.filter(|_| caption.is_some())
.map(|ci| norm_loc(®ions[ci], page.width, page_h));
let classification = match &enrichments[i] {
Some(Enrichment::PictureClasses(classes)) => Some(classes.clone()),
_ => None,
};
#[cfg(feature = "ocr-prep")]
let image =
crate::timing::timed("crop_region", || crop_region(page, region, picture_scale));
#[cfg(not(feature = "ocr-prep"))]
let image: Option<PictureImage> = None;
nodes.push(located(
loc,
Node::Picture {
caption,
caption_href: None,
image,
classification,
description: None,
caption_parent: CaptionParent::Item,
caption_location,
},
));
let children: Vec<Node> = picture_children[i]
.iter()
.zip(&child_texts[i])
.filter_map(|(r, text)| {
picture_child_node(r, text, norm_loc(r, page.width, page_h))
})
.collect();
if !children.is_empty() {
nodes.push(Node::PictureChildren(children));
}
continue;
}
let mut text = region_texts[i].clone();
text.push_str(&merge_suffix[i]);
if text.is_empty() && !is_table_like(region.label) {
continue;
}
match region.label {
"checkbox_selected" | "checkbox_unselected" => nodes.push(located(
loc,
Node::CheckboxItem {
checked: region.label == "checkbox_selected",
text: md_escape(strip_checkbox_glyph(&text)),
},
)),
"title" | "section_header" => nodes.push(located(
loc,
Node::Heading {
level: 2,
text: md_escape(&text),
},
)),
"list_item" => nodes.push(list_item_node(&text, loc, false)),
"table" | "document_index" => {
let blank = text.is_empty()
&& table_rows[i]
.as_ref()
.is_none_or(|g| g.rows.iter().flatten().all(|c| c.trim().is_empty()));
let (mut rows, cells, structure) =
match table_rows[i].clone().filter(|_| !blank).map(normalize_grid) {
_ if blank => (Vec::new(), None, None),
Some(grid) => {
let nrows = grid.rows.len();
let ncols = grid.rows.first().map_or(0, Vec::len);
let structure = structure_from_cells(&grid.cells, nrows, ncols);
(grid.rows, Some(grid.cells), Some(structure))
}
None => {
let rows = reconstruct_table(region, &page.cells);
let rows = if rows.iter().any(|r| r.len() > 1) {
rows
} else {
vec![vec![text.clone()]]
};
(rows, None, None)
}
};
let caption = table_caption_for[i]
.map(|ci| md_escape(®ion_texts[ci]))
.filter(|t| !t.is_empty());
let caption_location = table_caption_for[i]
.filter(|_| caption.is_some())
.map(|ci| norm_loc(®ions[ci], page.width, page_h));
let mut cell_blocks: Option<Vec<Vec<Vec<Node>>>> = None;
if let (Some(by_cell), Some(fc)) = (rich_cell_pictures.get(&i), cells.as_ref()) {
let nrows = rows.len();
let ncols = rows.iter().map(Vec::len).max().unwrap_or(0);
let mut blocks = vec![vec![Vec::<Node>::new(); ncols]; nrows];
for (cell_idx, pics) in by_cell {
let cell = &fc[*cell_idx];
let (r, c) = (cell.start_row, cell.start_col);
if r >= nrows || c >= ncols {
continue;
}
let mut parts: Vec<String> = Vec::new();
let mut cell_nodes: Vec<Node> = Vec::new();
if !cell.text.trim().is_empty() {
parts.push(cell.text.clone());
cell_nodes.push(Node::Paragraph {
text: cell.text.clone(),
});
}
for &p in pics {
parts.push("<!-- image -->".to_string());
let classification = match &enrichments[p] {
Some(Enrichment::PictureClasses(classes)) => Some(classes.clone()),
_ => None,
};
#[cfg(feature = "ocr-prep")]
let image = crop_region(page, ®ions[p], picture_scale);
#[cfg(not(feature = "ocr-prep"))]
let image: Option<PictureImage> = None;
cell_nodes.push(located(
norm_loc(®ions[p], page.width, page_h),
Node::Picture {
caption: None,
caption_href: None,
image,
classification,
description: None,
caption_parent: Default::default(),
caption_location: None,
},
));
}
let rendered = parts.join(" ");
for row in rows.iter_mut().skip(r).take(cell.row_span) {
for slot in row.iter_mut().skip(c).take(cell.col_span) {
*slot = rendered.clone();
}
}
blocks[r][c] = cell_nodes;
}
cell_blocks = Some(blocks);
}
nodes.push(located(
loc,
Node::Table(Table {
footnotes: Vec::new(),
rows,
location: None,
structure,
cell_blocks,
cells,
caption,
caption_parent: CaptionParent::Item,
caption_location,
document_index: region.label == "document_index",
}),
));
}
"formula" => {
let latex = match &enrichments[i] {
Some(Enrichment::Formula { latex }) => latex.clone(),
_ => String::new(),
};
nodes.push(Node::Formula {
latex,
orig: text.clone(),
location: Some(loc),
});
}
"code" => {
let code = code_region_text(region, &page.code_cells);
let code = if code.is_empty() {
tighten_code_punct(&text)
} else {
code
};
let flat = code
.lines()
.map(str::trim)
.filter(|l| !l.is_empty())
.collect::<Vec<_>>()
.join(" ");
let node = match &enrichments[i] {
Some(Enrichment::Code {
language,
text: enriched,
}) => Node::Code {
language: language.clone(),
text: enriched.clone(),
orig: Some(flat),
pretty: None,
},
_ => Node::Code {
language: None,
text: flat,
orig: None,
pretty: Some(code),
},
};
nodes.push(located(loc, node));
if let Some(ci) = code_caption_for[i] {
let cap = md_escape(®ion_texts[ci]);
if !cap.is_empty() {
nodes.push(located(
norm_loc(®ions[ci], page.width, page_h),
Node::Paragraph { text: cap },
));
}
}
}
"caption" => nodes.push(located(
loc,
Node::Caption {
text: md_escape(&text),
href: None,
},
)),
_ => {
let escaped = md_escape(&text);
let hyperlink = (region.label == "footnote")
.then(|| region_hyperlink(region, &page.links))
.flatten();
if let Some(uri) = &hyperlink {
links.retain(|(anchor, href)| {
!(href == uri && region_texts[i].contains(anchor.as_str()))
});
}
let node = if region.label == "footnote" {
Node::LabeledText {
label: "footnote".into(),
text: escaped,
href: hyperlink,
}
} else {
let mut escaped = escaped;
if wraps[i] {
escaped.push('\u{ad}');
}
Node::Paragraph { text: escaped }
};
nodes.push(located(loc, node))
}
}
}
node_in_container.resize(nodes.len(), last_in_container);
continue_lists_past_asides(&mut nodes, &node_in_container);
if page.rotation != 0 {
rotate_nodes_to_display(&mut nodes, page.rotation);
}
(nodes, links)
}
enum ListRole {
Item,
Aside,
Closes,
}
fn list_role(n: &Node) -> ListRole {
let n = match n {
Node::Located { inner, .. } => inner.as_ref(),
other => other,
};
match n {
Node::ListItem { .. } => ListRole::Item,
Node::Picture { .. }
| Node::PictureChildren(_)
| Node::Chart { .. }
| Node::Table(_)
| Node::Code { .. } => ListRole::Aside,
_ => ListRole::Closes,
}
}
fn continue_lists_past_asides(nodes: &mut Vec<Node>, in_container: &[bool]) {
let mut out = Vec::with_capacity(nodes.len());
let mut held = Vec::new();
let mut open = false;
for (n, &contained) in std::mem::take(nodes).into_iter().zip(in_container) {
match list_role(&n) {
ListRole::Item => {
open = true;
out.push(n);
}
ListRole::Aside if open && !contained => held.push(n),
ListRole::Aside if !contained => out.push(n),
ListRole::Aside | ListRole::Closes => {
open = false;
out.append(&mut held);
out.push(n);
}
}
}
out.append(&mut held);
*nodes = out;
}
fn picture_child_node(region: &Region, text: &str, loc: [u16; 4]) -> Option<Node> {
if text.is_empty() {
return None;
}
Some(match region.label {
"title" | "section_header" => located(
loc,
Node::Heading {
level: 2,
text: md_escape(text),
},
),
"list_item" => list_item_node(text, loc, true),
"page_header" | "page_footer" => Node::PageFurniture {
footer: region.label == "page_footer",
location: loc,
text: md_escape(text),
},
"caption" => located(
loc,
Node::Caption {
text: md_escape(text),
href: None,
},
),
_ => located(
loc,
Node::Paragraph {
text: md_escape(text),
},
),
})
}
fn rot_loc_cw(l: [u16; 4]) -> [u16; 4] {
[511 - l[3], l[0], 511 - l[1], l[2]]
}
fn rotate_nodes_to_display(nodes: &mut [Node], rot: u16) {
let quarter_turns = (rot / 90) as usize;
let rot_loc = |l: &mut [u16; 4]| {
for _ in 0..quarter_turns {
*l = rot_loc_cw(*l);
}
};
fn walk(node: &mut Node, rot_loc: &impl Fn(&mut [u16; 4]), swap_dims: bool) {
match node {
Node::PageInfo { width, height, .. } => {
if swap_dims {
std::mem::swap(width, height);
}
}
Node::Located { location, inner } => {
rot_loc(location);
walk(inner, rot_loc, swap_dims);
}
Node::Furniture { inner, .. } => walk(inner, rot_loc, swap_dims),
Node::Group { children, .. } | Node::PictureChildren(children) => {
for c in children {
walk(c, rot_loc, swap_dims);
}
}
Node::ListItem { location, .. }
| Node::Formula { location, .. }
| Node::Chart { location, .. } => {
if let Some(l) = location {
rot_loc(l);
}
}
Node::PageFurniture { location, .. } => rot_loc(location),
Node::Table(t) => {
if let Some(l) = &mut t.location {
rot_loc(l);
}
}
_ => {}
}
}
let swap_dims = quarter_turns % 2 == 1;
for node in nodes {
walk(node, &rot_loc, swap_dims);
}
}
fn looks_like_caption(text: &str) -> bool {
let head: String = text.trim_start().chars().take(14).collect();
(head.starts_with("Fig") || head.starts_with("Table"))
&& head.contains(|c: char| c.is_ascii_digit())
}
fn paragraph_is_open(text: &str) -> bool {
let t = text.trim_end();
t.chars().count() >= 2
&& t.chars()
.next_back()
.is_some_and(|c| matches!(c, 'a'..='z' | ',' | '-' | '\u{ad}'))
}
fn as_paragraph(n: &Node) -> Option<&str> {
match n {
Node::Paragraph { text } => Some(text),
Node::Located { inner, .. } => match inner.as_ref() {
Node::Paragraph { text } => Some(text),
_ => None,
},
_ => None,
}
}
fn is_picture_node(n: &Node) -> bool {
match n {
Node::Picture { .. } => true,
Node::Located { inner, .. } => matches!(inner.as_ref(), Node::Picture { .. }),
_ => false,
}
}
fn is_merge_trailer(n: &Node) -> bool {
is_picture_node(n)
|| matches!(
n,
Node::PageFurniture { .. }
| Node::PageInfo { .. }
| Node::ProvContinuation { .. }
| Node::Table(_)
| Node::PictureChildren(_)
)
|| matches!(n, Node::Located { inner, .. } if matches!(inner.as_ref(), Node::Table(_)))
|| is_footnote_node(n)
|| as_paragraph(n).is_some_and(looks_like_caption)
}
fn is_footnote_node(n: &Node) -> bool {
let n = match n {
Node::Located { inner, .. } => inner.as_ref(),
other => other,
};
matches!(n, Node::LabeledText { label, .. } if label == "footnote")
}
fn reparagraph(node: &Node, text: String) -> Node {
match node {
Node::Located { location, .. } => located(*location, Node::Paragraph { text }),
_ => Node::Paragraph { text },
}
}
fn starts_with_conjunction(text: &str) -> bool {
let word: String = text
.chars()
.take_while(|c| c.is_alphabetic())
.collect::<String>()
.to_lowercase();
matches!(
word.as_str(),
"und" | "oder" | "bzw" | "sowie" | "and" | "or" | "et" | "ou" | "en" | "y"
)
}
fn strip_wrap_marks(nodes: &mut [Node]) {
for n in nodes {
let n = match n {
Node::Located { inner, .. } => inner.as_mut(),
other => other,
};
if let Node::Paragraph { text } = n {
if text.ends_with('\u{ad}') {
text.pop();
}
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Continuations {
AcrossPages,
Lines,
}
pub(crate) fn merge_continuations(nodes: &mut Vec<Node>, how: Continuations) {
merge_open_paragraphs(nodes, how);
strip_wrap_marks(nodes);
}
fn merge_open_paragraphs(nodes: &mut Vec<Node>, how: Continuations) {
let mut i = 0;
while i + 1 < nodes.len() {
let Some(a) = as_paragraph(&nodes[i]) else {
i += 1;
continue;
};
if looks_like_caption(a) {
i += 1;
continue;
}
if !paragraph_is_open(a) {
i += 1;
continue;
}
let mut j = i + 1;
let mut new_page = false;
while nodes.get(j).is_some_and(is_merge_trailer) {
new_page |= matches!(nodes[j], Node::PageInfo { .. });
j += 1;
}
if how == Continuations::AcrossPages && !new_page {
i += 1;
continue;
}
let cont = nodes.get(j).and_then(as_paragraph).is_some_and(|b| {
b.trim_start()
.chars()
.next()
.is_some_and(char::is_lowercase)
});
if cont {
let a = as_paragraph(&nodes[i]).unwrap().trim_end().to_string();
let b = as_paragraph(&nodes[j]).unwrap().trim_start().to_string();
let elliptical = !a.ends_with('\u{ad}') && starts_with_conjunction(&b);
let merged = match a.strip_suffix('\u{ad}').or_else(|| a.strip_suffix('-')) {
Some(stem) if !elliptical => format!("{stem}{b}"),
_ => format!("{a} {b}"),
};
let marker = match &nodes[j] {
Node::Located { location, .. }
if new_page && matches!(nodes[i], Node::Located { .. }) =>
{
let head = merged.strip_suffix(b.as_str()).unwrap_or(&merged);
let page_skip = nodes[i + 1..j]
.iter()
.filter(|n| matches!(n, Node::PageInfo { .. }))
.count();
Some(Node::ProvContinuation {
location: *location,
prev_end: docling_core::text_char_len(head.trim_end()),
span: [
docling_core::text_char_len(head),
docling_core::text_char_len(&merged),
],
page_skip,
})
}
_ => None,
};
nodes[i] = reparagraph(&nodes[i], merged);
nodes.remove(j);
if let Some(marker) = marker {
let at = i
+ 1
+ nodes[i + 1..]
.iter()
.take_while(|n| matches!(n, Node::ProvContinuation { .. }))
.count();
if let (
Node::ProvContinuation { prev_end, .. },
Some(Node::ProvContinuation { span, .. }),
) = (&marker, nodes.get_mut(at - 1))
{
span[1] = *prev_end;
}
nodes.insert(at, marker);
}
} else {
i += 1;
}
}
}
fn hold_start(nodes: &[Node]) -> usize {
for k in (0..nodes.len()).rev() {
if is_merge_trailer(&nodes[k]) {
continue;
}
match as_paragraph(&nodes[k]) {
Some(text) if paragraph_is_open(text) => return k,
_ => return nodes.len(),
}
}
nodes.len()
}
pub(crate) struct StreamAssembler {
pending: Vec<Node>,
how: Continuations,
}
impl StreamAssembler {
pub(crate) fn new(how: Continuations) -> Self {
Self {
pending: Vec::new(),
how,
}
}
pub(crate) fn push(&mut self, mut nodes: Vec<Node>) -> Vec<Node> {
self.pending.append(&mut nodes);
merge_open_paragraphs(&mut self.pending, self.how);
let cut = hold_start(&self.pending);
let tail = self.pending.split_off(cut);
let mut done = std::mem::replace(&mut self.pending, tail);
strip_wrap_marks(&mut done);
done
}
pub(crate) fn finish(mut self) -> Vec<Node> {
strip_wrap_marks(&mut self.pending);
self.pending
}
}
#[cfg(test)]
mod tests {
use super::{
cells_text, clean_text, is_note_of, looks_like_note_call, merge_overlapping_regulars,
reclaim_heading_body_footers,
};
#[test]
fn full_page_pictures_are_dropped_like_docling() {
use super::drop_full_page_pictures;
use crate::layout::Region;
let region = |label: &'static str, l, t, r, b| Region {
label,
score: 0.99,
l,
t,
r,
b,
};
let mut regions = vec![
region("picture", 0.0, 0.5, 478.9, 241.8),
region("picture", 10.0, 10.0, 400.0, 200.0),
region("table", 0.0, 0.0, 480.0, 243.0),
region("text", 5.0, 5.0, 100.0, 20.0),
];
drop_full_page_pictures(&mut regions, 480.75, 243.75);
let labels: Vec<_> = regions.iter().map(|r| (r.label, r.l)).collect();
assert_eq!(
labels,
vec![("picture", 10.0), ("table", 0.0), ("text", 5.0)]
);
}
use super::{
code_region_text, float_running_head, merge_continuations, resolve_link_anchors,
Continuations, StreamAssembler,
};
use crate::layout::Region;
use crate::pdfium_backend::{LinkAnnot, PdfPage, TextCell};
use docling_core::Node;
#[test]
fn layout_cell_coverage_counts_claimed_text_cells() {
let cell = |text: &str, l: f32, t: f32| TextCell {
text: text.into(),
l,
t,
r: l + 40.0,
b: t + 10.0,
};
let region = Region {
label: "text",
score: 0.9,
l: 0.0,
t: 0.0,
r: 100.0,
b: 50.0,
};
let cells = vec![
cell("inside", 10.0, 10.0),
cell("also inside", 10.0, 30.0),
cell("outside", 10.0, 200.0),
cell(" ", 10.0, 210.0), ];
let cov = super::layout_cell_coverage(std::slice::from_ref(®ion), &cells);
assert!((cov - 2.0 / 3.0).abs() < 1e-6, "got {cov}");
assert_eq!(super::layout_cell_coverage(&[], &[]), 1.0);
assert_eq!(super::layout_cell_coverage(&[], &cells), 0.0);
}
#[test]
fn border_straddlers_are_page_text_picture_interior_is_a_picture_child() {
let pic = Region {
label: "picture",
score: 0.9,
l: 0.0,
t: 0.0,
r: 100.0,
b: 100.0,
};
let straddler = TextCell {
text: "axis label".into(),
l: 90.0,
t: 40.0,
r: 120.0,
b: 48.0,
};
let interior = TextCell {
text: "in-figure callout".into(),
l: 10.0,
t: 10.0,
r: 60.0,
b: 18.0,
};
let cells = vec![straddler, interior];
let mut regions = vec![pic];
super::add_orphan_regions(&mut regions, &cells);
super::drop_contained_regulars(&mut regions);
assert_eq!(
regions.iter().filter(|r| r.label == "text").count(),
2,
"both unclaimed lines become orphans, and a picture swallows neither"
);
let parents = super::picture_parents(®ions);
let parent_of = |l: f32| {
regions
.iter()
.zip(&parents)
.find(|(r, _)| r.label == "text" && r.l == l)
.and_then(|(_, p)| *p)
};
assert_eq!(
parent_of(10.0),
Some(0),
"the callout is the picture's child"
);
assert_eq!(parent_of(90.0), None, "the straddler is a page element");
let page = PdfPage::from_cells(200.0, 200.0, 2.0, cells);
let n = regions.len();
let (nodes, _) = super::assemble_page(&page, regions, &vec![None; n], &vec![None; n], None);
let children: Vec<&Node> = nodes
.iter()
.filter_map(|n| match n {
Node::PictureChildren(c) => Some(c),
_ => None,
})
.flatten()
.collect();
assert!(
matches!(children.as_slice(), [Node::Located { inner, .. }]
if matches!(inner.as_ref(), Node::Paragraph { text } if text == "in-figure callout")),
"{children:?}"
);
let mut doc = docling_core::DoclingDocument::new("t");
doc.nodes = nodes;
let md = doc.export_to_markdown();
assert!(md.contains("axis label"), "{md}");
assert!(!md.contains("in-figure callout"), "{md}");
let json = doc.export_to_json_value();
let pic = &json["pictures"][0];
let child = pic["children"][0]["$ref"].as_str().expect("a child ref");
let idx: usize = child.rsplit('/').next().unwrap().parse().unwrap();
assert_eq!(json["texts"][idx]["text"], "in-figure callout");
assert_eq!(json["texts"][idx]["parent"]["$ref"], "#/pictures/0");
assert_eq!(json["texts"][idx]["content_layer"], "body");
}
#[test]
fn picture_inside_a_table_region_survives_the_containment_drop() {
let mut regions = vec![
region("table", 0.9, 0.0, 0.0, 200.0, 200.0),
region("picture", 0.9, 20.0, 20.0, 120.0, 120.0),
region("text", 0.9, 20.0, 140.0, 180.0, 180.0),
];
super::drop_contained_regulars(&mut regions);
let labels: Vec<&str> = regions.iter().map(|r| r.label).collect();
assert_eq!(
labels,
["table", "picture"],
"the in-table picture stays; the in-table regular is the special's child"
);
}
#[test]
fn captions_pair_by_reading_order_adjacency() {
let regions = vec![
region("text", 0.9, 0.0, 0.0, 100.0, 10.0), region("caption", 0.9, 0.0, 12.0, 60.0, 20.0), region("table", 0.9, 20.0, 22.0, 90.0, 60.0), region("table", 0.9, 0.0, 70.0, 100.0, 110.0), region("caption", 0.9, 0.0, 112.0, 60.0, 120.0), region("text", 0.9, 0.0, 130.0, 100.0, 140.0), region("caption", 0.9, 0.0, 142.0, 60.0, 150.0), region("text", 0.9, 0.0, 152.0, 100.0, 162.0), region("table", 0.9, 0.0, 170.0, 100.0, 200.0), region("caption", 0.9, 0.0, 202.0, 60.0, 210.0), region("table", 0.9, 0.0, 212.0, 100.0, 240.0), region("page_footer", 0.9, 0.0, 250.0, 100.0, 260.0), ];
let pairs = super::pair_graphic_captions(®ions);
assert_eq!(pairs[2], Some(1), "caption directly above its table pairs");
assert_eq!(pairs[3], Some(4), "caption directly below its table pairs");
assert!(
!pairs.contains(&Some(6)),
"a text paragraph between caption and table breaks the bond"
);
assert_eq!(pairs[8], Some(9), "equal gaps go to the preceding graphic");
assert_eq!(pairs[10], None);
assert!(
pairs.iter().filter(|p| p.is_some()).count() == 3,
"{pairs:?}"
);
}
#[test]
fn a_caption_above_its_picture_binds() {
let regions = vec![
region("section_header", 0.9, 40.0, 200.0, 300.0, 215.0),
region("text", 0.9, 40.0, 220.0, 500.0, 250.0),
region("caption", 0.93, 40.0, 255.0, 287.0, 266.0), region("picture", 0.98, 118.0, 277.0, 423.0, 469.0), region("text", 0.9, 40.0, 480.0, 500.0, 520.0),
region("code", 0.9, 40.0, 530.0, 500.0, 600.0), region("caption", 0.9, 40.0, 604.0, 300.0, 614.0), ];
let pairs = super::pair_graphic_captions(®ions);
assert_eq!(pairs[3], Some(2));
assert_eq!(pairs[5], Some(6));
}
#[test]
fn captions_rank_by_gap_and_displace_rehousable_claims() {
let regions = vec![
region("picture", 0.9, 0.0, 0.0, 100.0, 50.0), region("picture", 0.9, 0.0, 60.0, 100.0, 110.0), region("caption", 0.9, 0.0, 112.0, 100.0, 120.0), ];
let pairs = super::pair_graphic_captions(®ions);
assert_eq!(pairs, vec![None, Some(2), None]);
let regions = vec![
region("caption", 0.9, 0.0, 0.0, 100.0, 8.0), region("picture", 0.9, 0.0, 10.0, 100.0, 50.0), region("picture", 0.9, 0.0, 60.0, 100.0, 110.0), region("caption", 0.9, 0.0, 112.0, 100.0, 120.0), ];
let pairs = super::pair_graphic_captions(®ions);
assert_eq!(pairs, vec![None, Some(0), Some(3), None]);
let regions = vec![
region("picture", 0.9, 0.0, 0.0, 100.0, 50.0), region("caption", 0.9, 0.0, 70.0, 100.0, 78.0), region("picture", 0.9, 0.0, 80.0, 100.0, 120.0), region("caption", 0.9, 0.0, 122.0, 100.0, 130.0), ];
let pairs = super::pair_graphic_captions(®ions);
assert_eq!(pairs, vec![Some(1), None, Some(3), None]);
let regions = vec![
region("caption", 0.9, 0.0, 0.0, 100.0, 8.0), region("picture", 0.9, 0.0, 10.0, 100.0, 50.0), region("caption", 0.9, 0.0, 52.0, 100.0, 60.0), ];
let pairs = super::pair_graphic_captions(®ions);
assert_eq!(pairs, vec![None, Some(0), None]);
}
#[test]
fn caption_labels_are_recognized() {
for t in [
"Table 5: Prediction Performance",
"Figure 2.1: Surveyed MSMEs",
"Fig. 3: Overview",
"Table 1. Pour volume by month (m3)",
" Figure 12a. Left panel",
"Listing 4: main.rs",
] {
assert!(super::is_caption_label(t), "{t}");
}
for t in [
"Table 5 shows the results",
"Tables 5: and 6:",
"Figure",
"Figure: no number",
"table 5: lowercase",
"Table : no number",
] {
assert!(!super::is_caption_label(t), "{t}");
}
}
#[test]
fn a_labelled_text_block_beside_a_graphic_becomes_a_caption() {
let cells = vec![
cell(
"Table 5: Prediction Performance of a network",
54.0,
87.0,
296.0,
100.0,
),
cell("across the datasets.", 54.0, 100.0, 200.0, 113.0),
cell("In this section we discuss", 54.0, 400.0, 296.0, 412.0),
];
let mut regions = vec![
region("text", 0.99, 54.0, 87.0, 296.0, 150.0),
region("table", 0.99, 73.0, 172.0, 275.0, 340.0),
region("text", 0.99, 54.0, 400.0, 296.0, 500.0),
];
super::promote_labelled_captions(&mut regions, &cells);
assert_eq!(regions[0].label, "caption");
assert_eq!(regions[2].label, "text");
let mut regions = vec![
region("text", 0.99, 54.0, 87.0, 296.0, 150.0),
region("text", 0.99, 54.0, 152.0, 296.0, 170.0),
region("table", 0.99, 73.0, 172.0, 275.0, 340.0),
];
super::promote_labelled_captions(&mut regions, &cells);
assert_eq!(regions[0].label, "text");
let cells = vec![cell("Table 5 shows the results", 54.0, 87.0, 296.0, 100.0)];
let mut regions = vec![
region("text", 0.99, 54.0, 87.0, 296.0, 150.0),
region("table", 0.99, 73.0, 172.0, 275.0, 340.0),
];
super::promote_labelled_captions(&mut regions, &cells);
assert_eq!(regions[0].label, "text");
}
#[test]
fn side_by_side_figure_captions_bind_their_own_picture() {
let regions = vec![
region("picture", 0.8, 9.0, 2.0, 367.0, 347.0), region("picture", 0.7, 423.0, 3.0, 718.0, 346.0), region("picture", 0.7, 756.0, 13.0, 1040.0, 341.0), region("caption", 0.6, 128.0, 371.0, 315.0, 392.0), region("caption", 0.6, 425.0, 371.0, 713.0, 396.0), region("caption", 0.6, 785.0, 371.0, 1069.0, 397.0), ];
let pairs = super::pair_graphic_captions(®ions);
assert_eq!(pairs[..3], [Some(3), Some(4), Some(5)]);
let regions = vec![
region("picture", 0.9, 316.0, 82.0, 564.0, 459.0),
region("text", 0.9, 400.0, 458.0, 476.0, 461.0), region("caption", 0.9, 318.0, 473.0, 559.0, 504.0),
];
assert_eq!(
super::pair_graphic_captions(®ions),
vec![None, None, None]
);
}
#[test]
fn text_panels_demote_to_paragraphs_but_charts_keep_their_crop() {
let cell = |text: &str, l: f32, t: f32, r: f32, b: f32| TextCell {
text: text.to_string(),
l,
t,
r,
b,
};
let panel = Region {
label: "picture",
score: 0.9,
l: 0.0,
t: 0.0,
r: 100.0,
b: 100.0,
};
let cells = vec![
cell(
"C.7. Wenn Sie diesen Vertrag widerrufen,",
5.0,
10.0,
95.0,
18.0,
),
cell(
"haben wir Ihnen alle Zahlungen, die wir",
5.0,
20.0,
95.0,
28.0,
),
cell(
"von Ihnen erhalten haben, zurückzuzahlen.",
5.0,
30.0,
90.0,
38.0,
),
cell(
"C.8. Wir können die Rückzahlung verweigern,",
5.0,
52.0,
95.0,
60.0,
),
cell(
"bis wir die Waren wieder zurückerhalten haben.",
5.0,
62.0,
92.0,
70.0,
),
];
let mut regions = vec![panel.clone()];
super::recover_text_panels(&mut regions, &cells);
assert_eq!(
regions.iter().map(|r| r.label).collect::<Vec<_>>(),
["text", "text"],
"dense panel must demote into one text region per paragraph"
);
assert!(regions[0].b < regions[1].t, "paragraphs split at the gap");
let labels = vec![
cell("0", 5.0, 90.0, 8.0, 95.0),
cell("50", 5.0, 50.0, 10.0, 55.0),
cell("100", 5.0, 10.0, 12.0, 15.0),
cell("t, s", 45.0, 96.0, 55.0, 100.0),
];
let mut regions = vec![panel];
super::recover_text_panels(&mut regions, &labels);
assert_eq!(
regions.iter().map(|r| r.label).collect::<Vec<_>>(),
["picture"]
);
}
#[test]
fn dense_titled_chart_keeps_its_crop() {
let cell = |text: &str, l: f32, t: f32, r: f32, b: f32| TextCell {
text: text.to_string(),
l,
t,
r,
b,
};
let chart = Region {
label: "picture",
score: 0.9,
l: 0.0,
t: 0.0,
r: 100.0,
b: 100.0,
};
let cells = vec![
cell("Underground Water Storage", 10.0, 5.0, 90.0, 17.0),
cell("aquifer recharge zone", 15.0, 30.0, 75.0, 50.0),
cell("confined | unconfined | perched", 12.0, 55.0, 80.0, 59.0),
cell("saturated thickness", 8.0, 70.0, 60.0, 90.0),
cell("distance from well, km", 20.0, 92.0, 85.0, 97.0),
];
let mut regions = vec![chart];
super::recover_text_panels(&mut regions, &cells);
assert_eq!(
regions.iter().map(|r| r.label).collect::<Vec<_>>(),
["picture"],
"ragged line heights mark a figure, not a text panel"
);
}
#[test]
fn cells_join_in_index_order_with_sanitize_text_rules() {
let cell = |text: &str, l: f32, t: f32, r: f32, b: f32| TextCell {
text: text.to_string(),
l,
t,
r,
b,
};
let region = Region {
label: "text",
score: 1.0,
l: 0.0,
t: 95.0,
r: 200.0,
b: 130.0,
};
let orcid = vec![
cell("[0000", 10.0, 100.0, 30.0, 110.0),
cell("−", 30.0, 100.0, 34.0, 110.0),
cell("0002", 34.0, 100.0, 50.0, 110.0),
cell("−", 50.0, 100.0, 54.0, 110.0),
cell("6960]", 54.0, 100.0, 70.0, 110.0),
];
assert_eq!(super::region_text(®ion, &orcid), "[0000 - 0002 - 6960]");
let wrapped = vec![
cell("platforms-", 10.0, 100.0, 60.0, 110.0),
cell("reflects the design", 10.0, 112.0, 90.0, 122.0),
];
assert_eq!(
super::region_text(®ion, &wrapped),
"platformsreflects the design"
);
let otsl = vec![
cell("–", 10.0, 100.0, 14.0, 110.0),
cell("\"C\" cell -", 16.0, 100.0, 60.0, 110.0),
cell("a new table cell", 10.0, 112.0, 80.0, 122.0),
];
assert_eq!(
super::region_text(®ion, &otsl),
"- \"C\" cell - a new table cell"
);
let numeral = vec![
cell("들어가며", 30.0, 100.0, 80.0, 110.0),
cell("1", 10.0, 98.0, 25.0, 112.0), ];
assert_eq!(super::region_text(®ion, &numeral), "들어가며 1");
}
#[test]
fn geometric_table_reads_a_tall_box_font_line_by_line() {
let cells = vec![
cell("Reagents", 77.6, 374.5, 115.5, 386.6),
cell("Supplies and Equipment", 311.4, 374.5, 411.9, 386.6),
cell("At each student station:", 77.6, 385.5, 166.2, 397.4),
cell(
"Resuspended DNA or ethanol precipitates from Part 1*",
77.6,
377.4,
288.0,
427.7,
),
cell("To be shared by all groups:", 77.6, 420.5, 178.3, 432.5),
cell("“Evidence A” DNA*", 77.6, 412.5, 150.6, 462.7),
cell("“Evidence B” DNA*", 77.6, 423.0, 150.5, 473.2),
cell("Microcentrifuge tube rack", 311.4, 366.9, 411.1, 417.1),
cell("3 1.5-mL microcentrifuge tubes", 311.4, 377.4, 437.0, 427.7),
cell("Micropipet tips", 311.4, 388.0, 370.2, 438.2),
cell(
"Beaker or similar container for waste",
311.4,
398.5,
454.5,
448.8,
),
cell(
"Beaker or similar container filled with ice",
311.4,
409.1,
470.6,
459.3,
),
];
let table = region("table", 0.99, 71.4, 366.0, 539.9, 488.8);
let grid = super::reconstruct_table(&table, &cells);
let column = |ci: usize| -> Vec<&str> {
grid.iter()
.map(|r| r[ci].as_str())
.filter(|c| !c.is_empty())
.collect()
};
assert_eq!(
column(0),
[
"Reagents",
"At each student station:",
"Resuspended DNA or ethanol precipitates from Part 1*",
"To be shared by all groups:",
"“Evidence A” DNA*",
"“Evidence B” DNA*",
],
"{grid:?}"
);
assert_eq!(
column(1),
[
"Supplies and Equipment",
"Microcentrifuge tube rack",
"3 1.5-mL microcentrifuge tubes",
"Micropipet tips",
"Beaker or similar container for waste",
"Beaker or similar container filled with ice",
],
"{grid:?}"
);
assert_eq!(grid[0], ["Reagents", "Supplies and Equipment"]);
let honest = cell("Supplies and Equipment", 311.4, 374.5, 411.9, 386.6);
assert_eq!(super::line_height(&honest), 386.6 - 374.5);
assert_eq!(super::line_top(&honest), 374.5);
assert!(super::geometric_table_is_reliable(&grid), "{grid:?}");
assert!(!super::geometric_rows_share_lines(&table, &cells));
}
#[test]
fn rows_on_one_line_are_trusted() {
let mut cells = Vec::new();
for (i, (label, a, b)) in [
("Model", "Simple", "Complex"),
("Tabula", "78.0", "57.8"),
("Camelot", "80.0", "66.0"),
("TableFormer", "95.4", "90.1"),
]
.into_iter()
.enumerate()
{
let y = 100.0 + 14.0 * i as f32;
cells.push(cell(
label,
60.0,
y,
60.0 + 5.0 * label.len() as f32,
y + 12.1,
));
cells.push(cell(
a,
160.0,
y + 0.9,
160.0 + 5.0 * a.len() as f32,
y + 12.1,
));
cells.push(cell(
b,
230.0,
y + 0.9,
230.0 + 5.0 * b.len() as f32,
y + 12.1,
));
}
let table = region("table", 0.99, 55.0, 95.0, 290.0, 160.0);
let grid = super::reconstruct_table(&table, &cells);
assert_eq!(grid.len(), 4, "{grid:?}");
assert_eq!(grid[3], ["TableFormer", "95.4", "90.1"]);
assert!(super::geometric_table_is_reliable(&grid));
assert!(super::geometric_rows_share_lines(&table, &cells));
}
#[test]
fn geometric_reliability_rejects_split_column_grids() {
let g = |rows: &[&[&str]]| -> Vec<Vec<String>> {
rows.iter()
.map(|r| r.iter().map(|c| c.to_string()).collect())
.collect()
};
assert!(super::geometric_table_is_reliable(&g(&[
&["Datum", "Leistung", "Anzahl", "Kosten"],
&["04.07", "Internet", "1", "40.30"],
&["04.07", "Telefon", "2", "8.06"],
])));
assert!(!super::geometric_table_is_reliable(&g(&[
&["www.magenta.at/faq", "", "", ""],
&["Serviceteam", "", "", ""],
&["Telefon", "0676/2000", "", ""],
&["Kundennummer", "", "", "1.21699482"],
&["Rechnungsnummer", "", "922769430725", ""],
&["Rechnungsdatum", "", "", "04.07.2025"],
])));
assert!(!super::geometric_table_is_reliable(&g(&[
&["a", "b", ""],
&["c", "d", ""],
&["e", "f", "g"],
])));
assert!(!super::geometric_table_is_reliable(&g(&[&[
"only one column"
]])));
assert!(!super::geometric_table_is_reliable(&[]));
}
#[test]
fn a_formula_region_is_a_formula_item_with_or_without_enrichment() {
let orig = "mf (y) = ∫ U p(y | u)f(u) dµ(u), y ∈ Y , (1)";
let cells = vec![
cell("An introductory sentence.", 100.0, 380.0, 400.0, 392.0),
cell(orig, 193.0, 426.0, 523.0, 453.0),
];
let regions = || {
vec![
region("text", 0.9, 99.0, 379.0, 401.0, 393.0),
region("formula", 0.9, 192.0, 425.0, 524.0, 454.0),
]
};
let convert = |enrichment: Option<super::Enrichment>| {
let page = PdfPage::from_cells(612.0, 792.0, 2.0, cells.clone());
let (mut nodes, _) =
super::assemble_page(&page, regions(), &[None, None], &[None, enrichment], None);
for n in &mut nodes {
if let Node::PageInfo { page_no, .. } = n {
*page_no = 2;
}
}
let mut doc = docling_core::DoclingDocument::new("t");
doc.nodes = nodes;
doc
};
let doc = convert(None);
assert!(
doc.nodes.iter().any(|n| matches!(
n,
Node::Formula { latex, orig: o, location: Some(_) } if latex.is_empty() && o == orig
)),
"{:?}",
doc.nodes
);
assert_eq!(
doc.export_to_markdown().trim(),
"An introductory sentence.\n\n<!-- formula-not-decoded -->"
);
let json = doc.export_to_json_value();
let texts = json["texts"].as_array().unwrap();
assert!(
texts
.iter()
.all(|t| !t["text"].as_str().unwrap().contains("formula-not-decoded")),
"the placeholder is Markdown, not an item's text"
);
let item = texts.iter().find(|t| t["label"] == "formula").unwrap();
assert_eq!(item["text"], "");
assert_eq!(item["orig"], orig);
let prov = &item["prov"][0];
assert_eq!(prov["page_no"], 2);
assert_eq!(prov["charspan"][1], orig.chars().count());
let bbox = &prov["bbox"];
assert_eq!(bbox["coord_origin"], "BOTTOMLEFT");
for (key, want) in [("l", 193.0), ("r", 523.0), ("t", 366.0), ("b", 339.0)] {
let got = bbox[key].as_f64().unwrap();
assert!((got - want).abs() < 2.0, "{key}: {got} vs {want}");
}
let doc = convert(Some(super::Enrichment::Formula {
latex: "m_f(y) = \\int_U p(y|u) f(u)\\,d\\mu(u)".into(),
}));
assert!(doc.export_to_markdown().contains("$$m_f(y) = \\int_U"));
let json = doc.export_to_json_value();
let item = json["texts"]
.as_array()
.unwrap()
.iter()
.find(|t| t["label"] == "formula")
.unwrap();
assert!(item["text"].as_str().unwrap().starts_with("m_f(y)"));
assert_eq!(item["orig"], orig);
assert_eq!(item["prov"][0]["charspan"][1], orig.chars().count());
}
#[test]
fn a_table_without_text_keeps_its_place_and_its_caption() {
let caption_text = "Table 1 - Decision matrix for risk insurance";
let cells = vec![
cell(
"Think about the consequences before you act.",
57.0,
560.0,
407.0,
572.0,
),
cell(caption_text, 192.0, 631.0, 403.0, 643.0),
];
let regions = || {
vec![
region("text", 0.9, 57.0, 559.0, 408.0, 573.0),
region("caption", 0.88, 192.0, 631.0, 403.0, 643.0),
region("table", 0.98, 59.0, 654.0, 560.0, 785.0),
region("text", 0.9, 57.0, 300.0, 400.0, 320.0),
]
};
let blank_grid = super::TableGrid {
rows: vec![vec![String::new(); 4]; 4],
cells: (0..16)
.map(|i| docling_core::TableCell {
text: String::new(),
bbox: Some([60.0, 655.0, 185.0, 687.0]),
start_row: i / 4,
start_col: i % 4,
row_span: 1,
col_span: 1,
column_header: i < 4,
row_header: false,
row_section: false,
})
.collect(),
};
for grid in [Some(blank_grid), None] {
let page = PdfPage::from_cells(595.0, 842.0, 2.0, cells.clone());
let (nodes, _) = super::assemble_page(
&page,
regions(),
&[None, None, grid, None],
&[None, None, None, None],
None,
);
let tables: Vec<&docling_core::Table> = nodes
.iter()
.filter_map(|n| match n {
Node::Located { inner, .. } => match &**inner {
Node::Table(t) => Some(t),
_ => None,
},
_ => None,
})
.collect();
assert_eq!(tables.len(), 1, "the table is an item: {nodes:?}");
let table = tables[0];
assert!(table.rows.is_empty(), "no rows: {:?}", table.rows);
assert!(table.cells.as_ref().is_none_or(Vec::is_empty));
assert_eq!(table.caption.as_deref(), Some(caption_text));
assert!(table.caption_location.is_some());
let mut doc = docling_core::DoclingDocument::new("t");
doc.nodes = nodes;
let md = doc.export_to_markdown();
assert_eq!(
md.trim(),
format!("Think about the consequences before you act.\n\n{caption_text}"),
"the caption prints, the empty grid does not"
);
let json = doc.export_to_json_value();
let t = &json["tables"][0];
assert_eq!(t["data"]["num_rows"], 0);
assert_eq!(t["data"]["num_cols"], 0);
assert_eq!(t["data"]["table_cells"].as_array().map(Vec::len), Some(0));
assert_eq!(t["captions"].as_array().map(Vec::len), Some(1));
let caption = json["texts"]
.as_array()
.unwrap()
.iter()
.find(|x| x["label"] == "caption")
.expect("a caption item");
assert_eq!(caption["text"], caption_text);
assert_eq!(caption["parent"]["$ref"], "#/tables/0");
}
let mut cells = cells;
cells.push(cell("Factors", 70.0, 660.0, 120.0, 672.0));
cells.push(cell("Insurance", 330.0, 660.0, 400.0, 672.0));
let page = PdfPage::from_cells(595.0, 842.0, 2.0, cells);
let (nodes, _) = super::assemble_page(
&page,
regions(),
&[None, None, None, None],
&[None, None, None, None],
None,
);
let mut doc = docling_core::DoclingDocument::new("t");
doc.nodes = nodes;
let md = doc.export_to_markdown();
assert!(md.contains("Factors") && md.contains(caption_text), "{md}");
}
#[cfg(feature = "ocr-prep")]
#[test]
fn picture_regions_are_cropped_from_a_host_supplied_page_image() {
let mut img = image::RgbImage::new(200, 200);
for y in 100..160 {
for x in 20..120 {
img.put_pixel(x, y, image::Rgb([255, 0, 0]));
}
}
let page = PdfPage::from_cells_with_image(100.0, 100.0, 2.0, Vec::new(), img);
let region = Region {
label: "picture",
score: 0.9,
l: 10.0,
t: 50.0,
r: 60.0,
b: 80.0,
};
let (nodes, _) = super::assemble_page(&page, vec![region], &[None], &[None], None);
let image = nodes
.iter()
.find_map(|n| match n {
Node::Located { inner, .. } => match &**inner {
Node::Picture { image, .. } => image.as_ref(),
_ => None,
},
Node::Picture { image, .. } => image.as_ref(),
_ => None,
})
.expect("a picture node with cropped pixels");
assert_eq!(image.mimetype, "image/png");
assert_eq!((image.width, image.height), (100, 60), "region × scale");
assert!(!image.data.is_empty(), "PNG bytes were encoded");
}
#[test]
fn link_anchors_split_a_shared_word_cell_between_adjacent_links() {
let annot = |l: f32, r: f32, uri: &str| LinkAnnot {
l,
t: 100.0,
r,
b: 114.0,
uri: uri.into(),
};
let page = PdfPage {
width: 600.0,
height: 800.0,
scale: 2.0,
cells: Vec::new(),
code_cells: Vec::new(),
checkboxes: Vec::new(),
bitmaps: Vec::new(),
shapes: Default::default(),
word_cells: vec![cell(
"LinkedIn | GitHub | Credly",
100.0,
100.0,
360.0,
114.0,
)],
image: image::RgbImage::new(1, 1),
image_layout: None,
links: vec![
annot(100.0, 180.0, "https://l"),
annot(200.0, 260.0, "https://g"),
annot(290.0, 360.0, "https://c"),
],
rotation: 0,
keep_typography: false,
#[cfg(feature = "render")]
embedded_images: Vec::new(),
#[cfg(feature = "ml")]
pending_image: None,
};
assert_eq!(
resolve_link_anchors(&page),
vec![
("LinkedIn".to_string(), "https://l".to_string()),
("GitHub".to_string(), "https://g".to_string()),
("Credly".to_string(), "https://c".to_string()),
]
);
}
fn cell(text: &str, l: f32, t: f32, r: f32, b: f32) -> TextCell {
TextCell {
text: text.into(),
l,
t,
r,
b,
}
}
#[test]
fn merge_overlapping_regulars_collapses_a_block_over_its_lines() {
let mut regions = vec![
region("text", 0.84, 60.0, 186.0, 270.0, 198.0),
region("text", 0.80, 60.0, 160.0, 294.0, 172.0),
region("text", 0.79, 59.0, 107.0, 272.0, 119.0),
region("text", 0.52, 59.0, 107.0, 295.0, 200.0),
region("section_header", 0.77, 60.0, 71.0, 253.0, 86.0),
region("picture", 0.9, 50.0, 100.0, 300.0, 210.0),
];
merge_overlapping_regulars(&mut regions);
assert_eq!(regions.len(), 3, "{regions:?}");
let block = regions
.iter()
.find(|r| r.label == "text")
.expect("one text");
assert_eq!(
(block.l, block.t, block.r, block.b),
(59.0, 107.0, 295.0, 200.0)
);
assert!(regions.iter().any(|r| r.label == "section_header"));
assert!(regions.iter().any(|r| r.label == "picture"));
}
#[test]
fn merge_overlapping_regulars_follows_the_preference_rules() {
let mut regions = vec![
region("list_item", 0.6, 0.0, 0.0, 102.0, 20.0),
region("text", 0.9, 0.0, 0.0, 100.0, 20.0),
];
merge_overlapping_regulars(&mut regions);
assert_eq!(regions.len(), 1);
assert_eq!(regions[0].label, "list_item");
let mut regions = vec![
region("code", 0.6, 0.0, 0.0, 100.0, 100.0),
region("text", 0.9, 2.0, 2.0, 98.0, 98.0),
];
merge_overlapping_regulars(&mut regions);
assert_eq!(regions.len(), 1);
assert_eq!(regions[0].label, "code");
for order in [[0.9, 0.6], [0.6, 0.9]] {
let mut regions = vec![
region("text", order[0], 0.0, 0.0, 100.0, 20.0),
region("text", order[1], 0.0, 0.0, 105.0, 21.0),
];
merge_overlapping_regulars(&mut regions);
assert_eq!(regions.len(), 1);
assert_eq!(regions[0].score, 0.9, "the confident twin wins");
assert_eq!(
(regions[0].r, regions[0].b),
(105.0, 21.0),
"on the union box"
);
}
let mut regions = vec![
region("text", 0.9, 0.0, 0.0, 100.0, 20.0),
region("text", 0.9, 0.0, 22.0, 100.0, 42.0),
];
merge_overlapping_regulars(&mut regions);
assert_eq!(regions.len(), 2);
}
#[test]
fn footer_under_a_body_less_heading_is_its_text() {
let mut regions = vec![
region("list_item", 0.9, 66.0, 754.0, 353.0, 765.0),
region("section_header", 0.94, 43.0, 773.7, 89.5, 783.4),
region("page_footer", 0.88, 43.0, 798.5, 527.8, 808.7),
];
reclaim_heading_body_footers(&mut regions, 595.28);
assert_eq!(regions[2].label, "text");
assert_eq!(regions[1].label, "section_header");
let mut regions = vec![
region("section_header", 0.9, 43.0, 700.0, 120.0, 710.0),
region("text", 0.9, 43.0, 714.0, 520.0, 780.0),
region("page_footer", 0.9, 43.0, 798.0, 520.0, 808.0),
];
reclaim_heading_body_footers(&mut regions, 595.28);
assert_eq!(regions[2].label, "page_footer");
let mut regions = vec![
region("section_header", 0.9, 43.0, 773.0, 120.0, 783.0),
region("page_footer", 0.9, 280.0, 798.0, 300.0, 808.0),
];
reclaim_heading_body_footers(&mut regions, 595.28);
assert_eq!(regions[1].label, "page_footer");
let mut regions = vec![
region("section_header", 0.9, 43.0, 700.0, 120.0, 710.0),
region("page_footer", 0.9, 43.0, 798.0, 520.0, 808.0),
];
reclaim_heading_body_footers(&mut regions, 595.28);
assert_eq!(regions[1].label, "page_footer");
}
fn region(label: &'static str, score: f32, l: f32, t: f32, r: f32, b: f32) -> Region {
Region {
label,
score,
l,
t,
r,
b,
}
}
#[test]
fn document_index_and_unpaired_caption_keep_their_labels() {
let cells = vec![
cell("Intro", 20.0, 20.0, 60.0, 28.0),
cell("1", 160.0, 20.0, 170.0, 28.0),
cell("Body text.", 20.0, 60.0, 100.0, 68.0),
cell(
"Figure 14: Example with multi-line text.",
20.0,
120.0,
180.0,
128.0,
),
];
let regions = vec![
region("document_index", 0.9, 18.0, 18.0, 182.0, 30.0),
region("text", 0.9, 18.0, 58.0, 102.0, 70.0),
region("caption", 0.9, 18.0, 118.0, 182.0, 130.0),
];
let page = PdfPage::from_cells(200.0, 200.0, 2.0, cells);
let n = regions.len();
let (nodes, _) = super::assemble_page(&page, regions, &vec![None; n], &vec![None; n], None);
let mut doc = docling_core::DoclingDocument::new("t");
doc.nodes = nodes;
let json = doc.export_to_json_value();
assert_eq!(json["tables"][0]["label"], "document_index", "{json}");
let caption = json["texts"]
.as_array()
.unwrap()
.iter()
.find(|t| t["text"] == "Figure 14: Example with multi-line text.")
.expect("the caption item");
assert_eq!(caption["label"], "caption", "{caption}");
assert_eq!(caption["parent"]["$ref"], "#/body", "{caption}");
let md = doc.export_to_markdown();
assert!(
md.ends_with("Body text.\n\nFigure 14: Example with multi-line text.\n"),
"{md}"
);
}
#[test]
fn resolve_collapses_nested_code_keeping_the_larger_box() {
let tight = region("code", 0.95, 78.0, 292.0, 300.0, 330.0);
let wide = region("code", 0.66, 63.0, 260.0, 320.0, 346.0);
let kept = super::resolve(vec![tight, wide]);
assert_eq!(kept.len(), 1, "nested code boxes must collapse to one");
assert!(
kept[0].l == 63.0 && kept[0].b == 346.0,
"the larger containing box is kept"
);
}
#[test]
fn resolve_keeps_distinct_and_differently_typed_regions() {
let text = region("text", 0.95, 90.0, 210.0, 200.0, 230.0);
let table = region("table", 0.60, 80.0, 200.0, 400.0, 500.0);
assert_eq!(super::resolve(vec![text, table]).len(), 2);
let code_a = region("code", 0.9, 78.0, 100.0, 300.0, 140.0);
let code_b = region("code", 0.9, 78.0, 300.0, 300.0, 360.0); assert_eq!(super::resolve(vec![code_a, code_b]).len(), 2);
}
#[test]
fn resolve_keeps_one_table_per_nested_group() {
let kept = super::resolve(vec![
region("table", 0.71, 26.0, 203.0, 183.0, 558.0),
region("table", 0.67, 26.0, 55.0, 183.0, 196.0),
region("table", 0.66, 196.0, 56.0, 354.0, 561.0),
region("table", 0.53, 25.0, 53.0, 354.0, 561.0),
]);
assert_eq!(kept.len(), 1, "one survivor per overlapping group");
assert_eq!((kept[0].l, kept[0].b), (25.0, 561.0));
let kept = super::resolve(vec![
region("table", 0.9, 26.0, 55.0, 183.0, 558.0),
region("table", 0.9, 196.0, 56.0, 354.0, 561.0),
]);
assert_eq!(kept.len(), 2);
}
#[test]
fn text_panel_over_a_table_does_not_repeat_its_cells() {
let lines = |t0: f32| -> Vec<TextCell> {
(0..4)
.map(|i| {
let t = t0 + 10.0 * i as f32;
cell("1 2 3 4 5 6 7 8 9 10 11 12", 5.0, t, 95.0, t + 8.0)
})
.collect()
};
let mut cells = lines(0.0);
cells.extend(lines(200.0));
let mut regions = vec![
region("picture", 0.80, 0.0, 0.0, 100.0, 45.0),
region("table", 0.62, 0.0, 0.0, 100.0, 45.0),
region("picture", 0.80, 0.0, 200.0, 100.0, 245.0),
];
super::recover_text_panels(&mut regions, &cells);
assert_eq!(
regions.iter().map(|r| r.label).collect::<Vec<_>>(),
["table", "text"]
);
assert_eq!(regions[1].t, 200.0, "the table-free panel still demotes");
}
#[test]
fn list_item_markers_split_like_docling() {
let text_of = |n: &Node| match n {
Node::ListItem {
ordered,
number,
text,
marker,
dclx,
..
} => (
*ordered,
*number,
text.clone(),
dclx.as_ref().map_or(marker.clone(), |d| d.marker.clone()),
),
other => panic!("{other:?}"),
};
let loc = [0, 0, 100, 10];
assert_eq!(
text_of(&super::list_item_node(
"- \"C\" cell - a new table cell",
loc,
false
)),
(
false,
0,
"\"C\" cell - a new table cell".into(),
Some("-".into())
)
);
assert_eq!(
text_of(&super::list_item_node("• Bullet text", loc, false)),
(false, 0, "Bullet text".into(), Some("•".into()))
);
assert_eq!(
text_of(&super::list_item_node("3. Third step", loc, false)),
(true, 3, "Third step".into(), Some("3.".into()))
);
assert_eq!(
text_of(&super::list_item_node("a) Option", loc, false)),
(false, 0, "a) Option".into(), Some("a)".into()))
);
assert_eq!(
text_of(&super::list_item_node("1.2 Nested outline", loc, false)),
(false, 0, "1.2 Nested outline".into(), Some("1.2".into()))
);
assert_eq!(
text_of(&super::list_item_node("3.a. If all IOU scores", loc, false)),
(
false,
0,
"3.a. If all IOU scores".into(),
Some("3.a.".into())
)
);
assert_eq!(
text_of(&super::list_item_node("•Glued", loc, false)),
(false, 0, "Glued".into(), Some("·".into()))
);
for bullet in ['\u{F0B7}', '\u{F0A7}', '\u{F0D8}'] {
let input = format!("{bullet} Transcribo: converts audio to text");
assert_eq!(
text_of(&super::list_item_node(&input, loc, false)),
(
false,
0,
"Transcribo: converts audio to text".into(),
Some(bullet.to_string())
)
);
assert_eq!(super::split_list_marker(&input).map(|m| m.2), Some(false));
assert_eq!(super::checkbox_glyph(bullet), None);
}
for bullet in super::PUA_FONT_BULLETS {
assert!(super::LIST_BULLET_MARKERS.contains(bullet), "{bullet:?}");
assert_eq!(
text_of(&super::list_item_node(
&format!("{bullet}Glued"),
loc,
false
)),
(false, 0, "Glued".into(), Some("·".into()))
);
}
assert_eq!(
text_of(&super::list_item_node("-5 degrees", loc, false)),
(false, 0, "-5 degrees".into(), Some("·".into()))
);
for (input, marker, body) in [
("1.2.3. Deep", "1.2.3.", "Deep"),
("9a) Nine-a", "9a)", "Nine-a"),
("(3.a) Paren", "(3.a)", "Paren"),
("12) Twelve", "12)", "Twelve"),
("(4) Four", "(4)", "Four"),
("[7] Seven", "[7]", "Seven"),
("iv. Roman", "iv.", "Roman"),
("IX. Roman", "IX.", "Roman"),
("b. Letter", "b.", "Letter"),
("B) Letter", "B)", "Letter"),
] {
assert_eq!(
super::split_list_marker(input),
Some((marker, body, true)),
"{input}"
);
}
assert_eq!(
super::split_list_marker("1.2.\tx"),
Some(("1.2.", "x", true))
);
assert_eq!(super::split_list_marker("1. "), None);
assert_eq!(super::split_list_marker("• "), None);
assert_eq!(
text_of(&super::list_item_node("Plain item", loc, false)),
(false, 0, "Plain item".into(), Some("·".into()))
);
}
#[test]
fn case_auto_marker_prints_once_in_markdown() {
let loc = [0, 0, 100, 10];
let nodes: Vec<Node> = [
"(1) Human annotation",
"[21] Peng Zhang",
"a) Option",
"• Bullet",
"3. Third",
]
.iter()
.enumerate()
.map(|(i, t)| super::list_item_node(t, loc, i == 0))
.collect();
for n in &nodes[..3] {
let Node::ListItem { marker, dclx, .. } = n else {
unreachable!()
};
assert_eq!(marker, &None, "{n:?}");
assert!(dclx.as_ref().is_some_and(|d| d.marker.is_some()), "{n:?}");
}
let mut doc = docling_core::DoclingDocument::new("t");
doc.nodes = nodes;
let md = doc.export_to_markdown();
for line in [
"- (1) Human annotation",
"- [21] Peng Zhang",
"- a) Option",
"- Bullet",
"3. Third",
] {
assert!(md.lines().any(|l| l == line), "missing {line:?} in:\n{md}");
}
assert!(!md.contains("(1) (1)") && !md.contains("[21] [21]"), "{md}");
}
#[test]
fn json_list_items_carry_the_marker_split() {
let loc = [0, 0, 100, 10];
let mut doc = docling_core::DoclingDocument::new("t");
doc.nodes = [
"[21] Peng Zhang",
"a) Option",
"3. Third",
"· Bullet",
"Plain <item>",
"•Glued",
]
.iter()
.enumerate()
.map(|(i, t)| super::list_item_node(t, loc, i == 0))
.collect();
let json = doc.export_to_json_value();
let got: Vec<(&str, &str, &str, bool)> = json["texts"]
.as_array()
.unwrap()
.iter()
.map(|t| {
(
t["orig"].as_str().unwrap(),
t["text"].as_str().unwrap(),
t["marker"].as_str().unwrap(),
t["enumerated"].as_bool().unwrap(),
)
})
.collect();
assert_eq!(
got,
vec![
("[21] Peng Zhang", "Peng Zhang", "[21]", true),
("a) Option", "Option", "a)", true),
("3. Third", "Third", "3.", true),
("· Bullet", "Bullet", "·", false),
("Plain <item>", "Plain <item>", "", false),
("Glued", "Glued", "", false),
]
);
let md = doc.export_to_markdown();
for line in ["- [21] Peng Zhang", "- a) Option", "3. Third", "- Bullet"] {
assert!(md.lines().any(|l| l == line), "missing {line:?} in:\n{md}");
}
}
#[test]
fn code_language_label_above_code_is_detected() {
let label = region("section_header", 0.9, 76.0, 540.0, 96.0, 549.0);
let code = region("code", 0.7, 77.0, 552.0, 290.0, 640.0);
let heading = region("section_header", 0.9, 76.0, 500.0, 260.0, 512.0);
let cells = vec![
cell("XML", 78.0, 541.0, 94.0, 548.0), cell("Overview", 78.0, 501.0, 250.0, 511.0), ];
let drop = super::code_language_labels(&[label, code, heading], &cells);
assert_eq!(drop, vec![true, false, false], "only the label is consumed");
let label2 = region("section_header", 0.9, 76.0, 540.0, 96.0, 549.0);
let only = vec![cell("XML", 78.0, 541.0, 94.0, 548.0)];
assert_eq!(super::code_language_labels(&[label2], &only), vec![false]);
let inside_lbl = region("text", 0.9, 76.0, 540.0, 96.0, 549.0);
let wide_code = region("code", 0.7, 63.0, 531.0, 320.0, 654.0);
let cells2 = vec![cell("XML", 78.0, 541.0, 94.0, 548.0)];
assert_eq!(
super::code_language_labels(&[inside_lbl, wide_code], &cells2),
vec![true, false]
);
assert!(super::is_code_language("XML") && super::is_code_language("c#"));
assert!(!super::is_code_language("Configure") && !super::is_code_language("XML schema"));
}
#[test]
fn code_region_text_keeps_lines_and_indentation() {
let region = Region {
label: "code",
score: 1.0,
l: 0.0,
t: -5.0,
r: 100.0,
b: 40.0,
};
let cells = vec![
cell("struct P {", 10.0, 0.0, 70.0, 10.0),
cell("int X;", 22.0, 12.0, 58.0, 22.0),
cell("}", 10.0, 24.0, 16.0, 34.0),
];
assert_eq!(code_region_text(®ion, &cells), "struct P {\n int X;\n}");
}
#[test]
fn code_region_text_tightens_punctuation_without_eating_indentation() {
let region = Region {
label: "code",
score: 1.0,
l: 0.0,
t: -5.0,
r: 100.0,
b: 40.0,
};
let cells = vec![
cell("builder", 10.0, 0.0, 52.0, 10.0),
cell(".Foo (x)", 22.0, 12.0, 70.0, 22.0),
];
assert_eq!(code_region_text(®ion, &cells), "builder\n .Foo(x)");
}
#[test]
fn code_region_text_orders_out_of_order_cells_and_ignores_blank_lines() {
let region = Region {
label: "code",
score: 1.0,
l: 0.0,
t: -5.0,
r: 100.0,
b: 60.0,
};
let cells = vec![
cell("b();", 10.0, 24.0, 34.0, 34.0),
cell(" ", 10.0, 12.0, 20.0, 22.0),
cell("a();", 10.0, 0.0, 34.0, 10.0),
];
assert_eq!(code_region_text(®ion, &cells), "a();\nb();");
assert_eq!(code_region_text(®ion, &[]), "");
}
fn para(text: &str) -> Node {
Node::Paragraph { text: text.into() }
}
fn assert_stream_eq(nodes: &[Node], splits: &[usize]) {
assert_stream_eq_under(nodes, splits, Continuations::Lines);
}
fn assert_stream_eq_under(nodes: &[Node], splits: &[usize], how: Continuations) {
let mut want = nodes.to_vec();
merge_continuations(&mut want, how);
let mut asm = StreamAssembler::new(how);
let mut got = Vec::new();
let mut start = 0;
for &end in splits {
got.extend(asm.push(nodes[start..end].to_vec()));
start = end;
}
got.extend(asm.push(nodes[start..].to_vec()));
got.extend(asm.finish());
assert_eq!(got, want, "stream assembly diverged (splits={splits:?})");
}
#[test]
fn running_head_floats_to_page_front() {
let cells = vec![
cell("HEAD — 7", 350.0, 40.0, 500.0, 50.0),
cell("marker", 70.0, 70.0, 90.0, 80.0),
cell("body", 100.0, 90.0, 300.0, 100.0),
];
let mut nodes = vec![
Node::PageInfo {
page_no: 1,
width: 600.0,
height: 800.0,
},
para("marker"),
para("body"),
para("HEAD — 7"),
];
float_running_head(&mut nodes, &cells);
assert_eq!(nodes[1], para("HEAD — 7"));
assert_eq!(nodes[3], para("body"));
}
#[test]
fn stream_assembler_matches_merge_continuations() {
let cross = [para("the definition of"), para("lists in scope")];
assert_stream_eq(&cross, &[1]);
assert_stream_eq(&cross, &[]);
let wrap = [
para("the wing type that is"),
Node::Picture {
caption: None,
caption_href: None,
image: None,
classification: None,
description: None,
caption_parent: Default::default(),
caption_location: None,
},
para("Fig. 1. a diagram"),
para("the most common kind"),
];
for splits in [&[][..], &[1][..], &[2][..], &[3][..], &[1, 3][..]] {
assert_stream_eq(&wrap, splits);
}
let blocked = [
para("ends mid word and"),
Node::Heading {
level: 2,
text: "New Section".into(),
},
para("more body here"),
];
for splits in [&[][..], &[1][..], &[2][..]] {
assert_stream_eq(&blocked, splits);
}
let chain = [
para("alpha beta"),
para("gamma delta"),
para("epsilon zeta"),
];
assert_stream_eq(&chain, &[1, 2]);
}
#[test]
fn laid_out_paragraphs_merge_only_across_a_page_break() {
let merged = |nodes: &[Node]| {
let mut nodes = nodes.to_vec();
merge_continuations(&mut nodes, Continuations::AcrossPages);
nodes
};
let one_page = [page(1), para("the definition of"), para("lists in scope")];
assert_eq!(merged(&one_page), one_page);
let two_pages = [
page(1),
para("the definition of"),
page(2),
para("lists in scope, as"),
para("given above"),
];
assert_eq!(
merged(&two_pages),
[
page(1),
para("the definition of lists in scope, as given above"),
page(2),
]
);
assert_stream_eq_under(&one_page, &[], Continuations::AcrossPages);
assert_stream_eq_under(&two_pages, &[2], Continuations::AcrossPages);
let mut lines = one_page.to_vec();
merge_continuations(&mut lines, Continuations::Lines);
assert_eq!(lines, [page(1), para("the definition of lists in scope")]);
}
#[test]
fn cross_page_merge_keeps_each_fragments_prov() {
let loc = |y: u16, text: &str| super::located([50, y, 450, y + 40], para(text));
let nodes = vec![
page(6),
loc(400, "As noted in Table 1, a self-attention"),
icon(10),
para("Figure 2: a diagram"),
page(7),
loc(20, "layer connects all positions and a separ-"),
page(8),
loc(30, "able convolution comes last."),
loc(100, "A new paragraph."),
];
let mut merged = nodes.clone();
merge_continuations(&mut merged, Continuations::AcrossPages);
let text = "As noted in Table 1, a self-attention layer connects all \
positions and a separable convolution comes last.";
assert_eq!(merged[1], loc(400, text));
let head = "As noted in Table 1, a self-attention".chars().count();
let stem = "As noted in Table 1, a self-attention layer connects all positions and a separ"
.chars()
.count();
let total = text.chars().count();
assert_eq!(
merged[2],
Node::ProvContinuation {
location: [50, 20, 450, 60],
prev_end: head,
span: [head + 1, stem],
page_skip: 1,
}
);
assert_eq!(
merged[3],
Node::ProvContinuation {
location: [50, 30, 450, 70],
prev_end: stem,
span: [stem, total],
page_skip: 2,
}
);
assert_stream_eq_under(&nodes, &[4, 6], Continuations::AcrossPages);
assert_stream_eq_under(&nodes, &[5], Continuations::AcrossPages);
let mut doc = docling_core::DoclingDocument::new("t");
doc.nodes = merged.clone();
let json = doc.export_to_json_value();
let item = &json["texts"][0];
assert_eq!(item["text"], text);
let prov = item["prov"].as_array().expect("prov");
let pages: Vec<_> = prov.iter().map(|p| p["page_no"].clone()).collect();
assert_eq!(pages, [6, 7, 8]);
let spans: Vec<[u64; 2]> = prov
.iter()
.map(|p| [0, 1].map(|k| p["charspan"][k].as_u64().unwrap()))
.collect();
let as_u64 = |v: usize| v as u64;
assert_eq!(
spans,
[
[0, as_u64(head)],
[as_u64(head) + 1, as_u64(stem)],
[as_u64(stem), as_u64(total)]
]
);
assert_eq!(prov[1]["bbox"]["t"], 800.0 - 20.0 * 800.0 / 512.0);
assert_eq!(prov[1]["bbox"]["b"], 800.0 - 60.0 * 800.0 / 512.0);
let next = json["texts"]
.as_array()
.unwrap()
.iter()
.find(|t| t["text"] == "A new paragraph.")
.unwrap();
assert_eq!(next["prov"].as_array().unwrap().len(), 1);
assert_eq!(next["prov"][0]["page_no"], 8);
let mut plain = docling_core::DoclingDocument::new("t");
plain.nodes = merged
.into_iter()
.filter(|n| !matches!(n, Node::ProvContinuation { .. }))
.collect();
assert_eq!(doc.export_to_markdown(), plain.export_to_markdown());
assert_eq!(doc.export_to_doclang(), plain.export_to_doclang());
}
fn item(text: &str) -> Node {
Node::ListItem {
ordered: false,
number: 0,
first_in_list: false,
text: text.into(),
level: 0,
marker: None,
location: None,
split: None,
dclx: None,
href: None,
layer: None,
}
}
fn icon(x: u16) -> Node {
super::located(
[x, 0, x + 10, 10],
Node::Picture {
caption: None,
caption_href: None,
image: None,
classification: None,
description: None,
caption_parent: Default::default(),
caption_location: None,
},
)
}
fn page(no: usize) -> Node {
Node::PageInfo {
page_no: no,
width: 600.0,
height: 800.0,
}
}
#[test]
fn a_list_continues_past_a_picture_as_in_docling() {
let heading = || Node::Heading {
level: 2,
text: "Cleaning".into(),
};
let mut nodes = vec![
page(1),
heading(),
item("Unplug the appliance."),
icon(1),
item("Wipe the base."),
item("Dry it."),
para("Then plug it in again."),
icon(2),
item("Store it."),
icon(3),
];
let flags = vec![false; nodes.len()];
super::continue_lists_past_asides(&mut nodes, &flags);
assert_eq!(
nodes,
vec![
page(1),
heading(),
item("Unplug the appliance."),
item("Wipe the base."),
item("Dry it."),
icon(1),
para("Then plug it in again."),
icon(2),
item("Store it."),
icon(3),
]
);
}
#[test]
fn a_form_area_list_stops_at_a_table() {
let mut nodes = vec![page(1), item("22 | first"), icon(1), item("23 | second")];
let want = nodes.clone();
super::continue_lists_past_asides(&mut nodes, &[false, true, true, true]);
assert_eq!(nodes, want);
}
#[test]
fn clean_text_dehyphenates_and_normalizes_typography() {
assert_eq!(clean_text("com\u{2} pact"), "compact");
assert_eq!(clean_text("end-to\u{2} end deep"), "end-toend deep");
assert_eq!(clean_text("word\u{2}"), "word");
assert_eq!(
clean_text("Graph\u{2019}s \u{201c}x\u{201d} \u{201e}y\u{201c}"),
"Graph's \"x\" \"y\""
);
assert_eq!(clean_text("a\u{2026}"), "a...");
assert_eq!(clean_text("a b\nc"), "a b c");
}
#[test]
fn form_children_stay_together_in_reading_order() {
let reg = |label: &'static str, l: f32, t: f32, r: f32, b: f32| Region {
label,
score: 0.9,
l,
t,
r,
b,
};
let mut items = vec![
reg("text", 50.0, 50.0, 550.0, 70.0), reg("form", 50.0, 100.0, 300.0, 400.0), reg("text", 60.0, 110.0, 290.0, 130.0), reg("text", 320.0, 110.0, 550.0, 130.0), reg("table", 60.0, 150.0, 290.0, 300.0), reg("text", 60.0, 320.0, 290.0, 340.0), reg("text", 50.0, 450.0, 550.0, 470.0), ];
let cids = super::cluster_cids(&items, &[]);
super::order_with_containers(&mut items, &cids, 600.0, 800.0, &[], |r| r);
let order: Vec<(&str, f32)> = items.iter().map(|r| (r.label, r.t)).collect();
let form_pos = order.iter().position(|(l, _)| *l == "form").unwrap();
assert_eq!(
&order[form_pos..form_pos + 4],
&[
("form", 100.0),
("text", 110.0),
("table", 150.0),
("text", 320.0)
]
);
assert_eq!(order[0], ("text", 50.0));
assert_eq!(order[order.len() - 1], ("text", 450.0));
let mut flat: Vec<Region> = items
.iter()
.filter(|r| r.label != "form")
.cloned()
.collect();
let cids = super::cluster_cids(&flat, &[]);
super::order_regions(&mut flat, &cids, 600.0, 800.0, &[], |r| r);
assert_ne!(
flat.iter().map(|r| r.t).collect::<Vec<_>>(),
order
.iter()
.filter(|(l, _)| *l != "form")
.map(|(_, t)| *t)
.collect::<Vec<_>>()
);
}
#[test]
fn picture_matches_the_cell_at_its_grid_position() {
let cell = |r: usize, c: usize, bbox: [f32; 4]| docling_core::TableCell {
text: format!("r{r}c{c}"),
bbox: Some(bbox),
start_row: r,
start_col: c,
row_span: 1,
col_span: 1,
column_header: false,
row_header: false,
row_section: false,
};
let cells = vec![
cell(0, 0, [0.0, 0.0, 100.0, 50.0]),
cell(0, 1, [100.0, 0.0, 200.0, 50.0]),
cell(1, 0, [0.0, 50.0, 100.0, 100.0]),
cell(1, 1, [100.0, 50.0, 200.0, 100.0]),
];
let pic = Region {
label: "picture",
score: 0.9,
l: 110.0,
t: 60.0,
r: 190.0,
b: 95.0,
};
assert_eq!(super::match_picture_to_cell(&pic, &cells), Some((1.0, 3)));
let straddling = Region {
label: "picture",
score: 0.9,
l: 60.0,
t: 60.0,
r: 160.0,
b: 95.0,
};
assert_eq!(super::match_picture_to_cell(&straddling, &cells), None);
}
#[test]
fn line_final_hyphen_fuses_only_when_attached_to_a_word() {
let line = |text: &str, t: f32| TextCell {
text: text.to_string(),
l: 0.0,
t,
r: 100.0,
b: t + 10.0,
};
assert_eq!(
cells_text(vec![&line("algo-", 0.0), &line("rithms", 12.0)]),
"algorithms"
);
assert_eq!(
cells_text(vec![&line("pp. 545-", 0.0), &line("561", 12.0)]),
"pp. 545561"
);
assert_eq!(
cells_text(vec![&line("range -", 0.0), &line("wide", 12.0)]),
"range - wide"
);
assert_eq!(
cells_text(vec![&line("-", 0.0), &line("item", 12.0)]),
"- item"
);
assert_eq!(
cells_text(vec![&line("x-", 0.0), &line("...", 12.0)]),
"x-..."
);
}
#[test]
fn lam_alef_only_swaps_a_genuinely_reversed_ligature() {
assert_eq!(
clean_text("\u{0628}\u{0623}\u{0644}"),
"\u{0628}\u{0644}\u{0623}"
);
assert_eq!(
clean_text("\u{0627}\u{0644}\u{0622}\u{0644}\u{064a}"),
"\u{0627}\u{0644}\u{0622}\u{0644}\u{064a}"
);
}
fn sliced_paragraph() -> (Vec<Region>, Vec<TextCell>) {
let line = |text: &str, t: f32, r: f32| cell(text, 60.0, t, r, t + 11.0);
let cells = vec![
line("The mission of this series is to improve", 135.0, 458.0),
line("The books in this series are technical,", 147.0, 458.0),
line("substantial. The authors are", 159.0, 458.0),
line("highly experienced craftsmen and", 171.5, 458.0), line("actually works in practice, as opposed", 185.0, 458.0),
line("about what the author has done, not", 197.0, 458.0),
line("about programming, there will be lots", 210.5, 458.0), line("will be lots of case studies from real", 223.0, 206.0), ];
let regions = vec![
region("text", 0.9, 60.0, 132.0, 458.0, 173.0), region("text", 0.9, 60.0, 184.0, 458.0, 212.0), region("text", 0.9, 60.0, 216.0, 206.0, 227.0), ];
(regions, cells)
}
fn ordered_texts(regions: &[Region], cells: &[TextCell]) -> Vec<String> {
let mut items: Vec<Region> = regions.to_vec();
let cids = super::cluster_cids(&items, cells);
super::order_regions(&mut items, &cids, 500.0, 700.0, &[], |r| r);
super::region_texts_exclusive(&items, cells)
.into_iter()
.map(|t| t.chars().take(9).collect())
.collect()
}
#[test]
fn a_text_box_over_paragraph_tails_gives_each_its_first_line() {
let line = |text: &str, t: f32| cell(text, 64.0, t, 515.0, t + 10.5);
let cells = vec![
cell(
"CS8091: A constructor cannot be extern",
93.0,
371.0,
471.0,
381.0,
),
line("Constructors are allowed only in class and", 403.0),
line("record struct types. You can't define them.", 423.0),
line("Instead, pass parameters by value.", 444.0),
line("You can declare extern constructors, however", 476.0),
line("constructor calls to call another constructor.", 497.0),
line(
"In addition, the following warnings can be generated:",
529.5,
),
cell(
"CS0824: Constructor is marked external.",
93.0,
561.0,
300.0,
571.0,
),
];
let mut regions = vec![
region("list_item", 0.9, 92.0, 370.0, 472.0, 382.0),
region("text", 0.9, 63.0, 422.0, 516.0, 455.5),
region("text", 0.9, 63.0, 496.0, 516.0, 508.5),
region("text", 0.6, 63.0, 402.0, 518.0, 541.0),
region("list_item", 0.9, 92.0, 560.0, 301.0, 572.0),
];
super::fit_regions_to_cells(&mut regions, &cells);
let cids = super::cluster_cids(®ions, &cells);
super::order_regions(&mut regions, &cids, 595.0, 842.0, &[], |r| r);
assert_eq!(
super::region_texts_exclusive(®ions, &cells),
[
"CS8091: A constructor cannot be extern",
"Constructors are allowed only in class and record struct types. \
You can't define them. Instead, pass parameters by value.",
"You can declare extern constructors, however constructor calls \
to call another constructor.",
"In addition, the following warnings can be generated:",
"CS0824: Constructor is marked external.",
]
);
let cells = vec![
cell("Remarks", 64.0, 100.0, 120.0, 110.5),
line("The body of the paragraph that the model boxed", 121.0),
line("on its own, two lines long.", 142.0),
line("A closing paragraph only the big box covers.", 174.0),
];
let mut regions = vec![
region("text", 0.9, 63.0, 120.0, 516.0, 153.0),
region("text", 0.5, 63.0, 99.0, 516.0, 185.0),
];
super::fit_regions_to_cells(&mut regions, &cells);
let cids = super::cluster_cids(®ions, &cells);
super::order_regions(&mut regions, &cids, 595.0, 842.0, &[], |r| r);
assert_eq!(
super::region_texts_exclusive(®ions, &cells),
[
"Remarks",
"The body of the paragraph that the model boxed on its own, two lines long.",
"A closing paragraph only the big box covers.",
]
);
let cells = vec![
line("If none of a switch expression's patterns matches", 100.0),
line(
"an input value, the runtime throws an exception. It is a",
121.0,
),
line(
"SwitchExpressionException. In most cases, the compiler",
142.0,
),
line("generates a warning.", 163.0),
];
let mut regions = vec![
region("text", 0.9, 63.0, 141.0, 516.0, 174.0),
region("text", 0.5, 63.0, 99.0, 516.0, 175.0),
];
super::fit_regions_to_cells(&mut regions, &cells);
assert_eq!(
super::region_texts_exclusive(®ions, &cells),
[
"If none of a switch expression's patterns matches an input value, \
the runtime throws an exception. It is a SwitchExpressionException. \
In most cases, the compiler generates a warning."
]
);
let cells = vec![
line(
"If none of a switch expression's patterns matches an input",
100.0,
),
cell(
"value, the runtime throws an exception, a",
64.0,
121.0,
300.0,
131.5,
),
line(
"System.Runtime.CompilerServices.SwitchExpressionException.",
142.0,
),
];
let mut regions = vec![
region("text", 0.9, 63.0, 120.0, 516.0, 132.0),
region("text", 0.5, 63.0, 99.0, 516.0, 154.0),
];
super::fit_regions_to_cells(&mut regions, &cells);
assert_eq!(regions.len(), 1);
let cells = vec![
cell("IBM Research", 260.0, 100.0, 330.0, 110.5),
cell(
"{mly,ahn,nli,cau,taa}@zurich.ibm.com",
200.0,
115.0,
390.0,
125.5,
),
];
let mut regions = vec![
region("text", 0.9, 199.0, 114.0, 391.0, 126.0),
region("text", 0.5, 199.0, 99.0, 391.0, 127.0),
];
super::fit_regions_to_cells(&mut regions, &cells);
assert_eq!(regions.len(), 2);
let cells = vec![
cell("left column one", 50.0, 100.0, 250.0, 110.0),
cell("left column two", 50.0, 120.0, 250.0, 130.0),
cell("right column one", 300.0, 100.0, 500.0, 110.0),
];
let mut regions = vec![
region("text", 0.9, 49.0, 99.0, 251.0, 131.0),
region("text", 0.5, 49.0, 99.0, 501.0, 131.0),
];
super::fit_regions_to_cells(&mut regions, &cells);
assert_eq!(regions.len(), 2);
assert_eq!(
super::region_texts_exclusive(®ions, &cells),
["left column one left column two", "right column one"]
);
}
#[test]
fn a_contained_short_last_line_is_no_column() {
let line = |text: &str, t: f32, r: f32| cell(text, 64.0, t, r, t + 9.0);
let page = |tail: &str, tail_r: f32, lines: [&str; 6]| {
let cells = vec![
line(lines[0], 106.0, 520.0),
line(tail, 125.0, tail_r),
line(lines[1], 157.0, 520.0),
line(lines[2], 176.0, 523.0),
line(lines[3], 195.5, 515.0),
line(lines[4], 215.0, 300.0),
line(lines[5], 245.0, 500.0),
];
let regions = vec![
region("text", 0.9, 63.0, 124.0, tail_r + 1.0, 135.0),
region("text", 0.9, 63.0, 175.0, 524.0, 225.0),
region("text", 0.5, 63.0, 105.0, 521.0, 255.0),
];
(regions, cells)
};
let resolve = |(mut regions, cells): (Vec<Region>, Vec<TextCell>)| {
super::fit_regions_to_cells(&mut regions, &cells);
let cids = super::cluster_cids(®ions, &cells);
super::order_regions(&mut regions, &cids, 595.0, 842.0, &[], |r| r);
super::region_texts_exclusive(®ions, &cells)
};
assert_eq!(
resolve(page(
"alias; using definition from 'File Name'",
266.0,
[
"The predefined type 'System.type name' is defined in multiple assemblies in the global",
"This error occurs when a predefined system type such as System.int32 is found in two",
"assemblies. One way this can happen is if you are referencing mscorlib from two",
"different places, such as trying to run the .NET Framework versions 1.0 and 1.1",
"side-byside.",
"The compiler will use the definition from only one of the assemblies.",
],
)),
[
"The predefined type 'System.type name' is defined in multiple assemblies in the \
global alias; using definition from 'File Name'",
"This error occurs when a predefined system type such as System.int32 is found in \
two assemblies. One way this can happen is if you are referencing mscorlib from \
two different places, such as trying to run the .NET Framework versions 1.0 and \
1.1 side-byside.",
"The compiler will use the definition from only one of the assemblies.",
]
);
assert_eq!(
resolve(page(
"assembly",
113.0,
[
"Added modules must be marked with the CLSCompliant attribute to match the",
"A module that was compiled with the module element of the TargetType compiler",
"option was added to a compilation with AddModule. However, the module's",
"compliance with the Common Language Specification (CLS) does not agree with",
"the CLS state of the current compilation.",
"CLS compliance is indicated with the module attribute.",
],
)),
[
"Added modules must be marked with the CLSCompliant attribute to match the assembly",
"A module that was compiled with the module element of the TargetType compiler \
option was added to a compilation with AddModule. However, the module's \
compliance with the Common Language Specification (CLS) does not agree with \
the CLS state of the current compilation.",
"CLS compliance is indicated with the module attribute.",
]
);
let cells = vec![
line(
"A full-width lead-in line that spans the whole box and",
106.0,
520.0,
),
cell("left column", 64.0, 125.0, 250.0, 134.0),
cell("right column", 300.0, 125.0, 500.0, 134.0),
];
let mut regions = vec![
region("text", 0.9, 63.0, 124.0, 251.0, 135.0),
region("text", 0.9, 299.0, 124.0, 501.0, 135.0),
region("text", 0.5, 63.0, 105.0, 521.0, 136.0),
];
super::fit_regions_to_cells(&mut regions, &cells);
assert_eq!(regions.len(), 3);
let cells = vec![
line(
"A full-width lead-in line that spans the whole box and",
106.0,
520.0,
),
line("a short line", 160.0, 200.0),
line(
"A closing line the container alone covers, also full width.",
190.0,
520.0,
),
];
let mut regions = vec![
region("text", 0.9, 63.0, 159.0, 201.0, 170.0),
region("text", 0.5, 63.0, 105.0, 521.0, 200.0),
];
super::fit_regions_to_cells(&mut regions, &cells);
assert_eq!(regions.len(), 2);
assert!(super::region_texts_exclusive(®ions, &cells).contains(
&"A full-width lead-in line that spans the whole box and \
A closing line the container alone covers, also full width."
.to_string()
));
}
#[test]
fn an_orphan_folds_only_into_a_raw_box_it_touches() {
let cells = vec![
cell("\"PMC2807444", 134.8, 430.3, 193.0, 436.9),
cell("_", 193.0, 438.52, 200.2, 438.92),
cell("006", 200.2, 430.7, 214.0, 436.9),
cell("_", 214.0, 438.52, 221.1, 438.92),
cell("00.png\" PubTabNet.", 221.1, 430.3, 304.7, 438.49),
cell("μ", 342.6, 430.2, 344.8, 439.72),
];
let mut regions = vec![region("caption", 0.96, 134.0, 396.6, 480.4, 438.44)];
super::add_orphan_regions(&mut regions, &cells);
assert_eq!(regions.len(), 3, "two orphans");
super::fit_regions_to_cells(&mut regions, &cells);
assert_eq!(regions.len(), 3, "neither folds into the grown caption");
assert_eq!(
super::region_texts_exclusive(®ions, &cells),
["\"PMC2807444 006 00.png\" PubTabNet. μ", "_", "_"]
);
let mut regions = vec![region("caption", 0.96, 134.0, 396.6, 480.4, 438.6)];
super::add_orphan_regions(&mut regions, &cells);
super::fit_regions_to_cells(&mut regions, &cells);
assert_eq!(regions.len(), 1);
}
#[test]
fn fitting_boxes_to_cells_puts_a_sliced_line_back_in_order() {
let (mut regions, cells) = sliced_paragraph();
super::add_orphan_regions(&mut regions, &cells);
assert_eq!(regions.len(), 5, "two orphan lines");
assert_eq!(
ordered_texts(®ions, &cells),
[
"The missi",
"highly ex",
"actually ",
"about pro",
"will be l"
]
);
super::fit_regions_to_cells(&mut regions, &cells);
assert_eq!(regions.len(), 5);
assert_eq!((regions[0].t, regions[0].b), (135.0, 170.0));
assert_eq!((regions[2].t, regions[2].b), (223.0, 234.0));
assert_eq!(
ordered_texts(®ions, &cells),
[
"The missi",
"highly ex",
"actually ",
"about pro",
"will be l"
]
);
}
#[test]
fn fitting_folds_surrounded_orphans_and_drops_empty_regulars() {
let wide = |text: &str, t: f32| cell(text, 60.0, t, 400.0, t + 11.0);
let cells = vec![
wide("first line of the paragraph", 100.0),
cell("stray", 150.0, 112.0, 400.0, 123.0), wide("third line of the paragraph", 124.0),
];
let mut regions = vec![
region("text", 0.9, 60.0, 98.0, 200.0, 136.0),
region("section_header", 0.8, 60.0, 300.0, 200.0, 320.0), region("formula", 0.8, 60.0, 340.0, 200.0, 360.0), region("picture", 0.8, 0.0, 400.0, 500.0, 600.0),
];
super::add_orphan_regions(&mut regions, &cells);
assert_eq!(regions.len(), 5, "the short line became an orphan");
super::fit_regions_to_cells(&mut regions, &cells);
let labels: Vec<&str> = regions.iter().map(|r| r.label).collect();
assert_eq!(labels, ["text", "formula", "picture"]);
let para = ®ions[0];
assert_eq!(
(para.l, para.t, para.r, para.b),
(60.0, 100.0, 400.0, 135.0)
);
assert_eq!(
super::region_texts_exclusive(®ions, &cells)[0],
"first line of the paragraph stray third line of the paragraph"
);
assert_eq!(
(regions[2].t, regions[2].b),
(400.0, 600.0),
"picture untouched"
);
let mut untouched = vec![region("text", 0.9, 0.0, 0.0, 10.0, 10.0)];
super::fit_regions_to_cells(&mut untouched, &[]);
assert_eq!(untouched.len(), 1, "no cells yet: nothing dropped");
}
#[test]
fn checklist_lines_with_squares_split_into_checkbox_regions() {
use crate::checkbox::CheckBox;
let cells = vec![
cell("Pick any:", 100.0, 90.0, 160.0, 100.0),
cell("First option", 120.0, 110.0, 180.0, 120.0),
cell("Second option that", 120.0, 129.0, 200.0, 139.0),
cell("wraps onto a line", 120.0, 141.0, 196.0, 151.0),
cell("Third option", 120.0, 160.0, 182.0, 170.0),
];
let sq = |t: f32, checked| CheckBox {
l: 100.0,
t,
r: 112.0,
b: t + 12.0,
checked,
};
let boxes = [sq(109.0, false), sq(128.0, true), sq(159.0, false)];
let block = || vec![region("text", 0.6, 100.0, 90.0, 200.0, 170.0)];
let mut regions = block();
super::split_checkbox_lines(&mut regions, &cells, &boxes);
let texts = super::region_texts_exclusive(®ions, &cells);
let got: Vec<(&str, &str)> = regions
.iter()
.zip(&texts)
.map(|(r, t)| (r.label, t.as_str()))
.collect();
assert_eq!(
got,
[
("text", "Pick any:"),
("checkbox_unselected", "First option"),
("checkbox_selected", "Second option that wraps onto a line"),
("checkbox_unselected", "Third option"),
]
);
assert_eq!((regions[2].l, regions[2].t), (100.0, 128.0));
assert_eq!((regions[2].r, regions[2].b), (200.0, 151.0));
let shape = |rs: &[Region]| -> Vec<(&'static str, [f32; 4])> {
rs.iter().map(|r| (r.label, [r.l, r.t, r.r, r.b])).collect()
};
let mut untouched = block();
super::split_checkbox_lines(&mut untouched, &cells, &[]);
assert_eq!(shape(&untouched), shape(&block()));
for far in [
CheckBox {
l: 40.0,
r: 52.0,
..sq(109.0, false)
},
CheckBox {
l: 115.0,
r: 127.0,
..sq(109.0, false)
},
] {
let mut regions = block();
super::split_checkbox_lines(&mut regions, &cells, &[far]);
assert_eq!(shape(®ions), shape(&block()));
}
let mut with_callout = cells.clone();
with_callout.push(cell("3", 103.0, 110.0, 109.0, 120.0));
let mut regions = block();
super::split_checkbox_lines(&mut regions, &with_callout, &[sq(109.0, false)]);
assert_eq!(shape(®ions), shape(&block()));
let labels = || vec![region("text", 0.6, 118.0, 90.0, 200.0, 170.0)];
for tick in ["X", "x", "v", "V", "\u{2714}"] {
let mut ticked = cells.clone();
ticked.push(cell(tick, 103.0, 110.0, 109.0, 120.0));
let mut regions = labels();
super::split_checkbox_lines(&mut regions, &ticked, &[sq(109.0, true)]);
let texts = super::region_texts_exclusive(®ions, &ticked);
let got: Vec<(&str, &str)> = regions
.iter()
.zip(&texts)
.map(|(r, t)| (r.label, t.as_str()))
.collect();
assert_eq!(got[1].0, "checkbox_selected", "{tick}: {got:?}");
assert!(got[1].1.starts_with("First option"), "{tick}: {got:?}");
}
let mut framed = cells.clone();
framed.push(cell("Xa", 103.0, 110.0, 109.0, 120.0));
let mut regions = labels();
super::split_checkbox_lines(&mut regions, &framed, &[sq(109.0, true)]);
assert_eq!(shape(®ions), shape(&labels()));
}
#[test]
fn checklist_lines_opening_with_a_ballot_box_split_into_checkbox_regions() {
let cells = vec![
cell("Options:", 100.0, 90.0, 150.0, 100.0),
cell("\u{2610} Tea", 100.0, 110.0, 140.0, 120.0),
cell("\u{2612} Coffee", 100.0, 130.0, 150.0, 140.0),
cell("\u{2610} Yes \u{2610} No", 100.0, 150.0, 170.0, 160.0),
];
let mut regions = vec![region("text", 0.6, 100.0, 90.0, 170.0, 160.0)];
super::split_checkbox_lines(&mut regions, &cells, &[]);
let texts = super::region_texts_exclusive(®ions, &cells);
let got: Vec<(&str, &str)> = regions
.iter()
.zip(&texts)
.map(|(r, t)| match r.label {
"text" => (r.label, t.as_str()),
_ => (r.label, super::strip_checkbox_glyph(t)),
})
.collect();
assert_eq!(
got,
[
("text", "Options:"),
("checkbox_unselected", "Tea"),
("checkbox_selected", "Coffee"),
("text", "\u{2610} Yes \u{2610} No"),
]
);
assert_eq!(super::strip_checkbox_glyph(" \u{2611} Done"), "Done");
assert_eq!(super::strip_checkbox_glyph("No box"), "No box");
}
#[test]
fn only_note_calls_move_behind_their_line() {
for t in ["(Anm. 1)", "[3]", "12", "*", "†", "¹²", "1,2"] {
assert!(looks_like_note_call(t), "{t}");
}
for t in [
"be",
"perfect",
"C",
"apply",
"RoPE",
"",
",",
"(this is a long aside)",
] {
assert!(!looks_like_note_call(t), "{t}");
}
let cell = |text: &str, l: f32, t: f32, r: f32, b: f32| TextCell {
text: text.into(),
l,
t,
r,
b,
};
let host = cell("und die Belegschaft besteht.", 100.0, 100.0, 300.0, 112.0);
assert!(is_note_of(
&cell("(Anm. 1)", 302.0, 98.0, 330.0, 106.0),
&host
));
assert!(!is_note_of(
&cell("perfect", 302.0, 98.0, 330.0, 106.0),
&host
));
}
#[test]
fn arabic_presentation_forms_fold_like_docling() {
assert_eq!(clean_text("ﻣﺎﺮﺱ"), "مارس");
assert_eq!(clean_text("ﻻ"), "لا");
assert_eq!(clean_text("ﭘ"), "پ");
assert!(clean_text("ﺍﻟﺤﺮﺱ").contains("الحرس"));
assert_eq!(clean_text("مرحبا"), "مرحبا");
assert_eq!(clean_text("x²"), "x²");
assert_eq!(clean_text("H₂O"), "H₂O");
let fullwidth: String = [0xFF46, 0xFF55, 0xFF4C, 0xFF4C]
.iter()
.map(|&c| char::from_u32(c).unwrap())
.collect();
assert_eq!(clean_text(&fullwidth), fullwidth);
assert_eq!(clean_text("2024 ﻣﺎﺮﺱ x²"), "2024 مارس x²");
}
#[test]
fn ligatures_and_the_private_use_glyph_fold_like_docling() {
assert_eq!(clean_text("\u{FB05}ar \u{FB06}op"), "star stop");
assert_eq!(clean_text("\u{132} is \u{133}s"), "IJ is ijs");
assert_eq!(clean_text("hello\u{F0A0} world"), "hello world");
assert_eq!(clean_text("con\u{FB01}gure"), "configure");
}
#[test]
fn table_cells_are_normalized_like_body_text() {
use super::{normalize_grid, TableGrid, TypographyScope};
let grid = || TableGrid {
rows: vec![vec![
"it\u{2019}s".into(),
"\u{201c}quoted\u{201d}".into(),
"\u{2022} dot".into(),
"ﻣﺎﺮﺱ".into(),
]],
cells: vec![docling_core::TableCell {
text: "\u{201c}a\u{FB01}\u{201d}".into(),
bbox: None,
start_row: 0,
start_col: 0,
row_span: 1,
col_span: 1,
column_header: false,
row_header: false,
row_section: false,
}],
};
let g = normalize_grid(grid());
assert_eq!(g.rows[0], ["it's", "\"quoted\"", "\u{b7} dot", "مارس"]);
assert_eq!(g.cells[0].text, "\"afi\"");
let _keep = TypographyScope::set(true);
let g = normalize_grid(grid());
assert_eq!(
g.rows[0],
[
"it\u{2019}s",
"\u{201c}quoted\u{201d}",
"\u{2022} dot",
"مارس"
]
);
}
}