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