Skip to content

Commit

Permalink
some hacks to reuse existing streams
Browse files Browse the repository at this point in the history
  • Loading branch information
s3bk committed Dec 15, 2024
1 parent e113662 commit 1bde313
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
12 changes: 6 additions & 6 deletions examples/src/bin/add_annot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn run() -> Result<(), PdfError> {
// right: 300.961,
// top: 742.55,
// }),
rect: None,
rect: Some(Rectangle { left: 10., bottom: 10., right: 200., top: 200. }),
contents: None,
page: Some(old_page.clone()),
border: None,
Expand All @@ -61,10 +61,10 @@ fn run() -> Result<(), PdfError> {
ink_list: None,
line: Some(Primitive::Array(
vec![
Primitive::Number(95.774),
Primitive::Number(734.237),
Primitive::Number(320.961),
Primitive::Number(734.863)
Primitive::Number(10.),
Primitive::Number(100.),
Primitive::Number(20.),
Primitive::Number(200.)
]
)),
// creation_date: None,
Expand Down Expand Up @@ -98,7 +98,7 @@ fn run() -> Result<(), PdfError> {
old_file.update_ref(&r, annots).unwrap();
}
}
old_file.save_to("/Users/apple/Downloads/test_pdf/out.pdf")?;
old_file.save_to("out.pdf")?;

Ok(())
}
Expand Down
5 changes: 4 additions & 1 deletion pdf/src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,10 @@ impl ObjectWrite for Content {
fn to_primitive(&self, update: &mut impl Updater) -> Result<Primitive> {
if self.parts.len() == 1 {
let obj = self.parts[0].to_primitive(update)?;
update.create(obj)?.to_primitive(update)
match obj {
Primitive::Reference(_) => Ok(obj),
_ => update.create(obj)?.to_primitive(update)
}
} else {
self.parts.to_primitive(update)
}
Expand Down
5 changes: 4 additions & 1 deletion pdf/src/object/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ impl<I: ObjectWrite> Stream<I> {
}
impl<I: ObjectWrite> ObjectWrite for Stream<I> {
fn to_primitive(&self, update: &mut impl Updater) -> Result<Primitive> {
self.to_pdf_stream(update).map(Primitive::Stream)
match self.inner_data {
StreamData::Original(_, id) => Ok(Primitive::Reference(id)),
_ => self.to_pdf_stream(update).map(Primitive::Stream),
}
}
}
impl<I: DeepClone> DeepClone for Stream<I> {
Expand Down
15 changes: 9 additions & 6 deletions pdf/src/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,23 +268,26 @@ impl Object for PdfStream {
}
impl ObjectWrite for PdfStream {
fn to_primitive(&self, update: &mut impl Updater) -> Result<Primitive> {
Ok(self.clone().into())
match self.inner {
StreamInner::InFile { id, .. } => Ok(Primitive::Reference(id)),
StreamInner::Pending { .. } => Ok(self.clone().into()),
}
}
}
impl PdfStream {
pub fn serialize(&self, out: &mut impl io::Write) -> Result<()> {
self.info.serialize(out)?;

writeln!(out, "stream")?;
match self.inner {
StreamInner::InFile { .. } => {
unimplemented!()
StreamInner::InFile { id, .. } => {
Primitive::Reference(id).serialize(out)?;
}
StreamInner::Pending { ref data } => {
self.info.serialize(out)?;
writeln!(out, "stream")?;
out.write_all(data)?;
writeln!(out, "\nendstream")?;
}
}
writeln!(out, "\nendstream")?;
Ok(())
}
pub fn raw_data(&self, resolve: &impl Resolve) -> Result<Arc<[u8]>> {
Expand Down

0 comments on commit 1bde313

Please sign in to comment.