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