#include<stdio.h>
#include<stdlib.h>  /* exit() */
#include<stdarg.h>
#include<string.h>
#include<math.h>

#include "fig2pdf.h"

void write_latex(char *text, int font, double size,
		 double llx, double lly, double angle, int colour, int depth);

extern double text_scale;

/* Special characters:
 *
 * Common to xfig and PDF: \n and \xxx (octal)
 *
 * PDF only: unbalanced () need escaping
 *           \ needs escaping
 *           \r, \t, \b, \f are special
 *
 */

void write_text(char *p){
  int i,debug;
  int obj,sub,colour,depth,pen,font,fl,x,y,posn;
  double size,angle,height,length,dx,dy;
  char *str,*p1,*p2;
  /* DANGER !!! */
  char buffer[1024];


  debug=flags&DEBUG_MASK;
  if (debug>2) fprintf(stderr,"write_text called\n");

  i=sscanf(p,"%d %d %d %d %d %d %lf %lf %d %lf %lf %d %d %n",
	 &obj,&sub,&colour,&depth,&pen,&font,&size,&angle,&fl,
	   &height,&length,&x,&y,&posn);

  if (i!=13){
    fprintf(stderr,"Error parsing text object\n");
    exit(1);
  }


  if ((fl&0x08)==1) {
    fprintf(stderr,"Skipping hidden text item\n");
    return;
  }

  if (font==-1){
    font=default_font;
    if (default_font_size!=0) size=default_font_size;
  }
  if (font>=35){
    fprintf(stderr,"Invalid font, skipping text item\n");
    return;
  }
  if (font>=0) fontmap[font]=1;
  str=p+posn;
  
  i=strlen(str)-5;
  if (str[i]=='\\') str[i]=0;
  else fprintf(stderr,"Error parsing text in text object\n");

  dx=72.0*x/1200;
  dy=pg[1]-72.0*y/1200;
  
  if ((fl&0x02)==2) {
    if ((fl&04)==4) font+=256;
    write_latex(str,font,size,dx,dy,angle,colour,depth);
    return;
  }

  /* The following lines are "correct" but sometimes underestimate */
  /*  length*=72.0/1200; */
  /*  height*=72.0/1200; */

  /* These lines at least correct for the two interpretations of font size */

  length*=80*text_scale/1200;
  height*=80*text_scale/1200;
  
  bb_fix(dx,dy,1);
  bb_fix(dx+length*cos(angle),dy+length*sin(angle),1);
  bb_fix(dx-height*sin(angle),dy+height*cos(angle),1);
  bb_fix(dx+length*cos(angle)-height*sin(angle),
	 dy+length*sin(angle)+height*cos(angle),1);

  i=sprintf(buffer,"BT /F%d %.2f Tf",font,size*text_scale);

  if (angle==0.0)
    i+=sprintf(buffer+i," %.2f %.2f Td",dx,dy);
  else
    i+=sprintf(buffer+i," %.5f %.5f %.5f %.5f %.2f %.2f Tm",cos(angle),sin(angle),
	       -sin(angle),cos(angle),dx,dy);

  if ((colour<-1)||(colour>MAX_COL)) colour=-1;

  if (colour==-1)
    i+=sprintf(buffer+i," 0 0 0 rg");
  else if (fig_colours[colour])
    i+=sprintf(buffer+i," %s rg",fig_colours[colour]);
  else
    fprintf(stderr,"Invalid text colour %d\n",colour);

  //  i+=sprintf(buffer+i,"\n(%s) Tj ET",str);

  /* Need to escape certain characters in string */

  i+=sprintf(buffer+i,"\n(");
  p1=p2=str;
  while(*p2){
    while ((*p2!='(')&&(*p2!=')')&&(*p2!=0x5c)&&(*p2)) p2++; /* \=0x5c */
    switch(*p2){
    case 0:
      break;
    case '(':
      i+=sprintf(buffer+i,"%.*s\\(",(int)(p2-p1),p1);
      p2++;
      p1=p2;
      break;
    case ')':
      i+=sprintf(buffer+i,"%.*s\\)",(int)(p2-p1),p1);
      p2++;
      p1=p2;
      break;
    case 0x5c:
      if (*(p2+1)=='n'){ /* leave \n untouched */
	p2+=2;
	break;
      }
      else if (*(p2+1)==0x5c){  /* leave \\ untouched */
	p2+=2;
	break;
      }
      else if ((*p2+1)&&(*(p2+1)>='0')&&((*p2+1)<='7')&&
	       (*p2+2)&&(*(p2+2)>='0')&&((*p2+2)<='7')&&
	       (*p2+3)&&(*(p2+3)>='0')&&((*p2+3)<='7')){
	p2+=4;
	break;
      }
      else{
	i+=sprintf(buffer+i,"%.*s\\",(int)(p2-p1),p1);
	p2++;
	p1=p2;
	break;
      }
    }
  }
  if (p1!=p2) i+=sprintf(buffer+i,"%s",p1);
  i+=sprintf(buffer+i,") Tj ET");


  OBJ_PRN("<</Length %d>> stream\n%s\nendstream\nendobj\n\n",i,buffer);

  add_obj(cref,depth);
}

