1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
|
/* Copyright (C) 2019-2021 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied,
modified or distributed except as expressly authorized under the terms
of the license contained in the file LICENSE in this distribution.
Refer to licensing information at http://www.artifex.com or contact
Artifex Software, Inc., 1305 Grant Avenue - Suite 200, Novato,
CA 94945, U.S.A., +1(415)492-9861, for further information.
*/
/* Checks for transparency and spots for the PDF interpreter */
#include "pdf_int.h"
#include "pdf_stack.h"
#include "pdf_page.h"
#include "pdf_file.h"
#include "pdf_dict.h"
#include "pdf_array.h"
#include "pdf_loop_detect.h"
#include "pdf_colour.h"
#include "pdf_trans.h"
#include "pdf_gstate.h"
#include "pdf_misc.h"
#include "pdf_check.h"
#include "pdf_device.h"
#include "gspaint.h" /* For gs_erasepage() */
/* For performance and resource reasons we do not want to install the transparency blending
* compositor unless we need it. Similarly, if a device handles spot colours it can minimise
* memory usage if it knows ahead of time how many spot colours there will be.
*
* The PDF interpreter written in PostScript performed these as two separate tasks, on opening
* a PDF file it would count spot colour usage and then for each page it would chek if the page
* used any transparency. The code below is used to check for both transparency and spot colours.
* If the int pointer to num_spots is NULL then we aren't interested in spot colours (not supported
* by the device), if the int pointer to transparent is NULL then we aren't interested in transparency
* (-dNOTRANSPARENCY is set).
*
* Currently the code is run when we open the PDF file, the existence of transparency on any given
* page is recorded in a bit array for later use. If it turns out that checking every page when we
* open the file is a performance burden then we could do it on a per-page basis instead. NB if
* the device supports spot colours then we do need to scan the file before starting any page.
*
* The technique is fairly straight-forward, we start with each page, and open its Resources
* dictionary, we then check by type each possible resource. Some resources (eg Pattern, XObject)
* can themselves contain Resources, in which case we recursively check that dictionary. Note; not
* all Resource types need to be checked for both transparency and spot colours, some types can
* only contain one or the other.
*
* Routines with the name pdfi_check_xxx_dict are intended to check a Resource dictionary entry, this
* will be a dictioanry of names and values, where the values are objects of the given Resource type.
*
*/
/* Structure for keeping track of resources as they are being checked */
/* CheckedResources is a bit of a hack, for the file Bug697655.pdf. That file has many (unused)
* XObjects which use Resources and the Resources use other XObjects and so on nested
* very deeply. This takes *hours* to check. Ghostscript gets around this because it
* stores all the objects (which we don't want to do because its wasteful) and checking
* to see if its already tested a given resource for spots/transparency.
* This is a temporary allocation, big enough to hold all the objects in the file (1 per bit)
* each time we have fully checked a resource we add it here, when checking a resoruce we
* first check this list to see if its already been checked, in which case we can skip
* it. When done we release the memory.
*/
typedef struct {
bool transparent;
bool has_overprint; /* Does it have OP or op in an ExtGState? */
pdf_dict *spot_dict;
uint32_t size;
byte *CheckedResources;
} pdfi_check_tracker_t;
static int pdfi_check_Resources(pdf_context *ctx, pdf_dict *Resources_dict, pdf_dict *page_dict, pdfi_check_tracker_t *tracker);
static inline bool resource_is_checked(pdfi_check_tracker_t *tracker, pdf_obj *o)
{
uint32_t byte_offset;
byte bit_offset;
if(tracker->CheckedResources == NULL)
return 0;
/* objects with object number 0 are directly defined, we can't
* store those so just return immediately
*/
if (o->object_num > 0 && (o->object_num >> 3) < tracker->size) {
/* CheckedResources is a byte array, each byte represents
* 8 objects. So the object number / 8 is the byte offset
* into the array, and then object number % 8 is the bit
* within that byte that we want.
*/
bit_offset = 0x01 << (o->object_num % 8);
byte_offset = o->object_num >> 3;
/* If its already set, then return that. */
if (tracker->CheckedResources[byte_offset] & bit_offset)
return true;
else
/* Otherwise set it for futre reference */
tracker->CheckedResources[byte_offset] |= bit_offset;
}
return false;
}
static int
pdfi_check_free_tracker(pdf_context *ctx, pdfi_check_tracker_t *tracker)
{
gs_free_object(ctx->memory, tracker->CheckedResources, "pdfi_check_free_tracker(flags)");
pdfi_countdown(tracker->spot_dict);
memset(tracker, 0, sizeof(*tracker));
return 0;
}
static int
pdfi_check_init_tracker(pdf_context *ctx, pdfi_check_tracker_t *tracker)
{
int code = 0;
memset(tracker, 0, sizeof(*tracker));
tracker->size = (ctx->xref_table->xref_size + 7) / 8;
tracker->CheckedResources = gs_alloc_bytes(ctx->memory, tracker->size,
"pdfi_check_init_tracker(flags)");
if (tracker->CheckedResources == NULL)
return_error(gs_error_VMerror);
memset(tracker->CheckedResources, 0x00, tracker->size);
if (ctx->device_state.spot_capable || ctx->args.overprint_control == PDF_OVERPRINT_SIMULATE) {
code = pdfi_dict_alloc(ctx, 32, &tracker->spot_dict);
if (code < 0)
goto cleanup;
pdfi_countup(tracker->spot_dict);
}
return 0;
cleanup:
pdfi_check_free_tracker(ctx, tracker);
return code;
}
/*
* Check the Resources dictionary ColorSpace entry. pdfi_check_ColorSpace_for_spots is defined
* in pdf_colour.c
*/
static int pdfi_check_ColorSpace_dict(pdf_context *ctx, pdf_dict *cspace_dict,
pdf_dict *page_dict, pdfi_check_tracker_t *tracker)
{
int code;
uint64_t i, index;
pdf_obj *Key = NULL, *Value = NULL;
if (resource_is_checked(tracker, (pdf_obj *)cspace_dict))
return 0;
if (pdfi_dict_entries(cspace_dict) > 0) {
code = pdfi_loop_detector_mark(ctx); /* Mark the start of the ColorSpace dictionary loop */
if (code < 0)
return code;
code = pdfi_dict_first(ctx, cspace_dict, &Key, &Value, &index);
if (code < 0)
goto error1;
i = 1;
do {
code = pdfi_check_ColorSpace_for_spots(ctx, Value, cspace_dict, page_dict, tracker->spot_dict);
if (code < 0)
goto error2;
pdfi_countdown(Key);
Key = NULL;
pdfi_countdown(Value);
Value = NULL;
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the Shading dictionary loop */
code = pdfi_loop_detector_mark(ctx); /* Mark the new start of the Shading dictionary loop */
if (code < 0)
goto error1;
do {
if (i++ >= pdfi_dict_entries(cspace_dict)) {
code = 0;
goto transparency_exit;
}
code = pdfi_dict_next(ctx, cspace_dict, &Key, &Value, &index);
if (code == 0 && Value->type == PDF_ARRAY)
break;
pdfi_countdown(Key);
Key = NULL;
pdfi_countdown(Value);
Value = NULL;
} while(1);
}while (1);
}
return 0;
transparency_exit:
error2:
pdfi_countdown(Key);
pdfi_countdown(Value);
error1:
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */
return code;
}
/*
* Process an individual Shading dictionary to see if it contains a ColorSpace with a spot colour
*/
static int pdfi_check_Shading(pdf_context *ctx, pdf_obj *shading,
pdf_dict *page_dict, pdfi_check_tracker_t *tracker)
{
int code;
pdf_obj *o = NULL;
pdf_dict *shading_dict = NULL;
if (resource_is_checked(tracker, shading))
return 0;
code = pdfi_dict_from_obj(ctx, shading, &shading_dict);
if (code < 0)
return code;
code = pdfi_dict_knownget(ctx, shading_dict, "ColorSpace", (pdf_obj **)&o);
if (code > 0) {
code = pdfi_check_ColorSpace_for_spots(ctx, o, shading_dict, page_dict, tracker->spot_dict);
pdfi_countdown(o);
return code;
}
return 0;
}
/*
* Check the Resources dictionary Shading entry.
*/
static int pdfi_check_Shading_dict(pdf_context *ctx, pdf_dict *shading_dict,
pdf_dict *page_dict, pdfi_check_tracker_t *tracker)
{
int code;
uint64_t i, index;
pdf_obj *Key = NULL, *Value = NULL;
if (resource_is_checked(tracker, (pdf_obj *)shading_dict))
return 0;
if (pdfi_dict_entries(shading_dict) > 0) {
code = pdfi_loop_detector_mark(ctx); /* Mark the start of the Shading dictionary loop */
if (code < 0)
return code;
code = pdfi_dict_first(ctx, shading_dict, &Key, &Value, &index);
if (code < 0 || !(Value->type == PDF_DICT || Value->type == PDF_STREAM))
goto error1;
i = 1;
do {
code = pdfi_check_Shading(ctx, Value, page_dict, tracker);
if (code < 0)
goto error2;
pdfi_countdown(Key);
Key = NULL;
pdfi_countdown(Value);
Value = NULL;
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the Shading dictionary loop */
code = pdfi_loop_detector_mark(ctx); /* Mark the new start of the Shading dictionary loop */
if (code < 0)
goto error1;
do {
if (i++ >= pdfi_dict_entries(shading_dict)) {
code = 0;
goto transparency_exit;
}
code = pdfi_dict_next(ctx, shading_dict, &Key, &Value, &index);
if (code == 0 && Value->type == PDF_DICT)
break;
pdfi_countdown(Key);
Key = NULL;
pdfi_countdown(Value);
Value = NULL;
} while(1);
}while (1);
}
return 0;
transparency_exit:
error2:
pdfi_countdown(Key);
pdfi_countdown(Value);
error1:
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */
return code;
}
/*
* This routine checks an XObject to see if it contains any spot
* colour definitions, or transparency usage.
*/
static int pdfi_check_XObject(pdf_context *ctx, pdf_dict *xobject, pdf_dict *page_dict,
pdfi_check_tracker_t *tracker)
{
int code = 0;
pdf_name *n = NULL;
bool known = false;
double f;
if (resource_is_checked(tracker, (pdf_obj *)xobject))
return 0;
code = pdfi_dict_get_type(ctx, xobject, "Subtype", PDF_NAME, (pdf_obj **)&n);
if (code >= 0) {
if (pdfi_name_is((const pdf_name *)n, "Image")) {
pdf_obj *CS = NULL;
pdfi_countdown(n);
n = NULL;
code = pdfi_dict_known(ctx, xobject, "SMask", &known);
if (code >= 0) {
if (known == true) {
tracker->transparent = true;
if (tracker->spot_dict == NULL)
goto transparency_exit;
}
code = pdfi_dict_knownget_number(ctx, xobject, "SMaskInData", &f);
if (code > 0) {
code = 0;
if (f != 0.0)
tracker->transparent = true;
if (tracker->spot_dict == NULL)
goto transparency_exit;
}
/* Check the image dictionary for a ColorSpace entry, if we are checking spot names */
if (tracker->spot_dict) {
code = pdfi_dict_knownget(ctx, xobject, "ColorSpace", (pdf_obj **)&CS);
if (code > 0) {
/* We don't care if there's an error here, it'll be picked up if we use the ColorSpace later */
(void)pdfi_check_ColorSpace_for_spots(ctx, CS, xobject, page_dict, tracker->spot_dict);
pdfi_countdown(CS);
}
}
}
} else {
if (pdfi_name_is((const pdf_name *)n, "Form")) {
pdf_dict *group_dict = NULL, *resource_dict = NULL;
pdf_obj *CS = NULL;
pdfi_countdown(n);
code = pdfi_dict_knownget_type(ctx, xobject, "Group", PDF_DICT, (pdf_obj **)&group_dict);
if (code > 0) {
tracker->transparent = true;
if (tracker->spot_dict == NULL) {
pdfi_countdown(group_dict);
goto transparency_exit;
}
/* Start a new loop detector group to avoid this being detected in the Resources check below */
code = pdfi_loop_detector_mark(ctx); /* Mark the start of the XObject dictionary loop */
if (code == 0) {
code = pdfi_dict_knownget(ctx, group_dict, "CS", &CS);
if (code > 0)
/* We don't care if there's an error here, it'll be picked up if we use the ColorSpace later */
(void)pdfi_check_ColorSpace_for_spots(ctx, CS, group_dict, page_dict, tracker->spot_dict);
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the XObject dictionary loop */
}
pdfi_countdown(group_dict);
pdfi_countdown(CS);
}
code = pdfi_dict_knownget_type(ctx, xobject, "Resources", PDF_DICT, (pdf_obj **)&resource_dict);
if (code > 0) {
code = pdfi_check_Resources(ctx, resource_dict, page_dict, tracker);
pdfi_countdown(resource_dict);
if (code < 0)
goto transparency_exit;
}
} else
pdfi_countdown(n);
}
}
return 0;
transparency_exit:
return code;
}
/*
* Check the Resources dictionary XObject entry.
*/
static int pdfi_check_XObject_dict(pdf_context *ctx, pdf_dict *xobject_dict, pdf_dict *page_dict,
pdfi_check_tracker_t *tracker)
{
int code;
uint64_t i, index;
pdf_obj *Key = NULL, *Value = NULL;
pdf_dict *Value_dict = NULL;
if (resource_is_checked(tracker, (pdf_obj *)xobject_dict))
return 0;
if (pdfi_dict_entries(xobject_dict) > 0) {
code = pdfi_loop_detector_mark(ctx); /* Mark the start of the XObject dictionary loop */
if (code < 0)
return code;
code = pdfi_dict_first(ctx, xobject_dict, &Key, &Value, &index);
if (code < 0)
goto error_exit;
if (Value->type != PDF_STREAM)
goto error_exit;
i = 1;
do {
code = pdfi_dict_from_obj(ctx, Value, &Value_dict);
if (code < 0)
goto error_exit;
code = pdfi_check_XObject(ctx, Value_dict, page_dict, tracker);
if (code < 0)
goto error_exit;
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the XObject dictionary loop */
code = pdfi_loop_detector_mark(ctx); /* Mark the new start of the XObject dictionary loop */
if (code < 0)
goto error_exit;
pdfi_countdown(Key);
Key = NULL;
pdfi_countdown(Value);
Value = NULL;
Value_dict = NULL;
do {
if (i++ >= pdfi_dict_entries(xobject_dict)) {
code = 0;
goto transparency_exit;
}
code = pdfi_dict_next(ctx, xobject_dict, &Key, &Value, &index);
if (code == 0 && Value->type == PDF_STREAM)
break;
pdfi_countdown(Key);
Key = NULL;
pdfi_countdown(Value);
Value = NULL;
} while(1);
}while(1);
}
return 0;
transparency_exit:
error_exit:
pdfi_countdown(Key);
pdfi_countdown(Value);
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */
return code;
}
/*
* This routine checks an ExtGState dictionary to see if it contains transparency or OP
*/
static int pdfi_check_ExtGState(pdf_context *ctx, pdf_dict *extgstate_dict, pdf_dict *page_dict,
pdfi_check_tracker_t *tracker)
{
int code;
pdf_obj *o = NULL;
double f;
bool overprint;
if (resource_is_checked(tracker, (pdf_obj *)extgstate_dict))
return 0;
if (pdfi_dict_entries(extgstate_dict) > 0) {
/* See if /OP or /op is true */
code = pdfi_dict_get_bool(ctx, extgstate_dict, "OP", &overprint);
if (code == 0 && overprint)
tracker->has_overprint = true;
code = pdfi_dict_get_bool(ctx, extgstate_dict, "op", &overprint);
if (code == 0 && overprint)
tracker->has_overprint = true;
/* Check SMask */
code = pdfi_dict_knownget(ctx, extgstate_dict, "SMask", &o);
if (code > 0) {
if (o->type == PDF_NAME) {
if (!pdfi_name_is((pdf_name *)o, "None")) {
pdfi_countdown(o);
tracker->transparent = true;
return 0;
}
} else {
if (o->type == PDF_DICT) {
pdf_obj *G = NULL;
tracker->transparent = true;
if (tracker->spot_dict != NULL) {
/* Check if the SMask has a /G (Group) */
code = pdfi_dict_knownget(ctx, (pdf_dict *)o, "G", &G);
if (code > 0) {
code = pdfi_check_XObject(ctx, (pdf_dict *)G, page_dict,
tracker);
pdfi_countdown(G);
}
}
pdfi_countdown(o);
return code;
}
}
}
pdfi_countdown(o);
o = NULL;
code = pdfi_dict_knownget_type(ctx, extgstate_dict, "BM", PDF_NAME, &o);
if (code > 0) {
if (!pdfi_name_is((pdf_name *)o, "Normal")) {
if (!pdfi_name_is((pdf_name *)o, "Compatible")) {
pdfi_countdown(o);
tracker->transparent = true;
return 0;
}
}
}
pdfi_countdown(o);
o = NULL;
code = pdfi_dict_knownget_number(ctx, extgstate_dict, "CA", &f);
if (code > 0) {
if (f != 1.0) {
tracker->transparent = true;
return 0;
}
}
code = pdfi_dict_knownget_number(ctx, extgstate_dict, "ca", &f);
if (code > 0) {
if (f != 1.0) {
tracker->transparent = true;
return 0;
}
}
}
return 0;
}
/*
* Check the Resources dictionary ExtGState entry.
*/
static int pdfi_check_ExtGState_dict(pdf_context *ctx, pdf_dict *extgstate_dict, pdf_dict *page_dict,
pdfi_check_tracker_t *tracker)
{
int code;
uint64_t i, index;
pdf_obj *Key = NULL, *Value = NULL;
if (resource_is_checked(tracker, (pdf_obj *)extgstate_dict))
return 0;
if (pdfi_dict_entries(extgstate_dict) > 0) {
code = pdfi_loop_detector_mark(ctx); /* Mark the start of the ColorSpace dictionary loop */
if (code < 0)
return code;
code = pdfi_dict_first(ctx, extgstate_dict, &Key, &Value, &index);
if (code < 0)
goto error1;
i = 1;
do {
(void)pdfi_check_ExtGState(ctx, (pdf_dict *)Value, page_dict, tracker);
if (tracker->transparent == true && tracker->spot_dict == NULL)
goto transparency_exit;
pdfi_countdown(Key);
Key = NULL;
pdfi_countdown(Value);
Value = NULL;
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the ExtGState dictionary loop */
code = pdfi_loop_detector_mark(ctx); /* Mark the new start of the ExtGState dictionary loop */
if (code < 0)
goto error1;
do {
if (i++ >= pdfi_dict_entries(extgstate_dict)) {
code = 0;
goto transparency_exit;
}
code = pdfi_dict_next(ctx, extgstate_dict, &Key, &Value, &index);
if (code == 0 && Value->type == PDF_DICT)
break;
pdfi_countdown(Key);
Key = NULL;
pdfi_countdown(Value);
Value = NULL;
} while(1);
}while (1);
}
return 0;
transparency_exit:
pdfi_countdown(Key);
pdfi_countdown(Value);
error1:
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */
return code;
}
/*
* This routine checks a Pattern dictionary to see if it contains any spot
* colour definitions, or transparency usage.
*/
static int pdfi_check_Pattern(pdf_context *ctx, pdf_dict *pattern, pdf_dict *page_dict,
pdfi_check_tracker_t *tracker)
{
int code = 0;
pdf_obj *o = NULL;
if (resource_is_checked(tracker, (pdf_obj *)pattern))
return 0;
if (tracker->spot_dict != NULL) {
code = pdfi_dict_knownget(ctx, pattern, "Shading", &o);
if (code > 0)
(void)pdfi_check_Shading(ctx, o, page_dict, tracker);
pdfi_countdown(o);
o = NULL;
}
code = pdfi_dict_knownget_type(ctx, pattern, "Resources", PDF_DICT, &o);
if (code > 0)
(void)pdfi_check_Resources(ctx, (pdf_dict *)o, page_dict, tracker);
pdfi_countdown(o);
o = NULL;
if (tracker->transparent == true && tracker->spot_dict == NULL)
goto transparency_exit;
code = pdfi_dict_knownget_type(ctx, pattern, "ExtGState", PDF_DICT, &o);
if (code > 0)
(void)pdfi_check_ExtGState(ctx, (pdf_dict *)o, page_dict, tracker);
pdfi_countdown(o);
o = NULL;
transparency_exit:
return 0;
}
/*
* This routine checks a Pattern dictionary for transparency.
*/
int pdfi_check_Pattern_transparency(pdf_context *ctx, pdf_dict *pattern, pdf_dict *page_dict,
bool *transparent)
{
int code;
pdfi_check_tracker_t tracker = {0, 0, NULL, 0, NULL};
/* NOTE: We use a "null" tracker that won't do any optimization to prevent
* checking the same resource twice.
* This means we don't get the optimization, but we also don't have the overhead
* of setting it up.
* If we do want the optimization, call pdfi_check_init_tracker() and
* pdfi_check_free_tracker() as in pdfi_check_page(), below.
*/
code = pdfi_check_Pattern(ctx, pattern, page_dict, &tracker);
if (code == 0)
*transparent = tracker.transparent;
else
*transparent = false;
return code;
}
/*
* Check the Resources dictionary Pattern entry.
*/
static int pdfi_check_Pattern_dict(pdf_context *ctx, pdf_dict *pattern_dict, pdf_dict *page_dict,
pdfi_check_tracker_t *tracker)
{
int code;
uint64_t i, index;
pdf_obj *Key = NULL, *Value = NULL;
pdf_dict *instance_dict = NULL;
if (resource_is_checked(tracker, (pdf_obj *)pattern_dict))
return 0;
if (pdfi_dict_entries(pattern_dict) > 0) {
code = pdfi_loop_detector_mark(ctx); /* Mark the start of the Pattern dictionary loop */
if (code < 0)
return code;
code = pdfi_dict_first(ctx, pattern_dict, &Key, &Value, &index);
if (code < 0)
goto error1;
if(Value->type != PDF_DICT && Value->type != PDF_STREAM)
goto transparency_exit;
i = 1;
do {
code = pdfi_dict_from_obj(ctx, Value, &instance_dict);
if (code < 0)
goto transparency_exit;
code = pdfi_check_Pattern(ctx, instance_dict, page_dict, tracker);
if (code < 0)
goto transparency_exit;
pdfi_countdown(Key);
Key = NULL;
pdfi_countdown(Value);
instance_dict = NULL;
Value = NULL;
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the Shading dictionary loop */
code = pdfi_loop_detector_mark(ctx); /* Mark the new start of the Shading dictionary loop */
if (code < 0)
goto error1;
do {
if (i++ >= pdfi_dict_entries(pattern_dict)) {
code = 0;
goto transparency_exit;
}
code = pdfi_dict_next(ctx, pattern_dict, &Key, &Value, &index);
if (code == 0 && (Value->type == PDF_DICT || Value->type == PDF_STREAM))
break;
pdfi_countdown(Key);
Key = NULL;
pdfi_countdown(Value);
Value = NULL;
} while(1);
}while (1);
}
return 0;
transparency_exit:
pdfi_countdown(Key);
pdfi_countdown(Value);
pdfi_countdown(instance_dict);
error1:
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */
return code;
}
/*
* This routine checks a Font dictionary to see if it contains any spot
* colour definitions, or transparency usage.
*/
static int pdfi_check_Font(pdf_context *ctx, pdf_dict *font, pdf_dict *page_dict,
pdfi_check_tracker_t *tracker)
{
int code = 0;
pdf_obj *o = NULL;
if (resource_is_checked(tracker, (pdf_obj *)font))
return 0;
if (font->type != PDF_DICT)
return_error(gs_error_typecheck);
code = pdfi_dict_knownget_type(ctx, font, "Subtype", PDF_NAME, &o);
if (code > 0) {
if (pdfi_name_is((pdf_name *)o, "Type3")) {
pdfi_countdown(o);
o = NULL;
code = pdfi_dict_knownget_type(ctx, font, "Resources", PDF_DICT, &o);
if (code > 0)
(void)pdfi_check_Resources(ctx, (pdf_dict *)o, page_dict, tracker);
}
}
pdfi_countdown(o);
o = NULL;
return 0;
}
/*
* Check the Resources dictionary Font entry.
*/
static int pdfi_check_Font_dict(pdf_context *ctx, pdf_dict *font_dict, pdf_dict *page_dict,
pdfi_check_tracker_t *tracker)
{
int code;
uint64_t i, index;
pdf_obj *Key = NULL, *Value = NULL;
if (resource_is_checked(tracker, (pdf_obj *)font_dict))
return 0;
if (pdfi_dict_entries(font_dict) > 0) {
code = pdfi_loop_detector_mark(ctx); /* Mark the start of the Font dictionary loop */
if (code < 0)
return code;
code = pdfi_dict_first(ctx, font_dict, &Key, &Value, &index);
if (code < 0)
goto error1;
i = 1;
do {
code = pdfi_check_Font(ctx, (pdf_dict *)Value, page_dict, tracker);
pdfi_countdown(Key);
Key = NULL;
pdfi_countdown(Value);
Value = NULL;
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the Font dictionary loop */
code = pdfi_loop_detector_mark(ctx); /* Mark the new start of the Font dictionary loop */
if (code < 0)
goto error1;
do {
if (i++ >= pdfi_dict_entries(font_dict)) {
code = 0;
goto transparency_exit;
}
code = pdfi_dict_next(ctx, font_dict, &Key, &Value, &index);
if (code == 0 && Value->type == PDF_DICT)
break;
pdfi_countdown(Key);
Key = NULL;
pdfi_countdown(Value);
Value = NULL;
} while(1);
}while (1);
}
return 0;
transparency_exit:
pdfi_countdown(Key);
pdfi_countdown(Value);
error1:
(void)pdfi_loop_detector_cleartomark(ctx); /* Clear to the mark for the current resource loop */
return code;
}
static int pdfi_check_Resources(pdf_context *ctx, pdf_dict *Resources_dict,
pdf_dict *page_dict, pdfi_check_tracker_t *tracker)
{
int code;
pdf_obj *d = NULL;
if (resource_is_checked(tracker, (pdf_obj *)Resources_dict))
return 0;
/* First up, check any colour spaces, for new spot colours.
* We only do this if asked because its expensive. spot_dict being NULL
* means we aren't interested in spot colours (not a DeviceN or Separation device)
*/
if (tracker->spot_dict != NULL) {
code = pdfi_dict_knownget_type(ctx, Resources_dict, "ColorSpace", PDF_DICT, &d);
if (code > 0)
(void)pdfi_check_ColorSpace_dict(ctx, (pdf_dict *)d, page_dict, tracker);
pdfi_countdown(d);
d = NULL;
code = pdfi_dict_knownget_type(ctx, Resources_dict, "Shading", PDF_DICT, &d);
if (code > 0)
(void)pdfi_check_Shading_dict(ctx, (pdf_dict *)d, page_dict, tracker);
pdfi_countdown(d);
d = NULL;
}
code = pdfi_dict_knownget_type(ctx, Resources_dict, "XObject", PDF_DICT, &d);
if (code > 0)
(void)pdfi_check_XObject_dict(ctx, (pdf_dict *)d, page_dict, tracker);
pdfi_countdown(d);
d = NULL;
code = pdfi_dict_knownget_type(ctx, Resources_dict, "Pattern", PDF_DICT, &d);
if (code > 0)
(void)pdfi_check_Pattern_dict(ctx, (pdf_dict *)d, page_dict, tracker);
pdfi_countdown(d);
d = NULL;
code = pdfi_dict_knownget_type(ctx, Resources_dict, "Font", PDF_DICT, &d);
if (code > 0)
(void)pdfi_check_Font_dict(ctx, (pdf_dict *)d, page_dict, tracker);
/* From this point onwards, if we detect transparency (or have already detected it) we
* can exit, we have already counted up any spot colours.
*/
pdfi_countdown(d);
d = NULL;
code = pdfi_dict_knownget_type(ctx, Resources_dict, "ExtGState", PDF_DICT, &d);
if (code > 0)
(void)pdfi_check_ExtGState_dict(ctx, (pdf_dict *)d, page_dict, tracker);
pdfi_countdown(d);
d = NULL;
return 0;
}
static int pdfi_check_annot_for_transparency(pdf_context *ctx, pdf_dict *annot, pdf_dict *page_dict,
pdfi_check_tracker_t *tracker)
{
int code;
pdf_name *n;
pdf_obj *N = NULL;
pdf_dict *ap = NULL;
pdf_dict *Resources = NULL;
double f;
if (resource_is_checked(tracker, (pdf_obj *)annot))
return 0;
/* Check #1 Does the (Normal) Appearnce stream use any Resources which include transparency.
* We check this first, because this also checks for spot colour spaces. Once we've done that we
* can exit the checks as soon as we detect transparency.
*/
code = pdfi_dict_knownget_type(ctx, annot, "AP", PDF_DICT, (pdf_obj **)&ap);
if (code > 0)
{
/* Fetch without resolving indirect ref because pdfmark wants it that way later */
code = pdfi_dict_get_no_store_R(ctx, ap, "N", (pdf_obj **)&N);
if (code >= 0) {
pdf_dict *dict = NULL;
code = pdfi_dict_from_obj(ctx, N, &dict);
if (code == 0)
code = pdfi_dict_knownget_type(ctx, dict, "Resources", PDF_DICT, (pdf_obj **)&Resources);
if (code > 0)
code = pdfi_check_Resources(ctx, (pdf_dict *)Resources, page_dict, tracker);
}
if (code == gs_error_undefined)
code = 0;
}
pdfi_countdown(ap);
pdfi_countdown(N);
pdfi_countdown(Resources);
if (code < 0)
return code;
/* We've checked the Resources, and nothing else in an annotation can define spot colours, so
* if we detected transparency in the Resources we need not check further.
*/
if (tracker->transparent == true)
return 0;
code = pdfi_dict_get_type(ctx, annot, "Subtype", PDF_NAME, (pdf_obj **)&n);
if (code < 0) {
if (ctx->args.pdfstoponerror)
return code;
} else {
/* Check #2, Highlight annotations are always preformed with transparency */
if (pdfi_name_is((const pdf_name *)n, "Highlight")) {
pdfi_countdown(n);
tracker->transparent = true;
return 0;
}
pdfi_countdown(n);
n = NULL;
/* Check #3 Blend Mode (BM) not being 'Normal' or 'Compatible' */
code = pdfi_dict_knownget_type(ctx, annot, "BM", PDF_NAME, (pdf_obj **)&n);
if (code > 0) {
if (!pdfi_name_is((const pdf_name *)n, "Normal")) {
if (!pdfi_name_is((const pdf_name *)n, "Compatible")) {
pdfi_countdown(n);
tracker->transparent = true;
return 0;
}
}
code = 0;
}
pdfi_countdown(n);
if (code < 0)
return code;
/* Check #4 stroke constant alpha (CA) is not 1 (100% opaque) */
code = pdfi_dict_knownget_number(ctx, annot, "CA", &f);
if (code > 0) {
if (f != 1.0) {
tracker->transparent = true;
return 0;
}
}
if (code < 0)
return code;
/* Check #5 non-stroke constant alpha (ca) is not 1 (100% opaque) */
code = pdfi_dict_knownget_number(ctx, annot, "ca", &f);
if (code > 0) {
if (f != 1.0) {
tracker->transparent = true;
return 0;
}
}
if (code < 0)
return code;
}
return 0;
}
static int pdfi_check_Annots_for_transparency(pdf_context *ctx, pdf_array *annots_array,
pdf_dict *page_dict, pdfi_check_tracker_t *tracker)
{
int i, code = 0;
pdf_dict *annot = NULL;
if (resource_is_checked(tracker, (pdf_obj *)annots_array))
return 0;
for (i=0; i < pdfi_array_size(annots_array); i++) {
code = pdfi_array_get_type(ctx, annots_array, (uint64_t)i, PDF_DICT, (pdf_obj **)&annot);
if (code >= 0) {
code = pdfi_check_annot_for_transparency(ctx, annot, page_dict, tracker);
if (code < 0 && ctx->args.pdfstoponerror)
goto exit;
/* If we've found transparency, and don't need to continue checkign for spot colours
* just exit as fast as possible.
*/
if (tracker->transparent == true && tracker->spot_dict == NULL)
goto exit;
pdfi_countdown(annot);
annot = NULL;
}
if (code < 0 && ctx->args.pdfstoponerror)
goto exit;
code = 0;
}
exit:
pdfi_countdown(annot);
return code;
}
/* Check for transparency and spots on page.
*
* Sets ctx->spot_capable_device
* Builds a dictionary of the unique spot names in spot_dict
* Set 'transparent' to true if there is transparency on the page
*
* From the original PDF interpreter written in PostScript:
* Note: we deliberately don't check to see whether a Group is defined,
* because Adobe Illustrator 10 (and possibly other applications) define
* a page-level group whether transparency is actually used or not.
* Ignoring the presence of Group is justified because, in the absence
* of any other transparency features, they have no effect.
*/
static int pdfi_check_page_inner(pdf_context *ctx, pdf_dict *page_dict,
pdfi_check_tracker_t *tracker)
{
int code;
pdf_dict *Resources = NULL;
pdf_array *Annots = NULL;
pdf_dict *Group = NULL;
pdf_obj *CS = NULL;
tracker->transparent = false;
/* Check if the page dictionary has a page Group entry (for spots).
* Page group should mean the page has transparency but we ignore it for the purposes
* of transparency detection. See above.
*/
if (tracker->spot_dict) {
code = pdfi_dict_knownget_type(ctx, page_dict, "Group", PDF_DICT, (pdf_obj **)&Group);
if (code > 0) {
/* If Group has a ColorSpace (CS), then check it for spot colours */
code = pdfi_dict_knownget(ctx, Group, "CS", &CS);
if (code > 0)
code = pdfi_check_ColorSpace_for_spots(ctx, CS, Group, page_dict, tracker->spot_dict);
if (code < 0 && ctx->args.pdfstoponerror)
goto exit;
}
}
/* Now check any Resources dictionary in the Page dictionary */
code = pdfi_dict_knownget_type(ctx, page_dict, "Resources", PDF_DICT, (pdf_obj **)&Resources);
if (code > 0)
code = pdfi_check_Resources(ctx, Resources, page_dict, tracker);
if ((code < 0 && ctx->args.pdfstoponerror) || (code == gs_error_stackoverflow))
goto exit;
/* If we are drawing Annotations, check to see if the page uses any Annots */
if (ctx->args.showannots) {
code = pdfi_dict_knownget_type(ctx, page_dict, "Annots", PDF_ARRAY, (pdf_obj **)&Annots);
if (code > 0)
code = pdfi_check_Annots_for_transparency(ctx, Annots, page_dict,
tracker);
if (code < 0 && ctx->args.pdfstoponerror)
goto exit;
}
code = 0;
exit:
pdfi_countdown(Resources);
pdfi_countdown(Annots);
pdfi_countdown(CS);
pdfi_countdown(Group);
return code;
}
/* Checks page for transparency, and sets up device for spots, if applicable
* Sets ctx->page.has_transparency and ctx->page.num_spots
* do_setup -- indicates whether to actually set up the device with the spot count.
*/
int pdfi_check_page(pdf_context *ctx, pdf_dict *page_dict, bool do_setup)
{
int code;
int spots = 0;
pdfi_check_tracker_t tracker;
ctx->page.num_spots = 0;
ctx->page.has_transparency = false;
/* Need to do this here so that pdfi_check_init_tracker will be setup
* for spot colours, if the device is spot colour capable.
* It is also called in pdfi_page_render() and pdfi_process_pdf_file()
* TODO: Should probably look into that..
*/
pdfi_device_set_flags(ctx);
code = pdfi_check_init_tracker(ctx, &tracker);
/* Check for spots and transparency in this page */
code = pdfi_check_page_inner(ctx, page_dict, &tracker);
if (code < 0)
goto exit;
/* Count the spots */
if (tracker.spot_dict)
spots = pdfi_dict_entries(tracker.spot_dict);
/* If setup requested, tell the device about spots and transparency */
if (do_setup) {
gs_c_param_list list;
gs_c_param_list_write(&list, ctx->memory);
/* If there are spot colours (and by inference, the device renders spot plates) then
* send the number of Spots to the device, so it can setup correctly.
*/
if (tracker.spot_dict)
param_write_int((gs_param_list *)&list, "PageSpotColors", &spots);
code = param_write_bool((gs_param_list *)&list, "PageUsesTransparency",
&tracker.transparent);
gs_c_param_list_read(&list);
code = gs_putdeviceparams(ctx->pgs->device, (gs_param_list *)&list);
gs_c_param_list_release(&list);
if (code > 0) {
/* The device was closed, we need to reopen it */
code = gs_setdevice_no_erase(ctx->pgs, ctx->pgs->device);
if (code < 0)
goto exit;
gs_erasepage(ctx->pgs);
}
}
/* Set our values in the context, for caller */
ctx->page.has_transparency = tracker.transparent;
ctx->page.num_spots = spots;
ctx->page.has_OP = tracker.has_overprint;
exit:
(void)pdfi_check_free_tracker(ctx, &tracker);
return code;
}
|