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
//! XY-Cut recursive spatial partitioning for multi-column text layout.
//!
//! This module implements the XY-Cut algorithm per PDF Spec Section 9.4 for
//! recursive geometric analysis without semantic heuristics. Uses projection
//! profiles to detect column boundaries in complex layouts.
//!
//! Per ISO 32000-1:2008:
//! - Section 9.4: Text Objects and coordinates
//! - Section 14.7: Logical Structure (prefers structure tree when available)
//!
//! # Algorithm Overview
//!
//! 1. Compute horizontal projection (white space density across X)
//! 2. Find valleys (gaps) where density < threshold
//! 3. Split region at widest valley (vertical line)
//! 4. Recursively partition left and right sub-regions
//! 5. Alternate to vertical projection if no horizontal valleys found
//! 6. Base case: Sort spans top-to-bottom, left-to-right
//!
//! # Performance
//!
//! Typical newspaper page: ~100 spans, < 5ms processing time
//! Recursive depth: O(log n) for balanced columns
use super::{ReadingOrderContext, ReadingOrderStrategy};
use crate::error::Result;
use crate::layout::TextSpan;
use crate::pipeline::{OrderedTextSpan, ReadingOrderInfo};
/// Maximum density-array length for XY-cut projection profiles.
///
/// A normal PDF page is at most a few thousand points wide/tall. This limit of
/// 100 000 bins is generous (≈ 33× a 3000-point A0 page) while being small
/// enough to never cause an allocation problem. Spans whose bounding-box span
/// exceeds this limit are the result of a degenerate CTM; returning `None` from
/// the projection safely skips the split instead of attempting a multi-terabyte
/// allocation that would abort the process via `handle_alloc_error`.
const MAX_PROJECTION_SIZE: usize = 100_000;
/// XY-Cut recursive spatial partitioning strategy.
///
/// Detects columns using projection profiles and white space analysis.
/// Suitable for newspapers, academic papers, and multi-column layouts.
pub struct XYCutStrategy {
/// Minimum number of spans in a region before attempting split (default: 5).
/// Prevents excessive recursion on small regions.
pub min_spans_for_split: usize,
/// Valley threshold as fraction of peak projection density (default: 0.3).
/// Lower values detect narrower gutters, higher values only detect wide gaps.
pub valley_threshold: f32,
/// Minimum valley width in points (default: 15.0).
/// Prevents detecting single-character gaps as column boundaries.
pub min_valley_width: f32,
/// Enable horizontal partitioning first, fallback to vertical (default: true).
pub prefer_horizontal: bool,
}
impl Default for XYCutStrategy {
fn default() -> Self {
Self {
min_spans_for_split: 5,
valley_threshold: 0.3,
min_valley_width: 15.0,
prefer_horizontal: true,
}
}
}
impl XYCutStrategy {
/// Create a new XY-Cut strategy with default parameters.
pub fn new() -> Self {
Self::default()
}
/// Create with custom valley threshold (0.0-1.0).
pub fn with_valley_threshold(mut self, threshold: f32) -> Self {
self.valley_threshold = threshold.clamp(0.0, 1.0);
self
}
/// Create with custom minimum valley width.
pub fn with_min_valley_width(mut self, width: f32) -> Self {
self.min_valley_width = width.max(1.0);
self
}
/// Core recursive partitioning algorithm.
///
/// Public for use by MarkdownConverter's ColumnAware reading order mode.
pub fn partition_region(&self, spans: &[TextSpan]) -> Vec<Vec<TextSpan>> {
let indices: Vec<usize> = (0..spans.len()).collect();
let index_groups = self.partition_indexed(spans, &indices);
// Clone spans only once at the end (not at every recursion level)
index_groups
.into_iter()
.map(|group| group.into_iter().map(|i| spans[i].clone()).collect())
.collect()
}
/// Index-based recursive partitioning — returns groups of indices into the input span slice.
///
/// Avoids cloning TextSpan at every recursive split level. Spans are only
/// read through shared reference; indices are partitioned instead.
fn partition_indexed(&self, all_spans: &[TextSpan], indices: &[usize]) -> Vec<Vec<usize>> {
if indices.is_empty() {
return Vec::new();
}
// Base case: small region, don't split further
if indices.len() < self.min_spans_for_split {
return vec![self.sort_indices(all_spans, indices)];
}
// Detect single-column body text up-front and skip all spatial
// splits. Real body text has density dips (indented code, short
// last-lines, paragraph breaks) that would otherwise trigger
// spurious horizontal (column) or vertical (row) splits,
// scrambling reading order. The subsequent sort-by-Y already
// handles row order within a column.
if self.is_single_column_region(all_spans, indices) {
return vec![self.sort_indices(all_spans, indices)];
}
// Try horizontal partitioning (vertical line split) — this detects
// columns. Per PDF Spec ISO 32000-1:2008 §14.8.4 (Logical Structure
// reading order), column detection is the primary purpose of XY-Cut.
if let Some((left, right)) = self.find_horizontal_split_indexed(all_spans, indices) {
let mut result = self.partition_indexed(all_spans, &left);
result.extend(self.partition_indexed(all_spans, &right));
return result;
}
// Try vertical partitioning (horizontal line split). This handles
// the "header band above a multi-column body" case: the split
// isolates the header row so the body can subsequently be column-
// split. Per PDF coordinate convention (origin at bottom-left, Y
// grows upward), the first tuple element is the upper-Y (top-of-
// page) partition and must be processed first in reading order.
if let Some((above, below)) = self.find_vertical_split_indexed(all_spans, indices) {
let mut result = self.partition_indexed(all_spans, &above);
result.extend(self.partition_indexed(all_spans, &below));
return result;
}
// No split found, return as single group
vec![self.sort_indices(all_spans, indices)]
}
/// Heuristic: does the region look like a single column of body text?
///
/// Called **before** horizontal split attempts. When true, the region
/// is returned as a single sorted group, bypassing both horizontal
/// (column) and vertical (row) splits. This prevents XY-Cut from
/// fragmenting body text at density dips caused by indentation or
/// short last-lines.
///
/// Detection: cluster spans into lines by rounded top-Y, then count
/// lines that are both **wide** (extent ≥ 60% region width) and
/// **dense** (covered ratio ≥ 80%). Body-text lines satisfy both.
/// Aligned multi-column rows look "wide" because their extent spans
/// the gutter, but fail the density check because the gutter is empty.
fn is_single_column_region(&self, all_spans: &[TextSpan], indices: &[usize]) -> bool {
if indices.len() < 3 {
return false;
}
let mut x_min = f32::MAX;
let mut x_max = f32::MIN;
for &i in indices {
x_min = x_min.min(all_spans[i].bbox.left());
x_max = x_max.max(all_spans[i].bbox.right());
}
let region_width = x_max - x_min;
if region_width <= 10.0 {
return true;
}
let mut lines: std::collections::BTreeMap<i32, Vec<(f32, f32)>> =
std::collections::BTreeMap::new();
for &i in indices {
let s = &all_spans[i];
let y_key = s.bbox.top().round() as i32;
lines
.entry(y_key)
.or_default()
.push((s.bbox.left(), s.bbox.right()));
}
if lines.len() < 3 {
return false;
}
// Primary check: majority of lines are wide AND densely covered.
// This catches clean body text where every line covers most of the
// region width with almost no intra-line gaps.
let width_threshold = region_width * 0.6;
let mut wide_dense_lines = 0usize;
for line_spans in lines.values() {
let mut sorted = line_spans.clone();
sorted.sort_by(|a, b| crate::utils::safe_float_cmp(a.0, b.0));
let extent_left = sorted.first().unwrap().0;
let extent_right = sorted.iter().map(|(_, r)| *r).fold(f32::MIN, f32::max);
let extent = extent_right - extent_left;
if extent < width_threshold {
continue;
}
let mut covered = 0.0f32;
let mut last_end = f32::MIN;
for &(l, r) in &sorted {
let start = l.max(last_end);
if r > start {
covered += r - start;
last_end = r;
}
}
if covered >= extent * 0.8 {
wide_dense_lines += 1;
}
}
if wide_dense_lines * 2 >= lines.len() {
return true;
}
// Fallback check: no line has a significant intra-line gap. Catches
// sparse-but-aligned single-column layouts like TOCs, dot-leader
// entries, and justified text where word gaps dominate. Any real
// multi-column layout has an inter-column gutter ≥ `min_valley_width`
// on at least some lines.
let max_gap = self.min_valley_width;
for line_spans in lines.values() {
let mut sorted = line_spans.clone();
sorted.sort_by(|a, b| crate::utils::safe_float_cmp(a.0, b.0));
for w in sorted.windows(2) {
let gap = w[1].0 - w[0].1;
if gap >= max_gap {
return false;
}
}
}
true
}
/// Find vertical line (X-axis) split using index-based partitioning.
///
/// Rejects lopsided splits where one side contains fewer than ~10% of
/// the region's spans — those come from single-column pages where
/// indentation or stray content creates a spurious density dip at
/// one edge of the projection, not from a real column boundary.
fn find_horizontal_split_indexed(
&self,
all_spans: &[TextSpan],
indices: &[usize],
) -> Option<(Vec<usize>, Vec<usize>)> {
let profile = self.horizontal_projection_indexed(all_spans, indices)?;
let (valley_start, valley_end, valley_width) = self.find_valley(&profile)?;
if valley_width < self.min_valley_width {
return None;
}
let split_x = profile.x_min + (valley_start + valley_end) as f32 / 2.0;
let (left, right): (Vec<usize>, Vec<usize>) = indices
.iter()
.partition(|&&i| all_spans[i].bbox.right() <= split_x);
if left.is_empty() || right.is_empty() {
return None;
}
// Real column splits produce balanced partitions. A 95/5 split is
// almost always from edge dips or stray content, not a column.
let min_side = (indices.len() / 10).max(2);
if left.len() < min_side || right.len() < min_side {
return None;
}
Some((left, right))
}
/// Find horizontal line (Y-axis) split using index-based partitioning.
///
/// Returns `(above, below)` where `above` holds spans whose rectangle
/// edge is at larger Y (higher on page in PDF coordinates) and must be
/// processed first in reading order. PDF Spec ISO 32000-1:2008 §8.3.2.3
/// defines the default user-space coordinate system with origin at the
/// lower-left corner and Y increasing upward.
fn find_vertical_split_indexed(
&self,
all_spans: &[TextSpan],
indices: &[usize],
) -> Option<(Vec<usize>, Vec<usize>)> {
let profile = self.vertical_projection_indexed(all_spans, indices)?;
let (valley_start, valley_end, valley_width) = self.find_valley(&profile)?;
if valley_width < self.min_valley_width {
return None;
}
let split_y = profile.y_min + (valley_start + valley_end) as f32 / 2.0;
// `Rect::top()` returns `self.y`, the SMALLER Y coordinate of the
// normalized rectangle — the method name follows a screen-coordinate
// convention (Y grows downward) but PDF user space has Y growing
// upward, so in PDF terms `bbox.top()` is actually the LOWER edge of
// the glyph's bounding box. The predicate `bbox.top() >= split_y`
// therefore classifies a span into `above` only when its *lowest*
// point is already above the split line, i.e. the entire span sits
// above the cut. Since `split_y` is the midpoint of a horizontal
// projection valley (an empty band by construction), spans should
// not straddle it in practice; any that do (e.g. a tall header
// glyph whose ascenders dip into the valley) fall into `below`.
let (above, below): (Vec<usize>, Vec<usize>) = indices
.iter()
.partition(|&&i| all_spans[i].bbox.top() >= split_y);
if above.is_empty() || below.is_empty() {
return None;
}
// Reject lopsided splits (see `find_horizontal_split_indexed`). A
// real row split has content on both sides; otherwise we're chasing
// a stray page header/footer.
let min_side = (indices.len() / 10).max(2);
if above.len() < min_side || below.len() < min_side {
return None;
}
Some((above, below))
}
/// Calculate horizontal projection profile from indexed spans.
fn horizontal_projection_indexed(
&self,
all_spans: &[TextSpan],
indices: &[usize],
) -> Option<ProjectionProfile> {
if indices.is_empty() {
return None;
}
let mut x_min = f32::MAX;
let mut x_max = f32::MIN;
let mut y_min = f32::MAX;
let mut y_max = f32::MIN;
for &i in indices {
let span = &all_spans[i];
x_min = x_min.min(span.bbox.left());
x_max = x_max.max(span.bbox.right());
y_min = y_min.min(span.bbox.top());
y_max = y_max.max(span.bbox.bottom());
}
let width = (x_max - x_min).ceil() as usize;
if width > MAX_PROJECTION_SIZE {
log::warn!(
"XY-cut: horizontal projection width {} exceeds MAX_PROJECTION_SIZE {}, skipping region (degenerate CTM?)",
width,
MAX_PROJECTION_SIZE
);
return None;
}
let mut density = vec![0.0; width];
for &i in indices {
let span = &all_spans[i];
let x_start = (span.bbox.left() - x_min).max(0.0).ceil() as usize;
let x_end = (span.bbox.right() - x_min).ceil() as usize;
let height = span.bbox.bottom() - span.bbox.top();
for j in x_start..x_end.min(width) {
density[j] += height;
}
}
Some(ProjectionProfile {
density,
x_min,
y_min,
})
}
/// Calculate vertical projection profile from indexed spans.
fn vertical_projection_indexed(
&self,
all_spans: &[TextSpan],
indices: &[usize],
) -> Option<ProjectionProfile> {
if indices.is_empty() {
return None;
}
let mut x_min = f32::MAX;
let mut x_max = f32::MIN;
let mut y_min = f32::MAX;
let mut y_max = f32::MIN;
for &i in indices {
let span = &all_spans[i];
x_min = x_min.min(span.bbox.left());
x_max = x_max.max(span.bbox.right());
y_min = y_min.min(span.bbox.top());
y_max = y_max.max(span.bbox.bottom());
}
let height = (y_max - y_min).ceil() as usize;
if height > MAX_PROJECTION_SIZE {
log::warn!(
"XY-cut: vertical projection height {} exceeds MAX_PROJECTION_SIZE {}, skipping region (degenerate CTM?)",
height,
MAX_PROJECTION_SIZE
);
return None;
}
let mut density = vec![0.0; height];
for &i in indices {
let span = &all_spans[i];
let y_start = (span.bbox.top() - y_min).max(0.0).ceil() as usize;
let y_end = (span.bbox.bottom() - y_min).ceil() as usize;
let w = span.bbox.right() - span.bbox.left();
for j in y_start..y_end.min(height) {
density[j] += w;
}
}
Some(ProjectionProfile {
density,
x_min,
y_min,
})
}
/// Find the widest valley (white space gap) in projection profile.
fn find_valley(&self, profile: &ProjectionProfile) -> Option<(usize, usize, f32)> {
if profile.density.is_empty() {
return None;
}
// Find peak density
let peak = profile.density.iter().copied().fold(0.0, f32::max);
if peak == 0.0 {
return None;
}
// Find valleys (regions below threshold)
let threshold = peak * self.valley_threshold;
let mut valleys = Vec::new();
let mut in_valley = false;
let mut valley_start = 0;
for (i, &density) in profile.density.iter().enumerate() {
if density < threshold {
if !in_valley {
valley_start = i;
in_valley = true;
}
} else if in_valley {
valleys.push((valley_start, i));
in_valley = false;
}
}
if in_valley {
valleys.push((valley_start, profile.density.len()));
}
// Return widest valley
valleys
.into_iter()
.map(|(start, end)| (start, end, (end - start) as f32))
.max_by(|a, b| crate::utils::safe_float_cmp(a.2, b.2))
}
/// Test-only wrapper for horizontal projection on a contiguous slice.
#[cfg(test)]
fn horizontal_projection(&self, spans: &[TextSpan]) -> Option<ProjectionProfile> {
let indices: Vec<usize> = (0..spans.len()).collect();
self.horizontal_projection_indexed(spans, &indices)
}
/// Test-only wrapper for vertical projection on a contiguous slice.
#[cfg(test)]
fn vertical_projection(&self, spans: &[TextSpan]) -> Option<ProjectionProfile> {
let indices: Vec<usize> = (0..spans.len()).collect();
self.vertical_projection_indexed(spans, &indices)
}
/// Sort spans in reading order (top-to-bottom, left-to-right).
#[cfg(test)]
fn sort_spans<'a>(&self, spans: &'a [TextSpan]) -> Vec<&'a TextSpan> {
let mut sorted: Vec<_> = spans.iter().collect();
sorted.sort_by(|a, b| {
// Sort by Y (top) first, descending (top of page first)
let y_cmp = crate::utils::safe_float_cmp(b.bbox.top(), a.bbox.top());
if y_cmp != std::cmp::Ordering::Equal {
return y_cmp;
}
// Same Y level, sort by X (left) ascending
crate::utils::safe_float_cmp(a.bbox.left(), b.bbox.left())
});
sorted
}
/// Sort indices in reading order (top-to-bottom, left-to-right).
fn sort_indices(&self, all_spans: &[TextSpan], indices: &[usize]) -> Vec<usize> {
let mut sorted: Vec<usize> = indices.to_vec();
sorted.sort_by(|&a, &b| {
let y_cmp =
crate::utils::safe_float_cmp(all_spans[b].bbox.top(), all_spans[a].bbox.top());
if y_cmp != std::cmp::Ordering::Equal {
return y_cmp;
}
crate::utils::safe_float_cmp(all_spans[a].bbox.left(), all_spans[b].bbox.left())
});
sorted
}
}
/// Internal projection profile representation.
struct ProjectionProfile {
/// Density values (height or width accumulated per bin)
density: Vec<f32>,
/// Origin coordinates
x_min: f32,
y_min: f32,
}
impl ReadingOrderStrategy for XYCutStrategy {
fn apply(
&self,
spans: Vec<TextSpan>,
_context: &ReadingOrderContext,
) -> Result<Vec<OrderedTextSpan>> {
// Use index-based partitioning to avoid cloning during recursion
let indices: Vec<usize> = (0..spans.len()).collect();
let index_groups = self.partition_indexed(&spans, &indices);
// Build result — moves spans out by index (no extra clone)
let mut ordered = Vec::with_capacity(spans.len());
// Convert spans to indexable storage for O(1) moves
let mut span_slots: Vec<Option<TextSpan>> = spans.into_iter().map(Some).collect();
let mut order_index = 0usize;
for (group_idx, group) in index_groups.iter().enumerate() {
for &i in group {
if let Some(span) = span_slots[i].take() {
ordered.push(
OrderedTextSpan::with_info(span, order_index, ReadingOrderInfo::xycut())
.with_group(group_idx),
);
order_index += 1;
}
}
}
Ok(ordered)
}
fn name(&self) -> &'static str {
"XYCutStrategy"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::geometry::Rect;
fn make_span(x: f32, y: f32, width: f32, height: f32) -> TextSpan {
use crate::layout::{Color, FontWeight};
TextSpan {
artifact_type: None,
text: "test".to_string(),
bbox: Rect::new(x, y, width, height),
font_size: 12.0,
font_name: "Arial".to_string(),
font_weight: FontWeight::Normal,
is_italic: false,
is_monospace: false,
color: Color {
r: 0.0,
g: 0.0,
b: 0.0,
},
mcid: None,
sequence: 0,
split_boundary_before: false,
offset_semantic: false,
char_spacing: 0.0,
word_spacing: 0.0,
horizontal_scaling: 100.0,
primary_detected: false,
char_widths: vec![],
}
}
#[test]
fn test_single_column_no_split() {
let strategy = XYCutStrategy::new();
let spans = vec![
make_span(10.0, 100.0, 50.0, 10.0), // Line 1
make_span(10.0, 85.0, 50.0, 10.0), // Line 2
make_span(10.0, 70.0, 50.0, 10.0), // Line 3
];
let groups = strategy.partition_region(&spans);
assert_eq!(groups.len(), 1); // No split for single column
assert_eq!(groups[0].len(), 3);
}
/// Realistic A4/Letter single-column page: 60 lines of body text,
/// 14pt leading, one paragraph gap (30pt) mid-page. Only one body
/// column exists, so XY-Cut must return exactly one group and
/// preserve top-to-bottom reading order. A density-dip split at the
/// paragraph gap would fragment the page and non-monotonically
/// interleave paragraph contents.
#[test]
fn test_single_column_body_text_no_fragmentation() {
let strategy = XYCutStrategy::new();
// Simulate 60 lines of body text at x=72..540 (letter page, 1" margins).
// Each line is a single span; line height 12pt, leading 14pt.
let mut spans = Vec::new();
let line_height = 12.0;
let leading = 14.0;
let left = 72.0;
let right = 540.0;
let width = right - left;
let mut y = 720.0; // start near top of letter page
for i in 0..60 {
// Insert a paragraph gap in the middle (30pt, larger than min_valley_width=15pt)
if i == 30 {
y -= 30.0;
}
// Lines are ~full-width body text
spans.push(make_span(left, y, width, line_height));
y -= leading;
}
let groups = strategy.partition_region(&spans);
assert_eq!(
groups.len(),
1,
"single-column body text must not be split by XY-Cut (got {} groups)",
groups.len()
);
assert_eq!(groups[0].len(), 60, "all 60 spans must be preserved");
// Verify the group preserves monotonic top-to-bottom reading order
// (each subsequent span's Y should be <= previous Y).
let mut last_y = f32::MAX;
for s in &groups[0] {
assert!(
s.bbox.top() <= last_y + 0.01,
"reading order must be top-to-bottom: {} > {}",
s.bbox.top(),
last_y
);
last_y = s.bbox.top();
}
}
/// After a vertical (row) split, the partition at higher Y (top of
/// page in PDF coords) must be processed first in reading order so
/// that header content appears before body content.
#[test]
fn test_vertical_split_preserves_top_to_bottom_order() {
use crate::pipeline::reading_order::{ReadingOrderContext, ReadingOrderStrategy};
let mut strategy = XYCutStrategy::new();
strategy.min_spans_for_split = 2;
// Header line at high Y (top of page in PDF coords).
// Body block at lower Y values. Gap between them > min_valley_width.
let make = |text: &str, x: f32, y: f32, w: f32| {
let mut s = make_span(x, y, w, 12.0);
s.text = text.to_string();
s
};
// Two columns at y ∈ {200, 180, 160} (body), header at y=400.
// Horizontal split will find the column gutter first; within each
// column the header must still come out first in reading order.
let spans = vec![
make("HEADER LEFT", 50.0, 400.0, 200.0),
make("HEADER RIGHT", 300.0, 400.0, 200.0),
make("body-L1", 50.0, 200.0, 150.0),
make("body-R1", 300.0, 200.0, 150.0),
make("body-L2", 50.0, 180.0, 150.0),
make("body-R2", 300.0, 180.0, 150.0),
];
let context = ReadingOrderContext::new();
let ordered = strategy.apply(spans, &context).unwrap();
let texts: Vec<&str> = ordered.iter().map(|o| o.span.text.as_str()).collect();
// First output must be from y=400 (header), not y=180 (body bottom).
assert!(texts[0].contains("HEADER"), "expected HEADER first, got sequence {:?}", texts);
}
/// Single-column page with a tall header band ("Title" or "Chapter
/// heading") at the top. XY-Cut may validly split the header from
/// the body (vertical Y-split) but must not further split the body
/// into per-paragraph chunks.
#[test]
fn test_single_column_with_header_at_most_two_groups() {
let strategy = XYCutStrategy::new();
let mut spans = Vec::new();
// Tall header band
spans.push(make_span(72.0, 750.0, 468.0, 24.0));
// 40 lines of body text below, separated by a ~50pt gap
let mut y = 670.0;
for _ in 0..40 {
spans.push(make_span(72.0, y, 468.0, 12.0));
y -= 14.0;
}
let groups = strategy.partition_region(&spans);
assert!(
groups.len() <= 2,
"single-column with header should produce at most 2 groups, got {}",
groups.len()
);
let total: usize = groups.iter().map(|g| g.len()).sum();
assert_eq!(total, 41);
}
#[test]
fn test_two_column_split() {
let mut strategy = XYCutStrategy::new();
strategy.min_spans_for_split = 2; // Lower threshold for testing
let spans = vec![
// Left column (x: 10-60)
make_span(10.0, 100.0, 50.0, 10.0),
make_span(10.0, 85.0, 50.0, 10.0),
// Right column (x: 100-150) - wide gap of 40 points
make_span(100.0, 100.0, 50.0, 10.0),
make_span(100.0, 85.0, 50.0, 10.0),
];
let groups = strategy.partition_region(&spans);
// With wide gap and lower threshold, should split into 2 columns or keep as 1 group
assert!(!groups.is_empty(), "Expected at least 1 group");
// Verify all spans are preserved
let total_spans: usize = groups.iter().map(|g| g.len()).sum();
assert_eq!(total_spans, 4, "Expected all 4 spans to be preserved");
}
#[test]
fn test_three_column_layout() {
let strategy = XYCutStrategy::new();
let spans = vec![
// Column 1 (x: 10-40)
make_span(10.0, 100.0, 30.0, 10.0),
make_span(10.0, 85.0, 30.0, 10.0),
// Column 2 (x: 70-100)
make_span(70.0, 100.0, 30.0, 10.0),
make_span(70.0, 85.0, 30.0, 10.0),
// Column 3 (x: 130-160)
make_span(130.0, 100.0, 30.0, 10.0),
make_span(130.0, 85.0, 30.0, 10.0),
];
let groups = strategy.partition_region(&spans);
// Should recursively split into at least 2 groups
assert!(groups.len() >= 2, "Expected at least 2 groups, got {}", groups.len());
}
#[test]
fn test_small_region_no_split() {
let strategy = XYCutStrategy::new();
let spans = vec![make_span(10.0, 100.0, 50.0, 10.0)];
let groups = strategy.partition_region(&spans);
assert_eq!(groups.len(), 1); // Single span region
assert_eq!(groups[0].len(), 1);
}
#[test]
fn test_sort_order() {
let strategy = XYCutStrategy::new();
let spans = vec![
make_span(100.0, 70.0, 50.0, 10.0), // Lower right
make_span(10.0, 100.0, 50.0, 10.0), // Upper left
make_span(100.0, 100.0, 50.0, 10.0), // Upper right
make_span(10.0, 70.0, 50.0, 10.0), // Lower left
];
let sorted = strategy.sort_spans(&spans);
// Expect: upper left, upper right, lower left, lower right
assert_eq!(sorted[0].bbox.top(), 100.0); // Upper
assert_eq!(sorted[0].bbox.left(), 10.0); // Left
assert_eq!(sorted[1].bbox.top(), 100.0); // Upper
assert_eq!(sorted[1].bbox.left(), 100.0); // Right
}
#[test]
fn test_horizontal_projection() {
let strategy = XYCutStrategy::new();
let spans = vec![
make_span(10.0, 100.0, 30.0, 10.0), // x: 10-40
make_span(100.0, 100.0, 30.0, 10.0), // x: 100-130
];
if let Some(profile) = strategy.horizontal_projection(&spans) {
// Should have density peaks around x=25 and x=115
assert!(!profile.density.is_empty());
assert!(profile.density.len() >= 120); // Total width from 10 to 130 = 120
// Gap is between local x=30 and x=90 (relative to x_min=10)
// So in density array indices [30..90]
let gap_start = 30;
let gap_end = 90;
if gap_end <= profile.density.len() {
let gap_region = &profile.density[gap_start..gap_end];
let gap_density: f32 = gap_region.iter().sum();
assert!(gap_density < 1.0); // Gap should be mostly empty
}
}
}
#[test]
fn test_vertical_projection() {
let strategy = XYCutStrategy::new();
let spans = vec![
make_span(10.0, 100.0, 50.0, 20.0), // y: 100-120
make_span(10.0, 50.0, 50.0, 20.0), // y: 50-70
];
if let Some(profile) = strategy.vertical_projection(&spans) {
// Should have density peaks around y=110 and y=60
assert!(!profile.density.is_empty());
// Large gap between 70 and 100
assert!(profile.density.len() > 50);
}
}
#[test]
fn test_narrow_gap_rejected() {
let strategy = XYCutStrategy::new();
let spans = vec![
make_span(10.0, 100.0, 30.0, 10.0), // x: 10-40
make_span(45.0, 100.0, 30.0, 10.0), // x: 45-75, gap: 5 points
];
let groups = strategy.partition_region(&spans);
// Gap is too narrow (< 15 points), should not split
assert_eq!(groups.len(), 1);
}
/// Regression test for Bug 2: degenerate CTM places spans at ~100 trillion PDF points.
/// horizontal_projection_indexed must return None instead of attempting a
/// ~100-trillion-element vec allocation (which triggers handle_alloc_error → abort).
#[test]
fn test_degenerate_ctm_horizontal_projection_returns_none() {
let strategy = XYCutStrategy::new();
// Observed crash coordinate: 99_992_777_785_344 PDF points on a ~3968-point page.
let degenerate_x: f32 = 99_992_777_785_344.0;
let spans = vec![
make_span(10.0, 100.0, 30.0, 10.0),
make_span(degenerate_x, 100.0, 30.0, 10.0),
];
// Must not panic or abort — projection should return None for oversized region.
let result = strategy.horizontal_projection(&spans);
assert!(
result.is_none(),
"expected None for projection spanning ~100 trillion points, got Some"
);
}
/// Vertical projection must also return None for degenerate CTM y-coordinates.
#[test]
fn test_degenerate_ctm_vertical_projection_returns_none() {
let strategy = XYCutStrategy::new();
let degenerate_y: f32 = 99_992_777_785_344.0;
let spans = vec![
make_span(10.0, 100.0, 30.0, 10.0),
make_span(10.0, degenerate_y, 30.0, 10.0),
];
let result = strategy.vertical_projection(&spans);
assert!(
result.is_none(),
"expected None for projection spanning ~100 trillion points, got Some"
);
}
/// XYCut must assign distinct group_id values to spans in different
/// spatial partitions so that converters can keep each column's content
/// contiguous instead of interleaving by Y-coordinate.
#[test]
fn test_xycut_group_id_two_column_layout() {
use crate::pipeline::reading_order::{ReadingOrderContext, ReadingOrderStrategy};
let mut strategy = XYCutStrategy::new();
strategy.min_spans_for_split = 2; // lower threshold for small test
// Left column (x=50-200) Right column (x=400-550)
// "Description" y=100 "Amount" y=100
// "Widget A" y=120 "$150.00" y=120
// "Widget B" y=140 "Discount" y=140
// "$25.00" y=160
let make = |text: &str, x: f32, y: f32, w: f32| {
let mut s = make_span(x, y, w, 12.0);
s.text = text.to_string();
s
};
let spans = vec![
make("Description", 50.0, 100.0, 150.0),
make("Amount", 400.0, 100.0, 150.0),
make("Widget A", 50.0, 120.0, 150.0),
make("$150.00", 400.0, 120.0, 150.0),
make("Widget B", 50.0, 140.0, 150.0),
make("Discount", 400.0, 140.0, 150.0),
make("$25.00", 400.0, 160.0, 150.0),
];
let context = ReadingOrderContext::new();
let ordered = strategy.apply(spans, &context).unwrap();
// Every span must have a group_id assigned.
assert!(
ordered.iter().all(|s| s.group_id.is_some()),
"all spans should have group_id set by XYCut"
);
// Left-column spans must share one group_id, right-column another.
let left_groups: Vec<usize> = ordered
.iter()
.filter(|s| s.span.bbox.left() < 300.0)
.map(|s| s.group_id.unwrap())
.collect();
let right_groups: Vec<usize> = ordered
.iter()
.filter(|s| s.span.bbox.left() >= 300.0)
.map(|s| s.group_id.unwrap())
.collect();
// Within each column, group_id must be the same.
assert!(
left_groups.windows(2).all(|w| w[0] == w[1]),
"left column spans should share the same group_id: {:?}",
left_groups
);
assert!(
right_groups.windows(2).all(|w| w[0] == w[1]),
"right column spans should share the same group_id: {:?}",
right_groups
);
// The two columns must have different group_ids.
assert_ne!(
left_groups[0], right_groups[0],
"left and right columns should have different group_ids"
);
// Verify reading order keeps each column contiguous: all left-column
// spans should appear before (or after) all right-column spans.
let left_orders: Vec<usize> = ordered
.iter()
.filter(|s| s.span.bbox.left() < 300.0)
.map(|s| s.reading_order)
.collect();
let right_orders: Vec<usize> = ordered
.iter()
.filter(|s| s.span.bbox.left() >= 300.0)
.map(|s| s.reading_order)
.collect();
let left_max = *left_orders.iter().max().unwrap();
let right_min = *right_orders.iter().min().unwrap();
let left_min = *left_orders.iter().min().unwrap();
let right_max = *right_orders.iter().max().unwrap();
// Either all left before all right, or all right before all left.
assert!(
left_max < right_min || right_max < left_min,
"columns must be contiguous in reading order: left={:?} right={:?}",
left_orders,
right_orders
);
}
/// Plain-text rendering must keep group_id-separated columns as
/// contiguous blocks, not interleave them by Y-coordinate.
#[test]
fn test_group_id_plain_text_no_interleave() {
use crate::pipeline::converters::OutputConverter;
use crate::pipeline::converters::PlainTextConverter;
use crate::pipeline::reading_order::{ReadingOrderContext, ReadingOrderStrategy};
use crate::pipeline::TextPipelineConfig;
let mut strategy = XYCutStrategy::new();
strategy.min_spans_for_split = 2;
let make = |text: &str, x: f32, y: f32, w: f32| {
let mut s = make_span(x, y, w, 12.0);
s.text = text.to_string();
s
};
let spans = vec![
make("Description", 50.0, 100.0, 150.0),
make("Amount", 400.0, 100.0, 150.0),
make("Widget A", 50.0, 120.0, 150.0),
make("$150.00", 400.0, 120.0, 150.0),
make("Widget B", 50.0, 140.0, 150.0),
make("Discount", 400.0, 140.0, 150.0),
make("$25.00", 400.0, 160.0, 150.0),
];
let context = ReadingOrderContext::new();
let ordered = strategy.apply(spans, &context).unwrap();
let converter = PlainTextConverter::new();
let config = TextPipelineConfig::default();
let text = converter.convert(&ordered, &config).unwrap();
// With Y-position-based merging, same-Y spans from left and right columns
// are placed on the same line. This produces better label-value pairing:
// "Description Amount" on one line, "Widget A $150.00" on the next.
assert!(text.contains("Description"), "missing Description:\n{text}");
assert!(text.contains("Amount"), "missing Amount:\n{text}");
assert!(text.contains("Widget A"), "missing Widget A:\n{text}");
assert!(text.contains("$150.00"), "missing $150.00:\n{text}");
// Same-Y spans should be on the same line
for line in text.lines() {
if line.contains("Description") {
assert!(
line.contains("Amount"),
"Description and Amount should be on same line:\n{text}"
);
}
}
}
/// End-to-end: partition_region must return all spans (unsplit) rather than aborting
/// when the page contains a degenerate-CTM span.
#[test]
fn test_degenerate_ctm_partition_region_does_not_abort() {
let strategy = XYCutStrategy::new();
let degenerate_x: f32 = 99_992_777_785_344.0;
let spans = vec![
make_span(10.0, 100.0, 30.0, 10.0),
make_span(10.0, 85.0, 30.0, 10.0),
make_span(10.0, 70.0, 30.0, 10.0),
make_span(10.0, 55.0, 30.0, 10.0),
make_span(10.0, 40.0, 30.0, 10.0),
make_span(degenerate_x, 100.0, 30.0, 10.0),
];
// Must complete without panicking and preserve all spans.
let groups = strategy.partition_region(&spans);
let total: usize = groups.iter().map(|g| g.len()).sum();
assert_eq!(total, spans.len(), "all spans must be preserved");
}
}