diff options
Diffstat (limited to 'pdf')
-rw-r--r-- | pdf/mupdf.h | 84 | ||||
-rw-r--r-- | pdf/pdf_annot.c | 113 | ||||
-rw-r--r-- | pdf/pdf_cmap.c | 60 | ||||
-rw-r--r-- | pdf/pdf_cmap_load.c | 41 | ||||
-rw-r--r-- | pdf/pdf_cmap_parse.c | 18 | ||||
-rw-r--r-- | pdf/pdf_colorspace.c | 134 | ||||
-rw-r--r-- | pdf/pdf_crypt.c | 199 | ||||
-rw-r--r-- | pdf/pdf_font.c | 319 | ||||
-rw-r--r-- | pdf/pdf_function.c | 170 | ||||
-rw-r--r-- | pdf/pdf_image.c | 98 | ||||
-rw-r--r-- | pdf/pdf_interpret.c | 312 | ||||
-rw-r--r-- | pdf/pdf_metrics.c | 8 | ||||
-rw-r--r-- | pdf/pdf_nametree.c | 95 | ||||
-rw-r--r-- | pdf/pdf_outline.c | 44 | ||||
-rw-r--r-- | pdf/pdf_page.c | 198 | ||||
-rw-r--r-- | pdf/pdf_parse.c | 244 | ||||
-rw-r--r-- | pdf/pdf_pattern.c | 35 | ||||
-rw-r--r-- | pdf/pdf_repair.c | 106 | ||||
-rw-r--r-- | pdf/pdf_shade.c | 236 | ||||
-rw-r--r-- | pdf/pdf_store.c | 70 | ||||
-rw-r--r-- | pdf/pdf_stream.c | 90 | ||||
-rw-r--r-- | pdf/pdf_type3.c | 73 | ||||
-rw-r--r-- | pdf/pdf_unicode.c | 21 | ||||
-rw-r--r-- | pdf/pdf_xobject.c | 43 | ||||
-rw-r--r-- | pdf/pdf_xref.c | 160 |
25 files changed, 1545 insertions, 1426 deletions
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)); |