/src/ghostpdl/pdf/pdf_check.c
Line | Count | Source |
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 | | /* Checks for transparency and spots for the PDF interpreter */ |
17 | | |
18 | | #include "pdf_int.h" |
19 | | #include "pdf_stack.h" |
20 | | #include "pdf_deref.h" |
21 | | #include "pdf_page.h" |
22 | | #include "pdf_file.h" |
23 | | #include "pdf_dict.h" |
24 | | #include "pdf_array.h" |
25 | | #include "pdf_loop_detect.h" |
26 | | #include "pdf_colour.h" |
27 | | #include "pdf_trans.h" |
28 | | #include "pdf_font_types.h" |
29 | | #include "pdf_gstate.h" |
30 | | #include "pdf_misc.h" |
31 | | #include "pdf_check.h" |
32 | | #include "pdf_device.h" |
33 | | #include "gsdevice.h" /* For gs_setdevice_no_erase */ |
34 | | #include "gspaint.h" /* For gs_erasepage() */ |
35 | | |
36 | | /* For performance and resource reasons we do not want to install the transparency blending |
37 | | * compositor unless we need it. Similarly, if a device handles spot colours it can minimise |
38 | | * memory usage if it knows ahead of time how many spot colours there will be. |
39 | | * |
40 | | * The PDF interpreter written in PostScript performed these as two separate tasks, on opening |
41 | | * a PDF file it would count spot colour usage and then for each page it would chek if the page |
42 | | * used any transparency. The code below is used to check for both transparency and spot colours. |
43 | | * If the int pointer to num_spots is NULL then we aren't interested in spot colours (not supported |
44 | | * by the device), if the int pointer to transparent is NULL then we aren't interested in transparency |
45 | | * (-dNOTRANSPARENCY is set). |
46 | | * |
47 | | * Currently the code is run when we open the PDF file, the existence of transparency on any given |
48 | | * page is recorded in a bit array for later use. If it turns out that checking every page when we |
49 | | * open the file is a performance burden then we could do it on a per-page basis instead. NB if |
50 | | * the device supports spot colours then we do need to scan the file before starting any page. |
51 | | * |
52 | | * The technique is fairly straight-forward, we start with each page, and open its Resources |
53 | | * dictionary, we then check by type each possible resource. Some resources (eg Pattern, XObject) |
54 | | * can themselves contain Resources, in which case we recursively check that dictionary. Note; not |
55 | | * all Resource types need to be checked for both transparency and spot colours, some types can |
56 | | * only contain one or the other. |
57 | | * |
58 | | * Routines with the name pdfi_check_xxx_dict are intended to check a Resource dictionary entry, this |
59 | | * will be a dictioanry of names and values, where the values are objects of the given Resource type. |
60 | | * |
61 | | */ |
62 | | |
63 | | /* Structure for keeping track of resources as they are being checked */ |
64 | | /* CheckedResources is a bit of a hack, for the file Bug697655.pdf. That file has many (unused) |
65 | | * XObjects which use Resources and the Resources use other XObjects and so on nested |
66 | | * very deeply. This takes *hours* to check. Ghostscript gets around this because it |
67 | | * stores all the objects (which we don't want to do because its wasteful) and checking |
68 | | * to see if its already tested a given resource for spots/transparency. |
69 | | * This is a temporary allocation, big enough to hold all the objects in the file (1 per bit) |
70 | | * each time we have fully checked a resource we add it here, when checking a resource we |
71 | | * first check this list to see if its already been checked, in which case we can skip |
72 | | * it. When done we release the memory. |
73 | | */ |
74 | | typedef struct { |
75 | | bool transparent; |
76 | | bool BM_Not_Normal; |
77 | | bool has_overprint; /* Does it have OP or op in an ExtGState? */ |
78 | | pdf_dict *spot_dict; |
79 | | pdf_array *font_array; |
80 | | uint32_t size; |
81 | | byte *CheckedResources; |
82 | | } pdfi_check_tracker_t; |
83 | | |
84 | | static int pdfi_check_Resources(pdf_context *ctx, pdf_dict *Resources_dict, pdf_dict *page_dict, pdfi_check_tracker_t *tracker); |
85 | | |
86 | | static inline bool resource_is_checked(pdfi_check_tracker_t *tracker, pdf_obj *o) |
87 | 7.25M | { |
88 | 7.25M | uint32_t byte_offset; |
89 | 7.25M | byte bit_offset; |
90 | 7.25M | int object_num; |
91 | | |
92 | 7.25M | if(tracker->CheckedResources == NULL) |
93 | 180k | return 0; |
94 | | |
95 | | /* objects with object number 0 are directly defined, we can't |
96 | | * store those so just return immediately |
97 | | */ |
98 | 7.07M | object_num = pdf_object_num(o); |
99 | 7.07M | if (object_num > 0 && (object_num >> 3) < tracker->size) { |
100 | | /* CheckedResources is a byte array, each byte represents |
101 | | * 8 objects. So the object number / 8 is the byte offset |
102 | | * into the array, and then object number % 8 is the bit |
103 | | * within that byte that we want. |
104 | | */ |
105 | 3.13M | bit_offset = 0x01 << (object_num % 8); |
106 | 3.13M | byte_offset = object_num >> 3; |
107 | | |
108 | | /* If its already set, then return that. */ |
109 | 3.13M | if (tracker->CheckedResources[byte_offset] & bit_offset) |
110 | 367k | return true; |
111 | 2.76M | else |
112 | | /* Otherwise set it for futre reference */ |
113 | 2.76M | tracker->CheckedResources[byte_offset] |= bit_offset; |
114 | 3.13M | } |
115 | 6.70M | return false; |
116 | 7.07M | } |
117 | | |
118 | | |
119 | | static int |
120 | | pdfi_check_free_tracker(pdf_context *ctx, pdfi_check_tracker_t *tracker) |
121 | 268k | { |
122 | 268k | gs_free_object(ctx->memory, tracker->CheckedResources, "pdfi_check_free_tracker(flags)"); |
123 | 268k | pdfi_countdown(tracker->spot_dict); |
124 | 268k | pdfi_countdown(tracker->font_array); |
125 | 268k | memset(tracker, 0, sizeof(*tracker)); |
126 | 268k | return 0; |
127 | 268k | } |
128 | | |
129 | | static int |
130 | | pdfi_check_init_tracker(pdf_context *ctx, pdfi_check_tracker_t *tracker, pdf_array **fonts_array, pdf_array **spot_array) |
131 | 268k | { |
132 | 268k | int code = 0; |
133 | | |
134 | 268k | memset(tracker, 0, sizeof(*tracker)); |
135 | | |
136 | 268k | tracker->size = (ctx->xref_table->xref_size + 7) / 8; |
137 | 268k | tracker->CheckedResources = gs_alloc_bytes(ctx->memory, tracker->size, |
138 | 268k | "pdfi_check_init_tracker(flags)"); |
139 | 268k | if (tracker->CheckedResources == NULL) |
140 | 0 | return_error(gs_error_VMerror); |
141 | | |
142 | 268k | memset(tracker->CheckedResources, 0x00, tracker->size); |
143 | | |
144 | 268k | if (ctx->device_state.spot_capable || |
145 | 239k | (ctx->pgs->device->icc_struct->overprint_control) == gs_overprint_control_simulate || |
146 | 239k | spot_array != NULL) |
147 | 29.2k | { |
148 | 29.2k | code = pdfi_dict_alloc(ctx, 32, &tracker->spot_dict); |
149 | 29.2k | if (code < 0) |
150 | 0 | goto cleanup; |
151 | 29.2k | pdfi_countup(tracker->spot_dict); |
152 | 29.2k | } |
153 | | |
154 | 268k | if (fonts_array != NULL) { |
155 | 0 | code = pdfi_array_alloc(ctx, 0, &tracker->font_array); |
156 | 0 | if (code < 0) |
157 | 0 | goto cleanup; |
158 | 0 | pdfi_countup(tracker->font_array); |
159 | 0 | } |
160 | | |
161 | 268k | return 0; |
162 | | |
163 | 0 | cleanup: |
164 | 0 | pdfi_check_free_tracker(ctx, tracker); |
165 | 0 | return code; |
166 | 268k | } |
167 | | |
168 | | /* |
169 | | * Check the Resources dictionary ColorSpace entry. pdfi_check_ColorSpace_for_spots is defined |
170 | | * in pdf_colour.c |
171 | | */ |
172 | | static int pdfi_check_ColorSpace_dict(pdf_context *ctx, pdf_dict *cspace_dict, |
173 | | pdf_dict *page_dict, pdfi_check_tracker_t *tracker) |
174 | 82.8k | { |
175 | 82.8k | int code; |
176 | 82.8k | uint64_t i, index; |
177 | 82.8k | pdf_obj *Key = NULL, *Value = NULL; |
178 | | |
179 | 82.8k | if (resource_is_checked(tracker, (pdf_obj *)cspace_dict)) |
180 | 16 | return 0; |
181 | | |
182 | 82.8k | if (pdfi_type_of(cspace_dict) != PDF_DICT) |
183 | 71.2k | return_error(gs_error_typecheck); |
184 | | |
185 | 11.6k | if (pdfi_dict_entries(cspace_dict) > 0) { |
186 | 11.6k | code = pdfi_loop_detector_mark(ctx); /* Mark the start of the ColorSpace dictionary loop */ |
187 | 11.6k | if (code < 0) |
188 | 0 | return code; |
189 | | |
190 | 11.6k | code = pdfi_dict_first(ctx, cspace_dict, &Key, &Value, &index); |
191 | 11.6k | if (code < 0) |
192 | 1.88k | goto error1; |
193 | | |
194 | 9.73k | i = 1; |
195 | 14.1k | do { |
196 | 14.1k | code = pdfi_check_ColorSpace_for_spots(ctx, Value, cspace_dict, page_dict, tracker->spot_dict); |
197 | 14.1k | if (code < 0) |
198 | 128 | goto error2; |
199 | | |
200 | 14.0k | pdfi_countdown(Key); |
201 | 14.0k | Key = NULL; |
202 | 14.0k | pdfi_countdown(Value); |
203 | 14.0k | Value = NULL; |
204 | | |
205 | 14.0k | (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the Shading dictionary loop */ |
206 | | |
207 | 14.0k | code = pdfi_loop_detector_mark(ctx); /* Mark the new start of the Shading dictionary loop */ |
208 | 14.0k | if (code < 0) |
209 | 0 | goto error1; |
210 | | |
211 | 14.2k | do { |
212 | 14.2k | if (i++ >= pdfi_dict_entries(cspace_dict)) { |
213 | 9.60k | code = 0; |
214 | 9.60k | goto transparency_exit; |
215 | 9.60k | } |
216 | | |
217 | 4.66k | code = pdfi_dict_next(ctx, cspace_dict, &Key, &Value, &index); |
218 | 4.66k | if (code == 0 && pdfi_type_of(Value) == PDF_ARRAY) |
219 | 4.39k | break; |
220 | 266 | pdfi_countdown(Key); |
221 | 266 | Key = NULL; |
222 | 266 | pdfi_countdown(Value); |
223 | 266 | Value = NULL; |
224 | 266 | } while(1); |
225 | 14.0k | }while (1); |
226 | 9.73k | } |
227 | 8 | return 0; |
228 | | |
229 | 9.60k | transparency_exit: |
230 | 9.73k | error2: |
231 | 9.73k | pdfi_countdown(Key); |
232 | 9.73k | pdfi_countdown(Value); |
233 | | |
234 | 11.6k | error1: |
235 | 11.6k | (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */ |
236 | 11.6k | return code; |
237 | 9.73k | } |
238 | | |
239 | | /* |
240 | | * Process an individual Shading dictionary to see if it contains a ColorSpace with a spot colour |
241 | | */ |
242 | | static int pdfi_check_Shading(pdf_context *ctx, pdf_obj *shading, |
243 | | pdf_dict *page_dict, pdfi_check_tracker_t *tracker) |
244 | 25.4k | { |
245 | 25.4k | int code; |
246 | 25.4k | pdf_obj *o = NULL; |
247 | 25.4k | pdf_dict *shading_dict = NULL; |
248 | | |
249 | 25.4k | if (resource_is_checked(tracker, shading)) |
250 | 893 | return 0; |
251 | | |
252 | 24.5k | code = pdfi_dict_from_obj(ctx, shading, &shading_dict); |
253 | 24.5k | if (code < 0) |
254 | 4 | return code; |
255 | | |
256 | 24.5k | if (pdfi_type_of(shading_dict) != PDF_DICT) |
257 | 0 | return_error(gs_error_typecheck); |
258 | | |
259 | 24.5k | code = pdfi_dict_knownget(ctx, shading_dict, "ColorSpace", (pdf_obj **)&o); |
260 | 24.5k | if (code > 0) { |
261 | 23.5k | code = pdfi_check_ColorSpace_for_spots(ctx, o, shading_dict, page_dict, tracker->spot_dict); |
262 | 23.5k | pdfi_countdown(o); |
263 | 23.5k | return code; |
264 | 23.5k | } |
265 | 1.02k | return 0; |
266 | 24.5k | } |
267 | | |
268 | | /* |
269 | | * Check the Resources dictionary Shading entry. |
270 | | */ |
271 | | static int pdfi_check_Shading_dict(pdf_context *ctx, pdf_dict *shading_dict, |
272 | | pdf_dict *page_dict, pdfi_check_tracker_t *tracker) |
273 | 82.8k | { |
274 | 82.8k | int code; |
275 | 82.8k | uint64_t i, index; |
276 | 82.8k | pdf_obj *Key = NULL, *Value = NULL; |
277 | | |
278 | 82.8k | if (resource_is_checked(tracker, (pdf_obj *)shading_dict)) |
279 | 0 | return 0; |
280 | | |
281 | 82.8k | if (pdfi_type_of(shading_dict) != PDF_DICT) |
282 | 79.5k | return_error(gs_error_typecheck); |
283 | | |
284 | 3.34k | if (pdfi_dict_entries(shading_dict) > 0) { |
285 | 3.34k | code = pdfi_loop_detector_mark(ctx); /* Mark the start of the Shading dictionary loop */ |
286 | 3.34k | if (code < 0) |
287 | 0 | return code; |
288 | | |
289 | 3.34k | code = pdfi_dict_first(ctx, shading_dict, &Key, &Value, &index); |
290 | 3.34k | if (code < 0) |
291 | 1.46k | goto error2; |
292 | | |
293 | 1.88k | i = 1; |
294 | 24.5k | do { |
295 | 24.5k | if ((pdfi_type_of(Value) == PDF_DICT || pdfi_type_of(Value) == PDF_STREAM)) { |
296 | 24.5k | code = pdfi_check_Shading(ctx, Value, page_dict, tracker); |
297 | 24.5k | if (code < 0) |
298 | 31 | goto error2; |
299 | 24.5k | } |
300 | | |
301 | 24.5k | pdfi_countdown(Key); |
302 | 24.5k | Key = NULL; |
303 | 24.5k | pdfi_countdown(Value); |
304 | 24.5k | Value = NULL; |
305 | | |
306 | 24.5k | (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the Shading dictionary loop */ |
307 | | |
308 | 24.5k | code = pdfi_loop_detector_mark(ctx); /* Mark the new start of the Shading dictionary loop */ |
309 | 24.5k | if (code < 0) |
310 | 0 | goto error1; |
311 | | |
312 | 45.1k | do { |
313 | 45.1k | if (i++ >= pdfi_dict_entries(shading_dict)) { |
314 | 1.85k | code = 0; |
315 | 1.85k | goto transparency_exit; |
316 | 1.85k | } |
317 | | |
318 | 43.3k | code = pdfi_dict_next(ctx, shading_dict, &Key, &Value, &index); |
319 | 43.3k | if (code == 0 && pdfi_type_of(Value) == PDF_DICT) |
320 | 22.6k | break; |
321 | 20.6k | pdfi_countdown(Key); |
322 | 20.6k | Key = NULL; |
323 | 20.6k | pdfi_countdown(Value); |
324 | 20.6k | Value = NULL; |
325 | 20.6k | } while(1); |
326 | 24.5k | }while (1); |
327 | 1.88k | } |
328 | 0 | return 0; |
329 | | |
330 | 1.85k | transparency_exit: |
331 | 3.34k | error2: |
332 | 3.34k | pdfi_countdown(Key); |
333 | 3.34k | pdfi_countdown(Value); |
334 | | |
335 | 3.34k | error1: |
336 | 3.34k | (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */ |
337 | 3.34k | return code; |
338 | 3.34k | } |
339 | | |
340 | | /* |
341 | | * This routine checks an XObject to see if it contains any spot |
342 | | * colour definitions, or transparency usage. |
343 | | */ |
344 | | static int pdfi_check_XObject(pdf_context *ctx, pdf_dict *xobject, pdf_dict *page_dict, |
345 | | pdfi_check_tracker_t *tracker) |
346 | 571k | { |
347 | 571k | int code = 0; |
348 | 571k | pdf_name *n = NULL; |
349 | 571k | bool known = false; |
350 | 571k | double f; |
351 | | |
352 | 571k | if (resource_is_checked(tracker, (pdf_obj *)xobject)) |
353 | 200k | return 0; |
354 | | |
355 | 371k | if (pdfi_type_of(xobject) != PDF_DICT) |
356 | 868 | return_error(gs_error_typecheck); |
357 | | |
358 | 370k | code = pdfi_dict_get_type(ctx, xobject, "Subtype", PDF_NAME, (pdf_obj **)&n); |
359 | 370k | if (code >= 0) { |
360 | 369k | if (pdfi_name_is((const pdf_name *)n, "Image")) { |
361 | 206k | pdf_obj *CS = NULL; |
362 | | |
363 | 206k | pdfi_countdown(n); |
364 | 206k | n = NULL; |
365 | 206k | code = pdfi_dict_known(ctx, xobject, "SMask", &known); |
366 | 206k | if (code >= 0) { |
367 | 206k | if (known == true) { |
368 | 80.1k | tracker->transparent = true; |
369 | 80.1k | if (tracker->spot_dict == NULL) |
370 | 68.5k | goto transparency_exit; |
371 | 80.1k | } |
372 | 137k | code = pdfi_dict_knownget_number(ctx, xobject, "SMaskInData", &f); |
373 | 137k | if (code > 0) { |
374 | 0 | code = 0; |
375 | 0 | if (f != 0.0) |
376 | 0 | tracker->transparent = true; |
377 | 0 | if (tracker->spot_dict == NULL) |
378 | 0 | goto transparency_exit; |
379 | 0 | } |
380 | | /* Check the image dictionary for a ColorSpace entry, if we are checking spot names */ |
381 | 137k | if (tracker->spot_dict) { |
382 | 27.8k | code = pdfi_dict_knownget(ctx, xobject, "ColorSpace", (pdf_obj **)&CS); |
383 | 27.8k | if (code > 0) { |
384 | | /* We don't care if there's an error here, it'll be picked up if we use the ColorSpace later */ |
385 | 24.8k | (void)pdfi_check_ColorSpace_for_spots(ctx, CS, xobject, page_dict, tracker->spot_dict); |
386 | 24.8k | pdfi_countdown(CS); |
387 | 24.8k | } |
388 | 27.8k | } |
389 | 137k | } |
390 | 206k | } else { |
391 | 162k | if (pdfi_name_is((const pdf_name *)n, "Form")) { |
392 | 162k | pdf_dict *group_dict = NULL, *resource_dict = NULL; |
393 | 162k | pdf_obj *CS = NULL; |
394 | | |
395 | 162k | pdfi_countdown(n); |
396 | 162k | code = pdfi_dict_knownget_type(ctx, xobject, "Group", PDF_DICT, (pdf_obj **)&group_dict); |
397 | 162k | if (code > 0) { |
398 | 29.4k | tracker->transparent = true; |
399 | 29.4k | if (tracker->spot_dict != NULL) { |
400 | | /* Start a new loop detector group to avoid this being detected in the Resources check below */ |
401 | 4.43k | code = pdfi_loop_detector_mark(ctx); /* Mark the start of the XObject dictionary loop */ |
402 | 4.43k | if (code == 0) { |
403 | 4.43k | code = pdfi_dict_knownget(ctx, group_dict, "CS", &CS); |
404 | 4.43k | if (code > 0) |
405 | | /* We don't care if there's an error here, it'll be picked up if we use the ColorSpace later */ |
406 | 3.30k | (void)pdfi_check_ColorSpace_for_spots(ctx, CS, group_dict, page_dict, tracker->spot_dict); |
407 | 4.43k | (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the XObject dictionary loop */ |
408 | 4.43k | } |
409 | 4.43k | pdfi_countdown(group_dict); |
410 | 4.43k | pdfi_countdown(CS); |
411 | 4.43k | } |
412 | 25.0k | else if (tracker->BM_Not_Normal) |
413 | 124 | { |
414 | | /* We already know it has a non-normal Blend Mode. No point in keeping searching. */ |
415 | 124 | pdfi_countdown(group_dict); |
416 | 124 | goto transparency_exit; |
417 | 124 | } |
418 | 24.9k | else |
419 | 24.9k | { |
420 | 24.9k | pdfi_countdown(group_dict); |
421 | 24.9k | } |
422 | | /* We need to keep checking Resources in case there are non-Normal blend mode things still to be found. */ |
423 | 29.4k | } |
424 | | |
425 | 162k | code = pdfi_dict_knownget_type(ctx, xobject, "Resources", PDF_DICT, (pdf_obj **)&resource_dict); |
426 | 162k | if (code > 0) { |
427 | 158k | if (ctx->loop_detection && pdf_object_num((pdf_obj *)resource_dict) != 0) { |
428 | 92.0k | code = pdfi_loop_detector_add_object(ctx, resource_dict->object_num); |
429 | 92.0k | if (code < 0) { |
430 | 0 | pdfi_countdown(resource_dict); |
431 | 0 | goto transparency_exit; |
432 | 0 | } |
433 | 92.0k | } |
434 | 158k | code = pdfi_check_Resources(ctx, resource_dict, page_dict, tracker); |
435 | 158k | pdfi_countdown(resource_dict); |
436 | 158k | if (code < 0) |
437 | 0 | goto transparency_exit; |
438 | 158k | } |
439 | 162k | } else |
440 | 562 | pdfi_countdown(n); |
441 | 162k | } |
442 | 369k | } |
443 | | |
444 | 301k | return 0; |
445 | | |
446 | 68.6k | transparency_exit: |
447 | 68.6k | return code; |
448 | 370k | } |
449 | | |
450 | | /* |
451 | | * Check the Resources dictionary XObject entry. |
452 | | */ |
453 | | static int pdfi_check_XObject_dict(pdf_context *ctx, pdf_dict *xobject_dict, pdf_dict *page_dict, |
454 | | pdfi_check_tracker_t *tracker) |
455 | 834k | { |
456 | 834k | int code; |
457 | 834k | uint64_t i, index; |
458 | 834k | pdf_obj *Key = NULL, *Value = NULL; |
459 | 834k | pdf_dict *Value_dict = NULL; |
460 | | |
461 | 834k | if (resource_is_checked(tracker, (pdf_obj *)xobject_dict)) |
462 | 0 | return 0; |
463 | | |
464 | 834k | if (pdfi_type_of(xobject_dict) != PDF_DICT) |
465 | 498k | return_error(gs_error_typecheck); |
466 | | |
467 | 335k | if (pdfi_dict_entries(xobject_dict) > 0) { |
468 | 334k | code = pdfi_loop_detector_mark(ctx); /* Mark the start of the XObject dictionary loop */ |
469 | 334k | if (code < 0) |
470 | 0 | return code; |
471 | | |
472 | 334k | code = pdfi_dict_first(ctx, xobject_dict, &Key, &Value, &index); |
473 | 334k | if (code < 0) |
474 | 46.9k | goto error_exit; |
475 | | |
476 | 287k | i = 1; |
477 | 572k | do { |
478 | 572k | if (pdfi_type_of(Value) == PDF_STREAM) { |
479 | 570k | code = pdfi_dict_from_obj(ctx, Value, &Value_dict); |
480 | 570k | if (code < 0) |
481 | 0 | goto error_exit; |
482 | | |
483 | 570k | code = pdfi_check_XObject(ctx, Value_dict, page_dict, tracker); |
484 | 570k | if (code < 0) |
485 | 0 | goto error_exit; |
486 | 570k | } |
487 | | |
488 | 572k | (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the XObject dictionary loop */ |
489 | | |
490 | 572k | code = pdfi_loop_detector_mark(ctx); /* Mark the new start of the XObject dictionary loop */ |
491 | 572k | if (code < 0) |
492 | 0 | goto error_exit; |
493 | | |
494 | 572k | pdfi_countdown(Key); |
495 | 572k | Key = NULL; |
496 | 572k | pdfi_countdown(Value); |
497 | 572k | Value = NULL; |
498 | 572k | Value_dict = NULL; |
499 | | |
500 | 669k | do { |
501 | 669k | if (i++ >= pdfi_dict_entries(xobject_dict)) { |
502 | 287k | code = 0; |
503 | 287k | goto transparency_exit; |
504 | 287k | } |
505 | | |
506 | 382k | code = pdfi_dict_next(ctx, xobject_dict, &Key, &Value, &index); |
507 | 382k | if (code == 0 && pdfi_type_of(Value) == PDF_STREAM) |
508 | 285k | break; |
509 | 97.1k | pdfi_countdown(Key); |
510 | 97.1k | Key = NULL; |
511 | 97.1k | pdfi_countdown(Value); |
512 | 97.1k | Value = NULL; |
513 | 97.1k | } while(1); |
514 | 572k | }while(1); |
515 | 287k | } |
516 | 1.77k | return 0; |
517 | | |
518 | 287k | transparency_exit: |
519 | 334k | error_exit: |
520 | 334k | pdfi_countdown(Key); |
521 | 334k | pdfi_countdown(Value); |
522 | 334k | (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */ |
523 | 334k | return code; |
524 | 287k | } |
525 | | |
526 | | /* |
527 | | * This routine checks an ExtGState dictionary to see if it contains transparency or OP |
528 | | */ |
529 | | static int pdfi_check_ExtGState(pdf_context *ctx, pdf_dict *extgstate_dict, pdf_dict *page_dict, |
530 | | pdfi_check_tracker_t *tracker) |
531 | 306k | { |
532 | 306k | int code; |
533 | 306k | pdf_obj *o = NULL; |
534 | 306k | double f; |
535 | 306k | bool overprint; |
536 | | |
537 | 306k | if (resource_is_checked(tracker, (pdf_obj *)extgstate_dict)) |
538 | 93.9k | return 0; |
539 | | |
540 | 212k | if (pdfi_type_of(extgstate_dict) != PDF_DICT) |
541 | 1.82k | return_error(gs_error_typecheck); |
542 | | |
543 | 210k | if (pdfi_dict_entries(extgstate_dict) > 0) { |
544 | | /* See if /OP or /op is true */ |
545 | 210k | code = pdfi_dict_get_bool(ctx, extgstate_dict, "OP", &overprint); |
546 | 210k | if (code == 0 && overprint) |
547 | 7.96k | tracker->has_overprint = true; |
548 | 210k | code = pdfi_dict_get_bool(ctx, extgstate_dict, "op", &overprint); |
549 | 210k | if (code == 0 && overprint) |
550 | 8.57k | tracker->has_overprint = true; |
551 | | |
552 | | /* Check Blend Mode *first* because we short-circuit transparency detection |
553 | | * when we find transparency, and we want to be able to record the use of |
554 | | * blend modes other than Compatible/Normal so that Patterns using these |
555 | | * always use the clist and not tiles. The tiles implementation can't work |
556 | | * if we change to a blend mode other than Normal or Compatible during |
557 | | * the course of a Pattern. |
558 | | */ |
559 | 210k | code = pdfi_dict_knownget_type(ctx, extgstate_dict, "BM", PDF_NAME, &o); |
560 | 210k | if (code > 0) { |
561 | 115k | if (!pdfi_name_is((pdf_name *)o, "Normal")) { |
562 | 9.69k | if (!pdfi_name_is((pdf_name *)o, "Compatible")) { |
563 | 9.65k | pdfi_countdown(o); |
564 | 9.65k | tracker->transparent = true; |
565 | 9.65k | tracker->BM_Not_Normal = true; |
566 | 9.65k | return 0; |
567 | 9.65k | } |
568 | 9.69k | } |
569 | 115k | } |
570 | 200k | pdfi_countdown(o); |
571 | 200k | o = NULL; |
572 | | |
573 | | /* Check SMask */ |
574 | 200k | code = pdfi_dict_knownget(ctx, extgstate_dict, "SMask", &o); |
575 | 200k | if (code > 0) { |
576 | 28.3k | switch (pdfi_type_of(o)) { |
577 | 24.1k | case PDF_NAME: |
578 | 24.1k | if (!pdfi_name_is((pdf_name *)o, "None")) { |
579 | 49 | pdfi_countdown(o); |
580 | 49 | tracker->transparent = true; |
581 | 49 | return 0; |
582 | 49 | } |
583 | 24.0k | break; |
584 | 24.0k | case PDF_DICT: |
585 | 4.20k | { |
586 | 4.20k | pdf_obj *G = NULL; |
587 | | |
588 | 4.20k | tracker->transparent = true; |
589 | | |
590 | 4.20k | if (tracker->spot_dict != NULL) { |
591 | | /* Check if the SMask has a /G (Group) */ |
592 | 1.01k | code = pdfi_dict_knownget(ctx, (pdf_dict *)o, "G", &G); |
593 | 1.01k | if (code > 0) { |
594 | 868 | code = pdfi_check_XObject(ctx, (pdf_dict *)G, page_dict, |
595 | 868 | tracker); |
596 | 868 | pdfi_countdown(G); |
597 | 868 | } |
598 | 1.01k | } |
599 | 4.20k | pdfi_countdown(o); |
600 | 4.20k | return code; |
601 | 24.1k | } |
602 | 0 | default: |
603 | 0 | break; |
604 | 28.3k | } |
605 | 28.3k | } |
606 | 196k | pdfi_countdown(o); |
607 | 196k | o = NULL; |
608 | | |
609 | 196k | code = pdfi_dict_knownget_number(ctx, extgstate_dict, "CA", &f); |
610 | 196k | if (code > 0) { |
611 | 105k | if (f != 1.0) { |
612 | 26.8k | tracker->transparent = true; |
613 | 26.8k | return 0; |
614 | 26.8k | } |
615 | 105k | } |
616 | | |
617 | 169k | code = pdfi_dict_knownget_number(ctx, extgstate_dict, "ca", &f); |
618 | 169k | if (code > 0) { |
619 | 82.1k | if (f != 1.0) { |
620 | 1.69k | tracker->transparent = true; |
621 | 1.69k | return 0; |
622 | 1.69k | } |
623 | 82.1k | } |
624 | | |
625 | 169k | } |
626 | 167k | return 0; |
627 | 210k | } |
628 | | |
629 | | /* |
630 | | * Check the Resources dictionary ExtGState entry. |
631 | | */ |
632 | | static int pdfi_check_ExtGState_dict(pdf_context *ctx, pdf_dict *extgstate_dict, pdf_dict *page_dict, |
633 | | pdfi_check_tracker_t *tracker) |
634 | 834k | { |
635 | 834k | int code; |
636 | 834k | uint64_t i, index; |
637 | 834k | pdf_obj *Key = NULL, *Value = NULL; |
638 | | |
639 | 834k | if (resource_is_checked(tracker, (pdf_obj *)extgstate_dict)) |
640 | 193 | return 0; |
641 | | |
642 | 834k | if (pdfi_type_of(extgstate_dict) != PDF_DICT) |
643 | 615k | return_error(gs_error_typecheck); |
644 | | |
645 | 219k | if (pdfi_dict_entries(extgstate_dict) > 0) { |
646 | 217k | code = pdfi_loop_detector_mark(ctx); /* Mark the start of the ColorSpace dictionary loop */ |
647 | 217k | if (code < 0) |
648 | 0 | return code; |
649 | | |
650 | 217k | code = pdfi_dict_first(ctx, extgstate_dict, &Key, &Value, &index); |
651 | 217k | if (code < 0) |
652 | 20.1k | goto error1; |
653 | | |
654 | 197k | i = 1; |
655 | 306k | do { |
656 | | |
657 | 306k | (void)pdfi_check_ExtGState(ctx, (pdf_dict *)Value, page_dict, tracker); |
658 | 306k | if (tracker->transparent == true && tracker->spot_dict == NULL) |
659 | 36.7k | goto transparency_exit; |
660 | | |
661 | 269k | pdfi_countdown(Key); |
662 | 269k | Key = NULL; |
663 | 269k | pdfi_countdown(Value); |
664 | 269k | Value = NULL; |
665 | | |
666 | 269k | (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the ExtGState dictionary loop */ |
667 | | |
668 | 269k | code = pdfi_loop_detector_mark(ctx); /* Mark the new start of the ExtGState dictionary loop */ |
669 | 269k | if (code < 0) |
670 | 0 | goto error1; |
671 | | |
672 | 291k | do { |
673 | 291k | if (i++ >= pdfi_dict_entries(extgstate_dict)) { |
674 | 160k | code = 0; |
675 | 160k | goto transparency_exit; |
676 | 160k | } |
677 | | |
678 | 130k | code = pdfi_dict_next(ctx, extgstate_dict, &Key, &Value, &index); |
679 | 130k | if (code == 0 && pdfi_type_of(Value) == PDF_DICT) |
680 | 108k | break; |
681 | 22.0k | pdfi_countdown(Key); |
682 | 22.0k | Key = NULL; |
683 | 22.0k | pdfi_countdown(Value); |
684 | 22.0k | Value = NULL; |
685 | 22.0k | } while(1); |
686 | 269k | }while (1); |
687 | 197k | } |
688 | 1.52k | return 0; |
689 | | |
690 | 197k | transparency_exit: |
691 | 197k | pdfi_countdown(Key); |
692 | 197k | pdfi_countdown(Value); |
693 | | |
694 | 217k | error1: |
695 | 217k | (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */ |
696 | 217k | return code; |
697 | 197k | } |
698 | | |
699 | | /* |
700 | | * This routine checks a Pattern dictionary to see if it contains any spot |
701 | | * colour definitions, or transparency usage (or blend modes other than Normal/Compatible). |
702 | | */ |
703 | | static int pdfi_check_Pattern(pdf_context *ctx, pdf_dict *pattern, pdf_dict *page_dict, |
704 | | pdfi_check_tracker_t *tracker) |
705 | 135k | { |
706 | 135k | int code = 0; |
707 | 135k | pdf_obj *o = NULL; |
708 | | |
709 | 135k | if (resource_is_checked(tracker, (pdf_obj *)pattern)) |
710 | 1.20k | return 0; |
711 | | |
712 | 134k | if (pdfi_type_of(pattern) != PDF_DICT) |
713 | 0 | return_error(gs_error_typecheck); |
714 | | |
715 | 134k | if (tracker->spot_dict != NULL) { |
716 | 18.4k | code = pdfi_dict_knownget(ctx, pattern, "Shading", &o); |
717 | 18.4k | if (code > 0) |
718 | 917 | (void)pdfi_check_Shading(ctx, o, page_dict, tracker); |
719 | 18.4k | pdfi_countdown(o); |
720 | 18.4k | o = NULL; |
721 | 18.4k | } |
722 | | |
723 | 134k | code = pdfi_dict_knownget_type(ctx, pattern, "Resources", PDF_DICT, &o); |
724 | 134k | if (code > 0) |
725 | 122k | (void)pdfi_check_Resources(ctx, (pdf_dict *)o, page_dict, tracker); |
726 | 134k | pdfi_countdown(o); |
727 | 134k | o = NULL; |
728 | 134k | if (tracker->transparent == true && tracker->spot_dict == NULL) |
729 | 63.7k | goto transparency_exit; |
730 | | |
731 | 71.0k | code = pdfi_dict_knownget_type(ctx, pattern, "ExtGState", PDF_DICT, &o); |
732 | 71.0k | if (code > 0) |
733 | 0 | (void)pdfi_check_ExtGState(ctx, (pdf_dict *)o, page_dict, tracker); |
734 | 71.0k | pdfi_countdown(o); |
735 | 71.0k | o = NULL; |
736 | | |
737 | 134k | transparency_exit: |
738 | 134k | return 0; |
739 | 71.0k | } |
740 | | |
741 | | /* |
742 | | * This routine checks a Pattern dictionary for transparency. |
743 | | */ |
744 | | int pdfi_check_Pattern_transparency(pdf_context *ctx, pdf_dict *pattern, pdf_dict *page_dict, |
745 | | bool *transparent, bool *BM_Not_Normal) |
746 | 26.2k | { |
747 | 26.2k | int code; |
748 | 26.2k | pdfi_check_tracker_t tracker = {0, 0, 0, NULL, NULL, 0, NULL}; |
749 | | |
750 | | /* NOTE: We use a "null" tracker that won't do any optimization to prevent |
751 | | * checking the same resource twice. |
752 | | * This means we don't get the optimization, but we also don't have the overhead |
753 | | * of setting it up. |
754 | | * If we do want the optimization, call pdfi_check_init_tracker() and |
755 | | * pdfi_check_free_tracker() as in pdfi_check_page(), below. |
756 | | */ |
757 | 26.2k | code = pdfi_check_Pattern(ctx, pattern, page_dict, &tracker); |
758 | 26.2k | if (code == 0) { |
759 | 26.2k | *transparent = tracker.transparent; |
760 | 26.2k | *BM_Not_Normal = tracker.BM_Not_Normal; |
761 | 26.2k | } |
762 | 0 | else |
763 | 0 | *transparent = false; |
764 | 26.2k | return code; |
765 | 26.2k | } |
766 | | |
767 | | /* |
768 | | * Check the Resources dictionary Pattern entry. |
769 | | */ |
770 | | static int pdfi_check_Pattern_dict(pdf_context *ctx, pdf_dict *pattern_dict, pdf_dict *page_dict, |
771 | | pdfi_check_tracker_t *tracker) |
772 | 834k | { |
773 | 834k | int code; |
774 | 834k | uint64_t i, index; |
775 | 834k | pdf_obj *Key = NULL, *Value = NULL; |
776 | 834k | pdf_dict *instance_dict = NULL; |
777 | | |
778 | 834k | if (resource_is_checked(tracker, (pdf_obj *)pattern_dict)) |
779 | 0 | return 0; |
780 | | |
781 | 834k | if (pdfi_type_of(pattern_dict) != PDF_DICT) |
782 | 817k | return_error(gs_error_typecheck); |
783 | | |
784 | 17.2k | if (pdfi_dict_entries(pattern_dict) > 0) { |
785 | 15.6k | code = pdfi_loop_detector_mark(ctx); /* Mark the start of the Pattern dictionary loop */ |
786 | 15.6k | if (code < 0) |
787 | 0 | return code; |
788 | | |
789 | 15.6k | code = pdfi_dict_first(ctx, pattern_dict, &Key, &Value, &index); |
790 | 15.6k | if (code < 0) |
791 | 2.67k | goto error1; |
792 | | |
793 | 12.9k | i = 1; |
794 | 109k | do { |
795 | 109k | if (pdfi_type_of(Value) == PDF_DICT || pdfi_type_of(Value) == PDF_STREAM) { |
796 | 109k | code = pdfi_dict_from_obj(ctx, Value, &instance_dict); |
797 | 109k | if (code < 0) |
798 | 0 | goto transparency_exit; |
799 | | |
800 | 109k | code = pdfi_check_Pattern(ctx, instance_dict, page_dict, tracker); |
801 | 109k | if (code < 0) |
802 | 0 | goto transparency_exit; |
803 | 109k | } |
804 | | |
805 | 109k | pdfi_countdown(Key); |
806 | 109k | Key = NULL; |
807 | 109k | pdfi_countdown(Value); |
808 | 109k | instance_dict = NULL; |
809 | 109k | Value = NULL; |
810 | 109k | (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the Shading dictionary loop */ |
811 | | |
812 | 109k | code = pdfi_loop_detector_mark(ctx); /* Mark the new start of the Shading dictionary loop */ |
813 | 109k | if (code < 0) |
814 | 0 | goto error1; |
815 | | |
816 | 235k | do { |
817 | 235k | if (i++ >= pdfi_dict_entries(pattern_dict)) { |
818 | 12.9k | code = 0; |
819 | 12.9k | goto transparency_exit; |
820 | 12.9k | } |
821 | | |
822 | 222k | code = pdfi_dict_next(ctx, pattern_dict, &Key, &Value, &index); |
823 | 222k | if (code == 0 && (pdfi_type_of(Value) == PDF_DICT || pdfi_type_of(Value) == PDF_STREAM)) |
824 | 97.0k | break; |
825 | 125k | pdfi_countdown(Key); |
826 | 125k | Key = NULL; |
827 | 125k | pdfi_countdown(Value); |
828 | 125k | Value = NULL; |
829 | 125k | } while(1); |
830 | 109k | }while (1); |
831 | 12.9k | } |
832 | 1.59k | return 0; |
833 | | |
834 | 12.9k | transparency_exit: |
835 | 12.9k | pdfi_countdown(Key); |
836 | 12.9k | pdfi_countdown(Value); |
837 | 12.9k | pdfi_countdown(instance_dict); |
838 | | |
839 | 15.6k | error1: |
840 | 15.6k | (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */ |
841 | 15.6k | return code; |
842 | 12.9k | } |
843 | | |
844 | | /* |
845 | | * This routine checks a Font dictionary to see if it contains any spot |
846 | | * colour definitions, or transparency usage. While we are here, if the tracker's font_array |
847 | | * is not NULL, pick up the font information and store it in the array. |
848 | | */ |
849 | | static int pdfi_check_Font(pdf_context *ctx, pdf_dict *font, pdf_dict *page_dict, |
850 | | pdfi_check_tracker_t *tracker) |
851 | 561k | { |
852 | 561k | int code = 0; |
853 | 561k | pdf_obj *o = NULL; |
854 | | |
855 | 561k | if (resource_is_checked(tracker, (pdf_obj *)font)) |
856 | 68.1k | return 0; |
857 | | |
858 | 493k | if (pdfi_type_of(font) != PDF_DICT) |
859 | 0 | return_error(gs_error_typecheck); |
860 | | |
861 | 493k | if (tracker->font_array != NULL) { |
862 | | /* If we get to here this is a font we have not seen before. We need |
863 | | * to make a new font array big enough to hold the existing entries +1 |
864 | | * copy the existing entries to the new array and free the old array. |
865 | | * Finally create a dictionary with all the font information we want |
866 | | * and add it to the array. |
867 | | */ |
868 | 0 | pdf_array *new_fonts = NULL; |
869 | 0 | int index = 0; |
870 | 0 | pdf_obj *array_obj = NULL; |
871 | 0 | pdf_dict *font_info_dict = NULL; |
872 | | |
873 | | /* Let's start by gathering the information we need and storing it in a dictionary */ |
874 | 0 | code = pdfi_dict_alloc(ctx, 4, &font_info_dict); |
875 | 0 | if (code < 0) |
876 | 0 | return code; |
877 | 0 | pdfi_countup(font_info_dict); |
878 | |
|
879 | 0 | if (font->object_num != 0) { |
880 | 0 | pdf_num *int_obj = NULL; |
881 | |
|
882 | 0 | code = pdfi_object_alloc(ctx, PDF_INT, 0, (pdf_obj **)&int_obj); |
883 | 0 | if (code >= 0) { |
884 | 0 | pdfi_countup(int_obj); |
885 | 0 | int_obj->value.i = font->object_num; |
886 | 0 | code = pdfi_dict_put(ctx, font_info_dict, "ObjectNum", (pdf_obj *)int_obj); |
887 | 0 | pdfi_countdown(int_obj); |
888 | 0 | } |
889 | 0 | if (code < 0) { |
890 | 0 | pdfi_countdown(font_info_dict); |
891 | 0 | return code; |
892 | 0 | } |
893 | 0 | } |
894 | | |
895 | | /* Try to get font name with fallback: |
896 | | * 1. Try /BaseFont (standard for most fonts) |
897 | | * 2. Try /FontDescriptor/FontName (common for Type 3 fonts) |
898 | | * 3. Try /Name as last resort |
899 | | */ |
900 | 0 | { |
901 | 0 | pdf_obj *font_name = NULL; |
902 | 0 | int lookup_code; |
903 | 0 | bool found = false; |
904 | |
|
905 | 0 | lookup_code = pdfi_dict_get(ctx, font, "BaseFont", &font_name); |
906 | 0 | if (lookup_code >= 0 && pdfi_type_of(font_name) == PDF_NAME) { |
907 | 0 | code = pdfi_dict_put(ctx, font_info_dict, "BaseFont", font_name); |
908 | 0 | pdfi_countdown(font_name); |
909 | 0 | if (code < 0) { |
910 | 0 | pdfi_countdown(font_info_dict); |
911 | 0 | return code; |
912 | 0 | } |
913 | 0 | found = true; |
914 | 0 | } else { |
915 | 0 | pdfi_countdown(font_name); |
916 | 0 | font_name = NULL; |
917 | 0 | } |
918 | | |
919 | 0 | if (!found) { |
920 | 0 | lookup_code = pdfi_dict_get(ctx, font, "FontDescriptor", &font_name); |
921 | 0 | if (lookup_code >= 0 && pdfi_type_of(font_name) == PDF_DICT) { |
922 | 0 | pdf_dict *font_desc = (pdf_dict *)font_name; |
923 | 0 | pdf_obj *desc_font_name = NULL; |
924 | |
|
925 | 0 | lookup_code = pdfi_dict_get(ctx, font_desc, "FontName", &desc_font_name); |
926 | 0 | pdfi_countdown(font_name); |
927 | 0 | font_name = NULL; |
928 | |
|
929 | 0 | if (lookup_code >= 0 && pdfi_type_of(desc_font_name) == PDF_NAME) { |
930 | 0 | code = pdfi_dict_put(ctx, font_info_dict, "BaseFont", desc_font_name); |
931 | 0 | pdfi_countdown(desc_font_name); |
932 | 0 | if (code < 0) { |
933 | 0 | pdfi_countdown(font_info_dict); |
934 | 0 | return code; |
935 | 0 | } |
936 | 0 | found = true; |
937 | 0 | } else { |
938 | 0 | pdfi_countdown(desc_font_name); |
939 | 0 | } |
940 | 0 | } else { |
941 | 0 | pdfi_countdown(font_name); |
942 | 0 | font_name = NULL; |
943 | 0 | } |
944 | 0 | } |
945 | | |
946 | 0 | if (!found) { |
947 | 0 | lookup_code = pdfi_dict_get(ctx, font, "Name", &font_name); |
948 | 0 | if (lookup_code >= 0 && pdfi_type_of(font_name) == PDF_NAME) { |
949 | 0 | code = pdfi_dict_put(ctx, font_info_dict, "BaseFont", font_name); |
950 | 0 | pdfi_countdown(font_name); |
951 | 0 | if (code < 0) { |
952 | 0 | pdfi_countdown(font_info_dict); |
953 | 0 | return code; |
954 | 0 | } |
955 | 0 | } else { |
956 | 0 | pdfi_countdown(font_name); |
957 | 0 | } |
958 | 0 | } |
959 | | |
960 | 0 | array_obj = NULL; |
961 | 0 | } |
962 | | |
963 | 0 | code = pdfi_dict_get(ctx, font, "ToUnicode", &array_obj); |
964 | 0 | if (code >= 0) |
965 | 0 | code = pdfi_dict_put(ctx, font_info_dict, "ToUnicode", PDF_TRUE_OBJ); |
966 | 0 | else |
967 | 0 | code = pdfi_dict_put(ctx, font_info_dict, "ToUnicode", PDF_FALSE_OBJ); |
968 | 0 | pdfi_countdown(array_obj); |
969 | 0 | array_obj = NULL; |
970 | 0 | if (code < 0) |
971 | 0 | return code; |
972 | | |
973 | 0 | code = pdfi_dict_get(ctx, font, "FontDescriptor", &array_obj); |
974 | 0 | if (code >= 0) { |
975 | 0 | bool known = false; |
976 | |
|
977 | 0 | (void)pdfi_dict_known(ctx, (pdf_dict *)array_obj, "FontFile", &known); |
978 | 0 | if (!known) { |
979 | 0 | (void)pdfi_dict_known(ctx, (pdf_dict *)array_obj, "FontFile2", &known); |
980 | 0 | if (!known) { |
981 | 0 | (void)pdfi_dict_known(ctx, (pdf_dict *)array_obj, "FontFile3", &known); |
982 | 0 | } |
983 | 0 | } |
984 | |
|
985 | 0 | if (known > 0) |
986 | 0 | code = pdfi_dict_put(ctx, font_info_dict, "Embedded", PDF_TRUE_OBJ); |
987 | 0 | else |
988 | 0 | code = pdfi_dict_put(ctx, font_info_dict, "Embedded", PDF_FALSE_OBJ); |
989 | 0 | } else |
990 | 0 | code = pdfi_dict_put(ctx, font_info_dict, "Embedded", PDF_FALSE_OBJ); |
991 | |
|
992 | 0 | pdfi_countdown(array_obj); |
993 | 0 | array_obj = NULL; |
994 | |
|
995 | 0 | if (code < 0) |
996 | 0 | return code; |
997 | | |
998 | | |
999 | 0 | code = pdfi_dict_knownget_type(ctx, font, "Subtype", PDF_NAME, &array_obj); |
1000 | 0 | if (code > 0) { |
1001 | 0 | code = pdfi_dict_put(ctx, font_info_dict, "Subtype", array_obj); |
1002 | 0 | if (code < 0) { |
1003 | 0 | pdfi_countdown(array_obj); |
1004 | 0 | pdfi_countdown(font_info_dict); |
1005 | 0 | return code; |
1006 | 0 | } |
1007 | | |
1008 | 0 | if (pdfi_name_is((pdf_name *)array_obj, "Type3")) { |
1009 | 0 | pdfi_countdown(o); |
1010 | 0 | o = NULL; |
1011 | |
|
1012 | 0 | code = pdfi_dict_knownget_type(ctx, font, "Resources", PDF_DICT, &o); |
1013 | 0 | if (code > 0) |
1014 | 0 | (void)pdfi_check_Resources(ctx, (pdf_dict *)o, page_dict, tracker); |
1015 | 0 | } |
1016 | |
|
1017 | 0 | if (pdfi_name_is((const pdf_name *)array_obj, "Type0")){ |
1018 | 0 | pdf_array *descendants = NULL; |
1019 | 0 | pdf_dict *desc_font = NULL; |
1020 | |
|
1021 | 0 | code = pdfi_dict_get(ctx, font, "DescendantFonts", (pdf_obj **)&descendants); |
1022 | 0 | if (code >= 0) { |
1023 | 0 | code = pdfi_array_get(ctx, descendants, 0, (pdf_obj **)&desc_font); |
1024 | 0 | if (code >= 0){ |
1025 | 0 | pdf_array *desc_array = NULL; |
1026 | |
|
1027 | 0 | code = pdfi_array_alloc(ctx, 0, &desc_array); |
1028 | 0 | if (code >= 0) { |
1029 | 0 | pdf_array *saved = tracker->font_array; |
1030 | |
|
1031 | 0 | pdfi_countup(desc_array); |
1032 | 0 | tracker->font_array = desc_array; |
1033 | 0 | (void)pdfi_check_Font(ctx, desc_font, page_dict, tracker); |
1034 | 0 | (void)pdfi_dict_put(ctx, font_info_dict, "Descendants", (pdf_obj *)tracker->font_array); |
1035 | 0 | pdfi_countdown((pdf_obj *)tracker->font_array); |
1036 | 0 | tracker->font_array = saved; |
1037 | 0 | } |
1038 | 0 | pdfi_countdown(descendants); |
1039 | 0 | pdfi_countdown(desc_font); |
1040 | 0 | } |
1041 | 0 | } |
1042 | 0 | } |
1043 | 0 | } |
1044 | 0 | pdfi_countdown(array_obj); |
1045 | 0 | array_obj = NULL; |
1046 | |
|
1047 | 0 | code = pdfi_array_alloc(ctx, pdfi_array_size(tracker->font_array) + 1, &new_fonts); |
1048 | 0 | if (code < 0) { |
1049 | 0 | pdfi_countdown(font_info_dict); |
1050 | 0 | return code; |
1051 | 0 | } |
1052 | 0 | pdfi_countup(new_fonts); |
1053 | |
|
1054 | 0 | for (index = 0; index < pdfi_array_size(tracker->font_array); index++) { |
1055 | 0 | code = pdfi_array_get(ctx, tracker->font_array, index, &array_obj); |
1056 | 0 | if (code < 0) { |
1057 | 0 | pdfi_countdown(font_info_dict); |
1058 | 0 | pdfi_countdown(new_fonts); |
1059 | 0 | return code; |
1060 | 0 | } |
1061 | 0 | code = pdfi_array_put(ctx, new_fonts, index, array_obj); |
1062 | 0 | pdfi_countdown(array_obj); |
1063 | 0 | if (code < 0) { |
1064 | 0 | pdfi_countdown(font_info_dict); |
1065 | 0 | pdfi_countdown(new_fonts); |
1066 | 0 | return code; |
1067 | 0 | } |
1068 | 0 | } |
1069 | 0 | code = pdfi_array_put(ctx, new_fonts, index, (pdf_obj *)font_info_dict); |
1070 | 0 | if (code < 0) { |
1071 | 0 | pdfi_countdown(font_info_dict); |
1072 | 0 | pdfi_countdown(new_fonts); |
1073 | 0 | return code; |
1074 | 0 | } |
1075 | 0 | pdfi_countdown(font_info_dict); |
1076 | 0 | pdfi_countdown(tracker->font_array); |
1077 | 0 | tracker->font_array = new_fonts; |
1078 | 493k | } else { |
1079 | 493k | code = pdfi_dict_knownget_type(ctx, font, "Subtype", PDF_NAME, &o); |
1080 | 493k | if (code > 0) { |
1081 | 490k | if (pdfi_name_is((pdf_name *)o, "Type3")) { |
1082 | 9.93k | pdfi_countdown(o); |
1083 | 9.93k | o = NULL; |
1084 | | |
1085 | 9.93k | code = pdfi_dict_knownget_type(ctx, font, "Resources", PDF_DICT, &o); |
1086 | 9.93k | if (code > 0) |
1087 | 3.07k | (void)pdfi_check_Resources(ctx, (pdf_dict *)o, page_dict, tracker); |
1088 | 9.93k | } |
1089 | 490k | } |
1090 | | |
1091 | 493k | pdfi_countdown(o); |
1092 | 493k | o = NULL; |
1093 | 493k | } |
1094 | | |
1095 | 493k | return 0; |
1096 | 493k | } |
1097 | | |
1098 | | /* |
1099 | | * Check the Resources dictionary Font entry. |
1100 | | */ |
1101 | | static int pdfi_check_Font_dict(pdf_context *ctx, pdf_dict *font_dict, pdf_dict *page_dict, |
1102 | | pdfi_check_tracker_t *tracker) |
1103 | 834k | { |
1104 | 834k | int code = 0; |
1105 | 834k | uint64_t i, index; |
1106 | 834k | pdf_obj *Key = NULL, *Value = NULL; |
1107 | | |
1108 | 834k | if (resource_is_checked(tracker, (pdf_obj *)font_dict)) |
1109 | 318 | return 0; |
1110 | | |
1111 | 834k | if (pdfi_type_of(font_dict) != PDF_DICT) |
1112 | 565k | return_error(gs_error_typecheck); |
1113 | | |
1114 | 268k | if (pdfi_dict_entries(font_dict) > 0) { |
1115 | 267k | code = pdfi_loop_detector_mark(ctx); /* Mark the start of the Font dictionary loop */ |
1116 | 267k | if (code < 0) |
1117 | 0 | return code; |
1118 | | |
1119 | | /* We don't want to store the dereferenced Font objects in the Resources dictionary |
1120 | | * Because we later create a substitute object, if we use the object here we will not |
1121 | | * find the correct font if we reference it through the Resources dictionary. |
1122 | | */ |
1123 | 267k | code = pdfi_dict_first_no_store_R(ctx, font_dict, &Key, &Value, &index); |
1124 | 267k | if (code < 0) { |
1125 | 51.4k | (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */ |
1126 | 51.4k | goto error1; |
1127 | 51.4k | } |
1128 | | |
1129 | 216k | i = 1; |
1130 | 573k | do { |
1131 | 573k | if (pdfi_type_of(Value) == PDF_DICT) |
1132 | 403k | code = pdfi_check_Font(ctx, (pdf_dict *)Value, page_dict, tracker); |
1133 | 169k | else if (pdfi_type_of(Value) == PDF_FONT) { |
1134 | 158k | pdf_dict *d = ((pdf_font *)Value)->PDF_font; |
1135 | | |
1136 | 158k | code = pdfi_check_Font(ctx, d, page_dict, tracker); |
1137 | 158k | } else { |
1138 | 11.4k | pdfi_set_warning(ctx, 0, NULL, W_PDF_FONTRESOURCE_TYPE, "pdfi_check_Font_dict", ""); |
1139 | 11.4k | } |
1140 | 573k | if (code < 0) |
1141 | 0 | break; |
1142 | | |
1143 | 573k | pdfi_countdown(Key); |
1144 | 573k | Key = NULL; |
1145 | 573k | pdfi_countdown(Value); |
1146 | 573k | Value = NULL; |
1147 | | |
1148 | 573k | (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the Font dictionary loop */ |
1149 | | |
1150 | 573k | code = pdfi_loop_detector_mark(ctx); /* Mark the new start of the Font dictionary loop */ |
1151 | 573k | if (code < 0) |
1152 | 0 | goto error1; |
1153 | | |
1154 | 573k | if (i++ >= pdfi_dict_entries(font_dict)) { |
1155 | 190k | code = 0; |
1156 | 190k | break; |
1157 | 190k | } |
1158 | | |
1159 | 383k | code = pdfi_dict_next_no_store_R(ctx, font_dict, &Key, &Value, &index); |
1160 | 383k | if (code < 0) |
1161 | 26.2k | break; |
1162 | 383k | }while (1); |
1163 | | |
1164 | 216k | (void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */ |
1165 | 216k | } |
1166 | | |
1167 | 217k | pdfi_countdown(Key); |
1168 | 217k | pdfi_countdown(Value); |
1169 | | |
1170 | 268k | error1: |
1171 | 268k | return code; |
1172 | 217k | } |
1173 | | |
1174 | | static int pdfi_check_Resources(pdf_context *ctx, pdf_dict *Resources_dict, |
1175 | | pdf_dict *page_dict, pdfi_check_tracker_t *tracker) |
1176 | 836k | { |
1177 | 836k | int code; |
1178 | 836k | pdf_obj *d = NULL; |
1179 | | |
1180 | 836k | if (resource_is_checked(tracker, (pdf_obj *)Resources_dict)) |
1181 | 2.07k | return 0; |
1182 | | |
1183 | 834k | if (pdfi_type_of(Resources_dict) != PDF_DICT) |
1184 | 0 | return_error(gs_error_typecheck); |
1185 | | |
1186 | | /* First up, check any colour spaces, for new spot colours. |
1187 | | * We only do this if asked because its expensive. spot_dict being NULL |
1188 | | * means we aren't interested in spot colours (not a DeviceN or Separation device) |
1189 | | */ |
1190 | 834k | if (tracker->spot_dict != NULL) { |
1191 | 82.8k | code = pdfi_dict_knownget_type(ctx, Resources_dict, "ColorSpace", PDF_DICT, &d); |
1192 | 82.8k | if (code < 0 && code != gs_error_undefined) { |
1193 | 34 | if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_RESOURCE, "pdfi_check_Resources", "ColorSpace")) < 0) |
1194 | 0 | return code; |
1195 | 34 | } |
1196 | 82.8k | (void)pdfi_check_ColorSpace_dict(ctx, (pdf_dict *)d, page_dict, tracker); |
1197 | | |
1198 | 82.8k | pdfi_countdown(d); |
1199 | 82.8k | d = NULL; |
1200 | | |
1201 | | /* Put the Resources dictionary back in cache (or promote it) in case checking ColorSpace dict |
1202 | | * created so many cache entires it flushed Resources from cache |
1203 | | */ |
1204 | 82.8k | code = pdfi_cache_object(ctx, (pdf_obj *)Resources_dict); |
1205 | 82.8k | if (code < 0) { |
1206 | 0 | if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_CACHE_FAIL, "pdfi_check_Resources", "")) < 0) |
1207 | 0 | return code; |
1208 | 0 | } |
1209 | | |
1210 | 82.8k | code = pdfi_dict_knownget_type(ctx, Resources_dict, "Shading", PDF_DICT, &d); |
1211 | 82.8k | if (code < 0 && code != gs_error_undefined) { |
1212 | 12 | if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_RESOURCE, "pdfi_check_Resources", "Shading")) < 0) |
1213 | 0 | return code; |
1214 | 12 | } |
1215 | 82.8k | (void)pdfi_check_Shading_dict(ctx, (pdf_dict *)d, page_dict, tracker); |
1216 | 82.8k | pdfi_countdown(d); |
1217 | 82.8k | d = NULL; |
1218 | | |
1219 | | /* Put the Resources dictionary back in cache (or promote it) in case checking Shading dict |
1220 | | * created so many cache entires it flushed Resources from cache |
1221 | | */ |
1222 | 82.8k | code = pdfi_cache_object(ctx, (pdf_obj *)Resources_dict); |
1223 | 82.8k | if (code < 0) { |
1224 | 0 | if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_CACHE_FAIL, "pdfi_check_Resources", "")) < 0) |
1225 | 0 | return code; |
1226 | 0 | } |
1227 | 82.8k | } |
1228 | | |
1229 | 834k | code = pdfi_dict_knownget_type(ctx, Resources_dict, "XObject", PDF_DICT, &d); |
1230 | 834k | if (code < 0 && code != gs_error_undefined) { |
1231 | 224 | if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_RESOURCE, "pdfi_check_Resources", "XObject")) < 0) |
1232 | 0 | return code; |
1233 | 224 | } |
1234 | 834k | (void)pdfi_check_XObject_dict(ctx, (pdf_dict *)d, page_dict, tracker); |
1235 | 834k | pdfi_countdown(d); |
1236 | 834k | d = NULL; |
1237 | | |
1238 | | /* Put the Resources dictionary back in cache (or promote it) in case checking XObject dict |
1239 | | * created so many cache entires it flushed Resources from cache |
1240 | | */ |
1241 | 834k | code = pdfi_cache_object(ctx, (pdf_obj *)Resources_dict); |
1242 | 834k | if (code < 0) { |
1243 | 0 | if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_CACHE_FAIL, "pdfi_check_Resources", "")) < 0) |
1244 | 0 | return code; |
1245 | 0 | } |
1246 | | |
1247 | 834k | code = pdfi_dict_knownget_type(ctx, Resources_dict, "Pattern", PDF_DICT, &d); |
1248 | 834k | if (code < 0 && code != gs_error_undefined) { |
1249 | 173 | if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_RESOURCE, "pdfi_check_Resources", "Pattern")) < 0) { |
1250 | 0 | return code; |
1251 | 0 | } |
1252 | 173 | } |
1253 | 834k | (void)pdfi_check_Pattern_dict(ctx, (pdf_dict *)d, page_dict, tracker); |
1254 | 834k | pdfi_countdown(d); |
1255 | 834k | d = NULL; |
1256 | | |
1257 | | /* Put the Resources dictionary back in cache (or promote it) in case checking Pattern dict |
1258 | | * created so many cache entires it flushed Resources from cache |
1259 | | */ |
1260 | 834k | code = pdfi_cache_object(ctx, (pdf_obj *)Resources_dict); |
1261 | 834k | if (code < 0) { |
1262 | 0 | if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_CACHE_FAIL, "pdfi_check_Resources", "")) < 0) |
1263 | 0 | return code; |
1264 | 0 | } |
1265 | | |
1266 | 834k | code = pdfi_dict_knownget_type(ctx, Resources_dict, "Font", PDF_DICT, &d); |
1267 | 834k | if (code < 0) { |
1268 | 6.84k | if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_RESOURCE, "pdfi_check_Resources", "Font")) < 0) |
1269 | 0 | return code; |
1270 | 6.84k | } |
1271 | 834k | (void)pdfi_check_Font_dict(ctx, (pdf_dict *)d, page_dict, tracker); |
1272 | | /* From this point onwards, if we detect transparency (or have already detected it) we |
1273 | | * can exit, we have already counted up any spot colours. |
1274 | | */ |
1275 | 834k | pdfi_countdown(d); |
1276 | 834k | d = NULL; |
1277 | | |
1278 | | /* Put the Resources dictionary back in cache (or promote it) in case checking Font dict |
1279 | | * created so many cache entires it flushed Resources from cache |
1280 | | */ |
1281 | 834k | code = pdfi_cache_object(ctx, (pdf_obj *)Resources_dict); |
1282 | 834k | if (code < 0) { |
1283 | 0 | if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_CACHE_FAIL, "pdfi_check_Resources", "")) < 0) |
1284 | 0 | return code; |
1285 | 0 | } |
1286 | | |
1287 | 834k | code = pdfi_dict_knownget_type(ctx, Resources_dict, "ExtGState", PDF_DICT, &d); |
1288 | 834k | if (code < 0 && code != gs_error_undefined) { |
1289 | 101 | if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_RESOURCE, "pdfi_check_Resources", "ExtGState")) < 0) |
1290 | 0 | return code; |
1291 | 101 | } |
1292 | 834k | (void)pdfi_check_ExtGState_dict(ctx, (pdf_dict *)d, page_dict, tracker); |
1293 | 834k | pdfi_countdown(d); |
1294 | 834k | d = NULL; |
1295 | | |
1296 | | /* Put the Resources dictionary back in cache (or promote it) in case checking ExtGState dict |
1297 | | * created so many cache entires it flushed Resources from cache |
1298 | | */ |
1299 | 834k | code = pdfi_cache_object(ctx, (pdf_obj *)Resources_dict); |
1300 | 834k | if (code < 0) { |
1301 | 0 | if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_CACHE_FAIL, "pdfi_check_Resources", "")) < 0) |
1302 | 0 | return code; |
1303 | 0 | } |
1304 | | |
1305 | 834k | return 0; |
1306 | 834k | } |
1307 | | |
1308 | | static int pdfi_check_annot_for_transparency(pdf_context *ctx, pdf_dict *annot, pdf_dict *page_dict, |
1309 | | pdfi_check_tracker_t *tracker) |
1310 | 1.24M | { |
1311 | 1.24M | int code; |
1312 | 1.24M | pdf_name *n; |
1313 | 1.24M | pdf_obj *N = NULL; |
1314 | 1.24M | pdf_dict *ap = NULL; |
1315 | 1.24M | pdf_dict *Resources = NULL; |
1316 | 1.24M | double f; |
1317 | | |
1318 | 1.24M | if (resource_is_checked(tracker, (pdf_obj *)annot)) |
1319 | 104 | return 0; |
1320 | | |
1321 | 1.24M | if (pdfi_type_of(annot) != PDF_DICT) |
1322 | 0 | return_error(gs_error_typecheck); |
1323 | | |
1324 | | /* Check #1 Does the (Normal) Appearnce stream use any Resources which include transparency. |
1325 | | * We check this first, because this also checks for spot colour spaces. Once we've done that we |
1326 | | * can exit the checks as soon as we detect transparency. |
1327 | | */ |
1328 | 1.24M | code = pdfi_dict_knownget_type(ctx, annot, "AP", PDF_DICT, (pdf_obj **)&ap); |
1329 | 1.24M | if (code > 0) |
1330 | 579k | { |
1331 | | /* Fetch without resolving indirect ref because pdfmark wants it that way later */ |
1332 | 579k | code = pdfi_dict_get_no_store_R(ctx, ap, "N", (pdf_obj **)&N); |
1333 | 579k | if (code >= 0) { |
1334 | 452k | pdf_dict *dict = NULL; |
1335 | | |
1336 | 452k | code = pdfi_dict_from_obj(ctx, N, &dict); |
1337 | 452k | if (code == 0) |
1338 | 452k | code = pdfi_dict_knownget_type(ctx, dict, "Resources", PDF_DICT, (pdf_obj **)&Resources); |
1339 | 452k | if (code > 0) |
1340 | 298k | code = pdfi_check_Resources(ctx, (pdf_dict *)Resources, page_dict, tracker); |
1341 | 452k | } |
1342 | 579k | if (code == gs_error_undefined) |
1343 | 118k | code = 0; |
1344 | 579k | } |
1345 | 1.24M | pdfi_countdown(ap); |
1346 | 1.24M | pdfi_countdown(N); |
1347 | 1.24M | pdfi_countdown(Resources); |
1348 | | |
1349 | 1.24M | if (code < 0) |
1350 | 28.2k | return code; |
1351 | | /* We've checked the Resources, and nothing else in an annotation can define spot colours, so |
1352 | | * if we detected transparency in the Resources we need not check further. |
1353 | | */ |
1354 | 1.21M | if (tracker->transparent == true) |
1355 | 20.4k | return 0; |
1356 | | |
1357 | 1.19M | code = pdfi_dict_get_type(ctx, annot, "Subtype", PDF_NAME, (pdf_obj **)&n); |
1358 | 1.19M | if (code < 0) { |
1359 | 1.83k | if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_TYPE, "pdfi_check_annot_for_transparency", "")) < 0) { |
1360 | 0 | return code; |
1361 | 0 | } |
1362 | 1.19M | } else { |
1363 | | /* Check #2, Highlight annotations are always preformed with transparency */ |
1364 | 1.19M | if (pdfi_name_is((const pdf_name *)n, "Highlight")) { |
1365 | 3.37k | pdfi_countdown(n); |
1366 | 3.37k | tracker->transparent = true; |
1367 | 3.37k | return 0; |
1368 | 3.37k | } |
1369 | 1.19M | pdfi_countdown(n); |
1370 | 1.19M | n = NULL; |
1371 | | |
1372 | | /* Check #3 Blend Mode (BM) not being 'Normal' or 'Compatible' */ |
1373 | 1.19M | code = pdfi_dict_knownget_type(ctx, annot, "BM", PDF_NAME, (pdf_obj **)&n); |
1374 | 1.19M | if (code > 0) { |
1375 | 0 | if (!pdfi_name_is((const pdf_name *)n, "Normal")) { |
1376 | 0 | if (!pdfi_name_is((const pdf_name *)n, "Compatible")) { |
1377 | 0 | pdfi_countdown(n); |
1378 | 0 | tracker->transparent = true; |
1379 | 0 | return 0; |
1380 | 0 | } |
1381 | 0 | } |
1382 | 0 | code = 0; |
1383 | 0 | } |
1384 | 1.19M | pdfi_countdown(n); |
1385 | 1.19M | if (code < 0) |
1386 | 0 | return code; |
1387 | | |
1388 | | /* Check #4 stroke constant alpha (CA) is not 1 (100% opaque) */ |
1389 | 1.19M | code = pdfi_dict_knownget_number(ctx, annot, "CA", &f); |
1390 | 1.19M | if (code > 0) { |
1391 | 2.92k | if (f != 1.0) { |
1392 | 272 | tracker->transparent = true; |
1393 | 272 | return 0; |
1394 | 272 | } |
1395 | 2.92k | } |
1396 | 1.19M | if (code < 0) |
1397 | 0 | return code; |
1398 | | |
1399 | | /* Check #5 non-stroke constant alpha (ca) is not 1 (100% opaque) */ |
1400 | 1.19M | code = pdfi_dict_knownget_number(ctx, annot, "ca", &f); |
1401 | 1.19M | if (code > 0) { |
1402 | 0 | if (f != 1.0) { |
1403 | 0 | tracker->transparent = true; |
1404 | 0 | return 0; |
1405 | 0 | } |
1406 | 0 | } |
1407 | 1.19M | if (code < 0) |
1408 | 0 | return code; |
1409 | 1.19M | } |
1410 | | |
1411 | 1.19M | return 0; |
1412 | 1.19M | } |
1413 | | |
1414 | | static int pdfi_check_Annots_for_transparency(pdf_context *ctx, pdf_array *annots_array, |
1415 | | pdf_dict *page_dict, pdfi_check_tracker_t *tracker) |
1416 | 68.2k | { |
1417 | 68.2k | int i, code = 0; |
1418 | 68.2k | pdf_dict *annot = NULL; |
1419 | | |
1420 | 68.2k | if (resource_is_checked(tracker, (pdf_obj *)annots_array)) |
1421 | 0 | return 0; |
1422 | | |
1423 | 68.2k | if (pdfi_type_of(annots_array) != PDF_ARRAY) |
1424 | 0 | return_error(gs_error_typecheck); |
1425 | | |
1426 | 1.48M | for (i=0; i < pdfi_array_size(annots_array); i++) { |
1427 | 1.43M | code = pdfi_array_get_type(ctx, annots_array, (uint64_t)i, PDF_DICT, (pdf_obj **)&annot); |
1428 | 1.43M | if (code >= 0) { |
1429 | 1.24M | code = pdfi_check_annot_for_transparency(ctx, annot, page_dict, tracker); |
1430 | 1.24M | if (code < 0 && (code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_INVALID_TRANS_XOBJECT, "pdfi_check_Annots_for_transparency", "")) < 0) { |
1431 | 0 | goto exit; |
1432 | 0 | } |
1433 | | |
1434 | | /* If we've found transparency, and don't need to continue checkign for spot colours |
1435 | | * just exit as fast as possible. |
1436 | | */ |
1437 | 1.24M | if (tracker->transparent == true && tracker->spot_dict == NULL) |
1438 | 13.5k | goto exit; |
1439 | | |
1440 | 1.23M | pdfi_countdown(annot); |
1441 | 1.23M | annot = NULL; |
1442 | 1.23M | } |
1443 | 185k | else if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_INVALID_TRANS_XOBJECT, "pdfi_check_Annots_for_transparency", "")) < 0) { |
1444 | 0 | goto exit; |
1445 | 0 | } |
1446 | 1.43M | } |
1447 | 68.2k | exit: |
1448 | 68.2k | pdfi_countdown(annot); |
1449 | 68.2k | return code; |
1450 | 68.2k | } |
1451 | | |
1452 | | /* Check for transparency and spots on page. |
1453 | | * |
1454 | | * Sets ctx->spot_capable_device |
1455 | | * Builds a dictionary of the unique spot names in spot_dict |
1456 | | * Set 'transparent' to true if there is transparency on the page |
1457 | | * |
1458 | | * From the original PDF interpreter written in PostScript: |
1459 | | * Note: we deliberately don't check to see whether a Group is defined, |
1460 | | * because Adobe Illustrator 10 (and possibly other applications) define |
1461 | | * a page-level group whether transparency is actually used or not. |
1462 | | * Ignoring the presence of Group is justified because, in the absence |
1463 | | * of any other transparency features, they have no effect. |
1464 | | */ |
1465 | | static int pdfi_check_page_inner(pdf_context *ctx, pdf_dict *page_dict, |
1466 | | pdfi_check_tracker_t *tracker) |
1467 | 268k | { |
1468 | 268k | int code; |
1469 | 268k | pdf_dict *Resources = NULL; |
1470 | 268k | pdf_array *Annots = NULL; |
1471 | 268k | pdf_dict *Group = NULL; |
1472 | 268k | pdf_obj *CS = NULL; |
1473 | | |
1474 | 268k | tracker->transparent = false; |
1475 | | |
1476 | 268k | if (pdfi_type_of(page_dict) != PDF_DICT) |
1477 | 10 | return_error(gs_error_typecheck); |
1478 | | |
1479 | | |
1480 | | /* Check if the page dictionary has a page Group entry (for spots). |
1481 | | * Page group should mean the page has transparency but we ignore it for the purposes |
1482 | | * of transparency detection. See above. |
1483 | | */ |
1484 | 268k | if (tracker->spot_dict) { |
1485 | 29.2k | code = pdfi_dict_knownget_type(ctx, page_dict, "Group", PDF_DICT, (pdf_obj **)&Group); |
1486 | 29.2k | if (code > 0) { |
1487 | | /* If Group has a ColorSpace (CS), then check it for spot colours */ |
1488 | 6.15k | code = pdfi_dict_knownget(ctx, Group, "CS", &CS); |
1489 | 6.15k | if (code > 0) |
1490 | 6.07k | code = pdfi_check_ColorSpace_for_spots(ctx, CS, Group, page_dict, tracker->spot_dict); |
1491 | | |
1492 | 6.15k | if (code < 0) { |
1493 | 66 | if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_GS_LIB_ERROR, "pdfi_check_page_inner", "")) < 0) { |
1494 | 0 | goto exit; |
1495 | 0 | } |
1496 | 66 | } |
1497 | 6.15k | } |
1498 | 29.2k | } |
1499 | | |
1500 | | /* Now check any Resources dictionary in the Page dictionary */ |
1501 | 268k | code = pdfi_dict_knownget_type(ctx, page_dict, "Resources", PDF_DICT, (pdf_obj **)&Resources); |
1502 | 268k | if (code > 0) |
1503 | 254k | code = pdfi_check_Resources(ctx, Resources, page_dict, tracker); |
1504 | | |
1505 | 268k | if (code == gs_error_pdf_stackoverflow || (code < 0 && |
1506 | 10.7k | (code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_GS_LIB_ERROR, "pdfi_check_page_inner", "")) < 0)) { |
1507 | 0 | goto exit; |
1508 | 0 | } |
1509 | | |
1510 | | /* If we are drawing Annotations, check to see if the page uses any Annots */ |
1511 | 268k | if (ctx->args.showannots) { |
1512 | 268k | code = pdfi_dict_knownget_type(ctx, page_dict, "Annots", PDF_ARRAY, (pdf_obj **)&Annots); |
1513 | 268k | if (code > 0) |
1514 | 68.2k | code = pdfi_check_Annots_for_transparency(ctx, Annots, page_dict, |
1515 | 68.2k | tracker); |
1516 | 268k | if (code < 0) { |
1517 | 2.94k | if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_GS_LIB_ERROR, "pdfi_check_page_inner", "")) < 0) { |
1518 | 0 | goto exit; |
1519 | 0 | } |
1520 | 2.94k | } |
1521 | 268k | } |
1522 | | |
1523 | 268k | code = 0; |
1524 | 268k | exit: |
1525 | 268k | pdfi_countdown(Resources); |
1526 | 268k | pdfi_countdown(Annots); |
1527 | 268k | pdfi_countdown(CS); |
1528 | 268k | pdfi_countdown(Group); |
1529 | 268k | return code; |
1530 | 268k | } |
1531 | | |
1532 | | /* Checks page for transparency, and sets up device for spots, if applicable |
1533 | | * Sets ctx->page.has_transparency and ctx->page.num_spots |
1534 | | * do_setup -- indicates whether to actually set up the device with the spot count. |
1535 | | */ |
1536 | | int pdfi_check_page(pdf_context *ctx, pdf_dict *page_dict, pdf_array **fonts_array, pdf_array **spots_array, bool do_setup) |
1537 | 268k | { |
1538 | 268k | int code; |
1539 | 268k | int spots = 0; |
1540 | 268k | pdfi_check_tracker_t tracker; |
1541 | 268k | pdf_dict *Resources = NULL; |
1542 | | |
1543 | 268k | ctx->page.num_spots = 0; |
1544 | 268k | ctx->page.has_transparency = false; |
1545 | | |
1546 | 268k | code = pdfi_check_init_tracker(ctx, &tracker, fonts_array, spots_array); |
1547 | 268k | if (code < 0) |
1548 | 0 | goto exit; |
1549 | | |
1550 | | /* Check for spots and transparency in this page */ |
1551 | 268k | code = pdfi_check_page_inner(ctx, page_dict, &tracker); |
1552 | 268k | if (code < 0) |
1553 | 10 | goto exit; |
1554 | | |
1555 | | /* Count the spots */ |
1556 | 268k | if (tracker.spot_dict) |
1557 | 29.2k | spots = pdfi_dict_entries(tracker.spot_dict); |
1558 | | |
1559 | | /* If setup requested, tell the device about spots and transparency */ |
1560 | 268k | if (do_setup) { |
1561 | 0 | gs_c_param_list list; |
1562 | 0 | int a = 0; |
1563 | 0 | pdf_name *Key = NULL; |
1564 | 0 | pdf_obj *Value = NULL; |
1565 | 0 | uint64_t index = 0; |
1566 | |
|
1567 | 0 | gs_c_param_list_write(&list, ctx->memory); |
1568 | | |
1569 | | /* If there are spot colours (and by inference, the device renders spot plates) then |
1570 | | * send the number of Spots to the device, so it can setup correctly. |
1571 | | */ |
1572 | 0 | if (tracker.spot_dict) { |
1573 | | /* There is some awkwardness here. If the SeparationColorNames setting |
1574 | | * fails, we want to ignore it (this can mean that we exceeded the maximum |
1575 | | * number of colourants and some will be converted to CMYK). But if that happens, |
1576 | | * any other parameters in the same list which haven't already been prcoessed |
1577 | | * will be lost. So we need to send two lists, the SeparationColorNames and |
1578 | | * 'everything else'. |
1579 | | */ |
1580 | 0 | if (spots > 0) { |
1581 | 0 | gs_param_string_array sa; |
1582 | 0 | gs_param_string *table = NULL; |
1583 | |
|
1584 | 0 | table = (gs_param_string *)gs_alloc_byte_array(ctx->memory, spots, sizeof(gs_param_string), "SeparationNames"); |
1585 | 0 | if (table != NULL) |
1586 | 0 | { |
1587 | 0 | memset(table, 0x00, spots * sizeof(gs_param_string)); |
1588 | |
|
1589 | 0 | code = pdfi_dict_first(ctx, tracker.spot_dict, (pdf_obj **)&Key, &Value, &index); |
1590 | 0 | while (code >= 0) |
1591 | 0 | { |
1592 | 0 | if (pdfi_type_of(Key) == PDF_NAME) { |
1593 | 0 | table[a].data = ((pdf_string *)Key)->data; |
1594 | 0 | table[a].size = ((pdf_string *)Key)->length; |
1595 | 0 | table[a++].persistent = false; |
1596 | 0 | } |
1597 | | /* Although we count down the returned PDF objects here, the pointers |
1598 | | * to the name data remain valid and won't move. Provided we don't |
1599 | | * retain the pointers after we free the tracker dictionary this is |
1600 | | * safe to do. |
1601 | | */ |
1602 | 0 | pdfi_countdown(Key); |
1603 | 0 | Key = NULL; |
1604 | 0 | pdfi_countdown(Value); |
1605 | 0 | Value = NULL; |
1606 | 0 | code = pdfi_dict_next(ctx, tracker.spot_dict, (pdf_obj **)&Key, &Value, &index); |
1607 | 0 | } |
1608 | 0 | sa.data = table; |
1609 | 0 | sa.size = spots; |
1610 | 0 | sa.persistent = false; |
1611 | |
|
1612 | 0 | (void)param_write_string_array((gs_param_list *)&list, "SeparationColorNames", &sa); |
1613 | 0 | gs_c_param_list_read(&list); |
1614 | 0 | code = gs_putdeviceparams(ctx->pgs->device, (gs_param_list *)&list); |
1615 | 0 | gs_c_param_list_release(&list); |
1616 | |
|
1617 | 0 | gs_free_object(ctx->memory, table, "SeparationNames"); |
1618 | 0 | if (code > 0) { |
1619 | | /* The device was closed, we need to reopen it */ |
1620 | 0 | code = gs_setdevice_no_erase(ctx->pgs, ctx->pgs->device); |
1621 | 0 | if (code < 0) |
1622 | 0 | goto exit; |
1623 | 0 | gs_erasepage(ctx->pgs); |
1624 | 0 | } |
1625 | | |
1626 | | /* Reset the list back to being writeable */ |
1627 | 0 | gs_c_param_list_write(&list, ctx->memory); |
1628 | 0 | } |
1629 | 0 | else { |
1630 | 0 | code = gs_note_error(gs_error_VMerror); |
1631 | 0 | goto exit; |
1632 | 0 | } |
1633 | 0 | } |
1634 | | /* Update the number of spots */ |
1635 | 0 | param_write_int((gs_param_list *)&list, "PageSpotColors", &spots); |
1636 | 0 | } |
1637 | | /* Update the page transparency */ |
1638 | 0 | (void)param_write_bool((gs_param_list *)&list, "PageUsesTransparency", |
1639 | 0 | &tracker.transparent); |
1640 | 0 | gs_c_param_list_read(&list); |
1641 | 0 | code = gs_putdeviceparams(ctx->pgs->device, (gs_param_list *)&list); |
1642 | 0 | gs_c_param_list_release(&list); |
1643 | |
|
1644 | 0 | if (code > 0) { |
1645 | | /* The device was closed, we need to reopen it */ |
1646 | 0 | code = gs_setdevice_no_erase(ctx->pgs, ctx->pgs->device); |
1647 | 0 | if (code < 0) |
1648 | 0 | goto exit; |
1649 | 0 | gs_erasepage(ctx->pgs); |
1650 | 0 | } |
1651 | 0 | } |
1652 | | |
1653 | | /* Set our values in the context, for caller */ |
1654 | 268k | if (!ctx->args.notransparency) |
1655 | 268k | ctx->page.has_transparency = tracker.transparent; |
1656 | 268k | ctx->page.num_spots = spots; |
1657 | 268k | ctx->page.has_OP = tracker.has_overprint; |
1658 | | |
1659 | | /* High level devices do not render overprint */ |
1660 | 268k | if (ctx->device_state.HighLevelDevice) |
1661 | 128k | ctx->page.has_OP = false; |
1662 | | |
1663 | 268k | exit: |
1664 | 268k | if (fonts_array != NULL) { |
1665 | 0 | *fonts_array = tracker.font_array; |
1666 | 0 | pdfi_countup(*fonts_array); |
1667 | 0 | } |
1668 | | |
1669 | 268k | if (spots_array != NULL && tracker.spot_dict != NULL && pdfi_dict_entries(tracker.spot_dict) != 0) { |
1670 | 0 | pdf_array *new_array = NULL; |
1671 | 0 | pdf_name *Key = NULL; |
1672 | 0 | pdf_obj *Value = NULL; |
1673 | 0 | uint64_t index = 0, a_index = 0; |
1674 | |
|
1675 | 0 | index = pdfi_dict_entries(tracker.spot_dict); |
1676 | |
|
1677 | 0 | code = pdfi_array_alloc(ctx, index, &new_array); |
1678 | 0 | if (code < 0) |
1679 | 0 | goto error; |
1680 | 0 | pdfi_countup(new_array); |
1681 | |
|
1682 | 0 | code = pdfi_dict_first(ctx, tracker.spot_dict, (pdf_obj **)&Key, &Value, &index); |
1683 | 0 | while (code >= 0) |
1684 | 0 | { |
1685 | 0 | if (pdfi_type_of(Key) == PDF_NAME) { |
1686 | 0 | code = pdfi_array_put(ctx, new_array, a_index++, (pdf_obj *)Key); |
1687 | 0 | if (code < 0) { |
1688 | 0 | pdfi_countdown(new_array); |
1689 | 0 | pdfi_countdown(Key); |
1690 | 0 | pdfi_countdown(Value); |
1691 | 0 | goto error; |
1692 | 0 | } |
1693 | 0 | } |
1694 | | |
1695 | 0 | pdfi_countdown(Key); |
1696 | 0 | Key = NULL; |
1697 | 0 | pdfi_countdown(Value); |
1698 | 0 | Value = NULL; |
1699 | 0 | code = pdfi_dict_next(ctx, tracker.spot_dict, (pdf_obj **)&Key, &Value, &index); |
1700 | 0 | } |
1701 | 0 | *spots_array = new_array; |
1702 | 0 | } |
1703 | | |
1704 | | /* Put the Resources dictionary back in cache (or promote it) in case checking the page |
1705 | | * created so many cache entires it flushed Resources from cache |
1706 | | */ |
1707 | 268k | code = pdfi_dict_knownget_type(ctx, page_dict, "Resources", PDF_DICT, (pdf_obj **)&Resources); |
1708 | 268k | if (code > 0) { |
1709 | 254k | code = pdfi_cache_object(ctx, (pdf_obj *)Resources); |
1710 | 254k | if (code < 0) |
1711 | 0 | code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_CACHE_FAIL, "pdfi_check_Resources", ""); |
1712 | 254k | pdfi_countdown(Resources); |
1713 | 254k | } |
1714 | 14.4k | else { |
1715 | 14.4k | if (code < 0) { |
1716 | 10.7k | if (code == gs_error_undefined) |
1717 | 3.81k | code = 0; |
1718 | 6.98k | else |
1719 | 6.98k | code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_PAGE_RESOURCES, "pdfi_check_page", NULL); |
1720 | 10.7k | } |
1721 | 14.4k | } |
1722 | | |
1723 | 268k | error: |
1724 | 268k | (void)pdfi_check_free_tracker(ctx, &tracker); |
1725 | 268k | return code; |
1726 | 268k | } |