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