Debian Codegraph @

pdf/pdf_device.c

raw
1 /* Copyright (C) 2019-2025 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 /* Routines for dealing with devices */
17
18 #include "pdf_int.h"
19 #include "pdf_stack.h"
20 #include "pdf_device.h"
21 #include "gsdevice.h" /* For gs_setdevice_no_erase */
22 #include "gspaint.h" /* For gs_erasepage */
23 #include "gdevvec.h" /* for gs_device_vector */
24 #include "gxdevsop.h" /* For special ops : dev_param_req_t */
25
26 int pdfi_device_check_param(gx_device *dev, const char *param, gs_c_param_list *list)
27 {
28 dev_param_req_t request;
29 int code;
30
31 gs_c_param_list_write(list, dev->memory);
32 /* Stuff the data into a structure for passing to the spec_op */
33 request.Param = (char *)param;
34 request.list = list;
35 code = dev_proc(dev, dev_spec_op)(dev, gxdso_get_dev_param, &request, sizeof(dev_param_req_t));
36 if (code < 0) {
37 gs_c_param_list_release(list);
38 return code;
39 }
40 return 0;
41 }
42
43 /* Check value of boolean device parameter */
44 bool pdfi_device_check_param_bool(gx_device *dev, const char *param)
45 {
46 int code;
47 gs_c_param_list list;
48 bool value;
49
50 code = pdfi_device_check_param(dev, param, &list);
51 if (code < 0)
52 return false;
53 gs_c_param_list_read(&list);
54 code = param_read_bool((gs_param_list *)&list,
55 param,
56 &value);
57 if (code < 0)
58 value = false;
59 gs_c_param_list_release(&list);
60 return value;
61 }
62
63 /* Set value of string device parameter */
64 int pdfi_device_set_param_string(gx_device *dev, const char *paramname, const char *value)
65 {
66 int code;
67 gs_c_param_list list;
68 gs_param_string paramstring;
69
70 paramstring.data = (byte *)value;
71 paramstring.size = strlen(value);
72 paramstring.persistent = 0;
73
74 gs_c_param_list_write(&list, dev->memory);
75
76 gs_param_list_set_persistent_keys((gs_param_list *) &list, false);
77 code = param_write_string((gs_param_list *)&list, paramname, &paramstring);
78 if (code < 0) goto exit;
79 gs_c_param_list_read(&list);
80 code = gs_putdeviceparams(dev, (gs_param_list *)&list);
81
82 exit:
83 gs_c_param_list_release(&list);
84 return code;
85 }
86
87 /* Set value of boolean device parameter */
88 int pdfi_device_set_param_bool(gx_device *dev, const char *param, bool value)
89 {
90 int code;
91 gs_c_param_list list;
92 bool paramval = value;
93
94 gs_c_param_list_write(&list, dev->memory);
95
96 code = param_write_bool((gs_param_list *)&list, param, &paramval);
97 if (code < 0) goto exit;
98 gs_c_param_list_read(&list);
99 code = gs_putdeviceparams(dev, (gs_param_list *)&list);
100
101 exit:
102 gs_c_param_list_release(&list);
103 return code;
104 }
105
106 int pdfi_device_set_param_float(gx_device *dev, const char *param, float value)
107 {
108 int code;
109 gs_c_param_list list;
110 float paramval = value;
111
112 gs_c_param_list_write(&list, dev->memory);
113
114 code = param_write_float((gs_param_list *)&list, param, &paramval);
115 if (code < 0) goto exit;
116 gs_c_param_list_read(&list);
117 code = gs_putdeviceparams(dev, (gs_param_list *)&list);
118
119 exit:
120 gs_c_param_list_release(&list);
121 return code;
122 }
123
124 /* Checks whether a parameter exists for the device */
125 bool pdfi_device_check_param_exists(gx_device *dev, const char *param)
126 {
127 int code;
128 gs_c_param_list list;
129
130 code = pdfi_device_check_param(dev, param, &list);
131 if (code < 0)
132 return false;
133 gs_c_param_list_release(&list);
134 return true;
135 }
136
137 /* Config some device-related variables */
138 void pdfi_device_set_flags(pdf_context *ctx)
139 {
140 bool has_pdfmark;
141 bool has_ForOPDFRead;
142 gx_device *dev = ctx->pgs->device;
143
144 has_pdfmark = pdfi_device_check_param_exists(dev, "pdfmark");
145 has_ForOPDFRead = pdfi_device_check_param_bool(dev, "ForOPDFRead");
146
147 /* Cache these so they don't have to constantly be calculated */
148 ctx->device_state.writepdfmarks = has_pdfmark || ctx->args.dopdfmarks;
149 ctx->device_state.annotations_preserved = ctx->device_state.writepdfmarks && !has_ForOPDFRead;
150
151 /* PreserveTrMode is for pdfwrite device */
152 ctx->device_state.preserve_tr_mode = pdfi_device_check_param_bool(dev, "PreserveTrMode");
153 ctx->device_state.preserve_smask = pdfi_device_check_param_bool(dev, "PreserveSMask");
154 ctx->device_state.HighLevelDevice = pdfi_device_check_param_bool(dev, "HighLevelDevice");
155 ctx->device_state.ForOPDFRead = pdfi_device_check_param_bool(dev, "ForOPDFRead");
156 ctx->device_state.WantsPageLabels = pdfi_device_check_param_bool(dev, "WantsPageLabels");
157 ctx->device_state.WantsOptionalContent = pdfi_device_check_param_bool(dev, "WantsOptionalContent");
158 ctx->device_state.PassUserUnit = pdfi_device_check_param_bool(dev, "PassUserUnit");
159
160 /* See if it is a DeviceN (spot capable) */
161 ctx->device_state.spot_capable = (dev_proc(dev, dev_spec_op)(dev, gxdso_supports_devn, NULL, 0) > 0);
162
163 ctx->device_state.ModifiesPageSize = pdfi_device_check_param_bool(dev, "ModifiesPageSize");
164 ctx->device_state.ModifiesPageOrder = pdfi_device_check_param_bool(dev, "ModifiesPageOrder");
165
166 /* If multi-page output, can't do certain pdfmarks */
167 if (ctx->device_state.writepdfmarks) {
168 if (gx_outputfile_is_separate_pages(((gx_device_vector *)dev)->fname, dev->memory) || ctx->device_state.ModifiesPageOrder) {
169 ctx->args.no_pdfmark_outlines = true;
170 ctx->args.no_pdfmark_dests = true;
171 }
172 }
173
174 #if DEBUG_DEVICE
175 dbgmprintf2(ctx->memory, "Device writepdfmarks=%s, annotations_preserved=%s\n",
176 ctx->writepdfmarks ? "TRUE" : "FALSE",
177 ctx->annotations_preserved ? "TRUE" : "FALSE");
178 #endif
179 }
180
181 /* Config the output device
182 * This will configure any special device parameters.
183 * Right now it just sets up some stuff for pdfwrite.
184 */
185 int pdfi_device_misc_config(pdf_context *ctx)
186 {
187 bool has_pdfmark = false;
188 int code;
189 gx_device *dev = ctx->pgs->device;
190
191 if (ctx->args.first_page != 0 || ctx->args.last_page != 0) {
192 code = pdfi_device_set_param_bool(dev, "DisablePageHandler", true);
193 if (code < 0) goto exit;
194 }
195
196 /* I am using pdfmark to identify the pdfwrite device */
197 has_pdfmark = pdfi_device_check_param_bool(dev, "pdfmark");
198 /* (only handling pdfwrite for now) */
199 if (!has_pdfmark)
200 return 0;
201
202 /* TODO: I think the pdfwrite device should have these automatically set to true,
203 * but that doesn't seem to be the case now.
204 * See pdf_document_metadata()
205 */
206 code = pdfi_device_set_param_string(dev, "AutoRotatePages", "PageByPage");
207 if (code < 0) goto exit;
208
209 exit:
210 return code;
211 }