Skip to content

Commit

Permalink
fix things
Browse files Browse the repository at this point in the history
  • Loading branch information
s3bk committed Dec 14, 2024
1 parent 1c170a2 commit e113662
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
36 changes: 19 additions & 17 deletions examples/src/bin/add_annot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ fn run() -> Result<(), PdfError> {
let mut old_file = FileOptions::cached().open(&path)?;
let mut old_page: PageRc = old_file.get_page(0).unwrap();

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

// let lazy_annots = Lazy::from_primitive(
// annots.to_primitive(&mut FileOptions::cached().storage()).unwrap(),
// &file.resolver()
// );

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


// let layz_annots = Lazy::from(annots);
// match annots {
// MaybeRef::Indirect(annot) => {
// old_page.annotations = Lazy::from(annot);
// }
// }

old_file.update(old_page.get_plain_ref(), old_page);
let lazy_annots = Lazy::safe(
MaybeRef::Indirect(old_file.create(annots).unwrap()),
&mut old_file
).unwrap();
new_page.annotations = lazy_annots;
PageRc::update(new_page, &old_page, &mut old_file).unwrap();
}
MaybeRef::Indirect(r) => {
// can just update the annot reference
old_file.update_ref(&r, annots).unwrap();
}
}
old_file.save_to("/Users/apple/Downloads/test_pdf/out.pdf")?;

Ok(())
Expand All @@ -105,4 +107,4 @@ fn main() {
if let Err(e) = run() {
println!("{e}");
}
}
}
10 changes: 9 additions & 1 deletion pdf/src/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ pub trait DeepClone: Sized + Sync + Send + 'static {
pub trait Updater {
fn create<T: ObjectWrite>(&mut self, obj: T) -> Result<RcRef<T>>;
fn update<T: ObjectWrite>(&mut self, old: PlainRef, obj: T) -> Result<RcRef<T>>;
fn update_ref<T: ObjectWrite>(&mut self, old: &RcRef<T>, obj: T) -> Result<RcRef<T>> {
self.update(old.get_ref().inner, obj)
}
fn promise<T: Object>(&mut self) -> PromisedRef<T>;
fn fulfill<T: ObjectWrite>(&mut self, promise: PromisedRef<T>, obj: T) -> Result<RcRef<T>>;
}
Expand Down Expand Up @@ -439,6 +442,12 @@ impl<T: Object> Lazy<T> {
pub fn load(&self, resolve: &impl Resolve) -> Result<T> {
T::from_primitive(self.primitive.clone(), resolve)
}
pub fn safe(value: T, update: &mut impl Updater) -> Result<Self>
where T: ObjectWrite
{
let primitive = value.to_primitive(update)?;
Ok(Lazy { primitive, _marker: PhantomData })
}
}
impl<T: Object> Object for Lazy<T> {
fn from_primitive(p: Primitive, _: &impl Resolve) -> Result<Self> {
Expand All @@ -462,7 +471,6 @@ impl<T> From<RcRef<T>> for Lazy<T> {
}
}


//////////////////////////////////////
// Object for Primitives & other types
//////////////////////////////////////
Expand Down
3 changes: 3 additions & 0 deletions pdf/src/object/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ impl PageRc {
pub fn create(page: Page, update: &mut impl Updater) -> Result<PageRc> {
Ok(PageRc(update.create(PagesNode::Leaf(page))?))
}
pub fn update(page: Page, old_page: &PageRc, update: &mut impl Updater) -> Result<PageRc> {
update.update(old_page.get_plain_ref(), PagesNode::Leaf(page)).map(PageRc)
}
pub fn get_ref(&self) -> Ref<PagesNode> {
self.0.get_ref()
}
Expand Down

0 comments on commit e113662

Please sign in to comment.