From 80292f4511278d6ec3c7b3c0cc8d860f1d9bb8c5 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Thu, 20 Mar 2014 16:55:55 +0100 Subject: Break dependencies on pdf-form.c and pdf-js.c Split functions out of pdf-form.c that shouldn't be there, and make javascript initialization explicit. --- source/pdf/js/pdf-js-none.c | 13 ++--- source/pdf/js/pdf-js.c | 54 ++++++++++++------- source/pdf/pdf-appearance.c | 80 +++++++++++++++++++++++++++ source/pdf/pdf-field.c | 48 +++++++++++++++++ source/pdf/pdf-form.c | 128 +------------------------------------------- source/pdf/pdf-xref.c | 5 +- 6 files changed, 170 insertions(+), 158 deletions(-) (limited to 'source/pdf') diff --git a/source/pdf/js/pdf-js-none.c b/source/pdf/js/pdf-js-none.c index 00cc5b54..37597b53 100644 --- a/source/pdf/js/pdf-js-none.c +++ b/source/pdf/js/pdf-js-none.c @@ -1,16 +1,16 @@ #include "mupdf/pdf.h" -pdf_js *pdf_new_js(pdf_document *doc) +void pdf_enable_js(pdf_document *doc) { - return NULL; } -void pdf_js_load_document_level(pdf_js *js) +void pdf_disable_js(pdf_document *doc) { } -void pdf_drop_js(pdf_js *js) +int pdf_js_supported(pdf_document *doc) { + return 0; } void pdf_js_setup_event(pdf_js *js, pdf_js_event *e) @@ -29,8 +29,3 @@ void pdf_js_execute(pdf_js *js, char *code) void pdf_js_execute_count(pdf_js *js, char *code, int count) { } - -int pdf_js_supported(void) -{ - return 0; -} diff --git a/source/pdf/js/pdf-js.c b/source/pdf/js/pdf-js.c index f18da8ac..8364fc95 100644 --- a/source/pdf/js/pdf-js.c +++ b/source/pdf/js/pdf-js.c @@ -770,7 +770,22 @@ static void preload_helpers(pdf_js *js) ); } -pdf_js *pdf_new_js(pdf_document *doc) +static void pdf_drop_js(pdf_js *js) +{ + if (js) + { + fz_context *ctx = js->doc->ctx; + fz_free(ctx, js->event.value); + pdf_jsimp_drop_type(js->imp, js->apptype); + pdf_jsimp_drop_type(js->imp, js->eventtype); + pdf_jsimp_drop_type(js->imp, js->fieldtype); + pdf_jsimp_drop_type(js->imp, js->doctype); + pdf_drop_jsimp(js->imp); + fz_free(ctx, js); + } +} + +static pdf_js *pdf_new_js(pdf_document *doc) { fz_context *ctx = doc->ctx; pdf_js *js = NULL; @@ -805,7 +820,7 @@ pdf_js *pdf_new_js(pdf_document *doc) return js; } -void pdf_js_load_document_level(pdf_js *js) +static void pdf_js_load_document_level(pdf_js *js) { pdf_document *doc = js->doc; fz_context *ctx = doc->ctx; @@ -854,21 +869,6 @@ void pdf_js_load_document_level(pdf_js *js) } } -void pdf_drop_js(pdf_js *js) -{ - if (js) - { - fz_context *ctx = js->doc->ctx; - fz_free(ctx, js->event.value); - pdf_jsimp_drop_type(js->imp, js->apptype); - pdf_jsimp_drop_type(js->imp, js->eventtype); - pdf_jsimp_drop_type(js->imp, js->fieldtype); - pdf_jsimp_drop_type(js->imp, js->doctype); - pdf_drop_jsimp(js->imp); - fz_free(ctx, js); - } -} - void pdf_js_setup_event(pdf_js *js, pdf_js_event *e) { if (js) @@ -920,7 +920,23 @@ void pdf_js_execute_count(pdf_js *js, char *code, int count) } } -int pdf_js_supported(void) +void pdf_enable_js(pdf_document *doc) +{ + if (!doc->js) { + doc->js = pdf_new_js(doc); + doc->drop_js = pdf_drop_js; + pdf_js_load_document_level(doc->js); + } +} + +void pdf_disable_js(pdf_document *doc) +{ + if (doc->js) + doc->drop_js(doc->js); + doc->js = NULL; +} + +int pdf_js_supported(pdf_document *doc) { - return 1; + return doc->js != NULL; } diff --git a/source/pdf/pdf-appearance.c b/source/pdf/pdf-appearance.c index 2d1a6b54..db11c32a 100644 --- a/source/pdf/pdf-appearance.c +++ b/source/pdf/pdf-appearance.c @@ -2207,3 +2207,83 @@ void pdf_set_signature_appearance(pdf_document *doc, pdf_annot *annot, char *nam fz_rethrow(ctx); } } + +void pdf_update_appearance(pdf_document *doc, pdf_annot *annot) +{ + pdf_obj *obj = annot->obj; + if (!pdf_dict_gets(obj, "AP") || pdf_obj_is_dirty(obj)) + { + fz_annot_type type = pdf_annot_obj_type(obj); + switch (type) + { + case FZ_ANNOT_WIDGET: + switch (pdf_field_type(doc, obj)) + { + case PDF_WIDGET_TYPE_TEXT: + { + #if 0 + pdf_obj *formatting = pdf_dict_getp(obj, "AA/F"); + if (formatting && doc->js) + { + /* Apply formatting */ + pdf_js_event e; + fz_context *ctx = doc->ctx; + + e.target = obj; + e.value = pdf_field_value(doc, obj); + fz_try(ctx) + { + pdf_js_setup_event(doc->js, &e); + } + fz_always(ctx) + { + fz_free(ctx, e.value); + } + fz_catch(ctx) + { + fz_rethrow(ctx); + } + execute_action(doc, obj, formatting); + /* Update appearance from JS event.value */ + pdf_update_text_appearance(doc, obj, pdf_js_get_event(doc->js)->value); + } + else + #endif + { + /* Update appearance from field value */ + pdf_update_text_appearance(doc, obj, NULL); + } + } + break; + case PDF_WIDGET_TYPE_PUSHBUTTON: + pdf_update_pushbutton_appearance(doc, obj); + break; + case PDF_WIDGET_TYPE_LISTBOX: + case PDF_WIDGET_TYPE_COMBOBOX: + /* Treating listbox and combobox identically for now, + * and the behaviour is most appropriate for a combobox */ + pdf_update_combobox_appearance(doc, obj); + break; + } + break; + case FZ_ANNOT_TEXT: + pdf_update_text_annot_appearance(doc, annot); + break; + case FZ_ANNOT_FREETEXT: + pdf_update_free_text_annot_appearance(doc, annot); + break; + case FZ_ANNOT_STRIKEOUT: + case FZ_ANNOT_UNDERLINE: + case FZ_ANNOT_HIGHLIGHT: + pdf_update_text_markup_appearance(doc, annot, type); + break; + case FZ_ANNOT_INK: + pdf_update_ink_appearance(doc, annot); + break; + default: + break; + } + + pdf_clean_obj(obj); + } +} diff --git a/source/pdf/pdf-field.c b/source/pdf/pdf-field.c index 127d92a5..e8f2508f 100644 --- a/source/pdf/pdf-field.c +++ b/source/pdf/pdf-field.c @@ -15,6 +15,54 @@ pdf_obj *pdf_get_inheritable(pdf_document *doc, pdf_obj *obj, char *key) return fobj ? fobj : pdf_dict_gets(pdf_dict_gets(pdf_dict_gets(pdf_trailer(doc), "Root"), "AcroForm"), key); } +char *pdf_get_string_or_stream(pdf_document *doc, pdf_obj *obj) +{ + fz_context *ctx = doc->ctx; + int len = 0; + char *buf = NULL; + fz_buffer *strmbuf = NULL; + char *text = NULL; + + fz_var(strmbuf); + fz_var(text); + fz_try(ctx) + { + if (pdf_is_string(obj)) + { + len = pdf_to_str_len(obj); + buf = pdf_to_str_buf(obj); + } + else if (pdf_is_stream(doc, pdf_to_num(obj), pdf_to_gen(obj))) + { + strmbuf = pdf_load_stream(doc, pdf_to_num(obj), pdf_to_gen(obj)); + len = fz_buffer_storage(ctx, strmbuf, (unsigned char **)&buf); + } + + if (buf) + { + text = fz_malloc(ctx, len+1); + memcpy(text, buf, len); + text[len] = 0; + } + } + fz_always(ctx) + { + fz_drop_buffer(ctx, strmbuf); + } + fz_catch(ctx) + { + fz_free(ctx, text); + fz_rethrow(ctx); + } + + return text; +} + +char *pdf_field_value(pdf_document *doc, pdf_obj *field) +{ + return pdf_get_string_or_stream(doc, pdf_get_inheritable(doc, field, "V")); +} + int pdf_get_field_flags(pdf_document *doc, pdf_obj *obj) { return pdf_to_int(pdf_get_inheritable(doc, obj, "Ff")); diff --git a/source/pdf/pdf-form.c b/source/pdf/pdf-form.c index a659192b..ab5a808c 100644 --- a/source/pdf/pdf-form.c +++ b/source/pdf/pdf-form.c @@ -15,49 +15,6 @@ enum SigFlag_AppendOnly = 2 }; -static char *get_string_or_stream(pdf_document *doc, pdf_obj *obj) -{ - fz_context *ctx = doc->ctx; - int len = 0; - char *buf = NULL; - fz_buffer *strmbuf = NULL; - char *text = NULL; - - fz_var(strmbuf); - fz_var(text); - fz_try(ctx) - { - if (pdf_is_string(obj)) - { - len = pdf_to_str_len(obj); - buf = pdf_to_str_buf(obj); - } - else if (pdf_is_stream(doc, pdf_to_num(obj), pdf_to_gen(obj))) - { - strmbuf = pdf_load_stream(doc, pdf_to_num(obj), pdf_to_gen(obj)); - len = fz_buffer_storage(ctx, strmbuf, (unsigned char **)&buf); - } - - if (buf) - { - text = fz_malloc(ctx, len+1); - memcpy(text, buf, len); - text[len] = 0; - } - } - fz_always(ctx) - { - fz_drop_buffer(ctx, strmbuf); - } - fz_catch(ctx) - { - fz_free(ctx, text); - fz_rethrow(ctx); - } - - return text; -} - /* Find the point in a field hierarchy where all descendents * share the same name */ static pdf_obj *find_head_of_field_group(pdf_obj *obj) @@ -408,84 +365,6 @@ static void execute_action(pdf_document *doc, pdf_obj *obj, pdf_obj *a) } } -void pdf_update_appearance(pdf_document *doc, pdf_annot *annot) -{ - pdf_obj *obj = annot->obj; - if (!pdf_dict_gets(obj, "AP") || pdf_obj_is_dirty(obj)) - { - fz_annot_type type = pdf_annot_obj_type(obj); - switch (type) - { - case FZ_ANNOT_WIDGET: - switch (pdf_field_type(doc, obj)) - { - case PDF_WIDGET_TYPE_TEXT: - { - pdf_obj *formatting = pdf_dict_getp(obj, "AA/F"); - if (formatting && doc->js) - { - /* Apply formatting */ - pdf_js_event e; - fz_context *ctx = doc->ctx; - - e.target = obj; - e.value = pdf_field_value(doc, obj); - fz_try(ctx) - { - pdf_js_setup_event(doc->js, &e); - } - fz_always(ctx) - { - fz_free(ctx, e.value); - } - fz_catch(ctx) - { - fz_rethrow(ctx); - } - execute_action(doc, obj, formatting); - /* Update appearance from JS event.value */ - pdf_update_text_appearance(doc, obj, pdf_js_get_event(doc->js)->value); - } - else - { - /* Update appearance from field value */ - pdf_update_text_appearance(doc, obj, NULL); - } - } - break; - case PDF_WIDGET_TYPE_PUSHBUTTON: - pdf_update_pushbutton_appearance(doc, obj); - break; - case PDF_WIDGET_TYPE_LISTBOX: - case PDF_WIDGET_TYPE_COMBOBOX: - /* Treating listbox and combobox identically for now, - * and the behaviour is most appropriate for a combobox */ - pdf_update_combobox_appearance(doc, obj); - break; - } - break; - case FZ_ANNOT_TEXT: - pdf_update_text_annot_appearance(doc, annot); - break; - case FZ_ANNOT_FREETEXT: - pdf_update_free_text_annot_appearance(doc, annot); - break; - case FZ_ANNOT_STRIKEOUT: - case FZ_ANNOT_UNDERLINE: - case FZ_ANNOT_HIGHLIGHT: - pdf_update_text_markup_appearance(doc, annot, type); - break; - case FZ_ANNOT_INK: - pdf_update_ink_appearance(doc, annot); - break; - default: - break; - } - - pdf_clean_obj(obj); - } -} - static void execute_action_chain(pdf_document *doc, pdf_obj *obj) { pdf_obj *a = pdf_dict_gets(obj, "A"); @@ -972,11 +851,6 @@ int pdf_widget_get_type(pdf_widget *widget) return annot->widget_type; } -char *pdf_field_value(pdf_document *doc, pdf_obj *field) -{ - return get_string_or_stream(doc, pdf_get_inheritable(doc, field, "V")); -} - static int set_text_field_value(pdf_document *doc, pdf_obj *field, char *text) { pdf_obj *v = pdf_dict_getp(field, "AA/V"); @@ -1375,7 +1249,7 @@ int pdf_text_widget_content_type(pdf_document *doc, pdf_widget *tw) fz_var(code); fz_try(ctx) { - code = get_string_or_stream(doc, pdf_dict_getp(annot->obj, "AA/F/JS")); + code = pdf_get_string_or_stream(doc, pdf_dict_getp(annot->obj, "AA/F/JS")); if (code) { if (strstr(code, "AFNumber_Format")) diff --git a/source/pdf/pdf-xref.c b/source/pdf/pdf-xref.c index ef238fb1..9bf735da 100644 --- a/source/pdf/pdf-xref.c +++ b/source/pdf/pdf-xref.c @@ -1168,8 +1168,6 @@ pdf_init_document(pdf_document *doc) dict = NULL; } } - doc->js = pdf_new_js(doc); - pdf_js_load_document_level(doc->js); } fz_catch(ctx) { @@ -1218,7 +1216,8 @@ pdf_close_document(pdf_document *doc) * glyph cache at this point. */ fz_purge_glyph_cache(ctx); - pdf_drop_js(doc->js); + if (doc->js) + doc->drop_js(doc->js); pdf_free_xref_sections(doc); -- cgit v1.2.3