xberg-native-pdf 1.2.6

PDF parsing, extraction and rendering -- the native PDF engine behind xberg.
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
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
//! Table extraction from PDF structure tree.
//!
//! Implements table detection and reconstruction according to ISO 32000-1:2008 Section 14.8.4.3.4
//! (Table Elements).
//!
//! Table structure hierarchy:
//! - Table: Top-level container
//!   - THead: Optional header row group
//!   - TBody: One or more body row groups
//!   - TFoot: Optional footer row group
//! - TR: Table row (contains TH and/or TD cells)
//!   - TH: Table header cell
//!   - TD: Table data cell

use crate::error::Error;
use crate::geometry::Rect;
use crate::layout::{Color, FontWeight, TextBlock, TextSpan};
use crate::structure::types::{McidScope, StructChild, StructElem, StructType};

/// A complete extracted table with rows and optional header information.
#[derive(Debug, Clone)]
pub struct Table {
    /// Rows of the table (alternating between header and body rows)
    pub rows: Vec<TableRow>,

    /// Whether the table has an explicit header section
    pub has_header: bool,

    /// Number of columns (inferred from first row)
    pub col_count: usize,

    /// Bounding box of the table region (used to exclude table spans from normal rendering)
    pub bbox: Option<Rect>,
}

/// A single row in a table.
#[derive(Debug, Clone)]
pub struct TableRow {
    /// Cells in this row
    pub cells: Vec<TableCell>,

    /// Whether this is a header row
    pub is_header: bool,
}

/// A single cell in a table.
#[derive(Debug, Clone)]
pub struct TableCell {
    /// Text content of the cell
    pub text: String,

    /// Original text spans that make up this cell's content, with
    /// font/style metadata preserved for format-aware rendering.
    pub spans: Vec<crate::layout::TextSpan>,

    /// Number of columns this cell spans (default 1)
    pub colspan: u32,

    /// Number of rows this cell spans (default 1)
    pub rowspan: u32,

    /// MCID values that make up this cell's content
    pub mcids: Vec<u32>,

    /// Bounding box of the cell
    pub bbox: Option<Rect>,

    /// Whether this is a header cell
    pub is_header: bool,
}

impl Default for Table {
    fn default() -> Self {
        Self::new()
    }
}

impl Table {
    /// Create a new extracted table
    pub fn new() -> Self {
        Self {
            rows: Vec::new(),
            has_header: false,
            col_count: 0,
            bbox: None,
        }
    }

    /// Check whether this table looks like a real data grid as opposed to
    /// spurious spatial output (form layouts, label-colon-value pairs).
    ///
    /// A real grid has:
    /// - ≥2 rows
    /// - ≥2 columns
    /// - Consistent column population: at least 50% of rows must have
    ///   at least 2 non-empty cells. Filters out the common
    ///   form-as-table false positive where rows look like
    ///   `| Single label, all other slots empty | | | |`.
    pub fn is_real_grid(&self) -> bool {
        if self.col_count < 2 || self.rows.len() < 2 {
            return false;
        }
        let rows_with_two_or_more_filled_cells = self
            .rows
            .iter()
            .filter(|r| r.cells.iter().filter(|c| !c.text.trim().is_empty()).count() >= 2)
            .count();
        let ratio = rows_with_two_or_more_filled_cells as f32 / self.rows.len() as f32;

        // Wide tables (≥ 8 columns) are high-risk false positives: prose sentences
        // can be split into many single-phrase cells by decorative rule lines.
        // Real wide data tables have most rows densely filled (≥ 60% of columns);
        // prose-split false tables have highly variable row fill counts (some rows
        // have 1-2 filled cells, others have 10+), so the fraction of "dense" rows
        // is well below 70%.
        //
        // Exception: a consolidated multi-row table can contain a mix
        // of dense data rows and sparse header / multi-row-label rows.  The sparse
        // rows are legitimate table content (column headers split across multiple
        // visual rows, lane-count labels that only appear on the first row of a
        // sub-group), so a strict dense-row ratio rejects real tables.  Accept the
        // table if it has BOTH enough dense rows in absolute terms (≥ half the
        // column count) AND a meaningful dense-row ratio (≥ 40 %). ~keep
        if self.col_count >= 8 {
            let min_dense = ((self.col_count as f32 * 0.6) as usize).max(2);
            let dense_rows = self
                .rows
                .iter()
                .filter(|r| r.cells.iter().filter(|c| !c.text.trim().is_empty()).count() >= min_dense)
                .count();
            let dense_row_ratio = dense_rows as f32 / self.rows.len() as f32;
            if self.rows.len() >= 3 && ratio >= 0.7 && dense_row_ratio >= 0.70 {
                return true;
            }
            let min_absolute_dense = (self.col_count / 2).max(3);
            return dense_rows >= min_absolute_dense && dense_row_ratio >= 0.40;
        }

        ratio >= 0.5
    }

    /// Render the table as clean, space-padded plain text.
    pub fn render_text(&self) -> String {
        let col_count = self.col_count;
        if col_count == 0 || self.rows.is_empty() {
            return String::new();
        }

        let mut col_widths = vec![0usize; col_count];
        for row in &self.rows {
            let mut col_idx = 0;
            for cell in &row.cells {
                if cell.colspan == 1 && col_idx < col_count {
                    let w = cell.text.trim().chars().count();
                    col_widths[col_idx] = col_widths[col_idx].max(w);
                }
                col_idx += cell.colspan as usize;
            }
        }

        for w in &mut col_widths {
            if *w < 2 {
                *w = 2;
            }
        }

        // Trim trailing empty columns (no non-empty cell contributes content
        // to that column, including cells with colspan > 1 that cover it). ~keep
        let effective_cols = {
            let mut eff = col_widths.len();
            while eff > 0 {
                let col = eff - 1;
                let all_empty = self.rows.iter().all(|row| {
                    let mut ci = 0;
                    for cell in &row.cells {
                        let span = cell.colspan as usize;
                        let covers_col = ci <= col && col < ci + span;
                        if covers_col {
                            return cell.text.trim().is_empty();
                        }
                        ci += span;
                    }
                    true
                });
                if all_empty {
                    eff -= 1;
                } else {
                    break;
                }
            }
            eff
        };
        if effective_cols == 0 {
            return String::new();
        }
        let col_widths = &col_widths[..effective_cols];

        let is_right_aligned: Vec<bool> = (0..effective_cols)
            .map(|c| {
                let mut has_content = false;
                for row in &self.rows {
                    let mut ci = 0;
                    for cell in &row.cells {
                        if ci == c && cell.colspan == 1 {
                            let t = cell.text.trim();
                            if !t.is_empty() {
                                has_content = true;
                                let stripped: String = t
                                    .chars()
                                    .filter(|ch| {
                                        !matches!(ch, '$' | '€' | '£' | ',' | ' ' | '%' | '+' | '-' | '(' | ')')
                                    })
                                    .collect();
                                if stripped.is_empty() || stripped.parse::<f64>().is_err() {
                                    return false;
                                }
                            }
                        }
                        ci += cell.colspan as usize;
                    }
                }
                has_content
            })
            .collect();

        let mut output = String::new();

        for row in &self.rows {
            let mut col_idx = 0;
            let mut cells_text = Vec::new();
            for cell in &row.cells {
                let text = cell.text.trim();
                if col_idx < effective_cols {
                    let width = if cell.colspan > 1 {
                        let end = (col_idx + cell.colspan as usize).min(effective_cols);
                        let base: usize = col_widths[col_idx..end].iter().sum();
                        base + (end - col_idx).saturating_sub(1) * 2
                    } else {
                        col_widths[col_idx]
                    };
                    let formatted = if cell.colspan == 1 && is_right_aligned[col_idx] {
                        format!("{:>width$}", text, width = width)
                    } else {
                        format!("{:<width$}", text, width = width)
                    };
                    cells_text.push(formatted);
                } else {
                    cells_text.push(text.to_string());
                }
                col_idx += cell.colspan as usize;
            }
            output.push_str(cells_text.join("  ").trim_end());
            output.push('\n');
        }

        output
    }

    /// Add a row to the table
    pub fn add_row(&mut self, row: TableRow) {
        if self.col_count == 0 && !row.cells.is_empty() {
            self.col_count = row.cells.len();
        }
        self.rows.push(row);
    }

    /// Check if table is empty
    pub fn is_empty(&self) -> bool {
        self.rows.is_empty()
    }
}

impl TableRow {
    /// Create a new table row
    pub fn new(is_header: bool) -> Self {
        Self {
            cells: Vec::new(),
            is_header,
        }
    }

    /// Check if any cell in this row has a colspan > 1.
    pub fn has_colspan(&self) -> bool {
        self.cells.iter().any(|c| c.colspan > 1)
    }

    /// Add a cell to the row
    pub fn add_cell(&mut self, cell: TableCell) {
        self.cells.push(cell);
    }
}

impl TableCell {
    /// Create a new table cell
    pub fn new(text: String, is_header: bool) -> Self {
        Self {
            text,
            spans: Vec::new(),
            colspan: 1,
            rowspan: 1,
            mcids: Vec::new(),
            bbox: None,
            is_header,
        }
    }

    /// Set colspan
    pub fn with_colspan(mut self, colspan: u32) -> Self {
        self.colspan = colspan;
        self
    }

    /// Set rowspan
    pub fn with_rowspan(mut self, rowspan: u32) -> Self {
        self.rowspan = rowspan;
        self
    }

    /// Add an MCID
    pub fn add_mcid(&mut self, mcid: u32) {
        self.mcids.push(mcid);
    }
}

/// Find all Table structure elements in the structure tree for a given page.
///
/// Recursively walks the structure tree to collect StructElem nodes where
/// `struct_type == StructType::Table` and the element (or any descendant)
/// has marked content on the specified page.
///
/// # Arguments
/// * `struct_tree` - The structure tree root
/// * `page_num` - Page number to match (0-based)
///
/// # Returns
/// * `Vec<&StructElem>` - Table elements found for the page
pub fn find_table_elements(struct_tree: &crate::structure::types::StructTreeRoot, page_num: u32) -> Vec<&StructElem> {
    let mut tables = Vec::new();
    for elem in &struct_tree.root_elements {
        collect_table_elements(elem, page_num, &mut tables);
    }
    tables
}

/// Recursively collect Table elements that have content on the given page.
fn collect_table_elements<'a>(elem: &'a StructElem, page_num: u32, tables: &mut Vec<&'a StructElem>) {
    if elem.struct_type == StructType::Table {
        if element_has_page_content(elem, page_num) {
            tables.push(elem);
        }
        return; // Don't recurse into table children looking for nested tables ~keep
    }

    for child in &elem.children {
        if let StructChild::StructElem(child_elem) = child {
            collect_table_elements(child_elem, page_num, tables);
        }
    }
}

/// Walk the structure tree once and bucket every `Table` element by each page
/// it has content on (owned clones), so the converter table path can do an
/// O(1) per-page lookup instead of `find_table_elements`'s per-page walk
/// (≈ O(pages²) on a tagged document). For a given page the result matches
/// `find_table_elements(tree, page)`: same DFS pre-order, and like
/// `collect_table_elements` it does not recurse into a Table's children.
pub fn find_table_elements_all_pages(
    struct_tree: &crate::structure::types::StructTreeRoot,
) -> std::collections::HashMap<u32, Vec<StructElem>> {
    let mut by_page: std::collections::HashMap<u32, Vec<StructElem>> = std::collections::HashMap::new();
    for elem in &struct_tree.root_elements {
        collect_table_elements_all_pages(elem, &mut by_page);
    }
    by_page
}

fn collect_table_elements_all_pages(elem: &StructElem, by_page: &mut std::collections::HashMap<u32, Vec<StructElem>>) {
    if elem.struct_type == StructType::Table {
        // Pages this table (or any descendant) has content on — the exact set
        // for which `element_has_page_content(elem, page)` is true. ~keep
        let mut pages = std::collections::BTreeSet::new();
        collect_content_pages(elem, &mut pages);
        for p in pages {
            by_page.entry(p).or_default().push(elem.clone());
        }
        return; // mirror collect_table_elements: don't recurse into table children ~keep
    }

    for child in &elem.children {
        if let StructChild::StructElem(child_elem) = child {
            collect_table_elements_all_pages(child_elem, by_page);
        }
    }
}

/// Collect the set of pages on which `elem` (or any descendant) has content.
/// Mirrors the truth set of [`element_has_page_content`] across all pages.
fn collect_content_pages(elem: &StructElem, pages: &mut std::collections::BTreeSet<u32>) {
    if let Some(p) = elem.page {
        pages.insert(p);
    }
    for child in &elem.children {
        match child {
            StructChild::MarkedContentRef { page, .. } => {
                pages.insert(*page);
            }
            StructChild::StructElem(child_elem) => {
                collect_content_pages(child_elem, pages);
            }
            StructChild::ObjectRef(_, _) => {}
        }
    }
}

/// Check if a structure element or any descendant has marked content on the given page.
fn element_has_page_content(elem: &StructElem, page_num: u32) -> bool {
    if elem.page == Some(page_num) {
        return true;
    }

    for child in &elem.children {
        match child {
            StructChild::MarkedContentRef { page, .. } => {
                if *page == page_num {
                    return true;
                }
            }
            StructChild::StructElem(child_elem) => {
                if element_has_page_content(child_elem, page_num) {
                    return true;
                }
            }
            StructChild::ObjectRef(_, _) => {}
        }
    }

    false
}

/// Extract a table from a structure element tree using TextSpans (MCID matching).
///
/// Converts TextSpans to a format suitable for MCID-based cell text extraction,
/// then delegates to the standard `extract_table` function.
///
/// # Arguments
/// * `table_elem` - The Table structure element
/// * `spans` - Text spans from the page (with MCID values)
///
/// # Returns
/// * `Table` containing all rows and cells
pub fn extract_table_from_spans(table_elem: &StructElem, spans: &[crate::layout::TextSpan]) -> Result<Table, Error> {
    // Convert spans to TextBlocks for MCID matching, applying column-spanning
    // decimal split so that "12.11" (sailing score columns) becomes "12 11". ~keep
    let text_blocks: Vec<TextBlock> = spans
        .iter()
        .filter(|s| s.mcid.is_some())
        .map(|s| {
            let text = span_text_for_cell(s);
            TextBlock {
                chars: Vec::new(),
                bbox: s.bbox,
                text,
                avg_font_size: s.font_size,
                dominant_font: s.font_name.clone(),
                is_bold: s.font_weight.is_bold(),
                is_italic: s.is_italic,
                mcid: s.mcid,
                mcid_scope: s.mcid_scope.clone(),
                sequence: s.sequence,
                rotation_degrees: s.rotation_degrees,
            }
        })
        .collect();
    extract_table(table_elem, &text_blocks)
}

/// Return the display text for a span when used as a table cell token.
/// Mirrors `PdfDocument::push_span_text`: splits column-spanning decimals
/// (e.g. "12.11" across adjacent score columns) at the decimal point.
pub(super) fn span_text_for_cell(span: &crate::layout::TextSpan) -> String {
    let text = &span.text;
    let dot_pos = match text.find('.') {
        Some(p) if p > 0 && p < text.len() - 1 => p,
        _ => return text.clone(),
    };
    if text[dot_pos + 1..].contains('.') {
        return text.clone();
    }
    if !text[..dot_pos].chars().all(|c| c.is_ascii_digit()) {
        return text.clone();
    }
    if !text[dot_pos + 1..].chars().all(|c| c.is_ascii_digit()) {
        return text.clone();
    }
    let char_count = text.chars().count();
    // Signal 1: sparse char_widths array (cw.len < char_count) means the
    // span was assembled from two concatenated Tj runs — see the matching
    // `is_column_spanning_decimal` rule in document.rs.  Catches sailing-
    // score cells emitted as a single Tj like "1.10" (cw=[w]) where the
    // PDF actually means "1" followed by "10" in adjacent score columns
    // bbox.width can still be tight here, so the
    // bbox-inflation check below isn't sufficient. ~keep
    if !span.char_widths.is_empty() && span.char_widths.len() < char_count {
        return format!("{} {}", &text[..dot_pos], &text[dot_pos + 1..]);
    }
    let expected_width = if !span.char_widths.is_empty() {
        let cw_sum: f32 = span.char_widths.iter().sum();
        cw_sum * (char_count as f32 / span.char_widths.len() as f32)
    } else if span.font_size > 0.0 {
        // 0.50em per char: digits are narrower than average; keeps the
        // fallback from producing false negatives on word_spans (char_widths=[]). ~keep
        span.font_size * 0.50 * char_count as f32
    } else {
        return text.clone();
    };
    let gap = span.bbox.width - expected_width;
    if span.font_size > 0.0 && gap > span.font_size * 1.0 {
        format!("{} {}", &text[..dot_pos], &text[dot_pos + 1..])
    } else {
        text.clone()
    }
}

/// Extract a table from a structure element tree.
///
/// According to PDF spec Section 14.8.4.3.4, a Table element may contain:
/// - Direct TR (table row) children, OR
/// - THead (optional) + TBody (one or more) + TFoot (optional)
///
/// # Arguments
/// * `table_elem` - The Table structure element
/// * `text_blocks` - All text blocks in the document (for MCID matching)
///
/// # Returns
/// * `Table` containing all rows and cells
pub fn extract_table(table_elem: &StructElem, text_blocks: &[TextBlock]) -> Result<Table, Error> {
    let mut table = Table::new();

    let has_thead = table_elem
        .children
        .iter()
        .any(|child| matches!(child, StructChild::StructElem(elem) if elem.struct_type == StructType::THead));

    if has_thead {
        table.has_header = true;
    }

    for child in &table_elem.children {
        match child {
            StructChild::StructElem(elem) => match elem.struct_type {
                StructType::TR => {
                    let row = extract_row(elem, text_blocks, false)?;
                    table.add_row(row);
                }
                StructType::THead => {
                    extract_row_group(elem, text_blocks, true, &mut table)?;
                }
                StructType::TBody => {
                    extract_row_group(elem, text_blocks, false, &mut table)?;
                }
                StructType::TFoot => {
                    extract_row_group(elem, text_blocks, false, &mut table)?;
                }
                _ => {}
            },
            StructChild::MarkedContentRef { .. } => {}
            StructChild::ObjectRef(_, _) => {}
        }
    }

    Ok(table)
}

/// Extract rows from a row group (THead, TBody, TFoot).
fn extract_row_group(
    group_elem: &StructElem,
    text_blocks: &[TextBlock],
    is_header: bool,
    table: &mut Table,
) -> Result<(), Error> {
    for child in &group_elem.children {
        match child {
            StructChild::StructElem(elem) if elem.struct_type == StructType::TR => {
                let row = extract_row(elem, text_blocks, is_header)?;
                table.add_row(row);
            }
            _ => {}
        }
    }
    Ok(())
}

/// Extract a single row (TR element).
fn extract_row(tr_elem: &StructElem, text_blocks: &[TextBlock], force_header: bool) -> Result<TableRow, Error> {
    let mut row = TableRow::new(force_header);

    for child in &tr_elem.children {
        match child {
            StructChild::StructElem(elem) => match elem.struct_type {
                StructType::TH => {
                    let cell = extract_cell(elem, text_blocks, true)?;
                    row.add_cell(cell);
                }
                StructType::TD => {
                    let cell = extract_cell(elem, text_blocks, false)?;
                    row.add_cell(cell);
                }
                _ => {}
            },
            StructChild::MarkedContentRef { .. } => {}
            StructChild::ObjectRef(_, _) => {}
        }
    }

    Ok(row)
}

/// Extract a single cell (TH or TD element).
fn extract_cell(cell_elem: &StructElem, text_blocks: &[TextBlock], is_header: bool) -> Result<TableCell, Error> {
    let mut mcids = Vec::new();
    collect_mcids(cell_elem, &mut mcids);
    let mut scoped_mcids = Vec::new();
    collect_scoped_mcids(cell_elem, &mut scoped_mcids);

    // Find all text blocks that match these MCIDs, joining them with position-aware
    // spacing: insert a space only when there is a genuine horizontal gap between
    // adjacent spans on the same line, or when spans are on different lines.
    // This prevents spurious spaces inside CJK expressions like "Q(peu/d)" whose
    // glyphs are stored as separate marked-content runs that abut each other. ~keep
    let mut cell_text = String::new();
    let mut cell_spans: Vec<TextSpan> = Vec::new();

    // Collect every block the cell's marked-content sequences own, then join
    // them in reading order: content-stream order routinely interleaves a
    // wrapped title's lines.
    //
    // Reading order follows the run's OWN writing axis, not the page's. ISO
    // 32000-1 §9.4.4 puts glyph displacement along the text matrix's writing
    // direction, so a line rotated 90° advances along y while successive
    // lines step along x, and right-to-left script is stored in logical order
    // while advancing toward decreasing x. Projecting onto those axes
    // recovers reading order for upright, rotated and RTL cells alike, where
    // a fixed y-then-x sort reverses the last two. ~keep
    let mut blocks: Vec<&TextBlock> = text_blocks.iter().filter(|b| block_belongs(b, &scoped_mcids)).collect();

    let (sin, cos) = cell_rotation_degrees(&blocks).to_radians().sin_cos();
    let direction = if blocks.iter().any(|b| crate::text::bidi::looks_rtl(&b.text)) {
        -1.0
    } else {
        1.0
    };
    // Along the writing direction, and perpendicular to it in the direction
    // successive lines advance (unrotated: +x and "down the page"). ~keep
    let along = |b: &TextBlock| direction * (b.bbox.x * cos + b.bbox.y * sin);
    let perp = |b: &TextBlock| b.bbox.x * sin - b.bbox.y * cos;
    // How far a run extends along its own writing direction. A span's bbox is
    // run-local — `width` is the accumulated advance and `height` the font
    // size, whatever the rotation (see the TextSpan construction in
    // extractors/text.rs) — so the writing-axis extent is always `width`. ~keep
    let advance_extent = |b: &TextBlock| b.bbox.width;

    // `perp` multiplies a bbox coordinate by a sine that is exactly 0.0 at
    // rotation 0, so a non-finite coordinate yields NaN on the axis we are not
    // even sorting along. Comparing NaN as Equal breaks transitivity and
    // `sort_by` then panics, so use the crate's NaN ordering. ~keep
    blocks.sort_by(|a, b| crate::utils::safe_float_cmp(perp(a), perp(b)));
    // Lines are formed by walking along the perpendicular axis and opening a
    // new line when the step from the current line's first block exceeds half
    // the SMALLER of the two heights — a per-pair threshold, so one tall
    // multi-line block cannot swallow line spacing tighter than its own
    // height. ~keep
    let mut keyed: Vec<(usize, &TextBlock)> = Vec::with_capacity(blocks.len());
    let mut line = 0usize;
    let mut anchor: Option<&TextBlock> = None;
    for b in blocks {
        if let Some(a) = anchor {
            let threshold = a.bbox.height.min(b.bbox.height).max(1.0) * 0.5;
            if perp(b) - perp(a) > threshold {
                line += 1;
                anchor = Some(b);
            }
        } else {
            anchor = Some(b);
        }
        keyed.push((line, b));
    }
    keyed.sort_by(|(line_a, a), (line_b, b)| {
        line_a
            .cmp(line_b)
            .then_with(|| crate::utils::safe_float_cmp(along(a), along(b)))
    });
    // `direction` reversed the whole cell, but UAX #9 reverses only the
    // surrounding script: numerals (class EN/AN) keep left-to-right order
    // inside a right-to-left paragraph. Producers routinely split one figure
    // across marked-content runs ("4" + "7.500"), so without this the sort
    // reads it back as "7.5004" — a silently altered figure, not a spacing
    // artefact. ~keep
    if direction < 0.0 {
        restore_numeric_run_order(&mut keyed);
    }

    let mut prev_block: Option<(usize, &TextBlock)> = None;
    for (line_index, block) in keyed {
        let mut leading_space = false;
        if !cell_text.is_empty() {
            let need_space = if let Some((prev_line, prev)) = prev_block {
                // Line membership was already decided above, in the writing
                // frame. Re-deriving it here from page-frame y would call
                // every run of a rotated line a new line (they differ in y by
                // their own advance) and inject a space into every word. ~keep
                let gap = along(block) - (along(prev) + advance_extent(prev));
                let font_size = prev.avg_font_size.max(block.avg_font_size).max(1.0);
                if line_index != prev_line {
                    true
                } else if gap <= font_size * 0.15 {
                    false
                } else {
                    // Suppress space insertion when one side is CJK and the
                    // other is CJK or a fullwidth/math operator (e.g. ≤, <, μ).
                    // This mirrors the CJK-pair suppression in document.rs and
                    // converters/mod.rs. ~keep
                    #[inline(always)]
                    fn is_cjk(c: char) -> bool {
                        matches!(c,
                            '\u{3040}'..='\u{309F}' |   // Hiragana ~keep
                            '\u{30A0}'..='\u{30FF}' |   // Katakana ~keep
                            '\u{4E00}'..='\u{9FFF}' |   // CJK Unified Ideographs ~keep
                            '\u{AC00}'..='\u{D7AF}' |   // Hangul ~keep
                            '\u{3400}'..='\u{4DBF}' |   // CJK Extension A ~keep
                            '\u{20000}'..='\u{2A6DF}'   // CJK Extension B ~keep
                        )
                    }
                    #[inline(always)]
                    fn is_fw_math(c: char) -> bool {
                        matches!(c,
                            '\u{FF0B}' | '\u{FF0D}' |
                            '\u{FF1A}' | '\u{FF1B}' |
                            '\u{FF1C}'..='\u{FF1E}' |
                            '\u{2260}' | '\u{2248}' |
                            '\u{2264}'..='\u{2265}' |
                            '\u{00B5}' | '\u{03BC}' |
                            '\u{00B1}' | '\u{00D7}' | '\u{00F7}'
                        )
                    }
                    let p_last = prev.text.chars().next_back();
                    let b_first = block.text.chars().next();
                    let suppress = if let (Some(p), Some(b)) = (p_last, b_first) {
                        let p_cjk = is_cjk(p);
                        let b_cjk = is_cjk(b);
                        (p_cjk || is_fw_math(p)) && (b_cjk || is_fw_math(b)) && (p_cjk || b_cjk)
                    } else {
                        false
                    };
                    !suppress
                }
            } else {
                !cell_text.ends_with(' ')
            };
            if need_space {
                cell_text.push(' ');
                leading_space = true;
            }
        }
        cell_text.push_str(&block.text);
        // Synthesize a minimal TextSpan capturing the block's
        // style. Only the fields the markdown converter
        // consults (text, font_weight, is_italic, font_size,
        // bbox) need real values — everything else is filled
        // from sensible defaults. Carry the inter-block space
        // into the span text as well: the markdown/HTML table
        // renderers reconstruct spacing from the spans (not from
        // cell_text), and their horizontal-gap heuristic cannot
        // see a line wrap, so without this they glue tokens
        // across wrapped lines. Both renderers already treat a
        // leading space in the span text as authoritative
        // (their `already_has_space` guard), so this never
        // double-spaces. ~keep
        let span_text = if leading_space {
            let mut s = String::with_capacity(block.text.len() + 1);
            s.push(' ');
            s.push_str(&block.text);
            s
        } else {
            block.text.clone()
        };
        cell_spans.push(TextSpan {
            provenance: None,
            artifact_type: None,
            text: span_text,
            bbox: block.bbox,
            font_name: block.dominant_font.clone(),
            font_size: block.avg_font_size,
            font_weight: if block.is_bold {
                FontWeight::Bold
            } else {
                FontWeight::Normal
            },
            is_italic: block.is_italic,
            is_monospace: false,
            color: Color::black(),
            mcid: block.mcid,
            mcid_scope: None,
            sequence: 0,
            offset_semantic: false,
            split_boundary_before: false,
            char_spacing: 0.0,
            word_spacing: 0.0,
            horizontal_scaling: 100.0,
            primary_detected: false,
            char_widths: vec![],
            char_x_offsets: Vec::new(),
            heading_level: None,
            // Carry the source block's rotation rather than resetting to 0.0: this push
            // re-synthesises a span from an already-rotated block, so hardcoding zero
            // silently flattens rotated table cells. Upstream fixed this on the older
            // per-block push; the fix belongs here now that the push is line-aware. ~keep
            rotation_degrees: block.rotation_degrees,
            wmode: 0,
            text_rise: 0.0,
            rtl_draw_logical: false,
            mirrored: false,
            page_rotation_applied: 0,
        });
        prev_block = Some((line_index, block));
    }

    let mut cell = TableCell::new(cell_text.trim().to_string(), is_header);
    cell.mcids = mcids;
    cell.spans = cell_spans;

    Ok(cell)
}

/// Whether a numeral keeps left-to-right order inside right-to-left text —
/// UAX #9 class EN (European) or AN (Arabic-Indic).
fn is_uax9_numeral(c: char) -> bool {
    c.is_ascii_digit() || matches!(c, '\u{0660}'..='\u{0669}' | '\u{06F0}'..='\u{06F9}')
}

/// Whether a cell block is a numeric run that must not share the surrounding
/// script's reversal.
///
/// At least one numeral is required deliberately. Absorbing bare separators
/// (a lone "." or "%") into the run measures worse: it pulls a trailing
/// separator into a run the sort already placed correctly, turning one wrong
/// cell into two. Full UAX #9 resolution in the cell sorter is a larger design
/// question than this defect.
fn is_numeric_run(text: &str) -> bool {
    let mut saw_numeral = false;
    for c in text.trim().chars() {
        if is_uax9_numeral(c) {
            saw_numeral = true;
        } else if !matches!(c, '.' | ',' | '\u{066B}' | '\u{066C}') {
            return false;
        }
    }
    saw_numeral
}

/// Undo the right-to-left sort reversal within each maximal run of numeric
/// blocks, leaving the surrounding script reversed. A run never spans a line
/// boundary, so line membership bounds it.
fn restore_numeric_run_order(keyed: &mut [(usize, &TextBlock)]) {
    let mut start = 0usize;
    while start < keyed.len() {
        if !is_numeric_run(&keyed[start].1.text) {
            start += 1;
            continue;
        }
        let line = keyed[start].0;
        let mut end = start + 1;
        while end < keyed.len() && keyed[end].0 == line && is_numeric_run(&keyed[end].1.text) {
            end += 1;
        }
        keyed[start..end].reverse();
        start = end;
    }
}

/// The quadrant the cell's glyph runs are drawn at, as the modal rotation of
/// its blocks. A cell mixing rotations (a rotated header beside an upright
/// note) reads in the majority frame; ties resolve to the smaller angle so
/// the choice is deterministic.
fn cell_rotation_degrees(blocks: &[&TextBlock]) -> f32 {
    let mut counts: [usize; 4] = [0; 4];
    for b in blocks {
        let normalized = b.rotation_degrees.rem_euclid(360.0);
        // Snap to a quadrant; a free angle (skew) reads in the upright frame,
        // which is what the unrotated sort did for it before. ~keep
        let quadrant = ((normalized / 90.0).round() as usize) % 4;
        if (normalized - (quadrant as f32) * 90.0).abs() < 0.5 {
            counts[quadrant] += 1;
        } else {
            counts[0] += 1;
        }
    }
    let (index, _) = counts
        .iter()
        .enumerate()
        .max_by_key(|&(ref index, &count)| (count, std::cmp::Reverse(*index)))
        .expect("counts is non-empty");
    (index as f32) * 90.0
}

/// Recursively collect all MCIDs from a structure element and its children.
fn collect_mcids(elem: &StructElem, mcids: &mut Vec<u32>) {
    let mut scoped = Vec::new();
    collect_scoped_mcids(elem, &mut scoped);
    mcids.extend(scoped.into_iter().map(|(_, mcid)| mcid));
}

/// Collect a cell's marked-content references with the content stream each one
/// names.
///
/// An MCID is unique only inside its own content stream (ISO 32000-1 §14.7.4.3),
/// so two Form XObjects on one page routinely both number a run 0. Carrying the
/// scope lets a cell tell its own run from an identically numbered one drawn by
/// a different stream.
fn collect_scoped_mcids(elem: &StructElem, out: &mut Vec<(McidScope, u32)>) {
    for child in &elem.children {
        match child {
            StructChild::MarkedContentRef { mcid, scope, .. } => {
                out.push((scope.clone(), *mcid));
            }
            StructChild::StructElem(child_elem) => {
                collect_scoped_mcids(child_elem, out);
            }
            StructChild::ObjectRef(_, _) => {}
        }
    }
}

/// Whether a text block is one of the runs this cell's references name.
///
/// The scope decides only where it can: a block whose stream matches a
/// reference naming that stream belongs to the cell. Where no block carries the
/// stream a reference names — the producer omitted `/Stm`, which is legal and
/// common — the MCID number alone decides, so those cells keep their text
/// instead of emptying.
fn block_belongs(block: &TextBlock, scoped: &[(McidScope, u32)]) -> bool {
    let Some(mcid) = block.mcid else { return false };
    let named: Vec<&McidScope> = scoped.iter().filter(|(_, m)| *m == mcid).map(|(s, _)| s).collect();
    if named.is_empty() {
        return false;
    }
    match &block.mcid_scope {
        // The block names the stream a reference names: it is this cell's run.
        Some(scope) if named.contains(&scope) => true,
        // The block names a stream, and it is not one this cell references:
        // a different stream's run that happens to share the number.
        Some(_) => false,
        // The block names no stream, so the producer omitted `/Stm`. The
        // number alone decides, which is what kept this cell's text before
        // scopes existed.
        None => true,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::structure::types::StructTreeRoot;

    /// A block whose bbox carries a non-finite coordinate, which a malformed
    /// file can produce. `perp()` multiplies x by a sine that is exactly 0.0 at
    /// rotation 0, so `inf * 0.0` is NaN on an axis the sort does not even use.
    fn block_at(x: f32, y: f32, mcid: u32, text: &str) -> TextBlock {
        let ch = crate::layout::TextChar {
            char: text.chars().next().unwrap_or('x'),
            bbox: Rect::new(x, y, 10.0, 10.0),
            font_name: "Test".to_string(),
            font_size: 10.0,
            font_weight: FontWeight::default(),
            is_italic: false,
            is_monospace: false,
            color: Color::default(),
            mcid: Some(mcid),
            origin_x: x,
            origin_y: y,
            rotation_degrees: 0.0,
            advance_width: 10.0,
            rendered_advance: 10.0,
            ascent: 8.0,
            descent: -2.0,
            matrix: None,
        };
        let mut block = TextBlock::from_chars(vec![ch]);
        block.bbox = Rect::new(x, y, 10.0, 10.0);
        block.text = text.to_string();
        block.mcid = Some(mcid);
        block
    }

    /// `sort_by` panics on a comparator that is not a total order, and NaN
    /// compares equal to everything. This class of panic has been fixed twice
    /// already; the cell sorts must go through `utils::safe_float_cmp`.
    #[test]
    fn cell_blocks_with_non_finite_bbox_do_not_panic() {
        // Enough blocks that `sort_by` takes its merge path: Rust's total-order
        // violation check is best-effort, and a three-element slice sorts by
        // insertion without ever comparing the offending pair. ~keep
        const N: u32 = 24;
        let children: Vec<StructChild> = (0..N)
            .map(|mcid| StructChild::MarkedContentRef {
                mcid,
                page: 0,
                scope: crate::structure::types::McidScope::Page(0),
            })
            .collect();
        let cell = StructElem {
            struct_type: StructType::TD,
            children,
            page: Some(0),
            attributes: std::collections::HashMap::new(),
            alt_text: None,
            expansion: None,
            actual_text: None,
            source_role: None,
        };
        // Two non-finite x coordinates scattered through the run. `perp`
        // multiplies x by a sine that is exactly 0.0 at rotation 0, so each
        // yields NaN on the axis the sort does not even use. ~keep
        let blocks: Vec<TextBlock> = (0..N)
            .map(|i| {
                // Four lines of six: y selects the line, x the position within
                // it. The perp sort decides line membership, so a NaN there
                // changes which line a block lands on. ~keep
                let x = if i == 7 || i == 17 {
                    f32::INFINITY
                } else {
                    10.0 * (i % 6) as f32
                };
                let text = ((b'a' + i as u8) as char).to_string();
                block_at(x, 20.0 + 40.0 * (i / 6) as f32, i, &text)
            })
            .collect();

        // The assertion is that this returns at all: with `partial_cmp(..)
        // .unwrap_or(Equal)` the NaN breaks transitivity and sort_by panics. ~keep
        let cell = extract_cell(&cell, &blocks, false).expect("cell extraction");
        assert!(
            cell.text.contains('a') && cell.text.contains('x'),
            "finite blocks must survive a non-finite sibling, got {:?}",
            cell.text
        );
    }

    #[test]
    fn test_table_new() {
        let table = Table::new();
        assert!(table.is_empty());
        assert_eq!(table.col_count, 0);
        assert!(!table.has_header);
        assert!(table.bbox.is_none());
    }

    #[test]
    fn test_table_bbox() {
        let mut table = Table::new();
        assert!(table.bbox.is_none());

        table.bbox = Some(Rect::new(10.0, 20.0, 100.0, 50.0));
        assert!(table.bbox.is_some());
        let bbox = table.bbox.unwrap();
        assert_eq!(bbox.x, 10.0);
        assert_eq!(bbox.y, 20.0);
        assert_eq!(bbox.width, 100.0);
        assert_eq!(bbox.height, 50.0);
    }

    #[test]
    fn test_table_row_new() {
        let header_row = TableRow::new(true);
        assert!(header_row.is_header);
        assert!(header_row.cells.is_empty());

        let body_row = TableRow::new(false);
        assert!(!body_row.is_header);
    }

    #[test]
    fn test_table_cell_new() {
        let cell = TableCell::new("Hello".to_string(), false);
        assert_eq!(cell.text, "Hello");
        assert!(!cell.is_header);
        assert_eq!(cell.colspan, 1);
        assert_eq!(cell.rowspan, 1);
        assert!(cell.mcids.is_empty());
    }

    #[test]
    fn test_table_cell_with_spans() {
        let cell = TableCell::new("Data".to_string(), false)
            .with_colspan(2)
            .with_rowspan(3);

        assert_eq!(cell.colspan, 2);
        assert_eq!(cell.rowspan, 3);
    }

    #[test]
    fn test_table_cell_header() {
        let cell = TableCell::new("Header".to_string(), true);
        assert!(cell.is_header);
    }

    #[test]
    fn test_table_row_add_cells() {
        let mut row = TableRow::new(false);
        row.add_cell(TableCell::new("Cell1".to_string(), false));
        row.add_cell(TableCell::new("Cell2".to_string(), false));

        assert_eq!(row.cells.len(), 2);
        assert_eq!(row.cells[0].text, "Cell1");
        assert_eq!(row.cells[1].text, "Cell2");
    }

    #[test]
    fn test_table_add_rows() {
        let mut table = Table::new();
        let mut row1 = TableRow::new(false);
        row1.add_cell(TableCell::new("A".to_string(), false));
        row1.add_cell(TableCell::new("B".to_string(), false));

        table.add_row(row1);
        assert_eq!(table.col_count, 2);
        assert_eq!(table.rows.len(), 1);
    }

    #[test]
    fn test_table_has_header() {
        let mut table = Table::new();
        assert!(!table.has_header);

        table.has_header = true;
        assert!(table.has_header);
    }

    /// Helper: create a minimal Table StructElem with MarkedContentRefs on a given page
    fn make_table_elem(page: u32, mcids: &[u32]) -> StructElem {
        let mut table = StructElem::new(StructType::Table);
        let mut tr = StructElem::new(StructType::TR);
        for &mcid in mcids {
            let mut td = StructElem::new(StructType::TD);
            td.add_child(StructChild::MarkedContentRef {
                mcid,
                page,
                scope: crate::structure::McidScope::Page(page),
            });
            tr.add_child(StructChild::StructElem(Box::new(td)));
        }
        table.add_child(StructChild::StructElem(Box::new(tr)));
        table
    }

    #[test]
    fn test_find_table_elements_finds_table_on_matching_page() {
        let mut tree = StructTreeRoot::new();
        tree.add_root_element(make_table_elem(0, &[1, 2]));

        let tables = find_table_elements(&tree, 0);
        assert_eq!(tables.len(), 1);
        assert_eq!(tables[0].struct_type, StructType::Table);
    }

    #[test]
    fn test_find_table_elements_skips_table_on_different_page() {
        let mut tree = StructTreeRoot::new();
        tree.add_root_element(make_table_elem(1, &[1, 2]));

        let tables = find_table_elements(&tree, 0);
        assert!(tables.is_empty());
    }

    #[test]
    fn test_find_table_elements_empty_tree() {
        let tree = StructTreeRoot::new();
        let tables = find_table_elements(&tree, 0);
        assert!(tables.is_empty());
    }

    #[test]
    fn test_find_table_elements_multiple_tables() {
        let mut tree = StructTreeRoot::new();
        tree.add_root_element(make_table_elem(0, &[1, 2]));
        tree.add_root_element(make_table_elem(0, &[3, 4]));

        let tables = find_table_elements(&tree, 0);
        assert_eq!(tables.len(), 2);
    }

    #[test]
    fn test_find_table_elements_nested_in_section() {
        let mut tree = StructTreeRoot::new();
        let mut sect = StructElem::new(StructType::Sect);
        sect.add_child(StructChild::StructElem(Box::new(make_table_elem(0, &[1]))));
        tree.add_root_element(sect);

        let tables = find_table_elements(&tree, 0);
        assert_eq!(tables.len(), 1);
    }

    #[test]
    fn test_find_table_elements_table_with_page_attribute() {
        let mut tree = StructTreeRoot::new();
        let mut table = StructElem::new(StructType::Table);
        table.page = Some(2);
        tree.add_root_element(table);

        let tables = find_table_elements(&tree, 2);
        assert_eq!(tables.len(), 1);
    }

    #[test]
    fn test_find_table_elements_mixed_pages() {
        let mut tree = StructTreeRoot::new();
        tree.add_root_element(make_table_elem(0, &[1]));
        tree.add_root_element(make_table_elem(1, &[2]));
        tree.add_root_element(make_table_elem(0, &[3]));

        let page0_tables = find_table_elements(&tree, 0);
        assert_eq!(page0_tables.len(), 2);

        let page1_tables = find_table_elements(&tree, 1);
        assert_eq!(page1_tables.len(), 1);
    }

    /// The single-walk all-pages bucketing must match, per page, the per-page
    /// `find_table_elements` walk it replaces.
    #[test]
    fn test_find_table_elements_all_pages_matches_per_page() {
        let mut tree = StructTreeRoot::new();
        tree.add_root_element(make_table_elem(0, &[1, 2]));
        let mut sect = StructElem::new(StructType::Sect);
        sect.add_child(StructChild::StructElem(Box::new(make_table_elem(0, &[3]))));
        tree.add_root_element(sect);
        tree.add_root_element(make_table_elem(1, &[4]));
        let mut page_attr_table = StructElem::new(StructType::Table);
        page_attr_table.page = Some(2);
        tree.add_root_element(page_attr_table);

        let all = find_table_elements_all_pages(&tree);
        for page in 0..4u32 {
            let per_page = find_table_elements(&tree, page);
            let bucket = all.get(&page).cloned().unwrap_or_default();
            assert_eq!(
                bucket.len(),
                per_page.len(),
                "page {page}: bucket count must match per-page walk"
            );
            for (b, p) in bucket.iter().zip(per_page.iter()) {
                // same DFS pre-order ⇒ structurally identical elements ~keep
                assert_eq!(b.struct_type, p.struct_type);
                assert_eq!(b.page, p.page);
                assert_eq!(b.children.len(), p.children.len());
            }
        }
    }

    #[test]
    fn test_find_table_elements_all_pages_empty_tree() {
        let tree = StructTreeRoot::new();
        assert!(find_table_elements_all_pages(&tree).is_empty());
    }

    #[test]
    fn test_element_has_page_content_via_mcid() {
        let mut elem = StructElem::new(StructType::P);
        elem.add_child(StructChild::MarkedContentRef {
            mcid: 1,
            page: 3,
            scope: crate::structure::McidScope::Page(3),
        });

        assert!(element_has_page_content(&elem, 3));
        assert!(!element_has_page_content(&elem, 0));
    }

    #[test]
    fn test_element_has_page_content_via_page_attribute() {
        let mut elem = StructElem::new(StructType::P);
        elem.page = Some(5);

        assert!(element_has_page_content(&elem, 5));
        assert!(!element_has_page_content(&elem, 0));
    }

    #[test]
    fn test_element_has_page_content_recursive() {
        let mut parent = StructElem::new(StructType::Sect);
        let mut child = StructElem::new(StructType::P);
        child.add_child(StructChild::MarkedContentRef {
            mcid: 1,
            page: 2,
            scope: crate::structure::McidScope::Page(2),
        });
        parent.add_child(StructChild::StructElem(Box::new(child)));

        assert!(element_has_page_content(&parent, 2));
        assert!(!element_has_page_content(&parent, 0));
    }

    #[test]
    fn test_element_has_page_content_empty() {
        let elem = StructElem::new(StructType::P);
        assert!(!element_has_page_content(&elem, 0));
    }

    #[test]
    fn test_element_has_page_content_object_ref_ignored() {
        let mut elem = StructElem::new(StructType::P);
        elem.add_child(StructChild::ObjectRef(1, 0));
        assert!(!element_has_page_content(&elem, 0));
    }

    fn make_text_span(text: &str, mcid: Option<u32>) -> crate::layout::TextSpan {
        use crate::layout::text_block::{Color, FontWeight};

        crate::layout::TextSpan {
            provenance: None,
            text_rise: 0.0,
            artifact_type: None,
            text: text.to_string(),
            bbox: Rect::new(0.0, 0.0, 50.0, 12.0),
            font_name: "Test".to_string(),
            font_size: 12.0,
            font_weight: FontWeight::Normal,
            is_italic: false,
            is_monospace: false,
            color: Color::black(),
            mcid,
            mcid_scope: None,
            sequence: 0,
            split_boundary_before: false,
            offset_semantic: false,
            char_spacing: 0.0,
            word_spacing: 0.0,
            horizontal_scaling: 1.0,
            primary_detected: false,
            char_widths: vec![],
            char_x_offsets: Vec::new(),
            heading_level: None,
            rotation_degrees: 0.0,
            wmode: 0,
            rtl_draw_logical: false,
            mirrored: false,
            page_rotation_applied: 0,
        }
    }

    #[test]
    fn test_extract_table_from_spans_basic() {
        let mut table_elem = StructElem::new(StructType::Table);
        let mut tr = StructElem::new(StructType::TR);
        let mut td1 = StructElem::new(StructType::TD);
        td1.add_child(StructChild::MarkedContentRef {
            mcid: 10,
            page: 0,
            scope: crate::structure::McidScope::Page(0),
        });
        let mut td2 = StructElem::new(StructType::TD);
        td2.add_child(StructChild::MarkedContentRef {
            mcid: 11,
            page: 0,
            scope: crate::structure::McidScope::Page(0),
        });
        tr.add_child(StructChild::StructElem(Box::new(td1)));
        tr.add_child(StructChild::StructElem(Box::new(td2)));
        table_elem.add_child(StructChild::StructElem(Box::new(tr)));

        let spans = vec![
            make_text_span("Hello", Some(10)),
            make_text_span("World", Some(11)),
            make_text_span("Unrelated", Some(99)),
        ];

        let result = extract_table_from_spans(&table_elem, &spans).unwrap();
        assert_eq!(result.rows.len(), 1);
        assert_eq!(result.rows[0].cells.len(), 2);
        assert_eq!(result.rows[0].cells[0].text, "Hello");
        assert_eq!(result.rows[0].cells[1].text, "World");
    }

    #[test]
    fn test_extract_table_from_spans_no_matching_mcids() {
        let mut table_elem = StructElem::new(StructType::Table);
        let mut tr = StructElem::new(StructType::TR);
        let mut td = StructElem::new(StructType::TD);
        td.add_child(StructChild::MarkedContentRef {
            mcid: 10,
            page: 0,
            scope: crate::structure::McidScope::Page(0),
        });
        tr.add_child(StructChild::StructElem(Box::new(td)));
        table_elem.add_child(StructChild::StructElem(Box::new(tr)));

        let spans = vec![make_text_span("Other", Some(99))];

        let result = extract_table_from_spans(&table_elem, &spans).unwrap();
        assert_eq!(result.rows.len(), 1);
        assert_eq!(result.rows[0].cells[0].text, "");
    }

    #[test]
    fn test_extract_table_from_spans_filters_no_mcid_spans() {
        let mut table_elem = StructElem::new(StructType::Table);
        let mut tr = StructElem::new(StructType::TR);
        let mut td = StructElem::new(StructType::TD);
        td.add_child(StructChild::MarkedContentRef {
            mcid: 5,
            page: 0,
            scope: crate::structure::McidScope::Page(0),
        });
        tr.add_child(StructChild::StructElem(Box::new(td)));
        table_elem.add_child(StructChild::StructElem(Box::new(tr)));

        let spans = vec![make_text_span("No MCID", None), make_text_span("Has MCID", Some(5))];

        let result = extract_table_from_spans(&table_elem, &spans).unwrap();
        assert_eq!(result.rows[0].cells[0].text, "Has MCID");
    }

    #[test]
    fn test_extract_table_from_spans_with_thead() {
        let mut table_elem = StructElem::new(StructType::Table);

        let mut thead = StructElem::new(StructType::THead);
        let mut hdr_tr = StructElem::new(StructType::TR);
        let mut th = StructElem::new(StructType::TH);
        th.add_child(StructChild::MarkedContentRef {
            mcid: 1,
            page: 0,
            scope: crate::structure::McidScope::Page(0),
        });
        hdr_tr.add_child(StructChild::StructElem(Box::new(th)));
        thead.add_child(StructChild::StructElem(Box::new(hdr_tr)));
        table_elem.add_child(StructChild::StructElem(Box::new(thead)));

        let mut tbody = StructElem::new(StructType::TBody);
        let mut body_tr = StructElem::new(StructType::TR);
        let mut td = StructElem::new(StructType::TD);
        td.add_child(StructChild::MarkedContentRef {
            mcid: 2,
            page: 0,
            scope: crate::structure::McidScope::Page(0),
        });
        body_tr.add_child(StructChild::StructElem(Box::new(td)));
        tbody.add_child(StructChild::StructElem(Box::new(body_tr)));
        table_elem.add_child(StructChild::StructElem(Box::new(tbody)));

        let spans = vec![make_text_span("Header", Some(1)), make_text_span("Data", Some(2))];

        let result = extract_table_from_spans(&table_elem, &spans).unwrap();
        assert!(result.has_header);
        assert_eq!(result.rows.len(), 2);
        assert!(result.rows[0].is_header);
        assert!(!result.rows[1].is_header);
        assert_eq!(result.rows[0].cells[0].text, "Header");
        assert_eq!(result.rows[1].cells[0].text, "Data");
    }

    #[test]
    fn test_extract_table_from_spans_empty_table() {
        let table_elem = StructElem::new(StructType::Table);
        let spans: Vec<crate::layout::TextSpan> = vec![];

        let result = extract_table_from_spans(&table_elem, &spans).unwrap();
        assert!(result.is_empty());
    }

    /// Adjacent MCID spans (gap ≤ 0) must NOT
    /// have a space inserted between them.  The PDF stores e.g. "Q" (MCID 1) and "("
    /// (MCID 2) as separate marked-content runs that abut each other on the same line.
    /// Before the fix, extract_cell always inserted a space between any two MCID blocks,
    /// producing "Q (peu/d)" instead of the correct "Q(peu/d)".
    #[test]
    fn test_extract_cell_adjacent_mcid_spans_no_space() {
        use crate::layout::text_block::{Color, FontWeight};

        let mut td = StructElem::new(StructType::TD);
        td.add_child(StructChild::MarkedContentRef {
            mcid: 1,
            page: 0,
            scope: crate::structure::McidScope::Page(0),
        });
        td.add_child(StructChild::MarkedContentRef {
            mcid: 2,
            page: 0,
            scope: crate::structure::McidScope::Page(0),
        });
        td.add_child(StructChild::MarkedContentRef {
            mcid: 3,
            page: 0,
            scope: crate::structure::McidScope::Page(0),
        });
        let mut tr = StructElem::new(StructType::TR);
        tr.add_child(StructChild::StructElem(Box::new(td)));
        let mut table_elem = StructElem::new(StructType::Table);
        table_elem.add_child(StructChild::StructElem(Box::new(tr)));

        // Exact coordinates for the Q(peu/d) column header case:
        //   "Q"     x=345.79 w=8.22  end=354.01
        //   "("    x=353.83 w=10.56 end=364.39   gap=-0.18 (overlap → no space)
        //   "peu/d" x=364.39 w=25.24             gap=0.00  (touching → no space) ~keep
        let base = crate::layout::TextSpan {
            provenance: None,
            text_rise: 0.0,
            artifact_type: None,
            text: String::new(),
            bbox: Rect::new(0.0, 678.0, 0.0, 10.56),
            font_name: "Test".to_string(),
            font_size: 10.56,
            font_weight: FontWeight::Normal,
            is_italic: false,
            is_monospace: false,
            color: Color::black(),
            mcid: None,
            mcid_scope: None,
            sequence: 0,
            split_boundary_before: false,
            offset_semantic: false,
            char_spacing: 0.0,
            word_spacing: 0.0,
            horizontal_scaling: 1.0,
            primary_detected: false,
            char_widths: vec![],
            char_x_offsets: Vec::new(),
            heading_level: None,
            rotation_degrees: 0.0,
            wmode: 0,
            rtl_draw_logical: false,
            mirrored: false,
            page_rotation_applied: 0,
        };
        let spans = vec![
            crate::layout::TextSpan {
                text: "Q".into(),
                bbox: Rect::new(345.79, 678.0, 8.22, 10.56),
                mcid: Some(1),
                mcid_scope: None,
                ..base.clone()
            },
            crate::layout::TextSpan {
                text: "(".into(),
                bbox: Rect::new(353.83, 678.0, 10.56, 10.56),
                mcid: Some(2),
                mcid_scope: None,
                ..base.clone()
            },
            crate::layout::TextSpan {
                text: "peu/d".into(),
                bbox: Rect::new(364.39, 678.0, 25.24, 10.56),
                mcid: Some(3),
                mcid_scope: None,
                ..base.clone()
            },
        ];

        let result = extract_table_from_spans(&table_elem, &spans).unwrap();
        assert_eq!(
            result.rows[0].cells[0].text, "Q(peu/d",
            "adjacent MCID spans must not get a space inserted between them"
        );
    }

    /// Companion test: MCID spans on different lines (multi-line cell) DO get a space.
    #[test]
    fn test_extract_cell_multiline_mcid_spans_have_space() {
        let mut td = StructElem::new(StructType::TD);
        td.add_child(StructChild::MarkedContentRef {
            mcid: 1,
            page: 0,
            scope: crate::structure::McidScope::Page(0),
        });
        td.add_child(StructChild::MarkedContentRef {
            mcid: 2,
            page: 0,
            scope: crate::structure::McidScope::Page(0),
        });
        let mut tr = StructElem::new(StructType::TR);
        tr.add_child(StructChild::StructElem(Box::new(td)));
        let mut table_elem = StructElem::new(StructType::Table);
        table_elem.add_child(StructChild::StructElem(Box::new(tr)));

        let base = crate::layout::TextSpan {
            provenance: None,
            text_rise: 0.0,
            artifact_type: None,
            text: String::new(),
            bbox: Rect::new(0.0, 0.0, 0.0, 12.0),
            font_name: "Test".to_string(),
            font_size: 12.0,
            font_weight: crate::layout::text_block::FontWeight::Normal,
            is_italic: false,
            is_monospace: false,
            color: crate::layout::text_block::Color::black(),
            mcid: None,
            mcid_scope: None,
            sequence: 0,
            split_boundary_before: false,
            offset_semantic: false,
            char_spacing: 0.0,
            word_spacing: 0.0,
            horizontal_scaling: 1.0,
            primary_detected: false,
            char_widths: vec![],
            char_x_offsets: Vec::new(),
            heading_level: None,
            rotation_degrees: 0.0,
            wmode: 0,
            rtl_draw_logical: false,
            mirrored: false,
            page_rotation_applied: 0,
        };
        // Line 1: "Hello" ends at x=100, y=200.  Line 2: "World" starts at x=10, y=188.
        // y_diff = 12 > line_h * 0.5 = 6 → different lines → space inserted. ~keep
        let spans = vec![
            crate::layout::TextSpan {
                text: "Hello".into(),
                bbox: Rect::new(10.0, 200.0, 90.0, 12.0),
                mcid: Some(1),
                mcid_scope: None,
                ..base.clone()
            },
            crate::layout::TextSpan {
                text: "World".into(),
                bbox: Rect::new(10.0, 188.0, 90.0, 12.0),
                mcid: Some(2),
                mcid_scope: None,
                ..base.clone()
            },
        ];

        let result = extract_table_from_spans(&table_elem, &spans).unwrap();
        assert_eq!(result.rows[0].cells[0].text, "Hello World");
    }

    /// The synthesized `cell.spans` on the tagged-PDF (MCID→TextBlock) path must
    /// carry per-block `font_weight`/`is_italic`, otherwise the markdown/HTML
    /// table renderers can't emit bold/italic markers and silently fall back to
    /// plain text. Also asserts the inter-line space is carried into the span
    /// text so renderers reconstructing from spans don't glue tokens across a
    /// wrapped line.
    #[test]
    fn test_extract_cell_spans_carry_bold_italic_and_spacing() {
        use crate::layout::text_block::{Color, FontWeight};

        let mut td = StructElem::new(StructType::TD);
        td.add_child(StructChild::MarkedContentRef {
            mcid: 1,
            page: 0,
            scope: crate::structure::McidScope::Page(0),
        });
        td.add_child(StructChild::MarkedContentRef {
            mcid: 2,
            page: 0,
            scope: crate::structure::McidScope::Page(0),
        });
        let mut tr = StructElem::new(StructType::TR);
        tr.add_child(StructChild::StructElem(Box::new(td)));
        let mut table_elem = StructElem::new(StructType::Table);
        table_elem.add_child(StructChild::StructElem(Box::new(tr)));

        let base = crate::layout::TextSpan {
            provenance: None,
            text_rise: 0.0,
            artifact_type: None,
            text: String::new(),
            bbox: Rect::new(0.0, 0.0, 0.0, 12.0),
            font_name: "Test".to_string(),
            font_size: 12.0,
            font_weight: FontWeight::Normal,
            is_italic: false,
            is_monospace: false,
            color: Color::black(),
            mcid: None,
            mcid_scope: None,
            sequence: 0,
            split_boundary_before: false,
            offset_semantic: false,
            char_spacing: 0.0,
            word_spacing: 0.0,
            horizontal_scaling: 1.0,
            primary_detected: false,
            char_widths: vec![],
            char_x_offsets: Vec::new(),
            heading_level: None,
            rotation_degrees: 0.0,
            wmode: 0,
            rtl_draw_logical: false,
            mirrored: false,
            page_rotation_applied: 0,
        };
        let spans = vec![
            crate::layout::TextSpan {
                text: "Bold".into(),
                bbox: Rect::new(10.0, 200.0, 40.0, 12.0),
                font_weight: FontWeight::Bold,
                mcid: Some(1),
                mcid_scope: None,
                ..base.clone()
            },
            crate::layout::TextSpan {
                text: "Italic".into(),
                bbox: Rect::new(10.0, 188.0, 40.0, 12.0),
                is_italic: true,
                mcid: Some(2),
                mcid_scope: None,
                ..base.clone()
            },
        ];

        let result = extract_table_from_spans(&table_elem, &spans).unwrap();
        let cell = &result.rows[0].cells[0];
        assert_eq!(cell.spans.len(), 2, "both MCID blocks must yield a span");
        assert_eq!(cell.spans[0].text, "Bold");
        assert!(
            matches!(cell.spans[0].font_weight, FontWeight::Bold),
            "bold block must propagate FontWeight::Bold into the synthesized span"
        );
        assert!(!cell.spans[0].is_italic, "non-italic block must not be italic");
        assert!(
            matches!(cell.spans[1].font_weight, FontWeight::Normal),
            "non-bold block must stay FontWeight::Normal"
        );
        assert!(
            cell.spans[1].is_italic,
            "italic block must propagate is_italic into the synthesized span"
        );
        assert_eq!(
            cell.spans[1].text, " Italic",
            "wrapped-line span must carry the leading inter-block space (review #533)"
        );
    }

    /// A figure a producer split across two marked-content runs must keep its
    /// own left-to-right order inside a right-to-left cell (UAX #9 class
    /// EN/AN); only the surrounding Arabic reverses. Blanket-reversing the
    /// whole cell reads "47.500" back as "7.5004", which changes the value
    /// rather than merely re-spacing it.
    #[test]
    fn rtl_table_cell_numeric_run_keeps_ltr_order() {
        // Visual layout, left to right on one line:
        //   تن   |  "4" "7.500"  |   حداقل
        //   x=5     x=30  x=37       x=75
        // Reading order is right to left, so حداقل comes first and تن last,
        // but the two numeral runs read "4" then "7.500" between them. ~keep
        let cell = rtl_cell_from(&[
            ("تن", 5.0, 20.0),
            ("4", 30.0, 7.0),
            ("7.500", 37.0, 33.0),
            ("حداقل", 75.0, 35.0),
        ]);

        assert!(
            cell.text.contains("47.500"),
            "the split figure must survive intact; got {:?}",
            cell.text
        );
        assert!(
            !cell.text.contains("7.5004"),
            "numerals must not inherit the surrounding script's reversal; got {:?}",
            cell.text
        );
        let arabic_first = cell.text.find("حداقل").expect("Arabic run present");
        let arabic_last = cell.text.find("تن").expect("Arabic run present");
        assert!(
            arabic_first < arabic_last,
            "the surrounding Arabic must still read right to left; got {:?}",
            cell.text
        );
    }

    /// The un-reversal is scoped to numerals: an all-Arabic cell keeps the
    /// plain right-to-left order the blanket reversal already produced.
    #[test]
    fn rtl_table_cell_without_numerals_is_unchanged() {
        let cell = rtl_cell_from(&[("تن", 5.0, 20.0), ("حداقل", 75.0, 35.0)]);
        assert_eq!(
            cell.text, "حداقل تن",
            "a cell with no numerals must read purely right to left"
        );
    }

    /// Build a single-cell tagged table from `(text, x, width)` runs laid out
    /// on one line, and return the extracted cell.
    fn rtl_cell_from(runs: &[(&str, f32, f32)]) -> crate::structure::TableCell {
        use crate::layout::text_block::{Color, FontWeight};

        let mut td = StructElem::new(StructType::TD);
        let mut spans = Vec::new();
        for (index, (text, x, width)) in runs.iter().enumerate() {
            let mcid = index as u32 + 1;
            td.add_child(StructChild::MarkedContentRef {
                mcid,
                page: 0,
                scope: crate::structure::McidScope::Page(0),
            });
            spans.push(crate::layout::TextSpan {
                provenance: None,
                text_rise: 0.0,
                artifact_type: None,
                text: (*text).into(),
                bbox: Rect::new(*x, 200.0, *width, 12.0),
                font_name: "Test".to_string(),
                font_size: 12.0,
                font_weight: FontWeight::Normal,
                is_italic: false,
                is_monospace: false,
                color: Color::black(),
                mcid: Some(mcid),
                mcid_scope: None,
                sequence: index,
                split_boundary_before: false,
                offset_semantic: false,
                char_spacing: 0.0,
                word_spacing: 0.0,
                horizontal_scaling: 1.0,
                primary_detected: false,
                char_widths: vec![],
                char_x_offsets: Vec::new(),
                heading_level: None,
                rotation_degrees: 0.0,
                wmode: 0,
                rtl_draw_logical: false,
                mirrored: false,
                page_rotation_applied: 0,
            });
        }
        let mut tr = StructElem::new(StructType::TR);
        tr.add_child(StructChild::StructElem(Box::new(td)));
        let mut table_elem = StructElem::new(StructType::Table);
        table_elem.add_child(StructChild::StructElem(Box::new(tr)));

        let result = extract_table_from_spans(&table_elem, &spans).unwrap();
        result.rows[0].cells[0].clone()
    }

    #[test]
    fn test_extract_cell_spans_carry_rotation_degrees() {
        use crate::layout::text_block::{Color, FontWeight};

        let mut td = StructElem::new(StructType::TD);
        td.add_child(StructChild::MarkedContentRef {
            mcid: 1,
            page: 0,
            scope: crate::structure::McidScope::Page(0),
        });
        let mut tr = StructElem::new(StructType::TR);
        tr.add_child(StructChild::StructElem(Box::new(td)));
        let mut table_elem = StructElem::new(StructType::Table);
        table_elem.add_child(StructChild::StructElem(Box::new(tr)));

        let span = crate::layout::TextSpan {
            provenance: None,
            text_rise: 0.0,
            mirrored: false,
            page_rotation_applied: 0,
            artifact_type: None,
            text: "Rotated".into(),
            bbox: Rect::new(10.0, 200.0, 40.0, 12.0),
            font_name: "Test".to_string(),
            font_size: 12.0,
            font_weight: FontWeight::Normal,
            is_italic: false,
            is_monospace: false,
            color: Color::black(),
            mcid: Some(1),
            mcid_scope: None,
            sequence: 0,
            split_boundary_before: false,
            offset_semantic: false,
            char_spacing: 0.0,
            word_spacing: 0.0,
            horizontal_scaling: 1.0,
            primary_detected: false,
            char_widths: vec![],
            char_x_offsets: Vec::new(),
            heading_level: None,
            rotation_degrees: 90.0,
            wmode: 0,
            rtl_draw_logical: false,
        };

        let result = extract_table_from_spans(&table_elem, &[span]).unwrap();
        let cell = &result.rows[0].cells[0];
        assert_eq!(cell.spans.len(), 1);
        assert_eq!(
            cell.spans[0].rotation_degrees, 90.0,
            "cell span re-synthesis must carry the source block's rotation through, \
             not silently reset it to 0.0"
        );
    }

    /// CJK + fullwidth operator with a gap that *exceeds* the 0.15em threshold must
    /// still suppress space insertion — this exercises the new CJK-suppression branch
    /// (the `test_extract_cell_adjacent_mcid_spans_no_space` test above only covers
    /// the gap ≤ threshold path, which never reaches this branch).
    #[test]
    fn test_extract_cell_cjk_fullwidth_gap_suppresses_space() {
        use crate::layout::text_block::{Color, FontWeight};

        // Build: TD with three MCIDs: "数" (CJK), "≤" (math op), "量" (CJK)
        // Place them with a gap of 3.0 pt (> font_size * 0.15 = 1.5 for 10 pt font)
        // so the gap branch fires, then the CJK suppression should prevent a space. ~keep
        let mut td = StructElem::new(StructType::TD);
        td.add_child(StructChild::MarkedContentRef {
            mcid: 10,
            page: 0,
            scope: crate::structure::McidScope::Page(0),
        });
        td.add_child(StructChild::MarkedContentRef {
            mcid: 11,
            page: 0,
            scope: crate::structure::McidScope::Page(0),
        });
        td.add_child(StructChild::MarkedContentRef {
            mcid: 12,
            page: 0,
            scope: crate::structure::McidScope::Page(0),
        });
        let mut tr = StructElem::new(StructType::TR);
        tr.add_child(StructChild::StructElem(Box::new(td)));
        let mut table_elem = StructElem::new(StructType::Table);
        table_elem.add_child(StructChild::StructElem(Box::new(tr)));

        let base = crate::layout::TextSpan {
            provenance: None,
            text_rise: 0.0,
            artifact_type: None,
            text: String::new(),
            bbox: Rect::new(0.0, 100.0, 0.0, 10.0),
            font_name: "Test".to_string(),
            font_size: 10.0,
            font_weight: FontWeight::Normal,
            is_italic: false,
            is_monospace: false,
            color: Color::black(),
            mcid: None,
            mcid_scope: None,
            sequence: 0,
            split_boundary_before: false,
            offset_semantic: false,
            char_spacing: 0.0,
            word_spacing: 0.0,
            horizontal_scaling: 1.0,
            primary_detected: false,
            char_widths: vec![],
            char_x_offsets: Vec::new(),
            heading_level: None,
            rotation_degrees: 0.0,
            wmode: 0,
            rtl_draw_logical: false,
            mirrored: false,
            page_rotation_applied: 0,
        };
        // "数" ends at x=10+10=20; "≤" starts at x=23 → gap=3.0 > 1.5 → gap branch fires
        // CJK("数")→math_op("≤") with at least one CJK side → suppress space
        // "≤" ends at x=23+10=33; "量" starts at x=36 → gap=3.0 → suppress space ~keep
        let spans = vec![
            crate::layout::TextSpan {
                text: "数".into(),
                bbox: Rect::new(10.0, 100.0, 10.0, 10.0),
                mcid: Some(10),
                mcid_scope: None,
                ..base.clone()
            },
            crate::layout::TextSpan {
                text: "≤".into(),
                bbox: Rect::new(23.0, 100.0, 10.0, 10.0),
                mcid: Some(11),
                mcid_scope: None,
                ..base.clone()
            },
            crate::layout::TextSpan {
                text: "量".into(),
                bbox: Rect::new(36.0, 100.0, 10.0, 10.0),
                mcid: Some(12),
                mcid_scope: None,
                ..base.clone()
            },
        ];

        let result = extract_table_from_spans(&table_elem, &spans).unwrap();
        assert_eq!(
            result.rows[0].cells[0].text, "数≤量",
            "CJK + math-op + CJK with gap > 0.15em should not have spaces inserted: \
             got '{}'",
            result.rows[0].cells[0].text
        );
    }

    /// Counterpart: Latin + Latin with a gap exceeding the threshold MUST insert a space.
    /// This guards that the CJK-suppression branch does not affect non-CJK pairs.
    #[test]
    fn test_extract_cell_latin_gap_inserts_space() {
        use crate::layout::text_block::{Color, FontWeight};

        let mut td = StructElem::new(StructType::TD);
        td.add_child(StructChild::MarkedContentRef {
            mcid: 20,
            page: 0,
            scope: crate::structure::McidScope::Page(0),
        });
        td.add_child(StructChild::MarkedContentRef {
            mcid: 21,
            page: 0,
            scope: crate::structure::McidScope::Page(0),
        });
        let mut tr = StructElem::new(StructType::TR);
        tr.add_child(StructChild::StructElem(Box::new(td)));
        let mut table_elem = StructElem::new(StructType::Table);
        table_elem.add_child(StructChild::StructElem(Box::new(tr)));

        let base = crate::layout::TextSpan {
            provenance: None,
            text_rise: 0.0,
            artifact_type: None,
            text: String::new(),
            bbox: Rect::new(0.0, 100.0, 0.0, 10.0),
            font_name: "Test".to_string(),
            font_size: 10.0,
            font_weight: FontWeight::Normal,
            is_italic: false,
            is_monospace: false,
            color: Color::black(),
            mcid: None,
            mcid_scope: None,
            sequence: 0,
            split_boundary_before: false,
            offset_semantic: false,
            char_spacing: 0.0,
            word_spacing: 0.0,
            horizontal_scaling: 1.0,
            primary_detected: false,
            char_widths: vec![],
            char_x_offsets: Vec::new(),
            heading_level: None,
            rotation_degrees: 0.0,
            wmode: 0,
            rtl_draw_logical: false,
            mirrored: false,
            page_rotation_applied: 0,
        };
        // "Hello" ends at 50; "world" starts at 53 → gap=3.0 > 1.5 → space inserted
        // Neither side is CJK, so the CJK suppression must NOT fire. ~keep
        let spans = vec![
            crate::layout::TextSpan {
                text: "Hello".into(),
                bbox: Rect::new(0.0, 100.0, 50.0, 10.0),
                mcid: Some(20),
                mcid_scope: None,
                ..base.clone()
            },
            crate::layout::TextSpan {
                text: "world".into(),
                bbox: Rect::new(53.0, 100.0, 30.0, 10.0),
                mcid: Some(21),
                mcid_scope: None,
                ..base.clone()
            },
        ];

        let result = extract_table_from_spans(&table_elem, &spans).unwrap();
        assert_eq!(
            result.rows[0].cells[0].text, "Hello world",
            "Latin→Latin with gap > 0.15em should insert a space: got '{}'",
            result.rows[0].cells[0].text
        );
    }
}