]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/pstops.c
2 * "$Id: pstops.c,v 1.53 2001/03/14 13:45:33 mike Exp $"
4 * PostScript filter for the Common UNIX Printing System (CUPS).
6 * Copyright 1993-2001 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 tempfd
; /* Temporary file descriptor */
92 int number
; /* Page number */
93 int slowcollate
; /* 1 if we need to collate manually */
94 int sloworder
; /* 1 if we need to order manually */
95 char line
[8192]; /* Line buffer */
96 float g
; /* Gamma correction value */
97 float b
; /* Brightness factor */
98 int level
; /* Nesting level for embedded files */
99 int nbytes
, /* Number of bytes read */
100 tbytes
; /* Total bytes to read for binary data */
101 int page
; /* Current page sequence number */
102 int page_count
; /* Page count for NUp */
103 int subpage
; /* Sub-page number */
104 int copy
; /* Current copy */
105 int saweof
; /* Did we see a %%EOF tag? */
109 * Make sure status messages are not buffered...
112 setbuf(stderr
, NULL
);
115 * Check command-line...
118 if (argc
< 6 || argc
> 7)
120 fputs("ERROR: pstops job-id user title copies options [file]\n", stderr
);
125 * If we have 7 arguments, print the file named on the command-line.
126 * Otherwise, send stdin instead...
134 * Try to open the print file...
137 if ((fp
= fopen(argv
[6], "rb")) == NULL
)
139 perror("ERROR: unable to open print file - ");
145 * Process command-line options and write the prolog...
151 Copies
= atoi(argv
[4]);
154 num_options
= cupsParseOptions(argv
[5], 0, &options
);
156 ppd
= SetCommonOptions(num_options
, options
, 1);
158 if ((val
= cupsGetOption("page-ranges", num_options
, options
)) != NULL
)
161 if ((val
= cupsGetOption("page-set", num_options
, options
)) != NULL
)
164 if ((val
= cupsGetOption("multiple-document-handling", num_options
, options
)) != NULL
)
167 * This IPP attribute is unnecessarily complicated...
169 * single-document, separate-documents-collated-copies, and
170 * single-document-new-sheet all require collated copies.
172 * separate-documents-uncollated-copies allows for uncollated copies.
175 Collate
= strcasecmp(val
, "separate-documents-uncollated-copies") != 0;
178 if ((val
= cupsGetOption("Collate", num_options
, options
)) != NULL
&&
179 strcasecmp(val
, "True") == 0)
182 if ((val
= cupsGetOption("OutputOrder", num_options
, options
)) != NULL
&&
183 strcasecmp(val
, "Reverse") == 0)
186 if ((val
= cupsGetOption("number-up", num_options
, options
)) != NULL
)
189 if ((val
= cupsGetOption("gamma", num_options
, options
)) != NULL
)
190 g
= atoi(val
) * 0.001f
;
192 if ((val
= cupsGetOption("brightness", num_options
, options
)) != NULL
)
193 b
= atoi(val
) * 0.01f
;
196 * See if we have to filter the fast or slow way...
199 if (ppdFindOption(ppd
, "Collate") == NULL
&& Collate
&& Copies
> 1)
204 if (ppdFindOption(ppd
, "OutputOrder") == NULL
&& Order
)
210 * If we need to filter slowly, then create a temporary file for page data...
212 * If the temp file can't be created, then we'll ignore the collating/output
216 if (sloworder
|| slowcollate
)
218 tempfd
= cupsTempFd(tempfile
, sizeof(tempfile
));
219 temp
= fdopen(tempfd
, "wb+");
222 slowcollate
= sloworder
= 0;
228 * Write any "exit server" options that have been selected...
231 ppdEmit(ppd
, stdout
, PPD_ORDER_EXIT
);
234 * Write any JCL commands that are needed to print PostScript code...
237 ppdEmitJCL(ppd
, stdout
, atoi(argv
[1]), argv
[2], argv
[3]);
240 * Read the first line to see if we have DSC comments...
243 if (psgets(line
, sizeof(line
), fp
) == NULL
)
245 fputs("ERROR: Empty print file!\n", stderr
);
251 * Start sending the document with any commands needed...
258 if (ppd
!= NULL
&& ppd
->patches
!= NULL
)
261 ppdEmit(ppd
, stdout
, PPD_ORDER_DOCUMENT
);
262 ppdEmit(ppd
, stdout
, PPD_ORDER_ANY
);
263 ppdEmit(ppd
, stdout
, PPD_ORDER_PROLOG
);
265 puts("userdict begin\n"
266 "/ESPshowpage /showpage load def\n"
267 "/showpage { } def\n"
270 if (g
!= 1.0 || b
!= 1.0)
271 printf("{ neg 1 add dup 0 lt { pop 1 } { %.3f exp neg 1 add } "
272 "ifelse %.3f mul } bind settransfer\n", g
, b
);
274 WriteLabelProlog(cupsGetOption("page-label", num_options
, options
));
276 if (Copies
> 1 && (!Collate
|| !slowcollate
))
278 if (ppd
== NULL
|| ppd
->language_level
== 1)
279 printf("/#copies %d def\n", Copies
);
281 printf("<</NumCopies %d>>setpagedevice\n", Copies
);
284 if (strncmp(line
, "%!PS-Adobe-", 11) == 0)
287 * OK, we have DSC comments; read until we find a %%Page comment...
292 while (psgets(line
, sizeof(line
), fp
) != NULL
)
293 if (strncmp(line
, "%%BeginDocument:", 16) == 0 ||
294 strncmp(line
, "%%BeginDocument ", 16) == 0) /* Adobe Acrobat BUG */
296 else if (strcmp(line
, "%%EndDocument") == 0 && level
> 0)
298 else if (strncmp(line
, "%%Page:", 7) == 0 && level
== 0)
300 else if (strncmp(line
, "%%BeginBinary:", 14) == 0 ||
301 (strncmp(line
, "%%BeginData:", 12) == 0 &&
302 strstr(line
, "Binary") != NULL
))
305 * Copy binary data...
308 tbytes
= atoi(strchr(line
, ':') + 1);
311 if (tbytes
> sizeof(line
))
312 nbytes
= fread(line
, 1, sizeof(line
), fp
);
314 nbytes
= fread(line
, 1, tbytes
, fp
);
316 fwrite(line
, 1, nbytes
, stdout
);
324 * Then read all of the pages, filtering as needed...
329 if (strncmp(line
, "%%BeginDocument:", 16) == 0 ||
330 strncmp(line
, "%%BeginDocument ", 16) == 0) /* Adobe Acrobat BUG */
332 else if (strcmp(line
, "%%EndDocument") == 0 && level
> 0)
334 else if (strcmp(line
, "%%EOF") == 0 && level
== 0)
336 else if (strncmp(line
, "%%Page:", 7) == 0 && level
== 0)
338 if (sscanf(line
, "%*s%*s%d", &number
) == 1)
340 if (!check_range(number
))
342 while (psgets(line
, sizeof(line
), fp
) != NULL
)
343 if (strncmp(line
, "%%BeginDocument:", 16) == 0 ||
344 strncmp(line
, "%%BeginDocument ", 16) == 0) /* Adobe Acrobat BUG */
346 else if (strcmp(line
, "%%EndDocument") == 0 && level
> 0)
348 else if (strncmp(line
, "%%Page:", 7) == 0 && level
== 0)
354 if (!sloworder
&& NumPages
> 0)
355 end_nup(NumPages
- 1);
357 if (slowcollate
|| sloworder
)
358 Pages
[NumPages
] = ftell(temp
);
362 if ((NumPages
& (NUp
- 1)) == 0)
364 if (ppd
== NULL
|| ppd
->num_filters
== 0)
365 fprintf(stderr
, "PAGE: %d %d\n", page
, Copies
);
367 printf("%%%%Page: %d %d\n", page
, page
);
369 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
378 else if (strncmp(line
, "%%BeginBinary:", 14) == 0 ||
379 (strncmp(line
, "%%BeginData:", 12) == 0 &&
380 strstr(line
, "Binary") != NULL
))
383 * Copy binary data...
386 tbytes
= atoi(strchr(line
, ':') + 1);
389 if (tbytes
> sizeof(line
))
390 nbytes
= fread(line
, 1, sizeof(line
), fp
);
392 nbytes
= fread(line
, 1, tbytes
, fp
);
395 fwrite(line
, 1, nbytes
, stdout
);
397 if (slowcollate
|| sloworder
)
398 fwrite(line
, 1, nbytes
, stdout
);
403 else if (strcmp(line
, "%%Trailer") == 0 && level
== 0)
410 if (slowcollate
|| sloworder
)
414 if (psgets(line
, sizeof(line
), fp
) == NULL
)
420 end_nup(NumPages
- 1);
422 if (NumPages
& (NUp
- 1))
429 if (slowcollate
|| sloworder
)
431 Pages
[NumPages
] = ftell(temp
);
440 for (number
= 0; number
< NumPages
; number
++)
442 if ((number
& (NUp
- 1)) == 0)
444 if (ppd
== NULL
|| ppd
->num_filters
== 0)
445 fprintf(stderr
, "PAGE: %d 1\n", page
);
447 printf("%%%%Page: %d %d\n", page
, page
);
449 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
453 copy_bytes(temp
, Pages
[number
+ 1] - Pages
[number
]);
457 if (NumPages
& (NUp
- 1))
468 page_count
= (NumPages
+ NUp
- 1) / NUp
;
473 for (page
= page_count
- 1; page
>= 0; page
--)
475 if (ppd
== NULL
|| ppd
->num_filters
== 0)
476 fprintf(stderr
, "PAGE: %d %d\n", page
+ 1,
477 slowcollate
? 1 : Copies
);
480 printf("%%%%Page: %d %d\n", page
+ 1,
481 page_count
- page
+ copy
* page_count
);
483 printf("%%%%Page: %d %d\n", page
+ 1, page_count
- page
);
485 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
487 for (subpage
= 0, number
= page
* NUp
;
488 subpage
< NUp
&& number
< NumPages
;
489 subpage
++, number
++)
492 fseek(temp
, Pages
[number
], SEEK_SET
);
493 copy_bytes(temp
, Pages
[number
+ 1] - Pages
[number
]);
497 if (number
& (NUp
- 1))
506 while (copy
< Copies
&& slowcollate
);
511 * Copy the trailer, if any...
514 while ((nbytes
= fread(line
, 1, sizeof(line
), fp
)) > 0)
515 fwrite(line
, 1, nbytes
, stdout
);
520 * No DSC comments - write any page commands and then the rest of the file...
523 if (ppd
== NULL
|| ppd
->num_filters
== 0)
524 fprintf(stderr
, "PAGE: 1 %d\n", slowcollate
? 1 : Copies
);
526 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
528 while (psgets(line
, sizeof(line
), fp
) != NULL
)
540 if (ppd
== NULL
|| ppd
->num_filters
== 0)
541 fputs("PAGE: 1 1\n", stderr
);
543 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
552 * Send %%EOF if needed...
559 * End the job with the appropriate JCL command or CTRL-D otherwise.
562 if (ppd
!= NULL
&& ppd
->jcl_end
)
563 fputs(ppd
->jcl_end
, stdout
);
568 * Close files and remove the temporary file if needed...
571 if (slowcollate
|| sloworder
)
587 * 'check_range()' - Check to see if the current page is selected for
591 static int /* O - 1 if selected, 0 otherwise */
592 check_range(int page
) /* I - Page number */
594 const char *range
; /* Pointer into range string */
595 int lower
, upper
; /* Lower and upper page numbers */
601 * See if we only print even or odd pages...
604 if (strcasecmp(PageSet
, "even") == 0 && (page
& 1))
606 if (strcasecmp(PageSet
, "odd") == 0 && !(page
& 1))
610 if (PageRanges
== NULL
)
611 return (1); /* No range, print all pages... */
613 for (range
= PageRanges
; *range
!= '\0';)
619 upper
= strtol(range
, (char **)&range
, 10);
623 lower
= strtol(range
, (char **)&range
, 10);
628 if (!isdigit(*range
))
631 upper
= strtol(range
, (char **)&range
, 10);
637 if (page
>= lower
&& page
<= upper
)
651 * 'copy_bytes()' - Copy bytes from the input file to stdout...
655 copy_bytes(FILE *fp
, /* I - File to read from */
656 size_t length
) /* I - Length of page data */
658 char buffer
[8192]; /* Data buffer */
659 size_t nbytes
, /* Number of bytes read */
660 nleft
; /* Number of bytes left/remaining */
665 while (nleft
> 0 || length
== 0)
667 if (nleft
> sizeof(buffer
))
668 nbytes
= sizeof(buffer
);
672 if ((nbytes
= fread(buffer
, 1, nbytes
, fp
)) < 1)
677 fwrite(buffer
, 1, nbytes
, stdout
);
683 * 'end_nup()' - End processing for N-up printing...
687 end_nup(int number
) /* I - Page number */
689 if (Flip
|| Orientation
|| NUp
> 1)
690 puts("ESPsave restore");
700 if ((number
& 1) == 1)
708 if ((number
& 3) == 3)
719 * 'psgets()' - Get a line from a file.
723 * This function differs from the gets() function in that it
724 * handles any combination of CR, LF, or CR LF to end input
728 static char * /* O - String or NULL if EOF */
729 psgets(char *buf
, /* I - Buffer to read into */
730 size_t len
, /* I - Length of buffer */
731 FILE *fp
) /* I - File to read from */
733 char *bufptr
; /* Pointer into buffer */
734 int ch
; /* Character from file */
741 while ((bufptr
- buf
) < len
)
743 if ((ch
= getc(fp
)) == EOF
)
749 * Got a CR; see if there is a LF as well...
753 if (ch
!= EOF
&& ch
!= 0x0a)
754 ungetc(ch
, fp
); /* Nope, save it for later... */
766 * Add a trailing newline if it is there...
773 * Nul-terminate the string and return it (or NULL for EOF).
778 if (ch
== EOF
&& bufptr
== buf
)
786 * 'start_nup()' - Start processing for N-up printing...
790 start_nup(int number
) /* I - Page number */
792 int x
, y
; /* Relative position of subpage */
793 float w
, l
, /* Width and length of subpage */
794 tx
, ty
; /* Translation values for subpage */
795 float pw
, pl
; /* Printable width and length of full page */
798 if (Flip
|| Orientation
|| NUp
> 1)
799 puts("/ESPsave save def");
802 printf("%.1f 0.0 translate -1 1 scale\n", PageWidth
);
804 pw
= PageRight
- PageLeft
;
805 pl
= PageTop
- PageBottom
;
807 fprintf(stderr
, "DEBUG: pw = %.1f, pl = %.1f\n", pw
, pl
);
808 fprintf(stderr
, "DEBUG: PageLeft = %.1f, PageRight = %.1f\n", PageLeft
, PageRight
);
809 fprintf(stderr
, "DEBUG: PageTop = %.1f, PageBottom = %.1f\n", PageTop
, PageBottom
);
810 fprintf(stderr
, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n", PageWidth
, PageLength
);
814 case 1 : /* Landscape */
815 printf("%.1f 0.0 translate 90 rotate\n", PageLength
);
817 case 2 : /* Reverse Portrait */
818 printf("%.1f %.1f translate 180 rotate\n", PageWidth
, PageLength
);
820 case 3 : /* Reverse Landscape */
821 printf("0.0 %.1f translate -90 rotate\n", PageWidth
);
834 l
= w
* PageLength
/ PageWidth
;
839 w
= l
* PageWidth
/ PageLength
;
848 w
= l
* PageWidth
/ PageLength
;
853 l
= w
* PageLength
/ PageWidth
;
860 if (Duplex
&& (number
& 2))
861 printf("%.1f %.1f translate\n", PageWidth
- PageRight
, PageBottom
);
863 printf("%.1f %.1f translate\n", PageLeft
, PageBottom
);
867 printf("0.0 %.1f translate -90 rotate\n", pl
);
868 printf("%.1f %.1f translate %.3f %.3f scale\n",
869 ty
, tx
+ l
* x
, w
/ PageWidth
, l
/ PageLength
);
873 printf("%.1f 0.0 translate 90 rotate\n", pw
);
874 printf("%.1f %.1f translate %.3f %.3f scale\n",
875 tx
+ w
* x
, ty
, w
/ PageWidth
, l
/ PageLength
);
883 "closepath clip newpath\n",
884 PageWidth
, PageWidth
, PageLength
, PageLength
);
889 y
= 1 - ((number
& 2) != 0);
892 l
= w
* PageLength
/ PageWidth
;
897 w
= l
* PageWidth
/ PageLength
;
900 if (Duplex
&& (number
& 4))
901 printf("%.1f %.1f translate\n", PageWidth
- PageRight
, PageBottom
);
903 printf("%.1f %.1f translate\n", PageLeft
, PageBottom
);
905 printf("%.1f %.1f translate %.3f %.3f scale\n", x
* w
, y
* l
,
906 w
/ PageWidth
, l
/ PageLength
);
912 "closepath clip newpath\n",
913 PageWidth
, PageWidth
, PageLength
, PageLength
);
920 * End of "$Id: pstops.c,v 1.53 2001/03/14 13:45:33 mike Exp $".