]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/pstops.c
2 * "$Id: pstops.c,v 1.27 1999/11/03 19:05:31 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
)
395 end_nup((NumPages
+ NUp
- 1) & (NUp
- 1));
397 if (slowcollate
|| sloworder
)
399 Pages
[NumPages
] = ftell(temp
);
408 for (number
= 0; number
< NumPages
; number
++)
410 if ((number
% NUp
) == 0)
412 if (ppd
== NULL
|| ppd
->num_filters
== 0)
413 fprintf(stderr
, "PAGE: %d 1\n", page
);
415 printf("%%%%Page: %d %d\n", page
, page
);
417 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
421 copy_bytes(temp
, Pages
[number
+ 1] - Pages
[number
]);
432 for (number
= NumPages
- 1; number
>= 0; number
--)
434 if ((number
% NUp
) == 0)
436 if (ppd
== NULL
|| ppd
->num_filters
== 0)
437 fprintf(stderr
, "PAGE: %d %d\n", page
,
438 slowcollate
? 1 : Copies
);
440 printf("%%%%Page: %d %d\n", page
, page
);
442 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
445 start_nup(NumPages
- 1 - number
);
446 fseek(temp
, Pages
[number
], SEEK_SET
);
447 copy_bytes(temp
, Pages
[number
+ 1] - Pages
[number
]);
448 end_nup(NumPages
- 1 - number
);
453 while (Copies
> 0 || !slowcollate
);
460 * No DSC comments - write any page commands and then the rest of the file...
463 if (ppd
== NULL
|| ppd
->num_filters
== 0)
464 fprintf(stderr
, "PAGE: 1 %d\n", slowcollate
? 1 : Copies
);
466 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
468 while (psgets(line
, sizeof(line
), fp
) != NULL
)
483 if (ppd
== NULL
|| ppd
->num_filters
== 0)
484 fputs("PAGE: 1 1\n", stderr
);
486 ppdEmit(ppd
, stdout
, PPD_ORDER_PAGE
);
494 * End the job with the appropriate JCL command or CTRL-D otherwise.
497 if (ppd
!= NULL
&& ppd
->jcl_end
)
498 fputs(ppd
->jcl_end
, stdout
);
503 * Close files and remove the temporary file if needed...
506 if (slowcollate
|| sloworder
)
522 * 'check_range()' - Check to see if the current page is selected for
526 static int /* O - 1 if selected, 0 otherwise */
527 check_range(int page
) /* I - Page number */
529 const char *range
; /* Pointer into range string */
530 int lower
, upper
; /* Lower and upper page numbers */
536 * See if we only print even or odd pages...
539 if (strcasecmp(PageSet
, "even") == 0 && (page
& 1))
541 if (strcasecmp(PageSet
, "odd") == 0 && !(page
& 1))
545 if (PageRanges
== NULL
)
546 return (1); /* No range, print all pages... */
548 for (range
= PageRanges
; *range
!= '\0';)
554 upper
= strtol(range
, (char **)&range
, 10);
558 lower
= strtol(range
, (char **)&range
, 10);
563 if (!isdigit(*range
))
566 upper
= strtol(range
, (char **)&range
, 10);
572 if (page
>= lower
&& page
<= upper
)
586 * 'copy_bytes()' - Copy bytes from the input file to stdout...
590 copy_bytes(FILE *fp
, /* I - File to read from */
591 size_t length
) /* I - Length of page data */
593 char buffer
[8192]; /* Data buffer */
594 size_t nbytes
, /* Number of bytes read */
595 nleft
; /* Number of bytes left/remaining */
600 while (nleft
> 0 || length
== 0)
602 if ((nbytes
= fread(buffer
, 1, sizeof(buffer
), fp
)) < 1)
607 fwrite(buffer
, 1, nbytes
, stdout
);
613 * 'end_nup()' - End processing for N-up printing...
617 end_nup(int number
) /* I - Page number */
619 if (Flip
|| Orientation
|| NUp
> 1)
620 puts("ESPsave restore");
625 if ((number
& 1) == 1)
630 if ((number
& 3) == 3)
638 * 'psgets()' - Get a line from a file.
642 * This function differs from the gets() function in that it
643 * handles any combination of CR, LF, or CR LF to end input
647 static char * /* O - String or NULL if EOF */
648 psgets(char *buf
, /* I - Buffer to read into */
649 size_t len
, /* I - Length of buffer */
650 FILE *fp
) /* I - File to read from */
652 char *bufptr
; /* Pointer into buffer */
653 int ch
; /* Character from file */
659 while ((bufptr
- buf
) < len
)
661 if ((ch
= getc(fp
)) == EOF
)
667 * Got a CR; see if there is a LF as well...
671 if (ch
!= EOF
&& ch
!= 0x0a)
672 ungetc(ch
, fp
); /* Nope, save it for later... */
683 * Nul-terminate the string and return it (or NULL for EOF).
688 if (ch
== EOF
&& bufptr
== buf
)
696 * 'start_nup()' - Start processing for N-up printing...
700 start_nup(int number
) /* I - Page number */
702 int x
, y
; /* Relative position of subpage */
703 float w
, l
, /* Width and length of subpage */
704 tx
, ty
; /* Translation values for subpage */
705 float pw
, pl
; /* Printable width and length of full page */
708 if (Flip
|| Orientation
|| NUp
> 1)
709 puts("/ESPsave save def");
712 printf("%.0f 0 translate -1 1 scale\n", PageWidth
);
714 pw
= PageRight
- PageLeft
;
715 pl
= PageTop
- PageBottom
;
719 case 1 : /* Landscape */
720 printf("%.0f 0 translate 90 rotate\n", PageLength
);
722 case 2 : /* Reverse Portrait */
723 printf("%.0f %.0f translate 180 rotate\n", PageWidth
, PageLength
);
725 case 3 : /* Reverse Landscape */
726 printf("0 %.0f translate -90 rotate\n", PageWidth
);
765 if (Duplex
&& (number
& 2))
766 printf("%.0f %.0f translate\n", PageWidth
- PageRight
, PageBottom
);
768 printf("%.0f %.0f translate\n", PageLeft
, PageBottom
);
772 printf("0 %.0f translate -90 rotate\n", pl
);
773 printf("%.0f %.0f translate %.3f %.3f scale\n",
774 ty
, tx
+ l
* x
, w
/ pw
, l
/ pl
);
778 printf("%.0f 0 translate 90 rotate\n", pw
);
779 printf("%.0f %.0f translate %.3f %.3f scale\n",
780 tx
+ w
* x
, ty
, w
/ pw
, l
/ pl
);
788 "closepath clip newpath\n",
794 y
= 1 - ((number
& 2) != 0);
798 if (Duplex
&& (number
& 4))
799 printf("%.0f %.0f translate\n", PageWidth
- PageRight
, PageBottom
);
801 printf("%.0f %.0f translate\n", PageLeft
, PageBottom
);
803 printf("%.0f %.0f translate 0.5 0.5 scale\n", x
* w
, y
* l
);
809 "closepath clip newpath\n",
817 * End of "$Id: pstops.c,v 1.27 1999/11/03 19:05:31 mike Exp $".