2 * Copyright (C) Angus J. C. Duggan 1991-1995
3 * See file LICENSE for details.
5 * alter pagesize of document
8 * psresize [-q] [-w<dim>] [-h<dim>] [-ppaper] [-W<dim>] [-H<dim>]
10 * -w<dim> sets the output paper width
11 * -h<dim> sets the output paper height
12 * -ppaper sets the output paper size (width and height) by name
13 * -W<dim> sets the input paper width
14 * -H<dim> sets the input paper height
15 * -Ppaper sets the input paper size (width and height) by name
28 char pagelabel
[BUFSIZ
] ;
31 static void usage(void)
33 fprintf(stderr
, "%s release %d patchlevel %d\n", program
, RELEASE
, PATCHLEVEL
);
34 fprintf(stderr
, "Copyright (C) Angus J. C. Duggan, 1991-1995. See file LICENSE for details.\n");
35 fprintf(stderr
, "Usage: %s [-q] [-wwidth] [-hheight] [-ppaper] [-Wwidth] [-Hheight] [-Ppaper] [infile [outfile]]\n",
41 static void argerror(void)
43 message(FATAL
, "bad dimension\n");
46 #define MIN(x,y) ((x) > (y) ? (y) : (x))
47 #define MAX(x,y) ((x) > (y) ? (x) : (y))
49 void main(int argc
, char *argv
[])
51 double scale
, rscale
; /* page scale */
52 double waste
, rwaste
; /* amount wasted */
53 double vshift
, hshift
; /* page centring shifts */
61 if ( (paper
= findpaper(PAPER
)) != (Paper
*)0 ) {
62 inwidth
= width
= (double)PaperWidth(paper
);
63 inheight
= height
= (double)PaperHeight(paper
);
73 for (program
= *argv
++; --argc
; argv
++) {
74 if (argv
[0][0] == '-') {
79 case 'w': /* page width */
80 width
= singledimen(*argv
+2, argerror
, usage
);
82 case 'h': /* page height */
83 height
= singledimen(*argv
+2, argerror
, usage
);
85 case 'p': /* paper type */
86 if ( (paper
= findpaper(*argv
+2)) != (Paper
*)0 ) {
87 width
= (double)PaperWidth(paper
);
88 height
= (double)PaperHeight(paper
);
90 message(FATAL
, "paper size '%s' not recognised\n", *argv
+2);
92 case 'W': /* input page width */
93 inwidth
= singledimen(*argv
+2, argerror
, usage
);
95 case 'H': /* input page height */
96 inheight
= singledimen(*argv
+2, argerror
, usage
);
98 case 'P': /* input paper type */
99 if ( (paper
= findpaper(*argv
+2)) != (Paper
*)0 ) {
100 inwidth
= (double)PaperWidth(paper
);
101 inheight
= (double)PaperHeight(paper
);
103 message(FATAL
, "paper size '%s' not recognised\n", *argv
+2);
105 case 'v': /* version */
109 } else if (infile
== stdin
) {
110 if ((infile
= fopen(*argv
, OPEN_READ
)) == NULL
)
111 message(FATAL
, "can't open input file %s\n", *argv
);
112 } else if (outfile
== stdout
) {
113 if ((outfile
= fopen(*argv
, OPEN_WRITE
)) == NULL
)
114 message(FATAL
, "can't open output file %s\n", *argv
);
117 #if defined(MSDOS) || defined(WINNT)
118 if ( infile
== stdin
) {
119 int fd
= fileno(stdin
) ;
120 if ( setmode(fd
, O_BINARY
) < 0 )
121 message(FATAL
, "can't open input file %s\n", argv
[4]);
123 if ( outfile
== stdout
) {
124 int fd
= fileno(stdout
) ;
125 if ( setmode(fd
, O_BINARY
) < 0 )
126 message(FATAL
, "can't reset stdout to binary mode\n");
129 if ((infile
=seekable(infile
))==NULL
)
130 message(FATAL
, "can't seek input\n");
132 if (width
<= 0 || height
<= 0)
133 message(FATAL
, "output page width and height must be set\n");
135 if (inwidth
<= 0 || inheight
<= 0)
136 message(FATAL
, "input page width and height must be set\n");
138 /* try normal orientation first */
139 scale
= MIN(width
/inwidth
, height
/inheight
);
140 waste
= (width
-scale
*inwidth
)*(width
-scale
*inwidth
) +
141 (height
-scale
*inheight
)*(height
-scale
*inheight
);
142 hshift
= (width
- inwidth
*scale
)/2;
143 vshift
= (height
- inheight
*scale
)/2;
145 /* try rotated orientation */
146 rscale
= MIN(height
/inwidth
, width
/inheight
);
147 rwaste
= (height
-scale
*inwidth
)*(height
-scale
*inwidth
) +
148 (width
-scale
*inheight
)*(width
-scale
*inheight
);
149 if (rwaste
< waste
) {
152 hshift
= (width
+ inheight
*scale
)/2;
153 vshift
= (height
- inwidth
*scale
)/2;
162 /* now construct specification list and run page rearrangement procedure */
167 specs
->flags
|= ROTATE
;
170 specs
->scale
= scale
;
171 specs
->flags
|= SCALE
;
172 specs
->xoff
= hshift
;
173 specs
->yoff
= vshift
;
174 specs
->flags
|= OFFSET
;
176 pstops(1, 1, 0, specs
, 0.0); /* do page rearrangement */