home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / gle / gle / d_dvi.c < prev    next >
C/C++ Source or Header  |  1992-11-29  |  13KB  |  545 lines

  1. /*--------------------------------------------------------------*/
  2. /*    Bitmap Driver 1.0 (VAX C, TURBO  C) , for GLE V3.0    */
  3. /*--------------------------------------------------------------*/
  4. /* This version writes the PATH to a temporary file which can
  5. /* then be read by a bitmap driver (which has more memory available) */
  6. /*---------------------------------------------------------------------------*/
  7. #include "all.h"
  8. #include <math.h>
  9. #include "core.h"
  10. #include "mygraph.h"
  11. #include "mydev.h"
  12. #ifndef __TURBOC__
  13. #define huge
  14. #else
  15. #include <alloc.h>
  16. #endif
  17. extern int dev_fill,dev_font;
  18. extern int gunit;
  19. extern struct gmodel g;
  20. /*---------------------------------------------------------------------------*/
  21. #define pi 3.141592653
  22. #define true (!false)
  23. #define SOLID_LINE 1
  24. #define DASHED_LINE 2
  25. #define BLACKANDWHITE 1
  26. #define false 0
  27. #define dbg if ((gle_debug & 64)>0)
  28. extern int gle_debug;
  29. int getch(void);
  30. int kbhit(void);
  31.  
  32.  
  33. int path_newpath(void);
  34. int path_lwidth(float x);
  35. int path_setcolor(float x);
  36. int path_setcolori(int x);
  37. int path_setfilli(int x);
  38. int path_stroke(void);
  39. int path_fill(void);
  40. int path_setfill(float x);
  41. int path_lstyle(int x);
  42. int path_dline(float x,float y);
  43. int path_size(float x,float y);
  44. int path_row(float x1, float y1, float x2, float y2);
  45. int path_box(float x1, float y1, float x2, float y2);
  46. int path_alloc(void);
  47. int path_move(float x1, float y1);
  48. int path_line(float x1, float y1);
  49. int flatten_bezier(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3);
  50. int path_closepath(void);
  51. int bitmap_print(void);
  52. int bitmap_free(void);
  53. int bitmap_alloc(void);
  54. int bitmap_paint(int row, int x1, int x2);
  55. int intline(int  x1, int  y1, int  x2, int  y2);
  56.  
  57.  
  58. /*---------------------------------------------------------------------------*/
  59.  
  60. #define scx(v) ( (v) )
  61. #define scy(v) ( ((v)  ))
  62. #define rx(v) ( ((v) ))
  63. #define ry(v) ( ((v) ))
  64.  
  65.  
  66. int d_graphmode;
  67. int d_fillstyle=1,d_fillcolor;
  68. int d_lstyle,d_lwidth,d_color;
  69. int d_maxy;
  70. /*---------------------------------------------------------------------------*/
  71. /*   Path variables for bitmap */
  72. typedef union {long l; float f;} longfloat;
  73. longfloat *path;
  74. int npath;
  75. int npath_alloc;
  76. #include "pathtypes.h"
  77. /* -ve is used to indicate up or down */
  78. int row_min,row_max;
  79. static int lastany = true,ffup = 3;
  80. static float ffx= -1,ffy= -1,lastx= -1,lasty= -1,cx= -1,cy= -1;
  81. path_reset()
  82. {
  83.     ffx = -1; ffy = -1; ffup = 3;
  84.     lastany = true;
  85.     lastx = -1; lasty = -1;
  86.     cx = -1; cy = -1;
  87. }
  88. path_newpath(void)
  89. {
  90.     path_alloc();
  91.     path[npath++].l = p_newpath;
  92. }
  93. path_move(float x,float y)
  94. {
  95.     path_alloc();
  96.     path[npath++].l = p_move;
  97.     path[npath++].f = x;
  98.     path[npath++].f = y;
  99. }
  100. path_lwidth(float x)
  101. {
  102.     path_alloc();
  103.     path[npath++].l = p_lwidth;
  104.     path[npath++].f = x;
  105. }
  106. path_setfilli(int x)
  107. {
  108.     path_alloc();
  109.     path[npath++].l = p_setcolori;
  110.     path[npath++].l = x;
  111. }
  112. path_setcolori(int x)
  113. {
  114.     path_alloc();
  115.     path[npath++].l = p_setcolori;
  116.     path[npath++].l = x;
  117. }
  118. path_setcolor(float x)
  119. {
  120.     path_alloc();
  121.     path[npath++].l = p_setcolor;
  122.     path[npath++].f = x;
  123. }
  124. path_stroke(void)
  125. {
  126.     path_alloc();
  127.     path[npath++].l = p_stroke;
  128. }
  129. path_fill(void)
  130. {
  131.     path_alloc();
  132.     path[npath++].l = p_fill;
  133. }
  134. path_setfill(float x)
  135. {
  136.     path_alloc();
  137.     path[npath++].l = p_setfill;
  138.     path[npath++].f = x;
  139. }
  140. path_lstyle(int x)
  141. {
  142.     path_alloc();
  143.     path[npath++].l = p_lstyle;
  144.     path[npath++].l = x;
  145. }
  146. path_dline(float x,float y)
  147. {
  148.     path_alloc();
  149.     path[npath++].l = p_dline;
  150.     path[npath++].f = x;
  151.     path[npath++].f = y;
  152. }
  153. path_size(float x,float y)
  154. {
  155.     path_alloc();
  156.     path[npath++].l = p_size;
  157.     path[npath++].f = x;
  158.     path[npath++].f = y;
  159. }
  160. path_line(float x,float y)
  161. {
  162.     path_alloc();
  163.     path[npath++].l = p_line;
  164.     path[npath++].f = x;
  165.     path[npath++].f = y;
  166. }
  167. path_bezier(float x1, float y1, float x2, float y2,float x3, float y3)
  168. {
  169.     path_alloc();
  170.     path[npath++].l = p_bezier;
  171.     path[npath++].f = x1;    path[npath++].f = y1;
  172.     path[npath++].f = x2;    path[npath++].f = y2;
  173.     path[npath++].f = x3;    path[npath++].f = y3;
  174. }
  175. path_closepath()
  176. {
  177.     path_alloc();
  178.     path[npath++].l = p_closepath;
  179. }
  180. path_box(float x1, float y1, float x2, float y2)
  181. {
  182.     path_move(x1,y1);
  183.     path_line(x2,y1);
  184.     path_line(x2,y2);
  185.     path_line(x1,y2);
  186.     path_closepath();
  187. }
  188. path_alloc(void)
  189. {
  190.     static int npa;
  191.     long *a;
  192.     if (npath > 100) path_flush();
  193.     if (npath < (npath_alloc-20)) return;
  194.     npath_alloc = 20 + 2 * npath;
  195.     a = myallocz(npath_alloc*sizeof(long));
  196.     if (a==NULL) {
  197.         gle_abort("Unable to allocate memory for path \n");
  198.     }
  199.     if (path != NULL) {
  200.         memcpy(a,path,(npa)*sizeof(long));
  201.         myfree(path);
  202.     }
  203.     npa = npath_alloc;
  204.     path = (longfloat *) a;
  205. }
  206. path_free(void)
  207. {
  208.     if (path==NULL) return;
  209.     myfree(path);
  210.     path = NULL;
  211.     npath = 0;
  212.     npath_alloc = 0;
  213. }
  214.  
  215. d_devcmd(char *s)
  216. {}
  217. dxy(double x, double y, float *dx, float *dy)
  218. {
  219.     static double fx,fy;
  220.     g_dev(x,y,&fx,&fy);
  221.     *dx = scx(fx);
  222.     *dy = scy(fy);
  223. }
  224. rxy(double x, double y, int *dx, int *dy)
  225. {
  226.     static double fx,fy,zx,zy;
  227.     g_dev(x,y,&fx,&fy);
  228.     g_dev(0.0,0.0,&zx,&zy);
  229.     *dx = (int) ( (fx-zx) );
  230.     *dy = (int) ( (fy-zy) );
  231. }
  232. /*---------------------------------------------------------------------------*/
  233. d_dfont(char *c)
  234. {
  235.     /* only used for the DFONT driver which builds fonts (never used)*/
  236. }
  237. /*---------------------------------------------------------------------------*/
  238. d_message(char *s)
  239. {
  240.     printf("%s\n",s);
  241. }
  242. /*---------------------------------------------------------------------------*/
  243. d_source(char *s)
  244. {
  245.     s=s;
  246. }
  247. /*---------------------------------------------------------------------------*/
  248. d_get_type(char *t)
  249. {
  250.     strcpy(t,"OUTPUT, BITMAP, BLACKANDWHITE, HARDCOPY, FILLPATH");
  251. }
  252. /*---------------------------------------------------------------------------*/
  253. d_set_path(int onoff)
  254. {
  255. }
  256. /*---------------------------------------------------------------------------*/
  257. d_newpath()
  258. {
  259.     path_flush();
  260.     path_newpath();
  261. }
  262. /*---------------------------------------------------------------------------*/
  263. FILE *dout;
  264. path_flush(void)
  265. {
  266.     if (npath>0) fwrite(path,sizeof(long),npath,dout);
  267.     npath = 0;
  268. }
  269. d_open(double width, double height)
  270. {
  271.     char *outfile;
  272.     outfile = "out.dvi";
  273. #ifdef ultrix
  274.     dout = fopen(outfile,"w");
  275.     if (dout == NULL) gle_abort("Unable to open OUT.DVI epson output file \n");
  276. #else
  277.     dout = fopen(outfile,"wb");
  278.     if (dout == NULL) gle_abort("Unable to open OUT.EP epson output file \n");
  279. #endif
  280.     path_alloc();
  281.     path_size(width,height);
  282.     if (!dev_font) plotter_fonts();
  283. }
  284. /*---------------------------------------------------------------------------*/
  285. d_tidyup()
  286. {
  287.     gprint("AARRRrrrrgg,  Dieing\n");
  288. }
  289. d_close()
  290. {
  291.     g_flush();
  292.     path_flush();
  293.     path_free();
  294.     fclose(dout);
  295.     printf("Now enter one of:\n");
  296.     printf("   DVIEPSON   (to print to a dot matrix printer)\n");
  297.     printf("   DVIEP24    (to print to 24 Pin epson printers)\n");
  298.     printf("   DVILJ      (to print to an HP LaserJet/DeskJet printer)\n");
  299.     printf("   DVILJ300   (same as dvilj but slower with better resolution\n");
  300.     printf("   DVIPJ      (HP Paint Jet, use DVIGLE myfile /FILL \n");
  301. }
  302. /*---------------------------------------------------------------------------*/
  303. d_set_line_cap(int i)
  304. {
  305.     i++;
  306. }
  307. /*---------------------------------------------------------------------------*/
  308. d_set_line_join(int i)
  309. {
  310.     i++;
  311. }
  312. /*---------------------------------------------------------------------------*/
  313. d_set_line_miterlimit(double d)
  314. {
  315.     int i=0;
  316.     i++;
  317. }
  318. /*---------------------------------------------------------------------------*/
  319. d_set_line_width(double w)
  320. {
  321.     float dx,dy,zx,zy;
  322.  
  323.     dxy(w,w,&dx,&dy);
  324.     dxy(0.0,0.0,&zx,&zy);
  325.     path_lwidth(sqrt(pow(dx-zx,2) + pow(dy-zy,2)));
  326.     path_lwidth(w);
  327. }
  328. /*---------------------------------------------------------------------------*/
  329. d_set_line_styled(double dd)
  330. {}
  331. d_set_line_style(char *s)
  332. {
  333.     d_lstyle = DASHED_LINE;
  334.     if (strcmp(s,"")==0) d_lstyle = SOLID_LINE;
  335.     if (strcmp(s,"1")==0) d_lstyle = SOLID_LINE;
  336.     path_lstyle(d_lstyle);
  337. }
  338. /*---------------------------------------------------------------------------*/
  339. d_fill()
  340. {
  341.     path_fill();
  342. }
  343. /*---------------------------------------------------------------------------*/
  344. d_fill_ary(int nwk,double (*wkx)[],double (*wky)[])
  345. {
  346.     int i;
  347.  
  348.     path_move( (*wkx)[0], (*wky)[0]);
  349.     for (i=1;i<nwk;i++) {
  350.         path_line( (*wkx)[i], (*wky)[i]);
  351.     }
  352.     d_fill();
  353. }
  354. d_line_ary(int nwk,double (*wkx)[],double (*wky)[])
  355. {
  356.     int i;
  357.  
  358.     path_move( (*wkx)[0], (*wky)[0]);
  359.     for (i=1;i<nwk;i++) {
  360.         path_line( (*wkx)[i], (*wky)[i]);
  361.     }
  362.     d_stroke();
  363. }
  364. /*---------------------------------------------------------------------------*/
  365. d_stroke()
  366. {
  367.     path_stroke();
  368. }
  369. /*---------------------------------------------------------------------------*/
  370. d_clip()
  371. {
  372. }
  373. /*---------------------------------------------------------------------------*/
  374. d_set_matrix(double newmat[3][3])
  375. {
  376.  
  377. }
  378. /*---------------------------------------------------------------------------*/
  379. d_move(double zx,double zy)
  380. {
  381. }
  382. /*---------------------------------------------------------------------------*/
  383. d_reverse()     /* reverse the order of stuff in the current path */
  384. {
  385. }
  386. /*---------------------------------------------------------------------------*/
  387. d_closepath()
  388. {
  389.     path_closepath();
  390. }
  391. int bit_line( double x, double y);
  392. /*---------------------------------------------------------------------------*/
  393. d_line(double zx,double zy)
  394. {
  395.     float dx,dy;
  396.     if (g.xinline==false) {
  397.         dxy(g.curx,g.cury,&dx,&dy);
  398.         path_move(dx,dy);
  399.     }
  400.     if (g.inpath==false) {
  401.         dxy(zx,zy,&dx,&dy);
  402.         path_dline(dx,dy);
  403.         return;
  404.     }
  405.     dxy(zx,zy,&dx,&dy);
  406.     path_line(dx,dy);
  407. }
  408.  
  409.  
  410. /*---------------------------------------------------------------------------*/
  411. d_clear()
  412. {
  413.     d_lstyle = SOLID_LINE;
  414.     d_lwidth = 1;
  415.  
  416. }
  417. /*---------------------------------------------------------------------------*/
  418. d_flush()
  419. {
  420. }
  421. /*---------------------------------------------------------------------------*/
  422. d_arcto(dbl x1,dbl y1,dbl x2,dbl y2,dbl rrr)
  423. {
  424.     df_arcto(x1,y1,x2,y2,rrr);
  425. }
  426. /*---------------------------------------------------------------------------*/
  427. d_arc(dbl r,dbl t1,dbl t2,dbl cx,dbl cy)
  428. {
  429.     df_arc(r,t1,t2,cx,cy);
  430. }
  431. /*---------------------------------------------------------------------------*/
  432. d_narc(dbl r,dbl t1,dbl t2,dbl cx,dbl cy)
  433. {
  434.     df_arc(r,t1,t2,cx,cy);
  435. }
  436. /*---------------------------------------------------------------------------*/
  437. d_box_fill(dbl x1, dbl y1, dbl x2, dbl y2)
  438. {
  439.     if (dev_fill) {
  440.         path_box(x1,y1,x2,y2);
  441.         d_fill();
  442.         return;
  443.     }
  444.     df_box_fill(x1,y1,x2,y2);
  445. }
  446. d_box_stroke(dbl x1, dbl y1, dbl x2, dbl y2)
  447. {
  448.     df_box_stroke(x1,y1,x2,y2);
  449. }
  450. /*---------------------------------------------------------------------------*/
  451. d_circle_stroke(double zr)
  452. {
  453.     df_circle_stroke(zr);
  454. }
  455. d_circle_fill(double zr)
  456. {
  457.     df_circle_fill(zr);
  458. }
  459. /*---------------------------------------------------------------------------*/
  460. int df_bezier(dbl x1,dbl y1,dbl x2,dbl y2,dbl x3,dbl y3);
  461. d_bezier(dbl x1,dbl y1,dbl x2,dbl y2,dbl x3,dbl y3)
  462. {
  463.     float dx1,dy1,dx2,dy2,dx3,dy3,dx0,dy0;
  464.  
  465.     if (g.inpath==false) {
  466.         df_bezier(x1,y1,x2,y2,x3,y3);
  467.         return;
  468.     }
  469.     if (g.xinline==false) {
  470.         dxy(g.curx,g.cury,&dx0,&dy0);
  471.         path_move(dx0,dy0);
  472.     }
  473.  
  474.     dxy(x1,y1,&dx1,&dy1);
  475.     dxy(x2,y2,&dx2,&dy2);
  476.     dxy(x3,y3,&dx3,&dy3);
  477.     path_bezier(dx1,dy1,dx2,dy2,dx3,dy3);
  478. }
  479. /*---------------------------------------------------------------------------*/
  480. d_set_color(long f)
  481. {
  482.     int i;
  483.     colortyp  cc;
  484.     cc.l = f;
  485.     path_setcolor(((cc.b[B_R]*3.0/255.0
  486.         +cc.b[B_G]*2.0/255.0+cc.b[B_B]/255.0) / 6));
  487.     path_setcolori(cvt_color_i(f));
  488. }
  489. cvt_color_i(long f)
  490. {
  491.     int i;
  492.     colortyp  cc;
  493.     cc.l = f;
  494.     i = 0;
  495.     if (cc.b[B_R]>1 && cc.b[B_G]>1 && cc.b[B_B]>1) i = 10; /* blue */
  496.     if (cc.b[B_R]>60 && cc.b[B_G]>60 && cc.b[B_B]>60) i = 9; /*green*/
  497.     if (cc.b[B_R]>100) i = 1;    /* red */
  498.     if (cc.b[B_G]>100) i = 2;    /* green */
  499.     if (cc.b[B_B]>100) i = 3;    /* blue */
  500.     if (cc.b[B_R]>100 && cc.b[B_G]>100) i = 4; /* yellow */
  501.     if (cc.b[B_G]>100 && cc.b[B_B]>100) i = 5; /* cyan */
  502.     if (cc.b[B_R]>100 && cc.b[B_B]>100) i = 6; /* Magenta */
  503.     if (cc.b[B_R]>100 && cc.b[B_G]>100 && cc.b[B_B]>100) i = 8;  /*red*/
  504.     if (cc.b[B_R]>250 && cc.b[B_G]>250 && cc.b[B_B]>250) i = 7;
  505.     return i;
  506. }
  507. d_set_fill(long f)
  508. {
  509.     int i;
  510.     colortyp  cc;
  511.     cc.l = f;
  512.     path_setfill(((cc.b[B_R]*3.0/255.0
  513.         +cc.b[B_G]*2.0/255.0+cc.b[B_B]/255.0) / 6));
  514.     path_setfilli(cvt_color_i(f));
  515. }
  516. /*---------------------------------------------------------------------------*/
  517. d_beginclip()
  518. {
  519. }
  520. d_endclip()
  521. {
  522. }
  523. struct char_data {float wx,wy,x1,y1,x2,y2; };
  524. int font_get_chardata(struct char_data **cd, int ff, int cc);
  525. /*---------------------------------------------------------------------------*/
  526. int safnt;
  527. int simple_char(int cc);
  528. d_char(int font, int cc)
  529. {
  530.     static struct char_data cd;
  531.     static int ix1,ix2,iy1,iy2;
  532.     static int ux,uy;
  533.     char ss[2];
  534.  
  535.     ss[0] = cc;
  536.     ss[1] = 0;
  537.     if (safnt==0) safnt = pass_font("PLSR");
  538.     if (font_get_encoding(font)>2) {
  539.         my_char(font,cc);
  540.         return;
  541.     }
  542.     my_char(safnt,cc);
  543. }
  544.  
  545.