]>
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-2010 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
, ...)
204 __attribute__ ((__format__ (__printf__
, 2, 3)))
205 #endif /* __GNUC__ */
207 static void doc_puts(pstops_doc_t
*doc
, const char *s
);
208 static void doc_write(pstops_doc_t
*doc
, const char *s
, size_t len
);
209 static void end_nup(pstops_doc_t
*doc
, int number
);
210 static int include_feature(ppd_file_t
*ppd
, const char *line
,
212 cups_option_t
**options
);
213 static char *parse_text(const char *start
, char **end
, char *buffer
,
215 static void set_pstops_options(pstops_doc_t
*doc
, ppd_file_t
*ppd
,
216 char *argv
[], int num_options
,
217 cups_option_t
*options
);
218 static ssize_t
skip_page(cups_file_t
*fp
, char *line
, ssize_t linelen
,
220 static void start_nup(pstops_doc_t
*doc
, int number
,
221 int show_border
, const int *bounding_box
);
222 static void write_label_prolog(pstops_doc_t
*doc
, const char *label
,
223 float bottom
, float top
,
225 static void write_labels(pstops_doc_t
*doc
, int orient
);
226 static void write_options(pstops_doc_t
*doc
, ppd_file_t
*ppd
,
227 int num_options
, cups_option_t
*options
);
231 * 'main()' - Main entry.
234 int /* O - Exit status */
235 main(int argc
, /* I - Number of command-line args */
236 char *argv
[]) /* I - Command-line arguments */
238 pstops_doc_t doc
; /* Document information */
239 cups_file_t
*fp
; /* Print file */
240 ppd_file_t
*ppd
; /* PPD file */
241 int num_options
; /* Number of print options */
242 cups_option_t
*options
; /* Print options */
243 char line
[8192]; /* Line buffer */
244 size_t len
; /* Length of line buffer */
245 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
246 struct sigaction action
; /* Actions for POSIX signals */
247 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
251 * Make sure status messages are not buffered...
254 setbuf(stderr
, NULL
);
257 * Check command-line...
260 if (argc
< 6 || argc
> 7)
262 _cupsLangPrintf(stderr
,
263 _("Usage: %s job-id user title copies options [file]\n"),
269 * Register a signal handler to cleanly cancel a job.
272 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
273 sigset(SIGTERM
, cancel_job
);
274 #elif defined(HAVE_SIGACTION)
275 memset(&action
, 0, sizeof(action
));
277 sigemptyset(&action
.sa_mask
);
278 action
.sa_handler
= cancel_job
;
279 sigaction(SIGTERM
, &action
, NULL
);
281 signal(SIGTERM
, cancel_job
);
282 #endif /* HAVE_SIGSET */
285 * If we have 7 arguments, print the file named on the command-line.
286 * Otherwise, send stdin instead...
290 fp
= cupsFileStdin();
294 * Try to open the print file...
297 if ((fp
= cupsFileOpen(argv
[6], "r")) == NULL
)
299 _cupsLangPrintf(stderr
, _("ERROR: Unable to open file \"%s\" - %s\n"),
300 argv
[6], strerror(errno
));
306 * Read the first line to see if we have DSC comments...
309 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
311 fputs("DEBUG: The print file is empty.\n", stderr
);
316 * Process command-line options...
320 num_options
= cupsParseOptions(argv
[5], 0, &options
);
321 ppd
= SetCommonOptions(num_options
, options
, 1);
323 set_pstops_options(&doc
, ppd
, argv
, num_options
, options
);
326 * Write any "exit server" options that have been selected...
329 ppdEmit(ppd
, stdout
, PPD_ORDER_EXIT
);
332 * Write any JCL commands that are needed to print PostScript code...
336 ppdEmitJCL(ppd
, stdout
, doc
.job_id
, doc
.user
, doc
.title
);
339 * Start with a DSC header...
342 puts("%!PS-Adobe-3.0");
345 * Skip leading PJL in the document...
348 while (!strncmp(line
, "\033%-12345X", 9) || !strncmp(line
, "@PJL ", 5))
351 * Yup, we have leading PJL fun, so skip it until we hit the line
352 * with "ENTER LANGUAGE"...
355 fputs("DEBUG: Skipping PJL header...\n", stderr
);
357 while (strstr(line
, "ENTER LANGUAGE") == NULL
&& strncmp(line
, "%!", 2))
358 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
361 if (!strncmp(line
, "%!", 2))
364 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
369 * Now see if the document conforms to the Adobe Document Structuring
373 if (!strncmp(line
, "%!PS-Adobe-", 11))
376 * Yes, filter the document...
379 copy_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
384 * No, display an error message and treat the file as if it contains
388 copy_non_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
392 * Send %%EOF as needed...
399 * End the job with the appropriate JCL command or CTRL-D...
404 if (ppd
&& ppd
->jcl_end
)
405 ppdEmitJCLEnd(ppd
, stdout
);
411 * Close files and remove the temporary file if needed...
416 cupsFileClose(doc
.temp
);
417 unlink(doc
.tempfile
);
421 cupsFreeOptions(num_options
, options
);
430 * 'add_page()' - Add a page to the pages array.
433 static pstops_page_t
* /* O - New page info object */
434 add_page(pstops_doc_t
*doc
, /* I - Document information */
435 const char *label
) /* I - Page label */
437 pstops_page_t
*pageinfo
; /* New page info object */
441 doc
->pages
= cupsArrayNew(NULL
, NULL
);
445 _cupsLangPrintf(stderr
,
446 _("EMERG: Unable to allocate memory for pages array: %s\n"),
451 if ((pageinfo
= calloc(1, sizeof(pstops_page_t
))) == NULL
)
453 _cupsLangPrintf(stderr
,
454 _("EMERG: Unable to allocate memory for page info: %s\n"),
459 pageinfo
->label
= strdup(label
);
460 pageinfo
->offset
= cupsFileTell(doc
->temp
);
462 cupsArrayAdd(doc
->pages
, pageinfo
);
471 * 'cancel_job()' - Flag the job as canceled.
475 cancel_job(int sig
) /* I - Signal number (unused) */
484 * 'check_range()' - Check to see if the current page is selected for
488 static int /* O - 1 if selected, 0 otherwise */
489 check_range(pstops_doc_t
*doc
, /* I - Document information */
490 int page
) /* I - Page number */
492 const char *range
; /* Pointer into range string */
493 int lower
, upper
; /* Lower and upper page numbers */
499 * See if we only print even or odd pages...
502 if (!strcasecmp(doc
->page_set
, "even") && (page
& 1))
505 if (!strcasecmp(doc
->page_set
, "odd") && !(page
& 1))
509 if (!doc
->page_ranges
)
510 return (1); /* No range, print all pages... */
512 for (range
= doc
->page_ranges
; *range
!= '\0';)
518 upper
= strtol(range
, (char **)&range
, 10);
522 lower
= strtol(range
, (char **)&range
, 10);
527 if (!isdigit(*range
& 255))
530 upper
= strtol(range
, (char **)&range
, 10);
536 if (page
>= lower
&& page
<= upper
)
550 * 'copy_bytes()' - Copy bytes from the input file to stdout.
554 copy_bytes(cups_file_t
*fp
, /* I - File to read from */
555 off_t offset
, /* I - Offset to page data */
556 size_t length
) /* I - Length of page data */
558 char buffer
[8192]; /* Data buffer */
559 ssize_t nbytes
; /* Number of bytes read */
560 size_t nleft
; /* Number of bytes left/remaining */
565 if (cupsFileSeek(fp
, offset
) < 0)
567 _cupsLangPrintf(stderr
,
568 #ifdef HAVE_LONG_LONG
569 _("ERROR: Unable to seek to offset %lld in file - %s\n"),
571 _("ERROR: Unable to seek to offset %ld in file - %s\n"),
572 #endif /* HAVE_LONG_LONG */
573 CUPS_LLCAST offset
, strerror(errno
));
577 while (nleft
> 0 || length
== 0)
579 if (nleft
> sizeof(buffer
) || length
== 0)
580 nbytes
= sizeof(buffer
);
584 if ((nbytes
= cupsFileRead(fp
, buffer
, nbytes
)) < 1)
589 fwrite(buffer
, 1, nbytes
, stdout
);
595 * 'copy_comments()' - Copy all of the comments section.
597 * This function expects "line" to be filled with a comment line.
598 * On return, "line" will contain the next line in the file, if any.
601 static ssize_t
/* O - Length of next line */
602 copy_comments(cups_file_t
*fp
, /* I - File to read from */
603 pstops_doc_t
*doc
, /* I - Document info */
604 ppd_file_t
*ppd
, /* I - PPD file */
605 char *line
, /* I - Line buffer */
606 ssize_t linelen
, /* I - Length of initial line */
607 size_t linesize
) /* I - Size of line buffer */
609 int saw_bounding_box
, /* Saw %%BoundingBox: comment? */
610 saw_for
, /* Saw %%For: comment? */
611 saw_pages
, /* Saw %%Pages: comment? */
612 saw_title
; /* Saw %%Title: comment? */
616 * Loop until we see %%EndComments or a non-comment line...
619 saw_bounding_box
= 0;
624 while (line
[0] == '%')
627 * Strip trailing whitespace...
634 if (!isspace(line
[linelen
] & 255))
637 line
[linelen
] = '\0';
644 fprintf(stderr
, "DEBUG: %s\n", line
);
647 * Pull the headers out...
650 if (!strncmp(line
, "%%Pages:", 8))
652 int pages
; /* Number of pages */
655 fputs("DEBUG: A duplicate %%Pages: comment was seen.\n", stderr
);
659 if (Duplex
&& (pages
= atoi(line
+ 8)) > 0 && pages
<= doc
->number_up
)
662 * Since we will only be printing on a single page, disable duplexing.
666 doc
->slow_duplex
= 0;
668 if (cupsGetOption("sides", doc
->num_options
, doc
->options
))
669 doc
->num_options
= cupsAddOption("sides", "one-sided",
670 doc
->num_options
, &(doc
->options
));
672 if (cupsGetOption("Duplex", doc
->num_options
, doc
->options
))
673 doc
->num_options
= cupsAddOption("Duplex", "None",
674 doc
->num_options
, &(doc
->options
));
676 if (cupsGetOption("EFDuplex", doc
->num_options
, doc
->options
))
677 doc
->num_options
= cupsAddOption("EFDuplex", "None",
678 doc
->num_options
, &(doc
->options
));
680 if (cupsGetOption("EFDuplexing", doc
->num_options
, doc
->options
))
681 doc
->num_options
= cupsAddOption("EFDuplexing", "False",
682 doc
->num_options
, &(doc
->options
));
684 if (cupsGetOption("KD03Duplex", doc
->num_options
, doc
->options
))
685 doc
->num_options
= cupsAddOption("KD03Duplex", "None",
686 doc
->num_options
, &(doc
->options
));
688 if (cupsGetOption("JCLDuplex", doc
->num_options
, doc
->options
))
689 doc
->num_options
= cupsAddOption("JCLDuplex", "None",
690 doc
->num_options
, &(doc
->options
));
692 ppdMarkOption(ppd
, "Duplex", "None");
693 ppdMarkOption(ppd
, "EFDuplex", "None");
694 ppdMarkOption(ppd
, "EFDuplexing", "False");
695 ppdMarkOption(ppd
, "KD03Duplex", "None");
696 ppdMarkOption(ppd
, "JCLDuplex", "None");
699 else if (!strncmp(line
, "%%BoundingBox:", 14))
701 if (saw_bounding_box
)
702 fputs("DEBUG: A duplicate %%BoundingBox: comment was seen.\n", stderr
);
703 else if (strstr(line
+ 14, "(atend)"))
706 * Do nothing for now but use the default imageable area...
709 else if (sscanf(line
+ 14, "%d%d%d%d", doc
->bounding_box
+ 0,
710 doc
->bounding_box
+ 1, doc
->bounding_box
+ 2,
711 doc
->bounding_box
+ 3) != 4)
713 fputs("DEBUG: A bad %%BoundingBox: comment was seen.\n", stderr
);
715 doc
->bounding_box
[0] = (int)PageLeft
;
716 doc
->bounding_box
[1] = (int)PageBottom
;
717 doc
->bounding_box
[2] = (int)PageRight
;
718 doc
->bounding_box
[3] = (int)PageTop
;
721 saw_bounding_box
= 1;
723 else if (!strncmp(line
, "%%For:", 6))
726 doc_printf(doc
, "%s\n", line
);
728 else if (!strncmp(line
, "%%Title:", 8))
731 doc_printf(doc
, "%s\n", line
);
733 else if (!strncmp(line
, "%cupsRotation:", 14))
736 * Reset orientation of document?
739 int orient
= (atoi(line
+ 14) / 90) & 3;
741 if (orient
!= Orientation
)
744 * Yes, update things so that the pages come out right...
747 Orientation
= (4 - Orientation
+ orient
) & 3;
749 Orientation
= orient
;
752 else if (!strcmp(line
, "%%EndComments"))
754 linelen
= cupsFileGetLine(fp
, line
, linesize
);
757 else if (strncmp(line
, "%!", 2) && strncmp(line
, "%cups", 5))
758 doc_printf(doc
, "%s\n", line
);
760 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
764 if (!saw_bounding_box
)
765 fputs("DEBUG: There wasn't a %%BoundingBox: comment in the header.\n",
769 fputs("DEBUG: There wasn't a %%Pages: comment in the header.\n", stderr
);
772 WriteTextComment("For", doc
->user
);
775 WriteTextComment("Title", doc
->title
);
777 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
780 * Tell the document processor the copy and duplex options
781 * that are required...
784 doc_printf(doc
, "%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
785 doc
->collate
? " collate" : "",
786 Duplex
? " duplex" : "");
789 * Apple uses RBI comments for various non-PPD options...
792 doc_printf(doc
, "%%RBINumCopies: %d\n", doc
->copies
);
797 * Tell the document processor the duplex option that is required...
801 doc_puts(doc
, "%%Requirements: duplex\n");
804 * Apple uses RBI comments for various non-PPD options...
807 doc_puts(doc
, "%RBINumCopies: 1\n");
810 doc_puts(doc
, "%%Pages: (atend)\n");
811 doc_puts(doc
, "%%BoundingBox: (atend)\n");
812 doc_puts(doc
, "%%EndComments\n");
819 * 'copy_dsc()' - Copy a DSC-conforming document.
821 * This function expects "line" to be filled with the %!PS-Adobe comment line.
825 copy_dsc(cups_file_t
*fp
, /* I - File to read from */
826 pstops_doc_t
*doc
, /* I - Document info */
827 ppd_file_t
*ppd
, /* I - PPD file */
828 char *line
, /* I - Line buffer */
829 ssize_t linelen
, /* I - Length of initial line */
830 size_t linesize
) /* I - Size of line buffer */
832 int number
; /* Page number */
833 pstops_page_t
*pageinfo
; /* Page information */
837 * Make sure we use ESPshowpage for EPS files...
840 if (strstr(line
, "EPSF"))
842 doc
->use_ESPshowpage
= 1;
847 * Start sending the document with any commands needed...
850 fprintf(stderr
, "DEBUG: Before copy_comments - %s", line
);
851 linelen
= copy_comments(fp
, doc
, ppd
, line
, linelen
, linesize
);
854 * Now find the prolog section, if any...
857 fprintf(stderr
, "DEBUG: Before copy_prolog - %s", line
);
858 linelen
= copy_prolog(fp
, doc
, ppd
, line
, linelen
, linesize
);
861 * Then the document setup section...
864 fprintf(stderr
, "DEBUG: Before copy_setup - %s", line
);
865 linelen
= copy_setup(fp
, doc
, ppd
, line
, linelen
, linesize
);
868 * Copy until we see %%Page:...
871 while (strncmp(line
, "%%Page:", 7) && strncmp(line
, "%%Trailer", 9))
873 doc_write(doc
, line
, linelen
);
875 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
880 * Then process pages until we have no more...
885 fprintf(stderr
, "DEBUG: Before page loop - %s", line
);
886 while (!strncmp(line
, "%%Page:", 7))
893 if (check_range(doc
, (number
- 1) / doc
->number_up
+ 1))
895 fprintf(stderr
, "DEBUG: Copying page %d...\n", number
);
896 linelen
= copy_page(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
900 fprintf(stderr
, "DEBUG: Skipping page %d...\n", number
);
901 linelen
= skip_page(fp
, line
, linelen
, linesize
);
906 * Finish up the last page(s)...
909 if (number
&& is_not_last_page(number
) && cupsArrayLast(doc
->pages
) &&
910 check_range(doc
, (number
- 1) / doc
->number_up
+ 1))
912 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
914 start_nup(doc
, doc
->number_up
, 0, doc
->bounding_box
);
915 doc_puts(doc
, "showpage\n");
916 end_nup(doc
, doc
->number_up
);
918 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
921 if (doc
->slow_duplex
&& (doc
->page
& 1))
924 * Make sure we have an even number of pages...
927 pageinfo
= add_page(doc
, "(filler)");
929 if (!doc
->slow_order
)
931 if (!ppd
|| !ppd
->num_filters
)
932 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
933 doc
->slow_collate
? 1 : doc
->copies
);
935 printf("%%%%Page: (filler) %d\n", doc
->page
);
938 start_nup(doc
, doc
->number_up
, 0, doc
->bounding_box
);
939 doc_puts(doc
, "showpage\n");
940 end_nup(doc
, doc
->number_up
);
942 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
946 * Make additional copies as necessary...
949 number
= doc
->slow_order
? 0 : doc
->page
;
951 if (doc
->temp
&& !JobCanceled
&& cupsArrayCount(doc
->pages
) > 0)
953 int copy
; /* Current copy */
957 * Reopen the temporary file for reading...
960 cupsFileClose(doc
->temp
);
962 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
968 if (doc
->slow_collate
)
969 copy
= !doc
->slow_order
;
971 copy
= doc
->copies
- 1;
973 for (; copy
< doc
->copies
; copy
++)
979 * Send end-of-job stuff followed by any start-of-job stuff required
980 * for the JCL options...
983 if (number
&& doc
->emit_jcl
&& ppd
&& ppd
->jcl_end
)
986 * Send the trailer...
990 printf("%%%%Pages: %d\n", cupsArrayCount(doc
->pages
));
991 if (doc
->number_up
> 1 || doc
->fitplot
)
992 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
993 PageLeft
, PageBottom
, PageRight
, PageTop
);
995 printf("%%%%BoundingBox: %d %d %d %d\n",
996 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
997 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
1001 * Start a new document...
1004 ppdEmitJCLEnd(ppd
, stdout
);
1005 ppdEmitJCL(ppd
, stdout
, doc
->job_id
, doc
->user
, doc
->title
);
1007 puts("%!PS-Adobe-3.0");
1013 * Copy the prolog as needed...
1018 pageinfo
= (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
1019 copy_bytes(doc
->temp
, 0, pageinfo
->offset
);
1023 * Then copy all of the pages...
1026 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayLast(doc
->pages
) :
1027 (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
1036 if (!ppd
|| !ppd
->num_filters
)
1037 fprintf(stderr
, "PAGE: %d %d\n", number
,
1038 doc
->slow_collate
? 1 : doc
->copies
);
1040 if (doc
->number_up
> 1)
1042 printf("%%%%Page: (%d) %d\n", number
, number
);
1043 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1044 PageLeft
, PageBottom
, PageRight
, PageTop
);
1048 printf("%%%%Page: %s %d\n", pageinfo
->label
, number
);
1049 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1050 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
1051 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
1054 copy_bytes(doc
->temp
, pageinfo
->offset
, pageinfo
->length
);
1056 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayPrev(doc
->pages
) :
1057 (pstops_page_t
*)cupsArrayNext(doc
->pages
);
1063 * Restore the old showpage operator as needed...
1066 if (doc
->use_ESPshowpage
)
1067 puts("userdict/showpage/ESPshowpage load put\n");
1070 * Write/copy the trailer...
1074 copy_trailer(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
1079 * 'copy_non_dsc()' - Copy a document that does not conform to the DSC.
1081 * This function expects "line" to be filled with the %! comment line.
1085 copy_non_dsc(cups_file_t
*fp
, /* I - File to read from */
1086 pstops_doc_t
*doc
, /* I - Document info */
1087 ppd_file_t
*ppd
, /* I - PPD file */
1088 char *line
, /* I - Line buffer */
1089 ssize_t linelen
, /* I - Length of initial line */
1090 size_t linesize
) /* I - Size of line buffer */
1092 int copy
; /* Current copy */
1093 char buffer
[8192]; /* Copy buffer */
1094 int bytes
; /* Number of bytes copied */
1098 * First let the user know that they are attempting to print a file
1099 * that may not print correctly...
1102 fputs("DEBUG: This document does not conform to the Adobe Document "
1103 "Structuring Conventions and may not print correctly.\n", stderr
);
1106 * Then write a standard DSC comment section...
1109 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", PageLeft
, PageBottom
,
1110 PageRight
, PageTop
);
1112 if (doc
->slow_collate
&& doc
->copies
> 1)
1113 printf("%%%%Pages: %d\n", doc
->copies
);
1117 WriteTextComment("For", doc
->user
);
1118 WriteTextComment("Title", doc
->title
);
1120 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1123 * Tell the document processor the copy and duplex options
1124 * that are required...
1127 printf("%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
1128 doc
->collate
? " collate" : "",
1129 Duplex
? " duplex" : "");
1132 * Apple uses RBI comments for various non-PPD options...
1135 printf("%%RBINumCopies: %d\n", doc
->copies
);
1140 * Tell the document processor the duplex option that is required...
1144 puts("%%Requirements: duplex");
1147 * Apple uses RBI comments for various non-PPD options...
1150 puts("%RBINumCopies: 1");
1153 puts("%%EndComments");
1156 * Then the prolog...
1159 puts("%%BeginProlog");
1161 do_prolog(doc
, ppd
);
1163 puts("%%EndProlog");
1166 * Then the setup section...
1169 puts("%%BeginSetup");
1176 * Finally, embed a copy of the file inside a %%Page...
1179 if (!ppd
|| !ppd
->num_filters
)
1180 fprintf(stderr
, "PAGE: 1 %d\n", doc
->temp
? 1 : doc
->copies
);
1182 puts("%%Page: 1 1");
1183 puts("%%BeginPageSetup");
1184 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1185 puts("%%EndPageSetup");
1186 puts("%%BeginDocument: nondsc");
1188 fwrite(line
, linelen
, 1, stdout
);
1191 cupsFileWrite(doc
->temp
, line
, linelen
);
1193 while ((bytes
= cupsFileRead(fp
, buffer
, sizeof(buffer
))) > 0)
1195 fwrite(buffer
, 1, bytes
, stdout
);
1198 cupsFileWrite(doc
->temp
, buffer
, bytes
);
1201 puts("%%EndDocument");
1203 if (doc
->use_ESPshowpage
)
1205 WriteLabels(Orientation
);
1206 puts("ESPshowpage");
1209 if (doc
->temp
&& !JobCanceled
)
1212 * Reopen the temporary file for reading...
1215 cupsFileClose(doc
->temp
);
1217 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
1220 * Make the additional copies as needed...
1223 for (copy
= 1; copy
< doc
->copies
; copy
++)
1228 if (!ppd
|| !ppd
->num_filters
)
1229 fputs("PAGE: 1 1\n", stderr
);
1231 printf("%%%%Page: %d %d\n", copy
+ 1, copy
+ 1);
1232 puts("%%BeginPageSetup");
1233 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1234 puts("%%EndPageSetup");
1235 puts("%%BeginDocument: nondsc");
1237 copy_bytes(doc
->temp
, 0, 0);
1239 puts("%%EndDocument");
1241 if (doc
->use_ESPshowpage
)
1243 WriteLabels(Orientation
);
1244 puts("ESPshowpage");
1250 * Restore the old showpage operator as needed...
1253 if (doc
->use_ESPshowpage
)
1254 puts("userdict/showpage/ESPshowpage load put\n");
1259 * 'copy_page()' - Copy a page description.
1261 * This function expects "line" to be filled with a %%Page comment line.
1262 * On return, "line" will contain the next line in the file, if any.
1265 static ssize_t
/* O - Length of next line */
1266 copy_page(cups_file_t
*fp
, /* I - File to read from */
1267 pstops_doc_t
*doc
, /* I - Document info */
1268 ppd_file_t
*ppd
, /* I - PPD file */
1269 int number
, /* I - Current page number */
1270 char *line
, /* I - Line buffer */
1271 ssize_t linelen
, /* I - Length of initial line */
1272 size_t linesize
) /* I - Size of line buffer */
1274 char label
[256], /* Page label string */
1275 *ptr
; /* Pointer into line */
1276 int level
; /* Embedded document level */
1277 pstops_page_t
*pageinfo
; /* Page information */
1278 int first_page
; /* First page on N-up output? */
1279 int has_page_setup
; /* Does the page have %%Begin/EndPageSetup? */
1280 int bounding_box
[4]; /* PageBoundingBox */
1284 * Get the page label for this page...
1287 first_page
= is_first_page(number
);
1289 if (!parse_text(line
+ 7, &ptr
, label
, sizeof(label
)))
1291 fputs("DEBUG: There was a bad %%Page: comment in the file.\n", stderr
);
1295 else if (strtol(ptr
, &ptr
, 10) == LONG_MAX
|| !isspace(*ptr
& 255))
1297 fputs("DEBUG: There was a bad %%Page: comment in the file.\n", stderr
);
1302 * Create or update the current output page...
1306 pageinfo
= add_page(doc
, label
);
1308 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
1311 * Handle first page override...
1314 if (doc
->ap_input_slot
|| doc
->ap_manual_feed
)
1319 * First page/sheet gets AP_FIRSTPAGE_* options...
1322 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->ap_input_slot
,
1323 pageinfo
->num_options
,
1324 &(pageinfo
->options
));
1325 pageinfo
->num_options
= cupsAddOption("ManualFeed",
1326 doc
->ap_input_slot
? "False" :
1327 doc
->ap_manual_feed
,
1328 pageinfo
->num_options
,
1329 &(pageinfo
->options
));
1330 pageinfo
->num_options
= cupsAddOption("MediaColor", doc
->ap_media_color
,
1331 pageinfo
->num_options
,
1332 &(pageinfo
->options
));
1333 pageinfo
->num_options
= cupsAddOption("MediaType", doc
->ap_media_type
,
1334 pageinfo
->num_options
,
1335 &(pageinfo
->options
));
1336 pageinfo
->num_options
= cupsAddOption("PageRegion", doc
->ap_page_region
,
1337 pageinfo
->num_options
,
1338 &(pageinfo
->options
));
1339 pageinfo
->num_options
= cupsAddOption("PageSize", doc
->ap_page_size
,
1340 pageinfo
->num_options
,
1341 &(pageinfo
->options
));
1343 else if (doc
->page
== (Duplex
+ 2))
1346 * Second page/sheet gets default options...
1349 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->input_slot
,
1350 pageinfo
->num_options
,
1351 &(pageinfo
->options
));
1352 pageinfo
->num_options
= cupsAddOption("ManualFeed",
1353 doc
->input_slot
? "False" :
1355 pageinfo
->num_options
,
1356 &(pageinfo
->options
));
1357 pageinfo
->num_options
= cupsAddOption("MediaColor", doc
->media_color
,
1358 pageinfo
->num_options
,
1359 &(pageinfo
->options
));
1360 pageinfo
->num_options
= cupsAddOption("MediaType", doc
->media_type
,
1361 pageinfo
->num_options
,
1362 &(pageinfo
->options
));
1363 pageinfo
->num_options
= cupsAddOption("PageRegion", doc
->page_region
,
1364 pageinfo
->num_options
,
1365 &(pageinfo
->options
));
1366 pageinfo
->num_options
= cupsAddOption("PageSize", doc
->page_size
,
1367 pageinfo
->num_options
,
1368 &(pageinfo
->options
));
1373 * Scan comments until we see something other than %%Page*: or
1377 memcpy(bounding_box
, doc
->bounding_box
, sizeof(bounding_box
));
1379 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1381 if (!strncmp(line
, "%%PageBoundingBox:", 18))
1384 * %%PageBoundingBox: llx lly urx ury
1387 if (sscanf(line
+ 18, "%d%d%d%d", bounding_box
+ 0,
1388 bounding_box
+ 1, bounding_box
+ 2,
1389 bounding_box
+ 3) != 4)
1391 fputs("DEBUG: There was a bad %%PageBoundingBox: comment in the file.\n", stderr
);
1392 memcpy(bounding_box
, doc
->bounding_box
,
1393 sizeof(bounding_box
));
1395 else if (doc
->number_up
== 1 && !doc
->fitplot
&& Orientation
)
1397 int temp_bbox
[4]; /* Temporary bounding box */
1400 memcpy(temp_bbox
, bounding_box
, sizeof(temp_bbox
));
1402 fprintf(stderr
, "DEBUG: Orientation = %d\n", Orientation
);
1403 fprintf(stderr
, "DEBUG: original bounding_box = [ %d %d %d %d ]\n",
1404 bounding_box
[0], bounding_box
[1],
1405 bounding_box
[2], bounding_box
[3]);
1406 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
1407 PageWidth
, PageLength
);
1409 switch (Orientation
)
1411 case 1 : /* Landscape */
1412 bounding_box
[0] = PageLength
- temp_bbox
[3];
1413 bounding_box
[1] = temp_bbox
[0];
1414 bounding_box
[2] = PageLength
- temp_bbox
[1];
1415 bounding_box
[3] = temp_bbox
[2];
1418 case 2 : /* Reverse Portrait */
1419 bounding_box
[0] = PageWidth
- temp_bbox
[2];
1420 bounding_box
[1] = PageLength
- temp_bbox
[3];
1421 bounding_box
[2] = PageWidth
- temp_bbox
[0];
1422 bounding_box
[3] = PageLength
- temp_bbox
[1];
1425 case 3 : /* Reverse Landscape */
1426 bounding_box
[0] = temp_bbox
[1];
1427 bounding_box
[1] = PageWidth
- temp_bbox
[2];
1428 bounding_box
[2] = temp_bbox
[3];
1429 bounding_box
[3] = PageWidth
- temp_bbox
[0];
1433 fprintf(stderr
, "DEBUG: updated bounding_box = [ %d %d %d %d ]\n",
1434 bounding_box
[0], bounding_box
[1],
1435 bounding_box
[2], bounding_box
[3]);
1439 else if (!strncmp(line
, "%%PageCustomColors:", 19) ||
1440 !strncmp(line
, "%%PageMedia:", 12) ||
1441 !strncmp(line
, "%%PageOrientation:", 18) ||
1442 !strncmp(line
, "%%PageProcessColors:", 20) ||
1443 !strncmp(line
, "%%PageRequirements:", 18) ||
1444 !strncmp(line
, "%%PageResources:", 16))
1451 else if (!strncmp(line
, "%%PageCustomColors:", 19))
1454 * %%PageCustomColors: ...
1457 else if (!strncmp(line
, "%%PageMedia:", 12))
1463 else if (!strncmp(line
, "%%PageOrientation:", 18))
1466 * %%PageOrientation: ...
1469 else if (!strncmp(line
, "%%PageProcessColors:", 20))
1472 * %%PageProcessColors: ...
1475 else if (!strncmp(line
, "%%PageRequirements:", 18))
1478 * %%PageRequirements: ...
1481 else if (!strncmp(line
, "%%PageResources:", 16))
1484 * %%PageResources: ...
1487 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1490 * %%IncludeFeature: *MainKeyword OptionKeyword
1493 if (doc
->number_up
== 1 &&!doc
->fitplot
)
1494 pageinfo
->num_options
= include_feature(ppd
, line
,
1495 pageinfo
->num_options
,
1496 &(pageinfo
->options
));
1498 else if (strncmp(line
, "%%Include", 9))
1502 if (doc
->number_up
== 1)
1505 * Update the document's composite and page bounding box...
1508 memcpy(pageinfo
->bounding_box
, bounding_box
,
1509 sizeof(pageinfo
->bounding_box
));
1511 if (bounding_box
[0] < doc
->new_bounding_box
[0])
1512 doc
->new_bounding_box
[0] = bounding_box
[0];
1513 if (bounding_box
[1] < doc
->new_bounding_box
[1])
1514 doc
->new_bounding_box
[1] = bounding_box
[1];
1515 if (bounding_box
[2] > doc
->new_bounding_box
[2])
1516 doc
->new_bounding_box
[2] = bounding_box
[2];
1517 if (bounding_box
[3] > doc
->new_bounding_box
[3])
1518 doc
->new_bounding_box
[3] = bounding_box
[3];
1522 * Output the page header as needed...
1525 if (!doc
->slow_order
&& first_page
)
1527 if (!ppd
|| !ppd
->num_filters
)
1528 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
1529 doc
->slow_collate
? 1 : doc
->copies
);
1531 if (doc
->number_up
> 1)
1533 printf("%%%%Page: (%d) %d\n", doc
->page
, doc
->page
);
1534 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1535 PageLeft
, PageBottom
, PageRight
, PageTop
);
1539 printf("%%%%Page: %s %d\n", pageinfo
->label
, doc
->page
);
1540 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1541 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
1542 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
1547 * Copy any page setup commands...
1551 doc_puts(doc
, "%%BeginPageSetup\n");
1553 if ((has_page_setup
= !strncmp(line
, "%%BeginPageSetup", 16)) != 0)
1555 int feature
= 0; /* In a Begin/EndFeature block? */
1557 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1559 if (!strncmp(line
, "%%EndPageSetup", 14))
1561 else if (!strncmp(line
, "%%BeginFeature:", 15))
1565 if (doc
->number_up
> 1 || doc
->fitplot
)
1568 else if (!strncmp(line
, "%%EndFeature", 12))
1572 if (doc
->number_up
> 1 || doc
->fitplot
)
1575 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1577 pageinfo
->num_options
= include_feature(ppd
, line
,
1578 pageinfo
->num_options
,
1579 &(pageinfo
->options
));
1582 else if (!strncmp(line
, "%%Include", 9))
1585 if (line
[0] != '%' && !feature
)
1588 if (!feature
|| (doc
->number_up
== 1 && !doc
->fitplot
))
1589 doc_write(doc
, line
, linelen
);
1593 * Skip %%EndPageSetup...
1596 if (linelen
> 0 && !strncmp(line
, "%%EndPageSetup", 14))
1598 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1605 char *page_setup
; /* PageSetup commands to send */
1608 if (pageinfo
->num_options
> 0)
1609 write_options(doc
, ppd
, pageinfo
->num_options
, pageinfo
->options
);
1612 * Output commands for the current page...
1615 page_setup
= ppdEmitString(ppd
, PPD_ORDER_PAGE
, 0);
1619 doc_puts(doc
, page_setup
);
1625 * Prep for the start of the page description...
1628 start_nup(doc
, number
, 1, bounding_box
);
1631 * Finish the PageSetup section as needed...
1636 int feature
= 0; /* In a Begin/EndFeature block? */
1638 doc_write(doc
, line
, linelen
);
1640 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1642 if (!strncmp(line
, "%%EndPageSetup", 14))
1644 else if (!strncmp(line
, "%%BeginFeature:", 15))
1648 if (doc
->number_up
> 1 || doc
->fitplot
)
1651 else if (!strncmp(line
, "%%EndFeature", 12))
1655 if (doc
->number_up
> 1 || doc
->fitplot
)
1658 else if (!strncmp(line
, "%%Include", 9))
1661 if (!feature
|| (doc
->number_up
== 1 && !doc
->fitplot
))
1662 doc_write(doc
, line
, linelen
);
1666 * Skip %%EndPageSetup...
1669 if (linelen
> 0 && !strncmp(line
, "%%EndPageSetup", 14))
1670 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1674 doc_puts(doc
, "%%EndPageSetup\n");
1677 * Read the rest of the page description...
1685 (!strncmp(line
, "%%Page:", 7) ||
1686 !strncmp(line
, "%%Trailer", 9) ||
1687 !strncmp(line
, "%%EOF", 5)))
1689 else if (!strncmp(line
, "%%BeginDocument", 15) ||
1690 !strncmp(line
, "%ADO_BeginApplication", 21))
1692 doc_write(doc
, line
, linelen
);
1696 else if ((!strncmp(line
, "%%EndDocument", 13) ||
1697 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
1699 doc_write(doc
, line
, linelen
);
1703 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
1704 (!strncmp(line
, "%%BeginData:", 12) &&
1705 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
1708 * Copy binary data...
1711 int bytes
; /* Bytes of data */
1714 doc_write(doc
, line
, linelen
);
1716 bytes
= atoi(strchr(line
, ':') + 1);
1720 if (bytes
> linesize
)
1721 linelen
= cupsFileRead(fp
, line
, linesize
);
1723 linelen
= cupsFileRead(fp
, line
, bytes
);
1728 perror("ERROR: Early end-of-file while reading binary data");
1732 doc_write(doc
, line
, linelen
);
1738 doc_write(doc
, line
, linelen
);
1740 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0);
1743 * Finish up this page and return...
1746 end_nup(doc
, number
);
1748 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
1755 * 'copy_prolog()' - Copy the document prolog section.
1757 * This function expects "line" to be filled with a %%BeginProlog comment line.
1758 * On return, "line" will contain the next line in the file, if any.
1761 static ssize_t
/* O - Length of next line */
1762 copy_prolog(cups_file_t
*fp
, /* I - File to read from */
1763 pstops_doc_t
*doc
, /* I - Document info */
1764 ppd_file_t
*ppd
, /* I - PPD file */
1765 char *line
, /* I - Line buffer */
1766 ssize_t linelen
, /* I - Length of initial line */
1767 size_t linesize
) /* I - Size of line buffer */
1769 while (strncmp(line
, "%%BeginProlog", 13))
1771 if (!strncmp(line
, "%%BeginSetup", 12) || !strncmp(line
, "%%Page:", 7))
1774 doc_write(doc
, line
, linelen
);
1776 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1780 doc_puts(doc
, "%%BeginProlog\n");
1782 do_prolog(doc
, ppd
);
1784 if (!strncmp(line
, "%%BeginProlog", 13))
1786 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1788 if (!strncmp(line
, "%%EndProlog", 11) ||
1789 !strncmp(line
, "%%BeginSetup", 12) ||
1790 !strncmp(line
, "%%Page:", 7))
1793 doc_write(doc
, line
, linelen
);
1796 if (!strncmp(line
, "%%EndProlog", 11))
1797 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1799 fputs("DEBUG: The %%EndProlog comment is missing.\n", stderr
);
1802 doc_puts(doc
, "%%EndProlog\n");
1809 * 'copy_setup()' - Copy the document setup section.
1811 * This function expects "line" to be filled with a %%BeginSetup comment line.
1812 * On return, "line" will contain the next line in the file, if any.
1815 static ssize_t
/* O - Length of next line */
1816 copy_setup(cups_file_t
*fp
, /* I - File to read from */
1817 pstops_doc_t
*doc
, /* I - Document info */
1818 ppd_file_t
*ppd
, /* I - PPD file */
1819 char *line
, /* I - Line buffer */
1820 ssize_t linelen
, /* I - Length of initial line */
1821 size_t linesize
) /* I - Size of line buffer */
1823 int num_options
; /* Number of options */
1824 cups_option_t
*options
; /* Options */
1827 while (strncmp(line
, "%%BeginSetup", 12))
1829 if (!strncmp(line
, "%%Page:", 7))
1832 doc_write(doc
, line
, linelen
);
1834 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1838 doc_puts(doc
, "%%BeginSetup\n");
1845 if (!strncmp(line
, "%%BeginSetup", 12))
1847 while (strncmp(line
, "%%EndSetup", 10))
1849 if (!strncmp(line
, "%%Page:", 7))
1851 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1854 * %%IncludeFeature: *MainKeyword OptionKeyword
1857 if (doc
->number_up
== 1 && !doc
->fitplot
)
1858 num_options
= include_feature(ppd
, line
, num_options
, &options
);
1860 else if (strncmp(line
, "%%BeginSetup", 12))
1861 doc_write(doc
, line
, linelen
);
1863 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1867 if (!strncmp(line
, "%%EndSetup", 10))
1868 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1870 fputs("DEBUG: The %%EndSetup comment is missing.\n", stderr
);
1873 if (num_options
> 0)
1875 write_options(doc
, ppd
, num_options
, options
);
1876 cupsFreeOptions(num_options
, options
);
1879 doc_puts(doc
, "%%EndSetup\n");
1886 * 'copy_trailer()' - Copy the document trailer.
1888 * This function expects "line" to be filled with a %%Trailer comment line.
1889 * On return, "line" will contain the next line in the file, if any.
1892 static ssize_t
/* O - Length of next line */
1893 copy_trailer(cups_file_t
*fp
, /* I - File to read from */
1894 pstops_doc_t
*doc
, /* I - Document info */
1895 ppd_file_t
*ppd
, /* I - PPD file */
1896 int number
, /* I - Number of pages */
1897 char *line
, /* I - Line buffer */
1898 ssize_t linelen
, /* I - Length of initial line */
1899 size_t linesize
) /* I - Size of line buffer */
1902 * Write the trailer comments...
1909 if (!strncmp(line
, "%%EOF", 5))
1911 else if (strncmp(line
, "%%Trailer", 9) &&
1912 strncmp(line
, "%%Pages:", 8) &&
1913 strncmp(line
, "%%BoundingBox:", 14))
1914 fwrite(line
, 1, linelen
, stdout
);
1916 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1919 fprintf(stderr
, "DEBUG: Wrote %d pages...\n", number
);
1921 printf("%%%%Pages: %d\n", number
);
1922 if (doc
->number_up
> 1 || doc
->fitplot
)
1923 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
1924 PageLeft
, PageBottom
, PageRight
, PageTop
);
1926 printf("%%%%BoundingBox: %d %d %d %d\n",
1927 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
1928 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
1935 * 'do_prolog()' - Send the necessary document prolog commands.
1939 do_prolog(pstops_doc_t
*doc
, /* I - Document information */
1940 ppd_file_t
*ppd
) /* I - PPD file */
1942 char *ps
; /* PS commands */
1946 * Send the document prolog commands...
1949 if (ppd
&& ppd
->patches
)
1951 doc_puts(doc
, "%%BeginFeature: *JobPatchFile 1\n");
1952 doc_puts(doc
, ppd
->patches
);
1953 doc_puts(doc
, "\n%%EndFeature\n");
1956 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_PROLOG
, 0.0)) != NULL
)
1963 * Define ESPshowpage here so that applications that define their
1964 * own procedure to do a showpage pick it up...
1967 if (doc
->use_ESPshowpage
)
1968 doc_puts(doc
, "userdict/ESPshowpage/showpage load put\n"
1969 "userdict/showpage{}put\n");
1974 * 'do_setup()' - Send the necessary document setup commands.
1978 do_setup(pstops_doc_t
*doc
, /* I - Document information */
1979 ppd_file_t
*ppd
) /* I - PPD file */
1981 char *ps
; /* PS commands */
1985 * Disable CTRL-D so that embedded files don't cause printing
1989 doc_puts(doc
, "% Disable CTRL-D as an end-of-file marker...\n");
1990 doc_puts(doc
, "userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put\n");
1993 * Mark job options...
1996 cupsMarkOptions(ppd
, doc
->num_options
, doc
->options
);
1999 * Send all the printer-specific setup commands...
2002 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, 0.0)) != NULL
)
2008 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_ANY
, 0.0)) != NULL
)
2015 * Set the number of copies for the job...
2018 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
2020 doc_printf(doc
, "%%RBIBeginNonPPDFeature: *NumCopies %d\n", doc
->copies
);
2022 "%d/languagelevel where{pop languagelevel 2 ge}{false}ifelse\n"
2023 "{1 dict begin/NumCopies exch def currentdict end "
2025 "{userdict/#copies 3 -1 roll put}ifelse\n", doc
->copies
);
2026 doc_puts(doc
, "%RBIEndNonPPDFeature\n");
2030 * If we are doing N-up printing, disable setpagedevice...
2033 if (doc
->number_up
> 1)
2035 doc_puts(doc
, "userdict/CUPSsetpagedevice/setpagedevice load put\n");
2036 doc_puts(doc
, "userdict/setpagedevice{pop}bind put\n");
2040 * Changes to the transfer function must be made AFTER any
2041 * setpagedevice code...
2044 if (doc
->gamma
!= 1.0f
|| doc
->brightness
!= 1.0f
)
2045 doc_printf(doc
, "{ neg 1 add dup 0 lt { pop 1 } { %.3f exp neg 1 add } "
2046 "ifelse %.3f mul } bind settransfer\n",
2047 doc
->gamma
, doc
->brightness
);
2050 * Make sure we have rectclip and rectstroke procedures of some sort...
2054 "% x y w h ESPrc - Clip to a rectangle.\n"
2055 "userdict/ESPrc/rectclip where{pop/rectclip load}\n"
2056 "{{newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
2057 "neg 0 rlineto closepath clip newpath}bind}ifelse put\n");
2060 "% x y w h ESPrf - Fill a rectangle.\n"
2061 "userdict/ESPrf/rectfill where{pop/rectfill load}\n"
2062 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
2063 "neg 0 rlineto closepath fill grestore}bind}ifelse put\n");
2066 "% x y w h ESPrs - Stroke a rectangle.\n"
2067 "userdict/ESPrs/rectstroke where{pop/rectstroke load}\n"
2068 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
2069 "neg 0 rlineto closepath stroke grestore}bind}ifelse put\n");
2072 * Write the page and label prologs...
2075 if (doc
->number_up
== 2 || doc
->number_up
== 6)
2078 * For 2- and 6-up output, rotate the labels to match the orientation
2082 if (Orientation
& 1)
2083 write_label_prolog(doc
, doc
->page_label
, PageBottom
,
2084 PageWidth
- PageLength
+ PageTop
, PageLength
);
2086 write_label_prolog(doc
, doc
->page_label
, PageLeft
, PageRight
,
2090 write_label_prolog(doc
, doc
->page_label
, PageBottom
, PageTop
, PageWidth
);
2095 * 'doc_printf()' - Send a formatted string to stdout and/or the temp file.
2097 * This function should be used for all page-level output that is affected
2098 * by ordering, collation, etc.
2102 doc_printf(pstops_doc_t
*doc
, /* I - Document information */
2103 const char *format
, /* I - Printf-style format string */
2104 ...) /* I - Additional arguments as needed */
2106 va_list ap
; /* Pointer to arguments */
2107 char buffer
[1024]; /* Output buffer */
2108 size_t bytes
; /* Number of bytes to write */
2111 va_start(ap
, format
);
2112 bytes
= vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
2115 if (bytes
> sizeof(buffer
))
2117 _cupsLangPrintf(stderr
,
2118 _("ERROR: doc_printf overflow (%d bytes) detected, "
2119 "aborting\n"), (int)bytes
);
2123 doc_write(doc
, buffer
, bytes
);
2128 * 'doc_puts()' - Send a nul-terminated string to stdout and/or the temp file.
2130 * This function should be used for all page-level output that is affected
2131 * by ordering, collation, etc.
2135 doc_puts(pstops_doc_t
*doc
, /* I - Document information */
2136 const char *s
) /* I - String to send */
2138 doc_write(doc
, s
, strlen(s
));
2143 * 'doc_write()' - Send data to stdout and/or the temp file.
2147 doc_write(pstops_doc_t
*doc
, /* I - Document information */
2148 const char *s
, /* I - Data to send */
2149 size_t len
) /* I - Number of bytes to send */
2151 if (!doc
->slow_order
)
2152 fwrite(s
, 1, len
, stdout
);
2155 cupsFileWrite(doc
->temp
, s
, len
);
2160 * 'end_nup()' - End processing for N-up printing.
2164 end_nup(pstops_doc_t
*doc
, /* I - Document information */
2165 int number
) /* I - Page number */
2167 if (doc
->number_up
> 1)
2168 doc_puts(doc
, "userdict/ESPsave get restore\n");
2170 switch (doc
->number_up
)
2173 if (doc
->use_ESPshowpage
)
2175 write_labels(doc
, Orientation
);
2176 doc_puts(doc
, "ESPshowpage\n");
2182 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2184 if (Orientation
& 1)
2187 * Rotate the labels back to portrait...
2190 write_labels(doc
, Orientation
- 1);
2192 else if (Orientation
== 0)
2195 * Rotate the labels to landscape...
2198 write_labels(doc
, doc
->normal_landscape
? 1 : 3);
2203 * Rotate the labels to landscape...
2206 write_labels(doc
, doc
->normal_landscape
? 3 : 1);
2209 doc_puts(doc
, "ESPshowpage\n");
2214 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2216 write_labels(doc
, Orientation
);
2217 doc_puts(doc
, "ESPshowpage\n");
2227 * 'include_feature()' - Include a printer option/feature command.
2230 static int /* O - New number of options */
2232 ppd_file_t
*ppd
, /* I - PPD file */
2233 const char *line
, /* I - DSC line */
2234 int num_options
, /* I - Number of options */
2235 cups_option_t
**options
) /* IO - Options */
2237 char name
[255], /* Option name */
2238 value
[255]; /* Option value */
2239 ppd_option_t
*option
; /* Option in file */
2243 * Get the "%%IncludeFeature: *Keyword OptionKeyword" values...
2246 if (sscanf(line
+ 17, "%254s%254s", name
, value
) != 2)
2248 fputs("DEBUG: The %%IncludeFeature: comment is not valid.\n", stderr
);
2249 return (num_options
);
2253 * Find the option and choice...
2256 if ((option
= ppdFindOption(ppd
, name
+ 1)) == NULL
)
2258 _cupsLangPrintf(stderr
, _("WARNING: Unknown option \"%s\"\n"), name
+ 1);
2259 return (num_options
);
2262 if (option
->section
== PPD_ORDER_EXIT
||
2263 option
->section
== PPD_ORDER_JCL
)
2265 _cupsLangPrintf(stderr
, _("WARNING: Option \"%s\" cannot be included via "
2266 "IncludeFeature\n"), name
+ 1);
2267 return (num_options
);
2270 if (!ppdFindChoice(option
, value
))
2272 _cupsLangPrintf(stderr
,
2273 _("WARNING: Unknown choice \"%s\" for option \"%s\"\n"),
2275 return (num_options
);
2279 * Add the option to the option array and return...
2282 return (cupsAddOption(name
+ 1, value
, num_options
, options
));
2287 * 'parse_text()' - Parse a text value in a comment.
2289 * This function parses a DSC text value as defined on page 36 of the
2290 * DSC specification. Text values are either surrounded by parenthesis
2291 * or whitespace-delimited.
2293 * The value returned is the literal characters for the entire text
2294 * string, including any parenthesis and escape characters.
2297 static char * /* O - Value or NULL on error */
2298 parse_text(const char *start
, /* I - Start of text value */
2299 char **end
, /* O - End of text value */
2300 char *buffer
, /* I - Buffer */
2301 size_t bufsize
) /* I - Size of buffer */
2303 char *bufptr
, /* Pointer in buffer */
2304 *bufend
; /* End of buffer */
2305 int level
; /* Parenthesis level */
2309 * Skip leading whitespace...
2312 while (isspace(*start
& 255))
2316 * Then copy the value...
2321 bufend
= buffer
+ bufsize
- 1;
2323 while (bufptr
< bufend
)
2325 if (isspace(*start
& 255) && !level
)
2332 else if (*start
== ')')
2342 else if (*start
== '\\')
2345 * Copy escaped character...
2348 int i
; /* Looping var */
2352 i
<= 3 && isdigit(start
[i
] & 255) && bufptr
< bufend
;
2353 *bufptr
++ = start
[i
], i
++);
2362 * Return the value and new pointer into the line...
2366 *end
= (char *)start
;
2368 if (bufptr
== bufend
)
2376 * 'set_pstops_options()' - Set pstops options.
2381 pstops_doc_t
*doc
, /* I - Document information */
2382 ppd_file_t
*ppd
, /* I - PPD file */
2383 char *argv
[], /* I - Command-line arguments */
2384 int num_options
, /* I - Number of options */
2385 cups_option_t
*options
) /* I - Options */
2387 const char *val
; /* Option value */
2388 int intval
; /* Integer option value */
2389 ppd_attr_t
*attr
; /* PPD attribute */
2390 ppd_option_t
*option
; /* PPD option */
2391 ppd_choice_t
*choice
; /* PPD choice */
2395 * Initialize document information structure...
2398 memset(doc
, 0, sizeof(pstops_doc_t
));
2400 doc
->job_id
= atoi(argv
[1]);
2401 doc
->user
= argv
[2];
2402 doc
->title
= argv
[3];
2403 doc
->copies
= atoi(argv
[4]);
2405 if (ppd
&& ppd
->landscape
> 0)
2406 doc
->normal_landscape
= 1;
2408 doc
->bounding_box
[0] = (int)PageLeft
;
2409 doc
->bounding_box
[1] = (int)PageBottom
;
2410 doc
->bounding_box
[2] = (int)PageRight
;
2411 doc
->bounding_box
[3] = (int)PageTop
;
2413 doc
->new_bounding_box
[0] = INT_MAX
;
2414 doc
->new_bounding_box
[1] = INT_MAX
;
2415 doc
->new_bounding_box
[2] = INT_MIN
;
2416 doc
->new_bounding_box
[3] = INT_MIN
;
2419 * AP_FIRSTPAGE_* and the corresponding non-first-page options.
2422 doc
->ap_input_slot
= cupsGetOption("AP_FIRSTPAGE_InputSlot", num_options
,
2424 doc
->ap_manual_feed
= cupsGetOption("AP_FIRSTPAGE_ManualFeed", num_options
,
2426 doc
->ap_media_color
= cupsGetOption("AP_FIRSTPAGE_MediaColor", num_options
,
2428 doc
->ap_media_type
= cupsGetOption("AP_FIRSTPAGE_MediaType", num_options
,
2430 doc
->ap_page_region
= cupsGetOption("AP_FIRSTPAGE_PageRegion", num_options
,
2432 doc
->ap_page_size
= cupsGetOption("AP_FIRSTPAGE_PageSize", num_options
,
2435 if ((choice
= ppdFindMarkedChoice(ppd
, "InputSlot")) != NULL
)
2436 doc
->input_slot
= choice
->choice
;
2437 if ((choice
= ppdFindMarkedChoice(ppd
, "ManualFeed")) != NULL
)
2438 doc
->manual_feed
= choice
->choice
;
2439 if ((choice
= ppdFindMarkedChoice(ppd
, "MediaColor")) != NULL
)
2440 doc
->media_color
= choice
->choice
;
2441 if ((choice
= ppdFindMarkedChoice(ppd
, "MediaType")) != NULL
)
2442 doc
->media_type
= choice
->choice
;
2443 if ((choice
= ppdFindMarkedChoice(ppd
, "PageRegion")) != NULL
)
2444 doc
->page_region
= choice
->choice
;
2445 if ((choice
= ppdFindMarkedChoice(ppd
, "PageSize")) != NULL
)
2446 doc
->page_size
= choice
->choice
;
2453 if ((val
= cupsGetOption("brightness", num_options
, options
)) != NULL
)
2456 * Get brightness value from 10 to 1000.
2461 if (intval
< 10 || intval
> 1000)
2463 _cupsLangPrintf(stderr
, _("ERROR: Unsupported brightness value %s, using "
2464 "brightness=100\n"), val
);
2465 doc
->brightness
= 1.0f
;
2468 doc
->brightness
= intval
* 0.01f
;
2471 doc
->brightness
= 1.0f
;
2474 * collate, multiple-document-handling
2477 if ((val
= cupsGetOption("multiple-document-handling", num_options
, options
)) != NULL
)
2480 * This IPP attribute is unnecessarily complicated...
2482 * single-document, separate-documents-collated-copies, and
2483 * single-document-new-sheet all require collated copies.
2485 * separate-documents-uncollated-copies allows for uncollated copies.
2488 doc
->collate
= strcasecmp(val
, "separate-documents-uncollated-copies") != 0;
2491 if ((val
= cupsGetOption("Collate", num_options
, options
)) != NULL
&&
2492 (!strcasecmp(val
, "true") ||!strcasecmp(val
, "on") ||
2493 !strcasecmp(val
, "yes")))
2500 if ((val
= cupsGetOption("emit-jcl", num_options
, options
)) != NULL
&&
2501 (!strcasecmp(val
, "false") || !strcasecmp(val
, "off") ||
2502 !strcasecmp(val
, "no") || !strcmp(val
, "0")))
2508 * fitplot/fit-to-page
2511 if ((val
= cupsGetOption("fitplot", num_options
, options
)) != NULL
&&
2512 !strcasecmp(val
, "true"))
2514 else if ((val
= cupsGetOption("fit-to-page", num_options
, options
)) != NULL
&&
2515 !strcasecmp(val
, "true"))
2522 if ((val
= cupsGetOption("gamma", num_options
, options
)) != NULL
)
2525 * Get gamma value from 1 to 10000...
2530 if (intval
< 1 || intval
> 10000)
2532 _cupsLangPrintf(stderr
, _("ERROR: Unsupported gamma value %s, using "
2533 "gamma=1000\n"), val
);
2537 doc
->gamma
= intval
* 0.001f
;
2543 * mirror/MirrorPrint
2546 if ((choice
= ppdFindMarkedChoice(ppd
, "MirrorPrint")) != NULL
)
2548 val
= choice
->choice
;
2552 val
= cupsGetOption("mirror", num_options
, options
);
2554 if (val
&& (!strcasecmp(val
, "true") || !strcasecmp(val
, "on") ||
2555 !strcasecmp(val
, "yes")))
2562 if ((val
= cupsGetOption("number-up", num_options
, options
)) != NULL
)
2564 switch (intval
= atoi(val
))
2572 doc
->number_up
= intval
;
2575 _cupsLangPrintf(stderr
,
2576 _("ERROR: Unsupported number-up value %d, using "
2577 "number-up=1\n"), intval
);
2589 if ((val
= cupsGetOption("number-up-layout", num_options
, options
)) != NULL
)
2591 if (!strcasecmp(val
, "lrtb"))
2592 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2593 else if (!strcasecmp(val
, "lrbt"))
2594 doc
->number_up_layout
= PSTOPS_LAYOUT_LRBT
;
2595 else if (!strcasecmp(val
, "rltb"))
2596 doc
->number_up_layout
= PSTOPS_LAYOUT_RLTB
;
2597 else if (!strcasecmp(val
, "rlbt"))
2598 doc
->number_up_layout
= PSTOPS_LAYOUT_RLBT
;
2599 else if (!strcasecmp(val
, "tblr"))
2600 doc
->number_up_layout
= PSTOPS_LAYOUT_TBLR
;
2601 else if (!strcasecmp(val
, "tbrl"))
2602 doc
->number_up_layout
= PSTOPS_LAYOUT_TBRL
;
2603 else if (!strcasecmp(val
, "btlr"))
2604 doc
->number_up_layout
= PSTOPS_LAYOUT_BTLR
;
2605 else if (!strcasecmp(val
, "btrl"))
2606 doc
->number_up_layout
= PSTOPS_LAYOUT_BTRL
;
2609 _cupsLangPrintf(stderr
, _("ERROR: Unsupported number-up-layout value %s, "
2610 "using number-up-layout=lrtb\n"), val
);
2611 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2615 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2621 if ((val
= cupsGetOption("OutputOrder", num_options
, options
)) != NULL
)
2623 if (!strcasecmp(val
, "Reverse"))
2624 doc
->output_order
= 1;
2629 * Figure out the right default output order from the PPD file...
2632 if ((choice
= ppdFindMarkedChoice(ppd
, "OutputBin")) != NULL
&&
2633 (attr
= ppdFindAttr(ppd
, "PageStackOrder", choice
->choice
)) != NULL
&&
2635 doc
->output_order
= !strcasecmp(attr
->value
, "Reverse");
2636 else if ((attr
= ppdFindAttr(ppd
, "DefaultOutputOrder", NULL
)) != NULL
&&
2638 doc
->output_order
= !strcasecmp(attr
->value
, "Reverse");
2645 if ((val
= cupsGetOption("page-border", num_options
, options
)) != NULL
)
2647 if (!strcasecmp(val
, "none"))
2648 doc
->page_border
= PSTOPS_BORDERNONE
;
2649 else if (!strcasecmp(val
, "single"))
2650 doc
->page_border
= PSTOPS_BORDERSINGLE
;
2651 else if (!strcasecmp(val
, "single-thick"))
2652 doc
->page_border
= PSTOPS_BORDERSINGLE2
;
2653 else if (!strcasecmp(val
, "double"))
2654 doc
->page_border
= PSTOPS_BORDERDOUBLE
;
2655 else if (!strcasecmp(val
, "double-thick"))
2656 doc
->page_border
= PSTOPS_BORDERDOUBLE2
;
2659 _cupsLangPrintf(stderr
, _("ERROR: Unsupported page-border value %s, "
2660 "using page-border=none\n"), val
);
2661 doc
->page_border
= PSTOPS_BORDERNONE
;
2665 doc
->page_border
= PSTOPS_BORDERNONE
;
2671 doc
->page_label
= cupsGetOption("page-label", num_options
, options
);
2677 doc
->page_ranges
= cupsGetOption("page-ranges", num_options
, options
);
2683 doc
->page_set
= cupsGetOption("page-set", num_options
, options
);
2686 * Now figure out if we have to force collated copies, etc.
2689 if (ppd
&& ppd
->manual_copies
&& Duplex
&& doc
->copies
> 1)
2692 * Force collated copies when printing a duplexed document to
2693 * a non-PS printer that doesn't do hardware copy generation.
2694 * Otherwise the copies will end up on the front/back side of
2702 * See if we have to filter the fast or slow way...
2705 if (doc
->collate
&& doc
->copies
> 1)
2708 * See if we need to manually collate the pages...
2711 doc
->slow_collate
= 1;
2713 if ((choice
= ppdFindMarkedChoice(ppd
, "Collate")) != NULL
&&
2714 !strcasecmp(choice
->choice
, "True"))
2717 * Hardware collate option is selected, see if the option is
2718 * conflicting - if not, collate in hardware. Otherwise,
2719 * turn the hardware collate option off...
2722 if ((option
= ppdFindOption(ppd
, "Collate")) != NULL
&&
2723 !option
->conflicted
)
2724 doc
->slow_collate
= 0;
2726 ppdMarkOption(ppd
, "Collate", "False");
2730 doc
->slow_collate
= 0;
2732 if (!ppdFindOption(ppd
, "OutputOrder") && doc
->output_order
)
2733 doc
->slow_order
= 1;
2735 doc
->slow_order
= 0;
2738 (doc
->slow_collate
|| doc
->slow_order
||
2739 ((attr
= ppdFindAttr(ppd
, "cupsEvenDuplex", NULL
)) != NULL
&&
2740 attr
->value
&& !strcasecmp(attr
->value
, "true"))))
2741 doc
->slow_duplex
= 1;
2743 doc
->slow_duplex
= 0;
2746 * Create a temporary file for page data if we need to filter slowly...
2749 if (doc
->slow_order
|| doc
->slow_collate
)
2751 if ((doc
->temp
= cupsTempFile2(doc
->tempfile
,
2752 sizeof(doc
->tempfile
))) == NULL
)
2754 perror("DEBUG: Unable to create temporary file");
2760 * Figure out if we should use ESPshowpage or not...
2763 if (doc
->page_label
|| getenv("CLASSIFICATION") || doc
->number_up
> 1 ||
2767 * Yes, use ESPshowpage...
2770 doc
->use_ESPshowpage
= 1;
2773 fprintf(stderr
, "DEBUG: slow_collate=%d, slow_duplex=%d, slow_order=%d\n",
2774 doc
->slow_collate
, doc
->slow_duplex
, doc
->slow_order
);
2779 * 'skip_page()' - Skip past a page that won't be printed.
2782 static ssize_t
/* O - Length of next line */
2783 skip_page(cups_file_t
*fp
, /* I - File to read from */
2784 char *line
, /* I - Line buffer */
2785 ssize_t linelen
, /* I - Length of initial line */
2786 size_t linesize
) /* I - Size of line buffer */
2788 int level
; /* Embedded document level */
2793 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
2796 (!strncmp(line
, "%%Page:", 7) || !strncmp(line
, "%%Trailer", 9)))
2798 else if (!strncmp(line
, "%%BeginDocument", 15) ||
2799 !strncmp(line
, "%ADO_BeginApplication", 21))
2801 else if ((!strncmp(line
, "%%EndDocument", 13) ||
2802 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
2804 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
2805 (!strncmp(line
, "%%BeginData:", 12) &&
2806 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
2809 * Skip binary data...
2812 int bytes
; /* Bytes of data */
2815 bytes
= atoi(strchr(line
, ':') + 1);
2819 if (bytes
> linesize
)
2820 linelen
= cupsFileRead(fp
, line
, linesize
);
2822 linelen
= cupsFileRead(fp
, line
, bytes
);
2827 perror("ERROR: Early end-of-file while reading binary data");
2841 * 'start_nup()' - Start processing for N-up printing.
2845 start_nup(pstops_doc_t
*doc
, /* I - Document information */
2846 int number
, /* I - Page number */
2847 int show_border
, /* I - Show the border? */
2848 const int *bounding_box
) /* I - BoundingBox value */
2850 int pos
; /* Position on page */
2851 int x
, y
; /* Relative position of subpage */
2852 float w
, l
, /* Width and length of subpage */
2853 tx
, ty
; /* Translation values for subpage */
2854 float pagew
, /* Printable width of page */
2855 pagel
; /* Printable height of page */
2856 int bboxx
, /* BoundingBox X origin */
2857 bboxy
, /* BoundingBox Y origin */
2858 bboxw
, /* BoundingBox width */
2859 bboxl
; /* BoundingBox height */
2860 float margin
= 0; /* Current margin for border */
2863 if (doc
->number_up
> 1)
2864 doc_puts(doc
, "userdict/ESPsave save put\n");
2866 pos
= (number
- 1) % doc
->number_up
;
2867 pagew
= PageRight
- PageLeft
;
2868 pagel
= PageTop
- PageBottom
;
2872 bboxx
= bounding_box
[0];
2873 bboxy
= bounding_box
[1];
2874 bboxw
= bounding_box
[2] - bounding_box
[0];
2875 bboxl
= bounding_box
[3] - bounding_box
[1];
2885 fprintf(stderr
, "DEBUG: pagew = %.1f, pagel = %.1f\n", pagew
, pagel
);
2886 fprintf(stderr
, "DEBUG: bboxx = %d, bboxy = %d, bboxw = %d, bboxl = %d\n",
2887 bboxx
, bboxy
, bboxw
, bboxl
);
2888 fprintf(stderr
, "DEBUG: PageLeft = %.1f, PageRight = %.1f\n",
2889 PageLeft
, PageRight
);
2890 fprintf(stderr
, "DEBUG: PageTop = %.1f, PageBottom = %.1f\n",
2891 PageTop
, PageBottom
);
2892 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
2893 PageWidth
, PageLength
);
2895 switch (Orientation
)
2897 case 1 : /* Landscape */
2898 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", PageLength
);
2900 case 2 : /* Reverse Portrait */
2901 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", PageWidth
,
2904 case 3 : /* Reverse Landscape */
2905 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", PageWidth
);
2909 if (Duplex
&& doc
->number_up
> 1 && ((number
/ doc
->number_up
) & 1))
2910 doc_printf(doc
, "%.1f %.1f translate\n", PageWidth
- PageRight
, PageBottom
);
2911 else if (doc
->number_up
> 1 || doc
->fitplot
)
2912 doc_printf(doc
, "%.1f %.1f translate\n", PageLeft
, PageBottom
);
2915 doc_printf(doc
, "%.1f 0.0 translate -1 1 scale\n", PageWidth
);
2917 switch (doc
->number_up
)
2923 l
= w
* bboxl
/ bboxw
;
2928 w
= l
* bboxw
/ bboxl
;
2931 tx
= 0.5 * (pagew
- w
);
2932 ty
= 0.5 * (pagel
- l
);
2934 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n", tx
, ty
,
2935 w
/ bboxw
, l
/ bboxl
);
2942 if (Orientation
& 1)
2946 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2950 l
= w
* bboxl
/ bboxw
;
2952 if (l
> (pagew
* 0.5))
2955 w
= l
* bboxw
/ bboxl
;
2958 tx
= 0.5 * (pagew
* 0.5 - l
);
2959 ty
= 0.5 * (pagel
- w
);
2961 if (doc
->normal_landscape
)
2962 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2964 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2966 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2967 ty
, tx
+ pagew
* 0.5 * x
, w
/ bboxw
, l
/ bboxl
);
2973 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2977 w
= l
* bboxw
/ bboxl
;
2979 if (w
> (pagel
* 0.5))
2982 l
= w
* bboxl
/ bboxw
;
2985 tx
= 0.5 * (pagel
* 0.5 - w
);
2986 ty
= 0.5 * (pagew
- l
);
2988 if (doc
->normal_landscape
)
2989 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2991 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2993 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2994 tx
+ pagel
* 0.5 * x
, ty
, w
/ bboxw
, l
/ bboxl
);
2999 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3010 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3013 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3017 l
= w
* bboxl
/ bboxw
;
3019 if (l
> (pagel
* 0.5))
3022 w
= l
* bboxw
/ bboxl
;
3025 tx
= 0.5 * (pagew
* 0.5 - w
);
3026 ty
= 0.5 * (pagel
* 0.5 - l
);
3028 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3029 tx
+ x
* pagew
* 0.5, ty
+ y
* pagel
* 0.5,
3030 w
/ bboxw
, l
/ bboxl
);
3034 if (Orientation
& 1)
3036 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3041 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3044 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3052 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3055 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3060 l
= w
* bboxl
/ bboxw
;
3062 if (l
> (pagew
* 0.333))
3065 w
= l
* bboxw
/ bboxl
;
3068 tx
= 0.5 * (pagel
- 2 * w
);
3069 ty
= 0.5 * (pagew
- 3 * l
);
3071 if (doc
->normal_landscape
)
3072 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
3074 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
3076 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3077 tx
+ x
* w
, ty
+ y
* l
, l
/ bboxl
, w
/ bboxw
);
3081 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3086 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3089 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3097 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3100 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3105 w
= l
* bboxw
/ bboxl
;
3107 if (w
> (pagel
* 0.333))
3110 l
= w
* bboxl
/ bboxw
;
3113 tx
= 0.5 * (pagel
- 3 * w
);
3114 ty
= 0.5 * (pagew
- 2 * l
);
3116 if (doc
->normal_landscape
)
3117 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
3119 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
3121 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3122 tx
+ w
* x
, ty
+ l
* y
, 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.333))
3151 w
= l
* bboxw
/ bboxl
;
3154 tx
= 0.5 * (pagew
* 0.333 - w
);
3155 ty
= 0.5 * (pagel
* 0.333 - l
);
3157 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3158 tx
+ x
* pagew
* 0.333, ty
+ y
* pagel
* 0.333,
3159 w
/ bboxw
, l
/ bboxl
);
3163 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3174 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3177 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3181 l
= w
* bboxl
/ bboxw
;
3183 if (l
> (pagel
* 0.25))
3186 w
= l
* bboxw
/ bboxl
;
3189 tx
= 0.5 * (pagew
* 0.25 - w
);
3190 ty
= 0.5 * (pagel
* 0.25 - l
);
3192 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3193 tx
+ x
* pagew
* 0.25, ty
+ y
* pagel
* 0.25,
3194 w
/ bboxw
, l
/ bboxl
);
3199 * Draw borders as necessary...
3202 if (doc
->page_border
&& show_border
)
3204 int rects
; /* Number of border rectangles */
3205 float fscale
; /* Scaling value for points */
3208 rects
= (doc
->page_border
& PSTOPS_BORDERDOUBLE
) ? 2 : 1;
3209 fscale
= PageWidth
/ w
;
3210 margin
= 2.25 * fscale
;
3213 * Set the line width and color...
3216 doc_puts(doc
, "gsave\n");
3217 doc_printf(doc
, "%.3f setlinewidth 0 setgray newpath\n",
3218 (doc
->page_border
& PSTOPS_BORDERTHICK
) ? 0.5 * fscale
:
3222 * Draw border boxes...
3225 for (; rects
> 0; rects
--, margin
+= 2 * fscale
)
3226 if (doc
->number_up
> 1)
3227 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3231 bboxl
- 2 * margin
);
3233 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3235 PageBottom
+ margin
,
3236 PageRight
- PageLeft
- 2 * margin
,
3237 PageTop
- PageBottom
- 2 * margin
);
3240 * Restore pen settings...
3243 doc_puts(doc
, "grestore\n");
3249 * Offset the page by its bounding box...
3252 doc_printf(doc
, "%d %d translate\n", -bounding_box
[0],
3256 if (doc
->fitplot
|| doc
->number_up
> 1)
3259 * Clip the page to the page's bounding box...
3262 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrc\n",
3263 bboxx
+ margin
, bboxy
+ margin
,
3264 bboxw
- 2 * margin
, bboxl
- 2 * margin
);
3270 * 'write_label_prolog()' - Write the prolog with the classification
3275 write_label_prolog(pstops_doc_t
*doc
, /* I - Document info */
3276 const char *label
, /* I - Page label */
3277 float bottom
, /* I - Bottom position in points */
3278 float top
, /* I - Top position in points */
3279 float width
) /* I - Width in points */
3281 const char *classification
; /* CLASSIFICATION environment variable */
3282 const char *ptr
; /* Temporary string pointer */
3286 * First get the current classification...
3289 if ((classification
= getenv("CLASSIFICATION")) == NULL
)
3290 classification
= "";
3291 if (strcmp(classification
, "none") == 0)
3292 classification
= "";
3295 * If there is nothing to show, bind an empty 'write labels' procedure
3299 if (!classification
[0] && (label
== NULL
|| !label
[0]))
3301 doc_puts(doc
, "userdict/ESPwl{}bind put\n");
3306 * Set the classification + page label string...
3309 doc_puts(doc
, "userdict");
3310 if (!strcmp(classification
, "confidential"))
3311 doc_puts(doc
, "/ESPpl(CONFIDENTIAL");
3312 else if (!strcmp(classification
, "classified"))
3313 doc_puts(doc
, "/ESPpl(CLASSIFIED");
3314 else if (!strcmp(classification
, "secret"))
3315 doc_puts(doc
, "/ESPpl(SECRET");
3316 else if (!strcmp(classification
, "topsecret"))
3317 doc_puts(doc
, "/ESPpl(TOP SECRET");
3318 else if (!strcmp(classification
, "unclassified"))
3319 doc_puts(doc
, "/ESPpl(UNCLASSIFIED");
3322 doc_puts(doc
, "/ESPpl(");
3324 for (ptr
= classification
; *ptr
; ptr
++)
3326 if (*ptr
< 32 || *ptr
> 126)
3327 doc_printf(doc
, "\\%03o", *ptr
);
3328 else if (*ptr
== '_')
3330 else if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
3331 doc_printf(doc
, "\\%c", *ptr
);
3333 doc_printf(doc
, "%c", *ptr
);
3339 if (classification
[0])
3340 doc_puts(doc
, " - ");
3343 * Quote the label string as needed...
3346 for (ptr
= label
; *ptr
; ptr
++)
3348 if (*ptr
< 32 || *ptr
> 126)
3349 doc_printf(doc
, "\\%03o", *ptr
);
3350 else if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
3351 doc_printf(doc
, "\\%c", *ptr
);
3353 doc_printf(doc
, "%c", *ptr
);
3357 doc_puts(doc
, ")put\n");
3360 * Then get a 14 point Helvetica-Bold font...
3363 doc_puts(doc
, "userdict/ESPpf /Helvetica-Bold findfont 14 scalefont put\n");
3366 * Finally, the procedure to write the labels on the page...
3369 doc_puts(doc
, "userdict/ESPwl{\n");
3370 doc_puts(doc
, " ESPpf setfont\n");
3371 doc_printf(doc
, " ESPpl stringwidth pop dup 12 add exch -0.5 mul %.0f add\n",
3373 doc_puts(doc
, " 1 setgray\n");
3374 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrf\n", bottom
- 2.0);
3375 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrf\n", top
- 18.0);
3376 doc_puts(doc
, " 0 setgray\n");
3377 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrs\n", bottom
- 2.0);
3378 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrs\n", top
- 18.0);
3379 doc_printf(doc
, " dup %.0f moveto ESPpl show\n", bottom
+ 2.0);
3380 doc_printf(doc
, " %.0f moveto ESPpl show\n", top
- 14.0);
3381 doc_puts(doc
, "pop\n");
3382 doc_puts(doc
, "}bind put\n");
3387 * 'write_labels()' - Write the actual page labels.
3389 * This function is a copy of the one in common.c since we need to
3390 * use doc_puts/doc_printf instead of puts/printf...
3394 write_labels(pstops_doc_t
*doc
, /* I - Document information */
3395 int orient
) /* I - Orientation of the page */
3397 float width
, /* Width of page */
3398 length
; /* Length of page */
3401 doc_puts(doc
, "gsave\n");
3403 if ((orient
^ Orientation
) & 1)
3411 length
= PageLength
;
3416 case 1 : /* Landscape */
3417 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", length
);
3419 case 2 : /* Reverse Portrait */
3420 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", width
, length
);
3422 case 3 : /* Reverse Landscape */
3423 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", width
);
3427 doc_puts(doc
, "ESPwl\n");
3428 doc_puts(doc
, "grestore\n");
3433 * 'write_options()' - Write options provided via %%IncludeFeature.
3438 pstops_doc_t
*doc
, /* I - Document */
3439 ppd_file_t
*ppd
, /* I - PPD file */
3440 int num_options
, /* I - Number of options */
3441 cups_option_t
*options
) /* I - Options */
3443 int i
; /* Looping var */
3444 ppd_option_t
*option
; /* PPD option */
3445 int min_order
; /* Minimum OrderDependency value */
3446 char *doc_setup
, /* DocumentSetup commands to send */
3447 *any_setup
; /* AnySetup commands to send */
3451 * Figure out the minimum OrderDependency value...
3454 if ((option
= ppdFindOption(ppd
, "PageRegion")) != NULL
)
3455 min_order
= option
->order
;
3459 for (i
= 0; i
< num_options
; i
++)
3460 if ((option
= ppdFindOption(ppd
, options
[i
].name
)) != NULL
&&
3461 option
->order
< min_order
)
3462 min_order
= option
->order
;
3465 * Mark and extract them...
3468 cupsMarkOptions(ppd
, num_options
, options
);
3470 doc_setup
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, min_order
);
3471 any_setup
= ppdEmitString(ppd
, PPD_ORDER_ANY
, min_order
);
3474 * Then send them out...
3477 if (doc
->number_up
> 1)
3480 * Temporarily restore setpagedevice so we can set the options...
3483 doc_puts(doc
, "userdict/setpagedevice/CUPSsetpagedevice load put\n");
3488 doc_puts(doc
, doc_setup
);
3494 doc_puts(doc
, any_setup
);
3498 if (doc
->number_up
> 1)
3501 * Disable setpagedevice again...
3504 doc_puts(doc
, "userdict/setpagedevice{pop}bind put\n");
3510 * End of "$Id: pstops.c 7977 2008-09-23 23:44:33Z mike $".