pdf_oxide 0.3.59

The fastest Rust PDF library with text extraction: 0.8ms mean, 100% pass rate on 3,830 PDFs. 5× faster than pdf_extract, 17× faster than oxidize_pdf. Extract, create, and edit PDFs.
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
//! 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::{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 (v0.3.14)
    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 (per #457 Step 4) 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 (issue 486) 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 %).
        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;
            }
            // Consolidated-table path: accept tables with many absolutely-dense
            // rows alongside sparse header/label rows (issue 486).
            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();
        }

        // Calculate column widths from cell content
        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;
            }
        }

        // Ensure minimum width of 2 per column
        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).
        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];

        // Detect right-aligned columns (all non-empty cells look like numbers/currency)
        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;
                                // Check if it looks like a number or currency value
                                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 {
                    // For colspan > 1, calculate merged width
                    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();
                        // Add 2 spaces per gap between merged columns
                        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 (v0.3.16).
    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
    }

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

/// 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 {
    // Check the element's own page attribute
    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".
    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,
            }
        })
        .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;
    // Must be an "N.M" pattern with all-digit parts and a single dot.
    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
    // (issue 487 nougat_018).  bbox.width can still be tight here, so the
    // bbox-inflation check below isn't sufficient.
    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=[]).
        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();

    // Check table structure
    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;
    }

    // Process all children
    for child in &table_elem.children {
        match child {
            StructChild::StructElem(elem) => match elem.struct_type {
                StructType::TR => {
                    // Direct row in table
                    let row = extract_row(elem, text_blocks, false)?;
                    table.add_row(row);
                },
                StructType::THead => {
                    // Header row group
                    extract_row_group(elem, text_blocks, true, &mut table)?;
                },
                StructType::TBody => {
                    // Body row group
                    extract_row_group(elem, text_blocks, false, &mut table)?;
                },
                StructType::TFoot => {
                    // Footer row group
                    extract_row_group(elem, text_blocks, false, &mut table)?;
                },
                _ => {
                    // Skip other elements (caption, etc.)
                },
            },
            StructChild::MarkedContentRef { .. } => {
                // Skip direct content references
            },
            StructChild::ObjectRef(_, _) => {
                // Skip object references
            },
        }
    }

    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);
            },
            _ => {
                // Skip non-row elements
            },
        }
    }
    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 => {
                    // Header cell
                    let cell = extract_cell(elem, text_blocks, true)?;
                    row.add_cell(cell);
                },
                StructType::TD => {
                    // Data cell
                    let cell = extract_cell(elem, text_blocks, false)?;
                    row.add_cell(cell);
                },
                _ => {
                    // Skip other elements
                },
            },
            StructChild::MarkedContentRef { .. } => {
                // Skip direct content references
            },
            StructChild::ObjectRef(_, _) => {
                // Skip object references
            },
        }
    }

    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> {
    // Collect all MCIDs from this cell
    let mut mcids = Vec::new();
    collect_mcids(cell_elem, &mut 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.
    let mut cell_text = String::new();
    // Issue #8 fix: also collect per-block style info as synthetic TextSpans
    // so the markdown renderer's `render_table_markdown` can emit bold /
    // italic markers per fragment. Without this, the tagged-PDF path
    // produced cells with empty `spans`, which the markdown renderer
    // falls back from to plain text — losing ~73% of inline formatting
    // in the reporter's 54-PDF corpus.
    let mut cell_spans: Vec<TextSpan> = Vec::new();
    let mut prev_block: Option<&TextBlock> = None;
    for mcid in &mcids {
        for block in text_blocks {
            if let Some(block_mcid) = block.mcid {
                if block_mcid == *mcid {
                    let mut leading_space = false;
                    if !cell_text.is_empty() {
                        let need_space = if let Some(prev) = prev_block {
                            let y_diff = (block.bbox.y - prev.bbox.y).abs();
                            let line_h = prev.bbox.height.max(block.bbox.height);
                            if y_diff > line_h * 0.5 {
                                // Different lines — always insert a space.
                                true
                            } else {
                                // Same line — only insert a space when there is an actual
                                // horizontal gap (> 15% of font size, matching document.rs).
                                let gap = block.bbox.x - (prev.bbox.x + prev.bbox.width);
                                let font_size =
                                    prev.avg_font_size.max(block.avg_font_size).max(1.0);
                                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 (Issue #485).
                                    #[inline(always)]
                                    fn is_cjk(c: char) -> bool {
                                        matches!(c,
                                            '\u{3040}'..='\u{309F}' |   // Hiragana
                                            '\u{30A0}'..='\u{30FF}' |   // Katakana
                                            '\u{4E00}'..='\u{9FFF}' |   // CJK Unified Ideographs
                                            '\u{AC00}'..='\u{D7AF}' |   // Hangul
                                            '\u{3400}'..='\u{4DBF}' |   // CJK Extension A
                                            '\u{20000}'..='\u{2A6DF}'   // CJK Extension B
                                        )
                                    }
                                    #[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.
                    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 {
                        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,
                        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![],
                        heading_level: None,
                    });
                    prev_block = Some(block);
                    break;
                }
            }
        }
    }

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

    Ok(cell)
}

/// Recursively collect all MCIDs from a structure element and its children.
fn collect_mcids(elem: &StructElem, mcids: &mut Vec<u32>) {
    for child in &elem.children {
        match child {
            StructChild::MarkedContentRef { mcid, .. } => {
                mcids.push(*mcid);
            },
            StructChild::StructElem(child_elem) => {
                // Recursively collect from child elements
                collect_mcids(child_elem, mcids);
            },
            StructChild::ObjectRef(_, _) => {
                // Skip object references
            },
        }
    }
}

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

    #[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);
    }

    // ============================================================================
    // find_table_elements() tests
    // ============================================================================

    /// 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 });
            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);
        // No MarkedContentRef children, but page attribute matches
        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);
    }

    // ============================================================================
    // element_has_page_content() tests
    // ============================================================================

    #[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 });

        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 });
        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));
    }

    // ============================================================================
    // extract_table_from_spans() tests
    // ============================================================================

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

        crate::layout::TextSpan {
            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,
            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![],
            heading_level: None,
        }
    }

    #[test]
    fn test_extract_table_from_spans_basic() {
        // Build a simple Table > TR > [TD, TD] structure
        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 });
        let mut td2 = StructElem::new(StructType::TD);
        td2.add_child(StructChild::MarkedContentRef { mcid: 11, 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 });
        tr.add_child(StructChild::StructElem(Box::new(td)));
        table_elem.add_child(StructChild::StructElem(Box::new(tr)));

        // Spans have different MCIDs
        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, ""); // No matching content
    }

    #[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 });
        tr.add_child(StructChild::StructElem(Box::new(td)));
        table_elem.add_child(StructChild::StructElem(Box::new(tr)));

        // Mix of spans with and without MCIDs
        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);

        // THead > TR > TH
        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 });
        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)));

        // TBody > TR > TD
        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 });
        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());
    }

    /// Regression test for issue-336-example: 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};

        // Build TD > [MCID 1, MCID 2, MCID 3]  (three adjacent spans on the same line)
        let mut td = StructElem::new(StructType::TD);
        td.add_child(StructChild::MarkedContentRef { mcid: 1, page: 0 });
        td.add_child(StructChild::MarkedContentRef { mcid: 2, page: 0 });
        td.add_child(StructChild::MarkedContentRef { mcid: 3, 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 from issue-336 page 0 (Q(peu/d) column header):
        //   "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)
        let base = crate::layout::TextSpan {
            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,
            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![],
            heading_level: None,
        };
        let spans = vec![
            crate::layout::TextSpan {
                text: "Q".into(),
                bbox: Rect::new(345.79, 678.0, 8.22, 10.56),
                mcid: Some(1),
                ..base.clone()
            },
            crate::layout::TextSpan {
                text: "".into(),
                bbox: Rect::new(353.83, 678.0, 10.56, 10.56),
                mcid: Some(2),
                ..base.clone()
            },
            crate::layout::TextSpan {
                text: "peu/d".into(),
                bbox: Rect::new(364.39, 678.0, 25.24, 10.56),
                mcid: Some(3),
                ..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 });
        td.add_child(StructChild::MarkedContentRef { mcid: 2, 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 {
            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,
            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![],
            heading_level: None,
        };
        // 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.
        let spans = vec![
            crate::layout::TextSpan {
                text: "Hello".into(),
                bbox: Rect::new(10.0, 200.0, 90.0, 12.0),
                mcid: Some(1),
                ..base.clone()
            },
            crate::layout::TextSpan {
                text: "World".into(),
                bbox: Rect::new(10.0, 188.0, 90.0, 12.0),
                mcid: Some(2),
                ..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 });
        td.add_child(StructChild::MarkedContentRef { mcid: 2, 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 {
            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,
            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![],
            heading_level: None,
        };
        // Line 1: bold "Bold" (y=200).  Line 2 (wrapped): italic "Italic" (y=188).
        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),
                ..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),
                ..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)"
        );
    }

    /// CJK + fullwidth operator with a gap that *exceeds* the 0.15em threshold must
    /// still suppress space insertion — this exercises the new CJK-suppression branch
    /// added in fix #485 (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.
        let mut td = StructElem::new(StructType::TD);
        td.add_child(StructChild::MarkedContentRef { mcid: 10, page: 0 });
        td.add_child(StructChild::MarkedContentRef { mcid: 11, page: 0 });
        td.add_child(StructChild::MarkedContentRef { mcid: 12, 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 {
            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,
            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![],
            heading_level: None,
        };
        // "数" 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
        let spans = vec![
            crate::layout::TextSpan {
                text: "".into(),
                bbox: Rect::new(10.0, 100.0, 10.0, 10.0),
                mcid: Some(10),
                ..base.clone()
            },
            crate::layout::TextSpan {
                text: "".into(),
                bbox: Rect::new(23.0, 100.0, 10.0, 10.0),
                mcid: Some(11),
                ..base.clone()
            },
            crate::layout::TextSpan {
                text: "".into(),
                bbox: Rect::new(36.0, 100.0, 10.0, 10.0),
                mcid: Some(12),
                ..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 });
        td.add_child(StructChild::MarkedContentRef { mcid: 21, 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 {
            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,
            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![],
            heading_level: None,
        };
        // "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.
        let spans = vec![
            crate::layout::TextSpan {
                text: "Hello".into(),
                bbox: Rect::new(0.0, 100.0, 50.0, 10.0),
                mcid: Some(20),
                ..base.clone()
            },
            crate::layout::TextSpan {
                text: "world".into(),
                bbox: Rect::new(53.0, 100.0, 30.0, 10.0),
                mcid: Some(21),
                ..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
        );
    }
}