Index: trunk/Ohana/src/kii/button/PSit.c
===================================================================
--- trunk/Ohana/src/kii/button/PSit.c	(revision 5852)
+++ trunk/Ohana/src/kii/button/PSit.c	(revision 10838)
@@ -1,25 +1,25 @@
 # include "Ximage.h"
-/* Notice: we are using a fixed border size of 10 pt */
-void ConvertPixmap8  (Layout *layout, FILE *f);
-void ConvertPixmap16 (Layout *layout, FILE *f);
-void ConvertPixmap24 (Layout *layout, FILE *f);
-void ConvertPixmap32 (Layout *layout, FILE *f);
+# 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;
+  int i, pageMode, scaleMode;
   double scale;
-  char filename[1024];
+  char filename[1024], pagename[1024], *version;
   int Nbytes, status;
 
   /* expect a line telling the number of bytes and a filename */
-  status = read (layout[0].Ximage, filename, 16);
-  filename[16] = 0;
-  sscanf (filename, "%*s %d", &Nbytes);
-  status = read (layout[0].Ximage, filename, Nbytes);
-  filename[status] = 0; /* make the string easy to parse */
+  KiiScanMessage (layout[0].Ximage, "%s %s %d %d", filename, pagename, &scaleMode, &pageMode);
 
-  f = fopen (filename, "w");
+  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);
@@ -32,17 +32,35 @@
   }
 
-  scale = MIN ((500.0/(double)layout[0].picture.dx), (700.0/(double)layout[0].picture.dy));
-  if (!Raw) {
-    fprintf (f, "%%!PS-Adobe-2.0 EPSF-2.0\n");
-    fprintf (f, "%%%%Title: %s\n", layout[0].file);
-    fprintf (f, "%%%%Creator: Ki'i (0.95)\n");
-    fprintf (f, "%%%%BoundingBox: 10 40 %d %d \n", (int)(scale*layout[0].picture.dx + 30), (int)(scale*layout[0].picture.dy + 60));
-    fprintf (f, "%%%%Pages: 1\n");
-    fprintf (f, "%%%%DocumentFonts:\n");
-    fprintf (f, "%%%%EndComments\n");
-    fprintf (f, "%%%%EndProlog\n");
-    fprintf (f, "%%%%Page: 1 1\n\n");
+  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");
@@ -55,6 +73,6 @@
   fprintf (f, "/L {newpath moveto lineto stroke} def\n\n");
 
-  if (!Raw) {
-    fprintf (f, " 20 50 translate\n");
+  if (pageMode != KAPA_PS_RAWPAGE) {
+    fprintf (f, " %d %d translate\n", XOFFSET, YOFFSET);
     fprintf (f, "  %f  %f scale\n", scale, scale);
   }
@@ -105,5 +123,5 @@
   fprintf (f, "grestore %% end of picture\n");
 
-  if (!Raw) fprintf (f, "showpage\n");
+  if (pageMode != KAPA_PS_RAWPAGE) fprintf (f, "showpage\n");
 
   fclose (f);
@@ -113,261 +131,2 @@
 }
 
-# if (0)
-void ConvertPixmap (Layout *layout, File *f) {
-  
-  /* set up expansions */
-  expand = expand_in = expand_out = 1.0;
-  if (layout[0].expand == 0)
-    layout[0].expand = 1;
-  if (layout[0].expand > 0) {
-    expand = 1 / (1.0*layout[0].expand);
-    expand_out = layout[0].expand;
-    expand_in  = 1;
-  }
-  if (layout[0].expand < 0) {
-    expand = fabs((double)layout[0].expand);
-    expand_out = 1;
-    expand_in  = -layout[0].expand;
-  }
-
-  dx = layout[0].picture.dx;
-  dy = layout[0].picture.dy;
-  DX = layout[0].matrix.Naxis[0];
-  DY = layout[0].matrix.Naxis[1];
-
-  /* X,Y are the image coordinates of the first image pixel */
-  X = MAX(0.5*(DX - dx*expand) - layout[0].X, 0);
-  if ((int)X != X) 
-    X = (int) X + 1;
-  else 
-    X = (int) X;
-  Y = MAX(0.5*(DY - dy*expand) - layout[0].Y, 0);
-  if ((int)Y != Y) 
-    Y = (int) Y + 1;
-  else 
-    Y = (int) Y;
-
-  /* Rx,Ry are the screen coordinates of the first image pixel */
-  Rx = (X + layout[0].X - 0.5*DX)/expand + 0.5*dx;
-  Ry = (Y + layout[0].Y - 0.5*DY)/expand + 0.5*dy;
-
-  i_start = MIN (MAX (Rx, 0), dx - expand_out + 1);
-  j_start = MIN (MAX (Ry, 0), dy - expand_out + 1);
-  
-  if (layout[0].expand > 0) {
-    i_end = MAX (MIN (i_start + ((int)(expand*(dx-i_start)))/expand, expand_out*(DX-X) + Rx), 0);
-    j_end = MAX (MIN (j_start + ((int)(expand*(dy-j_start)))/expand, expand_out*(DY-Y) + Ry), 0);
-  } else {
-    i_end = MAX (MIN (dx, (DX-X)/expand + Rx), 0);
-    j_end = MAX (MIN (dy, (DY-Y)/expand + Ry), 0);
-  }    
-
-  dropback = expand_out - (i_end - i_start) % expand_out;
-  if ((i_end - i_start) % expand_out == 0) dropback = 0;
-
-  /* output line buffer */
-  ALLOCATE (line_buffer, char, dx);
-
-  in_pix_ref  = (unsigned char *) (layout[0].matrix.buffer) + DX*(int)MAX(Y,0) + (int)MAX(X,0);
-
-  /********** below we do the mapping from buffer pixels (in) to picture pixels (out) **********/
-
-  Nchar = 255.0;
-  Npix = layout[0].Npixels;
-  frac = Nchar / Npix;
-
-  /**** fill in bottom area ****/
-  out_pix = line_buffer;
-  for (i = 0; i < dx; i++, out_pix++) {
-    *out_pix = WHITE;
-  }
-  for (j = 0; j < j_start; j++) {
-    for (i = 0; i < dx; i++) {
-      fprintf (f, "%02x", val);
-      if ((Npt % 40) == 0) {
-	fprintf (f, "\n");
-      }
-    }
-  }
-  
-  /*** fill in the image data region ***/
-  for (j = j_start; j < j_end; j+= expand_out, in_pix_ref += expand_in*DX) {
-    
-    /* create one output image line */
-    in_pix = in_pix_ref;
-    out_pix = line_buffer;
-
-    /**** fill in area to the left of the picture ****/
-    for (i = 0; i < i_start; i++, out_pix+=3) {
-      *out_pix = WHITE;
-    }
-    
-    /*** fill in the picture region ***/
-    for (i = i_start; i < i_end; i+=expand_out, in_pix+=expand_in) {
-      for (ii = 0; ii < expand_out; ii++, out_pix++) {
-	*out_pix = *in_pix*frac;
-      }
-    }
-    
-    /**** fill in area to the right of the picture ****/
-    for (i = i_end; i < dx; i++, out_pix++) {
-      *out_pix = WHITE;
-    }
-
-    /* write out the image line expand_out times */
-    for (j = 0; j < expand_out; j++) {
-      for (i = 0; i < dx; i++) {
-	fprintf (f, "%02x", val);
-	if ((Npt % 40) == 0) {
-	  fprintf (f, "\n");
-	}
-      }
-    }
-  }
-
-  /**** fill in top area ****/
-  out_pix = line_buffer;
-  for (i = 0; i < dx; i++, out_pix++) { 
-    *out_pix = WHITE;
-  }
-  for (j = j_end; j < dy; j++) {
-    for (i = 0; i < dx; i++) {
-      fprintf (f, "%02x", val);
-      if ((Npt % 40) == 0) {
-	fprintf (f, "\n");
-      }
-    }
-  }
-}
-# endif
-
-void ConvertPixmap8 (Layout *layout, FILE *f) {
-
-  int i, k, m, val;
-  double Nchar, Npix, start, slope, frac;
-  unsigned char *buff;
-
-  Nchar = 255.0;
-  Npix = layout[0].Npixels;
-  frac = Nchar / Npix;
-  /* start at the last line, print lines in decending order */
-  buff = (unsigned char *)layout[0].picture.data + layout[0].picture.dx*(layout[0].picture.dy - 1);
-  slope = layout[0].slope;
-  start = layout[0].start;
-
-  for (i = 0; i < layout[0].picture.dy; i++) {
-    for (k = 0; k < layout[0].picture.dx; k++, buff++) {
-      if (*buff == layout[0].white) 
-	val = Nchar;
-      else {
-	for (m = 0; (layout[0].cmap[m].pixel != *buff) && (m < Npix); m++);
-	val = Nchar - frac * MIN (MAX (start + m * slope, 0), Npix);
-      }
-      fprintf (f, "%02x", val);
-      if (!((k+1) % 40)) fprintf (f, "\n"); 
-    }
-    fprintf (f, "\n");
-    buff -= 2*layout[0].picture.dx;
-  }
-  return;
-}
-
-void ConvertPixmap16 (Layout *layout, FILE *f) {
-
-  int i, k, m, val;
-  double Nchar, Npix, start, slope, frac;
-  unsigned short *buff;
-
-  Nchar = 255.0;
-  Npix = layout[0].Npixels;
-  frac = Nchar / Npix;
-  /* start at the last line, print lines in decending order */
-  buff = (unsigned short *)layout[0].picture.data + layout[0].picture.dx*(layout[0].picture.dy - 1);
-  slope = layout[0].slope;
-  start = layout[0].start;
-
-  for (i = 0; i < layout[0].picture.dy; i++) {
-    for (k = 0; k < layout[0].picture.dx; k++, buff++) {
-      if (*buff == layout[0].white) 
-	val = Nchar;
-      else {
-	for (m = 0; (layout[0].cmap[m].pixel != *buff) && (m < Npix); m++);
-	val = Nchar - frac * MIN (MAX (start + m * slope, 0), Npix);
-      }
-      fprintf (f, "%02x", val);
-      if (!((k+1) % 40)) fprintf (f, "\n"); 
-    }
-    fprintf (f, "\n");
-    buff -= 2*layout[0].picture.dx;
-  }
-  return;
-}
-
-void ConvertPixmap24 (Layout *layout, FILE *f) {
-
-  int i, k, m, dx, dy, val, extra;
-  unsigned char *buff;
-  unsigned long color, byte;
-  double Nchar, Npix, start, slope, frac;
-
-  Nchar = 255.0;
-  Npix = layout[0].Npixels;
-  frac = Nchar / Npix;
-  dx = layout[0].picture.dx;
-  dy = layout[0].picture.dy;
-  extra = 4 - (dx * 3) % 4;
-  /* start at the last line, print lines in decending order */
-  buff = (unsigned char *)&layout[0].picture.data[(dy - 1)*(3*dx + extra)];
-  slope = layout[0].slope;
-  start = layout[0].start;
-
-  for (i = 0; i < dy; i++) {
-    for (k = 0; k < dx; k++, buff+=3) {
-      color = 0;
-      byte = buff[2];
-      color = (byte << 16);
-      byte = buff[1];
-      color |= (byte << 8);
-      byte = buff[0];
-      color |= byte;
-      for (m = 0; (layout[0].cmap[m].pixel != color) && (m < Npix); m++);
-      val = Nchar - frac * MIN (MAX (start + m * slope, 0), Npix);
-      fprintf (f, "%02x", val);
-      if (!((k+1) % 40)) fprintf (f, "\n"); 
-    }
-    fprintf (f, "\n");
-    buff -= 2*3*dx + extra;
-  }
-  return;
-}
-
-void ConvertPixmap32 (Layout *layout, FILE *f) {
-
-  int i, k, m, val;
-  double Nchar, Npix, start, slope, frac;
-  unsigned int *buff;
-
-  Nchar = 255.0;
-  Npix = layout[0].Npixels;
-  frac = Nchar / Npix;
-  /* start at the last line, print lines in decending order */
-  buff = (unsigned int *)layout[0].picture.data + layout[0].picture.dx*(layout[0].picture.dy - 1);
-  slope = layout[0].slope;
-  start = layout[0].start;
-
-  for (i = 0; i < layout[0].picture.dy; i++) {
-    for (k = 0; k < layout[0].picture.dx; k++, buff++) {
-      if (*buff == layout[0].white) 
-	val = Nchar;
-      else {
-	for (m = 0; (layout[0].cmap[m].pixel != *buff) && (m < Npix); m++);
-	val = Nchar - frac * MIN (MAX (start + m * slope, 0), Npix);
-      }
-      fprintf (f, "%02x", val);
-      if (!((k+1) % 40)) fprintf (f, "\n"); 
-    }
-    fprintf (f, "\n");
-    buff -= 2*layout[0].picture.dx;
-  }
-  return;
-}
