home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / t / tel2305s.zip / RG / RGEP.C < prev    next >
C/C++ Source or Header  |  1992-02-26  |  7KB  |  369 lines

  1. /*
  2. *
  3. *  rgep.c by Aaron Contorer for NCSA
  4. *
  5. *  graphics routines for drawing on Epson printer
  6. *  Input coordinate space = 0..4095 by 0..4095
  7. *  MUST BE COMPILED WITH LARGE MEMORY MODEL!
  8. *
  9. */
  10.  
  11. #include <stdio.h>    /* used for debugging only */
  12. #include <stdlib.h>
  13. #ifdef MSC
  14. #ifdef __TURBOC__
  15. #include <alloc.h>
  16. #else
  17. #include <malloc.h>
  18. #endif
  19. #endif
  20. #include "externs.h"
  21.  
  22. #define TRUE 1
  23. #define FALSE 0
  24. #define INXMAX 4096
  25. #define INYMAX 4096
  26. #define SCRNXHI 719
  27. #define ROWS 90
  28. #define SCRNYHI 929
  29. #define LINEBYTE 930
  30. #define MAXRW 1        /* max. number of real windows */
  31.  
  32. static int EPSactive; /* number of currently active window */
  33. static unsigned char EPSpbit[SCRNXHI+1];
  34. #ifdef OLD_WAY
  35. static unsigned char power2[8]={1,2,4,8,16,32,64,128};
  36. #endif
  37. static char *EPSname="Epson, IBM, or compatible printer";
  38. static int EPSxmax=720;
  39. static int EPSymax=LINEBYTE;
  40. #ifdef OLD_WAY
  41. static int EPSbytes=80;    /* number of bytes per line */
  42. #endif
  43. static unsigned char *EPSram[ROWS];
  44. static void (*EPSoutfunc)(char c);
  45. static void signore(char c);
  46. static void EPSpoint(int x,int y);
  47. static void EPSdump(void );
  48. static void EPSsetup(void );
  49.  
  50. /* Current status of an EPS window */
  51. struct EPSWIN 
  52. {
  53.     char inuse; /* true if window in use */
  54.     int pencolor, rotation, size;
  55.     int winbot,winleft,wintall,winwide;
  56.         /* position of the window in virtual space */
  57. };
  58.  
  59. static struct EPSWIN EPSwins[MAXRW];
  60.  
  61. /* prepare variables for use in other functions */
  62. static void EPSsetup(void )
  63. {
  64.     int x;
  65.     EPSpbit[0]=128; 
  66.     EPSpbit[1]=64; 
  67.     EPSpbit[2]=32; 
  68.     EPSpbit[3]=16;
  69.     EPSpbit[4]=8;   
  70.     EPSpbit[5]=4;  
  71.     EPSpbit[6]=2;  
  72.     EPSpbit[7]=1;
  73.     for(x=8; x<=SCRNXHI; x++) 
  74.         EPSpbit[x]=EPSpbit[x&7];
  75. }
  76.  
  77. /* go into EPS graphics mode */
  78. void RGEPgmode(void )
  79. {
  80. }
  81.  
  82. /* go into EPS text mode */
  83. void RGEPtmode(void )
  84. {
  85. }
  86.  
  87. /*
  88.     Clear the screen.
  89. */
  90. void RGEPclrscr(int w)
  91. {
  92.     if(w==EPSactive)
  93.         EPSsetup();
  94. }
  95.  
  96. /*
  97.     Set up a new window; return its number.
  98.     Returns -1 if cannot create window.
  99. */
  100. int RGEPnewwin(void )
  101. {
  102.     int w=0;
  103.     int x,y;
  104.  
  105.     for(x=0; x<ROWS; x++) {
  106.         if((EPSram[x]=malloc(LINEBYTE))==NULL) {    /* ran out of memory, free what we have now */
  107.             for(; x>=0; x--)
  108.                 free(EPSram[x]);
  109.             return(-1);
  110.           }    /* end if */
  111.         for(y=0; y<LINEBYTE; y++) 
  112.             (EPSram[x])[y]=0;
  113.       }
  114.     while(w<MAXRW&&EPSwins[w].inuse) 
  115.         w++;
  116.     if(w==MAXRW) 
  117.         return(-1); /* no windows available */
  118.     EPSwins[w].pencolor=7;
  119.     EPSwins[w].winbot=0;
  120.     EPSwins[w].wintall=3120;
  121.     EPSwins[w].winleft=0;
  122.     EPSwins[w].winwide=4096;
  123.     EPSwins[w].inuse=TRUE;
  124.     return(w);
  125. }
  126.  
  127. void RGEPclose(int w)
  128. {
  129.     if(EPSactive==w) {
  130.         RGEPclrscr(w);
  131.         EPSactive=-1;
  132.       }
  133.     EPSdump();
  134.     EPSwins[w].inuse=FALSE;
  135. }
  136.  
  137. /* set pixel at location (x,y) -- no range checking performed */
  138. void RGEPpoint(int w,int x,int y)
  139. {
  140.     int x2,y2;             /* on-screen coordinates */
  141.  
  142.     if(w==EPSactive) {
  143.         x2=(int)((long)x*EPSxmax/INXMAX);
  144.         y2=(int)((long)y*EPSymax/INYMAX);
  145.         EPSpoint(x2,y2);
  146.       }
  147. }  
  148.  
  149. /*
  150.     Do whatever has to be done when the drawing is all done.
  151.     (For printers, that means eject page.)
  152. */
  153. void RGEPpagedone(int w)
  154. {
  155.     EPSdump();
  156.     w=w;
  157. }
  158.  
  159. /*
  160.     Copy 'count' bytes of data to screen starting at current
  161.     cursor location.
  162. */
  163. void RGEPdataline(int w,char *data,int count)
  164. {
  165.     /* Function not supported yet. */
  166.     w=w;
  167.     data=data;
  168.     count=count;
  169. }
  170.  
  171. /*
  172.     Change pen color to the specified color.
  173. */
  174. void RGEPpencolor(int w,int color)
  175. {
  176.     /* Function not supported. */
  177.     w=w;
  178.     color=color;
  179. }
  180.  
  181. /*
  182.     Set description of future device-supported graphtext.
  183.     Rotation=quadrant.
  184. */
  185. void RGEPcharmode(int w,int rotation,int size)
  186. {
  187.     /* No rotatable device-supported graphtext is available on EPS. */
  188.     w=w;
  189.     rotation=rotation;
  190.     size=size;
  191. }
  192.  
  193. /* Not yet supported: */
  194. void RGEPshowcur(void) {}
  195. void RGEPlockcur(void) {}
  196. void RGEPhidecur(void) {}
  197.  
  198. /*
  199.     Set bit at x,y
  200. */
  201. static void EPSpoint(int x,int y)
  202. {
  203.     (EPSram[x>>3])[y]|=EPSpbit[x];
  204. }
  205.  
  206. /* draw a line from (x0,y0) to (x1,y1) */
  207. /* uses Bresenham's Line Algorithm */
  208. void RGEPdrawline(int w,int x0,int y0,int x1,int y1)
  209. {
  210.     int x,y,dx,dy,d,temp,
  211.     dx2,dy2,        /* 2dx and 2dy */
  212.     direction;        /* +1 or -1, used for slope */
  213.     char transpose;    /* true if switching x and y for vertical-ish line */
  214.  
  215.     if(w!=EPSactive) 
  216.         return;
  217.     x0=(int)((long)x0*EPSxmax/INXMAX);
  218.     y0=(int)((long)y0*EPSymax/INYMAX);
  219.     x1=(int)((long)x1*EPSxmax/INXMAX);
  220.     y1=(int)((long)y1*EPSymax/INYMAX);
  221.  
  222.     if(abs(y1-y0)>abs(x1-x0)) {            /* transpose vertical-ish to horizontal-ish */
  223.         temp=x1; 
  224.         x1=y1; 
  225.         y1=temp;
  226.         temp=x0; 
  227.         x0=y0; 
  228.         y0=temp;
  229.         transpose=TRUE;
  230.       } 
  231.     else 
  232.         transpose=FALSE;            /* make sure line is left to right */
  233.     if(x1<x0) {
  234.         temp=x1; 
  235.         x1=x0; 
  236.         x0=temp; 
  237.         temp=y1; 
  238.         y1=y0; 
  239.         y0=temp;
  240.       }
  241.                                     /* SPECIAL CASE: 1 POINT */
  242.     if(x1==x0&&y1==y0) { 
  243.         EPSpoint(x1,y1);
  244.         return;
  245.       }        
  246.                                     /* ANY LINE > 1 POINT */
  247.     x=x0;
  248.     y=y0;
  249.     dx=x1-x0;
  250.     if(y1>=y0) {
  251.         dy=y1-y0;
  252.         direction=1;
  253.       } 
  254.     else {
  255.         dy=y0-y1;
  256.         direction=-1;
  257.       }
  258.     dx2=dx<<1;
  259.     dy2=dy<<1;
  260.     d=(dy<<1)-dx;
  261.     if(transpose) {        /* CASE OF VERTICALISH (TRANSPOSED) LINES */
  262.         while(x<=x1) {
  263.             if(y>=0&&y<EPSxmax&&x>=0&&x<EPSymax)
  264.                 EPSpoint(y,x);
  265.             while(d>=0) {
  266.                 y+=direction;
  267.                 d-=dx2;
  268.               }
  269.             d+=dy2;
  270.             x++;
  271.           } 
  272.       } 
  273.     else {                /* CASE OF HORIZONTALISH LINES */
  274.         while(x<=x1) {
  275.             if(x>=0&&x<EPSxmax&&y>=0&&y<EPSymax)
  276.                 EPSpoint(x,y);
  277.             while(d>=0){
  278.                 y+=direction;
  279.                 d-=dx2;
  280.               }
  281.             d+=dy2;
  282.             x++;
  283.           }
  284.       }                 /* end horizontalish */
  285. }   /* end RGEPdrawline() */
  286.  
  287. /* Ring bell in window w */
  288. void RGEPbell(int w)
  289. {
  290.     w=w;
  291. }
  292.  
  293. /* return name of device that this RG supports */
  294. char *RGEPdevname(void )
  295. {
  296.     return(EPSname);
  297. }
  298.  
  299. /* Ignore the character pointer passed here. */
  300. static void signore(char s)
  301. {
  302.     s=s;
  303. }
  304.  
  305. /* initialize all RGEP variables */
  306. void RGEPinit(void )
  307. {
  308.     int i;
  309.     EPSsetup();
  310.     for(i=0; i<MAXRW; i++) 
  311.         EPSwins[i].inuse=FALSE;
  312.     EPSactive=-1;
  313.     EPSoutfunc=signore;
  314. }
  315.  
  316. /*
  317.     Specify the function that is to be called with pointers to all
  318.     the printout strings.
  319. */
  320. void RGEPoutfunc(void (*f)(char ))
  321. {
  322.     EPSoutfunc=f;
  323. }
  324.  
  325. /*
  326.     Make this window visible, hiding all others.
  327.     Caller should follow this with clrscr and redraw to show the current
  328.     contents of the window.
  329. */
  330. void RGEPuncover(int w)
  331. {
  332.     EPSactive=w;
  333. }
  334.  
  335. /* Needed for possible future functionality */
  336. void RGEPinfo(int w,int a,int b,int c,int d,int v)
  337. {
  338.     w=w;
  339.     a=a;
  340.     b=b;
  341.     c=c;
  342.     d=d;
  343.     v=v;
  344. }
  345.  
  346. /*
  347. *    Dump the contents of the buffer to the specified function.
  348. */
  349. static void EPSdump(void )
  350. {
  351.     int x,y;
  352.     int size=EPSymax*2;
  353.  
  354.     for(x=0;x<90;x++) {                /* init graphics mode */
  355.         (*EPSoutfunc)(033);
  356.         (*EPSoutfunc)('L');
  357.         (*EPSoutfunc)((char)(size&255));
  358.         (*EPSoutfunc)((char)(size>>8));        /* send a line of bit image data */
  359.         for(y=0; y<size; y++) 
  360.             (*EPSoutfunc)((EPSram[x]) [y]);
  361.                                     /* go to next line */
  362.         (*EPSoutfunc)(13);
  363.         (*EPSoutfunc)(033);
  364.         (*EPSoutfunc)('J');
  365.         (*EPSoutfunc)(24);
  366.       }
  367.     (*EPSoutfunc)(12); /* form feed */
  368. }
  369.