]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/pstops.c
2 * "$Id: pstops.c 6094 2006-11-14 16:54:50Z 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
));
1266 else if (doc
->number_up
== 1 && !doc
->fitplot
)
1268 int temp_bbox
[4]; /* Temporary bounding box */
1271 switch (Orientation
)
1273 case 0 : /* Portrait */
1276 case 1 : /* Landscape */
1277 temp_bbox
[0] = PageWidth
- bounding_box
[3];
1278 temp_bbox
[1] = bounding_box
[0];
1279 temp_bbox
[2] = PageWidth
- bounding_box
[1];
1280 temp_bbox
[3] = bounding_box
[2];
1282 memcpy(bounding_box
, temp_bbox
, sizeof(bounding_box
));
1285 case 2 : /* Reverse Portrait */
1286 temp_bbox
[0] = PageWidth
- bounding_box
[0];
1287 temp_bbox
[1] = PageLength
- bounding_box
[1];
1288 temp_bbox
[2] = PageWidth
- bounding_box
[2];
1289 temp_bbox
[3] = PageLength
- bounding_box
[3];
1291 memcpy(bounding_box
, temp_bbox
, sizeof(bounding_box
));
1294 case 3 : /* Reverse Landscape */
1295 temp_bbox
[0] = bounding_box
[1];
1296 temp_bbox
[1] = PageLength
- bounding_box
[2];
1297 temp_bbox
[2] = bounding_box
[3];
1298 temp_bbox
[3] = PageLength
- bounding_box
[0];
1300 memcpy(bounding_box
, temp_bbox
, sizeof(bounding_box
));
1306 else if (!strncmp(line
, "%%PageCustomColors:", 19) ||
1307 !strncmp(line
, "%%PageMedia:", 12) ||
1308 !strncmp(line
, "%%PageOrientation:", 18) ||
1309 !strncmp(line
, "%%PageProcessColors:", 20) ||
1310 !strncmp(line
, "%%PageRequirements:", 18) ||
1311 !strncmp(line
, "%%PageResources:", 16))
1318 else if (!strncmp(line
, "%%PageCustomColors:", 19))
1321 * %%PageCustomColors: ...
1324 else if (!strncmp(line
, "%%PageMedia:", 12))
1330 else if (!strncmp(line
, "%%PageOrientation:", 18))
1333 * %%PageOrientation: ...
1336 else if (!strncmp(line
, "%%PageProcessColors:", 20))
1339 * %%PageProcessColors: ...
1342 else if (!strncmp(line
, "%%PageRequirements:", 18))
1345 * %%PageRequirements: ...
1348 else if (!strncmp(line
, "%%PageResources:", 16))
1351 * %%PageResources: ...
1354 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1357 * %%IncludeFeature: *MainKeyword OptionKeyword
1360 if (doc
->number_up
== 1 &&!doc
->fitplot
)
1361 pageinfo
->num_options
= include_feature(ppd
, line
,
1362 pageinfo
->num_options
,
1363 &(pageinfo
->options
));
1365 else if (strncmp(line
, "%%Include", 9))
1369 if (doc
->number_up
== 1)
1372 * Update the document's composite and page bounding box...
1375 memcpy(pageinfo
->bounding_box
, bounding_box
,
1376 sizeof(pageinfo
->bounding_box
));
1378 if (bounding_box
[0] < doc
->new_bounding_box
[0])
1379 doc
->new_bounding_box
[0] = bounding_box
[0];
1380 if (bounding_box
[1] < doc
->new_bounding_box
[1])
1381 doc
->new_bounding_box
[1] = bounding_box
[1];
1382 if (bounding_box
[2] > doc
->new_bounding_box
[2])
1383 doc
->new_bounding_box
[2] = bounding_box
[2];
1384 if (bounding_box
[3] > doc
->new_bounding_box
[3])
1385 doc
->new_bounding_box
[3] = bounding_box
[3];
1389 * Output the page header as needed...
1392 if (!doc
->slow_order
&& first_page
)
1394 if (!ppd
|| !ppd
->num_filters
)
1395 fprintf(stderr
, "PAGE: %d %d\n", doc
->page
,
1396 doc
->slow_collate
? 1 : doc
->copies
);
1398 if (doc
->number_up
> 1)
1400 printf("%%%%Page: (%d) %d\n", doc
->page
, doc
->page
);
1401 printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
1402 PageLeft
, PageBottom
, PageRight
, PageTop
);
1406 printf("%%%%Page: %s %d\n", pageinfo
->label
, doc
->page
);
1407 printf("%%%%PageBoundingBox: %d %d %d %d\n",
1408 pageinfo
->bounding_box
[0], pageinfo
->bounding_box
[1],
1409 pageinfo
->bounding_box
[2], pageinfo
->bounding_box
[3]);
1414 * Copy any page setup commands...
1419 char *page_setup
; /* PageSetup commands to send */
1422 doc_puts(doc
, "%%BeginPageSetup\n");
1424 if (pageinfo
->num_options
> 0)
1426 int i
; /* Looping var */
1427 ppd_option_t
*option
; /* PPD option */
1428 int min_order
; /* Minimum OrderDependency value */
1429 char *doc_setup
, /* DocumentSetup commands to send */
1430 *any_setup
; /* AnySetup commands to send */
1434 * Yes, figure out the minimum OrderDependency value...
1437 if ((option
= ppdFindOption(ppd
, "PageRegion")) != NULL
)
1438 min_order
= option
->order
;
1442 for (i
= 0; i
< pageinfo
->num_options
; i
++)
1443 if ((option
= ppdFindOption(ppd
, pageinfo
->options
[i
].name
)) != NULL
&&
1444 option
->order
< min_order
)
1445 min_order
= option
->order
;
1448 * Mark and extract them...
1451 cupsMarkOptions(ppd
, pageinfo
->num_options
, pageinfo
->options
);
1453 doc_setup
= ppdEmitString(ppd
, PPD_ORDER_DOCUMENT
, min_order
);
1454 any_setup
= ppdEmitString(ppd
, PPD_ORDER_ANY
, min_order
);
1457 * Then send them out...
1462 doc_puts(doc
, doc_setup
);
1468 doc_puts(doc
, any_setup
);
1474 * Output commands for the current page...
1477 page_setup
= ppdEmitString(ppd
, PPD_ORDER_PAGE
, 0);
1481 doc_puts(doc
, page_setup
);
1487 * Prep for the start of the page description...
1490 start_nup(doc
, number
, 1, bounding_box
);
1493 * Copy page setup commands as needed...
1496 if (!strncmp(line
, "%%BeginPageSetup", 16))
1498 int feature
= 0; /* In a Begin/EndFeature block? */
1501 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1503 if (!strncmp(line
, "%%EndPageSetup", 14))
1505 else if (!strncmp(line
, "%%BeginFeature:", 15))
1509 if (doc
->number_up
> 1 || doc
->fitplot
)
1512 else if (!strncmp(line
, "%%EndFeature:", 13))
1516 if (doc
->number_up
> 1 || doc
->fitplot
)
1519 else if (!strncmp(line
, "%%Include", 9))
1522 if (!feature
|| (doc
->number_up
== 1 && !doc
->fitplot
))
1523 doc_write(doc
, line
, linelen
);
1527 * Skip %%EndPageSetup...
1531 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1535 * Finish the PageSetup section as needed...
1539 doc_puts(doc
, "%%EndPageSetup\n");
1542 * Read the rest of the page description...
1550 (!strncmp(line
, "%%Page:", 7) ||
1551 !strncmp(line
, "%%Trailer", 9) ||
1552 !strncmp(line
, "%%EOF", 5)))
1554 else if (!strncmp(line
, "%%BeginDocument", 15) ||
1555 !strncmp(line
, "%ADO_BeginApplication", 21))
1557 doc_write(doc
, line
, linelen
);
1561 else if ((!strncmp(line
, "%%EndDocument", 13) ||
1562 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
1564 doc_write(doc
, line
, linelen
);
1568 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
1569 (!strncmp(line
, "%%BeginData:", 12) &&
1570 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
1573 * Copy binary data...
1576 int bytes
; /* Bytes of data */
1579 doc_write(doc
, line
, linelen
);
1581 bytes
= atoi(strchr(line
, ':') + 1);
1585 if (bytes
> linesize
)
1586 linelen
= cupsFileRead(fp
, line
, linesize
);
1588 linelen
= cupsFileRead(fp
, line
, bytes
);
1593 perror("ERROR: Early end-of-file while reading binary data");
1597 doc_write(doc
, line
, linelen
);
1603 doc_write(doc
, line
, linelen
);
1605 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0);
1608 * Finish up this page and return...
1611 end_nup(doc
, number
);
1613 pageinfo
->length
= cupsFileTell(doc
->temp
) - pageinfo
->offset
;
1620 * 'copy_prolog()' - Copy the document prolog section...
1622 * This function expects "line" to be filled with a %%BeginProlog comment line.
1623 * On return, "line" will contain the next line in the file, if any.
1626 static size_t /* O - Length of next line */
1627 copy_prolog(cups_file_t
*fp
, /* I - File to read from */
1628 pstops_doc_t
*doc
, /* I - Document info */
1629 ppd_file_t
*ppd
, /* I - PPD file */
1630 char *line
, /* I - Line buffer */
1631 size_t linelen
, /* I - Length of initial line */
1632 size_t linesize
) /* I - Size of line buffer */
1634 while (strncmp(line
, "%%BeginProlog", 13))
1636 if (!strncmp(line
, "%%BeginSetup", 12) || !strncmp(line
, "%%Page:", 7))
1639 fwrite(line
, 1, linelen
, stdout
);
1641 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1645 puts("%%BeginProlog");
1647 do_prolog(doc
, ppd
);
1649 if (!strncmp(line
, "%%BeginProlog", 13))
1651 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
1653 if (!strncmp(line
, "%%EndProlog", 11) ||
1654 !strncmp(line
, "%%BeginSetup", 12) ||
1655 !strncmp(line
, "%%Page:", 7))
1658 fwrite(line
, 1, linelen
, stdout
);
1661 if (!strncmp(line
, "%%EndProlog", 11))
1662 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1664 fputs("ERROR: Missing %%EndProlog!\n", stderr
);
1667 puts("%%EndProlog");
1674 * 'copy_setup()' - Copy the document setup section...
1676 * This function expects "line" to be filled with a %%BeginSetup comment line.
1677 * On return, "line" will contain the next line in the file, if any.
1680 static size_t /* O - Length of next line */
1681 copy_setup(cups_file_t
*fp
, /* I - File to read from */
1682 pstops_doc_t
*doc
, /* I - Document info */
1683 ppd_file_t
*ppd
, /* I - PPD file */
1684 char *line
, /* I - Line buffer */
1685 size_t linelen
, /* I - Length of initial line */
1686 size_t linesize
) /* I - Size of line buffer */
1688 while (strncmp(line
, "%%BeginSetup", 12))
1690 if (!strncmp(line
, "%%Page:", 7))
1693 fwrite(line
, 1, linelen
, stdout
);
1695 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1699 puts("%%BeginSetup");
1703 if (!strncmp(line
, "%%BeginSetup", 12))
1705 while (strncmp(line
, "%%EndSetup", 10))
1707 if (!strncmp(line
, "%%Page:", 7))
1709 else if (!strncmp(line
, "%%IncludeFeature:", 17))
1712 * %%IncludeFeature: *MainKeyword OptionKeyword
1715 if (doc
->number_up
== 1 && !doc
->fitplot
)
1716 doc
->num_options
= include_feature(ppd
, line
, doc
->num_options
,
1719 else if (strncmp(line
, "%%BeginSetup", 12))
1720 fwrite(line
, 1, linelen
, stdout
);
1722 if ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) == 0)
1726 if (!strncmp(line
, "%%EndSetup", 10))
1727 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1729 fputs("ERROR: Missing %%EndSetup!\n", stderr
);
1739 * 'copy_trailer()' - Copy the document trailer...
1741 * This function expects "line" to be filled with a %%Trailer comment line.
1742 * On return, "line" will contain the next line in the file, if any.
1745 static size_t /* O - Length of next line */
1746 copy_trailer(cups_file_t
*fp
, /* I - File to read from */
1747 pstops_doc_t
*doc
, /* I - Document info */
1748 ppd_file_t
*ppd
, /* I - PPD file */
1749 int number
, /* I - Number of pages */
1750 char *line
, /* I - Line buffer */
1751 size_t linelen
, /* I - Length of initial line */
1752 size_t linesize
) /* I - Size of line buffer */
1755 * Write the trailer comments...
1762 if (!strncmp(line
, "%%EOF", 5))
1764 else if (strncmp(line
, "%%Trailer", 9) &&
1765 strncmp(line
, "%%Pages:", 8) &&
1766 strncmp(line
, "%%BoundingBox:", 14))
1767 fwrite(line
, 1, linelen
, stdout
);
1769 linelen
= cupsFileGetLine(fp
, line
, linesize
);
1772 fprintf(stderr
, "DEBUG: Wrote %d pages...\n", number
);
1774 printf("%%%%Pages: %d\n", number
);
1775 if (doc
->number_up
> 1 || doc
->fitplot
)
1776 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
1777 PageLeft
, PageBottom
, PageRight
, PageTop
);
1779 printf("%%%%BoundingBox: %d %d %d %d\n",
1780 doc
->new_bounding_box
[0], doc
->new_bounding_box
[1],
1781 doc
->new_bounding_box
[2], doc
->new_bounding_box
[3]);
1788 * 'do_prolog()' - Send the necessary document prolog commands...
1792 do_prolog(pstops_doc_t
*doc
, /* I - Document information */
1793 ppd_file_t
*ppd
) /* I - PPD file */
1796 * Send the document prolog commands...
1799 if (ppd
&& ppd
->patches
)
1801 puts("%%BeginFeature: *JobPatchFile 1");
1803 puts("%%EndFeature");
1806 ppdEmit(ppd
, stdout
, PPD_ORDER_PROLOG
);
1809 * Define ESPshowpage here so that applications that define their
1810 * own procedure to do a showpage pick it up...
1813 if (doc
->use_ESPshowpage
)
1814 puts("userdict/ESPshowpage/showpage load put\n"
1815 "userdict/showpage{}put");
1821 * 'do_setup()' - Send the necessary document setup commands...
1825 do_setup(pstops_doc_t
*doc
, /* I - Document information */
1826 ppd_file_t
*ppd
) /* I - PPD file */
1829 * Disable CTRL-D so that embedded files don't cause printing
1833 puts("% Disable CTRL-D as an end-of-file marker...");
1834 puts("userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put");
1837 * Mark any options from %%IncludeFeature: comments...
1840 cupsMarkOptions(ppd
, doc
->num_options
, doc
->options
);
1843 * Send all the printer-specific setup commands...
1846 ppdEmit(ppd
, stdout
, PPD_ORDER_DOCUMENT
);
1847 ppdEmit(ppd
, stdout
, PPD_ORDER_ANY
);
1850 * Set the number of copies for the job...
1853 if (doc
->copies
!= 1 && (!doc
->collate
|| !doc
->slow_collate
))
1855 printf("%%RBIBeginNonPPDFeature: *NumCopies %d\n", doc
->copies
);
1856 printf("%d/languagelevel where{pop languagelevel 2 ge}{false}ifelse\n"
1857 "{1 dict begin/NumCopies exch def currentdict end setpagedevice}\n"
1858 "{userdict/#copies 3 -1 roll put}ifelse\n", doc
->copies
);
1859 puts("%RBIEndNonPPDFeature");
1863 * If we are doing N-up printing, disable setpagedevice...
1866 if (doc
->number_up
> 1)
1867 puts("userdict/setpagedevice{pop}bind put");
1870 * Changes to the transfer function must be made AFTER any
1871 * setpagedevice code...
1874 if (doc
->gamma
!= 1.0f
|| doc
->brightness
!= 1.0f
)
1875 printf("{ neg 1 add dup 0 lt { pop 1 } { %.3f exp neg 1 add } "
1876 "ifelse %.3f mul } bind settransfer\n", doc
->gamma
,
1880 * Make sure we have rectclip and rectstroke procedures of some sort...
1886 * Write the page and label prologs...
1889 if (doc
->number_up
== 2 || doc
->number_up
== 6)
1892 * For 2- and 6-up output, rotate the labels to match the orientation
1896 if (Orientation
& 1)
1897 WriteLabelProlog(doc
->page_label
, PageBottom
,
1898 PageWidth
- PageLength
+ PageTop
, PageLength
);
1900 WriteLabelProlog(doc
->page_label
, PageLeft
, PageRight
, PageLength
);
1903 WriteLabelProlog(doc
->page_label
, PageBottom
, PageTop
, PageWidth
);
1908 * 'doc_printf()' - Send a formatted string to stdout and/or the temp file.
1910 * This function should be used for all page-level output that is affected
1911 * by ordering, collation, etc.
1915 doc_printf(pstops_doc_t
*doc
, /* I - Document information */
1916 const char *format
, /* I - Printf-style format string */
1917 ...) /* I - Additional arguments as needed */
1919 va_list ap
; /* Pointer to arguments */
1920 char buffer
[1024]; /* Output buffer */
1921 size_t bytes
; /* Number of bytes to write */
1924 va_start(ap
, format
);
1925 bytes
= vsnprintf(buffer
, sizeof(buffer
), format
, ap
);
1928 if (bytes
> sizeof(buffer
))
1931 "ERROR: doc_printf overflow (%d bytes) detected, aborting!\n",
1936 doc_write(doc
, buffer
, bytes
);
1941 * 'doc_puts()' - Send a nul-terminated string to stdout and/or the temp file.
1943 * This function should be used for all page-level output that is affected
1944 * by ordering, collation, etc.
1948 doc_puts(pstops_doc_t
*doc
, /* I - Document information */
1949 const char *s
) /* I - String to send */
1951 doc_write(doc
, s
, strlen(s
));
1956 * 'doc_write()' - Send data to stdout and/or the temp file.
1960 doc_write(pstops_doc_t
*doc
, /* I - Document information */
1961 const char *s
, /* I - Data to send */
1962 size_t len
) /* I - Number of bytes to send */
1964 if (!doc
->slow_order
)
1965 fwrite(s
, 1, len
, stdout
);
1968 cupsFileWrite(doc
->temp
, s
, len
);
1973 * 'end_nup()' - End processing for N-up printing...
1977 end_nup(pstops_doc_t
*doc
, /* I - Document information */
1978 int number
) /* I - Page number */
1980 if (doc
->number_up
> 1)
1981 doc_puts(doc
, "userdict/ESPsave get restore\n");
1983 switch (doc
->number_up
)
1986 if (doc
->use_ESPshowpage
)
1988 write_labels(doc
, Orientation
);
1989 doc_puts(doc
, "ESPshowpage\n");
1995 if (is_last_page(number
) && doc
->use_ESPshowpage
)
1997 if (Orientation
& 1)
2000 * Rotate the labels back to portrait...
2003 write_labels(doc
, Orientation
- 1);
2005 else if (Orientation
== 0)
2008 * Rotate the labels to landscape...
2011 write_labels(doc
, doc
->normal_landscape
? 1 : 3);
2016 * Rotate the labels to landscape...
2019 write_labels(doc
, doc
->normal_landscape
? 3 : 1);
2022 doc_puts(doc
, "ESPshowpage\n");
2027 if (is_last_page(number
) && doc
->use_ESPshowpage
)
2029 write_labels(doc
, Orientation
);
2030 doc_puts(doc
, "ESPshowpage\n");
2040 * 'include_feature()' - Include a printer option/feature command.
2043 static int /* O - New number of options */
2045 ppd_file_t
*ppd
, /* I - PPD file */
2046 const char *line
, /* I - DSC line */
2047 int num_options
, /* I - Number of options */
2048 cups_option_t
**options
) /* IO - Options */
2050 char name
[255], /* Option name */
2051 value
[255]; /* Option value */
2052 ppd_option_t
*option
; /* Option in file */
2053 ppd_choice_t
*choice
; /* Choice */
2057 * Get the "%%IncludeFeature: *Keyword OptionKeyword" values...
2060 if (sscanf(line
+ 17, "%254s%254s", name
, value
) != 2)
2062 fputs("ERROR: Bad %%IncludeFeature: comment!\n", stderr
);
2063 return (num_options
);
2067 * Find the option and choice...
2070 if ((option
= ppdFindOption(ppd
, name
+ 1)) == NULL
)
2072 fprintf(stderr
, "WARNING: Unknown option \"%s\"!\n", name
+ 1);
2073 return (num_options
);
2076 if (option
->section
== PPD_ORDER_EXIT
||
2077 option
->section
== PPD_ORDER_JCL
)
2079 fprintf(stderr
, "WARNING: Option \"%s\" cannot be included via "
2080 "IncludeFeature!\n", name
+ 1);
2081 return (num_options
);
2084 if ((choice
= ppdFindChoice(option
, value
)) == NULL
)
2086 fprintf(stderr
, "WARNING: Unknown choice \"%s\" for option \"%s\"!\n",
2088 return (num_options
);
2092 * Add the option to the option array and return...
2095 return (cupsAddOption(name
+ 1, value
, num_options
, options
));
2100 * 'parse_text()' - Parse a text value in a comment...
2102 * This function parses a DSC text value as defined on page 36 of the
2103 * DSC specification. Text values are either surrounded by parenthesis
2104 * or whitespace-delimited.
2106 * The value returned is the literal characters for the entire text
2107 * string, including any parenthesis and escape characters.
2110 static char * /* O - Value or NULL on error */
2111 parse_text(const char *start
, /* I - Start of text value */
2112 char **end
, /* O - End of text value */
2113 char *buffer
, /* I - Buffer */
2114 size_t bufsize
) /* I - Size of buffer */
2116 char *bufptr
, /* Pointer in buffer */
2117 *bufend
; /* End of buffer */
2118 int level
; /* Parenthesis level */
2122 * Skip leading whitespace...
2125 while (isspace(*start
& 255))
2129 * Then copy the value...
2134 bufend
= buffer
+ bufsize
- 1;
2136 while (bufptr
< bufend
)
2138 if (isspace(*start
& 255) && !level
)
2145 else if (*start
== ')')
2155 else if (*start
== '\\')
2158 * Copy escaped character...
2161 int i
; /* Looping var */
2165 i
<= 3 && isdigit(start
[i
] & 255) && bufptr
< bufend
;
2166 *bufptr
++ = start
[i
], i
++);
2175 * Return the value and new pointer into the line...
2179 *end
= (char *)start
;
2181 if (bufptr
== bufend
)
2189 * 'set_pstops_options()' - Set pstops options...
2194 pstops_doc_t
*doc
, /* I - Document information */
2195 ppd_file_t
*ppd
, /* I - PPD file */
2196 char *argv
[], /* I - Command-line arguments */
2197 int num_options
, /* I - Number of options */
2198 cups_option_t
*options
) /* I - Options */
2200 const char *val
; /* Option value */
2201 int intval
; /* Integer option value */
2202 ppd_attr_t
*attr
; /* PPD attribute */
2203 ppd_option_t
*option
; /* PPD option */
2204 ppd_choice_t
*choice
; /* PPD choice */
2208 * Initialize document information structure...
2211 memset(doc
, 0, sizeof(pstops_doc_t
));
2213 doc
->job_id
= atoi(argv
[1]);
2214 doc
->user
= argv
[2];
2215 doc
->title
= argv
[3];
2216 doc
->copies
= atoi(argv
[4]);
2218 if (ppd
&& ppd
->landscape
> 0)
2219 doc
->normal_landscape
= 1;
2221 doc
->bounding_box
[0] = (int)PageLeft
;
2222 doc
->bounding_box
[1] = (int)PageBottom
;
2223 doc
->bounding_box
[2] = (int)PageRight
;
2224 doc
->bounding_box
[3] = (int)PageTop
;
2226 doc
->new_bounding_box
[0] = INT_MAX
;
2227 doc
->new_bounding_box
[1] = INT_MAX
;
2228 doc
->new_bounding_box
[2] = INT_MIN
;
2229 doc
->new_bounding_box
[3] = INT_MIN
;
2232 * AP_FIRSTPAGE_InputSlot
2235 doc
->ap_input_slot
= cupsGetOption("AP_FIRSTPAGE_InputSlot", num_options
,
2239 * AP_FIRSTPAGE_ManualFeed
2242 doc
->ap_manual_feed
= cupsGetOption("AP_FIRSTPAGE_ManualFeed", num_options
,
2249 if ((val
= cupsGetOption("brightness", num_options
, options
)) != NULL
)
2252 * Get brightness value from 10 to 1000.
2257 if (intval
< 10 || intval
> 1000)
2259 fprintf(stderr
, "ERROR: Unsupported brightness value %s, using "
2260 "brightness=100!\n", val
);
2261 doc
->brightness
= 1.0f
;
2264 doc
->brightness
= intval
* 0.01f
;
2267 doc
->brightness
= 1.0f
;
2270 * collate, multiple-document-handling
2273 if ((val
= cupsGetOption("multiple-document-handling", num_options
, options
)) != NULL
)
2276 * This IPP attribute is unnecessarily complicated...
2278 * single-document, separate-documents-collated-copies, and
2279 * single-document-new-sheet all require collated copies.
2281 * separate-documents-uncollated-copies allows for uncollated copies.
2284 doc
->collate
= strcasecmp(val
, "separate-documents-uncollated-copies") != 0;
2287 if ((val
= cupsGetOption("Collate", num_options
, options
)) != NULL
&&
2288 (!strcasecmp(val
, "true") ||!strcasecmp(val
, "on") ||
2289 !strcasecmp(val
, "yes")))
2296 if ((val
= cupsGetOption("emit-jcl", num_options
, options
)) != NULL
&&
2297 (!strcasecmp(val
, "false") || !strcasecmp(val
, "off") ||
2298 !strcasecmp(val
, "no") || !strcmp(val
, "0")))
2307 if ((val
= cupsGetOption("fitplot", num_options
, options
)) != NULL
&&
2308 (!strcasecmp(val
, "true") || !strcasecmp(val
, "on") ||
2309 !strcasecmp(val
, "yes")))
2316 if ((val
= cupsGetOption("gamma", num_options
, options
)) != NULL
)
2319 * Get gamma value from 1 to 10000...
2324 if (intval
< 1 || intval
> 10000)
2326 fprintf(stderr
, "ERROR: Unsupported gamma value %s, using "
2327 "gamma=1000!\n", val
);
2331 doc
->gamma
= intval
* 0.001f
;
2340 if ((choice
= ppdFindMarkedChoice(ppd
, "InputSlot")) != NULL
)
2341 doc
->input_slot
= choice
->choice
;
2347 if ((choice
= ppdFindMarkedChoice(ppd
, "ManualFeed")) != NULL
)
2348 doc
->manual_feed
= choice
->choice
;
2350 if ((val
= cupsGetOption("mirror", num_options
, options
)) != NULL
&&
2351 (!strcasecmp(val
, "true") || !strcasecmp(val
, "on") ||
2352 !strcasecmp(val
, "yes")))
2359 if ((val
= cupsGetOption("number-up", num_options
, options
)) != NULL
)
2361 switch (intval
= atoi(val
))
2369 doc
->number_up
= intval
;
2373 "ERROR: Unsupported number-up value %d, using number-up=1!\n",
2386 if ((val
= cupsGetOption("number-up-layout", num_options
, options
)) != NULL
)
2388 if (!strcasecmp(val
, "lrtb"))
2389 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2390 else if (!strcasecmp(val
, "lrbt"))
2391 doc
->number_up_layout
= PSTOPS_LAYOUT_LRBT
;
2392 else if (!strcasecmp(val
, "rltb"))
2393 doc
->number_up_layout
= PSTOPS_LAYOUT_RLTB
;
2394 else if (!strcasecmp(val
, "rlbt"))
2395 doc
->number_up_layout
= PSTOPS_LAYOUT_RLBT
;
2396 else if (!strcasecmp(val
, "tblr"))
2397 doc
->number_up_layout
= PSTOPS_LAYOUT_TBLR
;
2398 else if (!strcasecmp(val
, "tbrl"))
2399 doc
->number_up_layout
= PSTOPS_LAYOUT_TBRL
;
2400 else if (!strcasecmp(val
, "btlr"))
2401 doc
->number_up_layout
= PSTOPS_LAYOUT_BTLR
;
2402 else if (!strcasecmp(val
, "btrl"))
2403 doc
->number_up_layout
= PSTOPS_LAYOUT_BTRL
;
2406 fprintf(stderr
, "ERROR: Unsupported number-up-layout value %s, using "
2407 "number-up-layout=lrtb!\n", val
);
2408 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2412 doc
->number_up_layout
= PSTOPS_LAYOUT_LRTB
;
2418 if ((val
= cupsGetOption("OutputOrder", num_options
, options
)) != NULL
)
2420 if (!strcasecmp(val
, "Reverse"))
2421 doc
->output_order
= 1;
2426 * Figure out the right default output order from the PPD file...
2429 if ((choice
= ppdFindMarkedChoice(ppd
, "OutputBin")) != NULL
&&
2430 (attr
= ppdFindAttr(ppd
, "PageStackOrder", choice
->choice
)) != NULL
&&
2432 doc
->output_order
= !strcasecmp(attr
->value
, "Reverse");
2433 else if ((attr
= ppdFindAttr(ppd
, "DefaultOutputOrder", NULL
)) != NULL
&&
2435 doc
->output_order
= !strcasecmp(attr
->value
, "Reverse");
2442 if ((val
= cupsGetOption("page-border", num_options
, options
)) != NULL
)
2444 if (!strcasecmp(val
, "none"))
2445 doc
->page_border
= PSTOPS_BORDERNONE
;
2446 else if (!strcasecmp(val
, "single"))
2447 doc
->page_border
= PSTOPS_BORDERSINGLE
;
2448 else if (!strcasecmp(val
, "single-thick"))
2449 doc
->page_border
= PSTOPS_BORDERSINGLE2
;
2450 else if (!strcasecmp(val
, "double"))
2451 doc
->page_border
= PSTOPS_BORDERDOUBLE
;
2452 else if (!strcasecmp(val
, "double-thick"))
2453 doc
->page_border
= PSTOPS_BORDERDOUBLE2
;
2456 fprintf(stderr
, "ERROR: Unsupported page-border value %s, using "
2457 "page-border=none!\n", val
);
2458 doc
->page_border
= PSTOPS_BORDERNONE
;
2462 doc
->page_border
= PSTOPS_BORDERNONE
;
2468 doc
->page_label
= cupsGetOption("page-label", num_options
, options
);
2474 doc
->page_ranges
= cupsGetOption("page-ranges", num_options
, options
);
2480 doc
->page_set
= cupsGetOption("page-set", num_options
, options
);
2483 * Now figure out if we have to force collated copies, etc.
2486 if (ppd
&& ppd
->manual_copies
&& Duplex
&& doc
->copies
> 1)
2489 * Force collated copies when printing a duplexed document to
2490 * a non-PS printer that doesn't do hardware copy generation.
2491 * Otherwise the copies will end up on the front/back side of
2499 * See if we have to filter the fast or slow way...
2502 if (doc
->collate
&& doc
->copies
> 1)
2505 * See if we need to manually collate the pages...
2508 doc
->slow_collate
= 1;
2510 if ((choice
= ppdFindMarkedChoice(ppd
, "Collate")) != NULL
&&
2511 !strcasecmp(choice
->choice
, "True"))
2514 * Hardware collate option is selected, see if the option is
2515 * conflicting - if not, collate in hardware. Otherwise,
2516 * turn the hardware collate option off...
2519 if ((option
= ppdFindOption(ppd
, "Collate")) != NULL
&&
2520 !option
->conflicted
)
2521 doc
->slow_collate
= 0;
2523 ppdMarkOption(ppd
, "Collate", "False");
2527 doc
->slow_collate
= 0;
2529 if (!ppdFindOption(ppd
, "OutputOrder") && doc
->output_order
)
2530 doc
->slow_order
= 1;
2532 doc
->slow_order
= 0;
2535 (doc
->slow_collate
|| doc
->slow_order
||
2536 ((attr
= ppdFindAttr(ppd
, "cupsEvenDuplex", NULL
)) != NULL
&&
2537 attr
->value
&& !strcasecmp(attr
->value
, "true"))))
2538 doc
->slow_duplex
= 1;
2540 doc
->slow_duplex
= 0;
2543 * Create a temporary file for page data if we need to filter slowly...
2546 if (doc
->slow_order
|| doc
->slow_collate
)
2548 if ((doc
->temp
= cupsTempFile2(doc
->tempfile
,
2549 sizeof(doc
->tempfile
))) == NULL
)
2551 fprintf(stderr
, "ERROR: Unable to create temporary file: %s\n",
2558 * Figure out if we should use ESPshowpage or not...
2561 if (doc
->page_label
|| getenv("CLASSIFICATION") || doc
->number_up
> 1 ||
2565 * Yes, use ESPshowpage...
2568 doc
->use_ESPshowpage
= 1;
2571 fprintf(stderr
, "DEBUG: slow_collate=%d, slow_duplex=%d, slow_order=%d\n",
2572 doc
->slow_collate
, doc
->slow_duplex
, doc
->slow_order
);
2577 * 'skip_page()' - Skip past a page that won't be printed...
2580 static size_t /* O - Length of next line */
2581 skip_page(cups_file_t
*fp
, /* I - File to read from */
2582 char *line
, /* I - Line buffer */
2583 size_t linelen
, /* I - Length of initial line */
2584 size_t linesize
) /* I - Size of line buffer */
2586 int level
; /* Embedded document level */
2591 while ((linelen
= cupsFileGetLine(fp
, line
, linesize
)) > 0)
2594 (!strncmp(line
, "%%Page:", 7) || !strncmp(line
, "%%Trailer", 9)))
2596 else if (!strncmp(line
, "%%BeginDocument", 15) ||
2597 !strncmp(line
, "%ADO_BeginApplication", 21))
2599 else if ((!strncmp(line
, "%%EndDocument", 13) ||
2600 !strncmp(line
, "%ADO_EndApplication", 19)) && level
> 0)
2602 else if (!strncmp(line
, "%%BeginBinary:", 14) ||
2603 (!strncmp(line
, "%%BeginData:", 12) &&
2604 !strstr(line
, "ASCII") && !strstr(line
, "Hex")))
2607 * Skip binary data...
2610 int bytes
; /* Bytes of data */
2613 bytes
= atoi(strchr(line
, ':') + 1);
2617 if (bytes
> linesize
)
2618 linelen
= cupsFileRead(fp
, line
, linesize
);
2620 linelen
= cupsFileRead(fp
, line
, bytes
);
2625 perror("ERROR: Early end-of-file while reading binary data");
2639 * 'start_nup()' - Start processing for N-up printing...
2643 start_nup(pstops_doc_t
*doc
, /* I - Document information */
2644 int number
, /* I - Page number */
2645 int show_border
, /* I - Show the border? */
2646 const int *bounding_box
) /* I - BoundingBox value */
2648 int pos
; /* Position on page */
2649 int x
, y
; /* Relative position of subpage */
2650 float w
, l
, /* Width and length of subpage */
2651 tx
, ty
; /* Translation values for subpage */
2652 float pagew
, /* Printable width of page */
2653 pagel
; /* Printable height of page */
2654 int bboxw
, /* BoundingBox width */
2655 bboxl
; /* BoundingBox height */
2658 if (doc
->number_up
> 1)
2659 doc_puts(doc
, "userdict/ESPsave save put\n");
2662 doc_printf(doc
, "%.1f 0.0 translate -1 1 scale\n", PageWidth
);
2664 pos
= (number
- 1) % doc
->number_up
;
2665 pagew
= PageRight
- PageLeft
;
2666 pagel
= PageTop
- PageBottom
;
2670 bboxw
= bounding_box
[2] - bounding_box
[0];
2671 bboxl
= bounding_box
[3] - bounding_box
[1];
2679 fprintf(stderr
, "DEBUG: pagew = %.1f, pagel = %.1f\n", pagew
, pagel
);
2680 fprintf(stderr
, "DEBUG: bboxw = %d, bboxl = %d\n", bboxw
, bboxl
);
2681 fprintf(stderr
, "DEBUG: PageLeft = %.1f, PageRight = %.1f\n",
2682 PageLeft
, PageRight
);
2683 fprintf(stderr
, "DEBUG: PageTop = %.1f, PageBottom = %.1f\n",
2684 PageTop
, PageBottom
);
2685 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
2686 PageWidth
, PageLength
);
2688 switch (Orientation
)
2690 case 1 : /* Landscape */
2691 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", PageLength
);
2693 case 2 : /* Reverse Portrait */
2694 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", PageWidth
,
2697 case 3 : /* Reverse Landscape */
2698 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", PageWidth
);
2702 if (Duplex
&& doc
->number_up
> 1 && ((number
/ doc
->number_up
) & 1))
2703 doc_printf(doc
, "%.1f %.1f translate\n", PageWidth
- PageRight
, PageBottom
);
2704 else if (doc
->number_up
> 1 || doc
->fitplot
)
2705 doc_printf(doc
, "%.1f %.1f translate\n", PageLeft
, PageBottom
);
2707 switch (doc
->number_up
)
2713 l
= w
* bboxl
/ bboxw
;
2718 w
= l
* bboxw
/ bboxl
;
2721 tx
= 0.5 * (pagew
- w
);
2722 ty
= 0.5 * (pagel
- l
);
2724 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n", tx
, ty
,
2725 w
/ bboxw
, l
/ bboxl
);
2735 if (Orientation
& 1)
2739 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2743 l
= w
* bboxl
/ bboxw
;
2745 if (l
> (pagew
* 0.5))
2748 w
= l
* bboxw
/ bboxl
;
2751 tx
= 0.5 * (pagew
* 0.5 - l
);
2752 ty
= 0.5 * (pagel
- w
);
2754 if (doc
->normal_landscape
)
2755 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2757 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2759 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2760 ty
, tx
+ pagew
* 0.5 * x
, w
/ bboxw
, l
/ bboxl
);
2766 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2770 w
= l
* bboxw
/ bboxl
;
2772 if (w
> (pagel
* 0.5))
2775 l
= w
* bboxl
/ bboxw
;
2778 tx
= 0.5 * (pagel
* 0.5 - w
);
2779 ty
= 0.5 * (pagew
- l
);
2781 if (doc
->normal_landscape
)
2782 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", pagew
);
2784 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", pagel
);
2786 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2787 tx
+ pagel
* 0.5 * x
, ty
, w
/ bboxw
, l
/ bboxl
);
2792 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2803 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2806 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2810 l
= w
* bboxl
/ bboxw
;
2812 if (l
> (pagel
* 0.5))
2815 w
= l
* bboxw
/ bboxl
;
2818 tx
= 0.5 * (pagew
* 0.5 - w
);
2819 ty
= 0.5 * (pagel
* 0.5 - l
);
2821 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2822 tx
+ x
* pagew
* 0.5, ty
+ y
* pagel
* 0.5,
2823 w
/ bboxw
, l
/ bboxl
);
2827 if (Orientation
& 1)
2829 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2834 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2837 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2845 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2848 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2853 l
= w
* bboxl
/ bboxw
;
2855 if (l
> (pagew
* 0.333))
2858 w
= l
* bboxw
/ bboxl
;
2861 tx
= 0.5 * (pagel
- 2 * w
);
2862 ty
= 0.5 * (pagew
- 3 * l
);
2864 if (doc
->normal_landscape
)
2865 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
2867 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
2869 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2870 tx
+ x
* w
, ty
+ y
* l
, l
/ bboxl
, w
/ bboxw
);
2874 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2879 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2882 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2890 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2893 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2898 w
= l
* bboxw
/ bboxl
;
2900 if (w
> (pagel
* 0.333))
2903 l
= w
* bboxl
/ bboxw
;
2906 tx
= 0.5 * (pagel
- 3 * w
);
2907 ty
= 0.5 * (pagew
- 2 * l
);
2909 if (doc
->normal_landscape
)
2910 doc_printf(doc
, "%.1f 0 translate 90 rotate\n", pagew
);
2912 doc_printf(doc
, "0 %.1f translate -90 rotate\n", pagel
);
2914 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2915 tx
+ w
* x
, ty
+ l
* y
, w
/ bboxw
, l
/ bboxl
);
2921 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2932 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2935 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2939 l
= w
* bboxl
/ bboxw
;
2941 if (l
> (pagel
* 0.333))
2944 w
= l
* bboxw
/ bboxl
;
2947 tx
= 0.5 * (pagew
* 0.333 - w
);
2948 ty
= 0.5 * (pagel
* 0.333 - l
);
2950 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2951 tx
+ x
* pagew
* 0.333, ty
+ y
* pagel
* 0.333,
2952 w
/ bboxw
, l
/ bboxl
);
2956 if (doc
->number_up_layout
& PSTOPS_LAYOUT_VERTICAL
)
2967 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEX
)
2970 if (doc
->number_up_layout
& PSTOPS_LAYOUT_NEGATEY
)
2974 l
= w
* bboxl
/ bboxw
;
2976 if (l
> (pagel
* 0.25))
2979 w
= l
* bboxw
/ bboxl
;
2982 tx
= 0.5 * (pagew
* 0.25 - w
);
2983 ty
= 0.5 * (pagel
* 0.25 - l
);
2985 doc_printf(doc
, "%.1f %.1f translate %.3f %.3f scale\n",
2986 tx
+ x
* pagew
* 0.25, ty
+ y
* pagel
* 0.25,
2987 w
/ bboxw
, l
/ bboxl
);
2992 * Draw borders as necessary...
2995 if (doc
->page_border
&& show_border
)
2997 int rects
; /* Number of border rectangles */
2998 float fscale
, /* Scaling value for points */
2999 margin
; /* Current margin for borders */
3002 rects
= (doc
->page_border
& PSTOPS_BORDERDOUBLE
) ? 2 : 1;
3003 fscale
= PageWidth
/ w
;
3004 margin
= 2.25 * fscale
;
3007 * Set the line width and color...
3010 doc_puts(doc
, "gsave\n");
3011 doc_printf(doc
, "%.3f setlinewidth 0 setgray newpath\n",
3012 (doc
->page_border
& PSTOPS_BORDERTHICK
) ? 0.5 * fscale
:
3016 * Draw border boxes...
3019 for (; rects
> 0; rects
--, margin
+= 2 * fscale
)
3020 if (doc
->number_up
> 1)
3021 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3022 margin
- 2.25 * fscale
,
3023 margin
- 2.25 * fscale
,
3024 bboxw
+ 4.5 * fscale
- 2 * margin
,
3025 bboxl
+ 4.5 * fscale
- 2 * margin
);
3027 doc_printf(doc
, "%.1f %.1f %.1f %.1f ESPrs\n",
3029 PageBottom
+ margin
,
3030 PageRight
- PageLeft
- 2 * margin
,
3031 PageTop
- PageBottom
- 2 * margin
);
3034 * Restore pen settings...
3037 doc_puts(doc
, "grestore\n");
3043 * Clip the page that follows to the bounding box of the page...
3046 doc_printf(doc
, "%d %d translate\n", -bounding_box
[0],
3048 doc_printf(doc
, "%d %d %d %d ESPrc\n", bounding_box
[0], bounding_box
[1],
3051 else if (doc
->number_up
> 1)
3054 * Clip the page that follows to the default page size...
3057 doc_printf(doc
, "0 0 %d %d ESPrc\n", bboxw
, bboxl
);
3063 * 'write_labels()' - Write the actual page labels.
3065 * This function is a copy of the one in common.c since we need to
3066 * use doc_puts/doc_printf instead of puts/printf...
3070 write_labels(pstops_doc_t
*doc
, /* I - Document information */
3071 int orient
) /* I - Orientation of the page */
3073 float width
, /* Width of page */
3074 length
; /* Length of page */
3077 doc_puts(doc
, "gsave\n");
3079 if ((orient
^ Orientation
) & 1)
3087 length
= PageLength
;
3092 case 1 : /* Landscape */
3093 doc_printf(doc
, "%.1f 0.0 translate 90 rotate\n", length
);
3095 case 2 : /* Reverse Portrait */
3096 doc_printf(doc
, "%.1f %.1f translate 180 rotate\n", width
, length
);
3098 case 3 : /* Reverse Landscape */
3099 doc_printf(doc
, "0.0 %.1f translate -90 rotate\n", width
);
3103 doc_puts(doc
, "ESPwl\n");
3104 doc_puts(doc
, "grestore\n");
3109 * End of "$Id: pstops.c 6094 2006-11-14 16:54:50Z mike $".