/*
 *                   customchar.c
 *
 *
 *     This procedure plots user-defined character
 *
 *     Marek Krawus, PCC, March '89
 */

#include "defn.h"

customchar()
{ 
  int     penUP = 0;
  int     x_exist = 0;
  int     y_exist = 0;
  float   dx, dy;
  float   X_delta, Y_delta;
  float   number;
  float   deltaX, deltaY;

  X_delta = (char_width + char_space) / 6.0;
  Y_delta = char_height / 8.0; 

  end_draw();

  fprintf(outfile, "  gsave\n");
  fprintf(outfile, "    newpath\n");
  fprintf(outfile, "      %g mm %g mm moveto\n", absX, absY);
  fprintf(outfile, "      %g rotate\n", char_angle);

  while (test_token())
   { 
      number = getval();
      if (number > - 99 && number < 99)
	{
	  switch (x_exist)
	   {
            case 0:
	      dx = number * X_delta;
	      x_exist = 1;
	      y_exist = 0;
	      break;
           
	    case 1:
	      dy = number * Y_delta;
	      y_exist = 1;
	      break;
           }
   
	  if (x_exist && y_exist)
	   {
	    switch (penUP)
	     {
	      case 0:
		fprintf(outfile, "      %g mm %g mm rmoveto\n", dx, dy);
		break;

              case 1:
		fprintf(outfile, "      %g mm %g mm rlineto\n", dx, dy);
		break;
             }
            x_exist = y_exist = 0;
           }
        }
     else             /* pen control numbers */
	{
          if (x_exist && !y_exist)
	      fprintf(stderr,"WARNING: unmatched X,Y increments in UC instruction\n");
          x_exist = y_exist = 0;
         
          if (number >= 99.0) penUP = 1;
	  if (number <= - 99.0) penUP = 0;
	}

   }

 fprintf(outfile, "    stroke\n");
 fprintf(outfile, "  grestore\n");
 deltaX = (char_width + char_space) * cos(char_angle * deg_rad);
 deltaY = (char_width + char_space) * sin(char_angle * deg_rad);
 absX += deltaX;
 absY += deltaY;
 fprintf(outfile, "%g mm %g mm moveto\n", absX, absY);
}
