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