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

  1. #include "all.h"
  2. #include "mygraph.h"
  3. #include "justify.h"
  4. #include "color.h"
  5. /* for key command and gx(), gy() */
  6. extern double graph_x1,graph_y1,graph_x2,graph_y2;  /* in cm */
  7. extern double graph_xmin,graph_ymin,graph_xmax,graph_ymax; /* graph units */
  8. #define BEGINDEF extern
  9. #include "begin.h"
  10. #include <math.h>
  11. #define dbg if ((gle_debug & 64)>0)
  12. #define LARGE_NUM 1E30
  13. char *un_quote(char *ct);
  14. extern int gle_debug;
  15. int doskip(char *s,int *ct);
  16. double get_next_exp(TOKENS tk,int *ntk,int *curtok);
  17. #define kw(ss) if (strcmp(tk[ct],ss)==0)
  18. #define true (!false)
  19. #define false 0
  20. #define skipspace doskip(tk[ct],&ct)
  21. #define tok(n)  (*tk)[n]
  22. #define next_exp (get_next_exp((TOKENS) tk,&ntk,&ct))
  23. #define next_font ((ct+=1),pass_font(tk[ct]))
  24. #define next_marker ((ct+=1),pass_marker(tk[ct]))
  25. #define next_color ((ct+=1),pass_color(tk[ct]))
  26. #define next_fill ((ct+=1),pass_color(tk[ct]))
  27. #define next_str(s)  (ct+=1,strcpy(s,tk[ct]))
  28. #define next_vstr(s)  (ct+=1,mystrcpy(&s,tk[ct]))
  29. #define next_vquote(s) (ct+=1,mystrcpy(&s,un_quote(tk[ct])))
  30. #define next_quote(s) (ct+=1,strcpy(&s,un_quote(tk[ct])))
  31.  
  32. #define ifer if (erflg)
  33.  
  34. struct key_struct {
  35.     char lstyle[9];
  36.     long color,fill;
  37.     int marker;
  38.     double msize,lwidth;
  39.     char *descrip;
  40. };
  41. struct key_struct *kd[100];
  42.  
  43. begin_key(int *pln,long *pcode, int *cp)
  44. {
  45.     int nkd=0;
  46.     double koffsetx=0,koffsety=0,khei=0,zzhei;
  47.     char kpos[34];
  48.     int knobox=0,st;
  49.     int sl,ct,i,ncol,ntmp,np,c,*m,t,d,b;
  50.  
  51.     g_get_hei(&zzhei);
  52.  
  53.     begin_init();
  54.     for (;;) {
  55.     st = begin_token(&pcode,cp,pln,srclin,(char *) tk,&ntk,outbuff);
  56.     if (!st) break;
  57.     ct = 1;
  58.     while (ct<=ntk)  {
  59.         skipspace;
  60.              kw("OFFSET") {
  61.             koffsetx = next_exp;
  62.             koffsety = next_exp;
  63.         }
  64.     else     kw("NOBOX")     knobox = true;
  65.     else     kw("HEI")    khei = next_exp;
  66.     else     kw("POSITION") next_str(kpos);
  67.     else     kw("POS") next_str(kpos);
  68.     else     kw("JUSTIFY") next_str(kpos);
  69.     else {
  70.         if (ct==1) {
  71.             nkd++;
  72.             kd[nkd] = myallocz(sizeof(*kd[1]));
  73.         }
  74.         if (nkd==0) return;
  75.         kw("TEXT")      next_vquote(kd[nkd]->descrip);
  76.      else     kw("MARKER")      {kd[nkd]->marker = next_marker;
  77.         }
  78.      else     kw("MSIZE")    kd[nkd]->msize = next_exp;
  79.      else     kw("MSCALE")    kd[nkd]->msize = (next_exp) * zzhei;
  80.      else     kw("COLOR")     kd[nkd]->color = next_color;
  81.      else     kw("FILL")     kd[nkd]->fill = next_fill;
  82.      else     kw("LSTYLE")     next_str(kd[nkd]->lstyle);
  83.      else     kw("LINE")     strcpy(kd[nkd]->lstyle,"1");
  84.      else     kw("LWIDTH")     kd[nkd]->lwidth = next_exp;
  85.      else gprint("Unrecognised KEY sub command {%s} %d \n ",tk[ct],ct);
  86.     }
  87.     ct++;
  88.     }
  89.     if (!begin_next_line(pcode,cp)) break;
  90.     }
  91. draw_it:
  92.     draw_key(nkd,koffsetx,koffsety,kpos,khei,knobox);
  93. }
  94. draw_key(int nkd, double koffsetx, double koffsety, char *kpos
  95.     ,double khei, int knobox)
  96. {
  97.     int kl=0,km=0,kf=0;
  98.     int i;
  99.     long old_color;
  100.     double cx,cy,maxwid=0,z;
  101.     double ox,oy,bl,br,bu,bd,savex,savey;
  102.     double sx=0,sy=0;
  103.     double cr,savelw,midx,midy;
  104.  
  105.     g_get_xy(&savex,&savey);
  106.     if (nkd==0) return;
  107.     if (khei==0) g_get_hei(&khei);
  108.     cr = 1.2*khei;
  109.     for (i=nkd;i>=1;i--) {
  110.         if (kd[i]->lstyle[0]==0) if (kd[i]->lwidth>0)
  111.             kd[i]->lstyle[0]='1';
  112.         if (kd[i]->lstyle[0]!=0) kl = true;
  113.         if (kd[i]->lwidth>0) kl = true;
  114.         if (kd[i]->marker!=0) km = true;
  115.         if (kd[i]->fill!=0) kf = true;
  116.     }
  117.  
  118.     g_set_hei(khei);
  119.     for (i=nkd;i>=1;i--) {
  120.         if (kd[i]->descrip!=NULL) {
  121.             g_measure(kd[i]->descrip,&bl,&br,&bu,&bd);
  122.             if (maxwid < br) maxwid = br;
  123.         }
  124.     }
  125.     sx = 0;
  126.     if (kl) sx = sx + 2*cr;
  127.     if (km) sx = sx + 1.5*cr;
  128.     if (kf) sx = sx + 1.3*cr;
  129.     sx = sx + maxwid;
  130.     sx = sx + 1.2*cr;
  131.     sy = nkd*cr + 1.2*cr-.3*khei;
  132.     if (koffsetx==0 && koffsety==0) {
  133.       midx = graph_x1 + (graph_x2-graph_x1)/2;
  134.         midy = graph_y1 + (graph_y2-graph_y1)/2;
  135.       if (strcmp(kpos,"TL")==0) {ox = graph_x1;oy = graph_y2-sy;}
  136.       else if (strcmp(kpos,"BL")==0) {ox = graph_x1; oy = graph_y1;}
  137.       else if (strcmp(kpos,"BR")==0) {ox = graph_x2-sx;oy = graph_y1;}
  138.       else if (strcmp(kpos,"TR")==0) {ox = graph_x2-sx; oy = graph_y2-sy;}
  139.       else if (strcmp(kpos,"TC")==0) {ox = midx-sx/2; oy = graph_y2-sy;}
  140.       else if (strcmp(kpos,"BC")==0) {ox = midx-sx/2; oy = graph_y1;}
  141.       else if (strcmp(kpos,"RC")==0) {ox = graph_x2-sx; oy = midy-sy/2;}
  142.       else if (strcmp(kpos,"LC")==0) {ox = graph_x1; oy = midy-sy/2;}
  143.       else if (strcmp(kpos,"CC")==0) {ox = midx-sx/2; oy = midy-sy/2;}
  144.       else {
  145.         if (strlen(kpos)>0) gprint("Expecting POS BL,BR,TR or TL \n");
  146.         ox = graph_x2-sx; oy = graph_y2-sy;
  147.       }
  148.     } else {
  149.         g_get_xy(&ox,&oy);
  150.         ox = ox + koffsetx;
  151.         oy = oy + koffsety;
  152.     }
  153.     g_get_color(&old_color);
  154.     g_set_fill(COLOR_WHITE);
  155.     if (!knobox) g_box_fill(ox,oy,ox+sx,oy+sy);
  156.  
  157.     g_set_color(old_color);
  158.     for (i=nkd; i>=1; i--)  {
  159.         g_move(ox+.6*cr,oy+.6*cr+cr*(nkd-i));
  160.         g_get_xy(&cx,&cy);
  161.         if (kd[i]->color!=0) g_set_color(kd[i]->color);
  162.         if (km) {
  163.             g_rmove(cr/2,khei*.35);
  164.             z = kd[i]->msize;
  165.             if (z==0) z = khei;
  166.             if (kd[i]->marker!=0) g_marker(kd[i]->marker,z);
  167.             g_rmove(cr,-khei*.35);
  168.         }
  169.         if (kl) {
  170.             g_set_line_style(kd[i]->lstyle);
  171.             g_get_line_width(&savelw);
  172.             g_set_line_width(kd[i]->lwidth);
  173.             g_rmove(0.0,cr*.3);
  174.             if (kd[i]->lstyle[0]==0) g_rmove(1.5*cr,0.0);
  175.             else g_rline(1.5*cr,0.0);
  176.             g_rmove(cr/2,-cr*.3);
  177.             g_set_line_style("1");
  178.             g_set_line_width(savelw);
  179.         }
  180.         if (kf) {
  181.             if (kd[i]->fill!=0) {
  182.                 g_set_fill(kd[i]->fill);
  183.                 g_get_xy(&cx,&cy);
  184.                 g_box_fill(cx,cy,cx+cr*.7,cy+cr*.66);
  185.                 g_box_stroke(cx,cy,cx+cr*.7,cy+cr*.66);
  186.             }
  187.             g_rmove(1.3*cr,0.0);
  188.         }
  189.         g_get_xy(&cx,&cy);
  190.         if (kd[i]->color!=0) g_set_color(old_color);
  191.         g_set_just(JUST_LEFT);
  192.         if (kd[i]->descrip!=NULL) g_text(kd[i]->descrip);
  193.     }
  194.     if (!knobox) g_box_stroke(ox,oy,ox+sx,oy+sy);
  195.     g_move(savex,savey);
  196. }
  197.  
  198.