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