# include "Ximage.h"
# define PADDING 10
# define XOFFSET 60
# define YOFFSET 60

static char *name = "$Name: not supported by cvs2svn $";

int PSit (Graphic *graphic, Layout *layout, int Raw) {
  
  FILE *f;
  int i, pageMode, scaleMode;
  double scale;
  char filename[1024], pagename[1024], *version;

  /* expect a line telling the number of bytes and a filename */
  KiiScanMessage (layout[0].Ximage, "%s %s %d %d", filename, pagename, &scaleMode, &pageMode);

  if (pageMode == KAPA_PS_NEWPAGE) {
    f = fopen (filename, "a+");
  } else {
    f = fopen (filename, "w");
  }
  if (f == (FILE *) NULL) {
    fprintf (stderr, "failed to open %s for output\n", filename);
    return (TRUE);
  }
  
  if (!USE_XWINDOW) {
    fprintf (stderr, "PS not working yet\n");
    return (TRUE);
  }

  if (scaleMode) {
    scale = MIN ((500.0/(double)layout[0].picture.dx), (700.0/(double)layout[0].picture.dy));
  } else {
    scale = 72.0 / 96.0; /* ratio of screen pixels to points */
  }

  switch (pageMode) {
    case KAPA_PS_NEWPLOT:
      fprintf (f, "%%!PS-Adobe-2.0 EPSF-2.0\n");
      fprintf (f, "%%%%Title: %s\n", filename);
      version = strip_version (name);
      fprintf (f, "%%%%Creator: Ki'i (%s)\n", version);
      free (version);
      fprintf (f, "%%%%BoundingBox: %d %d %.0f %.0f\n", 
	       XOFFSET - PADDING, YOFFSET - PADDING, 
	       scale*layout[0].picture.dx + PADDING + XOFFSET, 
	       scale*layout[0].picture.dy + PADDING + YOFFSET);
      fprintf (f, "%%%%Pages: 1\n");
      fprintf (f, "%%%%DocumentFonts:\n");
      fprintf (f, "%%%%EndComments\n");
      fprintf (f, "%%%%EndProlog\n");
      fprintf (f, "%%%%Page: %s\n\n", pagename);
      break;

    case KAPA_PS_NEWPAGE:
      fprintf (f, "%%%%Page: %s\n\n", pagename);
      break;

    case KAPA_PS_RAWPAGE:
      break;
  } 

  fprintf (f, "gsave %% encloses picture\n");
  fprintf (f, "%% local abbreviations\n");
  fprintf (f, "/Times-Roman findfont 18 scalefont setfont\n");
  fprintf (f, "/T {moveto show stroke} def\n");
  fprintf (f, "/B { newpath moveto dup 0 exch rlineto exch dup 0 rlineto exch -1 mul\n");
  fprintf (f, " 0 exch rlineto -1 mul 0 rlineto closepath stroke } def\n");
  fprintf (f, "/C {0 360 arc stroke} def\n");
  fprintf (f, "/L {newpath moveto lineto stroke} def\n\n");

  if (pageMode != KAPA_PS_RAWPAGE) {
    fprintf (f, " %d %d translate\n", XOFFSET, YOFFSET);
    fprintf (f, "  %f  %f scale\n", scale, scale);
  }

  fprintf (f, " newpath 0 0 moveto %d 0 lineto %d %d lineto 0 %d lineto closepath clip\n\n", 
	   layout[0].picture.dx, layout[0].picture.dx, layout[0].picture.dy, layout[0].picture.dy);
  fprintf (f, "gsave %% encloses image\n");
  fprintf (f, "%d %d 8\n", layout[0].picture.dx, layout[0].picture.dy);
  fprintf (f, "[1 0 0 1 0 0]\n");
  fprintf (f, "{currentfile %d string readhexstring pop} image\n\n", layout[0].picture.dx);

  /******** First we draw the picture itself ********/
  /* in !USE_XWINDOW, we'll have to change this to use the JPEG function */
  switch (graphic[0].Nbits) {
  case 8:
    ConvertPixmap8 (layout, f);
    break;
  case 16:
    ConvertPixmap16 (layout, f);
    break;
  case 24:
    ConvertPixmap24 (layout, f);
    break;
  case 32:
    ConvertPixmap32 (layout, f);
    break;
  }

  fprintf (f, "grestore %% end of image\n");
  fprintf (f, "stroke\n");
  fprintf (f, "%% plot overlay objects\n");
  fprintf (f, "1 setgray\n");

  for (i = 0; i < NOVERLAYS; i++) {
    if (OVERLAY[i]) {
      fprintf (f, "%% overlay %d\n", i);
      DrawOverlay (graphic, layout, i, f, 0);
    }
  }
  fprintf (f, "0 setgray\n");
  for (i = 0; i < NOVERLAYS; i++) {
    if (OVERLAY[i]) {
      fprintf (f, "%% overlay %d\n", i);
      DrawOverlay (graphic, layout, i, f, 1);
    }
  }
  
  fprintf (f, "grestore %% end of picture\n");

  if (pageMode != KAPA_PS_RAWPAGE) fprintf (f, "showpage\n");

  fclose (f);

  return (TRUE);

}

