summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2012-05-11 14:51:11 +0200
committerTor Andersson <tor.andersson@artifex.com>2012-05-11 14:51:11 +0200
commite54d4225e2d16d2b5cc928294009fce0a5b30718 (patch)
tree1732eb253ecbcd8e72e5d13fc3062944a57d9627
parent314c9ecaaba711741ecc5bb3e3e1e62655cee8c4 (diff)
downloadmupdf-e54d4225e2d16d2b5cc928294009fce0a5b30718.tar.xz
Split part of fz_document interface for pdf_document into separate file.
Make a separate constructor function that does not link in the interpreter, so we can save space in the mubusy binary by not including the font and cmap resources.
-rw-r--r--android/jni/Core.mk1
-rw-r--r--apps/mupdfclean.c2
-rw-r--r--apps/mupdfextract.c3
-rw-r--r--apps/mupdfinfo.c2
-rw-r--r--apps/mupdfposter.c2
-rw-r--r--apps/mupdfshow.c2
-rw-r--r--pdf/mupdf-internal.h3
-rw-r--r--pdf/mupdf.h5
-rw-r--r--pdf/pdf_xref.c214
-rw-r--r--pdf/pdf_xref_aux.c31
-rw-r--r--win32/libmupdf.vcproj4
11 files changed, 163 insertions, 106 deletions
diff --git a/android/jni/Core.mk b/android/jni/Core.mk
index 9fe01db0..cea16e74 100644
--- a/android/jni/Core.mk
+++ b/android/jni/Core.mk
@@ -102,6 +102,7 @@ LOCAL_SRC_FILES := \
$(MY_ROOT)/pdf/pdf_unicode.c \
$(MY_ROOT)/pdf/pdf_xobject.c \
$(MY_ROOT)/pdf/pdf_xref.c \
+ $(MY_ROOT)/pdf/pdf_xref_aux.c \
$(MY_ROOT)/xps/xps_common.c \
$(MY_ROOT)/xps/xps_doc.c \
$(MY_ROOT)/xps/xps_glyphs.c \
diff --git a/apps/mupdfclean.c b/apps/mupdfclean.c
index a1064f57..a3711c72 100644
--- a/apps/mupdfclean.c
+++ b/apps/mupdfclean.c
@@ -200,7 +200,7 @@ int pdfclean_main(int argc, char **argv)
exit(1);
}
- xref = pdf_open_document(ctx, infile);
+ xref = pdf_open_document_no_run(ctx, infile);
if (pdf_needs_password(xref))
if (!pdf_authenticate_password(xref, password))
fz_throw(ctx, "cannot authenticate password: %s", infile);
diff --git a/apps/mupdfextract.c b/apps/mupdfextract.c
index a46e0d88..d1131f67 100644
--- a/apps/mupdfextract.c
+++ b/apps/mupdfextract.c
@@ -3,6 +3,7 @@
*/
#include "mupdf.h"
+#include "mupdf-internal.h"
static pdf_document *doc = NULL;
static fz_context *ctx = NULL;
@@ -170,7 +171,7 @@ int pdfextract_main(int argc, char **argv)
exit(1);
}
- doc = pdf_open_document(ctx, infile);
+ doc = pdf_open_document_no_run(ctx, infile);
if (pdf_needs_password(doc))
if (!pdf_authenticate_password(doc, password))
fz_throw(ctx, "cannot authenticate password: %s", infile);
diff --git a/apps/mupdfinfo.c b/apps/mupdfinfo.c
index 52018a9c..78838ee2 100644
--- a/apps/mupdfinfo.c
+++ b/apps/mupdfinfo.c
@@ -996,7 +996,7 @@ int pdfinfo_main(int argc, char **argv)
filename = argv[fz_optind];
printf("%s:\n", filename);
- xref = pdf_open_document(ctx, filename);
+ xref = pdf_open_document_no_run(ctx, filename);
if (pdf_needs_password(xref))
if (!pdf_authenticate_password(xref, password))
fz_throw(ctx, "cannot authenticate password: %s", filename);
diff --git a/apps/mupdfposter.c b/apps/mupdfposter.c
index 415e3175..19198982 100644
--- a/apps/mupdfposter.c
+++ b/apps/mupdfposter.c
@@ -162,7 +162,7 @@ int pdfposter_main(int argc, char **argv)
exit(1);
}
- xref = pdf_open_document(ctx, infile);
+ xref = pdf_open_document_no_run(ctx, infile);
if (pdf_needs_password(xref))
if (!pdf_authenticate_password(xref, password))
fz_throw(ctx, "cannot authenticate password: %s", infile);
diff --git a/apps/mupdfshow.c b/apps/mupdfshow.c
index 1c198e8d..7114f249 100644
--- a/apps/mupdfshow.c
+++ b/apps/mupdfshow.c
@@ -203,7 +203,7 @@ int pdfshow_main(int argc, char **argv)
fz_var(doc);
fz_try(ctx)
{
- doc = pdf_open_document(ctx, filename);
+ doc = pdf_open_document_no_run(ctx, filename);
if (pdf_needs_password(doc))
if (!pdf_authenticate_password(doc, password))
fz_warn(ctx, "cannot authenticate password: %s", filename);
diff --git a/pdf/mupdf-internal.h b/pdf/mupdf-internal.h
index e947cbbd..bde094a1 100644
--- a/pdf/mupdf-internal.h
+++ b/pdf/mupdf-internal.h
@@ -191,6 +191,9 @@ struct pdf_document_s
pdf_lexbuf_large lexbuf;
};
+pdf_document *pdf_open_document_no_run(fz_context *ctx, const char *filename);
+pdf_document *pdf_open_document_no_run_with_stream(fz_stream *file);
+
void pdf_cache_object(pdf_document *doc, int num, int gen);
fz_stream *pdf_open_inline_stream(pdf_document *doc, pdf_obj *stmobj, int length, fz_stream *chain, pdf_image_params *params);
diff --git a/pdf/mupdf.h b/pdf/mupdf.h
index f02d1828..762d1bc5 100644
--- a/pdf/mupdf.h
+++ b/pdf/mupdf.h
@@ -217,4 +217,9 @@ void pdf_run_page(pdf_document *doc, pdf_page *page, fz_device *dev, fz_matrix c
void pdf_run_page_with_usage(pdf_document *doc, pdf_page *page, fz_device *dev, fz_matrix ctm, char *event, fz_cookie *cookie);
+/*
+ Metadata interface.
+*/
+int pdf_meta(pdf_document *doc, int key, void *ptr, int size);
+
#endif
diff --git a/pdf/pdf_xref.c b/pdf/pdf_xref.c
index 3af2901c..cbc0c1ec 100644
--- a/pdf/pdf_xref.c
+++ b/pdf/pdf_xref.c
@@ -672,29 +672,19 @@ 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)
+static void
+pdf_init_document(pdf_document *xref)
{
- pdf_document *xref;
+ fz_context *ctx = xref->ctx;
pdf_obj *encrypt, *id;
pdf_obj *dict = NULL;
pdf_obj *obj;
pdf_obj *nobj = NULL;
int i, repaired = 0;
- fz_context *ctx = file->ctx;
fz_var(dict);
fz_var(nobj);
- xref = fz_malloc_struct(ctx, pdf_document);
- pdf_init_document(xref);
- xref->lexbuf.base.size = PDF_LEXBUF_LARGE;
-
- xref->file = fz_keep_stream(file);
- xref->ctx = ctx;
-
fz_try(ctx)
{
pdf_load_xref(xref, &xref->lexbuf.base);
@@ -797,8 +787,6 @@ pdf_open_document_with_stream(fz_stream *file)
{
fz_warn(ctx, "Ignoring Broken Optional Content");
}
-
- return xref;
}
void
@@ -1083,7 +1071,8 @@ pdf_resolve_indirect(pdf_obj *ref)
return ref;
}
-int pdf_count_objects(pdf_document *doc)
+int
+pdf_count_objects(pdf_document *doc)
{
return doc->len;
}
@@ -1110,88 +1099,9 @@ pdf_update_object(pdf_document *xref, int num, int gen, pdf_obj *newobj)
x->ofs = 0;
}
-/*
- * Convenience function to open a file then call pdf_open_document_with_stream.
- */
-
-pdf_document *
-pdf_open_document(fz_context *ctx, const char *filename)
-{
- fz_stream *file = NULL;
- pdf_document *xref;
-
- fz_var(file);
- fz_try(ctx)
- {
- file = fz_open_file(ctx, filename);
- xref = pdf_open_document_with_stream(file);
- }
- fz_catch(ctx)
- {
- fz_close(file);
- fz_throw(ctx, "cannot load document '%s'", 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 int pdf_meta(fz_document *doc_, int key, void *ptr, int size)
+int
+pdf_meta(pdf_document *doc, int key, void *ptr, int size)
{
- pdf_document *doc = (pdf_document *)doc_;
-
switch(key)
{
/*
@@ -1263,9 +1173,72 @@ static int pdf_meta(fz_document *doc_, int key, void *ptr, int size)
}
}
-static void
-pdf_init_document(pdf_document *doc)
+/*
+ Wrappers to implement the fz_document interface for pdf_document.
+
+ The functions are split across two files to allow calls to a
+ version of the constructor that does not link in the interpreter.
+ The interpreter references the built-in font and cmap resources
+ which are quite big. Not linking those into the mubusy binary
+ saves roughly 6MB of space.
+*/
+
+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_free_page_shim(fz_document *doc, fz_page *page)
+{
+ pdf_free_page((pdf_document*)doc, (pdf_page*)page);
+}
+
+static int pdf_meta_shim(fz_document *doc, int key, void *ptr, int size)
+{
+ return pdf_meta((pdf_document*)doc, key, ptr, size);
+}
+
+static pdf_document *
+pdf_new_document(fz_stream *file)
+{
+ fz_context *ctx = file->ctx;
+ pdf_document *doc = fz_malloc_struct(ctx, pdf_document);
+
doc->super.close = pdf_close_document_shim;
doc->super.needs_password = pdf_needs_password_shim;
doc->super.authenticate_password = pdf_authenticate_password_shim;
@@ -1274,7 +1247,46 @@ pdf_init_document(pdf_document *doc)
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.run_page = NULL; /* see pdf_xref_aux.c */
doc->super.free_page = pdf_free_page_shim;
- doc->super.meta = pdf_meta;
+ doc->super.meta = pdf_meta_shim;
+
+ doc->lexbuf.base.size = PDF_LEXBUF_LARGE;
+ doc->file = fz_keep_stream(file);
+ doc->ctx = ctx;
+
+ return doc;
+}
+
+pdf_document *
+pdf_open_document_no_run_with_stream(fz_stream *file)
+{
+ pdf_document *doc = pdf_new_document(file);
+ pdf_init_document(doc);
+ return doc;
+}
+
+pdf_document *
+pdf_open_document_no_run(fz_context *ctx, const char *filename)
+{
+ fz_stream *file = NULL;
+ pdf_document *doc;
+
+ fz_var(file);
+
+ fz_try(ctx)
+ {
+ file = fz_open_file(ctx, filename);
+ doc = pdf_new_document(file);
+ pdf_init_document(doc);
+ }
+ fz_always(ctx)
+ {
+ fz_close(file);
+ }
+ fz_catch(ctx)
+ {
+ fz_throw(ctx, "cannot load document '%s'", filename);
+ }
+ return doc;
}
diff --git a/pdf/pdf_xref_aux.c b/pdf/pdf_xref_aux.c
new file mode 100644
index 00000000..2d760334
--- /dev/null
+++ b/pdf/pdf_xref_aux.c
@@ -0,0 +1,31 @@
+#include "fitz-internal.h"
+#include "mupdf-internal.h"
+
+/*
+ These functions have been split out of pdf_xref.c to allow tools
+ to be linked without pulling in the interpreter. The interpreter
+ references the built-in font and cmap resources which are quite
+ big. Not linking those into the tools saves roughly 6MB in the
+ resulting executables.
+*/
+
+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);
+}
+
+pdf_document *
+pdf_open_document_with_stream(fz_stream *file)
+{
+ pdf_document *doc = pdf_open_document_no_run_with_stream(file);
+ doc->super.run_page = pdf_run_page_shim;
+ return doc;
+}
+
+pdf_document *
+pdf_open_document(fz_context *ctx, const char *filename)
+{
+ pdf_document *doc = pdf_open_document_no_run(ctx, filename);
+ doc->super.run_page = pdf_run_page_shim;
+ return doc;
+}
diff --git a/win32/libmupdf.vcproj b/win32/libmupdf.vcproj
index 03b1bbd1..8706176c 100644
--- a/win32/libmupdf.vcproj
+++ b/win32/libmupdf.vcproj
@@ -345,6 +345,10 @@
RelativePath="..\pdf\pdf_xref.c"
>
</File>
+ <File
+ RelativePath="..\pdf\pdf_xref_aux.c"
+ >
+ </File>
</Filter>
<Filter
Name="fitz"