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