diff options
-rw-r--r-- | include/mupdf/pdf/annot.h | 4 | ||||
-rw-r--r-- | source/pdf/pdf-annot-edit.c | 24 | ||||
-rw-r--r-- | source/tools/murun.c | 26 |
3 files changed, 33 insertions, 21 deletions
diff --git a/include/mupdf/pdf/annot.h b/include/mupdf/pdf/annot.h index 866e5ba5..644671ec 100644 --- a/include/mupdf/pdf/annot.h +++ b/include/mupdf/pdf/annot.h @@ -31,8 +31,8 @@ typedef enum PDF_ANNOT_UNKNOWN = -1 } fz_annot_type; -const char *pdf_string_from_annot_type(fz_annot_type type); -int pdf_annot_type_from_string(const char *subtype); +const char *pdf_string_from_annot_type(fz_context *ctx, fz_annot_type type); +int pdf_annot_type_from_string(fz_context *ctx, const char *subtype); enum { diff --git a/source/pdf/pdf-annot-edit.c b/source/pdf/pdf-annot-edit.c index 04ba658e..a3a0137d 100644 --- a/source/pdf/pdf-annot-edit.c +++ b/source/pdf/pdf-annot-edit.c @@ -2,7 +2,7 @@ #define TEXT_ANNOT_SIZE (25.0) -const char *pdf_string_from_annot_type(fz_annot_type type) +const char *pdf_string_from_annot_type(fz_context *ctx, fz_annot_type type) { switch (type) { @@ -31,11 +31,11 @@ const char *pdf_string_from_annot_type(fz_annot_type type) case PDF_ANNOT_TRAP_NET: return "TrapNet"; case PDF_ANNOT_WATERMARK: return "Watermark"; case PDF_ANNOT_3D: return "3D"; - default: return "Unknown"; + default: return "UNKNOWN"; } } -int pdf_annot_type_from_string(const char *subtype) +int pdf_annot_type_from_string(fz_context *ctx, const char *subtype) { if (!strcmp("Text", subtype)) return PDF_ANNOT_TEXT; if (!strcmp("Link", subtype)) return PDF_ANNOT_LINK; @@ -79,8 +79,14 @@ pdf_create_annot(fz_context *ctx, pdf_page *page, fz_annot_type type) { int ind_obj_num; fz_rect rect = {0.0, 0.0, 0.0, 0.0}; - const char *type_str = pdf_string_from_annot_type(type); - pdf_obj *annot_arr = pdf_dict_get(ctx, page->obj, PDF_NAME_Annots); + const char *type_str; + pdf_obj *annot_arr; + + type_str = pdf_string_from_annot_type(ctx, type); + if (type == PDF_ANNOT_UNKNOWN) + fz_throw(ctx, FZ_ERROR_GENERIC, "cannot create unknown annotation"); + + annot_arr = pdf_dict_get(ctx, page->obj, PDF_NAME_Annots); if (annot_arr == NULL) { annot_arr = pdf_new_array(ctx, doc, 0); @@ -188,7 +194,7 @@ pdf_annot_type(fz_context *ctx, pdf_annot *annot) { pdf_obj *obj = annot->obj; pdf_obj *subtype = pdf_dict_get(ctx, obj, PDF_NAME_Subtype); - return pdf_annot_type_from_string(pdf_to_name(ctx, subtype)); + return pdf_annot_type_from_string(ctx, pdf_to_name(ctx, subtype)); } int @@ -307,7 +313,11 @@ static void pdf_set_annot_color_imp(fz_context *ctx, pdf_annot *annot, pdf_obj *key, int n, const float color[4]) { pdf_document *doc = annot->page->doc; - pdf_obj *obj = pdf_new_array(ctx, doc, 4); + pdf_obj *obj; + if (n != 0 && n != 1 && n != 3 && n != 4) + fz_throw(ctx, FZ_ERROR_GENERIC, "color must be 0, 1, 3 or 4 components"); + + obj = pdf_new_array(ctx, doc, n); switch (n) { default: diff --git a/source/tools/murun.c b/source/tools/murun.c index 3ee41764..85cb521d 100644 --- a/source/tools/murun.c +++ b/source/tools/murun.c @@ -3786,11 +3786,13 @@ static void ffi_PDFPage_createAnnotation(js_State *J) pdf_page *page = js_touserdata(J, 0, "pdf_page"); const char *name = js_tostring(J, 1); pdf_annot *annot; - int subtype = pdf_annot_type_from_string(name); - if (subtype < 0) - js_error(J, "unknown PDF annotation subtype: %s", name); + int subtype; + fz_try(ctx) + { + subtype = pdf_annot_type_from_string(ctx, name); annot = pdf_create_annot(ctx, page, subtype); + } fz_catch(ctx) rethrow(J); ffi_pushannot(J, (fz_annot*)annot); @@ -3811,12 +3813,16 @@ static void ffi_PDFAnnotation_getType(js_State *J) { fz_context *ctx = js_getcontext(J); pdf_annot *annot = js_touserdata(J, 0, "pdf_annot"); - int subtype; + int type; + const char *subtype; fz_try(ctx) - subtype = pdf_annot_type(ctx, annot); + { + type = pdf_annot_type(ctx, annot); + subtype = pdf_string_from_annot_type(ctx, type); + } fz_catch(ctx) rethrow(J); - js_pushstring(J, pdf_string_from_annot_type(subtype)); + js_pushstring(J, subtype); } static void ffi_PDFAnnotation_getFlags(js_State *J) @@ -3934,9 +3940,7 @@ static void ffi_PDFAnnotation_setColor(js_State *J) pdf_annot *annot = js_touserdata(J, 0, "pdf_annot"); int i, n = js_getlength(J, 1); float color[4]; - if (n != 0 && n != 1 && n != 3 && n != 4) - js_error(J, "color must be 0, 1, 3, or 4 components"); - for (i = 0; i < n; ++i) { + for (i = 0; i < n && i < 4; ++i) { js_getindex(J, 1, i); color[i] = js_tonumber(J, -1); js_pop(J, 1); @@ -3970,9 +3974,7 @@ static void ffi_PDFAnnotation_setInteriorColor(js_State *J) pdf_annot *annot = js_touserdata(J, 0, "pdf_annot"); int i, n = js_getlength(J, 1); float color[4]; - if (n != 0 && n != 1 && n != 3 && n != 4) - js_error(J, "color must be 0, 1, 3, or 4 components"); - for (i = 0; i < n; ++i) { + for (i = 0; i < n && i < 4; ++i) { js_getindex(J, 1, i); color[i] = js_tonumber(J, -1); js_pop(J, 1); |