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