| 1 |
/* psspec.c |
| 2 |
* Page spec routines for page rearrangement |
| 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 "psutil.h" |
| 12 |
#include "psspec.h" |
| 13 |
|
| 14 |
#include <string.h> |
| 15 |
|
| 16 |
double width = -1; |
| 17 |
double height = -1; |
| 18 |
|
| 19 |
/* create a new page spec */ |
| 20 |
PageSpec *newspec(void) |
| 21 |
{ |
| 22 |
PageSpec *temp = (PageSpec *)malloc(sizeof(PageSpec)); |
| 23 |
if (temp == NULL) |
| 24 |
die("out of memory"); |
| 25 |
temp->pageno = temp->flags = temp->rotate = 0; |
| 26 |
temp->scale = 1; |
| 27 |
temp->xoff = temp->yoff = 0; |
| 28 |
temp->next = NULL; |
| 29 |
return (temp); |
| 30 |
} |
| 31 |
|
| 32 |
/* dimension parsing routines */ |
| 33 |
int parseint(char **sp) |
| 34 |
{ |
| 35 |
char *s = *sp; |
| 36 |
int num = atoi(s); |
| 37 |
|
| 38 |
while (isdigit((unsigned char)*s)) |
| 39 |
s++; |
| 40 |
if (*sp == s) argerror(); |
| 41 |
*sp = s; |
| 42 |
return (num); |
| 43 |
} |
| 44 |
|
| 45 |
double parsedouble(char **sp) |
| 46 |
{ |
| 47 |
char *s = *sp; |
| 48 |
double num = atof(s); |
| 49 |
|
| 50 |
while (isdigit((unsigned char)*s) || *s == '-' || *s == '.') |
| 51 |
s++; |
| 52 |
if (*sp == s) argerror(); |
| 53 |
*sp = s; |
| 54 |
return (num); |
| 55 |
} |
| 56 |
|
| 57 |
double parsedimen(char **sp) |
| 58 |
{ |
| 59 |
double num = parsedouble(sp); |
| 60 |
char *s = *sp; |
| 61 |
|
| 62 |
if (strncmp(s, "pt", 2) == 0) { |
| 63 |
s += 2; |
| 64 |
} else if (strncmp(s, "in", 2) == 0) { |
| 65 |
num *= 72; |
| 66 |
s += 2; |
| 67 |
} else if (strncmp(s, "cm", 2) == 0) { |
| 68 |
num *= 28.346456692913385211; |
| 69 |
s += 2; |
| 70 |
} else if (strncmp(s, "mm", 2) == 0) { |
| 71 |
num *= 2.8346456692913385211; |
| 72 |
s += 2; |
| 73 |
} else if (*s == 'w') { |
| 74 |
if (width < 0) |
| 75 |
die("width not initialised"); |
| 76 |
num *= width; |
| 77 |
s++; |
| 78 |
} else if (*s == 'h') { |
| 79 |
if (height < 0) |
| 80 |
die("height not initialised"); |
| 81 |
num *= height; |
| 82 |
s++; |
| 83 |
} |
| 84 |
*sp = s; |
| 85 |
return (num); |
| 86 |
} |
| 87 |
|
| 88 |
double singledimen(char *str) |
| 89 |
{ |
| 90 |
double num = parsedimen(&str); |
| 91 |
if (*str) usage(); |
| 92 |
return (num); |
| 93 |
} |
| 94 |
|
| 95 |
static const char *prologue[] = { /* PStoPS procset */ |
| 96 |
/* Wrap these up with our own versions. We have to */ |
| 97 |
"userdict begin", |
| 98 |
"[/showpage/erasepage/copypage]{dup where{pop dup load", |
| 99 |
" type/operatortype eq{ /PStoPSenablepage cvx 1 index" |
| 100 |
" load 1 array astore cvx {} bind /ifelse cvx 4 array" |
| 101 |
" astore cvx def}{pop}ifelse}{pop}ifelse}forall" |
| 102 |
" /PStoPSenablepage true def", |
| 103 |
"[/letter/legal/executivepage/a4/a4small/b5/com10envelope", /* nullify */ |
| 104 |
" /monarchenvelope/c5envelope/dlenvelope/lettersmall/note", /* paper */ |
| 105 |
" /folio/quarto/a5]{dup where{dup wcheck{exch{}put}", /* operators */ |
| 106 |
" {pop{}def}ifelse}{pop}ifelse}forall", |
| 107 |
"/setpagedevice {pop}bind 1 index where{dup wcheck{3 1 roll put}", |
| 108 |
" {pop def}ifelse}{def}ifelse", |
| 109 |
"/PStoPSmatrix matrix currentmatrix def", |
| 110 |
"/PStoPSxform matrix def/PStoPSclip{clippath}def", |
| 111 |
"/defaultmatrix{PStoPSmatrix exch PStoPSxform exch concatmatrix}bind def", |
| 112 |
"/initmatrix{matrix defaultmatrix setmatrix}bind def", |
| 113 |
"/initclip[{matrix currentmatrix PStoPSmatrix setmatrix", |
| 114 |
" [{currentpoint}stopped{$error/newerror false put{newpath}}", |
| 115 |
" {/newpath cvx 3 1 roll/moveto cvx 4 array astore cvx}ifelse]", |
| 116 |
" {[/newpath cvx{/moveto cvx}{/lineto cvx}", |
| 117 |
" {/curveto cvx}{/closepath cvx}pathforall]cvx exch pop}", |
| 118 |
" stopped{$error/errorname get/invalidaccess eq{cleartomark", |
| 119 |
" $error/newerror false put cvx exec}{stop}ifelse}if}bind aload pop", |
| 120 |
" /initclip dup load dup type dup/operatortype eq{pop exch pop}", |
| 121 |
" {dup/arraytype eq exch/packedarraytype eq or", |
| 122 |
" {dup xcheck{exch pop aload pop}{pop cvx}ifelse}", |
| 123 |
" {pop cvx}ifelse}ifelse", |
| 124 |
" {newpath PStoPSclip clip newpath exec setmatrix} bind aload pop]cvx def", |
| 125 |
"/initgraphics{initmatrix newpath initclip 1 setlinewidth", |
| 126 |
" 0 setlinecap 0 setlinejoin []0 setdash 0 setgray", |
| 127 |
" 10 setmiterlimit}bind def", |
| 128 |
"end", |
| 129 |
NULL |
| 130 |
}; |
| 131 |
|
| 132 |
void pstops(int modulo, int pps, int nobind, PageSpec *specs, double draw) { |
| 133 |
|
| 134 |
scanpages(NULL); |
| 135 |
pstops_write(modulo, pps, nobind, specs, draw, NULL); |
| 136 |
} |
| 137 |
|
| 138 |
void pstops_write(int modulo, int pps, int nobind, PageSpec *specs, double draw, off_t *ignorelist) |
| 139 |
{ |
| 140 |
int thispg, maxpage; |
| 141 |
int pageindex = 0; |
| 142 |
const char **pro; |
| 143 |
|
| 144 |
maxpage = ((pages+modulo-1)/modulo)*modulo; |
| 145 |
|
| 146 |
/* rearrange pages: doesn't cope properly with loaded definitions */ |
| 147 |
writeheadermedia((maxpage/modulo)*pps, ignorelist, width, height); |
| 148 |
writestring("%%BeginProcSet: PStoPS"); |
| 149 |
if (nobind) |
| 150 |
writestring("-nobind"); |
| 151 |
writestring(" 1 15\n"); |
| 152 |
for (pro = prologue; *pro; pro++) { |
| 153 |
writestring(*pro); |
| 154 |
writestring("\n"); |
| 155 |
} |
| 156 |
if (nobind) /* desperation measures */ |
| 157 |
writestring("/bind{}def\n"); |
| 158 |
writestring("%%EndProcSet\n"); |
| 159 |
/* save transformation from original to current matrix */ |
| 160 |
if (writepartprolog()) { |
| 161 |
writestring("userdict/PStoPSxform PStoPSmatrix matrix currentmatrix\n"); |
| 162 |
writestring(" matrix invertmatrix matrix concatmatrix\n"); |
| 163 |
writestring(" matrix invertmatrix put\n"); |
| 164 |
} |
| 165 |
writesetup(); |
| 166 |
for (thispg = 0; thispg < maxpage; thispg += modulo) { |
| 167 |
int add_last = 0; |
| 168 |
PageSpec *ps; |
| 169 |
for (ps = specs; ps != NULL; ps = ps->next) { |
| 170 |
int actualpg; |
| 171 |
int add_next = ((ps->flags & ADD_NEXT) != 0); |
| 172 |
if (ps->flags & REVERSED) |
| 173 |
actualpg = maxpage-thispg-modulo+ps->pageno; |
| 174 |
else |
| 175 |
actualpg = thispg+ps->pageno; |
| 176 |
if (actualpg < pages) |
| 177 |
seekpage(actualpg); |
| 178 |
if (!add_last) { /* page label contains original pages */ |
| 179 |
PageSpec *np = ps; |
| 180 |
char *eob = pagelabel; |
| 181 |
char sep = '('; |
| 182 |
do { |
| 183 |
*eob++ = sep; |
| 184 |
if (np->flags & REVERSED) |
| 185 |
sprintf(eob, "%d", maxpage-thispg-modulo+np->pageno); |
| 186 |
else |
| 187 |
sprintf(eob, "%d", thispg+np->pageno); |
| 188 |
eob = eob + strlen(eob); |
| 189 |
sep = ','; |
| 190 |
} while ((np->flags & ADD_NEXT) && (np = np->next)); |
| 191 |
strcpy(eob, ")"); |
| 192 |
writepageheader(pagelabel, ++pageindex); |
| 193 |
} |
| 194 |
writestring("userdict/PStoPSsaved save put\n"); |
| 195 |
if (ps->flags & GSAVE) { |
| 196 |
char buffer[BUFSIZ]; |
| 197 |
writestring("PStoPSmatrix setmatrix\n"); |
| 198 |
if (ps->flags & OFFSET) { |
| 199 |
sprintf(buffer, "%f %f translate\n", ps->xoff, ps->yoff); |
| 200 |
writestring(buffer); |
| 201 |
} |
| 202 |
if (ps->flags & ROTATE) { |
| 203 |
sprintf(buffer, "%d rotate\n", ps->rotate); |
| 204 |
writestring(buffer); |
| 205 |
} |
| 206 |
if (ps->flags & HFLIP) { |
| 207 |
sprintf(buffer, "[ -1 0 0 1 %f 0 ] concat\n", width*ps->scale); |
| 208 |
writestring(buffer); |
| 209 |
} |
| 210 |
if (ps->flags & VFLIP) { |
| 211 |
sprintf(buffer, "[ 1 0 0 -1 0 %f ] concat\n", height*ps->scale); |
| 212 |
writestring(buffer); |
| 213 |
} |
| 214 |
if (ps->flags & SCALE) { |
| 215 |
sprintf(buffer, "%f dup scale\n", ps->scale); |
| 216 |
writestring(buffer); |
| 217 |
} |
| 218 |
writestring("userdict/PStoPSmatrix matrix currentmatrix put\n"); |
| 219 |
if (width > 0 && height > 0) { |
| 220 |
writestring("userdict/PStoPSclip{0 0 moveto\n"); |
| 221 |
sprintf(buffer, " %f 0 rlineto 0 %f rlineto -%f 0 rlineto\n", |
| 222 |
width, height, width); |
| 223 |
writestring(buffer); |
| 224 |
writestring(" closepath}put initclip\n"); |
| 225 |
if (draw > 0) { |
| 226 |
sprintf(buffer, "gsave clippath 0 setgray %f setlinewidth stroke grestore\n", draw); |
| 227 |
writestring(buffer); |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
if (add_next) |
| 232 |
writestring("/PStoPSenablepage false def\n"); |
| 233 |
if (actualpg < pages) { |
| 234 |
writepagesetup(); |
| 235 |
writestring("PStoPSxform concat\n"); |
| 236 |
writepagebody(actualpg); |
| 237 |
} else { |
| 238 |
writestring("PStoPSxform concat\n"); |
| 239 |
writestring("showpage\n"); |
| 240 |
} |
| 241 |
writestring("PStoPSsaved restore\n"); |
| 242 |
add_last = add_next; |
| 243 |
} |
| 244 |
} |
| 245 |
writetrailer(); |
| 246 |
} |