/src/ghostpdl/pdf/pdf_optcontent.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2019-2025 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 | 3.36k | { |
42 | 3.36k | bool is_visible = true; |
43 | 3.36k | pdf_dict *D = NULL; |
44 | 3.36k | pdf_obj *BaseState = NULL; |
45 | 3.36k | pdf_array *OFF = NULL; |
46 | 3.36k | pdf_array *ON = NULL; |
47 | 3.36k | int code; |
48 | | |
49 | 3.36k | if (ctx->OCProperties == NULL) |
50 | 270 | return is_visible; |
51 | | |
52 | 3.09k | code = pdfi_dict_knownget_type(ctx, ctx->OCProperties, "D", PDF_DICT, (pdf_obj **)&D); |
53 | 3.09k | if (code <= 0) |
54 | 0 | goto cleanup; |
55 | | |
56 | 3.09k | code = pdfi_dict_knownget_type(ctx, D, "BaseState", PDF_NAME, &BaseState); |
57 | 3.09k | if (code < 0) { |
58 | 0 | goto cleanup; |
59 | 0 | } |
60 | 3.09k | if (code > 0) { |
61 | 7 | if (pdfi_name_is((pdf_name *)BaseState, "OFF")) { |
62 | 0 | is_visible = false; |
63 | 0 | } |
64 | 7 | } |
65 | | |
66 | 3.09k | if (!is_visible) { |
67 | 0 | code = pdfi_dict_knownget_type(ctx, D, "ON", PDF_ARRAY, (pdf_obj **)&ON); |
68 | 0 | if (code < 0) |
69 | 0 | goto cleanup; |
70 | 0 | if (code > 0) { |
71 | 0 | if (pdfi_array_known(ctx, ON, (pdf_obj *)ocdict, NULL)) |
72 | 0 | is_visible = true; |
73 | 0 | } |
74 | 0 | } |
75 | | |
76 | 3.09k | if (is_visible) { |
77 | 3.09k | code = pdfi_dict_knownget_type(ctx, D, "OFF", PDF_ARRAY, (pdf_obj **)&OFF); |
78 | 3.09k | if (code < 0) |
79 | 164 | goto cleanup; |
80 | 2.92k | if (code > 0) { |
81 | 2.85k | if (pdfi_array_known(ctx, OFF, (pdf_obj *)ocdict, NULL)) |
82 | 2.71k | is_visible = false; |
83 | 2.85k | } |
84 | 2.92k | } |
85 | | |
86 | | |
87 | 3.09k | cleanup: |
88 | 3.09k | pdfi_countdown(BaseState); |
89 | 3.09k | pdfi_countdown(D); |
90 | 3.09k | pdfi_countdown(OFF); |
91 | 3.09k | pdfi_countdown(ON); |
92 | 3.09k | return is_visible; |
93 | 3.09k | } |
94 | | |
95 | | /* Check Usage for an OCG */ |
96 | | static bool |
97 | | pdfi_oc_check_OCG_usage(pdf_context *ctx, pdf_dict *ocdict) |
98 | 290 | { |
99 | 290 | bool is_visible = true; |
100 | 290 | int code; |
101 | 290 | pdf_dict *Usage = NULL; |
102 | 290 | pdf_dict *dict = NULL; |
103 | 290 | pdf_obj *name = NULL; |
104 | | |
105 | | /* Check Usage to see if it has additional info */ |
106 | 290 | code = pdfi_dict_knownget_type(ctx, ocdict, "Usage", PDF_DICT, (pdf_obj **)&Usage); |
107 | 290 | if (code <= 0) { |
108 | | /* No Usage, so we're done */ |
109 | 217 | goto cleanup; |
110 | 217 | } |
111 | | |
112 | 73 | if (ctx->args.printed) { |
113 | 73 | code = pdfi_dict_knownget_type(ctx, Usage, "Print", PDF_DICT, (pdf_obj **)&dict); |
114 | 73 | if (code <= 0) |
115 | 71 | goto cleanup; |
116 | 2 | code = pdfi_dict_knownget_type(ctx, dict, "PrintState", PDF_NAME, &name); |
117 | 2 | if (code <= 0) |
118 | 0 | goto cleanup; |
119 | 2 | } else { |
120 | 0 | code = pdfi_dict_knownget_type(ctx, Usage, "View", PDF_DICT, (pdf_obj **)&dict); |
121 | 0 | if (code <= 0) |
122 | 0 | goto cleanup; |
123 | 0 | code = pdfi_dict_knownget_type(ctx, dict, "ViewState", PDF_NAME, &name); |
124 | 0 | if (code <= 0) |
125 | 0 | goto cleanup; |
126 | 0 | } |
127 | 2 | if (pdfi_name_strcmp((pdf_name *)name, "OFF") == 0) { |
128 | 0 | is_visible = false; |
129 | 0 | } |
130 | | |
131 | 290 | cleanup: |
132 | 290 | pdfi_countdown(Usage); |
133 | 290 | pdfi_countdown(dict); |
134 | 290 | pdfi_countdown(name); |
135 | | |
136 | 290 | return is_visible; |
137 | 2 | } |
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 | 746 | { |
148 | 746 | int code; |
149 | 746 | pdf_dict *D_dict = NULL; |
150 | 746 | pdf_name *B = NULL; |
151 | | |
152 | 746 | code = pdfi_dict_knownget_type(ctx, ctx->OCProperties, "D", PDF_DICT, (pdf_obj **)&D_dict); |
153 | 746 | if (code < 0 || D_dict == NULL) |
154 | 0 | return true; |
155 | | |
156 | 746 | code = pdfi_dict_knownget_type(ctx, D_dict, "BaseState", PDF_NAME, (pdf_obj **)&B); |
157 | 746 | (void)pdfi_countdown(D_dict); |
158 | 746 | D_dict = NULL; |
159 | 746 | if (code < 0 || B == NULL) |
160 | 746 | return true; |
161 | | |
162 | 0 | if (pdfi_name_is(B, "OFF")) { |
163 | 0 | (void)pdfi_countdown(B); |
164 | 0 | return false; |
165 | 0 | } |
166 | | |
167 | 0 | (void)pdfi_countdown(B); |
168 | 0 | return true; |
169 | 0 | } |
170 | | |
171 | | static bool |
172 | | pdfi_oc_check_OCMD_array(pdf_context *ctx, pdf_array *array, ocmd_p_type type) |
173 | 3.78k | { |
174 | 3.78k | bool is_visible, hit = false; |
175 | 3.78k | uint64_t i; |
176 | 3.78k | int code; |
177 | 3.78k | pdf_obj *val = NULL; |
178 | | |
179 | | /* Setup default */ |
180 | 3.78k | switch (type) { |
181 | 3.78k | case P_AnyOn: |
182 | 3.78k | case P_AnyOff: |
183 | 3.78k | is_visible = false; |
184 | 3.78k | break; |
185 | 0 | case P_AllOn: |
186 | 0 | case P_AllOff: |
187 | 0 | is_visible = true; |
188 | 0 | break; |
189 | 3.78k | } |
190 | | |
191 | 7.22k | for (i=0; i<pdfi_array_size(array); i++) { |
192 | 3.78k | bool vis; |
193 | | |
194 | 3.78k | code = pdfi_array_get(ctx, array, i, &val); |
195 | 3.78k | if (code < 0) continue; |
196 | 3.04k | if (pdfi_type_of(val) != PDF_DICT) { |
197 | 0 | dbgprintf1("WARNING: OCMD array contains item type %d, expected PDF_DICT or PDF_NULL\n", pdfi_type_of(val)); |
198 | 0 | pdfi_countdown(val); |
199 | 0 | val = NULL; |
200 | 0 | continue; |
201 | 0 | } |
202 | | |
203 | 3.04k | hit = true; |
204 | 3.04k | vis = pdfi_get_default_OCG_val(ctx, (pdf_dict *)val); |
205 | 3.04k | switch (type) { |
206 | 3.04k | case P_AnyOn: |
207 | | /* visible if any is on */ |
208 | 3.04k | if (vis) { |
209 | 339 | is_visible = true; |
210 | 339 | goto cleanup; |
211 | 339 | } |
212 | 2.70k | break; |
213 | 2.70k | case P_AllOn: |
214 | | /* visible if all on */ |
215 | 0 | if (!vis) { |
216 | 0 | is_visible = false; |
217 | 0 | goto cleanup; |
218 | 0 | } |
219 | 0 | break; |
220 | 0 | case P_AllOff: |
221 | | /* visible if all are off */ |
222 | 0 | if (vis) { |
223 | 0 | is_visible = false; |
224 | 0 | goto cleanup; |
225 | 0 | } |
226 | 0 | break; |
227 | 0 | case P_AnyOff: |
228 | | /* visible if any is off */ |
229 | 0 | if (!vis) { |
230 | 0 | is_visible = true; |
231 | 0 | goto cleanup; |
232 | 0 | } |
233 | 0 | break; |
234 | 3.04k | } |
235 | 2.70k | pdfi_countdown(val); |
236 | 2.70k | val = NULL; |
237 | 2.70k | } |
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 | 3.44k | if (!hit) |
247 | 737 | is_visible = pdfi_oc_default_visibility(ctx); |
248 | | |
249 | 3.78k | cleanup: |
250 | 3.78k | pdfi_countdown(val); |
251 | 3.78k | return is_visible; |
252 | 3.44k | } |
253 | | |
254 | | static bool |
255 | | pdfi_oc_check_OCMD(pdf_context *ctx, pdf_dict *ocdict) |
256 | 3.82k | { |
257 | 3.82k | bool is_visible = true; |
258 | 3.82k | int code; |
259 | 3.82k | pdf_obj *VE = NULL; |
260 | 3.82k | pdf_obj *obj = NULL; |
261 | 3.82k | pdf_obj *Pname = NULL; |
262 | 3.82k | pdf_dict *OCGs_dict = NULL; /* alias, don't need to free */ |
263 | 3.82k | pdf_array *OCGs_array = NULL; /* alias, don't need to free */ |
264 | 3.82k | pdf_dict *UsageDict = NULL, *StateDict = NULL; |
265 | 3.82k | pdf_obj *State = NULL; |
266 | 3.82k | ocmd_p_type Ptype = P_AnyOn; |
267 | | |
268 | | /* TODO: We don't support this, so log a warning and ignore */ |
269 | 3.82k | code = pdfi_dict_knownget_type(ctx, ocdict, "VE", PDF_ARRAY, &VE); |
270 | 3.82k | if (code > 0) { |
271 | 17 | dbgprintf("WARNING: OCMD contains VE, which is not supported (ignoring)\n"); |
272 | 17 | } |
273 | | |
274 | 3.82k | code = pdfi_dict_knownget(ctx, ocdict, "OCGs", &obj); |
275 | 3.82k | if (code <= 0) |
276 | 20 | goto cleanup; |
277 | 3.80k | if (pdfi_type_of(obj) == PDF_ARRAY) { |
278 | 3.78k | OCGs_array = (pdf_array *)obj; |
279 | 3.78k | } else if (pdfi_type_of(obj) == PDF_DICT) { |
280 | 19 | OCGs_dict = (pdf_dict *)obj; |
281 | 19 | } else { |
282 | 9 | is_visible = pdfi_oc_default_visibility(ctx); |
283 | 9 | goto cleanup; |
284 | 9 | } |
285 | | |
286 | 3.80k | code = pdfi_dict_knownget_type(ctx, ocdict, "P", PDF_NAME, &Pname); |
287 | 3.80k | if (code < 0) |
288 | 0 | goto cleanup; |
289 | 3.80k | if (code == 0 || pdfi_name_is((pdf_name *)Pname, "AnyOn")) { |
290 | 3.80k | Ptype = P_AnyOn; |
291 | 3.80k | } else if (pdfi_name_is((pdf_name *)Pname, "AllOn")) { |
292 | 0 | Ptype = P_AllOn; |
293 | 0 | } else if (pdfi_name_is((pdf_name *)Pname, "AnyOff")) { |
294 | 0 | Ptype = P_AnyOff; |
295 | 0 | } else if (pdfi_name_is((pdf_name *)Pname, "AllOff")) { |
296 | 0 | Ptype = P_AllOff; |
297 | 0 | } else { |
298 | 0 | Ptype = P_AnyOn; |
299 | 0 | } |
300 | | |
301 | 3.80k | if (OCGs_dict) { |
302 | 19 | switch (Ptype) { |
303 | 19 | case P_AnyOn: |
304 | 19 | case P_AllOn: |
305 | 19 | is_visible = pdfi_get_default_OCG_val(ctx, OCGs_dict); |
306 | 19 | break; |
307 | 0 | case P_AllOff: |
308 | 0 | case P_AnyOff: |
309 | 0 | is_visible = !pdfi_get_default_OCG_val(ctx, OCGs_dict); |
310 | 0 | break; |
311 | 19 | } |
312 | 3.78k | } else { |
313 | 3.78k | is_visible = pdfi_oc_check_OCMD_array(ctx, OCGs_array, Ptype); |
314 | 3.78k | } |
315 | | |
316 | 3.80k | if (OCGs_dict) { |
317 | 19 | code = pdfi_dict_knownget_type(ctx, OCGs_dict, "Usage", PDF_DICT, (pdf_obj **)&UsageDict); |
318 | 19 | if (code < 0) |
319 | 0 | goto cleanup; |
320 | | |
321 | 19 | if (UsageDict != NULL) { |
322 | 9 | if (ctx->args.printed) { |
323 | 9 | code = pdfi_dict_knownget_type(ctx, UsageDict, "Print", PDF_DICT, (pdf_obj **)&StateDict); |
324 | 9 | if (code < 0) |
325 | 0 | goto cleanup; |
326 | 9 | if (StateDict) { |
327 | 2 | code = pdfi_dict_knownget_type(ctx, StateDict, "PrintState", PDF_NAME, &State); |
328 | 2 | if (code < 0) |
329 | 0 | goto cleanup; |
330 | 2 | } |
331 | 9 | } else { |
332 | 0 | code = pdfi_dict_knownget_type(ctx, UsageDict, "View", PDF_DICT, (pdf_obj **)&StateDict); |
333 | 0 | if (code < 0) |
334 | 0 | goto cleanup; |
335 | 0 | if (StateDict) { |
336 | 0 | code = pdfi_dict_knownget_type(ctx, StateDict, "ViewState", PDF_NAME, &State); |
337 | 0 | if (code < 0) |
338 | 0 | goto cleanup; |
339 | 0 | } |
340 | 0 | } |
341 | 9 | if (State) { |
342 | 2 | if (pdfi_name_is((const pdf_name *)State, "ON")) |
343 | 2 | is_visible = true; |
344 | 0 | else |
345 | 0 | if (pdfi_name_is((const pdf_name *)State, "OFF")) |
346 | 0 | is_visible = false; |
347 | 0 | else |
348 | 0 | pdfi_set_error(ctx, 0, NULL, E_PDF_BAD_VALUE, "pdfi_oc_check_OCMD", "Usage Dictionary State is neither ON nor OFF"); |
349 | 2 | } |
350 | 9 | } |
351 | 19 | } |
352 | | |
353 | 3.82k | cleanup: |
354 | 3.82k | pdfi_countdown(State); |
355 | 3.82k | pdfi_countdown(StateDict); |
356 | 3.82k | pdfi_countdown(UsageDict); |
357 | 3.82k | pdfi_countdown(VE); |
358 | 3.82k | pdfi_countdown(obj); |
359 | 3.82k | pdfi_countdown(Pname); |
360 | | |
361 | 3.82k | return is_visible; |
362 | 3.80k | } |
363 | | |
364 | | /* Check if an OCG or OCMD is visible, passing in OC dict */ |
365 | | bool |
366 | | pdfi_oc_is_ocg_visible(pdf_context *ctx, pdf_dict *ocdict) |
367 | 4.13k | { |
368 | 4.13k | pdf_name *type = NULL; |
369 | 4.13k | bool is_visible = true; |
370 | 4.13k | int code; |
371 | | |
372 | | /* Type can be either OCMD or OCG. |
373 | | */ |
374 | 4.13k | code = pdfi_dict_knownget_type(ctx, ocdict, "Type", PDF_NAME, (pdf_obj **)&type); |
375 | 4.13k | if (code <= 0) |
376 | 5 | goto cleanup; |
377 | | |
378 | 4.13k | if (pdfi_name_is(type, "OCMD")) { |
379 | 3.82k | is_visible = pdfi_oc_check_OCMD(ctx, ocdict); |
380 | 3.82k | } else if (pdfi_name_is(type, "OCG")) { |
381 | 298 | is_visible = pdfi_get_default_OCG_val(ctx, ocdict); |
382 | 298 | if (is_visible) |
383 | 290 | is_visible = pdfi_oc_check_OCG_usage(ctx, ocdict); |
384 | 298 | } else { |
385 | 5 | char str[100]; |
386 | 5 | memcpy(str, (const char *)type->data, type->length < 100 ? type->length : 99); |
387 | 5 | str[type->length < 100 ? type->length : 99] = '\0'; |
388 | 5 | dbgprintf1("WARNING: OC dict type is %s, expected OCG or OCMD\n", str); |
389 | 5 | } |
390 | | |
391 | 4.13k | cleanup: |
392 | 4.13k | pdfi_countdown(type); |
393 | | |
394 | 4.13k | if (ctx->args.pdfdebug) { |
395 | 0 | outprintf(ctx->memory, "OCG: OC Dict %d %s visible\n", ocdict->object_num, |
396 | 0 | is_visible ? "IS" : "IS NOT"); |
397 | 0 | } |
398 | 4.13k | return is_visible; |
399 | 4.13k | } |
400 | | |
401 | 12.1k | #define NUM_CONTENT_LEVELS 100 |
402 | | typedef struct { |
403 | | byte *flags; |
404 | | uint64_t num_off; |
405 | | uint64_t max_flags; |
406 | | } pdfi_oc_levels_t; |
407 | | |
408 | | static int pdfi_oc_levels_init(pdf_context *ctx, pdfi_oc_levels_t **levels) |
409 | 6.07k | { |
410 | 6.07k | byte *data; |
411 | 6.07k | pdfi_oc_levels_t *new; |
412 | | |
413 | 6.07k | *levels = NULL; |
414 | | |
415 | 6.07k | new = (pdfi_oc_levels_t *)gs_alloc_bytes(ctx->memory, sizeof(pdfi_oc_levels_t), |
416 | 6.07k | "pdfi_oc_levels_init (levels)"); |
417 | 6.07k | if (!new) |
418 | 0 | return_error(gs_error_VMerror); |
419 | | |
420 | 6.07k | data = (byte *)gs_alloc_bytes(ctx->memory, NUM_CONTENT_LEVELS, "pdfi_oc_levels_init (data)"); |
421 | 6.07k | if (!data) { |
422 | 0 | gs_free_object(ctx->memory, new, "pdfi_oc_levels_init (levels (error))"); |
423 | 0 | return_error(gs_error_VMerror); |
424 | 0 | } |
425 | 6.07k | memset(data, 0, NUM_CONTENT_LEVELS); |
426 | | |
427 | 6.07k | new->flags = data; |
428 | 6.07k | new->num_off = 0; |
429 | 6.07k | new->max_flags = NUM_CONTENT_LEVELS; |
430 | 6.07k | *levels = new; |
431 | | |
432 | 6.07k | return 0; |
433 | 6.07k | } |
434 | | |
435 | | static int pdfi_oc_levels_free(pdf_context *ctx, pdfi_oc_levels_t *levels) |
436 | 7.31k | { |
437 | 7.31k | if (!levels) |
438 | 1.24k | return 0; |
439 | 6.07k | gs_free_object(ctx->memory, levels->flags, "pdfi_oc_levels_free (flags)"); |
440 | 6.07k | gs_free_object(ctx->memory, levels, "pdfi_oc_levels_free (levels)"); |
441 | | |
442 | 6.07k | return 0; |
443 | 7.31k | } |
444 | | |
445 | | static int pdfi_oc_levels_set(pdf_context *ctx, pdfi_oc_levels_t *levels, uint64_t index) |
446 | 2.71k | { |
447 | 2.71k | byte *new = NULL; |
448 | 2.71k | uint64_t newmax; |
449 | | |
450 | 2.71k | if (index > levels->max_flags - 1) { |
451 | | /* Expand the flags buffer */ |
452 | 0 | newmax = levels->max_flags + NUM_CONTENT_LEVELS; |
453 | 0 | if (index > newmax) |
454 | 0 | return_error(gs_error_Fatal); /* shouldn't happen */ |
455 | 0 | new = gs_alloc_bytes(ctx->memory, newmax, "pdfi_oc_levels_set (new data)"); |
456 | 0 | if (!new) |
457 | 0 | return_error(gs_error_VMerror); |
458 | 0 | memset(new, 0, newmax); |
459 | 0 | memcpy(new, levels->flags, levels->max_flags); |
460 | 0 | gs_free_object(ctx->memory, levels->flags, "pdfi_oc_levels_set (old data)"); |
461 | 0 | levels->flags = new; |
462 | 0 | levels->max_flags += NUM_CONTENT_LEVELS; |
463 | 0 | } |
464 | | |
465 | 2.71k | if (levels->flags[index] == 0) |
466 | 2.71k | levels->num_off ++; |
467 | 2.71k | levels->flags[index] = 1; |
468 | 2.71k | return 0; |
469 | 2.71k | } |
470 | | |
471 | | static int pdfi_oc_levels_clear(pdf_context *ctx, pdfi_oc_levels_t *levels, uint64_t index) |
472 | 36.5k | { |
473 | 36.5k | if (index > levels->max_flags - 1) |
474 | 0 | return -1; |
475 | 36.5k | if (levels->flags[index] != 0) |
476 | 2.68k | levels->num_off --; |
477 | 36.5k | levels->flags[index] = 0; |
478 | 36.5k | return 0; |
479 | 36.5k | } |
480 | | |
481 | | |
482 | | /* Test if content is turned off for this element. |
483 | | */ |
484 | | bool pdfi_oc_is_off(pdf_context *ctx) |
485 | 700k | { |
486 | 700k | pdfi_oc_levels_t *levels = (pdfi_oc_levels_t *)ctx->OFFlevels; |
487 | 700k | uint64_t num_off = levels->num_off; |
488 | | |
489 | 700k | return (num_off != 0); |
490 | 700k | } |
491 | | |
492 | | int pdfi_oc_init(pdf_context *ctx) |
493 | 6.07k | { |
494 | 6.07k | int code; |
495 | | |
496 | 6.07k | ctx->BMClevel = 0; |
497 | 6.07k | if (ctx->OFFlevels) { |
498 | 1.77k | pdfi_oc_levels_free(ctx, ctx->OFFlevels); |
499 | 1.77k | ctx->OFFlevels = NULL; |
500 | 1.77k | } |
501 | 6.07k | code = pdfi_oc_levels_init(ctx, (pdfi_oc_levels_t **)&ctx->OFFlevels); |
502 | 6.07k | if (code < 0) |
503 | 0 | return code; |
504 | | |
505 | 6.07k | return 0; |
506 | 6.07k | } |
507 | | |
508 | | int pdfi_oc_free(pdf_context *ctx) |
509 | 5.54k | { |
510 | 5.54k | int code; |
511 | | |
512 | 5.54k | code = pdfi_oc_levels_free(ctx, (pdfi_oc_levels_t *)ctx->OFFlevels); |
513 | 5.54k | ctx->OFFlevels = NULL; |
514 | 5.54k | return code; |
515 | 5.54k | } |
516 | | |
517 | | int pdfi_op_MP(pdf_context *ctx) |
518 | 216 | { |
519 | 216 | pdf_obj *o = NULL; |
520 | 216 | int code = 0; |
521 | | |
522 | 216 | if (pdfi_count_stack(ctx) < 1) |
523 | 6 | return_error(gs_error_stackunderflow); |
524 | | |
525 | 210 | if (!ctx->device_state.writepdfmarks || !ctx->args.preservemarkedcontent) { |
526 | 210 | pdfi_pop(ctx, 1); |
527 | 210 | goto exit; |
528 | 210 | } |
529 | | |
530 | 0 | o = ctx->stack_top[-1]; |
531 | 0 | pdfi_countup(o); |
532 | 0 | pdfi_pop(ctx, 1); |
533 | |
|
534 | 0 | if (pdfi_type_of(o) != PDF_NAME) { |
535 | 0 | code = gs_note_error(gs_error_typecheck); |
536 | 0 | goto exit; |
537 | 0 | } |
538 | | |
539 | 0 | code = pdfi_pdfmark_from_objarray(ctx, &o, 1, NULL, "MP"); |
540 | 0 | ctx->BMClevel ++; |
541 | |
|
542 | 210 | exit: |
543 | 210 | pdfi_countdown(o); |
544 | 210 | return code; |
545 | 0 | } |
546 | | |
547 | | int pdfi_op_DP(pdf_context *ctx, pdf_dict *stream_dict, pdf_dict *page_dict) |
548 | 6 | { |
549 | 6 | pdf_name *properties = NULL; |
550 | 6 | int code = 0; |
551 | 6 | pdf_obj **objarray = NULL, *o = NULL; |
552 | | |
553 | 6 | if (pdfi_count_stack(ctx) < 2) { |
554 | 0 | pdfi_clearstack(ctx); |
555 | 0 | return gs_note_error(gs_error_stackunderflow); |
556 | 0 | } |
557 | | |
558 | 6 | if (!ctx->device_state.writepdfmarks || !ctx->args.preservemarkedcontent) { |
559 | 6 | pdfi_pop(ctx, 2); /* pop args */ |
560 | 6 | goto exit; |
561 | 6 | } |
562 | | |
563 | 0 | if (pdfi_type_of(ctx->stack_top[-2]) != PDF_NAME) { |
564 | 0 | pdfi_pop(ctx, 2); /* pop args */ |
565 | 0 | code = gs_note_error(gs_error_typecheck); |
566 | 0 | goto exit; |
567 | 0 | } |
568 | | |
569 | 0 | objarray = (pdf_obj **)gs_alloc_bytes(ctx->memory, 2 * sizeof(pdf_obj *), "pdfi_op_DP"); |
570 | 0 | if (objarray == NULL) { |
571 | 0 | code = gs_note_error(gs_error_VMerror); |
572 | 0 | goto exit; |
573 | 0 | } |
574 | | |
575 | 0 | objarray[0] = ctx->stack_top[-2]; |
576 | 0 | pdfi_countup(objarray[0]); |
577 | 0 | o = ctx->stack_top[-1]; |
578 | 0 | pdfi_countup(o); |
579 | 0 | pdfi_pop(ctx, 2); /* pop args */ |
580 | |
|
581 | 0 | switch (pdfi_type_of(o)) { |
582 | 0 | case PDF_NAME: |
583 | 0 | code = pdfi_find_resource(ctx, (unsigned char *)"Properties", (pdf_name *)o, stream_dict, page_dict, (pdf_obj **)&properties); |
584 | 0 | if(code < 0) |
585 | 0 | goto exit; |
586 | 0 | if (pdfi_type_of(properties) != PDF_DICT) { |
587 | 0 | code = gs_note_error(gs_error_typecheck); |
588 | 0 | goto exit; |
589 | 0 | } |
590 | 0 | objarray[1] = (pdf_obj *)properties; |
591 | 0 | break; |
592 | 0 | case PDF_DICT: |
593 | 0 | objarray[1] = o; |
594 | 0 | break; |
595 | 0 | default: |
596 | 0 | code = gs_note_error(gs_error_VMerror); |
597 | 0 | goto exit; |
598 | 0 | } |
599 | | |
600 | 0 | code = pdfi_pdfmark_from_objarray(ctx, objarray, 2, NULL, "DP"); |
601 | |
|
602 | 6 | exit: |
603 | 6 | if (objarray != NULL) { |
604 | 0 | pdfi_countdown(objarray[0]); |
605 | 0 | gs_free_object(ctx->memory, objarray, "free pdfi_op_DP"); |
606 | 0 | } |
607 | 6 | pdfi_countdown(o); |
608 | 6 | pdfi_countdown(properties); |
609 | 6 | return code; |
610 | 0 | } |
611 | | |
612 | | /* begin marked content sequence */ |
613 | | int pdfi_op_BMC(pdf_context *ctx) |
614 | 1.73k | { |
615 | 1.73k | pdf_obj *o = NULL; |
616 | 1.73k | int code = 0; |
617 | | |
618 | | /* This will also prevent us writing out an EMC if the BMC is in any way invalid */ |
619 | 1.73k | ctx->BDCWasOC = true; |
620 | | |
621 | 1.73k | if (pdfi_count_stack(ctx) < 1) |
622 | 1 | return_error(gs_error_stackunderflow); |
623 | | |
624 | 1.73k | if (!ctx->device_state.writepdfmarks || !ctx->args.preservemarkedcontent) { |
625 | | /* Need to increment the BMCLevel anyway, as the EMC will count it down. |
626 | | * If we already have a BDC, then that effectively will turn it on. |
627 | | */ |
628 | 1.73k | ctx->BMClevel ++; |
629 | 1.73k | pdfi_pop(ctx, 1); |
630 | 1.73k | goto exit; |
631 | 1.73k | } |
632 | | |
633 | 0 | o = ctx->stack_top[-1]; |
634 | 0 | pdfi_countup(o); |
635 | 0 | pdfi_pop(ctx, 1); |
636 | |
|
637 | 0 | if (pdfi_type_of(o) != PDF_NAME) { |
638 | 0 | code = gs_note_error(gs_error_typecheck); |
639 | 0 | goto exit; |
640 | 0 | } |
641 | | |
642 | 0 | ctx->BDCWasOC = false; |
643 | 0 | code = pdfi_pdfmark_from_objarray(ctx, &o, 1, NULL, "BMC"); |
644 | 0 | ctx->BMClevel ++; |
645 | |
|
646 | 1.73k | exit: |
647 | 1.73k | pdfi_countdown(o); |
648 | 1.73k | return code; |
649 | 0 | } |
650 | | |
651 | | /* begin marked content sequence with property list */ |
652 | | int pdfi_op_BDC(pdf_context *ctx, pdf_dict *stream_dict, pdf_dict *page_dict) |
653 | 35.4k | { |
654 | 35.4k | pdf_name *tag = NULL; |
655 | 35.4k | pdf_name *properties = NULL; |
656 | 35.4k | pdf_dict *oc_dict = NULL; |
657 | 35.4k | int code = 0; |
658 | 35.4k | bool ocg_is_visible; |
659 | 35.4k | pdf_obj **objarray = NULL, *o = NULL; |
660 | 35.4k | pdf_indirect_ref *dictref = NULL; |
661 | | |
662 | | /* This will also prevent us writing out an EMC if the BDC is in any way invalid */ |
663 | 35.4k | ctx->BDCWasOC = true; |
664 | | |
665 | 35.4k | if (pdfi_count_stack(ctx) < 2) { |
666 | 423 | pdfi_clearstack(ctx); |
667 | 423 | return gs_note_error(gs_error_stackunderflow); |
668 | 423 | } |
669 | | |
670 | 35.0k | ctx->BMClevel ++; |
671 | | |
672 | 35.0k | tag = (pdf_name *)ctx->stack_top[-2]; |
673 | 35.0k | pdfi_countup(tag); |
674 | 35.0k | o = ctx->stack_top[-1]; |
675 | 35.0k | pdfi_countup(o); |
676 | 35.0k | pdfi_pop(ctx, 2); |
677 | | |
678 | 35.0k | if (pdfi_type_of(tag) != PDF_NAME) |
679 | 119 | goto exit; |
680 | | |
681 | 34.8k | if (!pdfi_name_is(tag, "OC")) |
682 | 24.4k | ctx->BDCWasOC = false; |
683 | | |
684 | 34.8k | if (ctx->device_state.writepdfmarks && ctx->args.preservemarkedcontent && (!ctx->BDCWasOC || ctx->device_state.WantsOptionalContent)) { |
685 | 0 | objarray = (pdf_obj **)gs_alloc_bytes(ctx->memory, 2 * sizeof(pdf_obj *), "pdfi_op_BDC"); |
686 | 0 | if (objarray == NULL) { |
687 | 0 | code = gs_note_error(gs_error_VMerror); |
688 | 0 | goto exit; |
689 | 0 | } |
690 | | |
691 | 0 | objarray[0] = (pdf_obj *)tag; |
692 | |
|
693 | 0 | switch (pdfi_type_of(o)) { |
694 | 0 | case PDF_NAME: |
695 | 0 | code = pdfi_find_resource(ctx, (unsigned char *)"Properties", (pdf_name *)o, stream_dict, page_dict, (pdf_obj **)&oc_dict); |
696 | 0 | if(code < 0) |
697 | 0 | goto exit; |
698 | 0 | if (pdfi_type_of(oc_dict) != PDF_DICT) { |
699 | 0 | code = gs_note_error(gs_error_typecheck); |
700 | 0 | goto exit; |
701 | 0 | } |
702 | | /* If we are producing PDF/A we must not include any Metadata, as that |
703 | | * requires us to modify the XMP Metadata, which we don't know how to do. |
704 | | */ |
705 | 0 | if (ctx->args.PDFA > 0) { |
706 | 0 | uint64_t index = 0; |
707 | 0 | pdf_name *Key = NULL; |
708 | 0 | pdf_obj *Value = NULL; |
709 | |
|
710 | 0 | code = pdfi_dict_first(ctx, oc_dict, (pdf_obj **)&Key, &Value, &index); |
711 | 0 | if (code < 0) { |
712 | 0 | if (code == gs_error_undefined) |
713 | 0 | code = 0; |
714 | 0 | goto exit; |
715 | 0 | } |
716 | 0 | while (code >= 0) { |
717 | 0 | if (pdfi_name_is(Key, "Metadata")) { |
718 | 0 | pdfi_dict_delete_pair(ctx, oc_dict, Key); |
719 | 0 | } |
720 | 0 | pdfi_countdown(Key); |
721 | 0 | Key = NULL; |
722 | 0 | pdfi_countdown(Value); |
723 | 0 | Value = NULL; |
724 | |
|
725 | 0 | code = pdfi_dict_next(ctx, oc_dict, (pdf_obj **)&Key, &Value, &index); |
726 | 0 | if (code == gs_error_undefined) { |
727 | 0 | code = 0; |
728 | 0 | break; |
729 | 0 | } |
730 | 0 | } |
731 | 0 | } |
732 | 0 | if (pdfi_dict_entries(oc_dict) == 0) |
733 | 0 | goto exit; |
734 | | |
735 | 0 | code = pdfi_pdfmark_dict(ctx, oc_dict); |
736 | 0 | if (code < 0) |
737 | 0 | goto exit; |
738 | | |
739 | | /* Create an indirect ref for the dict */ |
740 | 0 | code = pdfi_object_alloc(ctx, PDF_INDIRECT, 0, (pdf_obj **)&dictref); |
741 | 0 | if (code < 0) goto exit; |
742 | 0 | pdfi_countup(dictref); |
743 | 0 | dictref->ref_object_num = oc_dict->object_num; |
744 | 0 | dictref->ref_generation_num = oc_dict->generation_num; |
745 | 0 | dictref->is_marking = true; |
746 | |
|
747 | 0 | objarray[1] = (pdf_obj *)dictref; |
748 | 0 | break; |
749 | 0 | case PDF_DICT: |
750 | 0 | objarray[1] = o; |
751 | 0 | break; |
752 | 0 | default: |
753 | 0 | code = gs_note_error(gs_error_VMerror); |
754 | 0 | goto exit; |
755 | 0 | } |
756 | | |
757 | 0 | code = pdfi_pdfmark_from_objarray(ctx, objarray, 2, NULL, "BDC"); |
758 | 0 | goto exit; |
759 | 0 | } |
760 | | |
761 | 34.8k | if (pdfi_name_is(tag, "OC")) { |
762 | | /* Check if first arg is a name and handle it if so */ |
763 | | /* TODO: spec says it could also be an inline dict that we should be able to handle, |
764 | | * but I am just matching what gs does for now, and it doesn't handle that case. |
765 | | */ |
766 | 10.3k | properties = (pdf_name *)o; |
767 | 10.3k | if (pdfi_type_of(properties) != PDF_NAME) |
768 | 2 | goto exit; |
769 | | |
770 | 10.3k | code = pdfi_loop_detector_mark(ctx); |
771 | | /* If it's a name, look it up in Properties */ |
772 | 10.3k | code = pdfi_find_resource(ctx, (unsigned char *)"Properties", properties, |
773 | 10.3k | (pdf_dict *)stream_dict, page_dict, (pdf_obj **)&oc_dict); |
774 | 10.3k | (void)pdfi_loop_detector_cleartomark(ctx); |
775 | 10.3k | if (code != 0) |
776 | 6.25k | goto exit; |
777 | 4.13k | if (pdfi_type_of(oc_dict) != PDF_DICT) |
778 | 76 | goto exit; |
779 | | |
780 | 4.05k | ocg_is_visible = pdfi_oc_is_ocg_visible(ctx, oc_dict); |
781 | 4.05k | if (!ocg_is_visible) |
782 | 2.71k | code = pdfi_oc_levels_set(ctx, ctx->OFFlevels, ctx->BMClevel); |
783 | | |
784 | 4.05k | } |
785 | | |
786 | 35.0k | exit: |
787 | 35.0k | if (objarray != NULL) |
788 | 0 | gs_free_object(ctx->memory, objarray, "free pdfi_op_BDC"); |
789 | 35.0k | pdfi_countdown(dictref); |
790 | 35.0k | pdfi_countdown(o); |
791 | 35.0k | pdfi_countdown(tag); |
792 | 35.0k | pdfi_countdown(oc_dict); |
793 | 35.0k | return code; |
794 | 34.8k | } |
795 | | |
796 | | /* end marked content sequence */ |
797 | | int pdfi_op_EMC(pdf_context *ctx) |
798 | 36.5k | { |
799 | 36.5k | int code, code1 = 0; |
800 | | |
801 | 36.5k | if (ctx->device_state.writepdfmarks && ctx->args.preservemarkedcontent && (!ctx->BDCWasOC || ctx->device_state.WantsOptionalContent)) |
802 | 0 | code1 = pdfi_pdfmark_from_objarray(ctx, NULL, 0, NULL, "EMC"); |
803 | | |
804 | 36.5k | code = pdfi_oc_levels_clear(ctx, ctx->OFFlevels, ctx->BMClevel); |
805 | 36.5k | if (code == 0) |
806 | 36.5k | code = code1; |
807 | | |
808 | | /* TODO: Should we flag error on too many EMC? */ |
809 | 36.5k | if (ctx->BMClevel > 0) |
810 | 36.2k | ctx->BMClevel --; |
811 | 36.5k | return code; |
812 | 36.5k | } |