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