]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/pstops.c
2 * "$Id: pstops.c 12656 2015-05-22 17:27:37Z msweet $"
4 * PostScript filter for CUPS.
6 * Copyright 2007-2015 by Apple Inc.
7 * Copyright 1993-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * Include necessary headers...
25 #include <cups/file.h>
26 #include <cups/array.h>
27 #include <cups/language-private.h>
35 #define PSTOPS_BORDERNONE 0 /* No border or hairline border */
36 #define PSTOPS_BORDERTHICK 1 /* Think border */
37 #define PSTOPS_BORDERSINGLE 2 /* Single-line hairline border */
38 #define PSTOPS_BORDERSINGLE2 3 /* Single-line thick border */
39 #define PSTOPS_BORDERDOUBLE 4 /* Double-line hairline border */
40 #define PSTOPS_BORDERDOUBLE2 5 /* Double-line thick border */
42 #define PSTOPS_LAYOUT_LRBT 0 /* Left to right, bottom to top */
43 #define PSTOPS_LAYOUT_LRTB 1 /* Left to right, top to bottom */
44 #define PSTOPS_LAYOUT_RLBT 2 /* Right to left, bottom to top */
45 #define PSTOPS_LAYOUT_RLTB 3 /* Right to left, top to bottom */
46 #define PSTOPS_LAYOUT_BTLR 4 /* Bottom to top, left to right */
47 #define PSTOPS_LAYOUT_TBLR 5 /* Top to bottom, left to right */
48 #define PSTOPS_LAYOUT_BTRL 6 /* Bottom to top, right to left */
49 #define PSTOPS_LAYOUT_TBRL 7 /* Top to bottom, right to left */
51 #define PSTOPS_LAYOUT_NEGATEY 1 /* The bits for the layout */
52 #define PSTOPS_LAYOUT_NEGATEX 2 /* definitions above... */
53 #define PSTOPS_LAYOUT_VERTICAL 4
60 typedef struct /**** Page information ****/
62 char *label
; /* Page label */
63 int bounding_box
[4]; /* PageBoundingBox */
64 off_t offset
; /* Offset to start of page */
65 ssize_t length
; /* Number of bytes for page */
66 int num_options
; /* Number of options for this page */
67 cups_option_t
*options
; /* Options for this page */
70 typedef struct /**** Document information ****/
72 int page
; /* Current page */
73 int bounding_box
[4]; /* BoundingBox from header */
74 int new_bounding_box
[4]; /* New composite bounding box */
75 int num_options
; /* Number of document-wide options */
76 cups_option_t
*options
; /* Document-wide options */
77 int normal_landscape
, /* Normal rotation for landscape? */
78 saw_eof
, /* Saw the %%EOF comment? */
79 slow_collate
, /* Collate copies by hand? */
80 slow_duplex
, /* Duplex pages slowly? */
81 slow_order
, /* Reverse pages slowly? */
82 use_ESPshowpage
; /* Use ESPshowpage? */
83 cups_array_t
*pages
; /* Pages in document */
84 cups_file_t
*temp
; /* Temporary file, if any */
85 char tempfile
[1024]; /* Temporary filename */
86 int job_id
; /* Job ID */
87 const char *user
, /* User name */
88 *title
; /* Job name */
89 int copies
; /* Number of copies */
90 const char *ap_input_slot
, /* AP_FIRSTPAGE_InputSlot value */
91 *ap_manual_feed
, /* AP_FIRSTPAGE_ManualFeed value */
92 *ap_media_color
, /* AP_FIRSTPAGE_MediaColor value */
93 *ap_media_type
, /* AP_FIRSTPAGE_MediaType value */
94 *ap_page_region
, /* AP_FIRSTPAGE_PageRegion value */
95 *ap_page_size
; /* AP_FIRSTPAGE_PageSize value */
96 int collate
, /* Collate copies? */
97 emit_jcl
, /* Emit JCL commands? */
98 fit_to_page
; /* Fit pages to media */
99 const char *input_slot
, /* InputSlot value */
100 *manual_feed
, /* ManualFeed value */
101 *media_color
, /* MediaColor value */
102 *media_type
, /* MediaType value */
103 *page_region
, /* PageRegion value */
104 *page_size
; /* PageSize value */
105 int mirror
, /* doc->mirror/mirror pages */
106 number_up
, /* Number of pages on each sheet */
107 number_up_layout
, /* doc->number_up_layout of N-up pages */
108 output_order
, /* Requested reverse output order? */
109 page_border
; /* doc->page_border around pages */
110 const char *page_label
, /* page-label option, if any */
111 *page_ranges
, /* page-ranges option, if any */
112 *page_set
; /* page-set option, if any */
117 * Convenience macros...
120 #define is_first_page(p) (doc->number_up == 1 || \
121 ((p) % doc->number_up) == 1)
122 #define is_last_page(p) (doc->number_up == 1 || \
123 ((p) % doc->number_up) == 0)
124 #define is_not_last_page(p) (doc->number_up > 1 && \
125 ((p) % doc->number_up) != 0)
132 static int JobCanceled
= 0;/* Set to 1 on SIGTERM */
139 static pstops_page_t
*add_page(pstops_doc_t
*doc
, const char *label
);
140 static void cancel_job(int sig
);
141 static int check_range(pstops_doc_t
*doc
, int page
);
142 static void copy_bytes(cups_file_t
*fp
, off_t offset
,
144 static ssize_t
copy_comments(cups_file_t
*fp
, pstops_doc_t
*doc
,
145 ppd_file_t
*ppd
, char *line
,
146 ssize_t linelen
, size_t linesize
);
147 static void copy_dsc(cups_file_t
*fp
, pstops_doc_t
*doc
,
148 ppd_file_t
*ppd
, char *line
, ssize_t linelen
,
150 static void copy_non_dsc(cups_file_t
*fp
, pstops_doc_t
*doc
,
151 ppd_file_t
*ppd
, char *line
,
152 ssize_t linelen
, size_t linesize
);
153 static ssize_t
copy_page(cups_file_t
*fp
, pstops_doc_t
*doc
,
154 ppd_file_t
*ppd
, int number
, char *line
,
155 ssize_t linelen
, size_t linesize
);
156 static ssize_t
copy_prolog(cups_file_t
*fp
, pstops_doc_t
*doc
,
157 ppd_file_t
*ppd
, char *line
,
158 ssize_t linelen
, size_t linesize
);
159 static ssize_t
copy_setup(cups_file_t
*fp
, pstops_doc_t
*doc
,
160 ppd_file_t
*ppd
, char *line
,
161 ssize_t linelen
, size_t linesize
);
162 static ssize_t
copy_trailer(cups_file_t
*fp
, pstops_doc_t
*doc
,
163 ppd_file_t
*ppd
, int number
, char *line
,
164 ssize_t linelen
, size_t linesize
);
165 static void do_prolog(pstops_doc_t
*doc
, ppd_file_t
*ppd
);
166 static void do_setup(pstops_doc_t
*doc
, ppd_file_t
*ppd
);
167 static void doc_printf(pstops_doc_t
*doc
, const char *format
, ...)
168 __attribute__ ((__format__ (__printf__
, 2, 3)));
169 static void doc_puts(pstops_doc_t
*doc
, const char *s
);
170 static void doc_write(pstops_doc_t
*doc
, const char *s
, size_t len
);
171 static void end_nup(pstops_doc_t
*doc
, int number
);
172 static int include_feature(ppd_file_t
*ppd
, const char *line
,
174 cups_option_t
**options
);
175 static char *parse_text(const char *start
, char **end
, char *buffer
,
177 static void set_pstops_options(pstops_doc_t
*doc
, ppd_file_t
*ppd
,
178 char *argv
[], int num_options
,
179 cups_option_t
*options
);
180 static ssize_t
skip_page(cups_file_t
*fp
, char *line
, ssize_t linelen
,
182 static void start_nup(pstops_doc_t
*doc
, int number
,
183 int show_border
, const int *bounding_box
);
184 static void write_label_prolog(pstops_doc_t
*doc
, const char *label
,
185 float bottom
, float top
,
187 static void write_labels(pstops_doc_t
*doc
, int orient
);
188 static void write_options(pstops_doc_t
*doc
, ppd_file_t
*ppd
,
189 int num_options
, cups_option_t
*options
);
193 * 'main()' - Main entry.
196 int /* O - Exit status */
197 main(int argc
, /* I - Number of command-line args */
198 char *argv
[]) /* I - Command-line arguments */
200 pstops_doc_t doc
; /* Document information */
201 cups_file_t
*fp
; /* Print file */
202 ppd_file_t
*ppd
; /* PPD file */
203 int num_options
; /* Number of print options */
204 cups_option_t
*options
; /* Print options */
205 char line
[8192]; /* Line buffer */
206 ssize_t len
; /* Length of line buffer */
207 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
208 struct sigaction action
; /* Actions for POSIX signals */
209 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
213 * Make sure status messages are not buffered...
216 setbuf(stderr
, NULL
);
219 * Ignore broken pipe signals...
222 signal(SIGPIPE
, SIG_IGN
);
225 * Check command-line...
228 if (argc
< 6 || argc
> 7)
230 _cupsLangPrintf(stderr
,
231 _("Usage: %s job-id user title copies options [file]"),
237 * Register a signal handler to cleanly cancel a job.
240 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
241 sigset(SIGTERM
, cancel_job
);
242 #elif defined(HAVE_SIGACTION)
243 memset(&action
, 0, sizeof(action
));
245 sigemptyset(&action
.sa_mask
);
246 action
.sa_handler
= cancel_job
;
247 sigaction(SIGTERM
, &action
, NULL
);
249 signal(SIGTERM
, cancel_job
);
250 #endif /* HAVE_SIGSET */
253 * If we have 7 arguments, print the file named on the command-line.
254 * Otherwise, send stdin instead...
258 fp
= cupsFileStdin();
262 * Try to open the print file...
265 if ((fp
= cupsFileOpen(argv
[6], "r")) == NULL
)
269 fprintf(stderr
, "DEBUG: Unable to open \"%s\".\n", argv
[6]);
270 _cupsLangPrintError("ERROR", _("Unable to open print file"));
278 * Read the first line to see if we have DSC comments...
281 if ((len
= (ssize_t
)cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
283 fputs("DEBUG: The print file is empty.\n", stderr
);
288 * Process command-line options...
292 num_options
= cupsParseOptions(argv
[5], 0, &options
);
293 ppd
= SetCommonOptions(num_options
, options
, 1);
295 set_pstops_options(&doc
, ppd
, argv
, num_options
, options
);
298 * Write any "exit server" options that have been selected...
301 ppdEmit(ppd
, stdout
, PPD_ORDER_EXIT
);
304 * Write any JCL commands that are needed to print PostScript code...
308 ppdEmitJCL(ppd
, stdout
, doc
.job_id
, doc
.user
, doc
.title
);
311 * Start with a DSC header...
314 puts("%!PS-Adobe-3.0");
317 * Skip leading PJL in the document...
320 while (!strncmp(line
, "\033%-12345X", 9) || !strncmp(line
, "@PJL ", 5))
323 * Yup, we have leading PJL fun, so skip it until we hit the line
324 * with "ENTER LANGUAGE"...
327 fputs("DEBUG: Skipping PJL header...\n", stderr
);
329 while (strstr(line
, "ENTER LANGUAGE") == NULL
&& strncmp(line
, "%!", 2))
330 if ((len
= (ssize_t
)cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
333 if (!strncmp(line
, "%!", 2))
336 if ((len
= (ssize_t
)cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
341 * Now see if the document conforms to the Adobe Document Structuring
345 if (!strncmp(line
, "%!PS-Adobe-", 11))
348 * Yes, filter the document...
351 copy_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
356 * No, display an error message and treat the file as if it contains
360 copy_non_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
364 * Send %%EOF as needed...
371 * End the job with the appropriate JCL command or CTRL-D...
376 if (ppd
&& ppd
->jcl_end
)
377 ppdEmitJCLEnd(ppd
, stdout
);
383 * Close files and remove the temporary file if needed...
388 cupsFileClose(doc
.temp
);
389 unlink(doc
.tempfile
);
393 cupsFreeOptions(num_options
, options
);
402 * 'add_page()' - Add a page to the pages array.
405 static pstops_page_t
* /* O - New page info object */
406 add_page(pstops_doc_t
*doc
, /* I - Document information */
407 const char *label
) /* I - Page label */
409 pstops_page_t
*pageinfo
; /* New page info object */
413 doc
->pages
= cupsArrayNew(NULL
, NULL
);
417 _cupsLangPrintError("EMERG", _("Unable to allocate memory for pages array"));
421 if ((pageinfo
= calloc(1, sizeof(pstops_page_t
))) == NULL
)
423 _cupsLangPrintError("EMERG", _("Unable to allocate memory for page info"));
427 pageinfo
->label
= strdup(label
);
428 pageinfo
->offset
= cupsFileTell(doc
->temp
);
430 cupsArrayAdd(doc
->pages
, pageinfo
);
439 * 'cancel_job()' - Flag the job as canceled.
443 cancel_job(int sig
) /* I - Signal number (unused) */
452 * 'check_range()' - Check to see if the current page is selected for
456 static int /* O - 1 if selected, 0 otherwise */
457 check_range(pstops_doc_t
*doc
, /* I - Document information */
458 int page
) /* I - Page number */
460 const char *range
; /* Pointer into range string */
461 int lower
, upper
; /* Lower and upper page numbers */
467 * See if we only print even or odd pages...
470 if (!_cups_strcasecmp(doc
->page_set
, "even") && (page
& 1))
473 if (!_cups_strcasecmp(doc
->page_set
, "odd") && !(page
& 1))
477 if (!doc
->page_ranges
)
478 return (1); /* No range, print all pages... */
480 for (range
= doc
->page_ranges
; *range
!= '\0';)
486 upper
= (int)strtol(range
, (char **)&range
, 10);
490 lower
= (int)strtol(range
, (char **)&range
, 10);
495 if (!isdigit(*range
& 255))
498 upper
= (int)strtol(range
, (char **)&range
, 10);
504 if (page
>= lower
&& page
<= upper
)
518 * 'copy_bytes()' - Copy bytes from the input file to stdout.
522 copy_bytes(cups_file_t
*fp
, /* I - File to read from */
523 off_t offset
, /* I - Offset to page data */
524 size_t length
) /* I - Length of page data */
526 char buffer
[8192]; /* Data buffer */
527 ssize_t nbytes
; /* Number of bytes read */
528 size_t nleft
; /* Number of bytes left/remaining */
533 if (cupsFileSeek(fp
, offset
) < 0)
535 _cupsLangPrintError("ERROR", _("Unable to see in file"));
539 while (nleft
> 0 || length
== 0)
541 if (nleft
> sizeof(buffer
) || length
== 0)
542 nbytes
= sizeof(buffer
);
544 nbytes
= (ssize_t
)nleft
;
546 if ((nbytes
= cupsFileRead(fp
, buffer
, (size_t)nbytes
)) < 1)
549 nleft
-= (size_t)nbytes
;
551 fwrite(buffer
, 1, (size_t)nbytes
, stdout
);
557 * 'copy_comments()' - Copy all of the comments section.
559 * This function expects "line" to be filled with a comment line.
560 * On return, "line" will contain the next line in the file, if any.
563 static ssize_t
/* O - Length of next line */
564 copy_comments(cups_file_t
*fp
, /* I - File to read from */
565 pstops_doc_t
*doc
, /* I - Document info */
566 ppd_file_t
*ppd
, /* I - PPD file */
567 char *line
, /* I - Line buffer */
568 ssize_t linelen
, /* I - Length of initial line */
569 size_t linesize
) /* I - Size of line buffer */
571 int saw_bounding_box
, /* Saw %%BoundingBox: comment? */
572 saw_for
, /* Saw %%For: comment? */
573 saw_pages
, /* Saw %%Pages: comment? */
574 saw_title
; /* Saw %%Title: comment? */
578 * Loop until we see %%EndComments or a non-comment line...
581 saw_bounding_box
= 0;
586 while (line
[0] == '%')
589 * Strip trailing whitespace...
596 if (!isspace(line
[linelen
] & 255))
599 line
[linelen
] = '\0';
606 fprintf(stderr
, "DEBUG: %s\n", line
);
609 * Pull the headers out...
612 if (!strncmp(line
, "%%Pages:", 8))
614 int pages
; /* Number of pages */
617 fputs("DEBUG: A duplicate %%Pages: comment was seen.\n", stderr
);
621 if (Duplex
&& (pages
= atoi(line
+ 8)) > 0 && pages
<= doc
->number_up
)
624 * Since we will only be printing on a single page, disable duplexing.
628 doc
->slow_duplex
= 0;
630 if (cupsGetOption("sides", doc
->num_options
, doc
->options
))
631 doc
->num_options
= cupsAddOption("sides", "one-sided",
632 doc
->num_options
, &(doc
->options
));
634 if (cupsGetOption("Duplex", doc
->num_options
, doc
->options
))
635 doc
->num_options
= cupsAddOption("Duplex", "None",
636 doc
->num_options
, &(doc
->options
));
638 if (cupsGetOption("EFDuplex", doc
->num_options
, doc
->options
))
639 doc
->num_options
= cupsAddOption("EFDuplex", "None",
640 doc
->num_options
, &(doc
->options
));
642 if (cupsGetOption("EFDuplexing", doc
->num_options
, doc
->options
))
643 doc
->num_options
= cupsAddOption("EFDuplexing", "False",
644 doc
->num_options
, &(doc
->options
));
646 if (cupsGetOption("KD03Duplex", doc
->num_options
, doc
->options
))
647 doc
->num_options
= cupsAddOption("KD03Duplex", "None",
648 doc
->num_options
, &(doc
->options
));
650 if (cupsGetOption("JCLDuplex", doc
->num_options
, doc
->options
))
651 doc
->num_options
= cupsAddOption("JCLDuplex", "None",
652 doc
->num_options
, &(doc
->options
));
654 ppdMarkOption(ppd
, "Duplex", "None");
655 ppdMarkOption(ppd
, "EFDuplex", "None");
656 ppdMarkOption(ppd
, "EFDuplexing", "False");
657 ppdMarkOption(ppd
, "KD03Duplex", "None");
658 ppdMarkOption(ppd
, "JCLDuplex", "None");
661 else if (!strncmp(line
, "%%BoundingBox:", 14))
663 if (saw_bounding_box
)
664 fputs("DEBUG: A duplicate %%BoundingBox: comment was seen.\n", stderr
);
665 else if (strstr(line
+ 14, "(atend)"))
668 * Do nothing for now but use the default imageable area...
671 else if (sscanf(line
+ 14, "%d%d%d%d", doc
->bounding_box
+ 0,
672 doc
->bounding_box
+ 1, doc
->bounding_box
+ 2,
673 doc
->bounding_box
+ 3) != 4)
675 fputs("DEBUG: A bad %%BoundingBox: comment was seen.\n", stderr
);
677 doc
->bounding_box
[0] = (int)PageLeft
;
678 doc
->bounding_box
[1] = (int)PageBottom
;
679 doc
->bounding_box
[2] = (int)PageRight
;
680 doc
->bounding_box
[3] = (int)PageTop
;
683 saw_bounding_box
= 1;
685 else if (!strncmp(line
, "%%For:", 6))
688 doc_printf(doc
, "%s\n", line
);
690 else if (!strncmp(line
, "%%Title:", 8))
693 doc_printf(doc
, "%s\n", line
);
695 else if (!strncmp(line
, "%cupsRotation:", 14))
698 * Reset orientation of document?
701 int orient
= (atoi(line
+ 14) / 90) & 3;
703 if (orient
!= Orientation
)
706 * Yes, update things so that the pages come out right...
709 Orientation
= (4 - Orientation
+ orient
) & 3;
711 Orientation
= orient
;
714 else if (!strcmp(line
, "%%EndComments"))
716 linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
);
719 else if (strncmp(line
, "%!", 2) && strncmp(line
, "%cups", 5))
720 doc_printf(doc
, "%s\n", line
);
722 if ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) == 0)
726 if (!saw_bounding_box
)
727 fputs("DEBUG: There wasn't a %%BoundingBox: comment in the header.\n",
731 fputs("DEBUG: There wasn't a %%Pages: comment in the header.\n", stderr
);
734 WriteTextComment("For", doc
->user
);
737 WriteTextComment("Title", doc
->title
);
739 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
742 * Tell the document processor the copy and duplex options
743 * that are required...
746 doc_printf(doc
, "%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
747 doc
->collate
? " collate" : "",
748 Duplex
? " duplex" : "");
751 * Apple uses RBI comments for various non-PPD options...
754 doc_printf(doc
, "%%RBINumCopies: %d\n", doc
->copies
);
759 * Tell the document processor the duplex option that is required...
763 doc_puts(doc
, "%%Requirements: duplex\n");
766 * Apple uses RBI comments for various non-PPD options...
769 doc_puts(doc
, "%RBINumCopies: 1\n");
772 doc_puts(doc
, "%%Pages: (atend)\n");
773 doc_puts(doc
, "%%BoundingBox: (atend)\n");
774 doc_puts(doc
, "%%EndComments\n");
781 * 'copy_dsc()' - Copy a DSC-conforming document.
783 * This function expects "line" to be filled with the %!PS-Adobe comment line.
787 copy_dsc(cups_file_t
*fp
, /* I - File to read from */
788 pstops_doc_t
*doc
, /* I - Document info */
789 ppd_file_t
*ppd
, /* I - PPD file */
790 char *line
, /* I - Line buffer */
791 ssize_t linelen
, /* I - Length of initial line */
792 size_t linesize
) /* I - Size of line buffer */
794 int number
; /* Page number */
795 pstops_page_t
*pageinfo
; /* Page information */
799 * Make sure we use ESPshowpage for EPS files...
802 if (strstr(line
, "EPSF"))
804 doc
->use_ESPshowpage
= 1;
809 * Start sending the document with any commands needed...
812 fprintf(stderr
, "DEBUG: Before copy_comments - %s", line
);
813 linelen
= copy_comments(fp
, doc
, ppd
, line
, linelen
, linesize
);
816 * Now find the prolog section, if any...
819 fprintf(stderr
, "DEBUG: Before copy_prolog - %s", line
);
820 linelen
= copy_prolog(fp
, doc
, ppd
, line
, linelen
, linesize
);
823 * Then the document setup section...
826 fprintf(stderr
, "DEBUG: Before copy_setup - %s", line
);
827 linelen
= copy_setup(fp
, doc
, ppd
, line
, linelen
, linesize
);
830 * Copy until we see %%Page:...
833 while (strncmp(line
, "%%Page:", 7) && strncmp(line
, "%%Trailer", 9))
835 doc_write(doc
, line
, (size_t)linelen
);
837 if ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) == 0)
842 * Then process pages until we have no more...
847 fprintf(stderr
, "DEBUG: Before page loop - %s", line
);
848 while (!strncmp(line
, "%%Page:", 7))
855 if (check_range(doc
, (number
- 1) / doc
->number_up
+ 1))
857 fprintf(stderr
, "DEBUG: Copying page %d...\n", number
);
858 linelen
= copy_page(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
862 fprintf(stderr
, "DEBUG: Skipping page %d...\n", number
);
863 linelen
= skip_page(fp
, line
, linelen
, linesize
);
868 * Finish up the last page(s)...
871 if (number
&& is_not_last_page(number
) && cupsArrayLast(doc
->pages
) &&
872 check_range(doc
, (number
- 1) / doc
->number_up
+ 1))
874 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
876 start_nup(doc
, doc
->number_up
, 0, doc
->bounding_box
);
877 doc_puts(doc
, "showpage\n");
878 end_nup(doc
, doc
->number_up
);
880 pageinfo
->length
= (ssize_t
)(cupsFileTell(doc
->temp
) - pageinfo
->offset
);
883 if (doc
->slow_duplex
&& (doc
->page
& 1))
886 * Make sure we have an even number of pages...
889 pageinfo
= add_page(doc
, "(filler)");
891 if (!doc
->slow_order
)
893 if (!ppd
|| !ppd
->num_filters
)
894 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
895 doc
->slow_collate
? 1 : doc
->copies
);
897 printf("%%%%Page: (filler) %d\n", doc
->page
);
900 start_nup(doc
, doc
->number_up
, 0, doc
->bounding_box
);
901 doc_puts(doc
, "showpage\n");
902 end_nup(doc
, doc
->number_up
);
904 pageinfo
->length
= (ssize_t
)(cupsFileTell(doc
->temp
) - pageinfo
->offset
);
908 * Make additional copies as necessary...
911 number
= doc
->slow_order
? 0 : doc
->page
;
913 if (doc
->temp
&& !JobCanceled
&& cupsArrayCount(doc
->pages
) > 0)
915 int copy
; /* Current copy */
919 * Reopen the temporary file for reading...
922 cupsFileClose(doc
->temp
);
924 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
930 if (doc
->slow_collate
)
931 copy
= !doc
->slow_order
;
933 copy
= doc
->copies
- 1;
935 for (; copy
< doc
->copies
; copy
++)
941 * Send end-of-job stuff followed by any start-of-job stuff required
942 * for the JCL options...
945 if (number
&& doc
->emit_jcl
&& ppd
&& ppd
->jcl_end
)
948 * Send the trailer...
952 printf("%%%%Pages: %d\n", cupsArrayCount(doc
->pages
));
953 if (doc
->number_up
> 1 || doc
->fit_to_page
)
954 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
955 PageLeft
, PageBottom
, PageRight
, PageTop
);
957 printf("%%%%BoundingBox: %d %d %d %d\n",
958 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
959 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
963 * Start a new document...
966 ppdEmitJCLEnd(ppd
, stdout
);
967 ppdEmitJCL(ppd
, stdout
, doc
->job_id
, doc
->user
, doc
->title
);
969 puts("%!PS-Adobe-3.0");
975 * Copy the prolog as needed...
980 pageinfo
= (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
981 copy_bytes(doc
->temp
, 0, (size_t)pageinfo
->offset
);
985 * Then copy all of the pages...
988 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayLast(doc
->pages
) :
989 (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
998 if (!ppd
|| !ppd
->num_filters
)
999 fprintf(stderr
, "PAGE: %d %d\n", number
,
1000 doc
->slow_collate
? 1 : doc
->copies
);
1002 if (doc
->number_up
> 1)
1004 printf("%%%%Page: (%d) %d\n", number
, number
);
1005 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1006 PageLeft
, PageBottom
, PageRight
, PageTop
);
1010 printf("%%%%Page: %s %d\n", pageinfo
->label
, number
);
1011 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1012 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
1013 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
1016 copy_bytes(doc
->temp
, pageinfo
->offset
, (size_t)pageinfo
->length
);
1018 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayPrev(doc
->pages
) :
1019 (pstops_page_t
*)cupsArrayNext(doc
->pages
);
1025 * Restore the old showpage operator as needed...
1028 if (doc
->use_ESPshowpage
)
1029 puts("userdict/showpage/ESPshowpage load put\n");
1032 * Write/copy the trailer...
1036 copy_trailer(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
1041 * 'copy_non_dsc()' - Copy a document that does not conform to the DSC.
1043 * This function expects "line" to be filled with the %! comment line.
1047 copy_non_dsc(cups_file_t
*fp
, /* I - File to read from */
1048 pstops_doc_t
*doc
, /* I - Document info */
1049 ppd_file_t
*ppd
, /* I - PPD file */
1050 char *line
, /* I - Line buffer */
1051 ssize_t linelen
, /* I - Length of initial line */
1052 size_t linesize
) /* I - Size of line buffer */
1054 int copy
; /* Current copy */
1055 char buffer
[8192]; /* Copy buffer */
1056 ssize_t bytes
; /* Number of bytes copied */
1062 * First let the user know that they are attempting to print a file
1063 * that may not print correctly...
1066 fputs("DEBUG: This document does not conform to the Adobe Document "
1067 "Structuring Conventions and may not print correctly.\n", stderr
);
1070 * Then write a standard DSC comment section...
1073 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", PageLeft
, PageBottom
,
1074 PageRight
, PageTop
);
1076 if (doc
->slow_collate
&& doc
->copies
> 1)
1077 printf("%%%%Pages: %d\n", doc
->copies
);
1081 WriteTextComment("For", doc
->user
);
1082 WriteTextComment("Title", doc
->title
);
1084 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1087 * Tell the document processor the copy and duplex options
1088 * that are required...
1091 printf("%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
1092 doc
->collate
? " collate" : "",
1093 Duplex
? " duplex" : "");
1096 * Apple uses RBI comments for various non-PPD options...
1099 printf("%%RBINumCopies: %d\n", doc
->copies
);
1104 * Tell the document processor the duplex option that is required...
1108 puts("%%Requirements: duplex");
1111 * Apple uses RBI comments for various non-PPD options...
1114 puts("%RBINumCopies: 1");
1117 puts("%%EndComments");
1120 * Then the prolog...
1123 puts("%%BeginProlog");
1125 do_prolog(doc
, ppd
);
1127 puts("%%EndProlog");
1130 * Then the setup section...
1133 puts("%%BeginSetup");
1140 * Finally, embed a copy of the file inside a %%Page...
1143 if (!ppd
|| !ppd
->num_filters
)
1144 fprintf(stderr
, "PAGE: 1 %d\n", doc
->temp
? 1 : doc
->copies
);
1146 puts("%%Page: 1 1");
1147 puts("%%BeginPageSetup");
1148 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1149 puts("%%EndPageSetup");
1150 puts("%%BeginDocument: nondsc");
1152 fwrite(line
, (size_t)linelen
, 1, stdout
);
1155 cupsFileWrite(doc
->temp
, line
, (size_t)linelen
);
1157 while ((bytes
= cupsFileRead(fp
, buffer
, sizeof(buffer
))) > 0)
1159 fwrite(buffer
, 1, (size_t)bytes
, stdout
);
1162 cupsFileWrite(doc
->temp
, buffer
, (size_t)bytes
);
1165 puts("%%EndDocument");
1167 if (doc
->use_ESPshowpage
)
1169 WriteLabels(Orientation
);
1170 puts("ESPshowpage");
1173 if (doc
->temp
&& !JobCanceled
)
1176 * Reopen the temporary file for reading...
1179 cupsFileClose(doc
->temp
);
1181 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
1184 * Make the additional copies as needed...
1187 for (copy
= 1; copy
< doc
->copies
; copy
++)
1192 if (!ppd
|| !ppd
->num_filters
)
1193 fputs("PAGE: 1 1\n", stderr
);
1195 printf("%%%%Page: %d %d\n", copy
+ 1, copy
+ 1);
1196 puts("%%BeginPageSetup");
1197 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1198 puts("%%EndPageSetup");
1199 puts("%%BeginDocument: nondsc");
1201 copy_bytes(doc
->temp
, 0, 0);
1203 puts("%%EndDocument");
1205 if (doc
->use_ESPshowpage
)
1207 WriteLabels(Orientation
);
1208 puts("ESPshowpage");
1214 * Restore the old showpage operator as needed...
1217 if (doc
->use_ESPshowpage
)
1218 puts("userdict/showpage/ESPshowpage load put\n");
1223 * 'copy_page()' - Copy a page description.
1225 * This function expects "line" to be filled with a %%Page comment line.
1226 * On return, "line" will contain the next line in the file, if any.
1229 static ssize_t
/* O - Length of next line */
1230 copy_page(cups_file_t
*fp
, /* I - File to read from */
1231 pstops_doc_t
*doc
, /* I - Document info */
1232 ppd_file_t
*ppd
, /* I - PPD file */
1233 int number
, /* I - Current page number */
1234 char *line
, /* I - Line buffer */
1235 ssize_t linelen
, /* I - Length of initial line */
1236 size_t linesize
) /* I - Size of line buffer */
1238 char label
[256], /* Page label string */
1239 *ptr
; /* Pointer into line */
1240 int level
; /* Embedded document level */
1241 pstops_page_t
*pageinfo
; /* Page information */
1242 int first_page
; /* First page on N-up output? */
1243 int has_page_setup
= 0; /* Does the page have %%Begin/EndPageSetup? */
1244 int bounding_box
[4]; /* PageBoundingBox */
1248 * Get the page label for this page...
1251 first_page
= is_first_page(number
);
1253 if (!parse_text(line
+ 7, &ptr
, label
, sizeof(label
)))
1255 fputs("DEBUG: There was a bad %%Page: comment in the file.\n", stderr
);
1259 else if (strtol(ptr
, &ptr
, 10) == LONG_MAX
|| !isspace(*ptr
& 255))
1261 fputs("DEBUG: There was a bad %%Page: comment in the file.\n", stderr
);
1266 * Create or update the current output page...
1270 pageinfo
= add_page(doc
, label
);
1272 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
1275 * Handle first page override...
1278 if (doc
->ap_input_slot
|| doc
->ap_manual_feed
)
1280 if ((doc
->page
== 1 && (!doc
->slow_order
|| !Duplex
)) ||
1281 (doc
->page
== 2 && doc
->slow_order
&& Duplex
))
1284 * First page/sheet gets AP_FIRSTPAGE_* options...
1287 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->ap_input_slot
,
1288 pageinfo
->num_options
,
1289 &(pageinfo
->options
));
1290 pageinfo
->num_options
= cupsAddOption("ManualFeed",
1291 doc
->ap_input_slot
? "False" :
1292 doc
->ap_manual_feed
,
1293 pageinfo
->num_options
,
1294 &(pageinfo
->options
));
1295 pageinfo
->num_options
= cupsAddOption("MediaColor", doc
->ap_media_color
,
1296 pageinfo
->num_options
,
1297 &(pageinfo
->options
));
1298 pageinfo
->num_options
= cupsAddOption("MediaType", doc
->ap_media_type
,
1299 pageinfo
->num_options
,
1300 &(pageinfo
->options
));
1301 pageinfo
->num_options
= cupsAddOption("PageRegion", doc
->ap_page_region
,
1302 pageinfo
->num_options
,
1303 &(pageinfo
->options
));
1304 pageinfo
->num_options
= cupsAddOption("PageSize", doc
->ap_page_size
,
1305 pageinfo
->num_options
,
1306 &(pageinfo
->options
));
1308 else if (doc
->page
== (Duplex
+ 2))
1311 * Second page/sheet gets default options...
1314 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->input_slot
,
1315 pageinfo
->num_options
,
1316 &(pageinfo
->options
));
1317 pageinfo
->num_options
= cupsAddOption("ManualFeed",
1318 doc
->input_slot
? "False" :
1320 pageinfo
->num_options
,
1321 &(pageinfo
->options
));
1322 pageinfo
->num_options
= cupsAddOption("MediaColor", doc
->media_color
,
1323 pageinfo
->num_options
,
1324 &(pageinfo
->options
));
1325 pageinfo
->num_options
= cupsAddOption("MediaType", doc
->media_type
,
1326 pageinfo
->num_options
,
1327 &(pageinfo
->options
));
1328 pageinfo
->num_options
= cupsAddOption("PageRegion", doc
->page_region
,
1329 pageinfo
->num_options
,
1330 &(pageinfo
->options
));
1331 pageinfo
->num_options
= cupsAddOption("PageSize", doc
->page_size
,
1332 pageinfo
->num_options
,
1333 &(pageinfo
->options
));
1338 * Scan comments until we see something other than %%Page*: or
1342 memcpy(bounding_box
, doc
->bounding_box
, sizeof(bounding_box
));
1344 while ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) > 0)
1346 if (!strncmp(line
, "%%PageBoundingBox:", 18))
1349 * %%PageBoundingBox: llx lly urx ury
1352 if (sscanf(line
+ 18, "%d%d%d%d", bounding_box
+ 0,
1353 bounding_box
+ 1, bounding_box
+ 2,
1354 bounding_box
+ 3) != 4)
1356 fputs("DEBUG: There was a bad %%PageBoundingBox: comment in the file.\n", stderr
);
1357 memcpy(bounding_box
, doc
->bounding_box
,
1358 sizeof(bounding_box
));
1360 else if (doc
->number_up
== 1 && !doc
->fit_to_page
&& Orientation
)
1362 int temp_bbox
[4]; /* Temporary bounding box */
1365 memcpy(temp_bbox
, bounding_box
, sizeof(temp_bbox
));
1367 fprintf(stderr
, "DEBUG: Orientation = %d\n", Orientation
);
1368 fprintf(stderr
, "DEBUG: original bounding_box = [ %d %d %d %d ]\n",
1369 bounding_box
[0], bounding_box
[1],
1370 bounding_box
[2], bounding_box
[3]);
1371 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
1372 PageWidth
, PageLength
);
1374 switch (Orientation
)
1376 case 1 : /* Landscape */
1377 bounding_box
[0] = (int)(PageLength
- temp_bbox
[3]);
1378 bounding_box
[1] = temp_bbox
[0];
1379 bounding_box
[2] = (int)(PageLength
- temp_bbox
[1]);
1380 bounding_box
[3] = temp_bbox
[2];
1383 case 2 : /* Reverse Portrait */
1384 bounding_box
[0] = (int)(PageWidth
- temp_bbox
[2]);
1385 bounding_box
[1] = (int)(PageLength
- temp_bbox
[3]);
1386 bounding_box
[2] = (int)(PageWidth
- temp_bbox
[0]);
1387 bounding_box
[3] = (int)(PageLength
- temp_bbox
[1]);
1390 case 3 : /* Reverse Landscape */
1391 bounding_box
[0] = temp_bbox
[1];
1392 bounding_box
[1] = (int)(PageWidth
- temp_bbox
[2]);
1393 bounding_box
[2] = temp_bbox
[3];
1394 bounding_box
[3] = (int)(PageWidth
- temp_bbox
[0]);
1398 fprintf(stderr
, "DEBUG: updated bounding_box = [ %d %d %d %d ]\n",
1399 bounding_box
[0], bounding_box
[1],
1400 bounding_box
[2], bounding_box
[3]);
1404 else if (!strncmp(line
, "%%PageCustomColors:", 19) ||
1405 !strncmp(line
, "%%PageMedia:", 12) ||
1406 !strncmp(line
, "%%PageOrientation:", 18) ||
1407 !strncmp(line
, "%%PageProcessColors:", 20) ||
1408 !strncmp(line
, "%%PageRequirements:", 18) ||
1409 !strncmp(line
, "%%PageResources:", 16))
1416 else if (!strncmp(line
, "%%PageCustomColors:", 19))
1419 * %%PageCustomColors: ...
1422 else if (!strncmp(line
, "%%PageMedia:", 12))
1428 else if (!strncmp(line
, "%%PageOrientation:", 18))
1431 * %%PageOrientation: ...
1434 else if (!strncmp(line
, "%%PageProcessColors:", 20))
1437 * %%PageProcessColors: ...
1440 else if (!strncmp(line
, "%%PageRequirements:", 18))
1443 * %%PageRequirements: ...
1446 else if (!strncmp(line
, "%%PageResources:", 16))
1449 * %%PageResources: ...
1452 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1455 * %%IncludeFeature: *MainKeyword OptionKeyword
1458 if (doc
->number_up
== 1 &&!doc
->fit_to_page
)
1459 pageinfo
->num_options
= include_feature(ppd
, line
,
1460 pageinfo
->num_options
,
1461 &(pageinfo
->options
));
1463 else if (!strncmp(line
, "%%BeginPageSetup", 16))
1472 if (doc
->number_up
== 1)
1475 * Update the document's composite and page bounding box...
1478 memcpy(pageinfo
->bounding_box
, bounding_box
,
1479 sizeof(pageinfo
->bounding_box
));
1481 if (bounding_box
[0] < doc
->new_bounding_box
[0])
1482 doc
->new_bounding_box
[0] = bounding_box
[0];
1483 if (bounding_box
[1] < doc
->new_bounding_box
[1])
1484 doc
->new_bounding_box
[1] = bounding_box
[1];
1485 if (bounding_box
[2] > doc
->new_bounding_box
[2])
1486 doc
->new_bounding_box
[2] = bounding_box
[2];
1487 if (bounding_box
[3] > doc
->new_bounding_box
[3])
1488 doc
->new_bounding_box
[3] = bounding_box
[3];
1492 * Output the page header as needed...
1495 if (!doc
->slow_order
&& first_page
)
1497 if (!ppd
|| !ppd
->num_filters
)
1498 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
1499 doc
->slow_collate
? 1 : doc
->copies
);
1501 if (doc
->number_up
> 1)
1503 printf("%%%%Page: (%d) %d\n", doc
->page
, doc
->page
);
1504 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1505 PageLeft
, PageBottom
, PageRight
, PageTop
);
1509 printf("%%%%Page: %s %d\n", pageinfo
->label
, doc
->page
);
1510 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1511 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
1512 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
1517 * Copy any page setup commands...
1521 doc_puts(doc
, "%%BeginPageSetup\n");
1525 int feature
= 0; /* In a Begin/EndFeature block? */
1527 while ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) > 0)
1529 if (!strncmp(line
, "%%EndPageSetup", 14))
1531 else if (!strncmp(line
, "%%BeginFeature:", 15))
1535 if (doc
->number_up
> 1 || doc
->fit_to_page
)
1538 else if (!strncmp(line
, "%%EndFeature", 12))
1542 if (doc
->number_up
> 1 || doc
->fit_to_page
)
1545 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1547 pageinfo
->num_options
= include_feature(ppd
, line
,
1548 pageinfo
->num_options
,
1549 &(pageinfo
->options
));
1552 else if (!strncmp(line
, "%%Include", 9))
1555 if (line
[0] != '%' && !feature
)
1558 if (!feature
|| (doc
->number_up
== 1 && !doc
->fit_to_page
))
1559 doc_write(doc
, line
, (size_t)linelen
);
1563 * Skip %%EndPageSetup...
1566 if (linelen
> 0 && !strncmp(line
, "%%EndPageSetup", 14))
1567 linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
);
1572 char *page_setup
; /* PageSetup commands to send */
1575 if (pageinfo
->num_options
> 0)
1576 write_options(doc
, ppd
, pageinfo
->num_options
, pageinfo
->options
);
1579 * Output commands for the current page...
1582 page_setup
= ppdEmitString(ppd
, PPD_ORDER_PAGE
, 0);
1586 doc_puts(doc
, page_setup
);
1592 * Prep for the start of the page description...
1595 start_nup(doc
, number
, 1, bounding_box
);
1598 doc_puts(doc
, "%%EndPageSetup\n");
1601 * Read the rest of the page description...
1609 (!strncmp(line
, "%%Page:", 7) ||
1610 !strncmp(line
, "%%Trailer", 9) ||
1611 !strncmp(line
, "%%EOF", 5)))
1613 else if (!strncmp(line
, "%%BeginDocument", 15) ||
1614 !strncmp(line
, "%ADO_BeginApplication", 21))
1616 doc_write(doc
, line
, (size_t)linelen
);
1620 else if ((!strncmp(line
, "%%EndDocument", 13) ||
1621 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
1623 doc_write(doc
, line
, (size_t)linelen
);
1627 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
1628 (!strncmp(line
, "%%BeginData:", 12) &&
1629 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
1632 * Copy binary data...
1635 int bytes
; /* Bytes of data */
1638 doc_write(doc
, line
, (size_t)linelen
);
1640 bytes
= atoi(strchr(line
, ':') + 1);
1644 if ((size_t)bytes
> linesize
)
1645 linelen
= cupsFileRead(fp
, line
, linesize
);
1647 linelen
= cupsFileRead(fp
, line
, (size_t)bytes
);
1652 perror("ERROR: Early end-of-file while reading binary data");
1656 doc_write(doc
, line
, (size_t)linelen
);
1662 doc_write(doc
, line
, (size_t)linelen
);
1664 while ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) > 0);
1667 * Finish up this page and return...
1670 end_nup(doc
, number
);
1672 pageinfo
->length
= (ssize_t
)(cupsFileTell(doc
->temp
) - pageinfo
->offset
);
1679 * 'copy_prolog()' - Copy the document prolog section.
1681 * This function expects "line" to be filled with a %%BeginProlog comment line.
1682 * On return, "line" will contain the next line in the file, if any.
1685 static ssize_t
/* O - Length of next line */
1686 copy_prolog(cups_file_t
*fp
, /* I - File to read from */
1687 pstops_doc_t
*doc
, /* I - Document info */
1688 ppd_file_t
*ppd
, /* I - PPD file */
1689 char *line
, /* I - Line buffer */
1690 ssize_t linelen
, /* I - Length of initial line */
1691 size_t linesize
) /* I - Size of line buffer */
1693 while (strncmp(line
, "%%BeginProlog", 13))
1695 if (!strncmp(line
, "%%BeginSetup", 12) || !strncmp(line
, "%%Page:", 7))
1698 doc_write(doc
, line
, (size_t)linelen
);
1700 if ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) == 0)
1704 doc_puts(doc
, "%%BeginProlog\n");
1706 do_prolog(doc
, ppd
);
1708 if (!strncmp(line
, "%%BeginProlog", 13))
1710 while ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) > 0)
1712 if (!strncmp(line
, "%%EndProlog", 11) ||
1713 !strncmp(line
, "%%BeginSetup", 12) ||
1714 !strncmp(line
, "%%Page:", 7))
1717 doc_write(doc
, line
, (size_t)linelen
);
1720 if (!strncmp(line
, "%%EndProlog", 11))
1721 linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
);
1723 fputs("DEBUG: The %%EndProlog comment is missing.\n", stderr
);
1726 doc_puts(doc
, "%%EndProlog\n");
1733 * 'copy_setup()' - Copy the document setup section.
1735 * This function expects "line" to be filled with a %%BeginSetup comment line.
1736 * On return, "line" will contain the next line in the file, if any.
1739 static ssize_t
/* O - Length of next line */
1740 copy_setup(cups_file_t
*fp
, /* I - File to read from */
1741 pstops_doc_t
*doc
, /* I - Document info */
1742 ppd_file_t
*ppd
, /* I - PPD file */
1743 char *line
, /* I - Line buffer */
1744 ssize_t linelen
, /* I - Length of initial line */
1745 size_t linesize
) /* I - Size of line buffer */
1747 int num_options
; /* Number of options */
1748 cups_option_t
*options
; /* Options */
1751 while (strncmp(line
, "%%BeginSetup", 12))
1753 if (!strncmp(line
, "%%Page:", 7))
1756 doc_write(doc
, line
, (size_t)linelen
);
1758 if ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) == 0)
1762 doc_puts(doc
, "%%BeginSetup\n");
1769 if (!strncmp(line
, "%%BeginSetup", 12))
1771 while (strncmp(line
, "%%EndSetup", 10))
1773 if (!strncmp(line
, "%%Page:", 7))
1775 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1778 * %%IncludeFeature: *MainKeyword OptionKeyword
1781 if (doc
->number_up
== 1 && !doc
->fit_to_page
)
1782 num_options
= include_feature(ppd
, line
, num_options
, &options
);
1784 else if (strncmp(line
, "%%BeginSetup", 12))
1785 doc_write(doc
, line
, (size_t)linelen
);
1787 if ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) == 0)
1791 if (!strncmp(line
, "%%EndSetup", 10))
1792 linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
);
1794 fputs("DEBUG: The %%EndSetup comment is missing.\n", stderr
);
1797 if (num_options
> 0)
1799 write_options(doc
, ppd
, num_options
, options
);
1800 cupsFreeOptions(num_options
, options
);
1803 doc_puts(doc
, "%%EndSetup\n");
1810 * 'copy_trailer()' - Copy the document trailer.
1812 * This function expects "line" to be filled with a %%Trailer comment line.
1813 * On return, "line" will contain the next line in the file, if any.
1816 static ssize_t
/* O - Length of next line */
1817 copy_trailer(cups_file_t
*fp
, /* I - File to read from */
1818 pstops_doc_t
*doc
, /* I - Document info */
1819 ppd_file_t
*ppd
, /* I - PPD file */
1820 int number
, /* I - Number of pages */
1821 char *line
, /* I - Line buffer */
1822 ssize_t linelen
, /* I - Length of initial line */
1823 size_t linesize
) /* I - Size of line buffer */
1826 * Write the trailer comments...
1835 if (!strncmp(line
, "%%EOF", 5))
1837 else if (strncmp(line
, "%%Trailer", 9) &&
1838 strncmp(line
, "%%Pages:", 8) &&
1839 strncmp(line
, "%%BoundingBox:", 14))
1840 fwrite(line
, 1, (size_t)linelen
, stdout
);
1842 linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
);
1845 fprintf(stderr
, "DEBUG: Wrote %d pages...\n", number
);
1847 printf("%%%%Pages: %d\n", number
);
1848 if (doc
->number_up
> 1 || doc
->fit_to_page
)
1849 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
1850 PageLeft
, PageBottom
, PageRight
, PageTop
);
1852 printf("%%%%BoundingBox: %d %d %d %d\n",
1853 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
1854 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
1861 * 'do_prolog()' - Send the necessary document prolog commands.
1865 do_prolog(pstops_doc_t
*doc
, /* I - Document information */
1866 ppd_file_t
*ppd
) /* I - PPD file */
1868 char *ps
; /* PS commands */
1872 * Send the document prolog commands...
1875 if (ppd
&& ppd
->patches
)
1877 doc_puts(doc
, "%%BeginFeature: *JobPatchFile 1\n");
1878 doc_puts(doc
, ppd
->patches
);
1879 doc_puts(doc
, "\n%%EndFeature\n");
1882 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_PROLOG
, 0.0)) != NULL
)
1889 * Define ESPshowpage here so that applications that define their
1890 * own procedure to do a showpage pick it up...
1893 if (doc
->use_ESPshowpage
)
1894 doc_puts(doc
, "userdict/ESPshowpage/showpage load put\n"
1895 "userdict/showpage{}put\n");
1900 * 'do_setup()' - Send the necessary document setup commands.
1904 do_setup(pstops_doc_t
*doc
, /* I - Document information */
1905 ppd_file_t
*ppd
) /* I - PPD file */
1907 char *ps
; /* PS commands */
1911 * Disable CTRL-D so that embedded files don't cause printing
1915 doc_puts(doc
, "% Disable CTRL-D as an end-of-file marker...\n");
1916 doc_puts(doc
, "userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put\n");
1919 * Mark job options...
1922 cupsMarkOptions(ppd
, doc
->num_options
, doc
->options
);
1925 * Send all the printer-specific setup commands...
1928 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, 0.0)) != NULL
)
1934 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_ANY
, 0.0)) != NULL
)
1941 * Set the number of copies for the job...
1944 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1946 doc_printf(doc
, "%%RBIBeginNonPPDFeature: *NumCopies %d\n", doc
->copies
);
1948 "%d/languagelevel where{pop languagelevel 2 ge}{false}ifelse\n"
1949 "{1 dict begin/NumCopies exch def currentdict end "
1951 "{userdict/#copies 3 -1 roll put}ifelse\n", doc
->copies
);
1952 doc_puts(doc
, "%RBIEndNonPPDFeature\n");
1956 * If we are doing N-up printing, disable setpagedevice...
1959 if (doc
->number_up
> 1)
1961 doc_puts(doc
, "userdict/CUPSsetpagedevice/setpagedevice load put\n");
1962 doc_puts(doc
, "userdict/setpagedevice{pop}bind put\n");
1966 * Make sure we have rectclip and rectstroke procedures of some sort...
1970 "% x y w h ESPrc - Clip to a rectangle.\n"
1971 "userdict/ESPrc/rectclip where{pop/rectclip load}\n"
1972 "{{newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
1973 "neg 0 rlineto closepath clip newpath}bind}ifelse put\n");
1976 "% x y w h ESPrf - Fill a rectangle.\n"
1977 "userdict/ESPrf/rectfill where{pop/rectfill load}\n"
1978 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
1979 "neg 0 rlineto closepath fill grestore}bind}ifelse put\n");
1982 "% x y w h ESPrs - Stroke a rectangle.\n"
1983 "userdict/ESPrs/rectstroke where{pop/rectstroke load}\n"
1984 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
1985 "neg 0 rlineto closepath stroke grestore}bind}ifelse put\n");
1988 * Write the page and label prologs...
1991 if (doc
->number_up
== 2 || doc
->number_up
== 6)
1994 * For 2- and 6-up output, rotate the labels to match the orientation
1998 if (Orientation
& 1)
1999 write_label_prolog(doc
, doc
->page_label
, PageBottom
,
2000 PageWidth
- PageLength
+ PageTop
, PageLength
);
2002 write_label_prolog(doc
, doc
->page_label
, PageLeft
, PageRight
,
2006 write_label_prolog(doc
, doc
->page_label
, PageBottom
, PageTop
, PageWidth
);
2011 * 'doc_printf()' - Send a formatted string to stdout and/or the temp file.
2013 * This function should be used for all page-level output that is affected
2014 * by ordering, collation, etc.
2018 doc_printf(pstops_doc_t
*doc
, /* I - Document information */
2019 const char *format
, /* I - Printf-style format string */
2020 ...) /* I - Additional arguments as needed */
2022 va_list ap
; /* Pointer to arguments */
2023 char buffer
[1024]; /* Output buffer */
2024 ssize_t bytes
; /* Number of bytes to write */
2027 va_start(ap
, format
);
2028 bytes
= vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
2031 if ((size_t)bytes
> sizeof(buffer
))
2033 _cupsLangPrintFilter(stderr
, "ERROR",
2034 _("Buffer overflow detected, aborting."));
2038 doc_write(doc
, buffer
, (size_t)bytes
);
2043 * 'doc_puts()' - Send a nul-terminated string to stdout and/or the temp file.
2045 * This function should be used for all page-level output that is affected
2046 * by ordering, collation, etc.
2050 doc_puts(pstops_doc_t
*doc
, /* I - Document information */
2051 const char *s
) /* I - String to send */
2053 doc_write(doc
, s
, strlen(s
));
2058 * 'doc_write()' - Send data to stdout and/or the temp file.
2062 doc_write(pstops_doc_t
*doc
, /* I - Document information */
2063 const char *s
, /* I - Data to send */
2064 size_t len
) /* I - Number of bytes to send */
2066 if (!doc
->slow_order
)
2067 fwrite(s
, 1, len
, stdout
);
2070 cupsFileWrite(doc
->temp
, s
, len
);
2075 * 'end_nup()' - End processing for N-up printing.
2079 end_nup(pstops_doc_t
*doc
, /* I - Document information */
2080 int number
) /* I - Page number */
2082 if (doc
->number_up
> 1)
2083 doc_puts(doc
, "userdict/ESPsave get restore\n");
2085 switch (doc
->number_up
)
2088 if (doc
->use_ESPshowpage
)
2090 write_labels(doc
, Orientation
);
2091 doc_puts(doc
, "ESPshowpage\n");
2097 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2099 if (Orientation
& 1)
2102 * Rotate the labels back to portrait...
2105 write_labels(doc
, Orientation
- 1);
2107 else if (Orientation
== 0)
2110 * Rotate the labels to landscape...
2113 write_labels(doc
, doc
->normal_landscape
? 1 : 3);
2118 * Rotate the labels to landscape...
2121 write_labels(doc
, doc
->normal_landscape
? 3 : 1);
2124 doc_puts(doc
, "ESPshowpage\n");
2129 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2131 write_labels(doc
, Orientation
);
2132 doc_puts(doc
, "ESPshowpage\n");
2142 * 'include_feature()' - Include a printer option/feature command.
2145 static int /* O - New number of options */
2147 ppd_file_t
*ppd
, /* I - PPD file */
2148 const char *line
, /* I - DSC line */
2149 int num_options
, /* I - Number of options */
2150 cups_option_t
**options
) /* IO - Options */
2152 char name
[255], /* Option name */
2153 value
[255]; /* Option value */
2154 ppd_option_t
*option
; /* Option in file */
2158 * Get the "%%IncludeFeature: *Keyword OptionKeyword" values...
2161 if (sscanf(line
+ 17, "%254s%254s", name
, value
) != 2)
2163 fputs("DEBUG: The %%IncludeFeature: comment is not valid.\n", stderr
);
2164 return (num_options
);
2168 * Find the option and choice...
2171 if ((option
= ppdFindOption(ppd
, name
+ 1)) == NULL
)
2173 _cupsLangPrintFilter(stderr
, "WARNING", _("Unknown option \"%s\"."),
2175 return (num_options
);
2178 if (option
->section
== PPD_ORDER_EXIT
||
2179 option
->section
== PPD_ORDER_JCL
)
2181 _cupsLangPrintFilter(stderr
, "WARNING",
2182 _("Option \"%s\" cannot be included via "
2183 "%%%%IncludeFeature."), name
+ 1);
2184 return (num_options
);
2187 if (!ppdFindChoice(option
, value
))
2189 _cupsLangPrintFilter(stderr
, "WARNING",
2190 _("Unknown choice \"%s\" for option \"%s\"."),
2192 return (num_options
);
2196 * Add the option to the option array and return...
2199 return (cupsAddOption(name
+ 1, value
, num_options
, options
));
2204 * 'parse_text()' - Parse a text value in a comment.
2206 * This function parses a DSC text value as defined on page 36 of the
2207 * DSC specification. Text values are either surrounded by parenthesis
2208 * or whitespace-delimited.
2210 * The value returned is the literal characters for the entire text
2211 * string, including any parenthesis and escape characters.
2214 static char * /* O - Value or NULL on error */
2215 parse_text(const char *start
, /* I - Start of text value */
2216 char **end
, /* O - End of text value */
2217 char *buffer
, /* I - Buffer */
2218 size_t bufsize
) /* I - Size of buffer */
2220 char *bufptr
, /* Pointer in buffer */
2221 *bufend
; /* End of buffer */
2222 int level
; /* Parenthesis level */
2226 * Skip leading whitespace...
2229 while (isspace(*start
& 255))
2233 * Then copy the value...
2238 bufend
= buffer
+ bufsize
- 1;
2240 while (bufptr
< bufend
)
2242 if (isspace(*start
& 255) && !level
)
2249 else if (*start
== ')')
2259 else if (*start
== '\\')
2262 * Copy escaped character...
2265 int i
; /* Looping var */
2269 i
<= 3 && isdigit(start
[i
] & 255) && bufptr
< bufend
;
2270 *bufptr
++ = start
[i
], i
++);
2279 * Return the value and new pointer into the line...
2283 *end
= (char *)start
;
2285 if (bufptr
== bufend
)
2293 * 'set_pstops_options()' - Set pstops options.
2298 pstops_doc_t
*doc
, /* I - Document information */
2299 ppd_file_t
*ppd
, /* I - PPD file */
2300 char *argv
[], /* I - Command-line arguments */
2301 int num_options
, /* I - Number of options */
2302 cups_option_t
*options
) /* I - Options */
2304 const char *val
; /* Option value */
2305 int intval
; /* Integer option value */
2306 ppd_attr_t
*attr
; /* PPD attribute */
2307 ppd_option_t
*option
; /* PPD option */
2308 ppd_choice_t
*choice
; /* PPD choice */
2309 const char *content_type
; /* Original content type */
2310 int max_copies
; /* Maximum number of copies supported */
2314 * Initialize document information structure...
2317 memset(doc
, 0, sizeof(pstops_doc_t
));
2319 doc
->job_id
= atoi(argv
[1]);
2320 doc
->user
= argv
[2];
2321 doc
->title
= argv
[3];
2322 doc
->copies
= atoi(argv
[4]);
2324 if (ppd
&& ppd
->landscape
> 0)
2325 doc
->normal_landscape
= 1;
2327 doc
->bounding_box
[0] = (int)PageLeft
;
2328 doc
->bounding_box
[1] = (int)PageBottom
;
2329 doc
->bounding_box
[2] = (int)PageRight
;
2330 doc
->bounding_box
[3] = (int)PageTop
;
2332 doc
->new_bounding_box
[0] = INT_MAX
;
2333 doc
->new_bounding_box
[1] = INT_MAX
;
2334 doc
->new_bounding_box
[2] = INT_MIN
;
2335 doc
->new_bounding_box
[3] = INT_MIN
;
2338 * AP_FIRSTPAGE_* and the corresponding non-first-page options.
2341 doc
->ap_input_slot
= cupsGetOption("AP_FIRSTPAGE_InputSlot", num_options
,
2343 doc
->ap_manual_feed
= cupsGetOption("AP_FIRSTPAGE_ManualFeed", num_options
,
2345 doc
->ap_media_color
= cupsGetOption("AP_FIRSTPAGE_MediaColor", num_options
,
2347 doc
->ap_media_type
= cupsGetOption("AP_FIRSTPAGE_MediaType", num_options
,
2349 doc
->ap_page_region
= cupsGetOption("AP_FIRSTPAGE_PageRegion", num_options
,
2351 doc
->ap_page_size
= cupsGetOption("AP_FIRSTPAGE_PageSize", num_options
,
2354 if ((choice
= ppdFindMarkedChoice(ppd
, "InputSlot")) != NULL
)
2355 doc
->input_slot
= choice
->choice
;
2356 if ((choice
= ppdFindMarkedChoice(ppd
, "ManualFeed")) != NULL
)
2357 doc
->manual_feed
= choice
->choice
;
2358 if ((choice
= ppdFindMarkedChoice(ppd
, "MediaColor")) != NULL
)
2359 doc
->media_color
= choice
->choice
;
2360 if ((choice
= ppdFindMarkedChoice(ppd
, "MediaType")) != NULL
)
2361 doc
->media_type
= choice
->choice
;
2362 if ((choice
= ppdFindMarkedChoice(ppd
, "PageRegion")) != NULL
)
2363 doc
->page_region
= choice
->choice
;
2364 if ((choice
= ppdFindMarkedChoice(ppd
, "PageSize")) != NULL
)
2365 doc
->page_size
= choice
->choice
;
2368 * collate, multiple-document-handling
2371 if ((val
= cupsGetOption("multiple-document-handling", num_options
, options
)) != NULL
)
2374 * This IPP attribute is unnecessarily complicated...
2376 * single-document, separate-documents-collated-copies, and
2377 * single-document-new-sheet all require collated copies.
2379 * separate-documents-uncollated-copies allows for uncollated copies.
2382 doc
->collate
= _cups_strcasecmp(val
, "separate-documents-uncollated-copies") != 0;
2385 if ((val
= cupsGetOption("Collate", num_options
, options
)) != NULL
&&
2386 (!_cups_strcasecmp(val
, "true") ||!_cups_strcasecmp(val
, "on") ||
2387 !_cups_strcasecmp(val
, "yes")))
2394 if ((val
= cupsGetOption("emit-jcl", num_options
, options
)) != NULL
&&
2395 (!_cups_strcasecmp(val
, "false") || !_cups_strcasecmp(val
, "off") ||
2396 !_cups_strcasecmp(val
, "no") || !strcmp(val
, "0")))
2402 * fit-to-page/ipp-attribute-fidelity
2404 * (Only for original PostScript content)
2407 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
2408 content_type
= "application/postscript";
2410 if (!_cups_strcasecmp(content_type
, "application/postscript"))
2412 if ((val
= cupsGetOption("fit-to-page", num_options
, options
)) != NULL
&&
2413 !_cups_strcasecmp(val
, "true"))
2414 doc
->fit_to_page
= 1;
2415 else if ((val
= cupsGetOption("ipp-attribute-fidelity", num_options
,
2416 options
)) != NULL
&&
2417 !_cups_strcasecmp(val
, "true"))
2418 doc
->fit_to_page
= 1;
2422 * mirror/MirrorPrint
2425 if ((choice
= ppdFindMarkedChoice(ppd
, "MirrorPrint")) != NULL
)
2427 val
= choice
->choice
;
2431 val
= cupsGetOption("mirror", num_options
, options
);
2433 if (val
&& (!_cups_strcasecmp(val
, "true") || !_cups_strcasecmp(val
, "on") ||
2434 !_cups_strcasecmp(val
, "yes")))
2441 if ((val
= cupsGetOption("number-up", num_options
, options
)) != NULL
)
2443 switch (intval
= atoi(val
))
2451 doc
->number_up
= intval
;
2454 _cupsLangPrintFilter(stderr
, "ERROR",
2455 _("Unsupported number-up value %d, using "
2456 "number-up=1."), intval
);
2468 if ((val
= cupsGetOption("number-up-layout", num_options
, options
)) != NULL
)
2470 if (!_cups_strcasecmp(val
, "lrtb"))
2471 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2472 else if (!_cups_strcasecmp(val
, "lrbt"))
2473 doc
->number_up_layout
= PSTOPS_LAYOUT_LRBT
;
2474 else if (!_cups_strcasecmp(val
, "rltb"))
2475 doc
->number_up_layout
= PSTOPS_LAYOUT_RLTB
;
2476 else if (!_cups_strcasecmp(val
, "rlbt"))
2477 doc
->number_up_layout
= PSTOPS_LAYOUT_RLBT
;
2478 else if (!_cups_strcasecmp(val
, "tblr"))
2479 doc
->number_up_layout
= PSTOPS_LAYOUT_TBLR
;
2480 else if (!_cups_strcasecmp(val
, "tbrl"))
2481 doc
->number_up_layout
= PSTOPS_LAYOUT_TBRL
;
2482 else if (!_cups_strcasecmp(val
, "btlr"))
2483 doc
->number_up_layout
= PSTOPS_LAYOUT_BTLR
;
2484 else if (!_cups_strcasecmp(val
, "btrl"))
2485 doc
->number_up_layout
= PSTOPS_LAYOUT_BTRL
;
2488 _cupsLangPrintFilter(stderr
, "ERROR",
2489 _("Unsupported number-up-layout value %s, using "
2490 "number-up-layout=lrtb."), val
);
2491 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2495 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2501 if ((val
= cupsGetOption("OutputOrder", num_options
, options
)) != NULL
)
2503 if (!_cups_strcasecmp(val
, "Reverse"))
2504 doc
->output_order
= 1;
2509 * Figure out the right default output order from the PPD file...
2512 if ((choice
= ppdFindMarkedChoice(ppd
, "OutputBin")) != NULL
&&
2513 (attr
= ppdFindAttr(ppd
, "PageStackOrder", choice
->choice
)) != NULL
&&
2515 doc
->output_order
= !_cups_strcasecmp(attr
->value
, "Reverse");
2516 else if ((attr
= ppdFindAttr(ppd
, "DefaultOutputOrder", NULL
)) != NULL
&&
2518 doc
->output_order
= !_cups_strcasecmp(attr
->value
, "Reverse");
2525 if ((val
= cupsGetOption("page-border", num_options
, options
)) != NULL
)
2527 if (!_cups_strcasecmp(val
, "none"))
2528 doc
->page_border
= PSTOPS_BORDERNONE
;
2529 else if (!_cups_strcasecmp(val
, "single"))
2530 doc
->page_border
= PSTOPS_BORDERSINGLE
;
2531 else if (!_cups_strcasecmp(val
, "single-thick"))
2532 doc
->page_border
= PSTOPS_BORDERSINGLE2
;
2533 else if (!_cups_strcasecmp(val
, "double"))
2534 doc
->page_border
= PSTOPS_BORDERDOUBLE
;
2535 else if (!_cups_strcasecmp(val
, "double-thick"))
2536 doc
->page_border
= PSTOPS_BORDERDOUBLE2
;
2539 _cupsLangPrintFilter(stderr
, "ERROR",
2540 _("Unsupported page-border value %s, using "
2541 "page-border=none."), val
);
2542 doc
->page_border
= PSTOPS_BORDERNONE
;
2546 doc
->page_border
= PSTOPS_BORDERNONE
;
2552 doc
->page_label
= cupsGetOption("page-label", num_options
, options
);
2558 doc
->page_ranges
= cupsGetOption("page-ranges", num_options
, options
);
2564 doc
->page_set
= cupsGetOption("page-set", num_options
, options
);
2567 * Now figure out if we have to force collated copies, etc.
2570 if ((attr
= ppdFindAttr(ppd
, "cupsMaxCopies", NULL
)) != NULL
)
2571 max_copies
= atoi(attr
->value
);
2572 else if (ppd
&& ppd
->manual_copies
)
2577 if (doc
->copies
> max_copies
)
2579 else if (ppd
&& ppd
->manual_copies
&& Duplex
&& doc
->copies
> 1)
2582 * Force collated copies when printing a duplexed document to
2583 * a non-PS printer that doesn't do hardware copy generation.
2584 * Otherwise the copies will end up on the front/back side of
2592 * See if we have to filter the fast or slow way...
2595 if (doc
->collate
&& doc
->copies
> 1)
2598 * See if we need to manually collate the pages...
2601 doc
->slow_collate
= 1;
2603 if (doc
->copies
<= max_copies
&&
2604 (choice
= ppdFindMarkedChoice(ppd
, "Collate")) != NULL
&&
2605 !_cups_strcasecmp(choice
->choice
, "True"))
2608 * Hardware collate option is selected, see if the option is
2609 * conflicting - if not, collate in hardware. Otherwise,
2610 * turn the hardware collate option off...
2613 if ((option
= ppdFindOption(ppd
, "Collate")) != NULL
&&
2614 !option
->conflicted
)
2615 doc
->slow_collate
= 0;
2617 ppdMarkOption(ppd
, "Collate", "False");
2621 doc
->slow_collate
= 0;
2623 if (!ppdFindOption(ppd
, "OutputOrder") && doc
->output_order
)
2624 doc
->slow_order
= 1;
2626 doc
->slow_order
= 0;
2629 (doc
->slow_collate
|| doc
->slow_order
||
2630 ((attr
= ppdFindAttr(ppd
, "cupsEvenDuplex", NULL
)) != NULL
&&
2631 attr
->value
&& !_cups_strcasecmp(attr
->value
, "true"))))
2632 doc
->slow_duplex
= 1;
2634 doc
->slow_duplex
= 0;
2637 * Create a temporary file for page data if we need to filter slowly...
2640 if (doc
->slow_order
|| doc
->slow_collate
)
2642 if ((doc
->temp
= cupsTempFile2(doc
->tempfile
,
2643 sizeof(doc
->tempfile
))) == NULL
)
2645 perror("DEBUG: Unable to create temporary file");
2651 * Figure out if we should use ESPshowpage or not...
2654 if (doc
->page_label
|| getenv("CLASSIFICATION") || doc
->number_up
> 1 ||
2658 * Yes, use ESPshowpage...
2661 doc
->use_ESPshowpage
= 1;
2664 fprintf(stderr
, "DEBUG: slow_collate=%d, slow_duplex=%d, slow_order=%d\n",
2665 doc
->slow_collate
, doc
->slow_duplex
, doc
->slow_order
);
2670 * 'skip_page()' - Skip past a page that won't be printed.
2673 static ssize_t
/* O - Length of next line */
2674 skip_page(cups_file_t
*fp
, /* I - File to read from */
2675 char *line
, /* I - Line buffer */
2676 ssize_t linelen
, /* I - Length of initial line */
2677 size_t linesize
) /* I - Size of line buffer */
2679 int level
; /* Embedded document level */
2684 while ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) > 0)
2687 (!strncmp(line
, "%%Page:", 7) || !strncmp(line
, "%%Trailer", 9)))
2689 else if (!strncmp(line
, "%%BeginDocument", 15) ||
2690 !strncmp(line
, "%ADO_BeginApplication", 21))
2692 else if ((!strncmp(line
, "%%EndDocument", 13) ||
2693 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
2695 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
2696 (!strncmp(line
, "%%BeginData:", 12) &&
2697 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
2700 * Skip binary data...
2703 ssize_t bytes
; /* Bytes of data */
2705 bytes
= atoi(strchr(line
, ':') + 1);
2709 if ((size_t)bytes
> linesize
)
2710 linelen
= (ssize_t
)cupsFileRead(fp
, line
, linesize
);
2712 linelen
= (ssize_t
)cupsFileRead(fp
, line
, (size_t)bytes
);
2717 perror("ERROR: Early end-of-file while reading binary data");
2731 * 'start_nup()' - Start processing for N-up printing.
2735 start_nup(pstops_doc_t
*doc
, /* I - Document information */
2736 int number
, /* I - Page number */
2737 int show_border
, /* I - Show the border? */
2738 const int *bounding_box
) /* I - BoundingBox value */
2740 int pos
; /* Position on page */
2741 int x
, y
; /* Relative position of subpage */
2742 double w
, l
, /* Width and length of subpage */
2743 tx
, ty
; /* Translation values for subpage */
2744 double pagew
, /* Printable width of page */
2745 pagel
; /* Printable height of page */
2746 int bboxx
, /* BoundingBox X origin */
2747 bboxy
, /* BoundingBox Y origin */
2748 bboxw
, /* BoundingBox width */
2749 bboxl
; /* BoundingBox height */
2750 double margin
= 0; /* Current margin for border */
2753 if (doc
->number_up
> 1)
2754 doc_puts(doc
, "userdict/ESPsave save put\n");
2756 pos
= (number
- 1) % doc
->number_up
;
2757 pagew
= PageRight
- PageLeft
;
2758 pagel
= PageTop
- PageBottom
;
2760 if (doc
->fit_to_page
)
2762 bboxx
= bounding_box
[0];
2763 bboxy
= bounding_box
[1];
2764 bboxw
= bounding_box
[2] - bounding_box
[0];
2765 bboxl
= bounding_box
[3] - bounding_box
[1];
2771 bboxw
= (int)PageWidth
;
2772 bboxl
= (int)PageLength
;
2775 fprintf(stderr
, "DEBUG: pagew = %.1f, pagel = %.1f\n", pagew
, pagel
);
2776 fprintf(stderr
, "DEBUG: bboxx = %d, bboxy = %d, bboxw = %d, bboxl = %d\n",
2777 bboxx
, bboxy
, bboxw
, bboxl
);
2778 fprintf(stderr
, "DEBUG: PageLeft = %.1f, PageRight = %.1f\n",
2779 PageLeft
, PageRight
);
2780 fprintf(stderr
, "DEBUG: PageTop = %.1f, PageBottom = %.1f\n",
2781 PageTop
, PageBottom
);
2782 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
2783 PageWidth
, PageLength
);
2785 switch (Orientation
)
2787 case 1 : /* Landscape */
2788 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", PageLength
);
2790 case 2 : /* Reverse Portrait */
2791 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", PageWidth
,
2794 case 3 : /* Reverse Landscape */
2795 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", PageWidth
);
2800 * Mirror the page as needed...
2804 doc_printf(doc
, "%.1f 0.0 translate -1 1 scale\n", PageWidth
);
2807 * Offset and scale as necessary for fit_to_page/fit-to-page/number-up...
2810 if (Duplex
&& doc
->number_up
> 1 && ((number
/ doc
->number_up
) & 1))
2811 doc_printf(doc
, "%.1f %.1f translate\n", PageWidth
- PageRight
, PageBottom
);
2812 else if (doc
->number_up
> 1 || doc
->fit_to_page
)
2813 doc_printf(doc
, "%.1f %.1f translate\n", PageLeft
, PageBottom
);
2815 switch (doc
->number_up
)
2818 if (doc
->fit_to_page
)
2821 l
= w
* bboxl
/ bboxw
;
2826 w
= l
* bboxw
/ bboxl
;
2829 tx
= 0.5 * (pagew
- w
);
2830 ty
= 0.5 * (pagel
- l
);
2832 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n", tx
, ty
,
2833 w
/ bboxw
, l
/ bboxl
);
2840 if (Orientation
& 1)
2844 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2848 l
= w
* bboxl
/ bboxw
;
2850 if (l
> (pagew
* 0.5))
2853 w
= l
* bboxw
/ bboxl
;
2856 tx
= 0.5 * (pagew
* 0.5 - l
);
2857 ty
= 0.5 * (pagel
- w
);
2859 if (doc
->normal_landscape
)
2860 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2862 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2864 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2865 ty
, tx
+ pagew
* 0.5 * x
, w
/ bboxw
, l
/ bboxl
);
2871 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2875 w
= l
* bboxw
/ bboxl
;
2877 if (w
> (pagel
* 0.5))
2880 l
= w
* bboxl
/ bboxw
;
2883 tx
= 0.5 * (pagel
* 0.5 - w
);
2884 ty
= 0.5 * (pagew
- l
);
2886 if (doc
->normal_landscape
)
2887 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2889 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2891 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2892 tx
+ pagel
* 0.5 * x
, ty
, w
/ bboxw
, l
/ bboxl
);
2897 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2908 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2911 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2915 l
= w
* bboxl
/ bboxw
;
2917 if (l
> (pagel
* 0.5))
2920 w
= l
* bboxw
/ bboxl
;
2923 tx
= 0.5 * (pagew
* 0.5 - w
);
2924 ty
= 0.5 * (pagel
* 0.5 - l
);
2926 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2927 tx
+ x
* pagew
* 0.5, ty
+ y
* pagel
* 0.5,
2928 w
/ bboxw
, l
/ bboxl
);
2932 if (Orientation
& 1)
2934 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2939 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2942 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2950 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2953 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2958 l
= w
* bboxl
/ bboxw
;
2960 if (l
> (pagew
* 0.333))
2963 w
= l
* bboxw
/ bboxl
;
2966 tx
= 0.5 * (pagel
- 2 * w
);
2967 ty
= 0.5 * (pagew
- 3 * l
);
2969 if (doc
->normal_landscape
)
2970 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
2972 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
2974 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2975 tx
+ x
* w
, ty
+ y
* l
, l
/ bboxl
, w
/ bboxw
);
2979 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2984 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2987 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2995 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2998 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3003 w
= l
* bboxw
/ bboxl
;
3005 if (w
> (pagel
* 0.333))
3008 l
= w
* bboxl
/ bboxw
;
3011 tx
= 0.5 * (pagel
- 3 * w
);
3012 ty
= 0.5 * (pagew
- 2 * l
);
3014 if (doc
->normal_landscape
)
3015 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
3017 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
3019 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3020 tx
+ w
* x
, ty
+ l
* y
, w
/ bboxw
, l
/ bboxl
);
3026 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3037 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3040 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3044 l
= w
* bboxl
/ bboxw
;
3046 if (l
> (pagel
* 0.333))
3049 w
= l
* bboxw
/ bboxl
;
3052 tx
= 0.5 * (pagew
* 0.333 - w
);
3053 ty
= 0.5 * (pagel
* 0.333 - l
);
3055 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3056 tx
+ x
* pagew
* 0.333, ty
+ y
* pagel
* 0.333,
3057 w
/ bboxw
, l
/ bboxl
);
3061 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3072 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3075 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3079 l
= w
* bboxl
/ bboxw
;
3081 if (l
> (pagel
* 0.25))
3084 w
= l
* bboxw
/ bboxl
;
3087 tx
= 0.5 * (pagew
* 0.25 - w
);
3088 ty
= 0.5 * (pagel
* 0.25 - l
);
3090 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3091 tx
+ x
* pagew
* 0.25, ty
+ y
* pagel
* 0.25,
3092 w
/ bboxw
, l
/ bboxl
);
3097 * Draw borders as necessary...
3100 if (doc
->page_border
&& show_border
)
3102 int rects
; /* Number of border rectangles */
3103 double fscale
; /* Scaling value for points */
3106 rects
= (doc
->page_border
& PSTOPS_BORDERDOUBLE
) ? 2 : 1;
3107 fscale
= PageWidth
/ w
;
3108 margin
= 2.25 * fscale
;
3111 * Set the line width and color...
3114 doc_puts(doc
, "gsave\n");
3115 doc_printf(doc
, "%.3f setlinewidth 0 setgray newpath\n",
3116 (doc
->page_border
& PSTOPS_BORDERTHICK
) ? 0.5 * fscale
:
3120 * Draw border boxes...
3123 for (; rects
> 0; rects
--, margin
+= 2 * fscale
)
3124 if (doc
->number_up
> 1)
3125 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3129 bboxl
- 2 * margin
);
3131 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3133 PageBottom
+ margin
,
3134 PageRight
- PageLeft
- 2 * margin
,
3135 PageTop
- PageBottom
- 2 * margin
);
3138 * Restore pen settings...
3141 doc_puts(doc
, "grestore\n");
3144 if (doc
->fit_to_page
)
3147 * Offset the page by its bounding box...
3150 doc_printf(doc
, "%d %d translate\n", -bounding_box
[0],
3154 if (doc
->fit_to_page
|| doc
->number_up
> 1)
3157 * Clip the page to the page's bounding box...
3160 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrc\n",
3161 bboxx
+ margin
, bboxy
+ margin
,
3162 bboxw
- 2 * margin
, bboxl
- 2 * margin
);
3168 * 'write_label_prolog()' - Write the prolog with the classification
3173 write_label_prolog(pstops_doc_t
*doc
, /* I - Document info */
3174 const char *label
, /* I - Page label */
3175 float bottom
, /* I - Bottom position in points */
3176 float top
, /* I - Top position in points */
3177 float width
) /* I - Width in points */
3179 const char *classification
; /* CLASSIFICATION environment variable */
3180 const char *ptr
; /* Temporary string pointer */
3184 * First get the current classification...
3187 if ((classification
= getenv("CLASSIFICATION")) == NULL
)
3188 classification
= "";
3189 if (strcmp(classification
, "none") == 0)
3190 classification
= "";
3193 * If there is nothing to show, bind an empty 'write labels' procedure
3197 if (!classification
[0] && (label
== NULL
|| !label
[0]))
3199 doc_puts(doc
, "userdict/ESPwl{}bind put\n");
3204 * Set the classification + page label string...
3207 doc_puts(doc
, "userdict");
3208 if (!strcmp(classification
, "confidential"))
3209 doc_puts(doc
, "/ESPpl(CONFIDENTIAL");
3210 else if (!strcmp(classification
, "classified"))
3211 doc_puts(doc
, "/ESPpl(CLASSIFIED");
3212 else if (!strcmp(classification
, "secret"))
3213 doc_puts(doc
, "/ESPpl(SECRET");
3214 else if (!strcmp(classification
, "topsecret"))
3215 doc_puts(doc
, "/ESPpl(TOP SECRET");
3216 else if (!strcmp(classification
, "unclassified"))
3217 doc_puts(doc
, "/ESPpl(UNCLASSIFIED");
3220 doc_puts(doc
, "/ESPpl(");
3222 for (ptr
= classification
; *ptr
; ptr
++)
3224 if (*ptr
< 32 || *ptr
> 126)
3225 doc_printf(doc
, "\\%03o", *ptr
);
3226 else if (*ptr
== '_')
3228 else if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
3229 doc_printf(doc
, "\\%c", *ptr
);
3231 doc_printf(doc
, "%c", *ptr
);
3237 if (classification
[0])
3238 doc_puts(doc
, " - ");
3241 * Quote the label string as needed...
3244 for (ptr
= label
; *ptr
; ptr
++)
3246 if (*ptr
< 32 || *ptr
> 126)
3247 doc_printf(doc
, "\\%03o", *ptr
);
3248 else if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
3249 doc_printf(doc
, "\\%c", *ptr
);
3251 doc_printf(doc
, "%c", *ptr
);
3255 doc_puts(doc
, ")put\n");
3258 * Then get a 14 point Helvetica-Bold font...
3261 doc_puts(doc
, "userdict/ESPpf /Helvetica-Bold findfont 14 scalefont put\n");
3264 * Finally, the procedure to write the labels on the page...
3267 doc_puts(doc
, "userdict/ESPwl{\n");
3268 doc_puts(doc
, " ESPpf setfont\n");
3269 doc_printf(doc
, " ESPpl stringwidth pop dup 12 add exch -0.5 mul %.0f add\n",
3271 doc_puts(doc
, " 1 setgray\n");
3272 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrf\n", bottom
- 2.0);
3273 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrf\n", top
- 18.0);
3274 doc_puts(doc
, " 0 setgray\n");
3275 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrs\n", bottom
- 2.0);
3276 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrs\n", top
- 18.0);
3277 doc_printf(doc
, " dup %.0f moveto ESPpl show\n", bottom
+ 2.0);
3278 doc_printf(doc
, " %.0f moveto ESPpl show\n", top
- 14.0);
3279 doc_puts(doc
, "pop\n");
3280 doc_puts(doc
, "}bind put\n");
3285 * 'write_labels()' - Write the actual page labels.
3287 * This function is a copy of the one in common.c since we need to
3288 * use doc_puts/doc_printf instead of puts/printf...
3292 write_labels(pstops_doc_t
*doc
, /* I - Document information */
3293 int orient
) /* I - Orientation of the page */
3295 float width
, /* Width of page */
3296 length
; /* Length of page */
3299 doc_puts(doc
, "gsave\n");
3301 if ((orient
^ Orientation
) & 1)
3309 length
= PageLength
;
3314 case 1 : /* Landscape */
3315 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", length
);
3317 case 2 : /* Reverse Portrait */
3318 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", width
, length
);
3320 case 3 : /* Reverse Landscape */
3321 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", width
);
3325 doc_puts(doc
, "ESPwl\n");
3326 doc_puts(doc
, "grestore\n");
3331 * 'write_options()' - Write options provided via %%IncludeFeature.
3336 pstops_doc_t
*doc
, /* I - Document */
3337 ppd_file_t
*ppd
, /* I - PPD file */
3338 int num_options
, /* I - Number of options */
3339 cups_option_t
*options
) /* I - Options */
3341 int i
; /* Looping var */
3342 ppd_option_t
*option
; /* PPD option */
3343 float min_order
; /* Minimum OrderDependency value */
3344 char *doc_setup
, /* DocumentSetup commands to send */
3345 *any_setup
; /* AnySetup commands to send */
3349 * Figure out the minimum OrderDependency value...
3352 if ((option
= ppdFindOption(ppd
, "PageRegion")) != NULL
)
3353 min_order
= option
->order
;
3357 for (i
= 0; i
< num_options
; i
++)
3358 if ((option
= ppdFindOption(ppd
, options
[i
].name
)) != NULL
&&
3359 option
->order
< min_order
)
3360 min_order
= option
->order
;
3363 * Mark and extract them...
3366 cupsMarkOptions(ppd
, num_options
, options
);
3368 doc_setup
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, min_order
);
3369 any_setup
= ppdEmitString(ppd
, PPD_ORDER_ANY
, min_order
);
3372 * Then send them out...
3375 if (doc
->number_up
> 1)
3378 * Temporarily restore setpagedevice so we can set the options...
3381 doc_puts(doc
, "userdict/setpagedevice/CUPSsetpagedevice load put\n");
3386 doc_puts(doc
, doc_setup
);
3392 doc_puts(doc
, any_setup
);
3396 if (doc
->number_up
> 1)
3399 * Disable setpagedevice again...
3402 doc_puts(doc
, "userdict/setpagedevice{pop}bind put\n");
3408 * End of "$Id: pstops.c 12656 2015-05-22 17:27:37Z msweet $".