home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume4 / xhpgl / part01 / hpgl.y < prev    next >
Encoding:
Lex Description  |  1989-07-16  |  5.9 KB  |  216 lines

  1. %{
  2. /*****************************************************************\
  3.  * FILE: hpgl.y   part of hpgl to X windows                      *
  4.  *                                                               *
  5.  * Written by Randy L. Yach aid by Jackie Harrison               *
  6.  *                                                               *
  7.  * Description:                                                  *
  8.  *    this is the yacc parser of HPGL syntax.  All plotting      *
  9.  *    line drawing and labeling are done in this procedure       *
  10. \*****************************************************************/
  11.  
  12. #include <stdio.h>
  13. #include <X/Xlib.h>
  14. #include <math.h>
  15.  
  16. /* need all these external global variables to interface with the many
  17.    X windows variables. */
  18. extern int hline;
  19. extern char yytext[];
  20. extern double xMin,xMax,yMin,yMax;
  21. extern int last_x, last_y;
  22. extern int x,y;
  23. extern int cur_pen,no_pen;
  24. extern int pen_down;
  25. extern int absolute;
  26. extern Color pen[];
  27. extern Window main_window;
  28. extern Font text_font;
  29. extern short font_offset;
  30. extern int minWidth,minHeight;
  31. extern Pattern line_type;
  32. extern line;
  33.  
  34. extern initialize_plotter();
  35.  
  36. /* line points used for line drawing */
  37. Vertex coordinate_points[2] =
  38.     {
  39.     0, 0, 0,
  40.     0, 0, 0
  41.     };
  42.  
  43. /* binary pattern definitions for drawing dashed lines */
  44. /* default |________|  */
  45. /* line 0  |        |  */
  46. /* line 1  |_       |  */
  47. /* line 2  |____    |  */
  48. /* line 3  |______  |  */
  49. /* line 4  |______ _|  */
  50. /* line 5  |_____ __|  */
  51. /* line 6  |____ _ _|  */
  52. unsigned short line_pattern[7] = {
  53.     0x00, 0x80, 0xf0, 0xfc, 0xfd, 0xfb, 0xf5
  54.     };
  55.  
  56. /* these variables are needed to calculate the default length
  57.    of the patterns used in the dashed lines */
  58. int pat_len;
  59. double pat_perc,tot_dist;
  60.  
  61. %}
  62.  
  63. %token LT LB PA PR PU PD SP SC DIGIT SEMICOLON COMMA NUMBER RESET
  64.  
  65. %start xhpgl_start
  66.  
  67. %union {
  68. int i;
  69. char sb[BUFSIZ];
  70. float f;
  71. }
  72.  
  73. %type <i> DIGIT SP
  74. %type <sb> LB
  75. %type <f> NUMBER
  76.  
  77. %%
  78.  
  79. xhpgl_start    : /* empty statement */
  80.            | xhpgl_start xhpgl_commands
  81.            ;
  82.  
  83. xhpgl_commands : plot_coordinates cordinate_list SEMICOLON
  84.            | SP SEMICOLON
  85.          { /* begin select pen */
  86.          /* set pen color from the SP command */
  87.          cur_pen = pen[$1].pixel;
  88.          } /* end select pen */
  89.            | SC scale_coordinates SEMICOLON
  90.            | text_labels
  91.            | LT line_type_change SEMICOLON
  92.            | RESET
  93.          { /* begin reset */
  94.          initialize_plotter();
  95.          } /* end reset */
  96.            ;
  97.  
  98. line_type_change : /* empty statement */ 
  99.                    { /* begin solid line */
  100.            /* set up solid line type */
  101.            line_type = XMakePattern(0xff,8,1);
  102.            } /* end solid line */
  103.          | DIGIT COMMA NUMBER
  104.                    { /* begin dashed line */
  105.            /* set up line pattern */
  106.            pat_perc=($3/100.0);
  107.            tot_dist=sqrt(pow((float)minWidth,(float)2)
  108.                 +pow((float)minHeight,(float)2));
  109.            pat_len=(int)((pat_perc*tot_dist)/8.0);
  110.            if (pat_len < 1) pat_len = 1;
  111.            line_type = XMakePattern(line_pattern[$1],8,pat_len);
  112.            } /* end dashed line */
  113.          | DIGIT COMMA DIGIT
  114.                    { /* begin dashed line */
  115.            /* set up line pattern */
  116.            pat_perc=($3/100.0);
  117.            tot_dist=sqrt(pow((float)minWidth,(float)2)
  118.                 +pow((float)minHeight,(float)2));
  119.            pat_len=(int)((pat_perc*tot_dist)/8.0);
  120.            if (pat_len < 1) pat_len = 1;
  121.            line_type = XMakePattern(line_pattern[$1],8,pat_len);
  122.            } /* end dashed line */
  123.          ;
  124.  
  125. text_labels    : LB
  126.                  { /* begin plot label */
  127.          /* plot the text given at the current X,Y locations */
  128.          XText(main_window,x,y-font_offset,$1,strlen($1),text_font,
  129.                cur_pen,no_pen);
  130.          } /* end plot label */
  131.            ;    
  132.  
  133. plot_coordinates : PA
  134.                { /* begin pen absolute coordinates */
  135.            absolute = 1;
  136.            } /* end pen absolute coordinates */
  137.          | PD
  138.            { /* begin pen relitave coordinates */
  139.            pen_down = 1;
  140.            } /* end pen relitave coordinates */
  141.          | PU
  142.            { /* begin pen up coordinates */
  143.            pen_down = 0;
  144.            } /* end pen up coordinates */
  145.          | PR
  146.            { /* begin pen down coordinates */
  147.            absolute = 0;
  148.            } /* end pen down coordinates */
  149.          ;
  150.  
  151. scale_coordinates : DIGIT COMMA DIGIT COMMA DIGIT COMMA DIGIT
  152.                     { /* begin user scale */
  153.             /* set up the user coodinate boundaries */
  154.             xMin = $1;
  155.             xMax = $3;
  156.             yMin = $5;
  157.             yMax = $7;
  158.                     } /* end user scale */
  159.                   ;
  160.  
  161. cordinate_list : /* empty statement */
  162.            | cordinate_list cordinate_def comma_opt
  163.            ;
  164.  
  165. comma_opt      : /* empty statement */
  166.            | COMMA
  167.            ;
  168.  
  169. cordinate_def  : DIGIT COMMA DIGIT
  170.                  { /* begin cordinate change */
  171.          /* get the new x,y points and convert then to absolute
  172.                     X window coodinates based on the user coordinates and 
  173.             plot scale. */
  174.          last_x = x;
  175.          last_y = y;
  176.          if (absolute)
  177.              { /* begin abs coordinate */
  178.              x=(double)(($1 - xMin)*(minWidth/(xMax - xMin)));
  179.              y=(double)(($3 - yMin)*(minHeight/(yMax - yMin)));
  180.              } /* end abs coordinate */
  181.          else
  182.              { /* begin relitave coordinate */
  183.              x=last_x+(double)(($1 - xMin)*(minWidth/(xMax - xMin)));
  184.              y=last_y+(double)(($3 - yMin)*(minHeight/(yMax - yMin)));
  185.              } /* end relitave coordinate */
  186.          y = minHeight - y;
  187.          
  188.                  /* if the pen is down, draw a line of the appropriate line
  189.             type between the last x,y and the current x,y
  190.             coordinates */
  191.                  if (pen_down)
  192.              { /* begin draw line */
  193.              coordinate_points[0].x = last_x;
  194.              coordinate_points[0].y = last_y;
  195.              coordinate_points[1].x = x;
  196.              coordinate_points[1].y = y;
  197.              XDrawDashed(main_window,coordinate_points,
  198.                2,1,1,cur_pen,line_type,GXcopy,AllPlanes);
  199.              } /* end draw line */
  200.          } /* end cordinate change */
  201.            ;
  202.  
  203. %%
  204. void yyerror(mess)
  205. char *mess;
  206. { /* begin parsing error */
  207.     fprintf(stderr,"Syntax error line %d token %s.\n",hline,yytext); 
  208.     exit();
  209. } /* end parsing error */
  210.  
  211. int yywrap()
  212. { /* begin */
  213.     return(1);
  214. } /* end */
  215.  
  216.