docling-pdf 2.80.1

PDF/image backend for docling.rs: pure-Rust text layer and page renderer + ONNX layout/table/OCR pipeline.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
//! docling's TableFormer cell matching, ported from docling-ibm-models
//! (`tf_cell_matcher.py` + `matching_post_processor.py`, and the response
//! assembly in `tf_predictor.py`). The predicted table cells and the page's
//! word cells are matched by intersection-over-word-area, then the
//! post-processor snaps unmatched cells to their column's median position,
//! de-duplicates columns, assigns each word to its best cell, and finally
//! picks up "orphan" words (no overlap with any cell — dot leaders in a TOC,
//! stray page numbers) through row/column banding, an orphan in only one band
//! snapped to the nearest band on the other axis (docling-ibm-models 4.0.1,
//! #185; 4.0.2's matcher is the same code). Everything is kept bug-for-bug
//! faithful, including duplicate `good` cells weighting the column medians and
//! Python's banker's rounding on orphan depths.
//!
//! Coordinates: docling matches in its 2x-scaled page space with the table
//! bbox rounded to integers *before* scaling (`round(cluster.bbox.l) * scale`);
//! the caller reproduces that space so absolute constants (orphan-depth
//! rounding) agree.

use std::collections::BTreeMap;

use docling_core::debug_log;

/// docling's `table_cell` dict: an OTSL grid cell with its predicted box in
/// the matching coordinate space. `colspan_val`/`rowspan_val` are 0 when the
/// cell has no span (the dict key is absent in docling).
#[derive(Debug, Clone)]
pub struct TfCell {
    pub bbox: [f64; 4],
    pub cell_id: usize,
    pub row_id: usize,
    pub column_id: usize,
    pub cell_class: i64,
    pub colspan_val: usize,
    pub rowspan_val: usize,
}

/// A page word cell in the matching space (docling's `pdf_cells` token).
#[derive(Debug, Clone)]
pub struct PdfWord {
    pub id: usize,
    pub bbox: [f64; 4],
    pub text: String,
}

/// One `{table_cell_id, iopdf|post}` match entry.
#[derive(Debug, Clone, PartialEq)]
pub struct MatchEntry {
    pub table_cell_id: usize,
    pub score: f64,
}

/// pdf-cell-id → its match list. BTreeMap keeps deterministic ascending-id
/// iteration; docling's insertion-ordered dicts never depend on their order
/// for the final result (the response is re-sorted by pdf cell id).
pub type Matches = BTreeMap<usize, Vec<MatchEntry>>;

/// docling's `find_intersection`, including the source's harmless
/// `b2[1] > b2[3]` self-comparison typo (never true for a valid box).
fn find_intersection(b1: &[f64; 4], b2: &[f64; 4]) -> Option<[f64; 4]> {
    if b1[2] < b2[0] || b2[2] < b1[0] || b1[1] > b2[3] {
        return None;
    }
    Some([
        b1[0].max(b2[0]),
        b1[1].max(b2[1]),
        b1[2].min(b2[2]),
        b1[3].min(b2[3]),
    ])
}

/// `CellMatcher._intersection_over_pdf_match`: every (table cell, word) pair
/// with a positive intersection-over-word-area becomes a match entry; exact
/// duplicates (same cell id *and* score — duplicated `good` cells) are
/// suppressed like Python's `match not in matches[id]`.
fn intersection_over_pdf_match(table_cells: &[TfCell], pdf_cells: &[PdfWord]) -> Matches {
    let mut matches: Matches = BTreeMap::new();
    for cell in table_cells {
        for word in pdf_cells {
            let Some(ib) = find_intersection(&cell.bbox, &word.bbox) else {
                continue;
            };
            let warea = (word.bbox[2] - word.bbox[0]) * (word.bbox[3] - word.bbox[1]);
            let iarea = (ib[2] - ib[0]) * (ib[3] - ib[1]);
            let iopdf = if warea > 0.0 { iarea / warea } else { 0.0 };
            if iopdf > 0.0 {
                let entry = MatchEntry {
                    table_cell_id: cell.cell_id,
                    score: iopdf,
                };
                let list = matches.entry(word.id).or_default();
                if !list.contains(&entry) {
                    list.push(entry);
                }
            }
        }
    }
    matches
}

/// Step 0: `_get_table_dimension` — (columns, rows, max_cell_id).
fn get_table_dimension(table_cells: &[TfCell]) -> (usize, usize, usize) {
    let mut columns = 1;
    let mut rows = 1;
    let mut max_cell_id = 0;
    for cell in table_cells {
        columns = columns.max(cell.column_id);
        rows = rows.max(cell.row_id);
        max_cell_id = max_cell_id.max(cell.cell_id);
    }
    (columns + 1, rows + 1, max_cell_id)
}

/// Step 1: `_get_good_bad_cells_in_column`. A cell is `good` once per match
/// entry pointing at it (docling appends without breaking, so a cell matched
/// by N words appears N times and weights the medians N-fold); a cell whose
/// predicted class is empty (`cell_class <= 1`) is always `bad`.
fn good_bad_cells_in_column(
    table_cells: &[TfCell],
    column: usize,
    matches: &Matches,
) -> (Vec<TfCell>, Vec<TfCell>) {
    let mut good = Vec::new();
    let mut bad = Vec::new();
    for cell in table_cells {
        if cell.column_id != column {
            continue;
        }
        let mut bad_match = true;
        if cell.cell_class > 1 {
            for list in matches.values() {
                for m in list {
                    if m.table_cell_id == cell.cell_id {
                        good.push(cell.clone());
                        bad_match = false;
                    }
                }
            }
        }
        if bad_match {
            bad.push(cell.clone());
        }
    }
    (good, bad)
}

#[derive(Clone, Copy, PartialEq)]
enum Alignment {
    Left,
    Middle,
    Right,
}

/// Step 2: `_find_alignment_in_column` — smallest min-max spread of the
/// left / middle / right cell edges decides the column alignment.
fn find_alignment_in_column(cells: &[TfCell]) -> Alignment {
    if cells.is_empty() {
        return Alignment::Left;
    }
    let (mut lmin, mut lmax) = (f64::INFINITY, f64::NEG_INFINITY);
    let (mut mmin, mut mmax) = (f64::INFINITY, f64::NEG_INFINITY);
    let (mut rmin, mut rmax) = (f64::INFINITY, f64::NEG_INFINITY);
    for cell in cells {
        let l = cell.bbox[0];
        let r = cell.bbox[2];
        let m = (l + r) / 2.0;
        lmin = lmin.min(l);
        lmax = lmax.max(l);
        mmin = mmin.min(m);
        mmax = mmax.max(m);
        rmin = rmin.min(r);
        rmax = rmax.max(r);
    }
    let deltas = [lmax - lmin, mmax - mmin, rmax - rmin];
    // Python's list.index(min(...)) keeps the first minimum.
    let mut best = 0;
    for (i, d) in deltas.iter().enumerate() {
        if *d < deltas[best] {
            best = i;
        }
    }
    match best {
        0 => Alignment::Left,
        1 => Alignment::Middle,
        _ => Alignment::Right,
    }
}

/// Test hook for the crate-internal [`median`] (empty-slice guard).
#[cfg(test)]
pub(crate) fn median_for_test(values: &mut [f64]) -> f64 {
    median(values)
}

/// `statistics.median`: mean of the two middle values for an even count.
/// Returns 0.0 for an empty slice — a crafted table can leave a row/column
/// with zero matched cells, and `values[n / 2 - 1]` would otherwise underflow
/// (`0usize - 1`) and panic, which (via docling-serve) is a remote crash.
fn median(values: &mut [f64]) -> f64 {
    values.sort_by(|a, b| a.total_cmp(b));
    let n = values.len();
    if n == 0 {
        0.0
    } else if n % 2 == 1 {
        values[n / 2]
    } else {
        (values[n / 2 - 1] + values[n / 2]) / 2.0
    }
}

/// Step 3: `_get_median_pos_size` — median alignment-edge X / width / height
/// over the good cells, skipping spans and predicted-empty cells. Defaults
/// (0, 1, 1) when nothing qualifies, exactly like the source.
fn get_median_pos_size(cells: &[TfCell], alignment: Alignment) -> (f64, f64, f64) {
    let mut xs = Vec::new();
    let mut ws = Vec::new();
    let mut hs = Vec::new();
    for cell in cells {
        if cell.rowspan_val > 0 || cell.colspan_val > 0 || cell.cell_class <= 1 {
            continue;
        }
        let x = match alignment {
            Alignment::Left => cell.bbox[0],
            Alignment::Middle => (cell.bbox[2] + cell.bbox[0]) / 2.0,
            Alignment::Right => cell.bbox[2],
        };
        xs.push(x);
        ws.push(cell.bbox[2] - cell.bbox[0]);
        hs.push(cell.bbox[3] - cell.bbox[1]);
    }
    let median_x = if xs.is_empty() { 0.0 } else { median(&mut xs) };
    let median_w = if ws.is_empty() { 1.0 } else { median(&mut ws) };
    let median_h = if hs.is_empty() { 1.0 } else { median(&mut hs) };
    (median_x, median_w, median_h)
}

/// Step 4: `_move_cells_to_left_pos` with `rescale=False` (docling's call):
/// slide each bad cell to the column's median alignment edge, keeping its
/// original width.
fn move_cells_to_left_pos(cells: &[TfCell], median_x: f64, alignment: Alignment) -> Vec<TfCell> {
    cells
        .iter()
        .map(|cell| {
            let width = cell.bbox[2] - cell.bbox[0];
            let (new_x1, new_x2) = match alignment {
                Alignment::Left => (median_x, median_x + width),
                // Bit-faithful to the source: `new_x2 = new_x1 + original_width`.
                Alignment::Middle => {
                    let x1 = median_x - width / 2.0;
                    (x1, x1 + width)
                }
                Alignment::Right => (median_x - width, median_x),
            };
            TfCell {
                bbox: [new_x1, cell.bbox[1], new_x2, cell.bbox[3]],
                ..cell.clone()
            }
        })
        .collect()
}

/// Step 7: `_deduplicate_cells` — when two *adjacent* structural columns share
/// more than 60 % of their matched words, drop the lower-scoring column (its
/// cells and their match entries).
fn deduplicate_cells(
    tab_columns: usize,
    table_cells: &[TfCell],
    iou_matches: &Matches,
    ioc_matches: &Matches,
) -> (Vec<TfCell>, Matches) {
    use std::collections::BTreeSet;
    let mut pdf_cells_in_columns: Vec<BTreeSet<usize>> = Vec::with_capacity(tab_columns);
    let mut total_score_in_columns: Vec<f64> = Vec::with_capacity(tab_columns);
    for col in 0..tab_columns {
        let column_cell_ids: BTreeSet<usize> = table_cells
            .iter()
            .filter(|c| c.column_id == col)
            .map(|c| c.cell_id)
            .collect();
        let mut ids = BTreeSet::new();
        let mut score = 0.0;
        for matches in [iou_matches, ioc_matches] {
            for (&pdf_id, list) in matches {
                for m in list {
                    if column_cell_ids.contains(&m.table_cell_id) {
                        score += m.score;
                        ids.insert(pdf_id);
                    }
                }
            }
        }
        pdf_cells_in_columns.push(ids);
        total_score_in_columns.push(score);
    }

    let mut cols_to_eliminate: Vec<usize> = Vec::new();
    for cl in 0..tab_columns.saturating_sub(1) {
        let col_a = &pdf_cells_in_columns[cl];
        let col_b = &pdf_cells_in_columns[cl + 1];
        let int_prc = if col_a.is_empty() {
            0.0
        } else {
            col_a.intersection(col_b).count() as f64 / col_a.len() as f64
        };
        if int_prc > 0.6 {
            if total_score_in_columns[cl] >= total_score_in_columns[cl + 1] {
                cols_to_eliminate.push(cl + 1);
            } else {
                cols_to_eliminate.push(cl);
            }
        }
    }

    let mut removed_ids: BTreeSet<usize> = BTreeSet::new();
    let mut new_table_cells = Vec::new();
    for cell in table_cells {
        if cols_to_eliminate.contains(&cell.column_id) {
            removed_ids.insert(cell.cell_id);
        } else {
            new_table_cells.push(cell.clone());
        }
    }
    let mut new_matches: Matches = BTreeMap::new();
    for (&pdf_id, list) in ioc_matches {
        let kept: Vec<MatchEntry> = list
            .iter()
            .filter(|m| !removed_ids.contains(&m.table_cell_id))
            .cloned()
            .collect();
        if !kept.is_empty() {
            new_matches.insert(pdf_id, kept);
        }
    }
    (new_table_cells, new_matches)
}

/// Step 8: `_do_final_asignment` — each word keeps only its highest-scoring
/// match (Python's `max` keeps the first of equals).
fn do_final_assignment(ioc_matches: &Matches) -> Matches {
    let mut new_matches: Matches = BTreeMap::new();
    for (&pdf_id, list) in ioc_matches {
        let mut best = &list[0];
        for m in &list[1..] {
            if m.score > best.score {
                best = m;
            }
        }
        new_matches.insert(pdf_id, vec![best.clone()]);
    }
    new_matches
}

/// Step 8.a: `_align_table_cells_to_pdf` — matched cells take the union box
/// of their matched words; cells with no match are dropped.
fn align_table_cells_to_pdf(
    table_cells: &[TfCell],
    pdf_cells: &[PdfWord],
    matches: &Matches,
) -> Vec<TfCell> {
    use std::collections::HashMap;
    let word_boxes: HashMap<usize, [f64; 4]> = pdf_cells.iter().map(|w| (w.id, w.bbox)).collect();
    let mut boxes_per_cell: BTreeMap<usize, [f64; 4]> = BTreeMap::new();
    let mut order: Vec<usize> = Vec::new();
    for (pdf_id, list) in matches {
        let Some(&wb) = word_boxes.get(pdf_id) else {
            continue;
        };
        let mut seen = std::collections::BTreeSet::new();
        for m in list {
            if !seen.insert(m.table_cell_id) {
                continue;
            }
            match boxes_per_cell.entry(m.table_cell_id) {
                std::collections::btree_map::Entry::Vacant(e) => {
                    e.insert(wb);
                    order.push(m.table_cell_id);
                }
                std::collections::btree_map::Entry::Occupied(mut e) => {
                    let b = e.get_mut();
                    b[0] = b[0].min(wb[0]);
                    b[1] = b[1].min(wb[1]);
                    b[2] = b[2].max(wb[2]);
                    b[3] = b[3].max(wb[3]);
                }
            }
        }
    }
    let by_id: HashMap<usize, &TfCell> = table_cells.iter().map(|c| (c.cell_id, c)).collect();
    let mut out = Vec::new();
    for cell_id in order {
        if let Some(&cell) = by_id.get(&cell_id) {
            let mut cell = cell.clone();
            cell.bbox = boxes_per_cell[&cell_id];
            out.push(cell);
        }
    }
    out
}

fn merge_two_bboxes(a: &[f64; 4], b: &[f64; 4]) -> [f64; 4] {
    [
        a[0].min(b[0]),
        a[1].min(b[1]),
        a[2].max(b[2]),
        a[3].max(b[3]),
    ]
}

/// Python 3 `round()` — banker's rounding to the nearest integer.
fn py_round(v: f64) -> i64 {
    v.round_ties_even() as i64
}

/// A per-band orphan record: (pdf id, rounded depth, word bbox).
type OrphanRecord = (usize, i64, [f64; 4]);

/// Shared row/column banding scan of step 9: for each band (min/max of the
/// non-span, non-empty member cells' `lo`/`hi` edges), collect the unmatched
/// words whose edge or centroid falls inside, resolving a word seen in an
/// earlier band by the smaller rounded centroid distance.
fn band_orphans(
    n_bands: usize,
    cells_in_band: impl Fn(usize) -> Vec<[f64; 4]>,
    pdf_cells: &[PdfWord],
    matches: &Matches,
    lo_ix: usize,
    hi_ix: usize,
) -> Vec<Vec<OrphanRecord>> {
    let mut bands: Vec<Vec<OrphanRecord>> = Vec::with_capacity(n_bands);
    // pdf id → (band index, index within band) of its current record.
    let mut used: BTreeMap<usize, usize> = BTreeMap::new();
    for band_ix in 0..n_bands {
        let boxes = cells_in_band(band_ix);
        let mut lo = -1.0f64;
        let mut hi = -1.0f64;
        if !boxes.is_empty() {
            lo = boxes.iter().map(|b| b[lo_ix]).fold(f64::INFINITY, f64::min);
            hi = boxes
                .iter()
                .map(|b| b[hi_ix])
                .fold(f64::NEG_INFINITY, f64::max);
        }
        let mut in_band: Vec<OrphanRecord> = Vec::new();
        for word in pdf_cells {
            if matches.contains_key(&word.id) {
                continue;
            }
            let centroid_band = (hi + lo) / 2.0;
            let centroid_cell = (word.bbox[hi_ix] + word.bbox[lo_ix]) / 2.0;
            let within = (word.bbox[lo_ix] >= lo && word.bbox[lo_ix] <= hi)
                || (word.bbox[hi_ix] >= lo && word.bbox[hi_ix] <= hi)
                || (word.bbox[lo_ix] <= lo && word.bbox[hi_ix] >= hi);
            if !within {
                continue;
            }
            let depth = py_round((centroid_band - centroid_cell).abs());
            match used.get(&word.id) {
                None => {
                    used.insert(word.id, band_ix);
                    in_band.push((word.id, depth, word.bbox));
                }
                Some(&old_band) => {
                    let Some(old_pos) = bands[old_band].iter().position(|r| r.0 == word.id) else {
                        continue;
                    };
                    if depth < bands[old_band][old_pos].1 {
                        bands[old_band].remove(old_pos);
                        used.insert(word.id, band_ix);
                        in_band.push((word.id, depth, word.bbox));
                    }
                }
            }
        }
        bands.push(in_band);
    }
    bands
}

/// The centre of each band (`row_centroids` / `col_centroids`): the band of
/// the same member cells [`band_orphans`] scans, for the nearest-band
/// fallbacks; `None` for a band with no member, or one reaching below zero
/// (docling keys the centre on `lo >= 0 and hi >= 0`).
fn band_centroids(
    n_bands: usize,
    cells_in_band: impl Fn(usize) -> Vec<[f64; 4]>,
    lo_ix: usize,
    hi_ix: usize,
) -> Vec<Option<f64>> {
    (0..n_bands)
        .map(|band_ix| {
            let boxes = cells_in_band(band_ix);
            if boxes.is_empty() {
                return None;
            }
            let lo = boxes.iter().map(|b| b[lo_ix]).fold(f64::INFINITY, f64::min);
            let hi = boxes
                .iter()
                .map(|b| b[hi_ix])
                .fold(f64::NEG_INFINITY, f64::max);
            (lo >= 0.0 && hi >= 0.0).then_some((lo + hi) / 2.0)
        })
        .collect()
}

/// The band whose centre is nearest `at`, and the distance; the lowest index
/// on a tie, as Python's `min` over the centroid dict in band order.
fn nearest_band(centroids: &[Option<f64>], at: f64) -> Option<(usize, f64)> {
    let mut best: Option<(usize, f64)> = None;
    for (band_ix, c) in centroids.iter().enumerate() {
        let Some(c) = c else { continue };
        let dist = (at - c).abs();
        if best.is_none_or(|(_, d)| dist < d) {
            best = Some((band_ix, dist));
        }
    }
    best
}

/// `_attach_orphan_to_cell`: an orphan word joins the structural cell at
/// (row, column), growing its box, or a new body cell there; returns the
/// possibly incremented `max_cell_id`.
#[allow(clippy::too_many_arguments)]
fn attach_orphan(
    table_cells: &mut Vec<TfCell>,
    matches: &mut Matches,
    mut max_cell_id: usize,
    pdf_id: usize,
    row_id: usize,
    column_id: usize,
    confidence: i64,
    pdf_bbox: [f64; 4],
) -> usize {
    let existing = table_cells
        .iter()
        .position(|c| c.row_id == row_id && c.column_id == column_id);
    let table_cell_id = match existing {
        Some(ix) => {
            let merged = merge_two_bboxes(&table_cells[ix].bbox, &pdf_bbox);
            table_cells[ix].bbox = merged;
            table_cells[ix].cell_id
        }
        None => {
            max_cell_id += 1;
            table_cells.push(TfCell {
                bbox: pdf_bbox,
                cell_id: max_cell_id,
                column_id,
                row_id,
                cell_class: 2,
                colspan_val: 0,
                rowspan_val: 0,
            });
            max_cell_id
        }
    };
    matches.insert(
        pdf_id,
        vec![MatchEntry {
            table_cell_id,
            score: confidence as f64,
        }],
    );
    max_cell_id
}

/// Step 9: `_pick_orphan_cells` — words with no match are placed by the row
/// band containing them vertically and the column band containing them
/// horizontally; the (row, column) either reuses an existing structural cell
/// (growing its box) or creates a new one. A word in a row band but in no
/// column band goes to the column whose centre is nearest, and one in a
/// column band but in no row band to the nearest row
/// (docling-ibm-models#185: before it, both were dropped from the table, and
/// their text with them — a TOC line between two predicted rows, a value right
/// of the predicted columns). The confidence of a snapped word is negative:
/// `-(distance) - 1`, as upstream. A word in no band at all is still dropped,
/// and said so under `DOCLING_RS_DEBUG`. Upstream also warns once per snapped
/// word; that is left out, since the word keeps its text.
fn pick_orphan_cells(
    tab_rows: usize,
    tab_cols: usize,
    mut max_cell_id: usize,
    mut table_cells: Vec<TfCell>,
    pdf_cells: &[PdfWord],
    mut matches: Matches,
) -> (Matches, Vec<TfCell>, usize) {
    let orphans: Vec<usize> = pdf_cells
        .iter()
        .filter(|w| !matches.contains_key(&w.id))
        .map(|w| w.id)
        .collect();
    let row_cells = |row: usize| -> Vec<[f64; 4]> {
        table_cells
            .iter()
            .filter(|c| c.row_id == row && c.rowspan_val == 0 && c.cell_class > 1)
            .map(|c| c.bbox)
            .collect()
    };
    let col_cells = |col: usize| -> Vec<[f64; 4]> {
        table_cells
            .iter()
            .filter(|c| c.column_id == col && c.colspan_val == 0 && c.cell_class > 1)
            .map(|c| c.bbox)
            .collect()
    };
    // NOTE: docling's row-band pass exposes an aliasing quirk — the band's
    // orphan test reads `matches` as it was on entry (orphans found later
    // never re-enter it), which we reproduce by collecting all bands before
    // assigning anything.
    let orphan_rows = band_orphans(tab_rows, row_cells, pdf_cells, &matches, 1, 3);
    let orphan_columns = band_orphans(tab_cols, col_cells, pdf_cells, &matches, 0, 2);
    let row_centroids = band_centroids(tab_rows, row_cells, 1, 3);
    let col_centroids = band_centroids(tab_cols, col_cells, 0, 2);

    // pdf id → row / column of its accepted band record.
    let mut row_of: BTreeMap<usize, (usize, [f64; 4])> = BTreeMap::new();
    for (row_id, records) in orphan_rows.iter().enumerate() {
        for r in records {
            row_of.insert(r.0, (row_id, r.2));
        }
    }
    let mut col_of: BTreeMap<usize, (usize, i64, [f64; 4])> = BTreeMap::new();
    for (col_id, records) in orphan_columns.iter().enumerate() {
        for r in records {
            col_of.insert(r.0, (col_id, r.1, r.2));
        }
    }

    // Ascending pdf id, matching the sorted C++-parity loop in the source.
    for (&pdf_id, &(new_row_id, pdf_bbox)) in row_of.iter() {
        let (new_column_id, confidence, pdf_bbox) = match col_of.get(&pdf_id) {
            Some(&record) => record,
            None => {
                let at = (pdf_bbox[0] + pdf_bbox[2]) / 2.0;
                let Some((col, dist)) = nearest_band(&col_centroids, at) else {
                    continue;
                };
                (col, -py_round(dist) - 1, pdf_bbox)
            }
        };
        max_cell_id = attach_orphan(
            &mut table_cells,
            &mut matches,
            max_cell_id,
            pdf_id,
            new_row_id,
            new_column_id,
            confidence,
            pdf_bbox,
        );
    }
    // The mirror on the row axis: in a column band, in no row band.
    for (&pdf_id, &(new_column_id, _, pdf_bbox)) in col_of.iter() {
        if row_of.contains_key(&pdf_id) || matches.contains_key(&pdf_id) {
            continue;
        }
        let at = (pdf_bbox[1] + pdf_bbox[3]) / 2.0;
        let Some((new_row_id, dist)) = nearest_band(&row_centroids, at) else {
            continue;
        };
        max_cell_id = attach_orphan(
            &mut table_cells,
            &mut matches,
            max_cell_id,
            pdf_id,
            new_row_id,
            new_column_id,
            -py_round(dist) - 1,
            pdf_bbox,
        );
    }

    let dropped = orphans
        .iter()
        .filter(|id| !matches.contains_key(id))
        .count();
    if dropped > 0 {
        debug_log!(
            "docling-pdf: tableformer: {dropped} of {} pdf cells matched neither a row nor a \
             column band of the {tab_rows}x{tab_cols} grid and were dropped from the table",
            pdf_cells.len()
        );
    }
    (matches, table_cells, max_cell_id)
}

/// `MatchingPostProcessor.process` with docling's defaults
/// (`correct_overlapping_cells=False`): returns the post-processed table
/// cells and the final one-cell-per-word matches.
pub fn match_and_post_process(
    table_cells: Vec<TfCell>,
    pdf_cells: &[PdfWord],
) -> (Vec<TfCell>, Matches) {
    let matches = intersection_over_pdf_match(&table_cells, pdf_cells);

    let (tab_columns, tab_rows, max_cell_id) = get_table_dimension(&table_cells);

    // Steps 1-4 per column: snap the unmatched cells to the matched cells'
    // median alignment edge.
    let mut fixed: Vec<TfCell> = Vec::new();
    for col in 0..tab_columns {
        let (good, bad) = good_bad_cells_in_column(&table_cells, col, &matches);
        let alignment = find_alignment_in_column(&good);
        let (median_x, median_w, median_h) = get_median_pos_size(&good, alignment);
        let _ = (median_w, median_h); // rescale=False: medians only steer X.
        let moved = move_cells_to_left_pos(&bad, median_x, alignment);
        fixed.extend(good);
        fixed.extend(moved);
    }
    fixed.sort_by_key(|c| c.cell_id);

    // Step 5: re-match against the fixed cells.
    let ioc = intersection_over_pdf_match(&fixed, pdf_cells);

    // Step 7: drop duplicated columns.
    let (dedupl_cells, dedupl_matches) = deduplicate_cells(tab_columns, &fixed, &matches, &ioc);

    // Step 8: one table cell per word.
    let final_matches = do_final_assignment(&dedupl_matches);

    // Step 8.a: align cell boxes to their matched words (dropping unmatched
    // cells) — skipped for large pages, as in the source.
    let mut dedupl_sorted = dedupl_cells;
    dedupl_sorted.sort_by_key(|c| c.cell_id);
    let aligned = if pdf_cells.len() > 300 {
        dedupl_sorted
    } else {
        align_table_cells_to_pdf(&dedupl_sorted, pdf_cells, &final_matches)
    };

    // Step 9: place the leftover words by row/column banding. docling passes
    // the *pre-dedup* column count into the orphan scan.
    let (final_matches, cells_wo, _) = pick_orphan_cells(
        tab_rows,
        tab_columns,
        max_cell_id,
        aligned,
        pdf_cells,
        final_matches,
    );
    (cells_wo, final_matches)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn cell(cell_id: usize, row_id: usize, column_id: usize, bbox: [f64; 4]) -> TfCell {
        TfCell {
            bbox,
            cell_id,
            row_id,
            column_id,
            cell_class: 2,
            colspan_val: 0,
            rowspan_val: 0,
        }
    }

    fn word(id: usize, bbox: [f64; 4]) -> PdfWord {
        PdfWord {
            id,
            bbox,
            text: "w".into(),
        }
    }

    fn matched(pairs: &[(usize, usize)]) -> Matches {
        pairs
            .iter()
            .map(|&(pdf_id, table_cell_id)| {
                (
                    pdf_id,
                    vec![MatchEntry {
                        table_cell_id,
                        score: 0.9,
                    }],
                )
            })
            .collect()
    }

    /// The table cell a word ended in, and the match's confidence.
    fn placed<'a>(cells: &'a [TfCell], matches: &Matches, pdf_id: usize) -> (&'a TfCell, f64) {
        let entry = &matches[&pdf_id][0];
        let cell = cells
            .iter()
            .find(|c| c.cell_id == entry.table_cell_id)
            .expect("the matched cell exists");
        (cell, entry.score)
    }

    /// Two columns at x 10–90 and 110–200 over two rows.
    fn two_by_two() -> Vec<TfCell> {
        vec![
            cell(0, 0, 0, [10.0, 10.0, 90.0, 25.0]),
            cell(1, 0, 1, [110.0, 10.0, 200.0, 25.0]),
            cell(2, 1, 0, [10.0, 30.0, 90.0, 45.0]),
            cell(3, 1, 1, [110.0, 30.0, 200.0, 45.0]),
        ]
    }

    /// docling-ibm-models' `test_orphan_with_no_col_band_match_is_recovered_to_nearest_column`
    /// and `test_band_matched_orphans_use_normal_path`: a word in row 1's band
    /// right of both columns goes to the nearer column (centre 155, not 50)
    /// with a negative confidence; one inside both bands is placed as before.
    #[test]
    fn an_orphan_in_a_row_band_only_goes_to_the_nearest_column() {
        let words = [word(99, [550.0, 32.0, 590.0, 43.0])];
        let (m, cells, _) = pick_orphan_cells(2, 2, 3, two_by_two(), &words, Matches::new());
        let (c, confidence) = placed(&cells, &m, 99);
        assert_eq!((c.row_id, c.column_id), (1, 1));
        // The word's centre x = 570 is 415 from the column's 155.
        assert_eq!(confidence, -416.0);

        let words = [word(7, [20.0, 32.0, 80.0, 43.0])];
        let (m, cells, _) = pick_orphan_cells(2, 2, 3, two_by_two(), &words, Matches::new());
        let (c, confidence) = placed(&cells, &m, 7);
        assert_eq!((c.row_id, c.column_id), (1, 0));
        assert!(confidence >= 0.0);
    }

    /// `test_orphan_with_column_band_no_row_band_recovered_to_nearest_row`: a
    /// trailing line below every row band (docling#3402) goes to the nearest
    /// row, 30–50 rather than 0–20.
    #[test]
    fn an_orphan_in_a_column_band_only_goes_to_the_nearest_row() {
        let grid = vec![
            cell(0, 0, 0, [0.0, 0.0, 50.0, 20.0]),
            cell(1, 1, 0, [0.0, 30.0, 50.0, 50.0]),
        ];
        let words = [
            word(0, [0.0, 30.0, 50.0, 50.0]),
            word(9, [0.0, 200.0, 50.0, 220.0]),
        ];
        let (m, cells, _) = pick_orphan_cells(2, 1, 1, grid, &words, matched(&[(0, 1)]));
        let (c, confidence) = placed(&cells, &m, 9);
        assert_eq!((c.row_id, c.column_id), (1, 0));
        // The word's centre y = 210 is 170 from the row's 40.
        assert_eq!(confidence, -171.0);
    }

    /// `test_orphan_cells_outside_every_band_are_reported` and
    /// `test_no_warning_when_every_orphan_is_assigned`: a word below and right
    /// of the only cell has no band on either axis and is still dropped; one
    /// inside the band joins the cell.
    #[test]
    fn an_orphan_outside_every_band_is_dropped() {
        let grid = || vec![cell(0, 0, 0, [0.0, 0.0, 50.0, 20.0])];
        let words = [
            word(0, [0.0, 0.0, 50.0, 20.0]),
            word(1, [200.0, 200.0, 250.0, 220.0]),
            word(2, [200.0, 300.0, 250.0, 320.0]),
        ];
        let (m, _, _) = pick_orphan_cells(1, 1, 0, grid(), &words, matched(&[(0, 0)]));
        assert_eq!(m.keys().copied().collect::<Vec<_>>(), [0]);

        let words = [
            word(0, [0.0, 0.0, 50.0, 20.0]),
            word(1, [0.0, 5.0, 50.0, 18.0]),
        ];
        let (m, _, _) = pick_orphan_cells(1, 1, 0, grid(), &words, matched(&[(0, 0)]));
        assert_eq!(m.keys().copied().collect::<Vec<_>>(), [0, 1]);
    }
}