| 1 |
/* Copyright (C) 2019-2026 Artifex Software, Inc. |
| 2 |
All Rights Reserved. |
| 3 |
|
| 4 |
This software is provided AS-IS with no warranty, either express or |
| 5 |
implied. |
| 6 |
|
| 7 |
This software is distributed under license and may not be copied, |
| 8 |
modified or distributed except as expressly authorized under the terms |
| 9 |
of the license contained in the file LICENSE in this distribution. |
| 10 |
|
| 11 |
Refer to licensing information at http://www.artifex.com or contact |
| 12 |
Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, |
| 13 |
CA 94129, USA, for further information. |
| 14 |
*/ |
| 15 |
|
| 16 |
/* 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 |
{ |
| 88 |
uint32_t byte_offset; |
| 89 |
byte bit_offset; |
| 90 |
int object_num; |
| 91 |
|
| 92 |
if(tracker->CheckedResources == NULL) |
| 93 |
return 0; |
| 94 |
|
| 95 |
/* objects with object number 0 are directly defined, we can't |
| 96 |
* store those so just return immediately |
| 97 |
*/ |
| 98 |
object_num = pdf_object_num(o); |
| 99 |
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 |
bit_offset = 0x01 << (object_num % 8); |
| 106 |
byte_offset = object_num >> 3; |
| 107 |
|
| 108 |
/* If its already set, then return that. */ |
| 109 |
if (tracker->CheckedResources[byte_offset] & bit_offset) |
| 110 |
return true; |
| 111 |
else |
| 112 |
/* Otherwise set it for futre reference */ |
| 113 |
tracker->CheckedResources[byte_offset] |= bit_offset; |
| 114 |
} |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
static int |
| 120 |
pdfi_check_free_tracker(pdf_context *ctx, pdfi_check_tracker_t *tracker) |
| 121 |
{ |
| 122 |
gs_free_object(ctx->memory, tracker->CheckedResources, "pdfi_check_free_tracker(flags)"); |
| 123 |
pdfi_countdown(tracker->spot_dict); |
| 124 |
pdfi_countdown(tracker->font_array); |
| 125 |
memset(tracker, 0, sizeof(*tracker)); |
| 126 |
return 0; |
| 127 |
} |
| 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 |
{ |
| 132 |
int code = 0; |
| 133 |
|
| 134 |
memset(tracker, 0, sizeof(*tracker)); |
| 135 |
|
| 136 |
tracker->size = (ctx->xref_table->xref_size + 7) / 8; |
| 137 |
tracker->CheckedResources = gs_alloc_bytes(ctx->memory, tracker->size, |
| 138 |
"pdfi_check_init_tracker(flags)"); |
| 139 |
if (tracker->CheckedResources == NULL) |
| 140 |
return_error(gs_error_VMerror); |
| 141 |
|
| 142 |
memset(tracker->CheckedResources, 0x00, tracker->size); |
| 143 |
|
| 144 |
if (ctx->device_state.spot_capable || |
| 145 |
(ctx->pgs->device->icc_struct->overprint_control) == gs_overprint_control_simulate || |
| 146 |
spot_array != NULL) |
| 147 |
{ |
| 148 |
code = pdfi_dict_alloc(ctx, 32, &tracker->spot_dict); |
| 149 |
if (code < 0) |
| 150 |
goto cleanup; |
| 151 |
pdfi_countup(tracker->spot_dict); |
| 152 |
} |
| 153 |
|
| 154 |
if (fonts_array != NULL) { |
| 155 |
code = pdfi_array_alloc(ctx, 0, &tracker->font_array); |
| 156 |
if (code < 0) |
| 157 |
goto cleanup; |
| 158 |
pdfi_countup(tracker->font_array); |
| 159 |
} |
| 160 |
|
| 161 |
return 0; |
| 162 |
|
| 163 |
cleanup: |
| 164 |
pdfi_check_free_tracker(ctx, tracker); |
| 165 |
return code; |
| 166 |
} |
| 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 |
{ |
| 175 |
int code; |
| 176 |
uint64_t i, index; |
| 177 |
pdf_obj *Key = NULL, *Value = NULL; |
| 178 |
|
| 179 |
if (resource_is_checked(tracker, (pdf_obj *)cspace_dict)) |
| 180 |
return 0; |
| 181 |
|
| 182 |
if (pdfi_type_of(cspace_dict) != PDF_DICT) |
| 183 |
return_error(gs_error_typecheck); |
| 184 |
|
| 185 |
if (pdfi_dict_entries(cspace_dict) > 0) { |
| 186 |
code = pdfi_loop_detector_mark(ctx); /* Mark the start of the ColorSpace dictionary loop */ |
| 187 |
if (code < 0) |
| 188 |
return code; |
| 189 |
|
| 190 |
code = pdfi_dict_first(ctx, cspace_dict, &Key, &Value, &index); |
| 191 |
if (code < 0) |
| 192 |
goto error1; |
| 193 |
|
| 194 |
i = 1; |
| 195 |
do { |
| 196 |
code = pdfi_check_ColorSpace_for_spots(ctx, Value, cspace_dict, page_dict, tracker->spot_dict); |
| 197 |
if (code < 0) |
| 198 |
goto error2; |
| 199 |
|
| 200 |
pdfi_countdown(Key); |
| 201 |
Key = NULL; |
| 202 |
pdfi_countdown(Value); |
| 203 |
Value = NULL; |
| 204 |
|
| 205 |
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the Shading dictionary loop */ |
| 206 |
|
| 207 |
code = pdfi_loop_detector_mark(ctx); /* Mark the new start of the Shading dictionary loop */ |
| 208 |
if (code < 0) |
| 209 |
goto error1; |
| 210 |
|
| 211 |
do { |
| 212 |
if (i++ >= pdfi_dict_entries(cspace_dict)) { |
| 213 |
code = 0; |
| 214 |
goto transparency_exit; |
| 215 |
} |
| 216 |
|
| 217 |
code = pdfi_dict_next(ctx, cspace_dict, &Key, &Value, &index); |
| 218 |
if (code == 0 && pdfi_type_of(Value) == PDF_ARRAY) |
| 219 |
break; |
| 220 |
pdfi_countdown(Key); |
| 221 |
Key = NULL; |
| 222 |
pdfi_countdown(Value); |
| 223 |
Value = NULL; |
| 224 |
} while(1); |
| 225 |
}while (1); |
| 226 |
} |
| 227 |
return 0; |
| 228 |
|
| 229 |
transparency_exit: |
| 230 |
error2: |
| 231 |
pdfi_countdown(Key); |
| 232 |
pdfi_countdown(Value); |
| 233 |
|
| 234 |
error1: |
| 235 |
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */ |
| 236 |
return code; |
| 237 |
} |
| 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 |
{ |
| 245 |
int code; |
| 246 |
pdf_obj *o = NULL; |
| 247 |
pdf_dict *shading_dict = NULL; |
| 248 |
|
| 249 |
if (resource_is_checked(tracker, shading)) |
| 250 |
return 0; |
| 251 |
|
| 252 |
code = pdfi_dict_from_obj(ctx, shading, &shading_dict); |
| 253 |
if (code < 0) |
| 254 |
return code; |
| 255 |
|
| 256 |
if (pdfi_type_of(shading_dict) != PDF_DICT) |
| 257 |
return_error(gs_error_typecheck); |
| 258 |
|
| 259 |
code = pdfi_dict_knownget(ctx, shading_dict, "ColorSpace", (pdf_obj **)&o); |
| 260 |
if (code > 0) { |
| 261 |
code = pdfi_check_ColorSpace_for_spots(ctx, o, shading_dict, page_dict, tracker->spot_dict); |
| 262 |
pdfi_countdown(o); |
| 263 |
return code; |
| 264 |
} |
| 265 |
return 0; |
| 266 |
} |
| 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 |
{ |
| 274 |
int code; |
| 275 |
uint64_t i, index; |
| 276 |
pdf_obj *Key = NULL, *Value = NULL; |
| 277 |
|
| 278 |
if (resource_is_checked(tracker, (pdf_obj *)shading_dict)) |
| 279 |
return 0; |
| 280 |
|
| 281 |
if (pdfi_type_of(shading_dict) != PDF_DICT) |
| 282 |
return_error(gs_error_typecheck); |
| 283 |
|
| 284 |
if (pdfi_dict_entries(shading_dict) > 0) { |
| 285 |
code = pdfi_loop_detector_mark(ctx); /* Mark the start of the Shading dictionary loop */ |
| 286 |
if (code < 0) |
| 287 |
return code; |
| 288 |
|
| 289 |
code = pdfi_dict_first(ctx, shading_dict, &Key, &Value, &index); |
| 290 |
if (code < 0) |
| 291 |
goto error2; |
| 292 |
|
| 293 |
i = 1; |
| 294 |
do { |
| 295 |
if ((pdfi_type_of(Value) == PDF_DICT || pdfi_type_of(Value) == PDF_STREAM)) { |
| 296 |
code = pdfi_check_Shading(ctx, Value, page_dict, tracker); |
| 297 |
if (code < 0) |
| 298 |
goto error2; |
| 299 |
} |
| 300 |
|
| 301 |
pdfi_countdown(Key); |
| 302 |
Key = NULL; |
| 303 |
pdfi_countdown(Value); |
| 304 |
Value = NULL; |
| 305 |
|
| 306 |
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the Shading dictionary loop */ |
| 307 |
|
| 308 |
code = pdfi_loop_detector_mark(ctx); /* Mark the new start of the Shading dictionary loop */ |
| 309 |
if (code < 0) |
| 310 |
goto error1; |
| 311 |
|
| 312 |
do { |
| 313 |
if (i++ >= pdfi_dict_entries(shading_dict)) { |
| 314 |
code = 0; |
| 315 |
goto transparency_exit; |
| 316 |
} |
| 317 |
|
| 318 |
code = pdfi_dict_next(ctx, shading_dict, &Key, &Value, &index); |
| 319 |
if (code == 0 && pdfi_type_of(Value) == PDF_DICT) |
| 320 |
break; |
| 321 |
pdfi_countdown(Key); |
| 322 |
Key = NULL; |
| 323 |
pdfi_countdown(Value); |
| 324 |
Value = NULL; |
| 325 |
} while(1); |
| 326 |
}while (1); |
| 327 |
} |
| 328 |
return 0; |
| 329 |
|
| 330 |
transparency_exit: |
| 331 |
error2: |
| 332 |
pdfi_countdown(Key); |
| 333 |
pdfi_countdown(Value); |
| 334 |
|
| 335 |
error1: |
| 336 |
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */ |
| 337 |
return code; |
| 338 |
} |
| 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 |
{ |
| 347 |
int code = 0; |
| 348 |
pdf_name *n = NULL; |
| 349 |
bool known = false; |
| 350 |
double f; |
| 351 |
|
| 352 |
if (resource_is_checked(tracker, (pdf_obj *)xobject)) |
| 353 |
return 0; |
| 354 |
|
| 355 |
if (pdfi_type_of(xobject) != PDF_DICT) |
| 356 |
return_error(gs_error_typecheck); |
| 357 |
|
| 358 |
code = pdfi_dict_get_type(ctx, xobject, "Subtype", PDF_NAME, (pdf_obj **)&n); |
| 359 |
if (code >= 0) { |
| 360 |
if (pdfi_name_is((const pdf_name *)n, "Image")) { |
| 361 |
pdf_obj *CS = NULL; |
| 362 |
|
| 363 |
pdfi_countdown(n); |
| 364 |
n = NULL; |
| 365 |
code = pdfi_dict_known(ctx, xobject, "SMask", &known); |
| 366 |
if (code >= 0) { |
| 367 |
if (known == true) { |
| 368 |
tracker->transparent = true; |
| 369 |
if (tracker->spot_dict == NULL) |
| 370 |
goto transparency_exit; |
| 371 |
} |
| 372 |
code = pdfi_dict_knownget_number(ctx, xobject, "SMaskInData", &f); |
| 373 |
if (code > 0) { |
| 374 |
code = 0; |
| 375 |
if (f != 0.0) |
| 376 |
tracker->transparent = true; |
| 377 |
if (tracker->spot_dict == NULL) |
| 378 |
goto transparency_exit; |
| 379 |
} |
| 380 |
/* Check the image dictionary for a ColorSpace entry, if we are checking spot names */ |
| 381 |
if (tracker->spot_dict) { |
| 382 |
code = pdfi_dict_knownget(ctx, xobject, "ColorSpace", (pdf_obj **)&CS); |
| 383 |
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 |
(void)pdfi_check_ColorSpace_for_spots(ctx, CS, xobject, page_dict, tracker->spot_dict); |
| 386 |
pdfi_countdown(CS); |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
} else { |
| 391 |
if (pdfi_name_is((const pdf_name *)n, "Form")) { |
| 392 |
pdf_dict *group_dict = NULL, *resource_dict = NULL; |
| 393 |
pdf_obj *CS = NULL; |
| 394 |
|
| 395 |
pdfi_countdown(n); |
| 396 |
code = pdfi_dict_knownget_type(ctx, xobject, "Group", PDF_DICT, (pdf_obj **)&group_dict); |
| 397 |
if (code > 0) { |
| 398 |
tracker->transparent = true; |
| 399 |
if (tracker->spot_dict != NULL) { |
| 400 |
/* Start a new loop detector group to avoid this being detected in the Resources check below */ |
| 401 |
code = pdfi_loop_detector_mark(ctx); /* Mark the start of the XObject dictionary loop */ |
| 402 |
if (code == 0) { |
| 403 |
code = pdfi_dict_knownget(ctx, group_dict, "CS", &CS); |
| 404 |
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 |
(void)pdfi_check_ColorSpace_for_spots(ctx, CS, group_dict, page_dict, tracker->spot_dict); |
| 407 |
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the XObject dictionary loop */ |
| 408 |
} |
| 409 |
pdfi_countdown(group_dict); |
| 410 |
pdfi_countdown(CS); |
| 411 |
} |
| 412 |
else if (tracker->BM_Not_Normal) |
| 413 |
{ |
| 414 |
/* We already know it has a non-normal Blend Mode. No point in keeping searching. */ |
| 415 |
pdfi_countdown(group_dict); |
| 416 |
goto transparency_exit; |
| 417 |
} |
| 418 |
else |
| 419 |
{ |
| 420 |
pdfi_countdown(group_dict); |
| 421 |
} |
| 422 |
/* We need to keep checking Resources in case there are non-Normal blend mode things still to be found. */ |
| 423 |
} |
| 424 |
|
| 425 |
code = pdfi_dict_knownget_type(ctx, xobject, "Resources", PDF_DICT, (pdf_obj **)&resource_dict); |
| 426 |
if (code > 0) { |
| 427 |
if (ctx->loop_detection && pdf_object_num((pdf_obj *)resource_dict) != 0) { |
| 428 |
code = pdfi_loop_detector_add_object(ctx, resource_dict->object_num); |
| 429 |
if (code < 0) { |
| 430 |
pdfi_countdown(resource_dict); |
| 431 |
goto transparency_exit; |
| 432 |
} |
| 433 |
} |
| 434 |
code = pdfi_check_Resources(ctx, resource_dict, page_dict, tracker); |
| 435 |
pdfi_countdown(resource_dict); |
| 436 |
if (code < 0) |
| 437 |
goto transparency_exit; |
| 438 |
} |
| 439 |
} else |
| 440 |
pdfi_countdown(n); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
return 0; |
| 445 |
|
| 446 |
transparency_exit: |
| 447 |
return code; |
| 448 |
} |
| 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 |
{ |
| 456 |
int code; |
| 457 |
uint64_t i, index; |
| 458 |
pdf_obj *Key = NULL, *Value = NULL; |
| 459 |
pdf_dict *Value_dict = NULL; |
| 460 |
|
| 461 |
if (resource_is_checked(tracker, (pdf_obj *)xobject_dict)) |
| 462 |
return 0; |
| 463 |
|
| 464 |
if (pdfi_type_of(xobject_dict) != PDF_DICT) |
| 465 |
return_error(gs_error_typecheck); |
| 466 |
|
| 467 |
if (pdfi_dict_entries(xobject_dict) > 0) { |
| 468 |
code = pdfi_loop_detector_mark(ctx); /* Mark the start of the XObject dictionary loop */ |
| 469 |
if (code < 0) |
| 470 |
return code; |
| 471 |
|
| 472 |
code = pdfi_dict_first(ctx, xobject_dict, &Key, &Value, &index); |
| 473 |
if (code < 0) |
| 474 |
goto error_exit; |
| 475 |
|
| 476 |
i = 1; |
| 477 |
do { |
| 478 |
if (pdfi_type_of(Value) == PDF_STREAM) { |
| 479 |
code = pdfi_dict_from_obj(ctx, Value, &Value_dict); |
| 480 |
if (code < 0) |
| 481 |
goto error_exit; |
| 482 |
|
| 483 |
code = pdfi_check_XObject(ctx, Value_dict, page_dict, tracker); |
| 484 |
if (code < 0) |
| 485 |
goto error_exit; |
| 486 |
} |
| 487 |
|
| 488 |
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the XObject dictionary loop */ |
| 489 |
|
| 490 |
code = pdfi_loop_detector_mark(ctx); /* Mark the new start of the XObject dictionary loop */ |
| 491 |
if (code < 0) |
| 492 |
goto error_exit; |
| 493 |
|
| 494 |
pdfi_countdown(Key); |
| 495 |
Key = NULL; |
| 496 |
pdfi_countdown(Value); |
| 497 |
Value = NULL; |
| 498 |
Value_dict = NULL; |
| 499 |
|
| 500 |
do { |
| 501 |
if (i++ >= pdfi_dict_entries(xobject_dict)) { |
| 502 |
code = 0; |
| 503 |
goto transparency_exit; |
| 504 |
} |
| 505 |
|
| 506 |
code = pdfi_dict_next(ctx, xobject_dict, &Key, &Value, &index); |
| 507 |
if (code == 0 && pdfi_type_of(Value) == PDF_STREAM) |
| 508 |
break; |
| 509 |
pdfi_countdown(Key); |
| 510 |
Key = NULL; |
| 511 |
pdfi_countdown(Value); |
| 512 |
Value = NULL; |
| 513 |
} while(1); |
| 514 |
}while(1); |
| 515 |
} |
| 516 |
return 0; |
| 517 |
|
| 518 |
transparency_exit: |
| 519 |
error_exit: |
| 520 |
pdfi_countdown(Key); |
| 521 |
pdfi_countdown(Value); |
| 522 |
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */ |
| 523 |
return code; |
| 524 |
} |
| 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 |
{ |
| 532 |
int code; |
| 533 |
pdf_obj *o = NULL; |
| 534 |
double f; |
| 535 |
bool overprint; |
| 536 |
|
| 537 |
if (resource_is_checked(tracker, (pdf_obj *)extgstate_dict)) |
| 538 |
return 0; |
| 539 |
|
| 540 |
if (pdfi_type_of(extgstate_dict) != PDF_DICT) |
| 541 |
return_error(gs_error_typecheck); |
| 542 |
|
| 543 |
if (pdfi_dict_entries(extgstate_dict) > 0) { |
| 544 |
/* See if /OP or /op is true */ |
| 545 |
code = pdfi_dict_get_bool(ctx, extgstate_dict, "OP", &overprint); |
| 546 |
if (code == 0 && overprint) |
| 547 |
tracker->has_overprint = true; |
| 548 |
code = pdfi_dict_get_bool(ctx, extgstate_dict, "op", &overprint); |
| 549 |
if (code == 0 && overprint) |
| 550 |
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 |
code = pdfi_dict_knownget_type(ctx, extgstate_dict, "BM", PDF_NAME, &o); |
| 560 |
if (code > 0) { |
| 561 |
if (!pdfi_name_is((pdf_name *)o, "Normal")) { |
| 562 |
if (!pdfi_name_is((pdf_name *)o, "Compatible")) { |
| 563 |
pdfi_countdown(o); |
| 564 |
tracker->transparent = true; |
| 565 |
tracker->BM_Not_Normal = true; |
| 566 |
return 0; |
| 567 |
} |
| 568 |
} |
| 569 |
} |
| 570 |
pdfi_countdown(o); |
| 571 |
o = NULL; |
| 572 |
|
| 573 |
/* Check SMask */ |
| 574 |
code = pdfi_dict_knownget(ctx, extgstate_dict, "SMask", &o); |
| 575 |
if (code > 0) { |
| 576 |
switch (pdfi_type_of(o)) { |
| 577 |
case PDF_NAME: |
| 578 |
if (!pdfi_name_is((pdf_name *)o, "None")) { |
| 579 |
pdfi_countdown(o); |
| 580 |
tracker->transparent = true; |
| 581 |
return 0; |
| 582 |
} |
| 583 |
break; |
| 584 |
case PDF_DICT: |
| 585 |
{ |
| 586 |
pdf_obj *G = NULL; |
| 587 |
|
| 588 |
tracker->transparent = true; |
| 589 |
|
| 590 |
if (tracker->spot_dict != NULL) { |
| 591 |
/* Check if the SMask has a /G (Group) */ |
| 592 |
code = pdfi_dict_knownget(ctx, (pdf_dict *)o, "G", &G); |
| 593 |
if (code > 0) { |
| 594 |
code = pdfi_check_XObject(ctx, (pdf_dict *)G, page_dict, |
| 595 |
tracker); |
| 596 |
pdfi_countdown(G); |
| 597 |
} |
| 598 |
} |
| 599 |
pdfi_countdown(o); |
| 600 |
return code; |
| 601 |
} |
| 602 |
default: |
| 603 |
break; |
| 604 |
} |
| 605 |
} |
| 606 |
pdfi_countdown(o); |
| 607 |
o = NULL; |
| 608 |
|
| 609 |
code = pdfi_dict_knownget_number(ctx, extgstate_dict, "CA", &f); |
| 610 |
if (code > 0) { |
| 611 |
if (f != 1.0) { |
| 612 |
tracker->transparent = true; |
| 613 |
return 0; |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
code = pdfi_dict_knownget_number(ctx, extgstate_dict, "ca", &f); |
| 618 |
if (code > 0) { |
| 619 |
if (f != 1.0) { |
| 620 |
tracker->transparent = true; |
| 621 |
return 0; |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
} |
| 626 |
return 0; |
| 627 |
} |
| 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 |
{ |
| 635 |
int code; |
| 636 |
uint64_t i, index; |
| 637 |
pdf_obj *Key = NULL, *Value = NULL; |
| 638 |
|
| 639 |
if (resource_is_checked(tracker, (pdf_obj *)extgstate_dict)) |
| 640 |
return 0; |
| 641 |
|
| 642 |
if (pdfi_type_of(extgstate_dict) != PDF_DICT) |
| 643 |
return_error(gs_error_typecheck); |
| 644 |
|
| 645 |
if (pdfi_dict_entries(extgstate_dict) > 0) { |
| 646 |
code = pdfi_loop_detector_mark(ctx); /* Mark the start of the ColorSpace dictionary loop */ |
| 647 |
if (code < 0) |
| 648 |
return code; |
| 649 |
|
| 650 |
code = pdfi_dict_first(ctx, extgstate_dict, &Key, &Value, &index); |
| 651 |
if (code < 0) |
| 652 |
goto error1; |
| 653 |
|
| 654 |
i = 1; |
| 655 |
do { |
| 656 |
|
| 657 |
(void)pdfi_check_ExtGState(ctx, (pdf_dict *)Value, page_dict, tracker); |
| 658 |
if (tracker->transparent == true && tracker->spot_dict == NULL) |
| 659 |
goto transparency_exit; |
| 660 |
|
| 661 |
pdfi_countdown(Key); |
| 662 |
Key = NULL; |
| 663 |
pdfi_countdown(Value); |
| 664 |
Value = NULL; |
| 665 |
|
| 666 |
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the ExtGState dictionary loop */ |
| 667 |
|
| 668 |
code = pdfi_loop_detector_mark(ctx); /* Mark the new start of the ExtGState dictionary loop */ |
| 669 |
if (code < 0) |
| 670 |
goto error1; |
| 671 |
|
| 672 |
do { |
| 673 |
if (i++ >= pdfi_dict_entries(extgstate_dict)) { |
| 674 |
code = 0; |
| 675 |
goto transparency_exit; |
| 676 |
} |
| 677 |
|
| 678 |
code = pdfi_dict_next(ctx, extgstate_dict, &Key, &Value, &index); |
| 679 |
if (code == 0 && pdfi_type_of(Value) == PDF_DICT) |
| 680 |
break; |
| 681 |
pdfi_countdown(Key); |
| 682 |
Key = NULL; |
| 683 |
pdfi_countdown(Value); |
| 684 |
Value = NULL; |
| 685 |
} while(1); |
| 686 |
}while (1); |
| 687 |
} |
| 688 |
return 0; |
| 689 |
|
| 690 |
transparency_exit: |
| 691 |
pdfi_countdown(Key); |
| 692 |
pdfi_countdown(Value); |
| 693 |
|
| 694 |
error1: |
| 695 |
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */ |
| 696 |
return code; |
| 697 |
} |
| 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 |
{ |
| 706 |
int code = 0; |
| 707 |
pdf_obj *o = NULL; |
| 708 |
|
| 709 |
if (resource_is_checked(tracker, (pdf_obj *)pattern)) |
| 710 |
return 0; |
| 711 |
|
| 712 |
if (pdfi_type_of(pattern) != PDF_DICT) |
| 713 |
return_error(gs_error_typecheck); |
| 714 |
|
| 715 |
if (tracker->spot_dict != NULL) { |
| 716 |
code = pdfi_dict_knownget(ctx, pattern, "Shading", &o); |
| 717 |
if (code > 0) |
| 718 |
(void)pdfi_check_Shading(ctx, o, page_dict, tracker); |
| 719 |
pdfi_countdown(o); |
| 720 |
o = NULL; |
| 721 |
} |
| 722 |
|
| 723 |
code = pdfi_dict_knownget_type(ctx, pattern, "Resources", PDF_DICT, &o); |
| 724 |
if (code > 0) |
| 725 |
(void)pdfi_check_Resources(ctx, (pdf_dict *)o, page_dict, tracker); |
| 726 |
pdfi_countdown(o); |
| 727 |
o = NULL; |
| 728 |
if (tracker->transparent == true && tracker->spot_dict == NULL) |
| 729 |
goto transparency_exit; |
| 730 |
|
| 731 |
code = pdfi_dict_knownget_type(ctx, pattern, "ExtGState", PDF_DICT, &o); |
| 732 |
if (code > 0) |
| 733 |
(void)pdfi_check_ExtGState(ctx, (pdf_dict *)o, page_dict, tracker); |
| 734 |
pdfi_countdown(o); |
| 735 |
o = NULL; |
| 736 |
|
| 737 |
transparency_exit: |
| 738 |
return 0; |
| 739 |
} |
| 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 |
{ |
| 747 |
int code; |
| 748 |
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 |
code = pdfi_check_Pattern(ctx, pattern, page_dict, &tracker); |
| 758 |
if (code == 0) { |
| 759 |
*transparent = tracker.transparent; |
| 760 |
*BM_Not_Normal = tracker.BM_Not_Normal; |
| 761 |
} |
| 762 |
else |
| 763 |
*transparent = false; |
| 764 |
return code; |
| 765 |
} |
| 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 |
{ |
| 773 |
int code; |
| 774 |
uint64_t i, index; |
| 775 |
pdf_obj *Key = NULL, *Value = NULL; |
| 776 |
pdf_dict *instance_dict = NULL; |
| 777 |
|
| 778 |
if (resource_is_checked(tracker, (pdf_obj *)pattern_dict)) |
| 779 |
return 0; |
| 780 |
|
| 781 |
if (pdfi_type_of(pattern_dict) != PDF_DICT) |
| 782 |
return_error(gs_error_typecheck); |
| 783 |
|
| 784 |
if (pdfi_dict_entries(pattern_dict) > 0) { |
| 785 |
code = pdfi_loop_detector_mark(ctx); /* Mark the start of the Pattern dictionary loop */ |
| 786 |
if (code < 0) |
| 787 |
return code; |
| 788 |
|
| 789 |
code = pdfi_dict_first(ctx, pattern_dict, &Key, &Value, &index); |
| 790 |
if (code < 0) |
| 791 |
goto error1; |
| 792 |
|
| 793 |
i = 1; |
| 794 |
do { |
| 795 |
if (pdfi_type_of(Value) == PDF_DICT || pdfi_type_of(Value) == PDF_STREAM) { |
| 796 |
code = pdfi_dict_from_obj(ctx, Value, &instance_dict); |
| 797 |
if (code < 0) |
| 798 |
goto transparency_exit; |
| 799 |
|
| 800 |
code = pdfi_check_Pattern(ctx, instance_dict, page_dict, tracker); |
| 801 |
if (code < 0) |
| 802 |
goto transparency_exit; |
| 803 |
} |
| 804 |
|
| 805 |
pdfi_countdown(Key); |
| 806 |
Key = NULL; |
| 807 |
pdfi_countdown(Value); |
| 808 |
instance_dict = NULL; |
| 809 |
Value = NULL; |
| 810 |
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the Shading dictionary loop */ |
| 811 |
|
| 812 |
code = pdfi_loop_detector_mark(ctx); /* Mark the new start of the Shading dictionary loop */ |
| 813 |
if (code < 0) |
| 814 |
goto error1; |
| 815 |
|
| 816 |
do { |
| 817 |
if (i++ >= pdfi_dict_entries(pattern_dict)) { |
| 818 |
code = 0; |
| 819 |
goto transparency_exit; |
| 820 |
} |
| 821 |
|
| 822 |
code = pdfi_dict_next(ctx, pattern_dict, &Key, &Value, &index); |
| 823 |
if (code == 0 && (pdfi_type_of(Value) == PDF_DICT || pdfi_type_of(Value) == PDF_STREAM)) |
| 824 |
break; |
| 825 |
pdfi_countdown(Key); |
| 826 |
Key = NULL; |
| 827 |
pdfi_countdown(Value); |
| 828 |
Value = NULL; |
| 829 |
} while(1); |
| 830 |
}while (1); |
| 831 |
} |
| 832 |
return 0; |
| 833 |
|
| 834 |
transparency_exit: |
| 835 |
pdfi_countdown(Key); |
| 836 |
pdfi_countdown(Value); |
| 837 |
pdfi_countdown(instance_dict); |
| 838 |
|
| 839 |
error1: |
| 840 |
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */ |
| 841 |
return code; |
| 842 |
} |
| 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 |
{ |
| 852 |
int code = 0; |
| 853 |
pdf_obj *o = NULL; |
| 854 |
|
| 855 |
if (resource_is_checked(tracker, (pdf_obj *)font)) |
| 856 |
return 0; |
| 857 |
|
| 858 |
if (pdfi_type_of(font) != PDF_DICT) |
| 859 |
return_error(gs_error_typecheck); |
| 860 |
|
| 861 |
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 |
pdf_array *new_fonts = NULL; |
| 869 |
int index = 0; |
| 870 |
pdf_obj *array_obj = NULL; |
| 871 |
pdf_dict *font_info_dict = NULL; |
| 872 |
|
| 873 |
/* Let's start by gathering the information we need and storing it in a dictionary */ |
| 874 |
code = pdfi_dict_alloc(ctx, 4, &font_info_dict); |
| 875 |
if (code < 0) |
| 876 |
return code; |
| 877 |
pdfi_countup(font_info_dict); |
| 878 |
|
| 879 |
if (font->object_num != 0) { |
| 880 |
pdf_num *int_obj = NULL; |
| 881 |
|
| 882 |
code = pdfi_object_alloc(ctx, PDF_INT, 0, (pdf_obj **)&int_obj); |
| 883 |
if (code >= 0) { |
| 884 |
pdfi_countup(int_obj); |
| 885 |
int_obj->value.i = font->object_num; |
| 886 |
code = pdfi_dict_put(ctx, font_info_dict, "ObjectNum", (pdf_obj *)int_obj); |
| 887 |
pdfi_countdown(int_obj); |
| 888 |
} |
| 889 |
if (code < 0) { |
| 890 |
pdfi_countdown(font_info_dict); |
| 891 |
return code; |
| 892 |
} |
| 893 |
} |
| 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 |
{ |
| 901 |
pdf_obj *font_name = NULL; |
| 902 |
int lookup_code; |
| 903 |
bool found = false; |
| 904 |
|
| 905 |
lookup_code = pdfi_dict_get(ctx, font, "BaseFont", &font_name); |
| 906 |
if (lookup_code >= 0 && pdfi_type_of(font_name) == PDF_NAME) { |
| 907 |
code = pdfi_dict_put(ctx, font_info_dict, "BaseFont", font_name); |
| 908 |
pdfi_countdown(font_name); |
| 909 |
if (code < 0) { |
| 910 |
pdfi_countdown(font_info_dict); |
| 911 |
return code; |
| 912 |
} |
| 913 |
found = true; |
| 914 |
} else { |
| 915 |
pdfi_countdown(font_name); |
| 916 |
font_name = NULL; |
| 917 |
} |
| 918 |
|
| 919 |
if (!found) { |
| 920 |
lookup_code = pdfi_dict_get(ctx, font, "FontDescriptor", &font_name); |
| 921 |
if (lookup_code >= 0 && pdfi_type_of(font_name) == PDF_DICT) { |
| 922 |
pdf_dict *font_desc = (pdf_dict *)font_name; |
| 923 |
pdf_obj *desc_font_name = NULL; |
| 924 |
|
| 925 |
lookup_code = pdfi_dict_get(ctx, font_desc, "FontName", &desc_font_name); |
| 926 |
pdfi_countdown(font_name); |
| 927 |
font_name = NULL; |
| 928 |
|
| 929 |
if (lookup_code >= 0 && pdfi_type_of(desc_font_name) == PDF_NAME) { |
| 930 |
code = pdfi_dict_put(ctx, font_info_dict, "BaseFont", desc_font_name); |
| 931 |
pdfi_countdown(desc_font_name); |
| 932 |
if (code < 0) { |
| 933 |
pdfi_countdown(font_info_dict); |
| 934 |
return code; |
| 935 |
} |
| 936 |
found = true; |
| 937 |
} else { |
| 938 |
pdfi_countdown(desc_font_name); |
| 939 |
} |
| 940 |
} else { |
| 941 |
pdfi_countdown(font_name); |
| 942 |
font_name = NULL; |
| 943 |
} |
| 944 |
} |
| 945 |
|
| 946 |
if (!found) { |
| 947 |
lookup_code = pdfi_dict_get(ctx, font, "Name", &font_name); |
| 948 |
if (lookup_code >= 0 && pdfi_type_of(font_name) == PDF_NAME) { |
| 949 |
code = pdfi_dict_put(ctx, font_info_dict, "BaseFont", font_name); |
| 950 |
pdfi_countdown(font_name); |
| 951 |
if (code < 0) { |
| 952 |
pdfi_countdown(font_info_dict); |
| 953 |
return code; |
| 954 |
} |
| 955 |
} else { |
| 956 |
pdfi_countdown(font_name); |
| 957 |
} |
| 958 |
} |
| 959 |
|
| 960 |
array_obj = NULL; |
| 961 |
} |
| 962 |
|
| 963 |
code = pdfi_dict_get(ctx, font, "ToUnicode", &array_obj); |
| 964 |
if (code >= 0) |
| 965 |
code = pdfi_dict_put(ctx, font_info_dict, "ToUnicode", PDF_TRUE_OBJ); |
| 966 |
else |
| 967 |
code = pdfi_dict_put(ctx, font_info_dict, "ToUnicode", PDF_FALSE_OBJ); |
| 968 |
pdfi_countdown(array_obj); |
| 969 |
array_obj = NULL; |
| 970 |
if (code < 0) |
| 971 |
return code; |
| 972 |
|
| 973 |
code = pdfi_dict_get(ctx, font, "FontDescriptor", &array_obj); |
| 974 |
if (code >= 0) { |
| 975 |
bool known = false; |
| 976 |
|
| 977 |
(void)pdfi_dict_known(ctx, (pdf_dict *)array_obj, "FontFile", &known); |
| 978 |
if (!known) { |
| 979 |
(void)pdfi_dict_known(ctx, (pdf_dict *)array_obj, "FontFile2", &known); |
| 980 |
if (!known) { |
| 981 |
(void)pdfi_dict_known(ctx, (pdf_dict *)array_obj, "FontFile3", &known); |
| 982 |
} |
| 983 |
} |
| 984 |
|
| 985 |
if (known > 0) |
| 986 |
code = pdfi_dict_put(ctx, font_info_dict, "Embedded", PDF_TRUE_OBJ); |
| 987 |
else |
| 988 |
code = pdfi_dict_put(ctx, font_info_dict, "Embedded", PDF_FALSE_OBJ); |
| 989 |
} else |
| 990 |
code = pdfi_dict_put(ctx, font_info_dict, "Embedded", PDF_FALSE_OBJ); |
| 991 |
|
| 992 |
pdfi_countdown(array_obj); |
| 993 |
array_obj = NULL; |
| 994 |
|
| 995 |
if (code < 0) |
| 996 |
return code; |
| 997 |
|
| 998 |
|
| 999 |
code = pdfi_dict_knownget_type(ctx, font, "Subtype", PDF_NAME, &array_obj); |
| 1000 |
if (code > 0) { |
| 1001 |
code = pdfi_dict_put(ctx, font_info_dict, "Subtype", array_obj); |
| 1002 |
if (code < 0) { |
| 1003 |
pdfi_countdown(array_obj); |
| 1004 |
pdfi_countdown(font_info_dict); |
| 1005 |
return code; |
| 1006 |
} |
| 1007 |
|
| 1008 |
if (pdfi_name_is((pdf_name *)array_obj, "Type3")) { |
| 1009 |
pdfi_countdown(o); |
| 1010 |
o = NULL; |
| 1011 |
|
| 1012 |
code = pdfi_dict_knownget_type(ctx, font, "Resources", PDF_DICT, &o); |
| 1013 |
if (code > 0) |
| 1014 |
(void)pdfi_check_Resources(ctx, (pdf_dict *)o, page_dict, tracker); |
| 1015 |
} |
| 1016 |
|
| 1017 |
if (pdfi_name_is((const pdf_name *)array_obj, "Type0")){ |
| 1018 |
pdf_array *descendants = NULL; |
| 1019 |
pdf_dict *desc_font = NULL; |
| 1020 |
|
| 1021 |
code = pdfi_dict_get(ctx, font, "DescendantFonts", (pdf_obj **)&descendants); |
| 1022 |
if (code >= 0) { |
| 1023 |
code = pdfi_array_get(ctx, descendants, 0, (pdf_obj **)&desc_font); |
| 1024 |
if (code >= 0){ |
| 1025 |
pdf_array *desc_array = NULL; |
| 1026 |
|
| 1027 |
code = pdfi_array_alloc(ctx, 0, &desc_array); |
| 1028 |
if (code >= 0) { |
| 1029 |
pdf_array *saved = tracker->font_array; |
| 1030 |
|
| 1031 |
pdfi_countup(desc_array); |
| 1032 |
tracker->font_array = desc_array; |
| 1033 |
(void)pdfi_check_Font(ctx, desc_font, page_dict, tracker); |
| 1034 |
(void)pdfi_dict_put(ctx, font_info_dict, "Descendants", (pdf_obj *)tracker->font_array); |
| 1035 |
pdfi_countdown((pdf_obj *)tracker->font_array); |
| 1036 |
tracker->font_array = saved; |
| 1037 |
} |
| 1038 |
pdfi_countdown(descendants); |
| 1039 |
pdfi_countdown(desc_font); |
| 1040 |
} |
| 1041 |
} |
| 1042 |
} |
| 1043 |
} |
| 1044 |
pdfi_countdown(array_obj); |
| 1045 |
array_obj = NULL; |
| 1046 |
|
| 1047 |
code = pdfi_array_alloc(ctx, pdfi_array_size(tracker->font_array) + 1, &new_fonts); |
| 1048 |
if (code < 0) { |
| 1049 |
pdfi_countdown(font_info_dict); |
| 1050 |
return code; |
| 1051 |
} |
| 1052 |
pdfi_countup(new_fonts); |
| 1053 |
|
| 1054 |
for (index = 0; index < pdfi_array_size(tracker->font_array); index++) { |
| 1055 |
code = pdfi_array_get(ctx, tracker->font_array, index, &array_obj); |
| 1056 |
if (code < 0) { |
| 1057 |
pdfi_countdown(font_info_dict); |
| 1058 |
pdfi_countdown(new_fonts); |
| 1059 |
return code; |
| 1060 |
} |
| 1061 |
code = pdfi_array_put(ctx, new_fonts, index, array_obj); |
| 1062 |
pdfi_countdown(array_obj); |
| 1063 |
if (code < 0) { |
| 1064 |
pdfi_countdown(font_info_dict); |
| 1065 |
pdfi_countdown(new_fonts); |
| 1066 |
return code; |
| 1067 |
} |
| 1068 |
} |
| 1069 |
code = pdfi_array_put(ctx, new_fonts, index, (pdf_obj *)font_info_dict); |
| 1070 |
if (code < 0) { |
| 1071 |
pdfi_countdown(font_info_dict); |
| 1072 |
pdfi_countdown(new_fonts); |
| 1073 |
return code; |
| 1074 |
} |
| 1075 |
pdfi_countdown(font_info_dict); |
| 1076 |
pdfi_countdown(tracker->font_array); |
| 1077 |
tracker->font_array = new_fonts; |
| 1078 |
} else { |
| 1079 |
code = pdfi_dict_knownget_type(ctx, font, "Subtype", PDF_NAME, &o); |
| 1080 |
if (code > 0) { |
| 1081 |
if (pdfi_name_is((pdf_name *)o, "Type3")) { |
| 1082 |
pdfi_countdown(o); |
| 1083 |
o = NULL; |
| 1084 |
|
| 1085 |
code = pdfi_dict_knownget_type(ctx, font, "Resources", PDF_DICT, &o); |
| 1086 |
if (code > 0) |
| 1087 |
(void)pdfi_check_Resources(ctx, (pdf_dict *)o, page_dict, tracker); |
| 1088 |
} |
| 1089 |
} |
| 1090 |
|
| 1091 |
pdfi_countdown(o); |
| 1092 |
o = NULL; |
| 1093 |
} |
| 1094 |
|
| 1095 |
return 0; |
| 1096 |
} |
| 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 |
{ |
| 1104 |
int code = 0; |
| 1105 |
uint64_t i, index; |
| 1106 |
pdf_obj *Key = NULL, *Value = NULL; |
| 1107 |
|
| 1108 |
if (resource_is_checked(tracker, (pdf_obj *)font_dict)) |
| 1109 |
return 0; |
| 1110 |
|
| 1111 |
if (pdfi_type_of(font_dict) != PDF_DICT) |
| 1112 |
return_error(gs_error_typecheck); |
| 1113 |
|
| 1114 |
if (pdfi_dict_entries(font_dict) > 0) { |
| 1115 |
code = pdfi_loop_detector_mark(ctx); /* Mark the start of the Font dictionary loop */ |
| 1116 |
if (code < 0) |
| 1117 |
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 |
code = pdfi_dict_first_no_store_R(ctx, font_dict, &Key, &Value, &index); |
| 1124 |
if (code < 0) { |
| 1125 |
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */ |
| 1126 |
goto error1; |
| 1127 |
} |
| 1128 |
|
| 1129 |
i = 1; |
| 1130 |
do { |
| 1131 |
if (pdfi_type_of(Value) == PDF_DICT) |
| 1132 |
code = pdfi_check_Font(ctx, (pdf_dict *)Value, page_dict, tracker); |
| 1133 |
else if (pdfi_type_of(Value) == PDF_FONT) { |
| 1134 |
pdf_dict *d = ((pdf_font *)Value)->PDF_font; |
| 1135 |
|
| 1136 |
code = pdfi_check_Font(ctx, d, page_dict, tracker); |
| 1137 |
} else { |
| 1138 |
pdfi_set_warning(ctx, 0, NULL, W_PDF_FONTRESOURCE_TYPE, "pdfi_check_Font_dict", ""); |
| 1139 |
} |
| 1140 |
if (code < 0) |
| 1141 |
break; |
| 1142 |
|
| 1143 |
pdfi_countdown(Key); |
| 1144 |
Key = NULL; |
| 1145 |
pdfi_countdown(Value); |
| 1146 |
Value = NULL; |
| 1147 |
|
| 1148 |
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the Font dictionary loop */ |
| 1149 |
|
| 1150 |
code = pdfi_loop_detector_mark(ctx); /* Mark the new start of the Font dictionary loop */ |
| 1151 |
if (code < 0) |
| 1152 |
goto error1; |
| 1153 |
|
| 1154 |
if (i++ >= pdfi_dict_entries(font_dict)) { |
| 1155 |
code = 0; |
| 1156 |
break; |
| 1157 |
} |
| 1158 |
|
| 1159 |
code = pdfi_dict_next_no_store_R(ctx, font_dict, &Key, &Value, &index); |
| 1160 |
if (code < 0) |
| 1161 |
break; |
| 1162 |
}while (1); |
| 1163 |
|
| 1164 |
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */ |
| 1165 |
} |
| 1166 |
|
| 1167 |
pdfi_countdown(Key); |
| 1168 |
pdfi_countdown(Value); |
| 1169 |
|
| 1170 |
error1: |
| 1171 |
return code; |
| 1172 |
} |
| 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 |
{ |
| 1177 |
int code; |
| 1178 |
pdf_obj *d = NULL; |
| 1179 |
|
| 1180 |
if (resource_is_checked(tracker, (pdf_obj *)Resources_dict)) |
| 1181 |
return 0; |
| 1182 |
|
| 1183 |
if (pdfi_type_of(Resources_dict) != PDF_DICT) |
| 1184 |
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 |
if (tracker->spot_dict != NULL) { |
| 1191 |
code = pdfi_dict_knownget_type(ctx, Resources_dict, "ColorSpace", PDF_DICT, &d); |
| 1192 |
if (code < 0 && code != gs_error_undefined) { |
| 1193 |
if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_RESOURCE, "pdfi_check_Resources", "ColorSpace")) < 0) |
| 1194 |
return code; |
| 1195 |
} |
| 1196 |
(void)pdfi_check_ColorSpace_dict(ctx, (pdf_dict *)d, page_dict, tracker); |
| 1197 |
|
| 1198 |
pdfi_countdown(d); |
| 1199 |
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 |
code = pdfi_cache_object(ctx, (pdf_obj *)Resources_dict); |
| 1205 |
if (code < 0) { |
| 1206 |
if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_CACHE_FAIL, "pdfi_check_Resources", "")) < 0) |
| 1207 |
return code; |
| 1208 |
} |
| 1209 |
|
| 1210 |
code = pdfi_dict_knownget_type(ctx, Resources_dict, "Shading", PDF_DICT, &d); |
| 1211 |
if (code < 0 && code != gs_error_undefined) { |
| 1212 |
if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_RESOURCE, "pdfi_check_Resources", "Shading")) < 0) |
| 1213 |
return code; |
| 1214 |
} |
| 1215 |
(void)pdfi_check_Shading_dict(ctx, (pdf_dict *)d, page_dict, tracker); |
| 1216 |
pdfi_countdown(d); |
| 1217 |
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 |
code = pdfi_cache_object(ctx, (pdf_obj *)Resources_dict); |
| 1223 |
if (code < 0) { |
| 1224 |
if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_CACHE_FAIL, "pdfi_check_Resources", "")) < 0) |
| 1225 |
return code; |
| 1226 |
} |
| 1227 |
} |
| 1228 |
|
| 1229 |
code = pdfi_dict_knownget_type(ctx, Resources_dict, "XObject", PDF_DICT, &d); |
| 1230 |
if (code < 0 && code != gs_error_undefined) { |
| 1231 |
if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_RESOURCE, "pdfi_check_Resources", "XObject")) < 0) |
| 1232 |
return code; |
| 1233 |
} |
| 1234 |
code = pdfi_check_XObject_dict(ctx, (pdf_dict *)d, page_dict, tracker); |
| 1235 |
if (code == gs_error_Fatal) { |
| 1236 |
if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_RESOURCE, "pdfi_check_Resources", "XObject")) < 0) |
| 1237 |
return code; |
| 1238 |
} |
| 1239 |
pdfi_countdown(d); |
| 1240 |
d = NULL; |
| 1241 |
|
| 1242 |
/* Put the Resources dictionary back in cache (or promote it) in case checking XObject dict |
| 1243 |
* created so many cache entires it flushed Resources from cache |
| 1244 |
*/ |
| 1245 |
code = pdfi_cache_object(ctx, (pdf_obj *)Resources_dict); |
| 1246 |
if (code < 0) { |
| 1247 |
if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_CACHE_FAIL, "pdfi_check_Resources", "")) < 0) |
| 1248 |
return code; |
| 1249 |
} |
| 1250 |
|
| 1251 |
code = pdfi_dict_knownget_type(ctx, Resources_dict, "Pattern", PDF_DICT, &d); |
| 1252 |
if (code < 0 && code != gs_error_undefined) { |
| 1253 |
if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_RESOURCE, "pdfi_check_Resources", "Pattern")) < 0) { |
| 1254 |
return code; |
| 1255 |
} |
| 1256 |
} |
| 1257 |
(void)pdfi_check_Pattern_dict(ctx, (pdf_dict *)d, page_dict, tracker); |
| 1258 |
pdfi_countdown(d); |
| 1259 |
d = NULL; |
| 1260 |
|
| 1261 |
/* Put the Resources dictionary back in cache (or promote it) in case checking Pattern dict |
| 1262 |
* created so many cache entires it flushed Resources from cache |
| 1263 |
*/ |
| 1264 |
code = pdfi_cache_object(ctx, (pdf_obj *)Resources_dict); |
| 1265 |
if (code < 0) { |
| 1266 |
if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_CACHE_FAIL, "pdfi_check_Resources", "")) < 0) |
| 1267 |
return code; |
| 1268 |
} |
| 1269 |
|
| 1270 |
code = pdfi_dict_knownget_type(ctx, Resources_dict, "Font", PDF_DICT, &d); |
| 1271 |
if (code < 0) { |
| 1272 |
if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_RESOURCE, "pdfi_check_Resources", "Font")) < 0) |
| 1273 |
return code; |
| 1274 |
} |
| 1275 |
(void)pdfi_check_Font_dict(ctx, (pdf_dict *)d, page_dict, tracker); |
| 1276 |
/* From this point onwards, if we detect transparency (or have already detected it) we |
| 1277 |
* can exit, we have already counted up any spot colours. |
| 1278 |
*/ |
| 1279 |
pdfi_countdown(d); |
| 1280 |
d = NULL; |
| 1281 |
|
| 1282 |
/* Put the Resources dictionary back in cache (or promote it) in case checking Font dict |
| 1283 |
* created so many cache entires it flushed Resources from cache |
| 1284 |
*/ |
| 1285 |
code = pdfi_cache_object(ctx, (pdf_obj *)Resources_dict); |
| 1286 |
if (code < 0) { |
| 1287 |
if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_CACHE_FAIL, "pdfi_check_Resources", "")) < 0) |
| 1288 |
return code; |
| 1289 |
} |
| 1290 |
|
| 1291 |
code = pdfi_dict_knownget_type(ctx, Resources_dict, "ExtGState", PDF_DICT, &d); |
| 1292 |
if (code < 0 && code != gs_error_undefined) { |
| 1293 |
if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_RESOURCE, "pdfi_check_Resources", "ExtGState")) < 0) |
| 1294 |
return code; |
| 1295 |
} |
| 1296 |
(void)pdfi_check_ExtGState_dict(ctx, (pdf_dict *)d, page_dict, tracker); |
| 1297 |
pdfi_countdown(d); |
| 1298 |
d = NULL; |
| 1299 |
|
| 1300 |
/* Put the Resources dictionary back in cache (or promote it) in case checking ExtGState dict |
| 1301 |
* created so many cache entires it flushed Resources from cache |
| 1302 |
*/ |
| 1303 |
code = pdfi_cache_object(ctx, (pdf_obj *)Resources_dict); |
| 1304 |
if (code < 0) { |
| 1305 |
if ((code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_CACHE_FAIL, "pdfi_check_Resources", "")) < 0) |
| 1306 |
return code; |
| 1307 |
} |
| 1308 |
|
| 1309 |
return 0; |
| 1310 |
} |
| 1311 |
|
| 1312 |
static int pdfi_check_annot_for_transparency(pdf_context *ctx, pdf_dict *annot, pdf_dict *page_dict, |
| 1313 |
pdfi_check_tracker_t *tracker) |
| 1314 |
{ |
| 1315 |
int code; |
| 1316 |
pdf_name *n; |
| 1317 |
pdf_obj *N = NULL; |
| 1318 |
pdf_dict *ap = NULL; |
| 1319 |
pdf_dict *Resources = NULL; |
| 1320 |
double f; |
| 1321 |
|
| 1322 |
if (resource_is_checked(tracker, (pdf_obj *)annot)) |
| 1323 |
return 0; |
| 1324 |
|
| 1325 |
if (pdfi_type_of(annot) != PDF_DICT) |
| 1326 |
return_error(gs_error_typecheck); |
| 1327 |
|
| 1328 |
/* Check #1 Does the (Normal) Appearnce stream use any Resources which include transparency. |
| 1329 |
* We check this first, because this also checks for spot colour spaces. Once we've done that we |
| 1330 |
* can exit the checks as soon as we detect transparency. |
| 1331 |
*/ |
| 1332 |
code = pdfi_dict_knownget_type(ctx, annot, "AP", PDF_DICT, (pdf_obj **)&ap); |
| 1333 |
if (code > 0) |
| 1334 |
{ |
| 1335 |
/* Fetch without resolving indirect ref because pdfmark wants it that way later */ |
| 1336 |
code = pdfi_dict_get_no_store_R(ctx, ap, "N", (pdf_obj **)&N); |
| 1337 |
if (code >= 0) { |
| 1338 |
pdf_dict *dict = NULL; |
| 1339 |
|
| 1340 |
code = pdfi_dict_from_obj(ctx, N, &dict); |
| 1341 |
if (code == 0) |
| 1342 |
code = pdfi_dict_knownget_type(ctx, dict, "Resources", PDF_DICT, (pdf_obj **)&Resources); |
| 1343 |
if (code > 0) |
| 1344 |
code = pdfi_check_Resources(ctx, (pdf_dict *)Resources, page_dict, tracker); |
| 1345 |
} |
| 1346 |
if (code == gs_error_undefined) |
| 1347 |
code = 0; |
| 1348 |
} |
| 1349 |
pdfi_countdown(ap); |
| 1350 |
pdfi_countdown(N); |
| 1351 |
pdfi_countdown(Resources); |
| 1352 |
|
| 1353 |
if (code < 0) |
| 1354 |
return code; |
| 1355 |
/* We've checked the Resources, and nothing else in an annotation can define spot colours, so |
| 1356 |
* if we detected transparency in the Resources we need not check further. |
| 1357 |
*/ |
| 1358 |
if (tracker->transparent == true) |
| 1359 |
return 0; |
| 1360 |
|
| 1361 |
code = pdfi_dict_get_type(ctx, annot, "Subtype", PDF_NAME, (pdf_obj **)&n); |
| 1362 |
if (code < 0) { |
| 1363 |
if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_TYPE, "pdfi_check_annot_for_transparency", "")) < 0) { |
| 1364 |
return code; |
| 1365 |
} |
| 1366 |
} else { |
| 1367 |
/* Check #2, Highlight annotations are always preformed with transparency */ |
| 1368 |
if (pdfi_name_is((const pdf_name *)n, "Highlight")) { |
| 1369 |
pdfi_countdown(n); |
| 1370 |
tracker->transparent = true; |
| 1371 |
return 0; |
| 1372 |
} |
| 1373 |
pdfi_countdown(n); |
| 1374 |
n = NULL; |
| 1375 |
|
| 1376 |
/* Check #3 Blend Mode (BM) not being 'Normal' or 'Compatible' */ |
| 1377 |
code = pdfi_dict_knownget_type(ctx, annot, "BM", PDF_NAME, (pdf_obj **)&n); |
| 1378 |
if (code > 0) { |
| 1379 |
if (!pdfi_name_is((const pdf_name *)n, "Normal")) { |
| 1380 |
if (!pdfi_name_is((const pdf_name *)n, "Compatible")) { |
| 1381 |
pdfi_countdown(n); |
| 1382 |
tracker->transparent = true; |
| 1383 |
return 0; |
| 1384 |
} |
| 1385 |
} |
| 1386 |
code = 0; |
| 1387 |
} |
| 1388 |
pdfi_countdown(n); |
| 1389 |
if (code < 0) |
| 1390 |
return code; |
| 1391 |
|
| 1392 |
/* Check #4 stroke constant alpha (CA) is not 1 (100% opaque) */ |
| 1393 |
code = pdfi_dict_knownget_number(ctx, annot, "CA", &f); |
| 1394 |
if (code > 0) { |
| 1395 |
if (f != 1.0) { |
| 1396 |
tracker->transparent = true; |
| 1397 |
return 0; |
| 1398 |
} |
| 1399 |
} |
| 1400 |
if (code < 0) |
| 1401 |
return code; |
| 1402 |
|
| 1403 |
/* Check #5 non-stroke constant alpha (ca) is not 1 (100% opaque) */ |
| 1404 |
code = pdfi_dict_knownget_number(ctx, annot, "ca", &f); |
| 1405 |
if (code > 0) { |
| 1406 |
if (f != 1.0) { |
| 1407 |
tracker->transparent = true; |
| 1408 |
return 0; |
| 1409 |
} |
| 1410 |
} |
| 1411 |
if (code < 0) |
| 1412 |
return code; |
| 1413 |
} |
| 1414 |
|
| 1415 |
return 0; |
| 1416 |
} |
| 1417 |
|
| 1418 |
static int pdfi_check_Annots_for_transparency(pdf_context *ctx, pdf_array *annots_array, |
| 1419 |
pdf_dict *page_dict, pdfi_check_tracker_t *tracker) |
| 1420 |
{ |
| 1421 |
int i, code = 0; |
| 1422 |
pdf_dict *annot = NULL; |
| 1423 |
|
| 1424 |
if (resource_is_checked(tracker, (pdf_obj *)annots_array)) |
| 1425 |
return 0; |
| 1426 |
|
| 1427 |
if (pdfi_type_of(annots_array) != PDF_ARRAY) |
| 1428 |
return_error(gs_error_typecheck); |
| 1429 |
|
| 1430 |
for (i=0; i < pdfi_array_size(annots_array); i++) { |
| 1431 |
code = pdfi_array_get_type(ctx, annots_array, (uint64_t)i, PDF_DICT, (pdf_obj **)&annot); |
| 1432 |
if (code >= 0) { |
| 1433 |
code = pdfi_check_annot_for_transparency(ctx, annot, page_dict, tracker); |
| 1434 |
if (code < 0 && (code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_INVALID_TRANS_XOBJECT, "pdfi_check_Annots_for_transparency", "")) < 0) { |
| 1435 |
goto exit; |
| 1436 |
} |
| 1437 |
|
| 1438 |
/* If we've found transparency, and don't need to continue checkign for spot colours |
| 1439 |
* just exit as fast as possible. |
| 1440 |
*/ |
| 1441 |
if (tracker->transparent == true && tracker->spot_dict == NULL) |
| 1442 |
goto exit; |
| 1443 |
|
| 1444 |
pdfi_countdown(annot); |
| 1445 |
annot = NULL; |
| 1446 |
} |
| 1447 |
else if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_INVALID_TRANS_XOBJECT, "pdfi_check_Annots_for_transparency", "")) < 0) { |
| 1448 |
goto exit; |
| 1449 |
} |
| 1450 |
} |
| 1451 |
exit: |
| 1452 |
pdfi_countdown(annot); |
| 1453 |
return code; |
| 1454 |
} |
| 1455 |
|
| 1456 |
/* Check for transparency and spots on page. |
| 1457 |
* |
| 1458 |
* Sets ctx->spot_capable_device |
| 1459 |
* Builds a dictionary of the unique spot names in spot_dict |
| 1460 |
* Set 'transparent' to true if there is transparency on the page |
| 1461 |
* |
| 1462 |
* From the original PDF interpreter written in PostScript: |
| 1463 |
* Note: we deliberately don't check to see whether a Group is defined, |
| 1464 |
* because Adobe Illustrator 10 (and possibly other applications) define |
| 1465 |
* a page-level group whether transparency is actually used or not. |
| 1466 |
* Ignoring the presence of Group is justified because, in the absence |
| 1467 |
* of any other transparency features, they have no effect. |
| 1468 |
*/ |
| 1469 |
static int pdfi_check_page_inner(pdf_context *ctx, pdf_dict *page_dict, |
| 1470 |
pdfi_check_tracker_t *tracker) |
| 1471 |
{ |
| 1472 |
int code; |
| 1473 |
pdf_dict *Resources = NULL; |
| 1474 |
pdf_array *Annots = NULL; |
| 1475 |
pdf_dict *Group = NULL; |
| 1476 |
pdf_obj *CS = NULL; |
| 1477 |
|
| 1478 |
tracker->transparent = false; |
| 1479 |
|
| 1480 |
if (pdfi_type_of(page_dict) != PDF_DICT) |
| 1481 |
return_error(gs_error_typecheck); |
| 1482 |
|
| 1483 |
|
| 1484 |
/* Check if the page dictionary has a page Group entry (for spots). |
| 1485 |
* Page group should mean the page has transparency but we ignore it for the purposes |
| 1486 |
* of transparency detection. See above. |
| 1487 |
*/ |
| 1488 |
if (tracker->spot_dict) { |
| 1489 |
code = pdfi_dict_knownget_type(ctx, page_dict, "Group", PDF_DICT, (pdf_obj **)&Group); |
| 1490 |
if (code > 0) { |
| 1491 |
/* If Group has a ColorSpace (CS), then check it for spot colours */ |
| 1492 |
code = pdfi_dict_knownget(ctx, Group, "CS", &CS); |
| 1493 |
if (code > 0) |
| 1494 |
code = pdfi_check_ColorSpace_for_spots(ctx, CS, Group, page_dict, tracker->spot_dict); |
| 1495 |
|
| 1496 |
if (code < 0) { |
| 1497 |
if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_GS_LIB_ERROR, "pdfi_check_page_inner", "")) < 0) { |
| 1498 |
goto exit; |
| 1499 |
} |
| 1500 |
} |
| 1501 |
} |
| 1502 |
} |
| 1503 |
|
| 1504 |
/* Now check any Resources dictionary in the Page dictionary */ |
| 1505 |
code = pdfi_dict_knownget_type(ctx, page_dict, "Resources", PDF_DICT, (pdf_obj **)&Resources); |
| 1506 |
if (code > 0) |
| 1507 |
code = pdfi_check_Resources(ctx, Resources, page_dict, tracker); |
| 1508 |
|
| 1509 |
if (code == gs_error_pdf_stackoverflow || (code < 0 && |
| 1510 |
(code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_GS_LIB_ERROR, "pdfi_check_page_inner", "")) < 0)) { |
| 1511 |
goto exit; |
| 1512 |
} |
| 1513 |
|
| 1514 |
/* If we are drawing Annotations, check to see if the page uses any Annots */ |
| 1515 |
if (ctx->args.showannots) { |
| 1516 |
code = pdfi_dict_knownget_type(ctx, page_dict, "Annots", PDF_ARRAY, (pdf_obj **)&Annots); |
| 1517 |
if (code > 0) |
| 1518 |
code = pdfi_check_Annots_for_transparency(ctx, Annots, page_dict, |
| 1519 |
tracker); |
| 1520 |
if (code < 0) { |
| 1521 |
if ((code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_GS_LIB_ERROR, "pdfi_check_page_inner", "")) < 0) { |
| 1522 |
goto exit; |
| 1523 |
} |
| 1524 |
} |
| 1525 |
} |
| 1526 |
|
| 1527 |
code = 0; |
| 1528 |
exit: |
| 1529 |
pdfi_countdown(Resources); |
| 1530 |
pdfi_countdown(Annots); |
| 1531 |
pdfi_countdown(CS); |
| 1532 |
pdfi_countdown(Group); |
| 1533 |
return code; |
| 1534 |
} |
| 1535 |
|
| 1536 |
/* Checks page for transparency, and sets up device for spots, if applicable |
| 1537 |
* Sets ctx->page.has_transparency and ctx->page.num_spots |
| 1538 |
* do_setup -- indicates whether to actually set up the device with the spot count. |
| 1539 |
*/ |
| 1540 |
int pdfi_check_page(pdf_context *ctx, pdf_dict *page_dict, pdf_array **fonts_array, pdf_array **spots_array, bool do_setup) |
| 1541 |
{ |
| 1542 |
int code, code1; |
| 1543 |
int spots = 0; |
| 1544 |
pdfi_check_tracker_t tracker; |
| 1545 |
pdf_dict *Resources = NULL; |
| 1546 |
|
| 1547 |
ctx->page.num_spots = 0; |
| 1548 |
ctx->page.has_transparency = false; |
| 1549 |
|
| 1550 |
code = pdfi_check_init_tracker(ctx, &tracker, fonts_array, spots_array); |
| 1551 |
if (code < 0) |
| 1552 |
goto exit; |
| 1553 |
|
| 1554 |
/* Check for spots and transparency in this page */ |
| 1555 |
code = pdfi_check_page_inner(ctx, page_dict, &tracker); |
| 1556 |
if (code < 0) |
| 1557 |
goto exit; |
| 1558 |
|
| 1559 |
/* Count the spots */ |
| 1560 |
if (tracker.spot_dict) |
| 1561 |
spots = pdfi_dict_entries(tracker.spot_dict); |
| 1562 |
|
| 1563 |
/* If setup requested, tell the device about spots and transparency */ |
| 1564 |
if (do_setup) { |
| 1565 |
gs_c_param_list list; |
| 1566 |
int a = 0; |
| 1567 |
pdf_name *Key = NULL; |
| 1568 |
pdf_obj *Value = NULL; |
| 1569 |
uint64_t index = 0; |
| 1570 |
|
| 1571 |
gs_c_param_list_write(&list, ctx->memory); |
| 1572 |
|
| 1573 |
/* If there are spot colours (and by inference, the device renders spot plates) then |
| 1574 |
* send the number of Spots to the device, so it can setup correctly. |
| 1575 |
*/ |
| 1576 |
if (tracker.spot_dict) { |
| 1577 |
/* There is some awkwardness here. If the SeparationColorNames setting |
| 1578 |
* fails, we want to ignore it (this can mean that we exceeded the maximum |
| 1579 |
* number of colourants and some will be converted to CMYK). But if that happens, |
| 1580 |
* any other parameters in the same list which haven't already been prcoessed |
| 1581 |
* will be lost. So we need to send two lists, the SeparationColorNames and |
| 1582 |
* 'everything else'. |
| 1583 |
*/ |
| 1584 |
if (spots > 0) { |
| 1585 |
gs_param_string_array sa; |
| 1586 |
gs_param_string *table = NULL; |
| 1587 |
|
| 1588 |
table = (gs_param_string *)gs_alloc_byte_array(ctx->memory, spots, sizeof(gs_param_string), "SeparationNames"); |
| 1589 |
if (table != NULL) |
| 1590 |
{ |
| 1591 |
memset(table, 0x00, spots * sizeof(gs_param_string)); |
| 1592 |
|
| 1593 |
code = pdfi_dict_first(ctx, tracker.spot_dict, (pdf_obj **)&Key, &Value, &index); |
| 1594 |
while (code >= 0) |
| 1595 |
{ |
| 1596 |
if (pdfi_type_of(Key) == PDF_NAME) { |
| 1597 |
table[a].data = ((pdf_string *)Key)->data; |
| 1598 |
table[a].size = ((pdf_string *)Key)->length; |
| 1599 |
table[a++].persistent = false; |
| 1600 |
} |
| 1601 |
/* Although we count down the returned PDF objects here, the pointers |
| 1602 |
* to the name data remain valid and won't move. Provided we don't |
| 1603 |
* retain the pointers after we free the tracker dictionary this is |
| 1604 |
* safe to do. |
| 1605 |
*/ |
| 1606 |
pdfi_countdown(Key); |
| 1607 |
Key = NULL; |
| 1608 |
pdfi_countdown(Value); |
| 1609 |
Value = NULL; |
| 1610 |
code = pdfi_dict_next(ctx, tracker.spot_dict, (pdf_obj **)&Key, &Value, &index); |
| 1611 |
} |
| 1612 |
sa.data = table; |
| 1613 |
sa.size = spots; |
| 1614 |
sa.persistent = false; |
| 1615 |
|
| 1616 |
(void)param_write_string_array((gs_param_list *)&list, "SeparationColorNames", &sa); |
| 1617 |
gs_c_param_list_read(&list); |
| 1618 |
code = gs_putdeviceparams(ctx->pgs->device, (gs_param_list *)&list); |
| 1619 |
gs_c_param_list_release(&list); |
| 1620 |
|
| 1621 |
gs_free_object(ctx->memory, table, "SeparationNames"); |
| 1622 |
if (code > 0) { |
| 1623 |
/* The device was closed, we need to reopen it */ |
| 1624 |
code = gs_setdevice_no_erase(ctx->pgs, ctx->pgs->device); |
| 1625 |
if (code < 0) |
| 1626 |
goto exit; |
| 1627 |
gs_erasepage(ctx->pgs); |
| 1628 |
} |
| 1629 |
|
| 1630 |
/* Reset the list back to being writeable */ |
| 1631 |
gs_c_param_list_write(&list, ctx->memory); |
| 1632 |
} |
| 1633 |
else { |
| 1634 |
code = gs_note_error(gs_error_VMerror); |
| 1635 |
goto exit; |
| 1636 |
} |
| 1637 |
} |
| 1638 |
/* Update the number of spots */ |
| 1639 |
param_write_int((gs_param_list *)&list, "PageSpotColors", &spots); |
| 1640 |
} |
| 1641 |
/* Update the page transparency */ |
| 1642 |
(void)param_write_bool((gs_param_list *)&list, "PageUsesTransparency", |
| 1643 |
&tracker.transparent); |
| 1644 |
gs_c_param_list_read(&list); |
| 1645 |
code = gs_putdeviceparams(ctx->pgs->device, (gs_param_list *)&list); |
| 1646 |
gs_c_param_list_release(&list); |
| 1647 |
|
| 1648 |
if (code > 0) { |
| 1649 |
/* The device was closed, we need to reopen it */ |
| 1650 |
code = gs_setdevice_no_erase(ctx->pgs, ctx->pgs->device); |
| 1651 |
if (code < 0) |
| 1652 |
goto exit; |
| 1653 |
gs_erasepage(ctx->pgs); |
| 1654 |
} |
| 1655 |
} |
| 1656 |
|
| 1657 |
/* Set our values in the context, for caller */ |
| 1658 |
if (!ctx->args.notransparency) |
| 1659 |
ctx->page.has_transparency = tracker.transparent; |
| 1660 |
ctx->page.num_spots = spots; |
| 1661 |
ctx->page.has_OP = tracker.has_overprint; |
| 1662 |
|
| 1663 |
/* High level devices do not render overprint */ |
| 1664 |
if (ctx->device_state.HighLevelDevice) |
| 1665 |
ctx->page.has_OP = false; |
| 1666 |
|
| 1667 |
exit: |
| 1668 |
code1 = code; |
| 1669 |
if (fonts_array != NULL) { |
| 1670 |
*fonts_array = tracker.font_array; |
| 1671 |
pdfi_countup(*fonts_array); |
| 1672 |
} |
| 1673 |
|
| 1674 |
if (spots_array != NULL && tracker.spot_dict != NULL && pdfi_dict_entries(tracker.spot_dict) != 0) { |
| 1675 |
pdf_array *new_array = NULL; |
| 1676 |
pdf_name *Key = NULL; |
| 1677 |
pdf_obj *Value = NULL; |
| 1678 |
uint64_t index = 0, a_index = 0; |
| 1679 |
|
| 1680 |
index = pdfi_dict_entries(tracker.spot_dict); |
| 1681 |
|
| 1682 |
code = pdfi_array_alloc(ctx, index, &new_array); |
| 1683 |
if (code < 0) |
| 1684 |
goto error; |
| 1685 |
pdfi_countup(new_array); |
| 1686 |
|
| 1687 |
code = pdfi_dict_first(ctx, tracker.spot_dict, (pdf_obj **)&Key, &Value, &index); |
| 1688 |
while (code >= 0) |
| 1689 |
{ |
| 1690 |
if (pdfi_type_of(Key) == PDF_NAME) { |
| 1691 |
code = pdfi_array_put(ctx, new_array, a_index++, (pdf_obj *)Key); |
| 1692 |
if (code < 0) { |
| 1693 |
pdfi_countdown(new_array); |
| 1694 |
pdfi_countdown(Key); |
| 1695 |
pdfi_countdown(Value); |
| 1696 |
goto error; |
| 1697 |
} |
| 1698 |
} |
| 1699 |
|
| 1700 |
pdfi_countdown(Key); |
| 1701 |
Key = NULL; |
| 1702 |
pdfi_countdown(Value); |
| 1703 |
Value = NULL; |
| 1704 |
code = pdfi_dict_next(ctx, tracker.spot_dict, (pdf_obj **)&Key, &Value, &index); |
| 1705 |
} |
| 1706 |
*spots_array = new_array; |
| 1707 |
} |
| 1708 |
|
| 1709 |
/* Put the Resources dictionary back in cache (or promote it) in case checking the page |
| 1710 |
* created so many cache entires it flushed Resources from cache |
| 1711 |
*/ |
| 1712 |
code = pdfi_dict_knownget_type(ctx, page_dict, "Resources", PDF_DICT, (pdf_obj **)&Resources); |
| 1713 |
if (code > 0) { |
| 1714 |
code = pdfi_cache_object(ctx, (pdf_obj *)Resources); |
| 1715 |
if (code < 0) |
| 1716 |
code = pdfi_set_warning_stop(ctx, code, NULL, W_PDF_CACHE_FAIL, "pdfi_check_Resources", ""); |
| 1717 |
pdfi_countdown(Resources); |
| 1718 |
} |
| 1719 |
else { |
| 1720 |
if (code < 0) { |
| 1721 |
if (code == gs_error_undefined) |
| 1722 |
code = 0; |
| 1723 |
else |
| 1724 |
code = pdfi_set_error_stop(ctx, code, NULL, E_PDF_BAD_PAGE_RESOURCES, "pdfi_check_page", NULL); |
| 1725 |
} |
| 1726 |
} |
| 1727 |
|
| 1728 |
error: |
| 1729 |
(void)pdfi_check_free_tracker(ctx, &tracker); |
| 1730 |
if (code1 < 0) |
| 1731 |
return code1; |
| 1732 |
return code; |
| 1733 |
} |