| 1 |
/* psnup.c |
| 2 |
* Put multiple pages on to one page |
| 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 <string.h> |
| 13 |
#include "progname.h" |
| 14 |
#include "binary-io.h" |
| 15 |
|
| 16 |
#include "psutil.h" |
| 17 |
#include "psspec.h" |
| 18 |
|
| 19 |
const char *syntax = "[-q] [-wWIDTH] [-hHEIGHT] [-pPAPER] [-WWIDTH] [-HHEIGHT] [-PPAPER] [-l] [-r] [-c] [-f] [-mMARGIN] [-bBORDER] [-dLWIDTH] [-sSCALE] [-NUP] [INFILE [OUTFILE]]\n"; |
| 20 |
|
| 21 |
const char *argerr_message = "bad dimension\n"; |
| 22 |
|
| 23 |
/* return next larger exact divisor of number, or 0 if none. There is probably |
| 24 |
* a much more efficient method of doing this, but the numbers involved are |
| 25 |
* small, so it's not a big loss. */ |
| 26 |
_GL_ATTRIBUTE_CONST static int nextdiv(int n, int m) |
| 27 |
{ |
| 28 |
while (++n <= m) { |
| 29 |
if (m%n == 0) |
| 30 |
return (n); |
| 31 |
} |
| 32 |
return (0); |
| 33 |
} |
| 34 |
|
| 35 |
int |
| 36 |
main(int argc, char *argv[]) |
| 37 |
{ |
| 38 |
int horiz = 0, vert = 0, rotate = 0, column = 0; |
| 39 |
int flip = 0, leftright = 0, topbottom = 0; |
| 40 |
int nup = 1; |
| 41 |
double draw = 0; /* draw page borders */ |
| 42 |
double scale = 1.0; /* page scale */ |
| 43 |
double uscale = 0; /* user supplied scale */ |
| 44 |
double ppwid, pphgt; /* paper dimensions */ |
| 45 |
double margin, border; /* paper & page margins */ |
| 46 |
double vshift, hshift; /* page centring shifts */ |
| 47 |
double iwidth, iheight ; /* input paper size */ |
| 48 |
double tolerance = 100000; /* layout tolerance */ |
| 49 |
off_t sizeheaders[20]; /* headers to remove */ |
| 50 |
int opt; |
| 51 |
|
| 52 |
set_program_name (argv[0]); |
| 53 |
|
| 54 |
verbose = 1; |
| 55 |
|
| 56 |
if (!paper_size(NULL, &width, &height)) |
| 57 |
die("could not get default paper size"); |
| 58 |
|
| 59 |
margin = border = vshift = hshift = column = flip = 0; |
| 60 |
leftright = topbottom = 1; |
| 61 |
iwidth = iheight = -1 ; |
| 62 |
|
| 63 |
while((opt = |
| 64 |
getopt(argc, argv, |
| 65 |
"qd::lrfcw:W:h:H:m:b:t:s:p:P:n:1::2::3::4::5::6::7::8::9::")) |
| 66 |
!= EOF) { |
| 67 |
switch(opt) { |
| 68 |
case 'q': /* quiet */ |
| 69 |
verbose = 0; |
| 70 |
break; |
| 71 |
case 'd': /* draw borders */ |
| 72 |
if (optarg) |
| 73 |
draw = singledimen(optarg); |
| 74 |
else |
| 75 |
draw = 1; |
| 76 |
break; |
| 77 |
case 'l': /* landscape (rotated left) */ |
| 78 |
column = !column; |
| 79 |
topbottom = !topbottom; |
| 80 |
break; |
| 81 |
case 'r': /* seascape (rotated right) */ |
| 82 |
column = !column; |
| 83 |
leftright = !leftright; |
| 84 |
break; |
| 85 |
case 'f': /* flipped */ |
| 86 |
flip = 1; |
| 87 |
break; |
| 88 |
case 'c': /* column major layout */ |
| 89 |
column = !column; |
| 90 |
break; |
| 91 |
case 'w': /* page width */ |
| 92 |
width = singledimen(optarg); |
| 93 |
break; |
| 94 |
case 'W': /* input page width */ |
| 95 |
iwidth = singledimen(optarg); |
| 96 |
break; |
| 97 |
case 'h': /* page height */ |
| 98 |
height = singledimen(optarg); |
| 99 |
break; |
| 100 |
case 'H': /* input page height */ |
| 101 |
iheight = singledimen(optarg); |
| 102 |
break; |
| 103 |
case 'm': /* margins around whole page */ |
| 104 |
margin = singledimen(optarg); |
| 105 |
break; |
| 106 |
case 'b': /* border around individual pages */ |
| 107 |
border = singledimen(optarg); |
| 108 |
break; |
| 109 |
case 't': /* layout tolerance */ |
| 110 |
tolerance = atof(optarg); |
| 111 |
break; |
| 112 |
case 's': /* override scale */ |
| 113 |
uscale = atof(optarg); |
| 114 |
break; |
| 115 |
case 'p': /* output (and by default input) paper type */ |
| 116 |
if (!paper_size(optarg, &width, &height)) |
| 117 |
die("paper size '%s' not recognised", optarg); |
| 118 |
break; |
| 119 |
case 'P': /* paper type */ |
| 120 |
if (!paper_size(optarg, &width, &height)) |
| 121 |
die("paper size '%s' not recognised", optarg); |
| 122 |
break; |
| 123 |
case 'n': /* n-up, for compatibility with other psnups */ |
| 124 |
if ((nup = atoi(optarg)) < 1) |
| 125 |
die("-n %d too small", nup); |
| 126 |
break; |
| 127 |
case '1': |
| 128 |
case '2': |
| 129 |
case '3': |
| 130 |
case '4': |
| 131 |
case '5': |
| 132 |
case '6': |
| 133 |
case '7': |
| 134 |
case '8': |
| 135 |
case '9': |
| 136 |
if(optarg) { |
| 137 |
char *valuestr = (char *) malloc(strlen(optarg) + 2); |
| 138 |
valuestr[0] = opt; |
| 139 |
strcpy(&(valuestr[1]), optarg); |
| 140 |
|
| 141 |
/* really should check that valuestr is only digits here...*/ |
| 142 |
if ((nup = atoi(valuestr)) < 1) |
| 143 |
die("-n %d too small", nup); |
| 144 |
free(valuestr); |
| 145 |
} else { |
| 146 |
nup = (opt - '0'); |
| 147 |
} |
| 148 |
break; |
| 149 |
case 'v': /* version */ |
| 150 |
default: |
| 151 |
usage(); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
infile = stdin; |
| 156 |
outfile = stdout; |
| 157 |
|
| 158 |
/* Be defensive */ |
| 159 |
if((argc - optind) < 0 || (argc - optind) > 2) usage(); |
| 160 |
|
| 161 |
if (optind != argc) { |
| 162 |
/* User specified an input file */ |
| 163 |
if ((infile = fopen(argv[optind], "rb")) == NULL) |
| 164 |
die("can't open input file %s", argv[optind]); |
| 165 |
optind++; |
| 166 |
} |
| 167 |
|
| 168 |
if (optind != argc) { |
| 169 |
/* User specified an output file */ |
| 170 |
if ((outfile = fopen(argv[optind], "wb")) == NULL) |
| 171 |
die("can't open output file %s", argv[optind]); |
| 172 |
optind++; |
| 173 |
} |
| 174 |
|
| 175 |
if (optind != argc) usage(); |
| 176 |
|
| 177 |
if (infile == stdin && set_binary_mode(fileno(stdin), O_BINARY) < 0) |
| 178 |
die("can't reset stdin to binary mode"); |
| 179 |
if (outfile == stdout && set_binary_mode(fileno(stdout), O_BINARY) < 0) |
| 180 |
die("can't reset stdout to binary mode"); |
| 181 |
|
| 182 |
if ((infile=seekable(infile))==NULL) |
| 183 |
die("can't seek input"); |
| 184 |
|
| 185 |
if (width <= 0 || height <= 0) |
| 186 |
die("page width and height must be set"); |
| 187 |
|
| 188 |
/* subtract paper margins from height & width */ |
| 189 |
ppwid = width - margin*2; |
| 190 |
pphgt = height - margin*2; |
| 191 |
|
| 192 |
if (ppwid <= 0 || pphgt <= 0) |
| 193 |
die("paper margins are too large"); |
| 194 |
|
| 195 |
scanpages(sizeheaders); |
| 196 |
|
| 197 |
/* set default values of input height & width */ |
| 198 |
if ( iwidth > 0 ) |
| 199 |
width = iwidth ; |
| 200 |
if ( iheight > 0 ) |
| 201 |
height = iheight ; |
| 202 |
|
| 203 |
/* Finding the best layout is an optimisation problem. We try all of the |
| 204 |
* combinations of width*height in both normal and rotated form, and |
| 205 |
* minimise the wasted space. */ |
| 206 |
{ |
| 207 |
double best = tolerance; |
| 208 |
int hor; |
| 209 |
for (hor = 1; hor; hor = nextdiv(hor, nup)) { |
| 210 |
int ver = nup/hor; |
| 211 |
/* try normal orientation first */ |
| 212 |
double scl = MIN(pphgt/(height*ver), ppwid/(width*hor)); |
| 213 |
double optim = (ppwid-scl*width*hor)*(ppwid-scl*width*hor) + |
| 214 |
(pphgt-scl*height*ver)*(pphgt-scl*height*ver); |
| 215 |
if (optim < best) { |
| 216 |
best = optim; |
| 217 |
/* recalculate scale to allow for internal borders */ |
| 218 |
scale = MIN((pphgt-2*border*ver)/(height*ver), |
| 219 |
(ppwid-2*border*hor)/(width*hor)); |
| 220 |
hshift = (ppwid/hor - width*scale)/2; |
| 221 |
vshift = (pphgt/ver - height*scale)/2; |
| 222 |
horiz = hor; vert = ver; |
| 223 |
rotate = flip; |
| 224 |
} |
| 225 |
/* try rotated orientation */ |
| 226 |
scl = MIN(pphgt/(width*hor), ppwid/(height*ver)); |
| 227 |
optim = (pphgt-scl*width*hor)*(pphgt-scl*width*hor) + |
| 228 |
(ppwid-scl*height*ver)*(ppwid-scl*height*ver); |
| 229 |
if (optim < best) { |
| 230 |
best = optim; |
| 231 |
/* recalculate scale to allow for internal borders */ |
| 232 |
scale = MIN((pphgt-2*border*hor)/(width*hor), |
| 233 |
(ppwid-2*border*ver)/(height*ver)); |
| 234 |
hshift = (ppwid/ver - height*scale)/2; |
| 235 |
vshift = (pphgt/hor - width*scale)/2; |
| 236 |
horiz = ver; vert = hor; |
| 237 |
rotate = !flip; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
/* fail if nothing better than worst tolerance was found */ |
| 242 |
if (best == tolerance) |
| 243 |
die("can't find acceptable layout for %d-up", nup); |
| 244 |
} |
| 245 |
|
| 246 |
if (flip) { /* swap width & height for clipping */ |
| 247 |
double tmp = width; |
| 248 |
width = height; |
| 249 |
height = tmp; |
| 250 |
} |
| 251 |
|
| 252 |
if (rotate) { /* rotate leftright and topbottom orders */ |
| 253 |
int tmp = topbottom; |
| 254 |
topbottom = !leftright; |
| 255 |
leftright = tmp; |
| 256 |
column = !column; |
| 257 |
} |
| 258 |
|
| 259 |
/* now construct specification list and run page rearrangement procedure */ |
| 260 |
{ |
| 261 |
int page = 0; |
| 262 |
PageSpec *specs, *tail; |
| 263 |
|
| 264 |
tail = specs = newspec(); |
| 265 |
|
| 266 |
while (page < nup) { |
| 267 |
int up, across; /* page index */ |
| 268 |
|
| 269 |
if (column) { |
| 270 |
if (leftright) /* left to right */ |
| 271 |
across = page/vert; |
| 272 |
else /* right to left */ |
| 273 |
across = horiz-1-page/vert; |
| 274 |
if (topbottom) /* top to bottom */ |
| 275 |
up = vert-1-page%vert; |
| 276 |
else /* bottom to top */ |
| 277 |
up = page%vert; |
| 278 |
} else { |
| 279 |
if (leftright) /* left to right */ |
| 280 |
across = page%horiz; |
| 281 |
else /* right to left */ |
| 282 |
across = horiz-1-page%horiz; |
| 283 |
if (topbottom) /* top to bottom */ |
| 284 |
up = vert-1-page/horiz; |
| 285 |
else /* bottom to top */ |
| 286 |
up = page/horiz; |
| 287 |
} |
| 288 |
if (rotate) { |
| 289 |
tail->xoff = margin + (across+1)*ppwid/horiz - hshift; |
| 290 |
tail->rotate = 90; |
| 291 |
tail->flags |= ROTATE; |
| 292 |
} else { |
| 293 |
tail->xoff = margin + across*ppwid/horiz + hshift; |
| 294 |
} |
| 295 |
tail->pageno = page; |
| 296 |
if (uscale > 0) |
| 297 |
tail->scale = uscale; |
| 298 |
else |
| 299 |
tail->scale = scale; |
| 300 |
tail->flags |= SCALE; |
| 301 |
tail->yoff = margin + up*pphgt/vert + vshift; |
| 302 |
tail->flags |= OFFSET; |
| 303 |
if (++page < nup) { |
| 304 |
tail->flags |= ADD_NEXT; |
| 305 |
tail->next = newspec(); |
| 306 |
tail = tail->next; |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
pstops_write(nup, 1, 0, specs, draw, sizeheaders); /* do page rearrangement */ |
| 311 |
} |
| 312 |
|
| 313 |
return 0; |
| 314 |
} |
| 315 |
|