]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
c0e1af83 | 2 | * "$Id: pstops.c 6420 2007-03-30 20:00:59Z mike $" |
ef416fc2 | 3 | * |
4 | * PostScript filter for the Common UNIX Printing System (CUPS). | |
5 | * | |
b86bc4cf | 6 | * Copyright 1993-2007 by Easy Software Products. |
ef416fc2 | 7 | * |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * This file is subject to the Apple OS-Developed Software exception. | |
25 | * | |
26 | * Contents: | |
27 | * | |
80ca4592 | 28 | * main() - Main entry... |
29 | * add_page() - Add a page to the pages array... | |
30 | * check_range() - Check to see if the current page is selected for | |
31 | * copy_bytes() - Copy bytes from the input file to stdout... | |
32 | * copy_comments() - Copy all of the comments section... | |
33 | * copy_dsc() - Copy a DSC-conforming document... | |
34 | * copy_non_dsc() - Copy a document that does not conform to the DSC... | |
35 | * copy_page() - Copy a page description... | |
36 | * copy_prolog() - Copy the document prolog section... | |
37 | * copy_setup() - Copy the document setup section... | |
38 | * copy_trailer() - Copy the document trailer... | |
39 | * do_prolog() - Send the necessary document prolog commands... | |
40 | * do_setup() - Send the necessary document setup commands... | |
41 | * doc_printf() - Send a formatted string to stdout and/or the temp | |
42 | * file. | |
43 | * doc_puts() - Send a nul-terminated string to stdout and/or the | |
44 | * temp file. | |
45 | * doc_write() - Send data to stdout and/or the temp file. | |
46 | * end_nup() - End processing for N-up printing... | |
47 | * include_feature() - Include a printer option/feature command. | |
48 | * parse_text() - Parse a text value in a comment... | |
49 | * set_pstops_options() - Set pstops options... | |
50 | * skip_page() - Skip past a page that won't be printed... | |
51 | * start_nup() - Start processing for N-up printing... | |
ed486911 | 52 | * write_labels() - Write the actual page labels. |
ef416fc2 | 53 | */ |
54 | ||
55 | /* | |
56 | * Include necessary headers... | |
57 | */ | |
58 | ||
59 | #include "common.h" | |
e53920b9 | 60 | #include <limits.h> |
bd7854cb | 61 | #include <math.h> |
62 | #include <cups/file.h> | |
63 | #include <cups/array.h> | |
c0e1af83 | 64 | #include <cups/i18n.h> |
ef416fc2 | 65 | |
66 | ||
67 | /* | |
68 | * Constants... | |
69 | */ | |
70 | ||
80ca4592 | 71 | #define PSTOPS_BORDERNONE 0 /* No border or hairline border */ |
72 | #define PSTOPS_BORDERTHICK 1 /* Think border */ | |
73 | #define PSTOPS_BORDERSINGLE 2 /* Single-line hairline border */ | |
74 | #define PSTOPS_BORDERSINGLE2 3 /* Single-line thick border */ | |
75 | #define PSTOPS_BORDERDOUBLE 4 /* Double-line hairline border */ | |
76 | #define PSTOPS_BORDERDOUBLE2 5 /* Double-line thick border */ | |
ef416fc2 | 77 | |
80ca4592 | 78 | #define PSTOPS_LAYOUT_LRBT 0 /* Left to right, bottom to top */ |
79 | #define PSTOPS_LAYOUT_LRTB 1 /* Left to right, top to bottom */ | |
80 | #define PSTOPS_LAYOUT_RLBT 2 /* Right to left, bottom to top */ | |
81 | #define PSTOPS_LAYOUT_RLTB 3 /* Right to left, top to bottom */ | |
82 | #define PSTOPS_LAYOUT_BTLR 4 /* Bottom to top, left to right */ | |
83 | #define PSTOPS_LAYOUT_TBLR 5 /* Top to bottom, left to right */ | |
84 | #define PSTOPS_LAYOUT_BTRL 6 /* Bottom to top, right to left */ | |
85 | #define PSTOPS_LAYOUT_TBRL 7 /* Top to bottom, right to left */ | |
ef416fc2 | 86 | |
80ca4592 | 87 | #define PSTOPS_LAYOUT_NEGATEY 1 /* The bits for the layout */ |
88 | #define PSTOPS_LAYOUT_NEGATEX 2 /* definitions above... */ | |
89 | #define PSTOPS_LAYOUT_VERTICAL 4 | |
ef416fc2 | 90 | |
91 | ||
bd7854cb | 92 | /* |
93 | * Types... | |
94 | */ | |
95 | ||
96 | typedef struct /**** Page information ****/ | |
97 | { | |
98 | char *label; /* Page label */ | |
80ca4592 | 99 | int bounding_box[4]; /* PageBoundingBox */ |
bd7854cb | 100 | off_t offset; /* Offset to start of page */ |
101 | ssize_t length; /* Number of bytes for page */ | |
80ca4592 | 102 | int num_options; /* Number of options for this page */ |
103 | cups_option_t *options; /* Options for this page */ | |
104 | } pstops_page_t; | |
105 | ||
106 | typedef struct /**** Document information ****/ | |
107 | { | |
108 | int page; /* Current page */ | |
109 | int bounding_box[4]; /* BoundingBox from header */ | |
110 | int new_bounding_box[4]; /* New composite bounding box */ | |
111 | int num_options; /* Number of document-wide options */ | |
112 | cups_option_t *options; /* Document-wide options */ | |
113 | int normal_landscape, /* Normal rotation for landscape? */ | |
114 | saw_eof, /* Saw the %%EOF comment? */ | |
115 | slow_collate, /* Collate copies by hand? */ | |
116 | slow_duplex, /* Duplex pages slowly? */ | |
117 | slow_order, /* Reverse pages slowly? */ | |
118 | use_ESPshowpage; /* Use ESPshowpage? */ | |
119 | cups_array_t *pages; /* Pages in document */ | |
120 | cups_file_t *temp; /* Temporary file, if any */ | |
121 | char tempfile[1024]; /* Temporary filename */ | |
122 | int job_id; /* Job ID */ | |
123 | const char *user, /* User name */ | |
124 | *title; /* Job name */ | |
125 | int copies; /* Number of copies */ | |
126 | const char *ap_input_slot, /* AP_FIRSTPAGE_InputSlot value */ | |
127 | *ap_manual_feed; /* AP_FIRSTPAGE_ManualFeed value */ | |
128 | float brightness; /* brightness value */ | |
129 | int collate, /* Collate copies? */ | |
130 | emit_jcl, /* Emit JCL commands? */ | |
131 | fitplot; /* Fit pages to media */ | |
132 | float gamma; /* gamma value */ | |
133 | const char *input_slot, /* InputSlot value */ | |
134 | *manual_feed; /* ManualFeed value */ | |
135 | int mirror, /* doc->mirror/mirror pages */ | |
136 | number_up, /* Number of pages on each sheet */ | |
137 | number_up_layout, /* doc->number_up_layout of N-up pages */ | |
138 | output_order, /* Requested reverse output order? */ | |
139 | page_border; /* doc->page_border around pages */ | |
140 | const char *page_label, /* page-label option, if any */ | |
141 | *page_ranges, /* page-ranges option, if any */ | |
142 | *page_set; /* page-set option, if any */ | |
143 | } pstops_doc_t; | |
bd7854cb | 144 | |
145 | ||
ef416fc2 | 146 | /* |
80ca4592 | 147 | * Convenience macros... |
ef416fc2 | 148 | */ |
149 | ||
80ca4592 | 150 | #define is_first_page(p) (doc->number_up == 1 || \ |
151 | ((p) % doc->number_up) == 1) | |
152 | #define is_last_page(p) (doc->number_up == 1 || \ | |
153 | ((p) % doc->number_up) == 0) | |
154 | #define is_not_last_page(p) (doc->number_up > 1 && \ | |
155 | ((p) % doc->number_up) != 0) | |
ef416fc2 | 156 | |
157 | ||
158 | /* | |
159 | * Local functions... | |
160 | */ | |
161 | ||
80ca4592 | 162 | static pstops_page_t *add_page(pstops_doc_t *doc, const char *label); |
163 | static int check_range(pstops_doc_t *doc, int page); | |
bd7854cb | 164 | static void copy_bytes(cups_file_t *fp, off_t offset, |
165 | size_t length); | |
80ca4592 | 166 | static size_t copy_comments(cups_file_t *fp, pstops_doc_t *doc, |
2abf387c | 167 | ppd_file_t *ppd, char *line, |
168 | size_t linelen, size_t linesize); | |
80ca4592 | 169 | static void copy_dsc(cups_file_t *fp, pstops_doc_t *doc, |
170 | ppd_file_t *ppd, char *line, size_t linelen, | |
171 | size_t linesize); | |
172 | static void copy_non_dsc(cups_file_t *fp, pstops_doc_t *doc, | |
173 | ppd_file_t *ppd, char *line, | |
174 | size_t linelen, size_t linesize); | |
175 | static size_t copy_page(cups_file_t *fp, pstops_doc_t *doc, | |
176 | ppd_file_t *ppd, int number, char *line, | |
177 | size_t linelen, size_t linesize); | |
178 | static size_t copy_prolog(cups_file_t *fp, pstops_doc_t *doc, | |
179 | ppd_file_t *ppd, char *line, | |
180 | size_t linelen, size_t linesize); | |
181 | static size_t copy_setup(cups_file_t *fp, pstops_doc_t *doc, | |
182 | ppd_file_t *ppd, char *line, | |
183 | size_t linelen, size_t linesize); | |
184 | static size_t copy_trailer(cups_file_t *fp, pstops_doc_t *doc, | |
185 | ppd_file_t *ppd, int number, char *line, | |
186 | size_t linelen, size_t linesize); | |
187 | static void do_prolog(pstops_doc_t *doc, ppd_file_t *ppd); | |
188 | static void do_setup(pstops_doc_t *doc, ppd_file_t *ppd); | |
f301802f | 189 | static void doc_printf(pstops_doc_t *doc, const char *format, ...) |
190 | #ifdef __GNUC__ | |
191 | __attribute__ ((__format__ (__printf__, 2, 3))) | |
192 | #endif /* __GNUC__ */ | |
193 | ; | |
80ca4592 | 194 | static void doc_puts(pstops_doc_t *doc, const char *s); |
195 | static void doc_write(pstops_doc_t *doc, const char *s, size_t len); | |
196 | static void end_nup(pstops_doc_t *doc, int number); | |
197 | static int include_feature(ppd_file_t *ppd, const char *line, | |
198 | int num_options, | |
199 | cups_option_t **options); | |
200 | static char *parse_text(const char *start, char **end, char *buffer, | |
201 | size_t bufsize); | |
202 | static void set_pstops_options(pstops_doc_t *doc, ppd_file_t *ppd, | |
203 | char *argv[], int num_options, | |
204 | cups_option_t *options); | |
205 | static size_t skip_page(cups_file_t *fp, char *line, size_t linelen, | |
206 | size_t linesize); | |
207 | static void start_nup(pstops_doc_t *doc, int number, | |
208 | int show_border, const int *bounding_box); | |
b86bc4cf | 209 | static void write_label_prolog(pstops_doc_t *doc, const char *label, |
210 | float bottom, float top, | |
211 | float width); | |
ed486911 | 212 | static void write_labels(pstops_doc_t *doc, int orient); |
ef416fc2 | 213 | |
214 | ||
215 | /* | |
216 | * 'main()' - Main entry... | |
217 | */ | |
218 | ||
bd7854cb | 219 | int /* O - Exit status */ |
220 | main(int argc, /* I - Number of command-line args */ | |
221 | char *argv[]) /* I - Command-line arguments */ | |
ef416fc2 | 222 | { |
80ca4592 | 223 | pstops_doc_t doc; /* Document information */ |
224 | cups_file_t *fp; /* Print file */ | |
bd7854cb | 225 | ppd_file_t *ppd; /* PPD file */ |
bd7854cb | 226 | int num_options; /* Number of print options */ |
227 | cups_option_t *options; /* Print options */ | |
bd7854cb | 228 | char line[8192]; /* Line buffer */ |
bd7854cb | 229 | size_t len; /* Length of line buffer */ |
80ca4592 | 230 | |
ef416fc2 | 231 | |
232 | /* | |
233 | * Make sure status messages are not buffered... | |
234 | */ | |
235 | ||
236 | setbuf(stderr, NULL); | |
237 | ||
238 | /* | |
239 | * Check command-line... | |
240 | */ | |
241 | ||
242 | if (argc < 6 || argc > 7) | |
243 | { | |
c0e1af83 | 244 | fprintf(stderr, _("Usage: %s job-id user title copies options [file]\n"), |
245 | argv[0]); | |
ef416fc2 | 246 | return (1); |
247 | } | |
248 | ||
249 | /* | |
250 | * If we have 7 arguments, print the file named on the command-line. | |
251 | * Otherwise, send stdin instead... | |
252 | */ | |
253 | ||
254 | if (argc == 6) | |
80ca4592 | 255 | fp = cupsFileStdin(); |
ef416fc2 | 256 | else |
257 | { | |
258 | /* | |
259 | * Try to open the print file... | |
260 | */ | |
261 | ||
80ca4592 | 262 | if ((fp = cupsFileOpen(argv[6], "r")) == NULL) |
ef416fc2 | 263 | { |
c0e1af83 | 264 | fprintf(stderr, _("ERROR: Unable to open file \"%s\" - %s\n"), |
ef416fc2 | 265 | argv[6], strerror(errno)); |
266 | return (1); | |
267 | } | |
268 | } | |
269 | ||
270 | /* | |
80ca4592 | 271 | * Read the first line to see if we have DSC comments... |
ef416fc2 | 272 | */ |
273 | ||
80ca4592 | 274 | if ((len = cupsFileGetLine(fp, line, sizeof(line))) == 0) |
275 | { | |
c0e1af83 | 276 | fputs(_("ERROR: Empty print file!\n"), stderr); |
80ca4592 | 277 | return (1); |
278 | } | |
ef416fc2 | 279 | |
80ca4592 | 280 | /* |
281 | * Process command-line options... | |
282 | */ | |
ef416fc2 | 283 | |
284 | options = NULL; | |
285 | num_options = cupsParseOptions(argv[5], 0, &options); | |
80ca4592 | 286 | ppd = SetCommonOptions(num_options, options, 1); |
ef416fc2 | 287 | |
80ca4592 | 288 | set_pstops_options(&doc, ppd, argv, num_options, options); |
ef416fc2 | 289 | |
80ca4592 | 290 | /* |
291 | * Write any "exit server" options that have been selected... | |
292 | */ | |
ef416fc2 | 293 | |
80ca4592 | 294 | ppdEmit(ppd, stdout, PPD_ORDER_EXIT); |
ef416fc2 | 295 | |
80ca4592 | 296 | /* |
297 | * Write any JCL commands that are needed to print PostScript code... | |
298 | */ | |
bd7854cb | 299 | |
80ca4592 | 300 | if (doc.emit_jcl) |
301 | ppdEmitJCL(ppd, stdout, doc.job_id, doc.user, doc.title); | |
ef416fc2 | 302 | |
80ca4592 | 303 | /* |
304 | * Start with a DSC header... | |
305 | */ | |
ef416fc2 | 306 | |
80ca4592 | 307 | puts("%!PS-Adobe-3.0"); |
ef416fc2 | 308 | |
80ca4592 | 309 | /* |
310 | * Skip leading PJL in the document... | |
311 | */ | |
ef416fc2 | 312 | |
80ca4592 | 313 | while (!strncmp(line, "\033%-12345X", 9) || !strncmp(line, "@PJL ", 5)) |
bd7854cb | 314 | { |
315 | /* | |
80ca4592 | 316 | * Yup, we have leading PJL fun, so skip it until we hit the line |
317 | * with "ENTER LANGUAGE"... | |
bd7854cb | 318 | */ |
319 | ||
80ca4592 | 320 | fputs("DEBUG: Skipping PJL header...\n", stderr); |
bd7854cb | 321 | |
d6ae789d | 322 | while (strstr(line, "ENTER LANGUAGE") == NULL && strncmp(line, "%!", 2)) |
80ca4592 | 323 | if ((len = cupsFileGetLine(fp, line, sizeof(line))) == 0) |
324 | break; | |
ef416fc2 | 325 | |
d6ae789d | 326 | if (!strncmp(line, "%!", 2)) |
327 | break; | |
328 | ||
80ca4592 | 329 | if ((len = cupsFileGetLine(fp, line, sizeof(line))) == 0) |
330 | break; | |
bd7854cb | 331 | } |
332 | ||
ef416fc2 | 333 | /* |
80ca4592 | 334 | * Now see if the document conforms to the Adobe Document Structuring |
335 | * Conventions... | |
ef416fc2 | 336 | */ |
337 | ||
80ca4592 | 338 | if (!strncmp(line, "%!PS-Adobe-", 11)) |
bd7854cb | 339 | { |
340 | /* | |
80ca4592 | 341 | * Yes, filter the document... |
bd7854cb | 342 | */ |
343 | ||
80ca4592 | 344 | copy_dsc(fp, &doc, ppd, line, len, sizeof(line)); |
bd7854cb | 345 | } |
80ca4592 | 346 | else |
ef416fc2 | 347 | { |
348 | /* | |
80ca4592 | 349 | * No, display an error message and treat the file as if it contains |
350 | * a single page... | |
ef416fc2 | 351 | */ |
352 | ||
80ca4592 | 353 | copy_non_dsc(fp, &doc, ppd, line, len, sizeof(line)); |
ef416fc2 | 354 | } |
ef416fc2 | 355 | |
bd7854cb | 356 | /* |
80ca4592 | 357 | * Send %%EOF as needed... |
bd7854cb | 358 | */ |
359 | ||
80ca4592 | 360 | if (!doc.saw_eof) |
361 | puts("%%EOF"); | |
bd7854cb | 362 | |
ef416fc2 | 363 | /* |
80ca4592 | 364 | * End the job with the appropriate JCL command or CTRL-D... |
ef416fc2 | 365 | */ |
366 | ||
80ca4592 | 367 | if (doc.emit_jcl) |
ef416fc2 | 368 | { |
80ca4592 | 369 | if (ppd && ppd->jcl_end) |
370 | ppdEmitJCLEnd(ppd, stdout); | |
371 | else | |
372 | putchar(0x04); | |
ef416fc2 | 373 | } |
ef416fc2 | 374 | |
375 | /* | |
80ca4592 | 376 | * Close files and remove the temporary file if needed... |
ef416fc2 | 377 | */ |
378 | ||
80ca4592 | 379 | if (doc.temp) |
ef416fc2 | 380 | { |
80ca4592 | 381 | cupsFileClose(doc.temp); |
382 | unlink(doc.tempfile); | |
ef416fc2 | 383 | } |
384 | ||
80ca4592 | 385 | ppdClose(ppd); |
386 | cupsFreeOptions(num_options, options); | |
ef416fc2 | 387 | |
80ca4592 | 388 | cupsFileClose(fp); |
ef416fc2 | 389 | |
80ca4592 | 390 | return (0); |
391 | } | |
ef416fc2 | 392 | |
ef416fc2 | 393 | |
80ca4592 | 394 | /* |
395 | * 'add_page()' - Add a page to the pages array... | |
396 | */ | |
ef416fc2 | 397 | |
80ca4592 | 398 | static pstops_page_t * /* O - New page info object */ |
399 | add_page(pstops_doc_t *doc, /* I - Document information */ | |
400 | const char *label) /* I - Page label */ | |
401 | { | |
402 | pstops_page_t *pageinfo; /* New page info object */ | |
ef416fc2 | 403 | |
ef416fc2 | 404 | |
80ca4592 | 405 | if (!doc->pages) |
406 | doc->pages = cupsArrayNew(NULL, NULL); | |
ef416fc2 | 407 | |
80ca4592 | 408 | if (!doc->pages) |
ef416fc2 | 409 | { |
c0e1af83 | 410 | fprintf(stderr, _("EMERG: Unable to allocate memory for pages array: %s\n"), |
80ca4592 | 411 | strerror(errno)); |
412 | exit(1); | |
ef416fc2 | 413 | } |
80ca4592 | 414 | |
415 | if ((pageinfo = calloc(1, sizeof(pstops_page_t))) == NULL) | |
ef416fc2 | 416 | { |
c0e1af83 | 417 | fprintf(stderr, _("EMERG: Unable to allocate memory for page info: %s\n"), |
80ca4592 | 418 | strerror(errno)); |
419 | exit(1); | |
420 | } | |
ef416fc2 | 421 | |
80ca4592 | 422 | pageinfo->label = strdup(label); |
423 | pageinfo->offset = cupsFileTell(doc->temp); | |
ef416fc2 | 424 | |
80ca4592 | 425 | cupsArrayAdd(doc->pages, pageinfo); |
ef416fc2 | 426 | |
80ca4592 | 427 | doc->page ++; |
ef416fc2 | 428 | |
80ca4592 | 429 | return (pageinfo); |
430 | } | |
ef416fc2 | 431 | |
ef416fc2 | 432 | |
80ca4592 | 433 | /* |
434 | * 'check_range()' - Check to see if the current page is selected for | |
435 | * printing. | |
436 | */ | |
ef416fc2 | 437 | |
80ca4592 | 438 | static int /* O - 1 if selected, 0 otherwise */ |
439 | check_range(pstops_doc_t *doc, /* I - Document information */ | |
440 | int page) /* I - Page number */ | |
441 | { | |
442 | const char *range; /* Pointer into range string */ | |
443 | int lower, upper; /* Lower and upper page numbers */ | |
ef416fc2 | 444 | |
ef416fc2 | 445 | |
80ca4592 | 446 | if (doc->page_set) |
ef416fc2 | 447 | { |
448 | /* | |
80ca4592 | 449 | * See if we only print even or odd pages... |
ef416fc2 | 450 | */ |
451 | ||
e53920b9 | 452 | if (!strcasecmp(doc->page_set, "even") && (page & 1)) |
80ca4592 | 453 | return (0); |
ef416fc2 | 454 | |
e53920b9 | 455 | if (!strcasecmp(doc->page_set, "odd") && !(page & 1)) |
80ca4592 | 456 | return (0); |
457 | } | |
ef416fc2 | 458 | |
80ca4592 | 459 | if (!doc->page_ranges) |
460 | return (1); /* No range, print all pages... */ | |
ef416fc2 | 461 | |
80ca4592 | 462 | for (range = doc->page_ranges; *range != '\0';) |
463 | { | |
464 | if (*range == '-') | |
465 | { | |
466 | lower = 1; | |
467 | range ++; | |
468 | upper = strtol(range, (char **)&range, 10); | |
469 | } | |
470 | else | |
471 | { | |
472 | lower = strtol(range, (char **)&range, 10); | |
ef416fc2 | 473 | |
80ca4592 | 474 | if (*range == '-') |
ef416fc2 | 475 | { |
80ca4592 | 476 | range ++; |
477 | if (!isdigit(*range & 255)) | |
478 | upper = 65535; | |
479 | else | |
480 | upper = strtol(range, (char **)&range, 10); | |
ef416fc2 | 481 | } |
80ca4592 | 482 | else |
483 | upper = lower; | |
484 | } | |
485 | ||
486 | if (page >= lower && page <= upper) | |
487 | return (1); | |
488 | ||
489 | if (*range == ',') | |
490 | range ++; | |
491 | else | |
492 | break; | |
493 | } | |
494 | ||
495 | return (0); | |
496 | } | |
497 | ||
498 | ||
499 | /* | |
500 | * 'copy_bytes()' - Copy bytes from the input file to stdout... | |
501 | */ | |
502 | ||
503 | static void | |
504 | copy_bytes(cups_file_t *fp, /* I - File to read from */ | |
505 | off_t offset, /* I - Offset to page data */ | |
506 | size_t length) /* I - Length of page data */ | |
507 | { | |
508 | char buffer[8192]; /* Data buffer */ | |
509 | ssize_t nbytes; /* Number of bytes read */ | |
510 | size_t nleft; /* Number of bytes left/remaining */ | |
511 | ||
512 | ||
513 | nleft = length; | |
514 | ||
515 | if (cupsFileSeek(fp, offset) < 0) | |
516 | { | |
c0e1af83 | 517 | fprintf(stderr, |
518 | #ifdef HAVE_LONG_LONG | |
519 | _("ERROR: Unable to seek to offset %lld in file - %s\n"), | |
520 | #else | |
521 | _("ERROR: Unable to seek to offset %ld in file - %s\n"), | |
522 | #endif /* HAVE_LONG_LONG */ | |
80ca4592 | 523 | CUPS_LLCAST offset, strerror(errno)); |
524 | return; | |
525 | } | |
526 | ||
527 | while (nleft > 0 || length == 0) | |
528 | { | |
529 | if (nleft > sizeof(buffer) || length == 0) | |
530 | nbytes = sizeof(buffer); | |
531 | else | |
532 | nbytes = nleft; | |
533 | ||
534 | if ((nbytes = cupsFileRead(fp, buffer, nbytes)) < 1) | |
535 | return; | |
536 | ||
537 | nleft -= nbytes; | |
538 | ||
539 | fwrite(buffer, 1, nbytes, stdout); | |
540 | } | |
541 | } | |
542 | ||
543 | ||
544 | /* | |
545 | * 'copy_comments()' - Copy all of the comments section... | |
546 | * | |
547 | * This function expects "line" to be filled with a comment line. | |
548 | * On return, "line" will contain the next line in the file, if any. | |
549 | */ | |
550 | ||
551 | static size_t /* O - Length of next line */ | |
552 | copy_comments(cups_file_t *fp, /* I - File to read from */ | |
553 | pstops_doc_t *doc, /* I - Document info */ | |
2abf387c | 554 | ppd_file_t *ppd, /* I - PPD file */ |
80ca4592 | 555 | char *line, /* I - Line buffer */ |
556 | size_t linelen, /* I - Length of initial line */ | |
557 | size_t linesize) /* I - Size of line buffer */ | |
558 | { | |
559 | int saw_bounding_box, /* Saw %%BoundingBox: comment? */ | |
560 | saw_for, /* Saw %%For: comment? */ | |
561 | saw_pages, /* Saw %%Pages: comment? */ | |
562 | saw_title; /* Saw %%Title: comment? */ | |
563 | ||
564 | ||
565 | /* | |
566 | * Loop until we see %%EndComments or a non-comment line... | |
567 | */ | |
568 | ||
569 | saw_bounding_box = 0; | |
570 | saw_for = 0; | |
571 | saw_pages = 0; | |
572 | saw_title = 0; | |
573 | ||
574 | while (line[0] == '%') | |
575 | { | |
576 | /* | |
577 | * Strip trailing whitespace... | |
578 | */ | |
579 | ||
580 | while (linelen > 0) | |
581 | { | |
582 | linelen --; | |
583 | ||
584 | if (!isspace(line[linelen] & 255)) | |
585 | break; | |
586 | else | |
587 | line[linelen] = '\0'; | |
588 | } | |
589 | ||
590 | /* | |
591 | * Log the header... | |
592 | */ | |
593 | ||
594 | fprintf(stderr, "DEBUG: %s\n", line); | |
595 | ||
596 | /* | |
597 | * Pull the headers out... | |
598 | */ | |
599 | ||
600 | if (!strncmp(line, "%%Pages:", 8)) | |
601 | { | |
2abf387c | 602 | int pages; /* Number of pages */ |
603 | ||
604 | ||
80ca4592 | 605 | if (saw_pages) |
c0e1af83 | 606 | fputs(_("ERROR: Duplicate %%Pages: comment seen!\n"), stderr); |
80ca4592 | 607 | |
608 | saw_pages = 1; | |
2abf387c | 609 | |
610 | if (Duplex && (pages = atoi(line + 8)) > 0 && pages <= doc->number_up) | |
611 | { | |
612 | /* | |
613 | * Since we will only be printing on a single page, disable duplexing. | |
614 | */ | |
615 | ||
616 | Duplex = 0; | |
617 | doc->slow_duplex = 0; | |
618 | ||
619 | if (cupsGetOption("sides", doc->num_options, doc->options)) | |
620 | doc->num_options = cupsAddOption("sides", "one-sided", | |
621 | doc->num_options, &(doc->options)); | |
622 | ||
623 | if (cupsGetOption("Duplex", doc->num_options, doc->options)) | |
624 | doc->num_options = cupsAddOption("Duplex", "None", | |
625 | doc->num_options, &(doc->options)); | |
626 | ||
627 | if (cupsGetOption("EFDuplex", doc->num_options, doc->options)) | |
628 | doc->num_options = cupsAddOption("EFDuplex", "None", | |
629 | doc->num_options, &(doc->options)); | |
630 | ||
631 | if (cupsGetOption("EFDuplexing", doc->num_options, doc->options)) | |
632 | doc->num_options = cupsAddOption("EFDuplexing", "False", | |
633 | doc->num_options, &(doc->options)); | |
634 | ||
635 | if (cupsGetOption("KD03Duplex", doc->num_options, doc->options)) | |
636 | doc->num_options = cupsAddOption("KD03Duplex", "None", | |
637 | doc->num_options, &(doc->options)); | |
638 | ||
639 | if (cupsGetOption("JCLDuplex", doc->num_options, doc->options)) | |
640 | doc->num_options = cupsAddOption("JCLDuplex", "None", | |
641 | doc->num_options, &(doc->options)); | |
642 | ||
643 | ppdMarkOption(ppd, "Duplex", "None"); | |
644 | ppdMarkOption(ppd, "EFDuplex", "None"); | |
645 | ppdMarkOption(ppd, "EFDuplexing", "False"); | |
646 | ppdMarkOption(ppd, "KD03Duplex", "None"); | |
647 | ppdMarkOption(ppd, "JCLDuplex", "None"); | |
648 | } | |
80ca4592 | 649 | } |
650 | else if (!strncmp(line, "%%BoundingBox:", 14)) | |
651 | { | |
652 | if (saw_bounding_box) | |
c0e1af83 | 653 | fputs(_("ERROR: Duplicate %%BoundingBox: comment seen!\n"), stderr); |
d6ae789d | 654 | else if (strstr(line + 14, "(atend)")) |
655 | { | |
656 | /* | |
657 | * Do nothing for now but use the default imageable area... | |
658 | */ | |
659 | } | |
80ca4592 | 660 | else if (sscanf(line + 14, "%d%d%d%d", doc->bounding_box + 0, |
661 | doc->bounding_box + 1, doc->bounding_box + 2, | |
662 | doc->bounding_box + 3) != 4) | |
ef416fc2 | 663 | { |
c0e1af83 | 664 | fputs(_("ERROR: Bad %%BoundingBox: comment seen!\n"), stderr); |
80ca4592 | 665 | |
666 | doc->bounding_box[0] = (int)PageLeft; | |
667 | doc->bounding_box[1] = (int)PageBottom; | |
668 | doc->bounding_box[2] = (int)PageRight; | |
669 | doc->bounding_box[3] = (int)PageTop; | |
ef416fc2 | 670 | } |
80ca4592 | 671 | |
672 | saw_bounding_box = 1; | |
673 | } | |
674 | else if (!strncmp(line, "%%For:", 6)) | |
675 | { | |
676 | saw_for = 1; | |
b86bc4cf | 677 | doc_printf(doc, "%s\n", line); |
80ca4592 | 678 | } |
679 | else if (!strncmp(line, "%%Title:", 8)) | |
680 | { | |
681 | saw_title = 1; | |
b86bc4cf | 682 | doc_printf(doc, "%s\n", line); |
80ca4592 | 683 | } |
684 | else if (!strncmp(line, "%cupsRotation:", 14)) | |
685 | { | |
686 | /* | |
687 | * Reset orientation of document? | |
688 | */ | |
689 | ||
690 | int orient = (atoi(line + 14) / 90) & 3; | |
691 | ||
692 | if (orient != Orientation) | |
ef416fc2 | 693 | { |
694 | /* | |
80ca4592 | 695 | * Yes, update things so that the pages come out right... |
ef416fc2 | 696 | */ |
697 | ||
80ca4592 | 698 | Orientation = (4 - Orientation + orient) & 3; |
699 | UpdatePageVars(); | |
700 | Orientation = orient; | |
ef416fc2 | 701 | } |
80ca4592 | 702 | } |
703 | else if (!strcmp(line, "%%EndComments")) | |
704 | { | |
705 | linelen = cupsFileGetLine(fp, line, linesize); | |
706 | break; | |
707 | } | |
708 | else if (strncmp(line, "%!", 2) && strncmp(line, "%cups", 5)) | |
b86bc4cf | 709 | doc_printf(doc, "%s\n", line); |
80ca4592 | 710 | |
711 | if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0) | |
712 | break; | |
713 | } | |
714 | ||
715 | if (!saw_bounding_box) | |
c0e1af83 | 716 | fputs(_("ERROR: No %%BoundingBox: comment in header!\n"), stderr); |
80ca4592 | 717 | |
718 | if (!saw_pages) | |
c0e1af83 | 719 | fputs(_("ERROR: No %%Pages: comment in header!\n"), stderr); |
80ca4592 | 720 | |
721 | if (!saw_for) | |
2abf387c | 722 | WriteTextComment("For", doc->user); |
80ca4592 | 723 | |
724 | if (!saw_title) | |
2abf387c | 725 | WriteTextComment("Title", doc->title); |
80ca4592 | 726 | |
727 | if (doc->copies != 1 && (!doc->collate || !doc->slow_collate)) | |
728 | { | |
729 | /* | |
730 | * Tell the document processor the copy and duplex options | |
731 | * that are required... | |
732 | */ | |
733 | ||
b86bc4cf | 734 | doc_printf(doc, "%%%%Requirements: numcopies(%d)%s%s\n", doc->copies, |
735 | doc->collate ? " collate" : "", | |
736 | Duplex ? " duplex" : ""); | |
80ca4592 | 737 | |
738 | /* | |
739 | * Apple uses RBI comments for various non-PPD options... | |
740 | */ | |
741 | ||
b86bc4cf | 742 | doc_printf(doc, "%%RBINumCopies: %d\n", doc->copies); |
80ca4592 | 743 | } |
744 | else | |
745 | { | |
746 | /* | |
747 | * Tell the document processor the duplex option that is required... | |
748 | */ | |
749 | ||
750 | if (Duplex) | |
b86bc4cf | 751 | doc_puts(doc, "%%Requirements: duplex\n"); |
80ca4592 | 752 | |
753 | /* | |
754 | * Apple uses RBI comments for various non-PPD options... | |
755 | */ | |
756 | ||
b86bc4cf | 757 | doc_puts(doc, "%RBINumCopies: 1\n"); |
80ca4592 | 758 | } |
759 | ||
b86bc4cf | 760 | doc_puts(doc, "%%Pages: (atend)\n"); |
761 | doc_puts(doc, "%%BoundingBox: (atend)\n"); | |
762 | doc_puts(doc, "%%EndComments\n"); | |
80ca4592 | 763 | |
764 | return (linelen); | |
765 | } | |
766 | ||
767 | ||
768 | /* | |
769 | * 'copy_dsc()' - Copy a DSC-conforming document... | |
770 | * | |
771 | * This function expects "line" to be filled with the %!PS-Adobe comment line. | |
772 | */ | |
773 | ||
774 | static void | |
775 | copy_dsc(cups_file_t *fp, /* I - File to read from */ | |
776 | pstops_doc_t *doc, /* I - Document info */ | |
777 | ppd_file_t *ppd, /* I - PPD file */ | |
778 | char *line, /* I - Line buffer */ | |
779 | size_t linelen, /* I - Length of initial line */ | |
780 | size_t linesize) /* I - Size of line buffer */ | |
781 | { | |
782 | int number; /* Page number */ | |
783 | pstops_page_t *pageinfo; /* Page information */ | |
784 | ||
785 | ||
786 | /* | |
787 | * Make sure we use ESPshowpage for EPS files... | |
788 | */ | |
789 | ||
790 | if (strstr(line, "EPSF")) | |
791 | { | |
792 | doc->use_ESPshowpage = 1; | |
793 | doc->number_up = 1; | |
794 | } | |
795 | ||
796 | /* | |
797 | * Start sending the document with any commands needed... | |
798 | */ | |
799 | ||
800 | fprintf(stderr, "DEBUG: Before copy_comments - %s", line); | |
2abf387c | 801 | linelen = copy_comments(fp, doc, ppd, line, linelen, linesize); |
80ca4592 | 802 | |
803 | /* | |
804 | * Now find the prolog section, if any... | |
805 | */ | |
806 | ||
807 | fprintf(stderr, "DEBUG: Before copy_prolog - %s", line); | |
d6ae789d | 808 | linelen = copy_prolog(fp, doc, ppd, line, linelen, linesize); |
80ca4592 | 809 | |
810 | /* | |
811 | * Then the document setup section... | |
812 | */ | |
813 | ||
814 | fprintf(stderr, "DEBUG: Before copy_setup - %s", line); | |
d6ae789d | 815 | linelen = copy_setup(fp, doc, ppd, line, linelen, linesize); |
816 | ||
817 | /* | |
818 | * Copy until we see %%Page:... | |
819 | */ | |
820 | ||
821 | while (strncmp(line, "%%Page:", 7) && strncmp(line, "%%Trailer", 9)) | |
822 | { | |
b86bc4cf | 823 | doc_write(doc, line, linelen); |
d6ae789d | 824 | |
825 | if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0) | |
826 | break; | |
827 | } | |
80ca4592 | 828 | |
829 | /* | |
830 | * Then process pages until we have no more... | |
831 | */ | |
832 | ||
833 | number = 0; | |
834 | ||
835 | fprintf(stderr, "DEBUG: Before page loop - %s", line); | |
836 | while (!strncmp(line, "%%Page:", 7)) | |
837 | { | |
838 | number ++; | |
839 | ||
840 | if (check_range(doc, (number - 1) / doc->number_up + 1)) | |
841 | { | |
842 | fprintf(stderr, "DEBUG: Copying page %d...\n", number); | |
843 | linelen = copy_page(fp, doc, ppd, number, line, linelen, linesize); | |
844 | } | |
845 | else | |
846 | { | |
847 | fprintf(stderr, "DEBUG: Skipping page %d...\n", number); | |
848 | linelen = skip_page(fp, line, linelen, linesize); | |
849 | } | |
850 | } | |
851 | ||
852 | /* | |
853 | * Finish up the last page(s)... | |
854 | */ | |
855 | ||
e53920b9 | 856 | if (number && is_not_last_page(number) && cupsArrayLast(doc->pages)) |
80ca4592 | 857 | { |
858 | pageinfo = (pstops_page_t *)cupsArrayLast(doc->pages); | |
859 | ||
d6ae789d | 860 | start_nup(doc, doc->number_up, 0, doc->bounding_box); |
80ca4592 | 861 | doc_puts(doc, "showpage\n"); |
d6ae789d | 862 | end_nup(doc, doc->number_up); |
80ca4592 | 863 | |
864 | pageinfo->length = cupsFileTell(doc->temp) - pageinfo->offset; | |
865 | } | |
866 | ||
867 | if (doc->slow_duplex && (doc->page & 1)) | |
868 | { | |
869 | /* | |
870 | * Make sure we have an even number of pages... | |
871 | */ | |
872 | ||
873 | pageinfo = add_page(doc, "(filler)"); | |
874 | ||
875 | if (!doc->slow_order) | |
876 | { | |
877 | if (!ppd || !ppd->num_filters) | |
878 | fprintf(stderr, "PAGE: %d %d\n", doc->page, | |
879 | doc->slow_collate ? 1 : doc->copies); | |
880 | ||
881 | printf("%%%%Page: (filler) %d\n", doc->page); | |
882 | } | |
883 | ||
d6ae789d | 884 | start_nup(doc, doc->number_up, 0, doc->bounding_box); |
80ca4592 | 885 | doc_puts(doc, "showpage\n"); |
d6ae789d | 886 | end_nup(doc, doc->number_up); |
80ca4592 | 887 | |
888 | pageinfo->length = cupsFileTell(doc->temp) - pageinfo->offset; | |
889 | } | |
890 | ||
891 | /* | |
892 | * Make additional copies as necessary... | |
893 | */ | |
894 | ||
895 | number = doc->slow_order ? 0 : doc->page; | |
896 | ||
897 | if (doc->temp) | |
898 | { | |
899 | int copy; /* Current copy */ | |
900 | ||
901 | ||
902 | /* | |
903 | * Reopen the temporary file for reading... | |
904 | */ | |
905 | ||
906 | cupsFileClose(doc->temp); | |
907 | ||
908 | doc->temp = cupsFileOpen(doc->tempfile, "r"); | |
909 | ||
910 | /* | |
911 | * Make the copies... | |
912 | */ | |
913 | ||
914 | for (copy = !doc->slow_order; copy < doc->copies; copy ++) | |
915 | { | |
2abf387c | 916 | /* |
917 | * Send end-of-job stuff followed by any start-of-job stuff required | |
918 | * for the JCL options... | |
919 | */ | |
920 | ||
b86bc4cf | 921 | if (number && doc->emit_jcl && ppd && ppd->jcl_end) |
2abf387c | 922 | { |
b86bc4cf | 923 | /* |
924 | * Send the trailer... | |
925 | */ | |
926 | ||
927 | puts("%%Trailer"); | |
928 | printf("%%%%Pages: %d\n", cupsArrayCount(doc->pages)); | |
929 | if (doc->number_up > 1 || doc->fitplot) | |
930 | printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", | |
931 | PageLeft, PageBottom, PageRight, PageTop); | |
2abf387c | 932 | else |
b86bc4cf | 933 | printf("%%%%BoundingBox: %d %d %d %d\n", |
934 | doc->new_bounding_box[0], doc->new_bounding_box[1], | |
935 | doc->new_bounding_box[2], doc->new_bounding_box[3]); | |
936 | puts("%%EOF"); | |
2abf387c | 937 | |
b86bc4cf | 938 | /* |
939 | * Start a new document... | |
940 | */ | |
941 | ||
942 | ppdEmitJCLEnd(ppd, stdout); | |
2abf387c | 943 | ppdEmitJCL(ppd, stdout, doc->job_id, doc->user, doc->title); |
2abf387c | 944 | |
b86bc4cf | 945 | puts("%!PS-Adobe-3.0"); |
b86bc4cf | 946 | |
947 | number = 0; | |
948 | } | |
2abf387c | 949 | |
f7deaa1a | 950 | /* |
951 | * Copy the prolog as needed... | |
952 | */ | |
953 | ||
954 | if (!number) | |
955 | { | |
956 | pageinfo = (pstops_page_t *)cupsArrayFirst(doc->pages); | |
957 | copy_bytes(doc->temp, 0, pageinfo->offset); | |
958 | } | |
959 | ||
2abf387c | 960 | /* |
961 | * Then copy all of the pages... | |
962 | */ | |
963 | ||
80ca4592 | 964 | pageinfo = doc->slow_order ? (pstops_page_t *)cupsArrayLast(doc->pages) : |
965 | (pstops_page_t *)cupsArrayFirst(doc->pages); | |
966 | ||
967 | while (pageinfo) | |
ef416fc2 | 968 | { |
80ca4592 | 969 | number ++; |
ef416fc2 | 970 | |
80ca4592 | 971 | if (!ppd || !ppd->num_filters) |
972 | fprintf(stderr, "PAGE: %d 1\n", number); | |
ef416fc2 | 973 | |
80ca4592 | 974 | if (doc->number_up > 1) |
975 | { | |
976 | printf("%%%%Page: (%d) %d\n", number, number); | |
977 | printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n", | |
978 | PageLeft, PageBottom, PageRight, PageTop); | |
979 | } | |
980 | else | |
ef416fc2 | 981 | { |
411affcf | 982 | printf("%%%%Page: %s %d\n", pageinfo->label, number); |
80ca4592 | 983 | printf("%%%%PageBoundingBox: %d %d %d %d\n", |
984 | pageinfo->bounding_box[0], pageinfo->bounding_box[1], | |
985 | pageinfo->bounding_box[2], pageinfo->bounding_box[3]); | |
ef416fc2 | 986 | } |
80ca4592 | 987 | |
988 | copy_bytes(doc->temp, pageinfo->offset, pageinfo->length); | |
989 | ||
990 | pageinfo = doc->slow_order ? (pstops_page_t *)cupsArrayPrev(doc->pages) : | |
991 | (pstops_page_t *)cupsArrayNext(doc->pages); | |
ef416fc2 | 992 | } |
80ca4592 | 993 | } |
994 | } | |
995 | ||
f301802f | 996 | /* |
997 | * Restore the old showpage operator as needed... | |
998 | */ | |
999 | ||
1000 | if (doc->use_ESPshowpage) | |
1001 | puts("userdict/showpage/ESPshowpage load put\n"); | |
1002 | ||
80ca4592 | 1003 | /* |
1004 | * Write/copy the trailer... | |
1005 | */ | |
1006 | ||
1007 | linelen = copy_trailer(fp, doc, ppd, number, line, linelen, linesize); | |
1008 | } | |
1009 | ||
1010 | ||
1011 | /* | |
1012 | * 'copy_non_dsc()' - Copy a document that does not conform to the DSC... | |
1013 | * | |
1014 | * This function expects "line" to be filled with the %! comment line. | |
1015 | */ | |
1016 | ||
1017 | static void | |
1018 | copy_non_dsc(cups_file_t *fp, /* I - File to read from */ | |
1019 | pstops_doc_t *doc, /* I - Document info */ | |
1020 | ppd_file_t *ppd, /* I - PPD file */ | |
1021 | char *line, /* I - Line buffer */ | |
1022 | size_t linelen, /* I - Length of initial line */ | |
1023 | size_t linesize) /* I - Size of line buffer */ | |
1024 | { | |
1025 | int copy; /* Current copy */ | |
1026 | char buffer[8192]; /* Copy buffer */ | |
1027 | int bytes; /* Number of bytes copied */ | |
1028 | ||
1029 | ||
1030 | /* | |
1031 | * First let the user know that they are attempting to print a file | |
1032 | * that may not print correctly... | |
1033 | */ | |
1034 | ||
c0e1af83 | 1035 | fputs(_("WARNING: This document does not conform to the Adobe Document " |
1036 | "Structuring Conventions and may not print correctly!\n"), stderr); | |
80ca4592 | 1037 | |
1038 | /* | |
1039 | * Then write a standard DSC comment section... | |
1040 | */ | |
1041 | ||
1042 | printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", PageLeft, PageBottom, | |
1043 | PageRight, PageTop); | |
1044 | ||
1045 | if (doc->slow_collate && doc->copies > 1) | |
1046 | printf("%%%%Pages: %d\n", doc->copies); | |
1047 | else | |
1048 | puts("%%Pages: 1"); | |
1049 | ||
2abf387c | 1050 | WriteTextComment("For", doc->user); |
1051 | WriteTextComment("Title", doc->title); | |
80ca4592 | 1052 | |
1053 | if (doc->copies != 1 && (!doc->collate || !doc->slow_collate)) | |
1054 | { | |
1055 | /* | |
1056 | * Tell the document processor the copy and duplex options | |
1057 | * that are required... | |
1058 | */ | |
1059 | ||
1060 | printf("%%%%Requirements: numcopies(%d)%s%s\n", doc->copies, | |
1061 | doc->collate ? " collate" : "", | |
1062 | Duplex ? " duplex" : ""); | |
1063 | ||
1064 | /* | |
1065 | * Apple uses RBI comments for various non-PPD options... | |
1066 | */ | |
1067 | ||
1068 | printf("%%RBINumCopies: %d\n", doc->copies); | |
1069 | } | |
1070 | else | |
1071 | { | |
1072 | /* | |
1073 | * Tell the document processor the duplex option that is required... | |
1074 | */ | |
1075 | ||
1076 | if (Duplex) | |
1077 | puts("%%Requirements: duplex"); | |
1078 | ||
1079 | /* | |
1080 | * Apple uses RBI comments for various non-PPD options... | |
1081 | */ | |
1082 | ||
1083 | puts("%RBINumCopies: 1"); | |
1084 | } | |
1085 | ||
1086 | puts("%%EndComments"); | |
1087 | ||
1088 | /* | |
1089 | * Then the prolog... | |
1090 | */ | |
1091 | ||
1092 | puts("%%BeginProlog"); | |
1093 | ||
1094 | do_prolog(doc, ppd); | |
1095 | ||
1096 | puts("%%EndProlog"); | |
1097 | ||
1098 | /* | |
1099 | * Then the setup section... | |
1100 | */ | |
1101 | ||
1102 | puts("%%BeginSetup"); | |
1103 | ||
1104 | do_setup(doc, ppd); | |
1105 | ||
1106 | puts("%%EndSetup"); | |
1107 | ||
1108 | /* | |
1109 | * Finally, embed a copy of the file inside a %%Page... | |
1110 | */ | |
1111 | ||
1112 | if (!ppd || !ppd->num_filters) | |
1113 | fprintf(stderr, "PAGE: 1 %d\n", doc->temp ? 1 : doc->copies); | |
1114 | ||
1115 | puts("%%Page: 1 1"); | |
1116 | puts("%%BeginPageSetup"); | |
1117 | ppdEmit(ppd, stdout, PPD_ORDER_PAGE); | |
1118 | puts("%%EndPageSetup"); | |
1119 | puts("%%BeginDocument: nondsc"); | |
1120 | ||
1121 | fwrite(line, linelen, 1, stdout); | |
1122 | ||
1123 | if (doc->temp) | |
1124 | cupsFileWrite(doc->temp, line, linelen); | |
1125 | ||
1126 | while ((bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0) | |
1127 | { | |
1128 | fwrite(buffer, 1, bytes, stdout); | |
1129 | ||
1130 | if (doc->temp) | |
1131 | cupsFileWrite(doc->temp, buffer, bytes); | |
1132 | } | |
1133 | ||
1134 | puts("%%EndDocument"); | |
1135 | ||
1136 | if (doc->use_ESPshowpage) | |
1137 | { | |
1138 | WriteLabels(Orientation); | |
1139 | puts("ESPshowpage"); | |
1140 | } | |
1141 | ||
1142 | if (doc->temp) | |
1143 | { | |
1144 | /* | |
1145 | * Reopen the temporary file for reading... | |
1146 | */ | |
1147 | ||
1148 | cupsFileClose(doc->temp); | |
1149 | ||
1150 | doc->temp = cupsFileOpen(doc->tempfile, "r"); | |
1151 | ||
1152 | /* | |
1153 | * Make the additional copies as needed... | |
1154 | */ | |
1155 | ||
1156 | for (copy = 1; copy < doc->copies; copy ++) | |
1157 | { | |
1158 | if (!ppd || !ppd->num_filters) | |
1159 | fputs("PAGE: 1 1\n", stderr); | |
1160 | ||
1161 | printf("%%%%Page: %d %d\n", copy + 1, copy + 1); | |
1162 | puts("%%BeginPageSetup"); | |
1163 | ppdEmit(ppd, stdout, PPD_ORDER_PAGE); | |
1164 | puts("%%EndPageSetup"); | |
1165 | puts("%%BeginDocument: nondsc"); | |
1166 | ||
1167 | copy_bytes(doc->temp, 0, 0); | |
1168 | ||
1169 | puts("%%EndDocument"); | |
1170 | ||
1171 | if (doc->use_ESPshowpage) | |
1172 | { | |
1173 | WriteLabels(Orientation); | |
1174 | puts("ESPshowpage"); | |
1175 | } | |
1176 | } | |
1177 | } | |
f301802f | 1178 | |
1179 | /* | |
1180 | * Restore the old showpage operator as needed... | |
1181 | */ | |
1182 | ||
1183 | if (doc->use_ESPshowpage) | |
1184 | puts("userdict/showpage/ESPshowpage load put\n"); | |
80ca4592 | 1185 | } |
1186 | ||
1187 | ||
1188 | /* | |
1189 | * 'copy_page()' - Copy a page description... | |
1190 | * | |
1191 | * This function expects "line" to be filled with a %%Page comment line. | |
1192 | * On return, "line" will contain the next line in the file, if any. | |
1193 | */ | |
1194 | ||
1195 | static size_t /* O - Length of next line */ | |
1196 | copy_page(cups_file_t *fp, /* I - File to read from */ | |
1197 | pstops_doc_t *doc, /* I - Document info */ | |
1198 | ppd_file_t *ppd, /* I - PPD file */ | |
1199 | int number, /* I - Current page number */ | |
1200 | char *line, /* I - Line buffer */ | |
1201 | size_t linelen, /* I - Length of initial line */ | |
1202 | size_t linesize) /* I - Size of line buffer */ | |
1203 | { | |
1204 | char label[256], /* Page label string */ | |
1205 | *ptr; /* Pointer into line */ | |
1206 | int level; /* Embedded document level */ | |
1207 | pstops_page_t *pageinfo; /* Page information */ | |
1208 | int first_page; /* First page on N-up output? */ | |
1209 | int bounding_box[4]; /* PageBoundingBox */ | |
1210 | ||
1211 | ||
1212 | /* | |
1213 | * Get the page label for this page... | |
1214 | */ | |
1215 | ||
1216 | first_page = is_first_page(number); | |
1217 | ||
1218 | if (!parse_text(line + 7, &ptr, label, sizeof(label))) | |
1219 | { | |
c0e1af83 | 1220 | fputs(_("ERROR: Bad %%Page: comment in file!\n"), stderr); |
80ca4592 | 1221 | label[0] = '\0'; |
1222 | number = doc->page; | |
1223 | } | |
1224 | else if (strtol(ptr, &ptr, 10) == LONG_MAX || !isspace(*ptr & 255)) | |
1225 | { | |
c0e1af83 | 1226 | fputs(_("ERROR: Bad %%Page: comment in file!\n"), stderr); |
80ca4592 | 1227 | number = doc->page; |
1228 | } | |
1229 | ||
1230 | /* | |
1231 | * Create or update the current output page... | |
1232 | */ | |
1233 | ||
1234 | if (first_page) | |
1235 | pageinfo = add_page(doc, label); | |
1236 | else | |
1237 | pageinfo = (pstops_page_t *)cupsArrayLast(doc->pages); | |
1238 | ||
1239 | /* | |
1240 | * Handle first page override... | |
1241 | */ | |
1242 | ||
1243 | if (doc->ap_input_slot || doc->ap_manual_feed) | |
1244 | { | |
1245 | if (doc->page == 1) | |
1246 | { | |
1247 | /* | |
1248 | * First page/sheet gets AP_FIRSTPAGE_* options... | |
1249 | */ | |
1250 | ||
1251 | pageinfo->num_options = cupsAddOption("InputSlot", doc->ap_input_slot, | |
1252 | pageinfo->num_options, | |
1253 | &(pageinfo->options)); | |
c0e1af83 | 1254 | pageinfo->num_options = cupsAddOption("ManualFeed", |
1255 | doc->ap_input_slot ? "False" : | |
1256 | doc->ap_manual_feed, | |
80ca4592 | 1257 | pageinfo->num_options, |
1258 | &(pageinfo->options)); | |
1259 | } | |
1260 | else if (doc->page == (Duplex + 2)) | |
1261 | { | |
1262 | /* | |
1263 | * Second page/sheet gets default options... | |
1264 | */ | |
1265 | ||
1266 | pageinfo->num_options = cupsAddOption("InputSlot", doc->input_slot, | |
1267 | pageinfo->num_options, | |
1268 | &(pageinfo->options)); | |
c0e1af83 | 1269 | pageinfo->num_options = cupsAddOption("ManualFeed", |
1270 | doc->input_slot ? "False" : | |
1271 | doc->manual_feed, | |
80ca4592 | 1272 | pageinfo->num_options, |
1273 | &(pageinfo->options)); | |
1274 | } | |
1275 | } | |
1276 | ||
1277 | /* | |
1278 | * Scan comments until we see something other than %%Page*: or | |
1279 | * %%Include*... | |
1280 | */ | |
1281 | ||
1282 | memcpy(bounding_box, doc->bounding_box, sizeof(bounding_box)); | |
1283 | ||
1284 | while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0) | |
1285 | { | |
1286 | if (!strncmp(line, "%%PageBoundingBox:", 18)) | |
1287 | { | |
1288 | /* | |
1289 | * %%PageBoundingBox: llx lly urx ury | |
1290 | */ | |
1291 | ||
1292 | if (sscanf(line + 18, "%d%d%d%d", bounding_box + 0, | |
1293 | bounding_box + 1, bounding_box + 2, | |
1294 | bounding_box + 3) != 4) | |
1295 | { | |
c0e1af83 | 1296 | fputs(_("ERROR: Bad %%PageBoundingBox: comment in file!\n"), stderr); |
80ca4592 | 1297 | memcpy(bounding_box, doc->bounding_box, |
1298 | sizeof(bounding_box)); | |
1299 | } | |
b86bc4cf | 1300 | else if (doc->number_up == 1 && !doc->fitplot && Orientation) |
411affcf | 1301 | { |
1302 | int temp_bbox[4]; /* Temporary bounding box */ | |
1303 | ||
1304 | ||
b86bc4cf | 1305 | memcpy(temp_bbox, bounding_box, sizeof(temp_bbox)); |
1306 | ||
1307 | fprintf(stderr, "DEBUG: Orientation = %d\n", Orientation); | |
1308 | fprintf(stderr, "DEBUG: original bounding_box = [ %d %d %d %d ]\n", | |
1309 | bounding_box[0], bounding_box[1], | |
1310 | bounding_box[2], bounding_box[3]); | |
1311 | fprintf(stderr, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n", | |
1312 | PageWidth, PageLength); | |
1313 | ||
411affcf | 1314 | switch (Orientation) |
1315 | { | |
411affcf | 1316 | case 1 : /* Landscape */ |
b86bc4cf | 1317 | bounding_box[0] = PageLength - temp_bbox[3]; |
1318 | bounding_box[1] = temp_bbox[0]; | |
1319 | bounding_box[2] = PageLength - temp_bbox[1]; | |
1320 | bounding_box[3] = temp_bbox[2]; | |
411affcf | 1321 | break; |
1322 | ||
1323 | case 2 : /* Reverse Portrait */ | |
b86bc4cf | 1324 | bounding_box[0] = PageWidth - temp_bbox[2]; |
1325 | bounding_box[1] = PageLength - temp_bbox[3]; | |
1326 | bounding_box[2] = PageWidth - temp_bbox[0]; | |
1327 | bounding_box[3] = PageLength - temp_bbox[1]; | |
411affcf | 1328 | break; |
1329 | ||
1330 | case 3 : /* Reverse Landscape */ | |
b86bc4cf | 1331 | bounding_box[0] = temp_bbox[1]; |
1332 | bounding_box[1] = PageWidth - temp_bbox[2]; | |
1333 | bounding_box[2] = temp_bbox[3]; | |
1334 | bounding_box[3] = PageWidth - temp_bbox[0]; | |
411affcf | 1335 | break; |
1336 | } | |
b86bc4cf | 1337 | |
1338 | fprintf(stderr, "DEBUG: updated bounding_box = [ %d %d %d %d ]\n", | |
1339 | bounding_box[0], bounding_box[1], | |
1340 | bounding_box[2], bounding_box[3]); | |
411affcf | 1341 | } |
80ca4592 | 1342 | } |
1343 | #if 0 | |
1344 | else if (!strncmp(line, "%%PageCustomColors:", 19) || | |
1345 | !strncmp(line, "%%PageMedia:", 12) || | |
1346 | !strncmp(line, "%%PageOrientation:", 18) || | |
1347 | !strncmp(line, "%%PageProcessColors:", 20) || | |
1348 | !strncmp(line, "%%PageRequirements:", 18) || | |
1349 | !strncmp(line, "%%PageResources:", 16)) | |
1350 | { | |
1351 | /* | |
1352 | * Copy literal... | |
1353 | */ | |
1354 | } | |
1355 | #endif /* 0 */ | |
1356 | else if (!strncmp(line, "%%PageCustomColors:", 19)) | |
1357 | { | |
1358 | /* | |
1359 | * %%PageCustomColors: ... | |
1360 | */ | |
1361 | } | |
1362 | else if (!strncmp(line, "%%PageMedia:", 12)) | |
1363 | { | |
1364 | /* | |
1365 | * %%PageMedia: ... | |
1366 | */ | |
1367 | } | |
1368 | else if (!strncmp(line, "%%PageOrientation:", 18)) | |
1369 | { | |
1370 | /* | |
1371 | * %%PageOrientation: ... | |
1372 | */ | |
1373 | } | |
1374 | else if (!strncmp(line, "%%PageProcessColors:", 20)) | |
1375 | { | |
1376 | /* | |
1377 | * %%PageProcessColors: ... | |
1378 | */ | |
1379 | } | |
1380 | else if (!strncmp(line, "%%PageRequirements:", 18)) | |
1381 | { | |
1382 | /* | |
1383 | * %%PageRequirements: ... | |
1384 | */ | |
1385 | } | |
1386 | else if (!strncmp(line, "%%PageResources:", 16)) | |
1387 | { | |
1388 | /* | |
1389 | * %%PageResources: ... | |
1390 | */ | |
1391 | } | |
1392 | else if (!strncmp(line, "%%IncludeFeature:", 17)) | |
1393 | { | |
1394 | /* | |
1395 | * %%IncludeFeature: *MainKeyword OptionKeyword | |
1396 | */ | |
1397 | ||
1398 | if (doc->number_up == 1 &&!doc->fitplot) | |
1399 | pageinfo->num_options = include_feature(ppd, line, | |
1400 | pageinfo->num_options, | |
1401 | &(pageinfo->options)); | |
1402 | } | |
1403 | else if (strncmp(line, "%%Include", 9)) | |
1404 | break; | |
1405 | } | |
1406 | ||
1407 | if (doc->number_up == 1) | |
1408 | { | |
1409 | /* | |
1410 | * Update the document's composite and page bounding box... | |
1411 | */ | |
1412 | ||
1413 | memcpy(pageinfo->bounding_box, bounding_box, | |
1414 | sizeof(pageinfo->bounding_box)); | |
1415 | ||
1416 | if (bounding_box[0] < doc->new_bounding_box[0]) | |
1417 | doc->new_bounding_box[0] = bounding_box[0]; | |
1418 | if (bounding_box[1] < doc->new_bounding_box[1]) | |
1419 | doc->new_bounding_box[1] = bounding_box[1]; | |
1420 | if (bounding_box[2] > doc->new_bounding_box[2]) | |
1421 | doc->new_bounding_box[2] = bounding_box[2]; | |
1422 | if (bounding_box[3] > doc->new_bounding_box[3]) | |
1423 | doc->new_bounding_box[3] = bounding_box[3]; | |
1424 | } | |
1425 | ||
1426 | /* | |
1427 | * Output the page header as needed... | |
1428 | */ | |
1429 | ||
1430 | if (!doc->slow_order && first_page) | |
1431 | { | |
1432 | if (!ppd || !ppd->num_filters) | |
1433 | fprintf(stderr, "PAGE: %d %d\n", doc->page, | |
1434 | doc->slow_collate ? 1 : doc->copies); | |
1435 | ||
1436 | if (doc->number_up > 1) | |
1437 | { | |
1438 | printf("%%%%Page: (%d) %d\n", doc->page, doc->page); | |
1439 | printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n", | |
1440 | PageLeft, PageBottom, PageRight, PageTop); | |
1441 | } | |
1442 | else | |
1443 | { | |
1444 | printf("%%%%Page: %s %d\n", pageinfo->label, doc->page); | |
1445 | printf("%%%%PageBoundingBox: %d %d %d %d\n", | |
1446 | pageinfo->bounding_box[0], pageinfo->bounding_box[1], | |
1447 | pageinfo->bounding_box[2], pageinfo->bounding_box[3]); | |
1448 | } | |
1449 | } | |
1450 | ||
1451 | /* | |
1452 | * Copy any page setup commands... | |
1453 | */ | |
1454 | ||
e53920b9 | 1455 | if (first_page) |
80ca4592 | 1456 | { |
ed486911 | 1457 | char *page_setup; /* PageSetup commands to send */ |
1458 | ||
1459 | ||
e53920b9 | 1460 | doc_puts(doc, "%%BeginPageSetup\n"); |
80ca4592 | 1461 | |
e53920b9 | 1462 | if (pageinfo->num_options > 0) |
80ca4592 | 1463 | { |
e53920b9 | 1464 | int i; /* Looping var */ |
1465 | ppd_option_t *option; /* PPD option */ | |
1466 | int min_order; /* Minimum OrderDependency value */ | |
1467 | char *doc_setup, /* DocumentSetup commands to send */ | |
1468 | *any_setup; /* AnySetup commands to send */ | |
80ca4592 | 1469 | |
80ca4592 | 1470 | |
e53920b9 | 1471 | /* |
1472 | * Yes, figure out the minimum OrderDependency value... | |
1473 | */ | |
80ca4592 | 1474 | |
e53920b9 | 1475 | if ((option = ppdFindOption(ppd, "PageRegion")) != NULL) |
1476 | min_order = option->order; | |
1477 | else | |
1478 | min_order = 999.0f; | |
80ca4592 | 1479 | |
e53920b9 | 1480 | for (i = 0; i < pageinfo->num_options; i ++) |
1481 | if ((option = ppdFindOption(ppd, pageinfo->options[i].name)) != NULL && | |
1482 | option->order < min_order) | |
1483 | min_order = option->order; | |
80ca4592 | 1484 | |
e53920b9 | 1485 | /* |
1486 | * Mark and extract them... | |
1487 | */ | |
80ca4592 | 1488 | |
e53920b9 | 1489 | cupsMarkOptions(ppd, pageinfo->num_options, pageinfo->options); |
80ca4592 | 1490 | |
e53920b9 | 1491 | doc_setup = ppdEmitString(ppd, PPD_ORDER_DOCUMENT, min_order); |
1492 | any_setup = ppdEmitString(ppd, PPD_ORDER_ANY, min_order); | |
80ca4592 | 1493 | |
e53920b9 | 1494 | /* |
1495 | * Then send them out... | |
1496 | */ | |
80ca4592 | 1497 | |
e53920b9 | 1498 | if (doc_setup) |
ed486911 | 1499 | { |
e53920b9 | 1500 | doc_puts(doc, doc_setup); |
ed486911 | 1501 | free(doc_setup); |
1502 | } | |
80ca4592 | 1503 | |
e53920b9 | 1504 | if (any_setup) |
ed486911 | 1505 | { |
e53920b9 | 1506 | doc_puts(doc, any_setup); |
ed486911 | 1507 | free(any_setup); |
1508 | } | |
1509 | } | |
80ca4592 | 1510 | |
ed486911 | 1511 | /* |
1512 | * Output commands for the current page... | |
1513 | */ | |
80ca4592 | 1514 | |
ed486911 | 1515 | page_setup = ppdEmitString(ppd, PPD_ORDER_PAGE, 0); |
80ca4592 | 1516 | |
ed486911 | 1517 | if (page_setup) |
1518 | { | |
1519 | doc_puts(doc, page_setup); | |
1520 | free(page_setup); | |
e53920b9 | 1521 | } |
1522 | } | |
80ca4592 | 1523 | |
e53920b9 | 1524 | /* |
1525 | * Prep for the start of the page description... | |
1526 | */ | |
80ca4592 | 1527 | |
e53920b9 | 1528 | start_nup(doc, number, 1, bounding_box); |
80ca4592 | 1529 | |
e53920b9 | 1530 | /* |
1531 | * Copy page setup commands as needed... | |
1532 | */ | |
80ca4592 | 1533 | |
e53920b9 | 1534 | if (!strncmp(line, "%%BeginPageSetup", 16)) |
1535 | { | |
1536 | int feature = 0; /* In a Begin/EndFeature block? */ | |
80ca4592 | 1537 | |
80ca4592 | 1538 | |
e53920b9 | 1539 | while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0) |
1540 | { | |
1541 | if (!strncmp(line, "%%EndPageSetup", 14)) | |
1542 | break; | |
1543 | else if (!strncmp(line, "%%BeginFeature:", 15)) | |
1544 | { | |
1545 | feature = 1; | |
1546 | ||
1547 | if (doc->number_up > 1 || doc->fitplot) | |
1548 | continue; | |
1549 | } | |
f42414bf | 1550 | else if (!strncmp(line, "%%EndFeature", 12)) |
e53920b9 | 1551 | { |
1552 | feature = 0; | |
1553 | ||
1554 | if (doc->number_up > 1 || doc->fitplot) | |
1555 | continue; | |
1556 | } | |
1557 | else if (!strncmp(line, "%%Include", 9)) | |
1558 | continue; | |
1559 | ||
1560 | if (!feature || (doc->number_up == 1 && !doc->fitplot)) | |
1561 | doc_write(doc, line, linelen); | |
1562 | } | |
1563 | ||
1564 | /* | |
1565 | * Skip %%EndPageSetup... | |
1566 | */ | |
1567 | ||
1568 | if (linelen > 0) | |
1569 | linelen = cupsFileGetLine(fp, line, linesize); | |
80ca4592 | 1570 | } |
1571 | ||
1572 | /* | |
e53920b9 | 1573 | * Finish the PageSetup section as needed... |
80ca4592 | 1574 | */ |
1575 | ||
e53920b9 | 1576 | if (first_page) |
1577 | doc_puts(doc, "%%EndPageSetup\n"); | |
80ca4592 | 1578 | |
1579 | /* | |
1580 | * Read the rest of the page description... | |
1581 | */ | |
1582 | ||
1583 | level = 0; | |
1584 | ||
1585 | do | |
1586 | { | |
1587 | if (level == 0 && | |
1588 | (!strncmp(line, "%%Page:", 7) || | |
d6ae789d | 1589 | !strncmp(line, "%%Trailer", 9) || |
80ca4592 | 1590 | !strncmp(line, "%%EOF", 5))) |
1591 | break; | |
1592 | else if (!strncmp(line, "%%BeginDocument", 15) || | |
1593 | !strncmp(line, "%ADO_BeginApplication", 21)) | |
1594 | { | |
1595 | doc_write(doc, line, linelen); | |
1596 | ||
1597 | level ++; | |
1598 | } | |
1599 | else if ((!strncmp(line, "%%EndDocument", 13) || | |
1600 | !strncmp(line, "%ADO_EndApplication", 19)) && level > 0) | |
1601 | { | |
1602 | doc_write(doc, line, linelen); | |
1603 | ||
1604 | level --; | |
1605 | } | |
1606 | else if (!strncmp(line, "%%BeginBinary:", 14) || | |
1607 | (!strncmp(line, "%%BeginData:", 12) && | |
1608 | !strstr(line, "ASCII") && !strstr(line, "Hex"))) | |
1609 | { | |
1610 | /* | |
1611 | * Copy binary data... | |
1612 | */ | |
ef416fc2 | 1613 | |
80ca4592 | 1614 | int bytes; /* Bytes of data */ |
ef416fc2 | 1615 | |
ef416fc2 | 1616 | |
80ca4592 | 1617 | doc_write(doc, line, linelen); |
ef416fc2 | 1618 | |
80ca4592 | 1619 | bytes = atoi(strchr(line, ':') + 1); |
1620 | ||
1621 | while (bytes > 0) | |
1622 | { | |
1623 | if (bytes > linesize) | |
1624 | linelen = cupsFileRead(fp, line, linesize); | |
1625 | else | |
1626 | linelen = cupsFileRead(fp, line, bytes); | |
ef416fc2 | 1627 | |
80ca4592 | 1628 | if (linelen < 1) |
ef416fc2 | 1629 | { |
80ca4592 | 1630 | line[0] = '\0'; |
1631 | perror("ERROR: Early end-of-file while reading binary data"); | |
1632 | return (0); | |
1633 | } | |
ef416fc2 | 1634 | |
80ca4592 | 1635 | doc_write(doc, line, linelen); |
ef416fc2 | 1636 | |
80ca4592 | 1637 | bytes -= linelen; |
ef416fc2 | 1638 | } |
ef416fc2 | 1639 | } |
80ca4592 | 1640 | else |
1641 | doc_write(doc, line, linelen); | |
1642 | } | |
1643 | while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0); | |
ef416fc2 | 1644 | |
80ca4592 | 1645 | /* |
1646 | * Finish up this page and return... | |
1647 | */ | |
ef416fc2 | 1648 | |
80ca4592 | 1649 | end_nup(doc, number); |
ef416fc2 | 1650 | |
80ca4592 | 1651 | pageinfo->length = cupsFileTell(doc->temp) - pageinfo->offset; |
ef416fc2 | 1652 | |
80ca4592 | 1653 | return (linelen); |
1654 | } | |
ef416fc2 | 1655 | |
ef416fc2 | 1656 | |
80ca4592 | 1657 | /* |
1658 | * 'copy_prolog()' - Copy the document prolog section... | |
1659 | * | |
1660 | * This function expects "line" to be filled with a %%BeginProlog comment line. | |
1661 | * On return, "line" will contain the next line in the file, if any. | |
1662 | */ | |
ef416fc2 | 1663 | |
80ca4592 | 1664 | static size_t /* O - Length of next line */ |
1665 | copy_prolog(cups_file_t *fp, /* I - File to read from */ | |
1666 | pstops_doc_t *doc, /* I - Document info */ | |
1667 | ppd_file_t *ppd, /* I - PPD file */ | |
1668 | char *line, /* I - Line buffer */ | |
1669 | size_t linelen, /* I - Length of initial line */ | |
1670 | size_t linesize) /* I - Size of line buffer */ | |
1671 | { | |
1672 | while (strncmp(line, "%%BeginProlog", 13)) | |
1673 | { | |
1674 | if (!strncmp(line, "%%BeginSetup", 12) || !strncmp(line, "%%Page:", 7)) | |
1675 | break; | |
ef416fc2 | 1676 | |
b86bc4cf | 1677 | doc_write(doc, line, linelen); |
ef416fc2 | 1678 | |
80ca4592 | 1679 | if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0) |
1680 | break; | |
1681 | } | |
ef416fc2 | 1682 | |
b86bc4cf | 1683 | doc_puts(doc, "%%BeginProlog\n"); |
ef416fc2 | 1684 | |
80ca4592 | 1685 | do_prolog(doc, ppd); |
ef416fc2 | 1686 | |
80ca4592 | 1687 | if (!strncmp(line, "%%BeginProlog", 13)) |
1688 | { | |
1689 | while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0) | |
ef416fc2 | 1690 | { |
80ca4592 | 1691 | if (!strncmp(line, "%%EndProlog", 11) || |
1692 | !strncmp(line, "%%BeginSetup", 12) || | |
1693 | !strncmp(line, "%%Page:", 7)) | |
1694 | break; | |
ef416fc2 | 1695 | |
b86bc4cf | 1696 | doc_write(doc, line, linelen); |
ef416fc2 | 1697 | } |
80ca4592 | 1698 | |
1699 | if (!strncmp(line, "%%EndProlog", 11)) | |
1700 | linelen = cupsFileGetLine(fp, line, linesize); | |
ef416fc2 | 1701 | else |
c0e1af83 | 1702 | fputs(_("ERROR: Missing %%EndProlog!\n"), stderr); |
80ca4592 | 1703 | } |
ef416fc2 | 1704 | |
b86bc4cf | 1705 | doc_puts(doc, "%%EndProlog\n"); |
ef416fc2 | 1706 | |
80ca4592 | 1707 | return (linelen); |
1708 | } | |
ef416fc2 | 1709 | |
bd7854cb | 1710 | |
80ca4592 | 1711 | /* |
1712 | * 'copy_setup()' - Copy the document setup section... | |
1713 | * | |
1714 | * This function expects "line" to be filled with a %%BeginSetup comment line. | |
1715 | * On return, "line" will contain the next line in the file, if any. | |
1716 | */ | |
bd7854cb | 1717 | |
80ca4592 | 1718 | static size_t /* O - Length of next line */ |
1719 | copy_setup(cups_file_t *fp, /* I - File to read from */ | |
1720 | pstops_doc_t *doc, /* I - Document info */ | |
1721 | ppd_file_t *ppd, /* I - PPD file */ | |
1722 | char *line, /* I - Line buffer */ | |
1723 | size_t linelen, /* I - Length of initial line */ | |
1724 | size_t linesize) /* I - Size of line buffer */ | |
1725 | { | |
1726 | while (strncmp(line, "%%BeginSetup", 12)) | |
1727 | { | |
1728 | if (!strncmp(line, "%%Page:", 7)) | |
1729 | break; | |
bd7854cb | 1730 | |
b86bc4cf | 1731 | doc_write(doc, line, linelen); |
bd7854cb | 1732 | |
80ca4592 | 1733 | if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0) |
1734 | break; | |
1735 | } | |
1736 | ||
b86bc4cf | 1737 | doc_puts(doc, "%%BeginSetup\n"); |
d09495fa | 1738 | |
1739 | do_setup(doc, ppd); | |
1740 | ||
80ca4592 | 1741 | if (!strncmp(line, "%%BeginSetup", 12)) |
1742 | { | |
1743 | while (strncmp(line, "%%EndSetup", 10)) | |
1744 | { | |
1745 | if (!strncmp(line, "%%Page:", 7)) | |
ef416fc2 | 1746 | break; |
80ca4592 | 1747 | else if (!strncmp(line, "%%IncludeFeature:", 17)) |
ef416fc2 | 1748 | { |
80ca4592 | 1749 | /* |
1750 | * %%IncludeFeature: *MainKeyword OptionKeyword | |
1751 | */ | |
ef416fc2 | 1752 | |
80ca4592 | 1753 | if (doc->number_up == 1 && !doc->fitplot) |
1754 | doc->num_options = include_feature(ppd, line, doc->num_options, | |
1755 | &(doc->options)); | |
1756 | } | |
d09495fa | 1757 | else if (strncmp(line, "%%BeginSetup", 12)) |
b86bc4cf | 1758 | doc_write(doc, line, linelen); |
ef416fc2 | 1759 | |
80ca4592 | 1760 | if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0) |
1761 | break; | |
1762 | } | |
bd7854cb | 1763 | |
80ca4592 | 1764 | if (!strncmp(line, "%%EndSetup", 10)) |
1765 | linelen = cupsFileGetLine(fp, line, linesize); | |
1766 | else | |
c0e1af83 | 1767 | fputs(_("ERROR: Missing %%EndSetup!\n"), stderr); |
80ca4592 | 1768 | } |
ef416fc2 | 1769 | |
b86bc4cf | 1770 | doc_puts(doc, "%%EndSetup\n"); |
ef416fc2 | 1771 | |
80ca4592 | 1772 | return (linelen); |
1773 | } | |
ef416fc2 | 1774 | |
ef416fc2 | 1775 | |
80ca4592 | 1776 | /* |
1777 | * 'copy_trailer()' - Copy the document trailer... | |
1778 | * | |
1779 | * This function expects "line" to be filled with a %%Trailer comment line. | |
1780 | * On return, "line" will contain the next line in the file, if any. | |
1781 | */ | |
ef416fc2 | 1782 | |
80ca4592 | 1783 | static size_t /* O - Length of next line */ |
1784 | copy_trailer(cups_file_t *fp, /* I - File to read from */ | |
1785 | pstops_doc_t *doc, /* I - Document info */ | |
1786 | ppd_file_t *ppd, /* I - PPD file */ | |
1787 | int number, /* I - Number of pages */ | |
1788 | char *line, /* I - Line buffer */ | |
1789 | size_t linelen, /* I - Length of initial line */ | |
1790 | size_t linesize) /* I - Size of line buffer */ | |
1791 | { | |
1792 | /* | |
1793 | * Write the trailer comments... | |
1794 | */ | |
ef416fc2 | 1795 | |
80ca4592 | 1796 | puts("%%Trailer"); |
ef416fc2 | 1797 | |
80ca4592 | 1798 | while (linelen > 0) |
1799 | { | |
1800 | if (!strncmp(line, "%%EOF", 5)) | |
1801 | break; | |
1802 | else if (strncmp(line, "%%Trailer", 9) && | |
1803 | strncmp(line, "%%Pages:", 8) && | |
1804 | strncmp(line, "%%BoundingBox:", 14)) | |
1805 | fwrite(line, 1, linelen, stdout); | |
ef416fc2 | 1806 | |
80ca4592 | 1807 | linelen = cupsFileGetLine(fp, line, linesize); |
1808 | } | |
ef416fc2 | 1809 | |
80ca4592 | 1810 | fprintf(stderr, "DEBUG: Wrote %d pages...\n", number); |
ef416fc2 | 1811 | |
80ca4592 | 1812 | printf("%%%%Pages: %d\n", number); |
1813 | if (doc->number_up > 1 || doc->fitplot) | |
1814 | printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", | |
1815 | PageLeft, PageBottom, PageRight, PageTop); | |
1816 | else | |
1817 | printf("%%%%BoundingBox: %d %d %d %d\n", | |
1818 | doc->new_bounding_box[0], doc->new_bounding_box[1], | |
1819 | doc->new_bounding_box[2], doc->new_bounding_box[3]); | |
ef416fc2 | 1820 | |
80ca4592 | 1821 | return (linelen); |
1822 | } | |
ef416fc2 | 1823 | |
ef416fc2 | 1824 | |
80ca4592 | 1825 | /* |
1826 | * 'do_prolog()' - Send the necessary document prolog commands... | |
1827 | */ | |
ef416fc2 | 1828 | |
80ca4592 | 1829 | static void |
1830 | do_prolog(pstops_doc_t *doc, /* I - Document information */ | |
1831 | ppd_file_t *ppd) /* I - PPD file */ | |
1832 | { | |
b86bc4cf | 1833 | char *ps; /* PS commands */ |
1834 | ||
1835 | ||
80ca4592 | 1836 | /* |
1837 | * Send the document prolog commands... | |
1838 | */ | |
ef416fc2 | 1839 | |
80ca4592 | 1840 | if (ppd && ppd->patches) |
1841 | { | |
b86bc4cf | 1842 | doc_puts(doc, "%%BeginFeature: *JobPatchFile 1\n"); |
1843 | doc_puts(doc, ppd->patches); | |
1844 | doc_puts(doc, "\n%%EndFeature\n"); | |
80ca4592 | 1845 | } |
bd7854cb | 1846 | |
b86bc4cf | 1847 | if ((ps = ppdEmitString(ppd, PPD_ORDER_PROLOG, 0.0)) != NULL) |
1848 | { | |
1849 | doc_puts(doc, ps); | |
1850 | free(ps); | |
1851 | } | |
bd7854cb | 1852 | |
80ca4592 | 1853 | /* |
1854 | * Define ESPshowpage here so that applications that define their | |
1855 | * own procedure to do a showpage pick it up... | |
1856 | */ | |
ef416fc2 | 1857 | |
80ca4592 | 1858 | if (doc->use_ESPshowpage) |
b86bc4cf | 1859 | doc_puts(doc, "userdict/ESPshowpage/showpage load put\n" |
1860 | "userdict/showpage{}put\n"); | |
80ca4592 | 1861 | } |
ef416fc2 | 1862 | |
ef416fc2 | 1863 | |
80ca4592 | 1864 | /* |
1865 | * 'do_setup()' - Send the necessary document setup commands... | |
1866 | */ | |
ef416fc2 | 1867 | |
80ca4592 | 1868 | static void |
1869 | do_setup(pstops_doc_t *doc, /* I - Document information */ | |
1870 | ppd_file_t *ppd) /* I - PPD file */ | |
1871 | { | |
b86bc4cf | 1872 | char *ps; /* PS commands */ |
1873 | ||
1874 | ||
f301802f | 1875 | /* |
1876 | * Disable CTRL-D so that embedded files don't cause printing | |
1877 | * errors... | |
1878 | */ | |
1879 | ||
b86bc4cf | 1880 | doc_puts(doc, "% Disable CTRL-D as an end-of-file marker...\n"); |
1881 | doc_puts(doc, "userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put\n"); | |
f301802f | 1882 | |
80ca4592 | 1883 | /* |
1884 | * Mark any options from %%IncludeFeature: comments... | |
1885 | */ | |
ef416fc2 | 1886 | |
80ca4592 | 1887 | cupsMarkOptions(ppd, doc->num_options, doc->options); |
ef416fc2 | 1888 | |
80ca4592 | 1889 | /* |
1890 | * Send all the printer-specific setup commands... | |
1891 | */ | |
ef416fc2 | 1892 | |
b86bc4cf | 1893 | if ((ps = ppdEmitString(ppd, PPD_ORDER_DOCUMENT, 0.0)) != NULL) |
1894 | { | |
1895 | doc_puts(doc, ps); | |
1896 | free(ps); | |
1897 | } | |
1898 | ||
1899 | if ((ps = ppdEmitString(ppd, PPD_ORDER_ANY, 0.0)) != NULL) | |
1900 | { | |
1901 | doc_puts(doc, ps); | |
1902 | free(ps); | |
1903 | } | |
ef416fc2 | 1904 | |
80ca4592 | 1905 | /* |
1906 | * Set the number of copies for the job... | |
1907 | */ | |
ef416fc2 | 1908 | |
80ca4592 | 1909 | if (doc->copies != 1 && (!doc->collate || !doc->slow_collate)) |
1910 | { | |
b86bc4cf | 1911 | doc_printf(doc, "%%RBIBeginNonPPDFeature: *NumCopies %d\n", doc->copies); |
1912 | doc_printf(doc, | |
1913 | "%d/languagelevel where{pop languagelevel 2 ge}{false}ifelse\n" | |
1914 | "{1 dict begin/NumCopies exch def currentdict end " | |
1915 | "setpagedevice}\n" | |
1916 | "{userdict/#copies 3 -1 roll put}ifelse\n", doc->copies); | |
1917 | doc_puts(doc, "%RBIEndNonPPDFeature\n"); | |
80ca4592 | 1918 | } |
ef416fc2 | 1919 | |
80ca4592 | 1920 | /* |
1921 | * If we are doing N-up printing, disable setpagedevice... | |
1922 | */ | |
ef416fc2 | 1923 | |
80ca4592 | 1924 | if (doc->number_up > 1) |
b86bc4cf | 1925 | doc_puts(doc, "userdict/setpagedevice{pop}bind put\n"); |
ef416fc2 | 1926 | |
80ca4592 | 1927 | /* |
1928 | * Changes to the transfer function must be made AFTER any | |
1929 | * setpagedevice code... | |
1930 | */ | |
ef416fc2 | 1931 | |
80ca4592 | 1932 | if (doc->gamma != 1.0f || doc->brightness != 1.0f) |
b86bc4cf | 1933 | doc_printf(doc, "{ neg 1 add dup 0 lt { pop 1 } { %.3f exp neg 1 add } " |
1934 | "ifelse %.3f mul } bind settransfer\n", | |
1935 | doc->gamma, doc->brightness); | |
ef416fc2 | 1936 | |
80ca4592 | 1937 | /* |
1938 | * Make sure we have rectclip and rectstroke procedures of some sort... | |
1939 | */ | |
ef416fc2 | 1940 | |
b86bc4cf | 1941 | doc_puts(doc, |
1942 | "% x y w h ESPrc - Clip to a rectangle.\n" | |
1943 | "userdict/ESPrc/rectclip where{pop/rectclip load}\n" | |
1944 | "{{newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n" | |
1945 | "neg 0 rlineto closepath clip newpath}bind}ifelse put\n"); | |
1946 | ||
1947 | doc_puts(doc, | |
1948 | "% x y w h ESPrf - Fill a rectangle.\n" | |
1949 | "userdict/ESPrf/rectfill where{pop/rectfill load}\n" | |
1950 | "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n" | |
1951 | "neg 0 rlineto closepath fill grestore}bind}ifelse put\n"); | |
1952 | ||
1953 | doc_puts(doc, | |
1954 | "% x y w h ESPrs - Stroke a rectangle.\n" | |
1955 | "userdict/ESPrs/rectstroke where{pop/rectstroke load}\n" | |
1956 | "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n" | |
1957 | "neg 0 rlineto closepath stroke grestore}bind}ifelse put\n"); | |
ef416fc2 | 1958 | |
80ca4592 | 1959 | /* |
1960 | * Write the page and label prologs... | |
1961 | */ | |
ef416fc2 | 1962 | |
80ca4592 | 1963 | if (doc->number_up == 2 || doc->number_up == 6) |
1964 | { | |
ef416fc2 | 1965 | /* |
80ca4592 | 1966 | * For 2- and 6-up output, rotate the labels to match the orientation |
1967 | * of the pages... | |
ef416fc2 | 1968 | */ |
1969 | ||
80ca4592 | 1970 | if (Orientation & 1) |
b86bc4cf | 1971 | write_label_prolog(doc, doc->page_label, PageBottom, |
1972 | PageWidth - PageLength + PageTop, PageLength); | |
80ca4592 | 1973 | else |
b86bc4cf | 1974 | write_label_prolog(doc, doc->page_label, PageLeft, PageRight, |
1975 | PageLength); | |
80ca4592 | 1976 | } |
1977 | else | |
b86bc4cf | 1978 | write_label_prolog(doc, doc->page_label, PageBottom, PageTop, PageWidth); |
80ca4592 | 1979 | } |
1980 | ||
ef416fc2 | 1981 | |
80ca4592 | 1982 | /* |
1983 | * 'doc_printf()' - Send a formatted string to stdout and/or the temp file. | |
1984 | * | |
1985 | * This function should be used for all page-level output that is affected | |
1986 | * by ordering, collation, etc. | |
1987 | */ | |
ef416fc2 | 1988 | |
80ca4592 | 1989 | static void |
1990 | doc_printf(pstops_doc_t *doc, /* I - Document information */ | |
1991 | const char *format, /* I - Printf-style format string */ | |
1992 | ...) /* I - Additional arguments as needed */ | |
1993 | { | |
1994 | va_list ap; /* Pointer to arguments */ | |
1995 | char buffer[1024]; /* Output buffer */ | |
1996 | size_t bytes; /* Number of bytes to write */ | |
ef416fc2 | 1997 | |
ef416fc2 | 1998 | |
80ca4592 | 1999 | va_start(ap, format); |
2000 | bytes = vsnprintf(buffer, sizeof(buffer), format, ap); | |
2001 | va_end(ap); | |
2002 | ||
2003 | if (bytes > sizeof(buffer)) | |
2004 | { | |
2005 | fprintf(stderr, | |
c0e1af83 | 2006 | _("ERROR: doc_printf overflow (%d bytes) detected, aborting!\n"), |
80ca4592 | 2007 | (int)bytes); |
2008 | exit(1); | |
2009 | } | |
2010 | ||
2011 | doc_write(doc, buffer, bytes); | |
2012 | } | |
2013 | ||
2014 | ||
2015 | /* | |
2016 | * 'doc_puts()' - Send a nul-terminated string to stdout and/or the temp file. | |
2017 | * | |
2018 | * This function should be used for all page-level output that is affected | |
2019 | * by ordering, collation, etc. | |
2020 | */ | |
2021 | ||
2022 | static void | |
2023 | doc_puts(pstops_doc_t *doc, /* I - Document information */ | |
2024 | const char *s) /* I - String to send */ | |
2025 | { | |
2026 | doc_write(doc, s, strlen(s)); | |
2027 | } | |
2028 | ||
ef416fc2 | 2029 | |
80ca4592 | 2030 | /* |
2031 | * 'doc_write()' - Send data to stdout and/or the temp file. | |
2032 | */ | |
ef416fc2 | 2033 | |
80ca4592 | 2034 | static void |
2035 | doc_write(pstops_doc_t *doc, /* I - Document information */ | |
2036 | const char *s, /* I - Data to send */ | |
2037 | size_t len) /* I - Number of bytes to send */ | |
2038 | { | |
2039 | if (!doc->slow_order) | |
2040 | fwrite(s, 1, len, stdout); | |
ef416fc2 | 2041 | |
80ca4592 | 2042 | if (doc->temp) |
2043 | cupsFileWrite(doc->temp, s, len); | |
2044 | } | |
ef416fc2 | 2045 | |
ef416fc2 | 2046 | |
80ca4592 | 2047 | /* |
2048 | * 'end_nup()' - End processing for N-up printing... | |
2049 | */ | |
ef416fc2 | 2050 | |
80ca4592 | 2051 | static void |
2052 | end_nup(pstops_doc_t *doc, /* I - Document information */ | |
2053 | int number) /* I - Page number */ | |
2054 | { | |
2abf387c | 2055 | if (doc->number_up > 1) |
ed486911 | 2056 | doc_puts(doc, "userdict/ESPsave get restore\n"); |
ef416fc2 | 2057 | |
80ca4592 | 2058 | switch (doc->number_up) |
2059 | { | |
2060 | case 1 : | |
2061 | if (doc->use_ESPshowpage) | |
2062 | { | |
ed486911 | 2063 | write_labels(doc, Orientation); |
2064 | doc_puts(doc, "ESPshowpage\n"); | |
80ca4592 | 2065 | } |
2066 | break; | |
ef416fc2 | 2067 | |
80ca4592 | 2068 | case 2 : |
2069 | case 6 : | |
2070 | if (is_last_page(number) && doc->use_ESPshowpage) | |
2071 | { | |
2072 | if (Orientation & 1) | |
2073 | { | |
2074 | /* | |
2075 | * Rotate the labels back to portrait... | |
2076 | */ | |
ef416fc2 | 2077 | |
ed486911 | 2078 | write_labels(doc, Orientation - 1); |
80ca4592 | 2079 | } |
2080 | else if (Orientation == 0) | |
2081 | { | |
2082 | /* | |
2083 | * Rotate the labels to landscape... | |
2084 | */ | |
ef416fc2 | 2085 | |
ed486911 | 2086 | write_labels(doc, doc->normal_landscape ? 1 : 3); |
80ca4592 | 2087 | } |
2088 | else | |
2089 | { | |
2090 | /* | |
2091 | * Rotate the labels to landscape... | |
2092 | */ | |
ef416fc2 | 2093 | |
ed486911 | 2094 | write_labels(doc, doc->normal_landscape ? 3 : 1); |
80ca4592 | 2095 | } |
ef416fc2 | 2096 | |
ed486911 | 2097 | doc_puts(doc, "ESPshowpage\n"); |
80ca4592 | 2098 | } |
2099 | break; | |
ef416fc2 | 2100 | |
80ca4592 | 2101 | default : |
2102 | if (is_last_page(number) && doc->use_ESPshowpage) | |
ef416fc2 | 2103 | { |
ed486911 | 2104 | write_labels(doc, Orientation); |
2105 | doc_puts(doc, "ESPshowpage\n"); | |
ef416fc2 | 2106 | } |
80ca4592 | 2107 | break; |
ef416fc2 | 2108 | } |
2109 | ||
80ca4592 | 2110 | fflush(stdout); |
2111 | } | |
2112 | ||
2113 | ||
2114 | /* | |
2115 | * 'include_feature()' - Include a printer option/feature command. | |
2116 | */ | |
2117 | ||
2118 | static int /* O - New number of options */ | |
2119 | include_feature( | |
2120 | ppd_file_t *ppd, /* I - PPD file */ | |
2121 | const char *line, /* I - DSC line */ | |
2122 | int num_options, /* I - Number of options */ | |
2123 | cups_option_t **options) /* IO - Options */ | |
2124 | { | |
2125 | char name[255], /* Option name */ | |
2126 | value[255]; /* Option value */ | |
2127 | ppd_option_t *option; /* Option in file */ | |
2128 | ppd_choice_t *choice; /* Choice */ | |
ef416fc2 | 2129 | |
ef416fc2 | 2130 | |
2131 | /* | |
80ca4592 | 2132 | * Get the "%%IncludeFeature: *Keyword OptionKeyword" values... |
ef416fc2 | 2133 | */ |
2134 | ||
80ca4592 | 2135 | if (sscanf(line + 17, "%254s%254s", name, value) != 2) |
bd7854cb | 2136 | { |
c0e1af83 | 2137 | fputs(_("ERROR: Bad %%IncludeFeature: comment!\n"), stderr); |
80ca4592 | 2138 | return (num_options); |
bd7854cb | 2139 | } |
ef416fc2 | 2140 | |
2141 | /* | |
80ca4592 | 2142 | * Find the option and choice... |
ef416fc2 | 2143 | */ |
2144 | ||
80ca4592 | 2145 | if ((option = ppdFindOption(ppd, name + 1)) == NULL) |
ef416fc2 | 2146 | { |
c0e1af83 | 2147 | fprintf(stderr, _("WARNING: Unknown option \"%s\"!\n"), name + 1); |
80ca4592 | 2148 | return (num_options); |
ef416fc2 | 2149 | } |
2150 | ||
80ca4592 | 2151 | if (option->section == PPD_ORDER_EXIT || |
2152 | option->section == PPD_ORDER_JCL) | |
2153 | { | |
c0e1af83 | 2154 | fprintf(stderr, _("WARNING: Option \"%s\" cannot be included via " |
2155 | "IncludeFeature!\n"), name + 1); | |
80ca4592 | 2156 | return (num_options); |
2157 | } | |
ef416fc2 | 2158 | |
80ca4592 | 2159 | if ((choice = ppdFindChoice(option, value)) == NULL) |
2160 | { | |
c0e1af83 | 2161 | fprintf(stderr, _("WARNING: Unknown choice \"%s\" for option \"%s\"!\n"), |
80ca4592 | 2162 | value, name + 1); |
2163 | return (num_options); | |
2164 | } | |
ef416fc2 | 2165 | |
80ca4592 | 2166 | /* |
2167 | * Add the option to the option array and return... | |
2168 | */ | |
2169 | ||
2170 | return (cupsAddOption(name + 1, value, num_options, options)); | |
ef416fc2 | 2171 | } |
2172 | ||
2173 | ||
2174 | /* | |
80ca4592 | 2175 | * 'parse_text()' - Parse a text value in a comment... |
2176 | * | |
2177 | * This function parses a DSC text value as defined on page 36 of the | |
2178 | * DSC specification. Text values are either surrounded by parenthesis | |
2179 | * or whitespace-delimited. | |
2180 | * | |
2181 | * The value returned is the literal characters for the entire text | |
2182 | * string, including any parenthesis and escape characters. | |
ef416fc2 | 2183 | */ |
2184 | ||
80ca4592 | 2185 | static char * /* O - Value or NULL on error */ |
2186 | parse_text(const char *start, /* I - Start of text value */ | |
2187 | char **end, /* O - End of text value */ | |
2188 | char *buffer, /* I - Buffer */ | |
2189 | size_t bufsize) /* I - Size of buffer */ | |
ef416fc2 | 2190 | { |
80ca4592 | 2191 | char *bufptr, /* Pointer in buffer */ |
2192 | *bufend; /* End of buffer */ | |
2193 | int level; /* Parenthesis level */ | |
ef416fc2 | 2194 | |
2195 | ||
80ca4592 | 2196 | /* |
2197 | * Skip leading whitespace... | |
2198 | */ | |
ef416fc2 | 2199 | |
80ca4592 | 2200 | while (isspace(*start & 255)) |
2201 | start ++; | |
2202 | ||
2203 | /* | |
2204 | * Then copy the value... | |
2205 | */ | |
ef416fc2 | 2206 | |
80ca4592 | 2207 | level = 0; |
2208 | bufptr = buffer; | |
2209 | bufend = buffer + bufsize - 1; | |
ef416fc2 | 2210 | |
80ca4592 | 2211 | while (bufptr < bufend) |
ef416fc2 | 2212 | { |
80ca4592 | 2213 | if (isspace(*start & 255) && !level) |
2214 | break; | |
ef416fc2 | 2215 | |
80ca4592 | 2216 | *bufptr++ = *start; |
2217 | ||
2218 | if (*start == '(') | |
2219 | level ++; | |
2220 | else if (*start == ')') | |
2221 | { | |
2222 | if (!level) | |
ef416fc2 | 2223 | { |
80ca4592 | 2224 | start ++; |
2225 | break; | |
ef416fc2 | 2226 | } |
2227 | else | |
80ca4592 | 2228 | level --; |
ef416fc2 | 2229 | } |
80ca4592 | 2230 | else if (*start == '\\') |
2231 | { | |
2232 | /* | |
2233 | * Copy escaped character... | |
2234 | */ | |
ef416fc2 | 2235 | |
80ca4592 | 2236 | int i; /* Looping var */ |
ef416fc2 | 2237 | |
80ca4592 | 2238 | |
2239 | for (i = 1; | |
2240 | i <= 3 && isdigit(start[i] & 255) && bufptr < bufend; | |
2241 | *bufptr++ = start[i], i ++); | |
2242 | } | |
2243 | ||
2244 | start ++; | |
ef416fc2 | 2245 | } |
2246 | ||
80ca4592 | 2247 | *bufptr = '\0'; |
2248 | ||
2249 | /* | |
2250 | * Return the value and new pointer into the line... | |
2251 | */ | |
2252 | ||
2253 | if (end) | |
2254 | *end = (char *)start; | |
2255 | ||
2256 | if (bufptr == bufend) | |
2257 | return (NULL); | |
2258 | else | |
2259 | return (buffer); | |
ef416fc2 | 2260 | } |
2261 | ||
2262 | ||
bd7854cb | 2263 | /* |
80ca4592 | 2264 | * 'set_pstops_options()' - Set pstops options... |
bd7854cb | 2265 | */ |
2266 | ||
80ca4592 | 2267 | static void |
2268 | set_pstops_options( | |
2269 | pstops_doc_t *doc, /* I - Document information */ | |
2270 | ppd_file_t *ppd, /* I - PPD file */ | |
2271 | char *argv[], /* I - Command-line arguments */ | |
2272 | int num_options, /* I - Number of options */ | |
2273 | cups_option_t *options) /* I - Options */ | |
bd7854cb | 2274 | { |
80ca4592 | 2275 | const char *val; /* Option value */ |
2276 | int intval; /* Integer option value */ | |
2277 | ppd_attr_t *attr; /* PPD attribute */ | |
2278 | ppd_option_t *option; /* PPD option */ | |
2279 | ppd_choice_t *choice; /* PPD choice */ | |
bd7854cb | 2280 | |
bd7854cb | 2281 | |
80ca4592 | 2282 | /* |
2283 | * Initialize document information structure... | |
2284 | */ | |
bd7854cb | 2285 | |
80ca4592 | 2286 | memset(doc, 0, sizeof(pstops_doc_t)); |
bd7854cb | 2287 | |
80ca4592 | 2288 | doc->job_id = atoi(argv[1]); |
2289 | doc->user = argv[2]; | |
2290 | doc->title = argv[3]; | |
2291 | doc->copies = atoi(argv[4]); | |
bd7854cb | 2292 | |
80ca4592 | 2293 | if (ppd && ppd->landscape > 0) |
2294 | doc->normal_landscape = 1; | |
bd7854cb | 2295 | |
80ca4592 | 2296 | doc->bounding_box[0] = (int)PageLeft; |
2297 | doc->bounding_box[1] = (int)PageBottom; | |
2298 | doc->bounding_box[2] = (int)PageRight; | |
2299 | doc->bounding_box[3] = (int)PageTop; | |
bd7854cb | 2300 | |
80ca4592 | 2301 | doc->new_bounding_box[0] = INT_MAX; |
2302 | doc->new_bounding_box[1] = INT_MAX; | |
2303 | doc->new_bounding_box[2] = INT_MIN; | |
2304 | doc->new_bounding_box[3] = INT_MIN; | |
bd7854cb | 2305 | |
80ca4592 | 2306 | /* |
2307 | * AP_FIRSTPAGE_InputSlot | |
2308 | */ | |
ef416fc2 | 2309 | |
80ca4592 | 2310 | doc->ap_input_slot = cupsGetOption("AP_FIRSTPAGE_InputSlot", num_options, |
2311 | options); | |
ef416fc2 | 2312 | |
80ca4592 | 2313 | /* |
2314 | * AP_FIRSTPAGE_ManualFeed | |
2315 | */ | |
ef416fc2 | 2316 | |
80ca4592 | 2317 | doc->ap_manual_feed = cupsGetOption("AP_FIRSTPAGE_ManualFeed", num_options, |
2318 | options); | |
ef416fc2 | 2319 | |
80ca4592 | 2320 | /* |
2321 | * brightness | |
2322 | */ | |
bd7854cb | 2323 | |
80ca4592 | 2324 | if ((val = cupsGetOption("brightness", num_options, options)) != NULL) |
ef416fc2 | 2325 | { |
80ca4592 | 2326 | /* |
2327 | * Get brightness value from 10 to 1000. | |
2328 | */ | |
ef416fc2 | 2329 | |
80ca4592 | 2330 | intval = atoi(val); |
ef416fc2 | 2331 | |
80ca4592 | 2332 | if (intval < 10 || intval > 1000) |
2333 | { | |
c0e1af83 | 2334 | fprintf(stderr, _("ERROR: Unsupported brightness value %s, using " |
2335 | "brightness=100!\n"), val); | |
80ca4592 | 2336 | doc->brightness = 1.0f; |
2337 | } | |
2338 | else | |
2339 | doc->brightness = intval * 0.01f; | |
ef416fc2 | 2340 | } |
80ca4592 | 2341 | else |
2342 | doc->brightness = 1.0f; | |
ef416fc2 | 2343 | |
ef416fc2 | 2344 | /* |
80ca4592 | 2345 | * collate, multiple-document-handling |
ef416fc2 | 2346 | */ |
2347 | ||
80ca4592 | 2348 | if ((val = cupsGetOption("multiple-document-handling", num_options, options)) != NULL) |
ef416fc2 | 2349 | { |
80ca4592 | 2350 | /* |
2351 | * This IPP attribute is unnecessarily complicated... | |
2352 | * | |
2353 | * single-document, separate-documents-collated-copies, and | |
2354 | * single-document-new-sheet all require collated copies. | |
2355 | * | |
2356 | * separate-documents-uncollated-copies allows for uncollated copies. | |
2357 | */ | |
2358 | ||
2359 | doc->collate = strcasecmp(val, "separate-documents-uncollated-copies") != 0; | |
ef416fc2 | 2360 | } |
2361 | ||
80ca4592 | 2362 | if ((val = cupsGetOption("Collate", num_options, options)) != NULL && |
2363 | (!strcasecmp(val, "true") ||!strcasecmp(val, "on") || | |
2364 | !strcasecmp(val, "yes"))) | |
2365 | doc->collate = 1; | |
ef416fc2 | 2366 | |
80ca4592 | 2367 | /* |
2368 | * emit-jcl | |
2369 | */ | |
ef416fc2 | 2370 | |
80ca4592 | 2371 | if ((val = cupsGetOption("emit-jcl", num_options, options)) != NULL && |
2372 | (!strcasecmp(val, "false") || !strcasecmp(val, "off") || | |
2373 | !strcasecmp(val, "no") || !strcmp(val, "0"))) | |
2374 | doc->emit_jcl = 0; | |
2375 | else | |
2376 | doc->emit_jcl = 1; | |
ef416fc2 | 2377 | |
ef416fc2 | 2378 | /* |
80ca4592 | 2379 | * fitplot |
ef416fc2 | 2380 | */ |
2381 | ||
80ca4592 | 2382 | if ((val = cupsGetOption("fitplot", num_options, options)) != NULL && |
2383 | (!strcasecmp(val, "true") || !strcasecmp(val, "on") || | |
2384 | !strcasecmp(val, "yes"))) | |
2385 | doc->fitplot = 1; | |
ef416fc2 | 2386 | |
2387 | /* | |
80ca4592 | 2388 | * gamma |
ef416fc2 | 2389 | */ |
2390 | ||
80ca4592 | 2391 | if ((val = cupsGetOption("gamma", num_options, options)) != NULL) |
ef416fc2 | 2392 | { |
80ca4592 | 2393 | /* |
2394 | * Get gamma value from 1 to 10000... | |
2395 | */ | |
ef416fc2 | 2396 | |
80ca4592 | 2397 | intval = atoi(val); |
ef416fc2 | 2398 | |
80ca4592 | 2399 | if (intval < 1 || intval > 10000) |
2400 | { | |
c0e1af83 | 2401 | fprintf(stderr, _("ERROR: Unsupported gamma value %s, using " |
2402 | "gamma=1000!\n"), val); | |
80ca4592 | 2403 | doc->gamma = 1.0f; |
2404 | } | |
2405 | else | |
2406 | doc->gamma = intval * 0.001f; | |
2407 | } | |
2408 | else | |
2409 | doc->gamma = 1.0f; | |
ef416fc2 | 2410 | |
2411 | /* | |
80ca4592 | 2412 | * InputSlot |
ef416fc2 | 2413 | */ |
2414 | ||
80ca4592 | 2415 | if ((choice = ppdFindMarkedChoice(ppd, "InputSlot")) != NULL) |
2416 | doc->input_slot = choice->choice; | |
ef416fc2 | 2417 | |
2418 | /* | |
80ca4592 | 2419 | * ManualFeed |
ef416fc2 | 2420 | */ |
2421 | ||
80ca4592 | 2422 | if ((choice = ppdFindMarkedChoice(ppd, "ManualFeed")) != NULL) |
2423 | doc->manual_feed = choice->choice; | |
ef416fc2 | 2424 | |
80ca4592 | 2425 | if ((val = cupsGetOption("mirror", num_options, options)) != NULL && |
2426 | (!strcasecmp(val, "true") || !strcasecmp(val, "on") || | |
2427 | !strcasecmp(val, "yes"))) | |
2428 | doc->mirror = 1; | |
ef416fc2 | 2429 | |
80ca4592 | 2430 | /* |
2431 | * number-up | |
2432 | */ | |
ef416fc2 | 2433 | |
80ca4592 | 2434 | if ((val = cupsGetOption("number-up", num_options, options)) != NULL) |
2435 | { | |
2436 | switch (intval = atoi(val)) | |
2437 | { | |
2438 | case 1 : | |
2439 | case 2 : | |
2440 | case 4 : | |
2441 | case 6 : | |
2442 | case 9 : | |
2443 | case 16 : | |
2444 | doc->number_up = intval; | |
2445 | break; | |
2446 | default : | |
2447 | fprintf(stderr, | |
c0e1af83 | 2448 | _("ERROR: Unsupported number-up value %d, using " |
2449 | "number-up=1!\n"), intval); | |
80ca4592 | 2450 | doc->number_up = 1; |
2451 | break; | |
2452 | } | |
2453 | } | |
2454 | else | |
2455 | doc->number_up = 1; | |
ef416fc2 | 2456 | |
80ca4592 | 2457 | /* |
2458 | * number-up-layout | |
2459 | */ | |
ef416fc2 | 2460 | |
80ca4592 | 2461 | if ((val = cupsGetOption("number-up-layout", num_options, options)) != NULL) |
ef416fc2 | 2462 | { |
80ca4592 | 2463 | if (!strcasecmp(val, "lrtb")) |
2464 | doc->number_up_layout = PSTOPS_LAYOUT_LRTB; | |
2465 | else if (!strcasecmp(val, "lrbt")) | |
2466 | doc->number_up_layout = PSTOPS_LAYOUT_LRBT; | |
2467 | else if (!strcasecmp(val, "rltb")) | |
2468 | doc->number_up_layout = PSTOPS_LAYOUT_RLTB; | |
2469 | else if (!strcasecmp(val, "rlbt")) | |
2470 | doc->number_up_layout = PSTOPS_LAYOUT_RLBT; | |
2471 | else if (!strcasecmp(val, "tblr")) | |
2472 | doc->number_up_layout = PSTOPS_LAYOUT_TBLR; | |
2473 | else if (!strcasecmp(val, "tbrl")) | |
2474 | doc->number_up_layout = PSTOPS_LAYOUT_TBRL; | |
2475 | else if (!strcasecmp(val, "btlr")) | |
2476 | doc->number_up_layout = PSTOPS_LAYOUT_BTLR; | |
2477 | else if (!strcasecmp(val, "btrl")) | |
2478 | doc->number_up_layout = PSTOPS_LAYOUT_BTRL; | |
2479 | else | |
2480 | { | |
c0e1af83 | 2481 | fprintf(stderr, _("ERROR: Unsupported number-up-layout value %s, using " |
2482 | "number-up-layout=lrtb!\n"), val); | |
80ca4592 | 2483 | doc->number_up_layout = PSTOPS_LAYOUT_LRTB; |
2484 | } | |
2485 | } | |
2486 | else | |
2487 | doc->number_up_layout = PSTOPS_LAYOUT_LRTB; | |
ef416fc2 | 2488 | |
80ca4592 | 2489 | /* |
2490 | * OutputOrder | |
2491 | */ | |
ef416fc2 | 2492 | |
80ca4592 | 2493 | if ((val = cupsGetOption("OutputOrder", num_options, options)) != NULL) |
2494 | { | |
2495 | if (!strcasecmp(val, "Reverse")) | |
2496 | doc->output_order = 1; | |
2497 | } | |
2498 | else if (ppd) | |
2499 | { | |
2500 | /* | |
2501 | * Figure out the right default output order from the PPD file... | |
2502 | */ | |
ef416fc2 | 2503 | |
80ca4592 | 2504 | if ((choice = ppdFindMarkedChoice(ppd, "OutputBin")) != NULL && |
2505 | (attr = ppdFindAttr(ppd, "PageStackOrder", choice->choice)) != NULL && | |
2506 | attr->value) | |
2507 | doc->output_order = !strcasecmp(attr->value, "Reverse"); | |
2508 | else if ((attr = ppdFindAttr(ppd, "DefaultOutputOrder", NULL)) != NULL && | |
2509 | attr->value) | |
2510 | doc->output_order = !strcasecmp(attr->value, "Reverse"); | |
2511 | } | |
ef416fc2 | 2512 | |
80ca4592 | 2513 | /* |
2514 | * page-border | |
2515 | */ | |
ef416fc2 | 2516 | |
80ca4592 | 2517 | if ((val = cupsGetOption("page-border", num_options, options)) != NULL) |
2518 | { | |
2519 | if (!strcasecmp(val, "none")) | |
2520 | doc->page_border = PSTOPS_BORDERNONE; | |
2521 | else if (!strcasecmp(val, "single")) | |
2522 | doc->page_border = PSTOPS_BORDERSINGLE; | |
2523 | else if (!strcasecmp(val, "single-thick")) | |
2524 | doc->page_border = PSTOPS_BORDERSINGLE2; | |
2525 | else if (!strcasecmp(val, "double")) | |
2526 | doc->page_border = PSTOPS_BORDERDOUBLE; | |
2527 | else if (!strcasecmp(val, "double-thick")) | |
2528 | doc->page_border = PSTOPS_BORDERDOUBLE2; | |
2529 | else | |
2530 | { | |
c0e1af83 | 2531 | fprintf(stderr, _("ERROR: Unsupported page-border value %s, using " |
2532 | "page-border=none!\n"), val); | |
80ca4592 | 2533 | doc->page_border = PSTOPS_BORDERNONE; |
2534 | } | |
ef416fc2 | 2535 | } |
80ca4592 | 2536 | else |
2537 | doc->page_border = PSTOPS_BORDERNONE; | |
ef416fc2 | 2538 | |
80ca4592 | 2539 | /* |
2540 | * page-label | |
2541 | */ | |
ef416fc2 | 2542 | |
80ca4592 | 2543 | doc->page_label = cupsGetOption("page-label", num_options, options); |
ef416fc2 | 2544 | |
80ca4592 | 2545 | /* |
2546 | * page-ranges | |
2547 | */ | |
ef416fc2 | 2548 | |
80ca4592 | 2549 | doc->page_ranges = cupsGetOption("page-ranges", num_options, options); |
ef416fc2 | 2550 | |
2551 | /* | |
80ca4592 | 2552 | * page-set |
ef416fc2 | 2553 | */ |
2554 | ||
80ca4592 | 2555 | doc->page_set = cupsGetOption("page-set", num_options, options); |
ef416fc2 | 2556 | |
2557 | /* | |
80ca4592 | 2558 | * Now figure out if we have to force collated copies, etc. |
ef416fc2 | 2559 | */ |
2560 | ||
80ca4592 | 2561 | if (ppd && ppd->manual_copies && Duplex && doc->copies > 1) |
ef416fc2 | 2562 | { |
80ca4592 | 2563 | /* |
2564 | * Force collated copies when printing a duplexed document to | |
2565 | * a non-PS printer that doesn't do hardware copy generation. | |
2566 | * Otherwise the copies will end up on the front/back side of | |
2567 | * each page. | |
2568 | */ | |
ef416fc2 | 2569 | |
80ca4592 | 2570 | doc->collate = 1; |
ef416fc2 | 2571 | } |
2572 | ||
2573 | /* | |
80ca4592 | 2574 | * See if we have to filter the fast or slow way... |
ef416fc2 | 2575 | */ |
2576 | ||
80ca4592 | 2577 | if (doc->collate && doc->copies > 1) |
ef416fc2 | 2578 | { |
80ca4592 | 2579 | /* |
2580 | * See if we need to manually collate the pages... | |
2581 | */ | |
2582 | ||
2583 | doc->slow_collate = 1; | |
2584 | ||
2585 | if ((choice = ppdFindMarkedChoice(ppd, "Collate")) != NULL && | |
2586 | !strcasecmp(choice->choice, "True")) | |
bd7854cb | 2587 | { |
80ca4592 | 2588 | /* |
2589 | * Hardware collate option is selected, see if the option is | |
2590 | * conflicting - if not, collate in hardware. Otherwise, | |
2591 | * turn the hardware collate option off... | |
2592 | */ | |
2593 | ||
2abf387c | 2594 | if ((option = ppdFindOption(ppd, "Collate")) != NULL && |
80ca4592 | 2595 | !option->conflicted) |
2596 | doc->slow_collate = 0; | |
bd7854cb | 2597 | else |
80ca4592 | 2598 | ppdMarkOption(ppd, "Collate", "False"); |
bd7854cb | 2599 | } |
ef416fc2 | 2600 | } |
2601 | else | |
80ca4592 | 2602 | doc->slow_collate = 0; |
2603 | ||
2604 | if (!ppdFindOption(ppd, "OutputOrder") && doc->output_order) | |
2605 | doc->slow_order = 1; | |
2606 | else | |
2607 | doc->slow_order = 0; | |
2608 | ||
2abf387c | 2609 | if (Duplex && |
2610 | (doc->slow_collate || doc->slow_order || | |
2611 | ((attr = ppdFindAttr(ppd, "cupsEvenDuplex", NULL)) != NULL && | |
2612 | attr->value && !strcasecmp(attr->value, "true")))) | |
80ca4592 | 2613 | doc->slow_duplex = 1; |
2614 | else | |
2615 | doc->slow_duplex = 0; | |
2616 | ||
2617 | /* | |
2618 | * Create a temporary file for page data if we need to filter slowly... | |
2619 | */ | |
2620 | ||
2621 | if (doc->slow_order || doc->slow_collate) | |
ef416fc2 | 2622 | { |
80ca4592 | 2623 | if ((doc->temp = cupsTempFile2(doc->tempfile, |
2624 | sizeof(doc->tempfile))) == NULL) | |
bd7854cb | 2625 | { |
c0e1af83 | 2626 | fprintf(stderr, _("ERROR: Unable to create temporary file: %s\n"), |
80ca4592 | 2627 | strerror(errno)); |
2628 | exit(1); | |
bd7854cb | 2629 | } |
ef416fc2 | 2630 | } |
80ca4592 | 2631 | |
2632 | /* | |
2633 | * Figure out if we should use ESPshowpage or not... | |
2634 | */ | |
2635 | ||
2636 | if (doc->page_label || getenv("CLASSIFICATION") || doc->number_up > 1 || | |
2637 | doc->page_border) | |
2638 | { | |
2639 | /* | |
2640 | * Yes, use ESPshowpage... | |
2641 | */ | |
2642 | ||
2643 | doc->use_ESPshowpage = 1; | |
2644 | } | |
2645 | ||
2646 | fprintf(stderr, "DEBUG: slow_collate=%d, slow_duplex=%d, slow_order=%d\n", | |
2647 | doc->slow_collate, doc->slow_duplex, doc->slow_order); | |
ef416fc2 | 2648 | } |
2649 | ||
2650 | ||
2651 | /* | |
80ca4592 | 2652 | * 'skip_page()' - Skip past a page that won't be printed... |
ef416fc2 | 2653 | */ |
2654 | ||
80ca4592 | 2655 | static size_t /* O - Length of next line */ |
2656 | skip_page(cups_file_t *fp, /* I - File to read from */ | |
2657 | char *line, /* I - Line buffer */ | |
2658 | size_t linelen, /* I - Length of initial line */ | |
2659 | size_t linesize) /* I - Size of line buffer */ | |
ef416fc2 | 2660 | { |
80ca4592 | 2661 | int level; /* Embedded document level */ |
ef416fc2 | 2662 | |
2663 | ||
80ca4592 | 2664 | level = 0; |
ef416fc2 | 2665 | |
80ca4592 | 2666 | while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0) |
ef416fc2 | 2667 | { |
80ca4592 | 2668 | if (level == 0 && |
d6ae789d | 2669 | (!strncmp(line, "%%Page:", 7) || !strncmp(line, "%%Trailer", 9))) |
ef416fc2 | 2670 | break; |
80ca4592 | 2671 | else if (!strncmp(line, "%%BeginDocument", 15) || |
2672 | !strncmp(line, "%ADO_BeginApplication", 21)) | |
2673 | level ++; | |
2674 | else if ((!strncmp(line, "%%EndDocument", 13) || | |
2675 | !strncmp(line, "%ADO_EndApplication", 19)) && level > 0) | |
2676 | level --; | |
2677 | else if (!strncmp(line, "%%BeginBinary:", 14) || | |
2678 | (!strncmp(line, "%%BeginData:", 12) && | |
2679 | !strstr(line, "ASCII") && !strstr(line, "Hex"))) | |
ef416fc2 | 2680 | { |
2681 | /* | |
80ca4592 | 2682 | * Skip binary data... |
ef416fc2 | 2683 | */ |
2684 | ||
80ca4592 | 2685 | int bytes; /* Bytes of data */ |
ef416fc2 | 2686 | |
ef416fc2 | 2687 | |
80ca4592 | 2688 | bytes = atoi(strchr(line, ':') + 1); |
ef416fc2 | 2689 | |
80ca4592 | 2690 | while (bytes > 0) |
2691 | { | |
2692 | if (bytes > linesize) | |
2693 | linelen = cupsFileRead(fp, line, linesize); | |
2694 | else | |
2695 | linelen = cupsFileRead(fp, line, bytes); | |
ef416fc2 | 2696 | |
80ca4592 | 2697 | if (linelen < 1) |
2698 | { | |
2699 | line[0] = '\0'; | |
2700 | perror("ERROR: Early end-of-file while reading binary data"); | |
2701 | return (0); | |
2702 | } | |
ef416fc2 | 2703 | |
80ca4592 | 2704 | bytes -= linelen; |
2705 | } | |
2706 | } | |
2707 | } | |
ef416fc2 | 2708 | |
80ca4592 | 2709 | return (linelen); |
ef416fc2 | 2710 | } |
2711 | ||
2712 | ||
ef416fc2 | 2713 | /* |
2714 | * 'start_nup()' - Start processing for N-up printing... | |
2715 | */ | |
2716 | ||
2717 | static void | |
80ca4592 | 2718 | start_nup(pstops_doc_t *doc, /* I - Document information */ |
2719 | int number, /* I - Page number */ | |
2720 | int show_border, /* I - Show the border? */ | |
2721 | const int *bounding_box) /* I - BoundingBox value */ | |
ef416fc2 | 2722 | { |
80ca4592 | 2723 | int pos; /* Position on page */ |
2724 | int x, y; /* Relative position of subpage */ | |
2725 | float w, l, /* Width and length of subpage */ | |
2726 | tx, ty; /* Translation values for subpage */ | |
2727 | float pagew, /* Printable width of page */ | |
2728 | pagel; /* Printable height of page */ | |
2729 | int bboxw, /* BoundingBox width */ | |
2730 | bboxl; /* BoundingBox height */ | |
2731 | ||
ef416fc2 | 2732 | |
2abf387c | 2733 | if (doc->number_up > 1) |
80ca4592 | 2734 | doc_puts(doc, "userdict/ESPsave save put\n"); |
ef416fc2 | 2735 | |
80ca4592 | 2736 | if (doc->mirror) |
2737 | doc_printf(doc, "%.1f 0.0 translate -1 1 scale\n", PageWidth); | |
ef416fc2 | 2738 | |
80ca4592 | 2739 | pos = (number - 1) % doc->number_up; |
2740 | pagew = PageRight - PageLeft; | |
2741 | pagel = PageTop - PageBottom; | |
ef416fc2 | 2742 | |
80ca4592 | 2743 | if (doc->fitplot) |
2744 | { | |
2745 | bboxw = bounding_box[2] - bounding_box[0]; | |
2746 | bboxl = bounding_box[3] - bounding_box[1]; | |
2747 | } | |
2748 | else | |
2749 | { | |
2750 | bboxw = PageWidth; | |
2751 | bboxl = PageLength; | |
2752 | } | |
ef416fc2 | 2753 | |
80ca4592 | 2754 | fprintf(stderr, "DEBUG: pagew = %.1f, pagel = %.1f\n", pagew, pagel); |
2755 | fprintf(stderr, "DEBUG: bboxw = %d, bboxl = %d\n", bboxw, bboxl); | |
2756 | fprintf(stderr, "DEBUG: PageLeft = %.1f, PageRight = %.1f\n", | |
2757 | PageLeft, PageRight); | |
2758 | fprintf(stderr, "DEBUG: PageTop = %.1f, PageBottom = %.1f\n", | |
2759 | PageTop, PageBottom); | |
2760 | fprintf(stderr, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n", | |
2761 | PageWidth, PageLength); | |
ef416fc2 | 2762 | |
2763 | switch (Orientation) | |
2764 | { | |
2765 | case 1 : /* Landscape */ | |
80ca4592 | 2766 | doc_printf(doc, "%.1f 0.0 translate 90 rotate\n", PageLength); |
ef416fc2 | 2767 | break; |
2768 | case 2 : /* Reverse Portrait */ | |
80ca4592 | 2769 | doc_printf(doc, "%.1f %.1f translate 180 rotate\n", PageWidth, |
2770 | PageLength); | |
ef416fc2 | 2771 | break; |
2772 | case 3 : /* Reverse Landscape */ | |
80ca4592 | 2773 | doc_printf(doc, "0.0 %.1f translate -90 rotate\n", PageWidth); |
ef416fc2 | 2774 | break; |
2775 | } | |
2776 | ||
80ca4592 | 2777 | if (Duplex && doc->number_up > 1 && ((number / doc->number_up) & 1)) |
2778 | doc_printf(doc, "%.1f %.1f translate\n", PageWidth - PageRight, PageBottom); | |
2779 | else if (doc->number_up > 1 || doc->fitplot) | |
2780 | doc_printf(doc, "%.1f %.1f translate\n", PageLeft, PageBottom); | |
ef416fc2 | 2781 | |
80ca4592 | 2782 | switch (doc->number_up) |
ef416fc2 | 2783 | { |
2784 | default : | |
80ca4592 | 2785 | if (doc->fitplot) |
bd7854cb | 2786 | { |
80ca4592 | 2787 | w = pagew; |
2788 | l = w * bboxl / bboxw; | |
bd7854cb | 2789 | |
80ca4592 | 2790 | if (l > pagel) |
bd7854cb | 2791 | { |
80ca4592 | 2792 | l = pagel; |
2793 | w = l * bboxw / bboxl; | |
bd7854cb | 2794 | } |
2795 | ||
80ca4592 | 2796 | tx = 0.5 * (pagew - w); |
2797 | ty = 0.5 * (pagel - l); | |
bd7854cb | 2798 | |
80ca4592 | 2799 | doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n", tx, ty, |
2800 | w / bboxw, l / bboxl); | |
bd7854cb | 2801 | } |
2802 | else | |
2803 | { | |
2804 | w = PageWidth; | |
2805 | l = PageLength; | |
2806 | } | |
ef416fc2 | 2807 | break; |
2808 | ||
2809 | case 2 : | |
2810 | if (Orientation & 1) | |
2811 | { | |
2812 | x = pos & 1; | |
2813 | ||
80ca4592 | 2814 | if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY) |
ef416fc2 | 2815 | x = 1 - x; |
2816 | ||
80ca4592 | 2817 | w = pagel; |
2818 | l = w * bboxl / bboxw; | |
ef416fc2 | 2819 | |
80ca4592 | 2820 | if (l > (pagew * 0.5)) |
ef416fc2 | 2821 | { |
80ca4592 | 2822 | l = pagew * 0.5; |
2823 | w = l * bboxw / bboxl; | |
ef416fc2 | 2824 | } |
2825 | ||
80ca4592 | 2826 | tx = 0.5 * (pagew * 0.5 - l); |
2827 | ty = 0.5 * (pagel - w); | |
ef416fc2 | 2828 | |
80ca4592 | 2829 | if (doc->normal_landscape) |
2830 | doc_printf(doc, "0.0 %.1f translate -90 rotate\n", pagel); | |
ef416fc2 | 2831 | else |
80ca4592 | 2832 | doc_printf(doc, "%.1f 0.0 translate 90 rotate\n", pagew); |
ef416fc2 | 2833 | |
80ca4592 | 2834 | doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n", |
2835 | ty, tx + pagew * 0.5 * x, w / bboxw, l / bboxl); | |
ef416fc2 | 2836 | } |
2837 | else | |
2838 | { | |
2839 | x = pos & 1; | |
2840 | ||
80ca4592 | 2841 | if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX) |
ef416fc2 | 2842 | x = 1 - x; |
2843 | ||
80ca4592 | 2844 | l = pagew; |
2845 | w = l * bboxw / bboxl; | |
ef416fc2 | 2846 | |
80ca4592 | 2847 | if (w > (pagel * 0.5)) |
ef416fc2 | 2848 | { |
80ca4592 | 2849 | w = pagel * 0.5; |
2850 | l = w * bboxl / bboxw; | |
ef416fc2 | 2851 | } |
2852 | ||
80ca4592 | 2853 | tx = 0.5 * (pagel * 0.5 - w); |
2854 | ty = 0.5 * (pagew - l); | |
ef416fc2 | 2855 | |
80ca4592 | 2856 | if (doc->normal_landscape) |
2857 | doc_printf(doc, "%.1f 0.0 translate 90 rotate\n", pagew); | |
ef416fc2 | 2858 | else |
80ca4592 | 2859 | doc_printf(doc, "0.0 %.1f translate -90 rotate\n", pagel); |
ef416fc2 | 2860 | |
80ca4592 | 2861 | doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n", |
2862 | tx + pagel * 0.5 * x, ty, w / bboxw, l / bboxl); | |
ef416fc2 | 2863 | } |
2864 | break; | |
2865 | ||
2866 | case 4 : | |
80ca4592 | 2867 | if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL) |
ef416fc2 | 2868 | { |
2869 | x = (pos / 2) & 1; | |
2870 | y = pos & 1; | |
2871 | } | |
2872 | else | |
2873 | { | |
2874 | x = pos & 1; | |
2875 | y = (pos / 2) & 1; | |
2876 | } | |
2877 | ||
80ca4592 | 2878 | if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX) |
ef416fc2 | 2879 | x = 1 - x; |
2880 | ||
80ca4592 | 2881 | if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY) |
ef416fc2 | 2882 | y = 1 - y; |
2883 | ||
80ca4592 | 2884 | w = pagew * 0.5; |
2885 | l = w * bboxl / bboxw; | |
ef416fc2 | 2886 | |
80ca4592 | 2887 | if (l > (pagel * 0.5)) |
ef416fc2 | 2888 | { |
80ca4592 | 2889 | l = pagel * 0.5; |
2890 | w = l * bboxw / bboxl; | |
ef416fc2 | 2891 | } |
2892 | ||
80ca4592 | 2893 | tx = 0.5 * (pagew * 0.5 - w); |
2894 | ty = 0.5 * (pagel * 0.5 - l); | |
ef416fc2 | 2895 | |
80ca4592 | 2896 | doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n", |
2897 | tx + x * pagew * 0.5, ty + y * pagel * 0.5, | |
2898 | w / bboxw, l / bboxl); | |
ef416fc2 | 2899 | break; |
2900 | ||
2901 | case 6 : | |
2902 | if (Orientation & 1) | |
2903 | { | |
80ca4592 | 2904 | if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL) |
ef416fc2 | 2905 | { |
2906 | x = pos / 3; | |
2907 | y = pos % 3; | |
2908 | ||
80ca4592 | 2909 | if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX) |
ef416fc2 | 2910 | x = 1 - x; |
2911 | ||
80ca4592 | 2912 | if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY) |
ef416fc2 | 2913 | y = 2 - y; |
2914 | } | |
2915 | else | |
2916 | { | |
2917 | x = pos & 1; | |
2918 | y = pos / 2; | |
2919 | ||
80ca4592 | 2920 | if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX) |
ef416fc2 | 2921 | x = 1 - x; |
2922 | ||
80ca4592 | 2923 | if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY) |
ef416fc2 | 2924 | y = 2 - y; |
2925 | } | |
2926 | ||
80ca4592 | 2927 | w = pagel * 0.5; |
2928 | l = w * bboxl / bboxw; | |
ef416fc2 | 2929 | |
80ca4592 | 2930 | if (l > (pagew * 0.333)) |
ef416fc2 | 2931 | { |
80ca4592 | 2932 | l = pagew * 0.333; |
2933 | w = l * bboxw / bboxl; | |
ef416fc2 | 2934 | } |
2935 | ||
80ca4592 | 2936 | tx = 0.5 * (pagel - 2 * w); |
2937 | ty = 0.5 * (pagew - 3 * l); | |
ef416fc2 | 2938 | |
80ca4592 | 2939 | if (doc->normal_landscape) |
f301802f | 2940 | doc_printf(doc, "0 %.1f translate -90 rotate\n", pagel); |
ef416fc2 | 2941 | else |
f301802f | 2942 | doc_printf(doc, "%.1f 0 translate 90 rotate\n", pagew); |
ef416fc2 | 2943 | |
80ca4592 | 2944 | doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n", |
f301802f | 2945 | tx + x * w, ty + y * l, l / bboxl, w / bboxw); |
ef416fc2 | 2946 | } |
2947 | else | |
2948 | { | |
80ca4592 | 2949 | if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL) |
ef416fc2 | 2950 | { |
2951 | x = pos / 2; | |
2952 | y = pos & 1; | |
2953 | ||
80ca4592 | 2954 | if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX) |
ef416fc2 | 2955 | x = 2 - x; |
2956 | ||
80ca4592 | 2957 | if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY) |
ef416fc2 | 2958 | y = 1 - y; |
2959 | } | |
2960 | else | |
2961 | { | |
2962 | x = pos % 3; | |
2963 | y = pos / 3; | |
2964 | ||
80ca4592 | 2965 | if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX) |
ef416fc2 | 2966 | x = 2 - x; |
2967 | ||
80ca4592 | 2968 | if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY) |
ef416fc2 | 2969 | y = 1 - y; |
2970 | } | |
2971 | ||
80ca4592 | 2972 | l = pagew * 0.5; |
2973 | w = l * bboxw / bboxl; | |
ef416fc2 | 2974 | |
80ca4592 | 2975 | if (w > (pagel * 0.333)) |
ef416fc2 | 2976 | { |
80ca4592 | 2977 | w = pagel * 0.333; |
2978 | l = w * bboxl / bboxw; | |
ef416fc2 | 2979 | } |
2980 | ||
f301802f | 2981 | tx = 0.5 * (pagel - 3 * w); |
2982 | ty = 0.5 * (pagew - 2 * l); | |
ef416fc2 | 2983 | |
80ca4592 | 2984 | if (doc->normal_landscape) |
f301802f | 2985 | doc_printf(doc, "%.1f 0 translate 90 rotate\n", pagew); |
ef416fc2 | 2986 | else |
f301802f | 2987 | doc_printf(doc, "0 %.1f translate -90 rotate\n", pagel); |
ef416fc2 | 2988 | |
80ca4592 | 2989 | doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n", |
f301802f | 2990 | tx + w * x, ty + l * y, w / bboxw, l / bboxl); |
2991 | ||
ef416fc2 | 2992 | } |
2993 | break; | |
2994 | ||
2995 | case 9 : | |
80ca4592 | 2996 | if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL) |
ef416fc2 | 2997 | { |
2998 | x = (pos / 3) % 3; | |
2999 | y = pos % 3; | |
3000 | } | |
3001 | else | |
3002 | { | |
3003 | x = pos % 3; | |
3004 | y = (pos / 3) % 3; | |
3005 | } | |
3006 | ||
80ca4592 | 3007 | if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX) |
ef416fc2 | 3008 | x = 2 - x; |
3009 | ||
80ca4592 | 3010 | if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY) |
ef416fc2 | 3011 | y = 2 - y; |
3012 | ||
80ca4592 | 3013 | w = pagew * 0.333; |
3014 | l = w * bboxl / bboxw; | |
ef416fc2 | 3015 | |
80ca4592 | 3016 | if (l > (pagel * 0.333)) |
ef416fc2 | 3017 | { |
80ca4592 | 3018 | l = pagel * 0.333; |
3019 | w = l * bboxw / bboxl; | |
ef416fc2 | 3020 | } |
3021 | ||
80ca4592 | 3022 | tx = 0.5 * (pagew * 0.333 - w); |
3023 | ty = 0.5 * (pagel * 0.333 - l); | |
ef416fc2 | 3024 | |
80ca4592 | 3025 | doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n", |
3026 | tx + x * pagew * 0.333, ty + y * pagel * 0.333, | |
3027 | w / bboxw, l / bboxl); | |
ef416fc2 | 3028 | break; |
3029 | ||
3030 | case 16 : | |
80ca4592 | 3031 | if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL) |
ef416fc2 | 3032 | { |
3033 | x = (pos / 4) & 3; | |
3034 | y = pos & 3; | |
3035 | } | |
3036 | else | |
3037 | { | |
3038 | x = pos & 3; | |
3039 | y = (pos / 4) & 3; | |
3040 | } | |
3041 | ||
80ca4592 | 3042 | if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX) |
ef416fc2 | 3043 | x = 3 - x; |
3044 | ||
80ca4592 | 3045 | if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY) |
ef416fc2 | 3046 | y = 3 - y; |
3047 | ||
80ca4592 | 3048 | w = pagew * 0.25; |
3049 | l = w * bboxl / bboxw; | |
ef416fc2 | 3050 | |
80ca4592 | 3051 | if (l > (pagel * 0.25)) |
ef416fc2 | 3052 | { |
80ca4592 | 3053 | l = pagel * 0.25; |
3054 | w = l * bboxw / bboxl; | |
ef416fc2 | 3055 | } |
3056 | ||
80ca4592 | 3057 | tx = 0.5 * (pagew * 0.25 - w); |
3058 | ty = 0.5 * (pagel * 0.25 - l); | |
ef416fc2 | 3059 | |
80ca4592 | 3060 | doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n", |
3061 | tx + x * pagew * 0.25, ty + y * pagel * 0.25, | |
3062 | w / bboxw, l / bboxl); | |
ef416fc2 | 3063 | break; |
3064 | } | |
3065 | ||
3066 | /* | |
3067 | * Draw borders as necessary... | |
3068 | */ | |
3069 | ||
80ca4592 | 3070 | if (doc->page_border && show_border) |
ef416fc2 | 3071 | { |
80ca4592 | 3072 | int rects; /* Number of border rectangles */ |
3073 | float fscale, /* Scaling value for points */ | |
3074 | margin; /* Current margin for borders */ | |
ef416fc2 | 3075 | |
3076 | ||
80ca4592 | 3077 | rects = (doc->page_border & PSTOPS_BORDERDOUBLE) ? 2 : 1; |
ef416fc2 | 3078 | fscale = PageWidth / w; |
3079 | margin = 2.25 * fscale; | |
3080 | ||
3081 | /* | |
3082 | * Set the line width and color... | |
3083 | */ | |
3084 | ||
80ca4592 | 3085 | doc_puts(doc, "gsave\n"); |
3086 | doc_printf(doc, "%.3f setlinewidth 0 setgray newpath\n", | |
3087 | (doc->page_border & PSTOPS_BORDERTHICK) ? 0.5 * fscale : | |
3088 | 0.24 * fscale); | |
ef416fc2 | 3089 | |
3090 | /* | |
3091 | * Draw border boxes... | |
3092 | */ | |
3093 | ||
3094 | for (; rects > 0; rects --, margin += 2 * fscale) | |
80ca4592 | 3095 | if (doc->number_up > 1) |
3096 | doc_printf(doc, "%.1f %.1f %.1f %.1f ESPrs\n", | |
3097 | margin - 2.25 * fscale, | |
3098 | margin - 2.25 * fscale, | |
3099 | bboxw + 4.5 * fscale - 2 * margin, | |
3100 | bboxl + 4.5 * fscale - 2 * margin); | |
ef416fc2 | 3101 | else |
80ca4592 | 3102 | doc_printf(doc, "%.1f %.1f %.1f %.1f ESPrs\n", |
3103 | PageLeft + margin, | |
3104 | PageBottom + margin, | |
3105 | PageRight - PageLeft - 2 * margin, | |
3106 | PageTop - PageBottom - 2 * margin); | |
ef416fc2 | 3107 | |
3108 | /* | |
3109 | * Restore pen settings... | |
3110 | */ | |
3111 | ||
80ca4592 | 3112 | doc_puts(doc, "grestore\n"); |
ef416fc2 | 3113 | } |
3114 | ||
80ca4592 | 3115 | if (doc->fitplot) |
ef416fc2 | 3116 | { |
3117 | /* | |
3118 | * Clip the page that follows to the bounding box of the page... | |
3119 | */ | |
3120 | ||
80ca4592 | 3121 | doc_printf(doc, "%d %d translate\n", -bounding_box[0], |
3122 | -bounding_box[1]); | |
3123 | doc_printf(doc, "%d %d %d %d ESPrc\n", bounding_box[0], bounding_box[1], | |
3124 | bboxw, bboxl); | |
3125 | } | |
3126 | else if (doc->number_up > 1) | |
3127 | { | |
3128 | /* | |
3129 | * Clip the page that follows to the default page size... | |
3130 | */ | |
3131 | ||
3132 | doc_printf(doc, "0 0 %d %d ESPrc\n", bboxw, bboxl); | |
ef416fc2 | 3133 | } |
3134 | } | |
3135 | ||
3136 | ||
b86bc4cf | 3137 | /* |
3138 | * 'write_label_prolog()' - Write the prolog with the classification | |
3139 | * and page label. | |
3140 | */ | |
3141 | ||
3142 | static void | |
3143 | write_label_prolog(pstops_doc_t *doc, /* I - Document info */ | |
3144 | const char *label, /* I - Page label */ | |
3145 | float bottom, /* I - Bottom position in points */ | |
3146 | float top, /* I - Top position in points */ | |
3147 | float width) /* I - Width in points */ | |
3148 | { | |
3149 | const char *classification; /* CLASSIFICATION environment variable */ | |
3150 | const char *ptr; /* Temporary string pointer */ | |
3151 | ||
3152 | ||
3153 | /* | |
3154 | * First get the current classification... | |
3155 | */ | |
3156 | ||
3157 | if ((classification = getenv("CLASSIFICATION")) == NULL) | |
3158 | classification = ""; | |
3159 | if (strcmp(classification, "none") == 0) | |
3160 | classification = ""; | |
3161 | ||
3162 | /* | |
3163 | * If there is nothing to show, bind an empty 'write labels' procedure | |
3164 | * and return... | |
3165 | */ | |
3166 | ||
3167 | if (!classification[0] && (label == NULL || !label[0])) | |
3168 | { | |
3169 | doc_puts(doc, "userdict/ESPwl{}bind put\n"); | |
3170 | return; | |
3171 | } | |
3172 | ||
3173 | /* | |
3174 | * Set the classification + page label string... | |
3175 | */ | |
3176 | ||
3177 | doc_puts(doc, "userdict"); | |
3178 | if (!strcmp(classification, "confidential")) | |
3179 | doc_puts(doc, "/ESPpl(CONFIDENTIAL"); | |
3180 | else if (!strcmp(classification, "classified")) | |
3181 | doc_puts(doc, "/ESPpl(CLASSIFIED"); | |
3182 | else if (!strcmp(classification, "secret")) | |
3183 | doc_puts(doc, "/ESPpl(SECRET"); | |
3184 | else if (!strcmp(classification, "topsecret")) | |
3185 | doc_puts(doc, "/ESPpl(TOP SECRET"); | |
3186 | else if (!strcmp(classification, "unclassified")) | |
3187 | doc_puts(doc, "/ESPpl(UNCLASSIFIED"); | |
3188 | else | |
3189 | { | |
3190 | doc_puts(doc, "/ESPpl("); | |
3191 | ||
3192 | for (ptr = classification; *ptr; ptr ++) | |
3193 | { | |
3194 | if (*ptr < 32 || *ptr > 126) | |
3195 | doc_printf(doc, "\\%03o", *ptr); | |
3196 | else if (*ptr == '_') | |
3197 | doc_puts(doc, " "); | |
3198 | else if (*ptr == '(' || *ptr == ')' || *ptr == '\\') | |
3199 | doc_printf(doc, "\\%c", *ptr); | |
3200 | else | |
3201 | doc_printf(doc, "%c", *ptr); | |
3202 | } | |
3203 | } | |
3204 | ||
3205 | if (label) | |
3206 | { | |
3207 | if (classification[0]) | |
3208 | doc_puts(doc, " - "); | |
3209 | ||
3210 | /* | |
3211 | * Quote the label string as needed... | |
3212 | */ | |
3213 | ||
3214 | for (ptr = label; *ptr; ptr ++) | |
3215 | { | |
3216 | if (*ptr < 32 || *ptr > 126) | |
3217 | doc_printf(doc, "\\%03o", *ptr); | |
3218 | else if (*ptr == '(' || *ptr == ')' || *ptr == '\\') | |
3219 | doc_printf(doc, "\\%c", *ptr); | |
3220 | else | |
3221 | doc_printf(doc, "%c", *ptr); | |
3222 | } | |
3223 | } | |
3224 | ||
3225 | doc_puts(doc, ")put\n"); | |
3226 | ||
3227 | /* | |
3228 | * Then get a 14 point Helvetica-Bold font... | |
3229 | */ | |
3230 | ||
3231 | doc_puts(doc, "userdict/ESPpf /Helvetica-Bold findfont 14 scalefont put\n"); | |
3232 | ||
3233 | /* | |
3234 | * Finally, the procedure to write the labels on the page... | |
3235 | */ | |
3236 | ||
3237 | doc_puts(doc, "userdict/ESPwl{\n"); | |
3238 | doc_puts(doc, " ESPpf setfont\n"); | |
3239 | doc_printf(doc, " ESPpl stringwidth pop dup 12 add exch -0.5 mul %.0f add\n", | |
3240 | width * 0.5f); | |
3241 | doc_puts(doc, " 1 setgray\n"); | |
3242 | doc_printf(doc, " dup 6 sub %.0f 3 index 20 ESPrf\n", bottom - 2.0); | |
3243 | doc_printf(doc, " dup 6 sub %.0f 3 index 20 ESPrf\n", top - 18.0); | |
3244 | doc_puts(doc, " 0 setgray\n"); | |
3245 | doc_printf(doc, " dup 6 sub %.0f 3 index 20 ESPrs\n", bottom - 2.0); | |
3246 | doc_printf(doc, " dup 6 sub %.0f 3 index 20 ESPrs\n", top - 18.0); | |
3247 | doc_printf(doc, " dup %.0f moveto ESPpl show\n", bottom + 2.0); | |
3248 | doc_printf(doc, " %.0f moveto ESPpl show\n", top - 14.0); | |
3249 | doc_puts(doc, "pop\n"); | |
3250 | doc_puts(doc, "}bind put\n"); | |
3251 | } | |
3252 | ||
3253 | ||
ed486911 | 3254 | /* |
3255 | * 'write_labels()' - Write the actual page labels. | |
3256 | * | |
3257 | * This function is a copy of the one in common.c since we need to | |
3258 | * use doc_puts/doc_printf instead of puts/printf... | |
3259 | */ | |
3260 | ||
3261 | static void | |
3262 | write_labels(pstops_doc_t *doc, /* I - Document information */ | |
3263 | int orient) /* I - Orientation of the page */ | |
3264 | { | |
3265 | float width, /* Width of page */ | |
3266 | length; /* Length of page */ | |
3267 | ||
3268 | ||
3269 | doc_puts(doc, "gsave\n"); | |
3270 | ||
3271 | if ((orient ^ Orientation) & 1) | |
3272 | { | |
3273 | width = PageLength; | |
3274 | length = PageWidth; | |
3275 | } | |
3276 | else | |
3277 | { | |
3278 | width = PageWidth; | |
3279 | length = PageLength; | |
3280 | } | |
3281 | ||
3282 | switch (orient & 3) | |
3283 | { | |
3284 | case 1 : /* Landscape */ | |
3285 | doc_printf(doc, "%.1f 0.0 translate 90 rotate\n", length); | |
3286 | break; | |
3287 | case 2 : /* Reverse Portrait */ | |
3288 | doc_printf(doc, "%.1f %.1f translate 180 rotate\n", width, length); | |
3289 | break; | |
3290 | case 3 : /* Reverse Landscape */ | |
3291 | doc_printf(doc, "0.0 %.1f translate -90 rotate\n", width); | |
3292 | break; | |
3293 | } | |
3294 | ||
3295 | doc_puts(doc, "ESPwl\n"); | |
3296 | doc_puts(doc, "grestore\n"); | |
3297 | } | |
3298 | ||
3299 | ||
ef416fc2 | 3300 | /* |
c0e1af83 | 3301 | * End of "$Id: pstops.c 6420 2007-03-30 20:00:59Z mike $". |
ef416fc2 | 3302 | */ |