use pdf_syntax::object::dict::keys::*;
use pdf_syntax::object::{Dict, Name, Stream};
#[derive(Debug, Clone)]
pub struct AppearanceDict<'a> {
dict: Dict<'a>,
}
impl<'a> AppearanceDict<'a> {
pub fn from_annot(annot_dict: &Dict<'a>) -> Option<Self> {
let dict = annot_dict.get::<Dict<'_>>(AP)?;
Some(Self { dict })
}
pub fn normal(&self, annot_dict: &Dict<'a>) -> Option<Stream<'a>> {
resolve_appearance(&self.dict, N, annot_dict)
}
pub fn rollover(&self, annot_dict: &Dict<'a>) -> Option<Stream<'a>> {
resolve_appearance(&self.dict, R, annot_dict)
}
pub fn down(&self, annot_dict: &Dict<'a>) -> Option<Stream<'a>> {
resolve_appearance(&self.dict, D, annot_dict)
}
}
fn resolve_appearance<'a>(
ap_dict: &Dict<'a>,
key: &[u8],
annot_dict: &Dict<'a>,
) -> Option<Stream<'a>> {
if let Some(stream) = ap_dict.get::<Stream<'_>>(key) {
return Some(stream);
}
if let Some(sub_dict) = ap_dict.get::<Dict<'_>>(key) {
let appearance_state = annot_dict.get::<Name>(AS)?;
return sub_dict.get::<Stream<'_>>(appearance_state.as_ref());
}
None
}