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

  1. /*--------------------------------------------------------------*/
  2. /*    HPGL Driver 1.0 , for GLE V3.0        */
  3. /*---------------------------------------------------------------------------*/
  4. #include "all.h"
  5. #include <math.h>
  6. #include "core.h"
  7. #include "mygraph.h"
  8. #include "mydev.h"
  9. #define GS 29
  10. #define FF 12
  11. long hp_clipx, hp_clipy;
  12. static long hpgl_addx, hpgl_addy;
  13. extern int hpgl_size; /* a0, a1, a2, a3, a4 */
  14. int ingraphmode;
  15. extern struct gmodel g;
  16. /*---------------------------------------------------------------------------*/
  17. #define pi 3.141592653
  18. #define true (!false)
  19. #define BLACKANDWHITE 1
  20. #define ESC 27
  21. #define false 0
  22. #define dbg if ((gle_debug & 64)>0)
  23. #define stop if ((gle_debug & 128)>0) return
  24. extern int gle_debug;
  25. int incap=true;
  26. int getch(void);
  27. /*---------------------------------------------------------------------------*/
  28. /* The global variables for the PC screen driver */
  29. /*-----------------------------------------------*/
  30.  
  31. int i,l,j,ix,iy;
  32. double f;
  33. double devxcm,devycm;  /* settable via environment variables (or symbols*/
  34. FILE *hpfile;
  35.  
  36. #define gerr() i = graphresult(); if (i!=0) printf("Graph error: %s \n",grapherrormsg(i));
  37. #define sx(v) ( ((v) * d_xscale) + hpgl_addx)
  38. #define sy(v) ( (((v) * d_yscale)) + hpgl_addy)
  39. #define rx(v) ( ((v) * d_xscale))
  40. #define ry(v) ( (((v) * d_yscale)))
  41.  
  42. double d_scale, d_xscale, d_yscale;
  43. int d_graphmode;
  44. int d_fillstyle=1,d_fillcolor;
  45. int d_lstyle,d_lwidth;
  46. char outstr[400];
  47.  
  48. d_devcmd(char *s)
  49. {}
  50. /*---------------------------------------------------------------------------*/
  51. d_dfont(char *c)
  52. {
  53.     /* only used for the DFONT driver which builds fonts */
  54. }
  55. /*---------------------------------------------------------------------------*/
  56. static char lastline[80];
  57. d_message(char *s)
  58. {
  59.     static int single_step;
  60.     int oldcolor,oldx,oldy;
  61.     printf("%s\n",s);
  62. }
  63. /*---------------------------------------------------------------------------*/
  64. d_source(char *s)
  65. {
  66.     s=s;
  67. }
  68. /*---------------------------------------------------------------------------*/
  69. d_get_type(char *t)
  70. {
  71.     strcpy(t,"HARDCOPY, HPGL, VAX, PC");
  72. }
  73. /*---------------------------------------------------------------------------*/
  74. d_set_path(int onoff)
  75. {}
  76. /*---------------------------------------------------------------------------*/
  77. d_newpath()
  78. {
  79. }
  80. /*---------------------------------------------------------------------------*/
  81. extern char output_file[];
  82. extern char input_file[];
  83. d_open(double width, double height)
  84. {
  85.     char *outfile;
  86.     char *s;
  87.     char outfileb[90] = "out.hp";
  88.  
  89.     outfile = &outfileb[0];
  90.     s = strrchr(input_file,'.');
  91.     if (s!=NULL) {
  92.         strcpy(outfile,input_file);
  93.         s = strrchr(outfile,'.');
  94.         strcpy(s,".hp");
  95.     }
  96.     if (output_file[0]!=0) outfile = &output_file[0];
  97. #ifdef __TURBOC__
  98.     printf("\n Writing output to file (%s) \n",outfile);
  99. #endif
  100.     hpfile = fopen(outfile,"w");
  101.     if (hpfile == NULL) {
  102.         gprint("Unable to HP output file {%s} \n",outfile);
  103.         gle_abort("Unable to open HP output file \n");
  104.     }
  105.  
  106.     hpgl_initstr(); /* read environment variables for set sequences */
  107.     graphmode();
  108.     ingraphmode = true;
  109.  
  110.     /* Get largest rectangle we can fit on the screen */
  111.     d_scale = devxcm / width;
  112.     f = devycm / height;
  113.     if (f<d_scale) d_scale = f;
  114.     if (d_scale<1) gprint("Shrinking picture to fit by factor %g \n",d_scale);
  115.     if (d_scale>1) d_scale = 1; /* scale down not up */
  116.  
  117.     d_xscale = d_scale * 400; /*  MAXX / devxcm;  Device Scale X, Device Scale y */
  118.     d_yscale = d_scale * 400; /*  MAXY / devycm; */
  119.  
  120.     hp_clipx = 400L * (devxcm) + 1;
  121.     hp_clipy = 400L * (devxcm) + 1;
  122.  
  123.     g.userwidth = width;
  124.     g.userheight = height;
  125.     plotter_fonts();
  126. }
  127. /*---------------------------------------------------------------------------*/
  128. d_tidyup()
  129. {
  130.     textmode();
  131. }
  132. d_close()
  133. {
  134.  
  135.     g_flush();
  136.     textmode();
  137. }
  138. /*---------------------------------------------------------------------------*/
  139. d_set_line_cap(int i)
  140. {
  141.     i++;
  142. }
  143. /*---------------------------------------------------------------------------*/
  144. d_set_line_join(int i)
  145. {
  146.     i++;
  147. }
  148. /*---------------------------------------------------------------------------*/
  149. d_set_line_miterlimit(double d)
  150. {
  151.     i++;
  152. }
  153. /*---------------------------------------------------------------------------*/
  154. d_set_line_width(double w)
  155. {
  156. }
  157. /*---------------------------------------------------------------------------*/
  158. d_set_line_styled(double dd)
  159. {}
  160. double lstylewidths[10]; /* widths */
  161. int nlstyle=0;    /* number in array, if zero then solid line */
  162. int lstylei=0;  /* entry we are drawing */
  163. double lstylex=0;    /* distance along current entry */
  164. int lstyleblack=true;
  165. d_line_dash(double zx, double zy)
  166. {
  167.     double ix,iy,ux,uy,dx,dy,d,dd,ddx,ddy,nx,ny,totald;
  168.     int newblack;
  169.     dx = zx - g.curx; dy = zy - g.cury;
  170.     d = sqrt(dx*dx + dy*dy);
  171.     totald = d;
  172.     if (totald==0) return;
  173.     for (;d>0;) {
  174.         if (d>(lstylewidths[lstylei]-lstylex)) {
  175.             dd = lstylewidths[lstylei++]-lstylex;
  176.             newblack = !lstyleblack;
  177.             lstylex = 0;
  178.             if (lstylei>=nlstyle) { lstylei = 0; newblack =true;}
  179.         } else {
  180.             dd = d;
  181.             newblack = lstyleblack;
  182.             lstylex += dd;
  183.         }
  184.         d -= dd;
  185.         ddx = dx *   dd/totald;  ddy = dy*dd/totald;
  186.         nx = g.curx + ddx; ny = g.cury + ddy;
  187.         if (lstyleblack) dd_line(nx,ny); else g.xinline = false;
  188.         g.curx = nx; g.cury = ny;
  189.         lstyleblack = newblack;
  190.     }
  191. }
  192. d_set_line_style(char *s)
  193. {
  194.     /* should deal with [] for solid lines */
  195.     static char *defline[] = {"","","12","42","14","92","1282"
  196.     ,"9229","4114","54","73","7337","6261","2514"};
  197.     static char ob[200];
  198.     int l;
  199.  
  200.     if (!g.inpath) g_flush();
  201.     strcpy(ob,"[");
  202.     if (strlen(s)==1) s = defline[*s-'0'];
  203.     l = strlen(s);
  204.     for (i=0;i<l;i++)
  205.         lstylewidths[i] = (*(s+i)-'0')*g.lstyled;
  206.     nlstyle = l;
  207. }
  208. /*---------------------------------------------------------------------------*/
  209. d_fill()
  210. {
  211. }
  212. /*---------------------------------------------------------------------------*/
  213. d_fill_ary(int nwk,double (*wkx)[],double (*wky)[])
  214. {
  215.     int i;
  216. /*    fprintf(psfile,"%g %g moveto \n",(*wkx)[0],(*wky)[0]);
  217.     for (i=1;i<nwk;i++)
  218.         fprintf(psfile,"%g %g l \n",(*wkx)[i],(*wky)[i]);
  219. */
  220. }
  221. d_line_ary(int nwk,double (*wkx)[],double (*wky)[])
  222. {
  223.     int i;
  224. /*
  225.     g_dev( (*wkx)[0], (*wky)[0], &ix, &iy);
  226.     moveto(ix,iy);
  227.     for (i=1;i<nwk;i++) {
  228.         g_dev( (*wkx)[i], (*wky)[i], &ix, &iy);
  229.         lineto(ix,iy);
  230.     }
  231. */
  232. }
  233. /*---------------------------------------------------------------------------*/
  234. d_stroke()
  235. {
  236. }
  237. /*---------------------------------------------------------------------------*/
  238. d_clip()
  239. {
  240. }
  241. /*---------------------------------------------------------------------------*/
  242. d_set_matrix(double newmat[3][3])
  243. {
  244. }
  245. /*---------------------------------------------------------------------------*/
  246. d_move(double zx,double zy)
  247. {
  248. }
  249. /*---------------------------------------------------------------------------*/
  250. d_reverse()     /* reverse the order of stuff in the current path */
  251. {
  252. }
  253. /*---------------------------------------------------------------------------*/
  254. d_closepath()
  255. {
  256.     g_line(g.closex,g.closey);
  257. }
  258. /*---------------------------------------------------------------------------*/
  259. int hpglxy(double xx, double yy);
  260. d_line(double zx,double zy)
  261. {
  262.     if (nlstyle==0) dd_line(zx,zy);
  263.     else d_line_dash(zx,zy);
  264. }
  265. dd_line(double zx,double zy)
  266. {
  267.     static double ix,iy,ux,uy;
  268.     static double oldx= -1000,oldy= -1000;
  269.     char s1[40],s2[40];
  270.     if (g.xinline==false) {
  271.         g_dev(g.curx,g.cury,&ux,&uy);
  272.         if (oldx!=ux || oldy!=uy) {
  273.          strcat(outstr,"; PU ; PA ");
  274.          hpglxy(ux,uy);
  275.         }
  276.         strcat(outstr,"; PD ; PA ");
  277.         g.xinline = true;
  278.     }
  279.     g_dev(zx,zy,&ix,&iy);
  280.     hpglxy(ix,iy);
  281.     oldx = ix; oldy = iy;
  282.     if (strlen(outstr)>70) d_flush();
  283. }
  284. /*---------------------------------------------------------------------------*/
  285. hpglxy(double xx, double yy)
  286. {
  287.     long x,y;
  288.     char ss[30];
  289.     x = sx(xx); y = sy(yy);
  290. /*     if (x<0) x = 0;
  291.     if (x>hp_clipx) x = hp_clipx;
  292.     if (y<0) y = 0;
  293.     if (y>hp_clipy) y = hp_clipy;
  294. */
  295.     sprintf(ss,"%ld %ld ",x,y);
  296.     strcat(outstr,ss);
  297. }
  298. /*---------------------------------------------------------------------------*/
  299. d_clear()
  300. {
  301.  }
  302. /*---------------------------------------------------------------------------*/
  303. d_flush()
  304. {
  305.     fprintf(hpfile,"%s;\n PA ",outstr);
  306.     outstr[0] = 0;
  307. }
  308. /*---------------------------------------------------------------------------*/
  309. d_arcto(dbl x1,dbl y1,dbl x2,dbl y2,dbl rrr)
  310. {
  311.     df_arcto(x1,y1,x2,y2,rrr);
  312. }
  313. /*---------------------------------------------------------------------------*/
  314. d_arc(dbl r,dbl t1,dbl t2,dbl cx,dbl cy)
  315. {
  316.     df_arc(r,t1,t2,cx,cy);
  317. }
  318. /*---------------------------------------------------------------------------*/
  319. d_narc(dbl r,dbl t1,dbl t2,dbl cx,dbl cy)
  320. {
  321.     df_arc(r,t1,t2,cx,cy);
  322. }
  323. /*---------------------------------------------------------------------------*/
  324. d_box_fill(dbl x1, dbl y1, dbl x2, dbl y2)
  325. {
  326.     df_box_fill(x1,y1,x2,y2);
  327. }
  328. d_box_stroke(dbl x1, dbl y1, dbl x2, dbl y2)
  329. {
  330.     df_box_stroke(x1,y1,x2,y2);
  331. }
  332. /*---------------------------------------------------------------------------*/
  333. d_circle_stroke(double zr)
  334. {
  335.     df_circle_stroke(zr);
  336. }
  337. d_circle_fill(double zr)
  338. {
  339.     df_circle_fill(zr);
  340. }
  341. /*---------------------------------------------------------------------------*/
  342. d_bezier(dbl x1,dbl y1,dbl x2,dbl y2,dbl x3,dbl y3)
  343. {
  344.     double ax,bx,cx,ay,by,cy,dist;
  345.     double xxx,yyy,i,t,nstep,x0,y0;
  346.     g_get_xy(&x0,&y0);
  347.     dist = fabs(x3-x0) + fabs(y3-y0);
  348.     nstep = 10;
  349.     if (dist>3) nstep = 20;
  350.     if (dist<.5) nstep = 5;
  351.     if (dist<.3) nstep = 3;
  352.      if (dist<.1) {
  353.         g_line(x3,y3);
  354.         return;
  355.     }
  356.     cx = (x1-x0)*3;
  357.     bx = (x2-x1)*3-cx;
  358.     ax = x3-x0-cx-bx;
  359.     cy = (y1-y0)*3;
  360.     by = (y2-y1)*3-cy;
  361.     ay = y3-y0-cy-by;
  362.     for (i=0;i<=nstep;i++) {
  363.         t = i/nstep;
  364.         xxx = ax*pow(t,3.0) + bx*t*t + cx*t + x0;
  365.         yyy = ay*pow(t,3.0) + by*t*t + cy*t + y0;
  366.         g_line(xxx,yyy);
  367.     }
  368. }
  369. /*---------------------------------------------------------------------------*/
  370. d_set_color(long f)
  371. {
  372.     int i;
  373.     char ss[50];
  374.     colortyp  cc;
  375.     cc.l = f;
  376.     i = 1;
  377.     if (cc.b[B_R]>100) i = 2;
  378.     if (cc.b[B_G]>100) i = 3;
  379.     if (cc.b[B_B]>100) i = 4;
  380.     if (cc.b[B_R]>100 && cc.b[B_G]>100) i = 5;
  381.     if (cc.b[B_B]>100 && cc.b[B_G]>100) i = 5;
  382.     if (cc.b[B_R]>250 && cc.b[B_G]>250 && cc.b[B_B]>250) i = 6;
  383.     sprintf(ss,"; SP%d ; PA ",i);
  384.     strcat(outstr,ss);
  385. }
  386. d_set_fill(long f)
  387. {
  388. }
  389. /*---------------------------------------------------------------------------*/
  390. d_beginclip()
  391. {
  392. }
  393. d_endclip()
  394. {
  395. }
  396. struct char_data {float wx,wy,x1,y1,x2,y2; };
  397. int font_get_chardata(struct char_data **cd, int ff, int cc);
  398. /*---------------------------------------------------------------------------*/
  399. d_char(int font, int cc)
  400. {
  401.     static struct char_data cd;
  402.     static int ix,iy,ix1,ix2,iy1,iy2,fz,fzx;
  403.     static int ux,uy;
  404.     static int safnt;
  405.     char ss[2];
  406.  
  407.     ss[0] = cc;
  408.     ss[1] = 0;
  409.     if (safnt==0) safnt = pass_font("PLSR");
  410.     if (font>=2) {
  411.         my_char(font,cc);
  412.         return;
  413.     }
  414.     my_char(safnt,cc);
  415. }
  416. char *hpglopenstr,*hpglclosestr;
  417. int inithpgl;
  418. graphmode()
  419. {
  420.     if (!inithpgl) hpgl_initstr();
  421.     ingraphmode = true;
  422.     fprintf(hpfile,"%s\n",hpglopenstr);
  423. }
  424. textmode()
  425. {
  426.     if (!inithpgl) hpgl_initstr();
  427.     fprintf(hpfile,";\n %s \n",hpglclosestr);
  428.     ingraphmode = false;
  429. }
  430. char *cvt_control(char *mystr);
  431. char *cvt_control(char *xx)
  432. {
  433.     char *s;
  434.     static char mystr[200];
  435.     strcpy(mystr,xx);
  436. loop1:    s = strchr(mystr,'^');
  437.     if (s!=NULL) {
  438.         *(s+1) = *(s+1) - 64;
  439.         memmove(s,s+1,strlen(s+1)+1);
  440.         goto loop1;
  441.     }
  442.     return mystr;
  443. }
  444. double getsymvalue(char *sym, double dflt);
  445. char *getsymbol(char *sym);
  446. double getsymvalue(char *sym, double dflt)
  447. {
  448.     char *s;
  449.     s = getsymbol(sym);
  450.     if (strlen(s)==0) return dflt;
  451.     return atof(s);
  452. }
  453. hpgl_initstr()
  454. {
  455.     char  *s;
  456.     double def_x,def_y;
  457.     inithpgl = true;
  458.     s = getsymbol("HPGL_OPEN");
  459.     /* Init plotter and scale so that 400 units = 1cm */
  460.     /* s = "^[.(^[.I;;17:^[.N;19:IN;IP 520,1140,15720,11140; sc 120,15320,540,10540;PU;VS10;SP1;PA;PU;"; */
  461.  
  462.  
  463.     if (strlen(s)==0) s = "^[.(^[.I;;17:^[.N;19:IN;IP ;PU;VS10;SP1;PA;PU;";
  464.     hpglopenstr = sdup(cvt_control(s));
  465.     devxcm = getsymvalue("HPGL_WIDTH",200);
  466.     devycm = getsymvalue("HPGL_HEIGHT",200);
  467.     def_x = .9; def_y = 1.5;
  468.     if (hpgl_size==0) { def_x = -22190.0/400; def_y = -15740.0/400; }
  469.     if (hpgl_size==1) { def_x = -15120.0/400; def_y = -10760.0/400; }
  470.     hpgl_addx = 400*getsymvalue("HPGL_ADDX",def_x);
  471.     hpgl_addy = 400*getsymvalue("HPGL_ADDY",def_y);
  472.  
  473.     /* printf("width %g hei %g str {%s} \n",devxcm,devycm,s); */
  474.     s = getsymbol("HPGL_CLOSE");
  475.     /* Advance Full and cut page */
  476.     if (strlen(s)==0) s = "SP0;IN;AF;^[.)";
  477. /*    if (strlen(s)==0) s = "SP0;IN;AH;^[.)"; */
  478.     hpglclosestr = sdup(cvt_control(s));
  479. }
  480.