static void cbz_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev) { fz_cbz_writer *wri = (fz_cbz_writer*)wri_; fz_buffer *buffer; char name[40]; fz_try(ctx) fz_close_device(ctx, dev); fz_always(ctx) fz_drop_device(ctx, dev); fz_catch(ctx) fz_rethrow(ctx); wri->count += 1; fz_snprintf(name, sizeof name, "p%04d.png", wri->count); buffer = fz_new_buffer_from_pixmap_as_png(ctx, wri->pixmap, NULL); fz_try(ctx) fz_write_zip_entry(ctx, wri->zip, name, buffer, 0); fz_always(ctx) fz_drop_buffer(ctx, buffer); fz_catch(ctx) fz_rethrow(ctx); fz_drop_pixmap(ctx, wri->pixmap); wri->pixmap = NULL; }
static void fz_test_fill_image(fz_context *ctx, fz_device *dev_, fz_image *image, fz_matrix ctm, float alpha, const fz_color_params *color_params) { fz_test_device *dev = (fz_test_device*)dev_; while (dev->resolved == 0) /* So we can break out */ { fz_compressed_buffer *buffer; if (*dev->is_color || !image->colorspace || fz_colorspace_is_gray(ctx, image->colorspace)) break; if ((dev->options & FZ_TEST_OPT_IMAGES) == 0) { /* Don't test every pixel. Upgrade us from "black and white" to "probably color" */ if (*dev->is_color == 0) *dev->is_color = 1; dev->resolved = 1; if (dev->passthrough == NULL) fz_throw(ctx, FZ_ERROR_ABORT, "Page found as color; stopping interpretation"); break; } buffer = fz_compressed_image_buffer(ctx, image); if (buffer && image->bpc == 8) { fz_stream *stream = fz_open_compressed_buffer(ctx, buffer); fz_try(ctx) fz_test_fill_compressed_8bpc_image(ctx, dev, image, stream, color_params); fz_always(ctx) fz_drop_stream(ctx, stream); fz_catch(ctx) fz_rethrow(ctx); } else { fz_pixmap *pix = fz_get_pixmap_from_image(ctx, image, NULL, NULL, 0, 0); if (pix == NULL) /* Should never happen really, but... */ break; fz_try(ctx) fz_test_fill_other_image(ctx, dev, pix, color_params); fz_always(ctx) fz_drop_pixmap(ctx, pix); fz_catch(ctx) fz_rethrow(ctx); } break; } if (dev->passthrough) fz_fill_image(ctx, dev->passthrough, image, ctm, alpha, color_params); }
static void reload(void) { fz_drop_outline(ctx, outline); fz_drop_document(ctx, doc); doc = fz_open_document(ctx, filename); if (fz_needs_password(ctx, doc)) { if (!fz_authenticate_password(ctx, doc, password)) { fprintf(stderr, "Invalid password.\n"); exit(1); } } fz_layout_document(ctx, doc, layout_w, layout_h, layout_em); fz_try(ctx) outline = fz_load_outline(ctx, doc); fz_catch(ctx) outline = NULL; pdf = pdf_specifics(ctx, doc); if (pdf) pdf_enable_js(ctx, pdf); currentpage = fz_clampi(currentpage, 0, fz_count_pages(ctx, doc) - 1); render_page(); update_title(); }
void fz_vprintf(fz_context *ctx, fz_output *out, const char *fmt, va_list old_args) { 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); va_copy(args, old_args); fz_vsnprintf(p, len, 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); }
xps_document * xps_open_document(fz_context *ctx, const char *filename) { char buf[2048]; fz_stream *file; char *p; xps_document *doc; if (strstr(filename, "/_rels/.rels") || strstr(filename, "\\_rels\\.rels")) { fz_strlcpy(buf, filename, sizeof buf); p = strstr(buf, "/_rels/.rels"); if (!p) p = strstr(buf, "\\_rels\\.rels"); *p = 0; return xps_open_document_with_directory(ctx, buf); } file = fz_open_file(ctx, filename); fz_try(ctx) doc = xps_open_document_with_stream(ctx, file); fz_always(ctx) fz_drop_stream(ctx, file); fz_catch(ctx) fz_rethrow_message(ctx, "cannot load document '%s'", filename); return doc; }
static void pwg_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev) { fz_pwg_writer *wri = (fz_pwg_writer*)wri_; fz_close_device(ctx, dev); fz_drop_device(ctx, dev); if (wri->mono) { fz_bitmap *bitmap = fz_new_bitmap_from_pixmap(ctx, wri->pixmap, NULL); fz_try(ctx) fz_write_bitmap_as_pwg_page(ctx, wri->out, bitmap, &wri->pwg); fz_always(ctx) fz_drop_bitmap(ctx, bitmap); fz_catch(ctx) fz_rethrow(ctx); } else { fz_write_pixmap_as_pwg_page(ctx, wri->out, wri->pixmap, &wri->pwg); } fz_drop_pixmap(ctx, wri->pixmap); wri->pixmap = NULL; }
void fz_save_pixmap_as_ps(fz_context *ctx, fz_pixmap *pixmap, char *filename, int append) { fz_output *out = fz_new_output_with_path(ctx, filename, append); fz_try(ctx) fz_write_pixmap_as_ps(ctx, out, pixmap); fz_always(ctx) fz_drop_output(ctx, out); fz_catch(ctx) fz_rethrow(ctx); }
void pdf_array_put_drop(fz_context *ctx, pdf_obj *obj, int i, pdf_obj *item) { fz_try(ctx) pdf_array_put(ctx, obj, i, item); fz_always(ctx) pdf_drop_obj(ctx, item); fz_catch(ctx) fz_rethrow(ctx); }
void fz_save_buffer(fz_context *ctx, fz_buffer *buf, const char *filename) { fz_output *out = fz_new_output_with_path(ctx, filename, 0); fz_try(ctx) fz_write(ctx, out, buf->data, buf->len); fz_always(ctx) fz_drop_output(ctx, out); fz_catch(ctx) fz_rethrow(ctx); }
void pdf_array_insert_drop(fz_context *ctx, pdf_obj *obj, pdf_obj *item, int i) { RESOLVE(obj); if (obj) { fz_try(ctx) pdf_array_insert(ctx, obj, item, i); fz_always(ctx) pdf_drop_obj(ctx, item); fz_catch(ctx) fz_rethrow(ctx); } }
void pdf_array_push_drop(fz_context *ctx, pdf_obj *obj, pdf_obj *item) { RESOLVE(obj); if (obj >= PDF_OBJ__LIMIT) { fz_try(ctx) pdf_array_push(ctx, obj, item); fz_always(ctx) pdf_drop_obj(ctx, item); fz_catch(ctx) fz_rethrow(ctx); } }
fz_buffer * fz_new_buffer_from_page_number(fz_context *ctx, fz_document *doc, int number, const fz_rect *sel, int crlf) { fz_page *page; fz_buffer *buf; page = fz_load_page(ctx, doc, number); fz_try(ctx) buf = fz_new_buffer_from_page(ctx, page, sel, crlf); fz_always(ctx) fz_drop_page(ctx, page); fz_catch(ctx) fz_rethrow(ctx); return buf; }
int fz_search_page_number(fz_context *ctx, fz_document *doc, int number, const char *needle, fz_rect *hit_bbox, int hit_max) { fz_page *page; int count; page = fz_load_page(ctx, doc, number); fz_try(ctx) count = fz_search_page(ctx, page, needle, hit_bbox, hit_max); fz_always(ctx) fz_drop_page(ctx, page); fz_catch(ctx) fz_rethrow(ctx); return count; }
void fz_drop_output_context(fz_context *ctx) { if (!ctx) return; if (fz_drop_imp(ctx, ctx->output, &ctx->output->refs)) { fz_try(ctx) fz_flush_output(ctx, ctx->output->out); fz_catch(ctx) fz_warn(ctx, "cannot flush stdout"); fz_drop_output(ctx, ctx->output->out); fz_try(ctx) fz_flush_output(ctx, ctx->output->err); fz_catch(ctx) fz_warn(ctx, "cannot flush stderr"); fz_drop_output(ctx, ctx->output->err); fz_free(ctx, ctx->output); ctx->output = NULL; } }
fz_pixmap * fz_new_pixmap_from_page_number(fz_context *ctx, fz_document *doc, int number, const fz_matrix *ctm, fz_colorspace *cs, int alpha) { fz_page *page; fz_pixmap *pix; page = fz_load_page(ctx, doc, number); fz_try(ctx) pix = fz_new_pixmap_from_page(ctx, page, ctm, cs, alpha); fz_always(ctx) fz_drop_page(ctx, page); fz_catch(ctx) fz_rethrow(ctx); return pix; }
fz_stext_page * fz_new_stext_page_from_page_number(fz_context *ctx, fz_document *doc, int number, fz_stext_sheet *sheet) { fz_page *page; fz_stext_page *text; page = fz_load_page(ctx, doc, number); fz_try(ctx) text = fz_new_stext_page_from_page(ctx, page, sheet); fz_always(ctx) fz_drop_page(ctx, page); fz_catch(ctx) fz_rethrow(ctx); return text; }
fz_display_list * fz_new_display_list_from_page_number(fz_context *ctx, fz_document *doc, int number) { fz_page *page; fz_display_list *list; page = fz_load_page(ctx, doc, number); fz_try(ctx) list = fz_new_display_list_from_page(ctx, page); fz_always(ctx) fz_drop_page(ctx, page); fz_catch(ctx) fz_rethrow(ctx); return list; }
static fz_document * svg_open_document(fz_context *ctx, const char *filename) { fz_stream *file; fz_document *doc; file = fz_open_file(ctx, filename); fz_try(ctx) doc = svg_open_document_with_stream(ctx, file); fz_always(ctx) fz_drop_stream(ctx, file); fz_catch(ctx) fz_rethrow(ctx); return doc; }
fz_image * fz_new_image_from_svg(fz_context *ctx, fz_buffer *buf) { fz_display_list *list; fz_image *image; float w, h; list = fz_new_display_list_from_svg(ctx, buf, &w, &h); fz_try(ctx) image = fz_new_image_from_display_list(ctx, w, h, list); fz_always(ctx) fz_drop_display_list(ctx, list); fz_catch(ctx) fz_rethrow(ctx); return image; }
static fz_document * svg_open_document_with_stream(fz_context *ctx, fz_stream *file) { fz_buffer *buf; fz_document *doc; buf = fz_read_all(ctx, file, 0); fz_try(ctx) doc = svg_open_document_with_buffer(ctx, buf); fz_always(ctx) fz_drop_buffer(ctx, buf); fz_catch(ctx) fz_rethrow(ctx); return doc; }
fz_archive * fz_open_archive(fz_context *ctx, const char *filename) { fz_stream *file; fz_archive *arch = NULL; file = fz_open_file(ctx, filename); fz_try(ctx) arch = fz_open_archive_with_stream(ctx, file); fz_always(ctx) fz_drop_stream(ctx, file); fz_catch(ctx) fz_rethrow(ctx); return arch; }
static img_document * img_open_document(fz_context *ctx, const char *filename) { fz_stream *stm; img_document *doc; stm = fz_open_file(ctx, filename); fz_try(ctx) doc = img_open_document_with_stream(ctx, stm); fz_always(ctx) fz_drop_stream(ctx, stm); fz_catch(ctx) fz_rethrow(ctx); return doc; }
static fz_link * pdf_load_link(fz_context *ctx, pdf_document *doc, pdf_obj *dict, int pagenum, const fz_matrix *page_ctm) { pdf_obj *action; pdf_obj *obj; fz_rect bbox; char *uri; fz_link *link = NULL; obj = pdf_dict_get(ctx, dict, PDF_NAME_Subtype); if (!pdf_name_eq(ctx, obj, PDF_NAME_Link)) return NULL; obj = pdf_dict_get(ctx, dict, PDF_NAME_Rect); if (!obj) return NULL; pdf_to_rect(ctx, obj, &bbox); fz_transform_rect(&bbox, page_ctm); obj = pdf_dict_get(ctx, dict, PDF_NAME_Dest); if (obj) uri = pdf_parse_link_dest(ctx, doc, obj); else { action = pdf_dict_get(ctx, dict, PDF_NAME_A); /* fall back to additional action button's down/up action */ if (!action) action = pdf_dict_geta(ctx, pdf_dict_get(ctx, dict, PDF_NAME_AA), PDF_NAME_U, PDF_NAME_D); uri = pdf_parse_link_action(ctx, doc, action, pagenum); } if (!uri) return NULL; fz_try(ctx) link = fz_new_link(ctx, &bbox, doc, uri); fz_always(ctx) fz_free(ctx, uri); fz_catch(ctx) fz_rethrow(ctx); return link; }
fz_colorspace * pdf_xobject_colorspace(fz_context *ctx, pdf_obj *xobj) { pdf_obj *group = pdf_dict_get(ctx, xobj, PDF_NAME(Group)); if (group) { pdf_obj *cs = pdf_dict_get(ctx, group, PDF_NAME(CS)); if (cs) { fz_colorspace *colorspace = NULL; fz_try(ctx) colorspace = pdf_load_colorspace(ctx, cs); fz_catch(ctx) fz_warn(ctx, "cannot load xobject colorspace"); return colorspace; } } return NULL; }
static void pdf_write_widget_appearance(fz_context *ctx, pdf_annot *annot, fz_buffer *buf, fz_rect *rect, fz_rect *bbox, fz_matrix *matrix, pdf_obj **res) { pdf_obj *ft = pdf_dict_get_inheritable(ctx, annot->obj, PDF_NAME(FT)); if (pdf_name_eq(ctx, ft, PDF_NAME(Tx))) { int ff = pdf_field_flags(ctx, annot->obj); char *format = NULL; const char *text = NULL; if (!annot->ignore_trigger_events) { format = pdf_field_event_format(ctx, annot->page->doc, annot->obj); if (format) text = format; else text = pdf_field_value(ctx, annot->obj); } else { text = pdf_field_value(ctx, annot->obj); } fz_try(ctx) pdf_write_tx_widget_appearance(ctx, annot, buf, rect, bbox, matrix, res, text, ff); fz_always(ctx) fz_free(ctx, format); fz_catch(ctx) fz_rethrow(ctx); } else if (pdf_name_eq(ctx, ft, PDF_NAME(Ch))) { pdf_write_ch_widget_appearance(ctx, annot, buf, rect, bbox, matrix, res); } else if (pdf_name_eq(ctx, ft, PDF_NAME(Sig))) { pdf_write_sig_widget_appearance(ctx, annot, buf, rect, bbox, matrix, res); } else { fz_throw(ctx, FZ_ERROR_GENERIC, "cannot create appearance stream for %s widgets", pdf_to_name(ctx, ft)); } }
static void pixmap_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev) { fz_pixmap_writer *wri = (fz_pixmap_writer*)wri_; char path[PATH_MAX]; fz_try(ctx) fz_close_device(ctx, dev); fz_always(ctx) fz_drop_device(ctx, dev); fz_catch(ctx) fz_rethrow(ctx); wri->count += 1; fz_format_output_path(ctx, path, sizeof path, wri->path, wri->count); wri->save(ctx, wri->pixmap, path); fz_drop_pixmap(ctx, wri->pixmap); wri->pixmap = NULL; }
static void close_dctd(fz_context *ctx, void *state_) { fz_dctd *state = (fz_dctd *)state_; if (state->init) { /* We call jpeg_abort rather than the more usual * jpeg_finish_decompress here. This has the same effect, * but doesn't spew warnings if we didn't read enough data etc. * Annoyingly jpeg_abort can throw */ fz_try(ctx) jpeg_abort((j_common_ptr)&state->cinfo); fz_catch(ctx) { /* Ignore any errors here */ } jpeg_destroy_decompress(&state->cinfo); }
/** * Perform MuPDF native operations on a given MuOfficeLib * instance. * * The function is called with a fz_context value that can * be safely used (i.e. the context is cloned/dropped * appropriately around the call). The function should signal * errors by fz_throw-ing. * * @param mu the MuOfficeLib instance. * @param fn the function to call to run the operations. * @param arg Opaque data pointer. * * @return error indication - 0 for success */ MuError MuOfficeLib_run(MuOfficeLib *mu, void (*fn)(fz_context *ctx, void *arg), void *arg) { fz_context *ctx; MuError err = MuError_OK; if (mu == NULL) return MuError_BadNull; if (fn == NULL) return err; ctx = fz_clone_context(mu->ctx); if (ctx == NULL) return MuError_OOM; fz_try(ctx) fn(ctx, arg); fz_catch(ctx) err = MuError_Generic; fz_drop_context(ctx); return err; }
MuError MuOfficeLib_create(MuOfficeLib **pMu) { MuOfficeLib *mu; fz_locks_context *locks; if (pMu == NULL) return MuOfficeDocErrorType_IllegalArgument; mu = Pal_Mem_calloc(1, sizeof(MuOfficeLib)); if (mu == NULL) return MuOfficeDocErrorType_OutOfMemory; locks = init_muoffice_locks(mu); if (locks == NULL) goto Fail; mu->ctx = fz_new_context(&muoffice_alloc, locks, FZ_STORE_DEFAULT); if (mu->ctx == NULL) goto Fail; fz_try(mu->ctx) fz_register_document_handlers(mu->ctx); fz_catch(mu->ctx) goto Fail; *pMu = mu; return MuOfficeDocErrorType_NoError; Fail: if (mu) { fin_muoffice_locks(mu); Pal_Mem_free(mu); } return MuOfficeDocErrorType_OutOfMemory; }
int pdfpages_main(int argc, char **argv) { char *filename = ""; char *password = ""; int c; int ret; fz_context *ctx; while ((c = fz_getopt(argc, argv, "p:")) != -1) { switch (c) { case 'p': password = fz_optarg; break; default: infousage(); break; } } if (fz_optind == argc) infousage(); ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "cannot initialise context\n"); exit(1); } ret = 0; fz_try(ctx) ret = pdfpages_pages(ctx, fz_stdout(ctx), filename, password, &argv[fz_optind], argc-fz_optind); fz_catch(ctx) ret = 1; fz_drop_context(ctx); return ret; }