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
|
/* Copyright (C) 2001-2023 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.
*/
/* plparams.c - PJL handling of pdfwrite device parameters */
#include "std.h"
#include "gsmemory.h"
#include "gsmatrix.h" /* for gsdevice.h */
#include "gsdevice.h"
#include "gsparam.h"
#include "gp.h"
#include "gserrors.h"
#include "string_.h"
#include "plparams.h"
#include <stdlib.h>
/*--------------------------------------------- pdfmark -------------------------------------------*/
/* Add a newly created param_string array to a param list, and then use that
* param list as an argument to put the device parameters.
*/
static int pdfmark_write_list(gs_memory_t *mem, gx_device *device, gs_param_string_array *array_list)
{
gs_c_param_list list;
int code;
/* Set the list to writeable, and initialise it */
gs_c_param_list_write(&list, mem);
/* We don't want keys to be persistent, as we are going to throw
* away our array, force them to be copied
*/
gs_param_list_set_persistent_keys((gs_param_list *) &list, false);
/* Make really sure the list is writable, but don't initialise it */
gs_c_param_list_write_more(&list);
/* Add the param string array to the list */
code = param_write_string_array((gs_param_list *)&list, "pdfmark", array_list);
if (code < 0)
return code;
/* Set the param list back to readable, so putceviceparams can readit (mad...) */
gs_c_param_list_read(&list);
/* and set the actual device parameters */
code = gs_putdeviceparams(device, (gs_param_list *)&list);
/* release the memory allocated for the list parameter */
gs_c_param_list_release(&list);
return code;
}
/* pdfmark operations always take an array parameter. Because (see below) we
* know that array valuess must be homogenous types we can treat them all as
* parameter strings.
*/
static int process_pdfmark(gs_memory_t *mem, gx_device *device, char *pdfmark)
{
char *p, *start, *copy, *stream_data = 0L;
int tokens = 0, code = 0;
gs_param_string *parray;
gs_param_string_array array_list;
bool putdict = false;
/* Our parsing will alter the string contents, so copy it and perform the parsing on the copy */
copy = (char *)gs_alloc_bytes(mem, strlen(pdfmark) + 1, "working buffer for pdfmark processing");
if (copy == 0)
return -1;
strcpy(copy, pdfmark);
start = copy + 1;
if (*pdfmark != '[') {
gs_free_object(mem, copy, "working buffer for pdfmark processing");
return -1;
}
p = start;
while (*p != 0x00){
if(*p == '(') {
while (*p != ')' && *p != 0x00) {
if (*p == '\\')
p++;
p++;
}
if (*p != ')') {
gs_free_object(mem, copy, "working buffer for pdfmark processing");
return -1;
}
} else {
if (*p == ' ') {
tokens++;
}
}
p++;
}
if (*(p-1) != ' ')
tokens++;
/* We need an extra one for a dummy CTM */
tokens++;
parray = (gs_param_string *)gs_alloc_bytes(mem, tokens * sizeof(gs_param_string), "temporary pdfmark array");
if (!parray) {
gs_free_object(mem, copy, "working buffer for pdfmark processing");
return -1;
}
tokens = 0;
while (*start == ' ')
start++;
p = start;
while (*p != 0x00){
if(*p == '(') {
while (*p != ')' && *p != 0x00) {
if (*p == '\\')
p++;
p++;
}
if (*p != ')') {
gs_free_object(mem, copy, "working buffer for pdfmark processing");
return -1;
}
} else {
if (*p == ' ') {
if (strncmp(start, "<<", 2) == 0) {
putdict = true;
} else {
if (strncmp(start, ">>", 2) != 0) {
*p = 0x00;
parray[tokens].data = (const byte *)start;
parray[tokens].size = strlen(start);
parray[tokens++].persistent = false;
}
}
start = ++p;
} else
p++;
}
}
if (*(p-1) != ' ') {
parray[tokens].data = (const byte *)start;
parray[tokens].size = strlen(start);
parray[tokens++].persistent = false;
}
/* Move last entry up one and add a dummy CTM where it used to be */
parray[tokens].data = parray[tokens - 1].data;
parray[tokens].size = parray[tokens - 1].size;
parray[tokens].persistent = parray[tokens - 1].persistent;
parray[tokens - 1].data = (const byte *)"[0 0 0 0 0 0]";
parray[tokens - 1].size = 13;
parray[tokens - 1].persistent = false;
/* Features are name objects (ie they start with a '/') but putdeviceparams wants them
* as strings without the '/', so we need to strip that here.
*/
if (parray[tokens].data[0] != '/') {
gs_free_object(mem, copy, "working buffer for pdfmark processing");
gs_free_object(mem, parray, "temporary pdfmark array");
return -1;
} else {
parray[tokens].data++;
parray[tokens].size--;
}
/* We need to convert a 'PUT' with a dictonary into a 'PUTDICT', this is normally done
* in PostScript
*/
if (putdict && strncmp((const char *)(parray[tokens].data), "PUT", 3) == 0) {
parray[tokens].data = (const byte *)".PUTDICT";
parray[tokens].size = 8;
parray[tokens].persistent = false;
}
/* We also need some means to handle file data. Normally ths is done by creating a
* PostScript file object and doing a 'PUT', but we can't do that, so we define a
* special variety of 'PUT' called 'PUTFILE' and we handle that here.
*/
if (strncmp((const char *)(parray[tokens].data), "PUTFILE", 7) == 0) {
gp_file *f;
char *filename;
int bytes;
if (parray[tokens - 2].data[0] != '(') {
gs_free_object(mem, copy, "working buffer for pdfmark processing");
gs_free_object(mem, parray, "temporary pdfmark array");
return -1;
}
filename = (char *)&(parray[tokens - 2].data[1]);
filename[strlen(filename) - 1] = 0x00;
f = gp_fopen(mem, (const char *)filename, "rb");
if (!f) {
gs_free_object(mem, copy, "working buffer for pdfmark processing");
gs_free_object(mem, parray, "temporary pdfmark array");
return -1;
}
if (gp_fseek(f, 0, SEEK_END) != 0) {
gs_free_object(mem, copy, "working buffer for pdfmark processing");
gs_free_object(mem, parray, "temporary pdfmark array");
gp_fclose(f);
return -1;
}
bytes = gp_ftell(f);
if (bytes < 0) {
gs_free_object(mem, copy, "working buffer for pdfmark processing");
gs_free_object(mem, parray, "temporary pdfmark array");
gp_fclose(f);
return -1;
}
parray[tokens - 2].data = (const byte *)gs_alloc_bytes(mem, bytes, "PJL pdfmark, stream");
if (!parray[tokens - 2].data) {
gs_free_object(mem, copy, "working buffer for pdfmark processing");
gs_free_object(mem, parray, "temporary pdfmark array");
gp_fclose(f);
return -1;
}
stream_data = (char *)(parray[tokens - 2].data);
if (gp_fseek(f, 0, SEEK_SET) != 0) {
gs_free_object(mem, stream_data, "PJL pdfmark, stream");
gs_free_object(mem, copy, "working buffer for pdfmark processing");
gs_free_object(mem, parray, "temporary pdfmark array");
gp_fclose(f);
return gs_note_error(gs_error_ioerror);
}
code = gp_fread(stream_data, 1, bytes, f);
if (code != bytes) {
gs_free_object(mem, stream_data, "PJL pdfmark, stream");
gs_free_object(mem, copy, "working buffer for pdfmark processing");
gs_free_object(mem, parray, "temporary pdfmark array");
gp_fclose(f);
return gs_note_error(gs_error_ioerror);
}
gp_fclose(f);
parray[tokens - 2].size = bytes;
parray[tokens].data = (const byte *)".PUTSTREAM";
parray[tokens].size = 10;
parray[tokens].persistent = false;
}
array_list.data = parray;
array_list.persistent = 0;
array_list.size = ++tokens;
code = pdfmark_write_list(mem, device, &array_list);
if (stream_data)
gs_free_object(mem, stream_data, "PJL pdfmark, stream");
gs_free_object(mem, copy, "working buffer for pdfmark processing");
gs_free_object(mem, parray, "temporary pdfmark array");
return code;
}
int pcl_pjl_pdfmark(gs_memory_t *mem, gx_device *device, char *pdfmark)
{
char *pdfmark_start, *token_start, end, *p;
int code;
p = token_start = pdfmark_start = pdfmark + 1;
do {
while (*p != ' ' && *p != '"' && *p != 0x00)
p++;
if((p - token_start) != 7 || strncmp(token_start, "pdfmark", 7) != 0){
if (*p != 0x00)
token_start = ++p;
else
break;
} else {
token_start--;
end = *token_start;
*token_start = 0x00;
code = process_pdfmark(mem, device, pdfmark_start);
if (code < 0)
return code;
*token_start = end;
token_start = pdfmark_start = ++p;
}
} while (*p != 0x00);
return 0;
}
/*--------------------------------------------- distillerparams -------------------------------------------*/
int pcl_pjl_setdistillerparams(gs_memory_t *mem, gx_device *device, char *distillerparams)
{
char *p, *start, *copy;
int code = 0;
gs_c_param_list *plist;
plist = gs_c_param_list_alloc(mem, "temp C param list for PJL distillerparams");
if (plist == NULL)
return -1;
gs_c_param_list_write(plist, mem);
gs_param_list_set_persistent_keys((gs_param_list *) plist, false);
gs_c_param_list_write_more(plist);
/* Our parsing will alter the string contents, so copy it and perform the parsing on the copy */
copy = (char *)gs_alloc_bytes(mem, strlen(distillerparams) + 1, "working buffer for distillerparams processing");
if (copy == 0)
return -1;
strcpy(copy, distillerparams);
if (*copy == '"') {
start = copy + 1;
copy[strlen(copy) - 1] = 0x00;
}
else
start = copy;
if (*start != '<' || start[1] != '<') {
gs_free_object(mem, copy, "working buffer for distillerparams processing");
return -1;
}
start += 2;
if (copy[strlen(copy) - 1] != '>' || copy[strlen(copy) - 2] != '>') {
gs_free_object(mem, copy, "working buffer for distillerparams processing");
return -1;
}
copy[strlen(copy) - 2] = 0x00;
while (*start == ' ')
start++;
p = start;
/* Parse the string, adding what we find to the plist */
code = gs_param_list_add_tokens((gs_param_list *)plist, p);
if (code < 0) {
gs_c_param_list_release(plist);
return code;
}
/* Discard our working copy of the string */
gs_free_object(mem, copy, "working buffer for distillerparams processing");
/* Set the list to 'read' (bonkers...) */
gs_c_param_list_read(plist);
/* Actually set the newly created device parameters */
code = gs_putdeviceparams(device, (gs_param_list *)plist);
/* And throw away the plist, we're done with it */
gs_c_param_list_release(plist);
return code;
}
|