1use pdf_syntax::object::dict::keys::*;
10use pdf_syntax::object::{Dict, Name, Stream};
11
12#[derive(Debug, Clone)]
14pub struct AppearanceDict<'a> {
15 dict: Dict<'a>,
16}
17
18impl<'a> AppearanceDict<'a> {
19 pub fn from_annot(annot_dict: &Dict<'a>) -> Option<Self> {
21 let dict = annot_dict.get::<Dict<'_>>(AP)?;
22 Some(Self { dict })
23 }
24
25 pub fn normal(&self, annot_dict: &Dict<'a>) -> Option<Stream<'a>> {
27 resolve_appearance(&self.dict, N, annot_dict)
28 }
29
30 pub fn rollover(&self, annot_dict: &Dict<'a>) -> Option<Stream<'a>> {
32 resolve_appearance(&self.dict, R, annot_dict)
33 }
34
35 pub fn down(&self, annot_dict: &Dict<'a>) -> Option<Stream<'a>> {
37 resolve_appearance(&self.dict, D, annot_dict)
38 }
39}
40
41fn resolve_appearance<'a>(
43 ap_dict: &Dict<'a>,
44 key: &[u8],
45 annot_dict: &Dict<'a>,
46) -> Option<Stream<'a>> {
47 if let Some(stream) = ap_dict.get::<Stream<'_>>(key) {
48 return Some(stream);
49 }
50 if let Some(sub_dict) = ap_dict.get::<Dict<'_>>(key) {
51 let appearance_state = annot_dict.get::<Name>(AS)?;
52 return sub_dict.get::<Stream<'_>>(appearance_state.as_ref());
53 }
54 None
55}