home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / VOGLE.ZIP / VOGLE / DRIVERS / UNIXPLOT.C < prev    next >
C/C++ Source or Header  |  2000-02-11  |  4KB  |  293 lines

  1. /*
  2. **    VOGLE Driver for UNIX "plot" format output
  3. */
  4.  
  5. #include <stdio.h>
  6. #include "vogle.h"
  7.  
  8. #define SPACE_SIZE    1024
  9. #define    POINT(x, y)    (0x10000 * (y) + (x))
  10. #define MAXCOLOR    7
  11.  
  12. extern FILE    *_voutfile();
  13.  
  14. static int    uplot_first_time = 1, drawn = 0,
  15.         curcol = 0,            /* black */
  16.         uplotlstx = -1, uplotlsty = -1;    /* last (x, y) drawn */
  17.  
  18. #undef MAP_TO_LINESTYLES    /* This works, but doesn't look great on text */
  19.  
  20. /*
  21.  * Line style map for our standard colours
  22.  */
  23. static char    *colormap[MAXCOLOR + 1] = {
  24.     "solid",
  25.     "dotted",
  26.     "shortdashed",
  27.     "longdashed",
  28.     "dotdashed",
  29.     "solid",
  30.     "solid",
  31.     "solid"
  32. };
  33.  
  34. static FILE    *fp;
  35.  
  36. /*
  37.  * noop
  38.  *
  39.  *    do nothing but return -1
  40.  */
  41. static int
  42. noop()
  43. {
  44.     return(-1);
  45. }
  46.  
  47. /*
  48.  * putpt
  49.  *
  50.  *    Put a point out to the file.  Two two-byte values, little-endian.
  51.  *    NOTE:  This assumes 8-bit chars and 16-bit shorts.
  52.  */
  53. static void
  54. putpt(x, y, fp)
  55. int x;
  56. int y;
  57. FILE *fp;
  58. {
  59.     short sx, sy;
  60.  
  61.     sx = (short) x;
  62.     sy = (short) y;
  63.  
  64.     putc((sx & 0xff), fp);
  65.     putc(((sx >> 8) & 0xff), fp);
  66.     putc((sy & 0xff), fp);
  67.     putc(((sy >> 8) & 0xff), fp);
  68. }
  69.  
  70. /*
  71.  * uplot_init
  72.  *
  73.  *    Set up the unixplot environment. Returns 1 on success.
  74.  */
  75. static
  76. uplot_init()
  77. {
  78.     fp = _voutfile();
  79.  
  80.     if (!uplot_first_time)
  81.         return(1);
  82.  
  83.     putc('s', fp);
  84.     putpt(0, 0, fp);
  85.     putpt(SPACE_SIZE, SPACE_SIZE, fp);
  86.  
  87.     vdevice.sizeSx = vdevice.sizeSy = SPACE_SIZE; 
  88.     vdevice.sizeX = vdevice.sizeY = SPACE_SIZE; 
  89.  
  90.     vdevice.depth = 1;
  91.  
  92.     return(1);
  93. }
  94.  
  95.  
  96. /*
  97.  * uplot_exit
  98.  *
  99.  *    Flush remaining data and close the output file if neccessary.
  100.  */
  101. static
  102. uplot_exit()
  103. {
  104.     fflush(fp);
  105.     if (fp != stdout)
  106.     {
  107.         fclose(fp);
  108.     }
  109. }
  110.  
  111. /*
  112.  * uplot_draw
  113.  *
  114.  *    draw to an x, y point.
  115.  */
  116. static
  117. uplot_draw(x, y)
  118.     int    x, y;
  119. {
  120.     if (uplotlstx != vdevice.cpVx || uplotlsty != vdevice.cpVy)
  121.     {
  122.         putc('m', fp);
  123.         putpt(vdevice.cpVx, vdevice.cpVy, fp);
  124.     }
  125.  
  126.     putc('n', fp);
  127.     putpt(x, y, fp);
  128.     uplotlstx = x;
  129.     uplotlsty = y;
  130.     drawn = 1;
  131. }
  132.  
  133. /*
  134.  * uplot_font
  135.  *
  136.  *    There's not much we can do for this.  We can't even know
  137.  *    the width or height!
  138.  */
  139. static
  140. uplot_font(font)
  141.     char    *font;
  142. {
  143.     if (strcmp(font, "large") == 0 || strcmp(font, "small") == 0)
  144.     {
  145.         vdevice.hwidth = 1.0;
  146.         vdevice.hheight = 1.0;
  147.         return(1);
  148.     }
  149.     else
  150.     {
  151.         return(0);
  152.     }
  153. }
  154.  
  155. /*
  156.  * uplot_clear
  157.  *
  158.  *    Erase the plot
  159.  */
  160. static
  161. uplot_clear()
  162. {
  163.     if (drawn)
  164.     {
  165.         putc('e', fp);
  166.     }
  167.  
  168.     drawn = 0;
  169. }
  170.  
  171. /*
  172.  * uplot_color
  173.  *
  174.  *    Change the linestyle of the lines
  175.  */
  176. static
  177. uplot_color(col)
  178.     int    col;
  179. {
  180.     if (col > MAXCOLOR)
  181.         return;
  182.  
  183.     curcol = col;
  184.  
  185. #ifdef MAP_TO_LINESTYLES
  186.     fprintf(fp, "f%s\n", colormap[curcol]);
  187. #endif
  188. }
  189.     
  190. /*
  191.  * uplot_char
  192.  *
  193.  *    Output a character
  194.  */
  195. static
  196. uplot_char(c)
  197.     char    c;
  198. {
  199.     if (uplotlstx != vdevice.cpVx || uplotlsty != vdevice.cpVy)
  200.     {
  201.         putc('m', fp);
  202.         putpt(vdevice.cpVx, vdevice.cpVy, fp);
  203.     }
  204.  
  205.     fprintf(fp, "t%c\n", c);
  206.  
  207.     drawn = 1;
  208.     uplotlstx = uplotlsty = -1;
  209. }
  210.  
  211. /*
  212.  * uplot_string
  213.  *
  214.  *    output a string one char at a time.
  215.  */
  216. static
  217. uplot_string(s)
  218.     char    *s;
  219. {
  220.     if (uplotlstx != vdevice.cpVx || uplotlsty != vdevice.cpVy)
  221.     {
  222.         putc('m', fp);
  223.         putpt(vdevice.cpVx, vdevice.cpVy, fp);
  224.     }
  225.  
  226.     fprintf(fp, "t%s\n", s);
  227.  
  228.     drawn = 1;
  229.     uplotlstx = uplotlsty = -1;
  230. }
  231.  
  232. /*
  233.  * uplot_fill
  234.  *
  235.  *    Should do a fill, but we can't so just draw the polygon
  236.  */
  237. static
  238. uplot_fill(n, x, y)
  239.     int     n, x[], y[];
  240. {
  241.     int     i;
  242.  
  243.     putc('m', fp);
  244.     putpt(x[0], y[0], fp);
  245.  
  246.     for (i = 1; i < n; i++)
  247.     {
  248.         putc('n', fp);
  249.         putpt(x[i], y[i], fp);
  250.     }
  251.     putc('n', fp);
  252.     putpt(x[0], y[0], fp);
  253.  
  254.     vdevice.cpVx = x[n - 1];
  255.     vdevice.cpVy = y[n - 1];
  256.  
  257.     uplotlstx = uplotlsty = -1;
  258. }
  259.  
  260. static DevEntry uplotdev = {
  261.     "unixplot",
  262.     "large",
  263.     "small",
  264.     noop,
  265.     uplot_char,
  266.     noop,
  267.     uplot_clear,
  268.     uplot_color,
  269.     uplot_draw,
  270.     uplot_exit,
  271.     uplot_fill,
  272.     uplot_font,
  273.     noop,
  274.     noop,
  275.     uplot_init,
  276.     noop,
  277.     noop,
  278.     noop,
  279.     uplot_string,
  280.     noop,
  281.     noop
  282. };
  283.  
  284. /*
  285.  * _unixplot_devcpy
  286.  *
  287.  *    copy the unixplot device into vdevice.dev.
  288.  */
  289. _unixplot_devcpy()
  290. {
  291.     vdevice.dev = uplotdev;
  292. }
  293.