From c22e6a6dc2bf6acbac955bd5fbdd896329dfd725 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Mon, 9 Nov 2015 14:17:00 +0100 Subject: 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. --- include/mupdf/fitz/buffer.h | 23 +-- include/mupdf/fitz/device.h | 5 +- include/mupdf/fitz/font.h | 4 +- include/mupdf/fitz/function.h | 8 +- include/mupdf/fitz/hash.h | 7 +- include/mupdf/fitz/output-pnm.h | 16 +- include/mupdf/fitz/output.h | 73 ++++----- include/mupdf/fitz/path.h | 4 +- include/mupdf/fitz/shade.h | 4 +- include/mupdf/fitz/store.h | 10 +- include/mupdf/fitz/stream.h | 5 + include/mupdf/fitz/text.h | 1 - include/mupdf/pdf/cmap.h | 4 +- include/mupdf/pdf/crypt.h | 4 +- include/mupdf/pdf/font.h | 4 +- include/mupdf/pdf/object.h | 8 +- source/fitz/bitmap.c | 31 ++-- source/fitz/draw-device.c | 10 +- source/fitz/font.c | 16 +- source/fitz/hash.c | 18 +- source/fitz/image.c | 11 +- source/fitz/output-pcl.c | 38 +---- source/fitz/output-pwg.c | 34 +--- source/fitz/output.c | 183 +++++++++++++-------- source/fitz/path.c | 30 ++-- source/fitz/pixmap.c | 22 ++- source/fitz/printf.c | 41 ----- source/fitz/shade.c | 30 ++-- source/fitz/store.c | 26 ++- source/fitz/stream-open.c | 9 + source/fitz/text.c | 28 ---- source/fitz/trace-device.c | 355 ++++++++++++++++++++++------------------ source/fitz/unzip.c | 6 +- source/gprf/gprf-skeleton.c | 4 +- source/pdf/pdf-crypt.c | 29 ++-- source/pdf/pdf-font.c | 38 ++--- source/pdf/pdf-function.c | 155 +++++++++--------- source/pdf/pdf-object.c | 45 +---- source/pdf/pdf-op-buffer.c | 8 +- source/pdf/pdf-store.c | 14 +- source/pdf/pdf-write.c | 113 +++++++------ source/tools/mudraw.c | 33 +--- source/tools/pdfextract.c | 36 ++-- source/tools/pdfinfo.c | 6 +- source/tools/pdfpages.c | 2 +- source/tools/pdfshow.c | 79 ++++----- 46 files changed, 723 insertions(+), 907 deletions(-) diff --git a/include/mupdf/fitz/buffer.h b/include/mupdf/fitz/buffer.h index a96f7603..0d0bb3b0 100644 --- a/include/mupdf/fitz/buffer.h +++ b/include/mupdf/fitz/buffer.h @@ -110,28 +110,19 @@ void fz_trim_buffer(fz_context *ctx, fz_buffer *buf); */ void fz_buffer_cat(fz_context *ctx, fz_buffer *buf, fz_buffer *extra); +/* + fz_write_buffer*: write to a buffer. + fz_buffer_printf: print formatted to a buffer. + fz_buffer_cat_pdf_string: Print a string using PDF syntax and escapes. + The buffer will grow as required. +*/ void fz_write_buffer(fz_context *ctx, fz_buffer *buf, const void *data, int len); - void fz_write_buffer_byte(fz_context *ctx, fz_buffer *buf, int val); - void fz_write_buffer_rune(fz_context *ctx, fz_buffer *buf, int val); - void fz_write_buffer_bits(fz_context *ctx, fz_buffer *buf, int val, int bits); - void fz_write_buffer_pad(fz_context *ctx, fz_buffer *buf); - -/* - fz_buffer_printf: print formatted to a buffer. The buffer will grow - as required. -*/ int fz_buffer_printf(fz_context *ctx, fz_buffer *buffer, const char *fmt, ...); int fz_buffer_vprintf(fz_context *ctx, fz_buffer *buffer, const char *fmt, va_list args); - -/* - fz_buffer_printf: print a string formatted as a pdf string to a buffer. - The buffer will grow. -*/ -void -fz_buffer_cat_pdf_string(fz_context *ctx, fz_buffer *buffer, const char *text); +void fz_buffer_cat_pdf_string(fz_context *ctx, fz_buffer *buffer, const char *text); #endif diff --git a/include/mupdf/fitz/device.h b/include/mupdf/fitz/device.h index e3b17fc6..3f2585ae 100644 --- a/include/mupdf/fitz/device.h +++ b/include/mupdf/fitz/device.h @@ -274,10 +274,9 @@ struct fz_cookie_s }; /* - fz_new_trace_device: Create a device to print a debug trace of - all device calls. + fz_new_trace_device: Create a device to print a debug trace of all device calls. */ -fz_device *fz_new_trace_device(fz_context *ctx); +fz_device *fz_new_trace_device(fz_context *ctx, fz_output *out); /* fz_new_bbox_device: Create a device to compute the bounding diff --git a/include/mupdf/fitz/font.h b/include/mupdf/fitz/font.h index e912eb75..1e053470 100644 --- a/include/mupdf/fitz/font.h +++ b/include/mupdf/fitz/font.h @@ -97,8 +97,6 @@ void fz_decouple_type3_font(fz_context *ctx, fz_font *font, void *t3doc); float fz_advance_glyph(fz_context *ctx, fz_font *font, int glyph); int fz_encode_character(fz_context *ctx, fz_font *font, int unicode); -#ifndef NDEBUG -void fz_print_font(fz_context *ctx, FILE *out, fz_font *font); -#endif +void fz_print_font(fz_context *ctx, fz_output *out, fz_font *font); #endif diff --git a/include/mupdf/fitz/function.h b/include/mupdf/fitz/function.h index 005cef3f..0d17da6d 100644 --- a/include/mupdf/fitz/function.h +++ b/include/mupdf/fitz/function.h @@ -16,9 +16,7 @@ void fz_eval_function(fz_context *ctx, fz_function *func, const float *in, int i fz_function *fz_keep_function(fz_context *ctx, fz_function *func); void fz_drop_function(fz_context *ctx, fz_function *func); unsigned int fz_function_size(fz_context *ctx, fz_function *func); -#ifndef NDEBUG -void pdf_debug_function(fz_context *ctx, fz_function *func); -#endif +void fz_print_function(fz_context *ctx, fz_output *out, fz_function *func); enum { @@ -33,9 +31,7 @@ struct fz_function_s int m; /* number of input values */ int n; /* number of output values */ void (*evaluate)(fz_context *ctx, fz_function *func, const float *in, float *out); -#ifndef NDEBUG - void (*debug)(fz_context *ctx, fz_function *func); -#endif + void (*print)(fz_context *ctx, fz_output *out, fz_function *func); }; #endif diff --git a/include/mupdf/fitz/hash.h b/include/mupdf/fitz/hash.h index c56c9d13..8d4874d7 100644 --- a/include/mupdf/fitz/hash.h +++ b/include/mupdf/fitz/hash.h @@ -3,6 +3,7 @@ #include "mupdf/fitz/system.h" #include "mupdf/fitz/context.h" +#include "mupdf/fitz/output.h" /* * Generic hash-table with fixed-length keys. @@ -24,9 +25,7 @@ int fz_hash_len(fz_context *ctx, fz_hash_table *table); void *fz_hash_get_key(fz_context *ctx, fz_hash_table *table, int idx); void *fz_hash_get_val(fz_context *ctx, fz_hash_table *table, int idx); -#ifndef NDEBUG -void fz_print_hash(fz_context *ctx, FILE *out, fz_hash_table *table); -void fz_print_hash_details(fz_context *ctx, FILE *out, fz_hash_table *table, void (*details)(FILE *, void *)); -#endif +void fz_print_hash(fz_context *ctx, fz_output *out, fz_hash_table *table); +void fz_print_hash_details(fz_context *ctx, fz_output *out, fz_hash_table *table, void (*details)(fz_context*, fz_output*, void*)); #endif diff --git a/include/mupdf/fitz/output-pnm.h b/include/mupdf/fitz/output-pnm.h index 54e8355c..0ea8a894 100644 --- a/include/mupdf/fitz/output-pnm.h +++ b/include/mupdf/fitz/output-pnm.h @@ -8,30 +8,28 @@ #include "mupdf/fitz/bitmap.h" /* - fz_write_pnm: Save a pixmap as a pnm - - filename: The filename to save as (including extension). + fz_write_pnm: Save a pixmap as a PNM image file. */ void fz_write_pnm(fz_context *ctx, fz_pixmap *pixmap, char *filename); +void fz_output_pnm(fz_context *ctx, fz_output *out, fz_pixmap *pixmap); void fz_output_pnm_header(fz_context *ctx, fz_output *out, int w, int h, int n); void fz_output_pnm_band(fz_context *ctx, fz_output *out, int w, int h, int n, int band, int bandheight, unsigned char *p); /* - fz_write_pam: Save a pixmap as a pam - - filename: The filename to save as (including extension). + fz_write_pam: Save a pixmap as a PAM image file. */ void fz_write_pam(fz_context *ctx, fz_pixmap *pixmap, char *filename, int savealpha); +void fz_output_pam(fz_context *ctx, fz_output *out, fz_pixmap *pixmap, int savealpha); void fz_output_pam_header(fz_context *ctx, fz_output *out, int w, int h, int n, int savealpha); void fz_output_pam_band(fz_context *ctx, fz_output *out, int w, int h, int n, int band, int bandheight, unsigned char *sp, int savealpha); /* - fz_write_pbm: Save a bitmap as a pbm - - filename: The filename to save as (including extension). + fz_write_pbm: Save a bitmap as a PBM image file. */ void fz_write_pbm(fz_context *ctx, fz_bitmap *bitmap, char *filename); +void fz_output_pbm(fz_context *ctx, fz_output *out, fz_bitmap *bitmap); + #endif diff --git a/include/mupdf/fitz/output.h b/include/mupdf/fitz/output.h index 576755d3..449b76a6 100644 --- a/include/mupdf/fitz/output.h +++ b/include/mupdf/fitz/output.h @@ -4,6 +4,7 @@ #include "mupdf/fitz/system.h" #include "mupdf/fitz/context.h" #include "mupdf/fitz/buffer.h" +#include "mupdf/fitz/string.h" /* Generic output streams - generalise between outputting to a file, @@ -12,52 +13,47 @@ typedef struct fz_output_s fz_output; /* - fz_new_output_with_file: Open an output stream onto a FILE *. - - The stream does NOT take ownership of the FILE *. -*/ -fz_output *fz_new_output_with_file(fz_context *, FILE *, int close); - -/* - fz_new_output_to_filename: Open an output stream to a filename. -*/ -fz_output *fz_new_output_to_filename(fz_context *, const char *filename); - -/* - fz_new_output_with_buffer: Open an output stream onto a buffer. - - The stream does NOT take ownership of the buffer. + fz_new_output_with_file: Open an output stream that writes to a FILE *. + fz_new_output_with_path: Open an output stream that writes to a file. + fz_new_output_with_buffer: Open an output stream that writes into a buffer. */ +fz_output *fz_new_output_with_file_ptr(fz_context *, FILE *, int close); +fz_output *fz_new_output_with_path(fz_context *, const char *filename, int append); fz_output *fz_new_output_with_buffer(fz_context *, fz_buffer *); /* - fz_printf: fprintf equivalent for output streams. -*/ -int fz_printf(fz_context *, fz_output *, const char *, ...); - -/* + fz_write: fwrite equivalent for output streams. + fz_printf: fprintf equivalent for output streams. See fz_snprintf. + fz_vprintf: vfprintf equivalent for output streams. See fz_vsnprintf. fz_puts: fputs equivalent for output streams. + fz_putc: fputc equivalent for output streams. + fz_putrune: fputrune equivalent for output streams. */ -int fz_puts(fz_context *, fz_output *, const char *); +void fz_printf(fz_context *ctx, fz_output *out, const char *fmt, ...); +void fz_vprintf(fz_context *ctx, fz_output *out, const char *fmt, va_list ap); +int fz_write(fz_context *, fz_output *out, const void *data, int len); +#define fz_puts(C,O,S) fz_write(C, O, (S), strlen(S)) +#define fz_putc(C,O,B) fz_write_byte(C, O, B) +#define fz_putrune(C,O,R) fz_write_rune(C, O, R) /* - fz_write: fwrite equivalent for output streams. + fz_seek_output: Seek to the specified file position. Throw an error on unseekable outputs. + fz_tell_output: Return the current file position. Throw an error on unseekable outputs. */ -int fz_write(fz_context *, fz_output *out, const void *data, int len); +void fz_seek_output(fz_context *ctx, fz_output *out, fz_off_t off, int whence); +fz_off_t fz_tell_output(fz_context *ctx, fz_output *out); /* - fz_putc: putc equivalent for output streams. + fz_drop_output: Close and free an output stream. */ -void fz_putc(fz_context *, fz_output *out, char c); +void fz_drop_output(fz_context *, fz_output *); /* - fz_drop_output: Close a previously opened fz_output stream. - - Note: whether or not this closes the underlying output method is - method dependent. FILE * streams created by fz_new_output_with_file - are NOT closed. + fz_write_int32be: Write a big-endian 32-bit binary integer. + fz_write_int32le: Write a little-endian 32-bit binary integer. + fz_write_byte: Write a single byte. + fz_write_rune: Write a UTF-8 encoded unicode character. */ -void fz_drop_output(fz_context *, fz_output *); static inline int fz_write_int32be(fz_context *ctx, fz_output *out, int x) { @@ -83,20 +79,19 @@ static inline int fz_write_int32le(fz_context *ctx, fz_output *out, int x) return fz_write(ctx, out, data, 4); } -static inline void -fz_write_byte(fz_context *ctx, fz_output *out, int x) +static inline void fz_write_byte(fz_context *ctx, fz_output *out, int x) { char data = x; fz_write(ctx, out, &data, 1); } -/* - fz_vfprintf: Our customised vfprintf routine. Same supported - format specifiers as for fz_vsnprintf. -*/ -int fz_vfprintf(fz_context *ctx, FILE *file, const char *fmt, va_list ap); -int fz_fprintf(fz_context *ctx, FILE *file, const char *fmt, ...); +static inline void fz_write_rune(fz_context *ctx, fz_output *out, int rune) +{ + char data[10]; + + fz_write(ctx, out, data, fz_runetochar(data, rune)); +} /* fz_vsnprintf: Our customised vsnprintf routine. Takes %c, %d, %o, %s, %u, %x, as usual. diff --git a/include/mupdf/fitz/path.h b/include/mupdf/fitz/path.h index 81b6f7bb..0a602f18 100644 --- a/include/mupdf/fitz/path.h +++ b/include/mupdf/fitz/path.h @@ -92,8 +92,6 @@ fz_stroke_state *fz_unshare_stroke_state(fz_context *ctx, fz_stroke_state *share fz_stroke_state *fz_unshare_stroke_state_with_dash_len(fz_context *ctx, fz_stroke_state *shared, int len); fz_stroke_state *fz_clone_stroke_state(fz_context *ctx, fz_stroke_state *stroke); -#ifndef NDEBUG -void fz_print_path(fz_context *ctx, FILE *out, fz_path *, int indent); -#endif +void fz_print_path(fz_context *ctx, fz_output *out, fz_path *, int indent); #endif diff --git a/include/mupdf/fitz/shade.h b/include/mupdf/fitz/shade.h index 55e8767b..56adea5b 100644 --- a/include/mupdf/fitz/shade.h +++ b/include/mupdf/fitz/shade.h @@ -96,8 +96,6 @@ typedef void (fz_mesh_process_fn)(fz_context *ctx, void *arg, fz_vertex *av, fz_ void fz_process_mesh(fz_context *ctx, fz_shade *shade, const fz_matrix *ctm, fz_mesh_prepare_fn *prepare, fz_mesh_process_fn *process, void *process_arg); -#ifndef NDEBUG -void fz_print_shade(fz_context *ctx, FILE *out, fz_shade *shade); -#endif +void fz_print_shade(fz_context *ctx, fz_output *out, fz_shade *shade); #endif diff --git a/include/mupdf/fitz/store.h b/include/mupdf/fitz/store.h index 49e3cc17..3b7b69ab 100644 --- a/include/mupdf/fitz/store.h +++ b/include/mupdf/fitz/store.h @@ -92,9 +92,7 @@ struct fz_store_type_s void *(*keep_key)(fz_context *,void *); void (*drop_key)(fz_context *,void *); int (*cmp_key)(fz_context *ctx, void *, void *); -#ifndef NDEBUG - void (*debug)(fz_context *ctx, FILE *, void *); -#endif + void (*print)(fz_context *ctx, fz_output *out, void *); }; /* @@ -197,9 +195,7 @@ int fz_shrink_store(fz_context *ctx, unsigned int percent); /* fz_print_store: Dump the contents of the store for debugging. */ -#ifndef NDEBUG -void fz_print_store(fz_context *ctx, FILE *out); -void fz_print_store_locked(fz_context *ctx, FILE *out); -#endif +void fz_print_store(fz_context *ctx, fz_output *out); +void fz_print_store_locked(fz_context *ctx, fz_output *out); #endif diff --git a/include/mupdf/fitz/stream.h b/include/mupdf/fitz/stream.h index 8e9ee638..d9cb7509 100644 --- a/include/mupdf/fitz/stream.h +++ b/include/mupdf/fitz/stream.h @@ -5,6 +5,11 @@ #include "mupdf/fitz/context.h" #include "mupdf/fitz/buffer.h" +/* + fz_file_exists: Return true if the named file exists and is readable. +*/ +int fz_file_exists(fz_context *ctx, const char *path); + /* fz_stream is a buffered reader capable of seeking in both directions. diff --git a/include/mupdf/fitz/text.h b/include/mupdf/fitz/text.h index eb2ad12e..303731bc 100644 --- a/include/mupdf/fitz/text.h +++ b/include/mupdf/fitz/text.h @@ -44,6 +44,5 @@ void fz_drop_text(fz_context *ctx, fz_text *text); void fz_add_text(fz_context *ctx, fz_text *text, int gid, int ucs, float x, float y); fz_rect *fz_bound_text(fz_context *ctx, fz_text *text, const fz_stroke_state *stroke, const fz_matrix *ctm, fz_rect *r); -void fz_print_text(fz_context *ctx, FILE *out, fz_text*); #endif diff --git a/include/mupdf/pdf/cmap.h b/include/mupdf/pdf/cmap.h index e17ebbaa..7b437b1d 100644 --- a/include/mupdf/pdf/cmap.h +++ b/include/mupdf/pdf/cmap.h @@ -81,8 +81,6 @@ pdf_cmap *pdf_load_system_cmap(fz_context *ctx, char *name); pdf_cmap *pdf_load_builtin_cmap(fz_context *ctx, char *name); pdf_cmap *pdf_load_embedded_cmap(fz_context *ctx, pdf_document *doc, pdf_obj *ref); -#ifndef NDEBUG -void pdf_print_cmap(fz_context *ctx, pdf_cmap *cmap); -#endif +void pdf_print_cmap(fz_context *ctx, fz_output *out, pdf_cmap *cmap); #endif diff --git a/include/mupdf/pdf/crypt.h b/include/mupdf/pdf/crypt.h index 00f36322..18260dbb 100644 --- a/include/mupdf/pdf/crypt.h +++ b/include/mupdf/pdf/crypt.h @@ -19,9 +19,7 @@ char *pdf_crypt_method(fz_context *ctx, pdf_document *doc); int pdf_crypt_length(fz_context *ctx, pdf_document *doc); unsigned char *pdf_crypt_key(fz_context *ctx, pdf_document *doc); -#ifndef NDEBUG -void pdf_print_crypt(fz_context *ctx, pdf_crypt *crypt); -#endif +void pdf_print_crypt(fz_context *ctx, fz_output *out, pdf_crypt *crypt); typedef struct pdf_designated_name_s { diff --git a/include/mupdf/pdf/font.h b/include/mupdf/pdf/font.h index 7697053f..9bd8d913 100644 --- a/include/mupdf/pdf/font.h +++ b/include/mupdf/pdf/font.h @@ -116,9 +116,7 @@ pdf_font_desc *pdf_new_font_desc(fz_context *ctx); pdf_font_desc *pdf_keep_font(fz_context *ctx, pdf_font_desc *fontdesc); void pdf_drop_font(fz_context *ctx, pdf_font_desc *font); -#ifndef NDEBUG -void pdf_print_font(fz_context *ctx, pdf_font_desc *fontdesc); -#endif +void pdf_print_font(fz_context *ctx, fz_output *out, pdf_font_desc *fontdesc); fz_rect *pdf_measure_text(fz_context *ctx, pdf_font_desc *fontdesc, unsigned char *buf, int len, fz_rect *rect); float pdf_text_stride(fz_context *ctx, pdf_font_desc *fontdesc, float fontsize, unsigned char *buf, int len, float room, int *count); diff --git a/include/mupdf/pdf/object.h b/include/mupdf/pdf/object.h index 84dbc4fe..c196bece 100644 --- a/include/mupdf/pdf/object.h +++ b/include/mupdf/pdf/object.h @@ -131,13 +131,7 @@ int pdf_obj_refs(fz_context *ctx, pdf_obj *ref); int pdf_obj_parent_num(fz_context *ctx, pdf_obj *obj); int pdf_sprint_obj(fz_context *ctx, char *s, int n, pdf_obj *obj, int tight); -int pdf_fprint_obj(fz_context *ctx, FILE *fp, pdf_obj *obj, int tight); -int pdf_output_obj(fz_context *ctx, fz_output *out, pdf_obj *obj, int tight); - -#ifndef NDEBUG -void pdf_print_obj(fz_context *ctx, pdf_obj *obj); -void pdf_print_ref(fz_context *ctx, pdf_obj *obj); -#endif +int pdf_print_obj(fz_context *ctx, fz_output *out, pdf_obj *obj, int tight); char *pdf_to_utf8(fz_context *ctx, pdf_document *doc, pdf_obj *src); unsigned short *pdf_to_ucs2(fz_context *ctx, pdf_document *doc, pdf_obj *src); diff --git a/source/fitz/bitmap.c b/source/fitz/bitmap.c index 4ae74932..5d2af061 100644 --- a/source/fitz/bitmap.c +++ b/source/fitz/bitmap.c @@ -45,36 +45,37 @@ fz_clear_bitmap(fz_context *ctx, fz_bitmap *bit) memset(bit->samples, 0, bit->stride * bit->h); } -/* - * Write bitmap to PBM file - */ - void -fz_write_pbm(fz_context *ctx, fz_bitmap *bitmap, char *filename) +fz_output_pbm(fz_context *ctx, fz_output *out, fz_bitmap *bitmap) { - FILE *fp; unsigned char *p; int h, bytestride; - fp = fz_fopen(filename, "wb"); - if (!fp) - fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file '%s': %s", filename, strerror(errno)); + if (bitmap->n != 1) + fz_throw(ctx, FZ_ERROR_GENERIC, "too many color components in bitmap"); - assert(bitmap->n == 1); - - fprintf(fp, "P4\n%d %d\n", bitmap->w, bitmap->h); + fz_printf(ctx, out, "P4\n%d %d\n", bitmap->w, bitmap->h); p = bitmap->samples; - h = bitmap->h; bytestride = (bitmap->w + 7) >> 3; while (h--) { - fwrite(p, 1, bytestride, fp); + fz_write(ctx, out, p, bytestride); p += bitmap->stride; } +} - fclose(fp); +void +fz_write_pbm(fz_context *ctx, fz_bitmap *bitmap, char *filename) +{ + fz_output *out = fz_new_output_with_path(ctx, filename, 0); + fz_try(ctx) + fz_output_pbm(ctx, out, bitmap); + fz_always(ctx) + fz_drop_output(ctx, out); + fz_catch(ctx) + fz_rethrow(ctx); } fz_colorspace *fz_pixmap_colorspace(fz_context *ctx, fz_pixmap *pix) diff --git a/source/fitz/draw-device.c b/source/fitz/draw-device.c index fdb631f4..b109e6b7 100644 --- a/source/fitz/draw-device.c +++ b/source/fitz/draw-device.c @@ -1711,14 +1711,12 @@ fz_cmp_tile_key(fz_context *ctx, void *k0_, void *k1_) return k0->id == k1->id && k0->ctm[0] == k1->ctm[0] && k0->ctm[1] == k1->ctm[1] && k0->ctm[2] == k1->ctm[2] && k0->ctm[3] == k1->ctm[3]; } -#ifndef NDEBUG static void -fz_debug_tile(fz_context *ctx, FILE *out, void *key_) +fz_print_tile(fz_context *ctx, fz_output *out, void *key_) { tile_key *key = (tile_key *)key_; - fprintf(out, "(tile id=%x, ctm=%g %g %g %g) ", key->id, key->ctm[0], key->ctm[1], key->ctm[2], key->ctm[3]); + fz_printf(ctx, out, "(tile id=%x, ctm=%g %g %g %g) ", key->id, key->ctm[0], key->ctm[1], key->ctm[2], key->ctm[3]); } -#endif static fz_store_type fz_tile_store_type = { @@ -1726,9 +1724,7 @@ static fz_store_type fz_tile_store_type = fz_keep_tile_key, fz_drop_tile_key, fz_cmp_tile_key, -#ifndef NDEBUG - fz_debug_tile -#endif + fz_print_tile }; static void diff --git a/source/fitz/font.c b/source/fitz/font.c index d5296eb9..75aa62c9 100644 --- a/source/fitz/font.c +++ b/source/fitz/font.c @@ -1218,33 +1218,31 @@ fz_render_t3_glyph_direct(fz_context *ctx, fz_device *dev, fz_font *font, int gi font->t3run(ctx, font->t3doc, font->t3resources, contents, dev, &ctm, gstate, nested_depth); } -#ifndef NDEBUG void -fz_print_font(fz_context *ctx, FILE *out, fz_font *font) +fz_print_font(fz_context *ctx, fz_output *out, fz_font *font) { - fprintf(out, "font '%s' {\n", font->name); + fz_printf(ctx, out, "font '%s' {\n", font->name); if (font->ft_face) { - fprintf(out, "\tfreetype face %p\n", font->ft_face); + fz_printf(ctx, out, "\tfreetype face %p\n", font->ft_face); if (font->ft_substitute) - fprintf(out, "\tsubstitute font\n"); + fz_printf(ctx, out, "\tsubstitute font\n"); } if (font->t3procs) { - fprintf(out, "\ttype3 matrix [%g %g %g %g]\n", + fz_printf(ctx, out, "\ttype3 matrix [%g %g %g %g]\n", font->t3matrix.a, font->t3matrix.b, font->t3matrix.c, font->t3matrix.d); - fprintf(out, "\ttype3 bbox [%g %g %g %g]\n", + fz_printf(ctx, out, "\ttype3 bbox [%g %g %g %g]\n", font->bbox.x0, font->bbox.y0, font->bbox.x1, font->bbox.y1); } - fprintf(out, "}\n"); + fz_printf(ctx, out, "}\n"); } -#endif fz_rect * fz_bound_glyph(fz_context *ctx, fz_font *font, int gid, const fz_matrix *trm, fz_rect *rect) diff --git a/source/fitz/hash.c b/source/fitz/hash.c index f80e79b3..6c4d9a5e 100644 --- a/source/fitz/hash.c +++ b/source/fitz/hash.c @@ -324,34 +324,32 @@ fz_hash_remove_fast(fz_context *ctx, fz_hash_table *table, const void *key, unsi do_removal(ctx, table, key, pos); } -#ifndef NDEBUG void -fz_print_hash(fz_context *ctx, FILE *out, fz_hash_table *table) +fz_print_hash(fz_context *ctx, fz_output *out, fz_hash_table *table) { fz_print_hash_details(ctx, out, table, NULL); } void -fz_print_hash_details(fz_context *ctx, FILE *out, fz_hash_table *table, void (*details)(FILE *,void*)) +fz_print_hash_details(fz_context *ctx, fz_output *out, fz_hash_table *table, void (*details)(fz_context*,fz_output*,void*)) { int i, k; - fprintf(out, "cache load %d / %d\n", table->load, table->size); + fz_printf(ctx, out, "cache load %d / %d\n", table->load, table->size); for (i = 0; i < table->size; i++) { if (!table->ents[i].val) - fprintf(out, "table % 4d: empty\n", i); + fz_printf(ctx, out, "table % 4d: empty\n", i); else { - fprintf(out, "table % 4d: key=", i); + fz_printf(ctx, out, "table % 4d: key=", i); for (k = 0; k < MAX_KEY_LEN; k++) - fprintf(out, "%02x", ((char*)table->ents[i].key)[k]); + fz_printf(ctx, out, "%02x", ((char*)table->ents[i].key)[k]); if (details) - details(out, table->ents[i].val); + details(ctx, out, table->ents[i].val); else - fprintf(out, " val=$%p\n", table->ents[i].val); + fz_printf(ctx, out, " val=$%p\n", table->ents[i].val); } } } -#endif diff --git a/source/fitz/image.c b/source/fitz/image.c index 4728c5f3..60c3578d 100644 --- a/source/fitz/image.c +++ b/source/fitz/image.c @@ -65,15 +65,12 @@ fz_cmp_image_key(fz_context *ctx, void *k0_, void *k1_) return k0->image == k1->image && k0->l2factor == k1->l2factor; } -#ifndef NDEBUG static void -fz_debug_image(fz_context *ctx, FILE *out, void *key_) +fz_print_image(fz_context *ctx, fz_output *out, void *key_) { fz_image_key *key = (fz_image_key *)key_; - - fprintf(out, "(image %d x %d sf=%d) ", key->image->w, key->image->h, key->l2factor); + fz_printf(ctx, out, "(image %d x %d sf=%d) ", key->image->w, key->image->h, key->l2factor); } -#endif static fz_store_type fz_image_store_type = { @@ -81,9 +78,7 @@ static fz_store_type fz_image_store_type = fz_keep_image_key, fz_drop_image_key, fz_cmp_image_key, -#ifndef NDEBUG - fz_debug_image -#endif + fz_print_image }; static void diff --git a/source/fitz/output-pcl.c b/source/fitz/output-pcl.c index 1299e73d..9d21a6d3 100644 --- a/source/fitz/output-pcl.c +++ b/source/fitz/output-pcl.c @@ -792,57 +792,23 @@ fz_output_pcl_bitmap(fz_context *ctx, fz_output *out, const fz_bitmap *bitmap, f void fz_write_pcl(fz_context *ctx, fz_pixmap *pixmap, char *filename, int append, fz_pcl_options *pcl) { - FILE *fp; - fz_output *out = NULL; - - fp = fz_fopen(filename, append ? "ab" : "wb"); - if (!fp) - { - fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file '%s': %s", filename, strerror(errno)); - } - - fz_var(out); - + fz_output *out = fz_new_output_with_path(ctx, filename, append); fz_try(ctx) - { - out = fz_new_output_with_file(ctx, fp, 1); fz_output_pcl(ctx, out, pixmap, pcl); - } fz_always(ctx) - { fz_drop_output(ctx, out); - } fz_catch(ctx) - { fz_rethrow(ctx); - } } void fz_write_pcl_bitmap(fz_context *ctx, fz_bitmap *bitmap, char *filename, int append, fz_pcl_options *pcl) { - FILE *fp; - fz_output *out = NULL; - - fp = fz_fopen(filename, append ? "ab" : "wb"); - if (!fp) - { - fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file '%s': %s", filename, strerror(errno)); - } - - fz_var(out); - + fz_output *out = fz_new_output_with_path(ctx, filename, append); fz_try(ctx) - { - out = fz_new_output_with_file(ctx, fp, 1); fz_output_pcl_bitmap(ctx, out, bitmap, pcl); - } fz_always(ctx) - { fz_drop_output(ctx, out); - } fz_catch(ctx) - { fz_rethrow(ctx); - } } diff --git a/source/fitz/output-pwg.c b/source/fitz/output-pwg.c index 205966d3..d46dd04f 100644 --- a/source/fitz/output-pwg.c +++ b/source/fitz/output-pwg.c @@ -260,61 +260,31 @@ fz_output_pwg(fz_context *ctx, fz_output *out, const fz_pixmap *pixmap, const fz void fz_write_pwg(fz_context *ctx, fz_pixmap *pixmap, char *filename, int append, const fz_pwg_options *pwg) { - FILE *fp; - fz_output *out = NULL; - - fp = fz_fopen(filename, append ? "ab" : "wb"); - if (!fp) - { - fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file '%s': %s", filename, strerror(errno)); - } - - fz_var(out); - + fz_output *out = fz_new_output_with_path(ctx, filename, append); fz_try(ctx) { - out = fz_new_output_with_file(ctx, fp, 1); if (!append) fz_output_pwg_file_header(ctx, out); fz_output_pwg_page(ctx, out, pixmap, pwg); } fz_always(ctx) - { fz_drop_output(ctx, out); - } fz_catch(ctx) - { fz_rethrow(ctx); - } } void fz_write_pwg_bitmap(fz_context *ctx, fz_bitmap *bitmap, char *filename, int append, const fz_pwg_options *pwg) { - FILE *fp; - fz_output *out = NULL; - - fp = fz_fopen(filename, append ? "ab" : "wb"); - if (!fp) - { - fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file '%s': %s", filename, strerror(errno)); - } - - fz_var(out); - + fz_output *out = fz_new_output_with_path(ctx, filename, append); fz_try(ctx) { - out = fz_new_output_with_file(ctx, fp, 1); if (!append) fz_output_pwg_file_header(ctx, out); fz_output_pwg_bitmap_page(ctx, out, bitmap, pwg); } fz_always(ctx) - { fz_drop_output(ctx, out); - } fz_catch(ctx) - { fz_rethrow(ctx); - } } diff --git a/source/fitz/output.c b/source/fitz/output.c index 027f66c3..e39dd352 100644 --- a/source/fitz/output.c +++ b/source/fitz/output.c @@ -3,61 +3,74 @@ struct fz_output_s { void *opaque; - int (*printf)(fz_context *, void *opaque, const char *, va_list ap); int (*write)(fz_context *, void *opaque, const void *, int n); + void (*seek)(fz_context *, void *opaque, fz_off_t off, int whence); + fz_off_t (*tell)(fz_context *, void *opaque); void (*close)(fz_context *, void *opaque); }; static int -file_printf(fz_context *ctx, void *opaque, const char *fmt, va_list ap) +file_write(fz_context *ctx, void *opaque, const void *buffer, int count) { FILE *file = opaque; - return fz_vfprintf(ctx, file, fmt, ap); + size_t n = fwrite(buffer, 1, count, file); + if (n < count && ferror(file)) + fz_throw(ctx, FZ_ERROR_GENERIC, "cannot fwrite: %s", strerror(errno)); + return n; } -static int -file_write(fz_context *ctx, void *opaque, const void *buffer, int count) +static void +file_seek(fz_context *ctx, void *opaque, fz_off_t off, int whence) { FILE *file = opaque; - return fwrite(buffer, 1, count, file); + int n = fz_fseek(file, off, whence); + if (n < 0) + fz_throw(ctx, FZ_ERROR_GENERIC, "cannot fseek: %s", strerror(errno)); +} + +static fz_off_t +file_tell(fz_context *ctx, void *opaque) +{ + FILE *file = opaque; + fz_off_t off = fz_ftell(file); + if (off == -1) + fz_throw(ctx, FZ_ERROR_GENERIC, "cannot ftell: %s", strerror(errno)); + return off; } static void file_close(fz_context *ctx, void *opaque) { FILE *file = opaque; - fclose(file); + int n = fclose(file); + if (n < 0) + fz_throw(ctx, FZ_ERROR_GENERIC, "cannot fclose: %s", strerror(errno)); } fz_output * -fz_new_output_with_file(fz_context *ctx, FILE *file, int close) +fz_new_output_with_file_ptr(fz_context *ctx, FILE *file, int close) { fz_output *out = fz_malloc_struct(ctx, fz_output); out->opaque = file; - out->printf = file_printf; out->write = file_write; + out->seek = file_seek; + out->tell = file_tell; out->close = close ? file_close : NULL; return out; } fz_output * -fz_new_output_to_filename(fz_context *ctx, const char *filename) +fz_new_output_with_path(fz_context *ctx, const char *filename, int append) { fz_output *out = NULL; - FILE *file = fz_fopen(filename, "wb"); + FILE *file = fz_fopen(filename, append ? "ab" : "wb"); if (!file) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file '%s': %s", filename, strerror(errno)); - fz_var(ctx); - fz_try(ctx) { - out = fz_malloc_struct(ctx, fz_output); - out->opaque = file; - out->printf = file_printf; - out->write = file_write; - out->close = file_close; + out = fz_new_output_with_file_ptr(ctx, file, 1); } fz_catch(ctx) { @@ -67,84 +80,114 @@ fz_new_output_to_filename(fz_context *ctx, const char *filename) return out; } -void -fz_drop_output(fz_context *ctx, fz_output *out) +static int +buffer_write(fz_context *ctx, void *opaque, const void *data, int len) { - if (!out) - return; - if (out->close) - out->close(ctx, out->opaque); - fz_free(ctx, out); + fz_buffer *buffer = opaque; + fz_write_buffer(ctx, buffer, (unsigned char *)data, len); + return len; } -int -fz_printf(fz_context *ctx, fz_output *out, const char *fmt, ...) +static void +buffer_seek(fz_context *ctx, void *opaque, fz_off_t off, int whence) { - int ret; - va_list ap; - - if (!out) - return 0; + fz_throw(ctx, FZ_ERROR_GENERIC, "cannot seek in buffer: %s", strerror(errno)); +} - va_start(ap, fmt); - ret = out->printf(ctx, out->opaque, fmt, ap); - va_end(ap); +static fz_off_t +buffer_tell(fz_context *ctx, void *opaque) +{ + fz_buffer *buffer = opaque; + return buffer->len; +} - return ret; +static void +buffer_close(fz_context *ctx, void *opaque) +{ + fz_buffer *buffer = opaque; + fz_drop_buffer(ctx, buffer); } -int -fz_write(fz_context *ctx, fz_output *out, const void *data, int len) +fz_output * +fz_new_output_with_buffer(fz_context *ctx, fz_buffer *buf) { - if (!out) - return 0; - return out->write(ctx, out->opaque, data, len); + fz_output *out = fz_malloc_struct(ctx, fz_output); + out->opaque = fz_keep_buffer(ctx, buf); + out->write = buffer_write; + out->seek = buffer_seek; + out->tell = buffer_tell; + out->close = buffer_close; + return out; } void -fz_putc(fz_context *ctx, fz_output *out, char c) +fz_drop_output(fz_context *ctx, fz_output *out) { - if (out) - (void)out->write(ctx, out->opaque, &c, 1); + if (!out) return; + if (out->close) + out->close(ctx, out->opaque); + fz_free(ctx, out); } -int -fz_puts(fz_context *ctx, fz_output *out, const char *str) +void +fz_seek_output(fz_context *ctx, fz_output *out, fz_off_t off, int whence) { - if (!out) - return 0; - return out->write(ctx, out->opaque, str, strlen(str)); + if (!out) return; + out->seek(ctx, out->opaque, off, whence); } -static int -buffer_printf(fz_context *ctx, void *opaque, const char *fmt, va_list list) +fz_off_t +fz_tell_output(fz_context *ctx, fz_output *out) { - fz_buffer *buffer = opaque; - return fz_buffer_vprintf(ctx, buffer, fmt, list); + if (!out) return 0; + return out->tell(ctx, out->opaque); } -static int -buffer_write(fz_context *ctx, void *opaque, const void *data, int len) +void +fz_vprintf(fz_context *ctx, fz_output *out, const char *fmt, va_list old_args) { - fz_buffer *buffer = opaque; - fz_write_buffer(ctx, buffer, (unsigned char *)data, len); - return len; + char buffer[256], *p = buffer; + int len; + va_list args; + + if (!out) return; + + /* First try using our fixed size buffer */ + va_copy(args, old_args); + len = fz_vsnprintf(buffer, sizeof buffer, fmt, args); + va_copy_end(args); + + /* If that failed, allocate a big enough buffer */ + if (len >= sizeof buffer) + { + p = fz_malloc(ctx, len + 1); + va_copy(args, old_args); + fz_vsnprintf(p, len + 1, fmt, args); + va_copy_end(args); + } + + fz_try(ctx) + out->write(ctx, out->opaque, p, len); + fz_always(ctx) + if (p != buffer) + fz_free(ctx, p); + fz_catch(ctx) + fz_rethrow(ctx); } -static void -buffer_close(fz_context *ctx, void *opaque) +void +fz_printf(fz_context *ctx, fz_output *out, const char *fmt, ...) { - fz_buffer *buffer = opaque; - fz_drop_buffer(ctx, buffer); + va_list args; + if (!out) return; + va_start(args, fmt); + fz_vprintf(ctx, out, fmt, args); + va_end(args); } -fz_output * -fz_new_output_with_buffer(fz_context *ctx, fz_buffer *buf) +int +fz_write(fz_context *ctx, fz_output *out, const void *data, int len) { - fz_output *out = fz_malloc_struct(ctx, fz_output); - out->opaque = fz_keep_buffer(ctx, buf); - out->printf = buffer_printf; - out->write = buffer_write; - out->close = buffer_close; - return out; + if (!out) return 0; + return out->write(ctx, out->opaque, data, len); } diff --git a/source/fitz/path.c b/source/fitz/path.c index a1ee1c15..5481a90d 100644 --- a/source/fitz/path.c +++ b/source/fitz/path.c @@ -1338,9 +1338,8 @@ void fz_trim_path(fz_context *ctx, fz_path *path) } } -#ifndef NDEBUG void -fz_print_path(fz_context *ctx, FILE *out, fz_path *path, int indent) +fz_print_path(fz_context *ctx, fz_output *out, fz_path *path, int indent) { float x, y; int i = 0, k = 0; @@ -1350,46 +1349,46 @@ fz_print_path(fz_context *ctx, FILE *out, fz_path *path, int indent) uint8_t cmd = path->cmds[i++]; for (n = 0; n < indent; n++) - fputc(' ', out); + fz_putc(ctx, out, ' '); switch (cmd) { case FZ_MOVETO: case FZ_MOVETOCLOSE: x = path->coords[k++]; y = path->coords[k++]; - fprintf(out, "%g %g m%s\n", x, y, cmd == FZ_MOVETOCLOSE ? " z" : ""); + fz_printf(ctx, out, "%g %g m%s\n", x, y, cmd == FZ_MOVETOCLOSE ? " z" : ""); break; case FZ_LINETO: case FZ_LINETOCLOSE: x = path->coords[k++]; y = path->coords[k++]; - fprintf(out, "%g %g l%s\n", x, y, cmd == FZ_LINETOCLOSE ? " z" : ""); + fz_printf(ctx, out, "%g %g l%s\n", x, y, cmd == FZ_LINETOCLOSE ? " z" : ""); break; case FZ_DEGENLINETO: case FZ_DEGENLINETOCLOSE: - fprintf(out, "d%s\n", cmd == FZ_DEGENLINETOCLOSE ? " z" : ""); + fz_printf(ctx, out, "d%s\n", cmd == FZ_DEGENLINETOCLOSE ? " z" : ""); break; case FZ_HORIZTO: case FZ_HORIZTOCLOSE: x = path->coords[k++]; - fprintf(out, "%g h%s\n", x, cmd == FZ_HORIZTOCLOSE ? " z" : ""); + fz_printf(ctx, out, "%g h%s\n", x, cmd == FZ_HORIZTOCLOSE ? " z" : ""); break; case FZ_VERTTOCLOSE: case FZ_VERTTO: y = path->coords[k++]; - fprintf(out, "%g i%s\n", y, cmd == FZ_VERTTOCLOSE ? " z" : ""); + fz_printf(ctx, out, "%g i%s\n", y, cmd == FZ_VERTTOCLOSE ? " z" : ""); break; case FZ_CURVETOCLOSE: case FZ_CURVETO: x = path->coords[k++]; y = path->coords[k++]; - fprintf(out, "%g %g ", x, y); + fz_printf(ctx, out, "%g %g ", x, y); x = path->coords[k++]; y = path->coords[k++]; - fprintf(out, "%g %g ", x, y); + fz_printf(ctx, out, "%g %g ", x, y); x = path->coords[k++]; y = path->coords[k++]; - fprintf(out, "%g %g c%s\n", x, y, cmd == FZ_CURVETOCLOSE ? " z" : ""); + fz_printf(ctx, out, "%g %g c%s\n", x, y, cmd == FZ_CURVETOCLOSE ? " z" : ""); break; case FZ_CURVETOVCLOSE: case FZ_CURVETOV: @@ -1397,23 +1396,22 @@ fz_print_path(fz_context *ctx, FILE *out, fz_path *path, int indent) case FZ_CURVETOY: x = path->coords[k++]; y = path->coords[k++]; - fprintf(out, "%g %g ", x, y); + fz_printf(ctx, out, "%g %g ", x, y); x = path->coords[k++]; y = path->coords[k++]; - fprintf(out, "%g %g %c%s\n", x, y, (cmd == FZ_CURVETOVCLOSE || cmd == FZ_CURVETOV ? 'v' : 'y'), (cmd == FZ_CURVETOVCLOSE || cmd == FZ_CURVETOYCLOSE) ? " z" : ""); + fz_printf(ctx, out, "%g %g %c%s\n", x, y, (cmd == FZ_CURVETOVCLOSE || cmd == FZ_CURVETOV ? 'v' : 'y'), (cmd == FZ_CURVETOVCLOSE || cmd == FZ_CURVETOYCLOSE) ? " z" : ""); break; case FZ_RECTTO: x = path->coords[k++]; y = path->coords[k++]; - fprintf(out, "%g %g ", x, y); + fz_printf(ctx, out, "%g %g ", x, y); x = path->coords[k++]; y = path->coords[k++]; - fprintf(out, "%g %g r\n", x, y); + fz_printf(ctx, out, "%g %g r\n", x, y); break; } } } -#endif const fz_stroke_state fz_default_stroke_state = { -2, /* -2 is the magic number we use when we have stroke states stored on the stack */ diff --git a/source/fitz/pixmap.c b/source/fitz/pixmap.c index fa4843bc..9f1ec20e 100644 --- a/source/fitz/pixmap.c +++ b/source/fitz/pixmap.c @@ -575,10 +575,17 @@ fz_output_pnm_band(fz_context *ctx, fz_output *out, int w, int h, int n, int ban } } +void +fz_output_pnm(fz_context *ctx, fz_output *out, fz_pixmap *pixmap) +{ + fz_output_pnm_header(ctx, out, pixmap->w, pixmap->h, pixmap->n); + fz_output_pnm_band(ctx, out, pixmap->w, pixmap->h, pixmap->n, 0, pixmap->h, pixmap->samples); +} + void fz_write_pnm(fz_context *ctx, fz_pixmap *pixmap, char *filename) { - fz_output *out = fz_new_output_to_filename(ctx, filename); + fz_output *out = fz_new_output_with_path(ctx, filename, 0); fz_output_pnm_header(ctx, out, pixmap->w, pixmap->h, pixmap->n); fz_output_pnm_band(ctx, out, pixmap->w, pixmap->h, pixmap->n, 0, pixmap->h, pixmap->samples); fz_drop_output(ctx, out); @@ -637,10 +644,17 @@ fz_output_pam_band(fz_context *ctx, fz_output *out, int w, int h, int n, int ban } } +void +fz_output_pam(fz_context *ctx, fz_output *out, fz_pixmap *pixmap, int savealpha) +{ + fz_output_pam_header(ctx, out, pixmap->w, pixmap->h, pixmap->n, savealpha); + fz_output_pam_band(ctx, out, pixmap->w, pixmap->h, pixmap->n, 0, pixmap->h, pixmap->samples, savealpha); +} + void fz_write_pam(fz_context *ctx, fz_pixmap *pixmap, char *filename, int savealpha) { - fz_output *out = fz_new_output_to_filename(ctx, filename); + fz_output *out = fz_new_output_with_path(ctx, filename, 0); fz_output_pam_header(ctx, out, pixmap->w, pixmap->h, pixmap->n, savealpha); fz_output_pam_band(ctx, out, pixmap->w, pixmap->h, pixmap->n, 0, pixmap->h, pixmap->samples, savealpha); fz_drop_output(ctx, out); @@ -675,7 +689,7 @@ static void putchunk(fz_context *ctx, fz_output *out, char *tag, unsigned char * void fz_write_png(fz_context *ctx, fz_pixmap *pixmap, char *filename, int savealpha) { - fz_output *out = fz_new_output_to_filename(ctx, filename); + fz_output *out = fz_new_output_with_path(ctx, filename, 0); fz_png_output_context *poc = NULL; fz_var(poc); @@ -979,7 +993,7 @@ fz_write_tga(fz_context *ctx, fz_pixmap *pixmap, const char *filename, int savea fz_throw(ctx, FZ_ERROR_GENERIC, "pixmap must be grayscale or rgb to write as tga"); } - out = fz_new_output_to_filename(ctx, filename); + out = fz_new_output_with_path(ctx, filename, 0); memset(head, 0, sizeof(head)); head[2] = n == 4 ? 10 : 11; diff --git a/source/fitz/printf.c b/source/fitz/printf.c index c554540b..dcc89952 100644 --- a/source/fitz/printf.c +++ b/source/fitz/printf.c @@ -321,47 +321,6 @@ fz_vsnprintf(char *buffer, int space, const char *fmt, va_list args) return out.n - 1; } -int -fz_vfprintf(fz_context *ctx, FILE *file, const char *fmt, va_list old_args) -{ - char buffer[256]; - int l; - va_list args; - char *b = buffer; - - /* First try using our fixed size buffer */ - va_copy(args, old_args); - l = fz_vsnprintf(buffer, sizeof buffer, fmt, args); - va_copy_end(args); - - /* If that failed, allocate the right size buffer dynamically */ - if (l >= sizeof buffer) - { - b = fz_malloc(ctx, l + 1); - va_copy(args, old_args); - fz_vsnprintf(b, l + 1, fmt, args); - va_copy_end(args); - } - - l = fwrite(b, 1, l, file); - - if (b != buffer) - fz_free(ctx, b); - - return l; -} - -int -fz_fprintf(fz_context *ctx, FILE *file, const char *fmt, ...) -{ - int n; - va_list ap; - va_start(ap, fmt); - n = fz_vfprintf(ctx, file, fmt, ap); - va_end(ap); - return n; -} - int fz_snprintf(char *buffer, int space, const char *fmt, ...) { diff --git a/source/fitz/shade.c b/source/fitz/shade.c index f5e25639..d543b020 100644 --- a/source/fitz/shade.c +++ b/source/fitz/shade.c @@ -1086,45 +1086,43 @@ fz_bound_shade(fz_context *ctx, fz_shade *shade, const fz_matrix *ctm, fz_rect * return fz_transform_rect(s, &local_ctm); } -#ifndef NDEBUG void -fz_print_shade(fz_context *ctx, FILE *out, fz_shade *shade) +fz_print_shade(fz_context *ctx, fz_output *out, fz_shade *shade) { int i; - fprintf(out, "shading {\n"); + fz_printf(ctx, out, "shading {\n"); switch (shade->type) { - case FZ_FUNCTION_BASED: fprintf(out, "\ttype function_based\n"); break; - case FZ_LINEAR: fprintf(out, "\ttype linear\n"); break; - case FZ_RADIAL: fprintf(out, "\ttype radial\n"); break; - default: /* MESH */ fprintf(out, "\ttype mesh\n"); break; + case FZ_FUNCTION_BASED: fz_printf(ctx, out, "\ttype function_based\n"); break; + case FZ_LINEAR: fz_printf(ctx, out, "\ttype linear\n"); break; + case FZ_RADIAL: fz_printf(ctx, out, "\ttype radial\n"); break; + default: /* MESH */ fz_printf(ctx, out, "\ttype mesh\n"); break; } - fprintf(out, "\tbbox [%g %g %g %g]\n", + fz_printf(ctx, out, "\tbbox [%g %g %g %g]\n", shade->bbox.x0, shade->bbox.y0, shade->bbox.x1, shade->bbox.y1); - fprintf(out, "\tcolorspace %s\n", shade->colorspace->name); + fz_printf(ctx, out, "\tcolorspace %s\n", shade->colorspace->name); - fprintf(out, "\tmatrix [%g %g %g %g %g %g]\n", + fz_printf(ctx, out, "\tmatrix [%g %g %g %g %g %g]\n", shade->matrix.a, shade->matrix.b, shade->matrix.c, shade->matrix.d, shade->matrix.e, shade->matrix.f); if (shade->use_background) { - fprintf(out, "\tbackground ["); + fz_printf(ctx, out, "\tbackground ["); for (i = 0; i < shade->colorspace->n; i++) - fprintf(out, "%s%g", i == 0 ? "" : " ", shade->background[i]); - fprintf(out, "]\n"); + fz_printf(ctx, out, "%s%g", i == 0 ? "" : " ", shade->background[i]); + fz_printf(ctx, out, "]\n"); } if (shade->use_function) { - fprintf(out, "\tfunction\n"); + fz_printf(ctx, out, "\tfunction\n"); } - fprintf(out, "}\n"); + fz_printf(ctx, out, "}\n"); } -#endif diff --git a/source/fitz/store.c b/source/fitz/store.c index ce83e87f..4bb54c19 100644 --- a/source/fitz/store.c +++ b/source/fitz/store.c @@ -484,52 +484,46 @@ fz_drop_store_context(fz_context *ctx) ctx->store = NULL; } -#ifndef NDEBUG static void -print_item(FILE *out, void *item_) +print_item(fz_context *ctx, fz_output *out, void *item_) { fz_item *item = (fz_item *)item_; - fprintf(out, " val=%p item=%p\n", item->val, item); - fflush(out); + fz_printf(ctx, out, " val=%p item=%p\n", item->val, item); } void -fz_print_store_locked(fz_context *ctx, FILE *out) +fz_print_store_locked(fz_context *ctx, fz_output *out) { fz_item *item, *next; fz_store *store = ctx->store; - fprintf(out, "-- resource store contents --\n"); - fflush(out); + fz_printf(ctx, out, "-- resource store contents --\n"); for (item = store->head; item; item = next) { next = item->next; if (next) next->val->refs++; - fprintf(out, "store[*][refs=%d][size=%d] ", item->val->refs, item->size); + fz_printf(ctx, out, "store[*][refs=%d][size=%d] ", item->val->refs, item->size); fz_unlock(ctx, FZ_LOCK_ALLOC); - item->type->debug(ctx, out, item->key); - fprintf(out, " = %p\n", item->val); - fflush(out); + item->type->print(ctx, out, item->key); + fz_printf(ctx, out, " = %p\n", item->val); fz_lock(ctx, FZ_LOCK_ALLOC); if (next) next->val->refs--; } - fprintf(out, "-- resource store hash contents --\n"); + fz_printf(ctx, out, "-- resource store hash contents --\n"); fz_print_hash_details(ctx, out, store->hash, print_item); - fprintf(out, "-- end --\n"); - fflush(out); + fz_printf(ctx, out, "-- end --\n"); } void -fz_print_store(fz_context *ctx, FILE *out) +fz_print_store(fz_context *ctx, fz_output *out) { fz_lock(ctx, FZ_LOCK_ALLOC); fz_print_store_locked(ctx, out); fz_unlock(ctx, FZ_LOCK_ALLOC); } -#endif /* This is now an n^2 algorithm - not ideal, but it'll only be bad if we are * actually managing to scavenge lots of blocks back. */ diff --git a/source/fitz/stream-open.c b/source/fitz/stream-open.c index b4df2a61..e9836dd1 100644 --- a/source/fitz/stream-open.c +++ b/source/fitz/stream-open.c @@ -1,5 +1,14 @@ #include "mupdf/fitz.h" +int +fz_file_exists(fz_context *ctx, const char *path) +{ + FILE *file = fz_fopen(path, "rb"); + if (file) + fclose(file); + return !!file; +} + fz_stream * fz_new_stream(fz_context *ctx, void *state, fz_stream_next_fn *next, fz_stream_close_fn *close) { diff --git a/source/fitz/text.c b/source/fitz/text.c index 466c869a..0616d6a9 100644 --- a/source/fitz/text.c +++ b/source/fitz/text.c @@ -109,31 +109,3 @@ fz_add_text(fz_context *ctx, fz_text *text, int gid, int ucs, float x, float y) text->items[text->len].y = y; text->len++; } - -static int -isxmlmeta(int c) -{ - return c < 32 || c >= 128 || c == '&' || c == '<' || c == '>' || c == '\'' || c == '"'; -} - -static void -do_print_text(FILE *out, fz_text *text, int indent) -{ - int i, n; - for (i = 0; i < text->len; i++) - { - for (n = 0; n < indent; n++) - fputc(' ', out); - if (!isxmlmeta(text->items[i].ucs)) - fprintf(out, "\n", - text->items[i].ucs, text->items[i].gid, text->items[i].x, text->items[i].y); - else - fprintf(out, "\n", - text->items[i].ucs, text->items[i].gid, text->items[i].x, text->items[i].y); - } -} - -void fz_print_text(fz_context *ctx, FILE *out, fz_text *text) -{ - do_print_text(out, text, 0); -} diff --git a/source/fitz/trace-device.c b/source/fitz/trace-device.c index 5ea716b4..59ea8ad8 100644 --- a/source/fitz/trace-device.c +++ b/source/fitz/trace-device.c @@ -1,73 +1,63 @@ #include "mupdf/fitz.h" +typedef struct fz_trace_device_s +{ + fz_device super; + fz_output *out; +} fz_trace_device; + static void -fz_trace_matrix(const fz_matrix *ctm) +fz_trace_matrix(fz_context *ctx, fz_output *out, const fz_matrix *ctm) { - printf(" matrix=\"%g %g %g %g %g %g\"", + fz_printf(ctx, out, " matrix=\"%g %g %g %g %g %g\"", ctm->a, ctm->b, ctm->c, ctm->d, ctm->e, ctm->f); } static void -fz_trace_trm(const fz_matrix *trm) +fz_trace_trm(fz_context *ctx, fz_output *out, const fz_matrix *trm) { - printf(" trm=\"%g %g %g %g\"", + fz_printf(ctx, out, " trm=\"%g %g %g %g\"", trm->a, trm->b, trm->c, trm->d); } static void -fz_trace_color(fz_colorspace *colorspace, float *color, float alpha) +fz_trace_color(fz_context *ctx, fz_output *out, fz_colorspace *colorspace, float *color, float alpha) { int i; - printf(" colorspace=\"%s\" color=\"", colorspace->name); + fz_printf(ctx, out, " colorspace=\"%s\" color=\"", colorspace->name); for (i = 0; i < colorspace->n; i++) - printf("%s%g", i == 0 ? "" : " ", color[i]); - printf("\""); + fz_printf(ctx, out, "%s%g", i == 0 ? "" : " ", color[i]); + fz_printf(ctx, out, "\""); if (alpha < 1) - printf(" alpha=\"%g\"", alpha); + fz_printf(ctx, out, " alpha=\"%g\"", alpha); } static void trace_moveto(fz_context *ctx, void *arg, float x, float y) { - int indent = (int)(intptr_t)arg; - int n; - - for (n = 0; n < indent; n++) - putchar(' '); - printf("\n", x, y); + fz_output *out = arg; + fz_printf(ctx, out, "\n", x, y); } static void trace_lineto(fz_context *ctx, void *arg, float x, float y) { - int indent = (int)(intptr_t)arg; - int n; - - for (n = 0; n < indent; n++) - putchar(' '); - printf("\n", x, y); + fz_output *out = arg; + fz_printf(ctx, out, "\n", x, y); } static void trace_curveto(fz_context *ctx, void *arg, float x1, float y1, float x2, float y2, float x3, float y3) { - int indent = (int)(intptr_t)arg; - int n; - - for (n = 0; n < indent; n++) - putchar(' '); - printf("\n", x1, y1, x2, y2, x3, y3); + fz_output *out = arg; + fz_printf(ctx, out, "\n", x1, y1, x2, y2, x3, y3); } static void trace_close(fz_context *ctx, void *arg) { - int indent = (int)(intptr_t)arg; - int n; - - for (n = 0; n < indent; n++) - putchar(' '); - printf("\n"); + fz_output *out = arg; + fz_printf(ctx, out, "\n"); } static const fz_path_processor trace_path_proc = @@ -79,219 +69,259 @@ static const fz_path_processor trace_path_proc = }; static void -fz_trace_path(fz_context *ctx, fz_path *path, int indent) +fz_trace_path(fz_context *ctx, fz_output *out, fz_path *path) { - fz_process_path(ctx, &trace_path_proc, (void *)(intptr_t)indent, path); + fz_process_path(ctx, &trace_path_proc, out, path); } static void fz_trace_begin_page(fz_context *ctx, fz_device *dev, const fz_rect *rect, const fz_matrix *ctm) { - printf("x0, rect->y0, rect->x1, rect->y1); - fz_trace_matrix(ctm); - printf(">\n"); + fz_output *out = ((fz_trace_device*)dev)->out; + fz_printf(ctx, out, "x0, rect->y0, rect->x1, rect->y1); + fz_trace_matrix(ctx, out, ctm); + fz_printf(ctx, out, ">\n"); } static void fz_trace_end_page(fz_context *ctx, fz_device *dev) { - printf("\n"); + fz_output *out = ((fz_trace_device*)dev)->out; + fz_printf(ctx, out, "\n"); } static void fz_trace_fill_path(fz_context *ctx, fz_device *dev, fz_path *path, int even_odd, const fz_matrix *ctm, fz_colorspace *colorspace, float *color, float alpha) { - printf("out; + fz_printf(ctx, out, "\n"); - fz_trace_path(ctx, path, 0); - printf("\n"); + fz_printf(ctx, out, " winding=\"nonzero\""); + fz_trace_color(ctx, out, colorspace, color, alpha); + fz_trace_matrix(ctx, out, ctm); + fz_printf(ctx, out, ">\n"); + fz_trace_path(ctx, out, path); + fz_printf(ctx, out, "\n"); } static void fz_trace_stroke_path(fz_context *ctx, fz_device *dev, fz_path *path, fz_stroke_state *stroke, const fz_matrix *ctm, fz_colorspace *colorspace, float *color, float alpha) { + fz_output *out = ((fz_trace_device*)dev)->out; int i; - printf("linewidth); - printf(" miterlimit=\"%g\"", stroke->miterlimit); - printf(" linecap=\"%d,%d,%d\"", stroke->start_cap, stroke->dash_cap, stroke->end_cap); - printf(" linejoin=\"%d\"", stroke->linejoin); + fz_printf(ctx, out, "linewidth); + fz_printf(ctx, out, " miterlimit=\"%g\"", stroke->miterlimit); + fz_printf(ctx, out, " linecap=\"%d,%d,%d\"", stroke->start_cap, stroke->dash_cap, stroke->end_cap); + fz_printf(ctx, out, " linejoin=\"%d\"", stroke->linejoin); if (stroke->dash_len) { - printf(" dash_phase=\"%g\" dash=\"", stroke->dash_phase); + fz_printf(ctx, out, " dash_phase=\"%g\" dash=\"", stroke->dash_phase); for (i = 0; i < stroke->dash_len; i++) - printf("%s%g", i > 0 ? " " : "", stroke->dash_list[i]); - printf("\""); + fz_printf(ctx, out, "%s%g", i > 0 ? " " : "", stroke->dash_list[i]); + fz_printf(ctx, out, "\""); } - fz_trace_color(colorspace, color, alpha); - fz_trace_matrix(ctm); - printf(">\n"); + fz_trace_color(ctx, out, colorspace, color, alpha); + fz_trace_matrix(ctx, out, ctm); + fz_printf(ctx, out, ">\n"); - fz_trace_path(ctx, path, 0); + fz_trace_path(ctx, out, path); - printf("\n"); + fz_printf(ctx, out, "\n"); } static void fz_trace_clip_path(fz_context *ctx, fz_device *dev, fz_path *path, const fz_rect *rect, int even_odd, const fz_matrix *ctm) { - printf("out; + fz_printf(ctx, out, "\n", rect->x0, rect->y0, rect->x1, rect->y1); + fz_printf(ctx, out, " contentbbox=\"%g %g %g %g\">\n", rect->x0, rect->y0, rect->x1, rect->y1); else - printf(">\n"); - fz_trace_path(ctx, path, 0); - printf("\n"); + fz_printf(ctx, out, ">\n"); + fz_trace_path(ctx, out, path); + fz_printf(ctx, out, "\n"); } static void fz_trace_clip_stroke_path(fz_context *ctx, fz_device *dev, fz_path *path, const fz_rect *rect, fz_stroke_state *stroke, const fz_matrix *ctm) { - printf("\n"); - fz_trace_path(ctx, path, 0); - printf("\n"); + fz_output *out = ((fz_trace_device*)dev)->out; + fz_printf(ctx, out, "\n"); + fz_trace_path(ctx, out, path); + fz_printf(ctx, out, "\n"); +} + +static int +isxmlmeta(int c) +{ + return c < 32 || c >= 128 || c == '&' || c == '<' || c == '>' || c == '\'' || c == '"'; +} + +static void +fz_trace_text(fz_context *ctx, fz_output *out, fz_text *text) +{ + int i; + for (i = 0; i < text->len; i++) + { + if (!isxmlmeta(text->items[i].ucs)) + fz_printf(ctx, out, "\n", + text->items[i].ucs, text->items[i].gid, text->items[i].x, text->items[i].y); + else + fz_printf(ctx, out, "\n", + text->items[i].ucs, text->items[i].gid, text->items[i].x, text->items[i].y); + } } static void fz_trace_fill_text(fz_context *ctx, fz_device *dev, fz_text *text, const fz_matrix *ctm, fz_colorspace *colorspace, float *color, float alpha) { - printf("font->name, text->wmode); - fz_trace_color(colorspace, color, alpha); - fz_trace_matrix(ctm); - fz_trace_trm(&text->trm); - printf(">\n"); - fz_print_text(ctx, stdout, text); - printf("\n"); + fz_output *out = ((fz_trace_device*)dev)->out; + fz_printf(ctx, out, "font->name, text->wmode); + fz_trace_color(ctx, out, colorspace, color, alpha); + fz_trace_matrix(ctx, out, ctm); + fz_trace_trm(ctx, out, &text->trm); + fz_printf(ctx, out, ">\n"); + fz_trace_text(ctx, out, text); + fz_printf(ctx, out, "\n"); } static void fz_trace_stroke_text(fz_context *ctx, fz_device *dev, fz_text *text, fz_stroke_state *stroke, const fz_matrix *ctm, fz_colorspace *colorspace, float *color, float alpha) { - printf("font->name, text->wmode); - fz_trace_color(colorspace, color, alpha); - fz_trace_matrix(ctm); - fz_trace_trm(&text->trm); - printf(">\n"); - fz_print_text(ctx, stdout, text); - printf("\n"); + fz_output *out = ((fz_trace_device*)dev)->out; + fz_printf(ctx, out, "font->name, text->wmode); + fz_trace_color(ctx, out, colorspace, color, alpha); + fz_trace_matrix(ctx, out, ctm); + fz_trace_trm(ctx, out, &text->trm); + fz_printf(ctx, out, ">\n"); + fz_trace_text(ctx, out, text); + fz_printf(ctx, out, "\n"); } static void fz_trace_clip_text(fz_context *ctx, fz_device *dev, fz_text *text, const fz_matrix *ctm, int accumulate) { - printf("font->name, text->wmode); - printf(" accumulate=\"%d\"", accumulate); - fz_trace_matrix(ctm); - fz_trace_trm(&text->trm); - printf(">\n"); - fz_print_text(ctx, stdout, text); - printf("\n"); + fz_output *out = ((fz_trace_device*)dev)->out; + fz_printf(ctx, out, "font->name, text->wmode); + fz_printf(ctx, out, " accumulate=\"%d\"", accumulate); + fz_trace_matrix(ctx, out, ctm); + fz_trace_trm(ctx, out, &text->trm); + fz_printf(ctx, out, ">\n"); + fz_trace_text(ctx, out, text); + fz_printf(ctx, out, "\n"); } static void fz_trace_clip_stroke_text(fz_context *ctx, fz_device *dev, fz_text *text, fz_stroke_state *stroke, const fz_matrix *ctm) { - printf("font->name, text->wmode); - fz_trace_matrix(ctm); - fz_trace_trm(&text->trm); - printf(">\n"); - fz_print_text(ctx, stdout, text); - printf("\n"); + fz_output *out = ((fz_trace_device*)dev)->out; + fz_printf(ctx, out, "font->name, text->wmode); + fz_trace_matrix(ctx, out, ctm); + fz_trace_trm(ctx, out, &text->trm); + fz_printf(ctx, out, ">\n"); + fz_trace_text(ctx, out, text); + fz_printf(ctx, out, "\n"); } static void fz_trace_ignore_text(fz_context *ctx, fz_device *dev, fz_text *text, const fz_matrix *ctm) { - printf("font->name, text->wmode); - fz_trace_matrix(ctm); - fz_trace_trm(&text->trm); - printf(">\n"); - fz_print_text(ctx, stdout, text); - printf("\n"); + fz_output *out = ((fz_trace_device*)dev)->out; + fz_printf(ctx, out, "font->name, text->wmode); + fz_trace_matrix(ctx, out, ctm); + fz_trace_trm(ctx, out, &text->trm); + fz_printf(ctx, out, ">\n"); + fz_trace_text(ctx, out, text); + fz_printf(ctx, out, "\n"); } static void fz_trace_fill_image(fz_context *ctx, fz_device *dev, fz_image *image, const fz_matrix *ctm, float alpha) { - printf("w, image->h); - printf("/>\n"); + fz_output *out = ((fz_trace_device*)dev)->out; + fz_printf(ctx, out, "w, image->h); + fz_printf(ctx, out, "/>\n"); } static void fz_trace_fill_shade(fz_context *ctx, fz_device *dev, fz_shade *shade, const fz_matrix *ctm, float alpha) { - printf("\n"); + fz_output *out = ((fz_trace_device*)dev)->out; + fz_printf(ctx, out, "\n"); } static void fz_trace_fill_image_mask(fz_context *ctx, fz_device *dev, fz_image *image, const fz_matrix *ctm, fz_colorspace *colorspace, float *color, float alpha) { - printf("w, image->h); - printf("/>\n"); + fz_output *out = ((fz_trace_device*)dev)->out; + fz_printf(ctx, out, "w, image->h); + fz_printf(ctx, out, "/>\n"); } static void fz_trace_clip_image_mask(fz_context *ctx, fz_device *dev, fz_image *image, const fz_rect *rect, const fz_matrix *ctm) { - printf("w, image->h); - printf("/>\n"); + fz_output *out = ((fz_trace_device*)dev)->out; + fz_printf(ctx, out, "w, image->h); + fz_printf(ctx, out, "/>\n"); } static void fz_trace_pop_clip(fz_context *ctx, fz_device *dev) { - printf("\n"); + fz_output *out = ((fz_trace_device*)dev)->out; + fz_printf(ctx, out, "\n"); } static void fz_trace_begin_mask(fz_context *ctx, fz_device *dev, const fz_rect *bbox, int luminosity, fz_colorspace *colorspace, float *color) { - printf("out; + fz_printf(ctx, out, "x0, bbox->y0, bbox->x1, bbox->y1, luminosity ? "luminosity" : "alpha"); - printf(">\n"); + fz_printf(ctx, out, ">\n"); } static void fz_trace_end_mask(fz_context *ctx, fz_device *dev) { - printf("\n"); + fz_output *out = ((fz_trace_device*)dev)->out; + fz_printf(ctx, out, "\n"); } static void fz_trace_begin_group(fz_context *ctx, fz_device *dev, const fz_rect *bbox, int isolated, int knockout, int blendmode, float alpha) { - printf("\n", + fz_output *out = ((fz_trace_device*)dev)->out; + fz_printf(ctx, out, "\n", bbox->x0, bbox->y0, bbox->x1, bbox->y1, isolated, knockout, fz_blendmode_name(blendmode), alpha); } @@ -299,59 +329,64 @@ fz_trace_begin_group(fz_context *ctx, fz_device *dev, const fz_rect *bbox, int i static void fz_trace_end_group(fz_context *ctx, fz_device *dev) { - printf("\n"); + fz_output *out = ((fz_trace_device*)dev)->out; + fz_printf(ctx, out, "\n"); } static int fz_trace_begin_tile(fz_context *ctx, fz_device *dev, const fz_rect *area, const fz_rect *view, float xstep, float ystep, const fz_matrix *ctm, int id) { - printf("x0, area->y0, area->x1, area->y1); - printf(" view=\"%g %g %g %g\"", view->x0, view->y0, view->x1, view->y1); - printf(" xstep=\"%g\" ystep=\"%g\"", xstep, ystep); - fz_trace_matrix(ctm); - printf(">\n"); + fz_output *out = ((fz_trace_device*)dev)->out; + fz_printf(ctx, out, "x0, area->y0, area->x1, area->y1); + fz_printf(ctx, out, " view=\"%g %g %g %g\"", view->x0, view->y0, view->x1, view->y1); + fz_printf(ctx, out, " xstep=\"%g\" ystep=\"%g\"", xstep, ystep); + fz_trace_matrix(ctx, out, ctm); + fz_printf(ctx, out, ">\n"); return 0; } static void fz_trace_end_tile(fz_context *ctx, fz_device *dev) { - printf("\n"); + fz_output *out = ((fz_trace_device*)dev)->out; + fz_printf(ctx, out, "\n"); } -fz_device *fz_new_trace_device(fz_context *ctx) +fz_device *fz_new_trace_device(fz_context *ctx, fz_output *out) { - fz_device *dev = fz_new_device(ctx, sizeof *dev); + fz_trace_device *dev = fz_new_device(ctx, sizeof *dev); + + dev->super.begin_page = fz_trace_begin_page; + dev->super.end_page = fz_trace_end_page; - dev->begin_page = fz_trace_begin_page; - dev->end_page = fz_trace_end_page; + dev->super.fill_path = fz_trace_fill_path; + dev->super.stroke_path = fz_trace_stroke_path; + dev->super.clip_path = fz_trace_clip_path; + dev->super.clip_stroke_path = fz_trace_clip_stroke_path; - dev->fill_path = fz_trace_fill_path; - dev->stroke_path = fz_trace_stroke_path; - dev->clip_path = fz_trace_clip_path; - dev->clip_stroke_path = fz_trace_clip_stroke_path; + dev->super.fill_text = fz_trace_fill_text; + dev->super.stroke_text = fz_trace_stroke_text; + dev->super.clip_text = fz_trace_clip_text; + dev->super.clip_stroke_text = fz_trace_clip_stroke_text; + dev->super.ignore_text = fz_trace_ignore_text; - dev->fill_text = fz_trace_fill_text; - dev->stroke_text = fz_trace_stroke_text; - dev->clip_text = fz_trace_clip_text; - dev->clip_stroke_text = fz_trace_clip_stroke_text; - dev->ignore_text = fz_trace_ignore_text; + dev->super.fill_shade = fz_trace_fill_shade; + dev->super.fill_image = fz_trace_fill_image; + dev->super.fill_image_mask = fz_trace_fill_image_mask; + dev->super.clip_image_mask = fz_trace_clip_image_mask; - dev->fill_shade = fz_trace_fill_shade; - dev->fill_image = fz_trace_fill_image; - dev->fill_image_mask = fz_trace_fill_image_mask; - dev->clip_image_mask = fz_trace_clip_image_mask; + dev->super.pop_clip = fz_trace_pop_clip; - dev->pop_clip = fz_trace_pop_clip; + dev->super.begin_mask = fz_trace_begin_mask; + dev->super.end_mask = fz_trace_end_mask; + dev->super.begin_group = fz_trace_begin_group; + dev->super.end_group = fz_trace_end_group; - dev->begin_mask = fz_trace_begin_mask; - dev->end_mask = fz_trace_end_mask; - dev->begin_group = fz_trace_begin_group; - dev->end_group = fz_trace_end_group; + dev->super.begin_tile = fz_trace_begin_tile; + dev->super.end_tile = fz_trace_end_tile; - dev->begin_tile = fz_trace_begin_tile; - dev->end_tile = fz_trace_end_tile; + dev->out = out; - return dev; + return (fz_device*)dev; } diff --git a/source/fitz/unzip.c b/source/fitz/unzip.c index b14705ba..21cfa7a5 100644 --- a/source/fitz/unzip.c +++ b/source/fitz/unzip.c @@ -364,14 +364,10 @@ fz_has_archive_entry(fz_context *ctx, fz_archive *zip, const char *name) if (zip->directory) { char path[2048]; - FILE *file; fz_strlcpy(path, zip->directory, sizeof path); fz_strlcat(path, "/", sizeof path); fz_strlcat(path, name, sizeof path); - file = fz_fopen(path, "rb"); - if (file) - fclose(file); - return file != NULL; + return fz_file_exists(ctx, path); } else { diff --git a/source/gprf/gprf-skeleton.c b/source/gprf/gprf-skeleton.c index 7d46348c..6ed8a29d 100644 --- a/source/gprf/gprf-skeleton.c +++ b/source/gprf/gprf-skeleton.c @@ -10,11 +10,11 @@ fz_write_gproof_file(fz_context *ctx, const char *pdf_file, fz_document *doc, co fz_page *page = NULL; fz_var(page); - + if (num_pages <= 0) fz_throw(ctx, FZ_ERROR_GENERIC, "Cannot write a 0 page GProof skeleton file"); - out = fz_new_output_to_filename(ctx, filename); + out = fz_new_output_with_path(ctx, filename, 0); fz_try(ctx) { 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); diff --git a/source/tools/mudraw.c b/source/tools/mudraw.c index 4614d4bc..705cd6eb 100644 --- a/source/tools/mudraw.c +++ b/source/tools/mudraw.c @@ -310,7 +310,7 @@ static void drawpage(fz_context *ctx, fz_document *doc, int pagenum) { fz_try(ctx) { - dev = fz_new_trace_device(ctx); + dev = fz_new_trace_device(ctx, out); if (list) fz_run_display_list(ctx, list, dev, &fz_identity, &fz_infinite_rect, &cookie); else @@ -420,21 +420,16 @@ static void drawpage(fz_context *ctx, fz_document *doc, int pagenum) fz_matrix ctm; fz_rect bounds, tbounds; char buf[512]; - FILE *file; fz_output *out; if (!strcmp(output, "-")) - file = stdout; + out = fz_new_output_with_file_ptr(ctx, stdout, 0); else { sprintf(buf, output, pagenum); - file = fz_fopen(buf, "wb"); - if (file == NULL) - fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file '%s': %s", buf, strerror(errno)); + out = fz_new_output_with_path(ctx, buf, 0); } - out = fz_new_output_with_file(ctx, file, 0); - fz_bound_page(ctx, page, &bounds); zoom = resolution / 72; fz_pre_rotate(fz_scale(&ctm, zoom, zoom), rotation); @@ -456,8 +451,6 @@ static void drawpage(fz_context *ctx, fz_document *doc, int pagenum) fz_drop_device(ctx, dev); dev = NULL; fz_drop_output(ctx, out); - if (file != stdout) - fclose(file); } fz_catch(ctx) { @@ -567,11 +560,11 @@ static void drawpage(fz_context *ctx, fz_document *doc, int pagenum) if (output) { if (!strcmp(output, "-")) - output_file = fz_new_output_with_file(ctx, stdout, 0); + output_file = fz_new_output_with_file_ptr(ctx, stdout, 0); else { sprintf(filename_buf, output, pagenum); - output_file = fz_new_output_to_filename(ctx, filename_buf); + output_file = fz_new_output_with_path(ctx, filename_buf, 0); } if (output_format == OUT_PGM || output_format == OUT_PPM || output_format == OUT_PNM) @@ -860,7 +853,6 @@ int mudraw_main(int argc, char **argv) int c; fz_context *ctx; fz_alloc_context alloc_ctx = { NULL, trace_malloc, trace_realloc, trace_free }; - FILE *my_output = NULL; fz_var(doc); @@ -1045,17 +1037,9 @@ int mudraw_main(int argc, char **argv) if (output_format == OUT_TEXT || output_format == OUT_HTML || output_format == OUT_STEXT || output_format == OUT_TRACE) { if (output && output[0] != '-' && *output != 0) - { - my_output = fopen(output, "w"); - if (my_output == NULL) - { - fprintf(stderr, "Failed to open file '%s' for output\n", output); - exit(1); - } - } + out = fz_new_output_with_path(ctx, output, 0); else - my_output = stdout; - out = fz_new_output_with_file(ctx, my_output, 0); + out = fz_new_output_with_file_ptr(ctx, stdout, 0); } if (output_format == OUT_STEXT || output_format == OUT_TRACE) @@ -1164,9 +1148,6 @@ int mudraw_main(int argc, char **argv) fz_drop_output(ctx, out); out = NULL; - if (my_output != NULL && my_output != stdout) - fclose(my_output); - if (showtime && timing.count > 0) { if (files == 1) diff --git a/source/tools/pdfextract.c b/source/tools/pdfextract.c index a558f264..0c89931a 100644 --- a/source/tools/pdfextract.c +++ b/source/tools/pdfextract.c @@ -89,9 +89,9 @@ static void savefont(pdf_obj *dict, int num) pdf_obj *stream = NULL; pdf_obj *obj; char *ext = ""; - FILE *f; + fz_output *out; char *fontname = "font"; - int n, len; + int len; unsigned char *data; obj = pdf_dict_get(ctx, dict, PDF_NAME_FontName); @@ -138,23 +138,23 @@ static void savefont(pdf_obj *dict, int num) } buf = pdf_load_stream(ctx, doc, pdf_to_num(ctx, stream), pdf_to_gen(ctx, stream)); - - snprintf(namebuf, sizeof(namebuf), "%s-%04d.%s", fontname, num, ext); - printf("extracting font %s\n", namebuf); - - f = fz_fopen(namebuf, "wb"); - if (!f) - fz_throw(ctx, FZ_ERROR_GENERIC, "cannot create font file"); - len = fz_buffer_storage(ctx, buf, &data); - n = fwrite(data, 1, len, f); - if (n < len) - fz_throw(ctx, FZ_ERROR_GENERIC, "cannot write font file"); - - if (fclose(f) < 0) - fz_throw(ctx, FZ_ERROR_GENERIC, "cannot close font file"); - - fz_drop_buffer(ctx, buf); + fz_try(ctx) + { + snprintf(namebuf, sizeof(namebuf), "%s-%04d.%s", fontname, num, ext); + printf("extracting font %s\n", namebuf); + out = fz_new_output_with_path(ctx, namebuf, 0); + fz_try(ctx) + fz_write(ctx, out, data, len); + fz_always(ctx) + fz_drop_output(ctx, out); + fz_catch(ctx) + fz_rethrow(ctx); + } + fz_always(ctx) + fz_drop_buffer(ctx, buf); + fz_catch(ctx) + fz_rethrow(ctx); } static void showobject(int num) diff --git a/source/tools/pdfinfo.c b/source/tools/pdfinfo.c index 4f370f16..6133e7f5 100644 --- a/source/tools/pdfinfo.c +++ b/source/tools/pdfinfo.c @@ -184,14 +184,14 @@ showglobalinfo(fz_context *ctx, globals *glo) if (obj) { fz_printf(ctx, out, "Info object (%d %d R):\n", pdf_to_num(ctx, obj), pdf_to_gen(ctx, obj)); - pdf_output_obj(ctx, out, pdf_resolve_indirect(ctx, obj), 1); + pdf_print_obj(ctx, out, pdf_resolve_indirect(ctx, obj), 1); } obj = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME_Encrypt); if (obj) { fz_printf(ctx, out, "\nEncryption object (%d %d R):\n", pdf_to_num(ctx, obj), pdf_to_gen(ctx, obj)); - pdf_output_obj(ctx, out, pdf_resolve_indirect(ctx, obj), 1); + pdf_print_obj(ctx, out, pdf_resolve_indirect(ctx, obj), 1); } fz_printf(ctx, out, "\nPages: %d\n\n", glo->pagecount); @@ -1073,7 +1073,7 @@ int pdfinfo_main(int argc, char **argv) ret = 0; fz_try(ctx) { - out = fz_new_output_with_file(ctx, stdout, 0); + out = fz_new_output_with_file_ptr(ctx, stdout, 0); pdfinfo_info(ctx, out, filename, password, show, &argv[fz_optind], argc-fz_optind); } fz_catch(ctx) diff --git a/source/tools/pdfpages.c b/source/tools/pdfpages.c index a0f04e7d..9f688ac5 100644 --- a/source/tools/pdfpages.c +++ b/source/tools/pdfpages.c @@ -241,7 +241,7 @@ int pdfpages_main(int argc, char **argv) ret = 0; fz_try(ctx) { - out = fz_new_output_with_file(ctx, stdout, 0); + out = fz_new_output_with_file_ptr(ctx, stdout, 0); ret = pdfpages_pages(ctx, out, filename, password, &argv[fz_optind], argc-fz_optind); } fz_catch(ctx) diff --git a/source/tools/pdfshow.c b/source/tools/pdfshow.c index 04a2c859..1a3d1fb7 100644 --- a/source/tools/pdfshow.c +++ b/source/tools/pdfshow.c @@ -4,10 +4,9 @@ #include "mupdf/pdf.h" -static FILE *out = NULL; - static pdf_document *doc = NULL; static fz_context *ctx = NULL; +static fz_output *out = NULL; static int showbinary = 0; static int showdecode = 1; static int showcolumn; @@ -26,9 +25,9 @@ static void showtrailer(void) { if (!doc) fz_throw(ctx, FZ_ERROR_GENERIC, "no file specified"); - fprintf(out, "trailer\n"); - pdf_fprint_obj(ctx, out, pdf_trailer(ctx, doc), 0); - fprintf(out, "\n"); + fz_printf(ctx, out, "trailer\n"); + pdf_print_obj(ctx, out, pdf_trailer(ctx, doc), 0); + fz_printf(ctx, out, "\n"); } static void showencrypt(void) @@ -40,9 +39,9 @@ static void showencrypt(void) encrypt = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME_Encrypt); if (!encrypt) fz_throw(ctx, FZ_ERROR_GENERIC, "document not encrypted"); - fprintf(out, "encryption dictionary\n"); - pdf_fprint_obj(ctx, out, pdf_resolve_indirect(ctx, encrypt), 0); - fprintf(out, "\n"); + fz_printf(ctx, out, "encryption dictionary\n"); + pdf_print_obj(ctx, out, pdf_resolve_indirect(ctx, encrypt), 0); + fz_printf(ctx, out, "\n"); } static void showxref(void) @@ -50,7 +49,7 @@ static void showxref(void) if (!doc) fz_throw(ctx, FZ_ERROR_GENERIC, "no file specified"); pdf_print_xref(ctx, doc); - fprintf(out, "\n"); + fz_printf(ctx, out, "\n"); } static void showpagetree(void) @@ -66,9 +65,9 @@ static void showpagetree(void) for (i = 0; i < count; i++) { ref = pdf_lookup_page_obj(ctx, doc, i); - fprintf(out, "page %d = %d %d R\n", i + 1, pdf_to_num(ctx, ref), pdf_to_gen(ctx, ref)); + fz_printf(ctx, out, "page %d = %d %d R\n", i + 1, pdf_to_num(ctx, ref), pdf_to_gen(ctx, ref)); } - fprintf(out, "\n"); + fz_printf(ctx, out, "\n"); } static void showsafe(unsigned char *buf, int n) @@ -113,7 +112,7 @@ static void showstream(int num, int gen) if (n == 0) break; if (showbinary) - fwrite(buf, 1, n, out); + fz_write(ctx, out, buf, n); else showsafe(buf, n); } @@ -138,19 +137,19 @@ static void showobject(int num, int gen) } else { - fprintf(out, "%d %d obj\n", num, gen); - pdf_fprint_obj(ctx, out, obj, 0); - fprintf(out, "stream\n"); + fz_printf(ctx, out, "%d %d obj\n", num, gen); + pdf_print_obj(ctx, out, obj, 0); + fz_printf(ctx, out, "stream\n"); showstream(num, gen); - fprintf(out, "endstream\n"); - fprintf(out, "endobj\n\n"); + fz_printf(ctx, out, "endstream\n"); + fz_printf(ctx, out, "endobj\n\n"); } } else { - fprintf(out, "%d %d obj\n", num, gen); - pdf_fprint_obj(ctx, out, obj, 0); - fprintf(out, "endobj\n\n"); + fz_printf(ctx, out, "%d %d obj\n", num, gen); + pdf_print_obj(ctx, out, obj, 0); + fz_printf(ctx, out, "endobj\n\n"); } pdf_drop_obj(ctx, obj); @@ -179,15 +178,15 @@ static void showgrep(char *filename) pdf_sort_dict(ctx, obj); - fprintf(out, "%s:%d: ", filename, i); - pdf_fprint_obj(ctx, out, obj, 1); + fz_printf(ctx, out, "%s:%d: ", filename, i); + pdf_print_obj(ctx, out, obj, 1); pdf_drop_obj(ctx, obj); } } - fprintf(out, "%s:trailer: ", filename); - pdf_fprint_obj(ctx, out, pdf_trailer(ctx, doc), 1); + fz_printf(ctx, out, "%s:trailer: ", filename); + pdf_print_obj(ctx, out, pdf_trailer(ctx, doc), 1); } static void showoutline(void) @@ -198,7 +197,7 @@ static void showoutline(void) fz_var(out); fz_try(ctx) { - out = fz_new_output_with_file(ctx, stdout, 0); + out = fz_new_output_with_file_ptr(ctx, stdout, 0); fz_print_outline(ctx, out, outline); } fz_always(ctx) @@ -219,6 +218,13 @@ int pdfshow_main(int argc, char **argv) char *output = NULL; int c; + ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); + if (!ctx) + { + fprintf(stderr, "cannot initialise context\n"); + exit(1); + } + while ((c = fz_getopt(argc, argv, "p:o:be")) != -1) { switch (c) @@ -236,23 +242,10 @@ int pdfshow_main(int argc, char **argv) filename = argv[fz_optind++]; - out = stdout; if (output) - { - out = fz_fopen(output, "wb"); - if (!out) - { - fprintf(stderr, "cannot open output file: '%s'\n", output); - exit(1); - } - } - - ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); - if (!ctx) - { - fprintf(stderr, "cannot initialise context\n"); - exit(1); - } + out = fz_new_output_with_path(ctx, output, 0); + else + out = fz_new_output_with_file_ptr(ctx, stdout, 0); fz_var(doc); fz_try(ctx) @@ -284,9 +277,7 @@ int pdfshow_main(int argc, char **argv) { } - if (out != stdout) - fclose(out); - + fz_drop_output(ctx, out); pdf_close_document(ctx, doc); fz_drop_context(ctx); return 0; -- cgit v1.2.3