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