xberg-native-pdf 1.3.3

PDF parsing, extraction and rendering -- the native PDF engine behind xberg.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
//! Glyph-advance, adaptive-threshold, and monospace-font-detection tests split out of `extractors/text/tests.rs` for file size. ~keep

use super::super::*;
use super::extraction_and_rotation::create_test_font;
use std::sync::Arc;

#[test]
fn test_deduplicate_content_not_overlapping() {
    let mut extractor = TextExtractor::new();
    extractor.spans = vec![
        TextSpan {
            provenance: None,
            text_rise: 0.0,
            artifact_type: None,
            text: "Hello World".to_string(),
            bbox: Rect::new(100.0, 700.0, 60.0, 12.0),
            font_name: "F1".to_string(),
            font_size: 12.0,
            font_weight: FontWeight::Normal,
            color: Color::black(),
            mcid: None,
            mcid_scope: None,
            sequence: 0,
            split_boundary_before: false,
            offset_semantic: false,
            is_italic: false,
            is_monospace: false,
            char_spacing: 0.0,
            word_spacing: 0.0,
            horizontal_scaling: 100.0,
            primary_detected: false,
            char_widths: vec![],
            char_x_offsets: Vec::new(),
            heading_level: None,
            rotation_degrees: 0.0,
            wmode: 0,
            rtl_draw_logical: false,
            mirrored: false,
            page_rotation_applied: 0,
        },
        TextSpan {
            provenance: None,
            text_rise: 0.0,
            artifact_type: None,
            text: "Hello World".to_string(),           // Same text but far apart ~keep
            bbox: Rect::new(500.0, 700.0, 60.0, 12.0), // X > 5pt difference ~keep
            font_name: "F1".to_string(),
            font_size: 12.0,
            font_weight: FontWeight::Normal,
            color: Color::black(),
            mcid: None,
            mcid_scope: None,
            sequence: 1,
            split_boundary_before: false,
            offset_semantic: false,
            is_italic: false,
            is_monospace: false,
            char_spacing: 0.0,
            word_spacing: 0.0,
            horizontal_scaling: 100.0,
            primary_detected: false,
            char_widths: vec![],
            char_x_offsets: Vec::new(),
            heading_level: None,
            rotation_degrees: 0.0,
            wmode: 0,
            rtl_draw_logical: false,
            mirrored: false,
            page_rotation_applied: 0,
        },
    ];

    extractor.deduplicate_overlapping_spans();
    assert_eq!(
        extractor.spans.len(),
        2,
        "Non-overlapping content should not be deduped"
    );
}

#[test]
fn test_advance_position_no_font() {
    let mut extractor = TextExtractor::new();
    extractor.state_stack.current_mut().font_size = 12.0;
    extractor.state_stack.current_mut().horizontal_scaling = 100.0;

    let width = extractor.advance_position_for_string(b"Hello", true).unwrap();
    assert!(width > 0.0, "Width should be positive even without font");
}

#[test]
fn test_advance_position_with_font() {
    let mut extractor = TextExtractor::new();
    let font = create_test_font();
    extractor.add_font("F1".to_string(), font);
    let current_font = extractor.fonts.get("F1").cloned();
    extractor.set_cached_current_font(current_font);
    extractor.state_stack.current_mut().font_size = 12.0;
    extractor.state_stack.current_mut().font_name = Some("F1".to_string());
    extractor.state_stack.current_mut().horizontal_scaling = 100.0;

    let width = extractor.advance_position_for_string(b"Hi", true).unwrap();
    assert!(width > 0.0, "Width should be positive with font");
}

#[test]
fn test_advance_position_with_word_space() {
    let mut extractor = TextExtractor::new();
    extractor.state_stack.current_mut().font_size = 12.0;
    extractor.state_stack.current_mut().horizontal_scaling = 100.0;
    extractor.state_stack.current_mut().word_space = 5.0;

    let width = extractor.advance_position_for_string(b"A B", true).unwrap();
    assert!(width > 0.0);
}

#[test]
fn test_insert_space_as_span() {
    let mut extractor = TextExtractor::new();
    extractor.state_stack.current_mut().font_size = 12.0;
    extractor.state_stack.current_mut().horizontal_scaling = 100.0;
    extractor.state_stack.current_mut().font_name = Some("F1".to_string());

    let before = extractor.spans.len();
    extractor.insert_space_as_span().unwrap();
    assert_eq!(extractor.spans.len(), before + 1);
    assert_eq!(extractor.spans.last().unwrap().text, " ");
    assert!(extractor.spans.last().unwrap().offset_semantic);
}

#[test]
fn test_adaptive_threshold_with_justified_text() {
    let config = TextExtractionConfig {
        use_adaptive_tj_threshold: true,
        word_margin_ratio: 0.1,
        ..TextExtractionConfig::default()
    };
    let mut extractor = TextExtractor::with_config(config);
    extractor.state_stack.current_mut().font_size = 12.0;

    // Simulate justified text (high CV) ~keep
    for i in 0..100 {
        extractor
            .tj_offset_history
            .push(if i % 2 == 0 { -50.0 } else { -200.0 });
    }

    let threshold = extractor.calculate_adaptive_tj_threshold();
    // Justified text uses 3x ratio, so threshold should be more negative ~keep
    assert!(threshold < 0.0);
}

#[test]
fn test_adaptive_threshold_with_font_name() {
    let config = TextExtractionConfig {
        use_adaptive_tj_threshold: true,
        word_margin_ratio: 0.1,
        ..TextExtractionConfig::default()
    };
    let mut extractor = TextExtractor::with_config(config);
    let font = create_test_font();
    extractor.add_font("F1".to_string(), font);
    extractor.state_stack.current_mut().font_size = 12.0;
    extractor.state_stack.current_mut().font_name = Some("F1".to_string());

    let threshold = extractor.calculate_adaptive_tj_threshold();
    assert!(threshold < 0.0);
}

#[test]
fn test_analyze_tj_distribution_zero_mean() {
    let mut extractor = TextExtractor::new();
    extractor.tj_offset_history = vec![100.0, -100.0, 100.0, -100.0];
    let (is_justified, cv) = extractor.analyze_tj_distribution();
    // Mean ~0, so CV should be 0 (avoid division by zero) ~keep
    assert_eq!(cv, 0.0);
}

#[test]
fn test_quote_operator_span_mode() {
    let mut extractor = TextExtractor::new();
    extractor.merging_config = SpanMergingConfig::legacy();
    let font = create_test_font();
    extractor.add_font("F1".to_string(), font);

    let stream = b"BT /F1 12 Tf 14 TL 100 700 Td (Line1) Tj (Line2) ' ET";
    let spans = extractor.extract_text_spans(stream).unwrap();

    let text: String = spans.iter().map(|s| s.text.as_str()).collect::<Vec<_>>().join("");
    assert!(text.contains("Line1"), "Should contain Line1, got: {}", text);
    assert!(text.contains("Line2"), "Should contain Line2, got: {}", text);
}

#[test]
fn test_double_quote_operator_span_mode() {
    let mut extractor = TextExtractor::new();
    extractor.merging_config = SpanMergingConfig::legacy();
    let font = create_test_font();
    extractor.add_font("F1".to_string(), font);

    let stream = b"BT /F1 12 Tf 14 TL 100 700 Td 1 2 (Text) \" ET";
    let spans = extractor.extract_text_spans(stream).unwrap();

    let text: String = spans.iter().map(|s| s.text.as_str()).collect::<Vec<_>>().join("");
    assert!(text.contains("Text"), "Should extract text, got: {}", text);
}

#[test]
fn test_sort_spans_by_columns() {
    let mut extractor = TextExtractor::new();
    let columns = vec![(0.0, 250.0), (300.0, 550.0)];

    extractor.spans = vec![
        TextSpan {
            provenance: None,
            text_rise: 0.0,
            artifact_type: None,
            text: "Right Col".to_string(),
            bbox: Rect::new(350.0, 700.0, 100.0, 12.0),
            font_name: "F1".to_string(),
            font_size: 12.0,
            font_weight: FontWeight::Normal,
            color: Color::black(),
            mcid: None,
            mcid_scope: None,
            sequence: 0,
            split_boundary_before: false,
            offset_semantic: false,
            is_italic: false,
            is_monospace: false,
            char_spacing: 0.0,
            word_spacing: 0.0,
            horizontal_scaling: 100.0,
            primary_detected: false,
            char_widths: vec![],
            char_x_offsets: Vec::new(),
            heading_level: None,
            rotation_degrees: 0.0,
            wmode: 0,
            rtl_draw_logical: false,
            mirrored: false,
            page_rotation_applied: 0,
        },
        TextSpan {
            provenance: None,
            text_rise: 0.0,
            artifact_type: None,
            text: "Left Col".to_string(),
            bbox: Rect::new(50.0, 700.0, 100.0, 12.0),
            font_name: "F1".to_string(),
            font_size: 12.0,
            font_weight: FontWeight::Normal,
            color: Color::black(),
            mcid: None,
            mcid_scope: None,
            sequence: 1,
            split_boundary_before: false,
            offset_semantic: false,
            is_italic: false,
            is_monospace: false,
            char_spacing: 0.0,
            word_spacing: 0.0,
            horizontal_scaling: 100.0,
            primary_detected: false,
            char_widths: vec![],
            char_x_offsets: Vec::new(),
            heading_level: None,
            rotation_degrees: 0.0,
            wmode: 0,
            rtl_draw_logical: false,
            mirrored: false,
            page_rotation_applied: 0,
        },
    ];

    extractor.sort_spans_by_columns(&columns);
    assert_eq!(extractor.spans[0].text, "Left Col");
    assert_eq!(extractor.spans[1].text, "Right Col");
}

#[test]
fn test_tj_buffer_with_mcid() {
    let state = crate::content::graphics_state::GraphicsStateStack::new();
    let buffer = TjBuffer::new(state.current(), Some(42), None);
    assert!(buffer.is_empty());
    assert_eq!(buffer.mcid, Some(42));
}

#[test]
fn test_extractor_with_primary_word_boundary() {
    let config = TextExtractionConfig {
        word_boundary_mode: WordBoundaryMode::Primary,
        ..TextExtractionConfig::default()
    };
    let mut extractor = TextExtractor::with_config(config);
    let font = create_test_font();
    extractor.add_font("F1".to_string(), font);
    extractor.merging_config = SpanMergingConfig::legacy();

    let stream = b"BT /F1 12 Tf 100 700 Td (Hello) Tj ET";
    let spans = extractor.extract_text_spans(stream).unwrap();

    let text: String = spans.iter().map(|s| s.text.as_str()).collect::<Vec<_>>().join("");
    assert!(
        text.contains("Hello"),
        "Primary mode should still extract text, got: {}",
        text
    );
}

#[test]
fn test_merge_prevents_double_space() {
    let mut extractor = TextExtractor::new();
    extractor.merging_config = SpanMergingConfig::legacy();

    extractor.spans = vec![
        TextSpan {
            provenance: None,
            text_rise: 0.0,
            artifact_type: None,
            text: "Hello ".to_string(), // ends with space ~keep
            bbox: Rect::new(100.0, 700.0, 35.0, 12.0),
            font_name: "F1".to_string(),
            font_size: 12.0,
            font_weight: FontWeight::Normal,
            color: Color::black(),
            mcid: None,
            mcid_scope: None,
            sequence: 0,
            split_boundary_before: false,
            offset_semantic: false,
            is_italic: false,
            is_monospace: false,
            char_spacing: 0.0,
            word_spacing: 0.0,
            horizontal_scaling: 100.0,
            primary_detected: false,
            char_widths: vec![],
            char_x_offsets: Vec::new(),
            heading_level: None,
            rotation_degrees: 0.0,
            wmode: 0,
            rtl_draw_logical: false,
            mirrored: false,
            page_rotation_applied: 0,
        },
        TextSpan {
            provenance: None,
            text_rise: 0.0,
            artifact_type: None,
            text: " World".to_string(),                // starts with space ~keep
            bbox: Rect::new(136.0, 700.0, 35.0, 12.0), // 1pt gap ~keep
            font_name: "F1".to_string(),
            font_size: 12.0,
            font_weight: FontWeight::Normal,
            color: Color::black(),
            mcid: None,
            mcid_scope: None,
            sequence: 1,
            split_boundary_before: true, // forces merge-with-space path ~keep
            offset_semantic: false,
            is_italic: false,
            is_monospace: false,
            char_spacing: 0.0,
            word_spacing: 0.0,
            horizontal_scaling: 100.0,
            primary_detected: false,
            char_widths: vec![],
            char_x_offsets: Vec::new(),
            heading_level: None,
            rotation_degrees: 0.0,
            wmode: 0,
            rtl_draw_logical: false,
            mirrored: false,
            page_rotation_applied: 0,
        },
    ];

    extractor.merge_adjacent_spans();
    assert_eq!(extractor.spans.len(), 1);
    // Should not have "Hello World" (triple space) ~keep
    assert!(!extractor.spans[0].text.contains("   "), "Should prevent triple space");
}

#[test]
fn test_extractor_with_config_copies_word_boundary_mode() {
    let config = TextExtractionConfig {
        word_boundary_mode: WordBoundaryMode::Primary,
        ..TextExtractionConfig::default()
    };
    let extractor = TextExtractor::with_config(config);
    assert_eq!(extractor.word_boundary_mode, WordBoundaryMode::Primary);
}

#[test]
fn test_partition_boundary_at_start() {
    let extractor = TextExtractor::new();
    let chars = vec![
        CharacterInfo {
            code: 65,
            glyph_id: None,
            width: 10.0,
            x_position: 0.0,
            tj_offset: None,
            font_size: 12.0,
            is_ligature: false,
            original_ligature: None,
            protected_from_split: false,
        },
        CharacterInfo {
            code: 66,
            glyph_id: None,
            width: 10.0,
            x_position: 10.0,
            tj_offset: None,
            font_size: 12.0,
            is_ligature: false,
            original_ligature: None,
            protected_from_split: false,
        },
    ];

    // Boundary at 0 means empty first cluster ~keep
    let clusters = extractor.partition_characters_by_boundaries(&chars, vec![0]);
    // Should have just one cluster (boundary at 0 produces no items before it) ~keep
    assert!(!clusters.is_empty());
}

#[test]
fn test_fill_cmyk_then_change_color_space() {
    let mut extractor = TextExtractor::new();
    extractor
        .execute_operator_public(Operator::SetFillCmyk {
            c: 0.5,
            m: 0.5,
            y: 0.5,
            k: 0.5,
        })
        .unwrap();
    assert!(extractor.state_stack.current().fill_color_cmyk.is_some());

    // Changing color space should reset CMYK ~keep
    extractor
        .execute_operator_public(Operator::SetFillColorSpace {
            name: "DeviceRGB".to_string(),
        })
        .unwrap();
    assert!(extractor.state_stack.current().fill_color_cmyk.is_none());
}

#[test]
fn test_bdc_with_mcid() {
    let mut extractor = TextExtractor::new();
    let mut props = HashMap::new();
    props.insert("MCID".to_string(), Object::Integer(5));

    extractor
        .execute_operator_public(Operator::BeginMarkedContentDict {
            tag: "P".to_string(),
            properties: Box::new(Object::Dictionary(props)),
        })
        .unwrap();

    assert_eq!(extractor.current_mcid, Some(5));
    assert!(!extractor.inside_artifact);
}

#[test]
fn test_bdc_artifact_with_type() {
    let mut extractor = TextExtractor::new();
    let mut props = HashMap::new();
    props.insert("Type".to_string(), Object::Name("Pagination".to_string()));
    props.insert("Subtype".to_string(), Object::Name("Header".to_string()));

    extractor
        .execute_operator_public(Operator::BeginMarkedContentDict {
            tag: "Artifact".to_string(),
            properties: Box::new(Object::Dictionary(props)),
        })
        .unwrap();

    assert!(extractor.inside_artifact);
}

#[test]
fn test_emc_resets_mcid() {
    let mut extractor = TextExtractor::new();
    extractor.current_mcid = Some(10);
    extractor.marked_content_stack.push(MarkedContentContext {
        artifact_type: None,
        tag: "P".to_string(),
        is_artifact: false,
        actual_text: None,
        expansion: None,
        is_excluded_layer: false,
        is_placed_pdf: false,
        actual_text_emitted: false,
        own_mcid: None,
    });

    extractor.execute_operator_public(Operator::EndMarkedContent).unwrap();

    assert_eq!(extractor.current_mcid, None);
    assert!(extractor.marked_content_stack.is_empty());
}

#[test]
fn test_emc_with_empty_stack() {
    let mut extractor = TextExtractor::new();
    extractor.execute_operator_public(Operator::EndMarkedContent).unwrap();
}

#[test]
fn test_bdc_with_actual_text() {
    let mut extractor = TextExtractor::new();
    let mut props = HashMap::new();
    props.insert("ActualText".to_string(), Object::String(b"fi".to_vec()));

    extractor
        .execute_operator_public(Operator::BeginMarkedContentDict {
            tag: "Span".to_string(),
            properties: Box::new(Object::Dictionary(props)),
        })
        .unwrap();

    let actual = extractor.get_current_actual_text();
    assert_eq!(actual, Some("fi".to_string()));
}

#[test]
fn test_bdc_with_expansion() {
    let mut extractor = TextExtractor::new();
    let mut props = HashMap::new();
    props.insert("E".to_string(), Object::String(b"PDF".to_vec()));

    extractor
        .execute_operator_public(Operator::BeginMarkedContentDict {
            tag: "Span".to_string(),
            properties: Box::new(Object::Dictionary(props)),
        })
        .unwrap();

    let ctx = &extractor.marked_content_stack[0];
    assert_eq!(ctx.expansion, Some("PDF".to_string()));
}

#[test]
fn test_do_operator_without_document() {
    let mut extractor = TextExtractor::new();
    // Do without document set should not panic ~keep
    extractor
        .execute_operator_public(Operator::Do {
            name: "Im1".to_string(),
        })
        .unwrap();
}

#[test]
fn test_flush_tj_span_buffer_empty_buffer() {
    let mut extractor = TextExtractor::new();
    let state = extractor.state_stack.current().clone();
    extractor.tj_span_buffer = Some(TjBuffer::new(&state, None, None));
    let before = extractor.spans.len();
    extractor.flush_tj_span_buffer().unwrap();
    assert_eq!(extractor.spans.len(), before);
}

#[test]
fn test_flush_tj_span_buffer_with_content() {
    let mut extractor = TextExtractor::new();
    let state_stack = crate::content::graphics_state::GraphicsStateStack::new();
    let mut buffer = TjBuffer::new(state_stack.current(), Some(7), None);
    buffer.append(b"Test").unwrap();
    buffer.accumulated_width = 20.0;
    extractor.tj_span_buffer = Some(buffer);

    extractor.flush_tj_span_buffer().unwrap();
    assert_eq!(extractor.spans.len(), 1);
    assert!(extractor.spans[0].text.contains("Test"));
}

#[test]
fn test_tj_array_span_mode_with_space_insertion() {
    let config = TextExtractionConfig {
        use_adaptive_tj_threshold: false,
        space_insertion_threshold: -120.0,
        ..TextExtractionConfig::default()
    };
    let mut extractor = TextExtractor::with_config(config);
    extractor.merging_config = SpanMergingConfig::legacy();
    let font = create_test_font();
    extractor.add_font("F1".to_string(), font);

    // TJ array with large offset that triggers space ~keep
    let stream = b"BT /F1 12 Tf 100 700 Td [(Word1) -500 (Word2)] TJ ET";
    let spans = extractor.extract_text_spans(stream).unwrap();

    let text: String = spans.iter().map(|s| s.text.as_str()).collect::<Vec<_>>().join("");
    assert!(text.contains("Word1"), "Should contain Word1");
    assert!(text.contains("Word2"), "Should contain Word2");
}

#[test]
fn test_sort_spans_single_column() {
    let mut extractor = TextExtractor::new();
    extractor.spans = vec![
        TextSpan {
            provenance: None,
            text_rise: 0.0,
            artifact_type: None,
            text: "Line2".to_string(),
            bbox: Rect::new(50.0, 680.0, 100.0, 12.0),
            font_name: "F1".to_string(),
            font_size: 12.0,
            font_weight: FontWeight::Normal,
            color: Color::black(),
            mcid: None,
            mcid_scope: None,
            sequence: 0,
            split_boundary_before: false,
            offset_semantic: false,
            is_italic: false,
            is_monospace: false,
            char_spacing: 0.0,
            word_spacing: 0.0,
            horizontal_scaling: 100.0,
            primary_detected: false,
            char_widths: vec![],
            char_x_offsets: Vec::new(),
            heading_level: None,
            rotation_degrees: 0.0,
            wmode: 0,
            rtl_draw_logical: false,
            mirrored: false,
            page_rotation_applied: 0,
        },
        TextSpan {
            provenance: None,
            text_rise: 0.0,
            artifact_type: None,
            text: "Line1".to_string(),
            bbox: Rect::new(50.0, 700.0, 100.0, 12.0),
            font_name: "F1".to_string(),
            font_size: 12.0,
            font_weight: FontWeight::Normal,
            color: Color::black(),
            mcid: None,
            mcid_scope: None,
            sequence: 1,
            split_boundary_before: false,
            offset_semantic: false,
            is_italic: false,
            is_monospace: false,
            char_spacing: 0.0,
            word_spacing: 0.0,
            horizontal_scaling: 100.0,
            primary_detected: false,
            char_widths: vec![],
            char_x_offsets: Vec::new(),
            heading_level: None,
            rotation_degrees: 0.0,
            wmode: 0,
            rtl_draw_logical: false,
            mirrored: false,
            page_rotation_applied: 0,
        },
    ];

    extractor.sort_spans_by_reading_order();
    assert_eq!(extractor.spans[0].text, "Line1"); // higher Y first ~keep
    assert_eq!(extractor.spans[1].text, "Line2");
}

/// A scanned vertical-CJK OCR layer can emit hundreds of single-glyph
/// `wmode=1` spans whose X-centers step by a fraction of the median
/// span width: every adjacent pair looks "same column" under a pairwise
/// `|a - b| <= tol` check, but the first and last span are hundreds of
/// points apart, so the comparator claims contradictory orderings
/// (A<B, B<C, C<A) and Rust's `sort_by` panics with "does not correctly
/// implement a total order" instead of returning a reading order.
#[test]
fn test_sort_spans_vertical_tategaki_chained_x_centers_does_not_panic() {
    let mut extractor = TextExtractor::new();
    extractor.spans = (0..240)
        .map(|i| TextSpan {
            text: format!("g{i}"),
            bbox: Rect::new(20.0 + i as f32 * 0.8, 700.0 - ((i * 37) % 96) as f32 * 7.0, 1.0, 12.0),
            font_size: 12.0,
            wmode: 1,
            ..TextSpan::default()
        })
        .collect();

    extractor.sort_spans_by_reading_order(); // must not panic ~keep
    assert_eq!(extractor.spans.len(), 240);
}

#[test]
fn test_tm_continuation_different_transform() {
    let mut extractor = TextExtractor::new();
    extractor.merging_config = SpanMergingConfig::legacy();
    let font = create_test_font();
    extractor.add_font("F1".to_string(), font);

    // Different transform params (a=2) should NOT be continuation ~keep
    let stream = b"BT /F1 12 Tf 1 0 0 1 100 700 Tm (A) Tj 2 0 0 1 120 700 Tm (B) Tj ET";
    let spans = extractor.extract_text_spans(stream).unwrap();

    assert!(!spans.is_empty());
}

#[test]
fn test_decode_pdf_text_string_single_byte() {
    let result = TextExtractor::decode_pdf_text_string(&[0x41]);
    assert_eq!(result, "A");
}

#[test]
fn test_decode_pdf_text_string_invalid_utf16() {
    // UTF-16BE BOM followed by invalid pair ~keep
    let bytes = vec![0xFE, 0xFF, 0xD8, 0x00]; // invalid surrogate half ~keep
    let result = TextExtractor::decode_pdf_text_string(&bytes);
    assert!(!result.is_empty() || result.is_empty()); // Just don't panic ~keep
}

#[test]
fn test_decode_pdf_text_string_utf16le_invalid() {
    // UTF-16LE BOM followed by odd byte count ~keep
    let bytes = vec![0xFF, 0xFE, 0x41]; // odd after BOM ~keep
    let result = TextExtractor::decode_pdf_text_string(&bytes);
    // Should handle gracefully ~keep
}

// ========================================================================
// TDD: decode_pdf_text_string — PDFDocEncoding fallback correctness
// Bytes 0xA0–0xFF and the special 0x80–0x9E zone must decode through
// PDFDocEncoding, not through from_utf8_lossy (which produces U+FFFD).
// ======================================================================== ~keep

#[test]
fn test_decode_pdfdocencoding_latin_byte() {
    // 0xE9 = PDFDocEncoding for é (U+00E9). Not valid UTF-8 on its own. ~keep
    let result = TextExtractor::decode_pdf_text_string(&[0xE9]);
    assert_eq!(
        result, "é",
        "0xE9 must decode as 'é' via PDFDocEncoding, not produce U+FFFD"
    );
}

#[test]
fn test_decode_pdfdocencoding_bullet() {
    // 0x80 = PDFDocEncoding for • (U+2022 BULLET) ~keep
    let result = TextExtractor::decode_pdf_text_string(&[0x80]);
    assert_eq!(result, "•", "0x80 must decode as bullet '•' via PDFDocEncoding");
}

#[test]
fn test_decode_pdfdocencoding_emdash() {
    // 0x84 = PDFDocEncoding for — (U+2014 EM DASH) ~keep
    let result = TextExtractor::decode_pdf_text_string(&[0x84]);
    assert_eq!(result, "—", "0x84 must decode as em-dash '—' via PDFDocEncoding");
}

#[test]
fn test_decode_pdfdocencoding_trademark() {
    // 0x92 = PDFDocEncoding for â„¢ (U+2122 TRADE MARK SIGN) ~keep
    let result = TextExtractor::decode_pdf_text_string(&[0x92]);
    assert_eq!(result, "â„¢", "0x92 must decode as trademark 'â„¢' via PDFDocEncoding");
}

#[test]
fn test_decode_pdfdocencoding_undefined_9f_is_dropped() {
    // 0x9F is undefined in PDFDocEncoding — must be silently dropped. ~keep
    let result = TextExtractor::decode_pdf_text_string(&[0x41, 0x9F, 0x42]);
    assert_eq!(result, "AB", "0x9F is undefined in PDFDocEncoding and must be dropped");
}

#[test]
fn test_decode_pdfdocencoding_mixed_ascii_and_latin() {
    // "Hello" followed by 0xE9 (é): 6 bytes → "Helloé" ~keep
    let bytes: Vec<u8> = b"Hello".iter().copied().chain([0xE9]).collect();
    let result = TextExtractor::decode_pdf_text_string(&bytes);
    assert_eq!(
        result, "Helloé",
        "Mixed ASCII + PDFDocEncoding bytes must decode correctly"
    );
}

#[test]
fn test_decode_pdfdocencoding_utf8_bytes_still_work() {
    // Valid UTF-8 without BOM: must still decode correctly (for lenient PDFs).
    // ASCII is a subset of UTF-8, so this path always works. ~keep
    let result = TextExtractor::decode_pdf_text_string(b"ASCII text");
    assert_eq!(result, "ASCII text");
}

#[test]
fn test_share_truetype_cmaps_no_donors() {
    let mut extractor = TextExtractor::new();
    let font = create_test_font();
    extractor.add_font("F1".to_string(), font);

    extractor.share_truetype_cmaps();
    assert_eq!(extractor.fonts.len(), 1);
}

#[test]
fn test_extractor_with_config_and_profile() {
    let config = TextExtractionConfig::new().with_profile(crate::config::ExtractionProfile::POLICY);

    let mut extractor = TextExtractor::with_config(config);
    let font = create_test_font();
    extractor.add_font("F1".to_string(), font);

    let stream = b"BT /F1 12 Tf 100 700 Td (Policy) Tj ET";
    let chars = extractor.extract(stream).unwrap();
    assert!(!chars.is_empty());
}

#[test]
fn test_merge_offset_semantic_space_suppression() {
    let mut extractor = TextExtractor::new();
    extractor.merging_config = SpanMergingConfig::legacy();

    extractor.spans = vec![
        TextSpan {
            provenance: None,
            text_rise: 0.0,
            artifact_type: None,
            text: "Hello".to_string(),
            bbox: Rect::new(100.0, 700.0, 30.0, 12.0),
            font_name: "F1".to_string(),
            font_size: 12.0,
            font_weight: FontWeight::Normal,
            color: Color::black(),
            mcid: None,
            mcid_scope: None,
            sequence: 0,
            split_boundary_before: false,
            offset_semantic: false,
            is_italic: false,
            is_monospace: false,
            char_spacing: 0.0,
            word_spacing: 0.0,
            horizontal_scaling: 100.0,
            primary_detected: false,
            char_widths: vec![],
            char_x_offsets: Vec::new(),
            heading_level: None,
            rotation_degrees: 0.0,
            wmode: 0,
            rtl_draw_logical: false,
            mirrored: false,
            page_rotation_applied: 0,
        },
        TextSpan {
            provenance: None,
            text_rise: 0.0,
            artifact_type: None,
            text: " ".to_string(), // offset_semantic space ~keep
            bbox: Rect::new(130.5, 700.0, 2.0, 12.0),
            font_name: "F1".to_string(),
            font_size: 12.0,
            font_weight: FontWeight::Normal,
            color: Color::black(),
            mcid: None,
            mcid_scope: None,
            sequence: 1,
            split_boundary_before: true, // forcing merge path ~keep
            offset_semantic: true,
            is_italic: false,
            is_monospace: false,
            char_spacing: 0.0,
            word_spacing: 0.0,
            horizontal_scaling: 100.0,
            primary_detected: false,
            char_widths: vec![],
            char_x_offsets: Vec::new(),
            heading_level: None,
            rotation_degrees: 0.0,
            wmode: 0,
            rtl_draw_logical: false,
            mirrored: false,
            page_rotation_applied: 0,
        },
    ];

    extractor.merge_adjacent_spans();
    // offset_semantic space should be merged without adding extra space ~keep
    let text = &extractor.spans[0].text;
    assert!(!text.contains("  "), "Should not have double space, got: '{}'", text);
}

#[test]
fn test_is_monospace_font_recognizes_broad_mono_families() {
    assert!(is_monospace_font("Menlo"));
    assert!(is_monospace_font("Fira Code"));
    assert!(is_monospace_font("Fira Mono"));
    assert!(is_monospace_font("Source Code Pro"));
    assert!(is_monospace_font("Inconsolata"));
    assert!(is_monospace_font("CMTT10"));
    assert!(is_monospace_font("LMMono10-Regular"));
    assert!(is_monospace_font("DejaVu Sans Mono"));
}

#[test]
fn test_is_monospace_font_recognizes_bare_word_mono_families() {
    // These carry "mono" as an ordinary word/suffix, not the "Monotype" foundry
    // name, and must still match. ~keep
    assert!(is_monospace_font("PT Mono"));
    assert!(is_monospace_font("Roboto Mono"));
    assert!(is_monospace_font("Nimbus Mono"));
}

#[test]
fn test_is_monospace_font_rejects_monotype_foundry_names() {
    // "Monotype" is a foundry, not a monospace family; these are script/display faces. ~keep
    assert!(!is_monospace_font("Monotype Corsiva"));
    assert!(!is_monospace_font("Arial Monotype"));
}

#[test]
fn test_is_monospace_font_still_matches_monotype_branded_monospace_faces() {
    // A Monotype-branded face that is genuinely monospace still matches via its
    // own marker ("consolas"), even though the "monotype" exclusion suppresses
    // the bare "mono" check for it. ~keep
    assert!(is_monospace_font("Monotype Consolas"));
}

#[test]
fn test_is_monospace_font_rejects_fira_sans() {
    // Fira Sans is a proportional face; only Fira Code / Fira Mono are monospace. ~keep
    assert!(!is_monospace_font("Fira Sans"));
    assert!(!is_monospace_font("Fira Sans Condensed"));
}

#[test]
fn test_tj_buffer_marks_menlo_as_monospace() {
    let state = crate::content::graphics_state::GraphicsStateStack::new();
    let font = Arc::new(FontInfo {
        base_font: "Menlo".to_string(),
        ..create_test_font()
    });
    let buffer = TjBuffer::new(state.current(), None, Some(font));
    assert!(
        buffer.is_monospace,
        "Menlo must be recognized as monospace via is_monospace_font, not just the narrow legacy list"
    );
}

#[test]
fn test_tj_buffer_does_not_mark_fira_sans_as_monospace() {
    let state = crate::content::graphics_state::GraphicsStateStack::new();
    let font = Arc::new(FontInfo {
        base_font: "Fira Sans".to_string(),
        ..create_test_font()
    });
    let buffer = TjBuffer::new(state.current(), None, Some(font));
    assert!(
        !buffer.is_monospace,
        "Fira Sans is proportional prose text and must not be routed toward code fencing"
    );
}