use lopdf;
use {
XObject, Pattern, ExtendedGraphicsState, ExtendedGraphicsStateList,
PatternRef, OCGRef, XObjectList, XObjectRef, ExtendedGraphicsStateRef,
OCGList, PatternList
};
#[derive(Default, Debug, Clone)]
pub struct PdfResources {
pub xobjects: XObjectList,
pub patterns: PatternList,
pub graphics_states: ExtendedGraphicsStateList,
pub layers: OCGList,
}
impl PdfResources {
pub fn new()
-> Self
{
Self::default()
}
#[inline]
pub fn add_graphics_state(&mut self, added_state: ExtendedGraphicsState)
-> ExtendedGraphicsStateRef
{
self.graphics_states.add_graphics_state(added_state)
}
#[inline]
pub fn add_xobject(&mut self, xobj: XObject)
-> XObjectRef
{
self.xobjects.add_xobject(xobj)
}
#[inline]
pub fn add_pattern(&mut self, pattern: Pattern)
-> PatternRef
{
self.patterns.add_pattern(pattern)
}
#[cfg_attr(feature = "cargo-clippy", allow(needless_return))]
pub fn into_with_document_and_layers(self, doc: &mut lopdf::Document, layers: Vec<lopdf::Object>)
-> (lopdf::Dictionary, Vec<OCGRef>)
{
let mut dict = lopdf::Dictionary::new();
let mut ocg_dict = self.layers;
let mut ocg_references = Vec::<OCGRef>::new();
let xobjects_dict: lopdf::Dictionary = self.xobjects.into_with_document(doc);
let patterns_dict: lopdf::Dictionary = self.patterns.into();
let graphics_state_dict: lopdf::Dictionary = self.graphics_states.into();
if !layers.is_empty() {
for l in layers {
ocg_references.push(ocg_dict.add_ocg(l));
}
let cur_ocg_dict_obj: lopdf::Dictionary = ocg_dict.into();
if cur_ocg_dict_obj.len() > 0 {
dict.set("Properties", lopdf::Object::Dictionary(cur_ocg_dict_obj));
}
}
if xobjects_dict.len() > 0 {
dict.set("XObject", lopdf::Object::Dictionary(xobjects_dict));
}
if patterns_dict.len() > 0 {
dict.set("Pattern", lopdf::Object::Dictionary(patterns_dict));
}
if graphics_state_dict.len() > 0 {
dict.set("ExtGState", lopdf::Object::Dictionary(graphics_state_dict));
}
return (dict, ocg_references);
}
}