summaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
Diffstat (limited to 'apps')
-rw-r--r--apps/pdfapp.c145
-rw-r--r--apps/pdfapp.h6
-rw-r--r--apps/pdfclean.c175
-rw-r--r--apps/pdfdraw.c107
-rw-r--r--apps/pdfextract.c93
-rw-r--r--apps/pdfinfo.c143
-rw-r--r--apps/pdfshow.c87
-rw-r--r--apps/win_main.c22
-rw-r--r--apps/x11_main.c27
-rw-r--r--apps/xpsdraw.c111
10 files changed, 559 insertions, 357 deletions
diff --git a/apps/pdfapp.c b/apps/pdfapp.c
index 0ef61e2d..57f5e8e8 100644
--- a/apps/pdfapp.c
+++ b/apps/pdfapp.c
@@ -68,12 +68,13 @@ char *pdfapp_usage(pdfapp_t *app)
;
}
-void pdfapp_init(pdfapp_t *app)
+void pdfapp_init(fz_context *ctx, pdfapp_t *app)
{
memset(app, 0, sizeof(pdfapp_t));
app->scrw = 640;
app->scrh = 480;
app->resolution = 72;
+ app->ctx = ctx;
}
void pdfapp_invert(pdfapp_t *app, fz_bbox rect)
@@ -99,20 +100,25 @@ void pdfapp_invert(pdfapp_t *app, fz_bbox rect)
static void pdfapp_open_pdf(pdfapp_t *app, char *filename, int fd)
{
- fz_error error;
fz_stream *file;
char *password = "";
fz_obj *obj;
fz_obj *info;
+ fz_context *ctx = app->ctx;
/*
* Open PDF and load xref table
*/
- file = fz_open_fd(fd);
- error = pdf_open_xref_with_stream(&app->xref, file, NULL);
- if (error)
- pdfapp_error(app, fz_rethrow(error, "cannot open document '%s'", filename));
+ file = fz_open_fd(ctx, fd);
+ fz_try(ctx)
+ {
+ app->xref = pdf_open_xref_with_stream(file, NULL);
+ }
+ fz_catch(ctx)
+ {
+ pdfapp_error(app, fz_error_note(1, "cannot open document '%s'", filename));
+ }
fz_close(file);
/*
@@ -139,12 +145,17 @@ static void pdfapp_open_pdf(pdfapp_t *app, char *filename, int fd)
app->outline = pdf_load_outline(app->xref);
+ app->doctitle = fz_strdup(ctx, filename);
+ if (strrchr(app->doctitle, '\\'))
+ app->doctitle = strrchr(app->doctitle, '\\') + 1;
+ if (strrchr(app->doctitle, '/'))
+ app->doctitle = strrchr(app->doctitle, '/') + 1;
info = fz_dict_gets(app->xref->trailer, "Info");
if (info)
{
obj = fz_dict_gets(info, "Title");
if (obj)
- app->doctitle = pdf_to_utf8(obj);
+ app->doctitle = pdf_to_utf8(ctx, obj);
}
if (!app->doctitle)
{
@@ -153,32 +164,41 @@ static void pdfapp_open_pdf(pdfapp_t *app, char *filename, int fd)
app->doctitle = strrchr(app->doctitle, '\\') + 1;
if (strrchr(app->doctitle, '/'))
app->doctitle = strrchr(app->doctitle, '/') + 1;
- app->doctitle = fz_strdup(app->doctitle);
+ app->doctitle = fz_strdup(ctx, app->doctitle);
}
/*
* Start at first page
*/
- error = pdf_load_page_tree(app->xref);
- if (error)
- pdfapp_error(app, fz_rethrow(error, "cannot load page tree"));
+ fz_try(ctx)
+ {
+ pdf_load_page_tree(app->xref);
+ }
+ fz_catch(ctx)
+ {
+ pdfapp_error(app, fz_error_note(1, "cannot load page tree"));
+ }
app->pagecount = pdf_count_pages(app->xref);
}
static void pdfapp_open_xps(pdfapp_t *app, char *filename, int fd)
{
- fz_error error;
fz_stream *file;
- file = fz_open_fd(fd);
- error = xps_open_stream(&app->xps, file);
- if (error)
- pdfapp_error(app, fz_rethrow(error, "cannot open document '%s'", filename));
+ file = fz_open_fd(app->ctx, fd);
+ fz_try(app->ctx)
+ {
+ app->xps = xps_open_stream(file);
+ }
+ fz_catch(app->ctx)
+ {
+ pdfapp_error(app, fz_error_note(-1, "cannot open document '%s'", filename));
+ }
fz_close(file);
- app->doctitle = fz_strdup(filename);
+ app->doctitle = fz_strdup(app->ctx, filename);
app->pagecount = xps_count_pages(app->xps);
}
@@ -190,7 +210,7 @@ void pdfapp_open(pdfapp_t *app, char *filename, int fd, int reload)
else
pdfapp_open_pdf(app, filename, fd);
- app->cache = fz_new_glyph_cache();
+ app->cache = fz_new_glyph_cache(app->ctx);
if (app->pageno < 1)
app->pageno = 1;
@@ -215,27 +235,27 @@ void pdfapp_open(pdfapp_t *app, char *filename, int fd, int reload)
void pdfapp_close(pdfapp_t *app)
{
if (app->page_list)
- fz_free_display_list(app->page_list);
+ fz_free_display_list(app->ctx, app->page_list);
app->page_list = NULL;
if (app->page_text)
- fz_free_text_span(app->page_text);
+ fz_free_text_span(app->ctx, app->page_text);
app->page_text = NULL;
if (app->page_links)
- pdf_free_link(app->page_links);
+ pdf_free_link(app->ctx, app->page_links);
app->page_links = NULL;
if (app->doctitle)
- fz_free(app->doctitle);
+ fz_free(app->ctx, app->doctitle);
app->doctitle = NULL;
if (app->cache)
- fz_free_glyph_cache(app->cache);
+ fz_free_glyph_cache(app->ctx, app->cache);
app->cache = NULL;
if (app->image)
- fz_drop_pixmap(app->image);
+ fz_drop_pixmap(app->ctx, app->image);
app->image = NULL;
if (app->outline)
@@ -245,7 +265,7 @@ void pdfapp_close(pdfapp_t *app)
if (app->xref)
{
if (app->xref->store)
- pdf_free_store(app->xref->store);
+ pdf_free_store(app->ctx, app->xref->store);
app->xref->store = NULL;
pdf_free_xref(app->xref);
@@ -258,7 +278,7 @@ void pdfapp_close(pdfapp_t *app)
app->xps = NULL;
}
- fz_flush_warnings();
+ fz_flush_warnings(app->ctx);
}
static fz_matrix pdfapp_viewctm(pdfapp_t *app)
@@ -301,12 +321,16 @@ static void pdfapp_panview(pdfapp_t *app, int newx, int newy)
static void pdfapp_loadpage_pdf(pdfapp_t *app)
{
pdf_page *page;
- fz_error error;
fz_device *mdev;
- error = pdf_load_page(&page, app->xref, app->pageno - 1);
- if (error)
- pdfapp_error(app, error);
+ fz_try(app->ctx)
+ {
+ page = pdf_load_page(app->xref, app->pageno - 1);
+ }
+ fz_catch(app->ctx)
+ {
+ pdfapp_error(app, 1);
+ }
app->page_bbox = page->mediabox;
app->page_rotate = page->rotate;
@@ -314,30 +338,36 @@ static void pdfapp_loadpage_pdf(pdfapp_t *app)
page->links = NULL;
/* Create display list */
- app->page_list = fz_new_display_list();
- mdev = fz_new_list_device(app->page_list);
- error = pdf_run_page(app->xref, page, mdev, fz_identity);
- if (error)
+ app->page_list = fz_new_display_list(app->ctx);
+ mdev = fz_new_list_device(app->ctx, app->page_list);
+ fz_try(app->ctx)
{
- error = fz_rethrow(error, "cannot draw page %d in '%s'", app->pageno, app->doctitle);
- pdfapp_error(app, error);
+ pdf_run_page(app->xref, page, mdev, fz_identity);
+ }
+ fz_catch(app->ctx)
+ {
+ pdfapp_error(app, fz_error_note(-1, "cannot draw page %d in '%s'", app->pageno, app->doctitle));
}
fz_free_device(mdev);
- pdf_free_page(page);
+ pdf_free_page(app->ctx, page);
- pdf_age_store(app->xref->store, 3);
+ pdf_age_store(app->ctx, app->xref->store, 3);
}
static void pdfapp_loadpage_xps(pdfapp_t *app)
{
xps_page *page;
fz_device *mdev;
- fz_error error;
- error = xps_load_page(&page, app->xps, app->pageno - 1);
- if (error)
- pdfapp_error(app, fz_rethrow(error, "cannot load page %d in file '%s'", app->pageno, app->doctitle));
+ fz_try(app->ctx)
+ {
+ page = xps_load_page(app->xps, app->pageno - 1);
+ }
+ fz_catch(app->ctx)
+ {
+ pdfapp_error(app, fz_error_note(1, "cannot load page %d in file '%s'", app->pageno, app->doctitle));
+ }
app->page_bbox.x0 = 0;
app->page_bbox.y0 = 0;
@@ -347,8 +377,8 @@ static void pdfapp_loadpage_xps(pdfapp_t *app)
app->page_links = NULL;
/* Create display list */
- app->page_list = fz_new_display_list();
- mdev = fz_new_list_device(app->page_list);
+ app->page_list = fz_new_display_list(app->ctx);
+ mdev = fz_new_list_device(app->ctx, app->page_list);
app->xps->dev = mdev;
xps_parse_fixed_page(app->xps, fz_identity, page);
app->xps->dev = NULL;
@@ -371,11 +401,11 @@ static void pdfapp_showpage(pdfapp_t *app, int loadpage, int drawpage, int repai
if (loadpage)
{
if (app->page_list)
- fz_free_display_list(app->page_list);
+ fz_free_display_list(app->ctx, app->page_list);
if (app->page_text)
- fz_free_text_span(app->page_text);
+ fz_free_text_span(app->ctx, app->page_text);
if (app->page_links)
- pdf_free_link(app->page_links);
+ pdf_free_link(app->ctx, app->page_links);
if (app->xref)
pdfapp_loadpage_pdf(app);
@@ -387,8 +417,8 @@ static void pdfapp_showpage(pdfapp_t *app, int loadpage, int drawpage, int repai
app->hitlen = 0;
/* Extract text */
- app->page_text = fz_new_text_span();
- tdev = fz_new_text_device(app->page_text);
+ app->page_text = fz_new_text_span(app->ctx);
+ tdev = fz_new_text_device(app->ctx, app->page_text);
fz_execute_display_list(app->page_list, tdev, fz_identity, fz_infinite_bbox);
fz_free_device(tdev);
}
@@ -404,7 +434,7 @@ static void pdfapp_showpage(pdfapp_t *app, int loadpage, int drawpage, int repai
/* Draw */
if (app->image)
- fz_drop_pixmap(app->image);
+ fz_drop_pixmap(app->ctx, app->image);
if (app->grayscale)
colorspace = fz_device_gray;
else
@@ -413,9 +443,9 @@ static void pdfapp_showpage(pdfapp_t *app, int loadpage, int drawpage, int repai
#else
colorspace = fz_device_rgb;
#endif
- app->image = fz_new_pixmap_with_rect(colorspace, bbox);
+ app->image = fz_new_pixmap_with_rect(app->ctx, colorspace, bbox);
fz_clear_pixmap_with_color(app->image, 255);
- idev = fz_new_draw_device(app->cache, app->image);
+ idev = fz_new_draw_device(app->ctx, app->cache, app->image);
fz_execute_display_list(app->page_list, idev, ctm, bbox);
fz_free_device(idev);
}
@@ -445,17 +475,18 @@ static void pdfapp_showpage(pdfapp_t *app, int loadpage, int drawpage, int repai
wincursor(app, ARROW);
}
- fz_flush_warnings();
+ fz_flush_warnings(app->ctx);
}
static void pdfapp_gotouri(pdfapp_t *app, fz_obj *uri)
{
char *buf;
- buf = fz_malloc(fz_to_str_len(uri) + 1);
- memcpy(buf, fz_to_str_buf(uri), fz_to_str_len(uri));
- buf[fz_to_str_len(uri)] = 0;
+ int n = fz_to_str_len(uri);
+ buf = fz_malloc(app->ctx, n + 1);
+ memcpy(buf, fz_to_str_buf(uri), n);
+ buf[n] = 0;
winopenuri(app, buf);
- fz_free(buf);
+ fz_free(app->ctx, buf);
}
static void pdfapp_gotopage(pdfapp_t *app, fz_obj *obj)
diff --git a/apps/pdfapp.h b/apps/pdfapp.h
index 270c450a..e617333d 100644
--- a/apps/pdfapp.h
+++ b/apps/pdfapp.h
@@ -32,7 +32,7 @@ struct pdfapp_s
char *doctitle;
pdf_xref *xref;
fz_outline *outline;
- xps_context *xps;
+ xps_document *xps;
int pagecount;
fz_glyph_cache *cache;
@@ -88,9 +88,11 @@ struct pdfapp_s
/* client context storage */
void *userdata;
+
+ fz_context *ctx;
};
-void pdfapp_init(pdfapp_t *app);
+void pdfapp_init(fz_context *ctx, pdfapp_t *app);
void pdfapp_open(pdfapp_t *app, char *filename, int fd, int reload);
void pdfapp_close(pdfapp_t *app);
diff --git a/apps/pdfclean.c b/apps/pdfclean.c
index b436a9cb..2c492951 100644
--- a/apps/pdfclean.c
+++ b/apps/pdfclean.c
@@ -24,10 +24,11 @@ static int doexpand = 0;
static int doascii = 0;
static pdf_xref *xref = NULL;
+static fz_context *ctx = NULL;
void die(fz_error error)
{
- fz_catch(error, "aborting");
+ fz_error_handle(error, "aborting");
if (xref)
pdf_free_xref(xref);
exit(1);
@@ -61,12 +62,18 @@ static void sweepobj(fz_obj *obj)
sweepref(obj);
else if (fz_is_dict(obj))
- for (i = 0; i < fz_dict_len(obj); i++)
+ {
+ int n = fz_dict_len(obj);
+ for (i = 0; i < n; i++)
sweepobj(fz_dict_get_val(obj, i));
+ }
else if (fz_is_array(obj))
- for (i = 0; i < fz_array_len(obj); i++)
+ {
+ int n = fz_array_len(obj);
+ for (i = 0; i < n; i++)
sweepobj(fz_array_get(obj, i));
+ }
}
static void sweepref(fz_obj *obj)
@@ -176,16 +183,18 @@ static void compactxref(void)
static void renumberobj(fz_obj *obj)
{
int i;
+ fz_context *ctx = xref->ctx;
if (fz_is_dict(obj))
{
- for (i = 0; i < fz_dict_len(obj); i++)
+ int n = fz_dict_len(obj);
+ for (i = 0; i < n; i++)
{
fz_obj *key = fz_dict_get_key(obj, i);
fz_obj *val = fz_dict_get_val(obj, i);
if (fz_is_indirect(val))
{
- val = fz_new_indirect(renumbermap[fz_to_num(val)], 0, xref);
+ val = fz_new_indirect(ctx, renumbermap[fz_to_num(val)], 0, xref);
fz_dict_put(obj, key, val);
fz_drop_obj(val);
}
@@ -198,12 +207,13 @@ static void renumberobj(fz_obj *obj)
else if (fz_is_array(obj))
{
- for (i = 0; i < fz_array_len(obj); i++)
+ int n = fz_array_len(obj);
+ for (i = 0; i < n; i++)
{
fz_obj *val = fz_array_get(obj, i);
if (fz_is_indirect(val))
{
- val = fz_new_indirect(renumbermap[fz_to_num(val)], 0, xref);
+ val = fz_new_indirect(ctx, renumbermap[fz_to_num(val)], 0, xref);
fz_array_put(obj, i, val);
fz_drop_obj(val);
}
@@ -229,7 +239,7 @@ static void renumberobjs(void)
if (fz_is_indirect(obj))
{
- obj = fz_new_indirect(renumbermap[fz_to_num(obj)], 0, xref);
+ obj = fz_new_indirect(ctx, renumbermap[fz_to_num(obj)], 0, xref);
pdf_update_object(xref, num, 0, obj);
fz_drop_obj(obj);
}
@@ -241,7 +251,7 @@ static void renumberobjs(void)
/* Create new table for the reordered, compacted xref */
oldxref = xref->table;
- xref->table = fz_calloc(xref->len, sizeof(pdf_xref_entry));
+ xref->table = fz_malloc_array(xref->ctx, xref->len, sizeof(pdf_xref_entry));
xref->table[0] = oldxref[0];
/* Move used objects into the new compacted xref */
@@ -261,7 +271,7 @@ static void renumberobjs(void)
}
}
- fz_free(oldxref);
+ fz_free(xref->ctx, oldxref);
/* Update the used objects count in compacted xref */
xref->len = newlen + 1;
@@ -277,13 +287,17 @@ static void renumberobjs(void)
static void retainpages(int argc, char **argv)
{
- fz_error error;
fz_obj *oldroot, *root, *pages, *kids, *countobj, *parent, *olddests;
/* Load the old page tree */
- error = pdf_load_page_tree(xref);
- if (error)
- die(fz_rethrow(error, "cannot load page tree"));
+ fz_try(xref->ctx)
+ {
+ pdf_load_page_tree(xref);
+ }
+ fz_catch(xref->ctx)
+ {
+ die(fz_error_note(1, "cannot load page tree"));
+ }
/* Keep only pages/type and (reduced) dest entries to avoid
* references to unretained pages */
@@ -291,7 +305,7 @@ static void retainpages(int argc, char **argv)
pages = fz_dict_gets(oldroot, "Pages");
olddests = pdf_load_name_tree(xref, "Dests");
- root = fz_new_dict(2);
+ root = fz_new_dict(ctx, 2);
fz_dict_puts(root, "Type", fz_dict_gets(oldroot, "Type"));
fz_dict_puts(root, "Pages", fz_dict_gets(oldroot, "Pages"));
@@ -300,8 +314,8 @@ static void retainpages(int argc, char **argv)
fz_drop_obj(root);
/* Create a new kids array with only the pages we want to keep */
- parent = fz_new_indirect(fz_to_num(pages), fz_to_gen(pages), xref);
- kids = fz_new_array(1);
+ parent = fz_new_indirect(ctx, fz_to_num(pages), fz_to_gen(pages), xref);
+ kids = fz_new_array(ctx, 1);
/* Retain pages specified */
while (argc - fz_optind)
@@ -356,7 +370,7 @@ static void retainpages(int argc, char **argv)
fz_drop_obj(parent);
/* Update page count and kids array */
- countobj = fz_new_int(fz_array_len(kids));
+ countobj = fz_new_int(ctx, fz_array_len(kids));
fz_dict_puts(pages, "Count", countobj);
fz_drop_obj(countobj);
fz_dict_puts(pages, "Kids", kids);
@@ -366,15 +380,15 @@ static void retainpages(int argc, char **argv)
if (olddests)
{
int i;
- fz_obj *names = fz_new_dict(1);
- fz_obj *dests = fz_new_dict(1);
- fz_obj *names_list = fz_new_array(32);
+ fz_obj *names = fz_new_dict(ctx, 1);
+ fz_obj *dests = fz_new_dict(ctx, 1);
+ fz_obj *names_list = fz_new_array(ctx, 32);
for (i = 0; i < fz_dict_len(olddests); i++)
{
fz_obj *key = fz_dict_get_key(olddests, i);
fz_obj *val = fz_dict_get_val(olddests, i);
- fz_obj *key_str = fz_new_string(fz_to_name(key), strlen(fz_to_name(key)));
+ fz_obj *key_str = fz_new_string(ctx, fz_to_name(key), strlen(fz_to_name(key)));
fz_obj *dest = fz_dict_gets(val, "D");
dest = fz_array_get(dest ? dest : val, 0);
@@ -404,7 +418,6 @@ static void retainpages(int argc, char **argv)
static void preloadobjstms(void)
{
- fz_error error;
fz_obj *obj;
int num;
@@ -412,9 +425,14 @@ static void preloadobjstms(void)
{
if (xref->table[num].type == 'o')
{
- error = pdf_load_object(&obj, xref, num, 0);
- if (error)
- die(error);
+ fz_try(ctx)
+ {
+ obj = pdf_load_object(xref, num, 0);
+ }
+ fz_catch(ctx)
+ {
+ die(1);
+ }
fz_drop_obj(obj);
}
}
@@ -446,7 +464,7 @@ static fz_buffer *hexbuf(unsigned char *p, int n)
fz_buffer *buf;
int x = 0;
- buf = fz_new_buffer(n * 2 + (n / 32) + 2);
+ buf = fz_new_buffer(ctx, n * 2 + (n / 32) + 2);
while (n--)
{
@@ -471,8 +489,8 @@ static void addhexfilter(fz_obj *dict)
fz_obj *f, *dp, *newf, *newdp;
fz_obj *ahx, *nullobj;
- ahx = fz_new_name("ASCIIHexDecode");
- nullobj = fz_new_null();
+ ahx = fz_new_name(ctx, "ASCIIHexDecode");
+ nullobj = fz_new_null(ctx);
newf = newdp = NULL;
f = fz_dict_gets(dict, "Filter");
@@ -480,13 +498,13 @@ static void addhexfilter(fz_obj *dict)
if (fz_is_name(f))
{
- newf = fz_new_array(2);
+ newf = fz_new_array(ctx, 2);
fz_array_push(newf, ahx);
fz_array_push(newf, f);
f = newf;
if (fz_is_dict(dp))
{
- newdp = fz_new_array(2);
+ newdp = fz_new_array(ctx, 2);
fz_array_push(newdp, nullobj);
fz_array_push(newdp, dp);
dp = newdp;
@@ -515,23 +533,27 @@ static void addhexfilter(fz_obj *dict)
static void copystream(fz_obj *obj, int num, int gen)
{
- fz_error error;
fz_buffer *buf, *tmp;
fz_obj *newlen;
- error = pdf_load_raw_stream(&buf, xref, num, gen);
- if (error)
- die(error);
+ fz_try(ctx)
+ {
+ buf = pdf_load_raw_stream(xref, num, gen);
+ }
+ fz_catch(ctx)
+ {
+ die(1);
+ }
if (doascii && isbinarystream(buf))
{
tmp = hexbuf(buf->data, buf->len);
- fz_drop_buffer(buf);
+ fz_drop_buffer(ctx, buf);
buf = tmp;
addhexfilter(obj);
- newlen = fz_new_int(buf->len);
+ newlen = fz_new_int(ctx, buf->len);
fz_dict_puts(obj, "Length", newlen);
fz_drop_obj(newlen);
}
@@ -542,18 +564,22 @@ static void copystream(fz_obj *obj, int num, int gen)
fwrite(buf->data, 1, buf->len, out);
fprintf(out, "endstream\nendobj\n\n");
- fz_drop_buffer(buf);
+ fz_drop_buffer(ctx, buf);
}
static void expandstream(fz_obj *obj, int num, int gen)
{
- fz_error error;
fz_buffer *buf, *tmp;
fz_obj *newlen;
- error = pdf_load_stream(&buf, xref, num, gen);
- if (error)
- die(error);
+ fz_try(ctx)
+ {
+ buf = pdf_load_stream(xref, num, gen);
+ }
+ fz_catch(ctx)
+ {
+ die(1);
+ }
fz_dict_dels(obj, "Filter");
fz_dict_dels(obj, "DecodeParms");
@@ -561,13 +587,13 @@ static void expandstream(fz_obj *obj, int num, int gen)
if (doascii && isbinarystream(buf))
{
tmp = hexbuf(buf->data, buf->len);
- fz_drop_buffer(buf);
+ fz_drop_buffer(ctx, buf);
buf = tmp;
addhexfilter(obj);
}
- newlen = fz_new_int(buf->len);
+ newlen = fz_new_int(ctx, buf->len);
fz_dict_puts(obj, "Length", newlen);
fz_drop_obj(newlen);
@@ -577,18 +603,22 @@ static void expandstream(fz_obj *obj, int num, int gen)
fwrite(buf->data, 1, buf->len, out);
fprintf(out, "endstream\nendobj\n\n");
- fz_drop_buffer(buf);
+ fz_drop_buffer(ctx, buf);
}
static void writeobject(int num, int gen)
{
- fz_error error;
fz_obj *obj;
fz_obj *type;
- error = pdf_load_object(&obj, xref, num, gen);
- if (error)
- die(error);
+ fz_try(ctx)
+ {
+ obj = pdf_load_object(xref, num, gen);
+ }
+ fz_catch(ctx)
+ {
+ die(1);
+ }
/* skip ObjStm and XRef objects */
if (fz_is_dict(obj))
@@ -616,7 +646,7 @@ static void writeobject(int num, int gen)
}
else
{
- if (doexpand && !pdf_is_jpx_image(obj))
+ if (doexpand && !pdf_is_jpx_image(ctx, obj))
expandstream(obj, num, gen);
else
copystream(obj, num, gen);
@@ -644,9 +674,9 @@ static void writexref(void)
}
fprintf(out, "\n");
- trailer = fz_new_dict(5);
+ trailer = fz_new_dict(ctx, 5);
- obj = fz_new_int(xref->len);
+ obj = fz_new_int(ctx, xref->len);
fz_dict_puts(trailer, "Size", obj);
fz_drop_obj(obj);
@@ -713,7 +743,6 @@ static void writepdf(void)
int main(int argc, char **argv)
{
- fz_error error;
char *infile;
char *outfile = "out.pdf";
char *password = "";
@@ -747,21 +776,30 @@ int main(int argc, char **argv)
if (argc - fz_optind > 0)
subset = 1;
- error = pdf_open_xref(&xref, infile, password);
- if (error)
- die(fz_rethrow(error, "cannot open input file '%s'", infile));
+ ctx = fz_new_context(&fz_alloc_default);
+ if (ctx == NULL)
+ die(fz_error_note(1, "failed to initialise context"));
+
+ fz_try(ctx)
+ {
+ xref = pdf_open_xref(ctx, infile, password);
+ }
+ fz_catch(ctx)
+ {
+ die(fz_error_note(1, "cannot open input file '%s'", infile));
+ }
out = fopen(outfile, "wb");
if (!out)
- die(fz_throw("cannot open output file '%s'", outfile));
+ die(fz_error_make("cannot open output file '%s'", outfile));
fprintf(out, "%%PDF-%d.%d\n", xref->version / 10, xref->version % 10);
fprintf(out, "%%\316\274\341\277\246\n\n");
- uselist = fz_calloc(xref->len + 1, sizeof(char));
- ofslist = fz_calloc(xref->len + 1, sizeof(int));
- genlist = fz_calloc(xref->len + 1, sizeof(int));
- renumbermap = fz_calloc(xref->len + 1, sizeof(int));
+ uselist = fz_malloc_array(ctx, xref->len + 1, sizeof(char));
+ ofslist = fz_malloc_array(ctx, xref->len + 1, sizeof(int));
+ genlist = fz_malloc_array(ctx, xref->len + 1, sizeof(int));
+ renumbermap = fz_malloc_array(ctx, xref->len + 1, sizeof(int));
for (num = 0; num < xref->len; num++)
{
@@ -800,16 +838,15 @@ int main(int argc, char **argv)
writepdf();
if (fclose(out))
- die(fz_throw("cannot close output file '%s'", outfile));
+ die(fz_error_make("cannot close output file '%s'", outfile));
- fz_free(uselist);
- fz_free(ofslist);
- fz_free(genlist);
- fz_free(renumbermap);
+ fz_free(xref->ctx, uselist);
+ fz_free(xref->ctx, ofslist);
+ fz_free(xref->ctx, genlist);
+ fz_free(xref->ctx, renumbermap);
pdf_free_xref(xref);
-
- fz_flush_warnings();
-
+ fz_flush_warnings(ctx);
+ fz_free_context(ctx);
return 0;
}
diff --git a/apps/pdfdraw.c b/apps/pdfdraw.c
index d79cd75c..fd7cc57f 100644
--- a/apps/pdfdraw.c
+++ b/apps/pdfdraw.c
@@ -38,7 +38,7 @@ struct {
static void die(fz_error error)
{
- fz_catch(error, "aborting");
+ fz_error_handle(error, "aborting");
exit(1);
}
@@ -94,36 +94,46 @@ static int isrange(char *s)
static void drawpage(pdf_xref *xref, int pagenum)
{
- fz_error error;
pdf_page *page;
fz_display_list *list;
fz_device *dev;
int start;
+ fz_context *ctx = xref->ctx;
if (showtime)
{
start = gettime();
}
- error = pdf_load_page(&page, xref, pagenum - 1);
- if (error)
- die(fz_rethrow(error, "cannot load page %d in file '%s'", pagenum, filename));
+ fz_try(ctx)
+ {
+ page = pdf_load_page(xref, pagenum - 1);
+ }
+ fz_catch(ctx)
+ {
+ die(fz_error_note(1, "cannot load page %d in file '%s'", pagenum, filename));
+ }
list = NULL;
if (uselist)
{
- list = fz_new_display_list();
- dev = fz_new_list_device(list);
- error = pdf_run_page(xref, page, dev, fz_identity);
- if (error)
- die(fz_rethrow(error, "cannot draw page %d in file '%s'", pagenum, filename));
+ list = fz_new_display_list(ctx);
+ dev = fz_new_list_device(ctx, list);
+ fz_try(ctx)
+ {
+ pdf_run_page(xref, page, dev, fz_identity);
+ }
+ fz_catch(ctx)
+ {
+ die(fz_error_note(1, "cannot draw page %d in file '%s'", pagenum, filename));
+ }
fz_free_device(dev);
}
if (showxml)
{
- dev = fz_new_trace_device();
+ dev = fz_new_trace_device(ctx);
printf("<page number=\"%d\">\n", pagenum);
if (list)
fz_execute_display_list(list, dev, fz_identity, fz_infinite_bbox);
@@ -135,8 +145,8 @@ static void drawpage(pdf_xref *xref, int pagenum)
if (showtext)
{
- fz_text_span *text = fz_new_text_span();
- dev = fz_new_text_device(text);
+ fz_text_span *text = fz_new_text_span(ctx);
+ dev = fz_new_text_device(ctx, text);
if (list)
fz_execute_display_list(list, dev, fz_identity, fz_infinite_bbox);
else
@@ -148,7 +158,7 @@ static void drawpage(pdf_xref *xref, int pagenum)
else
fz_debug_text_span(text);
printf("\n");
- fz_free_text_span(text);
+ fz_free_text_span(ctx, text);
}
if (showmd5 || showtime)
@@ -170,14 +180,14 @@ static void drawpage(pdf_xref *xref, int pagenum)
/* TODO: banded rendering and multi-page ppm */
- pix = fz_new_pixmap_with_rect(colorspace, bbox);
+ pix = fz_new_pixmap_with_rect(ctx, colorspace, bbox);
if (savealpha)
fz_clear_pixmap(pix);
else
fz_clear_pixmap_with_color(pix, 255);
- dev = fz_new_draw_device(glyphcache, pix);
+ dev = fz_new_draw_device(ctx, glyphcache, pix);
if (list)
fz_execute_display_list(list, dev, ctm, bbox);
else
@@ -194,17 +204,17 @@ static void drawpage(pdf_xref *xref, int pagenum)
char buf[512];
sprintf(buf, output, pagenum);
if (strstr(output, ".pgm") || strstr(output, ".ppm") || strstr(output, ".pnm"))
- fz_write_pnm(pix, buf);
+ fz_write_pnm(ctx, pix, buf);
else if (strstr(output, ".pam"))
- fz_write_pam(pix, buf, savealpha);
+ fz_write_pam(ctx, pix, buf, savealpha);
else if (strstr(output, ".png"))
- fz_write_png(pix, buf, savealpha);
+ fz_write_png(ctx, pix, buf, savealpha);
else if (strstr(output, ".pbm")) {
- fz_halftone *ht = fz_get_default_halftone(1);
- fz_bitmap *bit = fz_halftone_pixmap(pix, ht);
- fz_write_pbm(bit, buf);
- fz_drop_bitmap(bit);
- fz_drop_halftone(ht);
+ fz_halftone *ht = fz_get_default_halftone(ctx, 1);
+ fz_bitmap *bit = fz_halftone_pixmap(ctx, pix, ht);
+ fz_write_pbm(ctx, bit, buf);
+ fz_drop_bitmap(ctx, bit);
+ fz_drop_halftone(ctx, ht);
}
}
@@ -223,13 +233,13 @@ static void drawpage(pdf_xref *xref, int pagenum)
printf("%02x", digest[i]);
}
- fz_drop_pixmap(pix);
+ fz_drop_pixmap(ctx, pix);
}
if (list)
- fz_free_display_list(list);
+ fz_free_display_list(ctx, list);
- pdf_free_page(page);
+ pdf_free_page(ctx, page);
if (showtime)
{
@@ -255,9 +265,9 @@ static void drawpage(pdf_xref *xref, int pagenum)
if (showmd5 || showtime)
printf("\n");
- pdf_age_store(xref->store, 3);
+ pdf_age_store(ctx, xref->store, 3);
- fz_flush_warnings();
+ fz_flush_warnings(ctx);
}
static void drawrange(pdf_xref *xref, char *range)
@@ -315,6 +325,7 @@ int main(int argc, char **argv)
pdf_xref *xref;
fz_error error;
int c;
+ fz_context *ctx;
while ((c = fz_getopt(argc, argv, "lo:p:r:R:Aab:dgmtx5G:I")) != -1)
{
@@ -354,7 +365,14 @@ int main(int argc, char **argv)
if (accelerate)
fz_accelerate();
- glyphcache = fz_new_glyph_cache();
+ ctx = fz_new_context(&fz_alloc_default);
+ if (ctx == NULL)
+ {
+ fprintf(stderr, "Failed to init context\n");
+ exit(1);
+ }
+
+ glyphcache = fz_new_glyph_cache(ctx);
colorspace = fz_device_rgb;
if (grayscale)
@@ -380,13 +398,23 @@ int main(int argc, char **argv)
{
filename = argv[fz_optind++];
- error = pdf_open_xref(&xref, filename, password);
- if (error)
- die(fz_rethrow(error, "cannot open document: %s", filename));
+ fz_try(ctx)
+ {
+ xref = pdf_open_xref(ctx, filename, password);
+ }
+ fz_catch(ctx)
+ {
+ die(fz_error_note(error, "cannot open document: %s", filename));
+ }
- error = pdf_load_page_tree(xref);
- if (error)
- die(fz_rethrow(error, "cannot load page tree: %s", filename));
+ fz_try(ctx)
+ {
+ pdf_load_page_tree(xref);
+ }
+ fz_catch(ctx)
+ {
+ die(fz_error_note(error, "cannot load page tree: %s", filename));
+ }
if (showxml)
printf("<document name=\"%s\">\n", filename);
@@ -416,9 +444,8 @@ int main(int argc, char **argv)
printf("slowest page %d: %dms\n", timing.maxpage, timing.max);
}
- fz_free_glyph_cache(glyphcache);
-
- fz_flush_warnings();
-
+ fz_free_glyph_cache(ctx, glyphcache);
+ fz_flush_warnings(ctx);
+ fz_free_context(ctx);
return 0;
}
diff --git a/apps/pdfextract.c b/apps/pdfextract.c
index 7e6e4ace..bf0a74ed 100644
--- a/apps/pdfextract.c
+++ b/apps/pdfextract.c
@@ -6,11 +6,12 @@
#include "mupdf.h"
static pdf_xref *xref = NULL;
+static fz_context *ctx = NULL;
static int dorgb = 0;
void die(fz_error error)
{
- fz_catch(error, "aborting");
+ fz_error_handle(error, "aborting");
if (xref)
pdf_free_xref(xref);
exit(1);
@@ -38,25 +39,29 @@ static int isfontdesc(fz_obj *obj)
static void saveimage(int num)
{
- fz_error error;
fz_pixmap *img;
fz_obj *ref;
char name[1024];
- ref = fz_new_indirect(num, 0, xref);
+ ref = fz_new_indirect(ctx, num, 0, xref);
/* TODO: detect DCTD and save as jpeg */
- error = pdf_load_image(&img, xref, ref);
- if (error)
- die(error);
+ fz_try(ctx)
+ {
+ img = pdf_load_image(xref, ref);
+ }
+ fz_catch(ctx)
+ {
+ die(1);
+ }
if (dorgb && img->colorspace && img->colorspace != fz_device_rgb)
{
fz_pixmap *temp;
- temp = fz_new_pixmap_with_rect(fz_device_rgb, fz_bound_pixmap(img));
- fz_convert_pixmap(img, temp);
- fz_drop_pixmap(img);
+ temp = fz_new_pixmap_with_rect(ctx, fz_device_rgb, fz_bound_pixmap(img));
+ fz_convert_pixmap(ctx, img, temp);
+ fz_drop_pixmap(ctx, img);
img = temp;
}
@@ -64,22 +69,21 @@ static void saveimage(int num)
{
sprintf(name, "img-%04d.png", num);
printf("extracting image %s\n", name);
- fz_write_png(img, name, 0);
+ fz_write_png(ctx, img, name, 0);
}
else
{
sprintf(name, "img-%04d.pam", num);
printf("extracting image %s\n", name);
- fz_write_pam(img, name, 0);
+ fz_write_pam(ctx, img, name, 0);
}
- fz_drop_pixmap(img);
+ fz_drop_pixmap(ctx, img);
fz_drop_obj(ref);
}
static void savefont(fz_obj *dict, int num)
{
- fz_error error;
char name[1024];
char *subtype;
fz_buffer *buf;
@@ -115,7 +119,7 @@ static void savefont(fz_obj *dict, int num)
obj = fz_dict_gets(obj, "Subtype");
if (obj && !fz_is_name(obj))
- die(fz_throw("Invalid font descriptor subtype"));
+ die(fz_error_make("Invalid font descriptor subtype"));
subtype = fz_to_name(obj);
if (!strcmp(subtype, "Type1C"))
@@ -123,49 +127,56 @@ static void savefont(fz_obj *dict, int num)
else if (!strcmp(subtype, "CIDFontType0C"))
ext = "cid";
else
- die(fz_throw("Unhandled font type '%s'", subtype));
+ die(fz_error_make("Unhandled font type '%s'", subtype));
}
if (!stream)
{
- fz_warn("Unhandled font type");
+ fz_warn(ctx, "Unhandled font type");
return;
}
- buf = fz_new_buffer(0);
-
- error = pdf_load_stream(&buf, xref, fz_to_num(stream), fz_to_gen(stream));
- if (error)
- die(error);
+ fz_try(ctx)
+ {
+ buf = pdf_load_stream(xref, fz_to_num(stream), fz_to_gen(stream));
+ }
+ fz_catch(ctx)
+ {
+ die(1);
+ }
sprintf(name, "%s-%04d.%s", fontname, num, ext);
printf("extracting font %s\n", name);
f = fopen(name, "wb");
if (f == NULL)
- die(fz_throw("Error creating font file"));
+ die(fz_error_make("Error creating font file"));
n = fwrite(buf->data, 1, buf->len, f);
if (n < buf->len)
- die(fz_throw("Error writing font file"));
+ die(fz_error_make("Error writing font file"));
if (fclose(f) < 0)
- die(fz_throw("Error closing font file"));
+ die(fz_error_make("Error closing font file"));
- fz_drop_buffer(buf);
+ fz_drop_buffer(ctx, buf);
}
static void showobject(int num)
{
- fz_error error;
fz_obj *obj;
if (!xref)
- die(fz_throw("no file specified"));
+ die(fz_error_make("no file specified"));
- error = pdf_load_object(&obj, xref, num, 0);
- if (error)
- die(error);
+ fz_try(ctx)
+ {
+ obj = pdf_load_object(xref, num, 0);
+ }
+ fz_catch(ctx)
+ {
+ die(1);
+ }
if (isimage(obj))
saveimage(num);
@@ -177,7 +188,6 @@ static void showobject(int num)
int main(int argc, char **argv)
{
- fz_error error;
char *infile;
char *password = "";
int c, o;
@@ -196,9 +206,19 @@ int main(int argc, char **argv)
usage();
infile = argv[fz_optind++];
- error = pdf_open_xref(&xref, infile, password);
- if (error)
- die(fz_rethrow(error, "cannot open input file '%s'", infile));
+
+ ctx = fz_new_context(&fz_alloc_default);
+ if (ctx == NULL)
+ die(fz_error_note(1, "failed to initialise context"));
+
+ fz_try(ctx)
+ {
+ xref = pdf_open_xref(ctx, infile, password);
+ }
+ fz_catch(ctx)
+ {
+ die(fz_error_note(1, "cannot open input file '%s'", infile));
+ }
if (fz_optind == argc)
{
@@ -215,8 +235,7 @@ int main(int argc, char **argv)
}
pdf_free_xref(xref);
-
- fz_flush_warnings();
-
+ fz_flush_warnings(ctx);
+ fz_free_context(ctx);
return 0;
}
diff --git a/apps/pdfinfo.c b/apps/pdfinfo.c
index d8948264..d2aa0bb4 100644
--- a/apps/pdfinfo.c
+++ b/apps/pdfinfo.c
@@ -7,13 +7,14 @@
#include "mupdf.h"
pdf_xref *xref;
+fz_context *ctx;
int pagecount;
void closexref(void);
void die(fz_error error)
{
- fz_catch(error, "aborting");
+ fz_error_handle(error, "aborting");
closexref();
exit(1);
}
@@ -107,57 +108,57 @@ void closexref(void)
if (dim)
{
for (i = 0; i < dims; i++)
- fz_free(dim[i].u.dim.bbox);
- fz_free(dim);
+ fz_free(ctx, dim[i].u.dim.bbox);
+ fz_free(ctx, dim);
dim = NULL;
dims = 0;
}
if (font)
{
- fz_free(font);
+ fz_free(ctx, font);
font = NULL;
fonts = 0;
}
if (image)
{
- fz_free(image);
+ fz_free(ctx, image);
image = NULL;
images = 0;
}
if (shading)
{
- fz_free(shading);
+ fz_free(ctx, shading);
shading = NULL;
shadings = 0;
}
if (pattern)
{
- fz_free(pattern);
+ fz_free(ctx, pattern);
pattern = NULL;
patterns = 0;
}
if (form)
{
- fz_free(form);
+ fz_free(ctx, form);
form = NULL;
forms = 0;
}
if (psobj)
{
- fz_free(psobj);
+ fz_free(ctx, psobj);
psobj = NULL;
psobjs = 0;
}
if (xref && xref->store)
{
- pdf_free_store(xref->store);
+ pdf_free_store(ctx, xref->store);
xref->store = NULL;
}
}
@@ -212,7 +213,7 @@ gatherdimensions(int page, fz_obj *pageref, fz_obj *pageobj)
if (!fz_is_array(obj))
return;
- bbox = pdf_to_rect(obj);
+ bbox = pdf_to_rect(ctx, obj);
for (j = 0; j < dims; j++)
if (!memcmp(dim[j].u.dim.bbox, &bbox, sizeof (fz_rect)))
@@ -223,11 +224,11 @@ gatherdimensions(int page, fz_obj *pageref, fz_obj *pageobj)
dims++;
- dim = fz_realloc(dim, dims, sizeof(struct info));
+ dim = fz_resize_array(ctx, dim, dims, sizeof(struct info));
dim[dims - 1].page = page;
dim[dims - 1].pageref = pageref;
dim[dims - 1].pageobj = pageobj;
- dim[dims - 1].u.dim.bbox = fz_malloc(sizeof(fz_rect));
+ dim[dims - 1].u.dim.bbox = fz_malloc(ctx, sizeof(fz_rect));
memcpy(dim[dims - 1].u.dim.bbox, &bbox, sizeof (fz_rect));
return;
@@ -236,9 +237,10 @@ gatherdimensions(int page, fz_obj *pageref, fz_obj *pageobj)
static void
gatherfonts(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
{
- int i;
+ int i, n;
- for (i = 0; i < fz_dict_len(dict); i++)
+ n = fz_dict_len(dict);
+ for (i = 0; i < n; i++)
{
fz_obj *fontdict = NULL;
fz_obj *subtype = NULL;
@@ -249,7 +251,7 @@ gatherfonts(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
fontdict = fz_dict_get_val(dict, i);
if (!fz_is_dict(fontdict))
{
- fz_warn("not a font dict (%d %d R)", fz_to_num(fontdict), fz_to_gen(fontdict));
+ fz_warn(ctx, "not a font dict (%d %d R)", fz_to_num(fontdict), fz_to_gen(fontdict));
continue;
}
@@ -267,7 +269,7 @@ gatherfonts(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
fonts++;
- font = fz_realloc(font, fonts, sizeof(struct info));
+ font = fz_resize_array(ctx, font, fonts, sizeof(struct info));
font[fonts - 1].page = page;
font[fonts - 1].pageref = pageref;
font[fonts - 1].pageobj = pageobj;
@@ -280,9 +282,10 @@ gatherfonts(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
static void
gatherimages(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
{
- int i;
+ int i, n;
- for (i = 0; i < fz_dict_len(dict); i++)
+ n = fz_dict_len(dict);
+ for (i = 0; i < n; i++)
{
fz_obj *imagedict;
fz_obj *type;
@@ -297,7 +300,7 @@ gatherimages(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
imagedict = fz_dict_get_val(dict, i);
if (!fz_is_dict(imagedict))
{
- fz_warn("not an image dict (%d %d R)", fz_to_num(imagedict), fz_to_gen(imagedict));
+ fz_warn(ctx, "not an image dict (%d %d R)", fz_to_num(imagedict), fz_to_gen(imagedict));
continue;
}
@@ -335,7 +338,7 @@ gatherimages(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
images++;
- image = fz_realloc(image, images, sizeof(struct info));
+ image = fz_resize_array(ctx, image, images, sizeof(struct info));
image[images - 1].page = page;
image[images - 1].pageref = pageref;
image[images - 1].pageobj = pageobj;
@@ -352,9 +355,10 @@ gatherimages(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
static void
gatherforms(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
{
- int i;
+ int i, n;
- for (i = 0; i < fz_dict_len(dict); i++)
+ n = fz_dict_len(dict);
+ for (i = 0; i < n; i++)
{
fz_obj *xobjdict;
fz_obj *type;
@@ -367,7 +371,7 @@ gatherforms(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
xobjdict = fz_dict_get_val(dict, i);
if (!fz_is_dict(xobjdict))
{
- fz_warn("not a xobject dict (%d %d R)", fz_to_num(xobjdict), fz_to_gen(xobjdict));
+ fz_warn(ctx, "not a xobject dict (%d %d R)", fz_to_num(xobjdict), fz_to_gen(xobjdict));
continue;
}
@@ -392,7 +396,7 @@ gatherforms(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
forms++;
- form = fz_realloc(form, forms, sizeof(struct info));
+ form = fz_resize_array(ctx, form, forms, sizeof(struct info));
form[forms - 1].page = page;
form[forms - 1].pageref = pageref;
form[forms - 1].pageobj = pageobj;
@@ -405,9 +409,10 @@ gatherforms(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
static void
gatherpsobjs(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
{
- int i;
+ int i, n;
- for (i = 0; i < fz_dict_len(dict); i++)
+ n = fz_dict_len(dict);
+ for (i = 0; i < n; i++)
{
fz_obj *xobjdict;
fz_obj *type;
@@ -417,7 +422,7 @@ gatherpsobjs(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
xobjdict = fz_dict_get_val(dict, i);
if (!fz_is_dict(xobjdict))
{
- fz_warn("not a xobject dict (%d %d R)", fz_to_num(xobjdict), fz_to_gen(xobjdict));
+ fz_warn(ctx, "not a xobject dict (%d %d R)", fz_to_num(xobjdict), fz_to_gen(xobjdict));
continue;
}
@@ -436,7 +441,7 @@ gatherpsobjs(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
psobjs++;
- psobj = fz_realloc(psobj, psobjs, sizeof(struct info));
+ psobj = fz_resize_array(ctx, psobj, psobjs, sizeof(struct info));
psobj[psobjs - 1].page = page;
psobj[psobjs - 1].pageref = pageref;
psobj[psobjs - 1].pageobj = pageobj;
@@ -447,9 +452,10 @@ gatherpsobjs(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
static void
gathershadings(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
{
- int i;
+ int i, n;
- for (i = 0; i < fz_dict_len(dict); i++)
+ n = fz_dict_len(dict);
+ for (i = 0; i < n; i++)
{
fz_obj *shade;
fz_obj *type;
@@ -458,14 +464,14 @@ gathershadings(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
shade = fz_dict_get_val(dict, i);
if (!fz_is_dict(shade))
{
- fz_warn("not a shading dict (%d %d R)", fz_to_num(shade), fz_to_gen(shade));
+ fz_warn(ctx, "not a shading dict (%d %d R)", fz_to_num(shade), fz_to_gen(shade));
continue;
}
type = fz_dict_gets(shade, "ShadingType");
if (!fz_is_int(type) || fz_to_int(type) < 1 || fz_to_int(type) > 7)
{
- fz_warn("not a shading type (%d %d R)", fz_to_num(shade), fz_to_gen(shade));
+ fz_warn(ctx, "not a shading type (%d %d R)", fz_to_num(shade), fz_to_gen(shade));
type = NULL;
}
@@ -478,7 +484,7 @@ gathershadings(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
shadings++;
- shading = fz_realloc(shading, shadings, sizeof(struct info));
+ shading = fz_resize_array(ctx, shading, shadings, sizeof(struct info));
shading[shadings - 1].page = page;
shading[shadings - 1].pageref = pageref;
shading[shadings - 1].pageobj = pageobj;
@@ -490,9 +496,10 @@ gathershadings(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
static void
gatherpatterns(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
{
- int i;
+ int i, n;
- for (i = 0; i < fz_dict_len(dict); i++)
+ n = fz_dict_len(dict);
+ for (i = 0; i < n; i++)
{
fz_obj *patterndict;
fz_obj *type;
@@ -504,14 +511,14 @@ gatherpatterns(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
patterndict = fz_dict_get_val(dict, i);
if (!fz_is_dict(patterndict))
{
- fz_warn("not a pattern dict (%d %d R)", fz_to_num(patterndict), fz_to_gen(patterndict));
+ fz_warn(ctx, "not a pattern dict (%d %d R)", fz_to_num(patterndict), fz_to_gen(patterndict));
continue;
}
type = fz_dict_gets(patterndict, "PatternType");
if (!fz_is_int(type) || fz_to_int(type) < 1 || fz_to_int(type) > 2)
{
- fz_warn("not a pattern type (%d %d R)", fz_to_num(patterndict), fz_to_gen(patterndict));
+ fz_warn(ctx, "not a pattern type (%d %d R)", fz_to_num(patterndict), fz_to_gen(patterndict));
type = NULL;
}
@@ -520,14 +527,14 @@ gatherpatterns(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
paint = fz_dict_gets(patterndict, "PaintType");
if (!fz_is_int(paint) || fz_to_int(paint) < 1 || fz_to_int(paint) > 2)
{
- fz_warn("not a pattern paint type (%d %d R)", fz_to_num(patterndict), fz_to_gen(patterndict));
+ fz_warn(ctx, "not a pattern paint type (%d %d R)", fz_to_num(patterndict), fz_to_gen(patterndict));
paint = NULL;
}
tiling = fz_dict_gets(patterndict, "TilingType");
if (!fz_is_int(tiling) || fz_to_int(tiling) < 1 || fz_to_int(tiling) > 3)
{
- fz_warn("not a pattern tiling type (%d %d R)", fz_to_num(patterndict), fz_to_gen(patterndict));
+ fz_warn(ctx, "not a pattern tiling type (%d %d R)", fz_to_num(patterndict), fz_to_gen(patterndict));
tiling = NULL;
}
}
@@ -545,7 +552,7 @@ gatherpatterns(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
patterns++;
- pattern = fz_realloc(pattern, patterns, sizeof(struct info));
+ pattern = fz_resize_array(ctx, pattern, patterns, sizeof(struct info));
pattern[patterns - 1].page = page;
pattern[patterns - 1].pageref = pageref;
pattern[patterns - 1].pageobj = pageobj;
@@ -573,14 +580,16 @@ gatherresourceinfo(int page, fz_obj *rsrc)
pageref = xref->page_refs[page-1];
if (!pageobj)
- die(fz_throw("cannot retrieve info from page %d", page));
+ die(fz_error_make("cannot retrieve info from page %d", page));
font = fz_dict_gets(rsrc, "Font");
if (font)
{
- gatherfonts(page, pageref, pageobj, font);
+ int n;
- for (i = 0; i < fz_dict_len(font); i++)
+ gatherfonts(page, pageref, pageobj, font);
+ n = fz_dict_len(font);
+ for (i = 0; i < n; i++)
{
fz_obj *obj = fz_dict_get_val(font, i);
@@ -593,11 +602,13 @@ gatherresourceinfo(int page, fz_obj *rsrc)
xobj = fz_dict_gets(rsrc, "XObject");
if (xobj)
{
+ int n;
+
gatherimages(page, pageref, pageobj, xobj);
gatherforms(page, pageref, pageobj, xobj);
gatherpsobjs(page, pageref, pageobj, xobj);
-
- for (i = 0; i < fz_dict_len(xobj); i++)
+ n = fz_dict_len(xobj);
+ for (i = 0; i < n; i++)
{
fz_obj *obj = fz_dict_get_val(xobj, i);
subrsrc = fz_dict_gets(obj, "Resources");
@@ -613,9 +624,10 @@ gatherresourceinfo(int page, fz_obj *rsrc)
pattern = fz_dict_gets(rsrc, "Pattern");
if (pattern)
{
+ int n;
gatherpatterns(page, pageref, pageobj, pattern);
-
- for (i = 0; i < fz_dict_len(pattern); i++)
+ n = fz_dict_len(pattern);
+ for (i = 0; i < n; i++)
{
fz_obj *obj = fz_dict_get_val(pattern, i);
subrsrc = fz_dict_gets(obj, "Resources");
@@ -636,7 +648,7 @@ gatherpageinfo(int page)
pageref = xref->page_refs[page-1];
if (!pageobj)
- die(fz_throw("cannot retrieve info from page %d", page));
+ die(fz_error_make("cannot retrieve info from page %d", page));
gatherdimensions(page, pageref, pageobj);
@@ -696,10 +708,12 @@ printinfo(char *filename, int show, int page)
fz_to_num(image[i].pageref), fz_to_gen(image[i].pageref));
if (fz_is_array(image[i].u.image.filter))
- for (j = 0; j < fz_array_len(image[i].u.image.filter); j++)
+ {
+ int n = fz_array_len(image[i].u.image.filter);
+ for (j = 0; j < n; j++)
{
fz_obj *obj = fz_array_get(image[i].u.image.filter, j);
- char *filter = fz_strdup(fz_to_name(obj));
+ char *filter = fz_strdup(ctx, fz_to_name(obj));
if (strstr(filter, "Decode"))
*(strstr(filter, "Decode")) = '\0';
@@ -707,25 +721,26 @@ printinfo(char *filename, int show, int page)
printf("%s%s",
filter,
j == fz_array_len(image[i].u.image.filter) - 1 ? "" : " ");
- fz_free(filter);
+ fz_free(ctx, filter);
}
+ }
else if (image[i].u.image.filter)
{
fz_obj *obj = image[i].u.image.filter;
- char *filter = fz_strdup(fz_to_name(obj));
+ char *filter = fz_strdup(ctx, fz_to_name(obj));
if (strstr(filter, "Decode"))
*(strstr(filter, "Decode")) = '\0';
printf("%s", filter);
- fz_free(filter);
+ fz_free(ctx, filter);
}
else
printf("Raw");
if (image[i].u.image.cs)
{
- cs = fz_strdup(fz_to_name(image[i].u.image.cs));
+ cs = fz_strdup(ctx, fz_to_name(image[i].u.image.cs));
if (!strncmp(cs, "Device", 6))
{
@@ -744,7 +759,7 @@ printinfo(char *filename, int show, int page)
}
if (image[i].u.image.altcs)
{
- altcs = fz_strdup(fz_to_name(image[i].u.image.altcs));
+ altcs = fz_strdup(ctx, fz_to_name(image[i].u.image.altcs));
if (!strncmp(altcs, "Device", 6))
{
@@ -771,8 +786,8 @@ printinfo(char *filename, int show, int page)
image[i].u.image.altcs ? altcs : "",
fz_to_num(image[i].u.image.obj), fz_to_gen(image[i].u.image.obj));
- fz_free(cs);
- fz_free(altcs);
+ fz_free(ctx, cs);
+ fz_free(ctx, altcs);
}
printf("\n");
}
@@ -966,6 +981,10 @@ int main(int argc, char **argv)
if (fz_optind == argc)
infousage();
+ ctx = fz_new_context();
+ if (ctx == NULL)
+ die(fz_error_make("failed to initialise context"));
+
state = NO_FILE_OPENED;
while (fz_optind < argc)
{
@@ -981,13 +1000,13 @@ int main(int argc, char **argv)
filename = argv[fz_optind];
printf("%s:\n", filename);
- error = pdf_open_xref(&xref, filename, password);
+ error = pdf_open_xref(ctx, &xref, filename, password);
if (error)
- die(fz_rethrow(error, "cannot open input file '%s'", filename));
+ die(fz_error_note(error, "cannot open input file '%s'", filename));
error = pdf_load_page_tree(xref);
if (error)
- die(fz_rethrow(error, "cannot load page tree: %s", filename));
+ die(fz_error_note(error, "cannot load page tree: %s", filename));
pagecount = pdf_count_pages(xref);
showglobalinfo();
@@ -1006,6 +1025,6 @@ int main(int argc, char **argv)
showinfo(filename, show, "1-");
closexref();
-
+ fz_free_context(ctx);
return 0;
}
diff --git a/apps/pdfshow.c b/apps/pdfshow.c
index 5e74042b..fee20d2f 100644
--- a/apps/pdfshow.c
+++ b/apps/pdfshow.c
@@ -6,13 +6,14 @@
#include "mupdf.h"
static pdf_xref *xref = NULL;
+static fz_context *ctx = NULL;
static int showbinary = 0;
static int showdecode = 1;
static int showcolumn;
void die(fz_error error)
{
- fz_catch(error, "aborting");
+ fz_error_handle(error, "aborting");
if (xref)
pdf_free_xref(xref);
exit(1);
@@ -30,7 +31,7 @@ static void usage(void)
static void showtrailer(void)
{
if (!xref)
- die(fz_throw("no file specified"));
+ die(fz_error_make("no file specified"));
printf("trailer\n");
fz_debug_obj(xref->trailer);
printf("\n");
@@ -39,26 +40,30 @@ static void showtrailer(void)
static void showxref(void)
{
if (!xref)
- die(fz_throw("no file specified"));
+ die(fz_error_make("no file specified"));
pdf_debug_xref(xref);
printf("\n");
}
static void showpagetree(void)
{
- fz_error error;
fz_obj *ref;
int count;
int i;
if (!xref)
- die(fz_throw("no file specified"));
+ die(fz_error_make("no file specified"));
if (!xref->page_len)
{
- error = pdf_load_page_tree(xref);
- if (error)
- die(fz_rethrow(error, "cannot load page tree"));
+ fz_try(xref->ctx)
+ {
+ pdf_load_page_tree(xref);
+ }
+ fz_catch(xref->ctx)
+ {
+ die(fz_error_note(1, "cannot load page tree"));
+ }
}
count = pdf_count_pages(xref);
@@ -95,19 +100,23 @@ static void showsafe(unsigned char *buf, int n)
static void showstream(int num, int gen)
{
- fz_error error;
fz_stream *stm;
unsigned char buf[2048];
int n;
showcolumn = 0;
- if (showdecode)
- error = pdf_open_stream(&stm, xref, num, gen);
- else
- error = pdf_open_raw_stream(&stm, xref, num, gen);
- if (error)
- die(error);
+ fz_try(xref->ctx)
+ {
+ if (showdecode)
+ stm = pdf_open_stream(xref, num, gen);
+ else
+ stm = pdf_open_raw_stream(xref, num, gen);
+ }
+ fz_catch(xref->ctx)
+ {
+ die(1);
+ }
while (1)
{
@@ -127,15 +136,19 @@ static void showstream(int num, int gen)
static void showobject(int num, int gen)
{
- fz_error error;
fz_obj *obj;
if (!xref)
- die(fz_throw("no file specified"));
+ die(fz_error_make("no file specified"));
- error = pdf_load_object(&obj, xref, num, gen);
- if (error)
- die(error);
+ fz_try(ctx)
+ {
+ obj = pdf_load_object(xref, num, gen);
+ }
+ fz_catch(ctx)
+ {
+ die(1);
+ }
if (pdf_is_stream(xref, num, gen))
{
@@ -165,7 +178,6 @@ static void showobject(int num, int gen)
static void showgrep(char *filename)
{
- fz_error error;
fz_obj *obj;
int i;
@@ -173,9 +185,14 @@ static void showgrep(char *filename)
{
if (xref->table[i].type == 'n' || xref->table[i].type == 'o')
{
- error = pdf_load_object(&obj, xref, i, 0);
- if (error)
- die(error);
+ fz_try(ctx)
+ {
+ obj = pdf_load_object(xref, i, 0);
+ }
+ fz_catch(ctx)
+ {
+ die(1);
+ }
fz_sort_dict(obj);
@@ -194,7 +211,6 @@ int main(int argc, char **argv)
{
char *password = NULL; /* don't throw errors if encrypted */
char *filename;
- fz_error error;
int c;
while ((c = fz_getopt(argc, argv, "p:be")) != -1)
@@ -212,9 +228,19 @@ int main(int argc, char **argv)
usage();
filename = argv[fz_optind++];
- error = pdf_open_xref(&xref, filename, password);
- if (error)
- die(fz_rethrow(error, "cannot open document: %s", filename));
+
+ ctx = fz_new_context(&fz_alloc_default);
+ if (ctx == NULL)
+ die(fz_error_note(1, "failed to initialise context"));
+
+ fz_try(ctx)
+ {
+ xref = pdf_open_xref(ctx, filename, password);
+ }
+ fz_catch(ctx)
+ {
+ die(fz_error_note(1, "cannot open document: %s", filename));
+ }
if (fz_optind == argc)
showtrailer();
@@ -233,8 +259,7 @@ int main(int argc, char **argv)
}
pdf_free_xref(xref);
-
- fz_flush_warnings();
-
+ fz_flush_warnings(ctx);
+ fz_free_context(ctx);
return 0;
}
diff --git a/apps/win_main.c b/apps/win_main.c
index da97b011..3e293957 100644
--- a/apps/win_main.c
+++ b/apps/win_main.c
@@ -37,6 +37,7 @@ static pdfapp_t gapp;
static wchar_t wbuf[1024];
static char filename[1024];
+static fz_context *context;
/*
* Create registry keys to associate MuPDF with PDF and XPS files.
@@ -98,7 +99,7 @@ void winerror(pdfapp_t *app, fz_error error)
int i;
/* TODO: redirect stderr to a log file and display here */
- fz_catch(error, "displaying error message to user");
+ fz_error_handle(error, "displaying error message to user");
fz_strlcpy(msgbuf, "An error has occurred.\n\n", sizeof msgbuf);
for (i = 0; i < fz_get_error_count(); i++)
@@ -122,7 +123,7 @@ void win32error(char *msg)
code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&buf, 0, NULL);
- winerror(&gapp, fz_throw("%s:\n%s", msg, buf));
+ winerror(&gapp, fz_error_make("%s:\n%s", msg, buf));
}
int winfilename(wchar_t *buf, int len)
@@ -247,9 +248,9 @@ dloginfoproc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
#define SETUCS(ID) \
{ \
unsigned short *ucs; \
- ucs = pdf_to_ucs2(obj); \
+ ucs = pdf_to_ucs2(xref->ctx, obj); \
SetDlgItemTextW(hwnd, ID, ucs); \
- fz_free(ucs); \
+ fz_free(context, ucs); \
}
if ((obj = fz_dict_gets(info, "Title")))
@@ -617,7 +618,7 @@ void winreloadfile(pdfapp_t *app)
fd = _wopen(wbuf, O_BINARY | O_RDONLY, 0666);
if (fd < 0)
- winerror(&gapp, fz_throw("cannot reload file '%s'", filename));
+ winerror(&gapp, fz_error_make("cannot reload file '%s'", filename));
pdfapp_open(app, filename, fd, 1);
}
@@ -853,10 +854,17 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShow
MSG msg;
int fd;
int code;
+ fz_context *ctx;
fz_accelerate();
- pdfapp_init(&gapp);
+ ctx = fz_new_context(&fz_alloc_default);
+ if (ctx == NULL)
+ {
+ fprintf(stderr, "Failed to init context");
+ exit(1);
+ }
+ pdfapp_init(ctx, &gapp);
GetModuleFileNameA(NULL, argv0, sizeof argv0);
install_app(argv0);
@@ -875,7 +883,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShow
fd = _wopen(wbuf, O_BINARY | O_RDONLY, 0666);
if (fd < 0)
- winerror(&gapp, fz_throw("cannot open file '%s'", filename));
+ winerror(&gapp, fz_error_make("cannot open file '%s'", filename));
code = WideCharToMultiByte(CP_UTF8, 0, wbuf, -1, filename, sizeof filename, NULL, NULL);
if (code == 0)
diff --git a/apps/x11_main.c b/apps/x11_main.c
index a2d36ac5..5b19a176 100644
--- a/apps/x11_main.c
+++ b/apps/x11_main.c
@@ -105,7 +105,7 @@ void winwarn(pdfapp_t *app, char *msg)
void winerror(pdfapp_t *app, fz_error error)
{
- fz_catch(error, "aborting");
+ fz_error_handle(error, "aborting");
exit(1);
}
@@ -127,7 +127,7 @@ static void winopen(void)
xdpy = XOpenDisplay(NULL);
if (!xdpy)
- winerror(&gapp, fz_throw("cannot open display"));
+ winerror(&gapp, fz_error_make("cannot open display"));
XA_TARGETS = XInternAtom(xdpy, "TARGETS", False);
XA_TIMESTAMP = XInternAtom(xdpy, "TIMESTAMP", False);
@@ -161,7 +161,7 @@ static void winopen(void)
0,
NULL);
if (xwin == None)
- winerror(&gapp, fz_throw("cannot create window"));
+ winerror(&gapp, fz_error_make("cannot create window"));
XSetWindowColormap(xdpy, xwin, ximage_get_colormap());
XSelectInput(xdpy, xwin,
@@ -501,7 +501,7 @@ void winreloadfile(pdfapp_t *app)
fd = open(filename, O_BINARY | O_RDONLY, 0666);
if (fd < 0)
- winerror(app, fz_throw("cannot reload file '%s'", filename));
+ winerror(app, fz_error_make("cannot reload file '%s'", filename));
pdfapp_open(app, filename, fd, 1);
}
@@ -581,10 +581,14 @@ int main(int argc, char **argv)
fd_set fds;
int width = -1;
int height = -1;
+<<<<<<< HEAD
+ fz_context *ctx;
+=======
struct timeval tmo_at;
struct timeval now;
struct timeval tmo;
struct timeval *timeout;
+>>>>>>> master
while ((c = fz_getopt(argc, argv, "p:r:b:A")) != -1)
{
@@ -609,8 +613,18 @@ int main(int argc, char **argv)
if (accelerate)
fz_accelerate();
+ ctx = fz_new_context();
+ if (ctx == NULL)
+ {
+ fprintf(stderr, "failed to initialise context");
+ exit(1);
+ }
+
winopen();
+<<<<<<< HEAD
+ pdfapp_init(ctx, &gapp);
+=======
if (resolution == -1)
resolution = winresolution();
if (resolution < MINRES)
@@ -619,6 +633,7 @@ int main(int argc, char **argv)
resolution = MAXRES;
pdfapp_init(&gapp);
+>>>>>>> master
gapp.scrw = DisplayWidth(xdpy, xscr);
gapp.scrh = DisplayHeight(xdpy, xscr);
gapp.resolution = resolution;
@@ -626,7 +641,7 @@ int main(int argc, char **argv)
fd = open(filename, O_BINARY | O_RDONLY, 0666);
if (fd < 0)
- winerror(&gapp, fz_throw("cannot open file '%s'", filename));
+ winerror(&gapp, fz_error_make("cannot open file '%s'", filename));
pdfapp_open(&gapp, filename, fd, 0);
@@ -812,5 +827,7 @@ int main(int argc, char **argv)
XCloseDisplay(xdpy);
+ fz_free_context(ctx);
+
return 0;
}
diff --git a/apps/xpsdraw.c b/apps/xpsdraw.c
index 1d2992bc..4ac40b72 100644
--- a/apps/xpsdraw.c
+++ b/apps/xpsdraw.c
@@ -21,6 +21,7 @@ int uselist = 1;
fz_colorspace *colorspace;
fz_glyph_cache *glyphcache;
char *filename;
+fz_context *ctx;
struct {
int count, total;
@@ -30,7 +31,7 @@ struct {
static void die(fz_error error)
{
- fz_catch(error, "aborting");
+ fz_error_handle(error, "aborting");
exit(1);
}
@@ -79,60 +80,64 @@ static int isrange(char *s)
}
static void
-xps_run_page(xps_context *ctx, xps_page *page, fz_device *dev, fz_matrix ctm)
+xps_run_page(xps_document *doc, xps_page *page, fz_device *dev, fz_matrix ctm)
{
- ctx->dev = dev;
- xps_parse_fixed_page(ctx, ctm, page);
- ctx->dev = NULL;
+ doc->dev = dev;
+ xps_parse_fixed_page(doc, ctm, page);
+ doc->dev = NULL;
}
-static void drawpage(xps_context *ctx, int pagenum)
+static void drawpage(xps_document *doc, int pagenum)
{
xps_page *page;
fz_display_list *list;
fz_device *dev;
int start;
- int code;
if (showtime)
{
start = gettime();
}
- code = xps_load_page(&page, ctx, pagenum - 1);
- if (code)
- die(fz_rethrow(code, "cannot load page %d in file '%s'", pagenum, filename));
+ fz_try(doc->ctx)
+ {
+ page = xps_load_page(doc, pagenum - 1);
+ }
+ fz_catch(doc->ctx)
+ {
+ die(fz_error_note(1, "cannot load page %d in file '%s'", pagenum, filename));
+ }
list = NULL;
if (uselist)
{
- list = fz_new_display_list();
- dev = fz_new_list_device(list);
- xps_run_page(ctx, page, dev, fz_identity);
+ list = fz_new_display_list(doc->ctx);
+ dev = fz_new_list_device(doc->ctx, list);
+ xps_run_page(doc, page, dev, fz_identity);
fz_free_device(dev);
}
if (showxml)
{
- dev = fz_new_trace_device();
+ dev = fz_new_trace_device(doc->ctx);
printf("<page number=\"%d\">\n", pagenum);
if (list)
fz_execute_display_list(list, dev, fz_identity, fz_infinite_bbox);
else
- xps_run_page(ctx, page, dev, fz_identity);
+ xps_run_page(doc, page, dev, fz_identity);
printf("</page>\n");
fz_free_device(dev);
}
if (showtext)
{
- fz_text_span *text = fz_new_text_span();
- dev = fz_new_text_device(text);
+ fz_text_span *text = fz_new_text_span(doc->ctx);
+ dev = fz_new_text_device(doc->ctx, text);
if (list)
fz_execute_display_list(list, dev, fz_identity, fz_infinite_bbox);
else
- xps_run_page(ctx, page, dev, fz_identity);
+ xps_run_page(doc, page, dev, fz_identity);
fz_free_device(dev);
printf("[Page %d]\n", pagenum);
if (showtext > 1)
@@ -140,7 +145,7 @@ static void drawpage(xps_context *ctx, int pagenum)
else
fz_debug_text_span(text);
printf("\n");
- fz_free_text_span(text);
+ fz_free_text_span(doc->ctx, text);
}
if (showmd5 || showtime)
@@ -165,18 +170,18 @@ static void drawpage(xps_context *ctx, int pagenum)
/* TODO: banded rendering and multi-page ppm */
- pix = fz_new_pixmap_with_rect(colorspace, bbox);
+ pix = fz_new_pixmap_with_rect(doc->ctx, colorspace, bbox);
if (savealpha)
fz_clear_pixmap(pix);
else
fz_clear_pixmap_with_color(pix, 255);
- dev = fz_new_draw_device(glyphcache, pix);
+ dev = fz_new_draw_device(doc->ctx, glyphcache, pix);
if (list)
fz_execute_display_list(list, dev, ctm, bbox);
else
- xps_run_page(ctx, page, dev, ctm);
+ xps_run_page(doc, page, dev, ctm);
fz_free_device(dev);
if (output)
@@ -184,11 +189,11 @@ static void drawpage(xps_context *ctx, int pagenum)
char buf[512];
sprintf(buf, output, pagenum);
if (strstr(output, ".pgm") || strstr(output, ".ppm") || strstr(output, ".pnm"))
- fz_write_pnm(pix, buf);
+ fz_write_pnm(doc->ctx, pix, buf);
else if (strstr(output, ".pam"))
- fz_write_pam(pix, buf, savealpha);
+ fz_write_pam(doc->ctx, pix, buf, savealpha);
else if (strstr(output, ".png"))
- fz_write_png(pix, buf, savealpha);
+ fz_write_png(doc->ctx, pix, buf, savealpha);
}
if (showmd5)
@@ -206,11 +211,11 @@ static void drawpage(xps_context *ctx, int pagenum)
printf("%02x", digest[i]);
}
- fz_drop_pixmap(pix);
+ fz_drop_pixmap(doc->ctx, pix);
}
if (list)
- fz_free_display_list(list);
+ fz_free_display_list(doc->ctx, list);
if (showtime)
{
@@ -237,7 +242,7 @@ static void drawpage(xps_context *ctx, int pagenum)
printf("\n");
}
-static void drawrange(xps_context *ctx, char *range)
+static void drawrange(xps_document *doc, char *range)
{
int page, spage, epage;
char *spec, *dash;
@@ -248,7 +253,7 @@ static void drawrange(xps_context *ctx, char *range)
dash = strchr(spec, '-');
if (dash == spec)
- spage = epage = xps_count_pages(ctx);
+ spage = epage = xps_count_pages(doc);
else
spage = epage = atoi(spec);
@@ -257,26 +262,26 @@ static void drawrange(xps_context *ctx, char *range)
if (strlen(dash) > 1)
epage = atoi(dash + 1);
else
- epage = xps_count_pages(ctx);
+ epage = xps_count_pages(doc);
}
- spage = CLAMP(spage, 1, xps_count_pages(ctx));
- epage = CLAMP(epage, 1, xps_count_pages(ctx));
+ spage = CLAMP(spage, 1, xps_count_pages(doc));
+ epage = CLAMP(epage, 1, xps_count_pages(doc));
if (spage < epage)
for (page = spage; page <= epage; page++)
- drawpage(ctx, page);
+ drawpage(doc, page);
else
for (page = spage; page >= epage; page--)
- drawpage(ctx, page);
+ drawpage(doc, page);
spec = fz_strsep(&range, ",");
}
}
-static void drawoutline(xps_context *ctx)
+static void drawoutline(xps_document *doc)
{
- fz_outline *outline = xps_load_outline(ctx);
+ fz_outline *outline = xps_load_outline(doc);
if (showoutline > 1)
fz_debug_outline_xml(outline, 0);
else
@@ -288,8 +293,7 @@ int main(int argc, char **argv)
{
int grayscale = 0;
int accelerate = 1;
- xps_context *ctx;
- int code;
+ xps_document *doc;
int c;
while ((c = fz_getopt(argc, argv, "o:p:r:Aadglmtx5")) != -1)
@@ -323,7 +327,14 @@ int main(int argc, char **argv)
if (accelerate)
fz_accelerate();
- glyphcache = fz_new_glyph_cache();
+ ctx = fz_new_context(&fz_alloc_default);
+ if (ctx == NULL)
+ {
+ fprintf(stderr, "failed to initialise context");
+ exit(1);
+ }
+
+ glyphcache = fz_new_glyph_cache(ctx);
colorspace = fz_device_rgb;
if (grayscale)
@@ -347,28 +358,33 @@ int main(int argc, char **argv)
{
filename = argv[fz_optind++];
- code = xps_open_file(&ctx, filename);
- if (code)
- die(fz_rethrow(code, "cannot open document: %s", filename));
+ fz_try(ctx)
+ {
+ doc = xps_open_file(ctx, filename);
+ }
+ fz_catch(ctx)
+ {
+ die(fz_error_note(-1, "cannot open document: %s", filename));
+ }
if (showxml)
printf("<document name=\"%s\">\n", filename);
if (showoutline)
- drawoutline(ctx);
+ drawoutline(doc);
if (showtext || showxml || showtime || showmd5 || output)
{
if (fz_optind == argc || !isrange(argv[fz_optind]))
- drawrange(ctx, "1-");
+ drawrange(doc, "1-");
if (fz_optind < argc && isrange(argv[fz_optind]))
- drawrange(ctx, argv[fz_optind++]);
+ drawrange(doc, argv[fz_optind++]);
}
if (showxml)
printf("</document>\n");
- xps_free_context(ctx);
+ xps_free_context(doc);
}
if (showtime)
@@ -379,7 +395,8 @@ int main(int argc, char **argv)
printf("slowest page %d: %dms\n", timing.maxpage, timing.max);
}
- fz_free_glyph_cache(glyphcache);
+ fz_free_glyph_cache(ctx, glyphcache);
+ fz_free_context(ctx);
return 0;
}