]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/pstops.c
2 * "$Id: pstops.c,v 1.29 1999/11/17 18:27:13 mike Exp $"
4 * PostScript filter for the Common UNIX Printing System (CUPS).
6 * Copyright 1993-1999 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 */
103 if (argc
< 6 || argc
> 7)
105 fputs("ERROR: pstops job-id user title copies options [file]\n", stderr
);
110 * If we have 7 arguments, print the file named on the command-line.
111 * Otherwise, send stdin instead...
119 * Try to open the print file...
122 if ((fp
= fopen(argv
[6], "rb")) == NULL
)
124 perror("ERROR: unable to open print file - ");
130 * Process command-line options and write the prolog...
136 Copies
= atoi(argv
[4]);
138 ppd
= ppdOpenFile(getenv("PPD"));
141 num_options
= cupsParseOptions(argv
[5], 0, &options
);
143 ppd
= SetCommonOptions(num_options
, options
, 1);
145 ppdMarkDefaults(ppd
);
146 cupsMarkOptions(ppd
, num_options
, options
);
148 if ((val
= cupsGetOption("page-ranges", num_options
, options
)) != NULL
)
151 if ((val
= cupsGetOption("page-set", num_options
, options
)) != NULL
)
154 if ((val
= cupsGetOption("multiple-document-handling", num_options
, options
)) != NULL
)
157 * This IPP attribute is unnecessarily complicated...
159 * single-document, separate-documents-collated-copies, and
160 * single-document-new-sheet all require collated copies.
162 * separate-documents-collated-copies allows for uncollated copies.
165 Collate
= strcasecmp(val
, "separate-documents-collated-copies") != 0;
168 if ((val
= cupsGetOption("Collate", num_options
, options
)) != NULL
&&
169 strcasecmp(val
, "True") == 0)
172 if ((val
= cupsGetOption("OutputOrder", num_options
, options
)) != NULL
&&
173 strcasecmp(val
, "Reverse") == 0)
176 if ((val
= cupsGetOption("number-up", num_options
, options
)) != NULL
)
179 if ((val
= cupsGetOption("gamma", num_options
, options
)) != NULL
)
180 g
= atoi(val
) * 0.001f
;
182 if ((val
= cupsGetOption("brightness", num_options
, options
)) != NULL
)
183 b
= atoi(val
) * 0.01f
;
186 * See if we have to filter the fast or slow way...
189 if (ppdFindOption(ppd
, "Collate") == NULL
&& Collate
&& Copies
> 1)
194 if (ppdFindOption(ppd
, "OutputOrder") == NULL
&& Order
)
200 * If we need to filter slowly, then create a temporary file for page data...
202 * If the temp file can't be created, then we'll ignore the collating/output
206 if (sloworder
|| slowcollate
)
208 temp
= fopen(cupsTempFile(tempfile
, sizeof(tempfile
)), "wb+");
211 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
) == 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
);
383 if (slowcollate
|| sloworder
)
390 if (psgets(line
, sizeof(line
), fp
) == NULL
)
396 if (NumPages
& (NUp
- 1))
402 if (slowcollate
|| sloworder
)
404 Pages
[NumPages
] = ftell(temp
);
413 for (number
= 0; number
< NumPages
; number
++)
415 if ((number
% NUp
) == 0)
417 if (ppd
== NULL
|| ppd
->num_filters
== 0)
418 fprintf(stderr
, "PAGE: %d 1\n", page
);
420 printf("%%%%Page: %d %d\n", page
, page
);
422 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
426 copy_bytes(temp
, Pages
[number
+ 1] - Pages
[number
]);
430 if (NumPages
& (NUp
- 1))
442 for (number
= NumPages
- 1; number
>= 0; number
--)
444 if ((number
% NUp
) == 0)
446 if (ppd
== NULL
|| ppd
->num_filters
== 0)
447 fprintf(stderr
, "PAGE: %d %d\n", page
,
448 slowcollate
? 1 : Copies
);
450 printf("%%%%Page: %d %d\n", page
, page
);
452 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
455 start_nup(NumPages
- 1 - number
);
456 fseek(temp
, Pages
[number
], SEEK_SET
);
457 copy_bytes(temp
, Pages
[number
+ 1] - Pages
[number
]);
458 end_nup(NumPages
- 1 - number
);
461 if (NumPages
& (NUp
- 1))
468 while (Copies
> 0 || !slowcollate
);
475 * No DSC comments - write any page commands and then the rest of the file...
478 if (ppd
== NULL
|| ppd
->num_filters
== 0)
479 fprintf(stderr
, "PAGE: 1 %d\n", slowcollate
? 1 : Copies
);
481 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
483 while (psgets(line
, sizeof(line
), fp
) != NULL
)
498 if (ppd
== NULL
|| ppd
->num_filters
== 0)
499 fputs("PAGE: 1 1\n", stderr
);
501 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
509 * End the job with the appropriate JCL command or CTRL-D otherwise.
512 if (ppd
!= NULL
&& ppd
->jcl_end
)
513 fputs(ppd
->jcl_end
, stdout
);
518 * Close files and remove the temporary file if needed...
521 if (slowcollate
|| sloworder
)
537 * 'check_range()' - Check to see if the current page is selected for
541 static int /* O - 1 if selected, 0 otherwise */
542 check_range(int page
) /* I - Page number */
544 const char *range
; /* Pointer into range string */
545 int lower
, upper
; /* Lower and upper page numbers */
551 * See if we only print even or odd pages...
554 if (strcasecmp(PageSet
, "even") == 0 && (page
& 1))
556 if (strcasecmp(PageSet
, "odd") == 0 && !(page
& 1))
560 if (PageRanges
== NULL
)
561 return (1); /* No range, print all pages... */
563 for (range
= PageRanges
; *range
!= '\0';)
569 upper
= strtol(range
, (char **)&range
, 10);
573 lower
= strtol(range
, (char **)&range
, 10);
578 if (!isdigit(*range
))
581 upper
= strtol(range
, (char **)&range
, 10);
587 if (page
>= lower
&& page
<= upper
)
601 * 'copy_bytes()' - Copy bytes from the input file to stdout...
605 copy_bytes(FILE *fp
, /* I - File to read from */
606 size_t length
) /* I - Length of page data */
608 char buffer
[8192]; /* Data buffer */
609 size_t nbytes
, /* Number of bytes read */
610 nleft
; /* Number of bytes left/remaining */
615 while (nleft
> 0 || length
== 0)
617 if ((nbytes
= fread(buffer
, 1, sizeof(buffer
), fp
)) < 1)
622 fwrite(buffer
, 1, nbytes
, stdout
);
628 * 'end_nup()' - End processing for N-up printing...
632 end_nup(int number
) /* I - Page number */
634 if (Flip
|| Orientation
|| NUp
> 1)
635 puts("ESPsave restore");
640 if ((number
& 1) == 1)
645 if ((number
& 3) == 3)
653 * 'psgets()' - Get a line from a file.
657 * This function differs from the gets() function in that it
658 * handles any combination of CR, LF, or CR LF to end input
662 static char * /* O - String or NULL if EOF */
663 psgets(char *buf
, /* I - Buffer to read into */
664 size_t len
, /* I - Length of buffer */
665 FILE *fp
) /* I - File to read from */
667 char *bufptr
; /* Pointer into buffer */
668 int ch
; /* Character from file */
674 while ((bufptr
- buf
) < len
)
676 if ((ch
= getc(fp
)) == EOF
)
682 * Got a CR; see if there is a LF as well...
686 if (ch
!= EOF
&& ch
!= 0x0a)
687 ungetc(ch
, fp
); /* Nope, save it for later... */
698 * Nul-terminate the string and return it (or NULL for EOF).
703 if (ch
== EOF
&& bufptr
== buf
)
711 * 'start_nup()' - Start processing for N-up printing...
715 start_nup(int number
) /* I - Page number */
717 int x
, y
; /* Relative position of subpage */
718 float w
, l
, /* Width and length of subpage */
719 tx
, ty
; /* Translation values for subpage */
720 float pw
, pl
; /* Printable width and length of full page */
723 if (Flip
|| Orientation
|| NUp
> 1)
724 puts("/ESPsave save def");
727 printf("%.0f 0 translate -1 1 scale\n", PageWidth
);
729 pw
= PageRight
- PageLeft
;
730 pl
= PageTop
- PageBottom
;
734 case 1 : /* Landscape */
735 printf("%.0f 0 translate 90 rotate\n", PageLength
);
737 case 2 : /* Reverse Portrait */
738 printf("%.0f %.0f translate 180 rotate\n", PageWidth
, PageLength
);
740 case 3 : /* Reverse Landscape */
741 printf("0 %.0f translate -90 rotate\n", PageWidth
);
754 l
= w
* PageLength
/ PageWidth
;
759 w
= l
* PageWidth
/ PageLength
;
768 w
= l
* PageWidth
/ PageLength
;
773 l
= w
* PageLength
/ PageWidth
;
780 if (Duplex
&& (number
& 2))
781 printf("%.0f %.0f translate\n", PageWidth
- PageRight
, PageBottom
);
783 printf("%.0f %.0f translate\n", PageLeft
, PageBottom
);
787 printf("0 %.0f translate -90 rotate\n", pl
);
788 printf("%.0f %.0f translate %.3f %.3f scale\n",
789 ty
, tx
+ l
* x
, w
/ pw
, l
/ pl
);
793 printf("%.0f 0 translate 90 rotate\n", pw
);
794 printf("%.0f %.0f translate %.3f %.3f scale\n",
795 tx
+ w
* x
, ty
, w
/ pw
, l
/ pl
);
803 "closepath clip newpath\n",
804 PageWidth
, PageWidth
, PageLength
, PageLength
);
809 y
= 1 - ((number
& 2) != 0);
812 l
= w
* PageLength
/ PageWidth
;
817 w
= l
* PageWidth
/ PageLength
;
820 if (Duplex
&& (number
& 4))
821 printf("%.0f %.0f translate\n", PageWidth
- PageRight
, PageBottom
);
823 printf("%.0f %.0f translate\n", PageLeft
, PageBottom
);
825 printf("%.0f %.0f translate %.3f %.3f scale\n", x
* w
, y
* l
,
826 w
/ PageWidth
, l
/ PageLength
);
832 "closepath clip newpath\n",
833 PageWidth
, PageWidth
, PageLength
, PageLength
);
840 * End of "$Id: pstops.c,v 1.29 1999/11/17 18:27:13 mike Exp $".