]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/pstops.c
2 * "$Id: pstops.c 6320 2007-03-08 13:36:56Z mike $"
4 * PostScript filter for the Common UNIX Printing System (CUPS).
6 * Copyright 1993-2007 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * main() - Main entry...
29 * add_page() - Add a page to the pages array...
30 * check_range() - Check to see if the current page is selected for
31 * copy_bytes() - Copy bytes from the input file to stdout...
32 * copy_comments() - Copy all of the comments section...
33 * copy_dsc() - Copy a DSC-conforming document...
34 * copy_non_dsc() - Copy a document that does not conform to the DSC...
35 * copy_page() - Copy a page description...
36 * copy_prolog() - Copy the document prolog section...
37 * copy_setup() - Copy the document setup section...
38 * copy_trailer() - Copy the document trailer...
39 * do_prolog() - Send the necessary document prolog commands...
40 * do_setup() - Send the necessary document setup commands...
41 * doc_printf() - Send a formatted string to stdout and/or the temp
43 * doc_puts() - Send a nul-terminated string to stdout and/or the
45 * doc_write() - Send data to stdout and/or the temp file.
46 * end_nup() - End processing for N-up printing...
47 * include_feature() - Include a printer option/feature command.
48 * parse_text() - Parse a text value in a comment...
49 * set_pstops_options() - Set pstops options...
50 * skip_page() - Skip past a page that won't be printed...
51 * start_nup() - Start processing for N-up printing...
52 * write_labels() - Write the actual page labels.
56 * Include necessary headers...
62 #include <cups/file.h>
63 #include <cups/array.h>
70 #define PSTOPS_BORDERNONE 0 /* No border or hairline border */
71 #define PSTOPS_BORDERTHICK 1 /* Think border */
72 #define PSTOPS_BORDERSINGLE 2 /* Single-line hairline border */
73 #define PSTOPS_BORDERSINGLE2 3 /* Single-line thick border */
74 #define PSTOPS_BORDERDOUBLE 4 /* Double-line hairline border */
75 #define PSTOPS_BORDERDOUBLE2 5 /* Double-line thick border */
77 #define PSTOPS_LAYOUT_LRBT 0 /* Left to right, bottom to top */
78 #define PSTOPS_LAYOUT_LRTB 1 /* Left to right, top to bottom */
79 #define PSTOPS_LAYOUT_RLBT 2 /* Right to left, bottom to top */
80 #define PSTOPS_LAYOUT_RLTB 3 /* Right to left, top to bottom */
81 #define PSTOPS_LAYOUT_BTLR 4 /* Bottom to top, left to right */
82 #define PSTOPS_LAYOUT_TBLR 5 /* Top to bottom, left to right */
83 #define PSTOPS_LAYOUT_BTRL 6 /* Bottom to top, right to left */
84 #define PSTOPS_LAYOUT_TBRL 7 /* Top to bottom, right to left */
86 #define PSTOPS_LAYOUT_NEGATEY 1 /* The bits for the layout */
87 #define PSTOPS_LAYOUT_NEGATEX 2 /* definitions above... */
88 #define PSTOPS_LAYOUT_VERTICAL 4
95 typedef struct /**** Page information ****/
97 char *label
; /* Page label */
98 int bounding_box
[4]; /* PageBoundingBox */
99 off_t offset
; /* Offset to start of page */
100 ssize_t length
; /* Number of bytes for page */
101 int num_options
; /* Number of options for this page */
102 cups_option_t
*options
; /* Options for this page */
105 typedef struct /**** Document information ****/
107 int page
; /* Current page */
108 int bounding_box
[4]; /* BoundingBox from header */
109 int new_bounding_box
[4]; /* New composite bounding box */
110 int num_options
; /* Number of document-wide options */
111 cups_option_t
*options
; /* Document-wide options */
112 int normal_landscape
, /* Normal rotation for landscape? */
113 saw_eof
, /* Saw the %%EOF comment? */
114 slow_collate
, /* Collate copies by hand? */
115 slow_duplex
, /* Duplex pages slowly? */
116 slow_order
, /* Reverse pages slowly? */
117 use_ESPshowpage
; /* Use ESPshowpage? */
118 cups_array_t
*pages
; /* Pages in document */
119 cups_file_t
*temp
; /* Temporary file, if any */
120 char tempfile
[1024]; /* Temporary filename */
121 int job_id
; /* Job ID */
122 const char *user
, /* User name */
123 *title
; /* Job name */
124 int copies
; /* Number of copies */
125 const char *ap_input_slot
, /* AP_FIRSTPAGE_InputSlot value */
126 *ap_manual_feed
; /* AP_FIRSTPAGE_ManualFeed value */
127 float brightness
; /* brightness value */
128 int collate
, /* Collate copies? */
129 emit_jcl
, /* Emit JCL commands? */
130 fitplot
; /* Fit pages to media */
131 float gamma
; /* gamma value */
132 const char *input_slot
, /* InputSlot value */
133 *manual_feed
; /* ManualFeed value */
134 int mirror
, /* doc->mirror/mirror pages */
135 number_up
, /* Number of pages on each sheet */
136 number_up_layout
, /* doc->number_up_layout of N-up pages */
137 output_order
, /* Requested reverse output order? */
138 page_border
; /* doc->page_border around pages */
139 const char *page_label
, /* page-label option, if any */
140 *page_ranges
, /* page-ranges option, if any */
141 *page_set
; /* page-set option, if any */
146 * Convenience macros...
149 #define is_first_page(p) (doc->number_up == 1 || \
150 ((p) % doc->number_up) == 1)
151 #define is_last_page(p) (doc->number_up == 1 || \
152 ((p) % doc->number_up) == 0)
153 #define is_not_last_page(p) (doc->number_up > 1 && \
154 ((p) % doc->number_up) != 0)
161 static pstops_page_t
*add_page(pstops_doc_t
*doc
, const char *label
);
162 static int check_range(pstops_doc_t
*doc
, int page
);
163 static void copy_bytes(cups_file_t
*fp
, off_t offset
,
165 static size_t copy_comments(cups_file_t
*fp
, pstops_doc_t
*doc
,
166 ppd_file_t
*ppd
, char *line
,
167 size_t linelen
, size_t linesize
);
168 static void copy_dsc(cups_file_t
*fp
, pstops_doc_t
*doc
,
169 ppd_file_t
*ppd
, char *line
, size_t linelen
,
171 static void copy_non_dsc(cups_file_t
*fp
, pstops_doc_t
*doc
,
172 ppd_file_t
*ppd
, char *line
,
173 size_t linelen
, size_t linesize
);
174 static size_t copy_page(cups_file_t
*fp
, pstops_doc_t
*doc
,
175 ppd_file_t
*ppd
, int number
, char *line
,
176 size_t linelen
, size_t linesize
);
177 static size_t copy_prolog(cups_file_t
*fp
, pstops_doc_t
*doc
,
178 ppd_file_t
*ppd
, char *line
,
179 size_t linelen
, size_t linesize
);
180 static size_t copy_setup(cups_file_t
*fp
, pstops_doc_t
*doc
,
181 ppd_file_t
*ppd
, char *line
,
182 size_t linelen
, size_t linesize
);
183 static size_t copy_trailer(cups_file_t
*fp
, pstops_doc_t
*doc
,
184 ppd_file_t
*ppd
, int number
, char *line
,
185 size_t linelen
, size_t linesize
);
186 static void do_prolog(pstops_doc_t
*doc
, ppd_file_t
*ppd
);
187 static void do_setup(pstops_doc_t
*doc
, ppd_file_t
*ppd
);
188 static void doc_printf(pstops_doc_t
*doc
, const char *format
, ...)
190 __attribute__ ((__format__ (__printf__
, 2, 3)))
191 #endif /* __GNUC__ */
193 static void doc_puts(pstops_doc_t
*doc
, const char *s
);
194 static void doc_write(pstops_doc_t
*doc
, const char *s
, size_t len
);
195 static void end_nup(pstops_doc_t
*doc
, int number
);
196 static int include_feature(ppd_file_t
*ppd
, const char *line
,
198 cups_option_t
**options
);
199 static char *parse_text(const char *start
, char **end
, char *buffer
,
201 static void set_pstops_options(pstops_doc_t
*doc
, ppd_file_t
*ppd
,
202 char *argv
[], int num_options
,
203 cups_option_t
*options
);
204 static size_t skip_page(cups_file_t
*fp
, char *line
, size_t linelen
,
206 static void start_nup(pstops_doc_t
*doc
, int number
,
207 int show_border
, const int *bounding_box
);
208 static void write_label_prolog(pstops_doc_t
*doc
, const char *label
,
209 float bottom
, float top
,
211 static void write_labels(pstops_doc_t
*doc
, int orient
);
215 * 'main()' - Main entry...
218 int /* O - Exit status */
219 main(int argc
, /* I - Number of command-line args */
220 char *argv
[]) /* I - Command-line arguments */
222 pstops_doc_t doc
; /* Document information */
223 cups_file_t
*fp
; /* Print file */
224 ppd_file_t
*ppd
; /* PPD file */
225 int num_options
; /* Number of print options */
226 cups_option_t
*options
; /* Print options */
227 char line
[8192]; /* Line buffer */
228 size_t len
; /* Length of line buffer */
232 * Make sure status messages are not buffered...
235 setbuf(stderr
, NULL
);
238 * Check command-line...
241 if (argc
< 6 || argc
> 7)
243 fputs("ERROR: pstops job-id user title copies options [file]\n", stderr
);
248 * If we have 7 arguments, print the file named on the command-line.
249 * Otherwise, send stdin instead...
253 fp
= cupsFileStdin();
257 * Try to open the print file...
260 if ((fp
= cupsFileOpen(argv
[6], "r")) == NULL
)
262 fprintf(stderr
, "ERROR: Unable to open print file \"%s\" - %s\n",
263 argv
[6], strerror(errno
));
269 * Read the first line to see if we have DSC comments...
272 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
274 fputs("ERROR: Empty print file!\n", stderr
);
279 * Process command-line options...
283 num_options
= cupsParseOptions(argv
[5], 0, &options
);
284 ppd
= SetCommonOptions(num_options
, options
, 1);
286 set_pstops_options(&doc
, ppd
, argv
, num_options
, options
);
289 * Write any "exit server" options that have been selected...
292 ppdEmit(ppd
, stdout
, PPD_ORDER_EXIT
);
295 * Write any JCL commands that are needed to print PostScript code...
299 ppdEmitJCL(ppd
, stdout
, doc
.job_id
, doc
.user
, doc
.title
);
302 * Start with a DSC header...
305 puts("%!PS-Adobe-3.0");
308 * Skip leading PJL in the document...
311 while (!strncmp(line
, "\033%-12345X", 9) || !strncmp(line
, "@PJL ", 5))
314 * Yup, we have leading PJL fun, so skip it until we hit the line
315 * with "ENTER LANGUAGE"...
318 fputs("DEBUG: Skipping PJL header...\n", stderr
);
320 while (strstr(line
, "ENTER LANGUAGE") == NULL
&& strncmp(line
, "%!", 2))
321 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
324 if (!strncmp(line
, "%!", 2))
327 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
332 * Now see if the document conforms to the Adobe Document Structuring
336 if (!strncmp(line
, "%!PS-Adobe-", 11))
339 * Yes, filter the document...
342 copy_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
347 * No, display an error message and treat the file as if it contains
351 copy_non_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
355 * Send %%EOF as needed...
362 * End the job with the appropriate JCL command or CTRL-D...
367 if (ppd
&& ppd
->jcl_end
)
368 ppdEmitJCLEnd(ppd
, stdout
);
374 * Close files and remove the temporary file if needed...
379 cupsFileClose(doc
.temp
);
380 unlink(doc
.tempfile
);
384 cupsFreeOptions(num_options
, options
);
393 * 'add_page()' - Add a page to the pages array...
396 static pstops_page_t
* /* O - New page info object */
397 add_page(pstops_doc_t
*doc
, /* I - Document information */
398 const char *label
) /* I - Page label */
400 pstops_page_t
*pageinfo
; /* New page info object */
404 doc
->pages
= cupsArrayNew(NULL
, NULL
);
408 fprintf(stderr
, "EMERG: Unable to allocate memory for pages array: %s\n",
413 if ((pageinfo
= calloc(1, sizeof(pstops_page_t
))) == NULL
)
415 fprintf(stderr
, "EMERG: Unable to allocate memory for page info: %s\n",
420 pageinfo
->label
= strdup(label
);
421 pageinfo
->offset
= cupsFileTell(doc
->temp
);
423 cupsArrayAdd(doc
->pages
, pageinfo
);
432 * 'check_range()' - Check to see if the current page is selected for
436 static int /* O - 1 if selected, 0 otherwise */
437 check_range(pstops_doc_t
*doc
, /* I - Document information */
438 int page
) /* I - Page number */
440 const char *range
; /* Pointer into range string */
441 int lower
, upper
; /* Lower and upper page numbers */
447 * See if we only print even or odd pages...
450 if (!strcasecmp(doc
->page_set
, "even") && (page
& 1))
453 if (!strcasecmp(doc
->page_set
, "odd") && !(page
& 1))
457 if (!doc
->page_ranges
)
458 return (1); /* No range, print all pages... */
460 for (range
= doc
->page_ranges
; *range
!= '\0';)
466 upper
= strtol(range
, (char **)&range
, 10);
470 lower
= strtol(range
, (char **)&range
, 10);
475 if (!isdigit(*range
& 255))
478 upper
= strtol(range
, (char **)&range
, 10);
484 if (page
>= lower
&& page
<= upper
)
498 * 'copy_bytes()' - Copy bytes from the input file to stdout...
502 copy_bytes(cups_file_t
*fp
, /* I - File to read from */
503 off_t offset
, /* I - Offset to page data */
504 size_t length
) /* I - Length of page data */
506 char buffer
[8192]; /* Data buffer */
507 ssize_t nbytes
; /* Number of bytes read */
508 size_t nleft
; /* Number of bytes left/remaining */
513 if (cupsFileSeek(fp
, offset
) < 0)
515 fprintf(stderr
, "ERROR: Unable to seek to offset " CUPS_LLFMT
517 CUPS_LLCAST offset
, strerror(errno
));
521 while (nleft
> 0 || length
== 0)
523 if (nleft
> sizeof(buffer
) || length
== 0)
524 nbytes
= sizeof(buffer
);
528 if ((nbytes
= cupsFileRead(fp
, buffer
, nbytes
)) < 1)
533 fwrite(buffer
, 1, nbytes
, stdout
);
539 * 'copy_comments()' - Copy all of the comments section...
541 * This function expects "line" to be filled with a comment line.
542 * On return, "line" will contain the next line in the file, if any.
545 static size_t /* O - Length of next line */
546 copy_comments(cups_file_t
*fp
, /* I - File to read from */
547 pstops_doc_t
*doc
, /* I - Document info */
548 ppd_file_t
*ppd
, /* I - PPD file */
549 char *line
, /* I - Line buffer */
550 size_t linelen
, /* I - Length of initial line */
551 size_t linesize
) /* I - Size of line buffer */
553 int saw_bounding_box
, /* Saw %%BoundingBox: comment? */
554 saw_for
, /* Saw %%For: comment? */
555 saw_pages
, /* Saw %%Pages: comment? */
556 saw_title
; /* Saw %%Title: comment? */
560 * Loop until we see %%EndComments or a non-comment line...
563 saw_bounding_box
= 0;
568 while (line
[0] == '%')
571 * Strip trailing whitespace...
578 if (!isspace(line
[linelen
] & 255))
581 line
[linelen
] = '\0';
588 fprintf(stderr
, "DEBUG: %s\n", line
);
591 * Pull the headers out...
594 if (!strncmp(line
, "%%Pages:", 8))
596 int pages
; /* Number of pages */
600 fputs("ERROR: Duplicate %%Pages: comment seen!\n", stderr
);
604 if (Duplex
&& (pages
= atoi(line
+ 8)) > 0 && pages
<= doc
->number_up
)
607 * Since we will only be printing on a single page, disable duplexing.
611 doc
->slow_duplex
= 0;
613 if (cupsGetOption("sides", doc
->num_options
, doc
->options
))
614 doc
->num_options
= cupsAddOption("sides", "one-sided",
615 doc
->num_options
, &(doc
->options
));
617 if (cupsGetOption("Duplex", doc
->num_options
, doc
->options
))
618 doc
->num_options
= cupsAddOption("Duplex", "None",
619 doc
->num_options
, &(doc
->options
));
621 if (cupsGetOption("EFDuplex", doc
->num_options
, doc
->options
))
622 doc
->num_options
= cupsAddOption("EFDuplex", "None",
623 doc
->num_options
, &(doc
->options
));
625 if (cupsGetOption("EFDuplexing", doc
->num_options
, doc
->options
))
626 doc
->num_options
= cupsAddOption("EFDuplexing", "False",
627 doc
->num_options
, &(doc
->options
));
629 if (cupsGetOption("KD03Duplex", doc
->num_options
, doc
->options
))
630 doc
->num_options
= cupsAddOption("KD03Duplex", "None",
631 doc
->num_options
, &(doc
->options
));
633 if (cupsGetOption("JCLDuplex", doc
->num_options
, doc
->options
))
634 doc
->num_options
= cupsAddOption("JCLDuplex", "None",
635 doc
->num_options
, &(doc
->options
));
637 ppdMarkOption(ppd
, "Duplex", "None");
638 ppdMarkOption(ppd
, "EFDuplex", "None");
639 ppdMarkOption(ppd
, "EFDuplexing", "False");
640 ppdMarkOption(ppd
, "KD03Duplex", "None");
641 ppdMarkOption(ppd
, "JCLDuplex", "None");
644 else if (!strncmp(line
, "%%BoundingBox:", 14))
646 if (saw_bounding_box
)
647 fputs("ERROR: Duplicate %%BoundingBox: comment seen!\n", stderr
);
648 else if (strstr(line
+ 14, "(atend)"))
651 * Do nothing for now but use the default imageable area...
654 else if (sscanf(line
+ 14, "%d%d%d%d", doc
->bounding_box
+ 0,
655 doc
->bounding_box
+ 1, doc
->bounding_box
+ 2,
656 doc
->bounding_box
+ 3) != 4)
658 fputs("ERROR: Bad %%BoundingBox: comment seen!\n", stderr
);
660 doc
->bounding_box
[0] = (int)PageLeft
;
661 doc
->bounding_box
[1] = (int)PageBottom
;
662 doc
->bounding_box
[2] = (int)PageRight
;
663 doc
->bounding_box
[3] = (int)PageTop
;
666 saw_bounding_box
= 1;
668 else if (!strncmp(line
, "%%For:", 6))
671 doc_printf(doc
, "%s\n", line
);
673 else if (!strncmp(line
, "%%Title:", 8))
676 doc_printf(doc
, "%s\n", line
);
678 else if (!strncmp(line
, "%cupsRotation:", 14))
681 * Reset orientation of document?
684 int orient
= (atoi(line
+ 14) / 90) & 3;
686 if (orient
!= Orientation
)
689 * Yes, update things so that the pages come out right...
692 Orientation
= (4 - Orientation
+ orient
) & 3;
694 Orientation
= orient
;
697 else if (!strcmp(line
, "%%EndComments"))
699 linelen
= cupsFileGetLine(fp
, line
, linesize
);
702 else if (strncmp(line
, "%!", 2) && strncmp(line
, "%cups", 5))
703 doc_printf(doc
, "%s\n", line
);
705 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
709 if (!saw_bounding_box
)
710 fputs("ERROR: No %%BoundingBox: comment in header!\n", stderr
);
713 fputs("ERROR: No %%Pages: comment in header!\n", stderr
);
716 WriteTextComment("For", doc
->user
);
719 WriteTextComment("Title", doc
->title
);
721 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
724 * Tell the document processor the copy and duplex options
725 * that are required...
728 doc_printf(doc
, "%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
729 doc
->collate
? " collate" : "",
730 Duplex
? " duplex" : "");
733 * Apple uses RBI comments for various non-PPD options...
736 doc_printf(doc
, "%%RBINumCopies: %d\n", doc
->copies
);
741 * Tell the document processor the duplex option that is required...
745 doc_puts(doc
, "%%Requirements: duplex\n");
748 * Apple uses RBI comments for various non-PPD options...
751 doc_puts(doc
, "%RBINumCopies: 1\n");
754 doc_puts(doc
, "%%Pages: (atend)\n");
755 doc_puts(doc
, "%%BoundingBox: (atend)\n");
756 doc_puts(doc
, "%%EndComments\n");
763 * 'copy_dsc()' - Copy a DSC-conforming document...
765 * This function expects "line" to be filled with the %!PS-Adobe comment line.
769 copy_dsc(cups_file_t
*fp
, /* I - File to read from */
770 pstops_doc_t
*doc
, /* I - Document info */
771 ppd_file_t
*ppd
, /* I - PPD file */
772 char *line
, /* I - Line buffer */
773 size_t linelen
, /* I - Length of initial line */
774 size_t linesize
) /* I - Size of line buffer */
776 int number
; /* Page number */
777 pstops_page_t
*pageinfo
; /* Page information */
781 * Make sure we use ESPshowpage for EPS files...
784 if (strstr(line
, "EPSF"))
786 doc
->use_ESPshowpage
= 1;
791 * Start sending the document with any commands needed...
794 fprintf(stderr
, "DEBUG: Before copy_comments - %s", line
);
795 linelen
= copy_comments(fp
, doc
, ppd
, line
, linelen
, linesize
);
798 * Now find the prolog section, if any...
801 fprintf(stderr
, "DEBUG: Before copy_prolog - %s", line
);
802 linelen
= copy_prolog(fp
, doc
, ppd
, line
, linelen
, linesize
);
805 * Then the document setup section...
808 fprintf(stderr
, "DEBUG: Before copy_setup - %s", line
);
809 linelen
= copy_setup(fp
, doc
, ppd
, line
, linelen
, linesize
);
812 * Copy until we see %%Page:...
815 while (strncmp(line
, "%%Page:", 7) && strncmp(line
, "%%Trailer", 9))
817 doc_write(doc
, line
, linelen
);
819 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
824 * Then process pages until we have no more...
829 fprintf(stderr
, "DEBUG: Before page loop - %s", line
);
830 while (!strncmp(line
, "%%Page:", 7))
834 if (check_range(doc
, (number
- 1) / doc
->number_up
+ 1))
836 fprintf(stderr
, "DEBUG: Copying page %d...\n", number
);
837 linelen
= copy_page(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
841 fprintf(stderr
, "DEBUG: Skipping page %d...\n", number
);
842 linelen
= skip_page(fp
, line
, linelen
, linesize
);
847 * Finish up the last page(s)...
850 if (number
&& is_not_last_page(number
) && cupsArrayLast(doc
->pages
))
852 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
854 start_nup(doc
, doc
->number_up
, 0, doc
->bounding_box
);
855 doc_puts(doc
, "showpage\n");
856 end_nup(doc
, doc
->number_up
);
858 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
861 if (doc
->slow_duplex
&& (doc
->page
& 1))
864 * Make sure we have an even number of pages...
867 pageinfo
= add_page(doc
, "(filler)");
869 if (!doc
->slow_order
)
871 if (!ppd
|| !ppd
->num_filters
)
872 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
873 doc
->slow_collate
? 1 : doc
->copies
);
875 printf("%%%%Page: (filler) %d\n", doc
->page
);
878 start_nup(doc
, doc
->number_up
, 0, doc
->bounding_box
);
879 doc_puts(doc
, "showpage\n");
880 end_nup(doc
, doc
->number_up
);
882 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
886 * Make additional copies as necessary...
889 number
= doc
->slow_order
? 0 : doc
->page
;
893 int copy
; /* Current copy */
897 * Reopen the temporary file for reading...
900 cupsFileClose(doc
->temp
);
902 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
908 for (copy
= !doc
->slow_order
; copy
< doc
->copies
; copy
++)
911 * Send end-of-job stuff followed by any start-of-job stuff required
912 * for the JCL options...
915 if (number
&& doc
->emit_jcl
&& ppd
&& ppd
->jcl_end
)
918 * Send the trailer...
922 printf("%%%%Pages: %d\n", cupsArrayCount(doc
->pages
));
923 if (doc
->number_up
> 1 || doc
->fitplot
)
924 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
925 PageLeft
, PageBottom
, PageRight
, PageTop
);
927 printf("%%%%BoundingBox: %d %d %d %d\n",
928 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
929 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
933 * Start a new document...
936 ppdEmitJCLEnd(ppd
, stdout
);
937 ppdEmitJCL(ppd
, stdout
, doc
->job_id
, doc
->user
, doc
->title
);
939 puts("%!PS-Adobe-3.0");
945 * Copy the prolog as needed...
950 pageinfo
= (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
951 copy_bytes(doc
->temp
, 0, pageinfo
->offset
);
955 * Then copy all of the pages...
958 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayLast(doc
->pages
) :
959 (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
965 if (!ppd
|| !ppd
->num_filters
)
966 fprintf(stderr
, "PAGE: %d 1\n", number
);
968 if (doc
->number_up
> 1)
970 printf("%%%%Page: (%d) %d\n", number
, number
);
971 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
972 PageLeft
, PageBottom
, PageRight
, PageTop
);
976 printf("%%%%Page: %s %d\n", pageinfo
->label
, number
);
977 printf("%%%%PageBoundingBox: %d %d %d %d\n",
978 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
979 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
982 copy_bytes(doc
->temp
, pageinfo
->offset
, pageinfo
->length
);
984 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayPrev(doc
->pages
) :
985 (pstops_page_t
*)cupsArrayNext(doc
->pages
);
991 * Restore the old showpage operator as needed...
994 if (doc
->use_ESPshowpage
)
995 puts("userdict/showpage/ESPshowpage load put\n");
998 * Write/copy the trailer...
1001 linelen
= copy_trailer(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
1006 * 'copy_non_dsc()' - Copy a document that does not conform to the DSC...
1008 * This function expects "line" to be filled with the %! comment line.
1012 copy_non_dsc(cups_file_t
*fp
, /* I - File to read from */
1013 pstops_doc_t
*doc
, /* I - Document info */
1014 ppd_file_t
*ppd
, /* I - PPD file */
1015 char *line
, /* I - Line buffer */
1016 size_t linelen
, /* I - Length of initial line */
1017 size_t linesize
) /* I - Size of line buffer */
1019 int copy
; /* Current copy */
1020 char buffer
[8192]; /* Copy buffer */
1021 int bytes
; /* Number of bytes copied */
1025 * First let the user know that they are attempting to print a file
1026 * that may not print correctly...
1029 fputs("WARNING: This document does not conform to the Adobe Document "
1030 "Structuring Conventions and may not print correctly!\n", stderr
);
1033 * Then write a standard DSC comment section...
1036 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", PageLeft
, PageBottom
,
1037 PageRight
, PageTop
);
1039 if (doc
->slow_collate
&& doc
->copies
> 1)
1040 printf("%%%%Pages: %d\n", doc
->copies
);
1044 WriteTextComment("For", doc
->user
);
1045 WriteTextComment("Title", doc
->title
);
1047 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1050 * Tell the document processor the copy and duplex options
1051 * that are required...
1054 printf("%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
1055 doc
->collate
? " collate" : "",
1056 Duplex
? " duplex" : "");
1059 * Apple uses RBI comments for various non-PPD options...
1062 printf("%%RBINumCopies: %d\n", doc
->copies
);
1067 * Tell the document processor the duplex option that is required...
1071 puts("%%Requirements: duplex");
1074 * Apple uses RBI comments for various non-PPD options...
1077 puts("%RBINumCopies: 1");
1080 puts("%%EndComments");
1083 * Then the prolog...
1086 puts("%%BeginProlog");
1088 do_prolog(doc
, ppd
);
1090 puts("%%EndProlog");
1093 * Then the setup section...
1096 puts("%%BeginSetup");
1103 * Finally, embed a copy of the file inside a %%Page...
1106 if (!ppd
|| !ppd
->num_filters
)
1107 fprintf(stderr
, "PAGE: 1 %d\n", doc
->temp
? 1 : doc
->copies
);
1109 puts("%%Page: 1 1");
1110 puts("%%BeginPageSetup");
1111 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1112 puts("%%EndPageSetup");
1113 puts("%%BeginDocument: nondsc");
1115 fwrite(line
, linelen
, 1, stdout
);
1118 cupsFileWrite(doc
->temp
, line
, linelen
);
1120 while ((bytes
= cupsFileRead(fp
, buffer
, sizeof(buffer
))) > 0)
1122 fwrite(buffer
, 1, bytes
, stdout
);
1125 cupsFileWrite(doc
->temp
, buffer
, bytes
);
1128 puts("%%EndDocument");
1130 if (doc
->use_ESPshowpage
)
1132 WriteLabels(Orientation
);
1133 puts("ESPshowpage");
1139 * Reopen the temporary file for reading...
1142 cupsFileClose(doc
->temp
);
1144 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
1147 * Make the additional copies as needed...
1150 for (copy
= 1; copy
< doc
->copies
; copy
++)
1152 if (!ppd
|| !ppd
->num_filters
)
1153 fputs("PAGE: 1 1\n", stderr
);
1155 printf("%%%%Page: %d %d\n", copy
+ 1, copy
+ 1);
1156 puts("%%BeginPageSetup");
1157 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1158 puts("%%EndPageSetup");
1159 puts("%%BeginDocument: nondsc");
1161 copy_bytes(doc
->temp
, 0, 0);
1163 puts("%%EndDocument");
1165 if (doc
->use_ESPshowpage
)
1167 WriteLabels(Orientation
);
1168 puts("ESPshowpage");
1174 * Restore the old showpage operator as needed...
1177 if (doc
->use_ESPshowpage
)
1178 puts("userdict/showpage/ESPshowpage load put\n");
1183 * 'copy_page()' - Copy a page description...
1185 * This function expects "line" to be filled with a %%Page comment line.
1186 * On return, "line" will contain the next line in the file, if any.
1189 static size_t /* O - Length of next line */
1190 copy_page(cups_file_t
*fp
, /* I - File to read from */
1191 pstops_doc_t
*doc
, /* I - Document info */
1192 ppd_file_t
*ppd
, /* I - PPD file */
1193 int number
, /* I - Current page number */
1194 char *line
, /* I - Line buffer */
1195 size_t linelen
, /* I - Length of initial line */
1196 size_t linesize
) /* I - Size of line buffer */
1198 char label
[256], /* Page label string */
1199 *ptr
; /* Pointer into line */
1200 int level
; /* Embedded document level */
1201 pstops_page_t
*pageinfo
; /* Page information */
1202 int first_page
; /* First page on N-up output? */
1203 int bounding_box
[4]; /* PageBoundingBox */
1207 * Get the page label for this page...
1210 first_page
= is_first_page(number
);
1212 if (!parse_text(line
+ 7, &ptr
, label
, sizeof(label
)))
1214 fputs("ERROR: Bad %%Page: comment in file!\n", stderr
);
1218 else if (strtol(ptr
, &ptr
, 10) == LONG_MAX
|| !isspace(*ptr
& 255))
1220 fputs("ERROR: Bad %%Page: comment in file!\n", stderr
);
1225 * Create or update the current output page...
1229 pageinfo
= add_page(doc
, label
);
1231 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
1234 * Handle first page override...
1237 if (doc
->ap_input_slot
|| doc
->ap_manual_feed
)
1242 * First page/sheet gets AP_FIRSTPAGE_* options...
1245 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->ap_input_slot
,
1246 pageinfo
->num_options
,
1247 &(pageinfo
->options
));
1248 pageinfo
->num_options
= cupsAddOption("ManualFeed", doc
->ap_manual_feed
,
1249 pageinfo
->num_options
,
1250 &(pageinfo
->options
));
1252 else if (doc
->page
== (Duplex
+ 2))
1255 * Second page/sheet gets default options...
1258 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->input_slot
,
1259 pageinfo
->num_options
,
1260 &(pageinfo
->options
));
1261 pageinfo
->num_options
= cupsAddOption("ManualFeed", doc
->manual_feed
,
1262 pageinfo
->num_options
,
1263 &(pageinfo
->options
));
1268 * Scan comments until we see something other than %%Page*: or
1272 memcpy(bounding_box
, doc
->bounding_box
, sizeof(bounding_box
));
1274 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1276 if (!strncmp(line
, "%%PageBoundingBox:", 18))
1279 * %%PageBoundingBox: llx lly urx ury
1282 if (sscanf(line
+ 18, "%d%d%d%d", bounding_box
+ 0,
1283 bounding_box
+ 1, bounding_box
+ 2,
1284 bounding_box
+ 3) != 4)
1286 fputs("ERROR: Bad %%PageBoundingBox: comment in file!\n", stderr
);
1287 memcpy(bounding_box
, doc
->bounding_box
,
1288 sizeof(bounding_box
));
1290 else if (doc
->number_up
== 1 && !doc
->fitplot
&& Orientation
)
1292 int temp_bbox
[4]; /* Temporary bounding box */
1295 memcpy(temp_bbox
, bounding_box
, sizeof(temp_bbox
));
1297 fprintf(stderr
, "DEBUG: Orientation = %d\n", Orientation
);
1298 fprintf(stderr
, "DEBUG: original bounding_box = [ %d %d %d %d ]\n",
1299 bounding_box
[0], bounding_box
[1],
1300 bounding_box
[2], bounding_box
[3]);
1301 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
1302 PageWidth
, PageLength
);
1304 switch (Orientation
)
1306 case 1 : /* Landscape */
1307 bounding_box
[0] = PageLength
- temp_bbox
[3];
1308 bounding_box
[1] = temp_bbox
[0];
1309 bounding_box
[2] = PageLength
- temp_bbox
[1];
1310 bounding_box
[3] = temp_bbox
[2];
1313 case 2 : /* Reverse Portrait */
1314 bounding_box
[0] = PageWidth
- temp_bbox
[2];
1315 bounding_box
[1] = PageLength
- temp_bbox
[3];
1316 bounding_box
[2] = PageWidth
- temp_bbox
[0];
1317 bounding_box
[3] = PageLength
- temp_bbox
[1];
1320 case 3 : /* Reverse Landscape */
1321 bounding_box
[0] = temp_bbox
[1];
1322 bounding_box
[1] = PageWidth
- temp_bbox
[2];
1323 bounding_box
[2] = temp_bbox
[3];
1324 bounding_box
[3] = PageWidth
- temp_bbox
[0];
1328 fprintf(stderr
, "DEBUG: updated bounding_box = [ %d %d %d %d ]\n",
1329 bounding_box
[0], bounding_box
[1],
1330 bounding_box
[2], bounding_box
[3]);
1334 else if (!strncmp(line
, "%%PageCustomColors:", 19) ||
1335 !strncmp(line
, "%%PageMedia:", 12) ||
1336 !strncmp(line
, "%%PageOrientation:", 18) ||
1337 !strncmp(line
, "%%PageProcessColors:", 20) ||
1338 !strncmp(line
, "%%PageRequirements:", 18) ||
1339 !strncmp(line
, "%%PageResources:", 16))
1346 else if (!strncmp(line
, "%%PageCustomColors:", 19))
1349 * %%PageCustomColors: ...
1352 else if (!strncmp(line
, "%%PageMedia:", 12))
1358 else if (!strncmp(line
, "%%PageOrientation:", 18))
1361 * %%PageOrientation: ...
1364 else if (!strncmp(line
, "%%PageProcessColors:", 20))
1367 * %%PageProcessColors: ...
1370 else if (!strncmp(line
, "%%PageRequirements:", 18))
1373 * %%PageRequirements: ...
1376 else if (!strncmp(line
, "%%PageResources:", 16))
1379 * %%PageResources: ...
1382 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1385 * %%IncludeFeature: *MainKeyword OptionKeyword
1388 if (doc
->number_up
== 1 &&!doc
->fitplot
)
1389 pageinfo
->num_options
= include_feature(ppd
, line
,
1390 pageinfo
->num_options
,
1391 &(pageinfo
->options
));
1393 else if (strncmp(line
, "%%Include", 9))
1397 if (doc
->number_up
== 1)
1400 * Update the document's composite and page bounding box...
1403 memcpy(pageinfo
->bounding_box
, bounding_box
,
1404 sizeof(pageinfo
->bounding_box
));
1406 if (bounding_box
[0] < doc
->new_bounding_box
[0])
1407 doc
->new_bounding_box
[0] = bounding_box
[0];
1408 if (bounding_box
[1] < doc
->new_bounding_box
[1])
1409 doc
->new_bounding_box
[1] = bounding_box
[1];
1410 if (bounding_box
[2] > doc
->new_bounding_box
[2])
1411 doc
->new_bounding_box
[2] = bounding_box
[2];
1412 if (bounding_box
[3] > doc
->new_bounding_box
[3])
1413 doc
->new_bounding_box
[3] = bounding_box
[3];
1417 * Output the page header as needed...
1420 if (!doc
->slow_order
&& first_page
)
1422 if (!ppd
|| !ppd
->num_filters
)
1423 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
1424 doc
->slow_collate
? 1 : doc
->copies
);
1426 if (doc
->number_up
> 1)
1428 printf("%%%%Page: (%d) %d\n", doc
->page
, doc
->page
);
1429 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1430 PageLeft
, PageBottom
, PageRight
, PageTop
);
1434 printf("%%%%Page: %s %d\n", pageinfo
->label
, doc
->page
);
1435 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1436 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
1437 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
1442 * Copy any page setup commands...
1447 char *page_setup
; /* PageSetup commands to send */
1450 doc_puts(doc
, "%%BeginPageSetup\n");
1452 if (pageinfo
->num_options
> 0)
1454 int i
; /* Looping var */
1455 ppd_option_t
*option
; /* PPD option */
1456 int min_order
; /* Minimum OrderDependency value */
1457 char *doc_setup
, /* DocumentSetup commands to send */
1458 *any_setup
; /* AnySetup commands to send */
1462 * Yes, figure out the minimum OrderDependency value...
1465 if ((option
= ppdFindOption(ppd
, "PageRegion")) != NULL
)
1466 min_order
= option
->order
;
1470 for (i
= 0; i
< pageinfo
->num_options
; i
++)
1471 if ((option
= ppdFindOption(ppd
, pageinfo
->options
[i
].name
)) != NULL
&&
1472 option
->order
< min_order
)
1473 min_order
= option
->order
;
1476 * Mark and extract them...
1479 cupsMarkOptions(ppd
, pageinfo
->num_options
, pageinfo
->options
);
1481 doc_setup
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, min_order
);
1482 any_setup
= ppdEmitString(ppd
, PPD_ORDER_ANY
, min_order
);
1485 * Then send them out...
1490 doc_puts(doc
, doc_setup
);
1496 doc_puts(doc
, any_setup
);
1502 * Output commands for the current page...
1505 page_setup
= ppdEmitString(ppd
, PPD_ORDER_PAGE
, 0);
1509 doc_puts(doc
, page_setup
);
1515 * Prep for the start of the page description...
1518 start_nup(doc
, number
, 1, bounding_box
);
1521 * Copy page setup commands as needed...
1524 if (!strncmp(line
, "%%BeginPageSetup", 16))
1526 int feature
= 0; /* In a Begin/EndFeature block? */
1529 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1531 if (!strncmp(line
, "%%EndPageSetup", 14))
1533 else if (!strncmp(line
, "%%BeginFeature:", 15))
1537 if (doc
->number_up
> 1 || doc
->fitplot
)
1540 else if (!strncmp(line
, "%%EndFeature:", 13))
1544 if (doc
->number_up
> 1 || doc
->fitplot
)
1547 else if (!strncmp(line
, "%%Include", 9))
1550 if (!feature
|| (doc
->number_up
== 1 && !doc
->fitplot
))
1551 doc_write(doc
, line
, linelen
);
1555 * Skip %%EndPageSetup...
1559 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1563 * Finish the PageSetup section as needed...
1567 doc_puts(doc
, "%%EndPageSetup\n");
1570 * Read the rest of the page description...
1578 (!strncmp(line
, "%%Page:", 7) ||
1579 !strncmp(line
, "%%Trailer", 9) ||
1580 !strncmp(line
, "%%EOF", 5)))
1582 else if (!strncmp(line
, "%%BeginDocument", 15) ||
1583 !strncmp(line
, "%ADO_BeginApplication", 21))
1585 doc_write(doc
, line
, linelen
);
1589 else if ((!strncmp(line
, "%%EndDocument", 13) ||
1590 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
1592 doc_write(doc
, line
, linelen
);
1596 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
1597 (!strncmp(line
, "%%BeginData:", 12) &&
1598 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
1601 * Copy binary data...
1604 int bytes
; /* Bytes of data */
1607 doc_write(doc
, line
, linelen
);
1609 bytes
= atoi(strchr(line
, ':') + 1);
1613 if (bytes
> linesize
)
1614 linelen
= cupsFileRead(fp
, line
, linesize
);
1616 linelen
= cupsFileRead(fp
, line
, bytes
);
1621 perror("ERROR: Early end-of-file while reading binary data");
1625 doc_write(doc
, line
, linelen
);
1631 doc_write(doc
, line
, linelen
);
1633 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0);
1636 * Finish up this page and return...
1639 end_nup(doc
, number
);
1641 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
1648 * 'copy_prolog()' - Copy the document prolog section...
1650 * This function expects "line" to be filled with a %%BeginProlog comment line.
1651 * On return, "line" will contain the next line in the file, if any.
1654 static size_t /* O - Length of next line */
1655 copy_prolog(cups_file_t
*fp
, /* I - File to read from */
1656 pstops_doc_t
*doc
, /* I - Document info */
1657 ppd_file_t
*ppd
, /* I - PPD file */
1658 char *line
, /* I - Line buffer */
1659 size_t linelen
, /* I - Length of initial line */
1660 size_t linesize
) /* I - Size of line buffer */
1662 while (strncmp(line
, "%%BeginProlog", 13))
1664 if (!strncmp(line
, "%%BeginSetup", 12) || !strncmp(line
, "%%Page:", 7))
1667 doc_write(doc
, line
, linelen
);
1669 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1673 doc_puts(doc
, "%%BeginProlog\n");
1675 do_prolog(doc
, ppd
);
1677 if (!strncmp(line
, "%%BeginProlog", 13))
1679 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1681 if (!strncmp(line
, "%%EndProlog", 11) ||
1682 !strncmp(line
, "%%BeginSetup", 12) ||
1683 !strncmp(line
, "%%Page:", 7))
1686 doc_write(doc
, line
, linelen
);
1689 if (!strncmp(line
, "%%EndProlog", 11))
1690 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1692 fputs("ERROR: Missing %%EndProlog!\n", stderr
);
1695 doc_puts(doc
, "%%EndProlog\n");
1702 * 'copy_setup()' - Copy the document setup section...
1704 * This function expects "line" to be filled with a %%BeginSetup comment line.
1705 * On return, "line" will contain the next line in the file, if any.
1708 static size_t /* O - Length of next line */
1709 copy_setup(cups_file_t
*fp
, /* I - File to read from */
1710 pstops_doc_t
*doc
, /* I - Document info */
1711 ppd_file_t
*ppd
, /* I - PPD file */
1712 char *line
, /* I - Line buffer */
1713 size_t linelen
, /* I - Length of initial line */
1714 size_t linesize
) /* I - Size of line buffer */
1716 while (strncmp(line
, "%%BeginSetup", 12))
1718 if (!strncmp(line
, "%%Page:", 7))
1721 doc_write(doc
, line
, linelen
);
1723 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1727 doc_puts(doc
, "%%BeginSetup\n");
1731 if (!strncmp(line
, "%%BeginSetup", 12))
1733 while (strncmp(line
, "%%EndSetup", 10))
1735 if (!strncmp(line
, "%%Page:", 7))
1737 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1740 * %%IncludeFeature: *MainKeyword OptionKeyword
1743 if (doc
->number_up
== 1 && !doc
->fitplot
)
1744 doc
->num_options
= include_feature(ppd
, line
, doc
->num_options
,
1747 else if (strncmp(line
, "%%BeginSetup", 12))
1748 doc_write(doc
, line
, linelen
);
1750 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1754 if (!strncmp(line
, "%%EndSetup", 10))
1755 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1757 fputs("ERROR: Missing %%EndSetup!\n", stderr
);
1760 doc_puts(doc
, "%%EndSetup\n");
1767 * 'copy_trailer()' - Copy the document trailer...
1769 * This function expects "line" to be filled with a %%Trailer comment line.
1770 * On return, "line" will contain the next line in the file, if any.
1773 static size_t /* O - Length of next line */
1774 copy_trailer(cups_file_t
*fp
, /* I - File to read from */
1775 pstops_doc_t
*doc
, /* I - Document info */
1776 ppd_file_t
*ppd
, /* I - PPD file */
1777 int number
, /* I - Number of pages */
1778 char *line
, /* I - Line buffer */
1779 size_t linelen
, /* I - Length of initial line */
1780 size_t linesize
) /* I - Size of line buffer */
1783 * Write the trailer comments...
1790 if (!strncmp(line
, "%%EOF", 5))
1792 else if (strncmp(line
, "%%Trailer", 9) &&
1793 strncmp(line
, "%%Pages:", 8) &&
1794 strncmp(line
, "%%BoundingBox:", 14))
1795 fwrite(line
, 1, linelen
, stdout
);
1797 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1800 fprintf(stderr
, "DEBUG: Wrote %d pages...\n", number
);
1802 printf("%%%%Pages: %d\n", number
);
1803 if (doc
->number_up
> 1 || doc
->fitplot
)
1804 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
1805 PageLeft
, PageBottom
, PageRight
, PageTop
);
1807 printf("%%%%BoundingBox: %d %d %d %d\n",
1808 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
1809 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
1816 * 'do_prolog()' - Send the necessary document prolog commands...
1820 do_prolog(pstops_doc_t
*doc
, /* I - Document information */
1821 ppd_file_t
*ppd
) /* I - PPD file */
1823 char *ps
; /* PS commands */
1827 * Send the document prolog commands...
1830 if (ppd
&& ppd
->patches
)
1832 doc_puts(doc
, "%%BeginFeature: *JobPatchFile 1\n");
1833 doc_puts(doc
, ppd
->patches
);
1834 doc_puts(doc
, "\n%%EndFeature\n");
1837 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_PROLOG
, 0.0)) != NULL
)
1844 * Define ESPshowpage here so that applications that define their
1845 * own procedure to do a showpage pick it up...
1848 if (doc
->use_ESPshowpage
)
1849 doc_puts(doc
, "userdict/ESPshowpage/showpage load put\n"
1850 "userdict/showpage{}put\n");
1855 * 'do_setup()' - Send the necessary document setup commands...
1859 do_setup(pstops_doc_t
*doc
, /* I - Document information */
1860 ppd_file_t
*ppd
) /* I - PPD file */
1862 char *ps
; /* PS commands */
1866 * Disable CTRL-D so that embedded files don't cause printing
1870 doc_puts(doc
, "% Disable CTRL-D as an end-of-file marker...\n");
1871 doc_puts(doc
, "userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put\n");
1874 * Mark any options from %%IncludeFeature: comments...
1877 cupsMarkOptions(ppd
, doc
->num_options
, doc
->options
);
1880 * Send all the printer-specific setup commands...
1883 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, 0.0)) != NULL
)
1889 if ((ps
= ppdEmitString(ppd
, PPD_ORDER_ANY
, 0.0)) != NULL
)
1896 * Set the number of copies for the job...
1899 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1901 doc_printf(doc
, "%%RBIBeginNonPPDFeature: *NumCopies %d\n", doc
->copies
);
1903 "%d/languagelevel where{pop languagelevel 2 ge}{false}ifelse\n"
1904 "{1 dict begin/NumCopies exch def currentdict end "
1906 "{userdict/#copies 3 -1 roll put}ifelse\n", doc
->copies
);
1907 doc_puts(doc
, "%RBIEndNonPPDFeature\n");
1911 * If we are doing N-up printing, disable setpagedevice...
1914 if (doc
->number_up
> 1)
1915 doc_puts(doc
, "userdict/setpagedevice{pop}bind put\n");
1918 * Changes to the transfer function must be made AFTER any
1919 * setpagedevice code...
1922 if (doc
->gamma
!= 1.0f
|| doc
->brightness
!= 1.0f
)
1923 doc_printf(doc
, "{ neg 1 add dup 0 lt { pop 1 } { %.3f exp neg 1 add } "
1924 "ifelse %.3f mul } bind settransfer\n",
1925 doc
->gamma
, doc
->brightness
);
1928 * Make sure we have rectclip and rectstroke procedures of some sort...
1932 "% x y w h ESPrc - Clip to a rectangle.\n"
1933 "userdict/ESPrc/rectclip where{pop/rectclip load}\n"
1934 "{{newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
1935 "neg 0 rlineto closepath clip newpath}bind}ifelse put\n");
1938 "% x y w h ESPrf - Fill a rectangle.\n"
1939 "userdict/ESPrf/rectfill where{pop/rectfill load}\n"
1940 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
1941 "neg 0 rlineto closepath fill grestore}bind}ifelse put\n");
1944 "% x y w h ESPrs - Stroke a rectangle.\n"
1945 "userdict/ESPrs/rectstroke where{pop/rectstroke load}\n"
1946 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
1947 "neg 0 rlineto closepath stroke grestore}bind}ifelse put\n");
1950 * Write the page and label prologs...
1953 if (doc
->number_up
== 2 || doc
->number_up
== 6)
1956 * For 2- and 6-up output, rotate the labels to match the orientation
1960 if (Orientation
& 1)
1961 write_label_prolog(doc
, doc
->page_label
, PageBottom
,
1962 PageWidth
- PageLength
+ PageTop
, PageLength
);
1964 write_label_prolog(doc
, doc
->page_label
, PageLeft
, PageRight
,
1968 write_label_prolog(doc
, doc
->page_label
, PageBottom
, PageTop
, PageWidth
);
1973 * 'doc_printf()' - Send a formatted string to stdout and/or the temp file.
1975 * This function should be used for all page-level output that is affected
1976 * by ordering, collation, etc.
1980 doc_printf(pstops_doc_t
*doc
, /* I - Document information */
1981 const char *format
, /* I - Printf-style format string */
1982 ...) /* I - Additional arguments as needed */
1984 va_list ap
; /* Pointer to arguments */
1985 char buffer
[1024]; /* Output buffer */
1986 size_t bytes
; /* Number of bytes to write */
1989 va_start(ap
, format
);
1990 bytes
= vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
1993 if (bytes
> sizeof(buffer
))
1996 "ERROR: doc_printf overflow (%d bytes) detected, aborting!\n",
2001 doc_write(doc
, buffer
, bytes
);
2006 * 'doc_puts()' - Send a nul-terminated string to stdout and/or the temp file.
2008 * This function should be used for all page-level output that is affected
2009 * by ordering, collation, etc.
2013 doc_puts(pstops_doc_t
*doc
, /* I - Document information */
2014 const char *s
) /* I - String to send */
2016 doc_write(doc
, s
, strlen(s
));
2021 * 'doc_write()' - Send data to stdout and/or the temp file.
2025 doc_write(pstops_doc_t
*doc
, /* I - Document information */
2026 const char *s
, /* I - Data to send */
2027 size_t len
) /* I - Number of bytes to send */
2029 if (!doc
->slow_order
)
2030 fwrite(s
, 1, len
, stdout
);
2033 cupsFileWrite(doc
->temp
, s
, len
);
2038 * 'end_nup()' - End processing for N-up printing...
2042 end_nup(pstops_doc_t
*doc
, /* I - Document information */
2043 int number
) /* I - Page number */
2045 if (doc
->number_up
> 1)
2046 doc_puts(doc
, "userdict/ESPsave get restore\n");
2048 switch (doc
->number_up
)
2051 if (doc
->use_ESPshowpage
)
2053 write_labels(doc
, Orientation
);
2054 doc_puts(doc
, "ESPshowpage\n");
2060 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2062 if (Orientation
& 1)
2065 * Rotate the labels back to portrait...
2068 write_labels(doc
, Orientation
- 1);
2070 else if (Orientation
== 0)
2073 * Rotate the labels to landscape...
2076 write_labels(doc
, doc
->normal_landscape
? 1 : 3);
2081 * Rotate the labels to landscape...
2084 write_labels(doc
, doc
->normal_landscape
? 3 : 1);
2087 doc_puts(doc
, "ESPshowpage\n");
2092 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2094 write_labels(doc
, Orientation
);
2095 doc_puts(doc
, "ESPshowpage\n");
2105 * 'include_feature()' - Include a printer option/feature command.
2108 static int /* O - New number of options */
2110 ppd_file_t
*ppd
, /* I - PPD file */
2111 const char *line
, /* I - DSC line */
2112 int num_options
, /* I - Number of options */
2113 cups_option_t
**options
) /* IO - Options */
2115 char name
[255], /* Option name */
2116 value
[255]; /* Option value */
2117 ppd_option_t
*option
; /* Option in file */
2118 ppd_choice_t
*choice
; /* Choice */
2122 * Get the "%%IncludeFeature: *Keyword OptionKeyword" values...
2125 if (sscanf(line
+ 17, "%254s%254s", name
, value
) != 2)
2127 fputs("ERROR: Bad %%IncludeFeature: comment!\n", stderr
);
2128 return (num_options
);
2132 * Find the option and choice...
2135 if ((option
= ppdFindOption(ppd
, name
+ 1)) == NULL
)
2137 fprintf(stderr
, "WARNING: Unknown option \"%s\"!\n", name
+ 1);
2138 return (num_options
);
2141 if (option
->section
== PPD_ORDER_EXIT
||
2142 option
->section
== PPD_ORDER_JCL
)
2144 fprintf(stderr
, "WARNING: Option \"%s\" cannot be included via "
2145 "IncludeFeature!\n", name
+ 1);
2146 return (num_options
);
2149 if ((choice
= ppdFindChoice(option
, value
)) == NULL
)
2151 fprintf(stderr
, "WARNING: Unknown choice \"%s\" for option \"%s\"!\n",
2153 return (num_options
);
2157 * Add the option to the option array and return...
2160 return (cupsAddOption(name
+ 1, value
, num_options
, options
));
2165 * 'parse_text()' - Parse a text value in a comment...
2167 * This function parses a DSC text value as defined on page 36 of the
2168 * DSC specification. Text values are either surrounded by parenthesis
2169 * or whitespace-delimited.
2171 * The value returned is the literal characters for the entire text
2172 * string, including any parenthesis and escape characters.
2175 static char * /* O - Value or NULL on error */
2176 parse_text(const char *start
, /* I - Start of text value */
2177 char **end
, /* O - End of text value */
2178 char *buffer
, /* I - Buffer */
2179 size_t bufsize
) /* I - Size of buffer */
2181 char *bufptr
, /* Pointer in buffer */
2182 *bufend
; /* End of buffer */
2183 int level
; /* Parenthesis level */
2187 * Skip leading whitespace...
2190 while (isspace(*start
& 255))
2194 * Then copy the value...
2199 bufend
= buffer
+ bufsize
- 1;
2201 while (bufptr
< bufend
)
2203 if (isspace(*start
& 255) && !level
)
2210 else if (*start
== ')')
2220 else if (*start
== '\\')
2223 * Copy escaped character...
2226 int i
; /* Looping var */
2230 i
<= 3 && isdigit(start
[i
] & 255) && bufptr
< bufend
;
2231 *bufptr
++ = start
[i
], i
++);
2240 * Return the value and new pointer into the line...
2244 *end
= (char *)start
;
2246 if (bufptr
== bufend
)
2254 * 'set_pstops_options()' - Set pstops options...
2259 pstops_doc_t
*doc
, /* I - Document information */
2260 ppd_file_t
*ppd
, /* I - PPD file */
2261 char *argv
[], /* I - Command-line arguments */
2262 int num_options
, /* I - Number of options */
2263 cups_option_t
*options
) /* I - Options */
2265 const char *val
; /* Option value */
2266 int intval
; /* Integer option value */
2267 ppd_attr_t
*attr
; /* PPD attribute */
2268 ppd_option_t
*option
; /* PPD option */
2269 ppd_choice_t
*choice
; /* PPD choice */
2273 * Initialize document information structure...
2276 memset(doc
, 0, sizeof(pstops_doc_t
));
2278 doc
->job_id
= atoi(argv
[1]);
2279 doc
->user
= argv
[2];
2280 doc
->title
= argv
[3];
2281 doc
->copies
= atoi(argv
[4]);
2283 if (ppd
&& ppd
->landscape
> 0)
2284 doc
->normal_landscape
= 1;
2286 doc
->bounding_box
[0] = (int)PageLeft
;
2287 doc
->bounding_box
[1] = (int)PageBottom
;
2288 doc
->bounding_box
[2] = (int)PageRight
;
2289 doc
->bounding_box
[3] = (int)PageTop
;
2291 doc
->new_bounding_box
[0] = INT_MAX
;
2292 doc
->new_bounding_box
[1] = INT_MAX
;
2293 doc
->new_bounding_box
[2] = INT_MIN
;
2294 doc
->new_bounding_box
[3] = INT_MIN
;
2297 * AP_FIRSTPAGE_InputSlot
2300 doc
->ap_input_slot
= cupsGetOption("AP_FIRSTPAGE_InputSlot", num_options
,
2304 * AP_FIRSTPAGE_ManualFeed
2307 doc
->ap_manual_feed
= cupsGetOption("AP_FIRSTPAGE_ManualFeed", num_options
,
2314 if ((val
= cupsGetOption("brightness", num_options
, options
)) != NULL
)
2317 * Get brightness value from 10 to 1000.
2322 if (intval
< 10 || intval
> 1000)
2324 fprintf(stderr
, "ERROR: Unsupported brightness value %s, using "
2325 "brightness=100!\n", val
);
2326 doc
->brightness
= 1.0f
;
2329 doc
->brightness
= intval
* 0.01f
;
2332 doc
->brightness
= 1.0f
;
2335 * collate, multiple-document-handling
2338 if ((val
= cupsGetOption("multiple-document-handling", num_options
, options
)) != NULL
)
2341 * This IPP attribute is unnecessarily complicated...
2343 * single-document, separate-documents-collated-copies, and
2344 * single-document-new-sheet all require collated copies.
2346 * separate-documents-uncollated-copies allows for uncollated copies.
2349 doc
->collate
= strcasecmp(val
, "separate-documents-uncollated-copies") != 0;
2352 if ((val
= cupsGetOption("Collate", num_options
, options
)) != NULL
&&
2353 (!strcasecmp(val
, "true") ||!strcasecmp(val
, "on") ||
2354 !strcasecmp(val
, "yes")))
2361 if ((val
= cupsGetOption("emit-jcl", num_options
, options
)) != NULL
&&
2362 (!strcasecmp(val
, "false") || !strcasecmp(val
, "off") ||
2363 !strcasecmp(val
, "no") || !strcmp(val
, "0")))
2372 if ((val
= cupsGetOption("fitplot", num_options
, options
)) != NULL
&&
2373 (!strcasecmp(val
, "true") || !strcasecmp(val
, "on") ||
2374 !strcasecmp(val
, "yes")))
2381 if ((val
= cupsGetOption("gamma", num_options
, options
)) != NULL
)
2384 * Get gamma value from 1 to 10000...
2389 if (intval
< 1 || intval
> 10000)
2391 fprintf(stderr
, "ERROR: Unsupported gamma value %s, using "
2392 "gamma=1000!\n", val
);
2396 doc
->gamma
= intval
* 0.001f
;
2405 if ((choice
= ppdFindMarkedChoice(ppd
, "InputSlot")) != NULL
)
2406 doc
->input_slot
= choice
->choice
;
2412 if ((choice
= ppdFindMarkedChoice(ppd
, "ManualFeed")) != NULL
)
2413 doc
->manual_feed
= choice
->choice
;
2415 if ((val
= cupsGetOption("mirror", num_options
, options
)) != NULL
&&
2416 (!strcasecmp(val
, "true") || !strcasecmp(val
, "on") ||
2417 !strcasecmp(val
, "yes")))
2424 if ((val
= cupsGetOption("number-up", num_options
, options
)) != NULL
)
2426 switch (intval
= atoi(val
))
2434 doc
->number_up
= intval
;
2438 "ERROR: Unsupported number-up value %d, using number-up=1!\n",
2451 if ((val
= cupsGetOption("number-up-layout", num_options
, options
)) != NULL
)
2453 if (!strcasecmp(val
, "lrtb"))
2454 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2455 else if (!strcasecmp(val
, "lrbt"))
2456 doc
->number_up_layout
= PSTOPS_LAYOUT_LRBT
;
2457 else if (!strcasecmp(val
, "rltb"))
2458 doc
->number_up_layout
= PSTOPS_LAYOUT_RLTB
;
2459 else if (!strcasecmp(val
, "rlbt"))
2460 doc
->number_up_layout
= PSTOPS_LAYOUT_RLBT
;
2461 else if (!strcasecmp(val
, "tblr"))
2462 doc
->number_up_layout
= PSTOPS_LAYOUT_TBLR
;
2463 else if (!strcasecmp(val
, "tbrl"))
2464 doc
->number_up_layout
= PSTOPS_LAYOUT_TBRL
;
2465 else if (!strcasecmp(val
, "btlr"))
2466 doc
->number_up_layout
= PSTOPS_LAYOUT_BTLR
;
2467 else if (!strcasecmp(val
, "btrl"))
2468 doc
->number_up_layout
= PSTOPS_LAYOUT_BTRL
;
2471 fprintf(stderr
, "ERROR: Unsupported number-up-layout value %s, using "
2472 "number-up-layout=lrtb!\n", val
);
2473 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2477 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2483 if ((val
= cupsGetOption("OutputOrder", num_options
, options
)) != NULL
)
2485 if (!strcasecmp(val
, "Reverse"))
2486 doc
->output_order
= 1;
2491 * Figure out the right default output order from the PPD file...
2494 if ((choice
= ppdFindMarkedChoice(ppd
, "OutputBin")) != NULL
&&
2495 (attr
= ppdFindAttr(ppd
, "PageStackOrder", choice
->choice
)) != NULL
&&
2497 doc
->output_order
= !strcasecmp(attr
->value
, "Reverse");
2498 else if ((attr
= ppdFindAttr(ppd
, "DefaultOutputOrder", NULL
)) != NULL
&&
2500 doc
->output_order
= !strcasecmp(attr
->value
, "Reverse");
2507 if ((val
= cupsGetOption("page-border", num_options
, options
)) != NULL
)
2509 if (!strcasecmp(val
, "none"))
2510 doc
->page_border
= PSTOPS_BORDERNONE
;
2511 else if (!strcasecmp(val
, "single"))
2512 doc
->page_border
= PSTOPS_BORDERSINGLE
;
2513 else if (!strcasecmp(val
, "single-thick"))
2514 doc
->page_border
= PSTOPS_BORDERSINGLE2
;
2515 else if (!strcasecmp(val
, "double"))
2516 doc
->page_border
= PSTOPS_BORDERDOUBLE
;
2517 else if (!strcasecmp(val
, "double-thick"))
2518 doc
->page_border
= PSTOPS_BORDERDOUBLE2
;
2521 fprintf(stderr
, "ERROR: Unsupported page-border value %s, using "
2522 "page-border=none!\n", val
);
2523 doc
->page_border
= PSTOPS_BORDERNONE
;
2527 doc
->page_border
= PSTOPS_BORDERNONE
;
2533 doc
->page_label
= cupsGetOption("page-label", num_options
, options
);
2539 doc
->page_ranges
= cupsGetOption("page-ranges", num_options
, options
);
2545 doc
->page_set
= cupsGetOption("page-set", num_options
, options
);
2548 * Now figure out if we have to force collated copies, etc.
2551 if (ppd
&& ppd
->manual_copies
&& Duplex
&& doc
->copies
> 1)
2554 * Force collated copies when printing a duplexed document to
2555 * a non-PS printer that doesn't do hardware copy generation.
2556 * Otherwise the copies will end up on the front/back side of
2564 * See if we have to filter the fast or slow way...
2567 if (doc
->collate
&& doc
->copies
> 1)
2570 * See if we need to manually collate the pages...
2573 doc
->slow_collate
= 1;
2575 if ((choice
= ppdFindMarkedChoice(ppd
, "Collate")) != NULL
&&
2576 !strcasecmp(choice
->choice
, "True"))
2579 * Hardware collate option is selected, see if the option is
2580 * conflicting - if not, collate in hardware. Otherwise,
2581 * turn the hardware collate option off...
2584 if ((option
= ppdFindOption(ppd
, "Collate")) != NULL
&&
2585 !option
->conflicted
)
2586 doc
->slow_collate
= 0;
2588 ppdMarkOption(ppd
, "Collate", "False");
2592 doc
->slow_collate
= 0;
2594 if (!ppdFindOption(ppd
, "OutputOrder") && doc
->output_order
)
2595 doc
->slow_order
= 1;
2597 doc
->slow_order
= 0;
2600 (doc
->slow_collate
|| doc
->slow_order
||
2601 ((attr
= ppdFindAttr(ppd
, "cupsEvenDuplex", NULL
)) != NULL
&&
2602 attr
->value
&& !strcasecmp(attr
->value
, "true"))))
2603 doc
->slow_duplex
= 1;
2605 doc
->slow_duplex
= 0;
2608 * Create a temporary file for page data if we need to filter slowly...
2611 if (doc
->slow_order
|| doc
->slow_collate
)
2613 if ((doc
->temp
= cupsTempFile2(doc
->tempfile
,
2614 sizeof(doc
->tempfile
))) == NULL
)
2616 fprintf(stderr
, "ERROR: Unable to create temporary file: %s\n",
2623 * Figure out if we should use ESPshowpage or not...
2626 if (doc
->page_label
|| getenv("CLASSIFICATION") || doc
->number_up
> 1 ||
2630 * Yes, use ESPshowpage...
2633 doc
->use_ESPshowpage
= 1;
2636 fprintf(stderr
, "DEBUG: slow_collate=%d, slow_duplex=%d, slow_order=%d\n",
2637 doc
->slow_collate
, doc
->slow_duplex
, doc
->slow_order
);
2642 * 'skip_page()' - Skip past a page that won't be printed...
2645 static size_t /* O - Length of next line */
2646 skip_page(cups_file_t
*fp
, /* I - File to read from */
2647 char *line
, /* I - Line buffer */
2648 size_t linelen
, /* I - Length of initial line */
2649 size_t linesize
) /* I - Size of line buffer */
2651 int level
; /* Embedded document level */
2656 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
2659 (!strncmp(line
, "%%Page:", 7) || !strncmp(line
, "%%Trailer", 9)))
2661 else if (!strncmp(line
, "%%BeginDocument", 15) ||
2662 !strncmp(line
, "%ADO_BeginApplication", 21))
2664 else if ((!strncmp(line
, "%%EndDocument", 13) ||
2665 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
2667 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
2668 (!strncmp(line
, "%%BeginData:", 12) &&
2669 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
2672 * Skip binary data...
2675 int bytes
; /* Bytes of data */
2678 bytes
= atoi(strchr(line
, ':') + 1);
2682 if (bytes
> linesize
)
2683 linelen
= cupsFileRead(fp
, line
, linesize
);
2685 linelen
= cupsFileRead(fp
, line
, bytes
);
2690 perror("ERROR: Early end-of-file while reading binary data");
2704 * 'start_nup()' - Start processing for N-up printing...
2708 start_nup(pstops_doc_t
*doc
, /* I - Document information */
2709 int number
, /* I - Page number */
2710 int show_border
, /* I - Show the border? */
2711 const int *bounding_box
) /* I - BoundingBox value */
2713 int pos
; /* Position on page */
2714 int x
, y
; /* Relative position of subpage */
2715 float w
, l
, /* Width and length of subpage */
2716 tx
, ty
; /* Translation values for subpage */
2717 float pagew
, /* Printable width of page */
2718 pagel
; /* Printable height of page */
2719 int bboxw
, /* BoundingBox width */
2720 bboxl
; /* BoundingBox height */
2723 if (doc
->number_up
> 1)
2724 doc_puts(doc
, "userdict/ESPsave save put\n");
2727 doc_printf(doc
, "%.1f 0.0 translate -1 1 scale\n", PageWidth
);
2729 pos
= (number
- 1) % doc
->number_up
;
2730 pagew
= PageRight
- PageLeft
;
2731 pagel
= PageTop
- PageBottom
;
2735 bboxw
= bounding_box
[2] - bounding_box
[0];
2736 bboxl
= bounding_box
[3] - bounding_box
[1];
2744 fprintf(stderr
, "DEBUG: pagew = %.1f, pagel = %.1f\n", pagew
, pagel
);
2745 fprintf(stderr
, "DEBUG: bboxw = %d, bboxl = %d\n", bboxw
, bboxl
);
2746 fprintf(stderr
, "DEBUG: PageLeft = %.1f, PageRight = %.1f\n",
2747 PageLeft
, PageRight
);
2748 fprintf(stderr
, "DEBUG: PageTop = %.1f, PageBottom = %.1f\n",
2749 PageTop
, PageBottom
);
2750 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
2751 PageWidth
, PageLength
);
2753 switch (Orientation
)
2755 case 1 : /* Landscape */
2756 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", PageLength
);
2758 case 2 : /* Reverse Portrait */
2759 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", PageWidth
,
2762 case 3 : /* Reverse Landscape */
2763 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", PageWidth
);
2767 if (Duplex
&& doc
->number_up
> 1 && ((number
/ doc
->number_up
) & 1))
2768 doc_printf(doc
, "%.1f %.1f translate\n", PageWidth
- PageRight
, PageBottom
);
2769 else if (doc
->number_up
> 1 || doc
->fitplot
)
2770 doc_printf(doc
, "%.1f %.1f translate\n", PageLeft
, PageBottom
);
2772 switch (doc
->number_up
)
2778 l
= w
* bboxl
/ bboxw
;
2783 w
= l
* bboxw
/ bboxl
;
2786 tx
= 0.5 * (pagew
- w
);
2787 ty
= 0.5 * (pagel
- l
);
2789 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n", tx
, ty
,
2790 w
/ bboxw
, l
/ bboxl
);
2800 if (Orientation
& 1)
2804 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2808 l
= w
* bboxl
/ bboxw
;
2810 if (l
> (pagew
* 0.5))
2813 w
= l
* bboxw
/ bboxl
;
2816 tx
= 0.5 * (pagew
* 0.5 - l
);
2817 ty
= 0.5 * (pagel
- w
);
2819 if (doc
->normal_landscape
)
2820 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2822 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2824 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2825 ty
, tx
+ pagew
* 0.5 * x
, w
/ bboxw
, l
/ bboxl
);
2831 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2835 w
= l
* bboxw
/ bboxl
;
2837 if (w
> (pagel
* 0.5))
2840 l
= w
* bboxl
/ bboxw
;
2843 tx
= 0.5 * (pagel
* 0.5 - w
);
2844 ty
= 0.5 * (pagew
- l
);
2846 if (doc
->normal_landscape
)
2847 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2849 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2851 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2852 tx
+ pagel
* 0.5 * x
, ty
, w
/ bboxw
, l
/ bboxl
);
2857 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2868 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2871 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2875 l
= w
* bboxl
/ bboxw
;
2877 if (l
> (pagel
* 0.5))
2880 w
= l
* bboxw
/ bboxl
;
2883 tx
= 0.5 * (pagew
* 0.5 - w
);
2884 ty
= 0.5 * (pagel
* 0.5 - l
);
2886 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2887 tx
+ x
* pagew
* 0.5, ty
+ y
* pagel
* 0.5,
2888 w
/ bboxw
, l
/ bboxl
);
2892 if (Orientation
& 1)
2894 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2899 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2902 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2910 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2913 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2918 l
= w
* bboxl
/ bboxw
;
2920 if (l
> (pagew
* 0.333))
2923 w
= l
* bboxw
/ bboxl
;
2926 tx
= 0.5 * (pagel
- 2 * w
);
2927 ty
= 0.5 * (pagew
- 3 * l
);
2929 if (doc
->normal_landscape
)
2930 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
2932 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
2934 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2935 tx
+ x
* w
, ty
+ y
* l
, l
/ bboxl
, w
/ bboxw
);
2939 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2944 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2947 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2955 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2958 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2963 w
= l
* bboxw
/ bboxl
;
2965 if (w
> (pagel
* 0.333))
2968 l
= w
* bboxl
/ bboxw
;
2971 tx
= 0.5 * (pagel
- 3 * w
);
2972 ty
= 0.5 * (pagew
- 2 * l
);
2974 if (doc
->normal_landscape
)
2975 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
2977 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
2979 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2980 tx
+ w
* x
, ty
+ l
* y
, w
/ bboxw
, l
/ bboxl
);
2986 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2997 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3000 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3004 l
= w
* bboxl
/ bboxw
;
3006 if (l
> (pagel
* 0.333))
3009 w
= l
* bboxw
/ bboxl
;
3012 tx
= 0.5 * (pagew
* 0.333 - w
);
3013 ty
= 0.5 * (pagel
* 0.333 - l
);
3015 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3016 tx
+ x
* pagew
* 0.333, ty
+ y
* pagel
* 0.333,
3017 w
/ bboxw
, l
/ bboxl
);
3021 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
3032 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
3035 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
3039 l
= w
* bboxl
/ bboxw
;
3041 if (l
> (pagel
* 0.25))
3044 w
= l
* bboxw
/ bboxl
;
3047 tx
= 0.5 * (pagew
* 0.25 - w
);
3048 ty
= 0.5 * (pagel
* 0.25 - l
);
3050 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
3051 tx
+ x
* pagew
* 0.25, ty
+ y
* pagel
* 0.25,
3052 w
/ bboxw
, l
/ bboxl
);
3057 * Draw borders as necessary...
3060 if (doc
->page_border
&& show_border
)
3062 int rects
; /* Number of border rectangles */
3063 float fscale
, /* Scaling value for points */
3064 margin
; /* Current margin for borders */
3067 rects
= (doc
->page_border
& PSTOPS_BORDERDOUBLE
) ? 2 : 1;
3068 fscale
= PageWidth
/ w
;
3069 margin
= 2.25 * fscale
;
3072 * Set the line width and color...
3075 doc_puts(doc
, "gsave\n");
3076 doc_printf(doc
, "%.3f setlinewidth 0 setgray newpath\n",
3077 (doc
->page_border
& PSTOPS_BORDERTHICK
) ? 0.5 * fscale
:
3081 * Draw border boxes...
3084 for (; rects
> 0; rects
--, margin
+= 2 * fscale
)
3085 if (doc
->number_up
> 1)
3086 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3087 margin
- 2.25 * fscale
,
3088 margin
- 2.25 * fscale
,
3089 bboxw
+ 4.5 * fscale
- 2 * margin
,
3090 bboxl
+ 4.5 * fscale
- 2 * margin
);
3092 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3094 PageBottom
+ margin
,
3095 PageRight
- PageLeft
- 2 * margin
,
3096 PageTop
- PageBottom
- 2 * margin
);
3099 * Restore pen settings...
3102 doc_puts(doc
, "grestore\n");
3108 * Clip the page that follows to the bounding box of the page...
3111 doc_printf(doc
, "%d %d translate\n", -bounding_box
[0],
3113 doc_printf(doc
, "%d %d %d %d ESPrc\n", bounding_box
[0], bounding_box
[1],
3116 else if (doc
->number_up
> 1)
3119 * Clip the page that follows to the default page size...
3122 doc_printf(doc
, "0 0 %d %d ESPrc\n", bboxw
, bboxl
);
3128 * 'write_label_prolog()' - Write the prolog with the classification
3133 write_label_prolog(pstops_doc_t
*doc
, /* I - Document info */
3134 const char *label
, /* I - Page label */
3135 float bottom
, /* I - Bottom position in points */
3136 float top
, /* I - Top position in points */
3137 float width
) /* I - Width in points */
3139 const char *classification
; /* CLASSIFICATION environment variable */
3140 const char *ptr
; /* Temporary string pointer */
3144 * First get the current classification...
3147 if ((classification
= getenv("CLASSIFICATION")) == NULL
)
3148 classification
= "";
3149 if (strcmp(classification
, "none") == 0)
3150 classification
= "";
3153 * If there is nothing to show, bind an empty 'write labels' procedure
3157 if (!classification
[0] && (label
== NULL
|| !label
[0]))
3159 doc_puts(doc
, "userdict/ESPwl{}bind put\n");
3164 * Set the classification + page label string...
3167 doc_puts(doc
, "userdict");
3168 if (!strcmp(classification
, "confidential"))
3169 doc_puts(doc
, "/ESPpl(CONFIDENTIAL");
3170 else if (!strcmp(classification
, "classified"))
3171 doc_puts(doc
, "/ESPpl(CLASSIFIED");
3172 else if (!strcmp(classification
, "secret"))
3173 doc_puts(doc
, "/ESPpl(SECRET");
3174 else if (!strcmp(classification
, "topsecret"))
3175 doc_puts(doc
, "/ESPpl(TOP SECRET");
3176 else if (!strcmp(classification
, "unclassified"))
3177 doc_puts(doc
, "/ESPpl(UNCLASSIFIED");
3180 doc_puts(doc
, "/ESPpl(");
3182 for (ptr
= classification
; *ptr
; ptr
++)
3184 if (*ptr
< 32 || *ptr
> 126)
3185 doc_printf(doc
, "\\%03o", *ptr
);
3186 else if (*ptr
== '_')
3188 else if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
3189 doc_printf(doc
, "\\%c", *ptr
);
3191 doc_printf(doc
, "%c", *ptr
);
3197 if (classification
[0])
3198 doc_puts(doc
, " - ");
3201 * Quote the label string as needed...
3204 for (ptr
= label
; *ptr
; ptr
++)
3206 if (*ptr
< 32 || *ptr
> 126)
3207 doc_printf(doc
, "\\%03o", *ptr
);
3208 else if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
3209 doc_printf(doc
, "\\%c", *ptr
);
3211 doc_printf(doc
, "%c", *ptr
);
3215 doc_puts(doc
, ")put\n");
3218 * Then get a 14 point Helvetica-Bold font...
3221 doc_puts(doc
, "userdict/ESPpf /Helvetica-Bold findfont 14 scalefont put\n");
3224 * Finally, the procedure to write the labels on the page...
3227 doc_puts(doc
, "userdict/ESPwl{\n");
3228 doc_puts(doc
, " ESPpf setfont\n");
3229 doc_printf(doc
, " ESPpl stringwidth pop dup 12 add exch -0.5 mul %.0f add\n",
3231 doc_puts(doc
, " 1 setgray\n");
3232 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrf\n", bottom
- 2.0);
3233 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrf\n", top
- 18.0);
3234 doc_puts(doc
, " 0 setgray\n");
3235 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrs\n", bottom
- 2.0);
3236 doc_printf(doc
, " dup 6 sub %.0f 3 index 20 ESPrs\n", top
- 18.0);
3237 doc_printf(doc
, " dup %.0f moveto ESPpl show\n", bottom
+ 2.0);
3238 doc_printf(doc
, " %.0f moveto ESPpl show\n", top
- 14.0);
3239 doc_puts(doc
, "pop\n");
3240 doc_puts(doc
, "}bind put\n");
3245 * 'write_labels()' - Write the actual page labels.
3247 * This function is a copy of the one in common.c since we need to
3248 * use doc_puts/doc_printf instead of puts/printf...
3252 write_labels(pstops_doc_t
*doc
, /* I - Document information */
3253 int orient
) /* I - Orientation of the page */
3255 float width
, /* Width of page */
3256 length
; /* Length of page */
3259 doc_puts(doc
, "gsave\n");
3261 if ((orient
^ Orientation
) & 1)
3269 length
= PageLength
;
3274 case 1 : /* Landscape */
3275 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", length
);
3277 case 2 : /* Reverse Portrait */
3278 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", width
, length
);
3280 case 3 : /* Reverse Landscape */
3281 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", width
);
3285 doc_puts(doc
, "ESPwl\n");
3286 doc_puts(doc
, "grestore\n");
3291 * End of "$Id: pstops.c 6320 2007-03-08 13:36:56Z mike $".