home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / prog_c / plplot.lzh / PLPLOT / PLPLOT.LZH / plplot / unix / postscript.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-23  |  9.3 KB  |  297 lines

  1. /* This file contains the PostScript device dependent subroutines for */
  2. /* use with plplot. */
  3.  
  4. #include "plplot.h"
  5. #include <stdio.h>
  6.  
  7. #define LINELENGTH      70
  8. #define COPIES          1
  9. #define XSIZE           540
  10. #define YSIZE           720
  11. #define XPSSIZE         3000
  12. #define YPSSIZE         2250
  13. #define XOFFSET         576
  14. #define YOFFSET         36
  15. #define XSCALE          100
  16. #define YSCALE          100
  17. #define LINESCALE       100
  18. #define ANGLE           90
  19. #define PSX             XPSSIZE-1
  20. #define PSY             YPSSIZE-1
  21.  
  22. char *getdate();
  23. static int numpages = 0;
  24. static int linepos = 0;
  25.  
  26. static FILE *OutFile;
  27. static PLINT orient;
  28. static PLINT select=0;
  29. static char FileName[80];
  30.  
  31. void pssetup(xdpi, ydpi, xwid, ywid)
  32. PLINT xwid, ywid;
  33. PLFLT xdpi, ydpi;
  34. {
  35. }
  36.  
  37. void psselect(ori, name)
  38. PLINT ori;
  39. char *name;
  40. {
  41.    orient = ori;
  42.    strncpy(FileName,name,sizeof(FileName)-1);
  43.    FileName[sizeof(FileName)-1] = '\0';
  44.    select = 1;
  45. }
  46.  
  47.  
  48. /* Open file.  Set up for graphics. */
  49. void psinit()
  50. {
  51.       char line[80];
  52.  
  53.       if(!select) {
  54.          printf("Landscape or portrait orientation? (0 or 1) ");
  55.          fgets(line,sizeof(line),stdin);
  56.          if(sscanf(line,"%d",&orient) != 1)
  57.             orient = 0;
  58.       }
  59.  
  60.    /* setpxl() sets the dots/mm in the x and y directions */
  61.       setpxl(11.81,11.81);         /* 300 dpi */
  62.  
  63.    /* setphy() sets the device coordinates. These are integer */
  64.    /* values. Set up for landscape orientation (long axis of page in the */
  65.    /* x direction). Origin is in the lower left hand corner. */
  66.       if(!orient)
  67.          setphy(0,PSX,0,PSY);
  68.       else
  69.          setphy(0,PSY,0,PSX);
  70.  
  71.    /* Set default pen color using scol(color). */
  72.    /* Any default pen color can be used but a black pen is probably best. */
  73.       scol(1);
  74.  
  75.    /* Set default pen width using swid(width) */
  76.       swid(1);
  77.  
  78.    /* Set device interaction mode using smod(mode). Set mode to 0 for */
  79.    /* a noninteractive device, Unless you are writing your */
  80.    /* own Amiga screen driver mode should be 0. */
  81.       smod(0);
  82.  
  83.    /* Well that's all the information plplot needs. Let's prompt for a */
  84.    /* graphics file name. */
  85.       for(;;) {
  86.          if(!select) {
  87.             printf("Enter PostScript graphics file name. ");
  88.             fgets(line,sizeof(line),stdin);
  89.             if(sscanf(line,"%s",FileName)!=1)
  90.                continue;
  91.          }
  92.  
  93.          if (!(OutFile = fopen(FileName,"w"))) {
  94.             fprintf(stderr,"Can't open %s.\n",FileName);
  95.             select = 0;
  96.          }
  97.          else
  98.             break;
  99.       }
  100.       select = 0;
  101.  
  102.   /* Header comments into PostScript file */
  103.  
  104.       fprintf(OutFile,"%%!PS-Adobe-2.0 EPSF-2.0\n");
  105.       fprintf(OutFile,"%%%%BoundingBox: 36 36 540 720\n");
  106.       fprintf(OutFile,"%%%%Title: %s\n", FileName);
  107.       fprintf(OutFile,"%%%%Creator: PLPLOT Version 2.0\n");
  108.       fprintf(OutFile,"%%%%CreationDate: %s\n", getdate());
  109.       fprintf(OutFile,"%%%%Pages: (atend)\n");
  110.       fprintf(OutFile,"%%%%EndComments\n\n");
  111.  
  112.   /* Definitions */
  113.  
  114.       fprintf(OutFile, "/PSSave save def\n");      /* save VM state */
  115.       fprintf(OutFile, "/PSDict 200 dict def\n");  /* define a dictionary */
  116.       fprintf(OutFile, "PSDict begin\n");          /* start using it */
  117.       fprintf(OutFile, "/@restore /restore load def\n");
  118.       fprintf(OutFile, "/restore\n");
  119.       fprintf(OutFile, "   {vmstatus pop\n");
  120.       fprintf(OutFile, "    dup @VMused lt {pop @VMused} if\n");
  121.       fprintf(OutFile, "    exch pop exch @restore /@VMused exch def\n");
  122.       fprintf(OutFile, "   } def\n");
  123.       fprintf(OutFile, "/@pri\n");
  124.       fprintf(OutFile, "   {\n");
  125.       fprintf(OutFile, "    ( ) print\n");
  126.       fprintf(OutFile, "    (                                       ) cvs print\n");
  127.       fprintf(OutFile, "   } def\n");
  128.       fprintf(OutFile, "/@copies\n");   /* n @copies - */
  129.       fprintf(OutFile, "   {\n");
  130.       fprintf(OutFile, "    /#copies exch def\n");
  131.       fprintf(OutFile, "   } def\n");
  132.       fprintf(OutFile, "/@start\n");    /* - @start -  -- start everything */
  133.       fprintf(OutFile, "   {\n");
  134.       fprintf(OutFile, "    vmstatus pop /@VMused exch def pop\n");
  135.       fprintf(OutFile, "   } def\n");
  136.       fprintf(OutFile, "/@end\n");      /* - @end -  -- finished */
  137.       fprintf(OutFile, "   {(VM Used: ) print @VMused @pri\n");
  138.       fprintf(OutFile, "    (. Unused: ) print vmstatus @VMused sub @pri pop pop\n");
  139.       fprintf(OutFile, "    (\\n) print flush\n");
  140.       fprintf(OutFile, "    end\n");
  141.       fprintf(OutFile, "    PSSave restore\n");
  142.       fprintf(OutFile, "   } def\n");
  143.       fprintf(OutFile, "/bop\n");       /* bop -  -- begin a new page */
  144.       fprintf(OutFile, "   {\n");
  145.       fprintf(OutFile, "    /SaveImage save def\n");
  146.       fprintf(OutFile, "   } def\n");
  147.       fprintf(OutFile, "/eop\n");       /* - eop -  -- end a page */
  148.       fprintf(OutFile, "   {\n");
  149.       fprintf(OutFile, "    showpage\n");
  150.       fprintf(OutFile, "    SaveImage restore\n");
  151.       fprintf(OutFile, "   } def\n");
  152.       fprintf(OutFile, "/@line\n");     /* set line parameters */
  153.       fprintf(OutFile, "   {0 setlinecap\n");
  154.       fprintf(OutFile, "    0 setlinejoin\n");
  155.       fprintf(OutFile, "    1 setmiterlimit\n");
  156.       fprintf(OutFile, "   } def\n");
  157.                         /* d @hsize -  horizontal clipping dimension */
  158.       fprintf(OutFile, "/@hsize   {/hs exch def} def\n");
  159.       fprintf(OutFile, "/@vsize   {/vs exch def} def\n");
  160.                         /* d @hoffset - shift for the plots */
  161.       fprintf(OutFile, "/@hoffset {/ho exch def} def\n");
  162.       fprintf(OutFile, "/@voffset {/vo exch def} def\n");
  163.                         /* s @hscale - scale factors */
  164.       fprintf(OutFile, "/@hscale  {100 div /hsc exch def} def\n");
  165.       fprintf(OutFile, "/@vscale  {100 div /vsc exch def} def\n");
  166.                         /* s @lscale - linewidth scale factor */
  167.       fprintf(OutFile, "/@lscale  {100 div /lin exch def} def\n");
  168.                         /* a @angle - rotation angle */
  169.       fprintf(OutFile, "/@angle   {/ang exch def} def\n");
  170.       fprintf(OutFile, "/@SetPlot\n");  /* setup user specified offsets, */
  171.       fprintf(OutFile, "   {\n");       /* scales, sizes for clipping    */
  172.       fprintf(OutFile, "    ho vo translate\n");
  173.       fprintf(OutFile, "    XScale YScale scale\n");
  174.       fprintf(OutFile, "    ang rotate\n");
  175.       fprintf(OutFile, "    lin lw mul setlinewidth\n");
  176.       fprintf(OutFile, "   } def\n");
  177.       fprintf(OutFile, "/XScale\n");    /* setup x scale */
  178.       fprintf(OutFile, "   {hsc hs mul %d div} def\n", YPSSIZE);
  179.       fprintf(OutFile, "/YScale\n");    /* setup y scale */
  180.       fprintf(OutFile, "   {vsc vs mul %d div} def\n", XPSSIZE);
  181.       fprintf(OutFile, "/lw 3 def\n");  /* default line width */
  182.       fprintf(OutFile, "/M {moveto} def\n");
  183.       fprintf(OutFile, "/D {lineto} def\n");
  184.       fprintf(OutFile, "/S {stroke} def\n");
  185.       fprintf(OutFile, "/Z {stroke newpath} def\n");
  186.       fprintf(OutFile, "end\n\n");      /* end of dictionary definition */
  187.  
  188.   /* Set up the plots */
  189.  
  190.       fprintf(OutFile, "PSDict begin\n");
  191.       fprintf(OutFile, "@start\n");
  192.       fprintf(OutFile, "%d @copies\n", COPIES);
  193.       fprintf(OutFile, "@line\n");
  194.       fprintf(OutFile, "%d @hsize\n", XSIZE);
  195.       fprintf(OutFile, "%d @vsize\n", YSIZE);
  196.       fprintf(OutFile, "%d @hoffset\n", XOFFSET);
  197.       fprintf(OutFile, "%d @voffset\n", YOFFSET);
  198.       fprintf(OutFile, "%d @hscale\n", XSCALE);
  199.       fprintf(OutFile, "%d @vscale\n", YSCALE);
  200.       fprintf(OutFile, "%d @lscale\n", LINESCALE);
  201.       fprintf(OutFile, "%d @angle\n", ANGLE);
  202.       fprintf(OutFile, "@SetPlot\n\n");
  203.       fprintf(OutFile, "bop\n");
  204.       fprintf(OutFile, "%%%%Page: %d %d\n", numpages+1, numpages+1);
  205. }
  206.  
  207. /* Sets the printer to text mode */
  208. void pstext()
  209. {
  210. }
  211.  
  212. /* Sets the printer to graphics mode */
  213. void psgraph()
  214. {
  215. }
  216.  
  217. /* Form feed */
  218. void psclear()
  219. {
  220.      fprintf(OutFile," S\neop\nbop\n");
  221. }
  222.  
  223. static PLINT xlast, ylast;
  224.  
  225. void pspage()
  226. {
  227.      numpages++;
  228.      fprintf(OutFile, "%%%%Page: %d %d\n", numpages+1, numpages+1);
  229.      linepos = 0;
  230.      xlast = -100000; ylast = -100000;
  231. }
  232.  
  233. /* May put something here someday */
  234. void pscolor(colour)
  235. int colour;
  236. {
  237. }
  238.  
  239. /* I don't know what to put here - Tony */
  240. void pswidth()
  241. {
  242. }
  243.  
  244. void psline(x1,y1,x2,y2)
  245. PLINT x1,y1,x2,y2;
  246. {
  247.       if (linepos + 21 > LINELENGTH) {
  248.           putc('\n', OutFile);
  249.           linepos = 0;
  250.       } else
  251.           putc(' ', OutFile);
  252.  
  253.       if(!orient) {
  254.          if(x1 == xlast && y1 == ylast)
  255.             fprintf(OutFile, "%ld %ld D", x2, y2);
  256.          else
  257.             fprintf(OutFile, "Z %ld %ld M %ld %ld D", x1, y1, x2, y2);
  258.       }
  259.       else {
  260.          if(x1 == xlast && y1 == ylast)
  261.             fprintf(OutFile, "%ld %ld D", PSX-y2, x2);
  262.          else
  263.             fprintf(OutFile, "Z %ld %ld M %ld %ld D", PSX-y1,x1,PSX-y2,x2);
  264.       }
  265.  
  266.       xlast = x2;
  267.       ylast = y2;
  268.       linepos += 21;
  269. }
  270.  
  271. /* Close graphics file */
  272. void pstidy()
  273. {
  274.       fprintf(OutFile," S\neop\n");
  275.       numpages++;
  276.       fprintf(OutFile, "@end\n\n");
  277.       fprintf(OutFile, "%%%%Trailer\n");
  278.       fprintf(OutFile, "%%%%Pages: %d\n", numpages);
  279.       fclose(OutFile);
  280.       numpages = 0;
  281.       linepos = 0;
  282. }
  283.  
  284. /* Get the date and time */
  285. static char *getdate()
  286. {
  287.         int len;
  288.         long t, time();
  289.         char *p, *ctime();
  290.  
  291.         t = time((long *) 0);
  292.         p = ctime(&t);
  293.         len = strlen(p);
  294.         *(p + len - 1) = '\0';  /* zap the newline character */
  295.         return p;
  296. }
  297.