PDFDocument* 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(context, 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_drop_document(context, raw_pdf_document); } fz_drop_context(context); return nullptr; } PDFDocument* document = new PDFDocument(page_cache_size); document->_fz_context = context; document->_pdf_document = raw_pdf_document; return document; }
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_rect bbox = ((struct data *) data)->bbox; fz_pixmap *pix = ((struct data *) data)->pix; fz_device *dev; 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); dev = fz_new_draw_device(ctx, &fz_identity, pix); fz_run_display_list(ctx, list, dev, &fz_identity, &bbox, NULL); fz_close_device(ctx, dev); fz_drop_device(ctx, dev); // This threads context is freed. fz_drop_context(ctx); fprintf(stderr, "thread at page %d done!\n", pagenumber); return data; }
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(ctx, infile); if (pdf_needs_password(ctx, doc)) if (!pdf_authenticate_password(ctx, doc, password)) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot authenticate password: %s", infile); if (fz_optind == argc) { int len = pdf_count_objects(ctx, doc); for (o = 1; o < len; o++) showobject(o); } else { while (fz_optind < argc) { showobject(atoi(argv[fz_optind])); fz_optind++; } } pdf_close_document(ctx, doc); fz_flush_warnings(ctx); fz_drop_context(ctx); return 0; }
int pdfsign_main(int argc, char **argv) { fz_context *ctx; pdf_document *doc; char *password = ""; int i, n, c; pdf_page *page = NULL; while ((c = fz_getopt(argc, argv, "p:")) != -1) { switch (c) { case 'p': password = fz_optarg; break; default: usage(); break; } } if (argc - fz_optind < 1) usage(); filename = argv[fz_optind++]; ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "cannot initialize context\n"); exit(1); } fz_var(page); doc = pdf_open_document(ctx, filename); fz_try(ctx) { if (pdf_needs_password(ctx, doc)) if (!pdf_authenticate_password(ctx, doc, password)) fz_warn(ctx, "cannot authenticate password: %s", filename); n = pdf_count_pages(ctx, doc); for (i = 0; i < n; ++i) { page = pdf_load_page(ctx, doc, i); verify_page(ctx, doc, i, page); fz_drop_page(ctx, (fz_page*)page); page = NULL; } } fz_always(ctx) pdf_drop_document(ctx, doc); fz_catch(ctx) { fz_drop_page(ctx, (fz_page*)page); fprintf(stderr, "error verify signatures: %s\n", fz_caught_message(ctx)); } fz_flush_warnings(ctx); fz_drop_context(ctx); return 0; }
static void do_close(pdfapp_t *app) { fz_context *ctx = app->ctx; pdfapp_close(app); free(dibinf); fz_drop_context(ctx); }
int pdfposter_main(int argc, char **argv) { char *infile; char *outfile = "out.pdf"; char *password = ""; int c; fz_write_options opts = { 0 }; pdf_document *doc; fz_context *ctx; opts.do_incremental = 0; opts.do_garbage = 0; opts.do_expand = 0; opts.do_ascii = 0; opts.do_linear = 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(ctx, infile); if (pdf_needs_password(ctx, doc)) if (!pdf_authenticate_password(ctx, doc, password)) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot authenticate password: %s", infile); decimatepages(ctx, doc); pdf_write_document(ctx, doc, outfile, &opts); pdf_close_document(ctx, doc); fz_drop_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_drop_document(_fz_context, _pdf_document); fz_drop_context(_fz_context); }
/** * Destroy a MuOfficeLib instance * * @param mu the instance to destroy */ void MuOfficeLib_destroy(MuOfficeLib *mu) { if (mu == NULL) return; fz_drop_context(mu->ctx); fin_muoffice_locks(mu); Pal_Mem_free(mu); }
MupdfBackend::~MupdfBackend() { if (m_doc) { fz_drop_document(m_ctx, m_doc); m_doc = 0; } if (m_ctx) { fz_drop_document_handler_context(m_ctx); // TODO: is this needed? fz_drop_context(m_ctx); m_ctx = 0; } }
int pdfpages_main(int argc, char **argv) { char *filename = ""; char *password = ""; int c; fz_output *out = NULL; 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); } fz_var(out); ret = 0; fz_try(ctx) { out = fz_stdout(ctx); ret = pdfpages_pages(ctx, out, filename, password, &argv[fz_optind], argc-fz_optind); } fz_catch(ctx) { ret = 1; } fz_drop_output(ctx, out); fz_drop_context(ctx); return ret; }
Viewer::~Viewer() { if (dev) { fz_drop_device(ctx, dev); } if (pix) { fz_drop_pixmap(ctx, pix); } if (page) { fz_drop_page(ctx, page); } if (doc) { fz_drop_document(ctx, doc); } if (ctx) { fz_drop_context(ctx); } }
/* Allocate new context structure, and initialise allocator, and sections * that aren't shared between contexts. */ static fz_context * new_context_phase1(const fz_alloc_context *alloc, const 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->user = NULL; ctx->alloc = alloc; ctx->locks = *locks; ctx->glyph_cache = NULL; ctx->error = Memento_label(fz_malloc_no_throw(ctx, sizeof(fz_error_context)), "fz_error_context"); if (!ctx->error) goto cleanup; ctx->error->top = ctx->error->stack - 1; ctx->error->errcode = FZ_ERROR_NONE; ctx->error->message[0] = 0; ctx->warn = Memento_label(fz_malloc_no_throw(ctx, sizeof(fz_warn_context)), "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_drop_context(ctx); return NULL; }
fz_context * fz_new_context_imp(const fz_alloc_context *alloc, const fz_locks_context *locks, size_t 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\n", 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_output_context(ctx); fz_new_store_context(ctx, max_store); fz_new_glyph_cache_context(ctx); fz_new_cmm_context(ctx); fz_new_colorspace_context(ctx); fz_new_font_context(ctx); fz_new_id_context(ctx); fz_new_document_handler_context(ctx); fz_new_style_context(ctx); fz_new_tuning_context(ctx); } fz_catch(ctx) { fprintf(stderr, "cannot create context (phase 2)\n"); fz_drop_context(ctx); return NULL; } return ctx; }
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_drop_context(ctx); }
/** * 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; }
bool MupdfBackend::load(const QString& filePath) { Q_ASSERT(m_doc == 0); Q_ASSERT(m_ctx == 0); m_ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT); // TODO: locks if (!m_ctx) { return false; } fz_register_document_handlers(m_ctx); fz_try(m_ctx) { m_doc = fz_open_document(m_ctx, filePath.toUtf8()); } fz_catch(m_ctx) { fz_drop_context(m_ctx); m_ctx = 0; return false; } return true; }
int main(int argc, char **argv) { char *input; float zoom, rotate; int page_number, page_count; fz_context *ctx; fz_document *doc; fz_pixmap *pix; fz_matrix ctm; int x, y; if (argc < 3) { fprintf(stderr, "usage: example input-file page-number [ zoom [ rotate ] ]\n"); fprintf(stderr, "\tinput-file: path of PDF, XPS, CBZ or EPUB document to open\n"); fprintf(stderr, "\tPage numbering starts from one.\n"); fprintf(stderr, "\tZoom level is in percent (100 percent is 72 dpi).\n"); fprintf(stderr, "\tRotation is in degrees clockwise.\n"); return EXIT_FAILURE; } input = argv[1]; page_number = atoi(argv[2]) - 1; zoom = argc > 3 ? atof(argv[3]) : 100; rotate = argc > 4 ? atof(argv[4]) : 0; /* Create a context to hold the exception stack and various caches. */ ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "cannot create mupdf context\n"); return EXIT_FAILURE; } /* Register the default file types to handle. */ fz_try(ctx) fz_register_document_handlers(ctx); fz_catch(ctx) { fprintf(stderr, "cannot register document handlers: %s\n", fz_caught_message(ctx)); fz_drop_context(ctx); return EXIT_FAILURE; } /* Open the document. */ fz_try(ctx) doc = fz_open_document(ctx, input); fz_catch(ctx) { fprintf(stderr, "cannot open document: %s\n", fz_caught_message(ctx)); fz_drop_context(ctx); return EXIT_FAILURE; } /* Count the number of pages. */ fz_try(ctx) page_count = fz_count_pages(ctx, doc); fz_catch(ctx) { fprintf(stderr, "cannot count number of pages: %s\n", fz_caught_message(ctx)); fz_drop_document(ctx, doc); fz_drop_context(ctx); return EXIT_FAILURE; } if (page_number < 0 || page_number >= page_count) { fprintf(stderr, "page number out of range: %d (page count %d)\n", page_number + 1, page_count); fz_drop_document(ctx, doc); fz_drop_context(ctx); return EXIT_FAILURE; } /* Compute a transformation matrix for the zoom and rotation desired. */ /* The default resolution without scaling is 72 dpi. */ fz_scale(&ctm, zoom / 100, zoom / 100); fz_pre_rotate(&ctm, rotate); /* Render page to an RGB pixmap. */ fz_try(ctx) pix = fz_new_pixmap_from_page_number(ctx, doc, page_number, &ctm, fz_device_rgb(ctx), 0); fz_catch(ctx) { fprintf(stderr, "cannot render page: %s\n", fz_caught_message(ctx)); fz_drop_document(ctx, doc); fz_drop_context(ctx); return EXIT_FAILURE; } /* Print image data in ascii PPM format. */ printf("P3\n"); printf("%d %d\n", pix->w, pix->h); printf("255\n"); for (y = 0; y < pix->h; ++y) { unsigned char *p = &pix->samples[y * pix->stride]; for (x = 0; x < pix->w; ++x) { if (x > 0) printf(" "); printf("%3d %3d %3d", p[0], p[1], p[2]); p += pix->n; } printf("\n"); } /* Clean up. */ fz_drop_pixmap(ctx, pix); fz_drop_document(ctx, doc); fz_drop_context(ctx); return EXIT_SUCCESS; }
int muconvert_main(int argc, char **argv) { int i, c; while ((c = fz_getopt(argc, argv, "p:A:W:H:S:U:o:F:O:")) != -1) { switch (c) { default: usage(); break; case 'p': password = fz_optarg; break; case 'A': alphabits = atoi(fz_optarg); break; case 'W': layout_w = atof(fz_optarg); break; case 'H': layout_h = atof(fz_optarg); break; case 'S': layout_em = atof(fz_optarg); break; case 'U': layout_css = fz_optarg; break; case 'o': output = fz_optarg; break; case 'F': format = fz_optarg; break; case 'O': options = fz_optarg; break; } } if (fz_optind == argc || (!format && !output)) usage(); /* Create a context to hold the exception stack and various caches. */ ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "cannot create mupdf context\n"); return EXIT_FAILURE; } /* Register the default file types to handle. */ fz_try(ctx) fz_register_document_handlers(ctx); fz_catch(ctx) { fprintf(stderr, "cannot register document handlers: %s\n", fz_caught_message(ctx)); fz_drop_context(ctx); return EXIT_FAILURE; } fz_set_aa_level(ctx, alphabits); if (layout_css) { fz_buffer *buf = fz_read_file(ctx, layout_css); fz_write_buffer_byte(ctx, buf, 0); fz_set_user_css(ctx, (char*)buf->data); fz_drop_buffer(ctx, buf); } /* Open the output document. */ fz_try(ctx) out = fz_new_document_writer(ctx, output, format, options); fz_catch(ctx) { fprintf(stderr, "cannot create document: %s\n", fz_caught_message(ctx)); fz_drop_context(ctx); return EXIT_FAILURE; } for (i = fz_optind; i < argc; ++i) { doc = fz_open_document(ctx, argv[i]); if (fz_needs_password(ctx, doc)) if (!fz_authenticate_password(ctx, doc, password)) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot authenticate password: %s", argv[i]); fz_layout_document(ctx, doc, layout_w, layout_h, layout_em); count = fz_count_pages(ctx, doc); if (i+1 < argc && fz_is_page_range(ctx, argv[i+1])) runrange(argv[++i]); else runrange("1-N"); fz_drop_document(ctx, doc); } fz_drop_document_writer(ctx, out); fz_drop_context(ctx); return EXIT_SUCCESS; }
int main(int argc, char **argv) { char *password = ""; fz_document *doc = NULL; fz_context *ctx; int c; fz_var(ctx, doc); while ((c = fz_getopt(argc, argv, "p:")) != -1) { switch (c) { default: usage(); break; case 'p': password = fz_optarg; break; } } if (fz_optind + 2 != argc) usage(); filename = argv[fz_optind]; mujstest_filename = argv[fz_optind+1]; if (strcmp(mujstest_filename, "-") == 0) mujstest_file = stdout; else mujstest_file = fopen(mujstest_filename, "wb"); ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT); if (!ctx) { fprintf(stderr, "cannot initialise context\n"); exit(1); } fz_register_document_handlers(ctx); fz_try(ctx) { doc = fz_open_document(ctx, filename); if (fz_needs_password(doc)) { if (!fz_authenticate_password(ctx, doc, password)) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot authenticate password: %s", filename); fprintf(mujstest_file, "PASSWORD %s\n", password); } fprintf(mujstest_file, "OPEN %s\n", filename); processpages(ctx, doc); fz_drop_document(ctx, doc); } fz_catch(ctx) { fprintf(stderr, "mjsgen: cannot process document: %s\n", filename); return 1; } fclose(mujstest_file); fz_drop_context(ctx); return 0; }
int pdfclean_main(int argc, char **argv) { char *infile; char *outfile = "out.pdf"; char *password = ""; int c; pdf_write_options opts = { 0 }; int errors = 0; fz_context *ctx; opts.continue_on_error = 1; opts.errors = &errors; while ((c = fz_getopt(argc, argv, "adfgilp:sz")) != -1) { switch (c) { case 'p': password = fz_optarg; break; case 'd': opts.do_decompress += 1; break; case 'z': opts.do_compress += 1; break; case 'f': opts.do_compress_fonts += 1; break; case 'i': opts.do_compress_images += 1; break; case 'a': opts.do_ascii += 1; break; case 'g': opts.do_garbage += 1; break; case 'l': opts.do_linear += 1; break; case 's': opts.do_clean += 1; break; default: usage(); break; } } if ((opts.do_ascii || opts.do_decompress) && !opts.do_compress) opts.do_pretty = 1; 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); } fz_try(ctx) { pdf_clean_file(ctx, infile, outfile, password, &opts, &argv[fz_optind], argc - fz_optind); } fz_catch(ctx) { errors++; } fz_drop_context(ctx); return errors != 0; }
int pdfmerge_main(int argc, char **argv) { pdf_write_options opts = { 0 }; char *output = "out.pdf"; char *flags = ""; char *input; int c; while ((c = fz_getopt(argc, argv, "o:O:")) != -1) { switch (c) { case 'o': output = fz_optarg; break; case 'O': flags = fz_optarg; break; default: usage(); break; } } if (fz_optind == argc) usage(); ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "error: Cannot initialize MuPDF context.\n"); exit(1); } pdf_parse_write_options(ctx, &opts, flags); fz_try(ctx) { doc_des = pdf_create_document(ctx); } fz_catch(ctx) { fprintf(stderr, "error: Cannot create destination document.\n"); exit(1); } /* Step through the source files */ while (fz_optind < argc) { input = argv[fz_optind++]; fz_try(ctx) { pdf_drop_document(ctx, doc_src); doc_src = pdf_open_document(ctx, input); if (fz_optind == argc || !fz_is_page_range(ctx, argv[fz_optind])) merge_range("1-N"); else merge_range(argv[fz_optind++]); } fz_catch(ctx) { fprintf(stderr, "error: Cannot merge document '%s'.\n", input); exit(1); } } fz_try(ctx) { pdf_save_document(ctx, doc_des, output, &opts); } fz_always(ctx) { pdf_drop_document(ctx, doc_des); pdf_drop_document(ctx, doc_src); } fz_catch(ctx) { fprintf(stderr, "error: Cannot save output file: '%s'.\n", output); exit(1); } fz_flush_warnings(ctx); fz_drop_context(ctx); return 0; }
int main(int argc, char **argv) { fz_context *ctx; fz_stream *fi; pdf_cmap *cmap; int k, m, n, i; struct cidrange *r; if (argc != 2) { fprintf(stderr, "usage: cmapclean input.cmap\n"); return 1; } ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "cannot initialise context\n"); return 1; } fi = fz_open_file(ctx, argv[1]); cmap = pdf_load_cmap(ctx, fi); fz_drop_stream(ctx, fi); printf("begincmap\n"); printf("/CMapName /%s def\n", cmap->cmap_name); printf("/WMode %d def\n", cmap->wmode); if (cmap->usecmap_name[0]) printf("/%s usecmap\n", cmap->usecmap_name); if (cmap->codespace_len) { printf("begincodespacerange\n"); for (k = 0; k < cmap->codespace_len; k++) { if (cmap->codespace[k].n == 1) printf("<%02x> <%02x>\n", cmap->codespace[k].low, cmap->codespace[k].high); else if (cmap->codespace[k].n == 2) printf("<%04x> <%04x>\n", cmap->codespace[k].low, cmap->codespace[k].high); else if (cmap->codespace[k].n == 3) printf("<%06x> <%06x>\n", cmap->codespace[k].low, cmap->codespace[k].high); else if (cmap->codespace[k].n == 4) printf("<%08x> <%08x>\n", cmap->codespace[k].low, cmap->codespace[k].high); else printf("<%x> <%x>\n", cmap->codespace[k].low, cmap->codespace[k].high); } printf("endcodespacerange\n"); } n = cmap->rlen + cmap->xlen; r = fz_malloc(ctx, n * sizeof *r); i = 0; for (k = 0; k < cmap->rlen; k++) { r[i].lo = cmap->ranges[k].low; r[i].hi = cmap->ranges[k].high; r[i].v = cmap->ranges[k].out; ++i; } for (k = 0; k < cmap->xlen; k++) { r[i].lo = cmap->xranges[k].low; r[i].hi = cmap->xranges[k].high; r[i].v = cmap->xranges[k].out; ++i; } qsort(r, n, sizeof *r, cmpcidrange); if (n) { printf("begincidchar\n"); for (i = 0; i < n; ++i) { for (k = r[i].lo, m = r[i].v; k <= r[i].hi; ++k, ++m) { pc(k); printf("%u\n", m); } } printf("endcidchar\n"); } #if 0 if (cmap->mlen > 0) { printf("beginbfchar\n"); for (k = 0; k < cmap->mlen; k++) { pc(cmap->mranges[k].low); printf("<"); for (m = 0; m < cmap->mranges[k].len; ++m) printf("%04x", cmap->mranges[k].out[m]); printf(">\n"); } printf("endbfchar\n"); } #endif printf("endcmap\n"); fz_drop_context(ctx); return 0; }
int pdfmerge_main(int argc, char **argv) { pdf_write_options opts = { 0 }; char *output = "out.pdf"; char *infile_src; int c; while ((c = fz_getopt(argc, argv, "adlszo:")) != -1) { switch (c) { case 'o': output = fz_optarg; break; case 'a': opts.do_ascii ++; break; case 'd': opts.do_expand ^= PDF_EXPAND_ALL; break; case 'l': opts.do_linear ++; break; case 's': opts.do_clean ++; break; case 'z': opts.do_deflate ++; break; default: usage(); break; } } if (fz_optind == argc) usage(); ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "Cannot initialise context\n"); exit(1); } fz_try(ctx) { doc_des = pdf_create_document(ctx); } fz_catch(ctx) { fprintf(stderr, "Failed to allocate destination document file %s\n", output); exit(1); } /* Step through the source files */ while (fz_optind < argc) { fz_try(ctx) { infile_src = argv[fz_optind++]; pdf_drop_document(ctx, doc_src); doc_src = pdf_open_document(ctx, infile_src); if (fz_optind == argc || !isrange(argv[fz_optind])) merge_range("1-"); else merge_range(argv[fz_optind++]); } fz_catch(ctx) { fprintf(stderr, "Failed merging document %s\n", infile_src); exit(1); } } fz_try(ctx) { pdf_save_document(ctx, doc_des, output, &opts); } fz_always(ctx) { pdf_drop_document(ctx, doc_des); pdf_drop_document(ctx, doc_src); } fz_catch(ctx) { fprintf(stderr, "Error encountered during file save.\n"); exit(1); } fz_flush_warnings(ctx); fz_drop_context(ctx); return 0; }
int main(int argc, char **argv) { char *filename = argc >= 2 ? argv[1] : ""; pthread_t *thread = NULL; fz_locks_context locks; pthread_mutex_t mutex[FZ_LOCK_MAX]; fz_context *ctx; fz_document *doc; int threads; 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. ctx = fz_new_context(NULL, &locks, FZ_STORE_UNLIMITED); // Register default file types. fz_register_document_handlers(ctx); // Open the PDF, XPS or CBZ document. Note, this binds doc to ctx. // You must only ever use doc with ctx - never a clone of it! doc = fz_open_document(ctx, filename); // Retrieve the number of pages, which translates to the // number of threads used for rendering pages. threads = fz_count_pages(ctx, doc); fprintf(stderr, "spawning %d threads, one per page...\n", threads); thread = malloc(threads * sizeof (pthread_t)); for (i = 0; i < threads; i++) { fz_page *page; fz_rect bbox; fz_irect rbox; fz_display_list *list; fz_device *dev; fz_pixmap *pix; struct data *data; // Load the relevant page for each thread. Note, that this // cannot be done on the worker threads, as each use of doc // uses ctx, and only one thread can be using ctx at a time. page = fz_load_page(ctx, doc, i); // Compute the bounding box for each page. fz_bound_page(ctx, page, &bbox); // Create a display list that will hold the drawing // commands for the page. Once we have the display list // this can safely be used on any other thread as it is // not bound to a given context. list = fz_new_display_list(ctx, &bbox); // Run the loaded page through a display list device // to populate the page's display list. dev = fz_new_list_device(ctx, list); fz_run_page(ctx, page, dev, &fz_identity, NULL); fz_close_device(ctx, dev); fz_drop_device(ctx, dev); // The page is no longer needed, all drawing commands // are now in the display list. fz_drop_page(ctx, page); // Create a white pixmap using the correct dimensions. pix = fz_new_pixmap_with_bbox(ctx, fz_device_rgb(ctx), fz_round_rect(&rbox, &bbox), 0); fz_clear_pixmap_with_value(ctx, pix, 0xff); // Populate the data structure to be sent to the // rendering thread for this page. 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_save_pixmap_as_png(ctx, data->pix, filename); // Free the thread's pixmap and display list since // they were allocated by the main thread above. fz_drop_pixmap(ctx, data->pix); fz_drop_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_drop_document(ctx, doc); fz_drop_context(ctx); return 0; }
MuPdf::Sharp::Fitz::Context::~Context() { fz_drop_context(m_pCtx); }
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[LONGLINE]; if (file_open) pdfapp_close(&gapp); 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_save_pixmap_as_pnm(ctx, gapp.image, text); else fz_save_pixmap_as_png(ctx, gapp.image, text); } 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, "Ignoring line without script statement.\n"); } } while (!feof(script)); fclose(script); } } fz_catch(ctx) { fprintf(stderr, "error: cannot execute '%s'\n", scriptname); } if (file_open) pdfapp_close(&gapp); fz_drop_context(ctx); return 0; }
int main(int argc, char **argv) { pdf_cmap *cmap; fz_stream *fi; FILE *fo; char name[256]; char *realname; int i, k, m; fz_context *ctx; if (argc < 3) { fprintf(stderr, "usage: cmapdump output.c lots of cmap files\n"); return 1; } ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "cannot initialise context\n"); return 1; } #undef fopen fo = fopen(argv[1], "wb"); if (!fo) { fprintf(stderr, "cmapdump: could not open output file '%s'\n", argv[1]); return 1; } fprintf(fo, "/* This is an automatically generated file. Do not edit. */\n"); for (i = 2; i < argc; i++) { realname = strrchr(argv[i], '/'); if (!realname) realname = strrchr(argv[i], '\\'); if (realname) realname ++; else realname = argv[i]; /* ignore VCS folders (such as .svn) */ if (*realname == '.') continue; if (strlen(realname) > (sizeof name - 1)) { fprintf(stderr, "cmapdump: file name too long\n"); if (fclose(fo)) { fprintf(stderr, "cmapdump: could not close output file '%s'\n", argv[1]); return 1; } return 1; } strcpy(name, realname); clean(name); fi = fz_open_file(ctx, argv[i]); cmap = pdf_load_cmap(ctx, fi); fz_drop_stream(ctx, fi); fprintf(fo, "\n/* %s */\n\n", cmap->cmap_name); if (cmap->rlen) { fprintf(fo, "static const pdf_range cmap_%s_ranges[] = {", name); for (k = 0; k < cmap->rlen; k++) { if (k % 4 == 0) fprintf(fo, "\n"); fprintf(fo, "{%uu,%uu,%uu},", cmap->ranges[k].low, cmap->ranges[k].high, cmap->ranges[k].out); } fprintf(fo, "\n};\n\n"); } if (cmap->xlen) { fprintf(fo, "static const pdf_xrange cmap_%s_xranges[] = {", name); for (k = 0; k < cmap->xlen; k++) { if (k % 4 == 0) fprintf(fo, "\n"); fprintf(fo, "{%uu,%uu,%uu},", cmap->xranges[k].low, cmap->xranges[k].high, cmap->xranges[k].out); } fprintf(fo, "\n};\n\n"); } if (cmap->mlen > 0) { fprintf(fo, "static const pdf_mrange cmap_%s_mranges[] = {", name); for (k = 0; k < cmap->mlen; k++) { fprintf(fo, "\n{%uu,%uu,{", cmap->mranges[k].low, cmap->mranges[k].len); for (m = 0; m < PDF_MRANGE_CAP; ++m) fprintf(fo, "%uu,", cmap->mranges[k].out[m]); fprintf(fo, "}},"); } fprintf(fo, "\n};\n\n"); } fprintf(fo, "static pdf_cmap cmap_%s = {\n", name); fprintf(fo, "\t{-1, pdf_drop_cmap_imp}, "); fprintf(fo, "\"%s\", ", cmap->cmap_name); fprintf(fo, "\"%s\", 0, ", cmap->usecmap_name); fprintf(fo, "%u, ", cmap->wmode); fprintf(fo, "%u,\n\t{ ", cmap->codespace_len); if (cmap->codespace_len == 0) { fprintf(fo, "{0,0,0},"); } for (k = 0; k < cmap->codespace_len; k++) { fprintf(fo, "{%u,%uu,%uu},", cmap->codespace[k].n, cmap->codespace[k].low, cmap->codespace[k].high); } fprintf(fo, " },\n"); if (cmap->rlen) fprintf(fo, "\t%u, %u, (pdf_range*) cmap_%s_ranges,\n", cmap->rlen, cmap->rlen, name); else fprintf(fo, "\t0, 0, NULL,\n"); if (cmap->xlen) fprintf(fo, "\t%u, %u, (pdf_xrange*) cmap_%s_xranges,\n", cmap->xlen, cmap->xlen, name); else fprintf(fo, "\t0, 0, NULL,\n"); if (cmap->mlen) fprintf(fo, "\t%u, %u, (pdf_mrange*) cmap_%s_mranges,\n", cmap->mlen, cmap->mlen, name); else fprintf(fo, "\t0, 0, NULL,\n"); fprintf(fo, "};\n"); if (getenv("verbose")) printf("\t{\"%s\",&cmap_%s},\n", cmap->cmap_name, name); pdf_drop_cmap(ctx, cmap); } if (fclose(fo)) { fprintf(stderr, "cmapdump: could not close output file '%s'\n", argv[1]); return 1; } fz_drop_context(ctx); return 0; }
int main(int argc, char **argv) #endif { const GLFWvidmode *video_mode; int c; while ((c = fz_getopt(argc, argv, "p:r:W:H:S:U:X")) != -1) { switch (c) { default: usage(argv[0]); break; case 'p': password = fz_optarg; break; case 'r': currentzoom = fz_atof(fz_optarg); break; case 'W': layout_w = fz_atof(fz_optarg); break; case 'H': layout_h = fz_atof(fz_optarg); break; case 'S': layout_em = fz_atof(fz_optarg); break; case 'U': layout_css = fz_optarg; break; case 'X': layout_use_doc_css = 0; break; } } if (fz_optind < argc) { fz_strlcpy(filename, argv[fz_optind], sizeof filename); } else { #ifdef _WIN32 win_install(); if (!win_open_file(filename, sizeof filename)) exit(0); #else usage(argv[0]); #endif } title = strrchr(filename, '/'); if (!title) title = strrchr(filename, '\\'); if (title) ++title; else title = filename; memset(&ui, 0, sizeof ui); search_input.p = search_input.text; search_input.q = search_input.p; search_input.end = search_input.p; glfwSetErrorCallback(on_error); if (!glfwInit()) { fprintf(stderr, "cannot initialize glfw\n"); exit(1); } video_mode = glfwGetVideoMode(glfwGetPrimaryMonitor()); screen_w = video_mode->width; screen_h = video_mode->height; window = glfwCreateWindow(DEFAULT_WINDOW_W, DEFAULT_WINDOW_H, filename, NULL, NULL); if (!window) { fprintf(stderr, "cannot create glfw window\n"); exit(1); } glfwMakeContextCurrent(window); ctx = fz_new_context(NULL, NULL, 0); fz_register_document_handlers(ctx); if (layout_css) { fz_buffer *buf = fz_read_file(ctx, layout_css); fz_set_user_css(ctx, fz_string_from_buffer(ctx, buf)); fz_drop_buffer(ctx, buf); } fz_set_use_document_css(ctx, layout_use_doc_css); has_ARB_texture_non_power_of_two = glfwExtensionSupported("GL_ARB_texture_non_power_of_two"); if (!has_ARB_texture_non_power_of_two) fz_warn(ctx, "OpenGL implementation does not support non-power of two texture sizes"); glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_texture_size); ui.fontsize = DEFAULT_UI_FONTSIZE; ui.baseline = DEFAULT_UI_BASELINE; ui.lineheight = DEFAULT_UI_LINEHEIGHT; ui_init_fonts(ctx, ui.fontsize); reload(); shrinkwrap(); glfwSetFramebufferSizeCallback(window, on_reshape); glfwSetCursorPosCallback(window, on_mouse_motion); glfwSetMouseButtonCallback(window, on_mouse_button); glfwSetScrollCallback(window, on_scroll); glfwSetCharModsCallback(window, on_char); glfwSetKeyCallback(window, on_key); glfwSetWindowRefreshCallback(window, on_display); glfwGetFramebufferSize(window, &window_w, &window_h); ui_needs_update = 1; while (!glfwWindowShouldClose(window)) { glfwWaitEvents(); if (ui_needs_update) run_main_loop(); } ui_finish_fonts(ctx); fz_drop_link(ctx, links); fz_drop_page(ctx, page); fz_drop_outline(ctx, outline); fz_drop_document(ctx, doc); fz_drop_context(ctx); glfwTerminate(); return 0; }