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