diff options
43 files changed, 520 insertions, 218 deletions
diff --git a/apps/pdfclean.c b/apps/pdfclean.c index 302970bc..a1e70a1a 100644 --- a/apps/pdfclean.c +++ b/apps/pdfclean.c @@ -251,7 +251,7 @@ static void renumberobjs(void) /* Create new table for the reordered, compacted xref */ oldxref = xref->table; - xref->table = fz_calloc(xref->ctx, xref->len, sizeof(pdf_xref_entry)); + xref->table = fz_malloc_array(xref->ctx, xref->len, sizeof(pdf_xref_entry)); xref->table[0] = oldxref[0]; /* Move used objects into the new compacted xref */ @@ -720,7 +720,7 @@ int main(int argc, char **argv) if (argc - fz_optind > 0) subset = 1; - ctx = fz_new_context(&fz_alloc_default); + ctx = fz_new_context(); if (ctx == NULL) die(fz_error_note(1, "failed to initialise context")); @@ -735,10 +735,10 @@ int main(int argc, char **argv) fprintf(out, "%%PDF-%d.%d\n", xref->version / 10, xref->version % 10); fprintf(out, "%%\316\274\341\277\246\n\n"); - uselist = fz_calloc(ctx, xref->len + 1, sizeof(char)); - ofslist = fz_calloc(ctx, xref->len + 1, sizeof(int)); - genlist = fz_calloc(ctx, xref->len + 1, sizeof(int)); - renumbermap = fz_calloc(ctx, xref->len + 1, sizeof(int)); + uselist = fz_malloc_array(ctx, xref->len + 1, sizeof(char)); + ofslist = fz_malloc_array(ctx, xref->len + 1, sizeof(int)); + genlist = fz_malloc_array(ctx, xref->len + 1, sizeof(int)); + renumbermap = fz_malloc_array(ctx, xref->len + 1, sizeof(int)); for (num = 0; num < xref->len; num++) { diff --git a/apps/pdfdraw.c b/apps/pdfdraw.c index 2c2e476b..a088a938 100644 --- a/apps/pdfdraw.c +++ b/apps/pdfdraw.c @@ -343,7 +343,7 @@ int main(int argc, char **argv) if (accelerate) fz_accelerate(); - ctx = fz_new_context(&fz_alloc_default); + ctx = fz_new_context(); if (ctx == NULL) { fprintf(stderr, "Failed to init context\n"); diff --git a/apps/pdfextract.c b/apps/pdfextract.c index 490a8c1e..f59f7df8 100644 --- a/apps/pdfextract.c +++ b/apps/pdfextract.c @@ -198,7 +198,7 @@ int main(int argc, char **argv) infile = argv[fz_optind++]; - ctx = fz_new_context(&fz_alloc_default); + ctx = fz_new_context(); if (ctx == NULL) die(fz_error_note(1, "failed to initialise context")); diff --git a/apps/pdfinfo.c b/apps/pdfinfo.c index cf81105c..3e403b08 100644 --- a/apps/pdfinfo.c +++ b/apps/pdfinfo.c @@ -224,7 +224,7 @@ gatherdimensions(int page, fz_obj *pageref, fz_obj *pageobj) dims++; - dim = fz_realloc(ctx, dim, dims * sizeof(struct info)); + dim = fz_resize_array(ctx, dim, dims, sizeof(struct info)); dim[dims - 1].page = page; dim[dims - 1].pageref = pageref; dim[dims - 1].pageobj = pageobj; @@ -269,7 +269,7 @@ gatherfonts(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict) fonts++; - font = fz_realloc(ctx, font, fonts * sizeof(struct info)); + font = fz_resize_array(ctx, font, fonts, sizeof(struct info)); font[fonts - 1].page = page; font[fonts - 1].pageref = pageref; font[fonts - 1].pageobj = pageobj; @@ -338,7 +338,7 @@ gatherimages(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict) images++; - image = fz_realloc(ctx, image, images * sizeof(struct info)); + image = fz_resize_array(ctx, image, images, sizeof(struct info)); image[images - 1].page = page; image[images - 1].pageref = pageref; image[images - 1].pageobj = pageobj; @@ -396,7 +396,7 @@ gatherforms(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict) forms++; - form = fz_realloc(ctx, form, forms * sizeof(struct info)); + form = fz_resize_array(ctx, form, forms, sizeof(struct info)); form[forms - 1].page = page; form[forms - 1].pageref = pageref; form[forms - 1].pageobj = pageobj; @@ -441,7 +441,7 @@ gatherpsobjs(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict) psobjs++; - psobj = fz_realloc(ctx, psobj, psobjs * sizeof(struct info)); + psobj = fz_resize_array(ctx, psobj, psobjs, sizeof(struct info)); psobj[psobjs - 1].page = page; psobj[psobjs - 1].pageref = pageref; psobj[psobjs - 1].pageobj = pageobj; @@ -484,7 +484,7 @@ gathershadings(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict) shadings++; - shading = fz_realloc(ctx, shading, shadings * sizeof(struct info)); + shading = fz_resize_array(ctx, shading, shadings, sizeof(struct info)); shading[shadings - 1].page = page; shading[shadings - 1].pageref = pageref; shading[shadings - 1].pageobj = pageobj; @@ -552,7 +552,7 @@ gatherpatterns(int page, fz_obj *pageref, fz_obj *pageobj, fz_obj *dict) patterns++; - pattern = fz_realloc(ctx, pattern, patterns * sizeof(struct info)); + pattern = fz_resize_array(ctx, pattern, patterns, sizeof(struct info)); pattern[patterns - 1].page = page; pattern[patterns - 1].pageref = pageref; pattern[patterns - 1].pageobj = pageobj; @@ -981,7 +981,7 @@ int main(int argc, char **argv) if (fz_optind == argc) infousage(); - ctx = fz_new_context(&fz_alloc_default); + ctx = fz_new_context(); if (ctx == NULL) die(fz_error_make("failed to initialise context")); diff --git a/apps/pdfshow.c b/apps/pdfshow.c index 0fd49010..fc391b58 100644 --- a/apps/pdfshow.c +++ b/apps/pdfshow.c @@ -214,7 +214,7 @@ int main(int argc, char **argv) filename = argv[fz_optind++]; - ctx = fz_new_context(&fz_alloc_default); + ctx = fz_new_context(); if (ctx == NULL) die(fz_error_note(1, "failed to initialise context")); diff --git a/apps/x11_main.c b/apps/x11_main.c index a8c92074..d5b7dc55 100644 --- a/apps/x11_main.c +++ b/apps/x11_main.c @@ -584,7 +584,7 @@ int main(int argc, char **argv) if (accelerate) fz_accelerate(); - ctx = fz_new_context(&fz_alloc_default); + ctx = fz_new_context(); if (ctx == NULL) { fprintf(stderr, "failed to initialise context"); diff --git a/apps/xpsdraw.c b/apps/xpsdraw.c index 719719ea..2ec54322 100644 --- a/apps/xpsdraw.c +++ b/apps/xpsdraw.c @@ -311,7 +311,7 @@ int main(int argc, char **argv) if (accelerate) fz_accelerate(); - fzctx = fz_new_context(&fz_alloc_default); + fzctx = fz_new_context(); if (fzctx == NULL) { fprintf(stderr, "failed to initialise context"); diff --git a/draw/draw_edge.c b/draw/draw_edge.c index ee218bc9..d9372cfb 100644 --- a/draw/draw_edge.c +++ b/draw/draw_edge.c @@ -139,7 +139,7 @@ fz_new_gel(fz_context *ctx) gel->ctx = ctx; gel->cap = 512; gel->len = 0; - gel->edges = fz_calloc(ctx, gel->cap, sizeof(fz_edge)); + gel->edges = fz_malloc_array(ctx, gel->cap, sizeof(fz_edge)); gel->clip.x0 = gel->clip.y0 = BBOX_MAX; gel->clip.x1 = gel->clip.y1 = BBOX_MIN; @@ -149,7 +149,7 @@ fz_new_gel(fz_context *ctx) gel->acap = 64; gel->alen = 0; - gel->active = fz_calloc(ctx, gel->acap, sizeof(fz_edge*)); + gel->active = fz_malloc_array(ctx, gel->acap, sizeof(fz_edge*)); return gel; } @@ -255,7 +255,7 @@ fz_insert_gel_raw(fz_gel *gel, int x0, int y0, int x1, int y1) if (gel->len + 1 == gel->cap) { gel->cap = gel->cap + 512; - gel->edges = fz_realloc(gel->ctx, gel->edges, gel->cap * sizeof(fz_edge)); + gel->edges = fz_resize_array(gel->ctx, gel->edges, gel->cap, sizeof(fz_edge)); } edge = &gel->edges[gel->len++]; @@ -445,7 +445,7 @@ insert_active(fz_gel *gel, int y, int *e) while (*e < gel->len && gel->edges[*e].y == y) { if (gel->alen + 1 == gel->acap) { int newcap = gel->acap + 64; - fz_edge **newactive = fz_realloc(gel->ctx, gel->active, newcap * sizeof(fz_edge*)); + fz_edge **newactive = fz_resize_array(gel->ctx, gel->active, newcap, sizeof(fz_edge*)); gel->active = newactive; gel->acap = newcap; } diff --git a/draw/draw_scale.c b/draw/draw_scale.c index 045de810..4d1b66c5 100644 --- a/draw/draw_scale.c +++ b/draw/draw_scale.c @@ -1199,7 +1199,7 @@ fz_scale_pixmap(fz_context *ctx, fz_pixmap *src, float x, float y, float w, floa temp_rows = contrib_rows->max_len; if (temp_span <= 0 || temp_rows > INT_MAX / temp_span) goto cleanup; - temp = fz_calloc(ctx, temp_span*temp_rows, sizeof(int)); + temp = fz_malloc_array(ctx, temp_span*temp_rows, sizeof(int)); if (temp == NULL) goto cleanup; switch (src->n) diff --git a/fitz/base_context.c b/fitz/base_context.c index db702e8e..bce0cba1 100644 --- a/fitz/base_context.c +++ b/fitz/base_context.c @@ -18,27 +18,23 @@ fz_free_context(fz_context *ctx) if (ctx->error) { assert(ctx->error->top == -1); - ctx->alloc->free(ctx->alloc->opaque, ctx->error); + free(ctx->error); } /* Free the context itself */ - ctx->alloc->free(ctx->alloc->opaque, ctx); - - /* We do NOT free the allocator! */ + free(ctx); } fz_context * -fz_new_context(fz_alloc_context *alloc) +fz_new_context(void) { fz_context *ctx; - assert(alloc != NULL); - ctx = alloc->malloc(alloc->opaque, sizeof(fz_context)); - if (ctx == NULL) + ctx = malloc(sizeof(fz_context)); + if (!ctx) return NULL; - ctx->alloc = alloc; - ctx->error = alloc->malloc(alloc->opaque, sizeof(fz_error_context)); + ctx->error = malloc(sizeof(fz_error_context)); if (!ctx->error) goto cleanup; ctx->error->top = -1; @@ -57,5 +53,5 @@ cleanup: fz_context * fz_clone_context(fz_context *ctx) { - return fz_new_context(ctx->alloc); + return fz_new_context(); } diff --git a/fitz/base_hash.c b/fitz/base_hash.c index f754644d..43bdf2d1 100644 --- a/fitz/base_hash.c +++ b/fitz/base_hash.c @@ -52,7 +52,7 @@ fz_new_hash_table(fz_context *ctx, int initialsize, int keylen) table->keylen = keylen; table->size = initialsize; table->load = 0; - table->ents = fz_calloc(ctx, table->size, sizeof(fz_hash_entry)); + table->ents = fz_malloc_array(ctx, table->size, sizeof(fz_hash_entry)); return table; } @@ -103,7 +103,7 @@ fz_resize_hash(fz_context *ctx, fz_hash_table *table, int newsize) return; } - table->ents = fz_calloc(ctx, newsize, sizeof(fz_hash_entry)); + table->ents = fz_malloc_array(ctx, newsize, sizeof(fz_hash_entry)); memset(table->ents, 0, sizeof(fz_hash_entry) * newsize); table->size = newsize; table->load = 0; diff --git a/fitz/base_memory.c b/fitz/base_memory.c index 643c85b6..27474cc3 100644 --- a/fitz/base_memory.c +++ b/fitz/base_memory.c @@ -1,130 +1,72 @@ #include "fitz.h" void * -fz_malloc(fz_context *ctx, size_t size) +fz_malloc(fz_context *ctx, unsigned int size) { - void *p; - fz_alloc_context *alloc; - - assert(ctx != NULL); - alloc = ctx->alloc; - assert(alloc != NULL); - p = alloc->malloc(alloc->opaque, size); + void *p = malloc(size); if (!p) { - fz_throw(ctx, "malloc failed (%d bytes)", size); + fprintf(stderr, "fatal error: out of memory\n"); + abort(); } return p; } -void *fz_calloc(fz_context *ctx, size_t count, size_t size) +void * +fz_malloc_array(fz_context *ctx, unsigned int count, unsigned int size) { void *p; - fz_alloc_context *alloc; - assert(ctx != NULL); - alloc = ctx->alloc; - assert(alloc != NULL); - p = alloc->calloc(alloc->opaque, count, size); - if (!p) + if (count == 0 || size == 0) + return 0; + + if (count > UINT_MAX / size) { - fz_throw(ctx, "calloc failed (%d x %d bytes)", count, size); + fprintf(stderr, "fatal error: out of memory (integer overflow)\n"); + abort(); } - return p; -} -void * -fz_realloc(fz_context *ctx, void *p, size_t size) -{ - fz_alloc_context *alloc; - - assert(ctx != NULL); - alloc = ctx->alloc; - assert(alloc != NULL); - p = alloc->realloc(alloc->opaque, p, size); + p = malloc(count * size); if (!p) { - fz_throw(ctx, "realloc failed (%d bytes)", size); + fprintf(stderr, "fatal error: out of memory\n"); + abort(); } 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_malloc_nothrow(fz_context *ctx, size_t size) -{ - fz_alloc_context *alloc; - - assert(ctx != NULL); - alloc = ctx->alloc; - assert(alloc != NULL); - return alloc->malloc(alloc->opaque, size); -} - -void *fz_calloc_nothrow(fz_context *ctx, size_t count, size_t size) -{ - fz_alloc_context *alloc; - - 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_resize_array(fz_context *ctx, void *p, unsigned int count, unsigned int size) { - fz_alloc_context *alloc; + void *np; - 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); -} + if (count == 0 || size == 0) + { + fz_free(ctx, p); + return 0; + } -void * -fz_calloc_default(void *opaque, size_t count, size_t size) -{ - return calloc(count, size); -} + if (count > UINT_MAX / size) + { + fprintf(stderr, "fatal error: out of memory (integer overflow)\n"); + abort(); + } -void * -fz_realloc_default(void *opaque, void *p, size_t size) -{ - return realloc(p, size); + np = realloc(p, count * size); + if (np == NULL) + { + fprintf(stderr, "fatal error: out of memory\n"); + abort(); + } + return np; } void -fz_free_default(void *opaque, void *p) +fz_free(fz_context *ctx, 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(fz_context *ctx, char *s) { diff --git a/fitz/base_object.c b/fitz/base_object.c index c515b70d..fd8339a4 100644 --- a/fitz/base_object.c +++ b/fitz/base_object.c @@ -389,7 +389,7 @@ fz_new_array(fz_context *ctx, int initialcap) obj->u.a.len = 0; obj->u.a.cap = initialcap > 1 ? initialcap : 6; - obj->u.a.items = fz_calloc(ctx, obj->u.a.cap, sizeof(fz_obj*)); + obj->u.a.items = fz_malloc_array(ctx, obj->u.a.cap, sizeof(fz_obj*)); for (i = 0; i < obj->u.a.cap; i++) obj->u.a.items[i] = NULL; @@ -469,7 +469,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->ctx, obj->u.a.items, obj->u.a.cap * sizeof(fz_obj*)); + obj->u.a.items = fz_resize_array(obj->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; } @@ -491,7 +491,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->ctx, obj->u.a.items, obj->u.a.cap * sizeof(fz_obj*)); + obj->u.a.items = fz_resize_array(obj->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; } @@ -525,7 +525,7 @@ fz_new_dict(fz_context *ctx, int initialcap) obj->u.d.len = 0; obj->u.d.cap = initialcap > 1 ? initialcap : 10; - obj->u.d.items = fz_calloc(ctx, obj->u.d.cap, sizeof(struct keyval)); + obj->u.d.items = fz_malloc_array(ctx, obj->u.d.cap, sizeof(struct keyval)); for (i = 0; i < obj->u.d.cap; i++) { obj->u.d.items[i].k = NULL; @@ -694,7 +694,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->ctx, obj->u.d.items, obj->u.d.cap * sizeof(struct keyval)); + obj->u.d.items = fz_resize_array(obj->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; diff --git a/fitz/dev_text.c b/fitz/dev_text.c index cebfbceb..bbc8efcb 100644 --- a/fitz/dev_text.c +++ b/fitz/dev_text.c @@ -55,7 +55,7 @@ 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(ctx, span->text, span->cap * sizeof(fz_text_char)); + span->text = fz_resize_array(ctx, span->text, span->cap, sizeof(fz_text_char)); } span->text[span->len].c = c; span->text[span->len].bbox = bbox; diff --git a/fitz/filt_faxd.c b/fitz/filt_faxd.c index bd64939d..14a71a87 100644 --- a/fitz/filt_faxd.c +++ b/fitz/filt_faxd.c @@ -672,12 +672,12 @@ fz_open_faxd(fz_stream *chain, fz_obj *params) { fz_faxd *fax; fz_obj *obj; - fz_context *ctx; + fz_context *ctx; assert(chain != NULL); - ctx = chain->ctx; - fax = fz_malloc(ctx, sizeof(fz_faxd)); - fax->chain = chain; + ctx = chain->ctx; + fax = fz_malloc(ctx, sizeof(fz_faxd)); + fax->chain = chain; fax->ref = NULL; fax->dst = NULL; diff --git a/fitz/filt_flate.c b/fitz/filt_flate.c index b7ef163d..d9ac44e6 100644 --- a/fitz/filt_flate.c +++ b/fitz/filt_flate.c @@ -12,7 +12,7 @@ struct fz_flate_s static void *zalloc(void *opaque, unsigned int items, unsigned int size) { - return fz_calloc(opaque, items, size); + return fz_malloc_array(opaque, items, size); } static void zfree(void *opaque, void *ptr) diff --git a/fitz/fitz.h b/fitz/fitz.h index 0f0d3b5c..a15304b7 100644 --- a/fitz/fitz.h +++ b/fitz/fitz.h @@ -139,22 +139,8 @@ char *fz_get_error_line(int n); /* Context types */ typedef struct fz_error_context_s fz_error_context; -typedef struct fz_alloc_context_s fz_alloc_context; typedef struct fz_context_s fz_context; -/* Memory allocator context */ - -struct fz_alloc_context_s -{ - void *opaque; - void *(*malloc)(void *, size_t); - void *(*realloc)(void *, void *, size_t); - void (*free)(void *, void *); - void *(*calloc)(void *, size_t, size_t); -}; - -extern fz_alloc_context fz_alloc_default; - /* Exception context */ struct fz_error_context_s @@ -187,11 +173,10 @@ void fz_rethrow(fz_context *); struct fz_context_s { - fz_alloc_context *alloc; fz_error_context *error; }; -fz_context *fz_new_context(fz_alloc_context *alloc); +fz_context *fz_new_context(void); fz_context *fz_clone_context(fz_context *ctx); void fz_free_context(fz_context *ctx); @@ -202,18 +187,13 @@ void fz_free_context(fz_context *ctx); /* memory allocation */ /* 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); +void *fz_malloc(fz_context *ctx, unsigned int size); +void *fz_malloc_array(fz_context *ctx, unsigned int count, unsigned int size); +void *fz_resize_array(fz_context *ctx, void *p, unsigned int count, unsigned int 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); - /* runtime (hah!) test for endian-ness */ int fz_is_big_endian(void); diff --git a/fitz/res_bitmap.c b/fitz/res_bitmap.c index 2ca66efe..3b13883b 100644 --- a/fitz/res_bitmap.c +++ b/fitz/res_bitmap.c @@ -14,7 +14,7 @@ fz_new_bitmap(fz_context *ctx, int w, int h, int n) * use SSE2 etc. */ bit->stride = ((n * w + 31) & ~31) >> 3; - bit->samples = fz_calloc(ctx, h, bit->stride); + bit->samples = fz_malloc_array(ctx, h, bit->stride); return bit; } diff --git a/fitz/res_font.c b/fitz/res_font.c index b32f6a61..0ef7bf20 100644 --- a/fitz/res_font.c +++ b/fitz/res_font.c @@ -498,8 +498,8 @@ fz_new_type3_font(fz_context *ctx, char *name, fz_matrix matrix) int i; font = fz_new_font(ctx, name); - font->t3procs = fz_calloc(ctx, 256, sizeof(fz_buffer*)); - font->t3widths = fz_calloc(ctx, 256, sizeof(float)); + font->t3procs = fz_malloc_array(ctx, 256, sizeof(fz_buffer*)); + font->t3widths = fz_malloc_array(ctx, 256, sizeof(float)); font->t3matrix = matrix; for (i = 0; i < 256; i++) diff --git a/fitz/res_path.c b/fitz/res_path.c index 1cc92937..af5d9f43 100644 --- a/fitz/res_path.c +++ b/fitz/res_path.c @@ -23,7 +23,7 @@ fz_clone_path(fz_context *ctx, fz_path *old) path = fz_malloc(ctx, sizeof(fz_path)); path->len = old->len; path->cap = old->len; - path->items = fz_calloc(ctx, path->cap, sizeof(fz_path_item)); + path->items = fz_malloc_array(ctx, path->cap, sizeof(fz_path_item)); memcpy(path->items, old->items, sizeof(fz_path_item) * path->len); return path; @@ -43,7 +43,7 @@ grow_path(fz_context *ctx, fz_path *path, int n) return; while (path->len + n > path->cap) path->cap = path->cap + 36; - path->items = fz_realloc(ctx, path->items, path->cap * sizeof(fz_path_item)); + path->items = fz_resize_array(ctx, path->items, path->cap, sizeof(fz_path_item)); } void diff --git a/fitz/res_pixmap.c b/fitz/res_pixmap.c index 3706d495..040ec386 100644 --- a/fitz/res_pixmap.c +++ b/fitz/res_pixmap.c @@ -35,7 +35,7 @@ fz_new_pixmap_with_data(fz_context *ctx, fz_colorspace *colorspace, int w, int h else { fz_memory_used += pix->w * pix->h * pix->n; - pix->samples = fz_calloc(ctx, pix->h, pix->w * pix->n); + pix->samples = fz_malloc_array(ctx, pix->h, pix->w * pix->n); pix->free_samples = 1; } diff --git a/fitz/res_text.c b/fitz/res_text.c index 1c5bbad0..5eaf12b0 100644 --- a/fitz/res_text.c +++ b/fitz/res_text.c @@ -35,7 +35,7 @@ fz_clone_text(fz_context *ctx, fz_text *old) text->wmode = old->wmode; text->len = old->len; text->cap = text->len; - text->items = fz_calloc(ctx, text->len, sizeof(fz_text_item)); + text->items = fz_malloc_array(ctx, text->len, sizeof(fz_text_item)); memcpy(text->items, old->items, text->len * sizeof(fz_text_item)); return text; @@ -97,7 +97,7 @@ fz_grow_text(fz_context *ctx, fz_text *text, int n) return; while (text->len + n > text->cap) text->cap = text->cap + 36; - text->items = fz_realloc(ctx, text->items, text->cap * sizeof(fz_text_item)); + text->items = fz_resize_array(ctx, text->items, text->cap, sizeof(fz_text_item)); } void diff --git a/fitz/stm_buffer.c b/fitz/stm_buffer.c index edd5c9c9..eb96cd2f 100644 --- a/fitz/stm_buffer.c +++ b/fitz/stm_buffer.c @@ -36,7 +36,7 @@ fz_drop_buffer(fz_context *ctx, fz_buffer *buf) void fz_resize_buffer(fz_context *ctx, fz_buffer *buf, int size) { - buf->data = fz_realloc(ctx, buf->data, size); + buf->data = fz_resize_array(ctx, buf->data, size, 1); buf->cap = size; if (buf->len > buf->cap) buf->len = buf->cap; diff --git a/pdf/pdf_cmap.c b/pdf/pdf_cmap.c index 0c446470..3de3346b 100644 --- a/pdf/pdf_cmap.c +++ b/pdf/pdf_cmap.c @@ -189,7 +189,7 @@ add_table(fz_context *ctx, pdf_cmap *cmap, int value) if (cmap->tlen + 1 > cmap->tcap) { cmap->tcap = cmap->tcap > 1 ? (cmap->tcap * 3) / 2 : 256; - cmap->table = fz_realloc(ctx, cmap->table, cmap->tcap * sizeof(unsigned short)); + cmap->table = fz_resize_array(ctx, cmap->table, cmap->tcap, sizeof(unsigned short)); } cmap->table[cmap->tlen++] = value; } @@ -210,7 +210,7 @@ add_range(fz_context *ctx, pdf_cmap *cmap, int low, int high, int flag, int offs if (cmap->rlen + 1 > cmap->rcap) { cmap->rcap = cmap->rcap > 1 ? (cmap->rcap * 3) / 2 : 256; - cmap->ranges = fz_realloc(ctx, cmap->ranges, cmap->rcap * sizeof(pdf_range)); + cmap->ranges = fz_resize_array(ctx, cmap->ranges, cmap->rcap, sizeof(pdf_range)); } cmap->ranges[cmap->rlen].low = low; pdf_range_set_high(&cmap->ranges[cmap->rlen], high); diff --git a/pdf/pdf_colorspace.c b/pdf/pdf_colorspace.c index 56a54e5c..1a2e79b3 100644 --- a/pdf/pdf_colorspace.c +++ b/pdf/pdf_colorspace.c @@ -230,7 +230,7 @@ load_indexed(fz_colorspace **csp, pdf_xref *xref, fz_obj *array) idx->high = fz_to_int(highobj); idx->high = CLAMP(idx->high, 0, 255); n = base->n * (idx->high + 1); - idx->lookup = fz_calloc(ctx, 1, n); + idx->lookup = fz_malloc_array(ctx, 1, n); cs = fz_new_colorspace(ctx, "Indexed", 1); cs->to_rgb = indexed_to_rgb; diff --git a/pdf/pdf_font.c b/pdf/pdf_font.c index ad5b8d97..e1cae007 100644 --- a/pdf/pdf_font.c +++ b/pdf/pdf_font.c @@ -504,7 +504,7 @@ pdf_load_simple_font(pdf_font_desc **fontdescp, pdf_xref *xref, fz_obj *dict) else fz_warn("freetype could not find any cmaps"); - etable = fz_calloc(ctx, 256, sizeof(unsigned short)); + etable = fz_malloc_array(ctx, 256, sizeof(unsigned short)); for (i = 0; i < 256; i++) { estrings[i] = NULL; @@ -800,7 +800,7 @@ load_cid_font(pdf_font_desc **fontdescp, pdf_xref *xref, fz_obj *dict, fz_obj *e goto cleanup; fontdesc->cid_to_gid_len = (buf->len) / 2; - fontdesc->cid_to_gid = fz_calloc(ctx, fontdesc->cid_to_gid_len, sizeof(unsigned short)); + fontdesc->cid_to_gid = fz_malloc_array(ctx, fontdesc->cid_to_gid_len, sizeof(unsigned short)); for (i = 0; i < fontdesc->cid_to_gid_len; i++) fontdesc->cid_to_gid[i] = (buf->data[i * 2] << 8) + buf->data[i * 2 + 1]; @@ -1067,7 +1067,7 @@ pdf_make_width_table(fz_context *ctx, pdf_font_desc *fontdesc) } font->width_count ++; - font->width_table = fz_calloc(ctx, font->width_count, sizeof(int)); + font->width_table = fz_malloc_array(ctx, font->width_count, sizeof(int)); for (i = 0; i < fontdesc->hmtx_len; i++) { diff --git a/pdf/pdf_function.c b/pdf/pdf_function.c index 7ab681c3..50b09926 100644 --- a/pdf/pdf_function.c +++ b/pdf/pdf_function.c @@ -677,7 +677,7 @@ resize_code(fz_context *ctx, pdf_function *func, int newsize) if (newsize >= func->u.p.cap) { func->u.p.cap = func->u.p.cap + 64; - func->u.p.code = fz_realloc(ctx, func->u.p.code, func->u.p.cap * sizeof(psobj)); + func->u.p.code = fz_resize_array(ctx, func->u.p.code, func->u.p.cap, sizeof(psobj)); } } @@ -960,7 +960,7 @@ load_sample_func(pdf_function *func, pdf_xref *xref, fz_obj *dict, int num, int for (i = 0, samplecount = func->n; i < func->m; i++) samplecount *= func->u.sa.size[i]; - func->u.sa.samples = fz_calloc(ctx, samplecount, sizeof(float)); + func->u.sa.samples = fz_malloc_array(ctx, samplecount, sizeof(float)); error = pdf_open_stream(&stream, xref, num, gen); if (error) @@ -1206,9 +1206,9 @@ load_stitching_func(pdf_function *func, pdf_xref *xref, fz_obj *dict) { k = fz_array_len(obj); - func->u.st.funcs = fz_calloc(ctx, k, sizeof(pdf_function*)); - func->u.st.bounds = fz_calloc(ctx, k - 1, sizeof(float)); - func->u.st.encode = fz_calloc(ctx, k * 2, sizeof(float)); + func->u.st.funcs = fz_malloc_array(ctx, k, sizeof(pdf_function*)); + func->u.st.bounds = fz_malloc_array(ctx, k - 1, sizeof(float)); + func->u.st.encode = fz_malloc_array(ctx, k * 2, sizeof(float)); funcs = func->u.st.funcs; for (i = 0; i < k; i++) diff --git a/pdf/pdf_image.c b/pdf/pdf_image.c index cb63309d..c5242b72 100644 --- a/pdf/pdf_image.c +++ b/pdf/pdf_image.c @@ -191,7 +191,7 @@ pdf_load_image_imp(fz_pixmap **imgp, pdf_xref *xref, fz_obj *rdb, fz_obj *dict, } } - samples = fz_calloc(ctx, h, stride); + samples = fz_malloc_array(ctx, h, stride); len = fz_read(stm, samples, h * stride); if (len < 0) diff --git a/pdf/pdf_metrics.c b/pdf/pdf_metrics.c index 62353f9f..3aa0ef24 100644 --- a/pdf/pdf_metrics.c +++ b/pdf/pdf_metrics.c @@ -26,7 +26,7 @@ pdf_add_hmtx(fz_context *ctx, pdf_font_desc *font, int lo, int hi, int w) if (font->hmtx_len + 1 >= font->hmtx_cap) { font->hmtx_cap = font->hmtx_cap + 16; - font->hmtx = fz_realloc(ctx, font->hmtx, font->hmtx_cap * sizeof(pdf_hmtx)); + font->hmtx = fz_resize_array(ctx, font->hmtx, font->hmtx_cap, sizeof(pdf_hmtx)); } font->hmtx[font->hmtx_len].lo = lo; @@ -41,7 +41,7 @@ pdf_add_vmtx(fz_context *ctx, pdf_font_desc *font, int lo, int hi, int x, int y, if (font->vmtx_len + 1 >= font->vmtx_cap) { font->vmtx_cap = font->vmtx_cap + 16; - font->vmtx = fz_realloc(ctx, font->vmtx, font->vmtx_cap * sizeof(pdf_vmtx)); + font->vmtx = fz_resize_array(ctx, font->vmtx, font->vmtx_cap, sizeof(pdf_vmtx)); } font->vmtx[font->vmtx_len].lo = lo; diff --git a/pdf/pdf_page.c b/pdf/pdf_page.c index 88825531..7c8fb88e 100644 --- a/pdf/pdf_page.c +++ b/pdf/pdf_page.c @@ -85,8 +85,8 @@ pdf_load_page_tree_node(pdf_xref *xref, fz_obj *node, struct info info) { fz_warn("found more pages than expected"); xref->page_cap ++; - xref->page_refs = fz_realloc(ctx, xref->page_refs, xref->page_cap * sizeof(fz_obj*)); - xref->page_objs = fz_realloc(ctx, xref->page_objs, xref->page_cap * sizeof(fz_obj*)); + xref->page_refs = fz_resize_array(ctx, xref->page_refs, xref->page_cap, sizeof(fz_obj*)); + xref->page_objs = fz_resize_array(ctx, xref->page_objs, xref->page_cap, sizeof(fz_obj*)); } xref->page_refs[xref->page_len] = fz_keep_obj(node); @@ -111,8 +111,8 @@ pdf_load_page_tree(pdf_xref *xref) xref->page_cap = fz_to_int(count); xref->page_len = 0; - xref->page_refs = fz_calloc(ctx, xref->page_cap, sizeof(fz_obj*)); - xref->page_objs = fz_calloc(ctx, xref->page_cap, sizeof(fz_obj*)); + xref->page_refs = fz_malloc_array(ctx, xref->page_cap, sizeof(fz_obj*)); + xref->page_objs = fz_malloc_array(ctx, xref->page_cap, sizeof(fz_obj*)); info.resources = NULL; info.mediabox = NULL; diff --git a/pdf/pdf_parse.c b/pdf/pdf_parse.c index 04c67eca..b1f22e46 100644 --- a/pdf/pdf_parse.c +++ b/pdf/pdf_parse.c @@ -101,19 +101,19 @@ pdf_to_ucs2(fz_context *ctx, fz_obj *src) if (srclen >= 2 && srcptr[0] == 254 && srcptr[1] == 255) { - dstptr = dst = fz_calloc(ctx, (srclen - 2) / 2 + 1, sizeof(short)); + dstptr = dst = fz_malloc_array(ctx, (srclen - 2) / 2 + 1, sizeof(short)); for (i = 2; i + 1 < srclen; i += 2) *dstptr++ = srcptr[i] << 8 | srcptr[i+1]; } else if (srclen >= 2 && srcptr[0] == 255 && srcptr[1] == 254) { - dstptr = dst = fz_calloc(ctx, (srclen - 2) / 2 + 1, sizeof(short)); + dstptr = dst = fz_malloc_array(ctx, (srclen - 2) / 2 + 1, sizeof(short)); for (i = 2; i + 1 < srclen; i += 2) *dstptr++ = srcptr[i] | srcptr[i+1] << 8; } else { - dstptr = dst = fz_calloc(ctx, srclen + 1, sizeof(short)); + dstptr = dst = fz_malloc_array(ctx, srclen + 1, sizeof(short)); for (i = 0; i < srclen; i++) *dstptr++ = pdf_doc_encoding[srcptr[i]]; } diff --git a/pdf/pdf_repair.c b/pdf/pdf_repair.c index 83b0d814..461fd4de 100644 --- a/pdf/pdf_repair.c +++ b/pdf/pdf_repair.c @@ -208,7 +208,7 @@ pdf_repair_xref(pdf_xref *xref, char *buf, int bufsize) listlen = 0; listcap = 1024; - list = fz_calloc(ctx, listcap, sizeof(struct entry)); + list = fz_malloc_array(ctx, listcap, sizeof(struct entry)); /* look for '%PDF' version marker within first kilobyte of file */ n = fz_read(xref->file, (unsigned char *)buf, MAX(bufsize, 1024)); @@ -271,7 +271,7 @@ pdf_repair_xref(pdf_xref *xref, char *buf, int bufsize) if (listlen + 1 == listcap) { listcap = (listcap * 3) / 2; - list = fz_realloc(ctx, list, listcap * sizeof(struct entry)); + list = fz_resize_array(ctx, list, listcap, sizeof(struct entry)); } list[listlen].num = num; diff --git a/pdf/pdf_shade.c b/pdf/pdf_shade.c index a6819521..45fefba5 100644 --- a/pdf/pdf_shade.c +++ b/pdf/pdf_shade.c @@ -24,7 +24,7 @@ pdf_grow_mesh(fz_context *ctx, fz_shade *shade, int amount) while (shade->mesh_len + amount > shade->mesh_cap) shade->mesh_cap = (shade->mesh_cap * 3) / 2; - shade->mesh = fz_realloc(ctx, shade->mesh, shade->mesh_cap * sizeof(float)); + shade->mesh = fz_resize_array(ctx, shade->mesh, shade->mesh_cap, sizeof(float)); } static void @@ -688,8 +688,8 @@ pdf_load_type5_shade(fz_shade *shade, pdf_xref *xref, fz_obj *dict, else ncomp = shade->colorspace->n; - ref = fz_calloc(ctx, p.vprow, sizeof(struct vertex)); - buf = fz_calloc(ctx, p.vprow, sizeof(struct vertex)); + ref = fz_malloc_array(ctx, p.vprow, sizeof(struct vertex)); + buf = fz_malloc_array(ctx, p.vprow, sizeof(struct vertex)); first = 1; while (!fz_is_eof_bits(stream)) diff --git a/pdf/pdf_unicode.c b/pdf/pdf_unicode.c index 25781e1c..46c68a52 100644 --- a/pdf/pdf_unicode.c +++ b/pdf/pdf_unicode.c @@ -63,7 +63,7 @@ pdf_load_to_unicode(pdf_font_desc *font, pdf_xref *xref, /* TODO one-to-many mappings */ font->cid_to_ucs_len = 256; - font->cid_to_ucs = fz_calloc(ctx, 256, sizeof(unsigned short)); + font->cid_to_ucs = fz_malloc_array(ctx, 256, sizeof(unsigned short)); for (i = 0; i < 256; i++) { diff --git a/pdf/pdf_xref.c b/pdf/pdf_xref.c index d259e6d0..b514c193 100644 --- a/pdf/pdf_xref.c +++ b/pdf/pdf_xref.c @@ -172,7 +172,7 @@ pdf_resize_xref(pdf_xref *xref, int newlen) { int i; - xref->table = fz_realloc(xref->ctx, xref->table, newlen * sizeof(pdf_xref_entry)); + xref->table = fz_resize_array(xref->ctx, xref->table, newlen, sizeof(pdf_xref_entry)); for (i = xref->len; i < newlen; i++) { xref->table[i].type = 0; @@ -726,8 +726,8 @@ pdf_load_obj_stm(pdf_xref *xref, int num, int gen, char *buf, int cap) count = fz_to_int(fz_dict_gets(objstm, "N")); first = fz_to_int(fz_dict_gets(objstm, "First")); - numbuf = fz_calloc(ctx, count, sizeof(int)); - ofsbuf = fz_calloc(ctx, count, sizeof(int)); + numbuf = fz_malloc_array(ctx, count, sizeof(int)); + ofsbuf = fz_malloc_array(ctx, count, sizeof(int)); error = pdf_open_stream(&stm, xref, num, gen); if (error) diff --git a/scripts/cmapdump.c b/scripts/cmapdump.c index ab2eac1d..b245fd48 100644 --- a/scripts/cmapdump.c +++ b/scripts/cmapdump.c @@ -50,7 +50,7 @@ main(int argc, char **argv) return 1; } - ctx = fz_new_context(&fz_alloc_default); + ctx = fz_new_context(); if (ctx == NULL) { fprintf(stderr, "failed to initialise!\n"); diff --git a/viewer/ximage.o b/viewer/ximage.o Binary files differnew file mode 100644 index 00000000..ec5a5d10 --- /dev/null +++ b/viewer/ximage.o diff --git a/viewer/xmain.c b/viewer/xmain.c new file mode 100644 index 00000000..6223f1b6 --- /dev/null +++ b/viewer/xmain.c @@ -0,0 +1,382 @@ +#include "aekit.h" + +#include <xcb/xcb.h> +#include <xcb/xcb_icccm.h> /* for wm-hints */ +#include <xcb/xcb_image.h> /* for pixmap-from-bitmap-data */ + +#include "ximage.h" + +#define GENBASE 1000 +#define GENID (GENBASE + __LINE__) + +#ifndef XCB_ATOM_STRING +#define XCB_ATOM_STRING 31 +#endif + +#define DEFW 612 // 595 /* 612 */ +#define DEFH 792 // 842 /* 792 */ + +void ui_main_loop(void); + +char *title = "aekit"; + +void die(char *msg) +{ + fprintf(stderr, "error: %s\n", msg); + exit(1); +} + +static const char *xcb_error_name_list[] = { + "None", "Request", "Value", "Window", "Pixmap", "Atom", + "Cursor", "Font", "Match", "Drawable", "Access", "Alloc", + "Colormap", "GContext", "IDChoice", "Name", "Length", + "Implementation", +}; + +const char *xcb_get_error_name(int detail) +{ + if (detail >= 0 && detail < nelem(xcb_error_name_list)) + return xcb_error_name_list[detail]; + return "Unknown"; +} + +static xcb_screen_t * +find_screen_by_number(const xcb_setup_t *setup, int screen) +{ + xcb_screen_iterator_t i = xcb_setup_roots_iterator(setup); + for (; i.rem; --screen, xcb_screen_next(&i)) + if (screen == 0) + return i.data; + return 0; +} + +struct unifont *font; +struct image dstk; +struct image *dst = &dstk; +struct ui ui = { 0 }; + +struct color btn_brown = { 206, 193, 180 }; +struct color btn_brown_hi = { 218, 205, 192 }; +struct color btn_brown_lo = { 173, 160, 147 }; +struct color border = { 80, 80, 80 }; +struct color gray = { 153, 153, 153 }; +struct color black = { 0, 0, 0 }; + +#define icon_width 16 +#define icon_height 16 +static unsigned char icon_bits[] = { + 0x00, 0x00, 0x00, 0x1e, 0x00, 0x2b, 0x80, 0x55, 0x8c, 0x62, 0x8c, 0x51, + 0x9c, 0x61, 0x1c, 0x35, 0x3c, 0x1f, 0x3c, 0x0f, 0xfc, 0x0f, 0xec, 0x0d, + 0xec, 0x0d, 0xcc, 0x0c, 0xcc, 0x0c, 0x00, 0x00 }; + +#define icon_mask_width 16 +#define icon_mask_height 16 +static unsigned char icon_mask_bits[] = { + 0x00, 0x1e, 0x00, 0x3f, 0x80, 0x7f, 0xce, 0xff, 0xde, 0xff, 0xde, 0xff, + 0xfe, 0xff, 0xfe, 0x7f, 0xfe, 0x3f, 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, + 0xfe, 0x1f, 0xfe, 0x1f, 0xfe, 0x1f, 0xce, 0x1c }; + +struct ximage *surface; +xcb_connection_t *conn; +xcb_screen_t *screen; +xcb_window_t window; +xcb_gcontext_t gc; +int dirty; + +void +on_resize(int w, int h) +{ + if (dst->samples && dst->w == w && dst->h == h) + return; + + free(dst->samples); + + dst->w = w; + dst->h = h; + dst->stride = dst->w * 4; + dst->samples = malloc(dst->h * dst->stride); + + memset(dst->samples, 192, dst->h * dst->stride); + + ui.key = 0; + ui_main_loop(); + + dirty = 1; +} + +void +ui_start_frame(void) +{ + ui.hot = 0; +} + +void +ui_end_frame(void) +{ + if (!(ui.down & 1)) + ui.active = 0; + else if (!ui.active) + ui.active = -1; /* click outside widgets, mark as unavailable */ + dirty = 1; +} + +int +ui_region_hit(int x, int y, int w, int h) +{ + return ui.x >= x && ui.y >= y && ui.x < x + w && ui.y < y + h; +} + +int +ui_button(int id, int x, int y, char *label) +{ + int w = ui_measure_unifont_string(font, label) + 12; + int h = 25; + int b = 2; + + if (ui_region_hit(x, y, w, h)) { + ui.hot = id; + if (!ui.active && (ui.down & 1)) + ui.active = id; + } + + int x0 = x; + int y0 = y; + int x1 = x + w - 1; + int y1 = y + h - 1; + + ui_fill_rect(dst, x0, y0, x1+1, y0+1, border); + ui_fill_rect(dst, x0, y1, x1+1, y1+1, border); + ui_fill_rect(dst, x0, y0, x0+1, y1, border); + ui_fill_rect(dst, x1, y0, x1+1, y1, border); + + x0++; y0++; x1--; y1--; + ui_fill_rect(dst, x0, y0, x1+1, y0+1, btn_brown_hi); + ui_fill_rect(dst, x0, y1, x1+1, y1+1, btn_brown_lo); + ui_fill_rect(dst, x0, y0, x0+1, y1, btn_brown_hi); + ui_fill_rect(dst, x1, y0, x1+1, y1, btn_brown_lo); + + if (ui.active == id && (ui.down & 1)) { + if (ui.hot == id) + ui_fill_rect(dst, x+b-1, y+b-1, x+w-b+1, y+h-b+1, btn_brown_lo); + else + ui_fill_rect(dst, x+b, y+b, x+w-b, y+h-b, btn_brown); + } + else + ui_fill_rect(dst, x+b, y+b, x+w-b, y+h-b, btn_brown); + + ui_draw_unifont_string(dst, font, label, x+b+4, y+b+2, black); + + return !(ui.down & 1) && ui.hot == id && ui.active == id; +} + +void +ui_main_loop(void) +{ + ui_start_frame(); + + if (ui_button(GENID, 30, 30, "☑ Hello, world!")) puts("click 1"); + if (ui_button(GENID, 30, 60, "☐ 你好")) puts("click 2"); + if (ui_button(GENID, 30, 90, "Γεια σου")) puts("click 3"); + if (ui_button(GENID, 30, 120, "My ♚ will eat your ♖!")) puts("click 4"); + if (ui_button(GENID, 30, 150, "おはよう")) puts("click 5"); + if (ui_button(GENID, 30, 180, "안녕하세요")) puts("click 6"); + if (ui_button(GENID, 30, 210, "☏ xin chào")) puts("click 7"); + if (ui_button(GENID, 30, 240, "[ ☢ ☣ ☥ ☭ ☯ ]")) puts("click 7"); + if (ui_button(GENID, 30, 270, "(♃♄♅♆♇♈♉♊♋♌♍ ...)")) puts("click 7"); + + ui_end_frame(); +} + +int +main(int argc, char **argv) +{ + const xcb_setup_t *setup; + xcb_generic_event_t *event; + int screen_num; + xcb_wm_hints_t hints; + xcb_pixmap_t icon, icon_mask; + uint32_t mask; + uint32_t attrs[2]; + int w, h; + int button; + + font = ui_load_unifont("unifont.dat"); + if (!font) + die("cannot load 'unifont.dat'"); + + /* Connect and find the screen */ + + conn = xcb_connect(NULL, &screen_num); + if (!conn) + die("cannot connect to display"); + + if (xcb_connection_has_error(conn)) + die("cannot connect to display"); + + setup = xcb_get_setup(conn); + if (!setup) + die("cannot get display setup"); + + screen = find_screen_by_number(setup, screen_num); + if (!screen) + die("cannot find screen"); + + /* Create window */ + + mask = XCB_CW_EVENT_MASK; + attrs[0] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY | + XCB_EVENT_MASK_KEY_PRESS | + XCB_EVENT_MASK_POINTER_MOTION | + XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE; + + window = xcb_generate_id(conn); + xcb_create_window(conn, 0, window, screen->root, + 20, 20, DEFW, DEFH, 0, + XCB_WINDOW_CLASS_INPUT_OUTPUT, screen->root_visual, + mask, attrs); + + /* Set title and icon */ + + xcb_set_wm_name(conn, window, XCB_ATOM_STRING, strlen(title), title); + xcb_set_wm_icon_name(conn, window, XCB_ATOM_STRING, strlen(title), title); + + icon = xcb_create_pixmap_from_bitmap_data(conn, window, + icon_bits, icon_width, icon_height, 1, + screen->black_pixel, screen->white_pixel, NULL); + icon_mask = xcb_create_pixmap_from_bitmap_data(conn, window, + icon_mask_bits, icon_mask_width, icon_mask_height, 1, + screen->black_pixel, screen->white_pixel, NULL); + + memset(&hints, 0, sizeof hints); + xcb_wm_hints_set_icon_pixmap(&hints, icon); + xcb_wm_hints_set_icon_mask(&hints, icon_mask); + xcb_set_wm_hints(conn, window, &hints); + + xcb_map_window(conn, window); + + /* Create GC */ + + mask = XCB_GC_FOREGROUND; + attrs[0] = screen->black_pixel; + + gc = xcb_generate_id(conn); + xcb_create_gc(conn, gc, window, mask, attrs); + + xcb_flush(conn); + + /* Enter the event loop */ + + if (xcb_connection_has_error(conn)) + die("display connection problem 1"); + + surface = ximage_create(conn, screen, screen->root_visual); + + if (xcb_connection_has_error(conn)) + die("display connection problem 2"); + + dirty = 0; + w = h = 0; + + dst->w = 0; + dst->h = 0; + dst->samples = NULL; + + while (1) { + event = xcb_wait_for_event(conn); + if (!event) + break; + + while (event) { + switch (event->response_type & 0x7f) { + case 0: { + printf("xcb error: Bad%s\n", xcb_get_error_name(event->pad0)); + break; + } + case XCB_EXPOSE: { + dirty = 1; + break; + } + case XCB_REPARENT_NOTIFY: + case XCB_MAP_NOTIFY: + case XCB_UNMAP_NOTIFY: + break; + case XCB_CONFIGURE_NOTIFY: { + xcb_configure_notify_event_t *config; + config = (xcb_configure_notify_event_t*)event; + w = config->width; + h = config->height; + if (w != dst->w || h != dst->h) + dirty = 1; + break; + } + case XCB_BUTTON_PRESS: { + xcb_button_press_event_t *press; + press = (xcb_button_press_event_t*)event; + button = 1 << (press->detail - 1); +printf("press %d\n", button); + ui.x = press->event_x; + ui.y = press->event_y; + ui.down |= button; + ui.key = 0; + ui_main_loop(); + break; + } + case XCB_BUTTON_RELEASE: { + xcb_button_release_event_t *release; + release = (xcb_button_release_event_t*)event; + button = 1 << (release->detail - 1); +printf("release %d\n", button); + ui.x = release->event_x; + ui.y = release->event_y; + ui.down &= ~button; + ui.key = 0; + ui_main_loop(); + break; + } + case XCB_MOTION_NOTIFY: { + xcb_motion_notify_event_t *motion; + motion = (xcb_motion_notify_event_t*)event; + ui.x = motion->event_x; + ui.y = motion->event_y; + ui.key = 0; + /* TODO: coalesce motion events */ + if (ui.active > 0) { +//puts("motion while active"); + ui_main_loop(); + } + break; + } + case XCB_KEY_PRESS: { + xcb_key_press_event_t *key; + key = (xcb_key_press_event_t*)event; + ui.x = key->event_x; + ui.y = key->event_y; + ui.key = key->detail; + ui_main_loop(); + break; + } + default: + printf("got unknown event %d\n", event->response_type); + break; + } + event = xcb_poll_for_event(conn); + } + + if (dirty) { +//printf("dirty, repainting\n"); + if (w != dst->w || h != dst->h) + on_resize(w, h); + ximage_draw(conn, window, gc, surface, + 0, 0, dst->w, dst->h, dst->samples); + dirty = 0; + } + } + + printf("disconnecting\n"); + + ximage_destroy(surface); + + xcb_disconnect(conn); + + return 0; +} diff --git a/xps/xps_gradient.c b/xps/xps_gradient.c index 0c73fd65..4dcf1ef8 100644 --- a/xps/xps_gradient.c +++ b/xps/xps_gradient.c @@ -227,7 +227,7 @@ xps_draw_one_radial_gradient(xps_context *ctx, fz_matrix ctm, shade->mesh_len = 6; shade->mesh_cap = 6; - shade->mesh = fz_calloc(ctx->ctx, shade->mesh_cap, sizeof(float)); + shade->mesh = fz_malloc_array(ctx->ctx, shade->mesh_cap, sizeof(float)); shade->mesh[0] = x0; shade->mesh[1] = y0; shade->mesh[2] = r0; @@ -268,7 +268,7 @@ xps_draw_one_linear_gradient(xps_context *ctx, fz_matrix ctm, shade->mesh_len = 6; shade->mesh_cap = 6; - shade->mesh = fz_calloc(ctx->ctx, shade->mesh_cap, sizeof(float)); + shade->mesh = fz_malloc_array(ctx->ctx, shade->mesh_cap, sizeof(float)); shade->mesh[0] = x0; shade->mesh[1] = y0; shade->mesh[2] = 0; diff --git a/xps/xps_path.c b/xps/xps_path.c index 89d88511..28548c56 100644 --- a/xps/xps_path.c +++ b/xps/xps_path.c @@ -234,7 +234,7 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule) path = fz_new_path(ctx->ctx); - args = fz_calloc(ctx->ctx, strlen(geom) + 1, sizeof(char*)); + args = fz_malloc_array(ctx->ctx, strlen(geom) + 1, sizeof(char*)); pargs = args; while (*s) diff --git a/xps/xps_png.c b/xps/xps_png.c index 5a866dcc..4c8b5150 100644 --- a/xps/xps_png.c +++ b/xps/xps_png.c @@ -62,7 +62,7 @@ static const unsigned char png_signature[8] = static void *zalloc(void *opaque, unsigned int items, unsigned int size) { - return fz_calloc(opaque, items, size); + return fz_malloc_array(opaque, items, size); } static void zfree(void *opaque, void *address) @@ -189,7 +189,7 @@ png_deinterlace(struct info *info, int *passw, int *passh, int *passofs) unsigned char *output; int p, x, y, k; - output = fz_calloc(info->ctx, info->height, stride); + output = fz_malloc_array(info->ctx, info->height, stride); for (p = 0; p < 7; p++) { diff --git a/xps/xps_tiff.c b/xps/xps_tiff.c index faef1ca7..a777fdaa 100644 --- a/xps/xps_tiff.c +++ b/xps/xps_tiff.c @@ -430,7 +430,7 @@ xps_decode_tiff_strips(struct tiff *tiff) tiff->yresolution = 96; } - tiff->samples = fz_calloc(tiff->ctx, tiff->imagelength, tiff->stride); + tiff->samples = fz_malloc_array(tiff->ctx, tiff->imagelength, tiff->stride); memset(tiff->samples, 0x55, tiff->imagelength * tiff->stride); wp = tiff->samples; @@ -689,17 +689,17 @@ xps_read_tiff_tag(struct tiff *tiff, unsigned offset) break; case StripOffsets: - tiff->stripoffsets = fz_calloc(tiff->ctx, count, sizeof(unsigned)); + tiff->stripoffsets = fz_malloc_array(tiff->ctx, count, sizeof(unsigned)); xps_read_tiff_tag_value(tiff->stripoffsets, tiff, type, value, count); break; case StripByteCounts: - tiff->stripbytecounts = fz_calloc(tiff->ctx, count, sizeof(unsigned)); + tiff->stripbytecounts = fz_malloc_array(tiff->ctx, count, sizeof(unsigned)); xps_read_tiff_tag_value(tiff->stripbytecounts, tiff, type, value, count); break; case ColorMap: - tiff->colormap = fz_calloc(tiff->ctx, count, sizeof(unsigned)); + tiff->colormap = fz_malloc_array(tiff->ctx, count, sizeof(unsigned)); xps_read_tiff_tag_value(tiff->colormap, tiff, type, value, count); break; diff --git a/xps/xps_zip.c b/xps/xps_zip.c index ad1c30b1..880db0d4 100644 --- a/xps/xps_zip.c +++ b/xps/xps_zip.c @@ -44,7 +44,7 @@ static inline int getlong(fz_stream *file) static void * xps_zip_alloc_items(xps_context *ctx, int items, int size) { - return fz_calloc(ctx->ctx, items, size); + return fz_malloc_array(ctx->ctx, items, size); } static void @@ -177,7 +177,7 @@ xps_read_zip_dir(xps_context *ctx, int start_offset) offset = getlong(ctx->file); /* offset to central directory */ ctx->zip_count = count; - ctx->zip_table = fz_calloc(ctx->ctx, count, sizeof(xps_entry)); + ctx->zip_table = fz_malloc_array(ctx->ctx, count, sizeof(xps_entry)); fz_seek(ctx->file, offset, 0); @@ -395,7 +395,8 @@ xps_open_directory(fz_context *fctx, xps_context **ctxp, char *directory) xps_context *ctx; int code; - ctx = fz_calloc(fctx, 1, sizeof(xps_context)); + ctx = fz_malloc(fctx, sizeof(xps_context)); + memset(ctx, 0, sizeof *ctx); ctx->directory = fz_strdup(fctx, directory); ctx->ctx = fctx; @@ -417,7 +418,8 @@ xps_open_stream(xps_context **ctxp, fz_stream *file) xps_context *ctx; int code; - ctx = fz_calloc(file->ctx, 1, sizeof(xps_context)); + ctx = fz_malloc(file->ctx, sizeof(xps_context)); + memset(ctx, 0, sizeof *ctx); ctx->ctx = file->ctx; ctx->file = fz_keep_stream(file); |