Document *PDFDocument::Open(const std::string &path, int page_cache_size) { fz_context *context = fz_new_context(nullptr, nullptr, FZ_STORE_DEFAULT); pdf_document *raw_pdf_document = nullptr; fz_try(context) { raw_pdf_document = pdf_open_document(context, path.c_str()); if ((raw_pdf_document == nullptr) || (!pdf_count_pages(raw_pdf_document))) { fz_throw(context, FZ_ERROR_GENERIC, const_cast<char *>("Cannot open document \"%s\""), path.c_str()); } } fz_catch(context) { if (raw_pdf_document != nullptr) { pdf_close_document(raw_pdf_document); } fz_free_context(context); return nullptr; } PDFDocument *document = new PDFDocument(page_cache_size); document->_fz_context = context; document->_pdf_document = raw_pdf_document; return document; }
fz_context * fz_new_context(fz_alloc_context *alloc, fz_locks_context *locks, unsigned int max_store) { fz_context *ctx; if (!alloc) alloc = &fz_alloc_default; if (!locks) locks = &fz_locks_default; ctx = new_context_phase1(alloc, locks); if (!ctx) return NULL; /* Now initialise sections that are shared */ fz_try(ctx) { fz_new_store_context(ctx, max_store); fz_new_glyph_cache_context(ctx); fz_new_colorspace_context(ctx); fz_new_font_context(ctx); fz_new_id_context(ctx); } fz_catch(ctx) { fprintf(stderr, "cannot create context (phase 2)\n"); fz_free_context(ctx); return NULL; } return ctx; }
void * renderer(void *data) { int pagenumber = ((struct data *) data)->pagenumber; fz_context *ctx = ((struct data *) data)->ctx; fz_display_list *list = ((struct data *) data)->list; fz_bbox bbox = ((struct data *) data)->bbox; fz_pixmap *pix = ((struct data *) data)->pix; fprintf(stderr, "thread at page %d loading!\n", pagenumber); // The context pointer is pointing to the main thread's // context, so here we create a new context based on it for // use in this thread. ctx = fz_clone_context(ctx); // Next we run the display list through the draw device which // will render the request area of the page to the pixmap. fprintf(stderr, "thread at page %d rendering!\n", pagenumber); fz_device *dev = fz_new_draw_device(ctx, pix); fz_run_display_list(list, dev, fz_identity, bbox, NULL); fz_free_device(dev); // This threads context is freed. fz_free_context(ctx); fprintf(stderr, "thread at page %d done!\n", pagenumber); return data; }
int main(int argc, char **argv) { /* get filename */ char *filename = NULL; if (argc != 2) { printf("usage: %s filename.pdf\n", argv[0]); return 1; } if (strcmp(argv[1], "--help") == 0) { printf("usage: %s filename.pdf\n", argv[0]); return 0; } filename = argv[1]; /* open document */ fz_context *context = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); fz_document *document = fz_open_document(context, filename); /* print page count */ printf("page count: %d\n", fz_count_pages(document)); fz_close_document(document); fz_free_context(context); return 0; }
/* ** Get /Info strings, e.g. wmupdf_info_field("myfile.pdf","Author",buf,255); ** Info labels: ** Title Document title ** Author Name of the person who created the doc. ** Subject Subject of the doc ** Keywords Keywords associated with the document. ** Creator If doc was converted to PDF from another format, the name ** of the product that created the original document. ** Producer If doc was converted to PDF from another format, the name ** of the product that converted it to PDF. ** CreationDate Date/Time document was created. ** ModDate Date/Time of most recent mod. */ int wmupdf_info_field(char *infile,char *label,char *buf,int maxlen) { pdf_document *xref; fz_context *ctx; pdf_obj *info,*obj; char *password=""; xref=NULL; buf[0]='\0'; ctx = fz_new_context(NULL,NULL,FZ_STORE_UNLIMITED); if (!ctx) return(-1); fz_try(ctx) { xref=pdf_open_document_no_run(ctx,infile); if (!xref) { fz_free_context(ctx); return(-2); } if (pdf_needs_password(xref) && !pdf_authenticate_password(xref,password)) { pdf_close_document(xref); fz_free_context(ctx); return(-3); } if (xref->trailer!=NULL && (info=pdf_dict_gets(xref->trailer,"Info"))!=NULL && (obj=pdf_dict_gets(info,label))!=NULL && pdf_is_string(obj)) { strncpy(buf,pdf_to_str_buf(obj),maxlen-1); buf[maxlen-1]='\0'; } } fz_always(ctx) { pdf_close_document(xref); } fz_catch(ctx) { } fz_free_context(ctx); return(0); }
int pdfposter_main(int argc, char **argv) { char *infile; char *outfile = "out.pdf"; char *password = ""; int c; fz_write_options opts; pdf_document *doc; fz_context *ctx; opts.do_incremental = 0; opts.do_garbage = 0; opts.do_expand = 0; opts.do_ascii = 0; while ((c = fz_getopt(argc, argv, "x:y:")) != -1) { switch (c) { case 'p': password = fz_optarg; break; case 'x': x_factor = atoi(fz_optarg); break; case 'y': y_factor = atoi(fz_optarg); break; default: usage(); break; } } if (argc - fz_optind < 1) usage(); infile = argv[fz_optind++]; if (argc - fz_optind > 0 && (strstr(argv[fz_optind], ".pdf") || strstr(argv[fz_optind], ".PDF"))) { outfile = argv[fz_optind++]; } ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "cannot initialise context\n"); exit(1); } doc = pdf_open_document_no_run(ctx, infile); if (pdf_needs_password(doc)) if (!pdf_authenticate_password(doc, password)) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot authenticate password: %s", infile); /* Only retain the specified subset of the pages */ decimatepages(doc); pdf_write_document(doc, outfile, &opts); pdf_close_document(doc); fz_free_context(ctx); return 0; }
PDFDocument::~PDFDocument() { // Must destroy page cache explicitly first, since destroying cached pages // involves clearing MuPDF state, which requires document structures // (_pdf_document, _fz_context) to still exist. _page_cache.reset(); pdf_close_document(_pdf_document); fz_free_context(_fz_context); }
Pdf::~Pdf() { pdf_free_xref(xref); --references; if (references == 0) { fz_free_glyph_cache(ctx, cache); cache = 0; fz_free_context(ctx); ctx = 0; } }
void DrPDFExtractor::Initialize() { if (m_ctx) { fz_free_context(m_ctx); } m_ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (m_doc) { fz_close_document(m_doc); m_doc = NULL; } }
Pdf::~Pdf() { if (doc) fz_close_document(doc); doc = 0; --references; if (references == 0) { fz_free_context(ctx); ctx = 0; } }
int pdfextract_main(int argc, char **argv) { char *infile; char *password = ""; int c, o; while ((c = fz_getopt(argc, argv, "p:r")) != -1) { switch (c) { case 'p': password = fz_optarg; break; case 'r': dorgb++; break; default: usage(); break; } } if (fz_optind == argc) usage(); infile = argv[fz_optind++]; ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "cannot initialise context\n"); exit(1); } doc = pdf_open_document_no_run(ctx, infile); if (pdf_needs_password(doc)) if (!pdf_authenticate_password(doc, password)) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot authenticate password: %s", infile); if (fz_optind == argc) { int len = pdf_count_objects(doc); for (o = 1; o < len; o++) showobject(o); } else { while (fz_optind < argc) { showobject(atoi(argv[fz_optind])); fz_optind++; } } pdf_close_document(doc); fz_flush_warnings(ctx); fz_free_context(ctx); return 0; }
/* Destructor */ muctx::~muctx(void) { fz_free_outline(mu_ctx, mu_outline); fz_close_document(mu_doc); page_cache->Empty(mu_ctx); fz_free_context(mu_ctx); mu_ctx = NULL; mu_doc = NULL; mu_outline = NULL; delete page_cache; page_cache = NULL; }
void muctx::CleanUp(void) { fz_free_outline(mu_ctx, mu_outline); fz_close_document(mu_doc); page_cache->Empty(mu_ctx); fz_free_context(mu_ctx); delete page_cache; page_cache = NULL; this->mu_ctx = NULL; this->mu_doc = NULL; this->mu_outline = NULL; }
JNIEXPORT int JNICALL Java_com_artifex_mupdf_MuPDFCore_openFile(JNIEnv * env, jobject thiz, jstring jfilename) { const char *filename; int result = 0; filename = (*env)->GetStringUTFChars(env, jfilename, NULL); if (filename == NULL) { LOGE("Failed to get filename"); return 0; } /* 128 MB store for low memory devices. Tweak as necessary. */ ctx = fz_new_context(NULL, NULL, 128 << 20); if (!ctx) { LOGE("Failed to initialise context"); return 0; } doc = NULL; fz_try(ctx) { colorspace = fz_device_rgb; LOGE("Opening document..."); fz_try(ctx) { doc = fz_open_document(ctx, (char *)filename); } fz_catch(ctx) { fz_throw(ctx, "Cannot open document: '%s'\n", filename); } LOGE("Done!"); result = 1; } fz_catch(ctx) { LOGE("Failed: %s", ctx->error->message); fz_close_document(doc); doc = NULL; fz_free_context(ctx); ctx = NULL; } (*env)->ReleaseStringUTFChars(env, jfilename, filename); return result; }
int wmupdf_numpages(char *filename) { fz_context *ctx; fz_document *doc; int np; doc=NULL; ctx = fz_new_context(NULL,NULL,FZ_STORE_DEFAULT); if (!ctx) return(-1); fz_try(ctx) { doc=fz_open_document(ctx,filename); } fz_catch(ctx) { fz_free_context(ctx); return(-2); } np=fz_count_pages(doc); fz_close_document(doc); fz_flush_warnings(ctx); fz_free_context(ctx); return(np); }
void benchfile(char *pdffilename, int loadonly, int pageNo) { pdf_document *xref = NULL; mstimer timer; int page_count; int curpage; fz_context *ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT); if (!ctx) { logbench("Error: fz_new_context() failed\n"); return; } logbench("Starting: %s\n", pdffilename); timerstart(&timer); fz_var(xref); fz_try(ctx) { xref = openxref(ctx, pdffilename); } fz_catch(ctx) { goto Exit; } timerstop(&timer); logbench("load: %.2f ms\n", timeinms(&timer)); page_count = pdf_count_pages(xref); logbench("page count: %d\n", page_count); if (loadonly) goto Exit; for (curpage = 1; curpage <= page_count; curpage++) { pdf_page *page; if ((-1 != pageNo) && (pageNo != curpage)) continue; page = benchloadpage(ctx, xref, curpage); if (page) { benchrenderpage(ctx, xref, page, curpage); pdf_free_page(xref, page); } } Exit: logbench("Finished: %s\n", pdffilename); pdf_close_document(xref); fz_free_context(ctx); }
/* Render thread entry */ static void *render_thread(void *t) { SDL_Event my_event; struct least_thread *self = t; my_event.type = LEAST_PAGE_COMPLETE; my_event.user.data1 = t; self->context = fz_clone_context(self->context); if (!self->context) { fprintf(stderr, "In render thread %d: fz_clone_context returned NULL\n", self->id); abort(); } printf("Render thread %d up and running.\n", self->id); pthread_mutex_lock(&self->mutex); /* Wait for first command */ pthread_cond_wait(&self->cond, &self->mutex); while (self->keep_running) { printf("Thread %d: Rendering page %d\n", self->id, self->pagenum); /* Render a page */ self->pixmap = page_to_pixmap(self->context, doc, self->pagenum); if (!self->pixmap) { fprintf(stderr, "In render thread %d: " "page_to_pixmap returned NULL\n", self->id); abort(); } /* Push completed page to event queue */ SDL_PushEvent(&my_event); pthread_cond_wait(&self->cond, &self->mutex); } pthread_mutex_unlock(&self->mutex); /* Cleanup */ fz_free_context(self->context); return NULL; }
/* Allocate new context structure, and initialise allocator, and sections * that aren't shared between contexts. */ static fz_context * new_context_phase1(fz_alloc_context *alloc, fz_locks_context *locks) { fz_context *ctx; ctx = alloc->malloc(alloc->user, sizeof(fz_context)); if (!ctx) return NULL; memset(ctx, 0, sizeof *ctx); ctx->alloc = alloc; ctx->locks = locks; ctx->glyph_cache = NULL; ctx->error = fz_malloc_no_throw(ctx, sizeof(fz_error_context)); if (!ctx->error) goto cleanup; ctx->error->top = -1; ctx->error->errcode = FZ_ERROR_NONE; ctx->error->message[0] = 0; ctx->warn = fz_malloc_no_throw(ctx, sizeof(fz_warn_context)); if (!ctx->warn) goto cleanup; ctx->warn->message[0] = 0; ctx->warn->count = 0; /* New initialisation calls for context entries go here */ fz_try(ctx) { fz_new_aa_context(ctx); } fz_catch(ctx) { goto cleanup; } return ctx; cleanup: fprintf(stderr, "cannot create context (phase 1)\n"); fz_free_context(ctx); return NULL; }
DrPDFExtractor::~DrPDFExtractor() { if (m_doc) { fz_close_document(m_doc); m_doc = NULL; } if (m_ctx != NULL) { fz_free_context(m_ctx); m_ctx = NULL; } if (m_filename) { delete [] m_filename; } if (m_fontcache) { delete m_fontcache; m_fontcache = NULL; } m_isopen = false; }
void Mpdf::deletePdf() { if (!doc) { return; } fz_close_document(doc); doc = NULL; if (!ctx) { return; } fz_free_context(ctx); ctx = NULL; system("ls *.png | egrep '^[0-9]' | xargs rm -rf"); }
MuPDFDoc::~MuPDFDoc() { CoTaskMemFree(m_cts); if (m_outline) { fz_free_outline(m_context, m_outline); m_outline = nullptr; } if (m_document) { ClearPages(); fz_close_document(m_document); m_document = nullptr; } if (m_context) { fz_free_context(m_context); m_context = nullptr; } }
void cleanup(pdfapp_t *app) { fz_context *ctx = app->ctx; pdfapp_close(app); XDestroyWindow(xdpy, xwin); XFreePixmap(xdpy, xicon); XFreeCursor(xdpy, xccaret); XFreeCursor(xdpy, xcwait); XFreeCursor(xdpy, xchand); XFreeCursor(xdpy, xcarrow); XFreeGC(xdpy, xgc); XCloseDisplay(xdpy); fz_free_context(ctx); }
fz_context * fz_new_context_imp(fz_alloc_context *alloc, fz_locks_context *locks, unsigned int max_store, const char *version) { fz_context *ctx; if (strcmp(version, FZ_VERSION)) { fprintf(stderr, "cannot create context: incompatible header (%s) and library (%s) versions", version, FZ_VERSION); return NULL; } if (!alloc) alloc = &fz_alloc_default; if (!locks) locks = &fz_locks_default; ctx = new_context_phase1(alloc, locks); if (!ctx) return NULL; /* Now initialise sections that are shared */ fz_try(ctx) { fz_new_store_context(ctx, max_store); fz_new_glyph_cache_context(ctx); fz_new_colorspace_context(ctx); fz_new_font_context(ctx); fz_new_id_context(ctx); fz_new_document_handler_context(ctx); } fz_catch(ctx) { fprintf(stderr, "cannot create context (phase 2)\n"); fz_free_context(ctx); return NULL; } return ctx; }
void convertFile(AcString pdfName, int page, BOOL bDwg) { fz_context *ctx; ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT); if (!ctx) { return; } pdfapp_t gapp; pdfapp_init(ctx, &gapp); gapp.strFileName = pdfName; gapp.pageno = page; gapp.bOutputDwg = bDwg; //½øÐÐת»» char utf8Name[PATH_MAX]; pdfapp_AcString2Utf8Char(gapp.strFileName, utf8Name); pdfapp_open(&gapp, utf8Name, 0); pdfapp_close(&gapp); fz_free_context(ctx); }
int main(int argc, char **argv) { char *filename = argv[1]; pthread_t *thread = NULL; fz_locks_context locks; pthread_mutex_t mutex[FZ_LOCK_MAX]; int i; // Initialize FZ_LOCK_MAX number of non-recursive mutexes. for (i = 0; i < FZ_LOCK_MAX; i++) { if (pthread_mutex_init(&mutex[i], NULL) < 0) fail("pthread_mutex_init()"); } // Initialize the locking structure with function pointers to // the locking functions and to the user data. In this case // the user data is a pointer to the array of mutexes so the // locking functions can find the relevant lock to change when // they are called. This way we avoid global variables. locks.user = mutex; locks.lock = lock_mutex; locks.unlock = unlock_mutex; // This is the main threads context function, so supply the // locking structure. This context will be used to parse all // the pages from the document. fz_context *ctx = fz_new_context(NULL, &locks, FZ_STORE_UNLIMITED); // Open the PDF, XPS or CBZ document. fz_document *doc = fz_open_document(ctx, filename); // Retrieve the number of pages, which translates to the // number of threads used for rendering pages. int threads = fz_count_pages(doc); fprintf(stderr, "spawning %d threads, one per page...\n", threads); thread = malloc(threads * sizeof (pthread_t)); for (i = 0; i < threads; i++) { // Load the relevant page for each thread. fz_page *page = fz_load_page(doc, i); // Compute the bounding box for each page. fz_rect rect = fz_bound_page(doc, page); fz_bbox bbox = fz_round_rect(rect); // Create a display list that will hold the drawing // commands for the page. fz_display_list *list = fz_new_display_list(ctx); // Run the loaded page through a display list device // to populate the page's display list. fz_device *dev = fz_new_list_device(ctx, list); fz_run_page(doc, page, dev, fz_identity, NULL); fz_free_device(dev); // The page is no longer needed, all drawing commands // are now in the display list. fz_free_page(doc, page); // Create a white pixmap using the correct dimensions. fz_pixmap *pix = fz_new_pixmap_with_bbox(ctx, fz_device_rgb, bbox); fz_clear_pixmap_with_value(ctx, pix, 0xff); // Populate the data structure to be sent to the // rendering thread for this page. struct data *data = malloc(sizeof (struct data)); data->pagenumber = i + 1; data->ctx = ctx; data->list = list; data->bbox = bbox; data->pix = pix; // Create the thread and pass it the data structure. if (pthread_create(&thread[i], NULL, renderer, data) < 0) fail("pthread_create()"); } // Now each thread is rendering pages, so wait for each thread // to complete its rendering. fprintf(stderr, "joining %d threads...\n", threads); for (i = threads - 1; i >= 0; i--) { char filename[42]; struct data *data; if (pthread_join(thread[i], (void **) &data) < 0) fail("pthread_join"); sprintf(filename, "out%04d.png", i); fprintf(stderr, "\tSaving %s...\n", filename); // Write the rendered image to a PNG file fz_write_png(ctx, data->pix, filename, 0); // Free the thread's pixmap and display list since // they were allocated by the main thread above. fz_drop_pixmap(ctx, data->pix); fz_free_display_list(ctx, data->list); // Free the data structured passed back and forth // between the main thread and rendering thread. free(data); } fprintf(stderr, "finally!\n"); fflush(NULL); free(thread); // Finally the document is closed and the main thread's // context is freed. fz_close_document(doc); fz_free_context(ctx); return 0; }
ViewerWidget::~ViewerWidget() { scene->clear(); fz_free_context(context); }
void render(char *filename, int pagenumber, int zoom, int rotation) { // Create a context to hold the exception stack and various caches. fz_context *ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); // Open the PDF, XPS or CBZ document. fz_document *doc = fz_open_document(ctx, filename); // Retrieve the number of pages (not used in this example). int pagecount = fz_count_pages(doc); // Load the page we want. Page numbering starts from zero. fz_page *page = fz_load_page(doc, pagenumber - 1); // Calculate a transform to use when rendering. This transform // contains the scale and rotation. Convert zoom percentage to a // scaling factor. Without scaling the resolution is 72 dpi. fz_matrix transform; fz_rotate(&transform, rotation); fz_pre_scale(&transform, zoom / 100.0f, zoom / 100.0f); // Take the page bounds and transform them by the same matrix that // we will use to render the page. fz_rect bounds; fz_bound_page(doc, page, &bounds); fz_transform_rect(&bounds, &transform); // Create a blank pixmap to hold the result of rendering. The // pixmap bounds used here are the same as the transformed page // bounds, so it will contain the entire page. The page coordinate // space has the origin at the top left corner and the x axis // extends to the right and the y axis extends down. fz_irect bbox; fz_round_rect(&bbox, &bounds); fz_pixmap *pix = fz_new_pixmap_with_bbox(ctx, fz_device_rgb(ctx), &bbox); fz_clear_pixmap_with_value(ctx, pix, 0xff); // A page consists of a series of objects (text, line art, images, // gradients). These objects are passed to a device when the // interpreter runs the page. There are several devices, used for // different purposes: // // draw device -- renders objects to a target pixmap. // // text device -- extracts the text in reading order with styling // information. This text can be used to provide text search. // // list device -- records the graphic objects in a list that can // be played back through another device. This is useful if you // need to run the same page through multiple devices, without // the overhead of parsing the page each time. // Create a draw device with the pixmap as its target. // Run the page with the transform. fz_device *dev = fz_new_draw_device(ctx, pix); fz_run_page(doc, page, dev, &transform, NULL); fz_free_device(dev); // Save the pixmap to a file. fz_write_png(ctx, pix, "out.png", 0); // Clean up. fz_drop_pixmap(ctx, pix); fz_free_page(doc, page); fz_close_document(doc); fz_free_context(ctx); }
int main(int argc, char **argv) { fz_document *doc = NULL; int c; fz_context *ctx; fz_var(doc); yctx = yutani_init(); char * _width = getenv("WIDTH"); char * _height = getenv("HEIGHT"); width = _width ? atoi(_width) : 512; height = _height ? atoi(_height) : 512; init_decorations(); window = yutani_window_create(yctx, width + decor_width(), height + decor_height()); yutani_window_move(yctx, window, 50, 50); yutani_window_advertise_icon(yctx, window, "PDF Viewer", "pdfviewer"); gfx_ctx = init_graphics_yutani(window); draw_fill(gfx_ctx,rgb(0,0,0)); render_decorations(window, gfx_ctx, "PDFViewer - Loading..."); while ((c = fz_getopt(argc, argv, "wf")) != -1) { switch (c) { case 'f': fit = 1; break; } } ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT); if (!ctx) { fprintf(stderr, "Could not initialize fitz context.\n"); exit(1); } fz_set_aa_level(ctx, alphabits); colorspace = fz_device_rgb; fz_try(ctx) { while (fz_optind < argc) { fz_try(ctx) { filename = argv[fz_optind++]; files++; fz_try(ctx) { doc = fz_open_document(ctx, filename); } fz_catch(ctx) { fz_throw(ctx, "cannot open document: %s", filename); } if (fz_optind == argc || !isrange(argv[fz_optind])) drawrange(ctx, doc, "1-"); if (fz_optind < argc && isrange(argv[fz_optind])) drawrange(ctx, doc, argv[fz_optind++]); fz_close_document(doc); doc = NULL; } fz_catch(ctx) { if (!ignore_errors) fz_rethrow(ctx); fz_close_document(doc); doc = NULL; fz_warn(ctx, "ignoring error in '%s'", filename); } } } fz_catch(ctx) { fz_close_document(doc); fprintf(stderr, "error: cannot draw '%s'\n", filename); errored = 1; } fz_free_context(ctx); return (errored != 0); }
int pdfinfo_main(int argc, char **argv) { enum { NO_FILE_OPENED, NO_INFO_GATHERED, INFO_SHOWN } state; char *filename = ""; char *password = ""; int show = ALL; int c; while ((c = fz_getopt(argc, argv, "mfispxd:")) != -1) { switch (c) { case 'm': if (show == ALL) show = DIMENSIONS; else show |= DIMENSIONS; break; case 'f': if (show == ALL) show = FONTS; else show |= FONTS; break; case 'i': if (show == ALL) show = IMAGES; else show |= IMAGES; break; case 's': if (show == ALL) show = SHADINGS; else show |= SHADINGS; break; case 'p': if (show == ALL) show = PATTERNS; else show |= PATTERNS; break; case 'x': if (show == ALL) show = XOBJS; else show |= XOBJS; break; case 'd': 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); } state = NO_FILE_OPENED; while (fz_optind < argc) { if (state == NO_FILE_OPENED || !arg_is_page_range(argv[fz_optind])) { if (state == NO_INFO_GATHERED) { showinfo(filename, show, "1-"); } closexref(); filename = argv[fz_optind]; printf("%s:\n", filename); doc = pdf_open_document_no_run(ctx, filename); if (pdf_needs_password(doc)) if (!pdf_authenticate_password(doc, password)) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot authenticate password: %s", filename); pagecount = pdf_count_pages(doc); showglobalinfo(); state = NO_INFO_GATHERED; } else { showinfo(filename, show, argv[fz_optind]); state = INFO_SHOWN; } fz_optind++; } if (state == NO_INFO_GATHERED) showinfo(filename, show, "1-"); closexref(); fz_free_context(ctx); return 0; }
int main(int argc, char *argv[]) { fz_context *ctx; FILE *script = NULL; int c; while ((c = fz_getopt(argc, argv, "o:p:v")) != -1) { switch(c) { case 'o': output = fz_optarg; break; case 'p': prefix = fz_optarg; break; case 'v': verbosity ^= 1; break; default: usage(); break; } } if (fz_optind == argc) usage(); ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT); if (!ctx) { fprintf(stderr, "cannot initialise context\n"); exit(1); } pdfapp_init(ctx, &gapp); gapp.scrw = 640; gapp.scrh = 480; gapp.colorspace = fz_device_rgb(ctx); fz_try(ctx) { while (fz_optind < argc) { scriptname = argv[fz_optind++]; script = fopen(scriptname, "rb"); if (script == NULL) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open script: %s", scriptname); do { char *line = my_getline(script); if (line == NULL) continue; if (verbosity) fprintf(stderr, "'%s'\n", line); if (match(&line, "%")) { /* Comment */ } else if (match(&line, "PASSWORD")) { strcpy(pd_password, line); } else if (match(&line, "OPEN")) { char path[1024]; if (file_open) pdfapp_close(&gapp); strcpy(filename, line); if (prefix) { sprintf(path, "%s%s", prefix, line); } else { strcpy(path, line); } pdfapp_open(&gapp, path, 0); file_open = 1; } else if (match(&line, "GOTO")) { pdfapp_gotopage(&gapp, atoi(line)-1); } else if (match(&line, "SCREENSHOT")) { char text[1024]; sprintf(text, output, ++shotcount); if (strstr(text, ".pgm") || strstr(text, ".ppm") || strstr(text, ".pnm")) fz_write_pnm(ctx, gapp.image, text); else fz_write_png(ctx, gapp.image, text, 0); } else if (match(&line, "RESIZE")) { int w, h; sscanf(line, "%d %d", &w, &h); pdfapp_onresize(&gapp, w, h); } else if (match(&line, "CLICK")) { float x, y, b; int n; n = sscanf(line, "%f %f %f", &x, &y, &b); if (n < 1) x = 0.0f; if (n < 2) y = 0.0f; if (n < 3) b = 1; /* state = 1 = transition down */ pdfapp_onmouse(&gapp, (int)x, (int)y, b, 0, 1); /* state = -1 = transition up */ pdfapp_onmouse(&gapp, (int)x, (int)y, b, 0, -1); } else if (match(&line, "TEXT")) { unescape_string(td_textinput, line); } else { fprintf(stderr, "Unmatched: %s\n", line); } } while (!feof(script)); fclose(script); } } fz_catch(ctx) { fprintf(stderr, "error: cannot execute '%s'\n", scriptname); } if (file_open) pdfapp_close(&gapp); fz_free_context(ctx); return 0; }