Created
April 10, 2019 20:51
-
-
Save Ploppz/f8d5aa2dea1a95527eade8d7cdecd9a7 to your computer and use it in GitHub Desktop.
Object implementations derived in the pdf libray
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
impl ::pdf::object::Object for Catalog { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!(out, "<<")?; | |
write!(out, "{} ", "Pages")?; | |
self.pages.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "Names")?; | |
self.names.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "StructTreeRoot")?; | |
self.struct_tree_root.serialize(out)?; | |
writeln!(out, "")?; | |
writeln!(out, ">>")?; | |
Ok(()) | |
} | |
fn from_primitive(p: Primitive, resolve: &Resolve) -> Result<Self> { | |
let mut dict = Dictionary::from_primitive(p, resolve)?; | |
let pages = { | |
match dict.remove("Pages") { | |
Some(primitive) => match <PageTree as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Pages", | |
stringify!(PageTree) | |
))), | |
}, | |
None => match <PageTree as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(pages), "Pages"), | |
}, | |
} | |
}; | |
let names = { | |
match dict.remove("Names") { | |
Some(primitive) => { | |
match <Option<NameDictionary> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Names", | |
stringify!(Option<NameDictionary>) | |
))), | |
} | |
} | |
None => match <Option<NameDictionary> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(names), "Names"), | |
}, | |
} | |
}; | |
let struct_tree_root = { | |
match dict.remove("StructTreeRoot") { | |
Some(primitive) => { | |
match <Option<StructTreeRoot> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"StructTreeRoot", | |
stringify!(Option<StructTreeRoot>) | |
))), | |
} | |
} | |
None => match <Option<StructTreeRoot> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!( | |
"Object {}, Key {} not found", | |
stringify!(struct_tree_root), | |
"StructTreeRoot" | |
), | |
}, | |
} | |
}; | |
Ok(Catalog { | |
pages: pages, | |
names: names, | |
struct_tree_root: struct_tree_root, | |
}) | |
} | |
} | |
impl ::pdf::object::Object for PageTree { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!(out, "<<")?; | |
writeln!(out, "/Type /{}", "Pages")?; | |
write!(out, "{} ", "Parent")?; | |
self.parent.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "Kids")?; | |
self.kids.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "Count")?; | |
self.count.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "Resources")?; | |
self.resources.serialize(out)?; | |
writeln!(out, "")?; | |
writeln!(out, ">>")?; | |
Ok(()) | |
} | |
fn from_primitive(p: Primitive, resolve: &Resolve) -> Result<Self> { | |
let mut dict = Dictionary::from_primitive(p, resolve)?; | |
let ty = dict | |
.remove("Type") | |
.ok_or(::pdf::Error::from(::pdf::ErrorKind::EntryNotFound { | |
key: "Type", | |
}))? | |
.to_name()?; | |
if ty != "Pages" { | |
bail!("[Dict entry /{}] != /{}", "Type", "Pages"); | |
} | |
let parent = { | |
match dict.remove("Parent") { | |
Some(primitive) => { | |
match <Option<Ref<PageTree>> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Parent", | |
stringify!(Option<Ref<PageTree>>) | |
))), | |
} | |
} | |
None => match <Option<Ref<PageTree>> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(parent), "Parent"), | |
}, | |
} | |
}; | |
let kids = { | |
match dict.remove("Kids") { | |
Some(primitive) => { | |
match <Vec<PagesNode> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Kids", | |
stringify!(Vec<PagesNode>) | |
))), | |
} | |
} | |
None => match <Vec<PagesNode> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(kids), "Kids"), | |
}, | |
} | |
}; | |
let count = { | |
match dict.remove("Count") { | |
Some(primitive) => match <i32 as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Count", | |
stringify!(i32) | |
))), | |
}, | |
None => match <i32 as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(count), "Count"), | |
}, | |
} | |
}; | |
let resources = { | |
match dict.remove("Resources") { | |
Some(primitive) => { | |
match <Option<Resources> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Resources", | |
stringify!(Option<Resources>) | |
))), | |
} | |
} | |
None => match <Option<Resources> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!( | |
"Object {}, Key {} not found", | |
stringify!(resources), | |
"Resources" | |
), | |
}, | |
} | |
}; | |
Ok(PageTree { | |
parent: parent, | |
kids: kids, | |
count: count, | |
resources: resources, | |
}) | |
} | |
} | |
impl ::pdf::object::Object for Page { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!(out, "<<")?; | |
write!(out, "{} ", "Parent")?; | |
self.parent.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "Resources")?; | |
self.resources.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "MediaBox")?; | |
self.media_box.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "CropBox")?; | |
self.crop_box.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "TrimBox")?; | |
self.trim_box.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "Contents")?; | |
self.contents.serialize(out)?; | |
writeln!(out, "")?; | |
writeln!(out, ">>")?; | |
Ok(()) | |
} | |
fn from_primitive(p: Primitive, resolve: &Resolve) -> Result<Self> { | |
let mut dict = Dictionary::from_primitive(p, resolve)?; | |
let parent = { | |
match dict.remove("Parent") { | |
Some(primitive) => { | |
match <Ref<PageTree> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Parent", | |
stringify!(Ref<PageTree>) | |
))), | |
} | |
} | |
None => match <Ref<PageTree> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(parent), "Parent"), | |
}, | |
} | |
}; | |
let resources = { | |
match dict.remove("Resources") { | |
Some(primitive) => { | |
match <Option<Resources> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Resources", | |
stringify!(Option<Resources>) | |
))), | |
} | |
} | |
None => match <Option<Resources> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!( | |
"Object {}, Key {} not found", | |
stringify!(resources), | |
"Resources" | |
), | |
}, | |
} | |
}; | |
let media_box = { | |
match dict.remove("MediaBox") { | |
Some(primitive) => { | |
match <Option<Rect> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"MediaBox", | |
stringify!(Option<Rect>) | |
))), | |
} | |
} | |
None => match <Option<Rect> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!( | |
"Object {}, Key {} not found", | |
stringify!(media_box), | |
"MediaBox" | |
), | |
}, | |
} | |
}; | |
let crop_box = { | |
match dict.remove("CropBox") { | |
Some(primitive) => { | |
match <Option<Rect> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"CropBox", | |
stringify!(Option<Rect>) | |
))), | |
} | |
} | |
None => match <Option<Rect> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!( | |
"Object {}, Key {} not found", | |
stringify!(crop_box), | |
"CropBox" | |
), | |
}, | |
} | |
}; | |
let trim_box = { | |
match dict.remove("TrimBox") { | |
Some(primitive) => { | |
match <Option<Rect> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"TrimBox", | |
stringify!(Option<Rect>) | |
))), | |
} | |
} | |
None => match <Option<Rect> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!( | |
"Object {}, Key {} not found", | |
stringify!(trim_box), | |
"TrimBox" | |
), | |
}, | |
} | |
}; | |
let contents = { | |
match dict.remove("Contents") { | |
Some(primitive) => { | |
match <Vec<Content> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Contents", | |
stringify!(Vec<Content>) | |
))), | |
} | |
} | |
None => match <Vec<Content> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!( | |
"Object {}, Key {} not found", | |
stringify!(contents), | |
"Contents" | |
), | |
}, | |
} | |
}; | |
Ok(Page { | |
parent: parent, | |
resources: resources, | |
media_box: media_box, | |
crop_box: crop_box, | |
trim_box: trim_box, | |
contents: contents, | |
}) | |
} | |
} | |
impl ::pdf::object::Object for PageLabel { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!(out, "<<")?; | |
write!(out, "{} ", "S")?; | |
self.style.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "P")?; | |
self.prefix.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "St")?; | |
self.start.serialize(out)?; | |
writeln!(out, "")?; | |
writeln!(out, ">>")?; | |
Ok(()) | |
} | |
fn from_primitive(p: Primitive, resolve: &Resolve) -> Result<Self> { | |
let mut dict = Dictionary::from_primitive(p, resolve)?; | |
let style = { | |
match dict.remove("S") { | |
Some(primitive) => { | |
match <Option<Counter> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"S", | |
stringify!(Option<Counter>) | |
))), | |
} | |
} | |
None => match <Option<Counter> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(style), "S"), | |
}, | |
} | |
}; | |
let prefix = { | |
match dict.remove("P") { | |
Some(primitive) => { | |
match <Option<PdfString> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"P", | |
stringify!(Option<PdfString>) | |
))), | |
} | |
} | |
None => match <Option<PdfString> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(prefix), "P"), | |
}, | |
} | |
}; | |
let start = { | |
match dict.remove("St") { | |
Some(primitive) => { | |
match <Option<usize> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"St", | |
stringify!(Option<usize>) | |
))), | |
} | |
} | |
None => match <Option<usize> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(start), "St"), | |
}, | |
} | |
}; | |
Ok(PageLabel { | |
style: style, | |
prefix: prefix, | |
start: start, | |
}) | |
} | |
} | |
impl ::pdf::object::Object for Resources { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!(out, "<<")?; | |
write!(out, "{} ", "ExtGState")?; | |
self.ext_g_state.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "XObject")?; | |
self.xobjects.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "Font")?; | |
self.fonts.serialize(out)?; | |
writeln!(out, "")?; | |
writeln!(out, ">>")?; | |
Ok(()) | |
} | |
fn from_primitive(p: Primitive, resolve: &Resolve) -> Result<Self> { | |
let mut dict = Dictionary::from_primitive(p, resolve)?; | |
let ext_g_state = { | |
match dict.remove("ExtGState") { | |
Some(primitive) => { | |
match <Option<GraphicsStateParameters> as Object>::from_primitive( | |
primitive, resolve, | |
) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"ExtGState", | |
stringify!(Option<GraphicsStateParameters>) | |
))), | |
} | |
} | |
None => match <Option<GraphicsStateParameters> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!( | |
"Object {}, Key {} not found", | |
stringify!(ext_g_state), | |
"ExtGState" | |
), | |
}, | |
} | |
}; | |
let xobjects = { | |
match dict.remove("XObject") { | |
Some(primitive) => { | |
match <Option<BTreeMap<String, XObject>> as Object>::from_primitive( | |
primitive, resolve, | |
) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"XObject", | |
stringify!(Option<BTreeMap<String, XObject>>) | |
))), | |
} | |
} | |
None => match <Option<BTreeMap<String, XObject>> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!( | |
"Object {}, Key {} not found", | |
stringify!(xobjects), | |
"XObject" | |
), | |
}, | |
} | |
}; | |
let fonts = { | |
match dict.remove("Font") { | |
Some(primitive) => { | |
match <Option<BTreeMap<String, Font>> as Object>::from_primitive( | |
primitive, resolve, | |
) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Font", | |
stringify!(Option<BTreeMap<String, Font>>) | |
))), | |
} | |
} | |
None => match <Option<BTreeMap<String, Font>> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(fonts), "Font"), | |
}, | |
} | |
}; | |
Ok(Resources { | |
ext_g_state: ext_g_state, | |
xobjects: xobjects, | |
fonts: fonts, | |
}) | |
} | |
} | |
impl ::pdf::object::Object for GraphicsStateParameters { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!(out, "<<")?; | |
writeln!(out, "/Type /{}", "ExtGState")?; | |
writeln!(out, ">>")?; | |
Ok(()) | |
} | |
fn from_primitive(p: Primitive, resolve: &Resolve) -> Result<Self> { | |
let mut dict = Dictionary::from_primitive(p, resolve)?; | |
match dict.remove("Type") { | |
Some(ty) => { | |
if ty.to_name()? != "ExtGState" { | |
bail!("[Dict entry /{}] != /{}", "Type", "ExtGState"); | |
} | |
} | |
None => {} | |
} | |
Ok(GraphicsStateParameters {}) | |
} | |
} | |
impl ::pdf::object::Object for PostScriptDict { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!(out, "<<")?; | |
writeln!(out, "/Type /{}", "XObject")?; | |
writeln!(out, "/{} /{}", "Subtype", "PS")?; | |
writeln!(out, ">>")?; | |
Ok(()) | |
} | |
fn from_primitive(p: Primitive, resolve: &Resolve) -> Result<Self> { | |
let mut dict = Dictionary::from_primitive(p, resolve)?; | |
let ty = dict | |
.remove("Type") | |
.ok_or(::pdf::Error::from(::pdf::ErrorKind::EntryNotFound { | |
key: "Type", | |
}))? | |
.to_name()?; | |
if ty != "XObject" { | |
bail!("[Dict entry /{}] != /{}", "Type", "XObject"); | |
} | |
let ty = dict | |
.remove("Subtype") | |
.ok_or(::pdf::Error::from(::pdf::ErrorKind::EntryNotFound { | |
key: "Subtype", | |
}))? | |
.to_name()?; | |
if ty != "PS" { | |
bail!("[Dict entry /{}] != /{}", "Subtype", "PS"); | |
} | |
Ok(PostScriptDict {}) | |
} | |
} | |
impl ::pdf::object::Object for ImageDict { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!(out, "<<")?; | |
writeln!(out, "/Type /{}", "XObject")?; | |
writeln!(out, "/{} /{}", "Subtype", "Image")?; | |
write!(out, "{} ", "Width")?; | |
self.width.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "Height")?; | |
self.height.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "BitsPerComponent")?; | |
self.bits_per_component.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "Intent")?; | |
self.intent.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "ImageMask")?; | |
self.image_mask.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "Decode")?; | |
self.decode.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "Interpolate")?; | |
self.interpolate.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "StructParent")?; | |
self.struct_parent.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "ID")?; | |
self.id.serialize(out)?; | |
writeln!(out, "")?; | |
writeln!(out, ">>")?; | |
Ok(()) | |
} | |
fn from_primitive(p: Primitive, resolve: &Resolve) -> Result<Self> { | |
let mut dict = Dictionary::from_primitive(p, resolve)?; | |
let ty = dict | |
.remove("Type") | |
.ok_or(::pdf::Error::from(::pdf::ErrorKind::EntryNotFound { | |
key: "Type", | |
}))? | |
.to_name()?; | |
if ty != "XObject" { | |
bail!("[Dict entry /{}] != /{}", "Type", "XObject"); | |
} | |
let ty = dict | |
.remove("Subtype") | |
.ok_or(::pdf::Error::from(::pdf::ErrorKind::EntryNotFound { | |
key: "Subtype", | |
}))? | |
.to_name()?; | |
if ty != "Image" { | |
bail!("[Dict entry /{}] != /{}", "Subtype", "Image"); | |
} | |
let width = { | |
match dict.remove("Width") { | |
Some(primitive) => match <i32 as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Width", | |
stringify!(i32) | |
))), | |
}, | |
None => match <i32 as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(width), "Width"), | |
}, | |
} | |
}; | |
let height = { | |
match dict.remove("Height") { | |
Some(primitive) => match <i32 as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Height", | |
stringify!(i32) | |
))), | |
}, | |
None => match <i32 as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(height), "Height"), | |
}, | |
} | |
}; | |
let bits_per_component = { | |
match dict.remove("BitsPerComponent") { | |
Some(primitive) => match <i32 as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"BitsPerComponent", | |
stringify!(i32) | |
))), | |
}, | |
None => match <i32 as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!( | |
"Object {}, Key {} not found", | |
stringify!(bits_per_component), | |
"BitsPerComponent" | |
), | |
}, | |
} | |
}; | |
let intent = { | |
match dict.remove("Intent") { | |
Some(primitive) => { | |
match <Option<RenderingIntent> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Intent", | |
stringify!(Option<RenderingIntent>) | |
))), | |
} | |
} | |
None => match <Option<RenderingIntent> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(intent), "Intent"), | |
}, | |
} | |
}; | |
let image_mask = { | |
let primitive: Option<::pdf::primitive::Primitive> = dict.remove("ImageMask"); | |
let x: bool = match primitive { | |
Some(primitive) => <bool as Object>::from_primitive(primitive, resolve) | |
.chain_err(|| stringify!(image_mask))?, | |
None => false, | |
}; | |
x | |
}; | |
let decode = { | |
match dict.remove("Decode") { | |
Some(primitive) => match <Vec<i32> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Decode", | |
stringify!(Vec<i32>) | |
))), | |
}, | |
None => match <Vec<i32> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(decode), "Decode"), | |
}, | |
} | |
}; | |
let interpolate = { | |
let primitive: Option<::pdf::primitive::Primitive> = dict.remove("Interpolate"); | |
let x: bool = match primitive { | |
Some(primitive) => <bool as Object>::from_primitive(primitive, resolve) | |
.chain_err(|| stringify!(interpolate))?, | |
None => false, | |
}; | |
x | |
}; | |
let struct_parent = { | |
match dict.remove("StructParent") { | |
Some(primitive) => { | |
match <Option<i32> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"StructParent", | |
stringify!(Option<i32>) | |
))), | |
} | |
} | |
None => match <Option<i32> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!( | |
"Object {}, Key {} not found", | |
stringify!(struct_parent), | |
"StructParent" | |
), | |
}, | |
} | |
}; | |
let id = { | |
match dict.remove("ID") { | |
Some(primitive) => { | |
match <Option<PdfString> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"ID", | |
stringify!(Option<PdfString>) | |
))), | |
} | |
} | |
None => match <Option<PdfString> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(id), "ID"), | |
}, | |
} | |
}; | |
Ok(ImageDict { | |
width: width, | |
height: height, | |
bits_per_component: bits_per_component, | |
intent: intent, | |
image_mask: image_mask, | |
decode: decode, | |
interpolate: interpolate, | |
struct_parent: struct_parent, | |
id: id, | |
}) | |
} | |
} | |
impl ::pdf::object::Object for RenderingIntent { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!( | |
out, | |
"/{}", | |
match *self { | |
RenderingIntent::AbsoluteColorimetric => stringify!(AbsoluteColorimetric), | |
RenderingIntent::RelativeColorimetric => stringify!(RelativeColorimetric), | |
RenderingIntent::Saturation => stringify!(Saturation), | |
RenderingIntent::Perceptual => stringify!(Perceptual), | |
} | |
) | |
} | |
fn from_primitive(p: Primitive, _resolve: &Resolve) -> Result<Self> { | |
Ok(match p { | |
Primitive::Name(name) => match name.as_str() { | |
stringify!(AbsoluteColorimetric) => RenderingIntent::AbsoluteColorimetric, | |
stringify!(RelativeColorimetric) => RenderingIntent::RelativeColorimetric, | |
stringify!(Saturation) => RenderingIntent::Saturation, | |
stringify!(Perceptual) => RenderingIntent::Perceptual, | |
s => bail!(format!( | |
"Enum {} from_primitive: no variant {}.", | |
stringify!(RenderingIntent), | |
s | |
)), | |
}, | |
_ => bail!(::pdf::Error::from(::pdf::ErrorKind::UnexpectedPrimitive { | |
expected: "Name", | |
found: p.get_debug_name() | |
})), | |
}) | |
} | |
} | |
impl ::pdf::object::Object for FormDict { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!(out, "<<")?; | |
writeln!(out, "/Type /{}", "XObject")?; | |
writeln!(out, "/{} /{}", "Subtype", "Form")?; | |
writeln!(out, ">>")?; | |
Ok(()) | |
} | |
fn from_primitive(p: Primitive, resolve: &Resolve) -> Result<Self> { | |
let mut dict = Dictionary::from_primitive(p, resolve)?; | |
let ty = dict | |
.remove("Type") | |
.ok_or(::pdf::Error::from(::pdf::ErrorKind::EntryNotFound { | |
key: "Type", | |
}))? | |
.to_name()?; | |
if ty != "XObject" { | |
bail!("[Dict entry /{}] != /{}", "Type", "XObject"); | |
} | |
let ty = dict | |
.remove("Subtype") | |
.ok_or(::pdf::Error::from(::pdf::ErrorKind::EntryNotFound { | |
key: "Subtype", | |
}))? | |
.to_name()?; | |
if ty != "Form" { | |
bail!("[Dict entry /{}] != /{}", "Subtype", "Form"); | |
} | |
Ok(FormDict {}) | |
} | |
} | |
impl ::pdf::object::Object for NameDictionary { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!(out, "<<")?; | |
write!(out, "{} ", "EmbeddedFiles")?; | |
self.embedded_files.serialize(out)?; | |
writeln!(out, "")?; | |
writeln!(out, ">>")?; | |
Ok(()) | |
} | |
fn from_primitive(p: Primitive, resolve: &Resolve) -> Result<Self> { | |
let mut dict = Dictionary::from_primitive(p, resolve)?; | |
let embedded_files = { | |
match dict.remove("EmbeddedFiles") { | |
Some(primitive) => { | |
match <Option<NameTree<FileSpec>> as Object>::from_primitive(primitive, resolve) | |
{ | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"EmbeddedFiles", | |
stringify!(Option<NameTree<FileSpec>>) | |
))), | |
} | |
} | |
None => match <Option<NameTree<FileSpec>> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!( | |
"Object {}, Key {} not found", | |
stringify!(embedded_files), | |
"EmbeddedFiles" | |
), | |
}, | |
} | |
}; | |
Ok(NameDictionary { | |
embedded_files: embedded_files, | |
}) | |
} | |
} | |
impl ::pdf::object::Object for FileSpec { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!(out, "<<")?; | |
write!(out, "{} ", "EF")?; | |
self.ef.serialize(out)?; | |
writeln!(out, "")?; | |
writeln!(out, ">>")?; | |
Ok(()) | |
} | |
fn from_primitive(p: Primitive, resolve: &Resolve) -> Result<Self> { | |
let mut dict = Dictionary::from_primitive(p, resolve)?; | |
let ef = { | |
match dict.remove("EF") { | |
Some(primitive) => match <Option<Files<EmbeddedFile>> as Object>::from_primitive( | |
primitive, resolve, | |
) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"EF", | |
stringify!(Option<Files<EmbeddedFile>>) | |
))), | |
}, | |
None => match <Option<Files<EmbeddedFile>> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(ef), "EF"), | |
}, | |
} | |
}; | |
Ok(FileSpec { ef: ef }) | |
} | |
} | |
impl<T: Object> ::pdf::object::Object for Files<T> { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!(out, "<<")?; | |
write!(out, "{} ", "F")?; | |
self.f.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "UF")?; | |
self.uf.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "DOS")?; | |
self.dos.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "Mac")?; | |
self.mac.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "Unix")?; | |
self.unix.serialize(out)?; | |
writeln!(out, "")?; | |
writeln!(out, ">>")?; | |
Ok(()) | |
} | |
fn from_primitive(p: Primitive, resolve: &Resolve) -> Result<Self> { | |
let mut dict = Dictionary::from_primitive(p, resolve)?; | |
let f = { | |
match dict.remove("F") { | |
Some(primitive) => { | |
match <Option<T> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"F", | |
stringify!(Option<T>) | |
))), | |
} | |
} | |
None => match <Option<T> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(f), "F"), | |
}, | |
} | |
}; | |
let uf = { | |
match dict.remove("UF") { | |
Some(primitive) => { | |
match <Option<T> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"UF", | |
stringify!(Option<T>) | |
))), | |
} | |
} | |
None => match <Option<T> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(uf), "UF"), | |
}, | |
} | |
}; | |
let dos = { | |
match dict.remove("DOS") { | |
Some(primitive) => { | |
match <Option<T> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"DOS", | |
stringify!(Option<T>) | |
))), | |
} | |
} | |
None => match <Option<T> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(dos), "DOS"), | |
}, | |
} | |
}; | |
let mac = { | |
match dict.remove("Mac") { | |
Some(primitive) => { | |
match <Option<T> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Mac", | |
stringify!(Option<T>) | |
))), | |
} | |
} | |
None => match <Option<T> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(mac), "Mac"), | |
}, | |
} | |
}; | |
let unix = { | |
match dict.remove("Unix") { | |
Some(primitive) => { | |
match <Option<T> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Unix", | |
stringify!(Option<T>) | |
))), | |
} | |
} | |
None => match <Option<T> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(unix), "Unix"), | |
}, | |
} | |
}; | |
Ok(Files { | |
f: f, | |
uf: uf, | |
dos: dos, | |
mac: mac, | |
unix: unix, | |
}) | |
} | |
} | |
impl ::pdf::object::Object for EmbeddedFile { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!(out, "<<")?; | |
write!(out, "{} ", "Params")?; | |
self.params.serialize(out)?; | |
writeln!(out, "")?; | |
writeln!(out, ">>")?; | |
Ok(()) | |
} | |
fn from_primitive(p: Primitive, resolve: &Resolve) -> Result<Self> { | |
let mut dict = Dictionary::from_primitive(p, resolve)?; | |
let params = { | |
match dict.remove("Params") { | |
Some(primitive) => match <Option<EmbeddedFileParamDict> as Object>::from_primitive( | |
primitive, resolve, | |
) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Params", | |
stringify!(Option<EmbeddedFileParamDict>) | |
))), | |
}, | |
None => match <Option<EmbeddedFileParamDict> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(params), "Params"), | |
}, | |
} | |
}; | |
Ok(EmbeddedFile { params: params }) | |
} | |
} | |
impl ::pdf::object::Object for EmbeddedFileParamDict { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!(out, "<<")?; | |
write!(out, "{} ", "Size")?; | |
self.size.serialize(out)?; | |
writeln!(out, "")?; | |
writeln!(out, ">>")?; | |
Ok(()) | |
} | |
fn from_primitive(p: Primitive, resolve: &Resolve) -> Result<Self> { | |
let mut dict = Dictionary::from_primitive(p, resolve)?; | |
let size = { | |
match dict.remove("Size") { | |
Some(primitive) => { | |
match <Option<i32> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Size", | |
stringify!(Option<i32>) | |
))), | |
} | |
} | |
None => match <Option<i32> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(size), "Size"), | |
}, | |
} | |
}; | |
Ok(EmbeddedFileParamDict { size: size }) | |
} | |
} | |
impl ::pdf::object::Object for Outlines { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!(out, "<<")?; | |
write!(out, "{} ", "Count")?; | |
self.count.serialize(out)?; | |
writeln!(out, "")?; | |
writeln!(out, ">>")?; | |
Ok(()) | |
} | |
fn from_primitive(p: Primitive, resolve: &Resolve) -> Result<Self> { | |
let mut dict = Dictionary::from_primitive(p, resolve)?; | |
let count = { | |
match dict.remove("Count") { | |
Some(primitive) => match <usize as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Count", | |
stringify!(usize) | |
))), | |
}, | |
None => match <usize as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(count), "Count"), | |
}, | |
} | |
}; | |
Ok(Outlines { count: count }) | |
} | |
} | |
impl ::pdf::object::Object for MarkInformation { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!(out, "<<")?; | |
write!(out, "{} ", "Marked")?; | |
self.marked.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "UserProperties")?; | |
self.user_properties.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "Suspects")?; | |
self.suspects.serialize(out)?; | |
writeln!(out, "")?; | |
writeln!(out, ">>")?; | |
Ok(()) | |
} | |
fn from_primitive(p: Primitive, resolve: &Resolve) -> Result<Self> { | |
let mut dict = Dictionary::from_primitive(p, resolve)?; | |
let marked = { | |
let primitive: Option<::pdf::primitive::Primitive> = dict.remove("Marked"); | |
let x: bool = match primitive { | |
Some(primitive) => <bool as Object>::from_primitive(primitive, resolve) | |
.chain_err(|| stringify!(marked))?, | |
None => false, | |
}; | |
x | |
}; | |
let user_properties = { | |
let primitive: Option<::pdf::primitive::Primitive> = dict.remove("UserProperties"); | |
let x: bool = match primitive { | |
Some(primitive) => <bool as Object>::from_primitive(primitive, resolve) | |
.chain_err(|| stringify!(user_properties))?, | |
None => false, | |
}; | |
x | |
}; | |
let suspects = { | |
let primitive: Option<::pdf::primitive::Primitive> = dict.remove("Suspects"); | |
let x: bool = match primitive { | |
Some(primitive) => <bool as Object>::from_primitive(primitive, resolve) | |
.chain_err(|| stringify!(suspects))?, | |
None => false, | |
}; | |
x | |
}; | |
Ok(MarkInformation { | |
marked: marked, | |
user_properties: user_properties, | |
suspects: suspects, | |
}) | |
} | |
} | |
impl ::pdf::object::Object for StructTreeRoot { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!(out, "<<")?; | |
writeln!(out, "/Type /{}", "StructTreeRoot")?; | |
write!(out, "{} ", "K")?; | |
self.children.serialize(out)?; | |
writeln!(out, "")?; | |
writeln!(out, ">>")?; | |
Ok(()) | |
} | |
fn from_primitive(p: Primitive, resolve: &Resolve) -> Result<Self> { | |
let mut dict = Dictionary::from_primitive(p, resolve)?; | |
let ty = dict | |
.remove("Type") | |
.ok_or(::pdf::Error::from(::pdf::ErrorKind::EntryNotFound { | |
key: "Type", | |
}))? | |
.to_name()?; | |
if ty != "StructTreeRoot" { | |
bail!("[Dict entry /{}] != /{}", "Type", "StructTreeRoot"); | |
} | |
let children = { | |
match dict.remove("K") { | |
Some(primitive) => { | |
match <Vec<StructElem> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"K", | |
stringify!(Vec<StructElem>) | |
))), | |
} | |
} | |
None => match <Vec<StructElem> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(children), "K"), | |
}, | |
} | |
}; | |
Ok(StructTreeRoot { children: children }) | |
} | |
} | |
impl ::pdf::object::Object for StructElem { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!(out, "<<")?; | |
write!(out, "{} ", "S")?; | |
self.struct_type.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "P")?; | |
self.parent.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "ID")?; | |
self.id.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "Pg")?; | |
self.page.serialize(out)?; | |
writeln!(out, "")?; | |
writeln!(out, ">>")?; | |
Ok(()) | |
} | |
fn from_primitive(p: Primitive, resolve: &Resolve) -> Result<Self> { | |
let mut dict = Dictionary::from_primitive(p, resolve)?; | |
let struct_type = { | |
match dict.remove("S") { | |
Some(primitive) => match <StructType as Object>::from_primitive(primitive, resolve) | |
{ | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"S", | |
stringify!(StructType) | |
))), | |
}, | |
None => match <StructType as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(struct_type), "S"), | |
}, | |
} | |
}; | |
let parent = { | |
match dict.remove("P") { | |
Some(primitive) => { | |
match <Ref<StructElem> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"P", | |
stringify!(Ref<StructElem>) | |
))), | |
} | |
} | |
None => match <Ref<StructElem> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(parent), "P"), | |
}, | |
} | |
}; | |
let id = { | |
match dict.remove("ID") { | |
Some(primitive) => { | |
match <Option<PdfString> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"ID", | |
stringify!(Option<PdfString>) | |
))), | |
} | |
} | |
None => match <Option<PdfString> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(id), "ID"), | |
}, | |
} | |
}; | |
let page = { | |
match dict.remove("Pg") { | |
Some(primitive) => { | |
match <Option<Ref<Page>> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Pg", | |
stringify!(Option<Ref<Page>>) | |
))), | |
} | |
} | |
None => match <Option<Ref<Page>> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(page), "Pg"), | |
}, | |
} | |
}; | |
Ok(StructElem { | |
struct_type: struct_type, | |
parent: parent, | |
id: id, | |
page: page, | |
}) | |
} | |
} | |
impl ::pdf::object::Object for StructType { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!( | |
out, | |
"/{}", | |
match *self { | |
StructType::Document => stringify!(Document), | |
StructType::Part => stringify!(Part), | |
StructType::Art => stringify!(Art), | |
StructType::Sect => stringify!(Sect), | |
StructType::Div => stringify!(Div), | |
StructType::BlockQuote => stringify!(BlockQuote), | |
StructType::Caption => stringify!(Caption), | |
StructType::TOC => stringify!(TOC), | |
StructType::TOCI => stringify!(TOCI), | |
StructType::Index => stringify!(Index), | |
StructType::NonStruct => stringify!(NonStruct), | |
StructType::Private => stringify!(Private), | |
StructType::Book => stringify!(Book), | |
} | |
) | |
} | |
fn from_primitive(p: Primitive, _resolve: &Resolve) -> Result<Self> { | |
Ok(match p { | |
Primitive::Name(name) => match name.as_str() { | |
stringify!(Document) => StructType::Document, | |
stringify!(Part) => StructType::Part, | |
stringify!(Art) => StructType::Art, | |
stringify!(Sect) => StructType::Sect, | |
stringify!(Div) => StructType::Div, | |
stringify!(BlockQuote) => StructType::BlockQuote, | |
stringify!(Caption) => StructType::Caption, | |
stringify!(TOC) => StructType::TOC, | |
stringify!(TOCI) => StructType::TOCI, | |
stringify!(Index) => StructType::Index, | |
stringify!(NonStruct) => StructType::NonStruct, | |
stringify!(Private) => StructType::Private, | |
stringify!(Book) => StructType::Book, | |
s => bail!(format!( | |
"Enum {} from_primitive: no variant {}.", | |
stringify!(StructType), | |
s | |
)), | |
}, | |
_ => bail!(::pdf::Error::from(::pdf::ErrorKind::UnexpectedPrimitive { | |
expected: "Name", | |
found: p.get_debug_name() | |
})), | |
}) | |
} | |
} | |
impl ::pdf::object::Object for ObjStmInfo { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!(out, "<<")?; | |
writeln!(out, "/Type /{}", "ObjStm")?; | |
write!(out, "{} ", "N")?; | |
self.num_objects.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "First")?; | |
self.first.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "Extends")?; | |
self.extends.serialize(out)?; | |
writeln!(out, "")?; | |
writeln!(out, ">>")?; | |
Ok(()) | |
} | |
fn from_primitive(p: Primitive, resolve: &Resolve) -> Result<Self> { | |
let mut dict = Dictionary::from_primitive(p, resolve)?; | |
let ty = dict | |
.remove("Type") | |
.ok_or(::pdf::Error::from(::pdf::ErrorKind::EntryNotFound { | |
key: "Type", | |
}))? | |
.to_name()?; | |
if ty != "ObjStm" { | |
bail!("[Dict entry /{}] != /{}", "Type", "ObjStm"); | |
} | |
let num_objects = { | |
match dict.remove("N") { | |
Some(primitive) => match <i32 as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"N", | |
stringify!(i32) | |
))), | |
}, | |
None => match <i32 as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(num_objects), "N"), | |
}, | |
} | |
}; | |
let first = { | |
match dict.remove("First") { | |
Some(primitive) => match <i32 as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"First", | |
stringify!(i32) | |
))), | |
}, | |
None => match <i32 as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(first), "First"), | |
}, | |
} | |
}; | |
let extends = { | |
match dict.remove("Extends") { | |
Some(primitive) => { | |
match <Option<i32> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Extends", | |
stringify!(Option<i32>) | |
))), | |
} | |
} | |
None => match <Option<i32> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!( | |
"Object {}, Key {} not found", | |
stringify!(extends), | |
"Extends" | |
), | |
}, | |
} | |
}; | |
Ok(ObjStmInfo { | |
num_objects: num_objects, | |
first: first, | |
extends: extends, | |
}) | |
} | |
} | |
impl ::pdf::object::Object for Trailer { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!(out, "<<")?; | |
write!(out, "{} ", "Size")?; | |
self.highest_id.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "Prev")?; | |
self.prev_trailer_pos.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "Root")?; | |
self.root.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "Encrypt")?; | |
self.encrypt_dict.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "Info")?; | |
self.info_dict.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "ID")?; | |
self.id.serialize(out)?; | |
writeln!(out, "")?; | |
writeln!(out, ">>")?; | |
Ok(()) | |
} | |
fn from_primitive(p: Primitive, resolve: &Resolve) -> Result<Self> { | |
let mut dict = Dictionary::from_primitive(p, resolve)?; | |
let highest_id = { | |
match dict.remove("Size") { | |
Some(primitive) => match <i32 as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Size", | |
stringify!(i32) | |
))), | |
}, | |
None => match <i32 as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!( | |
"Object {}, Key {} not found", | |
stringify!(highest_id), | |
"Size" | |
), | |
}, | |
} | |
}; | |
let prev_trailer_pos = { | |
match dict.remove("Prev") { | |
Some(primitive) => { | |
match <Option<i32> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Prev", | |
stringify!(Option<i32>) | |
))), | |
} | |
} | |
None => match <Option<i32> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!( | |
"Object {}, Key {} not found", | |
stringify!(prev_trailer_pos), | |
"Prev" | |
), | |
}, | |
} | |
}; | |
let root = { | |
match dict.remove("Root") { | |
Some(primitive) => match <Catalog as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Root", | |
stringify!(Catalog) | |
))), | |
}, | |
None => match <Catalog as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(root), "Root"), | |
}, | |
} | |
}; | |
let encrypt_dict = { | |
match dict.remove("Encrypt") { | |
Some(primitive) => { | |
match <Option<Dictionary> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Encrypt", | |
stringify!(Option<Dictionary>) | |
))), | |
} | |
} | |
None => match <Option<Dictionary> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!( | |
"Object {}, Key {} not found", | |
stringify!(encrypt_dict), | |
"Encrypt" | |
), | |
}, | |
} | |
}; | |
let info_dict = { | |
match dict.remove("Info") { | |
Some(primitive) => { | |
match <Option<Dictionary> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Info", | |
stringify!(Option<Dictionary>) | |
))), | |
} | |
} | |
None => match <Option<Dictionary> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(info_dict), "Info"), | |
}, | |
} | |
}; | |
let id = { | |
match dict.remove("ID") { | |
Some(primitive) => { | |
match <Vec<PdfString> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"ID", | |
stringify!(Vec<PdfString>) | |
))), | |
} | |
} | |
None => match <Vec<PdfString> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(id), "ID"), | |
}, | |
} | |
}; | |
Ok(Trailer { | |
highest_id: highest_id, | |
prev_trailer_pos: prev_trailer_pos, | |
root: root, | |
encrypt_dict: encrypt_dict, | |
info_dict: info_dict, | |
id: id, | |
}) | |
} | |
} | |
impl ::pdf::object::Object for XRefInfo { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!(out, "<<")?; | |
writeln!(out, "/Type /{}", "XRef")?; | |
write!(out, "{} ", "Size")?; | |
self.size.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "Index")?; | |
self.index.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "Prev")?; | |
self.prev.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "W")?; | |
self.w.serialize(out)?; | |
writeln!(out, "")?; | |
writeln!(out, ">>")?; | |
Ok(()) | |
} | |
fn from_primitive(p: Primitive, resolve: &Resolve) -> Result<Self> { | |
let mut dict = Dictionary::from_primitive(p, resolve)?; | |
let ty = dict | |
.remove("Type") | |
.ok_or(::pdf::Error::from(::pdf::ErrorKind::EntryNotFound { | |
key: "Type", | |
}))? | |
.to_name()?; | |
if ty != "XRef" { | |
bail!("[Dict entry /{}] != /{}", "Type", "XRef"); | |
} | |
let size = { | |
match dict.remove("Size") { | |
Some(primitive) => match <i32 as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Size", | |
stringify!(i32) | |
))), | |
}, | |
None => match <i32 as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(size), "Size"), | |
}, | |
} | |
}; | |
let index = { | |
let primitive: Option<::pdf::primitive::Primitive> = dict.remove("Index"); | |
let x: Vec<i32> = match primitive { | |
Some(primitive) => <Vec<i32> as Object>::from_primitive(primitive, resolve) | |
.chain_err(|| stringify!(index))?, | |
None => vec![0, size], | |
}; | |
x | |
}; | |
let prev = { | |
match dict.remove("Prev") { | |
Some(primitive) => { | |
match <Option<i32> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"Prev", | |
stringify!(Option<i32>) | |
))), | |
} | |
} | |
None => match <Option<i32> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(prev), "Prev"), | |
}, | |
} | |
}; | |
let w = { | |
match dict.remove("W") { | |
Some(primitive) => match <Vec<i32> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"W", | |
stringify!(Vec<i32>) | |
))), | |
}, | |
None => match <Vec<i32> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!("Object {}, Key {} not found", stringify!(w), "W"), | |
}, | |
} | |
}; | |
Ok(XRefInfo { | |
size: size, | |
index: index, | |
prev: prev, | |
w: w, | |
}) | |
} | |
} | |
impl ::pdf::object::Object for LZWFlateParams { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!(out, "<<")?; | |
write!(out, "{} ", "Predictor")?; | |
self.predictor.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "Colors")?; | |
self.n_components.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "BitsPerComponent")?; | |
self.bits_per_component.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "Columns")?; | |
self.columns.serialize(out)?; | |
writeln!(out, "")?; | |
write!(out, "{} ", "EarlyChange")?; | |
self.early_change.serialize(out)?; | |
writeln!(out, "")?; | |
writeln!(out, ">>")?; | |
Ok(()) | |
} | |
fn from_primitive(p: Primitive, resolve: &Resolve) -> Result<Self> { | |
let mut dict = Dictionary::from_primitive(p, resolve)?; | |
let predictor = { | |
let primitive: Option<::pdf::primitive::Primitive> = dict.remove("Predictor"); | |
let x: i32 = match primitive { | |
Some(primitive) => <i32 as Object>::from_primitive(primitive, resolve) | |
.chain_err(|| stringify!(predictor))?, | |
None => 1, | |
}; | |
x | |
}; | |
let n_components = { | |
let primitive: Option<::pdf::primitive::Primitive> = dict.remove("Colors"); | |
let x: i32 = match primitive { | |
Some(primitive) => <i32 as Object>::from_primitive(primitive, resolve) | |
.chain_err(|| stringify!(n_components))?, | |
None => 1, | |
}; | |
x | |
}; | |
let bits_per_component = { | |
let primitive: Option<::pdf::primitive::Primitive> = dict.remove("BitsPerComponent"); | |
let x: i32 = match primitive { | |
Some(primitive) => <i32 as Object>::from_primitive(primitive, resolve) | |
.chain_err(|| stringify!(bits_per_component))?, | |
None => 8, | |
}; | |
x | |
}; | |
let columns = { | |
let primitive: Option<::pdf::primitive::Primitive> = dict.remove("Columns"); | |
let x: i32 = match primitive { | |
Some(primitive) => <i32 as Object>::from_primitive(primitive, resolve) | |
.chain_err(|| stringify!(columns))?, | |
None => 1, | |
}; | |
x | |
}; | |
let early_change = { | |
let primitive: Option<::pdf::primitive::Primitive> = dict.remove("EarlyChange"); | |
let x: i32 = match primitive { | |
Some(primitive) => <i32 as Object>::from_primitive(primitive, resolve) | |
.chain_err(|| stringify!(early_change))?, | |
None => 1, | |
}; | |
x | |
}; | |
Ok(LZWFlateParams { | |
predictor: predictor, | |
n_components: n_components, | |
bits_per_component: bits_per_component, | |
columns: columns, | |
early_change: early_change, | |
}) | |
} | |
} | |
impl ::pdf::object::Object for DCTDecodeParams { | |
fn serialize<W: ::std::io::Write>(&self, out: &mut W) -> ::std::io::Result<()> { | |
writeln!(out, "<<")?; | |
write!(out, "{} ", "ColorTransform")?; | |
self.color_transform.serialize(out)?; | |
writeln!(out, "")?; | |
writeln!(out, ">>")?; | |
Ok(()) | |
} | |
fn from_primitive(p: Primitive, resolve: &Resolve) -> Result<Self> { | |
let mut dict = Dictionary::from_primitive(p, resolve)?; | |
let color_transform = { | |
match dict.remove("ColorTransform") { | |
Some(primitive) => { | |
match <Option<i32> as Object>::from_primitive(primitive, resolve) { | |
Ok(obj) => obj, | |
Err(e) => bail!(e.chain_err(|| format!( | |
"Key {}: cannot convert from primitive to type {}", | |
"ColorTransform", | |
stringify!(Option<i32>) | |
))), | |
} | |
} | |
None => match <Option<i32> as Object>::from_primitive( | |
::pdf::primitive::Primitive::Null, | |
resolve, | |
) { | |
Ok(obj) => obj, | |
Err(_) => bail!( | |
"Object {}, Key {} not found", | |
stringify!(color_transform), | |
"ColorTransform" | |
), | |
}, | |
} | |
}; | |
Ok(DCTDecodeParams { | |
color_transform: color_transform, | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment