summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Rasmussen <sebras@gmail.com>2017-04-15 00:03:17 +0800
committerSebastian Rasmussen <sebras@gmail.com>2017-04-20 19:47:03 +0800
commit96b76d2b9748551df1ef50db0688b39d4670e23a (patch)
tree494f22c85889f4527506698b38dacaf66e1ed33c
parent23eddfa1032bd0111b03a8295544c4009be5ead3 (diff)
downloadmupdf-96b76d2b9748551df1ef50db0688b39d4670e23a.tar.xz
js: Push annotation error handling down to library.
This makes is possible for JNI code to depend on the library for error handling.
-rw-r--r--include/mupdf/pdf/annot.h4
-rw-r--r--source/pdf/pdf-annot-edit.c24
-rw-r--r--source/tools/murun.c26
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);