diff options
author | Fred Ross-Perry <fred.ross-perry@artifex.com> | 2017-11-13 17:04:06 -0800 |
---|---|---|
committer | Tor Andersson <tor.andersson@artifex.com> | 2017-11-22 23:09:51 +0100 |
commit | ed28a166dace50d1a555689b9d5353e62a1b1e69 (patch) | |
tree | 67dca9996a83d5939b7e3e4a91da8685a4764291 /source | |
parent | 52b6e193ba376f46dec2ae3b8c316f2e08fefc13 (diff) | |
download | mupdf-ed28a166dace50d1a555689b9d5353e62a1b1e69.tar.xz |
jni/js: Use correct text encoding in annotation author and contents.
Also clarify that a copy of author/contents is returned, and that
the caller must free them.
Diffstat (limited to 'source')
-rw-r--r-- | source/pdf/pdf-annot-edit.c | 55 | ||||
-rw-r--r-- | source/tools/murun.c | 48 |
2 files changed, 92 insertions, 11 deletions
diff --git a/source/pdf/pdf-annot-edit.c b/source/pdf/pdf-annot-edit.c index 20aeff11..09e63002 100644 --- a/source/pdf/pdf-annot-edit.c +++ b/source/pdf/pdf-annot-edit.c @@ -259,17 +259,18 @@ pdf_set_annot_rect(fz_context *ctx, pdf_annot *annot, const fz_rect *rect) pdf_dirty_annot(ctx, annot); } -const char * -pdf_annot_contents(fz_context *ctx, pdf_annot *annot) +char * +pdf_copy_annot_contents(fz_context *ctx, pdf_annot *annot) { - return pdf_to_str_buf(ctx, pdf_dict_get(ctx, annot->obj, PDF_NAME_Contents)); + return pdf_to_utf8(ctx, pdf_dict_get(ctx, annot->obj, PDF_NAME_Contents)); } void pdf_set_annot_contents(fz_context *ctx, pdf_annot *annot, const char *text) { pdf_document *doc = annot->page->doc; - pdf_dict_put_drop(ctx, annot->obj, PDF_NAME_Contents, pdf_new_string(ctx, doc, text, strlen(text))); + pdf_dict_put_drop(ctx, annot->obj, PDF_NAME_Contents, + pdf_new_text_string(ctx, doc, text)); pdf_dirty_annot(ctx, annot); } @@ -842,11 +843,25 @@ pdf_set_text_annot_position(fz_context *ctx, pdf_annot *annot, fz_point pt) pdf_dict_put_drop(ctx, annot->obj, PDF_NAME_F, pdf_new_int(ctx, doc, flags)); } -const char * -pdf_annot_author(fz_context *ctx, pdf_annot *annot) -{ - return pdf_to_str_buf(ctx, pdf_dict_get(ctx, annot->obj, PDF_NAME_T)); -} +static pdf_obj *markup_subtypes[] = { + PDF_NAME_Text, + PDF_NAME_FreeText, + PDF_NAME_Line, + PDF_NAME_Square, + PDF_NAME_Circle, + PDF_NAME_Polygon, + PDF_NAME_PolyLine, + PDF_NAME_Highlight, + PDF_NAME_Underline, + PDF_NAME_Squiggly, + PDF_NAME_StrikeOut, + PDF_NAME_Stamp, + PDF_NAME_Caret, + PDF_NAME_Ink, + PDF_NAME_FileAttachment, + PDF_NAME_Sound, + NULL, +}; const char * pdf_annot_date(fz_context *ctx, pdf_annot *annot) @@ -855,6 +870,28 @@ pdf_annot_date(fz_context *ctx, pdf_annot *annot) return pdf_to_str_buf(ctx, pdf_dict_get(ctx, annot->obj, PDF_NAME_CreationDate)); } +int +pdf_annot_has_author(fz_context *ctx, pdf_annot *annot) +{ + return is_allowed_subtype(ctx, annot, PDF_NAME_T, markup_subtypes); +} + +char * +pdf_copy_annot_author(fz_context *ctx, pdf_annot *annot) +{ + check_allowed_subtypes(ctx, annot, PDF_NAME_T, markup_subtypes); + return pdf_to_utf8(ctx, pdf_dict_get(ctx, annot->obj, PDF_NAME_T)); +} + +void +pdf_set_annot_author(fz_context *ctx, pdf_annot *annot, const char *author) +{ + pdf_document *doc = annot->page->doc; + check_allowed_subtypes(ctx, annot, PDF_NAME_T, markup_subtypes); + pdf_dict_put_drop(ctx, annot->obj, PDF_NAME_T, pdf_new_text_string(ctx, doc, author)); + pdf_dirty_annot(ctx, annot); +} + pdf_obj * pdf_annot_irt(fz_context *ctx, pdf_annot *annot) { diff --git a/source/tools/murun.c b/source/tools/murun.c index e90821c4..69765164 100644 --- a/source/tools/murun.c +++ b/source/tools/murun.c @@ -3988,12 +3988,21 @@ static void ffi_PDFAnnotation_getContents(js_State *J) { fz_context *ctx = js_getcontext(J); pdf_annot *annot = js_touserdata(J, 0, "pdf_annot"); - const char *contents = NULL; + char *contents = NULL; + fz_try(ctx) - contents = pdf_annot_contents(ctx, annot); + contents = pdf_copy_annot_contents(ctx, annot); fz_catch(ctx) rethrow(J); + + if (js_try(J)) { + fz_free(ctx, contents); + js_throw(J); + } js_pushstring(J, contents); + js_endtry(J); + + fz_free(ctx, contents); } static void ffi_PDFAnnotation_setContents(js_State *J) @@ -4270,6 +4279,39 @@ static void ffi_PDFAnnotation_setInkList(js_State *J) rethrow(J); } +static void ffi_PDFAnnotation_getAuthor(js_State *J) +{ + fz_context *ctx = js_getcontext(J); + pdf_annot *annot = js_touserdata(J, 0, "pdf_annot"); + char *author = NULL; + + fz_try(ctx) + author = pdf_copy_annot_author(ctx, annot); + fz_catch(ctx) + rethrow(J); + + if (js_try(J)) { + fz_free(ctx, author); + js_throw(J); + } + js_pushstring(J, author); + js_endtry(J); + + fz_free(ctx, author); +} + +static void ffi_PDFAnnotation_setAuthor(js_State *J) +{ + fz_context *ctx = js_getcontext(J); + pdf_annot *annot = js_touserdata(J, 0, "pdf_annot"); + const char *author = js_tostring(J, 1); + + fz_try(ctx) + pdf_set_annot_author(ctx, annot, author); + fz_catch(ctx) + rethrow(J); +} + static void ffi_PDFAnnotation_updateAppearance(js_State *J) { fz_context *ctx = js_getcontext(J); @@ -4599,6 +4641,8 @@ int murun_main(int argc, char **argv) jsB_propfun(J, "PDFAnnotation.setQuadPoints", ffi_PDFAnnotation_setQuadPoints, 1); jsB_propfun(J, "PDFAnnotation.getInkList", ffi_PDFAnnotation_getInkList, 0); jsB_propfun(J, "PDFAnnotation.setInkList", ffi_PDFAnnotation_setInkList, 1); + jsB_propfun(J, "PDFAnnotation.getAuthor", ffi_PDFAnnotation_getAuthor, 0); + jsB_propfun(J, "PDFAnnotation.setAuthor", ffi_PDFAnnotation_setAuthor, 1); jsB_propfun(J, "PDFAnnotation.updateAppearance", ffi_PDFAnnotation_updateAppearance, 0); } js_setregistry(J, "pdf_annot"); |