]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/pstops.c
4 * PostScript filter for CUPS.
6 * Copyright 2007-2014 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 * Include necessary headers...
25 #include <cups/file.h>
26 #include <cups/array.h>
27 #include <cups/language-private.h>
35 #define PSTOPS_BORDERNONE 0 /* No border or hairline border */
36 #define PSTOPS_BORDERTHICK 1 /* Think border */
37 #define PSTOPS_BORDERSINGLE 2 /* Single-line hairline border */
38 #define PSTOPS_BORDERSINGLE2 3 /* Single-line thick border */
39 #define PSTOPS_BORDERDOUBLE 4 /* Double-line hairline border */
40 #define PSTOPS_BORDERDOUBLE2 5 /* Double-line thick border */
42 #define PSTOPS_LAYOUT_LRBT 0 /* Left to right, bottom to top */
43 #define PSTOPS_LAYOUT_LRTB 1 /* Left to right, top to bottom */
44 #define PSTOPS_LAYOUT_RLBT 2 /* Right to left, bottom to top */
45 #define PSTOPS_LAYOUT_RLTB 3 /* Right to left, top to bottom */
46 #define PSTOPS_LAYOUT_BTLR 4 /* Bottom to top, left to right */
47 #define PSTOPS_LAYOUT_TBLR 5 /* Top to bottom, left to right */
48 #define PSTOPS_LAYOUT_BTRL 6 /* Bottom to top, right to left */
49 #define PSTOPS_LAYOUT_TBRL 7 /* Top to bottom, right to left */
51 #define PSTOPS_LAYOUT_NEGATEY 1 /* The bits for the layout */
52 #define PSTOPS_LAYOUT_NEGATEX 2 /* definitions above... */
53 #define PSTOPS_LAYOUT_VERTICAL 4
60 typedef struct /**** Page information ****/
62 char *label
; /* Page label */
63 int bounding_box
[4]; /* PageBoundingBox */
64 off_t offset
; /* Offset to start of page */
65 ssize_t length
; /* Number of bytes for page */
66 int num_options
; /* Number of options for this page */
67 cups_option_t
*options
; /* Options for this page */
70 typedef struct /**** Document information ****/
72 int page
; /* Current page */
73 int bounding_box
[4]; /* BoundingBox from header */
74 int new_bounding_box
[4]; /* New composite bounding box */
75 int num_options
; /* Number of document-wide options */
76 cups_option_t
*options
; /* Document-wide options */
77 int normal_landscape
, /* Normal rotation for landscape? */
78 saw_eof
, /* Saw the %%EOF comment? */
79 slow_collate
, /* Collate copies by hand? */
80 slow_duplex
, /* Duplex pages slowly? */
81 slow_order
, /* Reverse pages slowly? */
82 use_ESPshowpage
; /* Use ESPshowpage? */
83 cups_array_t
*pages
; /* Pages in document */
84 cups_file_t
*temp
; /* Temporary file, if any */
85 char tempfile
[1024]; /* Temporary filename */
86 int job_id
; /* Job ID */
87 const char *user
, /* User name */
88 *title
; /* Job name */
89 int copies
; /* Number of copies */
90 const char *ap_input_slot
, /* AP_FIRSTPAGE_InputSlot value */
91 *ap_manual_feed
, /* AP_FIRSTPAGE_ManualFeed value */
92 *ap_media_color
, /* AP_FIRSTPAGE_MediaColor value */
93 *ap_media_type
, /* AP_FIRSTPAGE_MediaType value */
94 *ap_page_region
, /* AP_FIRSTPAGE_PageRegion value */
95 *ap_page_size
; /* AP_FIRSTPAGE_PageSize value */
96 int collate
, /* Collate copies? */
97 emit_jcl
, /* Emit JCL commands? */
98 fit_to_page
; /* Fit pages to media */
99 const char *input_slot
, /* InputSlot value */
100 *manual_feed
, /* ManualFeed value */
101 *media_color
, /* MediaColor value */
102 *media_type
, /* MediaType value */
103 *page_region
, /* PageRegion value */
104 *page_size
; /* PageSize value */
105 int mirror
, /* doc->mirror/mirror pages */
106 number_up
, /* Number of pages on each sheet */
107 number_up_layout
, /* doc->number_up_layout of N-up pages */
108 output_order
, /* Requested reverse output order? */
109 page_border
; /* doc->page_border around pages */
110 const char *page_label
, /* page-label option, if any */
111 *page_ranges
, /* page-ranges option, if any */
112 *page_set
; /* page-set option, if any */
117 * Convenience macros...
120 #define is_first_page(p) (doc->number_up == 1 || \
121 ((p) % doc->number_up) == 1)
122 #define is_last_page(p) (doc->number_up == 1 || \
123 ((p) % doc->number_up) == 0)
124 #define is_not_last_page(p) (doc->number_up > 1 && \
125 ((p) % doc->number_up) != 0)
132 static int JobCanceled
= 0;/* Set to 1 on SIGTERM */
139 static pstops_page_t
*add_page(pstops_doc_t
*doc
, const char *label
);
140 static void cancel_job(int sig
);
141 static int check_range(pstops_doc_t
*doc
, int page
);
142 static void copy_bytes(cups_file_t
*fp
, off_t offset
,
144 static ssize_t
copy_comments(cups_file_t
*fp
, pstops_doc_t
*doc
,
145 ppd_file_t
*ppd
, char *line
,
146 ssize_t linelen
, size_t linesize
);
147 static void copy_dsc(cups_file_t
*fp
, pstops_doc_t
*doc
,
148 ppd_file_t
*ppd
, char *line
, ssize_t linelen
,
150 static void copy_non_dsc(cups_file_t
*fp
, pstops_doc_t
*doc
,
151 ppd_file_t
*ppd
, char *line
,
152 ssize_t linelen
, size_t linesize
);
153 static ssize_t
copy_page(cups_file_t
*fp
, pstops_doc_t
*doc
,
154 ppd_file_t
*ppd
, int number
, char *line
,
155 ssize_t linelen
, size_t linesize
);
156 static ssize_t
copy_prolog(cups_file_t
*fp
, pstops_doc_t
*doc
,
157 ppd_file_t
*ppd
, char *line
,
158 ssize_t linelen
, size_t linesize
);
159 static ssize_t
copy_setup(cups_file_t
*fp
, pstops_doc_t
*doc
,
160 ppd_file_t
*ppd
, char *line
,
161 ssize_t linelen
, size_t linesize
);
162 static ssize_t
copy_trailer(cups_file_t
*fp
, pstops_doc_t
*doc
,
163 ppd_file_t
*ppd
, int number
, char *line
,
164 ssize_t linelen
, size_t linesize
);
165 static void do_prolog(pstops_doc_t
*doc
, ppd_file_t
*ppd
);
166 static void do_setup(pstops_doc_t
*doc
, ppd_file_t
*ppd
);
167 static void doc_printf(pstops_doc_t
*doc
, const char *format
, ...)
168 __attribute__ ((__format__ (__printf__
, 2, 3)));
169 static void doc_puts(pstops_doc_t
*doc
, const char *s
);
170 static void doc_write(pstops_doc_t
*doc
, const char *s
, size_t len
);
171 static void end_nup(pstops_doc_t
*doc
, int number
);
172 static int include_feature(ppd_file_t
*ppd
, const char *line
,
174 cups_option_t
**options
);
175 static char *parse_text(const char *start
, char **end
, char *buffer
,
177 static void set_pstops_options(pstops_doc_t
*doc
, ppd_file_t
*ppd
,
178 char *argv
[], int num_options
,
179 cups_option_t
*options
);
180 static ssize_t
skip_page(cups_file_t
*fp
, char *line
, ssize_t linelen
,
182 static void start_nup(pstops_doc_t
*doc
, int number
,
183 int show_border
, const int *bounding_box
);
184 static void write_label_prolog(pstops_doc_t
*doc
, const char *label
,
185 float bottom
, float top
,
187 static void write_labels(pstops_doc_t
*doc
, int orient
);
188 static void write_options(pstops_doc_t
*doc
, ppd_file_t
*ppd
,
189 int num_options
, cups_option_t
*options
);
193 * 'main()' - Main entry.
196 int /* O - Exit status */
197 main(int argc
, /* I - Number of command-line args */
198 char *argv
[]) /* I - Command-line arguments */
200 pstops_doc_t doc
; /* Document information */
201 cups_file_t
*fp
; /* Print file */
202 ppd_file_t
*ppd
; /* PPD file */
203 int num_options
; /* Number of print options */
204 cups_option_t
*options
; /* Print options */
205 char line
[8192]; /* Line buffer */
206 ssize_t len
; /* Length of line buffer */
207 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
208 struct sigaction action
; /* Actions for POSIX signals */
209 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
213 * Make sure status messages are not buffered...
216 setbuf(stderr
, NULL
);
219 * Ignore broken pipe signals...
222 signal(SIGPIPE
, SIG_IGN
);
225 * Check command-line...
228 if (argc
< 6 || argc
> 7)
230 _cupsLangPrintf(stderr
,
231 _("Usage: %s job-id user title copies options [file]"),
237 * Register a signal handler to cleanly cancel a job.
240 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
241 sigset(SIGTERM
, cancel_job
);
242 #elif defined(HAVE_SIGACTION)
243 memset(&action
, 0, sizeof(action
));
245 sigemptyset(&action
.sa_mask
);
246 action
.sa_handler
= cancel_job
;
247 sigaction(SIGTERM
, &action
, NULL
);
249 signal(SIGTERM
, cancel_job
);
250 #endif /* HAVE_SIGSET */
253 * If we have 7 arguments, print the file named on the command-line.
254 * Otherwise, send stdin instead...
258 fp
= cupsFileStdin();
262 * Try to open the print file...
265 if ((fp
= cupsFileOpen(argv
[6], "r")) == NULL
)
267 _cupsLangPrintError("ERROR", _("Unable to open print file"));
273 * Read the first line to see if we have DSC comments...
276 if ((len
= (ssize_t
)cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
278 fputs("DEBUG: The print file is empty.\n", stderr
);
283 * Process command-line options...
287 num_options
= cupsParseOptions(argv
[5], 0, &options
);
288 ppd
= SetCommonOptions(num_options
, options
, 1);
290 set_pstops_options(&doc
, ppd
, argv
, num_options
, options
);
293 * Write any "exit server" options that have been selected...
296 ppdEmit(ppd
, stdout
, PPD_ORDER_EXIT
);
299 * Write any JCL commands that are needed to print PostScript code...
303 ppdEmitJCL(ppd
, stdout
, doc
.job_id
, doc
.user
, doc
.title
);
306 * Start with a DSC header...
309 puts("%!PS-Adobe-3.0");
312 * Skip leading PJL in the document...
315 while (!strncmp(line
, "\033%-12345X", 9) || !strncmp(line
, "@PJL ", 5))
318 * Yup, we have leading PJL fun, so skip it until we hit the line
319 * with "ENTER LANGUAGE"...
322 fputs("DEBUG: Skipping PJL header...\n", stderr
);
324 while (strstr(line
, "ENTER LANGUAGE") == NULL
&& strncmp(line
, "%!", 2))
325 if ((len
= (ssize_t
)cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
328 if (!strncmp(line
, "%!", 2))
331 if ((len
= (ssize_t
)cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
336 * Now see if the document conforms to the Adobe Document Structuring
340 if (!strncmp(line
, "%!PS-Adobe-", 11))
343 * Yes, filter the document...
346 copy_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
351 * No, display an error message and treat the file as if it contains
355 copy_non_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
359 * Send %%EOF as needed...
366 * End the job with the appropriate JCL command or CTRL-D...
371 if (ppd
&& ppd
->jcl_end
)
372 ppdEmitJCLEnd(ppd
, stdout
);
378 * Close files and remove the temporary file if needed...
383 cupsFileClose(doc
.temp
);
384 unlink(doc
.tempfile
);
388 cupsFreeOptions(num_options
, options
);
397 * 'add_page()' - Add a page to the pages array.
400 static pstops_page_t
* /* O - New page info object */
401 add_page(pstops_doc_t
*doc
, /* I - Document information */
402 const char *label
) /* I - Page label */
404 pstops_page_t
*pageinfo
; /* New page info object */
408 doc
->pages
= cupsArrayNew(NULL
, NULL
);
412 _cupsLangPrintError("EMERG", _("Unable to allocate memory for pages array"));
416 if ((pageinfo
= calloc(1, sizeof(pstops_page_t
))) == NULL
)
418 _cupsLangPrintError("EMERG", _("Unable to allocate memory for page info"));
422 pageinfo
->label
= strdup(label
);
423 pageinfo
->offset
= cupsFileTell(doc
->temp
);
425 cupsArrayAdd(doc
->pages
, pageinfo
);
434 * 'cancel_job()' - Flag the job as canceled.
438 cancel_job(int sig
) /* I - Signal number (unused) */
447 * 'check_range()' - Check to see if the current page is selected for
451 static int /* O - 1 if selected, 0 otherwise */
452 check_range(pstops_doc_t
*doc
, /* I - Document information */
453 int page
) /* I - Page number */
455 const char *range
; /* Pointer into range string */
456 int lower
, upper
; /* Lower and upper page numbers */
462 * See if we only print even or odd pages...
465 if (!_cups_strcasecmp(doc
->page_set
, "even") && (page
& 1))
468 if (!_cups_strcasecmp(doc
->page_set
, "odd") && !(page
& 1))
472 if (!doc
->page_ranges
)
473 return (1); /* No range, print all pages... */
475 for (range
= doc
->page_ranges
; *range
!= '\0';)
481 upper
= (int)strtol(range
, (char **)&range
, 10);
485 lower
= (int)strtol(range
, (char **)&range
, 10);
490 if (!isdigit(*range
& 255))
493 upper
= (int)strtol(range
, (char **)&range
, 10);
499 if (page
>= lower
&& page
<= upper
)
513 * 'copy_bytes()' - Copy bytes from the input file to stdout.
517 copy_bytes(cups_file_t
*fp
, /* I - File to read from */
518 off_t offset
, /* I - Offset to page data */
519 size_t length
) /* I - Length of page data */
521 char buffer
[8192]; /* Data buffer */
522 ssize_t nbytes
; /* Number of bytes read */
523 size_t nleft
; /* Number of bytes left/remaining */
528 if (cupsFileSeek(fp
, offset
) < 0)
530 _cupsLangPrintError("ERROR", _("Unable to see in file"));
534 while (nleft
> 0 || length
== 0)
536 if (nleft
> sizeof(buffer
) || length
== 0)
537 nbytes
= sizeof(buffer
);
539 nbytes
= (ssize_t
)nleft
;
541 if ((nbytes
= cupsFileRead(fp
, buffer
, (size_t)nbytes
)) < 1)
544 nleft
-= (size_t)nbytes
;
546 fwrite(buffer
, 1, (size_t)nbytes
, stdout
);
552 * 'copy_comments()' - Copy all of the comments section.
554 * This function expects "line" to be filled with a comment line.
555 * On return, "line" will contain the next line in the file, if any.
558 static ssize_t
/* O - Length of next line */
559 copy_comments(cups_file_t
*fp
, /* I - File to read from */
560 pstops_doc_t
*doc
, /* I - Document info */
561 ppd_file_t
*ppd
, /* I - PPD file */
562 char *line
, /* I - Line buffer */
563 ssize_t linelen
, /* I - Length of initial line */
564 size_t linesize
) /* I - Size of line buffer */
566 int saw_bounding_box
, /* Saw %%BoundingBox: comment? */
567 saw_for
, /* Saw %%For: comment? */
568 saw_pages
, /* Saw %%Pages: comment? */
569 saw_title
; /* Saw %%Title: comment? */
573 * Loop until we see %%EndComments or a non-comment line...
576 saw_bounding_box
= 0;
581 while (line
[0] == '%')
584 * Strip trailing whitespace...
591 if (!isspace(line
[linelen
] & 255))
594 line
[linelen
] = '\0';
601 fprintf(stderr
, "DEBUG: %s\n", line
);
604 * Pull the headers out...
607 if (!strncmp(line
, "%%Pages:", 8))
609 int pages
; /* Number of pages */
612 fputs("DEBUG: A duplicate %%Pages: comment was seen.\n", stderr
);
616 if (Duplex
&& (pages
= atoi(line
+ 8)) > 0 && pages
<= doc
->number_up
)
619 * Since we will only be printing on a single page, disable duplexing.
623 doc
->slow_duplex
= 0;
625 if (cupsGetOption("sides", doc
->num_options
, doc
->options
))
626 doc
->num_options
= cupsAddOption("sides", "one-sided",
627 doc
->num_options
, &(doc
->options
));
629 if (cupsGetOption("Duplex", doc
->num_options
, doc
->options
))
630 doc
->num_options
= cupsAddOption("Duplex", "None",
631 doc
->num_options
, &(doc
->options
));
633 if (cupsGetOption("EFDuplex", doc
->num_options
, doc
->options
))
634 doc
->num_options
= cupsAddOption("EFDuplex", "None",
635 doc
->num_options
, &(doc
->options
));
637 if (cupsGetOption("EFDuplexing", doc
->num_options
, doc
->options
))
638 doc
->num_options
= cupsAddOption("EFDuplexing", "False",
639 doc
->num_options
, &(doc
->options
));
641 if (cupsGetOption("KD03Duplex", doc
->num_options
, doc
->options
))
642 doc
->num_options
= cupsAddOption("KD03Duplex", "None",
643 doc
->num_options
, &(doc
->options
));
645 if (cupsGetOption("JCLDuplex", doc
->num_options
, doc
->options
))
646 doc
->num_options
= cupsAddOption("JCLDuplex", "None",
647 doc
->num_options
, &(doc
->options
));
649 ppdMarkOption(ppd
, "Duplex", "None");
650 ppdMarkOption(ppd
, "EFDuplex", "None");
651 ppdMarkOption(ppd
, "EFDuplexing", "False");
652 ppdMarkOption(ppd
, "KD03Duplex", "None");
653 ppdMarkOption(ppd
, "JCLDuplex", "None");
656 else if (!strncmp(line
, "%%BoundingBox:", 14))
658 if (saw_bounding_box
)
659 fputs("DEBUG: A duplicate %%BoundingBox: comment was seen.\n", stderr
);
660 else if (strstr(line
+ 14, "(atend)"))
663 * Do nothing for now but use the default imageable area...
666 else if (sscanf(line
+ 14, "%d%d%d%d", doc
->bounding_box
+ 0,
667 doc
->bounding_box
+ 1, doc
->bounding_box
+ 2,
668 doc
->bounding_box
+ 3) != 4)
670 fputs("DEBUG: A bad %%BoundingBox: comment was seen.\n", stderr
);
672 doc
->bounding_box
[0] = (int)PageLeft
;
673 doc
->bounding_box
[1] = (int)PageBottom
;
674 doc
->bounding_box
[2] = (int)PageRight
;
675 doc
->bounding_box
[3] = (int)PageTop
;
678 saw_bounding_box
= 1;
680 else if (!strncmp(line
, "%%For:", 6))
683 doc_printf(doc
, "%s\n", line
);
685 else if (!strncmp(line
, "%%Title:", 8))
688 doc_printf(doc
, "%s\n", line
);
690 else if (!strncmp(line
, "%cupsRotation:", 14))
693 * Reset orientation of document?
696 int orient
= (atoi(line
+ 14) / 90) & 3;
698 if (orient
!= Orientation
)
701 * Yes, update things so that the pages come out right...
704 Orientation
= (4 - Orientation
+ orient
) & 3;
706 Orientation
= orient
;
709 else if (!strcmp(line
, "%%EndComments"))
711 linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
);
714 else if (strncmp(line
, "%!", 2) && strncmp(line
, "%cups", 5))
715 doc_printf(doc
, "%s\n", line
);
717 if ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) == 0)
721 if (!saw_bounding_box
)
722 fputs("DEBUG: There wasn't a %%BoundingBox: comment in the header.\n",
726 fputs("DEBUG: There wasn't a %%Pages: comment in the header.\n", stderr
);
729 WriteTextComment("For", doc
->user
);
732 WriteTextComment("Title", doc
->title
);
734 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
737 * Tell the document processor the copy and duplex options
738 * that are required...
741 doc_printf(doc
, "%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
742 doc
->collate
? " collate" : "",
743 Duplex
? " duplex" : "");
746 * Apple uses RBI comments for various non-PPD options...
749 doc_printf(doc
, "%%RBINumCopies: %d\n", doc
->copies
);
754 * Tell the document processor the duplex option that is required...
758 doc_puts(doc
, "%%Requirements: duplex\n");
761 * Apple uses RBI comments for various non-PPD options...
764 doc_puts(doc
, "%RBINumCopies: 1\n");
767 doc_puts(doc
, "%%Pages: (atend)\n");
768 doc_puts(doc
, "%%BoundingBox: (atend)\n");
769 doc_puts(doc
, "%%EndComments\n");
776 * 'copy_dsc()' - Copy a DSC-conforming document.
778 * This function expects "line" to be filled with the %!PS-Adobe comment line.
782 copy_dsc(cups_file_t
*fp
, /* I - File to read from */
783 pstops_doc_t
*doc
, /* I - Document info */
784 ppd_file_t
*ppd
, /* I - PPD file */
785 char *line
, /* I - Line buffer */
786 ssize_t linelen
, /* I - Length of initial line */
787 size_t linesize
) /* I - Size of line buffer */
789 int number
; /* Page number */
790 pstops_page_t
*pageinfo
; /* Page information */
794 * Make sure we use ESPshowpage for EPS files...
797 if (strstr(line
, "EPSF"))
799 doc
->use_ESPshowpage
= 1;
804 * Start sending the document with any commands needed...
807 fprintf(stderr
, "DEBUG: Before copy_comments - %s", line
);
808 linelen
= copy_comments(fp
, doc
, ppd
, line
, linelen
, linesize
);
811 * Now find the prolog section, if any...
814 fprintf(stderr
, "DEBUG: Before copy_prolog - %s", line
);
815 linelen
= copy_prolog(fp
, doc
, ppd
, line
, linelen
, linesize
);
818 * Then the document setup section...
821 fprintf(stderr
, "DEBUG: Before copy_setup - %s", line
);
822 linelen
= copy_setup(fp
, doc
, ppd
, line
, linelen
, linesize
);
825 * Copy until we see %%Page:...
828 while (strncmp(line
, "%%Page:", 7) && strncmp(line
, "%%Trailer", 9))
830 doc_write(doc
, line
, (size_t)linelen
);
832 if ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) == 0)
837 * Then process pages until we have no more...
842 fprintf(stderr
, "DEBUG: Before page loop - %s", line
);
843 while (!strncmp(line
, "%%Page:", 7))
850 if (check_range(doc
, (number
- 1) / doc
->number_up
+ 1))
852 fprintf(stderr
, "DEBUG: Copying page %d...\n", number
);
853 linelen
= copy_page(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
857 fprintf(stderr
, "DEBUG: Skipping page %d...\n", number
);
858 linelen
= skip_page(fp
, line
, linelen
, linesize
);
863 * Finish up the last page(s)...
866 if (number
&& is_not_last_page(number
) && cupsArrayLast(doc
->pages
) &&
867 check_range(doc
, (number
- 1) / doc
->number_up
+ 1))
869 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
871 start_nup(doc
, doc
->number_up
, 0, doc
->bounding_box
);
872 doc_puts(doc
, "showpage\n");
873 end_nup(doc
, doc
->number_up
);
875 pageinfo
->length
= (ssize_t
)(cupsFileTell(doc
->temp
) - pageinfo
->offset
);
878 if (doc
->slow_duplex
&& (doc
->page
& 1))
881 * Make sure we have an even number of pages...
884 pageinfo
= add_page(doc
, "(filler)");
886 if (!doc
->slow_order
)
888 if (!ppd
|| !ppd
->num_filters
)
889 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
890 doc
->slow_collate
? 1 : doc
->copies
);
892 printf("%%%%Page: (filler) %d\n", doc
->page
);
895 start_nup(doc
, doc
->number_up
, 0, doc
->bounding_box
);
896 doc_puts(doc
, "showpage\n");
897 end_nup(doc
, doc
->number_up
);
899 pageinfo
->length
= (ssize_t
)(cupsFileTell(doc
->temp
) - pageinfo
->offset
);
903 * Make additional copies as necessary...
906 number
= doc
->slow_order
? 0 : doc
->page
;
908 if (doc
->temp
&& !JobCanceled
&& cupsArrayCount(doc
->pages
) > 0)
910 int copy
; /* Current copy */
914 * Reopen the temporary file for reading...
917 cupsFileClose(doc
->temp
);
919 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
925 if (doc
->slow_collate
)
926 copy
= !doc
->slow_order
;
928 copy
= doc
->copies
- 1;
930 for (; copy
< doc
->copies
; copy
++)
936 * Send end-of-job stuff followed by any start-of-job stuff required
937 * for the JCL options...
940 if (number
&& doc
->emit_jcl
&& ppd
&& ppd
->jcl_end
)
943 * Send the trailer...
947 printf("%%%%Pages: %d\n", cupsArrayCount(doc
->pages
));
948 if (doc
->number_up
> 1 || doc
->fit_to_page
)
949 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
950 PageLeft
, PageBottom
, PageRight
, PageTop
);
952 printf("%%%%BoundingBox: %d %d %d %d\n",
953 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
954 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
958 * Start a new document...
961 ppdEmitJCLEnd(ppd
, stdout
);
962 ppdEmitJCL(ppd
, stdout
, doc
->job_id
, doc
->user
, doc
->title
);
964 puts("%!PS-Adobe-3.0");
970 * Copy the prolog as needed...
975 pageinfo
= (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
976 copy_bytes(doc
->temp
, 0, (size_t)pageinfo
->offset
);
980 * Then copy all of the pages...
983 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayLast(doc
->pages
) :
984 (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
993 if (!ppd
|| !ppd
->num_filters
)
994 fprintf(stderr
, "PAGE: %d %d\n", number
,
995 doc
->slow_collate
? 1 : doc
->copies
);
997 if (doc
->number_up
> 1)
999 printf("%%%%Page: (%d) %d\n", number
, number
);
1000 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1001 PageLeft
, PageBottom
, PageRight
, PageTop
);
1005 printf("%%%%Page: %s %d\n", pageinfo
->label
, number
);
1006 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1007 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
1008 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
1011 copy_bytes(doc
->temp
, pageinfo
->offset
, (size_t)pageinfo
->length
);
1013 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayPrev(doc
->pages
) :
1014 (pstops_page_t
*)cupsArrayNext(doc
->pages
);
1020 * Restore the old showpage operator as needed...
1023 if (doc
->use_ESPshowpage
)
1024 puts("userdict/showpage/ESPshowpage load put\n");
1027 * Write/copy the trailer...
1031 copy_trailer(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
1036 * 'copy_non_dsc()' - Copy a document that does not conform to the DSC.
1038 * This function expects "line" to be filled with the %! comment line.
1042 copy_non_dsc(cups_file_t
*fp
, /* I - File to read from */
1043 pstops_doc_t
*doc
, /* I - Document info */
1044 ppd_file_t
*ppd
, /* I - PPD file */
1045 char *line
, /* I - Line buffer */
1046 ssize_t linelen
, /* I - Length of initial line */
1047 size_t linesize
) /* I - Size of line buffer */
1049 int copy
; /* Current copy */
1050 char buffer
[8192]; /* Copy buffer */
1051 ssize_t bytes
; /* Number of bytes copied */
1057 * First let the user know that they are attempting to print a file
1058 * that may not print correctly...
1061 fputs("DEBUG: This document does not conform to the Adobe Document "
1062 "Structuring Conventions and may not print correctly.\n", stderr
);
1065 * Then write a standard DSC comment section...
1068 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", PageLeft
, PageBottom
,
1069 PageRight
, PageTop
);
1071 if (doc
->slow_collate
&& doc
->copies
> 1)
1072 printf("%%%%Pages: %d\n", doc
->copies
);
1076 WriteTextComment("For", doc
->user
);
1077 WriteTextComment("Title", doc
->title
);
1079 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1082 * Tell the document processor the copy and duplex options
1083 * that are required...
1086 printf("%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
1087 doc
->collate
? " collate" : "",
1088 Duplex
? " duplex" : "");
1091 * Apple uses RBI comments for various non-PPD options...
1094 printf("%%RBINumCopies: %d\n", doc
->copies
);
1099 * Tell the document processor the duplex option that is required...
1103 puts("%%Requirements: duplex");
1106 * Apple uses RBI comments for various non-PPD options...
1109 puts("%RBINumCopies: 1");
1112 puts("%%EndComments");
1115 * Then the prolog...
1118 puts("%%BeginProlog");
1120 do_prolog(doc
, ppd
);
1122 puts("%%EndProlog");
1125 * Then the setup section...
1128 puts("%%BeginSetup");
1135 * Finally, embed a copy of the file inside a %%Page...
1138 if (!ppd
|| !ppd
->num_filters
)
1139 fprintf(stderr
, "PAGE: 1 %d\n", doc
->temp
? 1 : doc
->copies
);
1141 puts("%%Page: 1 1");
1142 puts("%%BeginPageSetup");
1143 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1144 puts("%%EndPageSetup");
1145 puts("%%BeginDocument: nondsc");
1147 fwrite(line
, (size_t)linelen
, 1, stdout
);
1150 cupsFileWrite(doc
->temp
, line
, (size_t)linelen
);
1152 while ((bytes
= cupsFileRead(fp
, buffer
, sizeof(buffer
))) > 0)
1154 fwrite(buffer
, 1, (size_t)bytes
, stdout
);
1157 cupsFileWrite(doc
->temp
, buffer
, (size_t)bytes
);
1160 puts("%%EndDocument");
1162 if (doc
->use_ESPshowpage
)
1164 WriteLabels(Orientation
);
1165 puts("ESPshowpage");
1168 if (doc
->temp
&& !JobCanceled
)
1171 * Reopen the temporary file for reading...
1174 cupsFileClose(doc
->temp
);
1176 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
1179 * Make the additional copies as needed...
1182 for (copy
= 1; copy
< doc
->copies
; copy
++)
1187 if (!ppd
|| !ppd
->num_filters
)
1188 fputs("PAGE: 1 1\n", stderr
);
1190 printf("%%%%Page: %d %d\n", copy
+ 1, copy
+ 1);
1191 puts("%%BeginPageSetup");
1192 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1193 puts("%%EndPageSetup");
1194 puts("%%BeginDocument: nondsc");
1196 copy_bytes(doc
->temp
, 0, 0);
1198 puts("%%EndDocument");
1200 if (doc
->use_ESPshowpage
)
1202 WriteLabels(Orientation
);
1203 puts("ESPshowpage");
1209 * Restore the old showpage operator as needed...
1212 if (doc
->use_ESPshowpage
)
1213 puts("userdict/showpage/ESPshowpage load put\n");
1218 * 'copy_page()' - Copy a page description.
1220 * This function expects "line" to be filled with a %%Page comment line.
1221 * On return, "line" will contain the next line in the file, if any.
1224 static ssize_t
/* O - Length of next line */
1225 copy_page(cups_file_t
*fp
, /* I - File to read from */
1226 pstops_doc_t
*doc
, /* I - Document info */
1227 ppd_file_t
*ppd
, /* I - PPD file */
1228 int number
, /* I - Current page number */
1229 char *line
, /* I - Line buffer */
1230 ssize_t linelen
, /* I - Length of initial line */
1231 size_t linesize
) /* I - Size of line buffer */
1233 char label
[256], /* Page label string */
1234 *ptr
; /* Pointer into line */
1235 int level
; /* Embedded document level */
1236 pstops_page_t
*pageinfo
; /* Page information */
1237 int first_page
; /* First page on N-up output? */
1238 int has_page_setup
= 0; /* Does the page have %%Begin/EndPageSetup? */
1239 int bounding_box
[4]; /* PageBoundingBox */
1243 * Get the page label for this page...
1246 first_page
= is_first_page(number
);
1248 if (!parse_text(line
+ 7, &ptr
, label
, sizeof(label
)))
1250 fputs("DEBUG: There was a bad %%Page: comment in the file.\n", stderr
);
1254 else if (strtol(ptr
, &ptr
, 10) == LONG_MAX
|| !isspace(*ptr
& 255))
1256 fputs("DEBUG: There was a bad %%Page: comment in the file.\n", stderr
);
1261 * Create or update the current output page...
1265 pageinfo
= add_page(doc
, label
);
1267 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
1270 * Handle first page override...
1273 if (doc
->ap_input_slot
|| doc
->ap_manual_feed
)
1275 if ((doc
->page
== 1 && (!doc
->slow_order
|| !Duplex
)) ||
1276 (doc
->page
== 2 && doc
->slow_order
&& Duplex
))
1279 * First page/sheet gets AP_FIRSTPAGE_* options...
1282 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->ap_input_slot
,
1283 pageinfo
->num_options
,
1284 &(pageinfo
->options
));
1285 pageinfo
->num_options
= cupsAddOption("ManualFeed",
1286 doc
->ap_input_slot
? "False" :
1287 doc
->ap_manual_feed
,
1288 pageinfo
->num_options
,
1289 &(pageinfo
->options
));
1290 pageinfo
->num_options
= cupsAddOption("MediaColor", doc
->ap_media_color
,
1291 pageinfo
->num_options
,
1292 &(pageinfo
->options
));
1293 pageinfo
->num_options
= cupsAddOption("MediaType", doc
->ap_media_type
,
1294 pageinfo
->num_options
,
1295 &(pageinfo
->options
));
1296 pageinfo
->num_options
= cupsAddOption("PageRegion", doc
->ap_page_region
,
1297 pageinfo
->num_options
,
1298 &(pageinfo
->options
));
1299 pageinfo
->num_options
= cupsAddOption("PageSize", doc
->ap_page_size
,
1300 pageinfo
->num_options
,
1301 &(pageinfo
->options
));
1303 else if (doc
->page
== (Duplex
+ 2))
1306 * Second page/sheet gets default options...
1309 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->input_slot
,
1310 pageinfo
->num_options
,
1311 &(pageinfo
->options
));
1312 pageinfo
->num_options
= cupsAddOption("ManualFeed",
1313 doc
->input_slot
? "False" :
1315 pageinfo
->num_options
,
1316 &(pageinfo
->options
));
1317 pageinfo
->num_options
= cupsAddOption("MediaColor", doc
->media_color
,
1318 pageinfo
->num_options
,
1319 &(pageinfo
->options
));
1320 pageinfo
->num_options
= cupsAddOption("MediaType", doc
->media_type
,
1321 pageinfo
->num_options
,
1322 &(pageinfo
->options
));
1323 pageinfo
->num_options
= cupsAddOption("PageRegion", doc
->page_region
,
1324 pageinfo
->num_options
,
1325 &(pageinfo
->options
));
1326 pageinfo
->num_options
= cupsAddOption("PageSize", doc
->page_size
,
1327 pageinfo
->num_options
,
1328 &(pageinfo
->options
));
1333 * Scan comments until we see something other than %%Page*: or
1337 memcpy(bounding_box
, doc
->bounding_box
, sizeof(bounding_box
));
1339 while ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) > 0)
1341 if (!strncmp(line
, "%%PageBoundingBox:", 18))
1344 * %%PageBoundingBox: llx lly urx ury
1347 if (sscanf(line
+ 18, "%d%d%d%d", bounding_box
+ 0,
1348 bounding_box
+ 1, bounding_box
+ 2,
1349 bounding_box
+ 3) != 4)
1351 fputs("DEBUG: There was a bad %%PageBoundingBox: comment in the file.\n", stderr
);
1352 memcpy(bounding_box
, doc
->bounding_box
,
1353 sizeof(bounding_box
));
1355 else if (doc
->number_up
== 1 && !doc
->fit_to_page
&& Orientation
)
1357 int temp_bbox
[4]; /* Temporary bounding box */
1360 memcpy(temp_bbox
, bounding_box
, sizeof(temp_bbox
));
1362 fprintf(stderr
, "DEBUG: Orientation = %d\n", Orientation
);
1363 fprintf(stderr
, "DEBUG: original bounding_box = [ %d %d %d %d ]\n",
1364 bounding_box
[0], bounding_box
[1],
1365 bounding_box
[2], bounding_box
[3]);
1366 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
1367 PageWidth
, PageLength
);
1369 switch (Orientation
)
1371 case 1 : /* Landscape */
1372 bounding_box
[0] = (int)(PageLength
- temp_bbox
[3]);
1373 bounding_box
[1] = temp_bbox
[0];
1374 bounding_box
[2] = (int)(PageLength
- temp_bbox
[1]);
1375 bounding_box
[3] = temp_bbox
[2];
1378 case 2 : /* Reverse Portrait */
1379 bounding_box
[0] = (int)(PageWidth
- temp_bbox
[2]);
1380 bounding_box
[1] = (int)(PageLength
- temp_bbox
[3]);
1381 bounding_box
[2] = (int)(PageWidth
- temp_bbox
[0]);
1382 bounding_box
[3] = (int)(PageLength
- temp_bbox
[1]);
1385 case 3 : /* Reverse Landscape */
1386 bounding_box
[0] = temp_bbox
[1];
1387 bounding_box
[1] = (int)(PageWidth
- temp_bbox
[2]);
1388 bounding_box
[2] = temp_bbox
[3];
1389 bounding_box
[3] = (int)(PageWidth
- temp_bbox
[0]);
1393 fprintf(stderr
, "DEBUG: updated bounding_box = [ %d %d %d %d ]\n",
1394 bounding_box
[0], bounding_box
[1],
1395 bounding_box
[2], bounding_box
[3]);
1399 else if (!strncmp(line
, "%%PageCustomColors:", 19) ||
1400 !strncmp(line
, "%%PageMedia:", 12) ||
1401 !strncmp(line
, "%%PageOrientation:", 18) ||
1402 !strncmp(line
, "%%PageProcessColors:", 20) ||
1403 !strncmp(line
, "%%PageRequirements:", 18) ||
1404 !strncmp(line
, "%%PageResources:", 16))
1411 else if (!strncmp(line
, "%%PageCustomColors:", 19))
1414 * %%PageCustomColors: ...
1417 else if (!strncmp(line
, "%%PageMedia:", 12))
1423 else if (!strncmp(line
, "%%PageOrientation:", 18))
1426 * %%PageOrientation: ...
1429 else if (!strncmp(line
, "%%PageProcessColors:", 20))
1432 * %%PageProcessColors: ...
1435 else if (!strncmp(line
, "%%PageRequirements:", 18))
1438 * %%PageRequirements: ...
1441 else if (!strncmp(line
, "%%PageResources:", 16))
1444 * %%PageResources: ...
1447 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1450 * %%IncludeFeature: *MainKeyword OptionKeyword
1453 if (doc
->number_up
== 1 &&!doc
->fit_to_page
)
1454 pageinfo
->num_options
= include_feature(ppd
, line
,
1455 pageinfo
->num_options
,
1456 &(pageinfo
->options
));
1458 else if (!strncmp(line
, "%%BeginPageSetup", 16))
1467 if (doc
->number_up
== 1)
1470 * Update the document's composite and page bounding box...
1473 memcpy(pageinfo
->bounding_box
, bounding_box
,
1474 sizeof(pageinfo
->bounding_box
));
1476 if (bounding_box
[0] < doc
->new_bounding_box
[0])
1477 doc
->new_bounding_box
[0] = bounding_box
[0];
1478 if (bounding_box
[1] < doc
->new_bounding_box
[1])
1479 doc
->new_bounding_box
[1] = bounding_box
[1];
1480 if (bounding_box
[2] > doc
->new_bounding_box
[2])
1481 doc
->new_bounding_box
[2] = bounding_box
[2];
1482 if (bounding_box
[3] > doc
->new_bounding_box
[3])
1483 doc
->new_bounding_box
[3] = bounding_box
[3];
1487 * Output the page header as needed...
1490 if (!doc
->slow_order
&& first_page
)
1492 if (!ppd
|| !ppd
->num_filters
)
1493 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
1494 doc
->slow_collate
? 1 : doc
->copies
);
1496 if (doc
->number_up
> 1)
1498 printf("%%%%Page: (%d) %d\n", doc
->page
, doc
->page
);
1499 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1500 PageLeft
, PageBottom
, PageRight
, PageTop
);
1504 printf("%%%%Page: %s %d\n", pageinfo
->label
, doc
->page
);
1505 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1506 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
1507 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
1512 * Copy any page setup commands...
1516 doc_puts(doc
, "%%BeginPageSetup\n");
1520 int feature
= 0; /* In a Begin/EndFeature block? */
1522 while ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) > 0)
1524 if (!strncmp(line
, "%%EndPageSetup", 14))
1526 else if (!strncmp(line
, "%%BeginFeature:", 15))
1530 if (doc
->number_up
> 1 || doc
->fit_to_page
)
1533 else if (!strncmp(line
, "%%EndFeature", 12))
1537 if (doc
->number_up
> 1 || doc
->fit_to_page
)
1540 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1542 pageinfo
->num_options
= include_feature(ppd
, line
,
1543 pageinfo
->num_options
,
1544 &(pageinfo
->options
));
1547 else if (!strncmp(line
, "%%Include", 9))
1550 if (line
[0] != '%' && !feature
)
1553 if (!feature
|| (doc
->number_up
== 1 && !doc
->fit_to_page
))
1554 doc_write(doc
, line
, (size_t)linelen
);
1558 * Skip %%EndPageSetup...
1561 if (linelen
> 0 && !strncmp(line
, "%%EndPageSetup", 14))
1562 linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
);
1567 char *page_setup
; /* PageSetup commands to send */
1570 if (pageinfo
->num_options
> 0)
1571 write_options(doc
, ppd
, pageinfo
->num_options
, pageinfo
->options
);
1574 * Output commands for the current page...
1577 page_setup
= ppdEmitString(ppd
, PPD_ORDER_PAGE
, 0);
1581 doc_puts(doc
, page_setup
);
1587 * Prep for the start of the page description...
1590 start_nup(doc
, number
, 1, bounding_box
);
1593 doc_puts(doc
, "%%EndPageSetup\n");
1596 * Read the rest of the page description...
1604 (!strncmp(line
, "%%Page:", 7) ||
1605 !strncmp(line
, "%%Trailer", 9) ||
1606 !strncmp(line
, "%%EOF", 5)))
1608 else if (!strncmp(line
, "%%BeginDocument", 15) ||
1609 !strncmp(line
, "%ADO_BeginApplication", 21))
1611 doc_write(doc
, line
, (size_t)linelen
);
1615 else if ((!strncmp(line
, "%%EndDocument", 13) ||
1616 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
1618 doc_write(doc
, line
, (size_t)linelen
);
1622 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
1623 (!strncmp(line
, "%%BeginData:", 12) &&
1624 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
1627 * Copy binary data...
1630 int bytes
; /* Bytes of data */
1633 doc_write(doc
, line
, (size_t)linelen
);
1635 bytes
= atoi(strchr(line
, ':') + 1);
1639 if ((size_t)bytes
> linesize
)
1640 linelen
= cupsFileRead(fp
, line
, linesize
);
1642 linelen
= cupsFileRead(fp
, line
, (size_t)bytes
);
1647 perror("ERROR: Early end-of-file while reading binary data");
1651 doc_write(doc
, line
, (size_t)linelen
);
1657 doc_write(doc
, line
, (size_t)linelen
);
1659 while ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) > 0);
1662 * Finish up this page and return...
1665 end_nup(doc
, number
);
1667 pageinfo
->length
= (ssize_t
)(cupsFileTell(doc
->temp
) - pageinfo
->offset
);
1674 * 'copy_prolog()' - Copy the document prolog section.
1676 * This function expects "line" to be filled with a %%BeginProlog comment line.
1677 * On return, "line" will contain the next line in the file, if any.
1680 static ssize_t
/* O - Length of next line */
1681 copy_prolog(cups_file_t
*fp
, /* I - File to read from */
1682 pstops_doc_t
*doc
, /* I - Document info */
1683 ppd_file_t
*ppd
, /* I - PPD file */
1684 char *line
, /* I - Line buffer */
1685 ssize_t linelen
, /* I - Length of initial line */
1686 size_t linesize
) /* I - Size of line buffer */
1688 while (strncmp(line
, "%%BeginProlog", 13))
1690 if (!strncmp(line
, "%%BeginSetup", 12) || !strncmp(line
, "%%Page:", 7))
1693 doc_write(doc
, line
, (size_t)linelen
);
1695 if ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) == 0)
1699 doc_puts(doc
, "%%BeginProlog\n");
1701 do_prolog(doc
, ppd
);
1703 if (!strncmp(line
, "%%BeginProlog", 13))
1705 while ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) > 0)
1707 if (!strncmp(line
, "%%EndProlog", 11) ||
1708 !strncmp(line
, "%%BeginSetup", 12) ||
1709 !strncmp(line
, "%%Page:", 7))
1712 doc_write(doc
, line
, (size_t)linelen
);
1715 if (!strncmp(line
, "%%EndProlog", 11))
1716 linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
);
1718 fputs("DEBUG: The %%EndProlog comment is missing.\n", stderr
);
1721 doc_puts(doc
, "%%EndProlog\n");
1728 * 'copy_setup()' - Copy the document setup section.
1730 * This function expects "line" to be filled with a %%BeginSetup comment line.
1731 * On return, "line" will contain the next line in the file, if any.
1734 static ssize_t
/* O - Length of next line */
1735 copy_setup(cups_file_t
*fp
, /* I - File to read from */
1736 pstops_doc_t
*doc
, /* I - Document info */
1737 ppd_file_t
*ppd
, /* I - PPD file */
1738 char *line
, /* I - Line buffer */
1739 ssize_t linelen
, /* I - Length of initial line */
1740 size_t linesize
) /* I - Size of line buffer */
1742 int num_options
; /* Number of options */
1743 cups_option_t
*options
; /* Options */
1746 while (strncmp(line
, "%%BeginSetup", 12))
1748 if (!strncmp(line
, "%%Page:", 7))
1751 doc_write(doc
, line
, (size_t)linelen
);
1753 if ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) == 0)
1757 doc_puts(doc
, "%%BeginSetup\n");
1764 if (!strncmp(line
, "%%BeginSetup", 12))
1766 while (strncmp(line
, "%%EndSetup", 10))
1768 if (!strncmp(line
, "%%Page:", 7))
1770 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1773 * %%IncludeFeature: *MainKeyword OptionKeyword
1776 if (doc
->number_up
== 1 && !doc
->fit_to_page
)
1777 num_options
= include_feature(ppd
, line
, num_options
, &options
);
1779 else if (strncmp(line
, "%%BeginSetup", 12))
1780 doc_write(doc
, line
, (size_t)linelen
);
1782 if ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) == 0)
1786 if (!strncmp(line
, "%%EndSetup", 10))
1787 linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
);
1789 fputs("DEBUG: The %%EndSetup comment is missing.\n", stderr
);
1792 if (num_options
> 0)
1794 write_options(doc
, ppd
, num_options
, options
);
1795 cupsFreeOptions(num_options
, options
);
1798 doc_puts(doc
, "%%EndSetup\n");
1805 * 'copy_trailer()' - Copy the document trailer.
1807 * This function expects "line" to be filled with a %%Trailer comment line.
1808 * On return, "line" will contain the next line in the file, if any.
1811 static ssize_t
/* O - Length of next line */
1812 copy_trailer(cups_file_t
*fp
, /* I - File to read from */
1813 pstops_doc_t
*doc
, /* I - Document info */
1814 ppd_file_t
*ppd
, /* I - PPD file */
1815 int number
, /* I - Number of pages */
1816 char *line
, /* I - Line buffer */
1817 ssize_t linelen
, /* I - Length of initial line */
1818 size_t linesize
) /* I - Size of line buffer */
1821 * Write the trailer comments...
1830 if (!strncmp(line
, "%%EOF", 5))
1832 else if (strncmp(line
, "%%Trailer", 9) &&
1833 strncmp(line
, "%%Pages:", 8) &&
1834 strncmp(line
, "%%BoundingBox:", 14))
1835 fwrite(line
, 1, (size_t)linelen
, stdout
);
1837 linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
);
1840 fprintf(stderr
, "DEBUG: Wrote %d pages...\n", number
);
1842 printf("%%%%Pages: %d\n", number
);
1843 if (doc
->number_up
> 1 || doc
->fit_to_page
)
1844 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
1845 PageLeft
, PageBottom
, PageRight
, PageTop
);
1847 printf("%%%%BoundingBox: %d %d %d %d\n",
1848 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
1849 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
1856 * 'do_prolog()' - Send the necessary document prolog commands.
1860 do_prolog(pstops_doc_t
*doc
, /* I - Document information */
1861 ppd_file_t
*ppd
) /* I - PPD file */
1863 char *ps
; /* PS commands */
1867 * Send the document prolog commands...
1870 if (ppd
&& ppd
->patches
)
1872 doc_puts(doc
, "%%BeginFeature: *JobPatchFile 1\n");
1873 doc_puts(doc
, ppd
->patches
);
1874 doc_puts(doc
, "\n%%EndFeature\n");
1877 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_PROLOG
, 0.0)) != NULL
)
1884 * Define ESPshowpage here so that applications that define their
1885 * own procedure to do a showpage pick it up...
1888 if (doc
->use_ESPshowpage
)
1889 doc_puts(doc
, "userdict/ESPshowpage/showpage load put\n"
1890 "userdict/showpage{}put\n");
1895 * 'do_setup()' - Send the necessary document setup commands.
1899 do_setup(pstops_doc_t
*doc
, /* I - Document information */
1900 ppd_file_t
*ppd
) /* I - PPD file */
1902 char *ps
; /* PS commands */
1906 * Disable CTRL-D so that embedded files don't cause printing
1910 doc_puts(doc
, "% Disable CTRL-D as an end-of-file marker...\n");
1911 doc_puts(doc
, "userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put\n");
1914 * Mark job options...
1917 cupsMarkOptions(ppd
, doc
->num_options
, doc
->options
);
1920 * Send all the printer-specific setup commands...
1923 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, 0.0)) != NULL
)
1929 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_ANY
, 0.0)) != NULL
)
1936 * Set the number of copies for the job...
1939 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1941 doc_printf(doc
, "%%RBIBeginNonPPDFeature: *NumCopies %d\n", doc
->copies
);
1943 "%d/languagelevel where{pop languagelevel 2 ge}{false}ifelse\n"
1944 "{1 dict begin/NumCopies exch def currentdict end "
1946 "{userdict/#copies 3 -1 roll put}ifelse\n", doc
->copies
);
1947 doc_puts(doc
, "%RBIEndNonPPDFeature\n");
1951 * If we are doing N-up printing, disable setpagedevice...
1954 if (doc
->number_up
> 1)
1956 doc_puts(doc
, "userdict/CUPSsetpagedevice/setpagedevice load put\n");
1957 doc_puts(doc
, "userdict/setpagedevice{pop}bind put\n");
1961 * Make sure we have rectclip and rectstroke procedures of some sort...
1965 "% x y w h ESPrc - Clip to a rectangle.\n"
1966 "userdict/ESPrc/rectclip where{pop/rectclip load}\n"
1967 "{{newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
1968 "neg 0 rlineto closepath clip newpath}bind}ifelse put\n");
1971 "% x y w h ESPrf - Fill a rectangle.\n"
1972 "userdict/ESPrf/rectfill where{pop/rectfill load}\n"
1973 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
1974 "neg 0 rlineto closepath fill grestore}bind}ifelse put\n");
1977 "% x y w h ESPrs - Stroke a rectangle.\n"
1978 "userdict/ESPrs/rectstroke where{pop/rectstroke load}\n"
1979 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
1980 "neg 0 rlineto closepath stroke grestore}bind}ifelse put\n");
1983 * Write the page and label prologs...
1986 if (doc
->number_up
== 2 || doc
->number_up
== 6)
1989 * For 2- and 6-up output, rotate the labels to match the orientation
1993 if (Orientation
& 1)
1994 write_label_prolog(doc
, doc
->page_label
, PageBottom
,
1995 PageWidth
- PageLength
+ PageTop
, PageLength
);
1997 write_label_prolog(doc
, doc
->page_label
, PageLeft
, PageRight
,
2001 write_label_prolog(doc
, doc
->page_label
, PageBottom
, PageTop
, PageWidth
);
2006 * 'doc_printf()' - Send a formatted string to stdout and/or the temp file.
2008 * This function should be used for all page-level output that is affected
2009 * by ordering, collation, etc.
2013 doc_printf(pstops_doc_t
*doc
, /* I - Document information */
2014 const char *format
, /* I - Printf-style format string */
2015 ...) /* I - Additional arguments as needed */
2017 va_list ap
; /* Pointer to arguments */
2018 char buffer
[1024]; /* Output buffer */
2019 ssize_t bytes
; /* Number of bytes to write */
2022 va_start(ap
, format
);
2023 bytes
= vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
2026 if ((size_t)bytes
> sizeof(buffer
))
2028 _cupsLangPrintFilter(stderr
, "ERROR",
2029 _("Buffer overflow detected, aborting."));
2033 doc_write(doc
, buffer
, (size_t)bytes
);
2038 * 'doc_puts()' - Send a nul-terminated string to stdout and/or the temp file.
2040 * This function should be used for all page-level output that is affected
2041 * by ordering, collation, etc.
2045 doc_puts(pstops_doc_t
*doc
, /* I - Document information */
2046 const char *s
) /* I - String to send */
2048 doc_write(doc
, s
, strlen(s
));
2053 * 'doc_write()' - Send data to stdout and/or the temp file.
2057 doc_write(pstops_doc_t
*doc
, /* I - Document information */
2058 const char *s
, /* I - Data to send */
2059 size_t len
) /* I - Number of bytes to send */
2061 if (!doc
->slow_order
)
2062 fwrite(s
, 1, len
, stdout
);
2065 cupsFileWrite(doc
->temp
, s
, len
);
2070 * 'end_nup()' - End processing for N-up printing.
2074 end_nup(pstops_doc_t
*doc
, /* I - Document information */
2075 int number
) /* I - Page number */
2077 if (doc
->number_up
> 1)
2078 doc_puts(doc
, "userdict/ESPsave get restore\n");
2080 switch (doc
->number_up
)
2083 if (doc
->use_ESPshowpage
)
2085 write_labels(doc
, Orientation
);
2086 doc_puts(doc
, "ESPshowpage\n");
2092 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2094 if (Orientation
& 1)
2097 * Rotate the labels back to portrait...
2100 write_labels(doc
, Orientation
- 1);
2102 else if (Orientation
== 0)
2105 * Rotate the labels to landscape...
2108 write_labels(doc
, doc
->normal_landscape
? 1 : 3);
2113 * Rotate the labels to landscape...
2116 write_labels(doc
, doc
->normal_landscape
? 3 : 1);
2119 doc_puts(doc
, "ESPshowpage\n");
2124 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2126 write_labels(doc
, Orientation
);
2127 doc_puts(doc
, "ESPshowpage\n");
2137 * 'include_feature()' - Include a printer option/feature command.
2140 static int /* O - New number of options */
2142 ppd_file_t
*ppd
, /* I - PPD file */
2143 const char *line
, /* I - DSC line */
2144 int num_options
, /* I - Number of options */
2145 cups_option_t
**options
) /* IO - Options */
2147 char name
[255], /* Option name */
2148 value
[255]; /* Option value */
2149 ppd_option_t
*option
; /* Option in file */
2153 * Get the "%%IncludeFeature: *Keyword OptionKeyword" values...
2156 if (sscanf(line
+ 17, "%254s%254s", name
, value
) != 2)
2158 fputs("DEBUG: The %%IncludeFeature: comment is not valid.\n", stderr
);
2159 return (num_options
);
2163 * Find the option and choice...
2166 if ((option
= ppdFindOption(ppd
, name
+ 1)) == NULL
)
2168 _cupsLangPrintFilter(stderr
, "WARNING", _("Unknown option \"%s\"."),
2170 return (num_options
);
2173 if (option
->section
== PPD_ORDER_EXIT
||
2174 option
->section
== PPD_ORDER_JCL
)
2176 _cupsLangPrintFilter(stderr
, "WARNING",
2177 _("Option \"%s\" cannot be included via "
2178 "%%%%IncludeFeature."), name
+ 1);
2179 return (num_options
);
2182 if (!ppdFindChoice(option
, value
))
2184 _cupsLangPrintFilter(stderr
, "WARNING",
2185 _("Unknown choice \"%s\" for option \"%s\"."),
2187 return (num_options
);
2191 * Add the option to the option array and return...
2194 return (cupsAddOption(name
+ 1, value
, num_options
, options
));
2199 * 'parse_text()' - Parse a text value in a comment.
2201 * This function parses a DSC text value as defined on page 36 of the
2202 * DSC specification. Text values are either surrounded by parenthesis
2203 * or whitespace-delimited.
2205 * The value returned is the literal characters for the entire text
2206 * string, including any parenthesis and escape characters.
2209 static char * /* O - Value or NULL on error */
2210 parse_text(const char *start
, /* I - Start of text value */
2211 char **end
, /* O - End of text value */
2212 char *buffer
, /* I - Buffer */
2213 size_t bufsize
) /* I - Size of buffer */
2215 char *bufptr
, /* Pointer in buffer */
2216 *bufend
; /* End of buffer */
2217 int level
; /* Parenthesis level */
2221 * Skip leading whitespace...
2224 while (isspace(*start
& 255))
2228 * Then copy the value...
2233 bufend
= buffer
+ bufsize
- 1;
2235 while (bufptr
< bufend
)
2237 if (isspace(*start
& 255) && !level
)
2244 else if (*start
== ')')
2254 else if (*start
== '\\')
2257 * Copy escaped character...
2260 int i
; /* Looping var */
2264 i
<= 3 && isdigit(start
[i
] & 255) && bufptr
< bufend
;
2265 *bufptr
++ = start
[i
], i
++);
2274 * Return the value and new pointer into the line...
2278 *end
= (char *)start
;
2280 if (bufptr
== bufend
)
2288 * 'set_pstops_options()' - Set pstops options.
2293 pstops_doc_t
*doc
, /* I - Document information */
2294 ppd_file_t
*ppd
, /* I - PPD file */
2295 char *argv
[], /* I - Command-line arguments */
2296 int num_options
, /* I - Number of options */
2297 cups_option_t
*options
) /* I - Options */
2299 const char *val
; /* Option value */
2300 int intval
; /* Integer option value */
2301 ppd_attr_t
*attr
; /* PPD attribute */
2302 ppd_option_t
*option
; /* PPD option */
2303 ppd_choice_t
*choice
; /* PPD choice */
2304 const char *content_type
; /* Original content type */
2305 int max_copies
; /* Maximum number of copies supported */
2309 * Initialize document information structure...
2312 memset(doc
, 0, sizeof(pstops_doc_t
));
2314 doc
->job_id
= atoi(argv
[1]);
2315 doc
->user
= argv
[2];
2316 doc
->title
= argv
[3];
2317 doc
->copies
= atoi(argv
[4]);
2319 if (ppd
&& ppd
->landscape
> 0)
2320 doc
->normal_landscape
= 1;
2322 doc
->bounding_box
[0] = (int)PageLeft
;
2323 doc
->bounding_box
[1] = (int)PageBottom
;
2324 doc
->bounding_box
[2] = (int)PageRight
;
2325 doc
->bounding_box
[3] = (int)PageTop
;
2327 doc
->new_bounding_box
[0] = INT_MAX
;
2328 doc
->new_bounding_box
[1] = INT_MAX
;
2329 doc
->new_bounding_box
[2] = INT_MIN
;
2330 doc
->new_bounding_box
[3] = INT_MIN
;
2333 * AP_FIRSTPAGE_* and the corresponding non-first-page options.
2336 doc
->ap_input_slot
= cupsGetOption("AP_FIRSTPAGE_InputSlot", num_options
,
2338 doc
->ap_manual_feed
= cupsGetOption("AP_FIRSTPAGE_ManualFeed", num_options
,
2340 doc
->ap_media_color
= cupsGetOption("AP_FIRSTPAGE_MediaColor", num_options
,
2342 doc
->ap_media_type
= cupsGetOption("AP_FIRSTPAGE_MediaType", num_options
,
2344 doc
->ap_page_region
= cupsGetOption("AP_FIRSTPAGE_PageRegion", num_options
,
2346 doc
->ap_page_size
= cupsGetOption("AP_FIRSTPAGE_PageSize", num_options
,
2349 if ((choice
= ppdFindMarkedChoice(ppd
, "InputSlot")) != NULL
)
2350 doc
->input_slot
= choice
->choice
;
2351 if ((choice
= ppdFindMarkedChoice(ppd
, "ManualFeed")) != NULL
)
2352 doc
->manual_feed
= choice
->choice
;
2353 if ((choice
= ppdFindMarkedChoice(ppd
, "MediaColor")) != NULL
)
2354 doc
->media_color
= choice
->choice
;
2355 if ((choice
= ppdFindMarkedChoice(ppd
, "MediaType")) != NULL
)
2356 doc
->media_type
= choice
->choice
;
2357 if ((choice
= ppdFindMarkedChoice(ppd
, "PageRegion")) != NULL
)
2358 doc
->page_region
= choice
->choice
;
2359 if ((choice
= ppdFindMarkedChoice(ppd
, "PageSize")) != NULL
)
2360 doc
->page_size
= choice
->choice
;
2363 * collate, multiple-document-handling
2366 if ((val
= cupsGetOption("multiple-document-handling", num_options
, options
)) != NULL
)
2369 * This IPP attribute is unnecessarily complicated...
2371 * single-document, separate-documents-collated-copies, and
2372 * single-document-new-sheet all require collated copies.
2374 * separate-documents-uncollated-copies allows for uncollated copies.
2377 doc
->collate
= _cups_strcasecmp(val
, "separate-documents-uncollated-copies") != 0;
2380 if ((val
= cupsGetOption("Collate", num_options
, options
)) != NULL
&&
2381 (!_cups_strcasecmp(val
, "true") ||!_cups_strcasecmp(val
, "on") ||
2382 !_cups_strcasecmp(val
, "yes")))
2389 if ((val
= cupsGetOption("emit-jcl", num_options
, options
)) != NULL
&&
2390 (!_cups_strcasecmp(val
, "false") || !_cups_strcasecmp(val
, "off") ||
2391 !_cups_strcasecmp(val
, "no") || !strcmp(val
, "0")))
2397 * fit-to-page/ipp-attribute-fidelity
2399 * (Only for original PostScript content)
2402 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
2403 content_type
= "application/postscript";
2405 if (!_cups_strcasecmp(content_type
, "application/postscript"))
2407 if ((val
= cupsGetOption("fit-to-page", num_options
, options
)) != NULL
&&
2408 !_cups_strcasecmp(val
, "true"))
2409 doc
->fit_to_page
= 1;
2410 else if ((val
= cupsGetOption("ipp-attribute-fidelity", num_options
,
2411 options
)) != NULL
&&
2412 !_cups_strcasecmp(val
, "true"))
2413 doc
->fit_to_page
= 1;
2417 * mirror/MirrorPrint
2420 if ((choice
= ppdFindMarkedChoice(ppd
, "MirrorPrint")) != NULL
)
2422 val
= choice
->choice
;
2426 val
= cupsGetOption("mirror", num_options
, options
);
2428 if (val
&& (!_cups_strcasecmp(val
, "true") || !_cups_strcasecmp(val
, "on") ||
2429 !_cups_strcasecmp(val
, "yes")))
2436 if ((val
= cupsGetOption("number-up", num_options
, options
)) != NULL
)
2438 switch (intval
= atoi(val
))
2446 doc
->number_up
= intval
;
2449 _cupsLangPrintFilter(stderr
, "ERROR",
2450 _("Unsupported number-up value %d, using "
2451 "number-up=1."), intval
);
2463 if ((val
= cupsGetOption("number-up-layout", num_options
, options
)) != NULL
)
2465 if (!_cups_strcasecmp(val
, "lrtb"))
2466 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2467 else if (!_cups_strcasecmp(val
, "lrbt"))
2468 doc
->number_up_layout
= PSTOPS_LAYOUT_LRBT
;
2469 else if (!_cups_strcasecmp(val
, "rltb"))
2470 doc
->number_up_layout
= PSTOPS_LAYOUT_RLTB
;
2471 else if (!_cups_strcasecmp(val
, "rlbt"))
2472 doc
->number_up_layout
= PSTOPS_LAYOUT_RLBT
;
2473 else if (!_cups_strcasecmp(val
, "tblr"))
2474 doc
->number_up_layout
= PSTOPS_LAYOUT_TBLR
;
2475 else if (!_cups_strcasecmp(val
, "tbrl"))
2476 doc
->number_up_layout
= PSTOPS_LAYOUT_TBRL
;
2477 else if (!_cups_strcasecmp(val
, "btlr"))
2478 doc
->number_up_layout
= PSTOPS_LAYOUT_BTLR
;
2479 else if (!_cups_strcasecmp(val
, "btrl"))
2480 doc
->number_up_layout
= PSTOPS_LAYOUT_BTRL
;
2483 _cupsLangPrintFilter(stderr
, "ERROR",
2484 _("Unsupported number-up-layout value %s, using "
2485 "number-up-layout=lrtb."), val
);
2486 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2490 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2496 if ((val
= cupsGetOption("OutputOrder", num_options
, options
)) != NULL
)
2498 if (!_cups_strcasecmp(val
, "Reverse"))
2499 doc
->output_order
= 1;
2504 * Figure out the right default output order from the PPD file...
2507 if ((choice
= ppdFindMarkedChoice(ppd
, "OutputBin")) != NULL
&&
2508 (attr
= ppdFindAttr(ppd
, "PageStackOrder", choice
->choice
)) != NULL
&&
2510 doc
->output_order
= !_cups_strcasecmp(attr
->value
, "Reverse");
2511 else if ((attr
= ppdFindAttr(ppd
, "DefaultOutputOrder", NULL
)) != NULL
&&
2513 doc
->output_order
= !_cups_strcasecmp(attr
->value
, "Reverse");
2520 if ((val
= cupsGetOption("page-border", num_options
, options
)) != NULL
)
2522 if (!_cups_strcasecmp(val
, "none"))
2523 doc
->page_border
= PSTOPS_BORDERNONE
;
2524 else if (!_cups_strcasecmp(val
, "single"))
2525 doc
->page_border
= PSTOPS_BORDERSINGLE
;
2526 else if (!_cups_strcasecmp(val
, "single-thick"))
2527 doc
->page_border
= PSTOPS_BORDERSINGLE2
;
2528 else if (!_cups_strcasecmp(val
, "double"))
2529 doc
->page_border
= PSTOPS_BORDERDOUBLE
;
2530 else if (!_cups_strcasecmp(val
, "double-thick"))
2531 doc
->page_border
= PSTOPS_BORDERDOUBLE2
;
2534 _cupsLangPrintFilter(stderr
, "ERROR",
2535 _("Unsupported page-border value %s, using "
2536 "page-border=none."), val
);
2537 doc
->page_border
= PSTOPS_BORDERNONE
;
2541 doc
->page_border
= PSTOPS_BORDERNONE
;
2547 doc
->page_label
= cupsGetOption("page-label", num_options
, options
);
2553 doc
->page_ranges
= cupsGetOption("page-ranges", num_options
, options
);
2559 doc
->page_set
= cupsGetOption("page-set", num_options
, options
);
2562 * Now figure out if we have to force collated copies, etc.
2565 if ((attr
= ppdFindAttr(ppd
, "cupsMaxCopies", NULL
)) != NULL
)
2566 max_copies
= atoi(attr
->value
);
2567 else if (ppd
&& ppd
->manual_copies
)
2572 if (doc
->copies
> max_copies
)
2574 else if (ppd
&& ppd
->manual_copies
&& Duplex
&& doc
->copies
> 1)
2577 * Force collated copies when printing a duplexed document to
2578 * a non-PS printer that doesn't do hardware copy generation.
2579 * Otherwise the copies will end up on the front/back side of
2587 * See if we have to filter the fast or slow way...
2590 if (doc
->collate
&& doc
->copies
> 1)
2593 * See if we need to manually collate the pages...
2596 doc
->slow_collate
= 1;
2598 if (doc
->copies
<= max_copies
&&
2599 (choice
= ppdFindMarkedChoice(ppd
, "Collate")) != NULL
&&
2600 !_cups_strcasecmp(choice
->choice
, "True"))
2603 * Hardware collate option is selected, see if the option is
2604 * conflicting - if not, collate in hardware. Otherwise,
2605 * turn the hardware collate option off...
2608 if ((option
= ppdFindOption(ppd
, "Collate")) != NULL
&&
2609 !option
->conflicted
)
2610 doc
->slow_collate
= 0;
2612 ppdMarkOption(ppd
, "Collate", "False");
2616 doc
->slow_collate
= 0;
2618 if (!ppdFindOption(ppd
, "OutputOrder") && doc
->output_order
)
2619 doc
->slow_order
= 1;
2621 doc
->slow_order
= 0;
2624 (doc
->slow_collate
|| doc
->slow_order
||
2625 ((attr
= ppdFindAttr(ppd
, "cupsEvenDuplex", NULL
)) != NULL
&&
2626 attr
->value
&& !_cups_strcasecmp(attr
->value
, "true"))))
2627 doc
->slow_duplex
= 1;
2629 doc
->slow_duplex
= 0;
2632 * Create a temporary file for page data if we need to filter slowly...
2635 if (doc
->slow_order
|| doc
->slow_collate
)
2637 if ((doc
->temp
= cupsTempFile2(doc
->tempfile
,
2638 sizeof(doc
->tempfile
))) == NULL
)
2640 perror("DEBUG: Unable to create temporary file");
2646 * Figure out if we should use ESPshowpage or not...
2649 if (doc
->page_label
|| getenv("CLASSIFICATION") || doc
->number_up
> 1 ||
2653 * Yes, use ESPshowpage...
2656 doc
->use_ESPshowpage
= 1;
2659 fprintf(stderr
, "DEBUG: slow_collate=%d, slow_duplex=%d, slow_order=%d\n",
2660 doc
->slow_collate
, doc
->slow_duplex
, doc
->slow_order
);
2665 * 'skip_page()' - Skip past a page that won't be printed.
2668 static ssize_t
/* O - Length of next line */
2669 skip_page(cups_file_t
*fp
, /* I - File to read from */
2670 char *line
, /* I - Line buffer */
2671 ssize_t linelen
, /* I - Length of initial line */
2672 size_t linesize
) /* I - Size of line buffer */
2674 int level
; /* Embedded document level */
2679 while ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) > 0)
2682 (!strncmp(line
, "%%Page:", 7) || !strncmp(line
, "%%Trailer", 9)))
2684 else if (!strncmp(line
, "%%BeginDocument", 15) ||
2685 !strncmp(line
, "%ADO_BeginApplication", 21))
2687 else if ((!strncmp(line
, "%%EndDocument", 13) ||
2688 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
2690 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
2691 (!strncmp(line
, "%%BeginData:", 12) &&
2692 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
2695 * Skip binary data...
2698 ssize_t bytes
; /* Bytes of data */
2700 bytes
= atoi(strchr(line
, ':') + 1);
2704 if ((size_t)bytes
> linesize
)
2705 linelen
= (ssize_t
)cupsFileRead(fp
, line
, linesize
);
2707 linelen
= (ssize_t
)cupsFileRead(fp
, line
, (size_t)bytes
);
2712 perror("ERROR: Early end-of-file while reading binary data");
2726 * 'start_nup()' - Start processing for N-up printing.
2730 start_nup(pstops_doc_t
*doc
, /* I - Document information */
2731 int number
, /* I - Page number */
2732 int show_border
, /* I - Show the border? */
2733 const int *bounding_box
) /* I - BoundingBox value */
2735 int pos
; /* Position on page */
2736 int x
, y
; /* Relative position of subpage */
2737 double w
, l
, /* Width and length of subpage */
2738 tx
, ty
; /* Translation values for subpage */
2739 double pagew
, /* Printable width of page */
2740 pagel
; /* Printable height of page */
2741 int bboxx
, /* BoundingBox X origin */
2742 bboxy
, /* BoundingBox Y origin */
2743 bboxw
, /* BoundingBox width */
2744 bboxl
; /* BoundingBox height */
2745 double margin
= 0; /* Current margin for border */
2748 if (doc
->number_up
> 1)
2749 doc_puts(doc
, "userdict/ESPsave save put\n");
2751 pos
= (number
- 1) % doc
->number_up
;
2752 pagew
= PageRight
- PageLeft
;
2753 pagel
= PageTop
- PageBottom
;
2755 if (doc
->fit_to_page
)
2757 bboxx
= bounding_box
[0];
2758 bboxy
= bounding_box
[1];
2759 bboxw
= bounding_box
[2] - bounding_box
[0];
2760 bboxl
= bounding_box
[3] - bounding_box
[1];
2766 bboxw
= (int)PageWidth
;
2767 bboxl
= (int)PageLength
;
2770 fprintf(stderr
, "DEBUG: pagew = %.1f, pagel = %.1f\n", pagew
, pagel
);
2771 fprintf(stderr
, "DEBUG: bboxx = %d, bboxy = %d, bboxw = %d, bboxl = %d\n",
2772 bboxx
, bboxy
, bboxw
, bboxl
);
2773 fprintf(stderr
, "DEBUG: PageLeft = %.1f, PageRight = %.1f\n",
2774 PageLeft
, PageRight
);
2775 fprintf(stderr
, "DEBUG: PageTop = %.1f, PageBottom = %.1f\n",
2776 PageTop
, PageBottom
);
2777 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
2778 PageWidth
, PageLength
);
2780 switch (Orientation
)
2782 case 1 : /* Landscape */
2783 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", PageLength
);
2785 case 2 : /* Reverse Portrait */
2786 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", PageWidth
,
2789 case 3 : /* Reverse Landscape */
2790 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", PageWidth
);
2795 * Mirror the page as needed...
2799 doc_printf(doc
, "%.1f 0.0 translate -1 1 scale\n", PageWidth
);
2802 * Offset and scale as necessary for fit_to_page/fit-to-page/number-up...
2805 if (Duplex
&& doc
->number_up
> 1 && ((number
/ doc
->number_up
) & 1))
2806 doc_printf(doc
, "%.1f %.1f translate\n", PageWidth
- PageRight
, PageBottom
);
2807 else if (doc
->number_up
> 1 || doc
->fit_to_page
)
2808 doc_printf(doc
, "%.1f %.1f translate\n", PageLeft
, PageBottom
);
2810 switch (doc
->number_up
)
2813 if (doc
->fit_to_page
)
2816 l
= w
* bboxl
/ bboxw
;
2821 w
= l
* bboxw
/ bboxl
;
2824 tx
= 0.5 * (pagew
- w
);
2825 ty
= 0.5 * (pagel
- l
);
2827 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n", tx
, ty
,
2828 w
/ bboxw
, l
/ bboxl
);
2835 if (Orientation
& 1)
2839 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2843 l
= w
* bboxl
/ bboxw
;
2845 if (l
> (pagew
* 0.5))
2848 w
= l
* bboxw
/ bboxl
;
2851 tx
= 0.5 * (pagew
* 0.5 - l
);
2852 ty
= 0.5 * (pagel
- w
);
2854 if (doc
->normal_landscape
)
2855 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2857 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2859 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2860 ty
, tx
+ pagew
* 0.5 * x
, w
/ bboxw
, l
/ bboxl
);
2866 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2870 w
= l
* bboxw
/ bboxl
;
2872 if (w
> (pagel
* 0.5))
2875 l
= w
* bboxl
/ bboxw
;
2878 tx
= 0.5 * (pagel
* 0.5 - w
);
2879 ty
= 0.5 * (pagew
- l
);
2881 if (doc
->normal_landscape
)
2882 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2884 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2886 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2887 tx
+ pagel
* 0.5 * x
, ty
, w
/ bboxw
, l
/ bboxl
);
2892 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2903 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2906 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2910 l
= w
* bboxl
/ bboxw
;
2912 if (l
> (pagel
* 0.5))
2915 w
= l
* bboxw
/ bboxl
;
2918 tx
= 0.5 * (pagew
* 0.5 - w
);
2919 ty
= 0.5 * (pagel
* 0.5 - l
);
2921 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2922 tx
+ x
* pagew
* 0.5, ty
+ y
* pagel
* 0.5,
2923 w
/ bboxw
, l
/ bboxl
);
2927 if (Orientation
& 1)
2929 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2934 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2937 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2945 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2948 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2953 l
= w
* bboxl
/ bboxw
;
2955 if (l
> (pagew
* 0.333))
2958 w
= l
* bboxw
/ bboxl
;
2961 tx
= 0.5 * (pagel
- 2 * w
);
2962 ty
= 0.5 * (pagew
- 3 * l
);
2964 if (doc
->normal_landscape
)
2965 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
2967 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
2969 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2970 tx
+ x
* w
, ty
+ y
* l
, l
/ bboxl
, w
/ bboxw
);
2974 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2979 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2982 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2990 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2993 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2998 w
= l
* bboxw
/ bboxl
;
3000 if (w
> (pagel
* 0.333))
3003 l
= w
* bboxl
/ bboxw
;
3006 tx
= 0.5 * (pagel
- 3 * w
);
3007 ty
= 0.5 * (pagew
- 2 * l
);
3009 if (doc
->normal_landscape
)
3010 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
3012 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
3014 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3015 tx
+ w
* x
, ty
+ l
* y
, w
/ bboxw
, l
/ bboxl
);
3021 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3032 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3035 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3039 l
= w
* bboxl
/ bboxw
;
3041 if (l
> (pagel
* 0.333))
3044 w
= l
* bboxw
/ bboxl
;
3047 tx
= 0.5 * (pagew
* 0.333 - w
);
3048 ty
= 0.5 * (pagel
* 0.333 - l
);
3050 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3051 tx
+ x
* pagew
* 0.333, ty
+ y
* pagel
* 0.333,
3052 w
/ bboxw
, l
/ bboxl
);
3056 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3067 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3070 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3074 l
= w
* bboxl
/ bboxw
;
3076 if (l
> (pagel
* 0.25))
3079 w
= l
* bboxw
/ bboxl
;
3082 tx
= 0.5 * (pagew
* 0.25 - w
);
3083 ty
= 0.5 * (pagel
* 0.25 - l
);
3085 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3086 tx
+ x
* pagew
* 0.25, ty
+ y
* pagel
* 0.25,
3087 w
/ bboxw
, l
/ bboxl
);
3092 * Draw borders as necessary...
3095 if (doc
->page_border
&& show_border
)
3097 int rects
; /* Number of border rectangles */
3098 double fscale
; /* Scaling value for points */
3101 rects
= (doc
->page_border
& PSTOPS_BORDERDOUBLE
) ? 2 : 1;
3102 fscale
= PageWidth
/ w
;
3103 margin
= 2.25 * fscale
;
3106 * Set the line width and color...
3109 doc_puts(doc
, "gsave\n");
3110 doc_printf(doc
, "%.3f setlinewidth 0 setgray newpath\n",
3111 (doc
->page_border
& PSTOPS_BORDERTHICK
) ? 0.5 * fscale
:
3115 * Draw border boxes...
3118 for (; rects
> 0; rects
--, margin
+= 2 * fscale
)
3119 if (doc
->number_up
> 1)
3120 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3124 bboxl
- 2 * margin
);
3126 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3128 PageBottom
+ margin
,
3129 PageRight
- PageLeft
- 2 * margin
,
3130 PageTop
- PageBottom
- 2 * margin
);
3133 * Restore pen settings...
3136 doc_puts(doc
, "grestore\n");
3139 if (doc
->fit_to_page
)
3142 * Offset the page by its bounding box...
3145 doc_printf(doc
, "%d %d translate\n", -bounding_box
[0],
3149 if (doc
->fit_to_page
|| doc
->number_up
> 1)
3152 * Clip the page to the page's bounding box...
3155 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrc\n",
3156 bboxx
+ margin
, bboxy
+ margin
,
3157 bboxw
- 2 * margin
, bboxl
- 2 * margin
);
3163 * 'write_label_prolog()' - Write the prolog with the classification
3168 write_label_prolog(pstops_doc_t
*doc
, /* I - Document info */
3169 const char *label
, /* I - Page label */
3170 float bottom
, /* I - Bottom position in points */
3171 float top
, /* I - Top position in points */
3172 float width
) /* I - Width in points */
3174 const char *classification
; /* CLASSIFICATION environment variable */
3175 const char *ptr
; /* Temporary string pointer */
3179 * First get the current classification...
3182 if ((classification
= getenv("CLASSIFICATION")) == NULL
)
3183 classification
= "";
3184 if (strcmp(classification
, "none") == 0)
3185 classification
= "";
3188 * If there is nothing to show, bind an empty 'write labels' procedure
3192 if (!classification
[0] && (label
== NULL
|| !label
[0]))
3194 doc_puts(doc
, "userdict/ESPwl{}bind put\n");
3199 * Set the classification + page label string...
3202 doc_puts(doc
, "userdict");
3203 if (!strcmp(classification
, "confidential"))
3204 doc_puts(doc
, "/ESPpl(CONFIDENTIAL");
3205 else if (!strcmp(classification
, "classified"))
3206 doc_puts(doc
, "/ESPpl(CLASSIFIED");
3207 else if (!strcmp(classification
, "secret"))
3208 doc_puts(doc
, "/ESPpl(SECRET");
3209 else if (!strcmp(classification
, "topsecret"))
3210 doc_puts(doc
, "/ESPpl(TOP SECRET");
3211 else if (!strcmp(classification
, "unclassified"))
3212 doc_puts(doc
, "/ESPpl(UNCLASSIFIED");
3215 doc_puts(doc
, "/ESPpl(");
3217 for (ptr
= classification
; *ptr
; ptr
++)
3219 if (*ptr
< 32 || *ptr
> 126)
3220 doc_printf(doc
, "\\%03o", *ptr
);
3221 else if (*ptr
== '_')
3223 else if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
3224 doc_printf(doc
, "\\%c", *ptr
);
3226 doc_printf(doc
, "%c", *ptr
);
3232 if (classification
[0])
3233 doc_puts(doc
, " - ");
3236 * Quote the label string as needed...
3239 for (ptr
= label
; *ptr
; ptr
++)
3241 if (*ptr
< 32 || *ptr
> 126)
3242 doc_printf(doc
, "\\%03o", *ptr
);
3243 else if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
3244 doc_printf(doc
, "\\%c", *ptr
);
3246 doc_printf(doc
, "%c", *ptr
);
3250 doc_puts(doc
, ")put\n");
3253 * Then get a 14 point Helvetica-Bold font...
3256 doc_puts(doc
, "userdict/ESPpf /Helvetica-Bold findfont 14 scalefont put\n");
3259 * Finally, the procedure to write the labels on the page...
3262 doc_puts(doc
, "userdict/ESPwl{\n");
3263 doc_puts(doc
, " ESPpf setfont\n");
3264 doc_printf(doc
, " ESPpl stringwidth pop dup 12 add exch -0.5 mul %.0f add\n",
3266 doc_puts(doc
, " 1 setgray\n");
3267 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrf\n", bottom
- 2.0);
3268 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrf\n", top
- 18.0);
3269 doc_puts(doc
, " 0 setgray\n");
3270 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrs\n", bottom
- 2.0);
3271 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrs\n", top
- 18.0);
3272 doc_printf(doc
, " dup %.0f moveto ESPpl show\n", bottom
+ 2.0);
3273 doc_printf(doc
, " %.0f moveto ESPpl show\n", top
- 14.0);
3274 doc_puts(doc
, "pop\n");
3275 doc_puts(doc
, "}bind put\n");
3280 * 'write_labels()' - Write the actual page labels.
3282 * This function is a copy of the one in common.c since we need to
3283 * use doc_puts/doc_printf instead of puts/printf...
3287 write_labels(pstops_doc_t
*doc
, /* I - Document information */
3288 int orient
) /* I - Orientation of the page */
3290 float width
, /* Width of page */
3291 length
; /* Length of page */
3294 doc_puts(doc
, "gsave\n");
3296 if ((orient
^ Orientation
) & 1)
3304 length
= PageLength
;
3309 case 1 : /* Landscape */
3310 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", length
);
3312 case 2 : /* Reverse Portrait */
3313 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", width
, length
);
3315 case 3 : /* Reverse Landscape */
3316 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", width
);
3320 doc_puts(doc
, "ESPwl\n");
3321 doc_puts(doc
, "grestore\n");
3326 * 'write_options()' - Write options provided via %%IncludeFeature.
3331 pstops_doc_t
*doc
, /* I - Document */
3332 ppd_file_t
*ppd
, /* I - PPD file */
3333 int num_options
, /* I - Number of options */
3334 cups_option_t
*options
) /* I - Options */
3336 int i
; /* Looping var */
3337 ppd_option_t
*option
; /* PPD option */
3338 float min_order
; /* Minimum OrderDependency value */
3339 char *doc_setup
, /* DocumentSetup commands to send */
3340 *any_setup
; /* AnySetup commands to send */
3344 * Figure out the minimum OrderDependency value...
3347 if ((option
= ppdFindOption(ppd
, "PageRegion")) != NULL
)
3348 min_order
= option
->order
;
3352 for (i
= 0; i
< num_options
; i
++)
3353 if ((option
= ppdFindOption(ppd
, options
[i
].name
)) != NULL
&&
3354 option
->order
< min_order
)
3355 min_order
= option
->order
;
3358 * Mark and extract them...
3361 cupsMarkOptions(ppd
, num_options
, options
);
3363 doc_setup
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, min_order
);
3364 any_setup
= ppdEmitString(ppd
, PPD_ORDER_ANY
, min_order
);
3367 * Then send them out...
3370 if (doc
->number_up
> 1)
3373 * Temporarily restore setpagedevice so we can set the options...
3376 doc_puts(doc
, "userdict/setpagedevice/CUPSsetpagedevice load put\n");
3381 doc_puts(doc
, doc_setup
);
3387 doc_puts(doc
, any_setup
);
3391 if (doc
->number_up
> 1)
3394 * Disable setpagedevice again...
3397 doc_puts(doc
, "userdict/setpagedevice{pop}bind put\n");