]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/pstops.c
2 * "$Id: pstops.c 6649 2007-07-11 21:46:42Z mike $"
4 * PostScript filter for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1993-2007 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * main() - Main entry...
20 * add_page() - Add a page to the pages array...
21 * check_range() - Check to see if the current page is selected for
22 * copy_bytes() - Copy bytes from the input file to stdout...
23 * copy_comments() - Copy all of the comments section...
24 * copy_dsc() - Copy a DSC-conforming document...
25 * copy_non_dsc() - Copy a document that does not conform to the DSC...
26 * copy_page() - Copy a page description...
27 * copy_prolog() - Copy the document prolog section...
28 * copy_setup() - Copy the document setup section...
29 * copy_trailer() - Copy the document trailer...
30 * do_prolog() - Send the necessary document prolog commands...
31 * do_setup() - Send the necessary document setup commands...
32 * doc_printf() - Send a formatted string to stdout and/or the temp
34 * doc_puts() - Send a nul-terminated string to stdout and/or the
36 * doc_write() - Send data to stdout and/or the temp file.
37 * end_nup() - End processing for N-up printing...
38 * include_feature() - Include a printer option/feature command.
39 * parse_text() - Parse a text value in a comment...
40 * set_pstops_options() - Set pstops options...
41 * skip_page() - Skip past a page that won't be printed...
42 * start_nup() - Start processing for N-up printing...
43 * write_labels() - Write the actual page labels.
47 * Include necessary headers...
53 #include <cups/file.h>
54 #include <cups/array.h>
55 #include <cups/i18n.h>
62 #define PSTOPS_BORDERNONE 0 /* No border or hairline border */
63 #define PSTOPS_BORDERTHICK 1 /* Think border */
64 #define PSTOPS_BORDERSINGLE 2 /* Single-line hairline border */
65 #define PSTOPS_BORDERSINGLE2 3 /* Single-line thick border */
66 #define PSTOPS_BORDERDOUBLE 4 /* Double-line hairline border */
67 #define PSTOPS_BORDERDOUBLE2 5 /* Double-line thick border */
69 #define PSTOPS_LAYOUT_LRBT 0 /* Left to right, bottom to top */
70 #define PSTOPS_LAYOUT_LRTB 1 /* Left to right, top to bottom */
71 #define PSTOPS_LAYOUT_RLBT 2 /* Right to left, bottom to top */
72 #define PSTOPS_LAYOUT_RLTB 3 /* Right to left, top to bottom */
73 #define PSTOPS_LAYOUT_BTLR 4 /* Bottom to top, left to right */
74 #define PSTOPS_LAYOUT_TBLR 5 /* Top to bottom, left to right */
75 #define PSTOPS_LAYOUT_BTRL 6 /* Bottom to top, right to left */
76 #define PSTOPS_LAYOUT_TBRL 7 /* Top to bottom, right to left */
78 #define PSTOPS_LAYOUT_NEGATEY 1 /* The bits for the layout */
79 #define PSTOPS_LAYOUT_NEGATEX 2 /* definitions above... */
80 #define PSTOPS_LAYOUT_VERTICAL 4
87 typedef struct /**** Page information ****/
89 char *label
; /* Page label */
90 int bounding_box
[4]; /* PageBoundingBox */
91 off_t offset
; /* Offset to start of page */
92 ssize_t length
; /* Number of bytes for page */
93 int num_options
; /* Number of options for this page */
94 cups_option_t
*options
; /* Options for this page */
97 typedef struct /**** Document information ****/
99 int page
; /* Current page */
100 int bounding_box
[4]; /* BoundingBox from header */
101 int new_bounding_box
[4]; /* New composite bounding box */
102 int num_options
; /* Number of document-wide options */
103 cups_option_t
*options
; /* Document-wide options */
104 int normal_landscape
, /* Normal rotation for landscape? */
105 saw_eof
, /* Saw the %%EOF comment? */
106 slow_collate
, /* Collate copies by hand? */
107 slow_duplex
, /* Duplex pages slowly? */
108 slow_order
, /* Reverse pages slowly? */
109 use_ESPshowpage
; /* Use ESPshowpage? */
110 cups_array_t
*pages
; /* Pages in document */
111 cups_file_t
*temp
; /* Temporary file, if any */
112 char tempfile
[1024]; /* Temporary filename */
113 int job_id
; /* Job ID */
114 const char *user
, /* User name */
115 *title
; /* Job name */
116 int copies
; /* Number of copies */
117 const char *ap_input_slot
, /* AP_FIRSTPAGE_InputSlot value */
118 *ap_manual_feed
; /* AP_FIRSTPAGE_ManualFeed value */
119 float brightness
; /* brightness value */
120 int collate
, /* Collate copies? */
121 emit_jcl
, /* Emit JCL commands? */
122 fitplot
; /* Fit pages to media */
123 float gamma
; /* gamma value */
124 const char *input_slot
, /* InputSlot value */
125 *manual_feed
; /* ManualFeed value */
126 int mirror
, /* doc->mirror/mirror pages */
127 number_up
, /* Number of pages on each sheet */
128 number_up_layout
, /* doc->number_up_layout of N-up pages */
129 output_order
, /* Requested reverse output order? */
130 page_border
; /* doc->page_border around pages */
131 const char *page_label
, /* page-label option, if any */
132 *page_ranges
, /* page-ranges option, if any */
133 *page_set
; /* page-set option, if any */
138 * Convenience macros...
141 #define is_first_page(p) (doc->number_up == 1 || \
142 ((p) % doc->number_up) == 1)
143 #define is_last_page(p) (doc->number_up == 1 || \
144 ((p) % doc->number_up) == 0)
145 #define is_not_last_page(p) (doc->number_up > 1 && \
146 ((p) % doc->number_up) != 0)
153 static pstops_page_t
*add_page(pstops_doc_t
*doc
, const char *label
);
154 static int check_range(pstops_doc_t
*doc
, int page
);
155 static void copy_bytes(cups_file_t
*fp
, off_t offset
,
157 static size_t copy_comments(cups_file_t
*fp
, pstops_doc_t
*doc
,
158 ppd_file_t
*ppd
, char *line
,
159 size_t linelen
, size_t linesize
);
160 static void copy_dsc(cups_file_t
*fp
, pstops_doc_t
*doc
,
161 ppd_file_t
*ppd
, char *line
, size_t linelen
,
163 static void copy_non_dsc(cups_file_t
*fp
, pstops_doc_t
*doc
,
164 ppd_file_t
*ppd
, char *line
,
165 size_t linelen
, size_t linesize
);
166 static size_t copy_page(cups_file_t
*fp
, pstops_doc_t
*doc
,
167 ppd_file_t
*ppd
, int number
, char *line
,
168 size_t linelen
, size_t linesize
);
169 static size_t copy_prolog(cups_file_t
*fp
, pstops_doc_t
*doc
,
170 ppd_file_t
*ppd
, char *line
,
171 size_t linelen
, size_t linesize
);
172 static size_t copy_setup(cups_file_t
*fp
, pstops_doc_t
*doc
,
173 ppd_file_t
*ppd
, char *line
,
174 size_t linelen
, size_t linesize
);
175 static size_t copy_trailer(cups_file_t
*fp
, pstops_doc_t
*doc
,
176 ppd_file_t
*ppd
, int number
, char *line
,
177 size_t linelen
, size_t linesize
);
178 static void do_prolog(pstops_doc_t
*doc
, ppd_file_t
*ppd
);
179 static void do_setup(pstops_doc_t
*doc
, ppd_file_t
*ppd
);
180 static void doc_printf(pstops_doc_t
*doc
, const char *format
, ...)
182 __attribute__ ((__format__ (__printf__
, 2, 3)))
183 #endif /* __GNUC__ */
185 static void doc_puts(pstops_doc_t
*doc
, const char *s
);
186 static void doc_write(pstops_doc_t
*doc
, const char *s
, size_t len
);
187 static void end_nup(pstops_doc_t
*doc
, int number
);
188 static int include_feature(ppd_file_t
*ppd
, const char *line
,
190 cups_option_t
**options
);
191 static char *parse_text(const char *start
, char **end
, char *buffer
,
193 static void set_pstops_options(pstops_doc_t
*doc
, ppd_file_t
*ppd
,
194 char *argv
[], int num_options
,
195 cups_option_t
*options
);
196 static size_t skip_page(cups_file_t
*fp
, char *line
, size_t linelen
,
198 static void start_nup(pstops_doc_t
*doc
, int number
,
199 int show_border
, const int *bounding_box
);
200 static void write_label_prolog(pstops_doc_t
*doc
, const char *label
,
201 float bottom
, float top
,
203 static void write_labels(pstops_doc_t
*doc
, int orient
);
207 * 'main()' - Main entry...
210 int /* O - Exit status */
211 main(int argc
, /* I - Number of command-line args */
212 char *argv
[]) /* I - Command-line arguments */
214 pstops_doc_t doc
; /* Document information */
215 cups_file_t
*fp
; /* Print file */
216 ppd_file_t
*ppd
; /* PPD file */
217 int num_options
; /* Number of print options */
218 cups_option_t
*options
; /* Print options */
219 char line
[8192]; /* Line buffer */
220 size_t len
; /* Length of line buffer */
224 * Make sure status messages are not buffered...
227 setbuf(stderr
, NULL
);
230 * Check command-line...
233 if (argc
< 6 || argc
> 7)
235 fprintf(stderr
, _("Usage: %s job-id user title copies options [file]\n"),
241 * If we have 7 arguments, print the file named on the command-line.
242 * Otherwise, send stdin instead...
246 fp
= cupsFileStdin();
250 * Try to open the print file...
253 if ((fp
= cupsFileOpen(argv
[6], "r")) == NULL
)
255 fprintf(stderr
, _("ERROR: Unable to open file \"%s\" - %s\n"),
256 argv
[6], strerror(errno
));
262 * Read the first line to see if we have DSC comments...
265 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
267 fputs(_("ERROR: Empty print file!\n"), stderr
);
272 * Process command-line options...
276 num_options
= cupsParseOptions(argv
[5], 0, &options
);
277 ppd
= SetCommonOptions(num_options
, options
, 1);
279 set_pstops_options(&doc
, ppd
, argv
, num_options
, options
);
282 * Write any "exit server" options that have been selected...
285 ppdEmit(ppd
, stdout
, PPD_ORDER_EXIT
);
288 * Write any JCL commands that are needed to print PostScript code...
292 ppdEmitJCL(ppd
, stdout
, doc
.job_id
, doc
.user
, doc
.title
);
295 * Start with a DSC header...
298 puts("%!PS-Adobe-3.0");
301 * Skip leading PJL in the document...
304 while (!strncmp(line
, "\033%-12345X", 9) || !strncmp(line
, "@PJL ", 5))
307 * Yup, we have leading PJL fun, so skip it until we hit the line
308 * with "ENTER LANGUAGE"...
311 fputs("DEBUG: Skipping PJL header...\n", stderr
);
313 while (strstr(line
, "ENTER LANGUAGE") == NULL
&& strncmp(line
, "%!", 2))
314 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
317 if (!strncmp(line
, "%!", 2))
320 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
325 * Now see if the document conforms to the Adobe Document Structuring
329 if (!strncmp(line
, "%!PS-Adobe-", 11))
332 * Yes, filter the document...
335 copy_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
340 * No, display an error message and treat the file as if it contains
344 copy_non_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
348 * Send %%EOF as needed...
355 * End the job with the appropriate JCL command or CTRL-D...
360 if (ppd
&& ppd
->jcl_end
)
361 ppdEmitJCLEnd(ppd
, stdout
);
367 * Close files and remove the temporary file if needed...
372 cupsFileClose(doc
.temp
);
373 unlink(doc
.tempfile
);
377 cupsFreeOptions(num_options
, options
);
386 * 'add_page()' - Add a page to the pages array...
389 static pstops_page_t
* /* O - New page info object */
390 add_page(pstops_doc_t
*doc
, /* I - Document information */
391 const char *label
) /* I - Page label */
393 pstops_page_t
*pageinfo
; /* New page info object */
397 doc
->pages
= cupsArrayNew(NULL
, NULL
);
401 fprintf(stderr
, _("EMERG: Unable to allocate memory for pages array: %s\n"),
406 if ((pageinfo
= calloc(1, sizeof(pstops_page_t
))) == NULL
)
408 fprintf(stderr
, _("EMERG: Unable to allocate memory for page info: %s\n"),
413 pageinfo
->label
= strdup(label
);
414 pageinfo
->offset
= cupsFileTell(doc
->temp
);
416 cupsArrayAdd(doc
->pages
, pageinfo
);
425 * 'check_range()' - Check to see if the current page is selected for
429 static int /* O - 1 if selected, 0 otherwise */
430 check_range(pstops_doc_t
*doc
, /* I - Document information */
431 int page
) /* I - Page number */
433 const char *range
; /* Pointer into range string */
434 int lower
, upper
; /* Lower and upper page numbers */
440 * See if we only print even or odd pages...
443 if (!strcasecmp(doc
->page_set
, "even") && (page
& 1))
446 if (!strcasecmp(doc
->page_set
, "odd") && !(page
& 1))
450 if (!doc
->page_ranges
)
451 return (1); /* No range, print all pages... */
453 for (range
= doc
->page_ranges
; *range
!= '\0';)
459 upper
= strtol(range
, (char **)&range
, 10);
463 lower
= strtol(range
, (char **)&range
, 10);
468 if (!isdigit(*range
& 255))
471 upper
= strtol(range
, (char **)&range
, 10);
477 if (page
>= lower
&& page
<= upper
)
491 * 'copy_bytes()' - Copy bytes from the input file to stdout...
495 copy_bytes(cups_file_t
*fp
, /* I - File to read from */
496 off_t offset
, /* I - Offset to page data */
497 size_t length
) /* I - Length of page data */
499 char buffer
[8192]; /* Data buffer */
500 ssize_t nbytes
; /* Number of bytes read */
501 size_t nleft
; /* Number of bytes left/remaining */
506 if (cupsFileSeek(fp
, offset
) < 0)
509 #ifdef HAVE_LONG_LONG
510 _("ERROR: Unable to seek to offset %lld in file - %s\n"),
512 _("ERROR: Unable to seek to offset %ld in file - %s\n"),
513 #endif /* HAVE_LONG_LONG */
514 CUPS_LLCAST offset
, strerror(errno
));
518 while (nleft
> 0 || length
== 0)
520 if (nleft
> sizeof(buffer
) || length
== 0)
521 nbytes
= sizeof(buffer
);
525 if ((nbytes
= cupsFileRead(fp
, buffer
, nbytes
)) < 1)
530 fwrite(buffer
, 1, nbytes
, stdout
);
536 * 'copy_comments()' - Copy all of the comments section...
538 * This function expects "line" to be filled with a comment line.
539 * On return, "line" will contain the next line in the file, if any.
542 static size_t /* O - Length of next line */
543 copy_comments(cups_file_t
*fp
, /* I - File to read from */
544 pstops_doc_t
*doc
, /* I - Document info */
545 ppd_file_t
*ppd
, /* I - PPD file */
546 char *line
, /* I - Line buffer */
547 size_t linelen
, /* I - Length of initial line */
548 size_t linesize
) /* I - Size of line buffer */
550 int saw_bounding_box
, /* Saw %%BoundingBox: comment? */
551 saw_for
, /* Saw %%For: comment? */
552 saw_pages
, /* Saw %%Pages: comment? */
553 saw_title
; /* Saw %%Title: comment? */
557 * Loop until we see %%EndComments or a non-comment line...
560 saw_bounding_box
= 0;
565 while (line
[0] == '%')
568 * Strip trailing whitespace...
575 if (!isspace(line
[linelen
] & 255))
578 line
[linelen
] = '\0';
585 fprintf(stderr
, "DEBUG: %s\n", line
);
588 * Pull the headers out...
591 if (!strncmp(line
, "%%Pages:", 8))
593 int pages
; /* Number of pages */
597 fputs(_("ERROR: Duplicate %%Pages: comment seen!\n"), stderr
);
601 if (Duplex
&& (pages
= atoi(line
+ 8)) > 0 && pages
<= doc
->number_up
)
604 * Since we will only be printing on a single page, disable duplexing.
608 doc
->slow_duplex
= 0;
610 if (cupsGetOption("sides", doc
->num_options
, doc
->options
))
611 doc
->num_options
= cupsAddOption("sides", "one-sided",
612 doc
->num_options
, &(doc
->options
));
614 if (cupsGetOption("Duplex", doc
->num_options
, doc
->options
))
615 doc
->num_options
= cupsAddOption("Duplex", "None",
616 doc
->num_options
, &(doc
->options
));
618 if (cupsGetOption("EFDuplex", doc
->num_options
, doc
->options
))
619 doc
->num_options
= cupsAddOption("EFDuplex", "None",
620 doc
->num_options
, &(doc
->options
));
622 if (cupsGetOption("EFDuplexing", doc
->num_options
, doc
->options
))
623 doc
->num_options
= cupsAddOption("EFDuplexing", "False",
624 doc
->num_options
, &(doc
->options
));
626 if (cupsGetOption("KD03Duplex", doc
->num_options
, doc
->options
))
627 doc
->num_options
= cupsAddOption("KD03Duplex", "None",
628 doc
->num_options
, &(doc
->options
));
630 if (cupsGetOption("JCLDuplex", doc
->num_options
, doc
->options
))
631 doc
->num_options
= cupsAddOption("JCLDuplex", "None",
632 doc
->num_options
, &(doc
->options
));
634 ppdMarkOption(ppd
, "Duplex", "None");
635 ppdMarkOption(ppd
, "EFDuplex", "None");
636 ppdMarkOption(ppd
, "EFDuplexing", "False");
637 ppdMarkOption(ppd
, "KD03Duplex", "None");
638 ppdMarkOption(ppd
, "JCLDuplex", "None");
641 else if (!strncmp(line
, "%%BoundingBox:", 14))
643 if (saw_bounding_box
)
644 fputs(_("ERROR: Duplicate %%BoundingBox: comment seen!\n"), stderr
);
645 else if (strstr(line
+ 14, "(atend)"))
648 * Do nothing for now but use the default imageable area...
651 else if (sscanf(line
+ 14, "%d%d%d%d", doc
->bounding_box
+ 0,
652 doc
->bounding_box
+ 1, doc
->bounding_box
+ 2,
653 doc
->bounding_box
+ 3) != 4)
655 fputs(_("ERROR: Bad %%BoundingBox: comment seen!\n"), stderr
);
657 doc
->bounding_box
[0] = (int)PageLeft
;
658 doc
->bounding_box
[1] = (int)PageBottom
;
659 doc
->bounding_box
[2] = (int)PageRight
;
660 doc
->bounding_box
[3] = (int)PageTop
;
663 saw_bounding_box
= 1;
665 else if (!strncmp(line
, "%%For:", 6))
668 doc_printf(doc
, "%s\n", line
);
670 else if (!strncmp(line
, "%%Title:", 8))
673 doc_printf(doc
, "%s\n", line
);
675 else if (!strncmp(line
, "%cupsRotation:", 14))
678 * Reset orientation of document?
681 int orient
= (atoi(line
+ 14) / 90) & 3;
683 if (orient
!= Orientation
)
686 * Yes, update things so that the pages come out right...
689 Orientation
= (4 - Orientation
+ orient
) & 3;
691 Orientation
= orient
;
694 else if (!strcmp(line
, "%%EndComments"))
696 linelen
= cupsFileGetLine(fp
, line
, linesize
);
699 else if (strncmp(line
, "%!", 2) && strncmp(line
, "%cups", 5))
700 doc_printf(doc
, "%s\n", line
);
702 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
706 if (!saw_bounding_box
)
707 fputs(_("ERROR: No %%BoundingBox: comment in header!\n"), stderr
);
710 fputs(_("ERROR: No %%Pages: comment in header!\n"), stderr
);
713 WriteTextComment("For", doc
->user
);
716 WriteTextComment("Title", doc
->title
);
718 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
721 * Tell the document processor the copy and duplex options
722 * that are required...
725 doc_printf(doc
, "%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
726 doc
->collate
? " collate" : "",
727 Duplex
? " duplex" : "");
730 * Apple uses RBI comments for various non-PPD options...
733 doc_printf(doc
, "%%RBINumCopies: %d\n", doc
->copies
);
738 * Tell the document processor the duplex option that is required...
742 doc_puts(doc
, "%%Requirements: duplex\n");
745 * Apple uses RBI comments for various non-PPD options...
748 doc_puts(doc
, "%RBINumCopies: 1\n");
751 doc_puts(doc
, "%%Pages: (atend)\n");
752 doc_puts(doc
, "%%BoundingBox: (atend)\n");
753 doc_puts(doc
, "%%EndComments\n");
760 * 'copy_dsc()' - Copy a DSC-conforming document...
762 * This function expects "line" to be filled with the %!PS-Adobe comment line.
766 copy_dsc(cups_file_t
*fp
, /* I - File to read from */
767 pstops_doc_t
*doc
, /* I - Document info */
768 ppd_file_t
*ppd
, /* I - PPD file */
769 char *line
, /* I - Line buffer */
770 size_t linelen
, /* I - Length of initial line */
771 size_t linesize
) /* I - Size of line buffer */
773 int number
; /* Page number */
774 pstops_page_t
*pageinfo
; /* Page information */
778 * Make sure we use ESPshowpage for EPS files...
781 if (strstr(line
, "EPSF"))
783 doc
->use_ESPshowpage
= 1;
788 * Start sending the document with any commands needed...
791 fprintf(stderr
, "DEBUG: Before copy_comments - %s", line
);
792 linelen
= copy_comments(fp
, doc
, ppd
, line
, linelen
, linesize
);
795 * Now find the prolog section, if any...
798 fprintf(stderr
, "DEBUG: Before copy_prolog - %s", line
);
799 linelen
= copy_prolog(fp
, doc
, ppd
, line
, linelen
, linesize
);
802 * Then the document setup section...
805 fprintf(stderr
, "DEBUG: Before copy_setup - %s", line
);
806 linelen
= copy_setup(fp
, doc
, ppd
, line
, linelen
, linesize
);
809 * Copy until we see %%Page:...
812 while (strncmp(line
, "%%Page:", 7) && strncmp(line
, "%%Trailer", 9))
814 doc_write(doc
, line
, linelen
);
816 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
821 * Then process pages until we have no more...
826 fprintf(stderr
, "DEBUG: Before page loop - %s", line
);
827 while (!strncmp(line
, "%%Page:", 7))
831 if (check_range(doc
, (number
- 1) / doc
->number_up
+ 1))
833 fprintf(stderr
, "DEBUG: Copying page %d...\n", number
);
834 linelen
= copy_page(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
838 fprintf(stderr
, "DEBUG: Skipping page %d...\n", number
);
839 linelen
= skip_page(fp
, line
, linelen
, linesize
);
844 * Finish up the last page(s)...
847 if (number
&& is_not_last_page(number
) && cupsArrayLast(doc
->pages
))
849 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
851 start_nup(doc
, doc
->number_up
, 0, doc
->bounding_box
);
852 doc_puts(doc
, "showpage\n");
853 end_nup(doc
, doc
->number_up
);
855 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
858 if (doc
->slow_duplex
&& (doc
->page
& 1))
861 * Make sure we have an even number of pages...
864 pageinfo
= add_page(doc
, "(filler)");
866 if (!doc
->slow_order
)
868 if (!ppd
|| !ppd
->num_filters
)
869 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
870 doc
->slow_collate
? 1 : doc
->copies
);
872 printf("%%%%Page: (filler) %d\n", doc
->page
);
875 start_nup(doc
, doc
->number_up
, 0, doc
->bounding_box
);
876 doc_puts(doc
, "showpage\n");
877 end_nup(doc
, doc
->number_up
);
879 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
883 * Make additional copies as necessary...
886 number
= doc
->slow_order
? 0 : doc
->page
;
890 int copy
; /* Current copy */
894 * Reopen the temporary file for reading...
897 cupsFileClose(doc
->temp
);
899 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
905 for (copy
= !doc
->slow_order
; copy
< doc
->copies
; copy
++)
908 * Send end-of-job stuff followed by any start-of-job stuff required
909 * for the JCL options...
912 if (number
&& doc
->emit_jcl
&& ppd
&& ppd
->jcl_end
)
915 * Send the trailer...
919 printf("%%%%Pages: %d\n", cupsArrayCount(doc
->pages
));
920 if (doc
->number_up
> 1 || doc
->fitplot
)
921 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
922 PageLeft
, PageBottom
, PageRight
, PageTop
);
924 printf("%%%%BoundingBox: %d %d %d %d\n",
925 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
926 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
930 * Start a new document...
933 ppdEmitJCLEnd(ppd
, stdout
);
934 ppdEmitJCL(ppd
, stdout
, doc
->job_id
, doc
->user
, doc
->title
);
936 puts("%!PS-Adobe-3.0");
942 * Copy the prolog as needed...
947 pageinfo
= (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
948 copy_bytes(doc
->temp
, 0, pageinfo
->offset
);
952 * Then copy all of the pages...
955 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayLast(doc
->pages
) :
956 (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
962 if (!ppd
|| !ppd
->num_filters
)
963 fprintf(stderr
, "PAGE: %d 1\n", number
);
965 if (doc
->number_up
> 1)
967 printf("%%%%Page: (%d) %d\n", number
, number
);
968 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
969 PageLeft
, PageBottom
, PageRight
, PageTop
);
973 printf("%%%%Page: %s %d\n", pageinfo
->label
, number
);
974 printf("%%%%PageBoundingBox: %d %d %d %d\n",
975 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
976 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
979 copy_bytes(doc
->temp
, pageinfo
->offset
, pageinfo
->length
);
981 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayPrev(doc
->pages
) :
982 (pstops_page_t
*)cupsArrayNext(doc
->pages
);
988 * Restore the old showpage operator as needed...
991 if (doc
->use_ESPshowpage
)
992 puts("userdict/showpage/ESPshowpage load put\n");
995 * Write/copy the trailer...
998 linelen
= copy_trailer(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
1003 * 'copy_non_dsc()' - Copy a document that does not conform to the DSC...
1005 * This function expects "line" to be filled with the %! comment line.
1009 copy_non_dsc(cups_file_t
*fp
, /* I - File to read from */
1010 pstops_doc_t
*doc
, /* I - Document info */
1011 ppd_file_t
*ppd
, /* I - PPD file */
1012 char *line
, /* I - Line buffer */
1013 size_t linelen
, /* I - Length of initial line */
1014 size_t linesize
) /* I - Size of line buffer */
1016 int copy
; /* Current copy */
1017 char buffer
[8192]; /* Copy buffer */
1018 int bytes
; /* Number of bytes copied */
1022 * First let the user know that they are attempting to print a file
1023 * that may not print correctly...
1026 fputs(_("WARNING: This document does not conform to the Adobe Document "
1027 "Structuring Conventions and may not print correctly!\n"), stderr
);
1030 * Then write a standard DSC comment section...
1033 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", PageLeft
, PageBottom
,
1034 PageRight
, PageTop
);
1036 if (doc
->slow_collate
&& doc
->copies
> 1)
1037 printf("%%%%Pages: %d\n", doc
->copies
);
1041 WriteTextComment("For", doc
->user
);
1042 WriteTextComment("Title", doc
->title
);
1044 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1047 * Tell the document processor the copy and duplex options
1048 * that are required...
1051 printf("%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
1052 doc
->collate
? " collate" : "",
1053 Duplex
? " duplex" : "");
1056 * Apple uses RBI comments for various non-PPD options...
1059 printf("%%RBINumCopies: %d\n", doc
->copies
);
1064 * Tell the document processor the duplex option that is required...
1068 puts("%%Requirements: duplex");
1071 * Apple uses RBI comments for various non-PPD options...
1074 puts("%RBINumCopies: 1");
1077 puts("%%EndComments");
1080 * Then the prolog...
1083 puts("%%BeginProlog");
1085 do_prolog(doc
, ppd
);
1087 puts("%%EndProlog");
1090 * Then the setup section...
1093 puts("%%BeginSetup");
1100 * Finally, embed a copy of the file inside a %%Page...
1103 if (!ppd
|| !ppd
->num_filters
)
1104 fprintf(stderr
, "PAGE: 1 %d\n", doc
->temp
? 1 : doc
->copies
);
1106 puts("%%Page: 1 1");
1107 puts("%%BeginPageSetup");
1108 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1109 puts("%%EndPageSetup");
1110 puts("%%BeginDocument: nondsc");
1112 fwrite(line
, linelen
, 1, stdout
);
1115 cupsFileWrite(doc
->temp
, line
, linelen
);
1117 while ((bytes
= cupsFileRead(fp
, buffer
, sizeof(buffer
))) > 0)
1119 fwrite(buffer
, 1, bytes
, stdout
);
1122 cupsFileWrite(doc
->temp
, buffer
, bytes
);
1125 puts("%%EndDocument");
1127 if (doc
->use_ESPshowpage
)
1129 WriteLabels(Orientation
);
1130 puts("ESPshowpage");
1136 * Reopen the temporary file for reading...
1139 cupsFileClose(doc
->temp
);
1141 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
1144 * Make the additional copies as needed...
1147 for (copy
= 1; copy
< doc
->copies
; copy
++)
1149 if (!ppd
|| !ppd
->num_filters
)
1150 fputs("PAGE: 1 1\n", stderr
);
1152 printf("%%%%Page: %d %d\n", copy
+ 1, copy
+ 1);
1153 puts("%%BeginPageSetup");
1154 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1155 puts("%%EndPageSetup");
1156 puts("%%BeginDocument: nondsc");
1158 copy_bytes(doc
->temp
, 0, 0);
1160 puts("%%EndDocument");
1162 if (doc
->use_ESPshowpage
)
1164 WriteLabels(Orientation
);
1165 puts("ESPshowpage");
1171 * Restore the old showpage operator as needed...
1174 if (doc
->use_ESPshowpage
)
1175 puts("userdict/showpage/ESPshowpage load put\n");
1180 * 'copy_page()' - Copy a page description...
1182 * This function expects "line" to be filled with a %%Page comment line.
1183 * On return, "line" will contain the next line in the file, if any.
1186 static size_t /* O - Length of next line */
1187 copy_page(cups_file_t
*fp
, /* I - File to read from */
1188 pstops_doc_t
*doc
, /* I - Document info */
1189 ppd_file_t
*ppd
, /* I - PPD file */
1190 int number
, /* I - Current page number */
1191 char *line
, /* I - Line buffer */
1192 size_t linelen
, /* I - Length of initial line */
1193 size_t linesize
) /* I - Size of line buffer */
1195 char label
[256], /* Page label string */
1196 *ptr
; /* Pointer into line */
1197 int level
; /* Embedded document level */
1198 pstops_page_t
*pageinfo
; /* Page information */
1199 int first_page
; /* First page on N-up output? */
1200 int bounding_box
[4]; /* PageBoundingBox */
1204 * Get the page label for this page...
1207 first_page
= is_first_page(number
);
1209 if (!parse_text(line
+ 7, &ptr
, label
, sizeof(label
)))
1211 fputs(_("ERROR: Bad %%Page: comment in file!\n"), stderr
);
1215 else if (strtol(ptr
, &ptr
, 10) == LONG_MAX
|| !isspace(*ptr
& 255))
1217 fputs(_("ERROR: Bad %%Page: comment in file!\n"), stderr
);
1222 * Create or update the current output page...
1226 pageinfo
= add_page(doc
, label
);
1228 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
1231 * Handle first page override...
1234 if (doc
->ap_input_slot
|| doc
->ap_manual_feed
)
1239 * First page/sheet gets AP_FIRSTPAGE_* options...
1242 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->ap_input_slot
,
1243 pageinfo
->num_options
,
1244 &(pageinfo
->options
));
1245 pageinfo
->num_options
= cupsAddOption("ManualFeed",
1246 doc
->ap_input_slot
? "False" :
1247 doc
->ap_manual_feed
,
1248 pageinfo
->num_options
,
1249 &(pageinfo
->options
));
1251 else if (doc
->page
== (Duplex
+ 2))
1254 * Second page/sheet gets default options...
1257 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->input_slot
,
1258 pageinfo
->num_options
,
1259 &(pageinfo
->options
));
1260 pageinfo
->num_options
= cupsAddOption("ManualFeed",
1261 doc
->input_slot
? "False" :
1263 pageinfo
->num_options
,
1264 &(pageinfo
->options
));
1269 * Scan comments until we see something other than %%Page*: or
1273 memcpy(bounding_box
, doc
->bounding_box
, sizeof(bounding_box
));
1275 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1277 if (!strncmp(line
, "%%PageBoundingBox:", 18))
1280 * %%PageBoundingBox: llx lly urx ury
1283 if (sscanf(line
+ 18, "%d%d%d%d", bounding_box
+ 0,
1284 bounding_box
+ 1, bounding_box
+ 2,
1285 bounding_box
+ 3) != 4)
1287 fputs(_("ERROR: Bad %%PageBoundingBox: comment in file!\n"), stderr
);
1288 memcpy(bounding_box
, doc
->bounding_box
,
1289 sizeof(bounding_box
));
1291 else if (doc
->number_up
== 1 && !doc
->fitplot
&& Orientation
)
1293 int temp_bbox
[4]; /* Temporary bounding box */
1296 memcpy(temp_bbox
, bounding_box
, sizeof(temp_bbox
));
1298 fprintf(stderr
, "DEBUG: Orientation = %d\n", Orientation
);
1299 fprintf(stderr
, "DEBUG: original bounding_box = [ %d %d %d %d ]\n",
1300 bounding_box
[0], bounding_box
[1],
1301 bounding_box
[2], bounding_box
[3]);
1302 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
1303 PageWidth
, PageLength
);
1305 switch (Orientation
)
1307 case 1 : /* Landscape */
1308 bounding_box
[0] = PageLength
- temp_bbox
[3];
1309 bounding_box
[1] = temp_bbox
[0];
1310 bounding_box
[2] = PageLength
- temp_bbox
[1];
1311 bounding_box
[3] = temp_bbox
[2];
1314 case 2 : /* Reverse Portrait */
1315 bounding_box
[0] = PageWidth
- temp_bbox
[2];
1316 bounding_box
[1] = PageLength
- temp_bbox
[3];
1317 bounding_box
[2] = PageWidth
- temp_bbox
[0];
1318 bounding_box
[3] = PageLength
- temp_bbox
[1];
1321 case 3 : /* Reverse Landscape */
1322 bounding_box
[0] = temp_bbox
[1];
1323 bounding_box
[1] = PageWidth
- temp_bbox
[2];
1324 bounding_box
[2] = temp_bbox
[3];
1325 bounding_box
[3] = PageWidth
- temp_bbox
[0];
1329 fprintf(stderr
, "DEBUG: updated bounding_box = [ %d %d %d %d ]\n",
1330 bounding_box
[0], bounding_box
[1],
1331 bounding_box
[2], bounding_box
[3]);
1335 else if (!strncmp(line
, "%%PageCustomColors:", 19) ||
1336 !strncmp(line
, "%%PageMedia:", 12) ||
1337 !strncmp(line
, "%%PageOrientation:", 18) ||
1338 !strncmp(line
, "%%PageProcessColors:", 20) ||
1339 !strncmp(line
, "%%PageRequirements:", 18) ||
1340 !strncmp(line
, "%%PageResources:", 16))
1347 else if (!strncmp(line
, "%%PageCustomColors:", 19))
1350 * %%PageCustomColors: ...
1353 else if (!strncmp(line
, "%%PageMedia:", 12))
1359 else if (!strncmp(line
, "%%PageOrientation:", 18))
1362 * %%PageOrientation: ...
1365 else if (!strncmp(line
, "%%PageProcessColors:", 20))
1368 * %%PageProcessColors: ...
1371 else if (!strncmp(line
, "%%PageRequirements:", 18))
1374 * %%PageRequirements: ...
1377 else if (!strncmp(line
, "%%PageResources:", 16))
1380 * %%PageResources: ...
1383 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1386 * %%IncludeFeature: *MainKeyword OptionKeyword
1389 if (doc
->number_up
== 1 &&!doc
->fitplot
)
1390 pageinfo
->num_options
= include_feature(ppd
, line
,
1391 pageinfo
->num_options
,
1392 &(pageinfo
->options
));
1394 else if (strncmp(line
, "%%Include", 9))
1398 if (doc
->number_up
== 1)
1401 * Update the document's composite and page bounding box...
1404 memcpy(pageinfo
->bounding_box
, bounding_box
,
1405 sizeof(pageinfo
->bounding_box
));
1407 if (bounding_box
[0] < doc
->new_bounding_box
[0])
1408 doc
->new_bounding_box
[0] = bounding_box
[0];
1409 if (bounding_box
[1] < doc
->new_bounding_box
[1])
1410 doc
->new_bounding_box
[1] = bounding_box
[1];
1411 if (bounding_box
[2] > doc
->new_bounding_box
[2])
1412 doc
->new_bounding_box
[2] = bounding_box
[2];
1413 if (bounding_box
[3] > doc
->new_bounding_box
[3])
1414 doc
->new_bounding_box
[3] = bounding_box
[3];
1418 * Output the page header as needed...
1421 if (!doc
->slow_order
&& first_page
)
1423 if (!ppd
|| !ppd
->num_filters
)
1424 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
1425 doc
->slow_collate
? 1 : doc
->copies
);
1427 if (doc
->number_up
> 1)
1429 printf("%%%%Page: (%d) %d\n", doc
->page
, doc
->page
);
1430 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1431 PageLeft
, PageBottom
, PageRight
, PageTop
);
1435 printf("%%%%Page: %s %d\n", pageinfo
->label
, doc
->page
);
1436 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1437 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
1438 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
1443 * Copy any page setup commands...
1448 char *page_setup
; /* PageSetup commands to send */
1451 doc_puts(doc
, "%%BeginPageSetup\n");
1453 if (pageinfo
->num_options
> 0)
1455 int i
; /* Looping var */
1456 ppd_option_t
*option
; /* PPD option */
1457 int min_order
; /* Minimum OrderDependency value */
1458 char *doc_setup
, /* DocumentSetup commands to send */
1459 *any_setup
; /* AnySetup commands to send */
1463 * Yes, figure out the minimum OrderDependency value...
1466 if ((option
= ppdFindOption(ppd
, "PageRegion")) != NULL
)
1467 min_order
= option
->order
;
1471 for (i
= 0; i
< pageinfo
->num_options
; i
++)
1472 if ((option
= ppdFindOption(ppd
, pageinfo
->options
[i
].name
)) != NULL
&&
1473 option
->order
< min_order
)
1474 min_order
= option
->order
;
1477 * Mark and extract them...
1480 cupsMarkOptions(ppd
, pageinfo
->num_options
, pageinfo
->options
);
1482 doc_setup
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, min_order
);
1483 any_setup
= ppdEmitString(ppd
, PPD_ORDER_ANY
, min_order
);
1486 * Then send them out...
1491 doc_puts(doc
, doc_setup
);
1497 doc_puts(doc
, any_setup
);
1503 * Output commands for the current page...
1506 page_setup
= ppdEmitString(ppd
, PPD_ORDER_PAGE
, 0);
1510 doc_puts(doc
, page_setup
);
1516 * Prep for the start of the page description...
1519 start_nup(doc
, number
, 1, bounding_box
);
1522 * Copy page setup commands as needed...
1525 if (!strncmp(line
, "%%BeginPageSetup", 16))
1527 int feature
= 0; /* In a Begin/EndFeature block? */
1530 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1532 if (!strncmp(line
, "%%EndPageSetup", 14))
1534 else if (!strncmp(line
, "%%BeginFeature:", 15))
1538 if (doc
->number_up
> 1 || doc
->fitplot
)
1541 else if (!strncmp(line
, "%%EndFeature", 12))
1545 if (doc
->number_up
> 1 || doc
->fitplot
)
1548 else if (!strncmp(line
, "%%Include", 9))
1551 if (!feature
|| (doc
->number_up
== 1 && !doc
->fitplot
))
1552 doc_write(doc
, line
, linelen
);
1556 * Skip %%EndPageSetup...
1560 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1564 * Finish the PageSetup section as needed...
1568 doc_puts(doc
, "%%EndPageSetup\n");
1571 * Read the rest of the page description...
1579 (!strncmp(line
, "%%Page:", 7) ||
1580 !strncmp(line
, "%%Trailer", 9) ||
1581 !strncmp(line
, "%%EOF", 5)))
1583 else if (!strncmp(line
, "%%BeginDocument", 15) ||
1584 !strncmp(line
, "%ADO_BeginApplication", 21))
1586 doc_write(doc
, line
, linelen
);
1590 else if ((!strncmp(line
, "%%EndDocument", 13) ||
1591 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
1593 doc_write(doc
, line
, linelen
);
1597 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
1598 (!strncmp(line
, "%%BeginData:", 12) &&
1599 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
1602 * Copy binary data...
1605 int bytes
; /* Bytes of data */
1608 doc_write(doc
, line
, linelen
);
1610 bytes
= atoi(strchr(line
, ':') + 1);
1614 if (bytes
> linesize
)
1615 linelen
= cupsFileRead(fp
, line
, linesize
);
1617 linelen
= cupsFileRead(fp
, line
, bytes
);
1622 perror("ERROR: Early end-of-file while reading binary data");
1626 doc_write(doc
, line
, linelen
);
1632 doc_write(doc
, line
, linelen
);
1634 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0);
1637 * Finish up this page and return...
1640 end_nup(doc
, number
);
1642 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
1649 * 'copy_prolog()' - Copy the document prolog section...
1651 * This function expects "line" to be filled with a %%BeginProlog comment line.
1652 * On return, "line" will contain the next line in the file, if any.
1655 static size_t /* O - Length of next line */
1656 copy_prolog(cups_file_t
*fp
, /* I - File to read from */
1657 pstops_doc_t
*doc
, /* I - Document info */
1658 ppd_file_t
*ppd
, /* I - PPD file */
1659 char *line
, /* I - Line buffer */
1660 size_t linelen
, /* I - Length of initial line */
1661 size_t linesize
) /* I - Size of line buffer */
1663 while (strncmp(line
, "%%BeginProlog", 13))
1665 if (!strncmp(line
, "%%BeginSetup", 12) || !strncmp(line
, "%%Page:", 7))
1668 doc_write(doc
, line
, linelen
);
1670 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1674 doc_puts(doc
, "%%BeginProlog\n");
1676 do_prolog(doc
, ppd
);
1678 if (!strncmp(line
, "%%BeginProlog", 13))
1680 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1682 if (!strncmp(line
, "%%EndProlog", 11) ||
1683 !strncmp(line
, "%%BeginSetup", 12) ||
1684 !strncmp(line
, "%%Page:", 7))
1687 doc_write(doc
, line
, linelen
);
1690 if (!strncmp(line
, "%%EndProlog", 11))
1691 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1693 fputs(_("ERROR: Missing %%EndProlog!\n"), stderr
);
1696 doc_puts(doc
, "%%EndProlog\n");
1703 * 'copy_setup()' - Copy the document setup section...
1705 * This function expects "line" to be filled with a %%BeginSetup comment line.
1706 * On return, "line" will contain the next line in the file, if any.
1709 static size_t /* O - Length of next line */
1710 copy_setup(cups_file_t
*fp
, /* I - File to read from */
1711 pstops_doc_t
*doc
, /* I - Document info */
1712 ppd_file_t
*ppd
, /* I - PPD file */
1713 char *line
, /* I - Line buffer */
1714 size_t linelen
, /* I - Length of initial line */
1715 size_t linesize
) /* I - Size of line buffer */
1717 while (strncmp(line
, "%%BeginSetup", 12))
1719 if (!strncmp(line
, "%%Page:", 7))
1722 doc_write(doc
, line
, linelen
);
1724 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1728 doc_puts(doc
, "%%BeginSetup\n");
1732 if (!strncmp(line
, "%%BeginSetup", 12))
1734 while (strncmp(line
, "%%EndSetup", 10))
1736 if (!strncmp(line
, "%%Page:", 7))
1738 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1741 * %%IncludeFeature: *MainKeyword OptionKeyword
1744 if (doc
->number_up
== 1 && !doc
->fitplot
)
1745 doc
->num_options
= include_feature(ppd
, line
, doc
->num_options
,
1748 else if (strncmp(line
, "%%BeginSetup", 12))
1749 doc_write(doc
, line
, linelen
);
1751 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1755 if (!strncmp(line
, "%%EndSetup", 10))
1756 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1758 fputs(_("ERROR: Missing %%EndSetup!\n"), stderr
);
1761 doc_puts(doc
, "%%EndSetup\n");
1768 * 'copy_trailer()' - Copy the document trailer...
1770 * This function expects "line" to be filled with a %%Trailer comment line.
1771 * On return, "line" will contain the next line in the file, if any.
1774 static size_t /* O - Length of next line */
1775 copy_trailer(cups_file_t
*fp
, /* I - File to read from */
1776 pstops_doc_t
*doc
, /* I - Document info */
1777 ppd_file_t
*ppd
, /* I - PPD file */
1778 int number
, /* I - Number of pages */
1779 char *line
, /* I - Line buffer */
1780 size_t linelen
, /* I - Length of initial line */
1781 size_t linesize
) /* I - Size of line buffer */
1784 * Write the trailer comments...
1791 if (!strncmp(line
, "%%EOF", 5))
1793 else if (strncmp(line
, "%%Trailer", 9) &&
1794 strncmp(line
, "%%Pages:", 8) &&
1795 strncmp(line
, "%%BoundingBox:", 14))
1796 fwrite(line
, 1, linelen
, stdout
);
1798 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1801 fprintf(stderr
, "DEBUG: Wrote %d pages...\n", number
);
1803 printf("%%%%Pages: %d\n", number
);
1804 if (doc
->number_up
> 1 || doc
->fitplot
)
1805 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
1806 PageLeft
, PageBottom
, PageRight
, PageTop
);
1808 printf("%%%%BoundingBox: %d %d %d %d\n",
1809 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
1810 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
1817 * 'do_prolog()' - Send the necessary document prolog commands...
1821 do_prolog(pstops_doc_t
*doc
, /* I - Document information */
1822 ppd_file_t
*ppd
) /* I - PPD file */
1824 char *ps
; /* PS commands */
1828 * Send the document prolog commands...
1831 if (ppd
&& ppd
->patches
)
1833 doc_puts(doc
, "%%BeginFeature: *JobPatchFile 1\n");
1834 doc_puts(doc
, ppd
->patches
);
1835 doc_puts(doc
, "\n%%EndFeature\n");
1838 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_PROLOG
, 0.0)) != NULL
)
1845 * Define ESPshowpage here so that applications that define their
1846 * own procedure to do a showpage pick it up...
1849 if (doc
->use_ESPshowpage
)
1850 doc_puts(doc
, "userdict/ESPshowpage/showpage load put\n"
1851 "userdict/showpage{}put\n");
1856 * 'do_setup()' - Send the necessary document setup commands...
1860 do_setup(pstops_doc_t
*doc
, /* I - Document information */
1861 ppd_file_t
*ppd
) /* I - PPD file */
1863 char *ps
; /* PS commands */
1867 * Disable CTRL-D so that embedded files don't cause printing
1871 doc_puts(doc
, "% Disable CTRL-D as an end-of-file marker...\n");
1872 doc_puts(doc
, "userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put\n");
1875 * Mark any options from %%IncludeFeature: comments...
1878 cupsMarkOptions(ppd
, doc
->num_options
, doc
->options
);
1881 * Send all the printer-specific setup commands...
1884 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, 0.0)) != NULL
)
1890 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_ANY
, 0.0)) != NULL
)
1897 * Set the number of copies for the job...
1900 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1902 doc_printf(doc
, "%%RBIBeginNonPPDFeature: *NumCopies %d\n", doc
->copies
);
1904 "%d/languagelevel where{pop languagelevel 2 ge}{false}ifelse\n"
1905 "{1 dict begin/NumCopies exch def currentdict end "
1907 "{userdict/#copies 3 -1 roll put}ifelse\n", doc
->copies
);
1908 doc_puts(doc
, "%RBIEndNonPPDFeature\n");
1912 * If we are doing N-up printing, disable setpagedevice...
1915 if (doc
->number_up
> 1)
1916 doc_puts(doc
, "userdict/setpagedevice{pop}bind put\n");
1919 * Changes to the transfer function must be made AFTER any
1920 * setpagedevice code...
1923 if (doc
->gamma
!= 1.0f
|| doc
->brightness
!= 1.0f
)
1924 doc_printf(doc
, "{ neg 1 add dup 0 lt { pop 1 } { %.3f exp neg 1 add } "
1925 "ifelse %.3f mul } bind settransfer\n",
1926 doc
->gamma
, doc
->brightness
);
1929 * Make sure we have rectclip and rectstroke procedures of some sort...
1933 "% x y w h ESPrc - Clip to a rectangle.\n"
1934 "userdict/ESPrc/rectclip where{pop/rectclip load}\n"
1935 "{{newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
1936 "neg 0 rlineto closepath clip newpath}bind}ifelse put\n");
1939 "% x y w h ESPrf - Fill a rectangle.\n"
1940 "userdict/ESPrf/rectfill where{pop/rectfill load}\n"
1941 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
1942 "neg 0 rlineto closepath fill grestore}bind}ifelse put\n");
1945 "% x y w h ESPrs - Stroke a rectangle.\n"
1946 "userdict/ESPrs/rectstroke where{pop/rectstroke load}\n"
1947 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
1948 "neg 0 rlineto closepath stroke grestore}bind}ifelse put\n");
1951 * Write the page and label prologs...
1954 if (doc
->number_up
== 2 || doc
->number_up
== 6)
1957 * For 2- and 6-up output, rotate the labels to match the orientation
1961 if (Orientation
& 1)
1962 write_label_prolog(doc
, doc
->page_label
, PageBottom
,
1963 PageWidth
- PageLength
+ PageTop
, PageLength
);
1965 write_label_prolog(doc
, doc
->page_label
, PageLeft
, PageRight
,
1969 write_label_prolog(doc
, doc
->page_label
, PageBottom
, PageTop
, PageWidth
);
1974 * 'doc_printf()' - Send a formatted string to stdout and/or the temp file.
1976 * This function should be used for all page-level output that is affected
1977 * by ordering, collation, etc.
1981 doc_printf(pstops_doc_t
*doc
, /* I - Document information */
1982 const char *format
, /* I - Printf-style format string */
1983 ...) /* I - Additional arguments as needed */
1985 va_list ap
; /* Pointer to arguments */
1986 char buffer
[1024]; /* Output buffer */
1987 size_t bytes
; /* Number of bytes to write */
1990 va_start(ap
, format
);
1991 bytes
= vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
1994 if (bytes
> sizeof(buffer
))
1997 _("ERROR: doc_printf overflow (%d bytes) detected, aborting!\n"),
2002 doc_write(doc
, buffer
, bytes
);
2007 * 'doc_puts()' - Send a nul-terminated string to stdout and/or the temp file.
2009 * This function should be used for all page-level output that is affected
2010 * by ordering, collation, etc.
2014 doc_puts(pstops_doc_t
*doc
, /* I - Document information */
2015 const char *s
) /* I - String to send */
2017 doc_write(doc
, s
, strlen(s
));
2022 * 'doc_write()' - Send data to stdout and/or the temp file.
2026 doc_write(pstops_doc_t
*doc
, /* I - Document information */
2027 const char *s
, /* I - Data to send */
2028 size_t len
) /* I - Number of bytes to send */
2030 if (!doc
->slow_order
)
2031 fwrite(s
, 1, len
, stdout
);
2034 cupsFileWrite(doc
->temp
, s
, len
);
2039 * 'end_nup()' - End processing for N-up printing...
2043 end_nup(pstops_doc_t
*doc
, /* I - Document information */
2044 int number
) /* I - Page number */
2046 if (doc
->number_up
> 1)
2047 doc_puts(doc
, "userdict/ESPsave get restore\n");
2049 switch (doc
->number_up
)
2052 if (doc
->use_ESPshowpage
)
2054 write_labels(doc
, Orientation
);
2055 doc_puts(doc
, "ESPshowpage\n");
2061 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2063 if (Orientation
& 1)
2066 * Rotate the labels back to portrait...
2069 write_labels(doc
, Orientation
- 1);
2071 else if (Orientation
== 0)
2074 * Rotate the labels to landscape...
2077 write_labels(doc
, doc
->normal_landscape
? 1 : 3);
2082 * Rotate the labels to landscape...
2085 write_labels(doc
, doc
->normal_landscape
? 3 : 1);
2088 doc_puts(doc
, "ESPshowpage\n");
2093 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2095 write_labels(doc
, Orientation
);
2096 doc_puts(doc
, "ESPshowpage\n");
2106 * 'include_feature()' - Include a printer option/feature command.
2109 static int /* O - New number of options */
2111 ppd_file_t
*ppd
, /* I - PPD file */
2112 const char *line
, /* I - DSC line */
2113 int num_options
, /* I - Number of options */
2114 cups_option_t
**options
) /* IO - Options */
2116 char name
[255], /* Option name */
2117 value
[255]; /* Option value */
2118 ppd_option_t
*option
; /* Option in file */
2119 ppd_choice_t
*choice
; /* Choice */
2123 * Get the "%%IncludeFeature: *Keyword OptionKeyword" values...
2126 if (sscanf(line
+ 17, "%254s%254s", name
, value
) != 2)
2128 fputs(_("ERROR: Bad %%IncludeFeature: comment!\n"), stderr
);
2129 return (num_options
);
2133 * Find the option and choice...
2136 if ((option
= ppdFindOption(ppd
, name
+ 1)) == NULL
)
2138 fprintf(stderr
, _("WARNING: Unknown option \"%s\"!\n"), name
+ 1);
2139 return (num_options
);
2142 if (option
->section
== PPD_ORDER_EXIT
||
2143 option
->section
== PPD_ORDER_JCL
)
2145 fprintf(stderr
, _("WARNING: Option \"%s\" cannot be included via "
2146 "IncludeFeature!\n"), name
+ 1);
2147 return (num_options
);
2150 if ((choice
= ppdFindChoice(option
, value
)) == NULL
)
2152 fprintf(stderr
, _("WARNING: Unknown choice \"%s\" for option \"%s\"!\n"),
2154 return (num_options
);
2158 * Add the option to the option array and return...
2161 return (cupsAddOption(name
+ 1, value
, num_options
, options
));
2166 * 'parse_text()' - Parse a text value in a comment...
2168 * This function parses a DSC text value as defined on page 36 of the
2169 * DSC specification. Text values are either surrounded by parenthesis
2170 * or whitespace-delimited.
2172 * The value returned is the literal characters for the entire text
2173 * string, including any parenthesis and escape characters.
2176 static char * /* O - Value or NULL on error */
2177 parse_text(const char *start
, /* I - Start of text value */
2178 char **end
, /* O - End of text value */
2179 char *buffer
, /* I - Buffer */
2180 size_t bufsize
) /* I - Size of buffer */
2182 char *bufptr
, /* Pointer in buffer */
2183 *bufend
; /* End of buffer */
2184 int level
; /* Parenthesis level */
2188 * Skip leading whitespace...
2191 while (isspace(*start
& 255))
2195 * Then copy the value...
2200 bufend
= buffer
+ bufsize
- 1;
2202 while (bufptr
< bufend
)
2204 if (isspace(*start
& 255) && !level
)
2211 else if (*start
== ')')
2221 else if (*start
== '\\')
2224 * Copy escaped character...
2227 int i
; /* Looping var */
2231 i
<= 3 && isdigit(start
[i
] & 255) && bufptr
< bufend
;
2232 *bufptr
++ = start
[i
], i
++);
2241 * Return the value and new pointer into the line...
2245 *end
= (char *)start
;
2247 if (bufptr
== bufend
)
2255 * 'set_pstops_options()' - Set pstops options...
2260 pstops_doc_t
*doc
, /* I - Document information */
2261 ppd_file_t
*ppd
, /* I - PPD file */
2262 char *argv
[], /* I - Command-line arguments */
2263 int num_options
, /* I - Number of options */
2264 cups_option_t
*options
) /* I - Options */
2266 const char *val
; /* Option value */
2267 int intval
; /* Integer option value */
2268 ppd_attr_t
*attr
; /* PPD attribute */
2269 ppd_option_t
*option
; /* PPD option */
2270 ppd_choice_t
*choice
; /* PPD choice */
2274 * Initialize document information structure...
2277 memset(doc
, 0, sizeof(pstops_doc_t
));
2279 doc
->job_id
= atoi(argv
[1]);
2280 doc
->user
= argv
[2];
2281 doc
->title
= argv
[3];
2282 doc
->copies
= atoi(argv
[4]);
2284 if (ppd
&& ppd
->landscape
> 0)
2285 doc
->normal_landscape
= 1;
2287 doc
->bounding_box
[0] = (int)PageLeft
;
2288 doc
->bounding_box
[1] = (int)PageBottom
;
2289 doc
->bounding_box
[2] = (int)PageRight
;
2290 doc
->bounding_box
[3] = (int)PageTop
;
2292 doc
->new_bounding_box
[0] = INT_MAX
;
2293 doc
->new_bounding_box
[1] = INT_MAX
;
2294 doc
->new_bounding_box
[2] = INT_MIN
;
2295 doc
->new_bounding_box
[3] = INT_MIN
;
2298 * AP_FIRSTPAGE_InputSlot
2301 doc
->ap_input_slot
= cupsGetOption("AP_FIRSTPAGE_InputSlot", num_options
,
2305 * AP_FIRSTPAGE_ManualFeed
2308 doc
->ap_manual_feed
= cupsGetOption("AP_FIRSTPAGE_ManualFeed", num_options
,
2315 if ((val
= cupsGetOption("brightness", num_options
, options
)) != NULL
)
2318 * Get brightness value from 10 to 1000.
2323 if (intval
< 10 || intval
> 1000)
2325 fprintf(stderr
, _("ERROR: Unsupported brightness value %s, using "
2326 "brightness=100!\n"), val
);
2327 doc
->brightness
= 1.0f
;
2330 doc
->brightness
= intval
* 0.01f
;
2333 doc
->brightness
= 1.0f
;
2336 * collate, multiple-document-handling
2339 if ((val
= cupsGetOption("multiple-document-handling", num_options
, options
)) != NULL
)
2342 * This IPP attribute is unnecessarily complicated...
2344 * single-document, separate-documents-collated-copies, and
2345 * single-document-new-sheet all require collated copies.
2347 * separate-documents-uncollated-copies allows for uncollated copies.
2350 doc
->collate
= strcasecmp(val
, "separate-documents-uncollated-copies") != 0;
2353 if ((val
= cupsGetOption("Collate", num_options
, options
)) != NULL
&&
2354 (!strcasecmp(val
, "true") ||!strcasecmp(val
, "on") ||
2355 !strcasecmp(val
, "yes")))
2362 if ((val
= cupsGetOption("emit-jcl", num_options
, options
)) != NULL
&&
2363 (!strcasecmp(val
, "false") || !strcasecmp(val
, "off") ||
2364 !strcasecmp(val
, "no") || !strcmp(val
, "0")))
2373 if ((val
= cupsGetOption("fitplot", num_options
, options
)) != NULL
&&
2374 (!strcasecmp(val
, "true") || !strcasecmp(val
, "on") ||
2375 !strcasecmp(val
, "yes")))
2382 if ((val
= cupsGetOption("gamma", num_options
, options
)) != NULL
)
2385 * Get gamma value from 1 to 10000...
2390 if (intval
< 1 || intval
> 10000)
2392 fprintf(stderr
, _("ERROR: Unsupported gamma value %s, using "
2393 "gamma=1000!\n"), val
);
2397 doc
->gamma
= intval
* 0.001f
;
2406 if ((choice
= ppdFindMarkedChoice(ppd
, "InputSlot")) != NULL
)
2407 doc
->input_slot
= choice
->choice
;
2413 if ((choice
= ppdFindMarkedChoice(ppd
, "ManualFeed")) != NULL
)
2414 doc
->manual_feed
= choice
->choice
;
2416 if ((choice
= ppdFindMarkedChoice(ppd
, "MirrorPrint")) != NULL
)
2418 val
= choice
->choice
;
2422 val
= cupsGetOption("mirror", num_options
, options
);
2424 if (val
&& (!strcasecmp(val
, "true") || !strcasecmp(val
, "on") ||
2425 !strcasecmp(val
, "yes")))
2432 if ((val
= cupsGetOption("number-up", num_options
, options
)) != NULL
)
2434 switch (intval
= atoi(val
))
2442 doc
->number_up
= intval
;
2446 _("ERROR: Unsupported number-up value %d, using "
2447 "number-up=1!\n"), intval
);
2459 if ((val
= cupsGetOption("number-up-layout", num_options
, options
)) != NULL
)
2461 if (!strcasecmp(val
, "lrtb"))
2462 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2463 else if (!strcasecmp(val
, "lrbt"))
2464 doc
->number_up_layout
= PSTOPS_LAYOUT_LRBT
;
2465 else if (!strcasecmp(val
, "rltb"))
2466 doc
->number_up_layout
= PSTOPS_LAYOUT_RLTB
;
2467 else if (!strcasecmp(val
, "rlbt"))
2468 doc
->number_up_layout
= PSTOPS_LAYOUT_RLBT
;
2469 else if (!strcasecmp(val
, "tblr"))
2470 doc
->number_up_layout
= PSTOPS_LAYOUT_TBLR
;
2471 else if (!strcasecmp(val
, "tbrl"))
2472 doc
->number_up_layout
= PSTOPS_LAYOUT_TBRL
;
2473 else if (!strcasecmp(val
, "btlr"))
2474 doc
->number_up_layout
= PSTOPS_LAYOUT_BTLR
;
2475 else if (!strcasecmp(val
, "btrl"))
2476 doc
->number_up_layout
= PSTOPS_LAYOUT_BTRL
;
2479 fprintf(stderr
, _("ERROR: Unsupported number-up-layout value %s, using "
2480 "number-up-layout=lrtb!\n"), val
);
2481 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2485 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2491 if ((val
= cupsGetOption("OutputOrder", num_options
, options
)) != NULL
)
2493 if (!strcasecmp(val
, "Reverse"))
2494 doc
->output_order
= 1;
2499 * Figure out the right default output order from the PPD file...
2502 if ((choice
= ppdFindMarkedChoice(ppd
, "OutputBin")) != NULL
&&
2503 (attr
= ppdFindAttr(ppd
, "PageStackOrder", choice
->choice
)) != NULL
&&
2505 doc
->output_order
= !strcasecmp(attr
->value
, "Reverse");
2506 else if ((attr
= ppdFindAttr(ppd
, "DefaultOutputOrder", NULL
)) != NULL
&&
2508 doc
->output_order
= !strcasecmp(attr
->value
, "Reverse");
2515 if ((val
= cupsGetOption("page-border", num_options
, options
)) != NULL
)
2517 if (!strcasecmp(val
, "none"))
2518 doc
->page_border
= PSTOPS_BORDERNONE
;
2519 else if (!strcasecmp(val
, "single"))
2520 doc
->page_border
= PSTOPS_BORDERSINGLE
;
2521 else if (!strcasecmp(val
, "single-thick"))
2522 doc
->page_border
= PSTOPS_BORDERSINGLE2
;
2523 else if (!strcasecmp(val
, "double"))
2524 doc
->page_border
= PSTOPS_BORDERDOUBLE
;
2525 else if (!strcasecmp(val
, "double-thick"))
2526 doc
->page_border
= PSTOPS_BORDERDOUBLE2
;
2529 fprintf(stderr
, _("ERROR: Unsupported page-border value %s, using "
2530 "page-border=none!\n"), val
);
2531 doc
->page_border
= PSTOPS_BORDERNONE
;
2535 doc
->page_border
= PSTOPS_BORDERNONE
;
2541 doc
->page_label
= cupsGetOption("page-label", num_options
, options
);
2547 doc
->page_ranges
= cupsGetOption("page-ranges", num_options
, options
);
2553 doc
->page_set
= cupsGetOption("page-set", num_options
, options
);
2556 * Now figure out if we have to force collated copies, etc.
2559 if (ppd
&& ppd
->manual_copies
&& Duplex
&& doc
->copies
> 1)
2562 * Force collated copies when printing a duplexed document to
2563 * a non-PS printer that doesn't do hardware copy generation.
2564 * Otherwise the copies will end up on the front/back side of
2572 * See if we have to filter the fast or slow way...
2575 if (doc
->collate
&& doc
->copies
> 1)
2578 * See if we need to manually collate the pages...
2581 doc
->slow_collate
= 1;
2583 if ((choice
= ppdFindMarkedChoice(ppd
, "Collate")) != NULL
&&
2584 !strcasecmp(choice
->choice
, "True"))
2587 * Hardware collate option is selected, see if the option is
2588 * conflicting - if not, collate in hardware. Otherwise,
2589 * turn the hardware collate option off...
2592 if ((option
= ppdFindOption(ppd
, "Collate")) != NULL
&&
2593 !option
->conflicted
)
2594 doc
->slow_collate
= 0;
2596 ppdMarkOption(ppd
, "Collate", "False");
2600 doc
->slow_collate
= 0;
2602 if (!ppdFindOption(ppd
, "OutputOrder") && doc
->output_order
)
2603 doc
->slow_order
= 1;
2605 doc
->slow_order
= 0;
2608 (doc
->slow_collate
|| doc
->slow_order
||
2609 ((attr
= ppdFindAttr(ppd
, "cupsEvenDuplex", NULL
)) != NULL
&&
2610 attr
->value
&& !strcasecmp(attr
->value
, "true"))))
2611 doc
->slow_duplex
= 1;
2613 doc
->slow_duplex
= 0;
2616 * Create a temporary file for page data if we need to filter slowly...
2619 if (doc
->slow_order
|| doc
->slow_collate
)
2621 if ((doc
->temp
= cupsTempFile2(doc
->tempfile
,
2622 sizeof(doc
->tempfile
))) == NULL
)
2624 fprintf(stderr
, _("ERROR: Unable to create temporary file: %s\n"),
2631 * Figure out if we should use ESPshowpage or not...
2634 if (doc
->page_label
|| getenv("CLASSIFICATION") || doc
->number_up
> 1 ||
2638 * Yes, use ESPshowpage...
2641 doc
->use_ESPshowpage
= 1;
2644 fprintf(stderr
, "DEBUG: slow_collate=%d, slow_duplex=%d, slow_order=%d\n",
2645 doc
->slow_collate
, doc
->slow_duplex
, doc
->slow_order
);
2650 * 'skip_page()' - Skip past a page that won't be printed...
2653 static size_t /* O - Length of next line */
2654 skip_page(cups_file_t
*fp
, /* I - File to read from */
2655 char *line
, /* I - Line buffer */
2656 size_t linelen
, /* I - Length of initial line */
2657 size_t linesize
) /* I - Size of line buffer */
2659 int level
; /* Embedded document level */
2664 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
2667 (!strncmp(line
, "%%Page:", 7) || !strncmp(line
, "%%Trailer", 9)))
2669 else if (!strncmp(line
, "%%BeginDocument", 15) ||
2670 !strncmp(line
, "%ADO_BeginApplication", 21))
2672 else if ((!strncmp(line
, "%%EndDocument", 13) ||
2673 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
2675 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
2676 (!strncmp(line
, "%%BeginData:", 12) &&
2677 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
2680 * Skip binary data...
2683 int bytes
; /* Bytes of data */
2686 bytes
= atoi(strchr(line
, ':') + 1);
2690 if (bytes
> linesize
)
2691 linelen
= cupsFileRead(fp
, line
, linesize
);
2693 linelen
= cupsFileRead(fp
, line
, bytes
);
2698 perror("ERROR: Early end-of-file while reading binary data");
2712 * 'start_nup()' - Start processing for N-up printing...
2716 start_nup(pstops_doc_t
*doc
, /* I - Document information */
2717 int number
, /* I - Page number */
2718 int show_border
, /* I - Show the border? */
2719 const int *bounding_box
) /* I - BoundingBox value */
2721 int pos
; /* Position on page */
2722 int x
, y
; /* Relative position of subpage */
2723 float w
, l
, /* Width and length of subpage */
2724 tx
, ty
; /* Translation values for subpage */
2725 float pagew
, /* Printable width of page */
2726 pagel
; /* Printable height of page */
2727 int bboxw
, /* BoundingBox width */
2728 bboxl
; /* BoundingBox height */
2731 if (doc
->number_up
> 1)
2732 doc_puts(doc
, "userdict/ESPsave save put\n");
2735 doc_printf(doc
, "%.1f 0.0 translate -1 1 scale\n", PageWidth
);
2737 pos
= (number
- 1) % doc
->number_up
;
2738 pagew
= PageRight
- PageLeft
;
2739 pagel
= PageTop
- PageBottom
;
2743 bboxw
= bounding_box
[2] - bounding_box
[0];
2744 bboxl
= bounding_box
[3] - bounding_box
[1];
2752 fprintf(stderr
, "DEBUG: pagew = %.1f, pagel = %.1f\n", pagew
, pagel
);
2753 fprintf(stderr
, "DEBUG: bboxw = %d, bboxl = %d\n", bboxw
, bboxl
);
2754 fprintf(stderr
, "DEBUG: PageLeft = %.1f, PageRight = %.1f\n",
2755 PageLeft
, PageRight
);
2756 fprintf(stderr
, "DEBUG: PageTop = %.1f, PageBottom = %.1f\n",
2757 PageTop
, PageBottom
);
2758 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
2759 PageWidth
, PageLength
);
2761 switch (Orientation
)
2763 case 1 : /* Landscape */
2764 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", PageLength
);
2766 case 2 : /* Reverse Portrait */
2767 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", PageWidth
,
2770 case 3 : /* Reverse Landscape */
2771 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", PageWidth
);
2775 if (Duplex
&& doc
->number_up
> 1 && ((number
/ doc
->number_up
) & 1))
2776 doc_printf(doc
, "%.1f %.1f translate\n", PageWidth
- PageRight
, PageBottom
);
2777 else if (doc
->number_up
> 1 || doc
->fitplot
)
2778 doc_printf(doc
, "%.1f %.1f translate\n", PageLeft
, PageBottom
);
2780 switch (doc
->number_up
)
2786 l
= w
* bboxl
/ bboxw
;
2791 w
= l
* bboxw
/ bboxl
;
2794 tx
= 0.5 * (pagew
- w
);
2795 ty
= 0.5 * (pagel
- l
);
2797 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n", tx
, ty
,
2798 w
/ bboxw
, l
/ bboxl
);
2808 if (Orientation
& 1)
2812 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2816 l
= w
* bboxl
/ bboxw
;
2818 if (l
> (pagew
* 0.5))
2821 w
= l
* bboxw
/ bboxl
;
2824 tx
= 0.5 * (pagew
* 0.5 - l
);
2825 ty
= 0.5 * (pagel
- w
);
2827 if (doc
->normal_landscape
)
2828 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2830 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2832 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2833 ty
, tx
+ pagew
* 0.5 * x
, w
/ bboxw
, l
/ bboxl
);
2839 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2843 w
= l
* bboxw
/ bboxl
;
2845 if (w
> (pagel
* 0.5))
2848 l
= w
* bboxl
/ bboxw
;
2851 tx
= 0.5 * (pagel
* 0.5 - w
);
2852 ty
= 0.5 * (pagew
- l
);
2854 if (doc
->normal_landscape
)
2855 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2857 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2859 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2860 tx
+ pagel
* 0.5 * x
, ty
, w
/ bboxw
, l
/ bboxl
);
2865 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2876 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2879 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2883 l
= w
* bboxl
/ bboxw
;
2885 if (l
> (pagel
* 0.5))
2888 w
= l
* bboxw
/ bboxl
;
2891 tx
= 0.5 * (pagew
* 0.5 - w
);
2892 ty
= 0.5 * (pagel
* 0.5 - l
);
2894 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2895 tx
+ x
* pagew
* 0.5, ty
+ y
* pagel
* 0.5,
2896 w
/ bboxw
, l
/ bboxl
);
2900 if (Orientation
& 1)
2902 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2907 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2910 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2918 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2921 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2926 l
= w
* bboxl
/ bboxw
;
2928 if (l
> (pagew
* 0.333))
2931 w
= l
* bboxw
/ bboxl
;
2934 tx
= 0.5 * (pagel
- 2 * w
);
2935 ty
= 0.5 * (pagew
- 3 * l
);
2937 if (doc
->normal_landscape
)
2938 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
2940 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
2942 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2943 tx
+ x
* w
, ty
+ y
* l
, l
/ bboxl
, w
/ bboxw
);
2947 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2952 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2955 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2963 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2966 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2971 w
= l
* bboxw
/ bboxl
;
2973 if (w
> (pagel
* 0.333))
2976 l
= w
* bboxl
/ bboxw
;
2979 tx
= 0.5 * (pagel
- 3 * w
);
2980 ty
= 0.5 * (pagew
- 2 * l
);
2982 if (doc
->normal_landscape
)
2983 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
2985 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
2987 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2988 tx
+ w
* x
, ty
+ l
* y
, w
/ bboxw
, l
/ bboxl
);
2994 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3005 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3008 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3012 l
= w
* bboxl
/ bboxw
;
3014 if (l
> (pagel
* 0.333))
3017 w
= l
* bboxw
/ bboxl
;
3020 tx
= 0.5 * (pagew
* 0.333 - w
);
3021 ty
= 0.5 * (pagel
* 0.333 - l
);
3023 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3024 tx
+ x
* pagew
* 0.333, ty
+ y
* pagel
* 0.333,
3025 w
/ bboxw
, l
/ bboxl
);
3029 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3040 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3043 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3047 l
= w
* bboxl
/ bboxw
;
3049 if (l
> (pagel
* 0.25))
3052 w
= l
* bboxw
/ bboxl
;
3055 tx
= 0.5 * (pagew
* 0.25 - w
);
3056 ty
= 0.5 * (pagel
* 0.25 - l
);
3058 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3059 tx
+ x
* pagew
* 0.25, ty
+ y
* pagel
* 0.25,
3060 w
/ bboxw
, l
/ bboxl
);
3065 * Draw borders as necessary...
3068 if (doc
->page_border
&& show_border
)
3070 int rects
; /* Number of border rectangles */
3071 float fscale
, /* Scaling value for points */
3072 margin
; /* Current margin for borders */
3075 rects
= (doc
->page_border
& PSTOPS_BORDERDOUBLE
) ? 2 : 1;
3076 fscale
= PageWidth
/ w
;
3077 margin
= 2.25 * fscale
;
3080 * Set the line width and color...
3083 doc_puts(doc
, "gsave\n");
3084 doc_printf(doc
, "%.3f setlinewidth 0 setgray newpath\n",
3085 (doc
->page_border
& PSTOPS_BORDERTHICK
) ? 0.5 * fscale
:
3089 * Draw border boxes...
3092 for (; rects
> 0; rects
--, margin
+= 2 * fscale
)
3093 if (doc
->number_up
> 1)
3094 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3095 margin
- 2.25 * fscale
,
3096 margin
- 2.25 * fscale
,
3097 bboxw
+ 4.5 * fscale
- 2 * margin
,
3098 bboxl
+ 4.5 * fscale
- 2 * margin
);
3100 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3102 PageBottom
+ margin
,
3103 PageRight
- PageLeft
- 2 * margin
,
3104 PageTop
- PageBottom
- 2 * margin
);
3107 * Restore pen settings...
3110 doc_puts(doc
, "grestore\n");
3116 * Clip the page that follows to the bounding box of the page...
3119 doc_printf(doc
, "%d %d translate\n", -bounding_box
[0],
3121 doc_printf(doc
, "%d %d %d %d ESPrc\n", bounding_box
[0], bounding_box
[1],
3124 else if (doc
->number_up
> 1)
3127 * Clip the page that follows to the default page size...
3130 doc_printf(doc
, "0 0 %d %d ESPrc\n", bboxw
, bboxl
);
3136 * 'write_label_prolog()' - Write the prolog with the classification
3141 write_label_prolog(pstops_doc_t
*doc
, /* I - Document info */
3142 const char *label
, /* I - Page label */
3143 float bottom
, /* I - Bottom position in points */
3144 float top
, /* I - Top position in points */
3145 float width
) /* I - Width in points */
3147 const char *classification
; /* CLASSIFICATION environment variable */
3148 const char *ptr
; /* Temporary string pointer */
3152 * First get the current classification...
3155 if ((classification
= getenv("CLASSIFICATION")) == NULL
)
3156 classification
= "";
3157 if (strcmp(classification
, "none") == 0)
3158 classification
= "";
3161 * If there is nothing to show, bind an empty 'write labels' procedure
3165 if (!classification
[0] && (label
== NULL
|| !label
[0]))
3167 doc_puts(doc
, "userdict/ESPwl{}bind put\n");
3172 * Set the classification + page label string...
3175 doc_puts(doc
, "userdict");
3176 if (!strcmp(classification
, "confidential"))
3177 doc_puts(doc
, "/ESPpl(CONFIDENTIAL");
3178 else if (!strcmp(classification
, "classified"))
3179 doc_puts(doc
, "/ESPpl(CLASSIFIED");
3180 else if (!strcmp(classification
, "secret"))
3181 doc_puts(doc
, "/ESPpl(SECRET");
3182 else if (!strcmp(classification
, "topsecret"))
3183 doc_puts(doc
, "/ESPpl(TOP SECRET");
3184 else if (!strcmp(classification
, "unclassified"))
3185 doc_puts(doc
, "/ESPpl(UNCLASSIFIED");
3188 doc_puts(doc
, "/ESPpl(");
3190 for (ptr
= classification
; *ptr
; ptr
++)
3192 if (*ptr
< 32 || *ptr
> 126)
3193 doc_printf(doc
, "\\%03o", *ptr
);
3194 else if (*ptr
== '_')
3196 else if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
3197 doc_printf(doc
, "\\%c", *ptr
);
3199 doc_printf(doc
, "%c", *ptr
);
3205 if (classification
[0])
3206 doc_puts(doc
, " - ");
3209 * Quote the label string as needed...
3212 for (ptr
= label
; *ptr
; ptr
++)
3214 if (*ptr
< 32 || *ptr
> 126)
3215 doc_printf(doc
, "\\%03o", *ptr
);
3216 else if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
3217 doc_printf(doc
, "\\%c", *ptr
);
3219 doc_printf(doc
, "%c", *ptr
);
3223 doc_puts(doc
, ")put\n");
3226 * Then get a 14 point Helvetica-Bold font...
3229 doc_puts(doc
, "userdict/ESPpf /Helvetica-Bold findfont 14 scalefont put\n");
3232 * Finally, the procedure to write the labels on the page...
3235 doc_puts(doc
, "userdict/ESPwl{\n");
3236 doc_puts(doc
, " ESPpf setfont\n");
3237 doc_printf(doc
, " ESPpl stringwidth pop dup 12 add exch -0.5 mul %.0f add\n",
3239 doc_puts(doc
, " 1 setgray\n");
3240 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrf\n", bottom
- 2.0);
3241 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrf\n", top
- 18.0);
3242 doc_puts(doc
, " 0 setgray\n");
3243 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrs\n", bottom
- 2.0);
3244 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrs\n", top
- 18.0);
3245 doc_printf(doc
, " dup %.0f moveto ESPpl show\n", bottom
+ 2.0);
3246 doc_printf(doc
, " %.0f moveto ESPpl show\n", top
- 14.0);
3247 doc_puts(doc
, "pop\n");
3248 doc_puts(doc
, "}bind put\n");
3253 * 'write_labels()' - Write the actual page labels.
3255 * This function is a copy of the one in common.c since we need to
3256 * use doc_puts/doc_printf instead of puts/printf...
3260 write_labels(pstops_doc_t
*doc
, /* I - Document information */
3261 int orient
) /* I - Orientation of the page */
3263 float width
, /* Width of page */
3264 length
; /* Length of page */
3267 doc_puts(doc
, "gsave\n");
3269 if ((orient
^ Orientation
) & 1)
3277 length
= PageLength
;
3282 case 1 : /* Landscape */
3283 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", length
);
3285 case 2 : /* Reverse Portrait */
3286 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", width
, length
);
3288 case 3 : /* Reverse Landscape */
3289 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", width
);
3293 doc_puts(doc
, "ESPwl\n");
3294 doc_puts(doc
, "grestore\n");
3299 * End of "$Id: pstops.c 6649 2007-07-11 21:46:42Z mike $".