]> git.ipfire.org Git - thirdparty/cups.git/blame - filter/pstops.c
Merge changes from 1.4svn-r7067.
[thirdparty/cups.git] / filter / pstops.c
CommitLineData
ef416fc2 1/*
2e4ff8af 2 * "$Id: pstops.c 7006 2007-10-04 17:43:38Z 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
a41f09e2 892 if (number && !is_first_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
2e4ff8af
MS
950 if (doc->slow_collate)
951 copy = !doc->slow_order;
952 else
953 copy = doc->copies - 1;
954
955 for (; copy < doc->copies; copy ++)
80ca4592 956 {
c24d2134
MS
957 if (JobCanceled)
958 break;
959
2abf387c 960 /*
961 * Send end-of-job stuff followed by any start-of-job stuff required
962 * for the JCL options...
963 */
964
b86bc4cf 965 if (number && doc->emit_jcl && ppd && ppd->jcl_end)
2abf387c 966 {
b86bc4cf 967 /*
968 * Send the trailer...
969 */
970
971 puts("%%Trailer");
972 printf("%%%%Pages: %d\n", cupsArrayCount(doc->pages));
973 if (doc->number_up > 1 || doc->fitplot)
974 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
975 PageLeft, PageBottom, PageRight, PageTop);
2abf387c 976 else
b86bc4cf 977 printf("%%%%BoundingBox: %d %d %d %d\n",
978 doc->new_bounding_box[0], doc->new_bounding_box[1],
979 doc->new_bounding_box[2], doc->new_bounding_box[3]);
980 puts("%%EOF");
2abf387c 981
b86bc4cf 982 /*
983 * Start a new document...
984 */
985
986 ppdEmitJCLEnd(ppd, stdout);
2abf387c 987 ppdEmitJCL(ppd, stdout, doc->job_id, doc->user, doc->title);
2abf387c 988
b86bc4cf 989 puts("%!PS-Adobe-3.0");
b86bc4cf 990
991 number = 0;
992 }
2abf387c 993
f7deaa1a 994 /*
995 * Copy the prolog as needed...
996 */
997
998 if (!number)
999 {
1000 pageinfo = (pstops_page_t *)cupsArrayFirst(doc->pages);
1001 copy_bytes(doc->temp, 0, pageinfo->offset);
1002 }
1003
2abf387c 1004 /*
1005 * Then copy all of the pages...
1006 */
1007
80ca4592 1008 pageinfo = doc->slow_order ? (pstops_page_t *)cupsArrayLast(doc->pages) :
1009 (pstops_page_t *)cupsArrayFirst(doc->pages);
1010
1011 while (pageinfo)
ef416fc2 1012 {
c24d2134
MS
1013 if (JobCanceled)
1014 break;
1015
80ca4592 1016 number ++;
ef416fc2 1017
80ca4592 1018 if (!ppd || !ppd->num_filters)
2e4ff8af
MS
1019 fprintf(stderr, "PAGE: %d %d\n", number,
1020 doc->slow_collate ? 1 : doc->copies);
ef416fc2 1021
80ca4592 1022 if (doc->number_up > 1)
1023 {
1024 printf("%%%%Page: (%d) %d\n", number, number);
1025 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1026 PageLeft, PageBottom, PageRight, PageTop);
1027 }
1028 else
ef416fc2 1029 {
411affcf 1030 printf("%%%%Page: %s %d\n", pageinfo->label, number);
80ca4592 1031 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1032 pageinfo->bounding_box[0], pageinfo->bounding_box[1],
1033 pageinfo->bounding_box[2], pageinfo->bounding_box[3]);
ef416fc2 1034 }
80ca4592 1035
1036 copy_bytes(doc->temp, pageinfo->offset, pageinfo->length);
1037
1038 pageinfo = doc->slow_order ? (pstops_page_t *)cupsArrayPrev(doc->pages) :
1039 (pstops_page_t *)cupsArrayNext(doc->pages);
ef416fc2 1040 }
80ca4592 1041 }
1042 }
1043
f301802f 1044 /*
1045 * Restore the old showpage operator as needed...
1046 */
1047
1048 if (doc->use_ESPshowpage)
1049 puts("userdict/showpage/ESPshowpage load put\n");
1050
80ca4592 1051 /*
1052 * Write/copy the trailer...
1053 */
1054
c24d2134
MS
1055 if (!JobCanceled)
1056 linelen = copy_trailer(fp, doc, ppd, number, line, linelen, linesize);
80ca4592 1057}
1058
1059
1060/*
1061 * 'copy_non_dsc()' - Copy a document that does not conform to the DSC...
1062 *
1063 * This function expects "line" to be filled with the %! comment line.
1064 */
1065
1066static void
1067copy_non_dsc(cups_file_t *fp, /* I - File to read from */
1068 pstops_doc_t *doc, /* I - Document info */
1069 ppd_file_t *ppd, /* I - PPD file */
1070 char *line, /* I - Line buffer */
1071 size_t linelen, /* I - Length of initial line */
1072 size_t linesize) /* I - Size of line buffer */
1073{
1074 int copy; /* Current copy */
1075 char buffer[8192]; /* Copy buffer */
1076 int bytes; /* Number of bytes copied */
1077
1078
1079 /*
1080 * First let the user know that they are attempting to print a file
1081 * that may not print correctly...
1082 */
1083
c0e1af83 1084 fputs(_("WARNING: This document does not conform to the Adobe Document "
1085 "Structuring Conventions and may not print correctly!\n"), stderr);
80ca4592 1086
1087 /*
1088 * Then write a standard DSC comment section...
1089 */
1090
1091 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", PageLeft, PageBottom,
1092 PageRight, PageTop);
1093
1094 if (doc->slow_collate && doc->copies > 1)
1095 printf("%%%%Pages: %d\n", doc->copies);
1096 else
1097 puts("%%Pages: 1");
1098
2abf387c 1099 WriteTextComment("For", doc->user);
1100 WriteTextComment("Title", doc->title);
80ca4592 1101
1102 if (doc->copies != 1 && (!doc->collate || !doc->slow_collate))
1103 {
1104 /*
1105 * Tell the document processor the copy and duplex options
1106 * that are required...
1107 */
1108
1109 printf("%%%%Requirements: numcopies(%d)%s%s\n", doc->copies,
1110 doc->collate ? " collate" : "",
1111 Duplex ? " duplex" : "");
1112
1113 /*
1114 * Apple uses RBI comments for various non-PPD options...
1115 */
1116
1117 printf("%%RBINumCopies: %d\n", doc->copies);
1118 }
1119 else
1120 {
1121 /*
1122 * Tell the document processor the duplex option that is required...
1123 */
1124
1125 if (Duplex)
1126 puts("%%Requirements: duplex");
1127
1128 /*
1129 * Apple uses RBI comments for various non-PPD options...
1130 */
1131
1132 puts("%RBINumCopies: 1");
1133 }
1134
1135 puts("%%EndComments");
1136
1137 /*
1138 * Then the prolog...
1139 */
1140
1141 puts("%%BeginProlog");
1142
1143 do_prolog(doc, ppd);
1144
1145 puts("%%EndProlog");
1146
1147 /*
1148 * Then the setup section...
1149 */
1150
1151 puts("%%BeginSetup");
1152
1153 do_setup(doc, ppd);
1154
1155 puts("%%EndSetup");
1156
1157 /*
1158 * Finally, embed a copy of the file inside a %%Page...
1159 */
1160
1161 if (!ppd || !ppd->num_filters)
1162 fprintf(stderr, "PAGE: 1 %d\n", doc->temp ? 1 : doc->copies);
1163
1164 puts("%%Page: 1 1");
1165 puts("%%BeginPageSetup");
1166 ppdEmit(ppd, stdout, PPD_ORDER_PAGE);
1167 puts("%%EndPageSetup");
1168 puts("%%BeginDocument: nondsc");
1169
1170 fwrite(line, linelen, 1, stdout);
1171
1172 if (doc->temp)
1173 cupsFileWrite(doc->temp, line, linelen);
1174
1175 while ((bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0)
1176 {
1177 fwrite(buffer, 1, bytes, stdout);
1178
1179 if (doc->temp)
1180 cupsFileWrite(doc->temp, buffer, bytes);
1181 }
1182
1183 puts("%%EndDocument");
1184
1185 if (doc->use_ESPshowpage)
1186 {
1187 WriteLabels(Orientation);
1188 puts("ESPshowpage");
1189 }
1190
c24d2134 1191 if (doc->temp && !JobCanceled)
80ca4592 1192 {
1193 /*
1194 * Reopen the temporary file for reading...
1195 */
1196
1197 cupsFileClose(doc->temp);
1198
1199 doc->temp = cupsFileOpen(doc->tempfile, "r");
1200
1201 /*
1202 * Make the additional copies as needed...
1203 */
1204
1205 for (copy = 1; copy < doc->copies; copy ++)
1206 {
c24d2134
MS
1207 if (JobCanceled)
1208 break;
1209
80ca4592 1210 if (!ppd || !ppd->num_filters)
1211 fputs("PAGE: 1 1\n", stderr);
1212
1213 printf("%%%%Page: %d %d\n", copy + 1, copy + 1);
1214 puts("%%BeginPageSetup");
1215 ppdEmit(ppd, stdout, PPD_ORDER_PAGE);
1216 puts("%%EndPageSetup");
1217 puts("%%BeginDocument: nondsc");
1218
1219 copy_bytes(doc->temp, 0, 0);
1220
1221 puts("%%EndDocument");
1222
1223 if (doc->use_ESPshowpage)
1224 {
1225 WriteLabels(Orientation);
1226 puts("ESPshowpage");
1227 }
1228 }
1229 }
f301802f 1230
1231 /*
1232 * Restore the old showpage operator as needed...
1233 */
1234
1235 if (doc->use_ESPshowpage)
1236 puts("userdict/showpage/ESPshowpage load put\n");
80ca4592 1237}
1238
1239
1240/*
1241 * 'copy_page()' - Copy a page description...
1242 *
1243 * This function expects "line" to be filled with a %%Page comment line.
1244 * On return, "line" will contain the next line in the file, if any.
1245 */
1246
1247static size_t /* O - Length of next line */
1248copy_page(cups_file_t *fp, /* I - File to read from */
1249 pstops_doc_t *doc, /* I - Document info */
1250 ppd_file_t *ppd, /* I - PPD file */
1251 int number, /* I - Current page number */
1252 char *line, /* I - Line buffer */
1253 size_t linelen, /* I - Length of initial line */
1254 size_t linesize) /* I - Size of line buffer */
1255{
1256 char label[256], /* Page label string */
1257 *ptr; /* Pointer into line */
1258 int level; /* Embedded document level */
1259 pstops_page_t *pageinfo; /* Page information */
1260 int first_page; /* First page on N-up output? */
1261 int bounding_box[4]; /* PageBoundingBox */
1262
1263
1264 /*
1265 * Get the page label for this page...
1266 */
1267
1268 first_page = is_first_page(number);
1269
1270 if (!parse_text(line + 7, &ptr, label, sizeof(label)))
1271 {
c0e1af83 1272 fputs(_("ERROR: Bad %%Page: comment in file!\n"), stderr);
80ca4592 1273 label[0] = '\0';
1274 number = doc->page;
1275 }
1276 else if (strtol(ptr, &ptr, 10) == LONG_MAX || !isspace(*ptr & 255))
1277 {
c0e1af83 1278 fputs(_("ERROR: Bad %%Page: comment in file!\n"), stderr);
80ca4592 1279 number = doc->page;
1280 }
1281
1282 /*
1283 * Create or update the current output page...
1284 */
1285
1286 if (first_page)
1287 pageinfo = add_page(doc, label);
1288 else
1289 pageinfo = (pstops_page_t *)cupsArrayLast(doc->pages);
1290
1291 /*
1292 * Handle first page override...
1293 */
1294
1295 if (doc->ap_input_slot || doc->ap_manual_feed)
1296 {
1297 if (doc->page == 1)
1298 {
1299 /*
1300 * First page/sheet gets AP_FIRSTPAGE_* options...
1301 */
1302
1303 pageinfo->num_options = cupsAddOption("InputSlot", doc->ap_input_slot,
1304 pageinfo->num_options,
1305 &(pageinfo->options));
c0e1af83 1306 pageinfo->num_options = cupsAddOption("ManualFeed",
1307 doc->ap_input_slot ? "False" :
1308 doc->ap_manual_feed,
80ca4592 1309 pageinfo->num_options,
1310 &(pageinfo->options));
1311 }
1312 else if (doc->page == (Duplex + 2))
1313 {
1314 /*
1315 * Second page/sheet gets default options...
1316 */
1317
1318 pageinfo->num_options = cupsAddOption("InputSlot", doc->input_slot,
1319 pageinfo->num_options,
1320 &(pageinfo->options));
c0e1af83 1321 pageinfo->num_options = cupsAddOption("ManualFeed",
1322 doc->input_slot ? "False" :
1323 doc->manual_feed,
80ca4592 1324 pageinfo->num_options,
1325 &(pageinfo->options));
1326 }
1327 }
1328
1329 /*
1330 * Scan comments until we see something other than %%Page*: or
1331 * %%Include*...
1332 */
1333
1334 memcpy(bounding_box, doc->bounding_box, sizeof(bounding_box));
1335
1336 while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0)
1337 {
1338 if (!strncmp(line, "%%PageBoundingBox:", 18))
1339 {
1340 /*
1341 * %%PageBoundingBox: llx lly urx ury
1342 */
1343
1344 if (sscanf(line + 18, "%d%d%d%d", bounding_box + 0,
1345 bounding_box + 1, bounding_box + 2,
1346 bounding_box + 3) != 4)
1347 {
c0e1af83 1348 fputs(_("ERROR: Bad %%PageBoundingBox: comment in file!\n"), stderr);
80ca4592 1349 memcpy(bounding_box, doc->bounding_box,
1350 sizeof(bounding_box));
1351 }
b86bc4cf 1352 else if (doc->number_up == 1 && !doc->fitplot && Orientation)
411affcf 1353 {
1354 int temp_bbox[4]; /* Temporary bounding box */
1355
1356
b86bc4cf 1357 memcpy(temp_bbox, bounding_box, sizeof(temp_bbox));
1358
1359 fprintf(stderr, "DEBUG: Orientation = %d\n", Orientation);
1360 fprintf(stderr, "DEBUG: original bounding_box = [ %d %d %d %d ]\n",
1361 bounding_box[0], bounding_box[1],
1362 bounding_box[2], bounding_box[3]);
1363 fprintf(stderr, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
1364 PageWidth, PageLength);
1365
411affcf 1366 switch (Orientation)
1367 {
411affcf 1368 case 1 : /* Landscape */
b86bc4cf 1369 bounding_box[0] = PageLength - temp_bbox[3];
1370 bounding_box[1] = temp_bbox[0];
1371 bounding_box[2] = PageLength - temp_bbox[1];
1372 bounding_box[3] = temp_bbox[2];
411affcf 1373 break;
1374
1375 case 2 : /* Reverse Portrait */
b86bc4cf 1376 bounding_box[0] = PageWidth - temp_bbox[2];
1377 bounding_box[1] = PageLength - temp_bbox[3];
1378 bounding_box[2] = PageWidth - temp_bbox[0];
1379 bounding_box[3] = PageLength - temp_bbox[1];
411affcf 1380 break;
1381
1382 case 3 : /* Reverse Landscape */
b86bc4cf 1383 bounding_box[0] = temp_bbox[1];
1384 bounding_box[1] = PageWidth - temp_bbox[2];
1385 bounding_box[2] = temp_bbox[3];
1386 bounding_box[3] = PageWidth - temp_bbox[0];
411affcf 1387 break;
1388 }
b86bc4cf 1389
1390 fprintf(stderr, "DEBUG: updated bounding_box = [ %d %d %d %d ]\n",
1391 bounding_box[0], bounding_box[1],
1392 bounding_box[2], bounding_box[3]);
411affcf 1393 }
80ca4592 1394 }
1395#if 0
1396 else if (!strncmp(line, "%%PageCustomColors:", 19) ||
1397 !strncmp(line, "%%PageMedia:", 12) ||
1398 !strncmp(line, "%%PageOrientation:", 18) ||
1399 !strncmp(line, "%%PageProcessColors:", 20) ||
1400 !strncmp(line, "%%PageRequirements:", 18) ||
1401 !strncmp(line, "%%PageResources:", 16))
1402 {
1403 /*
1404 * Copy literal...
1405 */
1406 }
1407#endif /* 0 */
1408 else if (!strncmp(line, "%%PageCustomColors:", 19))
1409 {
1410 /*
1411 * %%PageCustomColors: ...
1412 */
1413 }
1414 else if (!strncmp(line, "%%PageMedia:", 12))
1415 {
1416 /*
1417 * %%PageMedia: ...
1418 */
1419 }
1420 else if (!strncmp(line, "%%PageOrientation:", 18))
1421 {
1422 /*
1423 * %%PageOrientation: ...
1424 */
1425 }
1426 else if (!strncmp(line, "%%PageProcessColors:", 20))
1427 {
1428 /*
1429 * %%PageProcessColors: ...
1430 */
1431 }
1432 else if (!strncmp(line, "%%PageRequirements:", 18))
1433 {
1434 /*
1435 * %%PageRequirements: ...
1436 */
1437 }
1438 else if (!strncmp(line, "%%PageResources:", 16))
1439 {
1440 /*
1441 * %%PageResources: ...
1442 */
1443 }
1444 else if (!strncmp(line, "%%IncludeFeature:", 17))
1445 {
1446 /*
1447 * %%IncludeFeature: *MainKeyword OptionKeyword
1448 */
1449
1450 if (doc->number_up == 1 &&!doc->fitplot)
1451 pageinfo->num_options = include_feature(ppd, line,
1452 pageinfo->num_options,
1453 &(pageinfo->options));
1454 }
1455 else if (strncmp(line, "%%Include", 9))
1456 break;
1457 }
1458
1459 if (doc->number_up == 1)
1460 {
1461 /*
1462 * Update the document's composite and page bounding box...
1463 */
1464
1465 memcpy(pageinfo->bounding_box, bounding_box,
1466 sizeof(pageinfo->bounding_box));
1467
1468 if (bounding_box[0] < doc->new_bounding_box[0])
1469 doc->new_bounding_box[0] = bounding_box[0];
1470 if (bounding_box[1] < doc->new_bounding_box[1])
1471 doc->new_bounding_box[1] = bounding_box[1];
1472 if (bounding_box[2] > doc->new_bounding_box[2])
1473 doc->new_bounding_box[2] = bounding_box[2];
1474 if (bounding_box[3] > doc->new_bounding_box[3])
1475 doc->new_bounding_box[3] = bounding_box[3];
1476 }
1477
1478 /*
1479 * Output the page header as needed...
1480 */
1481
1482 if (!doc->slow_order && first_page)
1483 {
1484 if (!ppd || !ppd->num_filters)
1485 fprintf(stderr, "PAGE: %d %d\n", doc->page,
1486 doc->slow_collate ? 1 : doc->copies);
1487
1488 if (doc->number_up > 1)
1489 {
1490 printf("%%%%Page: (%d) %d\n", doc->page, doc->page);
1491 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1492 PageLeft, PageBottom, PageRight, PageTop);
1493 }
1494 else
1495 {
1496 printf("%%%%Page: %s %d\n", pageinfo->label, doc->page);
1497 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1498 pageinfo->bounding_box[0], pageinfo->bounding_box[1],
1499 pageinfo->bounding_box[2], pageinfo->bounding_box[3]);
1500 }
1501 }
1502
1503 /*
1504 * Copy any page setup commands...
1505 */
1506
e53920b9 1507 if (first_page)
80ca4592 1508 {
ed486911 1509 char *page_setup; /* PageSetup commands to send */
1510
1511
e53920b9 1512 doc_puts(doc, "%%BeginPageSetup\n");
80ca4592 1513
e53920b9 1514 if (pageinfo->num_options > 0)
80ca4592 1515 {
e53920b9 1516 int i; /* Looping var */
1517 ppd_option_t *option; /* PPD option */
1518 int min_order; /* Minimum OrderDependency value */
1519 char *doc_setup, /* DocumentSetup commands to send */
1520 *any_setup; /* AnySetup commands to send */
80ca4592 1521
80ca4592 1522
e53920b9 1523 /*
1524 * Yes, figure out the minimum OrderDependency value...
1525 */
80ca4592 1526
e53920b9 1527 if ((option = ppdFindOption(ppd, "PageRegion")) != NULL)
1528 min_order = option->order;
1529 else
1530 min_order = 999.0f;
80ca4592 1531
e53920b9 1532 for (i = 0; i < pageinfo->num_options; i ++)
1533 if ((option = ppdFindOption(ppd, pageinfo->options[i].name)) != NULL &&
1534 option->order < min_order)
1535 min_order = option->order;
80ca4592 1536
e53920b9 1537 /*
1538 * Mark and extract them...
1539 */
80ca4592 1540
e53920b9 1541 cupsMarkOptions(ppd, pageinfo->num_options, pageinfo->options);
80ca4592 1542
e53920b9 1543 doc_setup = ppdEmitString(ppd, PPD_ORDER_DOCUMENT, min_order);
1544 any_setup = ppdEmitString(ppd, PPD_ORDER_ANY, min_order);
80ca4592 1545
e53920b9 1546 /*
1547 * Then send them out...
1548 */
80ca4592 1549
e53920b9 1550 if (doc_setup)
ed486911 1551 {
e53920b9 1552 doc_puts(doc, doc_setup);
ed486911 1553 free(doc_setup);
1554 }
80ca4592 1555
e53920b9 1556 if (any_setup)
ed486911 1557 {
e53920b9 1558 doc_puts(doc, any_setup);
ed486911 1559 free(any_setup);
1560 }
1561 }
80ca4592 1562
ed486911 1563 /*
1564 * Output commands for the current page...
1565 */
80ca4592 1566
ed486911 1567 page_setup = ppdEmitString(ppd, PPD_ORDER_PAGE, 0);
80ca4592 1568
ed486911 1569 if (page_setup)
1570 {
1571 doc_puts(doc, page_setup);
1572 free(page_setup);
e53920b9 1573 }
1574 }
80ca4592 1575
e53920b9 1576 /*
1577 * Prep for the start of the page description...
1578 */
80ca4592 1579
e53920b9 1580 start_nup(doc, number, 1, bounding_box);
80ca4592 1581
e53920b9 1582 /*
1583 * Copy page setup commands as needed...
1584 */
80ca4592 1585
e53920b9 1586 if (!strncmp(line, "%%BeginPageSetup", 16))
1587 {
1588 int feature = 0; /* In a Begin/EndFeature block? */
80ca4592 1589
80ca4592 1590
e53920b9 1591 while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0)
1592 {
1593 if (!strncmp(line, "%%EndPageSetup", 14))
1594 break;
1595 else if (!strncmp(line, "%%BeginFeature:", 15))
1596 {
1597 feature = 1;
1598
1599 if (doc->number_up > 1 || doc->fitplot)
1600 continue;
1601 }
f42414bf 1602 else if (!strncmp(line, "%%EndFeature", 12))
e53920b9 1603 {
1604 feature = 0;
1605
1606 if (doc->number_up > 1 || doc->fitplot)
1607 continue;
1608 }
1609 else if (!strncmp(line, "%%Include", 9))
1610 continue;
1611
1612 if (!feature || (doc->number_up == 1 && !doc->fitplot))
1613 doc_write(doc, line, linelen);
1614 }
1615
1616 /*
1617 * Skip %%EndPageSetup...
1618 */
1619
1620 if (linelen > 0)
1621 linelen = cupsFileGetLine(fp, line, linesize);
80ca4592 1622 }
1623
1624 /*
e53920b9 1625 * Finish the PageSetup section as needed...
80ca4592 1626 */
1627
e53920b9 1628 if (first_page)
1629 doc_puts(doc, "%%EndPageSetup\n");
80ca4592 1630
1631 /*
1632 * Read the rest of the page description...
1633 */
1634
1635 level = 0;
1636
1637 do
1638 {
1639 if (level == 0 &&
1640 (!strncmp(line, "%%Page:", 7) ||
d6ae789d 1641 !strncmp(line, "%%Trailer", 9) ||
80ca4592 1642 !strncmp(line, "%%EOF", 5)))
1643 break;
1644 else if (!strncmp(line, "%%BeginDocument", 15) ||
1645 !strncmp(line, "%ADO_BeginApplication", 21))
1646 {
1647 doc_write(doc, line, linelen);
1648
1649 level ++;
1650 }
1651 else if ((!strncmp(line, "%%EndDocument", 13) ||
1652 !strncmp(line, "%ADO_EndApplication", 19)) && level > 0)
1653 {
1654 doc_write(doc, line, linelen);
1655
1656 level --;
1657 }
1658 else if (!strncmp(line, "%%BeginBinary:", 14) ||
1659 (!strncmp(line, "%%BeginData:", 12) &&
1660 !strstr(line, "ASCII") && !strstr(line, "Hex")))
1661 {
1662 /*
1663 * Copy binary data...
1664 */
ef416fc2 1665
80ca4592 1666 int bytes; /* Bytes of data */
ef416fc2 1667
ef416fc2 1668
80ca4592 1669 doc_write(doc, line, linelen);
ef416fc2 1670
80ca4592 1671 bytes = atoi(strchr(line, ':') + 1);
1672
1673 while (bytes > 0)
1674 {
1675 if (bytes > linesize)
1676 linelen = cupsFileRead(fp, line, linesize);
1677 else
1678 linelen = cupsFileRead(fp, line, bytes);
ef416fc2 1679
80ca4592 1680 if (linelen < 1)
ef416fc2 1681 {
80ca4592 1682 line[0] = '\0';
1683 perror("ERROR: Early end-of-file while reading binary data");
1684 return (0);
1685 }
ef416fc2 1686
80ca4592 1687 doc_write(doc, line, linelen);
ef416fc2 1688
80ca4592 1689 bytes -= linelen;
ef416fc2 1690 }
ef416fc2 1691 }
80ca4592 1692 else
1693 doc_write(doc, line, linelen);
1694 }
1695 while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0);
ef416fc2 1696
80ca4592 1697 /*
1698 * Finish up this page and return...
1699 */
ef416fc2 1700
80ca4592 1701 end_nup(doc, number);
ef416fc2 1702
80ca4592 1703 pageinfo->length = cupsFileTell(doc->temp) - pageinfo->offset;
ef416fc2 1704
80ca4592 1705 return (linelen);
1706}
ef416fc2 1707
ef416fc2 1708
80ca4592 1709/*
1710 * 'copy_prolog()' - Copy the document prolog section...
1711 *
1712 * This function expects "line" to be filled with a %%BeginProlog comment line.
1713 * On return, "line" will contain the next line in the file, if any.
1714 */
ef416fc2 1715
80ca4592 1716static size_t /* O - Length of next line */
1717copy_prolog(cups_file_t *fp, /* I - File to read from */
1718 pstops_doc_t *doc, /* I - Document info */
1719 ppd_file_t *ppd, /* I - PPD file */
1720 char *line, /* I - Line buffer */
1721 size_t linelen, /* I - Length of initial line */
1722 size_t linesize) /* I - Size of line buffer */
1723{
1724 while (strncmp(line, "%%BeginProlog", 13))
1725 {
1726 if (!strncmp(line, "%%BeginSetup", 12) || !strncmp(line, "%%Page:", 7))
1727 break;
ef416fc2 1728
b86bc4cf 1729 doc_write(doc, line, linelen);
ef416fc2 1730
80ca4592 1731 if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0)
1732 break;
1733 }
ef416fc2 1734
b86bc4cf 1735 doc_puts(doc, "%%BeginProlog\n");
ef416fc2 1736
80ca4592 1737 do_prolog(doc, ppd);
ef416fc2 1738
80ca4592 1739 if (!strncmp(line, "%%BeginProlog", 13))
1740 {
1741 while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0)
ef416fc2 1742 {
80ca4592 1743 if (!strncmp(line, "%%EndProlog", 11) ||
1744 !strncmp(line, "%%BeginSetup", 12) ||
1745 !strncmp(line, "%%Page:", 7))
1746 break;
ef416fc2 1747
b86bc4cf 1748 doc_write(doc, line, linelen);
ef416fc2 1749 }
80ca4592 1750
1751 if (!strncmp(line, "%%EndProlog", 11))
1752 linelen = cupsFileGetLine(fp, line, linesize);
ef416fc2 1753 else
c0e1af83 1754 fputs(_("ERROR: Missing %%EndProlog!\n"), stderr);
80ca4592 1755 }
ef416fc2 1756
b86bc4cf 1757 doc_puts(doc, "%%EndProlog\n");
ef416fc2 1758
80ca4592 1759 return (linelen);
1760}
ef416fc2 1761
bd7854cb 1762
80ca4592 1763/*
1764 * 'copy_setup()' - Copy the document setup section...
1765 *
1766 * This function expects "line" to be filled with a %%BeginSetup comment line.
1767 * On return, "line" will contain the next line in the file, if any.
1768 */
bd7854cb 1769
80ca4592 1770static size_t /* O - Length of next line */
1771copy_setup(cups_file_t *fp, /* I - File to read from */
1772 pstops_doc_t *doc, /* I - Document info */
1773 ppd_file_t *ppd, /* I - PPD file */
1774 char *line, /* I - Line buffer */
1775 size_t linelen, /* I - Length of initial line */
1776 size_t linesize) /* I - Size of line buffer */
1777{
1778 while (strncmp(line, "%%BeginSetup", 12))
1779 {
1780 if (!strncmp(line, "%%Page:", 7))
1781 break;
bd7854cb 1782
b86bc4cf 1783 doc_write(doc, line, linelen);
bd7854cb 1784
80ca4592 1785 if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0)
1786 break;
1787 }
1788
b86bc4cf 1789 doc_puts(doc, "%%BeginSetup\n");
d09495fa 1790
1791 do_setup(doc, ppd);
1792
80ca4592 1793 if (!strncmp(line, "%%BeginSetup", 12))
1794 {
1795 while (strncmp(line, "%%EndSetup", 10))
1796 {
1797 if (!strncmp(line, "%%Page:", 7))
ef416fc2 1798 break;
80ca4592 1799 else if (!strncmp(line, "%%IncludeFeature:", 17))
ef416fc2 1800 {
80ca4592 1801 /*
1802 * %%IncludeFeature: *MainKeyword OptionKeyword
1803 */
ef416fc2 1804
80ca4592 1805 if (doc->number_up == 1 && !doc->fitplot)
1806 doc->num_options = include_feature(ppd, line, doc->num_options,
1807 &(doc->options));
1808 }
d09495fa 1809 else if (strncmp(line, "%%BeginSetup", 12))
b86bc4cf 1810 doc_write(doc, line, linelen);
ef416fc2 1811
80ca4592 1812 if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0)
1813 break;
1814 }
bd7854cb 1815
80ca4592 1816 if (!strncmp(line, "%%EndSetup", 10))
1817 linelen = cupsFileGetLine(fp, line, linesize);
1818 else
c0e1af83 1819 fputs(_("ERROR: Missing %%EndSetup!\n"), stderr);
80ca4592 1820 }
ef416fc2 1821
b86bc4cf 1822 doc_puts(doc, "%%EndSetup\n");
ef416fc2 1823
80ca4592 1824 return (linelen);
1825}
ef416fc2 1826
ef416fc2 1827
80ca4592 1828/*
1829 * 'copy_trailer()' - Copy the document trailer...
1830 *
1831 * This function expects "line" to be filled with a %%Trailer comment line.
1832 * On return, "line" will contain the next line in the file, if any.
1833 */
ef416fc2 1834
80ca4592 1835static size_t /* O - Length of next line */
1836copy_trailer(cups_file_t *fp, /* I - File to read from */
1837 pstops_doc_t *doc, /* I - Document info */
1838 ppd_file_t *ppd, /* I - PPD file */
1839 int number, /* I - Number of pages */
1840 char *line, /* I - Line buffer */
1841 size_t linelen, /* I - Length of initial line */
1842 size_t linesize) /* I - Size of line buffer */
1843{
1844 /*
1845 * Write the trailer comments...
1846 */
ef416fc2 1847
80ca4592 1848 puts("%%Trailer");
ef416fc2 1849
80ca4592 1850 while (linelen > 0)
1851 {
1852 if (!strncmp(line, "%%EOF", 5))
1853 break;
1854 else if (strncmp(line, "%%Trailer", 9) &&
1855 strncmp(line, "%%Pages:", 8) &&
1856 strncmp(line, "%%BoundingBox:", 14))
1857 fwrite(line, 1, linelen, stdout);
ef416fc2 1858
80ca4592 1859 linelen = cupsFileGetLine(fp, line, linesize);
1860 }
ef416fc2 1861
80ca4592 1862 fprintf(stderr, "DEBUG: Wrote %d pages...\n", number);
ef416fc2 1863
80ca4592 1864 printf("%%%%Pages: %d\n", number);
1865 if (doc->number_up > 1 || doc->fitplot)
1866 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
1867 PageLeft, PageBottom, PageRight, PageTop);
1868 else
1869 printf("%%%%BoundingBox: %d %d %d %d\n",
1870 doc->new_bounding_box[0], doc->new_bounding_box[1],
1871 doc->new_bounding_box[2], doc->new_bounding_box[3]);
ef416fc2 1872
80ca4592 1873 return (linelen);
1874}
ef416fc2 1875
ef416fc2 1876
80ca4592 1877/*
1878 * 'do_prolog()' - Send the necessary document prolog commands...
1879 */
ef416fc2 1880
80ca4592 1881static void
1882do_prolog(pstops_doc_t *doc, /* I - Document information */
1883 ppd_file_t *ppd) /* I - PPD file */
1884{
b86bc4cf 1885 char *ps; /* PS commands */
1886
1887
80ca4592 1888 /*
1889 * Send the document prolog commands...
1890 */
ef416fc2 1891
80ca4592 1892 if (ppd && ppd->patches)
1893 {
b86bc4cf 1894 doc_puts(doc, "%%BeginFeature: *JobPatchFile 1\n");
1895 doc_puts(doc, ppd->patches);
1896 doc_puts(doc, "\n%%EndFeature\n");
80ca4592 1897 }
bd7854cb 1898
b86bc4cf 1899 if ((ps = ppdEmitString(ppd, PPD_ORDER_PROLOG, 0.0)) != NULL)
1900 {
1901 doc_puts(doc, ps);
1902 free(ps);
1903 }
bd7854cb 1904
80ca4592 1905 /*
1906 * Define ESPshowpage here so that applications that define their
1907 * own procedure to do a showpage pick it up...
1908 */
ef416fc2 1909
80ca4592 1910 if (doc->use_ESPshowpage)
b86bc4cf 1911 doc_puts(doc, "userdict/ESPshowpage/showpage load put\n"
1912 "userdict/showpage{}put\n");
80ca4592 1913}
ef416fc2 1914
ef416fc2 1915
80ca4592 1916/*
1917 * 'do_setup()' - Send the necessary document setup commands...
1918 */
ef416fc2 1919
80ca4592 1920static void
1921do_setup(pstops_doc_t *doc, /* I - Document information */
1922 ppd_file_t *ppd) /* I - PPD file */
1923{
b86bc4cf 1924 char *ps; /* PS commands */
1925
1926
f301802f 1927 /*
1928 * Disable CTRL-D so that embedded files don't cause printing
1929 * errors...
1930 */
1931
b86bc4cf 1932 doc_puts(doc, "% Disable CTRL-D as an end-of-file marker...\n");
1933 doc_puts(doc, "userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put\n");
f301802f 1934
80ca4592 1935 /*
1936 * Mark any options from %%IncludeFeature: comments...
1937 */
ef416fc2 1938
80ca4592 1939 cupsMarkOptions(ppd, doc->num_options, doc->options);
ef416fc2 1940
80ca4592 1941 /*
1942 * Send all the printer-specific setup commands...
1943 */
ef416fc2 1944
b86bc4cf 1945 if ((ps = ppdEmitString(ppd, PPD_ORDER_DOCUMENT, 0.0)) != NULL)
1946 {
1947 doc_puts(doc, ps);
1948 free(ps);
1949 }
1950
1951 if ((ps = ppdEmitString(ppd, PPD_ORDER_ANY, 0.0)) != NULL)
1952 {
1953 doc_puts(doc, ps);
1954 free(ps);
1955 }
ef416fc2 1956
80ca4592 1957 /*
1958 * Set the number of copies for the job...
1959 */
ef416fc2 1960
80ca4592 1961 if (doc->copies != 1 && (!doc->collate || !doc->slow_collate))
1962 {
b86bc4cf 1963 doc_printf(doc, "%%RBIBeginNonPPDFeature: *NumCopies %d\n", doc->copies);
1964 doc_printf(doc,
1965 "%d/languagelevel where{pop languagelevel 2 ge}{false}ifelse\n"
1966 "{1 dict begin/NumCopies exch def currentdict end "
1967 "setpagedevice}\n"
1968 "{userdict/#copies 3 -1 roll put}ifelse\n", doc->copies);
1969 doc_puts(doc, "%RBIEndNonPPDFeature\n");
80ca4592 1970 }
ef416fc2 1971
80ca4592 1972 /*
1973 * If we are doing N-up printing, disable setpagedevice...
1974 */
ef416fc2 1975
80ca4592 1976 if (doc->number_up > 1)
b86bc4cf 1977 doc_puts(doc, "userdict/setpagedevice{pop}bind put\n");
ef416fc2 1978
80ca4592 1979 /*
1980 * Changes to the transfer function must be made AFTER any
1981 * setpagedevice code...
1982 */
ef416fc2 1983
80ca4592 1984 if (doc->gamma != 1.0f || doc->brightness != 1.0f)
b86bc4cf 1985 doc_printf(doc, "{ neg 1 add dup 0 lt { pop 1 } { %.3f exp neg 1 add } "
1986 "ifelse %.3f mul } bind settransfer\n",
1987 doc->gamma, doc->brightness);
ef416fc2 1988
80ca4592 1989 /*
1990 * Make sure we have rectclip and rectstroke procedures of some sort...
1991 */
ef416fc2 1992
b86bc4cf 1993 doc_puts(doc,
1994 "% x y w h ESPrc - Clip to a rectangle.\n"
1995 "userdict/ESPrc/rectclip where{pop/rectclip load}\n"
1996 "{{newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
1997 "neg 0 rlineto closepath clip newpath}bind}ifelse put\n");
1998
1999 doc_puts(doc,
2000 "% x y w h ESPrf - Fill a rectangle.\n"
2001 "userdict/ESPrf/rectfill where{pop/rectfill load}\n"
2002 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
2003 "neg 0 rlineto closepath fill grestore}bind}ifelse put\n");
2004
2005 doc_puts(doc,
2006 "% x y w h ESPrs - Stroke a rectangle.\n"
2007 "userdict/ESPrs/rectstroke where{pop/rectstroke load}\n"
2008 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
2009 "neg 0 rlineto closepath stroke grestore}bind}ifelse put\n");
ef416fc2 2010
80ca4592 2011 /*
2012 * Write the page and label prologs...
2013 */
ef416fc2 2014
80ca4592 2015 if (doc->number_up == 2 || doc->number_up == 6)
2016 {
ef416fc2 2017 /*
80ca4592 2018 * For 2- and 6-up output, rotate the labels to match the orientation
2019 * of the pages...
ef416fc2 2020 */
2021
80ca4592 2022 if (Orientation & 1)
b86bc4cf 2023 write_label_prolog(doc, doc->page_label, PageBottom,
2024 PageWidth - PageLength + PageTop, PageLength);
80ca4592 2025 else
b86bc4cf 2026 write_label_prolog(doc, doc->page_label, PageLeft, PageRight,
2027 PageLength);
80ca4592 2028 }
2029 else
b86bc4cf 2030 write_label_prolog(doc, doc->page_label, PageBottom, PageTop, PageWidth);
80ca4592 2031}
2032
ef416fc2 2033
80ca4592 2034/*
2035 * 'doc_printf()' - Send a formatted string to stdout and/or the temp file.
2036 *
2037 * This function should be used for all page-level output that is affected
2038 * by ordering, collation, etc.
2039 */
ef416fc2 2040
80ca4592 2041static void
2042doc_printf(pstops_doc_t *doc, /* I - Document information */
2043 const char *format, /* I - Printf-style format string */
2044 ...) /* I - Additional arguments as needed */
2045{
2046 va_list ap; /* Pointer to arguments */
2047 char buffer[1024]; /* Output buffer */
2048 size_t bytes; /* Number of bytes to write */
ef416fc2 2049
ef416fc2 2050
80ca4592 2051 va_start(ap, format);
2052 bytes = vsnprintf(buffer, sizeof(buffer), format, ap);
2053 va_end(ap);
2054
2055 if (bytes > sizeof(buffer))
2056 {
2057 fprintf(stderr,
c0e1af83 2058 _("ERROR: doc_printf overflow (%d bytes) detected, aborting!\n"),
80ca4592 2059 (int)bytes);
2060 exit(1);
2061 }
2062
2063 doc_write(doc, buffer, bytes);
2064}
2065
2066
2067/*
2068 * 'doc_puts()' - Send a nul-terminated string to stdout and/or the temp file.
2069 *
2070 * This function should be used for all page-level output that is affected
2071 * by ordering, collation, etc.
2072 */
2073
2074static void
2075doc_puts(pstops_doc_t *doc, /* I - Document information */
2076 const char *s) /* I - String to send */
2077{
2078 doc_write(doc, s, strlen(s));
2079}
2080
ef416fc2 2081
80ca4592 2082/*
2083 * 'doc_write()' - Send data to stdout and/or the temp file.
2084 */
ef416fc2 2085
80ca4592 2086static void
2087doc_write(pstops_doc_t *doc, /* I - Document information */
2088 const char *s, /* I - Data to send */
2089 size_t len) /* I - Number of bytes to send */
2090{
2091 if (!doc->slow_order)
2092 fwrite(s, 1, len, stdout);
ef416fc2 2093
80ca4592 2094 if (doc->temp)
2095 cupsFileWrite(doc->temp, s, len);
2096}
ef416fc2 2097
ef416fc2 2098
80ca4592 2099/*
2100 * 'end_nup()' - End processing for N-up printing...
2101 */
ef416fc2 2102
80ca4592 2103static void
2104end_nup(pstops_doc_t *doc, /* I - Document information */
2105 int number) /* I - Page number */
2106{
2abf387c 2107 if (doc->number_up > 1)
ed486911 2108 doc_puts(doc, "userdict/ESPsave get restore\n");
ef416fc2 2109
80ca4592 2110 switch (doc->number_up)
2111 {
2112 case 1 :
2113 if (doc->use_ESPshowpage)
2114 {
ed486911 2115 write_labels(doc, Orientation);
2116 doc_puts(doc, "ESPshowpage\n");
80ca4592 2117 }
2118 break;
ef416fc2 2119
80ca4592 2120 case 2 :
2121 case 6 :
2122 if (is_last_page(number) && doc->use_ESPshowpage)
2123 {
2124 if (Orientation & 1)
2125 {
2126 /*
2127 * Rotate the labels back to portrait...
2128 */
ef416fc2 2129
ed486911 2130 write_labels(doc, Orientation - 1);
80ca4592 2131 }
2132 else if (Orientation == 0)
2133 {
2134 /*
2135 * Rotate the labels to landscape...
2136 */
ef416fc2 2137
ed486911 2138 write_labels(doc, doc->normal_landscape ? 1 : 3);
80ca4592 2139 }
2140 else
2141 {
2142 /*
2143 * Rotate the labels to landscape...
2144 */
ef416fc2 2145
ed486911 2146 write_labels(doc, doc->normal_landscape ? 3 : 1);
80ca4592 2147 }
ef416fc2 2148
ed486911 2149 doc_puts(doc, "ESPshowpage\n");
80ca4592 2150 }
2151 break;
ef416fc2 2152
80ca4592 2153 default :
2154 if (is_last_page(number) && doc->use_ESPshowpage)
ef416fc2 2155 {
ed486911 2156 write_labels(doc, Orientation);
2157 doc_puts(doc, "ESPshowpage\n");
ef416fc2 2158 }
80ca4592 2159 break;
ef416fc2 2160 }
2161
80ca4592 2162 fflush(stdout);
2163}
2164
2165
2166/*
2167 * 'include_feature()' - Include a printer option/feature command.
2168 */
2169
2170static int /* O - New number of options */
2171include_feature(
2172 ppd_file_t *ppd, /* I - PPD file */
2173 const char *line, /* I - DSC line */
2174 int num_options, /* I - Number of options */
2175 cups_option_t **options) /* IO - Options */
2176{
2177 char name[255], /* Option name */
2178 value[255]; /* Option value */
2179 ppd_option_t *option; /* Option in file */
2180 ppd_choice_t *choice; /* Choice */
ef416fc2 2181
ef416fc2 2182
2183 /*
80ca4592 2184 * Get the "%%IncludeFeature: *Keyword OptionKeyword" values...
ef416fc2 2185 */
2186
80ca4592 2187 if (sscanf(line + 17, "%254s%254s", name, value) != 2)
bd7854cb 2188 {
c0e1af83 2189 fputs(_("ERROR: Bad %%IncludeFeature: comment!\n"), stderr);
80ca4592 2190 return (num_options);
bd7854cb 2191 }
ef416fc2 2192
2193 /*
80ca4592 2194 * Find the option and choice...
ef416fc2 2195 */
2196
80ca4592 2197 if ((option = ppdFindOption(ppd, name + 1)) == NULL)
ef416fc2 2198 {
c0e1af83 2199 fprintf(stderr, _("WARNING: Unknown option \"%s\"!\n"), name + 1);
80ca4592 2200 return (num_options);
ef416fc2 2201 }
2202
80ca4592 2203 if (option->section == PPD_ORDER_EXIT ||
2204 option->section == PPD_ORDER_JCL)
2205 {
c0e1af83 2206 fprintf(stderr, _("WARNING: Option \"%s\" cannot be included via "
2207 "IncludeFeature!\n"), name + 1);
80ca4592 2208 return (num_options);
2209 }
ef416fc2 2210
80ca4592 2211 if ((choice = ppdFindChoice(option, value)) == NULL)
2212 {
c0e1af83 2213 fprintf(stderr, _("WARNING: Unknown choice \"%s\" for option \"%s\"!\n"),
80ca4592 2214 value, name + 1);
2215 return (num_options);
2216 }
ef416fc2 2217
80ca4592 2218 /*
2219 * Add the option to the option array and return...
2220 */
2221
2222 return (cupsAddOption(name + 1, value, num_options, options));
ef416fc2 2223}
2224
2225
2226/*
80ca4592 2227 * 'parse_text()' - Parse a text value in a comment...
2228 *
2229 * This function parses a DSC text value as defined on page 36 of the
2230 * DSC specification. Text values are either surrounded by parenthesis
2231 * or whitespace-delimited.
2232 *
2233 * The value returned is the literal characters for the entire text
2234 * string, including any parenthesis and escape characters.
ef416fc2 2235 */
2236
80ca4592 2237static char * /* O - Value or NULL on error */
2238parse_text(const char *start, /* I - Start of text value */
2239 char **end, /* O - End of text value */
2240 char *buffer, /* I - Buffer */
2241 size_t bufsize) /* I - Size of buffer */
ef416fc2 2242{
80ca4592 2243 char *bufptr, /* Pointer in buffer */
2244 *bufend; /* End of buffer */
2245 int level; /* Parenthesis level */
ef416fc2 2246
2247
80ca4592 2248 /*
2249 * Skip leading whitespace...
2250 */
ef416fc2 2251
80ca4592 2252 while (isspace(*start & 255))
2253 start ++;
2254
2255 /*
2256 * Then copy the value...
2257 */
ef416fc2 2258
80ca4592 2259 level = 0;
2260 bufptr = buffer;
2261 bufend = buffer + bufsize - 1;
ef416fc2 2262
80ca4592 2263 while (bufptr < bufend)
ef416fc2 2264 {
80ca4592 2265 if (isspace(*start & 255) && !level)
2266 break;
ef416fc2 2267
80ca4592 2268 *bufptr++ = *start;
2269
2270 if (*start == '(')
2271 level ++;
2272 else if (*start == ')')
2273 {
2274 if (!level)
ef416fc2 2275 {
80ca4592 2276 start ++;
2277 break;
ef416fc2 2278 }
2279 else
80ca4592 2280 level --;
ef416fc2 2281 }
80ca4592 2282 else if (*start == '\\')
2283 {
2284 /*
2285 * Copy escaped character...
2286 */
ef416fc2 2287
80ca4592 2288 int i; /* Looping var */
ef416fc2 2289
80ca4592 2290
2291 for (i = 1;
2292 i <= 3 && isdigit(start[i] & 255) && bufptr < bufend;
2293 *bufptr++ = start[i], i ++);
2294 }
2295
2296 start ++;
ef416fc2 2297 }
2298
80ca4592 2299 *bufptr = '\0';
2300
2301 /*
2302 * Return the value and new pointer into the line...
2303 */
2304
2305 if (end)
2306 *end = (char *)start;
2307
2308 if (bufptr == bufend)
2309 return (NULL);
2310 else
2311 return (buffer);
ef416fc2 2312}
2313
2314
bd7854cb 2315/*
80ca4592 2316 * 'set_pstops_options()' - Set pstops options...
bd7854cb 2317 */
2318
80ca4592 2319static void
2320set_pstops_options(
2321 pstops_doc_t *doc, /* I - Document information */
2322 ppd_file_t *ppd, /* I - PPD file */
2323 char *argv[], /* I - Command-line arguments */
2324 int num_options, /* I - Number of options */
2325 cups_option_t *options) /* I - Options */
bd7854cb 2326{
80ca4592 2327 const char *val; /* Option value */
2328 int intval; /* Integer option value */
2329 ppd_attr_t *attr; /* PPD attribute */
2330 ppd_option_t *option; /* PPD option */
2331 ppd_choice_t *choice; /* PPD choice */
bd7854cb 2332
bd7854cb 2333
80ca4592 2334 /*
2335 * Initialize document information structure...
2336 */
bd7854cb 2337
80ca4592 2338 memset(doc, 0, sizeof(pstops_doc_t));
bd7854cb 2339
80ca4592 2340 doc->job_id = atoi(argv[1]);
2341 doc->user = argv[2];
2342 doc->title = argv[3];
2343 doc->copies = atoi(argv[4]);
bd7854cb 2344
80ca4592 2345 if (ppd && ppd->landscape > 0)
2346 doc->normal_landscape = 1;
bd7854cb 2347
80ca4592 2348 doc->bounding_box[0] = (int)PageLeft;
2349 doc->bounding_box[1] = (int)PageBottom;
2350 doc->bounding_box[2] = (int)PageRight;
2351 doc->bounding_box[3] = (int)PageTop;
bd7854cb 2352
80ca4592 2353 doc->new_bounding_box[0] = INT_MAX;
2354 doc->new_bounding_box[1] = INT_MAX;
2355 doc->new_bounding_box[2] = INT_MIN;
2356 doc->new_bounding_box[3] = INT_MIN;
bd7854cb 2357
80ca4592 2358 /*
2359 * AP_FIRSTPAGE_InputSlot
2360 */
ef416fc2 2361
80ca4592 2362 doc->ap_input_slot = cupsGetOption("AP_FIRSTPAGE_InputSlot", num_options,
2363 options);
ef416fc2 2364
80ca4592 2365 /*
2366 * AP_FIRSTPAGE_ManualFeed
2367 */
ef416fc2 2368
80ca4592 2369 doc->ap_manual_feed = cupsGetOption("AP_FIRSTPAGE_ManualFeed", num_options,
2370 options);
ef416fc2 2371
80ca4592 2372 /*
2373 * brightness
2374 */
bd7854cb 2375
80ca4592 2376 if ((val = cupsGetOption("brightness", num_options, options)) != NULL)
ef416fc2 2377 {
80ca4592 2378 /*
2379 * Get brightness value from 10 to 1000.
2380 */
ef416fc2 2381
80ca4592 2382 intval = atoi(val);
ef416fc2 2383
80ca4592 2384 if (intval < 10 || intval > 1000)
2385 {
c0e1af83 2386 fprintf(stderr, _("ERROR: Unsupported brightness value %s, using "
2387 "brightness=100!\n"), val);
80ca4592 2388 doc->brightness = 1.0f;
2389 }
2390 else
2391 doc->brightness = intval * 0.01f;
ef416fc2 2392 }
80ca4592 2393 else
2394 doc->brightness = 1.0f;
ef416fc2 2395
ef416fc2 2396 /*
80ca4592 2397 * collate, multiple-document-handling
ef416fc2 2398 */
2399
80ca4592 2400 if ((val = cupsGetOption("multiple-document-handling", num_options, options)) != NULL)
ef416fc2 2401 {
80ca4592 2402 /*
2403 * This IPP attribute is unnecessarily complicated...
2404 *
2405 * single-document, separate-documents-collated-copies, and
2406 * single-document-new-sheet all require collated copies.
2407 *
2408 * separate-documents-uncollated-copies allows for uncollated copies.
2409 */
2410
2411 doc->collate = strcasecmp(val, "separate-documents-uncollated-copies") != 0;
ef416fc2 2412 }
2413
80ca4592 2414 if ((val = cupsGetOption("Collate", num_options, options)) != NULL &&
2415 (!strcasecmp(val, "true") ||!strcasecmp(val, "on") ||
2416 !strcasecmp(val, "yes")))
2417 doc->collate = 1;
ef416fc2 2418
80ca4592 2419 /*
2420 * emit-jcl
2421 */
ef416fc2 2422
80ca4592 2423 if ((val = cupsGetOption("emit-jcl", num_options, options)) != NULL &&
2424 (!strcasecmp(val, "false") || !strcasecmp(val, "off") ||
2425 !strcasecmp(val, "no") || !strcmp(val, "0")))
2426 doc->emit_jcl = 0;
2427 else
2428 doc->emit_jcl = 1;
ef416fc2 2429
ef416fc2 2430 /*
80ca4592 2431 * fitplot
ef416fc2 2432 */
2433
80ca4592 2434 if ((val = cupsGetOption("fitplot", num_options, options)) != NULL &&
2435 (!strcasecmp(val, "true") || !strcasecmp(val, "on") ||
2436 !strcasecmp(val, "yes")))
2437 doc->fitplot = 1;
ef416fc2 2438
2439 /*
80ca4592 2440 * gamma
ef416fc2 2441 */
2442
80ca4592 2443 if ((val = cupsGetOption("gamma", num_options, options)) != NULL)
ef416fc2 2444 {
80ca4592 2445 /*
2446 * Get gamma value from 1 to 10000...
2447 */
ef416fc2 2448
80ca4592 2449 intval = atoi(val);
ef416fc2 2450
80ca4592 2451 if (intval < 1 || intval > 10000)
2452 {
c0e1af83 2453 fprintf(stderr, _("ERROR: Unsupported gamma value %s, using "
2454 "gamma=1000!\n"), val);
80ca4592 2455 doc->gamma = 1.0f;
2456 }
2457 else
2458 doc->gamma = intval * 0.001f;
2459 }
2460 else
2461 doc->gamma = 1.0f;
ef416fc2 2462
2463 /*
80ca4592 2464 * InputSlot
ef416fc2 2465 */
2466
80ca4592 2467 if ((choice = ppdFindMarkedChoice(ppd, "InputSlot")) != NULL)
2468 doc->input_slot = choice->choice;
ef416fc2 2469
2470 /*
80ca4592 2471 * ManualFeed
ef416fc2 2472 */
2473
80ca4592 2474 if ((choice = ppdFindMarkedChoice(ppd, "ManualFeed")) != NULL)
2475 doc->manual_feed = choice->choice;
ef416fc2 2476
09a101d6 2477 if ((choice = ppdFindMarkedChoice(ppd, "MirrorPrint")) != NULL)
2478 {
2479 val = choice->choice;
2480 choice->marked = 0;
2481 }
2482 else
2483 val = cupsGetOption("mirror", num_options, options);
2484
2485 if (val && (!strcasecmp(val, "true") || !strcasecmp(val, "on") ||
2486 !strcasecmp(val, "yes")))
80ca4592 2487 doc->mirror = 1;
ef416fc2 2488
80ca4592 2489 /*
2490 * number-up
2491 */
ef416fc2 2492
80ca4592 2493 if ((val = cupsGetOption("number-up", num_options, options)) != NULL)
2494 {
2495 switch (intval = atoi(val))
2496 {
2497 case 1 :
2498 case 2 :
2499 case 4 :
2500 case 6 :
2501 case 9 :
2502 case 16 :
2503 doc->number_up = intval;
2504 break;
2505 default :
2506 fprintf(stderr,
c0e1af83 2507 _("ERROR: Unsupported number-up value %d, using "
2508 "number-up=1!\n"), intval);
80ca4592 2509 doc->number_up = 1;
2510 break;
2511 }
2512 }
2513 else
2514 doc->number_up = 1;
ef416fc2 2515
80ca4592 2516 /*
2517 * number-up-layout
2518 */
ef416fc2 2519
80ca4592 2520 if ((val = cupsGetOption("number-up-layout", num_options, options)) != NULL)
ef416fc2 2521 {
80ca4592 2522 if (!strcasecmp(val, "lrtb"))
2523 doc->number_up_layout = PSTOPS_LAYOUT_LRTB;
2524 else if (!strcasecmp(val, "lrbt"))
2525 doc->number_up_layout = PSTOPS_LAYOUT_LRBT;
2526 else if (!strcasecmp(val, "rltb"))
2527 doc->number_up_layout = PSTOPS_LAYOUT_RLTB;
2528 else if (!strcasecmp(val, "rlbt"))
2529 doc->number_up_layout = PSTOPS_LAYOUT_RLBT;
2530 else if (!strcasecmp(val, "tblr"))
2531 doc->number_up_layout = PSTOPS_LAYOUT_TBLR;
2532 else if (!strcasecmp(val, "tbrl"))
2533 doc->number_up_layout = PSTOPS_LAYOUT_TBRL;
2534 else if (!strcasecmp(val, "btlr"))
2535 doc->number_up_layout = PSTOPS_LAYOUT_BTLR;
2536 else if (!strcasecmp(val, "btrl"))
2537 doc->number_up_layout = PSTOPS_LAYOUT_BTRL;
2538 else
2539 {
c0e1af83 2540 fprintf(stderr, _("ERROR: Unsupported number-up-layout value %s, using "
2541 "number-up-layout=lrtb!\n"), val);
80ca4592 2542 doc->number_up_layout = PSTOPS_LAYOUT_LRTB;
2543 }
2544 }
2545 else
2546 doc->number_up_layout = PSTOPS_LAYOUT_LRTB;
ef416fc2 2547
80ca4592 2548 /*
2549 * OutputOrder
2550 */
ef416fc2 2551
80ca4592 2552 if ((val = cupsGetOption("OutputOrder", num_options, options)) != NULL)
2553 {
2554 if (!strcasecmp(val, "Reverse"))
2555 doc->output_order = 1;
2556 }
2557 else if (ppd)
2558 {
2559 /*
2560 * Figure out the right default output order from the PPD file...
2561 */
ef416fc2 2562
80ca4592 2563 if ((choice = ppdFindMarkedChoice(ppd, "OutputBin")) != NULL &&
2564 (attr = ppdFindAttr(ppd, "PageStackOrder", choice->choice)) != NULL &&
2565 attr->value)
2566 doc->output_order = !strcasecmp(attr->value, "Reverse");
2567 else if ((attr = ppdFindAttr(ppd, "DefaultOutputOrder", NULL)) != NULL &&
2568 attr->value)
2569 doc->output_order = !strcasecmp(attr->value, "Reverse");
2570 }
ef416fc2 2571
80ca4592 2572 /*
2573 * page-border
2574 */
ef416fc2 2575
80ca4592 2576 if ((val = cupsGetOption("page-border", num_options, options)) != NULL)
2577 {
2578 if (!strcasecmp(val, "none"))
2579 doc->page_border = PSTOPS_BORDERNONE;
2580 else if (!strcasecmp(val, "single"))
2581 doc->page_border = PSTOPS_BORDERSINGLE;
2582 else if (!strcasecmp(val, "single-thick"))
2583 doc->page_border = PSTOPS_BORDERSINGLE2;
2584 else if (!strcasecmp(val, "double"))
2585 doc->page_border = PSTOPS_BORDERDOUBLE;
2586 else if (!strcasecmp(val, "double-thick"))
2587 doc->page_border = PSTOPS_BORDERDOUBLE2;
2588 else
2589 {
c0e1af83 2590 fprintf(stderr, _("ERROR: Unsupported page-border value %s, using "
2591 "page-border=none!\n"), val);
80ca4592 2592 doc->page_border = PSTOPS_BORDERNONE;
2593 }
ef416fc2 2594 }
80ca4592 2595 else
2596 doc->page_border = PSTOPS_BORDERNONE;
ef416fc2 2597
80ca4592 2598 /*
2599 * page-label
2600 */
ef416fc2 2601
80ca4592 2602 doc->page_label = cupsGetOption("page-label", num_options, options);
ef416fc2 2603
80ca4592 2604 /*
2605 * page-ranges
2606 */
ef416fc2 2607
80ca4592 2608 doc->page_ranges = cupsGetOption("page-ranges", num_options, options);
ef416fc2 2609
2610 /*
80ca4592 2611 * page-set
ef416fc2 2612 */
2613
80ca4592 2614 doc->page_set = cupsGetOption("page-set", num_options, options);
ef416fc2 2615
2616 /*
80ca4592 2617 * Now figure out if we have to force collated copies, etc.
ef416fc2 2618 */
2619
80ca4592 2620 if (ppd && ppd->manual_copies && Duplex && doc->copies > 1)
ef416fc2 2621 {
80ca4592 2622 /*
2623 * Force collated copies when printing a duplexed document to
2624 * a non-PS printer that doesn't do hardware copy generation.
2625 * Otherwise the copies will end up on the front/back side of
2626 * each page.
2627 */
ef416fc2 2628
80ca4592 2629 doc->collate = 1;
ef416fc2 2630 }
2631
2632 /*
80ca4592 2633 * See if we have to filter the fast or slow way...
ef416fc2 2634 */
2635
80ca4592 2636 if (doc->collate && doc->copies > 1)
ef416fc2 2637 {
80ca4592 2638 /*
2639 * See if we need to manually collate the pages...
2640 */
2641
2642 doc->slow_collate = 1;
2643
2644 if ((choice = ppdFindMarkedChoice(ppd, "Collate")) != NULL &&
2645 !strcasecmp(choice->choice, "True"))
bd7854cb 2646 {
80ca4592 2647 /*
2648 * Hardware collate option is selected, see if the option is
2649 * conflicting - if not, collate in hardware. Otherwise,
2650 * turn the hardware collate option off...
2651 */
2652
2abf387c 2653 if ((option = ppdFindOption(ppd, "Collate")) != NULL &&
80ca4592 2654 !option->conflicted)
2655 doc->slow_collate = 0;
bd7854cb 2656 else
80ca4592 2657 ppdMarkOption(ppd, "Collate", "False");
bd7854cb 2658 }
ef416fc2 2659 }
2660 else
80ca4592 2661 doc->slow_collate = 0;
2662
2663 if (!ppdFindOption(ppd, "OutputOrder") && doc->output_order)
2664 doc->slow_order = 1;
2665 else
2666 doc->slow_order = 0;
2667
2abf387c 2668 if (Duplex &&
2669 (doc->slow_collate || doc->slow_order ||
2670 ((attr = ppdFindAttr(ppd, "cupsEvenDuplex", NULL)) != NULL &&
2671 attr->value && !strcasecmp(attr->value, "true"))))
80ca4592 2672 doc->slow_duplex = 1;
2673 else
2674 doc->slow_duplex = 0;
2675
2676 /*
2677 * Create a temporary file for page data if we need to filter slowly...
2678 */
2679
2680 if (doc->slow_order || doc->slow_collate)
ef416fc2 2681 {
80ca4592 2682 if ((doc->temp = cupsTempFile2(doc->tempfile,
2683 sizeof(doc->tempfile))) == NULL)
bd7854cb 2684 {
c0e1af83 2685 fprintf(stderr, _("ERROR: Unable to create temporary file: %s\n"),
80ca4592 2686 strerror(errno));
2687 exit(1);
bd7854cb 2688 }
ef416fc2 2689 }
80ca4592 2690
2691 /*
2692 * Figure out if we should use ESPshowpage or not...
2693 */
2694
2695 if (doc->page_label || getenv("CLASSIFICATION") || doc->number_up > 1 ||
2696 doc->page_border)
2697 {
2698 /*
2699 * Yes, use ESPshowpage...
2700 */
2701
2702 doc->use_ESPshowpage = 1;
2703 }
2704
2705 fprintf(stderr, "DEBUG: slow_collate=%d, slow_duplex=%d, slow_order=%d\n",
2706 doc->slow_collate, doc->slow_duplex, doc->slow_order);
ef416fc2 2707}
2708
2709
2710/*
80ca4592 2711 * 'skip_page()' - Skip past a page that won't be printed...
ef416fc2 2712 */
2713
80ca4592 2714static size_t /* O - Length of next line */
2715skip_page(cups_file_t *fp, /* I - File to read from */
2716 char *line, /* I - Line buffer */
2717 size_t linelen, /* I - Length of initial line */
2718 size_t linesize) /* I - Size of line buffer */
ef416fc2 2719{
80ca4592 2720 int level; /* Embedded document level */
ef416fc2 2721
2722
80ca4592 2723 level = 0;
ef416fc2 2724
80ca4592 2725 while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0)
ef416fc2 2726 {
80ca4592 2727 if (level == 0 &&
d6ae789d 2728 (!strncmp(line, "%%Page:", 7) || !strncmp(line, "%%Trailer", 9)))
ef416fc2 2729 break;
80ca4592 2730 else if (!strncmp(line, "%%BeginDocument", 15) ||
2731 !strncmp(line, "%ADO_BeginApplication", 21))
2732 level ++;
2733 else if ((!strncmp(line, "%%EndDocument", 13) ||
2734 !strncmp(line, "%ADO_EndApplication", 19)) && level > 0)
2735 level --;
2736 else if (!strncmp(line, "%%BeginBinary:", 14) ||
2737 (!strncmp(line, "%%BeginData:", 12) &&
2738 !strstr(line, "ASCII") && !strstr(line, "Hex")))
ef416fc2 2739 {
2740 /*
80ca4592 2741 * Skip binary data...
ef416fc2 2742 */
2743
80ca4592 2744 int bytes; /* Bytes of data */
ef416fc2 2745
ef416fc2 2746
80ca4592 2747 bytes = atoi(strchr(line, ':') + 1);
ef416fc2 2748
80ca4592 2749 while (bytes > 0)
2750 {
2751 if (bytes > linesize)
2752 linelen = cupsFileRead(fp, line, linesize);
2753 else
2754 linelen = cupsFileRead(fp, line, bytes);
ef416fc2 2755
80ca4592 2756 if (linelen < 1)
2757 {
2758 line[0] = '\0';
2759 perror("ERROR: Early end-of-file while reading binary data");
2760 return (0);
2761 }
ef416fc2 2762
80ca4592 2763 bytes -= linelen;
2764 }
2765 }
2766 }
ef416fc2 2767
80ca4592 2768 return (linelen);
ef416fc2 2769}
2770
2771
ef416fc2 2772/*
2773 * 'start_nup()' - Start processing for N-up printing...
2774 */
2775
2776static void
80ca4592 2777start_nup(pstops_doc_t *doc, /* I - Document information */
2778 int number, /* I - Page number */
2779 int show_border, /* I - Show the border? */
2780 const int *bounding_box) /* I - BoundingBox value */
ef416fc2 2781{
80ca4592 2782 int pos; /* Position on page */
2783 int x, y; /* Relative position of subpage */
2784 float w, l, /* Width and length of subpage */
2785 tx, ty; /* Translation values for subpage */
2786 float pagew, /* Printable width of page */
2787 pagel; /* Printable height of page */
a41f09e2
MS
2788 int bboxx, /* BoundingBox X origin */
2789 bboxy, /* BoundingBox Y origin */
2790 bboxw, /* BoundingBox width */
80ca4592 2791 bboxl; /* BoundingBox height */
a41f09e2 2792 float margin = 0; /* Current margin for border */
80ca4592 2793
ef416fc2 2794
2abf387c 2795 if (doc->number_up > 1)
80ca4592 2796 doc_puts(doc, "userdict/ESPsave save put\n");
ef416fc2 2797
80ca4592 2798 if (doc->mirror)
2799 doc_printf(doc, "%.1f 0.0 translate -1 1 scale\n", PageWidth);
ef416fc2 2800
80ca4592 2801 pos = (number - 1) % doc->number_up;
2802 pagew = PageRight - PageLeft;
2803 pagel = PageTop - PageBottom;
ef416fc2 2804
80ca4592 2805 if (doc->fitplot)
2806 {
a41f09e2
MS
2807 bboxx = bounding_box[0];
2808 bboxy = bounding_box[1];
80ca4592 2809 bboxw = bounding_box[2] - bounding_box[0];
2810 bboxl = bounding_box[3] - bounding_box[1];
2811 }
2812 else
2813 {
a41f09e2
MS
2814 bboxx = 0;
2815 bboxy = 0;
80ca4592 2816 bboxw = PageWidth;
2817 bboxl = PageLength;
2818 }
ef416fc2 2819
80ca4592 2820 fprintf(stderr, "DEBUG: pagew = %.1f, pagel = %.1f\n", pagew, pagel);
a41f09e2
MS
2821 fprintf(stderr, "DEBUG: bboxx = %d, bboxy = %d, bboxw = %d, bboxl = %d\n",
2822 bboxx, bboxy, bboxw, bboxl);
80ca4592 2823 fprintf(stderr, "DEBUG: PageLeft = %.1f, PageRight = %.1f\n",
2824 PageLeft, PageRight);
2825 fprintf(stderr, "DEBUG: PageTop = %.1f, PageBottom = %.1f\n",
2826 PageTop, PageBottom);
2827 fprintf(stderr, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
2828 PageWidth, PageLength);
ef416fc2 2829
2830 switch (Orientation)
2831 {
2832 case 1 : /* Landscape */
80ca4592 2833 doc_printf(doc, "%.1f 0.0 translate 90 rotate\n", PageLength);
ef416fc2 2834 break;
2835 case 2 : /* Reverse Portrait */
80ca4592 2836 doc_printf(doc, "%.1f %.1f translate 180 rotate\n", PageWidth,
2837 PageLength);
ef416fc2 2838 break;
2839 case 3 : /* Reverse Landscape */
80ca4592 2840 doc_printf(doc, "0.0 %.1f translate -90 rotate\n", PageWidth);
ef416fc2 2841 break;
2842 }
2843
80ca4592 2844 if (Duplex && doc->number_up > 1 && ((number / doc->number_up) & 1))
2845 doc_printf(doc, "%.1f %.1f translate\n", PageWidth - PageRight, PageBottom);
2846 else if (doc->number_up > 1 || doc->fitplot)
2847 doc_printf(doc, "%.1f %.1f translate\n", PageLeft, PageBottom);
ef416fc2 2848
80ca4592 2849 switch (doc->number_up)
ef416fc2 2850 {
2851 default :
80ca4592 2852 if (doc->fitplot)
bd7854cb 2853 {
80ca4592 2854 w = pagew;
2855 l = w * bboxl / bboxw;
bd7854cb 2856
80ca4592 2857 if (l > pagel)
bd7854cb 2858 {
80ca4592 2859 l = pagel;
2860 w = l * bboxw / bboxl;
bd7854cb 2861 }
2862
80ca4592 2863 tx = 0.5 * (pagew - w);
2864 ty = 0.5 * (pagel - l);
bd7854cb 2865
80ca4592 2866 doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n", tx, ty,
2867 w / bboxw, l / bboxl);
bd7854cb 2868 }
2869 else
2870 {
2871 w = PageWidth;
2872 l = PageLength;
2873 }
ef416fc2 2874 break;
2875
2876 case 2 :
2877 if (Orientation & 1)
2878 {
2879 x = pos & 1;
2880
80ca4592 2881 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY)
ef416fc2 2882 x = 1 - x;
2883
80ca4592 2884 w = pagel;
2885 l = w * bboxl / bboxw;
ef416fc2 2886
80ca4592 2887 if (l > (pagew * 0.5))
ef416fc2 2888 {
80ca4592 2889 l = pagew * 0.5;
2890 w = l * bboxw / bboxl;
ef416fc2 2891 }
2892
80ca4592 2893 tx = 0.5 * (pagew * 0.5 - l);
2894 ty = 0.5 * (pagel - w);
ef416fc2 2895
80ca4592 2896 if (doc->normal_landscape)
2897 doc_printf(doc, "0.0 %.1f translate -90 rotate\n", pagel);
ef416fc2 2898 else
80ca4592 2899 doc_printf(doc, "%.1f 0.0 translate 90 rotate\n", pagew);
ef416fc2 2900
80ca4592 2901 doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n",
2902 ty, tx + pagew * 0.5 * x, w / bboxw, l / bboxl);
ef416fc2 2903 }
2904 else
2905 {
2906 x = pos & 1;
2907
80ca4592 2908 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX)
ef416fc2 2909 x = 1 - x;
2910
80ca4592 2911 l = pagew;
2912 w = l * bboxw / bboxl;
ef416fc2 2913
80ca4592 2914 if (w > (pagel * 0.5))
ef416fc2 2915 {
80ca4592 2916 w = pagel * 0.5;
2917 l = w * bboxl / bboxw;
ef416fc2 2918 }
2919
80ca4592 2920 tx = 0.5 * (pagel * 0.5 - w);
2921 ty = 0.5 * (pagew - l);
ef416fc2 2922
80ca4592 2923 if (doc->normal_landscape)
2924 doc_printf(doc, "%.1f 0.0 translate 90 rotate\n", pagew);
ef416fc2 2925 else
80ca4592 2926 doc_printf(doc, "0.0 %.1f translate -90 rotate\n", pagel);
ef416fc2 2927
80ca4592 2928 doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n",
2929 tx + pagel * 0.5 * x, ty, w / bboxw, l / bboxl);
ef416fc2 2930 }
2931 break;
2932
2933 case 4 :
80ca4592 2934 if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL)
ef416fc2 2935 {
2936 x = (pos / 2) & 1;
2937 y = pos & 1;
2938 }
2939 else
2940 {
2941 x = pos & 1;
2942 y = (pos / 2) & 1;
2943 }
2944
80ca4592 2945 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX)
ef416fc2 2946 x = 1 - x;
2947
80ca4592 2948 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY)
ef416fc2 2949 y = 1 - y;
2950
80ca4592 2951 w = pagew * 0.5;
2952 l = w * bboxl / bboxw;
ef416fc2 2953
80ca4592 2954 if (l > (pagel * 0.5))
ef416fc2 2955 {
80ca4592 2956 l = pagel * 0.5;
2957 w = l * bboxw / bboxl;
ef416fc2 2958 }
2959
80ca4592 2960 tx = 0.5 * (pagew * 0.5 - w);
2961 ty = 0.5 * (pagel * 0.5 - l);
ef416fc2 2962
80ca4592 2963 doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n",
2964 tx + x * pagew * 0.5, ty + y * pagel * 0.5,
2965 w / bboxw, l / bboxl);
ef416fc2 2966 break;
2967
2968 case 6 :
2969 if (Orientation & 1)
2970 {
80ca4592 2971 if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL)
ef416fc2 2972 {
2973 x = pos / 3;
2974 y = pos % 3;
2975
80ca4592 2976 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX)
ef416fc2 2977 x = 1 - x;
2978
80ca4592 2979 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY)
ef416fc2 2980 y = 2 - y;
2981 }
2982 else
2983 {
2984 x = pos & 1;
2985 y = pos / 2;
2986
80ca4592 2987 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX)
ef416fc2 2988 x = 1 - x;
2989
80ca4592 2990 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY)
ef416fc2 2991 y = 2 - y;
2992 }
2993
80ca4592 2994 w = pagel * 0.5;
2995 l = w * bboxl / bboxw;
ef416fc2 2996
80ca4592 2997 if (l > (pagew * 0.333))
ef416fc2 2998 {
80ca4592 2999 l = pagew * 0.333;
3000 w = l * bboxw / bboxl;
ef416fc2 3001 }
3002
80ca4592 3003 tx = 0.5 * (pagel - 2 * w);
3004 ty = 0.5 * (pagew - 3 * l);
ef416fc2 3005
80ca4592 3006 if (doc->normal_landscape)
f301802f 3007 doc_printf(doc, "0 %.1f translate -90 rotate\n", pagel);
ef416fc2 3008 else
f301802f 3009 doc_printf(doc, "%.1f 0 translate 90 rotate\n", pagew);
ef416fc2 3010
80ca4592 3011 doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n",
f301802f 3012 tx + x * w, ty + y * l, l / bboxl, w / bboxw);
ef416fc2 3013 }
3014 else
3015 {
80ca4592 3016 if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL)
ef416fc2 3017 {
3018 x = pos / 2;
3019 y = pos & 1;
3020
80ca4592 3021 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX)
ef416fc2 3022 x = 2 - x;
3023
80ca4592 3024 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY)
ef416fc2 3025 y = 1 - y;
3026 }
3027 else
3028 {
3029 x = pos % 3;
3030 y = pos / 3;
3031
80ca4592 3032 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX)
ef416fc2 3033 x = 2 - x;
3034
80ca4592 3035 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY)
ef416fc2 3036 y = 1 - y;
3037 }
3038
80ca4592 3039 l = pagew * 0.5;
3040 w = l * bboxw / bboxl;
ef416fc2 3041
80ca4592 3042 if (w > (pagel * 0.333))
ef416fc2 3043 {
80ca4592 3044 w = pagel * 0.333;
3045 l = w * bboxl / bboxw;
ef416fc2 3046 }
3047
f301802f 3048 tx = 0.5 * (pagel - 3 * w);
3049 ty = 0.5 * (pagew - 2 * l);
ef416fc2 3050
80ca4592 3051 if (doc->normal_landscape)
f301802f 3052 doc_printf(doc, "%.1f 0 translate 90 rotate\n", pagew);
ef416fc2 3053 else
f301802f 3054 doc_printf(doc, "0 %.1f translate -90 rotate\n", pagel);
ef416fc2 3055
80ca4592 3056 doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n",
f301802f 3057 tx + w * x, ty + l * y, w / bboxw, l / bboxl);
3058
ef416fc2 3059 }
3060 break;
3061
3062 case 9 :
80ca4592 3063 if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL)
ef416fc2 3064 {
3065 x = (pos / 3) % 3;
3066 y = pos % 3;
3067 }
3068 else
3069 {
3070 x = pos % 3;
3071 y = (pos / 3) % 3;
3072 }
3073
80ca4592 3074 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX)
ef416fc2 3075 x = 2 - x;
3076
80ca4592 3077 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY)
ef416fc2 3078 y = 2 - y;
3079
80ca4592 3080 w = pagew * 0.333;
3081 l = w * bboxl / bboxw;
ef416fc2 3082
80ca4592 3083 if (l > (pagel * 0.333))
ef416fc2 3084 {
80ca4592 3085 l = pagel * 0.333;
3086 w = l * bboxw / bboxl;
ef416fc2 3087 }
3088
80ca4592 3089 tx = 0.5 * (pagew * 0.333 - w);
3090 ty = 0.5 * (pagel * 0.333 - l);
ef416fc2 3091
80ca4592 3092 doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n",
3093 tx + x * pagew * 0.333, ty + y * pagel * 0.333,
3094 w / bboxw, l / bboxl);
ef416fc2 3095 break;
3096
3097 case 16 :
80ca4592 3098 if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL)
ef416fc2 3099 {
3100 x = (pos / 4) & 3;
3101 y = pos & 3;
3102 }
3103 else
3104 {
3105 x = pos & 3;
3106 y = (pos / 4) & 3;
3107 }
3108
80ca4592 3109 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX)
ef416fc2 3110 x = 3 - x;
3111
80ca4592 3112 if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY)
ef416fc2 3113 y = 3 - y;
3114
80ca4592 3115 w = pagew * 0.25;
3116 l = w * bboxl / bboxw;
ef416fc2 3117
80ca4592 3118 if (l > (pagel * 0.25))
ef416fc2 3119 {
80ca4592 3120 l = pagel * 0.25;
3121 w = l * bboxw / bboxl;
ef416fc2 3122 }
3123
80ca4592 3124 tx = 0.5 * (pagew * 0.25 - w);
3125 ty = 0.5 * (pagel * 0.25 - l);
ef416fc2 3126
80ca4592 3127 doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n",
3128 tx + x * pagew * 0.25, ty + y * pagel * 0.25,
3129 w / bboxw, l / bboxl);
ef416fc2 3130 break;
3131 }
3132
3133 /*
3134 * Draw borders as necessary...
3135 */
3136
80ca4592 3137 if (doc->page_border && show_border)
ef416fc2 3138 {
80ca4592 3139 int rects; /* Number of border rectangles */
a41f09e2 3140 float fscale; /* Scaling value for points */
ef416fc2 3141
3142
80ca4592 3143 rects = (doc->page_border & PSTOPS_BORDERDOUBLE) ? 2 : 1;
ef416fc2 3144 fscale = PageWidth / w;
3145 margin = 2.25 * fscale;
3146
3147 /*
3148 * Set the line width and color...
3149 */
3150
80ca4592 3151 doc_puts(doc, "gsave\n");
3152 doc_printf(doc, "%.3f setlinewidth 0 setgray newpath\n",
3153 (doc->page_border & PSTOPS_BORDERTHICK) ? 0.5 * fscale :
3154 0.24 * fscale);
ef416fc2 3155
3156 /*
3157 * Draw border boxes...
3158 */
3159
3160 for (; rects > 0; rects --, margin += 2 * fscale)
80ca4592 3161 if (doc->number_up > 1)
3162 doc_printf(doc, "%.1f %.1f %.1f %.1f ESPrs\n",
a41f09e2
MS
3163 margin,
3164 margin,
3165 bboxw - 2 * margin,
3166 bboxl - 2 * margin);
ef416fc2 3167 else
80ca4592 3168 doc_printf(doc, "%.1f %.1f %.1f %.1f ESPrs\n",
3169 PageLeft + margin,
3170 PageBottom + margin,
3171 PageRight - PageLeft - 2 * margin,
3172 PageTop - PageBottom - 2 * margin);
ef416fc2 3173
3174 /*
3175 * Restore pen settings...
3176 */
3177
80ca4592 3178 doc_puts(doc, "grestore\n");
ef416fc2 3179 }
3180
80ca4592 3181 if (doc->fitplot)
ef416fc2 3182 {
3183 /*
a41f09e2 3184 * Offset the page by its bounding box...
ef416fc2 3185 */
3186
80ca4592 3187 doc_printf(doc, "%d %d translate\n", -bounding_box[0],
3188 -bounding_box[1]);
80ca4592 3189 }
a41f09e2
MS
3190
3191 if (doc->fitplot || doc->number_up > 1)
80ca4592 3192 {
3193 /*
a41f09e2 3194 * Clip the page to the page's bounding box...
80ca4592 3195 */
3196
a41f09e2
MS
3197 doc_printf(doc, "%.1f %.1f %.1f %.1f ESPrc\n",
3198 bboxx + margin, bboxy + margin,
3199 bboxw - 2 * margin, bboxl - 2 * margin);
ef416fc2 3200 }
3201}
3202
3203
b86bc4cf 3204/*
3205 * 'write_label_prolog()' - Write the prolog with the classification
3206 * and page label.
3207 */
3208
3209static void
3210write_label_prolog(pstops_doc_t *doc, /* I - Document info */
3211 const char *label, /* I - Page label */
3212 float bottom, /* I - Bottom position in points */
3213 float top, /* I - Top position in points */
3214 float width) /* I - Width in points */
3215{
3216 const char *classification; /* CLASSIFICATION environment variable */
3217 const char *ptr; /* Temporary string pointer */
3218
3219
3220 /*
3221 * First get the current classification...
3222 */
3223
3224 if ((classification = getenv("CLASSIFICATION")) == NULL)
3225 classification = "";
3226 if (strcmp(classification, "none") == 0)
3227 classification = "";
3228
3229 /*
3230 * If there is nothing to show, bind an empty 'write labels' procedure
3231 * and return...
3232 */
3233
3234 if (!classification[0] && (label == NULL || !label[0]))
3235 {
3236 doc_puts(doc, "userdict/ESPwl{}bind put\n");
3237 return;
3238 }
3239
3240 /*
3241 * Set the classification + page label string...
3242 */
3243
3244 doc_puts(doc, "userdict");
3245 if (!strcmp(classification, "confidential"))
3246 doc_puts(doc, "/ESPpl(CONFIDENTIAL");
3247 else if (!strcmp(classification, "classified"))
3248 doc_puts(doc, "/ESPpl(CLASSIFIED");
3249 else if (!strcmp(classification, "secret"))
3250 doc_puts(doc, "/ESPpl(SECRET");
3251 else if (!strcmp(classification, "topsecret"))
3252 doc_puts(doc, "/ESPpl(TOP SECRET");
3253 else if (!strcmp(classification, "unclassified"))
3254 doc_puts(doc, "/ESPpl(UNCLASSIFIED");
3255 else
3256 {
3257 doc_puts(doc, "/ESPpl(");
3258
3259 for (ptr = classification; *ptr; ptr ++)
3260 {
3261 if (*ptr < 32 || *ptr > 126)
3262 doc_printf(doc, "\\%03o", *ptr);
3263 else if (*ptr == '_')
3264 doc_puts(doc, " ");
3265 else if (*ptr == '(' || *ptr == ')' || *ptr == '\\')
3266 doc_printf(doc, "\\%c", *ptr);
3267 else
3268 doc_printf(doc, "%c", *ptr);
3269 }
3270 }
3271
3272 if (label)
3273 {
3274 if (classification[0])
3275 doc_puts(doc, " - ");
3276
3277 /*
3278 * Quote the label string as needed...
3279 */
3280
3281 for (ptr = label; *ptr; ptr ++)
3282 {
3283 if (*ptr < 32 || *ptr > 126)
3284 doc_printf(doc, "\\%03o", *ptr);
3285 else if (*ptr == '(' || *ptr == ')' || *ptr == '\\')
3286 doc_printf(doc, "\\%c", *ptr);
3287 else
3288 doc_printf(doc, "%c", *ptr);
3289 }
3290 }
3291
3292 doc_puts(doc, ")put\n");
3293
3294 /*
3295 * Then get a 14 point Helvetica-Bold font...
3296 */
3297
3298 doc_puts(doc, "userdict/ESPpf /Helvetica-Bold findfont 14 scalefont put\n");
3299
3300 /*
3301 * Finally, the procedure to write the labels on the page...
3302 */
3303
3304 doc_puts(doc, "userdict/ESPwl{\n");
3305 doc_puts(doc, " ESPpf setfont\n");
3306 doc_printf(doc, " ESPpl stringwidth pop dup 12 add exch -0.5 mul %.0f add\n",
3307 width * 0.5f);
3308 doc_puts(doc, " 1 setgray\n");
3309 doc_printf(doc, " dup 6 sub %.0f 3 index 20 ESPrf\n", bottom - 2.0);
3310 doc_printf(doc, " dup 6 sub %.0f 3 index 20 ESPrf\n", top - 18.0);
3311 doc_puts(doc, " 0 setgray\n");
3312 doc_printf(doc, " dup 6 sub %.0f 3 index 20 ESPrs\n", bottom - 2.0);
3313 doc_printf(doc, " dup 6 sub %.0f 3 index 20 ESPrs\n", top - 18.0);
3314 doc_printf(doc, " dup %.0f moveto ESPpl show\n", bottom + 2.0);
3315 doc_printf(doc, " %.0f moveto ESPpl show\n", top - 14.0);
3316 doc_puts(doc, "pop\n");
3317 doc_puts(doc, "}bind put\n");
3318}
3319
3320
ed486911 3321/*
3322 * 'write_labels()' - Write the actual page labels.
3323 *
3324 * This function is a copy of the one in common.c since we need to
3325 * use doc_puts/doc_printf instead of puts/printf...
3326 */
3327
3328static void
3329write_labels(pstops_doc_t *doc, /* I - Document information */
3330 int orient) /* I - Orientation of the page */
3331{
3332 float width, /* Width of page */
3333 length; /* Length of page */
3334
3335
3336 doc_puts(doc, "gsave\n");
3337
3338 if ((orient ^ Orientation) & 1)
3339 {
3340 width = PageLength;
3341 length = PageWidth;
3342 }
3343 else
3344 {
3345 width = PageWidth;
3346 length = PageLength;
3347 }
3348
3349 switch (orient & 3)
3350 {
3351 case 1 : /* Landscape */
3352 doc_printf(doc, "%.1f 0.0 translate 90 rotate\n", length);
3353 break;
3354 case 2 : /* Reverse Portrait */
3355 doc_printf(doc, "%.1f %.1f translate 180 rotate\n", width, length);
3356 break;
3357 case 3 : /* Reverse Landscape */
3358 doc_printf(doc, "0.0 %.1f translate -90 rotate\n", width);
3359 break;
3360 }
3361
3362 doc_puts(doc, "ESPwl\n");
3363 doc_puts(doc, "grestore\n");
3364}
3365
3366
ef416fc2 3367/*
2e4ff8af 3368 * End of "$Id: pstops.c 7006 2007-10-04 17:43:38Z mike $".
ef416fc2 3369 */