]>
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 the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 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/i18n.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 float brightness
; /* brightness value */
126 int collate
, /* Collate copies? */
127 emit_jcl
, /* Emit JCL commands? */
128 fitplot
; /* Fit pages to media */
129 float gamma
; /* gamma value */
130 const char *input_slot
, /* InputSlot value */
131 *manual_feed
; /* ManualFeed value */
132 int mirror
, /* doc->mirror/mirror pages */
133 number_up
, /* Number of pages on each sheet */
134 number_up_layout
, /* doc->number_up_layout of N-up pages */
135 output_order
, /* Requested reverse output order? */
136 page_border
; /* doc->page_border around pages */
137 const char *page_label
, /* page-label option, if any */
138 *page_ranges
, /* page-ranges option, if any */
139 *page_set
; /* page-set option, if any */
144 * Convenience macros...
147 #define is_first_page(p) (doc->number_up == 1 || \
148 ((p) % doc->number_up) == 1)
149 #define is_last_page(p) (doc->number_up == 1 || \
150 ((p) % doc->number_up) == 0)
151 #define is_not_last_page(p) (doc->number_up > 1 && \
152 ((p) % doc->number_up) != 0)
159 static int JobCanceled
= 0;/* Set to 1 on SIGTERM */
166 static pstops_page_t
*add_page(pstops_doc_t
*doc
, const char *label
);
167 static void cancel_job(int sig
);
168 static int check_range(pstops_doc_t
*doc
, int page
);
169 static void copy_bytes(cups_file_t
*fp
, off_t offset
,
171 static ssize_t
copy_comments(cups_file_t
*fp
, pstops_doc_t
*doc
,
172 ppd_file_t
*ppd
, char *line
,
173 ssize_t linelen
, size_t linesize
);
174 static void copy_dsc(cups_file_t
*fp
, pstops_doc_t
*doc
,
175 ppd_file_t
*ppd
, char *line
, ssize_t linelen
,
177 static void copy_non_dsc(cups_file_t
*fp
, pstops_doc_t
*doc
,
178 ppd_file_t
*ppd
, char *line
,
179 ssize_t linelen
, size_t linesize
);
180 static ssize_t
copy_page(cups_file_t
*fp
, pstops_doc_t
*doc
,
181 ppd_file_t
*ppd
, int number
, char *line
,
182 ssize_t linelen
, size_t linesize
);
183 static ssize_t
copy_prolog(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_setup(cups_file_t
*fp
, pstops_doc_t
*doc
,
187 ppd_file_t
*ppd
, char *line
,
188 ssize_t linelen
, size_t linesize
);
189 static ssize_t
copy_trailer(cups_file_t
*fp
, pstops_doc_t
*doc
,
190 ppd_file_t
*ppd
, int number
, char *line
,
191 ssize_t linelen
, size_t linesize
);
192 static void do_prolog(pstops_doc_t
*doc
, ppd_file_t
*ppd
);
193 static void do_setup(pstops_doc_t
*doc
, ppd_file_t
*ppd
);
194 static void doc_printf(pstops_doc_t
*doc
, const char *format
, ...)
196 __attribute__ ((__format__ (__printf__
, 2, 3)))
197 #endif /* __GNUC__ */
199 static void doc_puts(pstops_doc_t
*doc
, const char *s
);
200 static void doc_write(pstops_doc_t
*doc
, const char *s
, size_t len
);
201 static void end_nup(pstops_doc_t
*doc
, int number
);
202 static int include_feature(ppd_file_t
*ppd
, const char *line
,
204 cups_option_t
**options
);
205 static char *parse_text(const char *start
, char **end
, char *buffer
,
207 static void set_pstops_options(pstops_doc_t
*doc
, ppd_file_t
*ppd
,
208 char *argv
[], int num_options
,
209 cups_option_t
*options
);
210 static ssize_t
skip_page(cups_file_t
*fp
, char *line
, ssize_t linelen
,
212 static void start_nup(pstops_doc_t
*doc
, int number
,
213 int show_border
, const int *bounding_box
);
214 static void write_label_prolog(pstops_doc_t
*doc
, const char *label
,
215 float bottom
, float top
,
217 static void write_labels(pstops_doc_t
*doc
, int orient
);
218 static void write_options(pstops_doc_t
*doc
, ppd_file_t
*ppd
,
219 int num_options
, cups_option_t
*options
);
223 * 'main()' - Main entry.
226 int /* O - Exit status */
227 main(int argc
, /* I - Number of command-line args */
228 char *argv
[]) /* I - Command-line arguments */
230 pstops_doc_t doc
; /* Document information */
231 cups_file_t
*fp
; /* Print file */
232 ppd_file_t
*ppd
; /* PPD file */
233 int num_options
; /* Number of print options */
234 cups_option_t
*options
; /* Print options */
235 char line
[8192]; /* Line buffer */
236 size_t len
; /* Length of line buffer */
237 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
238 struct sigaction action
; /* Actions for POSIX signals */
239 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
243 * Make sure status messages are not buffered...
246 setbuf(stderr
, NULL
);
249 * Check command-line...
252 if (argc
< 6 || argc
> 7)
254 fprintf(stderr
, _("Usage: %s job-id user title copies options [file]\n"),
260 * Register a signal handler to cleanly cancel a job.
263 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
264 sigset(SIGTERM
, cancel_job
);
265 #elif defined(HAVE_SIGACTION)
266 memset(&action
, 0, sizeof(action
));
268 sigemptyset(&action
.sa_mask
);
269 action
.sa_handler
= cancel_job
;
270 sigaction(SIGTERM
, &action
, NULL
);
272 signal(SIGTERM
, cancel_job
);
273 #endif /* HAVE_SIGSET */
276 * If we have 7 arguments, print the file named on the command-line.
277 * Otherwise, send stdin instead...
281 fp
= cupsFileStdin();
285 * Try to open the print file...
288 if ((fp
= cupsFileOpen(argv
[6], "r")) == NULL
)
290 fprintf(stderr
, _("ERROR: Unable to open file \"%s\" - %s\n"),
291 argv
[6], strerror(errno
));
297 * Read the first line to see if we have DSC comments...
300 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
302 fputs(_("ERROR: Empty print file!\n"), stderr
);
307 * Process command-line options...
311 num_options
= cupsParseOptions(argv
[5], 0, &options
);
312 ppd
= SetCommonOptions(num_options
, options
, 1);
314 set_pstops_options(&doc
, ppd
, argv
, num_options
, options
);
317 * Write any "exit server" options that have been selected...
320 ppdEmit(ppd
, stdout
, PPD_ORDER_EXIT
);
323 * Write any JCL commands that are needed to print PostScript code...
327 ppdEmitJCL(ppd
, stdout
, doc
.job_id
, doc
.user
, doc
.title
);
330 * Start with a DSC header...
333 puts("%!PS-Adobe-3.0");
336 * Skip leading PJL in the document...
339 while (!strncmp(line
, "\033%-12345X", 9) || !strncmp(line
, "@PJL ", 5))
342 * Yup, we have leading PJL fun, so skip it until we hit the line
343 * with "ENTER LANGUAGE"...
346 fputs("DEBUG: Skipping PJL header...\n", stderr
);
348 while (strstr(line
, "ENTER LANGUAGE") == NULL
&& strncmp(line
, "%!", 2))
349 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
352 if (!strncmp(line
, "%!", 2))
355 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
360 * Now see if the document conforms to the Adobe Document Structuring
364 if (!strncmp(line
, "%!PS-Adobe-", 11))
367 * Yes, filter the document...
370 copy_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
375 * No, display an error message and treat the file as if it contains
379 copy_non_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
383 * Send %%EOF as needed...
390 * End the job with the appropriate JCL command or CTRL-D...
395 if (ppd
&& ppd
->jcl_end
)
396 ppdEmitJCLEnd(ppd
, stdout
);
402 * Close files and remove the temporary file if needed...
407 cupsFileClose(doc
.temp
);
408 unlink(doc
.tempfile
);
412 cupsFreeOptions(num_options
, options
);
421 * 'add_page()' - Add a page to the pages array.
424 static pstops_page_t
* /* O - New page info object */
425 add_page(pstops_doc_t
*doc
, /* I - Document information */
426 const char *label
) /* I - Page label */
428 pstops_page_t
*pageinfo
; /* New page info object */
432 doc
->pages
= cupsArrayNew(NULL
, NULL
);
436 fprintf(stderr
, _("EMERG: Unable to allocate memory for pages array: %s\n"),
441 if ((pageinfo
= calloc(1, sizeof(pstops_page_t
))) == NULL
)
443 fprintf(stderr
, _("EMERG: Unable to allocate memory for page info: %s\n"),
448 pageinfo
->label
= strdup(label
);
449 pageinfo
->offset
= cupsFileTell(doc
->temp
);
451 cupsArrayAdd(doc
->pages
, pageinfo
);
460 * 'cancel_job()' - Flag the job as canceled.
464 cancel_job(int sig
) /* I - Signal number (unused) */
473 * 'check_range()' - Check to see if the current page is selected for
477 static int /* O - 1 if selected, 0 otherwise */
478 check_range(pstops_doc_t
*doc
, /* I - Document information */
479 int page
) /* I - Page number */
481 const char *range
; /* Pointer into range string */
482 int lower
, upper
; /* Lower and upper page numbers */
488 * See if we only print even or odd pages...
491 if (!strcasecmp(doc
->page_set
, "even") && (page
& 1))
494 if (!strcasecmp(doc
->page_set
, "odd") && !(page
& 1))
498 if (!doc
->page_ranges
)
499 return (1); /* No range, print all pages... */
501 for (range
= doc
->page_ranges
; *range
!= '\0';)
507 upper
= strtol(range
, (char **)&range
, 10);
511 lower
= strtol(range
, (char **)&range
, 10);
516 if (!isdigit(*range
& 255))
519 upper
= strtol(range
, (char **)&range
, 10);
525 if (page
>= lower
&& page
<= upper
)
539 * 'copy_bytes()' - Copy bytes from the input file to stdout.
543 copy_bytes(cups_file_t
*fp
, /* I - File to read from */
544 off_t offset
, /* I - Offset to page data */
545 size_t length
) /* I - Length of page data */
547 char buffer
[8192]; /* Data buffer */
548 ssize_t nbytes
; /* Number of bytes read */
549 size_t nleft
; /* Number of bytes left/remaining */
554 if (cupsFileSeek(fp
, offset
) < 0)
557 #ifdef HAVE_LONG_LONG
558 _("ERROR: Unable to seek to offset %lld in file - %s\n"),
560 _("ERROR: Unable to seek to offset %ld in file - %s\n"),
561 #endif /* HAVE_LONG_LONG */
562 CUPS_LLCAST offset
, strerror(errno
));
566 while (nleft
> 0 || length
== 0)
568 if (nleft
> sizeof(buffer
) || length
== 0)
569 nbytes
= sizeof(buffer
);
573 if ((nbytes
= cupsFileRead(fp
, buffer
, nbytes
)) < 1)
578 fwrite(buffer
, 1, nbytes
, stdout
);
584 * 'copy_comments()' - Copy all of the comments section.
586 * This function expects "line" to be filled with a comment line.
587 * On return, "line" will contain the next line in the file, if any.
590 static ssize_t
/* O - Length of next line */
591 copy_comments(cups_file_t
*fp
, /* I - File to read from */
592 pstops_doc_t
*doc
, /* I - Document info */
593 ppd_file_t
*ppd
, /* I - PPD file */
594 char *line
, /* I - Line buffer */
595 ssize_t linelen
, /* I - Length of initial line */
596 size_t linesize
) /* I - Size of line buffer */
598 int saw_bounding_box
, /* Saw %%BoundingBox: comment? */
599 saw_for
, /* Saw %%For: comment? */
600 saw_pages
, /* Saw %%Pages: comment? */
601 saw_title
; /* Saw %%Title: comment? */
605 * Loop until we see %%EndComments or a non-comment line...
608 saw_bounding_box
= 0;
613 while (line
[0] == '%')
616 * Strip trailing whitespace...
623 if (!isspace(line
[linelen
] & 255))
626 line
[linelen
] = '\0';
633 fprintf(stderr
, "DEBUG: %s\n", line
);
636 * Pull the headers out...
639 if (!strncmp(line
, "%%Pages:", 8))
641 int pages
; /* Number of pages */
645 fputs(_("ERROR: Duplicate %%Pages: comment 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(_("ERROR: Duplicate %%BoundingBox: comment 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(_("ERROR: Bad %%BoundingBox: comment 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(_("ERROR: No %%BoundingBox: comment in header!\n"), stderr
);
758 fputs(_("ERROR: No %%Pages: comment in header!\n"), stderr
);
761 WriteTextComment("For", doc
->user
);
764 WriteTextComment("Title", doc
->title
);
766 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
769 * Tell the document processor the copy and duplex options
770 * that are required...
773 doc_printf(doc
, "%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
774 doc
->collate
? " collate" : "",
775 Duplex
? " duplex" : "");
778 * Apple uses RBI comments for various non-PPD options...
781 doc_printf(doc
, "%%RBINumCopies: %d\n", doc
->copies
);
786 * Tell the document processor the duplex option that is required...
790 doc_puts(doc
, "%%Requirements: duplex\n");
793 * Apple uses RBI comments for various non-PPD options...
796 doc_puts(doc
, "%RBINumCopies: 1\n");
799 doc_puts(doc
, "%%Pages: (atend)\n");
800 doc_puts(doc
, "%%BoundingBox: (atend)\n");
801 doc_puts(doc
, "%%EndComments\n");
808 * 'copy_dsc()' - Copy a DSC-conforming document.
810 * This function expects "line" to be filled with the %!PS-Adobe comment line.
814 copy_dsc(cups_file_t
*fp
, /* I - File to read from */
815 pstops_doc_t
*doc
, /* I - Document info */
816 ppd_file_t
*ppd
, /* I - PPD file */
817 char *line
, /* I - Line buffer */
818 ssize_t linelen
, /* I - Length of initial line */
819 size_t linesize
) /* I - Size of line buffer */
821 int number
; /* Page number */
822 pstops_page_t
*pageinfo
; /* Page information */
826 * Make sure we use ESPshowpage for EPS files...
829 if (strstr(line
, "EPSF"))
831 doc
->use_ESPshowpage
= 1;
836 * Start sending the document with any commands needed...
839 fprintf(stderr
, "DEBUG: Before copy_comments - %s", line
);
840 linelen
= copy_comments(fp
, doc
, ppd
, line
, linelen
, linesize
);
843 * Now find the prolog section, if any...
846 fprintf(stderr
, "DEBUG: Before copy_prolog - %s", line
);
847 linelen
= copy_prolog(fp
, doc
, ppd
, line
, linelen
, linesize
);
850 * Then the document setup section...
853 fprintf(stderr
, "DEBUG: Before copy_setup - %s", line
);
854 linelen
= copy_setup(fp
, doc
, ppd
, line
, linelen
, linesize
);
857 * Copy until we see %%Page:...
860 while (strncmp(line
, "%%Page:", 7) && strncmp(line
, "%%Trailer", 9))
862 doc_write(doc
, line
, linelen
);
864 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
869 * Then process pages until we have no more...
874 fprintf(stderr
, "DEBUG: Before page loop - %s", line
);
875 while (!strncmp(line
, "%%Page:", 7))
882 if (check_range(doc
, (number
- 1) / doc
->number_up
+ 1))
884 fprintf(stderr
, "DEBUG: Copying page %d...\n", number
);
885 linelen
= copy_page(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
889 fprintf(stderr
, "DEBUG: Skipping page %d...\n", number
);
890 linelen
= skip_page(fp
, line
, linelen
, linesize
);
895 * Finish up the last page(s)...
898 if (number
&& is_not_last_page(number
) && cupsArrayLast(doc
->pages
) &&
899 check_range(doc
, (number
- 1) / doc
->number_up
+ 1))
901 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
903 start_nup(doc
, doc
->number_up
, 0, doc
->bounding_box
);
904 doc_puts(doc
, "showpage\n");
905 end_nup(doc
, doc
->number_up
);
907 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
910 if (doc
->slow_duplex
&& (doc
->page
& 1))
913 * Make sure we have an even number of pages...
916 pageinfo
= add_page(doc
, "(filler)");
918 if (!doc
->slow_order
)
920 if (!ppd
|| !ppd
->num_filters
)
921 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
922 doc
->slow_collate
? 1 : doc
->copies
);
924 printf("%%%%Page: (filler) %d\n", doc
->page
);
927 start_nup(doc
, doc
->number_up
, 0, doc
->bounding_box
);
928 doc_puts(doc
, "showpage\n");
929 end_nup(doc
, doc
->number_up
);
931 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
935 * Make additional copies as necessary...
938 number
= doc
->slow_order
? 0 : doc
->page
;
940 if (doc
->temp
&& !JobCanceled
&& cupsArrayCount(doc
->pages
) > 0)
942 int copy
; /* Current copy */
946 * Reopen the temporary file for reading...
949 cupsFileClose(doc
->temp
);
951 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
957 if (doc
->slow_collate
)
958 copy
= !doc
->slow_order
;
960 copy
= doc
->copies
- 1;
962 for (; copy
< doc
->copies
; copy
++)
968 * Send end-of-job stuff followed by any start-of-job stuff required
969 * for the JCL options...
972 if (number
&& doc
->emit_jcl
&& ppd
&& ppd
->jcl_end
)
975 * Send the trailer...
979 printf("%%%%Pages: %d\n", cupsArrayCount(doc
->pages
));
980 if (doc
->number_up
> 1 || doc
->fitplot
)
981 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
982 PageLeft
, PageBottom
, PageRight
, PageTop
);
984 printf("%%%%BoundingBox: %d %d %d %d\n",
985 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
986 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
990 * Start a new document...
993 ppdEmitJCLEnd(ppd
, stdout
);
994 ppdEmitJCL(ppd
, stdout
, doc
->job_id
, doc
->user
, doc
->title
);
996 puts("%!PS-Adobe-3.0");
1002 * Copy the prolog as needed...
1007 pageinfo
= (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
1008 copy_bytes(doc
->temp
, 0, pageinfo
->offset
);
1012 * Then copy all of the pages...
1015 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayLast(doc
->pages
) :
1016 (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
1025 if (!ppd
|| !ppd
->num_filters
)
1026 fprintf(stderr
, "PAGE: %d %d\n", number
,
1027 doc
->slow_collate
? 1 : doc
->copies
);
1029 if (doc
->number_up
> 1)
1031 printf("%%%%Page: (%d) %d\n", number
, number
);
1032 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1033 PageLeft
, PageBottom
, PageRight
, PageTop
);
1037 printf("%%%%Page: %s %d\n", pageinfo
->label
, number
);
1038 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1039 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
1040 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
1043 copy_bytes(doc
->temp
, pageinfo
->offset
, pageinfo
->length
);
1045 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayPrev(doc
->pages
) :
1046 (pstops_page_t
*)cupsArrayNext(doc
->pages
);
1052 * Restore the old showpage operator as needed...
1055 if (doc
->use_ESPshowpage
)
1056 puts("userdict/showpage/ESPshowpage load put\n");
1059 * Write/copy the trailer...
1063 copy_trailer(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
1068 * 'copy_non_dsc()' - Copy a document that does not conform to the DSC.
1070 * This function expects "line" to be filled with the %! comment line.
1074 copy_non_dsc(cups_file_t
*fp
, /* I - File to read from */
1075 pstops_doc_t
*doc
, /* I - Document info */
1076 ppd_file_t
*ppd
, /* I - PPD file */
1077 char *line
, /* I - Line buffer */
1078 ssize_t linelen
, /* I - Length of initial line */
1079 size_t linesize
) /* I - Size of line buffer */
1081 int copy
; /* Current copy */
1082 char buffer
[8192]; /* Copy buffer */
1083 int bytes
; /* Number of bytes copied */
1087 * First let the user know that they are attempting to print a file
1088 * that may not print correctly...
1091 fputs(_("WARNING: This document does not conform to the Adobe Document "
1092 "Structuring Conventions and may not print correctly!\n"), stderr
);
1095 * Then write a standard DSC comment section...
1098 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", PageLeft
, PageBottom
,
1099 PageRight
, PageTop
);
1101 if (doc
->slow_collate
&& doc
->copies
> 1)
1102 printf("%%%%Pages: %d\n", doc
->copies
);
1106 WriteTextComment("For", doc
->user
);
1107 WriteTextComment("Title", doc
->title
);
1109 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1112 * Tell the document processor the copy and duplex options
1113 * that are required...
1116 printf("%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
1117 doc
->collate
? " collate" : "",
1118 Duplex
? " duplex" : "");
1121 * Apple uses RBI comments for various non-PPD options...
1124 printf("%%RBINumCopies: %d\n", doc
->copies
);
1129 * Tell the document processor the duplex option that is required...
1133 puts("%%Requirements: duplex");
1136 * Apple uses RBI comments for various non-PPD options...
1139 puts("%RBINumCopies: 1");
1142 puts("%%EndComments");
1145 * Then the prolog...
1148 puts("%%BeginProlog");
1150 do_prolog(doc
, ppd
);
1152 puts("%%EndProlog");
1155 * Then the setup section...
1158 puts("%%BeginSetup");
1165 * Finally, embed a copy of the file inside a %%Page...
1168 if (!ppd
|| !ppd
->num_filters
)
1169 fprintf(stderr
, "PAGE: 1 %d\n", doc
->temp
? 1 : doc
->copies
);
1171 puts("%%Page: 1 1");
1172 puts("%%BeginPageSetup");
1173 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1174 puts("%%EndPageSetup");
1175 puts("%%BeginDocument: nondsc");
1177 fwrite(line
, linelen
, 1, stdout
);
1180 cupsFileWrite(doc
->temp
, line
, linelen
);
1182 while ((bytes
= cupsFileRead(fp
, buffer
, sizeof(buffer
))) > 0)
1184 fwrite(buffer
, 1, bytes
, stdout
);
1187 cupsFileWrite(doc
->temp
, buffer
, bytes
);
1190 puts("%%EndDocument");
1192 if (doc
->use_ESPshowpage
)
1194 WriteLabels(Orientation
);
1195 puts("ESPshowpage");
1198 if (doc
->temp
&& !JobCanceled
)
1201 * Reopen the temporary file for reading...
1204 cupsFileClose(doc
->temp
);
1206 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
1209 * Make the additional copies as needed...
1212 for (copy
= 1; copy
< doc
->copies
; copy
++)
1217 if (!ppd
|| !ppd
->num_filters
)
1218 fputs("PAGE: 1 1\n", stderr
);
1220 printf("%%%%Page: %d %d\n", copy
+ 1, copy
+ 1);
1221 puts("%%BeginPageSetup");
1222 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1223 puts("%%EndPageSetup");
1224 puts("%%BeginDocument: nondsc");
1226 copy_bytes(doc
->temp
, 0, 0);
1228 puts("%%EndDocument");
1230 if (doc
->use_ESPshowpage
)
1232 WriteLabels(Orientation
);
1233 puts("ESPshowpage");
1239 * Restore the old showpage operator as needed...
1242 if (doc
->use_ESPshowpage
)
1243 puts("userdict/showpage/ESPshowpage load put\n");
1248 * 'copy_page()' - Copy a page description.
1250 * This function expects "line" to be filled with a %%Page comment line.
1251 * On return, "line" will contain the next line in the file, if any.
1254 static ssize_t
/* O - Length of next line */
1255 copy_page(cups_file_t
*fp
, /* I - File to read from */
1256 pstops_doc_t
*doc
, /* I - Document info */
1257 ppd_file_t
*ppd
, /* I - PPD file */
1258 int number
, /* I - Current page number */
1259 char *line
, /* I - Line buffer */
1260 ssize_t linelen
, /* I - Length of initial line */
1261 size_t linesize
) /* I - Size of line buffer */
1263 char label
[256], /* Page label string */
1264 *ptr
; /* Pointer into line */
1265 int level
; /* Embedded document level */
1266 pstops_page_t
*pageinfo
; /* Page information */
1267 int first_page
; /* First page on N-up output? */
1268 int has_page_setup
; /* Does the page have %%Begin/EndPageSetup? */
1269 int bounding_box
[4]; /* PageBoundingBox */
1273 * Get the page label for this page...
1276 first_page
= is_first_page(number
);
1278 if (!parse_text(line
+ 7, &ptr
, label
, sizeof(label
)))
1280 fputs(_("ERROR: Bad %%Page: comment in file!\n"), stderr
);
1284 else if (strtol(ptr
, &ptr
, 10) == LONG_MAX
|| !isspace(*ptr
& 255))
1286 fputs(_("ERROR: Bad %%Page: comment in file!\n"), stderr
);
1291 * Create or update the current output page...
1295 pageinfo
= add_page(doc
, label
);
1297 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
1300 * Handle first page override...
1303 if (doc
->ap_input_slot
|| doc
->ap_manual_feed
)
1308 * First page/sheet gets AP_FIRSTPAGE_* options...
1311 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->ap_input_slot
,
1312 pageinfo
->num_options
,
1313 &(pageinfo
->options
));
1314 pageinfo
->num_options
= cupsAddOption("ManualFeed",
1315 doc
->ap_input_slot
? "False" :
1316 doc
->ap_manual_feed
,
1317 pageinfo
->num_options
,
1318 &(pageinfo
->options
));
1320 else if (doc
->page
== (Duplex
+ 2))
1323 * Second page/sheet gets default options...
1326 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->input_slot
,
1327 pageinfo
->num_options
,
1328 &(pageinfo
->options
));
1329 pageinfo
->num_options
= cupsAddOption("ManualFeed",
1330 doc
->input_slot
? "False" :
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
= 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(_("ERROR: Bad %%PageBoundingBox: comment in file!\n"), stderr
);
1357 memcpy(bounding_box
, doc
->bounding_box
,
1358 sizeof(bounding_box
));
1360 else if (doc
->number_up
== 1 && !doc
->fitplot
&& 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] = PageLength
- temp_bbox
[3];
1378 bounding_box
[1] = temp_bbox
[0];
1379 bounding_box
[2] = PageLength
- temp_bbox
[1];
1380 bounding_box
[3] = temp_bbox
[2];
1383 case 2 : /* Reverse Portrait */
1384 bounding_box
[0] = PageWidth
- temp_bbox
[2];
1385 bounding_box
[1] = PageLength
- temp_bbox
[3];
1386 bounding_box
[2] = PageWidth
- temp_bbox
[0];
1387 bounding_box
[3] = PageLength
- temp_bbox
[1];
1390 case 3 : /* Reverse Landscape */
1391 bounding_box
[0] = temp_bbox
[1];
1392 bounding_box
[1] = PageWidth
- temp_bbox
[2];
1393 bounding_box
[2] = temp_bbox
[3];
1394 bounding_box
[3] = 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
->fitplot
)
1459 pageinfo
->num_options
= include_feature(ppd
, line
,
1460 pageinfo
->num_options
,
1461 &(pageinfo
->options
));
1463 else if (strncmp(line
, "%%Include", 9))
1467 if (doc
->number_up
== 1)
1470 * Update the document's composite and page bounding box...
1473 memcpy(pageinfo
->bounding_box
, bounding_box
,
1474 sizeof(pageinfo
->bounding_box
));
1476 if (bounding_box
[0] < doc
->new_bounding_box
[0])
1477 doc
->new_bounding_box
[0] = bounding_box
[0];
1478 if (bounding_box
[1] < doc
->new_bounding_box
[1])
1479 doc
->new_bounding_box
[1] = bounding_box
[1];
1480 if (bounding_box
[2] > doc
->new_bounding_box
[2])
1481 doc
->new_bounding_box
[2] = bounding_box
[2];
1482 if (bounding_box
[3] > doc
->new_bounding_box
[3])
1483 doc
->new_bounding_box
[3] = bounding_box
[3];
1487 * Output the page header as needed...
1490 if (!doc
->slow_order
&& first_page
)
1492 if (!ppd
|| !ppd
->num_filters
)
1493 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
1494 doc
->slow_collate
? 1 : doc
->copies
);
1496 if (doc
->number_up
> 1)
1498 printf("%%%%Page: (%d) %d\n", doc
->page
, doc
->page
);
1499 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1500 PageLeft
, PageBottom
, PageRight
, PageTop
);
1504 printf("%%%%Page: %s %d\n", pageinfo
->label
, doc
->page
);
1505 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1506 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
1507 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
1512 * Copy any page setup commands...
1516 doc_puts(doc
, "%%BeginPageSetup\n");
1518 if ((has_page_setup
= !strncmp(line
, "%%BeginPageSetup", 16)) != 0)
1520 int feature
= 0; /* In a Begin/EndFeature block? */
1522 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1524 if (!strncmp(line
, "%%EndPageSetup", 14))
1526 else if (!strncmp(line
, "%%BeginFeature:", 15))
1530 if (doc
->number_up
> 1 || doc
->fitplot
)
1533 else if (!strncmp(line
, "%%EndFeature", 12))
1537 if (doc
->number_up
> 1 || doc
->fitplot
)
1540 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1542 pageinfo
->num_options
= include_feature(ppd
, line
,
1543 pageinfo
->num_options
,
1544 &(pageinfo
->options
));
1547 else if (!strncmp(line
, "%%Include", 9))
1550 if (line
[0] != '%' && !feature
)
1553 if (!feature
|| (doc
->number_up
== 1 && !doc
->fitplot
))
1554 doc_write(doc
, line
, linelen
);
1558 * Skip %%EndPageSetup...
1561 if (linelen
> 0 && !strncmp(line
, "%%EndPageSetup", 14))
1563 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1570 char *page_setup
; /* PageSetup commands to send */
1573 if (pageinfo
->num_options
> 0)
1574 write_options(doc
, ppd
, pageinfo
->num_options
, pageinfo
->options
);
1577 * Output commands for the current page...
1580 page_setup
= ppdEmitString(ppd
, PPD_ORDER_PAGE
, 0);
1584 doc_puts(doc
, page_setup
);
1590 * Prep for the start of the page description...
1593 start_nup(doc
, number
, 1, bounding_box
);
1596 * Finish the PageSetup section as needed...
1601 int feature
= 0; /* In a Begin/EndFeature block? */
1603 doc_write(doc
, line
, linelen
);
1605 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1607 if (!strncmp(line
, "%%EndPageSetup", 14))
1609 else if (!strncmp(line
, "%%BeginFeature:", 15))
1613 if (doc
->number_up
> 1 || doc
->fitplot
)
1616 else if (!strncmp(line
, "%%EndFeature", 12))
1620 if (doc
->number_up
> 1 || doc
->fitplot
)
1623 else if (!strncmp(line
, "%%Include", 9))
1626 if (!feature
|| (doc
->number_up
== 1 && !doc
->fitplot
))
1627 doc_write(doc
, line
, linelen
);
1631 * Skip %%EndPageSetup...
1634 if (linelen
> 0 && !strncmp(line
, "%%EndPageSetup", 14))
1635 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1639 doc_puts(doc
, "%%EndPageSetup\n");
1642 * Read the rest of the page description...
1650 (!strncmp(line
, "%%Page:", 7) ||
1651 !strncmp(line
, "%%Trailer", 9) ||
1652 !strncmp(line
, "%%EOF", 5)))
1654 else if (!strncmp(line
, "%%BeginDocument", 15) ||
1655 !strncmp(line
, "%ADO_BeginApplication", 21))
1657 doc_write(doc
, line
, linelen
);
1661 else if ((!strncmp(line
, "%%EndDocument", 13) ||
1662 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
1664 doc_write(doc
, line
, linelen
);
1668 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
1669 (!strncmp(line
, "%%BeginData:", 12) &&
1670 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
1673 * Copy binary data...
1676 int bytes
; /* Bytes of data */
1679 doc_write(doc
, line
, linelen
);
1681 bytes
= atoi(strchr(line
, ':') + 1);
1685 if (bytes
> linesize
)
1686 linelen
= cupsFileRead(fp
, line
, linesize
);
1688 linelen
= cupsFileRead(fp
, line
, bytes
);
1693 perror("ERROR: Early end-of-file while reading binary data");
1697 doc_write(doc
, line
, linelen
);
1703 doc_write(doc
, line
, linelen
);
1705 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0);
1708 * Finish up this page and return...
1711 end_nup(doc
, number
);
1713 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
1720 * 'copy_prolog()' - Copy the document prolog section.
1722 * This function expects "line" to be filled with a %%BeginProlog comment line.
1723 * On return, "line" will contain the next line in the file, if any.
1726 static ssize_t
/* O - Length of next line */
1727 copy_prolog(cups_file_t
*fp
, /* I - File to read from */
1728 pstops_doc_t
*doc
, /* I - Document info */
1729 ppd_file_t
*ppd
, /* I - PPD file */
1730 char *line
, /* I - Line buffer */
1731 ssize_t linelen
, /* I - Length of initial line */
1732 size_t linesize
) /* I - Size of line buffer */
1734 while (strncmp(line
, "%%BeginProlog", 13))
1736 if (!strncmp(line
, "%%BeginSetup", 12) || !strncmp(line
, "%%Page:", 7))
1739 doc_write(doc
, line
, linelen
);
1741 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1745 doc_puts(doc
, "%%BeginProlog\n");
1747 do_prolog(doc
, ppd
);
1749 if (!strncmp(line
, "%%BeginProlog", 13))
1751 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1753 if (!strncmp(line
, "%%EndProlog", 11) ||
1754 !strncmp(line
, "%%BeginSetup", 12) ||
1755 !strncmp(line
, "%%Page:", 7))
1758 doc_write(doc
, line
, linelen
);
1761 if (!strncmp(line
, "%%EndProlog", 11))
1762 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1764 fputs(_("ERROR: Missing %%EndProlog!\n"), stderr
);
1767 doc_puts(doc
, "%%EndProlog\n");
1774 * 'copy_setup()' - Copy the document setup section.
1776 * This function expects "line" to be filled with a %%BeginSetup comment line.
1777 * On return, "line" will contain the next line in the file, if any.
1780 static ssize_t
/* O - Length of next line */
1781 copy_setup(cups_file_t
*fp
, /* I - File to read from */
1782 pstops_doc_t
*doc
, /* I - Document info */
1783 ppd_file_t
*ppd
, /* I - PPD file */
1784 char *line
, /* I - Line buffer */
1785 ssize_t linelen
, /* I - Length of initial line */
1786 size_t linesize
) /* I - Size of line buffer */
1788 int num_options
; /* Number of options */
1789 cups_option_t
*options
; /* Options */
1792 while (strncmp(line
, "%%BeginSetup", 12))
1794 if (!strncmp(line
, "%%Page:", 7))
1797 doc_write(doc
, line
, linelen
);
1799 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1803 doc_puts(doc
, "%%BeginSetup\n");
1810 if (!strncmp(line
, "%%BeginSetup", 12))
1812 while (strncmp(line
, "%%EndSetup", 10))
1814 if (!strncmp(line
, "%%Page:", 7))
1816 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1819 * %%IncludeFeature: *MainKeyword OptionKeyword
1822 if (doc
->number_up
== 1 && !doc
->fitplot
)
1823 num_options
= include_feature(ppd
, line
, num_options
, &options
);
1825 else if (strncmp(line
, "%%BeginSetup", 12))
1826 doc_write(doc
, line
, linelen
);
1828 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1832 if (!strncmp(line
, "%%EndSetup", 10))
1833 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1835 fputs(_("ERROR: Missing %%EndSetup!\n"), stderr
);
1838 if (num_options
> 0)
1840 write_options(doc
, ppd
, num_options
, options
);
1841 cupsFreeOptions(num_options
, options
);
1844 doc_puts(doc
, "%%EndSetup\n");
1851 * 'copy_trailer()' - Copy the document trailer.
1853 * This function expects "line" to be filled with a %%Trailer comment line.
1854 * On return, "line" will contain the next line in the file, if any.
1857 static ssize_t
/* O - Length of next line */
1858 copy_trailer(cups_file_t
*fp
, /* I - File to read from */
1859 pstops_doc_t
*doc
, /* I - Document info */
1860 ppd_file_t
*ppd
, /* I - PPD file */
1861 int number
, /* I - Number of pages */
1862 char *line
, /* I - Line buffer */
1863 ssize_t linelen
, /* I - Length of initial line */
1864 size_t linesize
) /* I - Size of line buffer */
1867 * Write the trailer comments...
1874 if (!strncmp(line
, "%%EOF", 5))
1876 else if (strncmp(line
, "%%Trailer", 9) &&
1877 strncmp(line
, "%%Pages:", 8) &&
1878 strncmp(line
, "%%BoundingBox:", 14))
1879 fwrite(line
, 1, linelen
, stdout
);
1881 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1884 fprintf(stderr
, "DEBUG: Wrote %d pages...\n", number
);
1886 printf("%%%%Pages: %d\n", number
);
1887 if (doc
->number_up
> 1 || doc
->fitplot
)
1888 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
1889 PageLeft
, PageBottom
, PageRight
, PageTop
);
1891 printf("%%%%BoundingBox: %d %d %d %d\n",
1892 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
1893 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
1900 * 'do_prolog()' - Send the necessary document prolog commands.
1904 do_prolog(pstops_doc_t
*doc
, /* I - Document information */
1905 ppd_file_t
*ppd
) /* I - PPD file */
1907 char *ps
; /* PS commands */
1911 * Send the document prolog commands...
1914 if (ppd
&& ppd
->patches
)
1916 doc_puts(doc
, "%%BeginFeature: *JobPatchFile 1\n");
1917 doc_puts(doc
, ppd
->patches
);
1918 doc_puts(doc
, "\n%%EndFeature\n");
1921 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_PROLOG
, 0.0)) != NULL
)
1928 * Define ESPshowpage here so that applications that define their
1929 * own procedure to do a showpage pick it up...
1932 if (doc
->use_ESPshowpage
)
1933 doc_puts(doc
, "userdict/ESPshowpage/showpage load put\n"
1934 "userdict/showpage{}put\n");
1939 * 'do_setup()' - Send the necessary document setup commands.
1943 do_setup(pstops_doc_t
*doc
, /* I - Document information */
1944 ppd_file_t
*ppd
) /* I - PPD file */
1946 char *ps
; /* PS commands */
1950 * Disable CTRL-D so that embedded files don't cause printing
1954 doc_puts(doc
, "% Disable CTRL-D as an end-of-file marker...\n");
1955 doc_puts(doc
, "userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put\n");
1958 * Mark job options...
1961 cupsMarkOptions(ppd
, doc
->num_options
, doc
->options
);
1964 * Send all the printer-specific setup commands...
1967 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, 0.0)) != NULL
)
1973 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_ANY
, 0.0)) != NULL
)
1980 * Set the number of copies for the job...
1983 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1985 doc_printf(doc
, "%%RBIBeginNonPPDFeature: *NumCopies %d\n", doc
->copies
);
1987 "%d/languagelevel where{pop languagelevel 2 ge}{false}ifelse\n"
1988 "{1 dict begin/NumCopies exch def currentdict end "
1990 "{userdict/#copies 3 -1 roll put}ifelse\n", doc
->copies
);
1991 doc_puts(doc
, "%RBIEndNonPPDFeature\n");
1995 * If we are doing N-up printing, disable setpagedevice...
1998 if (doc
->number_up
> 1)
1999 doc_puts(doc
, "userdict/setpagedevice{pop}bind put\n");
2002 * Changes to the transfer function must be made AFTER any
2003 * setpagedevice code...
2006 if (doc
->gamma
!= 1.0f
|| doc
->brightness
!= 1.0f
)
2007 doc_printf(doc
, "{ neg 1 add dup 0 lt { pop 1 } { %.3f exp neg 1 add } "
2008 "ifelse %.3f mul } bind settransfer\n",
2009 doc
->gamma
, doc
->brightness
);
2012 * Make sure we have rectclip and rectstroke procedures of some sort...
2016 "% x y w h ESPrc - Clip to a rectangle.\n"
2017 "userdict/ESPrc/rectclip where{pop/rectclip load}\n"
2018 "{{newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
2019 "neg 0 rlineto closepath clip newpath}bind}ifelse put\n");
2022 "% x y w h ESPrf - Fill a rectangle.\n"
2023 "userdict/ESPrf/rectfill where{pop/rectfill load}\n"
2024 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
2025 "neg 0 rlineto closepath fill grestore}bind}ifelse put\n");
2028 "% x y w h ESPrs - Stroke a rectangle.\n"
2029 "userdict/ESPrs/rectstroke where{pop/rectstroke load}\n"
2030 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
2031 "neg 0 rlineto closepath stroke grestore}bind}ifelse put\n");
2034 * Write the page and label prologs...
2037 if (doc
->number_up
== 2 || doc
->number_up
== 6)
2040 * For 2- and 6-up output, rotate the labels to match the orientation
2044 if (Orientation
& 1)
2045 write_label_prolog(doc
, doc
->page_label
, PageBottom
,
2046 PageWidth
- PageLength
+ PageTop
, PageLength
);
2048 write_label_prolog(doc
, doc
->page_label
, PageLeft
, PageRight
,
2052 write_label_prolog(doc
, doc
->page_label
, PageBottom
, PageTop
, PageWidth
);
2057 * 'doc_printf()' - Send a formatted string to stdout and/or the temp file.
2059 * This function should be used for all page-level output that is affected
2060 * by ordering, collation, etc.
2064 doc_printf(pstops_doc_t
*doc
, /* I - Document information */
2065 const char *format
, /* I - Printf-style format string */
2066 ...) /* I - Additional arguments as needed */
2068 va_list ap
; /* Pointer to arguments */
2069 char buffer
[1024]; /* Output buffer */
2070 size_t bytes
; /* Number of bytes to write */
2073 va_start(ap
, format
);
2074 bytes
= vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
2077 if (bytes
> sizeof(buffer
))
2080 _("ERROR: doc_printf overflow (%d bytes) detected, aborting!\n"),
2085 doc_write(doc
, buffer
, bytes
);
2090 * 'doc_puts()' - Send a nul-terminated string to stdout and/or the temp file.
2092 * This function should be used for all page-level output that is affected
2093 * by ordering, collation, etc.
2097 doc_puts(pstops_doc_t
*doc
, /* I - Document information */
2098 const char *s
) /* I - String to send */
2100 doc_write(doc
, s
, strlen(s
));
2105 * 'doc_write()' - Send data to stdout and/or the temp file.
2109 doc_write(pstops_doc_t
*doc
, /* I - Document information */
2110 const char *s
, /* I - Data to send */
2111 size_t len
) /* I - Number of bytes to send */
2113 if (!doc
->slow_order
)
2114 fwrite(s
, 1, len
, stdout
);
2117 cupsFileWrite(doc
->temp
, s
, len
);
2122 * 'end_nup()' - End processing for N-up printing.
2126 end_nup(pstops_doc_t
*doc
, /* I - Document information */
2127 int number
) /* I - Page number */
2129 if (doc
->number_up
> 1)
2130 doc_puts(doc
, "userdict/ESPsave get restore\n");
2132 switch (doc
->number_up
)
2135 if (doc
->use_ESPshowpage
)
2137 write_labels(doc
, Orientation
);
2138 doc_puts(doc
, "ESPshowpage\n");
2144 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2146 if (Orientation
& 1)
2149 * Rotate the labels back to portrait...
2152 write_labels(doc
, Orientation
- 1);
2154 else if (Orientation
== 0)
2157 * Rotate the labels to landscape...
2160 write_labels(doc
, doc
->normal_landscape
? 1 : 3);
2165 * Rotate the labels to landscape...
2168 write_labels(doc
, doc
->normal_landscape
? 3 : 1);
2171 doc_puts(doc
, "ESPshowpage\n");
2176 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2178 write_labels(doc
, Orientation
);
2179 doc_puts(doc
, "ESPshowpage\n");
2189 * 'include_feature()' - Include a printer option/feature command.
2192 static int /* O - New number of options */
2194 ppd_file_t
*ppd
, /* I - PPD file */
2195 const char *line
, /* I - DSC line */
2196 int num_options
, /* I - Number of options */
2197 cups_option_t
**options
) /* IO - Options */
2199 char name
[255], /* Option name */
2200 value
[255]; /* Option value */
2201 ppd_option_t
*option
; /* Option in file */
2205 * Get the "%%IncludeFeature: *Keyword OptionKeyword" values...
2208 if (sscanf(line
+ 17, "%254s%254s", name
, value
) != 2)
2210 fputs(_("ERROR: Bad %%IncludeFeature: comment!\n"), stderr
);
2211 return (num_options
);
2215 * Find the option and choice...
2218 if ((option
= ppdFindOption(ppd
, name
+ 1)) == NULL
)
2220 fprintf(stderr
, _("WARNING: Unknown option \"%s\"!\n"), name
+ 1);
2221 return (num_options
);
2224 if (option
->section
== PPD_ORDER_EXIT
||
2225 option
->section
== PPD_ORDER_JCL
)
2227 fprintf(stderr
, _("WARNING: Option \"%s\" cannot be included via "
2228 "IncludeFeature!\n"), name
+ 1);
2229 return (num_options
);
2232 if (!ppdFindChoice(option
, value
))
2234 fprintf(stderr
, _("WARNING: Unknown choice \"%s\" for option \"%s\"!\n"),
2236 return (num_options
);
2240 * Add the option to the option array and return...
2243 return (cupsAddOption(name
+ 1, value
, num_options
, options
));
2248 * 'parse_text()' - Parse a text value in a comment.
2250 * This function parses a DSC text value as defined on page 36 of the
2251 * DSC specification. Text values are either surrounded by parenthesis
2252 * or whitespace-delimited.
2254 * The value returned is the literal characters for the entire text
2255 * string, including any parenthesis and escape characters.
2258 static char * /* O - Value or NULL on error */
2259 parse_text(const char *start
, /* I - Start of text value */
2260 char **end
, /* O - End of text value */
2261 char *buffer
, /* I - Buffer */
2262 size_t bufsize
) /* I - Size of buffer */
2264 char *bufptr
, /* Pointer in buffer */
2265 *bufend
; /* End of buffer */
2266 int level
; /* Parenthesis level */
2270 * Skip leading whitespace...
2273 while (isspace(*start
& 255))
2277 * Then copy the value...
2282 bufend
= buffer
+ bufsize
- 1;
2284 while (bufptr
< bufend
)
2286 if (isspace(*start
& 255) && !level
)
2293 else if (*start
== ')')
2303 else if (*start
== '\\')
2306 * Copy escaped character...
2309 int i
; /* Looping var */
2313 i
<= 3 && isdigit(start
[i
] & 255) && bufptr
< bufend
;
2314 *bufptr
++ = start
[i
], i
++);
2323 * Return the value and new pointer into the line...
2327 *end
= (char *)start
;
2329 if (bufptr
== bufend
)
2337 * 'set_pstops_options()' - Set pstops options.
2342 pstops_doc_t
*doc
, /* I - Document information */
2343 ppd_file_t
*ppd
, /* I - PPD file */
2344 char *argv
[], /* I - Command-line arguments */
2345 int num_options
, /* I - Number of options */
2346 cups_option_t
*options
) /* I - Options */
2348 const char *val
; /* Option value */
2349 int intval
; /* Integer option value */
2350 ppd_attr_t
*attr
; /* PPD attribute */
2351 ppd_option_t
*option
; /* PPD option */
2352 ppd_choice_t
*choice
; /* PPD choice */
2356 * Initialize document information structure...
2359 memset(doc
, 0, sizeof(pstops_doc_t
));
2361 doc
->job_id
= atoi(argv
[1]);
2362 doc
->user
= argv
[2];
2363 doc
->title
= argv
[3];
2364 doc
->copies
= atoi(argv
[4]);
2366 if (ppd
&& ppd
->landscape
> 0)
2367 doc
->normal_landscape
= 1;
2369 doc
->bounding_box
[0] = (int)PageLeft
;
2370 doc
->bounding_box
[1] = (int)PageBottom
;
2371 doc
->bounding_box
[2] = (int)PageRight
;
2372 doc
->bounding_box
[3] = (int)PageTop
;
2374 doc
->new_bounding_box
[0] = INT_MAX
;
2375 doc
->new_bounding_box
[1] = INT_MAX
;
2376 doc
->new_bounding_box
[2] = INT_MIN
;
2377 doc
->new_bounding_box
[3] = INT_MIN
;
2380 * AP_FIRSTPAGE_InputSlot
2383 doc
->ap_input_slot
= cupsGetOption("AP_FIRSTPAGE_InputSlot", num_options
,
2387 * AP_FIRSTPAGE_ManualFeed
2390 doc
->ap_manual_feed
= cupsGetOption("AP_FIRSTPAGE_ManualFeed", num_options
,
2397 if ((val
= cupsGetOption("brightness", num_options
, options
)) != NULL
)
2400 * Get brightness value from 10 to 1000.
2405 if (intval
< 10 || intval
> 1000)
2407 fprintf(stderr
, _("ERROR: Unsupported brightness value %s, using "
2408 "brightness=100!\n"), val
);
2409 doc
->brightness
= 1.0f
;
2412 doc
->brightness
= intval
* 0.01f
;
2415 doc
->brightness
= 1.0f
;
2418 * collate, multiple-document-handling
2421 if ((val
= cupsGetOption("multiple-document-handling", num_options
, options
)) != NULL
)
2424 * This IPP attribute is unnecessarily complicated...
2426 * single-document, separate-documents-collated-copies, and
2427 * single-document-new-sheet all require collated copies.
2429 * separate-documents-uncollated-copies allows for uncollated copies.
2432 doc
->collate
= strcasecmp(val
, "separate-documents-uncollated-copies") != 0;
2435 if ((val
= cupsGetOption("Collate", num_options
, options
)) != NULL
&&
2436 (!strcasecmp(val
, "true") ||!strcasecmp(val
, "on") ||
2437 !strcasecmp(val
, "yes")))
2444 if ((val
= cupsGetOption("emit-jcl", num_options
, options
)) != NULL
&&
2445 (!strcasecmp(val
, "false") || !strcasecmp(val
, "off") ||
2446 !strcasecmp(val
, "no") || !strcmp(val
, "0")))
2452 * fitplot/fit-to-page
2455 if ((val
= cupsGetOption("fitplot", num_options
, options
)) != NULL
&&
2456 !strcasecmp(val
, "true"))
2458 else if ((val
= cupsGetOption("fit-to-page", num_options
, options
)) != NULL
&&
2459 !strcasecmp(val
, "true"))
2466 if ((val
= cupsGetOption("gamma", num_options
, options
)) != NULL
)
2469 * Get gamma value from 1 to 10000...
2474 if (intval
< 1 || intval
> 10000)
2476 fprintf(stderr
, _("ERROR: Unsupported gamma value %s, using "
2477 "gamma=1000!\n"), val
);
2481 doc
->gamma
= intval
* 0.001f
;
2490 if ((choice
= ppdFindMarkedChoice(ppd
, "InputSlot")) != NULL
)
2491 doc
->input_slot
= choice
->choice
;
2497 if ((choice
= ppdFindMarkedChoice(ppd
, "ManualFeed")) != NULL
)
2498 doc
->manual_feed
= choice
->choice
;
2500 if ((choice
= ppdFindMarkedChoice(ppd
, "MirrorPrint")) != NULL
)
2502 val
= choice
->choice
;
2506 val
= cupsGetOption("mirror", num_options
, options
);
2508 if (val
&& (!strcasecmp(val
, "true") || !strcasecmp(val
, "on") ||
2509 !strcasecmp(val
, "yes")))
2516 if ((val
= cupsGetOption("number-up", num_options
, options
)) != NULL
)
2518 switch (intval
= atoi(val
))
2526 doc
->number_up
= intval
;
2530 _("ERROR: Unsupported number-up value %d, using "
2531 "number-up=1!\n"), intval
);
2543 if ((val
= cupsGetOption("number-up-layout", num_options
, options
)) != NULL
)
2545 if (!strcasecmp(val
, "lrtb"))
2546 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2547 else if (!strcasecmp(val
, "lrbt"))
2548 doc
->number_up_layout
= PSTOPS_LAYOUT_LRBT
;
2549 else if (!strcasecmp(val
, "rltb"))
2550 doc
->number_up_layout
= PSTOPS_LAYOUT_RLTB
;
2551 else if (!strcasecmp(val
, "rlbt"))
2552 doc
->number_up_layout
= PSTOPS_LAYOUT_RLBT
;
2553 else if (!strcasecmp(val
, "tblr"))
2554 doc
->number_up_layout
= PSTOPS_LAYOUT_TBLR
;
2555 else if (!strcasecmp(val
, "tbrl"))
2556 doc
->number_up_layout
= PSTOPS_LAYOUT_TBRL
;
2557 else if (!strcasecmp(val
, "btlr"))
2558 doc
->number_up_layout
= PSTOPS_LAYOUT_BTLR
;
2559 else if (!strcasecmp(val
, "btrl"))
2560 doc
->number_up_layout
= PSTOPS_LAYOUT_BTRL
;
2563 fprintf(stderr
, _("ERROR: Unsupported number-up-layout value %s, using "
2564 "number-up-layout=lrtb!\n"), val
);
2565 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2569 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2575 if ((val
= cupsGetOption("OutputOrder", num_options
, options
)) != NULL
)
2577 if (!strcasecmp(val
, "Reverse"))
2578 doc
->output_order
= 1;
2583 * Figure out the right default output order from the PPD file...
2586 if ((choice
= ppdFindMarkedChoice(ppd
, "OutputBin")) != NULL
&&
2587 (attr
= ppdFindAttr(ppd
, "PageStackOrder", choice
->choice
)) != NULL
&&
2589 doc
->output_order
= !strcasecmp(attr
->value
, "Reverse");
2590 else if ((attr
= ppdFindAttr(ppd
, "DefaultOutputOrder", NULL
)) != NULL
&&
2592 doc
->output_order
= !strcasecmp(attr
->value
, "Reverse");
2599 if ((val
= cupsGetOption("page-border", num_options
, options
)) != NULL
)
2601 if (!strcasecmp(val
, "none"))
2602 doc
->page_border
= PSTOPS_BORDERNONE
;
2603 else if (!strcasecmp(val
, "single"))
2604 doc
->page_border
= PSTOPS_BORDERSINGLE
;
2605 else if (!strcasecmp(val
, "single-thick"))
2606 doc
->page_border
= PSTOPS_BORDERSINGLE2
;
2607 else if (!strcasecmp(val
, "double"))
2608 doc
->page_border
= PSTOPS_BORDERDOUBLE
;
2609 else if (!strcasecmp(val
, "double-thick"))
2610 doc
->page_border
= PSTOPS_BORDERDOUBLE2
;
2613 fprintf(stderr
, _("ERROR: Unsupported page-border value %s, using "
2614 "page-border=none!\n"), val
);
2615 doc
->page_border
= PSTOPS_BORDERNONE
;
2619 doc
->page_border
= PSTOPS_BORDERNONE
;
2625 doc
->page_label
= cupsGetOption("page-label", num_options
, options
);
2631 doc
->page_ranges
= cupsGetOption("page-ranges", num_options
, options
);
2637 doc
->page_set
= cupsGetOption("page-set", num_options
, options
);
2640 * Now figure out if we have to force collated copies, etc.
2643 if (ppd
&& ppd
->manual_copies
&& Duplex
&& doc
->copies
> 1)
2646 * Force collated copies when printing a duplexed document to
2647 * a non-PS printer that doesn't do hardware copy generation.
2648 * Otherwise the copies will end up on the front/back side of
2656 * See if we have to filter the fast or slow way...
2659 if (doc
->collate
&& doc
->copies
> 1)
2662 * See if we need to manually collate the pages...
2665 doc
->slow_collate
= 1;
2667 if ((choice
= ppdFindMarkedChoice(ppd
, "Collate")) != NULL
&&
2668 !strcasecmp(choice
->choice
, "True"))
2671 * Hardware collate option is selected, see if the option is
2672 * conflicting - if not, collate in hardware. Otherwise,
2673 * turn the hardware collate option off...
2676 if ((option
= ppdFindOption(ppd
, "Collate")) != NULL
&&
2677 !option
->conflicted
)
2678 doc
->slow_collate
= 0;
2680 ppdMarkOption(ppd
, "Collate", "False");
2684 doc
->slow_collate
= 0;
2686 if (!ppdFindOption(ppd
, "OutputOrder") && doc
->output_order
)
2687 doc
->slow_order
= 1;
2689 doc
->slow_order
= 0;
2692 (doc
->slow_collate
|| doc
->slow_order
||
2693 ((attr
= ppdFindAttr(ppd
, "cupsEvenDuplex", NULL
)) != NULL
&&
2694 attr
->value
&& !strcasecmp(attr
->value
, "true"))))
2695 doc
->slow_duplex
= 1;
2697 doc
->slow_duplex
= 0;
2700 * Create a temporary file for page data if we need to filter slowly...
2703 if (doc
->slow_order
|| doc
->slow_collate
)
2705 if ((doc
->temp
= cupsTempFile2(doc
->tempfile
,
2706 sizeof(doc
->tempfile
))) == NULL
)
2708 fprintf(stderr
, _("ERROR: Unable to create temporary file: %s\n"),
2715 * Figure out if we should use ESPshowpage or not...
2718 if (doc
->page_label
|| getenv("CLASSIFICATION") || doc
->number_up
> 1 ||
2722 * Yes, use ESPshowpage...
2725 doc
->use_ESPshowpage
= 1;
2728 fprintf(stderr
, "DEBUG: slow_collate=%d, slow_duplex=%d, slow_order=%d\n",
2729 doc
->slow_collate
, doc
->slow_duplex
, doc
->slow_order
);
2734 * 'skip_page()' - Skip past a page that won't be printed.
2737 static ssize_t
/* O - Length of next line */
2738 skip_page(cups_file_t
*fp
, /* I - File to read from */
2739 char *line
, /* I - Line buffer */
2740 ssize_t linelen
, /* I - Length of initial line */
2741 size_t linesize
) /* I - Size of line buffer */
2743 int level
; /* Embedded document level */
2748 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
2751 (!strncmp(line
, "%%Page:", 7) || !strncmp(line
, "%%Trailer", 9)))
2753 else if (!strncmp(line
, "%%BeginDocument", 15) ||
2754 !strncmp(line
, "%ADO_BeginApplication", 21))
2756 else if ((!strncmp(line
, "%%EndDocument", 13) ||
2757 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
2759 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
2760 (!strncmp(line
, "%%BeginData:", 12) &&
2761 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
2764 * Skip binary data...
2767 int bytes
; /* Bytes of data */
2770 bytes
= atoi(strchr(line
, ':') + 1);
2774 if (bytes
> linesize
)
2775 linelen
= cupsFileRead(fp
, line
, linesize
);
2777 linelen
= cupsFileRead(fp
, line
, bytes
);
2782 perror("ERROR: Early end-of-file while reading binary data");
2796 * 'start_nup()' - Start processing for N-up printing.
2800 start_nup(pstops_doc_t
*doc
, /* I - Document information */
2801 int number
, /* I - Page number */
2802 int show_border
, /* I - Show the border? */
2803 const int *bounding_box
) /* I - BoundingBox value */
2805 int pos
; /* Position on page */
2806 int x
, y
; /* Relative position of subpage */
2807 float w
, l
, /* Width and length of subpage */
2808 tx
, ty
; /* Translation values for subpage */
2809 float pagew
, /* Printable width of page */
2810 pagel
; /* Printable height of page */
2811 int bboxx
, /* BoundingBox X origin */
2812 bboxy
, /* BoundingBox Y origin */
2813 bboxw
, /* BoundingBox width */
2814 bboxl
; /* BoundingBox height */
2815 float margin
= 0; /* Current margin for border */
2818 if (doc
->number_up
> 1)
2819 doc_puts(doc
, "userdict/ESPsave save put\n");
2821 pos
= (number
- 1) % doc
->number_up
;
2822 pagew
= PageRight
- PageLeft
;
2823 pagel
= PageTop
- PageBottom
;
2827 bboxx
= bounding_box
[0];
2828 bboxy
= bounding_box
[1];
2829 bboxw
= bounding_box
[2] - bounding_box
[0];
2830 bboxl
= bounding_box
[3] - bounding_box
[1];
2840 fprintf(stderr
, "DEBUG: pagew = %.1f, pagel = %.1f\n", pagew
, pagel
);
2841 fprintf(stderr
, "DEBUG: bboxx = %d, bboxy = %d, bboxw = %d, bboxl = %d\n",
2842 bboxx
, bboxy
, bboxw
, bboxl
);
2843 fprintf(stderr
, "DEBUG: PageLeft = %.1f, PageRight = %.1f\n",
2844 PageLeft
, PageRight
);
2845 fprintf(stderr
, "DEBUG: PageTop = %.1f, PageBottom = %.1f\n",
2846 PageTop
, PageBottom
);
2847 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
2848 PageWidth
, PageLength
);
2850 switch (Orientation
)
2852 case 1 : /* Landscape */
2853 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", PageLength
);
2855 case 2 : /* Reverse Portrait */
2856 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", PageWidth
,
2859 case 3 : /* Reverse Landscape */
2860 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", PageWidth
);
2864 if (Duplex
&& doc
->number_up
> 1 && ((number
/ doc
->number_up
) & 1))
2865 doc_printf(doc
, "%.1f %.1f translate\n", PageWidth
- PageRight
, PageBottom
);
2866 else if (doc
->number_up
> 1 || doc
->fitplot
)
2867 doc_printf(doc
, "%.1f %.1f translate\n", PageLeft
, PageBottom
);
2870 doc_printf(doc
, "%.1f 0.0 translate -1 1 scale\n", PageWidth
);
2872 switch (doc
->number_up
)
2878 l
= w
* bboxl
/ bboxw
;
2883 w
= l
* bboxw
/ bboxl
;
2886 tx
= 0.5 * (pagew
- w
);
2887 ty
= 0.5 * (pagel
- l
);
2889 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n", tx
, ty
,
2890 w
/ bboxw
, l
/ bboxl
);
2897 if (Orientation
& 1)
2901 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2905 l
= w
* bboxl
/ bboxw
;
2907 if (l
> (pagew
* 0.5))
2910 w
= l
* bboxw
/ bboxl
;
2913 tx
= 0.5 * (pagew
* 0.5 - l
);
2914 ty
= 0.5 * (pagel
- w
);
2916 if (doc
->normal_landscape
)
2917 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2919 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2921 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2922 ty
, tx
+ pagew
* 0.5 * x
, w
/ bboxw
, l
/ bboxl
);
2928 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2932 w
= l
* bboxw
/ bboxl
;
2934 if (w
> (pagel
* 0.5))
2937 l
= w
* bboxl
/ bboxw
;
2940 tx
= 0.5 * (pagel
* 0.5 - w
);
2941 ty
= 0.5 * (pagew
- l
);
2943 if (doc
->normal_landscape
)
2944 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2946 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2948 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2949 tx
+ pagel
* 0.5 * x
, ty
, w
/ bboxw
, l
/ bboxl
);
2954 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2965 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2968 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2972 l
= w
* bboxl
/ bboxw
;
2974 if (l
> (pagel
* 0.5))
2977 w
= l
* bboxw
/ bboxl
;
2980 tx
= 0.5 * (pagew
* 0.5 - w
);
2981 ty
= 0.5 * (pagel
* 0.5 - l
);
2983 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2984 tx
+ x
* pagew
* 0.5, ty
+ y
* pagel
* 0.5,
2985 w
/ bboxw
, l
/ bboxl
);
2989 if (Orientation
& 1)
2991 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2996 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2999 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3007 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3010 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3015 l
= w
* bboxl
/ bboxw
;
3017 if (l
> (pagew
* 0.333))
3020 w
= l
* bboxw
/ bboxl
;
3023 tx
= 0.5 * (pagel
- 2 * w
);
3024 ty
= 0.5 * (pagew
- 3 * l
);
3026 if (doc
->normal_landscape
)
3027 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
3029 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
3031 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3032 tx
+ x
* w
, ty
+ y
* l
, l
/ bboxl
, w
/ bboxw
);
3036 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3041 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3044 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3052 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3055 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3060 w
= l
* bboxw
/ bboxl
;
3062 if (w
> (pagel
* 0.333))
3065 l
= w
* bboxl
/ bboxw
;
3068 tx
= 0.5 * (pagel
- 3 * w
);
3069 ty
= 0.5 * (pagew
- 2 * l
);
3071 if (doc
->normal_landscape
)
3072 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
3074 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
3076 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3077 tx
+ w
* x
, ty
+ l
* y
, w
/ bboxw
, l
/ bboxl
);
3083 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3094 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3097 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3101 l
= w
* bboxl
/ bboxw
;
3103 if (l
> (pagel
* 0.333))
3106 w
= l
* bboxw
/ bboxl
;
3109 tx
= 0.5 * (pagew
* 0.333 - w
);
3110 ty
= 0.5 * (pagel
* 0.333 - l
);
3112 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3113 tx
+ x
* pagew
* 0.333, ty
+ y
* pagel
* 0.333,
3114 w
/ bboxw
, l
/ bboxl
);
3118 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3129 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3132 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3136 l
= w
* bboxl
/ bboxw
;
3138 if (l
> (pagel
* 0.25))
3141 w
= l
* bboxw
/ bboxl
;
3144 tx
= 0.5 * (pagew
* 0.25 - w
);
3145 ty
= 0.5 * (pagel
* 0.25 - l
);
3147 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3148 tx
+ x
* pagew
* 0.25, ty
+ y
* pagel
* 0.25,
3149 w
/ bboxw
, l
/ bboxl
);
3154 * Draw borders as necessary...
3157 if (doc
->page_border
&& show_border
)
3159 int rects
; /* Number of border rectangles */
3160 float fscale
; /* Scaling value for points */
3163 rects
= (doc
->page_border
& PSTOPS_BORDERDOUBLE
) ? 2 : 1;
3164 fscale
= PageWidth
/ w
;
3165 margin
= 2.25 * fscale
;
3168 * Set the line width and color...
3171 doc_puts(doc
, "gsave\n");
3172 doc_printf(doc
, "%.3f setlinewidth 0 setgray newpath\n",
3173 (doc
->page_border
& PSTOPS_BORDERTHICK
) ? 0.5 * fscale
:
3177 * Draw border boxes...
3180 for (; rects
> 0; rects
--, margin
+= 2 * fscale
)
3181 if (doc
->number_up
> 1)
3182 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3186 bboxl
- 2 * margin
);
3188 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3190 PageBottom
+ margin
,
3191 PageRight
- PageLeft
- 2 * margin
,
3192 PageTop
- PageBottom
- 2 * margin
);
3195 * Restore pen settings...
3198 doc_puts(doc
, "grestore\n");
3204 * Offset the page by its bounding box...
3207 doc_printf(doc
, "%d %d translate\n", -bounding_box
[0],
3211 if (doc
->fitplot
|| doc
->number_up
> 1)
3214 * Clip the page to the page's bounding box...
3217 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrc\n",
3218 bboxx
+ margin
, bboxy
+ margin
,
3219 bboxw
- 2 * margin
, bboxl
- 2 * margin
);
3225 * 'write_label_prolog()' - Write the prolog with the classification
3230 write_label_prolog(pstops_doc_t
*doc
, /* I - Document info */
3231 const char *label
, /* I - Page label */
3232 float bottom
, /* I - Bottom position in points */
3233 float top
, /* I - Top position in points */
3234 float width
) /* I - Width in points */
3236 const char *classification
; /* CLASSIFICATION environment variable */
3237 const char *ptr
; /* Temporary string pointer */
3241 * First get the current classification...
3244 if ((classification
= getenv("CLASSIFICATION")) == NULL
)
3245 classification
= "";
3246 if (strcmp(classification
, "none") == 0)
3247 classification
= "";
3250 * If there is nothing to show, bind an empty 'write labels' procedure
3254 if (!classification
[0] && (label
== NULL
|| !label
[0]))
3256 doc_puts(doc
, "userdict/ESPwl{}bind put\n");
3261 * Set the classification + page label string...
3264 doc_puts(doc
, "userdict");
3265 if (!strcmp(classification
, "confidential"))
3266 doc_puts(doc
, "/ESPpl(CONFIDENTIAL");
3267 else if (!strcmp(classification
, "classified"))
3268 doc_puts(doc
, "/ESPpl(CLASSIFIED");
3269 else if (!strcmp(classification
, "secret"))
3270 doc_puts(doc
, "/ESPpl(SECRET");
3271 else if (!strcmp(classification
, "topsecret"))
3272 doc_puts(doc
, "/ESPpl(TOP SECRET");
3273 else if (!strcmp(classification
, "unclassified"))
3274 doc_puts(doc
, "/ESPpl(UNCLASSIFIED");
3277 doc_puts(doc
, "/ESPpl(");
3279 for (ptr
= classification
; *ptr
; ptr
++)
3281 if (*ptr
< 32 || *ptr
> 126)
3282 doc_printf(doc
, "\\%03o", *ptr
);
3283 else if (*ptr
== '_')
3285 else if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
3286 doc_printf(doc
, "\\%c", *ptr
);
3288 doc_printf(doc
, "%c", *ptr
);
3294 if (classification
[0])
3295 doc_puts(doc
, " - ");
3298 * Quote the label string as needed...
3301 for (ptr
= label
; *ptr
; ptr
++)
3303 if (*ptr
< 32 || *ptr
> 126)
3304 doc_printf(doc
, "\\%03o", *ptr
);
3305 else if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
3306 doc_printf(doc
, "\\%c", *ptr
);
3308 doc_printf(doc
, "%c", *ptr
);
3312 doc_puts(doc
, ")put\n");
3315 * Then get a 14 point Helvetica-Bold font...
3318 doc_puts(doc
, "userdict/ESPpf /Helvetica-Bold findfont 14 scalefont put\n");
3321 * Finally, the procedure to write the labels on the page...
3324 doc_puts(doc
, "userdict/ESPwl{\n");
3325 doc_puts(doc
, " ESPpf setfont\n");
3326 doc_printf(doc
, " ESPpl stringwidth pop dup 12 add exch -0.5 mul %.0f add\n",
3328 doc_puts(doc
, " 1 setgray\n");
3329 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrf\n", bottom
- 2.0);
3330 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrf\n", top
- 18.0);
3331 doc_puts(doc
, " 0 setgray\n");
3332 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrs\n", bottom
- 2.0);
3333 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrs\n", top
- 18.0);
3334 doc_printf(doc
, " dup %.0f moveto ESPpl show\n", bottom
+ 2.0);
3335 doc_printf(doc
, " %.0f moveto ESPpl show\n", top
- 14.0);
3336 doc_puts(doc
, "pop\n");
3337 doc_puts(doc
, "}bind put\n");
3342 * 'write_labels()' - Write the actual page labels.
3344 * This function is a copy of the one in common.c since we need to
3345 * use doc_puts/doc_printf instead of puts/printf...
3349 write_labels(pstops_doc_t
*doc
, /* I - Document information */
3350 int orient
) /* I - Orientation of the page */
3352 float width
, /* Width of page */
3353 length
; /* Length of page */
3356 doc_puts(doc
, "gsave\n");
3358 if ((orient
^ Orientation
) & 1)
3366 length
= PageLength
;
3371 case 1 : /* Landscape */
3372 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", length
);
3374 case 2 : /* Reverse Portrait */
3375 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", width
, length
);
3377 case 3 : /* Reverse Landscape */
3378 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", width
);
3382 doc_puts(doc
, "ESPwl\n");
3383 doc_puts(doc
, "grestore\n");
3388 * 'write_options()' - Write options provided via %%IncludeFeature.
3393 pstops_doc_t
*doc
, /* I - Document */
3394 ppd_file_t
*ppd
, /* I - PPD file */
3395 int num_options
, /* I - Number of options */
3396 cups_option_t
*options
) /* I - Options */
3398 int i
; /* Looping var */
3399 ppd_option_t
*option
; /* PPD option */
3400 int min_order
; /* Minimum OrderDependency value */
3401 char *doc_setup
, /* DocumentSetup commands to send */
3402 *any_setup
; /* AnySetup commands to send */
3406 * Figure out the minimum OrderDependency value...
3409 if ((option
= ppdFindOption(ppd
, "PageRegion")) != NULL
)
3410 min_order
= option
->order
;
3414 for (i
= 0; i
< num_options
; i
++)
3415 if ((option
= ppdFindOption(ppd
, options
[i
].name
)) != NULL
&&
3416 option
->order
< min_order
)
3417 min_order
= option
->order
;
3420 * Mark and extract them...
3423 cupsMarkOptions(ppd
, num_options
, options
);
3425 doc_setup
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, min_order
);
3426 any_setup
= ppdEmitString(ppd
, PPD_ORDER_ANY
, min_order
);
3429 * Then send them out...
3434 doc_puts(doc
, doc_setup
);
3440 doc_puts(doc
, any_setup
);
3447 * End of "$Id: pstops.c 7977 2008-09-23 23:44:33Z mike $".