summaryrefslogtreecommitdiff
path: root/source/fitz
diff options
context:
space:
mode:
authorTor Andersson <tor.andersson@artifex.com>2016-07-08 12:04:35 +0200
committerTor Andersson <tor.andersson@artifex.com>2016-07-08 17:21:24 +0200
commit10d6eaa73164b58c91ae8a4537b8a8589038a01d (patch)
tree5b739b0c2202b42a9079ee3ecbd98c3d3b09b124 /source/fitz
parentb53e7a42f7cc9756ed9fa1fed313271e3ae67855 (diff)
downloadmupdf-10d6eaa73164b58c91ae8a4537b8a8589038a01d.tar.xz
Separate close and drop functionality for devices and writers.
Closing a device or writer may throw exceptions, but much of the foreign language bindings (JNI and JS) depend on drop to never throw an exception (exceptions in finalizers are bad).
Diffstat (limited to 'source/fitz')
-rw-r--r--source/fitz/bbox-device.c4
-rw-r--r--source/fitz/device.c12
-rw-r--r--source/fitz/document.c12
-rw-r--r--source/fitz/draw-device.c4
-rw-r--r--source/fitz/font.c3
-rw-r--r--source/fitz/image.c1
-rw-r--r--source/fitz/list-device.c4
-rw-r--r--source/fitz/output-cbz.c23
-rw-r--r--source/fitz/output-png.c7
-rw-r--r--source/fitz/stext-device.c16
-rw-r--r--source/fitz/svg-device.c13
-rw-r--r--source/fitz/util.c9
-rw-r--r--source/fitz/writer.c12
-rw-r--r--source/fitz/zip.c48
14 files changed, 104 insertions, 64 deletions
diff --git a/source/fitz/bbox-device.c b/source/fitz/bbox-device.c
index 42ca57ad..340eef3c 100644
--- a/source/fitz/bbox-device.c
+++ b/source/fitz/bbox-device.c
@@ -179,7 +179,7 @@ fz_bbox_end_tile(fz_context *ctx, fz_device *dev)
}
static void
-fz_bbox_close(fz_context *ctx, fz_device *dev)
+fz_bbox_drop_device(fz_context *ctx, fz_device *dev)
{
fz_bbox_device *bdev = (fz_bbox_device*)dev;
if (bdev->top > 0)
@@ -191,7 +191,7 @@ fz_new_bbox_device(fz_context *ctx, fz_rect *result)
{
fz_bbox_device *dev = fz_new_device(ctx, sizeof *dev);
- dev->super.close = fz_bbox_close;
+ dev->super.drop_device = fz_bbox_drop_device;
dev->super.fill_path = fz_bbox_fill_path;
dev->super.stroke_path = fz_bbox_stroke_path;
diff --git a/source/fitz/device.c b/source/fitz/device.c
index 4379bb21..88b658a3 100644
--- a/source/fitz/device.c
+++ b/source/fitz/device.c
@@ -13,11 +13,11 @@ fz_close_device(fz_context *ctx, fz_device *dev)
{
if (dev == NULL)
return;
- if (dev->close)
- dev->close(ctx, dev);
+ if (dev->close_device)
+ dev->close_device(ctx, dev);
/* Don't call more than once! */
- dev->close = NULL;
+ dev->close_device = NULL;
/* And disable all further device calls. */
dev->fill_path = NULL;
@@ -53,8 +53,10 @@ fz_drop_device(fz_context *ctx, fz_device *dev)
{
if (fz_drop_imp(ctx, dev, &dev->refs))
{
- if (dev->close)
- dev->close(ctx, dev);
+ if (dev->close_device)
+ fz_warn(ctx, "dropping unclosed device");
+ if (dev->drop_device)
+ dev->drop_device(ctx, dev);
fz_free(ctx, dev->container);
fz_free(ctx, dev);
}
diff --git a/source/fitz/document.c b/source/fitz/document.c
index 02c30459..ad602bc2 100644
--- a/source/fitz/document.c
+++ b/source/fitz/document.c
@@ -150,8 +150,8 @@ fz_keep_document(fz_context *ctx, fz_document *doc)
void
fz_drop_document(fz_context *ctx, fz_document *doc)
{
- if (doc && --doc->refs == 0 && doc->close)
- doc->close(ctx, doc);
+ if (doc && --doc->refs == 0 && doc->drop_document)
+ doc->drop_document(ctx, doc);
}
static void
@@ -362,8 +362,8 @@ fz_drop_annot(fz_context *ctx, fz_annot *annot)
{
if (annot && --annot->refs == 0)
{
- if (annot->drop_annot_imp)
- annot->drop_annot_imp(ctx, annot);
+ if (annot->drop_annot)
+ annot->drop_annot(ctx, annot);
fz_free(ctx, annot);
}
}
@@ -389,9 +389,9 @@ fz_drop_page(fz_context *ctx, fz_page *page)
{
if (page)
{
- if (--page->refs == 0 && page->drop_page_imp)
+ if (--page->refs == 0 && page->drop_page)
{
- page->drop_page_imp(ctx, page);
+ page->drop_page(ctx, page);
fz_free(ctx, page);
}
}
diff --git a/source/fitz/draw-device.c b/source/fitz/draw-device.c
index 9a2cb507..ff746e29 100644
--- a/source/fitz/draw-device.c
+++ b/source/fitz/draw-device.c
@@ -2357,7 +2357,7 @@ fz_draw_end_tile(fz_context *ctx, fz_device *devp)
}
static void
-fz_draw_close(fz_context *ctx, fz_device *devp)
+fz_draw_drop_device(fz_context *ctx, fz_device *devp)
{
fz_draw_device *dev = (fz_draw_device*)devp;
fz_gel *gel = dev->gel;
@@ -2400,7 +2400,7 @@ fz_new_draw_device(fz_context *ctx, const fz_matrix *transform, fz_pixmap *dest)
{
fz_draw_device *dev = fz_new_device(ctx, sizeof *dev);
- dev->super.close = fz_draw_close;
+ dev->super.drop_device = fz_draw_drop_device;
dev->super.fill_path = fz_draw_fill_path;
dev->super.stroke_path = fz_draw_stroke_path;
diff --git a/source/fitz/font.c b/source/fitz/font.c
index b0248bd7..79cd050a 100644
--- a/source/fitz/font.c
+++ b/source/fitz/font.c
@@ -1169,6 +1169,7 @@ fz_prepare_t3_glyph(fz_context *ctx, fz_font *font, int gid, int nested_depth)
font->bbox_table[gid] = dev->d1_rect;
fz_transform_rect(&font->bbox_table[gid], &font->t3matrix);
}
+ fz_close_device(ctx, dev);
fz_drop_device(ctx, dev);
}
@@ -1191,6 +1192,7 @@ fz_bound_t3_glyph(fz_context *ctx, fz_font *font, int gid, fz_rect *bounds)
fz_try(ctx)
{
fz_run_display_list(ctx, list, dev, &font->t3matrix, &fz_infinite_rect, NULL);
+ fz_close_device(ctx, dev);
}
fz_always(ctx)
{
@@ -1270,6 +1272,7 @@ fz_render_t3_glyph_pixmap(fz_context *ctx, fz_font *font, int gid, const fz_matr
fz_try(ctx)
{
fz_run_t3_glyph(ctx, font, gid, trm, dev);
+ fz_close_device(ctx, dev);
}
fz_always(ctx)
{
diff --git a/source/fitz/image.c b/source/fitz/image.c
index 064b03c7..330b65cf 100644
--- a/source/fitz/image.c
+++ b/source/fitz/image.c
@@ -1085,6 +1085,7 @@ display_list_image_get_pixmap(fz_context *ctx, fz_image *image_, fz_irect *subar
fz_clear_pixmap(ctx, pix); /* clear to transparent */
dev = fz_new_draw_device(ctx, &ctm, pix);
fz_run_display_list(ctx, image->list, dev, &fz_identity, NULL, NULL);
+ fz_close_device(ctx, dev);
fz_drop_device(ctx, dev);
/* Never do more subsampling, cos we've already given them the right size */
diff --git a/source/fitz/list-device.c b/source/fitz/list-device.c
index 18af0e20..d5e13375 100644
--- a/source/fitz/list-device.c
+++ b/source/fitz/list-device.c
@@ -1199,7 +1199,7 @@ fz_list_render_flags(fz_context *ctx, fz_device *dev, int set, int clear)
}
static void
-drop_writer(fz_context *ctx, fz_device *dev)
+fz_list_drop_device(fz_context *ctx, fz_device *dev)
{
fz_list_device *writer = (fz_list_device *)dev;
@@ -1243,7 +1243,7 @@ fz_new_list_device(fz_context *ctx, fz_display_list *list)
dev->super.render_flags = fz_list_render_flags;
- dev->super.close = drop_writer;
+ dev->super.drop_device = fz_list_drop_device;
dev->list = list;
dev->path = NULL;
diff --git a/source/fitz/output-cbz.c b/source/fitz/output-cbz.c
index 80ef287c..43be16cb 100644
--- a/source/fitz/output-cbz.c
+++ b/source/fitz/output-cbz.c
@@ -29,6 +29,9 @@ cbz_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev)
fz_buffer *buffer;
char name[40];
+ fz_close_device(ctx, dev);
+ fz_drop_device(ctx, dev);
+
wri->count += 1;
fz_snprintf(name, sizeof name, "p%04d.png", wri->count);
@@ -43,20 +46,21 @@ cbz_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev)
fz_drop_pixmap(ctx, wri->pixmap);
wri->pixmap = NULL;
+}
- fz_drop_device(ctx, dev);
+static void
+cbz_close_writer(fz_context *ctx, fz_document_writer *wri_)
+{
+ fz_cbz_writer *wri = (fz_cbz_writer*)wri_;
+ fz_close_zip_writer(ctx, wri->zip);
}
static void
-cbz_close(fz_context *ctx, fz_document_writer *wri_)
+cbz_drop_writer(fz_context *ctx, fz_document_writer *wri_)
{
fz_cbz_writer *wri = (fz_cbz_writer*)wri_;
- fz_try(ctx)
- fz_drop_zip_writer(ctx, wri->zip);
- fz_always(ctx)
- fz_drop_pixmap(ctx, wri->pixmap);
- fz_catch(ctx)
- fz_rethrow(ctx);
+ fz_drop_zip_writer(ctx, wri->zip);
+ fz_drop_pixmap(ctx, wri->pixmap);
}
fz_document_writer *
@@ -67,7 +71,8 @@ fz_new_cbz_writer(fz_context *ctx, const char *path, const char *options)
wri = fz_malloc_struct(ctx, fz_cbz_writer);
wri->super.begin_page = cbz_begin_page;
wri->super.end_page = cbz_end_page;
- wri->super.close = cbz_close;
+ wri->super.close_writer = cbz_close_writer;
+ wri->super.drop_writer = cbz_drop_writer;
fz_try(ctx)
{
diff --git a/source/fitz/output-png.c b/source/fitz/output-png.c
index 5bd0737e..d710a0cd 100644
--- a/source/fitz/output-png.c
+++ b/source/fitz/output-png.c
@@ -324,6 +324,9 @@ png_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev)
fz_png_writer *wri = (fz_png_writer*)wri_;
char path[PATH_MAX];
+ fz_close_device(ctx, dev);
+ fz_drop_device(ctx, dev);
+
wri->count += 1;
fz_format_output_path(ctx, path, sizeof path, wri->path, wri->count);
@@ -333,7 +336,7 @@ png_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev)
}
static void
-png_close(fz_context *ctx, fz_document_writer *wri_)
+png_drop_writer(fz_context *ctx, fz_document_writer *wri_)
{
fz_png_writer *wri = (fz_png_writer*)wri_;
fz_drop_pixmap(ctx, wri->pixmap);
@@ -348,7 +351,7 @@ fz_new_png_writer(fz_context *ctx, const char *path, const char *options)
wri = fz_malloc_struct(ctx, fz_png_writer);
wri->super.begin_page = png_begin_page;
wri->super.end_page = png_end_page;
- wri->super.close = png_close;
+ wri->super.drop_writer = png_drop_writer;
fz_try(ctx)
{
diff --git a/source/fitz/stext-device.c b/source/fitz/stext-device.c
index f50bbf82..55f2033b 100644
--- a/source/fitz/stext-device.c
+++ b/source/fitz/stext-device.c
@@ -1015,7 +1015,7 @@ fz_bidi_reorder_stext_page(fz_context *ctx, fz_stext_page *page)
}
static void
-fz_stext_close(fz_context *ctx, fz_device *dev)
+fz_stext_close_device(fz_context *ctx, fz_device *dev)
{
fz_stext_device *tdev = (fz_stext_device*)dev;
@@ -1023,8 +1023,6 @@ fz_stext_close(fz_context *ctx, fz_device *dev)
tdev->cur_span = NULL;
strain_soup(ctx, tdev);
- free_span_soup(ctx, tdev->spans);
- tdev->spans = NULL;
/* TODO: smart sorting of blocks in reading order */
/* TODO: unicode NFC normalization */
@@ -1032,6 +1030,14 @@ fz_stext_close(fz_context *ctx, fz_device *dev)
fz_bidi_reorder_stext_page(ctx, tdev->page);
}
+static void
+fz_stext_drop_device(fz_context *ctx, fz_device *dev)
+{
+ fz_stext_device *tdev = (fz_stext_device*)dev;
+ free_span_soup(ctx, tdev->spans);
+ tdev->spans = NULL;
+}
+
fz_device *
fz_new_stext_device(fz_context *ctx, fz_stext_sheet *sheet, fz_stext_page *page)
{
@@ -1039,7 +1045,9 @@ fz_new_stext_device(fz_context *ctx, fz_stext_sheet *sheet, fz_stext_page *page)
dev->super.hints = FZ_IGNORE_IMAGE | FZ_IGNORE_SHADE;
- dev->super.close = fz_stext_close;
+ dev->super.close_device = fz_stext_close_device;
+ dev->super.drop_device = fz_stext_drop_device;
+
dev->super.fill_text = fz_stext_fill_text;
dev->super.stroke_text = fz_stext_stroke_text;
dev->super.clip_text = fz_stext_clip_text;
diff --git a/source/fitz/svg-device.c b/source/fitz/svg-device.c
index 1a486545..8a7f4bf9 100644
--- a/source/fitz/svg-device.c
+++ b/source/fitz/svg-device.c
@@ -1063,23 +1063,28 @@ svg_dev_end_tile(fz_context *ctx, fz_device *dev)
}
static void
-svg_dev_close(fz_context *ctx, fz_device *dev)
+svg_dev_close_device(fz_context *ctx, fz_device *dev)
{
svg_device *sdev = (svg_device*)dev;
fz_output *out = sdev->out;
+ fz_printf(ctx, out, "</svg>\n");
+}
+static void
+svg_dev_drop_device(fz_context *ctx, fz_device *dev)
+{
+ svg_device *sdev = (svg_device*)dev;
fz_free(ctx, sdev->tiles);
fz_drop_buffer(ctx, sdev->defs_buffer);
fz_drop_output(ctx, sdev->defs);
-
- fz_printf(ctx, out, "</svg>\n");
}
fz_device *fz_new_svg_device(fz_context *ctx, fz_output *out, float page_width, float page_height)
{
svg_device *dev = fz_new_device(ctx, sizeof *dev);
- dev->super.close = svg_dev_close;
+ dev->super.close_device = svg_dev_close_device;
+ dev->super.drop_device = svg_dev_drop_device;
dev->super.fill_path = svg_dev_fill_path;
dev->super.stroke_path = svg_dev_stroke_path;
diff --git a/source/fitz/util.c b/source/fitz/util.c
index f76e496a..cefac962 100644
--- a/source/fitz/util.c
+++ b/source/fitz/util.c
@@ -13,6 +13,7 @@ fz_new_display_list_from_page(fz_context *ctx, fz_page *page)
{
dev = fz_new_list_device(ctx, list);
fz_run_page(ctx, page, dev, &fz_identity, NULL);
+ fz_close_device(ctx, dev);
}
fz_always(ctx)
{
@@ -56,6 +57,7 @@ fz_new_display_list_from_page_contents(fz_context *ctx, fz_page *page)
{
dev = fz_new_list_device(ctx, list);
fz_run_page_contents(ctx, page, dev, &fz_identity, NULL);
+ fz_close_device(ctx, dev);
}
fz_always(ctx)
{
@@ -83,6 +85,7 @@ fz_new_display_list_from_annot(fz_context *ctx, fz_annot *annot)
{
dev = fz_new_list_device(ctx, list);
fz_run_annot(ctx, annot, dev, &fz_identity, NULL);
+ fz_close_device(ctx, dev);
}
fz_always(ctx)
{
@@ -119,6 +122,7 @@ fz_new_pixmap_from_display_list(fz_context *ctx, fz_display_list *list, const fz
{
dev = fz_new_draw_device(ctx, ctm, pix);
fz_run_display_list(ctx, list, dev, &fz_identity, NULL, NULL);
+ fz_close_device(ctx, dev);
}
fz_always(ctx)
{
@@ -155,6 +159,7 @@ fz_new_pixmap_from_page_contents(fz_context *ctx, fz_page *page, const fz_matrix
{
dev = fz_new_draw_device(ctx, ctm, pix);
fz_run_page_contents(ctx, page, dev, &fz_identity, NULL);
+ fz_close_device(ctx, dev);
}
fz_always(ctx)
{
@@ -191,6 +196,7 @@ fz_new_pixmap_from_annot(fz_context *ctx, fz_annot *annot, const fz_matrix *ctm,
{
dev = fz_new_draw_device(ctx, ctm, pix);
fz_run_annot(ctx, annot, dev, &fz_identity, NULL);
+ fz_close_device(ctx, dev);
}
fz_always(ctx)
{
@@ -227,6 +233,7 @@ fz_new_pixmap_from_page(fz_context *ctx, fz_page *page, const fz_matrix *ctm, fz
{
dev = fz_new_draw_device(ctx, ctm, pix);
fz_run_page(ctx, page, dev, &fz_identity, NULL);
+ fz_close_device(ctx, dev);
}
fz_always(ctx)
{
@@ -268,6 +275,7 @@ fz_new_stext_page_from_display_list(fz_context *ctx, fz_display_list *list, fz_s
{
dev = fz_new_stext_device(ctx, sheet, text);
fz_run_display_list(ctx, list, dev, &fz_identity, NULL, NULL);
+ fz_close_device(ctx, dev);
}
fz_always(ctx)
{
@@ -293,6 +301,7 @@ fz_new_stext_page_from_page(fz_context *ctx, fz_page *page, fz_stext_sheet *shee
{
dev = fz_new_stext_device(ctx, sheet, text);
fz_run_page(ctx, page, dev, &fz_identity, NULL);
+ fz_close_device(ctx, dev);
}
fz_always(ctx)
{
diff --git a/source/fitz/writer.c b/source/fitz/writer.c
index 556e01a8..c4e95e2a 100644
--- a/source/fitz/writer.c
+++ b/source/fitz/writer.c
@@ -67,16 +67,18 @@ fz_new_document_writer(fz_context *ctx, const char *path, const char *format, co
void
fz_close_document_writer(fz_context *ctx, fz_document_writer *wri)
{
- if (wri->close)
- wri->close(ctx, wri);
- wri->close = NULL;
+ if (wri->close_writer)
+ wri->close_writer(ctx, wri);
+ wri->close_writer = NULL;
}
void
fz_drop_document_writer(fz_context *ctx, fz_document_writer *wri)
{
- if (wri->close)
- wri->close(ctx, wri);
+ if (wri->close_writer)
+ fz_warn(ctx, "dropping unclosed document writer");
+ if (wri->drop_writer)
+ wri->drop_writer(ctx, wri);
fz_free(ctx, wri);
}
diff --git a/source/fitz/zip.c b/source/fitz/zip.c
index dd67e5ac..599d2954 100644
--- a/source/fitz/zip.c
+++ b/source/fitz/zip.c
@@ -15,6 +15,7 @@ struct fz_zip_writer_s
fz_output *output;
fz_buffer *central;
int count;
+ int closed;
};
void
@@ -63,33 +64,34 @@ fz_write_zip_entry(fz_context *ctx, fz_zip_writer *zip, const char *name, fz_buf
}
void
-fz_drop_zip_writer(fz_context *ctx, fz_zip_writer *zip)
+fz_close_zip_writer(fz_context *ctx, fz_zip_writer *zip)
{
- fz_try(ctx)
- {
- fz_off_t offset = fz_tell_output(ctx, zip->output);
+ fz_off_t offset = fz_tell_output(ctx, zip->output);
- fz_write(ctx, zip->output, zip->central->data, zip->central->len);
+ fz_write(ctx, zip->output, zip->central->data, zip->central->len);
- fz_write_int32_le(ctx, zip->output, ZIP_END_OF_CENTRAL_DIRECTORY_SIG);
- fz_write_int16_le(ctx, zip->output, 0); /* number of this disk */
- fz_write_int16_le(ctx, zip->output, 0); /* number of disk where central directory starts */
- fz_write_int16_le(ctx, zip->output, zip->count); /* entries in central directory in this disk */
- fz_write_int16_le(ctx, zip->output, zip->count); /* entries in central directory in total */
- fz_write_int32_le(ctx, zip->output, (int)zip->central->len); /* size of the central directory */
- fz_write_int32_le(ctx, zip->output, (int)offset); /* offset of the central directory */
- fz_write_int16_le(ctx, zip->output, 5); /* zip file comment length */
+ fz_write_int32_le(ctx, zip->output, ZIP_END_OF_CENTRAL_DIRECTORY_SIG);
+ fz_write_int16_le(ctx, zip->output, 0); /* number of this disk */
+ fz_write_int16_le(ctx, zip->output, 0); /* number of disk where central directory starts */
+ fz_write_int16_le(ctx, zip->output, zip->count); /* entries in central directory in this disk */
+ fz_write_int16_le(ctx, zip->output, zip->count); /* entries in central directory in total */
+ fz_write_int32_le(ctx, zip->output, (int)zip->central->len); /* size of the central directory */
+ fz_write_int32_le(ctx, zip->output, (int)offset); /* offset of the central directory */
+ fz_write_int16_le(ctx, zip->output, 5); /* zip file comment length */
- fz_write(ctx, zip->output, "MuPDF", 5);
- }
- fz_always(ctx)
- {
- fz_drop_output(ctx, zip->output);
- fz_drop_buffer(ctx, zip->central);
- fz_free(ctx, zip);
- }
- fz_catch(ctx)
- fz_rethrow(ctx);
+ fz_write(ctx, zip->output, "MuPDF", 5);
+
+ zip->closed = 1;
+}
+
+void
+fz_drop_zip_writer(fz_context *ctx, fz_zip_writer *zip)
+{
+ if (!zip->closed)
+ fz_warn(ctx, "dropping unclosed zip writer");
+ fz_drop_output(ctx, zip->output);
+ fz_drop_buffer(ctx, zip->central);
+ fz_free(ctx, zip);
}
fz_zip_writer *