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

  1. /*--------------------------------------------------------------*/
  2. /*    CORE, for GLE V3.0                     */
  3. /*--------------------------------------------------------------*/
  4.  
  5. /* g_scr_* needs to be added */
  6. /* g_int_* needs to be decided/added */
  7. /* g_tex_* needs to be decided/added */
  8.  
  9. /*---------------------------------------------------------------------------*/
  10. #include <math.h>
  11. #include "all.h"
  12. #include "mygraph.h"
  13. #include "mydev.h"
  14. #include "core.h"
  15. #include "justify.h"
  16. int mystrncmp(char *a, char *b, int n);
  17. struct gmodel g;
  18. double tmpimg[3][3];
  19. double tmpimg2[3][3];
  20. double font_lwidth;
  21. /*---------------------------------------------------------------------------*/
  22. #define PI 3.141592653
  23. #define pi 3.141592653
  24. #define true (!false)
  25. #define false 0
  26. #define dbg if ((gle_debug & 32)>0)
  27. extern int gle_debug;
  28. /*---------------------------------------------------------------------------*/
  29. /* The global variables that CORE keeps track of */
  30. /*-----------------------------------------------*/
  31.  
  32. static int jj;
  33. int test_unit(void);
  34. int gunit=false;
  35. #define color_black 0X01000000
  36. /*---------------------------------------------------------------------------*/
  37.  
  38. /*---------------------------------------------------------------------------*/
  39. g_dfont(char *s)
  40. {
  41.     d_dfont(s);
  42. }
  43. /*---------------------------------------------------------------------------*/
  44. long g_get_grey(double v)
  45. {
  46.     colortyp c;
  47.     c.b[B_F] = 1;
  48.     c.b[B_R] = 255*v;
  49.     c.b[B_G] = 255*v;
  50.     c.b[B_B] = 255*v;
  51.     return c.l;
  52. }
  53. /*---------------------------------------------------------------------------*/
  54. g_hint(char *s)
  55. {
  56.     gprint("%s\n",s);
  57. }
  58. /*---------------------------------------------------------------------------*/
  59. extern int noscreenio;
  60. g_message(char *s)
  61. {
  62.     if (noscreenio) printf("%s\n",s);
  63.     else d_message(s);
  64. }
  65. /*---------------------------------------------------------------------------*/
  66. g_source(char *s)
  67. {
  68.     d_source(s);
  69. }
  70. /*---------------------------------------------------------------------------*/
  71. g_get_end(dbl *x,dbl *y)
  72. {
  73.     *x = tex_xend();
  74.     *y = tex_yend();
  75. }
  76. g_set_end(dbl x,dbl y)
  77. {
  78.     gprint("What is setting end \n");
  79. }
  80. /*---------------------------------------------------------------------------*/
  81. g_get_usersize(dbl *x,dbl *y)
  82. {        /* Does this want device size, or user size ????? */
  83.     *x = g.userwidth;
  84.     *y = g.userheight;
  85. }
  86. g_get_devsize(dbl *x,dbl *y)
  87. {        /* Does this want device size, or user size ????? */
  88.     *x = g.devwidth;
  89.     *y = g.devheight;
  90. }
  91. /*---------------------------------------------------------------------------*/
  92. g_get_bounds(dbl *x1,dbl *y1,dbl *x2,dbl *y2)
  93. {
  94.     *x1 = g.xmin;
  95.     *y1 = g.ymin;
  96.     *x2 = g.xmax;
  97.     *y2 = g.ymax;
  98. }
  99. /*---------------------------------------------------------------------------*/
  100. g_init_bounds()
  101. {
  102.     g.xmin = 1e30;
  103.     g.ymin = 1e30;
  104.     g.xmax = -1e30;
  105.     g.ymax = -1e30;
  106. }
  107. /*---------------------------------------------------------------------------*/
  108. g_get_type(char *t)
  109. {
  110.     d_get_type(t);
  111. }
  112. /*---------------------------------------------------------------------------*/
  113. g_set_path(int onoff)
  114. {
  115.     if (onoff==g.inpath) return;
  116.     g_flush();
  117.     if (onoff==true) {
  118.         g.inpath=true;
  119.         g.npath = 0;
  120.         g.xinline=false;
  121.     } else {
  122.         g.inpath = false;
  123.         g.xinline = false;
  124.     }
  125.     d_set_path(onoff);
  126. }
  127. /*---------------------------------------------------------------------------*/
  128. g_get_path(int *onoff)
  129. {
  130.     *onoff = g.inpath;
  131. }
  132. /*---------------------------------------------------------------------------*/
  133. g_newpath()
  134. {
  135.     g.npath = 0;
  136.     d_newpath();
  137. }
  138. /*---------------------------------------------------------------------------*/
  139. static int isopen=0;
  140. g_open(dbl width,dbl height)
  141. {
  142.     if (isopen) return;
  143.     isopen = true;
  144.     if (width*height==0) {
  145.       gprint("G_OPEN, Width or height was zero w=%d, h=%d\n",width,height);
  146.       width = 10; height = 10;
  147.     }
  148.     g.userwidth = width;
  149.     g.userheight = height;
  150.     freeafont();
  151.     d_open(width,height);
  152.     g_clear();
  153. }
  154. /*---------------------------------------------------------------------------*/
  155. g_close()
  156. {
  157.     isopen = false;
  158.     g_flush();
  159.     d_close();
  160. }
  161. /*---------------------------------------------------------------------------*/
  162. g_set_line_cap(int i)
  163. {
  164.     /*  lcap, 0= butt, 1=round, 2=projecting square */
  165.     if (i<0 || i>2) {
  166.         gprint("Invalid line cap, {%d}, valid numbers are \n",i);
  167.         gprint("    0= butt, 1=round, 2=projecting square \n");
  168.     }
  169.     d_set_line_cap(i);
  170.     g.lcap = i;
  171. }
  172. /*---------------------------------------------------------------------------*/
  173. g_set_line_join(int i)
  174. {
  175.     if (i<0 || i>2) {
  176.         gprint("Invalid line join, {%d}, valid numbers are \n",i);
  177.         gprint("    0= mitre, 1=round, 2=bevel \n");
  178.     }
  179.     d_set_line_join(i);
  180.     g.ljoin = i;
  181. }
  182. g_get_line_join(int *i)
  183. {
  184.     *i = g.ljoin;
  185. }
  186. g_get_line_cap(int *i)
  187. {
  188.     *i = g.lcap;
  189. }
  190. /*---------------------------------------------------------------------------*/
  191. g_set_line_miterlimit(double d)
  192. {
  193.     d_set_line_miterlimit(d);
  194.     g.miterlimit = d;
  195. }
  196. /*---------------------------------------------------------------------------*/
  197. g_get_line_width(double *w)
  198. {
  199.     *w = g.lwidth;
  200. }
  201. /*---------------------------------------------------------------------------*/
  202. g_set_line_width(double w)
  203. {
  204.     if (w<0) return;
  205.     d_set_line_width(w);
  206.     g.lwidth = w;
  207. }
  208. g_set_font_width(double w)
  209. {
  210.     font_lwidth = w;
  211. }
  212. /*---------------------------------------------------------------------------*/
  213. g_get_line_styled(double *w)
  214. {
  215.     *w = g.lstyled;
  216. }
  217. /*---------------------------------------------------------------------------*/
  218. g_set_line_styled(double w)
  219. {
  220.     if (w==0) return;
  221.     d_set_line_styled(w);
  222.     g.lstyled = w;
  223. }
  224. /*---------------------------------------------------------------------------*/
  225. g_set_line_style(char *s)
  226. {
  227.     d_set_line_style(s);
  228.     strncpy(g.lstyle,s,8);
  229. }
  230. /*---------------------------------------------------------------------------*/
  231. g_get_line_style(char *s)
  232. {
  233.     strncpy(s,(char *) &g.lstyle,8);
  234. }
  235. /*---------------------------------------------------------------------------*/
  236. g_dev(double x, double y,double *xd,double *yd)
  237. {
  238.     static double xx,yy;
  239.     if (gunit==true) { *xd = x; *yd = y; }
  240.     else {
  241.     xx = g.image[0][0]*x + g.image[0][1]*y + g.image[0][2];
  242.     yy = g.image[1][0]*x + g.image[1][1]*y + g.image[1][2];
  243.     *xd = xx;
  244.     *yd = yy;
  245.     }
  246. }
  247. /*---------------------------------------------------------------------------*/
  248. g_undev(double ux,double uy, double *x,double *y)
  249. {
  250.     static double xx,yy,cdiv,xd,yd;
  251.     if (gunit==true) { *x = ux; *y = uy; }
  252.     else {
  253.     cdiv = ( g.image[0][1]*g.image[1][0] - g.image[0][0]*g.image[1][1] );
  254.     if (cdiv==0) {gprint("Image matrix FLAT, a 1D world, giving up \n");return;}
  255.     xd = ux - g.image[0][2];
  256.     yd = uy - g.image[1][2];
  257.     xx = -xd*g.image[1][1] + yd*g.image[0][1];
  258.     *x = xx/cdiv;
  259.     yy = xd*g.image[1][0]-yd*g.image[0][0];
  260.     *y = yy/cdiv;
  261.     }
  262. }
  263. /*---------------------------------------------------------------------------*/
  264. g_rundev(double x, double y,double *xd,double *yd)
  265. {
  266.     static double zx,zy;
  267.     g_undev(0,0,&zx,&zy);
  268.     g_undev(x,y,xd,yd);
  269.     *xd -= zx;
  270.     *yd -= zy;
  271. }
  272. g_rdev(double x, double y,double *xd,double *yd)
  273. {
  274.     static double zx,zy;
  275.     g_dev(0,0,&zx,&zy);
  276.     g_dev(x,y,xd,yd);
  277.     *xd -= zx;
  278.     *yd -= zy;
  279. }
  280. /*---------------------------------------------------------------------------*/
  281. g_fill()
  282. {
  283.     d_fill();
  284. }
  285. /*---------------------------------------------------------------------------*/
  286. g_fill_ary(int nwk,double (*wkx)[],double (*wky)[])
  287. {
  288.     d_fill_ary(nwk,wkx,wky);
  289. }
  290. g_line_ary(int nwk,double (*wkx)[],double  (*wky)[])
  291. {
  292.     d_line_ary(nwk,wkx,wky);
  293. }
  294. /*---------------------------------------------------------------------------*/
  295. g_stroke()
  296. {
  297.     d_stroke();
  298. }
  299. /*---------------------------------------------------------------------------*/
  300. g_clip()
  301. {
  302.     /* Find the intersection of the current path with the clipping path */
  303.     /* and thus define a new clipping path */
  304.     d_clip();
  305. }
  306. /*---------------------------------------------------------------------------*/
  307. g_set_matrix(double newmat[3][3])
  308. {
  309.     static double x1,y1,x2,y2,x3,y3,x4,y4;
  310.     static double a1,b1,a2,b2,a3,b3,a4,b4;
  311.  
  312.     g_dev(g.xmin,g.ymin,&x1,&y1);
  313.     g_dev(g.xmax,g.ymin,&x2,&y2);
  314.     g_dev(g.xmax,g.ymax,&x3,&y3);
  315.     g_dev(g.xmin,g.ymax,&x4,&y4);
  316.  
  317.     if (memcmp(newmat,g.image,3*3*8)!=0) d_set_matrix(newmat);
  318.     memcpy(&g.image,newmat,3*3*8);
  319.  
  320.     g_init_bounds();
  321.  
  322.     if (g.xmin<g.xmax)  {
  323.         g_undev(x1,y1,&a1,&b1);
  324.         g_undev(x2,y2,&a2,&b2);
  325.         g_undev(x3,y3,&a3,&b3);
  326.         g_undev(x4,y4,&a4,&b4);
  327.         g_set_bounds(a1,b1);
  328.         g_set_bounds(a2,b2);
  329.         g_set_bounds(a3,b3);
  330.         g_set_bounds(a4,b4);
  331.     }
  332. }
  333. /*---------------------------------------------------------------------------*/
  334. mat_mult(double a[3][3],double b[3][3])
  335. {
  336.     static double c[3][3],tot;
  337.     int y,xb,x;
  338.     for (y=0;y<3;y++) {
  339.       for (xb=0;xb<3;xb++) {
  340.         tot = 0;
  341.         for (x=0;x<3;x++) tot += a[x][y] * b[xb][x];
  342.         c[xb][y] = tot;
  343.       }
  344.     }
  345.     memcpy(a,&c,3*3*8);    /* maybe sizeof(*a) would be better? */
  346. }
  347. /*---------------------------------------------------------------------------*/
  348. g_rotate(double ar)
  349. {
  350.     static double r[3][3],unx,uny,cx,cy;
  351.     ar = pi*ar/180;
  352.     r[0][0] = cos(ar);
  353.     r[0][1] = -sin(ar);
  354.     r[1][0] = sin(ar);
  355.     r[1][1] = cos(ar);
  356.     r[2][2] = 1;
  357.     g_dev(g.curx,g.cury,&cx,&cy);
  358.     g_rundev(-cx,-cy,&unx,&uny);
  359.     g_translate(unx,uny);
  360.     memcpy(tmpimg,g.image,3*3*8);
  361.     mat_mult(tmpimg,r);
  362.     g_set_matrix(tmpimg);
  363.     g_rundev(cx,cy,&unx,&uny);
  364.     g_translate(unx,uny);    /* not shore about this ORIGIN stuff */
  365.     test_unit();
  366. }
  367. /*---------------------------------------------------------------------------*/
  368. int gg_unrotate(void);
  369. int gg_rerotate(void);
  370. g_scale(double sx,double sy)
  371. {
  372.     /* The idea is to rotate or scale about the CURRENT POINT */
  373.     static double r[3][3],unx,uny,cx,cy;
  374.     r[0][0] = sx;
  375.     r[1][1] = sy;
  376.     r[2][2] = 1;
  377.  
  378.     gg_unrotate();
  379.     g_dev(g.curx,g.cury,&cx,&cy);
  380.     g_rundev(-cx,-cy,&unx,&uny);
  381.     g_translate(unx,uny);
  382.     memcpy(tmpimg,g.image,3*3*8);
  383.     mat_mult(tmpimg,r);
  384.     g_set_matrix(tmpimg);
  385.     g_rundev(cx,cy,&unx,&uny);
  386.     g_translate(unx,uny);    /* not shore about this ORIGIN stuff */
  387.     gg_rerotate();
  388.     test_unit();
  389. }
  390.     static double ggra;
  391. gg_rerotate()
  392. {
  393.     g_rotate(ggra);
  394. }
  395. gg_unrotate()
  396. {
  397.     double ox,oy,x1,y0,dx,dy,tt;
  398.     g_dev(0.0,0.0,&ox,&oy);
  399.     g_dev(1.0,0.0,&x1,&y0);
  400.     dx = x1-ox;
  401.     dy = y0-oy;
  402.     tt = myatan2(dy,dx);
  403.     ggra = tt * 180.0 / PI;
  404.     g_rotate(-ggra);
  405. }
  406.  
  407. /*---------------------------------------------------------------------------*/
  408. dis_mat(char *s,double m[3][3])
  409. {
  410.     int i,j;
  411.     gprint("\n Matrix {%s} \n",s);
  412.     for (i=0;i<3;i++)
  413.         gprint("    %f %f %f \n",m[0][i],m[1][i],m[2][i]);
  414.  
  415. }
  416. /*---------------------------------------------------------------------------*/
  417. g_translate(double ztx,double zty)
  418. {
  419.     static double tx,ty,r[3][3];
  420.     g_rdev(ztx,zty,&tx,&ty);
  421.  
  422.     r[0][0] = 1;
  423.     r[1][1] = 1;
  424.     r[2][2] = 1;
  425.     r[0][2] = tx;
  426.     r[1][2] = ty;
  427.     memcpy(tmpimg,g.image,3*3*8);
  428.     mat_mult(tmpimg,r);
  429.     g_set_matrix(tmpimg);
  430.     test_unit();
  431. }
  432. /*---------------------------------------------------------------------------*/
  433. g_move(double zx,double zy)
  434. {
  435.     double x,y;
  436.     if (g.xinline==true) g_flush();
  437.     d_move(zx,zy);
  438.     g.curx = zx;
  439.     g.cury = zy;
  440.     g.closex = zx;
  441.     g.closey = zy;
  442. }
  443. g_rmove(double zx,double zy)
  444. {
  445.     g_move(g.curx+zx,g.cury+zy);
  446. }
  447. g_rline(double zx,double zy)
  448. {
  449.     g_line(g.curx+zx,g.cury+zy);
  450. }
  451. /*---------------------------------------------------------------------------*/
  452. g_reverse()     /* reverse the order of stuff in the current path */
  453. {    d_reverse();    }
  454. /*---------------------------------------------------------------------------*/
  455. g_closepath()
  456. {
  457.     if (g.inpath) d_closepath();
  458.     else g_line(g.closex,g.closey);
  459.     g.curx = g.closex;
  460.     g.cury = g.closey;
  461.     if (!g.inpath) g_flush();
  462. }
  463. /*---------------------------------------------------------------------------*/
  464. g_line(double zx,double zy)
  465. {
  466.     double x,y;
  467.     d_line(zx,zy);
  468.     if (g.xinline==false) {
  469.         g.xinline = true;
  470.         g_set_bounds(g.curx,g.cury);
  471.     }
  472.     g.curx = zx;
  473.     g.cury = zy;
  474.     g_set_bounds(zx,zy);
  475. }
  476. /*---------------------------------------------------------------------------*/
  477. g_set_bounds(double x,double y)
  478. {
  479.     if (x<g.xmin) g.xmin = x;
  480.     if (x>g.xmax) g.xmax = x;
  481.     if (y<g.ymin) g.ymin = y;
  482.     if (y>g.ymax) g.ymax = y;
  483. }
  484. /*---------------------------------------------------------------------------*/
  485. test_unit()
  486. {
  487.     int i,j;
  488.     gunit = true;
  489.     for (i=0;i<3;i++) for (j=0;j<3;j++) if (i!=j) if(g.image[i][j]!=0.0) gunit = false;
  490.     for (i=0;i<3;i++) if (g.image[i][i]!=1.0) gunit = false;
  491. }
  492. /*
  493.     for (i=0;i<3;i++) {
  494.         gprint("Matrix %g %g %g \n",g.image[i][0]
  495.             ,g.image[i][1],g.image[i][2]);
  496.     }
  497. */
  498. int tex_clear(void);
  499. g_clear()
  500. {
  501.     int i,j;
  502.     for (i=0;i<3;i++) for (j=0;j<3;j++) g.image[i][j] = 0;
  503.     for (i=0;i<3;i++) g.image[i][i] = 1;
  504.     d_clear();
  505.     tex_clear();
  506.     g_set_just(JUST_LEFT);
  507.     g_set_line_styled(.04);
  508.     g_set_line_style("1");
  509.     g_set_line_width(0);
  510.     g_set_color(color_black);
  511.     g_set_fill(color_black);
  512.     g_set_font(1);     /*    Load and set default font     */
  513.     g_set_font_width(-1);     /*    Load and set default font     */
  514.     g_set_hei(1.0);    /*    And set to 1 cm size          */
  515.     g_move(0.0,0.0);
  516.     test_unit();
  517.  
  518. }
  519. /*---------------------------------------------------------------------------*/
  520. g_get_xy(double *x,double *y)
  521. {
  522.     *x = g.curx;
  523.     *y = g.cury;
  524. }
  525. g_set_xy(double x, double y)    /* a synonym for MOVE */
  526. {    g_move(x,y); }
  527. /*---------------------------------------------------------------------------*/
  528. g_flush()
  529. {
  530.     d_flush();
  531.     g.xinline = false;
  532. }
  533. /*---------------------------------------------------------------------------*/
  534. g_arcto(dbl x1,dbl y1,dbl x2,dbl y2,dbl rrr)
  535. {
  536.     d_arcto(x1,y1,x2,y2,rrr);
  537.  
  538.     g.curx = x2;
  539.     g.cury = y2;
  540.     g.xinline = true;
  541.     /* should do MASSES of calc to find bounds of arc  */
  542. }
  543. /*---------------------------------------------------------------------------*/
  544. g_arc(dbl r,dbl t1,dbl t2,dbl cx,dbl cy)
  545. {
  546.     d_arc(r,t1,t2,cx,cy);
  547.     g.xinline = true;
  548. }
  549. /*---------------------------------------------------------------------------*/
  550. g_narc(dbl r,dbl t1,dbl t2,dbl cx,dbl cy)
  551. {
  552.     d_narc(r,t1,t2,cx,cy);
  553.     g.xinline = true;
  554. }
  555. /*---------------------------------------------------------------------------*/
  556. g_dojust(dbl *x1, dbl *y1, dbl *x2, dbl *y2, int jj)
  557. {
  558.     static int jx,jy;
  559.     static double w,y,d;
  560.     jx = (jj & 0xf0) / 16;
  561.     jy = jj & 0x0f;
  562.     d = jx * (*x2-*x1)/2;
  563.     *x1 -= d;
  564.     *x2 -= d;
  565.     d = jy * (*y2-*y1)/2;
  566.     *y1 -= d;
  567.     *y2 -= d;
  568. }
  569. g_dotjust(dbl *x1, dbl *y1, dbl l, dbl r, dbl u, dbl d, int jj)
  570. {
  571.     static int jx,jy,t;
  572.     static double ddd;
  573.     jx = (jj & 0xf0) / 16;
  574.     jy = jj & 0x0f;
  575.     t = (jj & 0xf00) / 256 ;
  576.     ddd = jx * (-l+r)/2;
  577.     *x1 = *x1 + -l - ddd;
  578.     ddd = jy * (u-d)/2;
  579.     if (t==0) *y1 = *y1 - d - ddd;
  580. }
  581. g_box_stroke(dbl x1, dbl y1, dbl x2, dbl y2)
  582. {
  583.     double x,y;
  584.     g_get_xy(&x,&y);
  585.     d_box_stroke(x1,y1,x2,y2);
  586.  
  587.     g_move(x,y);
  588. }
  589. /*---------------------------------------------------------------------------*/
  590. g_box_fill(dbl x1, dbl y1, dbl x2, dbl y2)
  591. {
  592.     double x,y;
  593.     g_get_xy(&x,&y);
  594.     d_box_fill(x1,y1,x2,y2);
  595.     g_move(x,y);
  596. }
  597. /*---------------------------------------------------------------------------*/
  598. g_circle_stroke(dbl zr)
  599. {
  600.     d_circle_stroke(zr);
  601.     g_set_bounds(g.curx-zr,g.cury-zr);
  602.     g_set_bounds(g.curx+zr,g.cury+zr);
  603. }
  604. g_circle_fill(dbl zr)
  605. {
  606.     d_circle_fill(zr);
  607.     g_set_bounds(g.curx-zr,g.cury-zr);
  608.     g_set_bounds(g.curx+zr,g.cury+zr);
  609. }
  610. /*---------------------------------------------------------------------------*/
  611. g_bezier(dbl x1,dbl y1,dbl x2,dbl y2,dbl x3,dbl y3)
  612. {
  613.     double x,y;
  614.     d_bezier(x1,y1,x2,y2,x3,y3);
  615.     if (g.xinline==false) {
  616.         g.xinline = true;
  617.         g_set_bounds(g.curx,g.cury);
  618.     }
  619.     g.curx = x3;
  620.     g.cury = y3;
  621.     g_set_bounds(x3,y3);
  622. }
  623. /*---------------------------------------------------------------------------*/
  624. g_dmove(double x, double y)
  625. {
  626.     double ux,uy;
  627.     g_undev(x,y,&ux,&uy);
  628.     d_move(ux,uy);
  629.     g.curx = ux;
  630.     g.cury = uy;
  631. }
  632. /*---------------------------------------------------------------------------*/
  633. g_dline(double x, double y)
  634. {
  635.     double ux,uy;
  636.     g_undev(x,y,&ux,&uy);
  637.     d_line(ux,uy);
  638.     g.curx = ux;
  639.     g.cury = uy;
  640. }
  641. /*---------------------------------------------------------------------------*/
  642. g_dbezier(dbl x1,dbl y1,dbl x2,dbl y2,dbl x3,dbl y3)
  643. {
  644.     double a1,b1,a2,b2,a3,b3;
  645.     g_undev(x1,y1,&a1,&b1);
  646.     g_undev(x2,y2,&a2,&b2);
  647.     g_undev(x3,y3,&a3,&b3);
  648.     g_bezier(a1,b1,a2,b2,a3,b3);
  649.     g.curx = a3;
  650.     g.cury = b3;
  651. }
  652. /*---------------------------------------------------------------------------*/
  653. g_bitmap(char *bmap)
  654. {
  655. }
  656. /*---------------------------------------------------------------------------*/
  657. g_set_rgbf(double rr, double gg, double bb, double ff)
  658. {
  659.     /* Fill pattern is left as-is */
  660.     g.color.b[B_R] = rr*255;
  661.     g.color.b[B_G] = gg*255;
  662.     g.color.b[B_B] = bb*255;
  663.     g.color.b[B_F] = ff*255;
  664.     d_set_color(g.color.l);
  665. }
  666. g_set_rgbf_fill(double rr, double gg, double bb, double ff)
  667. {
  668.     /* Fill pattern is left as-is */
  669.     g.fill.b[B_R] = rr*255;
  670.     g.fill.b[B_G] = gg*255;
  671.     g.fill.b[B_B] = bb*255;
  672.     g.fill.b[B_F] = ff*255;
  673.     d_set_fill(g.fill.l);
  674. }
  675. g_get_rgbf(double *rr, double *gg, double *bb, double *ff)
  676. {
  677.     *rr = g.color.b[B_R]/255;
  678.     *gg = g.color.b[B_G]/255;
  679.     *bb = g.color.b[B_B]/255;
  680.     *ff = g.color.b[B_F]/255;
  681. }
  682. /*---------------------------------------------------------------------------*/
  683. g_set_color(long l)
  684. {
  685.     if (l==0) return;
  686.     g.color.l = l;
  687.     d_set_color(g.color.l);
  688. }
  689. g_get_color(long *l)
  690. {
  691.     *l = g.color.l;
  692. }
  693. /*---------------------------------------------------------------------------*/
  694. g_set_fill(long l)
  695. {
  696.     g.fill.l = l;
  697.     d_set_fill(g.fill.l);
  698. }
  699. g_get_fill(long *l)
  700. {
  701.     *l = g.fill.l;
  702. }
  703. /*---------------------------------------------------------------------------*/
  704. g_beginclip() {
  705.     d_beginclip(); }
  706. g_endclip() {
  707.     d_endclip(); }
  708. /*---------------------------------------------------------------------------*/
  709. static int ngsave;
  710. static char *gsave[100];
  711.  
  712. g_gsave()
  713. {
  714.     ngsave++;
  715.     if (ngsave>=99) {
  716.         gprint("Over 99 GSAVE's, probably a loop in your code\n");
  717.         return;
  718.     }
  719.     gsave[ngsave] = myallocz(SIZEOFSTATE+10);
  720.     g_get_state(gsave[ngsave]);
  721. }
  722. g_grestore()
  723. {
  724.     static double a,b;
  725.     g_flush();
  726.     if (ngsave==0) {
  727.         gprint("Attempt to GRESTORE at top of stack\n");
  728.         if (gle_debug>0) a = a/b;
  729.         return;
  730.     }
  731.     g_set_state(gsave[ngsave]);
  732.     myfree(gsave[ngsave]);
  733.     ngsave--;
  734. }
  735. g_get_state(char *s)
  736. {
  737.     memcpy(s,&g,SIZEOFSTATE);
  738. }
  739. g_set_state(char *s)
  740. {
  741.     memcpy(tmpimg,g.image,3*3*8);
  742.     memcpy(&g,s,SIZEOFSTATE);
  743.     memcpy(tmpimg2,g.image,3*3*8);
  744.     memcpy(g.image,tmpimg,3*3*8);
  745.     g_set_matrix(tmpimg2);
  746.     d_set_color(g.color.l);
  747.     d_set_fill(g.fill.l);
  748.     d_set_line_width(g.lwidth);
  749.     d_set_line_style(g.lstyle);
  750.     d_set_line_styled(g.lstyled);
  751.     test_unit();
  752. }
  753.     /*         12,4,-.5,-.5,0.35,    original dot */
  754. struct mark_struct { int ff; int cc; double rx; double ry; double scl;};
  755. struct mark_struct minf[61];
  756. extern char *mrk_name[];
  757. extern char *mrk_fname[];
  758.  
  759. /* struct mark_struct { int ff, int cc, double rx, double ry, double scl;}; */
  760. extern char *mark_name[];
  761. extern char *mark_sub[];
  762. extern int mark_subp[];
  763. extern int nmark;
  764.  
  765. extern int nmrk;
  766. g_marker(int i, double sz)
  767. {
  768.     g_marker2(i,sz,1.0);
  769. }
  770. g_marker2(int i, double sz, double dval)
  771. {
  772.     static double cx,cy,h,scale;
  773.     static double x1,y1,x2,y2;
  774.     int otype;
  775.     if (i<0) {{
  776.         char *stk_str[6];
  777.         double stk[6];
  778.         int nstk=2;
  779.         ++i;
  780.         i = -i;
  781.         if (mark_subp[i]==-1) {{
  782.             int idx,zret,np,plist;
  783.             mark_subp[i] = sub_find(mark_sub[i],&idx,&zret,&np,(int **) &plist);
  784.             if (mark_subp[i] == 0)  {
  785.                 gprint("You MUST define the sub before defining the marker\n");
  786.                 return;
  787.             }
  788.         }}
  789.         stk[1] = sz;
  790.         stk[2] = dval;
  791.         g_get_xy(&cx,&cy);
  792.         sub_call(mark_subp[i],(double *) &stk,(char **) &stk_str,&nstk,&otype);
  793.         g_move(cx,cy);
  794.         return;
  795.     }}
  796.     if (i<1 || i>nmrk) {gprint("Invalid marker number %d \n",i); return;}
  797.     g_get_xy(&cx,&cy);
  798.     g_get_hei(&h);
  799.     i--;
  800.     scale = minf[i].scl*sz;
  801.     g_set_hei(scale);
  802.     if (minf[i].ff == 0) minf[i].ff = pass_font(mrk_fname[i]);
  803.     if (minf[i].ff == -1) {
  804.         minf[i].ff = pass_font(mrk_fname[i]);
  805.         char_bbox(minf[i].ff,minf[i].cc,&x1,&y1,&x2,&y2);
  806.         minf[i].ry = (minf[i].ry + -y1-(y2-y1)/2.0);
  807.         minf[i].rx = (minf[i].rx + -x1-(x2-x1)/2.0);
  808.     }
  809.  
  810.     g_move(cx+minf[i].rx*scale, cy+minf[i].ry*scale);
  811.     g_char(minf[i].ff,minf[i].cc);
  812.     g_move(cx,cy);
  813.     g_set_hei(h);
  814. }
  815. g_defmarker(char *mname,char *font, int ccc, double dx, double dy, double sz, int autodx)
  816. {
  817.     int i;
  818.     if (nmrk>61-1) {gprint("Too many markers defined \n"); return;}
  819.     i = nmrk;
  820.     nmrk++;
  821.     mrk_name[i] = sdup(mname);
  822.     mrk_fname[i] = sdup(font);
  823.     minf[i].ff = 0;
  824.     if (autodx) minf[i].ff = -1;
  825.     minf[i].cc = ccc;
  826.     minf[i].rx = dx;
  827.     minf[i].ry = dy;
  828.     minf[i].scl = sz;
  829. }
  830. g_marker_def(char *mname,char *subname)
  831. {
  832.     int i;
  833.     for (i=0; i<nmark; i++) if (strcmp(mname,mark_name[i])==0) {
  834.         myfree(mark_name[i]);
  835.         myfree(mark_sub[i]);
  836.         nmark--;
  837.         break;
  838.     }
  839.     nmark++;
  840.     mark_name[i] = sdup(mname);
  841.     mark_sub[i] = sdup(subname);
  842.     mark_subp[i] = -1;
  843. }
  844. g_char(int font, int cc)
  845. {
  846.     d_char(font,cc);
  847. }
  848. g_text(char *ss)
  849. {
  850.     text_block(ss,0.0,g.just);
  851. }
  852. g_set_just(int jj)
  853. {
  854.     g.just = jj;
  855. }
  856. g_set_font(int jj)
  857. {
  858.     if (jj==0) return;
  859.     font_load_metric(jj);
  860.     g.fontn = jj;
  861. }
  862. g_set_hei(double h)
  863. {
  864.     if (h==0) {
  865.         gprint("========================************ HEIGHT SET TO ZERO\n");
  866.         return;
  867.     }
  868.     g.fontsz = h;
  869. }
  870. g_get_just(int *jj)
  871. {
  872.     *jj = g.just;
  873. }
  874. g_get_font(int *jj)
  875. {
  876.     *jj = g.fontn;
  877. }
  878. g_get_hei(double *h)
  879. {
  880.     *h = g.fontsz;
  881. }
  882. g_postscript(char *fname,double wx, double wy)
  883. {
  884. char inbuff[200];
  885. FILE *fptr;
  886. int i;
  887. char *s;
  888. double bx1=0,by1=0,bx2,by2,x,y,cx,cy;
  889.     g_get_type(inbuff);
  890.     if (strstr(inbuff,"PS")== NULL) {
  891.         g_get_xy(&cx,&cy);
  892.         g_box_stroke(cx,cy,cx+wx,cy+wy);
  893.         return;
  894.     }
  895.     fptr = fopen(fname,"r");
  896.     if (fptr==NULL) {
  897.         gprint("Unable to open input file {%s} \n",fname);
  898.         perror("Reason");
  899.         return;
  900.     }
  901.     for (;!feof(fptr);) {
  902.       if (fgets(inbuff,190,fptr)!=NULL) {
  903.         if (strncmp(inbuff,"%%BoundingBox:",14)==0) {
  904.             s = strtok(inbuff," :\t");
  905.             bx1 = atof(strtok(0," :\t"));
  906.             by1 = atof(strtok(0," :\t"));
  907.             bx2 = atof(strtok(0," :\t"));
  908.             by2 = atof(strtok(0," :\t"));
  909.         }
  910.       }
  911.     }
  912.  
  913.     g_devcmd("/GLESTATE save def \n");
  914.     g_devcmd("gsave\n");
  915.     g_devcmd("/a4small {} def /legal {} def\n");
  916.     g_devcmd("/letter {} def /note {} def /copypage {} def \n");
  917.     g_devcmd("/erasepage {} def /showpage {} def \n");
  918.     rewind(fptr);
  919.     g_gsave();
  920.     g_get_xy(&cx,&cy);
  921.     g_translate(cx,cy);
  922.     bx2 -= bx1;
  923.     by2 -= by1;
  924.     if (bx2==0 || by2==0) {gprint("Invalid EPS file\n"); return;}
  925.     g_move(0.0,0.0);
  926.     g_scale(wx/bx2,wx/bx2); /* use wy/by2 to change aspect ratio */
  927.     g_translate(-bx1,-by1);
  928.     g_devcmd("0 setgray 0 setlinecap 0 setlinewidth 0 setlinejoin\n");
  929.     g_devcmd("10 setmiterlimit [] 0 setdash\n");
  930.     for (;!feof(fptr);) {
  931.       if (fgets(inbuff,200,fptr)!=NULL) {
  932.         g_devcmd(inbuff);
  933.       }
  934.     }
  935.     fclose(fptr);
  936.     g_devcmd("grestore GLESTATE restore \n");
  937.     g_grestore();
  938. }
  939. g_devcmd(char *s)
  940. {
  941.     d_devcmd(s);
  942. }
  943.