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