diff options
Diffstat (limited to 'fitz')
36 files changed, 1172 insertions, 914 deletions
diff --git a/fitz/base_hash.c b/fitz/base_hash.c index b7caf173..f754644d 100644 --- a/fitz/base_hash.c +++ b/fitz/base_hash.c @@ -42,18 +42,17 @@ static unsigned hash(unsigned char *s, int len) } fz_hash_table * -fz_new_hash_table(int initialsize, int keylen) +fz_new_hash_table(fz_context *ctx, int initialsize, int keylen) { fz_hash_table *table; assert(keylen <= MAX_KEY_LEN); - table = fz_malloc(sizeof(fz_hash_table)); + table = fz_malloc(ctx, sizeof(fz_hash_table)); table->keylen = keylen; table->size = initialsize; table->load = 0; - table->ents = fz_calloc(table->size, sizeof(fz_hash_entry)); - memset(table->ents, 0, sizeof(fz_hash_entry) * table->size); + table->ents = fz_calloc(ctx, table->size, sizeof(fz_hash_entry)); return table; } @@ -84,14 +83,14 @@ fz_hash_get_val(fz_hash_table *table, int idx) } void -fz_free_hash(fz_hash_table *table) +fz_free_hash(fz_context *ctx, fz_hash_table *table) { - fz_free(table->ents); - fz_free(table); + fz_free(ctx, table->ents); + fz_free(ctx, table); } static void -fz_resize_hash(fz_hash_table *table, int newsize) +fz_resize_hash(fz_context *ctx, fz_hash_table *table, int newsize) { fz_hash_entry *oldents = table->ents; int oldsize = table->size; @@ -104,7 +103,7 @@ fz_resize_hash(fz_hash_table *table, int newsize) return; } - table->ents = fz_calloc(newsize, sizeof(fz_hash_entry)); + table->ents = fz_calloc(ctx, newsize, sizeof(fz_hash_entry)); memset(table->ents, 0, sizeof(fz_hash_entry) * newsize); table->size = newsize; table->load = 0; @@ -113,11 +112,11 @@ fz_resize_hash(fz_hash_table *table, int newsize) { if (oldents[i].val) { - fz_hash_insert(table, oldents[i].key, oldents[i].val); + fz_hash_insert(ctx, table, oldents[i].key, oldents[i].val); } } - fz_free(oldents); + fz_free(ctx, oldents); } void * @@ -140,7 +139,7 @@ fz_hash_find(fz_hash_table *table, void *key) } void -fz_hash_insert(fz_hash_table *table, void *key, void *val) +fz_hash_insert(fz_context *ctx, fz_hash_table *table, void *key, void *val) { fz_hash_entry *ents; unsigned size; @@ -148,7 +147,7 @@ fz_hash_insert(fz_hash_table *table, void *key, void *val) if (table->load > table->size * 8 / 10) { - fz_resize_hash(table, table->size * 2); + fz_resize_hash(ctx, table, table->size * 2); } ents = table->ents; diff --git a/fitz/base_memory.c b/fitz/base_memory.c index f1b668eb..9847aa76 100644 --- a/fitz/base_memory.c +++ b/fitz/base_memory.c @@ -1,77 +1,146 @@ #include "fitz.h" +#include "except.h" void * -fz_malloc(int size) +fz_malloc(fz_context *ctx, size_t size) { - void *p = malloc(size); + void *p; + fz_alloc_context *alloc; + + assert(ctx != NULL); + alloc = ctx->alloc; + assert(alloc != NULL); + p = alloc->malloc(alloc->opaque, size); if (!p) { - fprintf(stderr, "fatal error: out of memory\n"); - abort(); + fz_throw(ctx, "malloc failed (%d bytes)", size); } return p; } -void * -fz_calloc(int count, int size) +void *fz_calloc(fz_context *ctx, size_t count, size_t size) { void *p; + fz_alloc_context *alloc; - if (count == 0 || size == 0) - return 0; - - if (count < 0 || size < 0 || count > INT_MAX / size) + assert(ctx != NULL); + alloc = ctx->alloc; + assert(alloc != NULL); + p = alloc->calloc(alloc->opaque, count, size); + if (!p) { - fprintf(stderr, "fatal error: out of memory (integer overflow)\n"); - abort(); + fz_throw(ctx, "calloc failed (%d x %d bytes)", count, size); } + return p; +} + +void * +fz_realloc(fz_context *ctx, void *p, size_t size) +{ + fz_alloc_context *alloc; - p = malloc(count * size); + assert(ctx != NULL); + alloc = ctx->alloc; + assert(alloc != NULL); + p = alloc->realloc(alloc->opaque, p, size); if (!p) { - fprintf(stderr, "fatal error: out of memory\n"); - abort(); + fz_throw(ctx, "realloc failed (%d bytes)", size); } return p; } +void +fz_free(fz_context *ctx, void *p) +{ + fz_alloc_context *alloc; + + assert(ctx != NULL); + alloc = ctx->alloc; + assert(alloc != NULL); + alloc->free(alloc->opaque, p); +} + void * -fz_realloc(void *p, int count, int size) +fz_malloc_nothrow(fz_context *ctx, size_t size) { - void *np; + fz_alloc_context *alloc; - if (count == 0 || size == 0) - { - fz_free(p); - return 0; - } + assert(ctx != NULL); + alloc = ctx->alloc; + assert(alloc != NULL); + return alloc->malloc(alloc->opaque, size); +} - if (count < 0 || size < 0 || count > INT_MAX / size) - { - fprintf(stderr, "fatal error: out of memory (integer overflow)\n"); - abort(); - } +void *fz_calloc_nothrow(fz_context *ctx, size_t count, size_t size) +{ + fz_alloc_context *alloc; - np = realloc(p, count * size); - if (np == NULL) - { - fprintf(stderr, "fatal error: out of memory\n"); - abort(); - } - return np; + assert(ctx != NULL); + alloc = ctx->alloc; + assert(alloc != NULL); + return alloc->calloc(alloc->opaque, count, size); +} + +void * +fz_realloc_nothrow(fz_context *ctx, void *p, size_t size) +{ + fz_alloc_context *alloc; + + assert(ctx != NULL); + alloc = ctx->alloc; + assert(alloc != NULL); + return alloc->realloc(alloc->opaque, p, size); +} + +void * +fz_malloc_default(void *opaque, size_t size) +{ + return malloc(size); +} + +void * +fz_calloc_default(void *opaque, size_t count, size_t size) +{ + return calloc(count, size); +} + +void * +fz_realloc_default(void *opaque, void *p, size_t size) +{ + return realloc(p, size); } void -fz_free(void *p) +fz_free_default(void *opaque, void *p) { free(p); } +fz_alloc_context fz_alloc_default = +{ + (void *)-1, + fz_malloc_default, + fz_realloc_default, + fz_free_default, + fz_calloc_default +}; + char * -fz_strdup(char *s) +fz_strdup(fz_context *ctx, char *s) { int len = strlen(s) + 1; - char *ns = fz_malloc(len); + char *ns = fz_malloc(ctx, len); memcpy(ns, s, len); return ns; } + +char * +fz_strdup_nothrow(fz_context *ctx, char *s) +{ + int len = strlen(s) + 1; + char *ns = fz_malloc_nothrow(ctx, len); + if (ns != NULL) + memcpy(ns, s, len); + return ns; +} diff --git a/fitz/base_object.c b/fitz/base_object.c index 4a23e5ab..5d10d860 100644 --- a/fitz/base_object.c +++ b/fitz/base_object.c @@ -1,5 +1,9 @@ #include "fitz.h" +#if defined(_WIN32) || defined(_WIN64) +#define MISSING_QSORT_R +#endif + typedef enum fz_objkind_e { FZ_NULL, @@ -52,26 +56,19 @@ struct fz_obj_s } u; }; -static fz_obj *fz_resolve_indirect_null(fz_obj *ref) -{ - return ref; -} - -fz_obj* (*fz_resolve_indirect)(fz_obj*) = fz_resolve_indirect_null; - fz_obj * -fz_new_null(void) +fz_new_null(fz_context *ctx) { - fz_obj *obj = fz_malloc(sizeof(fz_obj)); + fz_obj *obj = fz_malloc(ctx, sizeof(fz_obj)); obj->refs = 1; obj->kind = FZ_NULL; return obj; } fz_obj * -fz_new_bool(int b) +fz_new_bool(fz_context *ctx, int b) { - fz_obj *obj = fz_malloc(sizeof(fz_obj)); + fz_obj *obj = fz_malloc(ctx, sizeof(fz_obj)); obj->refs = 1; obj->kind = FZ_BOOL; obj->u.b = b; @@ -79,9 +76,9 @@ fz_new_bool(int b) } fz_obj * -fz_new_int(int i) +fz_new_int(fz_context *ctx, int i) { - fz_obj *obj = fz_malloc(sizeof(fz_obj)); + fz_obj *obj = fz_malloc(ctx, sizeof(fz_obj)); obj->refs = 1; obj->kind = FZ_INT; obj->u.i = i; @@ -89,9 +86,9 @@ fz_new_int(int i) } fz_obj * -fz_new_real(float f) +fz_new_real(fz_context *ctx, float f) { - fz_obj *obj = fz_malloc(sizeof(fz_obj)); + fz_obj *obj = fz_malloc(ctx, sizeof(fz_obj)); obj->refs = 1; obj->kind = FZ_REAL; obj->u.f = f; @@ -99,9 +96,9 @@ fz_new_real(float f) } fz_obj * -fz_new_string(char *str, int len) +fz_new_string(fz_context *ctx, char *str, int len) { - fz_obj *obj = fz_malloc(offsetof(fz_obj, u.s.buf) + len + 1); + fz_obj *obj = fz_malloc(ctx, offsetof(fz_obj, u.s.buf) + len + 1); obj->refs = 1; obj->kind = FZ_STRING; obj->u.s.len = len; @@ -111,9 +108,9 @@ fz_new_string(char *str, int len) } fz_obj * -fz_new_name(char *str) +fz_new_name(fz_context *ctx, char *str) { - fz_obj *obj = fz_malloc(offsetof(fz_obj, u.n) + strlen(str) + 1); + fz_obj *obj = fz_malloc(ctx, offsetof(fz_obj, u.n) + strlen(str) + 1); obj->refs = 1; obj->kind = FZ_NAME; strcpy(obj->u.n, str); @@ -121,9 +118,9 @@ fz_new_name(char *str) } fz_obj * -fz_new_indirect(int num, int gen, void *xref) +fz_new_indirect(fz_context *ctx, int num, int gen, void *xref) { - fz_obj *obj = fz_malloc(sizeof(fz_obj)); + fz_obj *obj = fz_malloc(ctx, sizeof(fz_obj)); obj->refs = 1; obj->kind = FZ_INDIRECT; obj->u.r.num = num; @@ -145,111 +142,111 @@ int fz_is_indirect(fz_obj *obj) return obj ? obj->kind == FZ_INDIRECT : 0; } -int fz_is_null(fz_obj *obj) +int fz_is_null(fz_context *ctx, fz_obj *obj) { - obj = fz_resolve_indirect(obj); + obj = fz_resolve_indirect(ctx, obj); return obj ? obj->kind == FZ_NULL : 0; } -int fz_is_bool(fz_obj *obj) +int fz_is_bool(fz_context *ctx, fz_obj *obj) { - obj = fz_resolve_indirect(obj); + obj = fz_resolve_indirect(ctx, obj); return obj ? obj->kind == FZ_BOOL : 0; } -int fz_is_int(fz_obj *obj) +int fz_is_int(fz_context *ctx, fz_obj *obj) { - obj = fz_resolve_indirect(obj); + obj = fz_resolve_indirect(ctx, obj); return obj ? obj->kind == FZ_INT : 0; } -int fz_is_real(fz_obj *obj) +int fz_is_real(fz_context *ctx, fz_obj *obj) { - obj = fz_resolve_indirect(obj); + obj = fz_resolve_indirect(ctx, obj); return obj ? obj->kind == FZ_REAL : 0; } -int fz_is_string(fz_obj *obj) +int fz_is_string(fz_context *ctx, fz_obj *obj) { - obj = fz_resolve_indirect(obj); + obj = fz_resolve_indirect(ctx, obj); return obj ? obj->kind == FZ_STRING : 0; } -int fz_is_name(fz_obj *obj) +int fz_is_name(fz_context *ctx, fz_obj *obj) { - obj = fz_resolve_indirect(obj); + obj = fz_resolve_indirect(ctx, obj); return obj ? obj->kind == FZ_NAME : 0; } -int fz_is_array(fz_obj *obj) +int fz_is_array(fz_context *ctx, fz_obj *obj) { - obj = fz_resolve_indirect(obj); + obj = fz_resolve_indirect(ctx, obj); return obj ? obj->kind == FZ_ARRAY : 0; } -int fz_is_dict(fz_obj *obj) +int fz_is_dict(fz_context *ctx, fz_obj *obj) { - obj = fz_resolve_indirect(obj); + obj = fz_resolve_indirect(ctx, obj); return obj ? obj->kind == FZ_DICT : 0; } -int fz_to_bool(fz_obj *obj) +int fz_to_bool(fz_context *ctx, fz_obj *obj) { - obj = fz_resolve_indirect(obj); - if (fz_is_bool(obj)) + obj = fz_resolve_indirect(ctx, obj); + if (fz_is_bool(ctx, obj)) return obj->u.b; return 0; } -int fz_to_int(fz_obj *obj) +int fz_to_int(fz_context *ctx, fz_obj *obj) { - obj = fz_resolve_indirect(obj); - if (fz_is_int(obj)) + obj = fz_resolve_indirect(ctx, obj); + if (fz_is_int(ctx, obj)) return obj->u.i; - if (fz_is_real(obj)) + if (fz_is_real(ctx, obj)) return obj->u.f; return 0; } -float fz_to_real(fz_obj *obj) +float fz_to_real(fz_context *ctx, fz_obj *obj) { - obj = fz_resolve_indirect(obj); - if (fz_is_real(obj)) + obj = fz_resolve_indirect(ctx, obj); + if (fz_is_real(ctx, obj)) return obj->u.f; - if (fz_is_int(obj)) + if (fz_is_int(ctx, obj)) return obj->u.i; return 0; } -char *fz_to_name(fz_obj *obj) +char *fz_to_name(fz_context *ctx, fz_obj *obj) { - obj = fz_resolve_indirect(obj); - if (fz_is_name(obj)) + obj = fz_resolve_indirect(ctx, obj); + if (fz_is_name(ctx, obj)) return obj->u.n; return ""; } -char *fz_to_str_buf(fz_obj *obj) +char *fz_to_str_buf(fz_context *ctx, fz_obj *obj) { - obj = fz_resolve_indirect(obj); - if (fz_is_string(obj)) + obj = fz_resolve_indirect(ctx, obj); + if (fz_is_string(ctx, obj)) return obj->u.s.buf; return ""; } -int fz_to_str_len(fz_obj *obj) +int fz_to_str_len(fz_context *ctx, fz_obj *obj) { - obj = fz_resolve_indirect(obj); - if (fz_is_string(obj)) + obj = fz_resolve_indirect(ctx, obj); + if (fz_is_string(ctx, obj)) return obj->u.s.len; return 0; } /* for use by pdf_crypt_obj_imp to decrypt AES string in place */ -void fz_set_str_len(fz_obj *obj, int newlen) +void fz_set_str_len(fz_context *ctx, fz_obj *obj, int newlen) { - obj = fz_resolve_indirect(obj); - if (fz_is_string(obj)) + obj = fz_resolve_indirect(ctx, obj); + if (fz_is_string(ctx, obj)) if (newlen < obj->u.s.len) obj->u.s.len = newlen; } @@ -375,19 +372,19 @@ fz_objkindstr(fz_obj *obj) } fz_obj * -fz_new_array(int initialcap) +fz_new_array(fz_context *ctx, int initialcap) { fz_obj *obj; int i; - obj = fz_malloc(sizeof(fz_obj)); + obj = fz_malloc(ctx, sizeof(fz_obj)); obj->refs = 1; obj->kind = FZ_ARRAY; obj->u.a.len = 0; obj->u.a.cap = initialcap > 1 ? initialcap : 6; - obj->u.a.items = fz_calloc(obj->u.a.cap, sizeof(fz_obj*)); + obj->u.a.items = fz_calloc(ctx, obj->u.a.cap, sizeof(fz_obj*)); for (i = 0; i < obj->u.a.cap; i++) obj->u.a.items[i] = NULL; @@ -395,36 +392,38 @@ fz_new_array(int initialcap) } fz_obj * -fz_copy_array(fz_obj *obj) +fz_copy_array(fz_context *ctx, fz_obj *obj) { fz_obj *new; int i; + int n; - if (fz_is_indirect(obj) || !fz_is_array(obj)) + if (fz_is_indirect(obj) || !fz_is_array(ctx, obj)) fz_warn("assert: not an array (%s)", fz_objkindstr(obj)); - new = fz_new_array(fz_array_len(obj)); - for (i = 0; i < fz_array_len(obj); i++) - fz_array_push(new, fz_array_get(obj, i)); + new = fz_new_array(ctx, fz_array_len(ctx, obj)); + n = fz_array_len(ctx, obj); + for (i = 0; i < n; i++) + fz_array_push(ctx, new, fz_array_get(ctx, obj, i)); return new; } int -fz_array_len(fz_obj *obj) +fz_array_len(fz_context *ctx, fz_obj *obj) { - obj = fz_resolve_indirect(obj); - if (!fz_is_array(obj)) + obj = fz_resolve_indirect(ctx, obj); + if (!fz_is_array(ctx, obj)) return 0; return obj->u.a.len; } fz_obj * -fz_array_get(fz_obj *obj, int i) +fz_array_get(fz_context *ctx, fz_obj *obj, int i) { - obj = fz_resolve_indirect(obj); + obj = fz_resolve_indirect(ctx, obj); - if (!fz_is_array(obj)) + if (!fz_is_array(ctx, obj)) return NULL; if (i < 0 || i >= obj->u.a.len) @@ -434,11 +433,11 @@ fz_array_get(fz_obj *obj, int i) } void -fz_array_put(fz_obj *obj, int i, fz_obj *item) +fz_array_put(fz_context *ctx, fz_obj *obj, int i, fz_obj *item) { - obj = fz_resolve_indirect(obj); + obj = fz_resolve_indirect(ctx, obj); - if (!fz_is_array(obj)) + if (!fz_is_array(ctx, obj)) fz_warn("assert: not an array (%s)", fz_objkindstr(obj)); else if (i < 0) fz_warn("assert: index %d < 0", i); @@ -447,17 +446,17 @@ fz_array_put(fz_obj *obj, int i, fz_obj *item) else { if (obj->u.a.items[i]) - fz_drop_obj(obj->u.a.items[i]); + fz_drop_obj(ctx, obj->u.a.items[i]); obj->u.a.items[i] = fz_keep_obj(item); } } void -fz_array_push(fz_obj *obj, fz_obj *item) +fz_array_push(fz_context *ctx, fz_obj *obj, fz_obj *item) { - obj = fz_resolve_indirect(obj); + obj = fz_resolve_indirect(ctx, obj); - if (!fz_is_array(obj)) + if (!fz_is_array(ctx, obj)) fz_warn("assert: not an array (%s)", fz_objkindstr(obj)); else { @@ -465,7 +464,7 @@ fz_array_push(fz_obj *obj, fz_obj *item) { int i; obj->u.a.cap = (obj->u.a.cap * 3) / 2; - obj->u.a.items = fz_realloc(obj->u.a.items, obj->u.a.cap, sizeof(fz_obj*)); + obj->u.a.items = fz_realloc(ctx, obj->u.a.items, obj->u.a.cap * sizeof(fz_obj*)); for (i = obj->u.a.len ; i < obj->u.a.cap; i++) obj->u.a.items[i] = NULL; } @@ -475,11 +474,11 @@ fz_array_push(fz_obj *obj, fz_obj *item) } void -fz_array_insert(fz_obj *obj, fz_obj *item) +fz_array_insert(fz_context *ctx, fz_obj *obj, fz_obj *item) { - obj = fz_resolve_indirect(obj); + obj = fz_resolve_indirect(ctx, obj); - if (!fz_is_array(obj)) + if (!fz_is_array(ctx, obj)) fz_warn("assert: not an array (%s)", fz_objkindstr(obj)); else { @@ -487,7 +486,7 @@ fz_array_insert(fz_obj *obj, fz_obj *item) { int i; obj->u.a.cap = (obj->u.a.cap * 3) / 2; - obj->u.a.items = fz_realloc(obj->u.a.items, obj->u.a.cap, sizeof(fz_obj*)); + obj->u.a.items = fz_realloc(ctx, obj->u.a.items, obj->u.a.cap * sizeof(fz_obj*)); for (i = obj->u.a.len ; i < obj->u.a.cap; i++) obj->u.a.items[i] = NULL; } @@ -499,20 +498,29 @@ fz_array_insert(fz_obj *obj, fz_obj *item) /* dicts may only have names as keys! */ -static int keyvalcmp(const void *ap, const void *bp) +static int keyvalcmp(void *ctxp, const void *ap, const void *bp) { + fz_context *ctx = (fz_context *)ctxp; const struct keyval *a = ap; const struct keyval *b = bp; - return strcmp(fz_to_name(a->k), fz_to_name(b->k)); + return strcmp(fz_to_name(ctx, a->k), fz_to_name(ctx, b->k)); } +#ifdef MISSING_QSORT_R +static void *qsort_r_hack; +static int keyvalcmp_hack(const void *ap, const void *bp) +{ + return keyvalcmp(qsort_r_hack, ap, bp); +} +#endif + fz_obj * -fz_new_dict(int initialcap) +fz_new_dict(fz_context *ctx, int initialcap) { fz_obj *obj; int i; - obj = fz_malloc(sizeof(fz_obj)); + obj = fz_malloc(ctx, sizeof(fz_obj)); obj->refs = 1; obj->kind = FZ_DICT; @@ -520,7 +528,7 @@ fz_new_dict(int initialcap) obj->u.d.len = 0; obj->u.d.cap = initialcap > 1 ? initialcap : 10; - obj->u.d.items = fz_calloc(obj->u.d.cap, sizeof(struct keyval)); + obj->u.d.items = fz_calloc(ctx, obj->u.d.cap, sizeof(struct keyval)); for (i = 0; i < obj->u.d.cap; i++) { obj->u.d.items[i].k = NULL; @@ -531,36 +539,37 @@ fz_new_dict(int initialcap) } fz_obj * -fz_copy_dict(fz_obj *obj) +fz_copy_dict(fz_context *ctx, fz_obj *obj) { fz_obj *new; - int i; + int i, n; - if (fz_is_indirect(obj) || !fz_is_dict(obj)) + if (fz_is_indirect(obj) || !fz_is_dict(ctx, obj)) fz_error_make("assert: not a dict (%s)", fz_objkindstr(obj)); - new = fz_new_dict(fz_dict_len(obj)); - for (i = 0; i < fz_dict_len(obj); i++) - fz_dict_put(new, fz_dict_get_key(obj, i), fz_dict_get_val(obj, i)); + n = fz_dict_len(ctx, obj); + new = fz_new_dict(ctx, n); + for (i = 0; i < n; i++) + fz_dict_put(ctx, new, fz_dict_get_key(ctx, obj, i), fz_dict_get_val(ctx, obj, i)); return new; } int -fz_dict_len(fz_obj *obj) +fz_dict_len(fz_context *ctx, fz_obj *obj) { - obj = fz_resolve_indirect(obj); - if (!fz_is_dict(obj)) + obj = fz_resolve_indirect(ctx, obj); + if (!fz_is_dict(ctx, obj)) return 0; return obj->u.d.len; } fz_obj * -fz_dict_get_key(fz_obj *obj, int i) +fz_dict_get_key(fz_context *ctx, fz_obj *obj, int i) { - obj = fz_resolve_indirect(obj); + obj = fz_resolve_indirect(ctx, obj); - if (!fz_is_dict(obj)) + if (!fz_is_dict(ctx, obj)) return NULL; if (i < 0 || i >= obj->u.d.len) @@ -570,11 +579,11 @@ fz_dict_get_key(fz_obj *obj, int i) } fz_obj * -fz_dict_get_val(fz_obj *obj, int i) +fz_dict_get_val(fz_context *ctx, fz_obj *obj, int i) { - obj = fz_resolve_indirect(obj); + obj = fz_resolve_indirect(ctx, obj); - if (!fz_is_dict(obj)) + if (!fz_is_dict(ctx, obj)) return NULL; if (i < 0 || i >= obj->u.d.len) @@ -584,7 +593,7 @@ fz_dict_get_val(fz_obj *obj, int i) } static int -fz_dict_finds(fz_obj *obj, char *key) +fz_dict_finds(fz_context *ctx, fz_obj *obj, char *key) { if (obj->u.d.sorted) { @@ -593,7 +602,7 @@ fz_dict_finds(fz_obj *obj, char *key) while (l <= r) { int m = (l + r) >> 1; - int c = -strcmp(fz_to_name(obj->u.d.items[m].k), key); + int c = -strcmp(fz_to_name(ctx, obj->u.d.items[m].k), key); if (c < 0) r = m - 1; else if (c > 0) @@ -607,7 +616,7 @@ fz_dict_finds(fz_obj *obj, char *key) { int i; for (i = 0; i < obj->u.d.len; i++) - if (strcmp(fz_to_name(obj->u.d.items[i].k), key) == 0) + if (strcmp(fz_to_name(ctx, obj->u.d.items[i].k), key) == 0) return i; } @@ -615,16 +624,16 @@ fz_dict_finds(fz_obj *obj, char *key) } fz_obj * -fz_dict_gets(fz_obj *obj, char *key) +fz_dict_gets(fz_context *ctx, fz_obj *obj, char *key) { int i; - obj = fz_resolve_indirect(obj); + obj = fz_resolve_indirect(ctx, obj); - if (!fz_is_dict(obj)) + if (!fz_is_dict(ctx, obj)) return NULL; - i = fz_dict_finds(obj, key); + i = fz_dict_finds(ctx, obj, key); if (i >= 0) return obj->u.d.items[i].v; @@ -632,39 +641,39 @@ fz_dict_gets(fz_obj *obj, char *key) } fz_obj * -fz_dict_get(fz_obj *obj, fz_obj *key) +fz_dict_get(fz_context *ctx, fz_obj *obj, fz_obj *key) { - if (fz_is_name(key)) - return fz_dict_gets(obj, fz_to_name(key)); + if (fz_is_name(ctx, key)) + return fz_dict_gets(ctx, obj, fz_to_name(ctx, key)); return NULL; } fz_obj * -fz_dict_getsa(fz_obj *obj, char *key, char *abbrev) +fz_dict_getsa(fz_context *ctx, fz_obj *obj, char *key, char *abbrev) { fz_obj *v; - v = fz_dict_gets(obj, key); + v = fz_dict_gets(ctx, obj, key); if (v) return v; - return fz_dict_gets(obj, abbrev); + return fz_dict_gets(ctx, obj, abbrev); } void -fz_dict_put(fz_obj *obj, fz_obj *key, fz_obj *val) +fz_dict_put(fz_context *ctx, fz_obj *obj, fz_obj *key, fz_obj *val) { char *s; int i; - obj = fz_resolve_indirect(obj); + obj = fz_resolve_indirect(ctx, obj); - if (!fz_is_dict(obj)) + if (!fz_is_dict(ctx, obj)) { fz_warn("assert: not a dict (%s)", fz_objkindstr(obj)); return; } - if (fz_is_name(key)) - s = fz_to_name(key); + if (fz_is_name(ctx, key)) + s = fz_to_name(ctx, key); else { fz_warn("assert: key is not a name (%s)", fz_objkindstr(obj)); @@ -677,10 +686,10 @@ fz_dict_put(fz_obj *obj, fz_obj *key, fz_obj *val) return; } - i = fz_dict_finds(obj, s); + i = fz_dict_finds(ctx, obj, s); if (i >= 0) { - fz_drop_obj(obj->u.d.items[i].v); + fz_drop_obj(ctx, obj->u.d.items[i].v); obj->u.d.items[i].v = fz_keep_obj(val); return; } @@ -688,7 +697,7 @@ fz_dict_put(fz_obj *obj, fz_obj *key, fz_obj *val) if (obj->u.d.len + 1 > obj->u.d.cap) { obj->u.d.cap = (obj->u.d.cap * 3) / 2; - obj->u.d.items = fz_realloc(obj->u.d.items, obj->u.d.cap, sizeof(struct keyval)); + obj->u.d.items = fz_realloc(ctx, obj->u.d.items, obj->u.d.cap * sizeof(struct keyval)); for (i = obj->u.d.len; i < obj->u.d.cap; i++) { obj->u.d.items[i].k = NULL; @@ -698,7 +707,7 @@ fz_dict_put(fz_obj *obj, fz_obj *key, fz_obj *val) /* borked! */ if (obj->u.d.len) - if (strcmp(fz_to_name(obj->u.d.items[obj->u.d.len - 1].k), s) > 0) + if (strcmp(fz_to_name(ctx, obj->u.d.items[obj->u.d.len - 1].k), s) > 0) obj->u.d.sorted = 0; obj->u.d.items[obj->u.d.len].k = fz_keep_obj(key); @@ -707,27 +716,27 @@ fz_dict_put(fz_obj *obj, fz_obj *key, fz_obj *val) } void -fz_dict_puts(fz_obj *obj, char *key, fz_obj *val) +fz_dict_puts(fz_context *ctx, fz_obj *obj, char *key, fz_obj *val) { - fz_obj *keyobj = fz_new_name(key); - fz_dict_put(obj, keyobj, val); - fz_drop_obj(keyobj); + fz_obj *keyobj = fz_new_name(ctx, key); + fz_dict_put(ctx, obj, keyobj, val); + fz_drop_obj(ctx, keyobj); } void -fz_dict_dels(fz_obj *obj, char *key) +fz_dict_dels(fz_context *ctx, fz_obj *obj, char *key) { - obj = fz_resolve_indirect(obj); + obj = fz_resolve_indirect(ctx, obj); - if (!fz_is_dict(obj)) + if (!fz_is_dict(ctx, obj)) fz_warn("assert: not a dict (%s)", fz_objkindstr(obj)); else { - int i = fz_dict_finds(obj, key); + int i = fz_dict_finds(ctx, obj, key); if (i >= 0) { - fz_drop_obj(obj->u.d.items[i].k); - fz_drop_obj(obj->u.d.items[i].v); + fz_drop_obj(ctx, obj->u.d.items[i].k); + fz_drop_obj(ctx, obj->u.d.items[i].v); obj->u.d.sorted = 0; obj->u.d.items[i] = obj->u.d.items[obj->u.d.len-1]; obj->u.d.len --; @@ -736,67 +745,72 @@ fz_dict_dels(fz_obj *obj, char *key) } void -fz_dict_del(fz_obj *obj, fz_obj *key) +fz_dict_del(fz_context *ctx, fz_obj *obj, fz_obj *key) { - if (fz_is_name(key)) - fz_dict_dels(obj, fz_to_name(key)); + if (fz_is_name(ctx, key)) + fz_dict_dels(ctx, obj, fz_to_name(ctx, key)); else fz_warn("assert: key is not a name (%s)", fz_objkindstr(obj)); } void -fz_sort_dict(fz_obj *obj) +fz_sort_dict(fz_context *ctx, fz_obj *obj) { - obj = fz_resolve_indirect(obj); - if (!fz_is_dict(obj)) + obj = fz_resolve_indirect(ctx, obj); + if (!fz_is_dict(ctx, obj)) return; if (!obj->u.d.sorted) { - qsort(obj->u.d.items, obj->u.d.len, sizeof(struct keyval), keyvalcmp); - obj->u.d.sorted = 1; +#ifdef MISSING_QSORT_R + qsort_r_hack = ctx; + qsort(obj->u.d.items, obj->u.d.len, sizeof(struct keyval), keyvalcmp_hack); +#else + qsort_r(obj->u.d.items, obj->u.d.len, sizeof(struct keyval), ctx, keyvalcmp); +#endif + obj->u.d.sorted = 1; } } static void -fz_free_array(fz_obj *obj) +fz_free_array(fz_context *ctx, fz_obj *obj) { int i; for (i = 0; i < obj->u.a.len; i++) if (obj->u.a.items[i]) - fz_drop_obj(obj->u.a.items[i]); + fz_drop_obj(ctx, obj->u.a.items[i]); - fz_free(obj->u.a.items); - fz_free(obj); + fz_free(ctx, obj->u.a.items); + fz_free(ctx, obj); } static void -fz_free_dict(fz_obj *obj) +fz_free_dict(fz_context *ctx, fz_obj *obj) { int i; for (i = 0; i < obj->u.d.len; i++) { if (obj->u.d.items[i].k) - fz_drop_obj(obj->u.d.items[i].k); + fz_drop_obj(ctx, obj->u.d.items[i].k); if (obj->u.d.items[i].v) - fz_drop_obj(obj->u.d.items[i].v); + fz_drop_obj(ctx, obj->u.d.items[i].v); } - fz_free(obj->u.d.items); - fz_free(obj); + fz_free(ctx, obj->u.d.items); + fz_free(ctx, obj); } void -fz_drop_obj(fz_obj *obj) +fz_drop_obj(fz_context *ctx, fz_obj *obj) { assert(obj != NULL); if (--obj->refs == 0) { if (obj->kind == FZ_ARRAY) - fz_free_array(obj); + fz_free_array(ctx, obj); else if (obj->kind == FZ_DICT) - fz_free_dict(obj); + fz_free_dict(ctx, obj); else - fz_free(obj); + fz_free(ctx, obj); } } diff --git a/fitz/context.c b/fitz/context.c new file mode 100644 index 00000000..aee521b6 --- /dev/null +++ b/fitz/context.c @@ -0,0 +1,63 @@ +#include "fitz.h" +#include "except.h" + +void fz_context_fin(fz_context *ctx) +{ + assert(ctx != NULL); + + /* Other finalisation calls go here (in reverse order) */ + fz_except_fin(ctx); + /* Free the context itself */ + ctx->alloc->free(ctx->alloc->opaque, ctx); + /* We do NOT free the allocator! */ +} + +fz_obj *fz_resolve_indirect_null(fz_obj *ref) +{ + return ref; +} + +fz_context *fz_context_init(fz_alloc_context *alloc) +{ + fz_context *ctx; + fz_error error; + + assert(alloc != NULL); + ctx = alloc->calloc(alloc->opaque, 1, sizeof(fz_context)); + if (ctx == NULL) + return NULL; + ctx->alloc = alloc; + + error = fz_except_init(ctx); + if (error != fz_okay) + goto cleanup; + ctx->fz_resolve_indirect = fz_resolve_indirect_null; + + /* New initialisation calls for context entries go here */ + return ctx; + cleanup: + fz_context_fin(ctx); + fz_error_handle(error, "fz_context_init failed"); + return NULL; +} + +fz_context *fz_context_clone(fz_context *ctx) +{ + fz_context *clone; + fz_error error; + + clone = ctx->alloc->calloc(ctx->alloc->opaque, 1, sizeof(*clone)); + if (clone == NULL) + return NULL; + clone->alloc = ctx->alloc; + error = fz_except_init(ctx); + if (error != fz_okay) + goto cleanup; + /* Other initialisations go here; either a copy (probably refcounted) + * or a new initialisation. */ + return clone; + cleanup: + fz_context_fin(ctx); + fz_error_handle(error, "fz_context_clone failed"); + return NULL; +} diff --git a/fitz/crypt_arc4.c b/fitz/crypt_arc4.c index 72871386..272891ce 100644 --- a/fitz/crypt_arc4.c +++ b/fitz/crypt_arc4.c @@ -24,7 +24,7 @@ #include "fitz.h" void -fz_arc4_init(fz_arc4 *arc4, const unsigned char *key, const unsigned keylen) +fz_arc4_init(fz_arc4 *arc4, const unsigned char *key, unsigned keylen) { unsigned int t, u; unsigned int keyindex; @@ -86,7 +86,7 @@ fz_arc4_next(fz_arc4 *arc4) } void -fz_arc4_encrypt(fz_arc4 *arc4, unsigned char *dest, const unsigned char *src, const unsigned len) +fz_arc4_encrypt(fz_arc4 *arc4, unsigned char *dest, const unsigned char *src, unsigned len) { unsigned int i; for (i = 0; i < len; i++) diff --git a/fitz/crypt_md5.c b/fitz/crypt_md5.c index ba2571c4..b6e06845 100644 --- a/fitz/crypt_md5.c +++ b/fitz/crypt_md5.c @@ -210,7 +210,7 @@ void fz_md5_init(fz_md5 *context) /* MD5 block update operation. Continues an MD5 message-digest operation, * processing another message block, and updating the context. */ -void fz_md5_update(fz_md5 *context, const unsigned char *input, const unsigned inlen) +void fz_md5_update(fz_md5 *context, const unsigned char *input, unsigned inlen) { unsigned i, index, partlen; diff --git a/fitz/dev_bbox.c b/fitz/dev_bbox.c index 179ff030..e83cbcc5 100644 --- a/fitz/dev_bbox.c +++ b/fitz/dev_bbox.c @@ -3,70 +3,70 @@ /* TODO: add clip stack and use to intersect bboxes */ static void -fz_bbox_fill_path(void *user, fz_path *path, int even_odd, fz_matrix ctm, +fz_bbox_fill_path(fz_device *dev, fz_path *path, int even_odd, fz_matrix ctm, fz_colorspace *colorspace, float *color, float alpha) { - fz_bbox *result = user; + fz_bbox *result = dev->user; fz_bbox bbox = fz_round_rect(fz_bound_path(path, NULL, ctm)); *result = fz_union_bbox(*result, bbox); } static void -fz_bbox_stroke_path(void *user, fz_path *path, fz_stroke_state *stroke, fz_matrix ctm, +fz_bbox_stroke_path(fz_device *dev, fz_path *path, fz_stroke_state *stroke, fz_matrix ctm, fz_colorspace *colorspace, float *color, float alpha) { - fz_bbox *result = user; + fz_bbox *result = dev->user; fz_bbox bbox = fz_round_rect(fz_bound_path(path, stroke, ctm)); *result = fz_union_bbox(*result, bbox); } static void -fz_bbox_fill_text(void *user, fz_text *text, fz_matrix ctm, +fz_bbox_fill_text(fz_device *dev, fz_text *text, fz_matrix ctm, fz_colorspace *colorspace, float *color, float alpha) { - fz_bbox *result = user; + fz_bbox *result = dev->user; fz_bbox bbox = fz_round_rect(fz_bound_text(text, ctm)); *result = fz_union_bbox(*result, bbox); } static void -fz_bbox_stroke_text(void *user, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm, +fz_bbox_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm, fz_colorspace *colorspace, float *color, float alpha) { - fz_bbox *result = user; + fz_bbox *result = dev->user; fz_bbox bbox = fz_round_rect(fz_bound_text(text, ctm)); *result = fz_union_bbox(*result, bbox); } static void -fz_bbox_fill_shade(void *user, fz_shade *shade, fz_matrix ctm, float alpha) +fz_bbox_fill_shade(fz_device *dev, fz_shade *shade, fz_matrix ctm, float alpha) { - fz_bbox *result = user; + fz_bbox *result = dev->user; fz_bbox bbox = fz_round_rect(fz_bound_shade(shade, ctm)); *result = fz_union_bbox(*result, bbox); } static void -fz_bbox_fill_image(void *user, fz_pixmap *image, fz_matrix ctm, float alpha) +fz_bbox_fill_image(fz_device *dev, fz_pixmap *image, fz_matrix ctm, float alpha) { - fz_bbox *result = user; + fz_bbox *result = dev->user; fz_bbox bbox = fz_round_rect(fz_transform_rect(ctm, fz_unit_rect)); *result = fz_union_bbox(*result, bbox); } static void -fz_bbox_fill_image_mask(void *user, fz_pixmap *image, fz_matrix ctm, +fz_bbox_fill_image_mask(fz_device *dev, fz_pixmap *image, fz_matrix ctm, fz_colorspace *colorspace, float *color, float alpha) { - fz_bbox_fill_image(user, image, ctm, alpha); + fz_bbox_fill_image(dev->user, image, ctm, alpha); } fz_device * -fz_new_bbox_device(fz_bbox *result) +fz_new_bbox_device(fz_context *ctx, fz_bbox *result) { fz_device *dev; - dev = fz_new_device(result); + dev = fz_new_device(ctx, result); dev->fill_path = fz_bbox_fill_path; dev->stroke_path = fz_bbox_stroke_path; diff --git a/fitz/dev_list.c b/fitz/dev_list.c index 955824f9..63e9eee0 100644 --- a/fitz/dev_list.c +++ b/fitz/dev_list.c @@ -64,13 +64,13 @@ struct fz_display_list_s enum { ISOLATED = 1, KNOCKOUT = 2 }; static fz_display_node * -fz_new_display_node(fz_display_command cmd, fz_matrix ctm, +fz_new_display_node(fz_context *ctx, fz_display_command cmd, fz_matrix ctm, fz_colorspace *colorspace, float *color, float alpha) { fz_display_node *node; int i; - node = fz_malloc(sizeof(fz_display_node)); + node = fz_malloc(ctx, sizeof(fz_display_node)); node->cmd = cmd; node->next = NULL; node->rect = fz_empty_rect; @@ -97,9 +97,9 @@ fz_new_display_node(fz_display_command cmd, fz_matrix ctm, } static fz_stroke_state * -fz_clone_stroke_state(fz_stroke_state *stroke) +fz_clone_stroke_state(fz_context *ctx, fz_stroke_state *stroke) { - fz_stroke_state *newstroke = fz_malloc(sizeof(fz_stroke_state)); + fz_stroke_state *newstroke = fz_malloc(ctx, sizeof(fz_stroke_state)); *newstroke = *stroke; return newstroke; } @@ -184,7 +184,7 @@ fz_append_display_node(fz_display_list *list, fz_display_node *node) } static void -fz_free_display_node(fz_display_node *node) +fz_free_display_node(fz_context *ctx, fz_display_node *node) { switch (node->cmd) { @@ -192,22 +192,22 @@ fz_free_display_node(fz_display_node *node) case FZ_CMD_STROKE_PATH: case FZ_CMD_CLIP_PATH: case FZ_CMD_CLIP_STROKE_PATH: - fz_free_path(node->item.path); + fz_free_path(ctx, node->item.path); break; case FZ_CMD_FILL_TEXT: case FZ_CMD_STROKE_TEXT: case FZ_CMD_CLIP_TEXT: case FZ_CMD_CLIP_STROKE_TEXT: case FZ_CMD_IGNORE_TEXT: - fz_free_text(node->item.text); + fz_free_text(ctx, node->item.text); break; case FZ_CMD_FILL_SHADE: - fz_drop_shade(node->item.shade); + fz_drop_shade(ctx, node->item.shade); break; case FZ_CMD_FILL_IMAGE: case FZ_CMD_FILL_IMAGE_MASK: case FZ_CMD_CLIP_IMAGE_MASK: - fz_drop_pixmap(node->item.image); + fz_drop_pixmap(ctx, node->item.image); break; case FZ_CMD_POP_CLIP: case FZ_CMD_BEGIN_MASK: @@ -219,214 +219,214 @@ fz_free_display_node(fz_display_node *node) break; } if (node->stroke) - fz_free(node->stroke); + fz_free(ctx, node->stroke); if (node->colorspace) - fz_drop_colorspace(node->colorspace); - fz_free(node); + fz_drop_colorspace(ctx, node->colorspace); + fz_free(ctx, node); } static void -fz_list_fill_path(void *user, fz_path *path, int even_odd, fz_matrix ctm, +fz_list_fill_path(fz_device *dev, fz_path *path, int even_odd, fz_matrix ctm, fz_colorspace *colorspace, float *color, float alpha) { fz_display_node *node; - node = fz_new_display_node(FZ_CMD_FILL_PATH, ctm, colorspace, color, alpha); + node = fz_new_display_node(dev->ctx, FZ_CMD_FILL_PATH, ctm, colorspace, color, alpha); node->rect = fz_bound_path(path, NULL, ctm); - node->item.path = fz_clone_path(path); + node->item.path = fz_clone_path(dev->ctx, path); node->flag = even_odd; - fz_append_display_node(user, node); + fz_append_display_node(dev->user, node); } static void -fz_list_stroke_path(void *user, fz_path *path, fz_stroke_state *stroke, fz_matrix ctm, +fz_list_stroke_path(fz_device *dev, fz_path *path, fz_stroke_state *stroke, fz_matrix ctm, fz_colorspace *colorspace, float *color, float alpha) { fz_display_node *node; - node = fz_new_display_node(FZ_CMD_STROKE_PATH, ctm, colorspace, color, alpha); + node = fz_new_display_node(dev->ctx, FZ_CMD_STROKE_PATH, ctm, colorspace, color, alpha); node->rect = fz_bound_path(path, stroke, ctm); - node->item.path = fz_clone_path(path); - node->stroke = fz_clone_stroke_state(stroke); - fz_append_display_node(user, node); + node->item.path = fz_clone_path(dev->ctx, path); + node->stroke = fz_clone_stroke_state(dev->ctx, stroke); + fz_append_display_node(dev->user, node); } static void -fz_list_clip_path(void *user, fz_path *path, fz_rect *rect, int even_odd, fz_matrix ctm) +fz_list_clip_path(fz_device *dev, fz_path *path, fz_rect *rect, int even_odd, fz_matrix ctm) { fz_display_node *node; - node = fz_new_display_node(FZ_CMD_CLIP_PATH, ctm, NULL, NULL, 0); + node = fz_new_display_node(dev->ctx, FZ_CMD_CLIP_PATH, ctm, NULL, NULL, 0); node->rect = fz_bound_path(path, NULL, ctm); if (rect != NULL) node->rect = fz_intersect_rect(node->rect, *rect); - node->item.path = fz_clone_path(path); + node->item.path = fz_clone_path(dev->ctx, path); node->flag = even_odd; - fz_append_display_node(user, node); + fz_append_display_node(dev->user, node); } static void -fz_list_clip_stroke_path(void *user, fz_path *path, fz_rect *rect, fz_stroke_state *stroke, fz_matrix ctm) +fz_list_clip_stroke_path(fz_device *dev, fz_path *path, fz_rect *rect, fz_stroke_state *stroke, fz_matrix ctm) { fz_display_node *node; - node = fz_new_display_node(FZ_CMD_CLIP_STROKE_PATH, ctm, NULL, NULL, 0); + node = fz_new_display_node(dev->ctx, FZ_CMD_CLIP_STROKE_PATH, ctm, NULL, NULL, 0); node->rect = fz_bound_path(path, stroke, ctm); if (rect != NULL) node->rect = fz_intersect_rect(node->rect, *rect); - node->item.path = fz_clone_path(path); - node->stroke = fz_clone_stroke_state(stroke); - fz_append_display_node(user, node); + node->item.path = fz_clone_path(dev->ctx, path); + node->stroke = fz_clone_stroke_state(dev->ctx, stroke); + fz_append_display_node(dev->user, node); } static void -fz_list_fill_text(void *user, fz_text *text, fz_matrix ctm, +fz_list_fill_text(fz_device *dev, fz_text *text, fz_matrix ctm, fz_colorspace *colorspace, float *color, float alpha) { fz_display_node *node; - node = fz_new_display_node(FZ_CMD_FILL_TEXT, ctm, colorspace, color, alpha); + node = fz_new_display_node(dev->ctx, FZ_CMD_FILL_TEXT, ctm, colorspace, color, alpha); node->rect = fz_bound_text(text, ctm); - node->item.text = fz_clone_text(text); - fz_append_display_node(user, node); + node->item.text = fz_clone_text(dev->ctx, text); + fz_append_display_node(dev->user, node); } static void -fz_list_stroke_text(void *user, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm, +fz_list_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm, fz_colorspace *colorspace, float *color, float alpha) { fz_display_node *node; - node = fz_new_display_node(FZ_CMD_STROKE_TEXT, ctm, colorspace, color, alpha); + node = fz_new_display_node(dev->ctx, FZ_CMD_STROKE_TEXT, ctm, colorspace, color, alpha); node->rect = fz_bound_text(text, ctm); - node->item.text = fz_clone_text(text); - node->stroke = fz_clone_stroke_state(stroke); - fz_append_display_node(user, node); + node->item.text = fz_clone_text(dev->ctx, text); + node->stroke = fz_clone_stroke_state(dev->ctx, stroke); + fz_append_display_node(dev->user, node); } static void -fz_list_clip_text(void *user, fz_text *text, fz_matrix ctm, int accumulate) +fz_list_clip_text(fz_device *dev, fz_text *text, fz_matrix ctm, int accumulate) { fz_display_node *node; - node = fz_new_display_node(FZ_CMD_CLIP_TEXT, ctm, NULL, NULL, 0); + node = fz_new_display_node(dev->ctx, FZ_CMD_CLIP_TEXT, ctm, NULL, NULL, 0); node->rect = fz_bound_text(text, ctm); - node->item.text = fz_clone_text(text); + node->item.text = fz_clone_text(dev->ctx, text); node->flag = accumulate; /* when accumulating, be conservative about culling */ if (accumulate) node->rect = fz_infinite_rect; - fz_append_display_node(user, node); + fz_append_display_node(dev->user, node); } static void -fz_list_clip_stroke_text(void *user, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm) +fz_list_clip_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm) { fz_display_node *node; - node = fz_new_display_node(FZ_CMD_CLIP_STROKE_TEXT, ctm, NULL, NULL, 0); + node = fz_new_display_node(dev->ctx, FZ_CMD_CLIP_STROKE_TEXT, ctm, NULL, NULL, 0); node->rect = fz_bound_text(text, ctm); - node->item.text = fz_clone_text(text); - node->stroke = fz_clone_stroke_state(stroke); - fz_append_display_node(user, node); + node->item.text = fz_clone_text(dev->ctx, text); + node->stroke = fz_clone_stroke_state(dev->ctx, stroke); + fz_append_display_node(dev->user, node); } static void -fz_list_ignore_text(void *user, fz_text *text, fz_matrix ctm) +fz_list_ignore_text(fz_device *dev, fz_text *text, fz_matrix ctm) { fz_display_node *node; - node = fz_new_display_node(FZ_CMD_IGNORE_TEXT, ctm, NULL, NULL, 0); + node = fz_new_display_node(dev->ctx, FZ_CMD_IGNORE_TEXT, ctm, NULL, NULL, 0); node->rect = fz_bound_text(text, ctm); - node->item.text = fz_clone_text(text); - fz_append_display_node(user, node); + node->item.text = fz_clone_text(dev->ctx, text); + fz_append_display_node(dev->user, node); } static void -fz_list_pop_clip(void *user) +fz_list_pop_clip(fz_device *dev) { fz_display_node *node; - node = fz_new_display_node(FZ_CMD_POP_CLIP, fz_identity, NULL, NULL, 0); - fz_append_display_node(user, node); + node = fz_new_display_node(dev->ctx, FZ_CMD_POP_CLIP, fz_identity, NULL, NULL, 0); + fz_append_display_node(dev->user, node); } static void -fz_list_fill_shade(void *user, fz_shade *shade, fz_matrix ctm, float alpha) +fz_list_fill_shade(fz_device *dev, fz_shade *shade, fz_matrix ctm, float alpha) { fz_display_node *node; - node = fz_new_display_node(FZ_CMD_FILL_SHADE, ctm, NULL, NULL, alpha); + node = fz_new_display_node(dev->ctx, FZ_CMD_FILL_SHADE, ctm, NULL, NULL, alpha); node->rect = fz_bound_shade(shade, ctm); node->item.shade = fz_keep_shade(shade); - fz_append_display_node(user, node); + fz_append_display_node(dev->user, node); } static void -fz_list_fill_image(void *user, fz_pixmap *image, fz_matrix ctm, float alpha) +fz_list_fill_image(fz_device *dev, fz_pixmap *image, fz_matrix ctm, float alpha) { fz_display_node *node; - node = fz_new_display_node(FZ_CMD_FILL_IMAGE, ctm, NULL, NULL, alpha); + node = fz_new_display_node(dev->ctx, FZ_CMD_FILL_IMAGE, ctm, NULL, NULL, alpha); node->rect = fz_transform_rect(ctm, fz_unit_rect); node->item.image = fz_keep_pixmap(image); - fz_append_display_node(user, node); + fz_append_display_node(dev->user, node); } static void -fz_list_fill_image_mask(void *user, fz_pixmap *image, fz_matrix ctm, +fz_list_fill_image_mask(fz_device *dev, fz_pixmap *image, fz_matrix ctm, fz_colorspace *colorspace, float *color, float alpha) { fz_display_node *node; - node = fz_new_display_node(FZ_CMD_FILL_IMAGE_MASK, ctm, colorspace, color, alpha); + node = fz_new_display_node(dev->ctx, FZ_CMD_FILL_IMAGE_MASK, ctm, colorspace, color, alpha); node->rect = fz_transform_rect(ctm, fz_unit_rect); node->item.image = fz_keep_pixmap(image); - fz_append_display_node(user, node); + fz_append_display_node(dev->user, node); } static void -fz_list_clip_image_mask(void *user, fz_pixmap *image, fz_rect *rect, fz_matrix ctm) +fz_list_clip_image_mask(fz_device *dev, fz_pixmap *image, fz_rect *rect, fz_matrix ctm) { fz_display_node *node; - node = fz_new_display_node(FZ_CMD_CLIP_IMAGE_MASK, ctm, NULL, NULL, 0); + node = fz_new_display_node(dev->ctx, FZ_CMD_CLIP_IMAGE_MASK, ctm, NULL, NULL, 0); node->rect = fz_transform_rect(ctm, fz_unit_rect); if (rect != NULL) node->rect = fz_intersect_rect(node->rect, *rect); node->item.image = fz_keep_pixmap(image); - fz_append_display_node(user, node); + fz_append_display_node(dev->user, node); } static void -fz_list_begin_mask(void *user, fz_rect rect, int luminosity, fz_colorspace *colorspace, float *color) +fz_list_begin_mask(fz_device *dev, fz_rect rect, int luminosity, fz_colorspace *colorspace, float *color) { fz_display_node *node; - node = fz_new_display_node(FZ_CMD_BEGIN_MASK, fz_identity, colorspace, color, 0); + node = fz_new_display_node(dev->ctx, FZ_CMD_BEGIN_MASK, fz_identity, colorspace, color, 0); node->rect = rect; node->flag = luminosity; - fz_append_display_node(user, node); + fz_append_display_node(dev->user, node); } static void -fz_list_end_mask(void *user) +fz_list_end_mask(fz_device *dev) { fz_display_node *node; - node = fz_new_display_node(FZ_CMD_END_MASK, fz_identity, NULL, NULL, 0); - fz_append_display_node(user, node); + node = fz_new_display_node(dev->ctx, FZ_CMD_END_MASK, fz_identity, NULL, NULL, 0); + fz_append_display_node(dev->user, node); } static void -fz_list_begin_group(void *user, fz_rect rect, int isolated, int knockout, int blendmode, float alpha) +fz_list_begin_group(fz_device *dev, fz_rect rect, int isolated, int knockout, int blendmode, float alpha) { fz_display_node *node; - node = fz_new_display_node(FZ_CMD_BEGIN_GROUP, fz_identity, NULL, NULL, alpha); + node = fz_new_display_node(dev->ctx, FZ_CMD_BEGIN_GROUP, fz_identity, NULL, NULL, alpha); node->rect = rect; node->item.blendmode = blendmode; node->flag |= isolated ? ISOLATED : 0; node->flag |= knockout ? KNOCKOUT : 0; - fz_append_display_node(user, node); + fz_append_display_node(dev->user, node); } static void -fz_list_end_group(void *user) +fz_list_end_group(fz_device *dev) { fz_display_node *node; - node = fz_new_display_node(FZ_CMD_END_GROUP, fz_identity, NULL, NULL, 0); - fz_append_display_node(user, node); + node = fz_new_display_node(dev->ctx, FZ_CMD_END_GROUP, fz_identity, NULL, NULL, 0); + fz_append_display_node(dev->user, node); } static void -fz_list_begin_tile(void *user, fz_rect area, fz_rect view, float xstep, float ystep, fz_matrix ctm) +fz_list_begin_tile(fz_device *dev, fz_rect area, fz_rect view, float xstep, float ystep, fz_matrix ctm) { fz_display_node *node; - node = fz_new_display_node(FZ_CMD_BEGIN_TILE, ctm, NULL, NULL, 0); + node = fz_new_display_node(dev->ctx, FZ_CMD_BEGIN_TILE, ctm, NULL, NULL, 0); node->rect = area; node->color[0] = xstep; node->color[1] = ystep; @@ -434,21 +434,21 @@ fz_list_begin_tile(void *user, fz_rect area, fz_rect view, float xstep, float ys node->color[3] = view.y0; node->color[4] = view.x1; node->color[5] = view.y1; - fz_append_display_node(user, node); + fz_append_display_node(dev->user, node); } static void -fz_list_end_tile(void *user) +fz_list_end_tile(fz_device *dev) { fz_display_node *node; - node = fz_new_display_node(FZ_CMD_END_TILE, fz_identity, NULL, NULL, 0); - fz_append_display_node(user, node); + node = fz_new_display_node(dev->ctx, FZ_CMD_END_TILE, fz_identity, NULL, NULL, 0); + fz_append_display_node(dev->user, node); } fz_device * -fz_new_list_device(fz_display_list *list) +fz_new_list_device(fz_context *ctx, fz_display_list *list) { - fz_device *dev = fz_new_device(list); + fz_device *dev = fz_new_device(ctx, list); dev->fill_path = fz_list_fill_path; dev->stroke_path = fz_list_stroke_path; @@ -480,9 +480,9 @@ fz_new_list_device(fz_display_list *list) } fz_display_list * -fz_new_display_list(void) +fz_new_display_list(fz_context *ctx) { - fz_display_list *list = fz_malloc(sizeof(fz_display_list)); + fz_display_list *list = fz_malloc(ctx, sizeof(fz_display_list)); list->first = NULL; list->last = NULL; list->top = 0; @@ -491,16 +491,16 @@ fz_new_display_list(void) } void -fz_free_display_list(fz_display_list *list) +fz_free_display_list(fz_context *ctx, fz_display_list *list) { fz_display_node *node = list->first; while (node) { fz_display_node *next = node->next; - fz_free_display_node(node); + fz_free_display_node(ctx, node); node = next; } - fz_free(list); + fz_free(ctx, list); } void diff --git a/fitz/dev_null.c b/fitz/dev_null.c index 508be7e0..394f8374 100644 --- a/fitz/dev_null.c +++ b/fitz/dev_null.c @@ -1,13 +1,13 @@ #include "fitz.h" fz_device * -fz_new_device(void *user) +fz_new_device(fz_context *ctx, void *user) { - fz_device *dev = fz_malloc(sizeof(fz_device)); - memset(dev, 0, sizeof(fz_device)); + fz_device *dev = fz_calloc(ctx, 1, sizeof(fz_device)); dev->hints = 0; dev->flags = 0; dev->user = user; + dev->ctx = ctx; return dev; } @@ -15,8 +15,8 @@ void fz_free_device(fz_device *dev) { if (dev->free_user) - dev->free_user(dev->user); - fz_free(dev); + dev->free_user(dev); + fz_free(dev->ctx, dev); } void @@ -24,7 +24,7 @@ fz_fill_path(fz_device *dev, fz_path *path, int even_odd, fz_matrix ctm, fz_colorspace *colorspace, float *color, float alpha) { if (dev->fill_path) - dev->fill_path(dev->user, path, even_odd, ctm, colorspace, color, alpha); + dev->fill_path(dev, path, even_odd, ctm, colorspace, color, alpha); } void @@ -32,21 +32,21 @@ fz_stroke_path(fz_device *dev, fz_path *path, fz_stroke_state *stroke, fz_matrix fz_colorspace *colorspace, float *color, float alpha) { if (dev->stroke_path) - dev->stroke_path(dev->user, path, stroke, ctm, colorspace, color, alpha); + dev->stroke_path(dev, path, stroke, ctm, colorspace, color, alpha); } void fz_clip_path(fz_device *dev, fz_path *path, fz_rect *rect, int even_odd, fz_matrix ctm) { if (dev->clip_path) - dev->clip_path(dev->user, path, rect, even_odd, ctm); + dev->clip_path(dev, path, rect, even_odd, ctm); } void fz_clip_stroke_path(fz_device *dev, fz_path *path, fz_rect *rect, fz_stroke_state *stroke, fz_matrix ctm) { if (dev->clip_stroke_path) - dev->clip_stroke_path(dev->user, path, rect, stroke, ctm); + dev->clip_stroke_path(dev, path, rect, stroke, ctm); } void @@ -54,7 +54,7 @@ fz_fill_text(fz_device *dev, fz_text *text, fz_matrix ctm, fz_colorspace *colorspace, float *color, float alpha) { if (dev->fill_text) - dev->fill_text(dev->user, text, ctm, colorspace, color, alpha); + dev->fill_text(dev, text, ctm, colorspace, color, alpha); } void @@ -62,49 +62,49 @@ fz_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, fz_matrix fz_colorspace *colorspace, float *color, float alpha) { if (dev->stroke_text) - dev->stroke_text(dev->user, text, stroke, ctm, colorspace, color, alpha); + dev->stroke_text(dev, text, stroke, ctm, colorspace, color, alpha); } void fz_clip_text(fz_device *dev, fz_text *text, fz_matrix ctm, int accumulate) { if (dev->clip_text) - dev->clip_text(dev->user, text, ctm, accumulate); + dev->clip_text(dev, text, ctm, accumulate); } void fz_clip_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm) { if (dev->clip_stroke_text) - dev->clip_stroke_text(dev->user, text, stroke, ctm); + dev->clip_stroke_text(dev, text, stroke, ctm); } void fz_ignore_text(fz_device *dev, fz_text *text, fz_matrix ctm) { if (dev->ignore_text) - dev->ignore_text(dev->user, text, ctm); + dev->ignore_text(dev, text, ctm); } void fz_pop_clip(fz_device *dev) { if (dev->pop_clip) - dev->pop_clip(dev->user); + dev->pop_clip(dev); } void fz_fill_shade(fz_device *dev, fz_shade *shade, fz_matrix ctm, float alpha) { if (dev->fill_shade) - dev->fill_shade(dev->user, shade, ctm, alpha); + dev->fill_shade(dev, shade, ctm, alpha); } void fz_fill_image(fz_device *dev, fz_pixmap *image, fz_matrix ctm, float alpha) { if (dev->fill_image) - dev->fill_image(dev->user, image, ctm, alpha); + dev->fill_image(dev, image, ctm, alpha); } void @@ -112,54 +112,54 @@ fz_fill_image_mask(fz_device *dev, fz_pixmap *image, fz_matrix ctm, fz_colorspace *colorspace, float *color, float alpha) { if (dev->fill_image_mask) - dev->fill_image_mask(dev->user, image, ctm, colorspace, color, alpha); + dev->fill_image_mask(dev, image, ctm, colorspace, color, alpha); } void fz_clip_image_mask(fz_device *dev, fz_pixmap *image, fz_rect *rect, fz_matrix ctm) { if (dev->clip_image_mask) - dev->clip_image_mask(dev->user, image, rect, ctm); + dev->clip_image_mask(dev, image, rect, ctm); } void fz_begin_mask(fz_device *dev, fz_rect area, int luminosity, fz_colorspace *colorspace, float *bc) { if (dev->begin_mask) - dev->begin_mask(dev->user, area, luminosity, colorspace, bc); + dev->begin_mask(dev, area, luminosity, colorspace, bc); } void fz_end_mask(fz_device *dev) { if (dev->end_mask) - dev->end_mask(dev->user); + dev->end_mask(dev); } void fz_begin_group(fz_device *dev, fz_rect area, int isolated, int knockout, int blendmode, float alpha) { if (dev->begin_group) - dev->begin_group(dev->user, area, isolated, knockout, blendmode, alpha); + dev->begin_group(dev, area, isolated, knockout, blendmode, alpha); } void fz_end_group(fz_device *dev) { if (dev->end_group) - dev->end_group(dev->user); + dev->end_group(dev); } void fz_begin_tile(fz_device *dev, fz_rect area, fz_rect view, float xstep, float ystep, fz_matrix ctm) { if (dev->begin_tile) - dev->begin_tile(dev->user, area, view, xstep, ystep, ctm); + dev->begin_tile(dev, area, view, xstep, ystep, ctm); } void fz_end_tile(fz_device *dev) { if (dev->end_tile) - dev->end_tile(dev->user); + dev->end_tile(dev); } diff --git a/fitz/dev_text.c b/fitz/dev_text.c index 139c3eed..cebfbceb 100644 --- a/fitz/dev_text.c +++ b/fitz/dev_text.c @@ -17,10 +17,10 @@ struct fz_text_device_s }; fz_text_span * -fz_new_text_span(void) +fz_new_text_span(fz_context *ctx) { fz_text_span *span; - span = fz_malloc(sizeof(fz_text_span)); + span = fz_malloc(ctx, sizeof(fz_text_span)); span->font = NULL; span->wmode = 0; span->size = 0; @@ -33,23 +33,29 @@ fz_new_text_span(void) } void -fz_free_text_span(fz_text_span *span) +fz_free_text_span(fz_context *ctx, fz_text_span *span) { - if (span->font) - fz_drop_font(span->font); - if (span->next) - fz_free_text_span(span->next); - fz_free(span->text); - fz_free(span); + fz_text_span *next; + + do + { + next = span->next; + if (span->font) + fz_drop_font(ctx, span->font); + fz_free(ctx, span->text); + fz_free(ctx, span); + span = next; + } + while (span != NULL); } static void -fz_add_text_char_imp(fz_text_span *span, int c, fz_bbox bbox) +fz_add_text_char_imp(fz_context *ctx, fz_text_span *span, int c, fz_bbox bbox) { if (span->len + 1 >= span->cap) { span->cap = span->cap > 1 ? (span->cap * 3) / 2 : 80; - span->text = fz_realloc(span->text, span->cap, sizeof(fz_text_char)); + span->text = fz_realloc(ctx, span->text, span->cap * sizeof(fz_text_char)); } span->text[span->len].c = c; span->text[span->len].bbox = bbox; @@ -67,7 +73,7 @@ fz_split_bbox(fz_bbox bbox, int i, int n) } static void -fz_add_text_char(fz_text_span **last, fz_font *font, float size, int wmode, int c, fz_bbox bbox) +fz_add_text_char(fz_context *ctx, fz_text_span **last, fz_font *font, float size, int wmode, int c, fz_bbox bbox) { fz_text_span *span = *last; @@ -79,7 +85,7 @@ fz_add_text_char(fz_text_span **last, fz_font *font, float size, int wmode, int if ((span->font != font || span->size != size || span->wmode != wmode) && c != 32) { - span = fz_new_text_span(); + span = fz_new_text_span(ctx); span->font = fz_keep_font(font); span->size = size; span->wmode = wmode; @@ -92,34 +98,34 @@ fz_add_text_char(fz_text_span **last, fz_font *font, float size, int wmode, int case -1: /* ignore when one unicode character maps to multiple glyphs */ break; case 0xFB00: /* ff */ - fz_add_text_char_imp(span, 'f', fz_split_bbox(bbox, 0, 2)); - fz_add_text_char_imp(span, 'f', fz_split_bbox(bbox, 1, 2)); + fz_add_text_char_imp(ctx, span, 'f', fz_split_bbox(bbox, 0, 2)); + fz_add_text_char_imp(ctx, span, 'f', fz_split_bbox(bbox, 1, 2)); break; case 0xFB01: /* fi */ - fz_add_text_char_imp(span, 'f', fz_split_bbox(bbox, 0, 2)); - fz_add_text_char_imp(span, 'i', fz_split_bbox(bbox, 1, 2)); + fz_add_text_char_imp(ctx, span, 'f', fz_split_bbox(bbox, 0, 2)); + fz_add_text_char_imp(ctx, span, 'i', fz_split_bbox(bbox, 1, 2)); break; case 0xFB02: /* fl */ - fz_add_text_char_imp(span, 'f', fz_split_bbox(bbox, 0, 2)); - fz_add_text_char_imp(span, 'l', fz_split_bbox(bbox, 1, 2)); + fz_add_text_char_imp(ctx, span, 'f', fz_split_bbox(bbox, 0, 2)); + fz_add_text_char_imp(ctx, span, 'l', fz_split_bbox(bbox, 1, 2)); break; case 0xFB03: /* ffi */ - fz_add_text_char_imp(span, 'f', fz_split_bbox(bbox, 0, 3)); - fz_add_text_char_imp(span, 'f', fz_split_bbox(bbox, 1, 3)); - fz_add_text_char_imp(span, 'i', fz_split_bbox(bbox, 2, 3)); + fz_add_text_char_imp(ctx, span, 'f', fz_split_bbox(bbox, 0, 3)); + fz_add_text_char_imp(ctx, span, 'f', fz_split_bbox(bbox, 1, 3)); + fz_add_text_char_imp(ctx, span, 'i', fz_split_bbox(bbox, 2, 3)); break; case 0xFB04: /* ffl */ - fz_add_text_char_imp(span, 'f', fz_split_bbox(bbox, 0, 3)); - fz_add_text_char_imp(span, 'f', fz_split_bbox(bbox, 1, 3)); - fz_add_text_char_imp(span, 'l', fz_split_bbox(bbox, 2, 3)); + fz_add_text_char_imp(ctx, span, 'f', fz_split_bbox(bbox, 0, 3)); + fz_add_text_char_imp(ctx, span, 'f', fz_split_bbox(bbox, 1, 3)); + fz_add_text_char_imp(ctx, span, 'l', fz_split_bbox(bbox, 2, 3)); break; case 0xFB05: /* long st */ case 0xFB06: /* st */ - fz_add_text_char_imp(span, 's', fz_split_bbox(bbox, 0, 2)); - fz_add_text_char_imp(span, 't', fz_split_bbox(bbox, 1, 2)); + fz_add_text_char_imp(ctx, span, 's', fz_split_bbox(bbox, 0, 2)); + fz_add_text_char_imp(ctx, span, 't', fz_split_bbox(bbox, 1, 2)); break; default: - fz_add_text_char_imp(span, c, bbox); + fz_add_text_char_imp(ctx, span, c, bbox); break; } } @@ -136,10 +142,10 @@ fz_divide_text_chars(fz_text_span **last, int n, fz_bbox bbox) } static void -fz_add_text_newline(fz_text_span **last, fz_font *font, float size, int wmode) +fz_add_text_newline(fz_context *ctx, fz_text_span **last, fz_font *font, float size, int wmode) { fz_text_span *span; - span = fz_new_text_span(); + span = fz_new_text_span(ctx); span->font = fz_keep_font(font); span->size = size; span->wmode = wmode; @@ -209,7 +215,7 @@ fz_debug_text_span(fz_text_span *span) } static void -fz_text_extract_span(fz_text_span **last, fz_text *text, fz_matrix ctm, fz_point *pen) +fz_text_extract_span(fz_context *ctx, fz_text_span **last, fz_text *text, fz_matrix ctm, fz_point *pen) { fz_font *font = text->font; FT_Face face = font->ft_face; @@ -267,7 +273,7 @@ fz_text_extract_span(fz_text_span **last, fz_text *text, fz_matrix ctm, fz_point { if (text->items[i].gid < 0) { - fz_add_text_char(last, font, size, text->wmode, text->items[i].ucs, fz_round_rect(rect)); + fz_add_text_char(ctx, last, font, size, text->wmode, text->items[i].ucs, fz_round_rect(rect)); multi ++; fz_divide_text_chars(last, multi, fz_round_rect(rect)); continue; @@ -295,7 +301,7 @@ fz_text_extract_span(fz_text_span **last, fz_text *text, fz_matrix ctm, fz_point if (dist > size * LINE_DIST) { - fz_add_text_newline(last, font, size, text->wmode); + fz_add_text_newline(ctx, last, font, size, text->wmode); } else if (fabsf(dot) > 0.95f && dist > size * SPACE_DIST) { @@ -307,7 +313,7 @@ fz_text_extract_span(fz_text_span **last, fz_text *text, fz_matrix ctm, fz_point spacerect.x1 = 0; spacerect.y1 = 1; spacerect = fz_transform_rect(trm, spacerect); - fz_add_text_char(last, font, size, text->wmode, ' ', fz_round_rect(spacerect)); + fz_add_text_char(ctx, last, font, size, text->wmode, ' ', fz_round_rect(spacerect)); } } } @@ -342,71 +348,71 @@ fz_text_extract_span(fz_text_span **last, fz_text *text, fz_matrix ctm, fz_point pen->x = trm.e + dir.x * adv; pen->y = trm.f + dir.y * adv; - fz_add_text_char(last, font, size, text->wmode, text->items[i].ucs, fz_round_rect(rect)); + fz_add_text_char(ctx, last, font, size, text->wmode, text->items[i].ucs, fz_round_rect(rect)); } } static void -fz_text_fill_text(void *user, fz_text *text, fz_matrix ctm, +fz_text_fill_text(fz_device *dev, fz_text *text, fz_matrix ctm, fz_colorspace *colorspace, float *color, float alpha) { - fz_text_device *tdev = user; - fz_text_extract_span(&tdev->span, text, ctm, &tdev->point); + fz_text_device *tdev = dev->user; + fz_text_extract_span(dev->ctx, &tdev->span, text, ctm, &tdev->point); } static void -fz_text_stroke_text(void *user, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm, +fz_text_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm, fz_colorspace *colorspace, float *color, float alpha) { - fz_text_device *tdev = user; - fz_text_extract_span(&tdev->span, text, ctm, &tdev->point); + fz_text_device *tdev = dev->user; + fz_text_extract_span(dev->ctx, &tdev->span, text, ctm, &tdev->point); } static void -fz_text_clip_text(void *user, fz_text *text, fz_matrix ctm, int accumulate) +fz_text_clip_text(fz_device *dev, fz_text *text, fz_matrix ctm, int accumulate) { - fz_text_device *tdev = user; - fz_text_extract_span(&tdev->span, text, ctm, &tdev->point); + fz_text_device *tdev = dev->user; + fz_text_extract_span(dev->ctx, &tdev->span, text, ctm, &tdev->point); } static void -fz_text_clip_stroke_text(void *user, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm) +fz_text_clip_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm) { - fz_text_device *tdev = user; - fz_text_extract_span(&tdev->span, text, ctm, &tdev->point); + fz_text_device *tdev = dev->user; + fz_text_extract_span(dev->ctx, &tdev->span, text, ctm, &tdev->point); } static void -fz_text_ignore_text(void *user, fz_text *text, fz_matrix ctm) +fz_text_ignore_text(fz_device *dev, fz_text *text, fz_matrix ctm) { - fz_text_device *tdev = user; - fz_text_extract_span(&tdev->span, text, ctm, &tdev->point); + fz_text_device *tdev = dev->user; + fz_text_extract_span(dev->ctx, &tdev->span, text, ctm, &tdev->point); } static void -fz_text_free_user(void *user) +fz_text_free_user(fz_device *dev) { - fz_text_device *tdev = user; + fz_text_device *tdev = dev->user; tdev->span->eol = 1; /* TODO: unicode NFC normalization */ /* TODO: bidi logical reordering */ - fz_free(tdev); + fz_free(dev->ctx, tdev); } fz_device * -fz_new_text_device(fz_text_span *root) +fz_new_text_device(fz_context *ctx, fz_text_span *root) { fz_device *dev; - fz_text_device *tdev = fz_malloc(sizeof(fz_text_device)); + fz_text_device *tdev = fz_malloc(ctx, sizeof(fz_text_device)); tdev->head = root; tdev->span = root; tdev->point.x = -1; tdev->point.y = -1; - dev = fz_new_device(tdev); + dev = fz_new_device(ctx, tdev); dev->hints = FZ_IGNORE_IMAGE | FZ_IGNORE_SHADE; dev->free_user = fz_text_free_user; dev->fill_text = fz_text_fill_text; diff --git a/fitz/dev_trace.c b/fitz/dev_trace.c index 03e05f91..4db215a0 100644 --- a/fitz/dev_trace.c +++ b/fitz/dev_trace.c @@ -60,7 +60,7 @@ fz_trace_path(fz_path *path, int indent) } static void -fz_trace_fill_path(void *user, fz_path *path, int even_odd, fz_matrix ctm, +fz_trace_fill_path(fz_device *dev, fz_path *path, int even_odd, fz_matrix ctm, fz_colorspace *colorspace, float *color, float alpha) { printf("<fill_path "); @@ -76,7 +76,7 @@ fz_trace_fill_path(void *user, fz_path *path, int even_odd, fz_matrix ctm, } static void -fz_trace_stroke_path(void *user, fz_path *path, fz_stroke_state *stroke, fz_matrix ctm, +fz_trace_stroke_path(fz_device *dev, fz_path *path, fz_stroke_state *stroke, fz_matrix ctm, fz_colorspace *colorspace, float *color, float alpha) { int i; @@ -105,7 +105,7 @@ fz_trace_stroke_path(void *user, fz_path *path, fz_stroke_state *stroke, fz_matr } static void -fz_trace_clip_path(void *user, fz_path *path, fz_rect *rect, int even_odd, fz_matrix ctm) +fz_trace_clip_path(fz_device *dev, fz_path *path, fz_rect *rect, int even_odd, fz_matrix ctm) { printf("<clip_path "); if (even_odd) @@ -120,7 +120,7 @@ fz_trace_clip_path(void *user, fz_path *path, fz_rect *rect, int even_odd, fz_ma } static void -fz_trace_clip_stroke_path(void *user, fz_path *path, fz_rect *rect, fz_stroke_state *stroke, fz_matrix ctm) +fz_trace_clip_stroke_path(fz_device *dev, fz_path *path, fz_rect *rect, fz_stroke_state *stroke, fz_matrix ctm) { printf("<clip_stroke_path "); fz_trace_matrix(ctm); @@ -130,7 +130,7 @@ fz_trace_clip_stroke_path(void *user, fz_path *path, fz_rect *rect, fz_stroke_st } static void -fz_trace_fill_text(void *user, fz_text *text, fz_matrix ctm, +fz_trace_fill_text(fz_device *dev, fz_text *text, fz_matrix ctm, fz_colorspace *colorspace, float *color, float alpha) { printf("<fill_text font=\"%s\" wmode=\"%d\" ", text->font->name, text->wmode); @@ -142,7 +142,7 @@ fz_trace_fill_text(void *user, fz_text *text, fz_matrix ctm, } static void -fz_trace_stroke_text(void *user, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm, +fz_trace_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm, fz_colorspace *colorspace, float *color, float alpha) { printf("<stroke_text font=\"%s\" wmode=\"%d\" ", text->font->name, text->wmode); @@ -154,7 +154,7 @@ fz_trace_stroke_text(void *user, fz_text *text, fz_stroke_state *stroke, fz_matr } static void -fz_trace_clip_text(void *user, fz_text *text, fz_matrix ctm, int accumulate) +fz_trace_clip_text(fz_device *dev, fz_text *text, fz_matrix ctm, int accumulate) { printf("<clip_text font=\"%s\" wmode=\"%d\" ", text->font->name, text->wmode); printf("accumulate=\"%d\" ", accumulate); @@ -165,7 +165,7 @@ fz_trace_clip_text(void *user, fz_text *text, fz_matrix ctm, int accumulate) } static void -fz_trace_clip_stroke_text(void *user, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm) +fz_trace_clip_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, fz_matrix ctm) { printf("<clip_stroke_text font=\"%s\" wmode=\"%d\" ", text->font->name, text->wmode); fz_trace_matrix(fz_concat(ctm, text->trm)); @@ -175,7 +175,7 @@ fz_trace_clip_stroke_text(void *user, fz_text *text, fz_stroke_state *stroke, fz } static void -fz_trace_ignore_text(void *user, fz_text *text, fz_matrix ctm) +fz_trace_ignore_text(fz_device *dev, fz_text *text, fz_matrix ctm) { printf("<ignore_text font=\"%s\" wmode=\"%d\" ", text->font->name, text->wmode); fz_trace_matrix(fz_concat(ctm, text->trm)); @@ -185,7 +185,7 @@ fz_trace_ignore_text(void *user, fz_text *text, fz_matrix ctm) } static void -fz_trace_fill_image(void *user, fz_pixmap *image, fz_matrix ctm, float alpha) +fz_trace_fill_image(fz_device *dev, fz_pixmap *image, fz_matrix ctm, float alpha) { printf("<fill_image alpha=\"%g\" ", alpha); fz_trace_matrix(ctm); @@ -193,7 +193,7 @@ fz_trace_fill_image(void *user, fz_pixmap *image, fz_matrix ctm, float alpha) } static void -fz_trace_fill_shade(void *user, fz_shade *shade, fz_matrix ctm, float alpha) +fz_trace_fill_shade(fz_device *dev, fz_shade *shade, fz_matrix ctm, float alpha) { printf("<fill_shade alpha=\"%g\" ", alpha); fz_trace_matrix(ctm); @@ -201,7 +201,7 @@ fz_trace_fill_shade(void *user, fz_shade *shade, fz_matrix ctm, float alpha) } static void -fz_trace_fill_image_mask(void *user, fz_pixmap *image, fz_matrix ctm, +fz_trace_fill_image_mask(fz_device *dev, fz_pixmap *image, fz_matrix ctm, fz_colorspace *colorspace, float *color, float alpha) { printf("<fill_image_mask "); @@ -211,7 +211,7 @@ fz_colorspace *colorspace, float *color, float alpha) } static void -fz_trace_clip_image_mask(void *user, fz_pixmap *image, fz_rect *rect, fz_matrix ctm) +fz_trace_clip_image_mask(fz_device *dev, fz_pixmap *image, fz_rect *rect, fz_matrix ctm) { printf("<clip_image_mask "); fz_trace_matrix(ctm); @@ -219,13 +219,13 @@ fz_trace_clip_image_mask(void *user, fz_pixmap *image, fz_rect *rect, fz_matrix } static void -fz_trace_pop_clip(void *user) +fz_trace_pop_clip(fz_device *dev) { printf("<pop_clip />\n"); } static void -fz_trace_begin_mask(void *user, fz_rect bbox, int luminosity, fz_colorspace *colorspace, float *color) +fz_trace_begin_mask(fz_device *dev, fz_rect bbox, int luminosity, fz_colorspace *colorspace, float *color) { printf("<mask bbox=\"%g %g %g %g\" s=\"%s\" ", bbox.x0, bbox.y0, bbox.x1, bbox.y1, @@ -235,13 +235,13 @@ fz_trace_begin_mask(void *user, fz_rect bbox, int luminosity, fz_colorspace *col } static void -fz_trace_end_mask(void *user) +fz_trace_end_mask(fz_device *dev) { printf("</mask>\n"); } static void -fz_trace_begin_group(void *user, fz_rect bbox, int isolated, int knockout, int blendmode, float alpha) +fz_trace_begin_group(fz_device *dev, fz_rect bbox, int isolated, int knockout, int blendmode, float alpha) { printf("<group bbox=\"%g %g %g %g\" isolated=\"%d\" knockout=\"%d\" blendmode=\"%s\" alpha=\"%g\">\n", bbox.x0, bbox.y0, bbox.x1, bbox.y1, @@ -249,13 +249,13 @@ fz_trace_begin_group(void *user, fz_rect bbox, int isolated, int knockout, int b } static void -fz_trace_end_group(void *user) +fz_trace_end_group(fz_device *dev) { printf("</group>\n"); } static void -fz_trace_begin_tile(void *user, fz_rect area, fz_rect view, float xstep, float ystep, fz_matrix ctm) +fz_trace_begin_tile(fz_device *dev, fz_rect area, fz_rect view, float xstep, float ystep, fz_matrix ctm) { printf("<tile "); printf("area=\"%g %g %g %g\" ", area.x0, area.y0, area.x1, area.y1); @@ -266,14 +266,14 @@ fz_trace_begin_tile(void *user, fz_rect area, fz_rect view, float xstep, float y } static void -fz_trace_end_tile(void *user) +fz_trace_end_tile(fz_device *dev) { printf("</tile>\n"); } -fz_device *fz_new_trace_device(void) +fz_device *fz_new_trace_device(fz_context *ctx) { - fz_device *dev = fz_new_device(NULL); + fz_device *dev = fz_new_device(ctx, NULL); dev->fill_path = fz_trace_fill_path; dev->stroke_path = fz_trace_stroke_path; diff --git a/fitz/except.c b/fitz/except.c index 348b950e..4055367d 100644 --- a/fitz/except.c +++ b/fitz/except.c @@ -1,3 +1,4 @@ +#include <assert.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -6,44 +7,76 @@ static void do_throw(fz_except_context *ex) { - if(ex->depth >= 0) - longjmp(ex->stack[ex->depth].buffer, 1); - else { - printf("Uncaught exception: %s\n", ex->except.mess); - exit(EXIT_FAILURE); /* Bale through normal channels */ - } + assert(ex != NULL); + if(ex->depth >= 0) + longjmp(ex->stack[ex->depth].buffer, 1); + else { + printf("Uncaught exception: %s\n", ex->except.mess); + exit(EXIT_FAILURE); /* Bale through normal channels */ + } } void fz_throw(fz_context *ctx, char *fmt, ...) { - va_list args; + va_list args; - va_start(args, fmt); - vsprintf(ctx->except->except.mess, fmt, args); - va_end(args); - do_throw(ctx->except); + va_start(args, fmt); + vsprintf(ctx->except->except.mess, fmt, args); + va_end(args); + do_throw(ctx->except); } fz_except *fz_caught(fz_context *ctx) { - return &ctx->except->except; + assert(ctx != NULL); + assert(ctx->except != NULL); + return &ctx->except->except; } void fz_rethrow(fz_context *ctx) { - do_throw(ctx->except); + assert(ctx != NULL); + do_throw(ctx->except); } void fz_except_xxx_push(fz_except_context *ex) { - if(ex->depth+1 >= MAXDEPTH) { - fprintf(stderr, "Exception stack overflow!\n"); - exit(EXIT_FAILURE); /* Bale through normal channels */ - } - ex->depth++; + assert(ex != NULL); + if(ex->depth+1 >= MAXDEPTH) { + fprintf(stderr, "Exception stack overflow!\n"); + exit(EXIT_FAILURE); /* Bale through normal channels */ + } + ex->depth++; } void fz_var_xxx(void *e) { - /* Do nothing */ + /* Do nothing */ +} + +fz_error fz_except_init(fz_context *ctx) +{ + fz_except_context *ex; + + assert(ctx != NULL); + if (ctx->except != NULL) + return fz_okay; + ex = fz_malloc_nothrow(ctx, sizeof(fz_except_context)); + if (ex == NULL) + return fz_error_make("Failed to initialise exception context"); + ex->depth = -1; + ctx->except = ex; + + return fz_okay; +} + +void fz_except_fin(fz_context *ctx) +{ + assert(ctx != NULL); + + if (ctx->except == NULL) + return; + assert(ctx->except->depth == -1); + free(ctx->except); + ctx->except = NULL; } diff --git a/fitz/except.h b/fitz/except.h index a964b94e..477fbc39 100644 --- a/fitz/except.h +++ b/fitz/except.h @@ -3,6 +3,9 @@ #include "fitz.h" +typedef struct fz_context fz_context; +typedef int fz_error; + typedef struct fz_except { char mess[256]; } fz_except; @@ -41,6 +44,8 @@ void fz_rethrow(fz_context *); void fz_var_xxx(void *x); -void fz_except_init(fz_context *); +fz_error fz_except_init(fz_context *); + +void fz_except_fin(fz_context *); #endif /* EXCEPT */ diff --git a/fitz/exceptxxx.h b/fitz/exceptxxx.h index 185f73f6..285e88b7 100644 --- a/fitz/exceptxxx.h +++ b/fitz/exceptxxx.h @@ -5,32 +5,32 @@ #define fz_try(ctx) \ if(fz_except_xxx_push(ctx->except), \ - !setjmp(ctx->except.stack[ctx->except.depth].buffer)) \ + !setjmp(ctx->except->stack[ctx->except->depth].buffer)) \ { #define fz_catch(ctx) \ - ctx->except.stack[ctx->except.depth].failed = 0;\ + ctx->except->stack[ctx->except->depth].failed = 0;\ }\ else\ {\ - ctx->except.stack[ctx->except.depth].failed = 1;\ + ctx->except->stack[ctx->except->depth].failed = 1;\ }\ -if(ex->stack[ex->depth--].failed) +if(ctx->except->stack[ctx->except->depth--].failed) typedef struct fz_except_xxx_stack { - int failed; - jmp_buf buffer; + int failed; + jmp_buf buffer; } fz_except_xxx_stack; #define MAXDEPTH (20) struct fz_except_context { - fz_except_xxx_stack stack[MAXDEPTH]; - int depth; - fz_except except; + fz_except_xxx_stack stack[MAXDEPTH]; + int depth; + fz_except except; }; -void fz_except_xxx_push(fz_except_context *); +void fz_except_xxx_push(struct fz_except_context *); #endif /* EXCEPTXXX */ diff --git a/fitz/filt_basic.c b/fitz/filt_basic.c index 15c42137..32fce6f2 100644 --- a/fitz/filt_basic.c +++ b/fitz/filt_basic.c @@ -32,8 +32,10 @@ static void close_null(fz_stream *stm) { struct null_filter *state = stm->state; - fz_close(state->chain); - fz_free(state); + fz_stream *chain = state->chain; + + fz_free(stm->ctx, state); + fz_close(chain); } fz_stream * @@ -41,11 +43,12 @@ fz_open_null(fz_stream *chain, int len) { struct null_filter *state; - state = fz_malloc(sizeof(struct null_filter)); + assert(chain != NULL); + state = fz_malloc(chain->ctx, sizeof(struct null_filter)); state->chain = chain; state->remain = len; - return fz_new_stream(state, read_null, close_null); + return fz_new_stream(chain->ctx, state, read_null, close_null); } /* ASCII Hex Decode */ @@ -135,8 +138,10 @@ static void close_ahxd(fz_stream *stm) { fz_ahxd *state = stm->state; - fz_close(state->chain); - fz_free(state); + fz_stream *chain = state->chain; + + fz_free(stm->ctx, state); + fz_close(chain); } fz_stream * @@ -144,11 +149,11 @@ fz_open_ahxd(fz_stream *chain) { fz_ahxd *state; - state = fz_malloc(sizeof(fz_ahxd)); + state = fz_malloc(chain->ctx, sizeof(fz_ahxd)); state->chain = chain; state->eod = 0; - return fz_new_stream(state, read_ahxd, close_ahxd); + return fz_new_stream(chain->ctx, state, read_ahxd, close_ahxd); } /* ASCII 85 Decode */ @@ -270,8 +275,10 @@ static void close_a85d(fz_stream *stm) { fz_a85d *state = stm->state; - fz_close(state->chain); - fz_free(state); + fz_stream *chain = state->chain; + + fz_free(stm->ctx, state); + fz_close(chain); } fz_stream * @@ -279,13 +286,14 @@ fz_open_a85d(fz_stream *chain) { fz_a85d *state; - state = fz_malloc(sizeof(fz_a85d)); + assert(chain != NULL); + state = fz_malloc(chain->ctx, sizeof(fz_a85d)); state->chain = chain; state->rp = state->bp; state->wp = state->bp; state->eod = 0; - return fz_new_stream(state, read_a85d, close_a85d); + return fz_new_stream(chain->ctx, state, read_a85d, close_a85d); } /* Run Length Decode */ @@ -355,8 +363,10 @@ static void close_rld(fz_stream *stm) { fz_rld *state = stm->state; - fz_close(state->chain); - fz_free(state); + fz_stream *chain = state->chain; + + fz_free(stm->ctx, state); + fz_close(chain); } fz_stream * @@ -364,13 +374,14 @@ fz_open_rld(fz_stream *chain) { fz_rld *state; - state = fz_malloc(sizeof(fz_rld)); + assert(chain != NULL); + state = fz_malloc(chain->ctx, sizeof(fz_rld)); state->chain = chain; state->run = 0; state->n = 0; state->c = 0; - return fz_new_stream(state, read_rld, close_rld); + return fz_new_stream(chain->ctx, state, read_rld, close_rld); } /* RC4 Filter */ @@ -402,8 +413,10 @@ static void close_arc4(fz_stream *stm) { fz_arc4c *state = stm->state; - fz_close(state->chain); - fz_free(state); + fz_stream *chain = state->chain; + + fz_free(stm->ctx, state); + fz_close(chain); } fz_stream * @@ -411,11 +424,11 @@ fz_open_arc4(fz_stream *chain, unsigned char *key, unsigned keylen) { fz_arc4c *state; - state = fz_malloc(sizeof(fz_arc4c)); + state = fz_malloc(chain->ctx, sizeof(fz_arc4c)); state->chain = chain; fz_arc4_init(&state->arc4, key, keylen); - return fz_new_stream(state, read_arc4, close_arc4); + return fz_new_stream(chain->ctx, state, read_arc4, close_arc4); } /* AES Filter */ @@ -484,8 +497,10 @@ static void close_aesd(fz_stream *stm) { fz_aesd *state = stm->state; - fz_close(state->chain); - fz_free(state); + fz_stream *chain = state->chain; + + fz_free(stm->ctx, state); + fz_close(chain); } fz_stream * @@ -493,12 +508,13 @@ fz_open_aesd(fz_stream *chain, unsigned char *key, unsigned keylen) { fz_aesd *state; - state = fz_malloc(sizeof(fz_aesd)); + assert(chain != NULL); + state = fz_malloc(chain->ctx, sizeof(fz_aesd)); state->chain = chain; aes_setkey_dec(&state->aes, key, keylen * 8); state->ivcount = 0; state->rp = state->bp; state->wp = state->bp; - return fz_new_stream(state, read_aesd, close_aesd); + return fz_new_stream(chain->ctx, state, read_aesd, close_aesd); } diff --git a/fitz/filt_dctd.c b/fitz/filt_dctd.c index 372efe9e..94a06afb 100644 --- a/fitz/filt_dctd.c +++ b/fitz/filt_dctd.c @@ -8,6 +8,7 @@ typedef struct fz_dctd_s fz_dctd; struct fz_dctd_s { fz_stream *chain; + fz_context *ctx; int color_transform; int init; int stride; @@ -144,7 +145,7 @@ read_dctd(fz_stream *stm, unsigned char *buf, int len) jpeg_start_decompress(cinfo); state->stride = cinfo->output_width * cinfo->output_components; - state->scanline = fz_malloc(state->stride); + state->scanline = fz_malloc(state->ctx, state->stride); state->rp = state->scanline; state->wp = state->scanline; @@ -196,9 +197,9 @@ close_dctd(fz_stream *stm) skip: state->chain->rp = state->chain->wp - state->cinfo.src->bytes_in_buffer; jpeg_destroy_decompress(&state->cinfo); - fz_free(state->scanline); + fz_free(stm->ctx, state->scanline); fz_close(state->chain); - fz_free(state); + fz_free(stm->ctx, state); } fz_stream * @@ -207,15 +208,16 @@ fz_open_dctd(fz_stream *chain, fz_obj *params) fz_dctd *state; fz_obj *obj; - state = fz_malloc(sizeof(fz_dctd)); - memset(state, 0, sizeof(fz_dctd)); + assert(chain); + state = fz_calloc(chain->ctx, 1, sizeof(fz_dctd)); + state->ctx = chain->ctx; state->chain = chain; state->color_transform = -1; /* unset */ state->init = 0; - obj = fz_dict_gets(params, "ColorTransform"); + obj = fz_dict_gets(chain->ctx, params, "ColorTransform"); if (obj) - state->color_transform = fz_to_int(obj); + state->color_transform = fz_to_int(chain->ctx, obj); - return fz_new_stream(state, read_dctd, close_dctd); + return fz_new_stream(chain->ctx, state, read_dctd, close_dctd); } diff --git a/fitz/filt_faxd.c b/fitz/filt_faxd.c index d36c8c76..27e8ed60 100644 --- a/fitz/filt_faxd.c +++ b/fitz/filt_faxd.c @@ -662,9 +662,9 @@ close_faxd(fz_stream *stm) fz_unread_byte(fax->chain); fz_close(fax->chain); - fz_free(fax->ref); - fz_free(fax->dst); - fz_free(fax); + fz_free(stm->ctx, fax->ref); + fz_free(stm->ctx, fax->dst); + fz_free(stm->ctx, fax); } fz_stream * @@ -672,9 +672,12 @@ fz_open_faxd(fz_stream *chain, fz_obj *params) { fz_faxd *fax; fz_obj *obj; + fz_context *ctx; - fax = fz_malloc(sizeof(fz_faxd)); - fax->chain = chain; + assert(chain != NULL); + ctx = chain->ctx; + fax = fz_malloc(ctx, sizeof(fz_faxd)); + fax->chain = chain; fax->ref = NULL; fax->dst = NULL; @@ -687,26 +690,26 @@ fz_open_faxd(fz_stream *chain, fz_obj *params) fax->end_of_block = 1; fax->black_is_1 = 0; - obj = fz_dict_gets(params, "K"); - if (obj) fax->k = fz_to_int(obj); + obj = fz_dict_gets(ctx, params, "K"); + if (obj) fax->k = fz_to_int(ctx, obj); - obj = fz_dict_gets(params, "EndOfLine"); - if (obj) fax->end_of_line = fz_to_bool(obj); + obj = fz_dict_gets(ctx, params, "EndOfLine"); + if (obj) fax->end_of_line = fz_to_bool(ctx, obj); - obj = fz_dict_gets(params, "EncodedByteAlign"); - if (obj) fax->encoded_byte_align = fz_to_bool(obj); + obj = fz_dict_gets(ctx, params, "EncodedByteAlign"); + if (obj) fax->encoded_byte_align = fz_to_bool(ctx, obj); - obj = fz_dict_gets(params, "Columns"); - if (obj) fax->columns = fz_to_int(obj); + obj = fz_dict_gets(ctx, params, "Columns"); + if (obj) fax->columns = fz_to_int(ctx, obj); - obj = fz_dict_gets(params, "Rows"); - if (obj) fax->rows = fz_to_int(obj); + obj = fz_dict_gets(ctx, params, "Rows"); + if (obj) fax->rows = fz_to_int(ctx, obj); - obj = fz_dict_gets(params, "EndOfBlock"); - if (obj) fax->end_of_block = fz_to_bool(obj); + obj = fz_dict_gets(ctx, params, "EndOfBlock"); + if (obj) fax->end_of_block = fz_to_bool(ctx, obj); - obj = fz_dict_gets(params, "BlackIs1"); - if (obj) fax->black_is_1 = fz_to_bool(obj); + obj = fz_dict_gets(ctx, params, "BlackIs1"); + if (obj) fax->black_is_1 = fz_to_bool(ctx, obj); fax->stride = ((fax->columns - 1) >> 3) + 1; fax->ridx = 0; @@ -719,13 +722,13 @@ fz_open_faxd(fz_stream *chain, fz_obj *params) fax->dim = fax->k < 0 ? 2 : 1; fax->eolc = 0; - fax->ref = fz_malloc(fax->stride); - fax->dst = fz_malloc(fax->stride); + fax->ref = fz_malloc(ctx, fax->stride); + fax->dst = fz_malloc(ctx, fax->stride); fax->rp = fax->dst; fax->wp = fax->dst + fax->stride; memset(fax->ref, 0, fax->stride); memset(fax->dst, 0, fax->stride); - return fz_new_stream(fax, read_faxd, close_faxd); + return fz_new_stream(ctx, fax, read_faxd, close_faxd); } diff --git a/fitz/filt_flate.c b/fitz/filt_flate.c index 4d4caf37..b7ef163d 100644 --- a/fitz/filt_flate.c +++ b/fitz/filt_flate.c @@ -12,12 +12,12 @@ struct fz_flate_s static void *zalloc(void *opaque, unsigned int items, unsigned int size) { - return fz_calloc(items, size); + return fz_calloc(opaque, items, size); } static void zfree(void *opaque, void *ptr) { - fz_free(ptr); + fz_free(opaque, ptr); } static int @@ -77,7 +77,7 @@ close_flated(fz_stream *stm) fz_warn("zlib error: inflateEnd: %s", state->z.msg); fz_close(state->chain); - fz_free(state); + fz_free(stm->ctx, state); } fz_stream * @@ -86,12 +86,12 @@ fz_open_flated(fz_stream *chain) fz_flate *state; int code; - state = fz_malloc(sizeof(fz_flate)); + state = fz_malloc(chain->ctx, sizeof(fz_flate)); state->chain = chain; state->z.zalloc = zalloc; state->z.zfree = zfree; - state->z.opaque = NULL; + state->z.opaque = chain->ctx; state->z.next_in = NULL; state->z.avail_in = 0; @@ -99,5 +99,5 @@ fz_open_flated(fz_stream *chain) if (code != Z_OK) fz_warn("zlib error: inflateInit: %s", state->z.msg); - return fz_new_stream(state, read_flated, close_flated); + return fz_new_stream(chain->ctx, state, read_flated, close_flated); } diff --git a/fitz/filt_jbig2d.c b/fitz/filt_jbig2d.c index ad335045..18aa0a22 100644 --- a/fitz/filt_jbig2d.c +++ b/fitz/filt_jbig2d.c @@ -38,7 +38,7 @@ close_jbig2d(fz_stream *stm) jbig2_global_ctx_free(state->gctx); jbig2_ctx_free(state->ctx); fz_close(state->chain); - fz_free(state); + fz_free(stm->ctx, state); } static int @@ -85,7 +85,7 @@ fz_open_jbig2d(fz_stream *chain, fz_buffer *globals) { fz_jbig2d *state; - state = fz_malloc(sizeof(fz_jbig2d)); + state = fz_malloc(chain->ctx, sizeof(fz_jbig2d)); state->chain = chain; state->ctx = jbig2_ctx_new(NULL, JBIG2_OPTIONS_EMBEDDED, NULL, NULL, NULL); state->gctx = NULL; @@ -99,5 +99,5 @@ fz_open_jbig2d(fz_stream *chain, fz_buffer *globals) state->ctx = jbig2_ctx_new(NULL, JBIG2_OPTIONS_EMBEDDED, state->gctx, NULL, NULL); } - return fz_new_stream(state, read_jbig2d, close_jbig2d); + return fz_new_stream(chain->ctx, state, read_jbig2d, close_jbig2d); } diff --git a/fitz/filt_jpxd.c b/fitz/filt_jpxd.c index d646cabc..07d6a572 100644 --- a/fitz/filt_jpxd.c +++ b/fitz/filt_jpxd.c @@ -19,7 +19,7 @@ static void fz_opj_info_callback(const char *msg, void *client_data) } fz_error -fz_load_jpx_image(fz_pixmap **imgp, unsigned char *data, int size, fz_colorspace *defcs) +fz_load_jpx_image(fz_context *ctx, fz_pixmap **imgp, unsigned char *data, int size, fz_colorspace *defcs) { fz_pixmap *img; opj_event_mgr_t evtmgr; @@ -108,7 +108,7 @@ fz_load_jpx_image(fz_pixmap **imgp, unsigned char *data, int size, fz_colorspace } } - img = fz_new_pixmap_with_limit(colorspace, w, h); + img = fz_new_pixmap_with_limit(ctx, colorspace, w, h); if (!img) { opj_image_destroy(jpx); @@ -138,9 +138,9 @@ fz_load_jpx_image(fz_pixmap **imgp, unsigned char *data, int size, fz_colorspace { if (n == 4) { - fz_pixmap *tmp = fz_new_pixmap(fz_device_rgb, w, h); - fz_convert_pixmap(img, tmp); - fz_drop_pixmap(img); + fz_pixmap *tmp = fz_new_pixmap(ctx, fz_device_rgb, w, h); + fz_convert_pixmap(ctx, img, tmp); + fz_drop_pixmap(ctx, img); img = tmp; } fz_premultiply_pixmap(img); diff --git a/fitz/filt_lzwd.c b/fitz/filt_lzwd.c index 942e5837..b6534dfe 100644 --- a/fitz/filt_lzwd.c +++ b/fitz/filt_lzwd.c @@ -160,7 +160,7 @@ close_lzwd(fz_stream *stm) { fz_lzwd *lzw = stm->state; fz_close(lzw->chain); - fz_free(lzw); + fz_free(stm->ctx, lzw); } fz_stream * @@ -170,14 +170,15 @@ fz_open_lzwd(fz_stream *chain, fz_obj *params) fz_obj *obj; int i; - lzw = fz_malloc(sizeof(fz_lzwd)); + assert(chain != NULL); + lzw = fz_malloc(chain->ctx, sizeof(fz_lzwd)); lzw->chain = chain; lzw->eod = 0; lzw->early_change = 1; - obj = fz_dict_gets(params, "EarlyChange"); + obj = fz_dict_gets(chain->ctx, params, "EarlyChange"); if (obj) - lzw->early_change = !!fz_to_int(obj); + lzw->early_change = !!fz_to_int(chain->ctx, obj); for (i = 0; i < 256; i++) { @@ -202,5 +203,5 @@ fz_open_lzwd(fz_stream *chain, fz_obj *params) lzw->rp = lzw->bp; lzw->wp = lzw->bp; - return fz_new_stream(lzw, read_lzwd, close_lzwd); + return fz_new_stream(chain->ctx, lzw, read_lzwd, close_lzwd); } diff --git a/fitz/filt_predict.c b/fitz/filt_predict.c index 17cfe905..fc22ed97 100644 --- a/fitz/filt_predict.c +++ b/fitz/filt_predict.c @@ -183,10 +183,10 @@ close_predict(fz_stream *stm) { fz_predict *state = stm->state; fz_close(state->chain); - fz_free(state->in); - fz_free(state->out); - fz_free(state->ref); - fz_free(state); + fz_free(stm->ctx, state->in); + fz_free(stm->ctx, state->out); + fz_free(stm->ctx, state->ref); + fz_free(stm->ctx, state); } fz_stream * @@ -194,8 +194,9 @@ fz_open_predict(fz_stream *chain, fz_obj *params) { fz_predict *state; fz_obj *obj; + fz_context *ctx = chain->ctx; - state = fz_malloc(sizeof(fz_predict)); + state = fz_malloc(ctx, sizeof(fz_predict)); state->chain = chain; state->predictor = 1; @@ -203,9 +204,9 @@ fz_open_predict(fz_stream *chain, fz_obj *params) state->colors = 1; state->bpc = 8; - obj = fz_dict_gets(params, "Predictor"); + obj = fz_dict_gets(ctx, params, "Predictor"); if (obj) - state->predictor = fz_to_int(obj); + state->predictor = fz_to_int(ctx, obj); if (state->predictor != 1 && state->predictor != 2 && state->predictor != 10 && state->predictor != 11 && @@ -216,28 +217,28 @@ fz_open_predict(fz_stream *chain, fz_obj *params) state->predictor = 1; } - obj = fz_dict_gets(params, "Columns"); + obj = fz_dict_gets(ctx, params, "Columns"); if (obj) - state->columns = fz_to_int(obj); + state->columns = fz_to_int(ctx, obj); - obj = fz_dict_gets(params, "Colors"); + obj = fz_dict_gets(ctx, params, "Colors"); if (obj) - state->colors = fz_to_int(obj); + state->colors = fz_to_int(ctx, obj); - obj = fz_dict_gets(params, "BitsPerComponent"); + obj = fz_dict_gets(ctx, params, "BitsPerComponent"); if (obj) - state->bpc = fz_to_int(obj); + state->bpc = fz_to_int(ctx, obj); state->stride = (state->bpc * state->colors * state->columns + 7) / 8; state->bpp = (state->bpc * state->colors + 7) / 8; - state->in = fz_malloc(state->stride + 1); - state->out = fz_malloc(state->stride); - state->ref = fz_malloc(state->stride); + state->in = fz_malloc(ctx, state->stride + 1); + state->out = fz_malloc(ctx, state->stride); + state->ref = fz_malloc(ctx, state->stride); state->rp = state->out; state->wp = state->out; memset(state->ref, 0, state->stride); - return fz_new_stream(state, read_predict, close_predict); + return fz_new_stream(ctx, state, read_predict, close_predict); } diff --git a/fitz/fitz.h b/fitz/fitz.h index afc92436..1311f019 100644 --- a/fitz/fitz.h +++ b/fitz/fitz.h @@ -62,6 +62,11 @@ int gettimeofday(struct timeval *tv, struct timezone *tz); #define M_SQRT2 1.41421356237309504880 #endif +/* Context types */ +typedef struct fz_except_context fz_except_context; +typedef struct fz_alloc_context fz_alloc_context; +typedef struct fz_context fz_context; + /* * Variadic macros, inline and restrict keywords */ @@ -70,7 +75,7 @@ int gettimeofday(struct timeval *tv, struct timezone *tz); #define fz_error_make(...) fz_error_make_imp(__FILE__, __LINE__, __func__, __VA_ARGS__) #define fz_error_note(cause, ...) fz_error_note_imp(__FILE__, __LINE__, __func__, cause, __VA_ARGS__) -#define fz_catch(cause, ...) fz_catch_imp(__FILE__, __LINE__, __func__, cause, __VA_ARGS__) +#define fz_error_handle(cause, ...) fz_error_handle_imp(__FILE__, __LINE__, __func__, cause, __VA_ARGS__) #elif _MSC_VER >= 1500 /* MSVC 9 or newer */ @@ -78,7 +83,7 @@ int gettimeofday(struct timeval *tv, struct timezone *tz); #define restrict __restrict #define fz_error_make(...) fz_error_make_imp(__FILE__, __LINE__, __FUNCTION__, __VA_ARGS__) #define fz_error_note(cause, ...) fz_error_note_imp(__FILE__, __LINE__, __FUNCTION__, cause, __VA_ARGS__) -#define fz_catch(cause, ...) fz_error_handle_imp(__FILE__, __LINE__, __FUNCTION__, cause, __VA_ARGS__) +#define fz_error_handle(cause, ...) fz_error_handle_imp(__FILE__, __LINE__, __FUNCTION__, cause, __VA_ARGS__) #elif __GNUC__ >= 3 /* GCC 3 or newer */ @@ -94,7 +99,7 @@ int gettimeofday(struct timeval *tv, struct timezone *tz); #define restrict #define fz_error_make fz_error_make_impx #define fz_error_note fz_error_note_impx -#define fz_error_handle fz_catch_impx +#define fz_error_handle fz_error_handle_impx #endif @@ -139,11 +144,20 @@ char *fz_get_error_line(int n); */ /* memory allocation */ -void *fz_malloc(int size); -void *fz_calloc(int count, int size); -void *fz_realloc(void *p, int count, int size); -void fz_free(void *p); -char *fz_strdup(char *s); + +/* The following throw exceptions on failure to allocate */ +void *fz_malloc(fz_context *ctx, size_t size); +void *fz_calloc(fz_context *ctx, size_t count, size_t size); +void *fz_realloc(fz_context *ctx, void *p, size_t size); +char *fz_strdup(fz_context *ctx, char *s); + +void fz_free(fz_context *ctx, void *p); + +/* The following returns NULL on failure to allocate */ +void *fz_malloc_nothrow(fz_context *ctx, size_t size); +void *fz_calloc_nothrow(fz_context *ctx, size_t count, size_t size); +void *fz_realloc_nothrow(fz_context *ctx, void *p, size_t size); +char *fz_strdup_nothrow(fz_context *ctx, char *s); /* runtime (hah!) test for endian-ness */ int fz_is_big_endian(void); @@ -172,13 +186,13 @@ extern char *fz_optarg; typedef struct fz_hash_table_s fz_hash_table; -fz_hash_table *fz_new_hash_table(int initialsize, int keylen); +fz_hash_table *fz_new_hash_table(fz_context *ctx, int initialsize, int keylen); void fz_debug_hash(fz_hash_table *table); void fz_empty_hash(fz_hash_table *table); -void fz_free_hash(fz_hash_table *table); +void fz_free_hash(fz_context *ctx, fz_hash_table *table); void *fz_hash_find(fz_hash_table *table, void *key); -void fz_hash_insert(fz_hash_table *table, void *key, void *val); +void fz_hash_insert(fz_context *ctx, fz_hash_table *table, void *key, void *val); void fz_hash_remove(fz_hash_table *table, void *key); int fz_hash_len(fz_hash_table *table); @@ -357,70 +371,70 @@ void aes_crypt_cbc( fz_aes *ctx, int mode, int length, typedef struct fz_obj_s fz_obj; -extern fz_obj* (*fz_resolve_indirect)(fz_obj*); +#define fz_resolve_indirect(ctx, obj) (ctx)->fz_resolve_indirect((obj)) -fz_obj *fz_new_null(void); -fz_obj *fz_new_bool(int b); -fz_obj *fz_new_int(int i); -fz_obj *fz_new_real(float f); -fz_obj *fz_new_name(char *str); -fz_obj *fz_new_string(char *str, int len); -fz_obj *fz_new_indirect(int num, int gen, void *xref); +fz_obj *fz_new_null(fz_context *ctx); +fz_obj *fz_new_bool(fz_context *ctx, int b); +fz_obj *fz_new_int(fz_context *ctx, int i); +fz_obj *fz_new_real(fz_context *ctx, float f); +fz_obj *fz_new_name(fz_context *ctx, char *str); +fz_obj *fz_new_string(fz_context *ctx, char *str, int len); +fz_obj *fz_new_indirect(fz_context *ctx, int num, int gen, void *xref); -fz_obj *fz_new_array(int initialcap); -fz_obj *fz_new_dict(int initialcap); -fz_obj *fz_copy_array(fz_obj *array); -fz_obj *fz_copy_dict(fz_obj *dict); +fz_obj *fz_new_array(fz_context *ctx, int initialcap); +fz_obj *fz_new_dict(fz_context *ctx, int initialcap); +fz_obj *fz_copy_array(fz_context *ctx, fz_obj *array); +fz_obj *fz_copy_dict(fz_context *ctx, fz_obj *dict); fz_obj *fz_keep_obj(fz_obj *obj); -void fz_drop_obj(fz_obj *obj); +void fz_drop_obj(fz_context *ctx, fz_obj *obj); /* type queries */ -int fz_is_null(fz_obj *obj); -int fz_is_bool(fz_obj *obj); -int fz_is_int(fz_obj *obj); -int fz_is_real(fz_obj *obj); -int fz_is_name(fz_obj *obj); -int fz_is_string(fz_obj *obj); -int fz_is_array(fz_obj *obj); -int fz_is_dict(fz_obj *obj); +int fz_is_null(fz_context *ctx, fz_obj *obj); +int fz_is_bool(fz_context *ctx, fz_obj *obj); +int fz_is_int(fz_context *ctx, fz_obj *obj); +int fz_is_real(fz_context *ctx, fz_obj *obj); +int fz_is_name(fz_context *ctx, fz_obj *obj); +int fz_is_string(fz_context *ctx, fz_obj *obj); +int fz_is_array(fz_context *ctx, fz_obj *obj); +int fz_is_dict(fz_context *ctx, fz_obj *obj); int fz_is_indirect(fz_obj *obj); int fz_objcmp(fz_obj *a, fz_obj *b); /* safe, silent failure, no error reporting */ -int fz_to_bool(fz_obj *obj); -int fz_to_int(fz_obj *obj); -float fz_to_real(fz_obj *obj); -char *fz_to_name(fz_obj *obj); -char *fz_to_str_buf(fz_obj *obj); -int fz_to_str_len(fz_obj *obj); +int fz_to_bool(fz_context *ctx, fz_obj *obj); +int fz_to_int(fz_context *ctx, fz_obj *obj); +float fz_to_real(fz_context *ctx, fz_obj *obj); +char *fz_to_name(fz_context *ctx, fz_obj *obj); +char *fz_to_str_buf(fz_context *ctx, fz_obj *obj); +int fz_to_str_len(fz_context *ctx, fz_obj *obj); int fz_to_num(fz_obj *obj); int fz_to_gen(fz_obj *obj); -int fz_array_len(fz_obj *array); -fz_obj *fz_array_get(fz_obj *array, int i); -void fz_array_put(fz_obj *array, int i, fz_obj *obj); -void fz_array_push(fz_obj *array, fz_obj *obj); -void fz_array_insert(fz_obj *array, fz_obj *obj); - -int fz_dict_len(fz_obj *dict); -fz_obj *fz_dict_get_key(fz_obj *dict, int idx); -fz_obj *fz_dict_get_val(fz_obj *dict, int idx); -fz_obj *fz_dict_get(fz_obj *dict, fz_obj *key); -fz_obj *fz_dict_gets(fz_obj *dict, char *key); -fz_obj *fz_dict_getsa(fz_obj *dict, char *key, char *abbrev); -void fz_dict_put(fz_obj *dict, fz_obj *key, fz_obj *val); -void fz_dict_puts(fz_obj *dict, char *key, fz_obj *val); -void fz_dict_del(fz_obj *dict, fz_obj *key); -void fz_dict_dels(fz_obj *dict, char *key); -void fz_sort_dict(fz_obj *dict); - -int fz_fprint_obj(FILE *fp, fz_obj *obj, int tight); -void fz_debug_obj(fz_obj *obj); -void fz_debug_ref(fz_obj *obj); - -void fz_set_str_len(fz_obj *obj, int newlen); /* private */ +int fz_array_len(fz_context *ctx, fz_obj *array); +fz_obj *fz_array_get(fz_context *ctx, fz_obj *array, int i); +void fz_array_put(fz_context *ctx, fz_obj *array, int i, fz_obj *obj); +void fz_array_push(fz_context *ctx, fz_obj *array, fz_obj *obj); +void fz_array_insert(fz_context *ctx, fz_obj *array, fz_obj *obj); + +int fz_dict_len(fz_context *ctx, fz_obj *dict); +fz_obj *fz_dict_get_key(fz_context *ctx, fz_obj *dict, int idx); +fz_obj *fz_dict_get_val(fz_context *ctx, fz_obj *dict, int idx); +fz_obj *fz_dict_get(fz_context *ctx, fz_obj *dict, fz_obj *key); +fz_obj *fz_dict_gets(fz_context *ctx, fz_obj *dict, char *key); +fz_obj *fz_dict_getsa(fz_context *ctx, fz_obj *dict, char *key, char *abbrev); +void fz_dict_put(fz_context *ctx, fz_obj *dict, fz_obj *key, fz_obj *val); +void fz_dict_puts(fz_context *ctx, fz_obj *dict, char *key, fz_obj *val); +void fz_dict_del(fz_context *ctx, fz_obj *dict, fz_obj *key); +void fz_dict_dels(fz_context *ctx, fz_obj *dict, char *key); +void fz_sort_dict(fz_context *ctx, fz_obj *dict); + +int fz_fprint_obj(fz_context *ctx, FILE *fp, fz_obj *obj, int tight); +void fz_debug_obj(fz_context *ctx, fz_obj *obj); +void fz_debug_ref(fz_context *ctx, fz_obj *obj); + +void fz_set_str_len(fz_context *ctx, fz_obj *obj, int newlen); /* private */ void *fz_get_indirect_xref(fz_obj *obj); /* private */ /* @@ -436,12 +450,12 @@ struct fz_buffer_s int cap, len; }; -fz_buffer *fz_new_buffer(int size); +fz_buffer *fz_new_buffer(fz_context *ctx, int size); fz_buffer *fz_keep_buffer(fz_buffer *buf); -void fz_drop_buffer(fz_buffer *buf); +void fz_drop_buffer(fz_context *ctx, fz_buffer *buf); -void fz_resize_buffer(fz_buffer *buf, int size); -void fz_grow_buffer(fz_buffer *buf); +void fz_resize_buffer(fz_context *ctx, fz_buffer *buf, int size); +void fz_grow_buffer(fz_context *ctx, fz_buffer *buf); /* * Buffered reader. @@ -460,20 +474,21 @@ struct fz_stream_s int bits; unsigned char *bp, *rp, *wp, *ep; void *state; + fz_context *ctx; int (*read)(fz_stream *stm, unsigned char *buf, int len); void (*close)(fz_stream *stm); void (*seek)(fz_stream *stm, int offset, int whence); unsigned char buf[4096]; }; -fz_stream *fz_open_fd(int file); -fz_stream *fz_open_file(const char *filename); -fz_stream *fz_open_file_w(const wchar_t *filename); /* only on win32 */ -fz_stream *fz_open_buffer(fz_buffer *buf); -fz_stream *fz_open_memory(unsigned char *data, int len); +fz_stream *fz_open_fd(fz_context *ctx, int file); +fz_stream *fz_open_file(fz_context *ctx, const char *filename); +fz_stream *fz_open_file_w(fz_context *ctx, const wchar_t *filename); /* only on win32 */ +fz_stream *fz_open_buffer(fz_context *ctx, fz_buffer *buf); +fz_stream *fz_open_memory(fz_context *ctx, unsigned char *data, int len); void fz_close(fz_stream *stm); -fz_stream *fz_new_stream(void*, int(*)(fz_stream*, unsigned char*, int), void(*)(fz_stream *)); +fz_stream *fz_new_stream(fz_context *ctx, void*, int(*)(fz_stream*, unsigned char*, int), void(*)(fz_stream *)); fz_stream *fz_keep_stream(fz_stream *stm); void fz_fill_buffer(fz_stream *stm); @@ -612,32 +627,32 @@ struct fz_pixmap_s }; /* will return NULL if soft limit is exceeded */ -fz_pixmap *fz_new_pixmap_with_limit(fz_colorspace *colorspace, int w, int h); +fz_pixmap *fz_new_pixmap_with_limit(fz_context *ctx, fz_colorspace *colorspace, int w, int h); -fz_pixmap *fz_new_pixmap_with_data(fz_colorspace *colorspace, int w, int h, unsigned char *samples); -fz_pixmap *fz_new_pixmap_with_rect(fz_colorspace *, fz_bbox bbox); -fz_pixmap *fz_new_pixmap_with_rect_and_data(fz_colorspace *, fz_bbox bbox, unsigned char *samples); -fz_pixmap *fz_new_pixmap(fz_colorspace *, int w, int h); +fz_pixmap *fz_new_pixmap_with_data(fz_context *ctx, fz_colorspace *colorspace, int w, int h, unsigned char *samples); +fz_pixmap *fz_new_pixmap_with_rect(fz_context *ctx, fz_colorspace *, fz_bbox bbox); +fz_pixmap *fz_new_pixmap_with_rect_and_data(fz_context *ctx, fz_colorspace *, fz_bbox bbox, unsigned char *samples); +fz_pixmap *fz_new_pixmap(fz_context *ctx, fz_colorspace *, int w, int h); fz_pixmap *fz_keep_pixmap(fz_pixmap *pix); -void fz_drop_pixmap(fz_pixmap *pix); +void fz_drop_pixmap(fz_context *ctx, fz_pixmap *pix); void fz_clear_pixmap(fz_pixmap *pix); void fz_clear_pixmap_with_color(fz_pixmap *pix, int value); void fz_clear_pixmap_rect_with_color(fz_pixmap *pix, int value, fz_bbox r); void fz_copy_pixmap_rect(fz_pixmap *dest, fz_pixmap *src, fz_bbox r); void fz_premultiply_pixmap(fz_pixmap *pix); -fz_pixmap *fz_alpha_from_gray(fz_pixmap *gray, int luminosity); +fz_pixmap *fz_alpha_from_gray(fz_context *ctx, fz_pixmap *gray, int luminosity); fz_bbox fz_bound_pixmap(fz_pixmap *pix); void fz_invert_pixmap(fz_pixmap *pix); void fz_gamma_pixmap(fz_pixmap *pix, float gamma); -fz_pixmap *fz_scale_pixmap(fz_pixmap *src, float x, float y, float w, float h); -fz_pixmap *fz_scale_pixmap_gridfit(fz_pixmap *src, float x, float y, float w, float h, int gridfit); +fz_pixmap *fz_scale_pixmap(fz_context *ctx, fz_pixmap *src, float x, float y, float w, float h); +fz_pixmap *fz_scale_pixmap_gridfit(fz_context *ctx, fz_pixmap *src, float x, float y, float w, float h, int gridfit); fz_error fz_write_pnm(fz_pixmap *pixmap, char *filename); fz_error fz_write_pam(fz_pixmap *pixmap, char *filename, int savealpha); -fz_error fz_write_png(fz_pixmap *pixmap, char *filename, int savealpha); +fz_error fz_write_png(fz_context *ctx, fz_pixmap *pixmap, char *filename, int savealpha); -fz_error fz_load_jpx_image(fz_pixmap **imgp, unsigned char *data, int size, fz_colorspace *dcs); +fz_error fz_load_jpx_image(fz_context *ctx, fz_pixmap **imgp, unsigned char *data, int size, fz_colorspace *dcs); /* * Bitmaps have 1 component per bit. Only used for creating halftoned versions @@ -654,10 +669,10 @@ struct fz_bitmap_s unsigned char *samples; }; -fz_bitmap *fz_new_bitmap(int w, int h, int n); +fz_bitmap *fz_new_bitmap(fz_context *ctx, int w, int h, int n); fz_bitmap *fz_keep_bitmap(fz_bitmap *bit); void fz_clear_bitmap(fz_bitmap *bit); -void fz_drop_bitmap(fz_bitmap *bit); +void fz_drop_bitmap(fz_context *ctx, fz_bitmap *bit); fz_error fz_write_pbm(fz_bitmap *bitmap, char *filename); @@ -675,12 +690,12 @@ struct fz_halftone_s fz_pixmap *comp[1]; }; -fz_halftone *fz_new_halftone(int num_comps); -fz_halftone *fz_get_default_halftone(int num_comps); +fz_halftone *fz_new_halftone(fz_context *ctx, int num_comps); +fz_halftone *fz_get_default_halftone(fz_context *ctx, int num_comps); fz_halftone *fz_keep_halftone(fz_halftone *half); -void fz_drop_halftone(fz_halftone *half); +void fz_drop_halftone(fz_context *ctx, fz_halftone *half); -fz_bitmap *fz_halftone_pixmap(fz_pixmap *pix, fz_halftone *ht); +fz_bitmap *fz_halftone_pixmap(fz_context *ctx, fz_pixmap *pix, fz_halftone *ht); /* * Colorspace resources. @@ -698,16 +713,16 @@ struct fz_colorspace_s int n; void (*to_rgb)(fz_colorspace *, float *src, float *rgb); void (*from_rgb)(fz_colorspace *, float *rgb, float *dst); - void (*free_data)(fz_colorspace *); + void (*free_data)(fz_context *Ctx, fz_colorspace *); void *data; }; -fz_colorspace *fz_new_colorspace(char *name, int n); +fz_colorspace *fz_new_colorspace(fz_context *ctx, char *name, int n); fz_colorspace *fz_keep_colorspace(fz_colorspace *colorspace); -void fz_drop_colorspace(fz_colorspace *colorspace); +void fz_drop_colorspace(fz_context *ctx, fz_colorspace *colorspace); void fz_convert_color(fz_colorspace *srcs, float *srcv, fz_colorspace *dsts, float *dstv); -void fz_convert_pixmap(fz_pixmap *src, fz_pixmap *dst); +void fz_convert_pixmap(fz_context *ctx, fz_pixmap *src, fz_pixmap *dst); fz_colorspace *fz_find_device_colorspace(char *name); @@ -753,13 +768,13 @@ struct fz_font_s int *width_table; }; -fz_font *fz_new_type3_font(char *name, fz_matrix matrix); +fz_font *fz_new_type3_font(fz_context *ctx, char *name, fz_matrix matrix); -fz_error fz_new_font_from_memory(fz_font **fontp, unsigned char *data, int len, int index); -fz_error fz_new_font_from_file(fz_font **fontp, char *path, int index); +fz_error fz_new_font_from_memory(fz_context *ctx, fz_font **fontp, unsigned char *data, int len, int index); +fz_error fz_new_font_from_file(fz_context *ctx, fz_font **fontp, char *path, int index); fz_font *fz_keep_font(fz_font *font); -void fz_drop_font(fz_font *font); +void fz_drop_font(fz_context *ctx, fz_font *font); void fz_debug_font(fz_font *font); void fz_set_font_bbox(fz_font *font, float xmin, float ymin, float xmax, float ymax); @@ -809,18 +824,18 @@ struct fz_stroke_state_s float dash_list[32]; }; -fz_path *fz_new_path(void); -void fz_moveto(fz_path*, float x, float y); -void fz_lineto(fz_path*, float x, float y); -void fz_curveto(fz_path*, float, float, float, float, float, float); -void fz_curvetov(fz_path*, float, float, float, float); -void fz_curvetoy(fz_path*, float, float, float, float); -void fz_closepath(fz_path*); -void fz_free_path(fz_path *path); +fz_path *fz_new_path(fz_context *ctx); +void fz_moveto(fz_context*, fz_path*, float x, float y); +void fz_lineto(fz_context*, fz_path*, float x, float y); +void fz_curveto(fz_context*,fz_path*, float, float, float, float, float, float); +void fz_curvetov(fz_context*,fz_path*, float, float, float, float); +void fz_curvetoy(fz_context*,fz_path*, float, float, float, float); +void fz_closepath(fz_context*,fz_path*); +void fz_free_path(fz_context *ctx, fz_path *path); void fz_transform_path(fz_path *path, fz_matrix transform); -fz_path *fz_clone_path(fz_path *old); +fz_path *fz_clone_path(fz_context *ctx, fz_path *old); fz_rect fz_bound_path(fz_path *path, fz_stroke_state *stroke, fz_matrix ctm); void fz_debug_path(fz_path *, int indent); @@ -856,12 +871,12 @@ struct fz_text_s fz_text_item *items; }; -fz_text *fz_new_text(fz_font *face, fz_matrix trm, int wmode); -void fz_add_text(fz_text *text, int gid, int ucs, float x, float y); -void fz_free_text(fz_text *text); +fz_text *fz_new_text(fz_context *ctx, fz_font *face, fz_matrix trm, int wmode); +void fz_add_text(fz_context *ctx, fz_text *text, int gid, int ucs, float x, float y); +void fz_free_text(fz_context *ctx, fz_text *text); void fz_debug_text(fz_text*, int indent); fz_rect fz_bound_text(fz_text *text, fz_matrix ctm); -fz_text *fz_clone_text(fz_text *old); +fz_text *fz_clone_text(fz_context *ctx, fz_text *old); /* * The shading code uses gouraud shaded triangle meshes. @@ -899,11 +914,11 @@ struct fz_shade_s }; fz_shade *fz_keep_shade(fz_shade *shade); -void fz_drop_shade(fz_shade *shade); +void fz_drop_shade(fz_context *ctx, fz_shade *shade); void fz_debug_shade(fz_shade *shade); fz_rect fz_bound_shade(fz_shade *shade, fz_matrix ctm); -void fz_paint_shade(fz_shade *shade, fz_matrix ctm, fz_pixmap *dest, fz_bbox bbox); +void fz_paint_shade(fz_context *ctx, fz_shade *shade, fz_matrix ctm, fz_pixmap *dest, fz_bbox bbox); /* * Glyph cache @@ -911,13 +926,13 @@ void fz_paint_shade(fz_shade *shade, fz_matrix ctm, fz_pixmap *dest, fz_bbox bbo typedef struct fz_glyph_cache_s fz_glyph_cache; -fz_glyph_cache *fz_new_glyph_cache(void); -fz_pixmap *fz_render_ft_glyph(fz_font *font, int cid, fz_matrix trm); -fz_pixmap *fz_render_t3_glyph(fz_font *font, int cid, fz_matrix trm, fz_colorspace *model); -fz_pixmap *fz_render_ft_stroked_glyph(fz_font *font, int gid, fz_matrix trm, fz_matrix ctm, fz_stroke_state *state); -fz_pixmap *fz_render_glyph(fz_glyph_cache*, fz_font*, int, fz_matrix, fz_colorspace *model); -fz_pixmap *fz_render_stroked_glyph(fz_glyph_cache*, fz_font*, int, fz_matrix, fz_matrix, fz_stroke_state *stroke); -void fz_free_glyph_cache(fz_glyph_cache *); +fz_glyph_cache *fz_new_glyph_cache(fz_context *ctx); +fz_pixmap *fz_render_ft_glyph(fz_context *ctx, fz_font *font, int cid, fz_matrix trm); +fz_pixmap *fz_render_t3_glyph(fz_context *ctx, fz_font *font, int cid, fz_matrix trm, fz_colorspace *model); +fz_pixmap *fz_render_ft_stroked_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm, fz_matrix ctm, fz_stroke_state *state); +fz_pixmap *fz_render_glyph(fz_context *ctx, fz_glyph_cache*, fz_font*, int, fz_matrix, fz_colorspace *model); +fz_pixmap *fz_render_stroked_glyph(fz_context *ctx, fz_glyph_cache*, fz_font*, int, fz_matrix, fz_matrix, fz_stroke_state *stroke); +void fz_free_glyph_cache(fz_context *ctx, fz_glyph_cache *); /* * Scan converter @@ -928,7 +943,7 @@ void fz_set_aa_level(int bits); typedef struct fz_gel_s fz_gel; -fz_gel *fz_new_gel(void); +fz_gel *fz_new_gel(fz_context *ctx); void fz_insert_gel(fz_gel *gel, float x0, float y0, float x1, float y1); void fz_reset_gel(fz_gel *gel, fz_bbox clip); void fz_sort_gel(fz_gel *gel); @@ -965,33 +980,34 @@ struct fz_device_s int flags; void *user; - void (*free_user)(void *); - - void (*fill_path)(void *, fz_path *, int even_odd, fz_matrix, fz_colorspace *, float *color, float alpha); - void (*stroke_path)(void *, fz_path *, fz_stroke_state *, fz_matrix, fz_colorspace *, float *color, float alpha); - void (*clip_path)(void *, fz_path *, fz_rect *rect, int even_odd, fz_matrix); - void (*clip_stroke_path)(void *, fz_path *, fz_rect *rect, fz_stroke_state *, fz_matrix); - - void (*fill_text)(void *, fz_text *, fz_matrix, fz_colorspace *, float *color, float alpha); - void (*stroke_text)(void *, fz_text *, fz_stroke_state *, fz_matrix, fz_colorspace *, float *color, float alpha); - void (*clip_text)(void *, fz_text *, fz_matrix, int accumulate); - void (*clip_stroke_text)(void *, fz_text *, fz_stroke_state *, fz_matrix); - void (*ignore_text)(void *, fz_text *, fz_matrix); - - void (*fill_shade)(void *, fz_shade *shd, fz_matrix ctm, float alpha); - void (*fill_image)(void *, fz_pixmap *img, fz_matrix ctm, float alpha); - void (*fill_image_mask)(void *, fz_pixmap *img, fz_matrix ctm, fz_colorspace *, float *color, float alpha); - void (*clip_image_mask)(void *, fz_pixmap *img, fz_rect *rect, fz_matrix ctm); - - void (*pop_clip)(void *); - - void (*begin_mask)(void *, fz_rect, int luminosity, fz_colorspace *, float *bc); - void (*end_mask)(void *); - void (*begin_group)(void *, fz_rect, int isolated, int knockout, int blendmode, float alpha); - void (*end_group)(void *); - - void (*begin_tile)(void *, fz_rect area, fz_rect view, float xstep, float ystep, fz_matrix ctm); - void (*end_tile)(void *); + void (*free_user)(fz_device *); + fz_context *ctx; + + void (*fill_path)(fz_device *, fz_path *, int even_odd, fz_matrix, fz_colorspace *, float *color, float alpha); + void (*stroke_path)(fz_device *, fz_path *, fz_stroke_state *, fz_matrix, fz_colorspace *, float *color, float alpha); + void (*clip_path)(fz_device *, fz_path *, fz_rect *rect, int even_odd, fz_matrix); + void (*clip_stroke_path)(fz_device *, fz_path *, fz_rect *rect, fz_stroke_state *, fz_matrix); + + void (*fill_text)(fz_device *, fz_text *, fz_matrix, fz_colorspace *, float *color, float alpha); + void (*stroke_text)(fz_device *, fz_text *, fz_stroke_state *, fz_matrix, fz_colorspace *, float *color, float alpha); + void (*clip_text)(fz_device *, fz_text *, fz_matrix, int accumulate); + void (*clip_stroke_text)(fz_device *, fz_text *, fz_stroke_state *, fz_matrix); + void (*ignore_text)(fz_device *, fz_text *, fz_matrix); + + void (*fill_shade)(fz_device *, fz_shade *shd, fz_matrix ctm, float alpha); + void (*fill_image)(fz_device *, fz_pixmap *img, fz_matrix ctm, float alpha); + void (*fill_image_mask)(fz_device *, fz_pixmap *img, fz_matrix ctm, fz_colorspace *, float *color, float alpha); + void (*clip_image_mask)(fz_device *, fz_pixmap *img, fz_rect *rect, fz_matrix ctm); + + void (*pop_clip)(fz_device *); + + void (*begin_mask)(fz_device *, fz_rect, int luminosity, fz_colorspace *, float *bc); + void (*end_mask)(fz_device *); + void (*begin_group)(fz_device *, fz_rect, int isolated, int knockout, int blendmode, float alpha); + void (*end_group)(fz_device *); + + void (*begin_tile)(fz_device *, fz_rect area, fz_rect view, float xstep, float ystep, fz_matrix ctm); + void (*end_tile)(fz_device *); }; void fz_fill_path(fz_device *dev, fz_path *path, int even_odd, fz_matrix ctm, fz_colorspace *colorspace, float *color, float alpha); @@ -1015,13 +1031,13 @@ void fz_end_group(fz_device *dev); void fz_begin_tile(fz_device *dev, fz_rect area, fz_rect view, float xstep, float ystep, fz_matrix ctm); void fz_end_tile(fz_device *dev); -fz_device *fz_new_device(void *user); +fz_device *fz_new_device(fz_context *ctx, void *user); void fz_free_device(fz_device *dev); -fz_device *fz_new_trace_device(void); -fz_device *fz_new_bbox_device(fz_bbox *bboxp); -fz_device *fz_new_draw_device(fz_glyph_cache *cache, fz_pixmap *dest); -fz_device *fz_new_draw_device_type3(fz_glyph_cache *cache, fz_pixmap *dest); +fz_device *fz_new_trace_device(fz_context *ctx); +fz_device *fz_new_bbox_device(fz_context *ctx, fz_bbox *bboxp); +fz_device *fz_new_draw_device(fz_context *ctx, fz_glyph_cache *cache, fz_pixmap *dest); +fz_device *fz_new_draw_device_type3(fz_context *ctx, fz_glyph_cache *cache, fz_pixmap *dest); /* * Text extraction device @@ -1047,12 +1063,12 @@ struct fz_text_span_s int eol; }; -fz_text_span *fz_new_text_span(void); -void fz_free_text_span(fz_text_span *line); +fz_text_span *fz_new_text_span(fz_context *ctx); +void fz_free_text_span(fz_context *ctx, fz_text_span *line); void fz_debug_text_span(fz_text_span *line); void fz_debug_text_span_xml(fz_text_span *span); -fz_device *fz_new_text_device(fz_text_span *text); +fz_device *fz_new_text_device(fz_context *ctx, fz_text_span *text); /* * Display list device -- record and play back device commands. @@ -1060,9 +1076,9 @@ fz_device *fz_new_text_device(fz_text_span *text); typedef struct fz_display_list_s fz_display_list; -fz_display_list *fz_new_display_list(void); -void fz_free_display_list(fz_display_list *list); -fz_device *fz_new_list_device(fz_display_list *list); +fz_display_list *fz_new_display_list(fz_context *ctx); +void fz_free_display_list(fz_context *ctx, fz_display_list *list); +fz_device *fz_new_list_device(fz_context *ctx, fz_display_list *list); void fz_execute_display_list(fz_display_list *list, fz_device *dev, fz_matrix ctm, fz_bbox area); /* @@ -1119,17 +1135,29 @@ enum FZ_BLEND_KNOCKOUT = 32 }; -/* Fitz context */ +/* Fitz allocator context */ -typedef struct fz_except_context fz_except_context; -typedef struct fz_alloc_context fz_alloc_context; +struct fz_alloc_context +{ + void *opaque; + void *(*malloc)(void *, size_t); + void *(*realloc)(void *, void *, size_t); + void (*free)(void *, void *); + void *(*calloc)(void *, size_t, size_t); +}; -typedef struct fz_context +extern fz_alloc_context fz_alloc_default; + +/* Fitz context */ + +struct fz_context { - fz_except_context *except; - fz_alloc_context *alloc; -} -fz_context; + fz_except_context *except; + fz_alloc_context *alloc; + fz_obj *(*fz_resolve_indirect)(fz_obj*); +}; +fz_context *fz_context_init(fz_alloc_context *alloc); +void fz_context_fin(fz_context *ctx); #endif diff --git a/fitz/memento.c b/fitz/memento.c index 638bb2b2..a9885072 100644 --- a/fitz/memento.c +++ b/fitz/memento.c @@ -30,6 +30,7 @@ #define COMPILING_MEMENTO_C #include "memento.h" #include <stdio.h> +#include <stdlib.h> #if defined(__linux__) #define MEMENTO_HAS_FORK @@ -52,7 +53,13 @@ char *getenv(const char *); #ifdef MEMENTO +#ifdef HAVE_VALGRIND #include "valgrind/memcheck.h" +#else +#define VALGRIND_MAKE_MEM_NOACCESS(p,s) do { } while (0==1) +#define VALGRIND_MAKE_MEM_UNDEFINED(p,s) do { } while (0==1) +#define VALGRIND_MAKE_MEM_DEFINED(p,s) do { } while (0==1) +#endif enum { Memento_PreSize = 16, diff --git a/fitz/obj_print.c b/fitz/obj_print.c index 30b34d95..a6313e02 100644 --- a/fitz/obj_print.c +++ b/fitz/obj_print.c @@ -10,6 +10,7 @@ struct fmt int col; int sep; int last; + fz_context *ctx; }; static void fmt_obj(struct fmt *fmt, fz_obj *obj); @@ -78,8 +79,8 @@ static inline void fmt_sep(struct fmt *fmt) static void fmt_str(struct fmt *fmt, fz_obj *obj) { - char *s = fz_to_str_buf(obj); - int n = fz_to_str_len(obj); + char *s = fz_to_str_buf(fmt->ctx, obj); + int n = fz_to_str_len(fmt->ctx, obj); int i, c; fmt_putc(fmt, '('); @@ -114,8 +115,8 @@ static void fmt_str(struct fmt *fmt, fz_obj *obj) static void fmt_hex(struct fmt *fmt, fz_obj *obj) { - char *s = fz_to_str_buf(obj); - int n = fz_to_str_len(obj); + char *s = fz_to_str_buf(fmt->ctx, obj); + int n = fz_to_str_len(fmt->ctx, obj); int i, b, c; fmt_putc(fmt, '<'); @@ -131,7 +132,7 @@ static void fmt_hex(struct fmt *fmt, fz_obj *obj) static void fmt_name(struct fmt *fmt, fz_obj *obj) { - unsigned char *s = (unsigned char *) fz_to_name(obj); + unsigned char *s = (unsigned char *) fz_to_name(fmt->ctx, obj); int i, c; fmt_putc(fmt, '/'); @@ -156,24 +157,26 @@ static void fmt_name(struct fmt *fmt, fz_obj *obj) static void fmt_array(struct fmt *fmt, fz_obj *obj) { - int i; + int i, n; + fz_context *ctx = fmt->ctx; + n = fz_array_len(ctx, obj); if (fmt->tight) { fmt_putc(fmt, '['); - for (i = 0; i < fz_array_len(obj); i++) { - fmt_obj(fmt, fz_array_get(obj, i)); + for (i = 0; i < n; i++) { + fmt_obj(fmt, fz_array_get(ctx, obj, i)); fmt_sep(fmt); } fmt_putc(fmt, ']'); } else { fmt_puts(fmt, "[ "); - for (i = 0; i < fz_array_len(obj); i++) { + for (i = 0; i < n; i++) { if (fmt->col > 60) { fmt_putc(fmt, '\n'); fmt_indent(fmt); } - fmt_obj(fmt, fz_array_get(obj, i)); + fmt_obj(fmt, fz_array_get(ctx, obj, i)); fmt_putc(fmt, ' '); } fmt_putc(fmt, ']'); @@ -183,15 +186,17 @@ static void fmt_array(struct fmt *fmt, fz_obj *obj) static void fmt_dict(struct fmt *fmt, fz_obj *obj) { - int i; + int i, n; fz_obj *key, *val; + fz_context *ctx = fmt->ctx; + n = fz_dict_len(ctx, obj); if (fmt->tight) { fmt_puts(fmt, "<<"); - for (i = 0; i < fz_dict_len(obj); i++) { - fmt_obj(fmt, fz_dict_get_key(obj, i)); + for (i = 0; i < n; i++) { + fmt_obj(fmt, fz_dict_get_key(ctx, obj, i)); fmt_sep(fmt); - fmt_obj(fmt, fz_dict_get_val(obj, i)); + fmt_obj(fmt, fz_dict_get_val(ctx, obj, i)); fmt_sep(fmt); } fmt_puts(fmt, ">>"); @@ -199,17 +204,17 @@ static void fmt_dict(struct fmt *fmt, fz_obj *obj) else { fmt_puts(fmt, "<<\n"); fmt->indent ++; - for (i = 0; i < fz_dict_len(obj); i++) { - key = fz_dict_get_key(obj, i); - val = fz_dict_get_val(obj, i); + for (i = 0; i < n; i++) { + key = fz_dict_get_key(ctx, obj, i); + val = fz_dict_get_val(ctx, obj, i); fmt_indent(fmt); fmt_obj(fmt, key); fmt_putc(fmt, ' '); - if (!fz_is_indirect(val) && fz_is_array(val)) + if (!fz_is_indirect(val) && fz_is_array(ctx, val)) fmt->indent ++; fmt_obj(fmt, val); fmt_putc(fmt, '\n'); - if (!fz_is_indirect(val) && fz_is_array(val)) + if (!fz_is_indirect(val) && fz_is_array(ctx, val)) fmt->indent --; } fmt->indent --; @@ -221,6 +226,7 @@ static void fmt_dict(struct fmt *fmt, fz_obj *obj) static void fmt_obj(struct fmt *fmt, fz_obj *obj) { char buf[256]; + fz_context *ctx = fmt->ctx; if (!obj) fmt_puts(fmt, "<NULL>"); @@ -229,26 +235,26 @@ static void fmt_obj(struct fmt *fmt, fz_obj *obj) sprintf(buf, "%d %d R", fz_to_num(obj), fz_to_gen(obj)); fmt_puts(fmt, buf); } - else if (fz_is_null(obj)) + else if (fz_is_null(ctx, obj)) fmt_puts(fmt, "null"); - else if (fz_is_bool(obj)) - fmt_puts(fmt, fz_to_bool(obj) ? "true" : "false"); - else if (fz_is_int(obj)) + else if (fz_is_bool(ctx, obj)) + fmt_puts(fmt, fz_to_bool(ctx, obj) ? "true" : "false"); + else if (fz_is_int(ctx, obj)) { - sprintf(buf, "%d", fz_to_int(obj)); + sprintf(buf, "%d", fz_to_int(ctx, obj)); fmt_puts(fmt, buf); } - else if (fz_is_real(obj)) + else if (fz_is_real(ctx, obj)) { - sprintf(buf, "%g", fz_to_real(obj)); + sprintf(buf, "%g", fz_to_real(ctx, obj)); if (strchr(buf, 'e')) /* bad news! */ - sprintf(buf, fabsf(fz_to_real(obj)) > 1 ? "%1.1f" : "%1.8f", fz_to_real(obj)); + sprintf(buf, fabsf(fz_to_real(ctx, obj)) > 1 ? "%1.1f" : "%1.8f", fz_to_real(ctx, obj)); fmt_puts(fmt, buf); } - else if (fz_is_string(obj)) + else if (fz_is_string(ctx, obj)) { - char *str = fz_to_str_buf(obj); - int len = fz_to_str_len(obj); + char *str = fz_to_str_buf(ctx, obj); + int len = fz_to_str_len(ctx, obj); int added = 0; int i, c; for (i = 0; i < len; i++) { @@ -263,18 +269,18 @@ static void fmt_obj(struct fmt *fmt, fz_obj *obj) else fmt_hex(fmt, obj); } - else if (fz_is_name(obj)) + else if (fz_is_name(ctx, obj)) fmt_name(fmt, obj); - else if (fz_is_array(obj)) + else if (fz_is_array(ctx, obj)) fmt_array(fmt, obj); - else if (fz_is_dict(obj)) + else if (fz_is_dict(ctx, obj)) fmt_dict(fmt, obj); else fmt_puts(fmt, "<unknown object>"); } static int -fz_sprint_obj(char *s, int n, fz_obj *obj, int tight) +fz_sprint_obj(fz_context *ctx, char *s, int n, fz_obj *obj, int tight) { struct fmt fmt; @@ -287,6 +293,7 @@ fz_sprint_obj(char *s, int n, fz_obj *obj, int tight) fmt.buf = s; fmt.cap = n; fmt.len = 0; + fmt.ctx = ctx; fmt_obj(&fmt, obj); if (fmt.buf && fmt.len < fmt.cap) @@ -296,40 +303,40 @@ fz_sprint_obj(char *s, int n, fz_obj *obj, int tight) } int -fz_fprint_obj(FILE *fp, fz_obj *obj, int tight) +fz_fprint_obj(fz_context *ctx, FILE *fp, fz_obj *obj, int tight) { char buf[1024]; char *ptr; int n; - n = fz_sprint_obj(NULL, 0, obj, tight); + n = fz_sprint_obj(ctx, NULL, 0, obj, tight); if ((n + 1) < sizeof buf) { - fz_sprint_obj(buf, sizeof buf, obj, tight); + fz_sprint_obj(ctx, buf, sizeof buf, obj, tight); fputs(buf, fp); fputc('\n', fp); } else { - ptr = fz_malloc(n + 1); - fz_sprint_obj(ptr, n + 1, obj, tight); + ptr = fz_malloc(ctx, n + 1); + fz_sprint_obj(ctx, ptr, n + 1, obj, tight); fputs(ptr, fp); fputc('\n', fp); - fz_free(ptr); + fz_free(ctx, ptr); } return n; } void -fz_debug_obj(fz_obj *obj) +fz_debug_obj(fz_context *ctx, fz_obj *obj) { - fz_fprint_obj(stdout, obj, 0); + fz_fprint_obj(ctx, stdout, obj, 0); } void -fz_debug_ref(fz_obj *ref) +fz_debug_ref(fz_context *ctx, fz_obj *ref) { fz_obj *obj; - obj = fz_resolve_indirect(ref); - fz_debug_obj(obj); + obj = fz_resolve_indirect(ctx, ref); + fz_debug_obj(ctx, obj); } diff --git a/fitz/res_bitmap.c b/fitz/res_bitmap.c index a50d0952..2ca66efe 100644 --- a/fitz/res_bitmap.c +++ b/fitz/res_bitmap.c @@ -1,11 +1,11 @@ #include "fitz.h" fz_bitmap * -fz_new_bitmap(int w, int h, int n) +fz_new_bitmap(fz_context *ctx, int w, int h, int n) { fz_bitmap *bit; - bit = fz_malloc(sizeof(fz_bitmap)); + bit = fz_malloc(ctx, sizeof(fz_bitmap)); bit->refs = 1; bit->w = w; bit->h = h; @@ -14,7 +14,7 @@ fz_new_bitmap(int w, int h, int n) * use SSE2 etc. */ bit->stride = ((n * w + 31) & ~31) >> 3; - bit->samples = fz_calloc(h, bit->stride); + bit->samples = fz_calloc(ctx, h, bit->stride); return bit; } @@ -27,12 +27,12 @@ fz_keep_bitmap(fz_bitmap *pix) } void -fz_drop_bitmap(fz_bitmap *bit) +fz_drop_bitmap(fz_context *ctx, fz_bitmap *bit) { if (bit && --bit->refs == 0) { - fz_free(bit->samples); - fz_free(bit); + fz_free(ctx, bit->samples); + fz_free(ctx, bit); } } diff --git a/fitz/res_colorspace.c b/fitz/res_colorspace.c index 47df2b34..3f5cd47e 100644 --- a/fitz/res_colorspace.c +++ b/fitz/res_colorspace.c @@ -3,9 +3,9 @@ #define SLOWCMYK fz_colorspace * -fz_new_colorspace(char *name, int n) +fz_new_colorspace(fz_context *ctx, char *name, int n) { - fz_colorspace *cs = fz_malloc(sizeof(fz_colorspace)); + fz_colorspace *cs = fz_malloc(ctx, sizeof(fz_colorspace)); cs->refs = 1; fz_strlcpy(cs->name, name, sizeof cs->name); cs->n = n; @@ -26,15 +26,15 @@ fz_keep_colorspace(fz_colorspace *cs) } void -fz_drop_colorspace(fz_colorspace *cs) +fz_drop_colorspace(fz_context *ctx, fz_colorspace *cs) { if (cs && cs->refs < 0) return; if (cs && --cs->refs == 0) { if (cs->free_data && cs->data) - cs->free_data(cs); - fz_free(cs); + cs->free_data(ctx, cs); + fz_free(ctx, cs); } } @@ -372,7 +372,7 @@ static void fast_rgb_to_bgr(fz_pixmap *src, fz_pixmap *dst) } static void -fz_std_conv_pixmap(fz_pixmap *src, fz_pixmap *dst) +fz_std_conv_pixmap(fz_context *ctx, fz_pixmap *src, fz_pixmap *dst) { float srcv[FZ_MAX_COLORS]; float dstv[FZ_MAX_COLORS]; @@ -464,7 +464,7 @@ fz_std_conv_pixmap(fz_pixmap *src, fz_pixmap *dst) fz_hash_table *lookup; unsigned char *color; - lookup = fz_new_hash_table(509, srcn); + lookup = fz_new_hash_table(ctx, 509, srcn); for (y = 0; y < src->h; y++) { @@ -486,19 +486,19 @@ fz_std_conv_pixmap(fz_pixmap *src, fz_pixmap *dst) for (k = 0; k < dstn; k++) *d++ = dstv[k] * 255; - fz_hash_insert(lookup, s - srcn, d - dstn); + fz_hash_insert(ctx, lookup, s - srcn, d - dstn); *d++ = *s++; } } } - fz_free_hash(lookup); + fz_free_hash(ctx, lookup); } } void -fz_convert_pixmap(fz_pixmap *sp, fz_pixmap *dp) +fz_convert_pixmap(fz_context *ctx, fz_pixmap *sp, fz_pixmap *dp) { fz_colorspace *ss = sp->colorspace; fz_colorspace *ds = dp->colorspace; @@ -514,7 +514,7 @@ fz_convert_pixmap(fz_pixmap *sp, fz_pixmap *dp) if (ds == fz_device_rgb) fast_gray_to_rgb(sp, dp); else if (ds == fz_device_bgr) fast_gray_to_rgb(sp, dp); /* bgr == rgb here */ else if (ds == fz_device_cmyk) fast_gray_to_cmyk(sp, dp); - else fz_std_conv_pixmap(sp, dp); + else fz_std_conv_pixmap(ctx, sp, dp); } else if (ss == fz_device_rgb) @@ -522,7 +522,7 @@ fz_convert_pixmap(fz_pixmap *sp, fz_pixmap *dp) if (ds == fz_device_gray) fast_rgb_to_gray(sp, dp); else if (ds == fz_device_bgr) fast_rgb_to_bgr(sp, dp); else if (ds == fz_device_cmyk) fast_rgb_to_cmyk(sp, dp); - else fz_std_conv_pixmap(sp, dp); + else fz_std_conv_pixmap(ctx, sp, dp); } else if (ss == fz_device_bgr) @@ -530,7 +530,7 @@ fz_convert_pixmap(fz_pixmap *sp, fz_pixmap *dp) if (ds == fz_device_gray) fast_bgr_to_gray(sp, dp); else if (ds == fz_device_rgb) fast_rgb_to_bgr(sp, dp); /* bgr = rgb here */ else if (ds == fz_device_cmyk) fast_bgr_to_cmyk(sp, dp); - else fz_std_conv_pixmap(sp, dp); + else fz_std_conv_pixmap(ctx, sp, dp); } else if (ss == fz_device_cmyk) @@ -538,10 +538,10 @@ fz_convert_pixmap(fz_pixmap *sp, fz_pixmap *dp) if (ds == fz_device_gray) fast_cmyk_to_gray(sp, dp); else if (ds == fz_device_bgr) fast_cmyk_to_bgr(sp, dp); else if (ds == fz_device_rgb) fast_cmyk_to_rgb(sp, dp); - else fz_std_conv_pixmap(sp, dp); + else fz_std_conv_pixmap(ctx, sp, dp); } - else fz_std_conv_pixmap(sp, dp); + else fz_std_conv_pixmap(ctx, sp, dp); } /* Convert a single color */ diff --git a/fitz/res_font.c b/fitz/res_font.c index c78b5995..f3018fcb 100644 --- a/fitz/res_font.c +++ b/fitz/res_font.c @@ -7,11 +7,11 @@ static void fz_finalize_freetype(void); static fz_font * -fz_new_font(char *name) +fz_new_font(fz_context *ctx, char *name) { fz_font *font; - font = fz_malloc(sizeof(fz_font)); + font = fz_malloc(ctx, sizeof(fz_font)); font->refs = 1; if (name) @@ -55,7 +55,7 @@ fz_keep_font(fz_font *font) } void -fz_drop_font(fz_font *font) +fz_drop_font(fz_context *ctx, fz_font *font) { int fterr; int i; @@ -65,12 +65,12 @@ fz_drop_font(fz_font *font) if (font->t3procs) { if (font->t3resources) - fz_drop_obj(font->t3resources); + fz_drop_obj(ctx, font->t3resources); for (i = 0; i < 256; i++) if (font->t3procs[i]) - fz_drop_buffer(font->t3procs[i]); - fz_free(font->t3procs); - fz_free(font->t3widths); + fz_drop_buffer(ctx, font->t3procs[i]); + fz_free(ctx, font->t3procs); + fz_free(ctx, font->t3widths); } if (font->ft_face) @@ -82,14 +82,14 @@ fz_drop_font(fz_font *font) } if (font->ft_file) - fz_free(font->ft_file); + fz_free(ctx, font->ft_file); if (font->ft_data) - fz_free(font->ft_data); + fz_free(ctx, font->ft_data); if (font->width_table) - fz_free(font->width_table); + fz_free(ctx, font->width_table); - fz_free(font); + fz_free(ctx, font); } } @@ -180,7 +180,7 @@ fz_finalize_freetype(void) } fz_error -fz_new_font_from_file(fz_font **fontp, char *path, int index) +fz_new_font_from_file(fz_context *ctx, fz_font **fontp, char *path, int index) { FT_Face face; fz_error error; @@ -195,7 +195,7 @@ fz_new_font_from_file(fz_font **fontp, char *path, int index) if (fterr) return fz_error_make("freetype: cannot load font: %s", ft_error_string(fterr)); - font = fz_new_font(face->family_name); + font = fz_new_font(ctx, face->family_name); font->ft_face = face; font->bbox.x0 = face->bbox.xMin * 1000 / face->units_per_EM; font->bbox.y0 = face->bbox.yMin * 1000 / face->units_per_EM; @@ -207,7 +207,7 @@ fz_new_font_from_file(fz_font **fontp, char *path, int index) } fz_error -fz_new_font_from_memory(fz_font **fontp, unsigned char *data, int len, int index) +fz_new_font_from_memory(fz_context *ctx, fz_font **fontp, unsigned char *data, int len, int index) { FT_Face face; fz_error error; @@ -222,7 +222,7 @@ fz_new_font_from_memory(fz_font **fontp, unsigned char *data, int len, int index if (fterr) return fz_error_make("freetype: cannot load font: %s", ft_error_string(fterr)); - font = fz_new_font(face->family_name); + font = fz_new_font(ctx, face->family_name); font->ft_face = face; font->bbox.x0 = face->bbox.xMin * 1000 / face->units_per_EM; font->bbox.y0 = face->bbox.yMin * 1000 / face->units_per_EM; @@ -268,12 +268,12 @@ fz_adjust_ft_glyph_width(fz_font *font, int gid, fz_matrix trm) } static fz_pixmap * -fz_copy_ft_bitmap(int left, int top, FT_Bitmap *bitmap) +fz_copy_ft_bitmap(fz_context *ctx, int left, int top, FT_Bitmap *bitmap) { fz_pixmap *pixmap; int y; - pixmap = fz_new_pixmap(NULL, bitmap->width, bitmap->rows); + pixmap = fz_new_pixmap(ctx, NULL, bitmap->width, bitmap->rows); pixmap->x = left; pixmap->y = top - bitmap->rows; @@ -311,7 +311,7 @@ fz_copy_ft_bitmap(int left, int top, FT_Bitmap *bitmap) } fz_pixmap * -fz_render_ft_glyph(fz_font *font, int gid, fz_matrix trm) +fz_render_ft_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm) { FT_Face face = font->ft_face; FT_Matrix m; @@ -399,11 +399,11 @@ fz_render_ft_glyph(fz_font *font, int gid, fz_matrix trm) return NULL; } - return fz_copy_ft_bitmap(face->glyph->bitmap_left, face->glyph->bitmap_top, &face->glyph->bitmap); + return fz_copy_ft_bitmap(ctx, face->glyph->bitmap_left, face->glyph->bitmap_top, &face->glyph->bitmap); } fz_pixmap * -fz_render_ft_stroked_glyph(fz_font *font, int gid, fz_matrix trm, fz_matrix ctm, fz_stroke_state *state) +fz_render_ft_stroked_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm, fz_matrix ctm, fz_stroke_state *state) { FT_Face face = font->ft_face; float expansion = fz_matrix_expansion(ctm); @@ -481,7 +481,7 @@ fz_render_ft_stroked_glyph(fz_font *font, int gid, fz_matrix trm, fz_matrix ctm, } bitmap = (FT_BitmapGlyph)glyph; - pixmap = fz_copy_ft_bitmap(bitmap->left, bitmap->top, &bitmap->bitmap); + pixmap = fz_copy_ft_bitmap(ctx, bitmap->left, bitmap->top, &bitmap->bitmap); FT_Done_Glyph(glyph); return pixmap; @@ -492,14 +492,14 @@ fz_render_ft_stroked_glyph(fz_font *font, int gid, fz_matrix trm, fz_matrix ctm, */ fz_font * -fz_new_type3_font(char *name, fz_matrix matrix) +fz_new_type3_font(fz_context *ctx, char *name, fz_matrix matrix) { fz_font *font; int i; - font = fz_new_font(name); - font->t3procs = fz_calloc(256, sizeof(fz_buffer*)); - font->t3widths = fz_calloc(256, sizeof(float)); + font = fz_new_font(ctx, name); + font->t3procs = fz_calloc(ctx, 256, sizeof(fz_buffer*)); + font->t3widths = fz_calloc(ctx, 256, sizeof(float)); font->t3matrix = matrix; for (i = 0; i < 256; i++) @@ -512,7 +512,7 @@ fz_new_type3_font(char *name, fz_matrix matrix) } fz_pixmap * -fz_render_t3_glyph(fz_font *font, int gid, fz_matrix trm, fz_colorspace *model) +fz_render_t3_glyph(fz_context *ctx, fz_font *font, int gid, fz_matrix trm, fz_colorspace *model) { fz_error error; fz_matrix ctm; @@ -531,7 +531,7 @@ fz_render_t3_glyph(fz_font *font, int gid, fz_matrix trm, fz_colorspace *model) return NULL; ctm = fz_concat(font->t3matrix, trm); - dev = fz_new_bbox_device(&bbox); + dev = fz_new_bbox_device(ctx, &bbox); error = font->t3run(font->t3xref, font->t3resources, contents, dev, ctm); if (error) fz_error_handle(error, "cannot draw type3 glyph"); @@ -560,21 +560,21 @@ fz_render_t3_glyph(fz_font *font, int gid, fz_matrix trm, fz_colorspace *model) bbox.x1++; bbox.y1++; - glyph = fz_new_pixmap_with_rect(model ? model : fz_device_gray, bbox); + glyph = fz_new_pixmap_with_rect(ctx, model ? model : fz_device_gray, bbox); fz_clear_pixmap(glyph); - cache = fz_new_glyph_cache(); - dev = fz_new_draw_device_type3(cache, glyph); + cache = fz_new_glyph_cache(ctx); + dev = fz_new_draw_device_type3(ctx, cache, glyph); error = font->t3run(font->t3xref, font->t3resources, contents, dev, ctm); if (error) fz_error_handle(error, "cannot draw type3 glyph"); fz_free_device(dev); - fz_free_glyph_cache(cache); + fz_free_glyph_cache(ctx, cache); if (model == NULL) { - result = fz_alpha_from_gray(glyph, 0); - fz_drop_pixmap(glyph); + result = fz_alpha_from_gray(ctx, glyph, 0); + fz_drop_pixmap(ctx, glyph); } else result = glyph; diff --git a/fitz/res_halftone.c b/fitz/res_halftone.c index e45454ea..ad2ab694 100644 --- a/fitz/res_halftone.c +++ b/fitz/res_halftone.c @@ -1,12 +1,12 @@ #include "fitz.h" fz_halftone * -fz_new_halftone(int comps) +fz_new_halftone(fz_context *ctx, int comps) { fz_halftone *ht; int i; - ht = fz_malloc(sizeof(fz_halftone) + (comps-1)*sizeof(fz_pixmap *)); + ht = fz_malloc(ctx, sizeof(fz_halftone) + (comps-1)*sizeof(fz_pixmap *)); ht->refs = 1; ht->n = comps; for (i = 0; i < comps; i++) @@ -23,15 +23,15 @@ fz_keep_halftone(fz_halftone *ht) } void -fz_drop_halftone(fz_halftone *ht) +fz_drop_halftone(fz_context *ctx, fz_halftone *ht) { int i; if (!ht || --ht->refs != 0) return; for (i = 0; i < ht->n; i++) - fz_drop_pixmap(ht->comp[i]); - fz_free(ht); + fz_drop_pixmap(ctx, ht->comp[i]); + fz_free(ctx, ht); } /* Default mono halftone, lifted from Ghostscript. */ @@ -55,11 +55,11 @@ static unsigned char mono_ht[] = 0xF2, 0x72, 0xD2, 0x52, 0xFA, 0x7A, 0xDA, 0x5A, 0xF0, 0x70, 0xD0, 0x50, 0xF8, 0x78, 0xD8, 0x58 }; -fz_halftone *fz_get_default_halftone(int num_comps) +fz_halftone *fz_get_default_halftone(fz_context *ctx, int num_comps) { - fz_halftone *ht = fz_new_halftone(num_comps); + fz_halftone *ht = fz_new_halftone(ctx, num_comps); assert(num_comps == 1); /* Only support 1 component for now */ - ht->comp[0] = fz_new_pixmap_with_data(NULL, 16, 16, mono_ht); + ht->comp[0] = fz_new_pixmap_with_data(ctx, NULL, 16, 16, mono_ht); return ht; } @@ -156,7 +156,7 @@ static void do_threshold_1(unsigned char *ht_line, unsigned char *pixmap, unsign *out++ = h; } -fz_bitmap *fz_halftone_pixmap(fz_pixmap *pix, fz_halftone *ht) +fz_bitmap *fz_halftone_pixmap(fz_context *ctx, fz_pixmap *pix, fz_halftone *ht) { fz_bitmap *out; unsigned char *ht_line, *o, *p; @@ -168,8 +168,8 @@ fz_bitmap *fz_halftone_pixmap(fz_pixmap *pix, fz_halftone *ht) assert(pix->n == 2); /* Mono + Alpha */ n = pix->n-1; /* Remove alpha */ - ht_line = fz_malloc(pix->w * n); - out = fz_new_bitmap(pix->w, pix->h, n); + ht_line = fz_malloc(ctx, pix->w * n); + out = fz_new_bitmap(ctx, pix->w, pix->h, n); o = out->samples; p = pix->samples; diff --git a/fitz/res_path.c b/fitz/res_path.c index 328e5ac1..1cc92937 100644 --- a/fitz/res_path.c +++ b/fitz/res_path.c @@ -1,11 +1,12 @@ +#include <assert.h> #include "fitz.h" fz_path * -fz_new_path(void) +fz_new_path(fz_context *ctx) { fz_path *path; - path = fz_malloc(sizeof(fz_path)); + path = fz_malloc(ctx, sizeof(fz_path)); path->len = 0; path->cap = 0; path->items = NULL; @@ -14,61 +15,62 @@ fz_new_path(void) } fz_path * -fz_clone_path(fz_path *old) +fz_clone_path(fz_context *ctx, fz_path *old) { fz_path *path; - path = fz_malloc(sizeof(fz_path)); + assert(old != NULL); + path = fz_malloc(ctx, sizeof(fz_path)); path->len = old->len; path->cap = old->len; - path->items = fz_calloc(path->cap, sizeof(fz_path_item)); + path->items = fz_calloc(ctx, path->cap, sizeof(fz_path_item)); memcpy(path->items, old->items, sizeof(fz_path_item) * path->len); return path; } void -fz_free_path(fz_path *path) +fz_free_path(fz_context *ctx, fz_path *path) { - fz_free(path->items); - fz_free(path); + fz_free(ctx, path->items); + fz_free(ctx, path); } static void -grow_path(fz_path *path, int n) +grow_path(fz_context *ctx, fz_path *path, int n) { if (path->len + n < path->cap) return; while (path->len + n > path->cap) path->cap = path->cap + 36; - path->items = fz_realloc(path->items, path->cap, sizeof(fz_path_item)); + path->items = fz_realloc(ctx, path->items, path->cap * sizeof(fz_path_item)); } void -fz_moveto(fz_path *path, float x, float y) +fz_moveto(fz_context *ctx, fz_path *path, float x, float y) { - grow_path(path, 3); + grow_path(ctx, path, 3); path->items[path->len++].k = FZ_MOVETO; path->items[path->len++].v = x; path->items[path->len++].v = y; } void -fz_lineto(fz_path *path, float x, float y) +fz_lineto(fz_context *ctx, fz_path *path, float x, float y) { if (path->len == 0) { fz_warn("lineto with no current point"); return; } - grow_path(path, 3); + grow_path(ctx, path, 3); path->items[path->len++].k = FZ_LINETO; path->items[path->len++].v = x; path->items[path->len++].v = y; } void -fz_curveto(fz_path *path, +fz_curveto(fz_context *ctx, fz_path *path, float x1, float y1, float x2, float y2, float x3, float y3) @@ -78,7 +80,7 @@ fz_curveto(fz_path *path, fz_warn("curveto with no current point"); return; } - grow_path(path, 7); + grow_path(ctx, path, 7); path->items[path->len++].k = FZ_CURVETO; path->items[path->len++].v = x1; path->items[path->len++].v = y1; @@ -89,7 +91,7 @@ fz_curveto(fz_path *path, } void -fz_curvetov(fz_path *path, float x2, float y2, float x3, float y3) +fz_curvetov(fz_context *ctx, fz_path *path, float x2, float y2, float x3, float y3) { float x1, y1; if (path->len == 0) @@ -99,24 +101,24 @@ fz_curvetov(fz_path *path, float x2, float y2, float x3, float y3) } x1 = path->items[path->len-2].v; y1 = path->items[path->len-1].v; - fz_curveto(path, x1, y1, x2, y2, x3, y3); + fz_curveto(ctx, path, x1, y1, x2, y2, x3, y3); } void -fz_curvetoy(fz_path *path, float x1, float y1, float x3, float y3) +fz_curvetoy(fz_context *ctx, fz_path *path, float x1, float y1, float x3, float y3) { - fz_curveto(path, x1, y1, x3, y3, x3, y3); + fz_curveto(ctx, path, x1, y1, x3, y3, x3, y3); } void -fz_closepath(fz_path *path) +fz_closepath(fz_context *ctx, fz_path *path) { if (path->len == 0) { fz_warn("closepath with no current point"); return; } - grow_path(path, 1); + grow_path(ctx, path, 1); path->items[path->len++].k = FZ_CLOSE_PATH; } diff --git a/fitz/res_pixmap.c b/fitz/res_pixmap.c index dccc62e3..3706d495 100644 --- a/fitz/res_pixmap.c +++ b/fitz/res_pixmap.c @@ -4,11 +4,11 @@ static int fz_memory_limit = 256 << 20; static int fz_memory_used = 0; fz_pixmap * -fz_new_pixmap_with_data(fz_colorspace *colorspace, int w, int h, unsigned char *samples) +fz_new_pixmap_with_data(fz_context *ctx, fz_colorspace *colorspace, int w, int h, unsigned char *samples) { fz_pixmap *pix; - pix = fz_malloc(sizeof(fz_pixmap)); + pix = fz_malloc(ctx, sizeof(fz_pixmap)); pix->refs = 1; pix->x = 0; pix->y = 0; @@ -35,7 +35,7 @@ fz_new_pixmap_with_data(fz_colorspace *colorspace, int w, int h, unsigned char * else { fz_memory_used += pix->w * pix->h * pix->n; - pix->samples = fz_calloc(pix->h, pix->w * pix->n); + pix->samples = fz_calloc(ctx, pix->h, pix->w * pix->n); pix->free_samples = 1; } @@ -43,7 +43,7 @@ fz_new_pixmap_with_data(fz_colorspace *colorspace, int w, int h, unsigned char * } fz_pixmap * -fz_new_pixmap_with_limit(fz_colorspace *colorspace, int w, int h) +fz_new_pixmap_with_limit(fz_context *ctx, fz_colorspace *colorspace, int w, int h) { int n = colorspace ? colorspace->n + 1 : 1; int size = w * h * n; @@ -53,30 +53,30 @@ fz_new_pixmap_with_limit(fz_colorspace *colorspace, int w, int h) fz_memory_used/(1<<20), size/(1<<20), fz_memory_limit/(1<<20)); return NULL; } - return fz_new_pixmap_with_data(colorspace, w, h, NULL); + return fz_new_pixmap_with_data(ctx, colorspace, w, h, NULL); } fz_pixmap * -fz_new_pixmap(fz_colorspace *colorspace, int w, int h) +fz_new_pixmap(fz_context *ctx, fz_colorspace *colorspace, int w, int h) { - return fz_new_pixmap_with_data(colorspace, w, h, NULL); + return fz_new_pixmap_with_data(ctx, colorspace, w, h, NULL); } fz_pixmap * -fz_new_pixmap_with_rect(fz_colorspace *colorspace, fz_bbox r) +fz_new_pixmap_with_rect(fz_context *ctx, fz_colorspace *colorspace, fz_bbox r) { fz_pixmap *pixmap; - pixmap = fz_new_pixmap(colorspace, r.x1 - r.x0, r.y1 - r.y0); + pixmap = fz_new_pixmap(ctx, colorspace, r.x1 - r.x0, r.y1 - r.y0); pixmap->x = r.x0; pixmap->y = r.y0; return pixmap; } fz_pixmap * -fz_new_pixmap_with_rect_and_data(fz_colorspace *colorspace, fz_bbox r, unsigned char *samples) +fz_new_pixmap_with_rect_and_data(fz_context *ctx, fz_colorspace *colorspace, fz_bbox r, unsigned char *samples) { fz_pixmap *pixmap; - pixmap = fz_new_pixmap_with_data(colorspace, r.x1 - r.x0, r.y1 - r.y0, samples); + pixmap = fz_new_pixmap_with_data(ctx, colorspace, r.x1 - r.x0, r.y1 - r.y0, samples); pixmap->x = r.x0; pixmap->y = r.y0; return pixmap; @@ -90,18 +90,18 @@ fz_keep_pixmap(fz_pixmap *pix) } void -fz_drop_pixmap(fz_pixmap *pix) +fz_drop_pixmap(fz_context *ctx, fz_pixmap *pix) { if (pix && --pix->refs == 0) { fz_memory_used -= pix->w * pix->h * pix->n; if (pix->mask) - fz_drop_pixmap(pix->mask); + fz_drop_pixmap(ctx, pix->mask); if (pix->colorspace) - fz_drop_colorspace(pix->colorspace); + fz_drop_colorspace(ctx, pix->colorspace); if (pix->free_samples) - fz_free(pix->samples); - fz_free(pix); + fz_free(ctx, pix->samples); + fz_free(ctx, pix); } } @@ -227,7 +227,7 @@ fz_premultiply_pixmap(fz_pixmap *pix) } fz_pixmap * -fz_alpha_from_gray(fz_pixmap *gray, int luminosity) +fz_alpha_from_gray(fz_context *ctx, fz_pixmap *gray, int luminosity) { fz_pixmap *alpha; unsigned char *sp, *dp; @@ -235,7 +235,7 @@ fz_alpha_from_gray(fz_pixmap *gray, int luminosity) assert(gray->n == 2); - alpha = fz_new_pixmap_with_rect(NULL, fz_bound_pixmap(gray)); + alpha = fz_new_pixmap_with_rect(ctx, NULL, fz_bound_pixmap(gray)); dp = alpha->samples; sp = gray->samples; if (!luminosity) @@ -431,7 +431,7 @@ static void putchunk(char *tag, unsigned char *data, int size, FILE *fp) } fz_error -fz_write_png(fz_pixmap *pixmap, char *filename, int savealpha) +fz_write_png(fz_context *ctx, fz_pixmap *pixmap, char *filename, int savealpha) { static const unsigned char pngsig[8] = { 137, 80, 78, 71, 13, 10, 26, 10 }; FILE *fp; @@ -461,8 +461,8 @@ fz_write_png(fz_pixmap *pixmap, char *filename, int savealpha) usize = (pixmap->w * dn + 1) * pixmap->h; csize = compressBound(usize); - udata = fz_malloc(usize); - cdata = fz_malloc(csize); + udata = fz_malloc(ctx, usize); + cdata = fz_malloc(ctx, csize); sp = pixmap->samples; dp = udata; @@ -486,16 +486,16 @@ fz_write_png(fz_pixmap *pixmap, char *filename, int savealpha) err = compress(cdata, &csize, udata, usize); if (err != Z_OK) { - fz_free(udata); - fz_free(cdata); + fz_free(ctx, udata); + fz_free(ctx, cdata); return fz_error_make("cannot compress image data"); } fp = fopen(filename, "wb"); if (!fp) { - fz_free(udata); - fz_free(cdata); + fz_free(ctx, udata); + fz_free(ctx, cdata); return fz_error_make("cannot open file '%s': %s", filename, strerror(errno)); } @@ -513,7 +513,7 @@ fz_write_png(fz_pixmap *pixmap, char *filename, int savealpha) putchunk("IEND", head, 0, fp); fclose(fp); - fz_free(udata); - fz_free(cdata); + fz_free(ctx, udata); + fz_free(ctx, cdata); return fz_okay; } diff --git a/fitz/res_shade.c b/fitz/res_shade.c index 7faff3bf..b7617161 100644 --- a/fitz/res_shade.c +++ b/fitz/res_shade.c @@ -8,14 +8,14 @@ fz_keep_shade(fz_shade *shade) } void -fz_drop_shade(fz_shade *shade) +fz_drop_shade(fz_context *ctx, fz_shade *shade) { if (shade && --shade->refs == 0) { if (shade->colorspace) - fz_drop_colorspace(shade->colorspace); - fz_free(shade->mesh); - fz_free(shade); + fz_drop_colorspace(ctx, shade->colorspace); + fz_free(ctx, shade->mesh); + fz_free(ctx, shade); } } diff --git a/fitz/res_text.c b/fitz/res_text.c index d38637f1..1c5bbad0 100644 --- a/fitz/res_text.c +++ b/fitz/res_text.c @@ -1,11 +1,11 @@ #include "fitz.h" fz_text * -fz_new_text(fz_font *font, fz_matrix trm, int wmode) +fz_new_text(fz_context *ctx, fz_font *font, fz_matrix trm, int wmode) { fz_text *text; - text = fz_malloc(sizeof(fz_text)); + text = fz_malloc(ctx, sizeof(fz_text)); text->font = fz_keep_font(font); text->trm = trm; text->wmode = wmode; @@ -17,25 +17,25 @@ fz_new_text(fz_font *font, fz_matrix trm, int wmode) } void -fz_free_text(fz_text *text) +fz_free_text(fz_context *ctx, fz_text *text) { - fz_drop_font(text->font); - fz_free(text->items); - fz_free(text); + fz_drop_font(ctx, text->font); + fz_free(ctx, text->items); + fz_free(ctx, text); } fz_text * -fz_clone_text(fz_text *old) +fz_clone_text(fz_context *ctx, fz_text *old) { fz_text *text; - text = fz_malloc(sizeof(fz_text)); + text = fz_malloc(ctx, sizeof(fz_text)); text->font = fz_keep_font(old->font); text->trm = old->trm; text->wmode = old->wmode; text->len = old->len; text->cap = text->len; - text->items = fz_calloc(text->len, sizeof(fz_text_item)); + text->items = fz_calloc(ctx, text->len, sizeof(fz_text_item)); memcpy(text->items, old->items, text->len * sizeof(fz_text_item)); return text; @@ -91,19 +91,19 @@ fz_bound_text(fz_text *text, fz_matrix ctm) } static void -fz_grow_text(fz_text *text, int n) +fz_grow_text(fz_context *ctx, fz_text *text, int n) { if (text->len + n < text->cap) return; while (text->len + n > text->cap) text->cap = text->cap + 36; - text->items = fz_realloc(text->items, text->cap, sizeof(fz_text_item)); + text->items = fz_realloc(ctx, text->items, text->cap * sizeof(fz_text_item)); } void -fz_add_text(fz_text *text, int gid, int ucs, float x, float y) +fz_add_text(fz_context *ctx, fz_text *text, int gid, int ucs, float x, float y) { - fz_grow_text(text, 1); + fz_grow_text(ctx, text, 1); text->items[text->len].ucs = ucs; text->items[text->len].gid = gid; text->items[text->len].x = x; diff --git a/fitz/stm_buffer.c b/fitz/stm_buffer.c index 4c7410c3..edd5c9c9 100644 --- a/fitz/stm_buffer.c +++ b/fitz/stm_buffer.c @@ -1,15 +1,15 @@ #include "fitz.h" fz_buffer * -fz_new_buffer(int size) +fz_new_buffer(fz_context *ctx, int size) { fz_buffer *b; size = size > 1 ? size : 16; - b = fz_malloc(sizeof(fz_buffer)); + b = fz_malloc(ctx, sizeof(fz_buffer)); b->refs = 1; - b->data = fz_malloc(size); + b->data = fz_malloc(ctx, size); b->cap = size; b->len = 0; @@ -24,26 +24,26 @@ fz_keep_buffer(fz_buffer *buf) } void -fz_drop_buffer(fz_buffer *buf) +fz_drop_buffer(fz_context *ctx, fz_buffer *buf) { if (--buf->refs == 0) { - fz_free(buf->data); - fz_free(buf); + fz_free(ctx, buf->data); + fz_free(ctx, buf); } } void -fz_resize_buffer(fz_buffer *buf, int size) +fz_resize_buffer(fz_context *ctx, fz_buffer *buf, int size) { - buf->data = fz_realloc(buf->data, size, 1); + buf->data = fz_realloc(ctx, buf->data, size); buf->cap = size; if (buf->len > buf->cap) buf->len = buf->cap; } void -fz_grow_buffer(fz_buffer *buf) +fz_grow_buffer(fz_context *ctx, fz_buffer *buf) { - fz_resize_buffer(buf, (buf->cap * 3) / 2); + fz_resize_buffer(ctx, buf, (buf->cap * 3) / 2); } diff --git a/fitz/stm_open.c b/fitz/stm_open.c index 81eb819e..f06bdc50 100644 --- a/fitz/stm_open.c +++ b/fitz/stm_open.c @@ -1,13 +1,13 @@ #include "fitz.h" fz_stream * -fz_new_stream(void *state, +fz_new_stream(fz_context *ctx, void *state, int(*read)(fz_stream *stm, unsigned char *buf, int len), void(*close)(fz_stream *stm)) { fz_stream *stm; - stm = fz_malloc(sizeof(fz_stream)); + stm = fz_malloc(ctx, sizeof(fz_stream)); stm->refs = 1; stm->error = 0; @@ -26,6 +26,7 @@ fz_new_stream(void *state, stm->read = read; stm->close = close; stm->seek = NULL; + stm->ctx = ctx; return stm; } @@ -45,7 +46,7 @@ fz_close(fz_stream *stm) { if (stm->close) stm->close(stm); - fz_free(stm); + fz_free(stm->ctx, stm); } } @@ -74,41 +75,41 @@ static void close_file(fz_stream *stm) int n = close(*(int*)stm->state); if (n < 0) fz_warn("close error: %s", strerror(errno)); - fz_free(stm->state); + fz_free(stm->ctx, stm->state); } fz_stream * -fz_open_fd(int fd) +fz_open_fd(fz_context *ctx, int fd) { fz_stream *stm; int *state; - state = fz_malloc(sizeof(int)); + state = fz_malloc(ctx, sizeof(int)); *state = fd; - stm = fz_new_stream(state, read_file, close_file); + stm = fz_new_stream(ctx, state, read_file, close_file); stm->seek = seek_file; return stm; } fz_stream * -fz_open_file(const char *name) +fz_open_file(fz_context *ctx, const char *name) { int fd = open(name, O_BINARY | O_RDONLY, 0); if (fd == -1) return NULL; - return fz_open_fd(fd); + return fz_open_fd(ctx, fd); } #ifdef _WIN32 fz_stream * -fz_open_file_w(const wchar_t *name) +fz_open_file_w(fz_context *ctx, const wchar_t *name) { int fd = _wopen(name, O_BINARY | O_RDONLY, 0); if (fd == -1) return NULL; - return fz_open_fd(fd); + return fz_open_fd(ctx, fd); } #endif @@ -134,15 +135,15 @@ static void seek_buffer(fz_stream *stm, int offset, int whence) static void close_buffer(fz_stream *stm) { if (stm->state) - fz_drop_buffer(stm->state); + fz_drop_buffer(stm->ctx, stm->state); } fz_stream * -fz_open_buffer(fz_buffer *buf) +fz_open_buffer(fz_context *ctx, fz_buffer *buf) { fz_stream *stm; - stm = fz_new_stream(fz_keep_buffer(buf), read_buffer, close_buffer); + stm = fz_new_stream(ctx, fz_keep_buffer(buf), read_buffer, close_buffer); stm->seek = seek_buffer; stm->bp = buf->data; @@ -156,11 +157,11 @@ fz_open_buffer(fz_buffer *buf) } fz_stream * -fz_open_memory(unsigned char *data, int len) +fz_open_memory(fz_context *ctx, unsigned char *data, int len) { fz_stream *stm; - stm = fz_new_stream(NULL, read_buffer, close_buffer); + stm = fz_new_stream(ctx, NULL, read_buffer, close_buffer); stm->seek = seek_buffer; stm->bp = data; diff --git a/fitz/stm_read.c b/fitz/stm_read.c index 89811333..062916c5 100644 --- a/fitz/stm_read.c +++ b/fitz/stm_read.c @@ -99,27 +99,28 @@ fz_read_all(fz_buffer **bufp, fz_stream *stm, int initial) { fz_buffer *buf; int n; + fz_context *ctx = stm->ctx; if (initial < 1024) initial = 1024; - buf = fz_new_buffer(initial); + buf = fz_new_buffer(ctx, initial); while (1) { if (buf->len == buf->cap) - fz_grow_buffer(buf); + fz_grow_buffer(ctx, buf); if (buf->len / 200 > initial) { - fz_drop_buffer(buf); + fz_drop_buffer(ctx, buf); return fz_error_make("compression bomb detected"); } n = fz_read(stm, buf->data + buf->len, buf->cap - buf->len); if (n < 0) { - fz_drop_buffer(buf); + fz_drop_buffer(ctx, buf); return fz_error_note(n, "read error"); } if (n == 0) |