# include "data.h"

int textline (int argc, char **argv) {

  int N, size, FracPositions;
  char name[64], buffer[128];
  double x, y, angle;
  int Ngraph, Xgraph;
  Graphdata graphmode;

  Ngraph = -1;
  if (N = get_argument (argc, argv, "-n")) {
    remove_argument (N, &argc, argv);
    Ngraph = atof (argv[N]);
    remove_argument (N, &argc, argv);
  }
  if (!GetGraph (&graphmode, &Xgraph, &Ngraph)) return (FALSE);

  if (N = get_argument (argc, argv, "-fn")) {
    remove_argument (N, &argc, argv);
    strcpy (name, argv[N]);
    remove_argument (N, &argc, argv);
    size = atof (argv[N]);
    remove_argument (N, &argc, argv);
    write (Xgraph, "FONT", 4);
    sprintf (buffer, "%s", name);
    write (Xgraph, buffer, 16);
    sprintf (buffer, "%d", size);
    write (Xgraph, buffer, 16);
  } 

  /* FracPositions uses coordinates of 0-1 relative to axis range */
  FracPositions = FALSE;
  if (N = get_argument (argc, argv, "-frac")) {
    remove_argument (N, &argc, argv);
    FracPositions = TRUE;
  } 

  angle = 0.0;
  if (N = get_argument (argc, argv, "-rot")) {
    remove_argument (N, &argc, argv);
    angle = atof (argv[N]);
    remove_argument (N, &argc, argv);
  } 

  if (argc != 4) {
    fprintf (stderr, "USAGE: text x y (line) [-fn (font) size] [-rot angle]\n");
    return (FALSE);
  }

  if (strlen (argv[3]) > 127) {
    fprintf (stderr, "labels currently limited to 127 chars\n");
    return (FALSE);
  }

  x = atof (argv[1]);
  y = atof (argv[2]);
  
  if (FracPositions) {
    x =  x * (graphmode.xmax - graphmode.xmin) + graphmode.xmin;
    y =  y * (graphmode.ymax - graphmode.ymin) + graphmode.ymin;
  }    

  write (Xgraph, "PTXT", 4);
  sprintf (buffer, "%f", x);
  write (Xgraph, buffer, 16);
  sprintf (buffer, "%f", y);
  write (Xgraph, buffer, 16);
  sprintf (buffer, "%f", angle);
  write (Xgraph, buffer, 16);

  bzero (buffer, 128);
  strcpy (buffer, argv[3]);
  write (Xgraph, buffer, 128);

  return (TRUE);
}
  
