/*
 *                  shade.c
 *
 *  this is the set of functions which implement FT, RA, RR commands
 * 
 *  Marek Krawus, PCC, March '89
 *
 */

#include "defn.h"

filltype()
{
  int     typ;
  float   spacing;
  int     spacing_flag = 0;
  int     angle;
  int     angle_flag = 0;

  if (test_token())  {
     typ = getval();
     if (typ < 1 || typ > 5)  {
	 fprintf(stderr, "WARNING: type parameter in FT instruction is out \
			  of range\n");
         return;
     }
     if (typ == 5) return;
     if (test_token())  {
	 spacing = getval();
	 if (spacing < 0 || spacing > 32767)  {
	    spacing_flag = 0;
	    fprintf(stderr,"WARNING: spacing parameter in FT instruction \
			    is out of range\n");
         }  else   {
	    spacing_flag = 1;
         }
     }
     if (test_token())  {
	 angle = getval();
	 if ((angle % 45) != 0) {
	     angle_flag = 0;
	     fprintf(stderr,"WARNING: angle parameter in FT instruction \
			    is wrong\n");
         }  else   {
	     angle_flag = 1;
         }
     }

     FILL_TYPE = typ;
     if (spacing_flag == 1)  FILL_SPACING = spacing;
     if (angle_flag == 1) FILL_ANGLE = angle % 180;
	  

  }  else  {
     FILL_TYPE = 1;
  }

}

rectangle_shade( kind )
int   kind;
{
 float    x_inc;
 float    y_inc;
 float    spacing;

 if (test_token())  x_inc = getval();
 else    {
    fprintf(stderr,"ERROR: number of parameters in RR or RA instruction is not \
		      equal 2\n");
      return;
 }
 if (test_token())  y_inc = getval();
 else    {
    fprintf(stderr,"ERROR: number of parameters in RR or RA instruction is not \
		       equal 2\n");
      return;
 }
 if ((x_inc < -32768.0 || x_inc > 32767.9999) || (y_inc < -32768.0 || y_inc > 32767.9999))   {
  fprintf(stderr,"ERROR: parameters in RR or RA instruction is out of range\n");
     return;
 }

 if ((FILL_TYPE == 1)||(FILL_TYPE == 2)||(FILL_SPACING == 0.0))
    spacing = PEN_THICKNESS * RESOLUTION * XSCALE_ABS;       /*  in mm  */

 else
   {
     spacing = FILL_SPACING * XSCALE;                   /*  in mm  */

   }

 if ((FILL_TYPE == 1)||(FILL_TYPE == 2))
  spacing /= 2;               /* because solid bi- or unidirectional shading  */

 if (USER_UNITS == 0)   {
          x_inc = floor(x_inc);
          y_inc = floor(y_inc);
 }

 x_inc *= XSCALE;
 y_inc *= YSCALE;

 if (kind == ABSOLUTE) {
          x_inc -= absX;
	  y_inc -= absY;
 }
 
 end_draw();
 fprintf(outfile, "%g %g %g %g %d %g %d RR\n", absX, absY, x_inc, y_inc,
                  FILL_TYPE, spacing, FILL_ANGLE);
}



rectangle_edge( kind )
int   kind;
{
 float    x_inc;
 float    y_inc;

 if (test_token())  x_inc = getval();
 else    {
    fprintf(stderr,"ERROR: number of parameters in EA or ER instruction is not \
		      equal 2\n");
      return;
 }
 if (test_token())  y_inc = getval();
 else    {
    fprintf(stderr,"ERROR: number of parameters in EA or ER instruction is not \
		       equal 2\n");
      return;
 }
 if ((x_inc < -32768.0 || x_inc > 32767.9999) || (y_inc < -32768.0 || y_inc > 32767.9999))   {
  fprintf(stderr,"ERROR: parameters in EA or ER instruction is out of range\n");
     return;
 }

 if (USER_UNITS == 0)   {
          x_inc = floor(x_inc);
          y_inc = floor(y_inc);
 }

 x_inc *= XSCALE;
 y_inc *= YSCALE;

 if (kind == ABSOLUTE) {
          x_inc -= absX;
	  y_inc -= absY;
 }
 
 end_draw();
 fprintf(outfile, "%g %g %g %g EE\n", absX, absY, x_inc, y_inc);

}
