home *** CD-ROM | disk | FTP | other *** search
- /* psspec.c
- * AJCD 5/6/93
- * page spec routines for page rearrangement
- */
-
- #include "psutil.h"
- #include "psspec.h"
- #include "patchlevel.h"
-
- double width = -1;
- double height = -1;
-
- /* create a new page spec */
- struct pagespec *newspec()
- {
- struct pagespec *temp = (struct pagespec *)malloc(sizeof(struct pagespec));
- if (temp == NULL) {
- fprintf(stderr, "%s: out of memory\n", prog);
- fflush(stderr);
- exit(1);
- }
- temp->reversed = temp->pageno = temp->flags = temp->rotate = 0;
- temp->scale = 1;
- temp->xoff = temp->yoff = 0;
- temp->next = NULL;
- return (temp);
- }
-
- /* dimension parsing routines */
- int parseint(sp)
- char **sp;
- {
- char *s = *sp;
- int num = atoi(s);
-
- while (isdigit(*s))
- s++;
- if (*sp == s) argerror();
- *sp = s;
- return (num);
- }
-
- double parsedouble(sp)
- char **sp;
- {
- char *s = *sp;
- double num = atof(s);
-
- while (isdigit(*s) || *s == '-' || *s == '.')
- s++;
- if (*sp == s) argerror();
- *sp = s;
- return (num);
- }
-
- double parsedimen(sp)
- char **sp;
- {
- double num = parsedouble(sp);
- char *s = *sp;
-
- if (strncmp(s, "pt", 2) == 0) {
- s += 2;
- } else if (strncmp(s, "in", 2) == 0) {
- num *= 72;
- s += 2;
- } else if (strncmp(s, "cm", 2) == 0) {
- num *= 28.346456692913385211;
- s += 2;
- } else if (strncmp(s, "mm", 2) == 0) {
- num *= 2.8346456692913385211;
- s += 2;
- } else if (*s == 'w') {
- if (width < 0) {
- fprintf(stderr, "%s: width not initialised\n", prog);
- fflush(stderr);
- exit(1);
- }
- num *= width;
- s++;
- } else if (*s == 'h') {
- if (height < 0) {
- fprintf(stderr, "%s: height not initialised\n", prog);
- fflush(stderr);
- exit(1);
- }
- num *= height;
- s++;
- }
- *sp = s;
- return (num);
- }
-
- double singledimen(str)
- char *str;
- {
- double num = parsedimen(&str);
- if (*str) usage();
- return (num);
- }
-
- void pstops(modulo, pps, nobind, specs, draw)
- int modulo, pps, nobind;
- double draw;
- struct pagespec *specs;
- {
- int thispg, maxpage;
- int pageindex = 0;
-
- scanpages();
-
- maxpage = ((pages+modulo-1)/modulo)*modulo;
-
- /* rearrange pages: doesn't cope properly with loaded definitions */
- writeheader((maxpage/modulo)*pps);
- writestring("%%BeginProcSet: pstops");
- if (width > 0 && height > 0)
- writestring("-clip");
- if (nobind)
- writestring("-nobind");
- #ifdef SHOWPAGE_LOAD
- writestring("-spload");
- #endif
- writestring(" 1 0\n");
- #ifndef SHOWPAGE_LOAD
- writestring("[/showpage/erasepage/copypage]{dup where{pop dup load\n");
- writestring(" type/operatortype eq{1 array cvx dup 0 3 index cvx put\n");
- writestring(" bind def}{pop}ifelse}{pop}ifelse}forall\n");
- #else
- writestring("[/showpage/copypage/erasepage]{dup 10 string cvs dup\n");
- writestring(" length 6 add string dup 0 (pstops) putinterval dup\n");
- writestring(" 6 4 -1 roll putinterval 2 copy cvn dup where\n");
- writestring(" {pop pop pop}{exch load def}ifelse cvx cvn 1 array cvx\n");
- writestring(" dup 0 4 -1 roll put def}forall\n");
- #endif
- writestring("[/letter/legal/executivepage/a4/a4small/b5/com10envelope\n");
- writestring(" /monarchenvelope/c5envelope/dlenvelope/lettersmall/note\n");
- writestring(" /folio/quarto/a5]{dup where{dup wcheck{exch{}put}\n");
- writestring(" {pop{}def}ifelse}{pop}ifelse}forall\n");
- writestring("/lcvx{dup load dup type dup/operatortype eq{pop exch pop}\n");
- writestring(" {/arraytype eq{dup xcheck{exch pop aload pop}\n");
- writestring(" {pop cvx}ifelse}{pop cvx}ifelse}ifelse}bind def\n");
- writestring("/pstopsmatrix matrix currentmatrix def\n");
- writestring("/pstopsxform matrix def\n");
- writestring("/defaultmatrix{pstopsmatrix exch pstopsxform exch concatmatrix}bind def\n");
- writestring("/initmatrix{matrix defaultmatrix setmatrix}bind def\n");
- writestring("/pathtoproc{[{currentpoint}stopped{$error/newerror false\n");
- writestring(" put{newpath}}{/newpath cvx 3 1 roll/moveto cvx 4 array\n");
- writestring(" astore cvx}ifelse]{[/newpath cvx{/moveto cvx}{/lineto cvx}\n");
- writestring(" {/curveto cvx}{/closepath cvx}pathforall]cvx exch pop}\n");
- writestring(" stopped{$error/errorname get/invalidaccess eq{cleartomark\n");
- writestring(" $error/newerror false put cvx exec}{stop}ifelse}if}def\n");
- if (width > 0 && height > 0) {
- char buffer[BUFSIZ];
- writestring("/initclip[/matrix lcvx/currentmatrix lcvx/pstopsmatrix cvx/setmatrix lcvx\n");
- writestring(" /pathtoproc lcvx/initclip lcvx/newpath lcvx\n");
- writestring(" 0 0 /moveto lcvx");
- sprintf(buffer,
- " %lf 0/rlineto lcvx\n 0 %lf/rlineto lcvx -%lf 0/rlineto lcvx\n",
- width, height, width);
- writestring(buffer);
- writestring(" /closepath lcvx/clip lcvx\n");
- writestring(" /newpath lcvx/exec lcvx/setmatrix lcvx]cvx def\n");
- }
- writestring("/initgraphics{initmatrix newpath initclip 1 setlinewidth\n");
- writestring(" 0 setlinecap 0 setlinejoin []0 setdash 0 setgray\n");
- writestring(" 10 setmiterlimit}bind def\n");
- if (nobind) /* desperation measures */
- writestring("/bind{}def\n");
- writestring("%%EndProcSet\n");
- /* save transformation from original to current matrix */
- writeprolog("/pstopsxform pstopsmatrix matrix currentmatrix matrix invertmatrix matrix concatmatrix matrix invertmatrix store\n");
- for (thispg = 0; thispg < maxpage; thispg += modulo) {
- int add_last = 0;
- struct pagespec *ps;
- for (ps = specs; ps != NULL; ps = ps->next) {
- int actualpg;
- int add_next = ((ps->flags & ADD_NEXT) != 0);
- if (ps->reversed)
- actualpg = maxpage-thispg-modulo+ps->pageno;
- else
- actualpg = thispg+ps->pageno;
- if (actualpg < pages)
- seekpage(actualpg);
- if (!add_last) {
- writepageheader("pstops", ++pageindex);
- }
- writestring("/pstopssaved save def\n");
- if (ps->flags & GSAVE) {
- char buffer[BUFSIZ];
- writestring("pstopsmatrix setmatrix\n");
- if (ps->flags & OFFSET) {
- sprintf(buffer, "%lf %lf translate\n", ps->xoff, ps->yoff);
- writestring(buffer);
- }
- if (ps->flags & ROTATE) {
- sprintf(buffer, "%d rotate\n", ps->rotate);
- writestring(buffer);
- }
- if (ps->flags & SCALE) {
- sprintf(buffer, "%lf dup scale\n", ps->scale);
- writestring(buffer);
- }
- if (width > 0 && height > 0) {
- writestring("/pstopsmatrix matrix currentmatrix def\n");
- writestring("initclip\n");
- if (draw > 0) {
- sprintf(buffer, "gsave clippath 0 setgray %lf setlinewidth stroke grestore\n", draw);
- writestring(buffer);
- }
- }
- writestring("pstopsxform concat\n");
- }
- if (add_next) {
- #ifndef SHOWPAGE_LOAD
- writestring("/showpage{}def/copypage{}def/erasepage{}def\n");
- #else
- writestring("/pstopsshowpage{}def/pstopscopypage{}def/pstopserasepage{}def\n");
- #endif
- }
- if (actualpg < pages)
- writepagebody(actualpg);
- else
- writestring("showpage\n");
- writestring("pstopssaved restore\n");
- add_last = add_next;
- }
- }
- writetrailer();
- }
-