This is postscript.c in view mode; [Download] [Up]
/* This file contains the PostScript device dependent subroutines for */ /* use with plplot. */ #include "plplot.h" #include <stdio.h> #define LINELENGTH 70 #define COPIES 1 /* 7.5" x 10" (72 points equal 1 inch) */ #define XSIZE 540 #define YSIZE 720 #define ENLARGE 5 #define XPSSIZE ENLARGE*XSIZE #define YPSSIZE ENLARGE*YSIZE /* Offsets are .5" each */ #define XOFFSET 36 #define YOFFSET 36 #define XSCALE 100 #define YSCALE 100 #define LINESCALE 100 #define PSX XPSSIZE-1 #define PSY YPSSIZE-1 char *getdate(); static int numpages = 0; static int linepos = 0; static FILE *OutFile=NULL; static int orient=1; static int fileopen=0; void pssetup(xdpi, ydpi, xwid, ywid) int xwid, ywid; double xdpi, ydpi; { } void psorient(ori) int ori; { orient = ori; } void psselect(file) FILE *file; { OutFile = file; fileopen=0; } /* Open file. Set up for graphics. */ void psinit() { char line[80]; /* setpxl() sets the dots/mm in the x and y directions */ setpxl(14.173,14.173); /* setphy() sets the device coordinates. These are integer */ /* values. Set up for landscape orientation (long axis of page in the */ /* x direction). Origin is in the lower left hand corner. */ /* postscript default is portrait */ if(!orient) setphy(0,PSY,0,PSX); else setphy(0,PSX,0,PSY); /* Set default pen color using scol(color). */ /* Any default pen color can be used but a black pen is probably best. */ scol(1); /* Set default pen width using swid(width) */ swid(1); /* Set device interaction mode using smod(mode). Set mode to 0 for */ /* a noninteractive device, Unless you are writing your */ /* own Amiga screen driver mode should be 0. */ smod(0); /* Well that's all the information plplot needs. Let's prompt for a */ /* graphics file name. */ while(!OutFile) { printf("Enter PostScript graphics file name. "); scanf("%s",line); if (!(OutFile = fopen(line,"w"))) fprintf(stderr,"Can't open %s.\n",line); else fileopen=1; } /* Header comments into PostScript file */ fprintf(OutFile,"%%!PS-Adobe-2.0 EPSF-2.0\n"); fprintf(OutFile,"%%%%BoundingBox: (atend)\n"); fprintf(OutFile,"%%%%Title: PLPLOT Graph\n"); fprintf(OutFile,"%%%%Creator: PLPLOT Version 3.0\n"); fprintf(OutFile,"%%%%CreationDate: %s\n", getdate()); fprintf(OutFile,"%%%%Pages: (atend)\n"); fprintf(OutFile,"%%%%EndComments\n\n"); /* Definitions */ fprintf(OutFile, "/PSSave save def\n"); /* save VM state */ fprintf(OutFile, "/PSDict 200 dict def\n"); /* define a dictionary */ fprintf(OutFile, "PSDict begin\n"); /* start using it */ fprintf(OutFile, "/@restore /restore load def\n"); fprintf(OutFile, "/restore\n"); fprintf(OutFile, " {vmstatus pop\n"); fprintf(OutFile, " dup @VMused lt {pop @VMused} if\n"); fprintf(OutFile, " exch pop exch @restore /@VMused exch def\n"); fprintf(OutFile, " } def\n"); fprintf(OutFile, "/@pri\n"); fprintf(OutFile, " {\n"); fprintf(OutFile, " ( ) print\n"); fprintf(OutFile, " ( ) cvs print\n"); fprintf(OutFile, " } def\n"); fprintf(OutFile, "/@copies\n"); /* n @copies - */ fprintf(OutFile, " {\n"); fprintf(OutFile, " /#copies exch def\n"); fprintf(OutFile, " } def\n"); fprintf(OutFile, "/@start\n"); /* - @start - -- start everything */ fprintf(OutFile, " {\n"); fprintf(OutFile, " vmstatus pop /@VMused exch def pop\n"); fprintf(OutFile, " } def\n"); fprintf(OutFile, "/@end\n"); /* - @end - -- finished */ fprintf(OutFile, " {(VM Used: ) print @VMused @pri\n"); fprintf(OutFile, " (. Unused: ) print vmstatus @VMused sub @pri pop pop\n"); fprintf(OutFile, " (\\n) print flush\n"); fprintf(OutFile, " end\n"); fprintf(OutFile, " PSSave restore\n"); fprintf(OutFile, " } def\n"); fprintf(OutFile, "/bop\n"); /* bop - -- begin a new page */ fprintf(OutFile, " {\n"); fprintf(OutFile, " /SaveImage save def\n"); fprintf(OutFile, " } def\n"); fprintf(OutFile, "/eop\n"); /* - eop - -- end a page */ fprintf(OutFile, " {\n"); fprintf(OutFile, " showpage\n"); fprintf(OutFile, " SaveImage restore\n"); fprintf(OutFile, " } def\n"); fprintf(OutFile, "/@line\n"); /* set line parameters */ fprintf(OutFile, " {0 setlinecap\n"); fprintf(OutFile, " 0 setlinejoin\n"); fprintf(OutFile, " 1 setmiterlimit\n"); fprintf(OutFile, " } def\n"); /* d @hsize - horizontal clipping dimension */ fprintf(OutFile, "/@hsize {/hs exch def} def\n"); fprintf(OutFile, "/@vsize {/vs exch def} def\n"); /* d @hoffset - shift for the plots */ fprintf(OutFile, "/@hoffset {/ho exch def} def\n"); fprintf(OutFile, "/@voffset {/vo exch def} def\n"); /* s @hscale - scale factors */ fprintf(OutFile, "/@hscale {100 div /hsc exch def} def\n"); fprintf(OutFile, "/@vscale {100 div /vsc exch def} def\n"); /* s @lscale - linewidth scale factor */ fprintf(OutFile, "/@lscale {100 div /lin exch def} def\n"); fprintf(OutFile, "/@lwidth {lin lw mul setlinewidth} def\n"); fprintf(OutFile, "/@SetPlot\n"); /* setup user specified offsets, */ fprintf(OutFile, " {\n"); /* scales, sizes for clipping */ fprintf(OutFile, " ho vo translate\n"); fprintf(OutFile, " XScale YScale scale\n"); fprintf(OutFile, " lin lw mul setlinewidth\n"); fprintf(OutFile, " } def\n"); fprintf(OutFile, "/XScale\n"); /* setup x scale */ fprintf(OutFile, " {hsc hs mul %d div} def\n", YPSSIZE); fprintf(OutFile, "/YScale\n"); /* setup y scale */ fprintf(OutFile, " {vsc vs mul %d div} def\n", XPSSIZE); fprintf(OutFile, "/lw 1 def\n"); /* default line width */ fprintf(OutFile, "/M {moveto} def\n"); fprintf(OutFile, "/D {lineto} def\n"); fprintf(OutFile, "/S {stroke} def\n"); fprintf(OutFile, "/Z {stroke newpath} def\n"); fprintf(OutFile, "end\n\n"); /* end of dictionary definition */ /* Set up the plots */ fprintf(OutFile, "PSDict begin\n"); fprintf(OutFile, "@start\n"); fprintf(OutFile, "%d @copies\n", COPIES); fprintf(OutFile, "@line\n"); fprintf(OutFile, "%d @hsize\n", YSIZE); fprintf(OutFile, "%d @vsize\n", XSIZE); fprintf(OutFile, "%d @hoffset\n", YOFFSET); fprintf(OutFile, "%d @voffset\n", XOFFSET); fprintf(OutFile, "%d @hscale\n", YSCALE); fprintf(OutFile, "%d @vscale\n", XSCALE); fprintf(OutFile, "%d @lscale\n", LINESCALE); fprintf(OutFile, "@SetPlot\n\n"); fprintf(OutFile, "bop\n"); } /* Sets the printer to text mode */ void pstext() { } /* Sets the printer to graphics mode */ void psgraph() { } /* Form feed */ void psclear() { fprintf(OutFile," S\neop\nbop\n"); } static int xlast, ylast; void pspage() { numpages++; fprintf(OutFile, "%%%%Page: %d %d\n", numpages, numpages); linepos = 0; xlast = -100000; ylast = -100000; } void pseop() { fprintf(OutFile," S\neop\nbop\n"); fflush(OutFile); } /* May put something here someday */ void pscolor(colour) int colour; { } void pswidth(width) int width; { if(width<1 || width>10) fprintf(stderr,"\nInvalid pen width selection."); else fprintf(OutFile, " S\n/lw %d def\n@lwidth\n",width); } /* define coordinates of bounding box */ static int llx=XPSSIZE, lly=YPSSIZE, urx=0, ury=0; void psline(x1,y1,x2,y2) int x1,y1,x2,y2; { static int ptcnt; if (linepos + 21 > LINELENGTH) { putc('\n', OutFile); linepos = 0; } else putc(' ', OutFile); if(!orient) { if(x1 == xlast && y1 == ylast && ptcnt < 40 ) { fprintf(OutFile, "%ld %ld D", PSX-y2, x2); ptcnt++; } else { fprintf(OutFile, "Z %ld %ld M %ld %ld D", PSX-y1, x1, PSX-y2, x2); llx = MIN(llx,PSX-y1); lly = MIN(lly,x1); urx = MAX(urx,PSX-y1); ury = MAX(ury,x1); ptcnt = 1; } llx = MIN(llx,PSX-y2); lly = MIN(lly,x2); urx = MAX(urx,PSX-y2); ury = MAX(ury,x2); } else { if(x1 == xlast && y1 == ylast && ptcnt < 40 ) { fprintf(OutFile, "%ld %ld D", x2, y2); ptcnt++; } else { fprintf(OutFile, "Z %ld %ld M %ld %ld D", x1, y1, x2, y2); llx = MIN(llx,x1); lly = MIN(lly,y1); urx = MAX(urx,x1); ury = MAX(ury,y1); ptcnt = 1; } llx = MIN(llx,x2); lly = MIN(lly,y2); urx = MAX(urx,x2); ury = MAX(ury,y2); } xlast = x2; ylast = y2; linepos += 21; } /* Close graphics file */ void pstidy() { fprintf(OutFile," S\neop\n"); fprintf(OutFile, "@end\n\n"); fprintf(OutFile, "%%%%Trailer\n"); llx /= ENLARGE; lly /= ENLARGE; urx /= ENLARGE; ury /= ENLARGE; llx += XOFFSET; lly += YOFFSET; urx += XOFFSET; ury += YOFFSET; fprintf(OutFile, "%%%%BoundingBox: %d %d %d %d\n",llx,lly,urx,ury); fprintf(OutFile, "%%%%Pages: %d\n", numpages); if(fileopen) fclose(OutFile); numpages = 0; linepos = 0; OutFile=NULL; orient=1; } void pscwin() { } void psgwin() { } /* Get the date and time */ static char *getdate() { int len; long t, time(); char *p, *ctime(); t = time((long *) 0); p = ctime(&t); len = strlen(p); *(p + len - 1) = '\0'; /* zap the newline character */ return p; }
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.