void fz_clip_image_mask(fz_context *ctx, fz_device *dev, fz_image *image, const fz_matrix *ctm, const fz_rect *scissor) { if (dev->error_depth) { dev->error_depth++; return; } fz_try(ctx) { if (dev->hints & FZ_MAINTAIN_CONTAINER_STACK) { if (scissor == NULL) { fz_rect bbox = fz_unit_rect; fz_transform_rect(&bbox, ctm); push_clip_stack(ctx, dev, &bbox, fz_device_container_stack_is_clip_image_mask); } else push_clip_stack(ctx, dev, scissor, fz_device_container_stack_is_clip_image_mask); } if (dev->clip_image_mask) dev->clip_image_mask(ctx, dev, image, ctm, scissor); } fz_catch(ctx) { dev->error_depth = 1; strcpy(dev->errmess, fz_caught_message(ctx)); /* Error swallowed */ } }
int fz_begin_tile_id(fz_device *dev, const fz_rect *area, const fz_rect *view, float xstep, float ystep, const fz_matrix *ctm, int id) { fz_context *ctx = dev->ctx; int ret = 0; if (dev->error_depth) { dev->error_depth++; return 0; } if (xstep < 0) xstep = -xstep; if (ystep < 0) ystep = -ystep; fz_try(ctx) { if (dev->begin_tile) ret = dev->begin_tile(dev, area, view, xstep, ystep, ctm, id); } fz_catch(ctx) { dev->error_depth = 1; strcpy(dev->errmess, fz_caught_message(ctx)); /* Error swallowed */ } return ret; }
void fz_clip_text(fz_device *dev, fz_text *text, const fz_matrix *ctm, int accumulate) { fz_context *ctx = dev->ctx; if (dev->error_depth) { if (accumulate == 0 || accumulate == 1) dev->error_depth++; return; } fz_try(ctx) { if (dev->hints & FZ_MAINTAIN_CONTAINER_STACK) { fz_rect bbox; fz_bound_text(ctx, text, NULL, ctm, &bbox); push_clip_stack_accumulate(dev, &bbox, accumulate); } if (dev->clip_text) dev->clip_text(dev, text, ctm, accumulate); } fz_catch(ctx) { if (accumulate == 2) fz_rethrow(ctx); dev->error_depth = 1; strcpy(dev->errmess, fz_caught_message(ctx)); /* Error swallowed */ } }
void fz_clip_text(fz_device *dev, fz_text *text, const fz_matrix *ctm, int accumulate) { fz_context *ctx = dev->ctx; if (dev->error_depth) { if (accumulate == 0 || accumulate == 1) dev->error_depth++; return; } fz_try(ctx) { if (dev->clip_text) dev->clip_text(dev, text, ctm, accumulate); } fz_catch(ctx) { if (accumulate == 2) fz_rethrow(ctx); dev->error_depth = 1; strcpy(dev->errmess, fz_caught_message(ctx)); /* Error swallowed */ } }
void fz_end_mask(fz_context *ctx, fz_device *dev) { if (dev->error_depth) { /* Converts from mask to clip, so no change in stack depth */ return; } if (dev->hints & FZ_MAINTAIN_CONTAINER_STACK) { dev->container[dev->container_len-1].flags &= ~fz_device_container_stack_in_mask; dev->container[dev->container_len-1].flags |= fz_device_container_stack_is_mask; } fz_try(ctx) { if (dev->end_mask) dev->end_mask(ctx, dev); } fz_catch(ctx) { dev->error_depth = 1; strcpy(dev->errmess, fz_caught_message(ctx)); /* Error swallowed */ } }
void fz_clip_text(fz_context *ctx, fz_device *dev, const fz_text *text, const fz_matrix *ctm, const fz_rect *scissor) { if (dev->error_depth) { dev->error_depth++; return; } fz_try(ctx) { if (dev->hints & FZ_MAINTAIN_CONTAINER_STACK) { if (scissor == NULL) { fz_rect bbox; fz_bound_text(ctx, text, NULL, ctm, &bbox); push_clip_stack(ctx, dev, &bbox, fz_device_container_stack_is_clip_text); } else push_clip_stack(ctx, dev, scissor, fz_device_container_stack_is_clip_text); } if (dev->clip_text) dev->clip_text(ctx, dev, text, ctm, scissor); } fz_catch(ctx) { dev->error_depth = 1; strcpy(dev->errmess, fz_caught_message(ctx)); /* Error swallowed */ } }
void fz_clip_stroke_text(fz_device *dev, fz_text *text, fz_stroke_state *stroke, const fz_matrix *ctm) { fz_context *ctx = dev->ctx; if (dev->error_depth) { dev->error_depth++; return; } fz_try(ctx) { if (dev->hints & FZ_MAINTAIN_CONTAINER_STACK) { fz_rect bbox; fz_bound_text(ctx, text, stroke, ctm, &bbox); push_clip_stack(dev, &bbox, fz_device_container_stack_is_clip_stroke_text); } if (dev->clip_stroke_text) dev->clip_stroke_text(dev, text, stroke, ctm); } fz_catch(ctx) { dev->error_depth = 1; strcpy(dev->errmess, fz_caught_message(ctx)); /* Error swallowed */ } }
void fz_clip_path(fz_device *dev, fz_path *path, const fz_rect *rect, int even_odd, const fz_matrix *ctm) { fz_context *ctx = dev->ctx; if (dev->error_depth) { dev->error_depth++; return; } fz_try(ctx) { if (dev->hints & FZ_MAINTAIN_CONTAINER_STACK) { if (rect == NULL) { fz_rect bbox; fz_bound_path(ctx, path, NULL, ctm, &bbox); push_clip_stack(dev, &bbox, fz_device_container_stack_is_clip_path); } else push_clip_stack(dev, rect, fz_device_container_stack_is_clip_path); } if (dev->clip_path) dev->clip_path(dev, path, rect, even_odd, ctm); } fz_catch(ctx) { dev->error_depth = 1; strcpy(dev->errmess, fz_caught_message(ctx)); /* Error swallowed */ } }
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; }
void pdf_load_type3_glyphs(fz_context *ctx, pdf_document *doc, pdf_font_desc *fontdesc, int nested_depth) { int i; fz_try(ctx) { for (i = 0; i < 256; i++) { if (fontdesc->font->t3procs[i]) { fz_prepare_t3_glyph(ctx, fontdesc->font, i, nested_depth); fontdesc->size += 0; // TODO: display list size calculation } } } fz_catch(ctx) { fz_rethrow_if(ctx, FZ_ERROR_TRYLATER); fz_warn(ctx, "Type3 glyph load failed: %s", fz_caught_message(ctx)); } }
static void do_sign(void) { pdf_pkcs7_signer *signer = NULL; fz_var(signer); fz_try(ctx) { signer = pkcs7_openssl_read_pfx(ctx, cert_filename, cert_password.text); pdf_sign_signature(ctx, pdf, sig_widget, signer); ui_show_warning_dialog("Signed document successfully."); } fz_always(ctx) { if (signer) signer->drop(signer); } fz_catch(ctx) ui_show_warning_dialog("%s", fz_caught_message(ctx)); if (pdf_update_page(ctx, sig_widget->page)) render_page(); }
void fz_clip_stroke_path(fz_device *dev, fz_path *path, const fz_rect *rect, fz_stroke_state *stroke, const fz_matrix *ctm) { fz_context *ctx = dev->ctx; if (dev->error_depth) { dev->error_depth++; return; } fz_try(ctx) { if (dev->clip_stroke_path) dev->clip_stroke_path(dev, path, rect, stroke, ctm); } fz_catch(ctx) { dev->error_depth = 1; strcpy(dev->errmess, fz_caught_message(ctx)); /* Error swallowed */ } }
void fz_begin_group(fz_device *dev, const fz_rect *area, int isolated, int knockout, int blendmode, float alpha) { fz_context *ctx = dev->ctx; if (dev->error_depth) { dev->error_depth++; return; } fz_try(ctx) { if (dev->begin_group) dev->begin_group(dev, area, isolated, knockout, blendmode, alpha); } fz_catch(ctx) { dev->error_depth = 1; strcpy(dev->errmess, fz_caught_message(ctx)); /* Error swallowed */ } }
void fz_clip_image_mask(fz_device *dev, fz_image *image, const fz_rect *rect, const fz_matrix *ctm) { fz_context *ctx = dev->ctx; if (dev->error_depth) { dev->error_depth++; return; } fz_try(ctx) { if (dev->clip_image_mask) dev->clip_image_mask(dev, image, rect, ctm); } fz_catch(ctx) { dev->error_depth = 1; strcpy(dev->errmess, fz_caught_message(ctx)); /* Error swallowed */ } }
void fz_begin_group(fz_context *ctx, fz_device *dev, const fz_rect *area, int isolated, int knockout, int blendmode, float alpha) { if (dev->error_depth) { dev->error_depth++; return; } fz_try(ctx) { if (dev->hints & FZ_MAINTAIN_CONTAINER_STACK) push_clip_stack(ctx, dev, area, fz_device_container_stack_is_group); if (dev->begin_group) dev->begin_group(ctx, dev, area, isolated, knockout, blendmode, alpha); } fz_catch(ctx) { dev->error_depth = 1; strcpy(dev->errmess, fz_caught_message(ctx)); /* Error swallowed */ } }
void fz_begin_mask(fz_context *ctx, fz_device *dev, const fz_rect *area, int luminosity, fz_colorspace *colorspace, const float *bc, const fz_color_params *color_params) { if (dev->error_depth) { dev->error_depth++; return; } fz_try(ctx) { if (dev->hints & FZ_MAINTAIN_CONTAINER_STACK) push_clip_stack(ctx, dev, area, fz_device_container_stack_in_mask); if (dev->begin_mask) dev->begin_mask(ctx, dev, area, luminosity, colorspace, bc, color_params); } fz_catch(ctx) { dev->error_depth = 1; strcpy(dev->errmess, fz_caught_message(ctx)); /* Error swallowed */ } }
void fz_begin_mask(fz_device *dev, const fz_rect *area, int luminosity, fz_colorspace *colorspace, float *bc) { fz_context *ctx = dev->ctx; if (dev->error_depth) { dev->error_depth++; return; } fz_try(ctx) { if (dev->begin_mask) dev->begin_mask(dev, area, luminosity, colorspace, bc); } fz_catch(ctx) { dev->error_depth = 1; strcpy(dev->errmess, fz_caught_message(ctx)); /* Error swallowed */ } }
static void rethrow(pdf_js *js) { js_newerror(js->imp, fz_caught_message(js->ctx)); js_throw(js->imp); }
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; }