diff options
author | Tor Andersson <tor.andersson@artifex.com> | 2013-07-12 16:44:54 +0200 |
---|---|---|
committer | Tor Andersson <tor.andersson@artifex.com> | 2013-07-12 16:44:54 +0200 |
commit | 3c559928d88fccfe17da4953ea1c93ceb42a90cb (patch) | |
tree | 04cdcbfa3ea952c4f7053edff72fece4aa235ab6 /source | |
parent | bc8b6cc1e73686a97a3b2ea3160ec851c850d3f1 (diff) | |
download | mupdf-3c559928d88fccfe17da4953ea1c93ceb42a90cb.tar.xz |
Set /Parent entry when inserting a page into the page tree.
Diffstat (limited to 'source')
-rw-r--r-- | source/pdf/pdf-page.c | 64 |
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); } } |