diff options
author | Robin Watts <robin.watts@artifex.com> | 2014-02-10 16:18:46 +0000 |
---|---|---|
committer | Robin Watts <robin.watts@artifex.com> | 2014-02-10 16:58:28 +0000 |
commit | addbdd3bc39e3ed07259706e896f275a9f35ae9e (patch) | |
tree | 5d9adda7416d0a797f80128936c4614de8ef8cf6 | |
parent | 7eddc1c63affc10e9e19b5f0de0a62556c36860e (diff) | |
download | mupdf-addbdd3bc39e3ed07259706e896f275a9f35ae9e.tar.xz |
Bug 695021: Fix pdf_insert_page operation with empty page tree.
Patch from Thomas Fach-Pedersen to fix the operation of pdf_insert_page
when called with an empty page tree. Many thanks! As noted in the code
with a FIXME this currently throws an error.
Also, cope with being told to add a page "at" INT_MAX as meaning to
add it at the end of the document.
Possibly this code should cope with a Root without a Pages entry, or
a Pages without a Kids too, but we can fix this in future if it ever
becomes a problem.
-rw-r--r-- | source/pdf/pdf-page.c | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/source/pdf/pdf-page.c b/source/pdf/pdf-page.c index f3c929d2..fd6f71bc 100644 --- a/source/pdf/pdf-page.c +++ b/source/pdf/pdf-page.c @@ -590,11 +590,22 @@ pdf_insert_page(pdf_document *doc, pdf_page *page, int at) { if (count == 0) { - /* TODO: create new page tree? */ - fz_throw(ctx, FZ_ERROR_GENERIC, "empty page tree, cannot insert page"); + pdf_obj *root = pdf_dict_gets(pdf_trailer(doc), "Root"); + parent = pdf_dict_gets(root, "Pages"); + if (!parent) + fz_throw(doc->ctx, FZ_ERROR_GENERIC, "cannot find page tree"); + + kids = pdf_dict_gets(parent, "Kids"); + if (!kids) + fz_throw(doc->ctx, FZ_ERROR_GENERIC, "malformed page tree"); + + pdf_array_insert(kids, page_ref, 0); } else if (at >= count) { + if (at == INT_MAX) + at = count; + if (at > count) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot insert page beyond end of page tree"); |