diff options
Diffstat (limited to 'apps')
-rw-r--r-- | apps/pdfapp.c | 74 | ||||
-rw-r--r-- | apps/pdfclean.c | 72 | ||||
-rw-r--r-- | apps/pdfdraw.c | 47 | ||||
-rw-r--r-- | apps/pdfextract.c | 52 | ||||
-rw-r--r-- | apps/pdfshow.c | 66 | ||||
-rw-r--r-- | apps/win_main.c | 20 | ||||
-rw-r--r-- | apps/xpsdraw.c | 40 |
7 files changed, 240 insertions, 131 deletions
diff --git a/apps/pdfapp.c b/apps/pdfapp.c index c992b504..5e235ddf 100644 --- a/apps/pdfapp.c +++ b/apps/pdfapp.c @@ -100,20 +100,25 @@ void pdfapp_invert(pdfapp_t *app, fz_bbox rect) static void pdfapp_open_pdf(pdfapp_t *app, char *filename, int fd) { - fz_error error; fz_stream *file; char *password = ""; fz_obj *obj; fz_obj *info; + fz_context *ctx = app->ctx; /* * Open PDF and load xref table */ - file = fz_open_fd(app->ctx, fd); - error = pdf_open_xref_with_stream(&app->xref, file, NULL); - if (error) - pdfapp_error(app, fz_error_note(error, "cannot open document '%s'", filename)); + file = fz_open_fd(ctx, fd); + fz_try(ctx) + { + app->xref = pdf_open_xref_with_stream(file, NULL); + } + fz_catch(ctx) + { + pdfapp_error(app, fz_error_note(1, "cannot open document '%s'", filename)); + } fz_close(file); /* @@ -150,29 +155,38 @@ static void pdfapp_open_pdf(pdfapp_t *app, char *filename, int fd) { obj = fz_dict_gets(info, "Title"); if (obj) - app->doctitle = pdf_to_utf8(app->ctx, obj); + app->doctitle = pdf_to_utf8(ctx, obj); } /* * Start at first page */ - error = pdf_load_page_tree(app->xref); - if (error) - pdfapp_error(app, fz_error_note(error, "cannot load page tree")); + fz_try(ctx) + { + pdf_load_page_tree(app->xref); + } + fz_catch(ctx) + { + pdfapp_error(app, fz_error_note(1, "cannot load page tree")); + } app->pagecount = pdf_count_pages(app->xref); } static void pdfapp_open_xps(pdfapp_t *app, char *filename, int fd) { - fz_error error; fz_stream *file; file = fz_open_fd(app->ctx, fd); - error = xps_open_stream(&app->xps, file); - if (error) - pdfapp_error(app, fz_error_note(error, "cannot open document '%s'", filename)); + fz_try(app->ctx) + { + app->xps = xps_open_stream(file); + } + fz_catch(app->ctx) + { + pdfapp_error(app, fz_error_note(-1, "cannot open document '%s'", filename)); + } fz_close(file); app->doctitle = filename; @@ -282,12 +296,16 @@ static void pdfapp_panview(pdfapp_t *app, int newx, int newy) static void pdfapp_loadpage_pdf(pdfapp_t *app) { pdf_page *page; - fz_error error; fz_device *mdev; - error = pdf_load_page(&page, app->xref, app->pageno - 1); - if (error) - pdfapp_error(app, error); + fz_try(app->ctx) + { + page = pdf_load_page(app->xref, app->pageno - 1); + } + fz_catch(app->ctx) + { + pdfapp_error(app, 1); + } app->page_bbox = page->mediabox; app->page_rotate = page->rotate; @@ -297,11 +315,13 @@ static void pdfapp_loadpage_pdf(pdfapp_t *app) /* Create display list */ app->page_list = fz_new_display_list(app->ctx); mdev = fz_new_list_device(app->ctx, app->page_list); - error = pdf_run_page(app->xref, page, mdev, fz_identity); - if (error) + fz_try(app->ctx) { - error = fz_error_note(error, "cannot draw page %d in '%s'", app->pageno, app->doctitle); - pdfapp_error(app, error); + pdf_run_page(app->xref, page, mdev, fz_identity); + } + fz_catch(app->ctx) + { + pdfapp_error(app, fz_error_note(-1, "cannot draw page %d in '%s'", app->pageno, app->doctitle)); } fz_free_device(mdev); @@ -314,11 +334,15 @@ static void pdfapp_loadpage_xps(pdfapp_t *app) { xps_page *page; fz_device *mdev; - fz_error error; - error = xps_load_page(&page, app->xps, app->pageno - 1); - if (error) - pdfapp_error(app, fz_error_note(error, "cannot load page %d in file '%s'", app->pageno, app->doctitle)); + fz_try(app->ctx) + { + page = xps_load_page(app->xps, app->pageno - 1); + } + fz_catch(app->ctx) + { + pdfapp_error(app, fz_error_note(1, "cannot load page %d in file '%s'", app->pageno, app->doctitle)); + } app->page_bbox.x0 = 0; app->page_bbox.y0 = 0; diff --git a/apps/pdfclean.c b/apps/pdfclean.c index 4b5fa139..4b76afa2 100644 --- a/apps/pdfclean.c +++ b/apps/pdfclean.c @@ -287,13 +287,17 @@ static void renumberobjs(void) static void retainpages(int argc, char **argv) { - fz_error error; fz_obj *oldroot, *root, *pages, *kids, *countobj, *parent; /* Load the old page tree */ - error = pdf_load_page_tree(xref); - if (error) - die(fz_error_note(error, "cannot load page tree")); + fz_try(xref->ctx) + { + pdf_load_page_tree(xref); + } + fz_catch(xref->ctx) + { + die(fz_error_note(1, "cannot load page tree")); + } /* Keep only pages/type entry to avoid references to unretained pages */ oldroot = fz_dict_gets(xref->trailer, "Root"); @@ -377,7 +381,6 @@ static void retainpages(int argc, char **argv) static void preloadobjstms(void) { - fz_error error; fz_obj *obj; int num; @@ -385,9 +388,14 @@ static void preloadobjstms(void) { if (xref->table[num].type == 'o') { - error = pdf_load_object(&obj, xref, num, 0); - if (error) - die(error); + fz_try(ctx) + { + obj = pdf_load_object(xref, num, 0); + } + fz_catch(ctx) + { + die(1); + } fz_drop_obj(obj); } } @@ -488,13 +496,17 @@ static void addhexfilter(fz_obj *dict) static void copystream(fz_obj *obj, int num, int gen) { - fz_error error; fz_buffer *buf, *tmp; fz_obj *newlen; - error = pdf_load_raw_stream(&buf, xref, num, gen); - if (error) - die(error); + fz_try(ctx) + { + buf = pdf_load_raw_stream(xref, num, gen); + } + fz_catch(ctx) + { + die(1); + } if (doascii && isbinarystream(buf)) { @@ -520,13 +532,17 @@ static void copystream(fz_obj *obj, int num, int gen) static void expandstream(fz_obj *obj, int num, int gen) { - fz_error error; fz_buffer *buf, *tmp; fz_obj *newlen; - error = pdf_load_stream(&buf, xref, num, gen); - if (error) - die(error); + fz_try(ctx) + { + buf = pdf_load_stream(xref, num, gen); + } + fz_catch(ctx) + { + die(1); + } fz_dict_dels(obj, "Filter"); fz_dict_dels(obj, "DecodeParms"); @@ -555,13 +571,17 @@ static void expandstream(fz_obj *obj, int num, int gen) static void writeobject(int num, int gen) { - fz_error error; fz_obj *obj; fz_obj *type; - error = pdf_load_object(&obj, xref, num, gen); - if (error) - die(error); + fz_try(ctx) + { + obj = pdf_load_object(xref, num, gen); + } + fz_catch(ctx) + { + die(1); + } /* skip ObjStm and XRef objects */ if (fz_is_dict(obj)) @@ -686,7 +706,6 @@ static void writepdf(void) int main(int argc, char **argv) { - fz_error error; char *infile; char *outfile = "out.pdf"; char *password = ""; @@ -724,9 +743,14 @@ int main(int argc, char **argv) if (ctx == NULL) die(fz_error_note(1, "failed to initialise context")); - error = pdf_open_xref(ctx, &xref, infile, password); - if (error) - die(fz_error_note(error, "cannot open input file '%s'", infile)); + fz_try(ctx) + { + xref = pdf_open_xref(ctx, infile, password); + } + fz_catch(ctx) + { + die(fz_error_note(1, "cannot open input file '%s'", infile)); + } out = fopen(outfile, "wb"); if (!out) diff --git a/apps/pdfdraw.c b/apps/pdfdraw.c index c65ef059..af21b9ea 100644 --- a/apps/pdfdraw.c +++ b/apps/pdfdraw.c @@ -92,7 +92,6 @@ static int isrange(char *s) static void drawpage(pdf_xref *xref, int pagenum) { - fz_error error; pdf_page *page; fz_display_list *list; fz_device *dev; @@ -104,9 +103,14 @@ static void drawpage(pdf_xref *xref, int pagenum) start = gettime(); } - error = pdf_load_page(&page, xref, pagenum - 1); - if (error) - die(fz_error_note(error, "cannot load page %d in file '%s'", pagenum, filename)); + fz_try(ctx) + { + page = pdf_load_page(xref, pagenum - 1); + } + fz_catch(ctx) + { + die(fz_error_note(1, "cannot load page %d in file '%s'", pagenum, filename)); + } list = NULL; @@ -114,9 +118,14 @@ static void drawpage(pdf_xref *xref, int pagenum) { list = fz_new_display_list(ctx); dev = fz_new_list_device(ctx, list); - error = pdf_run_page(xref, page, dev, fz_identity); - if (error) - die(fz_error_note(error, "cannot draw page %d in file '%s'", pagenum, filename)); + fz_try(ctx) + { + pdf_run_page(xref, page, dev, fz_identity); + } + fz_catch(ctx) + { + die(fz_error_note(1, "cannot draw page %d in file '%s'", pagenum, filename)); + } fz_free_device(dev); } @@ -193,15 +202,15 @@ static void drawpage(pdf_xref *xref, int pagenum) char buf[512]; sprintf(buf, output, pagenum); if (strstr(output, ".pgm") || strstr(output, ".ppm") || strstr(output, ".pnm")) - fz_write_pnm(pix, buf); + fz_write_pnm(ctx, pix, buf); else if (strstr(output, ".pam")) - fz_write_pam(pix, buf, savealpha); + fz_write_pam(ctx, pix, buf, savealpha); else if (strstr(output, ".png")) fz_write_png(ctx, pix, buf, savealpha); else if (strstr(output, ".pbm")) { fz_halftone *ht = fz_get_default_halftone(ctx, 1); fz_bitmap *bit = fz_halftone_pixmap(ctx, pix, ht); - fz_write_pbm(bit, buf); + fz_write_pbm(ctx, bit, buf); fz_drop_bitmap(ctx, bit); fz_drop_halftone(ctx, ht); } @@ -376,13 +385,23 @@ int main(int argc, char **argv) { filename = argv[fz_optind++]; - error = pdf_open_xref(ctx, &xref, filename, password); - if (error) + fz_try(ctx) + { + xref = pdf_open_xref(ctx, filename, password); + } + fz_catch(ctx) + { die(fz_error_note(error, "cannot open document: %s", filename)); + } - error = pdf_load_page_tree(xref); - if (error) + fz_try(ctx) + { + pdf_load_page_tree(xref); + } + fz_catch(ctx) + { die(fz_error_note(error, "cannot load page tree: %s", filename)); + } if (showxml) printf("<document name=\"%s\">\n", filename); diff --git a/apps/pdfextract.c b/apps/pdfextract.c index 7e09226e..5aa5fa09 100644 --- a/apps/pdfextract.c +++ b/apps/pdfextract.c @@ -39,7 +39,6 @@ static int isfontdesc(fz_obj *obj) static void saveimage(int num) { - fz_error error; fz_pixmap *img; fz_obj *ref; char name[1024]; @@ -48,9 +47,14 @@ static void saveimage(int num) /* TODO: detect DCTD and save as jpeg */ - error = pdf_load_image(&img, xref, ref); - if (error) - die(error); + fz_try(ctx) + { + img = pdf_load_image(xref, ref); + } + fz_catch(ctx) + { + die(1); + } if (dorgb && img->colorspace && img->colorspace != fz_device_rgb) { @@ -71,7 +75,7 @@ static void saveimage(int num) { sprintf(name, "img-%04d.pam", num); printf("extracting image %s\n", name); - fz_write_pam(img, name, 0); + fz_write_pam(ctx, img, name, 0); } fz_drop_pixmap(ctx, img); @@ -80,7 +84,6 @@ static void saveimage(int num) static void savefont(fz_obj *dict, int num) { - fz_error error; char name[1024]; char *subtype; fz_buffer *buf; @@ -133,11 +136,14 @@ static void savefont(fz_obj *dict, int num) return; } - buf = fz_new_buffer(ctx, 0); - - error = pdf_load_stream(&buf, xref, fz_to_num(stream), fz_to_gen(stream)); - if (error) - die(error); + fz_try(ctx) + { + buf = pdf_load_stream(xref, fz_to_num(stream), fz_to_gen(stream)); + } + fz_catch(ctx) + { + die(1); + } sprintf(name, "%s-%04d.%s", fontname, num, ext); printf("extracting font %s\n", name); @@ -158,15 +164,19 @@ static void savefont(fz_obj *dict, int num) static void showobject(int num) { - fz_error error; fz_obj *obj; if (!xref) die(fz_error_make("no file specified")); - error = pdf_load_object(&obj, xref, num, 0); - if (error) - die(error); + fz_try(ctx) + { + obj = pdf_load_object(xref, num, 0); + } + fz_catch(ctx) + { + die(1); + } if (isimage(obj)) saveimage(num); @@ -178,7 +188,6 @@ static void showobject(int num) int main(int argc, char **argv) { - fz_error error; char *infile; char *password = ""; int c, o; @@ -202,9 +211,14 @@ int main(int argc, char **argv) if (ctx == NULL) die(fz_error_note(1, "failed to initialise context")); - error = pdf_open_xref(ctx, &xref, infile, password); - if (error) - die(fz_error_note(error, "cannot open input file '%s'", infile)); + fz_try(ctx) + { + xref = pdf_open_xref(ctx, infile, password); + } + fz_catch(ctx) + { + die(fz_error_note(1, "cannot open input file '%s'", infile)); + } if (fz_optind == argc) { diff --git a/apps/pdfshow.c b/apps/pdfshow.c index fb4bebd1..21c3a8e1 100644 --- a/apps/pdfshow.c +++ b/apps/pdfshow.c @@ -47,7 +47,6 @@ static void showxref(void) static void showpagetree(void) { - fz_error error; fz_obj *ref; int count; int i; @@ -57,9 +56,14 @@ static void showpagetree(void) if (!xref->page_len) { - error = pdf_load_page_tree(xref); - if (error) - die(fz_error_note(error, "cannot load page tree")); + fz_try(xref->ctx) + { + pdf_load_page_tree(xref); + } + fz_catch(xref->ctx) + { + die(fz_error_note(1, "cannot load page tree")); + } } count = pdf_count_pages(xref); @@ -96,19 +100,23 @@ static void showsafe(unsigned char *buf, int n) static void showstream(int num, int gen) { - fz_error error; fz_stream *stm; unsigned char buf[2048]; int n; showcolumn = 0; - if (showdecode) - error = pdf_open_stream(&stm, xref, num, gen); - else - error = pdf_open_raw_stream(&stm, xref, num, gen); - if (error) - die(error); + fz_try(xref->ctx) + { + if (showdecode) + stm = pdf_open_stream(xref, num, gen); + else + stm = pdf_open_raw_stream(xref, num, gen); + } + fz_catch(xref->ctx) + { + die(1); + } while (1) { @@ -128,15 +136,19 @@ static void showstream(int num, int gen) static void showobject(int num, int gen) { - fz_error error; fz_obj *obj; if (!xref) die(fz_error_make("no file specified")); - error = pdf_load_object(&obj, xref, num, gen); - if (error) - die(error); + fz_try(ctx) + { + obj = pdf_load_object(xref, num, gen); + } + fz_catch(ctx) + { + die(1); + } if (pdf_is_stream(xref, num, gen)) { @@ -166,7 +178,6 @@ static void showobject(int num, int gen) static void showgrep(char *filename) { - fz_error error; fz_obj *obj; int i; @@ -174,9 +185,14 @@ static void showgrep(char *filename) { if (xref->table[i].type == 'n' || xref->table[i].type == 'o') { - error = pdf_load_object(&obj, xref, i, 0); - if (error) - die(error); + fz_try(ctx) + { + obj = pdf_load_object(xref, i, 0); + } + fz_catch(ctx) + { + die(1); + } fz_sort_dict(obj); @@ -195,7 +211,6 @@ int main(int argc, char **argv) { char *password = NULL; /* don't throw errors if encrypted */ char *filename; - fz_error error; int c; while ((c = fz_getopt(argc, argv, "p:be")) != -1) @@ -218,9 +233,14 @@ int main(int argc, char **argv) if (ctx == NULL) die(fz_error_note(1, "failed to initialise context")); - error = pdf_open_xref(ctx, &xref, filename, password); - if (error) - die(fz_error_note(error, "cannot open document: %s", filename)); + fz_try(ctx) + { + xref = pdf_open_xref(ctx, filename, password); + } + fz_catch(ctx) + { + die(fz_error_note(1, "cannot open document: %s", filename)); + } if (fz_optind == argc) showtrailer(); diff --git a/apps/win_main.c b/apps/win_main.c index 24279e1d..a77347e4 100644 --- a/apps/win_main.c +++ b/apps/win_main.c @@ -241,7 +241,7 @@ dloginfoproc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) SetDlgItemTextA(hwnd, 0x13, "n/a"); } - info = fz_dict_gets(xref->ctx, xref->trailer, "Info"); + info = fz_dict_gets(xref->trailer, "Info"); if (!info) return TRUE; @@ -253,21 +253,21 @@ dloginfoproc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) fz_free(context, ucs); \ } - if ((obj = fz_dict_gets(xref->ctx, info, "Title"))) + if ((obj = fz_dict_gets(info, "Title"))) SETUCS(0x20); - if ((obj = fz_dict_gets(xref->ctx, info, "Author"))) + if ((obj = fz_dict_gets(info, "Author"))) SETUCS(0x21); - if ((obj = fz_dict_gets(xref->ctx, info, "Subject"))) + if ((obj = fz_dict_gets(info, "Subject"))) SETUCS(0x22); - if ((obj = fz_dict_gets(xref->ctx, info, "Keywords"))) + if ((obj = fz_dict_gets(info, "Keywords"))) SETUCS(0x23); - if ((obj = fz_dict_gets(xref->ctx, info, "Creator"))) + if ((obj = fz_dict_gets(info, "Creator"))) SETUCS(0x24); - if ((obj = fz_dict_gets(xref->ctx, info, "Producer"))) + if ((obj = fz_dict_gets(info, "Producer"))) SETUCS(0x25); - if ((obj = fz_dict_gets(xref->ctx, info, "CreationDate"))) + if ((obj = fz_dict_gets(info, "CreationDate"))) SETUCS(0x26); - if ((obj = fz_dict_gets(xref->ctx, info, "ModDate"))) + if ((obj = fz_dict_gets(info, "ModDate"))) SETUCS(0x27); return TRUE; @@ -858,7 +858,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShow fz_accelerate(); - ctx = fz_context_init(&fz_alloc_default); + ctx = fz_new_context(/*&fz_alloc_default*/); if (ctx == NULL) { fprintf(stderr, "Failed to init context"); diff --git a/apps/xpsdraw.c b/apps/xpsdraw.c index 4544d09c..19cf600a 100644 --- a/apps/xpsdraw.c +++ b/apps/xpsdraw.c @@ -20,7 +20,7 @@ int uselist = 1; fz_colorspace *colorspace; fz_glyph_cache *glyphcache; char *filename; -fz_context *fzctx; +fz_context *ctx; struct { int count, total; @@ -91,16 +91,20 @@ static void drawpage(xps_document *doc, int pagenum) fz_display_list *list; fz_device *dev; int start; - int code; if (showtime) { start = gettime(); } - code = xps_load_page(&page, doc, pagenum - 1); - if (code) - die(fz_error_note(code, "cannot load page %d in file '%s'", pagenum, filename)); + fz_try(doc->ctx) + { + page = xps_load_page(doc, pagenum - 1); + } + fz_catch(doc->ctx) + { + die(fz_error_note(1, "cannot load page %d in file '%s'", pagenum, filename)); + } list = NULL; @@ -183,9 +187,9 @@ static void drawpage(xps_document *doc, int pagenum) char buf[512]; sprintf(buf, output, pagenum); if (strstr(output, ".pgm") || strstr(output, ".ppm") || strstr(output, ".pnm")) - fz_write_pnm(pix, buf); + fz_write_pnm(doc->ctx, pix, buf); else if (strstr(output, ".pam")) - fz_write_pam(pix, buf, savealpha); + fz_write_pam(doc->ctx, pix, buf, savealpha); else if (strstr(output, ".png")) fz_write_png(doc->ctx, pix, buf, savealpha); } @@ -278,7 +282,6 @@ int main(int argc, char **argv) int grayscale = 0; int accelerate = 1; xps_document *doc; - int code; int c; while ((c = fz_getopt(argc, argv, "o:p:r:Aadgmtx5")) != -1) @@ -311,14 +314,14 @@ int main(int argc, char **argv) if (accelerate) fz_accelerate(); - fzctx = fz_new_context(); - if (fzctx == NULL) + ctx = fz_new_context(); + if (ctx == NULL) { fprintf(stderr, "failed to initialise context"); exit(1); } - glyphcache = fz_new_glyph_cache(fzctx); + glyphcache = fz_new_glyph_cache(ctx); colorspace = fz_device_rgb; if (grayscale) @@ -342,9 +345,14 @@ int main(int argc, char **argv) { filename = argv[fz_optind++]; - code = xps_open_file(fzctx, &doc, filename); - if (code) - die(fz_error_note(code, "cannot open document: %s", filename)); + fz_try(ctx) + { + doc = xps_open_file(ctx, filename); + } + fz_catch(ctx) + { + die(fz_error_note(-1, "cannot open document: %s", filename)); + } if (showxml) printf("<document name=\"%s\">\n", filename); @@ -368,8 +376,8 @@ int main(int argc, char **argv) printf("slowest page %d: %dms\n", timing.maxpage, timing.max); } - fz_free_glyph_cache(fzctx, glyphcache); - fz_free_context(fzctx); + fz_free_glyph_cache(ctx, glyphcache); + fz_free_context(ctx); return 0; } |