diff options
author | Robin Watts <robin.watts@artifex.com> | 2011-09-14 17:36:57 +0100 |
---|---|---|
committer | Robin Watts <Robin.Watts@artifex.com> | 2011-09-15 14:50:17 +0100 |
commit | b51ef0eea028c73b6379e832eaa34fff3fbbb927 (patch) | |
tree | 1ab685ccd356e7fdc832b2e3322c0486b2670cfb /pdf/pdf_annot.c | |
parent | 89ae81f651bfa112b8e07317eb6983beaf7cb212 (diff) | |
download | mupdf-b51ef0eea028c73b6379e832eaa34fff3fbbb927.tar.xz |
Add context to mupdf.
Huge pervasive change to lots of files, adding a context for exception
handling and allocation.
In time we'll move more statics into there.
Also fix some for(i = 0; i < function(...); i++) calls.
Diffstat (limited to 'pdf/pdf_annot.c')
-rw-r--r-- | pdf/pdf_annot.c | 113 |
1 files changed, 65 insertions, 48 deletions
diff --git a/pdf/pdf_annot.c b/pdf/pdf_annot.c index 8401d79b..cc057d6f 100644 --- a/pdf/pdf_annot.c +++ b/pdf/pdf_annot.c @@ -2,32 +2,38 @@ #include "mupdf.h" void -pdf_free_link(pdf_link *link) +pdf_free_link(fz_context *ctx, pdf_link *link) { - if (link->next) - pdf_free_link(link->next); - if (link->dest) - fz_drop_obj(link->dest); - fz_free(link); + pdf_link *next; + + do + { + next = link->next; + if (link->dest) + fz_drop_obj(ctx, link->dest); + fz_free(ctx, link); + link = next; + } + while(link != NULL); } static fz_obj * resolve_dest(pdf_xref *xref, fz_obj *dest) { - if (fz_is_name(dest) || fz_is_string(dest)) + if (fz_is_name(xref->ctx, dest) || fz_is_string(xref->ctx, dest)) { dest = pdf_lookup_dest(xref, dest); return resolve_dest(xref, dest); } - else if (fz_is_array(dest)) + else if (fz_is_array(xref->ctx, dest)) { return dest; } - else if (fz_is_dict(dest)) + else if (fz_is_dict(xref->ctx, dest)) { - dest = fz_dict_gets(dest, "D"); + dest = fz_dict_gets(xref->ctx, dest, "D"); return resolve_dest(xref, dest); } @@ -45,52 +51,53 @@ pdf_load_link(pdf_xref *xref, fz_obj *dict) fz_obj *obj; fz_rect bbox; pdf_link_kind kind; + fz_context *ctx = xref->ctx; dest = NULL; - obj = fz_dict_gets(dict, "Rect"); + obj = fz_dict_gets(ctx, dict, "Rect"); if (obj) - bbox = pdf_to_rect(obj); + bbox = pdf_to_rect(ctx, obj); else bbox = fz_empty_rect; - obj = fz_dict_gets(dict, "Dest"); + obj = fz_dict_gets(ctx, dict, "Dest"); if (obj) { kind = PDF_LINK_GOTO; dest = resolve_dest(xref, obj); } - action = fz_dict_gets(dict, "A"); + action = fz_dict_gets(ctx, dict, "A"); /* fall back to additional action button's down/up action */ if (!action) - action = fz_dict_getsa(fz_dict_gets(dict, "AA"), "U", "D"); + action = fz_dict_getsa(ctx, fz_dict_gets(ctx, dict, "AA"), "U", "D"); if (action) { - obj = fz_dict_gets(action, "S"); - if (fz_is_name(obj) && !strcmp(fz_to_name(obj), "GoTo")) + obj = fz_dict_gets(ctx, action, "S"); + if (fz_is_name(ctx, obj) && !strcmp(fz_to_name(ctx, obj), "GoTo")) { kind = PDF_LINK_GOTO; - dest = resolve_dest(xref, fz_dict_gets(action, "D")); + dest = resolve_dest(xref, fz_dict_gets(ctx, action, "D")); } - else if (fz_is_name(obj) && !strcmp(fz_to_name(obj), "URI")) + else if (fz_is_name(ctx, obj) && !strcmp(fz_to_name(ctx, obj), "URI")) { kind = PDF_LINK_URI; - dest = fz_dict_gets(action, "URI"); + dest = fz_dict_gets(ctx, action, "URI"); } - else if (fz_is_name(obj) && !strcmp(fz_to_name(obj), "Launch")) + else if (fz_is_name(ctx, obj) && !strcmp(fz_to_name(ctx, obj), "Launch")) { kind = PDF_LINK_LAUNCH; - dest = fz_dict_gets(action, "F"); + dest = fz_dict_gets(ctx, action, "F"); } - else if (fz_is_name(obj) && !strcmp(fz_to_name(obj), "Named")) + else if (fz_is_name(ctx, obj) && !strcmp(fz_to_name(ctx, obj), "Named")) { kind = PDF_LINK_NAMED; - dest = fz_dict_gets(action, "N"); + dest = fz_dict_gets(ctx, action, "N"); } - else if (fz_is_name(obj) && (!strcmp(fz_to_name(obj), "GoToR"))) + else if (fz_is_name(ctx, obj) && (!strcmp(fz_to_name(ctx, obj), "GoToR"))) { kind = PDF_LINK_ACTION; dest = action; @@ -103,7 +110,7 @@ pdf_load_link(pdf_xref *xref, fz_obj *dict) if (dest) { - pdf_link *link = fz_malloc(sizeof(pdf_link)); + pdf_link *link = fz_malloc(ctx, sizeof(pdf_link)); link->kind = kind; link->rect = bbox; link->dest = fz_keep_obj(dest); @@ -119,14 +126,16 @@ pdf_load_links(pdf_link **linkp, pdf_xref *xref, fz_obj *annots) { pdf_link *link, *head, *tail; fz_obj *obj; - int i; + int i, n; + fz_context *ctx = xref->ctx; head = tail = NULL; link = NULL; - for (i = 0; i < fz_array_len(annots); i++) + n = fz_array_len(ctx, annots); + for (i = 0; i < n; i++) { - obj = fz_array_get(annots, i); + obj = fz_array_get(ctx, annots, i); link = pdf_load_link(xref, obj); if (link) { @@ -144,15 +153,21 @@ pdf_load_links(pdf_link **linkp, pdf_xref *xref, fz_obj *annots) } void -pdf_free_annot(pdf_annot *annot) +pdf_free_annot(fz_context *ctx, pdf_annot *annot) { - if (annot->next) - pdf_free_annot(annot->next); - if (annot->ap) - pdf_drop_xobject(annot->ap); - if (annot->obj) - fz_drop_obj(annot->obj); - fz_free(annot); + pdf_annot *next; + + do + { + next = annot->next; + if (annot->ap) + pdf_drop_xobject(ctx, annot->ap); + if (annot->obj) + fz_drop_obj(ctx, annot->obj); + fz_free(ctx, annot); + annot = next; + } + while (annot != NULL); } static void @@ -179,25 +194,27 @@ pdf_load_annots(pdf_annot **annotp, pdf_xref *xref, fz_obj *annots) fz_obj *obj, *ap, *as, *n, *rect; pdf_xobject *form; fz_error error; - int i; + int i, len; + fz_context *ctx = xref->ctx; head = tail = NULL; annot = NULL; - for (i = 0; i < fz_array_len(annots); i++) + len = fz_array_len(ctx, annots); + for (i = 0; i < len; i++) { - obj = fz_array_get(annots, i); + obj = fz_array_get(ctx, annots, i); - rect = fz_dict_gets(obj, "Rect"); - ap = fz_dict_gets(obj, "AP"); - as = fz_dict_gets(obj, "AS"); - if (fz_is_dict(ap)) + rect = fz_dict_gets(ctx, obj, "Rect"); + ap = fz_dict_gets(ctx, obj, "AP"); + as = fz_dict_gets(ctx, obj, "AS"); + if (fz_is_dict(ctx, ap)) { - n = fz_dict_gets(ap, "N"); /* normal state */ + n = fz_dict_gets(ctx, ap, "N"); /* normal state */ /* lookup current state in sub-dictionary */ if (!pdf_is_stream(xref, fz_to_num(n), fz_to_gen(n))) - n = fz_dict_get(n, as); + n = fz_dict_get(ctx, n, as); if (pdf_is_stream(xref, fz_to_num(n), fz_to_gen(n))) { @@ -208,9 +225,9 @@ pdf_load_annots(pdf_annot **annotp, pdf_xref *xref, fz_obj *annots) continue; } - annot = fz_malloc(sizeof(pdf_annot)); + annot = fz_malloc(ctx, sizeof(pdf_annot)); annot->obj = fz_keep_obj(obj); - annot->rect = pdf_to_rect(rect); + annot->rect = pdf_to_rect(ctx, rect); annot->ap = form; annot->next = NULL; |