stet-pdf 0.2.1

PDF output device for stet PostScript interpreter
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
// stet - A PostScript Interpreter
// Copyright (c) 2026 Scott Bowman
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! PDF output device — accumulates pages and writes a PDF file on finish().

use stet_core::context::Context;
use stet_core::device::OutputDevice;
use stet_fonts::geometry::PsPath;
use stet_graphics::device::{ClipParams, FillParams, ImageParams, StrokeParams};
use stet_graphics::display_list::DisplayList;

use std::collections::HashMap;

use crate::content_stream::{self, ContentStreamResult, ShadingRef};
use crate::font_embedder;
use crate::font_tracker::FontTracker;
use crate::image_ops::ImageXObject;
use crate::pdf_objects::PdfObj;
use crate::pdf_writer::PdfWriter;
use crate::shading_ops;

/// A single page's data. Display list is stored and content stream generated
/// at finalize time when Context is available for font width extraction.
struct PageData {
    display_list: DisplayList,
    width_pts: f64,
    height_pts: f64,
    page_w: u32,
    page_h: u32,
    dpi: f64,
    trim_box: Option<(f64, f64, f64, f64)>,
}

/// PDF output device. Accumulates display lists per page and generates
/// a single PDF file containing all pages on `finish()`.
pub struct PdfDevice {
    pages: Vec<PageData>,
    page_w: u32,
    page_h: u32,
    dpi: f64,
    output_path: Option<String>,
    pending_trim_box: Option<(f64, f64, f64, f64)>,
    /// ICC output profile bytes, retained for forward compatibility with a
    /// future PDF/X-4 OutputIntent implementation. Currently unused.
    #[allow(dead_code)]
    output_profile: Option<Vec<u8>>,
}

impl PdfDevice {
    /// Create a new PDF device with the given page dimensions and DPI.
    pub fn new(width: u32, height: u32, dpi: f64) -> Self {
        Self {
            pages: Vec::new(),
            page_w: width,
            page_h: height,
            dpi,
            output_path: None,
            pending_trim_box: None,
            output_profile: None,
        }
    }

    /// Set the trim box for the next page (in PDF points, lower-left origin).
    pub fn set_trim_box(&mut self, llx: f64, lly: f64, urx: f64, ury: f64) {
        self.pending_trim_box = Some((llx, lly, urx, ury));
    }

    /// Set an ICC output profile.
    ///
    /// Previously embedded as a PDF/X-3 OutputIntent, but the emitted output
    /// contained transparency features (soft masks) that PDF/X-3 prohibits.
    /// The OutputIntent emission path has been removed pending a correct
    /// PDF/X-4 implementation; calling this currently has no effect on the
    /// output. The setter is retained so the API is forward-compatible with
    /// the eventual X-4 work.
    #[deprecated(
        note = "OutputIntent emission is temporarily disabled pending PDF/X-4 support; calling this has no effect"
    )]
    pub fn set_output_profile(&mut self, bytes: Vec<u8>) {
        self.output_profile = Some(bytes);
    }

    /// Build the PDF document into a byte vector.
    ///
    /// Returns the complete PDF file contents. The device must have at least
    /// one page (call after `finish()` or `finish_with_context()`).
    pub fn take_pdf_bytes(&self) -> Option<Vec<u8>> {
        if self.pages.is_empty() {
            return None;
        }
        let (writer, catalog_ref, info_ref) = self.build_pdf(None).ok()?;
        let mut buf = Vec::new();
        writer
            .write_pdf(&mut buf, catalog_ref, Some(info_ref))
            .ok()?;
        Some(buf)
    }

    /// Build the PDF document into a byte vector, using Context for font data.
    pub fn take_pdf_bytes_with_context(&self, ctx: &Context) -> Option<Vec<u8>> {
        if self.pages.is_empty() {
            return None;
        }
        let (writer, catalog_ref, info_ref) = self.build_pdf(Some(ctx)).ok()?;
        let mut buf = Vec::new();
        writer
            .write_pdf(&mut buf, catalog_ref, Some(info_ref))
            .ok()?;
        Some(buf)
    }

    /// Assemble all accumulated pages into a PDF and write to the output file.
    fn write_pdf(&self, ctx: Option<&Context>) -> Result<(), String> {
        let path = self.output_path.as_deref().ok_or("no output path set")?;
        let (writer, catalog_ref, info_ref) = self.build_pdf(ctx)?;

        let file = std::fs::File::create(path).map_err(|e| format!("create {}: {}", path, e))?;
        let mut bw = std::io::BufWriter::new(file);
        writer
            .write_pdf(&mut bw, catalog_ref, Some(info_ref))
            .map_err(|e| format!("write {}: {}", path, e))?;

        eprintln!("PDF written: {} ({} pages)", path, self.pages.len());
        Ok(())
    }

    /// Build the contents of the /Info dict. Starts with device defaults
    /// (Producer + auto-derived Title + UTC CreationDate) and lets any
    /// `/DOCINFO` pdfmark record on `ctx.pdfmark_buffer` override or
    /// extend each key. The pdfmark buffer is *not* drained here; phases
    /// past Phase 1 may want to consult it for separate concerns.
    fn build_info_dict(&self, ctx: Option<&Context>) -> Vec<(Vec<u8>, PdfObj)> {
        let docinfo = ctx.map(|c| collect_docinfo(c)).unwrap_or_default();

        let producer = docinfo
            .producer
            .clone()
            .unwrap_or_else(|| "stet".to_string());
        let mut entries: Vec<(Vec<u8>, PdfObj)> = vec![(
            b"Producer".to_vec(),
            PdfObj::LitString(producer.into_bytes()),
        )];

        // Title — pdfmark wins; otherwise derive from filename.
        let title = docinfo.title.clone().or_else(|| {
            self.output_path
                .as_deref()
                .and_then(|p| std::path::Path::new(p).file_stem())
                .and_then(|s| s.to_str())
                .map(|s| s.to_string())
        });
        if let Some(t) = title {
            entries.push((b"Title".to_vec(), PdfObj::LitString(t.into_bytes())));
        }

        for (key, value) in [
            (&b"Author"[..], &docinfo.author),
            (&b"Subject"[..], &docinfo.subject),
            (&b"Keywords"[..], &docinfo.keywords),
            (&b"Creator"[..], &docinfo.creator),
        ] {
            if let Some(v) = value {
                entries.push((key.to_vec(), PdfObj::LitString(v.clone().into_bytes())));
            }
        }

        // CreationDate — pdfmark override or default to "now in UTC".
        let creation_date = docinfo
            .creation_date_string()
            .unwrap_or_else(default_now_pdf_date);
        entries.push((
            b"CreationDate".to_vec(),
            PdfObj::LitString(creation_date.into_bytes()),
        ));

        if let Some(md) = docinfo.mod_date_string() {
            entries.push((b"ModDate".to_vec(), PdfObj::LitString(md.into_bytes())));
        }

        if let Some(t) = docinfo.trapped {
            let name: &[u8] = match t {
                stet_core::pdfmark::TrappedState::True => b"True",
                stet_core::pdfmark::TrappedState::False => b"False",
                stet_core::pdfmark::TrappedState::Unknown => b"Unknown",
                _ => b"Unknown",
            };
            entries.push((b"Trapped".to_vec(), PdfObj::Name(name.to_vec())));
        }

        entries
    }

    /// Build the PDF document, returning the writer and object refs.
    fn build_pdf(&self, ctx: Option<&Context>) -> Result<(PdfWriter, u32, u32), String> {
        let mut writer = PdfWriter::new();

        // Pre-allocate catalog and pages objects
        let catalog_ref = writer.alloc_obj();
        let pages_ref = writer.alloc_obj();

        // Document-level font tracker — shared across all pages
        let mut font_tracker = FontTracker::new();

        // First pass: build content streams and register fonts
        let mut page_results: Vec<(ContentStreamResult, &PageData)> = Vec::new();
        for page in &self.pages {
            let result = content_stream::build_content_stream(
                &page.display_list,
                page.page_w,
                page.page_h,
                page.dpi,
                ctx,
                &mut font_tracker,
            );
            page_results.push((result, page));
        }

        // Embed each unique font once at document level
        let font_obj_map: HashMap<String, u32> =
            self.embed_all_fonts(&mut writer, &font_tracker, ctx);

        // Pre-allocate page object numbers so annotations can reference
        // their target pages by indirect ref before the page dict is
        // written, and so /Annots arrays can be assembled at build time.
        let page_refs: Vec<u32> = (0..page_results.len())
            .map(|_| writer.alloc_obj())
            .collect();

        // Build per-page annotation objects up front so each page dict
        // gets its /Annots array. Widget annotations are split off and
        // emitted by `form_fields::write_form`, which owns the field
        // tree they sit under; the rest go through the standard
        // annotation path.
        let mut per_page_annots: Vec<Vec<u32>> = ctx
            .map(|c| {
                let records: Vec<stet_core::pdfmark::AnnotationRecord> = c
                    .pdfmark_buffer
                    .records()
                    .iter()
                    .filter_map(|r| match r {
                        stet_core::pdfmark::PdfMarkRecord::Annotation(rec) => Some(rec.clone()),
                        _ => None,
                    })
                    .collect();
                if records.is_empty() {
                    return vec![Vec::new(); page_refs.len()];
                }
                crate::annotations::collect_per_page(&mut writer, &records, &page_refs)
            })
            .unwrap_or_else(|| vec![Vec::new(); page_refs.len()]);

        // Form fields — Widget annotations + /FORM record assembled
        // into /AcroForm. The output's per-page widget refs merge into
        // per_page_annots above so each page's /Annots array carries
        // both standard annotations and widget annotations.
        let acroform_output = ctx.and_then(|c| {
            let widgets: Vec<(usize, stet_core::pdfmark::AnnotationRecord)> = c
                .pdfmark_buffer
                .records()
                .iter()
                .enumerate()
                .filter_map(|(i, r)| match r {
                    stet_core::pdfmark::PdfMarkRecord::Annotation(rec)
                        if matches!(
                            rec.subtype,
                            stet_core::pdfmark::AnnotationSubtype::Widget(_)
                        ) =>
                    {
                        Some((i, rec.clone()))
                    }
                    _ => None,
                })
                .collect();
            let form_record = c
                .pdfmark_buffer
                .records()
                .iter()
                .filter_map(|r| match r {
                    stet_core::pdfmark::PdfMarkRecord::Form(rec) => Some(rec.clone()),
                    _ => None,
                })
                .reduce(|acc, next| next.merge_over(&acc));
            crate::form_fields::write_form(
                &mut writer,
                &widgets,
                form_record.as_ref(),
                page_refs.len(),
            )
        });
        if let Some(out) = &acroform_output {
            for (i, refs) in out.per_page_widget_refs.iter().enumerate() {
                per_page_annots[i].extend(refs);
            }
        }

        // Layer /PAGES (document-wide defaults) under /PAGE (per-page
        // overrides) into one PageOverride per page. Later /PAGE
        // records override earlier ones key-by-key, matching the same
        // "later wins" rule we apply to /DOCINFO.
        let per_page_overrides = compute_page_overrides(ctx, page_refs.len());

        // Second pass: build page objects referencing shared font objects
        for (i, (result, page)) in page_results.iter().enumerate() {
            self.build_page(
                &mut writer,
                page,
                pages_ref,
                page_refs[i],
                result,
                &font_obj_map,
                &mut font_tracker,
                &per_page_annots[i],
                &per_page_overrides[i],
            )?;
        }

        // Pages object
        writer.set_object(
            pages_ref,
            &PdfObj::Dict(vec![
                (b"Type".to_vec(), PdfObj::name("Pages")),
                (
                    b"Kids".to_vec(),
                    PdfObj::Array(page_refs.iter().map(|&r| PdfObj::Ref(r)).collect()),
                ),
                (b"Count".to_vec(), PdfObj::Int(page_refs.len() as i64)),
            ]),
        );

        // Outlines — emitted from `/OUT pdfmark` records on the
        // pdfmark buffer. Returns `None` when no /OUT records were
        // issued, in which case /Catalog stays free of /Outlines.
        let outlines_ref = ctx.and_then(|c| {
            let records: Vec<stet_core::pdfmark::OutlineRecord> = c
                .pdfmark_buffer
                .records()
                .iter()
                .filter_map(|r| match r {
                    stet_core::pdfmark::PdfMarkRecord::Outline(rec) => Some(rec.clone()),
                    _ => None,
                })
                .collect();
            if records.is_empty() {
                return None;
            }
            let tree = stet_core::pdfmark::build_outline_tree(&records);
            crate::outline::write_outline_tree(&mut writer, &tree, &page_refs)
        });

        // /Names — combined tree of /Dests (from /DEST records) and
        // /EmbeddedFiles (from /EMBED records). Each leaf is built
        // separately, then `write_names_root` combines them into one
        // catalog-level dict.
        let dests_leaf = ctx.and_then(|c| {
            let records: Vec<stet_core::pdfmark::DestRecord> = c
                .pdfmark_buffer
                .records()
                .iter()
                .filter_map(|r| match r {
                    stet_core::pdfmark::PdfMarkRecord::Dest(rec) => Some(rec.clone()),
                    _ => None,
                })
                .collect();
            crate::names::build_dests_leaf(&mut writer, &records, &page_refs)
        });
        let embedded_files_leaf = ctx.and_then(|c| {
            let records: Vec<stet_core::pdfmark::EmbedRecord> = c
                .pdfmark_buffer
                .records()
                .iter()
                .filter_map(|r| match r {
                    stet_core::pdfmark::PdfMarkRecord::Embed(rec) => Some(rec.clone()),
                    _ => None,
                })
                .collect();
            crate::attachments::build_embedded_files_leaf(&mut writer, &records)
        });
        let names_ref =
            crate::names::write_names_root(&mut writer, dests_leaf, embedded_files_leaf);

        // /VIEWERPREFERENCES — merge all records into one effective
        // viewer-prefs bag, then split into the `/ViewerPreferences`
        // indirect object plus the catalog-level `/PageLayout` and
        // `/PageMode` entries which sit on `/Catalog` directly.
        let merged_prefs = ctx.map(collect_viewer_prefs).unwrap_or_default();
        let viewer_prefs_ref = crate::metadata::write_viewer_prefs(&mut writer, &merged_prefs);

        // /Metadata — last record wins; emit the stream object.
        let metadata_ref = ctx.and_then(|c| {
            c.pdfmark_buffer
                .records()
                .iter()
                .rev()
                .find_map(|r| match r {
                    stet_core::pdfmark::PdfMarkRecord::Metadata(rec) => Some(rec.clone()),
                    _ => None,
                })
                .map(|rec| crate::metadata::write_xmp_metadata(&mut writer, &rec))
        });

        // Catalog
        let mut catalog_entries = vec![
            (b"Type".to_vec(), PdfObj::name("Catalog")),
            (b"Pages".to_vec(), PdfObj::Ref(pages_ref)),
        ];
        if let Some(outline_ref) = outlines_ref {
            catalog_entries.push((b"Outlines".to_vec(), PdfObj::Ref(outline_ref)));
        }
        if let Some(names_ref) = names_ref {
            catalog_entries.push((b"Names".to_vec(), PdfObj::Ref(names_ref)));
        }
        if let Some(viewer_prefs_ref) = viewer_prefs_ref {
            catalog_entries.push((b"ViewerPreferences".to_vec(), PdfObj::Ref(viewer_prefs_ref)));
        }
        // /PageLayout — only the producer-supplied value, validated.
        if let Some(layout_bytes) = merged_prefs
            .page_layout
            .as_deref()
            .and_then(crate::metadata::validated_page_layout)
        {
            catalog_entries.push((b"PageLayout".to_vec(), PdfObj::Name(layout_bytes.to_vec())));
        }
        // /PageMode — producer's /VIEWERPREFERENCES /PageMode wins;
        // otherwise fall back to /UseOutlines when an outline tree
        // exists so viewers open the bookmark pane by default.
        let effective_page_mode: Option<Vec<u8>> = merged_prefs
            .page_mode
            .as_deref()
            .and_then(crate::metadata::validated_page_mode)
            .map(|v| v.to_vec())
            .or_else(|| outlines_ref.map(|_| b"UseOutlines".to_vec()));
        if let Some(mode) = effective_page_mode {
            catalog_entries.push((b"PageMode".to_vec(), PdfObj::Name(mode)));
        }
        if let Some(metadata_ref) = metadata_ref {
            catalog_entries.push((b"Metadata".to_vec(), PdfObj::Ref(metadata_ref)));
        }
        if let Some(out) = &acroform_output {
            catalog_entries.push((b"AcroForm".to_vec(), PdfObj::Ref(out.acroform_ref)));
        }

        writer.set_object(catalog_ref, &PdfObj::Dict(catalog_entries));

        // Info dictionary — start with device defaults, then let any
        // /DOCINFO pdfmark records override or extend.
        let info_ref = writer.alloc_obj();
        let info_entries = self.build_info_dict(ctx);
        writer.set_object(info_ref, &PdfObj::Dict(info_entries));

        Ok((writer, catalog_ref, info_ref))
    }

    /// Embed all tracked fonts once at document level.
    /// Returns a map from PDF font name (e.g. "F0") to the PDF object number.
    fn embed_all_fonts(
        &self,
        writer: &mut PdfWriter,
        font_tracker: &FontTracker,
        ctx: Option<&Context>,
    ) -> HashMap<String, u32> {
        let mut map = HashMap::new();
        for usage in font_tracker.fonts() {
            let font_ref = if let Some(c) = ctx {
                font_embedder::build_font_resource(writer, usage, c).unwrap_or_else(|| {
                    let tu = font_embedder::build_tounicode_for_fallback(writer, usage, c);
                    self.build_font_reference(writer, usage, tu)
                })
            } else {
                self.build_font_reference(writer, usage, None)
            };
            map.insert(usage.pdf_name.clone(), font_ref);
        }
        map
    }

    /// Build PDF objects for a single page. The page's indirect object
    /// number is pre-allocated by the caller (so annotations can target
    /// the page before its dict is written), and the per-page
    /// annotation refs are passed in for inclusion in the page's
    /// `/Annots` array.
    #[allow(clippy::too_many_arguments)]
    fn build_page(
        &self,
        writer: &mut PdfWriter,
        page: &PageData,
        pages_ref: u32,
        page_ref: u32,
        result: &ContentStreamResult,
        font_obj_map: &HashMap<String, u32>,
        font_tracker: &mut FontTracker,
        annot_refs: &[u32],
        overrides: &EffectivePageOverride,
    ) -> Result<(), String> {
        let ContentStreamResult {
            content,
            images,
            shading_refs,
            used_font_names,
            ext_gstate_dicts,
            color_spaces,
            pattern_refs,
            pattern_cs_entries,
            transfer_refs,
            halftone_refs,
            bg_ucr_refs,
        } = result;

        // Build image XObjects
        let mut xobject_entries: Vec<(Vec<u8>, PdfObj)> = Vec::new();
        for (i, img) in images.iter().enumerate() {
            let img_ref = self.build_image_xobject(writer, img);
            xobject_entries.push((format!("Im{}", i).into_bytes(), PdfObj::Ref(img_ref)));
        }

        // Build shading objects
        let mut shading_entries: Vec<(Vec<u8>, PdfObj)> = Vec::new();
        for (i, sh_ref) in shading_refs.iter().enumerate() {
            let sh_obj = match sh_ref {
                ShadingRef::Axial(p) => shading_ops::build_axial_shading(writer, p),
                ShadingRef::Radial(p) => shading_ops::build_radial_shading(writer, p),
                ShadingRef::Mesh(p) => shading_ops::build_mesh_shading(writer, p),
                ShadingRef::Patch(p) => shading_ops::build_patch_shading(writer, p),
            };
            shading_entries.push((format!("Sh{}", i).into_bytes(), PdfObj::Ref(sh_obj)));
        }

        // Build per-page font resource references (pointing to shared document-level objects)
        let mut font_entries: Vec<(Vec<u8>, PdfObj)> = Vec::new();
        for name in used_font_names {
            if let Some(&obj_ref) = font_obj_map.get(name) {
                font_entries.push((name.clone().into_bytes(), PdfObj::Ref(obj_ref)));
            }
        }

        // Resources dict
        let mut resources: Vec<(Vec<u8>, PdfObj)> = Vec::new();
        if !font_entries.is_empty() {
            resources.push((b"Font".to_vec(), PdfObj::Dict(font_entries)));
        }
        if !xobject_entries.is_empty() {
            resources.push((b"XObject".to_vec(), PdfObj::Dict(xobject_entries)));
        }
        if !shading_entries.is_empty() {
            resources.push((b"Shading".to_vec(), PdfObj::Dict(shading_entries)));
        }

        // Build ExtGState resources
        if !ext_gstate_dicts.is_empty() {
            let mut gs_entries: Vec<(Vec<u8>, PdfObj)> = Vec::new();
            for (i, gs_dict) in ext_gstate_dicts.iter().enumerate() {
                // Rebuild entries (PdfObj doesn't derive Clone)
                let mut entries: Vec<(Vec<u8>, PdfObj)> = gs_dict
                    .entries
                    .iter()
                    .map(|(k, v)| {
                        let obj = match v {
                            PdfObj::Bool(b) => PdfObj::Bool(*b),
                            PdfObj::Int(n) => PdfObj::Int(*n),
                            PdfObj::Name(n) => PdfObj::Name(n.clone()),
                            _ => PdfObj::Null,
                        };
                        (k.clone(), obj)
                    })
                    .collect();

                // Check if this ExtGState has a transfer function reference
                if let Some(tr) = transfer_refs.iter().find(|r| r.ext_gstate_idx == i) {
                    let tr2_value = build_transfer_tr2(writer, &tr.tables, tr.is_color);
                    entries.push((b"TR2".to_vec(), tr2_value));
                }

                // Check if this ExtGState has a halftone reference
                if let Some(hr) = halftone_refs.iter().find(|r| r.ext_gstate_idx == i) {
                    let ht_value = build_halftone_ht(writer, &hr.state);
                    entries.push((b"HT".to_vec(), ht_value));
                }

                // Check if this ExtGState has BG/UCR references
                if let Some(br) = bg_ucr_refs.iter().find(|r| r.ext_gstate_idx == i) {
                    if let Some(ref bg) = br.state.bg {
                        let func_ref = build_type0_function(writer, bg);
                        entries.push((b"BG2".to_vec(), PdfObj::Ref(func_ref)));
                    }
                    if let Some(ref ucr) = br.state.ucr {
                        let func_ref = build_type0_function_signed(writer, ucr);
                        entries.push((b"UCR2".to_vec(), PdfObj::Ref(func_ref)));
                    }
                }

                let gs_ref = writer.add_object(&PdfObj::Dict(entries));
                gs_entries.push((format!("GS{}", i).into_bytes(), PdfObj::Ref(gs_ref)));
            }
            resources.push((b"ExtGState".to_vec(), PdfObj::Dict(gs_entries)));
        }

        // Build ColorSpace resources (for Separation/DeviceN fill/stroke colors)
        let mut cs_entries: Vec<(Vec<u8>, PdfObj)> = Vec::new();
        for (name, spot_cs) in color_spaces {
            let cs_obj = build_spot_colorspace(spot_cs, writer);
            cs_entries.push((name.clone().into_bytes(), cs_obj));
        }
        // Add uncolored pattern color space entries (e.g., [/Pattern /DeviceRGB])
        for (name, cs_obj) in pattern_cs_entries {
            // Reconstruct PdfObj since it doesn't derive Clone
            let obj = match cs_obj {
                PdfObj::Array(items) => {
                    let cloned: Vec<PdfObj> = items
                        .iter()
                        .map(|item| match item {
                            PdfObj::Name(n) => PdfObj::Name(n.clone()),
                            PdfObj::Int(n) => PdfObj::Int(*n),
                            PdfObj::Real(n) => PdfObj::Real(*n),
                            PdfObj::Ref(r) => PdfObj::Ref(*r),
                            _ => PdfObj::Null,
                        })
                        .collect();
                    PdfObj::Array(cloned)
                }
                _ => PdfObj::Null,
            };
            cs_entries.push((name.clone().into_bytes(), obj));
        }
        if !cs_entries.is_empty() {
            resources.push((b"ColorSpace".to_vec(), PdfObj::Dict(cs_entries)));
        }

        // Build Pattern XObject resources
        if !pattern_refs.is_empty() {
            let mut pattern_entries: Vec<(Vec<u8>, PdfObj)> = Vec::new();
            for (i, pat_ref) in pattern_refs.iter().enumerate() {
                let tile_result =
                    content_stream::build_tile_content_stream(&pat_ref.tile, font_tracker);

                // Build tile resources
                let mut tile_resources: Vec<(Vec<u8>, PdfObj)> = Vec::new();

                // Tile images
                if !tile_result.images.is_empty() {
                    let mut tile_xobj: Vec<(Vec<u8>, PdfObj)> = Vec::new();
                    for (j, img) in tile_result.images.iter().enumerate() {
                        let img_ref = self.build_image_xobject(writer, img);
                        tile_xobj.push((format!("Im{}", j).into_bytes(), PdfObj::Ref(img_ref)));
                    }
                    tile_resources.push((b"XObject".to_vec(), PdfObj::Dict(tile_xobj)));
                }

                // Tile shadings
                if !tile_result.shading_refs.is_empty() {
                    let mut tile_sh: Vec<(Vec<u8>, PdfObj)> = Vec::new();
                    for (j, sh_ref) in tile_result.shading_refs.iter().enumerate() {
                        let sh_obj = match sh_ref {
                            ShadingRef::Axial(p) => shading_ops::build_axial_shading(writer, p),
                            ShadingRef::Radial(p) => shading_ops::build_radial_shading(writer, p),
                            ShadingRef::Mesh(p) => shading_ops::build_mesh_shading(writer, p),
                            ShadingRef::Patch(p) => shading_ops::build_patch_shading(writer, p),
                        };
                        tile_sh.push((format!("Sh{}", j).into_bytes(), PdfObj::Ref(sh_obj)));
                    }
                    tile_resources.push((b"Shading".to_vec(), PdfObj::Dict(tile_sh)));
                }

                // Tile fonts
                if !tile_result.used_font_names.is_empty() {
                    let mut tile_fonts: Vec<(Vec<u8>, PdfObj)> = Vec::new();
                    for name in &tile_result.used_font_names {
                        if let Some(&obj_ref) = font_obj_map.get(name) {
                            tile_fonts.push((name.clone().into_bytes(), PdfObj::Ref(obj_ref)));
                        }
                    }
                    if !tile_fonts.is_empty() {
                        tile_resources.push((b"Font".to_vec(), PdfObj::Dict(tile_fonts)));
                    }
                }

                // Tile ExtGState
                if !tile_result.ext_gstate_dicts.is_empty() {
                    let mut tile_gs: Vec<(Vec<u8>, PdfObj)> = Vec::new();
                    for (j, gs_dict) in tile_result.ext_gstate_dicts.iter().enumerate() {
                        let mut entries: Vec<(Vec<u8>, PdfObj)> = gs_dict
                            .entries
                            .iter()
                            .map(|(k, v)| {
                                let obj = match v {
                                    PdfObj::Bool(b) => PdfObj::Bool(*b),
                                    PdfObj::Int(n) => PdfObj::Int(*n),
                                    PdfObj::Name(n) => PdfObj::Name(n.clone()),
                                    _ => PdfObj::Null,
                                };
                                (k.clone(), obj)
                            })
                            .collect();
                        if let Some(tr) = tile_result
                            .transfer_refs
                            .iter()
                            .find(|r| r.ext_gstate_idx == j)
                        {
                            let tr2_value = build_transfer_tr2(writer, &tr.tables, tr.is_color);
                            entries.push((b"TR2".to_vec(), tr2_value));
                        }
                        if let Some(hr) = tile_result
                            .halftone_refs
                            .iter()
                            .find(|r| r.ext_gstate_idx == j)
                        {
                            let ht_value = build_halftone_ht(writer, &hr.state);
                            entries.push((b"HT".to_vec(), ht_value));
                        }
                        if let Some(br) = tile_result
                            .bg_ucr_refs
                            .iter()
                            .find(|r| r.ext_gstate_idx == j)
                        {
                            if let Some(ref bg) = br.state.bg {
                                let func_ref = build_type0_function(writer, bg);
                                entries.push((b"BG2".to_vec(), PdfObj::Ref(func_ref)));
                            }
                            if let Some(ref ucr) = br.state.ucr {
                                let func_ref = build_type0_function_signed(writer, ucr);
                                entries.push((b"UCR2".to_vec(), PdfObj::Ref(func_ref)));
                            }
                        }
                        let gs_ref = writer.add_object(&PdfObj::Dict(entries));
                        tile_gs.push((format!("GS{}", j).into_bytes(), PdfObj::Ref(gs_ref)));
                    }
                    tile_resources.push((b"ExtGState".to_vec(), PdfObj::Dict(tile_gs)));
                }

                // Tile color spaces
                if !tile_result.color_spaces.is_empty() {
                    let mut tile_cs: Vec<(Vec<u8>, PdfObj)> = Vec::new();
                    for (name, spot_cs) in &tile_result.color_spaces {
                        let cs_obj = build_spot_colorspace(spot_cs, writer);
                        tile_cs.push((name.clone().into_bytes(), cs_obj));
                    }
                    tile_resources.push((b"ColorSpace".to_vec(), PdfObj::Dict(tile_cs)));
                }

                // Build Pattern stream object
                let m = &pat_ref.pattern_matrix;
                let pat_dict = vec![
                    (b"Type".to_vec(), PdfObj::name("Pattern")),
                    (b"PatternType".to_vec(), PdfObj::Int(1)),
                    (
                        b"PaintType".to_vec(),
                        PdfObj::Int(pat_ref.paint_type as i64),
                    ),
                    (b"TilingType".to_vec(), PdfObj::Int(1)),
                    (
                        b"BBox".to_vec(),
                        // Expand BBox slightly beyond XStep/YStep so adjacent tiles
                        // overlap, eliminating hairline seam artifacts in PDF viewers.
                        PdfObj::Array(vec![
                            PdfObj::Real(pat_ref.bbox[0] - 0.5),
                            PdfObj::Real(pat_ref.bbox[1] - 0.5),
                            PdfObj::Real(pat_ref.bbox[2] + 0.5),
                            PdfObj::Real(pat_ref.bbox[3] + 0.5),
                        ]),
                    ),
                    (b"XStep".to_vec(), PdfObj::Real(pat_ref.xstep)),
                    (b"YStep".to_vec(), PdfObj::Real(pat_ref.ystep)),
                    (
                        b"Matrix".to_vec(),
                        PdfObj::Array(vec![
                            PdfObj::Real(m.a),
                            PdfObj::Real(m.b),
                            PdfObj::Real(m.c),
                            PdfObj::Real(m.d),
                            PdfObj::Real(m.tx),
                            PdfObj::Real(m.ty),
                        ]),
                    ),
                    (b"Resources".to_vec(), PdfObj::Dict(tile_resources)),
                ];

                let pat_obj = writer.add_stream(pat_dict, &tile_result.content, true);
                pattern_entries.push((format!("P{}", i).into_bytes(), PdfObj::Ref(pat_obj)));
            }

            resources.push((b"Pattern".to_vec(), PdfObj::Dict(pattern_entries)));
        }

        // Content stream
        let content_ref = writer.add_stream(Vec::new(), content, true);

        // Page object
        let mut page_entries = vec![
            (b"Type".to_vec(), PdfObj::name("Page")),
            (b"Parent".to_vec(), PdfObj::Ref(pages_ref)),
            (
                b"MediaBox".to_vec(),
                PdfObj::Array(vec![
                    PdfObj::Int(0),
                    PdfObj::Int(0),
                    PdfObj::Real(page.width_pts),
                    PdfObj::Real(page.height_pts),
                ]),
            ),
            (b"Contents".to_vec(), PdfObj::Ref(content_ref)),
            (b"Resources".to_vec(), PdfObj::Dict(resources)),
        ];
        // /CropBox / /BleedBox / /TrimBox / /ArtBox: pdfmark /PAGE or
        // /PAGES wins; otherwise fall back to the device's pending
        // trim_box (set via PdfDevice::set_trim_box).
        let effective_trim = overrides.boxes.trim_box.or_else(|| {
            page.trim_box
                .map(|(llx, lly, urx, ury)| [llx, lly, urx, ury])
        });
        for (name, b) in [
            (b"CropBox".as_slice(), overrides.boxes.crop_box),
            (b"BleedBox".as_slice(), overrides.boxes.bleed_box),
            (b"TrimBox".as_slice(), effective_trim),
            (b"ArtBox".as_slice(), overrides.boxes.art_box),
        ] {
            if let Some([llx, lly, urx, ury]) = b {
                page_entries.push((
                    name.to_vec(),
                    PdfObj::Array(vec![
                        PdfObj::Real(llx),
                        PdfObj::Real(lly),
                        PdfObj::Real(urx),
                        PdfObj::Real(ury),
                    ]),
                ));
            }
        }
        if let Some(rotate) = overrides.rotate
            && matches!(rotate, 0 | 90 | 180 | 270 | -90 | -180 | -270)
        {
            page_entries.push((b"Rotate".to_vec(), PdfObj::Int(rotate as i64)));
        }
        if !annot_refs.is_empty() {
            page_entries.push((
                b"Annots".to_vec(),
                PdfObj::Array(annot_refs.iter().map(|r| PdfObj::Ref(*r)).collect()),
            ));
        }
        if let Some(aa) = &overrides.additional_actions
            && !aa.is_empty()
        {
            // Page-level /AA — open / close hooks. Page refs aren't
            // available to action targets here (an /O /GoTo can land
            // on any page; we use the empty page_refs slice so out-of-
            // range refs short-circuit to None and the action drops).
            let mut aa_entries: Vec<(Vec<u8>, PdfObj)> = Vec::new();
            if let Some(action) = &aa.on_open
                && let Some(dict) = crate::outline::encode_action(action, &[])
            {
                aa_entries.push((b"O".to_vec(), dict));
            }
            if let Some(action) = &aa.on_close
                && let Some(dict) = crate::outline::encode_action(action, &[])
            {
                aa_entries.push((b"C".to_vec(), dict));
            }
            if !aa_entries.is_empty() {
                page_entries.push((b"AA".to_vec(), PdfObj::Dict(aa_entries)));
            }
        }
        writer.set_object(page_ref, &PdfObj::Dict(page_entries));

        Ok(())
    }

    /// Build a PDF font reference for a tracked font.
    ///
    /// For Standard 14 fonts, creates a simple Type1 font dict.
    /// For other fonts, also creates a simple Type1 dict (no embedding yet).
    /// Both include a ToUnicode CMap for searchability.
    fn build_font_reference(
        &self,
        writer: &mut PdfWriter,
        usage: &crate::font_tracker::FontUsage,
        tounicode_override: Option<u32>,
    ) -> u32 {
        // Build ToUnicode CMap — use override if provided, otherwise fall back to naive mapping
        let tounicode_ref = tounicode_override.or_else(|| self.build_tounicode_cmap(writer, usage));

        let mut entries: Vec<(Vec<u8>, PdfObj)> = vec![
            (b"Type".to_vec(), PdfObj::name("Font")),
            (b"Subtype".to_vec(), PdfObj::name("Type1")),
            (b"BaseFont".to_vec(), PdfObj::Name(usage.font_name.clone())),
        ];

        if !usage.is_standard_14 {
            // For non-standard fonts, add Encoding
            entries.push((b"Encoding".to_vec(), PdfObj::name("WinAnsiEncoding")));
        }

        if let Some(tu_ref) = tounicode_ref {
            entries.push((b"ToUnicode".to_vec(), PdfObj::Ref(tu_ref)));
        }

        writer.add_object(&PdfObj::Dict(entries))
    }

    /// Build a ToUnicode CMap for a font.
    ///
    /// Maps character codes to Unicode based on common Adobe glyph naming.
    /// For printable ASCII codes, maps code→Unicode directly (works for most
    /// Latin text fonts). The full glyph-name-based mapping requires encoding
    /// array access (deferred to font embedding phase).
    fn build_tounicode_cmap(
        &self,
        writer: &mut PdfWriter,
        usage: &crate::font_tracker::FontUsage,
    ) -> Option<u32> {
        use std::collections::HashMap;

        let mut map: HashMap<u16, u16> = HashMap::new();

        for &code in &usage.used_codes {
            if code <= 255 {
                // For ASCII range, assume code = Unicode (works for standard encodings)
                if (0x20..=0x7E).contains(&code) {
                    map.insert(code, code);
                }
            }
        }

        if map.is_empty() {
            return None;
        }

        let font_name = String::from_utf8_lossy(&usage.font_name);
        let cmap_data = generate_tounicode_cmap(&map, &font_name);
        Some(writer.add_stream(Vec::new(), &cmap_data, true))
    }

    /// Build a PDF image XObject from prepared image data. Returns the object number.
    fn build_image_xobject(&self, writer: &mut PdfWriter, img: &ImageXObject) -> u32 {
        // Build SMask if present
        let smask_ref = img.smask_data.as_ref().map(|smask_data| {
            writer.add_stream(
                vec![
                    (b"Type".to_vec(), PdfObj::name("XObject")),
                    (b"Subtype".to_vec(), PdfObj::name("Image")),
                    (b"Width".to_vec(), PdfObj::Int(img.width as i64)),
                    (b"Height".to_vec(), PdfObj::Int(img.height as i64)),
                    (b"ColorSpace".to_vec(), PdfObj::name("DeviceGray")),
                    (b"BitsPerComponent".to_vec(), PdfObj::Int(8)),
                    (b"Interpolate".to_vec(), PdfObj::Bool(false)),
                ],
                smask_data,
                true,
            )
        });

        // Build ICC profile stream if needed
        let icc_ref = img.icc_profile.as_ref().map(|icc| {
            writer.add_stream(
                vec![(b"N".to_vec(), PdfObj::Int(icc.n as i64))],
                &icc.data,
                true,
            )
        });

        // Build PDF ColorSpace value
        let cs_obj = build_pdf_colorspace(&img.pdf_color_space, icc_ref, writer);

        // Main image XObject
        let mut entries = vec![
            (b"Type".to_vec(), PdfObj::name("XObject")),
            (b"Subtype".to_vec(), PdfObj::name("Image")),
            (b"Width".to_vec(), PdfObj::Int(img.width as i64)),
            (b"Height".to_vec(), PdfObj::Int(img.height as i64)),
        ];

        if img.is_imagemask {
            entries.push((b"ImageMask".to_vec(), PdfObj::Bool(true)));
            // Imagemasks don't have ColorSpace or BitsPerComponent in the XObject
            entries.push((
                b"Decode".to_vec(),
                PdfObj::Array(vec![PdfObj::Int(1), PdfObj::Int(0)]),
            ));
        } else {
            entries.push((b"ColorSpace".to_vec(), cs_obj));
            entries.push((
                b"BitsPerComponent".to_vec(),
                PdfObj::Int(img.bits_per_component as i64),
            ));
        }

        entries.push((b"Interpolate".to_vec(), PdfObj::Bool(false)));

        if let Some(smask) = smask_ref {
            entries.push((b"SMask".to_vec(), PdfObj::Ref(smask)));
        }

        // Color key masking (ImageType 4): /Mask array of 2×n integers
        if let Some(ref ckm) = img.color_key_mask {
            let ncomp = img.pdf_color_space.num_components();
            let mask_ints: Vec<PdfObj> = if ckm.len() == ncomp {
                // Exact match: expand each value v to [v, v] range pair
                ckm.iter()
                    .flat_map(|&v| [PdfObj::Int(v as i64), PdfObj::Int(v as i64)])
                    .collect()
            } else {
                // Range match: already in [min0, max0, min1, max1, ...] form
                ckm.iter().map(|&v| PdfObj::Int(v as i64)).collect()
            };
            entries.push((b"Mask".to_vec(), PdfObj::Array(mask_ints)));
        }

        writer.add_stream(entries, &img.sample_data, true)
    }
}

/// Build a PDF color space object from our enum.
fn build_pdf_colorspace(
    cs: &crate::image_ops::PdfColorSpace,
    icc_ref: Option<u32>,
    writer: &mut PdfWriter,
) -> PdfObj {
    use crate::image_ops::PdfColorSpace;
    match cs {
        PdfColorSpace::DeviceGray => PdfObj::name("DeviceGray"),
        PdfColorSpace::DeviceRGB => PdfObj::name("DeviceRGB"),
        PdfColorSpace::DeviceCMYK => PdfObj::name("DeviceCMYK"),
        PdfColorSpace::ICCBased { .. } => {
            if let Some(ref_num) = icc_ref {
                PdfObj::Array(vec![PdfObj::name("ICCBased"), PdfObj::Ref(ref_num)])
            } else {
                PdfObj::name("DeviceRGB") // fallback
            }
        }
        PdfColorSpace::Indexed {
            base,
            hival,
            lookup,
        } => {
            let base_obj = build_pdf_colorspace(base, None, writer);
            // Embed lookup table as a hex string stream
            let lookup_ref = writer.add_stream(Vec::new(), lookup, true);
            PdfObj::Array(vec![
                PdfObj::name("Indexed"),
                base_obj,
                PdfObj::Int(*hival as i64),
                PdfObj::Ref(lookup_ref),
            ])
        }
        PdfColorSpace::Separation {
            name,
            alt,
            tint_table,
        } => {
            let alt_obj = build_pdf_colorspace(alt, None, writer);
            let func_ref = build_tint_function(tint_table, writer);
            PdfObj::Array(vec![
                PdfObj::name("Separation"),
                PdfObj::Name(name.clone()),
                alt_obj,
                PdfObj::Ref(func_ref),
            ])
        }
        PdfColorSpace::DeviceN {
            names,
            alt,
            tint_table,
        } => {
            let alt_obj = build_pdf_colorspace(alt, None, writer);
            let func_ref = build_tint_function(tint_table, writer);
            let names_arr = PdfObj::Array(names.iter().map(|n| PdfObj::Name(n.clone())).collect());
            PdfObj::Array(vec![
                PdfObj::name("DeviceN"),
                names_arr,
                alt_obj,
                PdfObj::Ref(func_ref),
            ])
        }
    }
}

/// Build a PDF Type 0 (sampled) function stream from a TintLookupTable.
/// Returns the object number of the function stream.
fn build_tint_function(
    table: &stet_graphics::device::TintLookupTable,
    writer: &mut PdfWriter,
) -> u32 {
    let ni = table.num_inputs as usize;
    let no = table.num_outputs as usize;

    // Convert f32 data (0.0–1.0) to u8 samples (0–255).
    // Our TintLookupTable stores data in row-major order (last dimension varies fastest),
    // but PDF Type 0 functions require the first dimension to vary fastest.
    // For 1D, the order is the same. For ND, we must transpose.
    let spd = table.samples_per_dim as usize;
    let total_entries = spd.pow(ni as u32);
    let samples: Vec<u8> = if ni <= 1 {
        table
            .data
            .iter()
            .map(|&v| (v.clamp(0.0, 1.0) * 255.0) as u8)
            .collect()
    } else {
        // Reorder: iterate in PDF order (dim0 fastest) and look up in our order (dim0 slowest)
        let mut out = Vec::with_capacity(total_entries * no);
        for pdf_idx in 0..total_entries {
            // Decompose pdf_idx with dim0 varying fastest
            let mut coords = vec![0usize; ni];
            let mut rem = pdf_idx;
            for coord in coords.iter_mut() {
                *coord = rem % spd;
                rem /= spd;
            }
            // Convert to our row-major index (dim0 slowest, last dim fastest)
            let mut our_idx = 0;
            for coord in coords.iter() {
                our_idx = our_idx * spd + coord;
            }
            let base = our_idx * no;
            for c in 0..no {
                out.push((table.data[base + c].clamp(0.0, 1.0) * 255.0) as u8);
            }
        }
        out
    };

    // Domain: [0 1] repeated for each input
    let mut domain = Vec::with_capacity(ni * 2);
    for _ in 0..ni {
        domain.push(PdfObj::Int(0));
        domain.push(PdfObj::Int(1));
    }

    // Range: [0 1] repeated for each output
    let mut range = Vec::with_capacity(no * 2);
    for _ in 0..no {
        range.push(PdfObj::Int(0));
        range.push(PdfObj::Int(1));
    }

    // Size: samples_per_dim repeated for each input dimension
    let size: Vec<PdfObj> = (0..ni)
        .map(|_| PdfObj::Int(table.samples_per_dim as i64))
        .collect();

    let dict_entries = vec![
        (b"FunctionType".to_vec(), PdfObj::Int(0)),
        (b"Domain".to_vec(), PdfObj::Array(domain)),
        (b"Range".to_vec(), PdfObj::Array(range)),
        (b"Size".to_vec(), PdfObj::Array(size)),
        (b"BitsPerSample".to_vec(), PdfObj::Int(8)),
    ];

    writer.add_stream(dict_entries, &samples, true)
}

/// Build a PDF Separation or DeviceN color space array from a SpotColorSpace.
/// Returns a PdfObj (array) suitable for inclusion in the Resources/ColorSpace dict.
fn build_spot_colorspace(
    spot_cs: &stet_graphics::device::SpotColorSpace,
    writer: &mut PdfWriter,
) -> PdfObj {
    use stet_graphics::device::{SimpleColorSpace, SpotColorSpace};
    match spot_cs {
        SpotColorSpace::Separation {
            name,
            alt,
            tint_table,
        } => {
            let alt_obj = match alt {
                SimpleColorSpace::DeviceGray => PdfObj::name("DeviceGray"),
                SimpleColorSpace::DeviceRGB => PdfObj::name("DeviceRGB"),
                SimpleColorSpace::DeviceCMYK => PdfObj::name("DeviceCMYK"),
            };
            let func_ref = build_tint_function(tint_table, writer);
            PdfObj::Array(vec![
                PdfObj::name("Separation"),
                PdfObj::Name(name.clone()),
                alt_obj,
                PdfObj::Ref(func_ref),
            ])
        }
        SpotColorSpace::DeviceN {
            names,
            alt,
            tint_table,
        } => {
            let alt_obj = match alt {
                SimpleColorSpace::DeviceGray => PdfObj::name("DeviceGray"),
                SimpleColorSpace::DeviceRGB => PdfObj::name("DeviceRGB"),
                SimpleColorSpace::DeviceCMYK => PdfObj::name("DeviceCMYK"),
            };
            let func_ref = build_tint_function(tint_table, writer);
            let names_arr = PdfObj::Array(names.iter().map(|n| PdfObj::Name(n.clone())).collect());
            PdfObj::Array(vec![
                PdfObj::name("DeviceN"),
                names_arr,
                alt_obj,
                PdfObj::Ref(func_ref),
            ])
        }
        _ => PdfObj::name("DeviceRGB"),
    }
}

/// Generate a ToUnicode CMap stream.
fn generate_tounicode_cmap(map: &std::collections::HashMap<u16, u16>, font_name: &str) -> Vec<u8> {
    use std::io::Write;
    let mut buf = Vec::new();

    writeln!(buf, "/CIDInit /ProcSet findresource begin").unwrap();
    writeln!(buf, "12 dict begin").unwrap();
    writeln!(buf, "begincmap").unwrap();
    writeln!(buf, "/CIDSystemInfo <<").unwrap();
    writeln!(buf, "  /Registry (Adobe)").unwrap();
    writeln!(buf, "  /Ordering (UCS)").unwrap();
    writeln!(buf, "  /Supplement 0").unwrap();
    writeln!(buf, ">> def").unwrap();
    writeln!(buf, "/CMapName /{}-UCS def", font_name).unwrap();
    writeln!(buf, "/CMapType 2 def").unwrap();
    writeln!(buf, "1 begincodespacerange").unwrap();
    writeln!(buf, "<00> <FF>").unwrap();
    writeln!(buf, "endcodespacerange").unwrap();

    let mut sorted: Vec<_> = map.iter().collect();
    sorted.sort_by_key(|&(&code, _)| code);

    for chunk in sorted.chunks(100) {
        writeln!(buf, "{} beginbfchar", chunk.len()).unwrap();
        for &(&code, &unicode) in chunk {
            writeln!(buf, "<{:02X}> <{:04X}>", code, unicode).unwrap();
        }
        writeln!(buf, "endbfchar").unwrap();
    }

    writeln!(buf, "endcmap").unwrap();
    writeln!(buf, "CMapName currentdict /CMap defineresource pop").unwrap();
    writeln!(buf, "end").unwrap();
    writeln!(buf, "end").unwrap();

    buf
}

impl OutputDevice for PdfDevice {
    fn fill_path(&mut self, _path: &PsPath, _params: &FillParams) {}
    fn stroke_path(&mut self, _path: &PsPath, _params: &StrokeParams) {}
    fn clip_path(&mut self, _path: &PsPath, _params: &ClipParams) {}
    fn init_clip(&mut self) {}
    fn erase_page(&mut self) {}

    fn set_trim_box(&mut self, llx: f64, lly: f64, urx: f64, ury: f64) {
        self.pending_trim_box = Some((llx, lly, urx, ury));
    }

    fn show_page(&mut self, _output_path: &str) -> Result<(), String> {
        Ok(())
    }

    fn draw_image(&mut self, _sample_data: &[u8], _params: &ImageParams) {}

    fn page_size(&self) -> (u32, u32) {
        (self.page_w, self.page_h)
    }

    fn replay_and_show(&mut self, list: DisplayList, output_path: &str) -> Result<(), String> {
        // Capture output path from first page
        if self.output_path.is_none() {
            // Strip extension (.png or .pdf)
            let base = if let Some(pos) = output_path.rfind('.') {
                &output_path[..pos]
            } else {
                output_path
            };
            // Remove -NNNN page number suffix (e.g., "arc-0001" → "arc")
            let base = if base.len() >= 5 && base.as_bytes()[base.len() - 5] == b'-' {
                let suffix = &base[base.len() - 4..];
                if suffix.bytes().all(|b| b.is_ascii_digit()) {
                    &base[..base.len() - 5]
                } else {
                    base
                }
            } else {
                base
            };
            self.output_path = Some(format!("{}.pdf", base));
        }

        let scale = 72.0 / self.dpi;

        self.pages.push(PageData {
            display_list: list,
            width_pts: self.page_w as f64 * scale,
            height_pts: self.page_h as f64 * scale,
            page_w: self.page_w,
            page_h: self.page_h,
            dpi: self.dpi,
            trim_box: self.pending_trim_box.take(),
        });

        Ok(())
    }

    fn finish(&mut self) -> Result<(), String> {
        if self.pages.is_empty() {
            return Ok(());
        }
        self.write_pdf(None)
    }

    fn finish_with_context(&mut self, ctx: &Context) -> Result<(), String> {
        if self.pages.is_empty() {
            return Ok(());
        }
        self.write_pdf(Some(ctx))
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

/// Parse an ICC profile header to extract the number of components and description.
///
/// Returns (N, description) where N is derived from the color space signature
/// at bytes 16–19 and description is extracted from the `desc` or `mluc` tag.
///
/// Currently unused — kept for forward compatibility with the planned
/// PDF/X-4 OutputIntent implementation.
#[allow(dead_code)]
fn parse_icc_header(data: &[u8]) -> (u32, String) {
    let n = if data.len() >= 20 {
        match &data[16..20] {
            b"CMYK" => 4,
            b"RGB " => 3,
            b"GRAY" => 1,
            b"Lab " => 3,
            _ => 4, // assume CMYK for unknown
        }
    } else {
        4
    };
    let desc = extract_icc_description(data).unwrap_or_else(|| "Custom".to_string());
    (n, desc)
}

/// Extract the profile description from an ICC profile's tag table.
///
/// Looks for the `desc` tag (v2, type 'desc') or `mluc` tag (v4, type 'mluc').
#[allow(dead_code)]
fn extract_icc_description(data: &[u8]) -> Option<String> {
    if data.len() < 132 {
        return None;
    }
    let tag_count = u32::from_be_bytes(data[128..132].try_into().ok()?) as usize;
    let tag_table_start = 132;

    for i in 0..tag_count {
        let offset = tag_table_start + i * 12;
        if offset + 12 > data.len() {
            break;
        }
        let tag_sig = &data[offset..offset + 4];
        let tag_offset = u32::from_be_bytes(data[offset + 4..offset + 8].try_into().ok()?) as usize;
        let tag_size = u32::from_be_bytes(data[offset + 8..offset + 12].try_into().ok()?) as usize;

        if tag_sig != b"desc" {
            continue;
        }
        if tag_offset + tag_size > data.len() || tag_size < 12 {
            return None;
        }

        let type_sig = &data[tag_offset..tag_offset + 4];
        if type_sig == b"desc" {
            // ICC v2 'desc' type: u32 count at offset+8, ASCII string at offset+12
            let count =
                u32::from_be_bytes(data[tag_offset + 8..tag_offset + 12].try_into().ok()?) as usize;
            if count == 0 {
                return None;
            }
            let str_end = (tag_offset + 12 + count).min(tag_offset + tag_size);
            let s = &data[tag_offset + 12..str_end];
            // Trim trailing null bytes
            let s = s.split(|&b| b == 0).next().unwrap_or(s);
            return Some(String::from_utf8_lossy(s).to_string());
        } else if type_sig == b"mluc" {
            // ICC v4 'mluc' type: multi-localized Unicode
            if tag_size < 20 {
                return None;
            }
            let record_count =
                u32::from_be_bytes(data[tag_offset + 8..tag_offset + 12].try_into().ok()?) as usize;
            if record_count == 0 {
                return None;
            }
            // First record: language(2) + country(2) + length(4) + offset(4)
            let rec_base = tag_offset + 16;
            if rec_base + 12 > data.len() {
                return None;
            }
            let str_len =
                u32::from_be_bytes(data[rec_base + 4..rec_base + 8].try_into().ok()?) as usize;
            let str_off =
                u32::from_be_bytes(data[rec_base + 8..rec_base + 12].try_into().ok()?) as usize;
            let abs_off = tag_offset + str_off;
            if abs_off + str_len > data.len() || str_len < 2 {
                return None;
            }
            // UTF-16BE → String
            let utf16: Vec<u16> = data[abs_off..abs_off + str_len]
                .chunks_exact(2)
                .map(|c| u16::from_be_bytes([c[0], c[1]]))
                .collect();
            return Some(
                String::from_utf16_lossy(&utf16)
                    .trim_end_matches('\0')
                    .to_string(),
            );
        }

        break;
    }
    None
}

/// Build a PDF Type 0 (sampled) function stream from a 256-entry transfer table.
/// Returns the object number of the function stream.
fn build_type0_function(writer: &mut PdfWriter, table: &[f64]) -> u32 {
    let dict_entries = vec![
        (b"FunctionType".to_vec(), PdfObj::Int(0)),
        (
            b"Domain".to_vec(),
            PdfObj::Array(vec![PdfObj::Int(0), PdfObj::Int(1)]),
        ),
        (
            b"Range".to_vec(),
            PdfObj::Array(vec![PdfObj::Int(0), PdfObj::Int(1)]),
        ),
        (
            b"Size".to_vec(),
            PdfObj::Array(vec![PdfObj::Int(table.len() as i64)]),
        ),
        (b"BitsPerSample".to_vec(), PdfObj::Int(8)),
    ];
    let data: Vec<u8> = table
        .iter()
        .map(|&v| (v.clamp(0.0, 1.0) * 255.0).round() as u8)
        .collect();
    writer.add_stream(dict_entries, &data, false)
}

/// Build the /TR2 value for an ExtGState dict from transfer function tables.
/// Returns a PdfObj (Ref for single function, Array for 4-component, or Name for identity).
fn build_transfer_tr2(
    writer: &mut PdfWriter,
    tables: &[Option<std::sync::Arc<Vec<f64>>>],
    is_color: bool,
) -> PdfObj {
    if is_color && tables.len() == 4 {
        // 4-component: [R, G, B, Gray], use /Identity for None entries
        let refs: Vec<PdfObj> = tables
            .iter()
            .map(|t| {
                if let Some(table) = t {
                    let func_ref = build_type0_function(writer, table);
                    PdfObj::Ref(func_ref)
                } else {
                    PdfObj::name("Identity")
                }
            })
            .collect();
        PdfObj::Array(refs)
    } else if !is_color && tables.len() == 1 {
        if let Some(ref table) = tables[0] {
            let func_ref = build_type0_function(writer, table);
            PdfObj::Ref(func_ref)
        } else {
            PdfObj::name("Identity")
        }
    } else {
        PdfObj::name("Identity")
    }
}

/// Build a PDF Type 0 (sampled) function stream from a 256-entry table with signed range [-1,1].
/// Used for undercolor removal (UCR) functions.
fn build_type0_function_signed(writer: &mut PdfWriter, table: &[f64]) -> u32 {
    let dict_entries = vec![
        (b"FunctionType".to_vec(), PdfObj::Int(0)),
        (
            b"Domain".to_vec(),
            PdfObj::Array(vec![PdfObj::Int(0), PdfObj::Int(1)]),
        ),
        (
            b"Range".to_vec(),
            PdfObj::Array(vec![PdfObj::Int(-1), PdfObj::Int(1)]),
        ),
        (
            b"Size".to_vec(),
            PdfObj::Array(vec![PdfObj::Int(table.len() as i64)]),
        ),
        (b"BitsPerSample".to_vec(), PdfObj::Int(8)),
    ];
    // Encode [-1,1] → [0,255]: byte = (v + 1) / 2 * 255
    let data: Vec<u8> = table
        .iter()
        .map(|&v| ((v.clamp(-1.0, 1.0) + 1.0) / 2.0 * 255.0).round() as u8)
        .collect();
    writer.add_stream(dict_entries, &data, false)
}

/// Build a PDF Type 4 (PostScript calculator) function from token bytes.
/// Domain is 2D [-1,1]×[-1,1], Range [0,1].
fn build_type4_function(writer: &mut PdfWriter, tokens: &[u8]) -> u32 {
    let dict_entries = vec![
        (b"FunctionType".to_vec(), PdfObj::Int(4)),
        (
            b"Domain".to_vec(),
            PdfObj::Array(vec![
                PdfObj::Int(-1),
                PdfObj::Int(1),
                PdfObj::Int(-1),
                PdfObj::Int(1),
            ]),
        ),
        (
            b"Range".to_vec(),
            PdfObj::Array(vec![PdfObj::Int(0), PdfObj::Int(1)]),
        ),
    ];
    writer.add_stream(dict_entries, tokens, false)
}

/// Build a PDF Type 0 (sampled) 2D function from a 64×64 sample table.
/// Domain is [-1,1]×[-1,1], Range [0,1].
fn build_type0_function_2d(writer: &mut PdfWriter, table: &[f64]) -> u32 {
    let dict_entries = vec![
        (b"FunctionType".to_vec(), PdfObj::Int(0)),
        (
            b"Domain".to_vec(),
            PdfObj::Array(vec![
                PdfObj::Int(-1),
                PdfObj::Int(1),
                PdfObj::Int(-1),
                PdfObj::Int(1),
            ]),
        ),
        (
            b"Range".to_vec(),
            PdfObj::Array(vec![PdfObj::Int(0), PdfObj::Int(1)]),
        ),
        (
            b"Size".to_vec(),
            PdfObj::Array(vec![PdfObj::Int(64), PdfObj::Int(64)]),
        ),
        (b"BitsPerSample".to_vec(), PdfObj::Int(8)),
    ];
    let data: Vec<u8> = table
        .iter()
        .map(|&v| (v.clamp(0.0, 1.0) * 255.0).round() as u8)
        .collect();
    writer.add_stream(dict_entries, &data, false)
}

/// Build a PDF halftone screen object (Type 1 halftone dict) from a HalftoneScreen.
/// Returns a PdfObj (either inline Dict or Ref to indirect object).
fn build_halftone_screen(
    writer: &mut PdfWriter,
    screen: &stet_graphics::device::HalftoneScreen,
) -> PdfObj {
    let spot_func = if let Some(ref tokens) = screen.type4_tokens {
        let func_ref = build_type4_function(writer, tokens);
        PdfObj::Ref(func_ref)
    } else if let Some(ref table) = screen.sampled_2d {
        let func_ref = build_type0_function_2d(writer, table);
        PdfObj::Ref(func_ref)
    } else {
        PdfObj::name("Default")
    };

    let entries = vec![
        (b"Type".to_vec(), PdfObj::name("Halftone")),
        (b"HalftoneType".to_vec(), PdfObj::Int(1)),
        (b"Frequency".to_vec(), PdfObj::Real(screen.frequency)),
        (b"Angle".to_vec(), PdfObj::Real(screen.angle)),
        (b"SpotFunction".to_vec(), spot_func),
    ];
    let obj_ref = writer.add_object(&PdfObj::Dict(entries));
    PdfObj::Ref(obj_ref)
}

/// Build the /HT value for an ExtGState dict from a HalftoneState.
fn build_halftone_ht(
    writer: &mut PdfWriter,
    state: &stet_graphics::device::HalftoneState,
) -> PdfObj {
    if let Some(ref color) = state.color {
        // Type 5 composite halftone
        let mut entries = vec![
            (b"Type".to_vec(), PdfObj::name("Halftone")),
            (b"HalftoneType".to_vec(), PdfObj::Int(5)),
        ];
        let component_names: [&[u8]; 4] = [b"Red", b"Green", b"Blue", b"Default"];
        for (i, screen_opt) in color.iter().enumerate() {
            if let Some(screen) = screen_opt {
                let ht_obj = build_halftone_screen(writer, screen);
                entries.push((component_names[i].to_vec(), ht_obj));
            }
        }
        let obj_ref = writer.add_object(&PdfObj::Dict(entries));
        PdfObj::Ref(obj_ref)
    } else if let Some(ref gray) = state.gray {
        build_halftone_screen(writer, gray)
    } else {
        PdfObj::name("Default")
    }
}

/// Effective per-page override after layering /PAGES under /PAGE.
#[derive(Default, Clone)]
struct EffectivePageOverride {
    boxes: stet_core::pdfmark::PageBoxes,
    rotate: Option<i32>,
    additional_actions: Option<stet_core::pdfmark::PageAdditionalActions>,
}

/// Walk the pdfmark buffer and compute one [`EffectivePageOverride`]
/// per page in `0..page_count`. Order of precedence per key:
/// 1. Last `/PAGE` for that specific page (later record wins).
/// 2. Last `/PAGES` (later document-wide record wins).
fn compute_page_overrides(ctx: Option<&Context>, page_count: usize) -> Vec<EffectivePageOverride> {
    use stet_core::pdfmark::{PageOverrideScope, PdfMarkRecord};
    let mut out = vec![EffectivePageOverride::default(); page_count];
    let Some(c) = ctx else {
        return out;
    };
    let mut all_boxes = stet_core::pdfmark::PageBoxes::default();
    let mut all_rotate: Option<i32> = None;
    let mut all_aa: Option<stet_core::pdfmark::PageAdditionalActions> = None;
    let mut per_page_boxes: Vec<stet_core::pdfmark::PageBoxes> =
        vec![stet_core::pdfmark::PageBoxes::default(); page_count];
    let mut per_page_rotate: Vec<Option<i32>> = vec![None; page_count];
    let mut per_page_aa: Vec<Option<stet_core::pdfmark::PageAdditionalActions>> =
        vec![None; page_count];

    for record in c.pdfmark_buffer.records() {
        let PdfMarkRecord::PageOverride(rec) = record else {
            continue;
        };
        match rec.scope {
            PageOverrideScope::All => {
                all_boxes = rec.boxes.merge_over(&all_boxes);
                if rec.rotate.is_some() {
                    all_rotate = rec.rotate;
                }
                if let Some(new_aa) = &rec.additional_actions {
                    all_aa = Some(match all_aa {
                        Some(prev) => new_aa.merge_over(&prev),
                        None => new_aa.clone(),
                    });
                }
            }
            PageOverrideScope::Single(page) => {
                let idx = page as usize;
                if idx == 0 || idx > page_count {
                    continue;
                }
                let i = idx - 1;
                per_page_boxes[i] = rec.boxes.merge_over(&per_page_boxes[i]);
                if rec.rotate.is_some() {
                    per_page_rotate[i] = rec.rotate;
                }
                if let Some(new_aa) = &rec.additional_actions {
                    per_page_aa[i] = Some(match per_page_aa[i].clone() {
                        Some(prev) => new_aa.merge_over(&prev),
                        None => new_aa.clone(),
                    });
                }
            }
            _ => continue,
        }
    }

    for i in 0..page_count {
        out[i].boxes = per_page_boxes[i].merge_over(&all_boxes);
        out[i].rotate = per_page_rotate[i].or(all_rotate);
        out[i].additional_actions = match (&per_page_aa[i], &all_aa) {
            (Some(p), Some(a)) => Some(p.merge_over(a)),
            (Some(p), None) => Some(p.clone()),
            (None, Some(a)) => Some(a.clone()),
            (None, None) => None,
        };
    }
    out
}

/// Convert days since 1970-01-01 to (year, month, day).
fn days_to_ymd(days: u64) -> (u64, u64, u64) {
    // Civil calendar algorithm from Howard Hinnant
    let z = days + 719468;
    let era = z / 146097;
    let doe = z - era * 146097;
    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
    let y = yoe + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let d = doy - (153 * mp + 2) / 5 + 1;
    let m = if mp < 10 { mp + 3 } else { mp - 9 };
    let y = if m <= 2 { y + 1 } else { y };
    (y, m, d)
}

/// Format the current wall-clock time as a PDF date string in UTC.
fn default_now_pdf_date() -> String {
    use std::time::SystemTime;
    let now = SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();
    let secs_per_day = 86400u64;
    let days = now / secs_per_day;
    let time_of_day = now % secs_per_day;
    let hours = time_of_day / 3600;
    let minutes = (time_of_day % 3600) / 60;
    let seconds = time_of_day % 60;
    let (year, month, day) = days_to_ymd(days);
    format!(
        "D:{:04}{:02}{:02}{:02}{:02}{:02}Z",
        year, month, day, hours, minutes, seconds
    )
}

/// Merge every `/DOCINFO` pdfmark record on the buffer into a single
/// effective record. Later records override earlier ones key-by-key,
/// matching GhostScript pdfwrite's behaviour where multiple
/// `[ /DOCINFO pdfmark` blocks accumulate.
/// Merge every `/VIEWERPREFERENCES pdfmark` record into one effective
/// record. Later records override earlier ones key-by-key, matching
/// the same "later wins" rule we apply to `/DOCINFO`.
fn collect_viewer_prefs(ctx: &Context) -> stet_core::pdfmark::ViewerPrefsRecord {
    use stet_core::pdfmark::{PdfMarkRecord, ViewerPrefsRecord};
    let mut acc = ViewerPrefsRecord::default();
    for record in ctx.pdfmark_buffer.records() {
        if let PdfMarkRecord::ViewerPrefs(rec) = record {
            acc = rec.merge_over(&acc);
        }
    }
    acc
}

fn collect_docinfo(ctx: &Context) -> stet_core::pdfmark::DocInfoRecord {
    let mut acc = stet_core::pdfmark::DocInfoRecord::default();
    for record in ctx.pdfmark_buffer.records() {
        let stet_core::pdfmark::PdfMarkRecord::DocInfo(rec) = record else {
            continue;
        };
        if let Some(v) = &rec.title {
            acc.title = Some(v.clone());
        }
        if let Some(v) = &rec.author {
            acc.author = Some(v.clone());
        }
        if let Some(v) = &rec.subject {
            acc.subject = Some(v.clone());
        }
        if let Some(v) = &rec.keywords {
            acc.keywords = Some(v.clone());
        }
        if let Some(v) = &rec.creator {
            acc.creator = Some(v.clone());
        }
        if let Some(v) = &rec.producer {
            acc.producer = Some(v.clone());
        }
        if let Some(v) = &rec.creation_date {
            acc.creation_date = Some(v.clone());
        }
        if let Some(v) = &rec.mod_date {
            acc.mod_date = Some(v.clone());
        }
        if let Some(v) = rec.trapped {
            acc.trapped = Some(v);
        }
    }
    acc
}