/*
 *
 *                 user_unit.c
 *
 *
 *
 *      establish user units
 *
 *   Marek Krawus,  PCC,  March '89
 *
 */

#include "defn.h"

user_unit(count, z1, z2, z3, z4)
float  z1, z2, z3, z4;
int    count;
{
  if (count < 4)
     {
       fprintf(stderr, "ERROR: wrong number of parameters in SC instruction\n");
       return;
     }

   USER_UNITS = TRUE;
   U1x = z1;
   U2x = z2;
   U1y = z3;
   U2y = z4;

   redefineP1P2();
}


redefineP1P2()
{
  end_draw();

  /*  current point  keeps the same physically place on the sheet */

  if (USER_UNITS) 
    { 
      XSCALE = ((P2x - P1x) / (U2x - U1x)) * XSCALE_ABS;
      YSCALE = ((P2y - P1y) / (U2y - U1y)) * YSCALE_ABS;

      if (Xshift != 0.0 || Yshift != 0.0)
      {
	      fprintf(outfile, "%g mm %g mm translate\n", - Xshift, - Yshift);
	      absX += Xshift;
	      absY += Yshift;
	      CR_point_X += Xshift;
	      CR_point_Y += Yshift;
      }

      Xshift = P1x * XSCALE_ABS - U1x * XSCALE;
      Yshift = P1y * YSCALE_ABS - U1y * YSCALE;
      if (Xshift != 0.0 || Yshift != 0.0)
      {
	     fprintf(outfile, "%g mm %g mm translate\n", Xshift, Yshift);
	      absX -= Xshift;
	      absY -= Yshift;
	      CR_point_Y -= Yshift;
      }
      CR_point_X = 0.0;
    }
  else   /*  USER_UNIT == FALSE  */ 
    {
      XSCALE = XSCALE_ABS;
      YSCALE = YSCALE_ABS;

      if (Xshift != 0.0 || Yshift != 0.0)
      {
	     fprintf(outfile, "%g mm %g mm translate\n", - Xshift, - Yshift);
	      absX += Xshift;
	      absY += Yshift;
	      CR_point_X += Xshift;
	      CR_point_Y += Yshift;
      }
      CR_point_X = 0.0;
      Xshift = Yshift = 0.0;

      U1x = P1x;
      U1y = P1y;
      U2x = P2x;
      U2y = P2y;
    }

  CURR_LB_PT = FALSE;  /* flag for labeling */

  if ( SR_bad == TRUE )
  {
    char_width = (U2x - U1x) * XSCALE * rel_WIDTH / 100.0;
    char_height = (U2y - U1y) * YSCALE * rel_HEIGHT / 100.0;
    char_space = char_width * (1 / .67 - 1);
    fprintf(outfile, "/%s %g %g %g DefFont\n",
		      font, chWfact * char_width, chHfact * char_height,
		      char_slant);
    SR_bad = FALSE;
  }

}
