diff options
author | Vincent Sanders <vince@kyllikki.org> | 2018-01-05 15:40:09 +0000 |
---|---|---|
committer | Vincent Sanders <vince@kyllikki.org> | 2018-01-05 15:40:09 +0000 |
commit | ad6da4a71f2b837a791401e658a16bf6903fd3b5 (patch) | |
tree | 761f5e5a80a907721ff596f7f82cec85a160edad | |
parent | a65babe2fbd341f9d12a50a9530682ef1e015b58 (diff) | |
download | libnspdf-ad6da4a71f2b837a791401e658a16bf6903fd3b5.tar.gz libnspdf-ad6da4a71f2b837a791401e658a16bf6903fd3b5.tar.bz2 |
split out page handling into new module
-rw-r--r-- | include/nspdf/page.h | 24 | ||||
-rw-r--r-- | src/Makefile | 2 | ||||
-rw-r--r-- | src/document.c | 113 | ||||
-rw-r--r-- | src/page.c | 138 | ||||
-rw-r--r-- | src/pdf_doc.h | 2 | ||||
-rw-r--r-- | test/parsepdf.c | 10 |
6 files changed, 176 insertions, 113 deletions
diff --git a/include/nspdf/page.h b/include/nspdf/page.h new file mode 100644 index 0000000..119ef22 --- /dev/null +++ b/include/nspdf/page.h @@ -0,0 +1,24 @@ +/* + * Copyright 2018 Vincent Sanders <vince@netsurf-browser.org> + * + * This file is part of libnspdf. + * + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + */ + +/** + * \file + * NetSurf PDF library page manipulation. + */ + +#ifndef NSPDF_PAGE_H_ +#define NSPDF_PAGE_H_ + +#include <nspdf/errors.h> + +struct nspdf_doc; + +nspdferror nspdf_count_pages(struct nspdf_doc *doc, unsigned int *pages_out); + +#endif /* NSPDF_META_H_ */ diff --git a/src/Makefile b/src/Makefile index c4ddc7f..09bde65 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,3 +1,3 @@ -DIR_SOURCES := document.c byte_class.c cos_parse.c cos_object.c pdf_doc.c meta.c +DIR_SOURCES := document.c byte_class.c cos_parse.c cos_object.c pdf_doc.c meta.c page.c include $(NSBUILD)/Makefile.subdir diff --git a/src/document.c b/src/document.c index cef2c1a..3dea95e 100644 --- a/src/document.c +++ b/src/document.c @@ -461,117 +461,6 @@ static nspdferror decode_trailers(struct nspdf_doc *doc) } -/** - * recursively decodes a page tree - */ -static nspdferror -decode_page_tree(struct nspdf_doc *doc, - struct cos_object *page_tree_node, - unsigned int *page_index) -{ - nspdferror res; - const char *type; - - // Type = Pages - res = cos_get_dictionary_name(doc, page_tree_node, "Type", &type); - if (res != NSPDFERROR_OK) { - return res; - } - - if (strcmp(type, "Pages") == 0) { - struct cos_object *kids; - unsigned int kids_size; - unsigned int kids_index; - - if (doc->page_table == NULL) { - /* allocate top level page table */ - int64_t count; - - res = cos_get_dictionary_int(doc, page_tree_node, "Count", &count); - if (res != NSPDFERROR_OK) { - return res; - } - - doc->page_table = calloc(count, sizeof(struct page_table_entry)); - if (doc->page_table == NULL) { - return NSPDFERROR_NOMEM; - } - doc->page_table_size = count; - } - - res = cos_get_dictionary_array(doc, page_tree_node, "Kids", &kids); - if (res != NSPDFERROR_OK) { - return res; - } - - res = cos_get_array_size(doc, kids, &kids_size); - if (res != NSPDFERROR_OK) { - return res; - } - - for (kids_index = 0; kids_index < kids_size; kids_index++) { - struct cos_object *kid; - - res = cos_get_array_dictionary(doc, kids, kids_index, &kid); - if (res != NSPDFERROR_OK) { - return res; - } - - res = decode_page_tree(doc, kid, page_index); - if (res != NSPDFERROR_OK) { - return res; - } - } - - } else if (strcmp(type, "Page") == 0) { - struct page_table_entry *page; - - page = doc->page_table + (*page_index); - - /* required heritable resources */ - res = cos_heritable_dictionary_dictionary(doc, - page_tree_node, - "Resources", - &(page->resources)); - if (res != NSPDFERROR_OK) { - return res; - } - - /* required heritable mediabox */ - res = cos_heritable_dictionary_array(doc, - page_tree_node, - "MediaBox", - &(page->mediabox)); - if (res != NSPDFERROR_OK) { - return res; - } - - /* optional page contents */ - res = cos_get_dictionary_value(doc, - page_tree_node, - "Contents", - &(page->contents)); - if ((res != NSPDFERROR_OK) && - (res != NSPDFERROR_NOTFOUND)) { - return res; - } - - /* - printf("page index:%d page:%p resources:%p mediabox:%p contents:%p\n", - *page_index, - page, - page->resources, - page->mediabox, - page->contents); - */ - - (*page_index)++; - res = NSPDFERROR_OK; - } else { - res = NSPDFERROR_FORMAT; - } - return res; -} static nspdferror decode_catalog(struct nspdf_doc *doc) @@ -602,7 +491,7 @@ static nspdferror decode_catalog(struct nspdf_doc *doc) return res; } - res = decode_page_tree(doc, pages, &page_index); + res = nspdf__decode_page_tree(doc, pages, &page_index); if (res != NSPDFERROR_OK) { return res; } diff --git a/src/page.c b/src/page.c new file mode 100644 index 0000000..bca6dbc --- /dev/null +++ b/src/page.c @@ -0,0 +1,138 @@ +/* + * Copyright 2018 Vincent Sanders <vince@netsurf-browser.org> + * + * This file is part of libnspdf. + * + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + */ + +#include <stdint.h> +#include <stdbool.h> +#include <unistd.h> +#include <stdlib.h> +#include <string.h> +#include <nspdf/page.h> + +#include "cos_object.h" +#include "pdf_doc.h" + +/** + * recursively decodes a page tree + */ +nspdferror +nspdf__decode_page_tree(struct nspdf_doc *doc, + struct cos_object *page_tree_node, + unsigned int *page_index) +{ + nspdferror res; + const char *type; + + // Type = Pages + res = cos_get_dictionary_name(doc, page_tree_node, "Type", &type); + if (res != NSPDFERROR_OK) { + return res; + } + + if (strcmp(type, "Pages") == 0) { + struct cos_object *kids; + unsigned int kids_size; + unsigned int kids_index; + + if (doc->page_table == NULL) { + /* allocate top level page table */ + int64_t count; + + res = cos_get_dictionary_int(doc, page_tree_node, "Count", &count); + if (res != NSPDFERROR_OK) { + return res; + } + + doc->page_table = calloc(count, sizeof(struct page_table_entry)); + if (doc->page_table == NULL) { + return NSPDFERROR_NOMEM; + } + doc->page_table_size = count; + } + + res = cos_get_dictionary_array(doc, page_tree_node, "Kids", &kids); + if (res != NSPDFERROR_OK) { + return res; + } + + res = cos_get_array_size(doc, kids, &kids_size); + if (res != NSPDFERROR_OK) { + return res; + } + + for (kids_index = 0; kids_index < kids_size; kids_index++) { + struct cos_object *kid; + + res = cos_get_array_dictionary(doc, kids, kids_index, &kid); + if (res != NSPDFERROR_OK) { + return res; + } + + res = nspdf__decode_page_tree(doc, kid, page_index); + if (res != NSPDFERROR_OK) { + return res; + } + } + + } else if (strcmp(type, "Page") == 0) { + struct page_table_entry *page; + + page = doc->page_table + (*page_index); + + /* required heritable resources */ + res = cos_heritable_dictionary_dictionary(doc, + page_tree_node, + "Resources", + &(page->resources)); + if (res != NSPDFERROR_OK) { + return res; + } + + /* required heritable mediabox */ + res = cos_heritable_dictionary_array(doc, + page_tree_node, + "MediaBox", + &(page->mediabox)); + if (res != NSPDFERROR_OK) { + return res; + } + + /* optional page contents */ + res = cos_get_dictionary_value(doc, + page_tree_node, + "Contents", + &(page->contents)); + if ((res != NSPDFERROR_OK) && + (res != NSPDFERROR_NOTFOUND)) { + return res; + } + + /* + printf("page index:%d page:%p resources:%p mediabox:%p contents:%p\n", + *page_index, + page, + page->resources, + page->mediabox, + page->contents); + */ + + (*page_index)++; + res = NSPDFERROR_OK; + } else { + res = NSPDFERROR_FORMAT; + } + return res; +} + +/* exported interface documented in nspdf/page.h */ +nspdferror +nspdf_count_pages(struct nspdf_doc *doc, unsigned int *pages_out) +{ + *pages_out = doc->page_table_size; + return NSPDFERROR_OK; +} diff --git a/src/pdf_doc.h b/src/pdf_doc.h index e9bdc14..a75c90e 100644 --- a/src/pdf_doc.h +++ b/src/pdf_doc.h @@ -49,3 +49,5 @@ nspdferror doc_skip_ws(struct nspdf_doc *doc, uint64_t *offset); nspdferror doc_skip_eol(struct nspdf_doc *doc, uint64_t *offset); nspdferror xref_get_referenced(struct nspdf_doc *doc, struct cos_object **cobj_out); + +nspdferror nspdf__decode_page_tree(struct nspdf_doc *doc, struct cos_object *page_tree_node, unsigned int *page_index); diff --git a/test/parsepdf.c b/test/parsepdf.c index 1d7a086..ad6c6c4 100644 --- a/test/parsepdf.c +++ b/test/parsepdf.c @@ -18,6 +18,7 @@ #include <nspdf/document.h> #include <nspdf/meta.h> +#include <nspdf/page.h> static nspdferror read_whole_pdf(const char *fname, uint8_t **buffer, uint64_t *buffer_length) @@ -62,6 +63,7 @@ int main(int argc, char **argv) struct nspdf_doc *doc; nspdferror res; struct lwc_string_s *title; + unsigned int page_count; if (argc < 2) { fprintf(stderr, "Usage %s <filename>\n", argv[0]); @@ -91,6 +93,14 @@ int main(int argc, char **argv) printf("Title:%s\n", lwc_string_data(title)); } + res = nspdf_count_pages(doc, &page_count); + if (res != NSPDFERROR_OK) { + printf("page count failed (%d)\n", res); + return res; + } + printf("Pages:%d\n", page_count); + + res = nspdf_document_destroy(doc); if (res != NSPDFERROR_OK) { printf("failed to destroy document (%d)\n", res); |