stet 0.8.1

Pure-Rust PostScript Level 3 interpreter — renders PostScript and EPS to raster images or PDF, with no C dependencies
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
// stet - A PostScript Interpreter
// Copyright (c) 2026 Scott Bowman
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! End-to-end tests for the `pdfmark` operator: PostScript code that
//! issues `/DOCINFO pdfmark` calls is rendered through the stet facade,
//! and the resulting PDF bytes are scanned for the expected `/Info`
//! entries.

#![cfg(feature = "pdf-output")]

use stet::Interpreter;

/// The `/Producer` entry the PDF device writes when no `pdfmark` overrides it.
///
/// Built from the crate version rather than hardcoded, so a release bump does
/// not have to touch these tests.
fn default_producer_entry() -> String {
    format!("/Producer (stet {})", env!("CARGO_PKG_VERSION"))
}

/// Locate the PDF body byte offset of the trailer's `/Info NN 0 R`
/// reference, then read the referenced indirect object out of the file.
fn find_info_dict_bytes(pdf: &[u8]) -> Vec<u8> {
    let trailer_idx = pdf
        .windows(7)
        .rposition(|w| w == b"trailer")
        .expect("trailer marker present");
    let trailer = &pdf[trailer_idx..];
    let info_marker = b"/Info ";
    let m = trailer
        .windows(info_marker.len())
        .position(|w| w == info_marker)
        .expect("/Info present in trailer");
    let after = &trailer[m + info_marker.len()..];
    let after_str = std::str::from_utf8(&after[..16.min(after.len())]).unwrap();
    let space = after_str.find(' ').unwrap();
    let info_obj_num: u32 = after_str[..space].parse().unwrap();

    let header = format!("{} 0 obj", info_obj_num);
    let start = pdf
        .windows(header.len())
        .position(|w| w == header.as_bytes())
        .expect("info indirect object present");
    let body_start = start + header.len();
    let end = pdf[body_start..]
        .windows(7)
        .position(|w| w == b"endobj\n" || w == b"endobj ")
        .expect("endobj marker present");
    pdf[body_start..body_start + end].to_vec()
}

fn render_one_page_pdf(prelude: &str) -> Vec<u8> {
    let mut interp = Interpreter::new();
    let mut script = String::new();
    script.push_str(prelude);
    script.push_str("\nshowpage\n");
    interp
        .render_to_pdf(script.as_bytes(), 72.0)
        .expect("render_to_pdf succeeds")
}

#[test]
fn docinfo_writes_title_and_author() {
    let pdf = render_one_page_pdf(
        "[ /Title (Hello World) /Author (Scott) /Subject (Phase 1) /DOCINFO pdfmark",
    );
    let info = find_info_dict_bytes(&pdf);
    let info_str = String::from_utf8_lossy(&info);
    assert!(
        info_str.contains("/Title (Hello World)"),
        "missing /Title in info dict: {info_str}",
    );
    assert!(
        info_str.contains("/Author (Scott)"),
        "missing /Author in info dict: {info_str}",
    );
    assert!(
        info_str.contains("/Subject (Phase 1)"),
        "missing /Subject in info dict: {info_str}",
    );
}

#[test]
fn docinfo_overrides_default_producer() {
    let pdf = render_one_page_pdf("[ /Producer (custom-pipeline 1.0) /DOCINFO pdfmark");
    let info = find_info_dict_bytes(&pdf);
    let info_str = String::from_utf8_lossy(&info);
    assert!(
        info_str.contains("/Producer (custom-pipeline 1.0)"),
        "expected pdfmark producer to win, got: {info_str}",
    );
    assert!(
        !info_str.contains("/Producer (stet"),
        "default producer should have been overridden: {info_str}",
    );
}

#[test]
fn docinfo_creation_date_passthrough() {
    let pdf = render_one_page_pdf("[ /CreationDate (D:20261231120000Z) /DOCINFO pdfmark");
    let info = find_info_dict_bytes(&pdf);
    let info_str = String::from_utf8_lossy(&info);
    assert!(
        info_str.contains("/CreationDate (D:20261231120000Z)"),
        "expected pdfmark creation-date to round-trip, got: {info_str}",
    );
}

#[test]
fn docinfo_trapped_writes_name() {
    let pdf = render_one_page_pdf("[ /Trapped /True /DOCINFO pdfmark");
    let info = find_info_dict_bytes(&pdf);
    let info_str = String::from_utf8_lossy(&info);
    assert!(
        info_str.contains("/Trapped /True"),
        "expected /Trapped /True in info dict: {info_str}",
    );
}

#[test]
fn no_docinfo_keeps_default_producer() {
    let pdf = render_one_page_pdf("");
    let info = find_info_dict_bytes(&pdf);
    let info_str = String::from_utf8_lossy(&info);
    assert!(
        info_str.contains(&default_producer_entry()),
        "expected default Producer when no pdfmark issued: {info_str}",
    );
    // No pdfmark → no Author / Subject / Keywords
    assert!(!info_str.contains("/Author"));
    assert!(!info_str.contains("/Subject"));
    assert!(!info_str.contains("/Keywords"));
}

/// Locate every PDF indirect object body in `pdf` whose dict contains
/// the substring `marker`. Returns each matching object body as a
/// borrowed slice of `pdf`. Used to assert on outline-tree contents
/// without having to fully parse the PDF.
fn objects_containing(pdf: &[u8], marker: &[u8]) -> Vec<Vec<u8>> {
    let mut out = Vec::new();
    let mut cursor = 0;
    while cursor < pdf.len() {
        let Some(obj_idx) = pdf[cursor..]
            .windows(b" 0 obj".len())
            .position(|w| w == b" 0 obj")
        else {
            break;
        };
        let body_start_rel = obj_idx + b" 0 obj".len();
        let body_end_rel = pdf[cursor + body_start_rel..]
            .windows(b"endobj".len())
            .position(|w| w == b"endobj")
            .unwrap_or(pdf.len() - cursor - body_start_rel);
        let body = &pdf[cursor + body_start_rel..cursor + body_start_rel + body_end_rel];
        if body.windows(marker.len()).any(|w| w == marker) {
            out.push(body.to_vec());
        }
        cursor += body_start_rel + body_end_rel;
    }
    out
}

#[test]
fn outline_simple_two_bookmarks() {
    let pdf = render_one_page_pdf(
        "[ /Title (Intro) /Page 1 /OUT pdfmark
         [ /Title (Details) /Page 1 /OUT pdfmark",
    );
    // The catalog must reference /Outlines and have /PageMode /UseOutlines.
    assert!(
        pdf.windows(b"/Outlines ".len()).any(|w| w == b"/Outlines "),
        "/Outlines not referenced from catalog"
    );
    assert!(
        pdf.windows(b"/PageMode /UseOutlines".len())
            .any(|w| w == b"/PageMode /UseOutlines"),
        "/PageMode /UseOutlines missing"
    );
    // Both bookmark titles appear as /Title literals.
    let intro = objects_containing(&pdf, b"(Intro)");
    let details = objects_containing(&pdf, b"(Details)");
    assert_eq!(intro.len(), 1, "expected exactly one Intro outline node");
    assert_eq!(
        details.len(),
        1,
        "expected exactly one Details outline node"
    );
    // The /Outlines root carries /Count 2 (two visible top-level entries).
    let outlines_root = objects_containing(&pdf, b"/Type /Outlines");
    assert_eq!(outlines_root.len(), 1);
    let root_str = String::from_utf8_lossy(&outlines_root[0]);
    assert!(
        root_str.contains("/Count 2"),
        "expected /Count 2 on /Outlines root, got: {root_str}",
    );
}

#[test]
fn outline_count_based_nesting() {
    // Adobe count-based authoring: parent declares /Count 2, then two
    // children follow. Render multi-page so /Page targets resolve.
    let mut interp = Interpreter::new();
    let script = "[ /Title (Parent) /Page 1 /Count 2 /OUT pdfmark
                  [ /Title (Child A) /Page 1 /OUT pdfmark
                  [ /Title (Child B) /Page 1 /OUT pdfmark
                  showpage";
    let pdf = interp
        .render_to_pdf(script.as_bytes(), 72.0)
        .expect("render");
    let parent = objects_containing(&pdf, b"(Parent)");
    assert_eq!(parent.len(), 1);
    let parent_str = String::from_utf8_lossy(&parent[0]);
    // Parent dict carries /First, /Last, /Count (signed).
    assert!(
        parent_str.contains("/First"),
        "parent missing /First: {parent_str}"
    );
    assert!(
        parent_str.contains("/Last"),
        "parent missing /Last: {parent_str}"
    );
    // /Count -2 (default closed since the producer didn't set positive).
    assert!(
        parent_str.contains("/Count 2") || parent_str.contains("/Count -2"),
        "parent missing /Count: {parent_str}",
    );
    // Outlines root has /Count 3 (1 parent visible + 2 children counted
    // because /Count is positive — the producer asked for expanded).
    let outlines_root = objects_containing(&pdf, b"/Type /Outlines");
    let root_str = String::from_utf8_lossy(&outlines_root[0]);
    assert!(
        root_str.contains("/Count 3"),
        "expected /Count 3 on root, got: {root_str}",
    );
}

#[test]
fn outline_uri_action() {
    let pdf = render_one_page_pdf(
        "[ /Title (Visit Example)
            /Action << /S /URI /URI (https://example.org) >>
            /OUT pdfmark",
    );
    let nodes = objects_containing(&pdf, b"(Visit Example)");
    assert_eq!(nodes.len(), 1);
    let body = String::from_utf8_lossy(&nodes[0]);
    assert!(body.contains("/A "), "expected /A action entry: {body}",);
    assert!(
        body.contains("(https://example.org)"),
        "expected URI string: {body}",
    );
    assert!(body.contains("/S /URI"), "expected /S /URI: {body}",);
}

#[test]
fn outline_view_xyz_passes_through() {
    let pdf = render_one_page_pdf("[ /Title (Top) /Page 1 /View [/XYZ 100 700 1.5] /OUT pdfmark");
    let nodes = objects_containing(&pdf, b"(Top)");
    assert_eq!(nodes.len(), 1);
    let body = String::from_utf8_lossy(&nodes[0]);
    assert!(body.contains("/XYZ"), "expected /XYZ in dest: {body}");
    assert!(body.contains("100"), "expected x=100 in dest: {body}");
    assert!(body.contains("700"), "expected y=700 in dest: {body}");
    assert!(body.contains("1.5"), "expected zoom=1.5 in dest: {body}");
}

#[test]
fn outline_named_dest_passes_through() {
    let pdf = render_one_page_pdf("[ /Title (Jump) /Dest /chap1 /OUT pdfmark");
    let nodes = objects_containing(&pdf, b"(Jump)");
    assert_eq!(nodes.len(), 1);
    let body = String::from_utf8_lossy(&nodes[0]);
    assert!(
        body.contains("/Dest (chap1)"),
        "expected named destination as literal string, got: {body}",
    );
}

#[test]
fn outline_titleless_does_not_emit() {
    // No /Title → record dropped silently → no /Outlines on catalog.
    let pdf = render_one_page_pdf("[ /Page 1 /OUT pdfmark");
    assert!(
        !pdf.windows(b"/Outlines ".len()).any(|w| w == b"/Outlines "),
        "expected catalog without /Outlines reference",
    );
}

#[test]
fn no_outline_records_no_catalog_reference() {
    // Sanity: when no /OUT pdfmark issued, /Catalog has no /Outlines.
    let pdf = render_one_page_pdf("");
    assert!(
        !pdf.windows(b"/Outlines ".len()).any(|w| w == b"/Outlines "),
        "expected catalog without /Outlines reference",
    );
}

#[test]
fn annotation_link_uri_emits_on_page() {
    let pdf = render_one_page_pdf(
        "[ /Rect [72 720 540 750] /Subtype /Link /Border [0 0 1]
            /Action << /S /URI /URI (https://example.org) >>
            /ANN pdfmark",
    );
    // /Annots array on the page dict
    let pages = objects_containing(&pdf, b"/Type /Page\n");
    assert_eq!(pages.len(), 1, "expected one page dict");
    let page_str = String::from_utf8_lossy(&pages[0]);
    assert!(
        page_str.contains("/Annots"),
        "page missing /Annots: {page_str}",
    );
    // Link annotation indirect carries /Subtype /Link + /A /URI dict
    let links = objects_containing(&pdf, b"/Subtype /Link");
    assert_eq!(links.len(), 1, "expected one /Link annotation");
    let link_body = String::from_utf8_lossy(&links[0]);
    assert!(
        link_body.contains("(https://example.org)"),
        "link missing URI string: {link_body}",
    );
    assert!(
        link_body.contains("/S /URI"),
        "link missing /S /URI: {link_body}",
    );
    // /Rect round-trip
    assert!(
        link_body.contains("/Rect"),
        "link missing /Rect: {link_body}"
    );
}

#[test]
fn annotation_link_internal_goto() {
    let mut interp = Interpreter::new();
    let script = "showpage
        showpage
        [ /Rect [50 50 150 80] /Subtype /Link /Page 1
            /Action << /S /GoTo /D [1 /Fit] >>
            /ANN pdfmark
        showpage";
    let pdf = interp
        .render_to_pdf(script.as_bytes(), 72.0)
        .expect("render");
    let links = objects_containing(&pdf, b"/Subtype /Link");
    assert_eq!(links.len(), 1);
    let body = String::from_utf8_lossy(&links[0]);
    assert!(body.contains("/S /GoTo"), "expected /S /GoTo: {body}",);
    assert!(body.contains("/Fit"), "expected /Fit view: {body}");
}

#[test]
fn annotation_text_sticky_note() {
    let pdf = render_one_page_pdf(
        "[ /Rect [400 400 420 420]
            /Subtype /Text
            /Contents (Look here)
            /Name /Note
            /ANN pdfmark",
    );
    let texts = objects_containing(&pdf, b"/Subtype /Text");
    assert_eq!(texts.len(), 1);
    let body = String::from_utf8_lossy(&texts[0]);
    assert!(body.contains("(Look here)"), "missing /Contents: {body}",);
    assert!(body.contains("/Name /Note"), "missing /Name /Note: {body}",);
    assert!(
        body.contains("/Open false"),
        "expected /Open false default: {body}",
    );
}

#[test]
fn annotation_freetext_default_appearance() {
    let pdf = render_one_page_pdf(
        "[ /Rect [100 100 300 150]
            /Subtype /FreeText
            /Contents (Visible label)
            /ANN pdfmark",
    );
    let ft = objects_containing(&pdf, b"/Subtype /FreeText");
    assert_eq!(ft.len(), 1);
    let body = String::from_utf8_lossy(&ft[0]);
    assert!(body.contains("/DA "), "FreeText missing /DA: {body}",);
    // Default appearance string applied when /DA omitted.
    assert!(
        body.contains("(0 0 0 rg /Helv 10 Tf)"),
        "expected default /DA: {body}",
    );
}

#[test]
fn annotation_implicit_page_scoping() {
    // /ANN with no /Page key scopes to the page being assembled.
    // Two showpages, one /ANN before the second showpage → it lands
    // on page 2.
    let mut interp = Interpreter::new();
    let script = "showpage
        [ /Rect [0 0 10 10] /Subtype /Text /Contents (page 2 note) /ANN pdfmark
        showpage";
    let pdf = interp
        .render_to_pdf(script.as_bytes(), 72.0)
        .expect("render");
    let annotations = objects_containing(&pdf, b"/Subtype /Text");
    assert_eq!(annotations.len(), 1);
    // The annotation's indirect ref appears in page 2's /Annots
    // (the second /Type /Page object).
    let pages = objects_containing(&pdf, b"/Type /Page\n");
    assert_eq!(pages.len(), 2);
    let page1_str = String::from_utf8_lossy(&pages[0]);
    let page2_str = String::from_utf8_lossy(&pages[1]);
    assert!(
        !page1_str.contains("/Annots"),
        "page 1 should have no /Annots: {page1_str}",
    );
    assert!(
        page2_str.contains("/Annots"),
        "page 2 should have /Annots: {page2_str}",
    );
}

#[test]
fn annotation_multiple_per_page_accumulate() {
    let pdf = render_one_page_pdf(
        "[ /Rect [0 0 10 10] /Subtype /Text /Contents (one) /ANN pdfmark
         [ /Rect [20 0 30 10] /Subtype /Text /Contents (two) /ANN pdfmark
         [ /Rect [40 0 50 10] /Subtype /Text /Contents (three) /ANN pdfmark",
    );
    let texts = objects_containing(&pdf, b"/Subtype /Text");
    assert_eq!(texts.len(), 3);
    let pages = objects_containing(&pdf, b"/Type /Page\n");
    let page_str = String::from_utf8_lossy(&pages[0]);
    assert!(page_str.contains("/Annots"));
    // /Annots array carries three indirect refs
    let annots_idx = page_str.find("/Annots").unwrap();
    let after = &page_str[annots_idx..];
    let rs: usize = after.matches(" 0 R").count();
    assert!(rs >= 3, "expected ≥3 indirect refs in /Annots: {page_str}");
}

#[test]
fn annotation_unknown_subtype_no_emit() {
    let pdf = render_one_page_pdf("[ /Rect [0 0 10 10] /Subtype /WeirdThing /ANN pdfmark");
    let pages = objects_containing(&pdf, b"/Type /Page\n");
    let page_str = String::from_utf8_lossy(&pages[0]);
    assert!(
        !page_str.contains("/Annots"),
        "page should have no /Annots when subtype unknown: {page_str}",
    );
}

#[test]
fn dest_named_emits_in_catalog_names_tree() {
    let pdf = render_one_page_pdf("[ /Dest /chap1 /Page 1 /View [/XYZ 100 700 1.5] /DEST pdfmark");
    // /Catalog must reference /Names
    let catalog = objects_containing(&pdf, b"/Type /Catalog");
    assert_eq!(catalog.len(), 1);
    let cat_str = String::from_utf8_lossy(&catalog[0]);
    assert!(
        cat_str.contains("/Names"),
        "catalog missing /Names: {cat_str}"
    );
    // The leaf names array contains the (chap1) string
    let leaves = objects_containing(&pdf, b"(chap1)");
    assert!(!leaves.is_empty(), "expected (chap1) somewhere");
}

#[test]
fn dest_outline_can_reference_named_dest() {
    // An /OUT bookmark with /Dest /chap1 + a /DEST named-dest entry
    // both end up in the output PDF and a viewer can resolve the
    // outline against the name tree.
    let pdf = render_one_page_pdf(
        "[ /Dest /chap1 /Page 1 /View [/Fit] /DEST pdfmark
         [ /Title (Chapter 1) /Dest /chap1 /OUT pdfmark",
    );
    let outline = objects_containing(&pdf, b"(Chapter 1)");
    assert_eq!(outline.len(), 1);
    let outline_body = String::from_utf8_lossy(&outline[0]);
    assert!(
        outline_body.contains("/Dest (chap1)"),
        "outline missing /Dest (chap1): {outline_body}",
    );
    // Name tree present
    let leaves = objects_containing(&pdf, b"(chap1)");
    assert!(
        leaves.len() >= 2,
        "expected (chap1) in both outline + name tree"
    );
}

#[test]
fn page_cropbox_override_applied() {
    let pdf = render_one_page_pdf("[ /CropBox [36 36 576 756] /Page 1 /PAGE pdfmark");
    let pages = objects_containing(&pdf, b"/Type /Page\n");
    assert_eq!(pages.len(), 1);
    let page_str = String::from_utf8_lossy(&pages[0]);
    assert!(
        page_str.contains("/CropBox"),
        "missing /CropBox on page: {page_str}",
    );
    assert!(
        page_str.contains("36"),
        "expected llx=36 in /CropBox: {page_str}",
    );
    assert!(
        page_str.contains("576"),
        "expected urx=576 in /CropBox: {page_str}",
    );
}

#[test]
fn pages_default_overridden_by_page() {
    // /PAGES sets a doc-wide CropBox; /PAGE for page 1 overrides it.
    // Render two pages and check page 1 has the override CropBox while
    // page 2 falls through to the /PAGES default.
    let mut interp = Interpreter::new();
    let script = "[ /CropBox [10 10 100 100] /PAGES pdfmark
         [ /CropBox [200 200 400 400] /Page 1 /PAGE pdfmark
         showpage
         showpage";
    let pdf = interp
        .render_to_pdf(script.as_bytes(), 72.0)
        .expect("render");
    let pages = objects_containing(&pdf, b"/Type /Page\n");
    assert_eq!(pages.len(), 2);
    let p1 = String::from_utf8_lossy(&pages[0]);
    let p2 = String::from_utf8_lossy(&pages[1]);
    assert!(
        p1.contains("200") && p1.contains("400"),
        "page 1 should have /PAGE override (200,400): {p1}",
    );
    assert!(
        p2.contains("10") && p2.contains("100"),
        "page 2 should have /PAGES default (10,100): {p2}",
    );
}

#[test]
fn page_rotate_emits() {
    let pdf = render_one_page_pdf("[ /Rotate 90 /Page 1 /PAGE pdfmark");
    let pages = objects_containing(&pdf, b"/Type /Page\n");
    let page_str = String::from_utf8_lossy(&pages[0]);
    assert!(
        page_str.contains("/Rotate 90"),
        "expected /Rotate 90: {page_str}",
    );
}

#[test]
fn page_rotate_invalid_dropped() {
    // /Rotate 45 is not a multiple of 90 — writer drops it silently.
    let pdf = render_one_page_pdf("[ /Rotate 45 /Page 1 /PAGE pdfmark");
    let pages = objects_containing(&pdf, b"/Type /Page\n");
    let page_str = String::from_utf8_lossy(&pages[0]);
    assert!(
        !page_str.contains("/Rotate"),
        "expected no /Rotate when value invalid: {page_str}",
    );
}

#[test]
fn no_dest_records_no_names_in_catalog() {
    let pdf = render_one_page_pdf("");
    let catalog = objects_containing(&pdf, b"/Type /Catalog");
    let cat_str = String::from_utf8_lossy(&catalog[0]);
    assert!(
        !cat_str.contains("/Names"),
        "catalog should have no /Names when no /DEST records: {cat_str}",
    );
}

#[test]
fn viewer_prefs_emits_dict() {
    let pdf = render_one_page_pdf(
        "[ /HideToolbar true /FitWindow true /Direction /L2R /VIEWERPREFERENCES pdfmark",
    );
    let catalog = objects_containing(&pdf, b"/Type /Catalog");
    let cat_str = String::from_utf8_lossy(&catalog[0]);
    assert!(
        cat_str.contains("/ViewerPreferences"),
        "/ViewerPreferences not on catalog: {cat_str}",
    );
    let vp = objects_containing(&pdf, b"/HideToolbar true");
    assert_eq!(vp.len(), 1);
    let body = String::from_utf8_lossy(&vp[0]);
    assert!(
        body.contains("/FitWindow true"),
        "missing FitWindow: {body}"
    );
    assert!(
        body.contains("/Direction /L2R"),
        "missing Direction: {body}"
    );
}

#[test]
fn viewer_prefs_page_mode_lifts_to_catalog() {
    let pdf = render_one_page_pdf(
        "[ /PageMode /FullScreen /PageLayout /TwoColumnLeft /VIEWERPREFERENCES pdfmark",
    );
    let catalog = objects_containing(&pdf, b"/Type /Catalog");
    let cat_str = String::from_utf8_lossy(&catalog[0]);
    assert!(
        cat_str.contains("/PageMode /FullScreen"),
        "expected /PageMode /FullScreen on catalog: {cat_str}",
    );
    assert!(
        cat_str.contains("/PageLayout /TwoColumnLeft"),
        "expected /PageLayout on catalog: {cat_str}",
    );
}

#[test]
fn viewer_prefs_page_mode_overrides_outline_default() {
    // /OUT records normally cause /PageMode /UseOutlines on the catalog;
    // an explicit /VIEWERPREFERENCES /PageMode /UseThumbs must win.
    let pdf = render_one_page_pdf(
        "[ /Title (Intro) /Page 1 /OUT pdfmark
         [ /PageMode /UseThumbs /VIEWERPREFERENCES pdfmark",
    );
    let catalog = objects_containing(&pdf, b"/Type /Catalog");
    let cat_str = String::from_utf8_lossy(&catalog[0]);
    assert!(
        cat_str.contains("/PageMode /UseThumbs"),
        "expected /PageMode /UseThumbs override: {cat_str}",
    );
    assert!(
        !cat_str.contains("/PageMode /UseOutlines"),
        "did not expect /PageMode /UseOutlines after override: {cat_str}",
    );
}

#[test]
fn viewer_prefs_invalid_page_layout_dropped() {
    let pdf = render_one_page_pdf("[ /PageLayout /MadeUpLayout /VIEWERPREFERENCES pdfmark");
    let catalog = objects_containing(&pdf, b"/Type /Catalog");
    let cat_str = String::from_utf8_lossy(&catalog[0]);
    assert!(
        !cat_str.contains("/PageLayout"),
        "expected invalid /PageLayout to be dropped: {cat_str}",
    );
}

#[test]
fn metadata_emits_xmp_stream() {
    let xmp =
        "<?xpacket begin='?'?><x:xmpmeta xmlns:x='adobe:ns:meta/'></x:xmpmeta><?xpacket end='w'?>";
    let pdf = render_one_page_pdf(&format!("[ /Metadata ({}) /Metadata pdfmark", xmp));
    // /Catalog references /Metadata
    let catalog = objects_containing(&pdf, b"/Type /Catalog");
    let cat_str = String::from_utf8_lossy(&catalog[0]);
    assert!(
        cat_str.contains("/Metadata "),
        "/Metadata not on catalog: {cat_str}",
    );
    // The stream object carries /Type /Metadata /Subtype /XML.
    let metadata = objects_containing(&pdf, b"/Type /Metadata");
    // Two matches: catalog (with `/Metadata <ref>`) and the stream
    // dict itself. We want at least the stream dict to appear.
    assert!(!metadata.is_empty());
    let stream_body = metadata
        .iter()
        .find(|o| {
            let s = String::from_utf8_lossy(o);
            s.contains("/Subtype /XML") && s.contains("stream")
        })
        .expect("metadata stream object present");
    let stream_str = String::from_utf8_lossy(stream_body);
    assert!(
        stream_str.contains("xmpmeta"),
        "XMP body not preserved: {stream_str}",
    );
}

#[test]
fn no_viewer_prefs_no_catalog_entry() {
    let pdf = render_one_page_pdf("");
    let catalog = objects_containing(&pdf, b"/Type /Catalog");
    let cat_str = String::from_utf8_lossy(&catalog[0]);
    assert!(
        !cat_str.contains("/ViewerPreferences"),
        "expected no /ViewerPreferences when no record: {cat_str}",
    );
    assert!(
        !cat_str.contains("/Metadata "),
        "expected no /Metadata when no record: {cat_str}",
    );
}

#[test]
fn unknown_typetag_is_silent() {
    let pdf = render_one_page_pdf("[ /Foo (bar) /SOMENEWTHING pdfmark");
    let info = find_info_dict_bytes(&pdf);
    let info_str = String::from_utf8_lossy(&info);
    // Default info dict is built without crashing; no leak from unknown
    // type-tag.
    assert!(info_str.contains(&default_producer_entry()));
    assert!(!info_str.contains("/Foo"));
}

// ----- Phase 6: /Widget + /FORM (AcroForm authoring) -----------------------

/// Locate the catalog dict bytes by scanning for the root reference
/// in the trailer and reading that indirect object out of the file.
fn find_catalog_bytes(pdf: &[u8]) -> Vec<u8> {
    let trailer_idx = pdf
        .windows(7)
        .rposition(|w| w == b"trailer")
        .expect("trailer marker present");
    let trailer = &pdf[trailer_idx..];
    let m = trailer
        .windows(b"/Root ".len())
        .position(|w| w == b"/Root ")
        .expect("/Root present in trailer");
    let after = &trailer[m + b"/Root ".len()..];
    let after_str = std::str::from_utf8(&after[..16.min(after.len())]).unwrap();
    let space = after_str.find(' ').unwrap();
    let cat_obj_num: u32 = after_str[..space].parse().unwrap();
    read_indirect_object(pdf, cat_obj_num)
}

/// Read the body bytes of indirect object `obj_num` from a PDF blob.
fn read_indirect_object(pdf: &[u8], obj_num: u32) -> Vec<u8> {
    let header = format!("{} 0 obj", obj_num);
    let start = pdf
        .windows(header.len())
        .position(|w| w == header.as_bytes())
        .unwrap_or_else(|| panic!("indirect object {obj_num} missing"));
    let body_start = start + header.len();
    let end = pdf[body_start..]
        .windows(7)
        .position(|w| w == b"endobj\n" || w == b"endobj ")
        .expect("endobj marker present");
    pdf[body_start..body_start + end].to_vec()
}

/// Pull the indirect-ref `NN 0 R` value associated with `key` from
/// the bytes of a dict. Returns the object number.
fn extract_indirect_ref(dict: &[u8], key: &str) -> Option<u32> {
    let needle = format!("/{} ", key);
    let i = dict
        .windows(needle.len())
        .position(|w| w == needle.as_bytes())?;
    let after = &dict[i + needle.len()..];
    let s = std::str::from_utf8(&after[..32.min(after.len())]).ok()?;
    let space = s.find(' ')?;
    s[..space].parse().ok()
}

#[test]
fn widget_text_field_emits_acroform() {
    let pdf = render_one_page_pdf(
        "[ /Rect [72 720 320 740] /Subtype /Widget \
         /T (firstname) /FT /Tx /V (Scott) /MaxLen 50 /ANN pdfmark",
    );
    let catalog = find_catalog_bytes(&pdf);
    let catalog_str = String::from_utf8_lossy(&catalog);
    assert!(
        catalog_str.contains("/AcroForm "),
        "/AcroForm missing from catalog: {catalog_str}",
    );
    let acro_ref = extract_indirect_ref(&catalog, "AcroForm").expect("AcroForm ref");
    let acroform = read_indirect_object(&pdf, acro_ref);
    let acro_str = String::from_utf8_lossy(&acroform);
    assert!(acro_str.contains("/NeedAppearances true"));
    assert!(acro_str.contains("/Fields ["));
}

#[test]
fn widget_text_field_merged_keys_in_annot() {
    // A single-leaf widget merges field-level keys (/T /FT /V /MaxLen)
    // into the same dict as the /Subtype /Widget annotation.
    let pdf = render_one_page_pdf(
        "[ /Rect [72 720 320 740] /Subtype /Widget \
         /T (firstname) /FT /Tx /V (Scott) /MaxLen 50 /ANN pdfmark",
    );
    let pdf_str = String::from_utf8_lossy(&pdf);
    assert!(pdf_str.contains("/Subtype /Widget"));
    assert!(pdf_str.contains("/T (firstname)"));
    assert!(pdf_str.contains("/FT /Tx"));
    assert!(pdf_str.contains("/V (Scott)"));
    assert!(pdf_str.contains("/MaxLen 50"));
}

#[test]
fn widget_checkbox_emits_btn_field_type() {
    let pdf = render_one_page_pdf(
        "[ /Rect [72 700 92 720] /Subtype /Widget \
         /T (subscribe) /FT /Btn /V /Yes /ANN pdfmark",
    );
    let pdf_str = String::from_utf8_lossy(&pdf);
    assert!(pdf_str.contains("/FT /Btn"));
    assert!(pdf_str.contains("/V /Yes"));
}

#[test]
fn widget_radio_group_lifts_field_keys_to_parent() {
    // Three widgets sharing /T (answer) should produce a parent field
    // with /Kids = three widget refs and /FT /Btn lifted up. The
    // widgets themselves omit /T (the parent owns the field name).
    let pdf = render_one_page_pdf(
        "[ /Rect [72 700 92 720] /Subtype /Widget /T (answer) /FT /Btn /Ff 32768 /V /A /ANN pdfmark
         [ /Rect [72 680 92 700] /Subtype /Widget /T (answer) /FT /Btn /Ff 32768 /V /B /ANN pdfmark
         [ /Rect [72 660 92 680] /Subtype /Widget /T (answer) /FT /Btn /Ff 32768 /V /C /ANN pdfmark",
    );
    let pdf_str = String::from_utf8_lossy(&pdf);
    // The radio parent dict carries /T (answer) /FT /Btn /Ff 32768 /Kids
    // [...] — locate it by checking the catalog's /AcroForm root has a
    // /Fields array with at least one entry, and that the parent dict
    // is present.
    assert!(pdf_str.contains("/T (answer)"));
    assert!(pdf_str.contains("/FT /Btn"));
    // /Ff 32768 lifts to the parent.
    assert!(pdf_str.contains("/Ff 32768"));
    // /Kids array referencing the three widget refs — count widget
    // annot refs by counting "/Subtype /Widget" occurrences.
    let widget_count = pdf_str.matches("/Subtype /Widget").count();
    assert_eq!(widget_count, 3, "expected 3 widget annot dicts");
}

#[test]
fn widget_choice_field_with_options() {
    let pdf = render_one_page_pdf(
        "[ /Rect [72 700 320 720] /Subtype /Widget \
         /T (color) /FT /Ch /V (Red) /Opt [(Red) (Green) (Blue)] /ANN pdfmark",
    );
    let pdf_str = String::from_utf8_lossy(&pdf);
    assert!(pdf_str.contains("/FT /Ch"));
    assert!(pdf_str.contains("/V (Red)"));
    // /Opt array — single strings flatten to (Red) (Green) (Blue).
    assert!(pdf_str.contains("/Opt [(Red) (Green) (Blue)]"));
}

#[test]
fn widget_choice_field_with_export_display_pairs() {
    let pdf = render_one_page_pdf(
        "[ /Rect [72 700 320 720] /Subtype /Widget \
         /T (size) /FT /Ch /V (M) \
         /Opt [[(S) (Small)] [(M) (Medium)] [(L) (Large)]] /ANN pdfmark",
    );
    let pdf_str = String::from_utf8_lossy(&pdf);
    // Pairs survive — at least one [(export) (display)] sub-array.
    assert!(pdf_str.contains("[(S) (Small)]"));
    assert!(pdf_str.contains("[(M) (Medium)]"));
}

#[test]
fn widget_dotted_name_builds_parent_chain() {
    // order.shipping.street should produce three nested fields:
    // top-level "order" → "shipping" → "street" (the widget itself).
    let pdf = render_one_page_pdf(
        "[ /Rect [72 700 320 720] /Subtype /Widget \
         /T (order.shipping.street) /FT /Tx /V (123 Main St) /ANN pdfmark",
    );
    let pdf_str = String::from_utf8_lossy(&pdf);
    // Each dotted segment becomes its own /T value somewhere.
    assert!(pdf_str.contains("/T (order)"));
    assert!(pdf_str.contains("/T (shipping)"));
    assert!(pdf_str.contains("/T (street)"));
    // The widget annotation carries the /V on the leaf.
    assert!(pdf_str.contains("/V (123 Main St)"));
    // The leaf widget's /Subtype /Widget dict references its
    // immediate /Parent (shipping).
    assert!(pdf_str.contains("/Parent "));
}

#[test]
fn form_record_overrides_need_appearances() {
    let pdf = render_one_page_pdf(
        "[ /Rect [72 720 320 740] /Subtype /Widget /T (firstname) /FT /Tx /ANN pdfmark
         [ /NeedAppearances false /Q 1 /FORM pdfmark",
    );
    let catalog = find_catalog_bytes(&pdf);
    let acro_ref = extract_indirect_ref(&catalog, "AcroForm").expect("AcroForm ref");
    let acroform = read_indirect_object(&pdf, acro_ref);
    let s = String::from_utf8_lossy(&acroform);
    assert!(s.contains("/NeedAppearances false"));
    assert!(s.contains("/Q 1"));
}

#[test]
fn form_default_appearance_emitted() {
    let pdf = render_one_page_pdf(
        "[ /Rect [72 720 320 740] /Subtype /Widget /T (any) /FT /Tx /ANN pdfmark
         [ /DA (/Helv 12 Tf 0 g) /FORM pdfmark",
    );
    let catalog = find_catalog_bytes(&pdf);
    let acro_ref = extract_indirect_ref(&catalog, "AcroForm").expect("AcroForm ref");
    let acroform = read_indirect_object(&pdf, acro_ref);
    let s = String::from_utf8_lossy(&acroform);
    assert!(s.contains("/DA (/Helv 12 Tf 0 g)"));
}

#[test]
fn no_widgets_no_acroform() {
    // A document with neither /Widget annotations nor /FORM should
    // not have /AcroForm in /Catalog.
    let pdf = render_one_page_pdf("[ /Title (No forms here) /DOCINFO pdfmark");
    let catalog = find_catalog_bytes(&pdf);
    let s = String::from_utf8_lossy(&catalog);
    assert!(
        !s.contains("/AcroForm"),
        "/AcroForm should not be emitted when no widgets exist: {s}",
    );
}

#[test]
fn widget_without_field_name_dropped() {
    // /T missing → widget is dropped; no /AcroForm.
    let pdf = render_one_page_pdf(
        "[ /Rect [72 720 320 740] /Subtype /Widget /FT /Tx /V (Scott) /ANN pdfmark",
    );
    let catalog = find_catalog_bytes(&pdf);
    let s = String::from_utf8_lossy(&catalog);
    assert!(!s.contains("/AcroForm"));
}

#[test]
fn widget_appears_in_page_annots_array() {
    let pdf = render_one_page_pdf(
        "[ /Rect [72 720 320 740] /Subtype /Widget /T (firstname) /FT /Tx /ANN pdfmark",
    );
    let pdf_str = String::from_utf8_lossy(&pdf);
    // Page dict gets /Annots [N 0 R] referencing the widget.
    assert!(
        pdf_str.contains("/Annots ["),
        "page should carry /Annots array"
    );
}

#[test]
fn form_only_no_widgets_emits_minimal_acroform() {
    // A standalone /FORM record should still produce /AcroForm even
    // without widgets — useful for declaring document-level defaults
    // ahead of widgets that ship in a follow-up update path.
    let pdf = render_one_page_pdf("[ /SigFlags 3 /FORM pdfmark");
    let catalog = find_catalog_bytes(&pdf);
    let acro_ref = extract_indirect_ref(&catalog, "AcroForm").expect("AcroForm ref");
    let acroform = read_indirect_object(&pdf, acro_ref);
    let s = String::from_utf8_lossy(&acroform);
    assert!(s.contains("/Fields []"));
    assert!(s.contains("/SigFlags 3"));
}

// ----- Phase 7: /EMBED + JavaScript / Named actions + page /AA -----------

#[test]
fn embed_attaches_file_and_emits_embedded_files_tree() {
    let pdf = render_one_page_pdf(
        "[ /FS (notes.txt) /DataSource (Hello, attachment.) \
         /Desc (Reader notes) /AFRelationship /Source /EMBED pdfmark",
    );
    let catalog = find_catalog_bytes(&pdf);
    let names_ref = extract_indirect_ref(&catalog, "Names").expect("/Names ref");
    let names = read_indirect_object(&pdf, names_ref);
    let names_str = String::from_utf8_lossy(&names);
    assert!(
        names_str.contains("/EmbeddedFiles "),
        "/EmbeddedFiles missing from /Names: {names_str}",
    );
    // Filespec dict carries /F (notes.txt), /Desc (Reader notes),
    // /AFRelationship /Source.
    let pdf_str = String::from_utf8_lossy(&pdf);
    assert!(pdf_str.contains("/F (notes.txt)"));
    assert!(pdf_str.contains("/Desc (Reader notes)"));
    assert!(pdf_str.contains("/AFRelationship /Source"));
    assert!(pdf_str.contains("/Type /EmbeddedFile"));
}

#[test]
fn embed_without_filename_dropped() {
    let pdf = render_one_page_pdf("[ /DataSource (data) /EMBED pdfmark");
    let catalog = find_catalog_bytes(&pdf);
    let s = String::from_utf8_lossy(&catalog);
    assert!(!s.contains("/Names"));
}

#[test]
fn embed_unknown_af_relationship_dropped() {
    let pdf = render_one_page_pdf(
        "[ /FS (notes.txt) /DataSource (x) /AFRelationship /BogusValue /EMBED pdfmark",
    );
    let pdf_str = String::from_utf8_lossy(&pdf);
    // Filespec emitted but no /AFRelationship key (allow-list dropped it).
    assert!(pdf_str.contains("/F (notes.txt)"));
    assert!(!pdf_str.contains("/AFRelationship"));
}

#[test]
fn embed_dest_and_attachment_share_names_tree() {
    // /DEST → /Names /Dests, /EMBED → /Names /EmbeddedFiles. Both
    // should land under one /Names catalog entry.
    let pdf = render_one_page_pdf(
        "[ /Dest /chap1 /Page 1 /View [/Fit] /DEST pdfmark
         [ /FS (a.txt) /DataSource (data) /EMBED pdfmark",
    );
    let catalog = find_catalog_bytes(&pdf);
    let names_ref = extract_indirect_ref(&catalog, "Names").expect("/Names ref");
    let names = read_indirect_object(&pdf, names_ref);
    let names_str = String::from_utf8_lossy(&names);
    assert!(names_str.contains("/Dests "));
    assert!(names_str.contains("/EmbeddedFiles "));
}

#[test]
fn outline_javascript_action_round_trips() {
    let pdf = render_one_page_pdf(
        "[ /Title (Run Script) \
         /Action << /S /JavaScript /JS (app.alert\\(\"hi\"\\);) >> /OUT pdfmark",
    );
    let pdf_str = String::from_utf8_lossy(&pdf);
    assert!(pdf_str.contains("/S /JavaScript"));
    assert!(pdf_str.contains("/JS "));
    assert!(pdf_str.contains("app.alert"));
}

#[test]
fn link_javascript_action_round_trips() {
    let pdf = render_one_page_pdf(
        "[ /Rect [72 720 540 750] /Subtype /Link \
         /Action << /S /JavaScript /JS (app.alert\\(\"clicked\"\\);) >> /ANN pdfmark",
    );
    let pdf_str = String::from_utf8_lossy(&pdf);
    assert!(pdf_str.contains("/S /JavaScript"));
    assert!(pdf_str.contains("app.alert"));
}

#[test]
fn outline_named_action_emits_n_name() {
    let pdf = render_one_page_pdf(
        "[ /Title (Next page) /Action << /S /Named /N /NextPage >> /OUT pdfmark",
    );
    let pdf_str = String::from_utf8_lossy(&pdf);
    assert!(pdf_str.contains("/S /Named"));
    assert!(pdf_str.contains("/N /NextPage"));
}

#[test]
fn page_aa_open_action_emits_on_page_dict() {
    let pdf = render_one_page_pdf(
        "[ /AA << /O << /S /JavaScript /JS (console.println\\(\"open\"\\);) >> >> \
         /Page 1 /PAGE pdfmark",
    );
    let pdf_str = String::from_utf8_lossy(&pdf);
    assert!(
        pdf_str.contains("/AA "),
        "page /AA missing from output: {}",
        &pdf_str[..pdf_str.len().min(2000)]
    );
    assert!(pdf_str.contains("/O "));
    assert!(pdf_str.contains("console.println"));
}

#[test]
fn page_aa_close_action_named() {
    let pdf = render_one_page_pdf("[ /AA << /C << /S /Named /N /Print >> >> /Page 1 /PAGE pdfmark");
    let pdf_str = String::from_utf8_lossy(&pdf);
    assert!(pdf_str.contains("/C "));
    assert!(pdf_str.contains("/N /Print"));
}

#[test]
fn pages_aa_lifts_to_all_pages() {
    let mut interp = Interpreter::new();
    let script = "[ /AA << /O << /S /Named /N /FirstPage >> >> /PAGES pdfmark
                  showpage
                  showpage";
    let pdf = interp
        .render_to_pdf(script.as_bytes(), 72.0)
        .expect("render");
    let pdf_str = String::from_utf8_lossy(&pdf);
    let aa_count = pdf_str.matches("/N /FirstPage").count();
    assert!(
        aa_count >= 2,
        "expected /AA action on every page, found {aa_count} instances"
    );
}