]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/pstops.c
2 * "$Id: pstops.c 6025 2006-10-11 14:59:20Z mike $"
4 * PostScript filter for the Common UNIX Printing System (CUPS).
6 * Copyright 1993-2006 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_labels(pstops_doc_t
*doc
, int orient
);
212 * 'main()' - Main entry...
215 int /* O - Exit status */
216 main(int argc
, /* I - Number of command-line args */
217 char *argv
[]) /* I - Command-line arguments */
219 pstops_doc_t doc
; /* Document information */
220 cups_file_t
*fp
; /* Print file */
221 ppd_file_t
*ppd
; /* PPD file */
222 int num_options
; /* Number of print options */
223 cups_option_t
*options
; /* Print options */
224 char line
[8192]; /* Line buffer */
225 size_t len
; /* Length of line buffer */
229 * Make sure status messages are not buffered...
232 setbuf(stderr
, NULL
);
235 * Check command-line...
238 if (argc
< 6 || argc
> 7)
240 fputs("ERROR: pstops job-id user title copies options [file]\n", stderr
);
245 * If we have 7 arguments, print the file named on the command-line.
246 * Otherwise, send stdin instead...
250 fp
= cupsFileStdin();
254 * Try to open the print file...
257 if ((fp
= cupsFileOpen(argv
[6], "r")) == NULL
)
259 fprintf(stderr
, "ERROR: Unable to open print file \"%s\" - %s\n",
260 argv
[6], strerror(errno
));
266 * Read the first line to see if we have DSC comments...
269 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
271 fputs("ERROR: Empty print file!\n", stderr
);
276 * Process command-line options...
280 num_options
= cupsParseOptions(argv
[5], 0, &options
);
281 ppd
= SetCommonOptions(num_options
, options
, 1);
283 set_pstops_options(&doc
, ppd
, argv
, num_options
, options
);
286 * Write any "exit server" options that have been selected...
289 ppdEmit(ppd
, stdout
, PPD_ORDER_EXIT
);
292 * Write any JCL commands that are needed to print PostScript code...
296 ppdEmitJCL(ppd
, stdout
, doc
.job_id
, doc
.user
, doc
.title
);
299 * Start with a DSC header...
302 puts("%!PS-Adobe-3.0");
305 * Skip leading PJL in the document...
308 while (!strncmp(line
, "\033%-12345X", 9) || !strncmp(line
, "@PJL ", 5))
311 * Yup, we have leading PJL fun, so skip it until we hit the line
312 * with "ENTER LANGUAGE"...
315 fputs("DEBUG: Skipping PJL header...\n", stderr
);
317 while (strstr(line
, "ENTER LANGUAGE") == NULL
&& strncmp(line
, "%!", 2))
318 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
321 if (!strncmp(line
, "%!", 2))
324 if ((len
= cupsFileGetLine(fp
, line
, sizeof(line
))) == 0)
329 * Now see if the document conforms to the Adobe Document Structuring
333 if (!strncmp(line
, "%!PS-Adobe-", 11))
336 * Yes, filter the document...
339 copy_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
344 * No, display an error message and treat the file as if it contains
348 copy_non_dsc(fp
, &doc
, ppd
, line
, len
, sizeof(line
));
352 * Send %%EOF as needed...
359 * End the job with the appropriate JCL command or CTRL-D...
364 if (ppd
&& ppd
->jcl_end
)
365 ppdEmitJCLEnd(ppd
, stdout
);
371 * Close files and remove the temporary file if needed...
376 cupsFileClose(doc
.temp
);
377 unlink(doc
.tempfile
);
381 cupsFreeOptions(num_options
, options
);
390 * 'add_page()' - Add a page to the pages array...
393 static pstops_page_t
* /* O - New page info object */
394 add_page(pstops_doc_t
*doc
, /* I - Document information */
395 const char *label
) /* I - Page label */
397 pstops_page_t
*pageinfo
; /* New page info object */
401 doc
->pages
= cupsArrayNew(NULL
, NULL
);
405 fprintf(stderr
, "EMERG: Unable to allocate memory for pages array: %s\n",
410 if ((pageinfo
= calloc(1, sizeof(pstops_page_t
))) == NULL
)
412 fprintf(stderr
, "EMERG: Unable to allocate memory for page info: %s\n",
417 pageinfo
->label
= strdup(label
);
418 pageinfo
->offset
= cupsFileTell(doc
->temp
);
420 cupsArrayAdd(doc
->pages
, pageinfo
);
429 * 'check_range()' - Check to see if the current page is selected for
433 static int /* O - 1 if selected, 0 otherwise */
434 check_range(pstops_doc_t
*doc
, /* I - Document information */
435 int page
) /* I - Page number */
437 const char *range
; /* Pointer into range string */
438 int lower
, upper
; /* Lower and upper page numbers */
444 * See if we only print even or odd pages...
447 if (!strcasecmp(doc
->page_set
, "even") && (page
& 1))
450 if (!strcasecmp(doc
->page_set
, "odd") && !(page
& 1))
454 if (!doc
->page_ranges
)
455 return (1); /* No range, print all pages... */
457 for (range
= doc
->page_ranges
; *range
!= '\0';)
463 upper
= strtol(range
, (char **)&range
, 10);
467 lower
= strtol(range
, (char **)&range
, 10);
472 if (!isdigit(*range
& 255))
475 upper
= strtol(range
, (char **)&range
, 10);
481 if (page
>= lower
&& page
<= upper
)
495 * 'copy_bytes()' - Copy bytes from the input file to stdout...
499 copy_bytes(cups_file_t
*fp
, /* I - File to read from */
500 off_t offset
, /* I - Offset to page data */
501 size_t length
) /* I - Length of page data */
503 char buffer
[8192]; /* Data buffer */
504 ssize_t nbytes
; /* Number of bytes read */
505 size_t nleft
; /* Number of bytes left/remaining */
510 if (cupsFileSeek(fp
, offset
) < 0)
512 fprintf(stderr
, "ERROR: Unable to seek to offset " CUPS_LLFMT
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 printf("%s\n", line
);
670 else if (!strncmp(line
, "%%Title:", 8))
673 printf("%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 printf("%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 printf("%%%%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 printf("%%RBINumCopies: %d\n", doc
->copies
);
738 * Tell the document processor the duplex option that is required...
742 puts("%%Requirements: duplex");
745 * Apple uses RBI comments for various non-PPD options...
748 puts("%RBINumCopies: 1");
751 puts("%%Pages: (atend)");
752 puts("%%BoundingBox: (atend)");
753 puts("%%EndComments");
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 fwrite(line
, 1, linelen
, stdout
);
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...
917 if (ppd
&& ppd
->jcl_end
)
918 ppdEmitJCLEnd(ppd
, stdout
);
922 ppdEmitJCL(ppd
, stdout
, doc
->job_id
, doc
->user
, doc
->title
);
925 puts("%!PS-Adobe-3.0");
926 puts("%%Pages: (atend)");
927 puts("%%BoundingBox: (atend)");
928 puts("%%EndComments");
931 * Then copy all of the pages...
934 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayLast(doc
->pages
) :
935 (pstops_page_t
*)cupsArrayFirst(doc
->pages
);
941 if (!ppd
|| !ppd
->num_filters
)
942 fprintf(stderr
, "PAGE: %d 1\n", number
);
944 if (doc
->number_up
> 1)
946 printf("%%%%Page: (%d) %d\n", number
, number
);
947 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
948 PageLeft
, PageBottom
, PageRight
, PageTop
);
952 printf("%%%%Page: %s %d\n", pageinfo
->label
, number
);
953 printf("%%%%PageBoundingBox: %d %d %d %d\n",
954 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
955 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
958 copy_bytes(doc
->temp
, pageinfo
->offset
, pageinfo
->length
);
960 pageinfo
= doc
->slow_order
? (pstops_page_t
*)cupsArrayPrev(doc
->pages
) :
961 (pstops_page_t
*)cupsArrayNext(doc
->pages
);
967 * Restore the old showpage operator as needed...
970 if (doc
->use_ESPshowpage
)
971 puts("userdict/showpage/ESPshowpage load put\n");
974 * Write/copy the trailer...
977 linelen
= copy_trailer(fp
, doc
, ppd
, number
, line
, linelen
, linesize
);
982 * 'copy_non_dsc()' - Copy a document that does not conform to the DSC...
984 * This function expects "line" to be filled with the %! comment line.
988 copy_non_dsc(cups_file_t
*fp
, /* I - File to read from */
989 pstops_doc_t
*doc
, /* I - Document info */
990 ppd_file_t
*ppd
, /* I - PPD file */
991 char *line
, /* I - Line buffer */
992 size_t linelen
, /* I - Length of initial line */
993 size_t linesize
) /* I - Size of line buffer */
995 int copy
; /* Current copy */
996 char buffer
[8192]; /* Copy buffer */
997 int bytes
; /* Number of bytes copied */
1001 * First let the user know that they are attempting to print a file
1002 * that may not print correctly...
1005 fputs("WARNING: This document does not conform to the Adobe Document "
1006 "Structuring Conventions and may not print correctly!\n", stderr
);
1009 * Then write a standard DSC comment section...
1012 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", PageLeft
, PageBottom
,
1013 PageRight
, PageTop
);
1015 if (doc
->slow_collate
&& doc
->copies
> 1)
1016 printf("%%%%Pages: %d\n", doc
->copies
);
1020 WriteTextComment("For", doc
->user
);
1021 WriteTextComment("Title", doc
->title
);
1023 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1026 * Tell the document processor the copy and duplex options
1027 * that are required...
1030 printf("%%%%Requirements: numcopies(%d)%s%s\n", doc
->copies
,
1031 doc
->collate
? " collate" : "",
1032 Duplex
? " duplex" : "");
1035 * Apple uses RBI comments for various non-PPD options...
1038 printf("%%RBINumCopies: %d\n", doc
->copies
);
1043 * Tell the document processor the duplex option that is required...
1047 puts("%%Requirements: duplex");
1050 * Apple uses RBI comments for various non-PPD options...
1053 puts("%RBINumCopies: 1");
1056 puts("%%EndComments");
1059 * Then the prolog...
1062 puts("%%BeginProlog");
1064 do_prolog(doc
, ppd
);
1066 puts("%%EndProlog");
1069 * Then the setup section...
1072 puts("%%BeginSetup");
1079 * Finally, embed a copy of the file inside a %%Page...
1082 if (!ppd
|| !ppd
->num_filters
)
1083 fprintf(stderr
, "PAGE: 1 %d\n", doc
->temp
? 1 : doc
->copies
);
1085 puts("%%Page: 1 1");
1086 puts("%%BeginPageSetup");
1087 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1088 puts("%%EndPageSetup");
1089 puts("%%BeginDocument: nondsc");
1091 fwrite(line
, linelen
, 1, stdout
);
1094 cupsFileWrite(doc
->temp
, line
, linelen
);
1096 while ((bytes
= cupsFileRead(fp
, buffer
, sizeof(buffer
))) > 0)
1098 fwrite(buffer
, 1, bytes
, stdout
);
1101 cupsFileWrite(doc
->temp
, buffer
, bytes
);
1104 puts("%%EndDocument");
1106 if (doc
->use_ESPshowpage
)
1108 WriteLabels(Orientation
);
1109 puts("ESPshowpage");
1115 * Reopen the temporary file for reading...
1118 cupsFileClose(doc
->temp
);
1120 doc
->temp
= cupsFileOpen(doc
->tempfile
, "r");
1123 * Make the additional copies as needed...
1126 for (copy
= 1; copy
< doc
->copies
; copy
++)
1128 if (!ppd
|| !ppd
->num_filters
)
1129 fputs("PAGE: 1 1\n", stderr
);
1131 printf("%%%%Page: %d %d\n", copy
+ 1, copy
+ 1);
1132 puts("%%BeginPageSetup");
1133 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
1134 puts("%%EndPageSetup");
1135 puts("%%BeginDocument: nondsc");
1137 copy_bytes(doc
->temp
, 0, 0);
1139 puts("%%EndDocument");
1141 if (doc
->use_ESPshowpage
)
1143 WriteLabels(Orientation
);
1144 puts("ESPshowpage");
1150 * Restore the old showpage operator as needed...
1153 if (doc
->use_ESPshowpage
)
1154 puts("userdict/showpage/ESPshowpage load put\n");
1159 * 'copy_page()' - Copy a page description...
1161 * This function expects "line" to be filled with a %%Page comment line.
1162 * On return, "line" will contain the next line in the file, if any.
1165 static size_t /* O - Length of next line */
1166 copy_page(cups_file_t
*fp
, /* I - File to read from */
1167 pstops_doc_t
*doc
, /* I - Document info */
1168 ppd_file_t
*ppd
, /* I - PPD file */
1169 int number
, /* I - Current page number */
1170 char *line
, /* I - Line buffer */
1171 size_t linelen
, /* I - Length of initial line */
1172 size_t linesize
) /* I - Size of line buffer */
1174 char label
[256], /* Page label string */
1175 *ptr
; /* Pointer into line */
1176 int level
; /* Embedded document level */
1177 pstops_page_t
*pageinfo
; /* Page information */
1178 int first_page
; /* First page on N-up output? */
1179 int bounding_box
[4]; /* PageBoundingBox */
1183 * Get the page label for this page...
1186 first_page
= is_first_page(number
);
1188 if (!parse_text(line
+ 7, &ptr
, label
, sizeof(label
)))
1190 fputs("ERROR: Bad %%Page: comment in file!\n", stderr
);
1194 else if (strtol(ptr
, &ptr
, 10) == LONG_MAX
|| !isspace(*ptr
& 255))
1196 fputs("ERROR: Bad %%Page: comment in file!\n", stderr
);
1201 * Create or update the current output page...
1205 pageinfo
= add_page(doc
, label
);
1207 pageinfo
= (pstops_page_t
*)cupsArrayLast(doc
->pages
);
1210 * Handle first page override...
1213 if (doc
->ap_input_slot
|| doc
->ap_manual_feed
)
1218 * First page/sheet gets AP_FIRSTPAGE_* options...
1221 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->ap_input_slot
,
1222 pageinfo
->num_options
,
1223 &(pageinfo
->options
));
1224 pageinfo
->num_options
= cupsAddOption("ManualFeed", doc
->ap_manual_feed
,
1225 pageinfo
->num_options
,
1226 &(pageinfo
->options
));
1228 else if (doc
->page
== (Duplex
+ 2))
1231 * Second page/sheet gets default options...
1234 pageinfo
->num_options
= cupsAddOption("InputSlot", doc
->input_slot
,
1235 pageinfo
->num_options
,
1236 &(pageinfo
->options
));
1237 pageinfo
->num_options
= cupsAddOption("ManualFeed", doc
->manual_feed
,
1238 pageinfo
->num_options
,
1239 &(pageinfo
->options
));
1244 * Scan comments until we see something other than %%Page*: or
1248 memcpy(bounding_box
, doc
->bounding_box
, sizeof(bounding_box
));
1250 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1252 if (!strncmp(line
, "%%PageBoundingBox:", 18))
1255 * %%PageBoundingBox: llx lly urx ury
1258 if (sscanf(line
+ 18, "%d%d%d%d", bounding_box
+ 0,
1259 bounding_box
+ 1, bounding_box
+ 2,
1260 bounding_box
+ 3) != 4)
1262 fputs("ERROR: Bad %%PageBoundingBox: comment in file!\n", stderr
);
1263 memcpy(bounding_box
, doc
->bounding_box
,
1264 sizeof(bounding_box
));
1268 else if (!strncmp(line
, "%%PageCustomColors:", 19) ||
1269 !strncmp(line
, "%%PageMedia:", 12) ||
1270 !strncmp(line
, "%%PageOrientation:", 18) ||
1271 !strncmp(line
, "%%PageProcessColors:", 20) ||
1272 !strncmp(line
, "%%PageRequirements:", 18) ||
1273 !strncmp(line
, "%%PageResources:", 16))
1280 else if (!strncmp(line
, "%%PageCustomColors:", 19))
1283 * %%PageCustomColors: ...
1286 else if (!strncmp(line
, "%%PageMedia:", 12))
1292 else if (!strncmp(line
, "%%PageOrientation:", 18))
1295 * %%PageOrientation: ...
1298 else if (!strncmp(line
, "%%PageProcessColors:", 20))
1301 * %%PageProcessColors: ...
1304 else if (!strncmp(line
, "%%PageRequirements:", 18))
1307 * %%PageRequirements: ...
1310 else if (!strncmp(line
, "%%PageResources:", 16))
1313 * %%PageResources: ...
1316 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1319 * %%IncludeFeature: *MainKeyword OptionKeyword
1322 if (doc
->number_up
== 1 &&!doc
->fitplot
)
1323 pageinfo
->num_options
= include_feature(ppd
, line
,
1324 pageinfo
->num_options
,
1325 &(pageinfo
->options
));
1327 else if (strncmp(line
, "%%Include", 9))
1331 if (doc
->number_up
== 1)
1334 * Update the document's composite and page bounding box...
1337 memcpy(pageinfo
->bounding_box
, bounding_box
,
1338 sizeof(pageinfo
->bounding_box
));
1340 if (bounding_box
[0] < doc
->new_bounding_box
[0])
1341 doc
->new_bounding_box
[0] = bounding_box
[0];
1342 if (bounding_box
[1] < doc
->new_bounding_box
[1])
1343 doc
->new_bounding_box
[1] = bounding_box
[1];
1344 if (bounding_box
[2] > doc
->new_bounding_box
[2])
1345 doc
->new_bounding_box
[2] = bounding_box
[2];
1346 if (bounding_box
[3] > doc
->new_bounding_box
[3])
1347 doc
->new_bounding_box
[3] = bounding_box
[3];
1351 * Output the page header as needed...
1354 if (!doc
->slow_order
&& first_page
)
1356 if (!ppd
|| !ppd
->num_filters
)
1357 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
1358 doc
->slow_collate
? 1 : doc
->copies
);
1360 if (doc
->number_up
> 1)
1362 printf("%%%%Page: (%d) %d\n", doc
->page
, doc
->page
);
1363 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1364 PageLeft
, PageBottom
, PageRight
, PageTop
);
1368 printf("%%%%Page: %s %d\n", pageinfo
->label
, doc
->page
);
1369 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1370 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
1371 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
1376 * Copy any page setup commands...
1381 char *page_setup
; /* PageSetup commands to send */
1384 doc_puts(doc
, "%%BeginPageSetup\n");
1386 if (pageinfo
->num_options
> 0)
1388 int i
; /* Looping var */
1389 ppd_option_t
*option
; /* PPD option */
1390 int min_order
; /* Minimum OrderDependency value */
1391 char *doc_setup
, /* DocumentSetup commands to send */
1392 *any_setup
; /* AnySetup commands to send */
1396 * Yes, figure out the minimum OrderDependency value...
1399 if ((option
= ppdFindOption(ppd
, "PageRegion")) != NULL
)
1400 min_order
= option
->order
;
1404 for (i
= 0; i
< pageinfo
->num_options
; i
++)
1405 if ((option
= ppdFindOption(ppd
, pageinfo
->options
[i
].name
)) != NULL
&&
1406 option
->order
< min_order
)
1407 min_order
= option
->order
;
1410 * Mark and extract them...
1413 cupsMarkOptions(ppd
, pageinfo
->num_options
, pageinfo
->options
);
1415 doc_setup
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, min_order
);
1416 any_setup
= ppdEmitString(ppd
, PPD_ORDER_ANY
, min_order
);
1419 * Then send them out...
1424 doc_puts(doc
, doc_setup
);
1430 doc_puts(doc
, any_setup
);
1436 * Output commands for the current page...
1439 page_setup
= ppdEmitString(ppd
, PPD_ORDER_PAGE
, 0);
1443 doc_puts(doc
, page_setup
);
1449 * Prep for the start of the page description...
1452 start_nup(doc
, number
, 1, bounding_box
);
1455 * Copy page setup commands as needed...
1458 if (!strncmp(line
, "%%BeginPageSetup", 16))
1460 int feature
= 0; /* In a Begin/EndFeature block? */
1463 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1465 if (!strncmp(line
, "%%EndPageSetup", 14))
1467 else if (!strncmp(line
, "%%BeginFeature:", 15))
1471 if (doc
->number_up
> 1 || doc
->fitplot
)
1474 else if (!strncmp(line
, "%%EndFeature:", 13))
1478 if (doc
->number_up
> 1 || doc
->fitplot
)
1481 else if (!strncmp(line
, "%%Include", 9))
1484 if (!feature
|| (doc
->number_up
== 1 && !doc
->fitplot
))
1485 doc_write(doc
, line
, linelen
);
1489 * Skip %%EndPageSetup...
1493 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1497 * Finish the PageSetup section as needed...
1501 doc_puts(doc
, "%%EndPageSetup\n");
1504 * Read the rest of the page description...
1512 (!strncmp(line
, "%%Page:", 7) ||
1513 !strncmp(line
, "%%Trailer", 9) ||
1514 !strncmp(line
, "%%EOF", 5)))
1516 else if (!strncmp(line
, "%%BeginDocument", 15) ||
1517 !strncmp(line
, "%ADO_BeginApplication", 21))
1519 doc_write(doc
, line
, linelen
);
1523 else if ((!strncmp(line
, "%%EndDocument", 13) ||
1524 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
1526 doc_write(doc
, line
, linelen
);
1530 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
1531 (!strncmp(line
, "%%BeginData:", 12) &&
1532 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
1535 * Copy binary data...
1538 int bytes
; /* Bytes of data */
1541 doc_write(doc
, line
, linelen
);
1543 bytes
= atoi(strchr(line
, ':') + 1);
1547 if (bytes
> linesize
)
1548 linelen
= cupsFileRead(fp
, line
, linesize
);
1550 linelen
= cupsFileRead(fp
, line
, bytes
);
1555 perror("ERROR: Early end-of-file while reading binary data");
1559 doc_write(doc
, line
, linelen
);
1565 doc_write(doc
, line
, linelen
);
1567 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0);
1570 * Finish up this page and return...
1573 end_nup(doc
, number
);
1575 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
1582 * 'copy_prolog()' - Copy the document prolog section...
1584 * This function expects "line" to be filled with a %%BeginProlog comment line.
1585 * On return, "line" will contain the next line in the file, if any.
1588 static size_t /* O - Length of next line */
1589 copy_prolog(cups_file_t
*fp
, /* I - File to read from */
1590 pstops_doc_t
*doc
, /* I - Document info */
1591 ppd_file_t
*ppd
, /* I - PPD file */
1592 char *line
, /* I - Line buffer */
1593 size_t linelen
, /* I - Length of initial line */
1594 size_t linesize
) /* I - Size of line buffer */
1596 while (strncmp(line
, "%%BeginProlog", 13))
1598 if (!strncmp(line
, "%%BeginSetup", 12) || !strncmp(line
, "%%Page:", 7))
1601 fwrite(line
, 1, linelen
, stdout
);
1603 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1607 puts("%%BeginProlog");
1609 do_prolog(doc
, ppd
);
1611 if (!strncmp(line
, "%%BeginProlog", 13))
1613 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1615 if (!strncmp(line
, "%%EndProlog", 11) ||
1616 !strncmp(line
, "%%BeginSetup", 12) ||
1617 !strncmp(line
, "%%Page:", 7))
1620 fwrite(line
, 1, linelen
, stdout
);
1623 if (!strncmp(line
, "%%EndProlog", 11))
1624 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1626 fputs("ERROR: Missing %%EndProlog!\n", stderr
);
1629 puts("%%EndProlog");
1636 * 'copy_setup()' - Copy the document setup section...
1638 * This function expects "line" to be filled with a %%BeginSetup comment line.
1639 * On return, "line" will contain the next line in the file, if any.
1642 static size_t /* O - Length of next line */
1643 copy_setup(cups_file_t
*fp
, /* I - File to read from */
1644 pstops_doc_t
*doc
, /* I - Document info */
1645 ppd_file_t
*ppd
, /* I - PPD file */
1646 char *line
, /* I - Line buffer */
1647 size_t linelen
, /* I - Length of initial line */
1648 size_t linesize
) /* I - Size of line buffer */
1650 while (strncmp(line
, "%%BeginSetup", 12))
1652 if (!strncmp(line
, "%%Page:", 7))
1655 fwrite(line
, 1, linelen
, stdout
);
1657 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1661 puts("%%BeginSetup");
1665 if (!strncmp(line
, "%%BeginSetup", 12))
1667 while (strncmp(line
, "%%EndSetup", 10))
1669 if (!strncmp(line
, "%%Page:", 7))
1671 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1674 * %%IncludeFeature: *MainKeyword OptionKeyword
1677 if (doc
->number_up
== 1 && !doc
->fitplot
)
1678 doc
->num_options
= include_feature(ppd
, line
, doc
->num_options
,
1681 else if (strncmp(line
, "%%BeginSetup", 12))
1682 fwrite(line
, 1, linelen
, stdout
);
1684 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1688 if (!strncmp(line
, "%%EndSetup", 10))
1689 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1691 fputs("ERROR: Missing %%EndSetup!\n", stderr
);
1701 * 'copy_trailer()' - Copy the document trailer...
1703 * This function expects "line" to be filled with a %%Trailer comment line.
1704 * On return, "line" will contain the next line in the file, if any.
1707 static size_t /* O - Length of next line */
1708 copy_trailer(cups_file_t
*fp
, /* I - File to read from */
1709 pstops_doc_t
*doc
, /* I - Document info */
1710 ppd_file_t
*ppd
, /* I - PPD file */
1711 int number
, /* I - Number of pages */
1712 char *line
, /* I - Line buffer */
1713 size_t linelen
, /* I - Length of initial line */
1714 size_t linesize
) /* I - Size of line buffer */
1717 * Write the trailer comments...
1724 if (!strncmp(line
, "%%EOF", 5))
1726 else if (strncmp(line
, "%%Trailer", 9) &&
1727 strncmp(line
, "%%Pages:", 8) &&
1728 strncmp(line
, "%%BoundingBox:", 14))
1729 fwrite(line
, 1, linelen
, stdout
);
1731 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1734 fprintf(stderr
, "DEBUG: Wrote %d pages...\n", number
);
1736 printf("%%%%Pages: %d\n", number
);
1737 if (doc
->number_up
> 1 || doc
->fitplot
)
1738 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
1739 PageLeft
, PageBottom
, PageRight
, PageTop
);
1741 printf("%%%%BoundingBox: %d %d %d %d\n",
1742 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
1743 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
1750 * 'do_prolog()' - Send the necessary document prolog commands...
1754 do_prolog(pstops_doc_t
*doc
, /* I - Document information */
1755 ppd_file_t
*ppd
) /* I - PPD file */
1758 * Send the document prolog commands...
1761 if (ppd
&& ppd
->patches
)
1763 puts("%%BeginFeature: *JobPatchFile 1");
1765 puts("%%EndFeature");
1768 ppdEmit(ppd
, stdout
, PPD_ORDER_PROLOG
);
1771 * Define ESPshowpage here so that applications that define their
1772 * own procedure to do a showpage pick it up...
1775 if (doc
->use_ESPshowpage
)
1776 puts("userdict/ESPshowpage/showpage load put\n"
1777 "userdict/showpage{}put");
1783 * 'do_setup()' - Send the necessary document setup commands...
1787 do_setup(pstops_doc_t
*doc
, /* I - Document information */
1788 ppd_file_t
*ppd
) /* I - PPD file */
1791 * Disable CTRL-D so that embedded files don't cause printing
1795 puts("% Disable CTRL-D as an end-of-file marker...");
1796 puts("userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put");
1799 * Mark any options from %%IncludeFeature: comments...
1802 cupsMarkOptions(ppd
, doc
->num_options
, doc
->options
);
1805 * Send all the printer-specific setup commands...
1808 ppdEmit(ppd
, stdout
, PPD_ORDER_DOCUMENT
);
1809 ppdEmit(ppd
, stdout
, PPD_ORDER_ANY
);
1812 * Set the number of copies for the job...
1815 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1817 printf("%%RBIBeginNonPPDFeature: *NumCopies %d\n", doc
->copies
);
1818 printf("%d/languagelevel where{pop languagelevel 2 ge}{false}ifelse\n"
1819 "{1 dict begin/NumCopies exch def currentdict end setpagedevice}\n"
1820 "{userdict/#copies 3 -1 roll put}ifelse\n", doc
->copies
);
1821 puts("%RBIEndNonPPDFeature");
1825 * If we are doing N-up printing, disable setpagedevice...
1828 if (doc
->number_up
> 1)
1829 puts("userdict/setpagedevice{pop}bind put");
1832 * Changes to the transfer function must be made AFTER any
1833 * setpagedevice code...
1836 if (doc
->gamma
!= 1.0f
|| doc
->brightness
!= 1.0f
)
1837 printf("{ neg 1 add dup 0 lt { pop 1 } { %.3f exp neg 1 add } "
1838 "ifelse %.3f mul } bind settransfer\n", doc
->gamma
,
1842 * Make sure we have rectclip and rectstroke procedures of some sort...
1848 * Write the page and label prologs...
1851 if (doc
->number_up
== 2 || doc
->number_up
== 6)
1854 * For 2- and 6-up output, rotate the labels to match the orientation
1858 if (Orientation
& 1)
1859 WriteLabelProlog(doc
->page_label
, PageBottom
,
1860 PageWidth
- PageLength
+ PageTop
, PageLength
);
1862 WriteLabelProlog(doc
->page_label
, PageLeft
, PageRight
, PageLength
);
1865 WriteLabelProlog(doc
->page_label
, PageBottom
, PageTop
, PageWidth
);
1870 * 'doc_printf()' - Send a formatted string to stdout and/or the temp file.
1872 * This function should be used for all page-level output that is affected
1873 * by ordering, collation, etc.
1877 doc_printf(pstops_doc_t
*doc
, /* I - Document information */
1878 const char *format
, /* I - Printf-style format string */
1879 ...) /* I - Additional arguments as needed */
1881 va_list ap
; /* Pointer to arguments */
1882 char buffer
[1024]; /* Output buffer */
1883 size_t bytes
; /* Number of bytes to write */
1886 va_start(ap
, format
);
1887 bytes
= vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
1890 if (bytes
> sizeof(buffer
))
1893 "ERROR: doc_printf overflow (%d bytes) detected, aborting!\n",
1898 doc_write(doc
, buffer
, bytes
);
1903 * 'doc_puts()' - Send a nul-terminated string to stdout and/or the temp file.
1905 * This function should be used for all page-level output that is affected
1906 * by ordering, collation, etc.
1910 doc_puts(pstops_doc_t
*doc
, /* I - Document information */
1911 const char *s
) /* I - String to send */
1913 doc_write(doc
, s
, strlen(s
));
1918 * 'doc_write()' - Send data to stdout and/or the temp file.
1922 doc_write(pstops_doc_t
*doc
, /* I - Document information */
1923 const char *s
, /* I - Data to send */
1924 size_t len
) /* I - Number of bytes to send */
1926 if (!doc
->slow_order
)
1927 fwrite(s
, 1, len
, stdout
);
1930 cupsFileWrite(doc
->temp
, s
, len
);
1935 * 'end_nup()' - End processing for N-up printing...
1939 end_nup(pstops_doc_t
*doc
, /* I - Document information */
1940 int number
) /* I - Page number */
1942 if (doc
->number_up
> 1)
1943 doc_puts(doc
, "userdict/ESPsave get restore\n");
1945 switch (doc
->number_up
)
1948 if (doc
->use_ESPshowpage
)
1950 write_labels(doc
, Orientation
);
1951 doc_puts(doc
, "ESPshowpage\n");
1957 if (is_last_page(number
) && doc
->use_ESPshowpage
)
1959 if (Orientation
& 1)
1962 * Rotate the labels back to portrait...
1965 write_labels(doc
, Orientation
- 1);
1967 else if (Orientation
== 0)
1970 * Rotate the labels to landscape...
1973 write_labels(doc
, doc
->normal_landscape
? 1 : 3);
1978 * Rotate the labels to landscape...
1981 write_labels(doc
, doc
->normal_landscape
? 3 : 1);
1984 doc_puts(doc
, "ESPshowpage\n");
1989 if (is_last_page(number
) && doc
->use_ESPshowpage
)
1991 write_labels(doc
, Orientation
);
1992 doc_puts(doc
, "ESPshowpage\n");
2002 * 'include_feature()' - Include a printer option/feature command.
2005 static int /* O - New number of options */
2007 ppd_file_t
*ppd
, /* I - PPD file */
2008 const char *line
, /* I - DSC line */
2009 int num_options
, /* I - Number of options */
2010 cups_option_t
**options
) /* IO - Options */
2012 char name
[255], /* Option name */
2013 value
[255]; /* Option value */
2014 ppd_option_t
*option
; /* Option in file */
2015 ppd_choice_t
*choice
; /* Choice */
2019 * Get the "%%IncludeFeature: *Keyword OptionKeyword" values...
2022 if (sscanf(line
+ 17, "%254s%254s", name
, value
) != 2)
2024 fputs("ERROR: Bad %%IncludeFeature: comment!\n", stderr
);
2025 return (num_options
);
2029 * Find the option and choice...
2032 if ((option
= ppdFindOption(ppd
, name
+ 1)) == NULL
)
2034 fprintf(stderr
, "WARNING: Unknown option \"%s\"!\n", name
+ 1);
2035 return (num_options
);
2038 if (option
->section
== PPD_ORDER_EXIT
||
2039 option
->section
== PPD_ORDER_JCL
)
2041 fprintf(stderr
, "WARNING: Option \"%s\" cannot be included via "
2042 "IncludeFeature!\n", name
+ 1);
2043 return (num_options
);
2046 if ((choice
= ppdFindChoice(option
, value
)) == NULL
)
2048 fprintf(stderr
, "WARNING: Unknown choice \"%s\" for option \"%s\"!\n",
2050 return (num_options
);
2054 * Add the option to the option array and return...
2057 return (cupsAddOption(name
+ 1, value
, num_options
, options
));
2062 * 'parse_text()' - Parse a text value in a comment...
2064 * This function parses a DSC text value as defined on page 36 of the
2065 * DSC specification. Text values are either surrounded by parenthesis
2066 * or whitespace-delimited.
2068 * The value returned is the literal characters for the entire text
2069 * string, including any parenthesis and escape characters.
2072 static char * /* O - Value or NULL on error */
2073 parse_text(const char *start
, /* I - Start of text value */
2074 char **end
, /* O - End of text value */
2075 char *buffer
, /* I - Buffer */
2076 size_t bufsize
) /* I - Size of buffer */
2078 char *bufptr
, /* Pointer in buffer */
2079 *bufend
; /* End of buffer */
2080 int level
; /* Parenthesis level */
2084 * Skip leading whitespace...
2087 while (isspace(*start
& 255))
2091 * Then copy the value...
2096 bufend
= buffer
+ bufsize
- 1;
2098 while (bufptr
< bufend
)
2100 if (isspace(*start
& 255) && !level
)
2107 else if (*start
== ')')
2117 else if (*start
== '\\')
2120 * Copy escaped character...
2123 int i
; /* Looping var */
2127 i
<= 3 && isdigit(start
[i
] & 255) && bufptr
< bufend
;
2128 *bufptr
++ = start
[i
], i
++);
2137 * Return the value and new pointer into the line...
2141 *end
= (char *)start
;
2143 if (bufptr
== bufend
)
2151 * 'set_pstops_options()' - Set pstops options...
2156 pstops_doc_t
*doc
, /* I - Document information */
2157 ppd_file_t
*ppd
, /* I - PPD file */
2158 char *argv
[], /* I - Command-line arguments */
2159 int num_options
, /* I - Number of options */
2160 cups_option_t
*options
) /* I - Options */
2162 const char *val
; /* Option value */
2163 int intval
; /* Integer option value */
2164 ppd_attr_t
*attr
; /* PPD attribute */
2165 ppd_option_t
*option
; /* PPD option */
2166 ppd_choice_t
*choice
; /* PPD choice */
2170 * Initialize document information structure...
2173 memset(doc
, 0, sizeof(pstops_doc_t
));
2175 doc
->job_id
= atoi(argv
[1]);
2176 doc
->user
= argv
[2];
2177 doc
->title
= argv
[3];
2178 doc
->copies
= atoi(argv
[4]);
2180 if (ppd
&& ppd
->landscape
> 0)
2181 doc
->normal_landscape
= 1;
2183 doc
->bounding_box
[0] = (int)PageLeft
;
2184 doc
->bounding_box
[1] = (int)PageBottom
;
2185 doc
->bounding_box
[2] = (int)PageRight
;
2186 doc
->bounding_box
[3] = (int)PageTop
;
2188 doc
->new_bounding_box
[0] = INT_MAX
;
2189 doc
->new_bounding_box
[1] = INT_MAX
;
2190 doc
->new_bounding_box
[2] = INT_MIN
;
2191 doc
->new_bounding_box
[3] = INT_MIN
;
2194 * AP_FIRSTPAGE_InputSlot
2197 doc
->ap_input_slot
= cupsGetOption("AP_FIRSTPAGE_InputSlot", num_options
,
2201 * AP_FIRSTPAGE_ManualFeed
2204 doc
->ap_manual_feed
= cupsGetOption("AP_FIRSTPAGE_ManualFeed", num_options
,
2211 if ((val
= cupsGetOption("brightness", num_options
, options
)) != NULL
)
2214 * Get brightness value from 10 to 1000.
2219 if (intval
< 10 || intval
> 1000)
2221 fprintf(stderr
, "ERROR: Unsupported brightness value %s, using "
2222 "brightness=100!\n", val
);
2223 doc
->brightness
= 1.0f
;
2226 doc
->brightness
= intval
* 0.01f
;
2229 doc
->brightness
= 1.0f
;
2232 * collate, multiple-document-handling
2235 if ((val
= cupsGetOption("multiple-document-handling", num_options
, options
)) != NULL
)
2238 * This IPP attribute is unnecessarily complicated...
2240 * single-document, separate-documents-collated-copies, and
2241 * single-document-new-sheet all require collated copies.
2243 * separate-documents-uncollated-copies allows for uncollated copies.
2246 doc
->collate
= strcasecmp(val
, "separate-documents-uncollated-copies") != 0;
2249 if ((val
= cupsGetOption("Collate", num_options
, options
)) != NULL
&&
2250 (!strcasecmp(val
, "true") ||!strcasecmp(val
, "on") ||
2251 !strcasecmp(val
, "yes")))
2258 if ((val
= cupsGetOption("emit-jcl", num_options
, options
)) != NULL
&&
2259 (!strcasecmp(val
, "false") || !strcasecmp(val
, "off") ||
2260 !strcasecmp(val
, "no") || !strcmp(val
, "0")))
2269 if ((val
= cupsGetOption("fitplot", num_options
, options
)) != NULL
&&
2270 (!strcasecmp(val
, "true") || !strcasecmp(val
, "on") ||
2271 !strcasecmp(val
, "yes")))
2278 if ((val
= cupsGetOption("gamma", num_options
, options
)) != NULL
)
2281 * Get gamma value from 1 to 10000...
2286 if (intval
< 1 || intval
> 10000)
2288 fprintf(stderr
, "ERROR: Unsupported gamma value %s, using "
2289 "gamma=1000!\n", val
);
2293 doc
->gamma
= intval
* 0.001f
;
2302 if ((choice
= ppdFindMarkedChoice(ppd
, "InputSlot")) != NULL
)
2303 doc
->input_slot
= choice
->choice
;
2309 if ((choice
= ppdFindMarkedChoice(ppd
, "ManualFeed")) != NULL
)
2310 doc
->manual_feed
= choice
->choice
;
2312 if ((val
= cupsGetOption("mirror", num_options
, options
)) != NULL
&&
2313 (!strcasecmp(val
, "true") || !strcasecmp(val
, "on") ||
2314 !strcasecmp(val
, "yes")))
2321 if ((val
= cupsGetOption("number-up", num_options
, options
)) != NULL
)
2323 switch (intval
= atoi(val
))
2331 doc
->number_up
= intval
;
2335 "ERROR: Unsupported number-up value %d, using number-up=1!\n",
2348 if ((val
= cupsGetOption("number-up-layout", num_options
, options
)) != NULL
)
2350 if (!strcasecmp(val
, "lrtb"))
2351 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2352 else if (!strcasecmp(val
, "lrbt"))
2353 doc
->number_up_layout
= PSTOPS_LAYOUT_LRBT
;
2354 else if (!strcasecmp(val
, "rltb"))
2355 doc
->number_up_layout
= PSTOPS_LAYOUT_RLTB
;
2356 else if (!strcasecmp(val
, "rlbt"))
2357 doc
->number_up_layout
= PSTOPS_LAYOUT_RLBT
;
2358 else if (!strcasecmp(val
, "tblr"))
2359 doc
->number_up_layout
= PSTOPS_LAYOUT_TBLR
;
2360 else if (!strcasecmp(val
, "tbrl"))
2361 doc
->number_up_layout
= PSTOPS_LAYOUT_TBRL
;
2362 else if (!strcasecmp(val
, "btlr"))
2363 doc
->number_up_layout
= PSTOPS_LAYOUT_BTLR
;
2364 else if (!strcasecmp(val
, "btrl"))
2365 doc
->number_up_layout
= PSTOPS_LAYOUT_BTRL
;
2368 fprintf(stderr
, "ERROR: Unsupported number-up-layout value %s, using "
2369 "number-up-layout=lrtb!\n", val
);
2370 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2374 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2380 if ((val
= cupsGetOption("OutputOrder", num_options
, options
)) != NULL
)
2382 if (!strcasecmp(val
, "Reverse"))
2383 doc
->output_order
= 1;
2388 * Figure out the right default output order from the PPD file...
2391 if ((choice
= ppdFindMarkedChoice(ppd
, "OutputBin")) != NULL
&&
2392 (attr
= ppdFindAttr(ppd
, "PageStackOrder", choice
->choice
)) != NULL
&&
2394 doc
->output_order
= !strcasecmp(attr
->value
, "Reverse");
2395 else if ((attr
= ppdFindAttr(ppd
, "DefaultOutputOrder", NULL
)) != NULL
&&
2397 doc
->output_order
= !strcasecmp(attr
->value
, "Reverse");
2404 if ((val
= cupsGetOption("page-border", num_options
, options
)) != NULL
)
2406 if (!strcasecmp(val
, "none"))
2407 doc
->page_border
= PSTOPS_BORDERNONE
;
2408 else if (!strcasecmp(val
, "single"))
2409 doc
->page_border
= PSTOPS_BORDERSINGLE
;
2410 else if (!strcasecmp(val
, "single-thick"))
2411 doc
->page_border
= PSTOPS_BORDERSINGLE2
;
2412 else if (!strcasecmp(val
, "double"))
2413 doc
->page_border
= PSTOPS_BORDERDOUBLE
;
2414 else if (!strcasecmp(val
, "double-thick"))
2415 doc
->page_border
= PSTOPS_BORDERDOUBLE2
;
2418 fprintf(stderr
, "ERROR: Unsupported page-border value %s, using "
2419 "page-border=none!\n", val
);
2420 doc
->page_border
= PSTOPS_BORDERNONE
;
2424 doc
->page_border
= PSTOPS_BORDERNONE
;
2430 doc
->page_label
= cupsGetOption("page-label", num_options
, options
);
2436 doc
->page_ranges
= cupsGetOption("page-ranges", num_options
, options
);
2442 doc
->page_set
= cupsGetOption("page-set", num_options
, options
);
2445 * Now figure out if we have to force collated copies, etc.
2448 if (ppd
&& ppd
->manual_copies
&& Duplex
&& doc
->copies
> 1)
2451 * Force collated copies when printing a duplexed document to
2452 * a non-PS printer that doesn't do hardware copy generation.
2453 * Otherwise the copies will end up on the front/back side of
2461 * See if we have to filter the fast or slow way...
2464 if (doc
->collate
&& doc
->copies
> 1)
2467 * See if we need to manually collate the pages...
2470 doc
->slow_collate
= 1;
2472 if ((choice
= ppdFindMarkedChoice(ppd
, "Collate")) != NULL
&&
2473 !strcasecmp(choice
->choice
, "True"))
2476 * Hardware collate option is selected, see if the option is
2477 * conflicting - if not, collate in hardware. Otherwise,
2478 * turn the hardware collate option off...
2481 if ((option
= ppdFindOption(ppd
, "Collate")) != NULL
&&
2482 !option
->conflicted
)
2483 doc
->slow_collate
= 0;
2485 ppdMarkOption(ppd
, "Collate", "False");
2489 doc
->slow_collate
= 0;
2491 if (!ppdFindOption(ppd
, "OutputOrder") && doc
->output_order
)
2492 doc
->slow_order
= 1;
2494 doc
->slow_order
= 0;
2497 (doc
->slow_collate
|| doc
->slow_order
||
2498 ((attr
= ppdFindAttr(ppd
, "cupsEvenDuplex", NULL
)) != NULL
&&
2499 attr
->value
&& !strcasecmp(attr
->value
, "true"))))
2500 doc
->slow_duplex
= 1;
2502 doc
->slow_duplex
= 0;
2505 * Create a temporary file for page data if we need to filter slowly...
2508 if (doc
->slow_order
|| doc
->slow_collate
)
2510 if ((doc
->temp
= cupsTempFile2(doc
->tempfile
,
2511 sizeof(doc
->tempfile
))) == NULL
)
2513 fprintf(stderr
, "ERROR: Unable to create temporary file: %s\n",
2520 * Figure out if we should use ESPshowpage or not...
2523 if (doc
->page_label
|| getenv("CLASSIFICATION") || doc
->number_up
> 1 ||
2527 * Yes, use ESPshowpage...
2530 doc
->use_ESPshowpage
= 1;
2533 fprintf(stderr
, "DEBUG: slow_collate=%d, slow_duplex=%d, slow_order=%d\n",
2534 doc
->slow_collate
, doc
->slow_duplex
, doc
->slow_order
);
2539 * 'skip_page()' - Skip past a page that won't be printed...
2542 static size_t /* O - Length of next line */
2543 skip_page(cups_file_t
*fp
, /* I - File to read from */
2544 char *line
, /* I - Line buffer */
2545 size_t linelen
, /* I - Length of initial line */
2546 size_t linesize
) /* I - Size of line buffer */
2548 int level
; /* Embedded document level */
2553 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
2556 (!strncmp(line
, "%%Page:", 7) || !strncmp(line
, "%%Trailer", 9)))
2558 else if (!strncmp(line
, "%%BeginDocument", 15) ||
2559 !strncmp(line
, "%ADO_BeginApplication", 21))
2561 else if ((!strncmp(line
, "%%EndDocument", 13) ||
2562 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
2564 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
2565 (!strncmp(line
, "%%BeginData:", 12) &&
2566 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
2569 * Skip binary data...
2572 int bytes
; /* Bytes of data */
2575 bytes
= atoi(strchr(line
, ':') + 1);
2579 if (bytes
> linesize
)
2580 linelen
= cupsFileRead(fp
, line
, linesize
);
2582 linelen
= cupsFileRead(fp
, line
, bytes
);
2587 perror("ERROR: Early end-of-file while reading binary data");
2601 * 'start_nup()' - Start processing for N-up printing...
2605 start_nup(pstops_doc_t
*doc
, /* I - Document information */
2606 int number
, /* I - Page number */
2607 int show_border
, /* I - Show the border? */
2608 const int *bounding_box
) /* I - BoundingBox value */
2610 int pos
; /* Position on page */
2611 int x
, y
; /* Relative position of subpage */
2612 float w
, l
, /* Width and length of subpage */
2613 tx
, ty
; /* Translation values for subpage */
2614 float pagew
, /* Printable width of page */
2615 pagel
; /* Printable height of page */
2616 int bboxw
, /* BoundingBox width */
2617 bboxl
; /* BoundingBox height */
2620 if (doc
->number_up
> 1)
2621 doc_puts(doc
, "userdict/ESPsave save put\n");
2624 doc_printf(doc
, "%.1f 0.0 translate -1 1 scale\n", PageWidth
);
2626 pos
= (number
- 1) % doc
->number_up
;
2627 pagew
= PageRight
- PageLeft
;
2628 pagel
= PageTop
- PageBottom
;
2632 bboxw
= bounding_box
[2] - bounding_box
[0];
2633 bboxl
= bounding_box
[3] - bounding_box
[1];
2641 fprintf(stderr
, "DEBUG: pagew = %.1f, pagel = %.1f\n", pagew
, pagel
);
2642 fprintf(stderr
, "DEBUG: bboxw = %d, bboxl = %d\n", bboxw
, bboxl
);
2643 fprintf(stderr
, "DEBUG: PageLeft = %.1f, PageRight = %.1f\n",
2644 PageLeft
, PageRight
);
2645 fprintf(stderr
, "DEBUG: PageTop = %.1f, PageBottom = %.1f\n",
2646 PageTop
, PageBottom
);
2647 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
2648 PageWidth
, PageLength
);
2650 switch (Orientation
)
2652 case 1 : /* Landscape */
2653 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", PageLength
);
2655 case 2 : /* Reverse Portrait */
2656 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", PageWidth
,
2659 case 3 : /* Reverse Landscape */
2660 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", PageWidth
);
2664 if (Duplex
&& doc
->number_up
> 1 && ((number
/ doc
->number_up
) & 1))
2665 doc_printf(doc
, "%.1f %.1f translate\n", PageWidth
- PageRight
, PageBottom
);
2666 else if (doc
->number_up
> 1 || doc
->fitplot
)
2667 doc_printf(doc
, "%.1f %.1f translate\n", PageLeft
, PageBottom
);
2669 switch (doc
->number_up
)
2675 l
= w
* bboxl
/ bboxw
;
2680 w
= l
* bboxw
/ bboxl
;
2683 tx
= 0.5 * (pagew
- w
);
2684 ty
= 0.5 * (pagel
- l
);
2686 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n", tx
, ty
,
2687 w
/ bboxw
, l
/ bboxl
);
2697 if (Orientation
& 1)
2701 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2705 l
= w
* bboxl
/ bboxw
;
2707 if (l
> (pagew
* 0.5))
2710 w
= l
* bboxw
/ bboxl
;
2713 tx
= 0.5 * (pagew
* 0.5 - l
);
2714 ty
= 0.5 * (pagel
- w
);
2716 if (doc
->normal_landscape
)
2717 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2719 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2721 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2722 ty
, tx
+ pagew
* 0.5 * x
, w
/ bboxw
, l
/ bboxl
);
2728 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2732 w
= l
* bboxw
/ bboxl
;
2734 if (w
> (pagel
* 0.5))
2737 l
= w
* bboxl
/ bboxw
;
2740 tx
= 0.5 * (pagel
* 0.5 - w
);
2741 ty
= 0.5 * (pagew
- l
);
2743 if (doc
->normal_landscape
)
2744 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2746 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2748 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2749 tx
+ pagel
* 0.5 * x
, ty
, w
/ bboxw
, l
/ bboxl
);
2754 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2765 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2768 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2772 l
= w
* bboxl
/ bboxw
;
2774 if (l
> (pagel
* 0.5))
2777 w
= l
* bboxw
/ bboxl
;
2780 tx
= 0.5 * (pagew
* 0.5 - w
);
2781 ty
= 0.5 * (pagel
* 0.5 - l
);
2783 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2784 tx
+ x
* pagew
* 0.5, ty
+ y
* pagel
* 0.5,
2785 w
/ bboxw
, l
/ bboxl
);
2789 if (Orientation
& 1)
2791 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2796 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2799 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2807 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2810 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2815 l
= w
* bboxl
/ bboxw
;
2817 if (l
> (pagew
* 0.333))
2820 w
= l
* bboxw
/ bboxl
;
2823 tx
= 0.5 * (pagel
- 2 * w
);
2824 ty
= 0.5 * (pagew
- 3 * l
);
2826 if (doc
->normal_landscape
)
2827 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
2829 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
2831 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2832 tx
+ x
* w
, ty
+ y
* l
, l
/ bboxl
, w
/ bboxw
);
2836 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2841 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2844 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2852 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2855 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2860 w
= l
* bboxw
/ bboxl
;
2862 if (w
> (pagel
* 0.333))
2865 l
= w
* bboxl
/ bboxw
;
2868 tx
= 0.5 * (pagel
- 3 * w
);
2869 ty
= 0.5 * (pagew
- 2 * l
);
2871 if (doc
->normal_landscape
)
2872 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
2874 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
2876 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2877 tx
+ w
* x
, ty
+ l
* y
, w
/ bboxw
, l
/ bboxl
);
2883 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2894 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2897 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2901 l
= w
* bboxl
/ bboxw
;
2903 if (l
> (pagel
* 0.333))
2906 w
= l
* bboxw
/ bboxl
;
2909 tx
= 0.5 * (pagew
* 0.333 - w
);
2910 ty
= 0.5 * (pagel
* 0.333 - l
);
2912 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2913 tx
+ x
* pagew
* 0.333, ty
+ y
* pagel
* 0.333,
2914 w
/ bboxw
, l
/ bboxl
);
2918 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2929 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2932 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2936 l
= w
* bboxl
/ bboxw
;
2938 if (l
> (pagel
* 0.25))
2941 w
= l
* bboxw
/ bboxl
;
2944 tx
= 0.5 * (pagew
* 0.25 - w
);
2945 ty
= 0.5 * (pagel
* 0.25 - l
);
2947 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2948 tx
+ x
* pagew
* 0.25, ty
+ y
* pagel
* 0.25,
2949 w
/ bboxw
, l
/ bboxl
);
2954 * Draw borders as necessary...
2957 if (doc
->page_border
&& show_border
)
2959 int rects
; /* Number of border rectangles */
2960 float fscale
, /* Scaling value for points */
2961 margin
; /* Current margin for borders */
2964 rects
= (doc
->page_border
& PSTOPS_BORDERDOUBLE
) ? 2 : 1;
2965 fscale
= PageWidth
/ w
;
2966 margin
= 2.25 * fscale
;
2969 * Set the line width and color...
2972 doc_puts(doc
, "gsave\n");
2973 doc_printf(doc
, "%.3f setlinewidth 0 setgray newpath\n",
2974 (doc
->page_border
& PSTOPS_BORDERTHICK
) ? 0.5 * fscale
:
2978 * Draw border boxes...
2981 for (; rects
> 0; rects
--, margin
+= 2 * fscale
)
2982 if (doc
->number_up
> 1)
2983 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
2984 margin
- 2.25 * fscale
,
2985 margin
- 2.25 * fscale
,
2986 bboxw
+ 4.5 * fscale
- 2 * margin
,
2987 bboxl
+ 4.5 * fscale
- 2 * margin
);
2989 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
2991 PageBottom
+ margin
,
2992 PageRight
- PageLeft
- 2 * margin
,
2993 PageTop
- PageBottom
- 2 * margin
);
2996 * Restore pen settings...
2999 doc_puts(doc
, "grestore\n");
3005 * Clip the page that follows to the bounding box of the page...
3008 doc_printf(doc
, "%d %d translate\n", -bounding_box
[0],
3010 doc_printf(doc
, "%d %d %d %d ESPrc\n", bounding_box
[0], bounding_box
[1],
3013 else if (doc
->number_up
> 1)
3016 * Clip the page that follows to the default page size...
3019 doc_printf(doc
, "0 0 %d %d ESPrc\n", bboxw
, bboxl
);
3025 * 'write_labels()' - Write the actual page labels.
3027 * This function is a copy of the one in common.c since we need to
3028 * use doc_puts/doc_printf instead of puts/printf...
3032 write_labels(pstops_doc_t
*doc
, /* I - Document information */
3033 int orient
) /* I - Orientation of the page */
3035 float width
, /* Width of page */
3036 length
; /* Length of page */
3039 doc_puts(doc
, "gsave\n");
3041 if ((orient
^ Orientation
) & 1)
3049 length
= PageLength
;
3054 case 1 : /* Landscape */
3055 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", length
);
3057 case 2 : /* Reverse Portrait */
3058 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", width
, length
);
3060 case 3 : /* Reverse Landscape */
3061 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", width
);
3065 doc_puts(doc
, "ESPwl\n");
3066 doc_puts(doc
, "grestore\n");
3071 * End of "$Id: pstops.c 6025 2006-10-11 14:59:20Z mike $".