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