unpdf 0.4.5

High-performance PDF content extraction to Markdown, text, and JSON
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
//! Layout analysis for PDF documents.
//!
//! This module provides text extraction with position and font information,
//! enabling proper heading detection, paragraph separation, and structure analysis.

use std::collections::HashMap;

use super::backend::{get_number_from_value, PdfBackend, PdfValue};
use crate::error::{Error, Result};

/// A text span with position and style information.
#[derive(Debug, Clone)]
pub struct TextSpan {
    /// The text content
    pub text: String,
    /// X position (left edge)
    pub x: f32,
    /// Y position (baseline)
    pub y: f32,
    /// Width of the text
    pub width: f32,
    /// Font size in points
    pub font_size: f32,
    /// Font name (e.g., "Helvetica-Bold")
    pub font_name: String,
    /// Whether the font appears to be bold
    pub is_bold: bool,
    /// Whether the font appears to be italic
    pub is_italic: bool,
}

impl TextSpan {
    /// Create a new text span.
    pub fn new(text: String, x: f32, y: f32, font_size: f32, font_name: String) -> Self {
        let is_bold = font_name.to_lowercase().contains("bold")
            || font_name.to_lowercase().contains("black")
            || font_name.to_lowercase().contains("heavy");
        let is_italic = font_name.to_lowercase().contains("italic")
            || font_name.to_lowercase().contains("oblique");

        Self {
            text,
            x,
            y,
            width: 0.0, // Will be calculated later if needed
            font_size,
            font_name,
            is_bold,
            is_italic,
        }
    }

    /// Get the bottom Y coordinate (approximate, based on font size).
    pub fn bottom(&self) -> f32 {
        self.y - self.font_size * 0.2 // Approximate descender
    }

    /// Get the top Y coordinate (approximate, based on font size).
    pub fn top(&self) -> f32 {
        self.y + self.font_size * 0.8 // Approximate ascender
    }
}

/// A text line composed of multiple spans on the same baseline.
#[derive(Debug, Clone)]
pub struct TextLine {
    /// The spans in this line, sorted by X position
    pub spans: Vec<TextSpan>,
    /// Y position (baseline)
    pub y: f32,
    /// Leftmost X position
    pub x: f32,
    /// Dominant font size in this line
    pub font_size: f32,
    /// Whether this line appears to be a heading
    pub is_heading: bool,
    /// Detected heading level (1-6, or 0 for non-heading)
    pub heading_level: u8,
}

impl TextLine {
    /// Create a new text line from spans.
    pub fn from_spans(mut spans: Vec<TextSpan>) -> Self {
        if spans.is_empty() {
            return Self {
                spans: vec![],
                y: 0.0,
                x: 0.0,
                font_size: 0.0,
                is_heading: false,
                heading_level: 0,
            };
        }

        // Sort spans by X position
        spans.sort_by(|a, b| a.x.partial_cmp(&b.x).unwrap_or(std::cmp::Ordering::Equal));

        // Calculate dominant font size (weighted by text length)
        let total_chars: usize = spans.iter().map(|s| s.text.len()).sum();
        let weighted_size: f32 = spans
            .iter()
            .map(|s| s.font_size * s.text.len() as f32)
            .sum();
        let font_size = if total_chars > 0 {
            weighted_size / total_chars as f32
        } else {
            spans[0].font_size
        };

        let y = spans[0].y;
        let x = spans[0].x;

        Self {
            spans,
            y,
            x,
            font_size,
            is_heading: false,
            heading_level: 0,
        }
    }

    /// Get the combined text of all spans with appropriate spacing.
    ///
    /// Inserts spaces between spans based on their X coordinate gaps.
    /// For CJK characters, no space is inserted between adjacent characters.
    pub fn text(&self) -> String {
        if self.spans.is_empty() {
            return String::new();
        }

        if self.spans.len() == 1 {
            return self.spans[0].text.clone();
        }

        let mut result = String::new();

        for (i, span) in self.spans.iter().enumerate() {
            if i == 0 {
                result.push_str(&span.text);
                continue;
            }

            let prev_span = &self.spans[i - 1];

            // Calculate gap between end of previous span and start of current span
            let prev_end = prev_span.x + prev_span.width;
            let gap = span.x - prev_end;

            // Estimate average character width from current span
            let char_count = span.text.chars().count();
            let avg_char_width = if char_count > 0 && span.width > 0.0 {
                span.width / char_count as f32
            } else {
                span.font_size * 0.5 // Fallback: assume half of font size
            };

            // Check if we need to insert a space
            // Gap threshold: if gap is more than 20% of average char width, insert space
            let space_threshold = avg_char_width * 0.2;

            // Get last char of previous span and first char of current span
            let prev_last_char = prev_span.text.chars().last();
            let curr_first_char = span.text.chars().next();

            let should_insert_space = if gap > space_threshold {
                // Check if both characters are CJK (no space needed between CJK chars)
                let prev_is_cjk = prev_last_char
                    .map(is_spaceless_script_char)
                    .unwrap_or(false);
                let curr_is_cjk = curr_first_char
                    .map(is_spaceless_script_char)
                    .unwrap_or(false);

                // Don't insert space between CJK characters
                !(prev_is_cjk && curr_is_cjk)
            } else {
                false
            };

            // Also check if previous span ends with space or current starts with space
            let prev_ends_with_space =
                prev_span.text.ends_with(' ') || prev_span.text.ends_with('\u{00A0}');
            let curr_starts_with_space =
                span.text.starts_with(' ') || span.text.starts_with('\u{00A0}');

            if should_insert_space && !prev_ends_with_space && !curr_starts_with_space {
                result.push(' ');
            }

            result.push_str(&span.text);
        }

        // Apply BiDi reordering for RTL scripts (Arabic, Hebrew, etc.)
        if super::bidi::contains_rtl(&result) {
            result = super::bidi::reorder_bidi(&result);
        }

        result
    }

    /// Check if the line is predominantly bold.
    pub fn is_bold(&self) -> bool {
        let bold_chars: usize = self
            .spans
            .iter()
            .filter(|s| s.is_bold)
            .map(|s| s.text.len())
            .sum();
        let total_chars: usize = self.spans.iter().map(|s| s.text.len()).sum();
        total_chars > 0 && bold_chars as f32 / total_chars as f32 > 0.5
    }

    /// Check if the line appears to be uppercase.
    pub fn is_uppercase(&self) -> bool {
        let text = self.text();
        let letters: Vec<char> = text.chars().filter(|c| c.is_alphabetic()).collect();
        !letters.is_empty() && letters.iter().all(|c| c.is_uppercase())
    }
}

/// A text block (paragraph, heading, etc.).
#[derive(Debug, Clone)]
pub struct TextBlock {
    /// The lines in this block
    pub lines: Vec<TextLine>,
    /// Block type
    pub block_type: BlockType,
    /// Heading level (1-6 for headings, 0 otherwise)
    pub heading_level: u8,
}

/// A detected column in the page layout.
#[derive(Debug, Clone)]
pub struct Column {
    /// Left boundary X coordinate
    pub left: f32,
    /// Right boundary X coordinate
    pub right: f32,
    /// Column index (0 = leftmost)
    pub index: usize,
}

impl Column {
    /// Check if an X coordinate falls within this column.
    pub fn contains(&self, x: f32) -> bool {
        x >= self.left && x <= self.right
    }

    /// Check if a span belongs to this column.
    pub fn contains_span(&self, span: &TextSpan) -> bool {
        // A span belongs to a column if its left edge is within the column
        // or if its center point is within the column
        let center = span.x + span.width / 2.0;
        self.contains(span.x) || self.contains(center)
    }
}

/// Type of text block.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BlockType {
    /// A heading (H1-H6)
    Heading,
    /// A regular paragraph
    Paragraph,
    /// A list item
    ListItem,
    /// Unknown or unclassified
    Unknown,
}

impl TextBlock {
    /// Create a new text block.
    pub fn new(lines: Vec<TextLine>, block_type: BlockType) -> Self {
        Self {
            lines,
            block_type,
            heading_level: 0,
        }
    }

    /// Get the combined text of all lines.
    pub fn text(&self) -> String {
        self.lines
            .iter()
            .map(|l| l.text())
            .collect::<Vec<_>>()
            .join(" ")
    }

    /// Check if the block is empty.
    pub fn is_empty(&self) -> bool {
        self.lines.is_empty() || self.text().trim().is_empty()
    }
}

/// Layout analyzer for extracting structured text from PDF pages.
pub struct LayoutAnalyzer<'a> {
    backend: &'a dyn PdfBackend,
    /// Font size statistics for the document
    font_stats: FontStatistics,
}

/// Font statistics for heading detection.
#[derive(Debug, Clone, Default)]
pub struct FontStatistics {
    /// Body text font size (most common)
    pub body_size: f32,
    /// Font sizes larger than body (potential headings)
    pub heading_sizes: Vec<f32>,
    /// All observed font sizes with frequency
    pub size_histogram: HashMap<i32, usize>,
}

impl FontStatistics {
    /// Add a font size observation.
    pub fn add_size(&mut self, size: f32) {
        let key = (size * 10.0) as i32; // Round to 0.1 precision
        *self.size_histogram.entry(key).or_insert(0) += 1;
    }

    /// Calculate body size and heading sizes.
    pub fn analyze(&mut self) {
        if self.size_histogram.is_empty() {
            self.body_size = 12.0;
            return;
        }

        // Find the most common font size (body text)
        let (body_key, _) = self
            .size_histogram
            .iter()
            .max_by_key(|(_, count)| *count)
            .unwrap();
        self.body_size = *body_key as f32 / 10.0;

        // Find sizes larger than body (potential headings)
        let mut larger_sizes: Vec<f32> = self
            .size_histogram
            .keys()
            .filter(|k| **k as f32 / 10.0 > self.body_size + 0.5)
            .map(|k| *k as f32 / 10.0)
            .collect();
        larger_sizes.sort_by(|a, b| b.partial_cmp(a).unwrap_or(std::cmp::Ordering::Equal));
        self.heading_sizes = larger_sizes;
    }

    /// Get heading level for a font size (1-6, or 0 for body text).
    ///
    /// Conservative: requires font size ≥ body + 2.5 to qualify, or body + 1.5
    /// combined with bold. Caps at level 4 to avoid `#####` spam.
    pub fn get_heading_level(&self, font_size: f32, is_bold: bool) -> u8 {
        let strong_threshold = self.body_size + 2.5;
        let bold_threshold = self.body_size + 1.5;

        let qualifies = font_size >= strong_threshold || (is_bold && font_size >= bold_threshold);
        if !qualifies {
            return 0;
        }

        // Rank within distinct heading-size tiers. We dedupe `heading_sizes`
        // on the fly so that sizes clustered within 2pt count as one tier —
        // Hancom docs have many discrete sizes that would otherwise produce
        // erratic H-level assignment.
        let mut tier = 0u8;
        let mut last_tier_size: Option<f32> = None;
        for &heading_size in &self.heading_sizes {
            let is_new_tier = match last_tier_size {
                None => true,
                Some(prev) => (prev - heading_size).abs() >= 2.0,
            };
            if is_new_tier {
                tier = tier.saturating_add(1);
                last_tier_size = Some(heading_size);
            }
            if font_size >= heading_size - 0.5 {
                return tier.min(4);
            }
        }
        4
    }
}

impl<'a> LayoutAnalyzer<'a> {
    /// Create a new layout analyzer.
    pub fn new(backend: &'a dyn PdfBackend) -> Self {
        Self {
            backend,
            font_stats: FontStatistics::default(),
        }
    }

    /// Get mutable reference to font statistics (for external use).
    pub fn font_stats_mut(&mut self) -> &mut FontStatistics {
        &mut self.font_stats
    }

    /// Public wrapper for group_spans_into_lines.
    pub fn group_spans_into_lines_pub(&self, spans: Vec<TextSpan>) -> Vec<TextLine> {
        self.group_spans_into_lines(spans)
    }

    /// Public wrapper for detect_headings.
    pub fn detect_headings_pub(&self, lines: Vec<TextLine>) -> Vec<TextLine> {
        self.detect_headings(lines)
    }

    /// Public wrapper for group_lines_into_blocks.
    pub fn group_lines_into_blocks_pub(&self, lines: Vec<TextLine>) -> Vec<TextBlock> {
        self.group_lines_into_blocks(lines)
    }

    /// Extract text spans from a page with position and font information.
    pub fn extract_page_spans(&self, page_num: u32) -> Result<Vec<TextSpan>> {
        let pages = self.backend.pages();
        let page_id = pages
            .get(&page_num)
            .ok_or(Error::PageOutOfRange(page_num, pages.len() as u32))?;

        // Build font info map
        let backend_fonts = self.backend.page_fonts(*page_id)?;
        let mut fonts = HashMap::new();
        for fi in &backend_fonts {
            fonts.insert(
                fi.name.clone(),
                FontInfo {
                    name: fi.base_font.clone(),
                },
            );
        }

        let content = self.backend.page_content(*page_id)?;
        self.parse_operations(&content, &fonts, *page_id)
    }

    /// Extract structured text blocks from a page.
    pub fn extract_page_blocks(&mut self, page_num: u32) -> Result<Vec<TextBlock>> {
        // Get page dimensions for header/footer filtering
        let pages = self.backend.pages();
        let page_id = pages
            .get(&page_num)
            .ok_or(Error::PageOutOfRange(page_num, pages.len() as u32))?;
        let (_page_width, page_height) = self.backend.page_dimensions(*page_id);

        let mut spans = self.extract_page_spans(page_num)?;

        // Filter out page numbers / running headers from top/bottom margins
        filter_header_footer_spans(&mut spans, page_height);

        // Update font statistics
        for span in &spans {
            self.font_stats.add_size(span.font_size);
        }
        self.font_stats.analyze();

        // Group spans into lines
        let lines = self.group_spans_into_lines(spans);

        // Detect headings
        let lines = self.detect_headings(lines);

        // Group lines into blocks (paragraphs)
        let blocks = self.group_lines_into_blocks(lines);

        Ok(blocks)
    }

    /// Parse content stream operations into text spans.
    ///
    /// Delegates content decoding and text decoding to the backend,
    /// keeping layout.rs free from concrete PDF library types.
    fn parse_operations(
        &self,
        content: &[u8],
        fonts: &HashMap<Vec<u8>, FontInfo>,
        page_id: super::backend::PageId,
    ) -> Result<Vec<TextSpan>> {
        let operations = self.backend.decode_content(content)?;

        let mut spans = Vec::new();
        let mut current_font = String::new();
        let mut current_font_name: Vec<u8> = Vec::new();
        let mut current_font_size: f32 = 12.0;
        let mut text_matrix = TextMatrix::default();
        let mut in_text_block = false;

        for op in &operations {
            match op.operator.as_str() {
                "BT" => {
                    in_text_block = true;
                    text_matrix = TextMatrix::default();
                }
                "ET" => {
                    in_text_block = false;
                }
                "Tf" => {
                    if op.operands.len() >= 2 {
                        if let PdfValue::Name(font_name) = &op.operands[0] {
                            current_font_name = font_name.clone();
                            if let Some(info) = fonts.get(font_name.as_slice()) {
                                current_font = info.name.clone();
                            } else {
                                current_font =
                                    String::from_utf8_lossy(font_name.as_slice()).to_string();
                            }
                        }
                        current_font_size = get_number_from_value(&op.operands[1]).unwrap_or(12.0);
                    }
                }
                "Td" | "TD" => {
                    if op.operands.len() >= 2 {
                        let tx = get_number_from_value(&op.operands[0]).unwrap_or(0.0);
                        let ty = get_number_from_value(&op.operands[1]).unwrap_or(0.0);
                        text_matrix.translate(tx, ty);
                    }
                }
                "Tm" => {
                    if op.operands.len() >= 6 {
                        text_matrix.set(
                            get_number_from_value(&op.operands[0]).unwrap_or(1.0),
                            get_number_from_value(&op.operands[1]).unwrap_or(0.0),
                            get_number_from_value(&op.operands[2]).unwrap_or(0.0),
                            get_number_from_value(&op.operands[3]).unwrap_or(1.0),
                            get_number_from_value(&op.operands[4]).unwrap_or(0.0),
                            get_number_from_value(&op.operands[5]).unwrap_or(0.0),
                        );
                    }
                }
                "T*" => {
                    text_matrix.next_line();
                }
                "Tj" | "TJ" => {
                    if in_text_block {
                        let text = if op.operator == "TJ" {
                            // TJ: array of strings and positioning adjustments
                            // Numbers indicate kerning/spacing adjustments in 1/1000 text space units
                            // Large negative values (like -200 to -300) often indicate word spaces
                            if let Some(PdfValue::Array(arr)) = op.operands.first() {
                                let mut combined = String::new();

                                for item in arr {
                                    match item {
                                        PdfValue::Str(bytes) => {
                                            combined.push_str(&self.backend.decode_text(
                                                page_id,
                                                &current_font_name,
                                                bytes,
                                            ));
                                        }
                                        PdfValue::Integer(n) => {
                                            let adjustment = -(*n as f32);
                                            maybe_insert_space_tj(&mut combined, adjustment);
                                        }
                                        PdfValue::Real(n) => {
                                            let adjustment = -n;
                                            maybe_insert_space_tj(&mut combined, adjustment);
                                        }
                                        _ => {}
                                    }
                                }
                                combined
                            } else {
                                String::new()
                            }
                        } else {
                            // Tj: single string
                            if let Some(PdfValue::Str(bytes)) = op.operands.first() {
                                self.backend.decode_text(page_id, &current_font_name, bytes)
                            } else {
                                String::new()
                            }
                        };

                        if !text.trim().is_empty() {
                            let (x, y) = text_matrix.get_position();
                            let effective_size = current_font_size * text_matrix.get_scale();
                            spans.push(TextSpan::new(
                                text,
                                x,
                                y,
                                effective_size,
                                current_font.clone(),
                            ));
                        }
                    }
                }
                "'" | "\"" => {
                    text_matrix.next_line();
                    if in_text_block {
                        let text_idx = if op.operator == "\"" { 2 } else { 0 };
                        if let Some(PdfValue::Str(bytes)) = op.operands.get(text_idx) {
                            let text = self.backend.decode_text(page_id, &current_font_name, bytes);

                            if !text.trim().is_empty() {
                                let (x, y) = text_matrix.get_position();
                                let effective_size = current_font_size * text_matrix.get_scale();
                                spans.push(TextSpan::new(
                                    text,
                                    x,
                                    y,
                                    effective_size,
                                    current_font.clone(),
                                ));
                            }
                        }
                    }
                }
                _ => {}
            }
        }

        Ok(spans)
    }

    /// Detect columns in a page based on vertical gap (gutter) detection.
    ///
    /// This looks for vertical empty spaces between text regions to identify
    /// column boundaries. Returns columns sorted from left to right.
    fn detect_columns(&self, spans: &[TextSpan]) -> Vec<Column> {
        if spans.is_empty() {
            return vec![];
        }

        // Find minimum and maximum X to determine page extent
        let min_x = spans
            .iter()
            .map(|s| s.x)
            .min_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .unwrap_or(0.0);
        let max_x = spans
            .iter()
            .map(|s| s.x + s.width)
            .max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
            .unwrap_or(0.0);

        let page_width = max_x - min_x;

        // Don't detect columns if page is too narrow
        if page_width < 250.0 {
            return vec![Column {
                left: min_x - 10.0,
                right: max_x + 10.0,
                index: 0,
            }];
        }

        // Divide page into vertical slices and count spans in each
        let slice_width = 3.0; // Finer slices for better precision
        let num_slices = ((page_width / slice_width) as usize) + 1;
        let mut slice_occupancy = vec![0usize; num_slices];

        // Count how many spans occupy each slice
        for span in spans {
            let start_slice = ((span.x - min_x) / slice_width) as usize;
            let end_slice = (((span.x + span.width) - min_x) / slice_width) as usize;

            for slot in slice_occupancy
                .iter_mut()
                .take(end_slice.min(num_slices - 1) + 1)
                .skip(start_slice)
            {
                *slot += 1;
            }
        }

        // Find the largest gap (sequence of empty slices) in the middle 70% of the page
        // Extended from 50% to catch more gutters
        let search_start = num_slices * 15 / 100; // Start at 15%
        let search_end = num_slices * 85 / 100; // End at 85%

        let mut best_gap_start = 0;
        let mut best_gap_len = 0;
        let mut best_gap_center_dist = f32::MAX; // Distance from center

        let page_center = num_slices / 2;
        let mut current_gap_start = 0;
        let mut current_gap_len = 0;

        for (i, &occupancy) in slice_occupancy
            .iter()
            .enumerate()
            .take(search_end)
            .skip(search_start)
        {
            if occupancy == 0 {
                if current_gap_len == 0 {
                    current_gap_start = i;
                }
                current_gap_len += 1;
            } else {
                if current_gap_len > 0 {
                    let gap_center = current_gap_start + current_gap_len / 2;
                    let center_dist = (gap_center as i32 - page_center as i32).abs() as f32;

                    // Prefer gaps that are:
                    // 1. Larger (more confident it's a gutter)
                    // 2. Closer to center (more likely to be a column separator)
                    let current_gap_width = current_gap_len as f32 * slice_width;

                    if current_gap_width >= 10.0 {
                        // Minimum 10pt gap
                        // Score: gap_width * (1 - center_distance_ratio)
                        let best_gap_width = best_gap_len as f32 * slice_width;

                        // Prefer larger gaps, or similar-sized gaps closer to center
                        if current_gap_width > best_gap_width * 1.5
                            || (current_gap_width >= best_gap_width * 0.7
                                && center_dist < best_gap_center_dist)
                        {
                            best_gap_start = current_gap_start;
                            best_gap_len = current_gap_len;
                            best_gap_center_dist = center_dist;
                        }
                    }
                }
                current_gap_len = 0;
            }
        }

        // Check the last gap
        if current_gap_len > 0 {
            let gap_center = current_gap_start + current_gap_len / 2;
            let center_dist = (gap_center as i32 - page_center as i32).abs() as f32;
            let current_gap_width = current_gap_len as f32 * slice_width;
            let best_gap_width = best_gap_len as f32 * slice_width;

            if current_gap_width >= 10.0
                && (current_gap_width > best_gap_width * 1.5
                    || (current_gap_width >= best_gap_width * 0.7
                        && center_dist < best_gap_center_dist))
            {
                best_gap_start = current_gap_start;
                best_gap_len = current_gap_len;
            }
        }

        // Convert gap to actual X coordinates
        let gap_width = best_gap_len as f32 * slice_width;

        log::debug!(
            "Best gap: width={:.1}pt at x={:.1}, page_width={:.1}",
            gap_width,
            min_x + best_gap_start as f32 * slice_width,
            page_width
        );

        // Require a minimum gap width for column detection (at least 12 points)
        if gap_width < 12.0 {
            log::debug!("Gap too small (< 12pt), treating as single column");
            return vec![Column {
                left: min_x - 10.0,
                right: max_x + 10.0,
                index: 0,
            }];
        }

        // Calculate gutter center
        let gutter_center =
            min_x + (best_gap_start as f32 + best_gap_len as f32 / 2.0) * slice_width;

        // Validate that both columns have reasonable width (at least 80 points each)
        let left_col_width = gutter_center - min_x;
        let right_col_width = max_x - gutter_center;

        log::debug!(
            "Column widths: left={:.1}, right={:.1}",
            left_col_width,
            right_col_width
        );

        if left_col_width < 80.0 || right_col_width < 80.0 {
            log::debug!("Column too narrow, treating as single column");
            return vec![Column {
                left: min_x - 10.0,
                right: max_x + 10.0,
                index: 0,
            }];
        }

        // Validate that both columns have spans
        let left_spans = spans
            .iter()
            .filter(|s| s.x + s.width / 2.0 < gutter_center)
            .count();
        let right_spans = spans
            .iter()
            .filter(|s| s.x + s.width / 2.0 >= gutter_center)
            .count();

        log::debug!(
            "Spans: left={}, right={}, total={}",
            left_spans,
            right_spans,
            spans.len()
        );

        // Both columns should have at least 10% of spans
        let min_spans = spans.len() / 10;
        if left_spans < min_spans.max(2) || right_spans < min_spans.max(2) {
            log::debug!("Spans too imbalanced, treating as single column");
            return vec![Column {
                left: min_x - 10.0,
                right: max_x + 10.0,
                index: 0,
            }];
        }

        vec![
            Column {
                left: min_x - 10.0,
                right: gutter_center,
                index: 0,
            },
            Column {
                left: gutter_center,
                right: max_x + 10.0,
                index: 1,
            },
        ]
    }

    /// Group spans into lines based on Y position, using XY-Cut for layout segmentation.
    ///
    /// Uses the recursive XY-Cut algorithm to detect multi-column layouts and
    /// other complex structures. Each segmented region is processed independently
    /// as a single-column block.
    fn group_spans_into_lines(&self, spans: Vec<TextSpan>) -> Vec<TextLine> {
        if spans.is_empty() {
            return vec![];
        }

        // Convert spans to XY-cut blocks
        let blocks: Vec<super::xycut::Block> = spans
            .iter()
            .map(|s| super::xycut::Block {
                x: s.x,
                y: s.y,
                width: s.width,
                height: s.font_size,
            })
            .collect();

        // Determine gap thresholds based on median font size. These are
        // intentionally large so XY-Cut only fires on true multi-column
        // layouts — not on intra-table cell gaps or bulleted list
        // indentation, which previously fragmented pages into dozens of
        // groups on Hancom-produced PDFs.
        let median_font = median_font_size(&spans);
        let min_x_gap = (median_font * 5.0).max(60.0);
        let min_y_gap = (median_font * 3.0).max(36.0);

        let groups = super::xycut::xycut_segment(&blocks, min_x_gap, min_y_gap);

        log::debug!(
            "XY-Cut segmented {} spans into {} groups (median_font={:.1}, min_x_gap={:.1}, min_y_gap={:.1})",
            spans.len(),
            groups.len(),
            median_font,
            min_x_gap,
            min_y_gap,
        );

        if groups.len() <= 1 {
            // Single column — use simple grouping
            return self.group_spans_into_lines_single_column(spans);
        }

        // Multi-column: process each group independently
        let mut all_lines = Vec::new();
        for group in &groups {
            // Match spans to this group's blocks by position
            let group_spans: Vec<TextSpan> = spans
                .iter()
                .filter(|s| {
                    group
                        .iter()
                        .any(|b| (s.x - b.x).abs() < 1.0 && (s.y - b.y).abs() < 1.0)
                })
                .cloned()
                .collect();
            let lines = self.group_spans_into_lines_single_column(group_spans);
            all_lines.extend(lines);
        }
        all_lines
    }

    /// Group spans into lines using the legacy column-detection approach.
    ///
    /// This method uses `detect_columns()` to find a single gutter and split
    /// spans into columns. Kept as fallback; the primary path now uses XY-Cut
    /// via `group_spans_into_lines()`.
    #[allow(dead_code)]
    fn group_spans_into_lines_legacy_columns(&self, spans: Vec<TextSpan>) -> Vec<TextLine> {
        if spans.is_empty() {
            return vec![];
        }

        // Detect columns first
        let columns = self.detect_columns(&spans);

        log::debug!("Detected {} columns", columns.len());
        for col in &columns {
            log::debug!(
                "  Column {}: left={:.1}, right={:.1}",
                col.index,
                col.left,
                col.right
            );
        }

        // If single column, use simple Y-based grouping
        if columns.len() <= 1 {
            return self.group_spans_into_lines_single_column(spans);
        }

        // Multi-column layout: process each column separately, then interleave
        let mut column_lines: Vec<Vec<TextLine>> = vec![Vec::new(); columns.len()];

        // Assign spans to columns
        let mut column_spans: Vec<Vec<TextSpan>> = vec![Vec::new(); columns.len()];
        for span in spans {
            // Find which column this span belongs to
            let col_idx = columns
                .iter()
                .position(|c| c.contains_span(&span))
                .unwrap_or(0);
            column_spans[col_idx].push(span);
        }

        log::debug!(
            "Spans per column: {:?}",
            column_spans.iter().map(|v| v.len()).collect::<Vec<_>>()
        );

        // Group each column's spans into lines
        for (col_idx, col_spans) in column_spans.into_iter().enumerate() {
            column_lines[col_idx] = self.group_spans_into_lines_single_column(col_spans);
        }

        // Interleave lines from columns by Y position (top to bottom reading order)
        // First, collect all lines with their column index
        let mut all_lines: Vec<(usize, TextLine)> = Vec::new();
        for (col_idx, lines) in column_lines.into_iter().enumerate() {
            for line in lines {
                all_lines.push((col_idx, line));
            }
        }

        // Read each column sequentially: all lines from column 0, then column 1, etc.
        // Within each column, maintain top-to-bottom order (Y descending).
        all_lines.sort_by(|(col_a, line_a), (col_b, line_b)| {
            let col_cmp = col_a.cmp(col_b);
            if col_cmp != std::cmp::Ordering::Equal {
                col_cmp
            } else {
                line_b
                    .y
                    .partial_cmp(&line_a.y)
                    .unwrap_or(std::cmp::Ordering::Equal)
            }
        });

        all_lines.into_iter().map(|(_, line)| line).collect()
    }

    /// Simple Y-based line grouping for single-column layout.
    fn group_spans_into_lines_single_column(&self, spans: Vec<TextSpan>) -> Vec<TextLine> {
        if spans.is_empty() {
            return vec![];
        }

        // Sort spans by Y (descending, since PDF Y is bottom-up) then X
        let mut spans = spans;
        spans.sort_by(|a, b| {
            let y_cmp = b.y.partial_cmp(&a.y).unwrap_or(std::cmp::Ordering::Equal);
            if y_cmp == std::cmp::Ordering::Equal {
                a.x.partial_cmp(&b.x).unwrap_or(std::cmp::Ordering::Equal)
            } else {
                y_cmp
            }
        });

        // Merge adjacent single/few-character spans that likely form words.
        // This fixes the "w arranty", "M ac B ook" splitting caused by
        // per-character text rendering in some PDFs.
        spans = merge_fragmented_spans(spans);

        let mut lines: Vec<TextLine> = Vec::new();
        let mut current_line_spans: Vec<TextSpan> = Vec::new();
        let mut current_y: Option<f32> = None;

        for span in spans {
            let y_tolerance = span.font_size * 0.3; // Allow 30% of font size variance

            if let Some(y) = current_y {
                if (span.y - y).abs() <= y_tolerance {
                    // Same line
                    current_line_spans.push(span);
                } else {
                    // New line
                    if !current_line_spans.is_empty() {
                        lines.push(TextLine::from_spans(std::mem::take(
                            &mut current_line_spans,
                        )));
                    }
                    current_y = Some(span.y);
                    current_line_spans.push(span);
                }
            } else {
                current_y = Some(span.y);
                current_line_spans.push(span);
            }
        }

        // Don't forget the last line
        if !current_line_spans.is_empty() {
            lines.push(TextLine::from_spans(current_line_spans));
        }

        lines
    }

    /// Detect headings based on font size hierarchy.
    fn detect_headings(&self, mut lines: Vec<TextLine>) -> Vec<TextLine> {
        // Snapshot each line's font size so neighbour lookups aren't polluted
        // by mutations inside the loop.
        let sizes: Vec<f32> = lines.iter().map(|l| l.font_size).collect();
        let body_size = self.font_stats.body_size;

        for (i, line) in lines.iter_mut().enumerate() {
            let visible_chars: usize = line
                .text()
                .chars()
                .filter(|c| !c.is_whitespace() && !c.is_ascii_punctuation())
                .count();
            if visible_chars < 3 {
                continue;
            }

            // List / bullet marker exclusion — never promote a line that
            // begins with a common bullet/list glyph. These are inline
            // enumerations inside body content, regardless of font size.
            let trimmed = line.text();
            let trimmed = trimmed.trim_start();
            if let Some(first) = trimmed.chars().next() {
                const BULLETS: &[char] = &['-', '*', '·', '', '', '', '', '', '', ''];
                if BULLETS.contains(&first) {
                    continue;
                }
            }

            let level = self
                .font_stats
                .get_heading_level(line.font_size, line.is_bold() || line.is_uppercase());
            if level == 0 {
                continue;
            }

            // Neighbour-context suppression — if both prev and next lines
            // share the same font size (within 0.5pt), this line is part of
            // a body run, not a standalone heading. Prevents mid-paragraph
            // or table-cell promotion on Hancom docs where body font varies
            // by a few pts across cells.
            let prev_size = if i > 0 { Some(sizes[i - 1]) } else { None };
            let next_size = if i + 1 < sizes.len() {
                Some(sizes[i + 1])
            } else {
                None
            };
            let same = |a: f32, b: f32| (a - b).abs() < 0.5;
            // Neighbour-context suppression — if EITHER adjacent line
            // shares the same font size (±0.5pt), this line is likely part
            // of a sibling run (table column, list). Only promote when
            // the line sits alone within its font-size cohort.
            let matches_prev = prev_size.is_some_and(|p| same(p, line.font_size));
            let matches_next = next_size.is_some_and(|n| same(n, line.font_size));
            if (matches_prev || matches_next) && line.font_size < body_size + 6.0 {
                continue;
            }

            line.is_heading = true;
            line.heading_level = level;
        }
        lines
    }

    /// Group lines into blocks (paragraphs) based on spacing.
    fn group_lines_into_blocks(&self, lines: Vec<TextLine>) -> Vec<TextBlock> {
        if lines.is_empty() {
            return vec![];
        }

        let mut blocks: Vec<TextBlock> = Vec::new();
        let mut current_block_lines: Vec<TextLine> = Vec::new();

        // Calculate average line spacing
        let avg_spacing = self.calculate_avg_line_spacing(&lines);

        for (i, line) in lines.into_iter().enumerate() {
            if i == 0 {
                current_block_lines.push(line);
                continue;
            }

            let prev_line = current_block_lines.last().unwrap();

            // Check if this should start a new block
            let should_break = self.should_break_block(prev_line, &line, avg_spacing);

            if should_break {
                // Create block from current lines
                if !current_block_lines.is_empty() {
                    let block_type = if current_block_lines.iter().any(|l| l.is_heading) {
                        BlockType::Heading
                    } else {
                        BlockType::Paragraph
                    };
                    let mut block =
                        TextBlock::new(std::mem::take(&mut current_block_lines), block_type);
                    if block_type == BlockType::Heading {
                        block.heading_level = block
                            .lines
                            .iter()
                            .filter(|l| l.is_heading)
                            .map(|l| l.heading_level)
                            .min()
                            .unwrap_or(0);
                    }
                    blocks.push(block);
                }
            }

            current_block_lines.push(line);
        }

        // Don't forget the last block
        if !current_block_lines.is_empty() {
            let block_type = if current_block_lines.iter().any(|l| l.is_heading) {
                BlockType::Heading
            } else {
                BlockType::Paragraph
            };
            let mut block = TextBlock::new(current_block_lines, block_type);
            if block_type == BlockType::Heading {
                block.heading_level = block
                    .lines
                    .iter()
                    .filter(|l| l.is_heading)
                    .map(|l| l.heading_level)
                    .min()
                    .unwrap_or(0);
            }
            blocks.push(block);
        }

        blocks
    }

    /// Calculate average line spacing.
    fn calculate_avg_line_spacing(&self, lines: &[TextLine]) -> f32 {
        if lines.len() < 2 {
            return 12.0; // Default
        }

        let spacings: Vec<f32> = lines
            .windows(2)
            .map(|w| (w[0].y - w[1].y).abs())
            .filter(|s| *s > 0.1) // Filter out very small spacings
            .collect();

        if spacings.is_empty() {
            return 12.0;
        }

        spacings.iter().sum::<f32>() / spacings.len() as f32
    }

    /// Determine if a new block should start.
    fn should_break_block(
        &self,
        prev_line: &TextLine,
        curr_line: &TextLine,
        avg_spacing: f32,
    ) -> bool {
        // Heading always starts a new block, UNLESS the previous line is
        // also a heading of the same level sitting close by (within ~2x
        // line-height). This merges decorative stacked titles on covers —
        // e.g. "스마트\n제조혁신\n통합공고" stays one heading block.
        if curr_line.is_heading {
            // Merge consecutive heading lines that are spatially close and
            // at similar font size (≤ 2pt delta). Level difference is
            // tolerated — decorative stacked titles often vary font size
            // per word. The block picks up the minimum (most prominent)
            // level via existing `block.heading_level = ...min()` logic.
            if prev_line.is_heading && (prev_line.font_size - curr_line.font_size).abs() <= 2.0 {
                let gap = (prev_line.y - curr_line.y).abs();
                let bigger = prev_line.font_size.max(curr_line.font_size);
                let close = gap <= (bigger * 2.5).max(avg_spacing * 2.0);
                if close {
                    return false;
                }
            }
            return true;
        }

        // After a heading, start new block
        if prev_line.is_heading {
            return true;
        }

        // Large spacing indicates new paragraph
        let spacing = (prev_line.y - curr_line.y).abs();
        if spacing > avg_spacing * 1.5 {
            return true;
        }

        // Significant font size change (only break on >=2pt difference —
        // smaller changes are common in superscripts / mixed-font Korean
        // text and shouldn't fragment paragraphs).
        if (prev_line.font_size - curr_line.font_size).abs() >= 2.0 {
            return true;
        }

        // Significant left margin change (indentation) — raised from 20pt
        // to 40pt so minor indent variation within a Hancom bullet list
        // doesn't start a new block per line.
        if (prev_line.x - curr_line.x).abs() > 40.0 {
            return true;
        }

        false
    }
}

/// Filter out header/footer text spans (page numbers, running headers).
///
/// Removes spans in the top/bottom margin that contain only numbers or short
/// page-number patterns (e.g. "- 3 -", "Page 5", "2 / 10").
fn filter_header_footer_spans(spans: &mut Vec<TextSpan>, page_height: f32) {
    if spans.is_empty() || page_height <= 0.0 {
        return;
    }

    // Define margin regions: top/bottom 5% of page height.
    // PDF Y axis is bottom-up: Y=0 is at the bottom of the page.
    let margin = page_height * 0.05;
    let top_threshold = page_height - margin; // Near the top edge
    let bottom_threshold = margin; // Near the bottom edge

    spans.retain(|span| {
        let in_header = span.y >= top_threshold;
        let in_footer = span.y <= bottom_threshold;

        if !in_header && !in_footer {
            return true; // Keep spans that are not in the margins
        }

        let text = span.text.trim();
        if text.is_empty() {
            return false; // Remove empty spans in margins
        }

        // Keep the span unless it looks like a bare page number
        let is_page_num = text.chars().all(|c| c.is_ascii_digit()) || is_page_number_pattern(text);

        !is_page_num
    });
}

/// Return `true` if `text` matches a common page-number decoration pattern.
///
/// Recognised patterns:
/// - `"- N -"` / `"– N –"` / `"— N —"` (hyphen/dash-surrounded numbers)
/// - `"Page N"` / `"page N"`
/// - `"N / M"` or `"N of M"` (fraction-style)
fn is_page_number_pattern(text: &str) -> bool {
    let text = text.trim();

    // Pattern: "- N -" or "– N –" or "— N —"
    for dash in &['-', '', ''] {
        let dash_str = dash.to_string();
        if let Some(inner) = text.strip_prefix(dash_str.as_str()) {
            if let Some(inner) = inner.trim().strip_suffix(dash_str.as_str()) {
                if inner.trim().chars().all(|c| c.is_ascii_digit()) {
                    return true;
                }
            }
        }
    }

    // Pattern: "Page N" or "page N"
    if let Some(rest) = text
        .strip_prefix("Page ")
        .or_else(|| text.strip_prefix("page "))
    {
        if rest.trim().chars().all(|c| c.is_ascii_digit()) {
            return true;
        }
    }

    // Pattern: "N / M" or "N of M"
    // Split on whitespace and '/', keep non-empty tokens
    let tokens: Vec<&str> = text
        .split(|c: char| c == '/' || c.is_ascii_whitespace())
        .filter(|s| !s.is_empty())
        .collect();
    if tokens.len() == 3
        && tokens[0].chars().all(|c| c.is_ascii_digit())
        && (tokens[1] == "of" || tokens[1] == "/")
        && tokens[2].chars().all(|c| c.is_ascii_digit())
    {
        return true;
    }
    // "N / M" where slash is surrounded by spaces → tokens = ["N", "M"] after filtering
    if tokens.len() == 2
        && tokens[0].chars().all(|c| c.is_ascii_digit())
        && tokens[1].chars().all(|c| c.is_ascii_digit())
        && text.contains('/')
    {
        return true;
    }

    false
}

/// Font information.
#[derive(Debug, Clone)]
struct FontInfo {
    name: String,
}

/// Text matrix for tracking position in content stream.
#[derive(Debug, Clone)]
struct TextMatrix {
    a: f32,
    b: f32,
    c: f32,
    d: f32,
    e: f32, // X translation
    f: f32, // Y translation
    line_y: f32,
}

impl Default for TextMatrix {
    fn default() -> Self {
        Self {
            a: 1.0,
            b: 0.0,
            c: 0.0,
            d: 1.0,
            e: 0.0,
            f: 0.0,
            line_y: 0.0,
        }
    }
}

impl TextMatrix {
    fn set(&mut self, a: f32, b: f32, c: f32, d: f32, e: f32, f: f32) {
        self.a = a;
        self.b = b;
        self.c = c;
        self.d = d;
        self.e = e;
        self.f = f;
        self.line_y = f;
    }

    fn translate(&mut self, tx: f32, ty: f32) {
        self.e += tx * self.a + ty * self.c;
        self.f += tx * self.b + ty * self.d;
        if ty != 0.0 {
            self.line_y = self.f;
        }
    }

    fn next_line(&mut self) {
        // Default line leading (could be set by TL operator)
        self.f -= 12.0 * self.d;
        self.line_y = self.f;
    }

    fn get_position(&self) -> (f32, f32) {
        (self.e, self.f)
    }

    fn get_scale(&self) -> f32 {
        // Return the vertical scale factor
        (self.a * self.a + self.c * self.c).sqrt()
    }
}

/// Insert a space into `text` if it doesn't already end with one and the
/// last character is not from a spaceless script (CJK/Japanese).
/// Insert a space in TJ array based on kerning adjustment, with script-aware thresholds.
///
/// TJ adjustments are in 1/1000 text space units. The threshold for inserting a space
/// varies by script:
/// - Latin: 200 units (~33% of typical char width ~600)
/// - Hangul (Korean): 500 units (~50% of typical char width 1000)
///   Korean uses word spaces, but kerning between syllables is typically 100-300 units.
/// - CJK (Chinese/Japanese): never insert spaces (handled by is_spaceless_script_char)
fn maybe_insert_space_tj(text: &mut String, adjustment: f32) {
    if text.is_empty() || text.ends_with(' ') || text.ends_with('\u{00A0}') {
        return;
    }

    if let Some(last_char) = text.chars().last() {
        if is_spaceless_script_char(last_char) {
            return;
        }

        let threshold = if is_hangul_char(last_char) {
            500.0
        } else {
            200.0
        };
        if adjustment > threshold {
            text.push(' ');
        }
    }
}

/// Check if a character is a Hangul (Korean) syllable or jamo.
fn is_hangul_char(c: char) -> bool {
    let code = c as u32;
    // Hangul Syllables
    (0xAC00..=0xD7AF).contains(&code)
    // Hangul Jamo
    || (0x1100..=0x11FF).contains(&code)
    // Hangul Compatibility Jamo
    || (0x3130..=0x318F).contains(&code)
    // Hangul Jamo Extended-A/B
    || (0xA960..=0xA97F).contains(&code)
    || (0xD7B0..=0xD7FF).contains(&code)
}

/// Check if a character is a CJK (Chinese/Japanese/Korean) character.
///
/// CJK characters typically don't need spaces between them.
/// Compute the median font size from a slice of spans.
fn median_font_size(spans: &[TextSpan]) -> f32 {
    if spans.is_empty() {
        return 12.0;
    }
    let mut sizes: Vec<f32> = spans.iter().map(|s| s.font_size).collect();
    sizes.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
    sizes[sizes.len() / 2]
}

/// Check if character is from a script that doesn't use word spaces.
/// Chinese and Japanese don't use spaces between words, but Korean does.
fn is_spaceless_script_char(c: char) -> bool {
    let code = c as u32;

    // CJK Unified Ideographs (Chinese characters, used in Chinese/Japanese)
    (0x4E00..=0x9FFF).contains(&code)
    // CJK Unified Ideographs Extension A
    || (0x3400..=0x4DBF).contains(&code)
    // CJK Unified Ideographs Extension B-F
    || (0x20000..=0x2A6DF).contains(&code)
    || (0x2A700..=0x2B73F).contains(&code)
    || (0x2B740..=0x2B81F).contains(&code)
    || (0x2B820..=0x2CEAF).contains(&code)
    || (0x2CEB0..=0x2EBEF).contains(&code)
    // Hiragana (Japanese)
    || (0x3040..=0x309F).contains(&code)
    // Katakana (Japanese)
    || (0x30A0..=0x30FF).contains(&code)
    // NOTE: Hangul (Korean) is NOT included - Korean uses word spaces like English
    // CJK Symbols and Punctuation
    || (0x3000..=0x303F).contains(&code)
}

/// Merge adjacent fragmented spans that likely form words.
///
/// Some PDFs render text character-by-character with separate Tj operations,
/// creating many single-character spans with width=0. This function merges
/// consecutive short spans (≤3 chars) with width=0 that are:
/// - On the same baseline (same Y within tolerance)
/// - Using the same font at the same size
/// - Positioned sequentially without large gaps
///
/// Only merges into width=0 spans — when the previous span also has width=0 or
/// is itself a recently-merged fragment. This prevents merging normal
/// multi-character spans that happen to be adjacent.
fn merge_fragmented_spans(spans: Vec<TextSpan>) -> Vec<TextSpan> {
    if spans.len() < 2 {
        return spans;
    }

    let mut result: Vec<TextSpan> = Vec::with_capacity(spans.len());
    // Track which result spans were created by merging (started as width=0)
    let mut was_fragment: Vec<bool> = Vec::with_capacity(spans.len());

    for span in spans {
        let is_fragment = span.text.chars().count() <= 3 && span.width <= 0.0;

        let should_merge =
            if let Some((prev, prev_was_frag)) = result.last().zip(was_fragment.last()) {
                // Only merge if BOTH are fragments (or prev was already merged from fragments)
                if !is_fragment || !prev_was_frag {
                    false
                } else {
                    // Same baseline (Y within tolerance)
                    let y_tolerance = span.font_size * 0.3;
                    let same_y = (prev.y - span.y).abs() <= y_tolerance;

                    // Same font and size
                    let same_font = prev.font_name == span.font_name
                        && (prev.font_size - span.font_size).abs() < 0.1;

                    if !same_y || !same_font {
                        false
                    } else {
                        // Estimate character width from font size
                        let est_char_width = prev.font_size * 0.6;

                        // Estimate where the previous span ends
                        let prev_end = if prev.width > 0.0 {
                            prev.x + prev.width
                        } else {
                            prev.x + est_char_width * prev.text.chars().count() as f32
                        };

                        let gap = span.x - prev_end;

                        // Merge if gap is small enough to not be a word space.
                        // Character-to-character gap within a word: 0 to ~0.3 * char_width
                        // Word space gap: ~0.4 * char_width or more
                        gap < est_char_width * 0.4 && gap > -est_char_width * 0.5
                    }
                }
            } else {
                false
            };

        if should_merge {
            let prev = result.last_mut().unwrap();
            // Update width to cover the merged extent
            let new_end = span.x + span.font_size * 0.6 * span.text.chars().count() as f32;
            prev.width = new_end - prev.x;
            prev.text.push_str(&span.text);
        } else {
            was_fragment.push(is_fragment);
            result.push(span);
        }
    }

    result
}

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

    #[test]
    fn test_font_statistics() {
        let mut stats = FontStatistics::default();
        // Simulate body text (most common)
        for _ in 0..100 {
            stats.add_size(12.0);
        }
        // Simulate headings
        for _ in 0..5 {
            stats.add_size(18.0);
        }
        for _ in 0..3 {
            stats.add_size(24.0);
        }

        stats.analyze();

        assert!((stats.body_size - 12.0).abs() < 0.1);
        assert_eq!(stats.get_heading_level(12.0, false), 0);
        assert!(stats.get_heading_level(18.0, false) > 0);
        assert!(stats.get_heading_level(24.0, false) > 0);
    }

    #[test]
    fn test_text_span_bold_detection() {
        let span = TextSpan::new(
            "Test".to_string(),
            0.0,
            0.0,
            12.0,
            "Helvetica-Bold".to_string(),
        );
        assert!(span.is_bold);
        assert!(!span.is_italic);

        let span2 = TextSpan::new(
            "Test".to_string(),
            0.0,
            0.0,
            12.0,
            "Helvetica-Oblique".to_string(),
        );
        assert!(!span2.is_bold);
        assert!(span2.is_italic);
    }

    #[test]
    fn test_merge_fragmented_spans_single_chars() {
        // Simulate per-character rendering: "Hello" as 5 separate spans
        let spans: Vec<TextSpan> = "Hello"
            .chars()
            .enumerate()
            .map(|(i, c)| TextSpan {
                text: c.to_string(),
                x: 100.0 + i as f32 * 6.0,
                y: 500.0,
                width: 0.0, // width=0 is the fragmentation signal
                font_size: 12.0,
                font_name: "Helvetica".to_string(),
                is_bold: false,
                is_italic: false,
            })
            .collect();

        let merged = merge_fragmented_spans(spans);
        assert_eq!(merged.len(), 1);
        assert_eq!(merged[0].text, "Hello");
    }

    #[test]
    fn test_merge_fragmented_spans_preserves_normal() {
        // Normal multi-character spans should not be merged unnecessarily
        let spans = vec![
            TextSpan {
                text: "Hello".to_string(),
                x: 100.0,
                y: 500.0,
                width: 30.0,
                font_size: 12.0,
                font_name: "Helvetica".to_string(),
                is_bold: false,
                is_italic: false,
            },
            TextSpan {
                text: "World".to_string(),
                x: 145.0, // gap indicates word space
                y: 500.0,
                width: 30.0,
                font_size: 12.0,
                font_name: "Helvetica".to_string(),
                is_bold: false,
                is_italic: false,
            },
        ];

        let merged = merge_fragmented_spans(spans);
        // Should not merge because fragmentation threshold is not met
        assert_eq!(merged.len(), 2);
    }

    #[test]
    fn test_column_contains() {
        let col = Column {
            left: 100.0,
            right: 200.0,
            index: 0,
        };
        assert!(col.contains(100.0));
        assert!(col.contains(150.0));
        assert!(col.contains(200.0));
        assert!(!col.contains(99.0));
        assert!(!col.contains(201.0));
    }

    #[test]
    fn test_column_contains_span() {
        let col = Column {
            left: 100.0,
            right: 200.0,
            index: 0,
        };

        // Span fully inside column
        let span1 = TextSpan::new(
            "Test".to_string(),
            120.0,
            0.0,
            12.0,
            "Helvetica".to_string(),
        );
        let span1 = TextSpan {
            width: 50.0,
            ..span1
        };
        assert!(col.contains_span(&span1));

        // Span center inside column
        let span2 = TextSpan::new("Test".to_string(), 90.0, 0.0, 12.0, "Helvetica".to_string());
        let span2 = TextSpan {
            width: 40.0,
            ..span2
        }; // center at 110
        assert!(col.contains_span(&span2));

        // Span completely outside
        let span3 = TextSpan::new(
            "Test".to_string(),
            250.0,
            0.0,
            12.0,
            "Helvetica".to_string(),
        );
        let span3 = TextSpan {
            width: 30.0,
            ..span3
        };
        assert!(!col.contains_span(&span3));
    }
}