]>
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"),
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 _cupsLangPrintError("ERROR", _("Unable to open print file"));
305 * Read the first line to see if we have DSC comments...
308 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
310 fputs("DEBUG: The print file is empty.\n", stderr
);
315 * Process command-line options...
319 num_options
= cupsParseOptions(argv
[5], 0, &options
);
320 ppd
= SetCommonOptions(num_options
, options
, 1);
322 set_pstops_options(&doc
, ppd
, argv
, num_options
, options
);
325 * Write any "exit server" options that have been selected...
328 ppdEmit(ppd
, stdout
, PPD_ORDER_EXIT
);
331 * Write any JCL commands that are needed to print PostScript code...
335 ppdEmitJCL(ppd
, stdout
, doc
.job_id
, doc
.user
, doc
.title
);
338 * Start with a DSC header...
341 puts("%!PS-Adobe-3.0");
344 * Skip leading PJL in the document...
347 while (!strncmp(line
, "\033%-12345X", 9) || !strncmp(line
, "@PJL ", 5))
350 * Yup, we have leading PJL fun, so skip it until we hit the line
351 * with "ENTER LANGUAGE"...
354 fputs("DEBUG: Skipping PJL header...\n", stderr
);
356 while (strstr(line
, "ENTER LANGUAGE") == NULL
&& strncmp(line
, "%!", 2))
357 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
360 if (!strncmp(line
, "%!", 2))
363 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
368 * Now see if the document conforms to the Adobe Document Structuring
372 if (!strncmp(line
, "%!PS-Adobe-", 11))
375 * Yes, filter the document...
378 copy_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
383 * No, display an error message and treat the file as if it contains
387 copy_non_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
391 * Send %%EOF as needed...
398 * End the job with the appropriate JCL command or CTRL-D...
403 if (ppd
&& ppd
->jcl_end
)
404 ppdEmitJCLEnd(ppd
, stdout
);
410 * Close files and remove the temporary file if needed...
415 cupsFileClose(doc
.temp
);
416 unlink(doc
.tempfile
);
420 cupsFreeOptions(num_options
, options
);
429 * 'add_page()' - Add a page to the pages array.
432 static pstops_page_t
* /* O - New page info object */
433 add_page(pstops_doc_t
*doc
, /* I - Document information */
434 const char *label
) /* I - Page label */
436 pstops_page_t
*pageinfo
; /* New page info object */
440 doc
->pages
= cupsArrayNew(NULL
, NULL
);
444 _cupsLangPrintError("EMERG", _("Unable to allocate memory for pages array"));
448 if ((pageinfo
= calloc(1, sizeof(pstops_page_t
))) == NULL
)
450 _cupsLangPrintError("EMERG", _("Unable to allocate memory for page info"));
454 pageinfo
->label
= strdup(label
);
455 pageinfo
->offset
= cupsFileTell(doc
->temp
);
457 cupsArrayAdd(doc
->pages
, pageinfo
);
466 * 'cancel_job()' - Flag the job as canceled.
470 cancel_job(int sig
) /* I - Signal number (unused) */
479 * 'check_range()' - Check to see if the current page is selected for
483 static int /* O - 1 if selected, 0 otherwise */
484 check_range(pstops_doc_t
*doc
, /* I - Document information */
485 int page
) /* I - Page number */
487 const char *range
; /* Pointer into range string */
488 int lower
, upper
; /* Lower and upper page numbers */
494 * See if we only print even or odd pages...
497 if (!strcasecmp(doc
->page_set
, "even") && (page
& 1))
500 if (!strcasecmp(doc
->page_set
, "odd") && !(page
& 1))
504 if (!doc
->page_ranges
)
505 return (1); /* No range, print all pages... */
507 for (range
= doc
->page_ranges
; *range
!= '\0';)
513 upper
= strtol(range
, (char **)&range
, 10);
517 lower
= strtol(range
, (char **)&range
, 10);
522 if (!isdigit(*range
& 255))
525 upper
= strtol(range
, (char **)&range
, 10);
531 if (page
>= lower
&& page
<= upper
)
545 * 'copy_bytes()' - Copy bytes from the input file to stdout.
549 copy_bytes(cups_file_t
*fp
, /* I - File to read from */
550 off_t offset
, /* I - Offset to page data */
551 size_t length
) /* I - Length of page data */
553 char buffer
[8192]; /* Data buffer */
554 ssize_t nbytes
; /* Number of bytes read */
555 size_t nleft
; /* Number of bytes left/remaining */
560 if (cupsFileSeek(fp
, offset
) < 0)
562 _cupsLangPrintError("ERROR", _("Unable to see in file"));
566 while (nleft
> 0 || length
== 0)
568 if (nleft
> sizeof(buffer
) || length
== 0)
569 nbytes
= sizeof(buffer
);
573 if ((nbytes
= cupsFileRead(fp
, buffer
, nbytes
)) < 1)
578 fwrite(buffer
, 1, nbytes
, stdout
);
584 * 'copy_comments()' - Copy all of the comments section.
586 * This function expects "line" to be filled with a comment line.
587 * On return, "line" will contain the next line in the file, if any.
590 static ssize_t
/* O - Length of next line */
591 copy_comments(cups_file_t
*fp
, /* I - File to read from */
592 pstops_doc_t
*doc
, /* I - Document info */
593 ppd_file_t
*ppd
, /* I - PPD file */
594 char *line
, /* I - Line buffer */
595 ssize_t linelen
, /* I - Length of initial line */
596 size_t linesize
) /* I - Size of line buffer */
598 int saw_bounding_box
, /* Saw %%BoundingBox: comment? */
599 saw_for
, /* Saw %%For: comment? */
600 saw_pages
, /* Saw %%Pages: comment? */
601 saw_title
; /* Saw %%Title: comment? */
605 * Loop until we see %%EndComments or a non-comment line...
608 saw_bounding_box
= 0;
613 while (line
[0] == '%')
616 * Strip trailing whitespace...
623 if (!isspace(line
[linelen
] & 255))
626 line
[linelen
] = '\0';
633 fprintf(stderr
, "DEBUG: %s\n", line
);
636 * Pull the headers out...
639 if (!strncmp(line
, "%%Pages:", 8))
641 int pages
; /* Number of pages */
644 fputs("DEBUG: A duplicate %%Pages: comment was seen.\n", stderr
);
648 if (Duplex
&& (pages
= atoi(line
+ 8)) > 0 && pages
<= doc
->number_up
)
651 * Since we will only be printing on a single page, disable duplexing.
655 doc
->slow_duplex
= 0;
657 if (cupsGetOption("sides", doc
->num_options
, doc
->options
))
658 doc
->num_options
= cupsAddOption("sides", "one-sided",
659 doc
->num_options
, &(doc
->options
));
661 if (cupsGetOption("Duplex", doc
->num_options
, doc
->options
))
662 doc
->num_options
= cupsAddOption("Duplex", "None",
663 doc
->num_options
, &(doc
->options
));
665 if (cupsGetOption("EFDuplex", doc
->num_options
, doc
->options
))
666 doc
->num_options
= cupsAddOption("EFDuplex", "None",
667 doc
->num_options
, &(doc
->options
));
669 if (cupsGetOption("EFDuplexing", doc
->num_options
, doc
->options
))
670 doc
->num_options
= cupsAddOption("EFDuplexing", "False",
671 doc
->num_options
, &(doc
->options
));
673 if (cupsGetOption("KD03Duplex", doc
->num_options
, doc
->options
))
674 doc
->num_options
= cupsAddOption("KD03Duplex", "None",
675 doc
->num_options
, &(doc
->options
));
677 if (cupsGetOption("JCLDuplex", doc
->num_options
, doc
->options
))
678 doc
->num_options
= cupsAddOption("JCLDuplex", "None",
679 doc
->num_options
, &(doc
->options
));
681 ppdMarkOption(ppd
, "Duplex", "None");
682 ppdMarkOption(ppd
, "EFDuplex", "None");
683 ppdMarkOption(ppd
, "EFDuplexing", "False");
684 ppdMarkOption(ppd
, "KD03Duplex", "None");
685 ppdMarkOption(ppd
, "JCLDuplex", "None");
688 else if (!strncmp(line
, "%%BoundingBox:", 14))
690 if (saw_bounding_box
)
691 fputs("DEBUG: A duplicate %%BoundingBox: comment was seen.\n", stderr
);
692 else if (strstr(line
+ 14, "(atend)"))
695 * Do nothing for now but use the default imageable area...
698 else if (sscanf(line
+ 14, "%d%d%d%d", doc
->bounding_box
+ 0,
699 doc
->bounding_box
+ 1, doc
->bounding_box
+ 2,
700 doc
->bounding_box
+ 3) != 4)
702 fputs("DEBUG: A bad %%BoundingBox: comment was seen.\n", stderr
);
704 doc
->bounding_box
[0] = (int)PageLeft
;
705 doc
->bounding_box
[1] = (int)PageBottom
;
706 doc
->bounding_box
[2] = (int)PageRight
;
707 doc
->bounding_box
[3] = (int)PageTop
;
710 saw_bounding_box
= 1;
712 else if (!strncmp(line
, "%%For:", 6))
715 doc_printf(doc
, "%s\n", line
);
717 else if (!strncmp(line
, "%%Title:", 8))
720 doc_printf(doc
, "%s\n", line
);
722 else if (!strncmp(line
, "%cupsRotation:", 14))
725 * Reset orientation of document?
728 int orient
= (atoi(line
+ 14) / 90) & 3;
730 if (orient
!= Orientation
)
733 * Yes, update things so that the pages come out right...
736 Orientation
= (4 - Orientation
+ orient
) & 3;
738 Orientation
= orient
;
741 else if (!strcmp(line
, "%%EndComments"))
743 linelen
= cupsFileGetLine(fp
, line
, linesize
);
746 else if (strncmp(line
, "%!", 2) && strncmp(line
, "%cups", 5))
747 doc_printf(doc
, "%s\n", line
);
749 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
753 if (!saw_bounding_box
)
754 fputs("DEBUG: There wasn't a %%BoundingBox: comment in the header.\n",
758 fputs("DEBUG: There wasn't a %%Pages: comment in the header.\n", stderr
);
761 WriteTextComment("For", doc
->user
);
764 WriteTextComment("Title", doc
->title
);
766 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
769 * Tell the document processor the copy and duplex options
770 * that are required...
773 doc_printf(doc
, "%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
774 doc
->collate
? " collate" : "",
775 Duplex
? " duplex" : "");
778 * Apple uses RBI comments for various non-PPD options...
781 doc_printf(doc
, "%%RBINumCopies: %d\n", doc
->copies
);
786 * Tell the document processor the duplex option that is required...
790 doc_puts(doc
, "%%Requirements: duplex\n");
793 * Apple uses RBI comments for various non-PPD options...
796 doc_puts(doc
, "%RBINumCopies: 1\n");
799 doc_puts(doc
, "%%Pages: (atend)\n");
800 doc_puts(doc
, "%%BoundingBox: (atend)\n");
801 doc_puts(doc
, "%%EndComments\n");
808 * 'copy_dsc()' - Copy a DSC-conforming document.
810 * This function expects "line" to be filled with the %!PS-Adobe comment line.
814 copy_dsc(cups_file_t
*fp
, /* I - File to read from */
815 pstops_doc_t
*doc
, /* I - Document info */
816 ppd_file_t
*ppd
, /* I - PPD file */
817 char *line
, /* I - Line buffer */
818 ssize_t linelen
, /* I - Length of initial line */
819 size_t linesize
) /* I - Size of line buffer */
821 int number
; /* Page number */
822 pstops_page_t
*pageinfo
; /* Page information */
826 * Make sure we use ESPshowpage for EPS files...
829 if (strstr(line
, "EPSF"))
831 doc
->use_ESPshowpage
= 1;
836 * Start sending the document with any commands needed...
839 fprintf(stderr
, "DEBUG: Before copy_comments - %s", line
);
840 linelen
= copy_comments(fp
, doc
, ppd
, line
, linelen
, linesize
);
843 * Now find the prolog section, if any...
846 fprintf(stderr
, "DEBUG: Before copy_prolog - %s", line
);
847 linelen
= copy_prolog(fp
, doc
, ppd
, line
, linelen
, linesize
);
850 * Then the document setup section...
853 fprintf(stderr
, "DEBUG: Before copy_setup - %s", line
);
854 linelen
= copy_setup(fp
, doc
, ppd
, line
, linelen
, linesize
);
857 * Copy until we see %%Page:...
860 while (strncmp(line
, "%%Page:", 7) && strncmp(line
, "%%Trailer", 9))
862 doc_write(doc
, line
, linelen
);
864 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
869 * Then process pages until we have no more...
874 fprintf(stderr
, "DEBUG: Before page loop - %s", line
);
875 while (!strncmp(line
, "%%Page:", 7))
882 if (check_range(doc
, (number
- 1) / doc
->number_up
+ 1))
884 fprintf(stderr
, "DEBUG: Copying page %d...\n", number
);
885 linelen
= copy_page(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
889 fprintf(stderr
, "DEBUG: Skipping page %d...\n", number
);
890 linelen
= skip_page(fp
, line
, linelen
, linesize
);
895 * Finish up the last page(s)...
898 if (number
&& is_not_last_page(number
) && cupsArrayLast(doc
->pages
) &&
899 check_range(doc
, (number
- 1) / doc
->number_up
+ 1))
901 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
903 start_nup(doc
, doc
->number_up
, 0, doc
->bounding_box
);
904 doc_puts(doc
, "showpage\n");
905 end_nup(doc
, doc
->number_up
);
907 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
910 if (doc
->slow_duplex
&& (doc
->page
& 1))
913 * Make sure we have an even number of pages...
916 pageinfo
= add_page(doc
, "(filler)");
918 if (!doc
->slow_order
)
920 if (!ppd
|| !ppd
->num_filters
)
921 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
922 doc
->slow_collate
? 1 : doc
->copies
);
924 printf("%%%%Page: (filler) %d\n", doc
->page
);
927 start_nup(doc
, doc
->number_up
, 0, doc
->bounding_box
);
928 doc_puts(doc
, "showpage\n");
929 end_nup(doc
, doc
->number_up
);
931 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
935 * Make additional copies as necessary...
938 number
= doc
->slow_order
? 0 : doc
->page
;
940 if (doc
->temp
&& !JobCanceled
&& cupsArrayCount(doc
->pages
) > 0)
942 int copy
; /* Current copy */
946 * Reopen the temporary file for reading...
949 cupsFileClose(doc
->temp
);
951 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
957 if (doc
->slow_collate
)
958 copy
= !doc
->slow_order
;
960 copy
= doc
->copies
- 1;
962 for (; copy
< doc
->copies
; copy
++)
968 * Send end-of-job stuff followed by any start-of-job stuff required
969 * for the JCL options...
972 if (number
&& doc
->emit_jcl
&& ppd
&& ppd
->jcl_end
)
975 * Send the trailer...
979 printf("%%%%Pages: %d\n", cupsArrayCount(doc
->pages
));
980 if (doc
->number_up
> 1 || doc
->fitplot
)
981 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
982 PageLeft
, PageBottom
, PageRight
, PageTop
);
984 printf("%%%%BoundingBox: %d %d %d %d\n",
985 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
986 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
990 * Start a new document...
993 ppdEmitJCLEnd(ppd
, stdout
);
994 ppdEmitJCL(ppd
, stdout
, doc
->job_id
, doc
->user
, doc
->title
);
996 puts("%!PS-Adobe-3.0");
1002 * Copy the prolog as needed...
1007 pageinfo
= (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
1008 copy_bytes(doc
->temp
, 0, pageinfo
->offset
);
1012 * Then copy all of the pages...
1015 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayLast(doc
->pages
) :
1016 (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
1025 if (!ppd
|| !ppd
->num_filters
)
1026 fprintf(stderr
, "PAGE: %d %d\n", number
,
1027 doc
->slow_collate
? 1 : doc
->copies
);
1029 if (doc
->number_up
> 1)
1031 printf("%%%%Page: (%d) %d\n", number
, number
);
1032 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1033 PageLeft
, PageBottom
, PageRight
, PageTop
);
1037 printf("%%%%Page: %s %d\n", pageinfo
->label
, number
);
1038 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1039 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
1040 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
1043 copy_bytes(doc
->temp
, pageinfo
->offset
, pageinfo
->length
);
1045 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayPrev(doc
->pages
) :
1046 (pstops_page_t
*)cupsArrayNext(doc
->pages
);
1052 * Restore the old showpage operator as needed...
1055 if (doc
->use_ESPshowpage
)
1056 puts("userdict/showpage/ESPshowpage load put\n");
1059 * Write/copy the trailer...
1063 copy_trailer(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
1068 * 'copy_non_dsc()' - Copy a document that does not conform to the DSC.
1070 * This function expects "line" to be filled with the %! comment line.
1074 copy_non_dsc(cups_file_t
*fp
, /* I - File to read from */
1075 pstops_doc_t
*doc
, /* I - Document info */
1076 ppd_file_t
*ppd
, /* I - PPD file */
1077 char *line
, /* I - Line buffer */
1078 ssize_t linelen
, /* I - Length of initial line */
1079 size_t linesize
) /* I - Size of line buffer */
1081 int copy
; /* Current copy */
1082 char buffer
[8192]; /* Copy buffer */
1083 int bytes
; /* Number of bytes copied */
1087 * First let the user know that they are attempting to print a file
1088 * that may not print correctly...
1091 fputs("DEBUG: This document does not conform to the Adobe Document "
1092 "Structuring Conventions and may not print correctly.\n", stderr
);
1095 * Then write a standard DSC comment section...
1098 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", PageLeft
, PageBottom
,
1099 PageRight
, PageTop
);
1101 if (doc
->slow_collate
&& doc
->copies
> 1)
1102 printf("%%%%Pages: %d\n", doc
->copies
);
1106 WriteTextComment("For", doc
->user
);
1107 WriteTextComment("Title", doc
->title
);
1109 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1112 * Tell the document processor the copy and duplex options
1113 * that are required...
1116 printf("%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
1117 doc
->collate
? " collate" : "",
1118 Duplex
? " duplex" : "");
1121 * Apple uses RBI comments for various non-PPD options...
1124 printf("%%RBINumCopies: %d\n", doc
->copies
);
1129 * Tell the document processor the duplex option that is required...
1133 puts("%%Requirements: duplex");
1136 * Apple uses RBI comments for various non-PPD options...
1139 puts("%RBINumCopies: 1");
1142 puts("%%EndComments");
1145 * Then the prolog...
1148 puts("%%BeginProlog");
1150 do_prolog(doc
, ppd
);
1152 puts("%%EndProlog");
1155 * Then the setup section...
1158 puts("%%BeginSetup");
1165 * Finally, embed a copy of the file inside a %%Page...
1168 if (!ppd
|| !ppd
->num_filters
)
1169 fprintf(stderr
, "PAGE: 1 %d\n", doc
->temp
? 1 : doc
->copies
);
1171 puts("%%Page: 1 1");
1172 puts("%%BeginPageSetup");
1173 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1174 puts("%%EndPageSetup");
1175 puts("%%BeginDocument: nondsc");
1177 fwrite(line
, linelen
, 1, stdout
);
1180 cupsFileWrite(doc
->temp
, line
, linelen
);
1182 while ((bytes
= cupsFileRead(fp
, buffer
, sizeof(buffer
))) > 0)
1184 fwrite(buffer
, 1, bytes
, stdout
);
1187 cupsFileWrite(doc
->temp
, buffer
, bytes
);
1190 puts("%%EndDocument");
1192 if (doc
->use_ESPshowpage
)
1194 WriteLabels(Orientation
);
1195 puts("ESPshowpage");
1198 if (doc
->temp
&& !JobCanceled
)
1201 * Reopen the temporary file for reading...
1204 cupsFileClose(doc
->temp
);
1206 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
1209 * Make the additional copies as needed...
1212 for (copy
= 1; copy
< doc
->copies
; copy
++)
1217 if (!ppd
|| !ppd
->num_filters
)
1218 fputs("PAGE: 1 1\n", stderr
);
1220 printf("%%%%Page: %d %d\n", copy
+ 1, copy
+ 1);
1221 puts("%%BeginPageSetup");
1222 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1223 puts("%%EndPageSetup");
1224 puts("%%BeginDocument: nondsc");
1226 copy_bytes(doc
->temp
, 0, 0);
1228 puts("%%EndDocument");
1230 if (doc
->use_ESPshowpage
)
1232 WriteLabels(Orientation
);
1233 puts("ESPshowpage");
1239 * Restore the old showpage operator as needed...
1242 if (doc
->use_ESPshowpage
)
1243 puts("userdict/showpage/ESPshowpage load put\n");
1248 * 'copy_page()' - Copy a page description.
1250 * This function expects "line" to be filled with a %%Page comment line.
1251 * On return, "line" will contain the next line in the file, if any.
1254 static ssize_t
/* O - Length of next line */
1255 copy_page(cups_file_t
*fp
, /* I - File to read from */
1256 pstops_doc_t
*doc
, /* I - Document info */
1257 ppd_file_t
*ppd
, /* I - PPD file */
1258 int number
, /* I - Current page number */
1259 char *line
, /* I - Line buffer */
1260 ssize_t linelen
, /* I - Length of initial line */
1261 size_t linesize
) /* I - Size of line buffer */
1263 char label
[256], /* Page label string */
1264 *ptr
; /* Pointer into line */
1265 int level
; /* Embedded document level */
1266 pstops_page_t
*pageinfo
; /* Page information */
1267 int first_page
; /* First page on N-up output? */
1268 int has_page_setup
; /* Does the page have %%Begin/EndPageSetup? */
1269 int bounding_box
[4]; /* PageBoundingBox */
1273 * Get the page label for this page...
1276 first_page
= is_first_page(number
);
1278 if (!parse_text(line
+ 7, &ptr
, label
, sizeof(label
)))
1280 fputs("DEBUG: There was a bad %%Page: comment in the file.\n", stderr
);
1284 else if (strtol(ptr
, &ptr
, 10) == LONG_MAX
|| !isspace(*ptr
& 255))
1286 fputs("DEBUG: There was a bad %%Page: comment in the file.\n", stderr
);
1291 * Create or update the current output page...
1295 pageinfo
= add_page(doc
, label
);
1297 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
1300 * Handle first page override...
1303 if (doc
->ap_input_slot
|| doc
->ap_manual_feed
)
1308 * First page/sheet gets AP_FIRSTPAGE_* options...
1311 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->ap_input_slot
,
1312 pageinfo
->num_options
,
1313 &(pageinfo
->options
));
1314 pageinfo
->num_options
= cupsAddOption("ManualFeed",
1315 doc
->ap_input_slot
? "False" :
1316 doc
->ap_manual_feed
,
1317 pageinfo
->num_options
,
1318 &(pageinfo
->options
));
1319 pageinfo
->num_options
= cupsAddOption("MediaColor", doc
->ap_media_color
,
1320 pageinfo
->num_options
,
1321 &(pageinfo
->options
));
1322 pageinfo
->num_options
= cupsAddOption("MediaType", doc
->ap_media_type
,
1323 pageinfo
->num_options
,
1324 &(pageinfo
->options
));
1325 pageinfo
->num_options
= cupsAddOption("PageRegion", doc
->ap_page_region
,
1326 pageinfo
->num_options
,
1327 &(pageinfo
->options
));
1328 pageinfo
->num_options
= cupsAddOption("PageSize", doc
->ap_page_size
,
1329 pageinfo
->num_options
,
1330 &(pageinfo
->options
));
1332 else if (doc
->page
== (Duplex
+ 2))
1335 * Second page/sheet gets default options...
1338 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->input_slot
,
1339 pageinfo
->num_options
,
1340 &(pageinfo
->options
));
1341 pageinfo
->num_options
= cupsAddOption("ManualFeed",
1342 doc
->input_slot
? "False" :
1344 pageinfo
->num_options
,
1345 &(pageinfo
->options
));
1346 pageinfo
->num_options
= cupsAddOption("MediaColor", doc
->media_color
,
1347 pageinfo
->num_options
,
1348 &(pageinfo
->options
));
1349 pageinfo
->num_options
= cupsAddOption("MediaType", doc
->media_type
,
1350 pageinfo
->num_options
,
1351 &(pageinfo
->options
));
1352 pageinfo
->num_options
= cupsAddOption("PageRegion", doc
->page_region
,
1353 pageinfo
->num_options
,
1354 &(pageinfo
->options
));
1355 pageinfo
->num_options
= cupsAddOption("PageSize", doc
->page_size
,
1356 pageinfo
->num_options
,
1357 &(pageinfo
->options
));
1362 * Scan comments until we see something other than %%Page*: or
1366 memcpy(bounding_box
, doc
->bounding_box
, sizeof(bounding_box
));
1368 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1370 if (!strncmp(line
, "%%PageBoundingBox:", 18))
1373 * %%PageBoundingBox: llx lly urx ury
1376 if (sscanf(line
+ 18, "%d%d%d%d", bounding_box
+ 0,
1377 bounding_box
+ 1, bounding_box
+ 2,
1378 bounding_box
+ 3) != 4)
1380 fputs("DEBUG: There was a bad %%PageBoundingBox: comment in the file.\n", stderr
);
1381 memcpy(bounding_box
, doc
->bounding_box
,
1382 sizeof(bounding_box
));
1384 else if (doc
->number_up
== 1 && !doc
->fitplot
&& Orientation
)
1386 int temp_bbox
[4]; /* Temporary bounding box */
1389 memcpy(temp_bbox
, bounding_box
, sizeof(temp_bbox
));
1391 fprintf(stderr
, "DEBUG: Orientation = %d\n", Orientation
);
1392 fprintf(stderr
, "DEBUG: original bounding_box = [ %d %d %d %d ]\n",
1393 bounding_box
[0], bounding_box
[1],
1394 bounding_box
[2], bounding_box
[3]);
1395 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
1396 PageWidth
, PageLength
);
1398 switch (Orientation
)
1400 case 1 : /* Landscape */
1401 bounding_box
[0] = PageLength
- temp_bbox
[3];
1402 bounding_box
[1] = temp_bbox
[0];
1403 bounding_box
[2] = PageLength
- temp_bbox
[1];
1404 bounding_box
[3] = temp_bbox
[2];
1407 case 2 : /* Reverse Portrait */
1408 bounding_box
[0] = PageWidth
- temp_bbox
[2];
1409 bounding_box
[1] = PageLength
- temp_bbox
[3];
1410 bounding_box
[2] = PageWidth
- temp_bbox
[0];
1411 bounding_box
[3] = PageLength
- temp_bbox
[1];
1414 case 3 : /* Reverse Landscape */
1415 bounding_box
[0] = temp_bbox
[1];
1416 bounding_box
[1] = PageWidth
- temp_bbox
[2];
1417 bounding_box
[2] = temp_bbox
[3];
1418 bounding_box
[3] = PageWidth
- temp_bbox
[0];
1422 fprintf(stderr
, "DEBUG: updated bounding_box = [ %d %d %d %d ]\n",
1423 bounding_box
[0], bounding_box
[1],
1424 bounding_box
[2], bounding_box
[3]);
1428 else if (!strncmp(line
, "%%PageCustomColors:", 19) ||
1429 !strncmp(line
, "%%PageMedia:", 12) ||
1430 !strncmp(line
, "%%PageOrientation:", 18) ||
1431 !strncmp(line
, "%%PageProcessColors:", 20) ||
1432 !strncmp(line
, "%%PageRequirements:", 18) ||
1433 !strncmp(line
, "%%PageResources:", 16))
1440 else if (!strncmp(line
, "%%PageCustomColors:", 19))
1443 * %%PageCustomColors: ...
1446 else if (!strncmp(line
, "%%PageMedia:", 12))
1452 else if (!strncmp(line
, "%%PageOrientation:", 18))
1455 * %%PageOrientation: ...
1458 else if (!strncmp(line
, "%%PageProcessColors:", 20))
1461 * %%PageProcessColors: ...
1464 else if (!strncmp(line
, "%%PageRequirements:", 18))
1467 * %%PageRequirements: ...
1470 else if (!strncmp(line
, "%%PageResources:", 16))
1473 * %%PageResources: ...
1476 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1479 * %%IncludeFeature: *MainKeyword OptionKeyword
1482 if (doc
->number_up
== 1 &&!doc
->fitplot
)
1483 pageinfo
->num_options
= include_feature(ppd
, line
,
1484 pageinfo
->num_options
,
1485 &(pageinfo
->options
));
1487 else if (strncmp(line
, "%%Include", 9))
1491 if (doc
->number_up
== 1)
1494 * Update the document's composite and page bounding box...
1497 memcpy(pageinfo
->bounding_box
, bounding_box
,
1498 sizeof(pageinfo
->bounding_box
));
1500 if (bounding_box
[0] < doc
->new_bounding_box
[0])
1501 doc
->new_bounding_box
[0] = bounding_box
[0];
1502 if (bounding_box
[1] < doc
->new_bounding_box
[1])
1503 doc
->new_bounding_box
[1] = bounding_box
[1];
1504 if (bounding_box
[2] > doc
->new_bounding_box
[2])
1505 doc
->new_bounding_box
[2] = bounding_box
[2];
1506 if (bounding_box
[3] > doc
->new_bounding_box
[3])
1507 doc
->new_bounding_box
[3] = bounding_box
[3];
1511 * Output the page header as needed...
1514 if (!doc
->slow_order
&& first_page
)
1516 if (!ppd
|| !ppd
->num_filters
)
1517 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
1518 doc
->slow_collate
? 1 : doc
->copies
);
1520 if (doc
->number_up
> 1)
1522 printf("%%%%Page: (%d) %d\n", doc
->page
, doc
->page
);
1523 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1524 PageLeft
, PageBottom
, PageRight
, PageTop
);
1528 printf("%%%%Page: %s %d\n", pageinfo
->label
, doc
->page
);
1529 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1530 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
1531 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
1536 * Copy any page setup commands...
1540 doc_puts(doc
, "%%BeginPageSetup\n");
1542 if ((has_page_setup
= !strncmp(line
, "%%BeginPageSetup", 16)) != 0)
1544 int feature
= 0; /* In a Begin/EndFeature block? */
1546 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1548 if (!strncmp(line
, "%%EndPageSetup", 14))
1550 else if (!strncmp(line
, "%%BeginFeature:", 15))
1554 if (doc
->number_up
> 1 || doc
->fitplot
)
1557 else if (!strncmp(line
, "%%EndFeature", 12))
1561 if (doc
->number_up
> 1 || doc
->fitplot
)
1564 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1566 pageinfo
->num_options
= include_feature(ppd
, line
,
1567 pageinfo
->num_options
,
1568 &(pageinfo
->options
));
1571 else if (!strncmp(line
, "%%Include", 9))
1574 if (line
[0] != '%' && !feature
)
1577 if (!feature
|| (doc
->number_up
== 1 && !doc
->fitplot
))
1578 doc_write(doc
, line
, linelen
);
1582 * Skip %%EndPageSetup...
1585 if (linelen
> 0 && !strncmp(line
, "%%EndPageSetup", 14))
1587 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1594 char *page_setup
; /* PageSetup commands to send */
1597 if (pageinfo
->num_options
> 0)
1598 write_options(doc
, ppd
, pageinfo
->num_options
, pageinfo
->options
);
1601 * Output commands for the current page...
1604 page_setup
= ppdEmitString(ppd
, PPD_ORDER_PAGE
, 0);
1608 doc_puts(doc
, page_setup
);
1614 * Prep for the start of the page description...
1617 start_nup(doc
, number
, 1, bounding_box
);
1620 * Finish the PageSetup section as needed...
1625 int feature
= 0; /* In a Begin/EndFeature block? */
1627 doc_write(doc
, line
, linelen
);
1629 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1631 if (!strncmp(line
, "%%EndPageSetup", 14))
1633 else if (!strncmp(line
, "%%BeginFeature:", 15))
1637 if (doc
->number_up
> 1 || doc
->fitplot
)
1640 else if (!strncmp(line
, "%%EndFeature", 12))
1644 if (doc
->number_up
> 1 || doc
->fitplot
)
1647 else if (!strncmp(line
, "%%Include", 9))
1650 if (!feature
|| (doc
->number_up
== 1 && !doc
->fitplot
))
1651 doc_write(doc
, line
, linelen
);
1655 * Skip %%EndPageSetup...
1658 if (linelen
> 0 && !strncmp(line
, "%%EndPageSetup", 14))
1659 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1663 doc_puts(doc
, "%%EndPageSetup\n");
1666 * Read the rest of the page description...
1674 (!strncmp(line
, "%%Page:", 7) ||
1675 !strncmp(line
, "%%Trailer", 9) ||
1676 !strncmp(line
, "%%EOF", 5)))
1678 else if (!strncmp(line
, "%%BeginDocument", 15) ||
1679 !strncmp(line
, "%ADO_BeginApplication", 21))
1681 doc_write(doc
, line
, linelen
);
1685 else if ((!strncmp(line
, "%%EndDocument", 13) ||
1686 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
1688 doc_write(doc
, line
, linelen
);
1692 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
1693 (!strncmp(line
, "%%BeginData:", 12) &&
1694 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
1697 * Copy binary data...
1700 int bytes
; /* Bytes of data */
1703 doc_write(doc
, line
, linelen
);
1705 bytes
= atoi(strchr(line
, ':') + 1);
1709 if (bytes
> linesize
)
1710 linelen
= cupsFileRead(fp
, line
, linesize
);
1712 linelen
= cupsFileRead(fp
, line
, bytes
);
1717 perror("ERROR: Early end-of-file while reading binary data");
1721 doc_write(doc
, line
, linelen
);
1727 doc_write(doc
, line
, linelen
);
1729 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0);
1732 * Finish up this page and return...
1735 end_nup(doc
, number
);
1737 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
1744 * 'copy_prolog()' - Copy the document prolog section.
1746 * This function expects "line" to be filled with a %%BeginProlog comment line.
1747 * On return, "line" will contain the next line in the file, if any.
1750 static ssize_t
/* O - Length of next line */
1751 copy_prolog(cups_file_t
*fp
, /* I - File to read from */
1752 pstops_doc_t
*doc
, /* I - Document info */
1753 ppd_file_t
*ppd
, /* I - PPD file */
1754 char *line
, /* I - Line buffer */
1755 ssize_t linelen
, /* I - Length of initial line */
1756 size_t linesize
) /* I - Size of line buffer */
1758 while (strncmp(line
, "%%BeginProlog", 13))
1760 if (!strncmp(line
, "%%BeginSetup", 12) || !strncmp(line
, "%%Page:", 7))
1763 doc_write(doc
, line
, linelen
);
1765 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1769 doc_puts(doc
, "%%BeginProlog\n");
1771 do_prolog(doc
, ppd
);
1773 if (!strncmp(line
, "%%BeginProlog", 13))
1775 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1777 if (!strncmp(line
, "%%EndProlog", 11) ||
1778 !strncmp(line
, "%%BeginSetup", 12) ||
1779 !strncmp(line
, "%%Page:", 7))
1782 doc_write(doc
, line
, linelen
);
1785 if (!strncmp(line
, "%%EndProlog", 11))
1786 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1788 fputs("DEBUG: The %%EndProlog comment is missing.\n", stderr
);
1791 doc_puts(doc
, "%%EndProlog\n");
1798 * 'copy_setup()' - Copy the document setup section.
1800 * This function expects "line" to be filled with a %%BeginSetup comment line.
1801 * On return, "line" will contain the next line in the file, if any.
1804 static ssize_t
/* O - Length of next line */
1805 copy_setup(cups_file_t
*fp
, /* I - File to read from */
1806 pstops_doc_t
*doc
, /* I - Document info */
1807 ppd_file_t
*ppd
, /* I - PPD file */
1808 char *line
, /* I - Line buffer */
1809 ssize_t linelen
, /* I - Length of initial line */
1810 size_t linesize
) /* I - Size of line buffer */
1812 int num_options
; /* Number of options */
1813 cups_option_t
*options
; /* Options */
1816 while (strncmp(line
, "%%BeginSetup", 12))
1818 if (!strncmp(line
, "%%Page:", 7))
1821 doc_write(doc
, line
, linelen
);
1823 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1827 doc_puts(doc
, "%%BeginSetup\n");
1834 if (!strncmp(line
, "%%BeginSetup", 12))
1836 while (strncmp(line
, "%%EndSetup", 10))
1838 if (!strncmp(line
, "%%Page:", 7))
1840 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1843 * %%IncludeFeature: *MainKeyword OptionKeyword
1846 if (doc
->number_up
== 1 && !doc
->fitplot
)
1847 num_options
= include_feature(ppd
, line
, num_options
, &options
);
1849 else if (strncmp(line
, "%%BeginSetup", 12))
1850 doc_write(doc
, line
, linelen
);
1852 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1856 if (!strncmp(line
, "%%EndSetup", 10))
1857 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1859 fputs("DEBUG: The %%EndSetup comment is missing.\n", stderr
);
1862 if (num_options
> 0)
1864 write_options(doc
, ppd
, num_options
, options
);
1865 cupsFreeOptions(num_options
, options
);
1868 doc_puts(doc
, "%%EndSetup\n");
1875 * 'copy_trailer()' - Copy the document trailer.
1877 * This function expects "line" to be filled with a %%Trailer comment line.
1878 * On return, "line" will contain the next line in the file, if any.
1881 static ssize_t
/* O - Length of next line */
1882 copy_trailer(cups_file_t
*fp
, /* I - File to read from */
1883 pstops_doc_t
*doc
, /* I - Document info */
1884 ppd_file_t
*ppd
, /* I - PPD file */
1885 int number
, /* I - Number of pages */
1886 char *line
, /* I - Line buffer */
1887 ssize_t linelen
, /* I - Length of initial line */
1888 size_t linesize
) /* I - Size of line buffer */
1891 * Write the trailer comments...
1898 if (!strncmp(line
, "%%EOF", 5))
1900 else if (strncmp(line
, "%%Trailer", 9) &&
1901 strncmp(line
, "%%Pages:", 8) &&
1902 strncmp(line
, "%%BoundingBox:", 14))
1903 fwrite(line
, 1, linelen
, stdout
);
1905 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1908 fprintf(stderr
, "DEBUG: Wrote %d pages...\n", number
);
1910 printf("%%%%Pages: %d\n", number
);
1911 if (doc
->number_up
> 1 || doc
->fitplot
)
1912 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
1913 PageLeft
, PageBottom
, PageRight
, PageTop
);
1915 printf("%%%%BoundingBox: %d %d %d %d\n",
1916 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
1917 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
1924 * 'do_prolog()' - Send the necessary document prolog commands.
1928 do_prolog(pstops_doc_t
*doc
, /* I - Document information */
1929 ppd_file_t
*ppd
) /* I - PPD file */
1931 char *ps
; /* PS commands */
1935 * Send the document prolog commands...
1938 if (ppd
&& ppd
->patches
)
1940 doc_puts(doc
, "%%BeginFeature: *JobPatchFile 1\n");
1941 doc_puts(doc
, ppd
->patches
);
1942 doc_puts(doc
, "\n%%EndFeature\n");
1945 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_PROLOG
, 0.0)) != NULL
)
1952 * Define ESPshowpage here so that applications that define their
1953 * own procedure to do a showpage pick it up...
1956 if (doc
->use_ESPshowpage
)
1957 doc_puts(doc
, "userdict/ESPshowpage/showpage load put\n"
1958 "userdict/showpage{}put\n");
1963 * 'do_setup()' - Send the necessary document setup commands.
1967 do_setup(pstops_doc_t
*doc
, /* I - Document information */
1968 ppd_file_t
*ppd
) /* I - PPD file */
1970 char *ps
; /* PS commands */
1974 * Disable CTRL-D so that embedded files don't cause printing
1978 doc_puts(doc
, "% Disable CTRL-D as an end-of-file marker...\n");
1979 doc_puts(doc
, "userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put\n");
1982 * Mark job options...
1985 cupsMarkOptions(ppd
, doc
->num_options
, doc
->options
);
1988 * Send all the printer-specific setup commands...
1991 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, 0.0)) != NULL
)
1997 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_ANY
, 0.0)) != NULL
)
2004 * Set the number of copies for the job...
2007 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
2009 doc_printf(doc
, "%%RBIBeginNonPPDFeature: *NumCopies %d\n", doc
->copies
);
2011 "%d/languagelevel where{pop languagelevel 2 ge}{false}ifelse\n"
2012 "{1 dict begin/NumCopies exch def currentdict end "
2014 "{userdict/#copies 3 -1 roll put}ifelse\n", doc
->copies
);
2015 doc_puts(doc
, "%RBIEndNonPPDFeature\n");
2019 * If we are doing N-up printing, disable setpagedevice...
2022 if (doc
->number_up
> 1)
2024 doc_puts(doc
, "userdict/CUPSsetpagedevice/setpagedevice load put\n");
2025 doc_puts(doc
, "userdict/setpagedevice{pop}bind put\n");
2029 * Changes to the transfer function must be made AFTER any
2030 * setpagedevice code...
2033 if (doc
->gamma
!= 1.0f
|| doc
->brightness
!= 1.0f
)
2034 doc_printf(doc
, "{ neg 1 add dup 0 lt { pop 1 } { %.3f exp neg 1 add } "
2035 "ifelse %.3f mul } bind settransfer\n",
2036 doc
->gamma
, doc
->brightness
);
2039 * Make sure we have rectclip and rectstroke procedures of some sort...
2043 "% x y w h ESPrc - Clip to a rectangle.\n"
2044 "userdict/ESPrc/rectclip where{pop/rectclip load}\n"
2045 "{{newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
2046 "neg 0 rlineto closepath clip newpath}bind}ifelse put\n");
2049 "% x y w h ESPrf - Fill a rectangle.\n"
2050 "userdict/ESPrf/rectfill where{pop/rectfill load}\n"
2051 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
2052 "neg 0 rlineto closepath fill grestore}bind}ifelse put\n");
2055 "% x y w h ESPrs - Stroke a rectangle.\n"
2056 "userdict/ESPrs/rectstroke where{pop/rectstroke load}\n"
2057 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
2058 "neg 0 rlineto closepath stroke grestore}bind}ifelse put\n");
2061 * Write the page and label prologs...
2064 if (doc
->number_up
== 2 || doc
->number_up
== 6)
2067 * For 2- and 6-up output, rotate the labels to match the orientation
2071 if (Orientation
& 1)
2072 write_label_prolog(doc
, doc
->page_label
, PageBottom
,
2073 PageWidth
- PageLength
+ PageTop
, PageLength
);
2075 write_label_prolog(doc
, doc
->page_label
, PageLeft
, PageRight
,
2079 write_label_prolog(doc
, doc
->page_label
, PageBottom
, PageTop
, PageWidth
);
2084 * 'doc_printf()' - Send a formatted string to stdout and/or the temp file.
2086 * This function should be used for all page-level output that is affected
2087 * by ordering, collation, etc.
2091 doc_printf(pstops_doc_t
*doc
, /* I - Document information */
2092 const char *format
, /* I - Printf-style format string */
2093 ...) /* I - Additional arguments as needed */
2095 va_list ap
; /* Pointer to arguments */
2096 char buffer
[1024]; /* Output buffer */
2097 size_t bytes
; /* Number of bytes to write */
2100 va_start(ap
, format
);
2101 bytes
= vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
2104 if (bytes
> sizeof(buffer
))
2106 _cupsLangPrintFilter(stderr
, "ERROR",
2107 _("Buffer overflow detected, aborting."));
2111 doc_write(doc
, buffer
, bytes
);
2116 * 'doc_puts()' - Send a nul-terminated string to stdout and/or the temp file.
2118 * This function should be used for all page-level output that is affected
2119 * by ordering, collation, etc.
2123 doc_puts(pstops_doc_t
*doc
, /* I - Document information */
2124 const char *s
) /* I - String to send */
2126 doc_write(doc
, s
, strlen(s
));
2131 * 'doc_write()' - Send data to stdout and/or the temp file.
2135 doc_write(pstops_doc_t
*doc
, /* I - Document information */
2136 const char *s
, /* I - Data to send */
2137 size_t len
) /* I - Number of bytes to send */
2139 if (!doc
->slow_order
)
2140 fwrite(s
, 1, len
, stdout
);
2143 cupsFileWrite(doc
->temp
, s
, len
);
2148 * 'end_nup()' - End processing for N-up printing.
2152 end_nup(pstops_doc_t
*doc
, /* I - Document information */
2153 int number
) /* I - Page number */
2155 if (doc
->number_up
> 1)
2156 doc_puts(doc
, "userdict/ESPsave get restore\n");
2158 switch (doc
->number_up
)
2161 if (doc
->use_ESPshowpage
)
2163 write_labels(doc
, Orientation
);
2164 doc_puts(doc
, "ESPshowpage\n");
2170 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2172 if (Orientation
& 1)
2175 * Rotate the labels back to portrait...
2178 write_labels(doc
, Orientation
- 1);
2180 else if (Orientation
== 0)
2183 * Rotate the labels to landscape...
2186 write_labels(doc
, doc
->normal_landscape
? 1 : 3);
2191 * Rotate the labels to landscape...
2194 write_labels(doc
, doc
->normal_landscape
? 3 : 1);
2197 doc_puts(doc
, "ESPshowpage\n");
2202 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2204 write_labels(doc
, Orientation
);
2205 doc_puts(doc
, "ESPshowpage\n");
2215 * 'include_feature()' - Include a printer option/feature command.
2218 static int /* O - New number of options */
2220 ppd_file_t
*ppd
, /* I - PPD file */
2221 const char *line
, /* I - DSC line */
2222 int num_options
, /* I - Number of options */
2223 cups_option_t
**options
) /* IO - Options */
2225 char name
[255], /* Option name */
2226 value
[255]; /* Option value */
2227 ppd_option_t
*option
; /* Option in file */
2231 * Get the "%%IncludeFeature: *Keyword OptionKeyword" values...
2234 if (sscanf(line
+ 17, "%254s%254s", name
, value
) != 2)
2236 fputs("DEBUG: The %%IncludeFeature: comment is not valid.\n", stderr
);
2237 return (num_options
);
2241 * Find the option and choice...
2244 if ((option
= ppdFindOption(ppd
, name
+ 1)) == NULL
)
2246 _cupsLangPrintFilter(stderr
, "WARNING", _("Unknown option \"%s\"."),
2248 return (num_options
);
2251 if (option
->section
== PPD_ORDER_EXIT
||
2252 option
->section
== PPD_ORDER_JCL
)
2254 _cupsLangPrintFilter(stderr
, "WARNING",
2255 _("Option \"%s\" cannot be included via "
2256 "%%%%IncludeFeature."), name
+ 1);
2257 return (num_options
);
2260 if (!ppdFindChoice(option
, value
))
2262 _cupsLangPrintFilter(stderr
, "WARNING",
2263 _("Unknown choice \"%s\" for option \"%s\"."),
2265 return (num_options
);
2269 * Add the option to the option array and return...
2272 return (cupsAddOption(name
+ 1, value
, num_options
, options
));
2277 * 'parse_text()' - Parse a text value in a comment.
2279 * This function parses a DSC text value as defined on page 36 of the
2280 * DSC specification. Text values are either surrounded by parenthesis
2281 * or whitespace-delimited.
2283 * The value returned is the literal characters for the entire text
2284 * string, including any parenthesis and escape characters.
2287 static char * /* O - Value or NULL on error */
2288 parse_text(const char *start
, /* I - Start of text value */
2289 char **end
, /* O - End of text value */
2290 char *buffer
, /* I - Buffer */
2291 size_t bufsize
) /* I - Size of buffer */
2293 char *bufptr
, /* Pointer in buffer */
2294 *bufend
; /* End of buffer */
2295 int level
; /* Parenthesis level */
2299 * Skip leading whitespace...
2302 while (isspace(*start
& 255))
2306 * Then copy the value...
2311 bufend
= buffer
+ bufsize
- 1;
2313 while (bufptr
< bufend
)
2315 if (isspace(*start
& 255) && !level
)
2322 else if (*start
== ')')
2332 else if (*start
== '\\')
2335 * Copy escaped character...
2338 int i
; /* Looping var */
2342 i
<= 3 && isdigit(start
[i
] & 255) && bufptr
< bufend
;
2343 *bufptr
++ = start
[i
], i
++);
2352 * Return the value and new pointer into the line...
2356 *end
= (char *)start
;
2358 if (bufptr
== bufend
)
2366 * 'set_pstops_options()' - Set pstops options.
2371 pstops_doc_t
*doc
, /* I - Document information */
2372 ppd_file_t
*ppd
, /* I - PPD file */
2373 char *argv
[], /* I - Command-line arguments */
2374 int num_options
, /* I - Number of options */
2375 cups_option_t
*options
) /* I - Options */
2377 const char *val
; /* Option value */
2378 int intval
; /* Integer option value */
2379 ppd_attr_t
*attr
; /* PPD attribute */
2380 ppd_option_t
*option
; /* PPD option */
2381 ppd_choice_t
*choice
; /* PPD choice */
2382 const char *content_type
; /* Original content type */
2386 * Initialize document information structure...
2389 memset(doc
, 0, sizeof(pstops_doc_t
));
2391 doc
->job_id
= atoi(argv
[1]);
2392 doc
->user
= argv
[2];
2393 doc
->title
= argv
[3];
2394 doc
->copies
= atoi(argv
[4]);
2396 if (ppd
&& ppd
->landscape
> 0)
2397 doc
->normal_landscape
= 1;
2399 doc
->bounding_box
[0] = (int)PageLeft
;
2400 doc
->bounding_box
[1] = (int)PageBottom
;
2401 doc
->bounding_box
[2] = (int)PageRight
;
2402 doc
->bounding_box
[3] = (int)PageTop
;
2404 doc
->new_bounding_box
[0] = INT_MAX
;
2405 doc
->new_bounding_box
[1] = INT_MAX
;
2406 doc
->new_bounding_box
[2] = INT_MIN
;
2407 doc
->new_bounding_box
[3] = INT_MIN
;
2410 * AP_FIRSTPAGE_* and the corresponding non-first-page options.
2413 doc
->ap_input_slot
= cupsGetOption("AP_FIRSTPAGE_InputSlot", num_options
,
2415 doc
->ap_manual_feed
= cupsGetOption("AP_FIRSTPAGE_ManualFeed", num_options
,
2417 doc
->ap_media_color
= cupsGetOption("AP_FIRSTPAGE_MediaColor", num_options
,
2419 doc
->ap_media_type
= cupsGetOption("AP_FIRSTPAGE_MediaType", num_options
,
2421 doc
->ap_page_region
= cupsGetOption("AP_FIRSTPAGE_PageRegion", num_options
,
2423 doc
->ap_page_size
= cupsGetOption("AP_FIRSTPAGE_PageSize", num_options
,
2426 if ((choice
= ppdFindMarkedChoice(ppd
, "InputSlot")) != NULL
)
2427 doc
->input_slot
= choice
->choice
;
2428 if ((choice
= ppdFindMarkedChoice(ppd
, "ManualFeed")) != NULL
)
2429 doc
->manual_feed
= choice
->choice
;
2430 if ((choice
= ppdFindMarkedChoice(ppd
, "MediaColor")) != NULL
)
2431 doc
->media_color
= choice
->choice
;
2432 if ((choice
= ppdFindMarkedChoice(ppd
, "MediaType")) != NULL
)
2433 doc
->media_type
= choice
->choice
;
2434 if ((choice
= ppdFindMarkedChoice(ppd
, "PageRegion")) != NULL
)
2435 doc
->page_region
= choice
->choice
;
2436 if ((choice
= ppdFindMarkedChoice(ppd
, "PageSize")) != NULL
)
2437 doc
->page_size
= choice
->choice
;
2443 if ((val
= cupsGetOption("brightness", num_options
, options
)) != NULL
)
2446 * Get brightness value from 10 to 1000.
2451 if (intval
< 10 || intval
> 1000)
2453 _cupsLangPrintFilter(stderr
, "ERROR",
2454 _("Unsupported brightness value %s, using "
2455 "brightness=100."), val
);
2456 doc
->brightness
= 1.0f
;
2459 doc
->brightness
= intval
* 0.01f
;
2462 doc
->brightness
= 1.0f
;
2465 * collate, multiple-document-handling
2468 if ((val
= cupsGetOption("multiple-document-handling", num_options
, options
)) != NULL
)
2471 * This IPP attribute is unnecessarily complicated...
2473 * single-document, separate-documents-collated-copies, and
2474 * single-document-new-sheet all require collated copies.
2476 * separate-documents-uncollated-copies allows for uncollated copies.
2479 doc
->collate
= strcasecmp(val
, "separate-documents-uncollated-copies") != 0;
2482 if ((val
= cupsGetOption("Collate", num_options
, options
)) != NULL
&&
2483 (!strcasecmp(val
, "true") ||!strcasecmp(val
, "on") ||
2484 !strcasecmp(val
, "yes")))
2491 if ((val
= cupsGetOption("emit-jcl", num_options
, options
)) != NULL
&&
2492 (!strcasecmp(val
, "false") || !strcasecmp(val
, "off") ||
2493 !strcasecmp(val
, "no") || !strcmp(val
, "0")))
2499 * fitplot/fit-to-page/ipp-attribute-fidelity
2501 * (Only for original PostScript content)
2504 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
2505 content_type
= "application/postscript";
2507 if (!strcasecmp(content_type
, "application/postscript"))
2509 if ((val
= cupsGetOption("fitplot", num_options
, options
)) != NULL
&&
2510 !strcasecmp(val
, "true"))
2512 else if ((val
= cupsGetOption("fit-to-page", num_options
, options
)) != NULL
&&
2513 !strcasecmp(val
, "true"))
2515 else if ((val
= cupsGetOption("ipp-attribute-fidelity", num_options
,
2516 options
)) != NULL
&&
2517 !strcasecmp(val
, "true"))
2525 if ((val
= cupsGetOption("gamma", num_options
, options
)) != NULL
)
2528 * Get gamma value from 1 to 10000...
2533 if (intval
< 1 || intval
> 10000)
2535 _cupsLangPrintFilter(stderr
, "ERROR",
2536 _("Unsupported gamma value %s, using gamma=1000."),
2541 doc
->gamma
= intval
* 0.001f
;
2547 * mirror/MirrorPrint
2550 if ((choice
= ppdFindMarkedChoice(ppd
, "MirrorPrint")) != NULL
)
2552 val
= choice
->choice
;
2556 val
= cupsGetOption("mirror", num_options
, options
);
2558 if (val
&& (!strcasecmp(val
, "true") || !strcasecmp(val
, "on") ||
2559 !strcasecmp(val
, "yes")))
2566 if ((val
= cupsGetOption("number-up", num_options
, options
)) != NULL
)
2568 switch (intval
= atoi(val
))
2576 doc
->number_up
= intval
;
2579 _cupsLangPrintFilter(stderr
, "ERROR",
2580 _("Unsupported number-up value %d, using "
2581 "number-up=1."), intval
);
2593 if ((val
= cupsGetOption("number-up-layout", num_options
, options
)) != NULL
)
2595 if (!strcasecmp(val
, "lrtb"))
2596 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2597 else if (!strcasecmp(val
, "lrbt"))
2598 doc
->number_up_layout
= PSTOPS_LAYOUT_LRBT
;
2599 else if (!strcasecmp(val
, "rltb"))
2600 doc
->number_up_layout
= PSTOPS_LAYOUT_RLTB
;
2601 else if (!strcasecmp(val
, "rlbt"))
2602 doc
->number_up_layout
= PSTOPS_LAYOUT_RLBT
;
2603 else if (!strcasecmp(val
, "tblr"))
2604 doc
->number_up_layout
= PSTOPS_LAYOUT_TBLR
;
2605 else if (!strcasecmp(val
, "tbrl"))
2606 doc
->number_up_layout
= PSTOPS_LAYOUT_TBRL
;
2607 else if (!strcasecmp(val
, "btlr"))
2608 doc
->number_up_layout
= PSTOPS_LAYOUT_BTLR
;
2609 else if (!strcasecmp(val
, "btrl"))
2610 doc
->number_up_layout
= PSTOPS_LAYOUT_BTRL
;
2613 _cupsLangPrintFilter(stderr
, "ERROR",
2614 _("Unsupported number-up-layout value %s, using "
2615 "number-up-layout=lrtb."), val
);
2616 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2620 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2626 if ((val
= cupsGetOption("OutputOrder", num_options
, options
)) != NULL
)
2628 if (!strcasecmp(val
, "Reverse"))
2629 doc
->output_order
= 1;
2634 * Figure out the right default output order from the PPD file...
2637 if ((choice
= ppdFindMarkedChoice(ppd
, "OutputBin")) != NULL
&&
2638 (attr
= ppdFindAttr(ppd
, "PageStackOrder", choice
->choice
)) != NULL
&&
2640 doc
->output_order
= !strcasecmp(attr
->value
, "Reverse");
2641 else if ((attr
= ppdFindAttr(ppd
, "DefaultOutputOrder", NULL
)) != NULL
&&
2643 doc
->output_order
= !strcasecmp(attr
->value
, "Reverse");
2650 if ((val
= cupsGetOption("page-border", num_options
, options
)) != NULL
)
2652 if (!strcasecmp(val
, "none"))
2653 doc
->page_border
= PSTOPS_BORDERNONE
;
2654 else if (!strcasecmp(val
, "single"))
2655 doc
->page_border
= PSTOPS_BORDERSINGLE
;
2656 else if (!strcasecmp(val
, "single-thick"))
2657 doc
->page_border
= PSTOPS_BORDERSINGLE2
;
2658 else if (!strcasecmp(val
, "double"))
2659 doc
->page_border
= PSTOPS_BORDERDOUBLE
;
2660 else if (!strcasecmp(val
, "double-thick"))
2661 doc
->page_border
= PSTOPS_BORDERDOUBLE2
;
2664 _cupsLangPrintFilter(stderr
, "ERROR",
2665 _("Unsupported page-border value %s, using "
2666 "page-border=none."), val
);
2667 doc
->page_border
= PSTOPS_BORDERNONE
;
2671 doc
->page_border
= PSTOPS_BORDERNONE
;
2677 doc
->page_label
= cupsGetOption("page-label", num_options
, options
);
2683 doc
->page_ranges
= cupsGetOption("page-ranges", num_options
, options
);
2689 doc
->page_set
= cupsGetOption("page-set", num_options
, options
);
2692 * Now figure out if we have to force collated copies, etc.
2695 if (ppd
&& ppd
->manual_copies
&& Duplex
&& doc
->copies
> 1)
2698 * Force collated copies when printing a duplexed document to
2699 * a non-PS printer that doesn't do hardware copy generation.
2700 * Otherwise the copies will end up on the front/back side of
2708 * See if we have to filter the fast or slow way...
2711 if (doc
->collate
&& doc
->copies
> 1)
2714 * See if we need to manually collate the pages...
2717 doc
->slow_collate
= 1;
2719 if ((choice
= ppdFindMarkedChoice(ppd
, "Collate")) != NULL
&&
2720 !strcasecmp(choice
->choice
, "True"))
2723 * Hardware collate option is selected, see if the option is
2724 * conflicting - if not, collate in hardware. Otherwise,
2725 * turn the hardware collate option off...
2728 if ((option
= ppdFindOption(ppd
, "Collate")) != NULL
&&
2729 !option
->conflicted
)
2730 doc
->slow_collate
= 0;
2732 ppdMarkOption(ppd
, "Collate", "False");
2736 doc
->slow_collate
= 0;
2738 if (!ppdFindOption(ppd
, "OutputOrder") && doc
->output_order
)
2739 doc
->slow_order
= 1;
2741 doc
->slow_order
= 0;
2744 (doc
->slow_collate
|| doc
->slow_order
||
2745 ((attr
= ppdFindAttr(ppd
, "cupsEvenDuplex", NULL
)) != NULL
&&
2746 attr
->value
&& !strcasecmp(attr
->value
, "true"))))
2747 doc
->slow_duplex
= 1;
2749 doc
->slow_duplex
= 0;
2752 * Create a temporary file for page data if we need to filter slowly...
2755 if (doc
->slow_order
|| doc
->slow_collate
)
2757 if ((doc
->temp
= cupsTempFile2(doc
->tempfile
,
2758 sizeof(doc
->tempfile
))) == NULL
)
2760 perror("DEBUG: Unable to create temporary file");
2766 * Figure out if we should use ESPshowpage or not...
2769 if (doc
->page_label
|| getenv("CLASSIFICATION") || doc
->number_up
> 1 ||
2773 * Yes, use ESPshowpage...
2776 doc
->use_ESPshowpage
= 1;
2779 fprintf(stderr
, "DEBUG: slow_collate=%d, slow_duplex=%d, slow_order=%d\n",
2780 doc
->slow_collate
, doc
->slow_duplex
, doc
->slow_order
);
2785 * 'skip_page()' - Skip past a page that won't be printed.
2788 static ssize_t
/* O - Length of next line */
2789 skip_page(cups_file_t
*fp
, /* I - File to read from */
2790 char *line
, /* I - Line buffer */
2791 ssize_t linelen
, /* I - Length of initial line */
2792 size_t linesize
) /* I - Size of line buffer */
2794 int level
; /* Embedded document level */
2799 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
2802 (!strncmp(line
, "%%Page:", 7) || !strncmp(line
, "%%Trailer", 9)))
2804 else if (!strncmp(line
, "%%BeginDocument", 15) ||
2805 !strncmp(line
, "%ADO_BeginApplication", 21))
2807 else if ((!strncmp(line
, "%%EndDocument", 13) ||
2808 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
2810 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
2811 (!strncmp(line
, "%%BeginData:", 12) &&
2812 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
2815 * Skip binary data...
2818 int bytes
; /* Bytes of data */
2821 bytes
= atoi(strchr(line
, ':') + 1);
2825 if (bytes
> linesize
)
2826 linelen
= cupsFileRead(fp
, line
, linesize
);
2828 linelen
= cupsFileRead(fp
, line
, bytes
);
2833 perror("ERROR: Early end-of-file while reading binary data");
2847 * 'start_nup()' - Start processing for N-up printing.
2851 start_nup(pstops_doc_t
*doc
, /* I - Document information */
2852 int number
, /* I - Page number */
2853 int show_border
, /* I - Show the border? */
2854 const int *bounding_box
) /* I - BoundingBox value */
2856 int pos
; /* Position on page */
2857 int x
, y
; /* Relative position of subpage */
2858 float w
, l
, /* Width and length of subpage */
2859 tx
, ty
; /* Translation values for subpage */
2860 float pagew
, /* Printable width of page */
2861 pagel
; /* Printable height of page */
2862 int bboxx
, /* BoundingBox X origin */
2863 bboxy
, /* BoundingBox Y origin */
2864 bboxw
, /* BoundingBox width */
2865 bboxl
; /* BoundingBox height */
2866 float margin
= 0; /* Current margin for border */
2869 if (doc
->number_up
> 1)
2870 doc_puts(doc
, "userdict/ESPsave save put\n");
2872 pos
= (number
- 1) % doc
->number_up
;
2873 pagew
= PageRight
- PageLeft
;
2874 pagel
= PageTop
- PageBottom
;
2878 bboxx
= bounding_box
[0];
2879 bboxy
= bounding_box
[1];
2880 bboxw
= bounding_box
[2] - bounding_box
[0];
2881 bboxl
= bounding_box
[3] - bounding_box
[1];
2891 fprintf(stderr
, "DEBUG: pagew = %.1f, pagel = %.1f\n", pagew
, pagel
);
2892 fprintf(stderr
, "DEBUG: bboxx = %d, bboxy = %d, bboxw = %d, bboxl = %d\n",
2893 bboxx
, bboxy
, bboxw
, bboxl
);
2894 fprintf(stderr
, "DEBUG: PageLeft = %.1f, PageRight = %.1f\n",
2895 PageLeft
, PageRight
);
2896 fprintf(stderr
, "DEBUG: PageTop = %.1f, PageBottom = %.1f\n",
2897 PageTop
, PageBottom
);
2898 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
2899 PageWidth
, PageLength
);
2901 switch (Orientation
)
2903 case 1 : /* Landscape */
2904 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", PageLength
);
2906 case 2 : /* Reverse Portrait */
2907 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", PageWidth
,
2910 case 3 : /* Reverse Landscape */
2911 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", PageWidth
);
2915 if (Duplex
&& doc
->number_up
> 1 && ((number
/ doc
->number_up
) & 1))
2916 doc_printf(doc
, "%.1f %.1f translate\n", PageWidth
- PageRight
, PageBottom
);
2917 else if (doc
->number_up
> 1 || doc
->fitplot
)
2918 doc_printf(doc
, "%.1f %.1f translate\n", PageLeft
, PageBottom
);
2920 switch (doc
->number_up
)
2926 l
= w
* bboxl
/ bboxw
;
2931 w
= l
* bboxw
/ bboxl
;
2934 tx
= 0.5 * (pagew
- w
);
2935 ty
= 0.5 * (pagel
- l
);
2937 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n", tx
, ty
,
2938 w
/ bboxw
, l
/ bboxl
);
2945 if (Orientation
& 1)
2949 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2953 l
= w
* bboxl
/ bboxw
;
2955 if (l
> (pagew
* 0.5))
2958 w
= l
* bboxw
/ bboxl
;
2961 tx
= 0.5 * (pagew
* 0.5 - l
);
2962 ty
= 0.5 * (pagel
- w
);
2964 if (doc
->normal_landscape
)
2965 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2967 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2969 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2970 ty
, tx
+ pagew
* 0.5 * x
, w
/ bboxw
, l
/ bboxl
);
2976 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2980 w
= l
* bboxw
/ bboxl
;
2982 if (w
> (pagel
* 0.5))
2985 l
= w
* bboxl
/ bboxw
;
2988 tx
= 0.5 * (pagel
* 0.5 - w
);
2989 ty
= 0.5 * (pagew
- l
);
2991 if (doc
->normal_landscape
)
2992 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2994 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2996 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2997 tx
+ pagel
* 0.5 * x
, ty
, w
/ bboxw
, l
/ bboxl
);
3002 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3013 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3016 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3020 l
= w
* bboxl
/ bboxw
;
3022 if (l
> (pagel
* 0.5))
3025 w
= l
* bboxw
/ bboxl
;
3028 tx
= 0.5 * (pagew
* 0.5 - w
);
3029 ty
= 0.5 * (pagel
* 0.5 - l
);
3031 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3032 tx
+ x
* pagew
* 0.5, ty
+ y
* pagel
* 0.5,
3033 w
/ bboxw
, l
/ bboxl
);
3037 if (Orientation
& 1)
3039 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3044 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3047 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3055 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3058 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3063 l
= w
* bboxl
/ bboxw
;
3065 if (l
> (pagew
* 0.333))
3068 w
= l
* bboxw
/ bboxl
;
3071 tx
= 0.5 * (pagel
- 2 * w
);
3072 ty
= 0.5 * (pagew
- 3 * l
);
3074 if (doc
->normal_landscape
)
3075 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
3077 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
3079 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3080 tx
+ x
* w
, ty
+ y
* l
, l
/ bboxl
, w
/ bboxw
);
3084 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3089 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3092 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3100 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3103 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3108 w
= l
* bboxw
/ bboxl
;
3110 if (w
> (pagel
* 0.333))
3113 l
= w
* bboxl
/ bboxw
;
3116 tx
= 0.5 * (pagel
- 3 * w
);
3117 ty
= 0.5 * (pagew
- 2 * l
);
3119 if (doc
->normal_landscape
)
3120 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
3122 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
3124 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3125 tx
+ w
* x
, ty
+ l
* y
, w
/ bboxw
, l
/ bboxl
);
3131 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3142 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3145 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3149 l
= w
* bboxl
/ bboxw
;
3151 if (l
> (pagel
* 0.333))
3154 w
= l
* bboxw
/ bboxl
;
3157 tx
= 0.5 * (pagew
* 0.333 - w
);
3158 ty
= 0.5 * (pagel
* 0.333 - l
);
3160 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3161 tx
+ x
* pagew
* 0.333, ty
+ y
* pagel
* 0.333,
3162 w
/ bboxw
, l
/ bboxl
);
3166 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3177 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3180 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3184 l
= w
* bboxl
/ bboxw
;
3186 if (l
> (pagel
* 0.25))
3189 w
= l
* bboxw
/ bboxl
;
3192 tx
= 0.5 * (pagew
* 0.25 - w
);
3193 ty
= 0.5 * (pagel
* 0.25 - l
);
3195 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3196 tx
+ x
* pagew
* 0.25, ty
+ y
* pagel
* 0.25,
3197 w
/ bboxw
, l
/ bboxl
);
3202 * Draw borders as necessary...
3205 if (doc
->page_border
&& show_border
)
3207 int rects
; /* Number of border rectangles */
3208 float fscale
; /* Scaling value for points */
3211 rects
= (doc
->page_border
& PSTOPS_BORDERDOUBLE
) ? 2 : 1;
3212 fscale
= PageWidth
/ w
;
3213 margin
= 2.25 * fscale
;
3216 * Set the line width and color...
3219 doc_puts(doc
, "gsave\n");
3220 doc_printf(doc
, "%.3f setlinewidth 0 setgray newpath\n",
3221 (doc
->page_border
& PSTOPS_BORDERTHICK
) ? 0.5 * fscale
:
3225 * Draw border boxes...
3228 for (; rects
> 0; rects
--, margin
+= 2 * fscale
)
3229 if (doc
->number_up
> 1)
3230 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3234 bboxl
- 2 * margin
);
3236 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3238 PageBottom
+ margin
,
3239 PageRight
- PageLeft
- 2 * margin
,
3240 PageTop
- PageBottom
- 2 * margin
);
3243 * Restore pen settings...
3246 doc_puts(doc
, "grestore\n");
3252 * Offset the page by its bounding box...
3255 doc_printf(doc
, "%d %d translate\n", -bounding_box
[0],
3259 if (doc
->fitplot
|| doc
->number_up
> 1)
3262 * Clip the page to the page's bounding box...
3265 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrc\n",
3266 bboxx
+ margin
, bboxy
+ margin
,
3267 bboxw
- 2 * margin
, bboxl
- 2 * margin
);
3271 * Mirror the page as needed...
3275 doc_printf(doc
, "%.1f 0.0 translate -1 1 scale\n", PageWidth
);
3280 * 'write_label_prolog()' - Write the prolog with the classification
3285 write_label_prolog(pstops_doc_t
*doc
, /* I - Document info */
3286 const char *label
, /* I - Page label */
3287 float bottom
, /* I - Bottom position in points */
3288 float top
, /* I - Top position in points */
3289 float width
) /* I - Width in points */
3291 const char *classification
; /* CLASSIFICATION environment variable */
3292 const char *ptr
; /* Temporary string pointer */
3296 * First get the current classification...
3299 if ((classification
= getenv("CLASSIFICATION")) == NULL
)
3300 classification
= "";
3301 if (strcmp(classification
, "none") == 0)
3302 classification
= "";
3305 * If there is nothing to show, bind an empty 'write labels' procedure
3309 if (!classification
[0] && (label
== NULL
|| !label
[0]))
3311 doc_puts(doc
, "userdict/ESPwl{}bind put\n");
3316 * Set the classification + page label string...
3319 doc_puts(doc
, "userdict");
3320 if (!strcmp(classification
, "confidential"))
3321 doc_puts(doc
, "/ESPpl(CONFIDENTIAL");
3322 else if (!strcmp(classification
, "classified"))
3323 doc_puts(doc
, "/ESPpl(CLASSIFIED");
3324 else if (!strcmp(classification
, "secret"))
3325 doc_puts(doc
, "/ESPpl(SECRET");
3326 else if (!strcmp(classification
, "topsecret"))
3327 doc_puts(doc
, "/ESPpl(TOP SECRET");
3328 else if (!strcmp(classification
, "unclassified"))
3329 doc_puts(doc
, "/ESPpl(UNCLASSIFIED");
3332 doc_puts(doc
, "/ESPpl(");
3334 for (ptr
= classification
; *ptr
; ptr
++)
3336 if (*ptr
< 32 || *ptr
> 126)
3337 doc_printf(doc
, "\\%03o", *ptr
);
3338 else if (*ptr
== '_')
3340 else if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
3341 doc_printf(doc
, "\\%c", *ptr
);
3343 doc_printf(doc
, "%c", *ptr
);
3349 if (classification
[0])
3350 doc_puts(doc
, " - ");
3353 * Quote the label string as needed...
3356 for (ptr
= label
; *ptr
; ptr
++)
3358 if (*ptr
< 32 || *ptr
> 126)
3359 doc_printf(doc
, "\\%03o", *ptr
);
3360 else if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
3361 doc_printf(doc
, "\\%c", *ptr
);
3363 doc_printf(doc
, "%c", *ptr
);
3367 doc_puts(doc
, ")put\n");
3370 * Then get a 14 point Helvetica-Bold font...
3373 doc_puts(doc
, "userdict/ESPpf /Helvetica-Bold findfont 14 scalefont put\n");
3376 * Finally, the procedure to write the labels on the page...
3379 doc_puts(doc
, "userdict/ESPwl{\n");
3380 doc_puts(doc
, " ESPpf setfont\n");
3381 doc_printf(doc
, " ESPpl stringwidth pop dup 12 add exch -0.5 mul %.0f add\n",
3383 doc_puts(doc
, " 1 setgray\n");
3384 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrf\n", bottom
- 2.0);
3385 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrf\n", top
- 18.0);
3386 doc_puts(doc
, " 0 setgray\n");
3387 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrs\n", bottom
- 2.0);
3388 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrs\n", top
- 18.0);
3389 doc_printf(doc
, " dup %.0f moveto ESPpl show\n", bottom
+ 2.0);
3390 doc_printf(doc
, " %.0f moveto ESPpl show\n", top
- 14.0);
3391 doc_puts(doc
, "pop\n");
3392 doc_puts(doc
, "}bind put\n");
3397 * 'write_labels()' - Write the actual page labels.
3399 * This function is a copy of the one in common.c since we need to
3400 * use doc_puts/doc_printf instead of puts/printf...
3404 write_labels(pstops_doc_t
*doc
, /* I - Document information */
3405 int orient
) /* I - Orientation of the page */
3407 float width
, /* Width of page */
3408 length
; /* Length of page */
3411 doc_puts(doc
, "gsave\n");
3413 if ((orient
^ Orientation
) & 1)
3421 length
= PageLength
;
3426 case 1 : /* Landscape */
3427 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", length
);
3429 case 2 : /* Reverse Portrait */
3430 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", width
, length
);
3432 case 3 : /* Reverse Landscape */
3433 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", width
);
3437 doc_puts(doc
, "ESPwl\n");
3438 doc_puts(doc
, "grestore\n");
3443 * 'write_options()' - Write options provided via %%IncludeFeature.
3448 pstops_doc_t
*doc
, /* I - Document */
3449 ppd_file_t
*ppd
, /* I - PPD file */
3450 int num_options
, /* I - Number of options */
3451 cups_option_t
*options
) /* I - Options */
3453 int i
; /* Looping var */
3454 ppd_option_t
*option
; /* PPD option */
3455 int min_order
; /* Minimum OrderDependency value */
3456 char *doc_setup
, /* DocumentSetup commands to send */
3457 *any_setup
; /* AnySetup commands to send */
3461 * Figure out the minimum OrderDependency value...
3464 if ((option
= ppdFindOption(ppd
, "PageRegion")) != NULL
)
3465 min_order
= option
->order
;
3469 for (i
= 0; i
< num_options
; i
++)
3470 if ((option
= ppdFindOption(ppd
, options
[i
].name
)) != NULL
&&
3471 option
->order
< min_order
)
3472 min_order
= option
->order
;
3475 * Mark and extract them...
3478 cupsMarkOptions(ppd
, num_options
, options
);
3480 doc_setup
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, min_order
);
3481 any_setup
= ppdEmitString(ppd
, PPD_ORDER_ANY
, min_order
);
3484 * Then send them out...
3487 if (doc
->number_up
> 1)
3490 * Temporarily restore setpagedevice so we can set the options...
3493 doc_puts(doc
, "userdict/setpagedevice/CUPSsetpagedevice load put\n");
3498 doc_puts(doc
, doc_setup
);
3504 doc_puts(doc
, any_setup
);
3508 if (doc
->number_up
> 1)
3511 * Disable setpagedevice again...
3514 doc_puts(doc
, "userdict/setpagedevice{pop}bind put\n");
3520 * End of "$Id: pstops.c 7977 2008-09-23 23:44:33Z mike $".