]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/pstops.c
2 * "$Id: pstops.c 7977 2008-09-23 23:44:33Z mike $"
4 * PostScript filter for CUPS.
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 1993-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * main() - Main entry.
20 * add_page() - Add a page to the pages array.
21 * cancel_job() - Flag the job as canceled.
22 * check_range() - Check to see if the current page is selected for
24 * copy_bytes() - Copy bytes from the input file to stdout.
25 * copy_comments() - Copy all of the comments section.
26 * copy_dsc() - Copy a DSC-conforming document.
27 * copy_non_dsc() - Copy a document that does not conform to the DSC.
28 * copy_page() - Copy a page description.
29 * copy_prolog() - Copy the document prolog section.
30 * copy_setup() - Copy the document setup section.
31 * copy_trailer() - Copy the document trailer.
32 * do_prolog() - Send the necessary document prolog commands.
33 * do_setup() - Send the necessary document setup commands.
34 * doc_printf() - Send a formatted string to stdout and/or the temp
36 * doc_puts() - Send a nul-terminated string to stdout and/or the
38 * doc_write() - Send data to stdout and/or the temp file.
39 * end_nup() - End processing for N-up printing.
40 * include_feature() - Include a printer option/feature command.
41 * parse_text() - Parse a text value in a comment.
42 * set_pstops_options() - Set pstops options.
43 * skip_page() - Skip past a page that won't be printed.
44 * start_nup() - Start processing for N-up printing.
45 * write_label_prolog() - Write the prolog with the classification and page
47 * write_labels() - Write the actual page labels.
48 * write_options() - Write options provided via %%IncludeFeature.
52 * Include necessary headers...
58 #include <cups/file.h>
59 #include <cups/array.h>
60 #include <cups/language-private.h>
68 #define PSTOPS_BORDERNONE 0 /* No border or hairline border */
69 #define PSTOPS_BORDERTHICK 1 /* Think border */
70 #define PSTOPS_BORDERSINGLE 2 /* Single-line hairline border */
71 #define PSTOPS_BORDERSINGLE2 3 /* Single-line thick border */
72 #define PSTOPS_BORDERDOUBLE 4 /* Double-line hairline border */
73 #define PSTOPS_BORDERDOUBLE2 5 /* Double-line thick border */
75 #define PSTOPS_LAYOUT_LRBT 0 /* Left to right, bottom to top */
76 #define PSTOPS_LAYOUT_LRTB 1 /* Left to right, top to bottom */
77 #define PSTOPS_LAYOUT_RLBT 2 /* Right to left, bottom to top */
78 #define PSTOPS_LAYOUT_RLTB 3 /* Right to left, top to bottom */
79 #define PSTOPS_LAYOUT_BTLR 4 /* Bottom to top, left to right */
80 #define PSTOPS_LAYOUT_TBLR 5 /* Top to bottom, left to right */
81 #define PSTOPS_LAYOUT_BTRL 6 /* Bottom to top, right to left */
82 #define PSTOPS_LAYOUT_TBRL 7 /* Top to bottom, right to left */
84 #define PSTOPS_LAYOUT_NEGATEY 1 /* The bits for the layout */
85 #define PSTOPS_LAYOUT_NEGATEX 2 /* definitions above... */
86 #define PSTOPS_LAYOUT_VERTICAL 4
93 typedef struct /**** Page information ****/
95 char *label
; /* Page label */
96 int bounding_box
[4]; /* PageBoundingBox */
97 off_t offset
; /* Offset to start of page */
98 ssize_t length
; /* Number of bytes for page */
99 int num_options
; /* Number of options for this page */
100 cups_option_t
*options
; /* Options for this page */
103 typedef struct /**** Document information ****/
105 int page
; /* Current page */
106 int bounding_box
[4]; /* BoundingBox from header */
107 int new_bounding_box
[4]; /* New composite bounding box */
108 int num_options
; /* Number of document-wide options */
109 cups_option_t
*options
; /* Document-wide options */
110 int normal_landscape
, /* Normal rotation for landscape? */
111 saw_eof
, /* Saw the %%EOF comment? */
112 slow_collate
, /* Collate copies by hand? */
113 slow_duplex
, /* Duplex pages slowly? */
114 slow_order
, /* Reverse pages slowly? */
115 use_ESPshowpage
; /* Use ESPshowpage? */
116 cups_array_t
*pages
; /* Pages in document */
117 cups_file_t
*temp
; /* Temporary file, if any */
118 char tempfile
[1024]; /* Temporary filename */
119 int job_id
; /* Job ID */
120 const char *user
, /* User name */
121 *title
; /* Job name */
122 int copies
; /* Number of copies */
123 const char *ap_input_slot
, /* AP_FIRSTPAGE_InputSlot value */
124 *ap_manual_feed
, /* AP_FIRSTPAGE_ManualFeed value */
125 *ap_media_color
, /* AP_FIRSTPAGE_MediaColor value */
126 *ap_media_type
, /* AP_FIRSTPAGE_MediaType value */
127 *ap_page_region
, /* AP_FIRSTPAGE_PageRegion value */
128 *ap_page_size
; /* AP_FIRSTPAGE_PageSize value */
129 float brightness
; /* brightness value */
130 int collate
, /* Collate copies? */
131 emit_jcl
, /* Emit JCL commands? */
132 fitplot
; /* Fit pages to media */
133 float gamma
; /* gamma value */
134 const char *input_slot
, /* InputSlot value */
135 *manual_feed
, /* ManualFeed value */
136 *media_color
, /* MediaColor value */
137 *media_type
, /* MediaType value */
138 *page_region
, /* PageRegion value */
139 *page_size
; /* PageSize value */
140 int mirror
, /* doc->mirror/mirror pages */
141 number_up
, /* Number of pages on each sheet */
142 number_up_layout
, /* doc->number_up_layout of N-up pages */
143 output_order
, /* Requested reverse output order? */
144 page_border
; /* doc->page_border around pages */
145 const char *page_label
, /* page-label option, if any */
146 *page_ranges
, /* page-ranges option, if any */
147 *page_set
; /* page-set option, if any */
152 * Convenience macros...
155 #define is_first_page(p) (doc->number_up == 1 || \
156 ((p) % doc->number_up) == 1)
157 #define is_last_page(p) (doc->number_up == 1 || \
158 ((p) % doc->number_up) == 0)
159 #define is_not_last_page(p) (doc->number_up > 1 && \
160 ((p) % doc->number_up) != 0)
167 static int JobCanceled
= 0;/* Set to 1 on SIGTERM */
174 static pstops_page_t
*add_page(pstops_doc_t
*doc
, const char *label
);
175 static void cancel_job(int sig
);
176 static int check_range(pstops_doc_t
*doc
, int page
);
177 static void copy_bytes(cups_file_t
*fp
, off_t offset
,
179 static ssize_t
copy_comments(cups_file_t
*fp
, pstops_doc_t
*doc
,
180 ppd_file_t
*ppd
, char *line
,
181 ssize_t linelen
, size_t linesize
);
182 static void copy_dsc(cups_file_t
*fp
, pstops_doc_t
*doc
,
183 ppd_file_t
*ppd
, char *line
, ssize_t linelen
,
185 static void copy_non_dsc(cups_file_t
*fp
, pstops_doc_t
*doc
,
186 ppd_file_t
*ppd
, char *line
,
187 ssize_t linelen
, size_t linesize
);
188 static ssize_t
copy_page(cups_file_t
*fp
, pstops_doc_t
*doc
,
189 ppd_file_t
*ppd
, int number
, char *line
,
190 ssize_t linelen
, size_t linesize
);
191 static ssize_t
copy_prolog(cups_file_t
*fp
, pstops_doc_t
*doc
,
192 ppd_file_t
*ppd
, char *line
,
193 ssize_t linelen
, size_t linesize
);
194 static ssize_t
copy_setup(cups_file_t
*fp
, pstops_doc_t
*doc
,
195 ppd_file_t
*ppd
, char *line
,
196 ssize_t linelen
, size_t linesize
);
197 static ssize_t
copy_trailer(cups_file_t
*fp
, pstops_doc_t
*doc
,
198 ppd_file_t
*ppd
, int number
, char *line
,
199 ssize_t linelen
, size_t linesize
);
200 static void do_prolog(pstops_doc_t
*doc
, ppd_file_t
*ppd
);
201 static void do_setup(pstops_doc_t
*doc
, ppd_file_t
*ppd
);
202 static void doc_printf(pstops_doc_t
*doc
, const char *format
, ...)
203 __attribute__ ((__format__ (__printf__
, 2, 3)));
204 static void doc_puts(pstops_doc_t
*doc
, const char *s
);
205 static void doc_write(pstops_doc_t
*doc
, const char *s
, size_t len
);
206 static void end_nup(pstops_doc_t
*doc
, int number
);
207 static int include_feature(ppd_file_t
*ppd
, const char *line
,
209 cups_option_t
**options
);
210 static char *parse_text(const char *start
, char **end
, char *buffer
,
212 static void set_pstops_options(pstops_doc_t
*doc
, ppd_file_t
*ppd
,
213 char *argv
[], int num_options
,
214 cups_option_t
*options
);
215 static ssize_t
skip_page(cups_file_t
*fp
, char *line
, ssize_t linelen
,
217 static void start_nup(pstops_doc_t
*doc
, int number
,
218 int show_border
, const int *bounding_box
);
219 static void write_label_prolog(pstops_doc_t
*doc
, const char *label
,
220 float bottom
, float top
,
222 static void write_labels(pstops_doc_t
*doc
, int orient
);
223 static void write_options(pstops_doc_t
*doc
, ppd_file_t
*ppd
,
224 int num_options
, cups_option_t
*options
);
228 * 'main()' - Main entry.
231 int /* O - Exit status */
232 main(int argc
, /* I - Number of command-line args */
233 char *argv
[]) /* I - Command-line arguments */
235 pstops_doc_t doc
; /* Document information */
236 cups_file_t
*fp
; /* Print file */
237 ppd_file_t
*ppd
; /* PPD file */
238 int num_options
; /* Number of print options */
239 cups_option_t
*options
; /* Print options */
240 char line
[8192]; /* Line buffer */
241 size_t len
; /* Length of line buffer */
242 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
243 struct sigaction action
; /* Actions for POSIX signals */
244 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
248 * Make sure status messages are not buffered...
251 setbuf(stderr
, NULL
);
254 * Ignore broken pipe signals...
257 signal(SIGPIPE
, SIG_IGN
);
260 * Check command-line...
263 if (argc
< 6 || argc
> 7)
265 _cupsLangPrintf(stderr
,
266 _("Usage: %s job-id user title copies options file"),
272 * Register a signal handler to cleanly cancel a job.
275 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
276 sigset(SIGTERM
, cancel_job
);
277 #elif defined(HAVE_SIGACTION)
278 memset(&action
, 0, sizeof(action
));
280 sigemptyset(&action
.sa_mask
);
281 action
.sa_handler
= cancel_job
;
282 sigaction(SIGTERM
, &action
, NULL
);
284 signal(SIGTERM
, cancel_job
);
285 #endif /* HAVE_SIGSET */
288 * If we have 7 arguments, print the file named on the command-line.
289 * Otherwise, send stdin instead...
293 fp
= cupsFileStdin();
297 * Try to open the print file...
300 if ((fp
= cupsFileOpen(argv
[6], "r")) == NULL
)
302 _cupsLangPrintError("ERROR", _("Unable to open print file"));
308 * Read the first line to see if we have DSC comments...
311 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
313 fputs("DEBUG: The print file is empty.\n", stderr
);
318 * Process command-line options...
322 num_options
= cupsParseOptions(argv
[5], 0, &options
);
323 ppd
= SetCommonOptions(num_options
, options
, 1);
325 set_pstops_options(&doc
, ppd
, argv
, num_options
, options
);
328 * Write any "exit server" options that have been selected...
331 ppdEmit(ppd
, stdout
, PPD_ORDER_EXIT
);
334 * Write any JCL commands that are needed to print PostScript code...
338 ppdEmitJCL(ppd
, stdout
, doc
.job_id
, doc
.user
, doc
.title
);
341 * Start with a DSC header...
344 puts("%!PS-Adobe-3.0");
347 * Skip leading PJL in the document...
350 while (!strncmp(line
, "\033%-12345X", 9) || !strncmp(line
, "@PJL ", 5))
353 * Yup, we have leading PJL fun, so skip it until we hit the line
354 * with "ENTER LANGUAGE"...
357 fputs("DEBUG: Skipping PJL header...\n", stderr
);
359 while (strstr(line
, "ENTER LANGUAGE") == NULL
&& strncmp(line
, "%!", 2))
360 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
363 if (!strncmp(line
, "%!", 2))
366 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
371 * Now see if the document conforms to the Adobe Document Structuring
375 if (!strncmp(line
, "%!PS-Adobe-", 11))
378 * Yes, filter the document...
381 copy_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
386 * No, display an error message and treat the file as if it contains
390 copy_non_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
394 * Send %%EOF as needed...
401 * End the job with the appropriate JCL command or CTRL-D...
406 if (ppd
&& ppd
->jcl_end
)
407 ppdEmitJCLEnd(ppd
, stdout
);
413 * Close files and remove the temporary file if needed...
418 cupsFileClose(doc
.temp
);
419 unlink(doc
.tempfile
);
423 cupsFreeOptions(num_options
, options
);
432 * 'add_page()' - Add a page to the pages array.
435 static pstops_page_t
* /* O - New page info object */
436 add_page(pstops_doc_t
*doc
, /* I - Document information */
437 const char *label
) /* I - Page label */
439 pstops_page_t
*pageinfo
; /* New page info object */
443 doc
->pages
= cupsArrayNew(NULL
, NULL
);
447 _cupsLangPrintError("EMERG", _("Unable to allocate memory for pages array"));
451 if ((pageinfo
= calloc(1, sizeof(pstops_page_t
))) == NULL
)
453 _cupsLangPrintError("EMERG", _("Unable to allocate memory for page info"));
457 pageinfo
->label
= strdup(label
);
458 pageinfo
->offset
= cupsFileTell(doc
->temp
);
460 cupsArrayAdd(doc
->pages
, pageinfo
);
469 * 'cancel_job()' - Flag the job as canceled.
473 cancel_job(int sig
) /* I - Signal number (unused) */
482 * 'check_range()' - Check to see if the current page is selected for
486 static int /* O - 1 if selected, 0 otherwise */
487 check_range(pstops_doc_t
*doc
, /* I - Document information */
488 int page
) /* I - Page number */
490 const char *range
; /* Pointer into range string */
491 int lower
, upper
; /* Lower and upper page numbers */
497 * See if we only print even or odd pages...
500 if (!_cups_strcasecmp(doc
->page_set
, "even") && (page
& 1))
503 if (!_cups_strcasecmp(doc
->page_set
, "odd") && !(page
& 1))
507 if (!doc
->page_ranges
)
508 return (1); /* No range, print all pages... */
510 for (range
= doc
->page_ranges
; *range
!= '\0';)
516 upper
= strtol(range
, (char **)&range
, 10);
520 lower
= strtol(range
, (char **)&range
, 10);
525 if (!isdigit(*range
& 255))
528 upper
= strtol(range
, (char **)&range
, 10);
534 if (page
>= lower
&& page
<= upper
)
548 * 'copy_bytes()' - Copy bytes from the input file to stdout.
552 copy_bytes(cups_file_t
*fp
, /* I - File to read from */
553 off_t offset
, /* I - Offset to page data */
554 size_t length
) /* I - Length of page data */
556 char buffer
[8192]; /* Data buffer */
557 ssize_t nbytes
; /* Number of bytes read */
558 size_t nleft
; /* Number of bytes left/remaining */
563 if (cupsFileSeek(fp
, offset
) < 0)
565 _cupsLangPrintError("ERROR", _("Unable to see in file"));
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 */
647 fputs("DEBUG: A duplicate %%Pages: comment was seen.\n", stderr
);
651 if (Duplex
&& (pages
= atoi(line
+ 8)) > 0 && pages
<= doc
->number_up
)
654 * Since we will only be printing on a single page, disable duplexing.
658 doc
->slow_duplex
= 0;
660 if (cupsGetOption("sides", doc
->num_options
, doc
->options
))
661 doc
->num_options
= cupsAddOption("sides", "one-sided",
662 doc
->num_options
, &(doc
->options
));
664 if (cupsGetOption("Duplex", doc
->num_options
, doc
->options
))
665 doc
->num_options
= cupsAddOption("Duplex", "None",
666 doc
->num_options
, &(doc
->options
));
668 if (cupsGetOption("EFDuplex", doc
->num_options
, doc
->options
))
669 doc
->num_options
= cupsAddOption("EFDuplex", "None",
670 doc
->num_options
, &(doc
->options
));
672 if (cupsGetOption("EFDuplexing", doc
->num_options
, doc
->options
))
673 doc
->num_options
= cupsAddOption("EFDuplexing", "False",
674 doc
->num_options
, &(doc
->options
));
676 if (cupsGetOption("KD03Duplex", doc
->num_options
, doc
->options
))
677 doc
->num_options
= cupsAddOption("KD03Duplex", "None",
678 doc
->num_options
, &(doc
->options
));
680 if (cupsGetOption("JCLDuplex", doc
->num_options
, doc
->options
))
681 doc
->num_options
= cupsAddOption("JCLDuplex", "None",
682 doc
->num_options
, &(doc
->options
));
684 ppdMarkOption(ppd
, "Duplex", "None");
685 ppdMarkOption(ppd
, "EFDuplex", "None");
686 ppdMarkOption(ppd
, "EFDuplexing", "False");
687 ppdMarkOption(ppd
, "KD03Duplex", "None");
688 ppdMarkOption(ppd
, "JCLDuplex", "None");
691 else if (!strncmp(line
, "%%BoundingBox:", 14))
693 if (saw_bounding_box
)
694 fputs("DEBUG: A duplicate %%BoundingBox: comment was seen.\n", stderr
);
695 else if (strstr(line
+ 14, "(atend)"))
698 * Do nothing for now but use the default imageable area...
701 else if (sscanf(line
+ 14, "%d%d%d%d", doc
->bounding_box
+ 0,
702 doc
->bounding_box
+ 1, doc
->bounding_box
+ 2,
703 doc
->bounding_box
+ 3) != 4)
705 fputs("DEBUG: A bad %%BoundingBox: comment was seen.\n", stderr
);
707 doc
->bounding_box
[0] = (int)PageLeft
;
708 doc
->bounding_box
[1] = (int)PageBottom
;
709 doc
->bounding_box
[2] = (int)PageRight
;
710 doc
->bounding_box
[3] = (int)PageTop
;
713 saw_bounding_box
= 1;
715 else if (!strncmp(line
, "%%For:", 6))
718 doc_printf(doc
, "%s\n", line
);
720 else if (!strncmp(line
, "%%Title:", 8))
723 doc_printf(doc
, "%s\n", line
);
725 else if (!strncmp(line
, "%cupsRotation:", 14))
728 * Reset orientation of document?
731 int orient
= (atoi(line
+ 14) / 90) & 3;
733 if (orient
!= Orientation
)
736 * Yes, update things so that the pages come out right...
739 Orientation
= (4 - Orientation
+ orient
) & 3;
741 Orientation
= orient
;
744 else if (!strcmp(line
, "%%EndComments"))
746 linelen
= cupsFileGetLine(fp
, line
, linesize
);
749 else if (strncmp(line
, "%!", 2) && strncmp(line
, "%cups", 5))
750 doc_printf(doc
, "%s\n", line
);
752 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
756 if (!saw_bounding_box
)
757 fputs("DEBUG: There wasn't a %%BoundingBox: comment in the header.\n",
761 fputs("DEBUG: There wasn't a %%Pages: comment in the header.\n", stderr
);
764 WriteTextComment("For", doc
->user
);
767 WriteTextComment("Title", doc
->title
);
769 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
772 * Tell the document processor the copy and duplex options
773 * that are required...
776 doc_printf(doc
, "%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
777 doc
->collate
? " collate" : "",
778 Duplex
? " duplex" : "");
781 * Apple uses RBI comments for various non-PPD options...
784 doc_printf(doc
, "%%RBINumCopies: %d\n", doc
->copies
);
789 * Tell the document processor the duplex option that is required...
793 doc_puts(doc
, "%%Requirements: duplex\n");
796 * Apple uses RBI comments for various non-PPD options...
799 doc_puts(doc
, "%RBINumCopies: 1\n");
802 doc_puts(doc
, "%%Pages: (atend)\n");
803 doc_puts(doc
, "%%BoundingBox: (atend)\n");
804 doc_puts(doc
, "%%EndComments\n");
811 * 'copy_dsc()' - Copy a DSC-conforming document.
813 * This function expects "line" to be filled with the %!PS-Adobe comment line.
817 copy_dsc(cups_file_t
*fp
, /* I - File to read from */
818 pstops_doc_t
*doc
, /* I - Document info */
819 ppd_file_t
*ppd
, /* I - PPD file */
820 char *line
, /* I - Line buffer */
821 ssize_t linelen
, /* I - Length of initial line */
822 size_t linesize
) /* I - Size of line buffer */
824 int number
; /* Page number */
825 pstops_page_t
*pageinfo
; /* Page information */
829 * Make sure we use ESPshowpage for EPS files...
832 if (strstr(line
, "EPSF"))
834 doc
->use_ESPshowpage
= 1;
839 * Start sending the document with any commands needed...
842 fprintf(stderr
, "DEBUG: Before copy_comments - %s", line
);
843 linelen
= copy_comments(fp
, doc
, ppd
, line
, linelen
, linesize
);
846 * Now find the prolog section, if any...
849 fprintf(stderr
, "DEBUG: Before copy_prolog - %s", line
);
850 linelen
= copy_prolog(fp
, doc
, ppd
, line
, linelen
, linesize
);
853 * Then the document setup section...
856 fprintf(stderr
, "DEBUG: Before copy_setup - %s", line
);
857 linelen
= copy_setup(fp
, doc
, ppd
, line
, linelen
, linesize
);
860 * Copy until we see %%Page:...
863 while (strncmp(line
, "%%Page:", 7) && strncmp(line
, "%%Trailer", 9))
865 doc_write(doc
, line
, linelen
);
867 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
872 * Then process pages until we have no more...
877 fprintf(stderr
, "DEBUG: Before page loop - %s", line
);
878 while (!strncmp(line
, "%%Page:", 7))
885 if (check_range(doc
, (number
- 1) / doc
->number_up
+ 1))
887 fprintf(stderr
, "DEBUG: Copying page %d...\n", number
);
888 linelen
= copy_page(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
892 fprintf(stderr
, "DEBUG: Skipping page %d...\n", number
);
893 linelen
= skip_page(fp
, line
, linelen
, linesize
);
898 * Finish up the last page(s)...
901 if (number
&& is_not_last_page(number
) && cupsArrayLast(doc
->pages
) &&
902 check_range(doc
, (number
- 1) / doc
->number_up
+ 1))
904 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
906 start_nup(doc
, doc
->number_up
, 0, doc
->bounding_box
);
907 doc_puts(doc
, "showpage\n");
908 end_nup(doc
, doc
->number_up
);
910 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
913 if (doc
->slow_duplex
&& (doc
->page
& 1))
916 * Make sure we have an even number of pages...
919 pageinfo
= add_page(doc
, "(filler)");
921 if (!doc
->slow_order
)
923 if (!ppd
|| !ppd
->num_filters
)
924 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
925 doc
->slow_collate
? 1 : doc
->copies
);
927 printf("%%%%Page: (filler) %d\n", doc
->page
);
930 start_nup(doc
, doc
->number_up
, 0, doc
->bounding_box
);
931 doc_puts(doc
, "showpage\n");
932 end_nup(doc
, doc
->number_up
);
934 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
938 * Make additional copies as necessary...
941 number
= doc
->slow_order
? 0 : doc
->page
;
943 if (doc
->temp
&& !JobCanceled
&& cupsArrayCount(doc
->pages
) > 0)
945 int copy
; /* Current copy */
949 * Reopen the temporary file for reading...
952 cupsFileClose(doc
->temp
);
954 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
960 if (doc
->slow_collate
)
961 copy
= !doc
->slow_order
;
963 copy
= doc
->copies
- 1;
965 for (; copy
< doc
->copies
; copy
++)
971 * Send end-of-job stuff followed by any start-of-job stuff required
972 * for the JCL options...
975 if (number
&& doc
->emit_jcl
&& ppd
&& ppd
->jcl_end
)
978 * Send the trailer...
982 printf("%%%%Pages: %d\n", cupsArrayCount(doc
->pages
));
983 if (doc
->number_up
> 1 || doc
->fitplot
)
984 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
985 PageLeft
, PageBottom
, PageRight
, PageTop
);
987 printf("%%%%BoundingBox: %d %d %d %d\n",
988 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
989 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
993 * Start a new document...
996 ppdEmitJCLEnd(ppd
, stdout
);
997 ppdEmitJCL(ppd
, stdout
, doc
->job_id
, doc
->user
, doc
->title
);
999 puts("%!PS-Adobe-3.0");
1005 * Copy the prolog as needed...
1010 pageinfo
= (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
1011 copy_bytes(doc
->temp
, 0, pageinfo
->offset
);
1015 * Then copy all of the pages...
1018 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayLast(doc
->pages
) :
1019 (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
1028 if (!ppd
|| !ppd
->num_filters
)
1029 fprintf(stderr
, "PAGE: %d %d\n", number
,
1030 doc
->slow_collate
? 1 : doc
->copies
);
1032 if (doc
->number_up
> 1)
1034 printf("%%%%Page: (%d) %d\n", number
, number
);
1035 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1036 PageLeft
, PageBottom
, PageRight
, PageTop
);
1040 printf("%%%%Page: %s %d\n", pageinfo
->label
, number
);
1041 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1042 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
1043 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
1046 copy_bytes(doc
->temp
, pageinfo
->offset
, pageinfo
->length
);
1048 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayPrev(doc
->pages
) :
1049 (pstops_page_t
*)cupsArrayNext(doc
->pages
);
1055 * Restore the old showpage operator as needed...
1058 if (doc
->use_ESPshowpage
)
1059 puts("userdict/showpage/ESPshowpage load put\n");
1062 * Write/copy the trailer...
1066 copy_trailer(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
1071 * 'copy_non_dsc()' - Copy a document that does not conform to the DSC.
1073 * This function expects "line" to be filled with the %! comment line.
1077 copy_non_dsc(cups_file_t
*fp
, /* I - File to read from */
1078 pstops_doc_t
*doc
, /* I - Document info */
1079 ppd_file_t
*ppd
, /* I - PPD file */
1080 char *line
, /* I - Line buffer */
1081 ssize_t linelen
, /* I - Length of initial line */
1082 size_t linesize
) /* I - Size of line buffer */
1084 int copy
; /* Current copy */
1085 char buffer
[8192]; /* Copy buffer */
1086 int bytes
; /* Number of bytes copied */
1090 * First let the user know that they are attempting to print a file
1091 * that may not print correctly...
1094 fputs("DEBUG: This document does not conform to the Adobe Document "
1095 "Structuring Conventions and may not print correctly.\n", stderr
);
1098 * Then write a standard DSC comment section...
1101 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", PageLeft
, PageBottom
,
1102 PageRight
, PageTop
);
1104 if (doc
->slow_collate
&& doc
->copies
> 1)
1105 printf("%%%%Pages: %d\n", doc
->copies
);
1109 WriteTextComment("For", doc
->user
);
1110 WriteTextComment("Title", doc
->title
);
1112 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1115 * Tell the document processor the copy and duplex options
1116 * that are required...
1119 printf("%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
1120 doc
->collate
? " collate" : "",
1121 Duplex
? " duplex" : "");
1124 * Apple uses RBI comments for various non-PPD options...
1127 printf("%%RBINumCopies: %d\n", doc
->copies
);
1132 * Tell the document processor the duplex option that is required...
1136 puts("%%Requirements: duplex");
1139 * Apple uses RBI comments for various non-PPD options...
1142 puts("%RBINumCopies: 1");
1145 puts("%%EndComments");
1148 * Then the prolog...
1151 puts("%%BeginProlog");
1153 do_prolog(doc
, ppd
);
1155 puts("%%EndProlog");
1158 * Then the setup section...
1161 puts("%%BeginSetup");
1168 * Finally, embed a copy of the file inside a %%Page...
1171 if (!ppd
|| !ppd
->num_filters
)
1172 fprintf(stderr
, "PAGE: 1 %d\n", doc
->temp
? 1 : doc
->copies
);
1174 puts("%%Page: 1 1");
1175 puts("%%BeginPageSetup");
1176 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1177 puts("%%EndPageSetup");
1178 puts("%%BeginDocument: nondsc");
1180 fwrite(line
, linelen
, 1, stdout
);
1183 cupsFileWrite(doc
->temp
, line
, linelen
);
1185 while ((bytes
= cupsFileRead(fp
, buffer
, sizeof(buffer
))) > 0)
1187 fwrite(buffer
, 1, bytes
, stdout
);
1190 cupsFileWrite(doc
->temp
, buffer
, bytes
);
1193 puts("%%EndDocument");
1195 if (doc
->use_ESPshowpage
)
1197 WriteLabels(Orientation
);
1198 puts("ESPshowpage");
1201 if (doc
->temp
&& !JobCanceled
)
1204 * Reopen the temporary file for reading...
1207 cupsFileClose(doc
->temp
);
1209 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
1212 * Make the additional copies as needed...
1215 for (copy
= 1; copy
< doc
->copies
; copy
++)
1220 if (!ppd
|| !ppd
->num_filters
)
1221 fputs("PAGE: 1 1\n", stderr
);
1223 printf("%%%%Page: %d %d\n", copy
+ 1, copy
+ 1);
1224 puts("%%BeginPageSetup");
1225 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1226 puts("%%EndPageSetup");
1227 puts("%%BeginDocument: nondsc");
1229 copy_bytes(doc
->temp
, 0, 0);
1231 puts("%%EndDocument");
1233 if (doc
->use_ESPshowpage
)
1235 WriteLabels(Orientation
);
1236 puts("ESPshowpage");
1242 * Restore the old showpage operator as needed...
1245 if (doc
->use_ESPshowpage
)
1246 puts("userdict/showpage/ESPshowpage load put\n");
1251 * 'copy_page()' - Copy a page description.
1253 * This function expects "line" to be filled with a %%Page comment line.
1254 * On return, "line" will contain the next line in the file, if any.
1257 static ssize_t
/* O - Length of next line */
1258 copy_page(cups_file_t
*fp
, /* I - File to read from */
1259 pstops_doc_t
*doc
, /* I - Document info */
1260 ppd_file_t
*ppd
, /* I - PPD file */
1261 int number
, /* I - Current page number */
1262 char *line
, /* I - Line buffer */
1263 ssize_t linelen
, /* I - Length of initial line */
1264 size_t linesize
) /* I - Size of line buffer */
1266 char label
[256], /* Page label string */
1267 *ptr
; /* Pointer into line */
1268 int level
; /* Embedded document level */
1269 pstops_page_t
*pageinfo
; /* Page information */
1270 int first_page
; /* First page on N-up output? */
1271 int has_page_setup
= 0; /* Does the page have %%Begin/EndPageSetup? */
1272 int bounding_box
[4]; /* PageBoundingBox */
1276 * Get the page label for this page...
1279 first_page
= is_first_page(number
);
1281 if (!parse_text(line
+ 7, &ptr
, label
, sizeof(label
)))
1283 fputs("DEBUG: There was a bad %%Page: comment in the file.\n", stderr
);
1287 else if (strtol(ptr
, &ptr
, 10) == LONG_MAX
|| !isspace(*ptr
& 255))
1289 fputs("DEBUG: There was a bad %%Page: comment in the file.\n", stderr
);
1294 * Create or update the current output page...
1298 pageinfo
= add_page(doc
, label
);
1300 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
1303 * Handle first page override...
1306 if (doc
->ap_input_slot
|| doc
->ap_manual_feed
)
1311 * First page/sheet gets AP_FIRSTPAGE_* options...
1314 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->ap_input_slot
,
1315 pageinfo
->num_options
,
1316 &(pageinfo
->options
));
1317 pageinfo
->num_options
= cupsAddOption("ManualFeed",
1318 doc
->ap_input_slot
? "False" :
1319 doc
->ap_manual_feed
,
1320 pageinfo
->num_options
,
1321 &(pageinfo
->options
));
1322 pageinfo
->num_options
= cupsAddOption("MediaColor", doc
->ap_media_color
,
1323 pageinfo
->num_options
,
1324 &(pageinfo
->options
));
1325 pageinfo
->num_options
= cupsAddOption("MediaType", doc
->ap_media_type
,
1326 pageinfo
->num_options
,
1327 &(pageinfo
->options
));
1328 pageinfo
->num_options
= cupsAddOption("PageRegion", doc
->ap_page_region
,
1329 pageinfo
->num_options
,
1330 &(pageinfo
->options
));
1331 pageinfo
->num_options
= cupsAddOption("PageSize", doc
->ap_page_size
,
1332 pageinfo
->num_options
,
1333 &(pageinfo
->options
));
1335 else if (doc
->page
== (Duplex
+ 2))
1338 * Second page/sheet gets default options...
1341 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->input_slot
,
1342 pageinfo
->num_options
,
1343 &(pageinfo
->options
));
1344 pageinfo
->num_options
= cupsAddOption("ManualFeed",
1345 doc
->input_slot
? "False" :
1347 pageinfo
->num_options
,
1348 &(pageinfo
->options
));
1349 pageinfo
->num_options
= cupsAddOption("MediaColor", doc
->media_color
,
1350 pageinfo
->num_options
,
1351 &(pageinfo
->options
));
1352 pageinfo
->num_options
= cupsAddOption("MediaType", doc
->media_type
,
1353 pageinfo
->num_options
,
1354 &(pageinfo
->options
));
1355 pageinfo
->num_options
= cupsAddOption("PageRegion", doc
->page_region
,
1356 pageinfo
->num_options
,
1357 &(pageinfo
->options
));
1358 pageinfo
->num_options
= cupsAddOption("PageSize", doc
->page_size
,
1359 pageinfo
->num_options
,
1360 &(pageinfo
->options
));
1365 * Scan comments until we see something other than %%Page*: or
1369 memcpy(bounding_box
, doc
->bounding_box
, sizeof(bounding_box
));
1371 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1373 if (!strncmp(line
, "%%PageBoundingBox:", 18))
1376 * %%PageBoundingBox: llx lly urx ury
1379 if (sscanf(line
+ 18, "%d%d%d%d", bounding_box
+ 0,
1380 bounding_box
+ 1, bounding_box
+ 2,
1381 bounding_box
+ 3) != 4)
1383 fputs("DEBUG: There was a bad %%PageBoundingBox: comment in the file.\n", stderr
);
1384 memcpy(bounding_box
, doc
->bounding_box
,
1385 sizeof(bounding_box
));
1387 else if (doc
->number_up
== 1 && !doc
->fitplot
&& Orientation
)
1389 int temp_bbox
[4]; /* Temporary bounding box */
1392 memcpy(temp_bbox
, bounding_box
, sizeof(temp_bbox
));
1394 fprintf(stderr
, "DEBUG: Orientation = %d\n", Orientation
);
1395 fprintf(stderr
, "DEBUG: original bounding_box = [ %d %d %d %d ]\n",
1396 bounding_box
[0], bounding_box
[1],
1397 bounding_box
[2], bounding_box
[3]);
1398 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
1399 PageWidth
, PageLength
);
1401 switch (Orientation
)
1403 case 1 : /* Landscape */
1404 bounding_box
[0] = PageLength
- temp_bbox
[3];
1405 bounding_box
[1] = temp_bbox
[0];
1406 bounding_box
[2] = PageLength
- temp_bbox
[1];
1407 bounding_box
[3] = temp_bbox
[2];
1410 case 2 : /* Reverse Portrait */
1411 bounding_box
[0] = PageWidth
- temp_bbox
[2];
1412 bounding_box
[1] = PageLength
- temp_bbox
[3];
1413 bounding_box
[2] = PageWidth
- temp_bbox
[0];
1414 bounding_box
[3] = PageLength
- temp_bbox
[1];
1417 case 3 : /* Reverse Landscape */
1418 bounding_box
[0] = temp_bbox
[1];
1419 bounding_box
[1] = PageWidth
- temp_bbox
[2];
1420 bounding_box
[2] = temp_bbox
[3];
1421 bounding_box
[3] = PageWidth
- temp_bbox
[0];
1425 fprintf(stderr
, "DEBUG: updated bounding_box = [ %d %d %d %d ]\n",
1426 bounding_box
[0], bounding_box
[1],
1427 bounding_box
[2], bounding_box
[3]);
1431 else if (!strncmp(line
, "%%PageCustomColors:", 19) ||
1432 !strncmp(line
, "%%PageMedia:", 12) ||
1433 !strncmp(line
, "%%PageOrientation:", 18) ||
1434 !strncmp(line
, "%%PageProcessColors:", 20) ||
1435 !strncmp(line
, "%%PageRequirements:", 18) ||
1436 !strncmp(line
, "%%PageResources:", 16))
1443 else if (!strncmp(line
, "%%PageCustomColors:", 19))
1446 * %%PageCustomColors: ...
1449 else if (!strncmp(line
, "%%PageMedia:", 12))
1455 else if (!strncmp(line
, "%%PageOrientation:", 18))
1458 * %%PageOrientation: ...
1461 else if (!strncmp(line
, "%%PageProcessColors:", 20))
1464 * %%PageProcessColors: ...
1467 else if (!strncmp(line
, "%%PageRequirements:", 18))
1470 * %%PageRequirements: ...
1473 else if (!strncmp(line
, "%%PageResources:", 16))
1476 * %%PageResources: ...
1479 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1482 * %%IncludeFeature: *MainKeyword OptionKeyword
1485 if (doc
->number_up
== 1 &&!doc
->fitplot
)
1486 pageinfo
->num_options
= include_feature(ppd
, line
,
1487 pageinfo
->num_options
,
1488 &(pageinfo
->options
));
1490 else if (!strncmp(line
, "%%BeginPageSetup", 16))
1499 if (doc
->number_up
== 1)
1502 * Update the document's composite and page bounding box...
1505 memcpy(pageinfo
->bounding_box
, bounding_box
,
1506 sizeof(pageinfo
->bounding_box
));
1508 if (bounding_box
[0] < doc
->new_bounding_box
[0])
1509 doc
->new_bounding_box
[0] = bounding_box
[0];
1510 if (bounding_box
[1] < doc
->new_bounding_box
[1])
1511 doc
->new_bounding_box
[1] = bounding_box
[1];
1512 if (bounding_box
[2] > doc
->new_bounding_box
[2])
1513 doc
->new_bounding_box
[2] = bounding_box
[2];
1514 if (bounding_box
[3] > doc
->new_bounding_box
[3])
1515 doc
->new_bounding_box
[3] = bounding_box
[3];
1519 * Output the page header as needed...
1522 if (!doc
->slow_order
&& first_page
)
1524 if (!ppd
|| !ppd
->num_filters
)
1525 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
1526 doc
->slow_collate
? 1 : doc
->copies
);
1528 if (doc
->number_up
> 1)
1530 printf("%%%%Page: (%d) %d\n", doc
->page
, doc
->page
);
1531 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1532 PageLeft
, PageBottom
, PageRight
, PageTop
);
1536 printf("%%%%Page: %s %d\n", pageinfo
->label
, doc
->page
);
1537 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1538 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
1539 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
1544 * Copy any page setup commands...
1548 doc_puts(doc
, "%%BeginPageSetup\n");
1552 int feature
= 0; /* In a Begin/EndFeature block? */
1554 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1556 if (!strncmp(line
, "%%EndPageSetup", 14))
1558 else if (!strncmp(line
, "%%BeginFeature:", 15))
1562 if (doc
->number_up
> 1 || doc
->fitplot
)
1565 else if (!strncmp(line
, "%%EndFeature", 12))
1569 if (doc
->number_up
> 1 || doc
->fitplot
)
1572 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1574 pageinfo
->num_options
= include_feature(ppd
, line
,
1575 pageinfo
->num_options
,
1576 &(pageinfo
->options
));
1579 else if (!strncmp(line
, "%%Include", 9))
1582 if (line
[0] != '%' && !feature
)
1585 if (!feature
|| (doc
->number_up
== 1 && !doc
->fitplot
))
1586 doc_write(doc
, line
, linelen
);
1590 * Skip %%EndPageSetup...
1593 if (linelen
> 0 && !strncmp(line
, "%%EndPageSetup", 14))
1594 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1599 char *page_setup
; /* PageSetup commands to send */
1602 if (pageinfo
->num_options
> 0)
1603 write_options(doc
, ppd
, pageinfo
->num_options
, pageinfo
->options
);
1606 * Output commands for the current page...
1609 page_setup
= ppdEmitString(ppd
, PPD_ORDER_PAGE
, 0);
1613 doc_puts(doc
, page_setup
);
1619 * Prep for the start of the page description...
1622 start_nup(doc
, number
, 1, bounding_box
);
1625 doc_puts(doc
, "%%EndPageSetup\n");
1628 * Read the rest of the page description...
1636 (!strncmp(line
, "%%Page:", 7) ||
1637 !strncmp(line
, "%%Trailer", 9) ||
1638 !strncmp(line
, "%%EOF", 5)))
1640 else if (!strncmp(line
, "%%BeginDocument", 15) ||
1641 !strncmp(line
, "%ADO_BeginApplication", 21))
1643 doc_write(doc
, line
, linelen
);
1647 else if ((!strncmp(line
, "%%EndDocument", 13) ||
1648 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
1650 doc_write(doc
, line
, linelen
);
1654 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
1655 (!strncmp(line
, "%%BeginData:", 12) &&
1656 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
1659 * Copy binary data...
1662 int bytes
; /* Bytes of data */
1665 doc_write(doc
, line
, linelen
);
1667 bytes
= atoi(strchr(line
, ':') + 1);
1671 if (bytes
> linesize
)
1672 linelen
= cupsFileRead(fp
, line
, linesize
);
1674 linelen
= cupsFileRead(fp
, line
, bytes
);
1679 perror("ERROR: Early end-of-file while reading binary data");
1683 doc_write(doc
, line
, linelen
);
1689 doc_write(doc
, line
, linelen
);
1691 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0);
1694 * Finish up this page and return...
1697 end_nup(doc
, number
);
1699 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
1706 * 'copy_prolog()' - Copy the document prolog section.
1708 * This function expects "line" to be filled with a %%BeginProlog comment line.
1709 * On return, "line" will contain the next line in the file, if any.
1712 static ssize_t
/* O - Length of next line */
1713 copy_prolog(cups_file_t
*fp
, /* I - File to read from */
1714 pstops_doc_t
*doc
, /* I - Document info */
1715 ppd_file_t
*ppd
, /* I - PPD file */
1716 char *line
, /* I - Line buffer */
1717 ssize_t linelen
, /* I - Length of initial line */
1718 size_t linesize
) /* I - Size of line buffer */
1720 while (strncmp(line
, "%%BeginProlog", 13))
1722 if (!strncmp(line
, "%%BeginSetup", 12) || !strncmp(line
, "%%Page:", 7))
1725 doc_write(doc
, line
, linelen
);
1727 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1731 doc_puts(doc
, "%%BeginProlog\n");
1733 do_prolog(doc
, ppd
);
1735 if (!strncmp(line
, "%%BeginProlog", 13))
1737 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1739 if (!strncmp(line
, "%%EndProlog", 11) ||
1740 !strncmp(line
, "%%BeginSetup", 12) ||
1741 !strncmp(line
, "%%Page:", 7))
1744 doc_write(doc
, line
, linelen
);
1747 if (!strncmp(line
, "%%EndProlog", 11))
1748 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1750 fputs("DEBUG: The %%EndProlog comment is missing.\n", stderr
);
1753 doc_puts(doc
, "%%EndProlog\n");
1760 * 'copy_setup()' - Copy the document setup section.
1762 * This function expects "line" to be filled with a %%BeginSetup comment line.
1763 * On return, "line" will contain the next line in the file, if any.
1766 static ssize_t
/* O - Length of next line */
1767 copy_setup(cups_file_t
*fp
, /* I - File to read from */
1768 pstops_doc_t
*doc
, /* I - Document info */
1769 ppd_file_t
*ppd
, /* I - PPD file */
1770 char *line
, /* I - Line buffer */
1771 ssize_t linelen
, /* I - Length of initial line */
1772 size_t linesize
) /* I - Size of line buffer */
1774 int num_options
; /* Number of options */
1775 cups_option_t
*options
; /* Options */
1778 while (strncmp(line
, "%%BeginSetup", 12))
1780 if (!strncmp(line
, "%%Page:", 7))
1783 doc_write(doc
, line
, linelen
);
1785 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1789 doc_puts(doc
, "%%BeginSetup\n");
1796 if (!strncmp(line
, "%%BeginSetup", 12))
1798 while (strncmp(line
, "%%EndSetup", 10))
1800 if (!strncmp(line
, "%%Page:", 7))
1802 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1805 * %%IncludeFeature: *MainKeyword OptionKeyword
1808 if (doc
->number_up
== 1 && !doc
->fitplot
)
1809 num_options
= include_feature(ppd
, line
, num_options
, &options
);
1811 else if (strncmp(line
, "%%BeginSetup", 12))
1812 doc_write(doc
, line
, linelen
);
1814 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1818 if (!strncmp(line
, "%%EndSetup", 10))
1819 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1821 fputs("DEBUG: The %%EndSetup comment is missing.\n", stderr
);
1824 if (num_options
> 0)
1826 write_options(doc
, ppd
, num_options
, options
);
1827 cupsFreeOptions(num_options
, options
);
1830 doc_puts(doc
, "%%EndSetup\n");
1837 * 'copy_trailer()' - Copy the document trailer.
1839 * This function expects "line" to be filled with a %%Trailer comment line.
1840 * On return, "line" will contain the next line in the file, if any.
1843 static ssize_t
/* O - Length of next line */
1844 copy_trailer(cups_file_t
*fp
, /* I - File to read from */
1845 pstops_doc_t
*doc
, /* I - Document info */
1846 ppd_file_t
*ppd
, /* I - PPD file */
1847 int number
, /* I - Number of pages */
1848 char *line
, /* I - Line buffer */
1849 ssize_t linelen
, /* I - Length of initial line */
1850 size_t linesize
) /* I - Size of line buffer */
1853 * Write the trailer comments...
1860 if (!strncmp(line
, "%%EOF", 5))
1862 else if (strncmp(line
, "%%Trailer", 9) &&
1863 strncmp(line
, "%%Pages:", 8) &&
1864 strncmp(line
, "%%BoundingBox:", 14))
1865 fwrite(line
, 1, linelen
, stdout
);
1867 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1870 fprintf(stderr
, "DEBUG: Wrote %d pages...\n", number
);
1872 printf("%%%%Pages: %d\n", number
);
1873 if (doc
->number_up
> 1 || doc
->fitplot
)
1874 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
1875 PageLeft
, PageBottom
, PageRight
, PageTop
);
1877 printf("%%%%BoundingBox: %d %d %d %d\n",
1878 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
1879 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
1886 * 'do_prolog()' - Send the necessary document prolog commands.
1890 do_prolog(pstops_doc_t
*doc
, /* I - Document information */
1891 ppd_file_t
*ppd
) /* I - PPD file */
1893 char *ps
; /* PS commands */
1897 * Send the document prolog commands...
1900 if (ppd
&& ppd
->patches
)
1902 doc_puts(doc
, "%%BeginFeature: *JobPatchFile 1\n");
1903 doc_puts(doc
, ppd
->patches
);
1904 doc_puts(doc
, "\n%%EndFeature\n");
1907 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_PROLOG
, 0.0)) != NULL
)
1914 * Define ESPshowpage here so that applications that define their
1915 * own procedure to do a showpage pick it up...
1918 if (doc
->use_ESPshowpage
)
1919 doc_puts(doc
, "userdict/ESPshowpage/showpage load put\n"
1920 "userdict/showpage{}put\n");
1925 * 'do_setup()' - Send the necessary document setup commands.
1929 do_setup(pstops_doc_t
*doc
, /* I - Document information */
1930 ppd_file_t
*ppd
) /* I - PPD file */
1932 char *ps
; /* PS commands */
1936 * Disable CTRL-D so that embedded files don't cause printing
1940 doc_puts(doc
, "% Disable CTRL-D as an end-of-file marker...\n");
1941 doc_puts(doc
, "userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put\n");
1944 * Mark job options...
1947 cupsMarkOptions(ppd
, doc
->num_options
, doc
->options
);
1950 * Send all the printer-specific setup commands...
1953 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, 0.0)) != NULL
)
1959 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_ANY
, 0.0)) != NULL
)
1966 * Set the number of copies for the job...
1969 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1971 doc_printf(doc
, "%%RBIBeginNonPPDFeature: *NumCopies %d\n", doc
->copies
);
1973 "%d/languagelevel where{pop languagelevel 2 ge}{false}ifelse\n"
1974 "{1 dict begin/NumCopies exch def currentdict end "
1976 "{userdict/#copies 3 -1 roll put}ifelse\n", doc
->copies
);
1977 doc_puts(doc
, "%RBIEndNonPPDFeature\n");
1981 * If we are doing N-up printing, disable setpagedevice...
1984 if (doc
->number_up
> 1)
1986 doc_puts(doc
, "userdict/CUPSsetpagedevice/setpagedevice load put\n");
1987 doc_puts(doc
, "userdict/setpagedevice{pop}bind put\n");
1991 * Changes to the transfer function must be made AFTER any
1992 * setpagedevice code...
1995 if (doc
->gamma
!= 1.0f
|| doc
->brightness
!= 1.0f
)
1996 doc_printf(doc
, "{ neg 1 add dup 0 lt { pop 1 } { %.3f exp neg 1 add } "
1997 "ifelse %.3f mul } bind settransfer\n",
1998 doc
->gamma
, doc
->brightness
);
2001 * Make sure we have rectclip and rectstroke procedures of some sort...
2005 "% x y w h ESPrc - Clip to a rectangle.\n"
2006 "userdict/ESPrc/rectclip where{pop/rectclip load}\n"
2007 "{{newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
2008 "neg 0 rlineto closepath clip newpath}bind}ifelse put\n");
2011 "% x y w h ESPrf - Fill a rectangle.\n"
2012 "userdict/ESPrf/rectfill where{pop/rectfill load}\n"
2013 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
2014 "neg 0 rlineto closepath fill grestore}bind}ifelse put\n");
2017 "% x y w h ESPrs - Stroke a rectangle.\n"
2018 "userdict/ESPrs/rectstroke where{pop/rectstroke load}\n"
2019 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
2020 "neg 0 rlineto closepath stroke grestore}bind}ifelse put\n");
2023 * Write the page and label prologs...
2026 if (doc
->number_up
== 2 || doc
->number_up
== 6)
2029 * For 2- and 6-up output, rotate the labels to match the orientation
2033 if (Orientation
& 1)
2034 write_label_prolog(doc
, doc
->page_label
, PageBottom
,
2035 PageWidth
- PageLength
+ PageTop
, PageLength
);
2037 write_label_prolog(doc
, doc
->page_label
, PageLeft
, PageRight
,
2041 write_label_prolog(doc
, doc
->page_label
, PageBottom
, PageTop
, PageWidth
);
2046 * 'doc_printf()' - Send a formatted string to stdout and/or the temp file.
2048 * This function should be used for all page-level output that is affected
2049 * by ordering, collation, etc.
2053 doc_printf(pstops_doc_t
*doc
, /* I - Document information */
2054 const char *format
, /* I - Printf-style format string */
2055 ...) /* I - Additional arguments as needed */
2057 va_list ap
; /* Pointer to arguments */
2058 char buffer
[1024]; /* Output buffer */
2059 size_t bytes
; /* Number of bytes to write */
2062 va_start(ap
, format
);
2063 bytes
= vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
2066 if (bytes
> sizeof(buffer
))
2068 _cupsLangPrintFilter(stderr
, "ERROR",
2069 _("Buffer overflow detected, aborting."));
2073 doc_write(doc
, buffer
, bytes
);
2078 * 'doc_puts()' - Send a nul-terminated string to stdout and/or the temp file.
2080 * This function should be used for all page-level output that is affected
2081 * by ordering, collation, etc.
2085 doc_puts(pstops_doc_t
*doc
, /* I - Document information */
2086 const char *s
) /* I - String to send */
2088 doc_write(doc
, s
, strlen(s
));
2093 * 'doc_write()' - Send data to stdout and/or the temp file.
2097 doc_write(pstops_doc_t
*doc
, /* I - Document information */
2098 const char *s
, /* I - Data to send */
2099 size_t len
) /* I - Number of bytes to send */
2101 if (!doc
->slow_order
)
2102 fwrite(s
, 1, len
, stdout
);
2105 cupsFileWrite(doc
->temp
, s
, len
);
2110 * 'end_nup()' - End processing for N-up printing.
2114 end_nup(pstops_doc_t
*doc
, /* I - Document information */
2115 int number
) /* I - Page number */
2117 if (doc
->number_up
> 1)
2118 doc_puts(doc
, "userdict/ESPsave get restore\n");
2120 switch (doc
->number_up
)
2123 if (doc
->use_ESPshowpage
)
2125 write_labels(doc
, Orientation
);
2126 doc_puts(doc
, "ESPshowpage\n");
2132 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2134 if (Orientation
& 1)
2137 * Rotate the labels back to portrait...
2140 write_labels(doc
, Orientation
- 1);
2142 else if (Orientation
== 0)
2145 * Rotate the labels to landscape...
2148 write_labels(doc
, doc
->normal_landscape
? 1 : 3);
2153 * Rotate the labels to landscape...
2156 write_labels(doc
, doc
->normal_landscape
? 3 : 1);
2159 doc_puts(doc
, "ESPshowpage\n");
2164 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2166 write_labels(doc
, Orientation
);
2167 doc_puts(doc
, "ESPshowpage\n");
2177 * 'include_feature()' - Include a printer option/feature command.
2180 static int /* O - New number of options */
2182 ppd_file_t
*ppd
, /* I - PPD file */
2183 const char *line
, /* I - DSC line */
2184 int num_options
, /* I - Number of options */
2185 cups_option_t
**options
) /* IO - Options */
2187 char name
[255], /* Option name */
2188 value
[255]; /* Option value */
2189 ppd_option_t
*option
; /* Option in file */
2193 * Get the "%%IncludeFeature: *Keyword OptionKeyword" values...
2196 if (sscanf(line
+ 17, "%254s%254s", name
, value
) != 2)
2198 fputs("DEBUG: The %%IncludeFeature: comment is not valid.\n", stderr
);
2199 return (num_options
);
2203 * Find the option and choice...
2206 if ((option
= ppdFindOption(ppd
, name
+ 1)) == NULL
)
2208 _cupsLangPrintFilter(stderr
, "WARNING", _("Unknown option \"%s\"."),
2210 return (num_options
);
2213 if (option
->section
== PPD_ORDER_EXIT
||
2214 option
->section
== PPD_ORDER_JCL
)
2216 _cupsLangPrintFilter(stderr
, "WARNING",
2217 _("Option \"%s\" cannot be included via "
2218 "%%%%IncludeFeature."), name
+ 1);
2219 return (num_options
);
2222 if (!ppdFindChoice(option
, value
))
2224 _cupsLangPrintFilter(stderr
, "WARNING",
2225 _("Unknown choice \"%s\" for option \"%s\"."),
2227 return (num_options
);
2231 * Add the option to the option array and return...
2234 return (cupsAddOption(name
+ 1, value
, num_options
, options
));
2239 * 'parse_text()' - Parse a text value in a comment.
2241 * This function parses a DSC text value as defined on page 36 of the
2242 * DSC specification. Text values are either surrounded by parenthesis
2243 * or whitespace-delimited.
2245 * The value returned is the literal characters for the entire text
2246 * string, including any parenthesis and escape characters.
2249 static char * /* O - Value or NULL on error */
2250 parse_text(const char *start
, /* I - Start of text value */
2251 char **end
, /* O - End of text value */
2252 char *buffer
, /* I - Buffer */
2253 size_t bufsize
) /* I - Size of buffer */
2255 char *bufptr
, /* Pointer in buffer */
2256 *bufend
; /* End of buffer */
2257 int level
; /* Parenthesis level */
2261 * Skip leading whitespace...
2264 while (isspace(*start
& 255))
2268 * Then copy the value...
2273 bufend
= buffer
+ bufsize
- 1;
2275 while (bufptr
< bufend
)
2277 if (isspace(*start
& 255) && !level
)
2284 else if (*start
== ')')
2294 else if (*start
== '\\')
2297 * Copy escaped character...
2300 int i
; /* Looping var */
2304 i
<= 3 && isdigit(start
[i
] & 255) && bufptr
< bufend
;
2305 *bufptr
++ = start
[i
], i
++);
2314 * Return the value and new pointer into the line...
2318 *end
= (char *)start
;
2320 if (bufptr
== bufend
)
2328 * 'set_pstops_options()' - Set pstops options.
2333 pstops_doc_t
*doc
, /* I - Document information */
2334 ppd_file_t
*ppd
, /* I - PPD file */
2335 char *argv
[], /* I - Command-line arguments */
2336 int num_options
, /* I - Number of options */
2337 cups_option_t
*options
) /* I - Options */
2339 const char *val
; /* Option value */
2340 int intval
; /* Integer option value */
2341 ppd_attr_t
*attr
; /* PPD attribute */
2342 ppd_option_t
*option
; /* PPD option */
2343 ppd_choice_t
*choice
; /* PPD choice */
2344 const char *content_type
; /* Original content type */
2348 * Initialize document information structure...
2351 memset(doc
, 0, sizeof(pstops_doc_t
));
2353 doc
->job_id
= atoi(argv
[1]);
2354 doc
->user
= argv
[2];
2355 doc
->title
= argv
[3];
2356 doc
->copies
= atoi(argv
[4]);
2358 if (ppd
&& ppd
->landscape
> 0)
2359 doc
->normal_landscape
= 1;
2361 doc
->bounding_box
[0] = (int)PageLeft
;
2362 doc
->bounding_box
[1] = (int)PageBottom
;
2363 doc
->bounding_box
[2] = (int)PageRight
;
2364 doc
->bounding_box
[3] = (int)PageTop
;
2366 doc
->new_bounding_box
[0] = INT_MAX
;
2367 doc
->new_bounding_box
[1] = INT_MAX
;
2368 doc
->new_bounding_box
[2] = INT_MIN
;
2369 doc
->new_bounding_box
[3] = INT_MIN
;
2372 * AP_FIRSTPAGE_* and the corresponding non-first-page options.
2375 doc
->ap_input_slot
= cupsGetOption("AP_FIRSTPAGE_InputSlot", num_options
,
2377 doc
->ap_manual_feed
= cupsGetOption("AP_FIRSTPAGE_ManualFeed", num_options
,
2379 doc
->ap_media_color
= cupsGetOption("AP_FIRSTPAGE_MediaColor", num_options
,
2381 doc
->ap_media_type
= cupsGetOption("AP_FIRSTPAGE_MediaType", num_options
,
2383 doc
->ap_page_region
= cupsGetOption("AP_FIRSTPAGE_PageRegion", num_options
,
2385 doc
->ap_page_size
= cupsGetOption("AP_FIRSTPAGE_PageSize", num_options
,
2388 if ((choice
= ppdFindMarkedChoice(ppd
, "InputSlot")) != NULL
)
2389 doc
->input_slot
= choice
->choice
;
2390 if ((choice
= ppdFindMarkedChoice(ppd
, "ManualFeed")) != NULL
)
2391 doc
->manual_feed
= choice
->choice
;
2392 if ((choice
= ppdFindMarkedChoice(ppd
, "MediaColor")) != NULL
)
2393 doc
->media_color
= choice
->choice
;
2394 if ((choice
= ppdFindMarkedChoice(ppd
, "MediaType")) != NULL
)
2395 doc
->media_type
= choice
->choice
;
2396 if ((choice
= ppdFindMarkedChoice(ppd
, "PageRegion")) != NULL
)
2397 doc
->page_region
= choice
->choice
;
2398 if ((choice
= ppdFindMarkedChoice(ppd
, "PageSize")) != NULL
)
2399 doc
->page_size
= choice
->choice
;
2405 if ((val
= cupsGetOption("brightness", num_options
, options
)) != NULL
)
2408 * Get brightness value from 10 to 1000.
2413 if (intval
< 10 || intval
> 1000)
2415 _cupsLangPrintFilter(stderr
, "ERROR",
2416 _("Unsupported brightness value %s, using "
2417 "brightness=100."), val
);
2418 doc
->brightness
= 1.0f
;
2421 doc
->brightness
= intval
* 0.01f
;
2424 doc
->brightness
= 1.0f
;
2427 * collate, multiple-document-handling
2430 if ((val
= cupsGetOption("multiple-document-handling", num_options
, options
)) != NULL
)
2433 * This IPP attribute is unnecessarily complicated...
2435 * single-document, separate-documents-collated-copies, and
2436 * single-document-new-sheet all require collated copies.
2438 * separate-documents-uncollated-copies allows for uncollated copies.
2441 doc
->collate
= _cups_strcasecmp(val
, "separate-documents-uncollated-copies") != 0;
2444 if ((val
= cupsGetOption("Collate", num_options
, options
)) != NULL
&&
2445 (!_cups_strcasecmp(val
, "true") ||!_cups_strcasecmp(val
, "on") ||
2446 !_cups_strcasecmp(val
, "yes")))
2453 if ((val
= cupsGetOption("emit-jcl", num_options
, options
)) != NULL
&&
2454 (!_cups_strcasecmp(val
, "false") || !_cups_strcasecmp(val
, "off") ||
2455 !_cups_strcasecmp(val
, "no") || !strcmp(val
, "0")))
2461 * fitplot/fit-to-page/ipp-attribute-fidelity
2463 * (Only for original PostScript content)
2466 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
2467 content_type
= "application/postscript";
2469 if (!_cups_strcasecmp(content_type
, "application/postscript"))
2471 if ((val
= cupsGetOption("fitplot", num_options
, options
)) != NULL
&&
2472 !_cups_strcasecmp(val
, "true"))
2474 else if ((val
= cupsGetOption("fit-to-page", num_options
, options
)) != NULL
&&
2475 !_cups_strcasecmp(val
, "true"))
2477 else if ((val
= cupsGetOption("ipp-attribute-fidelity", num_options
,
2478 options
)) != NULL
&&
2479 !_cups_strcasecmp(val
, "true"))
2487 if ((val
= cupsGetOption("gamma", num_options
, options
)) != NULL
)
2490 * Get gamma value from 1 to 10000...
2495 if (intval
< 1 || intval
> 10000)
2497 _cupsLangPrintFilter(stderr
, "ERROR",
2498 _("Unsupported gamma value %s, using gamma=1000."),
2503 doc
->gamma
= intval
* 0.001f
;
2509 * mirror/MirrorPrint
2512 if ((choice
= ppdFindMarkedChoice(ppd
, "MirrorPrint")) != NULL
)
2514 val
= choice
->choice
;
2518 val
= cupsGetOption("mirror", num_options
, options
);
2520 if (val
&& (!_cups_strcasecmp(val
, "true") || !_cups_strcasecmp(val
, "on") ||
2521 !_cups_strcasecmp(val
, "yes")))
2528 if ((val
= cupsGetOption("number-up", num_options
, options
)) != NULL
)
2530 switch (intval
= atoi(val
))
2538 doc
->number_up
= intval
;
2541 _cupsLangPrintFilter(stderr
, "ERROR",
2542 _("Unsupported number-up value %d, using "
2543 "number-up=1."), intval
);
2555 if ((val
= cupsGetOption("number-up-layout", num_options
, options
)) != NULL
)
2557 if (!_cups_strcasecmp(val
, "lrtb"))
2558 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2559 else if (!_cups_strcasecmp(val
, "lrbt"))
2560 doc
->number_up_layout
= PSTOPS_LAYOUT_LRBT
;
2561 else if (!_cups_strcasecmp(val
, "rltb"))
2562 doc
->number_up_layout
= PSTOPS_LAYOUT_RLTB
;
2563 else if (!_cups_strcasecmp(val
, "rlbt"))
2564 doc
->number_up_layout
= PSTOPS_LAYOUT_RLBT
;
2565 else if (!_cups_strcasecmp(val
, "tblr"))
2566 doc
->number_up_layout
= PSTOPS_LAYOUT_TBLR
;
2567 else if (!_cups_strcasecmp(val
, "tbrl"))
2568 doc
->number_up_layout
= PSTOPS_LAYOUT_TBRL
;
2569 else if (!_cups_strcasecmp(val
, "btlr"))
2570 doc
->number_up_layout
= PSTOPS_LAYOUT_BTLR
;
2571 else if (!_cups_strcasecmp(val
, "btrl"))
2572 doc
->number_up_layout
= PSTOPS_LAYOUT_BTRL
;
2575 _cupsLangPrintFilter(stderr
, "ERROR",
2576 _("Unsupported number-up-layout value %s, using "
2577 "number-up-layout=lrtb."), val
);
2578 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2582 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2588 if ((val
= cupsGetOption("OutputOrder", num_options
, options
)) != NULL
)
2590 if (!_cups_strcasecmp(val
, "Reverse"))
2591 doc
->output_order
= 1;
2596 * Figure out the right default output order from the PPD file...
2599 if ((choice
= ppdFindMarkedChoice(ppd
, "OutputBin")) != NULL
&&
2600 (attr
= ppdFindAttr(ppd
, "PageStackOrder", choice
->choice
)) != NULL
&&
2602 doc
->output_order
= !_cups_strcasecmp(attr
->value
, "Reverse");
2603 else if ((attr
= ppdFindAttr(ppd
, "DefaultOutputOrder", NULL
)) != NULL
&&
2605 doc
->output_order
= !_cups_strcasecmp(attr
->value
, "Reverse");
2612 if ((val
= cupsGetOption("page-border", num_options
, options
)) != NULL
)
2614 if (!_cups_strcasecmp(val
, "none"))
2615 doc
->page_border
= PSTOPS_BORDERNONE
;
2616 else if (!_cups_strcasecmp(val
, "single"))
2617 doc
->page_border
= PSTOPS_BORDERSINGLE
;
2618 else if (!_cups_strcasecmp(val
, "single-thick"))
2619 doc
->page_border
= PSTOPS_BORDERSINGLE2
;
2620 else if (!_cups_strcasecmp(val
, "double"))
2621 doc
->page_border
= PSTOPS_BORDERDOUBLE
;
2622 else if (!_cups_strcasecmp(val
, "double-thick"))
2623 doc
->page_border
= PSTOPS_BORDERDOUBLE2
;
2626 _cupsLangPrintFilter(stderr
, "ERROR",
2627 _("Unsupported page-border value %s, using "
2628 "page-border=none."), val
);
2629 doc
->page_border
= PSTOPS_BORDERNONE
;
2633 doc
->page_border
= PSTOPS_BORDERNONE
;
2639 doc
->page_label
= cupsGetOption("page-label", num_options
, options
);
2645 doc
->page_ranges
= cupsGetOption("page-ranges", num_options
, options
);
2651 doc
->page_set
= cupsGetOption("page-set", num_options
, options
);
2654 * Now figure out if we have to force collated copies, etc.
2657 if (ppd
&& ppd
->manual_copies
&& Duplex
&& doc
->copies
> 1)
2660 * Force collated copies when printing a duplexed document to
2661 * a non-PS printer that doesn't do hardware copy generation.
2662 * Otherwise the copies will end up on the front/back side of
2670 * See if we have to filter the fast or slow way...
2673 if (doc
->collate
&& doc
->copies
> 1)
2676 * See if we need to manually collate the pages...
2679 doc
->slow_collate
= 1;
2681 if ((choice
= ppdFindMarkedChoice(ppd
, "Collate")) != NULL
&&
2682 !_cups_strcasecmp(choice
->choice
, "True"))
2685 * Hardware collate option is selected, see if the option is
2686 * conflicting - if not, collate in hardware. Otherwise,
2687 * turn the hardware collate option off...
2690 if ((option
= ppdFindOption(ppd
, "Collate")) != NULL
&&
2691 !option
->conflicted
)
2692 doc
->slow_collate
= 0;
2694 ppdMarkOption(ppd
, "Collate", "False");
2698 doc
->slow_collate
= 0;
2700 if (!ppdFindOption(ppd
, "OutputOrder") && doc
->output_order
)
2701 doc
->slow_order
= 1;
2703 doc
->slow_order
= 0;
2706 (doc
->slow_collate
|| doc
->slow_order
||
2707 ((attr
= ppdFindAttr(ppd
, "cupsEvenDuplex", NULL
)) != NULL
&&
2708 attr
->value
&& !_cups_strcasecmp(attr
->value
, "true"))))
2709 doc
->slow_duplex
= 1;
2711 doc
->slow_duplex
= 0;
2714 * Create a temporary file for page data if we need to filter slowly...
2717 if (doc
->slow_order
|| doc
->slow_collate
)
2719 if ((doc
->temp
= cupsTempFile2(doc
->tempfile
,
2720 sizeof(doc
->tempfile
))) == NULL
)
2722 perror("DEBUG: Unable to create temporary file");
2728 * Figure out if we should use ESPshowpage or not...
2731 if (doc
->page_label
|| getenv("CLASSIFICATION") || doc
->number_up
> 1 ||
2735 * Yes, use ESPshowpage...
2738 doc
->use_ESPshowpage
= 1;
2741 fprintf(stderr
, "DEBUG: slow_collate=%d, slow_duplex=%d, slow_order=%d\n",
2742 doc
->slow_collate
, doc
->slow_duplex
, doc
->slow_order
);
2747 * 'skip_page()' - Skip past a page that won't be printed.
2750 static ssize_t
/* O - Length of next line */
2751 skip_page(cups_file_t
*fp
, /* I - File to read from */
2752 char *line
, /* I - Line buffer */
2753 ssize_t linelen
, /* I - Length of initial line */
2754 size_t linesize
) /* I - Size of line buffer */
2756 int level
; /* Embedded document level */
2761 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
2764 (!strncmp(line
, "%%Page:", 7) || !strncmp(line
, "%%Trailer", 9)))
2766 else if (!strncmp(line
, "%%BeginDocument", 15) ||
2767 !strncmp(line
, "%ADO_BeginApplication", 21))
2769 else if ((!strncmp(line
, "%%EndDocument", 13) ||
2770 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
2772 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
2773 (!strncmp(line
, "%%BeginData:", 12) &&
2774 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
2777 * Skip binary data...
2780 int bytes
; /* Bytes of data */
2783 bytes
= atoi(strchr(line
, ':') + 1);
2787 if (bytes
> linesize
)
2788 linelen
= cupsFileRead(fp
, line
, linesize
);
2790 linelen
= cupsFileRead(fp
, line
, bytes
);
2795 perror("ERROR: Early end-of-file while reading binary data");
2809 * 'start_nup()' - Start processing for N-up printing.
2813 start_nup(pstops_doc_t
*doc
, /* I - Document information */
2814 int number
, /* I - Page number */
2815 int show_border
, /* I - Show the border? */
2816 const int *bounding_box
) /* I - BoundingBox value */
2818 int pos
; /* Position on page */
2819 int x
, y
; /* Relative position of subpage */
2820 float w
, l
, /* Width and length of subpage */
2821 tx
, ty
; /* Translation values for subpage */
2822 float pagew
, /* Printable width of page */
2823 pagel
; /* Printable height of page */
2824 int bboxx
, /* BoundingBox X origin */
2825 bboxy
, /* BoundingBox Y origin */
2826 bboxw
, /* BoundingBox width */
2827 bboxl
; /* BoundingBox height */
2828 float margin
= 0; /* Current margin for border */
2831 if (doc
->number_up
> 1)
2832 doc_puts(doc
, "userdict/ESPsave save put\n");
2834 pos
= (number
- 1) % doc
->number_up
;
2835 pagew
= PageRight
- PageLeft
;
2836 pagel
= PageTop
- PageBottom
;
2840 bboxx
= bounding_box
[0];
2841 bboxy
= bounding_box
[1];
2842 bboxw
= bounding_box
[2] - bounding_box
[0];
2843 bboxl
= bounding_box
[3] - bounding_box
[1];
2853 fprintf(stderr
, "DEBUG: pagew = %.1f, pagel = %.1f\n", pagew
, pagel
);
2854 fprintf(stderr
, "DEBUG: bboxx = %d, bboxy = %d, bboxw = %d, bboxl = %d\n",
2855 bboxx
, bboxy
, bboxw
, bboxl
);
2856 fprintf(stderr
, "DEBUG: PageLeft = %.1f, PageRight = %.1f\n",
2857 PageLeft
, PageRight
);
2858 fprintf(stderr
, "DEBUG: PageTop = %.1f, PageBottom = %.1f\n",
2859 PageTop
, PageBottom
);
2860 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
2861 PageWidth
, PageLength
);
2863 switch (Orientation
)
2865 case 1 : /* Landscape */
2866 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", PageLength
);
2868 case 2 : /* Reverse Portrait */
2869 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", PageWidth
,
2872 case 3 : /* Reverse Landscape */
2873 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", PageWidth
);
2878 * Mirror the page as needed...
2882 doc_printf(doc
, "%.1f 0.0 translate -1 1 scale\n", PageWidth
);
2885 * Offset and scale as necessary for fitplot/fit-to-page/number-up...
2888 if (Duplex
&& doc
->number_up
> 1 && ((number
/ doc
->number_up
) & 1))
2889 doc_printf(doc
, "%.1f %.1f translate\n", PageWidth
- PageRight
, PageBottom
);
2890 else if (doc
->number_up
> 1 || doc
->fitplot
)
2891 doc_printf(doc
, "%.1f %.1f translate\n", PageLeft
, PageBottom
);
2893 switch (doc
->number_up
)
2899 l
= w
* bboxl
/ bboxw
;
2904 w
= l
* bboxw
/ bboxl
;
2907 tx
= 0.5 * (pagew
- w
);
2908 ty
= 0.5 * (pagel
- l
);
2910 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n", tx
, ty
,
2911 w
/ bboxw
, l
/ bboxl
);
2918 if (Orientation
& 1)
2922 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2926 l
= w
* bboxl
/ bboxw
;
2928 if (l
> (pagew
* 0.5))
2931 w
= l
* bboxw
/ bboxl
;
2934 tx
= 0.5 * (pagew
* 0.5 - l
);
2935 ty
= 0.5 * (pagel
- w
);
2937 if (doc
->normal_landscape
)
2938 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2940 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2942 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2943 ty
, tx
+ pagew
* 0.5 * x
, w
/ bboxw
, l
/ bboxl
);
2949 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2953 w
= l
* bboxw
/ bboxl
;
2955 if (w
> (pagel
* 0.5))
2958 l
= w
* bboxl
/ bboxw
;
2961 tx
= 0.5 * (pagel
* 0.5 - w
);
2962 ty
= 0.5 * (pagew
- l
);
2964 if (doc
->normal_landscape
)
2965 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2967 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2969 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2970 tx
+ pagel
* 0.5 * x
, ty
, w
/ bboxw
, l
/ bboxl
);
2975 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2986 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2989 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2993 l
= w
* bboxl
/ bboxw
;
2995 if (l
> (pagel
* 0.5))
2998 w
= l
* bboxw
/ bboxl
;
3001 tx
= 0.5 * (pagew
* 0.5 - w
);
3002 ty
= 0.5 * (pagel
* 0.5 - l
);
3004 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3005 tx
+ x
* pagew
* 0.5, ty
+ y
* pagel
* 0.5,
3006 w
/ bboxw
, l
/ bboxl
);
3010 if (Orientation
& 1)
3012 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3017 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3020 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3028 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3031 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3036 l
= w
* bboxl
/ bboxw
;
3038 if (l
> (pagew
* 0.333))
3041 w
= l
* bboxw
/ bboxl
;
3044 tx
= 0.5 * (pagel
- 2 * w
);
3045 ty
= 0.5 * (pagew
- 3 * l
);
3047 if (doc
->normal_landscape
)
3048 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
3050 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
3052 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3053 tx
+ x
* w
, ty
+ y
* l
, l
/ bboxl
, w
/ bboxw
);
3057 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3062 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3065 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3073 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3076 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3081 w
= l
* bboxw
/ bboxl
;
3083 if (w
> (pagel
* 0.333))
3086 l
= w
* bboxl
/ bboxw
;
3089 tx
= 0.5 * (pagel
- 3 * w
);
3090 ty
= 0.5 * (pagew
- 2 * l
);
3092 if (doc
->normal_landscape
)
3093 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
3095 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
3097 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3098 tx
+ w
* x
, ty
+ l
* y
, w
/ bboxw
, l
/ bboxl
);
3104 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3115 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3118 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3122 l
= w
* bboxl
/ bboxw
;
3124 if (l
> (pagel
* 0.333))
3127 w
= l
* bboxw
/ bboxl
;
3130 tx
= 0.5 * (pagew
* 0.333 - w
);
3131 ty
= 0.5 * (pagel
* 0.333 - l
);
3133 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3134 tx
+ x
* pagew
* 0.333, ty
+ y
* pagel
* 0.333,
3135 w
/ bboxw
, l
/ bboxl
);
3139 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3150 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3153 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3157 l
= w
* bboxl
/ bboxw
;
3159 if (l
> (pagel
* 0.25))
3162 w
= l
* bboxw
/ bboxl
;
3165 tx
= 0.5 * (pagew
* 0.25 - w
);
3166 ty
= 0.5 * (pagel
* 0.25 - l
);
3168 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3169 tx
+ x
* pagew
* 0.25, ty
+ y
* pagel
* 0.25,
3170 w
/ bboxw
, l
/ bboxl
);
3175 * Draw borders as necessary...
3178 if (doc
->page_border
&& show_border
)
3180 int rects
; /* Number of border rectangles */
3181 float fscale
; /* Scaling value for points */
3184 rects
= (doc
->page_border
& PSTOPS_BORDERDOUBLE
) ? 2 : 1;
3185 fscale
= PageWidth
/ w
;
3186 margin
= 2.25 * fscale
;
3189 * Set the line width and color...
3192 doc_puts(doc
, "gsave\n");
3193 doc_printf(doc
, "%.3f setlinewidth 0 setgray newpath\n",
3194 (doc
->page_border
& PSTOPS_BORDERTHICK
) ? 0.5 * fscale
:
3198 * Draw border boxes...
3201 for (; rects
> 0; rects
--, margin
+= 2 * fscale
)
3202 if (doc
->number_up
> 1)
3203 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3207 bboxl
- 2 * margin
);
3209 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3211 PageBottom
+ margin
,
3212 PageRight
- PageLeft
- 2 * margin
,
3213 PageTop
- PageBottom
- 2 * margin
);
3216 * Restore pen settings...
3219 doc_puts(doc
, "grestore\n");
3225 * Offset the page by its bounding box...
3228 doc_printf(doc
, "%d %d translate\n", -bounding_box
[0],
3232 if (doc
->fitplot
|| doc
->number_up
> 1)
3235 * Clip the page to the page's bounding box...
3238 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrc\n",
3239 bboxx
+ margin
, bboxy
+ margin
,
3240 bboxw
- 2 * margin
, bboxl
- 2 * margin
);
3246 * 'write_label_prolog()' - Write the prolog with the classification
3251 write_label_prolog(pstops_doc_t
*doc
, /* I - Document info */
3252 const char *label
, /* I - Page label */
3253 float bottom
, /* I - Bottom position in points */
3254 float top
, /* I - Top position in points */
3255 float width
) /* I - Width in points */
3257 const char *classification
; /* CLASSIFICATION environment variable */
3258 const char *ptr
; /* Temporary string pointer */
3262 * First get the current classification...
3265 if ((classification
= getenv("CLASSIFICATION")) == NULL
)
3266 classification
= "";
3267 if (strcmp(classification
, "none") == 0)
3268 classification
= "";
3271 * If there is nothing to show, bind an empty 'write labels' procedure
3275 if (!classification
[0] && (label
== NULL
|| !label
[0]))
3277 doc_puts(doc
, "userdict/ESPwl{}bind put\n");
3282 * Set the classification + page label string...
3285 doc_puts(doc
, "userdict");
3286 if (!strcmp(classification
, "confidential"))
3287 doc_puts(doc
, "/ESPpl(CONFIDENTIAL");
3288 else if (!strcmp(classification
, "classified"))
3289 doc_puts(doc
, "/ESPpl(CLASSIFIED");
3290 else if (!strcmp(classification
, "secret"))
3291 doc_puts(doc
, "/ESPpl(SECRET");
3292 else if (!strcmp(classification
, "topsecret"))
3293 doc_puts(doc
, "/ESPpl(TOP SECRET");
3294 else if (!strcmp(classification
, "unclassified"))
3295 doc_puts(doc
, "/ESPpl(UNCLASSIFIED");
3298 doc_puts(doc
, "/ESPpl(");
3300 for (ptr
= classification
; *ptr
; ptr
++)
3302 if (*ptr
< 32 || *ptr
> 126)
3303 doc_printf(doc
, "\\%03o", *ptr
);
3304 else if (*ptr
== '_')
3306 else if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
3307 doc_printf(doc
, "\\%c", *ptr
);
3309 doc_printf(doc
, "%c", *ptr
);
3315 if (classification
[0])
3316 doc_puts(doc
, " - ");
3319 * Quote the label string as needed...
3322 for (ptr
= label
; *ptr
; ptr
++)
3324 if (*ptr
< 32 || *ptr
> 126)
3325 doc_printf(doc
, "\\%03o", *ptr
);
3326 else if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
3327 doc_printf(doc
, "\\%c", *ptr
);
3329 doc_printf(doc
, "%c", *ptr
);
3333 doc_puts(doc
, ")put\n");
3336 * Then get a 14 point Helvetica-Bold font...
3339 doc_puts(doc
, "userdict/ESPpf /Helvetica-Bold findfont 14 scalefont put\n");
3342 * Finally, the procedure to write the labels on the page...
3345 doc_puts(doc
, "userdict/ESPwl{\n");
3346 doc_puts(doc
, " ESPpf setfont\n");
3347 doc_printf(doc
, " ESPpl stringwidth pop dup 12 add exch -0.5 mul %.0f add\n",
3349 doc_puts(doc
, " 1 setgray\n");
3350 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrf\n", bottom
- 2.0);
3351 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrf\n", top
- 18.0);
3352 doc_puts(doc
, " 0 setgray\n");
3353 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrs\n", bottom
- 2.0);
3354 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrs\n", top
- 18.0);
3355 doc_printf(doc
, " dup %.0f moveto ESPpl show\n", bottom
+ 2.0);
3356 doc_printf(doc
, " %.0f moveto ESPpl show\n", top
- 14.0);
3357 doc_puts(doc
, "pop\n");
3358 doc_puts(doc
, "}bind put\n");
3363 * 'write_labels()' - Write the actual page labels.
3365 * This function is a copy of the one in common.c since we need to
3366 * use doc_puts/doc_printf instead of puts/printf...
3370 write_labels(pstops_doc_t
*doc
, /* I - Document information */
3371 int orient
) /* I - Orientation of the page */
3373 float width
, /* Width of page */
3374 length
; /* Length of page */
3377 doc_puts(doc
, "gsave\n");
3379 if ((orient
^ Orientation
) & 1)
3387 length
= PageLength
;
3392 case 1 : /* Landscape */
3393 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", length
);
3395 case 2 : /* Reverse Portrait */
3396 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", width
, length
);
3398 case 3 : /* Reverse Landscape */
3399 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", width
);
3403 doc_puts(doc
, "ESPwl\n");
3404 doc_puts(doc
, "grestore\n");
3409 * 'write_options()' - Write options provided via %%IncludeFeature.
3414 pstops_doc_t
*doc
, /* I - Document */
3415 ppd_file_t
*ppd
, /* I - PPD file */
3416 int num_options
, /* I - Number of options */
3417 cups_option_t
*options
) /* I - Options */
3419 int i
; /* Looping var */
3420 ppd_option_t
*option
; /* PPD option */
3421 int min_order
; /* Minimum OrderDependency value */
3422 char *doc_setup
, /* DocumentSetup commands to send */
3423 *any_setup
; /* AnySetup commands to send */
3427 * Figure out the minimum OrderDependency value...
3430 if ((option
= ppdFindOption(ppd
, "PageRegion")) != NULL
)
3431 min_order
= option
->order
;
3435 for (i
= 0; i
< num_options
; i
++)
3436 if ((option
= ppdFindOption(ppd
, options
[i
].name
)) != NULL
&&
3437 option
->order
< min_order
)
3438 min_order
= option
->order
;
3441 * Mark and extract them...
3444 cupsMarkOptions(ppd
, num_options
, options
);
3446 doc_setup
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, min_order
);
3447 any_setup
= ppdEmitString(ppd
, PPD_ORDER_ANY
, min_order
);
3450 * Then send them out...
3453 if (doc
->number_up
> 1)
3456 * Temporarily restore setpagedevice so we can set the options...
3459 doc_puts(doc
, "userdict/setpagedevice/CUPSsetpagedevice load put\n");
3464 doc_puts(doc
, doc_setup
);
3470 doc_puts(doc
, any_setup
);
3474 if (doc
->number_up
> 1)
3477 * Disable setpagedevice again...
3480 doc_puts(doc
, "userdict/setpagedevice{pop}bind put\n");
3486 * End of "$Id: pstops.c 7977 2008-09-23 23:44:33Z mike $".