summaryrefslogtreecommitdiff
path: root/gs/src/gdevpdfp.c
blob: 000de4c3a0f5d64a2bdaef4b255c7c29e61c0d66 (plain)
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
/* Copyright (C) 1996, 1997, 1999 Aladdin Enterprises.  All rights reserved.

   This file is part of Aladdin Ghostscript.

   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
   or distributor accepts any responsibility for the consequences of using it,
   or for whether it serves any particular purpose or works at all, unless he
   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
   License (the "License") for full details.

   Every copy of Aladdin Ghostscript must include a copy of the License,
   normally in a plain ASCII text file named PUBLIC.  The License grants you
   the right to copy, modify and redistribute Aladdin Ghostscript, but only
   under certain conditions described in the License.  Among other things, the
   License requires that the copyright notice and this notice be preserved on
   all copies.
 */


/* Get/put parameters for PDF-writing driver */
#include "gx.h"
#include "gserrors.h"
#include "gdevpdfx.h"
#include "gsparamx.h"

/*
 * The pdfwrite device supports the following "real" parameters:
 *      OutputFile <string>
 *      all the Distiller parameters -- see gdevpsdp.c
 * Only some of the Distiller parameters actually have any effect.
 *
 * The device also supports the following write-only pseudo-parameters that
 * serve only to communicate other information from the PostScript file.
 * Their "value" is an array of strings, some of which may be the result
 * of converting arbitrary PostScript objects to string form.
 *      pdfmark - see gdevpdfm.c
 *      show - see gdevpdft.c
 */

private const int CoreDistVersion = 3000;	/* Distiller 3.0 */

/* ---------------- Get parameters ---------------- */

/* Get parameters. */
int
gdev_pdf_get_params(gx_device * dev, gs_param_list * plist)
{
    gx_device_pdf *pdev = (gx_device_pdf *) dev;
    int code = gdev_psdf_get_params(dev, plist);

    if (code < 0 ||
	(code = param_write_float(plist, "CompatibilityLevel",
				  &pdev->CompatibilityLevel)) < 0 ||
	(code = param_write_int(plist, "CoreDistVersion",
				(int *)&CoreDistVersion)) < 0 ||
	(code = param_write_bool(plist, "ReAssignCharacters",
				 &pdev->ReAssignCharacters)) < 0 ||
	(code = param_write_bool(plist, "ReEncodeCharacters",
				 &pdev->ReEncodeCharacters)) < 0 ||
	(code = param_write_long(plist, "FirstObjectNumber",
				 &pdev->FirstObjectNumber)) < 0
	);
    return code;
}

/* ---------------- Put parameters ---------------- */

/* Put parameters. */
int
gdev_pdf_put_params(gx_device * dev, gs_param_list * plist)
{
    gx_device_pdf *pdev = (gx_device_pdf *) dev;
    int ecode = 0;
    int code;
    float cl = pdev->CompatibilityLevel;
    bool rac = pdev->ReAssignCharacters;
    bool rec = pdev->ReEncodeCharacters;
    long fon = pdev->FirstObjectNumber;
    gs_param_name param_name;
    psdf_version save_version = pdev->version;

    /*
     * If this is one of the pseudo-parameters (show or pdfmark),
     * don't bother checking for any real ones.
     */

    {
	gs_param_string pps;

	code = param_read_string(plist, (param_name = "show"), &pps);
	switch (code) {
	    case 0:
		pdf_open_document(pdev);
		code = pdfshow_process(pdev, plist, &pps);
		if (code >= 0)
		    return code;
		/* falls through for errors */
	    default:
		param_signal_error(plist, param_name, code);
		return code;
	    case 1:
		break;
	}
    }

    {
	gs_param_string_array ppa;

	code = param_read_string_array(plist, (param_name = "pdfmark"), &ppa);
	switch (code) {
	    case 0:
		pdf_open_document(pdev);
		code = pdfmark_process(pdev, &ppa);
		if (code >= 0)
		    return code;
		/* falls through for errors */
	    default:
		param_signal_error(plist, param_name, code);
		return code;
	    case 1:
		break;
	}
    }

    /* General parameters. */

    switch (code = param_read_float(plist, (param_name = "CompatibilityLevel"), &cl)) {
	default:
	    ecode = code;
	    param_signal_error(plist, param_name, ecode);
	case 0:
	case 1:
	    break;
    }

    {
	int cdv = CoreDistVersion;

	ecode = param_put_int(plist, (param_name = "CoreDistVersion"), &cdv, ecode);
	if (cdv != CoreDistVersion)
	    param_signal_error(plist, param_name, ecode = gs_error_rangecheck);
    }

    ecode = param_put_bool(plist, "ReAssignCharacters", &rac, ecode);
    ecode = param_put_bool(plist, "ReEncodeCharacters", &rec, ecode);
    switch (code = param_read_long(plist, (param_name = "FirstObjectNumber"), &fon)) {
	case 0:
	    /*
	     * Setting this parameter is only legal if the file
	     * has just been opened and nothing has been written,
	     * or if we are setting it to the same value.
	     */
	    if (fon == pdev->FirstObjectNumber)
		break;
	    if (fon <= 0 || fon > 0x7fff0000 ||
		(pdev->next_id != 0 &&
		 pdev->next_id !=
		 pdev->FirstObjectNumber + pdf_num_initial_ids)
		)
		ecode = gs_error_rangecheck;
	    else
		break;
	default:
	    ecode = code;
	    param_signal_error(plist, param_name, ecode);
	case 1:
	    break;
    }

    if (ecode < 0)
	return ecode;
    /*
     * We have to set version to the new value, because the set of
     * legal parameter values for psdf_put_params varies according to
     * the version.
     */
    pdev->version =
	(cl < 1.2 ? psdf_version_level2 : psdf_version_ll3);
    code = gdev_psdf_put_params(dev, plist);
    if (code < 0) {
	pdev->version = save_version;
	return code;
    }
    pdev->CompatibilityLevel = cl;
    pdev->ReAssignCharacters = rac;
    pdev->ReEncodeCharacters = rec;
    if (fon != pdev->FirstObjectNumber) {
	pdev->FirstObjectNumber = fon;
	if (pdev->tfile != 0) {
	    fseek(pdev->tfile, 0L, SEEK_SET);
	    pdf_initialize_ids(pdev);
	}
    }
    return 0;
}