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