summaryrefslogtreecommitdiff
path: root/source/pdf/pdf-page.c
diff options
context:
space:
mode:
Diffstat (limited to 'source/pdf/pdf-page.c')
-rw-r--r--source/pdf/pdf-page.c64
1 files changed, 41 insertions, 23 deletions
diff --git a/source/pdf/pdf-page.c b/source/pdf/pdf-page.c
index 66e72bc7..7de81f00 100644
--- a/source/pdf/pdf-page.c
+++ b/source/pdf/pdf-page.c
@@ -514,39 +514,57 @@ pdf_delete_page(pdf_document *doc, int at)
void
pdf_insert_page(pdf_document *doc, pdf_page *page, int at)
{
+ fz_context *ctx = doc->ctx;
int count = pdf_count_pages(doc);
pdf_obj *parent, *kids;
+ pdf_obj *page_ref;
int i;
- if (count == 0)
- {
- /* TODO: create new page tree? */
- fz_throw(doc->ctx, FZ_ERROR_GENERIC, "empty page tree, cannot insert page");
- }
- else if (at >= count)
+ page_ref = pdf_new_ref(doc, page->me);
+
+ fz_try(ctx)
{
- if (at > count)
- fz_throw(doc->ctx, FZ_ERROR_GENERIC, "cannot insert page beyond end of page tree");
+ if (count == 0)
+ {
+ /* TODO: create new page tree? */
+ fz_throw(ctx, FZ_ERROR_GENERIC, "empty page tree, cannot insert page");
+ }
+ else if (at >= count)
+ {
+ if (at > count)
+ fz_throw(ctx, FZ_ERROR_GENERIC, "cannot insert page beyond end of page tree");
+
+ /* append after last page */
+ pdf_lookup_page_loc(doc, count - 1, &parent, &i);
+ kids = pdf_dict_gets(parent, "Kids");
+ pdf_array_insert(kids, page_ref, i + 1);
+ }
+ else
+ {
+ /* insert before found page */
+ pdf_lookup_page_loc(doc, at, &parent, &i);
+ kids = pdf_dict_gets(parent, "Kids");
+ pdf_array_insert(kids, page_ref, i);
+ }
+
+ pdf_dict_puts(page->me, "Parent", page_ref);
+
+ /* Adjust page counts */
+ while (parent)
+ {
+ int count = pdf_to_int(pdf_dict_gets(parent, "Count"));
+ pdf_dict_puts_drop(parent, "Count", pdf_new_int(doc, count + 1));
+ parent = pdf_dict_gets(parent, "Parent");
+ }
- /* append after last page */
- pdf_lookup_page_loc(doc, count - 1, &parent, &i);
- kids = pdf_dict_gets(parent, "Kids");
- pdf_array_insert_drop(kids, pdf_new_ref(doc, page->me), i + 1);
}
- else
+ fz_always(ctx)
{
- /* insert before found page */
- pdf_lookup_page_loc(doc, at, &parent, &i);
- kids = pdf_dict_gets(parent, "Kids");
- pdf_array_insert_drop(kids, pdf_new_ref(doc, page->me), i);
+ pdf_drop_obj(page_ref);
}
-
- /* Adjust page counts */
- while (parent)
+ fz_catch(ctx)
{
- int count = pdf_to_int(pdf_dict_gets(parent, "Count"));
- pdf_dict_puts_drop(parent, "Count", pdf_new_int(doc, count + 1));
- parent = pdf_dict_gets(parent, "Parent");
+ fz_rethrow(ctx);
}
}