1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
#include "fitz-internal.h"
#include "mupdf-internal.h"
struct pdf_js_s
{
pdf_document *doc;
pdf_obj *form;
pdf_js_event event;
pdf_jsimp *imp;
pdf_jsimp_type *doctype;
pdf_jsimp_type *eventtype;
pdf_jsimp_type *fieldtype;
};
static pdf_obj *load_color(fz_context *ctx, pdf_jsimp *imp, pdf_jsimp_obj *val)
{
pdf_obj *col = NULL;
if (pdf_jsimp_array_len(imp, val) == 4)
{
pdf_obj *comp = NULL;
pdf_jsimp_obj *jscomp = NULL;
int i;
col = pdf_new_array(ctx, 3);
fz_var(comp);
fz_var(jscomp);
fz_try(ctx)
{
for (i = 0; i < 3; i++)
{
jscomp = pdf_jsimp_array_item(imp, val, i+1);
comp = pdf_new_real(ctx, pdf_jsimp_toNumber(imp, jscomp));
pdf_array_push(col, comp);
pdf_jsimp_drop_obj(imp, jscomp);
jscomp = NULL;
pdf_drop_obj(comp);
comp = NULL;
}
}
fz_catch(ctx)
{
pdf_jsimp_drop_obj(imp, jscomp);
pdf_drop_obj(comp);
pdf_drop_obj(col);
fz_rethrow(ctx);
}
}
return col;
}
static pdf_jsimp_obj *field_buttonSetCaption(void *jsctx, void *obj, int argc, pdf_jsimp_obj *args[])
{
pdf_js *js = (pdf_js *)jsctx;
pdf_obj *field = (pdf_obj *)obj;
char *name;
if (argc != 1)
return NULL;
name = pdf_jsimp_toString(js->imp, args[0]);
pdf_field_buttonSetCaption(js->doc, field, name);
return NULL;
}
static pdf_jsimp_obj *field_getFillColor(void *jsctx, void *obj)
{
return NULL;
}
static void field_setFillColor(void *jsctx, void *obj, pdf_jsimp_obj *val)
{
pdf_js *js = (pdf_js *)jsctx;
fz_context *ctx = js->doc->ctx;
pdf_obj *field = (pdf_obj *)obj;
pdf_obj *col = load_color(js->doc->ctx, js->imp, val);
fz_try(ctx)
{
pdf_field_setFillColor(js->doc, field, col);
}
fz_always(ctx)
{
pdf_drop_obj(col);
}
fz_catch(ctx)
{
fz_rethrow(ctx);
}
}
static pdf_jsimp_obj *field_getTextColor(void *jsctx, void *obj)
{
return NULL;
}
static void field_setTextColor(void *jsctx, void *obj, pdf_jsimp_obj *val)
{
pdf_js *js = (pdf_js *)jsctx;
fz_context *ctx = js->doc->ctx;
pdf_obj *field = (pdf_obj *)obj;
pdf_obj *col = load_color(js->doc->ctx, js->imp, val);
fz_try(ctx)
{
pdf_field_setTextColor(js->doc, field, col);
}
fz_always(ctx)
{
pdf_drop_obj(col);
}
fz_catch(ctx)
{
fz_rethrow(ctx);
}
}
static pdf_jsimp_obj *field_getBorderStyle(void *jsctx, void *obj)
{
pdf_js *js = (pdf_js *)jsctx;
pdf_obj *field = (pdf_obj *)obj;
return pdf_jsimp_fromString(js->imp, pdf_field_getBorderStyle(js->doc, field));
}
static void field_setBorderStyle(void *jsctx, void *obj, pdf_jsimp_obj *val)
{
pdf_js *js = (pdf_js *)jsctx;
pdf_obj *field = (pdf_obj *)obj;
pdf_field_setBorderStyle(js->doc, field, pdf_jsimp_toString(js->imp, val));
}
static pdf_jsimp_obj *field_getValue(void *jsctx, void *obj)
{
pdf_js *js = (pdf_js *)jsctx;
pdf_obj *field = (pdf_obj *)obj;
char *fval = pdf_field_getValue(js->doc, field);
return pdf_jsimp_fromString(js->imp, fval?fval:"");
}
static void field_setValue(void *jsctx, void *obj, pdf_jsimp_obj *val)
{
pdf_js *js = (pdf_js *)jsctx;
pdf_obj *field = (pdf_obj *)obj;
pdf_field_setValue(js->doc, field, pdf_jsimp_toString(js->imp, val));
}
static pdf_jsimp_obj *event_getTarget(void *jsctx, void *obj)
{
pdf_js *js = (pdf_js *)jsctx;
return pdf_jsimp_new_obj(js->imp, js->fieldtype, js->event.target);
}
static void event_setTarget(void *jsctx, void *obj, pdf_jsimp_obj *val)
{
pdf_js *js = (pdf_js *)jsctx;
fz_warn(js->doc->ctx, "Unexpected call to event_setTarget");
}
static pdf_jsimp_obj *event_getValue(void *jsctx, void *obj)
{
pdf_js *js = (pdf_js *)jsctx;
char *v = js->event.value;
return pdf_jsimp_fromString(js->imp, v?v:"");
}
static void event_setValue(void *jsctx, void *obj, pdf_jsimp_obj *val)
{
pdf_js *js = (pdf_js *)jsctx;
fz_context *ctx = js->doc->ctx;
fz_free(ctx, js->event.value);
js->event.value = NULL;
js->event.value = fz_strdup(ctx, pdf_jsimp_toString(js->imp, val));
}
static pdf_jsimp_obj *event_getWillCommit(void *jsctx, void *obj)
{
pdf_js *js = (pdf_js *)jsctx;
return pdf_jsimp_fromNumber(js->imp, 1.0);
}
static void event_setWillCommit(void *jsctx, void *obj, pdf_jsimp_obj *val)
{
pdf_js *js = (pdf_js *)jsctx;
fz_warn(js->doc->ctx, "Unexpected call to event_setWillCommit");
}
static pdf_jsimp_obj *event_getRC(void *jsctx, void *obj)
{
pdf_js *js = (pdf_js *)jsctx;
return pdf_jsimp_fromNumber(js->imp, (double)js->event.rc);
}
static void event_setRC(void *jsctx, void *obj, pdf_jsimp_obj *val)
{
pdf_js *js = (pdf_js *)jsctx;
js->event.rc = (int)pdf_jsimp_toNumber(js->imp, val);
}
static pdf_jsimp_obj *doc_getEvent(void *jsctx, void *obj)
{
pdf_js *js = (pdf_js *)jsctx;
return pdf_jsimp_new_obj(js->imp, js->eventtype, &js->event);
}
static void doc_setEvent(void *jsctx, void *obj, pdf_jsimp_obj *val)
{
pdf_js *js = (pdf_js *)jsctx;
fz_warn(js->doc->ctx, "Unexpected call to doc_setEvent");
}
static pdf_obj *find_field(pdf_obj *dict, char *name, int len)
{
pdf_obj *field;
int i, n = pdf_array_len(dict);
for (i = 0; i < n; i++)
{
char *part;
field = pdf_array_get(dict, i);
part = pdf_to_str_buf(pdf_dict_gets(field, "T"));
if (strlen(part) == len && !memcmp(part, name, len))
break;
}
return i < n ? field : NULL;
}
static pdf_jsimp_obj *doc_getField(void *jsctx, void *obj, int argc, pdf_jsimp_obj *args[])
{
pdf_js *js = (pdf_js *)jsctx;
pdf_obj *arr = js->form;
pdf_obj *dict = NULL;
int len;
char *name, *dot;
if (argc != 1)
return NULL;
/* Process the fully qualified field name which has
* the partial names delimited by '.' */
name = pdf_jsimp_toString(js->imp, args[0]);
/* Pretend there was a preceding '.' to simplify the loop */
dot = name - 1;
while (dot && arr)
{
name = dot + 1;
dot = strchr(name, '.');
len = dot ? dot - name : strlen(name);
dict = find_field(arr, name, len);
if (dot)
arr = pdf_dict_gets(dict, "Kids");
}
return dict ? pdf_jsimp_new_obj(js->imp, js->fieldtype, dict) : NULL;
}
static void declare_dom(pdf_js *js)
{
pdf_jsimp *imp = js->imp;
/* Create the document type */
js->doctype = pdf_jsimp_new_type(imp, NULL);
pdf_jsimp_addmethod(imp, js->doctype, "getField", doc_getField);
pdf_jsimp_addproperty(imp, js->doctype, "event", doc_getEvent, doc_setEvent);
/* Create the event type */
js->eventtype = pdf_jsimp_new_type(imp, NULL);
pdf_jsimp_addproperty(imp, js->eventtype, "target", event_getTarget, event_setTarget);
pdf_jsimp_addproperty(imp, js->eventtype, "value", event_getValue, event_setValue);
pdf_jsimp_addproperty(imp, js->eventtype, "willCommit", event_getWillCommit, event_setWillCommit);
pdf_jsimp_addproperty(imp, js->eventtype, "rc", event_getRC, event_setRC);
/* Create the field type */
js->fieldtype = pdf_jsimp_new_type(imp, NULL);
pdf_jsimp_addproperty(imp, js->fieldtype, "value", field_getValue, field_setValue);
pdf_jsimp_addproperty(imp, js->fieldtype, "borderStyle", field_getBorderStyle, field_setBorderStyle);
pdf_jsimp_addproperty(imp, js->fieldtype, "textColor", field_getTextColor, field_setTextColor);
pdf_jsimp_addproperty(imp, js->fieldtype, "fillColor", field_getFillColor, field_setFillColor);
pdf_jsimp_addmethod(imp, js->fieldtype, "buttonSetCaption", field_buttonSetCaption);
/* Create the document object and tell the engine to use */
pdf_jsimp_set_global_type(js->imp, js->doctype);
}
static void preload_helpers(pdf_js *js)
{
/* When testing on the cluster, redefine the Date object
* to use a fixed date */
#ifdef CLUSTER
pdf_jsimp_execute(js->imp,
"var MuPDFOldDate = Date\n"
"Date = function() { return new MuPDFOldDate(1979,5,15); }\n"
);
#endif
pdf_jsimp_execute(js->imp,
#include "../generated/js_util.h"
);
}
pdf_js *pdf_new_js(pdf_document *doc)
{
fz_context *ctx = doc->ctx;
pdf_js *js = NULL;
pdf_obj *javascript = NULL;
char *codebuf = NULL;
fz_var(js);
fz_var(javascript);
fz_var(codebuf);
fz_try(ctx)
{
int len, i;
pdf_obj *root, *acroform;
js = fz_malloc_struct(ctx, pdf_js);
js->doc = doc;
/* Find the form array */
root = pdf_dict_gets(doc->trailer, "Root");
acroform = pdf_dict_gets(root, "AcroForm");
js->form = pdf_dict_gets(acroform, "Fields");
/* Initialise the javascript engine, passing the main context
* for use in memory allocation and exception handling. Also
* pass our js context, for it to pass back to us. */
js->imp = pdf_new_jsimp(ctx, js);
declare_dom(js);
preload_helpers(js);
javascript = pdf_load_name_tree(doc, "JavaScript");
len = pdf_dict_len(javascript);
for (i = 0; i < len; i++)
{
pdf_obj *fragment = pdf_dict_get_val(javascript, i);
pdf_obj *code = pdf_dict_gets(fragment, "JS");
if (pdf_is_stream(doc, pdf_to_num(code), pdf_to_gen(code)))
{
fz_try(ctx)
{
codebuf = pdf_to_utf8(doc, code);
pdf_jsimp_execute(js->imp, codebuf);
}
fz_always(ctx)
{
fz_free(ctx, codebuf);
codebuf = NULL;
}
fz_catch(ctx)
{
fz_warn(ctx, "Warning: %s", ctx->error->message);
}
}
}
}
fz_always(ctx)
{
pdf_drop_obj(javascript);
}
fz_catch(ctx)
{
pdf_drop_js(js);
js = NULL;
}
return 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->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)
{
fz_context *ctx = js->doc->ctx;
js->event.target = e->target;
if (e->value)
{
fz_free(ctx, js->event.value);
js->event.value = NULL;
js->event.value = fz_strdup(ctx, e->value);
}
js->event.rc = 1;
}
}
pdf_js_event *pdf_js_get_event(pdf_js *js)
{
return js ? &js->event : NULL;
}
void pdf_js_execute(pdf_js *js, char *code)
{
if (js)
{
fz_context *ctx = js->doc->ctx;
fz_try(ctx)
{
pdf_jsimp_execute(js->imp, code);
}
fz_catch(ctx)
{
}
}
}
void pdf_js_execute_count(pdf_js *js, char *code, int count)
{
if (js)
{
fz_context *ctx = js->doc->ctx;
fz_try(ctx)
{
pdf_jsimp_execute_count(js->imp, code, count);
}
fz_catch(ctx)
{
}
}
}
|