Debian Codegraph @

texk/psutils/psutils-src/psresize.c

raw
1 /* psresize.c
2 * Alter page size of document
3 *
4 * (c) Reuben Thomas 2012-2014
5 * (c) Angus J. C. Duggan 1991-1997
6 * See file LICENSE for details.
7 */
8
9 #include "config.h"
10
11 #include <unistd.h>
12 #include "progname.h"
13 #include "binary-io.h"
14
15 #include "psutil.h"
16 #include "psspec.h"
17
18 const char *syntax = "[-q] [-wWIDTH] [-hHEIGHT] [-pPAPER] [-WWIDTH] [-HHEIGHT] [-PPAPER] [INFILE [OUTFILE]]\n";
19
20 const char *argerr_message = "bad dimension\n";
21
22 int
23 main(int argc, char *argv[])
24 {
25 double scale, rscale; /* page scale */
26 double waste, rwaste; /* amount wasted */
27 double vshift, hshift; /* page centring shifts */
28 int rotate;
29 double inwidth = -1;
30 double inheight = -1;
31 off_t sizeheaders[20]; /* headers to remove */
32 PageSpec *specs;
33 int opt;
34
35 set_program_name (argv[0]);
36
37 if (!paper_size(NULL, &width, &height))
38 die("could not get default paper size");
39
40 vshift = hshift = 0;
41 rotate = 0;
42
43 verbose = 1;
44
45 while((opt = getopt(argc, argv,
46 "qw:h:p:W:H:P:")) != EOF) {
47 switch(opt) {
48
49 case 'q': /* quiet */
50 verbose = 0;
51 break;
52 case 'w': /* page width */
53 width = singledimen(optarg);
54 break;
55 case 'h': /* page height */
56 height = singledimen(optarg);
57 break;
58 case 'p': /* paper type */
59 if (!paper_size(optarg, &width, &height))
60 die("paper size '%s' not recognised", optarg);
61 break;
62 case 'W': /* input page width */
63 inwidth = singledimen(optarg);
64 break;
65 case 'H': /* input page height */
66 inheight = singledimen(optarg);
67 break;
68 case 'P': /* input paper type */
69 if (!paper_size(optarg, &inwidth, &inheight))
70 die("paper size '%s' not recognised", optarg);
71 break;
72 case 'v': /* version */
73 default:
74 usage();
75 }
76 }
77
78 infile = stdin;
79 outfile = stdout;
80
81 /* Be defensive */
82 if((argc - optind) < 0 || (argc - optind) > 2) usage();
83
84 if (optind != argc) {
85 /* User specified an input file */
86 if ((infile = fopen(argv[optind], "rb")) == NULL)
87 die("can't open input file %s", argv[optind]);
88 optind++;
89 }
90
91 if (optind != argc) {
92 /* User specified an output file */
93 if ((outfile = fopen(argv[optind], "wb")) == NULL)
94 die("can't open output file %s", argv[optind]);
95 optind++;
96 }
97
98 if (optind != argc) usage();
99
100 if (infile == stdin && set_binary_mode(fileno(stdin), O_BINARY) < 0)
101 die("can't reset stdin to binary mode");
102 if (outfile == stdout && set_binary_mode(fileno(stdout), O_BINARY) < 0)
103 die("can't reset stdout to binary mode");
104
105 if ((infile=seekable(infile))==NULL)
106 die("can't seek input");
107
108 if (width <= 0 || height <= 0)
109 die("output page width and height must be set");
110
111 scanpages(sizeheaders);
112
113 if (inwidth <= 0 || inheight <= 0)
114 die("input page width and height must be set");
115
116 /* try normal orientation first */
117 scale = MIN(width/inwidth, height/inheight);
118 waste = (width-scale*inwidth)*(width-scale*inwidth) +
119 (height-scale*inheight)*(height-scale*inheight);
120 hshift = (width - inwidth*scale)/2;
121 vshift = (height - inheight*scale)/2;
122
123 /* try rotated orientation */
124 rscale = MIN(height/inwidth, width/inheight);
125 rwaste = (height-scale*inwidth)*(height-scale*inwidth) +
126 (width-scale*inheight)*(width-scale*inheight);
127 if (rwaste < waste) {
128 double tmp = width;
129 scale = rscale;
130 hshift = (width + inheight*scale)/2;
131 vshift = (height - inwidth*scale)/2;
132 rotate = 1;
133 width = height;
134 height = tmp;
135 }
136
137 width /= scale;
138 height /= scale;
139
140 /* now construct specification list and run page rearrangement procedure */
141 specs = newspec();
142
143 if (rotate) {
144 specs->rotate = 90;
145 specs->flags |= ROTATE;
146 }
147 specs->pageno = 0;
148 specs->scale = scale;
149 specs->flags |= SCALE;
150 specs->xoff = hshift;
151 specs->yoff = vshift;
152 specs->flags |= OFFSET;
153
154 pstops_write(1, 1, 0, specs, 0.0, sizeheaders); /* do page rearrangement */
155
156 return 0;
157 }
158