Skip to content

Commit e113662

Browse files
committed
fix things
1 parent 1c170a2 commit e113662

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

examples/src/bin/add_annot.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ fn run() -> Result<(), PdfError> {
1818
let mut old_file = FileOptions::cached().open(&path)?;
1919
let mut old_page: PageRc = old_file.get_page(0).unwrap();
2020

21-
let mut annots = old_page.annotations.load(&old_file.resolver()).expect("can't load annotations");
21+
let old_annots = old_page.annotations.load(&old_file.resolver()).expect("can't load annotations");
22+
let mut annots: Vec<_> = (*old_annots).clone();
2223
// let mut new_annots = annots.deref().clone();
2324
// for annot in &new_annots {
2425
// dbg!(&annot.subtype);
@@ -79,23 +80,24 @@ fn run() -> Result<(), PdfError> {
7980
let annot_ref = old_file.create(new_annot)?;
8081
annots.push(MaybeRef::Indirect(annot_ref));
8182

82-
// let lazy_annots = Lazy::from_primitive(
83-
// annots.to_primitive(&mut FileOptions::cached().storage()).unwrap(),
84-
// &file.resolver()
85-
// );
8683

87-
// old_page.update_annots(annots, &old_file.resolver(), &mut FileOptions::cached().storage());
88-
// let old_annots = old_page.annotations.to_primitive(&mut old_file).unwrap();
84+
match old_annots {
85+
MaybeRef::Direct(_) => {
86+
// need to update the whole page
87+
let mut new_page: Page = (*old_page).clone();
8988

90-
91-
// let layz_annots = Lazy::from(annots);
92-
// match annots {
93-
// MaybeRef::Indirect(annot) => {
94-
// old_page.annotations = Lazy::from(annot);
95-
// }
96-
// }
97-
98-
old_file.update(old_page.get_plain_ref(), old_page);
89+
let lazy_annots = Lazy::safe(
90+
MaybeRef::Indirect(old_file.create(annots).unwrap()),
91+
&mut old_file
92+
).unwrap();
93+
new_page.annotations = lazy_annots;
94+
PageRc::update(new_page, &old_page, &mut old_file).unwrap();
95+
}
96+
MaybeRef::Indirect(r) => {
97+
// can just update the annot reference
98+
old_file.update_ref(&r, annots).unwrap();
99+
}
100+
}
99101
old_file.save_to("/Users/apple/Downloads/test_pdf/out.pdf")?;
100102

101103
Ok(())
@@ -105,4 +107,4 @@ fn main() {
105107
if let Err(e) = run() {
106108
println!("{e}");
107109
}
108-
}
110+
}

pdf/src/object/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ pub trait DeepClone: Sized + Sync + Send + 'static {
108108
pub trait Updater {
109109
fn create<T: ObjectWrite>(&mut self, obj: T) -> Result<RcRef<T>>;
110110
fn update<T: ObjectWrite>(&mut self, old: PlainRef, obj: T) -> Result<RcRef<T>>;
111+
fn update_ref<T: ObjectWrite>(&mut self, old: &RcRef<T>, obj: T) -> Result<RcRef<T>> {
112+
self.update(old.get_ref().inner, obj)
113+
}
111114
fn promise<T: Object>(&mut self) -> PromisedRef<T>;
112115
fn fulfill<T: ObjectWrite>(&mut self, promise: PromisedRef<T>, obj: T) -> Result<RcRef<T>>;
113116
}
@@ -439,6 +442,12 @@ impl<T: Object> Lazy<T> {
439442
pub fn load(&self, resolve: &impl Resolve) -> Result<T> {
440443
T::from_primitive(self.primitive.clone(), resolve)
441444
}
445+
pub fn safe(value: T, update: &mut impl Updater) -> Result<Self>
446+
where T: ObjectWrite
447+
{
448+
let primitive = value.to_primitive(update)?;
449+
Ok(Lazy { primitive, _marker: PhantomData })
450+
}
442451
}
443452
impl<T: Object> Object for Lazy<T> {
444453
fn from_primitive(p: Primitive, _: &impl Resolve) -> Result<Self> {
@@ -462,7 +471,6 @@ impl<T> From<RcRef<T>> for Lazy<T> {
462471
}
463472
}
464473

465-
466474
//////////////////////////////////////
467475
// Object for Primitives & other types
468476
//////////////////////////////////////

pdf/src/object/types.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ impl PageRc {
6868
pub fn create(page: Page, update: &mut impl Updater) -> Result<PageRc> {
6969
Ok(PageRc(update.create(PagesNode::Leaf(page))?))
7070
}
71+
pub fn update(page: Page, old_page: &PageRc, update: &mut impl Updater) -> Result<PageRc> {
72+
update.update(old_page.get_plain_ref(), PagesNode::Leaf(page)).map(PageRc)
73+
}
7174
pub fn get_ref(&self) -> Ref<PagesNode> {
7275
self.0.get_ref()
7376
}

0 commit comments

Comments
 (0)