# include "Ximage.h"
# define DrawLine(X1,Y1,DX,DY) (fprintf (f, " %6.2f %6.2f %6.2f %6.2f L\n", X1, Y1, X1+DX, Y1+DY))

int PSFrame (KapaGraphWidget *graph, FILE *f) {
  
  int i, P, IsLabel, fontsize;
  double fx, fy, dfx, dfy, range, major, minor, first, next;
  char *fontname;
  Graphic *graphic;

  graphic = GetGraphic();

  /* each axis is drawn independently */
  fontname = GetRotFont (&fontsize);
  fprintf (f, "1 setlinewidth\n");
  for (i = 0; i < 4; i++) {
    fx = graph[0].axis[i].fx;
    fy = graphic->dy - graph[0].axis[i].fy;
    dfx = graph[0].axis[i].dfx;
    dfy = -graph[0].axis[i].dfy;
    P = hypot ((double)graph[0].axis[(i+1)%2].dfx, (double)graph[0].axis[(i+1)%2].dfy);

    if (graph[0].axis[i].isaxis) { DrawLine (fx, fy, dfx, dfy); }
    
    if (graph[0].axis[i].areticks) {
      range = graph[0].axis[i].max - graph[0].axis[i].min;
      AxisTickScale (&graph[0].axis[i], &major, &minor);

      first = minor*((int)(graph[0].axis[i].min/minor));
      if ((range > 0) && (first < graph[0].axis[i].min)) {
	first += minor;
      }
      if ((range < 0) && (first > graph[0].axis[i].min)) {
	first -= minor;
      }
      for (next = first; ((range > 0) && (next <= graph[0].axis[i].max)) || ((range < 0) && (next >= graph[0].axis[i].max));) {
	IsLabel = FALSE;
	if ((fabs((int)(next/major) - (next/major)) < 0.5*(minor/major)) || 
	    (fabs ((int)((next + 0.5*minor)/major) - (next/major)) < 0.5*(minor/major)) ||
	    (fabs ((int)((next - 0.5*minor)/major) - (next/major)) < 0.5*(minor/major)))
	  IsLabel = graph[0].axis[i].islabel;
	PSTick (f, fx, fy, dfx, dfy, P, graph[0].axis[i].min, graph[0].axis[i].max, next, IsLabel, i);
	if (range > 0) 
	  next += minor;
	else 
	  next -= minor;
      }
    }
  }
  return (TRUE);
}

void PSTick (FILE *f, double fx, double fy, double dfx, double dfy, int P, double min, double max, double value, int mode, int naxis) {
  
  int pos, dir, fontsize;
  double size, n, x, y, dx, dy;
  char string[64], *fontname;

  pos = size = 0;

  if (mode == 1) { size = MAX (0.02, 7.0 / P); }
  if (mode == 0) { size = MAX (0.01, 4.0 / P); }
  
  n = P / sqrt ((double)(dfx*dfx + dfy*dfy));
  x = fx + (value-min)*dfx/(max - min);
  y = fy + (value-min)*dfy/(max - min);

  dir = -1;
  if ((naxis == 0) || (naxis == 1)) dir *= -1;
  dx = dir*size*dfy*n;	
  dy = dir*size*dfx*n;
  
  DrawLine (x, y, dx, dy);

  if (mode == 1) {
    fontname = GetRotFont (&fontsize);

    /* temporarily assume rectilinear axes */
    if (naxis == 0) { dx = 0; dy = -dir*(0.8*fontsize + 1); pos = 1; }
    if (naxis == 2) { dx = 0; dy = -dir*(0.8*fontsize + 1); pos = 7; }

    if (naxis == 1) { dy = 0; dx = -(0.8*fontsize + 1); pos = 3; }
    if (naxis == 3) { dy = 0; dx = +(0.8*fontsize + 1); pos = 5; }

    x = fx + (value-min)*dfx/(max - min) + dx;
    y = fy + (value-min)*dfy/(max - min) + dy;
    if (fabs(value) < 1e-14) { value = 0.0; }
    sprintf (string, "%g", value);
    PSRotText (f, (int)x, (int)y, string, pos, 0.0);
  }
}

  /* 
  dx = size*dfy*n;	
  dy = size*dfx*n;
  DrawLine (x, y, dx, dy);
  
  dx = -size*dfy*n;	
  dy = -size*dfx*n;
  DrawLine (x, y, dx, dy);
  */
  
