/*
 *
 *                           textps.c
 *
 *
 */
 
#include "defn.h"

#define MAXBUFSIZE 100

textps(type)
int     type;
{
    char    string[4];
    char    buffer[MAXBUFSIZE];  /* holds text for one LB instruction */
    float   Xch, Ych;            /* start of text (for proportional spacing) */ 
    int	    intval;
    int     i;                   /* number of char in buffer */
    int     k;                   /* number of special char '\' in buffer */

    end_draw();

    if (type == TEXT)
    {
      if (CURR_LB_PT == FALSE)
      { 
	absX = CR_point_X;  /* see explanation in defn.h  */
	CURR_LB_PT = TRUE;
      }
      if (PROP_SPACE == FALSE) 
       {
	while ((ch = getc(stream)) != EOF)
	{
	    if (((int)ch > 31) && ((int)ch < 127))
	    {
	      if (ch == '(' || ch == ')')
	            fprintf(outfile, "%g %g 1 %g (\\%c) Text\n", absX, absY, char_angle, ch);
              else
	            fprintf(outfile, "%g %g 1 %g (%c) Text\n", absX, absY, char_angle, ch);
 
 	      absX += (char_width + char_space) * cos(char_angle * deg_rad);
 	      absY += (char_width + char_space) * sin(char_angle * deg_rad);
            }
	    else
	    {
	      if (ch == CR)
	      {
		absX = CR_point_X;
		absY = CR_point_Y;
              } 
	      if (ch == LF)  /* interline spacing   */
	      {
 	        absX += 2 * (char_height) * sin(char_angle * deg_rad);
 	        absY -= 2 * (char_height) * cos(char_angle * deg_rad);
		CR_point_X -= 2 * (char_height) * sin(char_angle * deg_rad);
		CR_point_Y -= 2 * (char_height) * cos(char_angle * deg_rad);
	      }
            }
            if (ch == EOL) break;

	}                      /* end of while ((ch = getc(stream)) != EOF)   */
      } 
      else      /*  PROP_SPACE ==  TRUE   */
      {
	Xch = absX;
	Ych = absY;
	i = 0;
	k = 0;
	buffer[i] = NULL;
	while ((ch = getc(stream)) != EOF)
	{
	    if (((int)ch > 31) && ((int)ch < 127))
            {	
	      if (ch == '(' || ch == ')') { buffer[i++] = '\134'; k++; }
	      buffer[i++] = ch;
	      if ((i >= MAXBUFSIZE - 1)&&(ch != EOF)) 
	      {
		  fprintf(stderr,"ERROR: textps -> number of characters in LB instruction exeeds max = %d\n",MAXBUFSIZE); 
		  buffer[i] = '\0';
	          fprintf(outfile,"%g %g 1 %g (%s) Text\n",
		      Xch, Ych, char_angle, buffer);
		  /* set correct absX, absY Xch, Ych */
 	          absX += (i - k) * (char_width + char_space) * cos(char_angle * deg_rad);
 	          absY += (i - k) * (char_width + char_space) * sin(char_angle * deg_rad);
		  Xch = absX;  Ych = absY;
                  i = 0; k = 0; buffer[i] = NULL; 

              }
              if (ch == EOL)
	      {
	         fprintf(outfile,"%g %g 1 %g (%s) Text\n",
		      Xch, Ych, char_angle, buffer);
                 /* set correct absX, absY    */
 	          absX += (i - k) * (char_width + char_space) * cos(char_angle * deg_rad);
 	          absY += (i - k) * (char_width + char_space) * sin(char_angle * deg_rad);
                  break;
               }
            }    /*  end of (int)char .....    */
	  else     /* cntrl characters */
	{
	  if (ch == EOL)
	  {
		 buffer[i] = '\0';
	         fprintf(outfile,"%g %g 1 %g (%s) Text\n",
		      Xch, Ych, char_angle, buffer);
 	          absX += (i - k) * (char_width + char_space) * cos(char_angle * deg_rad);
 	          absY += (i - k) * (char_width + char_space) * sin(char_angle * deg_rad);
                  break;
          }
	  if (ch == CR || ch == LF)
	  {
		  buffer[i] = '\0';
	          fprintf(outfile,"%g %g 1 %g (%s) Text\n",
		      Xch, Ych, char_angle, buffer);
          }
	  if (ch == CR)
	  {
		Xch = absX = CR_point_X;
		Ych = absY = CR_point_Y;
	  }
	  if (ch == LF)
	  {
 	        Xch = absX += 2 * (char_height) * sin(char_angle * deg_rad);
 	        Ych = absY -= 2 * (char_height) * cos(char_angle * deg_rad);
		CR_point_X -= 2 * (char_height) * sin(char_angle * deg_rad);
		CR_point_Y -= 2 * (char_height) * cos(char_angle * deg_rad);
	  }
          i = 0; k = 0; buffer[i] = NULL; 

	}   /* end of cntrl characters  */
       }    /*  end of while   */	
      }     /* end of else PROP_SPACE  */

    }                          /* end of type == TEXT    */
    


/************************  not  HPGL2PS   ******************************/
/*    else	*/		/* Must be a MARK */
/*    {
	int     symb_num;

	if (test_token())
	    symb_num = getval();
	else
	{
	    fprintf(stderr,
		    "Error: expecting a symbol number not %c (%d)",
		    symb_num, symb_num);
	    exit(1);
	}
	intval = (int)(getval() + 0.5);
	switch (intval)
	{
	case 0:
	    strcpy(string, "*");
	    break;

	case 1:
	    strcpy(string, "+");
	    break;

	case 2:
	    strcpy(string, "#");
	    break;

	case 3:
	    strcpy(string, "@");
	    break;

	case 4:
	    strcpy(string, "%");
	    break;

	case 5:
	    strcpy(string, "|");
	    break;

	case 6:
	    strcpy(string, "=");
	    break;

	case 7:
	    strcpy(string, "&");
	    break;

	case 9:
	    strcpy(string, "O");
	    break;

	case 10:
	    strcpy(string, "0");
	    break;

	case 11:
	    strcpy(string, "Y");
	    break;

	case 12:
	    strcpy(string, "X");
	    break;

	case 13:
	    strcpy(string, "Z");
	    break;

	case 14:
	    strcpy(string, "S");
	    break;

	case 15:
	    strcpy(string, "Q");
	    break;

	default:
	    fprintf(stderr, "Warning symbol number is %d\n", symb_num);
	    strcpy(string, "*");
	    break;
	}
	fprintf(outfile, "%g %g 5 %g (%s) Text\n", absX, absY, char_angle, string);
    }
*************************************************************************/ 
}
