Coverage Report

Created: 2024-05-20 06:23

/src/mupdf/source/pdf/pdf-form.c
Line
Count
Source (jump to first uncovered line)
1
// Copyright (C) 2004-2022 Artifex Software, Inc.
2
//
3
// This file is part of MuPDF.
4
//
5
// MuPDF is free software: you can redistribute it and/or modify it under the
6
// terms of the GNU Affero General Public License as published by the Free
7
// Software Foundation, either version 3 of the License, or (at your option)
8
// any later version.
9
//
10
// MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
11
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
13
// details.
14
//
15
// You should have received a copy of the GNU Affero General Public License
16
// along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
17
//
18
// Alternative licensing terms are available from the licensor.
19
// For commercial licensing, see <https://www.artifex.com/> or contact
20
// Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
21
// CA 94129, USA, for further information.
22
23
#include "mupdf/fitz.h"
24
#include "pdf-annot-imp.h"
25
26
#include <string.h>
27
28
/* Must be kept in sync with definitions in pdf_util.js */
29
enum
30
{
31
  Display_Visible,
32
  Display_Hidden,
33
  Display_NoPrint,
34
  Display_NoView
35
};
36
37
enum
38
{
39
  SigFlag_SignaturesExist = 1,
40
  SigFlag_AppendOnly = 2
41
};
42
43
const char *pdf_field_value(fz_context *ctx, pdf_obj *field)
44
3.45k
{
45
3.45k
  pdf_obj *v = pdf_dict_get_inheritable(ctx, field, PDF_NAME(V));
46
3.45k
  if (pdf_is_name(ctx, v))
47
18
    return pdf_to_name(ctx, v);
48
3.43k
  if (pdf_is_stream(ctx, v))
49
0
  {
50
    // FIXME: pdf_dict_put_inheritable...
51
0
    char *str = pdf_new_utf8_from_pdf_stream_obj(ctx, v);
52
0
    fz_try(ctx)
53
0
      pdf_dict_put_text_string(ctx, field, PDF_NAME(V), str);
54
0
    fz_always(ctx)
55
0
      fz_free(ctx, str);
56
0
    fz_catch(ctx)
57
0
      fz_rethrow(ctx);
58
0
    v = pdf_dict_get(ctx, field, PDF_NAME(V));
59
0
  }
60
3.43k
  return pdf_to_text_string(ctx, v);
61
3.43k
}
62
63
int pdf_field_flags(fz_context *ctx, pdf_obj *obj)
64
4.07k
{
65
4.07k
  return pdf_dict_get_inheritable_int(ctx, obj, PDF_NAME(Ff));
66
4.07k
}
67
68
int pdf_field_type(fz_context *ctx, pdf_obj *obj)
69
0
{
70
0
  pdf_obj *type = pdf_dict_get_inheritable(ctx, obj, PDF_NAME(FT));
71
0
  int flags = pdf_field_flags(ctx, obj);
72
0
  if (pdf_name_eq(ctx, type, PDF_NAME(Btn)))
73
0
  {
74
0
    if (flags & PDF_BTN_FIELD_IS_PUSHBUTTON)
75
0
      return PDF_WIDGET_TYPE_BUTTON;
76
0
    else if (flags & PDF_BTN_FIELD_IS_RADIO)
77
0
      return PDF_WIDGET_TYPE_RADIOBUTTON;
78
0
    else
79
0
      return PDF_WIDGET_TYPE_CHECKBOX;
80
0
  }
81
0
  else if (pdf_name_eq(ctx, type, PDF_NAME(Tx)))
82
0
    return PDF_WIDGET_TYPE_TEXT;
83
0
  else if (pdf_name_eq(ctx, type, PDF_NAME(Ch)))
84
0
  {
85
0
    if (flags & PDF_CH_FIELD_IS_COMBO)
86
0
      return PDF_WIDGET_TYPE_COMBOBOX;
87
0
    else
88
0
      return PDF_WIDGET_TYPE_LISTBOX;
89
0
  }
90
0
  else if (pdf_name_eq(ctx, type, PDF_NAME(Sig)))
91
0
    return PDF_WIDGET_TYPE_SIGNATURE;
92
0
  else
93
0
    return PDF_WIDGET_TYPE_BUTTON;
94
0
}
95
96
const char *pdf_field_type_string(fz_context *ctx, pdf_obj *obj)
97
0
{
98
0
  switch (pdf_field_type(ctx, obj))
99
0
  {
100
0
  default:
101
0
  case PDF_WIDGET_TYPE_BUTTON: return "button";
102
0
  case PDF_WIDGET_TYPE_CHECKBOX: return "checkbox";
103
0
  case PDF_WIDGET_TYPE_COMBOBOX: return "combobox";
104
0
  case PDF_WIDGET_TYPE_LISTBOX: return "listbox";
105
0
  case PDF_WIDGET_TYPE_RADIOBUTTON: return "radiobutton";
106
0
  case PDF_WIDGET_TYPE_SIGNATURE: return "signature";
107
0
  case PDF_WIDGET_TYPE_TEXT: return "text";
108
0
  }
109
0
}
110
111
/* Find the point in a field hierarchy where all descendants
112
 * share the same name */
113
static pdf_obj *find_head_of_field_group(fz_context *ctx, pdf_obj *obj)
114
0
{
115
0
  if (obj == NULL || pdf_dict_get(ctx, obj, PDF_NAME(T)))
116
0
    return obj;
117
0
  else
118
0
    return find_head_of_field_group(ctx, pdf_dict_get(ctx, obj, PDF_NAME(Parent)));
119
0
}
120
121
static void pdf_field_mark_dirty(fz_context *ctx, pdf_obj *field)
122
0
{
123
0
  pdf_document *doc = pdf_get_bound_document(ctx, field);
124
0
  pdf_obj *kids = pdf_dict_get(ctx, field, PDF_NAME(Kids));
125
0
  if (kids)
126
0
  {
127
0
    int i, n = pdf_array_len(ctx, kids);
128
0
    for (i = 0; i < n; i++)
129
0
      pdf_field_mark_dirty(ctx, pdf_array_get(ctx, kids, i));
130
0
  }
131
0
  pdf_dirty_obj(ctx, field);
132
0
  if (doc)
133
0
    doc->resynth_required = 1;
134
0
}
135
136
static void update_field_value(fz_context *ctx, pdf_document *doc, pdf_obj *obj, const char *text)
137
0
{
138
0
  const char *old_text;
139
0
  pdf_obj *grp;
140
141
0
  if (!text)
142
0
    text = "";
143
144
  /* All fields of the same name should be updated, so
145
   * set the value at the head of the group */
146
0
  grp = find_head_of_field_group(ctx, obj);
147
0
  if (grp)
148
0
    obj = grp;
149
150
  /* Only update if we change the actual value. */
151
0
  old_text = pdf_dict_get_text_string(ctx, obj, PDF_NAME(V));
152
0
  if (old_text && !strcmp(old_text, text))
153
0
    return;
154
155
0
  pdf_dict_put_text_string(ctx, obj, PDF_NAME(V), text);
156
157
0
  pdf_field_mark_dirty(ctx, obj);
158
0
}
159
160
static pdf_obj *
161
pdf_lookup_field_imp(fz_context *ctx, pdf_obj *arr, const char *str, pdf_cycle_list *cycle_up);
162
163
static pdf_obj *
164
lookup_field_sub(fz_context *ctx, pdf_obj *dict, const char *str, pdf_cycle_list *cycle_up)
165
0
{
166
0
  pdf_obj *kids;
167
0
  pdf_obj *name;
168
169
0
  name = pdf_dict_get(ctx, dict, PDF_NAME(T));
170
171
  /* If we have a name, check it matches. If it matches, consume that
172
   * portion of str. If not, exit. */
173
0
  if (name)
174
0
  {
175
0
    const char *match = pdf_to_text_string(ctx, name);
176
0
    const char *e = str;
177
0
    size_t len;
178
0
    while (*e && *e != '.')
179
0
      e++;
180
0
    len = e-str;
181
0
    if (strncmp(str, match, len) != 0 || (match[len] != 0 && match[len] != '.'))
182
      /* name doesn't match. */
183
0
      return NULL;
184
0
    str = e;
185
0
    if (*str == '.')
186
0
      str++;
187
0
  }
188
189
  /* If there is a kids array, but the search string is not empty, we have
190
  encountered an internal field which represents a set of terminal fields. */
191
192
  /* If there is a kids array and the search string is not empty,
193
  walk those looking for the appropriate one. */
194
0
  kids = pdf_dict_get(ctx, dict, PDF_NAME(Kids));
195
0
  if (kids && *str != 0)
196
0
    return pdf_lookup_field_imp(ctx, kids, str, cycle_up);
197
198
  /* The field may be a terminal or an internal field at this point.
199
  Accept it as the match if the match string is exhausted. */
200
0
  if (*str == 0)
201
0
    return dict;
202
203
0
  return NULL;
204
0
}
205
206
static pdf_obj *
207
pdf_lookup_field_imp(fz_context *ctx, pdf_obj *arr, const char *str, pdf_cycle_list *cycle_up)
208
0
{
209
0
  pdf_cycle_list cycle;
210
0
  int len = pdf_array_len(ctx, arr);
211
0
  int i;
212
213
0
  for (i = 0; i < len; i++)
214
0
  {
215
0
    pdf_obj *k = pdf_array_get(ctx, arr, i);
216
0
    pdf_obj *found;
217
0
    if (pdf_cycle(ctx, &cycle, cycle_up, k))
218
0
      fz_throw(ctx, FZ_ERROR_FORMAT, "cycle in fields");
219
0
    found = lookup_field_sub(ctx, k, str, &cycle);
220
0
    if (found)
221
0
      return found;
222
0
  }
223
224
0
  return NULL;
225
0
}
226
227
pdf_obj *
228
pdf_lookup_field(fz_context *ctx, pdf_obj *arr, const char *str)
229
0
{
230
0
  return pdf_lookup_field_imp(ctx, arr, str, NULL);
231
0
}
232
233
static void reset_form_field(fz_context *ctx, pdf_document *doc, pdf_obj *field)
234
0
{
235
  /* Set V to DV wherever DV is present, and delete V where DV is not.
236
   * FIXME: we assume for now that V has not been set unequal
237
   * to DV higher in the hierarchy than "field".
238
   *
239
   * At the bottom of the hierarchy we may find widget annotations
240
   * that aren't also fields, but DV and V will not be present in their
241
   * dictionaries, and attempts to remove V will be harmless. */
242
0
  pdf_obj *dv = pdf_dict_get(ctx, field, PDF_NAME(DV));
243
0
  pdf_obj *kids = pdf_dict_get(ctx, field, PDF_NAME(Kids));
244
245
0
  if (dv)
246
0
    pdf_dict_put(ctx, field, PDF_NAME(V), dv);
247
0
  else
248
0
    pdf_dict_del(ctx, field, PDF_NAME(V));
249
250
0
  if (kids == NULL)
251
0
  {
252
    /* The leaves of the tree are widget annotations
253
     * In some cases we need to update the appearance state;
254
     * in others we need to mark the field as dirty so that
255
     * the appearance stream will be regenerated. */
256
0
    switch (pdf_field_type(ctx, field))
257
0
    {
258
0
    case PDF_WIDGET_TYPE_CHECKBOX:
259
0
    case PDF_WIDGET_TYPE_RADIOBUTTON:
260
0
      {
261
0
        pdf_obj *leafv = pdf_dict_get_inheritable(ctx, field, PDF_NAME(V));
262
0
        pdf_obj *ap = pdf_dict_get(ctx, field, PDF_NAME(AP));
263
0
        pdf_obj *n = pdf_dict_get(ctx, ap, PDF_NAME(N));
264
265
        /* Value does not refer to any appearance state in the
266
        normal appearance stream dictionary, default to Off instead. */
267
0
        if (pdf_is_dict(ctx, n) && !pdf_dict_get(ctx, n, leafv))
268
0
          leafv = NULL;
269
270
0
        if (!pdf_is_name(ctx, leafv))
271
0
          leafv = PDF_NAME(Off);
272
0
        pdf_dict_put(ctx, field, PDF_NAME(AS), leafv);
273
0
      }
274
0
      pdf_field_mark_dirty(ctx, field);
275
0
      break;
276
277
0
    case PDF_WIDGET_TYPE_BUTTON:
278
0
    case PDF_WIDGET_TYPE_SIGNATURE:
279
      /* Pushbuttons and signatures have no value to reset. */
280
0
      break;
281
282
0
    default:
283
0
      pdf_field_mark_dirty(ctx, field);
284
0
      break;
285
0
    }
286
0
  }
287
0
}
288
289
void pdf_field_reset(fz_context *ctx, pdf_document *doc, pdf_obj *field)
290
0
{
291
0
  pdf_obj *kids = pdf_dict_get(ctx, field, PDF_NAME(Kids));
292
293
0
  reset_form_field(ctx, doc, field);
294
295
0
  if (kids)
296
0
  {
297
0
    int i, n = pdf_array_len(ctx, kids);
298
299
0
    for (i = 0; i < n; i++)
300
0
      pdf_field_reset(ctx, doc, pdf_array_get(ctx, kids, i));
301
0
  }
302
0
}
303
304
static void add_field_hierarchy_to_array(fz_context *ctx, pdf_obj *array, pdf_obj *field, pdf_obj *fields, int exclude)
305
0
{
306
0
  pdf_obj *kids = pdf_dict_get(ctx, field, PDF_NAME(Kids));
307
0
  char *needle = pdf_load_field_name(ctx, field);
308
0
  int i, n;
309
310
0
  fz_try(ctx)
311
0
  {
312
0
    n = pdf_array_len(ctx, fields);
313
0
    for (i = 0; i < n; i++)
314
0
    {
315
0
      char *name = pdf_load_field_name(ctx, pdf_array_get(ctx, fields, i));
316
0
      int found = !strcmp(needle, name);
317
0
      fz_free(ctx, name);
318
0
      if (found)
319
0
        break;
320
0
    }
321
0
  }
322
0
  fz_always(ctx)
323
0
    fz_free(ctx, needle);
324
0
  fz_catch(ctx)
325
0
    fz_rethrow(ctx);
326
327
0
  if ((exclude && i < n) || (!exclude && i == n))
328
0
    return;
329
330
0
  pdf_array_push(ctx, array, field);
331
332
0
  if (kids)
333
0
  {
334
0
    int i, n = pdf_array_len(ctx, kids);
335
336
0
    for (i = 0; i < n; i++)
337
0
      add_field_hierarchy_to_array(ctx, array, pdf_array_get(ctx, kids, i), fields, exclude);
338
0
  }
339
0
}
340
341
/*
342
  When resetting or submitting a form, the fields to act upon are defined
343
  by an array of either field references or field names, plus a flag determining
344
  whether to act upon the fields in the array, or all fields other than those in
345
  the array. specified_fields interprets this information and produces the array
346
  of fields to be acted upon.
347
*/
348
static pdf_obj *specified_fields(fz_context *ctx, pdf_document *doc, pdf_obj *fields, int exclude)
349
0
{
350
0
  pdf_obj *form = pdf_dict_getl(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root), PDF_NAME(AcroForm), PDF_NAME(Fields), NULL);
351
0
  int i, n;
352
0
  pdf_obj *result = pdf_new_array(ctx, doc, 0);
353
354
0
  fz_try(ctx)
355
0
  {
356
0
    n = pdf_array_len(ctx, fields);
357
358
0
    for (i = 0; i < n; i++)
359
0
    {
360
0
      pdf_obj *field = pdf_array_get(ctx, fields, i);
361
362
0
      if (pdf_is_string(ctx, field))
363
0
        field = pdf_lookup_field(ctx, form, pdf_to_str_buf(ctx, field));
364
365
0
      if (field)
366
0
        add_field_hierarchy_to_array(ctx, result, field, fields, exclude);
367
0
    }
368
0
  }
369
0
  fz_catch(ctx)
370
0
  {
371
0
    pdf_drop_obj(ctx, result);
372
0
    fz_rethrow(ctx);
373
0
  }
374
375
0
  return result;
376
0
}
377
378
void pdf_reset_form(fz_context *ctx, pdf_document *doc, pdf_obj *fields, int exclude)
379
0
{
380
0
  pdf_obj *sfields = specified_fields(ctx, doc, fields, exclude);
381
0
  fz_try(ctx)
382
0
  {
383
0
    int i, n = pdf_array_len(ctx, sfields);
384
0
    for (i = 0; i < n; i++)
385
0
      reset_form_field(ctx, doc, pdf_array_get(ctx, sfields, i));
386
0
    doc->recalculate = 1;
387
0
  }
388
0
  fz_always(ctx)
389
0
    pdf_drop_obj(ctx, sfields);
390
0
  fz_catch(ctx)
391
0
    fz_rethrow(ctx);
392
0
}
393
394
typedef struct
395
{
396
  pdf_obj *pageobj;
397
  pdf_obj *chk;
398
} lookup_state;
399
400
static void *find_widget_on_page(fz_context *ctx, fz_page *page_, void *state_)
401
0
{
402
0
  lookup_state *state = (lookup_state *) state_;
403
0
  pdf_page *page = (pdf_page *) page_;
404
0
  pdf_annot *widget;
405
406
0
  if (state->pageobj && pdf_objcmp_resolve(ctx, state->pageobj, page->obj))
407
0
    return NULL;
408
409
0
  for (widget = pdf_first_widget(ctx, page); widget != NULL; widget = pdf_next_widget(ctx, widget))
410
0
  {
411
0
    if (!pdf_objcmp_resolve(ctx, state->chk, widget->obj))
412
0
      return widget;
413
0
  }
414
415
0
  return NULL;
416
0
}
417
418
static pdf_annot *find_widget(fz_context *ctx, pdf_document *doc, pdf_obj *chk)
419
0
{
420
0
  lookup_state state;
421
422
0
  state.pageobj = pdf_dict_get(ctx, chk, PDF_NAME(P));
423
0
  state.chk = chk;
424
425
0
  return fz_process_opened_pages(ctx, (fz_document *) doc, find_widget_on_page, &state);
426
0
}
427
428
static void set_check(fz_context *ctx, pdf_document *doc, pdf_obj *chk, pdf_obj *name)
429
0
{
430
0
  pdf_obj *n = pdf_dict_getp(ctx, chk, "AP/N");
431
0
  pdf_obj *val;
432
433
  /* If name is a possible value of this check
434
  * box then use it, otherwise use "Off" */
435
0
  if (pdf_dict_get(ctx, n, name))
436
0
    val = name;
437
0
  else
438
0
    val = PDF_NAME(Off);
439
440
0
  if (pdf_name_eq(ctx, pdf_dict_get(ctx, chk, PDF_NAME(AS)), val))
441
0
    return;
442
443
0
  pdf_dict_put(ctx, chk, PDF_NAME(AS), val);
444
0
  pdf_set_annot_has_changed(ctx, find_widget(ctx, doc, chk));
445
0
}
446
447
/* Set the values of all fields in a group defined by a node
448
 * in the hierarchy */
449
static void set_check_grp(fz_context *ctx, pdf_document *doc, pdf_obj *grp, pdf_obj *val)
450
0
{
451
0
  pdf_obj *kids = pdf_dict_get(ctx, grp, PDF_NAME(Kids));
452
453
0
  if (kids == NULL)
454
0
  {
455
0
    set_check(ctx, doc, grp, val);
456
0
  }
457
0
  else
458
0
  {
459
0
    int i, n = pdf_array_len(ctx, kids);
460
461
0
    for (i = 0; i < n; i++)
462
0
      set_check_grp(ctx, doc, pdf_array_get(ctx, kids, i), val);
463
0
  }
464
0
}
465
466
void pdf_calculate_form(fz_context *ctx, pdf_document *doc)
467
0
{
468
0
  if (doc->js)
469
0
  {
470
0
    fz_try(ctx)
471
0
    {
472
0
      pdf_obj *co = pdf_dict_getp(ctx, pdf_trailer(ctx, doc), "Root/AcroForm/CO");
473
0
      int i, n = pdf_array_len(ctx, co);
474
0
      for (i = 0; i < n; i++)
475
0
      {
476
0
        pdf_obj *field = pdf_array_get(ctx, co, i);
477
0
        pdf_field_event_calculate(ctx, doc, field);
478
0
      }
479
0
    }
480
0
    fz_always(ctx)
481
0
      doc->recalculate = 0;
482
0
    fz_catch(ctx)
483
0
      fz_rethrow(ctx);
484
0
  }
485
0
}
486
487
static pdf_obj *find_on_state(fz_context *ctx, pdf_obj *dict)
488
587
{
489
587
  int i, n = pdf_dict_len(ctx, dict);
490
587
  for (i = 0; i < n; ++i)
491
583
  {
492
583
    pdf_obj *key = pdf_dict_get_key(ctx, dict, i);
493
583
    if (key != PDF_NAME(Off))
494
583
      return key;
495
583
  }
496
4
  return NULL;
497
587
}
498
499
pdf_obj *pdf_button_field_on_state(fz_context *ctx, pdf_obj *field)
500
585
{
501
585
  pdf_obj *ap = pdf_dict_get(ctx, field, PDF_NAME(AP));
502
585
  pdf_obj *on = find_on_state(ctx, pdf_dict_get(ctx, ap, PDF_NAME(N)));
503
585
  if (!on) on = find_on_state(ctx, pdf_dict_get(ctx, ap, PDF_NAME(D)));
504
585
  if (!on) on = PDF_NAME(Yes);
505
585
  return on;
506
585
}
507
508
static void
509
begin_annot_op(fz_context *ctx, pdf_annot *annot, const char *op)
510
0
{
511
0
  pdf_begin_operation(ctx, annot->page->doc, op);
512
0
}
513
514
static void
515
end_annot_op(fz_context *ctx, pdf_annot *annot)
516
0
{
517
0
  pdf_end_operation(ctx, annot->page->doc);
518
0
}
519
520
static void
521
abandon_annot_op(fz_context *ctx, pdf_annot *annot)
522
0
{
523
0
  pdf_abandon_operation(ctx, annot->page->doc);
524
0
}
525
526
static void toggle_check_box(fz_context *ctx, pdf_annot *annot)
527
0
{
528
0
  pdf_document *doc = annot->page->doc;
529
530
0
  begin_annot_op(ctx, annot, "Toggle checkbox");
531
532
0
  fz_try(ctx)
533
0
  {
534
0
    pdf_obj *field = annot->obj;
535
0
    int ff = pdf_field_flags(ctx, field);
536
0
    int is_radio = (ff & PDF_BTN_FIELD_IS_RADIO);
537
0
    int is_no_toggle_to_off = (ff & PDF_BTN_FIELD_IS_NO_TOGGLE_TO_OFF);
538
0
    pdf_obj *grp, *as, *val;
539
540
0
    grp = find_head_of_field_group(ctx, field);
541
0
    if (!grp)
542
0
      grp = field;
543
544
    /* TODO: check V value as well as or instead of AS? */
545
0
    as = pdf_dict_get(ctx, field, PDF_NAME(AS));
546
0
    if (as && as != PDF_NAME(Off))
547
0
    {
548
0
      if (is_radio && is_no_toggle_to_off)
549
0
      {
550
0
        end_annot_op(ctx, annot);
551
0
        break;
552
0
      }
553
0
      val = PDF_NAME(Off);
554
0
    }
555
0
    else
556
0
    {
557
0
      val = pdf_button_field_on_state(ctx, field);
558
0
    }
559
560
0
    pdf_dict_put(ctx, grp, PDF_NAME(V), val);
561
0
    set_check_grp(ctx, doc, grp, val);
562
0
    doc->recalculate = 1;
563
0
    end_annot_op(ctx, annot);
564
0
  }
565
0
  fz_catch(ctx)
566
0
  {
567
0
    abandon_annot_op(ctx, annot);
568
0
    fz_rethrow(ctx);
569
0
  }
570
571
0
  pdf_set_annot_has_changed(ctx, annot);
572
0
}
573
574
int pdf_has_unsaved_changes(fz_context *ctx, pdf_document *doc)
575
0
{
576
0
  int i;
577
578
0
  if (doc->num_incremental_sections == 0)
579
0
    return 0;
580
581
0
  for (i = 0; i < doc->xref_sections->num_objects; i++)
582
0
    if (doc->xref_sections->subsec->table[i].type != 0)
583
0
      break;
584
0
  return i != doc->xref_sections->num_objects;
585
0
}
586
587
int pdf_was_repaired(fz_context *ctx, pdf_document *doc)
588
0
{
589
0
  return doc->repair_attempted;
590
0
}
591
592
int pdf_toggle_widget(fz_context *ctx, pdf_annot *widget)
593
0
{
594
0
  switch (pdf_widget_type(ctx, widget))
595
0
  {
596
0
  default:
597
0
    return 0;
598
0
  case PDF_WIDGET_TYPE_CHECKBOX:
599
0
  case PDF_WIDGET_TYPE_RADIOBUTTON:
600
0
    toggle_check_box(ctx, widget);
601
0
    return 1;
602
0
  }
603
0
  return 0;
604
0
}
605
606
int
607
pdf_update_page(fz_context *ctx, pdf_page *page)
608
1.61k
{
609
1.61k
  pdf_annot *annot;
610
1.61k
  pdf_annot *widget;
611
1.61k
  int changed = 0;
612
613
3.23k
  fz_try(ctx)
614
3.23k
  {
615
1.61k
    pdf_begin_implicit_operation(ctx, page->doc);
616
1.61k
    if (page->doc->recalculate)
617
0
      pdf_calculate_form(ctx, page->doc);
618
619
3.32k
    for (annot = page->annots; annot; annot = annot->next)
620
1.71k
      if (pdf_update_annot(ctx, annot))
621
804
        changed = 1;
622
8.85k
    for (widget = page->widgets; widget; widget = widget->next)
623
7.24k
      if (pdf_update_annot(ctx, widget))
624
4.07k
        changed = 1;
625
1.61k
    pdf_end_operation(ctx, page->doc);
626
1.61k
  }
627
3.23k
  fz_catch(ctx)
628
1
  {
629
1
    pdf_abandon_operation(ctx, page->doc);
630
1
    fz_rethrow(ctx);
631
1
  }
632
633
1.61k
  return changed;
634
1.61k
}
635
636
pdf_annot *pdf_first_widget(fz_context *ctx, pdf_page *page)
637
1.81k
{
638
1.81k
  return page->widgets;
639
1.81k
}
640
641
pdf_annot *pdf_next_widget(fz_context *ctx, pdf_annot *widget)
642
14.4k
{
643
14.4k
  return widget->next;
644
14.4k
}
645
646
enum pdf_widget_type pdf_widget_type(fz_context *ctx, pdf_annot *widget)
647
0
{
648
0
  enum pdf_widget_type ret = PDF_WIDGET_TYPE_BUTTON;
649
650
0
  pdf_annot_push_local_xref(ctx, widget);
651
652
0
  fz_try(ctx)
653
0
  {
654
0
    pdf_obj *subtype = pdf_dict_get(ctx, widget->obj, PDF_NAME(Subtype));
655
0
    if (pdf_name_eq(ctx, subtype, PDF_NAME(Widget)))
656
0
      ret = pdf_field_type(ctx, widget->obj);
657
0
  }
658
0
  fz_always(ctx)
659
0
    pdf_annot_pop_local_xref(ctx, widget);
660
0
  fz_catch(ctx)
661
0
    fz_rethrow(ctx);
662
663
0
  return ret;
664
0
}
665
666
static int set_validated_field_value(fz_context *ctx, pdf_document *doc, pdf_obj *field, const char *text, int ignore_trigger_events)
667
0
{
668
0
  char *newtext = NULL;
669
670
0
  if (!ignore_trigger_events)
671
0
  {
672
0
    if (!pdf_field_event_validate(ctx, doc, field, text, &newtext))
673
0
      return 0;
674
0
  }
675
676
0
  update_field_value(ctx, doc, field, newtext ? newtext : text);
677
678
0
  fz_free(ctx, newtext);
679
680
0
  return 1;
681
0
}
682
683
static void update_checkbox_selector(fz_context *ctx, pdf_document *doc, pdf_obj *field, const char *val)
684
0
{
685
0
  pdf_obj *kids = pdf_dict_get(ctx, field, PDF_NAME(Kids));
686
687
0
  if (kids)
688
0
  {
689
0
    int i, n = pdf_array_len(ctx, kids);
690
691
0
    for (i = 0; i < n; i++)
692
0
      update_checkbox_selector(ctx, doc, pdf_array_get(ctx, kids, i), val);
693
0
  }
694
0
  else
695
0
  {
696
0
    pdf_obj *n = pdf_dict_getp(ctx, field, "AP/N");
697
0
    pdf_obj *oval;
698
699
0
    if (pdf_dict_gets(ctx, n, val))
700
0
      oval = pdf_new_name(ctx, val);
701
0
    else
702
0
      oval = PDF_NAME(Off);
703
0
    pdf_dict_put_drop(ctx, field, PDF_NAME(AS), oval);
704
0
  }
705
0
}
706
707
static int set_checkbox_value(fz_context *ctx, pdf_document *doc, pdf_obj *field, const char *val)
708
0
{
709
0
  update_checkbox_selector(ctx, doc, field, val);
710
0
  update_field_value(ctx, doc, field, val);
711
0
  return 1;
712
0
}
713
714
int pdf_set_field_value(fz_context *ctx, pdf_document *doc, pdf_obj *field, const char *text, int ignore_trigger_events)
715
0
{
716
0
  int accepted = 0;
717
718
0
  switch (pdf_field_type(ctx, field))
719
0
  {
720
0
  case PDF_WIDGET_TYPE_TEXT:
721
0
  case PDF_WIDGET_TYPE_COMBOBOX:
722
0
  case PDF_WIDGET_TYPE_LISTBOX:
723
0
    accepted = set_validated_field_value(ctx, doc, field, text, ignore_trigger_events);
724
0
    break;
725
726
0
  case PDF_WIDGET_TYPE_CHECKBOX:
727
0
  case PDF_WIDGET_TYPE_RADIOBUTTON:
728
0
    accepted = set_checkbox_value(ctx, doc, field, text);
729
0
    break;
730
731
0
  default:
732
0
    update_field_value(ctx, doc, field, text);
733
0
    accepted = 1;
734
0
    break;
735
0
  }
736
737
0
  if (!ignore_trigger_events)
738
0
    doc->recalculate = 1;
739
740
0
  return accepted;
741
0
}
742
743
char *pdf_field_border_style(fz_context *ctx, pdf_obj *field)
744
0
{
745
0
  const char *bs = pdf_to_name(ctx, pdf_dict_getl(ctx, field, PDF_NAME(BS), PDF_NAME(S), NULL));
746
0
  switch (*bs)
747
0
  {
748
0
  case 'S': return "Solid";
749
0
  case 'D': return "Dashed";
750
0
  case 'B': return "Beveled";
751
0
  case 'I': return "Inset";
752
0
  case 'U': return "Underline";
753
0
  }
754
0
  return "Solid";
755
0
}
756
757
void pdf_field_set_border_style(fz_context *ctx, pdf_obj *field, const char *text)
758
0
{
759
0
  pdf_obj *val;
760
761
0
  if (!strcmp(text, "Solid"))
762
0
    val = PDF_NAME(S);
763
0
  else if (!strcmp(text, "Dashed"))
764
0
    val = PDF_NAME(D);
765
0
  else if (!strcmp(text, "Beveled"))
766
0
    val = PDF_NAME(B);
767
0
  else if (!strcmp(text, "Inset"))
768
0
    val = PDF_NAME(I);
769
0
  else if (!strcmp(text, "Underline"))
770
0
    val = PDF_NAME(U);
771
0
  else
772
0
    return;
773
774
0
  pdf_dict_putl_drop(ctx, field, val, PDF_NAME(BS), PDF_NAME(S), NULL);
775
0
  pdf_field_mark_dirty(ctx, field);
776
0
}
777
778
void pdf_field_set_button_caption(fz_context *ctx, pdf_obj *field, const char *text)
779
0
{
780
0
  if (pdf_field_type(ctx, field) == PDF_WIDGET_TYPE_BUTTON)
781
0
  {
782
0
    pdf_obj *val = pdf_new_text_string(ctx, text);
783
0
    pdf_dict_putl_drop(ctx, field, val, PDF_NAME(MK), PDF_NAME(CA), NULL);
784
0
    pdf_field_mark_dirty(ctx, field);
785
0
  }
786
0
}
787
788
int pdf_field_display(fz_context *ctx, pdf_obj *field)
789
0
{
790
0
  pdf_obj *kids;
791
0
  int f, res = Display_Visible;
792
793
  /* Base response on first of children. Not ideal,
794
   * but not clear how to handle children with
795
   * differing values */
796
0
  while ((kids = pdf_dict_get(ctx, field, PDF_NAME(Kids))) != NULL)
797
0
    field = pdf_array_get(ctx, kids, 0);
798
799
0
  f = pdf_dict_get_int(ctx, field, PDF_NAME(F));
800
801
0
  if (f & PDF_ANNOT_IS_HIDDEN)
802
0
  {
803
0
    res = Display_Hidden;
804
0
  }
805
0
  else if (f & PDF_ANNOT_IS_PRINT)
806
0
  {
807
0
    if (f & PDF_ANNOT_IS_NO_VIEW)
808
0
      res = Display_NoView;
809
0
  }
810
0
  else
811
0
  {
812
0
    if (f & PDF_ANNOT_IS_NO_VIEW)
813
0
      res = Display_Hidden;
814
0
    else
815
0
      res = Display_NoPrint;
816
0
  }
817
818
0
  return res;
819
0
}
820
821
/*
822
 * get the field name in a char buffer that has spare room to
823
 * add more characters at the end.
824
 */
825
static char *load_field_name(fz_context *ctx, pdf_obj *field, int spare, pdf_cycle_list *cycle_up)
826
0
{
827
0
  pdf_cycle_list cycle;
828
0
  char *res = NULL;
829
0
  pdf_obj *parent;
830
0
  const char *lname;
831
0
  int llen;
832
833
0
  if (pdf_cycle(ctx, &cycle, cycle_up, field))
834
0
    fz_throw(ctx, FZ_ERROR_FORMAT, "Cycle in field parents");
835
836
0
  parent = pdf_dict_get(ctx, field, PDF_NAME(Parent));
837
0
  lname = pdf_dict_get_text_string(ctx, field, PDF_NAME(T));
838
0
  llen = (int)strlen(lname);
839
840
  // Limit fields to 16K
841
0
  if (llen > (16 << 10) || llen + spare > (16 << 10))
842
0
    fz_throw(ctx, FZ_ERROR_LIMIT, "Field name too long");
843
844
  /*
845
   * If we found a name at this point in the field hierarchy
846
   * then we'll need extra space for it and a dot
847
   */
848
0
  if (llen)
849
0
    spare += llen+1;
850
851
0
  if (parent)
852
0
  {
853
0
    res = load_field_name(ctx, parent, spare, &cycle);
854
0
  }
855
0
  else
856
0
  {
857
0
    res = Memento_label(fz_malloc(ctx, spare+1), "form_field_name");
858
0
    res[0] = 0;
859
0
  }
860
861
0
  if (llen)
862
0
  {
863
0
    if (res[0])
864
0
      strcat(res, ".");
865
866
0
    strcat(res, lname);
867
0
  }
868
869
0
  return res;
870
0
}
871
872
char *pdf_load_field_name(fz_context *ctx, pdf_obj *field)
873
0
{
874
0
  return load_field_name(ctx, field, 0, NULL);
875
0
}
876
877
void pdf_create_field_name(fz_context *ctx, pdf_document *doc, const char *prefix, char *buf, size_t len)
878
0
{
879
0
  pdf_obj *form = pdf_dict_getl(ctx, pdf_trailer(ctx, doc),
880
0
    PDF_NAME(Root), PDF_NAME(AcroForm), PDF_NAME(Fields), NULL);
881
0
  int i;
882
0
  for (i = 0; i < 65536; ++i) {
883
0
    fz_snprintf(buf, len, "%s%d", prefix, i);
884
0
    if (!pdf_lookup_field(ctx, form, buf))
885
0
      return;
886
0
  }
887
0
  fz_throw(ctx, FZ_ERROR_LIMIT, "Could not create unique field name.");
888
0
}
889
890
const char *pdf_field_label(fz_context *ctx, pdf_obj *field)
891
0
{
892
0
  pdf_obj *label = pdf_dict_get_inheritable(ctx, field, PDF_NAME(TU));
893
0
  if (!label)
894
0
    label = pdf_dict_get_inheritable(ctx, field, PDF_NAME(T));
895
0
  if (label)
896
0
    return pdf_to_text_string(ctx, label);
897
0
  return "Unnamed";
898
0
}
899
900
void pdf_field_set_display(fz_context *ctx, pdf_obj *field, int d)
901
0
{
902
0
  pdf_obj *kids = pdf_dict_get(ctx, field, PDF_NAME(Kids));
903
904
0
  if (!kids)
905
0
  {
906
0
    int mask = (PDF_ANNOT_IS_HIDDEN|PDF_ANNOT_IS_PRINT|PDF_ANNOT_IS_NO_VIEW);
907
0
    int f = pdf_dict_get_int(ctx, field, PDF_NAME(F)) & ~mask;
908
909
0
    switch (d)
910
0
    {
911
0
    case Display_Visible:
912
0
      f |= PDF_ANNOT_IS_PRINT;
913
0
      break;
914
0
    case Display_Hidden:
915
0
      f |= PDF_ANNOT_IS_HIDDEN;
916
0
      break;
917
0
    case Display_NoView:
918
0
      f |= (PDF_ANNOT_IS_PRINT|PDF_ANNOT_IS_NO_VIEW);
919
0
      break;
920
0
    case Display_NoPrint:
921
0
      break;
922
0
    }
923
924
0
    pdf_dict_put_int(ctx, field, PDF_NAME(F), f);
925
0
  }
926
0
  else
927
0
  {
928
0
    int i, n = pdf_array_len(ctx, kids);
929
930
0
    for (i = 0; i < n; i++)
931
0
      pdf_field_set_display(ctx, pdf_array_get(ctx, kids, i), d);
932
0
  }
933
0
}
934
935
void pdf_field_set_fill_color(fz_context *ctx, pdf_obj *field, pdf_obj *col)
936
0
{
937
  /* col == NULL mean transparent, but we can simply pass it on as with
938
   * non-NULL values because pdf_dict_putp interprets a NULL value as
939
   * delete */
940
0
  pdf_dict_putl(ctx, field, col, PDF_NAME(MK), PDF_NAME(BG), NULL);
941
0
  pdf_field_mark_dirty(ctx, field);
942
0
}
943
944
void pdf_field_set_text_color(fz_context *ctx, pdf_obj *field, pdf_obj *col)
945
0
{
946
0
  char buf[100];
947
0
  const char *font;
948
0
  float size, color[4];
949
  /* TODO? */
950
0
  const char *da = pdf_to_str_buf(ctx, pdf_dict_get_inheritable(ctx, field, PDF_NAME(DA)));
951
0
  int n;
952
953
0
  pdf_parse_default_appearance(ctx, da, &font, &size, &n, color);
954
955
0
  switch (pdf_array_len(ctx, col))
956
0
  {
957
0
  default:
958
0
    n = 0;
959
0
    color[0] = color[1] = color[2] = color[3] = 0;
960
0
    break;
961
0
  case 1:
962
0
    n = 1;
963
0
    color[0] = pdf_array_get_real(ctx, col, 0);
964
0
    break;
965
0
  case 3:
966
0
    n = 3;
967
0
    color[0] = pdf_array_get_real(ctx, col, 0);
968
0
    color[1] = pdf_array_get_real(ctx, col, 1);
969
0
    color[2] = pdf_array_get_real(ctx, col, 2);
970
0
    break;
971
0
  case 4:
972
0
    n = 4;
973
0
    color[0] = pdf_array_get_real(ctx, col, 0);
974
0
    color[1] = pdf_array_get_real(ctx, col, 1);
975
0
    color[2] = pdf_array_get_real(ctx, col, 2);
976
0
    color[3] = pdf_array_get_real(ctx, col, 3);
977
0
    break;
978
0
  }
979
980
0
  pdf_print_default_appearance(ctx, buf, sizeof buf, font, size, n, color);
981
0
  pdf_dict_put_string(ctx, field, PDF_NAME(DA), buf, strlen(buf));
982
0
  pdf_field_mark_dirty(ctx, field);
983
0
}
984
985
pdf_annot *
986
pdf_keep_widget(fz_context *ctx, pdf_annot *widget)
987
0
{
988
0
  return pdf_keep_annot(ctx, widget);
989
0
}
990
991
void
992
pdf_drop_widget(fz_context *ctx, pdf_annot *widget)
993
7.24k
{
994
7.24k
  pdf_drop_annot(ctx, widget);
995
7.24k
}
996
997
void
998
pdf_drop_widgets(fz_context *ctx, pdf_annot *widget)
999
12.6k
{
1000
19.9k
  while (widget)
1001
7.24k
  {
1002
7.24k
    pdf_annot *next = widget->next;
1003
7.24k
    pdf_drop_widget(ctx, widget);
1004
7.24k
    widget = next;
1005
7.24k
  }
1006
12.6k
}
1007
1008
pdf_annot *
1009
pdf_create_signature_widget(fz_context *ctx, pdf_page *page, char *name)
1010
0
{
1011
0
  fz_rect rect = { 12, 12, 12+100, 12+50 };
1012
0
  pdf_annot *annot;
1013
1014
0
  pdf_begin_operation(ctx, page->doc, "Create signature");
1015
1016
0
  annot = pdf_create_annot_raw(ctx, page, PDF_ANNOT_WIDGET);
1017
1018
0
  fz_try(ctx)
1019
0
  {
1020
0
    pdf_obj *obj = annot->obj;
1021
0
    pdf_obj *root = pdf_dict_get(ctx, pdf_trailer(ctx, page->doc), PDF_NAME(Root));
1022
0
    pdf_obj *acroform = pdf_dict_get(ctx, root, PDF_NAME(AcroForm));
1023
0
    pdf_obj *fields, *lock;
1024
0
    if (!acroform)
1025
0
    {
1026
0
      acroform = pdf_new_dict(ctx, page->doc, 1);
1027
0
      pdf_dict_put_drop(ctx, root, PDF_NAME(AcroForm), acroform);
1028
0
    }
1029
0
    fields = pdf_dict_get(ctx, acroform, PDF_NAME(Fields));
1030
0
    if (!fields)
1031
0
    {
1032
0
      fields = pdf_new_array(ctx, page->doc, 1);
1033
0
      pdf_dict_put_drop(ctx, acroform, PDF_NAME(Fields), fields);
1034
0
    }
1035
0
    pdf_set_annot_rect(ctx, annot, rect);
1036
0
    pdf_dict_put(ctx, obj, PDF_NAME(FT), PDF_NAME(Sig));
1037
0
    pdf_dict_put_int(ctx, obj, PDF_NAME(F), PDF_ANNOT_IS_PRINT);
1038
0
    pdf_dict_put_text_string(ctx, obj, PDF_NAME(DA), "/Helv 0 Tf 0 g");
1039
0
    pdf_dict_put_text_string(ctx, obj, PDF_NAME(T), name);
1040
0
    pdf_array_push(ctx, fields, obj);
1041
0
    lock = pdf_dict_put_dict(ctx, obj, PDF_NAME(Lock), 1);
1042
0
    pdf_dict_put(ctx, lock, PDF_NAME(Action), PDF_NAME(All));
1043
0
    pdf_end_operation(ctx, page->doc);
1044
0
  }
1045
0
  fz_catch(ctx)
1046
0
  {
1047
0
    pdf_abandon_operation(ctx, page->doc);
1048
0
    pdf_delete_annot(ctx, page, annot);
1049
0
  }
1050
0
  return (pdf_annot *)annot;
1051
0
}
1052
1053
fz_rect
1054
pdf_bound_widget(fz_context *ctx, pdf_annot *widget)
1055
0
{
1056
0
  return pdf_bound_annot(ctx, widget);
1057
0
}
1058
1059
int
1060
pdf_update_widget(fz_context *ctx, pdf_annot *widget)
1061
0
{
1062
0
  return pdf_update_annot(ctx, widget);
1063
0
}
1064
1065
int pdf_text_widget_max_len(fz_context *ctx, pdf_annot *tw)
1066
0
{
1067
0
  pdf_annot *annot = (pdf_annot *)tw;
1068
0
  return pdf_dict_get_inheritable_int(ctx, annot->obj, PDF_NAME(MaxLen));
1069
0
}
1070
1071
int pdf_text_widget_format(fz_context *ctx, pdf_annot *tw)
1072
0
{
1073
0
  pdf_annot *annot = (pdf_annot *)tw;
1074
0
  int type = PDF_WIDGET_TX_FORMAT_NONE;
1075
0
  pdf_obj *js = pdf_dict_getl(ctx, annot->obj, PDF_NAME(AA), PDF_NAME(F), PDF_NAME(JS), NULL);
1076
0
  if (js)
1077
0
  {
1078
0
    char *code = pdf_load_stream_or_string_as_utf8(ctx, js);
1079
0
    if (strstr(code, "AFNumber_Format"))
1080
0
      type = PDF_WIDGET_TX_FORMAT_NUMBER;
1081
0
    else if (strstr(code, "AFSpecial_Format"))
1082
0
      type = PDF_WIDGET_TX_FORMAT_SPECIAL;
1083
0
    else if (strstr(code, "AFDate_FormatEx"))
1084
0
      type = PDF_WIDGET_TX_FORMAT_DATE;
1085
0
    else if (strstr(code, "AFTime_FormatEx"))
1086
0
      type = PDF_WIDGET_TX_FORMAT_TIME;
1087
0
    fz_free(ctx, code);
1088
0
  }
1089
1090
0
  return type;
1091
0
}
1092
1093
static char *
1094
merge_changes(fz_context *ctx, const char *value, int start, int end, const char *change)
1095
0
{
1096
0
  int changelen = change ? (int)strlen(change) : 0;
1097
0
  int valuelen = value ? (int)strlen(value) : 0;
1098
0
  int prelen = (start >= 0 ? (start < valuelen ? start : valuelen) : 0);
1099
0
  int postlen = (end >= 0 && end <= valuelen ? valuelen - end : 0);
1100
0
  int newlen =  prelen + changelen + postlen + 1;
1101
0
  char *merged = fz_malloc(ctx, newlen);
1102
0
  char *m = merged;
1103
1104
0
  if (prelen)
1105
0
  {
1106
0
    memcpy(m, value, prelen);
1107
0
    m += prelen;
1108
0
  }
1109
0
  if (changelen)
1110
0
  {
1111
0
    memcpy(m, change, changelen);
1112
0
    m += changelen;
1113
0
  }
1114
0
  if (postlen)
1115
0
  {
1116
0
    memcpy(m, &value[end], postlen);
1117
0
    m += postlen;
1118
0
  }
1119
0
  *m = 0;
1120
1121
0
  return merged;
1122
0
}
1123
1124
int pdf_set_text_field_value(fz_context *ctx, pdf_annot *widget, const char *update)
1125
0
{
1126
0
  pdf_document *doc = widget->page->doc;
1127
0
  pdf_keystroke_event evt = { 0 };
1128
0
  char *new_change = NULL;
1129
0
  char *new_value = NULL;
1130
0
  char *merged_value = NULL;
1131
0
  int rc = 1;
1132
1133
0
  pdf_begin_operation(ctx, doc, "Edit text field");
1134
1135
0
  fz_var(new_value);
1136
0
  fz_var(new_change);
1137
0
  fz_var(merged_value);
1138
0
  fz_try(ctx)
1139
0
  {
1140
0
    if (!widget->ignore_trigger_events)
1141
0
    {
1142
0
      evt.value = pdf_annot_field_value(ctx, widget);
1143
0
      evt.change = update;
1144
0
      evt.selStart = 0;
1145
0
      evt.selEnd = (int)strlen(evt.value);
1146
0
      evt.willCommit = 0;
1147
0
      rc = pdf_annot_field_event_keystroke(ctx, doc, widget, &evt);
1148
0
      new_change = evt.newChange;
1149
0
      new_value = evt.newValue;
1150
0
      evt.newValue = NULL;
1151
0
      evt.newChange = NULL;
1152
0
      if (rc)
1153
0
      {
1154
0
        merged_value = merge_changes(ctx, new_value, evt.selStart, evt.selEnd, new_change);
1155
0
        evt.value = merged_value;
1156
0
        evt.change = "";
1157
0
        evt.selStart = -1;
1158
0
        evt.selEnd = -1;
1159
0
        evt.willCommit = 1;
1160
0
        rc = pdf_annot_field_event_keystroke(ctx, doc, widget, &evt);
1161
0
        if (rc)
1162
0
          rc = pdf_set_annot_field_value(ctx, doc, widget, evt.newValue, 0);
1163
0
      }
1164
0
    }
1165
0
    else
1166
0
    {
1167
0
      rc = pdf_set_annot_field_value(ctx, doc, widget, update, 1);
1168
0
    }
1169
0
    pdf_end_operation(ctx, doc);
1170
0
  }
1171
0
  fz_always(ctx)
1172
0
  {
1173
0
    fz_free(ctx, new_value);
1174
0
    fz_free(ctx, evt.newValue);
1175
0
    fz_free(ctx, new_change);
1176
0
    fz_free(ctx, evt.newChange);
1177
0
    fz_free(ctx, merged_value);
1178
0
  }
1179
0
  fz_catch(ctx)
1180
0
  {
1181
0
    pdf_abandon_operation(ctx, doc);
1182
0
    fz_warn(ctx, "could not set widget text");
1183
0
    rc = 0;
1184
0
  }
1185
0
  return rc;
1186
0
}
1187
1188
int pdf_edit_text_field_value(fz_context *ctx, pdf_annot *widget, const char *value, const char *change, int *selStart, int *selEnd, char **result)
1189
0
{
1190
0
  pdf_document *doc = widget->page->doc;
1191
0
  pdf_keystroke_event evt = {0};
1192
0
  int rc = 1;
1193
1194
0
  pdf_begin_operation(ctx, doc, "Text field keystroke");
1195
1196
0
  fz_try(ctx)
1197
0
  {
1198
0
    if (!widget->ignore_trigger_events)
1199
0
    {
1200
0
      evt.value = value;
1201
0
      evt.change = change;
1202
0
      evt.selStart = *selStart;
1203
0
      evt.selEnd = *selEnd;
1204
0
      evt.willCommit = 0;
1205
0
      rc = pdf_annot_field_event_keystroke(ctx, doc, widget, &evt);
1206
0
      if (rc)
1207
0
      {
1208
0
        *result = merge_changes(ctx, evt.newValue, evt.selStart, evt.selEnd, evt.newChange);
1209
0
        *selStart = evt.selStart + (int)strlen(evt.newChange);
1210
0
        *selEnd = *selStart;
1211
0
      }
1212
0
    }
1213
0
    else
1214
0
    {
1215
0
      *result = merge_changes(ctx, value, *selStart, *selEnd, change);
1216
0
      *selStart = evt.selStart + (int)strlen(change);
1217
0
      *selEnd = *selStart;
1218
0
    }
1219
0
    pdf_end_operation(ctx, doc);
1220
0
  }
1221
0
  fz_always(ctx)
1222
0
  {
1223
0
    fz_free(ctx, evt.newValue);
1224
0
    fz_free(ctx, evt.newChange);
1225
0
  }
1226
0
  fz_catch(ctx)
1227
0
  {
1228
0
    pdf_abandon_operation(ctx, doc);
1229
0
    fz_warn(ctx, "could not process text widget keystroke");
1230
0
    rc = 0;
1231
0
  }
1232
0
  return rc;
1233
0
}
1234
1235
int pdf_set_choice_field_value(fz_context *ctx, pdf_annot *widget, const char *new_value)
1236
0
{
1237
  /* Choice widgets use almost the same keystroke processing as text fields. */
1238
0
  return pdf_set_text_field_value(ctx, widget, new_value);
1239
0
}
1240
1241
int pdf_choice_widget_options(fz_context *ctx, pdf_annot *tw, int exportval, const char *opts[])
1242
0
{
1243
0
  pdf_annot *annot = (pdf_annot *)tw;
1244
0
  pdf_obj *optarr;
1245
0
  int i, n, m;
1246
1247
0
  optarr = pdf_dict_get_inheritable(ctx, annot->obj, PDF_NAME(Opt));
1248
0
  n = pdf_array_len(ctx, optarr);
1249
1250
0
  if (opts)
1251
0
  {
1252
0
    for (i = 0; i < n; i++)
1253
0
    {
1254
0
      m = pdf_array_len(ctx, pdf_array_get(ctx, optarr, i));
1255
      /* If it is a two element array, the second item is the one that we want if we want the listing value. */
1256
0
      if (m == 2)
1257
0
        if (exportval)
1258
0
          opts[i] = pdf_array_get_text_string(ctx, pdf_array_get(ctx, optarr, i), 0);
1259
0
        else
1260
0
          opts[i] = pdf_array_get_text_string(ctx, pdf_array_get(ctx, optarr, i), 1);
1261
0
      else
1262
0
        opts[i] = pdf_array_get_text_string(ctx, optarr, i);
1263
0
    }
1264
0
  }
1265
0
  return n;
1266
0
}
1267
1268
int pdf_choice_field_option_count(fz_context *ctx, pdf_obj *field)
1269
0
{
1270
0
  pdf_obj *opt = pdf_dict_get_inheritable(ctx, field, PDF_NAME(Opt));
1271
0
  return pdf_array_len(ctx, opt);
1272
0
}
1273
1274
const char *pdf_choice_field_option(fz_context *ctx, pdf_obj *field, int export, int i)
1275
0
{
1276
0
  pdf_obj *opt = pdf_dict_get_inheritable(ctx, field, PDF_NAME(Opt));
1277
0
  pdf_obj *ent = pdf_array_get(ctx, opt, i);
1278
0
  if (pdf_array_len(ctx, ent) == 2)
1279
0
    return pdf_array_get_text_string(ctx, ent, export ? 0 : 1);
1280
0
  else
1281
0
    return pdf_to_text_string(ctx, ent);
1282
0
}
1283
1284
int pdf_choice_widget_is_multiselect(fz_context *ctx, pdf_annot *tw)
1285
0
{
1286
0
  pdf_annot *annot = (pdf_annot *)tw;
1287
1288
0
  if (!annot) return 0;
1289
1290
0
  switch (pdf_field_type(ctx, annot->obj))
1291
0
  {
1292
0
  case PDF_WIDGET_TYPE_LISTBOX:
1293
0
    return (pdf_field_flags(ctx, annot->obj) & PDF_CH_FIELD_IS_MULTI_SELECT) != 0;
1294
0
  default:
1295
0
    return 0;
1296
0
  }
1297
0
}
1298
1299
int pdf_choice_widget_value(fz_context *ctx, pdf_annot *tw, const char *opts[])
1300
0
{
1301
0
  pdf_annot *annot = (pdf_annot *)tw;
1302
0
  pdf_obj *optarr;
1303
0
  int i, n;
1304
1305
0
  if (!annot)
1306
0
    return 0;
1307
1308
0
  optarr = pdf_dict_get(ctx, annot->obj, PDF_NAME(V));
1309
1310
0
  if (pdf_is_string(ctx, optarr))
1311
0
  {
1312
0
    if (opts)
1313
0
      opts[0] = pdf_to_text_string(ctx, optarr);
1314
0
    return 1;
1315
0
  }
1316
0
  else
1317
0
  {
1318
0
    n = pdf_array_len(ctx, optarr);
1319
0
    if (opts)
1320
0
    {
1321
0
      for (i = 0; i < n; i++)
1322
0
      {
1323
0
        pdf_obj *elem = pdf_array_get(ctx, optarr, i);
1324
0
        if (pdf_is_array(ctx, elem))
1325
0
          elem = pdf_array_get(ctx, elem, 1);
1326
0
        opts[i] = pdf_to_text_string(ctx, elem);
1327
0
      }
1328
0
    }
1329
0
    return n;
1330
0
  }
1331
0
}
1332
1333
void pdf_choice_widget_set_value(fz_context *ctx, pdf_annot *tw, int n, const char *opts[])
1334
0
{
1335
0
  pdf_annot *annot = (pdf_annot *)tw;
1336
0
  pdf_obj *optarr = NULL;
1337
0
  int i;
1338
1339
0
  if (!annot)
1340
0
    return;
1341
1342
0
  begin_annot_op(ctx, annot, "Set choice");
1343
1344
0
  fz_var(optarr);
1345
0
  fz_try(ctx)
1346
0
  {
1347
0
    if (n != 1)
1348
0
    {
1349
0
      optarr = pdf_new_array(ctx, annot->page->doc, n);
1350
1351
0
      for (i = 0; i < n; i++)
1352
0
        pdf_array_push_text_string(ctx, optarr, opts[i]);
1353
1354
0
      pdf_dict_put_drop(ctx, annot->obj, PDF_NAME(V), optarr);
1355
0
    }
1356
0
    else
1357
0
      pdf_dict_put_text_string(ctx, annot->obj, PDF_NAME(V), opts[0]);
1358
1359
    /* FIXME: when n > 1, we should be regenerating the indexes */
1360
0
    pdf_dict_del(ctx, annot->obj, PDF_NAME(I));
1361
1362
0
    pdf_field_mark_dirty(ctx, annot->obj);
1363
0
    end_annot_op(ctx, annot);
1364
0
  }
1365
0
  fz_catch(ctx)
1366
0
  {
1367
0
    abandon_annot_op(ctx, annot);
1368
0
    pdf_drop_obj(ctx, optarr);
1369
0
    fz_rethrow(ctx);
1370
0
  }
1371
0
}
1372
1373
int pdf_signature_byte_range(fz_context *ctx, pdf_document *doc, pdf_obj *signature, fz_range *byte_range)
1374
0
{
1375
0
  pdf_obj *br = pdf_dict_getl(ctx, signature, PDF_NAME(V), PDF_NAME(ByteRange), NULL);
1376
0
  int i, n = pdf_array_len(ctx, br)/2;
1377
1378
0
  if (byte_range)
1379
0
  {
1380
0
    for (i = 0; i < n; i++)
1381
0
    {
1382
0
      int64_t offset = pdf_array_get_int(ctx, br, 2*i);
1383
0
      int length = pdf_array_get_int(ctx, br, 2*i+1);
1384
1385
0
      if (offset < 0 || offset > doc->file_size)
1386
0
        fz_throw(ctx, FZ_ERROR_FORMAT, "offset of signature byte range outside of file");
1387
0
      else if (length < 0)
1388
0
        fz_throw(ctx, FZ_ERROR_FORMAT, "length of signature byte range negative");
1389
0
      else if (offset + length > doc->file_size)
1390
0
        fz_throw(ctx, FZ_ERROR_FORMAT, "signature byte range extends past end of file");
1391
1392
0
      byte_range[i].offset = offset;
1393
0
      byte_range[i].length = length;
1394
0
    }
1395
0
  }
1396
1397
0
  return n;
1398
0
}
1399
1400
static int is_white(int c)
1401
0
{
1402
0
  return c == '\x00' || c == '\x09' || c == '\x0a' || c == '\x0c' || c == '\x0d' || c == '\x20';
1403
0
}
1404
1405
static int is_hex_or_white(int c)
1406
0
{
1407
0
  return (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f') || (c >= '0' && c <= '9') || is_white(c);
1408
0
}
1409
1410
static void validate_certificate_data(fz_context *ctx, pdf_document *doc, fz_range *hole)
1411
0
{
1412
0
  fz_stream *stm;
1413
0
  int c;
1414
1415
0
  stm = fz_open_range_filter(ctx, doc->file, hole, 1);
1416
0
  fz_try(ctx)
1417
0
  {
1418
0
    while (is_white((c = fz_read_byte(ctx, stm))))
1419
0
      ;
1420
1421
0
    if (c == '<')
1422
0
      c = fz_read_byte(ctx, stm);
1423
1424
0
    while (is_hex_or_white(c))
1425
0
      c = fz_read_byte(ctx, stm);
1426
1427
0
    if (c == '>')
1428
0
      c = fz_read_byte(ctx, stm);
1429
1430
0
    while (is_white(c))
1431
0
      c = fz_read_byte(ctx, stm);
1432
1433
0
    if (c != EOF)
1434
0
      fz_throw(ctx, FZ_ERROR_FORMAT, "signature certificate data contains invalid character");
1435
0
    if ((size_t)fz_tell(ctx, stm) != hole->length)
1436
0
      fz_throw(ctx, FZ_ERROR_FORMAT, "premature end of signature certificate data");
1437
0
  }
1438
0
  fz_always(ctx)
1439
0
    fz_drop_stream(ctx, stm);
1440
0
  fz_catch(ctx)
1441
0
    fz_rethrow(ctx);
1442
0
}
1443
1444
static int rangecmp(const void *a_, const void *b_)
1445
0
{
1446
0
  const fz_range *a = (const fz_range *) a_;
1447
0
  const fz_range *b = (const fz_range *) b_;
1448
0
  return (int) (a->offset - b->offset);
1449
0
}
1450
1451
static void validate_byte_ranges(fz_context *ctx, pdf_document *doc, fz_range *unsorted, int nranges)
1452
0
{
1453
0
  int64_t offset = 0;
1454
0
  fz_range *sorted;
1455
0
  int i;
1456
1457
0
  sorted = fz_calloc(ctx, nranges, sizeof(*sorted));
1458
0
  memcpy(sorted, unsorted, nranges * sizeof(*sorted));
1459
0
  qsort(sorted, nranges, sizeof(*sorted), rangecmp);
1460
1461
0
  fz_try(ctx)
1462
0
  {
1463
0
    offset = 0;
1464
1465
0
    for (i = 0; i < nranges; i++)
1466
0
    {
1467
0
      if (sorted[i].offset > offset)
1468
0
      {
1469
0
        fz_range hole;
1470
1471
0
        hole.offset = offset;
1472
0
        hole.length = sorted[i].offset - offset;
1473
1474
0
        validate_certificate_data(ctx, doc, &hole);
1475
0
      }
1476
1477
0
      offset = fz_maxi64(offset, sorted[i].offset + sorted[i].length);
1478
0
    }
1479
0
  }
1480
0
  fz_always(ctx)
1481
0
    fz_free(ctx, sorted);
1482
0
  fz_catch(ctx)
1483
0
    fz_rethrow(ctx);
1484
0
}
1485
1486
fz_stream *pdf_signature_hash_bytes(fz_context *ctx, pdf_document *doc, pdf_obj *signature)
1487
0
{
1488
0
  fz_range *byte_range = NULL;
1489
0
  int byte_range_len;
1490
0
  fz_stream *bytes = NULL;
1491
1492
0
  fz_var(byte_range);
1493
0
  fz_try(ctx)
1494
0
  {
1495
0
    byte_range_len = pdf_signature_byte_range(ctx, doc, signature, NULL);
1496
0
    if (byte_range_len)
1497
0
    {
1498
0
      byte_range = fz_calloc(ctx, byte_range_len, sizeof(*byte_range));
1499
0
      pdf_signature_byte_range(ctx, doc, signature, byte_range);
1500
0
    }
1501
1502
0
    validate_byte_ranges(ctx, doc, byte_range, byte_range_len);
1503
0
    bytes = fz_open_range_filter(ctx, doc->file, byte_range, byte_range_len);
1504
0
  }
1505
0
  fz_always(ctx)
1506
0
  {
1507
0
    fz_free(ctx, byte_range);
1508
0
  }
1509
0
  fz_catch(ctx)
1510
0
  {
1511
0
    fz_rethrow(ctx);
1512
0
  }
1513
1514
0
  return bytes;
1515
0
}
1516
1517
int pdf_signature_incremental_change_since_signing(fz_context *ctx, pdf_document *doc, pdf_obj *signature)
1518
0
{
1519
0
  fz_range *byte_range = NULL;
1520
0
  int byte_range_len;
1521
0
  int changed = 0;
1522
1523
0
  fz_var(byte_range);
1524
0
  fz_try(ctx)
1525
0
  {
1526
0
    byte_range_len = pdf_signature_byte_range(ctx, doc, signature, NULL);
1527
0
    if (byte_range_len)
1528
0
    {
1529
0
      fz_range *last_range;
1530
0
      int64_t end_of_range;
1531
1532
0
      byte_range = fz_calloc(ctx, byte_range_len, sizeof(*byte_range));
1533
0
      pdf_signature_byte_range(ctx, doc, signature, byte_range);
1534
1535
0
      last_range = &byte_range[byte_range_len -1];
1536
0
      end_of_range = last_range->offset + last_range->length;
1537
1538
      /* We can see how long the document was when signed by inspecting the byte
1539
       * ranges of the signature.  The document, when read in, may have already
1540
       * had changes tagged on to it, past its extent when signed, or we may have
1541
       * made changes since reading it, which will be held in a new incremental
1542
       * xref section. */
1543
0
      if (doc->file_size > end_of_range || doc->num_incremental_sections > 0)
1544
0
        changed = 1;
1545
0
    }
1546
0
  }
1547
0
  fz_always(ctx)
1548
0
  {
1549
0
    fz_free(ctx, byte_range);
1550
0
  }
1551
0
  fz_catch(ctx)
1552
0
  {
1553
0
    fz_rethrow(ctx);
1554
0
  }
1555
1556
0
  return changed;
1557
0
}
1558
1559
int pdf_signature_is_signed(fz_context *ctx, pdf_document *doc, pdf_obj *field)
1560
50
{
1561
50
  pdf_obj *v;
1562
50
  pdf_obj* vtype;
1563
1564
50
  if (pdf_dict_get_inheritable(ctx, field, PDF_NAME(FT)) != PDF_NAME(Sig))
1565
0
    return 0;
1566
  /* Signatures can only be signed if the value is a dictionary,
1567
   * and if the value has a Type, it should be Sig. */
1568
50
  v = pdf_dict_get_inheritable(ctx, field, PDF_NAME(V));
1569
50
  vtype = pdf_dict_get(ctx, v, PDF_NAME(Type));
1570
50
  return pdf_is_dict(ctx, v) && (vtype ? pdf_name_eq(ctx, vtype, PDF_NAME(Sig)) : 1);
1571
50
}
1572
1573
int pdf_widget_is_signed(fz_context *ctx, pdf_annot *widget)
1574
0
{
1575
0
  if (widget == NULL)
1576
0
    return 0;
1577
0
  return pdf_signature_is_signed(ctx, widget->page->doc, widget->obj);
1578
0
}
1579
1580
int pdf_widget_is_readonly(fz_context *ctx, pdf_annot *widget)
1581
0
{
1582
0
  int fflags;
1583
0
  if (widget == NULL)
1584
0
    return 0;
1585
0
  fflags = pdf_field_flags(ctx, ((pdf_annot *) widget)->obj);
1586
0
  return fflags & PDF_FIELD_IS_READ_ONLY;
1587
0
}
1588
1589
size_t pdf_signature_contents(fz_context *ctx, pdf_document *doc, pdf_obj *signature, char **contents)
1590
0
{
1591
0
  pdf_obj *v_ref = pdf_dict_get_inheritable(ctx, signature, PDF_NAME(V));
1592
0
  pdf_obj *v_obj = pdf_load_unencrypted_object(ctx, doc, pdf_to_num(ctx, v_ref));
1593
0
  char *copy = NULL;
1594
0
  size_t len;
1595
1596
0
  fz_var(copy);
1597
0
  fz_try(ctx)
1598
0
  {
1599
0
    pdf_obj *c = pdf_dict_get(ctx, v_obj, PDF_NAME(Contents));
1600
0
    char *s;
1601
1602
0
    s = pdf_to_str_buf(ctx, c);
1603
0
    len = pdf_to_str_len(ctx, c);
1604
1605
0
    if (contents)
1606
0
    {
1607
0
      copy = Memento_label(fz_malloc(ctx, len), "sig_contents");
1608
0
      memcpy(copy, s, len);
1609
0
    }
1610
0
  }
1611
0
  fz_always(ctx)
1612
0
    pdf_drop_obj(ctx, v_obj);
1613
0
  fz_catch(ctx)
1614
0
  {
1615
0
    fz_free(ctx, copy);
1616
0
    fz_rethrow(ctx);
1617
0
  }
1618
1619
0
  if (contents)
1620
0
    *contents = copy;
1621
0
  return len;
1622
0
}
1623
1624
static fz_xml_doc *load_xfa(fz_context *ctx, pdf_document *doc)
1625
0
{
1626
0
  pdf_obj *xfa;
1627
0
  fz_buffer *buf = NULL;
1628
0
  fz_buffer *packet = NULL;
1629
0
  int i;
1630
1631
0
  if (doc->xfa)
1632
0
    return doc->xfa; /* Already loaded, and present. */
1633
1634
0
  xfa = pdf_dict_getp(ctx, pdf_trailer(ctx, doc), "Root/AcroForm/XFA");
1635
0
  if (!pdf_is_array(ctx, xfa) && !pdf_is_stream(ctx, xfa))
1636
0
    return NULL; /* No XFA */
1637
1638
0
  fz_var(buf);
1639
0
  fz_var(packet);
1640
1641
0
  fz_try(ctx)
1642
0
  {
1643
0
    if (pdf_is_stream(ctx, xfa))
1644
0
    {
1645
      /* Load entire XFA resource */
1646
0
      buf = pdf_load_stream(ctx, xfa);
1647
0
    }
1648
0
    else
1649
0
    {
1650
      /* Concatenate packets to create entire XFA resource */
1651
0
      buf = fz_new_buffer(ctx, 1024);
1652
0
      for(i = 0; i < pdf_array_len(ctx, xfa); ++i)
1653
0
      {
1654
0
        pdf_obj *ref = pdf_array_get(ctx, xfa, i);
1655
0
        if (pdf_is_stream(ctx, ref))
1656
0
        {
1657
0
          packet = pdf_load_stream(ctx, ref);
1658
0
          fz_append_buffer(ctx, buf, packet);
1659
0
          fz_drop_buffer(ctx, packet);
1660
0
          packet = NULL;
1661
0
        }
1662
0
      }
1663
0
    }
1664
1665
    /* Parse and stow away XFA resource in document */
1666
0
    doc->xfa = fz_parse_xml(ctx, buf, 0);
1667
0
  }
1668
0
  fz_always(ctx)
1669
0
  {
1670
0
    fz_drop_buffer(ctx, packet);
1671
0
    fz_drop_buffer(ctx, buf);
1672
0
  }
1673
0
  fz_catch(ctx)
1674
0
  {
1675
0
    fz_rethrow(ctx);
1676
0
  }
1677
1678
0
  return doc->xfa;
1679
0
}
1680
1681
static fz_xml *
1682
get_xfa_resource(fz_context *ctx, pdf_document *doc, const char *str)
1683
0
{
1684
0
  fz_xml_doc *xfa;
1685
1686
0
  xfa = load_xfa(ctx, doc);
1687
0
  if (!xfa)
1688
0
    return NULL;
1689
1690
0
  return fz_xml_find_down(fz_xml_root(xfa), str);
1691
0
}
1692
1693
static int
1694
find_name_component(char **np, char **sp, char **ep)
1695
0
{
1696
0
  char *n = *np;
1697
0
  char *s, *e;
1698
0
  int idx = 0;
1699
1700
0
  if (*n == '.')
1701
0
    n++;
1702
1703
  /* Find the next name we are looking for. */
1704
0
  s = e = n;
1705
0
  while (*e && *e != '[' && *e != '.')
1706
0
    e++;
1707
1708
  /* So the next name is s..e */
1709
0
  n = e;
1710
0
  if (*n == '[')
1711
0
  {
1712
0
    n++;
1713
0
    while (*n >= '0' && *n <= '9')
1714
0
      idx = idx*10 + *n++ - '0';
1715
0
    while (*n && *n != ']')
1716
0
      n++;
1717
0
    if (*n == ']')
1718
0
      n++;
1719
0
  }
1720
0
  *np = n;
1721
0
  *sp = s;
1722
0
  *ep = e;
1723
1724
0
  return idx;
1725
0
}
1726
1727
static pdf_obj *
1728
annot_from_name(fz_context *ctx, pdf_document *doc, const char *str)
1729
0
{
1730
0
  pdf_obj *fields = pdf_dict_getp(ctx, pdf_trailer(ctx, doc), "Root/AcroForm/Fields");
1731
1732
0
  if (strncmp(str, "xfa[0].", 7) == 0)
1733
0
    str += 7;
1734
0
  if (strncmp(str, "template[0].", 12) == 0)
1735
0
    str += 12;
1736
1737
0
  return pdf_lookup_field(ctx, fields, str);
1738
0
}
1739
1740
static pdf_obj *
1741
get_locked_fields_from_xfa(fz_context *ctx, pdf_document *doc, pdf_obj *field)
1742
0
{
1743
0
  char *name = pdf_load_field_name(ctx, field);
1744
0
  char *n = name;
1745
0
  const char *use;
1746
0
  fz_xml *node;
1747
1748
0
  if (name == NULL)
1749
0
    return NULL;
1750
1751
0
  fz_try(ctx)
1752
0
  {
1753
0
    node = get_xfa_resource(ctx, doc, "template");
1754
1755
0
    do
1756
0
    {
1757
0
      char c, *s, *e;
1758
0
      int idx = 0;
1759
0
      char *key;
1760
1761
0
      idx = find_name_component(&n, &s, &e);
1762
      /* We want the idx'th occurrence of s..e */
1763
1764
      /* Hacky */
1765
0
      c = *e;
1766
0
      *e = 0;
1767
0
      key = *n ? "subform" : "field";
1768
0
      node = fz_xml_find_down_match(node, key, "name", s);
1769
0
      while (node && idx > 0)
1770
0
      {
1771
0
        node = fz_xml_find_next_match(node, key, "name", s);
1772
0
        idx--;
1773
0
      }
1774
0
      *e = c;
1775
0
    }
1776
0
    while (node && *n == '.');
1777
0
  }
1778
0
  fz_always(ctx)
1779
0
    fz_free(ctx, name);
1780
0
  fz_catch(ctx)
1781
0
    fz_rethrow(ctx);
1782
1783
0
  if (node == NULL)
1784
0
    return NULL;
1785
1786
0
  node = fz_xml_find_down(node, "ui");
1787
0
  node = fz_xml_find_down(node, "signature");
1788
0
  node = fz_xml_find_down(node, "manifest");
1789
1790
0
  use = fz_xml_att(node, "use");
1791
0
  if (use == NULL)
1792
0
    return NULL;
1793
0
  if (*use == '#')
1794
0
    use++;
1795
1796
  /* Now look for a variables entry in a subform that defines this. */
1797
0
  while (node)
1798
0
  {
1799
0
    fz_xml *variables, *manifest, *ref;
1800
0
    pdf_obj *arr;
1801
1802
    /* Find the enclosing subform */
1803
0
    do {
1804
0
      node = fz_xml_up(node);
1805
0
    } while (node && strcmp(fz_xml_tag(node), "subform"));
1806
1807
    /* Look for a variables within that. */
1808
0
    variables = fz_xml_find_down(node, "variables");
1809
0
    if (variables == NULL)
1810
0
      continue;
1811
1812
0
    manifest = fz_xml_find_down_match(variables, "manifest", "id", use);
1813
0
    if (manifest == NULL)
1814
0
      continue;
1815
1816
0
    arr = pdf_new_array(ctx, doc, 16);
1817
0
    fz_try(ctx)
1818
0
    {
1819
0
      ref = fz_xml_find_down(manifest, "ref");
1820
0
      while (ref)
1821
0
      {
1822
0
        const char *s = fz_xml_text(fz_xml_down(ref));
1823
0
        pdf_array_push(ctx, arr, annot_from_name(ctx, doc, s));
1824
0
        ref = fz_xml_find_next(ref, "ref");
1825
0
      }
1826
0
    }
1827
0
    fz_catch(ctx)
1828
0
    {
1829
0
      pdf_drop_obj(ctx, arr);
1830
0
      fz_rethrow(ctx);
1831
0
    }
1832
0
    return arr;
1833
0
  }
1834
1835
0
  return NULL;
1836
0
}
1837
1838
static void
1839
lock_field(fz_context *ctx, pdf_obj *f)
1840
0
{
1841
0
  int ff = pdf_dict_get_inheritable_int(ctx, f, PDF_NAME(Ff));
1842
1843
0
  if ((ff & PDF_FIELD_IS_READ_ONLY) ||
1844
0
    !pdf_name_eq(ctx, pdf_dict_get(ctx, f, PDF_NAME(Type)), PDF_NAME(Annot)) ||
1845
0
    !pdf_name_eq(ctx, pdf_dict_get(ctx, f, PDF_NAME(Subtype)), PDF_NAME(Widget)))
1846
0
    return;
1847
1848
0
  pdf_dict_put_int(ctx, f, PDF_NAME(Ff), ff | PDF_FIELD_IS_READ_ONLY);
1849
0
}
1850
1851
static void
1852
lock_xfa_locked_fields(fz_context *ctx, pdf_obj *a)
1853
0
{
1854
0
  int i;
1855
0
  int len = pdf_array_len(ctx, a);
1856
1857
0
  for (i = 0; i < len; i++)
1858
0
  {
1859
0
    lock_field(ctx, pdf_array_get(ctx, a, i));
1860
0
  }
1861
0
}
1862
1863
1864
void pdf_signature_set_value(fz_context *ctx, pdf_document *doc, pdf_obj *field, pdf_pkcs7_signer *signer, int64_t stime)
1865
0
{
1866
0
  pdf_obj *v = NULL;
1867
0
  pdf_obj *o = NULL;
1868
0
  pdf_obj *r = NULL;
1869
0
  pdf_obj *t = NULL;
1870
0
  pdf_obj *a = NULL;
1871
0
  pdf_obj *b = NULL;
1872
0
  pdf_obj *l = NULL;
1873
0
  pdf_obj *indv;
1874
0
  int vnum;
1875
0
  size_t max_digest_size;
1876
0
  char *buf = NULL;
1877
1878
0
  vnum = pdf_create_object(ctx, doc);
1879
0
  indv = pdf_new_indirect(ctx, doc, vnum, 0);
1880
0
  pdf_dict_put_drop(ctx, field, PDF_NAME(V), indv);
1881
1882
0
  max_digest_size = signer->max_digest_size(ctx, signer);
1883
1884
0
  fz_var(v);
1885
0
  fz_var(o);
1886
0
  fz_var(r);
1887
0
  fz_var(t);
1888
0
  fz_var(a);
1889
0
  fz_var(b);
1890
0
  fz_var(l);
1891
0
  fz_var(buf);
1892
0
  fz_try(ctx)
1893
0
  {
1894
0
    v = pdf_new_dict(ctx, doc, 4);
1895
0
    pdf_update_object(ctx, doc, vnum, v);
1896
1897
0
    buf = fz_calloc(ctx, max_digest_size, 1);
1898
1899
    /* Ensure that the /Filter entry is the first entry in the
1900
       dictionary after the digest contents since we look for
1901
       this tag when completing signatures in pdf-write.c in order
1902
       to generate the correct byte range. */
1903
0
    pdf_dict_put_array(ctx, v, PDF_NAME(ByteRange), 4);
1904
0
    pdf_dict_put_string(ctx, v, PDF_NAME(Contents), buf, max_digest_size);
1905
0
    pdf_dict_put(ctx, v, PDF_NAME(Filter), PDF_NAME(Adobe_PPKLite));
1906
0
    pdf_dict_put(ctx, v, PDF_NAME(SubFilter), PDF_NAME(adbe_pkcs7_detached));
1907
0
    pdf_dict_put(ctx, v, PDF_NAME(Type), PDF_NAME(Sig));
1908
0
    pdf_dict_put_date(ctx, v, PDF_NAME(M), stime);
1909
1910
0
    o = pdf_dict_put_array(ctx, v, PDF_NAME(Reference), 1);
1911
0
    r = pdf_array_put_dict(ctx, o, 0, 4);
1912
0
    pdf_dict_put(ctx, r, PDF_NAME(Data), pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root)));
1913
0
    pdf_dict_put(ctx, r, PDF_NAME(TransformMethod), PDF_NAME(FieldMDP));
1914
0
    pdf_dict_put(ctx, r, PDF_NAME(Type), PDF_NAME(SigRef));
1915
0
    t = pdf_dict_put_dict(ctx, r, PDF_NAME(TransformParams), 5);
1916
1917
0
    l = pdf_dict_getp(ctx, field, "Lock/Action");
1918
0
    if (l)
1919
0
    {
1920
0
      a = pdf_dict_getp(ctx, field, "Lock/Fields");
1921
0
    }
1922
0
    else
1923
0
    {
1924
      /* Lock action wasn't specified so we need to encode an Include.
1925
       * Before we just use an empty array, check in the XFA for locking
1926
       * details. */
1927
0
      a = get_locked_fields_from_xfa(ctx, doc, field);
1928
0
      if (a)
1929
0
        lock_xfa_locked_fields(ctx, a);
1930
1931
      /* If we don't get a result from the XFA, just encode an empty array
1932
       * (leave a == NULL), even if Lock/Fields exists because we don't really
1933
       * know what to do with the information if the action isn't defined. */
1934
0
      l = PDF_NAME(Include);
1935
0
    }
1936
1937
0
    pdf_dict_put(ctx, t, PDF_NAME(Action), l);
1938
1939
0
    if (pdf_name_eq(ctx, l, PDF_NAME(Include)) || pdf_name_eq(ctx, l, PDF_NAME(Exclude)))
1940
0
    {
1941
      /* For action Include and Exclude, we need to encode a Fields array */
1942
0
      if (!a)
1943
0
      {
1944
        /* If one wasn't defined or we chose to ignore it because no action
1945
         * was defined then use an empty one. */
1946
0
        b = pdf_new_array(ctx, doc, 0);
1947
0
        a = b;
1948
0
      }
1949
1950
0
      pdf_dict_put_drop(ctx, t, PDF_NAME(Fields), pdf_copy_array(ctx, a));
1951
0
    }
1952
1953
0
    pdf_dict_put(ctx, t, PDF_NAME(Type), PDF_NAME(TransformParams));
1954
0
    pdf_dict_put(ctx, t, PDF_NAME(V), PDF_NAME(1_2));
1955
1956
    /* Record details within the document structure so that contents
1957
    * and byte_range can be updated with their correct values at
1958
    * saving time */
1959
0
    pdf_xref_store_unsaved_signature(ctx, doc, field, signer);
1960
0
  }
1961
0
  fz_always(ctx)
1962
0
  {
1963
0
    pdf_drop_obj(ctx, v);
1964
0
    pdf_drop_obj(ctx, b);
1965
0
    fz_free(ctx, buf);
1966
0
  }
1967
0
  fz_catch(ctx)
1968
0
  {
1969
0
    fz_rethrow(ctx);
1970
0
  }
1971
0
}
1972
1973
void pdf_set_widget_editing_state(fz_context *ctx, pdf_annot *widget, int editing)
1974
0
{
1975
0
  widget->ignore_trigger_events = editing;
1976
0
}
1977
1978
int pdf_get_widget_editing_state(fz_context *ctx, pdf_annot *widget)
1979
0
{
1980
0
  return widget->ignore_trigger_events;
1981
0
}
1982
1983
static void pdf_execute_js_action(fz_context *ctx, pdf_document *doc, pdf_obj *target, const char *path, pdf_obj *js)
1984
0
{
1985
0
  if (js)
1986
0
  {
1987
0
    char *code = pdf_load_stream_or_string_as_utf8(ctx, js);
1988
0
    int in_op = 0;
1989
1990
0
    fz_var(in_op);
1991
0
    fz_try(ctx)
1992
0
    {
1993
0
      char buf[100];
1994
0
      fz_snprintf(buf, sizeof buf, "%d/%s", pdf_to_num(ctx, target), path);
1995
0
      pdf_begin_operation(ctx, doc, "Javascript Event");
1996
0
      in_op = 1;
1997
0
      pdf_js_execute(doc->js, buf, code, NULL);
1998
0
      pdf_end_operation(ctx, doc);
1999
0
    }
2000
0
    fz_always(ctx)
2001
0
    {
2002
0
      fz_free(ctx, code);
2003
0
    }
2004
0
    fz_catch(ctx)
2005
0
    {
2006
0
      if (in_op)
2007
0
        pdf_abandon_operation(ctx, doc);
2008
0
      fz_rethrow(ctx);
2009
0
    }
2010
0
  }
2011
0
}
2012
2013
static void pdf_execute_action_imp(fz_context *ctx, pdf_document *doc, pdf_obj *target, const char *path, pdf_obj *action)
2014
0
{
2015
0
  pdf_obj *S = pdf_dict_get(ctx, action, PDF_NAME(S));
2016
0
  if (pdf_name_eq(ctx, S, PDF_NAME(JavaScript)))
2017
0
  {
2018
0
    if (doc->js)
2019
0
      pdf_execute_js_action(ctx, doc, target, path, pdf_dict_get(ctx, action, PDF_NAME(JS)));
2020
0
  }
2021
0
  if (pdf_name_eq(ctx, S, PDF_NAME(ResetForm)))
2022
0
  {
2023
0
    pdf_obj *fields = pdf_dict_get(ctx, action, PDF_NAME(Fields));
2024
0
    int flags = pdf_dict_get_int(ctx, action, PDF_NAME(Flags));
2025
0
    pdf_reset_form(ctx, doc, fields, flags & 1);
2026
0
  }
2027
0
}
2028
2029
static void pdf_execute_action_chain(fz_context *ctx, pdf_document *doc, pdf_obj *target, const char *path, pdf_obj *action, pdf_cycle_list *cycle_up)
2030
0
{
2031
0
  pdf_cycle_list cycle;
2032
0
  pdf_obj *next;
2033
2034
0
  if (pdf_cycle(ctx, &cycle, cycle_up, action))
2035
0
    fz_throw(ctx, FZ_ERROR_FORMAT, "cycle in action chain");
2036
2037
0
  if (pdf_is_array(ctx, action))
2038
0
  {
2039
0
    int i, n = pdf_array_len(ctx, action);
2040
0
    for (i = 0; i < n; ++i)
2041
0
      pdf_execute_action_chain(ctx, doc, target, path, pdf_array_get(ctx, action, i), &cycle);
2042
0
  }
2043
0
  else
2044
0
  {
2045
0
    pdf_execute_action_imp(ctx, doc, target, path, action);
2046
0
    next = pdf_dict_get(ctx, action, PDF_NAME(Next));
2047
0
    if (next)
2048
0
      pdf_execute_action_chain(ctx, doc, target, path, next, &cycle);
2049
0
  }
2050
0
}
2051
2052
static void pdf_execute_action(fz_context *ctx, pdf_document *doc, pdf_obj *target, const char *path)
2053
0
{
2054
0
  pdf_obj *action = pdf_dict_getp_inheritable(ctx, target, path);
2055
0
  if (action)
2056
0
    pdf_execute_action_chain(ctx, doc, target, path, action, NULL);
2057
0
}
2058
2059
void pdf_document_event_will_close(fz_context *ctx, pdf_document *doc)
2060
0
{
2061
0
  pdf_execute_action(ctx, doc, pdf_trailer(ctx, doc), "Root/AA/WC");
2062
0
}
2063
2064
void pdf_document_event_will_save(fz_context *ctx, pdf_document *doc)
2065
0
{
2066
0
  pdf_execute_action(ctx, doc, pdf_trailer(ctx, doc), "Root/AA/WS");
2067
0
}
2068
2069
void pdf_document_event_did_save(fz_context *ctx, pdf_document *doc)
2070
0
{
2071
0
  pdf_execute_action(ctx, doc, pdf_trailer(ctx, doc), "Root/AA/DS");
2072
0
}
2073
2074
void pdf_document_event_will_print(fz_context *ctx, pdf_document *doc)
2075
0
{
2076
0
  pdf_execute_action(ctx, doc, pdf_trailer(ctx, doc), "Root/AA/WP");
2077
0
}
2078
2079
void pdf_document_event_did_print(fz_context *ctx, pdf_document *doc)
2080
0
{
2081
0
  pdf_execute_action(ctx, doc, pdf_trailer(ctx, doc), "Root/AA/DP");
2082
0
}
2083
2084
void pdf_page_event_open(fz_context *ctx, pdf_page *page)
2085
0
{
2086
0
  pdf_execute_action(ctx, page->doc, page->obj, "AA/O");
2087
0
}
2088
2089
void pdf_page_event_close(fz_context *ctx, pdf_page *page)
2090
0
{
2091
0
  pdf_execute_action(ctx, page->doc, page->obj, "AA/C");
2092
0
}
2093
2094
static void
2095
annot_execute_action(fz_context *ctx, pdf_annot *annot, const char *act)
2096
0
{
2097
0
  begin_annot_op(ctx, annot, "JavaScript action");
2098
2099
0
  fz_try(ctx)
2100
0
  {
2101
0
    pdf_execute_action(ctx, annot->page->doc, annot->obj, act);
2102
0
    end_annot_op(ctx, annot);
2103
0
  }
2104
0
  fz_catch(ctx)
2105
0
  {
2106
0
    abandon_annot_op(ctx, annot);
2107
0
    fz_rethrow(ctx);
2108
0
  }
2109
0
}
2110
2111
void pdf_annot_event_enter(fz_context *ctx, pdf_annot *annot)
2112
0
{
2113
0
  annot_execute_action(ctx, annot, "AA/E");
2114
0
}
2115
2116
void pdf_annot_event_exit(fz_context *ctx, pdf_annot *annot)
2117
0
{
2118
0
  annot_execute_action(ctx, annot, "AA/X");
2119
0
}
2120
2121
void pdf_annot_event_down(fz_context *ctx, pdf_annot *annot)
2122
0
{
2123
0
  annot_execute_action(ctx, annot, "AA/D");
2124
0
}
2125
2126
void pdf_annot_event_up(fz_context *ctx, pdf_annot *annot)
2127
0
{
2128
0
  pdf_obj *action;
2129
2130
0
  begin_annot_op(ctx, annot, "JavaScript action");
2131
2132
0
  fz_try(ctx)
2133
0
  {
2134
0
    action = pdf_dict_get(ctx, annot->obj, PDF_NAME(A));
2135
0
    if (action)
2136
0
      pdf_execute_action_chain(ctx, annot->page->doc, annot->obj, "A", action, NULL);
2137
0
    else
2138
0
      pdf_execute_action(ctx, annot->page->doc, annot->obj, "AA/U");
2139
0
    end_annot_op(ctx, annot);
2140
0
  }
2141
0
  fz_catch(ctx)
2142
0
  {
2143
0
    abandon_annot_op(ctx, annot);
2144
0
    fz_rethrow(ctx);
2145
0
  }
2146
0
}
2147
2148
void pdf_annot_event_focus(fz_context *ctx, pdf_annot *annot)
2149
0
{
2150
0
  annot_execute_action(ctx, annot, "AA/Fo");
2151
0
}
2152
2153
void pdf_annot_event_blur(fz_context *ctx, pdf_annot *annot)
2154
0
{
2155
0
  annot_execute_action(ctx, annot, "AA/Bl");
2156
0
}
2157
2158
void pdf_annot_event_page_open(fz_context *ctx, pdf_annot *annot)
2159
0
{
2160
0
  annot_execute_action(ctx, annot, "AA/PO");
2161
0
}
2162
2163
void pdf_annot_event_page_close(fz_context *ctx, pdf_annot *annot)
2164
0
{
2165
0
  annot_execute_action(ctx, annot, "AA/PC");
2166
0
}
2167
2168
void pdf_annot_event_page_visible(fz_context *ctx, pdf_annot *annot)
2169
0
{
2170
0
  annot_execute_action(ctx, annot, "AA/PV");
2171
0
}
2172
2173
void pdf_annot_event_page_invisible(fz_context *ctx, pdf_annot *annot)
2174
0
{
2175
0
  annot_execute_action(ctx, annot, "AA/PI");
2176
0
}
2177
2178
int pdf_field_event_keystroke(fz_context *ctx, pdf_document *doc, pdf_obj *field, pdf_keystroke_event *evt)
2179
0
{
2180
0
  pdf_js *js = doc->js;
2181
0
  if (js)
2182
0
  {
2183
0
    pdf_obj *action = pdf_dict_getp_inheritable(ctx, field, "AA/K/JS");
2184
0
    if (action)
2185
0
    {
2186
0
      pdf_js_event_init_keystroke(js, field, evt);
2187
0
      pdf_execute_js_action(ctx, doc, field, "AA/K/JS", action);
2188
0
      return pdf_js_event_result_keystroke(js, evt);
2189
0
    }
2190
0
  }
2191
0
  evt->newChange = fz_strdup(ctx, evt->change);
2192
0
  evt->newValue = fz_strdup(ctx, evt->value);
2193
0
  return 1;
2194
0
}
2195
2196
int pdf_annot_field_event_keystroke(fz_context *ctx, pdf_document *doc, pdf_annot *annot, pdf_keystroke_event *evt)
2197
0
{
2198
0
  int ret;
2199
2200
0
  pdf_annot_push_local_xref(ctx, annot);
2201
2202
0
  fz_try(ctx)
2203
0
    ret = pdf_field_event_keystroke(ctx, doc, annot->obj, evt);
2204
0
  fz_always(ctx)
2205
0
    pdf_annot_pop_local_xref(ctx, annot);
2206
0
  fz_catch(ctx)
2207
0
    fz_rethrow(ctx);
2208
2209
0
  return ret;
2210
0
}
2211
2212
char *pdf_field_event_format(fz_context *ctx, pdf_document *doc, pdf_obj *field)
2213
3.44k
{
2214
3.44k
  pdf_js *js = doc->js;
2215
3.44k
  if (js)
2216
0
  {
2217
0
    pdf_obj *action = pdf_dict_getp_inheritable(ctx, field, "AA/F/JS");
2218
0
    if (action)
2219
0
    {
2220
0
      const char *value = pdf_field_value(ctx, field);
2221
0
      pdf_js_event_init(js, field, value, 1);
2222
0
      pdf_execute_js_action(ctx, doc, field, "AA/F/JS", action);
2223
0
      return pdf_js_event_value(js);
2224
0
    }
2225
0
  }
2226
3.44k
  return NULL;
2227
3.44k
}
2228
2229
int pdf_field_event_validate(fz_context *ctx, pdf_document *doc, pdf_obj *field, const char *value, char **newvalue)
2230
0
{
2231
0
  pdf_js *js = doc->js;
2232
2233
0
  *newvalue = NULL;
2234
0
  if (js)
2235
0
  {
2236
0
    pdf_obj *action = pdf_dict_getp_inheritable(ctx, field, "AA/V/JS");
2237
0
    if (action)
2238
0
    {
2239
0
      pdf_js_event_init(js, field, value, 1);
2240
0
      pdf_execute_js_action(ctx, doc, field, "AA/V/JS", action);
2241
0
      return pdf_js_event_result_validate(js, newvalue);
2242
0
    }
2243
0
  }
2244
0
  return 1;
2245
0
}
2246
2247
void pdf_field_event_calculate(fz_context *ctx, pdf_document *doc, pdf_obj *field)
2248
0
{
2249
0
  pdf_js *js = doc->js;
2250
0
  if (js)
2251
0
  {
2252
0
    pdf_obj *action = pdf_dict_getp_inheritable(ctx, field, "AA/C/JS");
2253
0
    if (action)
2254
0
    {
2255
0
      char *old_value = fz_strdup(ctx, pdf_field_value(ctx, field));
2256
0
      char *new_value = NULL;
2257
0
      fz_var(new_value);
2258
0
      fz_try(ctx)
2259
0
      {
2260
0
        pdf_js_event_init(js, field, old_value, 1);
2261
0
        pdf_execute_js_action(ctx, doc, field, "AA/C/JS", action);
2262
0
        if (pdf_js_event_result(js))
2263
0
        {
2264
0
          new_value = pdf_js_event_value(js);
2265
0
          if (strcmp(old_value, new_value))
2266
0
            pdf_set_field_value(ctx, doc, field, new_value, 0);
2267
0
        }
2268
0
      }
2269
0
      fz_always(ctx)
2270
0
      {
2271
0
        fz_free(ctx, old_value);
2272
0
        fz_free(ctx, new_value);
2273
0
      }
2274
0
      fz_catch(ctx)
2275
0
        fz_rethrow(ctx);
2276
0
    }
2277
0
  }
2278
0
}
2279
2280
static void
2281
count_sigs(fz_context *ctx, pdf_obj *field, void *arg, pdf_obj **ft)
2282
0
{
2283
0
  int *n = (int *)arg;
2284
2285
0
  if (!pdf_name_eq(ctx, pdf_dict_get(ctx, field, PDF_NAME(Type)), PDF_NAME(Annot)) ||
2286
0
    !pdf_name_eq(ctx, pdf_dict_get(ctx, field, PDF_NAME(Subtype)), PDF_NAME(Widget)) ||
2287
0
    !pdf_name_eq(ctx, *ft, PDF_NAME(Sig)))
2288
0
    return;
2289
2290
0
  (*n)++;
2291
0
}
2292
2293
static pdf_obj *ft_name[2] = { PDF_NAME(FT), NULL };
2294
2295
int pdf_count_signatures(fz_context *ctx, pdf_document *doc)
2296
0
{
2297
0
  int n = 0;
2298
0
  pdf_obj *ft = NULL;
2299
0
  pdf_obj *form_fields = pdf_dict_getp(ctx, pdf_trailer(ctx, doc), "Root/AcroForm/Fields");
2300
0
  pdf_walk_tree(ctx, form_fields, PDF_NAME(Kids), count_sigs, NULL, &n, ft_name, &ft);
2301
0
  return n;
2302
0
}
2303
2304
/*
2305
 * Bake interactive form fields into static content.
2306
 */
2307
2308
static pdf_obj *get_annot_ap(fz_context *ctx, pdf_obj *annot)
2309
0
{
2310
0
  pdf_obj *ap = pdf_dict_get(ctx, annot, PDF_NAME(AP));
2311
0
  pdf_obj *as = pdf_dict_get(ctx, annot, PDF_NAME(AS));
2312
0
  if (ap)
2313
0
  {
2314
0
    ap = pdf_dict_get(ctx, ap, PDF_NAME(N));
2315
0
    if (pdf_is_stream(ctx, ap))
2316
0
      return ap;
2317
0
    ap = pdf_dict_get(ctx, ap, as);
2318
0
    if (pdf_is_stream(ctx, ap))
2319
0
      return ap;
2320
0
  }
2321
0
  return NULL;
2322
0
}
2323
2324
static fz_matrix get_annot_transform(fz_context *ctx, pdf_obj *annot, pdf_obj *ap)
2325
0
{
2326
0
  float w, h, x, y;
2327
0
  fz_matrix transform;
2328
0
  fz_rect bbox;
2329
0
  fz_rect rect;
2330
2331
0
  rect = pdf_dict_get_rect(ctx, annot, PDF_NAME(Rect));
2332
0
  bbox = pdf_dict_get_rect(ctx, ap, PDF_NAME(BBox));
2333
0
  transform = pdf_dict_get_matrix(ctx, ap, PDF_NAME(Matrix));
2334
2335
0
  bbox = fz_transform_rect(bbox, transform);
2336
0
  w = (rect.x1 - rect.x0) / (bbox.x1 - bbox.x0);
2337
0
  h = (rect.y1 - rect.y0) / (bbox.y1 - bbox.y0);
2338
0
  x = rect.x0 - bbox.x0 * w;
2339
0
  y = rect.y0 - bbox.y0 * h;
2340
2341
0
  return fz_make_matrix(w, 0, 0, h, x, y);
2342
0
}
2343
2344
static void pdf_bake_annot(fz_context *ctx, fz_buffer *buf, pdf_document *doc, pdf_obj *page, pdf_obj *res_xobj, pdf_obj *annot)
2345
0
{
2346
0
  fz_matrix m;
2347
0
  pdf_obj *ap;
2348
0
  char name[20];
2349
2350
0
  ap = get_annot_ap(ctx, annot);
2351
0
  if (ap)
2352
0
  {
2353
0
    fz_snprintf(name, sizeof name, "Annot%d", pdf_to_num(ctx, annot));
2354
0
    pdf_dict_puts(ctx, res_xobj, name, ap);
2355
0
    pdf_dict_put(ctx, ap, PDF_NAME(Type), PDF_NAME(XObject));
2356
0
    pdf_dict_put(ctx, ap, PDF_NAME(Subtype), PDF_NAME(Form));
2357
0
    m = get_annot_transform(ctx, annot, ap);
2358
0
    fz_append_printf(ctx, buf,
2359
0
      "q\n%g %g %g %g %g %g cm\n/%s Do\nQ\n",
2360
0
      m.a, m.b, m.c, m.d, m.e, m.f,
2361
0
      name
2362
0
    );
2363
0
  }
2364
0
}
2365
2366
static void pdf_bake_page(fz_context *ctx, pdf_document *doc, pdf_obj *page, int bake_annots, int bake_widgets)
2367
0
{
2368
0
  pdf_obj *res;
2369
0
  pdf_obj *res_xobj;
2370
0
  pdf_obj *contents;
2371
0
  pdf_obj *new_contents = NULL;
2372
0
  pdf_obj *annots;
2373
0
  pdf_obj *annot;
2374
0
  pdf_obj *subtype;
2375
0
  pdf_obj *prologue = NULL;
2376
0
  fz_buffer *buf = NULL;
2377
0
  int prepend, append;
2378
0
  int i;
2379
2380
0
  fz_var(buf);
2381
0
  fz_var(prologue);
2382
0
  fz_var(new_contents);
2383
2384
0
  annots = pdf_dict_get(ctx, page, PDF_NAME(Annots));
2385
0
  if (pdf_array_len(ctx, annots) == 0)
2386
0
    return;
2387
2388
0
  res = pdf_dict_get(ctx, page, PDF_NAME(Resources));
2389
0
  if (!res)
2390
0
    res = pdf_dict_put_dict(ctx, page, PDF_NAME(Resources), 4);
2391
2392
0
  res_xobj = pdf_dict_get(ctx, res, PDF_NAME(XObject));
2393
0
  if (!res_xobj)
2394
0
    res_xobj = pdf_dict_put_dict(ctx, res, PDF_NAME(XObject), 8);
2395
2396
0
  fz_try(ctx)
2397
0
  {
2398
    // Ensure that the graphics state is balanced.
2399
0
    contents = pdf_dict_get(ctx, page, PDF_NAME(Contents));
2400
0
    pdf_count_q_balance(ctx, doc, res, contents, &prepend, &append);
2401
2402
    // Prepend enough 'q' to ensure we can get back to initial state.
2403
0
    buf = fz_new_buffer(ctx, 1024);
2404
0
    while (prepend-- > 0)
2405
0
      fz_append_string(ctx, buf, "q\n");
2406
2407
0
    prologue = pdf_add_stream(ctx, doc, buf, NULL, 0);
2408
0
    fz_drop_buffer(ctx, buf);
2409
0
    buf = NULL;
2410
2411
    // Append enough 'Q' to get back to initial state.
2412
0
    buf = fz_new_buffer(ctx, 1024);
2413
0
    while (append-- > 0)
2414
0
      fz_append_string(ctx, buf, "Q\n");
2415
2416
0
    for (i = 0; i < pdf_array_len(ctx, annots); )
2417
0
    {
2418
0
      annot = pdf_array_get(ctx, annots, i);
2419
0
      subtype = pdf_dict_get(ctx, annot, PDF_NAME(Subtype));
2420
0
      if (subtype == PDF_NAME(Link))
2421
0
      {
2422
0
        ++i;
2423
0
      }
2424
0
      else if (subtype == PDF_NAME(Widget))
2425
0
      {
2426
0
        if (bake_widgets)
2427
0
        {
2428
0
          pdf_bake_annot(ctx, buf, doc, page, res_xobj, annot);
2429
0
          pdf_array_delete(ctx, annots, i);
2430
0
        }
2431
0
        else
2432
0
        {
2433
0
          ++i;
2434
0
        }
2435
0
      }
2436
0
      else
2437
0
      {
2438
0
        if (bake_annots)
2439
0
        {
2440
0
          pdf_bake_annot(ctx, buf, doc, page, res_xobj, annot);
2441
0
          pdf_array_delete(ctx, annots, i);
2442
0
        }
2443
0
        else
2444
0
        {
2445
0
          ++i;
2446
0
        }
2447
0
      }
2448
0
    }
2449
2450
0
    if (!pdf_is_array(ctx, contents))
2451
0
    {
2452
0
      new_contents = pdf_new_array(ctx, doc, 10);
2453
0
      pdf_array_push(ctx, new_contents, prologue);
2454
0
      pdf_array_push(ctx, new_contents, contents);
2455
0
      pdf_dict_put(ctx, page, PDF_NAME(Contents), new_contents);
2456
0
      pdf_drop_obj(ctx, new_contents);
2457
0
      contents = new_contents;
2458
0
      new_contents = NULL;
2459
0
    }
2460
0
    else
2461
0
    {
2462
0
      pdf_array_insert(ctx, contents, prologue, 0);
2463
0
    }
2464
2465
0
    pdf_array_push_drop(ctx, contents, pdf_add_stream(ctx, doc, buf, NULL, 0));
2466
0
  }
2467
0
  fz_always(ctx)
2468
0
  {
2469
0
    fz_drop_buffer(ctx, buf);
2470
0
    pdf_drop_obj(ctx, prologue);
2471
0
    pdf_drop_obj(ctx, new_contents);
2472
0
  }
2473
0
  fz_catch(ctx)
2474
0
  {
2475
0
    fz_rethrow(ctx);
2476
0
  }
2477
0
}
2478
2479
void pdf_bake_document(fz_context *ctx, pdf_document *doc, int bake_annots, int bake_widgets)
2480
0
{
2481
0
  pdf_page *page = NULL;
2482
0
  pdf_annot *annot;
2483
0
  int i, n;
2484
2485
0
  fz_var(page);
2486
2487
0
  pdf_begin_operation(ctx, doc, "Bake interactive content");
2488
0
  fz_try(ctx)
2489
0
  {
2490
0
    n = pdf_count_pages(ctx, doc);
2491
0
    for (i = 0; i < n; ++i)
2492
0
    {
2493
0
      page = pdf_load_page(ctx, doc, i);
2494
2495
0
      if (bake_annots)
2496
0
        for (annot = pdf_first_annot(ctx, page); annot; annot = pdf_next_annot(ctx, annot))
2497
0
          pdf_annot_request_synthesis(ctx, annot);
2498
0
      if (bake_widgets)
2499
0
        for (annot = pdf_first_widget(ctx, page); annot; annot = pdf_next_widget(ctx, annot))
2500
0
          pdf_annot_request_synthesis(ctx, annot);
2501
0
      pdf_update_page(ctx, page);
2502
2503
0
      pdf_bake_page(ctx, doc, page->obj, bake_annots, bake_widgets);
2504
2505
0
      fz_drop_page(ctx, (fz_page*)page);
2506
0
      page = NULL;
2507
0
    }
2508
2509
0
    if (bake_widgets)
2510
0
    {
2511
0
      pdf_obj *trailer = pdf_trailer(ctx, doc);
2512
0
      pdf_obj *root = pdf_dict_get(ctx, trailer, PDF_NAME(Root));
2513
0
      pdf_dict_del(ctx, root, PDF_NAME(AcroForm));
2514
0
    }
2515
0
    pdf_end_operation(ctx, doc);
2516
0
  }
2517
0
  fz_always(ctx)
2518
0
  {
2519
0
    fz_drop_page(ctx, (fz_page*)page);
2520
0
  }
2521
0
  fz_catch(ctx)
2522
0
  {
2523
0
    pdf_abandon_operation(ctx, doc);
2524
0
  }
2525
0
}