File: gdevpdft.c

package info (click to toggle)
ghostscript 10.07.0~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 96,244 kB
  • sloc: ansic: 946,318; python: 7,917; cpp: 6,534; cs: 6,457; sh: 6,168; java: 4,028; perl: 2,373; tcl: 1,639; makefile: 539; awk: 66; yacc: 18
file content (753 lines) | stat: -rw-r--r-- 28,326 bytes parent folder | download
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
/* Copyright (C) 2001-2026 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.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/


/* transparency processing for PDF-writing driver */
#include "gx.h"
#include "string_.h"
#include "gserrors.h"
#include "gstrans.h"
#include "gscolor2.h"
#include "gzstate.h"
#include "gdevpdfx.h"
#include "gdevpdfg.h"
#include "gdevpdfo.h"
#include "gsccolor.h"
#include "gsicc_manage.h"
#include "gdevpdfc.h"

static int
pdf_make_soft_mask_dict(gx_device_pdf * pdev, const gs_pdf14trans_params_t * pparams)
{
    pdf_resource_t *pres_soft_mask_dict = 0;
    cos_dict_t *soft_mask_dict;
    int code;

    /* Fixme : merge redundant objects. */
    code = pdf_alloc_resource(pdev, resourceSoftMaskDict, gs_no_id, &pres_soft_mask_dict, -1);
    if (code < 0)
        return code;
    cos_become(pres_soft_mask_dict->object, cos_type_dict);
    pdev->pres_soft_mask_dict = pres_soft_mask_dict;
    soft_mask_dict = (cos_dict_t *)pres_soft_mask_dict->object;
    code = cos_dict_put_c_key_string(soft_mask_dict, "/S",
            pparams->subtype == TRANSPARENCY_MASK_Alpha ? (byte *)"/Alpha" : (byte *)"/Luminosity",
            pparams->subtype == TRANSPARENCY_MASK_Alpha ? 6 : 11);
    if (code < 0)
        return code;
    if (pparams->Background_components) {
        cos_array_t *Background;

        Background = cos_array_from_floats(pdev, pparams->Background,
                    pparams->Background_components, "pdf_write_soft_mask_dict");
        if (Background == NULL)
            return_error(gs_error_VMerror);
        code = cos_dict_put_c_key_object(soft_mask_dict, "/BC", (cos_object_t *)Background);
        if (code < 0)
            return code;
    }
    if (pdev->CompatibilityLevel <= 1.7 && pparams->transfer_function != NULL && pdev->params.TransferFunctionInfo == tfi_Preserve) {
        int64_t id;
        char buf[20];

        code = pdf_write_function(pdev, pparams->transfer_function, &id);
        if (code < 0)
            return code;
        gs_snprintf(buf, sizeof(buf), " %"PRId64" 0 R", id);
        code = cos_dict_put_c_key_string(soft_mask_dict, "/TR", (const byte *)buf, strlen(buf));
        if (code < 0)
            return code;
    }
    return 0;

}

static int WriteDefaultSpaces(gx_device_pdf * pdev, const gs_gstate * pgs, cos_value_t *cs_value)
{
    cmm_profile_t *pprofile = NULL;
    gs_color_space cs, *pcs;
    gs_color_space_type t;
    cos_array_t *pca;
    cos_dict_t *Resources = NULL, *ColorSpaces = NULL;
    int code = 0;
    const cos_value_t *pcos = NULL;
    pdf_color_space_t *ppcs;
    pdf_resource_t *pres = NULL;
    gs_color_space_index target;

    /* Check type before using */
    if (cs_value->value_type != COS_VALUE_CONST)
        return_error(gs_error_typecheck);
    /* Compare its value to find out which space we want */
    if (strncmp((const char *)cs_value->contents.chars.data, "/DeviceGray", 11) == 0)
        target = gs_color_space_index_DeviceGray;
    else
        if (strncmp((const char *)cs_value->contents.chars.data, "/DeviceRGB", 10) == 0)
            target = gs_color_space_index_DeviceRGB;
        else
            if (strncmp((const char *)cs_value->contents.chars.data, "/DeviceCMYK", 11) == 0)
                target = gs_color_space_index_DeviceCMYK;
            else
                return_error(gs_error_rangecheck);

    /* Retrieve/Create the Resources->ColorSpace dictionary in the Pages tree */
    Resources = (cos_dict_t *)cos_dict_find(pdev->Pages, (const byte *)"/Resources", 10);
    if (Resources == NULL) {
        Resources = cos_dict_alloc(pdev, "WriteDefaultSpaces");
        if (Resources == NULL)
            return_error(gs_error_undefined);
        code = cos_dict_put_c_key_object(pdev->Pages, "/Resources", (cos_object_t *)Resources);
        if (code < 0)
            return code;
    }

    /* Then the ColorSpace sub-dictionary */
    ColorSpaces = (cos_dict_t *)cos_dict_find(Resources, (const byte *)"/ColorSpace", 11);
    if(ColorSpaces == NULL) {
        ColorSpaces = cos_dict_alloc(pdev, "WriteDefaultSpaces");
        if (ColorSpaces == NULL)
            return_error(gs_error_undefined);
        code = cos_dict_put_c_key_object(Resources, "/ColorSpace", (cos_object_t *)ColorSpaces);
        if (code < 0)
            return code;
    }

    /* Now check to see if we already have a /Default space, if so just exit. */
    switch(target) {
        case gs_color_space_index_DeviceGray:
            pcos = cos_dict_find_c_key(ColorSpaces, "/DefaultGray");
            break;
        case gs_color_space_index_DeviceRGB:
            pcos = cos_dict_find_c_key(ColorSpaces, "/DefaultRGB");
            break;
        case gs_color_space_index_DeviceCMYK:
            pcos = cos_dict_find_c_key(ColorSpaces, "/DefaultCMYK");
            break;
        default:
            return_error(gs_error_rangecheck);
            break;
    }
    /* If we've already written the relevant Default space, don't write another one! */
    if (pcos != NULL)
        return 0;

    /* This is all a bit hairy, but there seems to be no easy way to achieve this
     * Probably could export some static functions from the ICC code, but this works too. Just manufacture
     * enough of a colour space to retrieve the ICC profile.
     */
    cs.cmm_icc_profile_data = NULL;
    t.index = target;
    cs.type = &t;
    pprofile = gsicc_get_gscs_profile(&cs, pgs->icc_manager);
    if (pprofile == NULL)
        return_error(gs_error_undefined);
    /* gsicc_get_gscs_profile() does not count up the returned reference. We
     * deliberately don't count it up here; we're only using it temporarily.
     */

    /* Now manufacture a colour space to attach the profile to. */
    pcs = gs_cspace_new_ICC(pdev->memory, (gs_gstate *)pgs, 4);
    if (pcs == NULL)
        return_error(gs_error_undefined);

    switch(target) {
        case gs_color_space_index_DeviceGray:
            pcs->ICC_Alternate_space = gs_ICC_Alternate_DeviceGray;
            break;
        case gs_color_space_index_DeviceRGB:
            pcs->ICC_Alternate_space = gs_ICC_Alternate_DeviceRGB;
            break;
        case gs_color_space_index_DeviceCMYK:
            pcs->ICC_Alternate_space = gs_ICC_Alternate_DeviceCMYK;
            break;
        default:
            return_error(gs_error_undefined);
    }

    pcs->cmm_icc_profile_data = pprofile;

    /* Create an array. */
    pca = cos_array_alloc(pdev, "WriteDefaultSpaces");
    if (pca == 0) {
        rc_decrement(pcs, "WriteDefaultSpaces");
        return_error(gs_error_VMerror);
    }

    /* Turn the array in to a PDF colour space, using the GS colour space */
    code = pdf_iccbased_color_space(pdev, pgs, NULL, pcs, pca);
    if (code < 0) {
        rc_decrement(pcs, "WriteDefaultSpaces");
        cos_release((cos_object_t *)pca, "WriteDefaultSpaces");
        return code;
    }

    /*
     * Register the color space as a resource, since it must be referenced
     * by name rather than directly.
     */
    {
        if (code < 0 ||
            (code = pdf_alloc_resource(pdev, resourceColorSpace, pcs->id,
                                       &pres, -1)) < 0
            ) {
            COS_FREE(pca, "pdf_color_space");
            return code;
        }
        pdf_reserve_object_id(pdev, pres, 0);

        switch(target) {
            case gs_color_space_index_DeviceGray:
                memcpy(pres->rname, "DefaultGray", 11);
                pres->rname[11] = 0;
                break;
            case gs_color_space_index_DeviceRGB:
                memcpy(pres->rname, "DefaultRGB", 10);
                pres->rname[10] = 0;
                break;
            case gs_color_space_index_DeviceCMYK:
                memcpy(pres->rname, "DefaultCMYK", 11);
                pres->rname[11] = 0;
                break;
            default:
                 return_error(gs_error_undefined);
        }

        ppcs = (pdf_color_space_t *)pres;
        ppcs->serialized = 0;
        ppcs->serialized_size = 0;
        ppcs->ranges = 0;
        pca->id = pres->object->id;
        COS_FREE(pres->object, "pdf_color_space");
        pres->object = (cos_object_t *)pca;
        cos_write_object(COS_OBJECT(pca), pdev, resourceColorSpace);
    }

    code = pdf_add_resource(pdev, Resources, "/ColorSpace", pres);

    rc_decrement(pcs, "WriteDefaultSpaces");
    cos_release((cos_object_t *)pca, "WriteDefaultSpaces");
    return code;
}

static int
pdf_make_group_dict(gx_device_pdf * pdev, const gs_pdf14trans_params_t * pparams,
                            const gs_gstate * pgs, cos_dict_t **pdict)
{
    pdf_resource_t *pres_group;
    cos_dict_t *group_dict;
    int code = 0;
    cos_value_t cs_value;

    code = pdf_alloc_resource(pdev, resourceGroup, gs_no_id, &pres_group, -1);
    if (code < 0)
        return code;
    cos_become(pres_group->object, cos_type_dict);
    group_dict = (cos_dict_t *)pres_group->object;
    code = cos_dict_put_c_key_string(group_dict, "/Type", (const byte *)"/Group", 6);
    if (code < 0)
        return code;
    code = cos_dict_put_c_key_string(group_dict, "/S", (const byte *)"/Transparency", 13);
    if (code < 0)
        return code;
    if (pparams->Isolated) {
        code = cos_dict_put_c_key_bool(group_dict, "/I", true);
        if (code < 0)
            return code;
    }
    if (pparams->Knockout) {
        code = cos_dict_put_c_key_bool(group_dict, "/K", true);
        if (code < 0)
            return code;
    }
    /* Note that we should not add in the graphic state
       color space for the group color if there was not
       a group color specified.
       In this case, the parent group is inherited from
       the previous group or the device color space */
    if (pgs != NULL && pparams->ColorSpace != NULL) {
        const gs_color_space *cs = pparams->ColorSpace;

        if (pparams->ColorSpace == NULL)
            code = pdf_color_space_named(pdev, pgs, &cs_value, NULL, cs,
                    &pdf_color_space_names, false, NULL, 0, false);
        else
            code = pdf_color_space_named(pdev, pgs, &cs_value, NULL, pparams->ColorSpace,
                    &pdf_color_space_names, false, NULL, 0, false);
        if (code < 0)
            return code;
        switch(pdev->params.BlendConversionStrategy) {
            case bcs_Managed:
                if (pdev->params.ColorConversionStrategy != ccs_LeaveColorUnchanged) {
                    if (pdev->params.ColorConversionStrategy < ccs_CMYK || pdev->params.ColorConversionStrategy == ccs_ByObjectType)
                        return_error(gs_error_rangecheck);
                    code = WriteDefaultSpaces(pdev, pgs, &cs_value);
                }
                if (code < 0)
                    return code;
                /* Fall through */
            case bcs_None:
                code = cos_dict_put_c_key(group_dict, "/CS", &cs_value);
                break;
            case bcs_Simple:
                switch(pdev->params.ColorConversionStrategy) {
                    case ccs_LeaveColorUnchanged:
                        code = cos_dict_put_c_key(group_dict, "/CS", &cs_value);
                        break;
                    case ccs_UseDeviceIndependentColor:
                    case ccs_UseDeviceIndependentColorForImages:
                    case ccs_ByObjectType:
                    case ccs_sRGB:
                        return_error(gs_error_rangecheck);
                        break;

                    case ccs_CMYK:
                        code = cos_dict_put_c_key_string(group_dict, "/CS", (const byte *)"/DeviceCMYK", 11);
                        break;
                    case ccs_Gray:
                        code = cos_dict_put_c_key_string(group_dict, "/CS", (const byte *)"/DeviceGray", 11);
                        break;
                    case ccs_RGB:
                        code = cos_dict_put_c_key_string(group_dict, "/CS", (const byte *)"/DeviceRGB", 10);
                        break;
                }
                break;
        }
    }
    if (code < 0)
        return code;
    group_dict = NULL; /* The next line invalidates it. */
    code = pdf_substitute_resource(pdev, &pres_group, resourceGroup, NULL, false);
    if (code < 0)
        return code;
    pres_group->where_used |= pdev->used_mask;
    *pdict = (cos_dict_t *)pres_group->object;
    return 0;
}

static int
pdf_make_form_dict(gx_device_pdf * pdev, const gs_pdf14trans_params_t * pparams,
                            const gs_gstate * pgs,
                            const cos_dict_t *group_dict, cos_dict_t *form_dict)
{
    cos_array_t *bbox_array;
    float bbox[4];
    gs_rect bbox_rect;
    int code;

    code = gs_bbox_transform(&pparams->bbox, &ctm_only(pgs), &bbox_rect);
    if (code < 0)
        return code;
    bbox[0] = bbox_rect.p.x;
    bbox[1] = bbox_rect.p.y;
    bbox[2] = bbox_rect.q.x;
    bbox[3] = bbox_rect.q.y;
    code = cos_dict_put_c_key_string(form_dict, "/Type", (const byte *)"/XObject", 8);
    if (code < 0)
        return code;
    code = cos_dict_put_c_key_string(form_dict, "/Subtype", (const byte *)"/Form", 5);
    if (code < 0)
        return code;
    code = cos_dict_put_c_key_int(form_dict, "/FormType", 1);
    if (code < 0)
        return code;
    code = cos_dict_put_c_key_string(form_dict, "/Matrix", (const byte *)"[1 0 0 1 0 0]", 13);
    if (code < 0)
        return code;
    bbox_array = cos_array_from_floats(pdev, bbox, 4, "pdf_begin_transparency_group");
    if (bbox_array == NULL)
        return_error(gs_error_VMerror);
    code = cos_dict_put_c_key_object(form_dict, "/BBox", (cos_object_t *)bbox_array);
    if (code < 0)
        return code;
    if (pdev->PendingOC != 0) {
        if (pdev->CompatibilityLevel < 1.4999) {
            if (pdev->PDFA) {
                switch (pdev->PDFACompatibilityPolicy) {
                    case 0:
                        emprintf(pdev->memory,
                                 "Optional Content not valid in this version of PDF, reverting to normal PDF output\n");
                        pdev->AbortPDFAX = true;
                        pdev->PDFA = 0;
                        break;
                    case 1:
                        emprintf(pdev->memory,
                                 "Optional Content not valid in this version of PDF. Dropping feature to preserve PDF/A compatibility\n");
                        break;
                    case 2:
                        emprintf(pdev->memory,
                                 "Optional Content not valid in this version of PDF,  aborting conversion\n");
                        return_error (gs_error_typecheck);
                        break;
                    default:
                        emprintf(pdev->memory,
                                 "Optional Content not valid in this version of PDF, unrecognised PDFACompatibilityLevel,\nreverting to normal PDF output\n");
                        pdev->AbortPDFAX = true;
                        pdev->PDFA = 0;
                        break;
                }
            } else {
                emprintf(pdev->memory,
                         "Optional Content not valid in this version of PDF. Dropping feature to preserve compatibility\n");
            }
        } else {
            char str[256];
            gs_param_string param;
            cos_object_t *pco = NULL;

            param.data = (const byte *)pdev->PendingOC;
            param.size = strlen(pdev->PendingOC);
            code = pdf_refer_named(pdev, &param, &pco);
            if(code < 0)
                return code;

            gs_snprintf(str, sizeof(str), "%"PRId64" 0 R", pco->id);
            code = cos_dict_put_string_copy(form_dict, "/OC", str);
            if (code < 0)
                return code;

            gs_free_object(pdev->memory->non_gc_memory, pdev->PendingOC, "");
            pdev->PendingOC = NULL;
        }
    }
    return cos_dict_put_c_key_object(form_dict, "/Group", (cos_object_t *)group_dict);
}

static int
pdf_begin_transparency_group(gs_gstate * pgs, gx_device_pdf * pdev,
                                const gs_pdf14trans_params_t * pparams, bool page_group)
{
    cos_dict_t *group_dict;
    int code;

    if (pgs == NULL)
        return_error(gs_error_unregistered); /* Must not happen. */
    code = pdf_make_group_dict(pdev, pparams, pgs, &group_dict);
    if (code < 0)
        return code;
    code = pdf_open_page(pdev, PDF_IN_STREAM);
    if (code < 0)
        return code;
    code = pdf_check_soft_mask(pdev, pgs);
    if (code < 0)
        return code;
    if (pdf_must_put_clip_path(pdev, pgs->clip_path)) {
        code = pdf_put_clip_path(pdev, pgs->clip_path);
        if (code < 0)
            return code;
    }
    if (page_group)
        pdev->pages[pdev->next_page].group_id = group_dict->id;
    else if (pparams->image_with_SMask) {
        /* An internal group for the image implementation.
           See doimagesmask in gs/lib/pdf_draw.ps .
           Just set a flag for skipping pdf_end_transparency_group. */
        pdev->image_with_SMask |= 1 << ++pdev->FormDepth;
        pdev->PatternsSinceForm = 0;
    } else {
        pdf_resource_t *pres, *pres_gstate = NULL;
        cos_dict_t *pcd = NULL, *pcd_Resources = NULL;

        code = pdf_prepare_drawing(pdev, pgs, &pres_gstate, false);
        if (code < 0)
            return code;
        code = pdf_end_gstate(pdev, pres_gstate);
        if (code < 0)
            return code;
        code = pdf_enter_substream(pdev, resourceXObject,
                gs_no_id, &pres, false, pdev->params.CompressPages);
        if (code < 0)
            return code;
        pdev->FormDepth++;
        pdev->PatternsSinceForm = 0;
        code = pdf_make_form_dict(pdev, pparams, pgs, group_dict, (cos_dict_t *)pres->object);
        if (code < 0)
            return code;

        /* Create a Resources dictionary and add it to the form dictionary */
        pcd = cos_stream_dict((cos_stream_t *)pres->object);
        pcd_Resources = cos_dict_alloc(pdev, "pdf_group(Resources)");
        if (pcd == NULL || pcd_Resources == NULL)
            return_error(gs_error_VMerror);
        code = cos_dict_put_c_key_object(pcd, "/Resources", COS_OBJECT(pcd_Resources));
        pdev->substream_Resources = pcd_Resources;
        return code;
    }
    return 0;
}

static int
pdf_end_transparency_group(gs_gstate * pgs, gx_device_pdf * pdev)
{
    int bottom = (pdev->ResourcesBeforeUsage ? 1 : 0);

    if (!is_in_page(pdev) && pdev->sbstack_depth == 0)
        return 0;	/* A Group definition at the page level, handled separately. */
    if (pdev->image_with_SMask & (1 << pdev->FormDepth)) {
        /* An internal group for the image implementation.
           See pdf_begin_transparency_group. */
        pdev->image_with_SMask &= ~(1 << pdev->FormDepth--);
        pdev->PatternsSinceForm = 0;
        return 0;
    } else if (pdev->sbstack_depth == bottom) {
        /* We're closing the page group. */
        if (pdev->pages[pdev->next_page].group_id == 0)
            return_error(gs_error_unregistered); /* Must not happen. */
        return 0;
    } else {
        pdf_resource_t *pres = pdev->accumulating_substream_resource;
        int code;
        uint ignore;

        if (pres == NULL)
            return_error(gs_error_unregistered);
        pdev->FormDepth--;
        pdev->PatternsSinceForm = 0;
        code = pdf_exit_substream(pdev);
        if (code < 0)
            return code;
        code = pdf_substitute_resource(pdev, &pres, resourceXObject, NULL, false);
        if (code < 0)
            return code;
        /* We need to update the 'where_used' field, in case we substituted a resource */
        pres->where_used |= pdev->used_mask;
        sputc(pdev->strm,'/');
        sputs(pdev->strm, (const byte *)pres->rname, strlen(pres->rname), &ignore);
        sputs(pdev->strm, (const byte *)" Do\n", 4, &ignore);
        code = pdf_add_resource(pdev, pdev->substream_Resources, "/XObject", pres);
        return code;
    }
}

static int
pdf_begin_transparency_mask(gs_gstate * pgs, gx_device_pdf * pdev,
                                const gs_pdf14trans_params_t * pparams)
{
    if (pparams->subtype == TRANSPARENCY_MASK_None) {
        int code;
        pdf_resource_t *pres = 0L;

        /* reset the soft mask ID. Apparently this is only used by pdfwrite, if we don't
         * reset it, then the pdf_prepare_drawing code doesn't know that the SMask has
         * changed, and so doesn't write out the GState
         */
        pgs->soft_mask_id = 0;
        code = pdf_prepare_drawing(pdev, pgs, &pres, false);
        if (code == gs_error_interrupt) {
            /* */
            /* Not in an appropriate context.  Do not restore the soft_mask_id.
               Otherwise any group push that occurs following this will use that
               softmask, which clearly should be NONE here.
             */
            /* ignore return code, we don't care about this graphics state as we aren't
             * emitting it anyway
             */
            pdf_end_gstate(pdev, pres);
            return 0;
        }
        if (code < 0)
            return code;
        code = pdf_end_gstate(pdev, pres);
        if (code < 0)
            return code;
        return 0;
    }
    if (pparams->mask_is_image) {
        /* HACK :
            The control comes here when
            the PDF interpreter will make the PS interpreter
            to interprete the mask for filling the transparency buffer
            with an SMask image.
            Since we handle Type 3 images as a high level objects,
            we don't install the transparency buffer here
            and need to skip the image enumeration for the SMask.
            However we have no right method for skipping
            an image enumeration due to possible side effect
            of the image data proc in Postscript language.
            Therefore we do enumerate the image mask and accumulate
            it as a PDF stream, but don't create a reference to it.
            Later it will be enumerated once again as a part of SMask-ed image,
            and the pdfwrite image handler will recognize duplicated images
            and won't create the second stream for same image.

            We could make a special workaround for
            skipping mask images either in the graphics library or
            in the PS code of the PDF interpreter,
            but we don't want to complicate things now.
            The performance leak for the second enumeration
            shouldn't be harmful.

            So now just set a flag for pdf_end_and_do_image.
        */
        pdev->image_mask_skip = true;
        return 0;
    } else {
        int code;

        pdev->smask_construction = true;
        code = pdf_make_soft_mask_dict(pdev, pparams);
        if (code < 0)
            return code;
        code = pdf_open_page(pdev, PDF_IN_STREAM);
        if (code < 0)
            return code;
        return pdf_begin_transparency_group(pgs, pdev, pparams, 0);
    }
}

static int
pdf_end_transparency_mask(gs_gstate * pgs, gx_device_pdf * pdev,
                                const gs_pdf14trans_params_t * pparams)
{
    pdev->smask_construction = false;
    if (pdev->image_mask_skip)
        pdev->image_mask_skip = false;
    else {
        pdf_resource_t *pres = pdev->accumulating_substream_resource;
        int code;
        char buf[20];

        if (pres == NULL)
            return_error(gs_error_unregistered);
        code = pdf_exit_substream(pdev);
        if (code < 0)
            return code;
        code = pdf_substitute_resource(pdev, &pres, resourceXObject, NULL, false);
        if (code < 0)
            return 0;
        /* We need to update the 'where_used' field, in case we substituted a resource */
        pres->where_used |= pdev->used_mask;
        gs_snprintf(buf, sizeof(buf), "%"PRId64" 0 R", pdf_resource_id(pres));
        if (pdev->pres_soft_mask_dict == 0L) {
            /* something went horribly wrong, we have an 'end' wihtout a matching 'begin'
             * Give up, throw an error.
             */
            return_error(gs_error_undefined);
        }
        code = cos_dict_put_c_key_string((cos_dict_t *)pdev->pres_soft_mask_dict->object,
                "/G", (const byte *)buf, strlen(buf));
        if (code < 0)
            return code;
        code = pdf_substitute_resource(pdev, &pdev->pres_soft_mask_dict,
                                        resourceSoftMaskDict, NULL, false);
        if (code < 0)
            return code;
        pdev->pres_soft_mask_dict->where_used |= pdev->used_mask;
        pgs->soft_mask_id = pdev->pres_soft_mask_dict->object->id;
        pdev->pres_soft_mask_dict = NULL;
        /* We called pdf_start_trnasparency_group (see pdf_begin_transparency_mask
         * above) but we don't call pdf_end_transparency_group, so we must reduce
         * the FormDepth ourselves.
         */
        pdev->FormDepth--;
        pdev->PatternsSinceForm = 0;
    }
    return 0;
}

static int
pdf_set_blend_params(gs_gstate * pgs, gx_device_pdf * dev,
                                const gs_pdf14trans_params_t * pparams)
{
    return 0;
}

int
gdev_pdf_composite(gx_device *dev,
    gx_device **pcdev, const gs_composite_t *pct,
    gs_gstate *pgs, gs_memory_t *memory, gx_device *cdev)
{
    gx_device_pdf *pdev = (gx_device_pdf *)dev;

    if (pdev->HaveTransparency && pdev->CompatibilityLevel >= 1.4 &&
            pct->type->comp_id == GX_COMPOSITOR_PDF14_TRANS &&
            pdev->PDFA != 1) {
        gs_pdf14trans_t *pcte = (gs_pdf14trans_t *)pct;
        gs_pdf14trans_params_t *params = &pcte->params;

        *pcdev = dev;
        switch(params->pdf14_op) {
            case PDF14_PUSH_DEVICE:
                return 0;
            case PDF14_POP_DEVICE:
                return 0;
            case PDF14_ABORT_DEVICE:
                return 0;
            case PDF14_BEGIN_TRANS_PAGE_GROUP:
                return pdf_begin_transparency_group(pgs, pdev, params, 1);
            case PDF14_BEGIN_TRANS_GROUP:
                return pdf_begin_transparency_group(pgs, pdev, params, 0);
            case PDF14_END_TRANS_GROUP:
                return pdf_end_transparency_group(pgs, pdev);
            case PDF14_BEGIN_TRANS_TEXT_GROUP:
                return 0;
            case PDF14_END_TRANS_TEXT_GROUP:
                return 0;
            case PDF14_BEGIN_TRANS_MASK:
                return pdf_begin_transparency_mask(pgs, pdev, params);
            case PDF14_END_TRANS_MASK:
                return pdf_end_transparency_mask(pgs, pdev, params);
            case PDF14_SET_BLEND_PARAMS:
                return pdf_set_blend_params(pgs, pdev, params);
            case PDF14_PUSH_TRANS_STATE:
                return 0;
            case PDF14_POP_TRANS_STATE:
                return 0;
            case PDF14_PUSH_SMASK_COLOR:
                return 0;
            case PDF14_POP_SMASK_COLOR:
                return 0;

            default :
                return_error(gs_error_unregistered); /* Must not happen. */
        }
        return 0;
    }
    return psdf_composite(dev, pcdev, pct, pgs, memory, cdev);
}

/* We're not sure why the folllowing device methods are never called.
   Stub them for a while. */

int
gdev_pdf_begin_transparency_group(gx_device *dev,
    const gs_transparency_group_params_t *ptgp,
    const gs_rect *pbbox,
    gs_gstate *pgs, gs_memory_t *mem)
{
    return 0;
}

int
gdev_pdf_end_transparency_group(gx_device *dev,
    gs_gstate *pgs)
{
    return 0;
}

int
gdev_pdf_begin_transparency_mask(gx_device *dev,
    const gx_transparency_mask_params_t *ptmp,
    const gs_rect *pbbox,
    gs_gstate *pgs, gs_memory_t *mem)
{
    return 0;
}

int
gdev_pdf_end_transparency_mask(gx_device *dev,
    gs_gstate *pgs)
{
    return 0;
}