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