]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/pstops.c
2 * "$Id: pstops.c 7977 2008-09-23 23:44:33Z mike $"
4 * PostScript filter for CUPS.
6 * Copyright 2007-2012 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 * main() - Main entry.
20 * add_page() - Add a page to the pages array.
21 * cancel_job() - Flag the job as canceled.
22 * check_range() - Check to see if the current page is selected for
24 * copy_bytes() - Copy bytes from the input file to stdout.
25 * copy_comments() - Copy all of the comments section.
26 * copy_dsc() - Copy a DSC-conforming document.
27 * copy_non_dsc() - Copy a document that does not conform to the DSC.
28 * copy_page() - Copy a page description.
29 * copy_prolog() - Copy the document prolog section.
30 * copy_setup() - Copy the document setup section.
31 * copy_trailer() - Copy the document trailer.
32 * do_prolog() - Send the necessary document prolog commands.
33 * do_setup() - Send the necessary document setup commands.
34 * doc_printf() - Send a formatted string to stdout and/or the temp
36 * doc_puts() - Send a nul-terminated string to stdout and/or the
38 * doc_write() - Send data to stdout and/or the temp file.
39 * end_nup() - End processing for N-up printing.
40 * include_feature() - Include a printer option/feature command.
41 * parse_text() - Parse a text value in a comment.
42 * set_pstops_options() - Set pstops options.
43 * skip_page() - Skip past a page that won't be printed.
44 * start_nup() - Start processing for N-up printing.
45 * write_label_prolog() - Write the prolog with the classification and page
47 * write_labels() - Write the actual page labels.
48 * write_options() - Write options provided via %%IncludeFeature.
52 * Include necessary headers...
58 #include <cups/file.h>
59 #include <cups/array.h>
60 #include <cups/language-private.h>
68 #define PSTOPS_BORDERNONE 0 /* No border or hairline border */
69 #define PSTOPS_BORDERTHICK 1 /* Think border */
70 #define PSTOPS_BORDERSINGLE 2 /* Single-line hairline border */
71 #define PSTOPS_BORDERSINGLE2 3 /* Single-line thick border */
72 #define PSTOPS_BORDERDOUBLE 4 /* Double-line hairline border */
73 #define PSTOPS_BORDERDOUBLE2 5 /* Double-line thick border */
75 #define PSTOPS_LAYOUT_LRBT 0 /* Left to right, bottom to top */
76 #define PSTOPS_LAYOUT_LRTB 1 /* Left to right, top to bottom */
77 #define PSTOPS_LAYOUT_RLBT 2 /* Right to left, bottom to top */
78 #define PSTOPS_LAYOUT_RLTB 3 /* Right to left, top to bottom */
79 #define PSTOPS_LAYOUT_BTLR 4 /* Bottom to top, left to right */
80 #define PSTOPS_LAYOUT_TBLR 5 /* Top to bottom, left to right */
81 #define PSTOPS_LAYOUT_BTRL 6 /* Bottom to top, right to left */
82 #define PSTOPS_LAYOUT_TBRL 7 /* Top to bottom, right to left */
84 #define PSTOPS_LAYOUT_NEGATEY 1 /* The bits for the layout */
85 #define PSTOPS_LAYOUT_NEGATEX 2 /* definitions above... */
86 #define PSTOPS_LAYOUT_VERTICAL 4
93 typedef struct /**** Page information ****/
95 char *label
; /* Page label */
96 int bounding_box
[4]; /* PageBoundingBox */
97 off_t offset
; /* Offset to start of page */
98 ssize_t length
; /* Number of bytes for page */
99 int num_options
; /* Number of options for this page */
100 cups_option_t
*options
; /* Options for this page */
103 typedef struct /**** Document information ****/
105 int page
; /* Current page */
106 int bounding_box
[4]; /* BoundingBox from header */
107 int new_bounding_box
[4]; /* New composite bounding box */
108 int num_options
; /* Number of document-wide options */
109 cups_option_t
*options
; /* Document-wide options */
110 int normal_landscape
, /* Normal rotation for landscape? */
111 saw_eof
, /* Saw the %%EOF comment? */
112 slow_collate
, /* Collate copies by hand? */
113 slow_duplex
, /* Duplex pages slowly? */
114 slow_order
, /* Reverse pages slowly? */
115 use_ESPshowpage
; /* Use ESPshowpage? */
116 cups_array_t
*pages
; /* Pages in document */
117 cups_file_t
*temp
; /* Temporary file, if any */
118 char tempfile
[1024]; /* Temporary filename */
119 int job_id
; /* Job ID */
120 const char *user
, /* User name */
121 *title
; /* Job name */
122 int copies
; /* Number of copies */
123 const char *ap_input_slot
, /* AP_FIRSTPAGE_InputSlot value */
124 *ap_manual_feed
, /* AP_FIRSTPAGE_ManualFeed value */
125 *ap_media_color
, /* AP_FIRSTPAGE_MediaColor value */
126 *ap_media_type
, /* AP_FIRSTPAGE_MediaType value */
127 *ap_page_region
, /* AP_FIRSTPAGE_PageRegion value */
128 *ap_page_size
; /* AP_FIRSTPAGE_PageSize value */
129 int collate
, /* Collate copies? */
130 emit_jcl
, /* Emit JCL commands? */
131 fitplot
; /* Fit pages to media */
132 const char *input_slot
, /* InputSlot value */
133 *manual_feed
, /* ManualFeed value */
134 *media_color
, /* MediaColor value */
135 *media_type
, /* MediaType value */
136 *page_region
, /* PageRegion value */
137 *page_size
; /* PageSize value */
138 int mirror
, /* doc->mirror/mirror pages */
139 number_up
, /* Number of pages on each sheet */
140 number_up_layout
, /* doc->number_up_layout of N-up pages */
141 output_order
, /* Requested reverse output order? */
142 page_border
; /* doc->page_border around pages */
143 const char *page_label
, /* page-label option, if any */
144 *page_ranges
, /* page-ranges option, if any */
145 *page_set
; /* page-set option, if any */
150 * Convenience macros...
153 #define is_first_page(p) (doc->number_up == 1 || \
154 ((p) % doc->number_up) == 1)
155 #define is_last_page(p) (doc->number_up == 1 || \
156 ((p) % doc->number_up) == 0)
157 #define is_not_last_page(p) (doc->number_up > 1 && \
158 ((p) % doc->number_up) != 0)
165 static int JobCanceled
= 0;/* Set to 1 on SIGTERM */
172 static pstops_page_t
*add_page(pstops_doc_t
*doc
, const char *label
);
173 static void cancel_job(int sig
);
174 static int check_range(pstops_doc_t
*doc
, int page
);
175 static void copy_bytes(cups_file_t
*fp
, off_t offset
,
177 static ssize_t
copy_comments(cups_file_t
*fp
, pstops_doc_t
*doc
,
178 ppd_file_t
*ppd
, char *line
,
179 ssize_t linelen
, size_t linesize
);
180 static void copy_dsc(cups_file_t
*fp
, pstops_doc_t
*doc
,
181 ppd_file_t
*ppd
, char *line
, ssize_t linelen
,
183 static void copy_non_dsc(cups_file_t
*fp
, pstops_doc_t
*doc
,
184 ppd_file_t
*ppd
, char *line
,
185 ssize_t linelen
, size_t linesize
);
186 static ssize_t
copy_page(cups_file_t
*fp
, pstops_doc_t
*doc
,
187 ppd_file_t
*ppd
, int number
, char *line
,
188 ssize_t linelen
, size_t linesize
);
189 static ssize_t
copy_prolog(cups_file_t
*fp
, pstops_doc_t
*doc
,
190 ppd_file_t
*ppd
, char *line
,
191 ssize_t linelen
, size_t linesize
);
192 static ssize_t
copy_setup(cups_file_t
*fp
, pstops_doc_t
*doc
,
193 ppd_file_t
*ppd
, char *line
,
194 ssize_t linelen
, size_t linesize
);
195 static ssize_t
copy_trailer(cups_file_t
*fp
, pstops_doc_t
*doc
,
196 ppd_file_t
*ppd
, int number
, char *line
,
197 ssize_t linelen
, size_t linesize
);
198 static void do_prolog(pstops_doc_t
*doc
, ppd_file_t
*ppd
);
199 static void do_setup(pstops_doc_t
*doc
, ppd_file_t
*ppd
);
200 static void doc_printf(pstops_doc_t
*doc
, const char *format
, ...)
201 __attribute__ ((__format__ (__printf__
, 2, 3)));
202 static void doc_puts(pstops_doc_t
*doc
, const char *s
);
203 static void doc_write(pstops_doc_t
*doc
, const char *s
, size_t len
);
204 static void end_nup(pstops_doc_t
*doc
, int number
);
205 static int include_feature(ppd_file_t
*ppd
, const char *line
,
207 cups_option_t
**options
);
208 static char *parse_text(const char *start
, char **end
, char *buffer
,
210 static void set_pstops_options(pstops_doc_t
*doc
, ppd_file_t
*ppd
,
211 char *argv
[], int num_options
,
212 cups_option_t
*options
);
213 static ssize_t
skip_page(cups_file_t
*fp
, char *line
, ssize_t linelen
,
215 static void start_nup(pstops_doc_t
*doc
, int number
,
216 int show_border
, const int *bounding_box
);
217 static void write_label_prolog(pstops_doc_t
*doc
, const char *label
,
218 float bottom
, float top
,
220 static void write_labels(pstops_doc_t
*doc
, int orient
);
221 static void write_options(pstops_doc_t
*doc
, ppd_file_t
*ppd
,
222 int num_options
, cups_option_t
*options
);
226 * 'main()' - Main entry.
229 int /* O - Exit status */
230 main(int argc
, /* I - Number of command-line args */
231 char *argv
[]) /* I - Command-line arguments */
233 pstops_doc_t doc
; /* Document information */
234 cups_file_t
*fp
; /* Print file */
235 ppd_file_t
*ppd
; /* PPD file */
236 int num_options
; /* Number of print options */
237 cups_option_t
*options
; /* Print options */
238 char line
[8192]; /* Line buffer */
239 size_t len
; /* Length of line buffer */
240 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
241 struct sigaction action
; /* Actions for POSIX signals */
242 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
246 * Make sure status messages are not buffered...
249 setbuf(stderr
, NULL
);
252 * Ignore broken pipe signals...
255 signal(SIGPIPE
, SIG_IGN
);
258 * Check command-line...
261 if (argc
< 6 || argc
> 7)
263 _cupsLangPrintf(stderr
,
264 _("Usage: %s job-id user title copies options file"),
270 * Register a signal handler to cleanly cancel a job.
273 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
274 sigset(SIGTERM
, cancel_job
);
275 #elif defined(HAVE_SIGACTION)
276 memset(&action
, 0, sizeof(action
));
278 sigemptyset(&action
.sa_mask
);
279 action
.sa_handler
= cancel_job
;
280 sigaction(SIGTERM
, &action
, NULL
);
282 signal(SIGTERM
, cancel_job
);
283 #endif /* HAVE_SIGSET */
286 * If we have 7 arguments, print the file named on the command-line.
287 * Otherwise, send stdin instead...
291 fp
= cupsFileStdin();
295 * Try to open the print file...
298 if ((fp
= cupsFileOpen(argv
[6], "r")) == NULL
)
300 _cupsLangPrintError("ERROR", _("Unable to open print file"));
306 * Read the first line to see if we have DSC comments...
309 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
311 fputs("DEBUG: The print file is empty.\n", stderr
);
316 * Process command-line options...
320 num_options
= cupsParseOptions(argv
[5], 0, &options
);
321 ppd
= SetCommonOptions(num_options
, options
, 1);
323 set_pstops_options(&doc
, ppd
, argv
, num_options
, options
);
326 * Write any "exit server" options that have been selected...
329 ppdEmit(ppd
, stdout
, PPD_ORDER_EXIT
);
332 * Write any JCL commands that are needed to print PostScript code...
336 ppdEmitJCL(ppd
, stdout
, doc
.job_id
, doc
.user
, doc
.title
);
339 * Start with a DSC header...
342 puts("%!PS-Adobe-3.0");
345 * Skip leading PJL in the document...
348 while (!strncmp(line
, "\033%-12345X", 9) || !strncmp(line
, "@PJL ", 5))
351 * Yup, we have leading PJL fun, so skip it until we hit the line
352 * with "ENTER LANGUAGE"...
355 fputs("DEBUG: Skipping PJL header...\n", stderr
);
357 while (strstr(line
, "ENTER LANGUAGE") == NULL
&& strncmp(line
, "%!", 2))
358 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
361 if (!strncmp(line
, "%!", 2))
364 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
369 * Now see if the document conforms to the Adobe Document Structuring
373 if (!strncmp(line
, "%!PS-Adobe-", 11))
376 * Yes, filter the document...
379 copy_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
384 * No, display an error message and treat the file as if it contains
388 copy_non_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
392 * Send %%EOF as needed...
399 * End the job with the appropriate JCL command or CTRL-D...
404 if (ppd
&& ppd
->jcl_end
)
405 ppdEmitJCLEnd(ppd
, stdout
);
411 * Close files and remove the temporary file if needed...
416 cupsFileClose(doc
.temp
);
417 unlink(doc
.tempfile
);
421 cupsFreeOptions(num_options
, options
);
430 * 'add_page()' - Add a page to the pages array.
433 static pstops_page_t
* /* O - New page info object */
434 add_page(pstops_doc_t
*doc
, /* I - Document information */
435 const char *label
) /* I - Page label */
437 pstops_page_t
*pageinfo
; /* New page info object */
441 doc
->pages
= cupsArrayNew(NULL
, NULL
);
445 _cupsLangPrintError("EMERG", _("Unable to allocate memory for pages array"));
449 if ((pageinfo
= calloc(1, sizeof(pstops_page_t
))) == NULL
)
451 _cupsLangPrintError("EMERG", _("Unable to allocate memory for page info"));
455 pageinfo
->label
= strdup(label
);
456 pageinfo
->offset
= cupsFileTell(doc
->temp
);
458 cupsArrayAdd(doc
->pages
, pageinfo
);
467 * 'cancel_job()' - Flag the job as canceled.
471 cancel_job(int sig
) /* I - Signal number (unused) */
480 * 'check_range()' - Check to see if the current page is selected for
484 static int /* O - 1 if selected, 0 otherwise */
485 check_range(pstops_doc_t
*doc
, /* I - Document information */
486 int page
) /* I - Page number */
488 const char *range
; /* Pointer into range string */
489 int lower
, upper
; /* Lower and upper page numbers */
495 * See if we only print even or odd pages...
498 if (!_cups_strcasecmp(doc
->page_set
, "even") && (page
& 1))
501 if (!_cups_strcasecmp(doc
->page_set
, "odd") && !(page
& 1))
505 if (!doc
->page_ranges
)
506 return (1); /* No range, print all pages... */
508 for (range
= doc
->page_ranges
; *range
!= '\0';)
514 upper
= strtol(range
, (char **)&range
, 10);
518 lower
= strtol(range
, (char **)&range
, 10);
523 if (!isdigit(*range
& 255))
526 upper
= strtol(range
, (char **)&range
, 10);
532 if (page
>= lower
&& page
<= upper
)
546 * 'copy_bytes()' - Copy bytes from the input file to stdout.
550 copy_bytes(cups_file_t
*fp
, /* I - File to read from */
551 off_t offset
, /* I - Offset to page data */
552 size_t length
) /* I - Length of page data */
554 char buffer
[8192]; /* Data buffer */
555 ssize_t nbytes
; /* Number of bytes read */
556 size_t nleft
; /* Number of bytes left/remaining */
561 if (cupsFileSeek(fp
, offset
) < 0)
563 _cupsLangPrintError("ERROR", _("Unable to see in file"));
567 while (nleft
> 0 || length
== 0)
569 if (nleft
> sizeof(buffer
) || length
== 0)
570 nbytes
= sizeof(buffer
);
574 if ((nbytes
= cupsFileRead(fp
, buffer
, nbytes
)) < 1)
579 fwrite(buffer
, 1, nbytes
, stdout
);
585 * 'copy_comments()' - Copy all of the comments section.
587 * This function expects "line" to be filled with a comment line.
588 * On return, "line" will contain the next line in the file, if any.
591 static ssize_t
/* O - Length of next line */
592 copy_comments(cups_file_t
*fp
, /* I - File to read from */
593 pstops_doc_t
*doc
, /* I - Document info */
594 ppd_file_t
*ppd
, /* I - PPD file */
595 char *line
, /* I - Line buffer */
596 ssize_t linelen
, /* I - Length of initial line */
597 size_t linesize
) /* I - Size of line buffer */
599 int saw_bounding_box
, /* Saw %%BoundingBox: comment? */
600 saw_for
, /* Saw %%For: comment? */
601 saw_pages
, /* Saw %%Pages: comment? */
602 saw_title
; /* Saw %%Title: comment? */
606 * Loop until we see %%EndComments or a non-comment line...
609 saw_bounding_box
= 0;
614 while (line
[0] == '%')
617 * Strip trailing whitespace...
624 if (!isspace(line
[linelen
] & 255))
627 line
[linelen
] = '\0';
634 fprintf(stderr
, "DEBUG: %s\n", line
);
637 * Pull the headers out...
640 if (!strncmp(line
, "%%Pages:", 8))
642 int pages
; /* Number of pages */
645 fputs("DEBUG: A duplicate %%Pages: comment was seen.\n", stderr
);
649 if (Duplex
&& (pages
= atoi(line
+ 8)) > 0 && pages
<= doc
->number_up
)
652 * Since we will only be printing on a single page, disable duplexing.
656 doc
->slow_duplex
= 0;
658 if (cupsGetOption("sides", doc
->num_options
, doc
->options
))
659 doc
->num_options
= cupsAddOption("sides", "one-sided",
660 doc
->num_options
, &(doc
->options
));
662 if (cupsGetOption("Duplex", doc
->num_options
, doc
->options
))
663 doc
->num_options
= cupsAddOption("Duplex", "None",
664 doc
->num_options
, &(doc
->options
));
666 if (cupsGetOption("EFDuplex", doc
->num_options
, doc
->options
))
667 doc
->num_options
= cupsAddOption("EFDuplex", "None",
668 doc
->num_options
, &(doc
->options
));
670 if (cupsGetOption("EFDuplexing", doc
->num_options
, doc
->options
))
671 doc
->num_options
= cupsAddOption("EFDuplexing", "False",
672 doc
->num_options
, &(doc
->options
));
674 if (cupsGetOption("KD03Duplex", doc
->num_options
, doc
->options
))
675 doc
->num_options
= cupsAddOption("KD03Duplex", "None",
676 doc
->num_options
, &(doc
->options
));
678 if (cupsGetOption("JCLDuplex", doc
->num_options
, doc
->options
))
679 doc
->num_options
= cupsAddOption("JCLDuplex", "None",
680 doc
->num_options
, &(doc
->options
));
682 ppdMarkOption(ppd
, "Duplex", "None");
683 ppdMarkOption(ppd
, "EFDuplex", "None");
684 ppdMarkOption(ppd
, "EFDuplexing", "False");
685 ppdMarkOption(ppd
, "KD03Duplex", "None");
686 ppdMarkOption(ppd
, "JCLDuplex", "None");
689 else if (!strncmp(line
, "%%BoundingBox:", 14))
691 if (saw_bounding_box
)
692 fputs("DEBUG: A duplicate %%BoundingBox: comment was seen.\n", stderr
);
693 else if (strstr(line
+ 14, "(atend)"))
696 * Do nothing for now but use the default imageable area...
699 else if (sscanf(line
+ 14, "%d%d%d%d", doc
->bounding_box
+ 0,
700 doc
->bounding_box
+ 1, doc
->bounding_box
+ 2,
701 doc
->bounding_box
+ 3) != 4)
703 fputs("DEBUG: A bad %%BoundingBox: comment was seen.\n", stderr
);
705 doc
->bounding_box
[0] = (int)PageLeft
;
706 doc
->bounding_box
[1] = (int)PageBottom
;
707 doc
->bounding_box
[2] = (int)PageRight
;
708 doc
->bounding_box
[3] = (int)PageTop
;
711 saw_bounding_box
= 1;
713 else if (!strncmp(line
, "%%For:", 6))
716 doc_printf(doc
, "%s\n", line
);
718 else if (!strncmp(line
, "%%Title:", 8))
721 doc_printf(doc
, "%s\n", line
);
723 else if (!strncmp(line
, "%cupsRotation:", 14))
726 * Reset orientation of document?
729 int orient
= (atoi(line
+ 14) / 90) & 3;
731 if (orient
!= Orientation
)
734 * Yes, update things so that the pages come out right...
737 Orientation
= (4 - Orientation
+ orient
) & 3;
739 Orientation
= orient
;
742 else if (!strcmp(line
, "%%EndComments"))
744 linelen
= cupsFileGetLine(fp
, line
, linesize
);
747 else if (strncmp(line
, "%!", 2) && strncmp(line
, "%cups", 5))
748 doc_printf(doc
, "%s\n", line
);
750 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
754 if (!saw_bounding_box
)
755 fputs("DEBUG: There wasn't a %%BoundingBox: comment in the header.\n",
759 fputs("DEBUG: There wasn't a %%Pages: comment in the header.\n", stderr
);
762 WriteTextComment("For", doc
->user
);
765 WriteTextComment("Title", doc
->title
);
767 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
770 * Tell the document processor the copy and duplex options
771 * that are required...
774 doc_printf(doc
, "%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
775 doc
->collate
? " collate" : "",
776 Duplex
? " duplex" : "");
779 * Apple uses RBI comments for various non-PPD options...
782 doc_printf(doc
, "%%RBINumCopies: %d\n", doc
->copies
);
787 * Tell the document processor the duplex option that is required...
791 doc_puts(doc
, "%%Requirements: duplex\n");
794 * Apple uses RBI comments for various non-PPD options...
797 doc_puts(doc
, "%RBINumCopies: 1\n");
800 doc_puts(doc
, "%%Pages: (atend)\n");
801 doc_puts(doc
, "%%BoundingBox: (atend)\n");
802 doc_puts(doc
, "%%EndComments\n");
809 * 'copy_dsc()' - Copy a DSC-conforming document.
811 * This function expects "line" to be filled with the %!PS-Adobe comment line.
815 copy_dsc(cups_file_t
*fp
, /* I - File to read from */
816 pstops_doc_t
*doc
, /* I - Document info */
817 ppd_file_t
*ppd
, /* I - PPD file */
818 char *line
, /* I - Line buffer */
819 ssize_t linelen
, /* I - Length of initial line */
820 size_t linesize
) /* I - Size of line buffer */
822 int number
; /* Page number */
823 pstops_page_t
*pageinfo
; /* Page information */
827 * Make sure we use ESPshowpage for EPS files...
830 if (strstr(line
, "EPSF"))
832 doc
->use_ESPshowpage
= 1;
837 * Start sending the document with any commands needed...
840 fprintf(stderr
, "DEBUG: Before copy_comments - %s", line
);
841 linelen
= copy_comments(fp
, doc
, ppd
, line
, linelen
, linesize
);
844 * Now find the prolog section, if any...
847 fprintf(stderr
, "DEBUG: Before copy_prolog - %s", line
);
848 linelen
= copy_prolog(fp
, doc
, ppd
, line
, linelen
, linesize
);
851 * Then the document setup section...
854 fprintf(stderr
, "DEBUG: Before copy_setup - %s", line
);
855 linelen
= copy_setup(fp
, doc
, ppd
, line
, linelen
, linesize
);
858 * Copy until we see %%Page:...
861 while (strncmp(line
, "%%Page:", 7) && strncmp(line
, "%%Trailer", 9))
863 doc_write(doc
, line
, linelen
);
865 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
870 * Then process pages until we have no more...
875 fprintf(stderr
, "DEBUG: Before page loop - %s", line
);
876 while (!strncmp(line
, "%%Page:", 7))
883 if (check_range(doc
, (number
- 1) / doc
->number_up
+ 1))
885 fprintf(stderr
, "DEBUG: Copying page %d...\n", number
);
886 linelen
= copy_page(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
890 fprintf(stderr
, "DEBUG: Skipping page %d...\n", number
);
891 linelen
= skip_page(fp
, line
, linelen
, linesize
);
896 * Finish up the last page(s)...
899 if (number
&& is_not_last_page(number
) && cupsArrayLast(doc
->pages
) &&
900 check_range(doc
, (number
- 1) / doc
->number_up
+ 1))
902 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
904 start_nup(doc
, doc
->number_up
, 0, doc
->bounding_box
);
905 doc_puts(doc
, "showpage\n");
906 end_nup(doc
, doc
->number_up
);
908 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
911 if (doc
->slow_duplex
&& (doc
->page
& 1))
914 * Make sure we have an even number of pages...
917 pageinfo
= add_page(doc
, "(filler)");
919 if (!doc
->slow_order
)
921 if (!ppd
|| !ppd
->num_filters
)
922 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
923 doc
->slow_collate
? 1 : doc
->copies
);
925 printf("%%%%Page: (filler) %d\n", doc
->page
);
928 start_nup(doc
, doc
->number_up
, 0, doc
->bounding_box
);
929 doc_puts(doc
, "showpage\n");
930 end_nup(doc
, doc
->number_up
);
932 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
936 * Make additional copies as necessary...
939 number
= doc
->slow_order
? 0 : doc
->page
;
941 if (doc
->temp
&& !JobCanceled
&& cupsArrayCount(doc
->pages
) > 0)
943 int copy
; /* Current copy */
947 * Reopen the temporary file for reading...
950 cupsFileClose(doc
->temp
);
952 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
958 if (doc
->slow_collate
)
959 copy
= !doc
->slow_order
;
961 copy
= doc
->copies
- 1;
963 for (; copy
< doc
->copies
; copy
++)
969 * Send end-of-job stuff followed by any start-of-job stuff required
970 * for the JCL options...
973 if (number
&& doc
->emit_jcl
&& ppd
&& ppd
->jcl_end
)
976 * Send the trailer...
980 printf("%%%%Pages: %d\n", cupsArrayCount(doc
->pages
));
981 if (doc
->number_up
> 1 || doc
->fitplot
)
982 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
983 PageLeft
, PageBottom
, PageRight
, PageTop
);
985 printf("%%%%BoundingBox: %d %d %d %d\n",
986 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
987 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
991 * Start a new document...
994 ppdEmitJCLEnd(ppd
, stdout
);
995 ppdEmitJCL(ppd
, stdout
, doc
->job_id
, doc
->user
, doc
->title
);
997 puts("%!PS-Adobe-3.0");
1003 * Copy the prolog as needed...
1008 pageinfo
= (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
1009 copy_bytes(doc
->temp
, 0, pageinfo
->offset
);
1013 * Then copy all of the pages...
1016 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayLast(doc
->pages
) :
1017 (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
1026 if (!ppd
|| !ppd
->num_filters
)
1027 fprintf(stderr
, "PAGE: %d %d\n", number
,
1028 doc
->slow_collate
? 1 : doc
->copies
);
1030 if (doc
->number_up
> 1)
1032 printf("%%%%Page: (%d) %d\n", number
, number
);
1033 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1034 PageLeft
, PageBottom
, PageRight
, PageTop
);
1038 printf("%%%%Page: %s %d\n", pageinfo
->label
, number
);
1039 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1040 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
1041 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
1044 copy_bytes(doc
->temp
, pageinfo
->offset
, pageinfo
->length
);
1046 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayPrev(doc
->pages
) :
1047 (pstops_page_t
*)cupsArrayNext(doc
->pages
);
1053 * Restore the old showpage operator as needed...
1056 if (doc
->use_ESPshowpage
)
1057 puts("userdict/showpage/ESPshowpage load put\n");
1060 * Write/copy the trailer...
1064 copy_trailer(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
1069 * 'copy_non_dsc()' - Copy a document that does not conform to the DSC.
1071 * This function expects "line" to be filled with the %! comment line.
1075 copy_non_dsc(cups_file_t
*fp
, /* I - File to read from */
1076 pstops_doc_t
*doc
, /* I - Document info */
1077 ppd_file_t
*ppd
, /* I - PPD file */
1078 char *line
, /* I - Line buffer */
1079 ssize_t linelen
, /* I - Length of initial line */
1080 size_t linesize
) /* I - Size of line buffer */
1082 int copy
; /* Current copy */
1083 char buffer
[8192]; /* Copy buffer */
1084 int bytes
; /* Number of bytes copied */
1088 * First let the user know that they are attempting to print a file
1089 * that may not print correctly...
1092 fputs("DEBUG: This document does not conform to the Adobe Document "
1093 "Structuring Conventions and may not print correctly.\n", stderr
);
1096 * Then write a standard DSC comment section...
1099 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", PageLeft
, PageBottom
,
1100 PageRight
, PageTop
);
1102 if (doc
->slow_collate
&& doc
->copies
> 1)
1103 printf("%%%%Pages: %d\n", doc
->copies
);
1107 WriteTextComment("For", doc
->user
);
1108 WriteTextComment("Title", doc
->title
);
1110 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1113 * Tell the document processor the copy and duplex options
1114 * that are required...
1117 printf("%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
1118 doc
->collate
? " collate" : "",
1119 Duplex
? " duplex" : "");
1122 * Apple uses RBI comments for various non-PPD options...
1125 printf("%%RBINumCopies: %d\n", doc
->copies
);
1130 * Tell the document processor the duplex option that is required...
1134 puts("%%Requirements: duplex");
1137 * Apple uses RBI comments for various non-PPD options...
1140 puts("%RBINumCopies: 1");
1143 puts("%%EndComments");
1146 * Then the prolog...
1149 puts("%%BeginProlog");
1151 do_prolog(doc
, ppd
);
1153 puts("%%EndProlog");
1156 * Then the setup section...
1159 puts("%%BeginSetup");
1166 * Finally, embed a copy of the file inside a %%Page...
1169 if (!ppd
|| !ppd
->num_filters
)
1170 fprintf(stderr
, "PAGE: 1 %d\n", doc
->temp
? 1 : doc
->copies
);
1172 puts("%%Page: 1 1");
1173 puts("%%BeginPageSetup");
1174 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1175 puts("%%EndPageSetup");
1176 puts("%%BeginDocument: nondsc");
1178 fwrite(line
, linelen
, 1, stdout
);
1181 cupsFileWrite(doc
->temp
, line
, linelen
);
1183 while ((bytes
= cupsFileRead(fp
, buffer
, sizeof(buffer
))) > 0)
1185 fwrite(buffer
, 1, bytes
, stdout
);
1188 cupsFileWrite(doc
->temp
, buffer
, bytes
);
1191 puts("%%EndDocument");
1193 if (doc
->use_ESPshowpage
)
1195 WriteLabels(Orientation
);
1196 puts("ESPshowpage");
1199 if (doc
->temp
&& !JobCanceled
)
1202 * Reopen the temporary file for reading...
1205 cupsFileClose(doc
->temp
);
1207 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
1210 * Make the additional copies as needed...
1213 for (copy
= 1; copy
< doc
->copies
; copy
++)
1218 if (!ppd
|| !ppd
->num_filters
)
1219 fputs("PAGE: 1 1\n", stderr
);
1221 printf("%%%%Page: %d %d\n", copy
+ 1, copy
+ 1);
1222 puts("%%BeginPageSetup");
1223 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1224 puts("%%EndPageSetup");
1225 puts("%%BeginDocument: nondsc");
1227 copy_bytes(doc
->temp
, 0, 0);
1229 puts("%%EndDocument");
1231 if (doc
->use_ESPshowpage
)
1233 WriteLabels(Orientation
);
1234 puts("ESPshowpage");
1240 * Restore the old showpage operator as needed...
1243 if (doc
->use_ESPshowpage
)
1244 puts("userdict/showpage/ESPshowpage load put\n");
1249 * 'copy_page()' - Copy a page description.
1251 * This function expects "line" to be filled with a %%Page comment line.
1252 * On return, "line" will contain the next line in the file, if any.
1255 static ssize_t
/* O - Length of next line */
1256 copy_page(cups_file_t
*fp
, /* I - File to read from */
1257 pstops_doc_t
*doc
, /* I - Document info */
1258 ppd_file_t
*ppd
, /* I - PPD file */
1259 int number
, /* I - Current page number */
1260 char *line
, /* I - Line buffer */
1261 ssize_t linelen
, /* I - Length of initial line */
1262 size_t linesize
) /* I - Size of line buffer */
1264 char label
[256], /* Page label string */
1265 *ptr
; /* Pointer into line */
1266 int level
; /* Embedded document level */
1267 pstops_page_t
*pageinfo
; /* Page information */
1268 int first_page
; /* First page on N-up output? */
1269 int has_page_setup
= 0; /* Does the page have %%Begin/EndPageSetup? */
1270 int bounding_box
[4]; /* PageBoundingBox */
1274 * Get the page label for this page...
1277 first_page
= is_first_page(number
);
1279 if (!parse_text(line
+ 7, &ptr
, label
, sizeof(label
)))
1281 fputs("DEBUG: There was a bad %%Page: comment in the file.\n", stderr
);
1285 else if (strtol(ptr
, &ptr
, 10) == LONG_MAX
|| !isspace(*ptr
& 255))
1287 fputs("DEBUG: There was a bad %%Page: comment in the file.\n", stderr
);
1292 * Create or update the current output page...
1296 pageinfo
= add_page(doc
, label
);
1298 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
1301 * Handle first page override...
1304 if (doc
->ap_input_slot
|| doc
->ap_manual_feed
)
1309 * First page/sheet gets AP_FIRSTPAGE_* options...
1312 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->ap_input_slot
,
1313 pageinfo
->num_options
,
1314 &(pageinfo
->options
));
1315 pageinfo
->num_options
= cupsAddOption("ManualFeed",
1316 doc
->ap_input_slot
? "False" :
1317 doc
->ap_manual_feed
,
1318 pageinfo
->num_options
,
1319 &(pageinfo
->options
));
1320 pageinfo
->num_options
= cupsAddOption("MediaColor", doc
->ap_media_color
,
1321 pageinfo
->num_options
,
1322 &(pageinfo
->options
));
1323 pageinfo
->num_options
= cupsAddOption("MediaType", doc
->ap_media_type
,
1324 pageinfo
->num_options
,
1325 &(pageinfo
->options
));
1326 pageinfo
->num_options
= cupsAddOption("PageRegion", doc
->ap_page_region
,
1327 pageinfo
->num_options
,
1328 &(pageinfo
->options
));
1329 pageinfo
->num_options
= cupsAddOption("PageSize", doc
->ap_page_size
,
1330 pageinfo
->num_options
,
1331 &(pageinfo
->options
));
1333 else if (doc
->page
== (Duplex
+ 2))
1336 * Second page/sheet gets default options...
1339 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->input_slot
,
1340 pageinfo
->num_options
,
1341 &(pageinfo
->options
));
1342 pageinfo
->num_options
= cupsAddOption("ManualFeed",
1343 doc
->input_slot
? "False" :
1345 pageinfo
->num_options
,
1346 &(pageinfo
->options
));
1347 pageinfo
->num_options
= cupsAddOption("MediaColor", doc
->media_color
,
1348 pageinfo
->num_options
,
1349 &(pageinfo
->options
));
1350 pageinfo
->num_options
= cupsAddOption("MediaType", doc
->media_type
,
1351 pageinfo
->num_options
,
1352 &(pageinfo
->options
));
1353 pageinfo
->num_options
= cupsAddOption("PageRegion", doc
->page_region
,
1354 pageinfo
->num_options
,
1355 &(pageinfo
->options
));
1356 pageinfo
->num_options
= cupsAddOption("PageSize", doc
->page_size
,
1357 pageinfo
->num_options
,
1358 &(pageinfo
->options
));
1363 * Scan comments until we see something other than %%Page*: or
1367 memcpy(bounding_box
, doc
->bounding_box
, sizeof(bounding_box
));
1369 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1371 if (!strncmp(line
, "%%PageBoundingBox:", 18))
1374 * %%PageBoundingBox: llx lly urx ury
1377 if (sscanf(line
+ 18, "%d%d%d%d", bounding_box
+ 0,
1378 bounding_box
+ 1, bounding_box
+ 2,
1379 bounding_box
+ 3) != 4)
1381 fputs("DEBUG: There was a bad %%PageBoundingBox: comment in the file.\n", stderr
);
1382 memcpy(bounding_box
, doc
->bounding_box
,
1383 sizeof(bounding_box
));
1385 else if (doc
->number_up
== 1 && !doc
->fitplot
&& Orientation
)
1387 int temp_bbox
[4]; /* Temporary bounding box */
1390 memcpy(temp_bbox
, bounding_box
, sizeof(temp_bbox
));
1392 fprintf(stderr
, "DEBUG: Orientation = %d\n", Orientation
);
1393 fprintf(stderr
, "DEBUG: original bounding_box = [ %d %d %d %d ]\n",
1394 bounding_box
[0], bounding_box
[1],
1395 bounding_box
[2], bounding_box
[3]);
1396 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
1397 PageWidth
, PageLength
);
1399 switch (Orientation
)
1401 case 1 : /* Landscape */
1402 bounding_box
[0] = PageLength
- temp_bbox
[3];
1403 bounding_box
[1] = temp_bbox
[0];
1404 bounding_box
[2] = PageLength
- temp_bbox
[1];
1405 bounding_box
[3] = temp_bbox
[2];
1408 case 2 : /* Reverse Portrait */
1409 bounding_box
[0] = PageWidth
- temp_bbox
[2];
1410 bounding_box
[1] = PageLength
- temp_bbox
[3];
1411 bounding_box
[2] = PageWidth
- temp_bbox
[0];
1412 bounding_box
[3] = PageLength
- temp_bbox
[1];
1415 case 3 : /* Reverse Landscape */
1416 bounding_box
[0] = temp_bbox
[1];
1417 bounding_box
[1] = PageWidth
- temp_bbox
[2];
1418 bounding_box
[2] = temp_bbox
[3];
1419 bounding_box
[3] = PageWidth
- temp_bbox
[0];
1423 fprintf(stderr
, "DEBUG: updated bounding_box = [ %d %d %d %d ]\n",
1424 bounding_box
[0], bounding_box
[1],
1425 bounding_box
[2], bounding_box
[3]);
1429 else if (!strncmp(line
, "%%PageCustomColors:", 19) ||
1430 !strncmp(line
, "%%PageMedia:", 12) ||
1431 !strncmp(line
, "%%PageOrientation:", 18) ||
1432 !strncmp(line
, "%%PageProcessColors:", 20) ||
1433 !strncmp(line
, "%%PageRequirements:", 18) ||
1434 !strncmp(line
, "%%PageResources:", 16))
1441 else if (!strncmp(line
, "%%PageCustomColors:", 19))
1444 * %%PageCustomColors: ...
1447 else if (!strncmp(line
, "%%PageMedia:", 12))
1453 else if (!strncmp(line
, "%%PageOrientation:", 18))
1456 * %%PageOrientation: ...
1459 else if (!strncmp(line
, "%%PageProcessColors:", 20))
1462 * %%PageProcessColors: ...
1465 else if (!strncmp(line
, "%%PageRequirements:", 18))
1468 * %%PageRequirements: ...
1471 else if (!strncmp(line
, "%%PageResources:", 16))
1474 * %%PageResources: ...
1477 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1480 * %%IncludeFeature: *MainKeyword OptionKeyword
1483 if (doc
->number_up
== 1 &&!doc
->fitplot
)
1484 pageinfo
->num_options
= include_feature(ppd
, line
,
1485 pageinfo
->num_options
,
1486 &(pageinfo
->options
));
1488 else if (!strncmp(line
, "%%BeginPageSetup", 16))
1497 if (doc
->number_up
== 1)
1500 * Update the document's composite and page bounding box...
1503 memcpy(pageinfo
->bounding_box
, bounding_box
,
1504 sizeof(pageinfo
->bounding_box
));
1506 if (bounding_box
[0] < doc
->new_bounding_box
[0])
1507 doc
->new_bounding_box
[0] = bounding_box
[0];
1508 if (bounding_box
[1] < doc
->new_bounding_box
[1])
1509 doc
->new_bounding_box
[1] = bounding_box
[1];
1510 if (bounding_box
[2] > doc
->new_bounding_box
[2])
1511 doc
->new_bounding_box
[2] = bounding_box
[2];
1512 if (bounding_box
[3] > doc
->new_bounding_box
[3])
1513 doc
->new_bounding_box
[3] = bounding_box
[3];
1517 * Output the page header as needed...
1520 if (!doc
->slow_order
&& first_page
)
1522 if (!ppd
|| !ppd
->num_filters
)
1523 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
1524 doc
->slow_collate
? 1 : doc
->copies
);
1526 if (doc
->number_up
> 1)
1528 printf("%%%%Page: (%d) %d\n", doc
->page
, doc
->page
);
1529 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1530 PageLeft
, PageBottom
, PageRight
, PageTop
);
1534 printf("%%%%Page: %s %d\n", pageinfo
->label
, doc
->page
);
1535 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1536 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
1537 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
1542 * Copy any page setup commands...
1546 doc_puts(doc
, "%%BeginPageSetup\n");
1550 int feature
= 0; /* In a Begin/EndFeature block? */
1552 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1554 if (!strncmp(line
, "%%EndPageSetup", 14))
1556 else if (!strncmp(line
, "%%BeginFeature:", 15))
1560 if (doc
->number_up
> 1 || doc
->fitplot
)
1563 else if (!strncmp(line
, "%%EndFeature", 12))
1567 if (doc
->number_up
> 1 || doc
->fitplot
)
1570 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1572 pageinfo
->num_options
= include_feature(ppd
, line
,
1573 pageinfo
->num_options
,
1574 &(pageinfo
->options
));
1577 else if (!strncmp(line
, "%%Include", 9))
1580 if (line
[0] != '%' && !feature
)
1583 if (!feature
|| (doc
->number_up
== 1 && !doc
->fitplot
))
1584 doc_write(doc
, line
, linelen
);
1588 * Skip %%EndPageSetup...
1591 if (linelen
> 0 && !strncmp(line
, "%%EndPageSetup", 14))
1592 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1597 char *page_setup
; /* PageSetup commands to send */
1600 if (pageinfo
->num_options
> 0)
1601 write_options(doc
, ppd
, pageinfo
->num_options
, pageinfo
->options
);
1604 * Output commands for the current page...
1607 page_setup
= ppdEmitString(ppd
, PPD_ORDER_PAGE
, 0);
1611 doc_puts(doc
, page_setup
);
1617 * Prep for the start of the page description...
1620 start_nup(doc
, number
, 1, bounding_box
);
1623 doc_puts(doc
, "%%EndPageSetup\n");
1626 * Read the rest of the page description...
1634 (!strncmp(line
, "%%Page:", 7) ||
1635 !strncmp(line
, "%%Trailer", 9) ||
1636 !strncmp(line
, "%%EOF", 5)))
1638 else if (!strncmp(line
, "%%BeginDocument", 15) ||
1639 !strncmp(line
, "%ADO_BeginApplication", 21))
1641 doc_write(doc
, line
, linelen
);
1645 else if ((!strncmp(line
, "%%EndDocument", 13) ||
1646 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
1648 doc_write(doc
, line
, linelen
);
1652 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
1653 (!strncmp(line
, "%%BeginData:", 12) &&
1654 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
1657 * Copy binary data...
1660 int bytes
; /* Bytes of data */
1663 doc_write(doc
, line
, linelen
);
1665 bytes
= atoi(strchr(line
, ':') + 1);
1669 if (bytes
> linesize
)
1670 linelen
= cupsFileRead(fp
, line
, linesize
);
1672 linelen
= cupsFileRead(fp
, line
, bytes
);
1677 perror("ERROR: Early end-of-file while reading binary data");
1681 doc_write(doc
, line
, linelen
);
1687 doc_write(doc
, line
, linelen
);
1689 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0);
1692 * Finish up this page and return...
1695 end_nup(doc
, number
);
1697 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
1704 * 'copy_prolog()' - Copy the document prolog section.
1706 * This function expects "line" to be filled with a %%BeginProlog comment line.
1707 * On return, "line" will contain the next line in the file, if any.
1710 static ssize_t
/* O - Length of next line */
1711 copy_prolog(cups_file_t
*fp
, /* I - File to read from */
1712 pstops_doc_t
*doc
, /* I - Document info */
1713 ppd_file_t
*ppd
, /* I - PPD file */
1714 char *line
, /* I - Line buffer */
1715 ssize_t linelen
, /* I - Length of initial line */
1716 size_t linesize
) /* I - Size of line buffer */
1718 while (strncmp(line
, "%%BeginProlog", 13))
1720 if (!strncmp(line
, "%%BeginSetup", 12) || !strncmp(line
, "%%Page:", 7))
1723 doc_write(doc
, line
, linelen
);
1725 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1729 doc_puts(doc
, "%%BeginProlog\n");
1731 do_prolog(doc
, ppd
);
1733 if (!strncmp(line
, "%%BeginProlog", 13))
1735 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1737 if (!strncmp(line
, "%%EndProlog", 11) ||
1738 !strncmp(line
, "%%BeginSetup", 12) ||
1739 !strncmp(line
, "%%Page:", 7))
1742 doc_write(doc
, line
, linelen
);
1745 if (!strncmp(line
, "%%EndProlog", 11))
1746 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1748 fputs("DEBUG: The %%EndProlog comment is missing.\n", stderr
);
1751 doc_puts(doc
, "%%EndProlog\n");
1758 * 'copy_setup()' - Copy the document setup section.
1760 * This function expects "line" to be filled with a %%BeginSetup comment line.
1761 * On return, "line" will contain the next line in the file, if any.
1764 static ssize_t
/* O - Length of next line */
1765 copy_setup(cups_file_t
*fp
, /* I - File to read from */
1766 pstops_doc_t
*doc
, /* I - Document info */
1767 ppd_file_t
*ppd
, /* I - PPD file */
1768 char *line
, /* I - Line buffer */
1769 ssize_t linelen
, /* I - Length of initial line */
1770 size_t linesize
) /* I - Size of line buffer */
1772 int num_options
; /* Number of options */
1773 cups_option_t
*options
; /* Options */
1776 while (strncmp(line
, "%%BeginSetup", 12))
1778 if (!strncmp(line
, "%%Page:", 7))
1781 doc_write(doc
, line
, linelen
);
1783 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1787 doc_puts(doc
, "%%BeginSetup\n");
1794 if (!strncmp(line
, "%%BeginSetup", 12))
1796 while (strncmp(line
, "%%EndSetup", 10))
1798 if (!strncmp(line
, "%%Page:", 7))
1800 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1803 * %%IncludeFeature: *MainKeyword OptionKeyword
1806 if (doc
->number_up
== 1 && !doc
->fitplot
)
1807 num_options
= include_feature(ppd
, line
, num_options
, &options
);
1809 else if (strncmp(line
, "%%BeginSetup", 12))
1810 doc_write(doc
, line
, linelen
);
1812 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1816 if (!strncmp(line
, "%%EndSetup", 10))
1817 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1819 fputs("DEBUG: The %%EndSetup comment is missing.\n", stderr
);
1822 if (num_options
> 0)
1824 write_options(doc
, ppd
, num_options
, options
);
1825 cupsFreeOptions(num_options
, options
);
1828 doc_puts(doc
, "%%EndSetup\n");
1835 * 'copy_trailer()' - Copy the document trailer.
1837 * This function expects "line" to be filled with a %%Trailer comment line.
1838 * On return, "line" will contain the next line in the file, if any.
1841 static ssize_t
/* O - Length of next line */
1842 copy_trailer(cups_file_t
*fp
, /* I - File to read from */
1843 pstops_doc_t
*doc
, /* I - Document info */
1844 ppd_file_t
*ppd
, /* I - PPD file */
1845 int number
, /* I - Number of pages */
1846 char *line
, /* I - Line buffer */
1847 ssize_t linelen
, /* I - Length of initial line */
1848 size_t linesize
) /* I - Size of line buffer */
1851 * Write the trailer comments...
1858 if (!strncmp(line
, "%%EOF", 5))
1860 else if (strncmp(line
, "%%Trailer", 9) &&
1861 strncmp(line
, "%%Pages:", 8) &&
1862 strncmp(line
, "%%BoundingBox:", 14))
1863 fwrite(line
, 1, linelen
, stdout
);
1865 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1868 fprintf(stderr
, "DEBUG: Wrote %d pages...\n", number
);
1870 printf("%%%%Pages: %d\n", number
);
1871 if (doc
->number_up
> 1 || doc
->fitplot
)
1872 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
1873 PageLeft
, PageBottom
, PageRight
, PageTop
);
1875 printf("%%%%BoundingBox: %d %d %d %d\n",
1876 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
1877 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
1884 * 'do_prolog()' - Send the necessary document prolog commands.
1888 do_prolog(pstops_doc_t
*doc
, /* I - Document information */
1889 ppd_file_t
*ppd
) /* I - PPD file */
1891 char *ps
; /* PS commands */
1895 * Send the document prolog commands...
1898 if (ppd
&& ppd
->patches
)
1900 doc_puts(doc
, "%%BeginFeature: *JobPatchFile 1\n");
1901 doc_puts(doc
, ppd
->patches
);
1902 doc_puts(doc
, "\n%%EndFeature\n");
1905 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_PROLOG
, 0.0)) != NULL
)
1912 * Define ESPshowpage here so that applications that define their
1913 * own procedure to do a showpage pick it up...
1916 if (doc
->use_ESPshowpage
)
1917 doc_puts(doc
, "userdict/ESPshowpage/showpage load put\n"
1918 "userdict/showpage{}put\n");
1923 * 'do_setup()' - Send the necessary document setup commands.
1927 do_setup(pstops_doc_t
*doc
, /* I - Document information */
1928 ppd_file_t
*ppd
) /* I - PPD file */
1930 char *ps
; /* PS commands */
1934 * Disable CTRL-D so that embedded files don't cause printing
1938 doc_puts(doc
, "% Disable CTRL-D as an end-of-file marker...\n");
1939 doc_puts(doc
, "userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put\n");
1942 * Mark job options...
1945 cupsMarkOptions(ppd
, doc
->num_options
, doc
->options
);
1948 * Send all the printer-specific setup commands...
1951 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, 0.0)) != NULL
)
1957 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_ANY
, 0.0)) != NULL
)
1964 * Set the number of copies for the job...
1967 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1969 doc_printf(doc
, "%%RBIBeginNonPPDFeature: *NumCopies %d\n", doc
->copies
);
1971 "%d/languagelevel where{pop languagelevel 2 ge}{false}ifelse\n"
1972 "{1 dict begin/NumCopies exch def currentdict end "
1974 "{userdict/#copies 3 -1 roll put}ifelse\n", doc
->copies
);
1975 doc_puts(doc
, "%RBIEndNonPPDFeature\n");
1979 * If we are doing N-up printing, disable setpagedevice...
1982 if (doc
->number_up
> 1)
1984 doc_puts(doc
, "userdict/CUPSsetpagedevice/setpagedevice load put\n");
1985 doc_puts(doc
, "userdict/setpagedevice{pop}bind put\n");
1989 * Make sure we have rectclip and rectstroke procedures of some sort...
1993 "% x y w h ESPrc - Clip to a rectangle.\n"
1994 "userdict/ESPrc/rectclip where{pop/rectclip load}\n"
1995 "{{newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
1996 "neg 0 rlineto closepath clip newpath}bind}ifelse put\n");
1999 "% x y w h ESPrf - Fill a rectangle.\n"
2000 "userdict/ESPrf/rectfill where{pop/rectfill load}\n"
2001 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
2002 "neg 0 rlineto closepath fill grestore}bind}ifelse put\n");
2005 "% x y w h ESPrs - Stroke a rectangle.\n"
2006 "userdict/ESPrs/rectstroke where{pop/rectstroke load}\n"
2007 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
2008 "neg 0 rlineto closepath stroke grestore}bind}ifelse put\n");
2011 * Write the page and label prologs...
2014 if (doc
->number_up
== 2 || doc
->number_up
== 6)
2017 * For 2- and 6-up output, rotate the labels to match the orientation
2021 if (Orientation
& 1)
2022 write_label_prolog(doc
, doc
->page_label
, PageBottom
,
2023 PageWidth
- PageLength
+ PageTop
, PageLength
);
2025 write_label_prolog(doc
, doc
->page_label
, PageLeft
, PageRight
,
2029 write_label_prolog(doc
, doc
->page_label
, PageBottom
, PageTop
, PageWidth
);
2034 * 'doc_printf()' - Send a formatted string to stdout and/or the temp file.
2036 * This function should be used for all page-level output that is affected
2037 * by ordering, collation, etc.
2041 doc_printf(pstops_doc_t
*doc
, /* I - Document information */
2042 const char *format
, /* I - Printf-style format string */
2043 ...) /* I - Additional arguments as needed */
2045 va_list ap
; /* Pointer to arguments */
2046 char buffer
[1024]; /* Output buffer */
2047 size_t bytes
; /* Number of bytes to write */
2050 va_start(ap
, format
);
2051 bytes
= vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
2054 if (bytes
> sizeof(buffer
))
2056 _cupsLangPrintFilter(stderr
, "ERROR",
2057 _("Buffer overflow detected, aborting."));
2061 doc_write(doc
, buffer
, bytes
);
2066 * 'doc_puts()' - Send a nul-terminated string to stdout and/or the temp file.
2068 * This function should be used for all page-level output that is affected
2069 * by ordering, collation, etc.
2073 doc_puts(pstops_doc_t
*doc
, /* I - Document information */
2074 const char *s
) /* I - String to send */
2076 doc_write(doc
, s
, strlen(s
));
2081 * 'doc_write()' - Send data to stdout and/or the temp file.
2085 doc_write(pstops_doc_t
*doc
, /* I - Document information */
2086 const char *s
, /* I - Data to send */
2087 size_t len
) /* I - Number of bytes to send */
2089 if (!doc
->slow_order
)
2090 fwrite(s
, 1, len
, stdout
);
2093 cupsFileWrite(doc
->temp
, s
, len
);
2098 * 'end_nup()' - End processing for N-up printing.
2102 end_nup(pstops_doc_t
*doc
, /* I - Document information */
2103 int number
) /* I - Page number */
2105 if (doc
->number_up
> 1)
2106 doc_puts(doc
, "userdict/ESPsave get restore\n");
2108 switch (doc
->number_up
)
2111 if (doc
->use_ESPshowpage
)
2113 write_labels(doc
, Orientation
);
2114 doc_puts(doc
, "ESPshowpage\n");
2120 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2122 if (Orientation
& 1)
2125 * Rotate the labels back to portrait...
2128 write_labels(doc
, Orientation
- 1);
2130 else if (Orientation
== 0)
2133 * Rotate the labels to landscape...
2136 write_labels(doc
, doc
->normal_landscape
? 1 : 3);
2141 * Rotate the labels to landscape...
2144 write_labels(doc
, doc
->normal_landscape
? 3 : 1);
2147 doc_puts(doc
, "ESPshowpage\n");
2152 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2154 write_labels(doc
, Orientation
);
2155 doc_puts(doc
, "ESPshowpage\n");
2165 * 'include_feature()' - Include a printer option/feature command.
2168 static int /* O - New number of options */
2170 ppd_file_t
*ppd
, /* I - PPD file */
2171 const char *line
, /* I - DSC line */
2172 int num_options
, /* I - Number of options */
2173 cups_option_t
**options
) /* IO - Options */
2175 char name
[255], /* Option name */
2176 value
[255]; /* Option value */
2177 ppd_option_t
*option
; /* Option in file */
2181 * Get the "%%IncludeFeature: *Keyword OptionKeyword" values...
2184 if (sscanf(line
+ 17, "%254s%254s", name
, value
) != 2)
2186 fputs("DEBUG: The %%IncludeFeature: comment is not valid.\n", stderr
);
2187 return (num_options
);
2191 * Find the option and choice...
2194 if ((option
= ppdFindOption(ppd
, name
+ 1)) == NULL
)
2196 _cupsLangPrintFilter(stderr
, "WARNING", _("Unknown option \"%s\"."),
2198 return (num_options
);
2201 if (option
->section
== PPD_ORDER_EXIT
||
2202 option
->section
== PPD_ORDER_JCL
)
2204 _cupsLangPrintFilter(stderr
, "WARNING",
2205 _("Option \"%s\" cannot be included via "
2206 "%%%%IncludeFeature."), name
+ 1);
2207 return (num_options
);
2210 if (!ppdFindChoice(option
, value
))
2212 _cupsLangPrintFilter(stderr
, "WARNING",
2213 _("Unknown choice \"%s\" for option \"%s\"."),
2215 return (num_options
);
2219 * Add the option to the option array and return...
2222 return (cupsAddOption(name
+ 1, value
, num_options
, options
));
2227 * 'parse_text()' - Parse a text value in a comment.
2229 * This function parses a DSC text value as defined on page 36 of the
2230 * DSC specification. Text values are either surrounded by parenthesis
2231 * or whitespace-delimited.
2233 * The value returned is the literal characters for the entire text
2234 * string, including any parenthesis and escape characters.
2237 static char * /* O - Value or NULL on error */
2238 parse_text(const char *start
, /* I - Start of text value */
2239 char **end
, /* O - End of text value */
2240 char *buffer
, /* I - Buffer */
2241 size_t bufsize
) /* I - Size of buffer */
2243 char *bufptr
, /* Pointer in buffer */
2244 *bufend
; /* End of buffer */
2245 int level
; /* Parenthesis level */
2249 * Skip leading whitespace...
2252 while (isspace(*start
& 255))
2256 * Then copy the value...
2261 bufend
= buffer
+ bufsize
- 1;
2263 while (bufptr
< bufend
)
2265 if (isspace(*start
& 255) && !level
)
2272 else if (*start
== ')')
2282 else if (*start
== '\\')
2285 * Copy escaped character...
2288 int i
; /* Looping var */
2292 i
<= 3 && isdigit(start
[i
] & 255) && bufptr
< bufend
;
2293 *bufptr
++ = start
[i
], i
++);
2302 * Return the value and new pointer into the line...
2306 *end
= (char *)start
;
2308 if (bufptr
== bufend
)
2316 * 'set_pstops_options()' - Set pstops options.
2321 pstops_doc_t
*doc
, /* I - Document information */
2322 ppd_file_t
*ppd
, /* I - PPD file */
2323 char *argv
[], /* I - Command-line arguments */
2324 int num_options
, /* I - Number of options */
2325 cups_option_t
*options
) /* I - Options */
2327 const char *val
; /* Option value */
2328 int intval
; /* Integer option value */
2329 ppd_attr_t
*attr
; /* PPD attribute */
2330 ppd_option_t
*option
; /* PPD option */
2331 ppd_choice_t
*choice
; /* PPD choice */
2332 const char *content_type
; /* Original content type */
2336 * Initialize document information structure...
2339 memset(doc
, 0, sizeof(pstops_doc_t
));
2341 doc
->job_id
= atoi(argv
[1]);
2342 doc
->user
= argv
[2];
2343 doc
->title
= argv
[3];
2344 doc
->copies
= atoi(argv
[4]);
2346 if (ppd
&& ppd
->landscape
> 0)
2347 doc
->normal_landscape
= 1;
2349 doc
->bounding_box
[0] = (int)PageLeft
;
2350 doc
->bounding_box
[1] = (int)PageBottom
;
2351 doc
->bounding_box
[2] = (int)PageRight
;
2352 doc
->bounding_box
[3] = (int)PageTop
;
2354 doc
->new_bounding_box
[0] = INT_MAX
;
2355 doc
->new_bounding_box
[1] = INT_MAX
;
2356 doc
->new_bounding_box
[2] = INT_MIN
;
2357 doc
->new_bounding_box
[3] = INT_MIN
;
2360 * AP_FIRSTPAGE_* and the corresponding non-first-page options.
2363 doc
->ap_input_slot
= cupsGetOption("AP_FIRSTPAGE_InputSlot", num_options
,
2365 doc
->ap_manual_feed
= cupsGetOption("AP_FIRSTPAGE_ManualFeed", num_options
,
2367 doc
->ap_media_color
= cupsGetOption("AP_FIRSTPAGE_MediaColor", num_options
,
2369 doc
->ap_media_type
= cupsGetOption("AP_FIRSTPAGE_MediaType", num_options
,
2371 doc
->ap_page_region
= cupsGetOption("AP_FIRSTPAGE_PageRegion", num_options
,
2373 doc
->ap_page_size
= cupsGetOption("AP_FIRSTPAGE_PageSize", num_options
,
2376 if ((choice
= ppdFindMarkedChoice(ppd
, "InputSlot")) != NULL
)
2377 doc
->input_slot
= choice
->choice
;
2378 if ((choice
= ppdFindMarkedChoice(ppd
, "ManualFeed")) != NULL
)
2379 doc
->manual_feed
= choice
->choice
;
2380 if ((choice
= ppdFindMarkedChoice(ppd
, "MediaColor")) != NULL
)
2381 doc
->media_color
= choice
->choice
;
2382 if ((choice
= ppdFindMarkedChoice(ppd
, "MediaType")) != NULL
)
2383 doc
->media_type
= choice
->choice
;
2384 if ((choice
= ppdFindMarkedChoice(ppd
, "PageRegion")) != NULL
)
2385 doc
->page_region
= choice
->choice
;
2386 if ((choice
= ppdFindMarkedChoice(ppd
, "PageSize")) != NULL
)
2387 doc
->page_size
= choice
->choice
;
2390 * collate, multiple-document-handling
2393 if ((val
= cupsGetOption("multiple-document-handling", num_options
, options
)) != NULL
)
2396 * This IPP attribute is unnecessarily complicated...
2398 * single-document, separate-documents-collated-copies, and
2399 * single-document-new-sheet all require collated copies.
2401 * separate-documents-uncollated-copies allows for uncollated copies.
2404 doc
->collate
= _cups_strcasecmp(val
, "separate-documents-uncollated-copies") != 0;
2407 if ((val
= cupsGetOption("Collate", num_options
, options
)) != NULL
&&
2408 (!_cups_strcasecmp(val
, "true") ||!_cups_strcasecmp(val
, "on") ||
2409 !_cups_strcasecmp(val
, "yes")))
2416 if ((val
= cupsGetOption("emit-jcl", num_options
, options
)) != NULL
&&
2417 (!_cups_strcasecmp(val
, "false") || !_cups_strcasecmp(val
, "off") ||
2418 !_cups_strcasecmp(val
, "no") || !strcmp(val
, "0")))
2424 * fitplot/fit-to-page/ipp-attribute-fidelity
2426 * (Only for original PostScript content)
2429 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
2430 content_type
= "application/postscript";
2432 if (!_cups_strcasecmp(content_type
, "application/postscript"))
2434 if ((val
= cupsGetOption("fitplot", num_options
, options
)) != NULL
&&
2435 !_cups_strcasecmp(val
, "true"))
2437 else if ((val
= cupsGetOption("fit-to-page", num_options
, options
)) != NULL
&&
2438 !_cups_strcasecmp(val
, "true"))
2440 else if ((val
= cupsGetOption("ipp-attribute-fidelity", num_options
,
2441 options
)) != NULL
&&
2442 !_cups_strcasecmp(val
, "true"))
2447 * mirror/MirrorPrint
2450 if ((choice
= ppdFindMarkedChoice(ppd
, "MirrorPrint")) != NULL
)
2452 val
= choice
->choice
;
2456 val
= cupsGetOption("mirror", num_options
, options
);
2458 if (val
&& (!_cups_strcasecmp(val
, "true") || !_cups_strcasecmp(val
, "on") ||
2459 !_cups_strcasecmp(val
, "yes")))
2466 if ((val
= cupsGetOption("number-up", num_options
, options
)) != NULL
)
2468 switch (intval
= atoi(val
))
2476 doc
->number_up
= intval
;
2479 _cupsLangPrintFilter(stderr
, "ERROR",
2480 _("Unsupported number-up value %d, using "
2481 "number-up=1."), intval
);
2493 if ((val
= cupsGetOption("number-up-layout", num_options
, options
)) != NULL
)
2495 if (!_cups_strcasecmp(val
, "lrtb"))
2496 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2497 else if (!_cups_strcasecmp(val
, "lrbt"))
2498 doc
->number_up_layout
= PSTOPS_LAYOUT_LRBT
;
2499 else if (!_cups_strcasecmp(val
, "rltb"))
2500 doc
->number_up_layout
= PSTOPS_LAYOUT_RLTB
;
2501 else if (!_cups_strcasecmp(val
, "rlbt"))
2502 doc
->number_up_layout
= PSTOPS_LAYOUT_RLBT
;
2503 else if (!_cups_strcasecmp(val
, "tblr"))
2504 doc
->number_up_layout
= PSTOPS_LAYOUT_TBLR
;
2505 else if (!_cups_strcasecmp(val
, "tbrl"))
2506 doc
->number_up_layout
= PSTOPS_LAYOUT_TBRL
;
2507 else if (!_cups_strcasecmp(val
, "btlr"))
2508 doc
->number_up_layout
= PSTOPS_LAYOUT_BTLR
;
2509 else if (!_cups_strcasecmp(val
, "btrl"))
2510 doc
->number_up_layout
= PSTOPS_LAYOUT_BTRL
;
2513 _cupsLangPrintFilter(stderr
, "ERROR",
2514 _("Unsupported number-up-layout value %s, using "
2515 "number-up-layout=lrtb."), val
);
2516 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2520 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2526 if ((val
= cupsGetOption("OutputOrder", num_options
, options
)) != NULL
)
2528 if (!_cups_strcasecmp(val
, "Reverse"))
2529 doc
->output_order
= 1;
2534 * Figure out the right default output order from the PPD file...
2537 if ((choice
= ppdFindMarkedChoice(ppd
, "OutputBin")) != NULL
&&
2538 (attr
= ppdFindAttr(ppd
, "PageStackOrder", choice
->choice
)) != NULL
&&
2540 doc
->output_order
= !_cups_strcasecmp(attr
->value
, "Reverse");
2541 else if ((attr
= ppdFindAttr(ppd
, "DefaultOutputOrder", NULL
)) != NULL
&&
2543 doc
->output_order
= !_cups_strcasecmp(attr
->value
, "Reverse");
2550 if ((val
= cupsGetOption("page-border", num_options
, options
)) != NULL
)
2552 if (!_cups_strcasecmp(val
, "none"))
2553 doc
->page_border
= PSTOPS_BORDERNONE
;
2554 else if (!_cups_strcasecmp(val
, "single"))
2555 doc
->page_border
= PSTOPS_BORDERSINGLE
;
2556 else if (!_cups_strcasecmp(val
, "single-thick"))
2557 doc
->page_border
= PSTOPS_BORDERSINGLE2
;
2558 else if (!_cups_strcasecmp(val
, "double"))
2559 doc
->page_border
= PSTOPS_BORDERDOUBLE
;
2560 else if (!_cups_strcasecmp(val
, "double-thick"))
2561 doc
->page_border
= PSTOPS_BORDERDOUBLE2
;
2564 _cupsLangPrintFilter(stderr
, "ERROR",
2565 _("Unsupported page-border value %s, using "
2566 "page-border=none."), val
);
2567 doc
->page_border
= PSTOPS_BORDERNONE
;
2571 doc
->page_border
= PSTOPS_BORDERNONE
;
2577 doc
->page_label
= cupsGetOption("page-label", num_options
, options
);
2583 doc
->page_ranges
= cupsGetOption("page-ranges", num_options
, options
);
2589 doc
->page_set
= cupsGetOption("page-set", num_options
, options
);
2592 * Now figure out if we have to force collated copies, etc.
2595 if (ppd
&& ppd
->manual_copies
&& Duplex
&& doc
->copies
> 1)
2598 * Force collated copies when printing a duplexed document to
2599 * a non-PS printer that doesn't do hardware copy generation.
2600 * Otherwise the copies will end up on the front/back side of
2608 * See if we have to filter the fast or slow way...
2611 if (doc
->collate
&& doc
->copies
> 1)
2614 * See if we need to manually collate the pages...
2617 doc
->slow_collate
= 1;
2619 if ((choice
= ppdFindMarkedChoice(ppd
, "Collate")) != NULL
&&
2620 !_cups_strcasecmp(choice
->choice
, "True"))
2623 * Hardware collate option is selected, see if the option is
2624 * conflicting - if not, collate in hardware. Otherwise,
2625 * turn the hardware collate option off...
2628 if ((option
= ppdFindOption(ppd
, "Collate")) != NULL
&&
2629 !option
->conflicted
)
2630 doc
->slow_collate
= 0;
2632 ppdMarkOption(ppd
, "Collate", "False");
2636 doc
->slow_collate
= 0;
2638 if (!ppdFindOption(ppd
, "OutputOrder") && doc
->output_order
)
2639 doc
->slow_order
= 1;
2641 doc
->slow_order
= 0;
2644 (doc
->slow_collate
|| doc
->slow_order
||
2645 ((attr
= ppdFindAttr(ppd
, "cupsEvenDuplex", NULL
)) != NULL
&&
2646 attr
->value
&& !_cups_strcasecmp(attr
->value
, "true"))))
2647 doc
->slow_duplex
= 1;
2649 doc
->slow_duplex
= 0;
2652 * Create a temporary file for page data if we need to filter slowly...
2655 if (doc
->slow_order
|| doc
->slow_collate
)
2657 if ((doc
->temp
= cupsTempFile2(doc
->tempfile
,
2658 sizeof(doc
->tempfile
))) == NULL
)
2660 perror("DEBUG: Unable to create temporary file");
2666 * Figure out if we should use ESPshowpage or not...
2669 if (doc
->page_label
|| getenv("CLASSIFICATION") || doc
->number_up
> 1 ||
2673 * Yes, use ESPshowpage...
2676 doc
->use_ESPshowpage
= 1;
2679 fprintf(stderr
, "DEBUG: slow_collate=%d, slow_duplex=%d, slow_order=%d\n",
2680 doc
->slow_collate
, doc
->slow_duplex
, doc
->slow_order
);
2685 * 'skip_page()' - Skip past a page that won't be printed.
2688 static ssize_t
/* O - Length of next line */
2689 skip_page(cups_file_t
*fp
, /* I - File to read from */
2690 char *line
, /* I - Line buffer */
2691 ssize_t linelen
, /* I - Length of initial line */
2692 size_t linesize
) /* I - Size of line buffer */
2694 int level
; /* Embedded document level */
2699 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
2702 (!strncmp(line
, "%%Page:", 7) || !strncmp(line
, "%%Trailer", 9)))
2704 else if (!strncmp(line
, "%%BeginDocument", 15) ||
2705 !strncmp(line
, "%ADO_BeginApplication", 21))
2707 else if ((!strncmp(line
, "%%EndDocument", 13) ||
2708 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
2710 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
2711 (!strncmp(line
, "%%BeginData:", 12) &&
2712 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
2715 * Skip binary data...
2718 int bytes
; /* Bytes of data */
2721 bytes
= atoi(strchr(line
, ':') + 1);
2725 if (bytes
> linesize
)
2726 linelen
= cupsFileRead(fp
, line
, linesize
);
2728 linelen
= cupsFileRead(fp
, line
, bytes
);
2733 perror("ERROR: Early end-of-file while reading binary data");
2747 * 'start_nup()' - Start processing for N-up printing.
2751 start_nup(pstops_doc_t
*doc
, /* I - Document information */
2752 int number
, /* I - Page number */
2753 int show_border
, /* I - Show the border? */
2754 const int *bounding_box
) /* I - BoundingBox value */
2756 int pos
; /* Position on page */
2757 int x
, y
; /* Relative position of subpage */
2758 float w
, l
, /* Width and length of subpage */
2759 tx
, ty
; /* Translation values for subpage */
2760 float pagew
, /* Printable width of page */
2761 pagel
; /* Printable height of page */
2762 int bboxx
, /* BoundingBox X origin */
2763 bboxy
, /* BoundingBox Y origin */
2764 bboxw
, /* BoundingBox width */
2765 bboxl
; /* BoundingBox height */
2766 float margin
= 0; /* Current margin for border */
2769 if (doc
->number_up
> 1)
2770 doc_puts(doc
, "userdict/ESPsave save put\n");
2772 pos
= (number
- 1) % doc
->number_up
;
2773 pagew
= PageRight
- PageLeft
;
2774 pagel
= PageTop
- PageBottom
;
2778 bboxx
= bounding_box
[0];
2779 bboxy
= bounding_box
[1];
2780 bboxw
= bounding_box
[2] - bounding_box
[0];
2781 bboxl
= bounding_box
[3] - bounding_box
[1];
2791 fprintf(stderr
, "DEBUG: pagew = %.1f, pagel = %.1f\n", pagew
, pagel
);
2792 fprintf(stderr
, "DEBUG: bboxx = %d, bboxy = %d, bboxw = %d, bboxl = %d\n",
2793 bboxx
, bboxy
, bboxw
, bboxl
);
2794 fprintf(stderr
, "DEBUG: PageLeft = %.1f, PageRight = %.1f\n",
2795 PageLeft
, PageRight
);
2796 fprintf(stderr
, "DEBUG: PageTop = %.1f, PageBottom = %.1f\n",
2797 PageTop
, PageBottom
);
2798 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
2799 PageWidth
, PageLength
);
2801 switch (Orientation
)
2803 case 1 : /* Landscape */
2804 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", PageLength
);
2806 case 2 : /* Reverse Portrait */
2807 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", PageWidth
,
2810 case 3 : /* Reverse Landscape */
2811 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", PageWidth
);
2816 * Mirror the page as needed...
2820 doc_printf(doc
, "%.1f 0.0 translate -1 1 scale\n", PageWidth
);
2823 * Offset and scale as necessary for fitplot/fit-to-page/number-up...
2826 if (Duplex
&& doc
->number_up
> 1 && ((number
/ doc
->number_up
) & 1))
2827 doc_printf(doc
, "%.1f %.1f translate\n", PageWidth
- PageRight
, PageBottom
);
2828 else if (doc
->number_up
> 1 || doc
->fitplot
)
2829 doc_printf(doc
, "%.1f %.1f translate\n", PageLeft
, PageBottom
);
2831 switch (doc
->number_up
)
2837 l
= w
* bboxl
/ bboxw
;
2842 w
= l
* bboxw
/ bboxl
;
2845 tx
= 0.5 * (pagew
- w
);
2846 ty
= 0.5 * (pagel
- l
);
2848 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n", tx
, ty
,
2849 w
/ bboxw
, l
/ bboxl
);
2856 if (Orientation
& 1)
2860 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2864 l
= w
* bboxl
/ bboxw
;
2866 if (l
> (pagew
* 0.5))
2869 w
= l
* bboxw
/ bboxl
;
2872 tx
= 0.5 * (pagew
* 0.5 - l
);
2873 ty
= 0.5 * (pagel
- w
);
2875 if (doc
->normal_landscape
)
2876 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2878 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2880 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2881 ty
, tx
+ pagew
* 0.5 * x
, w
/ bboxw
, l
/ bboxl
);
2887 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2891 w
= l
* bboxw
/ bboxl
;
2893 if (w
> (pagel
* 0.5))
2896 l
= w
* bboxl
/ bboxw
;
2899 tx
= 0.5 * (pagel
* 0.5 - w
);
2900 ty
= 0.5 * (pagew
- l
);
2902 if (doc
->normal_landscape
)
2903 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2905 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2907 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2908 tx
+ pagel
* 0.5 * x
, ty
, w
/ bboxw
, l
/ bboxl
);
2913 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2924 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2927 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2931 l
= w
* bboxl
/ bboxw
;
2933 if (l
> (pagel
* 0.5))
2936 w
= l
* bboxw
/ bboxl
;
2939 tx
= 0.5 * (pagew
* 0.5 - w
);
2940 ty
= 0.5 * (pagel
* 0.5 - l
);
2942 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2943 tx
+ x
* pagew
* 0.5, ty
+ y
* pagel
* 0.5,
2944 w
/ bboxw
, l
/ bboxl
);
2948 if (Orientation
& 1)
2950 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2955 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2958 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2966 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2969 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2974 l
= w
* bboxl
/ bboxw
;
2976 if (l
> (pagew
* 0.333))
2979 w
= l
* bboxw
/ bboxl
;
2982 tx
= 0.5 * (pagel
- 2 * w
);
2983 ty
= 0.5 * (pagew
- 3 * l
);
2985 if (doc
->normal_landscape
)
2986 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
2988 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
2990 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2991 tx
+ x
* w
, ty
+ y
* l
, l
/ bboxl
, w
/ bboxw
);
2995 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3000 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3003 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3011 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3014 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3019 w
= l
* bboxw
/ bboxl
;
3021 if (w
> (pagel
* 0.333))
3024 l
= w
* bboxl
/ bboxw
;
3027 tx
= 0.5 * (pagel
- 3 * w
);
3028 ty
= 0.5 * (pagew
- 2 * l
);
3030 if (doc
->normal_landscape
)
3031 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
3033 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
3035 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3036 tx
+ w
* x
, ty
+ l
* y
, w
/ bboxw
, l
/ bboxl
);
3042 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3053 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3056 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3060 l
= w
* bboxl
/ bboxw
;
3062 if (l
> (pagel
* 0.333))
3065 w
= l
* bboxw
/ bboxl
;
3068 tx
= 0.5 * (pagew
* 0.333 - w
);
3069 ty
= 0.5 * (pagel
* 0.333 - l
);
3071 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3072 tx
+ x
* pagew
* 0.333, ty
+ y
* pagel
* 0.333,
3073 w
/ bboxw
, l
/ bboxl
);
3077 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3088 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3091 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3095 l
= w
* bboxl
/ bboxw
;
3097 if (l
> (pagel
* 0.25))
3100 w
= l
* bboxw
/ bboxl
;
3103 tx
= 0.5 * (pagew
* 0.25 - w
);
3104 ty
= 0.5 * (pagel
* 0.25 - l
);
3106 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3107 tx
+ x
* pagew
* 0.25, ty
+ y
* pagel
* 0.25,
3108 w
/ bboxw
, l
/ bboxl
);
3113 * Draw borders as necessary...
3116 if (doc
->page_border
&& show_border
)
3118 int rects
; /* Number of border rectangles */
3119 float fscale
; /* Scaling value for points */
3122 rects
= (doc
->page_border
& PSTOPS_BORDERDOUBLE
) ? 2 : 1;
3123 fscale
= PageWidth
/ w
;
3124 margin
= 2.25 * fscale
;
3127 * Set the line width and color...
3130 doc_puts(doc
, "gsave\n");
3131 doc_printf(doc
, "%.3f setlinewidth 0 setgray newpath\n",
3132 (doc
->page_border
& PSTOPS_BORDERTHICK
) ? 0.5 * fscale
:
3136 * Draw border boxes...
3139 for (; rects
> 0; rects
--, margin
+= 2 * fscale
)
3140 if (doc
->number_up
> 1)
3141 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3145 bboxl
- 2 * margin
);
3147 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3149 PageBottom
+ margin
,
3150 PageRight
- PageLeft
- 2 * margin
,
3151 PageTop
- PageBottom
- 2 * margin
);
3154 * Restore pen settings...
3157 doc_puts(doc
, "grestore\n");
3163 * Offset the page by its bounding box...
3166 doc_printf(doc
, "%d %d translate\n", -bounding_box
[0],
3170 if (doc
->fitplot
|| doc
->number_up
> 1)
3173 * Clip the page to the page's bounding box...
3176 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrc\n",
3177 bboxx
+ margin
, bboxy
+ margin
,
3178 bboxw
- 2 * margin
, bboxl
- 2 * margin
);
3184 * 'write_label_prolog()' - Write the prolog with the classification
3189 write_label_prolog(pstops_doc_t
*doc
, /* I - Document info */
3190 const char *label
, /* I - Page label */
3191 float bottom
, /* I - Bottom position in points */
3192 float top
, /* I - Top position in points */
3193 float width
) /* I - Width in points */
3195 const char *classification
; /* CLASSIFICATION environment variable */
3196 const char *ptr
; /* Temporary string pointer */
3200 * First get the current classification...
3203 if ((classification
= getenv("CLASSIFICATION")) == NULL
)
3204 classification
= "";
3205 if (strcmp(classification
, "none") == 0)
3206 classification
= "";
3209 * If there is nothing to show, bind an empty 'write labels' procedure
3213 if (!classification
[0] && (label
== NULL
|| !label
[0]))
3215 doc_puts(doc
, "userdict/ESPwl{}bind put\n");
3220 * Set the classification + page label string...
3223 doc_puts(doc
, "userdict");
3224 if (!strcmp(classification
, "confidential"))
3225 doc_puts(doc
, "/ESPpl(CONFIDENTIAL");
3226 else if (!strcmp(classification
, "classified"))
3227 doc_puts(doc
, "/ESPpl(CLASSIFIED");
3228 else if (!strcmp(classification
, "secret"))
3229 doc_puts(doc
, "/ESPpl(SECRET");
3230 else if (!strcmp(classification
, "topsecret"))
3231 doc_puts(doc
, "/ESPpl(TOP SECRET");
3232 else if (!strcmp(classification
, "unclassified"))
3233 doc_puts(doc
, "/ESPpl(UNCLASSIFIED");
3236 doc_puts(doc
, "/ESPpl(");
3238 for (ptr
= classification
; *ptr
; ptr
++)
3240 if (*ptr
< 32 || *ptr
> 126)
3241 doc_printf(doc
, "\\%03o", *ptr
);
3242 else if (*ptr
== '_')
3244 else if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
3245 doc_printf(doc
, "\\%c", *ptr
);
3247 doc_printf(doc
, "%c", *ptr
);
3253 if (classification
[0])
3254 doc_puts(doc
, " - ");
3257 * Quote the label string as needed...
3260 for (ptr
= label
; *ptr
; ptr
++)
3262 if (*ptr
< 32 || *ptr
> 126)
3263 doc_printf(doc
, "\\%03o", *ptr
);
3264 else if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
3265 doc_printf(doc
, "\\%c", *ptr
);
3267 doc_printf(doc
, "%c", *ptr
);
3271 doc_puts(doc
, ")put\n");
3274 * Then get a 14 point Helvetica-Bold font...
3277 doc_puts(doc
, "userdict/ESPpf /Helvetica-Bold findfont 14 scalefont put\n");
3280 * Finally, the procedure to write the labels on the page...
3283 doc_puts(doc
, "userdict/ESPwl{\n");
3284 doc_puts(doc
, " ESPpf setfont\n");
3285 doc_printf(doc
, " ESPpl stringwidth pop dup 12 add exch -0.5 mul %.0f add\n",
3287 doc_puts(doc
, " 1 setgray\n");
3288 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrf\n", bottom
- 2.0);
3289 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrf\n", top
- 18.0);
3290 doc_puts(doc
, " 0 setgray\n");
3291 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrs\n", bottom
- 2.0);
3292 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrs\n", top
- 18.0);
3293 doc_printf(doc
, " dup %.0f moveto ESPpl show\n", bottom
+ 2.0);
3294 doc_printf(doc
, " %.0f moveto ESPpl show\n", top
- 14.0);
3295 doc_puts(doc
, "pop\n");
3296 doc_puts(doc
, "}bind put\n");
3301 * 'write_labels()' - Write the actual page labels.
3303 * This function is a copy of the one in common.c since we need to
3304 * use doc_puts/doc_printf instead of puts/printf...
3308 write_labels(pstops_doc_t
*doc
, /* I - Document information */
3309 int orient
) /* I - Orientation of the page */
3311 float width
, /* Width of page */
3312 length
; /* Length of page */
3315 doc_puts(doc
, "gsave\n");
3317 if ((orient
^ Orientation
) & 1)
3325 length
= PageLength
;
3330 case 1 : /* Landscape */
3331 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", length
);
3333 case 2 : /* Reverse Portrait */
3334 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", width
, length
);
3336 case 3 : /* Reverse Landscape */
3337 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", width
);
3341 doc_puts(doc
, "ESPwl\n");
3342 doc_puts(doc
, "grestore\n");
3347 * 'write_options()' - Write options provided via %%IncludeFeature.
3352 pstops_doc_t
*doc
, /* I - Document */
3353 ppd_file_t
*ppd
, /* I - PPD file */
3354 int num_options
, /* I - Number of options */
3355 cups_option_t
*options
) /* I - Options */
3357 int i
; /* Looping var */
3358 ppd_option_t
*option
; /* PPD option */
3359 int min_order
; /* Minimum OrderDependency value */
3360 char *doc_setup
, /* DocumentSetup commands to send */
3361 *any_setup
; /* AnySetup commands to send */
3365 * Figure out the minimum OrderDependency value...
3368 if ((option
= ppdFindOption(ppd
, "PageRegion")) != NULL
)
3369 min_order
= option
->order
;
3373 for (i
= 0; i
< num_options
; i
++)
3374 if ((option
= ppdFindOption(ppd
, options
[i
].name
)) != NULL
&&
3375 option
->order
< min_order
)
3376 min_order
= option
->order
;
3379 * Mark and extract them...
3382 cupsMarkOptions(ppd
, num_options
, options
);
3384 doc_setup
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, min_order
);
3385 any_setup
= ppdEmitString(ppd
, PPD_ORDER_ANY
, min_order
);
3388 * Then send them out...
3391 if (doc
->number_up
> 1)
3394 * Temporarily restore setpagedevice so we can set the options...
3397 doc_puts(doc
, "userdict/setpagedevice/CUPSsetpagedevice load put\n");
3402 doc_puts(doc
, doc_setup
);
3408 doc_puts(doc
, any_setup
);
3412 if (doc
->number_up
> 1)
3415 * Disable setpagedevice again...
3418 doc_puts(doc
, "userdict/setpagedevice{pop}bind put\n");
3424 * End of "$Id: pstops.c 7977 2008-09-23 23:44:33Z mike $".