]>
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-2009 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 _cupsLangPrintf(stderr
,
255 _("Usage: %s job-id user title copies options [file]\n"),
261 * Register a signal handler to cleanly cancel a job.
264 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
265 sigset(SIGTERM
, cancel_job
);
266 #elif defined(HAVE_SIGACTION)
267 memset(&action
, 0, sizeof(action
));
269 sigemptyset(&action
.sa_mask
);
270 action
.sa_handler
= cancel_job
;
271 sigaction(SIGTERM
, &action
, NULL
);
273 signal(SIGTERM
, cancel_job
);
274 #endif /* HAVE_SIGSET */
277 * If we have 7 arguments, print the file named on the command-line.
278 * Otherwise, send stdin instead...
282 fp
= cupsFileStdin();
286 * Try to open the print file...
289 if ((fp
= cupsFileOpen(argv
[6], "r")) == NULL
)
291 _cupsLangPrintf(stderr
, _("ERROR: Unable to open file \"%s\" - %s\n"),
292 argv
[6], strerror(errno
));
298 * Read the first line to see if we have DSC comments...
301 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
303 _cupsLangPuts(stderr
, _("ERROR: Empty print file!\n"));
308 * Process command-line options...
312 num_options
= cupsParseOptions(argv
[5], 0, &options
);
313 ppd
= SetCommonOptions(num_options
, options
, 1);
315 set_pstops_options(&doc
, ppd
, argv
, num_options
, options
);
318 * Write any "exit server" options that have been selected...
321 ppdEmit(ppd
, stdout
, PPD_ORDER_EXIT
);
324 * Write any JCL commands that are needed to print PostScript code...
328 ppdEmitJCL(ppd
, stdout
, doc
.job_id
, doc
.user
, doc
.title
);
331 * Start with a DSC header...
334 puts("%!PS-Adobe-3.0");
337 * Skip leading PJL in the document...
340 while (!strncmp(line
, "\033%-12345X", 9) || !strncmp(line
, "@PJL ", 5))
343 * Yup, we have leading PJL fun, so skip it until we hit the line
344 * with "ENTER LANGUAGE"...
347 fputs("DEBUG: Skipping PJL header...\n", stderr
);
349 while (strstr(line
, "ENTER LANGUAGE") == NULL
&& strncmp(line
, "%!", 2))
350 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
353 if (!strncmp(line
, "%!", 2))
356 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
361 * Now see if the document conforms to the Adobe Document Structuring
365 if (!strncmp(line
, "%!PS-Adobe-", 11))
368 * Yes, filter the document...
371 copy_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
376 * No, display an error message and treat the file as if it contains
380 copy_non_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
384 * Send %%EOF as needed...
391 * End the job with the appropriate JCL command or CTRL-D...
396 if (ppd
&& ppd
->jcl_end
)
397 ppdEmitJCLEnd(ppd
, stdout
);
403 * Close files and remove the temporary file if needed...
408 cupsFileClose(doc
.temp
);
409 unlink(doc
.tempfile
);
413 cupsFreeOptions(num_options
, options
);
422 * 'add_page()' - Add a page to the pages array.
425 static pstops_page_t
* /* O - New page info object */
426 add_page(pstops_doc_t
*doc
, /* I - Document information */
427 const char *label
) /* I - Page label */
429 pstops_page_t
*pageinfo
; /* New page info object */
433 doc
->pages
= cupsArrayNew(NULL
, NULL
);
437 _cupsLangPrintf(stderr
,
438 _("EMERG: Unable to allocate memory for pages array: %s\n"),
443 if ((pageinfo
= calloc(1, sizeof(pstops_page_t
))) == NULL
)
445 _cupsLangPrintf(stderr
,
446 _("EMERG: Unable to allocate memory for page info: %s\n"),
451 pageinfo
->label
= strdup(label
);
452 pageinfo
->offset
= cupsFileTell(doc
->temp
);
454 cupsArrayAdd(doc
->pages
, pageinfo
);
463 * 'cancel_job()' - Flag the job as canceled.
467 cancel_job(int sig
) /* I - Signal number (unused) */
476 * 'check_range()' - Check to see if the current page is selected for
480 static int /* O - 1 if selected, 0 otherwise */
481 check_range(pstops_doc_t
*doc
, /* I - Document information */
482 int page
) /* I - Page number */
484 const char *range
; /* Pointer into range string */
485 int lower
, upper
; /* Lower and upper page numbers */
491 * See if we only print even or odd pages...
494 if (!strcasecmp(doc
->page_set
, "even") && (page
& 1))
497 if (!strcasecmp(doc
->page_set
, "odd") && !(page
& 1))
501 if (!doc
->page_ranges
)
502 return (1); /* No range, print all pages... */
504 for (range
= doc
->page_ranges
; *range
!= '\0';)
510 upper
= strtol(range
, (char **)&range
, 10);
514 lower
= strtol(range
, (char **)&range
, 10);
519 if (!isdigit(*range
& 255))
522 upper
= strtol(range
, (char **)&range
, 10);
528 if (page
>= lower
&& page
<= upper
)
542 * 'copy_bytes()' - Copy bytes from the input file to stdout.
546 copy_bytes(cups_file_t
*fp
, /* I - File to read from */
547 off_t offset
, /* I - Offset to page data */
548 size_t length
) /* I - Length of page data */
550 char buffer
[8192]; /* Data buffer */
551 ssize_t nbytes
; /* Number of bytes read */
552 size_t nleft
; /* Number of bytes left/remaining */
557 if (cupsFileSeek(fp
, offset
) < 0)
559 _cupsLangPrintf(stderr
,
560 #ifdef HAVE_LONG_LONG
561 _("ERROR: Unable to seek to offset %lld in file - %s\n"),
563 _("ERROR: Unable to seek to offset %ld in file - %s\n"),
564 #endif /* HAVE_LONG_LONG */
565 CUPS_LLCAST offset
, strerror(errno
));
569 while (nleft
> 0 || length
== 0)
571 if (nleft
> sizeof(buffer
) || length
== 0)
572 nbytes
= sizeof(buffer
);
576 if ((nbytes
= cupsFileRead(fp
, buffer
, nbytes
)) < 1)
581 fwrite(buffer
, 1, nbytes
, stdout
);
587 * 'copy_comments()' - Copy all of the comments section.
589 * This function expects "line" to be filled with a comment line.
590 * On return, "line" will contain the next line in the file, if any.
593 static ssize_t
/* O - Length of next line */
594 copy_comments(cups_file_t
*fp
, /* I - File to read from */
595 pstops_doc_t
*doc
, /* I - Document info */
596 ppd_file_t
*ppd
, /* I - PPD file */
597 char *line
, /* I - Line buffer */
598 ssize_t linelen
, /* I - Length of initial line */
599 size_t linesize
) /* I - Size of line buffer */
601 int saw_bounding_box
, /* Saw %%BoundingBox: comment? */
602 saw_for
, /* Saw %%For: comment? */
603 saw_pages
, /* Saw %%Pages: comment? */
604 saw_title
; /* Saw %%Title: comment? */
608 * Loop until we see %%EndComments or a non-comment line...
611 saw_bounding_box
= 0;
616 while (line
[0] == '%')
619 * Strip trailing whitespace...
626 if (!isspace(line
[linelen
] & 255))
629 line
[linelen
] = '\0';
636 fprintf(stderr
, "DEBUG: %s\n", line
);
639 * Pull the headers out...
642 if (!strncmp(line
, "%%Pages:", 8))
644 int pages
; /* Number of pages */
648 _cupsLangPuts(stderr
, _("ERROR: Duplicate %%Pages: comment seen!\n"));
652 if (Duplex
&& (pages
= atoi(line
+ 8)) > 0 && pages
<= doc
->number_up
)
655 * Since we will only be printing on a single page, disable duplexing.
659 doc
->slow_duplex
= 0;
661 if (cupsGetOption("sides", doc
->num_options
, doc
->options
))
662 doc
->num_options
= cupsAddOption("sides", "one-sided",
663 doc
->num_options
, &(doc
->options
));
665 if (cupsGetOption("Duplex", doc
->num_options
, doc
->options
))
666 doc
->num_options
= cupsAddOption("Duplex", "None",
667 doc
->num_options
, &(doc
->options
));
669 if (cupsGetOption("EFDuplex", doc
->num_options
, doc
->options
))
670 doc
->num_options
= cupsAddOption("EFDuplex", "None",
671 doc
->num_options
, &(doc
->options
));
673 if (cupsGetOption("EFDuplexing", doc
->num_options
, doc
->options
))
674 doc
->num_options
= cupsAddOption("EFDuplexing", "False",
675 doc
->num_options
, &(doc
->options
));
677 if (cupsGetOption("KD03Duplex", doc
->num_options
, doc
->options
))
678 doc
->num_options
= cupsAddOption("KD03Duplex", "None",
679 doc
->num_options
, &(doc
->options
));
681 if (cupsGetOption("JCLDuplex", doc
->num_options
, doc
->options
))
682 doc
->num_options
= cupsAddOption("JCLDuplex", "None",
683 doc
->num_options
, &(doc
->options
));
685 ppdMarkOption(ppd
, "Duplex", "None");
686 ppdMarkOption(ppd
, "EFDuplex", "None");
687 ppdMarkOption(ppd
, "EFDuplexing", "False");
688 ppdMarkOption(ppd
, "KD03Duplex", "None");
689 ppdMarkOption(ppd
, "JCLDuplex", "None");
692 else if (!strncmp(line
, "%%BoundingBox:", 14))
694 if (saw_bounding_box
)
695 _cupsLangPuts(stderr
,
696 _("ERROR: Duplicate %%BoundingBox: comment seen!\n"));
697 else if (strstr(line
+ 14, "(atend)"))
700 * Do nothing for now but use the default imageable area...
703 else if (sscanf(line
+ 14, "%d%d%d%d", doc
->bounding_box
+ 0,
704 doc
->bounding_box
+ 1, doc
->bounding_box
+ 2,
705 doc
->bounding_box
+ 3) != 4)
707 _cupsLangPuts(stderr
, _("ERROR: Bad %%BoundingBox: comment seen!\n"));
709 doc
->bounding_box
[0] = (int)PageLeft
;
710 doc
->bounding_box
[1] = (int)PageBottom
;
711 doc
->bounding_box
[2] = (int)PageRight
;
712 doc
->bounding_box
[3] = (int)PageTop
;
715 saw_bounding_box
= 1;
717 else if (!strncmp(line
, "%%For:", 6))
720 doc_printf(doc
, "%s\n", line
);
722 else if (!strncmp(line
, "%%Title:", 8))
725 doc_printf(doc
, "%s\n", line
);
727 else if (!strncmp(line
, "%cupsRotation:", 14))
730 * Reset orientation of document?
733 int orient
= (atoi(line
+ 14) / 90) & 3;
735 if (orient
!= Orientation
)
738 * Yes, update things so that the pages come out right...
741 Orientation
= (4 - Orientation
+ orient
) & 3;
743 Orientation
= orient
;
746 else if (!strcmp(line
, "%%EndComments"))
748 linelen
= cupsFileGetLine(fp
, line
, linesize
);
751 else if (strncmp(line
, "%!", 2) && strncmp(line
, "%cups", 5))
752 doc_printf(doc
, "%s\n", line
);
754 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
758 if (!saw_bounding_box
)
759 _cupsLangPuts(stderr
, _("ERROR: No %%BoundingBox: comment in header!\n"));
762 _cupsLangPuts(stderr
, _("ERROR: No %%Pages: comment in header!\n"));
765 WriteTextComment("For", doc
->user
);
768 WriteTextComment("Title", doc
->title
);
770 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
773 * Tell the document processor the copy and duplex options
774 * that are required...
777 doc_printf(doc
, "%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
778 doc
->collate
? " collate" : "",
779 Duplex
? " duplex" : "");
782 * Apple uses RBI comments for various non-PPD options...
785 doc_printf(doc
, "%%RBINumCopies: %d\n", doc
->copies
);
790 * Tell the document processor the duplex option that is required...
794 doc_puts(doc
, "%%Requirements: duplex\n");
797 * Apple uses RBI comments for various non-PPD options...
800 doc_puts(doc
, "%RBINumCopies: 1\n");
803 doc_puts(doc
, "%%Pages: (atend)\n");
804 doc_puts(doc
, "%%BoundingBox: (atend)\n");
805 doc_puts(doc
, "%%EndComments\n");
812 * 'copy_dsc()' - Copy a DSC-conforming document.
814 * This function expects "line" to be filled with the %!PS-Adobe comment line.
818 copy_dsc(cups_file_t
*fp
, /* I - File to read from */
819 pstops_doc_t
*doc
, /* I - Document info */
820 ppd_file_t
*ppd
, /* I - PPD file */
821 char *line
, /* I - Line buffer */
822 ssize_t linelen
, /* I - Length of initial line */
823 size_t linesize
) /* I - Size of line buffer */
825 int number
; /* Page number */
826 pstops_page_t
*pageinfo
; /* Page information */
830 * Make sure we use ESPshowpage for EPS files...
833 if (strstr(line
, "EPSF"))
835 doc
->use_ESPshowpage
= 1;
840 * Start sending the document with any commands needed...
843 fprintf(stderr
, "DEBUG: Before copy_comments - %s", line
);
844 linelen
= copy_comments(fp
, doc
, ppd
, line
, linelen
, linesize
);
847 * Now find the prolog section, if any...
850 fprintf(stderr
, "DEBUG: Before copy_prolog - %s", line
);
851 linelen
= copy_prolog(fp
, doc
, ppd
, line
, linelen
, linesize
);
854 * Then the document setup section...
857 fprintf(stderr
, "DEBUG: Before copy_setup - %s", line
);
858 linelen
= copy_setup(fp
, doc
, ppd
, line
, linelen
, linesize
);
861 * Copy until we see %%Page:...
864 while (strncmp(line
, "%%Page:", 7) && strncmp(line
, "%%Trailer", 9))
866 doc_write(doc
, line
, linelen
);
868 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
873 * Then process pages until we have no more...
878 fprintf(stderr
, "DEBUG: Before page loop - %s", line
);
879 while (!strncmp(line
, "%%Page:", 7))
886 if (check_range(doc
, (number
- 1) / doc
->number_up
+ 1))
888 fprintf(stderr
, "DEBUG: Copying page %d...\n", number
);
889 linelen
= copy_page(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
893 fprintf(stderr
, "DEBUG: Skipping page %d...\n", number
);
894 linelen
= skip_page(fp
, line
, linelen
, linesize
);
899 * Finish up the last page(s)...
902 if (number
&& is_not_last_page(number
) && cupsArrayLast(doc
->pages
) &&
903 check_range(doc
, (number
- 1) / doc
->number_up
+ 1))
905 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
907 start_nup(doc
, doc
->number_up
, 0, doc
->bounding_box
);
908 doc_puts(doc
, "showpage\n");
909 end_nup(doc
, doc
->number_up
);
911 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
914 if (doc
->slow_duplex
&& (doc
->page
& 1))
917 * Make sure we have an even number of pages...
920 pageinfo
= add_page(doc
, "(filler)");
922 if (!doc
->slow_order
)
924 if (!ppd
|| !ppd
->num_filters
)
925 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
926 doc
->slow_collate
? 1 : doc
->copies
);
928 printf("%%%%Page: (filler) %d\n", doc
->page
);
931 start_nup(doc
, doc
->number_up
, 0, doc
->bounding_box
);
932 doc_puts(doc
, "showpage\n");
933 end_nup(doc
, doc
->number_up
);
935 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
939 * Make additional copies as necessary...
942 number
= doc
->slow_order
? 0 : doc
->page
;
944 if (doc
->temp
&& !JobCanceled
&& cupsArrayCount(doc
->pages
) > 0)
946 int copy
; /* Current copy */
950 * Reopen the temporary file for reading...
953 cupsFileClose(doc
->temp
);
955 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
961 if (doc
->slow_collate
)
962 copy
= !doc
->slow_order
;
964 copy
= doc
->copies
- 1;
966 for (; copy
< doc
->copies
; copy
++)
972 * Send end-of-job stuff followed by any start-of-job stuff required
973 * for the JCL options...
976 if (number
&& doc
->emit_jcl
&& ppd
&& ppd
->jcl_end
)
979 * Send the trailer...
983 printf("%%%%Pages: %d\n", cupsArrayCount(doc
->pages
));
984 if (doc
->number_up
> 1 || doc
->fitplot
)
985 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
986 PageLeft
, PageBottom
, PageRight
, PageTop
);
988 printf("%%%%BoundingBox: %d %d %d %d\n",
989 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
990 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
994 * Start a new document...
997 ppdEmitJCLEnd(ppd
, stdout
);
998 ppdEmitJCL(ppd
, stdout
, doc
->job_id
, doc
->user
, doc
->title
);
1000 puts("%!PS-Adobe-3.0");
1006 * Copy the prolog as needed...
1011 pageinfo
= (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
1012 copy_bytes(doc
->temp
, 0, pageinfo
->offset
);
1016 * Then copy all of the pages...
1019 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayLast(doc
->pages
) :
1020 (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
1029 if (!ppd
|| !ppd
->num_filters
)
1030 fprintf(stderr
, "PAGE: %d %d\n", number
,
1031 doc
->slow_collate
? 1 : doc
->copies
);
1033 if (doc
->number_up
> 1)
1035 printf("%%%%Page: (%d) %d\n", number
, number
);
1036 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1037 PageLeft
, PageBottom
, PageRight
, PageTop
);
1041 printf("%%%%Page: %s %d\n", pageinfo
->label
, number
);
1042 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1043 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
1044 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
1047 copy_bytes(doc
->temp
, pageinfo
->offset
, pageinfo
->length
);
1049 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayPrev(doc
->pages
) :
1050 (pstops_page_t
*)cupsArrayNext(doc
->pages
);
1056 * Restore the old showpage operator as needed...
1059 if (doc
->use_ESPshowpage
)
1060 puts("userdict/showpage/ESPshowpage load put\n");
1063 * Write/copy the trailer...
1067 copy_trailer(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
1072 * 'copy_non_dsc()' - Copy a document that does not conform to the DSC.
1074 * This function expects "line" to be filled with the %! comment line.
1078 copy_non_dsc(cups_file_t
*fp
, /* I - File to read from */
1079 pstops_doc_t
*doc
, /* I - Document info */
1080 ppd_file_t
*ppd
, /* I - PPD file */
1081 char *line
, /* I - Line buffer */
1082 ssize_t linelen
, /* I - Length of initial line */
1083 size_t linesize
) /* I - Size of line buffer */
1085 int copy
; /* Current copy */
1086 char buffer
[8192]; /* Copy buffer */
1087 int bytes
; /* Number of bytes copied */
1091 * First let the user know that they are attempting to print a file
1092 * that may not print correctly...
1095 _cupsLangPuts(stderr
,
1096 _("WARNING: This document does not conform to the Adobe "
1097 "Document Structuring Conventions and may not print "
1101 * Then write a standard DSC comment section...
1104 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", PageLeft
, PageBottom
,
1105 PageRight
, PageTop
);
1107 if (doc
->slow_collate
&& doc
->copies
> 1)
1108 printf("%%%%Pages: %d\n", doc
->copies
);
1112 WriteTextComment("For", doc
->user
);
1113 WriteTextComment("Title", doc
->title
);
1115 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1118 * Tell the document processor the copy and duplex options
1119 * that are required...
1122 printf("%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
1123 doc
->collate
? " collate" : "",
1124 Duplex
? " duplex" : "");
1127 * Apple uses RBI comments for various non-PPD options...
1130 printf("%%RBINumCopies: %d\n", doc
->copies
);
1135 * Tell the document processor the duplex option that is required...
1139 puts("%%Requirements: duplex");
1142 * Apple uses RBI comments for various non-PPD options...
1145 puts("%RBINumCopies: 1");
1148 puts("%%EndComments");
1151 * Then the prolog...
1154 puts("%%BeginProlog");
1156 do_prolog(doc
, ppd
);
1158 puts("%%EndProlog");
1161 * Then the setup section...
1164 puts("%%BeginSetup");
1171 * Finally, embed a copy of the file inside a %%Page...
1174 if (!ppd
|| !ppd
->num_filters
)
1175 fprintf(stderr
, "PAGE: 1 %d\n", doc
->temp
? 1 : doc
->copies
);
1177 puts("%%Page: 1 1");
1178 puts("%%BeginPageSetup");
1179 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1180 puts("%%EndPageSetup");
1181 puts("%%BeginDocument: nondsc");
1183 fwrite(line
, linelen
, 1, stdout
);
1186 cupsFileWrite(doc
->temp
, line
, linelen
);
1188 while ((bytes
= cupsFileRead(fp
, buffer
, sizeof(buffer
))) > 0)
1190 fwrite(buffer
, 1, bytes
, stdout
);
1193 cupsFileWrite(doc
->temp
, buffer
, bytes
);
1196 puts("%%EndDocument");
1198 if (doc
->use_ESPshowpage
)
1200 WriteLabels(Orientation
);
1201 puts("ESPshowpage");
1204 if (doc
->temp
&& !JobCanceled
)
1207 * Reopen the temporary file for reading...
1210 cupsFileClose(doc
->temp
);
1212 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
1215 * Make the additional copies as needed...
1218 for (copy
= 1; copy
< doc
->copies
; copy
++)
1223 if (!ppd
|| !ppd
->num_filters
)
1224 fputs("PAGE: 1 1\n", stderr
);
1226 printf("%%%%Page: %d %d\n", copy
+ 1, copy
+ 1);
1227 puts("%%BeginPageSetup");
1228 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1229 puts("%%EndPageSetup");
1230 puts("%%BeginDocument: nondsc");
1232 copy_bytes(doc
->temp
, 0, 0);
1234 puts("%%EndDocument");
1236 if (doc
->use_ESPshowpage
)
1238 WriteLabels(Orientation
);
1239 puts("ESPshowpage");
1245 * Restore the old showpage operator as needed...
1248 if (doc
->use_ESPshowpage
)
1249 puts("userdict/showpage/ESPshowpage load put\n");
1254 * 'copy_page()' - Copy a page description.
1256 * This function expects "line" to be filled with a %%Page comment line.
1257 * On return, "line" will contain the next line in the file, if any.
1260 static ssize_t
/* O - Length of next line */
1261 copy_page(cups_file_t
*fp
, /* I - File to read from */
1262 pstops_doc_t
*doc
, /* I - Document info */
1263 ppd_file_t
*ppd
, /* I - PPD file */
1264 int number
, /* I - Current page number */
1265 char *line
, /* I - Line buffer */
1266 ssize_t linelen
, /* I - Length of initial line */
1267 size_t linesize
) /* I - Size of line buffer */
1269 char label
[256], /* Page label string */
1270 *ptr
; /* Pointer into line */
1271 int level
; /* Embedded document level */
1272 pstops_page_t
*pageinfo
; /* Page information */
1273 int first_page
; /* First page on N-up output? */
1274 int has_page_setup
; /* Does the page have %%Begin/EndPageSetup? */
1275 int bounding_box
[4]; /* PageBoundingBox */
1279 * Get the page label for this page...
1282 first_page
= is_first_page(number
);
1284 if (!parse_text(line
+ 7, &ptr
, label
, sizeof(label
)))
1286 _cupsLangPuts(stderr
, _("ERROR: Bad %%Page: comment in file!\n"));
1290 else if (strtol(ptr
, &ptr
, 10) == LONG_MAX
|| !isspace(*ptr
& 255))
1292 _cupsLangPuts(stderr
, _("ERROR: Bad %%Page: comment in file!\n"));
1297 * Create or update the current output page...
1301 pageinfo
= add_page(doc
, label
);
1303 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
1306 * Handle first page override...
1309 if (doc
->ap_input_slot
|| doc
->ap_manual_feed
)
1314 * First page/sheet gets AP_FIRSTPAGE_* options...
1317 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->ap_input_slot
,
1318 pageinfo
->num_options
,
1319 &(pageinfo
->options
));
1320 pageinfo
->num_options
= cupsAddOption("ManualFeed",
1321 doc
->ap_input_slot
? "False" :
1322 doc
->ap_manual_feed
,
1323 pageinfo
->num_options
,
1324 &(pageinfo
->options
));
1326 else if (doc
->page
== (Duplex
+ 2))
1329 * Second page/sheet gets default options...
1332 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->input_slot
,
1333 pageinfo
->num_options
,
1334 &(pageinfo
->options
));
1335 pageinfo
->num_options
= cupsAddOption("ManualFeed",
1336 doc
->input_slot
? "False" :
1338 pageinfo
->num_options
,
1339 &(pageinfo
->options
));
1344 * Scan comments until we see something other than %%Page*: or
1348 memcpy(bounding_box
, doc
->bounding_box
, sizeof(bounding_box
));
1350 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1352 if (!strncmp(line
, "%%PageBoundingBox:", 18))
1355 * %%PageBoundingBox: llx lly urx ury
1358 if (sscanf(line
+ 18, "%d%d%d%d", bounding_box
+ 0,
1359 bounding_box
+ 1, bounding_box
+ 2,
1360 bounding_box
+ 3) != 4)
1362 _cupsLangPuts(stderr
,
1363 _("ERROR: Bad %%PageBoundingBox: comment in file!\n"));
1364 memcpy(bounding_box
, doc
->bounding_box
,
1365 sizeof(bounding_box
));
1367 else if (doc
->number_up
== 1 && !doc
->fitplot
&& Orientation
)
1369 int temp_bbox
[4]; /* Temporary bounding box */
1372 memcpy(temp_bbox
, bounding_box
, sizeof(temp_bbox
));
1374 fprintf(stderr
, "DEBUG: Orientation = %d\n", Orientation
);
1375 fprintf(stderr
, "DEBUG: original bounding_box = [ %d %d %d %d ]\n",
1376 bounding_box
[0], bounding_box
[1],
1377 bounding_box
[2], bounding_box
[3]);
1378 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
1379 PageWidth
, PageLength
);
1381 switch (Orientation
)
1383 case 1 : /* Landscape */
1384 bounding_box
[0] = PageLength
- temp_bbox
[3];
1385 bounding_box
[1] = temp_bbox
[0];
1386 bounding_box
[2] = PageLength
- temp_bbox
[1];
1387 bounding_box
[3] = temp_bbox
[2];
1390 case 2 : /* Reverse Portrait */
1391 bounding_box
[0] = PageWidth
- temp_bbox
[2];
1392 bounding_box
[1] = PageLength
- temp_bbox
[3];
1393 bounding_box
[2] = PageWidth
- temp_bbox
[0];
1394 bounding_box
[3] = PageLength
- temp_bbox
[1];
1397 case 3 : /* Reverse Landscape */
1398 bounding_box
[0] = temp_bbox
[1];
1399 bounding_box
[1] = PageWidth
- temp_bbox
[2];
1400 bounding_box
[2] = temp_bbox
[3];
1401 bounding_box
[3] = PageWidth
- temp_bbox
[0];
1405 fprintf(stderr
, "DEBUG: updated bounding_box = [ %d %d %d %d ]\n",
1406 bounding_box
[0], bounding_box
[1],
1407 bounding_box
[2], bounding_box
[3]);
1411 else if (!strncmp(line
, "%%PageCustomColors:", 19) ||
1412 !strncmp(line
, "%%PageMedia:", 12) ||
1413 !strncmp(line
, "%%PageOrientation:", 18) ||
1414 !strncmp(line
, "%%PageProcessColors:", 20) ||
1415 !strncmp(line
, "%%PageRequirements:", 18) ||
1416 !strncmp(line
, "%%PageResources:", 16))
1423 else if (!strncmp(line
, "%%PageCustomColors:", 19))
1426 * %%PageCustomColors: ...
1429 else if (!strncmp(line
, "%%PageMedia:", 12))
1435 else if (!strncmp(line
, "%%PageOrientation:", 18))
1438 * %%PageOrientation: ...
1441 else if (!strncmp(line
, "%%PageProcessColors:", 20))
1444 * %%PageProcessColors: ...
1447 else if (!strncmp(line
, "%%PageRequirements:", 18))
1450 * %%PageRequirements: ...
1453 else if (!strncmp(line
, "%%PageResources:", 16))
1456 * %%PageResources: ...
1459 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1462 * %%IncludeFeature: *MainKeyword OptionKeyword
1465 if (doc
->number_up
== 1 &&!doc
->fitplot
)
1466 pageinfo
->num_options
= include_feature(ppd
, line
,
1467 pageinfo
->num_options
,
1468 &(pageinfo
->options
));
1470 else if (strncmp(line
, "%%Include", 9))
1474 if (doc
->number_up
== 1)
1477 * Update the document's composite and page bounding box...
1480 memcpy(pageinfo
->bounding_box
, bounding_box
,
1481 sizeof(pageinfo
->bounding_box
));
1483 if (bounding_box
[0] < doc
->new_bounding_box
[0])
1484 doc
->new_bounding_box
[0] = bounding_box
[0];
1485 if (bounding_box
[1] < doc
->new_bounding_box
[1])
1486 doc
->new_bounding_box
[1] = bounding_box
[1];
1487 if (bounding_box
[2] > doc
->new_bounding_box
[2])
1488 doc
->new_bounding_box
[2] = bounding_box
[2];
1489 if (bounding_box
[3] > doc
->new_bounding_box
[3])
1490 doc
->new_bounding_box
[3] = bounding_box
[3];
1494 * Output the page header as needed...
1497 if (!doc
->slow_order
&& first_page
)
1499 if (!ppd
|| !ppd
->num_filters
)
1500 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
1501 doc
->slow_collate
? 1 : doc
->copies
);
1503 if (doc
->number_up
> 1)
1505 printf("%%%%Page: (%d) %d\n", doc
->page
, doc
->page
);
1506 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1507 PageLeft
, PageBottom
, PageRight
, PageTop
);
1511 printf("%%%%Page: %s %d\n", pageinfo
->label
, doc
->page
);
1512 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1513 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
1514 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
1519 * Copy any page setup commands...
1523 doc_puts(doc
, "%%BeginPageSetup\n");
1525 if ((has_page_setup
= !strncmp(line
, "%%BeginPageSetup", 16)) != 0)
1527 int feature
= 0; /* In a Begin/EndFeature block? */
1529 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1531 if (!strncmp(line
, "%%EndPageSetup", 14))
1533 else if (!strncmp(line
, "%%BeginFeature:", 15))
1537 if (doc
->number_up
> 1 || doc
->fitplot
)
1540 else if (!strncmp(line
, "%%EndFeature", 12))
1544 if (doc
->number_up
> 1 || doc
->fitplot
)
1547 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1549 pageinfo
->num_options
= include_feature(ppd
, line
,
1550 pageinfo
->num_options
,
1551 &(pageinfo
->options
));
1554 else if (!strncmp(line
, "%%Include", 9))
1557 if (line
[0] != '%' && !feature
)
1560 if (!feature
|| (doc
->number_up
== 1 && !doc
->fitplot
))
1561 doc_write(doc
, line
, linelen
);
1565 * Skip %%EndPageSetup...
1568 if (linelen
> 0 && !strncmp(line
, "%%EndPageSetup", 14))
1570 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1577 char *page_setup
; /* PageSetup commands to send */
1580 if (pageinfo
->num_options
> 0)
1581 write_options(doc
, ppd
, pageinfo
->num_options
, pageinfo
->options
);
1584 * Output commands for the current page...
1587 page_setup
= ppdEmitString(ppd
, PPD_ORDER_PAGE
, 0);
1591 doc_puts(doc
, page_setup
);
1597 * Prep for the start of the page description...
1600 start_nup(doc
, number
, 1, bounding_box
);
1603 * Finish the PageSetup section as needed...
1608 int feature
= 0; /* In a Begin/EndFeature block? */
1610 doc_write(doc
, line
, linelen
);
1612 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1614 if (!strncmp(line
, "%%EndPageSetup", 14))
1616 else if (!strncmp(line
, "%%BeginFeature:", 15))
1620 if (doc
->number_up
> 1 || doc
->fitplot
)
1623 else if (!strncmp(line
, "%%EndFeature", 12))
1627 if (doc
->number_up
> 1 || doc
->fitplot
)
1630 else if (!strncmp(line
, "%%Include", 9))
1633 if (!feature
|| (doc
->number_up
== 1 && !doc
->fitplot
))
1634 doc_write(doc
, line
, linelen
);
1638 * Skip %%EndPageSetup...
1641 if (linelen
> 0 && !strncmp(line
, "%%EndPageSetup", 14))
1642 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1646 doc_puts(doc
, "%%EndPageSetup\n");
1649 * Read the rest of the page description...
1657 (!strncmp(line
, "%%Page:", 7) ||
1658 !strncmp(line
, "%%Trailer", 9) ||
1659 !strncmp(line
, "%%EOF", 5)))
1661 else if (!strncmp(line
, "%%BeginDocument", 15) ||
1662 !strncmp(line
, "%ADO_BeginApplication", 21))
1664 doc_write(doc
, line
, linelen
);
1668 else if ((!strncmp(line
, "%%EndDocument", 13) ||
1669 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
1671 doc_write(doc
, line
, linelen
);
1675 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
1676 (!strncmp(line
, "%%BeginData:", 12) &&
1677 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
1680 * Copy binary data...
1683 int bytes
; /* Bytes of data */
1686 doc_write(doc
, line
, linelen
);
1688 bytes
= atoi(strchr(line
, ':') + 1);
1692 if (bytes
> linesize
)
1693 linelen
= cupsFileRead(fp
, line
, linesize
);
1695 linelen
= cupsFileRead(fp
, line
, bytes
);
1700 perror("ERROR: Early end-of-file while reading binary data");
1704 doc_write(doc
, line
, linelen
);
1710 doc_write(doc
, line
, linelen
);
1712 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0);
1715 * Finish up this page and return...
1718 end_nup(doc
, number
);
1720 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
1727 * 'copy_prolog()' - Copy the document prolog section.
1729 * This function expects "line" to be filled with a %%BeginProlog comment line.
1730 * On return, "line" will contain the next line in the file, if any.
1733 static ssize_t
/* O - Length of next line */
1734 copy_prolog(cups_file_t
*fp
, /* I - File to read from */
1735 pstops_doc_t
*doc
, /* I - Document info */
1736 ppd_file_t
*ppd
, /* I - PPD file */
1737 char *line
, /* I - Line buffer */
1738 ssize_t linelen
, /* I - Length of initial line */
1739 size_t linesize
) /* I - Size of line buffer */
1741 while (strncmp(line
, "%%BeginProlog", 13))
1743 if (!strncmp(line
, "%%BeginSetup", 12) || !strncmp(line
, "%%Page:", 7))
1746 doc_write(doc
, line
, linelen
);
1748 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1752 doc_puts(doc
, "%%BeginProlog\n");
1754 do_prolog(doc
, ppd
);
1756 if (!strncmp(line
, "%%BeginProlog", 13))
1758 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1760 if (!strncmp(line
, "%%EndProlog", 11) ||
1761 !strncmp(line
, "%%BeginSetup", 12) ||
1762 !strncmp(line
, "%%Page:", 7))
1765 doc_write(doc
, line
, linelen
);
1768 if (!strncmp(line
, "%%EndProlog", 11))
1769 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1771 _cupsLangPuts(stderr
, _("ERROR: Missing %%EndProlog!\n"));
1774 doc_puts(doc
, "%%EndProlog\n");
1781 * 'copy_setup()' - Copy the document setup section.
1783 * This function expects "line" to be filled with a %%BeginSetup comment line.
1784 * On return, "line" will contain the next line in the file, if any.
1787 static ssize_t
/* O - Length of next line */
1788 copy_setup(cups_file_t
*fp
, /* I - File to read from */
1789 pstops_doc_t
*doc
, /* I - Document info */
1790 ppd_file_t
*ppd
, /* I - PPD file */
1791 char *line
, /* I - Line buffer */
1792 ssize_t linelen
, /* I - Length of initial line */
1793 size_t linesize
) /* I - Size of line buffer */
1795 int num_options
; /* Number of options */
1796 cups_option_t
*options
; /* Options */
1799 while (strncmp(line
, "%%BeginSetup", 12))
1801 if (!strncmp(line
, "%%Page:", 7))
1804 doc_write(doc
, line
, linelen
);
1806 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1810 doc_puts(doc
, "%%BeginSetup\n");
1817 if (!strncmp(line
, "%%BeginSetup", 12))
1819 while (strncmp(line
, "%%EndSetup", 10))
1821 if (!strncmp(line
, "%%Page:", 7))
1823 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1826 * %%IncludeFeature: *MainKeyword OptionKeyword
1829 if (doc
->number_up
== 1 && !doc
->fitplot
)
1830 num_options
= include_feature(ppd
, line
, num_options
, &options
);
1832 else if (strncmp(line
, "%%BeginSetup", 12))
1833 doc_write(doc
, line
, linelen
);
1835 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1839 if (!strncmp(line
, "%%EndSetup", 10))
1840 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1842 _cupsLangPuts(stderr
, _("ERROR: Missing %%EndSetup!\n"));
1845 if (num_options
> 0)
1847 write_options(doc
, ppd
, num_options
, options
);
1848 cupsFreeOptions(num_options
, options
);
1851 doc_puts(doc
, "%%EndSetup\n");
1858 * 'copy_trailer()' - Copy the document trailer.
1860 * This function expects "line" to be filled with a %%Trailer comment line.
1861 * On return, "line" will contain the next line in the file, if any.
1864 static ssize_t
/* O - Length of next line */
1865 copy_trailer(cups_file_t
*fp
, /* I - File to read from */
1866 pstops_doc_t
*doc
, /* I - Document info */
1867 ppd_file_t
*ppd
, /* I - PPD file */
1868 int number
, /* I - Number of pages */
1869 char *line
, /* I - Line buffer */
1870 ssize_t linelen
, /* I - Length of initial line */
1871 size_t linesize
) /* I - Size of line buffer */
1874 * Write the trailer comments...
1881 if (!strncmp(line
, "%%EOF", 5))
1883 else if (strncmp(line
, "%%Trailer", 9) &&
1884 strncmp(line
, "%%Pages:", 8) &&
1885 strncmp(line
, "%%BoundingBox:", 14))
1886 fwrite(line
, 1, linelen
, stdout
);
1888 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1891 fprintf(stderr
, "DEBUG: Wrote %d pages...\n", number
);
1893 printf("%%%%Pages: %d\n", number
);
1894 if (doc
->number_up
> 1 || doc
->fitplot
)
1895 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
1896 PageLeft
, PageBottom
, PageRight
, PageTop
);
1898 printf("%%%%BoundingBox: %d %d %d %d\n",
1899 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
1900 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
1907 * 'do_prolog()' - Send the necessary document prolog commands.
1911 do_prolog(pstops_doc_t
*doc
, /* I - Document information */
1912 ppd_file_t
*ppd
) /* I - PPD file */
1914 char *ps
; /* PS commands */
1918 * Send the document prolog commands...
1921 if (ppd
&& ppd
->patches
)
1923 doc_puts(doc
, "%%BeginFeature: *JobPatchFile 1\n");
1924 doc_puts(doc
, ppd
->patches
);
1925 doc_puts(doc
, "\n%%EndFeature\n");
1928 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_PROLOG
, 0.0)) != NULL
)
1935 * Define ESPshowpage here so that applications that define their
1936 * own procedure to do a showpage pick it up...
1939 if (doc
->use_ESPshowpage
)
1940 doc_puts(doc
, "userdict/ESPshowpage/showpage load put\n"
1941 "userdict/showpage{}put\n");
1946 * 'do_setup()' - Send the necessary document setup commands.
1950 do_setup(pstops_doc_t
*doc
, /* I - Document information */
1951 ppd_file_t
*ppd
) /* I - PPD file */
1953 char *ps
; /* PS commands */
1957 * Disable CTRL-D so that embedded files don't cause printing
1961 doc_puts(doc
, "% Disable CTRL-D as an end-of-file marker...\n");
1962 doc_puts(doc
, "userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put\n");
1965 * Mark job options...
1968 cupsMarkOptions(ppd
, doc
->num_options
, doc
->options
);
1971 * Send all the printer-specific setup commands...
1974 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, 0.0)) != NULL
)
1980 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_ANY
, 0.0)) != NULL
)
1987 * Set the number of copies for the job...
1990 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1992 doc_printf(doc
, "%%RBIBeginNonPPDFeature: *NumCopies %d\n", doc
->copies
);
1994 "%d/languagelevel where{pop languagelevel 2 ge}{false}ifelse\n"
1995 "{1 dict begin/NumCopies exch def currentdict end "
1997 "{userdict/#copies 3 -1 roll put}ifelse\n", doc
->copies
);
1998 doc_puts(doc
, "%RBIEndNonPPDFeature\n");
2002 * If we are doing N-up printing, disable setpagedevice...
2005 if (doc
->number_up
> 1)
2007 doc_puts(doc
, "userdict/CUPSsetpagedevice/setpagedevice load put\n");
2008 doc_puts(doc
, "userdict/setpagedevice{pop}bind put\n");
2012 * Changes to the transfer function must be made AFTER any
2013 * setpagedevice code...
2016 if (doc
->gamma
!= 1.0f
|| doc
->brightness
!= 1.0f
)
2017 doc_printf(doc
, "{ neg 1 add dup 0 lt { pop 1 } { %.3f exp neg 1 add } "
2018 "ifelse %.3f mul } bind settransfer\n",
2019 doc
->gamma
, doc
->brightness
);
2022 * Make sure we have rectclip and rectstroke procedures of some sort...
2026 "% x y w h ESPrc - Clip to a rectangle.\n"
2027 "userdict/ESPrc/rectclip where{pop/rectclip load}\n"
2028 "{{newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
2029 "neg 0 rlineto closepath clip newpath}bind}ifelse put\n");
2032 "% x y w h ESPrf - Fill a rectangle.\n"
2033 "userdict/ESPrf/rectfill where{pop/rectfill load}\n"
2034 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
2035 "neg 0 rlineto closepath fill grestore}bind}ifelse put\n");
2038 "% x y w h ESPrs - Stroke a rectangle.\n"
2039 "userdict/ESPrs/rectstroke where{pop/rectstroke load}\n"
2040 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
2041 "neg 0 rlineto closepath stroke grestore}bind}ifelse put\n");
2044 * Write the page and label prologs...
2047 if (doc
->number_up
== 2 || doc
->number_up
== 6)
2050 * For 2- and 6-up output, rotate the labels to match the orientation
2054 if (Orientation
& 1)
2055 write_label_prolog(doc
, doc
->page_label
, PageBottom
,
2056 PageWidth
- PageLength
+ PageTop
, PageLength
);
2058 write_label_prolog(doc
, doc
->page_label
, PageLeft
, PageRight
,
2062 write_label_prolog(doc
, doc
->page_label
, PageBottom
, PageTop
, PageWidth
);
2067 * 'doc_printf()' - Send a formatted string to stdout and/or the temp file.
2069 * This function should be used for all page-level output that is affected
2070 * by ordering, collation, etc.
2074 doc_printf(pstops_doc_t
*doc
, /* I - Document information */
2075 const char *format
, /* I - Printf-style format string */
2076 ...) /* I - Additional arguments as needed */
2078 va_list ap
; /* Pointer to arguments */
2079 char buffer
[1024]; /* Output buffer */
2080 size_t bytes
; /* Number of bytes to write */
2083 va_start(ap
, format
);
2084 bytes
= vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
2087 if (bytes
> sizeof(buffer
))
2089 _cupsLangPrintf(stderr
,
2090 _("ERROR: doc_printf overflow (%d bytes) detected, "
2091 "aborting!\n"), (int)bytes
);
2095 doc_write(doc
, buffer
, bytes
);
2100 * 'doc_puts()' - Send a nul-terminated string to stdout and/or the temp file.
2102 * This function should be used for all page-level output that is affected
2103 * by ordering, collation, etc.
2107 doc_puts(pstops_doc_t
*doc
, /* I - Document information */
2108 const char *s
) /* I - String to send */
2110 doc_write(doc
, s
, strlen(s
));
2115 * 'doc_write()' - Send data to stdout and/or the temp file.
2119 doc_write(pstops_doc_t
*doc
, /* I - Document information */
2120 const char *s
, /* I - Data to send */
2121 size_t len
) /* I - Number of bytes to send */
2123 if (!doc
->slow_order
)
2124 fwrite(s
, 1, len
, stdout
);
2127 cupsFileWrite(doc
->temp
, s
, len
);
2132 * 'end_nup()' - End processing for N-up printing.
2136 end_nup(pstops_doc_t
*doc
, /* I - Document information */
2137 int number
) /* I - Page number */
2139 if (doc
->number_up
> 1)
2140 doc_puts(doc
, "userdict/ESPsave get restore\n");
2142 switch (doc
->number_up
)
2145 if (doc
->use_ESPshowpage
)
2147 write_labels(doc
, Orientation
);
2148 doc_puts(doc
, "ESPshowpage\n");
2154 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2156 if (Orientation
& 1)
2159 * Rotate the labels back to portrait...
2162 write_labels(doc
, Orientation
- 1);
2164 else if (Orientation
== 0)
2167 * Rotate the labels to landscape...
2170 write_labels(doc
, doc
->normal_landscape
? 1 : 3);
2175 * Rotate the labels to landscape...
2178 write_labels(doc
, doc
->normal_landscape
? 3 : 1);
2181 doc_puts(doc
, "ESPshowpage\n");
2186 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2188 write_labels(doc
, Orientation
);
2189 doc_puts(doc
, "ESPshowpage\n");
2199 * 'include_feature()' - Include a printer option/feature command.
2202 static int /* O - New number of options */
2204 ppd_file_t
*ppd
, /* I - PPD file */
2205 const char *line
, /* I - DSC line */
2206 int num_options
, /* I - Number of options */
2207 cups_option_t
**options
) /* IO - Options */
2209 char name
[255], /* Option name */
2210 value
[255]; /* Option value */
2211 ppd_option_t
*option
; /* Option in file */
2215 * Get the "%%IncludeFeature: *Keyword OptionKeyword" values...
2218 if (sscanf(line
+ 17, "%254s%254s", name
, value
) != 2)
2220 _cupsLangPuts(stderr
, _("ERROR: Bad %%IncludeFeature: comment!\n"));
2221 return (num_options
);
2225 * Find the option and choice...
2228 if ((option
= ppdFindOption(ppd
, name
+ 1)) == NULL
)
2230 _cupsLangPrintf(stderr
, _("WARNING: Unknown option \"%s\"!\n"), name
+ 1);
2231 return (num_options
);
2234 if (option
->section
== PPD_ORDER_EXIT
||
2235 option
->section
== PPD_ORDER_JCL
)
2237 _cupsLangPrintf(stderr
, _("WARNING: Option \"%s\" cannot be included via "
2238 "IncludeFeature!\n"), name
+ 1);
2239 return (num_options
);
2242 if (!ppdFindChoice(option
, value
))
2244 _cupsLangPrintf(stderr
,
2245 _("WARNING: Unknown choice \"%s\" for option \"%s\"!\n"),
2247 return (num_options
);
2251 * Add the option to the option array and return...
2254 return (cupsAddOption(name
+ 1, value
, num_options
, options
));
2259 * 'parse_text()' - Parse a text value in a comment.
2261 * This function parses a DSC text value as defined on page 36 of the
2262 * DSC specification. Text values are either surrounded by parenthesis
2263 * or whitespace-delimited.
2265 * The value returned is the literal characters for the entire text
2266 * string, including any parenthesis and escape characters.
2269 static char * /* O - Value or NULL on error */
2270 parse_text(const char *start
, /* I - Start of text value */
2271 char **end
, /* O - End of text value */
2272 char *buffer
, /* I - Buffer */
2273 size_t bufsize
) /* I - Size of buffer */
2275 char *bufptr
, /* Pointer in buffer */
2276 *bufend
; /* End of buffer */
2277 int level
; /* Parenthesis level */
2281 * Skip leading whitespace...
2284 while (isspace(*start
& 255))
2288 * Then copy the value...
2293 bufend
= buffer
+ bufsize
- 1;
2295 while (bufptr
< bufend
)
2297 if (isspace(*start
& 255) && !level
)
2304 else if (*start
== ')')
2314 else if (*start
== '\\')
2317 * Copy escaped character...
2320 int i
; /* Looping var */
2324 i
<= 3 && isdigit(start
[i
] & 255) && bufptr
< bufend
;
2325 *bufptr
++ = start
[i
], i
++);
2334 * Return the value and new pointer into the line...
2338 *end
= (char *)start
;
2340 if (bufptr
== bufend
)
2348 * 'set_pstops_options()' - Set pstops options.
2353 pstops_doc_t
*doc
, /* I - Document information */
2354 ppd_file_t
*ppd
, /* I - PPD file */
2355 char *argv
[], /* I - Command-line arguments */
2356 int num_options
, /* I - Number of options */
2357 cups_option_t
*options
) /* I - Options */
2359 const char *val
; /* Option value */
2360 int intval
; /* Integer option value */
2361 ppd_attr_t
*attr
; /* PPD attribute */
2362 ppd_option_t
*option
; /* PPD option */
2363 ppd_choice_t
*choice
; /* PPD choice */
2367 * Initialize document information structure...
2370 memset(doc
, 0, sizeof(pstops_doc_t
));
2372 doc
->job_id
= atoi(argv
[1]);
2373 doc
->user
= argv
[2];
2374 doc
->title
= argv
[3];
2375 doc
->copies
= atoi(argv
[4]);
2377 if (ppd
&& ppd
->landscape
> 0)
2378 doc
->normal_landscape
= 1;
2380 doc
->bounding_box
[0] = (int)PageLeft
;
2381 doc
->bounding_box
[1] = (int)PageBottom
;
2382 doc
->bounding_box
[2] = (int)PageRight
;
2383 doc
->bounding_box
[3] = (int)PageTop
;
2385 doc
->new_bounding_box
[0] = INT_MAX
;
2386 doc
->new_bounding_box
[1] = INT_MAX
;
2387 doc
->new_bounding_box
[2] = INT_MIN
;
2388 doc
->new_bounding_box
[3] = INT_MIN
;
2391 * AP_FIRSTPAGE_InputSlot
2394 doc
->ap_input_slot
= cupsGetOption("AP_FIRSTPAGE_InputSlot", num_options
,
2398 * AP_FIRSTPAGE_ManualFeed
2401 doc
->ap_manual_feed
= cupsGetOption("AP_FIRSTPAGE_ManualFeed", num_options
,
2408 if ((val
= cupsGetOption("brightness", num_options
, options
)) != NULL
)
2411 * Get brightness value from 10 to 1000.
2416 if (intval
< 10 || intval
> 1000)
2418 _cupsLangPrintf(stderr
, _("ERROR: Unsupported brightness value %s, using "
2419 "brightness=100!\n"), val
);
2420 doc
->brightness
= 1.0f
;
2423 doc
->brightness
= intval
* 0.01f
;
2426 doc
->brightness
= 1.0f
;
2429 * collate, multiple-document-handling
2432 if ((val
= cupsGetOption("multiple-document-handling", num_options
, options
)) != NULL
)
2435 * This IPP attribute is unnecessarily complicated...
2437 * single-document, separate-documents-collated-copies, and
2438 * single-document-new-sheet all require collated copies.
2440 * separate-documents-uncollated-copies allows for uncollated copies.
2443 doc
->collate
= strcasecmp(val
, "separate-documents-uncollated-copies") != 0;
2446 if ((val
= cupsGetOption("Collate", num_options
, options
)) != NULL
&&
2447 (!strcasecmp(val
, "true") ||!strcasecmp(val
, "on") ||
2448 !strcasecmp(val
, "yes")))
2455 if ((val
= cupsGetOption("emit-jcl", num_options
, options
)) != NULL
&&
2456 (!strcasecmp(val
, "false") || !strcasecmp(val
, "off") ||
2457 !strcasecmp(val
, "no") || !strcmp(val
, "0")))
2463 * fitplot/fit-to-page
2466 if ((val
= cupsGetOption("fitplot", num_options
, options
)) != NULL
&&
2467 !strcasecmp(val
, "true"))
2469 else if ((val
= cupsGetOption("fit-to-page", num_options
, options
)) != NULL
&&
2470 !strcasecmp(val
, "true"))
2477 if ((val
= cupsGetOption("gamma", num_options
, options
)) != NULL
)
2480 * Get gamma value from 1 to 10000...
2485 if (intval
< 1 || intval
> 10000)
2487 _cupsLangPrintf(stderr
, _("ERROR: Unsupported gamma value %s, using "
2488 "gamma=1000!\n"), val
);
2492 doc
->gamma
= intval
* 0.001f
;
2501 if ((choice
= ppdFindMarkedChoice(ppd
, "InputSlot")) != NULL
)
2502 doc
->input_slot
= choice
->choice
;
2508 if ((choice
= ppdFindMarkedChoice(ppd
, "ManualFeed")) != NULL
)
2509 doc
->manual_feed
= choice
->choice
;
2511 if ((choice
= ppdFindMarkedChoice(ppd
, "MirrorPrint")) != NULL
)
2513 val
= choice
->choice
;
2517 val
= cupsGetOption("mirror", num_options
, options
);
2519 if (val
&& (!strcasecmp(val
, "true") || !strcasecmp(val
, "on") ||
2520 !strcasecmp(val
, "yes")))
2527 if ((val
= cupsGetOption("number-up", num_options
, options
)) != NULL
)
2529 switch (intval
= atoi(val
))
2537 doc
->number_up
= intval
;
2540 _cupsLangPrintf(stderr
,
2541 _("ERROR: Unsupported number-up value %d, using "
2542 "number-up=1!\n"), intval
);
2554 if ((val
= cupsGetOption("number-up-layout", num_options
, options
)) != NULL
)
2556 if (!strcasecmp(val
, "lrtb"))
2557 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2558 else if (!strcasecmp(val
, "lrbt"))
2559 doc
->number_up_layout
= PSTOPS_LAYOUT_LRBT
;
2560 else if (!strcasecmp(val
, "rltb"))
2561 doc
->number_up_layout
= PSTOPS_LAYOUT_RLTB
;
2562 else if (!strcasecmp(val
, "rlbt"))
2563 doc
->number_up_layout
= PSTOPS_LAYOUT_RLBT
;
2564 else if (!strcasecmp(val
, "tblr"))
2565 doc
->number_up_layout
= PSTOPS_LAYOUT_TBLR
;
2566 else if (!strcasecmp(val
, "tbrl"))
2567 doc
->number_up_layout
= PSTOPS_LAYOUT_TBRL
;
2568 else if (!strcasecmp(val
, "btlr"))
2569 doc
->number_up_layout
= PSTOPS_LAYOUT_BTLR
;
2570 else if (!strcasecmp(val
, "btrl"))
2571 doc
->number_up_layout
= PSTOPS_LAYOUT_BTRL
;
2574 _cupsLangPrintf(stderr
, _("ERROR: Unsupported number-up-layout value %s, "
2575 "using number-up-layout=lrtb!\n"), val
);
2576 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2580 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2586 if ((val
= cupsGetOption("OutputOrder", num_options
, options
)) != NULL
)
2588 if (!strcasecmp(val
, "Reverse"))
2589 doc
->output_order
= 1;
2594 * Figure out the right default output order from the PPD file...
2597 if ((choice
= ppdFindMarkedChoice(ppd
, "OutputBin")) != NULL
&&
2598 (attr
= ppdFindAttr(ppd
, "PageStackOrder", choice
->choice
)) != NULL
&&
2600 doc
->output_order
= !strcasecmp(attr
->value
, "Reverse");
2601 else if ((attr
= ppdFindAttr(ppd
, "DefaultOutputOrder", NULL
)) != NULL
&&
2603 doc
->output_order
= !strcasecmp(attr
->value
, "Reverse");
2610 if ((val
= cupsGetOption("page-border", num_options
, options
)) != NULL
)
2612 if (!strcasecmp(val
, "none"))
2613 doc
->page_border
= PSTOPS_BORDERNONE
;
2614 else if (!strcasecmp(val
, "single"))
2615 doc
->page_border
= PSTOPS_BORDERSINGLE
;
2616 else if (!strcasecmp(val
, "single-thick"))
2617 doc
->page_border
= PSTOPS_BORDERSINGLE2
;
2618 else if (!strcasecmp(val
, "double"))
2619 doc
->page_border
= PSTOPS_BORDERDOUBLE
;
2620 else if (!strcasecmp(val
, "double-thick"))
2621 doc
->page_border
= PSTOPS_BORDERDOUBLE2
;
2624 _cupsLangPrintf(stderr
, _("ERROR: Unsupported page-border value %s, "
2625 "using page-border=none!\n"), val
);
2626 doc
->page_border
= PSTOPS_BORDERNONE
;
2630 doc
->page_border
= PSTOPS_BORDERNONE
;
2636 doc
->page_label
= cupsGetOption("page-label", num_options
, options
);
2642 doc
->page_ranges
= cupsGetOption("page-ranges", num_options
, options
);
2648 doc
->page_set
= cupsGetOption("page-set", num_options
, options
);
2651 * Now figure out if we have to force collated copies, etc.
2654 if (ppd
&& ppd
->manual_copies
&& Duplex
&& doc
->copies
> 1)
2657 * Force collated copies when printing a duplexed document to
2658 * a non-PS printer that doesn't do hardware copy generation.
2659 * Otherwise the copies will end up on the front/back side of
2667 * See if we have to filter the fast or slow way...
2670 if (doc
->collate
&& doc
->copies
> 1)
2673 * See if we need to manually collate the pages...
2676 doc
->slow_collate
= 1;
2678 if ((choice
= ppdFindMarkedChoice(ppd
, "Collate")) != NULL
&&
2679 !strcasecmp(choice
->choice
, "True"))
2682 * Hardware collate option is selected, see if the option is
2683 * conflicting - if not, collate in hardware. Otherwise,
2684 * turn the hardware collate option off...
2687 if ((option
= ppdFindOption(ppd
, "Collate")) != NULL
&&
2688 !option
->conflicted
)
2689 doc
->slow_collate
= 0;
2691 ppdMarkOption(ppd
, "Collate", "False");
2695 doc
->slow_collate
= 0;
2697 if (!ppdFindOption(ppd
, "OutputOrder") && doc
->output_order
)
2698 doc
->slow_order
= 1;
2700 doc
->slow_order
= 0;
2703 (doc
->slow_collate
|| doc
->slow_order
||
2704 ((attr
= ppdFindAttr(ppd
, "cupsEvenDuplex", NULL
)) != NULL
&&
2705 attr
->value
&& !strcasecmp(attr
->value
, "true"))))
2706 doc
->slow_duplex
= 1;
2708 doc
->slow_duplex
= 0;
2711 * Create a temporary file for page data if we need to filter slowly...
2714 if (doc
->slow_order
|| doc
->slow_collate
)
2716 if ((doc
->temp
= cupsTempFile2(doc
->tempfile
,
2717 sizeof(doc
->tempfile
))) == NULL
)
2719 _cupsLangPrintError(_("ERROR: Unable to create temporary file"));
2725 * Figure out if we should use ESPshowpage or not...
2728 if (doc
->page_label
|| getenv("CLASSIFICATION") || doc
->number_up
> 1 ||
2732 * Yes, use ESPshowpage...
2735 doc
->use_ESPshowpage
= 1;
2738 fprintf(stderr
, "DEBUG: slow_collate=%d, slow_duplex=%d, slow_order=%d\n",
2739 doc
->slow_collate
, doc
->slow_duplex
, doc
->slow_order
);
2744 * 'skip_page()' - Skip past a page that won't be printed.
2747 static ssize_t
/* O - Length of next line */
2748 skip_page(cups_file_t
*fp
, /* I - File to read from */
2749 char *line
, /* I - Line buffer */
2750 ssize_t linelen
, /* I - Length of initial line */
2751 size_t linesize
) /* I - Size of line buffer */
2753 int level
; /* Embedded document level */
2758 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
2761 (!strncmp(line
, "%%Page:", 7) || !strncmp(line
, "%%Trailer", 9)))
2763 else if (!strncmp(line
, "%%BeginDocument", 15) ||
2764 !strncmp(line
, "%ADO_BeginApplication", 21))
2766 else if ((!strncmp(line
, "%%EndDocument", 13) ||
2767 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
2769 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
2770 (!strncmp(line
, "%%BeginData:", 12) &&
2771 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
2774 * Skip binary data...
2777 int bytes
; /* Bytes of data */
2780 bytes
= atoi(strchr(line
, ':') + 1);
2784 if (bytes
> linesize
)
2785 linelen
= cupsFileRead(fp
, line
, linesize
);
2787 linelen
= cupsFileRead(fp
, line
, bytes
);
2792 perror("ERROR: Early end-of-file while reading binary data");
2806 * 'start_nup()' - Start processing for N-up printing.
2810 start_nup(pstops_doc_t
*doc
, /* I - Document information */
2811 int number
, /* I - Page number */
2812 int show_border
, /* I - Show the border? */
2813 const int *bounding_box
) /* I - BoundingBox value */
2815 int pos
; /* Position on page */
2816 int x
, y
; /* Relative position of subpage */
2817 float w
, l
, /* Width and length of subpage */
2818 tx
, ty
; /* Translation values for subpage */
2819 float pagew
, /* Printable width of page */
2820 pagel
; /* Printable height of page */
2821 int bboxx
, /* BoundingBox X origin */
2822 bboxy
, /* BoundingBox Y origin */
2823 bboxw
, /* BoundingBox width */
2824 bboxl
; /* BoundingBox height */
2825 float margin
= 0; /* Current margin for border */
2828 if (doc
->number_up
> 1)
2829 doc_puts(doc
, "userdict/ESPsave save put\n");
2831 pos
= (number
- 1) % doc
->number_up
;
2832 pagew
= PageRight
- PageLeft
;
2833 pagel
= PageTop
- PageBottom
;
2837 bboxx
= bounding_box
[0];
2838 bboxy
= bounding_box
[1];
2839 bboxw
= bounding_box
[2] - bounding_box
[0];
2840 bboxl
= bounding_box
[3] - bounding_box
[1];
2850 fprintf(stderr
, "DEBUG: pagew = %.1f, pagel = %.1f\n", pagew
, pagel
);
2851 fprintf(stderr
, "DEBUG: bboxx = %d, bboxy = %d, bboxw = %d, bboxl = %d\n",
2852 bboxx
, bboxy
, bboxw
, bboxl
);
2853 fprintf(stderr
, "DEBUG: PageLeft = %.1f, PageRight = %.1f\n",
2854 PageLeft
, PageRight
);
2855 fprintf(stderr
, "DEBUG: PageTop = %.1f, PageBottom = %.1f\n",
2856 PageTop
, PageBottom
);
2857 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
2858 PageWidth
, PageLength
);
2860 switch (Orientation
)
2862 case 1 : /* Landscape */
2863 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", PageLength
);
2865 case 2 : /* Reverse Portrait */
2866 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", PageWidth
,
2869 case 3 : /* Reverse Landscape */
2870 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", PageWidth
);
2874 if (Duplex
&& doc
->number_up
> 1 && ((number
/ doc
->number_up
) & 1))
2875 doc_printf(doc
, "%.1f %.1f translate\n", PageWidth
- PageRight
, PageBottom
);
2876 else if (doc
->number_up
> 1 || doc
->fitplot
)
2877 doc_printf(doc
, "%.1f %.1f translate\n", PageLeft
, PageBottom
);
2880 doc_printf(doc
, "%.1f 0.0 translate -1 1 scale\n", PageWidth
);
2882 switch (doc
->number_up
)
2888 l
= w
* bboxl
/ bboxw
;
2893 w
= l
* bboxw
/ bboxl
;
2896 tx
= 0.5 * (pagew
- w
);
2897 ty
= 0.5 * (pagel
- l
);
2899 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n", tx
, ty
,
2900 w
/ bboxw
, l
/ bboxl
);
2907 if (Orientation
& 1)
2911 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2915 l
= w
* bboxl
/ bboxw
;
2917 if (l
> (pagew
* 0.5))
2920 w
= l
* bboxw
/ bboxl
;
2923 tx
= 0.5 * (pagew
* 0.5 - l
);
2924 ty
= 0.5 * (pagel
- w
);
2926 if (doc
->normal_landscape
)
2927 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2929 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2931 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2932 ty
, tx
+ pagew
* 0.5 * x
, w
/ bboxw
, l
/ bboxl
);
2938 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2942 w
= l
* bboxw
/ bboxl
;
2944 if (w
> (pagel
* 0.5))
2947 l
= w
* bboxl
/ bboxw
;
2950 tx
= 0.5 * (pagel
* 0.5 - w
);
2951 ty
= 0.5 * (pagew
- l
);
2953 if (doc
->normal_landscape
)
2954 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2956 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2958 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2959 tx
+ pagel
* 0.5 * x
, ty
, w
/ bboxw
, l
/ bboxl
);
2964 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2975 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2978 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2982 l
= w
* bboxl
/ bboxw
;
2984 if (l
> (pagel
* 0.5))
2987 w
= l
* bboxw
/ bboxl
;
2990 tx
= 0.5 * (pagew
* 0.5 - w
);
2991 ty
= 0.5 * (pagel
* 0.5 - l
);
2993 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2994 tx
+ x
* pagew
* 0.5, ty
+ y
* pagel
* 0.5,
2995 w
/ bboxw
, l
/ bboxl
);
2999 if (Orientation
& 1)
3001 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3006 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3009 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3017 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3020 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3025 l
= w
* bboxl
/ bboxw
;
3027 if (l
> (pagew
* 0.333))
3030 w
= l
* bboxw
/ bboxl
;
3033 tx
= 0.5 * (pagel
- 2 * w
);
3034 ty
= 0.5 * (pagew
- 3 * l
);
3036 if (doc
->normal_landscape
)
3037 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
3039 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
3041 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3042 tx
+ x
* w
, ty
+ y
* l
, l
/ bboxl
, w
/ bboxw
);
3046 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3051 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3054 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3062 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3065 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3070 w
= l
* bboxw
/ bboxl
;
3072 if (w
> (pagel
* 0.333))
3075 l
= w
* bboxl
/ bboxw
;
3078 tx
= 0.5 * (pagel
- 3 * w
);
3079 ty
= 0.5 * (pagew
- 2 * l
);
3081 if (doc
->normal_landscape
)
3082 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
3084 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
3086 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3087 tx
+ w
* x
, ty
+ l
* y
, w
/ bboxw
, l
/ bboxl
);
3093 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3104 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3107 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3111 l
= w
* bboxl
/ bboxw
;
3113 if (l
> (pagel
* 0.333))
3116 w
= l
* bboxw
/ bboxl
;
3119 tx
= 0.5 * (pagew
* 0.333 - w
);
3120 ty
= 0.5 * (pagel
* 0.333 - l
);
3122 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3123 tx
+ x
* pagew
* 0.333, ty
+ y
* pagel
* 0.333,
3124 w
/ bboxw
, l
/ bboxl
);
3128 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3139 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3142 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3146 l
= w
* bboxl
/ bboxw
;
3148 if (l
> (pagel
* 0.25))
3151 w
= l
* bboxw
/ bboxl
;
3154 tx
= 0.5 * (pagew
* 0.25 - w
);
3155 ty
= 0.5 * (pagel
* 0.25 - l
);
3157 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3158 tx
+ x
* pagew
* 0.25, ty
+ y
* pagel
* 0.25,
3159 w
/ bboxw
, l
/ bboxl
);
3164 * Draw borders as necessary...
3167 if (doc
->page_border
&& show_border
)
3169 int rects
; /* Number of border rectangles */
3170 float fscale
; /* Scaling value for points */
3173 rects
= (doc
->page_border
& PSTOPS_BORDERDOUBLE
) ? 2 : 1;
3174 fscale
= PageWidth
/ w
;
3175 margin
= 2.25 * fscale
;
3178 * Set the line width and color...
3181 doc_puts(doc
, "gsave\n");
3182 doc_printf(doc
, "%.3f setlinewidth 0 setgray newpath\n",
3183 (doc
->page_border
& PSTOPS_BORDERTHICK
) ? 0.5 * fscale
:
3187 * Draw border boxes...
3190 for (; rects
> 0; rects
--, margin
+= 2 * fscale
)
3191 if (doc
->number_up
> 1)
3192 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3196 bboxl
- 2 * margin
);
3198 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3200 PageBottom
+ margin
,
3201 PageRight
- PageLeft
- 2 * margin
,
3202 PageTop
- PageBottom
- 2 * margin
);
3205 * Restore pen settings...
3208 doc_puts(doc
, "grestore\n");
3214 * Offset the page by its bounding box...
3217 doc_printf(doc
, "%d %d translate\n", -bounding_box
[0],
3221 if (doc
->fitplot
|| doc
->number_up
> 1)
3224 * Clip the page to the page's bounding box...
3227 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrc\n",
3228 bboxx
+ margin
, bboxy
+ margin
,
3229 bboxw
- 2 * margin
, bboxl
- 2 * margin
);
3235 * 'write_label_prolog()' - Write the prolog with the classification
3240 write_label_prolog(pstops_doc_t
*doc
, /* I - Document info */
3241 const char *label
, /* I - Page label */
3242 float bottom
, /* I - Bottom position in points */
3243 float top
, /* I - Top position in points */
3244 float width
) /* I - Width in points */
3246 const char *classification
; /* CLASSIFICATION environment variable */
3247 const char *ptr
; /* Temporary string pointer */
3251 * First get the current classification...
3254 if ((classification
= getenv("CLASSIFICATION")) == NULL
)
3255 classification
= "";
3256 if (strcmp(classification
, "none") == 0)
3257 classification
= "";
3260 * If there is nothing to show, bind an empty 'write labels' procedure
3264 if (!classification
[0] && (label
== NULL
|| !label
[0]))
3266 doc_puts(doc
, "userdict/ESPwl{}bind put\n");
3271 * Set the classification + page label string...
3274 doc_puts(doc
, "userdict");
3275 if (!strcmp(classification
, "confidential"))
3276 doc_puts(doc
, "/ESPpl(CONFIDENTIAL");
3277 else if (!strcmp(classification
, "classified"))
3278 doc_puts(doc
, "/ESPpl(CLASSIFIED");
3279 else if (!strcmp(classification
, "secret"))
3280 doc_puts(doc
, "/ESPpl(SECRET");
3281 else if (!strcmp(classification
, "topsecret"))
3282 doc_puts(doc
, "/ESPpl(TOP SECRET");
3283 else if (!strcmp(classification
, "unclassified"))
3284 doc_puts(doc
, "/ESPpl(UNCLASSIFIED");
3287 doc_puts(doc
, "/ESPpl(");
3289 for (ptr
= classification
; *ptr
; ptr
++)
3291 if (*ptr
< 32 || *ptr
> 126)
3292 doc_printf(doc
, "\\%03o", *ptr
);
3293 else if (*ptr
== '_')
3295 else if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
3296 doc_printf(doc
, "\\%c", *ptr
);
3298 doc_printf(doc
, "%c", *ptr
);
3304 if (classification
[0])
3305 doc_puts(doc
, " - ");
3308 * Quote the label string as needed...
3311 for (ptr
= label
; *ptr
; ptr
++)
3313 if (*ptr
< 32 || *ptr
> 126)
3314 doc_printf(doc
, "\\%03o", *ptr
);
3315 else if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
3316 doc_printf(doc
, "\\%c", *ptr
);
3318 doc_printf(doc
, "%c", *ptr
);
3322 doc_puts(doc
, ")put\n");
3325 * Then get a 14 point Helvetica-Bold font...
3328 doc_puts(doc
, "userdict/ESPpf /Helvetica-Bold findfont 14 scalefont put\n");
3331 * Finally, the procedure to write the labels on the page...
3334 doc_puts(doc
, "userdict/ESPwl{\n");
3335 doc_puts(doc
, " ESPpf setfont\n");
3336 doc_printf(doc
, " ESPpl stringwidth pop dup 12 add exch -0.5 mul %.0f add\n",
3338 doc_puts(doc
, " 1 setgray\n");
3339 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrf\n", bottom
- 2.0);
3340 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrf\n", top
- 18.0);
3341 doc_puts(doc
, " 0 setgray\n");
3342 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrs\n", bottom
- 2.0);
3343 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrs\n", top
- 18.0);
3344 doc_printf(doc
, " dup %.0f moveto ESPpl show\n", bottom
+ 2.0);
3345 doc_printf(doc
, " %.0f moveto ESPpl show\n", top
- 14.0);
3346 doc_puts(doc
, "pop\n");
3347 doc_puts(doc
, "}bind put\n");
3352 * 'write_labels()' - Write the actual page labels.
3354 * This function is a copy of the one in common.c since we need to
3355 * use doc_puts/doc_printf instead of puts/printf...
3359 write_labels(pstops_doc_t
*doc
, /* I - Document information */
3360 int orient
) /* I - Orientation of the page */
3362 float width
, /* Width of page */
3363 length
; /* Length of page */
3366 doc_puts(doc
, "gsave\n");
3368 if ((orient
^ Orientation
) & 1)
3376 length
= PageLength
;
3381 case 1 : /* Landscape */
3382 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", length
);
3384 case 2 : /* Reverse Portrait */
3385 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", width
, length
);
3387 case 3 : /* Reverse Landscape */
3388 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", width
);
3392 doc_puts(doc
, "ESPwl\n");
3393 doc_puts(doc
, "grestore\n");
3398 * 'write_options()' - Write options provided via %%IncludeFeature.
3403 pstops_doc_t
*doc
, /* I - Document */
3404 ppd_file_t
*ppd
, /* I - PPD file */
3405 int num_options
, /* I - Number of options */
3406 cups_option_t
*options
) /* I - Options */
3408 int i
; /* Looping var */
3409 ppd_option_t
*option
; /* PPD option */
3410 int min_order
; /* Minimum OrderDependency value */
3411 char *doc_setup
, /* DocumentSetup commands to send */
3412 *any_setup
; /* AnySetup commands to send */
3416 * Figure out the minimum OrderDependency value...
3419 if ((option
= ppdFindOption(ppd
, "PageRegion")) != NULL
)
3420 min_order
= option
->order
;
3424 for (i
= 0; i
< num_options
; i
++)
3425 if ((option
= ppdFindOption(ppd
, options
[i
].name
)) != NULL
&&
3426 option
->order
< min_order
)
3427 min_order
= option
->order
;
3430 * Mark and extract them...
3433 cupsMarkOptions(ppd
, num_options
, options
);
3435 doc_setup
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, min_order
);
3436 any_setup
= ppdEmitString(ppd
, PPD_ORDER_ANY
, min_order
);
3439 * Then send them out...
3442 if (doc
->number_up
> 1)
3445 * Temporarily restore setpagedevice so we can set the options...
3448 doc_puts(doc
, "userdict/setpagedevice/CUPSsetpagedevice load put\n");
3453 doc_puts(doc
, doc_setup
);
3459 doc_puts(doc
, any_setup
);
3463 if (doc
->number_up
> 1)
3466 * Disable setpagedevice again...
3469 doc_puts(doc
, "userdict/setpagedevice{pop}bind put\n");
3475 * End of "$Id: pstops.c 7977 2008-09-23 23:44:33Z mike $".