]>
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 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
23 #include <cups/file.h>
24 #include <cups/array.h>
25 #include <cups/language-private.h>
33 #define PSTOPS_BORDERNONE 0 /* No border or hairline border */
34 #define PSTOPS_BORDERTHICK 1 /* Think border */
35 #define PSTOPS_BORDERSINGLE 2 /* Single-line hairline border */
36 #define PSTOPS_BORDERSINGLE2 3 /* Single-line thick border */
37 #define PSTOPS_BORDERDOUBLE 4 /* Double-line hairline border */
38 #define PSTOPS_BORDERDOUBLE2 5 /* Double-line thick border */
40 #define PSTOPS_LAYOUT_LRBT 0 /* Left to right, bottom to top */
41 #define PSTOPS_LAYOUT_LRTB 1 /* Left to right, top to bottom */
42 #define PSTOPS_LAYOUT_RLBT 2 /* Right to left, bottom to top */
43 #define PSTOPS_LAYOUT_RLTB 3 /* Right to left, top to bottom */
44 #define PSTOPS_LAYOUT_BTLR 4 /* Bottom to top, left to right */
45 #define PSTOPS_LAYOUT_TBLR 5 /* Top to bottom, left to right */
46 #define PSTOPS_LAYOUT_BTRL 6 /* Bottom to top, right to left */
47 #define PSTOPS_LAYOUT_TBRL 7 /* Top to bottom, right to left */
49 #define PSTOPS_LAYOUT_NEGATEY 1 /* The bits for the layout */
50 #define PSTOPS_LAYOUT_NEGATEX 2 /* definitions above... */
51 #define PSTOPS_LAYOUT_VERTICAL 4
58 typedef struct /**** Page information ****/
60 char *label
; /* Page label */
61 int bounding_box
[4]; /* PageBoundingBox */
62 off_t offset
; /* Offset to start of page */
63 ssize_t length
; /* Number of bytes for page */
64 int num_options
; /* Number of options for this page */
65 cups_option_t
*options
; /* Options for this page */
68 typedef struct /**** Document information ****/
70 int page
; /* Current page */
71 int bounding_box
[4]; /* BoundingBox from header */
72 int new_bounding_box
[4]; /* New composite bounding box */
73 int num_options
; /* Number of document-wide options */
74 cups_option_t
*options
; /* Document-wide options */
75 int normal_landscape
, /* Normal rotation for landscape? */
76 saw_eof
, /* Saw the %%EOF comment? */
77 slow_collate
, /* Collate copies by hand? */
78 slow_duplex
, /* Duplex pages slowly? */
79 slow_order
, /* Reverse pages slowly? */
80 use_ESPshowpage
; /* Use ESPshowpage? */
81 cups_array_t
*pages
; /* Pages in document */
82 cups_file_t
*temp
; /* Temporary file, if any */
83 char tempfile
[1024]; /* Temporary filename */
84 int job_id
; /* Job ID */
85 const char *user
, /* User name */
86 *title
; /* Job name */
87 int copies
; /* Number of copies */
88 const char *ap_input_slot
, /* AP_FIRSTPAGE_InputSlot value */
89 *ap_manual_feed
, /* AP_FIRSTPAGE_ManualFeed value */
90 *ap_media_color
, /* AP_FIRSTPAGE_MediaColor value */
91 *ap_media_type
, /* AP_FIRSTPAGE_MediaType value */
92 *ap_page_region
, /* AP_FIRSTPAGE_PageRegion value */
93 *ap_page_size
; /* AP_FIRSTPAGE_PageSize value */
94 int collate
, /* Collate copies? */
95 emit_jcl
, /* Emit JCL commands? */
96 fit_to_page
; /* Fit pages to media */
97 const char *input_slot
, /* InputSlot value */
98 *manual_feed
, /* ManualFeed value */
99 *media_color
, /* MediaColor value */
100 *media_type
, /* MediaType value */
101 *page_region
, /* PageRegion value */
102 *page_size
; /* PageSize value */
103 int mirror
, /* doc->mirror/mirror pages */
104 number_up
, /* Number of pages on each sheet */
105 number_up_layout
, /* doc->number_up_layout of N-up pages */
106 output_order
, /* Requested reverse output order? */
107 page_border
; /* doc->page_border around pages */
108 const char *page_label
, /* page-label option, if any */
109 *page_ranges
, /* page-ranges option, if any */
110 *page_set
; /* page-set option, if any */
115 * Convenience macros...
118 #define is_first_page(p) (doc->number_up == 1 || \
119 ((p) % doc->number_up) == 1)
120 #define is_last_page(p) (doc->number_up == 1 || \
121 ((p) % doc->number_up) == 0)
122 #define is_not_last_page(p) (doc->number_up > 1 && \
123 ((p) % doc->number_up) != 0)
130 static int JobCanceled
= 0;/* Set to 1 on SIGTERM */
137 static pstops_page_t
*add_page(pstops_doc_t
*doc
, const char *label
);
138 static void cancel_job(int sig
);
139 static int check_range(pstops_doc_t
*doc
, int page
);
140 static void copy_bytes(cups_file_t
*fp
, off_t offset
,
142 static ssize_t
copy_comments(cups_file_t
*fp
, pstops_doc_t
*doc
,
143 ppd_file_t
*ppd
, char *line
,
144 ssize_t linelen
, size_t linesize
);
145 static void copy_dsc(cups_file_t
*fp
, pstops_doc_t
*doc
,
146 ppd_file_t
*ppd
, char *line
, ssize_t linelen
,
148 static void copy_non_dsc(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_page(cups_file_t
*fp
, pstops_doc_t
*doc
,
152 ppd_file_t
*ppd
, int number
, char *line
,
153 ssize_t linelen
, size_t linesize
);
154 static ssize_t
copy_prolog(cups_file_t
*fp
, pstops_doc_t
*doc
,
155 ppd_file_t
*ppd
, char *line
,
156 ssize_t linelen
, size_t linesize
);
157 static ssize_t
copy_setup(cups_file_t
*fp
, pstops_doc_t
*doc
,
158 ppd_file_t
*ppd
, char *line
,
159 ssize_t linelen
, size_t linesize
);
160 static ssize_t
copy_trailer(cups_file_t
*fp
, pstops_doc_t
*doc
,
161 ppd_file_t
*ppd
, int number
, char *line
,
162 ssize_t linelen
, size_t linesize
);
163 static void do_prolog(pstops_doc_t
*doc
, ppd_file_t
*ppd
);
164 static void do_setup(pstops_doc_t
*doc
, ppd_file_t
*ppd
);
165 static void doc_printf(pstops_doc_t
*doc
, const char *format
, ...)
166 __attribute__ ((__format__ (__printf__
, 2, 3)));
167 static void doc_puts(pstops_doc_t
*doc
, const char *s
);
168 static void doc_write(pstops_doc_t
*doc
, const char *s
, size_t len
);
169 static void end_nup(pstops_doc_t
*doc
, int number
);
170 static int include_feature(ppd_file_t
*ppd
, const char *line
,
172 cups_option_t
**options
);
173 static char *parse_text(const char *start
, char **end
, char *buffer
,
175 static void set_pstops_options(pstops_doc_t
*doc
, ppd_file_t
*ppd
,
176 char *argv
[], int num_options
,
177 cups_option_t
*options
);
178 static ssize_t
skip_page(cups_file_t
*fp
, char *line
, ssize_t linelen
,
180 static void start_nup(pstops_doc_t
*doc
, int number
,
181 int show_border
, const int *bounding_box
);
182 static void write_label_prolog(pstops_doc_t
*doc
, const char *label
,
183 float bottom
, float top
,
185 static void write_labels(pstops_doc_t
*doc
, int orient
);
186 static void write_options(pstops_doc_t
*doc
, ppd_file_t
*ppd
,
187 int num_options
, cups_option_t
*options
);
191 * 'main()' - Main entry.
194 int /* O - Exit status */
195 main(int argc
, /* I - Number of command-line args */
196 char *argv
[]) /* I - Command-line arguments */
198 pstops_doc_t doc
; /* Document information */
199 cups_file_t
*fp
; /* Print file */
200 ppd_file_t
*ppd
; /* PPD file */
201 int num_options
; /* Number of print options */
202 cups_option_t
*options
; /* Print options */
203 char line
[8192]; /* Line buffer */
204 ssize_t len
; /* Length of line buffer */
205 #if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
206 struct sigaction action
; /* Actions for POSIX signals */
207 #endif /* HAVE_SIGACTION && !HAVE_SIGSET */
211 * Make sure status messages are not buffered...
214 setbuf(stderr
, NULL
);
217 * Ignore broken pipe signals...
220 signal(SIGPIPE
, SIG_IGN
);
223 * Check command-line...
226 if (argc
< 6 || argc
> 7)
228 _cupsLangPrintf(stderr
,
229 _("Usage: %s job-id user title copies options [file]"),
235 * Register a signal handler to cleanly cancel a job.
238 #ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
239 sigset(SIGTERM
, cancel_job
);
240 #elif defined(HAVE_SIGACTION)
241 memset(&action
, 0, sizeof(action
));
243 sigemptyset(&action
.sa_mask
);
244 action
.sa_handler
= cancel_job
;
245 sigaction(SIGTERM
, &action
, NULL
);
247 signal(SIGTERM
, cancel_job
);
248 #endif /* HAVE_SIGSET */
251 * If we have 7 arguments, print the file named on the command-line.
252 * Otherwise, send stdin instead...
256 fp
= cupsFileStdin();
260 * Try to open the print file...
263 if ((fp
= cupsFileOpen(argv
[6], "r")) == NULL
)
267 fprintf(stderr
, "DEBUG: Unable to open \"%s\".\n", argv
[6]);
268 _cupsLangPrintError("ERROR", _("Unable to open print file"));
276 * Read the first line to see if we have DSC comments...
279 if ((len
= (ssize_t
)cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
281 fputs("DEBUG: The print file is empty.\n", stderr
);
286 * Process command-line options...
290 num_options
= cupsParseOptions(argv
[5], 0, &options
);
291 ppd
= SetCommonOptions(num_options
, options
, 1);
293 set_pstops_options(&doc
, ppd
, argv
, num_options
, options
);
296 * Write any "exit server" options that have been selected...
299 ppdEmit(ppd
, stdout
, PPD_ORDER_EXIT
);
302 * Write any JCL commands that are needed to print PostScript code...
306 ppdEmitJCL(ppd
, stdout
, doc
.job_id
, doc
.user
, doc
.title
);
309 * Start with a DSC header...
312 puts("%!PS-Adobe-3.0");
315 * Skip leading PJL in the document...
318 while (!strncmp(line
, "\033%-12345X", 9) || !strncmp(line
, "@PJL ", 5))
321 * Yup, we have leading PJL fun, so skip it until we hit the line
322 * with "ENTER LANGUAGE"...
325 fputs("DEBUG: Skipping PJL header...\n", stderr
);
327 while (strstr(line
, "ENTER LANGUAGE") == NULL
&& strncmp(line
, "%!", 2))
328 if ((len
= (ssize_t
)cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
331 if (!strncmp(line
, "%!", 2))
334 if ((len
= (ssize_t
)cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
339 * Now see if the document conforms to the Adobe Document Structuring
343 if (!strncmp(line
, "%!PS-Adobe-", 11))
346 * Yes, filter the document...
349 copy_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
354 * No, display an error message and treat the file as if it contains
358 copy_non_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
362 * Send %%EOF as needed...
369 * End the job with the appropriate JCL command or CTRL-D...
374 if (ppd
&& ppd
->jcl_end
)
375 ppdEmitJCLEnd(ppd
, stdout
);
381 * Close files and remove the temporary file if needed...
386 cupsFileClose(doc
.temp
);
387 unlink(doc
.tempfile
);
391 cupsFreeOptions(num_options
, options
);
400 * 'add_page()' - Add a page to the pages array.
403 static pstops_page_t
* /* O - New page info object */
404 add_page(pstops_doc_t
*doc
, /* I - Document information */
405 const char *label
) /* I - Page label */
407 pstops_page_t
*pageinfo
; /* New page info object */
411 doc
->pages
= cupsArrayNew(NULL
, NULL
);
415 _cupsLangPrintError("EMERG", _("Unable to allocate memory for pages array"));
419 if ((pageinfo
= calloc(1, sizeof(pstops_page_t
))) == NULL
)
421 _cupsLangPrintError("EMERG", _("Unable to allocate memory for page info"));
425 pageinfo
->label
= strdup(label
);
426 pageinfo
->offset
= cupsFileTell(doc
->temp
);
428 cupsArrayAdd(doc
->pages
, pageinfo
);
437 * 'cancel_job()' - Flag the job as canceled.
441 cancel_job(int sig
) /* I - Signal number (unused) */
450 * 'check_range()' - Check to see if the current page is selected for
454 static int /* O - 1 if selected, 0 otherwise */
455 check_range(pstops_doc_t
*doc
, /* I - Document information */
456 int page
) /* I - Page number */
458 const char *range
; /* Pointer into range string */
459 int lower
, upper
; /* Lower and upper page numbers */
465 * See if we only print even or odd pages...
468 if (!_cups_strcasecmp(doc
->page_set
, "even") && (page
& 1))
471 if (!_cups_strcasecmp(doc
->page_set
, "odd") && !(page
& 1))
475 if (!doc
->page_ranges
)
476 return (1); /* No range, print all pages... */
478 for (range
= doc
->page_ranges
; *range
!= '\0';)
484 upper
= (int)strtol(range
, (char **)&range
, 10);
488 lower
= (int)strtol(range
, (char **)&range
, 10);
493 if (!isdigit(*range
& 255))
496 upper
= (int)strtol(range
, (char **)&range
, 10);
502 if (page
>= lower
&& page
<= upper
)
516 * 'copy_bytes()' - Copy bytes from the input file to stdout.
520 copy_bytes(cups_file_t
*fp
, /* I - File to read from */
521 off_t offset
, /* I - Offset to page data */
522 size_t length
) /* I - Length of page data */
524 char buffer
[8192]; /* Data buffer */
525 ssize_t nbytes
; /* Number of bytes read */
526 size_t nleft
; /* Number of bytes left/remaining */
531 if (cupsFileSeek(fp
, offset
) < 0)
533 _cupsLangPrintError("ERROR", _("Unable to see in file"));
537 while (nleft
> 0 || length
== 0)
539 if (nleft
> sizeof(buffer
) || length
== 0)
540 nbytes
= sizeof(buffer
);
542 nbytes
= (ssize_t
)nleft
;
544 if ((nbytes
= cupsFileRead(fp
, buffer
, (size_t)nbytes
)) < 1)
547 nleft
-= (size_t)nbytes
;
549 fwrite(buffer
, 1, (size_t)nbytes
, stdout
);
555 * 'copy_comments()' - Copy all of the comments section.
557 * This function expects "line" to be filled with a comment line.
558 * On return, "line" will contain the next line in the file, if any.
561 static ssize_t
/* O - Length of next line */
562 copy_comments(cups_file_t
*fp
, /* I - File to read from */
563 pstops_doc_t
*doc
, /* I - Document info */
564 ppd_file_t
*ppd
, /* I - PPD file */
565 char *line
, /* I - Line buffer */
566 ssize_t linelen
, /* I - Length of initial line */
567 size_t linesize
) /* I - Size of line buffer */
569 int saw_bounding_box
, /* Saw %%BoundingBox: comment? */
570 saw_for
, /* Saw %%For: comment? */
571 saw_pages
, /* Saw %%Pages: comment? */
572 saw_title
; /* Saw %%Title: comment? */
576 * Loop until we see %%EndComments or a non-comment line...
579 saw_bounding_box
= 0;
584 while (line
[0] == '%')
587 * Strip trailing whitespace...
594 if (!isspace(line
[linelen
] & 255))
597 line
[linelen
] = '\0';
604 fprintf(stderr
, "DEBUG: %s\n", line
);
607 * Pull the headers out...
610 if (!strncmp(line
, "%%Pages:", 8))
612 int pages
; /* Number of pages */
615 fputs("DEBUG: A duplicate %%Pages: comment was seen.\n", stderr
);
619 if (Duplex
&& (pages
= atoi(line
+ 8)) > 0 && pages
<= doc
->number_up
)
622 * Since we will only be printing on a single page, disable duplexing.
626 doc
->slow_duplex
= 0;
628 if (cupsGetOption("sides", doc
->num_options
, doc
->options
))
629 doc
->num_options
= cupsAddOption("sides", "one-sided",
630 doc
->num_options
, &(doc
->options
));
632 if (cupsGetOption("Duplex", doc
->num_options
, doc
->options
))
633 doc
->num_options
= cupsAddOption("Duplex", "None",
634 doc
->num_options
, &(doc
->options
));
636 if (cupsGetOption("EFDuplex", doc
->num_options
, doc
->options
))
637 doc
->num_options
= cupsAddOption("EFDuplex", "None",
638 doc
->num_options
, &(doc
->options
));
640 if (cupsGetOption("EFDuplexing", doc
->num_options
, doc
->options
))
641 doc
->num_options
= cupsAddOption("EFDuplexing", "False",
642 doc
->num_options
, &(doc
->options
));
644 if (cupsGetOption("KD03Duplex", doc
->num_options
, doc
->options
))
645 doc
->num_options
= cupsAddOption("KD03Duplex", "None",
646 doc
->num_options
, &(doc
->options
));
648 if (cupsGetOption("JCLDuplex", doc
->num_options
, doc
->options
))
649 doc
->num_options
= cupsAddOption("JCLDuplex", "None",
650 doc
->num_options
, &(doc
->options
));
652 ppdMarkOption(ppd
, "Duplex", "None");
653 ppdMarkOption(ppd
, "EFDuplex", "None");
654 ppdMarkOption(ppd
, "EFDuplexing", "False");
655 ppdMarkOption(ppd
, "KD03Duplex", "None");
656 ppdMarkOption(ppd
, "JCLDuplex", "None");
659 else if (!strncmp(line
, "%%BoundingBox:", 14))
661 if (saw_bounding_box
)
662 fputs("DEBUG: A duplicate %%BoundingBox: comment was seen.\n", stderr
);
663 else if (strstr(line
+ 14, "(atend)"))
666 * Do nothing for now but use the default imageable area...
669 else if (sscanf(line
+ 14, "%d%d%d%d", doc
->bounding_box
+ 0,
670 doc
->bounding_box
+ 1, doc
->bounding_box
+ 2,
671 doc
->bounding_box
+ 3) != 4)
673 fputs("DEBUG: A bad %%BoundingBox: comment was seen.\n", stderr
);
675 doc
->bounding_box
[0] = (int)PageLeft
;
676 doc
->bounding_box
[1] = (int)PageBottom
;
677 doc
->bounding_box
[2] = (int)PageRight
;
678 doc
->bounding_box
[3] = (int)PageTop
;
681 saw_bounding_box
= 1;
683 else if (!strncmp(line
, "%%For:", 6))
686 doc_printf(doc
, "%s\n", line
);
688 else if (!strncmp(line
, "%%Title:", 8))
691 doc_printf(doc
, "%s\n", line
);
693 else if (!strncmp(line
, "%cupsRotation:", 14))
696 * Reset orientation of document?
699 int orient
= (atoi(line
+ 14) / 90) & 3;
701 if (orient
!= Orientation
)
704 * Yes, update things so that the pages come out right...
707 Orientation
= (4 - Orientation
+ orient
) & 3;
709 Orientation
= orient
;
712 else if (!strcmp(line
, "%%EndComments"))
714 linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
);
717 else if (strncmp(line
, "%!", 2) && strncmp(line
, "%cups", 5))
718 doc_printf(doc
, "%s\n", line
);
720 if ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) == 0)
724 if (!saw_bounding_box
)
725 fputs("DEBUG: There wasn't a %%BoundingBox: comment in the header.\n",
729 fputs("DEBUG: There wasn't a %%Pages: comment in the header.\n", stderr
);
732 WriteTextComment("For", doc
->user
);
735 WriteTextComment("Title", doc
->title
);
737 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
740 * Tell the document processor the copy and duplex options
741 * that are required...
744 doc_printf(doc
, "%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
745 doc
->collate
? " collate" : "",
746 Duplex
? " duplex" : "");
749 * Apple uses RBI comments for various non-PPD options...
752 doc_printf(doc
, "%%RBINumCopies: %d\n", doc
->copies
);
757 * Tell the document processor the duplex option that is required...
761 doc_puts(doc
, "%%Requirements: duplex\n");
764 * Apple uses RBI comments for various non-PPD options...
767 doc_puts(doc
, "%RBINumCopies: 1\n");
770 doc_puts(doc
, "%%Pages: (atend)\n");
771 doc_puts(doc
, "%%BoundingBox: (atend)\n");
772 doc_puts(doc
, "%%EndComments\n");
779 * 'copy_dsc()' - Copy a DSC-conforming document.
781 * This function expects "line" to be filled with the %!PS-Adobe comment line.
785 copy_dsc(cups_file_t
*fp
, /* I - File to read from */
786 pstops_doc_t
*doc
, /* I - Document info */
787 ppd_file_t
*ppd
, /* I - PPD file */
788 char *line
, /* I - Line buffer */
789 ssize_t linelen
, /* I - Length of initial line */
790 size_t linesize
) /* I - Size of line buffer */
792 int number
; /* Page number */
793 pstops_page_t
*pageinfo
; /* Page information */
797 * Make sure we use ESPshowpage for EPS files...
800 if (strstr(line
, "EPSF"))
802 doc
->use_ESPshowpage
= 1;
807 * Start sending the document with any commands needed...
810 fprintf(stderr
, "DEBUG: Before copy_comments - %s", line
);
811 linelen
= copy_comments(fp
, doc
, ppd
, line
, linelen
, linesize
);
814 * Now find the prolog section, if any...
817 fprintf(stderr
, "DEBUG: Before copy_prolog - %s", line
);
818 linelen
= copy_prolog(fp
, doc
, ppd
, line
, linelen
, linesize
);
821 * Then the document setup section...
824 fprintf(stderr
, "DEBUG: Before copy_setup - %s", line
);
825 linelen
= copy_setup(fp
, doc
, ppd
, line
, linelen
, linesize
);
828 * Copy until we see %%Page:...
831 while (strncmp(line
, "%%Page:", 7) && strncmp(line
, "%%Trailer", 9))
833 doc_write(doc
, line
, (size_t)linelen
);
835 if ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) == 0)
840 * Then process pages until we have no more...
845 fprintf(stderr
, "DEBUG: Before page loop - %s", line
);
846 while (!strncmp(line
, "%%Page:", 7))
853 if (check_range(doc
, (number
- 1) / doc
->number_up
+ 1))
855 fprintf(stderr
, "DEBUG: Copying page %d...\n", number
);
856 linelen
= copy_page(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
860 fprintf(stderr
, "DEBUG: Skipping page %d...\n", number
);
861 linelen
= skip_page(fp
, line
, linelen
, linesize
);
866 * Finish up the last page(s)...
869 if (number
&& is_not_last_page(number
) && cupsArrayLast(doc
->pages
) &&
870 check_range(doc
, (number
- 1) / doc
->number_up
+ 1))
872 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
874 start_nup(doc
, doc
->number_up
, 0, doc
->bounding_box
);
875 doc_puts(doc
, "showpage\n");
876 end_nup(doc
, doc
->number_up
);
878 pageinfo
->length
= (ssize_t
)(cupsFileTell(doc
->temp
) - pageinfo
->offset
);
881 if (doc
->slow_duplex
&& (doc
->page
& 1))
884 * Make sure we have an even number of pages...
887 pageinfo
= add_page(doc
, "(filler)");
889 if (!doc
->slow_order
)
891 if (!ppd
|| !ppd
->num_filters
)
892 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
893 doc
->slow_collate
? 1 : doc
->copies
);
895 printf("%%%%Page: (filler) %d\n", doc
->page
);
898 start_nup(doc
, doc
->number_up
, 0, doc
->bounding_box
);
899 doc_puts(doc
, "showpage\n");
900 end_nup(doc
, doc
->number_up
);
902 pageinfo
->length
= (ssize_t
)(cupsFileTell(doc
->temp
) - pageinfo
->offset
);
906 * Make additional copies as necessary...
909 number
= doc
->slow_order
? 0 : doc
->page
;
911 if (doc
->temp
&& !JobCanceled
&& cupsArrayCount(doc
->pages
) > 0)
913 int copy
; /* Current copy */
917 * Reopen the temporary file for reading...
920 cupsFileClose(doc
->temp
);
922 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
928 if (doc
->slow_collate
)
929 copy
= !doc
->slow_order
;
931 copy
= doc
->copies
- 1;
933 for (; copy
< doc
->copies
; copy
++)
939 * Send end-of-job stuff followed by any start-of-job stuff required
940 * for the JCL options...
943 if (number
&& doc
->emit_jcl
&& ppd
&& ppd
->jcl_end
)
946 * Send the trailer...
950 printf("%%%%Pages: %d\n", cupsArrayCount(doc
->pages
));
951 if (doc
->number_up
> 1 || doc
->fit_to_page
)
952 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
953 PageLeft
, PageBottom
, PageRight
, PageTop
);
955 printf("%%%%BoundingBox: %d %d %d %d\n",
956 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
957 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
961 * Start a new document...
964 ppdEmitJCLEnd(ppd
, stdout
);
965 ppdEmitJCL(ppd
, stdout
, doc
->job_id
, doc
->user
, doc
->title
);
967 puts("%!PS-Adobe-3.0");
973 * Copy the prolog as needed...
978 pageinfo
= (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
979 copy_bytes(doc
->temp
, 0, (size_t)pageinfo
->offset
);
983 * Then copy all of the pages...
986 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayLast(doc
->pages
) :
987 (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
996 if (!ppd
|| !ppd
->num_filters
)
997 fprintf(stderr
, "PAGE: %d %d\n", number
,
998 doc
->slow_collate
? 1 : doc
->copies
);
1000 if (doc
->number_up
> 1)
1002 printf("%%%%Page: (%d) %d\n", number
, number
);
1003 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1004 PageLeft
, PageBottom
, PageRight
, PageTop
);
1008 printf("%%%%Page: %s %d\n", pageinfo
->label
, number
);
1009 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1010 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
1011 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
1014 copy_bytes(doc
->temp
, pageinfo
->offset
, (size_t)pageinfo
->length
);
1016 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayPrev(doc
->pages
) :
1017 (pstops_page_t
*)cupsArrayNext(doc
->pages
);
1023 * Restore the old showpage operator as needed...
1026 if (doc
->use_ESPshowpage
)
1027 puts("userdict/showpage/ESPshowpage load put\n");
1030 * Write/copy the trailer...
1034 copy_trailer(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
1039 * 'copy_non_dsc()' - Copy a document that does not conform to the DSC.
1041 * This function expects "line" to be filled with the %! comment line.
1045 copy_non_dsc(cups_file_t
*fp
, /* I - File to read from */
1046 pstops_doc_t
*doc
, /* I - Document info */
1047 ppd_file_t
*ppd
, /* I - PPD file */
1048 char *line
, /* I - Line buffer */
1049 ssize_t linelen
, /* I - Length of initial line */
1050 size_t linesize
) /* I - Size of line buffer */
1052 int copy
; /* Current copy */
1053 char buffer
[8192]; /* Copy buffer */
1054 ssize_t bytes
; /* Number of bytes copied */
1060 * First let the user know that they are attempting to print a file
1061 * that may not print correctly...
1064 fputs("DEBUG: This document does not conform to the Adobe Document "
1065 "Structuring Conventions and may not print correctly.\n", stderr
);
1068 * Then write a standard DSC comment section...
1071 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", PageLeft
, PageBottom
,
1072 PageRight
, PageTop
);
1074 if (doc
->slow_collate
&& doc
->copies
> 1)
1075 printf("%%%%Pages: %d\n", doc
->copies
);
1079 WriteTextComment("For", doc
->user
);
1080 WriteTextComment("Title", doc
->title
);
1082 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1085 * Tell the document processor the copy and duplex options
1086 * that are required...
1089 printf("%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
1090 doc
->collate
? " collate" : "",
1091 Duplex
? " duplex" : "");
1094 * Apple uses RBI comments for various non-PPD options...
1097 printf("%%RBINumCopies: %d\n", doc
->copies
);
1102 * Tell the document processor the duplex option that is required...
1106 puts("%%Requirements: duplex");
1109 * Apple uses RBI comments for various non-PPD options...
1112 puts("%RBINumCopies: 1");
1115 puts("%%EndComments");
1118 * Then the prolog...
1121 puts("%%BeginProlog");
1123 do_prolog(doc
, ppd
);
1125 puts("%%EndProlog");
1128 * Then the setup section...
1131 puts("%%BeginSetup");
1138 * Finally, embed a copy of the file inside a %%Page...
1141 if (!ppd
|| !ppd
->num_filters
)
1142 fprintf(stderr
, "PAGE: 1 %d\n", doc
->temp
? 1 : doc
->copies
);
1144 puts("%%Page: 1 1");
1145 puts("%%BeginPageSetup");
1146 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1147 puts("%%EndPageSetup");
1148 puts("%%BeginDocument: nondsc");
1150 fwrite(line
, (size_t)linelen
, 1, stdout
);
1153 cupsFileWrite(doc
->temp
, line
, (size_t)linelen
);
1155 while ((bytes
= cupsFileRead(fp
, buffer
, sizeof(buffer
))) > 0)
1157 fwrite(buffer
, 1, (size_t)bytes
, stdout
);
1160 cupsFileWrite(doc
->temp
, buffer
, (size_t)bytes
);
1163 puts("%%EndDocument");
1165 if (doc
->use_ESPshowpage
)
1167 WriteLabels(Orientation
);
1168 puts("ESPshowpage");
1171 if (doc
->temp
&& !JobCanceled
)
1174 * Reopen the temporary file for reading...
1177 cupsFileClose(doc
->temp
);
1179 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
1182 * Make the additional copies as needed...
1185 for (copy
= 1; copy
< doc
->copies
; copy
++)
1190 if (!ppd
|| !ppd
->num_filters
)
1191 fputs("PAGE: 1 1\n", stderr
);
1193 printf("%%%%Page: %d %d\n", copy
+ 1, copy
+ 1);
1194 puts("%%BeginPageSetup");
1195 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1196 puts("%%EndPageSetup");
1197 puts("%%BeginDocument: nondsc");
1199 copy_bytes(doc
->temp
, 0, 0);
1201 puts("%%EndDocument");
1203 if (doc
->use_ESPshowpage
)
1205 WriteLabels(Orientation
);
1206 puts("ESPshowpage");
1212 * Restore the old showpage operator as needed...
1215 if (doc
->use_ESPshowpage
)
1216 puts("userdict/showpage/ESPshowpage load put\n");
1221 * 'copy_page()' - Copy a page description.
1223 * This function expects "line" to be filled with a %%Page comment line.
1224 * On return, "line" will contain the next line in the file, if any.
1227 static ssize_t
/* O - Length of next line */
1228 copy_page(cups_file_t
*fp
, /* I - File to read from */
1229 pstops_doc_t
*doc
, /* I - Document info */
1230 ppd_file_t
*ppd
, /* I - PPD file */
1231 int number
, /* I - Current page number */
1232 char *line
, /* I - Line buffer */
1233 ssize_t linelen
, /* I - Length of initial line */
1234 size_t linesize
) /* I - Size of line buffer */
1236 char label
[256], /* Page label string */
1237 *ptr
; /* Pointer into line */
1238 int level
; /* Embedded document level */
1239 pstops_page_t
*pageinfo
; /* Page information */
1240 int first_page
; /* First page on N-up output? */
1241 int has_page_setup
= 0; /* Does the page have %%Begin/EndPageSetup? */
1242 int bounding_box
[4]; /* PageBoundingBox */
1246 * Get the page label for this page...
1249 first_page
= is_first_page(number
);
1251 if (!parse_text(line
+ 7, &ptr
, label
, sizeof(label
)))
1253 fputs("DEBUG: There was a bad %%Page: comment in the file.\n", stderr
);
1257 else if (strtol(ptr
, &ptr
, 10) == LONG_MAX
|| !isspace(*ptr
& 255))
1259 fputs("DEBUG: There was a bad %%Page: comment in the file.\n", stderr
);
1264 * Create or update the current output page...
1268 pageinfo
= add_page(doc
, label
);
1270 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
1273 * Handle first page override...
1276 if (doc
->ap_input_slot
|| doc
->ap_manual_feed
)
1278 if ((doc
->page
== 1 && (!doc
->slow_order
|| !Duplex
)) ||
1279 (doc
->page
== 2 && doc
->slow_order
&& Duplex
))
1282 * First page/sheet gets AP_FIRSTPAGE_* options...
1285 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->ap_input_slot
,
1286 pageinfo
->num_options
,
1287 &(pageinfo
->options
));
1288 pageinfo
->num_options
= cupsAddOption("ManualFeed",
1289 doc
->ap_input_slot
? "False" :
1290 doc
->ap_manual_feed
,
1291 pageinfo
->num_options
,
1292 &(pageinfo
->options
));
1293 pageinfo
->num_options
= cupsAddOption("MediaColor", doc
->ap_media_color
,
1294 pageinfo
->num_options
,
1295 &(pageinfo
->options
));
1296 pageinfo
->num_options
= cupsAddOption("MediaType", doc
->ap_media_type
,
1297 pageinfo
->num_options
,
1298 &(pageinfo
->options
));
1299 pageinfo
->num_options
= cupsAddOption("PageRegion", doc
->ap_page_region
,
1300 pageinfo
->num_options
,
1301 &(pageinfo
->options
));
1302 pageinfo
->num_options
= cupsAddOption("PageSize", doc
->ap_page_size
,
1303 pageinfo
->num_options
,
1304 &(pageinfo
->options
));
1306 else if (doc
->page
== (Duplex
+ 2))
1309 * Second page/sheet gets default options...
1312 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->input_slot
,
1313 pageinfo
->num_options
,
1314 &(pageinfo
->options
));
1315 pageinfo
->num_options
= cupsAddOption("ManualFeed",
1316 doc
->input_slot
? "False" :
1318 pageinfo
->num_options
,
1319 &(pageinfo
->options
));
1320 pageinfo
->num_options
= cupsAddOption("MediaColor", doc
->media_color
,
1321 pageinfo
->num_options
,
1322 &(pageinfo
->options
));
1323 pageinfo
->num_options
= cupsAddOption("MediaType", doc
->media_type
,
1324 pageinfo
->num_options
,
1325 &(pageinfo
->options
));
1326 pageinfo
->num_options
= cupsAddOption("PageRegion", doc
->page_region
,
1327 pageinfo
->num_options
,
1328 &(pageinfo
->options
));
1329 pageinfo
->num_options
= cupsAddOption("PageSize", doc
->page_size
,
1330 pageinfo
->num_options
,
1331 &(pageinfo
->options
));
1336 * Scan comments until we see something other than %%Page*: or
1340 memcpy(bounding_box
, doc
->bounding_box
, sizeof(bounding_box
));
1342 while ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) > 0)
1344 if (!strncmp(line
, "%%PageBoundingBox:", 18))
1347 * %%PageBoundingBox: llx lly urx ury
1350 if (sscanf(line
+ 18, "%d%d%d%d", bounding_box
+ 0,
1351 bounding_box
+ 1, bounding_box
+ 2,
1352 bounding_box
+ 3) != 4)
1354 fputs("DEBUG: There was a bad %%PageBoundingBox: comment in the file.\n", stderr
);
1355 memcpy(bounding_box
, doc
->bounding_box
,
1356 sizeof(bounding_box
));
1358 else if (doc
->number_up
== 1 && !doc
->fit_to_page
&& Orientation
)
1360 int temp_bbox
[4]; /* Temporary bounding box */
1363 memcpy(temp_bbox
, bounding_box
, sizeof(temp_bbox
));
1365 fprintf(stderr
, "DEBUG: Orientation = %d\n", Orientation
);
1366 fprintf(stderr
, "DEBUG: original bounding_box = [ %d %d %d %d ]\n",
1367 bounding_box
[0], bounding_box
[1],
1368 bounding_box
[2], bounding_box
[3]);
1369 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
1370 PageWidth
, PageLength
);
1372 switch (Orientation
)
1374 case 1 : /* Landscape */
1375 bounding_box
[0] = (int)(PageLength
- temp_bbox
[3]);
1376 bounding_box
[1] = temp_bbox
[0];
1377 bounding_box
[2] = (int)(PageLength
- temp_bbox
[1]);
1378 bounding_box
[3] = temp_bbox
[2];
1381 case 2 : /* Reverse Portrait */
1382 bounding_box
[0] = (int)(PageWidth
- temp_bbox
[2]);
1383 bounding_box
[1] = (int)(PageLength
- temp_bbox
[3]);
1384 bounding_box
[2] = (int)(PageWidth
- temp_bbox
[0]);
1385 bounding_box
[3] = (int)(PageLength
- temp_bbox
[1]);
1388 case 3 : /* Reverse Landscape */
1389 bounding_box
[0] = temp_bbox
[1];
1390 bounding_box
[1] = (int)(PageWidth
- temp_bbox
[2]);
1391 bounding_box
[2] = temp_bbox
[3];
1392 bounding_box
[3] = (int)(PageWidth
- temp_bbox
[0]);
1396 fprintf(stderr
, "DEBUG: updated bounding_box = [ %d %d %d %d ]\n",
1397 bounding_box
[0], bounding_box
[1],
1398 bounding_box
[2], bounding_box
[3]);
1402 else if (!strncmp(line
, "%%PageCustomColors:", 19) ||
1403 !strncmp(line
, "%%PageMedia:", 12) ||
1404 !strncmp(line
, "%%PageOrientation:", 18) ||
1405 !strncmp(line
, "%%PageProcessColors:", 20) ||
1406 !strncmp(line
, "%%PageRequirements:", 18) ||
1407 !strncmp(line
, "%%PageResources:", 16))
1414 else if (!strncmp(line
, "%%PageCustomColors:", 19))
1417 * %%PageCustomColors: ...
1420 else if (!strncmp(line
, "%%PageMedia:", 12))
1426 else if (!strncmp(line
, "%%PageOrientation:", 18))
1429 * %%PageOrientation: ...
1432 else if (!strncmp(line
, "%%PageProcessColors:", 20))
1435 * %%PageProcessColors: ...
1438 else if (!strncmp(line
, "%%PageRequirements:", 18))
1441 * %%PageRequirements: ...
1444 else if (!strncmp(line
, "%%PageResources:", 16))
1447 * %%PageResources: ...
1450 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1453 * %%IncludeFeature: *MainKeyword OptionKeyword
1456 if (doc
->number_up
== 1 &&!doc
->fit_to_page
)
1457 pageinfo
->num_options
= include_feature(ppd
, line
,
1458 pageinfo
->num_options
,
1459 &(pageinfo
->options
));
1461 else if (!strncmp(line
, "%%BeginPageSetup", 16))
1470 if (doc
->number_up
== 1)
1473 * Update the document's composite and page bounding box...
1476 memcpy(pageinfo
->bounding_box
, bounding_box
,
1477 sizeof(pageinfo
->bounding_box
));
1479 if (bounding_box
[0] < doc
->new_bounding_box
[0])
1480 doc
->new_bounding_box
[0] = bounding_box
[0];
1481 if (bounding_box
[1] < doc
->new_bounding_box
[1])
1482 doc
->new_bounding_box
[1] = bounding_box
[1];
1483 if (bounding_box
[2] > doc
->new_bounding_box
[2])
1484 doc
->new_bounding_box
[2] = bounding_box
[2];
1485 if (bounding_box
[3] > doc
->new_bounding_box
[3])
1486 doc
->new_bounding_box
[3] = bounding_box
[3];
1490 * Output the page header as needed...
1493 if (!doc
->slow_order
&& first_page
)
1495 if (!ppd
|| !ppd
->num_filters
)
1496 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
1497 doc
->slow_collate
? 1 : doc
->copies
);
1499 if (doc
->number_up
> 1)
1501 printf("%%%%Page: (%d) %d\n", doc
->page
, doc
->page
);
1502 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1503 PageLeft
, PageBottom
, PageRight
, PageTop
);
1507 printf("%%%%Page: %s %d\n", pageinfo
->label
, doc
->page
);
1508 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1509 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
1510 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
1515 * Copy any page setup commands...
1519 doc_puts(doc
, "%%BeginPageSetup\n");
1523 int feature
= 0; /* In a Begin/EndFeature block? */
1525 while ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) > 0)
1527 if (!strncmp(line
, "%%EndPageSetup", 14))
1529 else if (!strncmp(line
, "%%BeginFeature:", 15))
1533 if (doc
->number_up
> 1 || doc
->fit_to_page
)
1536 else if (!strncmp(line
, "%%EndFeature", 12))
1540 if (doc
->number_up
> 1 || doc
->fit_to_page
)
1543 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1545 pageinfo
->num_options
= include_feature(ppd
, line
,
1546 pageinfo
->num_options
,
1547 &(pageinfo
->options
));
1550 else if (!strncmp(line
, "%%Include", 9))
1553 if (line
[0] != '%' && !feature
)
1556 if (!feature
|| (doc
->number_up
== 1 && !doc
->fit_to_page
))
1557 doc_write(doc
, line
, (size_t)linelen
);
1561 * Skip %%EndPageSetup...
1564 if (linelen
> 0 && !strncmp(line
, "%%EndPageSetup", 14))
1565 linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
);
1570 char *page_setup
; /* PageSetup commands to send */
1573 if (pageinfo
->num_options
> 0)
1574 write_options(doc
, ppd
, pageinfo
->num_options
, pageinfo
->options
);
1577 * Output commands for the current page...
1580 page_setup
= ppdEmitString(ppd
, PPD_ORDER_PAGE
, 0);
1584 doc_puts(doc
, page_setup
);
1590 * Prep for the start of the page description...
1593 start_nup(doc
, number
, 1, bounding_box
);
1596 doc_puts(doc
, "%%EndPageSetup\n");
1599 * Read the rest of the page description...
1607 (!strncmp(line
, "%%Page:", 7) ||
1608 !strncmp(line
, "%%Trailer", 9) ||
1609 !strncmp(line
, "%%EOF", 5)))
1611 else if (!strncmp(line
, "%%BeginDocument", 15) ||
1612 !strncmp(line
, "%ADO_BeginApplication", 21))
1614 doc_write(doc
, line
, (size_t)linelen
);
1618 else if ((!strncmp(line
, "%%EndDocument", 13) ||
1619 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
1621 doc_write(doc
, line
, (size_t)linelen
);
1625 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
1626 (!strncmp(line
, "%%BeginData:", 12) &&
1627 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
1630 * Copy binary data...
1633 int bytes
; /* Bytes of data */
1636 doc_write(doc
, line
, (size_t)linelen
);
1638 bytes
= atoi(strchr(line
, ':') + 1);
1642 if ((size_t)bytes
> linesize
)
1643 linelen
= cupsFileRead(fp
, line
, linesize
);
1645 linelen
= cupsFileRead(fp
, line
, (size_t)bytes
);
1650 perror("ERROR: Early end-of-file while reading binary data");
1654 doc_write(doc
, line
, (size_t)linelen
);
1660 doc_write(doc
, line
, (size_t)linelen
);
1662 while ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) > 0);
1665 * Finish up this page and return...
1668 end_nup(doc
, number
);
1670 pageinfo
->length
= (ssize_t
)(cupsFileTell(doc
->temp
) - pageinfo
->offset
);
1677 * 'copy_prolog()' - Copy the document prolog section.
1679 * This function expects "line" to be filled with a %%BeginProlog comment line.
1680 * On return, "line" will contain the next line in the file, if any.
1683 static ssize_t
/* O - Length of next line */
1684 copy_prolog(cups_file_t
*fp
, /* I - File to read from */
1685 pstops_doc_t
*doc
, /* I - Document info */
1686 ppd_file_t
*ppd
, /* I - PPD file */
1687 char *line
, /* I - Line buffer */
1688 ssize_t linelen
, /* I - Length of initial line */
1689 size_t linesize
) /* I - Size of line buffer */
1691 while (strncmp(line
, "%%BeginProlog", 13))
1693 if (!strncmp(line
, "%%BeginSetup", 12) || !strncmp(line
, "%%Page:", 7))
1696 doc_write(doc
, line
, (size_t)linelen
);
1698 if ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) == 0)
1702 doc_puts(doc
, "%%BeginProlog\n");
1704 do_prolog(doc
, ppd
);
1706 if (!strncmp(line
, "%%BeginProlog", 13))
1708 while ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) > 0)
1710 if (!strncmp(line
, "%%EndProlog", 11) ||
1711 !strncmp(line
, "%%BeginSetup", 12) ||
1712 !strncmp(line
, "%%Page:", 7))
1715 doc_write(doc
, line
, (size_t)linelen
);
1718 if (!strncmp(line
, "%%EndProlog", 11))
1719 linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
);
1721 fputs("DEBUG: The %%EndProlog comment is missing.\n", stderr
);
1724 doc_puts(doc
, "%%EndProlog\n");
1731 * 'copy_setup()' - Copy the document setup section.
1733 * This function expects "line" to be filled with a %%BeginSetup comment line.
1734 * On return, "line" will contain the next line in the file, if any.
1737 static ssize_t
/* O - Length of next line */
1738 copy_setup(cups_file_t
*fp
, /* I - File to read from */
1739 pstops_doc_t
*doc
, /* I - Document info */
1740 ppd_file_t
*ppd
, /* I - PPD file */
1741 char *line
, /* I - Line buffer */
1742 ssize_t linelen
, /* I - Length of initial line */
1743 size_t linesize
) /* I - Size of line buffer */
1745 int num_options
; /* Number of options */
1746 cups_option_t
*options
; /* Options */
1749 while (strncmp(line
, "%%BeginSetup", 12))
1751 if (!strncmp(line
, "%%Page:", 7))
1754 doc_write(doc
, line
, (size_t)linelen
);
1756 if ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) == 0)
1760 doc_puts(doc
, "%%BeginSetup\n");
1767 if (!strncmp(line
, "%%BeginSetup", 12))
1769 while (strncmp(line
, "%%EndSetup", 10))
1771 if (!strncmp(line
, "%%Page:", 7))
1773 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1776 * %%IncludeFeature: *MainKeyword OptionKeyword
1779 if (doc
->number_up
== 1 && !doc
->fit_to_page
)
1780 num_options
= include_feature(ppd
, line
, num_options
, &options
);
1782 else if (strncmp(line
, "%%BeginSetup", 12))
1783 doc_write(doc
, line
, (size_t)linelen
);
1785 if ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) == 0)
1789 if (!strncmp(line
, "%%EndSetup", 10))
1790 linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
);
1792 fputs("DEBUG: The %%EndSetup comment is missing.\n", stderr
);
1795 if (num_options
> 0)
1797 write_options(doc
, ppd
, num_options
, options
);
1798 cupsFreeOptions(num_options
, options
);
1801 doc_puts(doc
, "%%EndSetup\n");
1808 * 'copy_trailer()' - Copy the document trailer.
1810 * This function expects "line" to be filled with a %%Trailer comment line.
1811 * On return, "line" will contain the next line in the file, if any.
1814 static ssize_t
/* O - Length of next line */
1815 copy_trailer(cups_file_t
*fp
, /* I - File to read from */
1816 pstops_doc_t
*doc
, /* I - Document info */
1817 ppd_file_t
*ppd
, /* I - PPD file */
1818 int number
, /* I - Number of pages */
1819 char *line
, /* I - Line buffer */
1820 ssize_t linelen
, /* I - Length of initial line */
1821 size_t linesize
) /* I - Size of line buffer */
1824 * Write the trailer comments...
1833 if (!strncmp(line
, "%%EOF", 5))
1835 else if (strncmp(line
, "%%Trailer", 9) &&
1836 strncmp(line
, "%%Pages:", 8) &&
1837 strncmp(line
, "%%BoundingBox:", 14))
1838 fwrite(line
, 1, (size_t)linelen
, stdout
);
1840 linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
);
1843 fprintf(stderr
, "DEBUG: Wrote %d pages...\n", number
);
1845 printf("%%%%Pages: %d\n", number
);
1846 if (doc
->number_up
> 1 || doc
->fit_to_page
)
1847 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
1848 PageLeft
, PageBottom
, PageRight
, PageTop
);
1850 printf("%%%%BoundingBox: %d %d %d %d\n",
1851 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
1852 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
1859 * 'do_prolog()' - Send the necessary document prolog commands.
1863 do_prolog(pstops_doc_t
*doc
, /* I - Document information */
1864 ppd_file_t
*ppd
) /* I - PPD file */
1866 char *ps
; /* PS commands */
1870 * Send the document prolog commands...
1873 if (ppd
&& ppd
->patches
)
1875 doc_puts(doc
, "%%BeginFeature: *JobPatchFile 1\n");
1876 doc_puts(doc
, ppd
->patches
);
1877 doc_puts(doc
, "\n%%EndFeature\n");
1880 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_PROLOG
, 0.0)) != NULL
)
1887 * Define ESPshowpage here so that applications that define their
1888 * own procedure to do a showpage pick it up...
1891 if (doc
->use_ESPshowpage
)
1892 doc_puts(doc
, "userdict/ESPshowpage/showpage load put\n"
1893 "userdict/showpage{}put\n");
1898 * 'do_setup()' - Send the necessary document setup commands.
1902 do_setup(pstops_doc_t
*doc
, /* I - Document information */
1903 ppd_file_t
*ppd
) /* I - PPD file */
1905 char *ps
; /* PS commands */
1909 * Disable CTRL-D so that embedded files don't cause printing
1913 doc_puts(doc
, "% Disable CTRL-D as an end-of-file marker...\n");
1914 doc_puts(doc
, "userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put\n");
1917 * Mark job options...
1920 cupsMarkOptions(ppd
, doc
->num_options
, doc
->options
);
1923 * Send all the printer-specific setup commands...
1926 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, 0.0)) != NULL
)
1932 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_ANY
, 0.0)) != NULL
)
1939 * Set the number of copies for the job...
1942 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1944 doc_printf(doc
, "%%RBIBeginNonPPDFeature: *NumCopies %d\n", doc
->copies
);
1946 "%d/languagelevel where{pop languagelevel 2 ge}{false}ifelse\n"
1947 "{1 dict begin/NumCopies exch def currentdict end "
1949 "{userdict/#copies 3 -1 roll put}ifelse\n", doc
->copies
);
1950 doc_puts(doc
, "%RBIEndNonPPDFeature\n");
1954 * If we are doing N-up printing, disable setpagedevice...
1957 if (doc
->number_up
> 1)
1959 doc_puts(doc
, "userdict/CUPSsetpagedevice/setpagedevice load put\n");
1960 doc_puts(doc
, "userdict/setpagedevice{pop}bind put\n");
1964 * Make sure we have rectclip and rectstroke procedures of some sort...
1968 "% x y w h ESPrc - Clip to a rectangle.\n"
1969 "userdict/ESPrc/rectclip where{pop/rectclip load}\n"
1970 "{{newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
1971 "neg 0 rlineto closepath clip newpath}bind}ifelse put\n");
1974 "% x y w h ESPrf - Fill a rectangle.\n"
1975 "userdict/ESPrf/rectfill where{pop/rectfill load}\n"
1976 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
1977 "neg 0 rlineto closepath fill grestore}bind}ifelse put\n");
1980 "% x y w h ESPrs - Stroke a rectangle.\n"
1981 "userdict/ESPrs/rectstroke where{pop/rectstroke load}\n"
1982 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
1983 "neg 0 rlineto closepath stroke grestore}bind}ifelse put\n");
1986 * Write the page and label prologs...
1989 if (doc
->number_up
== 2 || doc
->number_up
== 6)
1992 * For 2- and 6-up output, rotate the labels to match the orientation
1996 if (Orientation
& 1)
1997 write_label_prolog(doc
, doc
->page_label
, PageBottom
,
1998 PageWidth
- PageLength
+ PageTop
, PageLength
);
2000 write_label_prolog(doc
, doc
->page_label
, PageLeft
, PageRight
,
2004 write_label_prolog(doc
, doc
->page_label
, PageBottom
, PageTop
, PageWidth
);
2009 * 'doc_printf()' - Send a formatted string to stdout and/or the temp file.
2011 * This function should be used for all page-level output that is affected
2012 * by ordering, collation, etc.
2016 doc_printf(pstops_doc_t
*doc
, /* I - Document information */
2017 const char *format
, /* I - Printf-style format string */
2018 ...) /* I - Additional arguments as needed */
2020 va_list ap
; /* Pointer to arguments */
2021 char buffer
[1024]; /* Output buffer */
2022 ssize_t bytes
; /* Number of bytes to write */
2025 va_start(ap
, format
);
2026 bytes
= vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
2029 if ((size_t)bytes
> sizeof(buffer
))
2031 _cupsLangPrintFilter(stderr
, "ERROR",
2032 _("Buffer overflow detected, aborting."));
2036 doc_write(doc
, buffer
, (size_t)bytes
);
2041 * 'doc_puts()' - Send a nul-terminated string to stdout and/or the temp file.
2043 * This function should be used for all page-level output that is affected
2044 * by ordering, collation, etc.
2048 doc_puts(pstops_doc_t
*doc
, /* I - Document information */
2049 const char *s
) /* I - String to send */
2051 doc_write(doc
, s
, strlen(s
));
2056 * 'doc_write()' - Send data to stdout and/or the temp file.
2060 doc_write(pstops_doc_t
*doc
, /* I - Document information */
2061 const char *s
, /* I - Data to send */
2062 size_t len
) /* I - Number of bytes to send */
2064 if (!doc
->slow_order
)
2065 fwrite(s
, 1, len
, stdout
);
2068 cupsFileWrite(doc
->temp
, s
, len
);
2073 * 'end_nup()' - End processing for N-up printing.
2077 end_nup(pstops_doc_t
*doc
, /* I - Document information */
2078 int number
) /* I - Page number */
2080 if (doc
->number_up
> 1)
2081 doc_puts(doc
, "userdict/ESPsave get restore\n");
2083 switch (doc
->number_up
)
2086 if (doc
->use_ESPshowpage
)
2088 write_labels(doc
, Orientation
);
2089 doc_puts(doc
, "ESPshowpage\n");
2095 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2097 if (Orientation
& 1)
2100 * Rotate the labels back to portrait...
2103 write_labels(doc
, Orientation
- 1);
2105 else if (Orientation
== 0)
2108 * Rotate the labels to landscape...
2111 write_labels(doc
, doc
->normal_landscape
? 1 : 3);
2116 * Rotate the labels to landscape...
2119 write_labels(doc
, doc
->normal_landscape
? 3 : 1);
2122 doc_puts(doc
, "ESPshowpage\n");
2127 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2129 write_labels(doc
, Orientation
);
2130 doc_puts(doc
, "ESPshowpage\n");
2140 * 'include_feature()' - Include a printer option/feature command.
2143 static int /* O - New number of options */
2145 ppd_file_t
*ppd
, /* I - PPD file */
2146 const char *line
, /* I - DSC line */
2147 int num_options
, /* I - Number of options */
2148 cups_option_t
**options
) /* IO - Options */
2150 char name
[255], /* Option name */
2151 value
[255]; /* Option value */
2152 ppd_option_t
*option
; /* Option in file */
2156 * Get the "%%IncludeFeature: *Keyword OptionKeyword" values...
2159 if (sscanf(line
+ 17, "%254s%254s", name
, value
) != 2)
2161 fputs("DEBUG: The %%IncludeFeature: comment is not valid.\n", stderr
);
2162 return (num_options
);
2166 * Find the option and choice...
2169 if ((option
= ppdFindOption(ppd
, name
+ 1)) == NULL
)
2171 _cupsLangPrintFilter(stderr
, "WARNING", _("Unknown option \"%s\"."),
2173 return (num_options
);
2176 if (option
->section
== PPD_ORDER_EXIT
||
2177 option
->section
== PPD_ORDER_JCL
)
2179 _cupsLangPrintFilter(stderr
, "WARNING",
2180 _("Option \"%s\" cannot be included via "
2181 "%%%%IncludeFeature."), name
+ 1);
2182 return (num_options
);
2185 if (!ppdFindChoice(option
, value
))
2187 _cupsLangPrintFilter(stderr
, "WARNING",
2188 _("Unknown choice \"%s\" for option \"%s\"."),
2190 return (num_options
);
2194 * Add the option to the option array and return...
2197 return (cupsAddOption(name
+ 1, value
, num_options
, options
));
2202 * 'parse_text()' - Parse a text value in a comment.
2204 * This function parses a DSC text value as defined on page 36 of the
2205 * DSC specification. Text values are either surrounded by parenthesis
2206 * or whitespace-delimited.
2208 * The value returned is the literal characters for the entire text
2209 * string, including any parenthesis and escape characters.
2212 static char * /* O - Value or NULL on error */
2213 parse_text(const char *start
, /* I - Start of text value */
2214 char **end
, /* O - End of text value */
2215 char *buffer
, /* I - Buffer */
2216 size_t bufsize
) /* I - Size of buffer */
2218 char *bufptr
, /* Pointer in buffer */
2219 *bufend
; /* End of buffer */
2220 int level
; /* Parenthesis level */
2224 * Skip leading whitespace...
2227 while (isspace(*start
& 255))
2231 * Then copy the value...
2236 bufend
= buffer
+ bufsize
- 1;
2238 while (bufptr
< bufend
)
2240 if (isspace(*start
& 255) && !level
)
2247 else if (*start
== ')')
2257 else if (*start
== '\\')
2260 * Copy escaped character...
2263 int i
; /* Looping var */
2267 i
<= 3 && isdigit(start
[i
] & 255) && bufptr
< bufend
;
2268 *bufptr
++ = start
[i
], i
++);
2277 * Return the value and new pointer into the line...
2281 *end
= (char *)start
;
2283 if (bufptr
== bufend
)
2291 * 'set_pstops_options()' - Set pstops options.
2296 pstops_doc_t
*doc
, /* I - Document information */
2297 ppd_file_t
*ppd
, /* I - PPD file */
2298 char *argv
[], /* I - Command-line arguments */
2299 int num_options
, /* I - Number of options */
2300 cups_option_t
*options
) /* I - Options */
2302 const char *val
; /* Option value */
2303 int intval
; /* Integer option value */
2304 ppd_attr_t
*attr
; /* PPD attribute */
2305 ppd_option_t
*option
; /* PPD option */
2306 ppd_choice_t
*choice
; /* PPD choice */
2307 const char *content_type
; /* Original content type */
2308 int max_copies
; /* Maximum number of copies supported */
2312 * Initialize document information structure...
2315 memset(doc
, 0, sizeof(pstops_doc_t
));
2317 doc
->job_id
= atoi(argv
[1]);
2318 doc
->user
= argv
[2];
2319 doc
->title
= argv
[3];
2320 doc
->copies
= atoi(argv
[4]);
2322 if (ppd
&& ppd
->landscape
> 0)
2323 doc
->normal_landscape
= 1;
2325 doc
->bounding_box
[0] = (int)PageLeft
;
2326 doc
->bounding_box
[1] = (int)PageBottom
;
2327 doc
->bounding_box
[2] = (int)PageRight
;
2328 doc
->bounding_box
[3] = (int)PageTop
;
2330 doc
->new_bounding_box
[0] = INT_MAX
;
2331 doc
->new_bounding_box
[1] = INT_MAX
;
2332 doc
->new_bounding_box
[2] = INT_MIN
;
2333 doc
->new_bounding_box
[3] = INT_MIN
;
2336 * AP_FIRSTPAGE_* and the corresponding non-first-page options.
2339 doc
->ap_input_slot
= cupsGetOption("AP_FIRSTPAGE_InputSlot", num_options
,
2341 doc
->ap_manual_feed
= cupsGetOption("AP_FIRSTPAGE_ManualFeed", num_options
,
2343 doc
->ap_media_color
= cupsGetOption("AP_FIRSTPAGE_MediaColor", num_options
,
2345 doc
->ap_media_type
= cupsGetOption("AP_FIRSTPAGE_MediaType", num_options
,
2347 doc
->ap_page_region
= cupsGetOption("AP_FIRSTPAGE_PageRegion", num_options
,
2349 doc
->ap_page_size
= cupsGetOption("AP_FIRSTPAGE_PageSize", num_options
,
2352 if ((choice
= ppdFindMarkedChoice(ppd
, "InputSlot")) != NULL
)
2353 doc
->input_slot
= choice
->choice
;
2354 if ((choice
= ppdFindMarkedChoice(ppd
, "ManualFeed")) != NULL
)
2355 doc
->manual_feed
= choice
->choice
;
2356 if ((choice
= ppdFindMarkedChoice(ppd
, "MediaColor")) != NULL
)
2357 doc
->media_color
= choice
->choice
;
2358 if ((choice
= ppdFindMarkedChoice(ppd
, "MediaType")) != NULL
)
2359 doc
->media_type
= choice
->choice
;
2360 if ((choice
= ppdFindMarkedChoice(ppd
, "PageRegion")) != NULL
)
2361 doc
->page_region
= choice
->choice
;
2362 if ((choice
= ppdFindMarkedChoice(ppd
, "PageSize")) != NULL
)
2363 doc
->page_size
= choice
->choice
;
2366 * collate, multiple-document-handling
2369 if ((val
= cupsGetOption("multiple-document-handling", num_options
, options
)) != NULL
)
2372 * This IPP attribute is unnecessarily complicated...
2374 * single-document, separate-documents-collated-copies, and
2375 * single-document-new-sheet all require collated copies.
2377 * separate-documents-uncollated-copies allows for uncollated copies.
2380 doc
->collate
= _cups_strcasecmp(val
, "separate-documents-uncollated-copies") != 0;
2383 if ((val
= cupsGetOption("Collate", num_options
, options
)) != NULL
&&
2384 (!_cups_strcasecmp(val
, "true") ||!_cups_strcasecmp(val
, "on") ||
2385 !_cups_strcasecmp(val
, "yes")))
2392 if ((val
= cupsGetOption("emit-jcl", num_options
, options
)) != NULL
&&
2393 (!_cups_strcasecmp(val
, "false") || !_cups_strcasecmp(val
, "off") ||
2394 !_cups_strcasecmp(val
, "no") || !strcmp(val
, "0")))
2400 * fit-to-page/ipp-attribute-fidelity
2402 * (Only for original PostScript content)
2405 if ((content_type
= getenv("CONTENT_TYPE")) == NULL
)
2406 content_type
= "application/postscript";
2408 if (!_cups_strcasecmp(content_type
, "application/postscript"))
2410 if ((val
= cupsGetOption("fit-to-page", num_options
, options
)) != NULL
&&
2411 !_cups_strcasecmp(val
, "true"))
2412 doc
->fit_to_page
= 1;
2413 else if ((val
= cupsGetOption("ipp-attribute-fidelity", num_options
,
2414 options
)) != NULL
&&
2415 !_cups_strcasecmp(val
, "true"))
2416 doc
->fit_to_page
= 1;
2420 * mirror/MirrorPrint
2423 if ((choice
= ppdFindMarkedChoice(ppd
, "MirrorPrint")) != NULL
)
2425 val
= choice
->choice
;
2429 val
= cupsGetOption("mirror", num_options
, options
);
2431 if (val
&& (!_cups_strcasecmp(val
, "true") || !_cups_strcasecmp(val
, "on") ||
2432 !_cups_strcasecmp(val
, "yes")))
2439 if ((val
= cupsGetOption("number-up", num_options
, options
)) != NULL
)
2441 switch (intval
= atoi(val
))
2449 doc
->number_up
= intval
;
2452 _cupsLangPrintFilter(stderr
, "ERROR",
2453 _("Unsupported number-up value %d, using "
2454 "number-up=1."), intval
);
2466 if ((val
= cupsGetOption("number-up-layout", num_options
, options
)) != NULL
)
2468 if (!_cups_strcasecmp(val
, "lrtb"))
2469 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2470 else if (!_cups_strcasecmp(val
, "lrbt"))
2471 doc
->number_up_layout
= PSTOPS_LAYOUT_LRBT
;
2472 else if (!_cups_strcasecmp(val
, "rltb"))
2473 doc
->number_up_layout
= PSTOPS_LAYOUT_RLTB
;
2474 else if (!_cups_strcasecmp(val
, "rlbt"))
2475 doc
->number_up_layout
= PSTOPS_LAYOUT_RLBT
;
2476 else if (!_cups_strcasecmp(val
, "tblr"))
2477 doc
->number_up_layout
= PSTOPS_LAYOUT_TBLR
;
2478 else if (!_cups_strcasecmp(val
, "tbrl"))
2479 doc
->number_up_layout
= PSTOPS_LAYOUT_TBRL
;
2480 else if (!_cups_strcasecmp(val
, "btlr"))
2481 doc
->number_up_layout
= PSTOPS_LAYOUT_BTLR
;
2482 else if (!_cups_strcasecmp(val
, "btrl"))
2483 doc
->number_up_layout
= PSTOPS_LAYOUT_BTRL
;
2486 _cupsLangPrintFilter(stderr
, "ERROR",
2487 _("Unsupported number-up-layout value %s, using "
2488 "number-up-layout=lrtb."), val
);
2489 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2493 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2499 if ((val
= cupsGetOption("OutputOrder", num_options
, options
)) != NULL
)
2501 if (!_cups_strcasecmp(val
, "Reverse"))
2502 doc
->output_order
= 1;
2507 * Figure out the right default output order from the PPD file...
2510 if ((choice
= ppdFindMarkedChoice(ppd
, "OutputBin")) != NULL
&&
2511 (attr
= ppdFindAttr(ppd
, "PageStackOrder", choice
->choice
)) != NULL
&&
2513 doc
->output_order
= !_cups_strcasecmp(attr
->value
, "Reverse");
2514 else if ((attr
= ppdFindAttr(ppd
, "DefaultOutputOrder", NULL
)) != NULL
&&
2516 doc
->output_order
= !_cups_strcasecmp(attr
->value
, "Reverse");
2523 if ((val
= cupsGetOption("page-border", num_options
, options
)) != NULL
)
2525 if (!_cups_strcasecmp(val
, "none"))
2526 doc
->page_border
= PSTOPS_BORDERNONE
;
2527 else if (!_cups_strcasecmp(val
, "single"))
2528 doc
->page_border
= PSTOPS_BORDERSINGLE
;
2529 else if (!_cups_strcasecmp(val
, "single-thick"))
2530 doc
->page_border
= PSTOPS_BORDERSINGLE2
;
2531 else if (!_cups_strcasecmp(val
, "double"))
2532 doc
->page_border
= PSTOPS_BORDERDOUBLE
;
2533 else if (!_cups_strcasecmp(val
, "double-thick"))
2534 doc
->page_border
= PSTOPS_BORDERDOUBLE2
;
2537 _cupsLangPrintFilter(stderr
, "ERROR",
2538 _("Unsupported page-border value %s, using "
2539 "page-border=none."), val
);
2540 doc
->page_border
= PSTOPS_BORDERNONE
;
2544 doc
->page_border
= PSTOPS_BORDERNONE
;
2550 doc
->page_label
= cupsGetOption("page-label", num_options
, options
);
2556 doc
->page_ranges
= cupsGetOption("page-ranges", num_options
, options
);
2562 doc
->page_set
= cupsGetOption("page-set", num_options
, options
);
2565 * Now figure out if we have to force collated copies, etc.
2568 if ((attr
= ppdFindAttr(ppd
, "cupsMaxCopies", NULL
)) != NULL
)
2569 max_copies
= atoi(attr
->value
);
2570 else if (ppd
&& ppd
->manual_copies
)
2575 if (doc
->copies
> max_copies
)
2577 else if (ppd
&& ppd
->manual_copies
&& Duplex
&& doc
->copies
> 1)
2580 * Force collated copies when printing a duplexed document to
2581 * a non-PS printer that doesn't do hardware copy generation.
2582 * Otherwise the copies will end up on the front/back side of
2590 * See if we have to filter the fast or slow way...
2593 if (doc
->collate
&& doc
->copies
> 1)
2596 * See if we need to manually collate the pages...
2599 doc
->slow_collate
= 1;
2601 if (doc
->copies
<= max_copies
&&
2602 (choice
= ppdFindMarkedChoice(ppd
, "Collate")) != NULL
&&
2603 !_cups_strcasecmp(choice
->choice
, "True"))
2606 * Hardware collate option is selected, see if the option is
2607 * conflicting - if not, collate in hardware. Otherwise,
2608 * turn the hardware collate option off...
2611 if ((option
= ppdFindOption(ppd
, "Collate")) != NULL
&&
2612 !option
->conflicted
)
2613 doc
->slow_collate
= 0;
2615 ppdMarkOption(ppd
, "Collate", "False");
2619 doc
->slow_collate
= 0;
2621 if (!ppdFindOption(ppd
, "OutputOrder") && doc
->output_order
)
2622 doc
->slow_order
= 1;
2624 doc
->slow_order
= 0;
2627 (doc
->slow_collate
|| doc
->slow_order
||
2628 ((attr
= ppdFindAttr(ppd
, "cupsEvenDuplex", NULL
)) != NULL
&&
2629 attr
->value
&& !_cups_strcasecmp(attr
->value
, "true"))))
2630 doc
->slow_duplex
= 1;
2632 doc
->slow_duplex
= 0;
2635 * Create a temporary file for page data if we need to filter slowly...
2638 if (doc
->slow_order
|| doc
->slow_collate
)
2640 if ((doc
->temp
= cupsTempFile2(doc
->tempfile
,
2641 sizeof(doc
->tempfile
))) == NULL
)
2643 perror("DEBUG: Unable to create temporary file");
2649 * Figure out if we should use ESPshowpage or not...
2652 if (doc
->page_label
|| getenv("CLASSIFICATION") || doc
->number_up
> 1 ||
2656 * Yes, use ESPshowpage...
2659 doc
->use_ESPshowpage
= 1;
2662 fprintf(stderr
, "DEBUG: slow_collate=%d, slow_duplex=%d, slow_order=%d\n",
2663 doc
->slow_collate
, doc
->slow_duplex
, doc
->slow_order
);
2668 * 'skip_page()' - Skip past a page that won't be printed.
2671 static ssize_t
/* O - Length of next line */
2672 skip_page(cups_file_t
*fp
, /* I - File to read from */
2673 char *line
, /* I - Line buffer */
2674 ssize_t linelen
, /* I - Length of initial line */
2675 size_t linesize
) /* I - Size of line buffer */
2677 int level
; /* Embedded document level */
2682 while ((linelen
= (ssize_t
)cupsFileGetLine(fp
, line
, linesize
)) > 0)
2685 (!strncmp(line
, "%%Page:", 7) || !strncmp(line
, "%%Trailer", 9)))
2687 else if (!strncmp(line
, "%%BeginDocument", 15) ||
2688 !strncmp(line
, "%ADO_BeginApplication", 21))
2690 else if ((!strncmp(line
, "%%EndDocument", 13) ||
2691 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
2693 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
2694 (!strncmp(line
, "%%BeginData:", 12) &&
2695 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
2698 * Skip binary data...
2701 ssize_t bytes
; /* Bytes of data */
2703 bytes
= atoi(strchr(line
, ':') + 1);
2707 if ((size_t)bytes
> linesize
)
2708 linelen
= (ssize_t
)cupsFileRead(fp
, line
, linesize
);
2710 linelen
= (ssize_t
)cupsFileRead(fp
, line
, (size_t)bytes
);
2715 perror("ERROR: Early end-of-file while reading binary data");
2729 * 'start_nup()' - Start processing for N-up printing.
2733 start_nup(pstops_doc_t
*doc
, /* I - Document information */
2734 int number
, /* I - Page number */
2735 int show_border
, /* I - Show the border? */
2736 const int *bounding_box
) /* I - BoundingBox value */
2738 int pos
; /* Position on page */
2739 int x
, y
; /* Relative position of subpage */
2740 double w
, l
, /* Width and length of subpage */
2741 tx
, ty
; /* Translation values for subpage */
2742 double pagew
, /* Printable width of page */
2743 pagel
; /* Printable height of page */
2744 int bboxx
, /* BoundingBox X origin */
2745 bboxy
, /* BoundingBox Y origin */
2746 bboxw
, /* BoundingBox width */
2747 bboxl
; /* BoundingBox height */
2748 double margin
= 0; /* Current margin for border */
2751 if (doc
->number_up
> 1)
2752 doc_puts(doc
, "userdict/ESPsave save put\n");
2754 pos
= (number
- 1) % doc
->number_up
;
2755 pagew
= PageRight
- PageLeft
;
2756 pagel
= PageTop
- PageBottom
;
2758 if (doc
->fit_to_page
)
2760 bboxx
= bounding_box
[0];
2761 bboxy
= bounding_box
[1];
2762 bboxw
= bounding_box
[2] - bounding_box
[0];
2763 bboxl
= bounding_box
[3] - bounding_box
[1];
2769 bboxw
= (int)PageWidth
;
2770 bboxl
= (int)PageLength
;
2773 fprintf(stderr
, "DEBUG: pagew = %.1f, pagel = %.1f\n", pagew
, pagel
);
2774 fprintf(stderr
, "DEBUG: bboxx = %d, bboxy = %d, bboxw = %d, bboxl = %d\n",
2775 bboxx
, bboxy
, bboxw
, bboxl
);
2776 fprintf(stderr
, "DEBUG: PageLeft = %.1f, PageRight = %.1f\n",
2777 PageLeft
, PageRight
);
2778 fprintf(stderr
, "DEBUG: PageTop = %.1f, PageBottom = %.1f\n",
2779 PageTop
, PageBottom
);
2780 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
2781 PageWidth
, PageLength
);
2783 switch (Orientation
)
2785 case 1 : /* Landscape */
2786 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", PageLength
);
2788 case 2 : /* Reverse Portrait */
2789 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", PageWidth
,
2792 case 3 : /* Reverse Landscape */
2793 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", PageWidth
);
2798 * Mirror the page as needed...
2802 doc_printf(doc
, "%.1f 0.0 translate -1 1 scale\n", PageWidth
);
2805 * Offset and scale as necessary for fit_to_page/fit-to-page/number-up...
2808 if (Duplex
&& doc
->number_up
> 1 && ((number
/ doc
->number_up
) & 1))
2809 doc_printf(doc
, "%.1f %.1f translate\n", PageWidth
- PageRight
, PageBottom
);
2810 else if (doc
->number_up
> 1 || doc
->fit_to_page
)
2811 doc_printf(doc
, "%.1f %.1f translate\n", PageLeft
, PageBottom
);
2813 switch (doc
->number_up
)
2816 if (doc
->fit_to_page
)
2819 l
= w
* bboxl
/ bboxw
;
2824 w
= l
* bboxw
/ bboxl
;
2827 tx
= 0.5 * (pagew
- w
);
2828 ty
= 0.5 * (pagel
- l
);
2830 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n", tx
, ty
,
2831 w
/ bboxw
, l
/ bboxl
);
2838 if (Orientation
& 1)
2842 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2846 l
= w
* bboxl
/ bboxw
;
2848 if (l
> (pagew
* 0.5))
2851 w
= l
* bboxw
/ bboxl
;
2854 tx
= 0.5 * (pagew
* 0.5 - l
);
2855 ty
= 0.5 * (pagel
- w
);
2857 if (doc
->normal_landscape
)
2858 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2860 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2862 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2863 ty
, tx
+ pagew
* 0.5 * x
, w
/ bboxw
, l
/ bboxl
);
2869 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2873 w
= l
* bboxw
/ bboxl
;
2875 if (w
> (pagel
* 0.5))
2878 l
= w
* bboxl
/ bboxw
;
2881 tx
= 0.5 * (pagel
* 0.5 - w
);
2882 ty
= 0.5 * (pagew
- l
);
2884 if (doc
->normal_landscape
)
2885 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2887 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2889 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2890 tx
+ pagel
* 0.5 * x
, ty
, w
/ bboxw
, l
/ bboxl
);
2895 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2906 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2909 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2913 l
= w
* bboxl
/ bboxw
;
2915 if (l
> (pagel
* 0.5))
2918 w
= l
* bboxw
/ bboxl
;
2921 tx
= 0.5 * (pagew
* 0.5 - w
);
2922 ty
= 0.5 * (pagel
* 0.5 - l
);
2924 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2925 tx
+ x
* pagew
* 0.5, ty
+ y
* pagel
* 0.5,
2926 w
/ bboxw
, l
/ bboxl
);
2930 if (Orientation
& 1)
2932 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2937 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2940 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2948 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2951 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2956 l
= w
* bboxl
/ bboxw
;
2958 if (l
> (pagew
* 0.333))
2961 w
= l
* bboxw
/ bboxl
;
2964 tx
= 0.5 * (pagel
- 2 * w
);
2965 ty
= 0.5 * (pagew
- 3 * l
);
2967 if (doc
->normal_landscape
)
2968 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
2970 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
2972 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2973 tx
+ x
* w
, ty
+ y
* l
, l
/ bboxl
, w
/ bboxw
);
2977 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2982 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2985 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2993 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2996 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3001 w
= l
* bboxw
/ bboxl
;
3003 if (w
> (pagel
* 0.333))
3006 l
= w
* bboxl
/ bboxw
;
3009 tx
= 0.5 * (pagel
- 3 * w
);
3010 ty
= 0.5 * (pagew
- 2 * l
);
3012 if (doc
->normal_landscape
)
3013 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
3015 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
3017 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3018 tx
+ w
* x
, ty
+ l
* y
, w
/ bboxw
, l
/ bboxl
);
3024 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3035 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3038 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3042 l
= w
* bboxl
/ bboxw
;
3044 if (l
> (pagel
* 0.333))
3047 w
= l
* bboxw
/ bboxl
;
3050 tx
= 0.5 * (pagew
* 0.333 - w
);
3051 ty
= 0.5 * (pagel
* 0.333 - l
);
3053 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3054 tx
+ x
* pagew
* 0.333, ty
+ y
* pagel
* 0.333,
3055 w
/ bboxw
, l
/ bboxl
);
3059 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3070 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3073 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3077 l
= w
* bboxl
/ bboxw
;
3079 if (l
> (pagel
* 0.25))
3082 w
= l
* bboxw
/ bboxl
;
3085 tx
= 0.5 * (pagew
* 0.25 - w
);
3086 ty
= 0.5 * (pagel
* 0.25 - l
);
3088 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3089 tx
+ x
* pagew
* 0.25, ty
+ y
* pagel
* 0.25,
3090 w
/ bboxw
, l
/ bboxl
);
3095 * Draw borders as necessary...
3098 if (doc
->page_border
&& show_border
)
3100 int rects
; /* Number of border rectangles */
3101 double fscale
; /* Scaling value for points */
3104 rects
= (doc
->page_border
& PSTOPS_BORDERDOUBLE
) ? 2 : 1;
3105 fscale
= PageWidth
/ w
;
3106 margin
= 2.25 * fscale
;
3109 * Set the line width and color...
3112 doc_puts(doc
, "gsave\n");
3113 doc_printf(doc
, "%.3f setlinewidth 0 setgray newpath\n",
3114 (doc
->page_border
& PSTOPS_BORDERTHICK
) ? 0.5 * fscale
:
3118 * Draw border boxes...
3121 for (; rects
> 0; rects
--, margin
+= 2 * fscale
)
3122 if (doc
->number_up
> 1)
3123 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3127 bboxl
- 2 * margin
);
3129 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3131 PageBottom
+ margin
,
3132 PageRight
- PageLeft
- 2 * margin
,
3133 PageTop
- PageBottom
- 2 * margin
);
3136 * Restore pen settings...
3139 doc_puts(doc
, "grestore\n");
3142 if (doc
->fit_to_page
)
3145 * Offset the page by its bounding box...
3148 doc_printf(doc
, "%d %d translate\n", -bounding_box
[0],
3152 if (doc
->fit_to_page
|| doc
->number_up
> 1)
3155 * Clip the page to the page's bounding box...
3158 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrc\n",
3159 bboxx
+ margin
, bboxy
+ margin
,
3160 bboxw
- 2 * margin
, bboxl
- 2 * margin
);
3166 * 'write_label_prolog()' - Write the prolog with the classification
3171 write_label_prolog(pstops_doc_t
*doc
, /* I - Document info */
3172 const char *label
, /* I - Page label */
3173 float bottom
, /* I - Bottom position in points */
3174 float top
, /* I - Top position in points */
3175 float width
) /* I - Width in points */
3177 const char *classification
; /* CLASSIFICATION environment variable */
3178 const char *ptr
; /* Temporary string pointer */
3182 * First get the current classification...
3185 if ((classification
= getenv("CLASSIFICATION")) == NULL
)
3186 classification
= "";
3187 if (strcmp(classification
, "none") == 0)
3188 classification
= "";
3191 * If there is nothing to show, bind an empty 'write labels' procedure
3195 if (!classification
[0] && (label
== NULL
|| !label
[0]))
3197 doc_puts(doc
, "userdict/ESPwl{}bind put\n");
3202 * Set the classification + page label string...
3205 doc_puts(doc
, "userdict");
3206 if (!strcmp(classification
, "confidential"))
3207 doc_puts(doc
, "/ESPpl(CONFIDENTIAL");
3208 else if (!strcmp(classification
, "classified"))
3209 doc_puts(doc
, "/ESPpl(CLASSIFIED");
3210 else if (!strcmp(classification
, "secret"))
3211 doc_puts(doc
, "/ESPpl(SECRET");
3212 else if (!strcmp(classification
, "topsecret"))
3213 doc_puts(doc
, "/ESPpl(TOP SECRET");
3214 else if (!strcmp(classification
, "unclassified"))
3215 doc_puts(doc
, "/ESPpl(UNCLASSIFIED");
3218 doc_puts(doc
, "/ESPpl(");
3220 for (ptr
= classification
; *ptr
; ptr
++)
3222 if (*ptr
< 32 || *ptr
> 126)
3223 doc_printf(doc
, "\\%03o", *ptr
);
3224 else if (*ptr
== '_')
3226 else if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
3227 doc_printf(doc
, "\\%c", *ptr
);
3229 doc_printf(doc
, "%c", *ptr
);
3235 if (classification
[0])
3236 doc_puts(doc
, " - ");
3239 * Quote the label string as needed...
3242 for (ptr
= label
; *ptr
; ptr
++)
3244 if (*ptr
< 32 || *ptr
> 126)
3245 doc_printf(doc
, "\\%03o", *ptr
);
3246 else if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
3247 doc_printf(doc
, "\\%c", *ptr
);
3249 doc_printf(doc
, "%c", *ptr
);
3253 doc_puts(doc
, ")put\n");
3256 * Then get a 14 point Helvetica-Bold font...
3259 doc_puts(doc
, "userdict/ESPpf /Helvetica-Bold findfont 14 scalefont put\n");
3262 * Finally, the procedure to write the labels on the page...
3265 doc_puts(doc
, "userdict/ESPwl{\n");
3266 doc_puts(doc
, " ESPpf setfont\n");
3267 doc_printf(doc
, " ESPpl stringwidth pop dup 12 add exch -0.5 mul %.0f add\n",
3269 doc_puts(doc
, " 1 setgray\n");
3270 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrf\n", bottom
- 2.0);
3271 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrf\n", top
- 18.0);
3272 doc_puts(doc
, " 0 setgray\n");
3273 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrs\n", bottom
- 2.0);
3274 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrs\n", top
- 18.0);
3275 doc_printf(doc
, " dup %.0f moveto ESPpl show\n", bottom
+ 2.0);
3276 doc_printf(doc
, " %.0f moveto ESPpl show\n", top
- 14.0);
3277 doc_puts(doc
, "pop\n");
3278 doc_puts(doc
, "}bind put\n");
3283 * 'write_labels()' - Write the actual page labels.
3285 * This function is a copy of the one in common.c since we need to
3286 * use doc_puts/doc_printf instead of puts/printf...
3290 write_labels(pstops_doc_t
*doc
, /* I - Document information */
3291 int orient
) /* I - Orientation of the page */
3293 float width
, /* Width of page */
3294 length
; /* Length of page */
3297 doc_puts(doc
, "gsave\n");
3299 if ((orient
^ Orientation
) & 1)
3307 length
= PageLength
;
3312 case 1 : /* Landscape */
3313 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", length
);
3315 case 2 : /* Reverse Portrait */
3316 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", width
, length
);
3318 case 3 : /* Reverse Landscape */
3319 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", width
);
3323 doc_puts(doc
, "ESPwl\n");
3324 doc_puts(doc
, "grestore\n");
3329 * 'write_options()' - Write options provided via %%IncludeFeature.
3334 pstops_doc_t
*doc
, /* I - Document */
3335 ppd_file_t
*ppd
, /* I - PPD file */
3336 int num_options
, /* I - Number of options */
3337 cups_option_t
*options
) /* I - Options */
3339 int i
; /* Looping var */
3340 ppd_option_t
*option
; /* PPD option */
3341 float min_order
; /* Minimum OrderDependency value */
3342 char *doc_setup
, /* DocumentSetup commands to send */
3343 *any_setup
; /* AnySetup commands to send */
3347 * Figure out the minimum OrderDependency value...
3350 if ((option
= ppdFindOption(ppd
, "PageRegion")) != NULL
)
3351 min_order
= option
->order
;
3355 for (i
= 0; i
< num_options
; i
++)
3356 if ((option
= ppdFindOption(ppd
, options
[i
].name
)) != NULL
&&
3357 option
->order
< min_order
)
3358 min_order
= option
->order
;
3361 * Mark and extract them...
3364 cupsMarkOptions(ppd
, num_options
, options
);
3366 doc_setup
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, min_order
);
3367 any_setup
= ppdEmitString(ppd
, PPD_ORDER_ANY
, min_order
);
3370 * Then send them out...
3373 if (doc
->number_up
> 1)
3376 * Temporarily restore setpagedevice so we can set the options...
3379 doc_puts(doc
, "userdict/setpagedevice/CUPSsetpagedevice load put\n");
3384 doc_puts(doc
, doc_setup
);
3390 doc_puts(doc
, any_setup
);
3394 if (doc
->number_up
> 1)
3397 * Disable setpagedevice again...
3400 doc_puts(doc
, "userdict/setpagedevice{pop}bind put\n");