diff options
author | Tor Andersson <tor.andersson@artifex.com> | 2015-11-09 14:17:00 +0100 |
---|---|---|
committer | Tor Andersson <tor.andersson@artifex.com> | 2015-12-11 12:11:31 +0100 |
commit | c22e6a6dc2bf6acbac955bd5fbdd896329dfd725 (patch) | |
tree | d9d84d3a6a74ed74c9d470b532097769545e8dc6 /source/pdf | |
parent | 95b928431f233052e4bbbd1b2bf9fc705657b5a7 (diff) | |
download | mupdf-c22e6a6dc2bf6acbac955bd5fbdd896329dfd725.tar.xz |
Use fz_output instead of FILE* for most of our output needs.
Use fz_output in debug printing functions.
Use fz_output in pdfshow.
Use fz_output in fz_trace_device instead of stdout.
Use fz_output in pdf-write.c.
Rename fz_new_output_to_filename to fz_new_output_with_path.
Add seek and tell to fz_output.
Remove unused functions like fz_fprintf.
Fix typo in pdf_print_obj.
Diffstat (limited to 'source/pdf')
-rw-r--r-- | source/pdf/pdf-crypt.c | 29 | ||||
-rw-r--r-- | source/pdf/pdf-font.c | 38 | ||||
-rw-r--r-- | source/pdf/pdf-function.c | 155 | ||||
-rw-r--r-- | source/pdf/pdf-object.c | 45 | ||||
-rw-r--r-- | source/pdf/pdf-op-buffer.c | 8 | ||||
-rw-r--r-- | source/pdf/pdf-store.c | 14 | ||||
-rw-r--r-- | source/pdf/pdf-write.c | 113 |
7 files changed, 172 insertions, 230 deletions
diff --git a/source/pdf/pdf-crypt.c b/source/pdf/pdf-crypt.c index 56142d3b..a26f91a3 100644 --- a/source/pdf/pdf-crypt.c +++ b/source/pdf/pdf-crypt.c @@ -1016,28 +1016,27 @@ pdf_open_crypt_with_filter(fz_context *ctx, fz_stream *chain, pdf_crypt *crypt, return chain; } -#ifndef NDEBUG -void pdf_print_crypt(fz_context *ctx, pdf_crypt *crypt) +void +pdf_print_crypt(fz_context *ctx, fz_output *out, pdf_crypt *crypt) { int i; - printf("crypt {\n"); + fz_printf(ctx, out, "crypt {\n"); - printf("\tv=%d length=%d\n", crypt->v, crypt->length); - printf("\tstmf method=%d length=%d\n", crypt->stmf.method, crypt->stmf.length); - printf("\tstrf method=%d length=%d\n", crypt->strf.method, crypt->strf.length); - printf("\tr=%d\n", crypt->r); + fz_printf(ctx, out, "\tv=%d length=%d\n", crypt->v, crypt->length); + fz_printf(ctx, out, "\tstmf method=%d length=%d\n", crypt->stmf.method, crypt->stmf.length); + fz_printf(ctx, out, "\tstrf method=%d length=%d\n", crypt->strf.method, crypt->strf.length); + fz_printf(ctx, out, "\tr=%d\n", crypt->r); - printf("\to=<"); + fz_printf(ctx, out, "\to=<"); for (i = 0; i < 32; i++) - printf("%02X", crypt->o[i]); - printf(">\n"); + fz_printf(ctx, out, "%02X", crypt->o[i]); + fz_printf(ctx, out, ">\n"); - printf("\tu=<"); + fz_printf(ctx, out, "\tu=<"); for (i = 0; i < 32; i++) - printf("%02X", crypt->u[i]); - printf(">\n"); + fz_printf(ctx, out, "%02X", crypt->u[i]); + fz_printf(ctx, out, ">\n"); - printf("}\n"); + fz_printf(ctx, out, "}\n"); } -#endif diff --git a/source/pdf/pdf-font.c b/source/pdf/pdf-font.c index 2f3fa13f..542393fc 100644 --- a/source/pdf/pdf-font.c +++ b/source/pdf/pdf-font.c @@ -799,13 +799,11 @@ hail_mary_cmp_key(fz_context *ctx, void *k0, void *k1) return k0 == k1; } -#ifndef NDEBUG static void -hail_mary_debug_key(fz_context *ctx, FILE *out, void *key_) +hail_mary_print_key(fz_context *ctx, fz_output *out, void *key_) { - fprintf(out, "hail mary "); + fz_printf(ctx, out, "hail mary "); } -#endif static fz_store_type hail_mary_store_type = { @@ -813,9 +811,7 @@ static fz_store_type hail_mary_store_type = hail_mary_keep_key, hail_mary_drop_key, hail_mary_cmp_key, -#ifndef NDEBUG - hail_mary_debug_key -#endif + hail_mary_print_key }; pdf_font_desc * @@ -1275,39 +1271,37 @@ pdf_load_font(fz_context *ctx, pdf_document *doc, pdf_obj *rdb, pdf_obj *dict, i return fontdesc; } -#ifndef NDEBUG void -pdf_print_font(fz_context *ctx, pdf_font_desc *fontdesc) +pdf_print_font(fz_context *ctx, fz_output *out, pdf_font_desc *fontdesc) { int i; - printf("fontdesc {\n"); + fz_printf(ctx, out, "fontdesc {\n"); if (fontdesc->font->ft_face) - printf("\tfreetype font\n"); + fz_printf(ctx, out, "\tfreetype font\n"); if (fontdesc->font->t3procs) - printf("\ttype3 font\n"); + fz_printf(ctx, out, "\ttype3 font\n"); - printf("\twmode %d\n", fontdesc->wmode); - printf("\tDW %d\n", fontdesc->dhmtx.w); + fz_printf(ctx, out, "\twmode %d\n", fontdesc->wmode); + fz_printf(ctx, out, "\tDW %d\n", fontdesc->dhmtx.w); - printf("\tW {\n"); + fz_printf(ctx, out, "\tW {\n"); for (i = 0; i < fontdesc->hmtx_len; i++) - printf("\t\t<%04x> <%04x> %d\n", + fz_printf(ctx, out, "\t\t<%04x> <%04x> %d\n", fontdesc->hmtx[i].lo, fontdesc->hmtx[i].hi, fontdesc->hmtx[i].w); - printf("\t}\n"); + fz_printf(ctx, out, "\t}\n"); if (fontdesc->wmode) { - printf("\tDW2 [%d %d]\n", fontdesc->dvmtx.y, fontdesc->dvmtx.w); - printf("\tW2 {\n"); + fz_printf(ctx, out, "\tDW2 [%d %d]\n", fontdesc->dvmtx.y, fontdesc->dvmtx.w); + fz_printf(ctx, out, "\tW2 {\n"); for (i = 0; i < fontdesc->vmtx_len; i++) - printf("\t\t<%04x> <%04x> %d %d %d\n", fontdesc->vmtx[i].lo, fontdesc->vmtx[i].hi, + fz_printf(ctx, out, "\t\t<%04x> <%04x> %d %d %d\n", fontdesc->vmtx[i].lo, fontdesc->vmtx[i].hi, fontdesc->vmtx[i].x, fontdesc->vmtx[i].y, fontdesc->vmtx[i].w); - printf("\t}\n"); + fz_printf(ctx, out, "\t}\n"); } } -#endif fz_rect *pdf_measure_text(fz_context *ctx, pdf_font_desc *fontdesc, unsigned char *buf, int len, fz_rect *acc) { diff --git a/source/pdf/pdf-function.c b/source/pdf/pdf-function.c index b182a85e..f8e72e87 100644 --- a/source/pdf/pdf-function.c +++ b/source/pdf/pdf-function.c @@ -109,13 +109,12 @@ struct ps_stack_s int sp; }; -#ifndef NDEBUG void -pdf_debug_ps_stack(fz_context *ctx, ps_stack *st) +pdf_print_ps_stack(fz_context *ctx, fz_output *out, ps_stack *st) { int i; - printf("stack: "); + fz_printf(ctx, out, "stack:"); for (i = 0; i < st->sp; i++) { @@ -123,24 +122,24 @@ pdf_debug_ps_stack(fz_context *ctx, ps_stack *st) { case PS_BOOL: if (st->stack[i].u.b) - printf("true "); + fz_printf(ctx, out, " true"); else - printf("false "); + fz_printf(ctx, out, " false"); break; case PS_INT: - printf("%d ", st->stack[i].u.i); + fz_printf(ctx, out, " %d", st->stack[i].u.i); break; case PS_REAL: - printf("%g ", st->stack[i].u.f); + fz_printf(ctx, out, " %g", st->stack[i].u.f); break; } } - printf("\n"); + + fz_printf(ctx, out, "\n"); } -#endif static void ps_init_stack(ps_stack *st) @@ -1386,26 +1385,25 @@ pdf_eval_function(fz_context *ctx, fz_function *func_, const float *in, float *o * Debugging prints */ -#ifndef NDEBUG static void -pdf_debug_indent(char *prefix, int level, char *suffix) +pdf_debug_indent(fz_context *ctx, fz_output *out, char *prefix, int level, char *suffix) { int i; - printf("%s", prefix); + fz_puts(ctx, out, prefix); for (i = 0; i < level; i++) - printf("\t"); + fz_putc(ctx, out, '\t'); - printf("%s", suffix); + fz_puts(ctx, out, suffix); } static void -pdf_debug_ps_func_code(psobj *funccode, psobj *code, int level) +pdf_debug_ps_func_code(fz_context *ctx, fz_output *out, psobj *funccode, psobj *code, int level) { int eof, wasop; - pdf_debug_indent("", level, "{"); + pdf_debug_indent(ctx, out, "", level, "{"); /* Print empty blocks as { }, instead of separating braces on different lines. */ if (code->type == PS_OPERATOR && code->u.op == PS_OP_RETURN) @@ -1414,7 +1412,7 @@ pdf_debug_ps_func_code(psobj *funccode, psobj *code, int level) return; } - pdf_debug_indent("\n", ++level, ""); + pdf_debug_indent(ctx, out, "\n", ++level, ""); eof = 0; wasop = 0; @@ -1424,7 +1422,7 @@ pdf_debug_ps_func_code(psobj *funccode, psobj *code, int level) { case PS_INT: if (wasop) - pdf_debug_indent("\n", level, ""); + pdf_debug_indent(ctx, out, "\n", level, ""); printf("%d ", code->u.i); wasop = 0; @@ -1433,7 +1431,7 @@ pdf_debug_ps_func_code(psobj *funccode, psobj *code, int level) case PS_REAL: if (wasop) - pdf_debug_indent("\n", level, ""); + pdf_debug_indent(ctx, out, "\n", level, ""); printf("%g ", code->u.f); wasop = 0; @@ -1449,27 +1447,27 @@ pdf_debug_ps_func_code(psobj *funccode, psobj *code, int level) else if (code->u.op == PS_OP_IF) { printf("\n"); - pdf_debug_ps_func_code(funccode, &funccode[(code + 2)->u.block], level); + pdf_debug_ps_func_code(ctx, out, funccode, &funccode[(code + 2)->u.block], level); printf("%s", ps_op_names[code->u.op]); code = &funccode[(code + 3)->u.block]; if (code->type != PS_OPERATOR || code->u.op != PS_OP_RETURN) - pdf_debug_indent("\n", level, ""); + pdf_debug_indent(ctx, out, "\n", level, ""); wasop = 0; } else if (code->u.op == PS_OP_IFELSE) { printf("\n"); - pdf_debug_ps_func_code(funccode, &funccode[(code + 2)->u.block], level); + pdf_debug_ps_func_code(ctx, out, funccode, &funccode[(code + 2)->u.block], level); printf("\n"); - pdf_debug_ps_func_code(funccode, &funccode[(code + 1)->u.block], level); + pdf_debug_ps_func_code(ctx, out, funccode, &funccode[(code + 1)->u.block], level); printf("%s", ps_op_names[code->u.op]); code = &funccode[(code + 3)->u.block]; if (code->type != PS_OPERATOR || code->u.op != PS_OP_RETURN) - pdf_debug_indent("\n", level, ""); + pdf_debug_indent(ctx, out, "\n", level, ""); wasop = 0; } @@ -1483,18 +1481,18 @@ pdf_debug_ps_func_code(psobj *funccode, psobj *code, int level) } } - pdf_debug_indent("", --level, "} "); + pdf_debug_indent(ctx, out, "", --level, "} "); } static void -pdf_debug_function_imp(fz_function *func_, int level) +pdf_debug_function_imp(fz_context *ctx, fz_output *out, fz_function *func_, int level) { int i; pdf_function *func = (pdf_function *)func_; - pdf_debug_indent("", level, "function {\n"); + pdf_debug_indent(ctx, out, "", level, "function {\n"); - pdf_debug_indent("", ++level, ""); + pdf_debug_indent(ctx, out, "", ++level, ""); switch (func->type) { case SAMPLE: @@ -1511,99 +1509,98 @@ pdf_debug_function_imp(fz_function *func_, int level) break; } - pdf_debug_indent("\n", level, ""); - printf("%d input -> %d output\n", func->base.m, func->base.n); + pdf_debug_indent(ctx, out, "\n", level, ""); + fz_printf(ctx, out, "%d input -> %d output\n", func->base.m, func->base.n); - pdf_debug_indent("", level, "domain "); + pdf_debug_indent(ctx, out, "", level, "domain "); for (i = 0; i < func->base.m; i++) - printf("%g %g ", func->domain[i][0], func->domain[i][1]); - printf("\n"); + fz_printf(ctx, out, "%g %g ", func->domain[i][0], func->domain[i][1]); + fz_printf(ctx, out, "\n"); if (func->has_range) { - pdf_debug_indent("", level, "range "); + pdf_debug_indent(ctx, out, "", level, "range "); for (i = 0; i < func->base.n; i++) - printf("%g %g ", func->range[i][0], func->range[i][1]); - printf("\n"); + fz_printf(ctx, out, "%g %g ", func->range[i][0], func->range[i][1]); + fz_printf(ctx, out, "\n"); } switch (func->type) { case SAMPLE: - pdf_debug_indent("", level, ""); - printf("bps: %d\n", func->u.sa.bps); + pdf_debug_indent(ctx, out, "", level, ""); + fz_printf(ctx, out, "bps: %d\n", func->u.sa.bps); - pdf_debug_indent("", level, ""); - printf("size: [ "); + pdf_debug_indent(ctx, out, "", level, ""); + fz_printf(ctx, out, "size: [ "); for (i = 0; i < func->base.m; i++) - printf("%d ", func->u.sa.size[i]); - printf("]\n"); + fz_printf(ctx, out, "%d ", func->u.sa.size[i]); + fz_printf(ctx, out, "]\n"); - pdf_debug_indent("", level, ""); - printf("encode: [ "); + pdf_debug_indent(ctx, out, "", level, ""); + fz_printf(ctx, out, "encode: [ "); for (i = 0; i < func->base.m; i++) - printf("%g %g ", func->u.sa.encode[i][0], func->u.sa.encode[i][1]); - printf("]\n"); + fz_printf(ctx, out, "%g %g ", func->u.sa.encode[i][0], func->u.sa.encode[i][1]); + fz_printf(ctx, out, "]\n"); - pdf_debug_indent("", level, ""); - printf("decode: [ "); + pdf_debug_indent(ctx, out, "", level, ""); + fz_printf(ctx, out, "decode: [ "); for (i = 0; i < func->base.m; i++) - printf("%g %g ", func->u.sa.decode[i][0], func->u.sa.decode[i][1]); - printf("]\n"); + fz_printf(ctx, out, "%g %g ", func->u.sa.decode[i][0], func->u.sa.decode[i][1]); + fz_printf(ctx, out, "]\n"); break; case EXPONENTIAL: - pdf_debug_indent("", level, ""); - printf("n: %g\n", func->u.e.n); + pdf_debug_indent(ctx, out, "", level, ""); + fz_printf(ctx, out, "n: %g\n", func->u.e.n); - pdf_debug_indent("", level, ""); - printf("c0: [ "); + pdf_debug_indent(ctx, out, "", level, ""); + fz_printf(ctx, out, "c0: [ "); for (i = 0; i < func->base.n; i++) - printf("%g ", func->u.e.c0[i]); - printf("]\n"); + fz_printf(ctx, out, "%g ", func->u.e.c0[i]); + fz_printf(ctx, out, "]\n"); - pdf_debug_indent("", level, ""); - printf("c1: [ "); + pdf_debug_indent(ctx, out, "", level, ""); + fz_printf(ctx, out, "c1: [ "); for (i = 0; i < func->base.n; i++) - printf("%g ", func->u.e.c1[i]); - printf("]\n"); + fz_printf(ctx, out, "%g ", func->u.e.c1[i]); + fz_printf(ctx, out, "]\n"); break; case STITCHING: - pdf_debug_indent("", level, ""); - printf("%d functions\n", func->u.st.k); + pdf_debug_indent(ctx, out, "", level, ""); + fz_printf(ctx, out, "%d functions\n", func->u.st.k); - pdf_debug_indent("", level, ""); - printf("bounds: [ "); + pdf_debug_indent(ctx, out, "", level, ""); + fz_printf(ctx, out, "bounds: [ "); for (i = 0; i < func->u.st.k - 1; i++) - printf("%g ", func->u.st.bounds[i]); - printf("]\n"); + fz_printf(ctx, out, "%g ", func->u.st.bounds[i]); + fz_printf(ctx, out, "]\n"); - pdf_debug_indent("", level, ""); - printf("encode: [ "); + pdf_debug_indent(ctx, out, "", level, ""); + fz_printf(ctx, out, "encode: [ "); for (i = 0; i < func->u.st.k * 2; i++) - printf("%g ", func->u.st.encode[i]); - printf("]\n"); + fz_printf(ctx, out, "%g ", func->u.st.encode[i]); + fz_printf(ctx, out, "]\n"); for (i = 0; i < func->u.st.k; i++) - pdf_debug_function_imp(func->u.st.funcs[i], level); + pdf_debug_function_imp(ctx, out, func->u.st.funcs[i], level); break; case POSTSCRIPT: - pdf_debug_ps_func_code(func->u.p.code, func->u.p.code, level); - printf("\n"); + pdf_debug_ps_func_code(ctx, out, func->u.p.code, func->u.p.code, level); + fz_printf(ctx, out, "\n"); break; } - pdf_debug_indent("", --level, "}\n"); + pdf_debug_indent(ctx, out, "", --level, "}\n"); } void -pdf_debug_function(fz_context *ctx, fz_function *func) +pdf_print_function(fz_context *ctx, fz_output *out, fz_function *func) { - pdf_debug_function_imp(func, 0); + pdf_debug_function_imp(ctx, out, func, 0); } -#endif fz_function * pdf_load_function(fz_context *ctx, pdf_document *doc, pdf_obj *dict, int in, int out) @@ -1624,9 +1621,7 @@ pdf_load_function(fz_context *ctx, pdf_document *doc, pdf_obj *dict, int in, int FZ_INIT_STORABLE(&func->base, 1, pdf_drop_function_imp); func->base.size = sizeof(*func); func->base.evaluate = pdf_eval_function; -#ifndef NDEBUG - func->base.debug = pdf_debug_function; -#endif + func->base.print = pdf_print_function; obj = pdf_dict_get(ctx, dict, PDF_NAME_FunctionType); func->type = pdf_to_int(ctx, obj); diff --git a/source/pdf/pdf-object.c b/source/pdf/pdf-object.c index 7ffb99c7..db4358e1 100644 --- a/source/pdf/pdf-object.c +++ b/source/pdf/pdf-object.c @@ -2078,32 +2078,7 @@ pdf_sprint_obj(fz_context *ctx, char *s, int n, pdf_obj *obj, int tight) return fmt.len; } -int -pdf_fprint_obj(fz_context *ctx, FILE *fp, pdf_obj *obj, int tight) -{ - char buf[1024]; - char *ptr; - int n; - - n = pdf_sprint_obj(ctx, NULL, 0, obj, tight); - if ((n + 1) < sizeof buf) - { - pdf_sprint_obj(ctx, buf, sizeof buf, obj, tight); - fputs(buf, fp); - fputc('\n', fp); - } - else - { - ptr = fz_malloc(ctx, n + 1); - pdf_sprint_obj(ctx, ptr, n + 1, obj, tight); - fputs(ptr, fp); - fputc('\n', fp); - fz_free(ctx, ptr); - } - return n; -} - -int pdf_output_obj(fz_context *ctx, fz_output *out, pdf_obj *obj, int tight) +int pdf_print_obj(fz_context *ctx, fz_output *out, pdf_obj *obj, int tight) { char buf[1024]; char *ptr; @@ -2113,32 +2088,18 @@ int pdf_output_obj(fz_context *ctx, fz_output *out, pdf_obj *obj, int tight) if ((n + 1) < sizeof buf) { pdf_sprint_obj(ctx, buf, sizeof buf, obj, tight); - fz_puts(ctx, out, buf); + fz_write(ctx, out, buf, n); } else { ptr = fz_malloc(ctx, n + 1); pdf_sprint_obj(ctx, ptr, n + 1, obj, tight); - fz_puts(ctx, out, buf); + fz_write(ctx, out, ptr, n); fz_free(ctx, ptr); } return n; } -#ifndef NDEBUG -void -pdf_print_obj(fz_context *ctx, pdf_obj *obj) -{ - pdf_fprint_obj(ctx, stdout, obj, 0); -} - -void -pdf_print_ref(fz_context *ctx, pdf_obj *ref) -{ - pdf_print_obj(ctx, pdf_resolve_indirect(ctx, ref)); -} -#endif - int pdf_obj_refs(fz_context *ctx, pdf_obj *ref) { return (ref >= PDF_OBJ__LIMIT ? ref->refs : 0); diff --git a/source/pdf/pdf-op-buffer.c b/source/pdf/pdf-op-buffer.c index d4f56286..6e79a7ee 100644 --- a/source/pdf/pdf-op-buffer.c +++ b/source/pdf/pdf-op-buffer.c @@ -50,7 +50,7 @@ pdf_out_d(fz_context *ctx, pdf_processor *proc, pdf_obj *array, float phase) fz_output *out = ((pdf_output_processor*)proc)->out; if (!((pdf_output_processor*)proc)->extgstate) { - pdf_output_obj(ctx, out, array, 1); + pdf_print_obj(ctx, out, array, 1); fz_printf(ctx, out, " %f d\n", phase); } } @@ -381,7 +381,7 @@ static void pdf_out_TJ(fz_context *ctx, pdf_processor *proc, pdf_obj *array) { fz_output *out = ((pdf_output_processor*)proc)->out; - pdf_output_obj(ctx, out, array, 1); + pdf_print_obj(ctx, out, array, 1); fz_printf(ctx, out, " TJ\n"); } @@ -716,7 +716,7 @@ pdf_out_DP(fz_context *ctx, pdf_processor *proc, const char *tag, pdf_obj *prope { fz_output *out = ((pdf_output_processor*)proc)->out; fz_printf(ctx, out, "/%s ", tag); - pdf_output_obj(ctx, out, properties, 1); + pdf_print_obj(ctx, out, properties, 1); fz_printf(ctx, out, " DP\n"); } @@ -732,7 +732,7 @@ pdf_out_BDC(fz_context *ctx, pdf_processor *proc, const char *tag, pdf_obj *prop { fz_output *out = ((pdf_output_processor*)proc)->out; fz_printf(ctx, out, "/%s ", tag); - pdf_output_obj(ctx, out, properties, 1); + pdf_print_obj(ctx, out, properties, 1); fz_printf(ctx, out, " BDC\n"); } diff --git a/source/pdf/pdf-store.c b/source/pdf/pdf-store.c index 51255414..b58d2667 100644 --- a/source/pdf/pdf-store.c +++ b/source/pdf/pdf-store.c @@ -31,20 +31,16 @@ pdf_cmp_key(fz_context *ctx, void *k0, void *k1) return pdf_objcmp(ctx, (pdf_obj *)k0, (pdf_obj *)k1); } -#ifndef NDEBUG static void -pdf_debug_key(fz_context *ctx, FILE *out, void *key_) +pdf_print_key(fz_context *ctx, fz_output *out, void *key_) { pdf_obj *key = (pdf_obj *)key_; if (pdf_is_indirect(ctx, key)) - { - fprintf(out, "(%d %d R) ", pdf_to_num(ctx, key), pdf_to_gen(ctx, key)); - } + fz_printf(ctx, out, "(%d %d R) ", pdf_to_num(ctx, key), pdf_to_gen(ctx, key)); else - pdf_fprint_obj(ctx, out, key, 0); + pdf_print_obj(ctx, out, key, 0); } -#endif static fz_store_type pdf_obj_store_type = { @@ -52,9 +48,7 @@ static fz_store_type pdf_obj_store_type = pdf_keep_key, pdf_drop_key, pdf_cmp_key, -#ifndef NDEBUG - pdf_debug_key -#endif + pdf_print_key }; void diff --git a/source/pdf/pdf-write.c b/source/pdf/pdf-write.c index 310df6c1..1c7a3739 100644 --- a/source/pdf/pdf-write.c +++ b/source/pdf/pdf-write.c @@ -47,7 +47,7 @@ typedef struct { struct pdf_write_options_s { - FILE *out; + fz_output *out; int do_incremental; int do_tight; int do_ascii; @@ -1621,11 +1621,11 @@ static void copystream(fz_context *ctx, pdf_document *doc, pdf_write_options *op pdf_drop_obj(ctx, newlen); } - fz_fprintf(ctx, opts->out, "%d %d obj\n", num, gen); - pdf_fprint_obj(ctx, opts->out, obj, opts->do_tight); - fputs("stream\n", opts->out); - fwrite(buf->data, 1, buf->len, opts->out); - fputs("\nendstream\nendobj\n\n", opts->out); + fz_printf(ctx, opts->out, "%d %d obj\n", num, gen); + pdf_print_obj(ctx, opts->out, obj, opts->do_tight); + fz_puts(ctx, opts->out, "stream\n"); + fz_write(ctx, opts->out, buf->data, buf->len); + fz_puts(ctx, opts->out, "\nendstream\nendobj\n\n"); fz_drop_buffer(ctx, buf); pdf_drop_obj(ctx, obj); @@ -1670,11 +1670,11 @@ static void expandstream(fz_context *ctx, pdf_document *doc, pdf_write_options * pdf_dict_put(ctx, obj, PDF_NAME_Length, newlen); pdf_drop_obj(ctx, newlen); - fz_fprintf(ctx, opts->out, "%d %d obj\n", num, gen); - pdf_fprint_obj(ctx, opts->out, obj, opts->do_tight); - fputs("stream\n", opts->out); - fwrite(buf->data, 1, buf->len, opts->out); - fputs("\nendstream\nendobj\n\n", opts->out); + fz_printf(ctx, opts->out, "%d %d obj\n", num, gen); + pdf_print_obj(ctx, opts->out, obj, opts->do_tight); + fz_puts(ctx, opts->out, "stream\n"); + fz_write(ctx, opts->out, buf->data, buf->len); + fz_puts(ctx, opts->out, "\nendstream\nendobj\n\n"); fz_drop_buffer(ctx, buf); pdf_drop_obj(ctx, obj); @@ -1723,7 +1723,7 @@ static void writeobject(fz_context *ctx, pdf_document *doc, pdf_write_options *o fz_rethrow_if(ctx, FZ_ERROR_TRYLATER); if (opts->continue_on_error) { - fz_fprintf(ctx, opts->out, "%d %d obj\nnull\nendobj\n", num, gen); + fz_printf(ctx, opts->out, "%d %d obj\nnull\nendobj\n", num, gen); if (opts->errors) (*opts->errors)++; fz_warn(ctx, "%s", fz_caught_message(ctx)); @@ -1754,15 +1754,15 @@ static void writeobject(fz_context *ctx, pdf_document *doc, pdf_write_options *o entry = pdf_get_xref_entry(ctx, doc, num); if (!pdf_is_stream(ctx, doc, num, gen)) { - fz_fprintf(ctx, opts->out, "%d %d obj\n", num, gen); - pdf_fprint_obj(ctx, opts->out, obj, opts->do_tight); - fputs("endobj\n\n", opts->out); + fz_printf(ctx, opts->out, "%d %d obj\n", num, gen); + pdf_print_obj(ctx, opts->out, obj, opts->do_tight); + fz_puts(ctx, opts->out, "endobj\n\n"); } else if (entry->stm_ofs < 0 && entry->stm_buf == NULL) { - fz_fprintf(ctx, opts->out, "%d %d obj\n", num, gen); - pdf_fprint_obj(ctx, opts->out, obj, opts->do_tight); - fputs("stream\nendstream\nendobj\n\n", opts->out); + fz_printf(ctx, opts->out, "%d %d obj\n", num, gen); + pdf_print_obj(ctx, opts->out, obj, opts->do_tight); + fz_puts(ctx, opts->out, "stream\nendstream\nendobj\n\n"); } else { @@ -1805,7 +1805,7 @@ static void writeobject(fz_context *ctx, pdf_document *doc, pdf_write_options *o fz_rethrow_if(ctx, FZ_ERROR_TRYLATER); if (opts->continue_on_error) { - fz_fprintf(ctx, opts->out, "%d %d obj\nnull\nendobj\n", num, gen); + fz_printf(ctx, opts->out, "%d %d obj\nnull\nendobj\n", num, gen); if (opts->errors) (*opts->errors)++; fz_warn(ctx, "%s", fz_caught_message(ctx)); @@ -1825,13 +1825,13 @@ static void writexrefsubsect(fz_context *ctx, pdf_write_options *opts, int from, { int num; - fz_fprintf(ctx, opts->out, "%d %d\n", from, to - from); + fz_printf(ctx, opts->out, "%d %d\n", from, to - from); for (num = from; num < to; num++) { if (opts->use_list[num]) - fz_fprintf(ctx, opts->out, "%010Zd %05d n \n", opts->ofs_list[num], opts->gen_list[num]); + fz_printf(ctx, opts->out, "%010Zd %05d n \n", opts->ofs_list[num], opts->gen_list[num]); else - fz_fprintf(ctx, opts->out, "%010Zd %05d f \n", opts->ofs_list[num], opts->gen_list[num]); + fz_printf(ctx, opts->out, "%010Zd %05d f \n", opts->ofs_list[num], opts->gen_list[num]); } } @@ -1841,8 +1841,8 @@ static void writexref(fz_context *ctx, pdf_document *doc, pdf_write_options *opt pdf_obj *obj; pdf_obj *nobj = NULL; - fputs("xref\n", opts->out); - opts->first_xref_entry_offset = fz_ftell(opts->out); + fz_puts(ctx, opts->out, "xref\n"); + opts->first_xref_entry_offset = fz_tell_output(ctx, opts->out); if (opts->do_incremental) { @@ -1869,7 +1869,7 @@ static void writexref(fz_context *ctx, pdf_document *doc, pdf_write_options *opt writexrefsubsect(ctx, opts, from, to); } - fputs("\n", opts->out); + fz_puts(ctx, opts->out, "\n"); fz_var(trailer); fz_var(nobj); @@ -1924,13 +1924,13 @@ static void writexref(fz_context *ctx, pdf_document *doc, pdf_write_options *opt fz_rethrow(ctx); } - fputs("trailer\n", opts->out); - pdf_fprint_obj(ctx, opts->out, trailer, opts->do_tight); - fputs("\n", opts->out); + fz_puts(ctx, opts->out, "trailer\n"); + pdf_print_obj(ctx, opts->out, trailer, opts->do_tight); + fz_puts(ctx, opts->out, "\n"); pdf_drop_obj(ctx, trailer); - fz_fprintf(ctx, opts->out, "startxref\n%d\n%%%%EOF\n", startxref); + fz_printf(ctx, opts->out, "startxref\n%d\n%%%%EOF\n", startxref); doc->has_xref_streams = 0; } @@ -1970,7 +1970,7 @@ static void writexrefstream(fz_context *ctx, pdf_document *doc, pdf_write_option dict = pdf_new_dict(ctx, doc, 6); pdf_update_object(ctx, doc, num, dict); - opts->first_xref_entry_offset = fz_ftell(opts->out); + opts->first_xref_entry_offset = fz_tell_output(ctx, opts->out); to++; @@ -2054,7 +2054,7 @@ static void writexrefstream(fz_context *ctx, pdf_document *doc, pdf_write_option pdf_update_stream(ctx, doc, dict, fzbuf, 0); writeobject(ctx, doc, opts, num, 0, 0); - fz_fprintf(ctx, opts->out, "startxref\n%Zd\n%%%%EOF\n", startxref); + fz_printf(ctx, opts->out, "startxref\n%Zd\n%%%%EOF\n", startxref); } fz_always(ctx) { @@ -2069,14 +2069,14 @@ static void writexrefstream(fz_context *ctx, pdf_document *doc, pdf_write_option } static void -padto(FILE *file, fz_off_t target) +padto(fz_context *ctx, fz_output *out, fz_off_t target) { - fz_off_t pos = fz_ftell(file); + fz_off_t pos = fz_tell_output(ctx, out); assert(pos <= target); while (pos < target) { - fputc('\n', file); + fz_putc(ctx, out, '\n'); pos++; } } @@ -2106,10 +2106,10 @@ dowriteobject(fz_context *ctx, pdf_document *doc, pdf_write_options *opts, int n if (entry->type == 'n' || entry->type == 'o') { if (pass > 0) - padto(opts->out, opts->ofs_list[num]); + padto(ctx, opts->out, opts->ofs_list[num]); if (!opts->do_incremental || pdf_xref_is_incremental(ctx, doc, num)) { - opts->ofs_list[num] = fz_ftell(opts->out); + opts->ofs_list[num] = fz_tell_output(ctx, opts->out); writeobject(ctx, doc, opts, num, opts->gen_list[num], 1); } } @@ -2125,8 +2125,8 @@ writeobjects(fz_context *ctx, pdf_document *doc, pdf_write_options *opts, int pa if (!opts->do_incremental) { - fprintf(opts->out, "%%PDF-%d.%d\n", doc->version / 10, doc->version % 10); - fputs("%%\316\274\341\277\246\n\n", opts->out); + fz_printf(ctx, opts->out, "%%PDF-%d.%d\n", doc->version / 10, doc->version % 10); + fz_puts(ctx, opts->out, "%%\316\274\341\277\246\n\n"); } dowriteobject(ctx, doc, opts, opts->start, pass); @@ -2135,9 +2135,9 @@ writeobjects(fz_context *ctx, pdf_document *doc, pdf_write_options *opts, int pa { /* Write first xref */ if (pass == 0) - opts->first_xref_offset = fz_ftell(opts->out); + opts->first_xref_offset = fz_tell_output(ctx, opts->out); else - padto(opts->out, opts->first_xref_offset); + padto(ctx, opts->out, opts->first_xref_offset); writexref(ctx, doc, opts, opts->start, pdf_xref_len(ctx, doc), 1, opts->main_xref_offset, 0); } @@ -2146,7 +2146,7 @@ writeobjects(fz_context *ctx, pdf_document *doc, pdf_write_options *opts, int pa if (opts->do_linear && pass == 1) { fz_off_t offset = (opts->start == 1 ? opts->main_xref_offset : opts->ofs_list[1] + opts->hintstream_len); - padto(opts->out, offset); + padto(ctx, opts->out, offset); } for (num = 1; num < opts->start; num++) { @@ -2514,11 +2514,11 @@ static void presize_unsaved_signature_byteranges(fz_context *ctx, pdf_document * static void complete_signatures(fz_context *ctx, pdf_document *doc, pdf_write_options *opts, char *filename) { pdf_unsaved_sig *usig; - FILE *f; char buf[5120]; int s; int i; int last_end; + FILE *f; for (s = 0; s < doc->num_incremental_sections; s++) { @@ -2673,8 +2673,7 @@ static void finalise_write_options(fz_context *ctx, pdf_write_options *opts) pdf_drop_obj(ctx, opts->hints_s); pdf_drop_obj(ctx, opts->hints_length); page_objects_list_destroy(ctx, opts->page_object_lists); - if (opts->out) - fclose(opts->out); + fz_drop_output(ctx, opts->out); } void pdf_write_document(fz_context *ctx, pdf_document *doc, char *filename, fz_write_options *fz_opts) @@ -2713,16 +2712,16 @@ void pdf_write_document(fz_context *ctx, pdf_document *doc, char *filename, fz_w /* If no changes, nothing to write */ if (doc->num_incremental_sections == 0) return; - opts.out = fz_fopen(filename, "ab"); + opts.out = fz_new_output_with_path(ctx, filename, 1); if (opts.out) { - fz_fseek(opts.out, 0, SEEK_END); - fputs("\n", opts.out); + fz_seek_output(ctx, opts.out, 0, SEEK_END); + fz_puts(ctx, opts.out, "\n"); } } else { - opts.out = fz_fopen(filename, "wb"); + opts.out = fz_new_output_with_path(ctx, filename, 0); } if (!opts.out) @@ -2792,13 +2791,13 @@ void pdf_write_document(fz_context *ctx, pdf_document *doc, char *filename, fz_w } } - opts.first_xref_offset = fz_ftell(opts.out); + opts.first_xref_offset = fz_tell_output(ctx, opts.out); if (doc->has_xref_streams) writexrefstream(ctx, doc, &opts, 0, xref_len, 1, 0, opts.first_xref_offset); else writexref(ctx, doc, &opts, 0, xref_len, 1, 0, opts.first_xref_offset); - doc->xref_sections[doc->xref_base].end_ofs = fz_ftell(opts.out); + doc->xref_sections[doc->xref_base].end_ofs = fz_tell_output(ctx, opts.out); } doc->xref_base = 0; @@ -2826,9 +2825,9 @@ void pdf_write_document(fz_context *ctx, pdf_document *doc, char *filename, fz_w if (opts.do_linear) { - opts.main_xref_offset = fz_ftell(opts.out); + opts.main_xref_offset = fz_tell_output(ctx, opts.out); writexref(ctx, doc, &opts, 0, opts.start, 0, 0, opts.first_xref_offset); - opts.file_len = fz_ftell(opts.out); + opts.file_len = fz_tell_output(ctx, opts.out); make_hint_stream(ctx, doc, &opts); if (opts.do_ascii) @@ -2839,22 +2838,22 @@ void pdf_write_document(fz_context *ctx, pdf_document *doc, char *filename, fz_w opts.file_len += opts.hintstream_len; opts.main_xref_offset += opts.hintstream_len; update_linearization_params(ctx, doc, &opts); - fz_fseek(opts.out, 0, 0); + fz_seek_output(ctx, opts.out, 0, 0); writeobjects(ctx, doc, &opts, 1); - padto(opts.out, opts.main_xref_offset); + padto(ctx, opts.out, opts.main_xref_offset); writexref(ctx, doc, &opts, 0, opts.start, 0, 0, opts.first_xref_offset); } else { - opts.first_xref_offset = fz_ftell(opts.out); + opts.first_xref_offset = fz_tell_output(ctx, opts.out); writexref(ctx, doc, &opts, 0, xref_len, 1, 0, opts.first_xref_offset); } - doc->xref_sections[0].end_ofs = fz_ftell(opts.out); + doc->xref_sections[0].end_ofs = fz_tell_output(ctx, opts.out); } - fclose(opts.out); + fz_drop_output(ctx, opts.out); opts.out = NULL; complete_signatures(ctx, doc, &opts, filename); |