diff options
-rw-r--r-- | apps/mupdfdraw.c | 10 | ||||
-rw-r--r-- | cbz/mucbz.c | 52 | ||||
-rw-r--r-- | fitz/doc_document.c | 78 | ||||
-rw-r--r-- | fitz/fitz.h | 39 | ||||
-rw-r--r-- | pdf/mupdf.h | 8 | ||||
-rw-r--r-- | pdf/pdf_annot.c | 2 | ||||
-rw-r--r-- | pdf/pdf_page.c | 20 | ||||
-rw-r--r-- | pdf/pdf_xref.c | 73 | ||||
-rw-r--r-- | xps/muxps.h | 2 | ||||
-rw-r--r-- | xps/xps_doc.c | 7 | ||||
-rw-r--r-- | xps/xps_zip.c | 61 |
11 files changed, 333 insertions, 19 deletions
diff --git a/apps/mupdfdraw.c b/apps/mupdfdraw.c index 06e74842..0ab89449 100644 --- a/apps/mupdfdraw.c +++ b/apps/mupdfdraw.c @@ -122,7 +122,7 @@ static void drawpage(pdf_document *doc, int pagenum) { fz_free_device(dev); fz_free_display_list(ctx, list); - pdf_free_page(ctx, page); + pdf_free_page(doc, page); fz_throw(ctx, "cannot draw page %d in file '%s'", pagenum, filename); } fz_free_device(dev); @@ -145,7 +145,7 @@ static void drawpage(pdf_document *doc, int pagenum) { fz_free_device(dev); fz_free_display_list(ctx, list); - pdf_free_page(ctx, page); + pdf_free_page(doc, page); fz_rethrow(ctx); } fz_free_device(dev); @@ -180,7 +180,7 @@ static void drawpage(pdf_document *doc, int pagenum) fz_free_device(dev); fz_free_text_span(ctx, text); fz_free_display_list(ctx, list); - pdf_free_page(ctx, page); + pdf_free_page(doc, page); fz_rethrow(ctx); } fz_free_text_span(ctx, text); @@ -273,7 +273,7 @@ static void drawpage(pdf_document *doc, int pagenum) fz_free_device(dev); fz_drop_pixmap(ctx, pix); fz_free_display_list(ctx, list); - pdf_free_page(ctx, page); + pdf_free_page(doc, page); fz_rethrow(ctx); } } @@ -281,7 +281,7 @@ static void drawpage(pdf_document *doc, int pagenum) if (list) fz_free_display_list(ctx, list); - pdf_free_page(ctx, page); + pdf_free_page(doc, page); if (showtime) { diff --git a/cbz/mucbz.c b/cbz/mucbz.c index a91589cf..83c463bd 100644 --- a/cbz/mucbz.c +++ b/cbz/mucbz.c @@ -11,6 +11,8 @@ #define DPI 72.0f +static void cbz_init_document(cbz_document *doc); + static const char *cbz_ext_list[] = { ".jpg", ".jpeg", ".png", ".JPG", ".JPEG", ".PNG", @@ -32,6 +34,8 @@ struct cbz_entry_s struct cbz_document_s { + fz_document super; + fz_context *ctx; fz_stream *file; int entry_count; @@ -278,6 +282,7 @@ cbz_open_document_with_stream(fz_stream *file) cbz_document *doc; doc = fz_malloc_struct(ctx, cbz_document); + cbz_init_document(doc); doc->ctx = ctx; doc->file = fz_keep_stream(file); doc->entry_count = 0; @@ -409,3 +414,50 @@ cbz_run_page(cbz_document *doc, cbz_page *page, fz_device *dev, fz_matrix ctm, f ctm = fz_concat(fz_scale(w, h), ctm); fz_fill_image(dev, image, ctm, 1); } + +/* Document interface wrappers */ + +static void cbz_close_document_shim(fz_document *doc) +{ + cbz_close_document((cbz_document*)doc); +} + +static int cbz_count_pages_shim(fz_document *doc) +{ + return cbz_count_pages((cbz_document*)doc); +} + +static fz_page *cbz_load_page_shim(fz_document *doc, int number) +{ + return (fz_page*) cbz_load_page((cbz_document*)doc, number); +} + +static fz_rect cbz_bound_page_shim(fz_document *doc, fz_page *page) +{ + return cbz_bound_page((cbz_document*)doc, (cbz_page*)page); +} + +static void cbz_run_page_shim(fz_document *doc, fz_page *page, fz_device *dev, fz_matrix transform, fz_cookie *cookie) +{ + cbz_run_page((cbz_document*)doc, (cbz_page*)page, dev, transform, cookie); +} + +static void cbz_free_page_shim(fz_document *doc, fz_page *page) +{ + cbz_free_page((cbz_document*)doc, (cbz_page*)page); +} + +static void +cbz_init_document(cbz_document *doc) +{ + doc->super.close = cbz_close_document_shim; + doc->super.needs_password = NULL; + doc->super.authenticate_password = NULL; + doc->super.load_outline = NULL; + doc->super.count_pages = cbz_count_pages_shim; + doc->super.load_page = cbz_load_page_shim; + doc->super.load_links = NULL; + doc->super.bound_page = cbz_bound_page_shim; + doc->super.run_page = cbz_run_page_shim; + doc->super.free_page = cbz_free_page_shim; +} diff --git a/fitz/doc_document.c b/fitz/doc_document.c new file mode 100644 index 00000000..36f43ee2 --- /dev/null +++ b/fitz/doc_document.c @@ -0,0 +1,78 @@ +#include "fitz.h" + +void +fz_close_document(fz_document *doc) +{ + if (doc && doc->close) + doc->close(doc); +} + +int +fz_needs_password(fz_document *doc) +{ + if (doc && doc->needs_password) + return doc->needs_password(doc); + return 0; +} + +int +fz_authenticate_password(fz_document *doc, char *password) +{ + if (doc && doc->authenticate_password) + return doc->authenticate_password(doc, password); + return 1; +} + +fz_outline * +fz_load_outline(fz_document *doc) +{ + if (doc && doc->load_outline) + return doc->load_outline(doc); + return NULL; +} + +int +fz_count_pages(fz_document *doc) +{ + if (doc && doc->count_pages) + return doc->count_pages(doc); + return 0; +} + +fz_page * +fz_load_page(fz_document *doc, int number) +{ + if (doc && doc->load_page) + return doc->load_page(doc, number); + return NULL; +} + +fz_link * +fz_load_links(fz_document *doc, fz_page *page) +{ + if (doc && doc->load_links && page) + return doc->load_links(doc, page); + return NULL; +} + +fz_rect +fz_bound_page(fz_document *doc, fz_page *page) +{ + if (doc && doc->bound_page && page) + return doc->bound_page(doc, page); + return fz_empty_rect; +} + +void +fz_run_page(fz_document *doc, fz_page *page, fz_device *dev, fz_matrix transform, fz_cookie *cookie) +{ + if (doc && doc->run_page && page) + doc->run_page(doc, page, dev, transform, cookie); +} + +void +fz_free_page(fz_document *doc, fz_page *page) +{ + if (doc && doc->free_page && page) + doc->free_page(doc, page); +} diff --git a/fitz/fitz.h b/fitz/fitz.h index 8483817d..9628e505 100644 --- a/fitz/fitz.h +++ b/fitz/fitz.h @@ -1580,9 +1580,7 @@ fz_link *fz_new_link(fz_context *ctx, fz_rect bbox, fz_link_dest dest); void fz_free_link(fz_context *ctx, fz_link *link); void fz_free_link_dest(fz_context *ctx, fz_link_dest *dest); -/* - * Document interface. - */ +/* Outline */ typedef struct fz_outline_s fz_outline; @@ -1598,4 +1596,39 @@ void fz_debug_outline_xml(fz_outline *outline, int level); void fz_debug_outline(fz_outline *outline, int level); void fz_free_outline(fz_context *ctx, fz_outline *outline); +/* Document interface */ + +typedef struct fz_document_s fz_document; +typedef struct fz_page_s fz_page; /* doesn't have a definition -- always cast to *_page */ + +struct fz_document_s +{ + void (*close)(fz_document *); + int (*needs_password)(fz_document *doc); + int (*authenticate_password)(fz_document *doc, char *password); + fz_outline *(*load_outline)(fz_document *doc); + int (*count_pages)(fz_document *doc); + fz_page *(*load_page)(fz_document *doc, int number); + fz_link *(*load_links)(fz_document *doc, fz_page *page); + fz_rect (*bound_page)(fz_document *doc, fz_page *page); + void (*run_page)(fz_document *doc, fz_page *page, fz_device *dev, fz_matrix transform, fz_cookie *cookie); + void (*free_page)(fz_document *doc, fz_page *page); +}; + +fz_document *fz_open_document(fz_context *ctx, char *filename); + +void fz_close_document(fz_document *doc); + +int fz_needs_password(fz_document *doc); +int fz_authenticate_password(fz_document *doc, char *password); + +fz_outline *fz_load_outline(fz_document *doc); + +int fz_count_pages(fz_document *doc); +fz_page *fz_load_page(fz_document *doc, int number); +fz_link *fz_load_links(fz_document *doc, fz_page *page); +fz_rect fz_bound_page(fz_document *doc, fz_page *page); +void fz_run_page(fz_document *doc, fz_page *page, fz_device *dev, fz_matrix transform, fz_cookie *cookie); +void fz_free_page(fz_document *doc, fz_page *page); + #endif diff --git a/pdf/mupdf.h b/pdf/mupdf.h index 5b546ba0..95f44c60 100644 --- a/pdf/mupdf.h +++ b/pdf/mupdf.h @@ -73,8 +73,11 @@ struct pdf_ocg_descriptor_s struct pdf_document_s { + fz_document super; + fz_context *ctx; fz_stream *file; + int version; int startxref; int file_size; @@ -429,7 +432,7 @@ fz_obj *pdf_load_name_tree(pdf_document *doc, char *which); fz_outline *pdf_load_outline(pdf_document *doc); -fz_link *pdf_load_links(pdf_document *, fz_obj *annots, fz_matrix page_ctm); +fz_link *pdf_load_link_annots(pdf_document *, fz_obj *annots, fz_matrix page_ctm); pdf_annot *pdf_load_annots(pdf_document *, fz_obj *annots); void pdf_free_annot(fz_context *ctx, pdf_annot *link); @@ -456,8 +459,9 @@ int pdf_find_page_number(pdf_document *doc, fz_obj *pageobj); int pdf_count_pages(pdf_document *doc); pdf_page *pdf_load_page(pdf_document *doc, int number); +fz_link *pdf_load_links(pdf_document *doc, pdf_page *page); fz_rect pdf_bound_page(pdf_document *doc, pdf_page *page); -void pdf_free_page(fz_context *ctx, pdf_page *page); +void pdf_free_page(pdf_document *doc, pdf_page *page); /* * Content stream parsing diff --git a/pdf/pdf_annot.c b/pdf/pdf_annot.c index 12c08bf4..ae03f2b6 100644 --- a/pdf/pdf_annot.c +++ b/pdf/pdf_annot.c @@ -278,7 +278,7 @@ pdf_load_link(pdf_document *xref, fz_obj *dict, fz_matrix page_ctm) } fz_link * -pdf_load_links(pdf_document *xref, fz_obj *annots, fz_matrix page_ctm) +pdf_load_link_annots(pdf_document *xref, fz_obj *annots, fz_matrix page_ctm) { fz_link *link, *head, *tail; fz_obj *obj; diff --git a/pdf/pdf_page.c b/pdf/pdf_page.c index ed80aa90..6f7a97e3 100644 --- a/pdf/pdf_page.c +++ b/pdf/pdf_page.c @@ -359,7 +359,7 @@ pdf_load_page(pdf_document *xref, int number) obj = fz_dict_gets(pageobj, "Annots"); if (obj) { - page->links = pdf_load_links(xref, obj, page->ctm); + page->links = pdf_load_link_annots(xref, obj, page->ctm); page->annots = pdf_load_annots(xref, obj); } @@ -381,7 +381,7 @@ pdf_load_page(pdf_document *xref, int number) } fz_catch(ctx) { - pdf_free_page(ctx, page); + pdf_free_page(xref, page); fz_throw(ctx, "cannot load page %d contents (%d 0 R)", number + 1, fz_to_num(pageref)); } @@ -398,16 +398,22 @@ pdf_bound_page(pdf_document *xref, pdf_page *page) return bounds; } +fz_link * +pdf_load_links(pdf_document *xref, pdf_page *page) +{ + return page->links; +} + void -pdf_free_page(fz_context *ctx, pdf_page *page) +pdf_free_page(pdf_document *xref, pdf_page *page) { if (page->resources) fz_drop_obj(page->resources); if (page->contents) - fz_drop_buffer(ctx, page->contents); + fz_drop_buffer(xref->ctx, page->contents); if (page->links) - fz_free_link(ctx, page->links); + fz_free_link(xref->ctx, page->links); if (page->annots) - pdf_free_annot(ctx, page->annots); - fz_free(ctx, page); + pdf_free_annot(xref->ctx, page->annots); + fz_free(xref->ctx, page); } diff --git a/pdf/pdf_xref.c b/pdf/pdf_xref.c index 7fb247d9..d1badc79 100644 --- a/pdf/pdf_xref.c +++ b/pdf/pdf_xref.c @@ -638,6 +638,8 @@ pdf_free_ocg(fz_context *ctx, pdf_ocg_descriptor *desc) * If password is not null, try to decrypt. */ +static void pdf_init_document(pdf_document *xref); + pdf_document * pdf_open_document_with_stream(fz_stream *file) { @@ -655,7 +657,9 @@ pdf_open_document_with_stream(fz_stream *file) /* install pdf specific callback */ fz_resolve_indirect = pdf_resolve_indirect; - xref = fz_calloc(ctx, 1, sizeof(pdf_document)); + xref = fz_malloc_struct(ctx, pdf_document); + pdf_init_document(xref); + xref->file = fz_keep_stream(file); xref->ctx = ctx; @@ -1092,3 +1096,70 @@ pdf_open_document(fz_context *ctx, const char *filename) fz_close(file); return xref; } + +/* Document interface wrappers */ + +static void pdf_close_document_shim(fz_document *doc) +{ + pdf_close_document((pdf_document*)doc); +} + +static int pdf_needs_password_shim(fz_document *doc) +{ + return pdf_needs_password((pdf_document*)doc); +} + +static int pdf_authenticate_password_shim(fz_document *doc, char *password) +{ + return pdf_authenticate_password((pdf_document*)doc, password); +} + +static fz_outline *pdf_load_outline_shim(fz_document *doc) +{ + return pdf_load_outline((pdf_document*)doc); +} + +static int pdf_count_pages_shim(fz_document *doc) +{ + return pdf_count_pages((pdf_document*)doc); +} + +static fz_page *pdf_load_page_shim(fz_document *doc, int number) +{ + return (fz_page*) pdf_load_page((pdf_document*)doc, number); +} + +static fz_link *pdf_load_links_shim(fz_document *doc, fz_page *page) +{ + return pdf_load_links((pdf_document*)doc, (pdf_page*)page); +} + +static fz_rect pdf_bound_page_shim(fz_document *doc, fz_page *page) +{ + return pdf_bound_page((pdf_document*)doc, (pdf_page*)page); +} + +static void pdf_run_page_shim(fz_document *doc, fz_page *page, fz_device *dev, fz_matrix transform, fz_cookie *cookie) +{ + pdf_run_page((pdf_document*)doc, (pdf_page*)page, dev, transform, cookie); +} + +static void pdf_free_page_shim(fz_document *doc, fz_page *page) +{ + pdf_free_page((pdf_document*)doc, (pdf_page*)page); +} + +static void +pdf_init_document(pdf_document *doc) +{ + doc->super.close = pdf_close_document_shim; + doc->super.needs_password = pdf_needs_password_shim; + doc->super.authenticate_password = pdf_authenticate_password_shim; + doc->super.load_outline = pdf_load_outline_shim; + doc->super.count_pages = pdf_count_pages_shim; + doc->super.load_page = pdf_load_page_shim; + doc->super.load_links = pdf_load_links_shim; + doc->super.bound_page = pdf_bound_page_shim; + doc->super.run_page = pdf_run_page_shim; + doc->super.free_page = pdf_free_page_shim; +} diff --git a/xps/muxps.h b/xps/muxps.h index 2e5c4206..6a6f345f 100644 --- a/xps/muxps.h +++ b/xps/muxps.h @@ -215,6 +215,8 @@ struct xps_entry_s struct xps_document_s { + fz_document super; + fz_context *ctx; char *directory; fz_stream *file; diff --git a/xps/xps_doc.c b/xps/xps_doc.c index 0bc7e443..3a4738dd 100644 --- a/xps/xps_doc.c +++ b/xps/xps_doc.c @@ -141,6 +141,13 @@ xps_add_link(xps_document *doc, fz_rect area, char *base_uri, char *target_uri) } } +fz_link * +xps_load_links(xps_document *doc, xps_page *page) +{ + if (!page->links_resolved) + fz_warn(doc->ctx, "xps_load_links before page has been executed!"); + return page->links; +} static void xps_add_fixed_page(xps_document *doc, char *name, int width, int height) diff --git a/xps/xps_zip.c b/xps/xps_zip.c index 38f0cb60..b3772138 100644 --- a/xps/xps_zip.c +++ b/xps/xps_zip.c @@ -3,6 +3,8 @@ #include <zlib.h> +static void xps_init_document(xps_document *doc); + xps_part * xps_new_part(xps_document *doc, char *name, int size) { @@ -456,6 +458,7 @@ xps_open_document_with_directory(fz_context *ctx, char *directory) xps_document *doc; doc = fz_malloc_struct(ctx, xps_document); + xps_init_document(doc); doc->ctx = ctx; doc->directory = fz_strdup(ctx, directory); @@ -479,6 +482,7 @@ xps_open_document_with_stream(fz_stream *file) xps_document *doc; doc = fz_malloc_struct(ctx, xps_document); + xps_init_document(doc); doc->ctx = ctx; doc->file = fz_keep_stream(file); @@ -563,3 +567,60 @@ xps_close_document(xps_document *doc) fz_free(doc->ctx, doc->directory); fz_free(doc->ctx, doc); } + +/* Document interface wrappers */ + +static void xps_close_document_shim(fz_document *doc) +{ + xps_close_document((xps_document*)doc); +} + +static fz_outline *xps_load_outline_shim(fz_document *doc) +{ + return xps_load_outline((xps_document*)doc); +} + +static int xps_count_pages_shim(fz_document *doc) +{ + return xps_count_pages((xps_document*)doc); +} + +static fz_page *xps_load_page_shim(fz_document *doc, int number) +{ + return (fz_page*) xps_load_page((xps_document*)doc, number); +} + +static fz_link *xps_load_links_shim(fz_document *doc, fz_page *page) +{ + return xps_load_links((xps_document*)doc, (xps_page*)page); +} + +static fz_rect xps_bound_page_shim(fz_document *doc, fz_page *page) +{ + return xps_bound_page((xps_document*)doc, (xps_page*)page); +} + +static void xps_run_page_shim(fz_document *doc, fz_page *page, fz_device *dev, fz_matrix transform, fz_cookie *cookie) +{ + xps_run_page((xps_document*)doc, (xps_page*)page, dev, transform, cookie); +} + +static void xps_free_page_shim(fz_document *doc, fz_page *page) +{ + xps_free_page((xps_document*)doc, (xps_page*)page); +} + +static void +xps_init_document(xps_document *doc) +{ + doc->super.close = xps_close_document_shim; + doc->super.needs_password = NULL; + doc->super.authenticate_password = NULL; + doc->super.load_outline = xps_load_outline_shim; + doc->super.count_pages = xps_count_pages_shim; + doc->super.load_page = xps_load_page_shim; + doc->super.load_links = xps_load_links_shim; + doc->super.bound_page = xps_bound_page_shim; + doc->super.run_page = xps_run_page_shim; + doc->super.free_page = xps_free_page_shim; +} |