Coverage Report

Created: 2026-07-24 07:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/devices/vector/gdevpdfp.c
Line
Count
Source
1
/* Copyright (C) 2001-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
17
/* Get/put parameters for PDF-writing driver */
18
#include "memory_.h"
19
#include "gx.h"
20
#include "gserrors.h"
21
#include "gdevpdfx.h"
22
#include "gdevpdfo.h"
23
#include "gdevpdfg.h"
24
#include "gdevpsdf.h"
25
#include "gsparamx.h"
26
#include "gsicc_manage.h"
27
28
/*
29
 * The pdfwrite device supports the following "real" parameters:
30
 *      OutputFile <string>
31
 *      all the Distiller parameters (also see gdevpsdp.c)
32
 * Only some of the Distiller parameters actually have any effect.
33
 *
34
 * The device also supports the following write-only pseudo-parameters that
35
 * serve only to communicate other information from the PostScript file.
36
 * Their "value" is an array of strings, some of which may be the result
37
 * of converting arbitrary PostScript objects to string form.
38
 *      pdfmark - see gdevpdfm.c
39
 *  DSC - processed in this file
40
 */
41
static int pdf_dsc_process(gx_device_pdf * pdev,
42
                            const gs_param_string_array * pma);
43
44
static const int CoreDistVersion = 5000;  /* Distiller 5.0 */
45
static const gs_param_item_t pdf_param_items[] = {
46
#define pi(key, type, memb) { key, type, offset_of(gx_device_pdf, memb) }
47
48
        /* Acrobat Distiller 4 parameters */
49
50
    /*
51
     * EndPage and StartPage are renamed because EndPage collides with
52
     * a page device parameter.
53
     */
54
    pi("PDFEndPage", gs_param_type_int, EndPage),
55
    pi("PDFStartPage", gs_param_type_int, StartPage),
56
    pi("Optimize", gs_param_type_bool, Optimize),
57
    pi("ParseDSCCommentsForDocInfo", gs_param_type_bool,
58
       ParseDSCCommentsForDocInfo),
59
    pi("ParseDSCComments", gs_param_type_bool, ParseDSCComments),
60
    pi("EmitDSCWarnings", gs_param_type_bool, EmitDSCWarnings),
61
    pi("CreateJobTicket", gs_param_type_bool, CreateJobTicket),
62
    pi("PreserveEPSInfo", gs_param_type_bool, PreserveEPSInfo),
63
    pi("AutoPositionEPSFiles", gs_param_type_bool, AutoPositionEPSFiles),
64
    pi("PreserveCopyPage", gs_param_type_bool, PreserveCopyPage),
65
    pi("UsePrologue", gs_param_type_bool, UsePrologue),
66
67
        /* Acrobat Distiller 5 parameters */
68
69
    pi("OffOptimizations", gs_param_type_int, OffOptimizations),
70
71
        /* Ghostscript-specific parameters */
72
73
    pi("ReAssignCharacters", gs_param_type_bool, ReAssignCharacters),
74
    pi("ReEncodeCharacters", gs_param_type_bool, ReEncodeCharacters),
75
    pi("FirstObjectNumber", gs_param_type_long, FirstObjectNumber),
76
    pi("CompressFonts", gs_param_type_bool, CompressFonts),
77
    pi("CompressStreams", gs_param_type_bool, CompressStreams),
78
    pi("PrintStatistics", gs_param_type_bool, PrintStatistics),
79
    pi("MaxInlineImageSize", gs_param_type_long, MaxInlineImageSize),
80
81
        /* PDF Encryption */
82
    pi("OwnerPassword", gs_param_type_string, OwnerPassword),
83
    pi("UserPassword", gs_param_type_string, UserPassword),
84
    pi("KeyLength", gs_param_type_int, KeyLength),
85
    pi("Permissions", gs_param_type_int, Permissions),
86
    pi("EncryptionR", gs_param_type_int, EncryptionR),
87
    pi("NoEncrypt", gs_param_type_string, NoEncrypt),
88
89
        /* Target viewer capabilities (Ghostscript-specific)  */
90
 /* pi("ForOPDFRead", gs_param_type_bool, ForOPDFRead),         pdfwrite-only */
91
    pi("ProduceDSC", gs_param_type_bool, ProduceDSC),
92
    pi("PatternImagemask", gs_param_type_bool, PatternImagemask),
93
    pi("MaxClipPathSize", gs_param_type_int, MaxClipPathSize),
94
    pi("MaxShadingBitmapSize", gs_param_type_int, MaxShadingBitmapSize),
95
    pi("HaveTrueTypes", gs_param_type_bool, HaveTrueTypes),
96
    pi("HaveCIDSystem", gs_param_type_bool, HaveCIDSystem),
97
    pi("HaveTransparency", gs_param_type_bool, HaveTransparency),
98
    pi("CompressEntireFile", gs_param_type_bool, CompressEntireFile),
99
    pi("PDFX", gs_param_type_int, PDFX),
100
    pi("PDFA", gs_param_type_int, PDFA),
101
    pi("DocumentUUID", gs_param_type_string, DocumentUUID),
102
    pi("InstanceUUID", gs_param_type_string, InstanceUUID),
103
    pi("DocumentTimeSeq", gs_param_type_int, DocumentTimeSeq),
104
105
    /* PDF/X parameters */
106
    pi("PDFXTrimBoxToMediaBoxOffset", gs_param_type_float_array, PDFXTrimBoxToMediaBoxOffset),
107
    pi("PDFXSetBleedBoxToMediaBox", gs_param_type_bool, PDFXSetBleedBoxToMediaBox),
108
    pi("PDFXBleedBoxToTrimBoxOffset", gs_param_type_float_array, PDFXBleedBoxToTrimBoxOffset),
109
110
    /* media selection parameters */
111
    pi("SetPageSize", gs_param_type_bool, SetPageSize),
112
    pi("RotatePages", gs_param_type_bool, RotatePages),
113
    pi("FitPages", gs_param_type_bool, FitPages),
114
    pi("CenterPages", gs_param_type_bool, CenterPages),
115
    pi("DoNumCopies", gs_param_type_bool, DoNumCopies),
116
    pi("PreserveSeparation", gs_param_type_bool, PreserveSeparation),
117
    pi("PreserveDeviceN", gs_param_type_bool, PreserveDeviceN),
118
    pi("PDFACompatibilityPolicy", gs_param_type_int, PDFACompatibilityPolicy),
119
    pi("DetectDuplicateImages", gs_param_type_bool, DetectDuplicateImages),
120
    pi("AllowIncrementalCFF", gs_param_type_bool, AllowIncrementalCFF),
121
    pi("WantsToUnicode", gs_param_type_bool, WantsToUnicode),
122
    pi("WantsOptionalContent", gs_param_type_bool, WantsOptionalContent),
123
    pi("PdfmarkCapable", gs_param_type_bool, PdfmarkCapable),
124
    pi("AllowPSRepeatFunctions", gs_param_type_bool, AllowPSRepeatFunctions),
125
    pi("IsDistiller", gs_param_type_bool, IsDistiller),
126
    pi("PreserveSMask", gs_param_type_bool, PreserveSMask),
127
    pi("PreserveTrMode", gs_param_type_bool, PreserveTrMode),
128
    pi("NoT3CCITT", gs_param_type_bool, NoT3CCITT),
129
    pi("FastWebView", gs_param_type_bool, Linearise),
130
    pi("NoOutputFonts", gs_param_type_bool, FlattenFonts),
131
    pi("WantsPageLabels", gs_param_type_bool, WantsPageLabels),
132
    pi("UserUnit", gs_param_type_float, UserUnit),
133
    pi("OmitInfoDate", gs_param_type_bool, OmitInfoDate),
134
    pi("OmitID", gs_param_type_bool, OmitID),
135
    pi("OmitXMP", gs_param_type_bool, OmitXMP),
136
    pi("ModifiesPageSize", gs_param_type_bool, ModifiesPageSize),
137
    pi("ModifiesPageOrder", gs_param_type_bool, ModifiesPageOrder),
138
    pi("WriteObjStms", gs_param_type_bool, WriteObjStms),
139
    pi("WriteXRefStm", gs_param_type_bool, WriteXRefStm),
140
    pi("ToUnicodeForStdEnc", gs_param_type_bool, ToUnicodeForStdEnc),
141
    pi("EmbedSubstituteFonts", gs_param_type_bool, EmbedSubstituteFonts),
142
    pi("UseBrotli", gs_param_type_bool, UseBrotli),
143
#undef pi
144
    gs_param_item_end
145
};
146
147
/*
148
  Notes on implementing the remaining Distiller functionality
149
  ===========================================================
150
151
  Architectural issues
152
  --------------------
153
154
  Must optionally disable application of TR, BG, UCR similarly.  Affects:
155
    PreserveHalftoneInfo
156
    PreserveOverprintSettings
157
    TransferFunctionInfo
158
    UCRandBGInfo
159
160
  Current limitations
161
  -------------------
162
163
  Non-primary elements in HalftoneType 5 are not written correctly
164
165
  Acrobat Distiller 3
166
  -------------------
167
168
  ---- Image parameters ----
169
170
  AntiAlias{Color,Gray,Mono}Images
171
172
  ---- Other parameters ----
173
174
  CompressPages
175
    Compress things other than page contents
176
  * PreserveHalftoneInfo
177
  PreserveOPIComments
178
    ? see OPI spec?
179
  * PreserveOverprintSettings
180
  * TransferFunctionInfo
181
  * UCRandBGInfo
182
  ColorConversionStrategy
183
    Select color space for drawing commands
184
  ConvertImagesToIndexed
185
    Postprocess image data *after* downsampling (requires an extra pass)
186
187
  Acrobat Distiller 4
188
  -------------------
189
190
  ---- Other functionality ----
191
192
  Document structure pdfmarks
193
194
  ---- Parameters ----
195
196
  xxxDownsampleType = /Bicubic
197
    Add new filter (or use siscale?) & to setup (gdevpsdi.c)
198
  DetectBlends
199
    Idiom recognition?  PatternType 2 patterns / shfill?  (see AD4)
200
  DoThumbnails
201
    Also output to memory device -- resolution issue
202
203
  ---- Job-level control ----
204
205
  EmitDSCWarnings
206
    Require DSC parser / interceptor
207
  CreateJobTicket
208
    ?
209
  AutoPositionEPSFiles
210
    Require DSC parsing
211
  PreserveCopyPage
212
    Concatenate Contents streams
213
  UsePrologue
214
    Needs hack in top-level control?
215
216
*/
217
218
/* Transfer a collection of parameters. */
219
static const byte xfer_item_sizes[] = {
220
    GS_PARAM_TYPE_SIZES(0)
221
};
222
int
223
gdev_pdf_get_param(gx_device *dev, char *Param, void *list)
224
2.65M
{
225
2.65M
    gx_device_pdf *pdev = (gx_device_pdf *)dev;
226
2.65M
    const gs_param_item_t *pi;
227
2.65M
    gs_param_list * plist = (gs_param_list *)list;
228
2.65M
    int code = 0;
229
230
158M
    for (pi = pdf_param_items; pi->key != 0; ++pi) {
231
157M
        if (strcmp(pi->key, Param) == 0) {
232
2.05M
            const char *key = pi->key;
233
2.05M
            const void *pvalue = (const void *)((const char *)pdev + pi->offset);
234
2.05M
            int size = xfer_item_sizes[pi->type];
235
2.05M
            gs_param_typed_value typed;
236
237
2.05M
            memcpy(&typed.value, pvalue, size);
238
2.05M
            typed.type = pi->type;
239
2.05M
            code = (*plist->procs->xmit_typed) (plist, key, &typed);
240
2.05M
            return code;
241
2.05M
        }
242
157M
    }
243
606k
    if (strcmp(Param, "CoreDistVersion") == 0) {
244
0
        return(param_write_int(plist, "CoreDistVersion", &CoreDistVersion));
245
0
    }
246
606k
    if (strcmp(Param, "CompatibilityLevel") == 0) {
247
0
        float f = pdev->CompatibilityLevel;
248
0
        return(param_write_float(plist, "CompatibilityLevel", &f));
249
0
    }
250
606k
    if (strcmp(Param, "ForOPDFRead") == 0) {
251
175k
        return(param_write_bool(plist, "ForOPDFRead", &pdev->ForOPDFRead));
252
175k
    }
253
430k
    if (strcmp(Param, "PassUserUnit") == 0) {
254
53.6k
        bool dummy;
255
53.6k
        if (pdev->CompatibilityLevel > 1.5)
256
24.1k
            dummy = true;
257
29.4k
        else
258
29.4k
            dummy = false;
259
53.6k
        return(param_write_bool(plist, "PassUserUnit", &dummy));
260
53.6k
    }
261
377k
    if (!pdev->is_ps2write) {
262
183k
        if (strcmp(Param, "pdfmark")  == 0){
263
10.2k
            return(param_write_null(plist, "pdfmark"));
264
10.2k
        }
265
172k
        if (strcmp(Param, "DSC") == 0){
266
0
            return(param_write_null(plist, "DSC"));
267
0
        }
268
172k
    }
269
270
#if OCR_VERSION > 0
271
    if (strcmp(Param, "OCRLanguage") == 0) {
272
        gs_param_string langstr;
273
        if (pdev->ocr_language[0]) {
274
            langstr.data = (const byte *)pdev->ocr_language;
275
            langstr.size = strlen(pdev->ocr_language);
276
            langstr.persistent = false;
277
        } else {
278
            langstr.data = (const byte *)"eng";
279
            langstr.size = 3;
280
            langstr.persistent = false;
281
        }
282
        return param_write_string(plist, "OCRLanguage", &langstr);
283
    }
284
    if (strcmp(Param, "OCREngine") == 0)
285
        return param_write_int(plist, "OCREngine", &pdev->ocr_engine);
286
287
    if (strcmp(Param, "UseOCR") == 0) {
288
        gs_param_string ocrstr;
289
290
        switch(pdev->UseOCR) {
291
            case UseOCRNever:
292
                ocrstr.data = (const byte *)"Never";
293
                ocrstr.size = 5;
294
                ocrstr.persistent = false;
295
                break;
296
            case UseOCRAsNeeded:
297
                ocrstr.data = (const byte *)"AsNeeded";
298
                ocrstr.size = 8;
299
                ocrstr.persistent = false;
300
                break;
301
            case UseOCRAlways:
302
                ocrstr.data = (const byte *)"Always";
303
                ocrstr.size = 8;
304
                ocrstr.persistent = false;
305
                break;
306
        }
307
        return param_write_string(plist, "UseOCR", &ocrstr);
308
    }
309
#endif
310
311
366k
    if (strcmp(Param, "OmitInfoDate") == 0)
312
0
        return(param_write_bool(plist, "OmitInfoDate", &pdev->OmitInfoDate));
313
366k
    if (strcmp(Param, "OmitXMP") == 0)
314
0
        return(param_write_bool(plist, "OmitXMP", &pdev->OmitXMP));
315
366k
    if (strcmp(Param, "OmitID") == 0)
316
0
        return(param_write_bool(plist, "OmitID", &pdev->OmitID));
317
318
366k
    return gdev_psdf_get_param(dev, Param, list);
319
366k
}
320
321
/* ---------------- Get parameters ---------------- */
322
323
/* Get parameters. */
324
int
325
gdev_pdf_get_params(gx_device * dev, gs_param_list * plist)
326
1.17M
{
327
1.17M
    gx_device_pdf *pdev = (gx_device_pdf *) dev;
328
1.17M
    float cl = (float)pdev->CompatibilityLevel;
329
1.17M
    int code;
330
1.17M
    int cdv = CoreDistVersion;
331
332
#if OCR_VERSION > 0
333
    gs_param_string langstr;
334
335
    if (pdev->ocr_language[0]) {
336
        langstr.data = (const byte *)pdev->ocr_language;
337
        langstr.size = strlen(pdev->ocr_language);
338
        langstr.persistent = false;
339
    } else {
340
        langstr.data = (const byte *)"eng";
341
        langstr.size = 3;
342
        langstr.persistent = false;
343
    }
344
345
    {
346
        gs_param_string ocrstr;
347
348
        switch(pdev->UseOCR) {
349
            case UseOCRNever:
350
                ocrstr.data = (const byte *)"Never";
351
                ocrstr.size = 5;
352
                ocrstr.persistent = false;
353
                break;
354
            case UseOCRAsNeeded:
355
                ocrstr.data = (const byte *)"AsNeeded";
356
                ocrstr.size = 8;
357
                ocrstr.persistent = false;
358
                break;
359
            case UseOCRAlways:
360
                ocrstr.data = (const byte *)"Always";
361
                ocrstr.size = 6;
362
                ocrstr.persistent = false;
363
                break;
364
        }
365
        code = param_write_string(plist, "UseOCR", &ocrstr);
366
    }
367
    code = param_write_string(plist, "OCRLanguage", &langstr);
368
    if(code < 0)
369
        return code;
370
    code = param_write_int(plist, "OCREngine", &pdev->ocr_engine);
371
    if(code < 0)
372
        return code;
373
#endif
374
375
1.17M
    pdev->ParamCompatibilityLevel = cl;
376
1.17M
    code = gdev_psdf_get_params(dev, plist);
377
1.17M
    if (code < 0 ||
378
1.17M
        (code = param_write_int(plist, "CoreDistVersion", &cdv)) < 0 ||
379
1.17M
        (code = param_write_float(plist, "CompatibilityLevel", &cl)) < 0 ||
380
1.17M
        (!pdev->is_ps2write && (code = param_write_bool(plist, "ForOPDFRead", &pdev->ForOPDFRead)) < 0) ||
381
        /* Indicate that we can process pdfmark and DSC. */
382
1.17M
        (param_requested(plist, "pdfmark") > 0 &&
383
0
         (code = param_write_null(plist, "pdfmark")) < 0) ||
384
1.17M
        (param_requested(plist, "DSC") > 0 &&
385
0
         (code = param_write_null(plist, "DSC")) < 0) ||
386
1.17M
        (code = gs_param_write_items(plist, pdev, NULL, pdf_param_items)) < 0
387
1.17M
        )
388
0
    {}
389
1.17M
    return code;
390
1.17M
}
391
392
/* ---------------- Put parameters ---------------- */
393
394
/* Put parameters, implementation */
395
static int
396
gdev_pdf_put_params_impl(gx_device * dev, const gx_device_pdf * save_dev, gs_param_list * plist)
397
834k
{
398
834k
    int ecode, code;
399
834k
    gx_device_pdf *pdev = (gx_device_pdf *) dev;
400
834k
    float cl = (float)pdev->CompatibilityLevel;
401
834k
    bool locked = pdev->params.LockDistillerParams, ForOPDFRead;
402
834k
    bool XRefStm_set = false, ObjStms_set = false;
403
834k
    gs_param_name param_name;
404
405
834k
    pdev->pdf_memory = gs_memory_stable(pdev->memory);
406
    /*
407
     * If this is a pseudo-parameter (pdfmark or DSC),
408
     * don't bother checking for any real ones.
409
     */
410
411
834k
    {
412
834k
        gs_param_string_array ppa;
413
834k
        gs_param_string pps;
414
415
834k
        code = param_read_string_array(plist, (param_name = "pdfmark"), &ppa);
416
834k
        switch (code) {
417
130k
            case 0:
418
130k
                code = pdfwrite_pdf_open_document(pdev);
419
130k
                if (code < 0)
420
0
                    return code;
421
130k
                code = pdfmark_process(pdev, &ppa);
422
130k
                if (code >= 0)
423
130k
                    return code;
424
                /* falls through for errors */
425
34
            default:
426
34
                param_signal_error(plist, param_name, code);
427
34
                return code;
428
704k
            case 1:
429
704k
                break;
430
834k
        }
431
432
704k
        code = param_read_string_array(plist, (param_name = "DSC"), &ppa);
433
704k
        switch (code) {
434
22.3k
            case 0:
435
22.3k
                code = pdfwrite_pdf_open_document(pdev);
436
22.3k
                if (code < 0)
437
0
                    return code;
438
22.3k
                code = pdf_dsc_process(pdev, &ppa);
439
22.3k
                if (code >= 0)
440
22.3k
                    return code;
441
                /* falls through for errors */
442
0
            default:
443
0
                param_signal_error(plist, param_name, code);
444
0
                return code;
445
681k
            case 1:
446
681k
                break;
447
704k
        }
448
449
681k
        code = param_read_string(plist, (param_name = "pdfpagelabels"), &pps);
450
681k
        switch (code) {
451
461
            case 0:
452
461
                {
453
461
                    if (!pdev->ForOPDFRead) {
454
461
                        cos_dict_t *const pcd = pdev->Catalog;
455
461
                        code = pdfwrite_pdf_open_document(pdev);
456
461
                        if (code < 0)
457
0
                            return code;
458
461
                        code = cos_dict_put_string(pcd, (const byte *)"/PageLabels", 11,
459
461
                                   pps.data, pps.size);
460
461
                        if (code >= 0)
461
461
                            return code;
462
461
                    } else
463
0
                        return 0;
464
461
                 }
465
                /* falls through for errors */
466
0
            default:
467
0
                param_signal_error(plist, param_name, code);
468
0
                return code;
469
681k
            case 1:
470
681k
                break;
471
681k
        }
472
681k
    }
473
474
#if OCR_VERSION > 0
475
    {
476
        int len;
477
        gs_param_string langstr;
478
        switch (code = param_read_string(plist, (param_name = "OCRLanguage"), &langstr)) {
479
            case 0:
480
                if (gs_is_path_control_active(pdev->memory)
481
                && (strlen(pdev->ocr_language) != langstr.size || memcmp(pdev->ocr_language, langstr.data, langstr.size) != 0)) {
482
                    return_error(gs_error_invalidaccess);
483
                }
484
                else {
485
                    len = langstr.size;
486
                    if (len >= sizeof(pdev->ocr_language))
487
                        len = sizeof(pdev->ocr_language)-1;
488
                    memcpy(pdev->ocr_language, langstr.data, len);
489
                    pdev->ocr_language[len] = 0;
490
                }
491
                break;
492
            case 1:
493
                break;
494
            default:
495
                ecode = code;
496
                param_signal_error(plist, param_name, ecode);
497
        }
498
    }
499
500
    {
501
        int engine;
502
        switch (code = param_read_int(plist, (param_name = "OCREngine"), &engine)) {
503
            case 0:
504
                pdev->ocr_engine = engine;
505
                break;
506
            case 1:
507
                break;
508
            default:
509
                ecode = code;
510
                param_signal_error(plist, param_name, ecode);
511
        }
512
    }
513
514
    {
515
        gs_param_string ocrstr;
516
517
        code = param_read_string(plist, (param_name = "UseOCR"), &ocrstr);
518
        switch(code) {
519
            case 0:
520
                if (ocrstr.size == 5 && memcmp(ocrstr.data, "Never", 5) == 0)
521
                    pdev->UseOCR = UseOCRNever;
522
                if (ocrstr.size == 8 && memcmp(ocrstr.data, "AsNeeded", 8) == 0)
523
                    pdev->UseOCR = UseOCRAsNeeded;
524
                if (ocrstr.size == 6 && memcmp(ocrstr.data, "Always", 6) == 0)
525
                    pdev->UseOCR = UseOCRAlways;
526
                break;
527
            case 1:
528
                break;
529
            default:
530
                param_signal_error(plist, param_name, code);
531
                break;
532
        }
533
    }
534
535
#endif
536
537
    /*
538
     * Check for LockDistillerParams before doing anything else.
539
     * If LockDistillerParams is true and is not being set to false,
540
     * ignore all resettings of PDF-specific parameters.  Note that
541
     * LockDistillerParams is read again, and reset if necessary, in
542
     * psdf_put_params.
543
     */
544
681k
    ecode = param_read_bool(plist, (param_name = "LockDistillerParams"), &locked);
545
681k
    if (ecode < 0)
546
0
        param_signal_error(plist, param_name, ecode);
547
548
    /* General parameters. */
549
550
681k
    {
551
681k
        int efo = 1;
552
553
681k
        ecode = param_put_int(plist, (param_name = ".EmbedFontObjects"), &efo, ecode);
554
681k
        if (ecode < 0)
555
0
            param_signal_error(plist, param_name, ecode);
556
681k
        if (efo != 1)
557
0
            param_signal_error(plist, param_name, ecode = gs_error_rangecheck);
558
681k
    }
559
681k
    {
560
681k
        int cdv = CoreDistVersion;
561
562
681k
        ecode = param_put_int(plist, (param_name = "CoreDistVersion"), &cdv, ecode);
563
681k
        if (ecode < 0)
564
0
            return gs_note_error(ecode);
565
681k
        if (cdv != CoreDistVersion)
566
0
            param_signal_error(plist, param_name, ecode = gs_error_rangecheck);
567
681k
    }
568
569
0
    switch (code = param_read_float(plist, (param_name = "CompatibilityLevel"), &cl)) {
570
0
        default:
571
0
            ecode = code;
572
0
            param_signal_error(plist, param_name, ecode);
573
0
            break;
574
51.5k
        case 0:
575
51.5k
            if (!(locked && pdev->params.LockDistillerParams)) {
576
                /*
577
                 * Must be 1.2, 1.3, 1.4, or 1.5.  Per Adobe documentation, substitute
578
                 * the nearest achievable value.
579
                 */
580
51.5k
                if (cl < (float)1.15)
581
10
                    cl = (float)1.1;
582
51.5k
                else if (cl < (float)1.25)
583
35.8k
                    cl = (float)1.2;
584
15.6k
                else if (cl < (float)1.35)
585
0
                    cl = (float)1.3;
586
15.6k
                else if (cl < (float)1.45)
587
46
                    cl = (float)1.4;
588
15.6k
                else if (cl < (float)1.55)
589
0
                    cl = (float)1.5;
590
15.6k
                else if (cl < (float)1.65)
591
0
                    cl = (float)1.6;
592
15.6k
                else if (cl < (float)1.75)
593
15.6k
                    cl = (float)1.7;
594
18
                else {
595
18
                    cl = (float)2.0;
596
18
                    if (pdev->params.TransferFunctionInfo == tfi_Preserve)
597
8
                        pdev->params.TransferFunctionInfo = tfi_Apply;
598
18
                }
599
51.5k
            }
600
681k
        case 1:
601
681k
            break;
602
681k
    }
603
604
681k
    ecode = param_read_bool(plist, (param_name = "WriteXRefStm"), &pdev->WriteXRefStm);
605
681k
    if (ecode < 0)
606
0
        param_signal_error(plist, param_name, ecode);
607
681k
    if (ecode == 0)
608
28.4k
        XRefStm_set = true;
609
681k
    ecode = param_read_bool(plist, (param_name = "WriteObjStms"), &pdev->WriteObjStms);
610
681k
    if (ecode < 0)
611
0
        param_signal_error(plist, param_name, ecode);
612
681k
    if (ecode == 0)
613
28.4k
        ObjStms_set = true;
614
615
681k
    {
616
681k
        code = gs_param_read_items(plist, pdev, pdf_param_items, pdev->pdf_memory);
617
681k
        if (code < 0 || (code = param_read_bool(plist, "ForOPDFRead", &ForOPDFRead)) < 0)
618
0
        {
619
0
        }
620
681k
        if (code == 0 && !pdev->is_ps2write && !(locked && pdev->params.LockDistillerParams))
621
15.6k
            pdev->ForOPDFRead = ForOPDFRead;
622
681k
    }
623
681k
    if (code < 0)
624
0
        ecode = code;
625
681k
    {
626
        /*
627
         * Setting FirstObjectNumber is only legal if the file
628
         * has just been opened and nothing has been written,
629
         * or if we are setting it to the same value.
630
         */
631
681k
        int64_t fon = pdev->FirstObjectNumber;
632
633
681k
        if (fon != save_dev->FirstObjectNumber) {
634
0
            if (fon <= 0 || fon > 0x7fff0000 ||
635
0
                (pdev->next_id != 0 &&
636
0
                 pdev->next_id !=
637
0
                 save_dev->FirstObjectNumber + pdf_num_initial_ids)
638
0
                ) {
639
0
                ecode = gs_error_rangecheck;
640
0
                param_signal_error(plist, "FirstObjectNumber", ecode);
641
0
            }
642
0
        }
643
681k
    }
644
681k
    {
645
        /*
646
         * Set ProcessColorModel now, because gx_default_put_params checks
647
         * it.
648
         */
649
681k
        static const char *const pcm_names[] = {
650
681k
            "DeviceGray", "DeviceRGB", "DeviceCMYK", "DeviceN", 0
651
681k
        };
652
681k
        int pcm = -1;
653
654
681k
        ecode = param_put_enum(plist, "ProcessColorModel", &pcm,
655
681k
                               pcm_names, ecode);
656
681k
        if (ecode < 0)
657
0
            goto fail;
658
681k
        if (pcm >= 0) {
659
28.4k
            pdf_set_process_color_model(pdev, pcm);
660
28.4k
            rc_decrement(pdev->icc_struct, "gdev_pdf_put_params_impl, ProcessColorModel changed");
661
28.4k
            pdev->icc_struct = 0;
662
28.4k
        }
663
681k
    }
664
665
681k
    if (pdev->is_ps2write && (code = param_read_bool(plist, "ProduceDSC", &pdev->ProduceDSC)) < 0) {
666
0
        param_signal_error(plist, param_name, code);
667
0
    }
668
669
681k
    if (pdev->OmitInfoDate && pdev->PDFX != 0) {
670
0
        emprintf(pdev->memory, "\nIt is not possible to omit the CreationDate when creating PDF/X\nOmitInfoDate is being ignored.\n");
671
0
        pdev->OmitInfoDate = 0;
672
0
    }
673
681k
    if (pdev->OmitID && pdev->PDFX != 0) {
674
0
        emprintf(pdev->memory, "\nIt is not possible to omit the ID array when creating PDF/X\nOmitID is being ignored.\n");
675
0
        pdev->OmitID = 0;
676
0
    }
677
681k
    if (pdev->OmitID && pdev->CompatibilityLevel > 1.7) {
678
0
        emprintf(pdev->memory, "\nIt is not possible to omit the ID array when creating a version 2.0 or greater PDF\nOmitID is being ignored.\n");
679
0
        pdev->OmitID = 0;
680
0
    }
681
681k
    if (pdev->OmitID && pdev->OwnerPassword.size != 0) {
682
0
        emprintf(pdev->memory, "\nIt is not possible to omit the ID array when creating an encrypted PDF\nOmitID is being ignored.\n");
683
0
        pdev->OmitID = 0;
684
0
    }
685
686
681k
    if (pdev->OmitXMP && pdev->PDFA != 0) {
687
0
        emprintf(pdev->memory, "\nIt is not possible to omit the XMP metadta when creating a PDF/A\nOmitXMP is being ignored.\n");
688
0
        pdev->OmitXMP = 0;
689
0
    }
690
691
    /* PDFA and PDFX are stored in the page device dictionary and therefore
692
     * set on every setpagedevice. However, if we have encountered a file which
693
     * can't be made this way, and the PDFACompatibilityPolicy is 1, we want to
694
     * continue producing the file, but not as a PDF/A or PDF/X file. Its more
695
     * or less impossible to alter the setting in the (potentially saved) page
696
     * device dictionary, so we use this rather clunky method.
697
     */
698
681k
    if (pdev->PDFA < 0 || pdev->PDFA > 3){
699
0
        ecode = gs_note_error(gs_error_rangecheck);
700
0
        param_signal_error(plist, "PDFA", ecode);
701
0
        goto fail;
702
0
    }
703
681k
    if(pdev->PDFA != 0 && pdev->AbortPDFAX)
704
0
        pdev->PDFA = 0;
705
681k
    if(pdev->PDFX && pdev->AbortPDFAX)
706
0
        pdev->PDFX = 0;
707
681k
    if (pdev->PDFX && pdev->PDFA != 0) {
708
0
        ecode = gs_note_error(gs_error_rangecheck);
709
0
        param_signal_error(plist, "PDFA", ecode);
710
0
        goto fail;
711
0
    }
712
681k
    if (pdev->PDFX != 0 && pdev->ForOPDFRead) {
713
0
        ecode = gs_note_error(gs_error_rangecheck);
714
0
        param_signal_error(plist, "PDFX", ecode);
715
0
        goto fail;
716
0
    }
717
681k
    if (pdev->PDFA != 0 && pdev->ForOPDFRead) {
718
0
        ecode = gs_note_error(gs_error_rangecheck);
719
0
        param_signal_error(plist, "PDFA", ecode);
720
0
        goto fail;
721
0
    }
722
681k
    if (pdev->PDFA == 1 || (pdev->PDFX > 0 && pdev->PDFX < 4) || pdev->CompatibilityLevel < 1.4) {
723
181k
         pdev->HaveTransparency = false;
724
181k
         pdev->PreserveSMask = false;
725
181k
         pdev->WantsOptionalContent = false;
726
181k
    }
727
728
    /* We cannot guarantee that all page objects have a Group with a /CS (ColorSpace) entry
729
     * which is a requirement for PDF/A-2+ when objects are defined in Device Independent Colour.
730
     * Try and warn the user.
731
     */
732
681k
    if (pdev->PDFA > 1 && pdev->params.ColorConversionStrategy == ccs_UseDeviceIndependentColor) {
733
0
        switch (pdev->PDFACompatibilityPolicy) {
734
0
            case 0:
735
0
                emprintf(pdev->memory,
736
0
                 "\n\tpdfwrite cannot guarantee creating a conformant PDF/A-2 file with device-independent colour.\n\tWe recommend converting to a device colour space.\n\tReverting to normal output.\n");
737
0
                pdev->AbortPDFAX = true;
738
0
                pdev->PDFA = 0;
739
0
                break;
740
0
            case 1:
741
0
                emprintf(pdev->memory,
742
0
                 "\n\tpdfwrite cannot guarantee creating a conformant PDF/A-2 file with device-independent colour.\n\tWe recommend converting to a device colour space.\n\tWe cannot ignore this request, reverting to normal output.\n");
743
0
                break;
744
0
            case 2:
745
0
                emprintf(pdev->memory,
746
0
                 "\n\tpdfwrite cannot guarantee creating a conformant PDF/A-2 file with device-independent colour.\n\tWe recommend converting to a device colour space.\n\tAborting.\n");
747
0
                return_error(gs_error_unknownerror);
748
0
                break;
749
0
            default:
750
0
                emprintf(pdev->memory,
751
0
                 "\n\tpdfwrite cannot guarantee creating a conformant PDF/A-2 file with device-independent colour.\n\tWe recommend converting to a device colour space.\n\tReverting to normal output.\n");
752
0
                pdev->AbortPDFAX = true;
753
0
                pdev->PDFA = 0;
754
0
                break;
755
0
        }
756
0
    }
757
    /*
758
     * We have to set version to the new value, because the set of
759
     * legal parameter values for psdf_put_params varies according to
760
     * the version.
761
     */
762
681k
    if (pdev->PDFX != 0) {
763
0
        if (pdev->PDFX < 4) {
764
0
            cl = (float)1.3; /* Instead pdev->CompatibilityLevel = 1.2; - see below. */
765
0
            if (pdev->WriteObjStms && ObjStms_set)
766
0
                emprintf(pdev->memory, "Can't use ObjStm before PDF 1.5, PDF/X does not support PDF 1.5, ignoring WriteObjStms directive\n");
767
0
            if (pdev->WriteXRefStm && XRefStm_set)
768
0
                emprintf(pdev->memory, "Can't use an XRef stream before PDF 1.5, PDF/X does not support PDF 1.5, ignoring WriteXRefStm directive\n");
769
770
0
            pdev->WriteObjStms = false;
771
0
            pdev->WriteXRefStm = false;
772
0
        } else {
773
0
            cl = (float)1.6; /* Instead pdev->CompatibilityLevel = 1.2; - see below. */
774
0
        }
775
0
    }
776
681k
    if (pdev->PDFA == 1 && cl != 1.4) {
777
0
        cl = (float)1.4;
778
779
0
        if (pdev->WriteObjStms && ObjStms_set)
780
0
            emprintf(pdev->memory, "Can't use ObjStm before PDF 1.5, PDF/A-1 does not support PDF 1.5, ignoring WriteObjStms directive\n");
781
0
        if (pdev->WriteXRefStm && XRefStm_set)
782
0
            emprintf(pdev->memory, "Can't use an XRef stream before PDF 1.5, PDF/A-1 does not support PDF 1.5, ignoring WriteXRefStm directive\n");
783
0
        pdev->WriteObjStms = false;
784
0
        pdev->WriteXRefStm = false;
785
0
    }
786
681k
    if (pdev->PDFA == 2 && cl < 1.7)
787
0
        cl = (float)1.7;
788
681k
    pdev->version = (cl < 1.2 ? psdf_version_level2 : psdf_version_ll3);
789
681k
    if (pdev->ForOPDFRead) {
790
204k
        pdev->ResourcesBeforeUsage = true;
791
204k
        pdev->HaveCFF = false;
792
204k
        pdev->HavePDFWidths = false;
793
204k
        pdev->HaveStrokeColor = false;
794
204k
        cl = (float)1.2; /* Instead pdev->CompatibilityLevel = 1.2; - see below. */
795
204k
        pdev->MaxInlineImageSize = max_long; /* Save printer's RAM from saving temporary image data.
796
                                                Immediate images doen't need buffering. */
797
204k
        pdev->version = psdf_version_level2;
798
476k
    } else {
799
476k
        pdev->ResourcesBeforeUsage = false;
800
476k
        pdev->HaveCFF = true;
801
476k
        pdev->HavePDFWidths = true;
802
476k
        pdev->HaveStrokeColor = true;
803
476k
    }
804
681k
    pdev->ParamCompatibilityLevel = cl;
805
681k
    if (cl < 1.2) {
806
6
        pdev->HaveCFF = false;
807
6
    }
808
681k
    if (cl < 1.5)
809
204k
        pdev->WantsOptionalContent = false;
810
811
681k
    ecode = param_read_float(plist, "UserUnit", &pdev->UserUnit);
812
681k
    if (ecode < 0)
813
0
        goto fail;
814
681k
    if (pdev->UserUnit == 0 || (pdev->UserUnit != 1 && pdev->CompatibilityLevel < 1.6)) {
815
2
        ecode = gs_note_error(gs_error_rangecheck);
816
2
        param_signal_error(plist, "UserUnit", ecode);
817
2
        goto fail;
818
2
    }
819
820
681k
    ecode = gdev_psdf_put_params(dev, plist);
821
681k
    if (ecode < 0)
822
414
        goto fail;
823
824
681k
    if (pdev->CompatibilityLevel > 1.7 && pdev->params.TransferFunctionInfo == tfi_Preserve) {
825
0
        pdev->params.TransferFunctionInfo = tfi_Apply;
826
0
        emprintf(pdev->memory, "\nIt is not possible to preserve transfer functions in PDF 2.0\ntransfer functions will be applied instead\n");
827
0
    }
828
829
681k
    if (pdev->params.ConvertCMYKImagesToRGB) {
830
0
        if (pdev->params.ColorConversionStrategy == ccs_CMYK) {
831
0
            emprintf(pdev->memory, "ConvertCMYKImagesToRGB is not compatible with ColorConversionStrategy of CMYK\n");
832
0
        } else {
833
0
            if (pdev->params.ColorConversionStrategy == ccs_Gray) {
834
0
                emprintf(pdev->memory, "ConvertCMYKImagesToRGB is not compatible with ColorConversionStrategy of Gray\n");
835
0
            } else {
836
0
                pdf_set_process_color_model(pdev,1);
837
0
                ecode = gsicc_init_device_profile_struct((gx_device *)pdev, NULL, 0);
838
0
                if (ecode < 0)
839
0
                    goto fail;
840
0
            }
841
0
        }
842
0
    }
843
    /* Record if the user has specified a custom profile, so we don't replace it
844
       with a default when we change color space - see win_pr2_set_bpp()
845
       Here, we only want to know *if* we have a user specced profile, actually
846
       setting it will be handled by gdev_vector_put_params()
847
     */
848
681k
    if (!pdev->user_icc) {
849
629k
        gs_param_string icc_pro_dummy;
850
851
629k
        pdev->user_icc = param_read_string(plist, "OutputICCProfile", &icc_pro_dummy) == 0;
852
629k
    }
853
681k
    switch (pdev->params.ColorConversionStrategy) {
854
0
        case ccs_ByObjectType:
855
681k
        case ccs_LeaveColorUnchanged:
856
681k
            break;
857
0
        case ccs_UseDeviceIndependentColor:
858
0
        case ccs_UseDeviceIndependentColorForImages:
859
0
            pdev->params.TransferFunctionInfo = tfi_Apply;
860
0
            break;
861
0
        case ccs_CMYK:
862
0
            pdf_set_process_color_model(pdev, 2);
863
0
            if (!pdev->user_icc) {
864
0
                ecode = gsicc_init_device_profile_struct((gx_device *)pdev, NULL, 0);
865
0
                if (ecode < 0)
866
0
                    goto fail;
867
0
            }
868
0
            break;
869
0
        case ccs_Gray:
870
0
            pdf_set_process_color_model(pdev,0);
871
0
            if (!pdev->user_icc) {
872
0
                ecode = gsicc_init_device_profile_struct((gx_device *)pdev, NULL, 0);
873
0
                if (ecode < 0)
874
0
                    goto fail;
875
0
            }
876
0
            break;
877
0
        case ccs_sRGB:
878
0
        case ccs_RGB:
879
            /* Only bother to do this if we didn't handle it above */
880
0
            if (!pdev->params.ConvertCMYKImagesToRGB) {
881
0
                pdf_set_process_color_model(pdev,1);
882
0
                if (!pdev->user_icc) {
883
0
                    ecode = gsicc_init_device_profile_struct((gx_device *)pdev, NULL, 0);
884
0
                    if (ecode < 0)
885
0
                        goto fail;
886
0
                }
887
0
            }
888
0
            break;
889
0
        default:
890
0
            break;
891
681k
    }
892
681k
    if (cl < 1.5f && pdev->params.ColorImage.Filter != NULL &&
893
158k
            !strcmp(pdev->params.ColorImage.Filter, "JPXEncode")) {
894
0
        emprintf(pdev->memory,
895
0
                 "JPXEncode requires CompatibilityLevel >= 1.5 .\n");
896
0
        ecode = gs_note_error(gs_error_rangecheck);
897
0
    }
898
681k
    if (cl < 1.5f && pdev->params.GrayImage.Filter != NULL &&
899
158k
            !strcmp(pdev->params.GrayImage.Filter, "JPXEncode")) {
900
0
        emprintf(pdev->memory,
901
0
                 "JPXEncode requires CompatibilityLevel >= 1.5 .\n");
902
0
        ecode = gs_note_error(gs_error_rangecheck);
903
0
    }
904
681k
    if (cl < 1.4f && pdev->params.MonoImage.Filter != NULL &&
905
204k
            !strcmp(pdev->params.MonoImage.Filter, "JBIG2Encode")) {
906
0
        emprintf(pdev->memory,
907
0
                 "JBIG2Encode requires CompatibilityLevel >= 1.4 .\n");
908
0
        ecode = gs_note_error(gs_error_rangecheck);
909
0
    }
910
681k
    if (pdev->HaveTrueTypes && pdev->version == psdf_version_level2) {
911
204k
        pdev->version = psdf_version_level2_with_TT ;
912
204k
    }
913
681k
    if (ecode < 0)
914
0
        goto fail;
915
916
681k
    if (pdev->FirstObjectNumber != save_dev->FirstObjectNumber) {
917
0
        if (pdev->xref.file != 0) {
918
0
            if (gp_fseek(pdev->xref.file, 0L, SEEK_SET) != 0) {
919
0
                ecode = gs_error_ioerror;
920
0
                goto fail;
921
0
            }
922
0
            pdf_initialize_ids(pdev);
923
0
        }
924
0
    }
925
    /* Handle the float/double mismatch. */
926
681k
    pdev->CompatibilityLevel = (int)(cl * 10 + 0.5) / 10.0;
927
928
681k
    if (pdev->ForOPDFRead && pdev->OwnerPassword.size != 0) {
929
0
        emprintf(pdev->memory, "\n\tSetting OwnerPassword for PostScript output would result in an encrypted\n\tunusable PostScript file, ignoring.\n");
930
0
        pdev->OwnerPassword.size = 0;
931
0
    }
932
933
681k
    if(pdev->OwnerPassword.size != save_dev->OwnerPassword.size ||
934
681k
        (pdev->OwnerPassword.size != 0 &&
935
0
         memcmp(pdev->OwnerPassword.data, save_dev->OwnerPassword.data,
936
0
         pdev->OwnerPassword.size) != 0)) {
937
0
        if (pdev->is_open) {
938
0
            if (pdev->PageCount == 0) {
939
0
                gs_closedevice((gx_device *)pdev);
940
0
                return 0;
941
0
            }
942
0
            else
943
0
                emprintf(pdev->memory, "Owner Password changed mid-job, ignoring.\n");
944
0
        }
945
0
    }
946
947
681k
    if (pdev->Linearise && pdev->file != NULL && !gp_fseekable(pdev->file)) {
948
0
        emprintf(pdev->memory, "Can't linearise a non-seekable output file, ignoring\n");
949
0
        pdev->Linearise = false;
950
0
    }
951
952
681k
    if (pdev->Linearise && pdev->is_ps2write) {
953
0
        emprintf(pdev->memory, "Can't linearise PostScript output, ignoring\n");
954
0
        pdev->Linearise = false;
955
0
    }
956
957
681k
    if (pdev->Linearise && pdev->OwnerPassword.size != 0) {
958
0
        emprintf(pdev->memory, "Can't linearise encrypted PDF, ignoring\n");
959
0
        pdev->Linearise = false;
960
0
    }
961
962
    /* Object streams and XRef streams */
963
681k
    if (pdev->is_ps2write) {
964
204k
        pdev->WriteObjStms = false;
965
204k
        pdev->WriteXRefStm = false;
966
476k
    } else {
967
476k
        if (pdev->Linearise) {
968
0
            if (XRefStm_set)
969
0
                emprintf(pdev->memory, "We don't support XRefStm with FastWebView (Linearized PDF), ignoring WriteXRefStm directive\n");
970
0
            pdev->WriteXRefStm = false;
971
0
            if (ObjStms_set)
972
0
                emprintf(pdev->memory, "We don't support ObjStms with FastWebView (Linearized PDF), ignoring WriteObjStms directive\n");
973
0
            pdev->WriteObjStms = false;
974
0
        }
975
476k
    }
976
681k
    if (pdev->WriteObjStms && pdev->CompatibilityLevel < 1.5) {
977
14
        if (ObjStms_set)
978
0
            emprintf(pdev->memory, "Can't use Object streams before PDF 1.5, ignoring WriteObjStms directive\n");
979
14
        pdev->WriteObjStms = false;
980
14
    }
981
681k
    if (pdev->WriteXRefStm && pdev->CompatibilityLevel < 1.5) {
982
14
        if (XRefStm_set)
983
0
            emprintf(pdev->memory, "Can't use an XRef stream before PDF 1.5, ignoring WriteXRefStm directive\n");
984
14
        pdev->WriteXRefStm = false;
985
14
    }
986
681k
    if (pdev->WriteObjStms && !pdev->WriteXRefStm) {
987
0
        if (ObjStms_set)
988
0
            emprintf(pdev->memory, "Can't use Object streams without XRef stream, ignoring WriteObjStms directive\n");
989
0
        pdev->WriteObjStms = false;
990
0
    }
991
681k
    if (pdev->WriteObjStms) {
992
476k
        if (pdev->next_id == 1)
993
0
            pdev->doubleXref = true;
994
476k
        if (!pdev->doubleXref) {
995
0
            emprintf(pdev->memory, "Already started writing output, too late to select WriteObjStms, ignoring directive\n");
996
0
            pdev->WriteObjStms = false;
997
0
        }
998
476k
    }
999
1000
681k
    if (pdev->FlattenFonts)
1001
0
        pdev->PreserveTrMode = false;
1002
1003
681k
    if (pdev->ForOPDFRead)
1004
204k
        pdev->params.UseBrotliCompression = pdev->UseBrotli = false;
1005
476k
    else
1006
476k
        pdev->params.UseBrotliCompression = pdev->UseBrotli;
1007
1008
681k
    return 0;
1009
416
 fail:
1010
    /* Restore all the parameters to their original state. */
1011
416
    pdev->version = save_dev->version;
1012
416
    pdf_set_process_color_model(pdev, save_dev->pcm_color_info_index);
1013
416
    pdev->saved_fill_color = save_dev->saved_fill_color;
1014
416
    pdev->saved_stroke_color = save_dev->saved_fill_color;
1015
416
    {
1016
416
        const gs_param_item_t *ppi = pdf_param_items;
1017
1018
30.7k
        for (; ppi->key; ++ppi)
1019
30.3k
            memcpy((char *)pdev + ppi->offset,
1020
30.3k
                   (char *)save_dev + ppi->offset,
1021
30.3k
                   gs_param_type_sizes[ppi->type]);
1022
416
        pdev->ForOPDFRead = save_dev->ForOPDFRead;
1023
416
    }
1024
416
    return ecode;
1025
681k
}
1026
1027
/* Put parameters */
1028
int
1029
gdev_pdf_put_params(gx_device * dev, gs_param_list * plist)
1030
834k
{
1031
834k
    int code;
1032
834k
    gx_device_pdf *pdev = (gx_device_pdf *) dev;
1033
834k
    gs_memory_t *mem = gs_memory_stable(pdev->memory);
1034
834k
    gx_device_pdf *save_dev = gs_malloc(mem, sizeof(gx_device_pdf), 1,
1035
834k
        "saved gx_device_pdf");
1036
1037
834k
    if (!save_dev)
1038
0
        return_error(gs_error_VMerror);
1039
834k
    memcpy(save_dev, pdev, sizeof(gx_device_pdf));
1040
834k
    code = gdev_pdf_put_params_impl(dev, save_dev, plist);
1041
834k
    gs_free(mem, save_dev, sizeof(gx_device_pdf), 1, "saved gx_device_pdf");
1042
834k
    return code;
1043
834k
}
1044
1045
/* ---------------- Process DSC comments ---------------- */
1046
1047
/* Bug #695850 DSC comments are not actually encoded, nor are they PostScript strings
1048
 * they are simply a sequence of bytes. SO it would seem we should just preserve that
1049
 * byte sequence. Bizarrely, Distiller treats the comment as 'almost' a PostScript
1050
 * string. In particular it converts octal codes into an equivalent binary byte. It
1051
 * also converts (eg) '\n' and '\r' into 'n' and 'r' and invalid octal escapes
1052
 * (eg \11) simply have the '\' turned into a '?'.
1053
 * We think this is nuts and have no intention of trying to mimic it precisely. This
1054
 * routine will find octal escapes and convert them into binary. The way this works is
1055
 * a little obscure. The DSC parser does convert the comment into a PostScript string
1056
 * and so has to escape any unusual characters. This means our octal escaped values in
1057
 * the original DSC comment have had the escape character ('\') escaped to become '\\'.
1058
 * All we need to do is remove the escape of the escape and we will end up with a
1059
 * properly escaped PostScript string.
1060
 */
1061
static int unescape_octals(gx_device_pdf * pdev, char *src, int size)
1062
12.0k
{
1063
12.0k
    char *start, *dest;
1064
1065
12.0k
    start = src;
1066
12.0k
    dest = src;
1067
1068
567k
    while(size) {
1069
555k
        if (size > 4 && src[0] == '\\' && src[1] == '\\' &&
1070
3.35k
                src[2] > 0x29 && src[2] < 0x35 &&
1071
78
                src[3] > 0x29 &&src[3] < 0x38 &&
1072
54
                src[4] > 0x29 && src[4] < 0x38) {
1073
52
            src++;
1074
52
            size--;
1075
555k
        } else {
1076
555k
            *dest++ = *src++;
1077
555k
            size--;
1078
555k
        }
1079
555k
    }
1080
12.0k
    return (dest - start);
1081
12.0k
}
1082
1083
static int
1084
pdf_dsc_process(gx_device_pdf * pdev, const gs_param_string_array * pma)
1085
22.3k
{
1086
    /*
1087
     * The Adobe "Distiller Parameters" documentation says that Distiller
1088
     * looks at DSC comments, but it doesn't say which ones.  We look at
1089
     * the ones that we see how to map directly to obvious PDF constructs.
1090
     */
1091
22.3k
    int code = 0;
1092
22.3k
    uint i;
1093
1094
    /*
1095
     * If ParseDSCComments is false, all DSC comments are ignored, even if
1096
     * ParseDSCComentsForDocInfo or PreserveEPSInfo is true.
1097
     */
1098
22.3k
    if (!pdev->ParseDSCComments)
1099
0
        return 0;
1100
1101
44.2k
    for (i = 0; i + 1 < pma->size && code >= 0; i += 2) {
1102
21.9k
        const gs_param_string *pkey = &pma->data[i];
1103
21.9k
        gs_param_string *pvalue = (gs_param_string *)&pma->data[i + 1];
1104
21.9k
        const char *key;
1105
21.9k
        int newsize;
1106
1107
        /*
1108
         * %%For, %%Creator, and %%Title are recognized only if either
1109
         * ParseDSCCommentsForDocInfo or PreserveEPSInfo is true.
1110
         * The other DSC comments are always recognized.
1111
         *
1112
         * Acrobat Distiller sets CreationDate and ModDate to the current
1113
         * time, not the value of %%CreationDate.  We think this is wrong,
1114
         * but we do the same -- we ignore %%CreationDate here.
1115
         */
1116
1117
21.9k
        if (pdf_key_eq(pkey, "Creator")) {
1118
4.15k
            key = "/Creator";
1119
4.15k
            newsize = unescape_octals(pdev, (char *)pvalue->data, pvalue->size);
1120
4.15k
            code = cos_dict_put_c_key_string(pdev->Info, key,
1121
4.15k
                                             pvalue->data, newsize);
1122
4.15k
            continue;
1123
17.7k
        } else if (pdf_key_eq(pkey, "Title")) {
1124
7.11k
            key = "/Title";
1125
7.11k
            newsize = unescape_octals(pdev, (char *)pvalue->data, pvalue->size);
1126
7.11k
            code = cos_dict_put_c_key_string(pdev->Info, key,
1127
7.11k
                                             pvalue->data, newsize);
1128
7.11k
            continue;
1129
10.6k
        } else if (pdf_key_eq(pkey, "For")) {
1130
770
            key = "/Author";
1131
770
            newsize = unescape_octals(pdev, (char *)pvalue->data, pvalue->size);
1132
770
            code = cos_dict_put_c_key_string(pdev->Info, key,
1133
770
                                             pvalue->data, newsize);
1134
770
            continue;
1135
9.89k
        } else {
1136
9.89k
            pdf_page_dsc_info_t *ppdi;
1137
9.89k
            char scan_buf[200]; /* arbitrary */
1138
1139
9.89k
            if ((ppdi = &pdev->doc_dsc_info,
1140
9.89k
                 pdf_key_eq(pkey, "Orientation")) ||
1141
9.89k
                (ppdi = &pdev->page_dsc_info,
1142
9.89k
                 pdf_key_eq(pkey, "PageOrientation"))
1143
9.89k
                ) {
1144
2
                if (pvalue->size == 1 && pvalue->data[0] >= '0' &&
1145
2
                    pvalue->data[0] <= '3'
1146
2
                    )
1147
2
                    ppdi->orientation = pvalue->data[0] - '0';
1148
0
                else
1149
0
                    ppdi->orientation = -1;
1150
9.89k
            } else if ((ppdi = &pdev->doc_dsc_info,
1151
9.89k
                        pdf_key_eq(pkey, "ViewingOrientation")) ||
1152
9.89k
                       (ppdi = &pdev->page_dsc_info,
1153
9.89k
                        pdf_key_eq(pkey, "PageViewingOrientation"))
1154
9.89k
                       ) {
1155
0
                gs_matrix mat;
1156
0
                int orient;
1157
1158
0
                if(pvalue->size >= sizeof(scan_buf) - 1)
1159
0
                    continue; /* error */
1160
0
                memcpy(scan_buf, pvalue->data, pvalue->size);
1161
0
                scan_buf[pvalue->size] = 0;
1162
0
                if (sscanf(scan_buf, "[%g %g %g %g]",
1163
0
                           &mat.xx, &mat.xy, &mat.yx, &mat.yy) != 4
1164
0
                    )
1165
0
                    continue; /* error */
1166
0
                for (orient = 0; orient < 4; ++orient) {
1167
0
                    if (mat.xx == 1 && mat.xy == 0 && mat.yx == 0 && mat.yy == 1)
1168
0
                        break;
1169
0
                    gs_matrix_rotate(&mat, -90.0, &mat);
1170
0
                }
1171
0
                if (orient == 4) /* error */
1172
0
                    orient = -1;
1173
0
                ppdi->viewing_orientation = orient;
1174
9.89k
            } else {
1175
9.89k
                gs_rect box;
1176
1177
9.89k
                if (pdf_key_eq(pkey, "EPSF")) {
1178
1.73k
                    pdev->is_EPS = (pvalue->size >= 1 && pvalue->data[0] != '0');
1179
1.73k
                    continue;
1180
1.73k
                }
1181
                /*
1182
                 * We only parse the BoundingBox for the sake of
1183
                 * AutoPositionEPSFiles.
1184
                 */
1185
8.16k
                if (pdf_key_eq(pkey, "BoundingBox"))
1186
3.48k
                    ppdi = &pdev->doc_dsc_info;
1187
4.68k
                else if (pdf_key_eq(pkey, "PageBoundingBox"))
1188
0
                    ppdi = &pdev->page_dsc_info;
1189
4.68k
                else
1190
4.68k
                    continue;
1191
3.48k
                if(pvalue->size >= sizeof(scan_buf) - 1)
1192
0
                    continue; /* error */
1193
3.48k
                memcpy(scan_buf, pvalue->data, pvalue->size);
1194
3.48k
                scan_buf[pvalue->size] = 0;
1195
3.48k
                if (sscanf(scan_buf, "[%lg %lg %lg %lg]",
1196
3.48k
                           &box.p.x, &box.p.y, &box.q.x, &box.q.y) != 4
1197
3.48k
                    )
1198
0
                    continue; /* error */
1199
3.48k
                ppdi->bounding_box = box;
1200
3.48k
            }
1201
3.48k
            continue;
1202
9.89k
        }
1203
21.9k
    }
1204
22.3k
    return code;
1205
22.3k
}