summaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
authorRobin Watts <robin.watts@artifex.com>2011-09-14 17:36:57 +0100
committerRobin Watts <Robin.Watts@artifex.com>2011-09-15 14:50:17 +0100
commitb51ef0eea028c73b6379e832eaa34fff3fbbb927 (patch)
tree1ab685ccd356e7fdc832b2e3322c0486b2670cfb /apps
parent89ae81f651bfa112b8e07317eb6983beaf7cb212 (diff)
downloadmupdf-b51ef0eea028c73b6379e832eaa34fff3fbbb927.tar.xz
Add context to mupdf.
Huge pervasive change to lots of files, adding a context for exception handling and allocation. In time we'll move more statics into there. Also fix some for(i = 0; i < function(...); i++) calls.
Diffstat (limited to 'apps')
-rw-r--r--apps/pdfapp.c62
-rw-r--r--apps/pdfapp.h4
-rw-r--r--apps/pdfclean.c243
-rw-r--r--apps/pdfdraw.c49
-rw-r--r--apps/pdfextract.c53
-rw-r--r--apps/pdfinfo.c272
-rw-r--r--apps/pdfshow.c25
-rw-r--r--apps/win_main.c32
-rw-r--r--apps/x11_main.c12
-rw-r--r--apps/xpsdraw.c37
10 files changed, 439 insertions, 350 deletions
diff --git a/apps/pdfapp.c b/apps/pdfapp.c
index 2c9ac4e0..0e328a0a 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)
@@ -109,7 +110,7 @@ static void pdfapp_open_pdf(pdfapp_t *app, char *filename, int fd)
* Open PDF and load xref table
*/
- file = fz_open_fd(fd);
+ file = fz_open_fd(app->ctx, fd);
error = pdf_open_xref_with_stream(&app->xref, file, NULL);
if (error)
pdfapp_error(app, fz_error_note(error, "cannot open document '%s'", filename));
@@ -144,12 +145,12 @@ 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;
- info = fz_dict_gets(app->xref->trailer, "Info");
+ info = fz_dict_gets(app->ctx, app->xref->trailer, "Info");
if (info)
{
- obj = fz_dict_gets(info, "Title");
+ obj = fz_dict_gets(app->ctx, info, "Title");
if (obj)
- app->doctitle = pdf_to_utf8(obj);
+ app->doctitle = pdf_to_utf8(app->ctx, obj);
}
/*
@@ -168,7 +169,7 @@ static void pdfapp_open_xps(pdfapp_t *app, char *filename, int fd)
fz_error error;
fz_stream *file;
- file = fz_open_fd(fd);
+ file = fz_open_fd(app->ctx, fd);
error = xps_open_stream(&app->xps, file);
if (error)
pdfapp_error(app, fz_error_note(error, "cannot open document '%s'", filename));
@@ -186,7 +187,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;
@@ -211,21 +212,21 @@ void pdfapp_open(pdfapp_t *app, char *filename, int fd, int reload)
void pdfapp_close(pdfapp_t *app)
{
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)
- pdf_free_outline(app->outline);
+ pdf_free_outline(app->ctx, app->outline);
app->outline = NULL;
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);
@@ -294,8 +295,8 @@ 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);
+ app->page_list = fz_new_display_list(app->ctx);
+ mdev = fz_new_list_device(app->ctx, app->page_list);
error = pdf_run_page(app->xref, page, mdev, fz_identity);
if (error)
{
@@ -304,9 +305,9 @@ static void pdfapp_loadpage_pdf(pdfapp_t *app)
}
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)
@@ -327,8 +328,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;
@@ -351,11 +352,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);
@@ -367,8 +368,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);
}
@@ -384,7 +385,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
@@ -393,9 +394,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);
}
@@ -431,11 +432,12 @@ static void pdfapp_showpage(pdfapp_t *app, int loadpage, int drawpage, int repai
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(app->ctx, uri);
+ buf = fz_malloc(app->ctx, n + 1);
+ memcpy(buf, fz_to_str_buf(app->ctx, 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)
@@ -999,7 +1001,7 @@ void pdfapp_onmouse(pdfapp_t *app, int x, int y, int btn, int modifiers, int sta
if (link->kind == PDF_LINK_URI)
pdfapp_gotouri(app, link->dest);
else if (link->kind == PDF_LINK_GOTO)
- pdfapp_gotopage(app, fz_array_get(link->dest, 0)); /* [ pageobj ... ] */
+ pdfapp_gotopage(app, fz_array_get(app->ctx, link->dest, 0)); /* [ pageobj ... ] */
return;
}
}
diff --git a/apps/pdfapp.h b/apps/pdfapp.h
index 732d637c..1a161d93 100644
--- a/apps/pdfapp.h
+++ b/apps/pdfapp.h
@@ -87,9 +87,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 99a00845..1767ebc5 100644
--- a/apps/pdfclean.c
+++ b/apps/pdfclean.c
@@ -24,6 +24,7 @@ static int doexpand = 0;
static int doascii = 0;
static pdf_xref *xref = NULL;
+static fz_context *ctx = NULL;
void die(fz_error error)
{
@@ -60,13 +61,19 @@ static void sweepobj(fz_obj *obj)
if (fz_is_indirect(obj))
sweepref(obj);
- else if (fz_is_dict(obj))
- for (i = 0; i < fz_dict_len(obj); i++)
- sweepobj(fz_dict_get_val(obj, i));
+ else if (fz_is_dict(ctx, obj))
+ {
+ int n = fz_dict_len(ctx, obj);
+ for (i = 0; i < n; i++)
+ sweepobj(fz_dict_get_val(ctx, obj, i));
+ }
- else if (fz_is_array(obj))
- for (i = 0; i < fz_array_len(obj); i++)
- sweepobj(fz_array_get(obj, i));
+ else if (fz_is_array(ctx, obj))
+ {
+ int n = fz_array_len(ctx, obj);
+ for (i = 0; i < n; i++)
+ sweepobj(fz_array_get(ctx, obj, i));
+ }
}
static void sweepref(fz_obj *obj)
@@ -84,16 +91,16 @@ static void sweepref(fz_obj *obj)
/* Bake in /Length in stream objects */
if (pdf_is_stream(xref, num, gen))
{
- fz_obj *len = fz_dict_gets(obj, "Length");
+ fz_obj *len = fz_dict_gets(ctx, obj, "Length");
if (fz_is_indirect(len))
{
uselist[fz_to_num(len)] = 0;
- len = fz_resolve_indirect(len);
- fz_dict_puts(obj, "Length", len);
+ len = fz_resolve_indirect(xref->ctx, len);
+ fz_dict_puts(ctx, obj, "Length", len);
}
}
- sweepobj(fz_resolve_indirect(obj));
+ sweepobj(fz_resolve_indirect(xref->ctx, obj));
}
/*
@@ -126,8 +133,8 @@ static void removeduplicateobjs(void)
a = xref->table[num].obj;
b = xref->table[other].obj;
- a = fz_resolve_indirect(a);
- b = fz_resolve_indirect(b);
+ a = fz_resolve_indirect(xref->ctx, a);
+ b = fz_resolve_indirect(xref->ctx, b);
if (fz_objcmp(a, b))
continue;
@@ -176,18 +183,20 @@ static void compactxref(void)
static void renumberobj(fz_obj *obj)
{
int i;
+ fz_context *ctx = xref->ctx;
- if (fz_is_dict(obj))
+ if (fz_is_dict(ctx, obj))
{
- for (i = 0; i < fz_dict_len(obj); i++)
+ int n = fz_dict_len(ctx, 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);
+ fz_obj *key = fz_dict_get_key(ctx, obj, i);
+ fz_obj *val = fz_dict_get_val(ctx, obj, i);
if (fz_is_indirect(val))
{
- val = fz_new_indirect(renumbermap[fz_to_num(val)], 0, xref);
- fz_dict_put(obj, key, val);
- fz_drop_obj(val);
+ val = fz_new_indirect(ctx, renumbermap[fz_to_num(val)], 0, xref);
+ fz_dict_put(ctx, obj, key, val);
+ fz_drop_obj(ctx, val);
}
else
{
@@ -196,16 +205,17 @@ static void renumberobj(fz_obj *obj)
}
}
- else if (fz_is_array(obj))
+ else if (fz_is_array(ctx, obj))
{
- for (i = 0; i < fz_array_len(obj); i++)
+ int n = fz_array_len(ctx, obj);
+ for (i = 0; i < n; i++)
{
- fz_obj *val = fz_array_get(obj, i);
+ fz_obj *val = fz_array_get(ctx, obj, i);
if (fz_is_indirect(val))
{
- val = fz_new_indirect(renumbermap[fz_to_num(val)], 0, xref);
- fz_array_put(obj, i, val);
- fz_drop_obj(val);
+ val = fz_new_indirect(ctx, renumbermap[fz_to_num(val)], 0, xref);
+ fz_array_put(ctx, obj, i, val);
+ fz_drop_obj(ctx, val);
}
else
{
@@ -229,9 +239,9 @@ 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);
+ fz_drop_obj(ctx, obj);
}
else
{
@@ -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_calloc(xref->ctx, xref->len, sizeof(pdf_xref_entry));
xref->table[0] = oldxref[0];
/* Move used objects into the new compacted xref */
@@ -257,11 +267,11 @@ static void renumberobjs(void)
else
{
if (oldxref[num].obj)
- fz_drop_obj(oldxref[num].obj);
+ fz_drop_obj(ctx, oldxref[num].obj);
}
}
- fz_free(oldxref);
+ fz_free(xref->ctx, oldxref);
/* Update the used objects count in compacted xref */
xref->len = newlen + 1;
@@ -286,20 +296,20 @@ static void retainpages(int argc, char **argv)
die(fz_error_note(error, "cannot load page tree"));
/* Keep only pages/type entry to avoid references to unretained pages */
- oldroot = fz_dict_gets(xref->trailer, "Root");
- pages = fz_dict_gets(oldroot, "Pages");
+ oldroot = fz_dict_gets(ctx, xref->trailer, "Root");
+ pages = fz_dict_gets(ctx, oldroot, "Pages");
- root = fz_new_dict(2);
- fz_dict_puts(root, "Type", fz_dict_gets(oldroot, "Type"));
- fz_dict_puts(root, "Pages", fz_dict_gets(oldroot, "Pages"));
+ root = fz_new_dict(ctx, 2);
+ fz_dict_puts(ctx, root, "Type", fz_dict_gets(ctx, oldroot, "Type"));
+ fz_dict_puts(ctx, root, "Pages", fz_dict_gets(ctx, oldroot, "Pages"));
pdf_update_object(xref, fz_to_num(oldroot), fz_to_gen(oldroot), root);
- fz_drop_obj(root);
+ fz_drop_obj(ctx, 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)
@@ -339,10 +349,10 @@ static void retainpages(int argc, char **argv)
fz_obj *pageobj = xref->page_objs[page-1];
fz_obj *pageref = xref->page_refs[page-1];
- fz_dict_puts(pageobj, "Parent", parent);
+ fz_dict_puts(ctx, pageobj, "Parent", parent);
/* Store page object in new kids array */
- fz_array_push(kids, pageref);
+ fz_array_push(ctx, kids, pageref);
}
spec = fz_strsep(&pagelist, ",");
@@ -351,14 +361,14 @@ static void retainpages(int argc, char **argv)
fz_optind++;
}
- fz_drop_obj(parent);
+ fz_drop_obj(ctx, parent);
/* Update page count and kids array */
- countobj = fz_new_int(fz_array_len(kids));
- fz_dict_puts(pages, "Count", countobj);
- fz_drop_obj(countobj);
- fz_dict_puts(pages, "Kids", kids);
- fz_drop_obj(kids);
+ countobj = fz_new_int(ctx, fz_array_len(ctx, kids));
+ fz_dict_puts(ctx, pages, "Count", countobj);
+ fz_drop_obj(ctx, countobj);
+ fz_dict_puts(ctx, pages, "Kids", kids);
+ fz_drop_obj(ctx, kids);
}
/*
@@ -378,7 +388,7 @@ static void preloadobjstms(void)
error = pdf_load_object(&obj, xref, num, 0);
if (error)
die(error);
- fz_drop_obj(obj);
+ fz_drop_obj(ctx, obj);
}
}
}
@@ -409,7 +419,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--)
{
@@ -434,46 +444,46 @@ 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");
- dp = fz_dict_gets(dict, "DecodeParms");
+ f = fz_dict_gets(ctx, dict, "Filter");
+ dp = fz_dict_gets(ctx, dict, "DecodeParms");
- if (fz_is_name(f))
+ if (fz_is_name(ctx, f))
{
- newf = fz_new_array(2);
- fz_array_push(newf, ahx);
- fz_array_push(newf, f);
+ newf = fz_new_array(ctx, 2);
+ fz_array_push(ctx, newf, ahx);
+ fz_array_push(ctx, newf, f);
f = newf;
- if (fz_is_dict(dp))
+ if (fz_is_dict(ctx, dp))
{
- newdp = fz_new_array(2);
- fz_array_push(newdp, nullobj);
- fz_array_push(newdp, dp);
+ newdp = fz_new_array(ctx, 2);
+ fz_array_push(ctx, newdp, nullobj);
+ fz_array_push(ctx, newdp, dp);
dp = newdp;
}
}
- else if (fz_is_array(f))
+ else if (fz_is_array(ctx, f))
{
- fz_array_insert(f, ahx);
- if (fz_is_array(dp))
- fz_array_insert(dp, nullobj);
+ fz_array_insert(ctx, f, ahx);
+ if (fz_is_array(ctx, dp))
+ fz_array_insert(ctx, dp, nullobj);
}
else
f = ahx;
- fz_dict_puts(dict, "Filter", f);
+ fz_dict_puts(ctx, dict, "Filter", f);
if (dp)
- fz_dict_puts(dict, "DecodeParms", dp);
+ fz_dict_puts(ctx, dict, "DecodeParms", dp);
- fz_drop_obj(ahx);
- fz_drop_obj(nullobj);
+ fz_drop_obj(ctx, ahx);
+ fz_drop_obj(ctx, nullobj);
if (newf)
- fz_drop_obj(newf);
+ fz_drop_obj(ctx, newf);
if (newdp)
- fz_drop_obj(newdp);
+ fz_drop_obj(ctx, newdp);
}
static void copystream(fz_obj *obj, int num, int gen)
@@ -489,23 +499,23 @@ static void copystream(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);
- fz_dict_puts(obj, "Length", newlen);
- fz_drop_obj(newlen);
+ newlen = fz_new_int(ctx, buf->len);
+ fz_dict_puts(ctx, obj, "Length", newlen);
+ fz_drop_obj(ctx, newlen);
}
fprintf(out, "%d %d obj\n", num, gen);
- fz_fprint_obj(out, obj, !doexpand);
+ fz_fprint_obj(ctx, out, obj, !doexpand);
fprintf(out, "stream\n");
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)
@@ -518,29 +528,29 @@ static void expandstream(fz_obj *obj, int num, int gen)
if (error)
die(error);
- fz_dict_dels(obj, "Filter");
- fz_dict_dels(obj, "DecodeParms");
+ fz_dict_dels(ctx, obj, "Filter");
+ fz_dict_dels(ctx, obj, "DecodeParms");
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);
- fz_dict_puts(obj, "Length", newlen);
- fz_drop_obj(newlen);
+ newlen = fz_new_int(ctx, buf->len);
+ fz_dict_puts(ctx, obj, "Length", newlen);
+ fz_drop_obj(ctx, newlen);
fprintf(out, "%d %d obj\n", num, gen);
- fz_fprint_obj(out, obj, !doexpand);
+ fz_fprint_obj(ctx, out, obj, !doexpand);
fprintf(out, "stream\n");
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)
@@ -554,19 +564,19 @@ static void writeobject(int num, int gen)
die(error);
/* skip ObjStm and XRef objects */
- if (fz_is_dict(obj))
+ if (fz_is_dict(ctx, obj))
{
- type = fz_dict_gets(obj, "Type");
- if (fz_is_name(type) && !strcmp(fz_to_name(type), "ObjStm"))
+ type = fz_dict_gets(ctx, obj, "Type");
+ if (fz_is_name(ctx, type) && !strcmp(fz_to_name(ctx, type), "ObjStm"))
{
uselist[num] = 0;
- fz_drop_obj(obj);
+ fz_drop_obj(ctx, obj);
return;
}
- if (fz_is_name(type) && !strcmp(fz_to_name(type), "XRef"))
+ if (fz_is_name(ctx, type) && !strcmp(fz_to_name(ctx, type), "XRef"))
{
uselist[num] = 0;
- fz_drop_obj(obj);
+ fz_drop_obj(ctx, obj);
return;
}
}
@@ -574,18 +584,18 @@ static void writeobject(int num, int gen)
if (!pdf_is_stream(xref, num, gen))
{
fprintf(out, "%d %d obj\n", num, gen);
- fz_fprint_obj(out, obj, !doexpand);
+ fz_fprint_obj(ctx, out, obj, !doexpand);
fprintf(out, "endobj\n\n");
}
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);
}
- fz_drop_obj(obj);
+ fz_drop_obj(ctx, obj);
}
static void writexref(void)
@@ -607,29 +617,29 @@ static void writexref(void)
}
fprintf(out, "\n");
- trailer = fz_new_dict(5);
+ trailer = fz_new_dict(ctx, 5);
- obj = fz_new_int(xref->len);
- fz_dict_puts(trailer, "Size", obj);
- fz_drop_obj(obj);
+ obj = fz_new_int(ctx, xref->len);
+ fz_dict_puts(ctx, trailer, "Size", obj);
+ fz_drop_obj(ctx, obj);
- obj = fz_dict_gets(xref->trailer, "Info");
+ obj = fz_dict_gets(ctx, xref->trailer, "Info");
if (obj)
- fz_dict_puts(trailer, "Info", obj);
+ fz_dict_puts(ctx, trailer, "Info", obj);
- obj = fz_dict_gets(xref->trailer, "Root");
+ obj = fz_dict_gets(ctx, xref->trailer, "Root");
if (obj)
- fz_dict_puts(trailer, "Root", obj);
+ fz_dict_puts(ctx, trailer, "Root", obj);
- obj = fz_dict_gets(xref->trailer, "ID");
+ obj = fz_dict_gets(ctx, xref->trailer, "ID");
if (obj)
- fz_dict_puts(trailer, "ID", obj);
+ fz_dict_puts(ctx, trailer, "ID", obj);
fprintf(out, "trailer\n");
- fz_fprint_obj(out, trailer, !doexpand);
+ fz_fprint_obj(ctx, out, trailer, !doexpand);
fprintf(out, "\n");
- fz_drop_obj(trailer);
+ fz_drop_obj(ctx, trailer);
fprintf(out, "startxref\n%d\n%%%%EOF\n", startxref);
}
@@ -710,7 +720,11 @@ int main(int argc, char **argv)
if (argc - fz_optind > 0)
subset = 1;
- error = pdf_open_xref(&xref, infile, password);
+ ctx = fz_context_init(&fz_alloc_default);
+ if (ctx == NULL)
+ die(fz_error_note(1, "failed to initialise context"));
+
+ error = pdf_open_xref(ctx, &xref, infile, password);
if (error)
die(fz_error_note(error, "cannot open input file '%s'", infile));
@@ -721,10 +735,10 @@ int main(int argc, char **argv)
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_calloc(ctx, xref->len + 1, sizeof(char));
+ ofslist = fz_calloc(ctx, xref->len + 1, sizeof(int));
+ genlist = fz_calloc(ctx, xref->len + 1, sizeof(int));
+ renumbermap = fz_calloc(ctx, xref->len + 1, sizeof(int));
for (num = 0; num < xref->len; num++)
{
@@ -762,14 +776,15 @@ int main(int argc, char **argv)
if (fclose(out))
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_context_fin(ctx);
return 0;
}
diff --git a/apps/pdfdraw.c b/apps/pdfdraw.c
index 7a23acef..ddaf10c5 100644
--- a/apps/pdfdraw.c
+++ b/apps/pdfdraw.c
@@ -97,6 +97,7 @@ static void drawpage(pdf_xref *xref, int pagenum)
fz_display_list *list;
fz_device *dev;
int start;
+ fz_context *ctx = xref->ctx;
if (showtime)
{
@@ -111,8 +112,8 @@ static void drawpage(pdf_xref *xref, int pagenum)
if (uselist)
{
- list = fz_new_display_list();
- dev = fz_new_list_device(list);
+ list = fz_new_display_list(ctx);
+ dev = fz_new_list_device(ctx, list);
error = pdf_run_page(xref, page, dev, fz_identity);
if (error)
die(fz_error_note(error, "cannot draw page %d in file '%s'", pagenum, filename));
@@ -121,7 +122,7 @@ static void drawpage(pdf_xref *xref, int pagenum)
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);
@@ -133,8 +134,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
@@ -146,7 +147,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)
@@ -168,14 +169,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
@@ -196,13 +197,13 @@ static void drawpage(pdf_xref *xref, int pagenum)
else if (strstr(output, ".pam"))
fz_write_pam(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_halftone *ht = fz_get_default_halftone(ctx, 1);
+ fz_bitmap *bit = fz_halftone_pixmap(ctx, pix, ht);
fz_write_pbm(bit, buf);
- fz_drop_bitmap(bit);
- fz_drop_halftone(ht);
+ fz_drop_bitmap(ctx, bit);
+ fz_drop_halftone(ctx, ht);
}
}
@@ -221,13 +222,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)
{
@@ -253,7 +254,7 @@ 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();
}
@@ -303,6 +304,7 @@ int main(int argc, char **argv)
pdf_xref *xref;
fz_error error;
int c;
+ fz_context *ctx;
while ((c = fz_getopt(argc, argv, "o:p:r:R:Aab:dgmtx5G:I")) != -1)
{
@@ -341,7 +343,14 @@ int main(int argc, char **argv)
if (accelerate)
fz_accelerate();
- glyphcache = fz_new_glyph_cache();
+ ctx = fz_context_init(&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)
@@ -367,7 +376,7 @@ int main(int argc, char **argv)
{
filename = argv[fz_optind++];
- error = pdf_open_xref(&xref, filename, password);
+ error = pdf_open_xref(ctx, &xref, filename, password);
if (error)
die(fz_error_note(error, "cannot open document: %s", filename));
@@ -397,7 +406,7 @@ 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_flush_warnings();
diff --git a/apps/pdfextract.c b/apps/pdfextract.c
index c84f6ad9..2f5baec5 100644
--- a/apps/pdfextract.c
+++ b/apps/pdfextract.c
@@ -6,6 +6,7 @@
#include "mupdf.h"
static pdf_xref *xref = NULL;
+static fz_context *ctx = NULL;
static int dorgb = 0;
void die(fz_error error)
@@ -26,14 +27,14 @@ static void usage(void)
static int isimage(fz_obj *obj)
{
- fz_obj *type = fz_dict_gets(obj, "Subtype");
- return fz_is_name(type) && !strcmp(fz_to_name(type), "Image");
+ fz_obj *type = fz_dict_gets(ctx, obj, "Subtype");
+ return fz_is_name(ctx, type) && !strcmp(fz_to_name(ctx, type), "Image");
}
static int isfontdesc(fz_obj *obj)
{
- fz_obj *type = fz_dict_gets(obj, "Type");
- return fz_is_name(type) && !strcmp(fz_to_name(type), "FontDescriptor");
+ fz_obj *type = fz_dict_gets(ctx, obj, "Type");
+ return fz_is_name(ctx, type) && !strcmp(fz_to_name(ctx, type), "FontDescriptor");
}
static void saveimage(int num)
@@ -43,7 +44,7 @@ static void saveimage(int num)
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 */
@@ -54,9 +55,9 @@ static void saveimage(int num)
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,7 +65,7 @@ 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
{
@@ -73,8 +74,8 @@ static void saveimage(int num)
fz_write_pam(img, name, 0);
}
- fz_drop_pixmap(img);
- fz_drop_obj(ref);
+ fz_drop_pixmap(ctx, img);
+ fz_drop_obj(ctx, ref);
}
static void savefont(fz_obj *dict, int num)
@@ -90,34 +91,34 @@ static void savefont(fz_obj *dict, int num)
char *fontname = "font";
int n;
- obj = fz_dict_gets(dict, "FontName");
+ obj = fz_dict_gets(ctx, dict, "FontName");
if (obj)
- fontname = fz_to_name(obj);
+ fontname = fz_to_name(ctx, obj);
- obj = fz_dict_gets(dict, "FontFile");
+ obj = fz_dict_gets(ctx, dict, "FontFile");
if (obj)
{
stream = obj;
ext = "pfa";
}
- obj = fz_dict_gets(dict, "FontFile2");
+ obj = fz_dict_gets(ctx, dict, "FontFile2");
if (obj)
{
stream = obj;
ext = "ttf";
}
- obj = fz_dict_gets(dict, "FontFile3");
+ obj = fz_dict_gets(ctx, dict, "FontFile3");
if (obj)
{
stream = obj;
- obj = fz_dict_gets(obj, "Subtype");
- if (obj && !fz_is_name(obj))
+ obj = fz_dict_gets(ctx, obj, "Subtype");
+ if (obj && !fz_is_name(ctx, obj))
die(fz_error_make("Invalid font descriptor subtype"));
- subtype = fz_to_name(obj);
+ subtype = fz_to_name(ctx, obj);
if (!strcmp(subtype, "Type1C"))
ext = "cff";
else if (!strcmp(subtype, "CIDFontType0C"))
@@ -132,7 +133,7 @@ static void savefont(fz_obj *dict, int num)
return;
}
- buf = fz_new_buffer(0);
+ buf = fz_new_buffer(ctx, 0);
error = pdf_load_stream(&buf, xref, fz_to_num(stream), fz_to_gen(stream));
if (error)
@@ -152,7 +153,7 @@ static void savefont(fz_obj *dict, int num)
if (fclose(f) < 0)
die(fz_error_make("Error closing font file"));
- fz_drop_buffer(buf);
+ fz_drop_buffer(ctx, buf);
}
static void showobject(int num)
@@ -172,7 +173,7 @@ static void showobject(int num)
else if (isfontdesc(obj))
savefont(obj, num);
- fz_drop_obj(obj);
+ fz_drop_obj(ctx, obj);
}
int main(int argc, char **argv)
@@ -196,7 +197,12 @@ int main(int argc, char **argv)
usage();
infile = argv[fz_optind++];
- error = pdf_open_xref(&xref, infile, password);
+
+ ctx = fz_context_init(&fz_alloc_default);
+ if (ctx == NULL)
+ die(fz_error_note(1, "failed to initialise context"));
+
+ error = pdf_open_xref(ctx, &xref, infile, password);
if (error)
die(fz_error_note(error, "cannot open input file '%s'", infile));
@@ -217,6 +223,7 @@ int main(int argc, char **argv)
pdf_free_xref(xref);
fz_flush_warnings();
+ fz_context_fin(ctx);
return 0;
}
diff --git a/apps/pdfinfo.c b/apps/pdfinfo.c
index 34370869..f7ee1800 100644
--- a/apps/pdfinfo.c
+++ b/apps/pdfinfo.c
@@ -7,6 +7,7 @@
#include "mupdf.h"
pdf_xref *xref;
+fz_context *ctx;
int pagecount;
void closexref(void);
@@ -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;
}
}
@@ -184,18 +185,18 @@ showglobalinfo(void)
printf("\nPDF-%d.%d\n", xref->version / 10, xref->version % 10);
- obj = fz_dict_gets(xref->trailer, "Info");
+ obj = fz_dict_gets(ctx, xref->trailer, "Info");
if (obj)
{
printf("Info object (%d %d R):\n", fz_to_num(obj), fz_to_gen(obj));
- fz_debug_obj(fz_resolve_indirect(obj));
+ fz_debug_obj(ctx, fz_resolve_indirect(xref->ctx, obj));
}
- obj = fz_dict_gets(xref->trailer, "Encrypt");
+ obj = fz_dict_gets(ctx, xref->trailer, "Encrypt");
if (obj)
{
printf("\nEncryption object (%d %d R):\n", fz_to_num(obj), fz_to_gen(obj));
- fz_debug_obj(fz_resolve_indirect(obj));
+ fz_debug_obj(ctx, fz_resolve_indirect(xref->ctx, obj));
}
printf("\nPages: %d\n\n", pagecount);
@@ -208,11 +209,11 @@ gatherdimensions(int page, fz_obj *pageref, fz_obj *pageobj)
fz_obj *obj;
int j;
- obj = fz_dict_gets(pageobj, "MediaBox");
- if (!fz_is_array(obj))
+ obj = fz_dict_gets(ctx, pageobj, "MediaBox");
+ if (!fz_is_array(ctx, 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_realloc(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(ctx, dict);
+ for (i = 0; i < n; i++)
{
fz_obj *fontdict = NULL;
fz_obj *subtype = NULL;
@@ -246,17 +248,17 @@ gatherfonts(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
fz_obj *name = NULL;
int k;
- fontdict = fz_dict_get_val(dict, i);
- if (!fz_is_dict(fontdict))
+ fontdict = fz_dict_get_val(ctx, dict, i);
+ if (!fz_is_dict(ctx, fontdict))
{
fz_warn("not a font dict (%d %d R)", fz_to_num(fontdict), fz_to_gen(fontdict));
continue;
}
- subtype = fz_dict_gets(fontdict, "Subtype");
- basefont = fz_dict_gets(fontdict, "BaseFont");
- if (!basefont || fz_is_null(basefont))
- name = fz_dict_gets(fontdict, "Name");
+ subtype = fz_dict_gets(ctx, fontdict, "Subtype");
+ basefont = fz_dict_gets(ctx, fontdict, "BaseFont");
+ if (!basefont || fz_is_null(ctx, basefont))
+ name = fz_dict_gets(ctx, fontdict, "Name");
for (k = 0; k < fonts; k++)
if (!fz_objcmp(font[k].u.font.obj, fontdict))
@@ -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_realloc(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(ctx, dict);
+ for (i = 0; i < n; i++)
{
fz_obj *imagedict;
fz_obj *type;
@@ -294,37 +297,37 @@ gatherimages(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
fz_obj *altcs;
int k;
- imagedict = fz_dict_get_val(dict, i);
- if (!fz_is_dict(imagedict))
+ imagedict = fz_dict_get_val(ctx, dict, i);
+ if (!fz_is_dict(ctx, imagedict))
{
fz_warn("not an image dict (%d %d R)", fz_to_num(imagedict), fz_to_gen(imagedict));
continue;
}
- type = fz_dict_gets(imagedict, "Subtype");
- if (strcmp(fz_to_name(type), "Image"))
+ type = fz_dict_gets(ctx, imagedict, "Subtype");
+ if (strcmp(fz_to_name(ctx, type), "Image"))
continue;
- filter = fz_dict_gets(imagedict, "Filter");
+ filter = fz_dict_gets(ctx, imagedict, "Filter");
altcs = NULL;
- cs = fz_dict_gets(imagedict, "ColorSpace");
- if (fz_is_array(cs))
+ cs = fz_dict_gets(ctx, imagedict, "ColorSpace");
+ if (fz_is_array(ctx, cs))
{
fz_obj *cses = cs;
- cs = fz_array_get(cses, 0);
- if (fz_is_name(cs) && (!strcmp(fz_to_name(cs), "DeviceN") || !strcmp(fz_to_name(cs), "Separation")))
+ cs = fz_array_get(ctx, cses, 0);
+ if (fz_is_name(ctx, cs) && (!strcmp(fz_to_name(ctx, cs), "DeviceN") || !strcmp(fz_to_name(ctx, cs), "Separation")))
{
- altcs = fz_array_get(cses, 2);
- if (fz_is_array(altcs))
- altcs = fz_array_get(altcs, 0);
+ altcs = fz_array_get(ctx, cses, 2);
+ if (fz_is_array(ctx, altcs))
+ altcs = fz_array_get(ctx, altcs, 0);
}
}
- width = fz_dict_gets(imagedict, "Width");
- height = fz_dict_gets(imagedict, "Height");
- bpc = fz_dict_gets(imagedict, "BitsPerComponent");
+ width = fz_dict_gets(ctx, imagedict, "Width");
+ height = fz_dict_gets(ctx, imagedict, "Height");
+ bpc = fz_dict_gets(ctx, imagedict, "BitsPerComponent");
for (k = 0; k < images; k++)
if (!fz_objcmp(image[k].u.image.obj, imagedict))
@@ -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_realloc(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(ctx, dict);
+ for (i = 0; i < n; i++)
{
fz_obj *xobjdict;
fz_obj *type;
@@ -364,24 +368,24 @@ gatherforms(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
fz_obj *reference;
int k;
- xobjdict = fz_dict_get_val(dict, i);
- if (!fz_is_dict(xobjdict))
+ xobjdict = fz_dict_get_val(ctx, dict, i);
+ if (!fz_is_dict(ctx, xobjdict))
{
fz_warn("not a xobject dict (%d %d R)", fz_to_num(xobjdict), fz_to_gen(xobjdict));
continue;
}
- type = fz_dict_gets(xobjdict, "Subtype");
- if (strcmp(fz_to_name(type), "Form"))
+ type = fz_dict_gets(ctx, xobjdict, "Subtype");
+ if (strcmp(fz_to_name(ctx, type), "Form"))
continue;
- subtype = fz_dict_gets(xobjdict, "Subtype2");
- if (!strcmp(fz_to_name(subtype), "PS"))
+ subtype = fz_dict_gets(ctx, xobjdict, "Subtype2");
+ if (!strcmp(fz_to_name(ctx, subtype), "PS"))
continue;
- group = fz_dict_gets(xobjdict, "Group");
- groupsubtype = fz_dict_gets(group, "S");
- reference = fz_dict_gets(xobjdict, "Ref");
+ group = fz_dict_gets(ctx, xobjdict, "Group");
+ groupsubtype = fz_dict_gets(ctx, group, "S");
+ reference = fz_dict_gets(ctx, xobjdict, "Ref");
for (k = 0; k < forms; k++)
if (!fz_objcmp(form[k].u.form.obj, xobjdict))
@@ -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_realloc(ctx, form, forms * sizeof(struct info));
form[forms - 1].page = page;
form[forms - 1].pageref = pageref;
form[forms - 1].pageobj = pageobj;
@@ -405,26 +409,27 @@ 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(ctx, dict);
+ for (i = 0; i < n; i++)
{
fz_obj *xobjdict;
fz_obj *type;
fz_obj *subtype;
int k;
- xobjdict = fz_dict_get_val(dict, i);
- if (!fz_is_dict(xobjdict))
+ xobjdict = fz_dict_get_val(ctx, dict, i);
+ if (!fz_is_dict(ctx, xobjdict))
{
fz_warn("not a xobject dict (%d %d R)", fz_to_num(xobjdict), fz_to_gen(xobjdict));
continue;
}
- type = fz_dict_gets(xobjdict, "Subtype");
- subtype = fz_dict_gets(xobjdict, "Subtype2");
- if (strcmp(fz_to_name(type), "PS") &&
- (strcmp(fz_to_name(type), "Form") || strcmp(fz_to_name(subtype), "PS")))
+ type = fz_dict_gets(ctx, xobjdict, "Subtype");
+ subtype = fz_dict_gets(ctx, xobjdict, "Subtype2");
+ if (strcmp(fz_to_name(ctx, type), "PS") &&
+ (strcmp(fz_to_name(ctx, type), "Form") || strcmp(fz_to_name(ctx, subtype), "PS")))
continue;
for (k = 0; k < psobjs; k++)
@@ -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_realloc(ctx, psobj, psobjs * sizeof(struct info));
psobj[psobjs - 1].page = page;
psobj[psobjs - 1].pageref = pageref;
psobj[psobjs - 1].pageobj = pageobj;
@@ -447,23 +452,24 @@ 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(ctx, dict);
+ for (i = 0; i < n; i++)
{
fz_obj *shade;
fz_obj *type;
int k;
- shade = fz_dict_get_val(dict, i);
- if (!fz_is_dict(shade))
+ shade = fz_dict_get_val(ctx, dict, i);
+ if (!fz_is_dict(ctx, shade))
{
fz_warn("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)
+ type = fz_dict_gets(ctx, shade, "ShadingType");
+ if (!fz_is_int(ctx, type) || fz_to_int(ctx, type) < 1 || fz_to_int(ctx, type) > 7)
{
fz_warn("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_realloc(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(ctx, dict);
+ for (i = 0; i < n; i++)
{
fz_obj *patterndict;
fz_obj *type;
@@ -501,31 +508,31 @@ gatherpatterns(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
fz_obj *shading = NULL;
int k;
- patterndict = fz_dict_get_val(dict, i);
- if (!fz_is_dict(patterndict))
+ patterndict = fz_dict_get_val(ctx, dict, i);
+ if (!fz_is_dict(ctx, patterndict))
{
fz_warn("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)
+ type = fz_dict_gets(ctx, patterndict, "PatternType");
+ if (!fz_is_int(ctx, type) || fz_to_int(ctx, type) < 1 || fz_to_int(ctx, type) > 2)
{
fz_warn("not a pattern type (%d %d R)", fz_to_num(patterndict), fz_to_gen(patterndict));
type = NULL;
}
- if (fz_to_int(type) == 1)
+ if (fz_to_int(ctx, type) == 1)
{
- paint = fz_dict_gets(patterndict, "PaintType");
- if (!fz_is_int(paint) || fz_to_int(paint) < 1 || fz_to_int(paint) > 2)
+ paint = fz_dict_gets(ctx, patterndict, "PaintType");
+ if (!fz_is_int(ctx, paint) || fz_to_int(ctx, paint) < 1 || fz_to_int(ctx, paint) > 2)
{
fz_warn("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)
+ tiling = fz_dict_gets(ctx, patterndict, "TilingType");
+ if (!fz_is_int(ctx, tiling) || fz_to_int(ctx, tiling) < 1 || fz_to_int(ctx, tiling) > 3)
{
fz_warn("not a pattern tiling type (%d %d R)", fz_to_num(patterndict), fz_to_gen(patterndict));
tiling = NULL;
@@ -533,7 +540,7 @@ gatherpatterns(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict)
}
else
{
- shading = fz_dict_gets(patterndict, "Shading");
+ shading = fz_dict_gets(ctx, patterndict, "Shading");
}
for (k = 0; k < patterns; k++)
@@ -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_realloc(ctx, pattern, patterns * sizeof(struct info));
pattern[patterns - 1].page = page;
pattern[patterns - 1].pageref = pageref;
pattern[patterns - 1].pageobj = pageobj;
@@ -575,50 +582,55 @@ gatherresourceinfo(int page, fz_obj *rsrc)
if (!pageobj)
die(fz_error_make("cannot retrieve info from page %d", page));
- font = fz_dict_gets(rsrc, "Font");
+ font = fz_dict_gets(ctx, 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(ctx, font);
+ for (i = 0; i < n; i++)
{
- fz_obj *obj = fz_dict_get_val(font, i);
+ fz_obj *obj = fz_dict_get_val(ctx, font, i);
- subrsrc = fz_dict_gets(obj, "Resources");
+ subrsrc = fz_dict_gets(ctx, obj, "Resources");
if (subrsrc && fz_objcmp(rsrc, subrsrc))
gatherresourceinfo(page, subrsrc);
}
}
- xobj = fz_dict_gets(rsrc, "XObject");
+ xobj = fz_dict_gets(ctx, 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(ctx, xobj);
+ for (i = 0; i < n; i++)
{
- fz_obj *obj = fz_dict_get_val(xobj, i);
- subrsrc = fz_dict_gets(obj, "Resources");
+ fz_obj *obj = fz_dict_get_val(ctx, xobj, i);
+ subrsrc = fz_dict_gets(ctx, obj, "Resources");
if (subrsrc && fz_objcmp(rsrc, subrsrc))
gatherresourceinfo(page, subrsrc);
}
}
- shade = fz_dict_gets(rsrc, "Shading");
+ shade = fz_dict_gets(ctx, rsrc, "Shading");
if (shade)
gathershadings(page, pageref, pageobj, shade);
- pattern = fz_dict_gets(rsrc, "Pattern");
+ pattern = fz_dict_gets(ctx, rsrc, "Pattern");
if (pattern)
{
+ int n;
gatherpatterns(page, pageref, pageobj, pattern);
-
- for (i = 0; i < fz_dict_len(pattern); i++)
+ n = fz_dict_len(ctx, pattern);
+ for (i = 0; i < n; i++)
{
- fz_obj *obj = fz_dict_get_val(pattern, i);
- subrsrc = fz_dict_gets(obj, "Resources");
+ fz_obj *obj = fz_dict_get_val(ctx, pattern, i);
+ subrsrc = fz_dict_gets(ctx, obj, "Resources");
if (subrsrc && fz_objcmp(rsrc, subrsrc))
gatherresourceinfo(page, subrsrc);
}
@@ -640,7 +652,7 @@ gatherpageinfo(int page)
gatherdimensions(page, pageref, pageobj);
- rsrc = fz_dict_gets(pageobj, "Resources");
+ rsrc = fz_dict_gets(ctx, pageobj, "Resources");
gatherresourceinfo(page, rsrc);
}
@@ -676,8 +688,8 @@ printinfo(char *filename, int show, int page)
printf(PAGE_FMT "%s '%s' (%d %d R)\n",
font[i].page,
fz_to_num(font[i].pageref), fz_to_gen(font[i].pageref),
- fz_to_name(font[i].u.font.subtype),
- fz_to_name(font[i].u.font.name),
+ fz_to_name(ctx, font[i].u.font.subtype),
+ fz_to_name(ctx, font[i].u.font.name),
fz_to_num(font[i].u.font.obj), fz_to_gen(font[i].u.font.obj));
}
printf("\n");
@@ -695,37 +707,40 @@ printinfo(char *filename, int show, int page)
image[i].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++)
+ if (fz_is_array(ctx, image[i].u.image.filter))
+ {
+ int n = fz_array_len(ctx, 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));
+ fz_obj *obj = fz_array_get(ctx, image[i].u.image.filter, j);
+ char *filter = fz_strdup(ctx, fz_to_name(ctx, obj));
if (strstr(filter, "Decode"))
*(strstr(filter, "Decode")) = '\0';
printf("%s%s",
filter,
- j == fz_array_len(image[i].u.image.filter) - 1 ? "" : " ");
- fz_free(filter);
+ j == fz_array_len(ctx, image[i].u.image.filter) - 1 ? "" : " ");
+ 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(ctx, 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(ctx, 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(ctx, image[i].u.image.altcs));
if (!strncmp(altcs, "Device", 6))
{
@@ -763,16 +778,16 @@ printinfo(char *filename, int show, int page)
}
printf(" ] %dx%d %dbpc %s%s%s (%d %d R)\n",
- fz_to_int(image[i].u.image.width),
- fz_to_int(image[i].u.image.height),
- image[i].u.image.bpc ? fz_to_int(image[i].u.image.bpc) : 1,
+ fz_to_int(ctx, image[i].u.image.width),
+ fz_to_int(ctx, image[i].u.image.height),
+ image[i].u.image.bpc ? fz_to_int(ctx, image[i].u.image.bpc) : 1,
image[i].u.image.cs ? cs : "ImageMask",
image[i].u.image.altcs ? " " : "",
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");
}
@@ -797,7 +812,7 @@ printinfo(char *filename, int show, int page)
printf(PAGE_FMT "%s (%d %d R)\n",
shading[i].page,
fz_to_num(shading[i].pageref), fz_to_gen(shading[i].pageref),
- shadingtype[fz_to_int(shading[i].u.shading.type)],
+ shadingtype[fz_to_int(ctx, shading[i].u.shading.type)],
fz_to_num(shading[i].u.shading.obj), fz_to_gen(shading[i].u.shading.obj));
}
printf("\n");
@@ -808,7 +823,7 @@ printinfo(char *filename, int show, int page)
printf("Patterns (%d):\n", patterns);
for (i = 0; i < patterns; i++)
{
- if (fz_to_int(pattern[i].u.pattern.type) == 1)
+ if (fz_to_int(ctx, pattern[i].u.pattern.type) == 1)
{
char *painttype[] =
{
@@ -827,8 +842,8 @@ printinfo(char *filename, int show, int page)
printf(PAGE_FMT "Tiling %s %s (%d %d R)\n",
pattern[i].page,
fz_to_num(pattern[i].pageref), fz_to_gen(pattern[i].pageref),
- painttype[fz_to_int(pattern[i].u.pattern.paint)],
- tilingtype[fz_to_int(pattern[i].u.pattern.tiling)],
+ painttype[fz_to_int(ctx, pattern[i].u.pattern.paint)],
+ tilingtype[fz_to_int(ctx, pattern[i].u.pattern.tiling)],
fz_to_num(pattern[i].u.pattern.obj), fz_to_gen(pattern[i].u.pattern.obj));
}
else
@@ -852,7 +867,7 @@ printinfo(char *filename, int show, int page)
form[i].page,
fz_to_num(form[i].pageref), fz_to_gen(form[i].pageref),
form[i].u.form.groupsubtype ? " " : "",
- form[i].u.form.groupsubtype ? fz_to_name(form[i].u.form.groupsubtype) : "",
+ form[i].u.form.groupsubtype ? fz_to_name(ctx, form[i].u.form.groupsubtype) : "",
form[i].u.form.groupsubtype ? " Group" : "",
form[i].u.form.reference ? " Reference" : "",
fz_to_num(form[i].u.form.obj), fz_to_gen(form[i].u.form.obj));
@@ -966,6 +981,10 @@ int main(int argc, char **argv)
if (fz_optind == argc)
infousage();
+ ctx = fz_context_init(&fz_alloc_default);
+ if (ctx == NULL)
+ die(fz_error_make("failed to initialise context"));
+
state = NO_FILE_OPENED;
while (fz_optind < argc)
{
@@ -981,7 +1000,7 @@ 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_error_note(error, "cannot open input file '%s'", filename));
@@ -1006,6 +1025,7 @@ int main(int argc, char **argv)
showinfo(filename, show, "1-");
closexref();
+ fz_context_fin(ctx);
return 0;
}
diff --git a/apps/pdfshow.c b/apps/pdfshow.c
index daf31298..bbb6747e 100644
--- a/apps/pdfshow.c
+++ b/apps/pdfshow.c
@@ -6,6 +6,7 @@
#include "mupdf.h"
static pdf_xref *xref = NULL;
+static fz_context *ctx = NULL;
static int showbinary = 0;
static int showdecode = 1;
static int showcolumn;
@@ -32,7 +33,7 @@ static void showtrailer(void)
if (!xref)
die(fz_error_make("no file specified"));
printf("trailer\n");
- fz_debug_obj(xref->trailer);
+ fz_debug_obj(ctx, xref->trailer);
printf("\n");
}
@@ -146,7 +147,7 @@ static void showobject(int num, int gen)
else
{
printf("%d %d obj\n", num, gen);
- fz_debug_obj(obj);
+ fz_debug_obj(ctx, obj);
printf("stream\n");
showstream(num, gen);
printf("endstream\n");
@@ -156,11 +157,11 @@ static void showobject(int num, int gen)
else
{
printf("%d %d obj\n", num, gen);
- fz_debug_obj(obj);
+ fz_debug_obj(ctx, obj);
printf("endobj\n\n");
}
- fz_drop_obj(obj);
+ fz_drop_obj(ctx, obj);
}
static void showgrep(char *filename)
@@ -177,17 +178,17 @@ static void showgrep(char *filename)
if (error)
die(error);
- fz_sort_dict(obj);
+ fz_sort_dict(ctx, obj);
printf("%s:%d: ", filename, i);
- fz_fprint_obj(stdout, obj, 1);
+ fz_fprint_obj(ctx, stdout, obj, 1);
- fz_drop_obj(obj);
+ fz_drop_obj(ctx, obj);
}
}
printf("%s:trailer: ", filename);
- fz_fprint_obj(stdout, xref->trailer, 1);
+ fz_fprint_obj(ctx, stdout, xref->trailer, 1);
}
int main(int argc, char **argv)
@@ -212,7 +213,12 @@ int main(int argc, char **argv)
usage();
filename = argv[fz_optind++];
- error = pdf_open_xref(&xref, filename, password);
+
+ ctx = fz_context_init(&fz_alloc_default);
+ if (ctx == NULL)
+ die(fz_error_note(1, "failed to initialise context"));
+
+ error = pdf_open_xref(ctx, &xref, filename, password);
if (error)
die(fz_error_note(error, "cannot open document: %s", filename));
@@ -235,6 +241,7 @@ int main(int argc, char **argv)
pdf_free_xref(xref);
fz_flush_warnings();
+ fz_context_fin(ctx);
return 0;
}
diff --git a/apps/win_main.c b/apps/win_main.c
index 92f7f085..24279e1d 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.
@@ -240,33 +241,33 @@ dloginfoproc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
SetDlgItemTextA(hwnd, 0x13, "n/a");
}
- info = fz_dict_gets(xref->trailer, "Info");
+ info = fz_dict_gets(xref->ctx, xref->trailer, "Info");
if (!info)
return TRUE;
#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")))
+ if ((obj = fz_dict_gets(xref->ctx, info, "Title")))
SETUCS(0x20);
- if ((obj = fz_dict_gets(info, "Author")))
+ if ((obj = fz_dict_gets(xref->ctx, info, "Author")))
SETUCS(0x21);
- if ((obj = fz_dict_gets(info, "Subject")))
+ if ((obj = fz_dict_gets(xref->ctx, info, "Subject")))
SETUCS(0x22);
- if ((obj = fz_dict_gets(info, "Keywords")))
+ if ((obj = fz_dict_gets(xref->ctx, info, "Keywords")))
SETUCS(0x23);
- if ((obj = fz_dict_gets(info, "Creator")))
+ if ((obj = fz_dict_gets(xref->ctx, info, "Creator")))
SETUCS(0x24);
- if ((obj = fz_dict_gets(info, "Producer")))
+ if ((obj = fz_dict_gets(xref->ctx, info, "Producer")))
SETUCS(0x25);
- if ((obj = fz_dict_gets(info, "CreationDate")))
+ if ((obj = fz_dict_gets(xref->ctx, info, "CreationDate")))
SETUCS(0x26);
- if ((obj = fz_dict_gets(info, "ModDate")))
+ if ((obj = fz_dict_gets(xref->ctx, info, "ModDate")))
SETUCS(0x27);
return TRUE;
@@ -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_context_init(&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);
diff --git a/apps/x11_main.c b/apps/x11_main.c
index d719abda..f0f4f66d 100644
--- a/apps/x11_main.c
+++ b/apps/x11_main.c
@@ -554,6 +554,7 @@ int main(int argc, char **argv)
fd_set fds;
int width = -1;
int height = -1;
+ fz_context *ctx;
while ((c = fz_getopt(argc, argv, "p:r:b:A")) != -1)
{
@@ -583,9 +584,16 @@ int main(int argc, char **argv)
if (accelerate)
fz_accelerate();
+ ctx = fz_context_init(&fz_alloc_default);
+ if (ctx == NULL)
+ {
+ fprintf(stderr, "failed to initialise context");
+ exit(1);
+ }
+
winopen();
- pdfapp_init(&gapp);
+ pdfapp_init(ctx, &gapp);
gapp.scrw = DisplayWidth(xdpy, xscr);
gapp.scrh = DisplayHeight(xdpy, xscr);
gapp.resolution = resolution;
@@ -742,5 +750,7 @@ int main(int argc, char **argv)
XCloseDisplay(xdpy);
+ fz_context_fin(ctx);
+
return 0;
}
diff --git a/apps/xpsdraw.c b/apps/xpsdraw.c
index 6afe344d..8283216a 100644
--- a/apps/xpsdraw.c
+++ b/apps/xpsdraw.c
@@ -20,6 +20,7 @@ int uselist = 1;
fz_colorspace *colorspace;
fz_glyph_cache *glyphcache;
char *filename;
+fz_context *fzctx;
struct {
int count, total;
@@ -105,15 +106,15 @@ static void drawpage(xps_context *ctx, int pagenum)
if (uselist)
{
- list = fz_new_display_list();
- dev = fz_new_list_device(list);
+ list = fz_new_display_list(ctx->ctx);
+ dev = fz_new_list_device(ctx->ctx, list);
xps_run_page(ctx, page, dev, fz_identity);
fz_free_device(dev);
}
if (showxml)
{
- dev = fz_new_trace_device();
+ dev = fz_new_trace_device(ctx->ctx);
printf("<page number=\"%d\">\n", pagenum);
if (list)
fz_execute_display_list(list, dev, fz_identity, fz_infinite_bbox);
@@ -125,8 +126,8 @@ static void drawpage(xps_context *ctx, 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->ctx);
+ dev = fz_new_text_device(ctx->ctx, text);
if (list)
fz_execute_display_list(list, dev, fz_identity, fz_infinite_bbox);
else
@@ -138,7 +139,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(ctx->ctx, text);
}
if (showmd5 || showtime)
@@ -163,14 +164,14 @@ 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(ctx->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->ctx, glyphcache, pix);
if (list)
fz_execute_display_list(list, dev, ctm, bbox);
else
@@ -186,7 +187,7 @@ static void drawpage(xps_context *ctx, int pagenum)
else if (strstr(output, ".pam"))
fz_write_pam(pix, buf, savealpha);
else if (strstr(output, ".png"))
- fz_write_png(pix, buf, savealpha);
+ fz_write_png(ctx->ctx, pix, buf, savealpha);
}
if (showmd5)
@@ -204,11 +205,11 @@ static void drawpage(xps_context *ctx, int pagenum)
printf("%02x", digest[i]);
}
- fz_drop_pixmap(pix);
+ fz_drop_pixmap(ctx->ctx, pix);
}
if (list)
- fz_free_display_list(list);
+ fz_free_display_list(ctx->ctx, list);
if (showtime)
{
@@ -310,7 +311,14 @@ int main(int argc, char **argv)
if (accelerate)
fz_accelerate();
- glyphcache = fz_new_glyph_cache();
+ fzctx = fz_context_init(&fz_alloc_default);
+ if (fzctx == NULL)
+ {
+ fprintf(stderr, "failed to initialise context");
+ exit(1);
+ }
+
+ glyphcache = fz_new_glyph_cache(fzctx);
colorspace = fz_device_rgb;
if (grayscale)
@@ -334,7 +342,7 @@ int main(int argc, char **argv)
{
filename = argv[fz_optind++];
- code = xps_open_file(&ctx, filename);
+ code = xps_open_file(fzctx, &ctx, filename);
if (code)
die(fz_error_note(code, "cannot open document: %s", filename));
@@ -360,7 +368,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(fzctx, glyphcache);
+ fz_context_fin(fzctx);
return 0;
}