flpdf 0.3.0

A pure-Rust PDF toolkit modeled on qpdf — read, rewrite, linearize, and inspect PDFs.
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
//! High-level AcroForm document helper.
//!
//! [`AcroFormDocumentHelper`] wraps a `&mut Pdf<R>` and exposes document-level
//! operations for interactive form fields. It builds on
//! [`crate::FormFieldObjectHelper`] for inherited value lookup and on
//! [`crate::copy_objects`] for cross-document field copying.

use crate::object::MAX_INLINE_DEPTH;
use crate::ref_chain::resolve_ref_chain;
use crate::{
    copy_objects, json_inspect::decode_pdf_text_string, Dictionary, Error, FormFieldObjectHelper,
    Object, ObjectRef, Pdf, Result, DEFAULT_MAX_ACROFORM_DEPTH,
};
use std::collections::{BTreeMap, BTreeSet};
use std::io::{Read, Seek};

type AcroFormInheritedEntries = Vec<(Vec<u8>, Object)>;
type FieldCopySet = BTreeSet<ObjectRef>;

/// Effective metadata for one AcroForm field-tree node.
///
/// Values are materialized from the current node plus inherited field-tree
/// state. `/DA`, `/Q`, and `/MaxLen` may inherit from `/AcroForm` defaults.
#[derive(Debug, Clone, PartialEq)]
pub struct AcroFormFieldInfo {
    /// The field dictionary object.
    pub object_ref: ObjectRef,
    /// Direct `/T` partial name bytes, when present.
    pub partial_name: Option<Vec<u8>>,
    /// Dot-joined field name path reconstructed from ancestor `/T` entries.
    pub full_name: String,
    /// Effective `/FT` field type.
    pub field_type: Option<Vec<u8>>,
    /// Effective `/V` field value.
    pub value: Option<Object>,
    /// Effective `/DV` default field value.
    pub default_value: Option<Object>,
    /// Effective `/Ff` field flags.
    pub field_flags: Option<i64>,
    /// Effective `/DA` default appearance.
    pub default_appearance: Option<Object>,
    /// Effective `/Q` quadding value.
    pub quadding: Option<i64>,
    /// Effective `/MaxLen` text-field maximum length.
    pub max_len: Option<i64>,
}

#[derive(Debug, Clone, Default)]
struct FieldInheritance {
    full_name: String,
    field_type: Option<Vec<u8>>,
    value: Option<Object>,
    default_value: Option<Object>,
    field_flags: Option<i64>,
    default_appearance: Option<Object>,
    quadding: Option<i64>,
    max_len: Option<i64>,
}

/// High-level helper for a document's `/AcroForm`.
///
/// Construct with [`AcroFormDocumentHelper::new`] or [`Pdf::acroform`]. The
/// helper holds no cached field state; methods re-read the live document so
/// prior mutations are immediately visible.
///
/// For a runnable walkthrough see `examples/list_form_fields.rs`.
pub struct AcroFormDocumentHelper<'a, R: Read + Seek> {
    pdf: &'a mut Pdf<R>,
}

impl<'a, R: Read + Seek> AcroFormDocumentHelper<'a, R> {
    /// Create a new helper borrowing `pdf` mutably.
    pub fn new(pdf: &'a mut Pdf<R>) -> Self {
        Self { pdf }
    }

    /// Return all field-tree object refs in preorder.
    ///
    /// Missing `/AcroForm` or missing/malformed `/Fields` returns an empty list.
    /// Cycles are ignored after the first visit.
    ///
    /// # Errors
    ///
    /// - [`Error::Unsupported`] when the catalog or a field-tree node is not a
    ///   dictionary, when an indirect `/AcroForm` reference does not resolve to a
    ///   dictionary, or when the field-tree depth limit is exceeded. A direct
    ///   non-dictionary `/AcroForm` value is ignored, not rejected.
    /// - Any error from [`Pdf::resolve`].
    pub fn fields(&mut self) -> Result<Vec<ObjectRef>> {
        let Some(acroform) = self.acroform_dict()? else {
            return Ok(Vec::new());
        };
        let Some(fields) = resolve_array_value(self.pdf, acroform.get("Fields").cloned())? else {
            return Ok(Vec::new());
        };

        let mut out = Vec::new();
        let mut seen = BTreeSet::new();
        for item in fields {
            if let Object::Reference(field_ref) = item {
                self.walk_field_tree(field_ref, &mut seen, &mut out)?;
            }
        }
        Ok(out)
    }

    /// Return all field-tree nodes with effective inherited metadata.
    ///
    /// Missing `/AcroForm` or missing/malformed `/Fields` returns an empty
    /// list. Cycles are ignored after the first visit.
    ///
    /// # Errors
    ///
    /// - [`Error::Unsupported`] when the catalog or a field-tree node is not a
    ///   dictionary, when an indirect `/AcroForm` reference does not resolve to a
    ///   dictionary, or when the field-tree depth limit is exceeded. A direct
    ///   non-dictionary `/AcroForm` value is ignored, not rejected.
    /// - Any error from [`Pdf::resolve`].
    pub fn field_infos(&mut self) -> Result<Vec<AcroFormFieldInfo>> {
        let Some(acroform) = self.acroform_dict()? else {
            return Ok(Vec::new());
        };
        let Some(fields) = resolve_array_value(self.pdf, acroform.get("Fields").cloned())? else {
            return Ok(Vec::new());
        };

        // `?` is not usable inside a struct literal, so materialize the
        // AcroForm-default leaves (which may be indirect) into locals first.
        let default_appearance = deref_leaf(self.pdf, acroform.get("DA").cloned())?;
        let quadding = inherited_integer(self.pdf, &acroform, "Q")?;
        let max_len = inherited_integer(self.pdf, &acroform, "MaxLen")?;
        let inherited = FieldInheritance {
            default_appearance,
            quadding,
            max_len,
            ..FieldInheritance::default()
        };

        let mut out = Vec::new();
        let mut seen = BTreeSet::new();
        for item in fields {
            if let Object::Reference(field_ref) = item {
                self.walk_field_info_tree(field_ref, inherited.clone(), &mut seen, &mut out, 0)?;
            }
        }
        Ok(out)
    }

    /// Return the field's inherited `/V` value.
    ///
    /// # Errors
    ///
    /// - [`Error::Unsupported`] when a field-tree node is not a dictionary, or
    ///   when the field-tree depth limit is exceeded.
    /// - Any error from [`Pdf::resolve`].
    pub fn field_value(&mut self, field_ref: ObjectRef) -> Result<Option<Object>> {
        FormFieldObjectHelper::new(field_ref, self.pdf).field_value()
    }

    /// Set the field's direct `/V` value.
    ///
    /// This updates the field dictionary itself. It does not synthesize widget
    /// appearance streams.
    ///
    /// # Errors
    ///
    /// - [`Error::Unsupported`] when `field_ref` does not resolve to a
    ///   dictionary.
    /// - Any error from [`Pdf::resolve`].
    pub fn set_field_value(&mut self, field_ref: ObjectRef, value: Object) -> Result<()> {
        let mut dict = self.resolve_field_dict(field_ref)?;
        dict.insert("V", value);
        self.pdf.set_object(field_ref, Object::Dictionary(dict));
        Ok(())
    }

    /// Set `/AcroForm/DA`, creating `/AcroForm` if needed.
    ///
    /// # Errors
    ///
    /// - [`Error::Missing`] when the document has no `/Root`.
    /// - [`Error::Unsupported`] when the catalog or `/AcroForm` does not resolve
    ///   to a dictionary, or when the object-number space is exhausted while
    ///   creating `/AcroForm`.
    /// - Any error from [`Pdf::resolve`].
    pub fn set_default_appearance(&mut self, appearance: Vec<u8>) -> Result<()> {
        let acroform_ref = self.ensure_acroform_ref()?;
        let mut acroform = self.resolve_dict(acroform_ref, "AcroForm")?;
        acroform.insert("DA", Object::String(appearance));
        self.pdf
            .set_object(acroform_ref, Object::Dictionary(acroform));
        Ok(())
    }

    /// Copy `/AcroForm/DA` onto fields that do not carry a direct `/DA`.
    ///
    /// Existing field-level `/DA` values are preserved.
    ///
    /// # Errors
    ///
    /// - [`Error::Unsupported`] when the catalog or a field-tree node is not a
    ///   dictionary, when an indirect `/AcroForm` reference does not resolve to a
    ///   dictionary, or when the field-tree depth limit is exceeded. A direct
    ///   non-dictionary `/AcroForm` value is ignored, not rejected.
    /// - Any error from [`Pdf::resolve`].
    pub fn fix_appearance_inheritance(&mut self) -> Result<()> {
        let Some(acroform) = self.acroform_dict()? else {
            return Ok(());
        };
        let Some(da) = acroform.get("DA").cloned() else {
            return Ok(());
        };
        let Some(fields) = resolve_array_value(self.pdf, acroform.get("Fields").cloned())? else {
            return Ok(());
        };

        let mut seen = BTreeSet::new();
        for item in fields {
            if let Object::Reference(field_ref) = item {
                self.fix_field_appearance_inheritance(
                    field_ref,
                    &da,
                    &BTreeMap::new(),
                    &mut seen,
                    0,
                )?;
            }
        }
        Ok(())
    }

    /// Copy all top-level fields from `source` and append them to this document.
    ///
    /// Returns the copied top-level field refs in the target document.
    ///
    /// # Errors
    ///
    /// - [`Error::Missing`] when the target document has no `/Root`.
    /// - [`Error::Unsupported`] when the catalog or a field-tree node is not a
    ///   dictionary, when an indirect `/AcroForm` reference does not resolve to a
    ///   dictionary, when a depth limit (field-tree or reference-chain) is
    ///   exceeded, or when the target object-number space is exhausted. A direct
    ///   non-dictionary `/AcroForm` value is ignored, not rejected.
    /// - Any error propagated from [`copy_objects`] (for example a failed
    ///   [`Pdf::resolve`] on `source`).
    pub fn copy_fields_from<RS: Read + Seek>(
        &mut self,
        source: &mut Pdf<RS>,
    ) -> Result<Vec<ObjectRef>> {
        let (top_fields, inherited_entries, copy_set) = source_field_copy_set(source)?;
        if top_fields.is_empty() {
            return Ok(Vec::new());
        }

        let map = copy_objects(source, self.pdf, &copy_set)?;
        let copied_top: Vec<ObjectRef> = top_fields
            .iter()
            .filter_map(|field_ref| map.get(field_ref).copied())
            .collect();

        let acroform_ref = self.ensure_acroform_ref()?;
        let mut acroform = self.resolve_dict(acroform_ref, "AcroForm")?;
        let mut fields =
            resolve_array_value(self.pdf, acroform.get("Fields").cloned())?.unwrap_or_default();
        fields.extend(copied_top.iter().copied().map(Object::Reference));
        acroform.insert("Fields", Object::Array(fields));

        let mut source_da = None;
        let mut source_dr = None;
        for (key, value) in inherited_entries {
            let mapped = remap_refs_in_object(value, &map);
            match key.as_slice() {
                b"DA" => {
                    source_da = Some(mapped);
                }
                b"DR" => {
                    source_dr = Some(mapped);
                }
                _ => {}
            }
        }
        materialize_acroform_dr(&mut acroform, self.pdf)?;
        let font_renames = match source_dr {
            Some(dr) => {
                let dr = resolve_dictionary_object(self.pdf, dr)?;
                let dr = materialize_resource_categories_in_object(self.pdf, dr)?;
                merge_acroform_dr(&mut acroform, dr)
            }
            None => BTreeMap::new(),
        };
        let source_da = source_da.map(|da| rewrite_da_resource_names(da, &font_renames));
        if let Some(da) = source_da.clone() {
            if acroform.get("DA").is_none() {
                acroform.insert("DA", da);
            }
        }
        self.pdf
            .set_object(acroform_ref, Object::Dictionary(acroform));

        if let Some(da) = source_da {
            let mut seen = BTreeSet::new();
            for copied_ref in &copied_top {
                self.fix_field_appearance_inheritance(
                    *copied_ref,
                    &da,
                    &font_renames,
                    &mut seen,
                    0,
                )?;
            }
        } else if !font_renames.is_empty() {
            let mut seen = BTreeSet::new();
            for copied_ref in &copied_top {
                self.rewrite_field_da_resource_names(*copied_ref, &font_renames, &mut seen, 0)?;
            }
        }

        Ok(copied_top)
    }

    fn acroform_ref(&mut self) -> Result<Option<ObjectRef>> {
        let Some(root_ref) = self.pdf.root_ref() else {
            return Ok(None);
        };
        let catalog = self.resolve_dict(root_ref, "catalog")?;
        Ok(catalog.get_ref("AcroForm"))
    }

    fn acroform_dict(&mut self) -> Result<Option<Dictionary>> {
        let Some(root_ref) = self.pdf.root_ref() else {
            return Ok(None);
        };
        let catalog = self.resolve_dict(root_ref, "catalog")?;
        match catalog.get("AcroForm").cloned() {
            None | Some(Object::Null) => Ok(None),
            Some(Object::Dictionary(dict)) => Ok(Some(dict)),
            Some(Object::Reference(acroform_ref)) => {
                Ok(Some(self.resolve_dict(acroform_ref, "AcroForm")?))
            }
            Some(_) => Ok(None),
        }
    }

    pub(crate) fn ensure_acroform_ref(&mut self) -> Result<ObjectRef> {
        if let Some(existing_ref) = self.acroform_ref()? {
            return Ok(existing_ref);
        }

        let root_ref = self.pdf.root_ref().ok_or(Error::Missing("/Root"))?;
        let mut catalog = self.resolve_dict(root_ref, "catalog")?;
        let new_ref = self.next_object_ref()?;
        let acroform = match catalog.get("AcroForm").cloned() {
            Some(Object::Dictionary(dict)) => dict,
            _ => {
                let mut dict = Dictionary::new();
                dict.insert("Fields", Object::Array(Vec::new()));
                dict
            }
        };

        catalog.insert("AcroForm", Object::Reference(new_ref));
        self.pdf.set_object(new_ref, Object::Dictionary(acroform));
        self.pdf.set_object(root_ref, Object::Dictionary(catalog));
        Ok(new_ref)
    }

    fn next_object_ref(&self) -> Result<ObjectRef> {
        let next = self
            .pdf
            .object_refs()
            .iter()
            .map(|r| r.number)
            .max()
            .unwrap_or(0)
            .checked_add(1)
            .ok_or_else(|| Error::Unsupported("object-number space exhausted".to_string()))?;
        Ok(ObjectRef::new(next, 0))
    }

    fn resolve_dict(&mut self, object_ref: ObjectRef, label: &str) -> Result<Dictionary> {
        match self.pdf.resolve_borrowed(object_ref)? {
            Object::Dictionary(dict) => Ok(dict.clone()),
            _ => Err(Error::Unsupported(format!(
                "{label} object {object_ref} is not a dictionary"
            ))),
        }
    }

    fn resolve_field_dict(&mut self, field_ref: ObjectRef) -> Result<Dictionary> {
        self.resolve_dict(field_ref, "field")
    }

    fn walk_field_tree(
        &mut self,
        field_ref: ObjectRef,
        seen: &mut BTreeSet<ObjectRef>,
        out: &mut Vec<ObjectRef>,
    ) -> Result<()> {
        self.walk_field_tree_rec(field_ref, seen, out, 0)
    }

    fn walk_field_tree_rec(
        &mut self,
        field_ref: ObjectRef,
        seen: &mut BTreeSet<ObjectRef>,
        out: &mut Vec<ObjectRef>,
        depth: usize,
    ) -> Result<()> {
        if depth > DEFAULT_MAX_ACROFORM_DEPTH {
            return Err(Error::Unsupported(format!(
                "AcroForm field tree depth exceeds maximum of {DEFAULT_MAX_ACROFORM_DEPTH}"
            )));
        }
        if !seen.insert(field_ref) {
            return Ok(());
        }
        out.push(field_ref);

        let field = self.resolve_field_dict(field_ref)?;
        let Some(kids) = resolve_array_value(self.pdf, field.get("Kids").cloned())? else {
            return Ok(());
        };
        for kid in kids {
            if let Object::Reference(kid_ref) = kid {
                self.walk_field_tree_rec(kid_ref, seen, out, depth + 1)?;
            }
        }
        Ok(())
    }

    fn walk_field_info_tree(
        &mut self,
        field_ref: ObjectRef,
        inherited: FieldInheritance,
        seen: &mut BTreeSet<ObjectRef>,
        out: &mut Vec<AcroFormFieldInfo>,
        depth: usize,
    ) -> Result<()> {
        if depth > DEFAULT_MAX_ACROFORM_DEPTH {
            return Err(Error::Unsupported(format!(
                "AcroForm field tree depth exceeds maximum of {DEFAULT_MAX_ACROFORM_DEPTH}"
            )));
        }
        if !seen.insert(field_ref) {
            return Ok(());
        }

        let field = self.resolve_field_dict(field_ref)?;
        if is_pure_widget_annotation(&field) {
            return Ok(());
        }
        let current = inherited.apply(self.pdf, &field)?;
        let partial_name = deref_leaf(self.pdf, field.get("T").cloned())?
            .as_ref()
            .and_then(Object::as_string)
            .map(|name| name.to_vec());

        out.push(AcroFormFieldInfo {
            object_ref: field_ref,
            partial_name,
            full_name: current.full_name.clone(),
            field_type: current.field_type.clone(),
            value: current.value.clone(),
            default_value: current.default_value.clone(),
            field_flags: current.field_flags,
            default_appearance: current.default_appearance.clone(),
            quadding: current.quadding,
            max_len: current.max_len,
        });

        let Some(kids) = resolve_array_value(self.pdf, field.get("Kids").cloned())? else {
            return Ok(());
        };
        for kid in kids {
            if let Object::Reference(kid_ref) = kid {
                self.walk_field_info_tree(kid_ref, current.clone(), seen, out, depth + 1)?;
            }
        }
        Ok(())
    }

    fn fix_field_appearance_inheritance(
        &mut self,
        field_ref: ObjectRef,
        inherited_da: &Object,
        font_renames: &BTreeMap<Vec<u8>, Vec<u8>>,
        seen: &mut BTreeSet<ObjectRef>,
        depth: usize,
    ) -> Result<()> {
        if depth > DEFAULT_MAX_ACROFORM_DEPTH {
            return Err(Error::Unsupported(format!(
                "AcroForm field tree depth exceeds maximum of {DEFAULT_MAX_ACROFORM_DEPTH}"
            )));
        }
        if !seen.insert(field_ref) {
            return Ok(());
        }

        let mut field = self.resolve_field_dict(field_ref)?;
        let current_da = match field.get("DA").cloned() {
            Some(da) => {
                let rewritten = rewrite_da_resource_names(da, font_renames);
                if field.get("DA") != Some(&rewritten) {
                    field.insert("DA", rewritten.clone());
                    self.pdf
                        .set_object(field_ref, Object::Dictionary(field.clone()));
                }
                rewritten
            }
            None => {
                field.insert("DA", inherited_da.clone());
                self.pdf
                    .set_object(field_ref, Object::Dictionary(field.clone()));
                inherited_da.clone()
            }
        };

        let Some(kids) = resolve_array_value(self.pdf, field.get("Kids").cloned())? else {
            return Ok(());
        };
        for kid in kids {
            if let Object::Reference(kid_ref) = kid {
                self.fix_field_appearance_inheritance(
                    kid_ref,
                    &current_da,
                    font_renames,
                    seen,
                    depth + 1,
                )?;
            }
        }
        Ok(())
    }

    fn rewrite_field_da_resource_names(
        &mut self,
        field_ref: ObjectRef,
        font_renames: &BTreeMap<Vec<u8>, Vec<u8>>,
        seen: &mut BTreeSet<ObjectRef>,
        depth: usize,
    ) -> Result<()> {
        if depth > DEFAULT_MAX_ACROFORM_DEPTH {
            return Err(Error::Unsupported(format!(
                "AcroForm field tree depth exceeds maximum of {DEFAULT_MAX_ACROFORM_DEPTH}"
            )));
        }
        if !seen.insert(field_ref) {
            return Ok(());
        }

        let mut field = self.resolve_field_dict(field_ref)?;
        if let Some(da) = field.get("DA").cloned() {
            let rewritten = rewrite_da_resource_names(da, font_renames);
            if field.get("DA") != Some(&rewritten) {
                field.insert("DA", rewritten);
                self.pdf
                    .set_object(field_ref, Object::Dictionary(field.clone()));
            }
        }

        let Some(kids) = resolve_array_value(self.pdf, field.get("Kids").cloned())? else {
            return Ok(());
        };
        for kid in kids {
            if let Object::Reference(kid_ref) = kid {
                self.rewrite_field_da_resource_names(kid_ref, font_renames, seen, depth + 1)?;
            }
        }
        Ok(())
    }
}

impl FieldInheritance {
    fn apply<R: Read + Seek>(&self, pdf: &mut Pdf<R>, field: &Dictionary) -> Result<Self> {
        let partial_name = deref_leaf(pdf, field.get("T").cloned())?
            .as_ref()
            .and_then(Object::as_string)
            .map(decode_field_name);
        let full_name = match (self.full_name.is_empty(), partial_name.as_deref()) {
            (_, None) => self.full_name.clone(),
            (true, Some(name)) => name.to_string(),
            (false, Some(name)) => format!("{}.{}", self.full_name, name),
        };

        Ok(Self {
            full_name,
            field_type: inherited_name(pdf, field, "FT")?.or_else(|| self.field_type.clone()),
            value: inherited_object(pdf, field, "V")?.or_else(|| self.value.clone()),
            default_value: inherited_object(pdf, field, "DV")?
                .or_else(|| self.default_value.clone()),
            field_flags: inherited_integer(pdf, field, "Ff")?.or(self.field_flags),
            default_appearance: inherited_object(pdf, field, "DA")?
                .or_else(|| self.default_appearance.clone()),
            quadding: inherited_integer(pdf, field, "Q")?.or(self.quadding),
            max_len: inherited_integer(pdf, field, "MaxLen")?.or(self.max_len),
        })
    }
}

impl<R: Read + Seek> Pdf<R> {
    /// Return a high-level AcroForm helper for this document.
    pub fn acroform(&mut self) -> AcroFormDocumentHelper<'_, R> {
        AcroFormDocumentHelper::new(self)
    }
}

/// Resolve one level of indirection for a metadata leaf value. A resolved
/// `Object::Null` (freed/unknown ref) is treated as absent to match
/// `inherited_object`'s existing Null handling. A direct (non-reference)
/// value passes through unchanged, so this is a no-op for already-materialized
/// PDFs.
fn deref_leaf<R: Read + Seek>(pdf: &mut Pdf<R>, value: Option<Object>) -> Result<Option<Object>> {
    match value {
        Some(Object::Reference(object_ref)) => match pdf.resolve(object_ref)? {
            Object::Null => Ok(None),
            resolved => Ok(Some(resolved)),
        },
        other => Ok(other),
    }
}

fn inherited_object<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    field: &Dictionary,
    key: &str,
) -> Result<Option<Object>> {
    match deref_leaf(pdf, field.get(key).cloned())? {
        Some(Object::Null) | None => Ok(None),
        Some(value) => Ok(Some(value)),
    }
}

fn inherited_name<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    field: &Dictionary,
    key: &str,
) -> Result<Option<Vec<u8>>> {
    Ok(deref_leaf(pdf, field.get(key).cloned())?
        .as_ref()
        .and_then(Object::as_name)
        .map(|name| name.to_vec()))
}

fn inherited_integer<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    field: &Dictionary,
    key: &str,
) -> Result<Option<i64>> {
    Ok(deref_leaf(pdf, field.get(key).cloned())?
        .as_ref()
        .and_then(Object::as_integer))
}

fn is_pure_widget_annotation(field: &Dictionary) -> bool {
    let is_widget = matches!(
        field.get("Subtype"),
        Some(Object::Name(name)) if name.as_slice() == b"Widget"
    );
    let has_field_entries = field.get("T").is_some()
        || field.get("FT").is_some()
        || field.get("Kids").is_some()
        || field.get("V").is_some()
        || field.get("DV").is_some()
        || field.get("Ff").is_some()
        || field.get("TU").is_some()
        || field.get("TM").is_some()
        || field.get("DA").is_some()
        || field.get("Q").is_some()
        || field.get("MaxLen").is_some();

    is_widget && !has_field_entries
}

fn decode_field_name(name: &[u8]) -> String {
    decode_pdf_text_string(name).unwrap_or_else(|| String::from_utf8_lossy(name).into_owned())
}

fn source_field_copy_set<RS: Read + Seek>(
    source: &mut Pdf<RS>,
) -> Result<(Vec<ObjectRef>, AcroFormInheritedEntries, FieldCopySet)> {
    let mut helper = AcroFormDocumentHelper::new(source);
    let top_fields = helper.top_level_fields()?;
    let inherited_entries = helper.acroform_inherited_entries()?;
    let mut copy_set = BTreeSet::new();
    let mut seen = BTreeSet::new();
    for field_ref in &top_fields {
        // Field-tree walk: skip a widget's /P (its page back-pointer) so the
        // closure never pulls the page and its sibling tree into the copy set.
        collect_reachable_refs(helper.pdf, *field_ref, &mut copy_set, &mut seen, 0, true)?;
    }
    for (_, value) in &inherited_entries {
        // /DR and /DA are resource subtrees, not field-tree nodes: a resource may
        // be legitimately named /P (e.g. a /DA-referenced font), so collect /P
        // here rather than dropping it as a field-tree back-pointer. A well-formed
        // resource dict holds no field-tree back-pointers; the `seen` set and the
        // depth cap still bound traversal against cycles and long reference chains
        // (DoS) on hostile input.
        collect_refs_in_object(helper.pdf, value, &mut copy_set, &mut seen, 0, 0, false)?;
    }
    Ok((top_fields, inherited_entries, copy_set))
}

impl<'a, R: Read + Seek> AcroFormDocumentHelper<'a, R> {
    pub(crate) fn top_level_fields(&mut self) -> Result<Vec<ObjectRef>> {
        let Some(acroform) = self.acroform_dict()? else {
            return Ok(Vec::new());
        };
        let Some(fields) = resolve_array_value(self.pdf, acroform.get("Fields").cloned())? else {
            return Ok(Vec::new());
        };
        Ok(fields
            .into_iter()
            .filter_map(|item| match item {
                Object::Reference(field_ref) => Some(field_ref),
                _ => None,
            })
            .collect())
    }

    pub(crate) fn acroform_inherited_entries(&mut self) -> Result<Vec<(Vec<u8>, Object)>> {
        let Some(acroform) = self.acroform_dict()? else {
            return Ok(Vec::new());
        };
        Ok([b"DA".as_slice(), b"DR".as_slice()]
            .into_iter()
            .filter_map(|key| {
                acroform
                    .get(key)
                    .cloned()
                    .map(|value| (key.to_vec(), value))
            })
            .collect())
    }
}

pub(crate) fn collect_reachable_refs<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    object_ref: ObjectRef,
    out: &mut BTreeSet<ObjectRef>,
    seen: &mut BTreeSet<ObjectRef>,
    depth: usize,
    skip_parent_key: bool,
) -> Result<()> {
    // The `seen` cycle guard cannot stop a long *acyclic* indirect-reference chain
    // (obj1 -> obj2 -> ... -> objN), where recursion depth grows with the chain length.
    // Bound the reference chain to avoid stack overflow on hostile source PDFs. Two
    // independent recursion axes are bounded separately: the `depth` parameter bounds the
    // indirect-reference-hop axis (`DEFAULT_MAX_ACROFORM_DEPTH`), incremented once per
    // resolved reference; inline structural nesting within a single resolved object is
    // bounded by the `inline_depth`/`MAX_INLINE_DEPTH` axis (see `collect_refs_in_object`),
    // reset to 0 at each ref hop because a freshly resolved object starts a new inline walk.
    if depth > DEFAULT_MAX_ACROFORM_DEPTH {
        return Err(Error::Unsupported(format!(
            "AcroForm reference chain depth exceeds maximum of {DEFAULT_MAX_ACROFORM_DEPTH}"
        )));
    }
    if !seen.insert(object_ref) {
        return Ok(());
    }
    out.insert(object_ref);

    let obj = pdf.resolve(object_ref)?;
    collect_refs_in_object(pdf, &obj, out, seen, depth, 0, skip_parent_key)
}

pub(crate) fn collect_refs_in_object<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    obj: &Object,
    out: &mut BTreeSet<ObjectRef>,
    seen: &mut BTreeSet<ObjectRef>,
    depth: usize,
    inline_depth: usize,
    skip_parent_key: bool,
) -> Result<()> {
    if inline_depth > MAX_INLINE_DEPTH {
        return Err(Error::Unsupported(format!(
            "AcroForm: inline object nesting exceeds maximum of {MAX_INLINE_DEPTH}"
        )));
    }
    match obj {
        Object::Reference(object_ref) => {
            // Ref hop: bump the ref-hop axis; `collect_reachable_refs` resets
            // `inline_depth` to 0 for the freshly resolved object.
            collect_reachable_refs(pdf, *object_ref, out, seen, depth + 1, skip_parent_key)
        }
        Object::Array(items) => {
            for item in items {
                collect_refs_in_object(
                    pdf,
                    item,
                    out,
                    seen,
                    depth,
                    inline_depth + 1,
                    skip_parent_key,
                )?;
            }
            Ok(())
        }
        Object::Dictionary(dict) => collect_refs_in_dict(
            pdf,
            dict,
            out,
            seen,
            depth,
            inline_depth + 1,
            skip_parent_key,
        ),
        Object::Stream(stream) => collect_refs_in_dict(
            pdf,
            &stream.dict,
            out,
            seen,
            depth,
            inline_depth + 1,
            skip_parent_key,
        ),
        Object::Null
        | Object::Boolean(_)
        | Object::Integer(_)
        | Object::Real(_)
        | Object::RealLiteral { .. }
        | Object::Name(_)
        | Object::String(_) => Ok(()),
    }
}

fn collect_refs_in_dict<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    dict: &Dictionary,
    out: &mut BTreeSet<ObjectRef>,
    seen: &mut BTreeSet<ObjectRef>,
    depth: usize,
    inline_depth: usize,
    skip_parent_key: bool,
) -> Result<()> {
    for (key, value) in dict.iter() {
        // Skip /P while it is a page back-pointer (`skip_parent_key` tracks that
        // context; see the `next_skip_parent_key` derivation below). Inside
        // resource data /P is an ordinary resource name and must be collected.
        if skip_parent_key && key == b"P" {
            continue;
        }
        // /P is a page back-pointer throughout the annotation/field graph — field
        // and widget dicts, but also nested annotations reached via non-field keys
        // (e.g. a widget's /Popup, whose own /P points at a page). Keep skipping it
        // across that whole graph. It only becomes an ordinary resource name once
        // the walk crosses into resource data via /Resources (an appearance
        // stream's, page's, or XObject's resources — e.g. a font named /P), so the
        // skip is lifted there and stays off for that resource subtree. (The
        // inherited /DR /DA walk already enters with the skip off; see the call in
        // `source_field_copy_set`.)
        let next_skip_parent_key = skip_parent_key && key != b"Resources";
        // Forward the same `inline_depth`: the caller incremented it when
        // descending into this dict, and each value re-enters
        // `collect_refs_in_object` where the guard re-checks.
        collect_refs_in_object(
            pdf,
            value,
            out,
            seen,
            depth,
            inline_depth,
            next_skip_parent_key,
        )?;
    }
    Ok(())
}

fn resolve_array_value<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    value: Option<Object>,
) -> Result<Option<Vec<Object>>> {
    match value {
        None | Some(Object::Null) => Ok(None),
        Some(Object::Array(values)) => Ok(Some(values)),
        Some(value @ Object::Reference(_)) => {
            // The array carrier itself may be a holder chain (`/Fields 20 0 R →
            // 21 0 R → [..]`); follow it to the terminal so a doubled-indirect
            // carrier yields its array instead of being dropped as a non-array.
            // The terminal is returned by value, so the array moves out without
            // the prior `.clone()`.
            match resolve_ref_chain(pdf, &value)?.0 {
                Object::Array(values) => Ok(Some(values)),
                _ => Ok(None), // Null or non-array terminal
            }
        }
        Some(_) => Ok(None),
    }
}

fn resolve_dictionary_object<R: Read + Seek>(pdf: &mut Pdf<R>, obj: Object) -> Result<Object> {
    match obj {
        Object::Reference(object_ref) => match pdf.resolve_borrowed(object_ref)? {
            Object::Dictionary(dict) => Ok(Object::Dictionary(dict.clone())),
            _ => Ok(Object::Reference(object_ref)),
        },
        other => Ok(other),
    }
}

fn materialize_acroform_dr<R: Read + Seek>(
    acroform: &mut Dictionary,
    pdf: &mut Pdf<R>,
) -> Result<()> {
    let Some(dr) = acroform.get("DR").cloned() else {
        return Ok(());
    };
    let dr = resolve_dictionary_object(pdf, dr)?;
    acroform.insert("DR", materialize_resource_categories_in_object(pdf, dr)?);
    Ok(())
}

pub(crate) fn remap_refs_in_object(obj: Object, map: &BTreeMap<ObjectRef, ObjectRef>) -> Object {
    match obj {
        Object::Reference(object_ref) => map
            .get(&object_ref)
            .copied()
            .map(Object::Reference)
            .unwrap_or(Object::Null),
        Object::Array(items) => Object::Array(
            items
                .into_iter()
                .map(|item| remap_refs_in_object(item, map))
                .collect(),
        ),
        Object::Dictionary(dict) => Object::Dictionary(remap_refs_in_dict(dict, map)),
        Object::Stream(mut stream) => {
            stream.dict = remap_refs_in_dict(stream.dict, map);
            Object::Stream(stream)
        }
        Object::Null
        | Object::Boolean(_)
        | Object::Integer(_)
        | Object::Real(_)
        | Object::RealLiteral { .. }
        | Object::Name(_)
        | Object::String(_) => obj,
    }
}

fn remap_refs_in_dict(dict: Dictionary, map: &BTreeMap<ObjectRef, ObjectRef>) -> Dictionary {
    let mut out = Dictionary::new();
    for (key, value) in dict.iter() {
        out.insert(key, remap_refs_in_object(value.clone(), map));
    }
    out
}

fn merge_acroform_dr(acroform: &mut Dictionary, source_dr: Object) -> BTreeMap<Vec<u8>, Vec<u8>> {
    match acroform.remove("DR") {
        None | Some(Object::Null) => {
            acroform.insert("DR", source_dr);
            BTreeMap::new()
        }
        Some(Object::Dictionary(target_dr)) => {
            if let Object::Dictionary(source_dr) = source_dr {
                let (merged, renames) = merge_resource_dicts(target_dr, source_dr);
                acroform.insert("DR", Object::Dictionary(merged));
                renames
            } else {
                acroform.insert("DR", Object::Dictionary(target_dr));
                BTreeMap::new()
            }
        }
        Some(existing) => {
            acroform.insert("DR", existing);
            BTreeMap::new()
        }
    }
}

fn materialize_resource_categories_in_object<R: Read + Seek>(
    pdf: &mut Pdf<R>,
    dr: Object,
) -> Result<Object> {
    match dr {
        Object::Dictionary(mut dict) => {
            materialize_resource_categories(&mut dict, pdf)?;
            Ok(Object::Dictionary(dict))
        }
        other => Ok(other),
    }
}

fn materialize_resource_categories<R: Read + Seek>(
    dr: &mut Dictionary,
    pdf: &mut Pdf<R>,
) -> Result<()> {
    let categories: Vec<Vec<u8>> = dr.iter().map(|(key, _)| key.to_vec()).collect();
    for category in categories {
        let Some(value) = dr.get(&category).cloned() else {
            continue;
        };
        dr.insert(&category, resolve_dictionary_object(pdf, value)?);
    }
    Ok(())
}

fn merge_resource_dicts(
    mut target: Dictionary,
    source: Dictionary,
) -> (Dictionary, BTreeMap<Vec<u8>, Vec<u8>>) {
    let mut font_renames = BTreeMap::new();
    for (category, source_value) in source.iter() {
        match (target.remove(category), source_value) {
            (None, _) => target.insert(category, source_value.clone()),
            (Some(Object::Dictionary(target_category)), Object::Dictionary(source_category)) => {
                let (merged, renames) =
                    merge_resource_category(target_category, source_category, category == b"Font");
                if category == b"Font" {
                    font_renames.extend(renames);
                }
                target.insert(category, Object::Dictionary(merged));
            }
            (Some(existing), _) => target.insert(category, existing),
        }
    }
    (target, font_renames)
}

fn merge_resource_category(
    mut target: Dictionary,
    source: &Dictionary,
    rename_conflicts: bool,
) -> (Dictionary, BTreeMap<Vec<u8>, Vec<u8>>) {
    let mut renames = BTreeMap::new();
    for (name, value) in source.iter() {
        match target.get(name) {
            None => target.insert(name, value.clone()),
            Some(existing) if existing == value => {}
            Some(_) if rename_conflicts => {
                let renamed = unique_resource_name(name, &target);
                target.insert(&renamed, value.clone());
                renames.insert(name.to_vec(), renamed);
            }
            Some(_) => {}
        }
    }
    (target, renames)
}

fn unique_resource_name(base: &[u8], existing: &Dictionary) -> Vec<u8> {
    let mut candidate = [base, b"_flpdf"].concat();
    let mut suffix = 2u32;
    while existing.get(&candidate).is_some() {
        candidate = [base, b"_flpdf", suffix.to_string().as_bytes()].concat();
        suffix += 1;
    }
    candidate
}

fn rewrite_da_resource_names(da: Object, renames: &BTreeMap<Vec<u8>, Vec<u8>>) -> Object {
    if renames.is_empty() {
        return da;
    }
    match da {
        Object::String(bytes) => Object::String(rewrite_pdf_name_tokens(&bytes, renames)),
        other => other,
    }
}

fn rewrite_pdf_name_tokens(bytes: &[u8], renames: &BTreeMap<Vec<u8>, Vec<u8>>) -> Vec<u8> {
    let mut out = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] != b'/' {
            out.push(bytes[i]);
            i += 1;
            continue;
        }

        let start = i + 1;
        let mut end = start;
        while end < bytes.len() && !is_pdf_name_delimiter(bytes[end]) {
            end += 1;
        }
        out.push(b'/');
        if let Some(renamed) = renames.get(&bytes[start..end]) {
            out.extend_from_slice(renamed);
        } else {
            out.extend_from_slice(&bytes[start..end]);
        }
        i = end;
    }
    out
}

fn is_pdf_name_delimiter(byte: u8) -> bool {
    byte.is_ascii_whitespace()
        || matches!(
            byte,
            b'(' | b')' | b'<' | b'>' | b'[' | b']' | b'{' | b'}' | b'/' | b'%'
        )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::object::{Stream, MAX_INLINE_DEPTH};

    fn dict(entries: &[(&str, Object)]) -> Dictionary {
        let mut dict = Dictionary::new();
        for (key, value) in entries {
            dict.insert(*key, value.clone());
        }
        dict
    }

    /// Build `depth` levels of single-element arrays wrapping `Object::Null`.
    /// Contains no `Reference`, so walking it never reaches the resolve path.
    fn nested_arrays(depth: usize) -> Object {
        let mut o = Object::Null;
        for _ in 0..depth {
            o = Object::Array(vec![o]);
        }
        o
    }

    /// Minimal valid `Pdf` for tests that walk a `Reference`-free object. The
    /// `pdf` argument is required by the walker signature but is never touched
    /// because pure inline structure never reaches `pdf.resolve`.
    fn minimal_pdf() -> Pdf<std::io::Cursor<Vec<u8>>> {
        let bytes = include_bytes!("../../../tests/fixtures/compat/one-page.pdf");
        Pdf::open_mem_owned(bytes.to_vec()).expect("open")
    }

    #[test]
    fn collect_refs_in_object_errors_on_excessive_inline_nesting() {
        let mut pdf = minimal_pdf();
        let mut out = BTreeSet::new();
        let mut seen = BTreeSet::new();
        let deep = nested_arrays(MAX_INLINE_DEPTH + 5);
        // arg order: (pdf, obj, out, seen, depth, inline_depth, skip_parent_key)
        let err = collect_refs_in_object(&mut pdf, &deep, &mut out, &mut seen, 0, 0, true);
        assert!(matches!(err, Err(crate::Error::Unsupported(_))));
    }

    #[test]
    fn collect_refs_in_object_accepts_inline_nesting_within_limit() {
        let mut pdf = minimal_pdf();
        let mut out = BTreeSet::new();
        let mut seen = BTreeSet::new();
        // Null leaf sits at inline_depth = MAX_INLINE_DEPTH, the deepest level
        // accepted under the strict `>` guard.
        let deep = nested_arrays(MAX_INLINE_DEPTH);
        collect_refs_in_object(&mut pdf, &deep, &mut out, &mut seen, 0, 0, true).unwrap();
        assert!(out.is_empty());
    }

    #[test]
    fn collect_refs_in_object_walks_dict_and_stream_arms_within_limit() {
        let mut pdf = minimal_pdf();
        let mut out = BTreeSet::new();
        let mut seen = BTreeSet::new();
        // Shallow object exercising the Dictionary and Stream arms (no Reference,
        // so the resolve path is never hit and `pdf` stays unused by the walk).
        let stream = Object::Stream(Stream::new(
            dict(&[("Length", Object::Integer(0))]),
            Vec::new(),
        ));
        let obj = Object::Dictionary(dict(&[("S", stream), ("N", Object::Integer(1))]));
        collect_refs_in_object(&mut pdf, &obj, &mut out, &mut seen, 0, 0, true).unwrap();
        assert!(out.is_empty());
    }

    #[test]
    fn merge_acroform_dr_keeps_existing_when_source_is_not_a_dictionary() {
        let target_dr = Object::Dictionary(dict(&[(
            "Font",
            Object::Dictionary(dict(&[("Helv", Object::Integer(1))])),
        )]));
        let mut acroform = dict(&[("DR", target_dr.clone())]);

        let renames = merge_acroform_dr(&mut acroform, Object::Name(b"Bad".to_vec()));

        assert!(renames.is_empty());
        assert_eq!(acroform.get("DR"), Some(&target_dr));
    }

    #[test]
    fn merge_acroform_dr_preserves_non_dictionary_target() {
        let existing = Object::Name(b"Bad".to_vec());
        let mut acroform = dict(&[("DR", existing.clone())]);
        let source_dr = Object::Dictionary(dict(&[(
            "Font",
            Object::Dictionary(dict(&[("Helv", Object::Integer(1))])),
        )]));

        let renames = merge_acroform_dr(&mut acroform, source_dr);

        assert!(renames.is_empty());
        assert_eq!(acroform.get("DR"), Some(&existing));
    }

    #[test]
    fn merge_acroform_dr_inserts_source_when_target_is_missing_or_null() {
        for initial in [None, Some(Object::Null)] {
            let mut acroform = Dictionary::new();
            if let Some(value) = initial {
                acroform.insert("DR", value);
            }
            let source_dr = Object::Dictionary(dict(&[(
                "Font",
                Object::Dictionary(dict(&[("Helv", Object::Integer(1))])),
            )]));

            let renames = merge_acroform_dr(&mut acroform, source_dr.clone());

            assert!(renames.is_empty());
            assert_eq!(acroform.get("DR"), Some(&source_dr));
        }
    }

    #[test]
    fn merge_resource_dicts_keeps_target_non_dictionary_categories() {
        let target = dict(&[("Font", Object::Name(b"Existing".to_vec()))]);
        let source = dict(&[(
            "Font",
            Object::Dictionary(dict(&[("Helv", Object::Integer(1))])),
        )]);

        let (merged, renames) = merge_resource_dicts(target, source);

        assert!(renames.is_empty());
        assert_eq!(
            merged.get("Font"),
            Some(&Object::Name(b"Existing".to_vec()))
        );
    }

    #[test]
    fn merge_resource_category_skips_non_font_conflicts() {
        let target = dict(&[("Img", Object::Integer(1))]);
        let source = dict(&[("Img", Object::Integer(2))]);

        let (merged, renames) = merge_resource_category(target, &source, false);

        assert!(renames.is_empty());
        assert_eq!(merged.get("Img"), Some(&Object::Integer(1)));
    }

    #[test]
    fn unique_resource_name_uses_numeric_suffix_after_first_conflict() {
        let existing = dict(&[
            ("Helv_flpdf", Object::Integer(1)),
            ("Helv_flpdf2", Object::Integer(2)),
        ]);

        assert_eq!(unique_resource_name(b"Helv", &existing), b"Helv_flpdf3");
    }

    #[test]
    fn rewrite_da_resource_names_handles_non_strings_and_unmapped_names() {
        let mut renames = BTreeMap::new();
        renames.insert(b"Helv".to_vec(), b"Helv_flpdf".to_vec());

        assert_eq!(
            rewrite_da_resource_names(Object::Name(b"DA".to_vec()), &renames),
            Object::Name(b"DA".to_vec())
        );
        assert_eq!(
            rewrite_da_resource_names(
                Object::String(b"/Other 9 Tf /Helv2 10 Tf".to_vec()),
                &renames
            ),
            Object::String(b"/Other 9 Tf /Helv2 10 Tf".to_vec())
        );
    }
}