diff options
-rw-r--r-- | include/mupdf/pdf/xref.h | 9 | ||||
-rw-r--r-- | source/pdf/pdf-appearance.c | 9 | ||||
-rw-r--r-- | source/pdf/pdf-clean.c | 33 | ||||
-rw-r--r-- | source/pdf/pdf-device.c | 15 | ||||
-rw-r--r-- | source/pdf/pdf-write.c | 5 | ||||
-rw-r--r-- | source/pdf/pdf-xobject.c | 4 | ||||
-rw-r--r-- | source/pdf/pdf-xref.c | 10 |
7 files changed, 32 insertions, 53 deletions
diff --git a/include/mupdf/pdf/xref.h b/include/mupdf/pdf/xref.h index c41b0291..aa54605f 100644 --- a/include/mupdf/pdf/xref.h +++ b/include/mupdf/pdf/xref.h @@ -19,12 +19,11 @@ void pdf_update_object(fz_context *ctx, pdf_document *doc, int num, pdf_obj *obj /* pdf_update_stream: Replace stream contents for object in xref table with the passed in buffer. - The buffer contents must match the /Filter setting. - If storing uncompressed data, make sure to delete the /Filter key from - the stream dictionary. If storing deflated data, make sure to set the - /Filter value to /FlateDecode. + The buffer contents must match the /Filter setting if 'compressed' is true. + If 'compressed' is false, the /Filter and /DecodeParms entries are deleted. + The /Length entry is updated. */ -void pdf_update_stream(fz_context *ctx, pdf_document *doc, int num, fz_buffer *buf); +void pdf_update_stream(fz_context *ctx, pdf_document *doc, pdf_obj *ref, fz_buffer *buf, int compressed); /* * xref and object / stream api diff --git a/source/pdf/pdf-appearance.c b/source/pdf/pdf-appearance.c index 830adeeb..52dfba0b 100644 --- a/source/pdf/pdf-appearance.c +++ b/source/pdf/pdf-appearance.c @@ -2051,8 +2051,7 @@ static void insert_signature_appearance_layers(fz_context *ctx, pdf_document *do pdf_dict_putp(ctx, main_ap, "Resources/XObject/FRM", frm); fzbuf = fz_new_buffer(ctx, 8); fz_buffer_printf(ctx, fzbuf, "/FRM Do"); - pdf_update_stream(ctx, doc, pdf_to_num(ctx, main_ap), fzbuf); - pdf_dict_puts_drop(ctx, main_ap, "Length", pdf_new_int(ctx, doc, fzbuf->len)); + pdf_update_stream(ctx, doc, main_ap, fzbuf, 0); fz_drop_buffer(ctx, fzbuf); fzbuf = NULL; @@ -2060,15 +2059,13 @@ static void insert_signature_appearance_layers(fz_context *ctx, pdf_document *do pdf_dict_putp(ctx, frm, "Resources/XObject/n2", ap); fzbuf = fz_new_buffer(ctx, 8); fz_buffer_printf(ctx, fzbuf, "q 1 0 0 1 0 0 cm /n0 Do Q q 1 0 0 1 0 0 cm /n2 Do Q"); - pdf_update_stream(ctx, doc, pdf_to_num(ctx, frm), fzbuf); - pdf_dict_puts_drop(ctx, frm, "Length", pdf_new_int(ctx, doc, fzbuf->len)); + pdf_update_stream(ctx, doc, frm, fzbuf, 0); fz_drop_buffer(ctx, fzbuf); fzbuf = NULL; fzbuf = fz_new_buffer(ctx, 8); fz_buffer_printf(ctx, fzbuf, "%% DSBlank"); - pdf_update_stream(ctx, doc, pdf_to_num(ctx, n0), fzbuf); - pdf_dict_puts_drop(ctx, n0, "Length", pdf_new_int(ctx, doc, fzbuf->len)); + pdf_update_stream(ctx, doc, n0, fzbuf, 0); fz_drop_buffer(ctx, fzbuf); fzbuf = NULL; diff --git a/source/pdf/pdf-clean.c b/source/pdf/pdf-clean.c index b88bbb71..7a72a2e2 100644 --- a/source/pdf/pdf-clean.c +++ b/source/pdf/pdf-clean.c @@ -5,7 +5,6 @@ pdf_clean_stream_object(fz_context *ctx, pdf_document *doc, pdf_obj *obj, pdf_ob { pdf_process process, process2; fz_buffer *buffer; - int num; pdf_obj *res = NULL; pdf_obj *ref = NULL; @@ -33,9 +32,7 @@ pdf_clean_stream_object(fz_context *ctx, pdf_document *doc, pdf_obj *obj, pdf_ob pdf_process_stream_object(ctx, doc, obj, &process, orig_res, cookie); - num = pdf_to_num(ctx, obj); - pdf_dict_dels(ctx, obj, "Filter"); - pdf_update_stream(ctx, doc, num, buffer); + pdf_update_stream(ctx, doc, obj, buffer, 0); if (own_res) { @@ -60,7 +57,7 @@ pdf_clean_type3(fz_context *ctx, pdf_document *doc, pdf_obj *obj, pdf_obj *orig_ { pdf_process process, process2; fz_buffer *buffer; - int num, i, l; + int i, l; pdf_obj *res = NULL; pdf_obj *ref = NULL; pdf_obj *charprocs; @@ -91,9 +88,7 @@ pdf_clean_type3(fz_context *ctx, pdf_document *doc, pdf_obj *obj, pdf_obj *orig_ pdf_process_stream_object(ctx, doc, val, &process, orig_res, cookie); - num = pdf_to_num(ctx, val); - pdf_dict_dels(ctx, val, "Filter"); - pdf_update_stream(ctx, doc, num, buffer); + pdf_update_stream(ctx, doc, val, buffer, 0); pdf_dict_put(ctx, charprocs, key, val); fz_drop_buffer(ctx, buffer); buffer = NULL; @@ -121,7 +116,6 @@ void pdf_clean_page_contents(fz_context *ctx, pdf_document *doc, pdf_page *page, { pdf_process process, process2; fz_buffer *buffer = fz_new_buffer(ctx, 1024); - int num; pdf_obj *contents; pdf_obj *new_obj = NULL; pdf_obj *new_ref = NULL; @@ -146,25 +140,15 @@ void pdf_clean_page_contents(fz_context *ctx, pdf_document *doc, pdf_page *page, contents = page->contents; if (pdf_is_array(ctx, contents)) { - int n = pdf_array_len(ctx, contents); - int i; - - for (i = n-1; i > 0; i--) - pdf_array_delete(ctx, contents, i); - /* We cannot rewrite the 0th entry of contents - * directly as it may occur in other pages content - * dictionaries too. We therefore clone it and make - * a new object reference. */ - new_obj = pdf_copy_dict(ctx, pdf_array_get(ctx, contents, 0)); + /* create a new object to replace the array */ + new_obj = pdf_new_dict(ctx, doc, 1); new_ref = pdf_new_ref(ctx, doc, new_obj); - num = pdf_to_num(ctx, new_ref); - pdf_array_put(ctx, contents, 0, new_ref); - pdf_dict_dels(ctx, new_obj, "Filter"); + page->contents = contents = new_ref; } else { - num = pdf_to_num(ctx, contents); pdf_dict_dels(ctx, contents, "Filter"); + pdf_dict_dels(ctx, contents, "DecodeParms"); } /* Now deal with resources. The spec allows for Type3 fonts and form @@ -262,7 +246,8 @@ void pdf_clean_page_contents(fz_context *ctx, pdf_document *doc, pdf_page *page, if (proc_fn) (*proc_fn)(proc_arg, buffer, res); - pdf_update_stream(ctx, doc, num, buffer); + pdf_update_stream(ctx, doc, contents, buffer, 0); + pdf_drop_obj(ctx, page->resources); ref = pdf_new_ref(ctx, doc, res); page->resources = pdf_keep_obj(ctx, ref); diff --git a/source/pdf/pdf-device.c b/source/pdf/pdf-device.c index 39063447..be477942 100644 --- a/source/pdf/pdf-device.c +++ b/source/pdf/pdf-device.c @@ -288,8 +288,7 @@ send_image(fz_context *ctx, pdf_device *pdev, fz_image *image, int mask, int sma } imref = pdf_new_ref(ctx, doc, imobj); - pdf_update_stream(ctx, doc, pdf_to_num(ctx, imref), buffer); - pdf_dict_puts_drop(ctx, imobj, "Length", pdf_new_int(ctx, doc, buffer->len)); + pdf_update_stream(ctx, doc, imref, buffer, 1); { char text[32]; @@ -1143,8 +1142,7 @@ pdf_dev_end_mask(fz_context *ctx, fz_device *dev) /* Here we do part of the pop, but not all of it. */ pdf_dev_end_text(ctx, pdev); fz_buffer_printf(ctx, buf, "Q\n"); - pdf_dict_puts_drop(ctx, form_ref, "Length", pdf_new_int(ctx, doc, buf->len)); - pdf_update_stream(ctx, doc, pdf_to_num(ctx, form_ref), buf); + pdf_update_stream(ctx, doc, form_ref, buf, 0); fz_drop_buffer(ctx, buf); gs->buf = fz_keep_buffer(ctx, gs[-1].buf); gs->on_pop_arg = NULL; @@ -1201,8 +1199,7 @@ pdf_dev_end_group(fz_context *ctx, fz_device *dev) pdf_dev_end_text(ctx, pdev); form_ref = (pdf_obj *)pdf_dev_pop(ctx, pdev); - pdf_dict_puts_drop(ctx, form_ref, "Length", pdf_new_int(ctx, doc, gs->buf->len)); - pdf_update_stream(ctx, doc, pdf_to_num(ctx, form_ref), buf); + pdf_update_stream(ctx, doc, form_ref, buf, 0); fz_drop_buffer(ctx, buf); pdf_drop_obj(ctx, form_ref); } @@ -1231,14 +1228,10 @@ pdf_dev_drop_imp(fz_context *ctx, fz_device *dev) { pdf_device *pdev = (pdf_device*)dev; pdf_document *doc = pdev->doc; - gstate *gs = CURRENT_GSTATE(pdev); int i; pdf_dev_end_text(ctx, pdev); - if (pdev->contents) - pdf_dict_puts_drop(ctx, pdev->contents, "Length", pdf_new_int(ctx, doc, gs->buf->len)); - for (i = pdev->num_gstates-1; i >= 0; i--) { fz_drop_stroke_state(ctx, pdev->gstates[i].stroke_state); @@ -1256,7 +1249,7 @@ pdf_dev_drop_imp(fz_context *ctx, fz_device *dev) if (pdev->contents) { - pdf_update_stream(ctx, doc, pdf_to_num(ctx, pdev->contents), pdev->gstates[0].buf); + pdf_update_stream(ctx, doc, pdev->contents, pdev->gstates[0].buf, 0); pdf_drop_obj(ctx, pdev->contents); } diff --git a/source/pdf/pdf-write.c b/source/pdf/pdf-write.c index f6886beb..ac97e6e4 100644 --- a/source/pdf/pdf-write.c +++ b/source/pdf/pdf-write.c @@ -2010,8 +2010,7 @@ static void writexrefstream(fz_context *ctx, pdf_document *doc, pdf_write_option writexrefstreamsubsect(ctx, doc, opts, index, fzbuf, from, to); } - pdf_update_stream(ctx, doc, num, fzbuf); - pdf_dict_puts_drop(ctx, dict, "Length", pdf_new_int(ctx, doc, fz_buffer_storage(ctx, fzbuf, NULL))); + pdf_update_stream(ctx, doc, dict, fzbuf, 0); writeobject(ctx, doc, opts, num, 0, 0); fprintf(opts->out, "startxref\n%d\n%%%%EOF\n", startxref); @@ -2409,7 +2408,7 @@ make_hint_stream(fz_context *ctx, pdf_document *doc, pdf_write_options *opts) fz_try(ctx) { make_page_offset_hints(ctx, doc, opts, buf); - pdf_update_stream(ctx, doc, pdf_xref_len(ctx, doc)-1, buf); + pdf_update_stream(ctx, doc, pdf_load_object(ctx, doc, pdf_xref_len(ctx, doc)-1, 0), buf, 0); // XXX opts->hintstream_len = buf->len; fz_drop_buffer(ctx, buf); } diff --git a/source/pdf/pdf-xobject.c b/source/pdf/pdf-xobject.c index e169a27a..b9afa857 100644 --- a/source/pdf/pdf-xobject.c +++ b/source/pdf/pdf-xobject.c @@ -228,8 +228,6 @@ pdf_new_xobject(fz_context *ctx, pdf_document *doc, const fz_rect *bbox, const f void pdf_update_xobject_contents(fz_context *ctx, pdf_document *doc, pdf_xobject *form, fz_buffer *buffer) { - pdf_dict_dels(ctx, form->contents, "Filter"); - pdf_dict_puts_drop(ctx, form->contents, "Length", pdf_new_int(ctx, doc, buffer->len)); - pdf_update_stream(ctx, doc, pdf_to_num(ctx, form->contents), buffer); + pdf_update_stream(ctx, doc, form->contents, buffer, 0); form->iteration ++; } diff --git a/source/pdf/pdf-xref.c b/source/pdf/pdf-xref.c index 586aab2d..64312010 100644 --- a/source/pdf/pdf-xref.c +++ b/source/pdf/pdf-xref.c @@ -2126,8 +2126,9 @@ pdf_update_object(fz_context *ctx, pdf_document *doc, int num, pdf_obj *newobj) } void -pdf_update_stream(fz_context *ctx, pdf_document *doc, int num, fz_buffer *newbuf) +pdf_update_stream(fz_context *ctx, pdf_document *doc, pdf_obj *ref, fz_buffer *newbuf, int compressed) { + int num = pdf_to_num(ctx, ref); pdf_xref_entry *x; if (num <= 0 || num >= pdf_xref_len(ctx, doc)) @@ -2140,6 +2141,13 @@ pdf_update_stream(fz_context *ctx, pdf_document *doc, int num, fz_buffer *newbuf fz_drop_buffer(ctx, x->stm_buf); x->stm_buf = fz_keep_buffer(ctx, newbuf); + + pdf_dict_puts_drop(ctx, ref, "Length", pdf_new_int(ctx, doc, newbuf->len)); + if (!compressed) + { + pdf_dict_dels(ctx, ref, "Filter"); + pdf_dict_dels(ctx, ref, "DecodeParms"); + } } int |