Debian Codegraph @

pdf/pdf_optcontent.c

raw
1 /* Copyright (C) 2019-2026 Artifex Software, Inc.
2 All Rights Reserved.
3
4 This software is provided AS-IS with no warranty, either express or
5 implied.
6
7 This software is distributed under license and may not be copied,
8 modified or distributed except as expressly authorized under the terms
9 of the license contained in the file LICENSE in this distribution.
10
11 Refer to licensing information at http://www.artifex.com or contact
12 Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
13 CA 94129, USA, for further information.
14 */
15
16 /* Optional Content routines */
17
18 #include "pdf_int.h"
19 #include "pdf_stack.h"
20 #include "pdf_misc.h"
21 #include "pdf_font_types.h"
22 #include "pdf_gstate.h"
23 #include "pdf_dict.h"
24 #include "pdf_array.h"
25 #include "pdf_doc.h"
26 #include "pdf_mark.h"
27 #include "pdf_optcontent.h"
28 #include "pdf_loop_detect.h"
29
30
31 /* Find the default value for an ocdict, based on contents of OCProperties */
32 /* NOTE: the spec says that if BaseState is present, it won't be set to "OFF",
33 * but this doesn't seem to be the case (Bug 691491). Also, the spec
34 * says the ON and OFF arrays are redundant in certain cases. We just
35 * look at everything anyway.
36 * Default is going to be visible unless anything here indicates that it
37 * should be turned off.
38 */
39 static bool
40 pdfi_get_default_OCG_val(pdf_context *ctx, pdf_dict *ocdict)
41 {
42 bool is_visible = true;
43 pdf_dict *D = NULL;
44 pdf_obj *BaseState = NULL;
45 pdf_array *OFF = NULL;
46 pdf_array *ON = NULL;
47 int code;
48
49 if (ctx->OCProperties == NULL)
50 return is_visible;
51
52 code = pdfi_dict_knownget_type(ctx, ctx->OCProperties, "D", PDF_DICT, (pdf_obj **)&D);
53 if (code <= 0)
54 goto cleanup;
55
56 code = pdfi_dict_knownget_type(ctx, D, "BaseState", PDF_NAME, &BaseState);
57 if (code < 0) {
58 goto cleanup;
59 }
60 if (code > 0) {
61 if (pdfi_name_is((pdf_name *)BaseState, "OFF")) {
62 is_visible = false;
63 }
64 }
65
66 if (!is_visible) {
67 code = pdfi_dict_knownget_type(ctx, D, "ON", PDF_ARRAY, (pdf_obj **)&ON);
68 if (code < 0)
69 goto cleanup;
70 if (code > 0) {
71 if (pdfi_array_known(ctx, ON, (pdf_obj *)ocdict, NULL))
72 is_visible = true;
73 }
74 }
75
76 if (is_visible) {
77 code = pdfi_dict_knownget_type(ctx, D, "OFF", PDF_ARRAY, (pdf_obj **)&OFF);
78 if (code < 0)
79 goto cleanup;
80 if (code > 0) {
81 if (pdfi_array_known(ctx, OFF, (pdf_obj *)ocdict, NULL))
82 is_visible = false;
83 }
84 }
85
86
87 cleanup:
88 pdfi_countdown(BaseState);
89 pdfi_countdown(D);
90 pdfi_countdown(OFF);
91 pdfi_countdown(ON);
92 return is_visible;
93 }
94
95 /* Check Usage for an OCG */
96 static bool
97 pdfi_oc_check_OCG_usage(pdf_context *ctx, pdf_dict *ocdict)
98 {
99 bool is_visible = true;
100 int code;
101 pdf_dict *Usage = NULL;
102 pdf_dict *dict = NULL;
103 pdf_obj *name = NULL;
104
105 /* Check Usage to see if it has additional info */
106 code = pdfi_dict_knownget_type(ctx, ocdict, "Usage", PDF_DICT, (pdf_obj **)&Usage);
107 if (code <= 0) {
108 /* No Usage, so we're done */
109 goto cleanup;
110 }
111
112 if (ctx->args.printed) {
113 code = pdfi_dict_knownget_type(ctx, Usage, "Print", PDF_DICT, (pdf_obj **)&dict);
114 if (code <= 0)
115 goto cleanup;
116 code = pdfi_dict_knownget_type(ctx, dict, "PrintState", PDF_NAME, &name);
117 if (code <= 0)
118 goto cleanup;
119 } else {
120 code = pdfi_dict_knownget_type(ctx, Usage, "View", PDF_DICT, (pdf_obj **)&dict);
121 if (code <= 0)
122 goto cleanup;
123 code = pdfi_dict_knownget_type(ctx, dict, "ViewState", PDF_NAME, &name);
124 if (code <= 0)
125 goto cleanup;
126 }
127 if (pdfi_name_strcmp((pdf_name *)name, "OFF") == 0) {
128 is_visible = false;
129 }
130
131 cleanup:
132 pdfi_countdown(Usage);
133 pdfi_countdown(dict);
134 pdfi_countdown(name);
135
136 return is_visible;
137 }
138
139 typedef enum {
140 P_AnyOn,
141 P_AllOn,
142 P_AllOff,
143 P_AnyOff
144 } ocmd_p_type;
145
146 static bool pdfi_oc_default_visibility(pdf_context *ctx)
147 {
148 int code;
149 pdf_dict *D_dict = NULL;
150 pdf_name *B = NULL;
151
152 code = pdfi_dict_knownget_type(ctx, ctx->OCProperties, "D", PDF_DICT, (pdf_obj **)&D_dict);
153 if (code < 0 || D_dict == NULL)
154 return true;
155
156 code = pdfi_dict_knownget_type(ctx, D_dict, "BaseState", PDF_NAME, (pdf_obj **)&B);
157 (void)pdfi_countdown(D_dict);
158 D_dict = NULL;
159 if (code < 0 || B == NULL)
160 return true;
161
162 if (pdfi_name_is(B, "OFF")) {
163 (void)pdfi_countdown(B);
164 return false;
165 }
166
167 (void)pdfi_countdown(B);
168 return true;
169 }
170
171 static bool
172 pdfi_oc_check_OCMD_array(pdf_context *ctx, pdf_array *array, ocmd_p_type type)
173 {
174 bool is_visible, hit = false;
175 uint64_t i;
176 int code;
177 pdf_obj *val = NULL;
178
179 /* Setup default */
180 switch (type) {
181 case P_AnyOn:
182 case P_AnyOff:
183 is_visible = false;
184 break;
185 case P_AllOn:
186 case P_AllOff:
187 is_visible = true;
188 break;
189 }
190
191 for (i=0; i<pdfi_array_size(array); i++) {
192 bool vis;
193
194 code = pdfi_array_get(ctx, array, i, &val);
195 if (code < 0) continue;
196 if (pdfi_type_of(val) != PDF_DICT) {
197 dbgprintf1("WARNING: OCMD array contains item type %d, expected PDF_DICT or PDF_NULL\n", pdfi_type_of(val));
198 pdfi_countdown(val);
199 val = NULL;
200 continue;
201 }
202
203 hit = true;
204 vis = pdfi_get_default_OCG_val(ctx, (pdf_dict *)val);
205 switch (type) {
206 case P_AnyOn:
207 /* visible if any is on */
208 if (vis) {
209 is_visible = true;
210 goto cleanup;
211 }
212 break;
213 case P_AllOn:
214 /* visible if all on */
215 if (!vis) {
216 is_visible = false;
217 goto cleanup;
218 }
219 break;
220 case P_AllOff:
221 /* visible if all are off */
222 if (vis) {
223 is_visible = false;
224 goto cleanup;
225 }
226 break;
227 case P_AnyOff:
228 /* visible if any is off */
229 if (!vis) {
230 is_visible = true;
231 goto cleanup;
232 }
233 break;
234 }
235 pdfi_countdown(val);
236 val = NULL;
237 }
238
239 /* If the array was empty, or contained only null or deleted entries, then it has no effect
240 * PDF Reference 1.7 p366, table 4.49, OCGs entry. I'm interpreting this to mean that we should use
241 * the OCProperties 'D' dictionary, set the visibility state to the BaseState. Since this is an OCMD
242 * I'm assuming that the ON and OFF arrays (which apply to Optional Content Groups) shouold not be
243 * consulted. We need to cater for the fact that BaseState is optional (but default is ON). D is
244 * specified as 'Required' but it seems best not to take any chances on that!
245 */
246 if (!hit)
247 is_visible = pdfi_oc_default_visibility(ctx);
248
249 cleanup:
250 pdfi_countdown(val);
251 return is_visible;
252 }
253
254 static bool
255 pdfi_oc_check_OCMD(pdf_context *ctx, pdf_dict *ocdict)
256 {
257 bool is_visible = true;
258 int code;
259 pdf_obj *VE = NULL;
260 pdf_obj *obj = NULL;
261 pdf_obj *Pname = NULL;
262 pdf_dict *OCGs_dict = NULL; /* alias, don't need to free */
263 pdf_array *OCGs_array = NULL; /* alias, don't need to free */
264 pdf_dict *UsageDict = NULL, *StateDict = NULL;
265 pdf_obj *State = NULL;
266 ocmd_p_type Ptype = P_AnyOn;
267
268 /* TODO: We don't support this, so log a warning and ignore */
269 code = pdfi_dict_knownget_type(ctx, ocdict, "VE", PDF_ARRAY, &VE);
270 if (code > 0) {
271 dbgprintf("WARNING: OCMD contains VE, which is not supported (ignoring)\n");
272 }
273
274 code = pdfi_dict_knownget(ctx, ocdict, "OCGs", &obj);
275 if (code <= 0)
276 goto cleanup;
277 if (pdfi_type_of(obj) == PDF_ARRAY) {
278 OCGs_array = (pdf_array *)obj;
279 } else if (pdfi_type_of(obj) == PDF_DICT) {
280 OCGs_dict = (pdf_dict *)obj;
281 } else {
282 is_visible = pdfi_oc_default_visibility(ctx);
283 goto cleanup;
284 }
285
286 code = pdfi_dict_knownget_type(ctx, ocdict, "P", PDF_NAME, &Pname);
287 if (code < 0)
288 goto cleanup;
289 if (code == 0 || pdfi_name_is((pdf_name *)Pname, "AnyOn")) {
290 Ptype = P_AnyOn;
291 } else if (pdfi_name_is((pdf_name *)Pname, "AllOn")) {
292 Ptype = P_AllOn;
293 } else if (pdfi_name_is((pdf_name *)Pname, "AnyOff")) {
294 Ptype = P_AnyOff;
295 } else if (pdfi_name_is((pdf_name *)Pname, "AllOff")) {
296 Ptype = P_AllOff;
297 } else {
298 Ptype = P_AnyOn;
299 }
300
301 if (OCGs_dict) {
302 switch (Ptype) {
303 case P_AnyOn:
304 case P_AllOn:
305 is_visible = pdfi_get_default_OCG_val(ctx, OCGs_dict);
306 break;
307 case P_AllOff:
308 case P_AnyOff:
309 is_visible = !pdfi_get_default_OCG_val(ctx, OCGs_dict);
310 break;
311 }
312 } else {
313 is_visible = pdfi_oc_check_OCMD_array(ctx, OCGs_array, Ptype);
314 }
315
316 if (OCGs_dict) {
317 code = pdfi_dict_knownget_type(ctx, OCGs_dict, "Usage", PDF_DICT, (pdf_obj **)&UsageDict);
318 if (code < 0)
319 goto cleanup;
320
321 if (UsageDict != NULL) {
322 if (ctx->args.printed) {
323 code = pdfi_dict_knownget_type(ctx, UsageDict, "Print", PDF_DICT, (pdf_obj **)&StateDict);
324 if (code < 0)
325 goto cleanup;
326 if (StateDict) {
327 code = pdfi_dict_knownget_type(ctx, StateDict, "PrintState", PDF_NAME, &State);
328 if (code < 0)
329 goto cleanup;
330 }
331 } else {
332 code = pdfi_dict_knownget_type(ctx, UsageDict, "View", PDF_DICT, (pdf_obj **)&StateDict);
333 if (code < 0)
334 goto cleanup;
335 if (StateDict) {
336 code = pdfi_dict_knownget_type(ctx, StateDict, "ViewState", PDF_NAME, &State);
337 if (code < 0)
338 goto cleanup;
339 }
340 }
341 if (State) {
342 if (pdfi_name_is((const pdf_name *)State, "ON"))
343 is_visible = true;
344 else
345 if (pdfi_name_is((const pdf_name *)State, "OFF"))
346 is_visible = false;
347 else
348 pdfi_set_error(ctx, 0, NULL, E_PDF_BAD_VALUE, "pdfi_oc_check_OCMD", "Usage Dictionary State is neither ON nor OFF");
349 }
350 }
351 }
352
353 cleanup:
354 pdfi_countdown(State);
355 pdfi_countdown(StateDict);
356 pdfi_countdown(UsageDict);
357 pdfi_countdown(VE);
358 pdfi_countdown(obj);
359 pdfi_countdown(Pname);
360
361 return is_visible;
362 }
363
364 static bool pdfi_must_check_usage(pdf_context *ctx, int64_t object)
365 {
366 pdf_dict *D = NULL, *EventDict = NULL;
367 pdf_array *AS = NULL, *OCGs = NULL;
368 pdf_name *Event = NULL;
369 bool check = false;
370 int i, code = 0;
371
372 if (ctx->OCProperties != NULL) {
373 if (pdfi_dict_knownget_type(ctx, ctx->OCProperties, "D", PDF_DICT, (pdf_obj **)&D) > 0) {
374 if (pdfi_dict_knownget_type(ctx, D, "AS", PDF_ARRAY, (pdf_obj **)&AS) > 0) {
375 for (i = 0; i < pdfi_array_size(AS); i++) {
376 code = pdfi_array_get(ctx, AS, i, (pdf_obj **)&EventDict);
377 if (code < 0)
378 goto exit;
379 if (pdfi_type_of(EventDict) == PDF_DICT) {
380 if (pdfi_dict_knownget_type(ctx, EventDict, "Event", PDF_NAME, (pdf_obj **)&Event) > 0) {
381 if (ctx->args.printed) {
382 if (Event->length == 5 && strncmp((const char *)Event->data, "Print", 5) == 0) {
383 if (pdfi_dict_knownget_type(ctx, EventDict, "OCGs", PDF_ARRAY, (pdf_obj **)&OCGs) > 0) {
384 pdfi_countdown(Event);
385 Event = NULL;
386 pdfi_countdown(EventDict);
387 EventDict = NULL;
388 break;
389 }
390 }
391 } else {
392 if (Event->length == 4 && strncmp((const char *)Event->data, "View", 4) == 0) {
393 if (pdfi_dict_knownget_type(ctx, EventDict, "OCGs", PDF_ARRAY, (pdf_obj **)&OCGs) > 0) {
394 pdfi_countdown(Event);
395 Event = NULL;
396 pdfi_countdown(EventDict);
397 EventDict = NULL;
398 break;
399 }
400 }
401 }
402 pdfi_countdown(Event);
403 Event = NULL;
404 }
405 }
406 pdfi_countdown(EventDict);
407 EventDict = NULL;
408 }
409 pdfi_countdown(AS);
410 AS = NULL;
411 if (OCGs) {
412 pdf_obj *Group = NULL;
413
414 for (i = 0; i < pdfi_array_size(OCGs); i++) {
415 code = pdfi_array_get(ctx, OCGs, i, (pdf_obj **)&Group);
416 if (code < 0)
417 goto exit;
418 if (Group->object_num == object) {
419 pdfi_countdown(Group);
420 check = true;
421 goto exit;
422 }
423 pdfi_countdown(Group);
424 Group = NULL;
425 }
426 pdfi_countdown(OCGs);
427 OCGs = NULL;
428 }
429 }
430 pdfi_countdown(D);
431 D = NULL;
432 }
433 }
434 exit:
435 pdfi_countdown(OCGs);
436 pdfi_countdown(Event);
437 pdfi_countdown(EventDict);
438 pdfi_countdown(AS);
439 pdfi_countdown(D);
440 return check;
441 }
442
443 /* Check if an OCG or OCMD is visible, passing in OC dict */
444 bool
445 pdfi_oc_is_ocg_visible(pdf_context *ctx, pdf_dict *ocdict)
446 {
447 pdf_name *type = NULL;
448 bool is_visible = true;
449 int code;
450
451 /* Type can be either OCMD or OCG.
452 */
453 code = pdfi_dict_knownget_type(ctx, ocdict, "Type", PDF_NAME, (pdf_obj **)&type);
454 if (code <= 0)
455 goto cleanup;
456
457 if (pdfi_name_is(type, "OCMD")) {
458 is_visible = pdfi_oc_check_OCMD(ctx, ocdict);
459 } else if (pdfi_name_is(type, "OCG")) {
460 is_visible = pdfi_get_default_OCG_val(ctx, ocdict);
461 if (pdfi_must_check_usage(ctx, ocdict->object_num))
462 is_visible = pdfi_oc_check_OCG_usage(ctx, ocdict);
463 } else {
464 char str[100];
465 memcpy(str, (const char *)type->data, type->length < 100 ? type->length : 99);
466 str[type->length < 100 ? type->length : 99] = '\0';
467 dbgprintf1("WARNING: OC dict type is %s, expected OCG or OCMD\n", str);
468 }
469
470 cleanup:
471 pdfi_countdown(type);
472
473 if (ctx->args.pdfdebug) {
474 outprintf(ctx->memory, "OCG: OC Dict %d %s visible\n", ocdict->object_num,
475 is_visible ? "IS" : "IS NOT");
476 }
477 return is_visible;
478 }
479
480 #define NUM_CONTENT_LEVELS 100
481 typedef struct {
482 byte *flags;
483 uint64_t num_off;
484 uint64_t max_flags;
485 } pdfi_oc_levels_t;
486
487 static int pdfi_oc_levels_init(pdf_context *ctx, pdfi_oc_levels_t **levels)
488 {
489 byte *data;
490 pdfi_oc_levels_t *new;
491
492 *levels = NULL;
493
494 new = (pdfi_oc_levels_t *)gs_alloc_bytes(ctx->memory, sizeof(pdfi_oc_levels_t),
495 "pdfi_oc_levels_init (levels)");
496 if (!new)
497 return_error(gs_error_VMerror);
498
499 data = (byte *)gs_alloc_bytes(ctx->memory, NUM_CONTENT_LEVELS, "pdfi_oc_levels_init (data)");
500 if (!data) {
501 gs_free_object(ctx->memory, new, "pdfi_oc_levels_init (levels (error))");
502 return_error(gs_error_VMerror);
503 }
504 memset(data, 0, NUM_CONTENT_LEVELS);
505
506 new->flags = data;
507 new->num_off = 0;
508 new->max_flags = NUM_CONTENT_LEVELS;
509 *levels = new;
510
511 return 0;
512 }
513
514 static int pdfi_oc_levels_free(pdf_context *ctx, pdfi_oc_levels_t *levels)
515 {
516 if (!levels)
517 return 0;
518 gs_free_object(ctx->memory, levels->flags, "pdfi_oc_levels_free (flags)");
519 gs_free_object(ctx->memory, levels, "pdfi_oc_levels_free (levels)");
520
521 return 0;
522 }
523
524 static int pdfi_oc_levels_set(pdf_context *ctx, pdfi_oc_levels_t *levels, uint64_t index)
525 {
526 byte *new = NULL;
527 uint64_t newmax;
528
529 if (index > levels->max_flags - 1) {
530 /* Expand the flags buffer */
531 newmax = levels->max_flags + NUM_CONTENT_LEVELS;
532 if (index >= newmax)
533 return_error(gs_error_Fatal); /* shouldn't happen */
534 new = gs_alloc_bytes(ctx->memory, newmax, "pdfi_oc_levels_set (new data)");
535 if (!new)
536 return_error(gs_error_VMerror);
537 memset(new, 0, newmax);
538 memcpy(new, levels->flags, levels->max_flags);
539 gs_free_object(ctx->memory, levels->flags, "pdfi_oc_levels_set (old data)");
540 levels->flags = new;
541 levels->max_flags += NUM_CONTENT_LEVELS;
542 }
543
544 if (levels->flags[index] == 0)
545 levels->num_off ++;
546 levels->flags[index] = 1;
547 return 0;
548 }
549
550 static int pdfi_oc_levels_clear(pdf_context *ctx, pdfi_oc_levels_t *levels, uint64_t index)
551 {
552 if (index > levels->max_flags - 1)
553 return -1;
554 if (levels->flags[index] != 0)
555 levels->num_off --;
556 levels->flags[index] = 0;
557 return 0;
558 }
559
560
561 /* Test if content is turned off for this element.
562 */
563 bool pdfi_oc_is_off(pdf_context *ctx)
564 {
565 pdfi_oc_levels_t *levels = (pdfi_oc_levels_t *)ctx->OFFlevels;
566 uint64_t num_off = levels->num_off;
567
568 return (num_off != 0);
569 }
570
571 int pdfi_oc_init(pdf_context *ctx)
572 {
573 int code;
574
575 ctx->BMClevel = 0;
576 if (ctx->OFFlevels) {
577 pdfi_oc_levels_free(ctx, ctx->OFFlevels);
578 ctx->OFFlevels = NULL;
579 }
580 code = pdfi_oc_levels_init(ctx, (pdfi_oc_levels_t **)&ctx->OFFlevels);
581 if (code < 0)
582 return code;
583
584 return 0;
585 }
586
587 int pdfi_oc_free(pdf_context *ctx)
588 {
589 int code;
590
591 code = pdfi_oc_levels_free(ctx, (pdfi_oc_levels_t *)ctx->OFFlevels);
592 ctx->OFFlevels = NULL;
593 return code;
594 }
595
596 int pdfi_op_MP(pdf_context *ctx)
597 {
598 pdf_obj *o = NULL;
599 int code = 0;
600
601 if (pdfi_count_stack(ctx) < 1)
602 return_error(gs_error_stackunderflow);
603
604 if (!ctx->device_state.writepdfmarks || !ctx->args.preservemarkedcontent) {
605 pdfi_pop(ctx, 1);
606 goto exit;
607 }
608
609 o = ctx->stack_top[-1];
610 pdfi_countup(o);
611 pdfi_pop(ctx, 1);
612
613 if (pdfi_type_of(o) != PDF_NAME) {
614 code = gs_note_error(gs_error_typecheck);
615 goto exit;
616 }
617
618 code = pdfi_pdfmark_from_objarray(ctx, &o, 1, NULL, "MP");
619 ctx->BMClevel ++;
620
621 exit:
622 pdfi_countdown(o);
623 return code;
624 }
625
626 int pdfi_op_DP(pdf_context *ctx, pdf_dict *stream_dict, pdf_dict *page_dict)
627 {
628 pdf_name *properties = NULL;
629 int code = 0;
630 pdf_obj **objarray = NULL, *o = NULL;
631
632 if (pdfi_count_stack(ctx) < 2) {
633 pdfi_clearstack(ctx);
634 return gs_note_error(gs_error_stackunderflow);
635 }
636
637 if (!ctx->device_state.writepdfmarks || !ctx->args.preservemarkedcontent) {
638 pdfi_pop(ctx, 2); /* pop args */
639 goto exit;
640 }
641
642 if (pdfi_type_of(ctx->stack_top[-2]) != PDF_NAME) {
643 pdfi_pop(ctx, 2); /* pop args */
644 code = gs_note_error(gs_error_typecheck);
645 goto exit;
646 }
647
648 objarray = (pdf_obj **)gs_alloc_bytes(ctx->memory, 2 * sizeof(pdf_obj *), "pdfi_op_DP");
649 if (objarray == NULL) {
650 code = gs_note_error(gs_error_VMerror);
651 goto exit;
652 }
653
654 objarray[0] = ctx->stack_top[-2];
655 pdfi_countup(objarray[0]);
656 o = ctx->stack_top[-1];
657 pdfi_countup(o);
658 pdfi_pop(ctx, 2); /* pop args */
659
660 switch (pdfi_type_of(o)) {
661 case PDF_NAME:
662 code = pdfi_find_resource(ctx, (unsigned char *)"Properties", (pdf_name *)o, stream_dict, page_dict, (pdf_obj **)&properties);
663 if(code < 0)
664 goto exit;
665 if (pdfi_type_of(properties) != PDF_DICT) {
666 code = gs_note_error(gs_error_typecheck);
667 goto exit;
668 }
669 objarray[1] = (pdf_obj *)properties;
670 break;
671 case PDF_DICT:
672 objarray[1] = o;
673 break;
674 default:
675 code = gs_note_error(gs_error_VMerror);
676 goto exit;
677 }
678
679 code = pdfi_pdfmark_from_objarray(ctx, objarray, 2, NULL, "DP");
680
681 exit:
682 if (objarray != NULL) {
683 pdfi_countdown(objarray[0]);
684 gs_free_object(ctx->memory, objarray, "free pdfi_op_DP");
685 }
686 pdfi_countdown(o);
687 pdfi_countdown(properties);
688 return code;
689 }
690
691 /* begin marked content sequence */
692 int pdfi_op_BMC(pdf_context *ctx)
693 {
694 pdf_obj *o = NULL;
695 int code = 0;
696
697 /* This will also prevent us writing out an EMC if the BMC is in any way invalid */
698 ctx->BDCWasOC = true;
699
700 if (pdfi_count_stack(ctx) < 1)
701 return_error(gs_error_stackunderflow);
702
703 if (!ctx->device_state.writepdfmarks || !ctx->args.preservemarkedcontent) {
704 /* Need to increment the BMCLevel anyway, as the EMC will count it down.
705 * If we already have a BDC, then that effectively will turn it on.
706 */
707 ctx->BMClevel ++;
708 pdfi_pop(ctx, 1);
709 goto exit;
710 }
711
712 o = ctx->stack_top[-1];
713 pdfi_countup(o);
714 pdfi_pop(ctx, 1);
715
716 if (pdfi_type_of(o) != PDF_NAME) {
717 code = gs_note_error(gs_error_typecheck);
718 goto exit;
719 }
720
721 ctx->BDCWasOC = false;
722 code = pdfi_pdfmark_from_objarray(ctx, &o, 1, NULL, "BMC");
723 ctx->BMClevel ++;
724
725 exit:
726 pdfi_countdown(o);
727 return code;
728 }
729
730 /* begin marked content sequence with property list */
731 int pdfi_op_BDC(pdf_context *ctx, pdf_dict *stream_dict, pdf_dict *page_dict)
732 {
733 pdf_name *tag = NULL;
734 pdf_name *properties = NULL;
735 pdf_dict *oc_dict = NULL;
736 int code = 0;
737 bool ocg_is_visible;
738 pdf_obj **objarray = NULL, *o = NULL;
739 pdf_indirect_ref *dictref = NULL;
740
741 /* This will also prevent us writing out an EMC if the BDC is in any way invalid */
742 ctx->BDCWasOC = true;
743
744 if (pdfi_count_stack(ctx) < 2) {
745 pdfi_clearstack(ctx);
746 return gs_note_error(gs_error_stackunderflow);
747 }
748
749 ctx->BMClevel ++;
750
751 tag = (pdf_name *)ctx->stack_top[-2];
752 pdfi_countup(tag);
753 o = ctx->stack_top[-1];
754 pdfi_countup(o);
755 pdfi_pop(ctx, 2);
756
757 if (pdfi_type_of(tag) != PDF_NAME)
758 goto exit;
759
760 if (!pdfi_name_is(tag, "OC"))
761 ctx->BDCWasOC = false;
762
763 if (ctx->device_state.writepdfmarks && ctx->args.preservemarkedcontent && (!ctx->BDCWasOC || ctx->device_state.WantsOptionalContent)) {
764 objarray = (pdf_obj **)gs_alloc_bytes(ctx->memory, 2 * sizeof(pdf_obj *), "pdfi_op_BDC");
765 if (objarray == NULL) {
766 code = gs_note_error(gs_error_VMerror);
767 goto exit;
768 }
769
770 objarray[0] = (pdf_obj *)tag;
771
772 switch (pdfi_type_of(o)) {
773 case PDF_NAME:
774 code = pdfi_find_resource(ctx, (unsigned char *)"Properties", (pdf_name *)o, stream_dict, page_dict, (pdf_obj **)&oc_dict);
775 if(code < 0)
776 goto exit;
777 if (pdfi_type_of(oc_dict) != PDF_DICT) {
778 code = gs_note_error(gs_error_typecheck);
779 goto exit;
780 }
781 /* If we are producing PDF/A we must not include any Metadata, as that
782 * requires us to modify the XMP Metadata, which we don't know how to do.
783 */
784 if (ctx->args.PDFA > 0) {
785 uint64_t index = 0;
786 pdf_name *Key = NULL;
787 pdf_obj *Value = NULL;
788
789 code = pdfi_dict_first(ctx, oc_dict, (pdf_obj **)&Key, &Value, &index);
790 if (code < 0) {
791 if (code == gs_error_undefined)
792 code = 0;
793 goto exit;
794 }
795 while (code >= 0) {
796 if (pdfi_name_is(Key, "Metadata")) {
797 pdfi_dict_delete_pair(ctx, oc_dict, Key);
798 }
799 pdfi_countdown(Key);
800 Key = NULL;
801 pdfi_countdown(Value);
802 Value = NULL;
803
804 code = pdfi_dict_next(ctx, oc_dict, (pdf_obj **)&Key, &Value, &index);
805 if (code == gs_error_undefined) {
806 code = 0;
807 break;
808 }
809 }
810 }
811 if (pdfi_dict_entries(oc_dict) == 0)
812 goto exit;
813
814 code = pdfi_pdfmark_dict(ctx, oc_dict);
815 if (code < 0)
816 goto exit;
817
818 /* Create an indirect ref for the dict */
819 code = pdfi_object_alloc(ctx, PDF_INDIRECT, 0, (pdf_obj **)&dictref);
820 if (code < 0) goto exit;
821 pdfi_countup(dictref);
822 dictref->ref_object_num = oc_dict->object_num;
823 dictref->ref_generation_num = oc_dict->generation_num;
824 dictref->is_marking = true;
825
826 objarray[1] = (pdf_obj *)dictref;
827 break;
828 case PDF_DICT:
829 objarray[1] = o;
830 break;
831 default:
832 code = gs_note_error(gs_error_VMerror);
833 goto exit;
834 }
835
836 code = pdfi_pdfmark_from_objarray(ctx, objarray, 2, NULL, "BDC");
837 goto exit;
838 }
839
840 if (pdfi_name_is(tag, "OC")) {
841 /* Check if first arg is a name and handle it if so */
842 /* TODO: spec says it could also be an inline dict that we should be able to handle,
843 * but I am just matching what gs does for now, and it doesn't handle that case.
844 */
845 properties = (pdf_name *)o;
846 if (pdfi_type_of(properties) != PDF_NAME)
847 goto exit;
848
849 code = pdfi_loop_detector_mark(ctx);
850 /* If it's a name, look it up in Properties */
851 code = pdfi_find_resource(ctx, (unsigned char *)"Properties", properties,
852 (pdf_dict *)stream_dict, page_dict, (pdf_obj **)&oc_dict);
853 (void)pdfi_loop_detector_cleartomark(ctx);
854 if (code != 0)
855 goto exit;
856 if (pdfi_type_of(oc_dict) != PDF_DICT)
857 goto exit;
858
859 ocg_is_visible = pdfi_oc_is_ocg_visible(ctx, oc_dict);
860 if (!ocg_is_visible)
861 code = pdfi_oc_levels_set(ctx, ctx->OFFlevels, ctx->BMClevel);
862
863 }
864
865 exit:
866 if (objarray != NULL)
867 gs_free_object(ctx->memory, objarray, "free pdfi_op_BDC");
868 pdfi_countdown(dictref);
869 pdfi_countdown(o);
870 pdfi_countdown(tag);
871 pdfi_countdown(oc_dict);
872 return code;
873 }
874
875 /* end marked content sequence */
876 int pdfi_op_EMC(pdf_context *ctx)
877 {
878 int code, code1 = 0;
879
880 if (ctx->device_state.writepdfmarks && ctx->args.preservemarkedcontent && (!ctx->BDCWasOC || ctx->device_state.WantsOptionalContent))
881 code1 = pdfi_pdfmark_from_objarray(ctx, NULL, 0, NULL, "EMC");
882
883 code = pdfi_oc_levels_clear(ctx, ctx->OFFlevels, ctx->BMClevel);
884 if (code == 0)
885 code = code1;
886
887 /* TODO: Should we flag error on too many EMC? */
888 if (ctx->BMClevel > 0)
889 ctx->BMClevel --;
890 return code;
891 }