]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/pstops.c
2 * PostScript filter for CUPS.
4 * Copyright 2007-2015 by Apple Inc.
5 * Copyright 1993-2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
17 #include <cups/file.h>
18 #include <cups/array.h>
19 #include <cups/language-private.h>
27 #define PSTOPS_BORDERNONE 0 /* No border or hairline border */
28 #define PSTOPS_BORDERTHICK 1 /* Think border */
29 #define PSTOPS_BORDERSINGLE 2 /* Single-line hairline border */
30 #define PSTOPS_BORDERSINGLE2 3 /* Single-line thick border */
31 #define PSTOPS_BORDERDOUBLE 4 /* Double-line hairline border */
32 #define PSTOPS_BORDERDOUBLE2 5 /* Double-line thick border */
34 #define PSTOPS_LAYOUT_LRBT 0 /* Left to right, bottom to top */
35 #define PSTOPS_LAYOUT_LRTB 1 /* Left to right, top to bottom */
36 #define PSTOPS_LAYOUT_RLBT 2 /* Right to left, bottom to top */
37 #define PSTOPS_LAYOUT_RLTB 3 /* Right to left, top to bottom */
38 #define PSTOPS_LAYOUT_BTLR 4 /* Bottom to top, left to right */
39 #define PSTOPS_LAYOUT_TBLR 5 /* Top to bottom, left to right */
40 #define PSTOPS_LAYOUT_BTRL 6 /* Bottom to top, right to left */
41 #define PSTOPS_LAYOUT_TBRL 7 /* Top to bottom, right to left */
43 #define PSTOPS_LAYOUT_NEGATEY 1 /* The bits for the layout */
44 #define PSTOPS_LAYOUT_NEGATEX 2 /* definitions above... */
45 #define PSTOPS_LAYOUT_VERTICAL 4
52 typedef struct /**** Page information ****/
54 char *label
; /* Page label */
55 int bounding_box
[4]; /* PageBoundingBox */
56 off_t offset
; /* Offset to start of page */
57 ssize_t length
; /* Number of bytes for page */
58 int num_options
; /* Number of options for this page */
59 cups_option_t
*options
; /* Options for this page */
62 typedef struct /**** Document information ****/
64 int page
; /* Current page */
65 int bounding_box
[4]; /* BoundingBox from header */
66 int new_bounding_box
[4]; /* New composite bounding box */
67 int num_options
; /* Number of document-wide options */
68 cups_option_t
*options
; /* Document-wide options */
69 int normal_landscape
, /* Normal rotation for landscape? */
70 saw_eof
, /* Saw the %%EOF comment? */
71 slow_collate
, /* Collate copies by hand? */
72 slow_duplex
, /* Duplex pages slowly? */
73 slow_order
, /* Reverse pages slowly? */
74 use_ESPshowpage
; /* Use ESPshowpage? */
75 cups_array_t
*pages
; /* Pages in document */
76 cups_file_t
*temp
; /* Temporary file, if any */
77 char tempfile
[1024]; /* Temporary filename */
78 int job_id
; /* Job ID */
79 const char *user
, /* User name */
80 *title
; /* Job name */
81 int copies
; /* Number of copies */
82 const char *ap_input_slot
, /* AP_FIRSTPAGE_InputSlot value */
83 *ap_manual_feed
, /* AP_FIRSTPAGE_ManualFeed value */
84 *ap_media_color
, /* AP_FIRSTPAGE_MediaColor value */
85 *ap_media_type
, /* AP_FIRSTPAGE_MediaType value */
86 *ap_page_region
, /* AP_FIRSTPAGE_PageRegion value */
87 *ap_page_size
; /* AP_FIRSTPAGE_PageSize value */
88 int collate
, /* Collate copies? */
89 emit_jcl
, /* Emit JCL commands? */
90 fit_to_page
; /* Fit pages to media */
91 const char *input_slot
, /* InputSlot value */
92 *manual_feed
, /* ManualFeed value */
93 *media_color
, /* MediaColor value */
94 *media_type
, /* MediaType value */
95 *page_region
, /* PageRegion value */
96 *page_size
; /* PageSize value */
97 int mirror
, /* doc->mirror/mirror pages */
98 number_up
, /* Number of pages on each sheet */
99 number_up_layout
, /* doc->number_up_layout of N-up pages */
100 output_order
, /* Requested reverse output order? */
101 page_border
; /* doc->page_border around pages */
102 const char *page_label
, /* page-label option, if any */
103 *page_ranges
, /* page-ranges option, if any */
104 *page_set
; /* page-set option, if any */
109 * Convenience macros...
112 #define is_first_page(p) (doc->number_up == 1 || \
113 ((p) % doc->number_up) == 1)
114 #define is_last_page(p) (doc->number_up == 1 || \
115 ((p) % doc->number_up) == 0)
116 #define is_not_last_page(p) (doc->number_up > 1 && \
117 ((p) % doc->number_up) != 0)
124 static int JobCanceled
= 0;/* Set to 1 on SIGTERM */
131 static pstops_page_t
*add_page(pstops_doc_t
*doc
, const char *label
);
132 static void cancel_job(int sig
);
133 static int check_range(pstops_doc_t
*doc
, int page
);
134 static void copy_bytes(cups_file_t
*fp
, off_t offset
,
136 static ssize_t
copy_comments(cups_file_t
*fp
, pstops_doc_t
*doc
,
137 ppd_file_t
*ppd
, char *line
,
138 ssize_t linelen
, size_t linesize
);
139 static void copy_dsc(cups_file_t
*fp
, pstops_doc_t
*doc
,
140 ppd_file_t
*ppd
, char *line
, ssize_t linelen
,
142 static void copy_non_dsc(cups_file_t
*fp
, pstops_doc_t
*doc
,
143 ppd_file_t
*ppd
, char *line
,
144 ssize_t linelen
, size_t linesize
);
145 static ssize_t
copy_page(cups_file_t
*fp
, pstops_doc_t
*doc
,
146 ppd_file_t
*ppd
, int number
, char *line
,
147 ssize_t linelen
, size_t linesize
);
148 static ssize_t
copy_prolog(cups_file_t
*fp
, pstops_doc_t
*doc
,
149 ppd_file_t
*ppd
, char *line
,
150 ssize_t linelen
, size_t linesize
);
151 static ssize_t
copy_setup(cups_file_t
*fp
, pstops_doc_t
*doc
,
152 ppd_file_t
*ppd
, char *line
,
153 ssize_t linelen
, size_t linesize
);
154 static ssize_t
copy_trailer(cups_file_t
*fp
, pstops_doc_t
*doc
,
155 ppd_file_t
*ppd
, int number
, char *line
,
156 ssize_t linelen
, size_t linesize
);
157 static void do_prolog(pstops_doc_t
*doc
, ppd_file_t
*ppd
);
158 static void do_setup(pstops_doc_t
*doc
, ppd_file_t
*ppd
);
159 static void doc_printf(pstops_doc_t
*doc
, const char *format
, ...)
160 __attribute__ ((__format__ (__printf__
, 2, 3)));
161 static void doc_puts(pstops_doc_t
*doc
, const char *s
);
162 static void doc_write(pstops_doc_t
*doc
, const char *s
, size_t len
);
163 static void end_nup(pstops_doc_t
*doc
, int number
);
164 static int include_feature(ppd_file_t
*ppd
, const char *line
,
166 cups_option_t
**options
);
167 static char *parse_text(const char *start
, char **end
, char *buffer
,
169 static void set_pstops_options(pstops_doc_t
*doc
, ppd_file_t
*ppd
,
170 char *argv
[], int num_options
,
171 cups_option_t
*options
);
172 static ssize_t
skip_page(cups_file_t
*fp
, char *line
, ssize_t linelen
,
174 static void start_nup(pstops_doc_t
*doc
, int number
,
175 int show_border
, const int *bounding_box
);
176 static void write_label_prolog(pstops_doc_t
*doc
, const char *label
,
177 float bottom
, float top
,
179 static void write_labels(pstops_doc_t
*doc
, int orient
);
180 static void write_options(pstops_doc_t
*doc
, ppd_file_t
*ppd
,
181 int num_options
, cups_option_t
*options
);
185 * 'main()' - Main entry.
188 int /* O - Exit status */
189 main(int argc
, /* I - Number of command-line args */
190 char *argv
[]) /* I - Command-line arguments */
192 pstops_doc_t doc
; /* Document information */
193 cups_file_t
*fp
; /* Print file */
194 ppd_file_t
*ppd
; /* PPD file */
195 int num_options
; /* Number of print options */
196 cups_option_t
*options
; /* Print options */
197 char line
[8192]; /* Line buffer */
198 ssize_t len
; /* Length of line buffer */
199 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
200 struct sigaction action
; /* Actions for POSIX signals */
201 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
205 * Make sure status messages are not buffered...
208 setbuf(stderr
, NULL
);
211 * Ignore broken pipe signals...
214 signal(SIGPIPE
, SIG_IGN
);
217 * Check command-line...
220 if (argc
< 6 || argc
> 7)
222 _cupsLangPrintf(stderr
,
223 _("Usage: %s job-id user title copies options [file]"),
229 * Register a signal handler to cleanly cancel a job.
232 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
233 sigset(SIGTERM
, cancel_job
);
234 #elif defined(HAVE_SIGACTION)
235 memset(&action
, 0, sizeof(action
));
237 sigemptyset(&action
.sa_mask
);
238 action
.sa_handler
= cancel_job
;
239 sigaction(SIGTERM
, &action
, NULL
);
241 signal(SIGTERM
, cancel_job
);
242 #endif /* HAVE_SIGSET */
245 * If we have 7 arguments, print the file named on the command-line.
246 * Otherwise, send stdin instead...
250 fp
= cupsFileStdin();
254 * Try to open the print file...
257 if ((fp
= cupsFileOpen(argv
[6], "r")) == NULL
)
261 fprintf(stderr
, "DEBUG: Unable to open \"%s\".\n", argv
[6]);
262 _cupsLangPrintError("ERROR", _("Unable to open print file"));
270 * Read the first line to see if we have DSC comments...
273 if ((len
= (ssize_t
)cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
275 fputs("DEBUG: The print file is empty.\n", stderr
);
280 * Process command-line options...
284 num_options
= cupsParseOptions(argv
[5], 0, &options
);
285 ppd
= SetCommonOptions(num_options
, options
, 1);
287 set_pstops_options(&doc
, ppd
, argv
, num_options
, options
);
290 * Write any "exit server" options that have been selected...
293 ppdEmit(ppd
, stdout
, PPD_ORDER_EXIT
);
296 * Write any JCL commands that are needed to print PostScript code...
300 ppdEmitJCL(ppd
, stdout
, doc
.job_id
, doc
.user
, doc
.title
);
303 * Start with a DSC header...
306 puts("%!PS-Adobe-3.0");
309 * Skip leading PJL in the document...
312 while (!strncmp(line
, "\033%-12345X", 9) || !strncmp(line
, "@PJL ", 5))
315 * Yup, we have leading PJL fun, so skip it until we hit the line
316 * with "ENTER LANGUAGE"...
319 fputs("DEBUG: Skipping PJL header...\n", stderr
);
321 while (strstr(line
, "ENTER LANGUAGE") == NULL
&& strncmp(line
, "%!", 2))
322 if ((len
= (ssize_t
)cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
325 if (!strncmp(line
, "%!", 2))
328 if ((len
= (ssize_t
)cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
333 * Now see if the document conforms to the Adobe Document Structuring
337 if (!strncmp(line
, "%!PS-Adobe-", 11))
340 * Yes, filter the document...
343 copy_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
348 * No, display an error message and treat the file as if it contains
352 copy_non_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
356 * Send %%EOF as needed...
363 * End the job with the appropriate JCL command or CTRL-D...
368 if (ppd
&& ppd
->jcl_end
)
369 ppdEmitJCLEnd(ppd
, stdout
);
375 * Close files and remove the temporary file if needed...
380 cupsFileClose(doc
.temp
);
381 unlink(doc
.tempfile
);
385 cupsFreeOptions(num_options
, options
);
394 * 'add_page()' - Add a page to the pages array.
397 static pstops_page_t
* /* O - New page info object */
398 add_page(pstops_doc_t
*doc
, /* I - Document information */
399 const char *label
) /* I - Page label */
401 pstops_page_t
*pageinfo
; /* New page info object */
405 doc
->pages
= cupsArrayNew(NULL
, NULL
);
409 _cupsLangPrintError("EMERG", _("Unable to allocate memory for pages array"));
413 if ((pageinfo
= calloc(1, sizeof(pstops_page_t
))) == NULL
)
415 _cupsLangPrintError("EMERG", _("Unable to allocate memory for page info"));
419 pageinfo
->label
= strdup(label
);
420 pageinfo
->offset
= cupsFileTell(doc
->temp
);
422 cupsArrayAdd(doc
->pages
, pageinfo
);
431 * 'cancel_job()' - Flag the job as canceled.
435 cancel_job(int sig
) /* I - Signal number (unused) */
444 * 'check_range()' - Check to see if the current page is selected for
448 static int /* O - 1 if selected, 0 otherwise */
449 check_range(pstops_doc_t
*doc
, /* I - Document information */
450 int page
) /* I - Page number */
452 const char *range
; /* Pointer into range string */
453 int lower
, upper
; /* Lower and upper page numbers */
459 * See if we only print even or odd pages...
462 if (!_cups_strcasecmp(doc
->page_set
, "even") && (page
& 1))
465 if (!_cups_strcasecmp(doc
->page_set
, "odd") && !(page
& 1))
469 if (!doc
->page_ranges
)
470 return (1); /* No range, print all pages... */
472 for (range
= doc
->page_ranges
; *range
!= '\0';)
478 upper
= (int)strtol(range
, (char **)&range
, 10);
482 lower
= (int)strtol(range
, (char **)&range
, 10);
487 if (!isdigit(*range
& 255))
490 upper
= (int)strtol(range
, (char **)&range
, 10);
496 if (page
>= lower
&& page
<= upper
)
510 * 'copy_bytes()' - Copy bytes from the input file to stdout.
514 copy_bytes(cups_file_t
*fp
, /* I - File to read from */
515 off_t offset
, /* I - Offset to page data */
516 size_t length
) /* I - Length of page data */
518 char buffer
[8192]; /* Data buffer */
519 ssize_t nbytes
; /* Number of bytes read */
520 size_t nleft
; /* Number of bytes left/remaining */
525 if (cupsFileSeek(fp
, offset
) < 0)
527 _cupsLangPrintError("ERROR", _("Unable to see in file"));
531 while (nleft
> 0 || length
== 0)
533 if (nleft
> sizeof(buffer
) || length
== 0)
534 nbytes
= sizeof(buffer
);
536 nbytes
= (ssize_t
)nleft
;
538 if ((nbytes
= cupsFileRead(fp
, buffer
, (size_t)nbytes
)) < 1)
541 nleft
-= (size_t)nbytes
;
543 fwrite(buffer
, 1, (size_t)nbytes
, stdout
);
549 * 'copy_comments()' - Copy all of the comments section.
551 * This function expects "line" to be filled with a comment line.
552 * On return, "line" will contain the next line in the file, if any.
555 static ssize_t
/* O - Length of next line */
556 copy_comments(cups_file_t
*fp
, /* I - File to read from */
557 pstops_doc_t
*doc
, /* I - Document info */
558 ppd_file_t
*ppd
, /* I - PPD file */
559 char *line
, /* I - Line buffer */
560 ssize_t linelen
, /* I - Length of initial line */
561 size_t linesize
) /* I - Size of line buffer */
563 int saw_bounding_box
, /* Saw %%BoundingBox: comment? */
564 saw_for
, /* Saw %%For: comment? */
565 saw_pages
, /* Saw %%Pages: comment? */
566 saw_title
; /* Saw %%Title: comment? */
570 * Loop until we see %%EndComments or a non-comment line...
573 saw_bounding_box
= 0;
578 while (line
[0] == '%')
581 * Strip trailing whitespace...
588 if (!isspace(line
[linelen
] & 255))
591 line
[linelen
] = '\0';
598 fprintf(stderr
, "DEBUG: %s\n", line
);
601 * Pull the headers out...
604 if (!strncmp(line
, "%%Pages:", 8))
606 int pages
; /* Number of pages */
609 fputs("DEBUG: A duplicate %%Pages: comment was seen.\n", stderr
);
613 if (Duplex
&& (pages
= atoi(line
+ 8)) > 0 && pages
<= doc
->number_up
)
616 * Since we will only be printing on a single page, disable duplexing.
620 doc
->slow_duplex
= 0;
622 if (cupsGetOption("sides", doc
->num_options
, doc
->options
))
623 doc
->num_options
= cupsAddOption("sides", "one-sided",
624 doc
->num_options
, &(doc
->options
));
626 if (cupsGetOption("Duplex", doc
->num_options
, doc
->options
))
627 doc
->num_options
= cupsAddOption("Duplex", "None",
628 doc
->num_options
, &(doc
->options
));
630 if (cupsGetOption("EFDuplex", doc
->num_options
, doc
->options
))
631 doc
->num_options
= cupsAddOption("EFDuplex", "None",
632 doc
->num_options
, &(doc
->options
));
634 if (cupsGetOption("EFDuplexing", doc
->num_options
, doc
->options
))
635 doc
->num_options
= cupsAddOption("EFDuplexing", "False",
636 doc
->num_options
, &(doc
->options
));
638 if (cupsGetOption("KD03Duplex", doc
->num_options
, doc
->options
))
639 doc
->num_options
= cupsAddOption("KD03Duplex", "None",
640 doc
->num_options
, &(doc
->options
));
642 if (cupsGetOption("JCLDuplex", doc
->num_options
, doc
->options
))
643 doc
->num_options
= cupsAddOption("JCLDuplex", "None",
644 doc
->num_options
, &(doc
->options
));
646 ppdMarkOption(ppd
, "Duplex", "None");
647 ppdMarkOption(ppd
, "EFDuplex", "None");
648 ppdMarkOption(ppd
, "EFDuplexing", "False");
649 ppdMarkOption(ppd
, "KD03Duplex", "None");
650 ppdMarkOption(ppd
, "JCLDuplex", "None");
653 else if (!strncmp(line
, "%%BoundingBox:", 14))
655 if (saw_bounding_box
)
656 fputs("DEBUG: A duplicate %%BoundingBox: comment was seen.\n", stderr
);
657 else if (strstr(line
+ 14, "(atend)"))
660 * Do nothing for now but use the default imageable area...
663 else if (sscanf(line
+ 14, "%d%d%d%d", doc
->bounding_box
+ 0,
664 doc
->bounding_box
+ 1, doc
->bounding_box
+ 2,
665 doc
->bounding_box
+ 3) != 4)
667 fputs("DEBUG: A bad %%BoundingBox: comment was seen.\n", stderr
);
669 doc
->bounding_box
[0] = (int)PageLeft
;
670 doc
->bounding_box
[1] = (int)PageBottom
;
671 doc
->bounding_box
[2] = (int)PageRight
;
672 doc
->bounding_box
[3] = (int)PageTop
;
675 saw_bounding_box
= 1;
677 else if (!strncmp(line
, "%%For:", 6))
680 doc_printf(doc
, "%s\n", line
);
682 else if (!strncmp(line
, "%%Title:", 8))
685 doc_printf(doc
, "%s\n", line
);
687 else if (!strncmp(line
, "%cupsRotation:", 14))
690 * Reset orientation of document?
693 int orient
= (atoi(line
+ 14) / 90) & 3;
695 if (orient
!= Orientation
)
698 * Yes, update things so that the pages come out right...
701 Orientation
= (4 - Orientation
+ orient
) & 3;
703 Orientation
= orient
;
706 else if (!strcmp(line
, "%%EndComments"))
708 linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
);
711 else if (strncmp(line
, "%!", 2) && strncmp(line
, "%cups", 5))
712 doc_printf(doc
, "%s\n", line
);
714 if ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) == 0)
718 if (!saw_bounding_box
)
719 fputs("DEBUG: There wasn't a %%BoundingBox: comment in the header.\n",
723 fputs("DEBUG: There wasn't a %%Pages: comment in the header.\n", stderr
);
726 WriteTextComment("For", doc
->user
);
729 WriteTextComment("Title", doc
->title
);
731 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
734 * Tell the document processor the copy and duplex options
735 * that are required...
738 doc_printf(doc
, "%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
739 doc
->collate
? " collate" : "",
740 Duplex
? " duplex" : "");
743 * Apple uses RBI comments for various non-PPD options...
746 doc_printf(doc
, "%%RBINumCopies: %d\n", doc
->copies
);
751 * Tell the document processor the duplex option that is required...
755 doc_puts(doc
, "%%Requirements: duplex\n");
758 * Apple uses RBI comments for various non-PPD options...
761 doc_puts(doc
, "%RBINumCopies: 1\n");
764 doc_puts(doc
, "%%Pages: (atend)\n");
765 doc_puts(doc
, "%%BoundingBox: (atend)\n");
766 doc_puts(doc
, "%%EndComments\n");
773 * 'copy_dsc()' - Copy a DSC-conforming document.
775 * This function expects "line" to be filled with the %!PS-Adobe comment line.
779 copy_dsc(cups_file_t
*fp
, /* I - File to read from */
780 pstops_doc_t
*doc
, /* I - Document info */
781 ppd_file_t
*ppd
, /* I - PPD file */
782 char *line
, /* I - Line buffer */
783 ssize_t linelen
, /* I - Length of initial line */
784 size_t linesize
) /* I - Size of line buffer */
786 int number
; /* Page number */
787 pstops_page_t
*pageinfo
; /* Page information */
791 * Make sure we use ESPshowpage for EPS files...
794 if (strstr(line
, "EPSF"))
796 doc
->use_ESPshowpage
= 1;
801 * Start sending the document with any commands needed...
804 fprintf(stderr
, "DEBUG: Before copy_comments - %s", line
);
805 linelen
= copy_comments(fp
, doc
, ppd
, line
, linelen
, linesize
);
808 * Now find the prolog section, if any...
811 fprintf(stderr
, "DEBUG: Before copy_prolog - %s", line
);
812 linelen
= copy_prolog(fp
, doc
, ppd
, line
, linelen
, linesize
);
815 * Then the document setup section...
818 fprintf(stderr
, "DEBUG: Before copy_setup - %s", line
);
819 linelen
= copy_setup(fp
, doc
, ppd
, line
, linelen
, linesize
);
822 * Copy until we see %%Page:...
825 while (strncmp(line
, "%%Page:", 7) && strncmp(line
, "%%Trailer", 9))
827 doc_write(doc
, line
, (size_t)linelen
);
829 if ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) == 0)
834 * Then process pages until we have no more...
839 fprintf(stderr
, "DEBUG: Before page loop - %s", line
);
840 while (!strncmp(line
, "%%Page:", 7))
847 if (check_range(doc
, (number
- 1) / doc
->number_up
+ 1))
849 fprintf(stderr
, "DEBUG: Copying page %d...\n", number
);
850 linelen
= copy_page(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
854 fprintf(stderr
, "DEBUG: Skipping page %d...\n", number
);
855 linelen
= skip_page(fp
, line
, linelen
, linesize
);
860 * Finish up the last page(s)...
863 if (number
&& is_not_last_page(number
) && cupsArrayLast(doc
->pages
) &&
864 check_range(doc
, (number
- 1) / doc
->number_up
+ 1))
866 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
868 start_nup(doc
, doc
->number_up
, 0, doc
->bounding_box
);
869 doc_puts(doc
, "showpage\n");
870 end_nup(doc
, doc
->number_up
);
872 pageinfo
->length
= (ssize_t
)(cupsFileTell(doc
->temp
) - pageinfo
->offset
);
875 if (doc
->slow_duplex
&& (doc
->page
& 1))
878 * Make sure we have an even number of pages...
881 pageinfo
= add_page(doc
, "(filler)");
883 if (!doc
->slow_order
)
885 if (!ppd
|| !ppd
->num_filters
)
886 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
887 doc
->slow_collate
? 1 : doc
->copies
);
889 printf("%%%%Page: (filler) %d\n", doc
->page
);
892 start_nup(doc
, doc
->number_up
, 0, doc
->bounding_box
);
893 doc_puts(doc
, "showpage\n");
894 end_nup(doc
, doc
->number_up
);
896 pageinfo
->length
= (ssize_t
)(cupsFileTell(doc
->temp
) - pageinfo
->offset
);
900 * Make additional copies as necessary...
903 number
= doc
->slow_order
? 0 : doc
->page
;
905 if (doc
->temp
&& !JobCanceled
&& cupsArrayCount(doc
->pages
) > 0)
907 int copy
; /* Current copy */
911 * Reopen the temporary file for reading...
914 cupsFileClose(doc
->temp
);
916 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
922 if (doc
->slow_collate
)
923 copy
= !doc
->slow_order
;
925 copy
= doc
->copies
- 1;
927 for (; copy
< doc
->copies
; copy
++)
933 * Send end-of-job stuff followed by any start-of-job stuff required
934 * for the JCL options...
937 if (number
&& doc
->emit_jcl
&& ppd
&& ppd
->jcl_end
)
940 * Send the trailer...
944 printf("%%%%Pages: %d\n", cupsArrayCount(doc
->pages
));
945 if (doc
->number_up
> 1 || doc
->fit_to_page
)
946 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
947 PageLeft
, PageBottom
, PageRight
, PageTop
);
949 printf("%%%%BoundingBox: %d %d %d %d\n",
950 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
951 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
955 * Start a new document...
958 ppdEmitJCLEnd(ppd
, stdout
);
959 ppdEmitJCL(ppd
, stdout
, doc
->job_id
, doc
->user
, doc
->title
);
961 puts("%!PS-Adobe-3.0");
967 * Copy the prolog as needed...
972 pageinfo
= (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
973 copy_bytes(doc
->temp
, 0, (size_t)pageinfo
->offset
);
977 * Then copy all of the pages...
980 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayLast(doc
->pages
) :
981 (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
990 if (!ppd
|| !ppd
->num_filters
)
991 fprintf(stderr
, "PAGE: %d %d\n", number
,
992 doc
->slow_collate
? 1 : doc
->copies
);
994 if (doc
->number_up
> 1)
996 printf("%%%%Page: (%d) %d\n", number
, number
);
997 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
998 PageLeft
, PageBottom
, PageRight
, PageTop
);
1002 printf("%%%%Page: %s %d\n", pageinfo
->label
, number
);
1003 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1004 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
1005 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
1008 copy_bytes(doc
->temp
, pageinfo
->offset
, (size_t)pageinfo
->length
);
1010 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayPrev(doc
->pages
) :
1011 (pstops_page_t
*)cupsArrayNext(doc
->pages
);
1017 * Restore the old showpage operator as needed...
1020 if (doc
->use_ESPshowpage
)
1021 puts("userdict/showpage/ESPshowpage load put\n");
1024 * Write/copy the trailer...
1028 copy_trailer(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
1033 * 'copy_non_dsc()' - Copy a document that does not conform to the DSC.
1035 * This function expects "line" to be filled with the %! comment line.
1039 copy_non_dsc(cups_file_t
*fp
, /* I - File to read from */
1040 pstops_doc_t
*doc
, /* I - Document info */
1041 ppd_file_t
*ppd
, /* I - PPD file */
1042 char *line
, /* I - Line buffer */
1043 ssize_t linelen
, /* I - Length of initial line */
1044 size_t linesize
) /* I - Size of line buffer */
1046 int copy
; /* Current copy */
1047 char buffer
[8192]; /* Copy buffer */
1048 ssize_t bytes
; /* Number of bytes copied */
1054 * First let the user know that they are attempting to print a file
1055 * that may not print correctly...
1058 fputs("DEBUG: This document does not conform to the Adobe Document "
1059 "Structuring Conventions and may not print correctly.\n", stderr
);
1062 * Then write a standard DSC comment section...
1065 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", PageLeft
, PageBottom
,
1066 PageRight
, PageTop
);
1068 if (doc
->slow_collate
&& doc
->copies
> 1)
1069 printf("%%%%Pages: %d\n", doc
->copies
);
1073 WriteTextComment("For", doc
->user
);
1074 WriteTextComment("Title", doc
->title
);
1076 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1079 * Tell the document processor the copy and duplex options
1080 * that are required...
1083 printf("%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
1084 doc
->collate
? " collate" : "",
1085 Duplex
? " duplex" : "");
1088 * Apple uses RBI comments for various non-PPD options...
1091 printf("%%RBINumCopies: %d\n", doc
->copies
);
1096 * Tell the document processor the duplex option that is required...
1100 puts("%%Requirements: duplex");
1103 * Apple uses RBI comments for various non-PPD options...
1106 puts("%RBINumCopies: 1");
1109 puts("%%EndComments");
1112 * Then the prolog...
1115 puts("%%BeginProlog");
1117 do_prolog(doc
, ppd
);
1119 puts("%%EndProlog");
1122 * Then the setup section...
1125 puts("%%BeginSetup");
1132 * Finally, embed a copy of the file inside a %%Page...
1135 if (!ppd
|| !ppd
->num_filters
)
1136 fprintf(stderr
, "PAGE: 1 %d\n", doc
->temp
? 1 : doc
->copies
);
1138 puts("%%Page: 1 1");
1139 puts("%%BeginPageSetup");
1140 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1141 puts("%%EndPageSetup");
1142 puts("%%BeginDocument: nondsc");
1144 fwrite(line
, (size_t)linelen
, 1, stdout
);
1147 cupsFileWrite(doc
->temp
, line
, (size_t)linelen
);
1149 while ((bytes
= cupsFileRead(fp
, buffer
, sizeof(buffer
))) > 0)
1151 fwrite(buffer
, 1, (size_t)bytes
, stdout
);
1154 cupsFileWrite(doc
->temp
, buffer
, (size_t)bytes
);
1157 puts("%%EndDocument");
1159 if (doc
->use_ESPshowpage
)
1161 WriteLabels(Orientation
);
1162 puts("ESPshowpage");
1165 if (doc
->temp
&& !JobCanceled
)
1168 * Reopen the temporary file for reading...
1171 cupsFileClose(doc
->temp
);
1173 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
1176 * Make the additional copies as needed...
1179 for (copy
= 1; copy
< doc
->copies
; copy
++)
1184 if (!ppd
|| !ppd
->num_filters
)
1185 fputs("PAGE: 1 1\n", stderr
);
1187 printf("%%%%Page: %d %d\n", copy
+ 1, copy
+ 1);
1188 puts("%%BeginPageSetup");
1189 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1190 puts("%%EndPageSetup");
1191 puts("%%BeginDocument: nondsc");
1193 copy_bytes(doc
->temp
, 0, 0);
1195 puts("%%EndDocument");
1197 if (doc
->use_ESPshowpage
)
1199 WriteLabels(Orientation
);
1200 puts("ESPshowpage");
1206 * Restore the old showpage operator as needed...
1209 if (doc
->use_ESPshowpage
)
1210 puts("userdict/showpage/ESPshowpage load put\n");
1215 * 'copy_page()' - Copy a page description.
1217 * This function expects "line" to be filled with a %%Page comment line.
1218 * On return, "line" will contain the next line in the file, if any.
1221 static ssize_t
/* O - Length of next line */
1222 copy_page(cups_file_t
*fp
, /* I - File to read from */
1223 pstops_doc_t
*doc
, /* I - Document info */
1224 ppd_file_t
*ppd
, /* I - PPD file */
1225 int number
, /* I - Current page number */
1226 char *line
, /* I - Line buffer */
1227 ssize_t linelen
, /* I - Length of initial line */
1228 size_t linesize
) /* I - Size of line buffer */
1230 char label
[256], /* Page label string */
1231 *ptr
; /* Pointer into line */
1232 int level
; /* Embedded document level */
1233 pstops_page_t
*pageinfo
; /* Page information */
1234 int first_page
; /* First page on N-up output? */
1235 int has_page_setup
= 0; /* Does the page have %%Begin/EndPageSetup? */
1236 int bounding_box
[4]; /* PageBoundingBox */
1240 * Get the page label for this page...
1243 first_page
= is_first_page(number
);
1245 if (!parse_text(line
+ 7, &ptr
, label
, sizeof(label
)))
1247 fputs("DEBUG: There was a bad %%Page: comment in the file.\n", stderr
);
1251 else if (strtol(ptr
, &ptr
, 10) == LONG_MAX
|| !isspace(*ptr
& 255))
1253 fputs("DEBUG: There was a bad %%Page: comment in the file.\n", stderr
);
1258 * Create or update the current output page...
1262 pageinfo
= add_page(doc
, label
);
1264 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
1267 * Handle first page override...
1270 if (doc
->ap_input_slot
|| doc
->ap_manual_feed
)
1272 if ((doc
->page
== 1 && (!doc
->slow_order
|| !Duplex
)) ||
1273 (doc
->page
== 2 && doc
->slow_order
&& Duplex
))
1276 * First page/sheet gets AP_FIRSTPAGE_* options...
1279 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->ap_input_slot
,
1280 pageinfo
->num_options
,
1281 &(pageinfo
->options
));
1282 pageinfo
->num_options
= cupsAddOption("ManualFeed",
1283 doc
->ap_input_slot
? "False" :
1284 doc
->ap_manual_feed
,
1285 pageinfo
->num_options
,
1286 &(pageinfo
->options
));
1287 pageinfo
->num_options
= cupsAddOption("MediaColor", doc
->ap_media_color
,
1288 pageinfo
->num_options
,
1289 &(pageinfo
->options
));
1290 pageinfo
->num_options
= cupsAddOption("MediaType", doc
->ap_media_type
,
1291 pageinfo
->num_options
,
1292 &(pageinfo
->options
));
1293 pageinfo
->num_options
= cupsAddOption("PageRegion", doc
->ap_page_region
,
1294 pageinfo
->num_options
,
1295 &(pageinfo
->options
));
1296 pageinfo
->num_options
= cupsAddOption("PageSize", doc
->ap_page_size
,
1297 pageinfo
->num_options
,
1298 &(pageinfo
->options
));
1300 else if (doc
->page
== (Duplex
+ 2))
1303 * Second page/sheet gets default options...
1306 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->input_slot
,
1307 pageinfo
->num_options
,
1308 &(pageinfo
->options
));
1309 pageinfo
->num_options
= cupsAddOption("ManualFeed",
1310 doc
->input_slot
? "False" :
1312 pageinfo
->num_options
,
1313 &(pageinfo
->options
));
1314 pageinfo
->num_options
= cupsAddOption("MediaColor", doc
->media_color
,
1315 pageinfo
->num_options
,
1316 &(pageinfo
->options
));
1317 pageinfo
->num_options
= cupsAddOption("MediaType", doc
->media_type
,
1318 pageinfo
->num_options
,
1319 &(pageinfo
->options
));
1320 pageinfo
->num_options
= cupsAddOption("PageRegion", doc
->page_region
,
1321 pageinfo
->num_options
,
1322 &(pageinfo
->options
));
1323 pageinfo
->num_options
= cupsAddOption("PageSize", doc
->page_size
,
1324 pageinfo
->num_options
,
1325 &(pageinfo
->options
));
1330 * Scan comments until we see something other than %%Page*: or
1334 memcpy(bounding_box
, doc
->bounding_box
, sizeof(bounding_box
));
1336 while ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) > 0)
1338 if (!strncmp(line
, "%%PageBoundingBox:", 18))
1341 * %%PageBoundingBox: llx lly urx ury
1344 if (sscanf(line
+ 18, "%d%d%d%d", bounding_box
+ 0,
1345 bounding_box
+ 1, bounding_box
+ 2,
1346 bounding_box
+ 3) != 4)
1348 fputs("DEBUG: There was a bad %%PageBoundingBox: comment in the file.\n", stderr
);
1349 memcpy(bounding_box
, doc
->bounding_box
,
1350 sizeof(bounding_box
));
1352 else if (doc
->number_up
== 1 && !doc
->fit_to_page
&& Orientation
)
1354 int temp_bbox
[4]; /* Temporary bounding box */
1357 memcpy(temp_bbox
, bounding_box
, sizeof(temp_bbox
));
1359 fprintf(stderr
, "DEBUG: Orientation = %d\n", Orientation
);
1360 fprintf(stderr
, "DEBUG: original bounding_box = [ %d %d %d %d ]\n",
1361 bounding_box
[0], bounding_box
[1],
1362 bounding_box
[2], bounding_box
[3]);
1363 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
1364 PageWidth
, PageLength
);
1366 switch (Orientation
)
1368 case 1 : /* Landscape */
1369 bounding_box
[0] = (int)(PageLength
- temp_bbox
[3]);
1370 bounding_box
[1] = temp_bbox
[0];
1371 bounding_box
[2] = (int)(PageLength
- temp_bbox
[1]);
1372 bounding_box
[3] = temp_bbox
[2];
1375 case 2 : /* Reverse Portrait */
1376 bounding_box
[0] = (int)(PageWidth
- temp_bbox
[2]);
1377 bounding_box
[1] = (int)(PageLength
- temp_bbox
[3]);
1378 bounding_box
[2] = (int)(PageWidth
- temp_bbox
[0]);
1379 bounding_box
[3] = (int)(PageLength
- temp_bbox
[1]);
1382 case 3 : /* Reverse Landscape */
1383 bounding_box
[0] = temp_bbox
[1];
1384 bounding_box
[1] = (int)(PageWidth
- temp_bbox
[2]);
1385 bounding_box
[2] = temp_bbox
[3];
1386 bounding_box
[3] = (int)(PageWidth
- temp_bbox
[0]);
1390 fprintf(stderr
, "DEBUG: updated bounding_box = [ %d %d %d %d ]\n",
1391 bounding_box
[0], bounding_box
[1],
1392 bounding_box
[2], bounding_box
[3]);
1396 else if (!strncmp(line
, "%%PageCustomColors:", 19) ||
1397 !strncmp(line
, "%%PageMedia:", 12) ||
1398 !strncmp(line
, "%%PageOrientation:", 18) ||
1399 !strncmp(line
, "%%PageProcessColors:", 20) ||
1400 !strncmp(line
, "%%PageRequirements:", 18) ||
1401 !strncmp(line
, "%%PageResources:", 16))
1408 else if (!strncmp(line
, "%%PageCustomColors:", 19))
1411 * %%PageCustomColors: ...
1414 else if (!strncmp(line
, "%%PageMedia:", 12))
1420 else if (!strncmp(line
, "%%PageOrientation:", 18))
1423 * %%PageOrientation: ...
1426 else if (!strncmp(line
, "%%PageProcessColors:", 20))
1429 * %%PageProcessColors: ...
1432 else if (!strncmp(line
, "%%PageRequirements:", 18))
1435 * %%PageRequirements: ...
1438 else if (!strncmp(line
, "%%PageResources:", 16))
1441 * %%PageResources: ...
1444 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1447 * %%IncludeFeature: *MainKeyword OptionKeyword
1450 if (doc
->number_up
== 1 &&!doc
->fit_to_page
)
1451 pageinfo
->num_options
= include_feature(ppd
, line
,
1452 pageinfo
->num_options
,
1453 &(pageinfo
->options
));
1455 else if (!strncmp(line
, "%%BeginPageSetup", 16))
1464 if (doc
->number_up
== 1)
1467 * Update the document's composite and page bounding box...
1470 memcpy(pageinfo
->bounding_box
, bounding_box
,
1471 sizeof(pageinfo
->bounding_box
));
1473 if (bounding_box
[0] < doc
->new_bounding_box
[0])
1474 doc
->new_bounding_box
[0] = bounding_box
[0];
1475 if (bounding_box
[1] < doc
->new_bounding_box
[1])
1476 doc
->new_bounding_box
[1] = bounding_box
[1];
1477 if (bounding_box
[2] > doc
->new_bounding_box
[2])
1478 doc
->new_bounding_box
[2] = bounding_box
[2];
1479 if (bounding_box
[3] > doc
->new_bounding_box
[3])
1480 doc
->new_bounding_box
[3] = bounding_box
[3];
1484 * Output the page header as needed...
1487 if (!doc
->slow_order
&& first_page
)
1489 if (!ppd
|| !ppd
->num_filters
)
1490 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
1491 doc
->slow_collate
? 1 : doc
->copies
);
1493 if (doc
->number_up
> 1)
1495 printf("%%%%Page: (%d) %d\n", doc
->page
, doc
->page
);
1496 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1497 PageLeft
, PageBottom
, PageRight
, PageTop
);
1501 printf("%%%%Page: %s %d\n", pageinfo
->label
, doc
->page
);
1502 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1503 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
1504 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
1509 * Copy any page setup commands...
1513 doc_puts(doc
, "%%BeginPageSetup\n");
1517 int feature
= 0; /* In a Begin/EndFeature block? */
1519 while ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) > 0)
1521 if (!strncmp(line
, "%%EndPageSetup", 14))
1523 else if (!strncmp(line
, "%%BeginFeature:", 15))
1527 if (doc
->number_up
> 1 || doc
->fit_to_page
)
1530 else if (!strncmp(line
, "%%EndFeature", 12))
1534 if (doc
->number_up
> 1 || doc
->fit_to_page
)
1537 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1539 pageinfo
->num_options
= include_feature(ppd
, line
,
1540 pageinfo
->num_options
,
1541 &(pageinfo
->options
));
1544 else if (!strncmp(line
, "%%Include", 9))
1547 if (line
[0] != '%' && !feature
)
1550 if (!feature
|| (doc
->number_up
== 1 && !doc
->fit_to_page
))
1551 doc_write(doc
, line
, (size_t)linelen
);
1555 * Skip %%EndPageSetup...
1558 if (linelen
> 0 && !strncmp(line
, "%%EndPageSetup", 14))
1559 linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
);
1564 char *page_setup
; /* PageSetup commands to send */
1567 if (pageinfo
->num_options
> 0)
1568 write_options(doc
, ppd
, pageinfo
->num_options
, pageinfo
->options
);
1571 * Output commands for the current page...
1574 page_setup
= ppdEmitString(ppd
, PPD_ORDER_PAGE
, 0);
1578 doc_puts(doc
, page_setup
);
1584 * Prep for the start of the page description...
1587 start_nup(doc
, number
, 1, bounding_box
);
1590 doc_puts(doc
, "%%EndPageSetup\n");
1593 * Read the rest of the page description...
1601 (!strncmp(line
, "%%Page:", 7) ||
1602 !strncmp(line
, "%%Trailer", 9) ||
1603 !strncmp(line
, "%%EOF", 5)))
1605 else if (!strncmp(line
, "%%BeginDocument", 15) ||
1606 !strncmp(line
, "%ADO_BeginApplication", 21))
1608 doc_write(doc
, line
, (size_t)linelen
);
1612 else if ((!strncmp(line
, "%%EndDocument", 13) ||
1613 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
1615 doc_write(doc
, line
, (size_t)linelen
);
1619 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
1620 (!strncmp(line
, "%%BeginData:", 12) &&
1621 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
1624 * Copy binary data...
1627 int bytes
; /* Bytes of data */
1630 doc_write(doc
, line
, (size_t)linelen
);
1632 bytes
= atoi(strchr(line
, ':') + 1);
1636 if ((size_t)bytes
> linesize
)
1637 linelen
= cupsFileRead(fp
, line
, linesize
);
1639 linelen
= cupsFileRead(fp
, line
, (size_t)bytes
);
1644 perror("ERROR: Early end-of-file while reading binary data");
1648 doc_write(doc
, line
, (size_t)linelen
);
1654 doc_write(doc
, line
, (size_t)linelen
);
1656 while ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) > 0);
1659 * Finish up this page and return...
1662 end_nup(doc
, number
);
1664 pageinfo
->length
= (ssize_t
)(cupsFileTell(doc
->temp
) - pageinfo
->offset
);
1671 * 'copy_prolog()' - Copy the document prolog section.
1673 * This function expects "line" to be filled with a %%BeginProlog comment line.
1674 * On return, "line" will contain the next line in the file, if any.
1677 static ssize_t
/* O - Length of next line */
1678 copy_prolog(cups_file_t
*fp
, /* I - File to read from */
1679 pstops_doc_t
*doc
, /* I - Document info */
1680 ppd_file_t
*ppd
, /* I - PPD file */
1681 char *line
, /* I - Line buffer */
1682 ssize_t linelen
, /* I - Length of initial line */
1683 size_t linesize
) /* I - Size of line buffer */
1685 while (strncmp(line
, "%%BeginProlog", 13))
1687 if (!strncmp(line
, "%%BeginSetup", 12) || !strncmp(line
, "%%Page:", 7))
1690 doc_write(doc
, line
, (size_t)linelen
);
1692 if ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) == 0)
1696 doc_puts(doc
, "%%BeginProlog\n");
1698 do_prolog(doc
, ppd
);
1700 if (!strncmp(line
, "%%BeginProlog", 13))
1702 while ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) > 0)
1704 if (!strncmp(line
, "%%EndProlog", 11) ||
1705 !strncmp(line
, "%%BeginSetup", 12) ||
1706 !strncmp(line
, "%%Page:", 7))
1709 doc_write(doc
, line
, (size_t)linelen
);
1712 if (!strncmp(line
, "%%EndProlog", 11))
1713 linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
);
1715 fputs("DEBUG: The %%EndProlog comment is missing.\n", stderr
);
1718 doc_puts(doc
, "%%EndProlog\n");
1725 * 'copy_setup()' - Copy the document setup section.
1727 * This function expects "line" to be filled with a %%BeginSetup comment line.
1728 * On return, "line" will contain the next line in the file, if any.
1731 static ssize_t
/* O - Length of next line */
1732 copy_setup(cups_file_t
*fp
, /* I - File to read from */
1733 pstops_doc_t
*doc
, /* I - Document info */
1734 ppd_file_t
*ppd
, /* I - PPD file */
1735 char *line
, /* I - Line buffer */
1736 ssize_t linelen
, /* I - Length of initial line */
1737 size_t linesize
) /* I - Size of line buffer */
1739 int num_options
; /* Number of options */
1740 cups_option_t
*options
; /* Options */
1743 while (strncmp(line
, "%%BeginSetup", 12))
1745 if (!strncmp(line
, "%%Page:", 7))
1748 doc_write(doc
, line
, (size_t)linelen
);
1750 if ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) == 0)
1754 doc_puts(doc
, "%%BeginSetup\n");
1761 if (!strncmp(line
, "%%BeginSetup", 12))
1763 while (strncmp(line
, "%%EndSetup", 10))
1765 if (!strncmp(line
, "%%Page:", 7))
1767 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1770 * %%IncludeFeature: *MainKeyword OptionKeyword
1773 if (doc
->number_up
== 1 && !doc
->fit_to_page
)
1774 num_options
= include_feature(ppd
, line
, num_options
, &options
);
1776 else if (strncmp(line
, "%%BeginSetup", 12))
1777 doc_write(doc
, line
, (size_t)linelen
);
1779 if ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) == 0)
1783 if (!strncmp(line
, "%%EndSetup", 10))
1784 linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
);
1786 fputs("DEBUG: The %%EndSetup comment is missing.\n", stderr
);
1789 if (num_options
> 0)
1791 write_options(doc
, ppd
, num_options
, options
);
1792 cupsFreeOptions(num_options
, options
);
1795 doc_puts(doc
, "%%EndSetup\n");
1802 * 'copy_trailer()' - Copy the document trailer.
1804 * This function expects "line" to be filled with a %%Trailer comment line.
1805 * On return, "line" will contain the next line in the file, if any.
1808 static ssize_t
/* O - Length of next line */
1809 copy_trailer(cups_file_t
*fp
, /* I - File to read from */
1810 pstops_doc_t
*doc
, /* I - Document info */
1811 ppd_file_t
*ppd
, /* I - PPD file */
1812 int number
, /* I - Number of pages */
1813 char *line
, /* I - Line buffer */
1814 ssize_t linelen
, /* I - Length of initial line */
1815 size_t linesize
) /* I - Size of line buffer */
1818 * Write the trailer comments...
1827 if (!strncmp(line
, "%%EOF", 5))
1829 else if (strncmp(line
, "%%Trailer", 9) &&
1830 strncmp(line
, "%%Pages:", 8) &&
1831 strncmp(line
, "%%BoundingBox:", 14))
1832 fwrite(line
, 1, (size_t)linelen
, stdout
);
1834 linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
);
1837 fprintf(stderr
, "DEBUG: Wrote %d pages...\n", number
);
1839 printf("%%%%Pages: %d\n", number
);
1840 if (doc
->number_up
> 1 || doc
->fit_to_page
)
1841 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
1842 PageLeft
, PageBottom
, PageRight
, PageTop
);
1844 printf("%%%%BoundingBox: %d %d %d %d\n",
1845 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
1846 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
1853 * 'do_prolog()' - Send the necessary document prolog commands.
1857 do_prolog(pstops_doc_t
*doc
, /* I - Document information */
1858 ppd_file_t
*ppd
) /* I - PPD file */
1860 char *ps
; /* PS commands */
1864 * Send the document prolog commands...
1867 if (ppd
&& ppd
->patches
)
1869 doc_puts(doc
, "%%BeginFeature: *JobPatchFile 1\n");
1870 doc_puts(doc
, ppd
->patches
);
1871 doc_puts(doc
, "\n%%EndFeature\n");
1874 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_PROLOG
, 0.0)) != NULL
)
1881 * Define ESPshowpage here so that applications that define their
1882 * own procedure to do a showpage pick it up...
1885 if (doc
->use_ESPshowpage
)
1886 doc_puts(doc
, "userdict/ESPshowpage/showpage load put\n"
1887 "userdict/showpage{}put\n");
1892 * 'do_setup()' - Send the necessary document setup commands.
1896 do_setup(pstops_doc_t
*doc
, /* I - Document information */
1897 ppd_file_t
*ppd
) /* I - PPD file */
1899 char *ps
; /* PS commands */
1903 * Disable CTRL-D so that embedded files don't cause printing
1907 doc_puts(doc
, "% Disable CTRL-D as an end-of-file marker...\n");
1908 doc_puts(doc
, "userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put\n");
1911 * Mark job options...
1914 cupsMarkOptions(ppd
, doc
->num_options
, doc
->options
);
1917 * Send all the printer-specific setup commands...
1920 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, 0.0)) != NULL
)
1926 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_ANY
, 0.0)) != NULL
)
1933 * Set the number of copies for the job...
1936 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1938 doc_printf(doc
, "%%RBIBeginNonPPDFeature: *NumCopies %d\n", doc
->copies
);
1940 "%d/languagelevel where{pop languagelevel 2 ge}{false}ifelse\n"
1941 "{1 dict begin/NumCopies exch def currentdict end "
1943 "{userdict/#copies 3 -1 roll put}ifelse\n", doc
->copies
);
1944 doc_puts(doc
, "%RBIEndNonPPDFeature\n");
1948 * If we are doing N-up printing, disable setpagedevice...
1951 if (doc
->number_up
> 1)
1953 doc_puts(doc
, "userdict/CUPSsetpagedevice/setpagedevice load put\n");
1954 doc_puts(doc
, "userdict/setpagedevice{pop}bind put\n");
1958 * Make sure we have rectclip and rectstroke procedures of some sort...
1962 "% x y w h ESPrc - Clip to a rectangle.\n"
1963 "userdict/ESPrc/rectclip where{pop/rectclip load}\n"
1964 "{{newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
1965 "neg 0 rlineto closepath clip newpath}bind}ifelse put\n");
1968 "% x y w h ESPrf - Fill a rectangle.\n"
1969 "userdict/ESPrf/rectfill where{pop/rectfill load}\n"
1970 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
1971 "neg 0 rlineto closepath fill grestore}bind}ifelse put\n");
1974 "% x y w h ESPrs - Stroke a rectangle.\n"
1975 "userdict/ESPrs/rectstroke where{pop/rectstroke load}\n"
1976 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
1977 "neg 0 rlineto closepath stroke grestore}bind}ifelse put\n");
1980 * Write the page and label prologs...
1983 if (doc
->number_up
== 2 || doc
->number_up
== 6)
1986 * For 2- and 6-up output, rotate the labels to match the orientation
1990 if (Orientation
& 1)
1991 write_label_prolog(doc
, doc
->page_label
, PageBottom
,
1992 PageWidth
- PageLength
+ PageTop
, PageLength
);
1994 write_label_prolog(doc
, doc
->page_label
, PageLeft
, PageRight
,
1998 write_label_prolog(doc
, doc
->page_label
, PageBottom
, PageTop
, PageWidth
);
2003 * 'doc_printf()' - Send a formatted string to stdout and/or the temp file.
2005 * This function should be used for all page-level output that is affected
2006 * by ordering, collation, etc.
2010 doc_printf(pstops_doc_t
*doc
, /* I - Document information */
2011 const char *format
, /* I - Printf-style format string */
2012 ...) /* I - Additional arguments as needed */
2014 va_list ap
; /* Pointer to arguments */
2015 char buffer
[1024]; /* Output buffer */
2016 ssize_t bytes
; /* Number of bytes to write */
2019 va_start(ap
, format
);
2020 bytes
= vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
2023 if ((size_t)bytes
> sizeof(buffer
))
2025 _cupsLangPrintFilter(stderr
, "ERROR",
2026 _("Buffer overflow detected, aborting."));
2030 doc_write(doc
, buffer
, (size_t)bytes
);
2035 * 'doc_puts()' - Send a nul-terminated string to stdout and/or the temp file.
2037 * This function should be used for all page-level output that is affected
2038 * by ordering, collation, etc.
2042 doc_puts(pstops_doc_t
*doc
, /* I - Document information */
2043 const char *s
) /* I - String to send */
2045 doc_write(doc
, s
, strlen(s
));
2050 * 'doc_write()' - Send data to stdout and/or the temp file.
2054 doc_write(pstops_doc_t
*doc
, /* I - Document information */
2055 const char *s
, /* I - Data to send */
2056 size_t len
) /* I - Number of bytes to send */
2058 if (!doc
->slow_order
)
2059 fwrite(s
, 1, len
, stdout
);
2062 cupsFileWrite(doc
->temp
, s
, len
);
2067 * 'end_nup()' - End processing for N-up printing.
2071 end_nup(pstops_doc_t
*doc
, /* I - Document information */
2072 int number
) /* I - Page number */
2074 if (doc
->number_up
> 1)
2075 doc_puts(doc
, "userdict/ESPsave get restore\n");
2077 switch (doc
->number_up
)
2080 if (doc
->use_ESPshowpage
)
2082 write_labels(doc
, Orientation
);
2083 doc_puts(doc
, "ESPshowpage\n");
2089 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2091 if (Orientation
& 1)
2094 * Rotate the labels back to portrait...
2097 write_labels(doc
, Orientation
- 1);
2099 else if (Orientation
== 0)
2102 * Rotate the labels to landscape...
2105 write_labels(doc
, doc
->normal_landscape
? 1 : 3);
2110 * Rotate the labels to landscape...
2113 write_labels(doc
, doc
->normal_landscape
? 3 : 1);
2116 doc_puts(doc
, "ESPshowpage\n");
2121 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2123 write_labels(doc
, Orientation
);
2124 doc_puts(doc
, "ESPshowpage\n");
2134 * 'include_feature()' - Include a printer option/feature command.
2137 static int /* O - New number of options */
2139 ppd_file_t
*ppd
, /* I - PPD file */
2140 const char *line
, /* I - DSC line */
2141 int num_options
, /* I - Number of options */
2142 cups_option_t
**options
) /* IO - Options */
2144 char name
[255], /* Option name */
2145 value
[255]; /* Option value */
2146 ppd_option_t
*option
; /* Option in file */
2150 * Get the "%%IncludeFeature: *Keyword OptionKeyword" values...
2153 if (sscanf(line
+ 17, "%254s%254s", name
, value
) != 2)
2155 fputs("DEBUG: The %%IncludeFeature: comment is not valid.\n", stderr
);
2156 return (num_options
);
2160 * Find the option and choice...
2163 if ((option
= ppdFindOption(ppd
, name
+ 1)) == NULL
)
2165 _cupsLangPrintFilter(stderr
, "WARNING", _("Unknown option \"%s\"."),
2167 return (num_options
);
2170 if (option
->section
== PPD_ORDER_EXIT
||
2171 option
->section
== PPD_ORDER_JCL
)
2173 _cupsLangPrintFilter(stderr
, "WARNING",
2174 _("Option \"%s\" cannot be included via "
2175 "%%%%IncludeFeature."), name
+ 1);
2176 return (num_options
);
2179 if (!ppdFindChoice(option
, value
))
2181 _cupsLangPrintFilter(stderr
, "WARNING",
2182 _("Unknown choice \"%s\" for option \"%s\"."),
2184 return (num_options
);
2188 * Add the option to the option array and return...
2191 return (cupsAddOption(name
+ 1, value
, num_options
, options
));
2196 * 'parse_text()' - Parse a text value in a comment.
2198 * This function parses a DSC text value as defined on page 36 of the
2199 * DSC specification. Text values are either surrounded by parenthesis
2200 * or whitespace-delimited.
2202 * The value returned is the literal characters for the entire text
2203 * string, including any parenthesis and escape characters.
2206 static char * /* O - Value or NULL on error */
2207 parse_text(const char *start
, /* I - Start of text value */
2208 char **end
, /* O - End of text value */
2209 char *buffer
, /* I - Buffer */
2210 size_t bufsize
) /* I - Size of buffer */
2212 char *bufptr
, /* Pointer in buffer */
2213 *bufend
; /* End of buffer */
2214 int level
; /* Parenthesis level */
2218 * Skip leading whitespace...
2221 while (isspace(*start
& 255))
2225 * Then copy the value...
2230 bufend
= buffer
+ bufsize
- 1;
2232 while (bufptr
< bufend
)
2234 if (isspace(*start
& 255) && !level
)
2241 else if (*start
== ')')
2251 else if (*start
== '\\')
2254 * Copy escaped character...
2257 int i
; /* Looping var */
2261 i
<= 3 && isdigit(start
[i
] & 255) && bufptr
< bufend
;
2262 *bufptr
++ = start
[i
], i
++);
2271 * Return the value and new pointer into the line...
2275 *end
= (char *)start
;
2277 if (bufptr
== bufend
)
2285 * 'set_pstops_options()' - Set pstops options.
2290 pstops_doc_t
*doc
, /* I - Document information */
2291 ppd_file_t
*ppd
, /* I - PPD file */
2292 char *argv
[], /* I - Command-line arguments */
2293 int num_options
, /* I - Number of options */
2294 cups_option_t
*options
) /* I - Options */
2296 const char *val
; /* Option value */
2297 int intval
; /* Integer option value */
2298 ppd_attr_t
*attr
; /* PPD attribute */
2299 ppd_option_t
*option
; /* PPD option */
2300 ppd_choice_t
*choice
; /* PPD choice */
2301 const char *content_type
; /* Original content type */
2302 int max_copies
; /* Maximum number of copies supported */
2306 * Initialize document information structure...
2309 memset(doc
, 0, sizeof(pstops_doc_t
));
2311 doc
->job_id
= atoi(argv
[1]);
2312 doc
->user
= argv
[2];
2313 doc
->title
= argv
[3];
2314 doc
->copies
= atoi(argv
[4]);
2316 if (ppd
&& ppd
->landscape
> 0)
2317 doc
->normal_landscape
= 1;
2319 doc
->bounding_box
[0] = (int)PageLeft
;
2320 doc
->bounding_box
[1] = (int)PageBottom
;
2321 doc
->bounding_box
[2] = (int)PageRight
;
2322 doc
->bounding_box
[3] = (int)PageTop
;
2324 doc
->new_bounding_box
[0] = INT_MAX
;
2325 doc
->new_bounding_box
[1] = INT_MAX
;
2326 doc
->new_bounding_box
[2] = INT_MIN
;
2327 doc
->new_bounding_box
[3] = INT_MIN
;
2330 * AP_FIRSTPAGE_* and the corresponding non-first-page options.
2333 doc
->ap_input_slot
= cupsGetOption("AP_FIRSTPAGE_InputSlot", num_options
,
2335 doc
->ap_manual_feed
= cupsGetOption("AP_FIRSTPAGE_ManualFeed", num_options
,
2337 doc
->ap_media_color
= cupsGetOption("AP_FIRSTPAGE_MediaColor", num_options
,
2339 doc
->ap_media_type
= cupsGetOption("AP_FIRSTPAGE_MediaType", num_options
,
2341 doc
->ap_page_region
= cupsGetOption("AP_FIRSTPAGE_PageRegion", num_options
,
2343 doc
->ap_page_size
= cupsGetOption("AP_FIRSTPAGE_PageSize", num_options
,
2346 if ((choice
= ppdFindMarkedChoice(ppd
, "InputSlot")) != NULL
)
2347 doc
->input_slot
= choice
->choice
;
2348 if ((choice
= ppdFindMarkedChoice(ppd
, "ManualFeed")) != NULL
)
2349 doc
->manual_feed
= choice
->choice
;
2350 if ((choice
= ppdFindMarkedChoice(ppd
, "MediaColor")) != NULL
)
2351 doc
->media_color
= choice
->choice
;
2352 if ((choice
= ppdFindMarkedChoice(ppd
, "MediaType")) != NULL
)
2353 doc
->media_type
= choice
->choice
;
2354 if ((choice
= ppdFindMarkedChoice(ppd
, "PageRegion")) != NULL
)
2355 doc
->page_region
= choice
->choice
;
2356 if ((choice
= ppdFindMarkedChoice(ppd
, "PageSize")) != NULL
)
2357 doc
->page_size
= choice
->choice
;
2360 * collate, multiple-document-handling
2363 if ((val
= cupsGetOption("multiple-document-handling", num_options
, options
)) != NULL
)
2366 * This IPP attribute is unnecessarily complicated...
2368 * single-document, separate-documents-collated-copies, and
2369 * single-document-new-sheet all require collated copies.
2371 * separate-documents-uncollated-copies allows for uncollated copies.
2374 doc
->collate
= _cups_strcasecmp(val
, "separate-documents-uncollated-copies") != 0;
2377 if ((val
= cupsGetOption("Collate", num_options
, options
)) != NULL
&&
2378 (!_cups_strcasecmp(val
, "true") ||!_cups_strcasecmp(val
, "on") ||
2379 !_cups_strcasecmp(val
, "yes")))
2386 if ((val
= cupsGetOption("emit-jcl", num_options
, options
)) != NULL
&&
2387 (!_cups_strcasecmp(val
, "false") || !_cups_strcasecmp(val
, "off") ||
2388 !_cups_strcasecmp(val
, "no") || !strcmp(val
, "0")))
2394 * fit-to-page/ipp-attribute-fidelity
2396 * (Only for original PostScript content)
2399 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
2400 content_type
= "application/postscript";
2402 if (!_cups_strcasecmp(content_type
, "application/postscript"))
2404 if ((val
= cupsGetOption("fit-to-page", num_options
, options
)) != NULL
&&
2405 !_cups_strcasecmp(val
, "true"))
2406 doc
->fit_to_page
= 1;
2407 else if ((val
= cupsGetOption("ipp-attribute-fidelity", num_options
,
2408 options
)) != NULL
&&
2409 !_cups_strcasecmp(val
, "true"))
2410 doc
->fit_to_page
= 1;
2414 * mirror/MirrorPrint
2417 if ((choice
= ppdFindMarkedChoice(ppd
, "MirrorPrint")) != NULL
)
2419 val
= choice
->choice
;
2423 val
= cupsGetOption("mirror", num_options
, options
);
2425 if (val
&& (!_cups_strcasecmp(val
, "true") || !_cups_strcasecmp(val
, "on") ||
2426 !_cups_strcasecmp(val
, "yes")))
2433 if ((val
= cupsGetOption("number-up", num_options
, options
)) != NULL
)
2435 switch (intval
= atoi(val
))
2443 doc
->number_up
= intval
;
2446 _cupsLangPrintFilter(stderr
, "ERROR",
2447 _("Unsupported number-up value %d, using "
2448 "number-up=1."), intval
);
2460 if ((val
= cupsGetOption("number-up-layout", num_options
, options
)) != NULL
)
2462 if (!_cups_strcasecmp(val
, "lrtb"))
2463 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2464 else if (!_cups_strcasecmp(val
, "lrbt"))
2465 doc
->number_up_layout
= PSTOPS_LAYOUT_LRBT
;
2466 else if (!_cups_strcasecmp(val
, "rltb"))
2467 doc
->number_up_layout
= PSTOPS_LAYOUT_RLTB
;
2468 else if (!_cups_strcasecmp(val
, "rlbt"))
2469 doc
->number_up_layout
= PSTOPS_LAYOUT_RLBT
;
2470 else if (!_cups_strcasecmp(val
, "tblr"))
2471 doc
->number_up_layout
= PSTOPS_LAYOUT_TBLR
;
2472 else if (!_cups_strcasecmp(val
, "tbrl"))
2473 doc
->number_up_layout
= PSTOPS_LAYOUT_TBRL
;
2474 else if (!_cups_strcasecmp(val
, "btlr"))
2475 doc
->number_up_layout
= PSTOPS_LAYOUT_BTLR
;
2476 else if (!_cups_strcasecmp(val
, "btrl"))
2477 doc
->number_up_layout
= PSTOPS_LAYOUT_BTRL
;
2480 _cupsLangPrintFilter(stderr
, "ERROR",
2481 _("Unsupported number-up-layout value %s, using "
2482 "number-up-layout=lrtb."), val
);
2483 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2487 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2493 if ((val
= cupsGetOption("OutputOrder", num_options
, options
)) != NULL
)
2495 if (!_cups_strcasecmp(val
, "Reverse"))
2496 doc
->output_order
= 1;
2501 * Figure out the right default output order from the PPD file...
2504 if ((choice
= ppdFindMarkedChoice(ppd
, "OutputBin")) != NULL
&&
2505 (attr
= ppdFindAttr(ppd
, "PageStackOrder", choice
->choice
)) != NULL
&&
2507 doc
->output_order
= !_cups_strcasecmp(attr
->value
, "Reverse");
2508 else if ((attr
= ppdFindAttr(ppd
, "DefaultOutputOrder", NULL
)) != NULL
&&
2510 doc
->output_order
= !_cups_strcasecmp(attr
->value
, "Reverse");
2517 if ((val
= cupsGetOption("page-border", num_options
, options
)) != NULL
)
2519 if (!_cups_strcasecmp(val
, "none"))
2520 doc
->page_border
= PSTOPS_BORDERNONE
;
2521 else if (!_cups_strcasecmp(val
, "single"))
2522 doc
->page_border
= PSTOPS_BORDERSINGLE
;
2523 else if (!_cups_strcasecmp(val
, "single-thick"))
2524 doc
->page_border
= PSTOPS_BORDERSINGLE2
;
2525 else if (!_cups_strcasecmp(val
, "double"))
2526 doc
->page_border
= PSTOPS_BORDERDOUBLE
;
2527 else if (!_cups_strcasecmp(val
, "double-thick"))
2528 doc
->page_border
= PSTOPS_BORDERDOUBLE2
;
2531 _cupsLangPrintFilter(stderr
, "ERROR",
2532 _("Unsupported page-border value %s, using "
2533 "page-border=none."), val
);
2534 doc
->page_border
= PSTOPS_BORDERNONE
;
2538 doc
->page_border
= PSTOPS_BORDERNONE
;
2544 doc
->page_label
= cupsGetOption("page-label", num_options
, options
);
2550 doc
->page_ranges
= cupsGetOption("page-ranges", num_options
, options
);
2556 doc
->page_set
= cupsGetOption("page-set", num_options
, options
);
2559 * Now figure out if we have to force collated copies, etc.
2562 if ((attr
= ppdFindAttr(ppd
, "cupsMaxCopies", NULL
)) != NULL
)
2563 max_copies
= atoi(attr
->value
);
2564 else if (ppd
&& ppd
->manual_copies
)
2569 if (doc
->copies
> max_copies
)
2571 else if (ppd
&& ppd
->manual_copies
&& Duplex
&& doc
->copies
> 1)
2574 * Force collated copies when printing a duplexed document to
2575 * a non-PS printer that doesn't do hardware copy generation.
2576 * Otherwise the copies will end up on the front/back side of
2584 * See if we have to filter the fast or slow way...
2587 if (doc
->collate
&& doc
->copies
> 1)
2590 * See if we need to manually collate the pages...
2593 doc
->slow_collate
= 1;
2595 if (doc
->copies
<= max_copies
&&
2596 (choice
= ppdFindMarkedChoice(ppd
, "Collate")) != NULL
&&
2597 !_cups_strcasecmp(choice
->choice
, "True"))
2600 * Hardware collate option is selected, see if the option is
2601 * conflicting - if not, collate in hardware. Otherwise,
2602 * turn the hardware collate option off...
2605 if ((option
= ppdFindOption(ppd
, "Collate")) != NULL
&&
2606 !option
->conflicted
)
2607 doc
->slow_collate
= 0;
2609 ppdMarkOption(ppd
, "Collate", "False");
2613 doc
->slow_collate
= 0;
2615 if (!ppdFindOption(ppd
, "OutputOrder") && doc
->output_order
)
2616 doc
->slow_order
= 1;
2618 doc
->slow_order
= 0;
2621 (doc
->slow_collate
|| doc
->slow_order
||
2622 ((attr
= ppdFindAttr(ppd
, "cupsEvenDuplex", NULL
)) != NULL
&&
2623 attr
->value
&& !_cups_strcasecmp(attr
->value
, "true"))))
2624 doc
->slow_duplex
= 1;
2626 doc
->slow_duplex
= 0;
2629 * Create a temporary file for page data if we need to filter slowly...
2632 if (doc
->slow_order
|| doc
->slow_collate
)
2634 if ((doc
->temp
= cupsTempFile2(doc
->tempfile
,
2635 sizeof(doc
->tempfile
))) == NULL
)
2637 perror("DEBUG: Unable to create temporary file");
2643 * Figure out if we should use ESPshowpage or not...
2646 if (doc
->page_label
|| getenv("CLASSIFICATION") || doc
->number_up
> 1 ||
2650 * Yes, use ESPshowpage...
2653 doc
->use_ESPshowpage
= 1;
2656 fprintf(stderr
, "DEBUG: slow_collate=%d, slow_duplex=%d, slow_order=%d\n",
2657 doc
->slow_collate
, doc
->slow_duplex
, doc
->slow_order
);
2662 * 'skip_page()' - Skip past a page that won't be printed.
2665 static ssize_t
/* O - Length of next line */
2666 skip_page(cups_file_t
*fp
, /* I - File to read from */
2667 char *line
, /* I - Line buffer */
2668 ssize_t linelen
, /* I - Length of initial line */
2669 size_t linesize
) /* I - Size of line buffer */
2671 int level
; /* Embedded document level */
2676 while ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) > 0)
2679 (!strncmp(line
, "%%Page:", 7) || !strncmp(line
, "%%Trailer", 9)))
2681 else if (!strncmp(line
, "%%BeginDocument", 15) ||
2682 !strncmp(line
, "%ADO_BeginApplication", 21))
2684 else if ((!strncmp(line
, "%%EndDocument", 13) ||
2685 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
2687 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
2688 (!strncmp(line
, "%%BeginData:", 12) &&
2689 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
2692 * Skip binary data...
2695 ssize_t bytes
; /* Bytes of data */
2697 bytes
= atoi(strchr(line
, ':') + 1);
2701 if ((size_t)bytes
> linesize
)
2702 linelen
= (ssize_t
)cupsFileRead(fp
, line
, linesize
);
2704 linelen
= (ssize_t
)cupsFileRead(fp
, line
, (size_t)bytes
);
2709 perror("ERROR: Early end-of-file while reading binary data");
2723 * 'start_nup()' - Start processing for N-up printing.
2727 start_nup(pstops_doc_t
*doc
, /* I - Document information */
2728 int number
, /* I - Page number */
2729 int show_border
, /* I - Show the border? */
2730 const int *bounding_box
) /* I - BoundingBox value */
2732 int pos
; /* Position on page */
2733 int x
, y
; /* Relative position of subpage */
2734 double w
, l
, /* Width and length of subpage */
2735 tx
, ty
; /* Translation values for subpage */
2736 double pagew
, /* Printable width of page */
2737 pagel
; /* Printable height of page */
2738 int bboxx
, /* BoundingBox X origin */
2739 bboxy
, /* BoundingBox Y origin */
2740 bboxw
, /* BoundingBox width */
2741 bboxl
; /* BoundingBox height */
2742 double margin
= 0; /* Current margin for border */
2745 if (doc
->number_up
> 1)
2746 doc_puts(doc
, "userdict/ESPsave save put\n");
2748 pos
= (number
- 1) % doc
->number_up
;
2749 pagew
= PageRight
- PageLeft
;
2750 pagel
= PageTop
- PageBottom
;
2752 if (doc
->fit_to_page
)
2754 bboxx
= bounding_box
[0];
2755 bboxy
= bounding_box
[1];
2756 bboxw
= bounding_box
[2] - bounding_box
[0];
2757 bboxl
= bounding_box
[3] - bounding_box
[1];
2763 bboxw
= (int)PageWidth
;
2764 bboxl
= (int)PageLength
;
2767 fprintf(stderr
, "DEBUG: pagew = %.1f, pagel = %.1f\n", pagew
, pagel
);
2768 fprintf(stderr
, "DEBUG: bboxx = %d, bboxy = %d, bboxw = %d, bboxl = %d\n",
2769 bboxx
, bboxy
, bboxw
, bboxl
);
2770 fprintf(stderr
, "DEBUG: PageLeft = %.1f, PageRight = %.1f\n",
2771 PageLeft
, PageRight
);
2772 fprintf(stderr
, "DEBUG: PageTop = %.1f, PageBottom = %.1f\n",
2773 PageTop
, PageBottom
);
2774 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
2775 PageWidth
, PageLength
);
2777 switch (Orientation
)
2779 case 1 : /* Landscape */
2780 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", PageLength
);
2782 case 2 : /* Reverse Portrait */
2783 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", PageWidth
,
2786 case 3 : /* Reverse Landscape */
2787 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", PageWidth
);
2792 * Mirror the page as needed...
2796 doc_printf(doc
, "%.1f 0.0 translate -1 1 scale\n", PageWidth
);
2799 * Offset and scale as necessary for fit_to_page/fit-to-page/number-up...
2802 if (Duplex
&& doc
->number_up
> 1 && ((number
/ doc
->number_up
) & 1))
2803 doc_printf(doc
, "%.1f %.1f translate\n", PageWidth
- PageRight
, PageBottom
);
2804 else if (doc
->number_up
> 1 || doc
->fit_to_page
)
2805 doc_printf(doc
, "%.1f %.1f translate\n", PageLeft
, PageBottom
);
2807 switch (doc
->number_up
)
2810 if (doc
->fit_to_page
)
2813 l
= w
* bboxl
/ bboxw
;
2818 w
= l
* bboxw
/ bboxl
;
2821 tx
= 0.5 * (pagew
- w
);
2822 ty
= 0.5 * (pagel
- l
);
2824 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n", tx
, ty
,
2825 w
/ bboxw
, l
/ bboxl
);
2832 if (Orientation
& 1)
2836 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2840 l
= w
* bboxl
/ bboxw
;
2842 if (l
> (pagew
* 0.5))
2845 w
= l
* bboxw
/ bboxl
;
2848 tx
= 0.5 * (pagew
* 0.5 - l
);
2849 ty
= 0.5 * (pagel
- w
);
2851 if (doc
->normal_landscape
)
2852 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2854 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2856 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2857 ty
, tx
+ pagew
* 0.5 * x
, w
/ bboxw
, l
/ bboxl
);
2863 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2867 w
= l
* bboxw
/ bboxl
;
2869 if (w
> (pagel
* 0.5))
2872 l
= w
* bboxl
/ bboxw
;
2875 tx
= 0.5 * (pagel
* 0.5 - w
);
2876 ty
= 0.5 * (pagew
- l
);
2878 if (doc
->normal_landscape
)
2879 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2881 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2883 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2884 tx
+ pagel
* 0.5 * x
, ty
, w
/ bboxw
, l
/ bboxl
);
2889 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2900 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2903 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2907 l
= w
* bboxl
/ bboxw
;
2909 if (l
> (pagel
* 0.5))
2912 w
= l
* bboxw
/ bboxl
;
2915 tx
= 0.5 * (pagew
* 0.5 - w
);
2916 ty
= 0.5 * (pagel
* 0.5 - l
);
2918 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2919 tx
+ x
* pagew
* 0.5, ty
+ y
* pagel
* 0.5,
2920 w
/ bboxw
, l
/ bboxl
);
2924 if (Orientation
& 1)
2926 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2931 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2934 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2942 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2945 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2950 l
= w
* bboxl
/ bboxw
;
2952 if (l
> (pagew
* 0.333))
2955 w
= l
* bboxw
/ bboxl
;
2958 tx
= 0.5 * (pagel
- 2 * w
);
2959 ty
= 0.5 * (pagew
- 3 * l
);
2961 if (doc
->normal_landscape
)
2962 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
2964 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
2966 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2967 tx
+ x
* w
, ty
+ y
* l
, l
/ bboxl
, w
/ bboxw
);
2971 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2976 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2979 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2987 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2990 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2995 w
= l
* bboxw
/ bboxl
;
2997 if (w
> (pagel
* 0.333))
3000 l
= w
* bboxl
/ bboxw
;
3003 tx
= 0.5 * (pagel
- 3 * w
);
3004 ty
= 0.5 * (pagew
- 2 * l
);
3006 if (doc
->normal_landscape
)
3007 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
3009 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
3011 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3012 tx
+ w
* x
, ty
+ l
* y
, w
/ bboxw
, l
/ bboxl
);
3018 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3029 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3032 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3036 l
= w
* bboxl
/ bboxw
;
3038 if (l
> (pagel
* 0.333))
3041 w
= l
* bboxw
/ bboxl
;
3044 tx
= 0.5 * (pagew
* 0.333 - w
);
3045 ty
= 0.5 * (pagel
* 0.333 - l
);
3047 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3048 tx
+ x
* pagew
* 0.333, ty
+ y
* pagel
* 0.333,
3049 w
/ bboxw
, l
/ bboxl
);
3053 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3064 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3067 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3071 l
= w
* bboxl
/ bboxw
;
3073 if (l
> (pagel
* 0.25))
3076 w
= l
* bboxw
/ bboxl
;
3079 tx
= 0.5 * (pagew
* 0.25 - w
);
3080 ty
= 0.5 * (pagel
* 0.25 - l
);
3082 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3083 tx
+ x
* pagew
* 0.25, ty
+ y
* pagel
* 0.25,
3084 w
/ bboxw
, l
/ bboxl
);
3089 * Draw borders as necessary...
3092 if (doc
->page_border
&& show_border
)
3094 int rects
; /* Number of border rectangles */
3095 double fscale
; /* Scaling value for points */
3098 rects
= (doc
->page_border
& PSTOPS_BORDERDOUBLE
) ? 2 : 1;
3099 fscale
= PageWidth
/ w
;
3100 margin
= 2.25 * fscale
;
3103 * Set the line width and color...
3106 doc_puts(doc
, "gsave\n");
3107 doc_printf(doc
, "%.3f setlinewidth 0 setgray newpath\n",
3108 (doc
->page_border
& PSTOPS_BORDERTHICK
) ? 0.5 * fscale
:
3112 * Draw border boxes...
3115 for (; rects
> 0; rects
--, margin
+= 2 * fscale
)
3116 if (doc
->number_up
> 1)
3117 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3121 bboxl
- 2 * margin
);
3123 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3125 PageBottom
+ margin
,
3126 PageRight
- PageLeft
- 2 * margin
,
3127 PageTop
- PageBottom
- 2 * margin
);
3130 * Restore pen settings...
3133 doc_puts(doc
, "grestore\n");
3136 if (doc
->fit_to_page
)
3139 * Offset the page by its bounding box...
3142 doc_printf(doc
, "%d %d translate\n", -bounding_box
[0],
3146 if (doc
->fit_to_page
|| doc
->number_up
> 1)
3149 * Clip the page to the page's bounding box...
3152 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrc\n",
3153 bboxx
+ margin
, bboxy
+ margin
,
3154 bboxw
- 2 * margin
, bboxl
- 2 * margin
);
3160 * 'write_label_prolog()' - Write the prolog with the classification
3165 write_label_prolog(pstops_doc_t
*doc
, /* I - Document info */
3166 const char *label
, /* I - Page label */
3167 float bottom
, /* I - Bottom position in points */
3168 float top
, /* I - Top position in points */
3169 float width
) /* I - Width in points */
3171 const char *classification
; /* CLASSIFICATION environment variable */
3172 const char *ptr
; /* Temporary string pointer */
3176 * First get the current classification...
3179 if ((classification
= getenv("CLASSIFICATION")) == NULL
)
3180 classification
= "";
3181 if (strcmp(classification
, "none") == 0)
3182 classification
= "";
3185 * If there is nothing to show, bind an empty 'write labels' procedure
3189 if (!classification
[0] && (label
== NULL
|| !label
[0]))
3191 doc_puts(doc
, "userdict/ESPwl{}bind put\n");
3196 * Set the classification + page label string...
3199 doc_puts(doc
, "userdict");
3200 if (!strcmp(classification
, "confidential"))
3201 doc_puts(doc
, "/ESPpl(CONFIDENTIAL");
3202 else if (!strcmp(classification
, "classified"))
3203 doc_puts(doc
, "/ESPpl(CLASSIFIED");
3204 else if (!strcmp(classification
, "secret"))
3205 doc_puts(doc
, "/ESPpl(SECRET");
3206 else if (!strcmp(classification
, "topsecret"))
3207 doc_puts(doc
, "/ESPpl(TOP SECRET");
3208 else if (!strcmp(classification
, "unclassified"))
3209 doc_puts(doc
, "/ESPpl(UNCLASSIFIED");
3212 doc_puts(doc
, "/ESPpl(");
3214 for (ptr
= classification
; *ptr
; ptr
++)
3216 if (*ptr
< 32 || *ptr
> 126)
3217 doc_printf(doc
, "\\%03o", *ptr
);
3218 else if (*ptr
== '_')
3220 else if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
3221 doc_printf(doc
, "\\%c", *ptr
);
3223 doc_printf(doc
, "%c", *ptr
);
3229 if (classification
[0])
3230 doc_puts(doc
, " - ");
3233 * Quote the label string as needed...
3236 for (ptr
= label
; *ptr
; ptr
++)
3238 if (*ptr
< 32 || *ptr
> 126)
3239 doc_printf(doc
, "\\%03o", *ptr
);
3240 else if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
3241 doc_printf(doc
, "\\%c", *ptr
);
3243 doc_printf(doc
, "%c", *ptr
);
3247 doc_puts(doc
, ")put\n");
3250 * Then get a 14 point Helvetica-Bold font...
3253 doc_puts(doc
, "userdict/ESPpf /Helvetica-Bold findfont 14 scalefont put\n");
3256 * Finally, the procedure to write the labels on the page...
3259 doc_puts(doc
, "userdict/ESPwl{\n");
3260 doc_puts(doc
, " ESPpf setfont\n");
3261 doc_printf(doc
, " ESPpl stringwidth pop dup 12 add exch -0.5 mul %.0f add\n",
3263 doc_puts(doc
, " 1 setgray\n");
3264 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrf\n", bottom
- 2.0);
3265 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrf\n", top
- 18.0);
3266 doc_puts(doc
, " 0 setgray\n");
3267 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrs\n", bottom
- 2.0);
3268 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrs\n", top
- 18.0);
3269 doc_printf(doc
, " dup %.0f moveto ESPpl show\n", bottom
+ 2.0);
3270 doc_printf(doc
, " %.0f moveto ESPpl show\n", top
- 14.0);
3271 doc_puts(doc
, "pop\n");
3272 doc_puts(doc
, "}bind put\n");
3277 * 'write_labels()' - Write the actual page labels.
3279 * This function is a copy of the one in common.c since we need to
3280 * use doc_puts/doc_printf instead of puts/printf...
3284 write_labels(pstops_doc_t
*doc
, /* I - Document information */
3285 int orient
) /* I - Orientation of the page */
3287 float width
, /* Width of page */
3288 length
; /* Length of page */
3291 doc_puts(doc
, "gsave\n");
3293 if ((orient
^ Orientation
) & 1)
3301 length
= PageLength
;
3306 case 1 : /* Landscape */
3307 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", length
);
3309 case 2 : /* Reverse Portrait */
3310 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", width
, length
);
3312 case 3 : /* Reverse Landscape */
3313 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", width
);
3317 doc_puts(doc
, "ESPwl\n");
3318 doc_puts(doc
, "grestore\n");
3323 * 'write_options()' - Write options provided via %%IncludeFeature.
3328 pstops_doc_t
*doc
, /* I - Document */
3329 ppd_file_t
*ppd
, /* I - PPD file */
3330 int num_options
, /* I - Number of options */
3331 cups_option_t
*options
) /* I - Options */
3333 int i
; /* Looping var */
3334 ppd_option_t
*option
; /* PPD option */
3335 float min_order
; /* Minimum OrderDependency value */
3336 char *doc_setup
, /* DocumentSetup commands to send */
3337 *any_setup
; /* AnySetup commands to send */
3341 * Figure out the minimum OrderDependency value...
3344 if ((option
= ppdFindOption(ppd
, "PageRegion")) != NULL
)
3345 min_order
= option
->order
;
3349 for (i
= 0; i
< num_options
; i
++)
3350 if ((option
= ppdFindOption(ppd
, options
[i
].name
)) != NULL
&&
3351 option
->order
< min_order
)
3352 min_order
= option
->order
;
3355 * Mark and extract them...
3358 cupsMarkOptions(ppd
, num_options
, options
);
3360 doc_setup
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, min_order
);
3361 any_setup
= ppdEmitString(ppd
, PPD_ORDER_ANY
, min_order
);
3364 * Then send them out...
3367 if (doc
->number_up
> 1)
3370 * Temporarily restore setpagedevice so we can set the options...
3373 doc_puts(doc
, "userdict/setpagedevice/CUPSsetpagedevice load put\n");
3378 doc_puts(doc
, doc_setup
);
3384 doc_puts(doc
, any_setup
);
3388 if (doc
->number_up
> 1)
3391 * Disable setpagedevice again...
3394 doc_puts(doc
, "userdict/setpagedevice{pop}bind put\n");