From fc9dbdec311d643fe020557f5aa0bde045b27186 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Wed, 24 Feb 2016 17:48:43 +0100 Subject: Remove pdf_res struct. Use pdf_obj indirect references directly. Fix refcounting bugs. --- include/mupdf/pdf/font.h | 4 +-- include/mupdf/pdf/resource.h | 16 +++-------- source/pdf/pdf-device.c | 36 ++++++++++++------------- source/pdf/pdf-font.c | 26 +++++++----------- source/pdf/pdf-image.c | 13 ++++----- source/pdf/pdf-resources.c | 63 +++++++++++++------------------------------- source/tools/pdfcreate.c | 12 ++++----- 7 files changed, 63 insertions(+), 107 deletions(-) diff --git a/include/mupdf/pdf/font.h b/include/mupdf/pdf/font.h index f472f188..40a1a3e5 100644 --- a/include/mupdf/pdf/font.h +++ b/include/mupdf/pdf/font.h @@ -122,8 +122,8 @@ float pdf_text_stride(fz_context *ctx, pdf_font_desc *fontdesc, float fontsize, void pdf_run_glyph(fz_context *ctx, pdf_document *doc, pdf_obj *resources, fz_buffer *contents, fz_device *dev, const fz_matrix *ctm, void *gstate, int nestedDepth); -pdf_res* pdf_add_simple_font_res(fz_context *ctx, pdf_document *doc, fz_buffer *buffer); -pdf_res* pdf_add_cid_font_res(fz_context *ctx, pdf_document *doc, fz_buffer *buffer, fz_font *font); +pdf_obj *pdf_add_simple_font_res(fz_context *ctx, pdf_document *doc, fz_buffer *buffer); +pdf_obj *pdf_add_cid_font_res(fz_context *ctx, pdf_document *doc, fz_buffer *buffer, fz_font *font); int pdf_font_writing_supported(fz_font *font); diff --git a/include/mupdf/pdf/resource.h b/include/mupdf/pdf/resource.h index fbcefc2e..41a8c27a 100644 --- a/include/mupdf/pdf/resource.h +++ b/include/mupdf/pdf/resource.h @@ -15,21 +15,13 @@ void pdf_remove_item(fz_context *ctx, fz_store_drop_fn *drop, pdf_obj *key); * refs are stored in an fz_hash_table structure using a hash of the md5 sum of * the data, enabling rapid lookup. */ -typedef struct pdf_res_s pdf_res; - -struct pdf_res_s -{ - int num; - pdf_obj *obj; -}; typedef struct pdf_res_table_s pdf_res_table; -typedef pdf_res *(pdf_res_search_fn)(fz_context *ctx, pdf_document *doc, pdf_res_table *list, +typedef pdf_obj *(pdf_res_search_fn)(fz_context *ctx, pdf_document *doc, pdf_res_table *list, void *item, unsigned char md5[16]); struct pdf_res_table_s { - int count; fz_hash_table *hash; pdf_res_search_fn *search; }; @@ -43,10 +35,10 @@ struct pdf_resource_tables_s pdf_res_table *shading; }; -pdf_res *pdf_find_resource(fz_context *ctx, pdf_document *doc, pdf_res_table *table, void *item, unsigned char md5[16]); +pdf_obj *pdf_find_resource(fz_context *ctx, pdf_document *doc, pdf_res_table *table, void *item, unsigned char md5[16]); void pdf_init_resource_tables(fz_context *ctx, pdf_document *doc); void pdf_drop_resource_tables(fz_context *ctx, pdf_document *doc); -pdf_res *pdf_insert_resource(fz_context *ctx, pdf_res_table *table, void *key, pdf_obj *obj); +pdf_obj *pdf_insert_resource(fz_context *ctx, pdf_res_table *table, void *key, pdf_obj *obj); /* * Functions, Colorspaces, Shadings and Images @@ -64,7 +56,7 @@ int pdf_is_jpx_image(fz_context *ctx, pdf_obj *dict); fz_image *pdf_load_image(fz_context *ctx, pdf_document *doc, pdf_obj *obj); -pdf_res* pdf_add_image_res(fz_context *ctx, pdf_document *doc, fz_image *image, int mask); +pdf_obj* pdf_add_image_res(fz_context *ctx, pdf_document *doc, fz_image *image, int mask); /* * Pattern diff --git a/source/pdf/pdf-device.c b/source/pdf/pdf-device.c index 56e37270..29bfdbf8 100644 --- a/source/pdf/pdf-device.c +++ b/source/pdf/pdf-device.c @@ -354,7 +354,7 @@ pdf_dev_alpha(fz_context *ctx, pdf_device *pdev, float alpha, int stroke) } static void -pdf_dev_add_font_res(fz_context *ctx, fz_device *dev, pdf_res *fres) +pdf_dev_add_font_res(fz_context *ctx, fz_device *dev, pdf_obj *fres) { char text[32]; pdf_device *pdev = (pdf_device*)dev; @@ -364,13 +364,13 @@ pdf_dev_add_font_res(fz_context *ctx, fz_device *dev, pdf_res *fres) /* Check if we already had this one */ for (k = 0; k < pdev->num_cid_fonts; k++) { - if (pdev->font_indices[k] == fres->num) + if (pdev->font_indices[k] == pdf_to_num(ctx, fres)) return; } /* Not there so add to resources */ - snprintf(text, sizeof(text), "Font/F%d", fres->num); - pdf_dict_putp(ctx, pdev->resources, text, fres->obj); + snprintf(text, sizeof(text), "Font/F%d", pdf_to_num(ctx, fres)); + pdf_dict_putp(ctx, pdev->resources, text, fres); /* And add index to our list for this page */ if (pdev->num_cid_fonts == pdev->max_cid_fonts) @@ -382,7 +382,7 @@ pdf_dev_add_font_res(fz_context *ctx, fz_device *dev, pdf_res *fres) pdev->max_cid_fonts = newmax; } num = pdev->num_cid_fonts++; - pdev->font_indices[num] = fres->num; + pdev->font_indices[num] = pdf_to_num(ctx, fres); } static void @@ -391,7 +391,7 @@ pdf_dev_font(fz_context *ctx, pdf_device *pdev, fz_font *font, float size) int i; pdf_document *doc = pdev->doc; gstate *gs = CURRENT_GSTATE(pdev); - pdf_res *fres; + pdf_obj *fres; /* If the font is unchanged, nothing to do */ if (gs->font >= 0 && pdev->fonts[gs->font].font == font) @@ -407,11 +407,11 @@ pdf_dev_font(fz_context *ctx, pdf_device *pdev, fz_font *font, float size) { /* This will add it to the xref if needed */ fres = pdf_add_cid_font_res(ctx, doc, font->ft_buffer, font); - fz_buffer_printf(ctx, gs->buf, "/F%d %f Tf\n", fres->num, size); + fz_buffer_printf(ctx, gs->buf, "/F%d %f Tf\n", pdf_to_num(ctx, fres), size); /* Possibly add to page resources */ pdf_dev_add_font_res(ctx, (fz_device*) pdev, fres); - pdf_drop_obj(ctx, fres->obj); + pdf_drop_obj(ctx, fres); } else { @@ -904,7 +904,7 @@ pdf_dev_ignore_text(fz_context *ctx, fz_device *dev, const fz_text *text, const } static void -pdf_dev_add_image_res(fz_context *ctx, fz_device *dev, pdf_res *im_res) +pdf_dev_add_image_res(fz_context *ctx, fz_device *dev, pdf_obj *im_res) { char text[32]; pdf_device *pdev = (pdf_device*)dev; @@ -914,13 +914,13 @@ pdf_dev_add_image_res(fz_context *ctx, fz_device *dev, pdf_res *im_res) /* Check if we already had this one */ for (k = 0; k < pdev->num_imgs; k++) { - if (pdev->image_indices[k] == im_res->num) + if (pdev->image_indices[k] == pdf_to_num(ctx, im_res)) return; } /* Not there so add to resources */ - snprintf(text, sizeof(text), "XObject/Img%d", im_res->num); - pdf_dict_putp(ctx, pdev->resources, text, im_res->obj); + snprintf(text, sizeof(text), "XObject/Img%d", pdf_to_num(ctx, im_res)); + pdf_dict_putp(ctx, pdev->resources, text, im_res); /* And add index to our list for this page */ if (pdev->num_imgs == pdev->max_imgs) @@ -932,14 +932,14 @@ pdf_dev_add_image_res(fz_context *ctx, fz_device *dev, pdf_res *im_res) pdev->max_imgs = newmax; } num = pdev->num_imgs++; - pdev->image_indices[num] = im_res->num; + pdev->image_indices[num] = pdf_to_num(ctx, im_res); } static void pdf_dev_fill_image(fz_context *ctx, fz_device *dev, fz_image *image, const fz_matrix *ctm, float alpha) { pdf_device *pdev = (pdf_device*)dev; - pdf_res *im_res; + pdf_obj *im_res; gstate *gs = CURRENT_GSTATE(pdev); fz_matrix local_ctm = *ctm; @@ -956,11 +956,11 @@ pdf_dev_fill_image(fz_context *ctx, fz_device *dev, fz_image *image, const fz_ma fz_pre_scale(&local_ctm, 1, -1); fz_pre_translate(&local_ctm, 0, -1); pdf_dev_ctm(ctx, pdev, &local_ctm); - fz_buffer_printf(ctx, gs->buf, "/Img%d Do\n", im_res->num); + fz_buffer_printf(ctx, gs->buf, "/Img%d Do\n", pdf_to_num(ctx, im_res)); /* Possibly add to page resources */ pdf_dev_add_image_res(ctx, dev, im_res); - pdf_drop_obj(ctx, im_res->obj); + pdf_drop_obj(ctx, im_res); } static void @@ -977,7 +977,7 @@ pdf_dev_fill_image_mask(fz_context *ctx, fz_device *dev, fz_image *image, const fz_colorspace *colorspace, const float *color, float alpha) { pdf_device *pdev = (pdf_device*)dev; - pdf_res* im_res = NULL; + pdf_obj *im_res = NULL; gstate *gs = CURRENT_GSTATE(pdev); fz_matrix local_ctm = *ctm; @@ -996,7 +996,7 @@ pdf_dev_fill_image_mask(fz_context *ctx, fz_device *dev, fz_image *image, const fz_pre_scale(&local_ctm, 1, -1); fz_pre_translate(&local_ctm, 0, -1); pdf_dev_ctm(ctx, pdev, &local_ctm); - fz_buffer_printf(ctx, gs->buf, "/Img%d Do Q\n", im_res->num); + fz_buffer_printf(ctx, gs->buf, "/Img%d Do Q\n", pdf_to_num(ctx, im_res)); } static void diff --git a/source/pdf/pdf-font.c b/source/pdf/pdf-font.c index ee3d7b9d..8fc01f0d 100644 --- a/source/pdf/pdf-font.c +++ b/source/pdf/pdf-font.c @@ -2122,12 +2122,11 @@ pdf_add_cid_to_unicode(fz_context *ctx, pdf_document *doc, pdf_font_desc *fontde * If this is coming from a source file, we have source_font so that we can * possibly get any width information that may have been embedded in the PDF * W name tag (or W2 if vertical text) */ -pdf_res* +pdf_obj * pdf_add_cid_font_res(fz_context *ctx, pdf_document *doc, fz_buffer *buffer, fz_font *source_font) { pdf_obj *fobj = NULL; pdf_obj *fref = NULL; - pdf_res *fres = NULL; pdf_obj *obj_desc_ref = NULL; pdf_obj *obj_tounicode_ref = NULL; pdf_obj *obj_array = NULL; @@ -2140,7 +2139,6 @@ pdf_add_cid_font_res(fz_context *ctx, pdf_document *doc, fz_buffer *buffer, fz_f fz_var(fobj); fz_var(fref); - fz_var(fres); fz_var(obj_desc_ref); fz_var(obj_tounicode_ref); fz_var(fontdesc); @@ -2153,8 +2151,8 @@ pdf_add_cid_font_res(fz_context *ctx, pdf_document *doc, fz_buffer *buffer, fz_f /* Before we add this font as a resource check if the same font * already exists in our resources for this doc. If yes, then * hand back that reference */ - fres = pdf_find_resource(ctx, doc, doc->resources->font, buffer, digest); - if (fres == NULL) + fref = pdf_find_resource(ctx, doc, doc->resources->font, buffer, digest); + if (fref == NULL) { /* Set up desc, width, and font file */ font = fz_new_font_from_memory(ctx, NULL, buffer->data, buffer->len, 0, 1); @@ -2188,12 +2186,11 @@ pdf_add_cid_font_res(fz_context *ctx, pdf_document *doc, fz_buffer *buffer, fz_f fref = pdf_new_ref(ctx, doc, fobj); /* Add ref to our font resource hash table. */ - fres = pdf_insert_resource(ctx, doc->resources->font, digest, fref); + fref = pdf_insert_resource(ctx, doc->resources->font, digest, fref); } } fz_always(ctx) { - fz_drop_font(ctx, font); if (fontdesc != NULL) fontdesc->font = NULL; pdf_drop_font(ctx, fontdesc); @@ -2209,16 +2206,15 @@ pdf_add_cid_font_res(fz_context *ctx, pdf_document *doc, fz_buffer *buffer, fz_f pdf_drop_obj(ctx, fref); fz_rethrow(ctx); } - return fres; + return fref; } /* Creates simple font */ -pdf_res* +pdf_obj * pdf_add_simple_font_res(fz_context *ctx, pdf_document *doc, fz_buffer *buffer) { pdf_obj *fobj = NULL; pdf_obj *fref = NULL; - pdf_res *fres = NULL; pdf_obj *fstr_ref = NULL; pdf_obj *fdes_ref = NULL; pdf_obj *fwidth_ref = NULL; @@ -2233,7 +2229,6 @@ pdf_add_simple_font_res(fz_context *ctx, pdf_document *doc, fz_buffer *buffer) fz_var(fobj); fz_var(fref); - fz_var(fres); fz_var(fstr_ref); fz_var(fdes_ref); fz_var(fwidth_ref); @@ -2246,8 +2241,8 @@ pdf_add_simple_font_res(fz_context *ctx, pdf_document *doc, fz_buffer *buffer) /* Before we add this font as a resource check if the same font * already exists in our resources for this doc. If yes, then * hand back that reference */ - fres = pdf_find_resource(ctx, doc, doc->resources->font, buffer, digest); - if (fres == NULL) + fref = pdf_find_resource(ctx, doc, doc->resources->font, buffer, digest); + if (fref == NULL) { /* Set up desc, width, and font file */ fobj = pdf_new_dict(ctx, doc, 3); @@ -2297,12 +2292,11 @@ pdf_add_simple_font_res(fz_context *ctx, pdf_document *doc, fz_buffer *buffer) fref = pdf_new_ref(ctx, doc, fobj); /* Add ref to our font resource hash table. */ - fres = pdf_insert_resource(ctx, doc->resources->font, digest, fref); + fref = pdf_insert_resource(ctx, doc->resources->font, digest, fref); } } fz_always(ctx) { - fz_drop_font(ctx, font); if (fontdesc != NULL) fontdesc->font = NULL; pdf_drop_font(ctx, fontdesc); @@ -2318,7 +2312,7 @@ pdf_add_simple_font_res(fz_context *ctx, pdf_document *doc, fz_buffer *buffer) pdf_drop_obj(ctx, fref); fz_rethrow(ctx); } - return fres; + return fref; } int diff --git a/source/pdf/pdf-image.c b/source/pdf/pdf-image.c index 9641520a..ef26a9b1 100644 --- a/source/pdf/pdf-image.c +++ b/source/pdf/pdf-image.c @@ -290,13 +290,12 @@ pdf_load_image(fz_context *ctx, pdf_document *doc, pdf_obj *dict) return image; } -pdf_res* +pdf_obj * pdf_add_image_res(fz_context *ctx, pdf_document *doc, fz_image *image, int mask) { fz_pixmap *pixmap = NULL; pdf_obj *imobj = NULL; pdf_obj *imref = NULL; - pdf_res *imres = NULL; fz_compressed_buffer *cbuffer = NULL; fz_compression_params *cp = NULL; fz_buffer *buffer = NULL; @@ -310,16 +309,14 @@ pdf_add_image_res(fz_context *ctx, pdf_document *doc, fz_image *image, int mask) fz_var(buffer); fz_var(imobj); fz_var(imref); - fz_var(imres); fz_try(ctx) { /* Before we add this image as a resource check if the same image * already exists in our resources for this doc. If yes, then * hand back that reference */ - imres = pdf_find_resource(ctx, doc, doc->resources->image, - (void*)image, (void*) &(digest[0])); - if (imres == NULL) + imref = pdf_find_resource(ctx, doc, doc->resources->image, image, digest); + if (imref == NULL) { if (cbuffer != NULL && cbuffer->params.type != FZ_IMAGE_PNG && cbuffer->params.type != FZ_IMAGE_TIFF) { @@ -448,7 +445,7 @@ pdf_add_image_res(fz_context *ctx, pdf_document *doc, fz_image *image, int mask) pdf_update_stream(ctx, doc, imref, buffer, 1); /* Add ref to our image resource hash table. */ - imres = pdf_insert_resource(ctx, doc->resources->image, digest, imref); + imref = pdf_insert_resource(ctx, doc->resources->image, digest, imref); } } fz_always(ctx) @@ -462,5 +459,5 @@ pdf_add_image_res(fz_context *ctx, pdf_document *doc, fz_image *image, int mask) pdf_drop_obj(ctx, imref); fz_rethrow(ctx); } - return imres; + return imref; } diff --git a/source/pdf/pdf-resources.c b/source/pdf/pdf-resources.c index 192e5893..577a6cc1 100644 --- a/source/pdf/pdf-resources.c +++ b/source/pdf/pdf-resources.c @@ -4,7 +4,7 @@ static void res_table_free(fz_context *ctx, pdf_res_table *table) { int i, n; - pdf_res *res; + pdf_obj *res; if (table == NULL) return; @@ -15,10 +15,7 @@ res_table_free(fz_context *ctx, pdf_res_table *table) { res = fz_hash_get_val(ctx, table->hash, i); if (res) - { - pdf_drop_obj(ctx, res->obj); - fz_free(ctx, res); - } + pdf_drop_obj(ctx, res); } fz_drop_hash(ctx, table->hash); } @@ -83,10 +80,9 @@ res_image_init(fz_context *ctx, pdf_document *doc, pdf_res_table *table) int len, k; pdf_obj *obj; pdf_obj *type; - pdf_res *res = NULL; + pdf_obj *res = NULL; fz_image *image = NULL; unsigned char digest[16]; - int num = 0; fz_var(obj); fz_var(image); @@ -107,15 +103,9 @@ res_image_init(fz_context *ctx, pdf_document *doc, pdf_res_table *table) fz_drop_image(ctx, image); image = NULL; - /* Don't allow overwrites. Number the resources for pdfwrite */ + /* Don't allow overwrites. */ if (fz_hash_find(ctx, table->hash, digest) == NULL) - { - res = fz_malloc(ctx, sizeof(pdf_res)); - res->num = num; - res->obj = obj; - num = num + 1; fz_hash_insert(ctx, table->hash, digest, obj); - } } else { @@ -126,23 +116,22 @@ res_image_init(fz_context *ctx, pdf_document *doc, pdf_res_table *table) } fz_always(ctx) { - table->count = num; fz_drop_image(ctx, image); pdf_drop_obj(ctx, obj); } fz_catch(ctx) { res_table_free(ctx, table); - fz_rethrow_message(ctx, "image resources table failed to initialize"); + fz_rethrow_message(ctx, "cannot initialize image resource table"); } } -static pdf_res * +static pdf_obj * res_image_search(fz_context *ctx, pdf_document *doc, pdf_res_table *table, void *item, unsigned char *digest) { fz_image *image = item; fz_hash_table *hash = table->hash; - pdf_res *res; + pdf_obj *res; if (hash == NULL) res_image_init(ctx, doc, doc->resources->image); @@ -152,7 +141,7 @@ res_image_search(fz_context *ctx, pdf_document *doc, pdf_res_table *table, void res_image_get_md5(ctx, image, digest); res = fz_hash_find(ctx, hash, digest); if (res) - pdf_keep_obj(ctx, res->obj); + pdf_keep_obj(ctx, res); return res; } @@ -178,12 +167,12 @@ res_font_get_md5(fz_context *ctx, fz_buffer *buffer, unsigned char *digest) fz_md5_final(&state, digest); } -static pdf_res * +static pdf_obj * res_font_search(fz_context *ctx, pdf_document *doc, pdf_res_table *table, void *item, unsigned char digest[16]) { fz_buffer *buffer = item; fz_hash_table *hash = table->hash; - pdf_res *res; + pdf_obj *res; if (hash == NULL) res_font_init(ctx, doc, doc->resources->font); @@ -193,49 +182,35 @@ res_font_search(fz_context *ctx, pdf_document *doc, pdf_res_table *table, void * res_font_get_md5(ctx, buffer, digest); res = fz_hash_find(ctx, hash, digest); if (res) - pdf_keep_obj(ctx, res->obj); + pdf_keep_obj(ctx, res); return res; } /* Accessible methods */ -pdf_res * +pdf_obj * pdf_find_resource(fz_context *ctx, pdf_document *doc, pdf_res_table *table, void *item, unsigned char md5[16]) { return table->search(ctx, doc, table, item, md5); } -pdf_res * +pdf_obj * pdf_insert_resource(fz_context *ctx, pdf_res_table *table, void *key, pdf_obj *obj) { - void *result; - pdf_res *res = NULL; - - fz_var(res); + pdf_obj *res; fz_try(ctx) { - res = fz_malloc(ctx, sizeof(pdf_res)); - res->num = table->count + 1; - res->obj = obj; - result = fz_hash_insert(ctx, table->hash, key, (void*)res); - if (result != NULL) - { - fz_free(ctx, res); - fz_warn(ctx, "warning: hash already present"); - } + res = fz_hash_insert(ctx, table->hash, key, obj); + if (res != NULL) + fz_warn(ctx, "warning: resource already present"); else - { - table->count = table->count + 1; - pdf_keep_obj(ctx, obj); - result = res; - } + res = pdf_keep_obj(ctx, obj); } fz_catch(ctx) { - fz_free(ctx, res); fz_rethrow(ctx); } - return result; + return res; } void diff --git a/source/tools/pdfcreate.c b/source/tools/pdfcreate.c index 6317e304..e36e0e68 100644 --- a/source/tools/pdfcreate.c +++ b/source/tools/pdfcreate.c @@ -310,8 +310,8 @@ static int create_pdf(fz_context *ctx, char *output, resources fonts[], int num_ fz_image *image = NULL; pdf_obj *font_dict = NULL; pdf_obj *im_dict = NULL; - pdf_res *im_res = NULL; - pdf_res *font_res = NULL; + pdf_obj *im_res = NULL; + pdf_obj *font_res = NULL; pdf_write_options opts = { 0 }; doc_content content = { 0 }; int k; @@ -356,11 +356,9 @@ static int create_pdf(fz_context *ctx, char *output, resources fonts[], int num_ image = NULL; /* Look through our image page resources and update the - * indirect reference number. Here we don't use the numbers - * set by the doc resource holder (i.e im_res->num) since we - * are using our own content specified for pdfcreate */ + * indirect reference number. */ update_res(ctx, content.num_pages, content.num_page_im_res, - content.ref_im_resources, content.images[k].name, im_res->obj); + content.ref_im_resources, content.images[k].name, im_res); } else fz_throw(ctx, FZ_ERROR_GENERIC, "Image indirect name too long"); @@ -381,7 +379,7 @@ static int create_pdf(fz_context *ctx, char *output, resources fonts[], int num_ /* Look through our font page resources and update the indirect * reference number */ update_res(ctx, content.num_pages, content.num_page_font_res, - content.ref_font_resources, content.fonts[k].name, font_res->obj); + content.ref_font_resources, content.fonts[k].name, font_res); } else fz_throw(ctx, FZ_ERROR_GENERIC, "Font indirect name too long"); -- cgit v1.2.3