]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/pstops.c
2 * "$Id: pstops.c,v 1.36 2000/03/21 04:03:28 mike Exp $"
4 * PostScript filter for the Common UNIX Printing System (CUPS).
6 * Copyright 1993-2000 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-3111 USA
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * main() - Main entry...
27 * check_range() - Check to see if the current page is selected for
28 * copy_bytes() - Copy bytes from the input file to stdout...
29 * end_nup() - End processing for N-up printing...
30 * psgets() - Get a line from a file.
31 * start_nup() - Start processing for N-up printing...
35 * Include necessary headers...
45 #define MAX_PAGES 10000
52 int NumPages
= 0; /* Number of pages in file */
53 long Pages
[MAX_PAGES
]; /* Offsets to each page */
54 char PageLabels
[MAX_PAGES
][64];
56 const char *PageRanges
= NULL
; /* Range of pages selected */
57 const char *PageSet
= NULL
; /* All, Even, Odd pages */
58 int Order
= 0, /* 0 = normal, 1 = reverse pages */
59 Flip
= 0, /* Flip/mirror pages */
60 NUp
= 1, /* Number of pages on each sheet (1, 2, 4) */
61 Collate
= 0, /* Collate copies? */
62 Copies
= 1; /* Number of copies */
69 static int check_range(int page
);
70 static void copy_bytes(FILE *fp
, size_t length
);
71 static void end_nup(int number
);
72 static char *psgets(char *buf
, size_t len
, FILE *fp
);
73 static void start_nup(int number
);
77 * 'main()' - Main entry...
80 int /* O - Exit status */
81 main(int argc
, /* I - Number of command-line arguments */
82 char *argv
[]) /* I - Command-line arguments */
84 FILE *fp
; /* Print file */
85 ppd_file_t
*ppd
; /* PPD file */
86 int num_options
; /* Number of print options */
87 cups_option_t
*options
; /* Print options */
88 const char *val
; /* Option value */
89 char tempfile
[255]; /* Temporary file name */
90 FILE *temp
; /* Temporary file */
91 int number
; /* Page number */
92 int slowcollate
; /* 1 if we need to collate manually */
93 int sloworder
; /* 1 if we need to order manually */
94 char line
[8192]; /* Line buffer */
95 float g
; /* Gamma correction value */
96 float b
; /* Brightness factor */
97 int level
; /* Nesting level for embedded files */
98 int nbytes
, /* Number of bytes read */
99 tbytes
; /* Total bytes to read for binary data */
100 int page
; /* Current page sequence number */
101 int page_count
; /* Page count for NUp */
102 int subpage
; /* Sub-page number */
103 int copy
; /* Current copy */
106 if (argc
< 6 || argc
> 7)
108 fputs("ERROR: pstops job-id user title copies options [file]\n", stderr
);
113 * If we have 7 arguments, print the file named on the command-line.
114 * Otherwise, send stdin instead...
122 * Try to open the print file...
125 if ((fp
= fopen(argv
[6], "rb")) == NULL
)
127 perror("ERROR: unable to open print file - ");
133 * Process command-line options and write the prolog...
139 Copies
= atoi(argv
[4]);
142 num_options
= cupsParseOptions(argv
[5], 0, &options
);
144 ppd
= SetCommonOptions(num_options
, options
, 1);
146 if ((val
= cupsGetOption("page-ranges", num_options
, options
)) != NULL
)
149 if ((val
= cupsGetOption("page-set", num_options
, options
)) != NULL
)
152 if ((val
= cupsGetOption("multiple-document-handling", num_options
, options
)) != NULL
)
155 * This IPP attribute is unnecessarily complicated...
157 * single-document, separate-documents-collated-copies, and
158 * single-document-new-sheet all require collated copies.
160 * separate-documents-collated-copies allows for uncollated copies.
163 Collate
= strcasecmp(val
, "separate-documents-collated-copies") != 0;
166 if ((val
= cupsGetOption("Collate", num_options
, options
)) != NULL
&&
167 strcasecmp(val
, "True") == 0)
170 if ((val
= cupsGetOption("OutputOrder", num_options
, options
)) != NULL
&&
171 strcasecmp(val
, "Reverse") == 0)
174 if ((val
= cupsGetOption("number-up", num_options
, options
)) != NULL
)
177 if ((val
= cupsGetOption("gamma", num_options
, options
)) != NULL
)
178 g
= atoi(val
) * 0.001f
;
180 if ((val
= cupsGetOption("brightness", num_options
, options
)) != NULL
)
181 b
= atoi(val
) * 0.01f
;
184 * See if we have to filter the fast or slow way...
187 if (ppdFindOption(ppd
, "Collate") == NULL
&& Collate
&& Copies
> 1)
192 if (ppdFindOption(ppd
, "OutputOrder") == NULL
&& Order
)
198 * If we need to filter slowly, then create a temporary file for page data...
200 * If the temp file can't be created, then we'll ignore the collating/output
204 if (sloworder
|| slowcollate
)
206 temp
= fopen(cupsTempFile(tempfile
, sizeof(tempfile
)), "wb+");
209 slowcollate
= sloworder
= 0;
215 * Write any "exit server" options that have been selected...
218 ppdEmit(ppd
, stdout
, PPD_ORDER_EXIT
);
221 * Write any JCL commands that are needed to print PostScript code...
224 if (ppd
!= NULL
&& ppd
->jcl_begin
&& ppd
->jcl_ps
)
226 fputs(ppd
->jcl_begin
, stdout
);
227 ppdEmit(ppd
, stdout
, PPD_ORDER_JCL
);
228 fputs(ppd
->jcl_ps
, stdout
);
232 * Read the first line to see if we have DSC comments...
235 if (psgets(line
, sizeof(line
), fp
) == NULL
)
237 fputs("ERROR: Empty print file!\n", stderr
);
243 * Start sending the document with any commands needed...
248 if (ppd
!= NULL
&& ppd
->patches
!= NULL
)
251 ppdEmit(ppd
, stdout
, PPD_ORDER_DOCUMENT
);
252 ppdEmit(ppd
, stdout
, PPD_ORDER_ANY
);
253 ppdEmit(ppd
, stdout
, PPD_ORDER_PROLOG
);
256 puts("userdict begin\n"
257 "/ESPshowpage /showpage load def\n"
258 "/showpage { } def\n"
261 if (g
!= 1.0 || b
!= 1.0)
262 printf("{ neg 1 add dup 0 lt { pop 1 } { %.3f exp neg 1 add } "
263 "ifelse %.3f mul } bind settransfer\n", g
, b
);
265 if (Copies
> 1 && (!Collate
|| !slowcollate
))
266 printf("/#copies %d def\n", Copies
);
268 if (strncmp(line
, "%!PS-Adobe-", 11) == 0)
271 * OK, we have DSC comments; read until we find a %%Page comment...
276 while (psgets(line
, sizeof(line
), fp
) != NULL
)
277 if (strncmp(line
, "%%BeginDocument:", 16) == 0 ||
278 strncmp(line
, "%%BeginDocument ", 16) == 0) /* Adobe Acrobat BUG */
280 else if (strcmp(line
, "%%EndDocument") == 0 && level
> 0)
282 else if (strncmp(line
, "%%Page:", 7) == 0 && level
== 0)
284 else if (strncmp(line
, "%%BeginBinary:", 14) == 0 ||
285 (strncmp(line
, "%%BeginData:", 12) == 0 &&
286 strstr(line
, "Binary") != NULL
))
289 * Copy binary data...
292 tbytes
= atoi(strchr(line
, ':') + 1);
295 nbytes
= fread(line
, 1, sizeof(line
), fp
);
296 fwrite(line
, 1, nbytes
, stdout
);
304 * Then read all of the pages, filtering as needed...
309 if (strncmp(line
, "%%BeginDocument:", 16) == 0 ||
310 strncmp(line
, "%%BeginDocument ", 16) == 0) /* Adobe Acrobat BUG */
312 else if (strcmp(line
, "%%EndDocument") == 0 && level
> 0)
314 else if (strncmp(line
, "%%Page:", 7) == 0 && level
== 0)
316 if (sscanf(line
, "%*s%*s%d", &number
) == 1)
318 if (!check_range(number
))
320 while (psgets(line
, sizeof(line
), fp
) != NULL
)
321 if (strncmp(line
, "%%BeginDocument:", 16) == 0 ||
322 strncmp(line
, "%%BeginDocument ", 16) == 0) /* Adobe Acrobat BUG */
324 else if (strcmp(line
, "%%EndDocument") == 0 && level
> 0)
326 else if (strncmp(line
, "%%Page:", 7) == 0 && level
== 0)
332 if (!sloworder
&& NumPages
> 0)
333 end_nup(NumPages
- 1);
335 if (slowcollate
|| sloworder
)
336 Pages
[NumPages
] = ftell(temp
);
340 if ((NumPages
& (NUp
- 1)) == 0)
342 if (ppd
== NULL
|| ppd
->num_filters
== 0)
343 fprintf(stderr
, "PAGE: %d %d\n", page
, Copies
);
345 printf("%%%%Page: %d %d\n", page
, page
);
347 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
356 else if (strncmp(line
, "%%BeginBinary:", 14) == 0 ||
357 (strncmp(line
, "%%BeginData:", 12) == 0 &&
358 strstr(line
, "Binary") != NULL
))
361 * Copy binary data...
364 tbytes
= atoi(strchr(line
, ':') + 1);
367 nbytes
= fread(line
, 1, sizeof(line
), fp
);
370 fwrite(line
, 1, nbytes
, stdout
);
372 if (slowcollate
|| sloworder
)
373 fwrite(line
, 1, nbytes
, stdout
);
378 else if (strcmp(line
, "%%Trailer") == 0 && level
== 0)
385 if (slowcollate
|| sloworder
)
392 if (psgets(line
, sizeof(line
), fp
) == NULL
)
398 end_nup(NumPages
- 1);
400 if (NumPages
& (NUp
- 1))
407 if (slowcollate
|| sloworder
)
409 Pages
[NumPages
] = ftell(temp
);
418 for (number
= 0; number
< NumPages
; number
++)
420 if ((number
& (NUp
- 1)) == 0)
422 if (ppd
== NULL
|| ppd
->num_filters
== 0)
423 fprintf(stderr
, "PAGE: %d 1\n", page
);
425 printf("%%%%Page: %d %d\n", page
, page
);
427 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
431 copy_bytes(temp
, Pages
[number
+ 1] - Pages
[number
]);
435 if (NumPages
& (NUp
- 1))
446 page_count
= (NumPages
+ NUp
- 1) / NUp
;
451 for (page
= page_count
- 1; page
>= 0; page
--)
453 if (ppd
== NULL
|| ppd
->num_filters
== 0)
454 fprintf(stderr
, "PAGE: %d %d\n", page
+ 1,
455 slowcollate
? 1 : Copies
);
458 printf("%%%%Page: %d %d\n", page
+ 1,
459 page_count
- page
+ copy
* page_count
);
461 printf("%%%%Page: %d %d\n", page
+ 1, page_count
- page
);
463 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
465 for (subpage
= 0, number
= page
* NUp
;
466 subpage
< NUp
&& number
< NumPages
;
467 subpage
++, number
++)
470 fseek(temp
, Pages
[number
], SEEK_SET
);
471 copy_bytes(temp
, Pages
[number
+ 1] - Pages
[number
]);
475 if (number
& (NUp
- 1))
484 while (copy
< Copies
&& slowcollate
);
489 * Copy the trailer, if any...
492 while ((nbytes
= fread(line
, 1, sizeof(line
), fp
)) > 0)
493 fwrite(line
, 1, nbytes
, stdout
);
498 * No DSC comments - write any page commands and then the rest of the file...
501 if (ppd
== NULL
|| ppd
->num_filters
== 0)
502 fprintf(stderr
, "PAGE: 1 %d\n", slowcollate
? 1 : Copies
);
504 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
506 while (psgets(line
, sizeof(line
), fp
) != NULL
)
521 if (ppd
== NULL
|| ppd
->num_filters
== 0)
522 fputs("PAGE: 1 1\n", stderr
);
524 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
532 * End the job with the appropriate JCL command or CTRL-D otherwise.
535 if (ppd
!= NULL
&& ppd
->jcl_end
)
536 fputs(ppd
->jcl_end
, stdout
);
541 * Close files and remove the temporary file if needed...
544 if (slowcollate
|| sloworder
)
560 * 'check_range()' - Check to see if the current page is selected for
564 static int /* O - 1 if selected, 0 otherwise */
565 check_range(int page
) /* I - Page number */
567 const char *range
; /* Pointer into range string */
568 int lower
, upper
; /* Lower and upper page numbers */
574 * See if we only print even or odd pages...
577 if (strcasecmp(PageSet
, "even") == 0 && (page
& 1))
579 if (strcasecmp(PageSet
, "odd") == 0 && !(page
& 1))
583 if (PageRanges
== NULL
)
584 return (1); /* No range, print all pages... */
586 for (range
= PageRanges
; *range
!= '\0';)
592 upper
= strtol(range
, (char **)&range
, 10);
596 lower
= strtol(range
, (char **)&range
, 10);
601 if (!isdigit(*range
))
604 upper
= strtol(range
, (char **)&range
, 10);
610 if (page
>= lower
&& page
<= upper
)
624 * 'copy_bytes()' - Copy bytes from the input file to stdout...
628 copy_bytes(FILE *fp
, /* I - File to read from */
629 size_t length
) /* I - Length of page data */
631 char buffer
[8192]; /* Data buffer */
632 size_t nbytes
, /* Number of bytes read */
633 nleft
; /* Number of bytes left/remaining */
638 while (nleft
> 0 || length
== 0)
640 if (nleft
> sizeof(buffer
))
641 nbytes
= sizeof(buffer
);
645 if ((nbytes
= fread(buffer
, 1, nbytes
, fp
)) < 1)
650 fwrite(buffer
, 1, nbytes
, stdout
);
656 * 'end_nup()' - End processing for N-up printing...
660 end_nup(int number
) /* I - Page number */
662 if (Flip
|| Orientation
|| NUp
> 1)
663 puts("ESPsave restore");
668 if ((number
& 1) == 1)
673 if ((number
& 3) == 3)
681 * 'psgets()' - Get a line from a file.
685 * This function differs from the gets() function in that it
686 * handles any combination of CR, LF, or CR LF to end input
690 static char * /* O - String or NULL if EOF */
691 psgets(char *buf
, /* I - Buffer to read into */
692 size_t len
, /* I - Length of buffer */
693 FILE *fp
) /* I - File to read from */
695 char *bufptr
; /* Pointer into buffer */
696 int ch
; /* Character from file */
703 while ((bufptr
- buf
) < len
)
705 if ((ch
= getc(fp
)) == EOF
)
711 * Got a CR; see if there is a LF as well...
715 if (ch
!= EOF
&& ch
!= 0x0a)
716 ungetc(ch
, fp
); /* Nope, save it for later... */
727 * Nul-terminate the string and return it (or NULL for EOF).
732 if (ch
== EOF
&& bufptr
== buf
)
740 * 'start_nup()' - Start processing for N-up printing...
744 start_nup(int number
) /* I - Page number */
746 int x
, y
; /* Relative position of subpage */
747 float w
, l
, /* Width and length of subpage */
748 tx
, ty
; /* Translation values for subpage */
749 float pw
, pl
; /* Printable width and length of full page */
752 if (Flip
|| Orientation
|| NUp
> 1)
753 puts("/ESPsave save def");
756 printf("%.0f 0 translate -1 1 scale\n", PageWidth
);
758 pw
= PageRight
- PageLeft
;
759 pl
= PageTop
- PageBottom
;
763 case 1 : /* Landscape */
764 printf("%.0f 0 translate 90 rotate\n", PageLength
);
766 case 2 : /* Reverse Portrait */
767 printf("%.0f %.0f translate 180 rotate\n", PageWidth
, PageLength
);
769 case 3 : /* Reverse Landscape */
770 printf("0 %.0f translate -90 rotate\n", PageWidth
);
783 l
= w
* PageLength
/ PageWidth
;
788 w
= l
* PageWidth
/ PageLength
;
797 w
= l
* PageWidth
/ PageLength
;
802 l
= w
* PageLength
/ PageWidth
;
809 if (Duplex
&& (number
& 2))
810 printf("%.0f %.0f translate\n", PageWidth
- PageRight
, PageBottom
);
812 printf("%.0f %.0f translate\n", PageLeft
, PageBottom
);
816 printf("0 %.0f translate -90 rotate\n", pl
);
817 printf("%.0f %.0f translate %.3f %.3f scale\n",
818 ty
, tx
+ l
* x
, w
/ pw
, l
/ pl
);
822 printf("%.0f 0 translate 90 rotate\n", pw
);
823 printf("%.0f %.0f translate %.3f %.3f scale\n",
824 tx
+ w
* x
, ty
, w
/ pw
, l
/ pl
);
832 "closepath clip newpath\n",
833 PageWidth
, PageWidth
, PageLength
, PageLength
);
838 y
= 1 - ((number
& 2) != 0);
841 l
= w
* PageLength
/ PageWidth
;
846 w
= l
* PageWidth
/ PageLength
;
849 if (Duplex
&& (number
& 4))
850 printf("%.0f %.0f translate\n", PageWidth
- PageRight
, PageBottom
);
852 printf("%.0f %.0f translate\n", PageLeft
, PageBottom
);
854 printf("%.0f %.0f translate %.3f %.3f scale\n", x
* w
, y
* l
,
855 w
/ PageWidth
, l
/ PageLength
);
861 "closepath clip newpath\n",
862 PageWidth
, PageWidth
, PageLength
, PageLength
);
869 * End of "$Id: pstops.c,v 1.36 2000/03/21 04:03:28 mike Exp $".