Skip to main content

hayro_syntax/object/
dict.rs

1//! Dictionaries.
2
3use crate::object::macros::object;
4use crate::object::r#ref::{MaybeRef, ObjRef};
5use crate::object::{Name, ObjectIdentifier};
6use crate::object::{Object, ObjectLike};
7use crate::reader::Reader;
8use crate::reader::{Readable, ReaderContext, ReaderExt, Skippable};
9use crate::sync::Arc;
10use crate::sync::FxHashMap;
11use alloc::format;
12use alloc::vec::Vec;
13use core::fmt::{Debug, Formatter};
14use core::ops::Deref;
15
16/// A dictionary, which is a key-value map, keys being names, and values being any PDF object or
17/// objetc reference.
18#[derive(Clone)]
19pub struct Dict<'a>(Inner<'a>);
20
21#[derive(Clone)]
22enum Inner<'a> {
23    Empty,
24    Some(Arc<Repr<'a>>),
25}
26
27impl Default for Dict<'_> {
28    fn default() -> Self {
29        Self::empty()
30    }
31}
32
33// Note that this is not structural equality, i.e. two dictionaries with the same
34// items are still considered different if they have different whitespaces.
35impl PartialEq for Dict<'_> {
36    fn eq(&self, other: &Self) -> bool {
37        self.data() == other.data()
38    }
39}
40
41impl<'a> Dict<'a> {
42    /// Create a new empty dictionary.
43    pub fn empty() -> Self {
44        Self(Inner::Empty)
45    }
46
47    /// Get the raw bytes underlying to the dictionary.
48    pub fn data(&self) -> &'a [u8] {
49        match &self.0 {
50            Inner::Empty => &[],
51            Inner::Some(repr) => repr.data,
52        }
53    }
54
55    /// Returns the number of entries in the dictionary.
56    pub fn len(&self) -> usize {
57        self.offsets().map_or(0, FxHashMap::len)
58    }
59
60    /// Return whether the dictionary is empty.
61    pub fn is_empty(&self) -> bool {
62        self.offsets().is_none_or(FxHashMap::is_empty)
63    }
64
65    /// Checks whether the dictionary contains an entry with a specific key.
66    pub fn contains_key(&self, key: impl AsRef<[u8]>) -> bool {
67        self.offsets()
68            .is_some_and(|offsets| offsets.contains_key(key.as_ref()))
69    }
70
71    /// Returns the entry of a key as a specific object, or try to resolve it in case it's
72    /// an object reference.
73    #[allow(
74        private_bounds,
75        reason = "users shouldn't be able to implement `ObjectLike` for custom objects."
76    )]
77    pub fn get<T>(&self, key: impl AsRef<[u8]>) -> Option<T>
78    where
79        T: ObjectLike<'a>,
80    {
81        self.get_raw::<T>(key.as_ref())?.resolve(self.ctx())
82    }
83
84    /// Get the object reference linked to a key.
85    pub fn get_ref(&self, key: impl AsRef<[u8]>) -> Option<ObjRef> {
86        let offset = *self.offsets()?.get(key.as_ref())?;
87
88        Reader::new(&self.data()[offset..]).read_with_context::<ObjRef>(self.ctx())
89    }
90
91    /// Returns an iterator over all keys in the dictionary.
92    pub fn keys(&self) -> impl Iterator<Item = Name<'a>> + '_ {
93        self.offsets()
94            .into_iter()
95            .flat_map(|offsets| offsets.keys().cloned())
96    }
97
98    /// An iterator over all entries in the dictionary, sorted by key.
99    pub fn entries(&self) -> impl Iterator<Item = (Name<'a>, MaybeRef<Object<'a>>)> + '_ {
100        let mut sorted_keys = self.keys().collect::<Vec<_>>();
101        sorted_keys.sort_by(|n1, n2| n1.as_ref().cmp(n2.as_ref()));
102        sorted_keys.into_iter().map(|k| {
103            let obj = self.get_raw(k.deref()).unwrap();
104            (k, obj)
105        })
106    }
107
108    /// Return the object identifier of the dict, if it's an indirect object.
109    pub fn obj_id(&self) -> Option<ObjectIdentifier> {
110        self.ctx().obj_number()
111    }
112
113    /// Return the raw entry for a specific key.
114    #[allow(private_bounds)]
115    pub fn get_raw<T>(&self, key: impl AsRef<[u8]>) -> Option<MaybeRef<T>>
116    where
117        T: Readable<'a>,
118    {
119        let offset = *self.offsets()?.get(key.as_ref())?;
120
121        Reader::new(&self.data()[offset..]).read_with_context::<MaybeRef<T>>(self.ctx())
122    }
123
124    pub(crate) fn ctx(&self) -> &ReaderContext<'a> {
125        match &self.0 {
126            Inner::Empty => ReaderContext::dummy_ref(),
127            Inner::Some(repr) => &repr.ctx,
128        }
129    }
130
131    fn offsets(&self) -> Option<&FxHashMap<Name<'a>, usize>> {
132        match &self.0 {
133            Inner::Empty => None,
134            Inner::Some(repr) => Some(&repr.offsets),
135        }
136    }
137}
138
139impl Debug for Dict<'_> {
140    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
141        let mut debug_struct = f.debug_struct("Dict");
142
143        if let Some(offsets) = self.offsets() {
144            let mut r = Reader::new(self.data());
145
146            for (key, val) in offsets {
147                r.jump(*val);
148                debug_struct.field(
149                    &format!("{:?}", key.as_str()),
150                    &r.read_with_context::<MaybeRef<Object<'_>>>(&ReaderContext::dummy())
151                        .unwrap(),
152                );
153            }
154        }
155        Ok(())
156    }
157}
158
159impl Skippable for Dict<'_> {
160    fn skip(r: &mut Reader<'_>, is_content_stream: bool) -> Option<()> {
161        r.forward_tag(b"<<")?;
162
163        loop {
164            r.skip_white_spaces_and_comments();
165
166            if let Some(()) = r.forward_tag(b">>") {
167                break Some(());
168            } else {
169                let Some(_) = r.skip::<Name<'_>>(is_content_stream) else {
170                    // In case there is garbage in-between, be lenient and just try to skip it.
171                    r.skip::<Object<'_>>(is_content_stream)?;
172                    continue;
173                };
174
175                r.skip_white_spaces_and_comments();
176
177                if is_content_stream {
178                    r.skip::<Object<'_>>(is_content_stream)?;
179                } else {
180                    r.skip::<MaybeRef<Object<'_>>>(is_content_stream)?;
181                }
182            }
183        }
184    }
185}
186
187impl<'a> Readable<'a> for Dict<'a> {
188    fn read(r: &mut Reader<'a>, ctx: &ReaderContext<'a>) -> Option<Self> {
189        read_inner(r, ctx, Some(b"<<"), b">>")
190    }
191}
192
193fn read_inner<'a>(
194    r: &mut Reader<'a>,
195    ctx: &ReaderContext<'a>,
196    start_tag: Option<&[u8]>,
197    end_tag: &[u8],
198) -> Option<Dict<'a>> {
199    // TODO: Figure out how to
200    // 1) Make dictionaries easily cloneable without wrapping in Arc.
201    // 2) Maybe have an efficient per-document allocator pool for hashmaps
202    // that can be reused.
203    #[cfg(feature = "std")]
204    let mut offsets = FxHashMap::with_capacity_and_hasher(8, rustc_hash::FxBuildHasher);
205    #[cfg(not(feature = "std"))]
206    let mut offsets = FxHashMap::new();
207
208    let data = parse_dict_with(
209        r,
210        ctx,
211        start_tag,
212        end_tag,
213        #[inline]
214        |name, offset, _| {
215            offsets.insert(name, offset);
216            Some(())
217        },
218    )?;
219
220    Some(Dict(Inner::Some(Arc::new(Repr {
221        data,
222        offsets,
223        ctx: ctx.clone(),
224    }))))
225}
226
227pub(crate) struct DictProbe<'a> {
228    pub(crate) data: &'a [u8],
229    pub(crate) has_root: bool,
230    pub(crate) has_type: bool,
231}
232
233// We use that method during fallback xref parsing, so that we can search for
234// the dictionaries we need without allocating the whole hash map.
235// If the above TODOs are ever resolved, we can probably remove this method.
236pub(crate) fn probe_dict<'a>(
237    r: &mut Reader<'a>,
238    ctx: &ReaderContext<'a>,
239    start_tag: Option<&[u8]>,
240    end_tag: &[u8],
241) -> Option<DictProbe<'a>> {
242    let mut has_root = false;
243    let mut has_type = false;
244
245    let data = parse_dict_with(
246        r,
247        ctx,
248        start_tag,
249        end_tag,
250        #[inline]
251        |name, _, _value_reader| {
252            if name.as_ref() == keys::ROOT {
253                has_root = true;
254            } else if name.as_ref() == keys::TYPE {
255                has_type = true;
256            }
257
258            Some(())
259        },
260    )?;
261
262    Some(DictProbe {
263        data,
264        has_root,
265        has_type,
266    })
267}
268
269fn parse_dict_with<'a, F>(
270    r: &mut Reader<'a>,
271    ctx: &ReaderContext<'a>,
272    start_tag: Option<&[u8]>,
273    end_tag: &[u8],
274    mut on_entry: F,
275) -> Option<&'a [u8]>
276where
277    F: FnMut(Name<'a>, usize, &Reader<'a>) -> Option<()>,
278{
279    let dict_data = r.tail()?;
280    let start_offset = r.offset();
281
282    // Inline image dictionaries don't start with '<<'.
283    if let Some(start_tag) = start_tag {
284        r.forward_tag(start_tag)?;
285    }
286
287    loop {
288        r.skip_white_spaces_and_comments();
289
290        // Normal dictionaries end with '>>', inline image dictionaries end with BD.
291        if let Some(()) = r.peek_tag(end_tag) {
292            r.forward_tag(end_tag)?;
293            let end_offset = r.offset() - start_offset;
294
295            break Some(&dict_data[..end_offset]);
296        } else {
297            let Some(name) = r.read_without_context::<Name<'_>>() else {
298                if start_tag.is_some() {
299                    // In case there is garbage in-between, be lenient and just try to skip it.
300                    // But only do this if we are parsing a proper dictionary as opposed to an
301                    // inline dictionary.
302                    r.read::<Object<'_>>(ctx)?;
303                    continue;
304                } else {
305                    return None;
306                }
307            };
308            r.skip_white_spaces_and_comments();
309
310            let offset = r.offset() - start_offset;
311            // Do note that we are including objects in our dictionary even if they
312            // are the null object, meaning that a call to `contains_key` will return `true`
313            // even if the object is the null object. The PDF reference in theory requires
314            // us to treat them as non-existing. Previously, we included a check to determine
315            // whether the object is `null` before inserting it, but that caused problems
316            // in some test cases where encryption + object streams are involved (as we would
317            // attempt to read an object stream before having resolved the encryption dictionary).
318            on_entry(name, offset, r)?;
319
320            if ctx.in_content_stream() {
321                r.skip::<Object<'_>>(ctx.in_content_stream())?;
322            } else {
323                r.skip::<MaybeRef<Object<'_>>>(ctx.in_content_stream())?;
324            }
325        }
326    }
327}
328
329object!(Dict<'a>, Dict);
330
331struct Repr<'a> {
332    data: &'a [u8],
333    offsets: FxHashMap<Name<'a>, usize>,
334    ctx: ReaderContext<'a>,
335}
336
337pub(crate) struct InlineImageDict<'a>(Dict<'a>);
338
339impl<'a> InlineImageDict<'a> {
340    pub(crate) fn get_dict(&self) -> &Dict<'a> {
341        &self.0
342    }
343}
344
345impl<'a> Readable<'a> for InlineImageDict<'a> {
346    fn read(r: &mut Reader<'a>, ctx: &ReaderContext<'a>) -> Option<Self> {
347        Some(Self(read_inner(r, ctx, None, b"ID")?))
348    }
349}
350
351/// A collection of possible keys in a PDF dictionary. Copied and adapted from `PDFBox`.
352#[allow(missing_docs)]
353pub mod keys {
354    macro_rules! key {
355        ($i:ident, $e:expr) => {
356            pub const $i: &'static [u8] = $e;
357        };
358    }
359
360    // A
361    key!(A, b"A");
362    key!(AA, b"AA");
363    key!(ABSOLUTE_COLORIMETRIC, b"AbsoluteColorimetric");
364    key!(AC, b"AC");
365    key!(ACRO_FORM, b"AcroForm");
366    key!(ACTUAL_TEXT, b"ActualText");
367    key!(ADBE, b"ADBE");
368    key!(ADBE_PKCS7_DETACHED, b"adbe.pkcs7.detached");
369    key!(ADBE_PKCS7_SHA1, b"adbe.pkcs7.sha1");
370    key!(ADBE_X509_RSA_SHA1, b"adbe.x509.rsa_sha1");
371    key!(ADOBE_PPKLITE, b"Adobe.PPKLite");
372    key!(AESV2, b"AESV2");
373    key!(AESV3, b"AESV3");
374    key!(AF, b"AF");
375    key!(AF_RELATIONSHIP, b"AFRelationship");
376    key!(AFTER, b"After");
377    key!(AI_META_DATA, b"AIMetaData");
378    key!(AIS, b"AIS");
379    key!(ALL_OFF, b"AllOff");
380    key!(ALL_ON, b"AllOn");
381    key!(ALT, b"Alt");
382    key!(ALPHA, b"Alpha");
383    key!(ALTERNATE, b"Alternate");
384    key!(ANNOT, b"Annot");
385    key!(ANNOTS, b"Annots");
386    key!(ANTI_ALIAS, b"AntiAlias");
387    key!(ANY_OFF, b"AnyOff");
388    key!(ANY_ON, b"AnyOn");
389    key!(AP, b"AP");
390    key!(AP_REF, b"APRef");
391    key!(APP, b"App");
392    key!(ART_BOX, b"ArtBox");
393    key!(ARTIFACT, b"Artifact");
394    key!(AS, b"AS");
395    key!(ASCENT, b"Ascent");
396    key!(ASCII_HEX_DECODE, b"ASCIIHexDecode");
397    key!(ASCII_HEX_DECODE_ABBREVIATION, b"AHx");
398    key!(ASCII85_DECODE, b"ASCII85Decode");
399    key!(ASCII85_DECODE_ABBREVIATION, b"A85");
400    key!(ATTACHED, b"Attached");
401    key!(AUTHOR, b"Author");
402    key!(AVG_WIDTH, b"AvgWidth");
403
404    // B
405    key!(B, b"B");
406    key!(BACKGROUND, b"Background");
407    key!(BASE_ENCODING, b"BaseEncoding");
408    key!(BASE_FONT, b"BaseFont");
409    key!(BASE_STATE, b"BaseState");
410    key!(BASE_VERSION, b"BaseVersion");
411    key!(BBOX, b"BBox");
412    key!(BC, b"BC");
413    key!(BE, b"BE");
414    key!(BEAD, b"BEAD");
415    key!(BEFORE, b"Before");
416    key!(BG, b"BG");
417    key!(BITS_PER_COMPONENT, b"BitsPerComponent");
418    key!(BITS_PER_COORDINATE, b"BitsPerCoordinate");
419    key!(BITS_PER_FLAG, b"BitsPerFlag");
420    key!(BITS_PER_SAMPLE, b"BitsPerSample");
421    key!(BL, b"Bl");
422    key!(BLACK_IS_1, b"BlackIs1");
423    key!(BLACK_POINT, b"BlackPoint");
424    key!(BLEED_BOX, b"BleedBox");
425    key!(BM, b"BM");
426    key!(BORDER, b"Border");
427    key!(BOUNDS, b"Bounds");
428    key!(BPC, b"BPC");
429    key!(BS, b"BS");
430    key!(BTN, b"Btn");
431    key!(BYTERANGE, b"ByteRange");
432
433    // C
434    key!(C, b"C");
435    key!(C0, b"C0");
436    key!(C1, b"C1");
437    key!(CA, b"CA");
438    key!(CA_NS, b"ca");
439    key!(CALGRAY, b"CalGray");
440    key!(CALRGB, b"CalRGB");
441    key!(CALCMYK, b"CalCMYK");
442    key!(CAP, b"Cap");
443    key!(CAP_HEIGHT, b"CapHeight");
444    key!(CATALOG, b"Catalog");
445    key!(CCITTFAX_DECODE, b"CCITTFaxDecode");
446    key!(CCITTFAX_DECODE_ABBREVIATION, b"CCF");
447    key!(CENTER_WINDOW, b"CenterWindow");
448    key!(CERT, b"Cert");
449    key!(CERTS, b"Certs");
450    key!(CF, b"CF");
451    key!(CFM, b"CFM");
452    key!(CH, b"Ch");
453    key!(CHAR_PROCS, b"CharProcs");
454    key!(CHAR_SET, b"CharSet");
455    key!(CHECK_SUM, b"CheckSum");
456    key!(CI, b"CI");
457    key!(CICI_SIGNIT, b"CICI.SignIt");
458    key!(CID_FONT_TYPE0, b"CIDFontType0");
459    key!(CID_FONT_TYPE0C, b"CIDFontType0C");
460    key!(CID_FONT_TYPE2, b"CIDFontType2");
461    key!(CID_TO_GID_MAP, b"CIDToGIDMap");
462    key!(CID_SET, b"CIDSet");
463    key!(CIDSYSTEMINFO, b"CIDSystemInfo");
464    key!(CL, b"CL");
465    key!(CLASS_MAP, b"ClassMap");
466    key!(CLR_F, b"ClrF");
467    key!(CLR_FF, b"ClrFf");
468    key!(CMAP, b"CMap");
469    key!(CMAPNAME, b"CMapName");
470    key!(CMYK, b"CMYK");
471    key!(CO, b"CO");
472    key!(COLOR, b"Color");
473    key!(COLLECTION, b"Collection");
474    key!(COLLECTION_ITEM, b"CollectionItem");
475    key!(COLLECTION_FIELD, b"CollectionField");
476    key!(COLLECTION_SCHEMA, b"CollectionSchema");
477    key!(COLLECTION_SORT, b"CollectionSort");
478    key!(COLLECTION_SUBITEM, b"CollectionSubitem");
479    key!(COLOR_BURN, b"ColorBurn");
480    key!(COLOR_DODGE, b"ColorDodge");
481    key!(COLORANTS, b"Colorants");
482    key!(COLORS, b"Colors");
483    key!(COLORSPACE, b"ColorSpace");
484    key!(COLOR_TRANSFORM, b"ColorTransform");
485    key!(COLUMNS, b"Columns");
486    key!(COMPATIBLE, b"Compatible");
487    key!(COMPONENTS, b"Components");
488    key!(CONTACT_INFO, b"ContactInfo");
489    key!(CONTENTS, b"Contents");
490    key!(COORDS, b"Coords");
491    key!(COUNT, b"Count");
492    key!(CP, b"CP");
493    key!(CREATION_DATE, b"CreationDate");
494    key!(CREATOR, b"Creator");
495    key!(CRL, b"CRL");
496    key!(CRLS, b"CRLS");
497    key!(CROP_BOX, b"CropBox");
498    key!(CRYPT, b"Crypt");
499    key!(CS, b"CS");
500    key!(CYX, b"CYX");
501
502    // D
503    key!(D, b"D");
504    key!(DA, b"DA");
505    key!(DARKEN, b"Darken");
506    key!(DATE, b"Date");
507    key!(DCT_DECODE, b"DCTDecode");
508    key!(DCT_DECODE_ABBREVIATION, b"DCT");
509    key!(DECODE, b"Decode");
510    key!(DECODE_PARMS, b"DecodeParms");
511    key!(DEFAULT, b"default");
512    key!(DEFAULT_CMYK, b"DefaultCMYK");
513    key!(DEFAULT_CRYPT_FILTER, b"DefaultCryptFilter");
514    key!(DEFAULT_GRAY, b"DefaultGray");
515    key!(DEFAULT_RGB, b"DefaultRGB");
516    key!(DESC, b"Desc");
517    key!(DESCENDANT_FONTS, b"DescendantFonts");
518    key!(DESCENT, b"Descent");
519    key!(DEST, b"Dest");
520    key!(DEST_OUTPUT_PROFILE, b"DestOutputProfile");
521    key!(DESTS, b"Dests");
522    key!(DEVICE_CMYK, b"DeviceCMYK");
523    key!(DEVICE_GRAY, b"DeviceGray");
524    key!(DEVICE_N, b"DeviceN");
525    key!(DEVICE_RGB, b"DeviceRGB");
526    key!(DI, b"Di");
527    key!(DIFFERENCE, b"Difference");
528    key!(DIFFERENCES, b"Differences");
529    key!(DIGEST_METHOD, b"DigestMethod");
530    key!(DIGEST_RIPEMD160, b"RIPEMD160");
531    key!(DIGEST_SHA1, b"SHA1");
532    key!(DIGEST_SHA256, b"SHA256");
533    key!(DIGEST_SHA384, b"SHA384");
534    key!(DIGEST_SHA512, b"SHA512");
535    key!(DIRECTION, b"Direction");
536    key!(DISPLAY_DOC_TITLE, b"DisplayDocTitle");
537    key!(DL, b"DL");
538    key!(DM, b"Dm");
539    key!(DOC, b"Doc");
540    key!(DOC_CHECKSUM, b"DocChecksum");
541    key!(DOC_TIME_STAMP, b"DocTimeStamp");
542    key!(DOCMDP, b"DocMDP");
543    key!(DOCUMENT, b"Document");
544    key!(DOMAIN, b"Domain");
545    key!(DOS, b"DOS");
546    key!(DP, b"DP");
547    key!(DR, b"DR");
548    key!(DS, b"DS");
549    key!(DSS, b"DSS");
550    key!(DUPLEX, b"Duplex");
551    key!(DUR, b"Dur");
552    key!(DV, b"DV");
553    key!(DW, b"DW");
554    key!(DW2, b"DW2");
555
556    // E
557    key!(E, b"E");
558    key!(EARLY_CHANGE, b"EarlyChange");
559    key!(EF, b"EF");
560    key!(EMBEDDED_FDFS, b"EmbeddedFDFs");
561    key!(EMBEDDED_FILE, b"EmbeddedFile");
562    key!(EMBEDDED_FILES, b"EmbeddedFiles");
563    key!(EMPTY, b"");
564    key!(ENCODE, b"Encode");
565    key!(ENCODED_BYTE_ALIGN, b"EncodedByteAlign");
566    key!(ENCODING, b"Encoding");
567    key!(ENCODING_90MS_RKSJ_H, b"90ms-RKSJ-H");
568    key!(ENCODING_90MS_RKSJ_V, b"90ms-RKSJ-V");
569    key!(ENCODING_ETEN_B5_H, b"ETen-B5-H");
570    key!(ENCODING_ETEN_B5_V, b"ETen-B5-V");
571    key!(ENCRYPT, b"Encrypt");
572    key!(ENCRYPT_META_DATA, b"EncryptMetadata");
573    key!(ENCRYPTED_PAYLOAD, b"EncryptedPayload");
574    key!(END_OF_BLOCK, b"EndOfBlock");
575    key!(END_OF_LINE, b"EndOfLine");
576    key!(ENTRUST_PPKEF, b"Entrust.PPKEF");
577    key!(EXCLUSION, b"Exclusion");
578    key!(EXTENSIONS, b"Extensions");
579    key!(EXTENSION_LEVEL, b"ExtensionLevel");
580    key!(EX_DATA, b"ExData");
581    key!(EXPORT, b"Export");
582    key!(EXPORT_STATE, b"ExportState");
583    key!(EXT_G_STATE, b"ExtGState");
584    key!(EXTEND, b"Extend");
585    key!(EXTENDS, b"Extends");
586
587    // F
588    key!(F, b"F");
589    key!(F_DECODE_PARMS, b"FDecodeParms");
590    key!(F_FILTER, b"FFilter");
591    key!(FB, b"FB");
592    key!(FDF, b"FDF");
593    key!(FF, b"Ff");
594    key!(FIELDS, b"Fields");
595    key!(FILESPEC, b"Filespec");
596    key!(FILTER, b"Filter");
597    key!(FIRST, b"First");
598    key!(FIRST_CHAR, b"FirstChar");
599    key!(FIT_WINDOW, b"FitWindow");
600    key!(FL, b"FL");
601    key!(FLAGS, b"Flags");
602    key!(FLATE_DECODE, b"FlateDecode");
603    key!(FLATE_DECODE_ABBREVIATION, b"Fl");
604    key!(FO, b"Fo");
605    key!(FOLDERS, b"Folders");
606    key!(FONT, b"Font");
607    key!(FONT_BBOX, b"FontBBox");
608    key!(FONT_DESC, b"FontDescriptor");
609    key!(FONT_FAMILY, b"FontFamily");
610    key!(FONT_FILE, b"FontFile");
611    key!(FONT_FILE2, b"FontFile2");
612    key!(FONT_FILE3, b"FontFile3");
613    key!(FONT_MATRIX, b"FontMatrix");
614    key!(FONT_NAME, b"FontName");
615    key!(FONT_STRETCH, b"FontStretch");
616    key!(FONT_WEIGHT, b"FontWeight");
617    key!(FORM, b"Form");
618    key!(FORMTYPE, b"FormType");
619    key!(FRM, b"FRM");
620    key!(FS, b"FS");
621    key!(FT, b"FT");
622    key!(FUNCTION, b"Function");
623    key!(FUNCTION_TYPE, b"FunctionType");
624    key!(FUNCTIONS, b"Functions");
625
626    // G
627    key!(G, b"G");
628    key!(GAMMA, b"Gamma");
629    key!(GROUP, b"Group");
630    key!(GTS_PDFA1, b"GTS_PDFA1");
631
632    // H
633    key!(H, b"H");
634    key!(HARD_LIGHT, b"HardLight");
635    key!(HEIGHT, b"Height");
636    key!(HELV, b"Helv");
637    key!(HIDE_MENUBAR, b"HideMenubar");
638    key!(HIDE_TOOLBAR, b"HideToolbar");
639    key!(HIDE_WINDOWUI, b"HideWindowUI");
640    key!(HUE, b"Hue");
641
642    // I
643    key!(I, b"I");
644    key!(IC, b"IC");
645    key!(ICC_BASED, b"ICCBased");
646    key!(ID, b"ID");
647    key!(ID_TREE, b"IDTree");
648    key!(IDENTITY, b"Identity");
649    key!(IDENTITY_H, b"Identity-H");
650    key!(IDENTITY_V, b"Identity-V");
651    key!(IF, b"IF");
652    key!(ILLUSTRATOR, b"Illustrator");
653    key!(IM, b"IM");
654    key!(IMAGE, b"Image");
655    key!(IMAGE_MASK, b"ImageMask");
656    key!(INDEX, b"Index");
657    key!(INDEXED, b"Indexed");
658    key!(INFO, b"Info");
659    key!(INKLIST, b"InkList");
660    key!(INTENT, b"Intent");
661    key!(INTERPOLATE, b"Interpolate");
662    key!(IRT, b"IRT");
663    key!(IT, b"IT");
664    key!(ITALIC_ANGLE, b"ItalicAngle");
665    key!(ISSUER, b"Issuer");
666    key!(IX, b"IX");
667
668    // J
669    key!(JAVA_SCRIPT, b"JavaScript");
670    key!(JBIG2_DECODE, b"JBIG2Decode");
671    key!(JBIG2_GLOBALS, b"JBIG2Globals");
672    key!(JPX_DECODE, b"JPXDecode");
673    key!(JS, b"JS");
674
675    // K
676    key!(K, b"K");
677    key!(KEYWORDS, b"Keywords");
678    key!(KEY_USAGE, b"KeyUsage");
679    key!(KIDS, b"Kids");
680
681    // L
682    key!(L, b"L");
683    key!(LAB, b"Lab");
684    key!(LANG, b"Lang");
685    key!(LAST, b"Last");
686    key!(LAST_CHAR, b"LastChar");
687    key!(LAST_MODIFIED, b"LastModified");
688    key!(LC, b"LC");
689    key!(LE, b"LE");
690    key!(LEADING, b"Leading");
691    key!(LEGAL_ATTESTATION, b"LegalAttestation");
692    key!(LENGTH, b"Length");
693    key!(LENGTH1, b"Length1");
694    key!(LENGTH2, b"Length2");
695    key!(LENGTH3, b"Length3");
696    key!(LIGHTEN, b"Lighten");
697    key!(LIMITS, b"Limits");
698    key!(LINEARIZED, b"Linearized");
699    key!(LJ, b"LJ");
700    key!(LL, b"LL");
701    key!(LLE, b"LLE");
702    key!(LLO, b"LLO");
703    key!(LOCATION, b"Location");
704    key!(LUMINOSITY, b"Luminosity");
705    key!(LW, b"LW");
706    key!(LZW_DECODE, b"LZWDecode");
707    key!(LZW_DECODE_ABBREVIATION, b"LZW");
708
709    // M
710    key!(M, b"M");
711    key!(MAC, b"Mac");
712    key!(MAC_EXPERT_ENCODING, b"MacExpertEncoding");
713    key!(MAC_ROMAN_ENCODING, b"MacRomanEncoding");
714    key!(MARK_INFO, b"MarkInfo");
715    key!(MASK, b"Mask");
716    key!(MATRIX, b"Matrix");
717    key!(MATTE, b"Matte");
718    key!(MAX_LEN, b"MaxLen");
719    key!(MAX_WIDTH, b"MaxWidth");
720    key!(MCID, b"MCID");
721    key!(MDP, b"MDP");
722    key!(MEDIA_BOX, b"MediaBox");
723    key!(MEASURE, b"Measure");
724    key!(METADATA, b"Metadata");
725    key!(MISSING_WIDTH, b"MissingWidth");
726    key!(MIX, b"Mix");
727    key!(MK, b"MK");
728    key!(ML, b"ML");
729    key!(MM_TYPE1, b"MMType1");
730    key!(MOD_DATE, b"ModDate");
731    key!(MULTIPLY, b"Multiply");
732
733    // N
734    key!(N, b"N");
735    key!(NAME, b"Name");
736    key!(NAMES, b"Names");
737    key!(NAVIGATOR, b"Navigator");
738    key!(NEED_APPEARANCES, b"NeedAppearances");
739    key!(NEW_WINDOW, b"NewWindow");
740    key!(NEXT, b"Next");
741    key!(NM, b"NM");
742    key!(NON_EFONT_NO_WARN, b"NonEFontNoWarn");
743    key!(NON_FULL_SCREEN_PAGE_MODE, b"NonFullScreenPageMode");
744    key!(NONE, b"None");
745    key!(NORMAL, b"Normal");
746    key!(NUMS, b"Nums");
747
748    // O
749    key!(O, b"O");
750    key!(OBJ, b"Obj");
751    key!(OBJR, b"OBJR");
752    key!(OBJ_STM, b"ObjStm");
753    key!(OC, b"OC");
754    key!(OCG, b"OCG");
755    key!(OCGS, b"OCGs");
756    key!(OCMD, b"OCMD");
757    key!(OCPROPERTIES, b"OCProperties");
758    key!(OCSP, b"OCSP");
759    key!(OCSPS, b"OCSPs");
760    key!(OE, b"OE");
761    key!(OID, b"OID");
762    key!(OFF, b"OFF");
763    key!(ON, b"ON");
764    key!(OP, b"OP");
765    key!(OP_NS, b"op");
766    key!(OPEN_ACTION, b"OpenAction");
767    key!(OPEN_TYPE, b"OpenType");
768    key!(OPI, b"OPI");
769    key!(OPM, b"OPM");
770    key!(OPT, b"Opt");
771    key!(ORDER, b"Order");
772    key!(ORDERING, b"Ordering");
773    key!(OS, b"OS");
774    key!(OUTLINES, b"Outlines");
775    key!(OUTPUT_CONDITION, b"OutputCondition");
776    key!(OUTPUT_CONDITION_IDENTIFIER, b"OutputConditionIdentifier");
777    key!(OUTPUT_INTENT, b"OutputIntent");
778    key!(OUTPUT_INTENTS, b"OutputIntents");
779    key!(OVERLAY, b"Overlay");
780
781    // P
782    key!(P, b"P");
783    key!(PA, b"PA");
784    key!(PAGE, b"Page");
785    key!(PAGE_LABELS, b"PageLabels");
786    key!(PAGE_LAYOUT, b"PageLayout");
787    key!(PAGE_MODE, b"PageMode");
788    key!(PAGES, b"Pages");
789    key!(PAINT_TYPE, b"PaintType");
790    key!(PANOSE, b"Panose");
791    key!(PARAMS, b"Params");
792    key!(PARENT, b"Parent");
793    key!(PARENT_TREE, b"ParentTree");
794    key!(PARENT_TREE_NEXT_KEY, b"ParentTreeNextKey");
795    key!(PART, b"Part");
796    key!(PATH, b"Path");
797    key!(PATTERN, b"Pattern");
798    key!(PATTERN_TYPE, b"PatternType");
799    key!(PC, b"PC");
800    key!(PDF_DOC_ENCODING, b"PDFDocEncoding");
801    key!(PERMS, b"Perms");
802    key!(PERCEPTUAL, b"Perceptual");
803    key!(PIECE_INFO, b"PieceInfo");
804    key!(PG, b"Pg");
805    key!(PI, b"PI");
806    key!(PO, b"PO");
807    key!(POPUP, b"Popup");
808    key!(PRE_RELEASE, b"PreRelease");
809    key!(PREDICTOR, b"Predictor");
810    key!(PREV, b"Prev");
811    key!(PRINT, b"Print");
812    key!(PRINT_AREA, b"PrintArea");
813    key!(PRINT_CLIP, b"PrintClip");
814    key!(PRINT_SCALING, b"PrintScaling");
815    key!(PRINT_STATE, b"PrintState");
816    key!(PRIVATE, b"Private");
817    key!(PROC_SET, b"ProcSet");
818    key!(PROCESS, b"Process");
819    key!(PRODUCER, b"Producer");
820    key!(PROP_BUILD, b"Prop_Build");
821    key!(PROPERTIES, b"Properties");
822    key!(PS, b"PS");
823    key!(PT_DATA, b"PtData");
824    key!(PUB_SEC, b"PubSec");
825    key!(PV, b"PV");
826
827    // Q
828    key!(Q, b"Q");
829    key!(QUADPOINTS, b"QuadPoints");
830
831    // R
832    key!(R, b"R");
833    key!(RANGE, b"Range");
834    key!(RC, b"RC");
835    key!(RD, b"RD");
836    key!(REASON, b"Reason");
837    key!(REASONS, b"Reasons");
838    key!(RECIPIENTS, b"Recipients");
839    key!(RECT, b"Rect");
840    key!(REF, b"Ref");
841    key!(REFERENCE, b"Reference");
842    key!(REGISTRY, b"Registry");
843    key!(REGISTRY_NAME, b"RegistryName");
844    key!(RELATIVE_COLORIMETRIC, b"RelativeColorimetric");
845    key!(RENAME, b"Rename");
846    key!(REPEAT, b"Repeat");
847    key!(RES_FORK, b"ResFork");
848    key!(RESOURCES, b"Resources");
849    key!(RGB, b"RGB");
850    key!(RI, b"RI");
851    key!(ROLE_MAP, b"RoleMap");
852    key!(ROOT, b"Root");
853    key!(ROTATE, b"Rotate");
854    key!(ROWS, b"Rows");
855    key!(RT, b"RT");
856    key!(RUN_LENGTH_DECODE, b"RunLengthDecode");
857    key!(RUN_LENGTH_DECODE_ABBREVIATION, b"RL");
858    key!(RV, b"RV");
859
860    // S
861    key!(S, b"S");
862    key!(SA, b"SA");
863    key!(SATURATION, b"Saturation");
864    key!(SCHEMA, b"Schema");
865    key!(SCREEN, b"Screen");
866    key!(SE, b"SE");
867    key!(SEPARATION, b"Separation");
868    key!(SET_F, b"SetF");
869    key!(SET_FF, b"SetFf");
870    key!(SHADING, b"Shading");
871    key!(SHADING_TYPE, b"ShadingType");
872    key!(SIG, b"Sig");
873    key!(SIG_FLAGS, b"SigFlags");
874    key!(SIG_REF, b"SigRef");
875    key!(SIZE, b"Size");
876    key!(SM, b"SM");
877    key!(SMASK, b"SMask");
878    key!(SMASK_IN_DATA, b"SMaskInData");
879    key!(SOFT_LIGHT, b"SoftLight");
880    key!(SORT, b"Sort");
881    key!(SOUND, b"Sound");
882    key!(SPLIT, b"Split");
883    key!(SS, b"SS");
884    key!(ST, b"St");
885    key!(STANDARD_ENCODING, b"StandardEncoding");
886    key!(STATE, b"State");
887    key!(STATE_MODEL, b"StateModel");
888    key!(STATUS, b"Status");
889    key!(STD_CF, b"StdCF");
890    key!(STEM_H, b"StemH");
891    key!(STEM_V, b"StemV");
892    key!(STM_F, b"StmF");
893    key!(STR_F, b"StrF");
894    key!(STRUCT_ELEM, b"StructElem");
895    key!(STRUCT_PARENT, b"StructParent");
896    key!(STRUCT_PARENTS, b"StructParents");
897    key!(STRUCT_TREE_ROOT, b"StructTreeRoot");
898    key!(STYLE, b"Style");
899    key!(SUB_FILTER, b"SubFilter");
900    key!(SUBJ, b"Subj");
901    key!(SUBJECT, b"Subject");
902    key!(SUBJECT_DN, b"SubjectDN");
903    key!(SUBTYPE, b"Subtype");
904    key!(SUPPLEMENT, b"Supplement");
905    key!(SV, b"SV");
906    key!(SV_CERT, b"SVCert");
907    key!(SW, b"SW");
908    key!(SY, b"Sy");
909    key!(SYNCHRONOUS, b"Synchronous");
910    key!(T, b"T");
911    key!(TARGET, b"Target");
912    key!(TEMPLATES, b"Templates");
913    key!(THREAD, b"Thread");
914    key!(THREADS, b"Threads");
915    key!(THREE_DD, b"3DD");
916    key!(THUMB, b"Thumb");
917    key!(TI, b"TI");
918    key!(TILING_TYPE, b"TilingType");
919    key!(TIME_STAMP, b"TimeStamp");
920    key!(TITLE, b"Title");
921    key!(TK, b"TK");
922    key!(TM, b"TM");
923    key!(TO_UNICODE, b"ToUnicode");
924    key!(TR, b"TR");
925    key!(TR2, b"TR2");
926    key!(TRAPPED, b"Trapped");
927    key!(TRANS, b"Trans");
928    key!(TRANSFORM_METHOD, b"TransformMethod");
929    key!(TRANSFORM_PARAMS, b"TransformParams");
930    key!(TRANSPARENCY, b"Transparency");
931    key!(TREF, b"TRef");
932    key!(TRIM_BOX, b"TrimBox");
933    key!(TRUE_TYPE, b"TrueType");
934    key!(TRUSTED_MODE, b"TrustedMode");
935    key!(TU, b"TU");
936    key!(TX, b"Tx");
937    key!(TYPE, b"Type");
938    key!(TYPE0, b"Type0");
939    key!(TYPE1, b"Type1");
940    key!(TYPE1C, b"Type1C");
941    key!(TYPE3, b"Type3");
942
943    // U
944    key!(U, b"U");
945    key!(UE, b"UE");
946    key!(UF, b"UF");
947    key!(UNCHANGED, b"Unchanged");
948    key!(UNIX, b"Unix");
949    key!(URI, b"URI");
950    key!(URL, b"URL");
951    key!(URL_TYPE, b"URLType");
952    key!(USAGE, b"Usage");
953    key!(USE_CMAP, b"UseCMap");
954    key!(USER_UNIT, b"UserUnit");
955
956    // V
957    key!(V, b"V");
958    key!(VE, b"VE");
959    key!(VERISIGN_PPKVS, b"VeriSign.PPKVS");
960    key!(VERSION, b"Version");
961    key!(VERTICES, b"Vertices");
962    key!(VERTICES_PER_ROW, b"VerticesPerRow");
963    key!(VIEW, b"View");
964    key!(VIEW_AREA, b"ViewArea");
965    key!(VIEW_CLIP, b"ViewClip");
966    key!(VIEW_STATE, b"ViewState");
967    key!(VIEWER_PREFERENCES, b"ViewerPreferences");
968    key!(VOLUME, b"Volume");
969    key!(VP, b"VP");
970    key!(VRI, b"VRI");
971
972    // W
973    key!(W, b"W");
974    key!(W2, b"W2");
975    key!(WC, b"WC");
976    key!(WHITE_POINT, b"WhitePoint");
977    key!(WIDGET, b"Widget");
978    key!(WIDTH, b"Width");
979    key!(WIDTHS, b"Widths");
980    key!(WIN, b"Win");
981    key!(WIN_ANSI_ENCODING, b"WinAnsiEncoding");
982    key!(WMODE, b"WMode");
983    key!(WP, b"WP");
984    key!(WS, b"WS");
985
986    // X
987    key!(X, b"X");
988    key!(XFA, b"XFA");
989    key!(X_STEP, b"XStep");
990    key!(XHEIGHT, b"XHeight");
991    key!(XOBJECT, b"XObject");
992    key!(XREF, b"XRef");
993    key!(XREF_STM, b"XRefStm");
994
995    // Y
996    key!(Y, b"Y");
997    key!(Y_STEP, b"YStep");
998    key!(YES, b"Yes");
999
1000    // Z
1001    key!(ZA_DB, b"ZaDb");
1002}
1003
1004#[cfg(test)]
1005mod tests {
1006    use crate::object::Number;
1007    use crate::object::dict::keys::{COLORSPACE, EXT_G_STATE, FONT, PROC_SET};
1008    use crate::object::dict::{Dict, InlineImageDict};
1009    use crate::object::string;
1010    use crate::object::{Name, ObjRef};
1011    use crate::reader::Reader;
1012    use crate::reader::{ReaderContext, ReaderExt};
1013
1014    fn dict_impl(data: &[u8]) -> Option<Dict<'_>> {
1015        Reader::new(data).read_with_context::<Dict<'_>>(&ReaderContext::dummy())
1016    }
1017
1018    #[test]
1019    fn empty_dict_1() {
1020        let dict_data = b"<<>>";
1021        let dict = dict_impl(dict_data).unwrap();
1022
1023        assert_eq!(dict.len(), 0);
1024    }
1025
1026    #[test]
1027    fn empty_dict_2() {
1028        let dict_data = b"<<   \n >>";
1029        let dict = dict_impl(dict_data).unwrap();
1030
1031        assert_eq!(dict.len(), 0);
1032    }
1033
1034    #[test]
1035    fn dict_1() {
1036        let dict_data = b"<<  /Hi 34.0 >>";
1037        let dict = dict_impl(dict_data).unwrap();
1038
1039        assert_eq!(dict.len(), 1);
1040        assert!(dict.get::<Number>(Name::new_unescaped(b"Hi")).is_some());
1041    }
1042
1043    #[test]
1044    fn dict_2() {
1045        let dict_data = b"<<  /Hi \n 34.0 /Second true >>";
1046        let dict = dict_impl(dict_data).unwrap();
1047
1048        assert_eq!(dict.len(), 2);
1049        assert!(dict.get::<Number>(Name::new_unescaped(b"Hi")).is_some());
1050        assert!(dict.get::<bool>(Name::new_unescaped(b"Second")).is_some());
1051    }
1052
1053    #[test]
1054    fn dict_complex() {
1055        let data = "<< /Type /Example
1056/Subtype /DictionaryExample
1057/Version 0.01
1058/IntegerItem 12
1059/StringItem ( a string )
1060/Subdictionary << /Item1 0.4
1061                /Item2 true
1062                /LastItem ( not ! )
1063                /VeryLastItem ( OK )
1064                >>
1065>>";
1066
1067        let dict = Reader::new(data.as_bytes())
1068            .read_with_context::<Dict<'_>>(&ReaderContext::dummy())
1069            .unwrap();
1070        assert_eq!(dict.len(), 6);
1071        assert!(dict.get::<Name<'_>>(Name::new_unescaped(b"Type")).is_some());
1072        assert!(
1073            dict.get::<Name<'_>>(Name::new_unescaped(b"Subtype"))
1074                .is_some()
1075        );
1076        assert!(
1077            dict.get::<Number>(Name::new_unescaped(b"Version"))
1078                .is_some()
1079        );
1080        assert!(
1081            dict.get::<i32>(Name::new_unescaped(b"IntegerItem"))
1082                .is_some()
1083        );
1084        assert!(
1085            dict.get::<string::String<'_>>(Name::new_unescaped(b"StringItem"))
1086                .is_some()
1087        );
1088        assert!(
1089            dict.get::<Dict<'_>>(Name::new_unescaped(b"Subdictionary"))
1090                .is_some()
1091        );
1092    }
1093
1094    #[test]
1095    fn dict_with_trailing() {
1096        let dict_data = b"<<  /Hi 67.0  >>trailing data";
1097        let dict = dict_impl(dict_data).unwrap();
1098
1099        assert_eq!(dict.len(), 1);
1100    }
1101
1102    #[test]
1103    fn dict_with_comment() {
1104        let dict_data = b"<<  /Hi % A comment \n 67.0 % Another comment \n >>";
1105        let dict = dict_impl(dict_data).unwrap();
1106
1107        assert_eq!(dict.len(), 1);
1108    }
1109
1110    #[test]
1111    fn inline_dict() {
1112        let dict_data = b"/W 17 /H 17 /CS /RGB /BPC 8 /F [ /A85 /LZW ] ID ";
1113
1114        let dict = Reader::new(&dict_data[..])
1115            .read_with_context::<InlineImageDict<'_>>(&ReaderContext::dummy())
1116            .unwrap();
1117
1118        assert_eq!(dict.get_dict().len(), 5);
1119    }
1120
1121    #[test]
1122    fn dict_with_escaped_name() {
1123        let dict_data = b"<< /PANTONE#20104#20C 234 >>";
1124        let dict = dict_impl(dict_data).unwrap();
1125
1126        assert!(dict.contains_key("PANTONE 104 C"));
1127    }
1128
1129    #[test]
1130    fn garbage_in_between() {
1131        let dict_data = b"<< 
1132/ProcSet [ /PDF /Text ] 
1133/Font << /F4 31 0 R /F6 23 0 R >> 
1134/ExtGState << /GS2 14 0 R
11352000
1136 /GS3 15 0 R >> 
1137/ColorSpace << /Cs5 13 0 R >> 
1138>> ";
1139        let dict = dict_impl(dict_data).unwrap();
1140
1141        assert!(dict.contains_key(PROC_SET));
1142        assert!(dict.contains_key(FONT));
1143        assert!(dict.contains_key(EXT_G_STATE));
1144        assert!(dict.contains_key(COLORSPACE));
1145
1146        let Some(dict) = dict.get::<Dict<'_>>(EXT_G_STATE) else {
1147            panic!("failed to parse ext g state");
1148        };
1149
1150        assert_eq!(dict.get_ref("GS2"), Some(ObjRef::new(14, 0)));
1151        assert_eq!(dict.get_ref("GS3"), Some(ObjRef::new(15, 0)));
1152    }
1153}