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