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