summaryrefslogtreecommitdiff
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
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.
-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
-rw-r--r--draw/draw_device.c230
-rw-r--r--draw/draw_edge.c28
-rw-r--r--draw/draw_glyph.c36
-rw-r--r--draw/draw_mesh.c10
-rw-r--r--draw/draw_scale.c28
-rw-r--r--fitz/base_hash.c25
-rw-r--r--fitz/base_memory.c143
-rw-r--r--fitz/base_object.c332
-rw-r--r--fitz/context.c63
-rw-r--r--fitz/crypt_arc4.c4
-rw-r--r--fitz/crypt_md5.c2
-rw-r--r--fitz/dev_bbox.c32
-rw-r--r--fitz/dev_list.c184
-rw-r--r--fitz/dev_null.c50
-rw-r--r--fitz/dev_text.c118
-rw-r--r--fitz/dev_trace.c44
-rw-r--r--fitz/except.c71
-rw-r--r--fitz/except.h7
-rw-r--r--fitz/exceptxxx.h20
-rw-r--r--fitz/filt_basic.c64
-rw-r--r--fitz/filt_dctd.c18
-rw-r--r--fitz/filt_faxd.c47
-rw-r--r--fitz/filt_flate.c12
-rw-r--r--fitz/filt_jbig2d.c6
-rw-r--r--fitz/filt_jpxd.c10
-rw-r--r--fitz/filt_lzwd.c11
-rw-r--r--fitz/filt_predict.c35
-rw-r--r--fitz/fitz.h358
-rw-r--r--fitz/memento.c7
-rw-r--r--fitz/obj_print.c97
-rw-r--r--fitz/res_bitmap.c12
-rw-r--r--fitz/res_colorspace.c30
-rw-r--r--fitz/res_font.c66
-rw-r--r--fitz/res_halftone.c22
-rw-r--r--fitz/res_path.c46
-rw-r--r--fitz/res_pixmap.c54
-rw-r--r--fitz/res_shade.c8
-rw-r--r--fitz/res_text.c26
-rw-r--r--fitz/stm_buffer.c20
-rw-r--r--fitz/stm_open.c33
-rw-r--r--fitz/stm_read.c9
-rw-r--r--pdf/mupdf.h84
-rw-r--r--pdf/pdf_annot.c113
-rw-r--r--pdf/pdf_cmap.c60
-rw-r--r--pdf/pdf_cmap_load.c41
-rw-r--r--pdf/pdf_cmap_parse.c18
-rw-r--r--pdf/pdf_colorspace.c134
-rw-r--r--pdf/pdf_crypt.c199
-rw-r--r--pdf/pdf_font.c319
-rw-r--r--pdf/pdf_function.c170
-rw-r--r--pdf/pdf_image.c98
-rw-r--r--pdf/pdf_interpret.c312
-rw-r--r--pdf/pdf_metrics.c8
-rw-r--r--pdf/pdf_nametree.c95
-rw-r--r--pdf/pdf_outline.c44
-rw-r--r--pdf/pdf_page.c198
-rw-r--r--pdf/pdf_parse.c244
-rw-r--r--pdf/pdf_pattern.c35
-rw-r--r--pdf/pdf_repair.c106
-rw-r--r--pdf/pdf_shade.c236
-rw-r--r--pdf/pdf_store.c70
-rw-r--r--pdf/pdf_stream.c90
-rw-r--r--pdf/pdf_type3.c73
-rw-r--r--pdf/pdf_unicode.c21
-rw-r--r--pdf/pdf_xobject.c43
-rw-r--r--pdf/pdf_xref.c160
-rw-r--r--scripts/cmapdump.c13
-rw-r--r--win32/libmupdf.vcproj88
-rw-r--r--win32/libthirdparty.vcproj63
-rw-r--r--win32/mupdf.sln19
-rw-r--r--win32/mupdf.vcproj81
-rw-r--r--win32/pdfclean.vcproj80
-rw-r--r--win32/pdfdraw.vcproj80
-rw-r--r--win32/pdfextract.vcproj80
-rw-r--r--win32/pdfshow.vcproj80
-rw-r--r--win32/xpsdraw.vcproj83
-rw-r--r--xps/muxps.h13
-rw-r--r--xps/xps_doc.c26
-rw-r--r--xps/xps_glyphs.c18
-rw-r--r--xps/xps_gradient.c12
-rw-r--r--xps/xps_image.c12
-rw-r--r--xps/xps_jpeg.c10
-rw-r--r--xps/xps_path.c114
-rw-r--r--xps/xps_png.c30
-rw-r--r--xps/xps_resource.c18
-rw-r--r--xps/xps_tiff.c83
-rw-r--r--xps/xps_tile.c14
-rw-r--r--xps/xps_xml.c36
-rw-r--r--xps/xps_zip.c59
99 files changed, 4203 insertions, 3087 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;
}
diff --git a/draw/draw_device.c b/draw/draw_device.c
index 76b55276..3e289f3e 100644
--- a/draw/draw_device.c
+++ b/draw/draw_device.c
@@ -38,6 +38,7 @@ struct fz_draw_device_s
{
fz_glyph_cache *cache;
fz_gel *gel;
+ fz_context *ctx;
fz_pixmap *dest;
fz_pixmap *shape;
@@ -88,9 +89,8 @@ static void dump_spaces(int x, const char *s)
#endif
-static void fz_knockout_begin(void *user)
+static void fz_knockout_begin(fz_draw_device *dev)
{
- fz_draw_device *dev = user;
fz_bbox bbox;
fz_pixmap *dest, *shape;
int isolated = dev->blendmode & FZ_BLEND_ISOLATED;
@@ -106,7 +106,7 @@ static void fz_knockout_begin(void *user)
bbox = fz_bound_pixmap(dev->dest);
bbox = fz_intersect_bbox(bbox, dev->scissor);
- dest = fz_new_pixmap_with_rect(dev->dest->colorspace, bbox);
+ dest = fz_new_pixmap_with_rect(dev->ctx, dev->dest->colorspace, bbox);
if (isolated)
{
@@ -130,7 +130,7 @@ static void fz_knockout_begin(void *user)
}
else
{
- shape = fz_new_pixmap_with_rect(NULL, bbox);
+ shape = fz_new_pixmap_with_rect(dev->ctx, NULL, bbox);
fz_clear_pixmap(shape);
}
dev->stack[dev->top].blendmode = dev->blendmode;
@@ -148,9 +148,8 @@ static void fz_knockout_begin(void *user)
dev->blendmode &= ~FZ_BLEND_MODEMASK;
}
-static void fz_knockout_end(void *user)
+static void fz_knockout_end(fz_draw_device *dev)
{
- fz_draw_device *dev = user;
fz_pixmap *group = dev->dest;
fz_pixmap *shape = dev->shape;
int blendmode;
@@ -194,14 +193,14 @@ static void fz_knockout_end(void *user)
else
fz_blend_pixmap(dev->dest, group, 255, blendmode, isolated, shape);
- fz_drop_pixmap(group);
+ fz_drop_pixmap(dev->ctx, group);
if (shape != dev->shape)
{
if (dev->shape)
{
fz_paint_pixmap(dev->shape, shape, 255);
}
- fz_drop_pixmap(shape);
+ fz_drop_pixmap(dev->ctx, shape);
}
#ifdef DUMP_GROUP_BLENDS
fz_dump_blend(dev->dest, " to get ");
@@ -213,10 +212,10 @@ static void fz_knockout_end(void *user)
}
static void
-fz_draw_fill_path(void *user, fz_path *path, int even_odd, fz_matrix ctm,
+fz_draw_fill_path(fz_device *devp, fz_path *path, int even_odd, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
- fz_draw_device *dev = user;
+ fz_draw_device *dev = devp->user;
fz_colorspace *model = dev->dest->colorspace;
float expansion = fz_matrix_expansion(ctm);
float flatness = 0.3f / expansion;
@@ -259,10 +258,10 @@ fz_draw_fill_path(void *user, fz_path *path, int even_odd, fz_matrix ctm,
}
static void
-fz_draw_stroke_path(void *user, fz_path *path, fz_stroke_state *stroke, fz_matrix ctm,
+fz_draw_stroke_path(fz_device *devp, fz_path *path, fz_stroke_state *stroke, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
- fz_draw_device *dev = user;
+ fz_draw_device *dev = devp->user;
fz_colorspace *model = dev->dest->colorspace;
float expansion = fz_matrix_expansion(ctm);
float flatness = 0.3f / expansion;
@@ -315,9 +314,9 @@ fz_draw_stroke_path(void *user, fz_path *path, fz_stroke_state *stroke, fz_matri
}
static void
-fz_draw_clip_path(void *user, fz_path *path, fz_rect *rect, int even_odd, fz_matrix ctm)
+fz_draw_clip_path(fz_device *devp, fz_path *path, fz_rect *rect, int even_odd, fz_matrix ctm)
{
- fz_draw_device *dev = user;
+ fz_draw_device *dev = devp->user;
fz_colorspace *model = dev->dest->colorspace;
float expansion = fz_matrix_expansion(ctm);
float flatness = 0.3f / expansion;
@@ -354,14 +353,14 @@ fz_draw_clip_path(void *user, fz_path *path, fz_rect *rect, int even_odd, fz_mat
return;
}
- mask = fz_new_pixmap_with_rect(NULL, bbox);
+ mask = fz_new_pixmap_with_rect(dev->ctx, NULL, bbox);
fz_clear_pixmap(mask);
- dest = fz_new_pixmap_with_rect(model, bbox);
+ dest = fz_new_pixmap_with_rect(dev->ctx, model, bbox);
/* FIXME: See note #1 */
fz_clear_pixmap(dest);
if (dev->shape)
{
- shape = fz_new_pixmap_with_rect(NULL, bbox);
+ shape = fz_new_pixmap_with_rect(dev->ctx, NULL, bbox);
fz_clear_pixmap(shape);
}
else
@@ -385,9 +384,9 @@ fz_draw_clip_path(void *user, fz_path *path, fz_rect *rect, int even_odd, fz_mat
}
static void
-fz_draw_clip_stroke_path(void *user, fz_path *path, fz_rect *rect, fz_stroke_state *stroke, fz_matrix ctm)
+fz_draw_clip_stroke_path(fz_device *devp, fz_path *path, fz_rect *rect, fz_stroke_state *stroke, fz_matrix ctm)
{
- fz_draw_device *dev = user;
+ fz_draw_device *dev = devp->user;
fz_colorspace *model = dev->dest->colorspace;
float expansion = fz_matrix_expansion(ctm);
float flatness = 0.3f / expansion;
@@ -416,14 +415,14 @@ fz_draw_clip_stroke_path(void *user, fz_path *path, fz_rect *rect, fz_stroke_sta
if (rect)
bbox = fz_intersect_bbox(bbox, fz_round_rect(*rect));
- mask = fz_new_pixmap_with_rect(NULL, bbox);
+ mask = fz_new_pixmap_with_rect(dev->ctx, NULL, bbox);
fz_clear_pixmap(mask);
- dest = fz_new_pixmap_with_rect(model, bbox);
+ dest = fz_new_pixmap_with_rect(dev->ctx, model, bbox);
/* FIXME: See note #1 */
fz_clear_pixmap(dest);
if (dev->shape)
{
- shape = fz_new_pixmap_with_rect(NULL, bbox);
+ shape = fz_new_pixmap_with_rect(dev->ctx, NULL, bbox);
fz_clear_pixmap(shape);
}
else
@@ -484,10 +483,10 @@ draw_glyph(unsigned char *colorbv, fz_pixmap *dst, fz_pixmap *msk,
}
static void
-fz_draw_fill_text(void *user, fz_text *text, fz_matrix ctm,
+fz_draw_fill_text(fz_device *devp, fz_text *text, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
- fz_draw_device *dev = user;
+ fz_draw_device *dev = devp->user;
fz_colorspace *model = dev->dest->colorspace;
unsigned char colorbv[FZ_MAX_COLORS + 1];
unsigned char shapebv;
@@ -521,7 +520,7 @@ fz_draw_fill_text(void *user, fz_text *text, fz_matrix ctm,
trm.e = QUANT(trm.e - floorf(trm.e), HSUBPIX);
trm.f = QUANT(trm.f - floorf(trm.f), VSUBPIX);
- glyph = fz_render_glyph(dev->cache, text->font, gid, trm, model);
+ glyph = fz_render_glyph(dev->ctx, dev->cache, text->font, gid, trm, model);
if (glyph)
{
if (glyph->n == 1)
@@ -535,7 +534,7 @@ fz_draw_fill_text(void *user, fz_text *text, fz_matrix ctm,
fz_matrix ctm = {glyph->w, 0.0, 0.0, -glyph->h, x + glyph->x, y + glyph->y + glyph->h};
fz_paint_image(dev->dest, dev->scissor, dev->shape, glyph, ctm, alpha * 255);
}
- fz_drop_pixmap(glyph);
+ fz_drop_pixmap(dev->ctx, glyph);
}
}
@@ -544,10 +543,10 @@ fz_draw_fill_text(void *user, fz_text *text, fz_matrix ctm,
}
static void
-fz_draw_stroke_text(void *user, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm,
+fz_draw_stroke_text(fz_device *devp, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
- fz_draw_device *dev = user;
+ fz_draw_device *dev = devp->user;
fz_colorspace *model = dev->dest->colorspace;
unsigned char colorbv[FZ_MAX_COLORS + 1];
float colorfv[FZ_MAX_COLORS];
@@ -579,13 +578,13 @@ fz_draw_stroke_text(void *user, fz_text *text, fz_stroke_state *stroke, fz_matri
trm.e = QUANT(trm.e - floorf(trm.e), HSUBPIX);
trm.f = QUANT(trm.f - floorf(trm.f), VSUBPIX);
- glyph = fz_render_stroked_glyph(dev->cache, text->font, gid, trm, ctm, stroke);
+ glyph = fz_render_stroked_glyph(dev->ctx, dev->cache, text->font, gid, trm, ctm, stroke);
if (glyph)
{
draw_glyph(colorbv, dev->dest, glyph, x, y, dev->scissor);
if (dev->shape)
draw_glyph(colorbv, dev->shape, glyph, x, y, dev->scissor);
- fz_drop_pixmap(glyph);
+ fz_drop_pixmap(dev->ctx, glyph);
}
}
@@ -594,9 +593,9 @@ fz_draw_stroke_text(void *user, fz_text *text, fz_stroke_state *stroke, fz_matri
}
static void
-fz_draw_clip_text(void *user, fz_text *text, fz_matrix ctm, int accumulate)
+fz_draw_clip_text(fz_device *devp, fz_text *text, fz_matrix ctm, int accumulate)
{
- fz_draw_device *dev = user;
+ fz_draw_device *dev = devp->user;
fz_colorspace *model = dev->dest->colorspace;
fz_bbox bbox;
fz_pixmap *mask, *dest, *shape;
@@ -628,14 +627,14 @@ fz_draw_clip_text(void *user, fz_text *text, fz_matrix ctm, int accumulate)
if (accumulate == 0 || accumulate == 1)
{
- mask = fz_new_pixmap_with_rect(NULL, bbox);
+ mask = fz_new_pixmap_with_rect(dev->ctx, NULL, bbox);
fz_clear_pixmap(mask);
- dest = fz_new_pixmap_with_rect(model, bbox);
+ dest = fz_new_pixmap_with_rect(dev->ctx, model, bbox);
/* FIXME: See note #1 */
fz_clear_pixmap(dest);
if (dev->shape)
{
- shape = fz_new_pixmap_with_rect(NULL, bbox);
+ shape = fz_new_pixmap_with_rect(dev->ctx, NULL, bbox);
fz_clear_pixmap(shape);
}
else
@@ -678,22 +677,22 @@ fz_draw_clip_text(void *user, fz_text *text, fz_matrix ctm, int accumulate)
trm.e = QUANT(trm.e - floorf(trm.e), HSUBPIX);
trm.f = QUANT(trm.f - floorf(trm.f), VSUBPIX);
- glyph = fz_render_glyph(dev->cache, text->font, gid, trm, model);
+ glyph = fz_render_glyph(dev->ctx, dev->cache, text->font, gid, trm, model);
if (glyph)
{
draw_glyph(NULL, mask, glyph, x, y, bbox);
if (dev->shape)
draw_glyph(NULL, dev->shape, glyph, x, y, bbox);
- fz_drop_pixmap(glyph);
+ fz_drop_pixmap(dev->ctx, glyph);
}
}
}
}
static void
-fz_draw_clip_stroke_text(void *user, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm)
+fz_draw_clip_stroke_text(fz_device *devp, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm)
{
- fz_draw_device *dev = user;
+ fz_draw_device *dev = devp->user;
fz_colorspace *model = dev->dest->colorspace;
fz_bbox bbox;
fz_pixmap *mask, *dest, *shape;
@@ -711,14 +710,14 @@ fz_draw_clip_stroke_text(void *user, fz_text *text, fz_stroke_state *stroke, fz_
bbox = fz_round_rect(fz_bound_text(text, ctm));
bbox = fz_intersect_bbox(bbox, dev->scissor);
- mask = fz_new_pixmap_with_rect(NULL, bbox);
+ mask = fz_new_pixmap_with_rect(dev->ctx, NULL, bbox);
fz_clear_pixmap(mask);
- dest = fz_new_pixmap_with_rect(model, bbox);
+ dest = fz_new_pixmap_with_rect(dev->ctx, model, bbox);
/* FIXME: See note #1 */
fz_clear_pixmap(dest);
if (dev->shape)
{
- shape = fz_new_pixmap_with_rect(NULL, bbox);
+ shape = fz_new_pixmap_with_rect(dev->ctx, NULL, bbox);
fz_clear_pixmap(shape);
}
else
@@ -756,27 +755,27 @@ fz_draw_clip_stroke_text(void *user, fz_text *text, fz_stroke_state *stroke, fz_
trm.e = QUANT(trm.e - floorf(trm.e), HSUBPIX);
trm.f = QUANT(trm.f - floorf(trm.f), VSUBPIX);
- glyph = fz_render_stroked_glyph(dev->cache, text->font, gid, trm, ctm, stroke);
+ glyph = fz_render_stroked_glyph(dev->ctx, dev->cache, text->font, gid, trm, ctm, stroke);
if (glyph)
{
draw_glyph(NULL, mask, glyph, x, y, bbox);
if (dev->shape)
draw_glyph(NULL, dev->shape, glyph, x, y, bbox);
- fz_drop_pixmap(glyph);
+ fz_drop_pixmap(dev->ctx, glyph);
}
}
}
}
static void
-fz_draw_ignore_text(void *user, fz_text *text, fz_matrix ctm)
+fz_draw_ignore_text(fz_device *dev, fz_text *text, fz_matrix ctm)
{
}
static void
-fz_draw_fill_shade(void *user, fz_shade *shade, fz_matrix ctm, float alpha)
+fz_draw_fill_shade(fz_device *devp, fz_shade *shade, fz_matrix ctm, float alpha)
{
- fz_draw_device *dev = user;
+ fz_draw_device *dev = devp->user;
fz_colorspace *model = dev->dest->colorspace;
fz_pixmap *dest = dev->dest;
fz_rect bounds;
@@ -801,7 +800,7 @@ fz_draw_fill_shade(void *user, fz_shade *shade, fz_matrix ctm, float alpha)
if (alpha < 1)
{
- dest = fz_new_pixmap_with_rect(dev->dest->colorspace, bbox);
+ dest = fz_new_pixmap_with_rect(dev->ctx, dev->dest->colorspace, bbox);
fz_clear_pixmap(dest);
}
@@ -840,14 +839,14 @@ fz_draw_fill_shade(void *user, fz_shade *shade, fz_matrix ctm, float alpha)
}
}
- fz_paint_shade(shade, ctm, dest, bbox);
+ fz_paint_shade(dev->ctx, shade, ctm, dest, bbox);
if (dev->shape)
fz_clear_pixmap_rect_with_color(dev->shape, 255, bbox);
if (alpha < 1)
{
fz_paint_pixmap(dev->dest, dest, alpha * 255);
- fz_drop_pixmap(dest);
+ fz_drop_pixmap(dev->ctx, dest);
}
if (dev->blendmode & FZ_BLEND_KNOCKOUT)
@@ -855,14 +854,14 @@ fz_draw_fill_shade(void *user, fz_shade *shade, fz_matrix ctm, float alpha)
}
static fz_pixmap *
-fz_transform_pixmap(fz_pixmap *image, fz_matrix *ctm, int x, int y, int dx, int dy, int gridfit)
+fz_transform_pixmap(fz_context *ctx, fz_pixmap *image, fz_matrix *ctm, int x, int y, int dx, int dy, int gridfit)
{
fz_pixmap *scaled;
if (ctm->a != 0 && ctm->b == 0 && ctm->c == 0 && ctm->d != 0)
{
/* Unrotated or X-flip or Y-flip or XY-flip */
- scaled = fz_scale_pixmap_gridfit(image, ctm->e, ctm->f, ctm->a, ctm->d, gridfit);
+ scaled = fz_scale_pixmap_gridfit(ctx, image, ctm->e, ctm->f, ctm->a, ctm->d, gridfit);
if (scaled == NULL)
return NULL;
ctm->a = scaled->w;
@@ -875,7 +874,7 @@ fz_transform_pixmap(fz_pixmap *image, fz_matrix *ctm, int x, int y, int dx, int
if (ctm->a == 0 && ctm->b != 0 && ctm->c != 0 && ctm->d == 0)
{
/* Other orthogonal flip/rotation cases */
- scaled = fz_scale_pixmap_gridfit(image, ctm->f, ctm->e, ctm->b, ctm->c, gridfit);
+ scaled = fz_scale_pixmap_gridfit(ctx, image, ctm->f, ctm->e, ctm->b, ctm->c, gridfit);
if (scaled == NULL)
return NULL;
ctm->b = scaled->w;
@@ -888,7 +887,7 @@ fz_transform_pixmap(fz_pixmap *image, fz_matrix *ctm, int x, int y, int dx, int
/* Downscale, non rectilinear case */
if (dx > 0 && dy > 0)
{
- scaled = fz_scale_pixmap(image, 0, 0, (float)dx, (float)dy);
+ scaled = fz_scale_pixmap(ctx, image, 0, 0, (float)dx, (float)dy);
return scaled;
}
@@ -896,9 +895,9 @@ fz_transform_pixmap(fz_pixmap *image, fz_matrix *ctm, int x, int y, int dx, int
}
static void
-fz_draw_fill_image(void *user, fz_pixmap *image, fz_matrix ctm, float alpha)
+fz_draw_fill_image(fz_device *devp, fz_pixmap *image, fz_matrix ctm, float alpha)
{
- fz_draw_device *dev = user;
+ fz_draw_device *dev = devp->user;
fz_colorspace *model = dev->dest->colorspace;
fz_pixmap *converted = NULL;
fz_pixmap *scaled = NULL;
@@ -927,8 +926,8 @@ fz_draw_fill_image(void *user, fz_pixmap *image, fz_matrix ctm, float alpha)
if (image->colorspace != model && !after)
{
- converted = fz_new_pixmap_with_rect(model, fz_bound_pixmap(image));
- fz_convert_pixmap(image, converted);
+ converted = fz_new_pixmap_with_rect(dev->ctx, model, fz_bound_pixmap(image));
+ fz_convert_pixmap(dev->ctx, image, converted);
image = converted;
}
@@ -937,14 +936,14 @@ fz_draw_fill_image(void *user, fz_pixmap *image, fz_matrix ctm, float alpha)
if (dx < image->w && dy < image->h)
{
int gridfit = alpha == 1.0f && !(dev->flags & FZ_DRAWDEV_FLAGS_TYPE3);
- scaled = fz_transform_pixmap(image, &ctm, dev->dest->x, dev->dest->y, dx, dy, gridfit);
+ scaled = fz_transform_pixmap(dev->ctx, image, &ctm, dev->dest->x, dev->dest->y, dx, dy, gridfit);
if (scaled == NULL)
{
if (dx < 1)
dx = 1;
if (dy < 1)
dy = 1;
- scaled = fz_scale_pixmap(image, image->x, image->y, dx, dy);
+ scaled = fz_scale_pixmap(dev->ctx, image, image->x, image->y, dx, dy);
}
if (scaled != NULL)
image = scaled;
@@ -959,8 +958,8 @@ fz_draw_fill_image(void *user, fz_pixmap *image, fz_matrix ctm, float alpha)
}
else
{
- converted = fz_new_pixmap_with_rect(model, fz_bound_pixmap(image));
- fz_convert_pixmap(image, converted);
+ converted = fz_new_pixmap_with_rect(dev->ctx, model, fz_bound_pixmap(image));
+ fz_convert_pixmap(dev->ctx, image, converted);
image = converted;
}
}
@@ -968,19 +967,19 @@ fz_draw_fill_image(void *user, fz_pixmap *image, fz_matrix ctm, float alpha)
fz_paint_image(dev->dest, dev->scissor, dev->shape, image, ctm, alpha * 255);
if (scaled)
- fz_drop_pixmap(scaled);
+ fz_drop_pixmap(dev->ctx, scaled);
if (converted)
- fz_drop_pixmap(converted);
+ fz_drop_pixmap(dev->ctx, converted);
if (dev->blendmode & FZ_BLEND_KNOCKOUT)
fz_knockout_end(dev);
}
static void
-fz_draw_fill_image_mask(void *user, fz_pixmap *image, fz_matrix ctm,
+fz_draw_fill_image_mask(fz_device *devp, fz_pixmap *image, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
- fz_draw_device *dev = user;
+ fz_draw_device *dev = devp->user;
fz_colorspace *model = dev->dest->colorspace;
unsigned char colorbv[FZ_MAX_COLORS + 1];
float colorfv[FZ_MAX_COLORS];
@@ -999,14 +998,14 @@ fz_draw_fill_image_mask(void *user, fz_pixmap *image, fz_matrix ctm,
if (dx < image->w && dy < image->h)
{
int gridfit = alpha == 1.0f && !(dev->flags & FZ_DRAWDEV_FLAGS_TYPE3);
- scaled = fz_transform_pixmap(image, &ctm, dev->dest->x, dev->dest->y, dx, dy, gridfit);
+ scaled = fz_transform_pixmap(dev->ctx, image, &ctm, dev->dest->x, dev->dest->y, dx, dy, gridfit);
if (scaled == NULL)
{
if (dx < 1)
dx = 1;
if (dy < 1)
dy = 1;
- scaled = fz_scale_pixmap(image, image->x, image->y, dx, dy);
+ scaled = fz_scale_pixmap(dev->ctx, image, image->x, image->y, dx, dy);
}
if (scaled != NULL)
image = scaled;
@@ -1020,16 +1019,16 @@ fz_draw_fill_image_mask(void *user, fz_pixmap *image, fz_matrix ctm,
fz_paint_image_with_color(dev->dest, dev->scissor, dev->shape, image, ctm, colorbv);
if (scaled)
- fz_drop_pixmap(scaled);
+ fz_drop_pixmap(dev->ctx, scaled);
if (dev->blendmode & FZ_BLEND_KNOCKOUT)
fz_knockout_begin(dev);
}
static void
-fz_draw_clip_image_mask(void *user, fz_pixmap *image, fz_rect *rect, fz_matrix ctm)
+fz_draw_clip_image_mask(fz_device *devp, fz_pixmap *image, fz_rect *rect, fz_matrix ctm)
{
- fz_draw_device *dev = user;
+ fz_draw_device *dev = devp->user;
fz_colorspace *model = dev->dest->colorspace;
fz_bbox bbox;
fz_pixmap *mask, *dest, *shape;
@@ -1062,14 +1061,14 @@ fz_draw_clip_image_mask(void *user, fz_pixmap *image, fz_rect *rect, fz_matrix c
if (rect)
bbox = fz_intersect_bbox(bbox, fz_round_rect(*rect));
- mask = fz_new_pixmap_with_rect(NULL, bbox);
+ mask = fz_new_pixmap_with_rect(dev->ctx, NULL, bbox);
fz_clear_pixmap(mask);
- dest = fz_new_pixmap_with_rect(model, bbox);
+ dest = fz_new_pixmap_with_rect(dev->ctx, model, bbox);
/* FIXME: See note #1 */
fz_clear_pixmap(dest);
if (dev->shape)
{
- shape = fz_new_pixmap_with_rect(NULL, bbox);
+ shape = fz_new_pixmap_with_rect(dev->ctx, NULL, bbox);
fz_clear_pixmap(shape);
}
else
@@ -1080,14 +1079,14 @@ fz_draw_clip_image_mask(void *user, fz_pixmap *image, fz_rect *rect, fz_matrix c
if (dx < image->w && dy < image->h)
{
int gridfit = !(dev->flags & FZ_DRAWDEV_FLAGS_TYPE3);
- scaled = fz_transform_pixmap(image, &ctm, dev->dest->x, dev->dest->y, dx, dy, gridfit);
+ scaled = fz_transform_pixmap(dev->ctx, image, &ctm, dev->dest->x, dev->dest->y, dx, dy, gridfit);
if (scaled == NULL)
{
if (dx < 1)
dx = 1;
if (dy < 1)
dy = 1;
- scaled = fz_scale_pixmap(image, image->x, image->y, dx, dy);
+ scaled = fz_scale_pixmap(dev->ctx, image, image->x, image->y, dx, dy);
}
if (scaled != NULL)
image = scaled;
@@ -1096,7 +1095,7 @@ fz_draw_clip_image_mask(void *user, fz_pixmap *image, fz_rect *rect, fz_matrix c
fz_paint_image(mask, bbox, dev->shape, image, ctm, 255);
if (scaled)
- fz_drop_pixmap(scaled);
+ fz_drop_pixmap(dev->ctx, scaled);
dev->stack[dev->top].scissor = dev->scissor;
dev->stack[dev->top].mask = mask;
@@ -1111,9 +1110,9 @@ fz_draw_clip_image_mask(void *user, fz_pixmap *image, fz_rect *rect, fz_matrix c
}
static void
-fz_draw_pop_clip(void *user)
+fz_draw_pop_clip(fz_device *devp)
{
- fz_draw_device *dev = user;
+ fz_draw_device *dev = devp->user;
fz_pixmap *mask, *dest, *shape;
if (dev->top > 0)
{
@@ -1147,11 +1146,11 @@ fz_draw_pop_clip(void *user)
{
assert(shape != dev->shape);
fz_paint_pixmap_with_mask(shape, dev->shape, mask);
- fz_drop_pixmap(dev->shape);
+ fz_drop_pixmap(dev->ctx, dev->shape);
dev->shape = shape;
}
- fz_drop_pixmap(mask);
- fz_drop_pixmap(dev->dest);
+ fz_drop_pixmap(dev->ctx, mask);
+ fz_drop_pixmap(dev->ctx, dev->dest);
dev->dest = dest;
#ifdef DUMP_GROUP_BLENDS
fz_dump_blend(dev->dest, " to get ");
@@ -1172,9 +1171,9 @@ fz_draw_pop_clip(void *user)
}
static void
-fz_draw_begin_mask(void *user, fz_rect rect, int luminosity, fz_colorspace *colorspace, float *colorfv)
+fz_draw_begin_mask(fz_device *devp, fz_rect rect, int luminosity, fz_colorspace *colorspace, float *colorfv)
{
- fz_draw_device *dev = user;
+ fz_draw_device *dev = devp->user;
fz_pixmap *dest;
fz_pixmap *shape = dev->shape;
fz_bbox bbox;
@@ -1187,7 +1186,7 @@ fz_draw_begin_mask(void *user, fz_rect rect, int luminosity, fz_colorspace *colo
bbox = fz_round_rect(rect);
bbox = fz_intersect_bbox(bbox, dev->scissor);
- dest = fz_new_pixmap_with_rect(fz_device_gray, bbox);
+ dest = fz_new_pixmap_with_rect(dev->ctx, fz_device_gray, bbox);
if (dev->shape)
{
/* FIXME: If we ever want to support AIS true, then we
@@ -1232,9 +1231,9 @@ fz_draw_begin_mask(void *user, fz_rect rect, int luminosity, fz_colorspace *colo
}
static void
-fz_draw_end_mask(void *user)
+fz_draw_end_mask(fz_device *devp)
{
- fz_draw_device *dev = user;
+ fz_draw_device *dev = devp->user;
fz_pixmap *mask = dev->dest;
fz_pixmap *maskshape = dev->shape;
fz_pixmap *temp, *dest;
@@ -1257,13 +1256,13 @@ fz_draw_end_mask(void *user)
dev->shape = dev->stack[dev->top].shape;
/* convert to alpha mask */
- temp = fz_alpha_from_gray(mask, luminosity);
- fz_drop_pixmap(mask);
- fz_drop_pixmap(maskshape);
+ temp = fz_alpha_from_gray(dev->ctx, mask, luminosity);
+ fz_drop_pixmap(dev->ctx, mask);
+ fz_drop_pixmap(dev->ctx, maskshape);
/* create new dest scratch buffer */
bbox = fz_bound_pixmap(temp);
- dest = fz_new_pixmap_with_rect(dev->dest->colorspace, bbox);
+ dest = fz_new_pixmap_with_rect(dev->ctx, dev->dest->colorspace, bbox);
/* FIXME: See note #1 */
fz_clear_pixmap(dest);
@@ -1278,7 +1277,7 @@ fz_draw_end_mask(void *user)
if (dev->shape)
{
dev->stack[dev->top].shape = dev->shape;
- dev->shape = fz_new_pixmap_with_rect(NULL, bbox);
+ dev->shape = fz_new_pixmap_with_rect(dev->ctx, NULL, bbox);
fz_clear_pixmap(dev->shape);
}
dev->scissor = bbox;
@@ -1291,9 +1290,9 @@ fz_draw_end_mask(void *user)
}
static void
-fz_draw_begin_group(void *user, fz_rect rect, int isolated, int knockout, int blendmode, float alpha)
+fz_draw_begin_group(fz_device *devp, fz_rect rect, int isolated, int knockout, int blendmode, float alpha)
{
- fz_draw_device *dev = user;
+ fz_draw_device *dev = devp->user;
fz_colorspace *model = dev->dest->colorspace;
fz_bbox bbox;
fz_pixmap *dest, *shape;
@@ -1309,7 +1308,7 @@ fz_draw_begin_group(void *user, fz_rect rect, int isolated, int knockout, int bl
bbox = fz_round_rect(rect);
bbox = fz_intersect_bbox(bbox, dev->scissor);
- dest = fz_new_pixmap_with_rect(model, bbox);
+ dest = fz_new_pixmap_with_rect(dev->ctx, model, bbox);
#ifndef ATTEMPT_KNOCKOUT_AND_ISOLATED
knockout = 0;
@@ -1333,7 +1332,7 @@ fz_draw_begin_group(void *user, fz_rect rect, int isolated, int knockout, int bl
}
else
{
- shape = fz_new_pixmap_with_rect(NULL, bbox);
+ shape = fz_new_pixmap_with_rect(dev->ctx, NULL, bbox);
fz_clear_pixmap(shape);
}
@@ -1354,9 +1353,9 @@ fz_draw_begin_group(void *user, fz_rect rect, int isolated, int knockout, int bl
}
static void
-fz_draw_end_group(void *user)
+fz_draw_end_group(fz_device *devp)
{
- fz_draw_device *dev = user;
+ fz_draw_device *dev = devp->user;
fz_pixmap *group = dev->dest;
fz_pixmap *shape = dev->shape;
int blendmode;
@@ -1396,14 +1395,14 @@ fz_draw_end_group(void *user)
else
fz_blend_pixmap(dev->dest, group, alpha * 255, blendmode, isolated, shape);
- fz_drop_pixmap(group);
+ fz_drop_pixmap(dev->ctx, group);
if (shape != dev->shape)
{
if (dev->shape)
{
fz_paint_pixmap(dev->shape, shape, alpha * 255);
}
- fz_drop_pixmap(shape);
+ fz_drop_pixmap(dev->ctx, shape);
}
#ifdef DUMP_GROUP_BLENDS
fz_dump_blend(dev->dest, " to get ");
@@ -1418,9 +1417,9 @@ fz_draw_end_group(void *user)
}
static void
-fz_draw_begin_tile(void *user, fz_rect area, fz_rect view, float xstep, float ystep, fz_matrix ctm)
+fz_draw_begin_tile(fz_device *devp, fz_rect area, fz_rect view, float xstep, float ystep, fz_matrix ctm)
{
- fz_draw_device *dev = user;
+ fz_draw_device *dev = devp->user;
fz_colorspace *model = dev->dest->colorspace;
fz_pixmap *dest;
fz_bbox bbox;
@@ -1438,7 +1437,7 @@ fz_draw_begin_tile(void *user, fz_rect area, fz_rect view, float xstep, float ys
fz_knockout_begin(dev);
bbox = fz_round_rect(fz_transform_rect(ctm, view));
- dest = fz_new_pixmap_with_rect(model, bbox);
+ dest = fz_new_pixmap_with_rect(dev->ctx, model, bbox);
/* FIXME: See note #1 */
fz_clear_pixmap(dest);
@@ -1461,9 +1460,9 @@ fz_draw_begin_tile(void *user, fz_rect area, fz_rect view, float xstep, float ys
}
static void
-fz_draw_end_tile(void *user)
+fz_draw_end_tile(fz_device *devp)
{
- fz_draw_device *dev = user;
+ fz_draw_device *dev = devp->user;
fz_pixmap *tile = dev->dest;
float xstep, ystep;
fz_matrix ctm, ttm;
@@ -1504,7 +1503,7 @@ fz_draw_end_tile(void *user)
}
}
- fz_drop_pixmap(tile);
+ fz_drop_pixmap(dev->ctx, tile);
}
if (dev->blendmode & FZ_BLEND_KNOCKOUT)
@@ -1512,35 +1511,36 @@ fz_draw_end_tile(void *user)
}
static void
-fz_draw_free_user(void *user)
+fz_draw_free_user(fz_device *devp)
{
- fz_draw_device *dev = user;
+ fz_draw_device *dev = devp->user;
/* TODO: pop and free the stacks */
if (dev->top > 0)
fz_warn("items left on stack in draw device: %d", dev->top);
fz_free_gel(dev->gel);
- fz_free(dev);
+ fz_free(devp->ctx, dev);
}
fz_device *
-fz_new_draw_device(fz_glyph_cache *cache, fz_pixmap *dest)
+fz_new_draw_device(fz_context *ctx, fz_glyph_cache *cache, fz_pixmap *dest)
{
fz_device *dev;
- fz_draw_device *ddev = fz_malloc(sizeof(fz_draw_device));
+ fz_draw_device *ddev = fz_malloc(ctx, sizeof(fz_draw_device));
ddev->cache = cache;
- ddev->gel = fz_new_gel();
+ ddev->gel = fz_new_gel(ctx);
ddev->dest = dest;
ddev->shape = NULL;
ddev->top = 0;
ddev->blendmode = 0;
ddev->flags = 0;
+ ddev->ctx = ctx;
ddev->scissor.x0 = dest->x;
ddev->scissor.y0 = dest->y;
ddev->scissor.x1 = dest->x + dest->w;
ddev->scissor.y1 = dest->y + dest->h;
- dev = fz_new_device(ddev);
+ dev = fz_new_device(ctx, ddev);
dev->free_user = fz_draw_free_user;
dev->fill_path = fz_draw_fill_path;
@@ -1573,9 +1573,9 @@ fz_new_draw_device(fz_glyph_cache *cache, fz_pixmap *dest)
}
fz_device *
-fz_new_draw_device_type3(fz_glyph_cache *cache, fz_pixmap *dest)
+fz_new_draw_device_type3(fz_context *ctx, fz_glyph_cache *cache, fz_pixmap *dest)
{
- fz_device *dev = fz_new_draw_device(cache, dest);
+ fz_device *dev = fz_new_draw_device(ctx, cache, dest);
fz_draw_device *ddev = dev->user;
ddev->flags |= FZ_DRAWDEV_FLAGS_TYPE3;
return dev;
diff --git a/draw/draw_edge.c b/draw/draw_edge.c
index 123c3268..ee218bc9 100644
--- a/draw/draw_edge.c
+++ b/draw/draw_edge.c
@@ -127,17 +127,19 @@ struct fz_gel_s
fz_edge *edges;
int acap, alen;
fz_edge **active;
+ fz_context *ctx;
};
fz_gel *
-fz_new_gel(void)
+fz_new_gel(fz_context *ctx)
{
fz_gel *gel;
- gel = fz_malloc(sizeof(fz_gel));
+ gel = fz_malloc(ctx, sizeof(fz_gel));
+ gel->ctx = ctx;
gel->cap = 512;
gel->len = 0;
- gel->edges = fz_calloc(gel->cap, sizeof(fz_edge));
+ gel->edges = fz_calloc(ctx, gel->cap, sizeof(fz_edge));
gel->clip.x0 = gel->clip.y0 = BBOX_MAX;
gel->clip.x1 = gel->clip.y1 = BBOX_MIN;
@@ -147,7 +149,7 @@ fz_new_gel(void)
gel->acap = 64;
gel->alen = 0;
- gel->active = fz_calloc(gel->acap, sizeof(fz_edge*));
+ gel->active = fz_calloc(ctx, gel->acap, sizeof(fz_edge*));
return gel;
}
@@ -176,9 +178,9 @@ fz_reset_gel(fz_gel *gel, fz_bbox clip)
void
fz_free_gel(fz_gel *gel)
{
- fz_free(gel->active);
- fz_free(gel->edges);
- fz_free(gel);
+ fz_free(gel->ctx, gel->active);
+ fz_free(gel->ctx, gel->edges);
+ fz_free(gel->ctx, gel);
}
fz_bbox
@@ -253,7 +255,7 @@ fz_insert_gel_raw(fz_gel *gel, int x0, int y0, int x1, int y1)
if (gel->len + 1 == gel->cap) {
gel->cap = gel->cap + 512;
- gel->edges = fz_realloc(gel->edges, gel->cap, sizeof(fz_edge));
+ gel->edges = fz_realloc(gel->ctx, gel->edges, gel->cap * sizeof(fz_edge));
}
edge = &gel->edges[gel->len++];
@@ -443,7 +445,7 @@ insert_active(fz_gel *gel, int y, int *e)
while (*e < gel->len && gel->edges[*e].y == y) {
if (gel->alen + 1 == gel->acap) {
int newcap = gel->acap + 64;
- fz_edge **newactive = fz_realloc(gel->active, newcap, sizeof(fz_edge*));
+ fz_edge **newactive = fz_realloc(gel->ctx, gel->active, newcap * sizeof(fz_edge*));
gel->active = newactive;
gel->acap = newcap;
}
@@ -593,8 +595,8 @@ fz_scan_convert_aa(fz_gel *gel, int eofill, fz_bbox clip,
assert(clip.x0 >= xmin);
assert(clip.x1 <= xmax);
- alphas = fz_malloc(xmax - xmin + 1);
- deltas = fz_malloc((xmax - xmin + 1) * sizeof(int));
+ alphas = fz_malloc(gel->ctx, xmax - xmin + 1);
+ deltas = fz_malloc(gel->ctx, (xmax - xmin + 1) * sizeof(int));
memset(deltas, 0, (xmax - xmin + 1) * sizeof(int));
e = 0;
@@ -640,8 +642,8 @@ fz_scan_convert_aa(fz_gel *gel, int eofill, fz_bbox clip,
blit_aa(dst, xmin + skipx, yd, alphas + skipx, clipn, color);
}
- fz_free(deltas);
- fz_free(alphas);
+ fz_free(gel->ctx, deltas);
+ fz_free(gel->ctx, alphas);
}
/*
diff --git a/draw/draw_glyph.c b/draw/draw_glyph.c
index 95f3955d..1d94f767 100644
--- a/draw/draw_glyph.c
+++ b/draw/draw_glyph.c
@@ -22,19 +22,19 @@ struct fz_glyph_key_s
};
fz_glyph_cache *
-fz_new_glyph_cache(void)
+fz_new_glyph_cache(fz_context *ctx)
{
fz_glyph_cache *cache;
- cache = fz_malloc(sizeof(fz_glyph_cache));
- cache->hash = fz_new_hash_table(509, sizeof(fz_glyph_key));
+ cache = fz_malloc(ctx, sizeof(fz_glyph_cache));
+ cache->hash = fz_new_hash_table(ctx, 509, sizeof(fz_glyph_key));
cache->total = 0;
return cache;
}
static void
-fz_evict_glyph_cache(fz_glyph_cache *cache)
+fz_evict_glyph_cache(fz_context *ctx, fz_glyph_cache *cache)
{
fz_glyph_key *key;
fz_pixmap *pixmap;
@@ -44,10 +44,10 @@ fz_evict_glyph_cache(fz_glyph_cache *cache)
{
key = fz_hash_get_key(cache->hash, i);
if (key->font)
- fz_drop_font(key->font);
+ fz_drop_font(ctx, key->font);
pixmap = fz_hash_get_val(cache->hash, i);
if (pixmap)
- fz_drop_pixmap(pixmap);
+ fz_drop_pixmap(ctx, pixmap);
}
cache->total = 0;
@@ -56,23 +56,23 @@ fz_evict_glyph_cache(fz_glyph_cache *cache)
}
void
-fz_free_glyph_cache(fz_glyph_cache *cache)
+fz_free_glyph_cache(fz_context *ctx, fz_glyph_cache *cache)
{
- fz_evict_glyph_cache(cache);
- fz_free_hash(cache->hash);
- fz_free(cache);
+ fz_evict_glyph_cache(ctx, cache);
+ fz_free_hash(ctx, cache->hash);
+ fz_free(ctx, cache);
}
fz_pixmap *
-fz_render_stroked_glyph(fz_glyph_cache *cache, fz_font *font, int gid, fz_matrix trm, fz_matrix ctm, fz_stroke_state *stroke)
+fz_render_stroked_glyph(fz_context *ctx, fz_glyph_cache *cache, fz_font *font, int gid, fz_matrix trm, fz_matrix ctm, fz_stroke_state *stroke)
{
if (font->ft_face)
- return fz_render_ft_stroked_glyph(font, gid, trm, ctm, stroke);
- return fz_render_glyph(cache, font, gid, trm, NULL);
+ return fz_render_ft_stroked_glyph(ctx, font, gid, trm, ctm, stroke);
+ return fz_render_glyph(ctx, cache, font, gid, trm, NULL);
}
fz_pixmap *
-fz_render_glyph(fz_glyph_cache *cache, fz_font *font, int gid, fz_matrix ctm, fz_colorspace *model)
+fz_render_glyph(fz_context *ctx, fz_glyph_cache *cache, fz_font *font, int gid, fz_matrix ctm, fz_colorspace *model)
{
fz_glyph_key key;
fz_pixmap *val;
@@ -104,11 +104,11 @@ fz_render_glyph(fz_glyph_cache *cache, fz_font *font, int gid, fz_matrix ctm, fz
if (font->ft_face)
{
- val = fz_render_ft_glyph(font, gid, ctm);
+ val = fz_render_ft_glyph(ctx, font, gid, ctm);
}
else if (font->t3procs)
{
- val = fz_render_t3_glyph(font, gid, ctm, model);
+ val = fz_render_t3_glyph(ctx, font, gid, ctm, model);
}
else
{
@@ -121,9 +121,9 @@ fz_render_glyph(fz_glyph_cache *cache, fz_font *font, int gid, fz_matrix ctm, fz
if (val->w < MAX_GLYPH_SIZE && val->h < MAX_GLYPH_SIZE)
{
if (cache->total + val->w * val->h > MAX_CACHE_SIZE)
- fz_evict_glyph_cache(cache);
+ fz_evict_glyph_cache(ctx, cache);
fz_keep_font(key.font);
- fz_hash_insert(cache->hash, &key, val);
+ fz_hash_insert(ctx, cache->hash, &key, val);
cache->total += val->w * val->h;
return fz_keep_pixmap(val);
}
diff --git a/draw/draw_mesh.c b/draw/draw_mesh.c
index 4254cb64..6df30307 100644
--- a/draw/draw_mesh.c
+++ b/draw/draw_mesh.c
@@ -520,7 +520,7 @@ fz_paint_mesh(fz_shade *shade, fz_matrix ctm, fz_pixmap *dest, fz_bbox bbox)
}
void
-fz_paint_shade(fz_shade *shade, fz_matrix ctm, fz_pixmap *dest, fz_bbox bbox)
+fz_paint_shade(fz_context *ctx, fz_shade *shade, fz_matrix ctm, fz_pixmap *dest, fz_bbox bbox)
{
unsigned char clut[256][FZ_MAX_COLORS];
fz_pixmap *temp, *conv;
@@ -538,8 +538,8 @@ fz_paint_shade(fz_shade *shade, fz_matrix ctm, fz_pixmap *dest, fz_bbox bbox)
clut[i][k] = color[k] * 255;
clut[i][k] = shade->function[i][shade->colorspace->n] * 255;
}
- conv = fz_new_pixmap_with_rect(dest->colorspace, bbox);
- temp = fz_new_pixmap_with_rect(fz_device_gray, bbox);
+ conv = fz_new_pixmap_with_rect(ctx, dest->colorspace, bbox);
+ temp = fz_new_pixmap_with_rect(ctx, fz_device_gray, bbox);
fz_clear_pixmap(temp);
}
else
@@ -568,7 +568,7 @@ fz_paint_shade(fz_shade *shade, fz_matrix ctm, fz_pixmap *dest, fz_bbox bbox)
*d++ = a;
}
fz_paint_pixmap(dest, conv, 255);
- fz_drop_pixmap(conv);
- fz_drop_pixmap(temp);
+ fz_drop_pixmap(ctx, conv);
+ fz_drop_pixmap(ctx, temp);
}
}
diff --git a/draw/draw_scale.c b/draw/draw_scale.c
index 427512ce..045de810 100644
--- a/draw/draw_scale.c
+++ b/draw/draw_scale.c
@@ -256,7 +256,7 @@ struct fz_weights_s
};
static fz_weights *
-new_weights(fz_scale_filter *filter, int src_w, float dst_w, int dst_w_i, int n, int flip)
+new_weights(fz_context *ctx, fz_scale_filter *filter, int src_w, float dst_w, int dst_w_i, int n, int flip)
{
int max_len;
fz_weights *weights;
@@ -282,7 +282,7 @@ new_weights(fz_scale_filter *filter, int src_w, float dst_w, int dst_w_i, int n,
* plus (2+max_len)*sizeof(int) for the weights
* plus room for an extra set of weights for reordering.
*/
- weights = fz_malloc(sizeof(*weights)+(max_len+3)*(dst_w_i+1)*sizeof(int));
+ weights = fz_malloc(ctx, sizeof(*weights)+(max_len+3)*(dst_w_i+1)*sizeof(int));
if (weights == NULL)
return NULL;
weights->count = -1;
@@ -490,7 +490,7 @@ check_weights(fz_weights *weights, int j, int w, float x, float wf)
}
static fz_weights *
-make_weights(int src_w, float x, float dst_w, fz_scale_filter *filter, int vertical, int dst_w_int, int n, int flip)
+make_weights(fz_context *ctx, int src_w, float x, float dst_w, fz_scale_filter *filter, int vertical, int dst_w_int, int n, int flip)
{
fz_weights *weights;
float F, G;
@@ -511,7 +511,7 @@ make_weights(int src_w, float x, float dst_w, fz_scale_filter *filter, int verti
}
window = filter->width / F;
DBUG(("make_weights src_w=%d x=%g dst_w=%g dst_w_int=%d F=%g window=%g\n", src_w, x, dst_w, dst_w_int, F, window));
- weights = new_weights(filter, src_w, dst_w, dst_w_int, n, flip);
+ weights = new_weights(ctx, filter, src_w, dst_w, dst_w_int, n, flip);
if (weights == NULL)
return NULL;
for (j = 0; j < dst_w_int; j++)
@@ -1005,7 +1005,7 @@ scale_single_col(unsigned char *dst, unsigned char *src, fz_weights *weights, in
#endif /* SINGLE_PIXEL_SPECIALS */
fz_pixmap *
-fz_scale_pixmap_gridfit(fz_pixmap *src, float x, float y, float w, float h, int gridfit)
+fz_scale_pixmap_gridfit(fz_context *ctx, fz_pixmap *src, float x, float y, float w, float h, int gridfit)
{
if (gridfit) {
float n;
@@ -1060,11 +1060,11 @@ fz_scale_pixmap_gridfit(fz_pixmap *src, float x, float y, float w, float h, int
h = n - 1.0f;
}
}
- return fz_scale_pixmap(src, x, y, w, h);
+ return fz_scale_pixmap(ctx, src, x, y, w, h);
}
fz_pixmap *
-fz_scale_pixmap(fz_pixmap *src, float x, float y, float w, float h)
+fz_scale_pixmap(fz_context *ctx, fz_pixmap *src, float x, float y, float w, float h)
{
fz_scale_filter *filter = &fz_scale_filter_simple;
fz_weights *contrib_rows = NULL;
@@ -1146,7 +1146,7 @@ fz_scale_pixmap(fz_pixmap *src, float x, float y, float w, float h)
else
#endif /* SINGLE_PIXEL_SPECIALS */
{
- contrib_cols = make_weights(src->w, x, w, filter, 0, dst_w_int, src->n, flip_x);
+ contrib_cols = make_weights(ctx, src->w, x, w, filter, 0, dst_w_int, src->n, flip_x);
if (contrib_cols == NULL)
goto cleanup;
}
@@ -1158,14 +1158,14 @@ fz_scale_pixmap(fz_pixmap *src, float x, float y, float w, float h)
else
#endif /* SINGLE_PIXEL_SPECIALS */
{
- contrib_rows = make_weights(src->h, y, h, filter, 1, dst_h_int, src->n, flip_y);
+ contrib_rows = make_weights(ctx, src->h, y, h, filter, 1, dst_h_int, src->n, flip_y);
if (contrib_rows == NULL)
goto cleanup;
}
assert(contrib_cols == NULL || contrib_cols->count == dst_w_int);
assert(contrib_rows == NULL || contrib_rows->count == dst_h_int);
- output = fz_new_pixmap(src->colorspace, dst_w_int, dst_h_int);
+ output = fz_new_pixmap(ctx, src->colorspace, dst_w_int, dst_h_int);
output->x = dst_x_int;
output->y = dst_y_int;
@@ -1199,7 +1199,7 @@ fz_scale_pixmap(fz_pixmap *src, float x, float y, float w, float h)
temp_rows = contrib_rows->max_len;
if (temp_span <= 0 || temp_rows > INT_MAX / temp_span)
goto cleanup;
- temp = fz_calloc(temp_span*temp_rows, sizeof(int));
+ temp = fz_calloc(ctx, temp_span*temp_rows, sizeof(int));
if (temp == NULL)
goto cleanup;
switch (src->n)
@@ -1240,11 +1240,11 @@ fz_scale_pixmap(fz_pixmap *src, float x, float y, float w, float h)
DBUG(("scaling row %d from temp\n", row));
scale_row_from_temp(&output->samples[row*output->w*output->n], temp, contrib_rows, temp_span, row);
}
- fz_free(temp);
+ fz_free(ctx, temp);
}
cleanup:
- fz_free(contrib_rows);
- fz_free(contrib_cols);
+ fz_free(ctx, contrib_rows);
+ fz_free(ctx, contrib_cols);
return output;
}
diff --git a/fitz/base_hash.c b/fitz/base_hash.c
index b7caf173..f754644d 100644
--- a/fitz/base_hash.c
+++ b/fitz/base_hash.c
@@ -42,18 +42,17 @@ static unsigned hash(unsigned char *s, int len)
}
fz_hash_table *
-fz_new_hash_table(int initialsize, int keylen)
+fz_new_hash_table(fz_context *ctx, int initialsize, int keylen)
{
fz_hash_table *table;
assert(keylen <= MAX_KEY_LEN);
- table = fz_malloc(sizeof(fz_hash_table));
+ table = fz_malloc(ctx, sizeof(fz_hash_table));
table->keylen = keylen;
table->size = initialsize;
table->load = 0;
- table->ents = fz_calloc(table->size, sizeof(fz_hash_entry));
- memset(table->ents, 0, sizeof(fz_hash_entry) * table->size);
+ table->ents = fz_calloc(ctx, table->size, sizeof(fz_hash_entry));
return table;
}
@@ -84,14 +83,14 @@ fz_hash_get_val(fz_hash_table *table, int idx)
}
void
-fz_free_hash(fz_hash_table *table)
+fz_free_hash(fz_context *ctx, fz_hash_table *table)
{
- fz_free(table->ents);
- fz_free(table);
+ fz_free(ctx, table->ents);
+ fz_free(ctx, table);
}
static void
-fz_resize_hash(fz_hash_table *table, int newsize)
+fz_resize_hash(fz_context *ctx, fz_hash_table *table, int newsize)
{
fz_hash_entry *oldents = table->ents;
int oldsize = table->size;
@@ -104,7 +103,7 @@ fz_resize_hash(fz_hash_table *table, int newsize)
return;
}
- table->ents = fz_calloc(newsize, sizeof(fz_hash_entry));
+ table->ents = fz_calloc(ctx, newsize, sizeof(fz_hash_entry));
memset(table->ents, 0, sizeof(fz_hash_entry) * newsize);
table->size = newsize;
table->load = 0;
@@ -113,11 +112,11 @@ fz_resize_hash(fz_hash_table *table, int newsize)
{
if (oldents[i].val)
{
- fz_hash_insert(table, oldents[i].key, oldents[i].val);
+ fz_hash_insert(ctx, table, oldents[i].key, oldents[i].val);
}
}
- fz_free(oldents);
+ fz_free(ctx, oldents);
}
void *
@@ -140,7 +139,7 @@ fz_hash_find(fz_hash_table *table, void *key)
}
void
-fz_hash_insert(fz_hash_table *table, void *key, void *val)
+fz_hash_insert(fz_context *ctx, fz_hash_table *table, void *key, void *val)
{
fz_hash_entry *ents;
unsigned size;
@@ -148,7 +147,7 @@ fz_hash_insert(fz_hash_table *table, void *key, void *val)
if (table->load > table->size * 8 / 10)
{
- fz_resize_hash(table, table->size * 2);
+ fz_resize_hash(ctx, table, table->size * 2);
}
ents = table->ents;
diff --git a/fitz/base_memory.c b/fitz/base_memory.c
index f1b668eb..9847aa76 100644
--- a/fitz/base_memory.c
+++ b/fitz/base_memory.c
@@ -1,77 +1,146 @@
#include "fitz.h"
+#include "except.h"
void *
-fz_malloc(int size)
+fz_malloc(fz_context *ctx, size_t size)
{
- void *p = malloc(size);
+ void *p;
+ fz_alloc_context *alloc;
+
+ assert(ctx != NULL);
+ alloc = ctx->alloc;
+ assert(alloc != NULL);
+ p = alloc->malloc(alloc->opaque, size);
if (!p)
{
- fprintf(stderr, "fatal error: out of memory\n");
- abort();
+ fz_throw(ctx, "malloc failed (%d bytes)", size);
}
return p;
}
-void *
-fz_calloc(int count, int size)
+void *fz_calloc(fz_context *ctx, size_t count, size_t size)
{
void *p;
+ fz_alloc_context *alloc;
- if (count == 0 || size == 0)
- return 0;
-
- if (count < 0 || size < 0 || count > INT_MAX / size)
+ assert(ctx != NULL);
+ alloc = ctx->alloc;
+ assert(alloc != NULL);
+ p = alloc->calloc(alloc->opaque, count, size);
+ if (!p)
{
- fprintf(stderr, "fatal error: out of memory (integer overflow)\n");
- abort();
+ fz_throw(ctx, "calloc failed (%d x %d bytes)", count, size);
}
+ return p;
+}
+
+void *
+fz_realloc(fz_context *ctx, void *p, size_t size)
+{
+ fz_alloc_context *alloc;
- p = malloc(count * size);
+ assert(ctx != NULL);
+ alloc = ctx->alloc;
+ assert(alloc != NULL);
+ p = alloc->realloc(alloc->opaque, p, size);
if (!p)
{
- fprintf(stderr, "fatal error: out of memory\n");
- abort();
+ fz_throw(ctx, "realloc failed (%d bytes)", size);
}
return p;
}
+void
+fz_free(fz_context *ctx, void *p)
+{
+ fz_alloc_context *alloc;
+
+ assert(ctx != NULL);
+ alloc = ctx->alloc;
+ assert(alloc != NULL);
+ alloc->free(alloc->opaque, p);
+}
+
void *
-fz_realloc(void *p, int count, int size)
+fz_malloc_nothrow(fz_context *ctx, size_t size)
{
- void *np;
+ fz_alloc_context *alloc;
- if (count == 0 || size == 0)
- {
- fz_free(p);
- return 0;
- }
+ assert(ctx != NULL);
+ alloc = ctx->alloc;
+ assert(alloc != NULL);
+ return alloc->malloc(alloc->opaque, size);
+}
- if (count < 0 || size < 0 || count > INT_MAX / size)
- {
- fprintf(stderr, "fatal error: out of memory (integer overflow)\n");
- abort();
- }
+void *fz_calloc_nothrow(fz_context *ctx, size_t count, size_t size)
+{
+ fz_alloc_context *alloc;
- np = realloc(p, count * size);
- if (np == NULL)
- {
- fprintf(stderr, "fatal error: out of memory\n");
- abort();
- }
- return np;
+ assert(ctx != NULL);
+ alloc = ctx->alloc;
+ assert(alloc != NULL);
+ return alloc->calloc(alloc->opaque, count, size);
+}
+
+void *
+fz_realloc_nothrow(fz_context *ctx, void *p, size_t size)
+{
+ fz_alloc_context *alloc;
+
+ assert(ctx != NULL);
+ alloc = ctx->alloc;
+ assert(alloc != NULL);
+ return alloc->realloc(alloc->opaque, p, size);
+}
+
+void *
+fz_malloc_default(void *opaque, size_t size)
+{
+ return malloc(size);
+}
+
+void *
+fz_calloc_default(void *opaque, size_t count, size_t size)
+{
+ return calloc(count, size);
+}
+
+void *
+fz_realloc_default(void *opaque, void *p, size_t size)
+{
+ return realloc(p, size);
}
void
-fz_free(void *p)
+fz_free_default(void *opaque, void *p)
{
free(p);
}
+fz_alloc_context fz_alloc_default =
+{
+ (void *)-1,
+ fz_malloc_default,
+ fz_realloc_default,
+ fz_free_default,
+ fz_calloc_default
+};
+
char *
-fz_strdup(char *s)
+fz_strdup(fz_context *ctx, char *s)
{
int len = strlen(s) + 1;
- char *ns = fz_malloc(len);
+ char *ns = fz_malloc(ctx, len);
memcpy(ns, s, len);
return ns;
}
+
+char *
+fz_strdup_nothrow(fz_context *ctx, char *s)
+{
+ int len = strlen(s) + 1;
+ char *ns = fz_malloc_nothrow(ctx, len);
+ if (ns != NULL)
+ memcpy(ns, s, len);
+ return ns;
+}
diff --git a/fitz/base_object.c b/fitz/base_object.c
index 4a23e5ab..5d10d860 100644
--- a/fitz/base_object.c
+++ b/fitz/base_object.c
@@ -1,5 +1,9 @@
#include "fitz.h"
+#if defined(_WIN32) || defined(_WIN64)
+#define MISSING_QSORT_R
+#endif
+
typedef enum fz_objkind_e
{
FZ_NULL,
@@ -52,26 +56,19 @@ struct fz_obj_s
} u;
};
-static fz_obj *fz_resolve_indirect_null(fz_obj *ref)
-{
- return ref;
-}
-
-fz_obj* (*fz_resolve_indirect)(fz_obj*) = fz_resolve_indirect_null;
-
fz_obj *
-fz_new_null(void)
+fz_new_null(fz_context *ctx)
{
- fz_obj *obj = fz_malloc(sizeof(fz_obj));
+ fz_obj *obj = fz_malloc(ctx, sizeof(fz_obj));
obj->refs = 1;
obj->kind = FZ_NULL;
return obj;
}
fz_obj *
-fz_new_bool(int b)
+fz_new_bool(fz_context *ctx, int b)
{
- fz_obj *obj = fz_malloc(sizeof(fz_obj));
+ fz_obj *obj = fz_malloc(ctx, sizeof(fz_obj));
obj->refs = 1;
obj->kind = FZ_BOOL;
obj->u.b = b;
@@ -79,9 +76,9 @@ fz_new_bool(int b)
}
fz_obj *
-fz_new_int(int i)
+fz_new_int(fz_context *ctx, int i)
{
- fz_obj *obj = fz_malloc(sizeof(fz_obj));
+ fz_obj *obj = fz_malloc(ctx, sizeof(fz_obj));
obj->refs = 1;
obj->kind = FZ_INT;
obj->u.i = i;
@@ -89,9 +86,9 @@ fz_new_int(int i)
}
fz_obj *
-fz_new_real(float f)
+fz_new_real(fz_context *ctx, float f)
{
- fz_obj *obj = fz_malloc(sizeof(fz_obj));
+ fz_obj *obj = fz_malloc(ctx, sizeof(fz_obj));
obj->refs = 1;
obj->kind = FZ_REAL;
obj->u.f = f;
@@ -99,9 +96,9 @@ fz_new_real(float f)
}
fz_obj *
-fz_new_string(char *str, int len)
+fz_new_string(fz_context *ctx, char *str, int len)
{
- fz_obj *obj = fz_malloc(offsetof(fz_obj, u.s.buf) + len + 1);
+ fz_obj *obj = fz_malloc(ctx, offsetof(fz_obj, u.s.buf) + len + 1);
obj->refs = 1;
obj->kind = FZ_STRING;
obj->u.s.len = len;
@@ -111,9 +108,9 @@ fz_new_string(char *str, int len)
}
fz_obj *
-fz_new_name(char *str)
+fz_new_name(fz_context *ctx, char *str)
{
- fz_obj *obj = fz_malloc(offsetof(fz_obj, u.n) + strlen(str) + 1);
+ fz_obj *obj = fz_malloc(ctx, offsetof(fz_obj, u.n) + strlen(str) + 1);
obj->refs = 1;
obj->kind = FZ_NAME;
strcpy(obj->u.n, str);
@@ -121,9 +118,9 @@ fz_new_name(char *str)
}
fz_obj *
-fz_new_indirect(int num, int gen, void *xref)
+fz_new_indirect(fz_context *ctx, int num, int gen, void *xref)
{
- fz_obj *obj = fz_malloc(sizeof(fz_obj));
+ fz_obj *obj = fz_malloc(ctx, sizeof(fz_obj));
obj->refs = 1;
obj->kind = FZ_INDIRECT;
obj->u.r.num = num;
@@ -145,111 +142,111 @@ int fz_is_indirect(fz_obj *obj)
return obj ? obj->kind == FZ_INDIRECT : 0;
}
-int fz_is_null(fz_obj *obj)
+int fz_is_null(fz_context *ctx, fz_obj *obj)
{
- obj = fz_resolve_indirect(obj);
+ obj = fz_resolve_indirect(ctx, obj);
return obj ? obj->kind == FZ_NULL : 0;
}
-int fz_is_bool(fz_obj *obj)
+int fz_is_bool(fz_context *ctx, fz_obj *obj)
{
- obj = fz_resolve_indirect(obj);
+ obj = fz_resolve_indirect(ctx, obj);
return obj ? obj->kind == FZ_BOOL : 0;
}
-int fz_is_int(fz_obj *obj)
+int fz_is_int(fz_context *ctx, fz_obj *obj)
{
- obj = fz_resolve_indirect(obj);
+ obj = fz_resolve_indirect(ctx, obj);
return obj ? obj->kind == FZ_INT : 0;
}
-int fz_is_real(fz_obj *obj)
+int fz_is_real(fz_context *ctx, fz_obj *obj)
{
- obj = fz_resolve_indirect(obj);
+ obj = fz_resolve_indirect(ctx, obj);
return obj ? obj->kind == FZ_REAL : 0;
}
-int fz_is_string(fz_obj *obj)
+int fz_is_string(fz_context *ctx, fz_obj *obj)
{
- obj = fz_resolve_indirect(obj);
+ obj = fz_resolve_indirect(ctx, obj);
return obj ? obj->kind == FZ_STRING : 0;
}
-int fz_is_name(fz_obj *obj)
+int fz_is_name(fz_context *ctx, fz_obj *obj)
{
- obj = fz_resolve_indirect(obj);
+ obj = fz_resolve_indirect(ctx, obj);
return obj ? obj->kind == FZ_NAME : 0;
}
-int fz_is_array(fz_obj *obj)
+int fz_is_array(fz_context *ctx, fz_obj *obj)
{
- obj = fz_resolve_indirect(obj);
+ obj = fz_resolve_indirect(ctx, obj);
return obj ? obj->kind == FZ_ARRAY : 0;
}
-int fz_is_dict(fz_obj *obj)
+int fz_is_dict(fz_context *ctx, fz_obj *obj)
{
- obj = fz_resolve_indirect(obj);
+ obj = fz_resolve_indirect(ctx, obj);
return obj ? obj->kind == FZ_DICT : 0;
}
-int fz_to_bool(fz_obj *obj)
+int fz_to_bool(fz_context *ctx, fz_obj *obj)
{
- obj = fz_resolve_indirect(obj);
- if (fz_is_bool(obj))
+ obj = fz_resolve_indirect(ctx, obj);
+ if (fz_is_bool(ctx, obj))
return obj->u.b;
return 0;
}
-int fz_to_int(fz_obj *obj)
+int fz_to_int(fz_context *ctx, fz_obj *obj)
{
- obj = fz_resolve_indirect(obj);
- if (fz_is_int(obj))
+ obj = fz_resolve_indirect(ctx, obj);
+ if (fz_is_int(ctx, obj))
return obj->u.i;
- if (fz_is_real(obj))
+ if (fz_is_real(ctx, obj))
return obj->u.f;
return 0;
}
-float fz_to_real(fz_obj *obj)
+float fz_to_real(fz_context *ctx, fz_obj *obj)
{
- obj = fz_resolve_indirect(obj);
- if (fz_is_real(obj))
+ obj = fz_resolve_indirect(ctx, obj);
+ if (fz_is_real(ctx, obj))
return obj->u.f;
- if (fz_is_int(obj))
+ if (fz_is_int(ctx, obj))
return obj->u.i;
return 0;
}
-char *fz_to_name(fz_obj *obj)
+char *fz_to_name(fz_context *ctx, fz_obj *obj)
{
- obj = fz_resolve_indirect(obj);
- if (fz_is_name(obj))
+ obj = fz_resolve_indirect(ctx, obj);
+ if (fz_is_name(ctx, obj))
return obj->u.n;
return "";
}
-char *fz_to_str_buf(fz_obj *obj)
+char *fz_to_str_buf(fz_context *ctx, fz_obj *obj)
{
- obj = fz_resolve_indirect(obj);
- if (fz_is_string(obj))
+ obj = fz_resolve_indirect(ctx, obj);
+ if (fz_is_string(ctx, obj))
return obj->u.s.buf;
return "";
}
-int fz_to_str_len(fz_obj *obj)
+int fz_to_str_len(fz_context *ctx, fz_obj *obj)
{
- obj = fz_resolve_indirect(obj);
- if (fz_is_string(obj))
+ obj = fz_resolve_indirect(ctx, obj);
+ if (fz_is_string(ctx, obj))
return obj->u.s.len;
return 0;
}
/* for use by pdf_crypt_obj_imp to decrypt AES string in place */
-void fz_set_str_len(fz_obj *obj, int newlen)
+void fz_set_str_len(fz_context *ctx, fz_obj *obj, int newlen)
{
- obj = fz_resolve_indirect(obj);
- if (fz_is_string(obj))
+ obj = fz_resolve_indirect(ctx, obj);
+ if (fz_is_string(ctx, obj))
if (newlen < obj->u.s.len)
obj->u.s.len = newlen;
}
@@ -375,19 +372,19 @@ fz_objkindstr(fz_obj *obj)
}
fz_obj *
-fz_new_array(int initialcap)
+fz_new_array(fz_context *ctx, int initialcap)
{
fz_obj *obj;
int i;
- obj = fz_malloc(sizeof(fz_obj));
+ obj = fz_malloc(ctx, sizeof(fz_obj));
obj->refs = 1;
obj->kind = FZ_ARRAY;
obj->u.a.len = 0;
obj->u.a.cap = initialcap > 1 ? initialcap : 6;
- obj->u.a.items = fz_calloc(obj->u.a.cap, sizeof(fz_obj*));
+ obj->u.a.items = fz_calloc(ctx, obj->u.a.cap, sizeof(fz_obj*));
for (i = 0; i < obj->u.a.cap; i++)
obj->u.a.items[i] = NULL;
@@ -395,36 +392,38 @@ fz_new_array(int initialcap)
}
fz_obj *
-fz_copy_array(fz_obj *obj)
+fz_copy_array(fz_context *ctx, fz_obj *obj)
{
fz_obj *new;
int i;
+ int n;
- if (fz_is_indirect(obj) || !fz_is_array(obj))
+ if (fz_is_indirect(obj) || !fz_is_array(ctx, obj))
fz_warn("assert: not an array (%s)", fz_objkindstr(obj));
- new = fz_new_array(fz_array_len(obj));
- for (i = 0; i < fz_array_len(obj); i++)
- fz_array_push(new, fz_array_get(obj, i));
+ new = fz_new_array(ctx, fz_array_len(ctx, obj));
+ n = fz_array_len(ctx, obj);
+ for (i = 0; i < n; i++)
+ fz_array_push(ctx, new, fz_array_get(ctx, obj, i));
return new;
}
int
-fz_array_len(fz_obj *obj)
+fz_array_len(fz_context *ctx, fz_obj *obj)
{
- obj = fz_resolve_indirect(obj);
- if (!fz_is_array(obj))
+ obj = fz_resolve_indirect(ctx, obj);
+ if (!fz_is_array(ctx, obj))
return 0;
return obj->u.a.len;
}
fz_obj *
-fz_array_get(fz_obj *obj, int i)
+fz_array_get(fz_context *ctx, fz_obj *obj, int i)
{
- obj = fz_resolve_indirect(obj);
+ obj = fz_resolve_indirect(ctx, obj);
- if (!fz_is_array(obj))
+ if (!fz_is_array(ctx, obj))
return NULL;
if (i < 0 || i >= obj->u.a.len)
@@ -434,11 +433,11 @@ fz_array_get(fz_obj *obj, int i)
}
void
-fz_array_put(fz_obj *obj, int i, fz_obj *item)
+fz_array_put(fz_context *ctx, fz_obj *obj, int i, fz_obj *item)
{
- obj = fz_resolve_indirect(obj);
+ obj = fz_resolve_indirect(ctx, obj);
- if (!fz_is_array(obj))
+ if (!fz_is_array(ctx, obj))
fz_warn("assert: not an array (%s)", fz_objkindstr(obj));
else if (i < 0)
fz_warn("assert: index %d < 0", i);
@@ -447,17 +446,17 @@ fz_array_put(fz_obj *obj, int i, fz_obj *item)
else
{
if (obj->u.a.items[i])
- fz_drop_obj(obj->u.a.items[i]);
+ fz_drop_obj(ctx, obj->u.a.items[i]);
obj->u.a.items[i] = fz_keep_obj(item);
}
}
void
-fz_array_push(fz_obj *obj, fz_obj *item)
+fz_array_push(fz_context *ctx, fz_obj *obj, fz_obj *item)
{
- obj = fz_resolve_indirect(obj);
+ obj = fz_resolve_indirect(ctx, obj);
- if (!fz_is_array(obj))
+ if (!fz_is_array(ctx, obj))
fz_warn("assert: not an array (%s)", fz_objkindstr(obj));
else
{
@@ -465,7 +464,7 @@ fz_array_push(fz_obj *obj, fz_obj *item)
{
int i;
obj->u.a.cap = (obj->u.a.cap * 3) / 2;
- obj->u.a.items = fz_realloc(obj->u.a.items, obj->u.a.cap, sizeof(fz_obj*));
+ obj->u.a.items = fz_realloc(ctx, obj->u.a.items, obj->u.a.cap * sizeof(fz_obj*));
for (i = obj->u.a.len ; i < obj->u.a.cap; i++)
obj->u.a.items[i] = NULL;
}
@@ -475,11 +474,11 @@ fz_array_push(fz_obj *obj, fz_obj *item)
}
void
-fz_array_insert(fz_obj *obj, fz_obj *item)
+fz_array_insert(fz_context *ctx, fz_obj *obj, fz_obj *item)
{
- obj = fz_resolve_indirect(obj);
+ obj = fz_resolve_indirect(ctx, obj);
- if (!fz_is_array(obj))
+ if (!fz_is_array(ctx, obj))
fz_warn("assert: not an array (%s)", fz_objkindstr(obj));
else
{
@@ -487,7 +486,7 @@ fz_array_insert(fz_obj *obj, fz_obj *item)
{
int i;
obj->u.a.cap = (obj->u.a.cap * 3) / 2;
- obj->u.a.items = fz_realloc(obj->u.a.items, obj->u.a.cap, sizeof(fz_obj*));
+ obj->u.a.items = fz_realloc(ctx, obj->u.a.items, obj->u.a.cap * sizeof(fz_obj*));
for (i = obj->u.a.len ; i < obj->u.a.cap; i++)
obj->u.a.items[i] = NULL;
}
@@ -499,20 +498,29 @@ fz_array_insert(fz_obj *obj, fz_obj *item)
/* dicts may only have names as keys! */
-static int keyvalcmp(const void *ap, const void *bp)
+static int keyvalcmp(void *ctxp, const void *ap, const void *bp)
{
+ fz_context *ctx = (fz_context *)ctxp;
const struct keyval *a = ap;
const struct keyval *b = bp;
- return strcmp(fz_to_name(a->k), fz_to_name(b->k));
+ return strcmp(fz_to_name(ctx, a->k), fz_to_name(ctx, b->k));
}
+#ifdef MISSING_QSORT_R
+static void *qsort_r_hack;
+static int keyvalcmp_hack(const void *ap, const void *bp)
+{
+ return keyvalcmp(qsort_r_hack, ap, bp);
+}
+#endif
+
fz_obj *
-fz_new_dict(int initialcap)
+fz_new_dict(fz_context *ctx, int initialcap)
{
fz_obj *obj;
int i;
- obj = fz_malloc(sizeof(fz_obj));
+ obj = fz_malloc(ctx, sizeof(fz_obj));
obj->refs = 1;
obj->kind = FZ_DICT;
@@ -520,7 +528,7 @@ fz_new_dict(int initialcap)
obj->u.d.len = 0;
obj->u.d.cap = initialcap > 1 ? initialcap : 10;
- obj->u.d.items = fz_calloc(obj->u.d.cap, sizeof(struct keyval));
+ obj->u.d.items = fz_calloc(ctx, obj->u.d.cap, sizeof(struct keyval));
for (i = 0; i < obj->u.d.cap; i++)
{
obj->u.d.items[i].k = NULL;
@@ -531,36 +539,37 @@ fz_new_dict(int initialcap)
}
fz_obj *
-fz_copy_dict(fz_obj *obj)
+fz_copy_dict(fz_context *ctx, fz_obj *obj)
{
fz_obj *new;
- int i;
+ int i, n;
- if (fz_is_indirect(obj) || !fz_is_dict(obj))
+ if (fz_is_indirect(obj) || !fz_is_dict(ctx, obj))
fz_error_make("assert: not a dict (%s)", fz_objkindstr(obj));
- new = fz_new_dict(fz_dict_len(obj));
- for (i = 0; i < fz_dict_len(obj); i++)
- fz_dict_put(new, fz_dict_get_key(obj, i), fz_dict_get_val(obj, i));
+ n = fz_dict_len(ctx, obj);
+ new = fz_new_dict(ctx, n);
+ for (i = 0; i < n; i++)
+ fz_dict_put(ctx, new, fz_dict_get_key(ctx, obj, i), fz_dict_get_val(ctx, obj, i));
return new;
}
int
-fz_dict_len(fz_obj *obj)
+fz_dict_len(fz_context *ctx, fz_obj *obj)
{
- obj = fz_resolve_indirect(obj);
- if (!fz_is_dict(obj))
+ obj = fz_resolve_indirect(ctx, obj);
+ if (!fz_is_dict(ctx, obj))
return 0;
return obj->u.d.len;
}
fz_obj *
-fz_dict_get_key(fz_obj *obj, int i)
+fz_dict_get_key(fz_context *ctx, fz_obj *obj, int i)
{
- obj = fz_resolve_indirect(obj);
+ obj = fz_resolve_indirect(ctx, obj);
- if (!fz_is_dict(obj))
+ if (!fz_is_dict(ctx, obj))
return NULL;
if (i < 0 || i >= obj->u.d.len)
@@ -570,11 +579,11 @@ fz_dict_get_key(fz_obj *obj, int i)
}
fz_obj *
-fz_dict_get_val(fz_obj *obj, int i)
+fz_dict_get_val(fz_context *ctx, fz_obj *obj, int i)
{
- obj = fz_resolve_indirect(obj);
+ obj = fz_resolve_indirect(ctx, obj);
- if (!fz_is_dict(obj))
+ if (!fz_is_dict(ctx, obj))
return NULL;
if (i < 0 || i >= obj->u.d.len)
@@ -584,7 +593,7 @@ fz_dict_get_val(fz_obj *obj, int i)
}
static int
-fz_dict_finds(fz_obj *obj, char *key)
+fz_dict_finds(fz_context *ctx, fz_obj *obj, char *key)
{
if (obj->u.d.sorted)
{
@@ -593,7 +602,7 @@ fz_dict_finds(fz_obj *obj, char *key)
while (l <= r)
{
int m = (l + r) >> 1;
- int c = -strcmp(fz_to_name(obj->u.d.items[m].k), key);
+ int c = -strcmp(fz_to_name(ctx, obj->u.d.items[m].k), key);
if (c < 0)
r = m - 1;
else if (c > 0)
@@ -607,7 +616,7 @@ fz_dict_finds(fz_obj *obj, char *key)
{
int i;
for (i = 0; i < obj->u.d.len; i++)
- if (strcmp(fz_to_name(obj->u.d.items[i].k), key) == 0)
+ if (strcmp(fz_to_name(ctx, obj->u.d.items[i].k), key) == 0)
return i;
}
@@ -615,16 +624,16 @@ fz_dict_finds(fz_obj *obj, char *key)
}
fz_obj *
-fz_dict_gets(fz_obj *obj, char *key)
+fz_dict_gets(fz_context *ctx, fz_obj *obj, char *key)
{
int i;
- obj = fz_resolve_indirect(obj);
+ obj = fz_resolve_indirect(ctx, obj);
- if (!fz_is_dict(obj))
+ if (!fz_is_dict(ctx, obj))
return NULL;
- i = fz_dict_finds(obj, key);
+ i = fz_dict_finds(ctx, obj, key);
if (i >= 0)
return obj->u.d.items[i].v;
@@ -632,39 +641,39 @@ fz_dict_gets(fz_obj *obj, char *key)
}
fz_obj *
-fz_dict_get(fz_obj *obj, fz_obj *key)
+fz_dict_get(fz_context *ctx, fz_obj *obj, fz_obj *key)
{
- if (fz_is_name(key))
- return fz_dict_gets(obj, fz_to_name(key));
+ if (fz_is_name(ctx, key))
+ return fz_dict_gets(ctx, obj, fz_to_name(ctx, key));
return NULL;
}
fz_obj *
-fz_dict_getsa(fz_obj *obj, char *key, char *abbrev)
+fz_dict_getsa(fz_context *ctx, fz_obj *obj, char *key, char *abbrev)
{
fz_obj *v;
- v = fz_dict_gets(obj, key);
+ v = fz_dict_gets(ctx, obj, key);
if (v)
return v;
- return fz_dict_gets(obj, abbrev);
+ return fz_dict_gets(ctx, obj, abbrev);
}
void
-fz_dict_put(fz_obj *obj, fz_obj *key, fz_obj *val)
+fz_dict_put(fz_context *ctx, fz_obj *obj, fz_obj *key, fz_obj *val)
{
char *s;
int i;
- obj = fz_resolve_indirect(obj);
+ obj = fz_resolve_indirect(ctx, obj);
- if (!fz_is_dict(obj))
+ if (!fz_is_dict(ctx, obj))
{
fz_warn("assert: not a dict (%s)", fz_objkindstr(obj));
return;
}
- if (fz_is_name(key))
- s = fz_to_name(key);
+ if (fz_is_name(ctx, key))
+ s = fz_to_name(ctx, key);
else
{
fz_warn("assert: key is not a name (%s)", fz_objkindstr(obj));
@@ -677,10 +686,10 @@ fz_dict_put(fz_obj *obj, fz_obj *key, fz_obj *val)
return;
}
- i = fz_dict_finds(obj, s);
+ i = fz_dict_finds(ctx, obj, s);
if (i >= 0)
{
- fz_drop_obj(obj->u.d.items[i].v);
+ fz_drop_obj(ctx, obj->u.d.items[i].v);
obj->u.d.items[i].v = fz_keep_obj(val);
return;
}
@@ -688,7 +697,7 @@ fz_dict_put(fz_obj *obj, fz_obj *key, fz_obj *val)
if (obj->u.d.len + 1 > obj->u.d.cap)
{
obj->u.d.cap = (obj->u.d.cap * 3) / 2;
- obj->u.d.items = fz_realloc(obj->u.d.items, obj->u.d.cap, sizeof(struct keyval));
+ obj->u.d.items = fz_realloc(ctx, obj->u.d.items, obj->u.d.cap * sizeof(struct keyval));
for (i = obj->u.d.len; i < obj->u.d.cap; i++)
{
obj->u.d.items[i].k = NULL;
@@ -698,7 +707,7 @@ fz_dict_put(fz_obj *obj, fz_obj *key, fz_obj *val)
/* borked! */
if (obj->u.d.len)
- if (strcmp(fz_to_name(obj->u.d.items[obj->u.d.len - 1].k), s) > 0)
+ if (strcmp(fz_to_name(ctx, obj->u.d.items[obj->u.d.len - 1].k), s) > 0)
obj->u.d.sorted = 0;
obj->u.d.items[obj->u.d.len].k = fz_keep_obj(key);
@@ -707,27 +716,27 @@ fz_dict_put(fz_obj *obj, fz_obj *key, fz_obj *val)
}
void
-fz_dict_puts(fz_obj *obj, char *key, fz_obj *val)
+fz_dict_puts(fz_context *ctx, fz_obj *obj, char *key, fz_obj *val)
{
- fz_obj *keyobj = fz_new_name(key);
- fz_dict_put(obj, keyobj, val);
- fz_drop_obj(keyobj);
+ fz_obj *keyobj = fz_new_name(ctx, key);
+ fz_dict_put(ctx, obj, keyobj, val);
+ fz_drop_obj(ctx, keyobj);
}
void
-fz_dict_dels(fz_obj *obj, char *key)
+fz_dict_dels(fz_context *ctx, fz_obj *obj, char *key)
{
- obj = fz_resolve_indirect(obj);
+ obj = fz_resolve_indirect(ctx, obj);
- if (!fz_is_dict(obj))
+ if (!fz_is_dict(ctx, obj))
fz_warn("assert: not a dict (%s)", fz_objkindstr(obj));
else
{
- int i = fz_dict_finds(obj, key);
+ int i = fz_dict_finds(ctx, obj, key);
if (i >= 0)
{
- fz_drop_obj(obj->u.d.items[i].k);
- fz_drop_obj(obj->u.d.items[i].v);
+ fz_drop_obj(ctx, obj->u.d.items[i].k);
+ fz_drop_obj(ctx, obj->u.d.items[i].v);
obj->u.d.sorted = 0;
obj->u.d.items[i] = obj->u.d.items[obj->u.d.len-1];
obj->u.d.len --;
@@ -736,67 +745,72 @@ fz_dict_dels(fz_obj *obj, char *key)
}
void
-fz_dict_del(fz_obj *obj, fz_obj *key)
+fz_dict_del(fz_context *ctx, fz_obj *obj, fz_obj *key)
{
- if (fz_is_name(key))
- fz_dict_dels(obj, fz_to_name(key));
+ if (fz_is_name(ctx, key))
+ fz_dict_dels(ctx, obj, fz_to_name(ctx, key));
else
fz_warn("assert: key is not a name (%s)", fz_objkindstr(obj));
}
void
-fz_sort_dict(fz_obj *obj)
+fz_sort_dict(fz_context *ctx, fz_obj *obj)
{
- obj = fz_resolve_indirect(obj);
- if (!fz_is_dict(obj))
+ obj = fz_resolve_indirect(ctx, obj);
+ if (!fz_is_dict(ctx, obj))
return;
if (!obj->u.d.sorted)
{
- qsort(obj->u.d.items, obj->u.d.len, sizeof(struct keyval), keyvalcmp);
- obj->u.d.sorted = 1;
+#ifdef MISSING_QSORT_R
+ qsort_r_hack = ctx;
+ qsort(obj->u.d.items, obj->u.d.len, sizeof(struct keyval), keyvalcmp_hack);
+#else
+ qsort_r(obj->u.d.items, obj->u.d.len, sizeof(struct keyval), ctx, keyvalcmp);
+#endif
+ obj->u.d.sorted = 1;
}
}
static void
-fz_free_array(fz_obj *obj)
+fz_free_array(fz_context *ctx, fz_obj *obj)
{
int i;
for (i = 0; i < obj->u.a.len; i++)
if (obj->u.a.items[i])
- fz_drop_obj(obj->u.a.items[i]);
+ fz_drop_obj(ctx, obj->u.a.items[i]);
- fz_free(obj->u.a.items);
- fz_free(obj);
+ fz_free(ctx, obj->u.a.items);
+ fz_free(ctx, obj);
}
static void
-fz_free_dict(fz_obj *obj)
+fz_free_dict(fz_context *ctx, fz_obj *obj)
{
int i;
for (i = 0; i < obj->u.d.len; i++) {
if (obj->u.d.items[i].k)
- fz_drop_obj(obj->u.d.items[i].k);
+ fz_drop_obj(ctx, obj->u.d.items[i].k);
if (obj->u.d.items[i].v)
- fz_drop_obj(obj->u.d.items[i].v);
+ fz_drop_obj(ctx, obj->u.d.items[i].v);
}
- fz_free(obj->u.d.items);
- fz_free(obj);
+ fz_free(ctx, obj->u.d.items);
+ fz_free(ctx, obj);
}
void
-fz_drop_obj(fz_obj *obj)
+fz_drop_obj(fz_context *ctx, fz_obj *obj)
{
assert(obj != NULL);
if (--obj->refs == 0)
{
if (obj->kind == FZ_ARRAY)
- fz_free_array(obj);
+ fz_free_array(ctx, obj);
else if (obj->kind == FZ_DICT)
- fz_free_dict(obj);
+ fz_free_dict(ctx, obj);
else
- fz_free(obj);
+ fz_free(ctx, obj);
}
}
diff --git a/fitz/context.c b/fitz/context.c
new file mode 100644
index 00000000..aee521b6
--- /dev/null
+++ b/fitz/context.c
@@ -0,0 +1,63 @@
+#include "fitz.h"
+#include "except.h"
+
+void fz_context_fin(fz_context *ctx)
+{
+ assert(ctx != NULL);
+
+ /* Other finalisation calls go here (in reverse order) */
+ fz_except_fin(ctx);
+ /* Free the context itself */
+ ctx->alloc->free(ctx->alloc->opaque, ctx);
+ /* We do NOT free the allocator! */
+}
+
+fz_obj *fz_resolve_indirect_null(fz_obj *ref)
+{
+ return ref;
+}
+
+fz_context *fz_context_init(fz_alloc_context *alloc)
+{
+ fz_context *ctx;
+ fz_error error;
+
+ assert(alloc != NULL);
+ ctx = alloc->calloc(alloc->opaque, 1, sizeof(fz_context));
+ if (ctx == NULL)
+ return NULL;
+ ctx->alloc = alloc;
+
+ error = fz_except_init(ctx);
+ if (error != fz_okay)
+ goto cleanup;
+ ctx->fz_resolve_indirect = fz_resolve_indirect_null;
+
+ /* New initialisation calls for context entries go here */
+ return ctx;
+ cleanup:
+ fz_context_fin(ctx);
+ fz_error_handle(error, "fz_context_init failed");
+ return NULL;
+}
+
+fz_context *fz_context_clone(fz_context *ctx)
+{
+ fz_context *clone;
+ fz_error error;
+
+ clone = ctx->alloc->calloc(ctx->alloc->opaque, 1, sizeof(*clone));
+ if (clone == NULL)
+ return NULL;
+ clone->alloc = ctx->alloc;
+ error = fz_except_init(ctx);
+ if (error != fz_okay)
+ goto cleanup;
+ /* Other initialisations go here; either a copy (probably refcounted)
+ * or a new initialisation. */
+ return clone;
+ cleanup:
+ fz_context_fin(ctx);
+ fz_error_handle(error, "fz_context_clone failed");
+ return NULL;
+}
diff --git a/fitz/crypt_arc4.c b/fitz/crypt_arc4.c
index 72871386..272891ce 100644
--- a/fitz/crypt_arc4.c
+++ b/fitz/crypt_arc4.c
@@ -24,7 +24,7 @@
#include "fitz.h"
void
-fz_arc4_init(fz_arc4 *arc4, const unsigned char *key, const unsigned keylen)
+fz_arc4_init(fz_arc4 *arc4, const unsigned char *key, unsigned keylen)
{
unsigned int t, u;
unsigned int keyindex;
@@ -86,7 +86,7 @@ fz_arc4_next(fz_arc4 *arc4)
}
void
-fz_arc4_encrypt(fz_arc4 *arc4, unsigned char *dest, const unsigned char *src, const unsigned len)
+fz_arc4_encrypt(fz_arc4 *arc4, unsigned char *dest, const unsigned char *src, unsigned len)
{
unsigned int i;
for (i = 0; i < len; i++)
diff --git a/fitz/crypt_md5.c b/fitz/crypt_md5.c
index ba2571c4..b6e06845 100644
--- a/fitz/crypt_md5.c
+++ b/fitz/crypt_md5.c
@@ -210,7 +210,7 @@ void fz_md5_init(fz_md5 *context)
/* MD5 block update operation. Continues an MD5 message-digest operation,
* processing another message block, and updating the context.
*/
-void fz_md5_update(fz_md5 *context, const unsigned char *input, const unsigned inlen)
+void fz_md5_update(fz_md5 *context, const unsigned char *input, unsigned inlen)
{
unsigned i, index, partlen;
diff --git a/fitz/dev_bbox.c b/fitz/dev_bbox.c
index 179ff030..e83cbcc5 100644
--- a/fitz/dev_bbox.c
+++ b/fitz/dev_bbox.c
@@ -3,70 +3,70 @@
/* TODO: add clip stack and use to intersect bboxes */
static void
-fz_bbox_fill_path(void *user, fz_path *path, int even_odd, fz_matrix ctm,
+fz_bbox_fill_path(fz_device *dev, fz_path *path, int even_odd, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
- fz_bbox *result = user;
+ fz_bbox *result = dev->user;
fz_bbox bbox = fz_round_rect(fz_bound_path(path, NULL, ctm));
*result = fz_union_bbox(*result, bbox);
}
static void
-fz_bbox_stroke_path(void *user, fz_path *path, fz_stroke_state *stroke, fz_matrix ctm,
+fz_bbox_stroke_path(fz_device *dev, fz_path *path, fz_stroke_state *stroke, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
- fz_bbox *result = user;
+ fz_bbox *result = dev->user;
fz_bbox bbox = fz_round_rect(fz_bound_path(path, stroke, ctm));
*result = fz_union_bbox(*result, bbox);
}
static void
-fz_bbox_fill_text(void *user, fz_text *text, fz_matrix ctm,
+fz_bbox_fill_text(fz_device *dev, fz_text *text, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
- fz_bbox *result = user;
+ fz_bbox *result = dev->user;
fz_bbox bbox = fz_round_rect(fz_bound_text(text, ctm));
*result = fz_union_bbox(*result, bbox);
}
static void
-fz_bbox_stroke_text(void *user, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm,
+fz_bbox_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
- fz_bbox *result = user;
+ fz_bbox *result = dev->user;
fz_bbox bbox = fz_round_rect(fz_bound_text(text, ctm));
*result = fz_union_bbox(*result, bbox);
}
static void
-fz_bbox_fill_shade(void *user, fz_shade *shade, fz_matrix ctm, float alpha)
+fz_bbox_fill_shade(fz_device *dev, fz_shade *shade, fz_matrix ctm, float alpha)
{
- fz_bbox *result = user;
+ fz_bbox *result = dev->user;
fz_bbox bbox = fz_round_rect(fz_bound_shade(shade, ctm));
*result = fz_union_bbox(*result, bbox);
}
static void
-fz_bbox_fill_image(void *user, fz_pixmap *image, fz_matrix ctm, float alpha)
+fz_bbox_fill_image(fz_device *dev, fz_pixmap *image, fz_matrix ctm, float alpha)
{
- fz_bbox *result = user;
+ fz_bbox *result = dev->user;
fz_bbox bbox = fz_round_rect(fz_transform_rect(ctm, fz_unit_rect));
*result = fz_union_bbox(*result, bbox);
}
static void
-fz_bbox_fill_image_mask(void *user, fz_pixmap *image, fz_matrix ctm,
+fz_bbox_fill_image_mask(fz_device *dev, fz_pixmap *image, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
- fz_bbox_fill_image(user, image, ctm, alpha);
+ fz_bbox_fill_image(dev->user, image, ctm, alpha);
}
fz_device *
-fz_new_bbox_device(fz_bbox *result)
+fz_new_bbox_device(fz_context *ctx, fz_bbox *result)
{
fz_device *dev;
- dev = fz_new_device(result);
+ dev = fz_new_device(ctx, result);
dev->fill_path = fz_bbox_fill_path;
dev->stroke_path = fz_bbox_stroke_path;
diff --git a/fitz/dev_list.c b/fitz/dev_list.c
index 955824f9..63e9eee0 100644
--- a/fitz/dev_list.c
+++ b/fitz/dev_list.c
@@ -64,13 +64,13 @@ struct fz_display_list_s
enum { ISOLATED = 1, KNOCKOUT = 2 };
static fz_display_node *
-fz_new_display_node(fz_display_command cmd, fz_matrix ctm,
+fz_new_display_node(fz_context *ctx, fz_display_command cmd, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
fz_display_node *node;
int i;
- node = fz_malloc(sizeof(fz_display_node));
+ node = fz_malloc(ctx, sizeof(fz_display_node));
node->cmd = cmd;
node->next = NULL;
node->rect = fz_empty_rect;
@@ -97,9 +97,9 @@ fz_new_display_node(fz_display_command cmd, fz_matrix ctm,
}
static fz_stroke_state *
-fz_clone_stroke_state(fz_stroke_state *stroke)
+fz_clone_stroke_state(fz_context *ctx, fz_stroke_state *stroke)
{
- fz_stroke_state *newstroke = fz_malloc(sizeof(fz_stroke_state));
+ fz_stroke_state *newstroke = fz_malloc(ctx, sizeof(fz_stroke_state));
*newstroke = *stroke;
return newstroke;
}
@@ -184,7 +184,7 @@ fz_append_display_node(fz_display_list *list, fz_display_node *node)
}
static void
-fz_free_display_node(fz_display_node *node)
+fz_free_display_node(fz_context *ctx, fz_display_node *node)
{
switch (node->cmd)
{
@@ -192,22 +192,22 @@ fz_free_display_node(fz_display_node *node)
case FZ_CMD_STROKE_PATH:
case FZ_CMD_CLIP_PATH:
case FZ_CMD_CLIP_STROKE_PATH:
- fz_free_path(node->item.path);
+ fz_free_path(ctx, node->item.path);
break;
case FZ_CMD_FILL_TEXT:
case FZ_CMD_STROKE_TEXT:
case FZ_CMD_CLIP_TEXT:
case FZ_CMD_CLIP_STROKE_TEXT:
case FZ_CMD_IGNORE_TEXT:
- fz_free_text(node->item.text);
+ fz_free_text(ctx, node->item.text);
break;
case FZ_CMD_FILL_SHADE:
- fz_drop_shade(node->item.shade);
+ fz_drop_shade(ctx, node->item.shade);
break;
case FZ_CMD_FILL_IMAGE:
case FZ_CMD_FILL_IMAGE_MASK:
case FZ_CMD_CLIP_IMAGE_MASK:
- fz_drop_pixmap(node->item.image);
+ fz_drop_pixmap(ctx, node->item.image);
break;
case FZ_CMD_POP_CLIP:
case FZ_CMD_BEGIN_MASK:
@@ -219,214 +219,214 @@ fz_free_display_node(fz_display_node *node)
break;
}
if (node->stroke)
- fz_free(node->stroke);
+ fz_free(ctx, node->stroke);
if (node->colorspace)
- fz_drop_colorspace(node->colorspace);
- fz_free(node);
+ fz_drop_colorspace(ctx, node->colorspace);
+ fz_free(ctx, node);
}
static void
-fz_list_fill_path(void *user, fz_path *path, int even_odd, fz_matrix ctm,
+fz_list_fill_path(fz_device *dev, fz_path *path, int even_odd, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
fz_display_node *node;
- node = fz_new_display_node(FZ_CMD_FILL_PATH, ctm, colorspace, color, alpha);
+ node = fz_new_display_node(dev->ctx, FZ_CMD_FILL_PATH, ctm, colorspace, color, alpha);
node->rect = fz_bound_path(path, NULL, ctm);
- node->item.path = fz_clone_path(path);
+ node->item.path = fz_clone_path(dev->ctx, path);
node->flag = even_odd;
- fz_append_display_node(user, node);
+ fz_append_display_node(dev->user, node);
}
static void
-fz_list_stroke_path(void *user, fz_path *path, fz_stroke_state *stroke, fz_matrix ctm,
+fz_list_stroke_path(fz_device *dev, fz_path *path, fz_stroke_state *stroke, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
fz_display_node *node;
- node = fz_new_display_node(FZ_CMD_STROKE_PATH, ctm, colorspace, color, alpha);
+ node = fz_new_display_node(dev->ctx, FZ_CMD_STROKE_PATH, ctm, colorspace, color, alpha);
node->rect = fz_bound_path(path, stroke, ctm);
- node->item.path = fz_clone_path(path);
- node->stroke = fz_clone_stroke_state(stroke);
- fz_append_display_node(user, node);
+ node->item.path = fz_clone_path(dev->ctx, path);
+ node->stroke = fz_clone_stroke_state(dev->ctx, stroke);
+ fz_append_display_node(dev->user, node);
}
static void
-fz_list_clip_path(void *user, fz_path *path, fz_rect *rect, int even_odd, fz_matrix ctm)
+fz_list_clip_path(fz_device *dev, fz_path *path, fz_rect *rect, int even_odd, fz_matrix ctm)
{
fz_display_node *node;
- node = fz_new_display_node(FZ_CMD_CLIP_PATH, ctm, NULL, NULL, 0);
+ node = fz_new_display_node(dev->ctx, FZ_CMD_CLIP_PATH, ctm, NULL, NULL, 0);
node->rect = fz_bound_path(path, NULL, ctm);
if (rect != NULL)
node->rect = fz_intersect_rect(node->rect, *rect);
- node->item.path = fz_clone_path(path);
+ node->item.path = fz_clone_path(dev->ctx, path);
node->flag = even_odd;
- fz_append_display_node(user, node);
+ fz_append_display_node(dev->user, node);
}
static void
-fz_list_clip_stroke_path(void *user, fz_path *path, fz_rect *rect, fz_stroke_state *stroke, fz_matrix ctm)
+fz_list_clip_stroke_path(fz_device *dev, fz_path *path, fz_rect *rect, fz_stroke_state *stroke, fz_matrix ctm)
{
fz_display_node *node;
- node = fz_new_display_node(FZ_CMD_CLIP_STROKE_PATH, ctm, NULL, NULL, 0);
+ node = fz_new_display_node(dev->ctx, FZ_CMD_CLIP_STROKE_PATH, ctm, NULL, NULL, 0);
node->rect = fz_bound_path(path, stroke, ctm);
if (rect != NULL)
node->rect = fz_intersect_rect(node->rect, *rect);
- node->item.path = fz_clone_path(path);
- node->stroke = fz_clone_stroke_state(stroke);
- fz_append_display_node(user, node);
+ node->item.path = fz_clone_path(dev->ctx, path);
+ node->stroke = fz_clone_stroke_state(dev->ctx, stroke);
+ fz_append_display_node(dev->user, node);
}
static void
-fz_list_fill_text(void *user, fz_text *text, fz_matrix ctm,
+fz_list_fill_text(fz_device *dev, fz_text *text, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
fz_display_node *node;
- node = fz_new_display_node(FZ_CMD_FILL_TEXT, ctm, colorspace, color, alpha);
+ node = fz_new_display_node(dev->ctx, FZ_CMD_FILL_TEXT, ctm, colorspace, color, alpha);
node->rect = fz_bound_text(text, ctm);
- node->item.text = fz_clone_text(text);
- fz_append_display_node(user, node);
+ node->item.text = fz_clone_text(dev->ctx, text);
+ fz_append_display_node(dev->user, node);
}
static void
-fz_list_stroke_text(void *user, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm,
+fz_list_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
fz_display_node *node;
- node = fz_new_display_node(FZ_CMD_STROKE_TEXT, ctm, colorspace, color, alpha);
+ node = fz_new_display_node(dev->ctx, FZ_CMD_STROKE_TEXT, ctm, colorspace, color, alpha);
node->rect = fz_bound_text(text, ctm);
- node->item.text = fz_clone_text(text);
- node->stroke = fz_clone_stroke_state(stroke);
- fz_append_display_node(user, node);
+ node->item.text = fz_clone_text(dev->ctx, text);
+ node->stroke = fz_clone_stroke_state(dev->ctx, stroke);
+ fz_append_display_node(dev->user, node);
}
static void
-fz_list_clip_text(void *user, fz_text *text, fz_matrix ctm, int accumulate)
+fz_list_clip_text(fz_device *dev, fz_text *text, fz_matrix ctm, int accumulate)
{
fz_display_node *node;
- node = fz_new_display_node(FZ_CMD_CLIP_TEXT, ctm, NULL, NULL, 0);
+ node = fz_new_display_node(dev->ctx, FZ_CMD_CLIP_TEXT, ctm, NULL, NULL, 0);
node->rect = fz_bound_text(text, ctm);
- node->item.text = fz_clone_text(text);
+ node->item.text = fz_clone_text(dev->ctx, text);
node->flag = accumulate;
/* when accumulating, be conservative about culling */
if (accumulate)
node->rect = fz_infinite_rect;
- fz_append_display_node(user, node);
+ fz_append_display_node(dev->user, node);
}
static void
-fz_list_clip_stroke_text(void *user, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm)
+fz_list_clip_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm)
{
fz_display_node *node;
- node = fz_new_display_node(FZ_CMD_CLIP_STROKE_TEXT, ctm, NULL, NULL, 0);
+ node = fz_new_display_node(dev->ctx, FZ_CMD_CLIP_STROKE_TEXT, ctm, NULL, NULL, 0);
node->rect = fz_bound_text(text, ctm);
- node->item.text = fz_clone_text(text);
- node->stroke = fz_clone_stroke_state(stroke);
- fz_append_display_node(user, node);
+ node->item.text = fz_clone_text(dev->ctx, text);
+ node->stroke = fz_clone_stroke_state(dev->ctx, stroke);
+ fz_append_display_node(dev->user, node);
}
static void
-fz_list_ignore_text(void *user, fz_text *text, fz_matrix ctm)
+fz_list_ignore_text(fz_device *dev, fz_text *text, fz_matrix ctm)
{
fz_display_node *node;
- node = fz_new_display_node(FZ_CMD_IGNORE_TEXT, ctm, NULL, NULL, 0);
+ node = fz_new_display_node(dev->ctx, FZ_CMD_IGNORE_TEXT, ctm, NULL, NULL, 0);
node->rect = fz_bound_text(text, ctm);
- node->item.text = fz_clone_text(text);
- fz_append_display_node(user, node);
+ node->item.text = fz_clone_text(dev->ctx, text);
+ fz_append_display_node(dev->user, node);
}
static void
-fz_list_pop_clip(void *user)
+fz_list_pop_clip(fz_device *dev)
{
fz_display_node *node;
- node = fz_new_display_node(FZ_CMD_POP_CLIP, fz_identity, NULL, NULL, 0);
- fz_append_display_node(user, node);
+ node = fz_new_display_node(dev->ctx, FZ_CMD_POP_CLIP, fz_identity, NULL, NULL, 0);
+ fz_append_display_node(dev->user, node);
}
static void
-fz_list_fill_shade(void *user, fz_shade *shade, fz_matrix ctm, float alpha)
+fz_list_fill_shade(fz_device *dev, fz_shade *shade, fz_matrix ctm, float alpha)
{
fz_display_node *node;
- node = fz_new_display_node(FZ_CMD_FILL_SHADE, ctm, NULL, NULL, alpha);
+ node = fz_new_display_node(dev->ctx, FZ_CMD_FILL_SHADE, ctm, NULL, NULL, alpha);
node->rect = fz_bound_shade(shade, ctm);
node->item.shade = fz_keep_shade(shade);
- fz_append_display_node(user, node);
+ fz_append_display_node(dev->user, node);
}
static void
-fz_list_fill_image(void *user, fz_pixmap *image, fz_matrix ctm, float alpha)
+fz_list_fill_image(fz_device *dev, fz_pixmap *image, fz_matrix ctm, float alpha)
{
fz_display_node *node;
- node = fz_new_display_node(FZ_CMD_FILL_IMAGE, ctm, NULL, NULL, alpha);
+ node = fz_new_display_node(dev->ctx, FZ_CMD_FILL_IMAGE, ctm, NULL, NULL, alpha);
node->rect = fz_transform_rect(ctm, fz_unit_rect);
node->item.image = fz_keep_pixmap(image);
- fz_append_display_node(user, node);
+ fz_append_display_node(dev->user, node);
}
static void
-fz_list_fill_image_mask(void *user, fz_pixmap *image, fz_matrix ctm,
+fz_list_fill_image_mask(fz_device *dev, fz_pixmap *image, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
fz_display_node *node;
- node = fz_new_display_node(FZ_CMD_FILL_IMAGE_MASK, ctm, colorspace, color, alpha);
+ node = fz_new_display_node(dev->ctx, FZ_CMD_FILL_IMAGE_MASK, ctm, colorspace, color, alpha);
node->rect = fz_transform_rect(ctm, fz_unit_rect);
node->item.image = fz_keep_pixmap(image);
- fz_append_display_node(user, node);
+ fz_append_display_node(dev->user, node);
}
static void
-fz_list_clip_image_mask(void *user, fz_pixmap *image, fz_rect *rect, fz_matrix ctm)
+fz_list_clip_image_mask(fz_device *dev, fz_pixmap *image, fz_rect *rect, fz_matrix ctm)
{
fz_display_node *node;
- node = fz_new_display_node(FZ_CMD_CLIP_IMAGE_MASK, ctm, NULL, NULL, 0);
+ node = fz_new_display_node(dev->ctx, FZ_CMD_CLIP_IMAGE_MASK, ctm, NULL, NULL, 0);
node->rect = fz_transform_rect(ctm, fz_unit_rect);
if (rect != NULL)
node->rect = fz_intersect_rect(node->rect, *rect);
node->item.image = fz_keep_pixmap(image);
- fz_append_display_node(user, node);
+ fz_append_display_node(dev->user, node);
}
static void
-fz_list_begin_mask(void *user, fz_rect rect, int luminosity, fz_colorspace *colorspace, float *color)
+fz_list_begin_mask(fz_device *dev, fz_rect rect, int luminosity, fz_colorspace *colorspace, float *color)
{
fz_display_node *node;
- node = fz_new_display_node(FZ_CMD_BEGIN_MASK, fz_identity, colorspace, color, 0);
+ node = fz_new_display_node(dev->ctx, FZ_CMD_BEGIN_MASK, fz_identity, colorspace, color, 0);
node->rect = rect;
node->flag = luminosity;
- fz_append_display_node(user, node);
+ fz_append_display_node(dev->user, node);
}
static void
-fz_list_end_mask(void *user)
+fz_list_end_mask(fz_device *dev)
{
fz_display_node *node;
- node = fz_new_display_node(FZ_CMD_END_MASK, fz_identity, NULL, NULL, 0);
- fz_append_display_node(user, node);
+ node = fz_new_display_node(dev->ctx, FZ_CMD_END_MASK, fz_identity, NULL, NULL, 0);
+ fz_append_display_node(dev->user, node);
}
static void
-fz_list_begin_group(void *user, fz_rect rect, int isolated, int knockout, int blendmode, float alpha)
+fz_list_begin_group(fz_device *dev, fz_rect rect, int isolated, int knockout, int blendmode, float alpha)
{
fz_display_node *node;
- node = fz_new_display_node(FZ_CMD_BEGIN_GROUP, fz_identity, NULL, NULL, alpha);
+ node = fz_new_display_node(dev->ctx, FZ_CMD_BEGIN_GROUP, fz_identity, NULL, NULL, alpha);
node->rect = rect;
node->item.blendmode = blendmode;
node->flag |= isolated ? ISOLATED : 0;
node->flag |= knockout ? KNOCKOUT : 0;
- fz_append_display_node(user, node);
+ fz_append_display_node(dev->user, node);
}
static void
-fz_list_end_group(void *user)
+fz_list_end_group(fz_device *dev)
{
fz_display_node *node;
- node = fz_new_display_node(FZ_CMD_END_GROUP, fz_identity, NULL, NULL, 0);
- fz_append_display_node(user, node);
+ node = fz_new_display_node(dev->ctx, FZ_CMD_END_GROUP, fz_identity, NULL, NULL, 0);
+ fz_append_display_node(dev->user, node);
}
static void
-fz_list_begin_tile(void *user, fz_rect area, fz_rect view, float xstep, float ystep, fz_matrix ctm)
+fz_list_begin_tile(fz_device *dev, fz_rect area, fz_rect view, float xstep, float ystep, fz_matrix ctm)
{
fz_display_node *node;
- node = fz_new_display_node(FZ_CMD_BEGIN_TILE, ctm, NULL, NULL, 0);
+ node = fz_new_display_node(dev->ctx, FZ_CMD_BEGIN_TILE, ctm, NULL, NULL, 0);
node->rect = area;
node->color[0] = xstep;
node->color[1] = ystep;
@@ -434,21 +434,21 @@ fz_list_begin_tile(void *user, fz_rect area, fz_rect view, float xstep, float ys
node->color[3] = view.y0;
node->color[4] = view.x1;
node->color[5] = view.y1;
- fz_append_display_node(user, node);
+ fz_append_display_node(dev->user, node);
}
static void
-fz_list_end_tile(void *user)
+fz_list_end_tile(fz_device *dev)
{
fz_display_node *node;
- node = fz_new_display_node(FZ_CMD_END_TILE, fz_identity, NULL, NULL, 0);
- fz_append_display_node(user, node);
+ node = fz_new_display_node(dev->ctx, FZ_CMD_END_TILE, fz_identity, NULL, NULL, 0);
+ fz_append_display_node(dev->user, node);
}
fz_device *
-fz_new_list_device(fz_display_list *list)
+fz_new_list_device(fz_context *ctx, fz_display_list *list)
{
- fz_device *dev = fz_new_device(list);
+ fz_device *dev = fz_new_device(ctx, list);
dev->fill_path = fz_list_fill_path;
dev->stroke_path = fz_list_stroke_path;
@@ -480,9 +480,9 @@ fz_new_list_device(fz_display_list *list)
}
fz_display_list *
-fz_new_display_list(void)
+fz_new_display_list(fz_context *ctx)
{
- fz_display_list *list = fz_malloc(sizeof(fz_display_list));
+ fz_display_list *list = fz_malloc(ctx, sizeof(fz_display_list));
list->first = NULL;
list->last = NULL;
list->top = 0;
@@ -491,16 +491,16 @@ fz_new_display_list(void)
}
void
-fz_free_display_list(fz_display_list *list)
+fz_free_display_list(fz_context *ctx, fz_display_list *list)
{
fz_display_node *node = list->first;
while (node)
{
fz_display_node *next = node->next;
- fz_free_display_node(node);
+ fz_free_display_node(ctx, node);
node = next;
}
- fz_free(list);
+ fz_free(ctx, list);
}
void
diff --git a/fitz/dev_null.c b/fitz/dev_null.c
index 508be7e0..394f8374 100644
--- a/fitz/dev_null.c
+++ b/fitz/dev_null.c
@@ -1,13 +1,13 @@
#include "fitz.h"
fz_device *
-fz_new_device(void *user)
+fz_new_device(fz_context *ctx, void *user)
{
- fz_device *dev = fz_malloc(sizeof(fz_device));
- memset(dev, 0, sizeof(fz_device));
+ fz_device *dev = fz_calloc(ctx, 1, sizeof(fz_device));
dev->hints = 0;
dev->flags = 0;
dev->user = user;
+ dev->ctx = ctx;
return dev;
}
@@ -15,8 +15,8 @@ void
fz_free_device(fz_device *dev)
{
if (dev->free_user)
- dev->free_user(dev->user);
- fz_free(dev);
+ dev->free_user(dev);
+ fz_free(dev->ctx, dev);
}
void
@@ -24,7 +24,7 @@ fz_fill_path(fz_device *dev, fz_path *path, int even_odd, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
if (dev->fill_path)
- dev->fill_path(dev->user, path, even_odd, ctm, colorspace, color, alpha);
+ dev->fill_path(dev, path, even_odd, ctm, colorspace, color, alpha);
}
void
@@ -32,21 +32,21 @@ fz_stroke_path(fz_device *dev, fz_path *path, fz_stroke_state *stroke, fz_matrix
fz_colorspace *colorspace, float *color, float alpha)
{
if (dev->stroke_path)
- dev->stroke_path(dev->user, path, stroke, ctm, colorspace, color, alpha);
+ dev->stroke_path(dev, path, stroke, ctm, colorspace, color, alpha);
}
void
fz_clip_path(fz_device *dev, fz_path *path, fz_rect *rect, int even_odd, fz_matrix ctm)
{
if (dev->clip_path)
- dev->clip_path(dev->user, path, rect, even_odd, ctm);
+ dev->clip_path(dev, path, rect, even_odd, ctm);
}
void
fz_clip_stroke_path(fz_device *dev, fz_path *path, fz_rect *rect, fz_stroke_state *stroke, fz_matrix ctm)
{
if (dev->clip_stroke_path)
- dev->clip_stroke_path(dev->user, path, rect, stroke, ctm);
+ dev->clip_stroke_path(dev, path, rect, stroke, ctm);
}
void
@@ -54,7 +54,7 @@ fz_fill_text(fz_device *dev, fz_text *text, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
if (dev->fill_text)
- dev->fill_text(dev->user, text, ctm, colorspace, color, alpha);
+ dev->fill_text(dev, text, ctm, colorspace, color, alpha);
}
void
@@ -62,49 +62,49 @@ fz_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, fz_matrix
fz_colorspace *colorspace, float *color, float alpha)
{
if (dev->stroke_text)
- dev->stroke_text(dev->user, text, stroke, ctm, colorspace, color, alpha);
+ dev->stroke_text(dev, text, stroke, ctm, colorspace, color, alpha);
}
void
fz_clip_text(fz_device *dev, fz_text *text, fz_matrix ctm, int accumulate)
{
if (dev->clip_text)
- dev->clip_text(dev->user, text, ctm, accumulate);
+ dev->clip_text(dev, text, ctm, accumulate);
}
void
fz_clip_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm)
{
if (dev->clip_stroke_text)
- dev->clip_stroke_text(dev->user, text, stroke, ctm);
+ dev->clip_stroke_text(dev, text, stroke, ctm);
}
void
fz_ignore_text(fz_device *dev, fz_text *text, fz_matrix ctm)
{
if (dev->ignore_text)
- dev->ignore_text(dev->user, text, ctm);
+ dev->ignore_text(dev, text, ctm);
}
void
fz_pop_clip(fz_device *dev)
{
if (dev->pop_clip)
- dev->pop_clip(dev->user);
+ dev->pop_clip(dev);
}
void
fz_fill_shade(fz_device *dev, fz_shade *shade, fz_matrix ctm, float alpha)
{
if (dev->fill_shade)
- dev->fill_shade(dev->user, shade, ctm, alpha);
+ dev->fill_shade(dev, shade, ctm, alpha);
}
void
fz_fill_image(fz_device *dev, fz_pixmap *image, fz_matrix ctm, float alpha)
{
if (dev->fill_image)
- dev->fill_image(dev->user, image, ctm, alpha);
+ dev->fill_image(dev, image, ctm, alpha);
}
void
@@ -112,54 +112,54 @@ fz_fill_image_mask(fz_device *dev, fz_pixmap *image, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
if (dev->fill_image_mask)
- dev->fill_image_mask(dev->user, image, ctm, colorspace, color, alpha);
+ dev->fill_image_mask(dev, image, ctm, colorspace, color, alpha);
}
void
fz_clip_image_mask(fz_device *dev, fz_pixmap *image, fz_rect *rect, fz_matrix ctm)
{
if (dev->clip_image_mask)
- dev->clip_image_mask(dev->user, image, rect, ctm);
+ dev->clip_image_mask(dev, image, rect, ctm);
}
void
fz_begin_mask(fz_device *dev, fz_rect area, int luminosity, fz_colorspace *colorspace, float *bc)
{
if (dev->begin_mask)
- dev->begin_mask(dev->user, area, luminosity, colorspace, bc);
+ dev->begin_mask(dev, area, luminosity, colorspace, bc);
}
void
fz_end_mask(fz_device *dev)
{
if (dev->end_mask)
- dev->end_mask(dev->user);
+ dev->end_mask(dev);
}
void
fz_begin_group(fz_device *dev, fz_rect area, int isolated, int knockout, int blendmode, float alpha)
{
if (dev->begin_group)
- dev->begin_group(dev->user, area, isolated, knockout, blendmode, alpha);
+ dev->begin_group(dev, area, isolated, knockout, blendmode, alpha);
}
void
fz_end_group(fz_device *dev)
{
if (dev->end_group)
- dev->end_group(dev->user);
+ dev->end_group(dev);
}
void
fz_begin_tile(fz_device *dev, fz_rect area, fz_rect view, float xstep, float ystep, fz_matrix ctm)
{
if (dev->begin_tile)
- dev->begin_tile(dev->user, area, view, xstep, ystep, ctm);
+ dev->begin_tile(dev, area, view, xstep, ystep, ctm);
}
void
fz_end_tile(fz_device *dev)
{
if (dev->end_tile)
- dev->end_tile(dev->user);
+ dev->end_tile(dev);
}
diff --git a/fitz/dev_text.c b/fitz/dev_text.c
index 139c3eed..cebfbceb 100644
--- a/fitz/dev_text.c
+++ b/fitz/dev_text.c
@@ -17,10 +17,10 @@ struct fz_text_device_s
};
fz_text_span *
-fz_new_text_span(void)
+fz_new_text_span(fz_context *ctx)
{
fz_text_span *span;
- span = fz_malloc(sizeof(fz_text_span));
+ span = fz_malloc(ctx, sizeof(fz_text_span));
span->font = NULL;
span->wmode = 0;
span->size = 0;
@@ -33,23 +33,29 @@ fz_new_text_span(void)
}
void
-fz_free_text_span(fz_text_span *span)
+fz_free_text_span(fz_context *ctx, fz_text_span *span)
{
- if (span->font)
- fz_drop_font(span->font);
- if (span->next)
- fz_free_text_span(span->next);
- fz_free(span->text);
- fz_free(span);
+ fz_text_span *next;
+
+ do
+ {
+ next = span->next;
+ if (span->font)
+ fz_drop_font(ctx, span->font);
+ fz_free(ctx, span->text);
+ fz_free(ctx, span);
+ span = next;
+ }
+ while (span != NULL);
}
static void
-fz_add_text_char_imp(fz_text_span *span, int c, fz_bbox bbox)
+fz_add_text_char_imp(fz_context *ctx, fz_text_span *span, int c, fz_bbox bbox)
{
if (span->len + 1 >= span->cap)
{
span->cap = span->cap > 1 ? (span->cap * 3) / 2 : 80;
- span->text = fz_realloc(span->text, span->cap, sizeof(fz_text_char));
+ span->text = fz_realloc(ctx, span->text, span->cap * sizeof(fz_text_char));
}
span->text[span->len].c = c;
span->text[span->len].bbox = bbox;
@@ -67,7 +73,7 @@ fz_split_bbox(fz_bbox bbox, int i, int n)
}
static void
-fz_add_text_char(fz_text_span **last, fz_font *font, float size, int wmode, int c, fz_bbox bbox)
+fz_add_text_char(fz_context *ctx, fz_text_span **last, fz_font *font, float size, int wmode, int c, fz_bbox bbox)
{
fz_text_span *span = *last;
@@ -79,7 +85,7 @@ fz_add_text_char(fz_text_span **last, fz_font *font, float size, int wmode, int
if ((span->font != font || span->size != size || span->wmode != wmode) && c != 32)
{
- span = fz_new_text_span();
+ span = fz_new_text_span(ctx);
span->font = fz_keep_font(font);
span->size = size;
span->wmode = wmode;
@@ -92,34 +98,34 @@ fz_add_text_char(fz_text_span **last, fz_font *font, float size, int wmode, int
case -1: /* ignore when one unicode character maps to multiple glyphs */
break;
case 0xFB00: /* ff */
- fz_add_text_char_imp(span, 'f', fz_split_bbox(bbox, 0, 2));
- fz_add_text_char_imp(span, 'f', fz_split_bbox(bbox, 1, 2));
+ fz_add_text_char_imp(ctx, span, 'f', fz_split_bbox(bbox, 0, 2));
+ fz_add_text_char_imp(ctx, span, 'f', fz_split_bbox(bbox, 1, 2));
break;
case 0xFB01: /* fi */
- fz_add_text_char_imp(span, 'f', fz_split_bbox(bbox, 0, 2));
- fz_add_text_char_imp(span, 'i', fz_split_bbox(bbox, 1, 2));
+ fz_add_text_char_imp(ctx, span, 'f', fz_split_bbox(bbox, 0, 2));
+ fz_add_text_char_imp(ctx, span, 'i', fz_split_bbox(bbox, 1, 2));
break;
case 0xFB02: /* fl */
- fz_add_text_char_imp(span, 'f', fz_split_bbox(bbox, 0, 2));
- fz_add_text_char_imp(span, 'l', fz_split_bbox(bbox, 1, 2));
+ fz_add_text_char_imp(ctx, span, 'f', fz_split_bbox(bbox, 0, 2));
+ fz_add_text_char_imp(ctx, span, 'l', fz_split_bbox(bbox, 1, 2));
break;
case 0xFB03: /* ffi */
- fz_add_text_char_imp(span, 'f', fz_split_bbox(bbox, 0, 3));
- fz_add_text_char_imp(span, 'f', fz_split_bbox(bbox, 1, 3));
- fz_add_text_char_imp(span, 'i', fz_split_bbox(bbox, 2, 3));
+ fz_add_text_char_imp(ctx, span, 'f', fz_split_bbox(bbox, 0, 3));
+ fz_add_text_char_imp(ctx, span, 'f', fz_split_bbox(bbox, 1, 3));
+ fz_add_text_char_imp(ctx, span, 'i', fz_split_bbox(bbox, 2, 3));
break;
case 0xFB04: /* ffl */
- fz_add_text_char_imp(span, 'f', fz_split_bbox(bbox, 0, 3));
- fz_add_text_char_imp(span, 'f', fz_split_bbox(bbox, 1, 3));
- fz_add_text_char_imp(span, 'l', fz_split_bbox(bbox, 2, 3));
+ fz_add_text_char_imp(ctx, span, 'f', fz_split_bbox(bbox, 0, 3));
+ fz_add_text_char_imp(ctx, span, 'f', fz_split_bbox(bbox, 1, 3));
+ fz_add_text_char_imp(ctx, span, 'l', fz_split_bbox(bbox, 2, 3));
break;
case 0xFB05: /* long st */
case 0xFB06: /* st */
- fz_add_text_char_imp(span, 's', fz_split_bbox(bbox, 0, 2));
- fz_add_text_char_imp(span, 't', fz_split_bbox(bbox, 1, 2));
+ fz_add_text_char_imp(ctx, span, 's', fz_split_bbox(bbox, 0, 2));
+ fz_add_text_char_imp(ctx, span, 't', fz_split_bbox(bbox, 1, 2));
break;
default:
- fz_add_text_char_imp(span, c, bbox);
+ fz_add_text_char_imp(ctx, span, c, bbox);
break;
}
}
@@ -136,10 +142,10 @@ fz_divide_text_chars(fz_text_span **last, int n, fz_bbox bbox)
}
static void
-fz_add_text_newline(fz_text_span **last, fz_font *font, float size, int wmode)
+fz_add_text_newline(fz_context *ctx, fz_text_span **last, fz_font *font, float size, int wmode)
{
fz_text_span *span;
- span = fz_new_text_span();
+ span = fz_new_text_span(ctx);
span->font = fz_keep_font(font);
span->size = size;
span->wmode = wmode;
@@ -209,7 +215,7 @@ fz_debug_text_span(fz_text_span *span)
}
static void
-fz_text_extract_span(fz_text_span **last, fz_text *text, fz_matrix ctm, fz_point *pen)
+fz_text_extract_span(fz_context *ctx, fz_text_span **last, fz_text *text, fz_matrix ctm, fz_point *pen)
{
fz_font *font = text->font;
FT_Face face = font->ft_face;
@@ -267,7 +273,7 @@ fz_text_extract_span(fz_text_span **last, fz_text *text, fz_matrix ctm, fz_point
{
if (text->items[i].gid < 0)
{
- fz_add_text_char(last, font, size, text->wmode, text->items[i].ucs, fz_round_rect(rect));
+ fz_add_text_char(ctx, last, font, size, text->wmode, text->items[i].ucs, fz_round_rect(rect));
multi ++;
fz_divide_text_chars(last, multi, fz_round_rect(rect));
continue;
@@ -295,7 +301,7 @@ fz_text_extract_span(fz_text_span **last, fz_text *text, fz_matrix ctm, fz_point
if (dist > size * LINE_DIST)
{
- fz_add_text_newline(last, font, size, text->wmode);
+ fz_add_text_newline(ctx, last, font, size, text->wmode);
}
else if (fabsf(dot) > 0.95f && dist > size * SPACE_DIST)
{
@@ -307,7 +313,7 @@ fz_text_extract_span(fz_text_span **last, fz_text *text, fz_matrix ctm, fz_point
spacerect.x1 = 0;
spacerect.y1 = 1;
spacerect = fz_transform_rect(trm, spacerect);
- fz_add_text_char(last, font, size, text->wmode, ' ', fz_round_rect(spacerect));
+ fz_add_text_char(ctx, last, font, size, text->wmode, ' ', fz_round_rect(spacerect));
}
}
}
@@ -342,71 +348,71 @@ fz_text_extract_span(fz_text_span **last, fz_text *text, fz_matrix ctm, fz_point
pen->x = trm.e + dir.x * adv;
pen->y = trm.f + dir.y * adv;
- fz_add_text_char(last, font, size, text->wmode, text->items[i].ucs, fz_round_rect(rect));
+ fz_add_text_char(ctx, last, font, size, text->wmode, text->items[i].ucs, fz_round_rect(rect));
}
}
static void
-fz_text_fill_text(void *user, fz_text *text, fz_matrix ctm,
+fz_text_fill_text(fz_device *dev, fz_text *text, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
- fz_text_device *tdev = user;
- fz_text_extract_span(&tdev->span, text, ctm, &tdev->point);
+ fz_text_device *tdev = dev->user;
+ fz_text_extract_span(dev->ctx, &tdev->span, text, ctm, &tdev->point);
}
static void
-fz_text_stroke_text(void *user, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm,
+fz_text_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
- fz_text_device *tdev = user;
- fz_text_extract_span(&tdev->span, text, ctm, &tdev->point);
+ fz_text_device *tdev = dev->user;
+ fz_text_extract_span(dev->ctx, &tdev->span, text, ctm, &tdev->point);
}
static void
-fz_text_clip_text(void *user, fz_text *text, fz_matrix ctm, int accumulate)
+fz_text_clip_text(fz_device *dev, fz_text *text, fz_matrix ctm, int accumulate)
{
- fz_text_device *tdev = user;
- fz_text_extract_span(&tdev->span, text, ctm, &tdev->point);
+ fz_text_device *tdev = dev->user;
+ fz_text_extract_span(dev->ctx, &tdev->span, text, ctm, &tdev->point);
}
static void
-fz_text_clip_stroke_text(void *user, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm)
+fz_text_clip_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm)
{
- fz_text_device *tdev = user;
- fz_text_extract_span(&tdev->span, text, ctm, &tdev->point);
+ fz_text_device *tdev = dev->user;
+ fz_text_extract_span(dev->ctx, &tdev->span, text, ctm, &tdev->point);
}
static void
-fz_text_ignore_text(void *user, fz_text *text, fz_matrix ctm)
+fz_text_ignore_text(fz_device *dev, fz_text *text, fz_matrix ctm)
{
- fz_text_device *tdev = user;
- fz_text_extract_span(&tdev->span, text, ctm, &tdev->point);
+ fz_text_device *tdev = dev->user;
+ fz_text_extract_span(dev->ctx, &tdev->span, text, ctm, &tdev->point);
}
static void
-fz_text_free_user(void *user)
+fz_text_free_user(fz_device *dev)
{
- fz_text_device *tdev = user;
+ fz_text_device *tdev = dev->user;
tdev->span->eol = 1;
/* TODO: unicode NFC normalization */
/* TODO: bidi logical reordering */
- fz_free(tdev);
+ fz_free(dev->ctx, tdev);
}
fz_device *
-fz_new_text_device(fz_text_span *root)
+fz_new_text_device(fz_context *ctx, fz_text_span *root)
{
fz_device *dev;
- fz_text_device *tdev = fz_malloc(sizeof(fz_text_device));
+ fz_text_device *tdev = fz_malloc(ctx, sizeof(fz_text_device));
tdev->head = root;
tdev->span = root;
tdev->point.x = -1;
tdev->point.y = -1;
- dev = fz_new_device(tdev);
+ dev = fz_new_device(ctx, tdev);
dev->hints = FZ_IGNORE_IMAGE | FZ_IGNORE_SHADE;
dev->free_user = fz_text_free_user;
dev->fill_text = fz_text_fill_text;
diff --git a/fitz/dev_trace.c b/fitz/dev_trace.c
index 03e05f91..4db215a0 100644
--- a/fitz/dev_trace.c
+++ b/fitz/dev_trace.c
@@ -60,7 +60,7 @@ fz_trace_path(fz_path *path, int indent)
}
static void
-fz_trace_fill_path(void *user, fz_path *path, int even_odd, fz_matrix ctm,
+fz_trace_fill_path(fz_device *dev, fz_path *path, int even_odd, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
printf("<fill_path ");
@@ -76,7 +76,7 @@ fz_trace_fill_path(void *user, fz_path *path, int even_odd, fz_matrix ctm,
}
static void
-fz_trace_stroke_path(void *user, fz_path *path, fz_stroke_state *stroke, fz_matrix ctm,
+fz_trace_stroke_path(fz_device *dev, fz_path *path, fz_stroke_state *stroke, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
int i;
@@ -105,7 +105,7 @@ fz_trace_stroke_path(void *user, fz_path *path, fz_stroke_state *stroke, fz_matr
}
static void
-fz_trace_clip_path(void *user, fz_path *path, fz_rect *rect, int even_odd, fz_matrix ctm)
+fz_trace_clip_path(fz_device *dev, fz_path *path, fz_rect *rect, int even_odd, fz_matrix ctm)
{
printf("<clip_path ");
if (even_odd)
@@ -120,7 +120,7 @@ fz_trace_clip_path(void *user, fz_path *path, fz_rect *rect, int even_odd, fz_ma
}
static void
-fz_trace_clip_stroke_path(void *user, fz_path *path, fz_rect *rect, fz_stroke_state *stroke, fz_matrix ctm)
+fz_trace_clip_stroke_path(fz_device *dev, fz_path *path, fz_rect *rect, fz_stroke_state *stroke, fz_matrix ctm)
{
printf("<clip_stroke_path ");
fz_trace_matrix(ctm);
@@ -130,7 +130,7 @@ fz_trace_clip_stroke_path(void *user, fz_path *path, fz_rect *rect, fz_stroke_st
}
static void
-fz_trace_fill_text(void *user, fz_text *text, fz_matrix ctm,
+fz_trace_fill_text(fz_device *dev, fz_text *text, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
printf("<fill_text font=\"%s\" wmode=\"%d\" ", text->font->name, text->wmode);
@@ -142,7 +142,7 @@ fz_trace_fill_text(void *user, fz_text *text, fz_matrix ctm,
}
static void
-fz_trace_stroke_text(void *user, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm,
+fz_trace_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
printf("<stroke_text font=\"%s\" wmode=\"%d\" ", text->font->name, text->wmode);
@@ -154,7 +154,7 @@ fz_trace_stroke_text(void *user, fz_text *text, fz_stroke_state *stroke, fz_matr
}
static void
-fz_trace_clip_text(void *user, fz_text *text, fz_matrix ctm, int accumulate)
+fz_trace_clip_text(fz_device *dev, fz_text *text, fz_matrix ctm, int accumulate)
{
printf("<clip_text font=\"%s\" wmode=\"%d\" ", text->font->name, text->wmode);
printf("accumulate=\"%d\" ", accumulate);
@@ -165,7 +165,7 @@ fz_trace_clip_text(void *user, fz_text *text, fz_matrix ctm, int accumulate)
}
static void
-fz_trace_clip_stroke_text(void *user, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm)
+fz_trace_clip_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm)
{
printf("<clip_stroke_text font=\"%s\" wmode=\"%d\" ", text->font->name, text->wmode);
fz_trace_matrix(fz_concat(ctm, text->trm));
@@ -175,7 +175,7 @@ fz_trace_clip_stroke_text(void *user, fz_text *text, fz_stroke_state *stroke, fz
}
static void
-fz_trace_ignore_text(void *user, fz_text *text, fz_matrix ctm)
+fz_trace_ignore_text(fz_device *dev, fz_text *text, fz_matrix ctm)
{
printf("<ignore_text font=\"%s\" wmode=\"%d\" ", text->font->name, text->wmode);
fz_trace_matrix(fz_concat(ctm, text->trm));
@@ -185,7 +185,7 @@ fz_trace_ignore_text(void *user, fz_text *text, fz_matrix ctm)
}
static void
-fz_trace_fill_image(void *user, fz_pixmap *image, fz_matrix ctm, float alpha)
+fz_trace_fill_image(fz_device *dev, fz_pixmap *image, fz_matrix ctm, float alpha)
{
printf("<fill_image alpha=\"%g\" ", alpha);
fz_trace_matrix(ctm);
@@ -193,7 +193,7 @@ fz_trace_fill_image(void *user, fz_pixmap *image, fz_matrix ctm, float alpha)
}
static void
-fz_trace_fill_shade(void *user, fz_shade *shade, fz_matrix ctm, float alpha)
+fz_trace_fill_shade(fz_device *dev, fz_shade *shade, fz_matrix ctm, float alpha)
{
printf("<fill_shade alpha=\"%g\" ", alpha);
fz_trace_matrix(ctm);
@@ -201,7 +201,7 @@ fz_trace_fill_shade(void *user, fz_shade *shade, fz_matrix ctm, float alpha)
}
static void
-fz_trace_fill_image_mask(void *user, fz_pixmap *image, fz_matrix ctm,
+fz_trace_fill_image_mask(fz_device *dev, fz_pixmap *image, fz_matrix ctm,
fz_colorspace *colorspace, float *color, float alpha)
{
printf("<fill_image_mask ");
@@ -211,7 +211,7 @@ fz_colorspace *colorspace, float *color, float alpha)
}
static void
-fz_trace_clip_image_mask(void *user, fz_pixmap *image, fz_rect *rect, fz_matrix ctm)
+fz_trace_clip_image_mask(fz_device *dev, fz_pixmap *image, fz_rect *rect, fz_matrix ctm)
{
printf("<clip_image_mask ");
fz_trace_matrix(ctm);
@@ -219,13 +219,13 @@ fz_trace_clip_image_mask(void *user, fz_pixmap *image, fz_rect *rect, fz_matrix
}
static void
-fz_trace_pop_clip(void *user)
+fz_trace_pop_clip(fz_device *dev)
{
printf("<pop_clip />\n");
}
static void
-fz_trace_begin_mask(void *user, fz_rect bbox, int luminosity, fz_colorspace *colorspace, float *color)
+fz_trace_begin_mask(fz_device *dev, fz_rect bbox, int luminosity, fz_colorspace *colorspace, float *color)
{
printf("<mask bbox=\"%g %g %g %g\" s=\"%s\" ",
bbox.x0, bbox.y0, bbox.x1, bbox.y1,
@@ -235,13 +235,13 @@ fz_trace_begin_mask(void *user, fz_rect bbox, int luminosity, fz_colorspace *col
}
static void
-fz_trace_end_mask(void *user)
+fz_trace_end_mask(fz_device *dev)
{
printf("</mask>\n");
}
static void
-fz_trace_begin_group(void *user, fz_rect bbox, int isolated, int knockout, int blendmode, float alpha)
+fz_trace_begin_group(fz_device *dev, fz_rect bbox, int isolated, int knockout, int blendmode, float alpha)
{
printf("<group bbox=\"%g %g %g %g\" isolated=\"%d\" knockout=\"%d\" blendmode=\"%s\" alpha=\"%g\">\n",
bbox.x0, bbox.y0, bbox.x1, bbox.y1,
@@ -249,13 +249,13 @@ fz_trace_begin_group(void *user, fz_rect bbox, int isolated, int knockout, int b
}
static void
-fz_trace_end_group(void *user)
+fz_trace_end_group(fz_device *dev)
{
printf("</group>\n");
}
static void
-fz_trace_begin_tile(void *user, fz_rect area, fz_rect view, float xstep, float ystep, fz_matrix ctm)
+fz_trace_begin_tile(fz_device *dev, fz_rect area, fz_rect view, float xstep, float ystep, fz_matrix ctm)
{
printf("<tile ");
printf("area=\"%g %g %g %g\" ", area.x0, area.y0, area.x1, area.y1);
@@ -266,14 +266,14 @@ fz_trace_begin_tile(void *user, fz_rect area, fz_rect view, float xstep, float y
}
static void
-fz_trace_end_tile(void *user)
+fz_trace_end_tile(fz_device *dev)
{
printf("</tile>\n");
}
-fz_device *fz_new_trace_device(void)
+fz_device *fz_new_trace_device(fz_context *ctx)
{
- fz_device *dev = fz_new_device(NULL);
+ fz_device *dev = fz_new_device(ctx, NULL);
dev->fill_path = fz_trace_fill_path;
dev->stroke_path = fz_trace_stroke_path;
diff --git a/fitz/except.c b/fitz/except.c
index 348b950e..4055367d 100644
--- a/fitz/except.c
+++ b/fitz/except.c
@@ -1,3 +1,4 @@
+#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -6,44 +7,76 @@
static void do_throw(fz_except_context *ex)
{
- if(ex->depth >= 0)
- longjmp(ex->stack[ex->depth].buffer, 1);
- else {
- printf("Uncaught exception: %s\n", ex->except.mess);
- exit(EXIT_FAILURE); /* Bale through normal channels */
- }
+ assert(ex != NULL);
+ if(ex->depth >= 0)
+ longjmp(ex->stack[ex->depth].buffer, 1);
+ else {
+ printf("Uncaught exception: %s\n", ex->except.mess);
+ exit(EXIT_FAILURE); /* Bale through normal channels */
+ }
}
void fz_throw(fz_context *ctx, char *fmt, ...)
{
- va_list args;
+ va_list args;
- va_start(args, fmt);
- vsprintf(ctx->except->except.mess, fmt, args);
- va_end(args);
- do_throw(ctx->except);
+ va_start(args, fmt);
+ vsprintf(ctx->except->except.mess, fmt, args);
+ va_end(args);
+ do_throw(ctx->except);
}
fz_except *fz_caught(fz_context *ctx)
{
- return &ctx->except->except;
+ assert(ctx != NULL);
+ assert(ctx->except != NULL);
+ return &ctx->except->except;
}
void fz_rethrow(fz_context *ctx)
{
- do_throw(ctx->except);
+ assert(ctx != NULL);
+ do_throw(ctx->except);
}
void fz_except_xxx_push(fz_except_context *ex)
{
- if(ex->depth+1 >= MAXDEPTH) {
- fprintf(stderr, "Exception stack overflow!\n");
- exit(EXIT_FAILURE); /* Bale through normal channels */
- }
- ex->depth++;
+ assert(ex != NULL);
+ if(ex->depth+1 >= MAXDEPTH) {
+ fprintf(stderr, "Exception stack overflow!\n");
+ exit(EXIT_FAILURE); /* Bale through normal channels */
+ }
+ ex->depth++;
}
void fz_var_xxx(void *e)
{
- /* Do nothing */
+ /* Do nothing */
+}
+
+fz_error fz_except_init(fz_context *ctx)
+{
+ fz_except_context *ex;
+
+ assert(ctx != NULL);
+ if (ctx->except != NULL)
+ return fz_okay;
+ ex = fz_malloc_nothrow(ctx, sizeof(fz_except_context));
+ if (ex == NULL)
+ return fz_error_make("Failed to initialise exception context");
+ ex->depth = -1;
+ ctx->except = ex;
+
+ return fz_okay;
+}
+
+void fz_except_fin(fz_context *ctx)
+{
+ assert(ctx != NULL);
+
+ if (ctx->except == NULL)
+ return;
+ assert(ctx->except->depth == -1);
+ free(ctx->except);
+ ctx->except = NULL;
}
diff --git a/fitz/except.h b/fitz/except.h
index a964b94e..477fbc39 100644
--- a/fitz/except.h
+++ b/fitz/except.h
@@ -3,6 +3,9 @@
#include "fitz.h"
+typedef struct fz_context fz_context;
+typedef int fz_error;
+
typedef struct fz_except {
char mess[256];
} fz_except;
@@ -41,6 +44,8 @@ void fz_rethrow(fz_context *);
void fz_var_xxx(void *x);
-void fz_except_init(fz_context *);
+fz_error fz_except_init(fz_context *);
+
+void fz_except_fin(fz_context *);
#endif /* EXCEPT */
diff --git a/fitz/exceptxxx.h b/fitz/exceptxxx.h
index 185f73f6..285e88b7 100644
--- a/fitz/exceptxxx.h
+++ b/fitz/exceptxxx.h
@@ -5,32 +5,32 @@
#define fz_try(ctx) \
if(fz_except_xxx_push(ctx->except), \
- !setjmp(ctx->except.stack[ctx->except.depth].buffer)) \
+ !setjmp(ctx->except->stack[ctx->except->depth].buffer)) \
{
#define fz_catch(ctx) \
- ctx->except.stack[ctx->except.depth].failed = 0;\
+ ctx->except->stack[ctx->except->depth].failed = 0;\
}\
else\
{\
- ctx->except.stack[ctx->except.depth].failed = 1;\
+ ctx->except->stack[ctx->except->depth].failed = 1;\
}\
-if(ex->stack[ex->depth--].failed)
+if(ctx->except->stack[ctx->except->depth--].failed)
typedef struct fz_except_xxx_stack
{
- int failed;
- jmp_buf buffer;
+ int failed;
+ jmp_buf buffer;
} fz_except_xxx_stack;
#define MAXDEPTH (20)
struct fz_except_context {
- fz_except_xxx_stack stack[MAXDEPTH];
- int depth;
- fz_except except;
+ fz_except_xxx_stack stack[MAXDEPTH];
+ int depth;
+ fz_except except;
};
-void fz_except_xxx_push(fz_except_context *);
+void fz_except_xxx_push(struct fz_except_context *);
#endif /* EXCEPTXXX */
diff --git a/fitz/filt_basic.c b/fitz/filt_basic.c
index 15c42137..32fce6f2 100644
--- a/fitz/filt_basic.c
+++ b/fitz/filt_basic.c
@@ -32,8 +32,10 @@ static void
close_null(fz_stream *stm)
{
struct null_filter *state = stm->state;
- fz_close(state->chain);
- fz_free(state);
+ fz_stream *chain = state->chain;
+
+ fz_free(stm->ctx, state);
+ fz_close(chain);
}
fz_stream *
@@ -41,11 +43,12 @@ fz_open_null(fz_stream *chain, int len)
{
struct null_filter *state;
- state = fz_malloc(sizeof(struct null_filter));
+ assert(chain != NULL);
+ state = fz_malloc(chain->ctx, sizeof(struct null_filter));
state->chain = chain;
state->remain = len;
- return fz_new_stream(state, read_null, close_null);
+ return fz_new_stream(chain->ctx, state, read_null, close_null);
}
/* ASCII Hex Decode */
@@ -135,8 +138,10 @@ static void
close_ahxd(fz_stream *stm)
{
fz_ahxd *state = stm->state;
- fz_close(state->chain);
- fz_free(state);
+ fz_stream *chain = state->chain;
+
+ fz_free(stm->ctx, state);
+ fz_close(chain);
}
fz_stream *
@@ -144,11 +149,11 @@ fz_open_ahxd(fz_stream *chain)
{
fz_ahxd *state;
- state = fz_malloc(sizeof(fz_ahxd));
+ state = fz_malloc(chain->ctx, sizeof(fz_ahxd));
state->chain = chain;
state->eod = 0;
- return fz_new_stream(state, read_ahxd, close_ahxd);
+ return fz_new_stream(chain->ctx, state, read_ahxd, close_ahxd);
}
/* ASCII 85 Decode */
@@ -270,8 +275,10 @@ static void
close_a85d(fz_stream *stm)
{
fz_a85d *state = stm->state;
- fz_close(state->chain);
- fz_free(state);
+ fz_stream *chain = state->chain;
+
+ fz_free(stm->ctx, state);
+ fz_close(chain);
}
fz_stream *
@@ -279,13 +286,14 @@ fz_open_a85d(fz_stream *chain)
{
fz_a85d *state;
- state = fz_malloc(sizeof(fz_a85d));
+ assert(chain != NULL);
+ state = fz_malloc(chain->ctx, sizeof(fz_a85d));
state->chain = chain;
state->rp = state->bp;
state->wp = state->bp;
state->eod = 0;
- return fz_new_stream(state, read_a85d, close_a85d);
+ return fz_new_stream(chain->ctx, state, read_a85d, close_a85d);
}
/* Run Length Decode */
@@ -355,8 +363,10 @@ static void
close_rld(fz_stream *stm)
{
fz_rld *state = stm->state;
- fz_close(state->chain);
- fz_free(state);
+ fz_stream *chain = state->chain;
+
+ fz_free(stm->ctx, state);
+ fz_close(chain);
}
fz_stream *
@@ -364,13 +374,14 @@ fz_open_rld(fz_stream *chain)
{
fz_rld *state;
- state = fz_malloc(sizeof(fz_rld));
+ assert(chain != NULL);
+ state = fz_malloc(chain->ctx, sizeof(fz_rld));
state->chain = chain;
state->run = 0;
state->n = 0;
state->c = 0;
- return fz_new_stream(state, read_rld, close_rld);
+ return fz_new_stream(chain->ctx, state, read_rld, close_rld);
}
/* RC4 Filter */
@@ -402,8 +413,10 @@ static void
close_arc4(fz_stream *stm)
{
fz_arc4c *state = stm->state;
- fz_close(state->chain);
- fz_free(state);
+ fz_stream *chain = state->chain;
+
+ fz_free(stm->ctx, state);
+ fz_close(chain);
}
fz_stream *
@@ -411,11 +424,11 @@ fz_open_arc4(fz_stream *chain, unsigned char *key, unsigned keylen)
{
fz_arc4c *state;
- state = fz_malloc(sizeof(fz_arc4c));
+ state = fz_malloc(chain->ctx, sizeof(fz_arc4c));
state->chain = chain;
fz_arc4_init(&state->arc4, key, keylen);
- return fz_new_stream(state, read_arc4, close_arc4);
+ return fz_new_stream(chain->ctx, state, read_arc4, close_arc4);
}
/* AES Filter */
@@ -484,8 +497,10 @@ static void
close_aesd(fz_stream *stm)
{
fz_aesd *state = stm->state;
- fz_close(state->chain);
- fz_free(state);
+ fz_stream *chain = state->chain;
+
+ fz_free(stm->ctx, state);
+ fz_close(chain);
}
fz_stream *
@@ -493,12 +508,13 @@ fz_open_aesd(fz_stream *chain, unsigned char *key, unsigned keylen)
{
fz_aesd *state;
- state = fz_malloc(sizeof(fz_aesd));
+ assert(chain != NULL);
+ state = fz_malloc(chain->ctx, sizeof(fz_aesd));
state->chain = chain;
aes_setkey_dec(&state->aes, key, keylen * 8);
state->ivcount = 0;
state->rp = state->bp;
state->wp = state->bp;
- return fz_new_stream(state, read_aesd, close_aesd);
+ return fz_new_stream(chain->ctx, state, read_aesd, close_aesd);
}
diff --git a/fitz/filt_dctd.c b/fitz/filt_dctd.c
index 372efe9e..94a06afb 100644
--- a/fitz/filt_dctd.c
+++ b/fitz/filt_dctd.c
@@ -8,6 +8,7 @@ typedef struct fz_dctd_s fz_dctd;
struct fz_dctd_s
{
fz_stream *chain;
+ fz_context *ctx;
int color_transform;
int init;
int stride;
@@ -144,7 +145,7 @@ read_dctd(fz_stream *stm, unsigned char *buf, int len)
jpeg_start_decompress(cinfo);
state->stride = cinfo->output_width * cinfo->output_components;
- state->scanline = fz_malloc(state->stride);
+ state->scanline = fz_malloc(state->ctx, state->stride);
state->rp = state->scanline;
state->wp = state->scanline;
@@ -196,9 +197,9 @@ close_dctd(fz_stream *stm)
skip:
state->chain->rp = state->chain->wp - state->cinfo.src->bytes_in_buffer;
jpeg_destroy_decompress(&state->cinfo);
- fz_free(state->scanline);
+ fz_free(stm->ctx, state->scanline);
fz_close(state->chain);
- fz_free(state);
+ fz_free(stm->ctx, state);
}
fz_stream *
@@ -207,15 +208,16 @@ fz_open_dctd(fz_stream *chain, fz_obj *params)
fz_dctd *state;
fz_obj *obj;
- state = fz_malloc(sizeof(fz_dctd));
- memset(state, 0, sizeof(fz_dctd));
+ assert(chain);
+ state = fz_calloc(chain->ctx, 1, sizeof(fz_dctd));
+ state->ctx = chain->ctx;
state->chain = chain;
state->color_transform = -1; /* unset */
state->init = 0;
- obj = fz_dict_gets(params, "ColorTransform");
+ obj = fz_dict_gets(chain->ctx, params, "ColorTransform");
if (obj)
- state->color_transform = fz_to_int(obj);
+ state->color_transform = fz_to_int(chain->ctx, obj);
- return fz_new_stream(state, read_dctd, close_dctd);
+ return fz_new_stream(chain->ctx, state, read_dctd, close_dctd);
}
diff --git a/fitz/filt_faxd.c b/fitz/filt_faxd.c
index d36c8c76..27e8ed60 100644
--- a/fitz/filt_faxd.c
+++ b/fitz/filt_faxd.c
@@ -662,9 +662,9 @@ close_faxd(fz_stream *stm)
fz_unread_byte(fax->chain);
fz_close(fax->chain);
- fz_free(fax->ref);
- fz_free(fax->dst);
- fz_free(fax);
+ fz_free(stm->ctx, fax->ref);
+ fz_free(stm->ctx, fax->dst);
+ fz_free(stm->ctx, fax);
}
fz_stream *
@@ -672,9 +672,12 @@ fz_open_faxd(fz_stream *chain, fz_obj *params)
{
fz_faxd *fax;
fz_obj *obj;
+ fz_context *ctx;
- fax = fz_malloc(sizeof(fz_faxd));
- fax->chain = chain;
+ assert(chain != NULL);
+ ctx = chain->ctx;
+ fax = fz_malloc(ctx, sizeof(fz_faxd));
+ fax->chain = chain;
fax->ref = NULL;
fax->dst = NULL;
@@ -687,26 +690,26 @@ fz_open_faxd(fz_stream *chain, fz_obj *params)
fax->end_of_block = 1;
fax->black_is_1 = 0;
- obj = fz_dict_gets(params, "K");
- if (obj) fax->k = fz_to_int(obj);
+ obj = fz_dict_gets(ctx, params, "K");
+ if (obj) fax->k = fz_to_int(ctx, obj);
- obj = fz_dict_gets(params, "EndOfLine");
- if (obj) fax->end_of_line = fz_to_bool(obj);
+ obj = fz_dict_gets(ctx, params, "EndOfLine");
+ if (obj) fax->end_of_line = fz_to_bool(ctx, obj);
- obj = fz_dict_gets(params, "EncodedByteAlign");
- if (obj) fax->encoded_byte_align = fz_to_bool(obj);
+ obj = fz_dict_gets(ctx, params, "EncodedByteAlign");
+ if (obj) fax->encoded_byte_align = fz_to_bool(ctx, obj);
- obj = fz_dict_gets(params, "Columns");
- if (obj) fax->columns = fz_to_int(obj);
+ obj = fz_dict_gets(ctx, params, "Columns");
+ if (obj) fax->columns = fz_to_int(ctx, obj);
- obj = fz_dict_gets(params, "Rows");
- if (obj) fax->rows = fz_to_int(obj);
+ obj = fz_dict_gets(ctx, params, "Rows");
+ if (obj) fax->rows = fz_to_int(ctx, obj);
- obj = fz_dict_gets(params, "EndOfBlock");
- if (obj) fax->end_of_block = fz_to_bool(obj);
+ obj = fz_dict_gets(ctx, params, "EndOfBlock");
+ if (obj) fax->end_of_block = fz_to_bool(ctx, obj);
- obj = fz_dict_gets(params, "BlackIs1");
- if (obj) fax->black_is_1 = fz_to_bool(obj);
+ obj = fz_dict_gets(ctx, params, "BlackIs1");
+ if (obj) fax->black_is_1 = fz_to_bool(ctx, obj);
fax->stride = ((fax->columns - 1) >> 3) + 1;
fax->ridx = 0;
@@ -719,13 +722,13 @@ fz_open_faxd(fz_stream *chain, fz_obj *params)
fax->dim = fax->k < 0 ? 2 : 1;
fax->eolc = 0;
- fax->ref = fz_malloc(fax->stride);
- fax->dst = fz_malloc(fax->stride);
+ fax->ref = fz_malloc(ctx, fax->stride);
+ fax->dst = fz_malloc(ctx, fax->stride);
fax->rp = fax->dst;
fax->wp = fax->dst + fax->stride;
memset(fax->ref, 0, fax->stride);
memset(fax->dst, 0, fax->stride);
- return fz_new_stream(fax, read_faxd, close_faxd);
+ return fz_new_stream(ctx, fax, read_faxd, close_faxd);
}
diff --git a/fitz/filt_flate.c b/fitz/filt_flate.c
index 4d4caf37..b7ef163d 100644
--- a/fitz/filt_flate.c
+++ b/fitz/filt_flate.c
@@ -12,12 +12,12 @@ struct fz_flate_s
static void *zalloc(void *opaque, unsigned int items, unsigned int size)
{
- return fz_calloc(items, size);
+ return fz_calloc(opaque, items, size);
}
static void zfree(void *opaque, void *ptr)
{
- fz_free(ptr);
+ fz_free(opaque, ptr);
}
static int
@@ -77,7 +77,7 @@ close_flated(fz_stream *stm)
fz_warn("zlib error: inflateEnd: %s", state->z.msg);
fz_close(state->chain);
- fz_free(state);
+ fz_free(stm->ctx, state);
}
fz_stream *
@@ -86,12 +86,12 @@ fz_open_flated(fz_stream *chain)
fz_flate *state;
int code;
- state = fz_malloc(sizeof(fz_flate));
+ state = fz_malloc(chain->ctx, sizeof(fz_flate));
state->chain = chain;
state->z.zalloc = zalloc;
state->z.zfree = zfree;
- state->z.opaque = NULL;
+ state->z.opaque = chain->ctx;
state->z.next_in = NULL;
state->z.avail_in = 0;
@@ -99,5 +99,5 @@ fz_open_flated(fz_stream *chain)
if (code != Z_OK)
fz_warn("zlib error: inflateInit: %s", state->z.msg);
- return fz_new_stream(state, read_flated, close_flated);
+ return fz_new_stream(chain->ctx, state, read_flated, close_flated);
}
diff --git a/fitz/filt_jbig2d.c b/fitz/filt_jbig2d.c
index ad335045..18aa0a22 100644
--- a/fitz/filt_jbig2d.c
+++ b/fitz/filt_jbig2d.c
@@ -38,7 +38,7 @@ close_jbig2d(fz_stream *stm)
jbig2_global_ctx_free(state->gctx);
jbig2_ctx_free(state->ctx);
fz_close(state->chain);
- fz_free(state);
+ fz_free(stm->ctx, state);
}
static int
@@ -85,7 +85,7 @@ fz_open_jbig2d(fz_stream *chain, fz_buffer *globals)
{
fz_jbig2d *state;
- state = fz_malloc(sizeof(fz_jbig2d));
+ state = fz_malloc(chain->ctx, sizeof(fz_jbig2d));
state->chain = chain;
state->ctx = jbig2_ctx_new(NULL, JBIG2_OPTIONS_EMBEDDED, NULL, NULL, NULL);
state->gctx = NULL;
@@ -99,5 +99,5 @@ fz_open_jbig2d(fz_stream *chain, fz_buffer *globals)
state->ctx = jbig2_ctx_new(NULL, JBIG2_OPTIONS_EMBEDDED, state->gctx, NULL, NULL);
}
- return fz_new_stream(state, read_jbig2d, close_jbig2d);
+ return fz_new_stream(chain->ctx, state, read_jbig2d, close_jbig2d);
}
diff --git a/fitz/filt_jpxd.c b/fitz/filt_jpxd.c
index d646cabc..07d6a572 100644
--- a/fitz/filt_jpxd.c
+++ b/fitz/filt_jpxd.c
@@ -19,7 +19,7 @@ static void fz_opj_info_callback(const char *msg, void *client_data)
}
fz_error
-fz_load_jpx_image(fz_pixmap **imgp, unsigned char *data, int size, fz_colorspace *defcs)
+fz_load_jpx_image(fz_context *ctx, fz_pixmap **imgp, unsigned char *data, int size, fz_colorspace *defcs)
{
fz_pixmap *img;
opj_event_mgr_t evtmgr;
@@ -108,7 +108,7 @@ fz_load_jpx_image(fz_pixmap **imgp, unsigned char *data, int size, fz_colorspace
}
}
- img = fz_new_pixmap_with_limit(colorspace, w, h);
+ img = fz_new_pixmap_with_limit(ctx, colorspace, w, h);
if (!img)
{
opj_image_destroy(jpx);
@@ -138,9 +138,9 @@ fz_load_jpx_image(fz_pixmap **imgp, unsigned char *data, int size, fz_colorspace
{
if (n == 4)
{
- fz_pixmap *tmp = fz_new_pixmap(fz_device_rgb, w, h);
- fz_convert_pixmap(img, tmp);
- fz_drop_pixmap(img);
+ fz_pixmap *tmp = fz_new_pixmap(ctx, fz_device_rgb, w, h);
+ fz_convert_pixmap(ctx, img, tmp);
+ fz_drop_pixmap(ctx, img);
img = tmp;
}
fz_premultiply_pixmap(img);
diff --git a/fitz/filt_lzwd.c b/fitz/filt_lzwd.c
index 942e5837..b6534dfe 100644
--- a/fitz/filt_lzwd.c
+++ b/fitz/filt_lzwd.c
@@ -160,7 +160,7 @@ close_lzwd(fz_stream *stm)
{
fz_lzwd *lzw = stm->state;
fz_close(lzw->chain);
- fz_free(lzw);
+ fz_free(stm->ctx, lzw);
}
fz_stream *
@@ -170,14 +170,15 @@ fz_open_lzwd(fz_stream *chain, fz_obj *params)
fz_obj *obj;
int i;
- lzw = fz_malloc(sizeof(fz_lzwd));
+ assert(chain != NULL);
+ lzw = fz_malloc(chain->ctx, sizeof(fz_lzwd));
lzw->chain = chain;
lzw->eod = 0;
lzw->early_change = 1;
- obj = fz_dict_gets(params, "EarlyChange");
+ obj = fz_dict_gets(chain->ctx, params, "EarlyChange");
if (obj)
- lzw->early_change = !!fz_to_int(obj);
+ lzw->early_change = !!fz_to_int(chain->ctx, obj);
for (i = 0; i < 256; i++)
{
@@ -202,5 +203,5 @@ fz_open_lzwd(fz_stream *chain, fz_obj *params)
lzw->rp = lzw->bp;
lzw->wp = lzw->bp;
- return fz_new_stream(lzw, read_lzwd, close_lzwd);
+ return fz_new_stream(chain->ctx, lzw, read_lzwd, close_lzwd);
}
diff --git a/fitz/filt_predict.c b/fitz/filt_predict.c
index 17cfe905..fc22ed97 100644
--- a/fitz/filt_predict.c
+++ b/fitz/filt_predict.c
@@ -183,10 +183,10 @@ close_predict(fz_stream *stm)
{
fz_predict *state = stm->state;
fz_close(state->chain);
- fz_free(state->in);
- fz_free(state->out);
- fz_free(state->ref);
- fz_free(state);
+ fz_free(stm->ctx, state->in);
+ fz_free(stm->ctx, state->out);
+ fz_free(stm->ctx, state->ref);
+ fz_free(stm->ctx, state);
}
fz_stream *
@@ -194,8 +194,9 @@ fz_open_predict(fz_stream *chain, fz_obj *params)
{
fz_predict *state;
fz_obj *obj;
+ fz_context *ctx = chain->ctx;
- state = fz_malloc(sizeof(fz_predict));
+ state = fz_malloc(ctx, sizeof(fz_predict));
state->chain = chain;
state->predictor = 1;
@@ -203,9 +204,9 @@ fz_open_predict(fz_stream *chain, fz_obj *params)
state->colors = 1;
state->bpc = 8;
- obj = fz_dict_gets(params, "Predictor");
+ obj = fz_dict_gets(ctx, params, "Predictor");
if (obj)
- state->predictor = fz_to_int(obj);
+ state->predictor = fz_to_int(ctx, obj);
if (state->predictor != 1 && state->predictor != 2 &&
state->predictor != 10 && state->predictor != 11 &&
@@ -216,28 +217,28 @@ fz_open_predict(fz_stream *chain, fz_obj *params)
state->predictor = 1;
}
- obj = fz_dict_gets(params, "Columns");
+ obj = fz_dict_gets(ctx, params, "Columns");
if (obj)
- state->columns = fz_to_int(obj);
+ state->columns = fz_to_int(ctx, obj);
- obj = fz_dict_gets(params, "Colors");
+ obj = fz_dict_gets(ctx, params, "Colors");
if (obj)
- state->colors = fz_to_int(obj);
+ state->colors = fz_to_int(ctx, obj);
- obj = fz_dict_gets(params, "BitsPerComponent");
+ obj = fz_dict_gets(ctx, params, "BitsPerComponent");
if (obj)
- state->bpc = fz_to_int(obj);
+ state->bpc = fz_to_int(ctx, obj);
state->stride = (state->bpc * state->colors * state->columns + 7) / 8;
state->bpp = (state->bpc * state->colors + 7) / 8;
- state->in = fz_malloc(state->stride + 1);
- state->out = fz_malloc(state->stride);
- state->ref = fz_malloc(state->stride);
+ state->in = fz_malloc(ctx, state->stride + 1);
+ state->out = fz_malloc(ctx, state->stride);
+ state->ref = fz_malloc(ctx, state->stride);
state->rp = state->out;
state->wp = state->out;
memset(state->ref, 0, state->stride);
- return fz_new_stream(state, read_predict, close_predict);
+ return fz_new_stream(ctx, state, read_predict, close_predict);
}
diff --git a/fitz/fitz.h b/fitz/fitz.h
index afc92436..1311f019 100644
--- a/fitz/fitz.h
+++ b/fitz/fitz.h
@@ -62,6 +62,11 @@ int gettimeofday(struct timeval *tv, struct timezone *tz);
#define M_SQRT2 1.41421356237309504880
#endif
+/* Context types */
+typedef struct fz_except_context fz_except_context;
+typedef struct fz_alloc_context fz_alloc_context;
+typedef struct fz_context fz_context;
+
/*
* Variadic macros, inline and restrict keywords
*/
@@ -70,7 +75,7 @@ int gettimeofday(struct timeval *tv, struct timezone *tz);
#define fz_error_make(...) fz_error_make_imp(__FILE__, __LINE__, __func__, __VA_ARGS__)
#define fz_error_note(cause, ...) fz_error_note_imp(__FILE__, __LINE__, __func__, cause, __VA_ARGS__)
-#define fz_catch(cause, ...) fz_catch_imp(__FILE__, __LINE__, __func__, cause, __VA_ARGS__)
+#define fz_error_handle(cause, ...) fz_error_handle_imp(__FILE__, __LINE__, __func__, cause, __VA_ARGS__)
#elif _MSC_VER >= 1500 /* MSVC 9 or newer */
@@ -78,7 +83,7 @@ int gettimeofday(struct timeval *tv, struct timezone *tz);
#define restrict __restrict
#define fz_error_make(...) fz_error_make_imp(__FILE__, __LINE__, __FUNCTION__, __VA_ARGS__)
#define fz_error_note(cause, ...) fz_error_note_imp(__FILE__, __LINE__, __FUNCTION__, cause, __VA_ARGS__)
-#define fz_catch(cause, ...) fz_error_handle_imp(__FILE__, __LINE__, __FUNCTION__, cause, __VA_ARGS__)
+#define fz_error_handle(cause, ...) fz_error_handle_imp(__FILE__, __LINE__, __FUNCTION__, cause, __VA_ARGS__)
#elif __GNUC__ >= 3 /* GCC 3 or newer */
@@ -94,7 +99,7 @@ int gettimeofday(struct timeval *tv, struct timezone *tz);
#define restrict
#define fz_error_make fz_error_make_impx
#define fz_error_note fz_error_note_impx
-#define fz_error_handle fz_catch_impx
+#define fz_error_handle fz_error_handle_impx
#endif
@@ -139,11 +144,20 @@ char *fz_get_error_line(int n);
*/
/* memory allocation */
-void *fz_malloc(int size);
-void *fz_calloc(int count, int size);
-void *fz_realloc(void *p, int count, int size);
-void fz_free(void *p);
-char *fz_strdup(char *s);
+
+/* The following throw exceptions on failure to allocate */
+void *fz_malloc(fz_context *ctx, size_t size);
+void *fz_calloc(fz_context *ctx, size_t count, size_t size);
+void *fz_realloc(fz_context *ctx, void *p, size_t size);
+char *fz_strdup(fz_context *ctx, char *s);
+
+void fz_free(fz_context *ctx, void *p);
+
+/* The following returns NULL on failure to allocate */
+void *fz_malloc_nothrow(fz_context *ctx, size_t size);
+void *fz_calloc_nothrow(fz_context *ctx, size_t count, size_t size);
+void *fz_realloc_nothrow(fz_context *ctx, void *p, size_t size);
+char *fz_strdup_nothrow(fz_context *ctx, char *s);
/* runtime (hah!) test for endian-ness */
int fz_is_big_endian(void);
@@ -172,13 +186,13 @@ extern char *fz_optarg;
typedef struct fz_hash_table_s fz_hash_table;
-fz_hash_table *fz_new_hash_table(int initialsize, int keylen);
+fz_hash_table *fz_new_hash_table(fz_context *ctx, int initialsize, int keylen);
void fz_debug_hash(fz_hash_table *table);
void fz_empty_hash(fz_hash_table *table);
-void fz_free_hash(fz_hash_table *table);
+void fz_free_hash(fz_context *ctx, fz_hash_table *table);
void *fz_hash_find(fz_hash_table *table, void *key);
-void fz_hash_insert(fz_hash_table *table, void *key, void *val);
+void fz_hash_insert(fz_context *ctx, fz_hash_table *table, void *key, void *val);
void fz_hash_remove(fz_hash_table *table, void *key);
int fz_hash_len(fz_hash_table *table);
@@ -357,70 +371,70 @@ void aes_crypt_cbc( fz_aes *ctx, int mode, int length,
typedef struct fz_obj_s fz_obj;
-extern fz_obj* (*fz_resolve_indirect)(fz_obj*);
+#define fz_resolve_indirect(ctx, obj) (ctx)->fz_resolve_indirect((obj))
-fz_obj *fz_new_null(void);
-fz_obj *fz_new_bool(int b);
-fz_obj *fz_new_int(int i);
-fz_obj *fz_new_real(float f);
-fz_obj *fz_new_name(char *str);
-fz_obj *fz_new_string(char *str, int len);
-fz_obj *fz_new_indirect(int num, int gen, void *xref);
+fz_obj *fz_new_null(fz_context *ctx);
+fz_obj *fz_new_bool(fz_context *ctx, int b);
+fz_obj *fz_new_int(fz_context *ctx, int i);
+fz_obj *fz_new_real(fz_context *ctx, float f);
+fz_obj *fz_new_name(fz_context *ctx, char *str);
+fz_obj *fz_new_string(fz_context *ctx, char *str, int len);
+fz_obj *fz_new_indirect(fz_context *ctx, int num, int gen, void *xref);
-fz_obj *fz_new_array(int initialcap);
-fz_obj *fz_new_dict(int initialcap);
-fz_obj *fz_copy_array(fz_obj *array);
-fz_obj *fz_copy_dict(fz_obj *dict);
+fz_obj *fz_new_array(fz_context *ctx, int initialcap);
+fz_obj *fz_new_dict(fz_context *ctx, int initialcap);
+fz_obj *fz_copy_array(fz_context *ctx, fz_obj *array);
+fz_obj *fz_copy_dict(fz_context *ctx, fz_obj *dict);
fz_obj *fz_keep_obj(fz_obj *obj);
-void fz_drop_obj(fz_obj *obj);
+void fz_drop_obj(fz_context *ctx, fz_obj *obj);
/* type queries */
-int fz_is_null(fz_obj *obj);
-int fz_is_bool(fz_obj *obj);
-int fz_is_int(fz_obj *obj);
-int fz_is_real(fz_obj *obj);
-int fz_is_name(fz_obj *obj);
-int fz_is_string(fz_obj *obj);
-int fz_is_array(fz_obj *obj);
-int fz_is_dict(fz_obj *obj);
+int fz_is_null(fz_context *ctx, fz_obj *obj);
+int fz_is_bool(fz_context *ctx, fz_obj *obj);
+int fz_is_int(fz_context *ctx, fz_obj *obj);
+int fz_is_real(fz_context *ctx, fz_obj *obj);
+int fz_is_name(fz_context *ctx, fz_obj *obj);
+int fz_is_string(fz_context *ctx, fz_obj *obj);
+int fz_is_array(fz_context *ctx, fz_obj *obj);
+int fz_is_dict(fz_context *ctx, fz_obj *obj);
int fz_is_indirect(fz_obj *obj);
int fz_objcmp(fz_obj *a, fz_obj *b);
/* safe, silent failure, no error reporting */
-int fz_to_bool(fz_obj *obj);
-int fz_to_int(fz_obj *obj);
-float fz_to_real(fz_obj *obj);
-char *fz_to_name(fz_obj *obj);
-char *fz_to_str_buf(fz_obj *obj);
-int fz_to_str_len(fz_obj *obj);
+int fz_to_bool(fz_context *ctx, fz_obj *obj);
+int fz_to_int(fz_context *ctx, fz_obj *obj);
+float fz_to_real(fz_context *ctx, fz_obj *obj);
+char *fz_to_name(fz_context *ctx, fz_obj *obj);
+char *fz_to_str_buf(fz_context *ctx, fz_obj *obj);
+int fz_to_str_len(fz_context *ctx, fz_obj *obj);
int fz_to_num(fz_obj *obj);
int fz_to_gen(fz_obj *obj);
-int fz_array_len(fz_obj *array);
-fz_obj *fz_array_get(fz_obj *array, int i);
-void fz_array_put(fz_obj *array, int i, fz_obj *obj);
-void fz_array_push(fz_obj *array, fz_obj *obj);
-void fz_array_insert(fz_obj *array, fz_obj *obj);
-
-int fz_dict_len(fz_obj *dict);
-fz_obj *fz_dict_get_key(fz_obj *dict, int idx);
-fz_obj *fz_dict_get_val(fz_obj *dict, int idx);
-fz_obj *fz_dict_get(fz_obj *dict, fz_obj *key);
-fz_obj *fz_dict_gets(fz_obj *dict, char *key);
-fz_obj *fz_dict_getsa(fz_obj *dict, char *key, char *abbrev);
-void fz_dict_put(fz_obj *dict, fz_obj *key, fz_obj *val);
-void fz_dict_puts(fz_obj *dict, char *key, fz_obj *val);
-void fz_dict_del(fz_obj *dict, fz_obj *key);
-void fz_dict_dels(fz_obj *dict, char *key);
-void fz_sort_dict(fz_obj *dict);
-
-int fz_fprint_obj(FILE *fp, fz_obj *obj, int tight);
-void fz_debug_obj(fz_obj *obj);
-void fz_debug_ref(fz_obj *obj);
-
-void fz_set_str_len(fz_obj *obj, int newlen); /* private */
+int fz_array_len(fz_context *ctx, fz_obj *array);
+fz_obj *fz_array_get(fz_context *ctx, fz_obj *array, int i);
+void fz_array_put(fz_context *ctx, fz_obj *array, int i, fz_obj *obj);
+void fz_array_push(fz_context *ctx, fz_obj *array, fz_obj *obj);
+void fz_array_insert(fz_context *ctx, fz_obj *array, fz_obj *obj);
+
+int fz_dict_len(fz_context *ctx, fz_obj *dict);
+fz_obj *fz_dict_get_key(fz_context *ctx, fz_obj *dict, int idx);
+fz_obj *fz_dict_get_val(fz_context *ctx, fz_obj *dict, int idx);
+fz_obj *fz_dict_get(fz_context *ctx, fz_obj *dict, fz_obj *key);
+fz_obj *fz_dict_gets(fz_context *ctx, fz_obj *dict, char *key);
+fz_obj *fz_dict_getsa(fz_context *ctx, fz_obj *dict, char *key, char *abbrev);
+void fz_dict_put(fz_context *ctx, fz_obj *dict, fz_obj *key, fz_obj *val);
+void fz_dict_puts(fz_context *ctx, fz_obj *dict, char *key, fz_obj *val);
+void fz_dict_del(fz_context *ctx, fz_obj *dict, fz_obj *key);
+void fz_dict_dels(fz_context *ctx, fz_obj *dict, char *key);
+void fz_sort_dict(fz_context *ctx, fz_obj *dict);
+
+int fz_fprint_obj(fz_context *ctx, FILE *fp, fz_obj *obj, int tight);
+void fz_debug_obj(fz_context *ctx, fz_obj *obj);
+void fz_debug_ref(fz_context *ctx, fz_obj *obj);
+
+void fz_set_str_len(fz_context *ctx, fz_obj *obj, int newlen); /* private */
void *fz_get_indirect_xref(fz_obj *obj); /* private */
/*
@@ -436,12 +450,12 @@ struct fz_buffer_s
int cap, len;
};
-fz_buffer *fz_new_buffer(int size);
+fz_buffer *fz_new_buffer(fz_context *ctx, int size);
fz_buffer *fz_keep_buffer(fz_buffer *buf);
-void fz_drop_buffer(fz_buffer *buf);
+void fz_drop_buffer(fz_context *ctx, fz_buffer *buf);
-void fz_resize_buffer(fz_buffer *buf, int size);
-void fz_grow_buffer(fz_buffer *buf);
+void fz_resize_buffer(fz_context *ctx, fz_buffer *buf, int size);
+void fz_grow_buffer(fz_context *ctx, fz_buffer *buf);
/*
* Buffered reader.
@@ -460,20 +474,21 @@ struct fz_stream_s
int bits;
unsigned char *bp, *rp, *wp, *ep;
void *state;
+ fz_context *ctx;
int (*read)(fz_stream *stm, unsigned char *buf, int len);
void (*close)(fz_stream *stm);
void (*seek)(fz_stream *stm, int offset, int whence);
unsigned char buf[4096];
};
-fz_stream *fz_open_fd(int file);
-fz_stream *fz_open_file(const char *filename);
-fz_stream *fz_open_file_w(const wchar_t *filename); /* only on win32 */
-fz_stream *fz_open_buffer(fz_buffer *buf);
-fz_stream *fz_open_memory(unsigned char *data, int len);
+fz_stream *fz_open_fd(fz_context *ctx, int file);
+fz_stream *fz_open_file(fz_context *ctx, const char *filename);
+fz_stream *fz_open_file_w(fz_context *ctx, const wchar_t *filename); /* only on win32 */
+fz_stream *fz_open_buffer(fz_context *ctx, fz_buffer *buf);
+fz_stream *fz_open_memory(fz_context *ctx, unsigned char *data, int len);
void fz_close(fz_stream *stm);
-fz_stream *fz_new_stream(void*, int(*)(fz_stream*, unsigned char*, int), void(*)(fz_stream *));
+fz_stream *fz_new_stream(fz_context *ctx, void*, int(*)(fz_stream*, unsigned char*, int), void(*)(fz_stream *));
fz_stream *fz_keep_stream(fz_stream *stm);
void fz_fill_buffer(fz_stream *stm);
@@ -612,32 +627,32 @@ struct fz_pixmap_s
};
/* will return NULL if soft limit is exceeded */
-fz_pixmap *fz_new_pixmap_with_limit(fz_colorspace *colorspace, int w, int h);
+fz_pixmap *fz_new_pixmap_with_limit(fz_context *ctx, fz_colorspace *colorspace, int w, int h);
-fz_pixmap *fz_new_pixmap_with_data(fz_colorspace *colorspace, int w, int h, unsigned char *samples);
-fz_pixmap *fz_new_pixmap_with_rect(fz_colorspace *, fz_bbox bbox);
-fz_pixmap *fz_new_pixmap_with_rect_and_data(fz_colorspace *, fz_bbox bbox, unsigned char *samples);
-fz_pixmap *fz_new_pixmap(fz_colorspace *, int w, int h);
+fz_pixmap *fz_new_pixmap_with_data(fz_context *ctx, fz_colorspace *colorspace, int w, int h, unsigned char *samples);
+fz_pixmap *fz_new_pixmap_with_rect(fz_context *ctx, fz_colorspace *, fz_bbox bbox);
+fz_pixmap *fz_new_pixmap_with_rect_and_data(fz_context *ctx, fz_colorspace *, fz_bbox bbox, unsigned char *samples);
+fz_pixmap *fz_new_pixmap(fz_context *ctx, fz_colorspace *, int w, int h);
fz_pixmap *fz_keep_pixmap(fz_pixmap *pix);
-void fz_drop_pixmap(fz_pixmap *pix);
+void fz_drop_pixmap(fz_context *ctx, fz_pixmap *pix);
void fz_clear_pixmap(fz_pixmap *pix);
void fz_clear_pixmap_with_color(fz_pixmap *pix, int value);
void fz_clear_pixmap_rect_with_color(fz_pixmap *pix, int value, fz_bbox r);
void fz_copy_pixmap_rect(fz_pixmap *dest, fz_pixmap *src, fz_bbox r);
void fz_premultiply_pixmap(fz_pixmap *pix);
-fz_pixmap *fz_alpha_from_gray(fz_pixmap *gray, int luminosity);
+fz_pixmap *fz_alpha_from_gray(fz_context *ctx, fz_pixmap *gray, int luminosity);
fz_bbox fz_bound_pixmap(fz_pixmap *pix);
void fz_invert_pixmap(fz_pixmap *pix);
void fz_gamma_pixmap(fz_pixmap *pix, float gamma);
-fz_pixmap *fz_scale_pixmap(fz_pixmap *src, float x, float y, float w, float h);
-fz_pixmap *fz_scale_pixmap_gridfit(fz_pixmap *src, float x, float y, float w, float h, int gridfit);
+fz_pixmap *fz_scale_pixmap(fz_context *ctx, fz_pixmap *src, float x, float y, float w, float h);
+fz_pixmap *fz_scale_pixmap_gridfit(fz_context *ctx, fz_pixmap *src, float x, float y, float w, float h, int gridfit);
fz_error fz_write_pnm(fz_pixmap *pixmap, char *filename);
fz_error fz_write_pam(fz_pixmap *pixmap, char *filename, int savealpha);
-fz_error fz_write_png(fz_pixmap *pixmap, char *filename, int savealpha);
+fz_error fz_write_png(fz_context *ctx, fz_pixmap *pixmap, char *filename, int savealpha);
-fz_error fz_load_jpx_image(fz_pixmap **imgp, unsigned char *data, int size, fz_colorspace *dcs);
+fz_error fz_load_jpx_image(fz_context *ctx, fz_pixmap **imgp, unsigned char *data, int size, fz_colorspace *dcs);
/*
* Bitmaps have 1 component per bit. Only used for creating halftoned versions
@@ -654,10 +669,10 @@ struct fz_bitmap_s
unsigned char *samples;
};
-fz_bitmap *fz_new_bitmap(int w, int h, int n);
+fz_bitmap *fz_new_bitmap(fz_context *ctx, int w, int h, int n);
fz_bitmap *fz_keep_bitmap(fz_bitmap *bit);
void fz_clear_bitmap(fz_bitmap *bit);
-void fz_drop_bitmap(fz_bitmap *bit);
+void fz_drop_bitmap(fz_context *ctx, fz_bitmap *bit);
fz_error fz_write_pbm(fz_bitmap *bitmap, char *filename);
@@ -675,12 +690,12 @@ struct fz_halftone_s
fz_pixmap *comp[1];
};
-fz_halftone *fz_new_halftone(int num_comps);
-fz_halftone *fz_get_default_halftone(int num_comps);
+fz_halftone *fz_new_halftone(fz_context *ctx, int num_comps);
+fz_halftone *fz_get_default_halftone(fz_context *ctx, int num_comps);
fz_halftone *fz_keep_halftone(fz_halftone *half);
-void fz_drop_halftone(fz_halftone *half);
+void fz_drop_halftone(fz_context *ctx, fz_halftone *half);
-fz_bitmap *fz_halftone_pixmap(fz_pixmap *pix, fz_halftone *ht);
+fz_bitmap *fz_halftone_pixmap(fz_context *ctx, fz_pixmap *pix, fz_halftone *ht);
/*
* Colorspace resources.
@@ -698,16 +713,16 @@ struct fz_colorspace_s
int n;
void (*to_rgb)(fz_colorspace *, float *src, float *rgb);
void (*from_rgb)(fz_colorspace *, float *rgb, float *dst);
- void (*free_data)(fz_colorspace *);
+ void (*free_data)(fz_context *Ctx, fz_colorspace *);
void *data;
};
-fz_colorspace *fz_new_colorspace(char *name, int n);
+fz_colorspace *fz_new_colorspace(fz_context *ctx, char *name, int n);
fz_colorspace *fz_keep_colorspace(fz_colorspace *colorspace);
-void fz_drop_colorspace(fz_colorspace *colorspace);
+void fz_drop_colorspace(fz_context *ctx, fz_colorspace *colorspace);
void fz_convert_color(fz_colorspace *srcs, float *srcv, fz_colorspace *dsts, float *dstv);
-void fz_convert_pixmap(fz_pixmap *src, fz_pixmap *dst);
+void fz_convert_pixmap(fz_context *ctx, fz_pixmap *src, fz_pixmap *dst);
fz_colorspace *fz_find_device_colorspace(char *name);
@@ -753,13 +768,13 @@ struct fz_font_s
int *width_table;
};
-fz_font *fz_new_type3_font(char *name, fz_matrix matrix);
+fz_font *fz_new_type3_font(fz_context *ctx, char *name, fz_matrix matrix);
-fz_error fz_new_font_from_memory(fz_font **fontp, unsigned char *data, int len, int index);
-fz_error fz_new_font_from_file(fz_font **fontp, char *path, int index);
+fz_error fz_new_font_from_memory(fz_context *ctx, fz_font **fontp, unsigned char *data, int len, int index);
+fz_error fz_new_font_from_file(fz_context *ctx, fz_font **fontp, char *path, int index);
fz_font *fz_keep_font(fz_font *font);
-void fz_drop_font(fz_font *font);
+void fz_drop_font(fz_context *ctx, fz_font *font);
void fz_debug_font(fz_font *font);
void fz_set_font_bbox(fz_font *font, float xmin, float ymin, float xmax, float ymax);
@@ -809,18 +824,18 @@ struct fz_stroke_state_s
float dash_list[32];
};
-fz_path *fz_new_path(void);
-void fz_moveto(fz_path*, float x, float y);
-void fz_lineto(fz_path*, float x, float y);
-void fz_curveto(fz_path*, float, float, float, float, float, float);
-void fz_curvetov(fz_path*, float, float, float, float);
-void fz_curvetoy(fz_path*, float, float, float, float);
-void fz_closepath(fz_path*);
-void fz_free_path(fz_path *path);
+fz_path *fz_new_path(fz_context *ctx);
+void fz_moveto(fz_context*, fz_path*, float x, float y);
+void fz_lineto(fz_context*, fz_path*, float x, float y);
+void fz_curveto(fz_context*,fz_path*, float, float, float, float, float, float);
+void fz_curvetov(fz_context*,fz_path*, float, float, float, float);
+void fz_curvetoy(fz_context*,fz_path*, float, float, float, float);
+void fz_closepath(fz_context*,fz_path*);
+void fz_free_path(fz_context *ctx, fz_path *path);
void fz_transform_path(fz_path *path, fz_matrix transform);
-fz_path *fz_clone_path(fz_path *old);
+fz_path *fz_clone_path(fz_context *ctx, fz_path *old);
fz_rect fz_bound_path(fz_path *path, fz_stroke_state *stroke, fz_matrix ctm);
void fz_debug_path(fz_path *, int indent);
@@ -856,12 +871,12 @@ struct fz_text_s
fz_text_item *items;
};
-fz_text *fz_new_text(fz_font *face, fz_matrix trm, int wmode);
-void fz_add_text(fz_text *text, int gid, int ucs, float x, float y);
-void fz_free_text(fz_text *text);
+fz_text *fz_new_text(fz_context *ctx, fz_font *face, fz_matrix trm, int wmode);
+void fz_add_text(fz_context *ctx, fz_text *text, int gid, int ucs, float x, float y);
+void fz_free_text(fz_context *ctx, fz_text *text);
void fz_debug_text(fz_text*, int indent);
fz_rect fz_bound_text(fz_text *text, fz_matrix ctm);
-fz_text *fz_clone_text(fz_text *old);
+fz_text *fz_clone_text(fz_context *ctx, fz_text *old);
/*
* The shading code uses gouraud shaded triangle meshes.
@@ -899,11 +914,11 @@ struct fz_shade_s
};
fz_shade *fz_keep_shade(fz_shade *shade);
-void fz_drop_shade(fz_shade *shade);
+void fz_drop_shade(fz_context *ctx, fz_shade *shade);
void fz_debug_shade(fz_shade *shade);
fz_rect fz_bound_shade(fz_shade *shade, fz_matrix ctm);
-void fz_paint_shade(fz_shade *shade, fz_matrix ctm, fz_pixmap *dest, fz_bbox bbox);
+void fz_paint_shade(fz_context *ctx, fz_shade *shade, fz_matrix ctm, fz_pixmap *dest, fz_bbox bbox);
/*
* Glyph cache
@@ -911,13 +926,13 @@ void fz_paint_shade(fz_shade *shade, fz_matrix ctm, fz_pixmap *dest, fz_bbox bbo
typedef struct fz_glyph_cache_s fz_glyph_cache;
-fz_glyph_cache *fz_new_glyph_cache(void);
-fz_pixmap *fz_render_ft_glyph(fz_font *font, int cid, fz_matrix trm);
-fz_pixmap *fz_render_t3_glyph(fz_font *font, int cid, fz_matrix trm, fz_colorspace *model);
-fz_pixmap *fz_render_ft_stroked_glyph(fz_font *font, int gid, fz_matrix trm, fz_matrix ctm, fz_stroke_state *state);
-fz_pixmap *fz_render_glyph(fz_glyph_cache*, fz_font*, int, fz_matrix, fz_colorspace *model);
-fz_pixmap *fz_render_stroked_glyph(fz_glyph_cache*, fz_font*, int, fz_matrix, fz_matrix, fz_stroke_state *stroke);
-void fz_free_glyph_cache(fz_glyph_cache *);
+fz_glyph_cache *fz_new_glyph_cache(fz_context *ctx);
+fz_pixmap *fz_render_ft_glyph(fz_context *ctx, fz_font *font, int cid, fz_matrix trm);
+fz_pixmap *fz_render_t3_glyph(fz_context *ctx, fz_font *font, int cid, fz_matrix trm, fz_colorspace *model);
+fz_pixmap *fz_render_ft_stroked_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm, fz_matrix ctm, fz_stroke_state *state);
+fz_pixmap *fz_render_glyph(fz_context *ctx, fz_glyph_cache*, fz_font*, int, fz_matrix, fz_colorspace *model);
+fz_pixmap *fz_render_stroked_glyph(fz_context *ctx, fz_glyph_cache*, fz_font*, int, fz_matrix, fz_matrix, fz_stroke_state *stroke);
+void fz_free_glyph_cache(fz_context *ctx, fz_glyph_cache *);
/*
* Scan converter
@@ -928,7 +943,7 @@ void fz_set_aa_level(int bits);
typedef struct fz_gel_s fz_gel;
-fz_gel *fz_new_gel(void);
+fz_gel *fz_new_gel(fz_context *ctx);
void fz_insert_gel(fz_gel *gel, float x0, float y0, float x1, float y1);
void fz_reset_gel(fz_gel *gel, fz_bbox clip);
void fz_sort_gel(fz_gel *gel);
@@ -965,33 +980,34 @@ struct fz_device_s
int flags;
void *user;
- void (*free_user)(void *);
-
- void (*fill_path)(void *, fz_path *, int even_odd, fz_matrix, fz_colorspace *, float *color, float alpha);
- void (*stroke_path)(void *, fz_path *, fz_stroke_state *, fz_matrix, fz_colorspace *, float *color, float alpha);
- void (*clip_path)(void *, fz_path *, fz_rect *rect, int even_odd, fz_matrix);
- void (*clip_stroke_path)(void *, fz_path *, fz_rect *rect, fz_stroke_state *, fz_matrix);
-
- void (*fill_text)(void *, fz_text *, fz_matrix, fz_colorspace *, float *color, float alpha);
- void (*stroke_text)(void *, fz_text *, fz_stroke_state *, fz_matrix, fz_colorspace *, float *color, float alpha);
- void (*clip_text)(void *, fz_text *, fz_matrix, int accumulate);
- void (*clip_stroke_text)(void *, fz_text *, fz_stroke_state *, fz_matrix);
- void (*ignore_text)(void *, fz_text *, fz_matrix);
-
- void (*fill_shade)(void *, fz_shade *shd, fz_matrix ctm, float alpha);
- void (*fill_image)(void *, fz_pixmap *img, fz_matrix ctm, float alpha);
- void (*fill_image_mask)(void *, fz_pixmap *img, fz_matrix ctm, fz_colorspace *, float *color, float alpha);
- void (*clip_image_mask)(void *, fz_pixmap *img, fz_rect *rect, fz_matrix ctm);
-
- void (*pop_clip)(void *);
-
- void (*begin_mask)(void *, fz_rect, int luminosity, fz_colorspace *, float *bc);
- void (*end_mask)(void *);
- void (*begin_group)(void *, fz_rect, int isolated, int knockout, int blendmode, float alpha);
- void (*end_group)(void *);
-
- void (*begin_tile)(void *, fz_rect area, fz_rect view, float xstep, float ystep, fz_matrix ctm);
- void (*end_tile)(void *);
+ void (*free_user)(fz_device *);
+ fz_context *ctx;
+
+ void (*fill_path)(fz_device *, fz_path *, int even_odd, fz_matrix, fz_colorspace *, float *color, float alpha);
+ void (*stroke_path)(fz_device *, fz_path *, fz_stroke_state *, fz_matrix, fz_colorspace *, float *color, float alpha);
+ void (*clip_path)(fz_device *, fz_path *, fz_rect *rect, int even_odd, fz_matrix);
+ void (*clip_stroke_path)(fz_device *, fz_path *, fz_rect *rect, fz_stroke_state *, fz_matrix);
+
+ void (*fill_text)(fz_device *, fz_text *, fz_matrix, fz_colorspace *, float *color, float alpha);
+ void (*stroke_text)(fz_device *, fz_text *, fz_stroke_state *, fz_matrix, fz_colorspace *, float *color, float alpha);
+ void (*clip_text)(fz_device *, fz_text *, fz_matrix, int accumulate);
+ void (*clip_stroke_text)(fz_device *, fz_text *, fz_stroke_state *, fz_matrix);
+ void (*ignore_text)(fz_device *, fz_text *, fz_matrix);
+
+ void (*fill_shade)(fz_device *, fz_shade *shd, fz_matrix ctm, float alpha);
+ void (*fill_image)(fz_device *, fz_pixmap *img, fz_matrix ctm, float alpha);
+ void (*fill_image_mask)(fz_device *, fz_pixmap *img, fz_matrix ctm, fz_colorspace *, float *color, float alpha);
+ void (*clip_image_mask)(fz_device *, fz_pixmap *img, fz_rect *rect, fz_matrix ctm);
+
+ void (*pop_clip)(fz_device *);
+
+ void (*begin_mask)(fz_device *, fz_rect, int luminosity, fz_colorspace *, float *bc);
+ void (*end_mask)(fz_device *);
+ void (*begin_group)(fz_device *, fz_rect, int isolated, int knockout, int blendmode, float alpha);
+ void (*end_group)(fz_device *);
+
+ void (*begin_tile)(fz_device *, fz_rect area, fz_rect view, float xstep, float ystep, fz_matrix ctm);
+ void (*end_tile)(fz_device *);
};
void fz_fill_path(fz_device *dev, fz_path *path, int even_odd, fz_matrix ctm, fz_colorspace *colorspace, float *color, float alpha);
@@ -1015,13 +1031,13 @@ void fz_end_group(fz_device *dev);
void fz_begin_tile(fz_device *dev, fz_rect area, fz_rect view, float xstep, float ystep, fz_matrix ctm);
void fz_end_tile(fz_device *dev);
-fz_device *fz_new_device(void *user);
+fz_device *fz_new_device(fz_context *ctx, void *user);
void fz_free_device(fz_device *dev);
-fz_device *fz_new_trace_device(void);
-fz_device *fz_new_bbox_device(fz_bbox *bboxp);
-fz_device *fz_new_draw_device(fz_glyph_cache *cache, fz_pixmap *dest);
-fz_device *fz_new_draw_device_type3(fz_glyph_cache *cache, fz_pixmap *dest);
+fz_device *fz_new_trace_device(fz_context *ctx);
+fz_device *fz_new_bbox_device(fz_context *ctx, fz_bbox *bboxp);
+fz_device *fz_new_draw_device(fz_context *ctx, fz_glyph_cache *cache, fz_pixmap *dest);
+fz_device *fz_new_draw_device_type3(fz_context *ctx, fz_glyph_cache *cache, fz_pixmap *dest);
/*
* Text extraction device
@@ -1047,12 +1063,12 @@ struct fz_text_span_s
int eol;
};
-fz_text_span *fz_new_text_span(void);
-void fz_free_text_span(fz_text_span *line);
+fz_text_span *fz_new_text_span(fz_context *ctx);
+void fz_free_text_span(fz_context *ctx, fz_text_span *line);
void fz_debug_text_span(fz_text_span *line);
void fz_debug_text_span_xml(fz_text_span *span);
-fz_device *fz_new_text_device(fz_text_span *text);
+fz_device *fz_new_text_device(fz_context *ctx, fz_text_span *text);
/*
* Display list device -- record and play back device commands.
@@ -1060,9 +1076,9 @@ fz_device *fz_new_text_device(fz_text_span *text);
typedef struct fz_display_list_s fz_display_list;
-fz_display_list *fz_new_display_list(void);
-void fz_free_display_list(fz_display_list *list);
-fz_device *fz_new_list_device(fz_display_list *list);
+fz_display_list *fz_new_display_list(fz_context *ctx);
+void fz_free_display_list(fz_context *ctx, fz_display_list *list);
+fz_device *fz_new_list_device(fz_context *ctx, fz_display_list *list);
void fz_execute_display_list(fz_display_list *list, fz_device *dev, fz_matrix ctm, fz_bbox area);
/*
@@ -1119,17 +1135,29 @@ enum
FZ_BLEND_KNOCKOUT = 32
};
-/* Fitz context */
+/* Fitz allocator context */
-typedef struct fz_except_context fz_except_context;
-typedef struct fz_alloc_context fz_alloc_context;
+struct fz_alloc_context
+{
+ void *opaque;
+ void *(*malloc)(void *, size_t);
+ void *(*realloc)(void *, void *, size_t);
+ void (*free)(void *, void *);
+ void *(*calloc)(void *, size_t, size_t);
+};
-typedef struct fz_context
+extern fz_alloc_context fz_alloc_default;
+
+/* Fitz context */
+
+struct fz_context
{
- fz_except_context *except;
- fz_alloc_context *alloc;
-}
-fz_context;
+ fz_except_context *except;
+ fz_alloc_context *alloc;
+ fz_obj *(*fz_resolve_indirect)(fz_obj*);
+};
+fz_context *fz_context_init(fz_alloc_context *alloc);
+void fz_context_fin(fz_context *ctx);
#endif
diff --git a/fitz/memento.c b/fitz/memento.c
index 638bb2b2..a9885072 100644
--- a/fitz/memento.c
+++ b/fitz/memento.c
@@ -30,6 +30,7 @@
#define COMPILING_MEMENTO_C
#include "memento.h"
#include <stdio.h>
+#include <stdlib.h>
#if defined(__linux__)
#define MEMENTO_HAS_FORK
@@ -52,7 +53,13 @@ char *getenv(const char *);
#ifdef MEMENTO
+#ifdef HAVE_VALGRIND
#include "valgrind/memcheck.h"
+#else
+#define VALGRIND_MAKE_MEM_NOACCESS(p,s) do { } while (0==1)
+#define VALGRIND_MAKE_MEM_UNDEFINED(p,s) do { } while (0==1)
+#define VALGRIND_MAKE_MEM_DEFINED(p,s) do { } while (0==1)
+#endif
enum {
Memento_PreSize = 16,
diff --git a/fitz/obj_print.c b/fitz/obj_print.c
index 30b34d95..a6313e02 100644
--- a/fitz/obj_print.c
+++ b/fitz/obj_print.c
@@ -10,6 +10,7 @@ struct fmt
int col;
int sep;
int last;
+ fz_context *ctx;
};
static void fmt_obj(struct fmt *fmt, fz_obj *obj);
@@ -78,8 +79,8 @@ static inline void fmt_sep(struct fmt *fmt)
static void fmt_str(struct fmt *fmt, fz_obj *obj)
{
- char *s = fz_to_str_buf(obj);
- int n = fz_to_str_len(obj);
+ char *s = fz_to_str_buf(fmt->ctx, obj);
+ int n = fz_to_str_len(fmt->ctx, obj);
int i, c;
fmt_putc(fmt, '(');
@@ -114,8 +115,8 @@ static void fmt_str(struct fmt *fmt, fz_obj *obj)
static void fmt_hex(struct fmt *fmt, fz_obj *obj)
{
- char *s = fz_to_str_buf(obj);
- int n = fz_to_str_len(obj);
+ char *s = fz_to_str_buf(fmt->ctx, obj);
+ int n = fz_to_str_len(fmt->ctx, obj);
int i, b, c;
fmt_putc(fmt, '<');
@@ -131,7 +132,7 @@ static void fmt_hex(struct fmt *fmt, fz_obj *obj)
static void fmt_name(struct fmt *fmt, fz_obj *obj)
{
- unsigned char *s = (unsigned char *) fz_to_name(obj);
+ unsigned char *s = (unsigned char *) fz_to_name(fmt->ctx, obj);
int i, c;
fmt_putc(fmt, '/');
@@ -156,24 +157,26 @@ static void fmt_name(struct fmt *fmt, fz_obj *obj)
static void fmt_array(struct fmt *fmt, fz_obj *obj)
{
- int i;
+ int i, n;
+ fz_context *ctx = fmt->ctx;
+ n = fz_array_len(ctx, obj);
if (fmt->tight) {
fmt_putc(fmt, '[');
- for (i = 0; i < fz_array_len(obj); i++) {
- fmt_obj(fmt, fz_array_get(obj, i));
+ for (i = 0; i < n; i++) {
+ fmt_obj(fmt, fz_array_get(ctx, obj, i));
fmt_sep(fmt);
}
fmt_putc(fmt, ']');
}
else {
fmt_puts(fmt, "[ ");
- for (i = 0; i < fz_array_len(obj); i++) {
+ for (i = 0; i < n; i++) {
if (fmt->col > 60) {
fmt_putc(fmt, '\n');
fmt_indent(fmt);
}
- fmt_obj(fmt, fz_array_get(obj, i));
+ fmt_obj(fmt, fz_array_get(ctx, obj, i));
fmt_putc(fmt, ' ');
}
fmt_putc(fmt, ']');
@@ -183,15 +186,17 @@ static void fmt_array(struct fmt *fmt, fz_obj *obj)
static void fmt_dict(struct fmt *fmt, fz_obj *obj)
{
- int i;
+ int i, n;
fz_obj *key, *val;
+ fz_context *ctx = fmt->ctx;
+ n = fz_dict_len(ctx, obj);
if (fmt->tight) {
fmt_puts(fmt, "<<");
- for (i = 0; i < fz_dict_len(obj); i++) {
- fmt_obj(fmt, fz_dict_get_key(obj, i));
+ for (i = 0; i < n; i++) {
+ fmt_obj(fmt, fz_dict_get_key(ctx, obj, i));
fmt_sep(fmt);
- fmt_obj(fmt, fz_dict_get_val(obj, i));
+ fmt_obj(fmt, fz_dict_get_val(ctx, obj, i));
fmt_sep(fmt);
}
fmt_puts(fmt, ">>");
@@ -199,17 +204,17 @@ static void fmt_dict(struct fmt *fmt, fz_obj *obj)
else {
fmt_puts(fmt, "<<\n");
fmt->indent ++;
- for (i = 0; i < fz_dict_len(obj); i++) {
- key = fz_dict_get_key(obj, i);
- val = fz_dict_get_val(obj, i);
+ for (i = 0; i < n; i++) {
+ key = fz_dict_get_key(ctx, obj, i);
+ val = fz_dict_get_val(ctx, obj, i);
fmt_indent(fmt);
fmt_obj(fmt, key);
fmt_putc(fmt, ' ');
- if (!fz_is_indirect(val) && fz_is_array(val))
+ if (!fz_is_indirect(val) && fz_is_array(ctx, val))
fmt->indent ++;
fmt_obj(fmt, val);
fmt_putc(fmt, '\n');
- if (!fz_is_indirect(val) && fz_is_array(val))
+ if (!fz_is_indirect(val) && fz_is_array(ctx, val))
fmt->indent --;
}
fmt->indent --;
@@ -221,6 +226,7 @@ static void fmt_dict(struct fmt *fmt, fz_obj *obj)
static void fmt_obj(struct fmt *fmt, fz_obj *obj)
{
char buf[256];
+ fz_context *ctx = fmt->ctx;
if (!obj)
fmt_puts(fmt, "<NULL>");
@@ -229,26 +235,26 @@ static void fmt_obj(struct fmt *fmt, fz_obj *obj)
sprintf(buf, "%d %d R", fz_to_num(obj), fz_to_gen(obj));
fmt_puts(fmt, buf);
}
- else if (fz_is_null(obj))
+ else if (fz_is_null(ctx, obj))
fmt_puts(fmt, "null");
- else if (fz_is_bool(obj))
- fmt_puts(fmt, fz_to_bool(obj) ? "true" : "false");
- else if (fz_is_int(obj))
+ else if (fz_is_bool(ctx, obj))
+ fmt_puts(fmt, fz_to_bool(ctx, obj) ? "true" : "false");
+ else if (fz_is_int(ctx, obj))
{
- sprintf(buf, "%d", fz_to_int(obj));
+ sprintf(buf, "%d", fz_to_int(ctx, obj));
fmt_puts(fmt, buf);
}
- else if (fz_is_real(obj))
+ else if (fz_is_real(ctx, obj))
{
- sprintf(buf, "%g", fz_to_real(obj));
+ sprintf(buf, "%g", fz_to_real(ctx, obj));
if (strchr(buf, 'e')) /* bad news! */
- sprintf(buf, fabsf(fz_to_real(obj)) > 1 ? "%1.1f" : "%1.8f", fz_to_real(obj));
+ sprintf(buf, fabsf(fz_to_real(ctx, obj)) > 1 ? "%1.1f" : "%1.8f", fz_to_real(ctx, obj));
fmt_puts(fmt, buf);
}
- else if (fz_is_string(obj))
+ else if (fz_is_string(ctx, obj))
{
- char *str = fz_to_str_buf(obj);
- int len = fz_to_str_len(obj);
+ char *str = fz_to_str_buf(ctx, obj);
+ int len = fz_to_str_len(ctx, obj);
int added = 0;
int i, c;
for (i = 0; i < len; i++) {
@@ -263,18 +269,18 @@ static void fmt_obj(struct fmt *fmt, fz_obj *obj)
else
fmt_hex(fmt, obj);
}
- else if (fz_is_name(obj))
+ else if (fz_is_name(ctx, obj))
fmt_name(fmt, obj);
- else if (fz_is_array(obj))
+ else if (fz_is_array(ctx, obj))
fmt_array(fmt, obj);
- else if (fz_is_dict(obj))
+ else if (fz_is_dict(ctx, obj))
fmt_dict(fmt, obj);
else
fmt_puts(fmt, "<unknown object>");
}
static int
-fz_sprint_obj(char *s, int n, fz_obj *obj, int tight)
+fz_sprint_obj(fz_context *ctx, char *s, int n, fz_obj *obj, int tight)
{
struct fmt fmt;
@@ -287,6 +293,7 @@ fz_sprint_obj(char *s, int n, fz_obj *obj, int tight)
fmt.buf = s;
fmt.cap = n;
fmt.len = 0;
+ fmt.ctx = ctx;
fmt_obj(&fmt, obj);
if (fmt.buf && fmt.len < fmt.cap)
@@ -296,40 +303,40 @@ fz_sprint_obj(char *s, int n, fz_obj *obj, int tight)
}
int
-fz_fprint_obj(FILE *fp, fz_obj *obj, int tight)
+fz_fprint_obj(fz_context *ctx, FILE *fp, fz_obj *obj, int tight)
{
char buf[1024];
char *ptr;
int n;
- n = fz_sprint_obj(NULL, 0, obj, tight);
+ n = fz_sprint_obj(ctx, NULL, 0, obj, tight);
if ((n + 1) < sizeof buf)
{
- fz_sprint_obj(buf, sizeof buf, obj, tight);
+ fz_sprint_obj(ctx, buf, sizeof buf, obj, tight);
fputs(buf, fp);
fputc('\n', fp);
}
else
{
- ptr = fz_malloc(n + 1);
- fz_sprint_obj(ptr, n + 1, obj, tight);
+ ptr = fz_malloc(ctx, n + 1);
+ fz_sprint_obj(ctx, ptr, n + 1, obj, tight);
fputs(ptr, fp);
fputc('\n', fp);
- fz_free(ptr);
+ fz_free(ctx, ptr);
}
return n;
}
void
-fz_debug_obj(fz_obj *obj)
+fz_debug_obj(fz_context *ctx, fz_obj *obj)
{
- fz_fprint_obj(stdout, obj, 0);
+ fz_fprint_obj(ctx, stdout, obj, 0);
}
void
-fz_debug_ref(fz_obj *ref)
+fz_debug_ref(fz_context *ctx, fz_obj *ref)
{
fz_obj *obj;
- obj = fz_resolve_indirect(ref);
- fz_debug_obj(obj);
+ obj = fz_resolve_indirect(ctx, ref);
+ fz_debug_obj(ctx, obj);
}
diff --git a/fitz/res_bitmap.c b/fitz/res_bitmap.c
index a50d0952..2ca66efe 100644
--- a/fitz/res_bitmap.c
+++ b/fitz/res_bitmap.c
@@ -1,11 +1,11 @@
#include "fitz.h"
fz_bitmap *
-fz_new_bitmap(int w, int h, int n)
+fz_new_bitmap(fz_context *ctx, int w, int h, int n)
{
fz_bitmap *bit;
- bit = fz_malloc(sizeof(fz_bitmap));
+ bit = fz_malloc(ctx, sizeof(fz_bitmap));
bit->refs = 1;
bit->w = w;
bit->h = h;
@@ -14,7 +14,7 @@ fz_new_bitmap(int w, int h, int n)
* use SSE2 etc. */
bit->stride = ((n * w + 31) & ~31) >> 3;
- bit->samples = fz_calloc(h, bit->stride);
+ bit->samples = fz_calloc(ctx, h, bit->stride);
return bit;
}
@@ -27,12 +27,12 @@ fz_keep_bitmap(fz_bitmap *pix)
}
void
-fz_drop_bitmap(fz_bitmap *bit)
+fz_drop_bitmap(fz_context *ctx, fz_bitmap *bit)
{
if (bit && --bit->refs == 0)
{
- fz_free(bit->samples);
- fz_free(bit);
+ fz_free(ctx, bit->samples);
+ fz_free(ctx, bit);
}
}
diff --git a/fitz/res_colorspace.c b/fitz/res_colorspace.c
index 47df2b34..3f5cd47e 100644
--- a/fitz/res_colorspace.c
+++ b/fitz/res_colorspace.c
@@ -3,9 +3,9 @@
#define SLOWCMYK
fz_colorspace *
-fz_new_colorspace(char *name, int n)
+fz_new_colorspace(fz_context *ctx, char *name, int n)
{
- fz_colorspace *cs = fz_malloc(sizeof(fz_colorspace));
+ fz_colorspace *cs = fz_malloc(ctx, sizeof(fz_colorspace));
cs->refs = 1;
fz_strlcpy(cs->name, name, sizeof cs->name);
cs->n = n;
@@ -26,15 +26,15 @@ fz_keep_colorspace(fz_colorspace *cs)
}
void
-fz_drop_colorspace(fz_colorspace *cs)
+fz_drop_colorspace(fz_context *ctx, fz_colorspace *cs)
{
if (cs && cs->refs < 0)
return;
if (cs && --cs->refs == 0)
{
if (cs->free_data && cs->data)
- cs->free_data(cs);
- fz_free(cs);
+ cs->free_data(ctx, cs);
+ fz_free(ctx, cs);
}
}
@@ -372,7 +372,7 @@ static void fast_rgb_to_bgr(fz_pixmap *src, fz_pixmap *dst)
}
static void
-fz_std_conv_pixmap(fz_pixmap *src, fz_pixmap *dst)
+fz_std_conv_pixmap(fz_context *ctx, fz_pixmap *src, fz_pixmap *dst)
{
float srcv[FZ_MAX_COLORS];
float dstv[FZ_MAX_COLORS];
@@ -464,7 +464,7 @@ fz_std_conv_pixmap(fz_pixmap *src, fz_pixmap *dst)
fz_hash_table *lookup;
unsigned char *color;
- lookup = fz_new_hash_table(509, srcn);
+ lookup = fz_new_hash_table(ctx, 509, srcn);
for (y = 0; y < src->h; y++)
{
@@ -486,19 +486,19 @@ fz_std_conv_pixmap(fz_pixmap *src, fz_pixmap *dst)
for (k = 0; k < dstn; k++)
*d++ = dstv[k] * 255;
- fz_hash_insert(lookup, s - srcn, d - dstn);
+ fz_hash_insert(ctx, lookup, s - srcn, d - dstn);
*d++ = *s++;
}
}
}
- fz_free_hash(lookup);
+ fz_free_hash(ctx, lookup);
}
}
void
-fz_convert_pixmap(fz_pixmap *sp, fz_pixmap *dp)
+fz_convert_pixmap(fz_context *ctx, fz_pixmap *sp, fz_pixmap *dp)
{
fz_colorspace *ss = sp->colorspace;
fz_colorspace *ds = dp->colorspace;
@@ -514,7 +514,7 @@ fz_convert_pixmap(fz_pixmap *sp, fz_pixmap *dp)
if (ds == fz_device_rgb) fast_gray_to_rgb(sp, dp);
else if (ds == fz_device_bgr) fast_gray_to_rgb(sp, dp); /* bgr == rgb here */
else if (ds == fz_device_cmyk) fast_gray_to_cmyk(sp, dp);
- else fz_std_conv_pixmap(sp, dp);
+ else fz_std_conv_pixmap(ctx, sp, dp);
}
else if (ss == fz_device_rgb)
@@ -522,7 +522,7 @@ fz_convert_pixmap(fz_pixmap *sp, fz_pixmap *dp)
if (ds == fz_device_gray) fast_rgb_to_gray(sp, dp);
else if (ds == fz_device_bgr) fast_rgb_to_bgr(sp, dp);
else if (ds == fz_device_cmyk) fast_rgb_to_cmyk(sp, dp);
- else fz_std_conv_pixmap(sp, dp);
+ else fz_std_conv_pixmap(ctx, sp, dp);
}
else if (ss == fz_device_bgr)
@@ -530,7 +530,7 @@ fz_convert_pixmap(fz_pixmap *sp, fz_pixmap *dp)
if (ds == fz_device_gray) fast_bgr_to_gray(sp, dp);
else if (ds == fz_device_rgb) fast_rgb_to_bgr(sp, dp); /* bgr = rgb here */
else if (ds == fz_device_cmyk) fast_bgr_to_cmyk(sp, dp);
- else fz_std_conv_pixmap(sp, dp);
+ else fz_std_conv_pixmap(ctx, sp, dp);
}
else if (ss == fz_device_cmyk)
@@ -538,10 +538,10 @@ fz_convert_pixmap(fz_pixmap *sp, fz_pixmap *dp)
if (ds == fz_device_gray) fast_cmyk_to_gray(sp, dp);
else if (ds == fz_device_bgr) fast_cmyk_to_bgr(sp, dp);
else if (ds == fz_device_rgb) fast_cmyk_to_rgb(sp, dp);
- else fz_std_conv_pixmap(sp, dp);
+ else fz_std_conv_pixmap(ctx, sp, dp);
}
- else fz_std_conv_pixmap(sp, dp);
+ else fz_std_conv_pixmap(ctx, sp, dp);
}
/* Convert a single color */
diff --git a/fitz/res_font.c b/fitz/res_font.c
index c78b5995..f3018fcb 100644
--- a/fitz/res_font.c
+++ b/fitz/res_font.c
@@ -7,11 +7,11 @@
static void fz_finalize_freetype(void);
static fz_font *
-fz_new_font(char *name)
+fz_new_font(fz_context *ctx, char *name)
{
fz_font *font;
- font = fz_malloc(sizeof(fz_font));
+ font = fz_malloc(ctx, sizeof(fz_font));
font->refs = 1;
if (name)
@@ -55,7 +55,7 @@ fz_keep_font(fz_font *font)
}
void
-fz_drop_font(fz_font *font)
+fz_drop_font(fz_context *ctx, fz_font *font)
{
int fterr;
int i;
@@ -65,12 +65,12 @@ fz_drop_font(fz_font *font)
if (font->t3procs)
{
if (font->t3resources)
- fz_drop_obj(font->t3resources);
+ fz_drop_obj(ctx, font->t3resources);
for (i = 0; i < 256; i++)
if (font->t3procs[i])
- fz_drop_buffer(font->t3procs[i]);
- fz_free(font->t3procs);
- fz_free(font->t3widths);
+ fz_drop_buffer(ctx, font->t3procs[i]);
+ fz_free(ctx, font->t3procs);
+ fz_free(ctx, font->t3widths);
}
if (font->ft_face)
@@ -82,14 +82,14 @@ fz_drop_font(fz_font *font)
}
if (font->ft_file)
- fz_free(font->ft_file);
+ fz_free(ctx, font->ft_file);
if (font->ft_data)
- fz_free(font->ft_data);
+ fz_free(ctx, font->ft_data);
if (font->width_table)
- fz_free(font->width_table);
+ fz_free(ctx, font->width_table);
- fz_free(font);
+ fz_free(ctx, font);
}
}
@@ -180,7 +180,7 @@ fz_finalize_freetype(void)
}
fz_error
-fz_new_font_from_file(fz_font **fontp, char *path, int index)
+fz_new_font_from_file(fz_context *ctx, fz_font **fontp, char *path, int index)
{
FT_Face face;
fz_error error;
@@ -195,7 +195,7 @@ fz_new_font_from_file(fz_font **fontp, char *path, int index)
if (fterr)
return fz_error_make("freetype: cannot load font: %s", ft_error_string(fterr));
- font = fz_new_font(face->family_name);
+ font = fz_new_font(ctx, face->family_name);
font->ft_face = face;
font->bbox.x0 = face->bbox.xMin * 1000 / face->units_per_EM;
font->bbox.y0 = face->bbox.yMin * 1000 / face->units_per_EM;
@@ -207,7 +207,7 @@ fz_new_font_from_file(fz_font **fontp, char *path, int index)
}
fz_error
-fz_new_font_from_memory(fz_font **fontp, unsigned char *data, int len, int index)
+fz_new_font_from_memory(fz_context *ctx, fz_font **fontp, unsigned char *data, int len, int index)
{
FT_Face face;
fz_error error;
@@ -222,7 +222,7 @@ fz_new_font_from_memory(fz_font **fontp, unsigned char *data, int len, int index
if (fterr)
return fz_error_make("freetype: cannot load font: %s", ft_error_string(fterr));
- font = fz_new_font(face->family_name);
+ font = fz_new_font(ctx, face->family_name);
font->ft_face = face;
font->bbox.x0 = face->bbox.xMin * 1000 / face->units_per_EM;
font->bbox.y0 = face->bbox.yMin * 1000 / face->units_per_EM;
@@ -268,12 +268,12 @@ fz_adjust_ft_glyph_width(fz_font *font, int gid, fz_matrix trm)
}
static fz_pixmap *
-fz_copy_ft_bitmap(int left, int top, FT_Bitmap *bitmap)
+fz_copy_ft_bitmap(fz_context *ctx, int left, int top, FT_Bitmap *bitmap)
{
fz_pixmap *pixmap;
int y;
- pixmap = fz_new_pixmap(NULL, bitmap->width, bitmap->rows);
+ pixmap = fz_new_pixmap(ctx, NULL, bitmap->width, bitmap->rows);
pixmap->x = left;
pixmap->y = top - bitmap->rows;
@@ -311,7 +311,7 @@ fz_copy_ft_bitmap(int left, int top, FT_Bitmap *bitmap)
}
fz_pixmap *
-fz_render_ft_glyph(fz_font *font, int gid, fz_matrix trm)
+fz_render_ft_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm)
{
FT_Face face = font->ft_face;
FT_Matrix m;
@@ -399,11 +399,11 @@ fz_render_ft_glyph(fz_font *font, int gid, fz_matrix trm)
return NULL;
}
- return fz_copy_ft_bitmap(face->glyph->bitmap_left, face->glyph->bitmap_top, &face->glyph->bitmap);
+ return fz_copy_ft_bitmap(ctx, face->glyph->bitmap_left, face->glyph->bitmap_top, &face->glyph->bitmap);
}
fz_pixmap *
-fz_render_ft_stroked_glyph(fz_font *font, int gid, fz_matrix trm, fz_matrix ctm, fz_stroke_state *state)
+fz_render_ft_stroked_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm, fz_matrix ctm, fz_stroke_state *state)
{
FT_Face face = font->ft_face;
float expansion = fz_matrix_expansion(ctm);
@@ -481,7 +481,7 @@ fz_render_ft_stroked_glyph(fz_font *font, int gid, fz_matrix trm, fz_matrix ctm,
}
bitmap = (FT_BitmapGlyph)glyph;
- pixmap = fz_copy_ft_bitmap(bitmap->left, bitmap->top, &bitmap->bitmap);
+ pixmap = fz_copy_ft_bitmap(ctx, bitmap->left, bitmap->top, &bitmap->bitmap);
FT_Done_Glyph(glyph);
return pixmap;
@@ -492,14 +492,14 @@ fz_render_ft_stroked_glyph(fz_font *font, int gid, fz_matrix trm, fz_matrix ctm,
*/
fz_font *
-fz_new_type3_font(char *name, fz_matrix matrix)
+fz_new_type3_font(fz_context *ctx, char *name, fz_matrix matrix)
{
fz_font *font;
int i;
- font = fz_new_font(name);
- font->t3procs = fz_calloc(256, sizeof(fz_buffer*));
- font->t3widths = fz_calloc(256, sizeof(float));
+ font = fz_new_font(ctx, name);
+ font->t3procs = fz_calloc(ctx, 256, sizeof(fz_buffer*));
+ font->t3widths = fz_calloc(ctx, 256, sizeof(float));
font->t3matrix = matrix;
for (i = 0; i < 256; i++)
@@ -512,7 +512,7 @@ fz_new_type3_font(char *name, fz_matrix matrix)
}
fz_pixmap *
-fz_render_t3_glyph(fz_font *font, int gid, fz_matrix trm, fz_colorspace *model)
+fz_render_t3_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm, fz_colorspace *model)
{
fz_error error;
fz_matrix ctm;
@@ -531,7 +531,7 @@ fz_render_t3_glyph(fz_font *font, int gid, fz_matrix trm, fz_colorspace *model)
return NULL;
ctm = fz_concat(font->t3matrix, trm);
- dev = fz_new_bbox_device(&bbox);
+ dev = fz_new_bbox_device(ctx, &bbox);
error = font->t3run(font->t3xref, font->t3resources, contents, dev, ctm);
if (error)
fz_error_handle(error, "cannot draw type3 glyph");
@@ -560,21 +560,21 @@ fz_render_t3_glyph(fz_font *font, int gid, fz_matrix trm, fz_colorspace *model)
bbox.x1++;
bbox.y1++;
- glyph = fz_new_pixmap_with_rect(model ? model : fz_device_gray, bbox);
+ glyph = fz_new_pixmap_with_rect(ctx, model ? model : fz_device_gray, bbox);
fz_clear_pixmap(glyph);
- cache = fz_new_glyph_cache();
- dev = fz_new_draw_device_type3(cache, glyph);
+ cache = fz_new_glyph_cache(ctx);
+ dev = fz_new_draw_device_type3(ctx, cache, glyph);
error = font->t3run(font->t3xref, font->t3resources, contents, dev, ctm);
if (error)
fz_error_handle(error, "cannot draw type3 glyph");
fz_free_device(dev);
- fz_free_glyph_cache(cache);
+ fz_free_glyph_cache(ctx, cache);
if (model == NULL)
{
- result = fz_alpha_from_gray(glyph, 0);
- fz_drop_pixmap(glyph);
+ result = fz_alpha_from_gray(ctx, glyph, 0);
+ fz_drop_pixmap(ctx, glyph);
}
else
result = glyph;
diff --git a/fitz/res_halftone.c b/fitz/res_halftone.c
index e45454ea..ad2ab694 100644
--- a/fitz/res_halftone.c
+++ b/fitz/res_halftone.c
@@ -1,12 +1,12 @@
#include "fitz.h"
fz_halftone *
-fz_new_halftone(int comps)
+fz_new_halftone(fz_context *ctx, int comps)
{
fz_halftone *ht;
int i;
- ht = fz_malloc(sizeof(fz_halftone) + (comps-1)*sizeof(fz_pixmap *));
+ ht = fz_malloc(ctx, sizeof(fz_halftone) + (comps-1)*sizeof(fz_pixmap *));
ht->refs = 1;
ht->n = comps;
for (i = 0; i < comps; i++)
@@ -23,15 +23,15 @@ fz_keep_halftone(fz_halftone *ht)
}
void
-fz_drop_halftone(fz_halftone *ht)
+fz_drop_halftone(fz_context *ctx, fz_halftone *ht)
{
int i;
if (!ht || --ht->refs != 0)
return;
for (i = 0; i < ht->n; i++)
- fz_drop_pixmap(ht->comp[i]);
- fz_free(ht);
+ fz_drop_pixmap(ctx, ht->comp[i]);
+ fz_free(ctx, ht);
}
/* Default mono halftone, lifted from Ghostscript. */
@@ -55,11 +55,11 @@ static unsigned char mono_ht[] =
0xF2, 0x72, 0xD2, 0x52, 0xFA, 0x7A, 0xDA, 0x5A, 0xF0, 0x70, 0xD0, 0x50, 0xF8, 0x78, 0xD8, 0x58
};
-fz_halftone *fz_get_default_halftone(int num_comps)
+fz_halftone *fz_get_default_halftone(fz_context *ctx, int num_comps)
{
- fz_halftone *ht = fz_new_halftone(num_comps);
+ fz_halftone *ht = fz_new_halftone(ctx, num_comps);
assert(num_comps == 1); /* Only support 1 component for now */
- ht->comp[0] = fz_new_pixmap_with_data(NULL, 16, 16, mono_ht);
+ ht->comp[0] = fz_new_pixmap_with_data(ctx, NULL, 16, 16, mono_ht);
return ht;
}
@@ -156,7 +156,7 @@ static void do_threshold_1(unsigned char *ht_line, unsigned char *pixmap, unsign
*out++ = h;
}
-fz_bitmap *fz_halftone_pixmap(fz_pixmap *pix, fz_halftone *ht)
+fz_bitmap *fz_halftone_pixmap(fz_context *ctx, fz_pixmap *pix, fz_halftone *ht)
{
fz_bitmap *out;
unsigned char *ht_line, *o, *p;
@@ -168,8 +168,8 @@ fz_bitmap *fz_halftone_pixmap(fz_pixmap *pix, fz_halftone *ht)
assert(pix->n == 2); /* Mono + Alpha */
n = pix->n-1; /* Remove alpha */
- ht_line = fz_malloc(pix->w * n);
- out = fz_new_bitmap(pix->w, pix->h, n);
+ ht_line = fz_malloc(ctx, pix->w * n);
+ out = fz_new_bitmap(ctx, pix->w, pix->h, n);
o = out->samples;
p = pix->samples;
diff --git a/fitz/res_path.c b/fitz/res_path.c
index 328e5ac1..1cc92937 100644
--- a/fitz/res_path.c
+++ b/fitz/res_path.c
@@ -1,11 +1,12 @@
+#include <assert.h>
#include "fitz.h"
fz_path *
-fz_new_path(void)
+fz_new_path(fz_context *ctx)
{
fz_path *path;
- path = fz_malloc(sizeof(fz_path));
+ path = fz_malloc(ctx, sizeof(fz_path));
path->len = 0;
path->cap = 0;
path->items = NULL;
@@ -14,61 +15,62 @@ fz_new_path(void)
}
fz_path *
-fz_clone_path(fz_path *old)
+fz_clone_path(fz_context *ctx, fz_path *old)
{
fz_path *path;
- path = fz_malloc(sizeof(fz_path));
+ assert(old != NULL);
+ path = fz_malloc(ctx, sizeof(fz_path));
path->len = old->len;
path->cap = old->len;
- path->items = fz_calloc(path->cap, sizeof(fz_path_item));
+ path->items = fz_calloc(ctx, path->cap, sizeof(fz_path_item));
memcpy(path->items, old->items, sizeof(fz_path_item) * path->len);
return path;
}
void
-fz_free_path(fz_path *path)
+fz_free_path(fz_context *ctx, fz_path *path)
{
- fz_free(path->items);
- fz_free(path);
+ fz_free(ctx, path->items);
+ fz_free(ctx, path);
}
static void
-grow_path(fz_path *path, int n)
+grow_path(fz_context *ctx, fz_path *path, int n)
{
if (path->len + n < path->cap)
return;
while (path->len + n > path->cap)
path->cap = path->cap + 36;
- path->items = fz_realloc(path->items, path->cap, sizeof(fz_path_item));
+ path->items = fz_realloc(ctx, path->items, path->cap * sizeof(fz_path_item));
}
void
-fz_moveto(fz_path *path, float x, float y)
+fz_moveto(fz_context *ctx, fz_path *path, float x, float y)
{
- grow_path(path, 3);
+ grow_path(ctx, path, 3);
path->items[path->len++].k = FZ_MOVETO;
path->items[path->len++].v = x;
path->items[path->len++].v = y;
}
void
-fz_lineto(fz_path *path, float x, float y)
+fz_lineto(fz_context *ctx, fz_path *path, float x, float y)
{
if (path->len == 0)
{
fz_warn("lineto with no current point");
return;
}
- grow_path(path, 3);
+ grow_path(ctx, path, 3);
path->items[path->len++].k = FZ_LINETO;
path->items[path->len++].v = x;
path->items[path->len++].v = y;
}
void
-fz_curveto(fz_path *path,
+fz_curveto(fz_context *ctx, fz_path *path,
float x1, float y1,
float x2, float y2,
float x3, float y3)
@@ -78,7 +80,7 @@ fz_curveto(fz_path *path,
fz_warn("curveto with no current point");
return;
}
- grow_path(path, 7);
+ grow_path(ctx, path, 7);
path->items[path->len++].k = FZ_CURVETO;
path->items[path->len++].v = x1;
path->items[path->len++].v = y1;
@@ -89,7 +91,7 @@ fz_curveto(fz_path *path,
}
void
-fz_curvetov(fz_path *path, float x2, float y2, float x3, float y3)
+fz_curvetov(fz_context *ctx, fz_path *path, float x2, float y2, float x3, float y3)
{
float x1, y1;
if (path->len == 0)
@@ -99,24 +101,24 @@ fz_curvetov(fz_path *path, float x2, float y2, float x3, float y3)
}
x1 = path->items[path->len-2].v;
y1 = path->items[path->len-1].v;
- fz_curveto(path, x1, y1, x2, y2, x3, y3);
+ fz_curveto(ctx, path, x1, y1, x2, y2, x3, y3);
}
void
-fz_curvetoy(fz_path *path, float x1, float y1, float x3, float y3)
+fz_curvetoy(fz_context *ctx, fz_path *path, float x1, float y1, float x3, float y3)
{
- fz_curveto(path, x1, y1, x3, y3, x3, y3);
+ fz_curveto(ctx, path, x1, y1, x3, y3, x3, y3);
}
void
-fz_closepath(fz_path *path)
+fz_closepath(fz_context *ctx, fz_path *path)
{
if (path->len == 0)
{
fz_warn("closepath with no current point");
return;
}
- grow_path(path, 1);
+ grow_path(ctx, path, 1);
path->items[path->len++].k = FZ_CLOSE_PATH;
}
diff --git a/fitz/res_pixmap.c b/fitz/res_pixmap.c
index dccc62e3..3706d495 100644
--- a/fitz/res_pixmap.c
+++ b/fitz/res_pixmap.c
@@ -4,11 +4,11 @@ static int fz_memory_limit = 256 << 20;
static int fz_memory_used = 0;
fz_pixmap *
-fz_new_pixmap_with_data(fz_colorspace *colorspace, int w, int h, unsigned char *samples)
+fz_new_pixmap_with_data(fz_context *ctx, fz_colorspace *colorspace, int w, int h, unsigned char *samples)
{
fz_pixmap *pix;
- pix = fz_malloc(sizeof(fz_pixmap));
+ pix = fz_malloc(ctx, sizeof(fz_pixmap));
pix->refs = 1;
pix->x = 0;
pix->y = 0;
@@ -35,7 +35,7 @@ fz_new_pixmap_with_data(fz_colorspace *colorspace, int w, int h, unsigned char *
else
{
fz_memory_used += pix->w * pix->h * pix->n;
- pix->samples = fz_calloc(pix->h, pix->w * pix->n);
+ pix->samples = fz_calloc(ctx, pix->h, pix->w * pix->n);
pix->free_samples = 1;
}
@@ -43,7 +43,7 @@ fz_new_pixmap_with_data(fz_colorspace *colorspace, int w, int h, unsigned char *
}
fz_pixmap *
-fz_new_pixmap_with_limit(fz_colorspace *colorspace, int w, int h)
+fz_new_pixmap_with_limit(fz_context *ctx, fz_colorspace *colorspace, int w, int h)
{
int n = colorspace ? colorspace->n + 1 : 1;
int size = w * h * n;
@@ -53,30 +53,30 @@ fz_new_pixmap_with_limit(fz_colorspace *colorspace, int w, int h)
fz_memory_used/(1<<20), size/(1<<20), fz_memory_limit/(1<<20));
return NULL;
}
- return fz_new_pixmap_with_data(colorspace, w, h, NULL);
+ return fz_new_pixmap_with_data(ctx, colorspace, w, h, NULL);
}
fz_pixmap *
-fz_new_pixmap(fz_colorspace *colorspace, int w, int h)
+fz_new_pixmap(fz_context *ctx, fz_colorspace *colorspace, int w, int h)
{
- return fz_new_pixmap_with_data(colorspace, w, h, NULL);
+ return fz_new_pixmap_with_data(ctx, colorspace, w, h, NULL);
}
fz_pixmap *
-fz_new_pixmap_with_rect(fz_colorspace *colorspace, fz_bbox r)
+fz_new_pixmap_with_rect(fz_context *ctx, fz_colorspace *colorspace, fz_bbox r)
{
fz_pixmap *pixmap;
- pixmap = fz_new_pixmap(colorspace, r.x1 - r.x0, r.y1 - r.y0);
+ pixmap = fz_new_pixmap(ctx, colorspace, r.x1 - r.x0, r.y1 - r.y0);
pixmap->x = r.x0;
pixmap->y = r.y0;
return pixmap;
}
fz_pixmap *
-fz_new_pixmap_with_rect_and_data(fz_colorspace *colorspace, fz_bbox r, unsigned char *samples)
+fz_new_pixmap_with_rect_and_data(fz_context *ctx, fz_colorspace *colorspace, fz_bbox r, unsigned char *samples)
{
fz_pixmap *pixmap;
- pixmap = fz_new_pixmap_with_data(colorspace, r.x1 - r.x0, r.y1 - r.y0, samples);
+ pixmap = fz_new_pixmap_with_data(ctx, colorspace, r.x1 - r.x0, r.y1 - r.y0, samples);
pixmap->x = r.x0;
pixmap->y = r.y0;
return pixmap;
@@ -90,18 +90,18 @@ fz_keep_pixmap(fz_pixmap *pix)
}
void
-fz_drop_pixmap(fz_pixmap *pix)
+fz_drop_pixmap(fz_context *ctx, fz_pixmap *pix)
{
if (pix && --pix->refs == 0)
{
fz_memory_used -= pix->w * pix->h * pix->n;
if (pix->mask)
- fz_drop_pixmap(pix->mask);
+ fz_drop_pixmap(ctx, pix->mask);
if (pix->colorspace)
- fz_drop_colorspace(pix->colorspace);
+ fz_drop_colorspace(ctx, pix->colorspace);
if (pix->free_samples)
- fz_free(pix->samples);
- fz_free(pix);
+ fz_free(ctx, pix->samples);
+ fz_free(ctx, pix);
}
}
@@ -227,7 +227,7 @@ fz_premultiply_pixmap(fz_pixmap *pix)
}
fz_pixmap *
-fz_alpha_from_gray(fz_pixmap *gray, int luminosity)
+fz_alpha_from_gray(fz_context *ctx, fz_pixmap *gray, int luminosity)
{
fz_pixmap *alpha;
unsigned char *sp, *dp;
@@ -235,7 +235,7 @@ fz_alpha_from_gray(fz_pixmap *gray, int luminosity)
assert(gray->n == 2);
- alpha = fz_new_pixmap_with_rect(NULL, fz_bound_pixmap(gray));
+ alpha = fz_new_pixmap_with_rect(ctx, NULL, fz_bound_pixmap(gray));
dp = alpha->samples;
sp = gray->samples;
if (!luminosity)
@@ -431,7 +431,7 @@ static void putchunk(char *tag, unsigned char *data, int size, FILE *fp)
}
fz_error
-fz_write_png(fz_pixmap *pixmap, char *filename, int savealpha)
+fz_write_png(fz_context *ctx, fz_pixmap *pixmap, char *filename, int savealpha)
{
static const unsigned char pngsig[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
FILE *fp;
@@ -461,8 +461,8 @@ fz_write_png(fz_pixmap *pixmap, char *filename, int savealpha)
usize = (pixmap->w * dn + 1) * pixmap->h;
csize = compressBound(usize);
- udata = fz_malloc(usize);
- cdata = fz_malloc(csize);
+ udata = fz_malloc(ctx, usize);
+ cdata = fz_malloc(ctx, csize);
sp = pixmap->samples;
dp = udata;
@@ -486,16 +486,16 @@ fz_write_png(fz_pixmap *pixmap, char *filename, int savealpha)
err = compress(cdata, &csize, udata, usize);
if (err != Z_OK)
{
- fz_free(udata);
- fz_free(cdata);
+ fz_free(ctx, udata);
+ fz_free(ctx, cdata);
return fz_error_make("cannot compress image data");
}
fp = fopen(filename, "wb");
if (!fp)
{
- fz_free(udata);
- fz_free(cdata);
+ fz_free(ctx, udata);
+ fz_free(ctx, cdata);
return fz_error_make("cannot open file '%s': %s", filename, strerror(errno));
}
@@ -513,7 +513,7 @@ fz_write_png(fz_pixmap *pixmap, char *filename, int savealpha)
putchunk("IEND", head, 0, fp);
fclose(fp);
- fz_free(udata);
- fz_free(cdata);
+ fz_free(ctx, udata);
+ fz_free(ctx, cdata);
return fz_okay;
}
diff --git a/fitz/res_shade.c b/fitz/res_shade.c
index 7faff3bf..b7617161 100644
--- a/fitz/res_shade.c
+++ b/fitz/res_shade.c
@@ -8,14 +8,14 @@ fz_keep_shade(fz_shade *shade)
}
void
-fz_drop_shade(fz_shade *shade)
+fz_drop_shade(fz_context *ctx, fz_shade *shade)
{
if (shade && --shade->refs == 0)
{
if (shade->colorspace)
- fz_drop_colorspace(shade->colorspace);
- fz_free(shade->mesh);
- fz_free(shade);
+ fz_drop_colorspace(ctx, shade->colorspace);
+ fz_free(ctx, shade->mesh);
+ fz_free(ctx, shade);
}
}
diff --git a/fitz/res_text.c b/fitz/res_text.c
index d38637f1..1c5bbad0 100644
--- a/fitz/res_text.c
+++ b/fitz/res_text.c
@@ -1,11 +1,11 @@
#include "fitz.h"
fz_text *
-fz_new_text(fz_font *font, fz_matrix trm, int wmode)
+fz_new_text(fz_context *ctx, fz_font *font, fz_matrix trm, int wmode)
{
fz_text *text;
- text = fz_malloc(sizeof(fz_text));
+ text = fz_malloc(ctx, sizeof(fz_text));
text->font = fz_keep_font(font);
text->trm = trm;
text->wmode = wmode;
@@ -17,25 +17,25 @@ fz_new_text(fz_font *font, fz_matrix trm, int wmode)
}
void
-fz_free_text(fz_text *text)
+fz_free_text(fz_context *ctx, fz_text *text)
{
- fz_drop_font(text->font);
- fz_free(text->items);
- fz_free(text);
+ fz_drop_font(ctx, text->font);
+ fz_free(ctx, text->items);
+ fz_free(ctx, text);
}
fz_text *
-fz_clone_text(fz_text *old)
+fz_clone_text(fz_context *ctx, fz_text *old)
{
fz_text *text;
- text = fz_malloc(sizeof(fz_text));
+ text = fz_malloc(ctx, sizeof(fz_text));
text->font = fz_keep_font(old->font);
text->trm = old->trm;
text->wmode = old->wmode;
text->len = old->len;
text->cap = text->len;
- text->items = fz_calloc(text->len, sizeof(fz_text_item));
+ text->items = fz_calloc(ctx, text->len, sizeof(fz_text_item));
memcpy(text->items, old->items, text->len * sizeof(fz_text_item));
return text;
@@ -91,19 +91,19 @@ fz_bound_text(fz_text *text, fz_matrix ctm)
}
static void
-fz_grow_text(fz_text *text, int n)
+fz_grow_text(fz_context *ctx, fz_text *text, int n)
{
if (text->len + n < text->cap)
return;
while (text->len + n > text->cap)
text->cap = text->cap + 36;
- text->items = fz_realloc(text->items, text->cap, sizeof(fz_text_item));
+ text->items = fz_realloc(ctx, text->items, text->cap * sizeof(fz_text_item));
}
void
-fz_add_text(fz_text *text, int gid, int ucs, float x, float y)
+fz_add_text(fz_context *ctx, fz_text *text, int gid, int ucs, float x, float y)
{
- fz_grow_text(text, 1);
+ fz_grow_text(ctx, text, 1);
text->items[text->len].ucs = ucs;
text->items[text->len].gid = gid;
text->items[text->len].x = x;
diff --git a/fitz/stm_buffer.c b/fitz/stm_buffer.c
index 4c7410c3..edd5c9c9 100644
--- a/fitz/stm_buffer.c
+++ b/fitz/stm_buffer.c
@@ -1,15 +1,15 @@
#include "fitz.h"
fz_buffer *
-fz_new_buffer(int size)
+fz_new_buffer(fz_context *ctx, int size)
{
fz_buffer *b;
size = size > 1 ? size : 16;
- b = fz_malloc(sizeof(fz_buffer));
+ b = fz_malloc(ctx, sizeof(fz_buffer));
b->refs = 1;
- b->data = fz_malloc(size);
+ b->data = fz_malloc(ctx, size);
b->cap = size;
b->len = 0;
@@ -24,26 +24,26 @@ fz_keep_buffer(fz_buffer *buf)
}
void
-fz_drop_buffer(fz_buffer *buf)
+fz_drop_buffer(fz_context *ctx, fz_buffer *buf)
{
if (--buf->refs == 0)
{
- fz_free(buf->data);
- fz_free(buf);
+ fz_free(ctx, buf->data);
+ fz_free(ctx, buf);
}
}
void
-fz_resize_buffer(fz_buffer *buf, int size)
+fz_resize_buffer(fz_context *ctx, fz_buffer *buf, int size)
{
- buf->data = fz_realloc(buf->data, size, 1);
+ buf->data = fz_realloc(ctx, buf->data, size);
buf->cap = size;
if (buf->len > buf->cap)
buf->len = buf->cap;
}
void
-fz_grow_buffer(fz_buffer *buf)
+fz_grow_buffer(fz_context *ctx, fz_buffer *buf)
{
- fz_resize_buffer(buf, (buf->cap * 3) / 2);
+ fz_resize_buffer(ctx, buf, (buf->cap * 3) / 2);
}
diff --git a/fitz/stm_open.c b/fitz/stm_open.c
index 81eb819e..f06bdc50 100644
--- a/fitz/stm_open.c
+++ b/fitz/stm_open.c
@@ -1,13 +1,13 @@
#include "fitz.h"
fz_stream *
-fz_new_stream(void *state,
+fz_new_stream(fz_context *ctx, void *state,
int(*read)(fz_stream *stm, unsigned char *buf, int len),
void(*close)(fz_stream *stm))
{
fz_stream *stm;
- stm = fz_malloc(sizeof(fz_stream));
+ stm = fz_malloc(ctx, sizeof(fz_stream));
stm->refs = 1;
stm->error = 0;
@@ -26,6 +26,7 @@ fz_new_stream(void *state,
stm->read = read;
stm->close = close;
stm->seek = NULL;
+ stm->ctx = ctx;
return stm;
}
@@ -45,7 +46,7 @@ fz_close(fz_stream *stm)
{
if (stm->close)
stm->close(stm);
- fz_free(stm);
+ fz_free(stm->ctx, stm);
}
}
@@ -74,41 +75,41 @@ static void close_file(fz_stream *stm)
int n = close(*(int*)stm->state);
if (n < 0)
fz_warn("close error: %s", strerror(errno));
- fz_free(stm->state);
+ fz_free(stm->ctx, stm->state);
}
fz_stream *
-fz_open_fd(int fd)
+fz_open_fd(fz_context *ctx, int fd)
{
fz_stream *stm;
int *state;
- state = fz_malloc(sizeof(int));
+ state = fz_malloc(ctx, sizeof(int));
*state = fd;
- stm = fz_new_stream(state, read_file, close_file);
+ stm = fz_new_stream(ctx, state, read_file, close_file);
stm->seek = seek_file;
return stm;
}
fz_stream *
-fz_open_file(const char *name)
+fz_open_file(fz_context *ctx, const char *name)
{
int fd = open(name, O_BINARY | O_RDONLY, 0);
if (fd == -1)
return NULL;
- return fz_open_fd(fd);
+ return fz_open_fd(ctx, fd);
}
#ifdef _WIN32
fz_stream *
-fz_open_file_w(const wchar_t *name)
+fz_open_file_w(fz_context *ctx, const wchar_t *name)
{
int fd = _wopen(name, O_BINARY | O_RDONLY, 0);
if (fd == -1)
return NULL;
- return fz_open_fd(fd);
+ return fz_open_fd(ctx, fd);
}
#endif
@@ -134,15 +135,15 @@ static void seek_buffer(fz_stream *stm, int offset, int whence)
static void close_buffer(fz_stream *stm)
{
if (stm->state)
- fz_drop_buffer(stm->state);
+ fz_drop_buffer(stm->ctx, stm->state);
}
fz_stream *
-fz_open_buffer(fz_buffer *buf)
+fz_open_buffer(fz_context *ctx, fz_buffer *buf)
{
fz_stream *stm;
- stm = fz_new_stream(fz_keep_buffer(buf), read_buffer, close_buffer);
+ stm = fz_new_stream(ctx, fz_keep_buffer(buf), read_buffer, close_buffer);
stm->seek = seek_buffer;
stm->bp = buf->data;
@@ -156,11 +157,11 @@ fz_open_buffer(fz_buffer *buf)
}
fz_stream *
-fz_open_memory(unsigned char *data, int len)
+fz_open_memory(fz_context *ctx, unsigned char *data, int len)
{
fz_stream *stm;
- stm = fz_new_stream(NULL, read_buffer, close_buffer);
+ stm = fz_new_stream(ctx, NULL, read_buffer, close_buffer);
stm->seek = seek_buffer;
stm->bp = data;
diff --git a/fitz/stm_read.c b/fitz/stm_read.c
index 89811333..062916c5 100644
--- a/fitz/stm_read.c
+++ b/fitz/stm_read.c
@@ -99,27 +99,28 @@ fz_read_all(fz_buffer **bufp, fz_stream *stm, int initial)
{
fz_buffer *buf;
int n;
+ fz_context *ctx = stm->ctx;
if (initial < 1024)
initial = 1024;
- buf = fz_new_buffer(initial);
+ buf = fz_new_buffer(ctx, initial);
while (1)
{
if (buf->len == buf->cap)
- fz_grow_buffer(buf);
+ fz_grow_buffer(ctx, buf);
if (buf->len / 200 > initial)
{
- fz_drop_buffer(buf);
+ fz_drop_buffer(ctx, buf);
return fz_error_make("compression bomb detected");
}
n = fz_read(stm, buf->data + buf->len, buf->cap - buf->len);
if (n < 0)
{
- fz_drop_buffer(buf);
+ fz_drop_buffer(ctx, buf);
return fz_error_note(n, "read error");
}
if (n == 0)
diff --git a/pdf/mupdf.h b/pdf/mupdf.h
index 6870edfc..c2e651cc 100644
--- a/pdf/mupdf.h
+++ b/pdf/mupdf.h
@@ -32,12 +32,12 @@ fz_error pdf_parse_dict(fz_obj **op, pdf_xref *xref, fz_stream *f, char *buf, in
fz_error pdf_parse_stm_obj(fz_obj **op, pdf_xref *xref, fz_stream *f, char *buf, int cap);
fz_error pdf_parse_ind_obj(fz_obj **op, pdf_xref *xref, fz_stream *f, char *buf, int cap, int *num, int *gen, int *stm_ofs);
-fz_rect pdf_to_rect(fz_obj *array);
-fz_matrix pdf_to_matrix(fz_obj *array);
-char *pdf_to_utf8(fz_obj *src);
-unsigned short *pdf_to_ucs2(fz_obj *src);
-fz_obj *pdf_to_utf8_name(fz_obj *src);
-char *pdf_from_ucs2(unsigned short *str);
+fz_rect pdf_to_rect(fz_context *ctx, fz_obj *array);
+fz_matrix pdf_to_matrix(fz_context *ctx, fz_obj *array);
+char *pdf_to_utf8(fz_context *ctx, fz_obj *src);
+unsigned short *pdf_to_ucs2(fz_context *ctx, fz_obj *src);
+fz_obj *pdf_to_utf8_name(fz_context *ctx, fz_obj *src);
+char *pdf_from_ucs2(fz_context *ctx, unsigned short *str);
/*
* xref and object / stream api
@@ -57,6 +57,7 @@ struct pdf_xref_entry_s
struct pdf_xref_s
{
+ fz_context *ctx;
fz_stream *file;
int version;
int startxref;
@@ -91,7 +92,7 @@ fz_error pdf_open_stream(fz_stream **stmp, pdf_xref *, int num, int gen);
fz_error pdf_open_stream_at(fz_stream **stmp, pdf_xref *xref, int num, int gen, fz_obj *dict, int stm_ofs);
fz_error pdf_open_xref_with_stream(pdf_xref **xrefp, fz_stream *file, char *password);
-fz_error pdf_open_xref(pdf_xref **xrefp, const char *filename, char *password);
+fz_error pdf_open_xref(fz_context *ctx, pdf_xref **xrefp, const char *filename, char *password);
void pdf_free_xref(pdf_xref *);
/* private */
@@ -117,10 +118,10 @@ enum
PDF_DEFAULT_PERM_FLAGS = 0xfffc
};
-fz_error pdf_new_crypt(pdf_crypt **cp, fz_obj *enc, fz_obj *id);
-void pdf_free_crypt(pdf_crypt *crypt);
+fz_error pdf_new_crypt(fz_context *ctx, pdf_crypt **cp, fz_obj *enc, fz_obj *id);
+void pdf_free_crypt(fz_context *ctx, pdf_crypt *crypt);
-void pdf_crypt_obj(pdf_crypt *crypt, fz_obj *obj, int num, int gen);
+void pdf_crypt_obj(fz_context *ctx, pdf_crypt *crypt, fz_obj *obj, int num, int gen);
fz_stream *pdf_open_crypt(fz_stream *chain, pdf_crypt *crypt, int num, int gen);
fz_stream *pdf_open_crypt_with_filter(fz_stream *chain, pdf_crypt *crypt, char *name, int num, int gen);
@@ -141,14 +142,17 @@ void pdf_debug_crypt(pdf_crypt *crypt);
typedef struct pdf_store_s pdf_store;
-pdf_store *pdf_new_store(void);
-void pdf_free_store(pdf_store *store);
-void pdf_debug_store(pdf_store *store);
+pdf_store *pdf_new_store(fz_context *ctx);
+void pdf_free_store(fz_context *ctx, pdf_store *store);
+void pdf_debug_store(fz_context *ctx, pdf_store *store);
-void pdf_store_item(pdf_store *store, void *keepfn, void *dropfn, fz_obj *key, void *val);
-void *pdf_find_item(pdf_store *store, void *dropfn, fz_obj *key);
-void pdf_remove_item(pdf_store *store, void *dropfn, fz_obj *key);
-void pdf_age_store(pdf_store *store, int maxage);
+typedef void *(pdf_store_keep_fn)(void *);
+typedef void (pdf_store_drop_fn)(fz_context *, void *);
+
+void pdf_store_item(fz_context *ctx, pdf_store *store, pdf_store_keep_fn *keepfn, pdf_store_drop_fn *dropfn, fz_obj *key, void *val);
+void *pdf_find_item(fz_context *ctx, pdf_store *store, pdf_store_drop_fn *dropfn, fz_obj *key);
+void pdf_remove_item(fz_context *ctx, pdf_store *store, pdf_store_drop_fn *dropfn, fz_obj *key);
+void pdf_age_store(fz_context *ctx, pdf_store *store, int maxage);
/*
* Functions, Colorspaces, Shadings and Images
@@ -159,16 +163,16 @@ typedef struct pdf_function_s pdf_function;
fz_error pdf_load_function(pdf_function **func, pdf_xref *xref, fz_obj *ref);
void pdf_eval_function(pdf_function *func, float *in, int inlen, float *out, int outlen);
pdf_function *pdf_keep_function(pdf_function *func);
-void pdf_drop_function(pdf_function *func);
+void pdf_drop_function(fz_context *ctx, pdf_function *func);
fz_error pdf_load_colorspace(fz_colorspace **csp, pdf_xref *xref, fz_obj *obj);
-fz_pixmap *pdf_expand_indexed_pixmap(fz_pixmap *src);
+fz_pixmap *pdf_expand_indexed_pixmap(fz_context *ctx, fz_pixmap *src);
fz_error pdf_load_shading(fz_shade **shadep, pdf_xref *xref, fz_obj *obj);
fz_error pdf_load_inline_image(fz_pixmap **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict, fz_stream *file);
fz_error pdf_load_image(fz_pixmap **imgp, pdf_xref *xref, fz_obj *obj);
-int pdf_is_jpx_image(fz_obj *dict);
+int pdf_is_jpx_image(fz_context *ctx, fz_obj *dict);
/*
* Pattern
@@ -190,7 +194,7 @@ struct pdf_pattern_s
fz_error pdf_load_pattern(pdf_pattern **patp, pdf_xref *xref, fz_obj *obj);
pdf_pattern *pdf_keep_pattern(pdf_pattern *pat);
-void pdf_drop_pattern(pdf_pattern *pat);
+void pdf_drop_pattern(fz_context *ctx, pdf_pattern *pat);
/*
* XObject
@@ -213,7 +217,7 @@ struct pdf_xobject_s
fz_error pdf_load_xobject(pdf_xobject **xobjp, pdf_xref *xref, fz_obj *obj);
pdf_xobject *pdf_keep_xobject(pdf_xobject *xobj);
-void pdf_drop_xobject(pdf_xobject *xobj);
+void pdf_drop_xobject(fz_context *ctx, pdf_xobject *xobj);
/*
* CMap
@@ -259,29 +263,29 @@ struct pdf_cmap_s
unsigned short *table;
};
-pdf_cmap *pdf_new_cmap(void);
+pdf_cmap *pdf_new_cmap(fz_context *ctx);
pdf_cmap *pdf_keep_cmap(pdf_cmap *cmap);
-void pdf_drop_cmap(pdf_cmap *cmap);
+void pdf_drop_cmap(fz_context *ctx, pdf_cmap *cmap);
void pdf_debug_cmap(pdf_cmap *cmap);
int pdf_get_wmode(pdf_cmap *cmap);
void pdf_set_wmode(pdf_cmap *cmap, int wmode);
-void pdf_set_usecmap(pdf_cmap *cmap, pdf_cmap *usecmap);
+void pdf_set_usecmap(fz_context *ctx, pdf_cmap *cmap, pdf_cmap *usecmap);
void pdf_add_codespace(pdf_cmap *cmap, int low, int high, int n);
-void pdf_map_range_to_table(pdf_cmap *cmap, int low, int *map, int len);
-void pdf_map_range_to_range(pdf_cmap *cmap, int srclo, int srchi, int dstlo);
-void pdf_map_one_to_many(pdf_cmap *cmap, int one, int *many, int len);
-void pdf_sort_cmap(pdf_cmap *cmap);
+void pdf_map_range_to_table(fz_context *ctx, pdf_cmap *cmap, int low, int *map, int len);
+void pdf_map_range_to_range(fz_context *ctx, pdf_cmap *cmap, int srclo, int srchi, int dstlo);
+void pdf_map_one_to_many(fz_context *ctx, pdf_cmap *cmap, int one, int *many, int len);
+void pdf_sort_cmap(fz_context *ctx, pdf_cmap *cmap);
int pdf_lookup_cmap(pdf_cmap *cmap, int cpt);
int pdf_lookup_cmap_full(pdf_cmap *cmap, int cpt, int *out);
unsigned char *pdf_decode_cmap(pdf_cmap *cmap, unsigned char *s, int *cpt);
-pdf_cmap *pdf_new_identity_cmap(int wmode, int bytes);
+pdf_cmap *pdf_new_identity_cmap(fz_context *ctx, int wmode, int bytes);
fz_error pdf_parse_cmap(pdf_cmap **cmapp, fz_stream *file);
fz_error pdf_load_embedded_cmap(pdf_cmap **cmapp, pdf_xref *xref, fz_obj *ref);
-fz_error pdf_load_system_cmap(pdf_cmap **cmapp, char *name);
+fz_error pdf_load_system_cmap(fz_context *ctx, pdf_cmap **cmapp, char *name);
pdf_cmap *pdf_find_builtin_cmap(char *cmap_name);
/*
@@ -376,8 +380,8 @@ struct pdf_font_desc_s
void pdf_set_font_wmode(pdf_font_desc *font, int wmode);
void pdf_set_default_hmtx(pdf_font_desc *font, int w);
void pdf_set_default_vmtx(pdf_font_desc *font, int y, int w);
-void pdf_add_hmtx(pdf_font_desc *font, int lo, int hi, int w);
-void pdf_add_vmtx(pdf_font_desc *font, int lo, int hi, int x, int y, int w);
+void pdf_add_hmtx(fz_context *ctx, pdf_font_desc *font, int lo, int hi, int w);
+void pdf_add_vmtx(fz_context *ctx, pdf_font_desc *font, int lo, int hi, int x, int y, int w);
void pdf_end_hmtx(pdf_font_desc *font);
void pdf_end_vmtx(pdf_font_desc *font);
pdf_hmtx pdf_get_hmtx(pdf_font_desc *font, int cid);
@@ -394,9 +398,9 @@ unsigned char *pdf_find_substitute_cjk_font(int ros, int serif, unsigned int *le
fz_error pdf_load_type3_font(pdf_font_desc **fontp, pdf_xref *xref, fz_obj *rdb, fz_obj *obj);
fz_error pdf_load_font(pdf_font_desc **fontp, pdf_xref *xref, fz_obj *rdb, fz_obj *obj);
-pdf_font_desc *pdf_new_font_desc(void);
+pdf_font_desc *pdf_new_font_desc(fz_context *ctx);
pdf_font_desc *pdf_keep_font(pdf_font_desc *fontdesc);
-void pdf_drop_font(pdf_font_desc *font);
+void pdf_drop_font(fz_context *ctx, pdf_font_desc *font);
void pdf_debug_font(pdf_font_desc *fontdesc);
@@ -448,15 +452,15 @@ fz_obj *pdf_lookup_name(pdf_xref *xref, char *which, fz_obj *needle);
fz_obj *pdf_load_name_tree(pdf_xref *xref, char *which);
pdf_outline *pdf_load_outline(pdf_xref *xref);
-void pdf_debug_outline(pdf_outline *outline, int level);
-void pdf_free_outline(pdf_outline *outline);
+void pdf_debug_outline(fz_context *ctx, pdf_outline *outline, int level);
+void pdf_free_outline(fz_context *ctx, pdf_outline *outline);
pdf_link *pdf_load_link(pdf_xref *xref, fz_obj *dict);
void pdf_load_links(pdf_link **, pdf_xref *, fz_obj *annots);
-void pdf_free_link(pdf_link *link);
+void pdf_free_link(fz_context *ctx, pdf_link *link);
void pdf_load_annots(pdf_annot **, pdf_xref *, fz_obj *annots);
-void pdf_free_annot(pdf_annot *link);
+void pdf_free_annot(fz_context *ctx, pdf_annot *link);
/*
* Page tree, pages and related objects
@@ -480,7 +484,7 @@ int pdf_find_page_number(pdf_xref *xref, fz_obj *pageobj);
int pdf_count_pages(pdf_xref *xref);
fz_error pdf_load_page(pdf_page **pagep, pdf_xref *xref, int number);
-void pdf_free_page(pdf_page *page);
+void pdf_free_page(fz_context *ctx, pdf_page *page);
/*
* Content stream parsing
diff --git a/pdf/pdf_annot.c b/pdf/pdf_annot.c
index 8401d79b..cc057d6f 100644
--- a/pdf/pdf_annot.c
+++ b/pdf/pdf_annot.c
@@ -2,32 +2,38 @@
#include "mupdf.h"
void
-pdf_free_link(pdf_link *link)
+pdf_free_link(fz_context *ctx, pdf_link *link)
{
- if (link->next)
- pdf_free_link(link->next);
- if (link->dest)
- fz_drop_obj(link->dest);
- fz_free(link);
+ pdf_link *next;
+
+ do
+ {
+ next = link->next;
+ if (link->dest)
+ fz_drop_obj(ctx, link->dest);
+ fz_free(ctx, link);
+ link = next;
+ }
+ while(link != NULL);
}
static fz_obj *
resolve_dest(pdf_xref *xref, fz_obj *dest)
{
- if (fz_is_name(dest) || fz_is_string(dest))
+ if (fz_is_name(xref->ctx, dest) || fz_is_string(xref->ctx, dest))
{
dest = pdf_lookup_dest(xref, dest);
return resolve_dest(xref, dest);
}
- else if (fz_is_array(dest))
+ else if (fz_is_array(xref->ctx, dest))
{
return dest;
}
- else if (fz_is_dict(dest))
+ else if (fz_is_dict(xref->ctx, dest))
{
- dest = fz_dict_gets(dest, "D");
+ dest = fz_dict_gets(xref->ctx, dest, "D");
return resolve_dest(xref, dest);
}
@@ -45,52 +51,53 @@ pdf_load_link(pdf_xref *xref, fz_obj *dict)
fz_obj *obj;
fz_rect bbox;
pdf_link_kind kind;
+ fz_context *ctx = xref->ctx;
dest = NULL;
- obj = fz_dict_gets(dict, "Rect");
+ obj = fz_dict_gets(ctx, dict, "Rect");
if (obj)
- bbox = pdf_to_rect(obj);
+ bbox = pdf_to_rect(ctx, obj);
else
bbox = fz_empty_rect;
- obj = fz_dict_gets(dict, "Dest");
+ obj = fz_dict_gets(ctx, dict, "Dest");
if (obj)
{
kind = PDF_LINK_GOTO;
dest = resolve_dest(xref, obj);
}
- action = fz_dict_gets(dict, "A");
+ action = fz_dict_gets(ctx, dict, "A");
/* fall back to additional action button's down/up action */
if (!action)
- action = fz_dict_getsa(fz_dict_gets(dict, "AA"), "U", "D");
+ action = fz_dict_getsa(ctx, fz_dict_gets(ctx, dict, "AA"), "U", "D");
if (action)
{
- obj = fz_dict_gets(action, "S");
- if (fz_is_name(obj) && !strcmp(fz_to_name(obj), "GoTo"))
+ obj = fz_dict_gets(ctx, action, "S");
+ if (fz_is_name(ctx, obj) && !strcmp(fz_to_name(ctx, obj), "GoTo"))
{
kind = PDF_LINK_GOTO;
- dest = resolve_dest(xref, fz_dict_gets(action, "D"));
+ dest = resolve_dest(xref, fz_dict_gets(ctx, action, "D"));
}
- else if (fz_is_name(obj) && !strcmp(fz_to_name(obj), "URI"))
+ else if (fz_is_name(ctx, obj) && !strcmp(fz_to_name(ctx, obj), "URI"))
{
kind = PDF_LINK_URI;
- dest = fz_dict_gets(action, "URI");
+ dest = fz_dict_gets(ctx, action, "URI");
}
- else if (fz_is_name(obj) && !strcmp(fz_to_name(obj), "Launch"))
+ else if (fz_is_name(ctx, obj) && !strcmp(fz_to_name(ctx, obj), "Launch"))
{
kind = PDF_LINK_LAUNCH;
- dest = fz_dict_gets(action, "F");
+ dest = fz_dict_gets(ctx, action, "F");
}
- else if (fz_is_name(obj) && !strcmp(fz_to_name(obj), "Named"))
+ else if (fz_is_name(ctx, obj) && !strcmp(fz_to_name(ctx, obj), "Named"))
{
kind = PDF_LINK_NAMED;
- dest = fz_dict_gets(action, "N");
+ dest = fz_dict_gets(ctx, action, "N");
}
- else if (fz_is_name(obj) && (!strcmp(fz_to_name(obj), "GoToR")))
+ else if (fz_is_name(ctx, obj) && (!strcmp(fz_to_name(ctx, obj), "GoToR")))
{
kind = PDF_LINK_ACTION;
dest = action;
@@ -103,7 +110,7 @@ pdf_load_link(pdf_xref *xref, fz_obj *dict)
if (dest)
{
- pdf_link *link = fz_malloc(sizeof(pdf_link));
+ pdf_link *link = fz_malloc(ctx, sizeof(pdf_link));
link->kind = kind;
link->rect = bbox;
link->dest = fz_keep_obj(dest);
@@ -119,14 +126,16 @@ pdf_load_links(pdf_link **linkp, pdf_xref *xref, fz_obj *annots)
{
pdf_link *link, *head, *tail;
fz_obj *obj;
- int i;
+ int i, n;
+ fz_context *ctx = xref->ctx;
head = tail = NULL;
link = NULL;
- for (i = 0; i < fz_array_len(annots); i++)
+ n = fz_array_len(ctx, annots);
+ for (i = 0; i < n; i++)
{
- obj = fz_array_get(annots, i);
+ obj = fz_array_get(ctx, annots, i);
link = pdf_load_link(xref, obj);
if (link)
{
@@ -144,15 +153,21 @@ pdf_load_links(pdf_link **linkp, pdf_xref *xref, fz_obj *annots)
}
void
-pdf_free_annot(pdf_annot *annot)
+pdf_free_annot(fz_context *ctx, pdf_annot *annot)
{
- if (annot->next)
- pdf_free_annot(annot->next);
- if (annot->ap)
- pdf_drop_xobject(annot->ap);
- if (annot->obj)
- fz_drop_obj(annot->obj);
- fz_free(annot);
+ pdf_annot *next;
+
+ do
+ {
+ next = annot->next;
+ if (annot->ap)
+ pdf_drop_xobject(ctx, annot->ap);
+ if (annot->obj)
+ fz_drop_obj(ctx, annot->obj);
+ fz_free(ctx, annot);
+ annot = next;
+ }
+ while (annot != NULL);
}
static void
@@ -179,25 +194,27 @@ pdf_load_annots(pdf_annot **annotp, pdf_xref *xref, fz_obj *annots)
fz_obj *obj, *ap, *as, *n, *rect;
pdf_xobject *form;
fz_error error;
- int i;
+ int i, len;
+ fz_context *ctx = xref->ctx;
head = tail = NULL;
annot = NULL;
- for (i = 0; i < fz_array_len(annots); i++)
+ len = fz_array_len(ctx, annots);
+ for (i = 0; i < len; i++)
{
- obj = fz_array_get(annots, i);
+ obj = fz_array_get(ctx, annots, i);
- rect = fz_dict_gets(obj, "Rect");
- ap = fz_dict_gets(obj, "AP");
- as = fz_dict_gets(obj, "AS");
- if (fz_is_dict(ap))
+ rect = fz_dict_gets(ctx, obj, "Rect");
+ ap = fz_dict_gets(ctx, obj, "AP");
+ as = fz_dict_gets(ctx, obj, "AS");
+ if (fz_is_dict(ctx, ap))
{
- n = fz_dict_gets(ap, "N"); /* normal state */
+ n = fz_dict_gets(ctx, ap, "N"); /* normal state */
/* lookup current state in sub-dictionary */
if (!pdf_is_stream(xref, fz_to_num(n), fz_to_gen(n)))
- n = fz_dict_get(n, as);
+ n = fz_dict_get(ctx, n, as);
if (pdf_is_stream(xref, fz_to_num(n), fz_to_gen(n)))
{
@@ -208,9 +225,9 @@ pdf_load_annots(pdf_annot **annotp, pdf_xref *xref, fz_obj *annots)
continue;
}
- annot = fz_malloc(sizeof(pdf_annot));
+ annot = fz_malloc(ctx, sizeof(pdf_annot));
annot->obj = fz_keep_obj(obj);
- annot->rect = pdf_to_rect(rect);
+ annot->rect = pdf_to_rect(ctx, rect);
annot->ap = form;
annot->next = NULL;
diff --git a/pdf/pdf_cmap.c b/pdf/pdf_cmap.c
index 2c5048ae..0c446470 100644
--- a/pdf/pdf_cmap.c
+++ b/pdf/pdf_cmap.c
@@ -31,11 +31,11 @@
*/
pdf_cmap *
-pdf_new_cmap(void)
+pdf_new_cmap(fz_context *ctx)
{
pdf_cmap *cmap;
- cmap = fz_malloc(sizeof(pdf_cmap));
+ cmap = fz_malloc(ctx, sizeof(pdf_cmap));
cmap->refs = 1;
strcpy(cmap->cmap_name, "");
@@ -64,28 +64,28 @@ pdf_keep_cmap(pdf_cmap *cmap)
}
void
-pdf_drop_cmap(pdf_cmap *cmap)
+pdf_drop_cmap(fz_context *ctx, pdf_cmap *cmap)
{
if (cmap->refs >= 0)
{
if (--cmap->refs == 0)
{
if (cmap->usecmap)
- pdf_drop_cmap(cmap->usecmap);
- fz_free(cmap->ranges);
- fz_free(cmap->table);
- fz_free(cmap);
+ pdf_drop_cmap(ctx, cmap->usecmap);
+ fz_free(ctx, cmap->ranges);
+ fz_free(ctx, cmap->table);
+ fz_free(ctx, cmap);
}
}
}
void
-pdf_set_usecmap(pdf_cmap *cmap, pdf_cmap *usecmap)
+pdf_set_usecmap(fz_context *ctx, pdf_cmap *cmap, pdf_cmap *usecmap)
{
int i;
if (cmap->usecmap)
- pdf_drop_cmap(cmap->usecmap);
+ pdf_drop_cmap(ctx, cmap->usecmap);
cmap->usecmap = pdf_keep_cmap(usecmap);
if (cmap->codespace_len == 0)
@@ -179,7 +179,7 @@ pdf_add_codespace(pdf_cmap *cmap, int low, int high, int n)
* Add an integer to the table.
*/
static void
-add_table(pdf_cmap *cmap, int value)
+add_table(fz_context *ctx, pdf_cmap *cmap, int value)
{
if (cmap->tlen == USHRT_MAX)
{
@@ -189,7 +189,7 @@ add_table(pdf_cmap *cmap, int value)
if (cmap->tlen + 1 > cmap->tcap)
{
cmap->tcap = cmap->tcap > 1 ? (cmap->tcap * 3) / 2 : 256;
- cmap->table = fz_realloc(cmap->table, cmap->tcap, sizeof(unsigned short));
+ cmap->table = fz_realloc(ctx, cmap->table, cmap->tcap * sizeof(unsigned short));
}
cmap->table[cmap->tlen++] = value;
}
@@ -198,19 +198,19 @@ add_table(pdf_cmap *cmap, int value)
* Add a range.
*/
static void
-add_range(pdf_cmap *cmap, int low, int high, int flag, int offset)
+add_range(fz_context *ctx, pdf_cmap *cmap, int low, int high, int flag, int offset)
{
/* If the range is too large to be represented, split it */
if (high - low > 0x3fff)
{
- add_range(cmap, low, low+0x3fff, flag, offset);
- add_range(cmap, low+0x3fff, high, flag, offset+0x3fff);
+ add_range(ctx, cmap, low, low+0x3fff, flag, offset);
+ add_range(ctx, cmap, low+0x3fff, high, flag, offset+0x3fff);
return;
}
if (cmap->rlen + 1 > cmap->rcap)
{
cmap->rcap = cmap->rcap > 1 ? (cmap->rcap * 3) / 2 : 256;
- cmap->ranges = fz_realloc(cmap->ranges, cmap->rcap, sizeof(pdf_range));
+ cmap->ranges = fz_realloc(ctx, cmap->ranges, cmap->rcap * sizeof(pdf_range));
}
cmap->ranges[cmap->rlen].low = low;
pdf_range_set_high(&cmap->ranges[cmap->rlen], high);
@@ -223,7 +223,7 @@ add_range(pdf_cmap *cmap, int low, int high, int flag, int offset)
* Add a range-to-table mapping.
*/
void
-pdf_map_range_to_table(pdf_cmap *cmap, int low, int *table, int len)
+pdf_map_range_to_table(fz_context *ctx, pdf_cmap *cmap, int low, int *table, int len)
{
int i;
int high = low + len;
@@ -233,8 +233,8 @@ pdf_map_range_to_table(pdf_cmap *cmap, int low, int *table, int len)
else
{
for (i = 0; i < len; i++)
- add_table(cmap, table[i]);
- add_range(cmap, low, high, PDF_CMAP_TABLE, offset);
+ add_table(ctx, cmap, table[i]);
+ add_range(ctx, cmap, low, high, PDF_CMAP_TABLE, offset);
}
}
@@ -242,22 +242,22 @@ pdf_map_range_to_table(pdf_cmap *cmap, int low, int *table, int len)
* Add a range of contiguous one-to-one mappings (ie 1..5 maps to 21..25)
*/
void
-pdf_map_range_to_range(pdf_cmap *cmap, int low, int high, int offset)
+pdf_map_range_to_range(fz_context *ctx, pdf_cmap *cmap, int low, int high, int offset)
{
- add_range(cmap, low, high, high - low == 0 ? PDF_CMAP_SINGLE : PDF_CMAP_RANGE, offset);
+ add_range(ctx, cmap, low, high, high - low == 0 ? PDF_CMAP_SINGLE : PDF_CMAP_RANGE, offset);
}
/*
* Add a single one-to-many mapping.
*/
void
-pdf_map_one_to_many(pdf_cmap *cmap, int low, int *values, int len)
+pdf_map_one_to_many(fz_context *ctx, pdf_cmap *cmap, int low, int *values, int len)
{
int offset, i;
if (len == 1)
{
- add_range(cmap, low, low, PDF_CMAP_SINGLE, values[0]);
+ add_range(ctx, cmap, low, low, PDF_CMAP_SINGLE, values[0]);
return;
}
@@ -280,10 +280,10 @@ pdf_map_one_to_many(pdf_cmap *cmap, int low, int *values, int len)
else
{
offset = cmap->tlen;
- add_table(cmap, len);
+ add_table(ctx, cmap, len);
for (i = 0; i < len; i++)
- add_table(cmap, values[i]);
- add_range(cmap, low, low, PDF_CMAP_MULTI, offset);
+ add_table(ctx, cmap, values[i]);
+ add_range(ctx, cmap, low, low, PDF_CMAP_MULTI, offset);
}
}
@@ -299,7 +299,7 @@ static int cmprange(const void *va, const void *vb)
}
void
-pdf_sort_cmap(pdf_cmap *cmap)
+pdf_sort_cmap(fz_context *ctx, pdf_cmap *cmap)
{
pdf_range *a; /* last written range on output */
pdf_range *b; /* current range examined on input */
@@ -343,7 +343,7 @@ pdf_sort_cmap(pdf_cmap *cmap)
else if (pdf_range_flags(a) == PDF_CMAP_TABLE && pdf_range_flags(b) == PDF_CMAP_SINGLE && (pdf_range_high(b) - a->low <= 0x3fff))
{
pdf_range_set_high(a, pdf_range_high(b));
- add_table(cmap, b->offset);
+ add_table(ctx, cmap, b->offset);
}
/* LR -> LR */
@@ -367,8 +367,8 @@ pdf_sort_cmap(pdf_cmap *cmap)
{
pdf_range_set_flags(a, PDF_CMAP_TABLE);
pdf_range_set_high(a, pdf_range_high(b));
- add_table(cmap, a->offset);
- add_table(cmap, b->offset);
+ add_table(ctx, cmap, a->offset);
+ add_table(ctx, cmap, b->offset);
a->offset = cmap->tlen - 2;
}
@@ -376,7 +376,7 @@ pdf_sort_cmap(pdf_cmap *cmap)
else if (pdf_range_flags(a) == PDF_CMAP_TABLE && pdf_range_flags(b) == PDF_CMAP_SINGLE && (pdf_range_high(b) - a->low <= 0x3fff))
{
pdf_range_set_high(a, pdf_range_high(b));
- add_table(cmap, b->offset);
+ add_table(ctx, cmap, b->offset);
}
/* XX -> XX */
diff --git a/pdf/pdf_cmap_load.c b/pdf/pdf_cmap_load.c
index 7acc60d3..b9c2a5f4 100644
--- a/pdf/pdf_cmap_load.c
+++ b/pdf/pdf_cmap_load.c
@@ -13,8 +13,9 @@ pdf_load_embedded_cmap(pdf_cmap **cmapp, pdf_xref *xref, fz_obj *stmobj)
pdf_cmap *usecmap;
fz_obj *wmode;
fz_obj *obj;
+ fz_context *ctx = xref->ctx;
- if ((*cmapp = pdf_find_item(xref->store, pdf_drop_cmap, stmobj)))
+ if ((*cmapp = pdf_find_item(ctx, xref->store, (pdf_store_drop_fn *)pdf_drop_cmap, stmobj)))
{
pdf_keep_cmap(*cmapp);
return fz_okay;
@@ -36,21 +37,21 @@ pdf_load_embedded_cmap(pdf_cmap **cmapp, pdf_xref *xref, fz_obj *stmobj)
fz_close(file);
- wmode = fz_dict_gets(stmobj, "WMode");
- if (fz_is_int(wmode))
- pdf_set_wmode(cmap, fz_to_int(wmode));
+ wmode = fz_dict_gets(ctx, stmobj, "WMode");
+ if (fz_is_int(ctx, wmode))
+ pdf_set_wmode(cmap, fz_to_int(ctx, wmode));
- obj = fz_dict_gets(stmobj, "UseCMap");
- if (fz_is_name(obj))
+ obj = fz_dict_gets(ctx, stmobj, "UseCMap");
+ if (fz_is_name(ctx, obj))
{
- error = pdf_load_system_cmap(&usecmap, fz_to_name(obj));
+ error = pdf_load_system_cmap(ctx, &usecmap, fz_to_name(ctx, obj));
if (error)
{
- error = fz_error_note(error, "cannot load system usecmap '%s'", fz_to_name(obj));
+ error = fz_error_note(error, "cannot load system usecmap '%s'", fz_to_name(ctx, obj));
goto cleanup;
}
- pdf_set_usecmap(cmap, usecmap);
- pdf_drop_cmap(usecmap);
+ pdf_set_usecmap(ctx, cmap, usecmap);
+ pdf_drop_cmap(ctx, usecmap);
}
else if (fz_is_indirect(obj))
{
@@ -60,11 +61,11 @@ pdf_load_embedded_cmap(pdf_cmap **cmapp, pdf_xref *xref, fz_obj *stmobj)
error = fz_error_note(error, "cannot load embedded usecmap (%d %d R)", fz_to_num(obj), fz_to_gen(obj));
goto cleanup;
}
- pdf_set_usecmap(cmap, usecmap);
- pdf_drop_cmap(usecmap);
+ pdf_set_usecmap(ctx, cmap, usecmap);
+ pdf_drop_cmap(ctx, usecmap);
}
- pdf_store_item(xref->store, pdf_keep_cmap, pdf_drop_cmap, stmobj, cmap);
+ pdf_store_item(ctx, xref->store, (pdf_store_keep_fn *)pdf_keep_cmap, (pdf_store_drop_fn *)pdf_drop_cmap, stmobj, cmap);
*cmapp = cmap;
return fz_okay;
@@ -73,7 +74,7 @@ cleanup:
if (file)
fz_close(file);
if (cmap)
- pdf_drop_cmap(cmap);
+ pdf_drop_cmap(ctx, cmap);
return error; /* already rethrown */
}
@@ -81,13 +82,13 @@ cleanup:
* Create an Identity-* CMap (for both 1 and 2-byte encodings)
*/
pdf_cmap *
-pdf_new_identity_cmap(int wmode, int bytes)
+pdf_new_identity_cmap(fz_context *ctx, int wmode, int bytes)
{
- pdf_cmap *cmap = pdf_new_cmap();
+ pdf_cmap *cmap = pdf_new_cmap(ctx);
sprintf(cmap->cmap_name, "Identity-%c", wmode ? 'V' : 'H');
pdf_add_codespace(cmap, 0x0000, 0xffff, bytes);
- pdf_map_range_to_range(cmap, 0x0000, 0xffff, 0);
- pdf_sort_cmap(cmap);
+ pdf_map_range_to_range(ctx, cmap, 0x0000, 0xffff, 0);
+ pdf_sort_cmap(ctx, cmap);
pdf_set_wmode(cmap, wmode);
return cmap;
}
@@ -96,7 +97,7 @@ pdf_new_identity_cmap(int wmode, int bytes)
* Load predefined CMap from system.
*/
fz_error
-pdf_load_system_cmap(pdf_cmap **cmapp, char *cmap_name)
+pdf_load_system_cmap(fz_context *ctx, pdf_cmap **cmapp, char *cmap_name)
{
pdf_cmap *usecmap;
pdf_cmap *cmap;
@@ -110,7 +111,7 @@ pdf_load_system_cmap(pdf_cmap **cmapp, char *cmap_name)
usecmap = pdf_find_builtin_cmap(cmap->usecmap_name);
if (!usecmap)
return fz_error_make("nu builtin cmap file: %s", cmap->usecmap_name);
- pdf_set_usecmap(cmap, usecmap);
+ pdf_set_usecmap(ctx, cmap, usecmap);
}
*cmapp = cmap;
diff --git a/pdf/pdf_cmap_parse.c b/pdf/pdf_cmap_parse.c
index 06421fbb..1e71e176 100644
--- a/pdf/pdf_cmap_parse.c
+++ b/pdf/pdf_cmap_parse.c
@@ -180,7 +180,7 @@ pdf_parse_cid_range(pdf_cmap *cmap, fz_stream *file)
dst = atoi(buf);
- pdf_map_range_to_range(cmap, lo, hi, dst);
+ pdf_map_range_to_range(file->ctx, cmap, lo, hi, dst);
}
}
@@ -215,7 +215,7 @@ pdf_parse_cid_char(pdf_cmap *cmap, fz_stream *file)
dst = atoi(buf);
- pdf_map_range_to_range(cmap, src, src, dst);
+ pdf_map_range_to_range(file->ctx, cmap, src, src, dst);
}
}
@@ -247,7 +247,7 @@ pdf_parse_bf_range_array(pdf_cmap *cmap, fz_stream *file, int lo, int hi)
for (i = 0; i < len / 2; i++)
dst[i] = pdf_code_from_string(buf + i * 2, 2);
- pdf_map_one_to_many(cmap, lo, dst, len / 2);
+ pdf_map_one_to_many(file->ctx, cmap, lo, dst, len / 2);
}
lo ++;
@@ -294,7 +294,7 @@ pdf_parse_bf_range(pdf_cmap *cmap, fz_stream *file)
if (len == 2)
{
dst = pdf_code_from_string(buf, len);
- pdf_map_range_to_range(cmap, lo, hi, dst);
+ pdf_map_range_to_range(file->ctx, cmap, lo, hi, dst);
}
else
{
@@ -309,7 +309,7 @@ pdf_parse_bf_range(pdf_cmap *cmap, fz_stream *file)
while (lo <= hi)
{
dststr[i-1] ++;
- pdf_map_one_to_many(cmap, lo, dststr, i);
+ pdf_map_one_to_many(file->ctx, cmap, lo, dststr, i);
lo ++;
}
}
@@ -366,7 +366,7 @@ pdf_parse_bf_char(pdf_cmap *cmap, fz_stream *file)
{
for (i = 0; i < len / 2; i++)
dst[i] = pdf_code_from_string(buf + i * 2, 2);
- pdf_map_one_to_many(cmap, src, dst, i);
+ pdf_map_one_to_many(file->ctx, cmap, src, dst, i);
}
}
}
@@ -381,7 +381,7 @@ pdf_parse_cmap(pdf_cmap **cmapp, fz_stream *file)
int tok;
int len;
- cmap = pdf_new_cmap();
+ cmap = pdf_new_cmap(file->ctx);
strcpy(key, ".notdef");
@@ -479,12 +479,12 @@ pdf_parse_cmap(pdf_cmap **cmapp, fz_stream *file)
/* ignore everything else */
}
- pdf_sort_cmap(cmap);
+ pdf_sort_cmap(file->ctx, cmap);
*cmapp = cmap;
return fz_okay;
cleanup:
- pdf_drop_cmap(cmap);
+ pdf_drop_cmap(file->ctx, cmap);
return error; /* already rethrown */
}
diff --git a/pdf/pdf_colorspace.c b/pdf/pdf_colorspace.c
index 2fe5bccd..a83cde09 100644
--- a/pdf/pdf_colorspace.c
+++ b/pdf/pdf_colorspace.c
@@ -8,7 +8,7 @@ load_icc_based(fz_colorspace **csp, pdf_xref *xref, fz_obj *dict)
{
int n;
- n = fz_to_int(fz_dict_gets(dict, "N"));
+ n = fz_to_int(xref->ctx, fz_dict_gets(xref->ctx, dict, "N"));
switch (n)
{
@@ -81,12 +81,12 @@ separation_to_rgb(fz_colorspace *cs, float *color, float *rgb)
}
static void
-free_separation(fz_colorspace *cs)
+free_separation(fz_context *ctx, fz_colorspace *cs)
{
struct separation *sep = cs->data;
- fz_drop_colorspace(sep->base);
- pdf_drop_function(sep->tint);
- fz_free(sep);
+ fz_drop_colorspace(ctx, sep->base);
+ pdf_drop_function(ctx, sep->tint);
+ fz_free(ctx, sep);
}
static fz_error
@@ -95,15 +95,16 @@ load_separation(fz_colorspace **csp, pdf_xref *xref, fz_obj *array)
fz_error error;
fz_colorspace *cs;
struct separation *sep;
- fz_obj *nameobj = fz_array_get(array, 1);
- fz_obj *baseobj = fz_array_get(array, 2);
- fz_obj *tintobj = fz_array_get(array, 3);
+ fz_context *ctx = xref->ctx;
+ fz_obj *nameobj = fz_array_get(ctx, array, 1);
+ fz_obj *baseobj = fz_array_get(ctx, array, 2);
+ fz_obj *tintobj = fz_array_get(ctx, array, 3);
fz_colorspace *base;
pdf_function *tint;
int n;
- if (fz_is_array(nameobj))
- n = fz_array_len(nameobj);
+ if (fz_is_array(ctx, nameobj))
+ n = fz_array_len(ctx, nameobj);
else
n = 1;
@@ -117,15 +118,15 @@ load_separation(fz_colorspace **csp, pdf_xref *xref, fz_obj *array)
error = pdf_load_function(&tint, xref, tintobj);
if (error)
{
- fz_drop_colorspace(base);
+ fz_drop_colorspace(ctx, base);
return fz_error_note(error, "cannot load tint function (%d %d R)", fz_to_num(tintobj), fz_to_gen(tintobj));
}
- sep = fz_malloc(sizeof(struct separation));
+ sep = fz_malloc(ctx, sizeof(struct separation));
sep->base = base;
sep->tint = tint;
- cs = fz_new_colorspace(n == 1 ? "Separation" : "DeviceN", n);
+ cs = fz_new_colorspace(ctx, n == 1 ? "Separation" : "DeviceN", n);
cs->to_rgb = separation_to_rgb;
cs->free_data = free_separation;
cs->data = sep;
@@ -157,17 +158,17 @@ indexed_to_rgb(fz_colorspace *cs, float *color, float *rgb)
}
static void
-free_indexed(fz_colorspace *cs)
+free_indexed(fz_context *ctx, fz_colorspace *cs)
{
struct indexed *idx = cs->data;
if (idx->base)
- fz_drop_colorspace(idx->base);
- fz_free(idx->lookup);
- fz_free(idx);
+ fz_drop_colorspace(ctx, idx->base);
+ fz_free(ctx, idx->lookup);
+ fz_free(ctx, idx);
}
fz_pixmap *
-pdf_expand_indexed_pixmap(fz_pixmap *src)
+pdf_expand_indexed_pixmap(fz_context *ctx, fz_pixmap *src)
{
struct indexed *idx;
fz_pixmap *dst;
@@ -183,7 +184,7 @@ pdf_expand_indexed_pixmap(fz_pixmap *src)
lookup = idx->lookup;
n = idx->base->n;
- dst = fz_new_pixmap_with_rect(idx->base, fz_bound_pixmap(src));
+ dst = fz_new_pixmap_with_rect(ctx, idx->base, fz_bound_pixmap(src));
s = src->samples;
d = dst->samples;
@@ -213,9 +214,10 @@ load_indexed(fz_colorspace **csp, pdf_xref *xref, fz_obj *array)
fz_error error;
fz_colorspace *cs;
struct indexed *idx;
- fz_obj *baseobj = fz_array_get(array, 1);
- fz_obj *highobj = fz_array_get(array, 2);
- fz_obj *lookup = fz_array_get(array, 3);
+ fz_context *ctx = xref->ctx;
+ fz_obj *baseobj = fz_array_get(ctx, array, 1);
+ fz_obj *highobj = fz_array_get(ctx, array, 2);
+ fz_obj *lookup = fz_array_get(ctx, array, 3);
fz_colorspace *base;
int i, n;
@@ -223,22 +225,21 @@ load_indexed(fz_colorspace **csp, pdf_xref *xref, fz_obj *array)
if (error)
return fz_error_note(error, "cannot load base colorspace (%d %d R)", fz_to_num(baseobj), fz_to_gen(baseobj));
- idx = fz_malloc(sizeof(struct indexed));
+ idx = fz_malloc(ctx, sizeof(struct indexed));
idx->base = base;
- idx->high = fz_to_int(highobj);
+ idx->high = fz_to_int(ctx, highobj);
idx->high = CLAMP(idx->high, 0, 255);
n = base->n * (idx->high + 1);
- idx->lookup = fz_malloc(n);
- memset(idx->lookup, 0, n);
+ idx->lookup = fz_calloc(ctx, 1, n);
- cs = fz_new_colorspace("Indexed", 1);
+ cs = fz_new_colorspace(ctx, "Indexed", 1);
cs->to_rgb = indexed_to_rgb;
cs->free_data = free_indexed;
cs->data = idx;
- if (fz_is_string(lookup) && fz_to_str_len(lookup) == n)
+ if (fz_is_string(ctx, lookup) && fz_to_str_len(ctx, lookup) == n)
{
- unsigned char *buf = (unsigned char *) fz_to_str_buf(lookup);
+ unsigned char *buf = (unsigned char *) fz_to_str_buf(ctx, lookup);
for (i = 0; i < n; i++)
idx->lookup[i] = buf[i];
}
@@ -249,14 +250,14 @@ load_indexed(fz_colorspace **csp, pdf_xref *xref, fz_obj *array)
error = pdf_open_stream(&file, xref, fz_to_num(lookup), fz_to_gen(lookup));
if (error)
{
- fz_drop_colorspace(cs);
+ fz_drop_colorspace(ctx, cs);
return fz_error_note(error, "cannot open colorspace lookup table (%d 0 R)", fz_to_num(lookup));
}
i = fz_read(file, idx->lookup, n);
if (i < 0)
{
- fz_drop_colorspace(cs);
+ fz_drop_colorspace(ctx, cs);
return fz_error_make("cannot read colorspace lookup table (%d 0 R)", fz_to_num(lookup));
}
@@ -264,7 +265,7 @@ load_indexed(fz_colorspace **csp, pdf_xref *xref, fz_obj *array)
}
else
{
- fz_drop_colorspace(cs);
+ fz_drop_colorspace(ctx, cs);
return fz_error_make("cannot parse colorspace lookup table");
}
@@ -277,39 +278,41 @@ load_indexed(fz_colorspace **csp, pdf_xref *xref, fz_obj *array)
static fz_error
pdf_load_colorspace_imp(fz_colorspace **csp, pdf_xref *xref, fz_obj *obj)
{
- if (fz_is_name(obj))
+ fz_context *ctx = xref->ctx;
+
+ if (fz_is_name(ctx, obj))
{
- if (!strcmp(fz_to_name(obj), "Pattern"))
+ if (!strcmp(fz_to_name(ctx, obj), "Pattern"))
*csp = fz_device_gray;
- else if (!strcmp(fz_to_name(obj), "G"))
+ else if (!strcmp(fz_to_name(ctx, obj), "G"))
*csp = fz_device_gray;
- else if (!strcmp(fz_to_name(obj), "RGB"))
+ else if (!strcmp(fz_to_name(ctx, obj), "RGB"))
*csp = fz_device_rgb;
- else if (!strcmp(fz_to_name(obj), "CMYK"))
+ else if (!strcmp(fz_to_name(ctx, obj), "CMYK"))
*csp = fz_device_cmyk;
- else if (!strcmp(fz_to_name(obj), "DeviceGray"))
+ else if (!strcmp(fz_to_name(ctx, obj), "DeviceGray"))
*csp = fz_device_gray;
- else if (!strcmp(fz_to_name(obj), "DeviceRGB"))
+ else if (!strcmp(fz_to_name(ctx, obj), "DeviceRGB"))
*csp = fz_device_rgb;
- else if (!strcmp(fz_to_name(obj), "DeviceCMYK"))
+ else if (!strcmp(fz_to_name(ctx, obj), "DeviceCMYK"))
*csp = fz_device_cmyk;
else
- return fz_error_make("unknown colorspace: %s", fz_to_name(obj));
+ return fz_error_make("unknown colorspace: %s", fz_to_name(ctx, obj));
return fz_okay;
}
- else if (fz_is_array(obj))
+ else if (fz_is_array(ctx, obj))
{
- fz_obj *name = fz_array_get(obj, 0);
+ fz_obj *name = fz_array_get(ctx, obj, 0);
- if (fz_is_name(name))
+ if (fz_is_name(ctx, name))
{
/* load base colorspace instead */
- if (!strcmp(fz_to_name(name), "Pattern"))
+ if (!strcmp(fz_to_name(ctx, name), "Pattern"))
{
fz_error error;
- obj = fz_array_get(obj, 1);
+ obj = fz_array_get(ctx, obj, 1);
if (!obj)
{
*csp = fz_device_gray;
@@ -321,43 +324,43 @@ pdf_load_colorspace_imp(fz_colorspace **csp, pdf_xref *xref, fz_obj *obj)
return fz_error_note(error, "cannot load pattern (%d %d R)", fz_to_num(obj), fz_to_gen(obj));
}
- else if (!strcmp(fz_to_name(name), "G"))
+ else if (!strcmp(fz_to_name(ctx, name), "G"))
*csp = fz_device_gray;
- else if (!strcmp(fz_to_name(name), "RGB"))
+ else if (!strcmp(fz_to_name(ctx, name), "RGB"))
*csp = fz_device_rgb;
- else if (!strcmp(fz_to_name(name), "CMYK"))
+ else if (!strcmp(fz_to_name(ctx, name), "CMYK"))
*csp = fz_device_cmyk;
- else if (!strcmp(fz_to_name(name), "DeviceGray"))
+ else if (!strcmp(fz_to_name(ctx, name), "DeviceGray"))
*csp = fz_device_gray;
- else if (!strcmp(fz_to_name(name), "DeviceRGB"))
+ else if (!strcmp(fz_to_name(ctx, name), "DeviceRGB"))
*csp = fz_device_rgb;
- else if (!strcmp(fz_to_name(name), "DeviceCMYK"))
+ else if (!strcmp(fz_to_name(ctx, name), "DeviceCMYK"))
*csp = fz_device_cmyk;
- else if (!strcmp(fz_to_name(name), "CalGray"))
+ else if (!strcmp(fz_to_name(ctx, name), "CalGray"))
*csp = fz_device_gray;
- else if (!strcmp(fz_to_name(name), "CalRGB"))
+ else if (!strcmp(fz_to_name(ctx, name), "CalRGB"))
*csp = fz_device_rgb;
- else if (!strcmp(fz_to_name(name), "CalCMYK"))
+ else if (!strcmp(fz_to_name(ctx, name), "CalCMYK"))
*csp = fz_device_cmyk;
- else if (!strcmp(fz_to_name(name), "Lab"))
+ else if (!strcmp(fz_to_name(ctx, name), "Lab"))
*csp = fz_device_lab;
- else if (!strcmp(fz_to_name(name), "ICCBased"))
- return load_icc_based(csp, xref, fz_array_get(obj, 1));
+ else if (!strcmp(fz_to_name(ctx, name), "ICCBased"))
+ return load_icc_based(csp, xref, fz_array_get(ctx, obj, 1));
- else if (!strcmp(fz_to_name(name), "Indexed"))
+ else if (!strcmp(fz_to_name(ctx, name), "Indexed"))
return load_indexed(csp, xref, obj);
- else if (!strcmp(fz_to_name(name), "I"))
+ else if (!strcmp(fz_to_name(ctx, name), "I"))
return load_indexed(csp, xref, obj);
- else if (!strcmp(fz_to_name(name), "Separation"))
+ else if (!strcmp(fz_to_name(ctx, name), "Separation"))
return load_separation(csp, xref, obj);
- else if (!strcmp(fz_to_name(name), "DeviceN"))
+ else if (!strcmp(fz_to_name(ctx, name), "DeviceN"))
return load_separation(csp, xref, obj);
else
- return fz_error_make("syntaxerror: unknown colorspace %s", fz_to_name(name));
+ return fz_error_make("syntaxerror: unknown colorspace %s", fz_to_name(ctx, name));
return fz_okay;
}
@@ -370,8 +373,9 @@ fz_error
pdf_load_colorspace(fz_colorspace **csp, pdf_xref *xref, fz_obj *obj)
{
fz_error error;
+ fz_context *ctx = xref->ctx;
- if ((*csp = pdf_find_item(xref->store, fz_drop_colorspace, obj)))
+ if ((*csp = pdf_find_item(ctx, xref->store, (pdf_store_drop_fn *)fz_drop_colorspace, obj)))
{
fz_keep_colorspace(*csp);
return fz_okay;
@@ -381,7 +385,7 @@ pdf_load_colorspace(fz_colorspace **csp, pdf_xref *xref, fz_obj *obj)
if (error)
return fz_error_note(error, "cannot load colorspace (%d %d R)", fz_to_num(obj), fz_to_gen(obj));
- pdf_store_item(xref->store, fz_keep_colorspace, fz_drop_colorspace, obj, *csp);
+ pdf_store_item(ctx, xref->store, (pdf_store_keep_fn *)fz_keep_colorspace, (pdf_store_drop_fn *)fz_drop_colorspace, obj, *csp);
return fz_okay;
}
diff --git a/pdf/pdf_crypt.c b/pdf/pdf_crypt.c
index 0bf52357..81f3781a 100644
--- a/pdf/pdf_crypt.c
+++ b/pdf/pdf_crypt.c
@@ -37,9 +37,10 @@ struct pdf_crypt_s
int encrypt_metadata;
unsigned char key[32]; /* decryption key generated from password */
+ fz_context *ctx;
};
-static fz_error pdf_parse_crypt_filter(pdf_crypt_filter *cf, fz_obj *dict, char *name, int defaultlength);
+static fz_error pdf_parse_crypt_filter(fz_context *ctx, pdf_crypt_filter *cf, fz_obj *dict, char *name, int defaultlength);
/*
* Create crypt object for decrypting strings and streams
@@ -47,45 +48,44 @@ static fz_error pdf_parse_crypt_filter(pdf_crypt_filter *cf, fz_obj *dict, char
*/
fz_error
-pdf_new_crypt(pdf_crypt **cryptp, fz_obj *dict, fz_obj *id)
+pdf_new_crypt(fz_context *ctx, pdf_crypt **cryptp, fz_obj *dict, fz_obj *id)
{
pdf_crypt *crypt;
fz_error error;
fz_obj *obj;
- crypt = fz_malloc(sizeof(pdf_crypt));
- memset(crypt, 0x00, sizeof(pdf_crypt));
+ crypt = fz_calloc(ctx, 1, sizeof(pdf_crypt));
/* Common to all security handlers (PDF 1.7 table 3.18) */
- obj = fz_dict_gets(dict, "Filter");
- if (!fz_is_name(obj))
+ obj = fz_dict_gets(ctx, dict, "Filter");
+ if (!fz_is_name(ctx, obj))
{
- pdf_free_crypt(crypt);
+ pdf_free_crypt(ctx, crypt);
return fz_error_make("unspecified encryption handler");
}
- if (strcmp(fz_to_name(obj), "Standard") != 0)
+ if (strcmp(fz_to_name(ctx, obj), "Standard") != 0)
{
- pdf_free_crypt(crypt);
- return fz_error_make("unknown encryption handler: '%s'", fz_to_name(obj));
+ pdf_free_crypt(ctx, crypt);
+ return fz_error_make("unknown encryption handler: '%s'", fz_to_name(ctx, obj));
}
crypt->v = 0;
- obj = fz_dict_gets(dict, "V");
- if (fz_is_int(obj))
- crypt->v = fz_to_int(obj);
+ obj = fz_dict_gets(ctx, dict, "V");
+ if (fz_is_int(ctx, obj))
+ crypt->v = fz_to_int(ctx, obj);
if (crypt->v != 1 && crypt->v != 2 && crypt->v != 4 && crypt->v != 5)
{
- pdf_free_crypt(crypt);
+ pdf_free_crypt(ctx, crypt);
return fz_error_make("unknown encryption version");
}
crypt->length = 40;
if (crypt->v == 2 || crypt->v == 4)
{
- obj = fz_dict_gets(dict, "Length");
- if (fz_is_int(obj))
- crypt->length = fz_to_int(obj);
+ obj = fz_dict_gets(ctx, dict, "Length");
+ if (fz_is_int(ctx, obj))
+ crypt->length = fz_to_int(ctx, obj);
/* work-around for pdf generators that assume length is in bytes */
if (crypt->length < 40)
@@ -93,12 +93,12 @@ pdf_new_crypt(pdf_crypt **cryptp, fz_obj *dict, fz_obj *id)
if (crypt->length % 8 != 0)
{
- pdf_free_crypt(crypt);
+ pdf_free_crypt(ctx, crypt);
return fz_error_make("invalid encryption key length");
}
if (crypt->length > 256)
{
- pdf_free_crypt(crypt);
+ pdf_free_crypt(ctx, crypt);
return fz_error_make("invalid encryption key length");
}
}
@@ -123,8 +123,8 @@ pdf_new_crypt(pdf_crypt **cryptp, fz_obj *dict, fz_obj *id)
crypt->strf.method = PDF_CRYPT_NONE;
crypt->strf.length = crypt->length;
- obj = fz_dict_gets(dict, "CF");
- if (fz_is_dict(obj))
+ obj = fz_dict_gets(ctx, dict, "CF");
+ if (fz_is_dict(ctx, obj))
{
crypt->cf = fz_keep_obj(obj);
}
@@ -133,24 +133,24 @@ pdf_new_crypt(pdf_crypt **cryptp, fz_obj *dict, fz_obj *id)
crypt->cf = NULL;
}
- obj = fz_dict_gets(dict, "StmF");
- if (fz_is_name(obj))
+ obj = fz_dict_gets(ctx, dict, "StmF");
+ if (fz_is_name(ctx, obj))
{
- error = pdf_parse_crypt_filter(&crypt->stmf, crypt->cf, fz_to_name(obj), crypt->length);
+ error = pdf_parse_crypt_filter(ctx, &crypt->stmf, crypt->cf, fz_to_name(ctx, obj), crypt->length);
if (error)
{
- pdf_free_crypt(crypt);
+ pdf_free_crypt(ctx, crypt);
return fz_error_note(error, "cannot parse stream crypt filter (%d %d R)", fz_to_num(obj), fz_to_gen(obj));
}
}
- obj = fz_dict_gets(dict, "StrF");
- if (fz_is_name(obj))
+ obj = fz_dict_gets(ctx, dict, "StrF");
+ if (fz_is_name(ctx, obj))
{
- error = pdf_parse_crypt_filter(&crypt->strf, crypt->cf, fz_to_name(obj), crypt->length);
+ error = pdf_parse_crypt_filter(ctx, &crypt->strf, crypt->cf, fz_to_name(ctx, obj), crypt->length);
if (error)
{
- pdf_free_crypt(crypt);
+ pdf_free_crypt(ctx, crypt);
return fz_error_note(error, "cannot parse string crypt filter (%d %d R)", fz_to_num(obj), fz_to_gen(obj));
}
}
@@ -162,82 +162,82 @@ pdf_new_crypt(pdf_crypt **cryptp, fz_obj *dict, fz_obj *id)
/* Standard security handler (PDF 1.7 table 3.19) */
- obj = fz_dict_gets(dict, "R");
- if (fz_is_int(obj))
- crypt->r = fz_to_int(obj);
+ obj = fz_dict_gets(ctx, dict, "R");
+ if (fz_is_int(ctx, obj))
+ crypt->r = fz_to_int(ctx, obj);
else
{
- pdf_free_crypt(crypt);
+ pdf_free_crypt(ctx, crypt);
return fz_error_make("encryption dictionary missing revision value");
}
- obj = fz_dict_gets(dict, "O");
- if (fz_is_string(obj) && fz_to_str_len(obj) == 32)
- memcpy(crypt->o, fz_to_str_buf(obj), 32);
+ obj = fz_dict_gets(ctx, dict, "O");
+ if (fz_is_string(ctx, obj) && fz_to_str_len(ctx, obj) == 32)
+ memcpy(crypt->o, fz_to_str_buf(ctx, obj), 32);
/* /O and /U are supposed to be 48 bytes long for revision 5, they're often longer, though */
- else if (crypt->r == 5 && fz_is_string(obj) && fz_to_str_len(obj) >= 48)
- memcpy(crypt->o, fz_to_str_buf(obj), 48);
+ else if (crypt->r == 5 && fz_is_string(ctx, obj) && fz_to_str_len(ctx, obj) >= 48)
+ memcpy(crypt->o, fz_to_str_buf(ctx, obj), 48);
else
{
- pdf_free_crypt(crypt);
+ pdf_free_crypt(ctx, crypt);
return fz_error_make("encryption dictionary missing owner password");
}
- obj = fz_dict_gets(dict, "U");
- if (fz_is_string(obj) && fz_to_str_len(obj) == 32)
- memcpy(crypt->u, fz_to_str_buf(obj), 32);
- else if (fz_is_string(obj) && fz_to_str_len(obj) >= 48 && crypt->r == 5)
- memcpy(crypt->u, fz_to_str_buf(obj), 48);
- else if (fz_is_string(obj) && fz_to_str_len(obj) < 32)
+ obj = fz_dict_gets(ctx, dict, "U");
+ if (fz_is_string(ctx, obj) && fz_to_str_len(ctx, obj) == 32)
+ memcpy(crypt->u, fz_to_str_buf(ctx, obj), 32);
+ else if (fz_is_string(ctx, obj) && fz_to_str_len(ctx, obj) >= 48 && crypt->r == 5)
+ memcpy(crypt->u, fz_to_str_buf(ctx, obj), 48);
+ else if (fz_is_string(ctx, obj) && fz_to_str_len(ctx, obj) < 32)
{
- fz_warn("encryption password key too short (%d)", fz_to_str_len(obj));
- memcpy(crypt->u, fz_to_str_buf(obj), fz_to_str_len(obj));
+ fz_warn("encryption password key too short (%d)", fz_to_str_len(ctx, obj));
+ memcpy(crypt->u, fz_to_str_buf(ctx, obj), fz_to_str_len(ctx, obj));
}
else
{
- pdf_free_crypt(crypt);
+ pdf_free_crypt(ctx, crypt);
return fz_error_make("encryption dictionary missing user password");
}
- obj = fz_dict_gets(dict, "P");
- if (fz_is_int(obj))
- crypt->p = fz_to_int(obj);
+ obj = fz_dict_gets(ctx, dict, "P");
+ if (fz_is_int(ctx, obj))
+ crypt->p = fz_to_int(ctx, obj);
else
{
- pdf_free_crypt(crypt);
+ pdf_free_crypt(ctx, crypt);
return fz_error_make("encryption dictionary missing permissions value");
}
if (crypt->r == 5)
{
- obj = fz_dict_gets(dict, "OE");
- if (!fz_is_string(obj) || fz_to_str_len(obj) != 32)
+ obj = fz_dict_gets(ctx, dict, "OE");
+ if (!fz_is_string(ctx, obj) || fz_to_str_len(ctx, obj) != 32)
{
- pdf_free_crypt(crypt);
+ pdf_free_crypt(ctx, crypt);
return fz_error_make("encryption dictionary missing owner encryption key");
}
- memcpy(crypt->oe, fz_to_str_buf(obj), 32);
+ memcpy(crypt->oe, fz_to_str_buf(ctx, obj), 32);
- obj = fz_dict_gets(dict, "UE");
- if (!fz_is_string(obj) || fz_to_str_len(obj) != 32)
+ obj = fz_dict_gets(ctx, dict, "UE");
+ if (!fz_is_string(ctx, obj) || fz_to_str_len(ctx, obj) != 32)
{
- pdf_free_crypt(crypt);
+ pdf_free_crypt(ctx, crypt);
return fz_error_make("encryption dictionary missing user encryption key");
}
- memcpy(crypt->ue, fz_to_str_buf(obj), 32);
+ memcpy(crypt->ue, fz_to_str_buf(ctx, obj), 32);
}
crypt->encrypt_metadata = 1;
- obj = fz_dict_gets(dict, "EncryptMetadata");
- if (fz_is_bool(obj))
- crypt->encrypt_metadata = fz_to_bool(obj);
+ obj = fz_dict_gets(ctx, dict, "EncryptMetadata");
+ if (fz_is_bool(ctx, obj))
+ crypt->encrypt_metadata = fz_to_bool(ctx, obj);
/* Extract file identifier string */
- if (fz_is_array(id) && fz_array_len(id) == 2)
+ if (fz_is_array(ctx, id) && fz_array_len(ctx, id) == 2)
{
- obj = fz_array_get(id, 0);
- if (fz_is_string(obj))
+ obj = fz_array_get(ctx, id, 0);
+ if (fz_is_string(ctx, obj))
crypt->id = fz_keep_obj(obj);
}
else
@@ -248,11 +248,11 @@ pdf_new_crypt(pdf_crypt **cryptp, fz_obj *dict, fz_obj *id)
}
void
-pdf_free_crypt(pdf_crypt *crypt)
+pdf_free_crypt(fz_context *ctx, pdf_crypt *crypt)
{
- if (crypt->id) fz_drop_obj(crypt->id);
- if (crypt->cf) fz_drop_obj(crypt->cf);
- fz_free(crypt);
+ if (crypt->id) fz_drop_obj(ctx, crypt->id);
+ if (crypt->cf) fz_drop_obj(ctx, crypt->cf);
+ fz_free(ctx, crypt);
}
/*
@@ -260,7 +260,7 @@ pdf_free_crypt(pdf_crypt *crypt)
*/
static fz_error
-pdf_parse_crypt_filter(pdf_crypt_filter *cf, fz_obj *cf_obj, char *name, int defaultlength)
+pdf_parse_crypt_filter(fz_context *ctx, pdf_crypt_filter *cf, fz_obj *cf_obj, char *name, int defaultlength)
{
fz_obj *obj;
fz_obj *dict;
@@ -280,29 +280,29 @@ pdf_parse_crypt_filter(pdf_crypt_filter *cf, fz_obj *cf_obj, char *name, int def
return fz_okay;
}
- dict = fz_dict_gets(cf_obj, name);
- if (!fz_is_dict(dict))
+ dict = fz_dict_gets(ctx, cf_obj, name);
+ if (!fz_is_dict(ctx, dict))
{
return fz_error_make("cannot parse crypt filter (%d %d R)", fz_to_num(cf_obj), fz_to_gen(cf_obj));
}
- obj = fz_dict_gets(dict, "CFM");
- if (fz_is_name(obj))
+ obj = fz_dict_gets(ctx, dict, "CFM");
+ if (fz_is_name(ctx, obj))
{
- if (!strcmp(fz_to_name(obj), "None"))
+ if (!strcmp(fz_to_name(ctx, obj), "None"))
cf->method = PDF_CRYPT_NONE;
- else if (!strcmp(fz_to_name(obj), "V2"))
+ else if (!strcmp(fz_to_name(ctx, obj), "V2"))
cf->method = PDF_CRYPT_RC4;
- else if (!strcmp(fz_to_name(obj), "AESV2"))
+ else if (!strcmp(fz_to_name(ctx, obj), "AESV2"))
cf->method = PDF_CRYPT_AESV2;
- else if (!strcmp(fz_to_name(obj), "AESV3"))
+ else if (!strcmp(fz_to_name(ctx, obj), "AESV3"))
cf->method = PDF_CRYPT_AESV3;
else
- fz_error_make("unknown encryption method: %s", fz_to_name(obj));
+ fz_error_make("unknown encryption method: %s", fz_to_name(ctx, obj));
}
- obj = fz_dict_gets(dict, "Length");
- if (fz_is_int(obj))
- cf->length = fz_to_int(obj);
+ obj = fz_dict_gets(ctx, dict, "Length");
+ if (fz_is_int(ctx, obj))
+ cf->length = fz_to_int(ctx, obj);
/* the length for crypt filters is supposed to be in bytes not bits */
if (cf->length < 40)
@@ -358,7 +358,7 @@ pdf_compute_encryption_key(pdf_crypt *crypt, unsigned char *password, int pwlen,
fz_md5_update(&md5, buf, 4);
/* Step 5 - pass first element of ID array */
- fz_md5_update(&md5, (unsigned char *)fz_to_str_buf(crypt->id), fz_to_str_len(crypt->id));
+ fz_md5_update(&md5, (unsigned char *)fz_to_str_buf(crypt->ctx, crypt->id), fz_to_str_len(crypt->ctx, crypt->id));
/* Step 6 (revision 4 or greater) - if metadata is not encrypted pass 0xFFFFFFFF */
if (crypt->r >= 4)
@@ -467,7 +467,7 @@ pdf_compute_user_password(pdf_crypt *crypt, unsigned char *password, int pwlen,
fz_md5_init(&md5);
fz_md5_update(&md5, padding, 32);
- fz_md5_update(&md5, (unsigned char*)fz_to_str_buf(crypt->id), fz_to_str_len(crypt->id));
+ fz_md5_update(&md5, (unsigned char*)fz_to_str_buf(crypt->ctx, crypt->id), fz_to_str_len(crypt->ctx, crypt->id));
fz_md5_final(&md5, digest);
fz_arc4_init(&arc4, crypt->key, n);
@@ -707,7 +707,7 @@ pdf_compute_object_key(pdf_crypt *crypt, pdf_crypt_filter *cf, int num, int gen,
*/
static void
-pdf_crypt_obj_imp(pdf_crypt *crypt, fz_obj *obj, unsigned char *key, int keylen)
+pdf_crypt_obj_imp(fz_context *ctx, pdf_crypt *crypt, fz_obj *obj, unsigned char *key, int keylen)
{
unsigned char *s;
int i, n;
@@ -715,10 +715,10 @@ pdf_crypt_obj_imp(pdf_crypt *crypt, fz_obj *obj, unsigned char *key, int keylen)
if (fz_is_indirect(obj))
return;
- if (fz_is_string(obj))
+ if (fz_is_string(ctx, obj))
{
- s = (unsigned char *) fz_to_str_buf(obj);
- n = fz_to_str_len(obj);
+ s = (unsigned char *)fz_to_str_buf(ctx, obj);
+ n = fz_to_str_len(ctx, obj);
if (crypt->strf.method == PDF_CRYPT_RC4)
{
@@ -742,39 +742,39 @@ pdf_crypt_obj_imp(pdf_crypt *crypt, fz_obj *obj, unsigned char *key, int keylen)
if (s[n - 17] < 1 || s[n - 17] > 16)
fz_warn("aes padding out of range");
else
- fz_set_str_len(obj, n - 16 - s[n - 17]);
+ fz_set_str_len(ctx, obj, n - 16 - s[n - 17]);
}
}
}
- else if (fz_is_array(obj))
+ else if (fz_is_array(ctx, obj))
{
- n = fz_array_len(obj);
+ n = fz_array_len(ctx, obj);
for (i = 0; i < n; i++)
{
- pdf_crypt_obj_imp(crypt, fz_array_get(obj, i), key, keylen);
+ pdf_crypt_obj_imp(ctx, crypt, fz_array_get(ctx, obj, i), key, keylen);
}
}
- else if (fz_is_dict(obj))
+ else if (fz_is_dict(ctx, obj))
{
- n = fz_dict_len(obj);
+ n = fz_dict_len(ctx, obj);
for (i = 0; i < n; i++)
{
- pdf_crypt_obj_imp(crypt, fz_dict_get_val(obj, i), key, keylen);
+ pdf_crypt_obj_imp(ctx, crypt, fz_dict_get_val(ctx, obj, i), key, keylen);
}
}
}
void
-pdf_crypt_obj(pdf_crypt *crypt, fz_obj *obj, int num, int gen)
+pdf_crypt_obj(fz_context *ctx, pdf_crypt *crypt, fz_obj *obj, int num, int gen)
{
unsigned char key[32];
int len;
len = pdf_compute_object_key(crypt, &crypt->strf, num, gen, key);
- pdf_crypt_obj_imp(crypt, obj, key, len);
+ pdf_crypt_obj_imp(ctx, crypt, obj, key, len);
}
/*
@@ -788,6 +788,7 @@ pdf_open_crypt_imp(fz_stream *chain, pdf_crypt *crypt, pdf_crypt_filter *stmf, i
unsigned char key[32];
int len;
+ crypt->ctx = chain->ctx;
len = pdf_compute_object_key(crypt, stmf, num, gen, key);
if (stmf->method == PDF_CRYPT_RC4)
@@ -813,7 +814,7 @@ pdf_open_crypt_with_filter(fz_stream *chain, pdf_crypt *crypt, char *name, int n
if (strcmp(name, "Identity"))
{
- error = pdf_parse_crypt_filter(&cf, crypt->cf, name, crypt->length);
+ error = pdf_parse_crypt_filter(chain->ctx, &cf, crypt->cf, name, crypt->length);
if (error)
fz_error_handle(error, "cannot parse crypt filter (%d %d R)", num, gen);
else
diff --git a/pdf/pdf_font.c b/pdf/pdf_font.c
index 0e4b2229..3b8322db 100644
--- a/pdf/pdf_font.c
+++ b/pdf/pdf_font.c
@@ -171,7 +171,7 @@ static int lookup_mre_code(char *name)
*/
static fz_error
-pdf_load_builtin_font(pdf_font_desc *fontdesc, char *fontname)
+pdf_load_builtin_font(fz_context *ctx, pdf_font_desc *fontdesc, char *fontname)
{
fz_error error;
unsigned char *data;
@@ -181,7 +181,7 @@ pdf_load_builtin_font(pdf_font_desc *fontdesc, char *fontname)
if (!data)
return fz_error_make("cannot find builtin font: '%s'", fontname);
- error = fz_new_font_from_memory(&fontdesc->font, data, len, 0);
+ error = fz_new_font_from_memory(ctx, &fontdesc->font, data, len, 0);
if (error)
return fz_error_note(error, "cannot load freetype font from memory");
@@ -192,7 +192,7 @@ pdf_load_builtin_font(pdf_font_desc *fontdesc, char *fontname)
}
static fz_error
-pdf_load_substitute_font(pdf_font_desc *fontdesc, int mono, int serif, int bold, int italic)
+pdf_load_substitute_font(fz_context *ctx, pdf_font_desc *fontdesc, int mono, int serif, int bold, int italic)
{
fz_error error;
unsigned char *data;
@@ -202,7 +202,7 @@ pdf_load_substitute_font(pdf_font_desc *fontdesc, int mono, int serif, int bold,
if (!data)
return fz_error_make("cannot find substitute font");
- error = fz_new_font_from_memory(&fontdesc->font, data, len, 0);
+ error = fz_new_font_from_memory(ctx, &fontdesc->font, data, len, 0);
if (error)
return fz_error_note(error, "cannot load freetype font from memory");
@@ -213,7 +213,7 @@ pdf_load_substitute_font(pdf_font_desc *fontdesc, int mono, int serif, int bold,
}
static fz_error
-pdf_load_substitute_cjk_font(pdf_font_desc *fontdesc, int ros, int serif)
+pdf_load_substitute_cjk_font(fz_context *ctx, pdf_font_desc *fontdesc, int ros, int serif)
{
fz_error error;
unsigned char *data;
@@ -223,7 +223,7 @@ pdf_load_substitute_cjk_font(pdf_font_desc *fontdesc, int ros, int serif)
if (!data)
return fz_error_make("cannot find builtin CJK font");
- error = fz_new_font_from_memory(&fontdesc->font, data, len, 0);
+ error = fz_new_font_from_memory(ctx, &fontdesc->font, data, len, 0);
if (error)
return fz_error_note(error, "cannot load builtin CJK font");
@@ -232,7 +232,7 @@ pdf_load_substitute_cjk_font(pdf_font_desc *fontdesc, int ros, int serif)
}
static fz_error
-pdf_load_system_font(pdf_font_desc *fontdesc, char *fontname, char *collection)
+pdf_load_system_font(fz_context *ctx, pdf_font_desc *fontdesc, char *fontname, char *collection)
{
fz_error error;
int bold = 0;
@@ -259,17 +259,17 @@ pdf_load_system_font(pdf_font_desc *fontdesc, char *fontname, char *collection)
if (collection)
{
if (!strcmp(collection, "Adobe-CNS1"))
- return pdf_load_substitute_cjk_font(fontdesc, PDF_ROS_CNS, serif);
+ return pdf_load_substitute_cjk_font(ctx, fontdesc, PDF_ROS_CNS, serif);
else if (!strcmp(collection, "Adobe-GB1"))
- return pdf_load_substitute_cjk_font(fontdesc, PDF_ROS_GB, serif);
+ return pdf_load_substitute_cjk_font(ctx, fontdesc, PDF_ROS_GB, serif);
else if (!strcmp(collection, "Adobe-Japan1"))
- return pdf_load_substitute_cjk_font(fontdesc, PDF_ROS_JAPAN, serif);
+ return pdf_load_substitute_cjk_font(ctx, fontdesc, PDF_ROS_JAPAN, serif);
else if (!strcmp(collection, "Adobe-Korea1"))
- return pdf_load_substitute_cjk_font(fontdesc, PDF_ROS_KOREA, serif);
+ return pdf_load_substitute_cjk_font(ctx, fontdesc, PDF_ROS_KOREA, serif);
return fz_error_make("unknown cid collection: %s", collection);
}
- error = pdf_load_substitute_font(fontdesc, mono, serif, bold, italic);
+ error = pdf_load_substitute_font(ctx, fontdesc, mono, serif, bold, italic);
if (error)
return fz_error_note(error, "cannot load substitute font");
@@ -281,22 +281,23 @@ pdf_load_embedded_font(pdf_font_desc *fontdesc, pdf_xref *xref, fz_obj *stmref)
{
fz_error error;
fz_buffer *buf;
+ fz_context *ctx = xref->ctx;
error = pdf_load_stream(&buf, xref, fz_to_num(stmref), fz_to_gen(stmref));
if (error)
return fz_error_note(error, "cannot load font stream (%d %d R)", fz_to_num(stmref), fz_to_gen(stmref));
- error = fz_new_font_from_memory(&fontdesc->font, buf->data, buf->len, 0);
+ error = fz_new_font_from_memory(ctx, &fontdesc->font, buf->data, buf->len, 0);
if (error)
{
- fz_drop_buffer(buf);
+ fz_drop_buffer(ctx, buf);
return fz_error_note(error, "cannot load embedded font (%d %d R)", fz_to_num(stmref), fz_to_gen(stmref));
}
/* save the buffer so we can free it later */
fontdesc->font->ft_data = buf->data;
fontdesc->font->ft_size = buf->len;
- fz_free(buf); /* only free the fz_buffer struct, not the contained data */
+ fz_free(ctx, buf); /* only free the fz_buffer struct, not the contained data */
fontdesc->is_embedded = 1;
@@ -315,32 +316,32 @@ pdf_keep_font(pdf_font_desc *fontdesc)
}
void
-pdf_drop_font(pdf_font_desc *fontdesc)
+pdf_drop_font(fz_context *ctx, pdf_font_desc *fontdesc)
{
if (fontdesc && --fontdesc->refs == 0)
{
if (fontdesc->font)
- fz_drop_font(fontdesc->font);
+ fz_drop_font(ctx, fontdesc->font);
if (fontdesc->encoding)
- pdf_drop_cmap(fontdesc->encoding);
+ pdf_drop_cmap(ctx, fontdesc->encoding);
if (fontdesc->to_ttf_cmap)
- pdf_drop_cmap(fontdesc->to_ttf_cmap);
+ pdf_drop_cmap(ctx, fontdesc->to_ttf_cmap);
if (fontdesc->to_unicode)
- pdf_drop_cmap(fontdesc->to_unicode);
- fz_free(fontdesc->cid_to_gid);
- fz_free(fontdesc->cid_to_ucs);
- fz_free(fontdesc->hmtx);
- fz_free(fontdesc->vmtx);
- fz_free(fontdesc);
+ pdf_drop_cmap(ctx, fontdesc->to_unicode);
+ fz_free(ctx, fontdesc->cid_to_gid);
+ fz_free(ctx, fontdesc->cid_to_ucs);
+ fz_free(ctx, fontdesc->hmtx);
+ fz_free(ctx, fontdesc->vmtx);
+ fz_free(ctx, fontdesc);
}
}
pdf_font_desc *
-pdf_new_font_desc(void)
+pdf_new_font_desc(fz_context *ctx)
{
pdf_font_desc *fontdesc;
- fontdesc = fz_malloc(sizeof(pdf_font_desc));
+ fontdesc = fz_malloc(ctx, sizeof(pdf_font_desc));
fontdesc->refs = 1;
fontdesc->font = NULL;
@@ -410,27 +411,28 @@ pdf_load_simple_font(pdf_font_desc **fontdescp, pdf_xref *xref, fz_obj *dict)
char ebuffer[256][32];
int i, k, n;
int fterr;
+ fz_context *ctx = xref->ctx;
- basefont = fz_to_name(fz_dict_gets(dict, "BaseFont"));
+ basefont = fz_to_name(ctx, fz_dict_gets(ctx, dict, "BaseFont"));
fontname = clean_font_name(basefont);
/* Load font file */
- fontdesc = pdf_new_font_desc();
+ fontdesc = pdf_new_font_desc(ctx);
- descriptor = fz_dict_gets(dict, "FontDescriptor");
+ descriptor = fz_dict_gets(ctx, dict, "FontDescriptor");
if (descriptor)
error = pdf_load_font_descriptor(fontdesc, xref, descriptor, NULL, basefont);
else
- error = pdf_load_builtin_font(fontdesc, fontname);
+ error = pdf_load_builtin_font(ctx, fontdesc, fontname);
if (error)
goto cleanup;
/* Some chinese documents mistakenly consider WinAnsiEncoding to be codepage 936 */
if (!*fontdesc->font->name &&
- !fz_dict_gets(dict, "ToUnicode") &&
- !strcmp(fz_to_name(fz_dict_gets(dict, "Encoding")), "WinAnsiEncoding") &&
- fz_to_int(fz_dict_gets(descriptor, "Flags")) == 4)
+ !fz_dict_gets(ctx, dict, "ToUnicode") &&
+ !strcmp(fz_to_name(ctx, fz_dict_gets(ctx, dict, "Encoding")), "WinAnsiEncoding") &&
+ fz_to_int(ctx, fz_dict_gets(ctx, descriptor, "Flags")) == 4)
{
/* note: without the comma, pdf_load_font_descriptor would prefer /FontName over /BaseFont */
char *cp936fonts[] = {
@@ -447,12 +449,12 @@ pdf_load_simple_font(pdf_font_desc **fontdescp, pdf_xref *xref, fz_obj *dict)
if (cp936fonts[i])
{
fz_warn("workaround for S22PDF lying about chinese font encodings");
- pdf_drop_font(fontdesc);
- fontdesc = pdf_new_font_desc();
+ pdf_drop_font(ctx, fontdesc);
+ fontdesc = pdf_new_font_desc(ctx);
error = pdf_load_font_descriptor(fontdesc, xref, descriptor, "Adobe-GB1", cp936fonts[i+1]);
- error |= pdf_load_system_cmap(&fontdesc->encoding, "GBK-EUC-H");
- error |= pdf_load_system_cmap(&fontdesc->to_unicode, "Adobe-GB1-UCS2");
- error |= pdf_load_system_cmap(&fontdesc->to_ttf_cmap, "Adobe-GB1-UCS2");
+ error |= pdf_load_system_cmap(ctx, &fontdesc->encoding, "GBK-EUC-H");
+ error |= pdf_load_system_cmap(ctx, &fontdesc->to_unicode, "Adobe-GB1-UCS2");
+ error |= pdf_load_system_cmap(ctx, &fontdesc->to_ttf_cmap, "Adobe-GB1-UCS2");
if (error)
return fz_error_note(error, "cannot load font");
@@ -502,41 +504,41 @@ pdf_load_simple_font(pdf_font_desc **fontdescp, pdf_xref *xref, fz_obj *dict)
else
fz_warn("freetype could not find any cmaps");
- etable = fz_calloc(256, sizeof(unsigned short));
+ etable = fz_calloc(ctx, 256, sizeof(unsigned short));
for (i = 0; i < 256; i++)
{
estrings[i] = NULL;
etable[i] = 0;
}
- encoding = fz_dict_gets(dict, "Encoding");
+ encoding = fz_dict_gets(ctx, dict, "Encoding");
if (encoding)
{
- if (fz_is_name(encoding))
- pdf_load_encoding(estrings, fz_to_name(encoding));
+ if (fz_is_name(ctx, encoding))
+ pdf_load_encoding(estrings, fz_to_name(ctx, encoding));
- if (fz_is_dict(encoding))
+ if (fz_is_dict(ctx, encoding))
{
fz_obj *base, *diff, *item;
- base = fz_dict_gets(encoding, "BaseEncoding");
- if (fz_is_name(base))
- pdf_load_encoding(estrings, fz_to_name(base));
+ base = fz_dict_gets(ctx, encoding, "BaseEncoding");
+ if (fz_is_name(ctx, base))
+ pdf_load_encoding(estrings, fz_to_name(ctx, base));
else if (!fontdesc->is_embedded && !symbolic)
pdf_load_encoding(estrings, "StandardEncoding");
- diff = fz_dict_gets(encoding, "Differences");
- if (fz_is_array(diff))
+ diff = fz_dict_gets(ctx, encoding, "Differences");
+ if (fz_is_array(ctx, diff))
{
- n = fz_array_len(diff);
+ n = fz_array_len(ctx, diff);
k = 0;
for (i = 0; i < n; i++)
{
- item = fz_array_get(diff, i);
- if (fz_is_int(item))
- k = fz_to_int(item);
- if (fz_is_name(item))
- estrings[k++] = fz_to_name(item);
+ item = fz_array_get(ctx, diff, i);
+ if (fz_is_int(ctx, item))
+ k = fz_to_int(ctx, item);
+ if (fz_is_name(ctx, item))
+ estrings[k++] = fz_to_name(ctx, item);
if (k < 0) k = 0;
if (k > 255) k = 255;
}
@@ -642,11 +644,11 @@ pdf_load_simple_font(pdf_font_desc **fontdescp, pdf_xref *xref, fz_obj *dict)
}
}
- fontdesc->encoding = pdf_new_identity_cmap(0, 1);
+ fontdesc->encoding = pdf_new_identity_cmap(ctx, 0, 1);
fontdesc->cid_to_gid_len = 256;
fontdesc->cid_to_gid = etable;
- error = pdf_load_to_unicode(fontdesc, xref, estrings, NULL, fz_dict_gets(dict, "ToUnicode"));
+ error = pdf_load_to_unicode(fontdesc, xref, estrings, NULL, fz_dict_gets(ctx, dict, "ToUnicode"));
if (error)
fz_error_handle(error, "cannot load to_unicode");
@@ -656,21 +658,21 @@ skip_encoding:
pdf_set_default_hmtx(fontdesc, fontdesc->missing_width);
- widths = fz_dict_gets(dict, "Widths");
+ widths = fz_dict_gets(ctx, dict, "Widths");
if (widths)
{
int first, last;
- first = fz_to_int(fz_dict_gets(dict, "FirstChar"));
- last = fz_to_int(fz_dict_gets(dict, "LastChar"));
+ first = fz_to_int(ctx, fz_dict_gets(ctx, dict, "FirstChar"));
+ last = fz_to_int(ctx, fz_dict_gets(ctx, dict, "LastChar"));
if (first < 0 || last > 255 || first > last)
first = last = 0;
for (i = 0; i < last - first + 1; i++)
{
- int wid = fz_to_int(fz_array_get(widths, i));
- pdf_add_hmtx(fontdesc, i + first, i + first, wid);
+ int wid = fz_to_int(ctx, fz_array_get(ctx, widths, i));
+ pdf_add_hmtx(ctx, fontdesc, i + first, i + first, wid);
}
}
else
@@ -680,7 +682,7 @@ skip_encoding:
fz_warn("freetype set character size: %s", ft_error_string(fterr));
for (i = 0; i < 256; i++)
{
- pdf_add_hmtx(fontdesc, i, i, ft_width(fontdesc, i));
+ pdf_add_hmtx(ctx, fontdesc, i, i, ft_width(fontdesc, i));
}
}
@@ -691,8 +693,8 @@ skip_encoding:
cleanup:
if (etable != fontdesc->cid_to_gid)
- fz_free(etable);
- pdf_drop_font(fontdesc);
+ fz_free(ctx, etable);
+ pdf_drop_font(ctx, fontdesc);
return fz_error_note(error, "cannot load simple font (%d %d R)", fz_to_num(dict), fz_to_gen(dict));
}
@@ -714,40 +716,41 @@ load_cid_font(pdf_font_desc **fontdescp, pdf_xref *xref, fz_obj *dict, fz_obj *e
int i, k, fterr;
fz_obj *obj;
int dw;
+ fz_context *ctx = xref->ctx;
/* Get font name and CID collection */
- basefont = fz_to_name(fz_dict_gets(dict, "BaseFont"));
+ basefont = fz_to_name(ctx, fz_dict_gets(ctx, dict, "BaseFont"));
{
fz_obj *cidinfo;
char tmpstr[64];
int tmplen;
- cidinfo = fz_dict_gets(dict, "CIDSystemInfo");
+ cidinfo = fz_dict_gets(ctx, dict, "CIDSystemInfo");
if (!cidinfo)
return fz_error_make("cid font is missing info");
- obj = fz_dict_gets(cidinfo, "Registry");
- tmplen = MIN(sizeof tmpstr - 1, fz_to_str_len(obj));
- memcpy(tmpstr, fz_to_str_buf(obj), tmplen);
+ obj = fz_dict_gets(ctx, cidinfo, "Registry");
+ tmplen = MIN(sizeof tmpstr - 1, fz_to_str_len(ctx, obj));
+ memcpy(tmpstr, fz_to_str_buf(ctx, obj), tmplen);
tmpstr[tmplen] = '\0';
fz_strlcpy(collection, tmpstr, sizeof collection);
fz_strlcat(collection, "-", sizeof collection);
- obj = fz_dict_gets(cidinfo, "Ordering");
- tmplen = MIN(sizeof tmpstr - 1, fz_to_str_len(obj));
- memcpy(tmpstr, fz_to_str_buf(obj), tmplen);
+ obj = fz_dict_gets(ctx, cidinfo, "Ordering");
+ tmplen = MIN(sizeof tmpstr - 1, fz_to_str_len(ctx, obj));
+ memcpy(tmpstr, fz_to_str_buf(ctx, obj), tmplen);
tmpstr[tmplen] = '\0';
fz_strlcat(collection, tmpstr, sizeof collection);
}
/* Load font file */
- fontdesc = pdf_new_font_desc();
+ fontdesc = pdf_new_font_desc(ctx);
- descriptor = fz_dict_gets(dict, "FontDescriptor");
+ descriptor = fz_dict_gets(ctx, dict, "FontDescriptor");
if (descriptor)
error = pdf_load_font_descriptor(fontdesc, xref, descriptor, collection, basefont);
else
@@ -761,14 +764,14 @@ load_cid_font(pdf_font_desc **fontdescp, pdf_xref *xref, fz_obj *dict, fz_obj *e
/* Encoding */
error = fz_okay;
- if (fz_is_name(encoding))
+ if (fz_is_name(ctx, encoding))
{
- if (!strcmp(fz_to_name(encoding), "Identity-H"))
- fontdesc->encoding = pdf_new_identity_cmap(0, 2);
- else if (!strcmp(fz_to_name(encoding), "Identity-V"))
- fontdesc->encoding = pdf_new_identity_cmap(1, 2);
+ if (!strcmp(fz_to_name(ctx, encoding), "Identity-H"))
+ fontdesc->encoding = pdf_new_identity_cmap(ctx, 0, 2);
+ else if (!strcmp(fz_to_name(ctx, encoding), "Identity-V"))
+ fontdesc->encoding = pdf_new_identity_cmap(ctx, 1, 2);
else
- error = pdf_load_system_cmap(&fontdesc->encoding, fz_to_name(encoding));
+ error = pdf_load_system_cmap(ctx, &fontdesc->encoding, fz_to_name(ctx, encoding));
}
else if (fz_is_indirect(encoding))
{
@@ -787,7 +790,7 @@ load_cid_font(pdf_font_desc **fontdescp, pdf_xref *xref, fz_obj *dict, fz_obj *e
{
fz_obj *cidtogidmap;
- cidtogidmap = fz_dict_gets(dict, "CIDToGIDMap");
+ cidtogidmap = fz_dict_gets(ctx, dict, "CIDToGIDMap");
if (fz_is_indirect(cidtogidmap))
{
fz_buffer *buf;
@@ -797,11 +800,11 @@ load_cid_font(pdf_font_desc **fontdescp, pdf_xref *xref, fz_obj *dict, fz_obj *e
goto cleanup;
fontdesc->cid_to_gid_len = (buf->len) / 2;
- fontdesc->cid_to_gid = fz_calloc(fontdesc->cid_to_gid_len, sizeof(unsigned short));
+ fontdesc->cid_to_gid = fz_calloc(ctx, fontdesc->cid_to_gid_len, sizeof(unsigned short));
for (i = 0; i < fontdesc->cid_to_gid_len; i++)
fontdesc->cid_to_gid[i] = (buf->data[i * 2] << 8) + buf->data[i * 2 + 1];
- fz_drop_buffer(buf);
+ fz_drop_buffer(ctx, buf);
}
/* if truetype font is external, cidtogidmap should not be identity */
@@ -817,15 +820,15 @@ load_cid_font(pdf_font_desc **fontdescp, pdf_xref *xref, fz_obj *dict, fz_obj *e
}
if (!strcmp(collection, "Adobe-CNS1"))
- error = pdf_load_system_cmap(&fontdesc->to_ttf_cmap, "Adobe-CNS1-UCS2");
+ error = pdf_load_system_cmap(ctx, &fontdesc->to_ttf_cmap, "Adobe-CNS1-UCS2");
else if (!strcmp(collection, "Adobe-GB1"))
- error = pdf_load_system_cmap(&fontdesc->to_ttf_cmap, "Adobe-GB1-UCS2");
+ error = pdf_load_system_cmap(ctx, &fontdesc->to_ttf_cmap, "Adobe-GB1-UCS2");
else if (!strcmp(collection, "Adobe-Japan1"))
- error = pdf_load_system_cmap(&fontdesc->to_ttf_cmap, "Adobe-Japan1-UCS2");
+ error = pdf_load_system_cmap(ctx, &fontdesc->to_ttf_cmap, "Adobe-Japan1-UCS2");
else if (!strcmp(collection, "Adobe-Japan2"))
- error = pdf_load_system_cmap(&fontdesc->to_ttf_cmap, "Adobe-Japan2-UCS2");
+ error = pdf_load_system_cmap(ctx, &fontdesc->to_ttf_cmap, "Adobe-Japan2-UCS2");
else if (!strcmp(collection, "Adobe-Korea1"))
- error = pdf_load_system_cmap(&fontdesc->to_ttf_cmap, "Adobe-Korea1-UCS2");
+ error = pdf_load_system_cmap(ctx, &fontdesc->to_ttf_cmap, "Adobe-Korea1-UCS2");
else
error = fz_okay;
@@ -844,34 +847,36 @@ load_cid_font(pdf_font_desc **fontdescp, pdf_xref *xref, fz_obj *dict, fz_obj *e
/* Horizontal */
dw = 1000;
- obj = fz_dict_gets(dict, "DW");
+ obj = fz_dict_gets(ctx, dict, "DW");
if (obj)
- dw = fz_to_int(obj);
+ dw = fz_to_int(ctx, obj);
pdf_set_default_hmtx(fontdesc, dw);
- widths = fz_dict_gets(dict, "W");
+ widths = fz_dict_gets(ctx, dict, "W");
if (widths)
{
- int c0, c1, w;
+ int c0, c1, w, n, m;
- for (i = 0; i < fz_array_len(widths); )
+ n = fz_array_len(ctx, widths);
+ for (i = 0; i < n; )
{
- c0 = fz_to_int(fz_array_get(widths, i));
- obj = fz_array_get(widths, i + 1);
- if (fz_is_array(obj))
+ c0 = fz_to_int(ctx, fz_array_get(ctx, widths, i));
+ obj = fz_array_get(ctx, widths, i + 1);
+ if (fz_is_array(ctx, obj))
{
- for (k = 0; k < fz_array_len(obj); k++)
+ m = fz_array_len(ctx, obj);
+ for (k = 0; k < m; k++)
{
- w = fz_to_int(fz_array_get(obj, k));
- pdf_add_hmtx(fontdesc, c0 + k, c0 + k, w);
+ w = fz_to_int(ctx, fz_array_get(ctx, obj, k));
+ pdf_add_hmtx(ctx, fontdesc, c0 + k, c0 + k, w);
}
i += 2;
}
else
{
- c1 = fz_to_int(obj);
- w = fz_to_int(fz_array_get(widths, i + 2));
- pdf_add_hmtx(fontdesc, c0, c1, w);
+ c1 = fz_to_int(ctx, obj);
+ w = fz_to_int(ctx, fz_array_get(ctx, widths, i + 2));
+ pdf_add_hmtx(ctx, fontdesc, c0, c1, w);
i += 3;
}
}
@@ -886,42 +891,44 @@ load_cid_font(pdf_font_desc **fontdescp, pdf_xref *xref, fz_obj *dict, fz_obj *e
int dw2y = 880;
int dw2w = -1000;
- obj = fz_dict_gets(dict, "DW2");
+ obj = fz_dict_gets(ctx, dict, "DW2");
if (obj)
{
- dw2y = fz_to_int(fz_array_get(obj, 0));
- dw2w = fz_to_int(fz_array_get(obj, 1));
+ dw2y = fz_to_int(ctx, fz_array_get(ctx, obj, 0));
+ dw2w = fz_to_int(ctx, fz_array_get(ctx, obj, 1));
}
pdf_set_default_vmtx(fontdesc, dw2y, dw2w);
- widths = fz_dict_gets(dict, "W2");
+ widths = fz_dict_gets(ctx, dict, "W2");
if (widths)
{
- int c0, c1, w, x, y;
+ int c0, c1, w, x, y, n;
- for (i = 0; i < fz_array_len(widths); )
+ n = fz_array_len(ctx, widths);
+ for (i = 0; i < n; )
{
- c0 = fz_to_int(fz_array_get(widths, i));
- obj = fz_array_get(widths, i + 1);
- if (fz_is_array(obj))
+ c0 = fz_to_int(ctx, fz_array_get(ctx, widths, i));
+ obj = fz_array_get(ctx, widths, i + 1);
+ if (fz_is_array(ctx, obj))
{
- for (k = 0; k * 3 < fz_array_len(obj); k ++)
+ int m = fz_array_len(ctx, obj);
+ for (k = 0; k * 3 < m; k ++)
{
- w = fz_to_int(fz_array_get(obj, k * 3 + 0));
- x = fz_to_int(fz_array_get(obj, k * 3 + 1));
- y = fz_to_int(fz_array_get(obj, k * 3 + 2));
- pdf_add_vmtx(fontdesc, c0 + k, c0 + k, x, y, w);
+ w = fz_to_int(ctx, fz_array_get(ctx, obj, k * 3 + 0));
+ x = fz_to_int(ctx, fz_array_get(ctx, obj, k * 3 + 1));
+ y = fz_to_int(ctx, fz_array_get(ctx, obj, k * 3 + 2));
+ pdf_add_vmtx(ctx, fontdesc, c0 + k, c0 + k, x, y, w);
}
i += 2;
}
else
{
- c1 = fz_to_int(obj);
- w = fz_to_int(fz_array_get(widths, i + 2));
- x = fz_to_int(fz_array_get(widths, i + 3));
- y = fz_to_int(fz_array_get(widths, i + 4));
- pdf_add_vmtx(fontdesc, c0, c1, x, y, w);
+ c1 = fz_to_int(ctx, obj);
+ w = fz_to_int(ctx, fz_array_get(ctx, widths, i + 2));
+ x = fz_to_int(ctx, fz_array_get(ctx, widths, i + 3));
+ y = fz_to_int(ctx, fz_array_get(ctx, widths, i + 4));
+ pdf_add_vmtx(ctx, fontdesc, c0, c1, x, y, w);
i += 5;
}
}
@@ -934,7 +941,7 @@ load_cid_font(pdf_font_desc **fontdescp, pdf_xref *xref, fz_obj *dict, fz_obj *e
return fz_okay;
cleanup:
- pdf_drop_font(fontdesc);
+ pdf_drop_font(ctx, fontdesc);
return fz_error_note(error, "cannot load cid font (%d %d R)", fz_to_num(dict), fz_to_gen(dict));
}
@@ -947,20 +954,21 @@ pdf_load_type0_font(pdf_font_desc **fontdescp, pdf_xref *xref, fz_obj *dict)
fz_obj *subtype;
fz_obj *encoding;
fz_obj *to_unicode;
+ fz_context *ctx = xref->ctx;
- dfonts = fz_dict_gets(dict, "DescendantFonts");
+ dfonts = fz_dict_gets(ctx, dict, "DescendantFonts");
if (!dfonts)
return fz_error_make("cid font is missing descendant fonts");
- dfont = fz_array_get(dfonts, 0);
+ dfont = fz_array_get(ctx, dfonts, 0);
- subtype = fz_dict_gets(dfont, "Subtype");
- encoding = fz_dict_gets(dict, "Encoding");
- to_unicode = fz_dict_gets(dict, "ToUnicode");
+ subtype = fz_dict_gets(ctx, dfont, "Subtype");
+ encoding = fz_dict_gets(ctx, dict, "Encoding");
+ to_unicode = fz_dict_gets(ctx, dict, "ToUnicode");
- if (fz_is_name(subtype) && !strcmp(fz_to_name(subtype), "CIDFontType0"))
+ if (fz_is_name(ctx, subtype) && !strcmp(fz_to_name(ctx, subtype), "CIDFontType0"))
error = load_cid_font(fontdescp, xref, dfont, encoding, to_unicode);
- else if (fz_is_name(subtype) && !strcmp(fz_to_name(subtype), "CIDFontType2"))
+ else if (fz_is_name(ctx, subtype) && !strcmp(fz_to_name(ctx, subtype), "CIDFontType2"))
error = load_cid_font(fontdescp, xref, dfont, encoding, to_unicode);
else
error = fz_error_make("syntaxerror: unknown cid font type");
@@ -982,24 +990,25 @@ pdf_load_font_descriptor(pdf_font_desc *fontdesc, pdf_xref *xref, fz_obj *dict,
char *fontname;
char *origname;
FT_Face face;
+ fz_context *ctx = xref->ctx;
if (!strchr(basefont, ',') || strchr(basefont, '+'))
- origname = fz_to_name(fz_dict_gets(dict, "FontName"));
+ origname = fz_to_name(ctx, fz_dict_gets(ctx, dict, "FontName"));
else
origname = basefont;
fontname = clean_font_name(origname);
- fontdesc->flags = fz_to_int(fz_dict_gets(dict, "Flags"));
- fontdesc->italic_angle = fz_to_real(fz_dict_gets(dict, "ItalicAngle"));
- fontdesc->ascent = fz_to_real(fz_dict_gets(dict, "Ascent"));
- fontdesc->descent = fz_to_real(fz_dict_gets(dict, "Descent"));
- fontdesc->cap_height = fz_to_real(fz_dict_gets(dict, "CapHeight"));
- fontdesc->x_height = fz_to_real(fz_dict_gets(dict, "XHeight"));
- fontdesc->missing_width = fz_to_real(fz_dict_gets(dict, "MissingWidth"));
-
- obj1 = fz_dict_gets(dict, "FontFile");
- obj2 = fz_dict_gets(dict, "FontFile2");
- obj3 = fz_dict_gets(dict, "FontFile3");
+ fontdesc->flags = fz_to_int(ctx, fz_dict_gets(ctx, dict, "Flags"));
+ fontdesc->italic_angle = fz_to_real(ctx, fz_dict_gets(ctx, dict, "ItalicAngle"));
+ fontdesc->ascent = fz_to_real(ctx, fz_dict_gets(ctx, dict, "Ascent"));
+ fontdesc->descent = fz_to_real(ctx, fz_dict_gets(ctx, dict, "Descent"));
+ fontdesc->cap_height = fz_to_real(ctx, fz_dict_gets(ctx, dict, "CapHeight"));
+ fontdesc->x_height = fz_to_real(ctx, fz_dict_gets(ctx, dict, "XHeight"));
+ fontdesc->missing_width = fz_to_real(ctx, fz_dict_gets(ctx, dict, "MissingWidth"));
+
+ obj1 = fz_dict_gets(ctx, dict, "FontFile");
+ obj2 = fz_dict_gets(ctx, dict, "FontFile2");
+ obj3 = fz_dict_gets(ctx, dict, "FontFile3");
obj = obj1 ? obj1 : obj2 ? obj2 : obj3;
if (fz_is_indirect(obj))
@@ -1009,9 +1018,9 @@ pdf_load_font_descriptor(pdf_font_desc *fontdesc, pdf_xref *xref, fz_obj *dict,
{
fz_error_handle(error, "ignored error when loading embedded font, attempting to load system font");
if (origname != fontname)
- error = pdf_load_builtin_font(fontdesc, fontname);
+ error = pdf_load_builtin_font(ctx, fontdesc, fontname);
else
- error = pdf_load_system_font(fontdesc, fontname, collection);
+ error = pdf_load_system_font(ctx, fontdesc, fontname, collection);
if (error)
return fz_error_note(error, "cannot load font descriptor (%d %d R)", fz_to_num(dict), fz_to_gen(dict));
}
@@ -1019,9 +1028,9 @@ pdf_load_font_descriptor(pdf_font_desc *fontdesc, pdf_xref *xref, fz_obj *dict,
else
{
if (origname != fontname)
- error = pdf_load_builtin_font(fontdesc, fontname);
+ error = pdf_load_builtin_font(ctx, fontdesc, fontname);
else
- error = pdf_load_system_font(fontdesc, fontname, collection);
+ error = pdf_load_system_font(ctx, fontdesc, fontname, collection);
if (error)
return fz_error_note(error, "cannot load font descriptor (%d %d R)", fz_to_num(dict), fz_to_gen(dict));
}
@@ -1041,7 +1050,7 @@ pdf_load_font_descriptor(pdf_font_desc *fontdesc, pdf_xref *xref, fz_obj *dict,
}
static void
-pdf_make_width_table(pdf_font_desc *fontdesc)
+pdf_make_width_table(fz_context *ctx, pdf_font_desc *fontdesc)
{
fz_font *font = fontdesc->font;
int i, k, cid, gid;
@@ -1059,8 +1068,7 @@ pdf_make_width_table(pdf_font_desc *fontdesc)
}
font->width_count ++;
- font->width_table = fz_calloc(font->width_count, sizeof(int));
- memset(font->width_table, 0, sizeof(int) * font->width_count);
+ font->width_table = fz_calloc(ctx, font->width_count, sizeof(int));
for (i = 0; i < fontdesc->hmtx_len; i++)
{
@@ -1081,16 +1089,17 @@ pdf_load_font(pdf_font_desc **fontdescp, pdf_xref *xref, fz_obj *rdb, fz_obj *di
char *subtype;
fz_obj *dfonts;
fz_obj *charprocs;
+ fz_context *ctx = xref->ctx;
- if ((*fontdescp = pdf_find_item(xref->store, pdf_drop_font, dict)))
+ if ((*fontdescp = pdf_find_item(ctx, xref->store, (pdf_store_drop_fn *)pdf_drop_font, dict)))
{
pdf_keep_font(*fontdescp);
return fz_okay;
}
- subtype = fz_to_name(fz_dict_gets(dict, "Subtype"));
- dfonts = fz_dict_gets(dict, "DescendantFonts");
- charprocs = fz_dict_gets(dict, "CharProcs");
+ subtype = fz_to_name(ctx, fz_dict_gets(ctx, dict, "Subtype"));
+ dfonts = fz_dict_gets(ctx, dict, "DescendantFonts");
+ charprocs = fz_dict_gets(ctx, dict, "CharProcs");
if (subtype && !strcmp(subtype, "Type0"))
error = pdf_load_type0_font(fontdescp, xref, dict);
@@ -1122,9 +1131,9 @@ pdf_load_font(pdf_font_desc **fontdescp, pdf_xref *xref, fz_obj *rdb, fz_obj *di
/* Save the widths to stretch non-CJK substitute fonts */
if ((*fontdescp)->font->ft_substitute && !(*fontdescp)->to_ttf_cmap)
- pdf_make_width_table(*fontdescp);
+ pdf_make_width_table(ctx, *fontdescp);
- pdf_store_item(xref->store, pdf_keep_font, pdf_drop_font, dict, *fontdescp);
+ pdf_store_item(ctx, xref->store, (pdf_store_keep_fn *)pdf_keep_font, (pdf_store_drop_fn *)pdf_drop_font, dict, *fontdescp);
return fz_okay;
}
diff --git a/pdf/pdf_function.c b/pdf/pdf_function.c
index 7eeb7863..3905f75e 100644
--- a/pdf/pdf_function.c
+++ b/pdf/pdf_function.c
@@ -672,12 +672,12 @@ ps_run(psobj *code, ps_stack *st, int pc)
}
static void
-resize_code(pdf_function *func, int newsize)
+resize_code(fz_context *ctx, pdf_function *func, int newsize)
{
if (newsize >= func->u.p.cap)
{
func->u.p.cap = func->u.p.cap + 64;
- func->u.p.code = fz_realloc(func->u.p.code, func->u.p.cap, sizeof(psobj));
+ func->u.p.code = fz_realloc(ctx, func->u.p.code, func->u.p.cap * sizeof(psobj));
}
}
@@ -705,14 +705,14 @@ parse_code(pdf_function *func, fz_stream *stream, int *codeptr)
return fz_error_make("truncated calculator function");
case PDF_TOK_INT:
- resize_code(func, *codeptr);
+ resize_code(stream->ctx, func, *codeptr);
func->u.p.code[*codeptr].type = PS_INT;
func->u.p.code[*codeptr].u.i = atoi(buf);
++*codeptr;
break;
case PDF_TOK_REAL:
- resize_code(func, *codeptr);
+ resize_code(stream->ctx, func, *codeptr);
func->u.p.code[*codeptr].type = PS_REAL;
func->u.p.code[*codeptr].u.f = fz_atof(buf);
++*codeptr;
@@ -722,7 +722,7 @@ parse_code(pdf_function *func, fz_stream *stream, int *codeptr)
opptr = *codeptr;
*codeptr += 4;
- resize_code(func, *codeptr);
+ resize_code(stream->ctx, func, *codeptr);
ifptr = *codeptr;
error = parse_code(func, stream, codeptr);
@@ -787,7 +787,7 @@ parse_code(pdf_function *func, fz_stream *stream, int *codeptr)
break;
case PDF_TOK_CLOSE_BRACE:
- resize_code(func, *codeptr);
+ resize_code(stream->ctx, func, *codeptr);
func->u.p.code[*codeptr].type = PS_OPERATOR;
func->u.p.code[*codeptr].u.op = PS_OP_RETURN;
++*codeptr;
@@ -811,7 +811,7 @@ parse_code(pdf_function *func, fz_stream *stream, int *codeptr)
if (cmp != 0)
return fz_error_make("unknown operator: '%s'", buf);
- resize_code(func, *codeptr);
+ resize_code(stream->ctx, func, *codeptr);
func->u.p.code[*codeptr].type = PS_OPERATOR;
func->u.p.code[*codeptr].u.op = a;
++*codeptr;
@@ -902,29 +902,30 @@ load_sample_func(pdf_function *func, pdf_xref *xref, fz_obj *dict, int num, int
int samplecount;
int bps;
int i;
+ fz_context *ctx = xref->ctx;
func->u.sa.samples = NULL;
- obj = fz_dict_gets(dict, "Size");
- if (!fz_is_array(obj) || fz_array_len(obj) != func->m)
+ obj = fz_dict_gets(ctx, dict, "Size");
+ if (!fz_is_array(ctx, obj) || fz_array_len(ctx, obj) != func->m)
return fz_error_make("malformed /Size");
for (i = 0; i < func->m; i++)
- func->u.sa.size[i] = fz_to_int(fz_array_get(obj, i));
+ func->u.sa.size[i] = fz_to_int(ctx, fz_array_get(ctx, obj, i));
- obj = fz_dict_gets(dict, "BitsPerSample");
- if (!fz_is_int(obj))
+ obj = fz_dict_gets(ctx, dict, "BitsPerSample");
+ if (!fz_is_int(ctx, obj))
return fz_error_make("malformed /BitsPerSample");
- func->u.sa.bps = bps = fz_to_int(obj);
+ func->u.sa.bps = bps = fz_to_int(ctx, obj);
- obj = fz_dict_gets(dict, "Encode");
- if (fz_is_array(obj))
+ obj = fz_dict_gets(ctx, dict, "Encode");
+ if (fz_is_array(ctx, obj))
{
- if (fz_array_len(obj) != func->m * 2)
+ if (fz_array_len(ctx, obj) != func->m * 2)
return fz_error_make("malformed /Encode");
for (i = 0; i < func->m; i++)
{
- func->u.sa.encode[i][0] = fz_to_real(fz_array_get(obj, i*2+0));
- func->u.sa.encode[i][1] = fz_to_real(fz_array_get(obj, i*2+1));
+ func->u.sa.encode[i][0] = fz_to_real(ctx, fz_array_get(ctx, obj, i*2+0));
+ func->u.sa.encode[i][1] = fz_to_real(ctx, fz_array_get(ctx, obj, i*2+1));
}
}
else
@@ -936,15 +937,15 @@ load_sample_func(pdf_function *func, pdf_xref *xref, fz_obj *dict, int num, int
}
}
- obj = fz_dict_gets(dict, "Decode");
- if (fz_is_array(obj))
+ obj = fz_dict_gets(ctx, dict, "Decode");
+ if (fz_is_array(ctx, obj))
{
- if (fz_array_len(obj) != func->n * 2)
+ if (fz_array_len(ctx, obj) != func->n * 2)
return fz_error_make("malformed /Decode");
for (i = 0; i < func->n; i++)
{
- func->u.sa.decode[i][0] = fz_to_real(fz_array_get(obj, i*2+0));
- func->u.sa.decode[i][1] = fz_to_real(fz_array_get(obj, i*2+1));
+ func->u.sa.decode[i][0] = fz_to_real(ctx, fz_array_get(ctx, obj, i*2+0));
+ func->u.sa.decode[i][1] = fz_to_real(ctx, fz_array_get(ctx, obj, i*2+1));
}
}
else
@@ -959,7 +960,7 @@ load_sample_func(pdf_function *func, pdf_xref *xref, fz_obj *dict, int num, int
for (i = 0, samplecount = func->n; i < func->m; i++)
samplecount *= func->u.sa.size[i];
- func->u.sa.samples = fz_calloc(samplecount, sizeof(float));
+ func->u.sa.samples = fz_calloc(ctx, samplecount, sizeof(float));
error = pdf_open_stream(&stream, xref, num, gen);
if (error)
@@ -1107,7 +1108,7 @@ eval_sample_func(pdf_function *func, float *in, float *out)
*/
static fz_error
-load_exponential_func(pdf_function *func, fz_obj *dict)
+load_exponential_func(fz_context *ctx, pdf_function *func, fz_obj *dict)
{
fz_obj *obj;
int i;
@@ -1115,19 +1116,19 @@ load_exponential_func(pdf_function *func, fz_obj *dict)
if (func->m != 1)
return fz_error_make("/Domain must be one dimension (%d)", func->m);
- obj = fz_dict_gets(dict, "N");
- if (!fz_is_int(obj) && !fz_is_real(obj))
+ obj = fz_dict_gets(ctx, dict, "N");
+ if (!fz_is_int(ctx, obj) && !fz_is_real(ctx, obj))
return fz_error_make("malformed /N");
- func->u.e.n = fz_to_real(obj);
+ func->u.e.n = fz_to_real(ctx, obj);
- obj = fz_dict_gets(dict, "C0");
- if (fz_is_array(obj))
+ obj = fz_dict_gets(ctx, dict, "C0");
+ if (fz_is_array(ctx, obj))
{
- func->n = fz_array_len(obj);
+ func->n = fz_array_len(ctx, obj);
if (func->n >= MAXN)
return fz_error_make("exponential function result array out of range");
for (i = 0; i < func->n; i++)
- func->u.e.c0[i] = fz_to_real(fz_array_get(obj, i));
+ func->u.e.c0[i] = fz_to_real(ctx, fz_array_get(ctx, obj, i));
}
else
{
@@ -1135,13 +1136,13 @@ load_exponential_func(pdf_function *func, fz_obj *dict)
func->u.e.c0[0] = 0;
}
- obj = fz_dict_gets(dict, "C1");
- if (fz_is_array(obj))
+ obj = fz_dict_gets(ctx, dict, "C1");
+ if (fz_is_array(ctx, obj))
{
- if (fz_array_len(obj) != func->n)
+ if (fz_array_len(ctx, obj) != func->n)
return fz_error_make("/C1 must match /C0 length");
for (i = 0; i < func->n; i++)
- func->u.e.c1[i] = fz_to_real(fz_array_get(obj, i));
+ func->u.e.c1[i] = fz_to_real(ctx, fz_array_get(ctx, obj, i));
}
else
{
@@ -1192,26 +1193,27 @@ load_stitching_func(pdf_function *func, pdf_xref *xref, fz_obj *dict)
fz_obj *num;
int k;
int i;
+ fz_context *ctx = xref->ctx;
func->u.st.k = 0;
if (func->m != 1)
return fz_error_make("/Domain must be one dimension (%d)", func->m);
- obj = fz_dict_gets(dict, "Functions");
- if (!fz_is_array(obj))
+ obj = fz_dict_gets(ctx, dict, "Functions");
+ if (!fz_is_array(ctx, obj))
return fz_error_make("stitching function has no input functions");
{
- k = fz_array_len(obj);
+ k = fz_array_len(ctx, obj);
- func->u.st.funcs = fz_calloc(k, sizeof(pdf_function*));
- func->u.st.bounds = fz_calloc(k - 1, sizeof(float));
- func->u.st.encode = fz_calloc(k * 2, sizeof(float));
+ func->u.st.funcs = fz_calloc(ctx, k, sizeof(pdf_function*));
+ func->u.st.bounds = fz_calloc(ctx, k - 1, sizeof(float));
+ func->u.st.encode = fz_calloc(ctx, k * 2, sizeof(float));
funcs = func->u.st.funcs;
for (i = 0; i < k; i++)
{
- sub = fz_array_get(obj, i);
+ sub = fz_array_get(ctx, obj, i);
error = pdf_load_function(&funcs[i], xref, sub);
if (error)
return fz_error_note(error, "cannot load sub function %d (%d %d R)", i, fz_to_num(sub), fz_to_gen(sub));
@@ -1226,19 +1228,19 @@ load_stitching_func(pdf_function *func, pdf_xref *xref, fz_obj *dict)
return fz_error_make("sub function /Domain or /Range mismatch");
}
- obj = fz_dict_gets(dict, "Bounds");
- if (!fz_is_array(obj))
+ obj = fz_dict_gets(ctx, dict, "Bounds");
+ if (!fz_is_array(ctx, obj))
return fz_error_make("stitching function has no bounds");
{
- if (!fz_is_array(obj) || fz_array_len(obj) != k - 1)
+ if (!fz_is_array(ctx, obj) || fz_array_len(ctx, obj) != k - 1)
return fz_error_make("malformed /Bounds (not array or wrong length)");
for (i = 0; i < k-1; i++)
{
- num = fz_array_get(obj, i);
- if (!fz_is_int(num) && !fz_is_real(num))
+ num = fz_array_get(ctx, obj, i);
+ if (!fz_is_int(ctx, num) && !fz_is_real(ctx, num))
return fz_error_make("malformed /Bounds (item not real)");
- func->u.st.bounds[i] = fz_to_real(num);
+ func->u.st.bounds[i] = fz_to_real(ctx, num);
if (i && func->u.st.bounds[i-1] > func->u.st.bounds[i])
return fz_error_make("malformed /Bounds (item not monotonic)");
}
@@ -1248,16 +1250,16 @@ load_stitching_func(pdf_function *func, pdf_xref *xref, fz_obj *dict)
fz_warn("malformed shading function bounds (domain mismatch), proceeding anyway.");
}
- obj = fz_dict_gets(dict, "Encode");
- if (!fz_is_array(obj))
+ obj = fz_dict_gets(ctx, dict, "Encode");
+ if (!fz_is_array(ctx, obj))
return fz_error_make("stitching function is missing encoding");
{
- if (!fz_is_array(obj) || fz_array_len(obj) != k * 2)
+ if (!fz_is_array(ctx, obj) || fz_array_len(ctx, obj) != k * 2)
return fz_error_make("malformed /Encode");
for (i = 0; i < k; i++)
{
- func->u.st.encode[i*2+0] = fz_to_real(fz_array_get(obj, i*2+0));
- func->u.st.encode[i*2+1] = fz_to_real(fz_array_get(obj, i*2+1));
+ func->u.st.encode[i*2+0] = fz_to_real(ctx, fz_array_get(ctx, obj, i*2+0));
+ func->u.st.encode[i*2+1] = fz_to_real(ctx, fz_array_get(ctx, obj, i*2+1));
}
}
@@ -1318,7 +1320,7 @@ pdf_keep_function(pdf_function *func)
}
void
-pdf_drop_function(pdf_function *func)
+pdf_drop_function(fz_context *ctx, pdf_function *func)
{
int i;
if (--func->refs == 0)
@@ -1326,22 +1328,22 @@ pdf_drop_function(pdf_function *func)
switch(func->type)
{
case SAMPLE:
- fz_free(func->u.sa.samples);
+ fz_free(ctx, func->u.sa.samples);
break;
case EXPONENTIAL:
break;
case STITCHING:
for (i = 0; i < func->u.st.k; i++)
- pdf_drop_function(func->u.st.funcs[i]);
- fz_free(func->u.st.funcs);
- fz_free(func->u.st.bounds);
- fz_free(func->u.st.encode);
+ pdf_drop_function(ctx, func->u.st.funcs[i]);
+ fz_free(ctx, func->u.st.funcs);
+ fz_free(ctx, func->u.st.bounds);
+ fz_free(ctx, func->u.st.encode);
break;
case POSTSCRIPT:
- fz_free(func->u.p.code);
+ fz_free(ctx, func->u.p.code);
break;
}
- fz_free(func);
+ fz_free(ctx, func);
}
}
@@ -1352,39 +1354,39 @@ pdf_load_function(pdf_function **funcp, pdf_xref *xref, fz_obj *dict)
pdf_function *func;
fz_obj *obj;
int i;
+ fz_context *ctx = xref->ctx;
- if ((*funcp = pdf_find_item(xref->store, pdf_drop_function, dict)))
+ if ((*funcp = pdf_find_item(ctx, xref->store, (pdf_store_drop_fn *)pdf_drop_function, dict)))
{
pdf_keep_function(*funcp);
return fz_okay;
}
- func = fz_malloc(sizeof(pdf_function));
- memset(func, 0, sizeof(pdf_function));
+ func = fz_calloc(ctx, 1, sizeof(pdf_function));
func->refs = 1;
- obj = fz_dict_gets(dict, "FunctionType");
- func->type = fz_to_int(obj);
+ obj = fz_dict_gets(ctx, dict, "FunctionType");
+ func->type = fz_to_int(ctx, obj);
/* required for all */
- obj = fz_dict_gets(dict, "Domain");
- func->m = fz_array_len(obj) / 2;
+ obj = fz_dict_gets(ctx, dict, "Domain");
+ func->m = fz_array_len(ctx, obj) / 2;
for (i = 0; i < func->m; i++)
{
- func->domain[i][0] = fz_to_real(fz_array_get(obj, i * 2 + 0));
- func->domain[i][1] = fz_to_real(fz_array_get(obj, i * 2 + 1));
+ func->domain[i][0] = fz_to_real(ctx, fz_array_get(ctx, obj, i * 2 + 0));
+ func->domain[i][1] = fz_to_real(ctx, fz_array_get(ctx, obj, i * 2 + 1));
}
/* required for type0 and type4, optional otherwise */
- obj = fz_dict_gets(dict, "Range");
- if (fz_is_array(obj))
+ obj = fz_dict_gets(ctx, dict, "Range");
+ if (fz_is_array(ctx, obj))
{
func->has_range = 1;
- func->n = fz_array_len(obj) / 2;
+ func->n = fz_array_len(ctx, obj) / 2;
for (i = 0; i < func->n; i++)
{
- func->range[i][0] = fz_to_real(fz_array_get(obj, i * 2 + 0));
- func->range[i][1] = fz_to_real(fz_array_get(obj, i * 2 + 1));
+ func->range[i][0] = fz_to_real(ctx, fz_array_get(ctx, obj, i * 2 + 0));
+ func->range[i][1] = fz_to_real(ctx, fz_array_get(ctx, obj, i * 2 + 1));
}
}
else
@@ -1395,7 +1397,7 @@ pdf_load_function(pdf_function **funcp, pdf_xref *xref, fz_obj *dict)
if (func->m >= MAXM || func->n >= MAXN)
{
- fz_free(func);
+ fz_free(ctx, func);
return fz_error_make("assert: /Domain or /Range too big");
}
@@ -1405,16 +1407,16 @@ pdf_load_function(pdf_function **funcp, pdf_xref *xref, fz_obj *dict)
error = load_sample_func(func, xref, dict, fz_to_num(dict), fz_to_gen(dict));
if (error)
{
- pdf_drop_function(func);
+ pdf_drop_function(ctx, func);
return fz_error_note(error, "cannot load sampled function (%d %d R)", fz_to_num(dict), fz_to_gen(dict));
}
break;
case EXPONENTIAL:
- error = load_exponential_func(func, dict);
+ error = load_exponential_func(ctx, func, dict);
if (error)
{
- pdf_drop_function(func);
+ pdf_drop_function(ctx, func);
return fz_error_note(error, "cannot load exponential function (%d %d R)", fz_to_num(dict), fz_to_gen(dict));
}
break;
@@ -1423,7 +1425,7 @@ pdf_load_function(pdf_function **funcp, pdf_xref *xref, fz_obj *dict)
error = load_stitching_func(func, xref, dict);
if (error)
{
- pdf_drop_function(func);
+ pdf_drop_function(ctx, func);
return fz_error_note(error, "cannot load stitching function (%d %d R)", fz_to_num(dict), fz_to_gen(dict));
}
break;
@@ -1432,17 +1434,17 @@ pdf_load_function(pdf_function **funcp, pdf_xref *xref, fz_obj *dict)
error = load_postscript_func(func, xref, dict, fz_to_num(dict), fz_to_gen(dict));
if (error)
{
- pdf_drop_function(func);
+ pdf_drop_function(ctx, func);
return fz_error_note(error, "cannot load calculator function (%d %d R)", fz_to_num(dict), fz_to_gen(dict));
}
break;
default:
- fz_free(func);
+ fz_free(ctx, func);
return fz_error_make("unknown function type (%d %d R)", fz_to_num(dict), fz_to_gen(dict));
}
- pdf_store_item(xref->store, pdf_keep_function, pdf_drop_function, dict, func);
+ pdf_store_item(ctx, xref->store, (pdf_store_keep_fn *)pdf_keep_function, (pdf_store_drop_fn *)pdf_drop_function, dict, func);
*funcp = func;
return fz_okay;
diff --git a/pdf/pdf_image.c b/pdf/pdf_image.c
index 413efc98..95f26c31 100644
--- a/pdf/pdf_image.c
+++ b/pdf/pdf_image.c
@@ -46,9 +46,10 @@ pdf_load_image_imp(fz_pixmap **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict,
int stride;
unsigned char *samples;
int i, len;
+ fz_context *ctx = xref->ctx;
/* special case for JPEG2000 images */
- if (pdf_is_jpx_image(dict))
+ if (pdf_is_jpx_image(ctx, dict))
{
tile = NULL;
error = pdf_load_jpx_image(&tile, xref, dict);
@@ -58,11 +59,11 @@ pdf_load_image_imp(fz_pixmap **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict,
{
if (tile->n != 2)
{
- fz_drop_pixmap(tile);
+ fz_drop_pixmap(ctx, tile);
return fz_error_make("softmask must be grayscale");
}
- mask = fz_alpha_from_gray(tile, 1);
- fz_drop_pixmap(tile);
+ mask = fz_alpha_from_gray(ctx, tile, 1);
+ fz_drop_pixmap(ctx, tile);
*imgp = mask;
return fz_okay;
}
@@ -70,11 +71,11 @@ pdf_load_image_imp(fz_pixmap **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict,
return fz_okay;
}
- w = fz_to_int(fz_dict_getsa(dict, "Width", "W"));
- h = fz_to_int(fz_dict_getsa(dict, "Height", "H"));
- bpc = fz_to_int(fz_dict_getsa(dict, "BitsPerComponent", "BPC"));
- imagemask = fz_to_bool(fz_dict_getsa(dict, "ImageMask", "IM"));
- interpolate = fz_to_bool(fz_dict_getsa(dict, "Interpolate", "I"));
+ w = fz_to_int(ctx, fz_dict_getsa(ctx, dict, "Width", "W"));
+ h = fz_to_int(ctx, fz_dict_getsa(ctx, dict, "Height", "H"));
+ bpc = fz_to_int(ctx, fz_dict_getsa(ctx, dict, "BitsPerComponent", "BPC"));
+ imagemask = fz_to_bool(ctx, fz_dict_getsa(ctx, dict, "ImageMask", "IM"));
+ interpolate = fz_to_bool(ctx, fz_dict_getsa(ctx, dict, "Interpolate", "I"));
indexed = 0;
usecolorkey = 0;
@@ -97,13 +98,13 @@ pdf_load_image_imp(fz_pixmap **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict,
if (h > (1 << 16))
return fz_error_make("image is too high");
- obj = fz_dict_getsa(dict, "ColorSpace", "CS");
+ obj = fz_dict_getsa(ctx, dict, "ColorSpace", "CS");
if (obj && !imagemask && !forcemask)
{
/* colorspace resource lookup is only done for inline images */
- if (fz_is_name(obj))
+ if (fz_is_name(ctx, obj))
{
- res = fz_dict_get(fz_dict_gets(rdb, "ColorSpace"), obj);
+ res = fz_dict_get(ctx, fz_dict_gets(ctx, rdb, "ColorSpace"), obj);
if (res)
obj = res;
}
@@ -122,11 +123,11 @@ pdf_load_image_imp(fz_pixmap **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict,
n = 1;
}
- obj = fz_dict_getsa(dict, "Decode", "D");
+ obj = fz_dict_getsa(ctx, dict, "Decode", "D");
if (obj)
{
for (i = 0; i < n * 2; i++)
- decode[i] = fz_to_real(fz_array_get(obj, i));
+ decode[i] = fz_to_real(ctx, fz_array_get(ctx, obj, i));
}
else
{
@@ -135,8 +136,8 @@ pdf_load_image_imp(fz_pixmap **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict,
decode[i] = i & 1 ? maxval : 0;
}
- obj = fz_dict_getsa(dict, "SMask", "Mask");
- if (fz_is_dict(obj))
+ obj = fz_dict_getsa(ctx, dict, "SMask", "Mask");
+ if (fz_is_dict(ctx, obj))
{
/* Not allowed for inline images */
if (!cstm)
@@ -145,31 +146,31 @@ pdf_load_image_imp(fz_pixmap **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict,
if (error)
{
if (colorspace)
- fz_drop_colorspace(colorspace);
+ fz_drop_colorspace(ctx, colorspace);
return fz_error_note(error, "cannot load image mask/softmask");
}
}
}
- else if (fz_is_array(obj))
+ else if (fz_is_array(ctx, obj))
{
usecolorkey = 1;
for (i = 0; i < n * 2; i++)
- colorkey[i] = fz_to_int(fz_array_get(obj, i));
+ colorkey[i] = fz_to_int(ctx, fz_array_get(ctx, obj, i));
}
/* Allocate now, to fail early if we run out of memory */
- tile = fz_new_pixmap_with_limit(colorspace, w, h);
+ tile = fz_new_pixmap_with_limit(ctx, colorspace, w, h);
if (!tile)
{
if (colorspace)
- fz_drop_colorspace(colorspace);
+ fz_drop_colorspace(ctx, colorspace);
if (mask)
- fz_drop_pixmap(mask);
+ fz_drop_pixmap(ctx, mask);
return fz_error_make("out of memory");
}
if (colorspace)
- fz_drop_colorspace(colorspace);
+ fz_drop_colorspace(ctx, colorspace);
tile->mask = mask;
tile->interpolate = interpolate;
@@ -185,19 +186,19 @@ pdf_load_image_imp(fz_pixmap **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict,
error = pdf_open_stream(&stm, xref, fz_to_num(dict), fz_to_gen(dict));
if (error)
{
- fz_drop_pixmap(tile);
+ fz_drop_pixmap(ctx, tile);
return fz_error_note(error, "cannot open image data stream (%d 0 R)", fz_to_num(dict));
}
}
- samples = fz_calloc(h, stride);
+ samples = fz_calloc(ctx, h, stride);
len = fz_read(stm, samples, h * stride);
if (len < 0)
{
fz_close(stm);
- fz_free(samples);
- fz_drop_pixmap(tile);
+ fz_free(ctx, samples);
+ fz_drop_pixmap(ctx, tile);
return fz_error_note(len, "cannot read image data");
}
@@ -233,7 +234,7 @@ pdf_load_image_imp(fz_pixmap **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict,
fz_unpack_tile(tile, samples, n, bpc, stride, indexed);
- fz_free(samples);
+ fz_free(ctx, samples);
if (usecolorkey)
pdf_mask_color_key(tile, n, colorkey);
@@ -242,8 +243,8 @@ pdf_load_image_imp(fz_pixmap **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict,
{
fz_pixmap *conv;
fz_decode_indexed_tile(tile, decode, (1 << bpc) - 1);
- conv = pdf_expand_indexed_pixmap(tile);
- fz_drop_pixmap(tile);
+ conv = pdf_expand_indexed_pixmap(ctx, tile);
+ fz_drop_pixmap(ctx, tile);
tile = conv;
}
else
@@ -268,16 +269,17 @@ pdf_load_inline_image(fz_pixmap **pixp, pdf_xref *xref, fz_obj *rdb, fz_obj *dic
}
int
-pdf_is_jpx_image(fz_obj *dict)
+pdf_is_jpx_image(fz_context *ctx, fz_obj *dict)
{
fz_obj *filter;
- int i;
+ int i, n;
- filter = fz_dict_gets(dict, "Filter");
- if (!strcmp(fz_to_name(filter), "JPXDecode"))
+ filter = fz_dict_gets(ctx, dict, "Filter");
+ if (!strcmp(fz_to_name(ctx, filter), "JPXDecode"))
return 1;
- for (i = 0; i < fz_array_len(filter); i++)
- if (!strcmp(fz_to_name(fz_array_get(filter, i)), "JPXDecode"))
+ n = fz_array_len(ctx, filter);
+ for (i = 0; i < n; i++)
+ if (!strcmp(fz_to_name(ctx, fz_array_get(ctx, filter, i)), "JPXDecode"))
return 1;
return 0;
}
@@ -290,6 +292,7 @@ pdf_load_jpx_image(fz_pixmap **imgp, pdf_xref *xref, fz_obj *dict)
fz_colorspace *colorspace;
fz_pixmap *img;
fz_obj *obj;
+ fz_context *ctx = xref->ctx;
colorspace = NULL;
@@ -297,7 +300,7 @@ pdf_load_jpx_image(fz_pixmap **imgp, pdf_xref *xref, fz_obj *dict)
if (error)
return fz_error_note(error, "cannot load jpx image data");
- obj = fz_dict_gets(dict, "ColorSpace");
+ obj = fz_dict_gets(ctx, dict, "ColorSpace");
if (obj)
{
error = pdf_load_colorspace(&colorspace, xref, obj);
@@ -305,26 +308,26 @@ pdf_load_jpx_image(fz_pixmap **imgp, pdf_xref *xref, fz_obj *dict)
fz_error_handle(error, "cannot load image colorspace");
}
- error = fz_load_jpx_image(&img, buf->data, buf->len, colorspace);
+ error = fz_load_jpx_image(ctx, &img, buf->data, buf->len, colorspace);
if (error)
{
if (colorspace)
- fz_drop_colorspace(colorspace);
- fz_drop_buffer(buf);
+ fz_drop_colorspace(ctx, colorspace);
+ fz_drop_buffer(ctx, buf);
return fz_error_note(error, "cannot load jpx image");
}
if (colorspace)
- fz_drop_colorspace(colorspace);
- fz_drop_buffer(buf);
+ fz_drop_colorspace(ctx, colorspace);
+ fz_drop_buffer(ctx, buf);
- obj = fz_dict_getsa(dict, "SMask", "Mask");
- if (fz_is_dict(obj))
+ obj = fz_dict_getsa(ctx, dict, "SMask", "Mask");
+ if (fz_is_dict(ctx, obj))
{
error = pdf_load_image_imp(&img->mask, xref, NULL, obj, NULL, 1);
if (error)
{
- fz_drop_pixmap(img);
+ fz_drop_pixmap(ctx, img);
return fz_error_note(error, "cannot load image mask/softmask");
}
}
@@ -337,8 +340,9 @@ fz_error
pdf_load_image(fz_pixmap **pixp, pdf_xref *xref, fz_obj *dict)
{
fz_error error;
+ fz_context *ctx = xref->ctx;
- if ((*pixp = pdf_find_item(xref->store, fz_drop_pixmap, dict)))
+ if ((*pixp = pdf_find_item(ctx, xref->store, (pdf_store_drop_fn *)fz_drop_pixmap, dict)))
{
fz_keep_pixmap(*pixp);
return fz_okay;
@@ -348,7 +352,7 @@ pdf_load_image(fz_pixmap **pixp, pdf_xref *xref, fz_obj *dict)
if (error)
return fz_error_note(error, "cannot load image (%d 0 R)", fz_to_num(dict));
- pdf_store_item(xref->store, fz_keep_pixmap, fz_drop_pixmap, dict, *pixp);
+ pdf_store_item(ctx, xref->store, (pdf_store_keep_fn *)fz_keep_pixmap, (pdf_store_drop_fn *)fz_drop_pixmap, dict, *pixp);
return fz_okay;
}
diff --git a/pdf/pdf_interpret.c b/pdf/pdf_interpret.c
index 77ff59d8..d4d909bd 100644
--- a/pdf/pdf_interpret.c
+++ b/pdf/pdf_interpret.c
@@ -102,7 +102,7 @@ static void pdf_show_pattern(pdf_csi *csi, pdf_pattern *pat, fz_rect area, int w
static int
-pdf_is_hidden_ocg(fz_obj *xobj, char *target)
+pdf_is_hidden_ocg(fz_context *ctx, fz_obj *xobj, char *target)
{
char target_state[16];
fz_obj *obj;
@@ -110,14 +110,14 @@ pdf_is_hidden_ocg(fz_obj *xobj, char *target)
fz_strlcpy(target_state, target, sizeof target_state);
fz_strlcat(target_state, "State", sizeof target_state);
- obj = fz_dict_gets(xobj, "OC");
- obj = fz_dict_gets(obj, "OCGs");
- if (fz_is_array(obj))
- obj = fz_array_get(obj, 0);
- obj = fz_dict_gets(obj, "Usage");
- obj = fz_dict_gets(obj, target);
- obj = fz_dict_gets(obj, target_state);
- return !strcmp(fz_to_name(obj), "OFF");
+ obj = fz_dict_gets(ctx, xobj, "OC");
+ obj = fz_dict_gets(ctx, obj, "OCGs");
+ if (fz_is_array(ctx, obj))
+ obj = fz_array_get(ctx, obj, 0);
+ obj = fz_dict_gets(ctx, obj, "Usage");
+ obj = fz_dict_gets(ctx, obj, target);
+ obj = fz_dict_gets(ctx, obj, target_state);
+ return !strcmp(fz_to_name(ctx, obj), "OFF");
}
/*
@@ -257,12 +257,13 @@ pdf_show_path(pdf_csi *csi, int doclose, int dofill, int dostroke, int even_odd)
pdf_gstate *gstate = csi->gstate + csi->gtop;
fz_path *path;
fz_rect bbox;
+ fz_context *ctx = csi->dev->ctx;
path = csi->path;
- csi->path = fz_new_path();
+ csi->path = fz_new_path(ctx);
if (doclose)
- fz_closepath(path);
+ fz_closepath(ctx, path);
if (dostroke)
bbox = fz_bound_path(path, &gstate->stroke_state, gstate->ctm);
@@ -333,7 +334,7 @@ pdf_show_path(pdf_csi *csi, int doclose, int dofill, int dostroke, int even_odd)
if (dofill || dostroke)
pdf_end_group(csi);
- fz_free_path(path);
+ fz_free_path(ctx, path);
}
/*
@@ -350,6 +351,7 @@ pdf_flush_text(pdf_csi *csi)
int doclip = 0;
int doinvisible = 0;
fz_rect bbox;
+ fz_context *ctx = csi->dev->ctx;
if (!csi->text)
return;
@@ -444,7 +446,7 @@ pdf_flush_text(pdf_csi *csi)
pdf_end_group(csi);
- fz_free_text(text);
+ fz_free_text(ctx, text);
}
static void
@@ -460,6 +462,7 @@ pdf_show_char(pdf_csi *csi, int cid)
int ucsbuf[8];
int ucslen;
int i;
+ fz_context *ctx = csi->dev->ctx;
tsm.a = gstate->size * gstate->scale;
tsm.b = 0;
@@ -505,18 +508,18 @@ pdf_show_char(pdf_csi *csi, int cid)
{
pdf_flush_text(csi);
- csi->text = fz_new_text(fontdesc->font, trm, fontdesc->wmode);
+ csi->text = fz_new_text(ctx, fontdesc->font, trm, fontdesc->wmode);
csi->text->trm.e = 0;
csi->text->trm.f = 0;
csi->text_mode = gstate->render;
}
/* add glyph to textobject */
- fz_add_text(csi->text, gid, ucsbuf[0], trm.e, trm.f);
+ fz_add_text(ctx, csi->text, gid, ucsbuf[0], trm.e, trm.f);
/* add filler glyphs for one-to-many unicode mapping */
for (i = 1; i < ucslen; i++)
- fz_add_text(csi->text, -1, ucsbuf[i], trm.e, trm.f);
+ fz_add_text(ctx, csi->text, -1, ucsbuf[i], trm.e, trm.f);
if (fontdesc->wmode == 0)
{
@@ -584,21 +587,23 @@ pdf_show_text(pdf_csi *csi, fz_obj *text)
{
pdf_gstate *gstate = csi->gstate + csi->gtop;
int i;
+ fz_context *ctx = csi->dev->ctx;
- if (fz_is_array(text))
+ if (fz_is_array(ctx, text))
{
- for (i = 0; i < fz_array_len(text); i++)
+ int n = fz_array_len(ctx, text);
+ for (i = 0; i < n; i++)
{
- fz_obj *item = fz_array_get(text, i);
- if (fz_is_string(item))
- pdf_show_string(csi, (unsigned char *)fz_to_str_buf(item), fz_to_str_len(item));
+ fz_obj *item = fz_array_get(ctx, text, i);
+ if (fz_is_string(ctx, item))
+ pdf_show_string(csi, (unsigned char *)fz_to_str_buf(ctx, item), fz_to_str_len(ctx, item));
else
- pdf_show_space(csi, - fz_to_real(item) * gstate->size * 0.001f);
+ pdf_show_space(csi, - fz_to_real(ctx, item) * gstate->size * 0.001f);
}
}
- else if (fz_is_string(text))
+ else if (fz_is_string(ctx, text))
{
- pdf_show_string(csi, (unsigned char *)fz_to_str_buf(text), fz_to_str_len(text));
+ pdf_show_string(csi, (unsigned char *)fz_to_str_buf(ctx, text), fz_to_str_len(ctx, text));
}
}
@@ -656,7 +661,7 @@ pdf_new_csi(pdf_xref *xref, fz_device *dev, fz_matrix ctm, char *target)
{
pdf_csi *csi;
- csi = fz_malloc(sizeof(pdf_csi));
+ csi = fz_malloc(dev->ctx, sizeof(pdf_csi));
csi->xref = xref;
csi->dev = dev;
csi->target = target;
@@ -670,7 +675,7 @@ pdf_new_csi(pdf_xref *xref, fz_device *dev, fz_matrix ctm, char *target)
csi->xbalance = 0;
csi->in_text = 0;
- csi->path = fz_new_path();
+ csi->path = fz_new_path(dev->ctx);
csi->text = NULL;
csi->tlm = fz_identity;
@@ -691,7 +696,7 @@ pdf_clear_stack(pdf_csi *csi)
int i;
if (csi->obj)
- fz_drop_obj(csi->obj);
+ fz_drop_obj(csi->dev->ctx, csi->obj);
csi->obj = NULL;
csi->name[0] = 0;
@@ -715,14 +720,14 @@ pdf_keep_material(pdf_material *mat)
}
static pdf_material *
-pdf_drop_material(pdf_material *mat)
+pdf_drop_material(fz_context *ctx, pdf_material *mat)
{
if (mat->colorspace)
- fz_drop_colorspace(mat->colorspace);
+ fz_drop_colorspace(ctx, mat->colorspace);
if (mat->pattern)
- pdf_drop_pattern(mat->pattern);
+ pdf_drop_pattern(ctx, mat->pattern);
if (mat->shade)
- fz_drop_shade(mat->shade);
+ fz_drop_shade(ctx, mat->shade);
return mat;
}
@@ -754,6 +759,7 @@ pdf_grestore(pdf_csi *csi)
{
pdf_gstate *gs = csi->gstate + csi->gtop;
int clip_depth = gs->clip_depth;
+ fz_context *ctx = csi->dev->ctx;
if (csi->gtop == 0)
{
@@ -761,12 +767,12 @@ pdf_grestore(pdf_csi *csi)
return;
}
- pdf_drop_material(&gs->stroke);
- pdf_drop_material(&gs->fill);
+ pdf_drop_material(ctx, &gs->stroke);
+ pdf_drop_material(ctx, &gs->fill);
if (gs->font)
- pdf_drop_font(gs->font);
+ pdf_drop_font(ctx, gs->font);
if (gs->softmask)
- pdf_drop_xobject(gs->softmask);
+ pdf_drop_xobject(ctx, gs->softmask);
csi->gtop --;
@@ -781,25 +787,27 @@ pdf_grestore(pdf_csi *csi)
static void
pdf_free_csi(pdf_csi *csi)
{
+ fz_context *ctx = csi->dev->ctx;
+
while (csi->gtop)
pdf_grestore(csi);
- pdf_drop_material(&csi->gstate[0].fill);
- pdf_drop_material(&csi->gstate[0].stroke);
+ pdf_drop_material(ctx, &csi->gstate[0].fill);
+ pdf_drop_material(ctx, &csi->gstate[0].stroke);
if (csi->gstate[0].font)
- pdf_drop_font(csi->gstate[0].font);
+ pdf_drop_font(ctx, csi->gstate[0].font);
if (csi->gstate[0].softmask)
- pdf_drop_xobject(csi->gstate[0].softmask);
+ pdf_drop_xobject(ctx, csi->gstate[0].softmask);
while (csi->gstate[0].clip_depth--)
fz_pop_clip(csi->dev);
- if (csi->path) fz_free_path(csi->path);
- if (csi->text) fz_free_text(csi->text);
+ if (csi->path) fz_free_path(ctx, csi->path);
+ if (csi->text) fz_free_text(ctx, csi->text);
pdf_clear_stack(csi);
- fz_free(csi);
+ fz_free(ctx, csi);
}
/*
@@ -816,7 +824,7 @@ pdf_set_colorspace(pdf_csi *csi, int what, fz_colorspace *colorspace)
mat = what == PDF_FILL ? &gs->fill : &gs->stroke;
- fz_drop_colorspace(mat->colorspace);
+ fz_drop_colorspace(csi->dev->ctx, mat->colorspace);
mat->kind = PDF_MAT_COLOR;
mat->colorspace = fz_keep_colorspace(colorspace);
@@ -867,7 +875,7 @@ pdf_set_shade(pdf_csi *csi, int what, fz_shade *shade)
mat = what == PDF_FILL ? &gs->fill : &gs->stroke;
if (mat->shade)
- fz_drop_shade(mat->shade);
+ fz_drop_shade(csi->dev->ctx, mat->shade);
mat->kind = PDF_MAT_SHADE;
mat->shade = fz_keep_shade(shade);
@@ -884,7 +892,7 @@ pdf_set_pattern(pdf_csi *csi, int what, pdf_pattern *pat, float *v)
mat = what == PDF_FILL ? &gs->fill : &gs->stroke;
if (mat->pattern)
- pdf_drop_pattern(mat->pattern);
+ pdf_drop_pattern(csi->dev->ctx, mat->pattern);
mat->kind = PDF_MAT_PATTERN;
if (pat)
@@ -905,7 +913,7 @@ pdf_unset_pattern(pdf_csi *csi, int what)
if (mat->kind == PDF_MAT_PATTERN)
{
if (mat->pattern)
- pdf_drop_pattern(mat->pattern);
+ pdf_drop_pattern(csi->dev->ctx, mat->pattern);
mat->pattern = NULL;
mat->kind = PDF_MAT_COLOR;
}
@@ -924,6 +932,7 @@ pdf_show_pattern(pdf_csi *csi, pdf_pattern *pat, fz_rect area, int what)
fz_error error;
int x0, y0, x1, y1;
int oldtop;
+ fz_context *ctx = csi->dev->ctx;
pdf_gsave(csi);
gstate = csi->gstate + csi->gtop;
@@ -934,13 +943,13 @@ pdf_show_pattern(pdf_csi *csi, pdf_pattern *pat, fz_rect area, int what)
pdf_unset_pattern(csi, PDF_STROKE);
if (what == PDF_FILL)
{
- pdf_drop_material(&gstate->stroke);
+ pdf_drop_material(ctx, &gstate->stroke);
pdf_keep_material(&gstate->fill);
gstate->stroke = gstate->fill;
}
if (what == PDF_STROKE)
{
- pdf_drop_material(&gstate->fill);
+ pdf_drop_material(ctx, &gstate->fill);
pdf_keep_material(&gstate->stroke);
gstate->fill = gstate->stroke;
}
@@ -954,7 +963,7 @@ pdf_show_pattern(pdf_csi *csi, pdf_pattern *pat, fz_rect area, int what)
/* don't apply softmasks to objects in the pattern as well */
if (gstate->softmask)
{
- pdf_drop_xobject(gstate->softmask);
+ pdf_drop_xobject(ctx, gstate->softmask);
gstate->softmask = NULL;
}
@@ -1027,6 +1036,7 @@ pdf_run_xobject(pdf_csi *csi, fz_obj *resources, pdf_xobject *xobj, fz_matrix tr
fz_matrix oldtopctm;
int oldtop;
int popmask;
+ fz_context *ctx = csi->dev->ctx;
pdf_gsave(csi);
@@ -1056,7 +1066,7 @@ pdf_run_xobject(pdf_csi *csi, fz_obj *resources, pdf_xobject *xobj, fz_matrix tr
return fz_error_note(error, "cannot run softmask");
fz_end_mask(csi->dev);
- pdf_drop_xobject(softmask);
+ pdf_drop_xobject(ctx, softmask);
}
fz_begin_group(csi->dev,
@@ -1070,11 +1080,11 @@ pdf_run_xobject(pdf_csi *csi, fz_obj *resources, pdf_xobject *xobj, fz_matrix tr
/* clip to the bounds */
- fz_moveto(csi->path, xobj->bbox.x0, xobj->bbox.y0);
- fz_lineto(csi->path, xobj->bbox.x1, xobj->bbox.y0);
- fz_lineto(csi->path, xobj->bbox.x1, xobj->bbox.y1);
- fz_lineto(csi->path, xobj->bbox.x0, xobj->bbox.y1);
- fz_closepath(csi->path);
+ fz_moveto(ctx, csi->path, xobj->bbox.x0, xobj->bbox.y0);
+ fz_lineto(ctx, csi->path, xobj->bbox.x1, xobj->bbox.y0);
+ fz_lineto(ctx, csi->path, xobj->bbox.x1, xobj->bbox.y1);
+ fz_lineto(ctx, csi->path, xobj->bbox.x0, xobj->bbox.y1);
+ fz_closepath(ctx, csi->path);
pdf_show_clip(csi, 0);
pdf_show_path(csi, 0, 0, 0, 0);
@@ -1114,26 +1124,28 @@ pdf_run_extgstate(pdf_csi *csi, fz_obj *rdb, fz_obj *extgstate)
{
pdf_gstate *gstate = csi->gstate + csi->gtop;
fz_colorspace *colorspace;
- int i, k;
+ int i, k, n;
+ fz_context *ctx = csi->dev->ctx;
pdf_flush_text(csi);
- for (i = 0; i < fz_dict_len(extgstate); i++)
+ n = fz_dict_len(ctx, extgstate);
+ for (i = 0; i < n; i++)
{
- fz_obj *key = fz_dict_get_key(extgstate, i);
- fz_obj *val = fz_dict_get_val(extgstate, i);
- char *s = fz_to_name(key);
+ fz_obj *key = fz_dict_get_key(ctx, extgstate, i);
+ fz_obj *val = fz_dict_get_val(ctx, extgstate, i);
+ char *s = fz_to_name(ctx, key);
if (!strcmp(s, "Font"))
{
- if (fz_is_array(val) && fz_array_len(val) == 2)
+ if (fz_is_array(ctx, val) && fz_array_len(ctx, val) == 2)
{
fz_error error;
- fz_obj *font = fz_array_get(val, 0);
+ fz_obj *font = fz_array_get(ctx, val, 0);
if (gstate->font)
{
- pdf_drop_font(gstate->font);
+ pdf_drop_font(ctx, gstate->font);
gstate->font = NULL;
}
@@ -1142,7 +1154,7 @@ pdf_run_extgstate(pdf_csi *csi, fz_obj *rdb, fz_obj *extgstate)
return fz_error_note(error, "cannot load font (%d %d R)", fz_to_num(font), fz_to_gen(font));
if (!gstate->font)
return fz_error_make("cannot find font in store");
- gstate->size = fz_to_real(fz_array_get(val, 1));
+ gstate->size = fz_to_real(ctx, fz_array_get(ctx, val, 1));
}
else
return fz_error_make("malformed /Font dictionary");
@@ -1150,47 +1162,47 @@ pdf_run_extgstate(pdf_csi *csi, fz_obj *rdb, fz_obj *extgstate)
else if (!strcmp(s, "LC"))
{
- gstate->stroke_state.start_cap = fz_to_int(val);
- gstate->stroke_state.dash_cap = fz_to_int(val);
- gstate->stroke_state.end_cap = fz_to_int(val);
+ gstate->stroke_state.start_cap = fz_to_int(ctx, val);
+ gstate->stroke_state.dash_cap = fz_to_int(ctx, val);
+ gstate->stroke_state.end_cap = fz_to_int(ctx, val);
}
else if (!strcmp(s, "LW"))
- gstate->stroke_state.linewidth = fz_to_real(val);
+ gstate->stroke_state.linewidth = fz_to_real(ctx, val);
else if (!strcmp(s, "LJ"))
- gstate->stroke_state.linejoin = fz_to_int(val);
+ gstate->stroke_state.linejoin = fz_to_int(ctx, val);
else if (!strcmp(s, "ML"))
- gstate->stroke_state.miterlimit = fz_to_real(val);
+ gstate->stroke_state.miterlimit = fz_to_real(ctx, val);
else if (!strcmp(s, "D"))
{
- if (fz_is_array(val) && fz_array_len(val) == 2)
+ if (fz_is_array(ctx, val) && fz_array_len(ctx, val) == 2)
{
- fz_obj *dashes = fz_array_get(val, 0);
- gstate->stroke_state.dash_len = MAX(fz_array_len(dashes), 32);
+ fz_obj *dashes = fz_array_get(ctx, val, 0);
+ gstate->stroke_state.dash_len = MAX(fz_array_len(ctx, dashes), 32);
for (k = 0; k < gstate->stroke_state.dash_len; k++)
- gstate->stroke_state.dash_list[k] = fz_to_real(fz_array_get(dashes, k));
- gstate->stroke_state.dash_phase = fz_to_real(fz_array_get(val, 1));
+ gstate->stroke_state.dash_list[k] = fz_to_real(ctx, fz_array_get(ctx, dashes, k));
+ gstate->stroke_state.dash_phase = fz_to_real(ctx, fz_array_get(ctx, val, 1));
}
else
return fz_error_make("malformed /D");
}
else if (!strcmp(s, "CA"))
- gstate->stroke.alpha = fz_to_real(val);
+ gstate->stroke.alpha = fz_to_real(ctx, val);
else if (!strcmp(s, "ca"))
- gstate->fill.alpha = fz_to_real(val);
+ gstate->fill.alpha = fz_to_real(ctx, val);
else if (!strcmp(s, "BM"))
{
- if (fz_is_array(val))
- val = fz_array_get(val, 0);
- gstate->blendmode = fz_find_blendmode(fz_to_name(val));
+ if (fz_is_array(ctx, val))
+ val = fz_array_get(ctx, val, 0);
+ gstate->blendmode = fz_find_blendmode(fz_to_name(ctx, val));
}
else if (!strcmp(s, "SMask"))
{
- if (fz_is_dict(val))
+ if (fz_is_dict(ctx, val))
{
fz_error error;
pdf_xobject *xobj;
@@ -1198,11 +1210,11 @@ pdf_run_extgstate(pdf_csi *csi, fz_obj *rdb, fz_obj *extgstate)
if (gstate->softmask)
{
- pdf_drop_xobject(gstate->softmask);
+ pdf_drop_xobject(ctx, gstate->softmask);
gstate->softmask = NULL;
}
- group = fz_dict_gets(val, "G");
+ group = fz_dict_gets(ctx, val, "G");
if (!group)
return fz_error_make("cannot load softmask xobject (%d %d R)", fz_to_num(val), fz_to_gen(val));
error = pdf_load_xobject(&xobj, csi->xref, group);
@@ -1218,24 +1230,24 @@ pdf_run_extgstate(pdf_csi *csi, fz_obj *rdb, fz_obj *extgstate)
for (k = 0; k < colorspace->n; k++)
gstate->softmask_bc[k] = 0;
- bc = fz_dict_gets(val, "BC");
- if (fz_is_array(bc))
+ bc = fz_dict_gets(ctx, val, "BC");
+ if (fz_is_array(ctx, bc))
{
for (k = 0; k < colorspace->n; k++)
- gstate->softmask_bc[k] = fz_to_real(fz_array_get(bc, k));
+ gstate->softmask_bc[k] = fz_to_real(ctx, fz_array_get(ctx, bc, k));
}
- luminosity = fz_dict_gets(val, "S");
- if (fz_is_name(luminosity) && !strcmp(fz_to_name(luminosity), "Luminosity"))
+ luminosity = fz_dict_gets(ctx, val, "S");
+ if (fz_is_name(ctx, luminosity) && !strcmp(fz_to_name(ctx, luminosity), "Luminosity"))
gstate->luminosity = 1;
else
gstate->luminosity = 0;
}
- else if (fz_is_name(val) && !strcmp(fz_to_name(val), "None"))
+ else if (fz_is_name(ctx, val) && !strcmp(fz_to_name(ctx, val), "None"))
{
if (gstate->softmask)
{
- pdf_drop_xobject(gstate->softmask);
+ pdf_drop_xobject(ctx, gstate->softmask);
gstate->softmask = NULL;
}
}
@@ -1243,7 +1255,7 @@ pdf_run_extgstate(pdf_csi *csi, fz_obj *rdb, fz_obj *extgstate)
else if (!strcmp(s, "TR"))
{
- if (fz_is_name(val) && strcmp(fz_to_name(val), "Identity"))
+ if (fz_is_name(ctx, val) && strcmp(fz_to_name(ctx, val), "Identity"))
fz_warn("ignoring transfer function");
}
}
@@ -1267,6 +1279,7 @@ static fz_error pdf_run_BI(pdf_csi *csi, fz_obj *rdb, fz_stream *file)
int buflen = sizeof(csi->xref->scratch);
fz_pixmap *img;
fz_obj *obj;
+ fz_context *ctx = csi->dev->ctx;
error = pdf_parse_dict(&obj, csi->xref, file, buf, buflen);
if (error)
@@ -1279,13 +1292,13 @@ static fz_error pdf_run_BI(pdf_csi *csi, fz_obj *rdb, fz_stream *file)
fz_read_byte(file);
error = pdf_load_inline_image(&img, csi->xref, rdb, obj, file);
- fz_drop_obj(obj);
+ fz_drop_obj(ctx, obj);
if (error)
return fz_error_note(error, "cannot load inline image");
pdf_show_image(csi, img);
- fz_drop_pixmap(img);
+ fz_drop_pixmap(ctx, img);
/* find EI */
ch = fz_read_byte(file);
@@ -1329,6 +1342,7 @@ static fz_error pdf_run_cs_imp(pdf_csi *csi, fz_obj *rdb, int what)
fz_colorspace *colorspace;
fz_obj *obj, *dict;
fz_error error;
+ fz_context *ctx = csi->dev->ctx;
if (!strcmp(csi->name, "Pattern"))
{
@@ -1344,10 +1358,10 @@ static fz_error pdf_run_cs_imp(pdf_csi *csi, fz_obj *rdb, int what)
colorspace = fz_keep_colorspace(fz_device_cmyk);
else
{
- dict = fz_dict_gets(rdb, "ColorSpace");
+ dict = fz_dict_gets(ctx, rdb, "ColorSpace");
if (!dict)
return fz_error_make("cannot find ColorSpace dictionary");
- obj = fz_dict_gets(dict, csi->name);
+ obj = fz_dict_gets(ctx, dict, csi->name);
if (!obj)
return fz_error_make("cannot find colorspace resource '%s'", csi->name);
error = pdf_load_colorspace(&colorspace, csi->xref, obj);
@@ -1357,7 +1371,7 @@ static fz_error pdf_run_cs_imp(pdf_csi *csi, fz_obj *rdb, int what)
pdf_set_colorspace(csi, what, colorspace);
- fz_drop_colorspace(colorspace);
+ fz_drop_colorspace(ctx, colorspace);
}
return fz_okay;
}
@@ -1388,26 +1402,27 @@ static fz_error pdf_run_Do(pdf_csi *csi, fz_obj *rdb)
fz_obj *obj;
fz_obj *subtype;
fz_error error;
+ fz_context *ctx = csi->dev->ctx;
- dict = fz_dict_gets(rdb, "XObject");
+ dict = fz_dict_gets(ctx, rdb, "XObject");
if (!dict)
return fz_error_make("cannot find XObject dictionary when looking for: '%s'", csi->name);
- obj = fz_dict_gets(dict, csi->name);
+ obj = fz_dict_gets(ctx, dict, csi->name);
if (!obj)
return fz_error_make("cannot find xobject resource: '%s'", csi->name);
- subtype = fz_dict_gets(obj, "Subtype");
- if (!fz_is_name(subtype))
+ subtype = fz_dict_gets(ctx, obj, "Subtype");
+ if (!fz_is_name(ctx, subtype))
return fz_error_make("no XObject subtype specified");
- if (pdf_is_hidden_ocg(obj, csi->target))
+ if (pdf_is_hidden_ocg(ctx, obj, csi->target))
return fz_okay;
- if (!strcmp(fz_to_name(subtype), "Form") && fz_dict_gets(obj, "Subtype2"))
- subtype = fz_dict_gets(obj, "Subtype2");
+ if (!strcmp(fz_to_name(ctx, subtype), "Form") && fz_dict_gets(ctx, obj, "Subtype2"))
+ subtype = fz_dict_gets(ctx, obj, "Subtype2");
- if (!strcmp(fz_to_name(subtype), "Form"))
+ if (!strcmp(fz_to_name(ctx, subtype), "Form"))
{
pdf_xobject *xobj;
@@ -1423,10 +1438,10 @@ static fz_error pdf_run_Do(pdf_csi *csi, fz_obj *rdb)
if (error)
return fz_error_note(error, "cannot draw xobject (%d %d R)", fz_to_num(obj), fz_to_gen(obj));
- pdf_drop_xobject(xobj);
+ pdf_drop_xobject(ctx, xobj);
}
- else if (!strcmp(fz_to_name(subtype), "Image"))
+ else if (!strcmp(fz_to_name(ctx, subtype), "Image"))
{
if ((csi->dev->hints & FZ_IGNORE_IMAGE) == 0)
{
@@ -1435,18 +1450,18 @@ static fz_error pdf_run_Do(pdf_csi *csi, fz_obj *rdb)
if (error)
return fz_error_note(error, "cannot load image (%d %d R)", fz_to_num(obj), fz_to_gen(obj));
pdf_show_image(csi, img);
- fz_drop_pixmap(img);
+ fz_drop_pixmap(ctx, img);
}
}
- else if (!strcmp(fz_to_name(subtype), "PS"))
+ else if (!strcmp(fz_to_name(ctx, subtype), "PS"))
{
fz_warn("ignoring XObject with subtype PS");
}
else
{
- return fz_error_make("unknown XObject subtype: '%s'", fz_to_name(subtype));
+ return fz_error_make("unknown XObject subtype: '%s'", fz_to_name(ctx, subtype));
}
return fz_okay;
@@ -1526,6 +1541,7 @@ static fz_error pdf_run_SC_imp(pdf_csi *csi, fz_obj *rdb, int what, pdf_material
fz_obj *dict;
fz_obj *obj;
int kind;
+ fz_context *ctx = csi->dev->ctx;
kind = mat->kind;
if (csi->name[0])
@@ -1541,37 +1557,37 @@ static fz_error pdf_run_SC_imp(pdf_csi *csi, fz_obj *rdb, int what, pdf_material
break;
case PDF_MAT_PATTERN:
- dict = fz_dict_gets(rdb, "Pattern");
+ dict = fz_dict_gets(ctx, rdb, "Pattern");
if (!dict)
return fz_error_make("cannot find Pattern dictionary");
- obj = fz_dict_gets(dict, csi->name);
+ obj = fz_dict_gets(ctx, dict, csi->name);
if (!obj)
return fz_error_make("cannot find pattern resource '%s'", csi->name);
- patterntype = fz_dict_gets(obj, "PatternType");
+ patterntype = fz_dict_gets(ctx, obj, "PatternType");
- if (fz_to_int(patterntype) == 1)
+ if (fz_to_int(ctx, patterntype) == 1)
{
pdf_pattern *pat;
error = pdf_load_pattern(&pat, csi->xref, obj);
if (error)
return fz_error_note(error, "cannot load pattern (%d 0 R)", fz_to_num(obj));
pdf_set_pattern(csi, what, pat, csi->top > 0 ? csi->stack : NULL);
- pdf_drop_pattern(pat);
+ pdf_drop_pattern(ctx, pat);
}
- else if (fz_to_int(patterntype) == 2)
+ else if (fz_to_int(ctx, patterntype) == 2)
{
fz_shade *shd;
error = pdf_load_shading(&shd, csi->xref, obj);
if (error)
return fz_error_note(error, "cannot load shading (%d 0 R)", fz_to_num(obj));
pdf_set_shade(csi, what, shd);
- fz_drop_shade(shd);
+ fz_drop_shade(ctx, shd);
}
else
{
- return fz_error_make("unknown pattern type: %d", fz_to_int(patterntype));
+ return fz_error_make("unknown pattern type: %d", fz_to_int(ctx, patterntype));
}
break;
@@ -1632,17 +1648,18 @@ static fz_error pdf_run_Tf(pdf_csi *csi, fz_obj *rdb)
fz_error error;
fz_obj *dict;
fz_obj *obj;
+ fz_context *ctx = csi->dev->ctx;
gstate->size = csi->stack[0];
if (gstate->font)
- pdf_drop_font(gstate->font);
+ pdf_drop_font(ctx, gstate->font);
gstate->font = NULL;
- dict = fz_dict_gets(rdb, "Font");
+ dict = fz_dict_gets(ctx, rdb, "Font");
if (!dict)
return fz_error_make("cannot find Font dictionary");
- obj = fz_dict_gets(dict, csi->name);
+ obj = fz_dict_gets(ctx, dict, csi->name);
if (!obj)
return fz_error_make("cannot find font resource: '%s'", csi->name);
@@ -1747,7 +1764,7 @@ static void pdf_run_c(pdf_csi *csi)
d = csi->stack[3];
e = csi->stack[4];
f = csi->stack[5];
- fz_curveto(csi->path, a, b, c, d, e, f);
+ fz_curveto(csi->dev->ctx, csi->path, a, b, c, d, e, f);
}
static void pdf_run_cm(pdf_csi *csi)
@@ -1770,11 +1787,12 @@ static void pdf_run_d(pdf_csi *csi)
pdf_gstate *gstate = csi->gstate + csi->gtop;
fz_obj *array;
int i;
+ fz_context *ctx = csi->dev->ctx;
array = csi->obj;
- gstate->stroke_state.dash_len = MIN(fz_array_len(array), nelem(gstate->stroke_state.dash_list));
+ gstate->stroke_state.dash_len = MIN(fz_array_len(ctx, array), nelem(gstate->stroke_state.dash_list));
for (i = 0; i < gstate->stroke_state.dash_len; i++)
- gstate->stroke_state.dash_list[i] = fz_to_real(fz_array_get(array, i));
+ gstate->stroke_state.dash_list[i] = fz_to_real(ctx, fz_array_get(ctx, array, i));
gstate->stroke_state.dash_phase = csi->stack[0];
}
@@ -1809,12 +1827,13 @@ static fz_error pdf_run_gs(pdf_csi *csi, fz_obj *rdb)
fz_error error;
fz_obj *dict;
fz_obj *obj;
+ fz_context *ctx = csi->dev->ctx;
- dict = fz_dict_gets(rdb, "ExtGState");
+ dict = fz_dict_gets(ctx, rdb, "ExtGState");
if (!dict)
return fz_error_make("cannot find ExtGState dictionary");
- obj = fz_dict_gets(dict, csi->name);
+ obj = fz_dict_gets(ctx, dict, csi->name);
if (!obj)
return fz_error_make("cannot find extgstate resource '%s'", csi->name);
@@ -1826,7 +1845,7 @@ static fz_error pdf_run_gs(pdf_csi *csi, fz_obj *rdb)
static void pdf_run_h(pdf_csi *csi)
{
- fz_closepath(csi->path);
+ fz_closepath(csi->dev->ctx, csi->path);
}
static void pdf_run_i(pdf_csi *csi)
@@ -1850,7 +1869,7 @@ static void pdf_run_l(pdf_csi *csi)
float a, b;
a = csi->stack[0];
b = csi->stack[1];
- fz_lineto(csi->path, a, b);
+ fz_lineto(csi->dev->ctx, csi->path, a, b);
}
static void pdf_run_m(pdf_csi *csi)
@@ -1858,7 +1877,7 @@ static void pdf_run_m(pdf_csi *csi)
float a, b;
a = csi->stack[0];
b = csi->stack[1];
- fz_moveto(csi->path, a, b);
+ fz_moveto(csi->dev->ctx, csi->path, a, b);
}
static void pdf_run_n(pdf_csi *csi)
@@ -1874,17 +1893,18 @@ static void pdf_run_q(pdf_csi *csi)
static void pdf_run_re(pdf_csi *csi)
{
float x, y, w, h;
+ fz_context *ctx = csi->dev->ctx;
x = csi->stack[0];
y = csi->stack[1];
w = csi->stack[2];
h = csi->stack[3];
- fz_moveto(csi->path, x, y);
- fz_lineto(csi->path, x + w, y);
- fz_lineto(csi->path, x + w, y + h);
- fz_lineto(csi->path, x, y + h);
- fz_closepath(csi->path);
+ fz_moveto(ctx, csi->path, x, y);
+ fz_lineto(ctx, csi->path, x + w, y);
+ fz_lineto(ctx, csi->path, x + w, y + h);
+ fz_lineto(ctx, csi->path, x, y + h);
+ fz_closepath(ctx, csi->path);
}
static void pdf_run_rg(pdf_csi *csi)
@@ -1908,12 +1928,13 @@ static fz_error pdf_run_sh(pdf_csi *csi, fz_obj *rdb)
fz_obj *obj;
fz_shade *shd;
fz_error error;
+ fz_context *ctx = csi->dev->ctx;
- dict = fz_dict_gets(rdb, "Shading");
+ dict = fz_dict_gets(ctx, rdb, "Shading");
if (!dict)
return fz_error_make("cannot find shading dictionary");
- obj = fz_dict_gets(dict, csi->name);
+ obj = fz_dict_gets(ctx, dict, csi->name);
if (!obj)
return fz_error_make("cannot find shading resource: '%s'", csi->name);
@@ -1923,7 +1944,7 @@ static fz_error pdf_run_sh(pdf_csi *csi, fz_obj *rdb)
if (error)
return fz_error_note(error, "cannot load shading (%d %d R)", fz_to_num(obj), fz_to_gen(obj));
pdf_show_shade(csi, shd);
- fz_drop_shade(shd);
+ fz_drop_shade(ctx, shd);
}
return fz_okay;
}
@@ -1935,7 +1956,7 @@ static void pdf_run_v(pdf_csi *csi)
b = csi->stack[1];
c = csi->stack[2];
d = csi->stack[3];
- fz_curvetov(csi->path, a, b, c, d);
+ fz_curvetov(csi->dev->ctx, csi->path, a, b, c, d);
}
static void pdf_run_w(pdf_csi *csi)
@@ -1952,7 +1973,7 @@ static void pdf_run_y(pdf_csi *csi)
b = csi->stack[1];
c = csi->stack[2];
d = csi->stack[3];
- fz_curvetoy(csi->path, a, b, c, d);
+ fz_curvetoy(csi->dev->ctx, csi->path, a, b, c, d);
}
static void pdf_run_squote(pdf_csi *csi)
@@ -2117,6 +2138,7 @@ pdf_run_stream(pdf_csi *csi, fz_obj *rdb, fz_stream *file, char *buf, int buflen
{
fz_error error;
int tok, len, in_array;
+ fz_context *ctx = csi->dev->ctx;
/* make sure we have a clean slate if we come here from flush_text */
pdf_clear_stack(csi);
@@ -2206,7 +2228,7 @@ pdf_run_stream(pdf_csi *csi, fz_obj *rdb, fz_stream *file, char *buf, int buflen
}
else
{
- csi->obj = fz_new_string(buf, len);
+ csi->obj = fz_new_string(ctx, buf, len);
}
break;
@@ -2231,15 +2253,16 @@ static fz_error
pdf_run_buffer(pdf_csi *csi, fz_obj *rdb, fz_buffer *contents)
{
fz_error error;
+ fz_context *ctx = csi->dev->ctx;
int len = sizeof csi->xref->scratch;
- char *buf = fz_malloc(len); /* we must be re-entrant for type3 fonts */
- fz_stream *file = fz_open_buffer(contents);
+ char *buf = fz_malloc(ctx, len); /* we must be re-entrant for type3 fonts */
+ fz_stream *file = fz_open_buffer(ctx, contents);
int save_in_text = csi->in_text;
csi->in_text = 0;
error = pdf_run_stream(csi, rdb, file, buf, len);
csi->in_text = save_in_text;
fz_close(file);
- fz_free(buf);
+ fz_free(ctx, buf);
if (error)
return fz_error_note(error, "cannot parse content stream");
return fz_okay;
@@ -2252,6 +2275,7 @@ pdf_run_page_with_usage(pdf_xref *xref, pdf_page *page, fz_device *dev, fz_matri
fz_error error;
pdf_annot *annot;
int flags;
+ fz_context *ctx = dev->ctx;
if (page->transparency)
fz_begin_group(dev, fz_transform_rect(ctm, page->mediabox), 1, 0, 0, 1);
@@ -2264,7 +2288,7 @@ pdf_run_page_with_usage(pdf_xref *xref, pdf_page *page, fz_device *dev, fz_matri
for (annot = page->annots; annot; annot = annot->next)
{
- flags = fz_to_int(fz_dict_gets(annot->obj, "F"));
+ flags = fz_to_int(ctx, fz_dict_gets(ctx, annot->obj, "F"));
/* TODO: NoZoom and NoRotate */
if (flags & (1 << 0)) /* Invisible */
@@ -2274,7 +2298,7 @@ pdf_run_page_with_usage(pdf_xref *xref, pdf_page *page, fz_device *dev, fz_matri
if (flags & (1 << 5)) /* NoView */
continue;
- if (pdf_is_hidden_ocg(annot->obj, target))
+ if (pdf_is_hidden_ocg(ctx, annot->obj, target))
continue;
csi = pdf_new_csi(xref, dev, ctm, target);
diff --git a/pdf/pdf_metrics.c b/pdf/pdf_metrics.c
index 2fc085de..62353f9f 100644
--- a/pdf/pdf_metrics.c
+++ b/pdf/pdf_metrics.c
@@ -21,12 +21,12 @@ pdf_set_default_vmtx(pdf_font_desc *font, int y, int w)
}
void
-pdf_add_hmtx(pdf_font_desc *font, int lo, int hi, int w)
+pdf_add_hmtx(fz_context *ctx, pdf_font_desc *font, int lo, int hi, int w)
{
if (font->hmtx_len + 1 >= font->hmtx_cap)
{
font->hmtx_cap = font->hmtx_cap + 16;
- font->hmtx = fz_realloc(font->hmtx, font->hmtx_cap, sizeof(pdf_hmtx));
+ font->hmtx = fz_realloc(ctx, font->hmtx, font->hmtx_cap * sizeof(pdf_hmtx));
}
font->hmtx[font->hmtx_len].lo = lo;
@@ -36,12 +36,12 @@ pdf_add_hmtx(pdf_font_desc *font, int lo, int hi, int w)
}
void
-pdf_add_vmtx(pdf_font_desc *font, int lo, int hi, int x, int y, int w)
+pdf_add_vmtx(fz_context *ctx, pdf_font_desc *font, int lo, int hi, int x, int y, int w)
{
if (font->vmtx_len + 1 >= font->vmtx_cap)
{
font->vmtx_cap = font->vmtx_cap + 16;
- font->vmtx = fz_realloc(font->vmtx, font->vmtx_cap, sizeof(pdf_vmtx));
+ font->vmtx = fz_realloc(ctx, font->vmtx, font->vmtx_cap * sizeof(pdf_vmtx));
}
font->vmtx[font->vmtx_len].lo = lo;
diff --git a/pdf/pdf_nametree.c b/pdf/pdf_nametree.c
index e2b3e160..838caa94 100644
--- a/pdf/pdf_nametree.c
+++ b/pdf/pdf_nametree.c
@@ -2,44 +2,44 @@
#include "mupdf.h"
static fz_obj *
-pdf_lookup_name_imp(fz_obj *node, fz_obj *needle)
+pdf_lookup_name_imp(fz_context *ctx, fz_obj *node, fz_obj *needle)
{
- fz_obj *kids = fz_dict_gets(node, "Kids");
- fz_obj *names = fz_dict_gets(node, "Names");
+ fz_obj *kids = fz_dict_gets(ctx, node, "Kids");
+ fz_obj *names = fz_dict_gets(ctx, node, "Names");
- if (fz_is_array(kids))
+ if (fz_is_array(ctx, kids))
{
int l = 0;
- int r = fz_array_len(kids) - 1;
+ int r = fz_array_len(ctx, kids) - 1;
while (l <= r)
{
int m = (l + r) >> 1;
- fz_obj *kid = fz_array_get(kids, m);
- fz_obj *limits = fz_dict_gets(kid, "Limits");
- fz_obj *first = fz_array_get(limits, 0);
- fz_obj *last = fz_array_get(limits, 1);
+ fz_obj *kid = fz_array_get(ctx, kids, m);
+ fz_obj *limits = fz_dict_gets(ctx, kid, "Limits");
+ fz_obj *first = fz_array_get(ctx, limits, 0);
+ fz_obj *last = fz_array_get(ctx, limits, 1);
if (fz_objcmp(needle, first) < 0)
r = m - 1;
else if (fz_objcmp(needle, last) > 0)
l = m + 1;
else
- return pdf_lookup_name_imp(kid, needle);
+ return pdf_lookup_name_imp(ctx, kid, needle);
}
}
- if (fz_is_array(names))
+ if (fz_is_array(ctx, names))
{
int l = 0;
- int r = (fz_array_len(names) / 2) - 1;
+ int r = (fz_array_len(ctx, names) / 2) - 1;
while (l <= r)
{
int m = (l + r) >> 1;
int c;
- fz_obj *key = fz_array_get(names, m * 2);
- fz_obj *val = fz_array_get(names, m * 2 + 1);
+ fz_obj *key = fz_array_get(ctx, names, m * 2);
+ fz_obj *val = fz_array_get(ctx, names, m * 2 + 1);
c = fz_objcmp(needle, key);
if (c < 0)
@@ -57,34 +57,38 @@ pdf_lookup_name_imp(fz_obj *node, fz_obj *needle)
fz_obj *
pdf_lookup_name(pdf_xref *xref, char *which, fz_obj *needle)
{
- fz_obj *root = fz_dict_gets(xref->trailer, "Root");
- fz_obj *names = fz_dict_gets(root, "Names");
- fz_obj *tree = fz_dict_gets(names, which);
- return pdf_lookup_name_imp(tree, needle);
+ fz_context *ctx = xref->ctx;
+
+ fz_obj *root = fz_dict_gets(ctx, xref->trailer, "Root");
+ fz_obj *names = fz_dict_gets(ctx, root, "Names");
+ fz_obj *tree = fz_dict_gets(ctx, names, which);
+ return pdf_lookup_name_imp(ctx, tree, needle);
}
fz_obj *
pdf_lookup_dest(pdf_xref *xref, fz_obj *needle)
{
- fz_obj *root = fz_dict_gets(xref->trailer, "Root");
- fz_obj *dests = fz_dict_gets(root, "Dests");
- fz_obj *names = fz_dict_gets(root, "Names");
+ fz_context *ctx = xref->ctx;
+
+ fz_obj *root = fz_dict_gets(ctx, xref->trailer, "Root");
+ fz_obj *dests = fz_dict_gets(ctx, root, "Dests");
+ fz_obj *names = fz_dict_gets(ctx, root, "Names");
fz_obj *dest = NULL;
/* PDF 1.1 has destinations in a dictionary */
if (dests)
{
- if (fz_is_name(needle))
- return fz_dict_get(dests, needle);
+ if (fz_is_name(ctx, needle))
+ return fz_dict_get(ctx, dests, needle);
else
- return fz_dict_gets(dests, fz_to_str_buf(needle));
+ return fz_dict_gets(ctx, dests, fz_to_str_buf(ctx, needle));
}
/* PDF 1.2 has destinations in a name tree */
if (names && !dest)
{
- fz_obj *tree = fz_dict_gets(names, "Dests");
- return pdf_lookup_name_imp(tree, needle);
+ fz_obj *tree = fz_dict_gets(ctx, names, "Dests");
+ return pdf_lookup_name_imp(ctx, tree, needle);
}
return NULL;
@@ -93,31 +97,32 @@ pdf_lookup_dest(pdf_xref *xref, fz_obj *needle)
static void
pdf_load_name_tree_imp(fz_obj *dict, pdf_xref *xref, fz_obj *node)
{
- fz_obj *kids = fz_dict_gets(node, "Kids");
- fz_obj *names = fz_dict_gets(node, "Names");
+ fz_context *ctx = xref->ctx;
+ fz_obj *kids = fz_dict_gets(ctx, node, "Kids");
+ fz_obj *names = fz_dict_gets(ctx, node, "Names");
int i;
if (kids)
{
- for (i = 0; i < fz_array_len(kids); i++)
- pdf_load_name_tree_imp(dict, xref, fz_array_get(kids, i));
+ for (i = 0; i < fz_array_len(ctx, kids); i++)
+ pdf_load_name_tree_imp(dict, xref, fz_array_get(ctx, kids, i));
}
if (names)
{
- for (i = 0; i + 1 < fz_array_len(names); i += 2)
+ for (i = 0; i + 1 < fz_array_len(ctx, names); i += 2)
{
- fz_obj *key = fz_array_get(names, i);
- fz_obj *val = fz_array_get(names, i + 1);
- if (fz_is_string(key))
+ fz_obj *key = fz_array_get(ctx, names, i);
+ fz_obj *val = fz_array_get(ctx, names, i + 1);
+ if (fz_is_string(ctx, key))
{
- key = pdf_to_utf8_name(key);
- fz_dict_put(dict, key, val);
- fz_drop_obj(key);
+ key = pdf_to_utf8_name(ctx, key);
+ fz_dict_put(ctx, dict, key, val);
+ fz_drop_obj(ctx, key);
}
- else if (fz_is_name(key))
+ else if (fz_is_name(ctx, key))
{
- fz_dict_put(dict, key, val);
+ fz_dict_put(ctx, dict, key, val);
}
}
}
@@ -126,12 +131,14 @@ pdf_load_name_tree_imp(fz_obj *dict, pdf_xref *xref, fz_obj *node)
fz_obj *
pdf_load_name_tree(pdf_xref *xref, char *which)
{
- fz_obj *root = fz_dict_gets(xref->trailer, "Root");
- fz_obj *names = fz_dict_gets(root, "Names");
- fz_obj *tree = fz_dict_gets(names, which);
- if (fz_is_dict(tree))
+ fz_context *ctx = xref->ctx;
+
+ fz_obj *root = fz_dict_gets(ctx, xref->trailer, "Root");
+ fz_obj *names = fz_dict_gets(ctx, root, "Names");
+ fz_obj *tree = fz_dict_gets(ctx, names, which);
+ if (fz_is_dict(ctx, tree))
{
- fz_obj *dict = fz_new_dict(100);
+ fz_obj *dict = fz_new_dict(ctx, 100);
pdf_load_name_tree_imp(dict, xref, tree);
return dict;
}
diff --git a/pdf/pdf_outline.c b/pdf/pdf_outline.c
index 4f81bb9b..0e6ab233 100644
--- a/pdf/pdf_outline.c
+++ b/pdf/pdf_outline.c
@@ -6,33 +6,34 @@ pdf_load_outline_imp(pdf_xref *xref, fz_obj *dict)
{
pdf_outline *node;
fz_obj *obj;
+ fz_context *ctx = xref->ctx;
- if (fz_is_null(dict))
+ if (fz_is_null(ctx, dict))
return NULL;
- node = fz_malloc(sizeof(pdf_outline));
+ node = fz_malloc(ctx, sizeof(pdf_outline));
node->title = NULL;
node->link = NULL;
node->child = NULL;
node->next = NULL;
node->count = 0;
- obj = fz_dict_gets(dict, "Title");
+ obj = fz_dict_gets(ctx, dict, "Title");
if (obj)
- node->title = pdf_to_utf8(obj);
+ node->title = pdf_to_utf8(ctx, obj);
- obj = fz_dict_gets(dict, "Count");
+ obj = fz_dict_gets(ctx, dict, "Count");
if (obj)
- node->count = fz_to_int(obj);
+ node->count = fz_to_int(ctx, obj);
- if (fz_dict_gets(dict, "Dest") || fz_dict_gets(dict, "A"))
+ if (fz_dict_gets(ctx, dict, "Dest") || fz_dict_gets(ctx, dict, "A"))
node->link = pdf_load_link(xref, dict);
- obj = fz_dict_gets(dict, "First");
+ obj = fz_dict_gets(ctx, dict, "First");
if (obj)
node->child = pdf_load_outline_imp(xref, obj);
- obj = fz_dict_gets(dict, "Next");
+ obj = fz_dict_gets(ctx, dict, "Next");
if (obj)
node->next = pdf_load_outline_imp(xref, obj);
@@ -43,10 +44,11 @@ pdf_outline *
pdf_load_outline(pdf_xref *xref)
{
fz_obj *root, *obj, *first;
+ fz_context *ctx = xref->ctx;
- root = fz_dict_gets(xref->trailer, "Root");
- obj = fz_dict_gets(root, "Outlines");
- first = fz_dict_gets(obj, "First");
+ root = fz_dict_gets(ctx, xref->trailer, "Root");
+ obj = fz_dict_gets(ctx, root, "Outlines");
+ first = fz_dict_gets(ctx, obj, "First");
if (first)
return pdf_load_outline_imp(xref, first);
@@ -54,20 +56,20 @@ pdf_load_outline(pdf_xref *xref)
}
void
-pdf_free_outline(pdf_outline *outline)
+pdf_free_outline(fz_context *ctx, pdf_outline *outline)
{
if (outline->child)
- pdf_free_outline(outline->child);
+ pdf_free_outline(ctx, outline->child);
if (outline->next)
- pdf_free_outline(outline->next);
+ pdf_free_outline(ctx, outline->next);
if (outline->link)
- pdf_free_link(outline->link);
- fz_free(outline->title);
- fz_free(outline);
+ pdf_free_link(ctx, outline->link);
+ fz_free(ctx, outline->title);
+ fz_free(ctx, outline);
}
void
-pdf_debug_outline(pdf_outline *outline, int level)
+pdf_debug_outline(fz_context *ctx, pdf_outline *outline, int level)
{
int i;
while (outline)
@@ -81,12 +83,12 @@ pdf_debug_outline(pdf_outline *outline, int level)
printf("<NULL> ");
if (outline->link)
- fz_debug_obj(outline->link->dest);
+ fz_debug_obj(ctx, outline->link->dest);
else
printf("<NULL>\n");
if (outline->child)
- pdf_debug_outline(outline->child, level + 2);
+ pdf_debug_outline(ctx, outline->child, level + 2);
outline = outline->next;
}
diff --git a/pdf/pdf_page.c b/pdf/pdf_page.c
index d7eeb3cd..724a4d13 100644
--- a/pdf/pdf_page.c
+++ b/pdf/pdf_page.c
@@ -31,61 +31,62 @@ pdf_load_page_tree_node(pdf_xref *xref, fz_obj *node, struct info info)
fz_obj *dict, *kids, *count;
fz_obj *obj, *tmp;
int i, n;
+ fz_context *ctx = xref->ctx;
/* prevent infinite recursion */
- if (fz_dict_gets(node, ".seen"))
+ if (fz_dict_gets(ctx, node, ".seen"))
return;
- kids = fz_dict_gets(node, "Kids");
- count = fz_dict_gets(node, "Count");
+ kids = fz_dict_gets(ctx, node, "Kids");
+ count = fz_dict_gets(ctx, node, "Count");
- if (fz_is_array(kids) && fz_is_int(count))
+ if (fz_is_array(ctx, kids) && fz_is_int(ctx, count))
{
- obj = fz_dict_gets(node, "Resources");
+ obj = fz_dict_gets(ctx, node, "Resources");
if (obj)
info.resources = obj;
- obj = fz_dict_gets(node, "MediaBox");
+ obj = fz_dict_gets(ctx, node, "MediaBox");
if (obj)
info.mediabox = obj;
- obj = fz_dict_gets(node, "CropBox");
+ obj = fz_dict_gets(ctx, node, "CropBox");
if (obj)
info.cropbox = obj;
- obj = fz_dict_gets(node, "Rotate");
+ obj = fz_dict_gets(ctx, node, "Rotate");
if (obj)
info.rotate = obj;
- tmp = fz_new_null();
- fz_dict_puts(node, ".seen", tmp);
- fz_drop_obj(tmp);
+ tmp = fz_new_null(ctx);
+ fz_dict_puts(ctx, node, ".seen", tmp);
+ fz_drop_obj(ctx, tmp);
- n = fz_array_len(kids);
+ n = fz_array_len(ctx, kids);
for (i = 0; i < n; i++)
{
- obj = fz_array_get(kids, i);
+ obj = fz_array_get(ctx, kids, i);
pdf_load_page_tree_node(xref, obj, info);
}
- fz_dict_dels(node, ".seen");
+ fz_dict_dels(ctx, node, ".seen");
}
else
{
- dict = fz_resolve_indirect(node);
+ dict = fz_resolve_indirect(ctx, node);
- if (info.resources && !fz_dict_gets(dict, "Resources"))
- fz_dict_puts(dict, "Resources", info.resources);
- if (info.mediabox && !fz_dict_gets(dict, "MediaBox"))
- fz_dict_puts(dict, "MediaBox", info.mediabox);
- if (info.cropbox && !fz_dict_gets(dict, "CropBox"))
- fz_dict_puts(dict, "CropBox", info.cropbox);
- if (info.rotate && !fz_dict_gets(dict, "Rotate"))
- fz_dict_puts(dict, "Rotate", info.rotate);
+ if (info.resources && !fz_dict_gets(ctx, dict, "Resources"))
+ fz_dict_puts(ctx, dict, "Resources", info.resources);
+ if (info.mediabox && !fz_dict_gets(ctx, dict, "MediaBox"))
+ fz_dict_puts(ctx, dict, "MediaBox", info.mediabox);
+ if (info.cropbox && !fz_dict_gets(ctx, dict, "CropBox"))
+ fz_dict_puts(ctx, dict, "CropBox", info.cropbox);
+ if (info.rotate && !fz_dict_gets(ctx, dict, "Rotate"))
+ fz_dict_puts(ctx, dict, "Rotate", info.rotate);
if (xref->page_len == xref->page_cap)
{
fz_warn("found more pages than expected");
xref->page_cap ++;
- xref->page_refs = fz_realloc(xref->page_refs, xref->page_cap, sizeof(fz_obj*));
- xref->page_objs = fz_realloc(xref->page_objs, xref->page_cap, sizeof(fz_obj*));
+ xref->page_refs = fz_realloc(ctx, xref->page_refs, xref->page_cap * sizeof(fz_obj*));
+ xref->page_objs = fz_realloc(ctx, xref->page_objs, xref->page_cap * sizeof(fz_obj*));
}
xref->page_refs[xref->page_len] = fz_keep_obj(node);
@@ -98,19 +99,20 @@ fz_error
pdf_load_page_tree(pdf_xref *xref)
{
struct info info;
- fz_obj *catalog = fz_dict_gets(xref->trailer, "Root");
- fz_obj *pages = fz_dict_gets(catalog, "Pages");
- fz_obj *count = fz_dict_gets(pages, "Count");
+ fz_context *ctx = xref->ctx;
+ fz_obj *catalog = fz_dict_gets(ctx, xref->trailer, "Root");
+ fz_obj *pages = fz_dict_gets(ctx, catalog, "Pages");
+ fz_obj *count = fz_dict_gets(ctx, pages, "Count");
- if (!fz_is_dict(pages))
+ if (!fz_is_dict(ctx, pages))
return fz_error_make("missing page tree");
- if (!fz_is_int(count))
+ if (!fz_is_int(ctx, count))
return fz_error_make("missing page count");
- xref->page_cap = fz_to_int(count);
+ xref->page_cap = fz_to_int(ctx, count);
xref->page_len = 0;
- xref->page_refs = fz_calloc(xref->page_cap, sizeof(fz_obj*));
- xref->page_objs = fz_calloc(xref->page_cap, sizeof(fz_obj*));
+ xref->page_refs = fz_calloc(ctx, xref->page_cap, sizeof(fz_obj*));
+ xref->page_objs = fz_calloc(ctx, xref->page_cap, sizeof(fz_obj*));
info.resources = NULL;
info.mediabox = NULL;
@@ -124,78 +126,77 @@ pdf_load_page_tree(pdf_xref *xref)
/* We need to know whether to install a page-level transparency group */
-static int pdf_resources_use_blending(fz_obj *rdb);
+static int pdf_resources_use_blending(fz_context *ctx, fz_obj *rdb);
static int
-pdf_extgstate_uses_blending(fz_obj *dict)
+pdf_extgstate_uses_blending(fz_context *ctx, fz_obj *dict)
{
- fz_obj *obj = fz_dict_gets(dict, "BM");
- if (fz_is_name(obj) && strcmp(fz_to_name(obj), "Normal"))
+ fz_obj *obj = fz_dict_gets(ctx, dict, "BM");
+ if (fz_is_name(ctx, obj) && strcmp(fz_to_name(ctx, obj), "Normal"))
return 1;
return 0;
}
static int
-pdf_pattern_uses_blending(fz_obj *dict)
+pdf_pattern_uses_blending(fz_context *ctx, fz_obj *dict)
{
fz_obj *obj;
- obj = fz_dict_gets(dict, "Resources");
- if (pdf_resources_use_blending(obj))
+ obj = fz_dict_gets(ctx, dict, "Resources");
+ if (pdf_resources_use_blending(ctx, obj))
return 1;
- obj = fz_dict_gets(dict, "ExtGState");
- if (pdf_extgstate_uses_blending(obj))
- return 1;
- return 0;
+ obj = fz_dict_gets(ctx, dict, "ExtGState");
+ return pdf_extgstate_uses_blending(ctx, obj);
}
static int
-pdf_xobject_uses_blending(fz_obj *dict)
+pdf_xobject_uses_blending(fz_context *ctx, fz_obj *dict)
{
- fz_obj *obj = fz_dict_gets(dict, "Resources");
- if (pdf_resources_use_blending(obj))
- return 1;
- return 0;
+ fz_obj *obj = fz_dict_gets(ctx, dict, "Resources");
+ return pdf_resources_use_blending(ctx, obj);
}
static int
-pdf_resources_use_blending(fz_obj *rdb)
+pdf_resources_use_blending(fz_context *ctx, fz_obj *rdb)
{
fz_obj *dict;
fz_obj *tmp;
- int i;
+ int i, n;
if (!rdb)
return 0;
/* stop on cyclic resource dependencies */
- if (fz_dict_gets(rdb, ".useBM"))
- return fz_to_bool(fz_dict_gets(rdb, ".useBM"));
+ if (fz_dict_gets(ctx, rdb, ".useBM"))
+ return fz_to_bool(ctx, fz_dict_gets(ctx, rdb, ".useBM"));
- tmp = fz_new_bool(0);
- fz_dict_puts(rdb, ".useBM", tmp);
- fz_drop_obj(tmp);
+ tmp = fz_new_bool(ctx, 0);
+ fz_dict_puts(ctx, rdb, ".useBM", tmp);
+ fz_drop_obj(ctx, tmp);
- dict = fz_dict_gets(rdb, "ExtGState");
- for (i = 0; i < fz_dict_len(dict); i++)
- if (pdf_extgstate_uses_blending(fz_dict_get_val(dict, i)))
+ dict = fz_dict_gets(ctx, rdb, "ExtGState");
+ n = fz_dict_len(ctx, dict);
+ for (i = 0; i < n; i++)
+ if (pdf_extgstate_uses_blending(ctx, fz_dict_get_val(ctx, dict, i)))
goto found;
- dict = fz_dict_gets(rdb, "Pattern");
- for (i = 0; i < fz_dict_len(dict); i++)
- if (pdf_pattern_uses_blending(fz_dict_get_val(dict, i)))
+ dict = fz_dict_gets(ctx, rdb, "Pattern");
+ n = fz_dict_len(ctx, dict);
+ for (i = 0; i < n; i++)
+ if (pdf_pattern_uses_blending(ctx, fz_dict_get_val(ctx, dict, i)))
goto found;
- dict = fz_dict_gets(rdb, "XObject");
- for (i = 0; i < fz_dict_len(dict); i++)
- if (pdf_xobject_uses_blending(fz_dict_get_val(dict, i)))
+ dict = fz_dict_gets(ctx, rdb, "XObject");
+ n = fz_dict_len(ctx, dict);
+ for (i = 0; i < n; i++)
+ if (pdf_xobject_uses_blending(ctx, fz_dict_get_val(ctx, dict, i)))
goto found;
return 0;
found:
- tmp = fz_new_bool(1);
- fz_dict_puts(rdb, ".useBM", tmp);
- fz_drop_obj(tmp);
+ tmp = fz_new_bool(ctx, 1);
+ fz_dict_puts(ctx, rdb, ".useBM", tmp);
+ fz_drop_obj(ctx, tmp);
return 1;
}
@@ -208,13 +209,14 @@ pdf_load_page_contents_array(fz_buffer **bigbufp, pdf_xref *xref, fz_obj *list)
fz_buffer *big;
fz_buffer *one;
int i, n;
+ fz_context *ctx = xref->ctx;
- big = fz_new_buffer(32 * 1024);
+ big = fz_new_buffer(ctx, 32 * 1024);
- n = fz_array_len(list);
+ n = fz_array_len(ctx, list);
for (i = 0; i < n; i++)
{
- fz_obj *stm = fz_array_get(list, i);
+ fz_obj *stm = fz_array_get(ctx, list, i);
error = pdf_load_stream(&one, xref, fz_to_num(stm), fz_to_gen(stm));
if (error)
{
@@ -223,17 +225,17 @@ pdf_load_page_contents_array(fz_buffer **bigbufp, pdf_xref *xref, fz_obj *list)
}
if (big->len + one->len + 1 > big->cap)
- fz_resize_buffer(big, big->len + one->len + 1);
+ fz_resize_buffer(ctx, big, big->len + one->len + 1);
memcpy(big->data + big->len, one->data, one->len);
big->data[big->len + one->len] = ' ';
big->len += one->len + 1;
- fz_drop_buffer(one);
+ fz_drop_buffer(ctx, one);
}
if (n > 0 && big->len == 0)
{
- fz_drop_buffer(big);
+ fz_drop_buffer(ctx, big);
return fz_error_make("cannot load content stream");
}
@@ -245,8 +247,9 @@ static fz_error
pdf_load_page_contents(fz_buffer **bufp, pdf_xref *xref, fz_obj *obj)
{
fz_error error;
+ fz_context *ctx = xref->ctx;
- if (fz_is_array(obj))
+ if (fz_is_array(ctx, obj))
{
error = pdf_load_page_contents_array(bufp, xref, obj);
if (error)
@@ -261,7 +264,7 @@ pdf_load_page_contents(fz_buffer **bufp, pdf_xref *xref, fz_obj *obj)
else
{
fz_warn("page contents missing, leaving page blank");
- *bufp = fz_new_buffer(0);
+ *bufp = fz_new_buffer(ctx, 0);
}
return fz_okay;
@@ -276,27 +279,28 @@ pdf_load_page(pdf_page **pagep, pdf_xref *xref, int number)
fz_obj *pageobj, *pageref;
fz_obj *obj;
fz_bbox bbox;
+ fz_context *ctx = xref->ctx;
if (number < 0 || number >= xref->page_len)
return fz_error_make("cannot find page %d", number + 1);
/* Ensure that we have a store for resource objects */
if (!xref->store)
- xref->store = pdf_new_store();
+ xref->store = pdf_new_store(ctx);
pageobj = xref->page_objs[number];
pageref = xref->page_refs[number];
- page = fz_malloc(sizeof(pdf_page));
+ page = fz_malloc(ctx, sizeof(pdf_page));
page->resources = NULL;
page->contents = NULL;
page->transparency = 0;
page->links = NULL;
page->annots = NULL;
- obj = fz_dict_gets(pageobj, "MediaBox");
- bbox = fz_round_rect(pdf_to_rect(obj));
- if (fz_is_empty_rect(pdf_to_rect(obj)))
+ obj = fz_dict_gets(ctx, pageobj, "MediaBox");
+ bbox = fz_round_rect(pdf_to_rect(ctx, obj));
+ if (fz_is_empty_rect(pdf_to_rect(ctx, obj)))
{
fz_warn("cannot find page size for page %d", number + 1);
bbox.x0 = 0;
@@ -305,10 +309,10 @@ pdf_load_page(pdf_page **pagep, pdf_xref *xref, int number)
bbox.y1 = 792;
}
- obj = fz_dict_gets(pageobj, "CropBox");
- if (fz_is_array(obj))
+ obj = fz_dict_gets(ctx, pageobj, "CropBox");
+ if (fz_is_array(ctx, obj))
{
- fz_bbox cropbox = fz_round_rect(pdf_to_rect(obj));
+ fz_bbox cropbox = fz_round_rect(pdf_to_rect(ctx, obj));
bbox = fz_intersect_bbox(bbox, cropbox);
}
@@ -323,32 +327,32 @@ pdf_load_page(pdf_page **pagep, pdf_xref *xref, int number)
page->mediabox = fz_unit_rect;
}
- page->rotate = fz_to_int(fz_dict_gets(pageobj, "Rotate"));
+ page->rotate = fz_to_int(ctx, fz_dict_gets(ctx, pageobj, "Rotate"));
- obj = fz_dict_gets(pageobj, "Annots");
+ obj = fz_dict_gets(ctx, pageobj, "Annots");
if (obj)
{
pdf_load_links(&page->links, xref, obj);
pdf_load_annots(&page->annots, xref, obj);
}
- page->resources = fz_dict_gets(pageobj, "Resources");
+ page->resources = fz_dict_gets(ctx, pageobj, "Resources");
if (page->resources)
fz_keep_obj(page->resources);
- obj = fz_dict_gets(pageobj, "Contents");
+ obj = fz_dict_gets(ctx, pageobj, "Contents");
error = pdf_load_page_contents(&page->contents, xref, obj);
if (error)
{
- pdf_free_page(page);
+ pdf_free_page(ctx, page);
return fz_error_note(error, "cannot load page %d contents (%d 0 R)", number + 1, fz_to_num(pageref));
}
- if (pdf_resources_use_blending(page->resources))
+ if (pdf_resources_use_blending(ctx, page->resources))
page->transparency = 1;
for (annot = page->annots; annot && !page->transparency; annot = annot->next)
- if (pdf_resources_use_blending(annot->ap->resources))
+ if (pdf_resources_use_blending(ctx, annot->ap->resources))
page->transparency = 1;
*pagep = page;
@@ -356,15 +360,15 @@ pdf_load_page(pdf_page **pagep, pdf_xref *xref, int number)
}
void
-pdf_free_page(pdf_page *page)
+pdf_free_page(fz_context *ctx, pdf_page *page)
{
if (page->resources)
- fz_drop_obj(page->resources);
+ fz_drop_obj(ctx, page->resources);
if (page->contents)
- fz_drop_buffer(page->contents);
+ fz_drop_buffer(ctx, page->contents);
if (page->links)
- pdf_free_link(page->links);
+ pdf_free_link(ctx, page->links);
if (page->annots)
- pdf_free_annot(page->annots);
- fz_free(page);
+ pdf_free_annot(ctx, page->annots);
+ fz_free(ctx, page);
}
diff --git a/pdf/pdf_parse.c b/pdf/pdf_parse.c
index abbac2c6..e01734d7 100644
--- a/pdf/pdf_parse.c
+++ b/pdf/pdf_parse.c
@@ -2,13 +2,13 @@
#include "mupdf.h"
fz_rect
-pdf_to_rect(fz_obj *array)
+pdf_to_rect(fz_context *ctx, fz_obj *array)
{
fz_rect r;
- float a = fz_to_real(fz_array_get(array, 0));
- float b = fz_to_real(fz_array_get(array, 1));
- float c = fz_to_real(fz_array_get(array, 2));
- float d = fz_to_real(fz_array_get(array, 3));
+ float a = fz_to_real(ctx, fz_array_get(ctx, array, 0));
+ float b = fz_to_real(ctx, fz_array_get(ctx, array, 1));
+ float c = fz_to_real(ctx, fz_array_get(ctx, array, 2));
+ float d = fz_to_real(ctx, fz_array_get(ctx, array, 3));
r.x0 = MIN(a, c);
r.y0 = MIN(b, d);
r.x1 = MAX(a, c);
@@ -17,25 +17,25 @@ pdf_to_rect(fz_obj *array)
}
fz_matrix
-pdf_to_matrix(fz_obj *array)
+pdf_to_matrix(fz_context *ctx, fz_obj *array)
{
fz_matrix m;
- m.a = fz_to_real(fz_array_get(array, 0));
- m.b = fz_to_real(fz_array_get(array, 1));
- m.c = fz_to_real(fz_array_get(array, 2));
- m.d = fz_to_real(fz_array_get(array, 3));
- m.e = fz_to_real(fz_array_get(array, 4));
- m.f = fz_to_real(fz_array_get(array, 5));
+ m.a = fz_to_real(ctx, fz_array_get(ctx, array, 0));
+ m.b = fz_to_real(ctx, fz_array_get(ctx, array, 1));
+ m.c = fz_to_real(ctx, fz_array_get(ctx, array, 2));
+ m.d = fz_to_real(ctx, fz_array_get(ctx, array, 3));
+ m.e = fz_to_real(ctx, fz_array_get(ctx, array, 4));
+ m.f = fz_to_real(ctx, fz_array_get(ctx, array, 5));
return m;
}
/* Convert Unicode/PdfDocEncoding string into utf-8 */
char *
-pdf_to_utf8(fz_obj *src)
+pdf_to_utf8(fz_context *ctx, fz_obj *src)
{
- unsigned char *srcptr = (unsigned char *) fz_to_str_buf(src);
+ unsigned char *srcptr = (unsigned char *) fz_to_str_buf(ctx, src);
char *dstptr, *dst;
- int srclen = fz_to_str_len(src);
+ int srclen = fz_to_str_len(ctx, src);
int dstlen = 0;
int ucs;
int i;
@@ -48,7 +48,7 @@ pdf_to_utf8(fz_obj *src)
dstlen += runelen(ucs);
}
- dstptr = dst = fz_malloc(dstlen + 1);
+ dstptr = dst = fz_malloc(ctx, dstlen + 1);
for (i = 2; i < srclen; i += 2)
{
@@ -64,7 +64,7 @@ pdf_to_utf8(fz_obj *src)
dstlen += runelen(ucs);
}
- dstptr = dst = fz_malloc(dstlen + 1);
+ dstptr = dst = fz_malloc(ctx, dstlen + 1);
for (i = 2; i + 1 < srclen; i += 2)
{
@@ -77,7 +77,7 @@ pdf_to_utf8(fz_obj *src)
for (i = 0; i < srclen; i++)
dstlen += runelen(pdf_doc_encoding[srcptr[i]]);
- dstptr = dst = fz_malloc(dstlen + 1);
+ dstptr = dst = fz_malloc(ctx, dstlen + 1);
for (i = 0; i < srclen; i++)
{
@@ -92,28 +92,28 @@ pdf_to_utf8(fz_obj *src)
/* Convert Unicode/PdfDocEncoding string into ucs-2 */
unsigned short *
-pdf_to_ucs2(fz_obj *src)
+pdf_to_ucs2(fz_context *ctx, fz_obj *src)
{
- unsigned char *srcptr = (unsigned char *) fz_to_str_buf(src);
+ unsigned char *srcptr = (unsigned char *) fz_to_str_buf(ctx, src);
unsigned short *dstptr, *dst;
- int srclen = fz_to_str_len(src);
+ int srclen = fz_to_str_len(ctx, src);
int i;
if (srclen >= 2 && srcptr[0] == 254 && srcptr[1] == 255)
{
- dstptr = dst = fz_calloc((srclen - 2) / 2 + 1, sizeof(short));
+ dstptr = dst = fz_calloc(ctx, (srclen - 2) / 2 + 1, sizeof(short));
for (i = 2; i + 1 < srclen; i += 2)
*dstptr++ = srcptr[i] << 8 | srcptr[i+1];
}
else if (srclen >= 2 && srcptr[0] == 255 && srcptr[1] == 254)
{
- dstptr = dst = fz_calloc((srclen - 2) / 2 + 1, sizeof(short));
+ dstptr = dst = fz_calloc(ctx, (srclen - 2) / 2 + 1, sizeof(short));
for (i = 2; i + 1 < srclen; i += 2)
*dstptr++ = srcptr[i] | srcptr[i+1] << 8;
}
else
{
- dstptr = dst = fz_calloc(srclen + 1, sizeof(short));
+ dstptr = dst = fz_calloc(ctx, srclen + 1, sizeof(short));
for (i = 0; i < srclen; i++)
*dstptr++ = pdf_doc_encoding[srcptr[i]];
}
@@ -124,7 +124,7 @@ pdf_to_ucs2(fz_obj *src)
/* Convert UCS-2 string into PdfDocEncoding for authentication */
char *
-pdf_from_ucs2(unsigned short *src)
+pdf_from_ucs2(fz_context *ctx, unsigned short *src)
{
int i, j, len;
char *docstr;
@@ -133,7 +133,7 @@ pdf_from_ucs2(unsigned short *src)
while (src[len])
len++;
- docstr = fz_malloc(len + 1);
+ docstr = fz_malloc(ctx, len + 1);
for (i = 0; i < len; i++)
{
@@ -152,7 +152,7 @@ pdf_from_ucs2(unsigned short *src)
/* fail, if a character can't be encoded */
if (!docstr[i])
{
- fz_free(docstr);
+ fz_free(ctx, docstr);
return NULL;
}
}
@@ -162,11 +162,11 @@ pdf_from_ucs2(unsigned short *src)
}
fz_obj *
-pdf_to_utf8_name(fz_obj *src)
+pdf_to_utf8_name(fz_context *ctx, fz_obj *src)
{
- char *buf = pdf_to_utf8(src);
- fz_obj *dst = fz_new_name(buf);
- fz_free(buf);
+ char *buf = pdf_to_utf8(ctx, src);
+ fz_obj *dst = fz_new_name(ctx, buf);
+ fz_free(ctx, buf);
return dst;
}
@@ -179,15 +179,16 @@ pdf_parse_array(fz_obj **op, pdf_xref *xref, fz_stream *file, char *buf, int cap
int a = 0, b = 0, n = 0;
int tok;
int len;
+ fz_context *ctx = file->ctx;
- ary = fz_new_array(4);
+ ary = fz_new_array(ctx, 4);
while (1)
{
error = pdf_lex(&tok, file, buf, cap, &len);
if (error)
{
- fz_drop_obj(ary);
+ fz_drop_obj(ctx, ary);
return fz_error_note(error, "cannot parse array");
}
@@ -195,24 +196,24 @@ pdf_parse_array(fz_obj **op, pdf_xref *xref, fz_stream *file, char *buf, int cap
{
if (n > 0)
{
- obj = fz_new_int(a);
- fz_array_push(ary, obj);
- fz_drop_obj(obj);
+ obj = fz_new_int(ctx, a);
+ fz_array_push(ctx, ary, obj);
+ fz_drop_obj(ctx, obj);
}
if (n > 1)
{
- obj = fz_new_int(b);
- fz_array_push(ary, obj);
- fz_drop_obj(obj);
+ obj = fz_new_int(ctx, b);
+ fz_array_push(ctx, ary, obj);
+ fz_drop_obj(ctx, obj);
}
n = 0;
}
if (tok == PDF_TOK_INT && n == 2)
{
- obj = fz_new_int(a);
- fz_array_push(ary, obj);
- fz_drop_obj(obj);
+ obj = fz_new_int(ctx, a);
+ fz_array_push(ctx, ary, obj);
+ fz_drop_obj(ctx, obj);
a = b;
n --;
}
@@ -234,12 +235,12 @@ pdf_parse_array(fz_obj **op, pdf_xref *xref, fz_stream *file, char *buf, int cap
case PDF_TOK_R:
if (n != 2)
{
- fz_drop_obj(ary);
+ fz_drop_obj(ctx, ary);
return fz_error_make("cannot parse indirect reference in array");
}
- obj = fz_new_indirect(a, b, xref);
- fz_array_push(ary, obj);
- fz_drop_obj(obj);
+ obj = fz_new_indirect(ctx, a, b, xref);
+ fz_array_push(ctx, ary, obj);
+ fz_drop_obj(ctx, obj);
n = 0;
break;
@@ -247,57 +248,57 @@ pdf_parse_array(fz_obj **op, pdf_xref *xref, fz_stream *file, char *buf, int cap
error = pdf_parse_array(&obj, xref, file, buf, cap);
if (error)
{
- fz_drop_obj(ary);
+ fz_drop_obj(ctx, ary);
return fz_error_note(error, "cannot parse array");
}
- fz_array_push(ary, obj);
- fz_drop_obj(obj);
+ fz_array_push(ctx, ary, obj);
+ fz_drop_obj(ctx, obj);
break;
case PDF_TOK_OPEN_DICT:
error = pdf_parse_dict(&obj, xref, file, buf, cap);
if (error)
{
- fz_drop_obj(ary);
+ fz_drop_obj(ctx, ary);
return fz_error_note(error, "cannot parse array");
}
- fz_array_push(ary, obj);
- fz_drop_obj(obj);
+ fz_array_push(ctx, ary, obj);
+ fz_drop_obj(ctx, obj);
break;
case PDF_TOK_NAME:
- obj = fz_new_name(buf);
- fz_array_push(ary, obj);
- fz_drop_obj(obj);
+ obj = fz_new_name(ctx, buf);
+ fz_array_push(ctx, ary, obj);
+ fz_drop_obj(ctx, obj);
break;
case PDF_TOK_REAL:
- obj = fz_new_real(fz_atof(buf));
- fz_array_push(ary, obj);
- fz_drop_obj(obj);
+ obj = fz_new_real(ctx, fz_atof(buf));
+ fz_array_push(ctx, ary, obj);
+ fz_drop_obj(ctx, obj);
break;
case PDF_TOK_STRING:
- obj = fz_new_string(buf, len);
- fz_array_push(ary, obj);
- fz_drop_obj(obj);
+ obj = fz_new_string(ctx, buf, len);
+ fz_array_push(ctx, ary, obj);
+ fz_drop_obj(ctx, obj);
break;
case PDF_TOK_TRUE:
- obj = fz_new_bool(1);
- fz_array_push(ary, obj);
- fz_drop_obj(obj);
+ obj = fz_new_bool(ctx, 1);
+ fz_array_push(ctx, ary, obj);
+ fz_drop_obj(ctx, obj);
break;
case PDF_TOK_FALSE:
- obj = fz_new_bool(0);
- fz_array_push(ary, obj);
- fz_drop_obj(obj);
+ obj = fz_new_bool(ctx, 0);
+ fz_array_push(ctx, ary, obj);
+ fz_drop_obj(ctx, obj);
break;
case PDF_TOK_NULL:
- obj = fz_new_null();
- fz_array_push(ary, obj);
- fz_drop_obj(obj);
+ obj = fz_new_null(ctx);
+ fz_array_push(ctx, ary, obj);
+ fz_drop_obj(ctx, obj);
break;
default:
- fz_drop_obj(ary);
+ fz_drop_obj(ctx, ary);
return fz_error_make("cannot parse token in array");
}
}
@@ -313,15 +314,16 @@ pdf_parse_dict(fz_obj **op, pdf_xref *xref, fz_stream *file, char *buf, int cap)
int tok;
int len;
int a, b;
+ fz_context *ctx = file->ctx;
- dict = fz_new_dict(8);
+ dict = fz_new_dict(ctx, 8);
while (1)
{
error = pdf_lex(&tok, file, buf, cap, &len);
if (error)
{
- fz_drop_obj(dict);
+ fz_drop_obj(ctx, dict);
return fz_error_note(error, "cannot parse dict");
}
@@ -341,17 +343,17 @@ skip:
if (tok != PDF_TOK_NAME)
{
- fz_drop_obj(dict);
+ fz_drop_obj(ctx, dict);
return fz_error_make("invalid key in dict");
}
- key = fz_new_name(buf);
+ key = fz_new_name(ctx, buf);
error = pdf_lex(&tok, file, buf, cap, &len);
if (error)
{
- fz_drop_obj(key);
- fz_drop_obj(dict);
+ fz_drop_obj(ctx, key);
+ fz_drop_obj(ctx, dict);
return fz_error_note(error, "cannot parse dict");
}
@@ -361,8 +363,8 @@ skip:
error = pdf_parse_array(&val, xref, file, buf, cap);
if (error)
{
- fz_drop_obj(key);
- fz_drop_obj(dict);
+ fz_drop_obj(ctx, key);
+ fz_drop_obj(ctx, dict);
return fz_error_note(error, "cannot parse dict");
}
break;
@@ -371,18 +373,18 @@ skip:
error = pdf_parse_dict(&val, xref, file, buf, cap);
if (error)
{
- fz_drop_obj(key);
- fz_drop_obj(dict);
+ fz_drop_obj(ctx, key);
+ fz_drop_obj(ctx, dict);
return fz_error_note(error, "cannot parse dict");
}
break;
- case PDF_TOK_NAME: val = fz_new_name(buf); break;
- case PDF_TOK_REAL: val = fz_new_real(fz_atof(buf)); break;
- case PDF_TOK_STRING: val = fz_new_string(buf, len); break;
- case PDF_TOK_TRUE: val = fz_new_bool(1); break;
- case PDF_TOK_FALSE: val = fz_new_bool(0); break;
- case PDF_TOK_NULL: val = fz_new_null(); break;
+ case PDF_TOK_NAME: val = fz_new_name(ctx, buf); break;
+ case PDF_TOK_REAL: val = fz_new_real(ctx, fz_atof(buf)); break;
+ case PDF_TOK_STRING: val = fz_new_string(ctx, buf, len); break;
+ case PDF_TOK_TRUE: val = fz_new_bool(ctx, 1); break;
+ case PDF_TOK_FALSE: val = fz_new_bool(ctx, 0); break;
+ case PDF_TOK_NULL: val = fz_new_null(ctx); break;
case PDF_TOK_INT:
/* 64-bit to allow for numbers > INT_MAX and overflow */
@@ -390,17 +392,17 @@ skip:
error = pdf_lex(&tok, file, buf, cap, &len);
if (error)
{
- fz_drop_obj(key);
- fz_drop_obj(dict);
+ fz_drop_obj(ctx, key);
+ fz_drop_obj(ctx, dict);
return fz_error_note(error, "cannot parse dict");
}
if (tok == PDF_TOK_CLOSE_DICT || tok == PDF_TOK_NAME ||
(tok == PDF_TOK_KEYWORD && !strcmp(buf, "ID")))
{
- val = fz_new_int(a);
- fz_dict_put(dict, key, val);
- fz_drop_obj(val);
- fz_drop_obj(key);
+ val = fz_new_int(ctx, a);
+ fz_dict_put(ctx, dict, key, val);
+ fz_drop_obj(ctx, val);
+ fz_drop_obj(ctx, key);
goto skip;
}
if (tok == PDF_TOK_INT)
@@ -409,29 +411,29 @@ skip:
error = pdf_lex(&tok, file, buf, cap, &len);
if (error)
{
- fz_drop_obj(key);
- fz_drop_obj(dict);
+ fz_drop_obj(ctx, key);
+ fz_drop_obj(ctx, dict);
return fz_error_note(error, "cannot parse dict");
}
if (tok == PDF_TOK_R)
{
- val = fz_new_indirect(a, b, xref);
+ val = fz_new_indirect(ctx, a, b, xref);
break;
}
}
- fz_drop_obj(key);
- fz_drop_obj(dict);
+ fz_drop_obj(ctx, key);
+ fz_drop_obj(ctx, dict);
return fz_error_make("invalid indirect reference in dict");
default:
- fz_drop_obj(key);
- fz_drop_obj(dict);
+ fz_drop_obj(ctx, key);
+ fz_drop_obj(ctx, dict);
return fz_error_make("unknown token in dict");
}
- fz_dict_put(dict, key, val);
- fz_drop_obj(val);
- fz_drop_obj(key);
+ fz_dict_put(ctx, dict, key, val);
+ fz_drop_obj(ctx, val);
+ fz_drop_obj(ctx, key);
}
}
@@ -441,6 +443,7 @@ pdf_parse_stm_obj(fz_obj **op, pdf_xref *xref, fz_stream *file, char *buf, int c
fz_error error;
int tok;
int len;
+ fz_context *ctx = file->ctx;
error = pdf_lex(&tok, file, buf, cap, &len);
if (error)
@@ -458,13 +461,13 @@ pdf_parse_stm_obj(fz_obj **op, pdf_xref *xref, fz_stream *file, char *buf, int c
if (error)
return fz_error_note(error, "cannot parse object stream");
break;
- case PDF_TOK_NAME: *op = fz_new_name(buf); break;
- case PDF_TOK_REAL: *op = fz_new_real(fz_atof(buf)); break;
- case PDF_TOK_STRING: *op = fz_new_string(buf, len); break;
- case PDF_TOK_TRUE: *op = fz_new_bool(1); break;
- case PDF_TOK_FALSE: *op = fz_new_bool(0); break;
- case PDF_TOK_NULL: *op = fz_new_null(); break;
- case PDF_TOK_INT: *op = fz_new_int(atoi(buf)); break;
+ case PDF_TOK_NAME: *op = fz_new_name(ctx, buf); break;
+ case PDF_TOK_REAL: *op = fz_new_real(ctx, fz_atof(buf)); break;
+ case PDF_TOK_STRING: *op = fz_new_string(ctx, buf, len); break;
+ case PDF_TOK_TRUE: *op = fz_new_bool(ctx, 1); break;
+ case PDF_TOK_FALSE: *op = fz_new_bool(ctx, 0); break;
+ case PDF_TOK_NULL: *op = fz_new_null(ctx); break;
+ case PDF_TOK_INT: *op = fz_new_int(ctx, atoi(buf)); break;
default: return fz_error_make("unknown token in object stream");
}
@@ -482,6 +485,7 @@ pdf_parse_ind_obj(fz_obj **op, pdf_xref *xref,
int tok;
int len;
int a, b;
+ fz_context *ctx = file->ctx;
error = pdf_lex(&tok, file, buf, cap, &len);
if (error)
@@ -521,12 +525,12 @@ pdf_parse_ind_obj(fz_obj **op, pdf_xref *xref,
return fz_error_note(error, "cannot parse indirect object (%d %d R)", num, gen);
break;
- case PDF_TOK_NAME: obj = fz_new_name(buf); break;
- case PDF_TOK_REAL: obj = fz_new_real(fz_atof(buf)); break;
- case PDF_TOK_STRING: obj = fz_new_string(buf, len); break;
- case PDF_TOK_TRUE: obj = fz_new_bool(1); break;
- case PDF_TOK_FALSE: obj = fz_new_bool(0); break;
- case PDF_TOK_NULL: obj = fz_new_null(); break;
+ case PDF_TOK_NAME: obj = fz_new_name(ctx, buf); break;
+ case PDF_TOK_REAL: obj = fz_new_real(ctx, fz_atof(buf)); break;
+ case PDF_TOK_STRING: obj = fz_new_string(ctx, buf, len); break;
+ case PDF_TOK_TRUE: obj = fz_new_bool(ctx, 1); break;
+ case PDF_TOK_FALSE: obj = fz_new_bool(ctx, 0); break;
+ case PDF_TOK_NULL: obj = fz_new_null(ctx); break;
case PDF_TOK_INT:
a = atoi(buf);
@@ -535,7 +539,7 @@ pdf_parse_ind_obj(fz_obj **op, pdf_xref *xref,
return fz_error_note(error, "cannot parse indirect object (%d %d R)", num, gen);
if (tok == PDF_TOK_STREAM || tok == PDF_TOK_ENDOBJ)
{
- obj = fz_new_int(a);
+ obj = fz_new_int(ctx, a);
goto skip;
}
if (tok == PDF_TOK_INT)
@@ -546,14 +550,14 @@ pdf_parse_ind_obj(fz_obj **op, pdf_xref *xref,
return fz_error_note(error, "cannot parse indirect object (%d %d R)", num, gen);
if (tok == PDF_TOK_R)
{
- obj = fz_new_indirect(a, b, xref);
+ obj = fz_new_indirect(ctx, a, b, xref);
break;
}
}
return fz_error_make("expected 'R' keyword (%d %d R)", num, gen);
case PDF_TOK_ENDOBJ:
- obj = fz_new_null();
+ obj = fz_new_null(ctx);
goto skip;
default:
@@ -563,7 +567,7 @@ pdf_parse_ind_obj(fz_obj **op, pdf_xref *xref,
error = pdf_lex(&tok, file, buf, cap, &len);
if (error)
{
- fz_drop_obj(obj);
+ fz_drop_obj(ctx, obj);
return fz_error_note(error, "cannot parse indirect object (%d %d R)", num, gen);
}
diff --git a/pdf/pdf_pattern.c b/pdf/pdf_pattern.c
index 418062d6..d058b2b5 100644
--- a/pdf/pdf_pattern.c
+++ b/pdf/pdf_pattern.c
@@ -7,43 +7,44 @@ pdf_load_pattern(pdf_pattern **patp, pdf_xref *xref, fz_obj *dict)
fz_error error;
pdf_pattern *pat;
fz_obj *obj;
+ fz_context *ctx = xref->ctx;
- if ((*patp = pdf_find_item(xref->store, pdf_drop_pattern, dict)))
+ if ((*patp = pdf_find_item(ctx, xref->store, (pdf_store_drop_fn *)pdf_drop_pattern, dict)))
{
pdf_keep_pattern(*patp);
return fz_okay;
}
- pat = fz_malloc(sizeof(pdf_pattern));
+ pat = fz_malloc(ctx, sizeof(pdf_pattern));
pat->refs = 1;
pat->resources = NULL;
pat->contents = NULL;
/* Store pattern now, to avoid possible recursion if objects refer back to this one */
- pdf_store_item(xref->store, pdf_keep_pattern, pdf_drop_pattern, dict, pat);
+ pdf_store_item(ctx, xref->store, (pdf_store_keep_fn *)pdf_keep_pattern, (pdf_store_drop_fn *)pdf_drop_pattern, dict, pat);
- pat->ismask = fz_to_int(fz_dict_gets(dict, "PaintType")) == 2;
- pat->xstep = fz_to_real(fz_dict_gets(dict, "XStep"));
- pat->ystep = fz_to_real(fz_dict_gets(dict, "YStep"));
+ pat->ismask = fz_to_int(ctx, fz_dict_gets(ctx, dict, "PaintType")) == 2;
+ pat->xstep = fz_to_real(ctx, fz_dict_gets(ctx, dict, "XStep"));
+ pat->ystep = fz_to_real(ctx, fz_dict_gets(ctx, dict, "YStep"));
- obj = fz_dict_gets(dict, "BBox");
- pat->bbox = pdf_to_rect(obj);
+ obj = fz_dict_gets(ctx, dict, "BBox");
+ pat->bbox = pdf_to_rect(ctx, obj);
- obj = fz_dict_gets(dict, "Matrix");
+ obj = fz_dict_gets(ctx, dict, "Matrix");
if (obj)
- pat->matrix = pdf_to_matrix(obj);
+ pat->matrix = pdf_to_matrix(ctx, obj);
else
pat->matrix = fz_identity;
- pat->resources = fz_dict_gets(dict, "Resources");
+ pat->resources = fz_dict_gets(ctx, dict, "Resources");
if (pat->resources)
fz_keep_obj(pat->resources);
error = pdf_load_stream(&pat->contents, xref, fz_to_num(dict), fz_to_gen(dict));
if (error)
{
- pdf_remove_item(xref->store, pdf_drop_pattern, dict);
- pdf_drop_pattern(pat);
+ pdf_remove_item(ctx, xref->store, (pdf_store_drop_fn *)pdf_drop_pattern, dict);
+ pdf_drop_pattern(ctx, pat);
return fz_error_note(error, "cannot load pattern stream (%d %d R)", fz_to_num(dict), fz_to_gen(dict));
}
@@ -59,14 +60,14 @@ pdf_keep_pattern(pdf_pattern *pat)
}
void
-pdf_drop_pattern(pdf_pattern *pat)
+pdf_drop_pattern(fz_context *ctx, pdf_pattern *pat)
{
if (pat && --pat->refs == 0)
{
if (pat->resources)
- fz_drop_obj(pat->resources);
+ fz_drop_obj(ctx, pat->resources);
if (pat->contents)
- fz_drop_buffer(pat->contents);
- fz_free(pat);
+ fz_drop_buffer(ctx, pat->contents);
+ fz_free(ctx, pat);
}
}
diff --git a/pdf/pdf_repair.c b/pdf/pdf_repair.c
index 3b7d2a3d..e31822af 100644
--- a/pdf/pdf_repair.c
+++ b/pdf/pdf_repair.c
@@ -20,6 +20,7 @@ pdf_repair_obj(fz_stream *file, char *buf, int cap, int *stmofsp, int *stmlenp,
int stm_len;
int len;
int n;
+ fz_context *ctx = file->ctx;
*stmofsp = 0;
*stmlenp = -1;
@@ -38,31 +39,31 @@ pdf_repair_obj(fz_stream *file, char *buf, int cap, int *stmofsp, int *stmlenp,
if (error)
return fz_error_note(error, "cannot parse object");
- obj = fz_dict_gets(dict, "Type");
- if (fz_is_name(obj) && !strcmp(fz_to_name(obj), "XRef"))
+ obj = fz_dict_gets(ctx, dict, "Type");
+ if (fz_is_name(ctx, obj) && !strcmp(fz_to_name(ctx, obj), "XRef"))
{
- obj = fz_dict_gets(dict, "Encrypt");
+ obj = fz_dict_gets(ctx, dict, "Encrypt");
if (obj)
{
if (*encrypt)
- fz_drop_obj(*encrypt);
+ fz_drop_obj(ctx, *encrypt);
*encrypt = fz_keep_obj(obj);
}
- obj = fz_dict_gets(dict, "ID");
+ obj = fz_dict_gets(ctx, dict, "ID");
if (obj)
{
if (*id)
- fz_drop_obj(*id);
+ fz_drop_obj(ctx, *id);
*id = fz_keep_obj(obj);
}
}
- obj = fz_dict_gets(dict, "Length");
- if (fz_is_int(obj))
- stm_len = fz_to_int(obj);
+ obj = fz_dict_gets(ctx, dict, "Length");
+ if (fz_is_int(ctx, obj))
+ stm_len = fz_to_int(ctx, obj);
- fz_drop_obj(dict);
+ fz_drop_obj(ctx, dict);
}
while ( tok != PDF_TOK_STREAM &&
@@ -134,14 +135,15 @@ pdf_repair_obj_stm(pdf_xref *xref, int num, int gen)
int tok;
int i, n, count;
char buf[256];
+ fz_context *ctx = xref->ctx;
error = pdf_load_object(&obj, xref, num, gen);
if (error)
return fz_error_note(error, "cannot load object stream object (%d %d R)", num, gen);
- count = fz_to_int(fz_dict_gets(obj, "N"));
+ count = fz_to_int(ctx, fz_dict_gets(ctx, obj, "N"));
- fz_drop_obj(obj);
+ fz_drop_obj(ctx, obj);
error = pdf_open_stream(&stm, xref, num, gen);
if (error)
@@ -202,12 +204,13 @@ pdf_repair_xref(pdf_xref *xref, char *buf, int bufsize)
int tok;
int next;
int i, n, c;
+ fz_context *ctx = xref->ctx;
fz_seek(xref->file, 0, 0);
listlen = 0;
listcap = 1024;
- list = fz_calloc(listcap, sizeof(struct entry));
+ list = fz_calloc(ctx, listcap, sizeof(struct entry));
/* look for '%PDF' version marker within first kilobyte of file */
n = fz_read(xref->file, (unsigned char *)buf, MAX(bufsize, 1024));
@@ -270,7 +273,7 @@ pdf_repair_xref(pdf_xref *xref, char *buf, int bufsize)
if (listlen + 1 == listcap)
{
listcap = (listcap * 3) / 2;
- list = fz_realloc(list, listcap, sizeof(struct entry));
+ list = fz_realloc(ctx, list, listcap * sizeof(struct entry));
}
list[listlen].num = num;
@@ -294,39 +297,39 @@ pdf_repair_xref(pdf_xref *xref, char *buf, int bufsize)
goto cleanup;
}
- obj = fz_dict_gets(dict, "Encrypt");
+ obj = fz_dict_gets(ctx, dict, "Encrypt");
if (obj)
{
if (encrypt)
- fz_drop_obj(encrypt);
+ fz_drop_obj(ctx, encrypt);
encrypt = fz_keep_obj(obj);
}
- obj = fz_dict_gets(dict, "ID");
+ obj = fz_dict_gets(ctx, dict, "ID");
if (obj)
{
if (id)
- fz_drop_obj(id);
+ fz_drop_obj(ctx, id);
id = fz_keep_obj(obj);
}
- obj = fz_dict_gets(dict, "Root");
+ obj = fz_dict_gets(ctx, dict, "Root");
if (obj)
{
if (root)
- fz_drop_obj(root);
+ fz_drop_obj(ctx, root);
root = fz_keep_obj(obj);
}
- obj = fz_dict_gets(dict, "Info");
+ obj = fz_dict_gets(ctx, dict, "Info");
if (obj)
{
if (info)
- fz_drop_obj(info);
+ fz_drop_obj(ctx, info);
info = fz_keep_obj(obj);
}
- fz_drop_obj(dict);
+ fz_drop_obj(ctx, dict);
}
else if (tok == PDF_TOK_ERROR)
@@ -358,11 +361,11 @@ pdf_repair_xref(pdf_xref *xref, char *buf, int bufsize)
goto cleanup;
}
- length = fz_new_int(list[i].stm_len);
- fz_dict_puts(dict, "Length", length);
- fz_drop_obj(length);
+ length = fz_new_int(ctx, list[i].stm_len);
+ fz_dict_puts(ctx, dict, "Length", length);
+ fz_drop_obj(ctx, length);
- fz_drop_obj(dict);
+ fz_drop_obj(ctx, dict);
}
}
@@ -387,21 +390,21 @@ pdf_repair_xref(pdf_xref *xref, char *buf, int bufsize)
/* create a repaired trailer, Root will be added later */
- xref->trailer = fz_new_dict(5);
+ xref->trailer = fz_new_dict(ctx, 5);
- obj = fz_new_int(maxnum + 1);
- fz_dict_puts(xref->trailer, "Size", obj);
- fz_drop_obj(obj);
+ obj = fz_new_int(ctx, maxnum + 1);
+ fz_dict_puts(ctx, xref->trailer, "Size", obj);
+ fz_drop_obj(ctx, obj);
if (root)
{
- fz_dict_puts(xref->trailer, "Root", root);
- fz_drop_obj(root);
+ fz_dict_puts(ctx, xref->trailer, "Root", root);
+ fz_drop_obj(ctx, root);
}
if (info)
{
- fz_dict_puts(xref->trailer, "Info", info);
- fz_drop_obj(info);
+ fz_dict_puts(ctx, xref->trailer, "Info", info);
+ fz_drop_obj(ctx, info);
}
if (encrypt)
@@ -409,12 +412,12 @@ pdf_repair_xref(pdf_xref *xref, char *buf, int bufsize)
if (fz_is_indirect(encrypt))
{
/* create new reference with non-NULL xref pointer */
- obj = fz_new_indirect(fz_to_num(encrypt), fz_to_gen(encrypt), xref);
- fz_drop_obj(encrypt);
+ obj = fz_new_indirect(ctx, fz_to_num(encrypt), fz_to_gen(encrypt), xref);
+ fz_drop_obj(ctx, encrypt);
encrypt = obj;
}
- fz_dict_puts(xref->trailer, "Encrypt", encrypt);
- fz_drop_obj(encrypt);
+ fz_dict_puts(ctx, xref->trailer, "Encrypt", encrypt);
+ fz_drop_obj(ctx, encrypt);
}
if (id)
@@ -422,23 +425,23 @@ pdf_repair_xref(pdf_xref *xref, char *buf, int bufsize)
if (fz_is_indirect(id))
{
/* create new reference with non-NULL xref pointer */
- obj = fz_new_indirect(fz_to_num(id), fz_to_gen(id), xref);
- fz_drop_obj(id);
+ obj = fz_new_indirect(ctx, fz_to_num(id), fz_to_gen(id), xref);
+ fz_drop_obj(ctx, id);
id = obj;
}
- fz_dict_puts(xref->trailer, "ID", id);
- fz_drop_obj(id);
+ fz_dict_puts(ctx, xref->trailer, "ID", id);
+ fz_drop_obj(ctx, id);
}
- fz_free(list);
+ fz_free(ctx, list);
return fz_okay;
cleanup:
- if (encrypt) fz_drop_obj(encrypt);
- if (id) fz_drop_obj(id);
- if (root) fz_drop_obj(root);
- if (info) fz_drop_obj(info);
- fz_free(list);
+ if (encrypt) fz_drop_obj(ctx, encrypt);
+ if (id) fz_drop_obj(ctx, id);
+ if (root) fz_drop_obj(ctx, root);
+ if (info) fz_drop_obj(ctx, info);
+ fz_free(ctx, list);
return error; /* already rethrown */
}
@@ -447,15 +450,16 @@ pdf_repair_obj_stms(pdf_xref *xref)
{
fz_obj *dict;
int i;
+ fz_context *ctx = xref->ctx;
for (i = 0; i < xref->len; i++)
{
if (xref->table[i].stm_ofs)
{
pdf_load_object(&dict, xref, i, 0);
- if (!strcmp(fz_to_name(fz_dict_gets(dict, "Type")), "ObjStm"))
+ if (!strcmp(fz_to_name(ctx, fz_dict_gets(ctx, dict, "Type")), "ObjStm"))
pdf_repair_obj_stm(xref, i, 0);
- fz_drop_obj(dict);
+ fz_drop_obj(ctx, dict);
}
}
diff --git a/pdf/pdf_shade.c b/pdf/pdf_shade.c
index cd3ae0e3..53d5700d 100644
--- a/pdf/pdf_shade.c
+++ b/pdf/pdf_shade.c
@@ -13,7 +13,7 @@ struct vertex
};
static void
-pdf_grow_mesh(fz_shade *shade, int amount)
+pdf_grow_mesh(fz_context *ctx, fz_shade *shade, int amount)
{
if (shade->mesh_len + amount < shade->mesh_cap)
return;
@@ -24,15 +24,15 @@ pdf_grow_mesh(fz_shade *shade, int amount)
while (shade->mesh_len + amount > shade->mesh_cap)
shade->mesh_cap = (shade->mesh_cap * 3) / 2;
- shade->mesh = fz_realloc(shade->mesh, shade->mesh_cap, sizeof(float));
+ shade->mesh = fz_realloc(ctx, shade->mesh, shade->mesh_cap * sizeof(float));
}
static void
-pdf_add_vertex(fz_shade *shade, struct vertex *v)
+pdf_add_vertex(fz_context *ctx, fz_shade *shade, struct vertex *v)
{
int ncomp = shade->use_function ? 1 : shade->colorspace->n;
int i;
- pdf_grow_mesh(shade, 2 + ncomp);
+ pdf_grow_mesh(ctx, shade, 2 + ncomp);
shade->mesh[shade->mesh_len++] = v->x;
shade->mesh[shade->mesh_len++] = v->y;
for (i = 0; i < ncomp; i++)
@@ -40,25 +40,25 @@ pdf_add_vertex(fz_shade *shade, struct vertex *v)
}
static void
-pdf_add_triangle(fz_shade *shade,
+pdf_add_triangle(fz_context *ctx, fz_shade *shade,
struct vertex *v0,
struct vertex *v1,
struct vertex *v2)
{
- pdf_add_vertex(shade, v0);
- pdf_add_vertex(shade, v1);
- pdf_add_vertex(shade, v2);
+ pdf_add_vertex(ctx, shade, v0);
+ pdf_add_vertex(ctx, shade, v1);
+ pdf_add_vertex(ctx, shade, v2);
}
static void
-pdf_add_quad(fz_shade *shade,
+pdf_add_quad(fz_context *ctx, fz_shade *shade,
struct vertex *v0,
struct vertex *v1,
struct vertex *v2,
struct vertex *v3)
{
- pdf_add_triangle(shade, v0, v1, v3);
- pdf_add_triangle(shade, v1, v3, v2);
+ pdf_add_triangle(ctx, shade, v0, v1, v3);
+ pdf_add_triangle(ctx, shade, v1, v3, v2);
}
/* Subdivide and tesselate tensor-patches */
@@ -72,7 +72,7 @@ struct pdf_tensor_patch_s
};
static void
-triangulate_patch(pdf_tensor_patch p, fz_shade *shade)
+triangulate_patch(fz_context *ctx, pdf_tensor_patch p, fz_shade *shade)
{
struct vertex v0, v1, v2, v3;
@@ -92,7 +92,7 @@ triangulate_patch(pdf_tensor_patch p, fz_shade *shade)
v3.y = p.pole[3][0].y;
memcpy(v3.c, p.color[3], sizeof(v3.c));
- pdf_add_quad(shade, &v0, &v1, &v2, &v3);
+ pdf_add_quad(ctx, shade, &v0, &v1, &v2, &v3);
}
static inline void midcolor(float *c, float *c1, float *c2)
@@ -163,7 +163,7 @@ split_stripe(pdf_tensor_patch *p, pdf_tensor_patch *s0, pdf_tensor_patch *s1)
}
static void
-draw_stripe(pdf_tensor_patch *p, fz_shade *shade, int depth)
+draw_stripe(fz_context *ctx, pdf_tensor_patch *p, fz_shade *shade, int depth)
{
pdf_tensor_patch s0, s1;
@@ -174,14 +174,14 @@ draw_stripe(pdf_tensor_patch *p, fz_shade *shade, int depth)
if (depth == 0)
{
/* if no more subdividing, draw two new patches... */
- triangulate_patch(s0, shade);
- triangulate_patch(s1, shade);
+ triangulate_patch(ctx, s0, shade);
+ triangulate_patch(ctx, s1, shade);
}
else
{
/* ...otherwise, continue subdividing. */
- draw_stripe(&s0, shade, depth);
- draw_stripe(&s1, shade, depth);
+ draw_stripe(ctx, &s0, shade, depth);
+ draw_stripe(ctx, &s1, shade, depth);
}
}
@@ -210,7 +210,7 @@ split_patch(pdf_tensor_patch *p, pdf_tensor_patch *s0, pdf_tensor_patch *s1)
}
static void
-draw_patch(fz_shade *shade, pdf_tensor_patch *p, int depth, int origdepth)
+draw_patch(fz_context *ctx, fz_shade *shade, pdf_tensor_patch *p, int depth, int origdepth)
{
pdf_tensor_patch s0, s1;
@@ -221,14 +221,14 @@ draw_patch(fz_shade *shade, pdf_tensor_patch *p, int depth, int origdepth)
if (depth == 0)
{
/* if no more subdividing, draw two new patches... */
- draw_stripe(&s0, shade, origdepth);
- draw_stripe(&s1, shade, origdepth);
+ draw_stripe(ctx, &s0, shade, origdepth);
+ draw_stripe(ctx, &s1, shade, origdepth);
}
else
{
/* ...otherwise, continue subdividing. */
- draw_patch(shade, &s0, depth, origdepth);
- draw_patch(shade, &s1, depth, origdepth);
+ draw_patch(ctx, shade, &s0, depth, origdepth);
+ draw_patch(ctx, shade, &s1, depth, origdepth);
}
}
@@ -373,22 +373,23 @@ pdf_load_function_based_shading(fz_shade *shade, pdf_xref *xref, fz_obj *dict, p
float x, y;
float xn, yn;
int i;
+ fz_context *ctx = xref->ctx;
x0 = y0 = 0;
x1 = y1 = 1;
- obj = fz_dict_gets(dict, "Domain");
- if (fz_array_len(obj) == 4)
+ obj = fz_dict_gets(ctx, dict, "Domain");
+ if (fz_array_len(ctx, obj) == 4)
{
- x0 = fz_to_real(fz_array_get(obj, 0));
- x1 = fz_to_real(fz_array_get(obj, 1));
- y0 = fz_to_real(fz_array_get(obj, 2));
- y1 = fz_to_real(fz_array_get(obj, 3));
+ x0 = fz_to_real(ctx, fz_array_get(ctx, obj, 0));
+ x1 = fz_to_real(ctx, fz_array_get(ctx, obj, 1));
+ y0 = fz_to_real(ctx, fz_array_get(ctx, obj, 2));
+ y1 = fz_to_real(ctx, fz_array_get(ctx, obj, 3));
}
matrix = fz_identity;
- obj = fz_dict_gets(dict, "Matrix");
- if (fz_array_len(obj) == 6)
- matrix = pdf_to_matrix(obj);
+ obj = fz_dict_gets(ctx, dict, "Matrix");
+ if (fz_array_len(ctx, obj) == 6)
+ matrix = pdf_to_matrix(ctx, obj);
for (yy = 0; yy < FUNSEGS; yy++)
{
@@ -421,7 +422,7 @@ pdf_load_function_based_shading(fz_shade *shade, pdf_xref *xref, fz_obj *dict, p
v[i].y = pt.y;
}
- pdf_add_quad(shade, &v[0], &v[1], &v[2], &v[3]);
+ pdf_add_quad(ctx, shade, &v[0], &v[1], &v[2], &v[3]);
}
}
}
@@ -434,28 +435,29 @@ pdf_load_axial_shading(fz_shade *shade, pdf_xref *xref, fz_obj *dict, int funcs,
int e0, e1;
float x0, y0, x1, y1;
struct vertex p1, p2;
+ fz_context *ctx = xref->ctx;
- obj = fz_dict_gets(dict, "Coords");
- x0 = fz_to_real(fz_array_get(obj, 0));
- y0 = fz_to_real(fz_array_get(obj, 1));
- x1 = fz_to_real(fz_array_get(obj, 2));
- y1 = fz_to_real(fz_array_get(obj, 3));
+ obj = fz_dict_gets(ctx, dict, "Coords");
+ x0 = fz_to_real(ctx, fz_array_get(ctx, obj, 0));
+ y0 = fz_to_real(ctx, fz_array_get(ctx, obj, 1));
+ x1 = fz_to_real(ctx, fz_array_get(ctx, obj, 2));
+ y1 = fz_to_real(ctx, fz_array_get(ctx, obj, 3));
d0 = 0;
d1 = 1;
- obj = fz_dict_gets(dict, "Domain");
- if (fz_array_len(obj) == 2)
+ obj = fz_dict_gets(ctx, dict, "Domain");
+ if (fz_array_len(ctx, obj) == 2)
{
- d0 = fz_to_real(fz_array_get(obj, 0));
- d1 = fz_to_real(fz_array_get(obj, 1));
+ d0 = fz_to_real(ctx, fz_array_get(ctx, obj, 0));
+ d1 = fz_to_real(ctx, fz_array_get(ctx, obj, 1));
}
e0 = e1 = 0;
- obj = fz_dict_gets(dict, "Extend");
- if (fz_array_len(obj) == 2)
+ obj = fz_dict_gets(ctx, dict, "Extend");
+ if (fz_array_len(ctx, obj) == 2)
{
- e0 = fz_to_bool(fz_array_get(obj, 0));
- e1 = fz_to_bool(fz_array_get(obj, 1));
+ e0 = fz_to_bool(ctx, fz_array_get(ctx, obj, 0));
+ e1 = fz_to_bool(ctx, fz_array_get(ctx, obj, 1));
}
pdf_sample_shade_function(shade, funcs, func, d0, d1);
@@ -468,12 +470,12 @@ pdf_load_axial_shading(fz_shade *shade, pdf_xref *xref, fz_obj *dict, int funcs,
p1.x = x0;
p1.y = y0;
p1.c[0] = 0;
- pdf_add_vertex(shade, &p1);
+ pdf_add_vertex(ctx, shade, &p1);
p2.x = x1;
p2.y = y1;
p2.c[0] = 0;
- pdf_add_vertex(shade, &p2);
+ pdf_add_vertex(ctx, shade, &p2);
}
static void
@@ -484,30 +486,31 @@ pdf_load_radial_shading(fz_shade *shade, pdf_xref *xref, fz_obj *dict, int funcs
int e0, e1;
float x0, y0, r0, x1, y1, r1;
struct vertex p1, p2;
+ fz_context *ctx = xref->ctx;
- obj = fz_dict_gets(dict, "Coords");
- x0 = fz_to_real(fz_array_get(obj, 0));
- y0 = fz_to_real(fz_array_get(obj, 1));
- r0 = fz_to_real(fz_array_get(obj, 2));
- x1 = fz_to_real(fz_array_get(obj, 3));
- y1 = fz_to_real(fz_array_get(obj, 4));
- r1 = fz_to_real(fz_array_get(obj, 5));
+ obj = fz_dict_gets(ctx, dict, "Coords");
+ x0 = fz_to_real(ctx, fz_array_get(ctx, obj, 0));
+ y0 = fz_to_real(ctx, fz_array_get(ctx, obj, 1));
+ r0 = fz_to_real(ctx, fz_array_get(ctx, obj, 2));
+ x1 = fz_to_real(ctx, fz_array_get(ctx, obj, 3));
+ y1 = fz_to_real(ctx, fz_array_get(ctx, obj, 4));
+ r1 = fz_to_real(ctx, fz_array_get(ctx, obj, 5));
d0 = 0;
d1 = 1;
- obj = fz_dict_gets(dict, "Domain");
- if (fz_array_len(obj) == 2)
+ obj = fz_dict_gets(ctx, dict, "Domain");
+ if (fz_array_len(ctx, obj) == 2)
{
- d0 = fz_to_real(fz_array_get(obj, 0));
- d1 = fz_to_real(fz_array_get(obj, 1));
+ d0 = fz_to_real(ctx, fz_array_get(ctx, obj, 0));
+ d1 = fz_to_real(ctx, fz_array_get(ctx, obj, 1));
}
e0 = e1 = 0;
- obj = fz_dict_gets(dict, "Extend");
- if (fz_array_len(obj) == 2)
+ obj = fz_dict_gets(ctx, dict, "Extend");
+ if (fz_array_len(ctx, obj) == 2)
{
- e0 = fz_to_bool(fz_array_get(obj, 0));
- e1 = fz_to_bool(fz_array_get(obj, 1));
+ e0 = fz_to_bool(ctx, fz_array_get(ctx, obj, 0));
+ e1 = fz_to_bool(ctx, fz_array_get(ctx, obj, 1));
}
pdf_sample_shade_function(shade, funcs, func, d0, d1);
@@ -520,12 +523,12 @@ pdf_load_radial_shading(fz_shade *shade, pdf_xref *xref, fz_obj *dict, int funcs
p1.x = x0;
p1.y = y0;
p1.c[0] = r0;
- pdf_add_vertex(shade, &p1);
+ pdf_add_vertex(ctx, shade, &p1);
p2.x = x1;
p2.y = y1;
p2.c[0] = r1;
- pdf_add_vertex(shade, &p2);
+ pdf_add_vertex(ctx, shade, &p2);
}
/* Type 4-7 -- Triangle and patch mesh shadings */
@@ -554,6 +557,7 @@ pdf_load_mesh_params(pdf_xref *xref, fz_obj *dict, struct mesh_params *p)
{
fz_obj *obj;
int i, n;
+ fz_context *ctx = xref->ctx;
p->x0 = p->y0 = 0;
p->x1 = p->y1 = 1;
@@ -563,23 +567,23 @@ pdf_load_mesh_params(pdf_xref *xref, fz_obj *dict, struct mesh_params *p)
p->c1[i] = 1;
}
- p->vprow = fz_to_int(fz_dict_gets(dict, "VerticesPerRow"));
- p->bpflag = fz_to_int(fz_dict_gets(dict, "BitsPerFlag"));
- p->bpcoord = fz_to_int(fz_dict_gets(dict, "BitsPerCoordinate"));
- p->bpcomp = fz_to_int(fz_dict_gets(dict, "BitsPerComponent"));
+ p->vprow = fz_to_int(ctx, fz_dict_gets(ctx, dict, "VerticesPerRow"));
+ p->bpflag = fz_to_int(ctx, fz_dict_gets(ctx, dict, "BitsPerFlag"));
+ p->bpcoord = fz_to_int(ctx, fz_dict_gets(ctx, dict, "BitsPerCoordinate"));
+ p->bpcomp = fz_to_int(ctx, fz_dict_gets(ctx, dict, "BitsPerComponent"));
- obj = fz_dict_gets(dict, "Decode");
- if (fz_array_len(obj) >= 6)
+ obj = fz_dict_gets(ctx, dict, "Decode");
+ if (fz_array_len(ctx, obj) >= 6)
{
- n = (fz_array_len(obj) - 4) / 2;
- p->x0 = fz_to_real(fz_array_get(obj, 0));
- p->x1 = fz_to_real(fz_array_get(obj, 1));
- p->y0 = fz_to_real(fz_array_get(obj, 2));
- p->y1 = fz_to_real(fz_array_get(obj, 3));
+ n = (fz_array_len(ctx, obj) - 4) / 2;
+ p->x0 = fz_to_real(ctx, fz_array_get(ctx, obj, 0));
+ p->x1 = fz_to_real(ctx, fz_array_get(ctx, obj, 1));
+ p->y0 = fz_to_real(ctx, fz_array_get(ctx, obj, 2));
+ p->y1 = fz_to_real(ctx, fz_array_get(ctx, obj, 3));
for (i = 0; i < n; i++)
{
- p->c0[i] = fz_to_real(fz_array_get(obj, 4 + i * 2));
- p->c1[i] = fz_to_real(fz_array_get(obj, 5 + i * 2));
+ p->c0[i] = fz_to_real(ctx, fz_array_get(ctx, obj, 4 + i * 2));
+ p->c1[i] = fz_to_real(ctx, fz_array_get(ctx, obj, 5 + i * 2));
}
}
@@ -608,6 +612,7 @@ pdf_load_type4_shade(fz_shade *shade, pdf_xref *xref, fz_obj *dict,
int ncomp;
int flag;
int i;
+ fz_context *ctx = xref->ctx;
pdf_load_mesh_params(xref, dict, &p);
@@ -644,20 +649,20 @@ pdf_load_type4_shade(fz_shade *shade, pdf_xref *xref, fz_obj *dict,
for (i = 0; i < ncomp; i++)
vc.c[i] = read_sample(stream, p.bpcomp, p.c0[i], p.c1[i]);
- pdf_add_triangle(shade, &va, &vb, &vc);
+ pdf_add_triangle(ctx, shade, &va, &vb, &vc);
break;
case 1: /* Vb, Vc, Vd */
va = vb;
vb = vc;
vc = vd;
- pdf_add_triangle(shade, &va, &vb, &vc);
+ pdf_add_triangle(ctx, shade, &va, &vb, &vc);
break;
case 2: /* Va, Vc, Vd */
vb = vc;
vc = vd;
- pdf_add_triangle(shade, &va, &vb, &vc);
+ pdf_add_triangle(ctx, shade, &va, &vb, &vc);
break;
}
}
@@ -672,6 +677,7 @@ pdf_load_type5_shade(fz_shade *shade, pdf_xref *xref, fz_obj *dict,
int first;
int ncomp;
int i, k;
+ fz_context *ctx = xref->ctx;
pdf_load_mesh_params(xref, dict, &p);
@@ -683,8 +689,8 @@ pdf_load_type5_shade(fz_shade *shade, pdf_xref *xref, fz_obj *dict,
else
ncomp = shade->colorspace->n;
- ref = fz_calloc(p.vprow, sizeof(struct vertex));
- buf = fz_calloc(p.vprow, sizeof(struct vertex));
+ ref = fz_calloc(ctx, p.vprow, sizeof(struct vertex));
+ buf = fz_calloc(ctx, p.vprow, sizeof(struct vertex));
first = 1;
while (!fz_is_eof_bits(stream))
@@ -699,7 +705,7 @@ pdf_load_type5_shade(fz_shade *shade, pdf_xref *xref, fz_obj *dict,
if (!first)
for (i = 0; i < p.vprow - 1; i++)
- pdf_add_quad(shade,
+ pdf_add_quad(ctx, shade,
&ref[i], &ref[i+1], &buf[i+1], &buf[i]);
memcpy(ref, buf, p.vprow * sizeof(struct vertex));
@@ -722,6 +728,7 @@ pdf_load_type6_shade(fz_shade *shade, pdf_xref *xref, fz_obj *dict,
fz_point prevp[12];
int ncomp;
int i, k;
+ fz_context *ctx = xref->ctx;
pdf_load_mesh_params(xref, dict, &p);
@@ -817,7 +824,7 @@ pdf_load_type6_shade(fz_shade *shade, pdf_xref *xref, fz_obj *dict,
for (i = 0; i < 4; i++)
memcpy(patch.color[i], c[i], ncomp * sizeof(float));
- draw_patch(shade, &patch, SUBDIV, SUBDIV);
+ draw_patch(ctx, shade, &patch, SUBDIV, SUBDIV);
for (i = 0; i < 12; i++)
prevp[i] = v[i];
@@ -840,6 +847,7 @@ pdf_load_type7_shade(fz_shade *shade, pdf_xref *xref, fz_obj *dict,
fz_point prevp[16];
int ncomp;
int i, k;
+ fz_context *ctx = xref->ctx;
pdf_load_mesh_params(xref, dict, &p);
@@ -935,7 +943,7 @@ pdf_load_type7_shade(fz_shade *shade, pdf_xref *xref, fz_obj *dict,
for (i = 0; i < 4; i++)
memcpy(patch.color[i], c[i], ncomp * sizeof(float));
- draw_patch(shade, &patch, SUBDIV, SUBDIV);
+ draw_patch(ctx, shade, &patch, SUBDIV, SUBDIV);
for (i = 0; i < 16; i++)
prevp[i] = v[i];
@@ -961,8 +969,9 @@ pdf_load_shading_dict(fz_shade **shadep, pdf_xref *xref, fz_obj *dict, fz_matrix
int funcs;
int type;
int i;
+ fz_context *ctx = xref->ctx;
- shade = fz_malloc(sizeof(fz_shade));
+ shade = fz_malloc(ctx, sizeof(fz_shade));
shade->refs = 1;
shade->type = FZ_MESH;
shade->use_background = 0;
@@ -980,38 +989,38 @@ pdf_load_shading_dict(fz_shade **shadep, pdf_xref *xref, fz_obj *dict, fz_matrix
funcs = 0;
- obj = fz_dict_gets(dict, "ShadingType");
- type = fz_to_int(obj);
+ obj = fz_dict_gets(ctx, dict, "ShadingType");
+ type = fz_to_int(ctx, obj);
- obj = fz_dict_gets(dict, "ColorSpace");
+ obj = fz_dict_gets(ctx, dict, "ColorSpace");
if (!obj)
{
- fz_drop_shade(shade);
+ fz_drop_shade(ctx, shade);
return fz_error_make("shading colorspace is missing");
}
error = pdf_load_colorspace(&shade->colorspace, xref, obj);
if (error)
{
- fz_drop_shade(shade);
+ fz_drop_shade(ctx, shade);
return fz_error_note(error, "cannot load colorspace (%d %d R)", fz_to_num(obj), fz_to_gen(obj));
}
- obj = fz_dict_gets(dict, "Background");
+ obj = fz_dict_gets(ctx, dict, "Background");
if (obj)
{
shade->use_background = 1;
for (i = 0; i < shade->colorspace->n; i++)
- shade->background[i] = fz_to_real(fz_array_get(obj, i));
+ shade->background[i] = fz_to_real(ctx, fz_array_get(ctx, obj, i));
}
- obj = fz_dict_gets(dict, "BBox");
- if (fz_is_array(obj))
+ obj = fz_dict_gets(ctx, dict, "BBox");
+ if (fz_is_array(ctx, obj))
{
- shade->bbox = pdf_to_rect(obj);
+ shade->bbox = pdf_to_rect(ctx, obj);
}
- obj = fz_dict_gets(dict, "Function");
- if (fz_is_dict(obj))
+ obj = fz_dict_gets(ctx, dict, "Function");
+ if (fz_is_dict(ctx, obj))
{
funcs = 1;
@@ -1022,9 +1031,9 @@ pdf_load_shading_dict(fz_shade **shadep, pdf_xref *xref, fz_obj *dict, fz_matrix
goto cleanup;
}
}
- else if (fz_is_array(obj))
+ else if (fz_is_array(ctx, obj))
{
- funcs = fz_array_len(obj);
+ funcs = fz_array_len(ctx, obj);
if (funcs != 1 && funcs != shade->colorspace->n)
{
error = fz_error_make("incorrect number of shading functions");
@@ -1033,7 +1042,7 @@ pdf_load_shading_dict(fz_shade **shadep, pdf_xref *xref, fz_obj *dict, fz_matrix
for (i = 0; i < funcs; i++)
{
- error = pdf_load_function(&func[i], xref, fz_array_get(obj, i));
+ error = pdf_load_function(&func[i], xref, fz_array_get(ctx, obj, i));
if (error)
{
error = fz_error_note(error, "cannot load shading function (%d %d R)", fz_to_num(obj), fz_to_gen(obj));
@@ -1070,7 +1079,7 @@ pdf_load_shading_dict(fz_shade **shadep, pdf_xref *xref, fz_obj *dict, fz_matrix
fz_close(stream);
for (i = 0; i < funcs; i++)
if (func[i])
- pdf_drop_function(func[i]);
+ pdf_drop_function(ctx, func[i]);
*shadep = shade;
return fz_okay;
@@ -1080,8 +1089,8 @@ cleanup:
fz_close(stream);
for (i = 0; i < funcs; i++)
if (func[i])
- pdf_drop_function(func[i]);
- fz_drop_shade(shade);
+ pdf_drop_function(ctx, func[i]);
+ fz_drop_shade(ctx, shade);
return fz_error_note(error, "cannot load shading type %d (%d %d R)", type, fz_to_num(dict), fz_to_gen(dict));
}
@@ -1092,32 +1101,33 @@ pdf_load_shading(fz_shade **shadep, pdf_xref *xref, fz_obj *dict)
fz_error error;
fz_matrix mat;
fz_obj *obj;
+ fz_context *ctx = xref->ctx;
- if ((*shadep = pdf_find_item(xref->store, fz_drop_shade, dict)))
+ if ((*shadep = pdf_find_item(ctx, xref->store, (pdf_store_drop_fn *)fz_drop_shade, dict)))
{
fz_keep_shade(*shadep);
return fz_okay;
}
/* Type 2 pattern dictionary */
- if (fz_dict_gets(dict, "PatternType"))
+ if (fz_dict_gets(ctx, dict, "PatternType"))
{
- obj = fz_dict_gets(dict, "Matrix");
+ obj = fz_dict_gets(ctx, dict, "Matrix");
if (obj)
- mat = pdf_to_matrix(obj);
+ mat = pdf_to_matrix(ctx, obj);
else
mat = fz_identity;
- obj = fz_dict_gets(dict, "ExtGState");
+ obj = fz_dict_gets(ctx, dict, "ExtGState");
if (obj)
{
- if (fz_dict_gets(obj, "CA") || fz_dict_gets(obj, "ca"))
+ if (fz_dict_gets(ctx, obj, "CA") || fz_dict_gets(ctx, obj, "ca"))
{
fz_warn("shading with alpha not supported");
}
}
- obj = fz_dict_gets(dict, "Shading");
+ obj = fz_dict_gets(ctx, dict, "Shading");
if (!obj)
return fz_error_make("syntaxerror: missing shading dictionary");
@@ -1134,7 +1144,7 @@ pdf_load_shading(fz_shade **shadep, pdf_xref *xref, fz_obj *dict)
return fz_error_note(error, "cannot load shading dictionary (%d %d R)", fz_to_num(dict), fz_to_gen(dict));
}
- pdf_store_item(xref->store, fz_keep_shade, fz_drop_shade, dict, *shadep);
+ pdf_store_item(ctx, xref->store, (pdf_store_keep_fn *)fz_keep_shade, (pdf_store_drop_fn *)fz_drop_shade, dict, *shadep);
return fz_okay;
}
diff --git a/pdf/pdf_store.c b/pdf/pdf_store.c
index a57f839a..346afc5d 100644
--- a/pdf/pdf_store.c
+++ b/pdf/pdf_store.c
@@ -5,7 +5,7 @@ typedef struct pdf_item_s pdf_item;
struct pdf_item_s
{
- void *drop_func;
+ pdf_store_drop_fn *drop_func;
fz_obj *key;
void *val;
int age;
@@ -14,7 +14,7 @@ struct pdf_item_s
struct refkey
{
- void *drop_func;
+ pdf_store_drop_fn *drop_func;
int num;
int gen;
};
@@ -26,27 +26,27 @@ struct pdf_store_s
};
pdf_store *
-pdf_new_store(void)
+pdf_new_store(fz_context *ctx)
{
pdf_store *store;
- store = fz_malloc(sizeof(pdf_store));
- store->hash = fz_new_hash_table(4096, sizeof(struct refkey));
+ store = fz_malloc(ctx, sizeof(pdf_store));
+ store->hash = fz_new_hash_table(ctx, 4096, sizeof(struct refkey));
store->root = NULL;
return store;
}
void
-pdf_store_item(pdf_store *store, void *keepfunc, void *drop_func, fz_obj *key, void *val)
+pdf_store_item(fz_context *ctx, pdf_store *store, pdf_store_keep_fn *keep_func, pdf_store_drop_fn *drop_func, fz_obj *key, void *val)
{
pdf_item *item;
if (!store)
return;
- item = fz_malloc(sizeof(pdf_item));
+ item = fz_malloc(ctx, sizeof(pdf_item));
item->drop_func = drop_func;
item->key = fz_keep_obj(key);
- item->val = ((void*(*)(void*))keepfunc)(val);
+ item->val = keep_func(val);
item->age = 0;
item->next = NULL;
@@ -56,7 +56,7 @@ pdf_store_item(pdf_store *store, void *keepfunc, void *drop_func, fz_obj *key, v
refkey.drop_func = drop_func;
refkey.num = fz_to_num(key);
refkey.gen = fz_to_gen(key);
- fz_hash_insert(store->hash, &refkey, item);
+ fz_hash_insert(ctx, store->hash, &refkey, item);
}
else
{
@@ -66,7 +66,7 @@ pdf_store_item(pdf_store *store, void *keepfunc, void *drop_func, fz_obj *key, v
}
void *
-pdf_find_item(pdf_store *store, void *drop_func, fz_obj *key)
+pdf_find_item(fz_context *ctx, pdf_store *store, pdf_store_drop_fn *drop_func, fz_obj *key)
{
struct refkey refkey;
pdf_item *item;
@@ -105,7 +105,7 @@ pdf_find_item(pdf_store *store, void *drop_func, fz_obj *key)
}
void
-pdf_remove_item(pdf_store *store, void *drop_func, fz_obj *key)
+pdf_remove_item(fz_context *ctx, pdf_store *store, pdf_store_drop_fn *drop_func, fz_obj *key)
{
struct refkey refkey;
pdf_item *item, *prev, *next;
@@ -119,9 +119,9 @@ pdf_remove_item(pdf_store *store, void *drop_func, fz_obj *key)
if (item)
{
fz_hash_remove(store->hash, &refkey);
- ((void(*)(void*))item->drop_func)(item->val);
- fz_drop_obj(item->key);
- fz_free(item);
+ item->drop_func(ctx, item->val);
+ fz_drop_obj(ctx, item->key);
+ fz_free(ctx, item);
}
}
else
@@ -136,9 +136,9 @@ pdf_remove_item(pdf_store *store, void *drop_func, fz_obj *key)
store->root = next;
else
prev->next = next;
- ((void(*)(void*))item->drop_func)(item->val);
- fz_drop_obj(item->key);
- fz_free(item);
+ item->drop_func(ctx, item->val);
+ fz_drop_obj(ctx, item->key);
+ fz_free(ctx, item);
}
else
prev = item;
@@ -147,22 +147,23 @@ pdf_remove_item(pdf_store *store, void *drop_func, fz_obj *key)
}
void
-pdf_age_store(pdf_store *store, int maxage)
+pdf_age_store(fz_context *ctx, pdf_store *store, int maxage)
{
struct refkey *refkey;
pdf_item *item, *prev, *next;
- int i;
+ int i, n;
- for (i = 0; i < fz_hash_len(store->hash); i++)
+ n = fz_hash_len(store->hash);
+ for (i = 0; i < n; i++)
{
refkey = fz_hash_get_key(store->hash, i);
item = fz_hash_get_val(store->hash, i);
if (item && ++item->age > maxage)
{
fz_hash_remove(store->hash, refkey);
- ((void(*)(void*))item->drop_func)(item->val);
- fz_drop_obj(item->key);
- fz_free(item);
+ item->drop_func(ctx, item->val);
+ fz_drop_obj(ctx, item->key);
+ fz_free(ctx, item);
i--; /* items with same hash may move into place */
}
}
@@ -177,9 +178,9 @@ pdf_age_store(pdf_store *store, int maxage)
store->root = next;
else
prev->next = next;
- ((void(*)(void*))item->drop_func)(item->val);
- fz_drop_obj(item->key);
- fz_free(item);
+ item->drop_func(ctx, item->val);
+ fz_drop_obj(ctx, item->key);
+ fz_free(ctx, item);
}
else
prev = item;
@@ -187,24 +188,25 @@ pdf_age_store(pdf_store *store, int maxage)
}
void
-pdf_free_store(pdf_store *store)
+pdf_free_store(fz_context *ctx, pdf_store *store)
{
- pdf_age_store(store, 0);
- fz_free_hash(store->hash);
- fz_free(store);
+ pdf_age_store(ctx, store, 0);
+ fz_free_hash(ctx, store->hash);
+ fz_free(ctx, store);
}
void
-pdf_debug_store(pdf_store *store)
+pdf_debug_store(fz_context *ctx, pdf_store *store)
{
pdf_item *item;
pdf_item *next;
struct refkey *refkey;
- int i;
+ int i, n;
printf("-- resource store contents --\n");
- for (i = 0; i < fz_hash_len(store->hash); i++)
+ n = fz_hash_len(store->hash);
+ for (i = 0; i < n; i++)
{
refkey = fz_hash_get_key(store->hash, i);
item = fz_hash_get_val(store->hash, i);
@@ -216,7 +218,7 @@ pdf_debug_store(pdf_store *store)
{
next = item->next;
printf("store[*] ");
- fz_debug_obj(item->key);
+ fz_debug_obj(ctx, item->key);
printf(" = %p\n", item->val);
}
}
diff --git a/pdf/pdf_stream.c b/pdf/pdf_stream.c
index d0e3bad0..4fb9adce 100644
--- a/pdf/pdf_stream.c
+++ b/pdf/pdf_stream.c
@@ -26,23 +26,24 @@ pdf_is_stream(pdf_xref *xref, int num, int gen)
* Scan stream dictionary for an explicit /Crypt filter
*/
static int
-pdf_stream_has_crypt(fz_obj *stm)
+pdf_stream_has_crypt(fz_context *ctx, fz_obj *stm)
{
fz_obj *filters;
fz_obj *obj;
int i;
- filters = fz_dict_getsa(stm, "Filter", "F");
+ filters = fz_dict_getsa(ctx, stm, "Filter", "F");
if (filters)
{
- if (!strcmp(fz_to_name(filters), "Crypt"))
+ if (!strcmp(fz_to_name(ctx, filters), "Crypt"))
return 1;
- if (fz_is_array(filters))
+ if (fz_is_array(ctx, filters))
{
- for (i = 0; i < fz_array_len(filters); i++)
+ int n = fz_array_len(ctx, filters);
+ for (i = 0; i < n; i++)
{
- obj = fz_array_get(filters, i);
- if (!strcmp(fz_to_name(obj), "Crypt"))
+ obj = fz_array_get(ctx, filters, i);
+ if (!strcmp(fz_to_name(ctx, obj), "Crypt"))
return 1;
}
}
@@ -58,8 +59,9 @@ build_filter(fz_stream *chain, pdf_xref * xref, fz_obj * f, fz_obj * p, int num,
{
fz_error error;
char *s;
+ fz_context *ctx = chain->ctx;
- s = fz_to_name(f);
+ s = fz_to_name(ctx, f);
if (!strcmp(s, "ASCIIHexDecode") || !strcmp(s, "AHx"))
return fz_open_ahxd(chain);
@@ -78,23 +80,23 @@ build_filter(fz_stream *chain, pdf_xref * xref, fz_obj * f, fz_obj * p, int num,
else if (!strcmp(s, "FlateDecode") || !strcmp(s, "Fl"))
{
- fz_obj *obj = fz_dict_gets(p, "Predictor");
- if (fz_to_int(obj) > 1)
+ fz_obj *obj = fz_dict_gets(ctx, p, "Predictor");
+ if (fz_to_int(ctx, obj) > 1)
return fz_open_predict(fz_open_flated(chain), p);
return fz_open_flated(chain);
}
else if (!strcmp(s, "LZWDecode") || !strcmp(s, "LZW"))
{
- fz_obj *obj = fz_dict_gets(p, "Predictor");
- if (fz_to_int(obj) > 1)
+ fz_obj *obj = fz_dict_gets(ctx, p, "Predictor");
+ if (fz_to_int(ctx, obj) > 1)
return fz_open_predict(fz_open_lzwd(chain, p), p);
return fz_open_lzwd(chain, p);
}
else if (!strcmp(s, "JBIG2Decode"))
{
- fz_obj *obj = fz_dict_gets(p, "JBIG2Globals");
+ fz_obj *obj = fz_dict_gets(ctx, p, "JBIG2Globals");
if (obj)
{
fz_buffer *globals;
@@ -102,7 +104,7 @@ build_filter(fz_stream *chain, pdf_xref * xref, fz_obj * f, fz_obj * p, int num,
if (error)
fz_error_handle(error, "cannot load jbig2 global segments");
chain = fz_open_jbig2d(chain, globals);
- fz_drop_buffer(globals);
+ fz_drop_buffer(ctx, globals);
return chain;
}
return fz_open_jbig2d(chain, NULL);
@@ -121,9 +123,9 @@ build_filter(fz_stream *chain, pdf_xref * xref, fz_obj * f, fz_obj * p, int num,
return chain;
}
- name = fz_dict_gets(p, "Name");
- if (fz_is_name(name))
- return pdf_open_crypt_with_filter(chain, xref->crypt, fz_to_name(name), num, gen);
+ name = fz_dict_gets(ctx, p, "Name");
+ if (fz_is_name(ctx, name))
+ return pdf_open_crypt_with_filter(chain, xref->crypt, fz_to_name(ctx, name), num, gen);
return chain;
}
@@ -142,12 +144,14 @@ build_filter_chain(fz_stream *chain, pdf_xref *xref, fz_obj *fs, fz_obj *ps, int
{
fz_obj *f;
fz_obj *p;
- int i;
+ int i, n;
+ fz_context *ctx = chain->ctx;
- for (i = 0; i < fz_array_len(fs); i++)
+ n = fz_array_len(ctx, fs);
+ for (i = 0; i < n; i++)
{
- f = fz_array_get(fs, i);
- p = fz_array_get(ps, i);
+ f = fz_array_get(ctx, fs, i);
+ p = fz_array_get(ctx, ps, i);
chain = build_filter(chain, xref, f, p, num, gen);
}
@@ -164,14 +168,15 @@ pdf_open_raw_filter(fz_stream *chain, pdf_xref *xref, fz_obj *stmobj, int num, i
{
int hascrypt;
int len;
+ fz_context *ctx = chain->ctx;
/* don't close chain when we close this filter */
fz_keep_stream(chain);
- len = fz_to_int(fz_dict_gets(stmobj, "Length"));
+ len = fz_to_int(ctx, fz_dict_gets(ctx, stmobj, "Length"));
chain = fz_open_null(chain, len);
- hascrypt = pdf_stream_has_crypt(stmobj);
+ hascrypt = pdf_stream_has_crypt(ctx, stmobj);
if (xref->crypt && !hascrypt)
chain = pdf_open_crypt(chain, xref->crypt, num, gen);
@@ -187,15 +192,16 @@ pdf_open_filter(fz_stream *chain, pdf_xref *xref, fz_obj *stmobj, int num, int g
{
fz_obj *filters;
fz_obj *params;
+ fz_context *ctx = chain->ctx;
- filters = fz_dict_getsa(stmobj, "Filter", "F");
- params = fz_dict_getsa(stmobj, "DecodeParms", "DP");
+ filters = fz_dict_getsa(ctx, stmobj, "Filter", "F");
+ params = fz_dict_getsa(ctx, stmobj, "DecodeParms", "DP");
chain = pdf_open_raw_filter(chain, xref, stmobj, num, gen);
- if (fz_is_name(filters))
+ if (fz_is_name(ctx, filters))
return build_filter(chain, xref, filters, params, num, gen);
- if (fz_array_len(filters) > 0)
+ if (fz_array_len(ctx, filters) > 0)
return build_filter_chain(chain, xref, filters, params, num, gen);
return chain;
@@ -210,16 +216,17 @@ pdf_open_inline_stream(fz_stream *chain, pdf_xref *xref, fz_obj *stmobj, int len
{
fz_obj *filters;
fz_obj *params;
+ fz_context *ctx = chain->ctx;
- filters = fz_dict_getsa(stmobj, "Filter", "F");
- params = fz_dict_getsa(stmobj, "DecodeParms", "DP");
+ filters = fz_dict_getsa(ctx, stmobj, "Filter", "F");
+ params = fz_dict_getsa(ctx, stmobj, "DecodeParms", "DP");
/* don't close chain when we close this filter */
fz_keep_stream(chain);
- if (fz_is_name(filters))
+ if (fz_is_name(ctx, filters))
return build_filter(chain, xref, filters, params, 0, 0);
- if (fz_array_len(filters) > 0)
+ if (fz_array_len(ctx, filters) > 0)
return build_filter_chain(chain, xref, filters, params, 0, 0);
return fz_open_null(chain, length);
@@ -306,14 +313,15 @@ pdf_load_raw_stream(fz_buffer **bufp, pdf_xref *xref, int num, int gen)
fz_stream *stm;
fz_obj *dict;
int len;
+ fz_context *ctx = xref->ctx;
error = pdf_load_object(&dict, xref, num, gen);
if (error)
return fz_error_note(error, "cannot load stream dictionary (%d %d R)", num, gen);
- len = fz_to_int(fz_dict_gets(dict, "Length"));
+ len = fz_to_int(ctx, fz_dict_gets(ctx, dict, "Length"));
- fz_drop_obj(dict);
+ fz_drop_obj(ctx, dict);
error = pdf_open_raw_stream(&stm, xref, num, gen);
if (error)
@@ -355,7 +363,8 @@ pdf_load_stream(fz_buffer **bufp, pdf_xref *xref, int num, int gen)
fz_error error;
fz_stream *stm;
fz_obj *dict, *obj;
- int i, len;
+ int i, len, n;
+ fz_context *ctx = xref->ctx;
error = pdf_open_stream(&stm, xref, num, gen);
if (error)
@@ -365,13 +374,14 @@ pdf_load_stream(fz_buffer **bufp, pdf_xref *xref, int num, int gen)
if (error)
return fz_error_note(error, "cannot load stream dictionary (%d %d R)", num, gen);
- len = fz_to_int(fz_dict_gets(dict, "Length"));
- obj = fz_dict_gets(dict, "Filter");
- len = pdf_guess_filter_length(len, fz_to_name(obj));
- for (i = 0; i < fz_array_len(obj); i++)
- len = pdf_guess_filter_length(len, fz_to_name(fz_array_get(obj, i)));
+ len = fz_to_int(ctx, fz_dict_gets(ctx, dict, "Length"));
+ obj = fz_dict_gets(ctx, dict, "Filter");
+ len = pdf_guess_filter_length(len, fz_to_name(ctx, obj));
+ n = fz_array_len(ctx, obj);
+ for (i = 0; i < n; i++)
+ len = pdf_guess_filter_length(len, fz_to_name(ctx, fz_array_get(ctx, obj, i)));
- fz_drop_obj(dict);
+ fz_drop_obj(ctx, dict);
error = fz_read_all(bufp, stm, len);
if (error)
diff --git a/pdf/pdf_type3.c b/pdf/pdf_type3.c
index 1d814ddd..7f055f9f 100644
--- a/pdf/pdf_type3.c
+++ b/pdf/pdf_type3.c
@@ -22,22 +22,23 @@ pdf_load_type3_font(pdf_font_desc **fontdescp, pdf_xref *xref, fz_obj *rdb, fz_o
int i, k, n;
fz_rect bbox;
fz_matrix matrix;
+ fz_context *ctx = xref->ctx;
- obj = fz_dict_gets(dict, "Name");
- if (fz_is_name(obj))
- fz_strlcpy(buf, fz_to_name(obj), sizeof buf);
+ obj = fz_dict_gets(ctx, dict, "Name");
+ if (fz_is_name(ctx, obj))
+ fz_strlcpy(buf, fz_to_name(ctx, obj), sizeof buf);
else
sprintf(buf, "Unnamed-T3");
- fontdesc = pdf_new_font_desc();
+ fontdesc = pdf_new_font_desc(ctx);
- obj = fz_dict_gets(dict, "FontMatrix");
- matrix = pdf_to_matrix(obj);
+ obj = fz_dict_gets(ctx, dict, "FontMatrix");
+ matrix = pdf_to_matrix(ctx, obj);
- obj = fz_dict_gets(dict, "FontBBox");
- bbox = pdf_to_rect(obj);
+ obj = fz_dict_gets(ctx, dict, "FontBBox");
+ bbox = pdf_to_rect(ctx, obj);
- fontdesc->font = fz_new_type3_font(buf, matrix);
+ fontdesc->font = fz_new_type3_font(ctx, buf, matrix);
fz_set_font_bbox(fontdesc->font, bbox.x0, bbox.y0, bbox.x1, bbox.y1);
@@ -46,45 +47,45 @@ pdf_load_type3_font(pdf_font_desc **fontdescp, pdf_xref *xref, fz_obj *rdb, fz_o
for (i = 0; i < 256; i++)
estrings[i] = NULL;
- encoding = fz_dict_gets(dict, "Encoding");
+ encoding = fz_dict_gets(ctx, dict, "Encoding");
if (!encoding)
{
error = fz_error_make("syntaxerror: Type3 font missing Encoding");
goto cleanup;
}
- if (fz_is_name(encoding))
- pdf_load_encoding(estrings, fz_to_name(encoding));
+ if (fz_is_name(ctx, encoding))
+ pdf_load_encoding(estrings, fz_to_name(ctx, encoding));
- if (fz_is_dict(encoding))
+ if (fz_is_dict(ctx, encoding))
{
fz_obj *base, *diff, *item;
- base = fz_dict_gets(encoding, "BaseEncoding");
- if (fz_is_name(base))
- pdf_load_encoding(estrings, fz_to_name(base));
+ base = fz_dict_gets(ctx, encoding, "BaseEncoding");
+ if (fz_is_name(ctx, base))
+ pdf_load_encoding(estrings, fz_to_name(ctx, base));
- diff = fz_dict_gets(encoding, "Differences");
- if (fz_is_array(diff))
+ diff = fz_dict_gets(ctx, encoding, "Differences");
+ if (fz_is_array(ctx, diff))
{
- n = fz_array_len(diff);
+ n = fz_array_len(ctx, diff);
k = 0;
for (i = 0; i < n; i++)
{
- item = fz_array_get(diff, i);
- if (fz_is_int(item))
- k = fz_to_int(item);
- if (fz_is_name(item))
- estrings[k++] = fz_to_name(item);
+ item = fz_array_get(ctx, diff, i);
+ if (fz_is_int(ctx, item))
+ k = fz_to_int(ctx, item);
+ if (fz_is_name(ctx, item))
+ estrings[k++] = fz_to_name(ctx, item);
if (k < 0) k = 0;
if (k > 255) k = 255;
}
}
}
- fontdesc->encoding = pdf_new_identity_cmap(0, 1);
+ fontdesc->encoding = pdf_new_identity_cmap(ctx, 0, 1);
- error = pdf_load_to_unicode(fontdesc, xref, estrings, NULL, fz_dict_gets(dict, "ToUnicode"));
+ error = pdf_load_to_unicode(fontdesc, xref, estrings, NULL, fz_dict_gets(ctx, dict, "ToUnicode"));
if (error)
goto cleanup;
@@ -92,10 +93,10 @@ pdf_load_type3_font(pdf_font_desc **fontdescp, pdf_xref *xref, fz_obj *rdb, fz_o
pdf_set_default_hmtx(fontdesc, 0);
- first = fz_to_int(fz_dict_gets(dict, "FirstChar"));
- last = fz_to_int(fz_dict_gets(dict, "LastChar"));
+ first = fz_to_int(ctx, fz_dict_gets(ctx, dict, "FirstChar"));
+ last = fz_to_int(ctx, fz_dict_gets(ctx, dict, "LastChar"));
- widths = fz_dict_gets(dict, "Widths");
+ widths = fz_dict_gets(ctx, dict, "Widths");
if (!widths)
{
error = fz_error_make("syntaxerror: Type3 font missing Widths");
@@ -104,17 +105,17 @@ pdf_load_type3_font(pdf_font_desc **fontdescp, pdf_xref *xref, fz_obj *rdb, fz_o
for (i = first; i <= last; i++)
{
- float w = fz_to_real(fz_array_get(widths, i - first));
+ float w = fz_to_real(ctx, fz_array_get(ctx, widths, i - first));
w = fontdesc->font->t3matrix.a * w * 1000;
fontdesc->font->t3widths[i] = w * 0.001f;
- pdf_add_hmtx(fontdesc, i, i, w);
+ pdf_add_hmtx(ctx, fontdesc, i, i, w);
}
pdf_end_hmtx(fontdesc);
/* Resources -- inherit page resources if the font doesn't have its own */
- fontdesc->font->t3resources = fz_dict_gets(dict, "Resources");
+ fontdesc->font->t3resources = fz_dict_gets(ctx, dict, "Resources");
if (!fontdesc->font->t3resources)
fontdesc->font->t3resources = rdb;
if (fontdesc->font->t3resources)
@@ -127,7 +128,7 @@ pdf_load_type3_font(pdf_font_desc **fontdescp, pdf_xref *xref, fz_obj *rdb, fz_o
/* CharProcs */
- charprocs = fz_dict_gets(dict, "CharProcs");
+ charprocs = fz_dict_gets(ctx, dict, "CharProcs");
if (!charprocs)
{
error = fz_error_make("syntaxerror: Type3 font missing CharProcs");
@@ -138,7 +139,7 @@ pdf_load_type3_font(pdf_font_desc **fontdescp, pdf_xref *xref, fz_obj *rdb, fz_o
{
if (estrings[i])
{
- obj = fz_dict_gets(charprocs, estrings[i]);
+ obj = fz_dict_gets(ctx, charprocs, estrings[i]);
if (pdf_is_stream(xref, fz_to_num(obj), fz_to_gen(obj)))
{
error = pdf_load_stream(&fontdesc->font->t3procs[i], xref, fz_to_num(obj), fz_to_gen(obj));
@@ -152,7 +153,7 @@ pdf_load_type3_font(pdf_font_desc **fontdescp, pdf_xref *xref, fz_obj *rdb, fz_o
return fz_okay;
cleanup:
- fz_drop_font(fontdesc->font);
- fz_free(fontdesc);
+ fz_drop_font(ctx, fontdesc->font);
+ fz_free(ctx, fontdesc);
return fz_error_note(error, "cannot load type3 font (%d %d R)", fz_to_num(dict), fz_to_gen(dict));
}
diff --git a/pdf/pdf_unicode.c b/pdf/pdf_unicode.c
index 3f27eeb7..25781e1c 100644
--- a/pdf/pdf_unicode.c
+++ b/pdf/pdf_unicode.c
@@ -13,6 +13,7 @@ pdf_load_to_unicode(pdf_font_desc *font, pdf_xref *xref,
int ucsbuf[8];
int ucslen;
int i;
+ fz_context *ctx = xref->ctx;
if (pdf_is_stream(xref, fz_to_num(cmapstm), fz_to_gen(cmapstm)))
{
@@ -20,7 +21,7 @@ pdf_load_to_unicode(pdf_font_desc *font, pdf_xref *xref,
if (error)
return fz_error_note(error, "cannot load embedded cmap (%d %d R)", fz_to_num(cmapstm), fz_to_gen(cmapstm));
- font->to_unicode = pdf_new_cmap();
+ font->to_unicode = pdf_new_cmap(ctx);
for (i = 0; i < (strings ? 256 : 65536); i++)
{
@@ -29,15 +30,15 @@ pdf_load_to_unicode(pdf_font_desc *font, pdf_xref *xref,
{
ucslen = pdf_lookup_cmap_full(cmap, i, ucsbuf);
if (ucslen == 1)
- pdf_map_range_to_range(font->to_unicode, cid, cid, ucsbuf[0]);
+ pdf_map_range_to_range(ctx, font->to_unicode, cid, cid, ucsbuf[0]);
if (ucslen > 1)
- pdf_map_one_to_many(font->to_unicode, cid, ucsbuf, ucslen);
+ pdf_map_one_to_many(ctx, font->to_unicode, cid, ucsbuf, ucslen);
}
}
- pdf_sort_cmap(font->to_unicode);
+ pdf_sort_cmap(ctx, font->to_unicode);
- pdf_drop_cmap(cmap);
+ pdf_drop_cmap(ctx, cmap);
}
else if (collection)
@@ -45,13 +46,13 @@ pdf_load_to_unicode(pdf_font_desc *font, pdf_xref *xref,
error = fz_okay;
if (!strcmp(collection, "Adobe-CNS1"))
- error = pdf_load_system_cmap(&font->to_unicode, "Adobe-CNS1-UCS2");
+ error = pdf_load_system_cmap(ctx, &font->to_unicode, "Adobe-CNS1-UCS2");
else if (!strcmp(collection, "Adobe-GB1"))
- error = pdf_load_system_cmap(&font->to_unicode, "Adobe-GB1-UCS2");
+ error = pdf_load_system_cmap(ctx, &font->to_unicode, "Adobe-GB1-UCS2");
else if (!strcmp(collection, "Adobe-Japan1"))
- error = pdf_load_system_cmap(&font->to_unicode, "Adobe-Japan1-UCS2");
+ error = pdf_load_system_cmap(ctx, &font->to_unicode, "Adobe-Japan1-UCS2");
else if (!strcmp(collection, "Adobe-Korea1"))
- error = pdf_load_system_cmap(&font->to_unicode, "Adobe-Korea1-UCS2");
+ error = pdf_load_system_cmap(ctx, &font->to_unicode, "Adobe-Korea1-UCS2");
if (error)
return fz_error_note(error, "cannot load ToUnicode system cmap %s-UCS2", collection);
@@ -62,7 +63,7 @@ pdf_load_to_unicode(pdf_font_desc *font, pdf_xref *xref,
/* TODO one-to-many mappings */
font->cid_to_ucs_len = 256;
- font->cid_to_ucs = fz_calloc(256, sizeof(unsigned short));
+ font->cid_to_ucs = fz_calloc(ctx, 256, sizeof(unsigned short));
for (i = 0; i < 256; i++)
{
diff --git a/pdf/pdf_xobject.c b/pdf/pdf_xobject.c
index 630ca1d9..1c1bd276 100644
--- a/pdf/pdf_xobject.c
+++ b/pdf/pdf_xobject.c
@@ -7,28 +7,29 @@ pdf_load_xobject(pdf_xobject **formp, pdf_xref *xref, fz_obj *dict)
fz_error error;
pdf_xobject *form;
fz_obj *obj;
+ fz_context *ctx = xref->ctx;
- if ((*formp = pdf_find_item(xref->store, pdf_drop_xobject, dict)))
+ if ((*formp = pdf_find_item(ctx, xref->store, (pdf_store_drop_fn *)pdf_drop_xobject, dict)))
{
pdf_keep_xobject(*formp);
return fz_okay;
}
- form = fz_malloc(sizeof(pdf_xobject));
+ form = fz_malloc(ctx, sizeof(pdf_xobject));
form->refs = 1;
form->resources = NULL;
form->contents = NULL;
form->colorspace = NULL;
/* Store item immediately, to avoid possible recursion if objects refer back to this one */
- pdf_store_item(xref->store, pdf_keep_xobject, pdf_drop_xobject, dict, form);
+ pdf_store_item(ctx, xref->store, (pdf_store_keep_fn *)pdf_keep_xobject, (pdf_store_drop_fn *)pdf_drop_xobject, dict, form);
- obj = fz_dict_gets(dict, "BBox");
- form->bbox = pdf_to_rect(obj);
+ obj = fz_dict_gets(ctx, dict, "BBox");
+ form->bbox = pdf_to_rect(ctx, obj);
- obj = fz_dict_gets(dict, "Matrix");
+ obj = fz_dict_gets(ctx, dict, "Matrix");
if (obj)
- form->matrix = pdf_to_matrix(obj);
+ form->matrix = pdf_to_matrix(ctx, obj);
else
form->matrix = fz_identity;
@@ -36,19 +37,19 @@ pdf_load_xobject(pdf_xobject **formp, pdf_xref *xref, fz_obj *dict)
form->knockout = 0;
form->transparency = 0;
- obj = fz_dict_gets(dict, "Group");
+ obj = fz_dict_gets(ctx, dict, "Group");
if (obj)
{
fz_obj *attrs = obj;
- form->isolated = fz_to_bool(fz_dict_gets(attrs, "I"));
- form->knockout = fz_to_bool(fz_dict_gets(attrs, "K"));
+ form->isolated = fz_to_bool(ctx, fz_dict_gets(ctx, attrs, "I"));
+ form->knockout = fz_to_bool(ctx, fz_dict_gets(ctx, attrs, "K"));
- obj = fz_dict_gets(attrs, "S");
- if (fz_is_name(obj) && !strcmp(fz_to_name(obj), "Transparency"))
+ obj = fz_dict_gets(ctx, attrs, "S");
+ if (fz_is_name(ctx, obj) && !strcmp(fz_to_name(ctx, obj), "Transparency"))
form->transparency = 1;
- obj = fz_dict_gets(attrs, "CS");
+ obj = fz_dict_gets(ctx, attrs, "CS");
if (obj)
{
error = pdf_load_colorspace(&form->colorspace, xref, obj);
@@ -57,15 +58,15 @@ pdf_load_xobject(pdf_xobject **formp, pdf_xref *xref, fz_obj *dict)
}
}
- form->resources = fz_dict_gets(dict, "Resources");
+ form->resources = fz_dict_gets(ctx, dict, "Resources");
if (form->resources)
fz_keep_obj(form->resources);
error = pdf_load_stream(&form->contents, xref, fz_to_num(dict), fz_to_gen(dict));
if (error)
{
- pdf_remove_item(xref->store, pdf_drop_xobject, dict);
- pdf_drop_xobject(form);
+ pdf_remove_item(ctx, xref->store, (pdf_store_drop_fn *)pdf_drop_xobject, dict);
+ pdf_drop_xobject(ctx, form);
return fz_error_note(error, "cannot load xobject content stream (%d %d R)", fz_to_num(dict), fz_to_gen(dict));
}
@@ -81,16 +82,16 @@ pdf_keep_xobject(pdf_xobject *xobj)
}
void
-pdf_drop_xobject(pdf_xobject *xobj)
+pdf_drop_xobject(fz_context *ctx, pdf_xobject *xobj)
{
if (xobj && --xobj->refs == 0)
{
if (xobj->colorspace)
- fz_drop_colorspace(xobj->colorspace);
+ fz_drop_colorspace(ctx, xobj->colorspace);
if (xobj->resources)
- fz_drop_obj(xobj->resources);
+ fz_drop_obj(ctx, xobj->resources);
if (xobj->contents)
- fz_drop_buffer(xobj->contents);
- fz_free(xobj);
+ fz_drop_buffer(ctx, xobj->contents);
+ fz_free(ctx, xobj);
}
}
diff --git a/pdf/pdf_xref.c b/pdf/pdf_xref.c
index 2411ae99..d9691be5 100644
--- a/pdf/pdf_xref.c
+++ b/pdf/pdf_xref.c
@@ -172,7 +172,7 @@ pdf_resize_xref(pdf_xref *xref, int newlen)
{
int i;
- xref->table = fz_realloc(xref->table, newlen, sizeof(pdf_xref_entry));
+ xref->table = fz_realloc(xref->ctx, xref->table, newlen * sizeof(pdf_xref_entry));
for (i = xref->len; i < newlen; i++)
{
xref->table[i].type = 0;
@@ -311,18 +311,19 @@ pdf_read_new_xref(fz_obj **trailerp, pdf_xref *xref, char *buf, int cap)
int num, gen, stm_ofs;
int size, w0, w1, w2;
int t;
+ fz_context *ctx = xref->ctx;
error = pdf_parse_ind_obj(&trailer, xref, xref->file, buf, cap, &num, &gen, &stm_ofs);
if (error)
return fz_error_note(error, "cannot parse compressed xref stream object");
- obj = fz_dict_gets(trailer, "Size");
+ obj = fz_dict_gets(ctx, trailer, "Size");
if (!obj)
{
- fz_drop_obj(trailer);
+ fz_drop_obj(ctx, trailer);
return fz_error_make("xref stream missing Size entry (%d %d R)", num, gen);
}
- size = fz_to_int(obj);
+ size = fz_to_int(ctx, obj);
if (size > xref->len)
{
@@ -331,25 +332,25 @@ pdf_read_new_xref(fz_obj **trailerp, pdf_xref *xref, char *buf, int cap)
if (num < 0 || num >= xref->len)
{
- fz_drop_obj(trailer);
+ fz_drop_obj(ctx, trailer);
return fz_error_make("object id (%d %d R) out of range (0..%d)", num, gen, xref->len - 1);
}
- obj = fz_dict_gets(trailer, "W");
+ obj = fz_dict_gets(ctx, trailer, "W");
if (!obj) {
- fz_drop_obj(trailer);
+ fz_drop_obj(ctx, trailer);
return fz_error_make("xref stream missing W entry (%d %d R)", num, gen);
}
- w0 = fz_to_int(fz_array_get(obj, 0));
- w1 = fz_to_int(fz_array_get(obj, 1));
- w2 = fz_to_int(fz_array_get(obj, 2));
+ w0 = fz_to_int(ctx, fz_array_get(ctx, obj, 0));
+ w1 = fz_to_int(ctx, fz_array_get(ctx, obj, 1));
+ w2 = fz_to_int(ctx, fz_array_get(ctx, obj, 2));
- index = fz_dict_gets(trailer, "Index");
+ index = fz_dict_gets(ctx, trailer, "Index");
error = pdf_open_stream_at(&stm, xref, num, gen, trailer, stm_ofs);
if (error)
{
- fz_drop_obj(trailer);
+ fz_drop_obj(ctx, trailer);
return fz_error_note(error, "cannot open compressed xref stream (%d %d R)", num, gen);
}
@@ -359,21 +360,22 @@ pdf_read_new_xref(fz_obj **trailerp, pdf_xref *xref, char *buf, int cap)
if (error)
{
fz_close(stm);
- fz_drop_obj(trailer);
+ fz_drop_obj(ctx, trailer);
return fz_error_note(error, "cannot read xref stream (%d %d R)", num, gen);
}
}
else
{
- for (t = 0; t < fz_array_len(index); t += 2)
+ int n = fz_array_len(ctx, index);
+ for (t = 0; t < n; t += 2)
{
- int i0 = fz_to_int(fz_array_get(index, t + 0));
- int i1 = fz_to_int(fz_array_get(index, t + 1));
+ int i0 = fz_to_int(ctx, fz_array_get(ctx, index, t + 0));
+ int i1 = fz_to_int(ctx, fz_array_get(ctx, index, t + 1));
error = pdf_read_new_xref_section(xref, stm, i0, i1, w0, w1, w2);
if (error)
{
fz_close(stm);
- fz_drop_obj(trailer);
+ fz_drop_obj(ctx, trailer);
return fz_error_note(error, "cannot read xref stream section (%d %d R)", num, gen);
}
}
@@ -425,35 +427,36 @@ pdf_read_xref_sections(pdf_xref *xref, int ofs, char *buf, int cap)
fz_obj *trailer;
fz_obj *prev;
fz_obj *xrefstm;
+ fz_context *ctx = xref->ctx;
error = pdf_read_xref(&trailer, xref, ofs, buf, cap);
if (error)
return fz_error_note(error, "cannot read xref section");
/* FIXME: do we overwrite free entries properly? */
- xrefstm = fz_dict_gets(trailer, "XRefStm");
+ xrefstm = fz_dict_gets(ctx, trailer, "XRefStm");
if (xrefstm)
{
- error = pdf_read_xref_sections(xref, fz_to_int(xrefstm), buf, cap);
+ error = pdf_read_xref_sections(xref, fz_to_int(ctx, xrefstm), buf, cap);
if (error)
{
- fz_drop_obj(trailer);
+ fz_drop_obj(ctx, trailer);
return fz_error_note(error, "cannot read /XRefStm xref section");
}
}
- prev = fz_dict_gets(trailer, "Prev");
+ prev = fz_dict_gets(ctx, trailer, "Prev");
if (prev)
{
- error = pdf_read_xref_sections(xref, fz_to_int(prev), buf, cap);
+ error = pdf_read_xref_sections(xref, fz_to_int(ctx, prev), buf, cap);
if (error)
{
- fz_drop_obj(trailer);
+ fz_drop_obj(ctx, trailer);
return fz_error_note(error, "cannot read /Prev xref section");
}
}
- fz_drop_obj(trailer);
+ fz_drop_obj(ctx, trailer);
return fz_okay;
}
@@ -480,11 +483,11 @@ pdf_load_xref(pdf_xref *xref, char *buf, int bufsize)
if (error)
return fz_error_note(error, "cannot read trailer");
- size = fz_dict_gets(xref->trailer, "Size");
+ size = fz_dict_gets(xref->ctx, xref->trailer, "Size");
if (!size)
return fz_error_make("trailer missing Size entry");
- pdf_resize_xref(xref, fz_to_int(size));
+ pdf_resize_xref(xref, fz_to_int(xref->ctx, size));
error = pdf_read_xref_sections(xref, xref->startxref, buf, bufsize);
if (error)
@@ -502,7 +505,7 @@ pdf_load_xref(pdf_xref *xref, char *buf, int bufsize)
return fz_error_make("object offset out of range: %d (%d 0 R)", xref->table[i].ofs, i);
if (xref->table[i].type == 'o')
if (xref->table[i].ofs <= 0 || xref->table[i].ofs >= xref->len || xref->table[xref->table[i].ofs].type != 'n')
- return error_make("invalid reference to an objstm that does not exist: %d (%d 0 R)", xref->table[i].ofs, i);
+ return fz_error_make("invalid reference to an objstm that does not exist: %d (%d 0 R)", xref->table[i].ofs, i);
}
return fz_okay;
@@ -521,15 +524,15 @@ pdf_open_xref_with_stream(pdf_xref **xrefp, fz_stream *file, char *password)
fz_obj *encrypt, *id;
fz_obj *dict, *obj;
int i, repaired = 0;
+ fz_context *ctx = file->ctx;
/* install pdf specific callback */
- fz_resolve_indirect = pdf_resolve_indirect;
+ ctx->fz_resolve_indirect = pdf_resolve_indirect;
- xref = fz_malloc(sizeof(pdf_xref));
-
- memset(xref, 0, sizeof(pdf_xref));
+ xref = fz_calloc(ctx, 1, sizeof(pdf_xref));
xref->file = fz_keep_stream(file);
+ xref->ctx = ctx;
error = pdf_load_xref(xref, xref->scratch, sizeof xref->scratch);
if (error)
@@ -537,13 +540,13 @@ pdf_open_xref_with_stream(pdf_xref **xrefp, fz_stream *file, char *password)
fz_error_handle(error, "trying to repair");
if (xref->table)
{
- fz_free(xref->table);
+ fz_free(xref->ctx, xref->table);
xref->table = NULL;
xref->len = 0;
}
if (xref->trailer)
{
- fz_drop_obj(xref->trailer);
+ fz_drop_obj(ctx, xref->trailer);
xref->trailer = NULL;
}
error = pdf_repair_xref(xref, xref->scratch, sizeof xref->scratch);
@@ -555,11 +558,11 @@ pdf_open_xref_with_stream(pdf_xref **xrefp, fz_stream *file, char *password)
repaired = 1;
}
- encrypt = fz_dict_gets(xref->trailer, "Encrypt");
- id = fz_dict_gets(xref->trailer, "ID");
- if (fz_is_dict(encrypt))
+ encrypt = fz_dict_gets(ctx, xref->trailer, "Encrypt");
+ id = fz_dict_gets(ctx, xref->trailer, "ID");
+ if (fz_is_dict(ctx, encrypt))
{
- error = pdf_new_crypt(&xref->crypt, encrypt, id);
+ error = pdf_new_crypt(ctx, &xref->crypt, encrypt, id);
if (error)
{
pdf_free_xref(xref);
@@ -592,8 +595,8 @@ pdf_open_xref_with_stream(pdf_xref **xrefp, fz_stream *file, char *password)
return fz_error_note(error, "cannot repair document");
}
- hasroot = fz_dict_gets(xref->trailer, "Root") != NULL;
- hasinfo = fz_dict_gets(xref->trailer, "Info") != NULL;
+ hasroot = fz_dict_gets(ctx, xref->trailer, "Root") != NULL;
+ hasinfo = fz_dict_gets(ctx, xref->trailer, "Info") != NULL;
for (i = 1; i < xref->len; i++)
{
@@ -609,26 +612,26 @@ pdf_open_xref_with_stream(pdf_xref **xrefp, fz_stream *file, char *password)
if (!hasroot)
{
- obj = fz_dict_gets(dict, "Type");
- if (fz_is_name(obj) && !strcmp(fz_to_name(obj), "Catalog"))
+ obj = fz_dict_gets(ctx, dict, "Type");
+ if (fz_is_name(ctx, obj) && !strcmp(fz_to_name(ctx, obj), "Catalog"))
{
- obj = fz_new_indirect(i, 0, xref);
- fz_dict_puts(xref->trailer, "Root", obj);
- fz_drop_obj(obj);
+ obj = fz_new_indirect(ctx, i, 0, xref);
+ fz_dict_puts(ctx, xref->trailer, "Root", obj);
+ fz_drop_obj(ctx, obj);
}
}
if (!hasinfo)
{
- if (fz_dict_gets(dict, "Creator") || fz_dict_gets(dict, "Producer"))
+ if (fz_dict_gets(ctx, dict, "Creator") || fz_dict_gets(ctx, dict, "Producer"))
{
- obj = fz_new_indirect(i, 0, xref);
- fz_dict_puts(xref->trailer, "Info", obj);
- fz_drop_obj(obj);
+ obj = fz_new_indirect(ctx, i, 0, xref);
+ fz_dict_puts(ctx, xref->trailer, "Info", obj);
+ fz_drop_obj(ctx, obj);
}
}
- fz_drop_obj(dict);
+ fz_drop_obj(ctx, dict);
}
}
@@ -640,9 +643,10 @@ void
pdf_free_xref(pdf_xref *xref)
{
int i;
+ fz_context *ctx = xref->ctx;
if (xref->store)
- pdf_free_store(xref->store);
+ pdf_free_store(ctx, xref->store);
if (xref->table)
{
@@ -650,35 +654,35 @@ pdf_free_xref(pdf_xref *xref)
{
if (xref->table[i].obj)
{
- fz_drop_obj(xref->table[i].obj);
+ fz_drop_obj(ctx, xref->table[i].obj);
xref->table[i].obj = NULL;
}
}
- fz_free(xref->table);
+ fz_free(xref->ctx, xref->table);
}
if (xref->page_objs)
{
for (i = 0; i < xref->page_len; i++)
- fz_drop_obj(xref->page_objs[i]);
- fz_free(xref->page_objs);
+ fz_drop_obj(ctx, xref->page_objs[i]);
+ fz_free(ctx, xref->page_objs);
}
if (xref->page_refs)
{
for (i = 0; i < xref->page_len; i++)
- fz_drop_obj(xref->page_refs[i]);
- fz_free(xref->page_refs);
+ fz_drop_obj(ctx, xref->page_refs[i]);
+ fz_free(ctx, xref->page_refs);
}
if (xref->file)
fz_close(xref->file);
if (xref->trailer)
- fz_drop_obj(xref->trailer);
+ fz_drop_obj(ctx, xref->trailer);
if (xref->crypt)
- pdf_free_crypt(xref->crypt);
+ pdf_free_crypt(ctx, xref->crypt);
- fz_free(xref);
+ fz_free(ctx, xref);
}
void
@@ -714,16 +718,17 @@ pdf_load_obj_stm(pdf_xref *xref, int num, int gen, char *buf, int cap)
int count;
int i, n;
int tok;
+ fz_context *ctx = xref->ctx;
error = pdf_load_object(&objstm, xref, num, gen);
if (error)
return fz_error_note(error, "cannot load object stream object (%d %d R)", num, gen);
- count = fz_to_int(fz_dict_gets(objstm, "N"));
- first = fz_to_int(fz_dict_gets(objstm, "First"));
+ count = fz_to_int(ctx, fz_dict_gets(ctx, objstm, "N"));
+ first = fz_to_int(ctx, fz_dict_gets(ctx, objstm, "First"));
- numbuf = fz_calloc(count, sizeof(int));
- ofsbuf = fz_calloc(count, sizeof(int));
+ numbuf = fz_calloc(ctx, count, sizeof(int));
+ ofsbuf = fz_calloc(ctx, count, sizeof(int));
error = pdf_open_stream(&stm, xref, num, gen);
if (error)
@@ -766,7 +771,7 @@ pdf_load_obj_stm(pdf_xref *xref, int num, int gen, char *buf, int cap)
if (numbuf[i] < 1 || numbuf[i] >= xref->len)
{
- fz_drop_obj(obj);
+ fz_drop_obj(ctx, obj);
error = fz_error_make("object id (%d 0 R) out of range (0..%d)", numbuf[i], xref->len - 1);
goto cleanupstm;
}
@@ -774,27 +779,27 @@ pdf_load_obj_stm(pdf_xref *xref, int num, int gen, char *buf, int cap)
if (xref->table[numbuf[i]].type == 'o' && xref->table[numbuf[i]].ofs == num)
{
if (xref->table[numbuf[i]].obj)
- fz_drop_obj(xref->table[numbuf[i]].obj);
+ fz_drop_obj(ctx, xref->table[numbuf[i]].obj);
xref->table[numbuf[i]].obj = obj;
}
else
{
- fz_drop_obj(obj);
+ fz_drop_obj(ctx, obj);
}
}
fz_close(stm);
- fz_free(ofsbuf);
- fz_free(numbuf);
- fz_drop_obj(objstm);
+ fz_free(xref->ctx, ofsbuf);
+ fz_free(xref->ctx, numbuf);
+ fz_drop_obj(ctx, objstm);
return fz_okay;
cleanupstm:
fz_close(stm);
cleanupbuf:
- fz_free(ofsbuf);
- fz_free(numbuf);
- fz_drop_obj(objstm);
+ fz_free(xref->ctx, ofsbuf);
+ fz_free(xref->ctx, numbuf);
+ fz_drop_obj(ctx, objstm);
return error; /* already rethrown */
}
@@ -808,6 +813,7 @@ pdf_cache_object(pdf_xref *xref, int num, int gen)
fz_error error;
pdf_xref_entry *x;
int rnum, rgen;
+ fz_context *ctx = xref->ctx;
if (num < 0 || num >= xref->len)
return fz_error_make("object out of range (%d %d R); xref size %d", num, gen, xref->len);
@@ -819,7 +825,7 @@ pdf_cache_object(pdf_xref *xref, int num, int gen)
if (x->type == 'f')
{
- x->obj = fz_new_null();
+ x->obj = fz_new_null(ctx);
return fz_okay;
}
else if (x->type == 'n')
@@ -835,7 +841,7 @@ pdf_cache_object(pdf_xref *xref, int num, int gen)
return fz_error_make("found object (%d %d R) instead of (%d %d R)", rnum, rgen, num, gen);
if (xref->crypt)
- pdf_crypt_obj(xref->crypt, x->obj, num, gen);
+ pdf_crypt_obj(ctx, xref->crypt, x->obj, num, gen);
}
else if (x->type == 'o')
{
@@ -910,7 +916,7 @@ pdf_update_object(pdf_xref *xref, int num, int gen, fz_obj *newobj)
x = &xref->table[num];
if (x->obj)
- fz_drop_obj(x->obj);
+ fz_drop_obj(xref->ctx, x->obj);
x->obj = fz_keep_obj(newobj);
x->type = 'n';
@@ -922,12 +928,12 @@ pdf_update_object(pdf_xref *xref, int num, int gen, fz_obj *newobj)
*/
fz_error
-pdf_open_xref(pdf_xref **xrefp, const char *filename, char *password)
+pdf_open_xref(fz_context *ctx, pdf_xref **xrefp, const char *filename, char *password)
{
fz_error error;
fz_stream *file;
- file = fz_open_file(filename);
+ file = fz_open_file(ctx, filename);
if (!file)
return fz_error_make("cannot open file '%s': %s", filename, strerror(errno));
diff --git a/scripts/cmapdump.c b/scripts/cmapdump.c
index b1e91f9b..06e74beb 100644
--- a/scripts/cmapdump.c
+++ b/scripts/cmapdump.c
@@ -15,6 +15,8 @@
#include "../fitz/stm_buffer.c"
#include "../fitz/stm_open.c"
#include "../fitz/stm_read.c"
+#include "../fitz/context.c"
+#include "../fitz/except.c"
#include "../pdf/pdf_lex.c"
#include "../pdf/pdf_cmap.c"
@@ -41,6 +43,7 @@ main(int argc, char **argv)
char name[256];
char *realname;
int i, k;
+ fz_context *ctx;
if (argc < 3)
{
@@ -48,6 +51,13 @@ main(int argc, char **argv)
return 1;
}
+ ctx = fz_context_init(&fz_alloc_default);
+ if (ctx == NULL)
+ {
+ fprintf(stderr, "failed to initialise!\n");
+ return 1;
+ }
+
fo = fopen(argv[1], "wb");
if (!fo)
{
@@ -76,7 +86,7 @@ main(int argc, char **argv)
strcpy(name, realname);
clean(name);
- fi = fz_open_file(argv[i]);
+ fi = fz_open_file(ctx, argv[i]);
if (!fi)
fz_error_make("cmapdump: could not open input file '%s'\n", argv[i]);
@@ -156,5 +166,6 @@ main(int argc, char **argv)
return 1;
}
+ fz_context_fin(ctx);
return 0;
}
diff --git a/win32/libmupdf.vcproj b/win32/libmupdf.vcproj
index 88af36ef..21852ea0 100644
--- a/win32/libmupdf.vcproj
+++ b/win32/libmupdf.vcproj
@@ -42,6 +42,7 @@
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="..\scripts;..\fitz;..\pdf;..\thirdparty\jbig2dec;..\thirdparty\jpeg-8c;..\thirdparty\openjpeg-1.4\libopenjpeg;..\thirdparty\zlib-1.2.5;..\thirdparty\freetype-2.4.4\include"
+ PreprocessorDefinitions="DEBUG=1"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="1"
@@ -139,6 +140,69 @@
Name="VCPostBuildEventTool"
/>
</Configuration>
+ <Configuration
+ Name="Memento|Win32"
+ OutputDirectory="$(ConfigurationName)"
+ IntermediateDirectory="$(ConfigurationName)\$(ProjectName)"
+ ConfigurationType="4"
+ CharacterSet="2"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ Description="Generate CMap and Font source files"
+ CommandLine="generate.bat"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ Optimization="0"
+ AdditionalIncludeDirectories="..\scripts;..\fitz;..\pdf;..\thirdparty\jbig2dec;..\thirdparty\jpeg-8c;..\thirdparty\openjpeg-1.4\libopenjpeg;..\thirdparty\zlib-1.2.5;..\thirdparty\freetype-2.4.4\include"
+ PreprocessorDefinitions="MEMENTO=1;DEBUG=1"
+ MinimalRebuild="true"
+ BasicRuntimeChecks="3"
+ RuntimeLibrary="1"
+ WarningLevel="3"
+ DebugInformationFormat="4"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLibrarianTool"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
</Configurations>
<References>
</References>
@@ -307,6 +371,10 @@
>
</File>
<File
+ RelativePath="..\fitz\context.c"
+ >
+ </File>
+ <File
RelativePath="..\fitz\crypt_aes.c"
>
</File>
@@ -343,6 +411,18 @@
>
</File>
<File
+ RelativePath="..\fitz\except.c"
+ >
+ </File>
+ <File
+ RelativePath="..\fitz\except.h"
+ >
+ </File>
+ <File
+ RelativePath="..\fitz\exceptxxx.h"
+ >
+ </File>
+ <File
RelativePath="..\fitz\filt_basic.c"
>
</File>
@@ -379,6 +459,14 @@
>
</File>
<File
+ RelativePath="..\fitz\memento.c"
+ >
+ </File>
+ <File
+ RelativePath="..\fitz\memento.h"
+ >
+ </File>
+ <File
RelativePath="..\fitz\obj_print.c"
>
</File>
diff --git a/win32/libthirdparty.vcproj b/win32/libthirdparty.vcproj
index 6b3cc70b..703b0eb2 100644
--- a/win32/libthirdparty.vcproj
+++ b/win32/libthirdparty.vcproj
@@ -40,7 +40,7 @@
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="..\scripts;..\thirdparty\jbig2dec;..\thirdparty\jpeg-8c;..\thirdparty\openjpeg-1.4\libopenjpeg;..\thirdparty\zlib-1.2.5;..\thirdparty\freetype-2.4.4\include"
- PreprocessorDefinitions="_CRT_SECURE_NO_WARNINGS;FT2_BUILD_LIBRARY;OPJ_STATIC;FT_CONFIG_MODULES_H=\&quot;slimftmodules.h\&quot;;FT_CONFIG_OPTIONS_H=\&quot;slimftoptions.h\&quot;"
+ PreprocessorDefinitions="_CRT_SECURE_NO_WARNINGS;FT2_BUILD_LIBRARY;OPJ_STATIC;FT_CONFIG_MODULES_H=\&quot;slimftmodules.h\&quot;;FT_CONFIG_OPTIONS_H=\&quot;slimftoptions.h\&quot;;DEBUG=1"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="1"
@@ -137,6 +137,67 @@
Name="VCPostBuildEventTool"
/>
</Configuration>
+ <Configuration
+ Name="Memento|Win32"
+ OutputDirectory="$(ConfigurationName)"
+ IntermediateDirectory="$(ConfigurationName)\$(ProjectName)"
+ ConfigurationType="4"
+ CharacterSet="2"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ Optimization="0"
+ AdditionalIncludeDirectories="..\scripts;..\thirdparty\jbig2dec;..\thirdparty\jpeg-8c;..\thirdparty\openjpeg-1.4\libopenjpeg;..\thirdparty\zlib-1.2.5;..\thirdparty\freetype-2.4.4\include"
+ PreprocessorDefinitions="_CRT_SECURE_NO_WARNINGS;FT2_BUILD_LIBRARY;OPJ_STATIC;FT_CONFIG_MODULES_H=\&quot;slimftmodules.h\&quot;;FT_CONFIG_OPTIONS_H=\&quot;slimftoptions.h\&quot;;MEMENTO=1;DEBUG=1"
+ MinimalRebuild="true"
+ BasicRuntimeChecks="3"
+ RuntimeLibrary="1"
+ WarningLevel="3"
+ DebugInformationFormat="4"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLibrarianTool"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
</Configurations>
<References>
</References>
diff --git a/win32/mupdf.sln b/win32/mupdf.sln
index 54793c52..ba2875a6 100644
--- a/win32/mupdf.sln
+++ b/win32/mupdf.sln
@@ -1,6 +1,6 @@

Microsoft Visual Studio Solution File, Format Version 9.00
-# Visual C++ Express 2005
+# Visual Studio 2005
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mupdf", "mupdf.vcproj", "{E74F29F0-FA43-4ADC-B92C-6AFA08E4A417}"
ProjectSection(ProjectDependencies) = postProject
{5F615F91-DFF8-4F05-BF48-6222B7D86519} = {5F615F91-DFF8-4F05-BF48-6222B7D86519}
@@ -44,39 +44,56 @@ EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
+ Memento|Win32 = Memento|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E74F29F0-FA43-4ADC-B92C-6AFA08E4A417}.Debug|Win32.ActiveCfg = Debug|Win32
{E74F29F0-FA43-4ADC-B92C-6AFA08E4A417}.Debug|Win32.Build.0 = Debug|Win32
+ {E74F29F0-FA43-4ADC-B92C-6AFA08E4A417}.Memento|Win32.ActiveCfg = Memento|Win32
+ {E74F29F0-FA43-4ADC-B92C-6AFA08E4A417}.Memento|Win32.Build.0 = Memento|Win32
{E74F29F0-FA43-4ADC-B92C-6AFA08E4A417}.Release|Win32.ActiveCfg = Release|Win32
{E74F29F0-FA43-4ADC-B92C-6AFA08E4A417}.Release|Win32.Build.0 = Release|Win32
{5EDCF4FD-0291-4FB9-8D96-D58957CA5E3C}.Debug|Win32.ActiveCfg = Debug|Win32
{5EDCF4FD-0291-4FB9-8D96-D58957CA5E3C}.Debug|Win32.Build.0 = Debug|Win32
+ {5EDCF4FD-0291-4FB9-8D96-D58957CA5E3C}.Memento|Win32.ActiveCfg = Memento|Win32
+ {5EDCF4FD-0291-4FB9-8D96-D58957CA5E3C}.Memento|Win32.Build.0 = Memento|Win32
{5EDCF4FD-0291-4FB9-8D96-D58957CA5E3C}.Release|Win32.ActiveCfg = Release|Win32
{5EDCF4FD-0291-4FB9-8D96-D58957CA5E3C}.Release|Win32.Build.0 = Release|Win32
{5F615F91-DFF8-4F05-BF48-6222B7D86519}.Debug|Win32.ActiveCfg = Debug|Win32
{5F615F91-DFF8-4F05-BF48-6222B7D86519}.Debug|Win32.Build.0 = Debug|Win32
+ {5F615F91-DFF8-4F05-BF48-6222B7D86519}.Memento|Win32.ActiveCfg = Memento|Win32
+ {5F615F91-DFF8-4F05-BF48-6222B7D86519}.Memento|Win32.Build.0 = Memento|Win32
{5F615F91-DFF8-4F05-BF48-6222B7D86519}.Release|Win32.ActiveCfg = Release|Win32
{5F615F91-DFF8-4F05-BF48-6222B7D86519}.Release|Win32.Build.0 = Release|Win32
{26C5D7C3-BEE9-4886-9CDB-846D26F5BCDB}.Debug|Win32.ActiveCfg = Debug|Win32
{26C5D7C3-BEE9-4886-9CDB-846D26F5BCDB}.Debug|Win32.Build.0 = Debug|Win32
+ {26C5D7C3-BEE9-4886-9CDB-846D26F5BCDB}.Memento|Win32.ActiveCfg = Memento|Win32
+ {26C5D7C3-BEE9-4886-9CDB-846D26F5BCDB}.Memento|Win32.Build.0 = Memento|Win32
{26C5D7C3-BEE9-4886-9CDB-846D26F5BCDB}.Release|Win32.ActiveCfg = Release|Win32
{26C5D7C3-BEE9-4886-9CDB-846D26F5BCDB}.Release|Win32.Build.0 = Release|Win32
{923D7C3D-A5CD-47F1-9BB3-F716531DCCCE}.Debug|Win32.ActiveCfg = Debug|Win32
{923D7C3D-A5CD-47F1-9BB3-F716531DCCCE}.Debug|Win32.Build.0 = Debug|Win32
+ {923D7C3D-A5CD-47F1-9BB3-F716531DCCCE}.Memento|Win32.ActiveCfg = Memento|Win32
+ {923D7C3D-A5CD-47F1-9BB3-F716531DCCCE}.Memento|Win32.Build.0 = Memento|Win32
{923D7C3D-A5CD-47F1-9BB3-F716531DCCCE}.Release|Win32.ActiveCfg = Release|Win32
{923D7C3D-A5CD-47F1-9BB3-F716531DCCCE}.Release|Win32.Build.0 = Release|Win32
{50644121-C85F-4EE9-9C54-F7D1BDFAE354}.Debug|Win32.ActiveCfg = Debug|Win32
{50644121-C85F-4EE9-9C54-F7D1BDFAE354}.Debug|Win32.Build.0 = Debug|Win32
+ {50644121-C85F-4EE9-9C54-F7D1BDFAE354}.Memento|Win32.ActiveCfg = Memento|Win32
+ {50644121-C85F-4EE9-9C54-F7D1BDFAE354}.Memento|Win32.Build.0 = Memento|Win32
{50644121-C85F-4EE9-9C54-F7D1BDFAE354}.Release|Win32.ActiveCfg = Release|Win32
{50644121-C85F-4EE9-9C54-F7D1BDFAE354}.Release|Win32.Build.0 = Release|Win32
{BD4473E7-2DBE-4568-A0FC-38EED70182DC}.Debug|Win32.ActiveCfg = Debug|Win32
{BD4473E7-2DBE-4568-A0FC-38EED70182DC}.Debug|Win32.Build.0 = Debug|Win32
+ {BD4473E7-2DBE-4568-A0FC-38EED70182DC}.Memento|Win32.ActiveCfg = Memento|Win32
+ {BD4473E7-2DBE-4568-A0FC-38EED70182DC}.Memento|Win32.Build.0 = Memento|Win32
{BD4473E7-2DBE-4568-A0FC-38EED70182DC}.Release|Win32.ActiveCfg = Release|Win32
{BD4473E7-2DBE-4568-A0FC-38EED70182DC}.Release|Win32.Build.0 = Release|Win32
{E6A93617-41BD-45B2-BF61-4F7ED263CDF2}.Debug|Win32.ActiveCfg = Debug|Win32
{E6A93617-41BD-45B2-BF61-4F7ED263CDF2}.Debug|Win32.Build.0 = Debug|Win32
+ {E6A93617-41BD-45B2-BF61-4F7ED263CDF2}.Memento|Win32.ActiveCfg = Memento|Win32
+ {E6A93617-41BD-45B2-BF61-4F7ED263CDF2}.Memento|Win32.Build.0 = Memento|Win32
{E6A93617-41BD-45B2-BF61-4F7ED263CDF2}.Release|Win32.ActiveCfg = Release|Win32
{E6A93617-41BD-45B2-BF61-4F7ED263CDF2}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
diff --git a/win32/mupdf.vcproj b/win32/mupdf.vcproj
index b0e899e1..9de7ca25 100644
--- a/win32/mupdf.vcproj
+++ b/win32/mupdf.vcproj
@@ -5,7 +5,6 @@
Name="mupdf"
ProjectGUID="{E74F29F0-FA43-4ADC-B92C-6AFA08E4A417}"
RootNamespace="mupdf"
- TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
@@ -41,7 +40,7 @@
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="..\fitz;..\pdf;..\xps"
- PreprocessorDefinitions="FT2_BUILD_LIBRARY;OPJ_STATIC"
+ PreprocessorDefinitions="FT2_BUILD_LIBRARY;OPJ_STATIC;DEBUG=1"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="1"
@@ -81,6 +80,9 @@
Name="VCAppVerifierTool"
/>
<Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
@@ -152,6 +154,81 @@
Name="VCAppVerifierTool"
/>
<Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
+ <Configuration
+ Name="Memento|Win32"
+ OutputDirectory="$(ConfigurationName)"
+ IntermediateDirectory="$(ConfigurationName)\$(ProjectName)"
+ ConfigurationType="1"
+ CharacterSet="2"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ Optimization="0"
+ AdditionalIncludeDirectories="..\fitz;..\pdf;..\xps"
+ PreprocessorDefinitions="FT2_BUILD_LIBRARY;OPJ_STATIC;MEMENTO=1;DEBUG=1"
+ MinimalRebuild="true"
+ BasicRuntimeChecks="3"
+ RuntimeLibrary="1"
+ WarningLevel="3"
+ DebugInformationFormat="4"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLinkerTool"
+ GenerateDebugInformation="true"
+ TargetMachine="1"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCManifestTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCAppVerifierTool"
+ />
+ <Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
diff --git a/win32/pdfclean.vcproj b/win32/pdfclean.vcproj
index 2c6e5655..b3bdcb6c 100644
--- a/win32/pdfclean.vcproj
+++ b/win32/pdfclean.vcproj
@@ -5,7 +5,6 @@
Name="pdfclean"
ProjectGUID="{923D7C3D-A5CD-47F1-9BB3-F716531DCCCE}"
RootNamespace="mupdf"
- TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
@@ -41,6 +40,7 @@
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="..\fitz;..\pdf"
+ PreprocessorDefinitions="DEBUG=1"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="1"
@@ -80,6 +80,9 @@
Name="VCAppVerifierTool"
/>
<Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
@@ -151,6 +154,81 @@
Name="VCAppVerifierTool"
/>
<Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
+ <Configuration
+ Name="Memento|Win32"
+ OutputDirectory="$(ConfigurationName)"
+ IntermediateDirectory="$(ConfigurationName)\$(ProjectName)"
+ ConfigurationType="1"
+ CharacterSet="2"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ Optimization="0"
+ AdditionalIncludeDirectories="..\fitz;..\pdf"
+ PreprocessorDefinitions="MEMENTO=1;DEBUG=1"
+ MinimalRebuild="true"
+ BasicRuntimeChecks="3"
+ RuntimeLibrary="1"
+ WarningLevel="3"
+ DebugInformationFormat="4"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLinkerTool"
+ GenerateDebugInformation="true"
+ TargetMachine="1"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCManifestTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCAppVerifierTool"
+ />
+ <Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
diff --git a/win32/pdfdraw.vcproj b/win32/pdfdraw.vcproj
index 769c2b19..5ae5bdfe 100644
--- a/win32/pdfdraw.vcproj
+++ b/win32/pdfdraw.vcproj
@@ -5,7 +5,6 @@
Name="pdfdraw"
ProjectGUID="{26C5D7C3-BEE9-4886-9CDB-846D26F5BCDB}"
RootNamespace="mupdf"
- TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
@@ -41,6 +40,7 @@
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="..\fitz;..\pdf"
+ PreprocessorDefinitions="DEBUG=1"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="1"
@@ -80,6 +80,9 @@
Name="VCAppVerifierTool"
/>
<Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
@@ -151,6 +154,81 @@
Name="VCAppVerifierTool"
/>
<Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
+ <Configuration
+ Name="Memento|Win32"
+ OutputDirectory="$(ConfigurationName)"
+ IntermediateDirectory="$(ConfigurationName)\$(ProjectName)"
+ ConfigurationType="1"
+ CharacterSet="2"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ Optimization="0"
+ AdditionalIncludeDirectories="..\fitz;..\pdf"
+ PreprocessorDefinitions="MEMENTO=1;DEBUG=1"
+ MinimalRebuild="true"
+ BasicRuntimeChecks="3"
+ RuntimeLibrary="1"
+ WarningLevel="3"
+ DebugInformationFormat="4"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLinkerTool"
+ GenerateDebugInformation="true"
+ TargetMachine="1"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCManifestTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCAppVerifierTool"
+ />
+ <Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
diff --git a/win32/pdfextract.vcproj b/win32/pdfextract.vcproj
index 91e975f9..1d14b6c6 100644
--- a/win32/pdfextract.vcproj
+++ b/win32/pdfextract.vcproj
@@ -5,7 +5,6 @@
Name="pdfextract"
ProjectGUID="{BD4473E7-2DBE-4568-A0FC-38EED70182DC}"
RootNamespace="mupdf"
- TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
@@ -41,6 +40,7 @@
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="..\fitz;..\pdf"
+ PreprocessorDefinitions="DEBUG=1"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="1"
@@ -80,6 +80,9 @@
Name="VCAppVerifierTool"
/>
<Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
@@ -151,6 +154,81 @@
Name="VCAppVerifierTool"
/>
<Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
+ <Configuration
+ Name="Memento|Win32"
+ OutputDirectory="$(ConfigurationName)"
+ IntermediateDirectory="$(ConfigurationName)\$(ProjectName)"
+ ConfigurationType="1"
+ CharacterSet="2"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ Optimization="0"
+ AdditionalIncludeDirectories="..\fitz;..\pdf"
+ PreprocessorDefinitions="MEMENTO=1;DEBUG=1"
+ MinimalRebuild="true"
+ BasicRuntimeChecks="3"
+ RuntimeLibrary="1"
+ WarningLevel="3"
+ DebugInformationFormat="4"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLinkerTool"
+ GenerateDebugInformation="true"
+ TargetMachine="1"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCManifestTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCAppVerifierTool"
+ />
+ <Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
diff --git a/win32/pdfshow.vcproj b/win32/pdfshow.vcproj
index a115fd20..30fd1784 100644
--- a/win32/pdfshow.vcproj
+++ b/win32/pdfshow.vcproj
@@ -5,7 +5,6 @@
Name="pdfshow"
ProjectGUID="{50644121-C85F-4EE9-9C54-F7D1BDFAE354}"
RootNamespace="mupdf"
- TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
@@ -41,6 +40,7 @@
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="..\fitz;..\pdf"
+ PreprocessorDefinitions="DEBUG=1"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="1"
@@ -80,6 +80,9 @@
Name="VCAppVerifierTool"
/>
<Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
@@ -151,6 +154,81 @@
Name="VCAppVerifierTool"
/>
<Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
+ <Configuration
+ Name="Memento|Win32"
+ OutputDirectory="$(ConfigurationName)"
+ IntermediateDirectory="$(ConfigurationName)\$(ProjectName)"
+ ConfigurationType="1"
+ CharacterSet="2"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ Optimization="0"
+ AdditionalIncludeDirectories="..\fitz;..\pdf"
+ PreprocessorDefinitions="MEMENTO=1;DEBUG=1"
+ MinimalRebuild="true"
+ BasicRuntimeChecks="3"
+ RuntimeLibrary="1"
+ WarningLevel="3"
+ DebugInformationFormat="4"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLinkerTool"
+ GenerateDebugInformation="true"
+ TargetMachine="1"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCManifestTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCAppVerifierTool"
+ />
+ <Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
diff --git a/win32/xpsdraw.vcproj b/win32/xpsdraw.vcproj
index 52357d31..d2db91fa 100644
--- a/win32/xpsdraw.vcproj
+++ b/win32/xpsdraw.vcproj
@@ -40,6 +40,7 @@
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="..\fitz;..\xps"
+ PreprocessorDefinitions="DEBUG=1"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="1"
@@ -58,8 +59,6 @@
<Tool
Name="VCLinkerTool"
GenerateDebugInformation="true"
- RandomizedBaseAddress="1"
- DataExecutionPrevention="0"
TargetMachine="1"
/>
<Tool
@@ -81,6 +80,9 @@
Name="VCAppVerifierTool"
/>
<Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
@@ -131,8 +133,6 @@
GenerateDebugInformation="true"
OptimizeReferences="2"
EnableCOMDATFolding="2"
- RandomizedBaseAddress="1"
- DataExecutionPrevention="0"
TargetMachine="1"
/>
<Tool
@@ -154,6 +154,81 @@
Name="VCAppVerifierTool"
/>
<Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
+ Name="VCPostBuildEventTool"
+ />
+ </Configuration>
+ <Configuration
+ Name="Memento|Win32"
+ OutputDirectory="$(ConfigurationName)"
+ IntermediateDirectory="$(ConfigurationName)\$(ProjectName)"
+ ConfigurationType="1"
+ CharacterSet="2"
+ >
+ <Tool
+ Name="VCPreBuildEventTool"
+ />
+ <Tool
+ Name="VCCustomBuildTool"
+ />
+ <Tool
+ Name="VCXMLDataGeneratorTool"
+ />
+ <Tool
+ Name="VCWebServiceProxyGeneratorTool"
+ />
+ <Tool
+ Name="VCMIDLTool"
+ />
+ <Tool
+ Name="VCCLCompilerTool"
+ Optimization="0"
+ AdditionalIncludeDirectories="..\fitz;..\xps"
+ PreprocessorDefinitions="MEMENTO=1;DEBUG=1"
+ MinimalRebuild="true"
+ BasicRuntimeChecks="3"
+ RuntimeLibrary="1"
+ WarningLevel="3"
+ DebugInformationFormat="4"
+ />
+ <Tool
+ Name="VCManagedResourceCompilerTool"
+ />
+ <Tool
+ Name="VCResourceCompilerTool"
+ />
+ <Tool
+ Name="VCPreLinkEventTool"
+ />
+ <Tool
+ Name="VCLinkerTool"
+ GenerateDebugInformation="true"
+ TargetMachine="1"
+ />
+ <Tool
+ Name="VCALinkTool"
+ />
+ <Tool
+ Name="VCManifestTool"
+ />
+ <Tool
+ Name="VCXDCMakeTool"
+ />
+ <Tool
+ Name="VCBscMakeTool"
+ />
+ <Tool
+ Name="VCFxCopTool"
+ />
+ <Tool
+ Name="VCAppVerifierTool"
+ />
+ <Tool
+ Name="VCWebDeploymentTool"
+ />
+ <Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
diff --git a/xps/muxps.h b/xps/muxps.h
index 28603aea..bdae3eea 100644
--- a/xps/muxps.h
+++ b/xps/muxps.h
@@ -38,12 +38,12 @@ void xps_absolute_path(char *output, char *base_uri, char *path, int output_size
typedef struct element xml_element;
-xml_element *xml_parse_document(byte *buf, int len);
+xml_element *xml_parse_document(fz_context *ctx, byte *buf, int len);
xml_element *xml_next(xml_element *item);
xml_element *xml_down(xml_element *item);
char *xml_tag(xml_element *item);
char *xml_att(xml_element *item, const char *att);
-void xml_free_element(xml_element *item);
+void xml_free_element(fz_context *ctx, xml_element *item);
void xml_print_element(xml_element *item, int level);
/*
@@ -98,9 +98,9 @@ void xps_free_page(xps_context *ctx, xps_page *page);
* Images, fonts, and colorspaces.
*/
-int xps_decode_jpeg(fz_pixmap **imagep, byte *rbuf, int rlen);
-int xps_decode_png(fz_pixmap **imagep, byte *rbuf, int rlen);
-int xps_decode_tiff(fz_pixmap **imagep, byte *rbuf, int rlen);
+int xps_decode_jpeg(fz_context *ctx, fz_pixmap **imagep, byte *rbuf, int rlen);
+int xps_decode_png(fz_context *ctx, fz_pixmap **imagep, byte *rbuf, int rlen);
+int xps_decode_tiff(fz_context *ctx, fz_pixmap **imagep, byte *rbuf, int rlen);
typedef struct xps_font_cache_s xps_font_cache;
@@ -196,6 +196,7 @@ struct xps_entry_s
struct xps_context_s
{
+ fz_context *ctx;
char *directory;
fz_stream *file;
int zip_count;
@@ -226,7 +227,7 @@ struct xps_context_s
fz_device *dev;
};
-int xps_open_file(xps_context **ctxp, char *filename);
+int xps_open_file(fz_context *ctx, xps_context **ctxp, char *filename);
int xps_open_stream(xps_context **ctxp, fz_stream *file);
void xps_free_context(xps_context *ctx);
diff --git a/xps/xps_doc.c b/xps/xps_doc.c
index c704ec40..e6d4dca3 100644
--- a/xps/xps_doc.c
+++ b/xps/xps_doc.c
@@ -38,8 +38,8 @@ xps_add_fixed_document(xps_context *ctx, char *name)
if (!strcmp(fixdoc->name, name))
return;
- fixdoc = fz_malloc(sizeof(xps_document));
- fixdoc->name = fz_strdup(name);
+ fixdoc = fz_malloc(ctx->ctx, sizeof(xps_document));
+ fixdoc->name = fz_strdup(ctx->ctx, name);
fixdoc->next = NULL;
if (!ctx->first_fixdoc)
@@ -64,8 +64,8 @@ xps_add_fixed_page(xps_context *ctx, char *name, int width, int height)
if (!strcmp(page->name, name))
return;
- page = fz_malloc(sizeof(xps_page));
- page->name = fz_strdup(name);
+ page = fz_malloc(ctx->ctx, sizeof(xps_page));
+ page->name = fz_strdup(ctx->ctx, name);
page->width = width;
page->height = height;
page->root = NULL;
@@ -91,8 +91,8 @@ xps_free_fixed_pages(xps_context *ctx)
{
xps_page *next = page->next;
xps_free_page(ctx, page);
- fz_free(page->name);
- fz_free(page);
+ fz_free(ctx->ctx, page->name);
+ fz_free(ctx->ctx, page);
page = next;
}
ctx->first_page = NULL;
@@ -106,8 +106,8 @@ xps_free_fixed_documents(xps_context *ctx)
while (doc)
{
xps_document *next = doc->next;
- fz_free(doc->name);
- fz_free(doc);
+ fz_free(ctx->ctx, doc->name);
+ fz_free(ctx->ctx, doc);
doc = next;
}
ctx->first_fixdoc = NULL;
@@ -141,7 +141,7 @@ xps_parse_metadata_imp(xps_context *ctx, xml_element *item)
char tgtbuf[1024];
xps_absolute_path(tgtbuf, ctx->base_uri, target, sizeof tgtbuf);
if (!strcmp(type, REL_START_PART))
- ctx->start_part = fz_strdup(tgtbuf);
+ ctx->start_part = fz_strdup(ctx->ctx, tgtbuf);
}
}
@@ -199,13 +199,13 @@ xps_parse_metadata(xps_context *ctx, xps_part *part)
ctx->base_uri = buf;
ctx->part_uri = part->name;
- root = xml_parse_document(part->data, part->size);
+ root = xml_parse_document(ctx->ctx, part->data, part->size);
if (!root)
return fz_error_note(-1, "cannot parse metadata part '%s'", part->name);
xps_parse_metadata_imp(ctx, root);
- xml_free_element(root);
+ xml_free_element(ctx->ctx, root);
ctx->base_uri = NULL;
ctx->part_uri = NULL;
@@ -281,7 +281,7 @@ xps_load_fixed_page(xps_context *ctx, xps_page *page)
if (!part)
return fz_error_note(-1, "cannot read zip part '%s'", page->name);
- root = xml_parse_document(part->data, part->size);
+ root = xml_parse_document(ctx->ctx, part->data, part->size);
if (!root)
return fz_error_note(-1, "cannot parse xml part '%s'", page->name);
@@ -336,6 +336,6 @@ xps_free_page(xps_context *ctx, xps_page *page)
{
/* only free the XML contents */
if (page->root)
- xml_free_element(page->root);
+ xml_free_element(ctx->ctx, page->root);
page->root = NULL;
}
diff --git a/xps/xps_glyphs.c b/xps/xps_glyphs.c
index 50cde1e3..d5b3ea23 100644
--- a/xps/xps_glyphs.c
+++ b/xps/xps_glyphs.c
@@ -81,8 +81,8 @@ xps_lookup_font(xps_context *ctx, char *name)
static void
xps_insert_font(xps_context *ctx, char *name, fz_font *font)
{
- xps_font_cache *cache = fz_malloc(sizeof(xps_font_cache));
- cache->name = fz_strdup(name);
+ xps_font_cache *cache = fz_malloc(ctx->ctx, sizeof(xps_font_cache));
+ cache->name = fz_strdup(ctx->ctx, name);
cache->font = fz_keep_font(font);
cache->next = ctx->font_table;
ctx->font_table = cache;
@@ -281,7 +281,7 @@ xps_parse_glyphs_imp(xps_context *ctx, fz_matrix ctm,
else
tm = fz_scale(size, -size);
- text = fz_new_text(font, tm, is_sideways);
+ text = fz_new_text(ctx->ctx, font, tm, is_sideways);
while ((us && un > 0) || (is && *is))
{
@@ -355,7 +355,7 @@ xps_parse_glyphs_imp(xps_context *ctx, fz_matrix ctm,
f = y - v_offset;
}
- fz_add_text(text, glyph_index, char_code, e, f);
+ fz_add_text(ctx->ctx, text, glyph_index, char_code, e, f);
x += advance * 0.01f * size;
}
@@ -496,7 +496,7 @@ xps_parse_glyphs(xps_context *ctx, fz_matrix ctm,
if (strstr(part->name, ".ODTTF"))
xps_deobfuscate_font_resource(ctx, part);
- code = fz_new_font_from_memory(&font, part->data, part->size, subfontid);
+ code = fz_new_font_from_memory(ctx->ctx, &font, part->data, part->size, subfontid);
if (code) {
fz_error_handle(code, "cannot load font resource '%s'", partname);
xps_free_part(ctx, part);
@@ -510,8 +510,8 @@ xps_parse_glyphs(xps_context *ctx, fz_matrix ctm,
/* NOTE: we keep part->data in the font */
font->ft_data = part->data;
font->ft_size = part->size;
- fz_free(part->name);
- fz_free(part);
+ fz_free(ctx->ctx, part->name);
+ fz_free(ctx->ctx, part);
}
/*
@@ -575,10 +575,10 @@ xps_parse_glyphs(xps_context *ctx, fz_matrix ctm,
xps_end_opacity(ctx, opacity_mask_uri, dict, opacity_att, opacity_mask_tag);
- fz_free_text(text);
+ fz_free_text(ctx->ctx, text);
if (clip_att || clip_tag)
fz_pop_clip(ctx->dev);
- fz_drop_font(font);
+ fz_drop_font(ctx->ctx, font);
}
diff --git a/xps/xps_gradient.c b/xps/xps_gradient.c
index f73d33fe..0c73fd65 100644
--- a/xps/xps_gradient.c
+++ b/xps/xps_gradient.c
@@ -212,7 +212,7 @@ xps_draw_one_radial_gradient(xps_context *ctx, fz_matrix ctm,
fz_shade *shade;
/* TODO: this (and the stuff in pdf_shade) should move to res_shade.c */
- shade = fz_malloc(sizeof(fz_shade));
+ shade = fz_malloc(ctx->ctx, sizeof(fz_shade));
shade->refs = 1;
shade->colorspace = fz_device_rgb;
shade->bbox = fz_infinite_rect;
@@ -227,7 +227,7 @@ xps_draw_one_radial_gradient(xps_context *ctx, fz_matrix ctm,
shade->mesh_len = 6;
shade->mesh_cap = 6;
- shade->mesh = fz_calloc(shade->mesh_cap, sizeof(float));
+ shade->mesh = fz_calloc(ctx->ctx, shade->mesh_cap, sizeof(float));
shade->mesh[0] = x0;
shade->mesh[1] = y0;
shade->mesh[2] = r0;
@@ -237,7 +237,7 @@ xps_draw_one_radial_gradient(xps_context *ctx, fz_matrix ctm,
fz_fill_shade(ctx->dev, shade, ctm, 1);
- fz_drop_shade(shade);
+ fz_drop_shade(ctx->ctx, shade);
}
/*
@@ -253,7 +253,7 @@ xps_draw_one_linear_gradient(xps_context *ctx, fz_matrix ctm,
fz_shade *shade;
/* TODO: this (and the stuff in pdf_shade) should move to res_shade.c */
- shade = fz_malloc(sizeof(fz_shade));
+ shade = fz_malloc(ctx->ctx, sizeof(fz_shade));
shade->refs = 1;
shade->colorspace = fz_device_rgb;
shade->bbox = fz_infinite_rect;
@@ -268,7 +268,7 @@ xps_draw_one_linear_gradient(xps_context *ctx, fz_matrix ctm,
shade->mesh_len = 6;
shade->mesh_cap = 6;
- shade->mesh = fz_calloc(shade->mesh_cap, sizeof(float));
+ shade->mesh = fz_calloc(ctx->ctx, shade->mesh_cap, sizeof(float));
shade->mesh[0] = x0;
shade->mesh[1] = y0;
shade->mesh[2] = 0;
@@ -278,7 +278,7 @@ xps_draw_one_linear_gradient(xps_context *ctx, fz_matrix ctm,
fz_fill_shade(ctx->dev, shade, ctm, 1);
- fz_drop_shade(shade);
+ fz_drop_shade(ctx->ctx, shade);
}
/*
diff --git a/xps/xps_image.c b/xps/xps_image.c
index 5a9d7b13..e3570ffb 100644
--- a/xps/xps_image.c
+++ b/xps/xps_image.c
@@ -2,7 +2,7 @@
#include "muxps.h"
static int
-xps_decode_image(fz_pixmap **imagep, byte *buf, int len)
+xps_decode_image(fz_context *ctx, fz_pixmap **imagep, byte *buf, int len)
{
int error;
@@ -11,13 +11,13 @@ xps_decode_image(fz_pixmap **imagep, byte *buf, int len)
if (buf[0] == 0xff && buf[1] == 0xd8)
{
- error = xps_decode_jpeg(imagep, buf, len);
+ error = xps_decode_jpeg(ctx, imagep, buf, len);
if (error)
return fz_error_note(error, "cannot decode jpeg image");
}
else if (memcmp(buf, "\211PNG\r\n\032\n", 8) == 0)
{
- error = xps_decode_png(imagep, buf, len);
+ error = xps_decode_png(ctx, imagep, buf, len);
if (error)
return fz_error_note(error, "cannot decode png image");
}
@@ -27,7 +27,7 @@ xps_decode_image(fz_pixmap **imagep, byte *buf, int len)
}
else if (memcmp(buf, "MM", 2) == 0 || memcmp(buf, "II", 2) == 0)
{
- error = xps_decode_tiff(imagep, buf, len);
+ error = xps_decode_tiff(ctx, imagep, buf, len);
if (error)
return fz_error_note(error, "cannot decode TIFF image");
}
@@ -114,7 +114,7 @@ xps_parse_image_brush(xps_context *ctx, fz_matrix ctm, fz_rect area,
return;
}
- code = xps_decode_image(&image, part->data, part->size);
+ code = xps_decode_image(ctx->ctx, &image, part->data, part->size);
if (code < 0) {
xps_free_part(ctx, part);
fz_error_handle(-1, "cannot decode image resource");
@@ -123,6 +123,6 @@ xps_parse_image_brush(xps_context *ctx, fz_matrix ctm, fz_rect area,
xps_parse_tiling_brush(ctx, ctm, area, base_uri, dict, root, xps_paint_image_brush, image);
- fz_drop_pixmap(image);
+ fz_drop_pixmap(ctx->ctx, image);
xps_free_part(ctx, part);
}
diff --git a/xps/xps_jpeg.c b/xps/xps_jpeg.c
index ad5e2569..368f1d73 100644
--- a/xps/xps_jpeg.c
+++ b/xps/xps_jpeg.c
@@ -48,7 +48,7 @@ static void skip_input_data(j_decompress_ptr cinfo, long num_bytes)
}
int
-xps_decode_jpeg(fz_pixmap **imagep, byte *rbuf, int rlen)
+xps_decode_jpeg(fz_context *ctx, fz_pixmap **imagep, byte *rbuf, int rlen)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr_jmp err;
@@ -63,7 +63,7 @@ xps_decode_jpeg(fz_pixmap **imagep, byte *rbuf, int rlen)
if (setjmp(err.env))
{
if (image)
- fz_drop_pixmap(image);
+ fz_drop_pixmap(ctx, image);
return fz_error_make("jpeg error: %s", err.msg);
}
@@ -94,7 +94,7 @@ xps_decode_jpeg(fz_pixmap **imagep, byte *rbuf, int rlen)
else
return fz_error_make("bad number of components in jpeg: %d", cinfo.output_components);
- image = fz_new_pixmap_with_limit(colorspace, cinfo.output_width, cinfo.output_height);
+ image = fz_new_pixmap_with_limit(ctx, colorspace, cinfo.output_width, cinfo.output_height);
if (!image)
{
jpeg_finish_decompress(&cinfo);
@@ -115,7 +115,7 @@ xps_decode_jpeg(fz_pixmap **imagep, byte *rbuf, int rlen)
fz_clear_pixmap(image);
- row[0] = fz_malloc(cinfo.output_components * cinfo.output_width);
+ row[0] = fz_malloc(ctx, cinfo.output_components * cinfo.output_width);
dp = image->samples;
while (cinfo.output_scanline < cinfo.output_height)
{
@@ -128,7 +128,7 @@ xps_decode_jpeg(fz_pixmap **imagep, byte *rbuf, int rlen)
*dp++ = 255;
}
}
- fz_free(row[0]);
+ fz_free(ctx, row[0]);
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
diff --git a/xps/xps_path.c b/xps/xps_path.c
index 33526262..89d88511 100644
--- a/xps/xps_path.c
+++ b/xps/xps_path.c
@@ -41,7 +41,7 @@ fz_currentpoint(fz_path *path)
* without transforming the line width.
*/
static void
-xps_draw_arc_segment(fz_path *path, fz_matrix mtx, float th0, float th1, int iscw)
+xps_draw_arc_segment(fz_context *ctx, fz_path *path, fz_matrix mtx, float th0, float th1, int iscw)
{
float t, d;
fz_point p;
@@ -56,18 +56,18 @@ xps_draw_arc_segment(fz_path *path, fz_matrix mtx, float th0, float th1, int isc
p.x = cosf(th0);
p.y = sinf(th0);
p = fz_transform_point(mtx, p);
- fz_lineto(path, p.x, p.y);
+ fz_lineto(ctx, path, p.x, p.y);
for (t = th0; t < th1; t += d)
{
p.x = cosf(t);
p.y = sinf(t);
p = fz_transform_point(mtx, p);
- fz_lineto(path, p.x, p.y);
+ fz_lineto(ctx, path, p.x, p.y);
}
p.x = cosf(th1);
p.y = sinf(th1);
p = fz_transform_point(mtx, p);
- fz_lineto(path, p.x, p.y);
+ fz_lineto(ctx, path, p.x, p.y);
}
else
{
@@ -75,18 +75,18 @@ xps_draw_arc_segment(fz_path *path, fz_matrix mtx, float th0, float th1, int isc
p.x = cosf(th0);
p.y = sinf(th0);
p = fz_transform_point(mtx, p);
- fz_lineto(path, p.x, p.y);
+ fz_lineto(ctx, path, p.x, p.y);
for (t = th0; t > th1; t -= d)
{
p.x = cosf(t);
p.y = sinf(t);
p = fz_transform_point(mtx, p);
- fz_lineto(path, p.x, p.y);
+ fz_lineto(ctx, path, p.x, p.y);
}
p.x = cosf(th1);
p.y = sinf(th1);
p = fz_transform_point(mtx, p);
- fz_lineto(path, p.x, p.y);
+ fz_lineto(ctx, path, p.x, p.y);
}
}
@@ -107,7 +107,7 @@ angle_between(const fz_point u, const fz_point v)
}
static void
-xps_draw_arc(fz_path *path,
+xps_draw_arc(fz_context *ctx, fz_path *path,
float size_x, float size_y, float rotation_angle,
int is_large_arc, int is_clockwise,
float point_x, float point_y)
@@ -147,7 +147,7 @@ xps_draw_arc(fz_path *path,
ry = fabsf(ry);
if (rx < 0.001f || ry < 0.001f)
{
- fz_lineto(path, x2, y2);
+ fz_lineto(ctx, path, x2, y2);
return;
}
@@ -207,9 +207,9 @@ xps_draw_arc(fz_path *path,
mtx = fz_concat(fz_translate(cx, cy), mtx);
mtx = fz_concat(fz_rotate(rotation_angle), mtx);
mtx = fz_concat(fz_scale(rx, ry), mtx);
- xps_draw_arc_segment(path, mtx, th1, th1 + dth, is_clockwise);
+ xps_draw_arc_segment(ctx, path, mtx, th1, th1 + dth, is_clockwise);
- fz_lineto(path, point_x, point_y);
+ fz_lineto(ctx, path, point_x, point_y);
}
/*
@@ -232,9 +232,9 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule)
float smooth_x, smooth_y; /* saved cubic bezier control point for smooth curves */
int reset_smooth;
- path = fz_new_path();
+ path = fz_new_path(ctx->ctx);
- args = fz_calloc(strlen(geom) + 1, sizeof(char*));
+ args = fz_calloc(ctx->ctx, strlen(geom) + 1, sizeof(char*));
pargs = args;
while (*s)
@@ -291,44 +291,44 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule)
break;
case 'M':
- fz_moveto(path, fz_atof(args[i]), fz_atof(args[i+1]));
+ fz_moveto(ctx->ctx, path, fz_atof(args[i]), fz_atof(args[i+1]));
i += 2;
break;
case 'm':
pt = fz_currentpoint(path);
- fz_moveto(path, pt.x + fz_atof(args[i]), pt.y + fz_atof(args[i+1]));
+ fz_moveto(ctx->ctx, path, pt.x + fz_atof(args[i]), pt.y + fz_atof(args[i+1]));
i += 2;
break;
case 'L':
- fz_lineto(path, fz_atof(args[i]), fz_atof(args[i+1]));
+ fz_lineto(ctx->ctx, path, fz_atof(args[i]), fz_atof(args[i+1]));
i += 2;
break;
case 'l':
pt = fz_currentpoint(path);
- fz_lineto(path, pt.x + fz_atof(args[i]), pt.y + fz_atof(args[i+1]));
+ fz_lineto(ctx->ctx, path, pt.x + fz_atof(args[i]), pt.y + fz_atof(args[i+1]));
i += 2;
break;
case 'H':
pt = fz_currentpoint(path);
- fz_lineto(path, fz_atof(args[i]), pt.y);
+ fz_lineto(ctx->ctx, path, fz_atof(args[i]), pt.y);
i += 1;
break;
case 'h':
pt = fz_currentpoint(path);
- fz_lineto(path, pt.x + fz_atof(args[i]), pt.y);
+ fz_lineto(ctx->ctx, path, pt.x + fz_atof(args[i]), pt.y);
i += 1;
break;
case 'V':
pt = fz_currentpoint(path);
- fz_lineto(path, pt.x, fz_atof(args[i]));
+ fz_lineto(ctx->ctx, path, pt.x, fz_atof(args[i]));
i += 1;
break;
case 'v':
pt = fz_currentpoint(path);
- fz_lineto(path, pt.x, pt.y + fz_atof(args[i]));
+ fz_lineto(ctx->ctx, path, pt.x, pt.y + fz_atof(args[i]));
i += 1;
break;
@@ -339,7 +339,7 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule)
y2 = fz_atof(args[i+3]);
x3 = fz_atof(args[i+4]);
y3 = fz_atof(args[i+5]);
- fz_curveto(path, x1, y1, x2, y2, x3, y3);
+ fz_curveto(ctx->ctx, path, x1, y1, x2, y2, x3, y3);
i += 6;
reset_smooth = 0;
smooth_x = x3 - x2;
@@ -354,7 +354,7 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule)
y2 = fz_atof(args[i+3]) + pt.y;
x3 = fz_atof(args[i+4]) + pt.x;
y3 = fz_atof(args[i+5]) + pt.y;
- fz_curveto(path, x1, y1, x2, y2, x3, y3);
+ fz_curveto(ctx->ctx, path, x1, y1, x2, y2, x3, y3);
i += 6;
reset_smooth = 0;
smooth_x = x3 - x2;
@@ -367,7 +367,7 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule)
y1 = fz_atof(args[i+1]);
x2 = fz_atof(args[i+2]);
y2 = fz_atof(args[i+3]);
- fz_curveto(path, pt.x + smooth_x, pt.y + smooth_y, x1, y1, x2, y2);
+ fz_curveto(ctx->ctx, path, pt.x + smooth_x, pt.y + smooth_y, x1, y1, x2, y2);
i += 4;
reset_smooth = 0;
smooth_x = x2 - x1;
@@ -380,7 +380,7 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule)
y1 = fz_atof(args[i+1]) + pt.y;
x2 = fz_atof(args[i+2]) + pt.x;
y2 = fz_atof(args[i+3]) + pt.y;
- fz_curveto(path, pt.x + smooth_x, pt.y + smooth_y, x1, y1, x2, y2);
+ fz_curveto(ctx->ctx, path, pt.x + smooth_x, pt.y + smooth_y, x1, y1, x2, y2);
i += 4;
reset_smooth = 0;
smooth_x = x2 - x1;
@@ -393,7 +393,7 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule)
y1 = fz_atof(args[i+1]);
x2 = fz_atof(args[i+2]);
y2 = fz_atof(args[i+3]);
- fz_curveto(path,
+ fz_curveto(ctx->ctx, path,
(pt.x + 2 * x1) / 3, (pt.y + 2 * y1) / 3,
(x2 + 2 * x1) / 3, (y2 + 2 * y1) / 3,
x2, y2);
@@ -405,7 +405,7 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule)
y1 = fz_atof(args[i+1]) + pt.y;
x2 = fz_atof(args[i+2]) + pt.x;
y2 = fz_atof(args[i+3]) + pt.y;
- fz_curveto(path,
+ fz_curveto(ctx->ctx, path,
(pt.x + 2 * x1) / 3, (pt.y + 2 * y1) / 3,
(x2 + 2 * x1) / 3, (y2 + 2 * y1) / 3,
x2, y2);
@@ -413,7 +413,7 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule)
break;
case 'A':
- xps_draw_arc(path,
+ xps_draw_arc(ctx->ctx, path,
fz_atof(args[i+0]), fz_atof(args[i+1]), fz_atof(args[i+2]),
atoi(args[i+3]), atoi(args[i+4]),
fz_atof(args[i+5]), fz_atof(args[i+6]));
@@ -421,7 +421,7 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule)
break;
case 'a':
pt = fz_currentpoint(path);
- xps_draw_arc(path,
+ xps_draw_arc(ctx->ctx, path,
fz_atof(args[i+0]), fz_atof(args[i+1]), fz_atof(args[i+2]),
atoi(args[i+3]), atoi(args[i+4]),
fz_atof(args[i+5]) + pt.x, fz_atof(args[i+6]) + pt.y);
@@ -430,7 +430,7 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule)
case 'Z':
case 'z':
- fz_closepath(path);
+ fz_closepath(ctx->ctx, path);
break;
default:
@@ -441,12 +441,12 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule)
old = cmd;
}
- fz_free(args);
+ fz_free(ctx->ctx, args);
return path;
}
static void
-xps_parse_arc_segment(fz_path *path, xml_element *root, int stroking, int *skipped_stroke)
+xps_parse_arc_segment(fz_context *ctx, fz_path *path, xml_element *root, int stroking, int *skipped_stroke)
{
/* ArcSegment pretty much follows the SVG algorithm for converting an
* arc in endpoint representation to an arc in centerpoint
@@ -486,15 +486,15 @@ xps_parse_arc_segment(fz_path *path, xml_element *root, int stroking, int *skipp
if (stroking && !is_stroked)
{
- fz_moveto(path, point_x, point_y);
+ fz_moveto(ctx, path, point_x, point_y);
return;
}
- xps_draw_arc(path, size_x, size_y, rotation_angle, is_large_arc, is_clockwise, point_x, point_y);
+ xps_draw_arc(ctx, path, size_x, size_y, rotation_angle, is_large_arc, is_clockwise, point_x, point_y);
}
static void
-xps_parse_poly_quadratic_bezier_segment(fz_path *path, xml_element *root, int stroking, int *skipped_stroke)
+xps_parse_poly_quadratic_bezier_segment(fz_context *ctx, fz_path *path, xml_element *root, int stroking, int *skipped_stroke)
{
char *points_att = xml_att(root, "Points");
char *is_stroked_att = xml_att(root, "IsStroked");
@@ -528,12 +528,12 @@ xps_parse_poly_quadratic_bezier_segment(fz_path *path, xml_element *root, int st
{
if (stroking && !is_stroked)
{
- fz_moveto(path, x[1], y[1]);
+ fz_moveto(ctx, path, x[1], y[1]);
}
else
{
pt = fz_currentpoint(path);
- fz_curveto(path,
+ fz_curveto(ctx, path,
(pt.x + 2 * x[0]) / 3, (pt.y + 2 * y[0]) / 3,
(x[1] + 2 * x[0]) / 3, (y[1] + 2 * y[0]) / 3,
x[1], y[1]);
@@ -544,7 +544,7 @@ xps_parse_poly_quadratic_bezier_segment(fz_path *path, xml_element *root, int st
}
static void
-xps_parse_poly_bezier_segment(fz_path *path, xml_element *root, int stroking, int *skipped_stroke)
+xps_parse_poly_bezier_segment(fz_context *ctx, fz_path *path, xml_element *root, int stroking, int *skipped_stroke)
{
char *points_att = xml_att(root, "Points");
char *is_stroked_att = xml_att(root, "IsStroked");
@@ -576,16 +576,16 @@ xps_parse_poly_bezier_segment(fz_path *path, xml_element *root, int stroking, in
if (n == 3)
{
if (stroking && !is_stroked)
- fz_moveto(path, x[2], y[2]);
+ fz_moveto(ctx, path, x[2], y[2]);
else
- fz_curveto(path, x[0], y[0], x[1], y[1], x[2], y[2]);
+ fz_curveto(ctx, path, x[0], y[0], x[1], y[1], x[2], y[2]);
n = 0;
}
}
}
static void
-xps_parse_poly_line_segment(fz_path *path, xml_element *root, int stroking, int *skipped_stroke)
+xps_parse_poly_line_segment(fz_context *ctx, fz_path *path, xml_element *root, int stroking, int *skipped_stroke)
{
char *points_att = xml_att(root, "Points");
char *is_stroked_att = xml_att(root, "IsStroked");
@@ -611,15 +611,15 @@ xps_parse_poly_line_segment(fz_path *path, xml_element *root, int stroking, int
while (*s == ' ') s++;
sscanf(s, "%g,%g", &x, &y);
if (stroking && !is_stroked)
- fz_moveto(path, x, y);
+ fz_moveto(ctx, path, x, y);
else
- fz_lineto(path, x, y);
+ fz_lineto(ctx, path, x, y);
while (*s != ' ' && *s != 0) s++;
}
}
static void
-xps_parse_path_figure(fz_path *path, xml_element *root, int stroking)
+xps_parse_path_figure(fz_context *ctx, fz_path *path, xml_element *root, int stroking)
{
xml_element *node;
@@ -648,26 +648,26 @@ xps_parse_path_figure(fz_path *path, xml_element *root, int stroking)
if (!stroking && !is_filled) /* not filled, when filling */
return;
- fz_moveto(path, start_x, start_y);
+ fz_moveto(ctx, path, start_x, start_y);
for (node = xml_down(root); node; node = xml_next(node))
{
if (!strcmp(xml_tag(node), "ArcSegment"))
- xps_parse_arc_segment(path, node, stroking, &skipped_stroke);
+ xps_parse_arc_segment(ctx, path, node, stroking, &skipped_stroke);
if (!strcmp(xml_tag(node), "PolyBezierSegment"))
- xps_parse_poly_bezier_segment(path, node, stroking, &skipped_stroke);
+ xps_parse_poly_bezier_segment(ctx, path, node, stroking, &skipped_stroke);
if (!strcmp(xml_tag(node), "PolyLineSegment"))
- xps_parse_poly_line_segment(path, node, stroking, &skipped_stroke);
+ xps_parse_poly_line_segment(ctx, path, node, stroking, &skipped_stroke);
if (!strcmp(xml_tag(node), "PolyQuadraticBezierSegment"))
- xps_parse_poly_quadratic_bezier_segment(path, node, stroking, &skipped_stroke);
+ xps_parse_poly_quadratic_bezier_segment(ctx, path, node, stroking, &skipped_stroke);
}
if (is_closed)
{
if (stroking && skipped_stroke)
- fz_lineto(path, start_x, start_y); /* we've skipped using fz_moveto... */
+ fz_lineto(ctx, path, start_x, start_y); /* we've skipped using fz_moveto... */
else
- fz_closepath(path); /* no skipped segments, safe to closepath properly */
+ fz_closepath(ctx, path); /* no skipped segments, safe to closepath properly */
}
}
@@ -716,15 +716,15 @@ xps_parse_path_geometry(xps_context *ctx, xps_resource *dict, xml_element *root,
if (figures_att)
path = xps_parse_abbreviated_geometry(ctx, figures_att, fill_rule);
else
- path = fz_new_path();
+ path = fz_new_path(ctx->ctx);
if (figures_tag)
- xps_parse_path_figure(path, figures_tag, stroking);
+ xps_parse_path_figure(ctx->ctx, path, figures_tag, stroking);
for (node = xml_down(root); node; node = xml_next(node))
{
if (!strcmp(xml_tag(node), "PathFigure"))
- xps_parse_path_figure(path, node, stroking);
+ xps_parse_path_figure(ctx->ctx, path, node, stroking);
}
if (transform_att || transform_tag)
@@ -757,9 +757,9 @@ xps_clip(xps_context *ctx, fz_matrix ctm, xps_resource *dict, char *clip_att, xm
else if (clip_tag)
path = xps_parse_path_geometry(ctx, dict, clip_tag, 0, &fill_rule);
else
- path = fz_new_path();
+ path = fz_new_path(ctx->ctx);
fz_clip_path(ctx->dev, path, NULL, fill_rule == 0, ctm);
- fz_free_path(path);
+ fz_free_path(ctx->ctx, path);
}
/*
@@ -982,7 +982,7 @@ xps_parse_path(xps_context *ctx, fz_matrix ctm, char *base_uri, xps_resource *di
xps_end_opacity(ctx, opacity_mask_uri, dict, opacity_att, opacity_mask_tag);
- fz_free_path(path);
+ fz_free_path(ctx->ctx, path);
path = NULL;
if (clip_att || clip_tag)
diff --git a/xps/xps_png.c b/xps/xps_png.c
index aec6610a..5a866dcc 100644
--- a/xps/xps_png.c
+++ b/xps/xps_png.c
@@ -5,6 +5,7 @@
struct info
{
+ fz_context *ctx;
int width, height, depth, n;
int interlace, indexed;
int size;
@@ -61,12 +62,12 @@ static const unsigned char png_signature[8] =
static void *zalloc(void *opaque, unsigned int items, unsigned int size)
{
- return fz_calloc(items, size);
+ return fz_calloc(opaque, items, size);
}
static void zfree(void *opaque, void *address)
{
- fz_free(address);
+ fz_free(opaque, address);
}
static inline int paeth(int a, int b, int c)
@@ -188,7 +189,7 @@ png_deinterlace(struct info *info, int *passw, int *passh, int *passofs)
unsigned char *output;
int p, x, y, k;
- output = fz_calloc(info->height, stride);
+ output = fz_calloc(info->ctx, info->height, stride);
for (p = 0; p < 7; p++)
{
@@ -214,7 +215,7 @@ png_deinterlace(struct info *info, int *passw, int *passh, int *passofs)
}
}
- fz_free(info->samples);
+ fz_free(info->ctx, info->samples);
info->samples = output;
}
@@ -408,11 +409,11 @@ png_read_image(struct info *info, unsigned char *p, int total)
info->size = passofs[7];
}
- info->samples = fz_malloc(info->size);
+ info->samples = fz_malloc(info->ctx, info->size);
stm.zalloc = zalloc;
stm.zfree = zfree;
- stm.opaque = NULL;
+ stm.opaque = info->ctx;
stm.next_out = info->samples;
stm.avail_out = info->size;
@@ -480,9 +481,9 @@ png_read_image(struct info *info, unsigned char *p, int total)
}
static fz_pixmap *
-png_expand_palette(struct info *info, fz_pixmap *src)
+png_expand_palette(fz_context *ctx, struct info *info, fz_pixmap *src)
{
- fz_pixmap *dst = fz_new_pixmap(fz_device_rgb, src->w, src->h);
+ fz_pixmap *dst = fz_new_pixmap(ctx, fz_device_rgb, src->w, src->h);
unsigned char *sp = src->samples;
unsigned char *dp = dst->samples;
int x, y;
@@ -503,7 +504,7 @@ png_expand_palette(struct info *info, fz_pixmap *src)
}
}
- fz_drop_pixmap(src);
+ fz_drop_pixmap(info->ctx, src);
return dst;
}
@@ -532,7 +533,7 @@ png_mask_transparency(struct info *info, fz_pixmap *dst)
}
int
-xps_decode_png(fz_pixmap **imagep, byte *p, int total)
+xps_decode_png(fz_context *ctx, fz_pixmap **imagep, byte *p, int total)
{
fz_pixmap *image;
fz_colorspace *colorspace;
@@ -540,6 +541,7 @@ xps_decode_png(fz_pixmap **imagep, byte *p, int total)
int code;
int stride;
+ png.ctx = ctx;
code = png_read_image(&png, p, total);
if (code)
return fz_error_note(code, "cannot read png image");
@@ -551,10 +553,10 @@ xps_decode_png(fz_pixmap **imagep, byte *p, int total)
stride = (png.width * png.n * png.depth + 7) / 8;
- image = fz_new_pixmap_with_limit(colorspace, png.width, png.height);
+ image = fz_new_pixmap_with_limit(ctx, colorspace, png.width, png.height);
if (!image)
{
- fz_free(png.samples);
+ fz_free(png.ctx, png.samples);
return fz_error_make("out of memory");
}
@@ -564,14 +566,14 @@ xps_decode_png(fz_pixmap **imagep, byte *p, int total)
fz_unpack_tile(image, png.samples, png.n, png.depth, stride, png.indexed);
if (png.indexed)
- image = png_expand_palette(&png, image);
+ image = png_expand_palette(ctx, &png, image);
else if (png.transparency)
png_mask_transparency(&png, image);
if (png.transparency || png.n == 2 || png.n == 4)
fz_premultiply_pixmap(image);
- fz_free(png.samples);
+ fz_free(png.ctx, png.samples);
*imagep = image;
return fz_okay;
diff --git a/xps/xps_resource.c b/xps/xps_resource.c
index bd414894..176afd77 100644
--- a/xps/xps_resource.c
+++ b/xps/xps_resource.c
@@ -71,7 +71,7 @@ xps_parse_remote_resource_dictionary(xps_context *ctx, xps_resource **dictp, cha
return fz_error_make("cannot find remote resource part '%s'", part_name);
}
- xml = xml_parse_document(part->data, part->size);
+ xml = xml_parse_document(ctx->ctx, part->data, part->size);
if (!xml)
{
xps_free_part(ctx, part);
@@ -80,7 +80,7 @@ xps_parse_remote_resource_dictionary(xps_context *ctx, xps_resource **dictp, cha
if (strcmp(xml_tag(xml), "ResourceDictionary"))
{
- xml_free_element(xml);
+ xml_free_element(ctx->ctx, xml);
xps_free_part(ctx, part);
return fz_error_make("expected ResourceDictionary element (found %s)", xml_tag(xml));
}
@@ -93,7 +93,7 @@ xps_parse_remote_resource_dictionary(xps_context *ctx, xps_resource **dictp, cha
code = xps_parse_resource_dictionary(ctx, &dict, part_uri, xml);
if (code)
{
- xml_free_element(xml);
+ xml_free_element(ctx->ctx, xml);
xps_free_part(ctx, part);
return fz_error_note(code, "cannot parse remote resource dictionary: %s", part_uri);
}
@@ -132,7 +132,7 @@ xps_parse_resource_dictionary(xps_context *ctx, xps_resource **dictp, char *base
key = xml_att(node, "x:Key");
if (key)
{
- entry = fz_malloc(sizeof(xps_resource));
+ entry = fz_malloc(ctx->ctx, sizeof(xps_resource));
entry->name = key;
entry->base_uri = NULL;
entry->base_xml = NULL;
@@ -144,9 +144,9 @@ xps_parse_resource_dictionary(xps_context *ctx, xps_resource **dictp, char *base
}
if (head)
- head->base_uri = fz_strdup(base_uri);
+ head->base_uri = fz_strdup(ctx->ctx, base_uri);
else
- return fz_throw("empty resource dictionary");
+ return fz_error_make("empty resource dictionary");
*dictp = head;
return fz_okay;
@@ -160,10 +160,10 @@ xps_free_resource_dictionary(xps_context *ctx, xps_resource *dict)
{
next = dict->next;
if (dict->base_xml)
- xml_free_element(dict->base_xml);
+ xml_free_element(ctx->ctx, dict->base_xml);
if (dict->base_uri)
- fz_free(dict->base_uri);
- fz_free(dict);
+ fz_free(ctx->ctx, dict->base_uri);
+ fz_free(ctx->ctx, dict);
dict = next;
}
}
diff --git a/xps/xps_tiff.c b/xps/xps_tiff.c
index 22137259..6e890f28 100644
--- a/xps/xps_tiff.c
+++ b/xps/xps_tiff.c
@@ -56,6 +56,7 @@ struct tiff
fz_colorspace *colorspace;
byte *samples;
int stride;
+ fz_context *ctx;
};
enum
@@ -182,30 +183,31 @@ xps_decode_tiff_fax(struct tiff *tiff, int comp, fz_stream *chain, byte *wp, int
fz_obj *params;
fz_obj *columns, *rows, *black_is_1, *k, *encoded_byte_align;
int n;
-
- columns = fz_new_int(tiff->imagewidth);
- rows = fz_new_int(tiff->imagelength);
- black_is_1 = fz_new_bool(tiff->photometric == 0);
- k = fz_new_int(comp == 4 ? -1 : 0);
- encoded_byte_align = fz_new_bool(comp == 2);
-
- params = fz_new_dict(5);
- fz_dict_puts(params, "Columns", columns);
- fz_dict_puts(params, "Rows", rows);
- fz_dict_puts(params, "BlackIs1", black_is_1);
- fz_dict_puts(params, "K", k);
- fz_dict_puts(params, "EncodedByteAlign", encoded_byte_align);
-
- fz_drop_obj(columns);
- fz_drop_obj(rows);
- fz_drop_obj(black_is_1);
- fz_drop_obj(k);
- fz_drop_obj(encoded_byte_align);
+ fz_context *ctx = tiff->ctx;
+
+ columns = fz_new_int(ctx, tiff->imagewidth);
+ rows = fz_new_int(ctx, tiff->imagelength);
+ black_is_1 = fz_new_bool(ctx, tiff->photometric == 0);
+ k = fz_new_int(ctx, comp == 4 ? -1 : 0);
+ encoded_byte_align = fz_new_bool(ctx, comp == 2);
+
+ params = fz_new_dict(ctx, 5);
+ fz_dict_puts(ctx, params, "Columns", columns);
+ fz_dict_puts(ctx, params, "Rows", rows);
+ fz_dict_puts(ctx, params, "BlackIs1", black_is_1);
+ fz_dict_puts(ctx, params, "K", k);
+ fz_dict_puts(ctx, params, "EncodedByteAlign", encoded_byte_align);
+
+ fz_drop_obj(ctx, columns);
+ fz_drop_obj(ctx, rows);
+ fz_drop_obj(ctx, black_is_1);
+ fz_drop_obj(ctx, k);
+ fz_drop_obj(ctx, encoded_byte_align);
stm = fz_open_faxd(chain, params);
n = fz_read(stm, wp, wlen);
fz_close(stm);
- fz_drop_obj(params);
+ fz_drop_obj(ctx, params);
if (n < 0)
return fz_error_note(n, "cannot read fax strip");
@@ -318,7 +320,7 @@ xps_expand_tiff_colormap(struct tiff *tiff)
stride = tiff->imagewidth * (tiff->samplesperpixel + 2);
- samples = fz_malloc(stride * tiff->imagelength);
+ samples = fz_malloc(tiff->ctx, stride * tiff->imagelength);
for (y = 0; y < tiff->imagelength; y++)
{
@@ -428,7 +430,7 @@ xps_decode_tiff_strips(struct tiff *tiff)
tiff->yresolution = 96;
}
- tiff->samples = fz_calloc(tiff->imagelength, tiff->stride);
+ tiff->samples = fz_calloc(tiff->ctx, tiff->imagelength, tiff->stride);
memset(tiff->samples, 0x55, tiff->imagelength * tiff->stride);
wp = tiff->samples;
@@ -452,7 +454,7 @@ xps_decode_tiff_strips(struct tiff *tiff)
rp[i] = bitrev[rp[i]];
/* the strip decoders will close this */
- stm = fz_open_memory(rp, rlen);
+ stm = fz_open_memory(tiff->ctx, rp, rlen);
switch (tiff->compression)
{
@@ -673,7 +675,7 @@ xps_read_tiff_tag(struct tiff *tiff, unsigned offset)
break;
case ICCProfile:
- tiff->profile = fz_malloc(count);
+ tiff->profile = fz_malloc(tiff->ctx, count);
/* ICC profile data type is set to UNDEFINED.
* TBYTE reading not correct in xps_read_tiff_tag_value */
xps_read_tiff_bytes(tiff->profile, tiff, value, count);
@@ -687,17 +689,17 @@ xps_read_tiff_tag(struct tiff *tiff, unsigned offset)
break;
case StripOffsets:
- tiff->stripoffsets = fz_calloc(count, sizeof(unsigned));
+ tiff->stripoffsets = fz_calloc(tiff->ctx, count, sizeof(unsigned));
xps_read_tiff_tag_value(tiff->stripoffsets, tiff, type, value, count);
break;
case StripByteCounts:
- tiff->stripbytecounts = fz_calloc(count, sizeof(unsigned));
+ tiff->stripbytecounts = fz_calloc(tiff->ctx, count, sizeof(unsigned));
xps_read_tiff_tag_value(tiff->stripbytecounts, tiff, type, value, count);
break;
case ColorMap:
- tiff->colormap = fz_calloc(count, sizeof(unsigned));
+ tiff->colormap = fz_calloc(tiff->ctx, count, sizeof(unsigned));
xps_read_tiff_tag_value(tiff->colormap, tiff, type, value, count);
break;
@@ -794,12 +796,13 @@ xps_decode_tiff_header(struct tiff *tiff, byte *buf, int len)
}
int
-xps_decode_tiff(fz_pixmap **imagep, byte *buf, int len)
+xps_decode_tiff(fz_context *ctx, fz_pixmap **imagep, byte *buf, int len)
{
int error;
fz_pixmap *image;
struct tiff tiff;
+ tiff.ctx = ctx;
error = xps_decode_tiff_header(&tiff, buf, len);
if (error)
return fz_error_note(error, "cannot decode tiff header");
@@ -822,13 +825,13 @@ xps_decode_tiff(fz_pixmap **imagep, byte *buf, int len)
/* Expand into fz_pixmap struct */
- image = fz_new_pixmap_with_limit(tiff.colorspace, tiff.imagewidth, tiff.imagelength);
+ image = fz_new_pixmap_with_limit(tiff.ctx, tiff.colorspace, tiff.imagewidth, tiff.imagelength);
if (!image)
{
- if (tiff.colormap) fz_free(tiff.colormap);
- if (tiff.stripoffsets) fz_free(tiff.stripoffsets);
- if (tiff.stripbytecounts) fz_free(tiff.stripbytecounts);
- if (tiff.samples) fz_free(tiff.samples);
+ if (tiff.colormap) fz_free(ctx, tiff.colormap);
+ if (tiff.stripoffsets) fz_free(ctx, tiff.stripoffsets);
+ if (tiff.stripbytecounts) fz_free(ctx, tiff.stripbytecounts);
+ if (tiff.samples) fz_free(ctx, tiff.samples);
return fz_error_make("out of memory");
}
@@ -843,11 +846,11 @@ xps_decode_tiff(fz_pixmap **imagep, byte *buf, int len)
/* CMYK is a subtractive colorspace, we want additive for premul alpha */
if (image->n == 5)
{
- fz_pixmap *rgb = fz_new_pixmap(fz_device_rgb, image->w, image->h);
- fz_convert_pixmap(image, rgb);
+ fz_pixmap *rgb = fz_new_pixmap(tiff.ctx, fz_device_rgb, image->w, image->h);
+ fz_convert_pixmap(tiff.ctx, image, rgb);
rgb->xres = image->xres;
rgb->yres = image->yres;
- fz_drop_pixmap(image);
+ fz_drop_pixmap(ctx, image);
image = rgb;
}
fz_premultiply_pixmap(image);
@@ -855,10 +858,10 @@ xps_decode_tiff(fz_pixmap **imagep, byte *buf, int len)
/* Clean up scratch memory */
- if (tiff.colormap) fz_free(tiff.colormap);
- if (tiff.stripoffsets) fz_free(tiff.stripoffsets);
- if (tiff.stripbytecounts) fz_free(tiff.stripbytecounts);
- if (tiff.samples) fz_free(tiff.samples);
+ if (tiff.colormap) fz_free(ctx, tiff.colormap);
+ if (tiff.stripoffsets) fz_free(ctx, tiff.stripoffsets);
+ if (tiff.stripbytecounts) fz_free(ctx, tiff.stripbytecounts);
+ if (tiff.samples) fz_free(ctx, tiff.samples);
*imagep = image;
return fz_okay;
diff --git a/xps/xps_tile.c b/xps/xps_tile.c
index 5aa45891..7a0a4f66 100644
--- a/xps/xps_tile.c
+++ b/xps/xps_tile.c
@@ -22,14 +22,14 @@ struct closure
static void
xps_paint_tiling_brush_clipped(xps_context *ctx, fz_matrix ctm, fz_rect viewbox, struct closure *c)
{
- fz_path *path = fz_new_path();
- fz_moveto(path, viewbox.x0, viewbox.y0);
- fz_lineto(path, viewbox.x0, viewbox.y1);
- fz_lineto(path, viewbox.x1, viewbox.y1);
- fz_lineto(path, viewbox.x1, viewbox.y0);
- fz_closepath(path);
+ fz_path *path = fz_new_path(ctx->ctx);
+ fz_moveto(ctx->ctx, path, viewbox.x0, viewbox.y0);
+ fz_lineto(ctx->ctx, path, viewbox.x0, viewbox.y1);
+ fz_lineto(ctx->ctx, path, viewbox.x1, viewbox.y1);
+ fz_lineto(ctx->ctx, path, viewbox.x1, viewbox.y0);
+ fz_closepath(ctx->ctx, path);
fz_clip_path(ctx->dev, path, NULL, 0, ctm);
- fz_free_path(path);
+ fz_free_path(ctx->ctx, path);
c->func(ctx, ctm, viewbox, c->base_uri, c->dict, c->root, c->user);
fz_pop_clip(ctx->dev);
}
diff --git a/xps/xps_xml.c b/xps/xps_xml.c
index 5a564fbc..47e1728b 100644
--- a/xps/xps_xml.c
+++ b/xps/xps_xml.c
@@ -18,6 +18,7 @@ struct element
struct parser
{
struct element *head;
+ fz_context *ctx;
};
static inline void indent(int n)
@@ -70,26 +71,26 @@ char *xml_att(struct element *item, const char *name)
return NULL;
}
-static void xml_free_attribute(struct attribute *att)
+static void xml_free_attribute(fz_context *ctx, struct attribute *att)
{
while (att) {
struct attribute *next = att->next;
if (att->value)
- fz_free(att->value);
- fz_free(att);
+ fz_free(ctx, att->value);
+ fz_free(ctx, att);
att = next;
}
}
-void xml_free_element(struct element *item)
+void xml_free_element(fz_context *ctx, struct element *item)
{
while (item) {
struct element *next = item->next;
if (item->atts)
- xml_free_attribute(item->atts);
+ xml_free_attribute(ctx, item->atts);
if (item->down)
- xml_free_element(item->down);
- fz_free(item);
+ xml_free_element(ctx, item->down);
+ fz_free(ctx, item);
item = next;
}
}
@@ -133,7 +134,7 @@ static void xml_emit_open_tag(struct parser *parser, char *a, char *b)
{
struct element *head, *tail;
- head = fz_malloc(sizeof(struct element));
+ head = fz_malloc(parser->ctx, sizeof(struct element));
if (b - a > sizeof(head->name))
b = a + sizeof(head->name);
memcpy(head->name, a, b - a);
@@ -162,7 +163,7 @@ static void xml_emit_att_name(struct parser *parser, char *a, char *b)
struct element *head = parser->head;
struct attribute *att;
- att = fz_malloc(sizeof(struct attribute));
+ att = fz_malloc(parser->ctx, sizeof(struct attribute));
if (b - a > sizeof(att->name))
b = a + sizeof(att->name);
memcpy(att->name, a, b - a);
@@ -180,7 +181,7 @@ static void xml_emit_att_value(struct parser *parser, char *a, char *b)
int c;
/* entities are all longer than UTFmax so runetochar is safe */
- s = att->value = fz_malloc(b - a + 1);
+ s = att->value = fz_malloc(parser->ctx, b - a + 1);
while (a < b) {
if (*a == '&') {
a += xml_parse_entity(&c, a);
@@ -329,14 +330,14 @@ parse_attribute_value:
return "end of data in attribute value";
}
-static char *convert_to_utf8(unsigned char *s, int n)
+static char *convert_to_utf8(fz_context *ctx, unsigned char *s, int n)
{
unsigned char *e = s + n;
char *dst, *d;
int c;
if (s[0] == 0xFE && s[1] == 0xFF) {
- dst = d = fz_malloc(n * 2);
+ dst = d = fz_malloc(ctx, n * 2);
while (s + 1 < e) {
c = s[0] << 8 | s[1];
d += runetochar(d, &c);
@@ -347,7 +348,7 @@ static char *convert_to_utf8(unsigned char *s, int n)
}
if (s[0] == 0xFF && s[1] == 0xFE) {
- dst = d = fz_malloc(n * 2);
+ dst = d = fz_malloc(ctx, n * 2);
while (s + 1 < e) {
c = s[0] | s[1] << 8;
d += runetochar(d, &c);
@@ -361,7 +362,7 @@ static char *convert_to_utf8(unsigned char *s, int n)
}
struct element *
-xml_parse_document(unsigned char *s, int n)
+xml_parse_document(fz_context *ctx, unsigned char *s, int n)
{
struct parser parser;
struct element root;
@@ -371,17 +372,18 @@ xml_parse_document(unsigned char *s, int n)
memset(&root, 0, sizeof(root));
parser.head = &root;
+ parser.ctx = ctx;
- p = convert_to_utf8(s, n);
+ p = convert_to_utf8(ctx, s, n);
error = xml_parse_document_imp(&parser, p);
if (error) {
- fz_error_make(error);
+ fz_error_make(error, "");
return NULL;
}
if (p != (char*)s)
- fz_free(p);
+ fz_free(ctx, p);
return root.down;
}
diff --git a/xps/xps_zip.c b/xps/xps_zip.c
index 0f89fe56..ad1c30b1 100644
--- a/xps/xps_zip.c
+++ b/xps/xps_zip.c
@@ -8,10 +8,10 @@ xps_new_part(xps_context *ctx, char *name, int size)
{
xps_part *part;
- part = fz_malloc(sizeof(xps_part));
- part->name = fz_strdup(name);
+ part = fz_malloc(ctx->ctx, sizeof(xps_part));
+ part->name = fz_strdup(ctx->ctx, name);
part->size = size;
- part->data = fz_malloc(size + 1);
+ part->data = fz_malloc(ctx->ctx, size + 1);
part->data[size] = 0; /* null-terminate for xml parser */
return part;
@@ -20,9 +20,9 @@ xps_new_part(xps_context *ctx, char *name, int size)
void
xps_free_part(xps_context *ctx, xps_part *part)
{
- fz_free(part->name);
- fz_free(part->data);
- fz_free(part);
+ fz_free(ctx->ctx, part->name);
+ fz_free(ctx->ctx, part->data);
+ fz_free(ctx->ctx, part);
}
static inline int getshort(fz_stream *file)
@@ -44,13 +44,13 @@ static inline int getlong(fz_stream *file)
static void *
xps_zip_alloc_items(xps_context *ctx, int items, int size)
{
- return fz_calloc(items, size);
+ return fz_calloc(ctx->ctx, items, size);
}
static void
xps_zip_free(xps_context *ctx, void *ptr)
{
- fz_free(ptr);
+ fz_free(ctx->ctx, ptr);
}
static int
@@ -115,7 +115,7 @@ xps_read_zip_entry(xps_context *ctx, xps_entry *ent, unsigned char *outbuf)
}
else if (method == 8)
{
- inbuf = fz_malloc(ent->csize);
+ inbuf = fz_malloc(ctx->ctx, ent->csize);
fz_read(ctx->file, inbuf, ent->csize);
@@ -141,7 +141,7 @@ xps_read_zip_entry(xps_context *ctx, xps_entry *ent, unsigned char *outbuf)
if (code != Z_OK)
return fz_error_make("zlib inflateEnd error: %s", stream.msg);
- fz_free(inbuf);
+ fz_free(ctx->ctx, inbuf);
}
else
{
@@ -177,8 +177,7 @@ xps_read_zip_dir(xps_context *ctx, int start_offset)
offset = getlong(ctx->file); /* offset to central directory */
ctx->zip_count = count;
- ctx->zip_table = fz_calloc(count, sizeof(xps_entry));
- memset(ctx->zip_table, 0, sizeof(xps_entry) * count);
+ ctx->zip_table = fz_calloc(ctx->ctx, count, sizeof(xps_entry));
fz_seek(ctx->file, offset, 0);
@@ -205,7 +204,7 @@ xps_read_zip_dir(xps_context *ctx, int start_offset)
(void) getlong(ctx->file); /* ext file atts */
ctx->zip_table[i].offset = getlong(ctx->file);
- ctx->zip_table[i].name = fz_malloc(namesize + 1);
+ ctx->zip_table[i].name = fz_malloc(ctx->ctx, namesize + 1);
fz_read(ctx->file, (unsigned char*)ctx->zip_table[i].name, namesize);
ctx->zip_table[i].name[namesize] = 0;
@@ -391,15 +390,15 @@ xps_read_part(xps_context *ctx, char *partname)
}
static int
-xps_open_directory(xps_context **ctxp, char *directory)
+xps_open_directory(fz_context *fctx, xps_context **ctxp, char *directory)
{
xps_context *ctx;
int code;
- ctx = fz_malloc(sizeof(xps_context));
- memset(ctx, 0, sizeof(xps_context));
+ ctx = fz_calloc(fctx, 1, sizeof(xps_context));
- ctx->directory = fz_strdup(directory);
+ ctx->directory = fz_strdup(fctx, directory);
+ ctx->ctx = fctx;
code = xps_read_page_list(ctx);
if (code)
@@ -418,9 +417,9 @@ xps_open_stream(xps_context **ctxp, fz_stream *file)
xps_context *ctx;
int code;
- ctx = fz_malloc(sizeof(xps_context));
- memset(ctx, 0, sizeof(xps_context));
+ ctx = fz_calloc(file->ctx, 1, sizeof(xps_context));
+ ctx->ctx = file->ctx;
ctx->file = fz_keep_stream(file);
code = xps_find_and_read_zip_dir(ctx);
@@ -442,7 +441,7 @@ xps_open_stream(xps_context **ctxp, fz_stream *file)
}
int
-xps_open_file(xps_context **ctxp, char *filename)
+xps_open_file(fz_context *ctx, xps_context **ctxp, char *filename)
{
char buf[2048];
fz_stream *file;
@@ -456,10 +455,10 @@ xps_open_file(xps_context **ctxp, char *filename)
if (!p)
p = strstr(buf, "\\_rels\\.rels");
*p = 0;
- return xps_open_directory(ctxp, buf);
+ return xps_open_directory(ctx, ctxp, buf);
}
- file = fz_open_file(filename);
+ file = fz_open_file(ctx, filename);
if (!file)
return fz_error_make("cannot open file '%s': %s", filename, strerror(errno));
@@ -480,22 +479,22 @@ xps_free_context(xps_context *ctx)
fz_close(ctx->file);
for (i = 0; i < ctx->zip_count; i++)
- fz_free(ctx->zip_table[i].name);
- fz_free(ctx->zip_table);
+ fz_free(ctx->ctx, ctx->zip_table[i].name);
+ fz_free(ctx->ctx, ctx->zip_table);
font = ctx->font_table;
while (font)
{
next = font->next;
- fz_drop_font(font->font);
- fz_free(font->name);
- fz_free(font);
+ fz_drop_font(ctx->ctx, font->font);
+ fz_free(ctx->ctx, font->name);
+ fz_free(ctx->ctx, font);
font = next;
}
xps_free_page_list(ctx);
- fz_free(ctx->start_part);
- fz_free(ctx->directory);
- fz_free(ctx);
+ fz_free(ctx->ctx, ctx->start_part);
+ fz_free(ctx->ctx, ctx->directory);
+ fz_free(ctx->ctx, ctx);
}