home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / maths / plplot / plplot_2 / drivers / xfig.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-19  |  6.8 KB  |  294 lines

  1. /* $Id: xfig.c,v 1.15 1994/07/19 22:30:30 mjl Exp $
  2.  * $Log: xfig.c,v $
  3.  * Revision 1.15  1994/07/19  22:30:30  mjl
  4.  * All device drivers: enabling macro renamed to PLD_<driver>, where <driver>
  5.  * is xwin, ps, etc.  See plDevs.h for more detail.
  6.  *
  7.  * Revision 1.14  1994/03/23  06:34:34  mjl
  8.  * All drivers: cleaned up by eliminating extraneous includes (stdio.h and
  9.  * stdlib.h now included automatically by plplotP.h), extraneous clears
  10.  * of pls->fileset, pls->page, and pls->OutFile = NULL (now handled in
  11.  * driver interface or driver initialization as appropriate).  Special
  12.  * handling for malloc includes eliminated (no longer needed) and malloc
  13.  * prototypes fixed as necessary.
  14.  *
  15.  * Revision 1.13  1993/12/06  22:37:31  mjl
  16.  * Fixed zero line width bug (thanks to Paul Kirschner)
  17.  *
  18.  * Revision 1.12  1993/07/31  07:56:46  mjl
  19.  * Several driver functions consolidated, for all drivers.  The width and color
  20.  * commands are now part of a more general "state" command.  The text and
  21.  * graph commands used for switching between modes is now handled by the
  22.  * escape function (very few drivers require it).  The device-specific PLDev
  23.  * structure is now malloc'ed for each driver that requires it, and freed when
  24.  * the stream is terminated.
  25. */
  26.  
  27. /*    xfig.c
  28.  
  29.     PLPLOT xfig device driver.
  30. */
  31. #include "plDevs.h"
  32.  
  33. #ifdef PLD_xfig
  34.  
  35. #include "plplotP.h"
  36. #include "drivers.h"
  37.  
  38. /* Function prototypes */
  39. /* INDENT OFF */
  40.  
  41. static void flushbuffer(PLStream *);
  42.  
  43. /* top level declarations */
  44.  
  45. #define FIGX    599
  46. #define FIGY    599
  47. #define DPI    80
  48. #define BSIZE    25
  49.  
  50. static short *buffptr, bufflen;
  51. static short count;
  52. static int curwid = 1;
  53. static int firstline = 1;
  54.  
  55. /* INDENT ON */
  56. /*----------------------------------------------------------------------*\
  57. * plD_init_xfig()
  58. *
  59. * Initialize device.
  60. \*----------------------------------------------------------------------*/
  61.  
  62. void
  63. plD_init_xfig(PLStream *pls)
  64. {
  65.     PLDev *dev;
  66.  
  67.     pls->termin = 0;        /* not an interactive terminal */
  68.     pls->icol0 = 1;
  69.     pls->color = 0;
  70.     pls->width = 1;
  71.     pls->bytecnt = 0;
  72.     pls->page = 0;
  73.  
  74. /* Initialize family file info */
  75.  
  76.     plFamInit(pls);
  77.  
  78. /* Prompt for a file name if not already set */
  79.  
  80.     plOpenFile(pls);
  81.  
  82. /* Allocate and initialize device-specific data */
  83.  
  84.     dev = plAllocDev(pls);
  85.  
  86.     dev->xold = UNDEFINED;
  87.     dev->yold = UNDEFINED;
  88.     dev->xmin = 0;
  89.     dev->xmax = FIGX;
  90.     dev->ymin = 0;
  91.     dev->ymax = FIGY;
  92.  
  93.     plP_setpxl(3.1496, 3.1496);    /* 80 DPI */
  94.  
  95.     plP_setphy(0, FIGX, 0, FIGY);
  96.  
  97.     /* Write out header */
  98.  
  99.     fprintf(pls->OutFile, "#FIG 1.4X\n");
  100.     fprintf(pls->OutFile, "%d 2\n", DPI);
  101.  
  102.     bufflen = 2 * BSIZE;
  103.     buffptr = (short *) malloc(sizeof(short) * bufflen);
  104.     if (buffptr == NULL)
  105.     plexit("Out of memory!");
  106. }
  107.  
  108. /*----------------------------------------------------------------------*\
  109. * plD_line_xfig()
  110. *
  111. * Draw a line in the current color from (x1,y1) to (x2,y2).
  112. \*----------------------------------------------------------------------*/
  113.  
  114. void
  115. plD_line_xfig(PLStream *pls, short x1a, short y1a, short x2a, short y2a)
  116. {
  117.     PLDev *dev = (PLDev *) pls->dev;
  118.     int x1 = x1a, y1 = y1a, x2 = x2a, y2 = y2a;
  119.     short *tempptr;
  120.  
  121.     /* If starting point of this line is the same as the ending point of */
  122.     /* the previous line then don't raise the pen. (This really speeds up */
  123.     /* plotting and reduces the size of the file. */
  124.  
  125.     if (firstline) {
  126.     count = 0;
  127.     *(buffptr + count++) = x1;
  128.     *(buffptr + count++) = y1;
  129.     *(buffptr + count++) = x2;
  130.     *(buffptr + count++) = y2;
  131.     firstline = 0;
  132.     }
  133.     else if (x1 == dev->xold && y1 == dev->yold) {
  134.     if (count + 2 >= bufflen) {
  135.         bufflen += 2 * BSIZE;
  136.         tempptr = (short *)
  137.         realloc((void *) buffptr, bufflen * sizeof(short));
  138.         if (tempptr == NULL) {
  139.         free((void *) buffptr);
  140.         plexit("Out of memory!");
  141.         }
  142.         buffptr = tempptr;
  143.     }
  144.     *(buffptr + count++) = x2;
  145.     *(buffptr + count++) = y2;
  146.     }
  147.     else {
  148.     flushbuffer(pls);
  149.     *(buffptr + count++) = x1;
  150.     *(buffptr + count++) = y1;
  151.     *(buffptr + count++) = x2;
  152.     *(buffptr + count++) = y2;
  153.     }
  154.     dev->xold = x2;
  155.     dev->yold = y2;
  156. }
  157.  
  158. /*----------------------------------------------------------------------*\
  159. * plD_polyline_xfig()
  160. *
  161. * Draw a polyline in the current color.
  162. \*----------------------------------------------------------------------*/
  163.  
  164. void
  165. plD_polyline_xfig(PLStream *pls, short *xa, short *ya, PLINT npts)
  166. {
  167.     PLINT i;
  168.  
  169.     for (i = 0; i < npts - 1; i++)
  170.     plD_line_xfig(pls, xa[i], ya[i], xa[i + 1], ya[i + 1]);
  171. }
  172.  
  173. /*----------------------------------------------------------------------*\
  174. * plD_eop_xfig()
  175. *
  176. * End of page.
  177. \*----------------------------------------------------------------------*/
  178.  
  179. void
  180. plD_eop_xfig(PLStream *pls)
  181. {
  182.     if (!firstline)
  183.     flushbuffer(pls);
  184. }
  185.  
  186. /*----------------------------------------------------------------------*\
  187. * plD_bop_xfig()
  188. *
  189. * Set up for the next page.
  190. * Advance to next family file if necessary (file output).
  191. \*----------------------------------------------------------------------*/
  192.  
  193. void
  194. plD_bop_xfig(PLStream *pls)
  195. {
  196.     PLDev *dev = (PLDev *) pls->dev;
  197.  
  198.     dev->xold = UNDEFINED;
  199.     dev->yold = UNDEFINED;
  200.     firstline = 1;
  201.  
  202.     if (!pls->termin)
  203.     plGetFam(pls);
  204.  
  205.     pls->page++;
  206. }
  207.  
  208. /*----------------------------------------------------------------------*\
  209. * plD_tidy_xfig()
  210. *
  211. * Close graphics file or otherwise clean up.
  212. \*----------------------------------------------------------------------*/
  213.  
  214. void
  215. plD_tidy_xfig(PLStream *pls)
  216. {
  217.     flushbuffer(pls);
  218.     free((void *) buffptr);
  219.     fclose(pls->OutFile);
  220. }
  221.  
  222. /*----------------------------------------------------------------------*\
  223. * plD_state_xfig()
  224. *
  225. * Handle change in PLStream state (color, pen width, fill attribute, etc).
  226. \*----------------------------------------------------------------------*/
  227.  
  228. void 
  229. plD_state_xfig(PLStream *pls, PLINT op)
  230. {
  231.     switch (op) {
  232.  
  233.     case PLSTATE_WIDTH:
  234.     flushbuffer(pls);
  235.     firstline = 1;
  236.  
  237.     if (pls->width <= 1)
  238.         curwid = 1;
  239.     else if (pls->width >= 4)
  240.         curwid = 3;
  241.     else
  242.         curwid = (int) pls->width;
  243.  
  244.     break;
  245.  
  246.     case PLSTATE_COLOR0:
  247.     break;
  248.  
  249.     case PLSTATE_COLOR1:
  250.     break;
  251.     }
  252. }
  253.  
  254. /*----------------------------------------------------------------------*\
  255. * plD_esc_xfig()
  256. *
  257. * Escape function.
  258. \*----------------------------------------------------------------------*/
  259.  
  260. void
  261. plD_esc_xfig(PLStream *pls, PLINT op, void *ptr)
  262. {
  263. }
  264.  
  265. /*----------------------------------------------------------------------*\
  266. * Utility functions.
  267. \*----------------------------------------------------------------------*/
  268.  
  269. static void
  270. flushbuffer(PLStream *pls)
  271. {
  272.     short i = 0;
  273.  
  274.     if (count == 0)
  275.     return;
  276.     fprintf(pls->OutFile, "2 1 0 %d 0 0 0 0 0.000 0 0\n", curwid);
  277.     while (i < count) {
  278.     fprintf(pls->OutFile, "%d %d ", *(buffptr + i),
  279.         FIGY - *(buffptr + i + 1));
  280.     i += 2;
  281.     }
  282.     fprintf(pls->OutFile, "9999 9999\n");
  283.     count = 0;
  284. }
  285.  
  286. #else
  287. int 
  288. pldummy_xfig()
  289. {
  290.     return 0;
  291. }
  292.  
  293. #endif                /* PLD_xfig */
  294.