diff options
Diffstat (limited to 'xps')
-rw-r--r-- | xps/muxps.h | 103 | ||||
-rw-r--r-- | xps/xps_common.c | 66 | ||||
-rw-r--r-- | xps/xps_doc.c | 230 | ||||
-rw-r--r-- | xps/xps_glyphs.c | 109 | ||||
-rw-r--r-- | xps/xps_gradient.c | 68 | ||||
-rw-r--r-- | xps/xps_image.c | 62 | ||||
-rw-r--r-- | xps/xps_jpeg.c | 21 | ||||
-rw-r--r-- | xps/xps_outline.c | 36 | ||||
-rw-r--r-- | xps/xps_path.c | 202 | ||||
-rw-r--r-- | xps/xps_png.c | 109 | ||||
-rw-r--r-- | xps/xps_resource.c | 58 | ||||
-rw-r--r-- | xps/xps_tiff.c | 115 | ||||
-rw-r--r-- | xps/xps_tile.c | 114 | ||||
-rw-r--r-- | xps/xps_xml.c | 36 | ||||
-rw-r--r-- | xps/xps_zip.c | 341 |
15 files changed, 845 insertions, 825 deletions
diff --git a/xps/muxps.h b/xps/muxps.h index 172a8535..18ca528e 100644 --- a/xps/muxps.h +++ b/xps/muxps.h @@ -11,7 +11,7 @@ typedef unsigned char byte; * XPS and ZIP constants. */ -typedef struct xps_context_s xps_context; +typedef struct xps_document_s xps_document; #define REL_START_PART \ "http://schemas.microsoft.com/xps/2005/06/fixedrepresentation" @@ -40,12 +40,12 @@ void xps_absolute_path(char *output, char *base_uri, char *path, int output_size typedef struct element xml_element; -xml_element *xml_parse_document(byte *buf, int len); +xml_element *xml_parse_document(fz_context *doc, byte *buf, int len); xml_element *xml_next(xml_element *item); xml_element *xml_down(xml_element *item); char *xml_tag(xml_element *item); char *xml_att(xml_element *item, const char *att); -void xml_free_element(xml_element *item); +void xml_free_element(fz_context *doc, xml_element *item); void xml_print_element(xml_element *item, int level); /* @@ -62,23 +62,23 @@ struct xps_part_s byte *data; }; -xps_part *xps_new_part(xps_context *ctx, char *name, int size); -xps_part *xps_read_part(xps_context *ctx, char *partname); -void xps_free_part(xps_context *ctx, xps_part *part); +xps_part *xps_new_part(xps_document *doc, char *name, int size); +xps_part *xps_read_part(xps_document *doc, char *partname); +void xps_free_part(xps_document *doc, xps_part *part); /* * Document structure. */ -typedef struct xps_document_s xps_document; +typedef struct xps_fixdoc_s xps_fixdoc; typedef struct xps_page_s xps_page; typedef struct xps_target_s xps_target; -struct xps_document_s +struct xps_fixdoc_s { char *name; char *outline; - xps_document *next; + xps_fixdoc *next; }; struct xps_page_s @@ -98,25 +98,25 @@ struct xps_target_s xps_target *next; }; -int xps_read_page_list(xps_context *ctx); -void xps_debug_page_list(xps_context *ctx); -void xps_free_page_list(xps_context *ctx); +int xps_read_page_list(xps_document *doc); +void xps_debug_page_list(xps_document *doc); +void xps_free_page_list(xps_document *doc); -int xps_count_pages(xps_context *ctx); -int xps_load_page(xps_page **page, xps_context *ctx, int number); -void xps_free_page(xps_context *ctx, xps_page *page); +int xps_count_pages(xps_document *doc); +xps_page *xps_load_page(xps_document *doc, int number); +void xps_free_page(xps_document *doc, xps_page *page); -fz_outline *xps_load_outline(xps_context *ctx); +fz_outline *xps_load_outline(xps_document *doc); -int xps_find_link_target(xps_context *ctx, char *target_uri); +int xps_find_link_target(xps_document *doc, char *target_uri); /* * Images, fonts, and colorspaces. */ -int xps_decode_jpeg(fz_pixmap **imagep, byte *rbuf, int rlen); -int xps_decode_png(fz_pixmap **imagep, byte *rbuf, int rlen); -int xps_decode_tiff(fz_pixmap **imagep, byte *rbuf, int rlen); +fz_pixmap *xps_decode_jpeg(fz_context *doc, byte *rbuf, int rlen); +fz_pixmap *xps_decode_png(fz_context *doc, byte *rbuf, int rlen); +fz_pixmap *xps_decode_tiff(fz_context *doc, byte *rbuf, int rlen); typedef struct xps_font_cache_s xps_font_cache; @@ -139,12 +139,12 @@ void xps_identify_font_encoding(fz_font *font, int idx, int *pid, int *eid); void xps_select_font_encoding(fz_font *font, int idx); int xps_encode_font_char(fz_font *font, int key); -void xps_measure_font_glyph(xps_context *ctx, fz_font *font, int gid, xps_glyph_metrics *mtx); +void xps_measure_font_glyph(xps_document *doc, fz_font *font, int gid, xps_glyph_metrics *mtx); -void xps_debug_path(xps_context *ctx); +void xps_debug_path(xps_document *doc); -void xps_parse_color(xps_context *ctx, char *base_uri, char *hexstring, fz_colorspace **csp, float *samples); -void xps_set_color(xps_context *ctx, fz_colorspace *colorspace, float *samples); +void xps_parse_color(xps_document *doc, char *base_uri, char *hexstring, fz_colorspace **csp, float *samples); +void xps_set_color(xps_document *doc, fz_colorspace *colorspace, float *samples); /* * Resource dictionaries. @@ -162,9 +162,9 @@ struct xps_resource_s xps_resource *parent; /* up to the previous dict in the stack */ }; -int xps_parse_resource_dictionary(xps_context *ctx, xps_resource **dictp, char *base_uri, xml_element *root); -void xps_free_resource_dictionary(xps_context *ctx, xps_resource *dict); -void xps_resolve_resource_reference(xps_context *ctx, xps_resource *dict, char **attp, xml_element **tagp, char **urip); +int xps_parse_resource_dictionary(xps_document *doc, xps_resource **dictp, char *base_uri, xml_element *root); +void xps_free_resource_dictionary(xps_document *doc, xps_resource *dict); +void xps_resolve_resource_reference(xps_document *doc, xps_resource *dict, char **attp, xml_element **tagp, char **urip); void xps_debug_resource_dictionary(xps_resource *dict); @@ -172,29 +172,29 @@ void xps_debug_resource_dictionary(xps_resource *dict); * Fixed page/graphics parsing. */ -void xps_parse_fixed_page(xps_context *ctx, fz_matrix ctm, xps_page *page); -void xps_parse_canvas(xps_context *ctx, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *node); -void xps_parse_path(xps_context *ctx, fz_matrix ctm, char *base_uri, xps_resource *dict, xml_element *node); -void xps_parse_glyphs(xps_context *ctx, fz_matrix ctm, char *base_uri, xps_resource *dict, xml_element *node); -void xps_parse_solid_color_brush(xps_context *ctx, fz_matrix ctm, char *base_uri, xps_resource *dict, xml_element *node); -void xps_parse_image_brush(xps_context *ctx, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *node); -void xps_parse_visual_brush(xps_context *ctx, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *node); -void xps_parse_linear_gradient_brush(xps_context *ctx, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *node); -void xps_parse_radial_gradient_brush(xps_context *ctx, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *node); +void xps_parse_fixed_page(xps_document *doc, fz_matrix ctm, xps_page *page); +void xps_parse_canvas(xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *node); +void xps_parse_path(xps_document *doc, fz_matrix ctm, char *base_uri, xps_resource *dict, xml_element *node); +void xps_parse_glyphs(xps_document *doc, fz_matrix ctm, char *base_uri, xps_resource *dict, xml_element *node); +void xps_parse_solid_color_brush(xps_document *doc, fz_matrix ctm, char *base_uri, xps_resource *dict, xml_element *node); +void xps_parse_image_brush(xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *node); +void xps_parse_visual_brush(xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *node); +void xps_parse_linear_gradient_brush(xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *node); +void xps_parse_radial_gradient_brush(xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *node); -void xps_parse_tiling_brush(xps_context *ctx, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *root, void(*func)(xps_context*, fz_matrix, fz_rect, char*, xps_resource*, xml_element*, void*), void *user); +void xps_parse_tiling_brush(xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *root, void(*func)(xps_document*, fz_matrix, fz_rect, char*, xps_resource*, xml_element*, void*), void *user); -void xps_parse_matrix_transform(xps_context *ctx, xml_element *root, fz_matrix *matrix); -void xps_parse_render_transform(xps_context *ctx, char *text, fz_matrix *matrix); -void xps_parse_rectangle(xps_context *ctx, char *text, fz_rect *rect); +void xps_parse_matrix_transform(xps_document *doc, xml_element *root, fz_matrix *matrix); +void xps_parse_render_transform(xps_document *doc, char *text, fz_matrix *matrix); +void xps_parse_rectangle(xps_document *doc, char *text, fz_rect *rect); -void xps_begin_opacity(xps_context *ctx, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, char *opacity_att, xml_element *opacity_mask_tag); -void xps_end_opacity(xps_context *ctx, char *base_uri, xps_resource *dict, char *opacity_att, xml_element *opacity_mask_tag); +void xps_begin_opacity(xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, char *opacity_att, xml_element *opacity_mask_tag); +void xps_end_opacity(xps_document *doc, char *base_uri, xps_resource *dict, char *opacity_att, xml_element *opacity_mask_tag); -void xps_parse_brush(xps_context *ctx, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *node); -void xps_parse_element(xps_context *ctx, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *node); +void xps_parse_brush(xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *node); +void xps_parse_element(xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *node); -void xps_clip(xps_context *ctx, fz_matrix ctm, xps_resource *dict, char *clip_att, xml_element *clip_tag); +void xps_clip(xps_document *doc, fz_matrix ctm, xps_resource *dict, char *clip_att, xml_element *clip_tag); /* * The interpreter context. @@ -210,16 +210,17 @@ struct xps_entry_s int usize; }; -struct xps_context_s +struct xps_document_s { + fz_context *ctx; char *directory; fz_stream *file; int zip_count; xps_entry *zip_table; char *start_part; /* fixed document sequence */ - xps_document *first_fixdoc; /* first fixed document */ - xps_document *last_fixdoc; /* last fixed document */ + xps_fixdoc *first_fixdoc; /* first fixed document */ + xps_fixdoc *last_fixdoc; /* last fixed document */ xps_page *first_page; /* first page of document */ xps_page *last_page; /* last page of document */ int page_count; @@ -245,8 +246,8 @@ struct xps_context_s fz_device *dev; }; -int xps_open_file(xps_context **ctxp, char *filename); -int xps_open_stream(xps_context **ctxp, fz_stream *file); -void xps_free_context(xps_context *ctx); +xps_document *xps_open_file(fz_context *ctx, char *filename); +xps_document *xps_open_stream(fz_stream *file); +void xps_free_context(xps_document *doc); #endif diff --git a/xps/xps_common.c b/xps/xps_common.c index 894a4941..745d1be1 100644 --- a/xps/xps_common.c +++ b/xps/xps_common.c @@ -10,35 +10,35 @@ static inline int unhex(int a) } void -xps_parse_brush(xps_context *ctx, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *node) +xps_parse_brush(xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *node) { /* SolidColorBrushes are handled in a special case and will never show up here */ if (!strcmp(xml_tag(node), "ImageBrush")) - xps_parse_image_brush(ctx, ctm, area, base_uri, dict, node); + xps_parse_image_brush(doc, ctm, area, base_uri, dict, node); else if (!strcmp(xml_tag(node), "VisualBrush")) - xps_parse_visual_brush(ctx, ctm, area, base_uri, dict, node); + xps_parse_visual_brush(doc, ctm, area, base_uri, dict, node); else if (!strcmp(xml_tag(node), "LinearGradientBrush")) - xps_parse_linear_gradient_brush(ctx, ctm, area, base_uri, dict, node); + xps_parse_linear_gradient_brush(doc, ctm, area, base_uri, dict, node); else if (!strcmp(xml_tag(node), "RadialGradientBrush")) - xps_parse_radial_gradient_brush(ctx, ctm, area, base_uri, dict, node); + xps_parse_radial_gradient_brush(doc, ctm, area, base_uri, dict, node); else - fz_warn("unknown brush tag: %s", xml_tag(node)); + fz_warn(doc->ctx, "unknown brush tag: %s", xml_tag(node)); } void -xps_parse_element(xps_context *ctx, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *node) +xps_parse_element(xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *node) { if (!strcmp(xml_tag(node), "Path")) - xps_parse_path(ctx, ctm, base_uri, dict, node); + xps_parse_path(doc, ctm, base_uri, dict, node); if (!strcmp(xml_tag(node), "Glyphs")) - xps_parse_glyphs(ctx, ctm, base_uri, dict, node); + xps_parse_glyphs(doc, ctm, base_uri, dict, node); if (!strcmp(xml_tag(node), "Canvas")) - xps_parse_canvas(ctx, ctm, area, base_uri, dict, node); + xps_parse_canvas(doc, ctm, area, base_uri, dict, node); /* skip unknown tags (like Foo.Resources and similar) */ } void -xps_begin_opacity(xps_context *ctx, fz_matrix ctm, fz_rect area, +xps_begin_opacity(xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, char *opacity_att, xml_element *opacity_mask_tag) { @@ -61,45 +61,45 @@ xps_begin_opacity(xps_context *ctx, fz_matrix ctm, fz_rect area, { fz_colorspace *colorspace; float samples[32]; - xps_parse_color(ctx, base_uri, scb_color_att, &colorspace, samples); + xps_parse_color(doc, base_uri, scb_color_att, &colorspace, samples); opacity = opacity * samples[0]; } opacity_mask_tag = NULL; } - if (ctx->opacity_top + 1 < nelem(ctx->opacity)) + if (doc->opacity_top + 1 < nelem(doc->opacity)) { - ctx->opacity[ctx->opacity_top + 1] = ctx->opacity[ctx->opacity_top] * opacity; - ctx->opacity_top++; + doc->opacity[doc->opacity_top + 1] = doc->opacity[doc->opacity_top] * opacity; + doc->opacity_top++; } if (opacity_mask_tag) { - fz_begin_mask(ctx->dev, area, 0, NULL, NULL); - xps_parse_brush(ctx, ctm, area, base_uri, dict, opacity_mask_tag); - fz_end_mask(ctx->dev); + fz_begin_mask(doc->dev, area, 0, NULL, NULL); + xps_parse_brush(doc, ctm, area, base_uri, dict, opacity_mask_tag); + fz_end_mask(doc->dev); } } void -xps_end_opacity(xps_context *ctx, char *base_uri, xps_resource *dict, +xps_end_opacity(xps_document *doc, char *base_uri, xps_resource *dict, char *opacity_att, xml_element *opacity_mask_tag) { if (!opacity_att && !opacity_mask_tag) return; - if (ctx->opacity_top > 0) - ctx->opacity_top--; + if (doc->opacity_top > 0) + doc->opacity_top--; if (opacity_mask_tag) { if (strcmp(xml_tag(opacity_mask_tag), "SolidColorBrush")) - fz_pop_clip(ctx->dev); + fz_pop_clip(doc->dev); } } void -xps_parse_render_transform(xps_context *ctx, char *transform, fz_matrix *matrix) +xps_parse_render_transform(xps_document *doc, char *transform, fz_matrix *matrix) { float args[6]; char *s = transform; @@ -124,7 +124,7 @@ xps_parse_render_transform(xps_context *ctx, char *transform, fz_matrix *matrix) } void -xps_parse_matrix_transform(xps_context *ctx, xml_element *root, fz_matrix *matrix) +xps_parse_matrix_transform(xps_document *doc, xml_element *root, fz_matrix *matrix) { char *transform; @@ -134,12 +134,12 @@ xps_parse_matrix_transform(xps_context *ctx, xml_element *root, fz_matrix *matri { transform = xml_att(root, "Matrix"); if (transform) - xps_parse_render_transform(ctx, transform, matrix); + xps_parse_render_transform(doc, transform, matrix); } } void -xps_parse_rectangle(xps_context *ctx, char *text, fz_rect *rect) +xps_parse_rectangle(xps_document *doc, char *text, fz_rect *rect) { float args[4]; char *s = text; @@ -176,7 +176,7 @@ static int count_commas(char *s) } void -xps_parse_color(xps_context *ctx, char *base_uri, char *string, +xps_parse_color(xps_document *doc, char *base_uri, char *string, fz_colorspace **csp, float *samples) { char *p; @@ -230,7 +230,7 @@ xps_parse_color(xps_context *ctx, char *base_uri, char *string, profile = strchr(buf, ' '); if (!profile) { - fz_warn("cannot find icc profile uri in '%s'", string); + fz_warn(doc->ctx, "cannot find icc profile uri in '%s'", string); return; } @@ -238,7 +238,7 @@ xps_parse_color(xps_context *ctx, char *base_uri, char *string, p = strchr(profile, ' '); if (!p) { - fz_warn("cannot find component values in '%s'", profile); + fz_warn(doc->ctx, "cannot find component values in '%s'", profile); return; } @@ -272,11 +272,11 @@ xps_parse_color(xps_context *ctx, char *base_uri, char *string, } void -xps_set_color(xps_context *ctx, fz_colorspace *colorspace, float *samples) +xps_set_color(xps_document *doc, fz_colorspace *colorspace, float *samples) { int i; - ctx->colorspace = colorspace; + doc->colorspace = colorspace; for (i = 0; i < colorspace->n; i++) - ctx->color[i] = samples[i + 1]; - ctx->alpha = samples[0] * ctx->opacity[ctx->opacity_top]; + doc->color[i] = samples[i + 1]; + doc->alpha = samples[0] * doc->opacity[doc->opacity_top]; } diff --git a/xps/xps_doc.c b/xps/xps_doc.c index e0ab7d61..6b6c0c08 100644 --- a/xps/xps_doc.c +++ b/xps/xps_doc.c @@ -21,13 +21,13 @@ xps_rels_for_part(char *buf, char *name, int buflen) */ void -xps_debug_page_list(xps_context *ctx) +xps_debug_page_list(xps_document *doc) { - xps_document *fixdoc = ctx->first_fixdoc; - xps_page *page = ctx->first_page; + xps_fixdoc *fixdoc = doc->first_fixdoc; + xps_page *page = doc->first_page; - if (ctx->start_part) - printf("start part %s\n", ctx->start_part); + if (doc->start_part) + printf("start part %s\n", doc->start_part); while (fixdoc) { @@ -43,135 +43,135 @@ xps_debug_page_list(xps_context *ctx) } static void -xps_add_fixed_document(xps_context *ctx, char *name) +xps_add_fixed_document(xps_document *doc, char *name) { - xps_document *fixdoc; + xps_fixdoc *fixdoc; /* Check for duplicates first */ - for (fixdoc = ctx->first_fixdoc; fixdoc; fixdoc = fixdoc->next) + for (fixdoc = doc->first_fixdoc; fixdoc; fixdoc = fixdoc->next) if (!strcmp(fixdoc->name, name)) return; - fixdoc = fz_malloc(sizeof(xps_document)); - fixdoc->name = fz_strdup(name); + fixdoc = fz_malloc(doc->ctx, sizeof(xps_fixdoc)); + fixdoc->name = fz_strdup(doc->ctx, name); fixdoc->outline = NULL; fixdoc->next = NULL; - if (!ctx->first_fixdoc) + if (!doc->first_fixdoc) { - ctx->first_fixdoc = fixdoc; - ctx->last_fixdoc = fixdoc; + doc->first_fixdoc = fixdoc; + doc->last_fixdoc = fixdoc; } else { - ctx->last_fixdoc->next = fixdoc; - ctx->last_fixdoc = fixdoc; + doc->last_fixdoc->next = fixdoc; + doc->last_fixdoc = fixdoc; } } static void -xps_add_fixed_page(xps_context *ctx, char *name, int width, int height) +xps_add_fixed_page(xps_document *doc, char *name, int width, int height) { xps_page *page; /* Check for duplicates first */ - for (page = ctx->first_page; page; page = page->next) + for (page = doc->first_page; page; page = page->next) if (!strcmp(page->name, name)) return; - page = fz_malloc(sizeof(xps_page)); - page->name = fz_strdup(name); - page->number = ctx->page_count++; + page = fz_malloc(doc->ctx, sizeof(xps_page)); + page->name = fz_strdup(doc->ctx, name); + page->number = doc->page_count++; page->width = width; page->height = height; page->root = NULL; page->next = NULL; - if (!ctx->first_page) + if (!doc->first_page) { - ctx->first_page = page; - ctx->last_page = page; + doc->first_page = page; + doc->last_page = page; } else { - ctx->last_page->next = page; - ctx->last_page = page; + doc->last_page->next = page; + doc->last_page = page; } } static void -xps_add_link_target(xps_context *ctx, char *name) +xps_add_link_target(xps_document *doc, char *name) { - xps_page *page = ctx->last_page; - xps_target *target = fz_malloc(sizeof *target); - target->name = fz_strdup(name); + xps_page *page = doc->last_page; + xps_target *target = fz_malloc(doc->ctx, sizeof *target); + target->name = fz_strdup(doc->ctx, name); target->page = page->number; - target->next = ctx->target; - ctx->target = target; + target->next = doc->target; + doc->target = target; } int -xps_find_link_target(xps_context *ctx, char *target_uri) +xps_find_link_target(xps_document *doc, char *target_uri) { xps_target *target; char *needle = strrchr(target_uri, '#'); needle = needle ? needle + 1 : target_uri; - for (target = ctx->target; target; target = target->next) + for (target = doc->target; target; target = target->next) if (!strcmp(target->name, needle)) return target->page; return 0; } static void -xps_free_link_targets(xps_context *ctx) +xps_free_link_targets(xps_document *doc) { - xps_target *target = ctx->target, *next; + xps_target *target = doc->target, *next; while (target) { next = target->next; - fz_free(target->name); - fz_free(target); + fz_free(doc->ctx, target->name); + fz_free(doc->ctx, target); target = next; } } static void -xps_free_fixed_pages(xps_context *ctx) +xps_free_fixed_pages(xps_document *doc) { - xps_page *page = ctx->first_page; + xps_page *page = doc->first_page; while (page) { xps_page *next = page->next; - xps_free_page(ctx, page); - fz_free(page->name); - fz_free(page); + xps_free_page(doc, page); + fz_free(doc->ctx, page->name); + fz_free(doc->ctx, page); page = next; } - ctx->first_page = NULL; - ctx->last_page = NULL; + doc->first_page = NULL; + doc->last_page = NULL; } static void -xps_free_fixed_documents(xps_context *ctx) +xps_free_fixed_documents(xps_document *doc) { - xps_document *fixdoc = ctx->first_fixdoc; + xps_fixdoc *fixdoc = doc->first_fixdoc; while (fixdoc) { - xps_document *next = fixdoc->next; - fz_free(fixdoc->name); - fz_free(fixdoc); + xps_fixdoc *next = fixdoc->next; + fz_free(doc->ctx, fixdoc->name); + fz_free(doc->ctx, fixdoc); fixdoc = next; } - ctx->first_fixdoc = NULL; - ctx->last_fixdoc = NULL; + doc->first_fixdoc = NULL; + doc->last_fixdoc = NULL; } void -xps_free_page_list(xps_context *ctx) +xps_free_page_list(xps_document *doc) { - xps_free_fixed_documents(ctx); - xps_free_fixed_pages(ctx); - xps_free_link_targets(ctx); + xps_free_fixed_documents(doc); + xps_free_fixed_pages(doc); + xps_free_link_targets(doc); } /* @@ -179,7 +179,7 @@ xps_free_page_list(xps_context *ctx) */ static void -xps_parse_metadata_imp(xps_context *ctx, xml_element *item, xps_document *fixdoc) +xps_parse_metadata_imp(xps_document *doc, xml_element *item, xps_fixdoc *fixdoc) { while (item) { @@ -190,11 +190,11 @@ xps_parse_metadata_imp(xps_context *ctx, xml_element *item, xps_document *fixdoc if (target && type) { char tgtbuf[1024]; - xps_absolute_path(tgtbuf, ctx->base_uri, target, sizeof tgtbuf); + xps_absolute_path(tgtbuf, doc->base_uri, target, sizeof tgtbuf); if (!strcmp(type, REL_START_PART)) - ctx->start_part = fz_strdup(tgtbuf); + doc->start_part = fz_strdup(doc->ctx, tgtbuf); if (!strcmp(type, REL_DOC_STRUCTURE) && fixdoc) - fixdoc->outline = fz_strdup(tgtbuf); + fixdoc->outline = fz_strdup(doc->ctx, tgtbuf); } } @@ -204,8 +204,8 @@ xps_parse_metadata_imp(xps_context *ctx, xml_element *item, xps_document *fixdoc if (source) { char srcbuf[1024]; - xps_absolute_path(srcbuf, ctx->base_uri, source, sizeof srcbuf); - xps_add_fixed_document(ctx, srcbuf); + xps_absolute_path(srcbuf, doc->base_uri, source, sizeof srcbuf); + xps_add_fixed_document(doc, srcbuf); } } @@ -219,8 +219,8 @@ xps_parse_metadata_imp(xps_context *ctx, xml_element *item, xps_document *fixdoc if (source) { char srcbuf[1024]; - xps_absolute_path(srcbuf, ctx->base_uri, source, sizeof srcbuf); - xps_add_fixed_page(ctx, srcbuf, width, height); + xps_absolute_path(srcbuf, doc->base_uri, source, sizeof srcbuf); + xps_add_fixed_page(doc, srcbuf, width, height); } } @@ -228,17 +228,17 @@ xps_parse_metadata_imp(xps_context *ctx, xml_element *item, xps_document *fixdoc { char *name = xml_att(item, "Name"); if (name) - xps_add_link_target(ctx, name); + xps_add_link_target(doc, name); } - xps_parse_metadata_imp(ctx, xml_down(item), fixdoc); + xps_parse_metadata_imp(doc, xml_down(item), fixdoc); item = xml_next(item); } } static int -xps_parse_metadata(xps_context *ctx, xps_part *part, xps_document *fixdoc) +xps_parse_metadata(xps_document *doc, xps_part *part, xps_fixdoc *fixdoc) { xml_element *root; char buf[1024]; @@ -258,110 +258,110 @@ xps_parse_metadata(xps_context *ctx, xps_part *part, xps_document *fixdoc) if (s) *s = 0; - ctx->base_uri = buf; - ctx->part_uri = part->name; + doc->base_uri = buf; + doc->part_uri = part->name; - root = xml_parse_document(part->data, part->size); + root = xml_parse_document(doc->ctx, part->data, part->size); if (!root) - return fz_rethrow(-1, "cannot parse metadata part '%s'", part->name); + return fz_error_note(-1, "cannot parse metadata part '%s'", part->name); - xps_parse_metadata_imp(ctx, root, fixdoc); + xps_parse_metadata_imp(doc, root, fixdoc); - xml_free_element(root); + xml_free_element(doc->ctx, root); - ctx->base_uri = NULL; - ctx->part_uri = NULL; + doc->base_uri = NULL; + doc->part_uri = NULL; return fz_okay; } static int -xps_read_and_process_metadata_part(xps_context *ctx, char *name, xps_document *fixdoc) +xps_read_and_process_metadata_part(xps_document *doc, char *name, xps_fixdoc *fixdoc) { xps_part *part; int code; - part = xps_read_part(ctx, name); + part = xps_read_part(doc, name); if (!part) - return fz_rethrow(-1, "cannot read zip part '%s'", name); + return fz_error_note(-1, "cannot read zip part '%s'", name); - code = xps_parse_metadata(ctx, part, fixdoc); + code = xps_parse_metadata(doc, part, fixdoc); if (code) - return fz_rethrow(code, "cannot process metadata part '%s'", name); + return fz_error_note(code, "cannot process metadata part '%s'", name); - xps_free_part(ctx, part); + xps_free_part(doc, part); return fz_okay; } int -xps_read_page_list(xps_context *ctx) +xps_read_page_list(xps_document *doc) { - xps_document *fixdoc; + xps_fixdoc *fixdoc; int code; - code = xps_read_and_process_metadata_part(ctx, "/_rels/.rels", NULL); + code = xps_read_and_process_metadata_part(doc, "/_rels/.rels", NULL); if (code) - return fz_rethrow(code, "cannot process root relationship part"); + return fz_error_note(code, "cannot process root relationship part"); - if (!ctx->start_part) - return fz_throw("cannot find fixed document sequence start part"); + if (!doc->start_part) + return fz_error_make("cannot find fixed document sequence start part"); - code = xps_read_and_process_metadata_part(ctx, ctx->start_part, NULL); + code = xps_read_and_process_metadata_part(doc, doc->start_part, NULL); if (code) - return fz_rethrow(code, "cannot process FixedDocumentSequence part"); + return fz_error_note(code, "cannot process FixedDocumentSequence part"); - for (fixdoc = ctx->first_fixdoc; fixdoc; fixdoc = fixdoc->next) + for (fixdoc = doc->first_fixdoc; fixdoc; fixdoc = fixdoc->next) { char relbuf[1024]; xps_rels_for_part(relbuf, fixdoc->name, sizeof relbuf); - code = xps_read_and_process_metadata_part(ctx, relbuf, fixdoc); + code = xps_read_and_process_metadata_part(doc, relbuf, fixdoc); if (code) - fz_catch(code, "cannot process FixedDocument rels part"); + fz_error_handle(code, "cannot process FixedDocument rels part"); - code = xps_read_and_process_metadata_part(ctx, fixdoc->name, fixdoc); + code = xps_read_and_process_metadata_part(doc, fixdoc->name, fixdoc); if (code) - return fz_rethrow(code, "cannot process FixedDocument part"); + return fz_error_note(code, "cannot process FixedDocument part"); } return fz_okay; } int -xps_count_pages(xps_context *ctx) +xps_count_pages(xps_document *doc) { - return ctx->page_count; + return doc->page_count; } static int -xps_load_fixed_page(xps_context *ctx, xps_page *page) +xps_load_fixed_page(xps_document *doc, xps_page *page) { xps_part *part; xml_element *root; char *width_att; char *height_att; - part = xps_read_part(ctx, page->name); + part = xps_read_part(doc, page->name); if (!part) - return fz_rethrow(-1, "cannot read zip part '%s'", page->name); + return fz_error_note(-1, "cannot read zip part '%s'", page->name); - root = xml_parse_document(part->data, part->size); + root = xml_parse_document(doc->ctx, part->data, part->size); if (!root) - return fz_rethrow(-1, "cannot parse xml part '%s'", page->name); + return fz_error_note(-1, "cannot parse xml part '%s'", page->name); - xps_free_part(ctx, part); + xps_free_part(doc, part); if (strcmp(xml_tag(root), "FixedPage")) - return fz_throw("expected FixedPage element (found %s)", xml_tag(root)); + return fz_error_make("expected FixedPage element (found %s)", xml_tag(root)); width_att = xml_att(root, "Width"); if (!width_att) - return fz_throw("FixedPage missing required attribute: Width"); + return fz_error_make("FixedPage missing required attribute: Width"); height_att = xml_att(root, "Height"); if (!height_att) - return fz_throw("FixedPage missing required attribute: Height"); + return fz_error_make("FixedPage missing required attribute: Height"); page->width = atoi(width_att); page->height = atoi(height_att); @@ -370,37 +370,35 @@ xps_load_fixed_page(xps_context *ctx, xps_page *page) return 0; } -int -xps_load_page(xps_page **pagep, xps_context *ctx, int number) +xps_page * +xps_load_page(xps_document *doc, int number) { xps_page *page; - int code; int n = 0; - for (page = ctx->first_page; page; page = page->next) + for (page = doc->first_page; page; page = page->next) { if (n == number) { if (!page->root) { - code = xps_load_fixed_page(ctx, page); - if (code) - return fz_rethrow(code, "cannot load page %d", number + 1); + xps_load_fixed_page(doc, page); + /* RJW: "cannot load page %d", number + 1 */ } - *pagep = page; - return fz_okay; + return page; } n ++; } - return fz_throw("cannot find page %d", number + 1); + fz_throw(doc->ctx, "cannot find page %d", number + 1); + return NULL; /* Stupid MSVC */ } void -xps_free_page(xps_context *ctx, xps_page *page) +xps_free_page(xps_document *doc, xps_page *page) { /* only free the XML contents */ if (page->root) - xml_free_element(page->root); + xml_free_element(doc->ctx, page->root); page->root = NULL; } diff --git a/xps/xps_glyphs.c b/xps/xps_glyphs.c index 4b7a8ceb..56e6e4c6 100644 --- a/xps/xps_glyphs.c +++ b/xps/xps_glyphs.c @@ -53,7 +53,7 @@ xps_encode_font_char(fz_font *font, int code) } void -xps_measure_font_glyph(xps_context *ctx, fz_font *font, int gid, xps_glyph_metrics *mtx) +xps_measure_font_glyph(xps_document *doc, fz_font *font, int gid, xps_glyph_metrics *mtx) { int mask = FT_LOAD_NO_BITMAP | FT_LOAD_NO_HINTING | FT_LOAD_IGNORE_TRANSFORM; FT_Face face = font->ft_face; @@ -69,23 +69,23 @@ xps_measure_font_glyph(xps_context *ctx, fz_font *font, int gid, xps_glyph_metri } static fz_font * -xps_lookup_font(xps_context *ctx, char *name) +xps_lookup_font(xps_document *doc, char *name) { xps_font_cache *cache; - for (cache = ctx->font_table; cache; cache = cache->next) + for (cache = doc->font_table; cache; cache = cache->next) if (!xps_strcasecmp(cache->name, name)) return fz_keep_font(cache->font); return NULL; } static void -xps_insert_font(xps_context *ctx, char *name, fz_font *font) +xps_insert_font(xps_document *doc, char *name, fz_font *font) { - xps_font_cache *cache = fz_malloc(sizeof(xps_font_cache)); - cache->name = fz_strdup(name); + xps_font_cache *cache = fz_malloc(doc->ctx, sizeof(xps_font_cache)); + cache->name = fz_strdup(doc->ctx, name); cache->font = fz_keep_font(font); - cache->next = ctx->font_table; - ctx->font_table = cache; + cache->next = doc->font_table; + doc->font_table = cache; } /* @@ -93,7 +93,7 @@ xps_insert_font(xps_context *ctx, char *name, fz_font *font) * data with the GUID in the fontname. */ static void -xps_deobfuscate_font_resource(xps_context *ctx, xps_part *part) +xps_deobfuscate_font_resource(xps_document *doc, xps_part *part) { byte buf[33]; byte key[16]; @@ -113,7 +113,7 @@ xps_deobfuscate_font_resource(xps_context *ctx, xps_part *part) if (i != 32) { - fz_warn("cannot extract GUID from obfuscated font part name"); + fz_warn(doc->ctx, "cannot extract GUID from obfuscated font part name"); return; } @@ -128,7 +128,7 @@ xps_deobfuscate_font_resource(xps_context *ctx, xps_part *part) } static void -xps_select_best_font_encoding(fz_font *font) +xps_select_best_font_encoding(xps_document *doc, fz_font *font) { static struct { int pid, eid; } xps_cmap_list[] = { @@ -160,7 +160,7 @@ xps_select_best_font_encoding(fz_font *font) } } - fz_warn("cannot find a suitable cmap"); + fz_warn(doc->ctx, "cannot find a suitable cmap"); } /* @@ -251,7 +251,7 @@ xps_parse_glyph_metrics(char *s, float *advance, float *uofs, float *vofs) * Calculate metrics for positioning. */ static fz_text * -xps_parse_glyphs_imp(xps_context *ctx, fz_matrix ctm, +xps_parse_glyphs_imp(xps_document *doc, fz_matrix ctm, fz_font *font, float size, float originx, float originy, int is_sideways, int bidi_level, char *indices, char *unicode) @@ -267,7 +267,7 @@ xps_parse_glyphs_imp(xps_context *ctx, fz_matrix ctm, int un = 0; if (!unicode && !indices) - fz_warn("glyphs element with neither characters nor indices"); + fz_warn(doc->ctx, "glyphs element with neither characters nor indices"); if (us) { @@ -281,7 +281,7 @@ xps_parse_glyphs_imp(xps_context *ctx, fz_matrix ctm, else tm = fz_scale(size, -size); - text = fz_new_text(font, tm, is_sideways); + text = fz_new_text(doc->ctx, font, tm, is_sideways); while ((us && un > 0) || (is && *is)) { @@ -323,7 +323,7 @@ xps_parse_glyphs_imp(xps_context *ctx, fz_matrix ctm, if (glyph_index == -1) glyph_index = xps_encode_font_char(font, char_code); - xps_measure_font_glyph(ctx, font, glyph_index, &mtx); + xps_measure_font_glyph(doc, font, glyph_index, &mtx); if (is_sideways) advance = mtx.vadv * 100; else if (bidi_level & 1) @@ -355,7 +355,7 @@ xps_parse_glyphs_imp(xps_context *ctx, fz_matrix ctm, f = y - v_offset; } - fz_add_text(text, glyph_index, char_code, e, f); + fz_add_text(doc->ctx, text, glyph_index, char_code, e, f); x += advance * 0.01f * size; } @@ -365,11 +365,10 @@ xps_parse_glyphs_imp(xps_context *ctx, fz_matrix ctm, } void -xps_parse_glyphs(xps_context *ctx, fz_matrix ctm, +xps_parse_glyphs(xps_document *doc, fz_matrix ctm, char *base_uri, xps_resource *dict, xml_element *root) { xml_element *node; - int code; char *fill_uri; char *opacity_mask_uri; @@ -446,17 +445,17 @@ xps_parse_glyphs(xps_context *ctx, fz_matrix ctm, fill_uri = base_uri; opacity_mask_uri = base_uri; - xps_resolve_resource_reference(ctx, dict, &transform_att, &transform_tag, NULL); - xps_resolve_resource_reference(ctx, dict, &clip_att, &clip_tag, NULL); - xps_resolve_resource_reference(ctx, dict, &fill_att, &fill_tag, &fill_uri); - xps_resolve_resource_reference(ctx, dict, &opacity_mask_att, &opacity_mask_tag, &opacity_mask_uri); + xps_resolve_resource_reference(doc, dict, &transform_att, &transform_tag, NULL); + xps_resolve_resource_reference(doc, dict, &clip_att, &clip_tag, NULL); + xps_resolve_resource_reference(doc, dict, &fill_att, &fill_tag, &fill_uri); + xps_resolve_resource_reference(doc, dict, &opacity_mask_att, &opacity_mask_tag, &opacity_mask_uri); /* * Check that we have all the necessary information. */ if (!font_size_att || !font_uri_att || !origin_x_att || !origin_y_att) { - fz_warn("missing attributes in glyphs element"); + fz_warn(doc->ctx, "missing attributes in glyphs element"); return; } @@ -481,37 +480,41 @@ xps_parse_glyphs(xps_context *ctx, fz_matrix ctm, *subfont = 0; } - font = xps_lookup_font(ctx, partname); + font = xps_lookup_font(doc, partname); if (!font) { - part = xps_read_part(ctx, partname); + part = xps_read_part(doc, partname); if (!part) { - fz_warn("cannot find font resource part '%s'", partname); + fz_warn(doc->ctx, "cannot find font resource part '%s'", partname); return; } /* deobfuscate if necessary */ if (strstr(part->name, ".odttf")) - xps_deobfuscate_font_resource(ctx, part); + xps_deobfuscate_font_resource(doc, part); if (strstr(part->name, ".ODTTF")) - xps_deobfuscate_font_resource(ctx, part); + xps_deobfuscate_font_resource(doc, part); - code = fz_new_font_from_memory(&font, part->data, part->size, subfontid); - if (code) { - fz_catch(code, "cannot load font resource '%s'", partname); - xps_free_part(ctx, part); + fz_try(doc->ctx) + { + font = fz_new_font_from_memory(doc->ctx, part->data, part->size, subfontid); + } + fz_catch(doc->ctx) + { + fz_error_handle(1, "cannot load font resource '%s'", partname); + xps_free_part(doc, part); return; } - xps_select_best_font_encoding(font); + xps_select_best_font_encoding(doc, font); - xps_insert_font(ctx, part->name, font); + xps_insert_font(doc, part->name, font); /* NOTE: we keep part->data in the font */ font->ft_data = part->data; font->ft_size = part->size; - fz_free(part->name); - fz_free(part); + fz_free(doc->ctx, part->name); + fz_free(doc->ctx, part); } /* @@ -522,24 +525,24 @@ xps_parse_glyphs(xps_context *ctx, fz_matrix ctm, { fz_matrix transform; if (transform_att) - xps_parse_render_transform(ctx, transform_att, &transform); + xps_parse_render_transform(doc, transform_att, &transform); if (transform_tag) - xps_parse_matrix_transform(ctx, transform_tag, &transform); + xps_parse_matrix_transform(doc, transform_tag, &transform); ctm = fz_concat(transform, ctm); } if (clip_att || clip_tag) - xps_clip(ctx, ctm, dict, clip_att, clip_tag); + xps_clip(doc, ctm, dict, clip_att, clip_tag); font_size = fz_atof(font_size_att); - text = xps_parse_glyphs_imp(ctx, ctm, font, font_size, + text = xps_parse_glyphs_imp(doc, ctm, font, font_size, fz_atof(origin_x_att), fz_atof(origin_y_att), is_sideways, bidi_level, indices_att, unicode_att); area = fz_bound_text(text, ctm); - xps_begin_opacity(ctx, ctm, area, opacity_mask_uri, dict, opacity_att, opacity_mask_tag); + xps_begin_opacity(doc, ctm, area, opacity_mask_uri, dict, opacity_att, opacity_mask_tag); /* If it's a solid color brush fill/stroke do a simple fill */ @@ -555,30 +558,30 @@ xps_parse_glyphs(xps_context *ctx, fz_matrix ctm, float samples[32]; fz_colorspace *colorspace; - xps_parse_color(ctx, base_uri, fill_att, &colorspace, samples); + xps_parse_color(doc, base_uri, fill_att, &colorspace, samples); if (fill_opacity_att) samples[0] = fz_atof(fill_opacity_att); - xps_set_color(ctx, colorspace, samples); + xps_set_color(doc, colorspace, samples); - fz_fill_text(ctx->dev, text, ctm, - ctx->colorspace, ctx->color, ctx->alpha); + fz_fill_text(doc->dev, text, ctm, + doc->colorspace, doc->color, doc->alpha); } /* If it's a complex brush, use the charpath as a clip mask */ if (fill_tag) { - fz_clip_text(ctx->dev, text, ctm, 0); - xps_parse_brush(ctx, ctm, area, fill_uri, dict, fill_tag); - fz_pop_clip(ctx->dev); + fz_clip_text(doc->dev, text, ctm, 0); + xps_parse_brush(doc, ctm, area, fill_uri, dict, fill_tag); + fz_pop_clip(doc->dev); } - xps_end_opacity(ctx, opacity_mask_uri, dict, opacity_att, opacity_mask_tag); + xps_end_opacity(doc, opacity_mask_uri, dict, opacity_att, opacity_mask_tag); - fz_free_text(text); + fz_free_text(doc->ctx, text); if (clip_att || clip_tag) - fz_pop_clip(ctx->dev); + fz_pop_clip(doc->dev); - fz_drop_font(font); + fz_drop_font(doc->ctx, font); } diff --git a/xps/xps_gradient.c b/xps/xps_gradient.c index f73d33fe..b13d14ef 100644 --- a/xps/xps_gradient.c +++ b/xps/xps_gradient.c @@ -35,7 +35,7 @@ static inline float lerp(float a, float b, float x) } static int -xps_parse_gradient_stops(xps_context *ctx, char *base_uri, xml_element *node, +xps_parse_gradient_stops(xps_document *doc, char *base_uri, xml_element *node, struct stop *stops, int maxcount) { fz_colorspace *colorspace; @@ -59,9 +59,9 @@ xps_parse_gradient_stops(xps_context *ctx, char *base_uri, xml_element *node, { stops[count].offset = fz_atof(offset); - xps_parse_color(ctx, base_uri, color, &colorspace, sample); + xps_parse_color(doc, base_uri, color, &colorspace, sample); - fz_convert_color(colorspace, sample + 1, fz_device_rgb, rgb); + fz_convert_color(doc->ctx, colorspace, sample + 1, fz_device_rgb, rgb); stops[count].r = rgb[0]; stops[count].g = rgb[1]; @@ -76,7 +76,7 @@ xps_parse_gradient_stops(xps_context *ctx, char *base_uri, xml_element *node, if (count == 0) { - fz_warn("gradient brush has no gradient stops"); + fz_warn(doc->ctx, "gradient brush has no gradient stops"); stops[0].offset = 0; stops[0].r = 0; stops[0].g = 0; @@ -91,7 +91,7 @@ xps_parse_gradient_stops(xps_context *ctx, char *base_uri, xml_element *node, } if (count == maxcount) - fz_warn("gradient brush exceeded maximum number of gradient stops"); + fz_warn(doc->ctx, "gradient brush exceeded maximum number of gradient stops"); /* Postprocess to make sure the range of offsets is 0.0 to 1.0 */ @@ -203,7 +203,7 @@ xps_sample_gradient_stops(fz_shade *shade, struct stop *stops, int count) */ static void -xps_draw_one_radial_gradient(xps_context *ctx, fz_matrix ctm, +xps_draw_one_radial_gradient(xps_document *doc, fz_matrix ctm, struct stop *stops, int count, int extend, float x0, float y0, float r0, @@ -212,7 +212,7 @@ xps_draw_one_radial_gradient(xps_context *ctx, fz_matrix ctm, fz_shade *shade; /* TODO: this (and the stuff in pdf_shade) should move to res_shade.c */ - shade = fz_malloc(sizeof(fz_shade)); + shade = fz_malloc(doc->ctx, sizeof(fz_shade)); shade->refs = 1; shade->colorspace = fz_device_rgb; shade->bbox = fz_infinite_rect; @@ -227,7 +227,7 @@ xps_draw_one_radial_gradient(xps_context *ctx, fz_matrix ctm, shade->mesh_len = 6; shade->mesh_cap = 6; - shade->mesh = fz_calloc(shade->mesh_cap, sizeof(float)); + shade->mesh = fz_malloc_array(doc->ctx, shade->mesh_cap, sizeof(float)); shade->mesh[0] = x0; shade->mesh[1] = y0; shade->mesh[2] = r0; @@ -235,9 +235,9 @@ xps_draw_one_radial_gradient(xps_context *ctx, fz_matrix ctm, shade->mesh[4] = y1; shade->mesh[5] = r1; - fz_fill_shade(ctx->dev, shade, ctm, 1); + fz_fill_shade(doc->dev, shade, ctm, 1); - fz_drop_shade(shade); + fz_drop_shade(doc->ctx, shade); } /* @@ -245,7 +245,7 @@ xps_draw_one_radial_gradient(xps_context *ctx, fz_matrix ctm, */ static void -xps_draw_one_linear_gradient(xps_context *ctx, fz_matrix ctm, +xps_draw_one_linear_gradient(xps_document *doc, fz_matrix ctm, struct stop *stops, int count, int extend, float x0, float y0, float x1, float y1) @@ -253,7 +253,7 @@ xps_draw_one_linear_gradient(xps_context *ctx, fz_matrix ctm, fz_shade *shade; /* TODO: this (and the stuff in pdf_shade) should move to res_shade.c */ - shade = fz_malloc(sizeof(fz_shade)); + shade = fz_malloc(doc->ctx, sizeof(fz_shade)); shade->refs = 1; shade->colorspace = fz_device_rgb; shade->bbox = fz_infinite_rect; @@ -268,7 +268,7 @@ xps_draw_one_linear_gradient(xps_context *ctx, fz_matrix ctm, shade->mesh_len = 6; shade->mesh_cap = 6; - shade->mesh = fz_calloc(shade->mesh_cap, sizeof(float)); + shade->mesh = fz_malloc_array(doc->ctx, shade->mesh_cap, sizeof(float)); shade->mesh[0] = x0; shade->mesh[1] = y0; shade->mesh[2] = 0; @@ -276,9 +276,9 @@ xps_draw_one_linear_gradient(xps_context *ctx, fz_matrix ctm, shade->mesh[4] = y1; shade->mesh[5] = 0; - fz_fill_shade(ctx->dev, shade, ctm, 1); + fz_fill_shade(doc->dev, shade, ctm, 1); - fz_drop_shade(shade); + fz_drop_shade(doc->ctx, shade); } /* @@ -297,7 +297,7 @@ static inline float point_inside_circle(float px, float py, float x, float y, fl } static void -xps_draw_radial_gradient(xps_context *ctx, fz_matrix ctm, +xps_draw_radial_gradient(xps_document *doc, fz_matrix ctm, struct stop *stops, int count, xml_element *root, int spread) { @@ -331,7 +331,7 @@ xps_draw_radial_gradient(xps_context *ctx, fz_matrix ctm, r0 = 0; r1 = xrad; - xps_draw_one_radial_gradient(ctx, ctm, stops, count, 1, x0, y0, r0, x1, y1, r1); + xps_draw_one_radial_gradient(doc, ctm, stops, count, 1, x0, y0, r0, x1, y1, r1); } /* @@ -340,7 +340,7 @@ xps_draw_radial_gradient(xps_context *ctx, fz_matrix ctm, */ static void -xps_draw_linear_gradient(xps_context *ctx, fz_matrix ctm, +xps_draw_linear_gradient(xps_document *doc, fz_matrix ctm, struct stop *stops, int count, xml_element *root, int spread) { @@ -357,7 +357,7 @@ xps_draw_linear_gradient(xps_context *ctx, fz_matrix ctm, if (end_point_att) sscanf(end_point_att, "%g,%g", &x1, &y1); - xps_draw_one_linear_gradient(ctx, ctm, stops, count, 1, x0, y0, x1, y1); + xps_draw_one_linear_gradient(doc, ctm, stops, count, 1, x0, y0, x1, y1); } /* @@ -366,9 +366,9 @@ xps_draw_linear_gradient(xps_context *ctx, fz_matrix ctm, */ static void -xps_parse_gradient_brush(xps_context *ctx, fz_matrix ctm, fz_rect area, +xps_parse_gradient_brush(xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *root, - void (*draw)(xps_context *, fz_matrix, struct stop *, int, xml_element *, int)) + void (*draw)(xps_document *, fz_matrix, struct stop *, int, xml_element *, int)) { xml_element *node; @@ -404,7 +404,7 @@ xps_parse_gradient_brush(xps_context *ctx, fz_matrix ctm, fz_rect area, stop_tag = xml_down(node); } - xps_resolve_resource_reference(ctx, dict, &transform_att, &transform_tag, NULL); + xps_resolve_resource_reference(doc, dict, &transform_att, &transform_tag, NULL); spread_method = SPREAD_PAD; if (spread_att) @@ -419,40 +419,40 @@ xps_parse_gradient_brush(xps_context *ctx, fz_matrix ctm, fz_rect area, transform = fz_identity; if (transform_att) - xps_parse_render_transform(ctx, transform_att, &transform); + xps_parse_render_transform(doc, transform_att, &transform); if (transform_tag) - xps_parse_matrix_transform(ctx, transform_tag, &transform); + xps_parse_matrix_transform(doc, transform_tag, &transform); ctm = fz_concat(transform, ctm); if (!stop_tag) { - fz_warn("missing gradient stops tag"); + fz_warn(doc->ctx, "missing gradient stops tag"); return; } - stop_count = xps_parse_gradient_stops(ctx, base_uri, stop_tag, stop_list, MAX_STOPS); + stop_count = xps_parse_gradient_stops(doc, base_uri, stop_tag, stop_list, MAX_STOPS); if (stop_count == 0) { - fz_warn("no gradient stops found"); + fz_warn(doc->ctx, "no gradient stops found"); return; } - xps_begin_opacity(ctx, ctm, area, base_uri, dict, opacity_att, NULL); + xps_begin_opacity(doc, ctm, area, base_uri, dict, opacity_att, NULL); - draw(ctx, ctm, stop_list, stop_count, root, spread_method); + draw(doc, ctm, stop_list, stop_count, root, spread_method); - xps_end_opacity(ctx, base_uri, dict, opacity_att, NULL); + xps_end_opacity(doc, base_uri, dict, opacity_att, NULL); } void -xps_parse_linear_gradient_brush(xps_context *ctx, fz_matrix ctm, fz_rect area, +xps_parse_linear_gradient_brush(xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *root) { - xps_parse_gradient_brush(ctx, ctm, area, base_uri, dict, root, xps_draw_linear_gradient); + xps_parse_gradient_brush(doc, ctm, area, base_uri, dict, root, xps_draw_linear_gradient); } void -xps_parse_radial_gradient_brush(xps_context *ctx, fz_matrix ctm, fz_rect area, +xps_parse_radial_gradient_brush(xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *root) { - xps_parse_gradient_brush(ctx, ctm, area, base_uri, dict, root, xps_draw_radial_gradient); + xps_parse_gradient_brush(doc, ctm, area, base_uri, dict, root, xps_draw_radial_gradient); } diff --git a/xps/xps_image.c b/xps/xps_image.c index 4a347435..129637d9 100644 --- a/xps/xps_image.c +++ b/xps/xps_image.c @@ -1,44 +1,41 @@ #include "fitz.h" #include "muxps.h" -static int -xps_decode_image(fz_pixmap **imagep, byte *buf, int len) +static fz_pixmap * +xps_decode_image(fz_context *ctx, byte *buf, int len) { - int error; + fz_pixmap *image; if (len < 8) - return fz_throw("unknown image file format"); + fz_throw(ctx, "unknown image file format"); if (buf[0] == 0xff && buf[1] == 0xd8) { - error = xps_decode_jpeg(imagep, buf, len); - if (error) - return fz_rethrow(error, "cannot decode jpeg image"); + image = xps_decode_jpeg(ctx, buf, len); + /* RJW: "cannot decode jpeg image" */ } else if (memcmp(buf, "\211PNG\r\n\032\n", 8) == 0) { - error = xps_decode_png(imagep, buf, len); - if (error) - return fz_rethrow(error, "cannot decode png image"); + image = xps_decode_png(ctx, buf, len); + /* RJW: "cannot decode png image" */ } else if (memcmp(buf, "II", 2) == 0 && buf[2] == 0xBC) { - return fz_throw("JPEG-XR codec is not available"); + fz_throw(ctx, "JPEG-XR codec is not available"); } else if (memcmp(buf, "MM", 2) == 0 || memcmp(buf, "II", 2) == 0) { - error = xps_decode_tiff(imagep, buf, len); - if (error) - return fz_rethrow(error, "cannot decode TIFF image"); + image = xps_decode_tiff(ctx, buf, len); + /* RJW: "cannot decode TIFF image" */ } else - return fz_throw("unknown image file format"); + fz_throw(ctx, "unknown image file format"); - return fz_okay; + return image; } static void -xps_paint_image_brush(xps_context *ctx, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, +xps_paint_image_brush(xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *root, void *vimage) { fz_pixmap *pixmap = vimage; @@ -47,11 +44,11 @@ xps_paint_image_brush(xps_context *ctx, fz_matrix ctm, fz_rect area, char *base_ fz_matrix im = fz_scale(xs, -ys); im.f = ys; ctm = fz_concat(im, ctm); - fz_fill_image(ctx->dev, pixmap, ctm, ctx->opacity[ctx->opacity_top]); + fz_fill_image(doc->dev, pixmap, ctm, doc->opacity[doc->opacity_top]); } static xps_part * -xps_find_image_brush_source_part(xps_context *ctx, char *base_uri, xml_element *root) +xps_find_image_brush_source_part(xps_document *doc, char *base_uri, xml_element *root) { char *image_source_att; char buf[1024]; @@ -97,32 +94,35 @@ xps_find_image_brush_source_part(xps_context *ctx, char *base_uri, xml_element * xps_absolute_path(partname, base_uri, image_name, sizeof partname); - return xps_read_part(ctx, partname); + return xps_read_part(doc, partname); } void -xps_parse_image_brush(xps_context *ctx, fz_matrix ctm, fz_rect area, +xps_parse_image_brush(xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *root) { xps_part *part; fz_pixmap *image; - int code; - part = xps_find_image_brush_source_part(ctx, base_uri, root); + part = xps_find_image_brush_source_part(doc, base_uri, root); if (!part) { - fz_warn("cannot find image source"); + fz_warn(doc->ctx, "cannot find image source"); return; } - code = xps_decode_image(&image, part->data, part->size); - if (code < 0) { - xps_free_part(ctx, part); - fz_catch(-1, "cannot decode image resource"); + fz_try(doc->ctx) + { + image = xps_decode_image(doc->ctx, part->data, part->size); + } + fz_catch(doc->ctx) + { + xps_free_part(doc, part); + fz_error_handle(-1, "cannot decode image resource"); return; } - xps_parse_tiling_brush(ctx, ctm, area, base_uri, dict, root, xps_paint_image_brush, image); + xps_parse_tiling_brush(doc, ctm, area, base_uri, dict, root, xps_paint_image_brush, image); - fz_drop_pixmap(image); - xps_free_part(ctx, part); + fz_drop_pixmap(doc->ctx, image); + xps_free_part(doc, part); } diff --git a/xps/xps_jpeg.c b/xps/xps_jpeg.c index 68bb5eb9..381baffd 100644 --- a/xps/xps_jpeg.c +++ b/xps/xps_jpeg.c @@ -47,8 +47,8 @@ static void skip_input_data(j_decompress_ptr cinfo, long num_bytes) } } -int -xps_decode_jpeg(fz_pixmap **imagep, byte *rbuf, int rlen) +fz_pixmap * +xps_decode_jpeg(fz_context *ctx, byte *rbuf, int rlen) { struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr_jmp err; @@ -63,8 +63,8 @@ xps_decode_jpeg(fz_pixmap **imagep, byte *rbuf, int rlen) if (setjmp(err.env)) { if (image) - fz_drop_pixmap(image); - return fz_throw("jpeg error: %s", err.msg); + fz_drop_pixmap(ctx, image); + fz_throw(ctx, "jpeg error: %s", err.msg); } cinfo.err = jpeg_std_error(&err.super); @@ -92,14 +92,14 @@ xps_decode_jpeg(fz_pixmap **imagep, byte *rbuf, int rlen) else if (cinfo.output_components == 4) colorspace = fz_device_cmyk; else - return fz_throw("bad number of components in jpeg: %d", cinfo.output_components); + fz_throw(ctx, "bad number of components in jpeg: %d", cinfo.output_components); - image = fz_new_pixmap_with_limit(colorspace, cinfo.output_width, cinfo.output_height); + image = fz_new_pixmap_with_limit(ctx, colorspace, cinfo.output_width, cinfo.output_height); if (!image) { jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); - return fz_throw("out of memory"); + fz_throw(ctx, "out of memory"); } if (cinfo.density_unit == 1) @@ -115,7 +115,7 @@ xps_decode_jpeg(fz_pixmap **imagep, byte *rbuf, int rlen) fz_clear_pixmap(image); - row[0] = fz_malloc(cinfo.output_components * cinfo.output_width); + row[0] = fz_malloc(ctx, cinfo.output_components * cinfo.output_width); dp = image->samples; while (cinfo.output_scanline < cinfo.output_height) { @@ -128,11 +128,10 @@ xps_decode_jpeg(fz_pixmap **imagep, byte *rbuf, int rlen) *dp++ = 255; } } - fz_free(row[0]); + fz_free(ctx, row[0]); jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); - *imagep = image; - return fz_okay; + return image; } diff --git a/xps/xps_outline.c b/xps/xps_outline.c index a4ab6c19..48eb100b 100644 --- a/xps/xps_outline.c +++ b/xps/xps_outline.c @@ -16,7 +16,7 @@ xps_find_last_outline_at_level(fz_outline *node, int level, int target_level) } static fz_outline * -xps_parse_document_outline(xps_context *ctx, xml_element *root) +xps_parse_document_outline(xps_document *doc, xml_element *root) { xml_element *node; fz_outline *head = NULL, *entry, *tail; @@ -31,9 +31,9 @@ xps_parse_document_outline(xps_context *ctx, xml_element *root) if (!target || !description) continue; - entry = fz_malloc(sizeof *entry); - entry->title = fz_strdup(description); - entry->page = xps_find_link_target(ctx, target); + entry = fz_malloc(doc->ctx, sizeof *entry); + entry->title = fz_strdup(doc->ctx, description); + entry->page = xps_find_link_target(doc, target); entry->down = NULL; entry->next = NULL; @@ -59,7 +59,7 @@ xps_parse_document_outline(xps_context *ctx, xml_element *root) } static fz_outline * -xps_parse_document_structure(xps_context *ctx, xml_element *root) +xps_parse_document_structure(xps_document *doc, xml_element *root) { xml_element *node; if (!strcmp(xml_tag(root), "DocumentStructure")) @@ -69,48 +69,48 @@ xps_parse_document_structure(xps_context *ctx, xml_element *root) { node = xml_down(node); if (!strcmp(xml_tag(node), "DocumentOutline")) - return xps_parse_document_outline(ctx, node); + return xps_parse_document_outline(doc, node); } } return NULL; } static fz_outline * -xps_load_document_structure(xps_context *ctx, xps_document *fixdoc) +xps_load_document_structure(xps_document *doc, xps_fixdoc *fixdoc) { xps_part *part; xml_element *root; fz_outline *outline; - part = xps_read_part(ctx, fixdoc->outline); + part = xps_read_part(doc, fixdoc->outline); if (!part) return NULL; - root = xml_parse_document(part->data, part->size); + root = xml_parse_document(doc->ctx, part->data, part->size); if (!root) { - fz_catch(-1, "cannot parse document structure part '%s'", part->name); - xps_free_part(ctx, part); + fz_error_handle(-1, "cannot parse document structure part '%s'", part->name); + xps_free_part(doc, part); return NULL; } - outline = xps_parse_document_structure(ctx, root); + outline = xps_parse_document_structure(doc, root); - xml_free_element(root); - xps_free_part(ctx, part); + xml_free_element(doc->ctx, root); + xps_free_part(doc, part); return outline; } fz_outline * -xps_load_outline(xps_context *ctx) +xps_load_outline(xps_document *doc) { - xps_document *fixdoc; + xps_fixdoc *fixdoc; fz_outline *head = NULL, *tail, *outline; - for (fixdoc = ctx->first_fixdoc; fixdoc; fixdoc = fixdoc->next) { + for (fixdoc = doc->first_fixdoc; fixdoc; fixdoc = fixdoc->next) { if (fixdoc->outline) { - outline = xps_load_document_structure(ctx, fixdoc); + outline = xps_load_document_structure(doc, fixdoc); if (outline) { if (!head) head = outline; diff --git a/xps/xps_path.c b/xps/xps_path.c index 33526262..ead2c7fa 100644 --- a/xps/xps_path.c +++ b/xps/xps_path.c @@ -41,7 +41,7 @@ fz_currentpoint(fz_path *path) * without transforming the line width. */ static void -xps_draw_arc_segment(fz_path *path, fz_matrix mtx, float th0, float th1, int iscw) +xps_draw_arc_segment(fz_context *doc, fz_path *path, fz_matrix mtx, float th0, float th1, int iscw) { float t, d; fz_point p; @@ -56,18 +56,18 @@ xps_draw_arc_segment(fz_path *path, fz_matrix mtx, float th0, float th1, int isc p.x = cosf(th0); p.y = sinf(th0); p = fz_transform_point(mtx, p); - fz_lineto(path, p.x, p.y); + fz_lineto(doc, path, p.x, p.y); for (t = th0; t < th1; t += d) { p.x = cosf(t); p.y = sinf(t); p = fz_transform_point(mtx, p); - fz_lineto(path, p.x, p.y); + fz_lineto(doc, path, p.x, p.y); } p.x = cosf(th1); p.y = sinf(th1); p = fz_transform_point(mtx, p); - fz_lineto(path, p.x, p.y); + fz_lineto(doc, path, p.x, p.y); } else { @@ -75,18 +75,18 @@ xps_draw_arc_segment(fz_path *path, fz_matrix mtx, float th0, float th1, int isc p.x = cosf(th0); p.y = sinf(th0); p = fz_transform_point(mtx, p); - fz_lineto(path, p.x, p.y); + fz_lineto(doc, path, p.x, p.y); for (t = th0; t > th1; t -= d) { p.x = cosf(t); p.y = sinf(t); p = fz_transform_point(mtx, p); - fz_lineto(path, p.x, p.y); + fz_lineto(doc, path, p.x, p.y); } p.x = cosf(th1); p.y = sinf(th1); p = fz_transform_point(mtx, p); - fz_lineto(path, p.x, p.y); + fz_lineto(doc, path, p.x, p.y); } } @@ -107,7 +107,7 @@ angle_between(const fz_point u, const fz_point v) } static void -xps_draw_arc(fz_path *path, +xps_draw_arc(fz_context *doc, fz_path *path, float size_x, float size_y, float rotation_angle, int is_large_arc, int is_clockwise, float point_x, float point_y) @@ -147,7 +147,7 @@ xps_draw_arc(fz_path *path, ry = fabsf(ry); if (rx < 0.001f || ry < 0.001f) { - fz_lineto(path, x2, y2); + fz_lineto(doc, path, x2, y2); return; } @@ -207,9 +207,9 @@ xps_draw_arc(fz_path *path, mtx = fz_concat(fz_translate(cx, cy), mtx); mtx = fz_concat(fz_rotate(rotation_angle), mtx); mtx = fz_concat(fz_scale(rx, ry), mtx); - xps_draw_arc_segment(path, mtx, th1, th1 + dth, is_clockwise); + xps_draw_arc_segment(doc, path, mtx, th1, th1 + dth, is_clockwise); - fz_lineto(path, point_x, point_y); + fz_lineto(doc, path, point_x, point_y); } /* @@ -219,7 +219,7 @@ xps_draw_arc(fz_path *path, */ static fz_path * -xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule) +xps_parse_abbreviated_geometry(xps_document *doc, char *geom, int *fill_rule) { fz_path *path; char **args; @@ -232,9 +232,9 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule) float smooth_x, smooth_y; /* saved cubic bezier control point for smooth curves */ int reset_smooth; - path = fz_new_path(); + path = fz_new_path(doc->ctx); - args = fz_calloc(strlen(geom) + 1, sizeof(char*)); + args = fz_malloc_array(doc->ctx, strlen(geom) + 1, sizeof(char*)); pargs = args; while (*s) @@ -291,44 +291,44 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule) break; case 'M': - fz_moveto(path, fz_atof(args[i]), fz_atof(args[i+1])); + fz_moveto(doc->ctx, path, fz_atof(args[i]), fz_atof(args[i+1])); i += 2; break; case 'm': pt = fz_currentpoint(path); - fz_moveto(path, pt.x + fz_atof(args[i]), pt.y + fz_atof(args[i+1])); + fz_moveto(doc->ctx, path, pt.x + fz_atof(args[i]), pt.y + fz_atof(args[i+1])); i += 2; break; case 'L': - fz_lineto(path, fz_atof(args[i]), fz_atof(args[i+1])); + fz_lineto(doc->ctx, path, fz_atof(args[i]), fz_atof(args[i+1])); i += 2; break; case 'l': pt = fz_currentpoint(path); - fz_lineto(path, pt.x + fz_atof(args[i]), pt.y + fz_atof(args[i+1])); + fz_lineto(doc->ctx, path, pt.x + fz_atof(args[i]), pt.y + fz_atof(args[i+1])); i += 2; break; case 'H': pt = fz_currentpoint(path); - fz_lineto(path, fz_atof(args[i]), pt.y); + fz_lineto(doc->ctx, path, fz_atof(args[i]), pt.y); i += 1; break; case 'h': pt = fz_currentpoint(path); - fz_lineto(path, pt.x + fz_atof(args[i]), pt.y); + fz_lineto(doc->ctx, path, pt.x + fz_atof(args[i]), pt.y); i += 1; break; case 'V': pt = fz_currentpoint(path); - fz_lineto(path, pt.x, fz_atof(args[i])); + fz_lineto(doc->ctx, path, pt.x, fz_atof(args[i])); i += 1; break; case 'v': pt = fz_currentpoint(path); - fz_lineto(path, pt.x, pt.y + fz_atof(args[i])); + fz_lineto(doc->ctx, path, pt.x, pt.y + fz_atof(args[i])); i += 1; break; @@ -339,7 +339,7 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule) y2 = fz_atof(args[i+3]); x3 = fz_atof(args[i+4]); y3 = fz_atof(args[i+5]); - fz_curveto(path, x1, y1, x2, y2, x3, y3); + fz_curveto(doc->ctx, path, x1, y1, x2, y2, x3, y3); i += 6; reset_smooth = 0; smooth_x = x3 - x2; @@ -354,7 +354,7 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule) y2 = fz_atof(args[i+3]) + pt.y; x3 = fz_atof(args[i+4]) + pt.x; y3 = fz_atof(args[i+5]) + pt.y; - fz_curveto(path, x1, y1, x2, y2, x3, y3); + fz_curveto(doc->ctx, path, x1, y1, x2, y2, x3, y3); i += 6; reset_smooth = 0; smooth_x = x3 - x2; @@ -367,7 +367,7 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule) y1 = fz_atof(args[i+1]); x2 = fz_atof(args[i+2]); y2 = fz_atof(args[i+3]); - fz_curveto(path, pt.x + smooth_x, pt.y + smooth_y, x1, y1, x2, y2); + fz_curveto(doc->ctx, path, pt.x + smooth_x, pt.y + smooth_y, x1, y1, x2, y2); i += 4; reset_smooth = 0; smooth_x = x2 - x1; @@ -380,7 +380,7 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule) y1 = fz_atof(args[i+1]) + pt.y; x2 = fz_atof(args[i+2]) + pt.x; y2 = fz_atof(args[i+3]) + pt.y; - fz_curveto(path, pt.x + smooth_x, pt.y + smooth_y, x1, y1, x2, y2); + fz_curveto(doc->ctx, path, pt.x + smooth_x, pt.y + smooth_y, x1, y1, x2, y2); i += 4; reset_smooth = 0; smooth_x = x2 - x1; @@ -393,7 +393,7 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule) y1 = fz_atof(args[i+1]); x2 = fz_atof(args[i+2]); y2 = fz_atof(args[i+3]); - fz_curveto(path, + fz_curveto(doc->ctx, path, (pt.x + 2 * x1) / 3, (pt.y + 2 * y1) / 3, (x2 + 2 * x1) / 3, (y2 + 2 * y1) / 3, x2, y2); @@ -405,7 +405,7 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule) y1 = fz_atof(args[i+1]) + pt.y; x2 = fz_atof(args[i+2]) + pt.x; y2 = fz_atof(args[i+3]) + pt.y; - fz_curveto(path, + fz_curveto(doc->ctx, path, (pt.x + 2 * x1) / 3, (pt.y + 2 * y1) / 3, (x2 + 2 * x1) / 3, (y2 + 2 * y1) / 3, x2, y2); @@ -413,7 +413,7 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule) break; case 'A': - xps_draw_arc(path, + xps_draw_arc(doc->ctx, path, fz_atof(args[i+0]), fz_atof(args[i+1]), fz_atof(args[i+2]), atoi(args[i+3]), atoi(args[i+4]), fz_atof(args[i+5]), fz_atof(args[i+6])); @@ -421,7 +421,7 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule) break; case 'a': pt = fz_currentpoint(path); - xps_draw_arc(path, + xps_draw_arc(doc->ctx, path, fz_atof(args[i+0]), fz_atof(args[i+1]), fz_atof(args[i+2]), atoi(args[i+3]), atoi(args[i+4]), fz_atof(args[i+5]) + pt.x, fz_atof(args[i+6]) + pt.y); @@ -430,7 +430,7 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule) case 'Z': case 'z': - fz_closepath(path); + fz_closepath(doc->ctx, path); break; default: @@ -441,12 +441,12 @@ xps_parse_abbreviated_geometry(xps_context *ctx, char *geom, int *fill_rule) old = cmd; } - fz_free(args); + fz_free(doc->ctx, args); return path; } static void -xps_parse_arc_segment(fz_path *path, xml_element *root, int stroking, int *skipped_stroke) +xps_parse_arc_segment(fz_context *doc, fz_path *path, xml_element *root, int stroking, int *skipped_stroke) { /* ArcSegment pretty much follows the SVG algorithm for converting an * arc in endpoint representation to an arc in centerpoint @@ -468,7 +468,7 @@ xps_parse_arc_segment(fz_path *path, xml_element *root, int stroking, int *skipp if (!point_att || !size_att || !rotation_angle_att || !is_large_arc_att || !sweep_direction_att) { - fz_warn("ArcSegment element is missing attributes"); + fz_warn(doc, "ArcSegment element is missing attributes"); return; } @@ -486,15 +486,15 @@ xps_parse_arc_segment(fz_path *path, xml_element *root, int stroking, int *skipp if (stroking && !is_stroked) { - fz_moveto(path, point_x, point_y); + fz_moveto(doc, path, point_x, point_y); return; } - xps_draw_arc(path, size_x, size_y, rotation_angle, is_large_arc, is_clockwise, point_x, point_y); + xps_draw_arc(doc, path, size_x, size_y, rotation_angle, is_large_arc, is_clockwise, point_x, point_y); } static void -xps_parse_poly_quadratic_bezier_segment(fz_path *path, xml_element *root, int stroking, int *skipped_stroke) +xps_parse_poly_quadratic_bezier_segment(fz_context *doc, fz_path *path, xml_element *root, int stroking, int *skipped_stroke) { char *points_att = xml_att(root, "Points"); char *is_stroked_att = xml_att(root, "IsStroked"); @@ -506,7 +506,7 @@ xps_parse_poly_quadratic_bezier_segment(fz_path *path, xml_element *root, int st if (!points_att) { - fz_warn("PolyQuadraticBezierSegment element has no points"); + fz_warn(doc, "PolyQuadraticBezierSegment element has no points"); return; } @@ -528,12 +528,12 @@ xps_parse_poly_quadratic_bezier_segment(fz_path *path, xml_element *root, int st { if (stroking && !is_stroked) { - fz_moveto(path, x[1], y[1]); + fz_moveto(doc, path, x[1], y[1]); } else { pt = fz_currentpoint(path); - fz_curveto(path, + fz_curveto(doc, path, (pt.x + 2 * x[0]) / 3, (pt.y + 2 * y[0]) / 3, (x[1] + 2 * x[0]) / 3, (y[1] + 2 * y[0]) / 3, x[1], y[1]); @@ -544,7 +544,7 @@ xps_parse_poly_quadratic_bezier_segment(fz_path *path, xml_element *root, int st } static void -xps_parse_poly_bezier_segment(fz_path *path, xml_element *root, int stroking, int *skipped_stroke) +xps_parse_poly_bezier_segment(fz_context *doc, fz_path *path, xml_element *root, int stroking, int *skipped_stroke) { char *points_att = xml_att(root, "Points"); char *is_stroked_att = xml_att(root, "IsStroked"); @@ -555,7 +555,7 @@ xps_parse_poly_bezier_segment(fz_path *path, xml_element *root, int stroking, in if (!points_att) { - fz_warn("PolyBezierSegment element has no points"); + fz_warn(doc, "PolyBezierSegment element has no points"); return; } @@ -576,16 +576,16 @@ xps_parse_poly_bezier_segment(fz_path *path, xml_element *root, int stroking, in if (n == 3) { if (stroking && !is_stroked) - fz_moveto(path, x[2], y[2]); + fz_moveto(doc, path, x[2], y[2]); else - fz_curveto(path, x[0], y[0], x[1], y[1], x[2], y[2]); + fz_curveto(doc, path, x[0], y[0], x[1], y[1], x[2], y[2]); n = 0; } } } static void -xps_parse_poly_line_segment(fz_path *path, xml_element *root, int stroking, int *skipped_stroke) +xps_parse_poly_line_segment(fz_context *doc, fz_path *path, xml_element *root, int stroking, int *skipped_stroke) { char *points_att = xml_att(root, "Points"); char *is_stroked_att = xml_att(root, "IsStroked"); @@ -595,7 +595,7 @@ xps_parse_poly_line_segment(fz_path *path, xml_element *root, int stroking, int if (!points_att) { - fz_warn("PolyLineSegment element has no points"); + fz_warn(doc, "PolyLineSegment element has no points"); return; } @@ -611,15 +611,15 @@ xps_parse_poly_line_segment(fz_path *path, xml_element *root, int stroking, int while (*s == ' ') s++; sscanf(s, "%g,%g", &x, &y); if (stroking && !is_stroked) - fz_moveto(path, x, y); + fz_moveto(doc, path, x, y); else - fz_lineto(path, x, y); + fz_lineto(doc, path, x, y); while (*s != ' ' && *s != 0) s++; } } static void -xps_parse_path_figure(fz_path *path, xml_element *root, int stroking) +xps_parse_path_figure(fz_context *doc, fz_path *path, xml_element *root, int stroking) { xml_element *node; @@ -648,31 +648,31 @@ xps_parse_path_figure(fz_path *path, xml_element *root, int stroking) if (!stroking && !is_filled) /* not filled, when filling */ return; - fz_moveto(path, start_x, start_y); + fz_moveto(doc, path, start_x, start_y); for (node = xml_down(root); node; node = xml_next(node)) { if (!strcmp(xml_tag(node), "ArcSegment")) - xps_parse_arc_segment(path, node, stroking, &skipped_stroke); + xps_parse_arc_segment(doc, path, node, stroking, &skipped_stroke); if (!strcmp(xml_tag(node), "PolyBezierSegment")) - xps_parse_poly_bezier_segment(path, node, stroking, &skipped_stroke); + xps_parse_poly_bezier_segment(doc, path, node, stroking, &skipped_stroke); if (!strcmp(xml_tag(node), "PolyLineSegment")) - xps_parse_poly_line_segment(path, node, stroking, &skipped_stroke); + xps_parse_poly_line_segment(doc, path, node, stroking, &skipped_stroke); if (!strcmp(xml_tag(node), "PolyQuadraticBezierSegment")) - xps_parse_poly_quadratic_bezier_segment(path, node, stroking, &skipped_stroke); + xps_parse_poly_quadratic_bezier_segment(doc, path, node, stroking, &skipped_stroke); } if (is_closed) { if (stroking && skipped_stroke) - fz_lineto(path, start_x, start_y); /* we've skipped using fz_moveto... */ + fz_lineto(doc, path, start_x, start_y); /* we've skipped using fz_moveto... */ else - fz_closepath(path); /* no skipped segments, safe to closepath properly */ + fz_closepath(doc, path); /* no skipped segments, safe to closepath properly */ } } fz_path * -xps_parse_path_geometry(xps_context *ctx, xps_resource *dict, xml_element *root, int stroking, int *fill_rule) +xps_parse_path_geometry(xps_document *doc, xps_resource *dict, xml_element *root, int stroking, int *fill_rule) { xml_element *node; @@ -696,8 +696,8 @@ xps_parse_path_geometry(xps_context *ctx, xps_resource *dict, xml_element *root, transform_tag = xml_down(node); } - xps_resolve_resource_reference(ctx, dict, &transform_att, &transform_tag, NULL); - xps_resolve_resource_reference(ctx, dict, &figures_att, &figures_tag, NULL); + xps_resolve_resource_reference(doc, dict, &transform_att, &transform_tag, NULL); + xps_resolve_resource_reference(doc, dict, &figures_att, &figures_tag, NULL); if (fill_rule_att) { @@ -709,22 +709,22 @@ xps_parse_path_geometry(xps_context *ctx, xps_resource *dict, xml_element *root, transform = fz_identity; if (transform_att) - xps_parse_render_transform(ctx, transform_att, &transform); + xps_parse_render_transform(doc, transform_att, &transform); if (transform_tag) - xps_parse_matrix_transform(ctx, transform_tag, &transform); + xps_parse_matrix_transform(doc, transform_tag, &transform); if (figures_att) - path = xps_parse_abbreviated_geometry(ctx, figures_att, fill_rule); + path = xps_parse_abbreviated_geometry(doc, figures_att, fill_rule); else - path = fz_new_path(); + path = fz_new_path(doc->ctx); if (figures_tag) - xps_parse_path_figure(path, figures_tag, stroking); + xps_parse_path_figure(doc->ctx, path, figures_tag, stroking); for (node = xml_down(root); node; node = xml_next(node)) { if (!strcmp(xml_tag(node), "PathFigure")) - xps_parse_path_figure(path, node, stroking); + xps_parse_path_figure(doc->ctx, path, node, stroking); } if (transform_att || transform_tag) @@ -747,19 +747,19 @@ xps_parse_line_cap(char *attr) } void -xps_clip(xps_context *ctx, fz_matrix ctm, xps_resource *dict, char *clip_att, xml_element *clip_tag) +xps_clip(xps_document *doc, fz_matrix ctm, xps_resource *dict, char *clip_att, xml_element *clip_tag) { fz_path *path; int fill_rule = 0; if (clip_att) - path = xps_parse_abbreviated_geometry(ctx, clip_att, &fill_rule); + path = xps_parse_abbreviated_geometry(doc, clip_att, &fill_rule); else if (clip_tag) - path = xps_parse_path_geometry(ctx, dict, clip_tag, 0, &fill_rule); + path = xps_parse_path_geometry(doc, dict, clip_tag, 0, &fill_rule); else - path = fz_new_path(); - fz_clip_path(ctx->dev, path, NULL, fill_rule == 0, ctm); - fz_free_path(path); + path = fz_new_path(doc->ctx); + fz_clip_path(doc->dev, path, NULL, fill_rule == 0, ctm); + fz_free_path(doc->ctx, path); } /* @@ -768,7 +768,7 @@ xps_clip(xps_context *ctx, fz_matrix ctm, xps_resource *dict, char *clip_att, xm */ void -xps_parse_path(xps_context *ctx, fz_matrix ctm, char *base_uri, xps_resource *dict, xml_element *root) +xps_parse_path(xps_document *doc, fz_matrix ctm, char *base_uri, xps_resource *dict, xml_element *root) { xml_element *node; @@ -852,12 +852,12 @@ xps_parse_path(xps_context *ctx, fz_matrix ctm, char *base_uri, xps_resource *di stroke_uri = base_uri; opacity_mask_uri = base_uri; - xps_resolve_resource_reference(ctx, dict, &data_att, &data_tag, NULL); - xps_resolve_resource_reference(ctx, dict, &clip_att, &clip_tag, NULL); - xps_resolve_resource_reference(ctx, dict, &transform_att, &transform_tag, NULL); - xps_resolve_resource_reference(ctx, dict, &fill_att, &fill_tag, &fill_uri); - xps_resolve_resource_reference(ctx, dict, &stroke_att, &stroke_tag, &stroke_uri); - xps_resolve_resource_reference(ctx, dict, &opacity_mask_att, &opacity_mask_tag, &opacity_mask_uri); + xps_resolve_resource_reference(doc, dict, &data_att, &data_tag, NULL); + xps_resolve_resource_reference(doc, dict, &clip_att, &clip_tag, NULL); + xps_resolve_resource_reference(doc, dict, &transform_att, &transform_tag, NULL); + xps_resolve_resource_reference(doc, dict, &fill_att, &fill_tag, &fill_uri); + xps_resolve_resource_reference(doc, dict, &stroke_att, &stroke_tag, &stroke_uri); + xps_resolve_resource_reference(doc, dict, &opacity_mask_att, &opacity_mask_tag, &opacity_mask_uri); /* * Act on the information we have gathered: @@ -921,70 +921,70 @@ xps_parse_path(xps_context *ctx, fz_matrix ctm, char *base_uri, xps_resource *di transform = fz_identity; if (transform_att) - xps_parse_render_transform(ctx, transform_att, &transform); + xps_parse_render_transform(doc, transform_att, &transform); if (transform_tag) - xps_parse_matrix_transform(ctx, transform_tag, &transform); + xps_parse_matrix_transform(doc, transform_tag, &transform); ctm = fz_concat(transform, ctm); if (clip_att || clip_tag) - xps_clip(ctx, ctm, dict, clip_att, clip_tag); + xps_clip(doc, ctm, dict, clip_att, clip_tag); fill_rule = 0; if (data_att) - path = xps_parse_abbreviated_geometry(ctx, data_att, &fill_rule); + path = xps_parse_abbreviated_geometry(doc, data_att, &fill_rule); else if (data_tag) - path = xps_parse_path_geometry(ctx, dict, data_tag, 0, &fill_rule); + path = xps_parse_path_geometry(doc, dict, data_tag, 0, &fill_rule); if (stroke_att || stroke_tag) area = fz_bound_path(path, &stroke, ctm); else area = fz_bound_path(path, NULL, ctm); - xps_begin_opacity(ctx, ctm, area, opacity_mask_uri, dict, opacity_att, opacity_mask_tag); + xps_begin_opacity(doc, ctm, area, opacity_mask_uri, dict, opacity_att, opacity_mask_tag); if (fill_att) { - xps_parse_color(ctx, base_uri, fill_att, &colorspace, samples); + xps_parse_color(doc, base_uri, fill_att, &colorspace, samples); if (fill_opacity_att) samples[0] = fz_atof(fill_opacity_att); - xps_set_color(ctx, colorspace, samples); + xps_set_color(doc, colorspace, samples); - fz_fill_path(ctx->dev, path, fill_rule == 0, ctm, - ctx->colorspace, ctx->color, ctx->alpha); + fz_fill_path(doc->dev, path, fill_rule == 0, ctm, + doc->colorspace, doc->color, doc->alpha); } if (fill_tag) { area = fz_bound_path(path, NULL, ctm); - fz_clip_path(ctx->dev, path, NULL, fill_rule == 0, ctm); - xps_parse_brush(ctx, ctm, area, fill_uri, dict, fill_tag); - fz_pop_clip(ctx->dev); + fz_clip_path(doc->dev, path, NULL, fill_rule == 0, ctm); + xps_parse_brush(doc, ctm, area, fill_uri, dict, fill_tag); + fz_pop_clip(doc->dev); } if (stroke_att) { - xps_parse_color(ctx, base_uri, stroke_att, &colorspace, samples); + xps_parse_color(doc, base_uri, stroke_att, &colorspace, samples); if (stroke_opacity_att) samples[0] = fz_atof(stroke_opacity_att); - xps_set_color(ctx, colorspace, samples); + xps_set_color(doc, colorspace, samples); - fz_stroke_path(ctx->dev, path, &stroke, ctm, - ctx->colorspace, ctx->color, ctx->alpha); + fz_stroke_path(doc->dev, path, &stroke, ctm, + doc->colorspace, doc->color, doc->alpha); } if (stroke_tag) { - fz_clip_stroke_path(ctx->dev, path, NULL, &stroke, ctm); - xps_parse_brush(ctx, ctm, area, stroke_uri, dict, stroke_tag); - fz_pop_clip(ctx->dev); + fz_clip_stroke_path(doc->dev, path, NULL, &stroke, ctm); + xps_parse_brush(doc, ctm, area, stroke_uri, dict, stroke_tag); + fz_pop_clip(doc->dev); } - xps_end_opacity(ctx, opacity_mask_uri, dict, opacity_att, opacity_mask_tag); + xps_end_opacity(doc, opacity_mask_uri, dict, opacity_att, opacity_mask_tag); - fz_free_path(path); + fz_free_path(doc->ctx, path); path = NULL; if (clip_att || clip_tag) - fz_pop_clip(ctx->dev); + fz_pop_clip(doc->dev); } diff --git a/xps/xps_png.c b/xps/xps_png.c index 245dd285..d9e952a7 100644 --- a/xps/xps_png.c +++ b/xps/xps_png.c @@ -5,6 +5,7 @@ struct info { + fz_context *ctx; int width, height, depth, n; int interlace, indexed; int size; @@ -61,12 +62,12 @@ static const unsigned char png_signature[8] = static void *zalloc(void *opaque, unsigned int items, unsigned int size) { - return fz_calloc(items, size); + return fz_malloc_array(opaque, items, size); } static void zfree(void *opaque, void *address) { - fz_free(address); + fz_free(opaque, address); } static inline int paeth(int a, int b, int c) @@ -188,7 +189,7 @@ png_deinterlace(struct info *info, int *passw, int *passh, int *passofs) unsigned char *output; int p, x, y, k; - output = fz_calloc(info->height, stride); + output = fz_malloc_array(info->ctx, info->height, stride); for (p = 0; p < 7; p++) { @@ -214,7 +215,7 @@ png_deinterlace(struct info *info, int *passw, int *passh, int *passofs) } } - fz_free(info->samples); + fz_free(info->ctx, info->samples); info->samples = output; } @@ -224,7 +225,7 @@ png_read_ihdr(struct info *info, unsigned char *p, int size) int color, compression, filter; if (size != 13) - return fz_throw("IHDR chunk is the wrong size"); + return fz_error_make("IHDR chunk is the wrong size"); info->width = getint(p + 0); info->height = getint(p + 4); @@ -236,21 +237,21 @@ png_read_ihdr(struct info *info, unsigned char *p, int size) info->interlace = p[12]; if (info->width <= 0) - return fz_throw("image width must be > 0"); + return fz_error_make("image width must be > 0"); if (info->height <= 0) - return fz_throw("image height must be > 0"); + return fz_error_make("image height must be > 0"); if (info->depth != 1 && info->depth != 2 && info->depth != 4 && info->depth != 8 && info->depth != 16) - return fz_throw("image bit depth must be one of 1, 2, 4, 8, 16"); + return fz_error_make("image bit depth must be one of 1, 2, 4, 8, 16"); if (color == 2 && info->depth < 8) - return fz_throw("illegal bit depth for truecolor"); + return fz_error_make("illegal bit depth for truecolor"); if (color == 3 && info->depth > 8) - return fz_throw("illegal bit depth for indexed"); + return fz_error_make("illegal bit depth for indexed"); if (color == 4 && info->depth < 8) - return fz_throw("illegal bit depth for grayscale with alpha"); + return fz_error_make("illegal bit depth for grayscale with alpha"); if (color == 6 && info->depth < 8) - return fz_throw("illegal bit depth for truecolor with alpha"); + return fz_error_make("illegal bit depth for truecolor with alpha"); info->indexed = 0; if (color == 0) /* gray */ @@ -267,14 +268,14 @@ png_read_ihdr(struct info *info, unsigned char *p, int size) info->n = 1; } else - return fz_throw("unknown color type"); + return fz_error_make("unknown color type"); if (compression != 0) - return fz_throw("unknown compression method"); + return fz_error_make("unknown compression method"); if (filter != 0) - return fz_throw("unknown filter method"); + return fz_error_make("unknown filter method"); if (info->interlace != 0 && info->interlace != 1) - return fz_throw("interlace method not supported"); + return fz_error_make("interlace method not supported"); return fz_okay; } @@ -286,7 +287,7 @@ png_read_plte(struct info *info, unsigned char *p, int size) int i; if (n > 256 || n > (1 << info->depth)) - return fz_throw("too many samples in palette"); + return fz_error_make("too many samples in palette"); for (i = 0; i < n; i++) { @@ -308,14 +309,14 @@ png_read_trns(struct info *info, unsigned char *p, int size) if (info->indexed) { if (size > 256 || size > (1 << info->depth)) - return fz_throw("too many samples in transparency table"); + return fz_error_make("too many samples in transparency table"); for (i = 0; i < size; i++) info->palette[i * 4 + 3] = p[i]; } else { if (size != info->n * 2) - return fz_throw("tRNS chunk is the wrong size"); + return fz_error_make("tRNS chunk is the wrong size"); for (i = 0; i < info->n; i++) info->trns[i] = (p[i * 2] << 8 | p[i * 2 + 1]) & ((1 << info->depth) - 1); } @@ -333,12 +334,12 @@ png_read_idat(struct info *info, unsigned char *p, int size, z_stream *stm) code = inflate(stm, Z_SYNC_FLUSH); if (code != Z_OK && code != Z_STREAM_END) - return fz_throw("zlib error: %s", stm->msg); + return fz_error_make("zlib error: %s", stm->msg); if (stm->avail_in != 0) { if (stm->avail_out == 0) - return fz_throw("ran out of output before input"); - return fz_throw("inflate did not consume buffer (%d remaining)", stm->avail_in); + return fz_error_make("ran out of output before input"); + return fz_error_make("inflate did not consume buffer (%d remaining)", stm->avail_in); } return fz_okay; @@ -348,7 +349,7 @@ static int png_read_phys(struct info *info, unsigned char *p, int size) { if (size != 9) - return fz_throw("pHYs chunk is the wrong size"); + return fz_error_make("pHYs chunk is the wrong size"); if (p[8] == 1) { info->xres = getint(p) * 254 / 10000; @@ -372,7 +373,7 @@ png_read_image(struct info *info, unsigned char *p, int total) /* Read signature */ if (total < 8 + 12 || memcmp(p, png_signature, 8)) - return fz_throw("not a png image (wrong signature)"); + return fz_error_make("not a png image (wrong signature)"); p += 8; total -= 8; @@ -382,16 +383,16 @@ png_read_image(struct info *info, unsigned char *p, int total) size = getint(p); if (size + 12 > total) - return fz_throw("premature end of data in png image"); + return fz_error_make("premature end of data in png image"); if (!memcmp(p + 4, "IHDR", 4)) { code = png_read_ihdr(info, p + 8, size); if (code) - return fz_rethrow(code, "cannot read png header"); + return fz_error_note(code, "cannot read png header"); } else - return fz_throw("png file must start with IHDR chunk"); + return fz_error_make("png file must start with IHDR chunk"); p += size + 12; total -= size + 12; @@ -408,18 +409,18 @@ png_read_image(struct info *info, unsigned char *p, int total) info->size = passofs[7]; } - info->samples = fz_malloc(info->size); + info->samples = fz_malloc(info->ctx, info->size); stm.zalloc = zalloc; stm.zfree = zfree; - stm.opaque = NULL; + stm.opaque = info->ctx; stm.next_out = info->samples; stm.avail_out = info->size; code = inflateInit(&stm); if (code != Z_OK) - return fz_throw("zlib error: %s", stm.msg); + return fz_error_make("zlib error: %s", stm.msg); /* Read remaining chunks until IEND */ @@ -428,34 +429,34 @@ png_read_image(struct info *info, unsigned char *p, int total) size = getint(p); if (size + 12 > total) - return fz_throw("premature end of data in png image"); + return fz_error_make("premature end of data in png image"); if (!memcmp(p + 4, "PLTE", 4)) { code = png_read_plte(info, p + 8, size); if (code) - return fz_rethrow(code, "cannot read png palette"); + return fz_error_note(code, "cannot read png palette"); } if (!memcmp(p + 4, "tRNS", 4)) { code = png_read_trns(info, p + 8, size); if (code) - return fz_rethrow(code, "cannot read png transparency"); + return fz_error_note(code, "cannot read png transparency"); } if (!memcmp(p + 4, "pHYs", 4)) { code = png_read_phys(info, p + 8, size); if (code) - return fz_rethrow(code, "cannot read png resolution"); + return fz_error_note(code, "cannot read png resolution"); } if (!memcmp(p + 4, "IDAT", 4)) { code = png_read_idat(info, p + 8, size, &stm); if (code) - return fz_rethrow(code, "cannot read png image data"); + return fz_error_note(code, "cannot read png image data"); } if (!memcmp(p + 4, "IEND", 4)) @@ -467,7 +468,7 @@ png_read_image(struct info *info, unsigned char *p, int total) code = inflateEnd(&stm); if (code != Z_OK) - return fz_throw("zlib error: %s", stm.msg); + return fz_error_make("zlib error: %s", stm.msg); /* Apply prediction filter and deinterlacing */ @@ -480,9 +481,9 @@ png_read_image(struct info *info, unsigned char *p, int total) } static fz_pixmap * -png_expand_palette(struct info *info, fz_pixmap *src) +png_expand_palette(fz_context *ctx, struct info *info, fz_pixmap *src) { - fz_pixmap *dst = fz_new_pixmap(fz_device_rgb, src->w, src->h); + fz_pixmap *dst = fz_new_pixmap(ctx, fz_device_rgb, src->w, src->h); unsigned char *sp = src->samples; unsigned char *dp = dst->samples; int x, y; @@ -503,7 +504,7 @@ png_expand_palette(struct info *info, fz_pixmap *src) } } - fz_drop_pixmap(src); + fz_drop_pixmap(info->ctx, src); return dst; } @@ -531,18 +532,23 @@ png_mask_transparency(struct info *info, fz_pixmap *dst) } } -int -xps_decode_png(fz_pixmap **imagep, byte *p, int total) +fz_pixmap * +xps_decode_png(fz_context *ctx, byte *p, int total) { fz_pixmap *image; fz_colorspace *colorspace; struct info png; - int code; int stride; - code = png_read_image(&png, p, total); - if (code) - return fz_rethrow(code, "cannot read png image"); + png.ctx = ctx; + fz_try(ctx) + { + png_read_image(&png, p, total); + } + fz_catch(ctx) + { + fz_throw(ctx, "cannot read png image"); + } if (png.n == 3 || png.n == 4) colorspace = fz_device_rgb; @@ -551,11 +557,11 @@ xps_decode_png(fz_pixmap **imagep, byte *p, int total) stride = (png.width * png.n * png.depth + 7) / 8; - image = fz_new_pixmap_with_limit(colorspace, png.width, png.height); + image = fz_new_pixmap_with_limit(ctx, colorspace, png.width, png.height); if (!image) { - fz_free(png.samples); - return fz_throw("out of memory"); + fz_free(png.ctx, png.samples); + fz_throw(ctx, "out of memory"); } image->xres = png.xres; @@ -564,15 +570,14 @@ xps_decode_png(fz_pixmap **imagep, byte *p, int total) fz_unpack_tile(image, png.samples, png.n, png.depth, stride, png.indexed); if (png.indexed) - image = png_expand_palette(&png, image); + image = png_expand_palette(ctx, &png, image); else if (png.transparency) png_mask_transparency(&png, image); if (png.transparency || png.n == 2 || png.n == 4) fz_premultiply_pixmap(image); - fz_free(png.samples); + fz_free(png.ctx, png.samples); - *imagep = image; - return fz_okay; + return image; } diff --git a/xps/xps_resource.c b/xps/xps_resource.c index c96f3619..4a084b29 100644 --- a/xps/xps_resource.c +++ b/xps/xps_resource.c @@ -2,7 +2,7 @@ #include "muxps.h" static xml_element * -xps_find_resource(xps_context *ctx, xps_resource *dict, char *name, char **urip) +xps_find_resource(xps_document *doc, xps_resource *dict, char *name, char **urip) { xps_resource *head, *node; for (head = dict; head; head = head->parent) @@ -21,7 +21,7 @@ xps_find_resource(xps_context *ctx, xps_resource *dict, char *name, char **urip) } static xml_element * -xps_parse_resource_reference(xps_context *ctx, xps_resource *dict, char *att, char **urip) +xps_parse_resource_reference(xps_document *doc, xps_resource *dict, char *att, char **urip) { char name[1024]; char *s; @@ -34,16 +34,16 @@ xps_parse_resource_reference(xps_context *ctx, xps_resource *dict, char *att, ch if (s) *s = 0; - return xps_find_resource(ctx, dict, name, urip); + return xps_find_resource(doc, dict, name, urip); } void -xps_resolve_resource_reference(xps_context *ctx, xps_resource *dict, +xps_resolve_resource_reference(xps_document *doc, xps_resource *dict, char **attp, xml_element **tagp, char **urip) { if (*attp) { - xml_element *rsrc = xps_parse_resource_reference(ctx, dict, *attp, urip); + xml_element *rsrc = xps_parse_resource_reference(doc, dict, *attp, urip); if (rsrc) { *attp = NULL; @@ -53,7 +53,7 @@ xps_resolve_resource_reference(xps_context *ctx, xps_resource *dict, } static int -xps_parse_remote_resource_dictionary(xps_context *ctx, xps_resource **dictp, char *base_uri, char *source_att) +xps_parse_remote_resource_dictionary(xps_document *doc, xps_resource **dictp, char *base_uri, char *source_att) { char part_name[1024]; char part_uri[1024]; @@ -65,24 +65,24 @@ xps_parse_remote_resource_dictionary(xps_context *ctx, xps_resource **dictp, cha /* External resource dictionaries MUST NOT reference other resource dictionaries */ xps_absolute_path(part_name, base_uri, source_att, sizeof part_name); - part = xps_read_part(ctx, part_name); + part = xps_read_part(doc, part_name); if (!part) { - return fz_throw("cannot find remote resource part '%s'", part_name); + return fz_error_make("cannot find remote resource part '%s'", part_name); } - xml = xml_parse_document(part->data, part->size); + xml = xml_parse_document(doc->ctx, part->data, part->size); if (!xml) { - xps_free_part(ctx, part); - return fz_rethrow(-1, "cannot parse xml"); + xps_free_part(doc, part); + return fz_error_note(-1, "cannot parse xml"); } if (strcmp(xml_tag(xml), "ResourceDictionary")) { - xml_free_element(xml); - xps_free_part(ctx, part); - return fz_throw("expected ResourceDictionary element (found %s)", xml_tag(xml)); + xml_free_element(doc->ctx, xml); + xps_free_part(doc, part); + return fz_error_make("expected ResourceDictionary element (found %s)", xml_tag(xml)); } fz_strlcpy(part_uri, part_name, sizeof part_uri); @@ -90,24 +90,24 @@ xps_parse_remote_resource_dictionary(xps_context *ctx, xps_resource **dictp, cha if (s) s[1] = 0; - code = xps_parse_resource_dictionary(ctx, &dict, part_uri, xml); + code = xps_parse_resource_dictionary(doc, &dict, part_uri, xml); if (code) { - xml_free_element(xml); - xps_free_part(ctx, part); - return fz_rethrow(code, "cannot parse remote resource dictionary: %s", part_uri); + xml_free_element(doc->ctx, xml); + xps_free_part(doc, part); + return fz_error_note(code, "cannot parse remote resource dictionary: %s", part_uri); } dict->base_xml = xml; /* pass on ownership */ - xps_free_part(ctx, part); + xps_free_part(doc, part); *dictp = dict; return fz_okay; } int -xps_parse_resource_dictionary(xps_context *ctx, xps_resource **dictp, char *base_uri, xml_element *root) +xps_parse_resource_dictionary(xps_document *doc, xps_resource **dictp, char *base_uri, xml_element *root) { xps_resource *head; xps_resource *entry; @@ -119,9 +119,9 @@ xps_parse_resource_dictionary(xps_context *ctx, xps_resource **dictp, char *base source = xml_att(root, "Source"); if (source) { - code = xps_parse_remote_resource_dictionary(ctx, dictp, base_uri, source); + code = xps_parse_remote_resource_dictionary(doc, dictp, base_uri, source); if (code) - return fz_rethrow(code, "cannot parse remote resource dictionary"); + return fz_error_note(code, "cannot parse remote resource dictionary"); return fz_okay; } @@ -132,7 +132,7 @@ xps_parse_resource_dictionary(xps_context *ctx, xps_resource **dictp, char *base key = xml_att(node, "x:Key"); if (key) { - entry = fz_malloc(sizeof(xps_resource)); + entry = fz_malloc(doc->ctx, sizeof(xps_resource)); entry->name = key; entry->base_uri = NULL; entry->base_xml = NULL; @@ -144,26 +144,26 @@ xps_parse_resource_dictionary(xps_context *ctx, xps_resource **dictp, char *base } if (head) - head->base_uri = fz_strdup(base_uri); + head->base_uri = fz_strdup(doc->ctx, base_uri); else - return fz_throw("empty resource dictionary"); + return fz_error_make("empty resource dictionary"); *dictp = head; return fz_okay; } void -xps_free_resource_dictionary(xps_context *ctx, xps_resource *dict) +xps_free_resource_dictionary(xps_document *doc, xps_resource *dict) { xps_resource *next; while (dict) { next = dict->next; if (dict->base_xml) - xml_free_element(dict->base_xml); + xml_free_element(doc->ctx, dict->base_xml); if (dict->base_uri) - fz_free(dict->base_uri); - fz_free(dict); + fz_free(doc->ctx, dict->base_uri); + fz_free(doc->ctx, dict); dict = next; } } diff --git a/xps/xps_tiff.c b/xps/xps_tiff.c index 8c58b4d1..b9571465 100644 --- a/xps/xps_tiff.c +++ b/xps/xps_tiff.c @@ -56,6 +56,7 @@ struct tiff fz_colorspace *colorspace; byte *samples; int stride; + fz_context *ctx; }; enum @@ -139,7 +140,7 @@ xps_decode_tiff_uncompressed(struct tiff *tiff, fz_stream *stm, byte *wp, int wl int n = fz_read(stm, wp, wlen); fz_close(stm); if (n < 0) - return fz_rethrow(n, "cannot read uncompressed strip"); + return fz_error_note(n, "cannot read uncompressed strip"); return fz_okay; } @@ -150,7 +151,7 @@ xps_decode_tiff_packbits(struct tiff *tiff, fz_stream *chain, byte *wp, int wlen int n = fz_read(stm, wp, wlen); fz_close(stm); if (n < 0) - return fz_rethrow(n, "cannot read packbits strip"); + return fz_error_note(n, "cannot read packbits strip"); return fz_okay; } @@ -161,7 +162,7 @@ xps_decode_tiff_lzw(struct tiff *tiff, fz_stream *chain, byte *wp, int wlen) int n = fz_read(stm, wp, wlen); fz_close(stm); if (n < 0) - return fz_rethrow(n, "cannot read lzw strip"); + return fz_error_note(n, "cannot read lzw strip"); return fz_okay; } static int @@ -171,7 +172,7 @@ xps_decode_tiff_flate(struct tiff *tiff, fz_stream *chain, byte *wp, int wlen) int n = fz_read(stm, wp, wlen); fz_close(stm); if (n < 0) - return fz_rethrow(n, "cannot read flate strip"); + return fz_error_note(n, "cannot read flate strip"); return fz_okay; } @@ -182,14 +183,15 @@ xps_decode_tiff_fax(struct tiff *tiff, int comp, fz_stream *chain, byte *wp, int fz_obj *params; fz_obj *columns, *rows, *black_is_1, *k, *encoded_byte_align; int n; + fz_context *ctx = tiff->ctx; - columns = fz_new_int(tiff->imagewidth); - rows = fz_new_int(tiff->imagelength); - black_is_1 = fz_new_bool(tiff->photometric == 0); - k = fz_new_int(comp == 4 ? -1 : 0); - encoded_byte_align = fz_new_bool(comp == 2); + columns = fz_new_int(ctx, tiff->imagewidth); + rows = fz_new_int(ctx, tiff->imagelength); + black_is_1 = fz_new_bool(ctx, tiff->photometric == 0); + k = fz_new_int(ctx, comp == 4 ? -1 : 0); + encoded_byte_align = fz_new_bool(ctx, comp == 2); - params = fz_new_dict(5); + params = fz_new_dict(ctx, 5); fz_dict_puts(params, "Columns", columns); fz_dict_puts(params, "Rows", rows); fz_dict_puts(params, "BlackIs1", black_is_1); @@ -208,7 +210,7 @@ xps_decode_tiff_fax(struct tiff *tiff, int comp, fz_stream *chain, byte *wp, int fz_drop_obj(params); if (n < 0) - return fz_rethrow(n, "cannot read fax strip"); + return fz_error_note(n, "cannot read fax strip"); return fz_okay; } @@ -219,7 +221,7 @@ xps_decode_tiff_jpeg(struct tiff *tiff, fz_stream *chain, byte *wp, int wlen) int n = fz_read(stm, wp, wlen); fz_close(stm); if (n < 0) - return fz_rethrow(n, "cannot read jpeg strip"); + return fz_error_note(n, "cannot read jpeg strip"); return fz_okay; } @@ -311,14 +313,14 @@ xps_expand_tiff_colormap(struct tiff *tiff) /* image can be with or without extrasamples: comps is 1 or 2 */ if (tiff->samplesperpixel != 1 && tiff->samplesperpixel != 2) - return fz_throw("invalid number of samples for RGBPal"); + return fz_error_make("invalid number of samples for RGBPal"); if (tiff->bitspersample != 4 && tiff->bitspersample != 8) - return fz_throw("invalid number of bits for RGBPal"); + return fz_error_make("invalid number of bits for RGBPal"); stride = tiff->imagewidth * (tiff->samplesperpixel + 2); - samples = fz_malloc(stride * tiff->imagelength); + samples = fz_malloc(tiff->ctx, stride * tiff->imagelength); for (y = 0; y < tiff->imagelength; y++) { @@ -374,10 +376,10 @@ xps_decode_tiff_strips(struct tiff *tiff) unsigned i; if (!tiff->rowsperstrip || !tiff->stripoffsets || !tiff->rowsperstrip) - return fz_throw("no image data in tiff; maybe it is tiled"); + return fz_error_make("no image data in tiff; maybe it is tiled"); if (tiff->planar != 1) - return fz_throw("image data is not in chunky format"); + return fz_error_make("image data is not in chunky format"); tiff->stride = (tiff->imagewidth * tiff->samplesperpixel * tiff->bitspersample + 7) / 8; @@ -403,7 +405,7 @@ xps_decode_tiff_strips(struct tiff *tiff) tiff->colorspace = fz_device_rgb; break; default: - return fz_throw("unknown photometric: %d", tiff->photometric); + return fz_error_make("unknown photometric: %d", tiff->photometric); } switch (tiff->resolutionunit) @@ -428,7 +430,7 @@ xps_decode_tiff_strips(struct tiff *tiff) tiff->yresolution = 96; } - tiff->samples = fz_calloc(tiff->imagelength, tiff->stride); + tiff->samples = fz_malloc_array(tiff->ctx, tiff->imagelength, tiff->stride); memset(tiff->samples, 0x55, tiff->imagelength * tiff->stride); wp = tiff->samples; @@ -444,7 +446,7 @@ xps_decode_tiff_strips(struct tiff *tiff) wlen = tiff->samples + tiff->stride * tiff->imagelength - wp; if (rp + rlen > tiff->ep) - return fz_throw("strip extends beyond the end of the file"); + return fz_error_make("strip extends beyond the end of the file"); /* the bits are in un-natural order */ if (tiff->fillorder == 2) @@ -452,7 +454,7 @@ xps_decode_tiff_strips(struct tiff *tiff) rp[i] = bitrev[rp[i]]; /* the strip decoders will close this */ - stm = fz_open_memory(rp, rlen); + stm = fz_open_memory(tiff->ctx, rp, rlen); switch (tiff->compression) { @@ -472,7 +474,7 @@ xps_decode_tiff_strips(struct tiff *tiff) error = xps_decode_tiff_lzw(tiff, stm, wp, wlen); break; case 6: - error = fz_throw("deprecated JPEG in TIFF compression not supported"); + error = fz_error_make("deprecated JPEG in TIFF compression not supported"); break; case 7: error = xps_decode_tiff_jpeg(tiff, stm, wp, wlen); @@ -484,11 +486,11 @@ xps_decode_tiff_strips(struct tiff *tiff) error = xps_decode_tiff_packbits(tiff, stm, wp, wlen); break; default: - error = fz_throw("unknown TIFF compression: %d", tiff->compression); + error = fz_error_make("unknown TIFF compression: %d", tiff->compression); } if (error) - return fz_rethrow(error, "cannot decode strip %d", row / tiff->rowsperstrip); + return fz_error_note(error, "cannot decode strip %d", row / tiff->rowsperstrip); /* scramble the bits back into original order */ if (tiff->fillorder == 2) @@ -515,7 +517,7 @@ xps_decode_tiff_strips(struct tiff *tiff) { error = xps_expand_tiff_colormap(tiff); if (error) - return fz_rethrow(error, "cannot expand colormap"); + return fz_error_note(error, "cannot expand colormap"); } /* WhiteIsZero .. invert */ @@ -673,7 +675,7 @@ xps_read_tiff_tag(struct tiff *tiff, unsigned offset) break; case ICCProfile: - tiff->profile = fz_malloc(count); + tiff->profile = fz_malloc(tiff->ctx, count); /* ICC profile data type is set to UNDEFINED. * TBYTE reading not correct in xps_read_tiff_tag_value */ xps_read_tiff_bytes(tiff->profile, tiff, value, count); @@ -681,23 +683,23 @@ xps_read_tiff_tag(struct tiff *tiff, unsigned offset) break; case JPEGTables: - fz_warn("jpeg tables in tiff not implemented"); + fz_warn(tiff->ctx, "jpeg tables in tiff not implemented"); tiff->jpegtables = tiff->bp + value; tiff->jpegtableslen = count; break; case StripOffsets: - tiff->stripoffsets = fz_calloc(count, sizeof(unsigned)); + tiff->stripoffsets = fz_malloc_array(tiff->ctx, count, sizeof(unsigned)); xps_read_tiff_tag_value(tiff->stripoffsets, tiff, type, value, count); break; case StripByteCounts: - tiff->stripbytecounts = fz_calloc(count, sizeof(unsigned)); + tiff->stripbytecounts = fz_malloc_array(tiff->ctx, count, sizeof(unsigned)); xps_read_tiff_tag_value(tiff->stripbytecounts, tiff, type, value, count); break; case ColorMap: - tiff->colormap = fz_calloc(count, sizeof(unsigned)); + tiff->colormap = fz_malloc_array(tiff->ctx, count, sizeof(unsigned)); xps_read_tiff_tag_value(tiff->colormap, tiff, type, value, count); break; @@ -705,7 +707,7 @@ xps_read_tiff_tag(struct tiff *tiff, unsigned offset) case TileLength: case TileOffsets: case TileByteCounts: - return fz_throw("tiled tiffs not supported"); + return fz_error_make("tiled tiffs not supported"); default: /* printf("unknown tag: %d t=%d n=%d\n", tag, type, count); */ @@ -763,12 +765,12 @@ xps_decode_tiff_header(struct tiff *tiff, byte *buf, int len) tiff->order = TII; tiff->order = readshort(tiff); if (tiff->order != TII && tiff->order != TMM) - return fz_throw("not a TIFF file, wrong magic marker"); + return fz_error_make("not a TIFF file, wrong magic marker"); /* check version */ version = readshort(tiff); if (version != 42) - return fz_throw("not a TIFF file, wrong version marker"); + return fz_error_make("not a TIFF file, wrong version marker"); /* get offset of IFD */ offset = readlong(tiff); @@ -786,32 +788,30 @@ xps_decode_tiff_header(struct tiff *tiff, byte *buf, int len) { error = xps_read_tiff_tag(tiff, offset); if (error) - return fz_rethrow(error, "cannot read TIFF header tag"); + return fz_error_note(error, "cannot read TIFF header tag"); offset += 12; } return fz_okay; } -int -xps_decode_tiff(fz_pixmap **imagep, byte *buf, int len) +fz_pixmap * +xps_decode_tiff(fz_context *ctx, byte *buf, int len) { - int error; fz_pixmap *image; struct tiff tiff; - error = xps_decode_tiff_header(&tiff, buf, len); - if (error) - return fz_rethrow(error, "cannot decode tiff header"); + tiff.ctx = ctx; + xps_decode_tiff_header(&tiff, buf, len); + /* RJW: "cannot decode tiff header" */ /* Decode the image strips */ if (tiff.rowsperstrip > tiff.imagelength) tiff.rowsperstrip = tiff.imagelength; - error = xps_decode_tiff_strips(&tiff); - if (error) - return fz_rethrow(error, "cannot decode image data"); + xps_decode_tiff_strips(&tiff); + /* RJW: "cannot decode image data" */ /* Byte swap 16-bit images to big endian if necessary */ if (tiff.bitspersample == 16) @@ -822,14 +822,14 @@ xps_decode_tiff(fz_pixmap **imagep, byte *buf, int len) /* Expand into fz_pixmap struct */ - image = fz_new_pixmap_with_limit(tiff.colorspace, tiff.imagewidth, tiff.imagelength); + image = fz_new_pixmap_with_limit(tiff.ctx, tiff.colorspace, tiff.imagewidth, tiff.imagelength); if (!image) { - if (tiff.colormap) fz_free(tiff.colormap); - if (tiff.stripoffsets) fz_free(tiff.stripoffsets); - if (tiff.stripbytecounts) fz_free(tiff.stripbytecounts); - if (tiff.samples) fz_free(tiff.samples); - return fz_throw("out of memory"); + if (tiff.colormap) fz_free(ctx, tiff.colormap); + if (tiff.stripoffsets) fz_free(ctx, tiff.stripoffsets); + if (tiff.stripbytecounts) fz_free(ctx, tiff.stripbytecounts); + if (tiff.samples) fz_free(ctx, tiff.samples); + fz_throw(ctx, "out of memory"); } image->xres = tiff.xresolution; @@ -843,11 +843,11 @@ xps_decode_tiff(fz_pixmap **imagep, byte *buf, int len) /* CMYK is a subtractive colorspace, we want additive for premul alpha */ if (image->n == 5) { - fz_pixmap *rgb = fz_new_pixmap(fz_device_rgb, image->w, image->h); - fz_convert_pixmap(image, rgb); + fz_pixmap *rgb = fz_new_pixmap(tiff.ctx, fz_device_rgb, image->w, image->h); + fz_convert_pixmap(tiff.ctx, image, rgb); rgb->xres = image->xres; rgb->yres = image->yres; - fz_drop_pixmap(image); + fz_drop_pixmap(ctx, image); image = rgb; } fz_premultiply_pixmap(image); @@ -855,11 +855,10 @@ xps_decode_tiff(fz_pixmap **imagep, byte *buf, int len) /* Clean up scratch memory */ - if (tiff.colormap) fz_free(tiff.colormap); - if (tiff.stripoffsets) fz_free(tiff.stripoffsets); - if (tiff.stripbytecounts) fz_free(tiff.stripbytecounts); - if (tiff.samples) fz_free(tiff.samples); + if (tiff.colormap) fz_free(ctx, tiff.colormap); + if (tiff.stripoffsets) fz_free(ctx, tiff.stripoffsets); + if (tiff.stripbytecounts) fz_free(ctx, tiff.stripbytecounts); + if (tiff.samples) fz_free(ctx, tiff.samples); - *imagep = image; - return fz_okay; + return image; } diff --git a/xps/xps_tile.c b/xps/xps_tile.c index d67a9b24..f6a2143f 100644 --- a/xps/xps_tile.c +++ b/xps/xps_tile.c @@ -16,57 +16,57 @@ struct closure xps_resource *dict; xml_element *root; void *user; - void (*func)(xps_context*, fz_matrix, fz_rect, char*, xps_resource*, xml_element*, void*); + void (*func)(xps_document*, fz_matrix, fz_rect, char*, xps_resource*, xml_element*, void*); }; static void -xps_paint_tiling_brush_clipped(xps_context *ctx, fz_matrix ctm, fz_rect viewbox, struct closure *c) +xps_paint_tiling_brush_clipped(xps_document *doc, fz_matrix ctm, fz_rect viewbox, struct closure *c) { - fz_path *path = fz_new_path(); - fz_moveto(path, viewbox.x0, viewbox.y0); - fz_lineto(path, viewbox.x0, viewbox.y1); - fz_lineto(path, viewbox.x1, viewbox.y1); - fz_lineto(path, viewbox.x1, viewbox.y0); - fz_closepath(path); - fz_clip_path(ctx->dev, path, NULL, 0, ctm); - fz_free_path(path); - c->func(ctx, ctm, viewbox, c->base_uri, c->dict, c->root, c->user); - fz_pop_clip(ctx->dev); + fz_path *path = fz_new_path(doc->ctx); + fz_moveto(doc->ctx, path, viewbox.x0, viewbox.y0); + fz_lineto(doc->ctx, path, viewbox.x0, viewbox.y1); + fz_lineto(doc->ctx, path, viewbox.x1, viewbox.y1); + fz_lineto(doc->ctx, path, viewbox.x1, viewbox.y0); + fz_closepath(doc->ctx, path); + fz_clip_path(doc->dev, path, NULL, 0, ctm); + fz_free_path(doc->ctx, path); + c->func(doc, ctm, viewbox, c->base_uri, c->dict, c->root, c->user); + fz_pop_clip(doc->dev); } static void -xps_paint_tiling_brush(xps_context *ctx, fz_matrix ctm, fz_rect viewbox, int tile_mode, struct closure *c) +xps_paint_tiling_brush(xps_document *doc, fz_matrix ctm, fz_rect viewbox, int tile_mode, struct closure *c) { fz_matrix ttm; - xps_paint_tiling_brush_clipped(ctx, ctm, viewbox, c); + xps_paint_tiling_brush_clipped(doc, ctm, viewbox, c); if (tile_mode == TILE_FLIP_X || tile_mode == TILE_FLIP_X_Y) { ttm = fz_concat(fz_translate(viewbox.x1 * 2, 0), ctm); ttm = fz_concat(fz_scale(-1, 1), ttm); - xps_paint_tiling_brush_clipped(ctx, ttm, viewbox, c); + xps_paint_tiling_brush_clipped(doc, ttm, viewbox, c); } if (tile_mode == TILE_FLIP_Y || tile_mode == TILE_FLIP_X_Y) { ttm = fz_concat(fz_translate(0, viewbox.y1 * 2), ctm); ttm = fz_concat(fz_scale(1, -1), ttm); - xps_paint_tiling_brush_clipped(ctx, ttm, viewbox, c); + xps_paint_tiling_brush_clipped(doc, ttm, viewbox, c); } if (tile_mode == TILE_FLIP_X_Y) { ttm = fz_concat(fz_translate(viewbox.x1 * 2, viewbox.y1 * 2), ctm); ttm = fz_concat(fz_scale(-1, -1), ttm); - xps_paint_tiling_brush_clipped(ctx, ttm, viewbox, c); + xps_paint_tiling_brush_clipped(doc, ttm, viewbox, c); } } void -xps_parse_tiling_brush(xps_context *ctx, fz_matrix ctm, fz_rect area, +xps_parse_tiling_brush(xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *root, - void (*func)(xps_context*, fz_matrix, fz_rect, char*, xps_resource*, xml_element*, void*), void *user) + void (*func)(xps_document*, fz_matrix, fz_rect, char*, xps_resource*, xml_element*, void*), void *user) { xml_element *node; struct closure c; @@ -110,22 +110,22 @@ xps_parse_tiling_brush(xps_context *ctx, fz_matrix ctm, fz_rect area, transform_tag = xml_down(node); } - xps_resolve_resource_reference(ctx, dict, &transform_att, &transform_tag, NULL); + xps_resolve_resource_reference(doc, dict, &transform_att, &transform_tag, NULL); transform = fz_identity; if (transform_att) - xps_parse_render_transform(ctx, transform_att, &transform); + xps_parse_render_transform(doc, transform_att, &transform); if (transform_tag) - xps_parse_matrix_transform(ctx, transform_tag, &transform); + xps_parse_matrix_transform(doc, transform_tag, &transform); ctm = fz_concat(transform, ctm); viewbox = fz_unit_rect; if (viewbox_att) - xps_parse_rectangle(ctx, viewbox_att, &viewbox); + xps_parse_rectangle(doc, viewbox_att, &viewbox); viewport = fz_unit_rect; if (viewport_att) - xps_parse_rectangle(ctx, viewport_att, &viewport); + xps_parse_rectangle(doc, viewport_att, &viewport); /* some sanity checks on the viewport/viewbox size */ if (fabsf(viewport.x1 - viewport.x0) < 0.01f) return; @@ -159,7 +159,7 @@ xps_parse_tiling_brush(xps_context *ctx, fz_matrix ctm, fz_rect area, if (tile_mode == TILE_FLIP_Y || tile_mode == TILE_FLIP_X_Y) ystep *= 2; - xps_begin_opacity(ctx, ctm, area, base_uri, dict, opacity_att, NULL); + xps_begin_opacity(doc, ctm, area, base_uri, dict, opacity_att, NULL); ctm = fz_concat(fz_translate(viewport.x0, viewport.y0), ctm); ctm = fz_concat(fz_scale(xscale, yscale), ctm); @@ -184,9 +184,9 @@ xps_parse_tiling_brush(xps_context *ctx, fz_matrix ctm, fz_rect area, fz_rect bigview = viewbox; bigview.x1 = bigview.x0 + xstep; bigview.y1 = bigview.y0 + ystep; - fz_begin_tile(ctx->dev, area, bigview, xstep, ystep, ctm); - xps_paint_tiling_brush(ctx, ctm, viewbox, tile_mode, &c); - fz_end_tile(ctx->dev); + fz_begin_tile(doc->dev, area, bigview, xstep, ystep, ctm); + xps_paint_tiling_brush(doc, ctm, viewbox, tile_mode, &c); + fz_end_tile(doc->dev); } else { @@ -196,28 +196,28 @@ xps_parse_tiling_brush(xps_context *ctx, fz_matrix ctm, fz_rect area, for (x = x0; x < x1; x++) { fz_matrix ttm = fz_concat(fz_translate(xstep * x, ystep * y), ctm); - xps_paint_tiling_brush(ctx, ttm, viewbox, tile_mode, &c); + xps_paint_tiling_brush(doc, ttm, viewbox, tile_mode, &c); } } } } else { - xps_paint_tiling_brush(ctx, ctm, viewbox, tile_mode, &c); + xps_paint_tiling_brush(doc, ctm, viewbox, tile_mode, &c); } - xps_end_opacity(ctx, base_uri, dict, opacity_att, NULL); + xps_end_opacity(doc, base_uri, dict, opacity_att, NULL); } static void -xps_paint_visual_brush(xps_context *ctx, fz_matrix ctm, fz_rect area, +xps_paint_visual_brush(xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *root, void *visual_tag) { - xps_parse_element(ctx, ctm, area, base_uri, dict, (xml_element *)visual_tag); + xps_parse_element(doc, ctm, area, base_uri, dict, (xml_element *)visual_tag); } void -xps_parse_visual_brush(xps_context *ctx, fz_matrix ctm, fz_rect area, +xps_parse_visual_brush(xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *root) { xml_element *node; @@ -235,17 +235,17 @@ xps_parse_visual_brush(xps_context *ctx, fz_matrix ctm, fz_rect area, } visual_uri = base_uri; - xps_resolve_resource_reference(ctx, dict, &visual_att, &visual_tag, &visual_uri); + xps_resolve_resource_reference(doc, dict, &visual_att, &visual_tag, &visual_uri); if (visual_tag) { - xps_parse_tiling_brush(ctx, ctm, area, + xps_parse_tiling_brush(doc, ctm, area, visual_uri, dict, root, xps_paint_visual_brush, visual_tag); } } void -xps_parse_canvas(xps_context *ctx, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *root) +xps_parse_canvas(xps_document *doc, fz_matrix ctm, fz_rect area, char *base_uri, xps_resource *dict, xml_element *root) { xps_resource *new_dict = NULL; xml_element *node; @@ -272,9 +272,9 @@ xps_parse_canvas(xps_context *ctx, fz_matrix ctm, fz_rect area, char *base_uri, { if (!strcmp(xml_tag(node), "Canvas.Resources") && xml_down(node)) { - code = xps_parse_resource_dictionary(ctx, &new_dict, base_uri, xml_down(node)); + code = xps_parse_resource_dictionary(doc, &new_dict, base_uri, xml_down(node)); if (code) - fz_catch(code, "cannot load Canvas.Resources"); + fz_error_handle(code, "cannot load Canvas.Resources"); else { new_dict->parent = dict; @@ -291,38 +291,38 @@ xps_parse_canvas(xps_context *ctx, fz_matrix ctm, fz_rect area, char *base_uri, } opacity_mask_uri = base_uri; - xps_resolve_resource_reference(ctx, dict, &transform_att, &transform_tag, NULL); - xps_resolve_resource_reference(ctx, dict, &clip_att, &clip_tag, NULL); - xps_resolve_resource_reference(ctx, dict, &opacity_mask_att, &opacity_mask_tag, &opacity_mask_uri); + xps_resolve_resource_reference(doc, dict, &transform_att, &transform_tag, NULL); + xps_resolve_resource_reference(doc, dict, &clip_att, &clip_tag, NULL); + xps_resolve_resource_reference(doc, dict, &opacity_mask_att, &opacity_mask_tag, &opacity_mask_uri); transform = fz_identity; if (transform_att) - xps_parse_render_transform(ctx, transform_att, &transform); + xps_parse_render_transform(doc, transform_att, &transform); if (transform_tag) - xps_parse_matrix_transform(ctx, transform_tag, &transform); + xps_parse_matrix_transform(doc, transform_tag, &transform); ctm = fz_concat(transform, ctm); if (clip_att || clip_tag) - xps_clip(ctx, ctm, dict, clip_att, clip_tag); + xps_clip(doc, ctm, dict, clip_att, clip_tag); - xps_begin_opacity(ctx, ctm, area, opacity_mask_uri, dict, opacity_att, opacity_mask_tag); + xps_begin_opacity(doc, ctm, area, opacity_mask_uri, dict, opacity_att, opacity_mask_tag); for (node = xml_down(root); node; node = xml_next(node)) { - xps_parse_element(ctx, ctm, area, base_uri, dict, node); + xps_parse_element(doc, ctm, area, base_uri, dict, node); } - xps_end_opacity(ctx, opacity_mask_uri, dict, opacity_att, opacity_mask_tag); + xps_end_opacity(doc, opacity_mask_uri, dict, opacity_att, opacity_mask_tag); if (clip_att || clip_tag) - fz_pop_clip(ctx->dev); + fz_pop_clip(doc->dev); if (new_dict) - xps_free_resource_dictionary(ctx, new_dict); + xps_free_resource_dictionary(doc, new_dict); } void -xps_parse_fixed_page(xps_context *ctx, fz_matrix ctm, xps_page *page) +xps_parse_fixed_page(xps_document *doc, fz_matrix ctm, xps_page *page) { xml_element *node; xps_resource *dict; @@ -338,8 +338,8 @@ xps_parse_fixed_page(xps_context *ctx, fz_matrix ctm, xps_page *page) dict = NULL; - ctx->opacity_top = 0; - ctx->opacity[0] = 1; + doc->opacity_top = 0; + doc->opacity[0] = 1; if (!page->root) return; @@ -350,15 +350,15 @@ xps_parse_fixed_page(xps_context *ctx, fz_matrix ctm, xps_page *page) { if (!strcmp(xml_tag(node), "FixedPage.Resources") && xml_down(node)) { - code = xps_parse_resource_dictionary(ctx, &dict, base_uri, xml_down(node)); + code = xps_parse_resource_dictionary(doc, &dict, base_uri, xml_down(node)); if (code) - fz_catch(code, "cannot load FixedPage.Resources"); + fz_error_handle(code, "cannot load FixedPage.Resources"); } - xps_parse_element(ctx, ctm, area, base_uri, dict, node); + xps_parse_element(doc, ctm, area, base_uri, dict, node); } if (dict) { - xps_free_resource_dictionary(ctx, dict); + xps_free_resource_dictionary(doc, dict); } } diff --git a/xps/xps_xml.c b/xps/xps_xml.c index aacabd5a..d92804af 100644 --- a/xps/xps_xml.c +++ b/xps/xps_xml.c @@ -18,6 +18,7 @@ struct element struct parser { struct element *head; + fz_context *ctx; }; static inline void indent(int n) @@ -70,26 +71,26 @@ char *xml_att(struct element *item, const char *name) return NULL; } -static void xml_free_attribute(struct attribute *att) +static void xml_free_attribute(fz_context *ctx, struct attribute *att) { while (att) { struct attribute *next = att->next; if (att->value) - fz_free(att->value); - fz_free(att); + fz_free(ctx, att->value); + fz_free(ctx, att); att = next; } } -void xml_free_element(struct element *item) +void xml_free_element(fz_context *ctx, struct element *item) { while (item) { struct element *next = item->next; if (item->atts) - xml_free_attribute(item->atts); + xml_free_attribute(ctx, item->atts); if (item->down) - xml_free_element(item->down); - fz_free(item); + xml_free_element(ctx, item->down); + fz_free(ctx, item); item = next; } } @@ -133,7 +134,7 @@ static void xml_emit_open_tag(struct parser *parser, char *a, char *b) { struct element *head, *tail; - head = fz_malloc(sizeof(struct element)); + head = fz_malloc(parser->ctx, sizeof(struct element)); if (b - a > sizeof(head->name)) b = a + sizeof(head->name); memcpy(head->name, a, b - a); @@ -162,7 +163,7 @@ static void xml_emit_att_name(struct parser *parser, char *a, char *b) struct element *head = parser->head; struct attribute *att; - att = fz_malloc(sizeof(struct attribute)); + att = fz_malloc(parser->ctx, sizeof(struct attribute)); if (b - a > sizeof(att->name)) b = a + sizeof(att->name); memcpy(att->name, a, b - a); @@ -180,7 +181,7 @@ static void xml_emit_att_value(struct parser *parser, char *a, char *b) int c; /* entities are all longer than UTFmax so runetochar is safe */ - s = att->value = fz_malloc(b - a + 1); + s = att->value = fz_malloc(parser->ctx, b - a + 1); while (a < b) { if (*a == '&') { a += xml_parse_entity(&c, a); @@ -329,14 +330,14 @@ parse_attribute_value: return "end of data in attribute value"; } -static char *convert_to_utf8(unsigned char *s, int n) +static char *convert_to_utf8(fz_context *doc, unsigned char *s, int n) { unsigned char *e = s + n; char *dst, *d; int c; if (s[0] == 0xFE && s[1] == 0xFF) { - dst = d = fz_malloc(n * 2); + dst = d = fz_malloc(doc, n * 2); while (s + 1 < e) { c = s[0] << 8 | s[1]; d += runetochar(d, &c); @@ -347,7 +348,7 @@ static char *convert_to_utf8(unsigned char *s, int n) } if (s[0] == 0xFF && s[1] == 0xFE) { - dst = d = fz_malloc(n * 2); + dst = d = fz_malloc(doc, n * 2); while (s + 1 < e) { c = s[0] | s[1] << 8; d += runetochar(d, &c); @@ -361,7 +362,7 @@ static char *convert_to_utf8(unsigned char *s, int n) } struct element * -xml_parse_document(unsigned char *s, int n) +xml_parse_document(fz_context *ctx, unsigned char *s, int n) { struct parser parser; struct element root; @@ -371,17 +372,18 @@ xml_parse_document(unsigned char *s, int n) memset(&root, 0, sizeof(root)); parser.head = &root; + parser.ctx = ctx; - p = convert_to_utf8(s, n); + p = convert_to_utf8(ctx, s, n); error = xml_parse_document_imp(&parser, p); if (error) { - fz_throw("%s", error); + fz_throw(ctx, "%s", error); return NULL; } if (p != (char*)s) - fz_free(p); + fz_free(ctx, p); return root.down; } diff --git a/xps/xps_zip.c b/xps/xps_zip.c index a0c981bf..f02bf33e 100644 --- a/xps/xps_zip.c +++ b/xps/xps_zip.c @@ -4,25 +4,25 @@ #include <zlib.h> xps_part * -xps_new_part(xps_context *ctx, char *name, int size) +xps_new_part(xps_document *doc, char *name, int size) { xps_part *part; - part = fz_malloc(sizeof(xps_part)); - part->name = fz_strdup(name); + part = fz_malloc(doc->ctx, sizeof(xps_part)); + part->name = fz_strdup(doc->ctx, name); part->size = size; - part->data = fz_malloc(size + 1); + part->data = fz_malloc(doc->ctx, size + 1); part->data[size] = 0; /* null-terminate for xml parser */ return part; } void -xps_free_part(xps_context *ctx, xps_part *part) +xps_free_part(xps_document *doc, xps_part *part) { - fz_free(part->name); - fz_free(part->data); - fz_free(part); + fz_free(doc->ctx, part->name); + fz_free(doc->ctx, part->data); + fz_free(doc->ctx, part); } static inline int getshort(fz_stream *file) @@ -42,15 +42,15 @@ static inline int getlong(fz_stream *file) } static void * -xps_zip_alloc_items(xps_context *ctx, int items, int size) +xps_zip_alloc_items(xps_document *doc, int items, int size) { - return fz_calloc(items, size); + return fz_malloc_array(doc->ctx, items, size); } static void -xps_zip_free(xps_context *ctx, void *ptr) +xps_zip_free(xps_document *doc, void *ptr) { - fz_free(ptr); + fz_free(doc->ctx, ptr); } static int @@ -62,26 +62,26 @@ xps_compare_entries(const void *a0, const void *b0) } static xps_entry * -xps_find_zip_entry(xps_context *ctx, char *name) +xps_find_zip_entry(xps_document *doc, char *name) { int l = 0; - int r = ctx->zip_count - 1; + int r = doc->zip_count - 1; while (l <= r) { int m = (l + r) >> 1; - int c = xps_strcasecmp(name, ctx->zip_table[m].name); + int c = xps_strcasecmp(name, doc->zip_table[m].name); if (c < 0) r = m - 1; else if (c > 0) l = m + 1; else - return &ctx->zip_table[m]; + return &doc->zip_table[m]; } return NULL; } static int -xps_read_zip_entry(xps_context *ctx, xps_entry *ent, unsigned char *outbuf) +xps_read_zip_entry(xps_document *doc, xps_entry *ent, unsigned char *outbuf) { z_stream stream; unsigned char *inbuf; @@ -90,39 +90,39 @@ xps_read_zip_entry(xps_context *ctx, xps_entry *ent, unsigned char *outbuf) int namelength, extralength; int code; - fz_seek(ctx->file, ent->offset, 0); + fz_seek(doc->file, ent->offset, 0); - sig = getlong(ctx->file); + sig = getlong(doc->file); if (sig != ZIP_LOCAL_FILE_SIG) - return fz_throw("wrong zip local file signature (0x%x)", sig); + return fz_error_make("wrong zip local file signature (0x%x)", sig); - version = getshort(ctx->file); - general = getshort(ctx->file); - method = getshort(ctx->file); - (void) getshort(ctx->file); /* file time */ - (void) getshort(ctx->file); /* file date */ - (void) getlong(ctx->file); /* crc-32 */ - (void) getlong(ctx->file); /* csize */ - (void) getlong(ctx->file); /* usize */ - namelength = getshort(ctx->file); - extralength = getshort(ctx->file); + version = getshort(doc->file); + general = getshort(doc->file); + method = getshort(doc->file); + (void) getshort(doc->file); /* file time */ + (void) getshort(doc->file); /* file date */ + (void) getlong(doc->file); /* crc-32 */ + (void) getlong(doc->file); /* csize */ + (void) getlong(doc->file); /* usize */ + namelength = getshort(doc->file); + extralength = getshort(doc->file); - fz_seek(ctx->file, namelength + extralength, 1); + fz_seek(doc->file, namelength + extralength, 1); if (method == 0) { - fz_read(ctx->file, outbuf, ent->usize); + fz_read(doc->file, outbuf, ent->usize); } else if (method == 8) { - inbuf = fz_malloc(ent->csize); + inbuf = fz_malloc(doc->ctx, ent->csize); - fz_read(ctx->file, inbuf, ent->csize); + fz_read(doc->file, inbuf, ent->csize); memset(&stream, 0, sizeof(z_stream)); stream.zalloc = (alloc_func) xps_zip_alloc_items; stream.zfree = (free_func) xps_zip_free; - stream.opaque = ctx; + stream.opaque = doc; stream.next_in = inbuf; stream.avail_in = ent->csize; stream.next_out = outbuf; @@ -130,22 +130,22 @@ xps_read_zip_entry(xps_context *ctx, xps_entry *ent, unsigned char *outbuf) code = inflateInit2(&stream, -15); if (code != Z_OK) - return fz_throw("zlib inflateInit2 error: %s", stream.msg); + return fz_error_make("zlib inflateInit2 error: %s", stream.msg); code = inflate(&stream, Z_FINISH); if (code != Z_STREAM_END) { inflateEnd(&stream); - return fz_throw("zlib inflate error: %s", stream.msg); + return fz_error_make("zlib inflate error: %s", stream.msg); } code = inflateEnd(&stream); if (code != Z_OK) - return fz_throw("zlib inflateEnd error: %s", stream.msg); + return fz_error_make("zlib inflateEnd error: %s", stream.msg); - fz_free(inbuf); + fz_free(doc->ctx, inbuf); } else { - return fz_throw("unknown compression method (%d)", method); + return fz_error_make("unknown compression method (%d)", method); } return fz_okay; @@ -156,104 +156,103 @@ xps_read_zip_entry(xps_context *ctx, xps_entry *ent, unsigned char *outbuf) */ static int -xps_read_zip_dir(xps_context *ctx, int start_offset) +xps_read_zip_dir(xps_document *doc, int start_offset) { int sig; int offset, count; int namesize, metasize, commentsize; int i; - fz_seek(ctx->file, start_offset, 0); + fz_seek(doc->file, start_offset, 0); - sig = getlong(ctx->file); + sig = getlong(doc->file); if (sig != ZIP_END_OF_CENTRAL_DIRECTORY_SIG) - return fz_throw("wrong zip end of central directory signature (0x%x)", sig); + return fz_error_make("wrong zip end of central directory signature (0x%x)", sig); - (void) getshort(ctx->file); /* this disk */ - (void) getshort(ctx->file); /* start disk */ - (void) getshort(ctx->file); /* entries in this disk */ - count = getshort(ctx->file); /* entries in central directory disk */ - (void) getlong(ctx->file); /* size of central directory */ - offset = getlong(ctx->file); /* offset to central directory */ + (void) getshort(doc->file); /* this disk */ + (void) getshort(doc->file); /* start disk */ + (void) getshort(doc->file); /* entries in this disk */ + count = getshort(doc->file); /* entries in central directory disk */ + (void) getlong(doc->file); /* size of central directory */ + offset = getlong(doc->file); /* offset to central directory */ - ctx->zip_count = count; - ctx->zip_table = fz_calloc(count, sizeof(xps_entry)); - memset(ctx->zip_table, 0, sizeof(xps_entry) * count); + doc->zip_count = count; + doc->zip_table = fz_malloc_array(doc->ctx, count, sizeof(xps_entry)); - fz_seek(ctx->file, offset, 0); + fz_seek(doc->file, offset, 0); for (i = 0; i < count; i++) { - sig = getlong(ctx->file); + sig = getlong(doc->file); if (sig != ZIP_CENTRAL_DIRECTORY_SIG) - return fz_throw("wrong zip central directory signature (0x%x)", sig); - - (void) getshort(ctx->file); /* version made by */ - (void) getshort(ctx->file); /* version to extract */ - (void) getshort(ctx->file); /* general */ - (void) getshort(ctx->file); /* method */ - (void) getshort(ctx->file); /* last mod file time */ - (void) getshort(ctx->file); /* last mod file date */ - (void) getlong(ctx->file); /* crc-32 */ - ctx->zip_table[i].csize = getlong(ctx->file); - ctx->zip_table[i].usize = getlong(ctx->file); - namesize = getshort(ctx->file); - metasize = getshort(ctx->file); - commentsize = getshort(ctx->file); - (void) getshort(ctx->file); /* disk number start */ - (void) getshort(ctx->file); /* int file atts */ - (void) getlong(ctx->file); /* ext file atts */ - ctx->zip_table[i].offset = getlong(ctx->file); - - ctx->zip_table[i].name = fz_malloc(namesize + 1); - fz_read(ctx->file, (unsigned char*)ctx->zip_table[i].name, namesize); - ctx->zip_table[i].name[namesize] = 0; - - fz_seek(ctx->file, metasize, 1); - fz_seek(ctx->file, commentsize, 1); + return fz_error_make("wrong zip central directory signature (0x%x)", sig); + + (void) getshort(doc->file); /* version made by */ + (void) getshort(doc->file); /* version to extract */ + (void) getshort(doc->file); /* general */ + (void) getshort(doc->file); /* method */ + (void) getshort(doc->file); /* last mod file time */ + (void) getshort(doc->file); /* last mod file date */ + (void) getlong(doc->file); /* crc-32 */ + doc->zip_table[i].csize = getlong(doc->file); + doc->zip_table[i].usize = getlong(doc->file); + namesize = getshort(doc->file); + metasize = getshort(doc->file); + commentsize = getshort(doc->file); + (void) getshort(doc->file); /* disk number start */ + (void) getshort(doc->file); /* int file atts */ + (void) getlong(doc->file); /* ext file atts */ + doc->zip_table[i].offset = getlong(doc->file); + + doc->zip_table[i].name = fz_malloc(doc->ctx, namesize + 1); + fz_read(doc->file, (unsigned char*)doc->zip_table[i].name, namesize); + doc->zip_table[i].name[namesize] = 0; + + fz_seek(doc->file, metasize, 1); + fz_seek(doc->file, commentsize, 1); } - qsort(ctx->zip_table, count, sizeof(xps_entry), xps_compare_entries); + qsort(doc->zip_table, count, sizeof(xps_entry), xps_compare_entries); return fz_okay; } static int -xps_find_and_read_zip_dir(xps_context *ctx) +xps_find_and_read_zip_dir(xps_document *doc) { unsigned char buf[512]; int file_size, back, maxback; int i, n; - fz_seek(ctx->file, 0, SEEK_END); - file_size = fz_tell(ctx->file); + fz_seek(doc->file, 0, SEEK_END); + file_size = fz_tell(doc->file); maxback = MIN(file_size, 0xFFFF + sizeof buf); back = MIN(maxback, sizeof buf); while (back < maxback) { - fz_seek(ctx->file, file_size - back, 0); + fz_seek(doc->file, file_size - back, 0); - n = fz_read(ctx->file, buf, sizeof buf); + n = fz_read(doc->file, buf, sizeof buf); if (n < 0) - return fz_throw("cannot read end of central directory"); + return fz_error_make("cannot read end of central directory"); for (i = n - 4; i > 0; i--) if (!memcmp(buf + i, "PK\5\6", 4)) - return xps_read_zip_dir(ctx, file_size - back + i); + return xps_read_zip_dir(doc, file_size - back + i); back += sizeof buf - 4; } - return fz_throw("cannot find end of central directory"); + return fz_error_make("cannot find end of central directory"); } /* * Read and interleave split parts from a ZIP file. */ static xps_part * -xps_read_zip_part(xps_context *ctx, char *partname) +xps_read_zip_part(xps_document *doc, char *partname) { char buf[2048]; xps_entry *ent; @@ -266,11 +265,11 @@ xps_read_zip_part(xps_context *ctx, char *partname) name ++; /* All in one piece */ - ent = xps_find_zip_entry(ctx, name); + ent = xps_find_zip_entry(doc, name); if (ent) { - part = xps_new_part(ctx, partname, ent->usize); - xps_read_zip_entry(ctx, ent, part->data); + part = xps_new_part(doc, partname, ent->usize); + xps_read_zip_entry(doc, ent, part->data); return part; } @@ -280,11 +279,11 @@ xps_read_zip_part(xps_context *ctx, char *partname) while (1) { sprintf(buf, "%s/[%d].piece", name, count); - ent = xps_find_zip_entry(ctx, buf); + ent = xps_find_zip_entry(doc, buf); if (!ent) { sprintf(buf, "%s/[%d].last.piece", name, count); - ent = xps_find_zip_entry(ctx, buf); + ent = xps_find_zip_entry(doc, buf); } if (!ent) break; @@ -295,7 +294,7 @@ xps_read_zip_part(xps_context *ctx, char *partname) /* Inflate the pieces */ if (count) { - part = xps_new_part(ctx, partname, size); + part = xps_new_part(doc, partname, size); offset = 0; for (i = 0; i < count; i++) { @@ -303,8 +302,8 @@ xps_read_zip_part(xps_context *ctx, char *partname) sprintf(buf, "%s/[%d].piece", name, i); else sprintf(buf, "%s/[%d].last.piece", name, i); - ent = xps_find_zip_entry(ctx, buf); - xps_read_zip_entry(ctx, ent, part->data + offset); + ent = xps_find_zip_entry(doc, buf); + xps_read_zip_entry(doc, ent, part->data + offset); offset += ent->usize; } return part; @@ -317,14 +316,14 @@ xps_read_zip_part(xps_context *ctx, char *partname) * Read and interleave split parts from files in the directory. */ static xps_part * -xps_read_dir_part(xps_context *ctx, char *name) +xps_read_dir_part(xps_document *doc, char *name) { char buf[2048]; xps_part *part; FILE *file; int count, size, offset, i, n; - fz_strlcpy(buf, ctx->directory, sizeof buf); + fz_strlcpy(buf, doc->directory, sizeof buf); fz_strlcat(buf, name, sizeof buf); /* All in one piece */ @@ -334,7 +333,7 @@ xps_read_dir_part(xps_context *ctx, char *name) fseek(file, 0, SEEK_END); size = ftell(file); fseek(file, 0, SEEK_SET); - part = xps_new_part(ctx, name, size); + part = xps_new_part(doc, name, size); fread(part->data, 1, size, file); fclose(file); return part; @@ -345,11 +344,11 @@ xps_read_dir_part(xps_context *ctx, char *name) size = 0; while (1) { - sprintf(buf, "%s%s/[%d].piece", ctx->directory, name, count); + sprintf(buf, "%s%s/[%d].piece", doc->directory, name, count); file = fopen(buf, "rb"); if (!file) { - sprintf(buf, "%s%s/[%d].last.piece", ctx->directory, name, count); + sprintf(buf, "%s%s/[%d].last.piece", doc->directory, name, count); file = fopen(buf, "rb"); } if (!file) @@ -363,14 +362,14 @@ xps_read_dir_part(xps_context *ctx, char *name) /* Inflate the pieces */ if (count) { - part = xps_new_part(ctx, name, size); + part = xps_new_part(doc, name, size); offset = 0; for (i = 0; i < count; i++) { if (i < count - 1) - sprintf(buf, "%s%s/[%d].piece", ctx->directory, name, i); + sprintf(buf, "%s%s/[%d].piece", doc->directory, name, i); else - sprintf(buf, "%s%s/[%d].last.piece", ctx->directory, name, i); + sprintf(buf, "%s%s/[%d].last.piece", doc->directory, name, i); file = fopen(buf, "rb"); n = fread(part->data + offset, 1, size - offset, file); offset += n; @@ -383,71 +382,79 @@ xps_read_dir_part(xps_context *ctx, char *name) } xps_part * -xps_read_part(xps_context *ctx, char *partname) +xps_read_part(xps_document *doc, char *partname) { - if (ctx->directory) - return xps_read_dir_part(ctx, partname); - return xps_read_zip_part(ctx, partname); + if (doc->directory) + return xps_read_dir_part(doc, partname); + return xps_read_zip_part(doc, partname); } -static int -xps_open_directory(xps_context **ctxp, char *directory) +static xps_document * +xps_open_directory(fz_context *ctx, char *directory) { - xps_context *ctx; - int code; + xps_document *doc; - ctx = fz_malloc(sizeof(xps_context)); - memset(ctx, 0, sizeof(xps_context)); + doc = fz_malloc(ctx, sizeof(xps_document)); + memset(doc, 0, sizeof *doc); - ctx->directory = fz_strdup(directory); + doc->directory = fz_strdup(ctx, directory); + doc->ctx = ctx; - code = xps_read_page_list(ctx); - if (code) + fz_try(ctx) { - xps_free_context(ctx); - return fz_rethrow(code, "cannot read page list"); + xps_read_page_list(doc); + } + fz_catch(ctx) + { + xps_free_context(doc); + fz_throw(ctx, "cannot read page list"); } - *ctxp = ctx; - return fz_okay; + return doc; } -int -xps_open_stream(xps_context **ctxp, fz_stream *file) +xps_document * +xps_open_stream(fz_stream *file) { - xps_context *ctx; - int code; + xps_document *doc; + fz_context *ctx = file->ctx; - ctx = fz_malloc(sizeof(xps_context)); - memset(ctx, 0, sizeof(xps_context)); + doc = fz_malloc(ctx, sizeof(xps_document)); + memset(doc, 0, sizeof *doc); - ctx->file = fz_keep_stream(file); + doc->ctx = ctx; + doc->file = fz_keep_stream(file); - code = xps_find_and_read_zip_dir(ctx); - if (code < 0) + fz_try(ctx) + { + xps_find_and_read_zip_dir(doc); + } + fz_catch(ctx) { - xps_free_context(ctx); - return fz_rethrow(code, "cannot read zip central directory"); + xps_free_context(doc); + fz_throw(ctx, "cannot read zip central directory"); } - code = xps_read_page_list(ctx); - if (code) + fz_try(ctx) { - xps_free_context(ctx); - return fz_rethrow(code, "cannot read page list"); + xps_read_page_list(doc); + } + fz_catch(ctx) + { + xps_free_context(doc); + fz_throw(ctx, "cannot read page list"); } - *ctxp = ctx; - return fz_okay; + return doc; } -int -xps_open_file(xps_context **ctxp, char *filename) +xps_document * +xps_open_file(fz_context *ctx, char *filename) { char buf[2048]; fz_stream *file; char *p; - int code; + xps_document *doc; if (strstr(filename, "/_rels/.rels") || strstr(filename, "\\_rels\\.rels")) { @@ -456,46 +463,52 @@ xps_open_file(xps_context **ctxp, char *filename) if (!p) p = strstr(buf, "\\_rels\\.rels"); *p = 0; - return xps_open_directory(ctxp, buf); + return xps_open_directory(ctx, buf); } - file = fz_open_file(filename); + file = fz_open_file(ctx, filename); if (!file) - return fz_throw("cannot open file '%s': %s", filename, strerror(errno)); + fz_throw(ctx, "cannot open file '%s': %s", filename, strerror(errno)); - code = xps_open_stream(ctxp, file); + fz_try(ctx) + { + doc = xps_open_stream(file); + } + fz_catch(ctx) + { + fz_close(file); + fz_throw(ctx, "cannot load document '%s'", filename); + } fz_close(file); - if (code) - return fz_rethrow(code, "cannot load document '%s'", filename); - return fz_okay; + return doc; } void -xps_free_context(xps_context *ctx) +xps_free_context(xps_document *doc) { xps_font_cache *font, *next; int i; - if (ctx->file) - fz_close(ctx->file); + if (doc->file) + fz_close(doc->file); - for (i = 0; i < ctx->zip_count; i++) - fz_free(ctx->zip_table[i].name); - fz_free(ctx->zip_table); + for (i = 0; i < doc->zip_count; i++) + fz_free(doc->ctx, doc->zip_table[i].name); + fz_free(doc->ctx, doc->zip_table); - font = ctx->font_table; + font = doc->font_table; while (font) { next = font->next; - fz_drop_font(font->font); - fz_free(font->name); - fz_free(font); + fz_drop_font(doc->ctx, font->font); + fz_free(doc->ctx, font->name); + fz_free(doc->ctx, font); font = next; } - xps_free_page_list(ctx); + xps_free_page_list(doc); - fz_free(ctx->start_part); - fz_free(ctx->directory); - fz_free(ctx); + fz_free(doc->ctx, doc->start_part); + fz_free(doc->ctx, doc->directory); + fz_free(doc->ctx, doc); } |