static cbz_page * cbz_load_page(fz_context *ctx, cbz_document *doc, int number) { unsigned char *data = NULL; cbz_page *page = NULL; fz_buffer *buf; if (number < 0 || number >= doc->page_count) return NULL; fz_var(data); fz_var(page); buf = fz_read_archive_entry(ctx, doc->zip, doc->page[number]); fz_try(ctx) { page = fz_new_page(ctx, sizeof *page); page->super.bound_page = (fz_page_bound_page_fn *)cbz_bound_page; page->super.run_page_contents = (fz_page_run_page_contents_fn *)cbz_run_page; page->super.drop_page_imp = (fz_page_drop_page_imp_fn *)cbz_drop_page_imp; page->image = fz_new_image_from_buffer(ctx, buf); } fz_always(ctx) { fz_drop_buffer(ctx, buf); } fz_catch(ctx) { fz_free(ctx, data); cbz_drop_page_imp(ctx, page); fz_rethrow(ctx); } return page; }
static void fz_process_mesh_type5(fz_context *ctx, fz_shade *shade, const fz_matrix *ctm, fz_mesh_processor *painter) { fz_stream *stream = fz_open_compressed_buffer(ctx, shade->buffer); fz_vertex *buf = NULL; fz_vertex *ref = NULL; int first; int ncomp = painter->ncomp; int i, k; int vprow = shade->u.m.vprow; int bpcoord = shade->u.m.bpcoord; int bpcomp = shade->u.m.bpcomp; float x0 = shade->u.m.x0; float x1 = shade->u.m.x1; float y0 = shade->u.m.y0; float y1 = shade->u.m.y1; float *c0 = shade->u.m.c0; float *c1 = shade->u.m.c1; float x, y, c[FZ_MAX_COLORS]; fz_var(buf); fz_var(ref); fz_try(ctx) { ref = fz_malloc_array(ctx, vprow, sizeof(fz_vertex)); buf = fz_malloc_array(ctx, vprow, sizeof(fz_vertex)); first = 1; while (!fz_is_eof_bits(ctx, stream)) { for (i = 0; i < vprow; i++) { x = read_sample(ctx, stream, bpcoord, x0, x1); y = read_sample(ctx, stream, bpcoord, y0, y1); for (k = 0; k < ncomp; k++) c[k] = read_sample(ctx, stream, bpcomp, c0[k], c1[k]); fz_prepare_vertex(ctx, painter, &buf[i], ctm, x, y, c); } if (!first) for (i = 0; i < vprow - 1; i++) paint_quad(ctx, painter, &ref[i], &ref[i+1], &buf[i+1], &buf[i]); SWAP(ref,buf); first = 0; } } fz_always(ctx) { fz_free(ctx, ref); fz_free(ctx, buf); fz_drop_stream(ctx, stream); } fz_catch(ctx) { fz_rethrow(ctx); } }
static void fz_mesh_type5_process(fz_context *ctx, fz_shade *shade, const fz_matrix *ctm, fz_mesh_processor *painter) { fz_stream *stream = fz_open_compressed_buffer(ctx, shade->buffer); fz_vertex *buf = NULL; fz_vertex *ref = NULL; int first; int ncomp; int i, k; int vprow = shade->u.m.vprow; int bpcoord = shade->u.m.bpcoord; int bpcomp = shade->u.m.bpcomp; float x0 = shade->u.m.x0; float x1 = shade->u.m.x1; float y0 = shade->u.m.y0; float y1 = shade->u.m.y1; float *c0 = shade->u.m.c0; float *c1 = shade->u.m.c1; fz_var(buf); fz_var(ref); fz_try(ctx) { ref = fz_malloc_array(ctx, vprow, sizeof(fz_vertex)); buf = fz_malloc_array(ctx, vprow, sizeof(fz_vertex)); first = 1; ncomp = (shade->use_function > 0 ? 1 : shade->colorspace->n); while (!fz_is_eof_bits(stream)) { for (i = 0; i < vprow; i++) { buf[i].p.x = read_sample(stream, bpcoord, x0, x1); buf[i].p.y = read_sample(stream, bpcoord, y0, y1); fz_transform_point(&buf[i].p, ctm); for (k = 0; k < ncomp; k++) buf[i].c[k] = read_sample(stream, bpcomp, c0[k], c1[k]); } if (!first) for (i = 0; i < vprow - 1; i++) paint_quad(painter, &ref[i], &ref[i+1], &buf[i+1], &buf[i]); SWAP(ref,buf); first = 0; } } fz_always(ctx) { fz_free(ctx, ref); fz_free(ctx, buf); fz_close(stream); } fz_catch(ctx) { fz_rethrow(ctx); } }
static fz_colorspace * load_indexed(pdf_document *doc, pdf_obj *array) { fz_context *ctx = doc->ctx; pdf_obj *baseobj = pdf_array_get(array, 1); pdf_obj *highobj = pdf_array_get(array, 2); pdf_obj *lookupobj = pdf_array_get(array, 3); fz_colorspace *base = NULL; fz_colorspace *cs; int i, n, high; unsigned char *lookup = NULL; fz_var(base); fz_var(lookup); fz_try(ctx) { base = pdf_load_colorspace(doc, baseobj); high = pdf_to_int(highobj); high = fz_clampi(high, 0, 255); n = base->n * (high + 1); lookup = fz_malloc_array(ctx, 1, n); if (pdf_is_string(lookupobj) && pdf_to_str_len(lookupobj) >= n) { unsigned char *buf = (unsigned char *) pdf_to_str_buf(lookupobj); for (i = 0; i < n; i++) lookup[i] = buf[i]; } else if (pdf_is_indirect(lookupobj)) { fz_stream *file = NULL; fz_var(file); fz_try(ctx) { file = pdf_open_stream(doc, pdf_to_num(lookupobj), pdf_to_gen(lookupobj)); i = fz_read(file, lookup, n); if (i < n) memset(lookup+i, 0, n-i); } fz_always(ctx) { fz_close(file); } fz_catch(ctx) { fz_rethrow_message(ctx, "cannot open colorspace lookup table (%d 0 R)", pdf_to_num(lookupobj)); } } else { fz_rethrow_message(ctx, "cannot parse colorspace lookup table"); } cs = fz_new_indexed_colorspace(ctx, base, high, lookup); }
static void pdf_clean_stream_object(fz_context *ctx, pdf_document *doc, pdf_obj *obj, pdf_obj *orig_res, fz_cookie *cookie, int own_res, pdf_text_filter_fn *text_filter, pdf_after_text_object_fn *after_text, void *arg, int sanitize, int ascii) { pdf_processor *proc_buffer = NULL; pdf_processor *proc_filter = NULL; pdf_obj *res = NULL; pdf_obj *ref; fz_buffer *buffer; if (!obj) return; fz_var(res); fz_var(proc_buffer); fz_var(proc_filter); buffer = fz_new_buffer(ctx, 1024); fz_try(ctx) { if (own_res) { pdf_obj *r = pdf_dict_get(ctx, obj, PDF_NAME(Resources)); if (r) orig_res = r; } res = pdf_new_dict(ctx, doc, 1); proc_buffer = pdf_new_buffer_processor(ctx, buffer, ascii); proc_filter = pdf_new_filter_processor_with_text_filter(ctx, doc, proc_buffer, orig_res, res, text_filter, after_text, arg); pdf_process_contents(ctx, proc_filter, doc, orig_res, obj, cookie); pdf_close_processor(ctx, proc_filter); pdf_close_processor(ctx, proc_buffer); pdf_update_stream(ctx, doc, obj, buffer, 0); if (own_res) { ref = pdf_add_object(ctx, doc, res); pdf_dict_put_drop(ctx, obj, PDF_NAME(Resources), ref); } } fz_always(ctx) { pdf_drop_processor(ctx, proc_filter); pdf_drop_processor(ctx, proc_buffer); fz_drop_buffer(ctx, buffer); pdf_drop_obj(ctx, res); } fz_catch(ctx) { fz_rethrow(ctx); } }
static void pdf_clean_stream_object(fz_context *ctx, pdf_document *doc, pdf_obj *obj, pdf_obj *orig_res, fz_cookie *cookie, int own_res) { pdf_processor *proc_buffer = NULL; pdf_processor *proc_filter = NULL; pdf_obj *res = NULL; pdf_obj *ref = NULL; fz_buffer *buffer; if (!obj) return; fz_var(res); fz_var(ref); fz_var(proc_buffer); fz_var(proc_filter); buffer = fz_new_buffer(ctx, 1024); fz_try(ctx) { if (own_res) { pdf_obj *r = pdf_dict_get(ctx, obj, PDF_NAME_Resources); if (r) orig_res = r; } res = pdf_new_dict(ctx, doc, 1); proc_buffer = pdf_new_buffer_processor(ctx, buffer); proc_filter = pdf_new_filter_processor(ctx, proc_buffer, doc, orig_res, res); pdf_process_contents(ctx, proc_filter, doc, orig_res, obj, cookie); pdf_update_stream(ctx, doc, obj, buffer, 0); if (own_res) { ref = pdf_new_ref(ctx, doc, res); pdf_dict_put(ctx, obj, PDF_NAME_Resources, ref); } } fz_always(ctx) { pdf_drop_processor(ctx, proc_filter); pdf_drop_processor(ctx, proc_buffer); fz_drop_buffer(ctx, buffer); pdf_drop_obj(ctx, res); pdf_drop_obj(ctx, ref); } fz_catch(ctx) { fz_rethrow_message(ctx, "Failed while cleaning xobject"); } }
static fz_outline * pdf_load_outline_imp(pdf_document *xref, pdf_obj *dict) { fz_context *ctx = xref->ctx; fz_outline *node, **prev, *first; pdf_obj *obj; pdf_obj *odict = dict; fz_var(dict); fz_var(first); fz_try(ctx) { first = NULL; prev = &first; while (dict && pdf_is_dict(dict)) { if (pdf_obj_mark(dict)) break; node = fz_malloc_struct(ctx, fz_outline); node->title = NULL; node->dest.kind = FZ_LINK_NONE; node->down = NULL; node->next = NULL; *prev = node; prev = &node->next; obj = pdf_dict_gets(dict, "Title"); if (obj) node->title = pdf_to_utf8(xref, obj); if ((obj = pdf_dict_gets(dict, "Dest"))) node->dest = pdf_parse_link_dest(xref, obj); else if ((obj = pdf_dict_gets(dict, "A"))) node->dest = pdf_parse_action(xref, obj); obj = pdf_dict_gets(dict, "First"); if (obj) node->down = pdf_load_outline_imp(xref, obj); dict = pdf_dict_gets(dict, "Next"); } } fz_always(ctx) { for (dict = odict; dict && pdf_obj_marked(dict); dict = pdf_dict_gets(dict, "Next")) pdf_obj_unmark(dict); } fz_catch(ctx) { fz_free_outline(ctx, first); fz_rethrow(ctx); } return first; }
static void page_merge(int page_from, int page_to, pdf_graft_map *graft_map) { pdf_obj *pageref = NULL; pdf_obj *page_dict; pdf_obj *obj = NULL, *ref = NULL; /* Include minimal number of objects for page. Do not include items that * reference other pages */ pdf_obj *known_page_objs[] = { PDF_NAME_Contents, PDF_NAME_Resources, PDF_NAME_MediaBox, PDF_NAME_CropBox, PDF_NAME_BleedBox, PDF_NAME_TrimBox, PDF_NAME_ArtBox, PDF_NAME_Rotate, PDF_NAME_UserUnit}; int n = nelem(known_page_objs); int i; int num; fz_var(obj); fz_var(ref); fz_try(ctx) { pageref = pdf_lookup_page_obj(ctx, doc_src, page_from - 1); /* Make a new dictionary and copy over the items from the source object to * the new dict that we want to deep copy. */ page_dict = pdf_new_dict(ctx, doc_des, 4); pdf_dict_put_drop(ctx, page_dict, PDF_NAME_Type, PDF_NAME_Page); for (i = 0; i < n; i++) { obj = pdf_dict_get(ctx, pageref, known_page_objs[i]); if (obj != NULL) pdf_dict_put_drop(ctx, page_dict, known_page_objs[i], pdf_graft_object(ctx, doc_des, doc_src, obj, graft_map)); } /* Add the dictionary */ obj = pdf_add_object_drop(ctx, doc_des, page_dict); /* Get indirect ref */ num = pdf_to_num(ctx, obj); ref = pdf_new_indirect(ctx, doc_des, num, 0); /* Insert */ pdf_insert_page(ctx, doc_des, page_to - 1, ref); } fz_always(ctx) { pdf_drop_obj(ctx, obj); pdf_drop_obj(ctx, ref); } fz_catch(ctx) { fz_rethrow(ctx); } }
static fz_colorspace * load_separation(pdf_document *xref, pdf_obj *array) { fz_colorspace *cs; struct separation *sep = NULL; fz_context *ctx = xref->ctx; pdf_obj *nameobj = pdf_array_get(array, 1); pdf_obj *baseobj = pdf_array_get(array, 2); pdf_obj *tintobj = pdf_array_get(array, 3); fz_colorspace *base; pdf_function *tint = NULL; int n; fz_var(tint); fz_var(sep); if (pdf_is_array(nameobj)) n = pdf_array_len(nameobj); else n = 1; if (n > FZ_MAX_COLORS) fz_throw(ctx, "too many components in colorspace"); base = pdf_load_colorspace(xref, baseobj); /* RJW: "cannot load base colorspace (%d %d R)", pdf_to_num(baseobj), pdf_to_gen(baseobj) */ fz_try(ctx) { tint = pdf_load_function(xref, tintobj); /* RJW: fz_drop_colorspace(ctx, base); * "cannot load tint function (%d %d R)", pdf_to_num(tintobj), pdf_to_gen(tintobj) */ sep = fz_malloc_struct(ctx, struct separation); sep->base = base; sep->tint = tint; cs = fz_new_colorspace(ctx, n == 1 ? "Separation" : "DeviceN", n); cs->to_rgb = separation_to_rgb; cs->free_data = free_separation; cs->data = sep; cs->size += sizeof(struct separation) + (base ? base->size : 0) + pdf_function_size(tint); } fz_catch(ctx) { fz_drop_colorspace(ctx, base); pdf_drop_function(ctx, tint); fz_free(ctx, sep); fz_rethrow(ctx); } return cs; }
static int do_name_tree_map(fz_context *ctx, pdf_obj *tree, pdf_name_tree_map_fn *fn, void *arg) { int i; int n = 0; int m = 0; fz_var(n); fz_var(m); if (pdf_mark_obj(ctx, tree)) fz_throw(ctx, FZ_ERROR_GENERIC, "Recursive name tree!"); fz_try(ctx) { pdf_obj *arr = pdf_dict_get(ctx, tree, PDF_NAME_Kids); n = pdf_array_len(ctx, arr); for (i = n; i > 0;) { i--; if (do_name_tree_map(ctx, pdf_array_get(ctx, arr, i), fn, arg)) { pdf_array_delete(ctx, arr, i); n--; } } arr = pdf_dict_get(ctx, tree, PDF_NAME_Names); m = pdf_array_len(ctx, arr); if (m & 1) fz_throw(ctx, FZ_ERROR_GENERIC, "Malformed Names array"); for (i = m; i > 0;) { i -= 2; if (fn(ctx, tree, pdf_array_get(ctx, arr, i), pdf_array_get(ctx, arr, i+1), arg)) { pdf_array_delete(ctx, arr, i+1); pdf_array_delete(ctx, arr, i); m -= 2; } } } fz_always(ctx) pdf_unmark_obj(ctx, tree); fz_catch(ctx) fz_rethrow(ctx); return n == 0 && m == 0; }
/* Image specific methods */ static void res_image_init(fz_context *ctx, pdf_document *doc, pdf_res_table *table) { int len, k; pdf_obj *obj; pdf_obj *type; pdf_obj *res = NULL; fz_image *image = NULL; unsigned char digest[16]; fz_var(obj); fz_var(image); fz_var(res); fz_try(ctx) { table->hash = fz_new_hash_table(ctx, 4096, 16, -1); len = pdf_count_objects(ctx, doc); for (k = 1; k < len; k++) { obj = pdf_load_object(ctx, doc, k, 0); type = pdf_dict_get(ctx, obj, PDF_NAME_Subtype); if (pdf_name_eq(ctx, type, PDF_NAME_Image)) { image = pdf_load_image(ctx, doc, obj); res_image_get_md5(ctx, image, digest); fz_drop_image(ctx, image); image = NULL; /* Don't allow overwrites. */ if (fz_hash_find(ctx, table->hash, digest) == NULL) fz_hash_insert(ctx, table->hash, digest, obj); } else { pdf_drop_obj(ctx, obj); } obj = NULL; } } fz_always(ctx) { fz_drop_image(ctx, image); pdf_drop_obj(ctx, obj); } fz_catch(ctx) { res_table_free(ctx, table); fz_rethrow(ctx); } }
pdf_obj *pdf_new_matrix(fz_context *ctx, pdf_document *doc, const fz_matrix *mtx) { pdf_obj *arr = NULL; pdf_obj *item = NULL; fz_var(arr); fz_var(item); fz_try(ctx) { arr = pdf_new_array(ctx, doc, 6); item = pdf_new_real(ctx, doc, mtx->a); pdf_array_push(ctx, arr, item); pdf_drop_obj(ctx, item); item = NULL; item = pdf_new_real(ctx, doc, mtx->b); pdf_array_push(ctx, arr, item); pdf_drop_obj(ctx, item); item = NULL; item = pdf_new_real(ctx, doc, mtx->c); pdf_array_push(ctx, arr, item); pdf_drop_obj(ctx, item); item = NULL; item = pdf_new_real(ctx, doc, mtx->d); pdf_array_push(ctx, arr, item); pdf_drop_obj(ctx, item); item = NULL; item = pdf_new_real(ctx, doc, mtx->e); pdf_array_push(ctx, arr, item); pdf_drop_obj(ctx, item); item = NULL; item = pdf_new_real(ctx, doc, mtx->f); pdf_array_push(ctx, arr, item); pdf_drop_obj(ctx, item); item = NULL; } fz_catch(ctx) { pdf_drop_obj(ctx, item); pdf_drop_obj(ctx, arr); fz_rethrow(ctx); } return arr; }
pdf_obj *pdf_new_matrix(fz_context *ctx, fz_matrix mtx) { pdf_obj *arr = NULL; pdf_obj *item = NULL; fz_var(arr); fz_var(item); fz_try(ctx) { arr = pdf_new_array(ctx, 6); item = pdf_new_real(ctx, mtx.a); pdf_array_push(arr, item); pdf_drop_obj(item); item = NULL; item = pdf_new_real(ctx, mtx.b); pdf_array_push(arr, item); pdf_drop_obj(item); item = NULL; item = pdf_new_real(ctx, mtx.c); pdf_array_push(arr, item); pdf_drop_obj(item); item = NULL; item = pdf_new_real(ctx, mtx.d); pdf_array_push(arr, item); pdf_drop_obj(item); item = NULL; item = pdf_new_real(ctx, mtx.e); pdf_array_push(arr, item); pdf_drop_obj(item); item = NULL; item = pdf_new_real(ctx, mtx.f); pdf_array_push(arr, item); pdf_drop_obj(item); item = NULL; } fz_catch(ctx) { pdf_drop_obj(item); pdf_drop_obj(arr); fz_rethrow(ctx); } return arr; }
std::shared_ptr<std::vector<std::shared_ptr<RectFloat>>> MuPDFDoc::SearchText(const char* searchText) { fz_text_sheet *sheet = nullptr; fz_text_page *text = nullptr; fz_device *dev = nullptr; PageCache *pageCache = &m_pages[m_currentPage]; fz_var(sheet); fz_var(text); fz_var(dev); std::shared_ptr<std::vector<std::shared_ptr<RectFloat>>> hints(new std::vector<std::shared_ptr<RectFloat>>()); fz_try(m_context) { int hitCount = 0; fz_matrix ctm = CalcConvertMatrix(); fz_rect mbrect = fz_transform_rect(ctm, pageCache->mediaBox); sheet = fz_new_text_sheet(m_context); text = fz_new_text_page(m_context, mbrect); dev = fz_new_text_device(m_context, sheet, text); fz_run_page(m_document, pageCache->page, dev, ctm, nullptr); fz_free_device(dev); dev = nullptr; int len = TextLen(text); for (int pos = 0; pos < len; pos++) { fz_bbox rr = fz_empty_bbox; int n = Match(text, searchText, pos); for (int i = 0; i < n; i++) rr = fz_union_bbox(rr, BBoxCharAt(text, pos + i)); if (!fz_is_empty_bbox(rr) && hitCount < MAX_SEARCH_HITS) { hints->push_back(std::shared_ptr<RectFloat>(new RectFloat(rr.x0, rr.y0, rr.x1, rr.y1))); if (++hitCount >= MAX_SEARCH_HITS) break; } } } fz_always(m_context) { fz_free_text_page(m_context, text); fz_free_text_sheet(m_context, sheet); fz_free_device(dev); } fz_catch(m_context) { return std::shared_ptr<std::vector<std::shared_ptr<RectFloat>>>(nullptr); } return hints; }
/* cf. http://code.google.com/p/sumatrapdf/issues/detail?id=1333 */ static fz_pixmap * decomp_image_banded(fz_context *ctx, fz_stream *stm, fz_image *image, int indexed, int l2factor, int native_l2factor) { fz_pixmap *tile = NULL, *part = NULL; int w = (image->w + (1 << l2factor) - 1) >> l2factor; int h = (image->h + (1 << l2factor) - 1) >> l2factor; int part_h, orig_h = image->h; int band = 1 << fz_maxi(8, l2factor); fz_var(tile); fz_var(part); fz_try(ctx) { fz_colorspace *cs = image->colorspace; if (indexed) cs = *(fz_colorspace **)cs->data; // cf. struct indexed in res_colorspace.c tile = fz_new_pixmap(ctx, cs, w, h); tile->interpolate = image->interpolate; tile->has_alpha = 0; /* SumatraPDF: allow optimizing non-alpha pixmaps */ /* decompress the image in bands of 256 lines */ for (part_h = h; part_h > 0; part_h -= band >> l2factor) { image->h = part_h > band >> l2factor ? band : ((orig_h - 1) % band) + 1; part = fz_decomp_image_from_stream(ctx, fz_keep_stream(stm), image, -1 - indexed, l2factor, native_l2factor); memcpy(tile->samples + (h - part_h) * tile->w * tile->n, part->samples, part->h * part->w * part->n); tile->has_alpha |= part->has_alpha; /* SumatraPDF: allow optimizing non-alpha pixmaps */ fz_drop_pixmap(ctx, part); part = NULL; } /* cf. http://bugs.ghostscript.com/show_bug.cgi?id=693517 */ if (image->usecolorkey && image->mask) fz_unblend_masked_tile(ctx, tile, image); } fz_always(ctx) { image->h = orig_h; fz_close(stm); } fz_catch(ctx) { fz_drop_pixmap(ctx, part); fz_drop_pixmap(ctx, tile); fz_rethrow(ctx); } return tile; }
static epub_chapter * epub_parse_chapter(fz_context *ctx, epub_document *doc, const char *path) { fz_archive *zip = doc->zip; fz_buffer *buf = NULL; epub_chapter *ch; char base_uri[2048]; fz_dirname(base_uri, path, sizeof base_uri); ch = fz_malloc_struct(ctx, epub_chapter); ch->path = NULL; ch->html = NULL; ch->next = NULL; fz_var(buf); fz_try(ctx) { buf = fz_read_archive_entry(ctx, zip, path); ch->path = fz_strdup(ctx, path); ch->html = fz_parse_html(ctx, doc->set, zip, base_uri, buf, fz_user_css(ctx)); } fz_always(ctx) fz_drop_buffer(ctx, buf); fz_catch(ctx) { fz_drop_html(ctx, ch->html); fz_free(ctx, ch->path); fz_free(ctx, ch); fz_rethrow(ctx); } return ch; }
static void update_field_value(pdf_document *doc, pdf_obj *obj, char *text) { fz_context *ctx = doc->ctx; pdf_obj *sobj = NULL; pdf_obj *grp; if (!text) text = ""; /* All fields of the same name should be updated, so * set the value at the head of the group */ grp = find_head_of_field_group(obj); if (grp) obj = grp; fz_var(sobj); fz_try(ctx) { sobj = pdf_new_string(doc, text, strlen(text)); pdf_dict_puts(obj, "V", sobj); } fz_always(ctx) { pdf_drop_obj(sobj); } fz_catch(ctx) { fz_rethrow(ctx); } pdf_field_mark_dirty(doc, obj); }
void fz_write_pcl_bitmap(fz_context *ctx, fz_bitmap *bitmap, char *filename, int append, fz_pcl_options *pcl) { FILE *fp; fz_output *out = NULL; fp = fopen(filename, append ? "ab" : "wb"); if (!fp) { fz_throw(ctx, FZ_ERROR_GENERIC, "cannot open file '%s': %s", filename, strerror(errno)); } fz_var(out); fz_try(ctx) { out = fz_new_output_with_file(ctx, fp); fz_output_pcl_bitmap(out, bitmap, pcl); } fz_always(ctx) { fz_close_output(out); fclose(fp); } fz_catch(ctx) { fz_rethrow(ctx); } }
fz_pixmap * fz_load_jxr(fz_context *ctx, unsigned char *data, size_t size) { struct info info = { 0 }; fz_pixmap *image = NULL; fz_var(image); jxr_read_image(ctx, data, size, &info, 0); image = fz_new_pixmap(ctx, info.cspace, info.width, info.height, NULL, 1); image->xres = info.xres; image->yres = info.yres; fz_try(ctx) { fz_unpack_tile(ctx, image, info.samples, fz_colorspace_n(ctx, info.cspace) + 1, 8, info.stride, 0); if (info.has_alpha && !info.has_premul) fz_premultiply_pixmap(ctx, image); } fz_always(ctx) { fz_free(ctx, info.samples); fz_drop_colorspace(ctx, info.cspace); } fz_catch(ctx) { fz_drop_pixmap(ctx, image); fz_rethrow(ctx); } return image; }
pdf_process * pdf_process_filter(pdf_process *process, fz_context *ctx, pdf_process *underlying, pdf_obj *resources) { pdf_filter_state *p = NULL; fz_var(p); fz_try(ctx) { p = fz_malloc_struct(ctx, pdf_filter_state); p->ctx = ctx; p->process = *underlying; p->gstate = fz_malloc_struct(ctx, filter_gstate); p->resources = resources; p->gstate->ctm = fz_identity; p->gstate->current_ctm = fz_identity; } fz_catch(ctx) { fz_free(ctx, p); pdf_process_op(NULL, PDF_OP_END, underlying); fz_rethrow(ctx); } process->state = p; process->processor = &pdf_processor_filter; return process; }
int fz_begin_tile_id(fz_context *ctx, fz_device *dev, const fz_rect *area, const fz_rect *view, float xstep, float ystep, const fz_matrix *ctm, int id) { int ret = 0; if (dev->error_depth) { dev->error_depth++; return 0; } if (xstep < 0) xstep = -xstep; if (ystep < 0) ystep = -ystep; fz_var(ret); fz_try(ctx) { if (dev->begin_tile) ret = dev->begin_tile(ctx, 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; }
pdf_jsimp_obj *pdf_jsimp_new_obj(pdf_jsimp *imp, pdf_jsimp_type *type, void *natobj) { fz_context *ctx = imp->ctx; pdf_jsimp_obj *obj = fz_malloc_struct(ctx, pdf_jsimp_obj); priv_data *pdata = NULL; fz_var(pdata); fz_try(ctx) { pdata = fz_malloc_struct(ctx, priv_data); pdata->type = type; pdata->natobj = natobj; obj->ref = JSObjectMake(imp->jscore_ctx, imp->class_ref, pdata); if (obj->ref == NULL) fz_throw(ctx, FZ_ERROR_GENERIC, "JSObjectMake failed"); JSValueProtect(imp->jscore_ctx, obj->ref); } fz_catch(ctx) { fz_free(ctx, pdata); fz_free(ctx, obj); fz_rethrow(ctx); } return obj; }
int main(int argc, char *argv[]) { int saved = 0; if (argc != 3) { printf("Usage: extr file.pdf pageno\n"); exit(1); } char *filename = strdup(argv[1]); char *dir = dirname(strdup(filename)); int pageno = atoi(argv[2]); fz_context *ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "extr: cannot create context\n"); exit(1); } fz_var(doc); fz_try(ctx) { doc = pdf_open_document(ctx, filename); saved = save_attachments(pageno, dir); } fz_catch(ctx) { } printf("%d\n", saved); return 0; }
/* Create a new output object with the given internal state and function pointers. state: Internal state (opaque to everything but implementation). write: Function to output a given buffer. close: Cleanup function to destroy state when output closed. May permissibly be null. */ fz_output * fz_new_output(fz_context *ctx, int bufsiz, void *state, fz_output_write_fn *write, fz_output_close_fn *close, fz_output_drop_fn *drop) { fz_output *out = NULL; fz_var(out); fz_try(ctx) { out = fz_malloc_struct(ctx, fz_output); out->state = state; out->write = write; out->close = close; out->drop = drop; if (bufsiz > 0) { out->bp = fz_malloc(ctx, bufsiz); out->wp = out->bp; out->ep = out->bp + bufsiz; } } fz_catch(ctx) { if (drop) drop(ctx, state); fz_free(ctx, out); fz_rethrow(ctx); } return out; }
static fz_stream * pdf_open_object_array(fz_context *ctx, pdf_document *doc, pdf_obj *list) { fz_stream *stm; int i, n; n = pdf_array_len(ctx, list); stm = fz_open_concat(ctx, n, 1); fz_var(i); /* Workaround Mac compiler bug */ for (i = 0; i < n; i++) { pdf_obj *obj = pdf_array_get(ctx, list, i); fz_try(ctx) { fz_concat_push(ctx, stm, pdf_open_stream(ctx, doc, pdf_to_num(ctx, obj), pdf_to_gen(ctx, obj))); } fz_catch(ctx) { fz_rethrow_if(ctx, FZ_ERROR_TRYLATER); fz_warn(ctx, "cannot load content stream part %d/%d", i + 1, n); continue; } } return stm; }
static fz_jbig2_globals * pdf_load_jbig2_globals(fz_context *ctx, pdf_document *doc, pdf_obj *dict) { fz_jbig2_globals *globals; fz_buffer *buf = NULL; fz_var(buf); if ((globals = pdf_find_item(ctx, fz_drop_jbig2_globals_imp, dict)) != NULL) { return globals; } fz_try(ctx) { buf = pdf_load_stream(ctx, doc, pdf_to_num(ctx, dict), pdf_to_gen(ctx, dict)); globals = fz_load_jbig2_globals(ctx, buf->data, buf->len); pdf_store_item(ctx, dict, globals, buf->len); } fz_always(ctx) { fz_drop_buffer(ctx, buf); } fz_catch(ctx) { fz_rethrow(ctx); } return globals; }
static void pwg_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev) { fz_pwg_writer *wri = (fz_pwg_writer*)wri_; fz_bitmap *bitmap = NULL; fz_var(bitmap); fz_try(ctx) { fz_close_device(ctx, dev); if (wri->mono) { bitmap = fz_new_bitmap_from_pixmap(ctx, wri->pixmap, NULL); fz_write_bitmap_as_pwg_page(ctx, wri->out, bitmap, &wri->pwg); } else { fz_write_pixmap_as_pwg_page(ctx, wri->out, wri->pixmap, &wri->pwg); } } fz_always(ctx) { fz_drop_device(ctx, dev); fz_drop_bitmap(ctx, bitmap); fz_drop_pixmap(ctx, wri->pixmap); wri->pixmap = NULL; } fz_catch(ctx) { fz_rethrow(ctx); } }
fz_stream * fz_open_resized_dctd(fz_stream *chain, int color_transform, int factor) { fz_context *ctx = chain->ctx; fz_dctd *state = NULL; fz_var(state); fz_try(ctx) { state = fz_malloc_struct(chain->ctx, fz_dctd); state->ctx = ctx; state->chain = chain; state->color_transform = color_transform; state->init = 0; state->factor = factor; } fz_catch(ctx) { fz_free(ctx, state); fz_close(chain); fz_rethrow(ctx); } return fz_new_stream(ctx, state, read_dctd, close_dctd); }
/* Default: color_transform = -1 (unset), l2factor = 0, jpegtables = NULL */ fz_stream * fz_open_dctd(fz_stream *chain, int color_transform, int l2factor, fz_stream *jpegtables) { fz_context *ctx = chain->ctx; fz_dctd *state = NULL; fz_var(state); fz_try(ctx) { state = fz_malloc_struct(chain->ctx, fz_dctd); state->ctx = ctx; state->chain = chain; state->jpegtables = jpegtables; state->curr_stm = chain; state->color_transform = color_transform; state->init = 0; state->l2factor = l2factor; } fz_catch(ctx) { fz_free(ctx, state); fz_close(chain); fz_close(jpegtables); fz_rethrow(ctx); } return fz_new_stream(ctx, state, read_dctd, close_dctd); }
/* Ensure we have an incremental xref section where we can store updated versions of indirect objects */ static void ensure_incremental_xref(pdf_document *doc) { fz_context *ctx = doc->ctx; if (!doc->xref_altered) { pdf_xref *xref = &doc->xref_sections[0]; pdf_xref *pxref; pdf_xref_entry *new_table = fz_calloc(ctx, xref->len, sizeof(pdf_xref_entry)); pdf_obj *trailer = NULL; fz_var(trailer); fz_try(ctx) { trailer = pdf_copy_dict(xref->trailer); doc->xref_sections = fz_resize_array(ctx, doc->xref_sections, doc->num_xref_sections + 1, sizeof(pdf_xref)); xref = &doc->xref_sections[0]; pxref = &doc->xref_sections[1]; memmove(pxref, xref, doc->num_xref_sections * sizeof(pdf_xref)); /* xref->len is already correct */ xref->table = new_table; xref->trailer = trailer; doc->num_xref_sections++; doc->xref_altered = 1; } fz_catch(ctx) { fz_free(ctx, new_table); pdf_drop_obj(trailer); fz_rethrow(ctx); } }