static char RCSid[] = "$Id: PlotterPS.c,v 1.0 91/08/22 15:33:42 gnb Exp $"; 
/*
 * $Source: /export/data/sources/x/At/Plotter/RCS/PlotterPS.c,v $
 * 
 * $Log:	PlotterPS.c,v $
 * Revision 1.0  91/08/22  15:33:42  gnb
 * Initial revision
 * 
 * 
 */

/*

Copyright 1990,1991 by the Massachusetts Institute of Technology

All rights reserved.

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of the Massachusetts
Institute of Technology (M.I.T.) not be used in advertising or publicity
pertaining to distribution of the software without specific, written
prior permission.

M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
M.I.T. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

*/

#include <stdio.h>
#ifdef __STDC__
#include <stdlib.h>
#endif 

#include <X11/Intrinsic.h>

#ifdef _AtDevelopment_
#include "Text.h"
#include "PlotterP.h"
#include "AxisCore.h"
#include "Plot.h"
#else
#include <At/Text.h>
#include <At/PlotterP.h>
#include <At/AxisCore.h>
#include <At/Plot.h>
#endif

#define NUMCHILDREN(w) (w->composite.num_children)
#define CHILD(w,i) ((AtPlotWidget)(w->composite.children[i]))
#define CONSTRAINT(w,i) \
     ((AtPlotterConstraints)(((Widget)CHILD(w,i))->core.constraints))
#define CONSTRAINTS(cw) \
     ((AtPlotterConstraints)((Widget)cw)->core.constraints)

#define NTHCHILDISDISPLAYED(w, i) (CONSTRAINT(w, i)->plotter.displayed)
#define ISDISPLAYED(cw) (CONSTRAINTS(cw)->plotter.displayed)

#define height(x) AtTextPSAscent(x) + AtTextPSDescent(x)

/*
 * Calculate the width of the legend FOR THE POSTSCRIPT VERSION!
 */

static int CalcLegendWidth P((AtPlotterWidget pw));
static int CalcLegendWidth(pw)
AtPlotterWidget pw;
{
     int i, w, mw = 0;
     AtText *t;
     
     for(i=0; i < NUMCHILDREN(pw); i++) {
	  if (NTHCHILDISDISPLAYED(pw, i)) {
	       t = CONSTRAINT(pw,i)->plotter.legend_text;
	       if (t != NULL) {
		    w = AtTextPSWidth(t);
		    if (w > mw) mw = w;
	       }
	  }
     }
     return mw + 16 + pw->plotter.margin_width;
}

/*
 * Print out the legend
 */
static void drawlegend P((FILE *f, AtPlotterWidget w,
			  int x1, int y1, int x2, int y2));
static void drawlegend(f, w, x1, y1, x2, y2)
FILE *f;
AtPlotterWidget w;
int x1, y1, x2, y2;
{
     int x, y, h;
     int i;
     AtText *t;
     
     fprintf(f, "%%%%BeginObject: AtPlotter legend\nGS\n");
     
     h = height(w->plotter.legend_title_text) + w->plotter.margin_height;
     for(i=0; i < NUMCHILDREN(w); i++) {
	  if (NTHCHILDISDISPLAYED(w, i)) {
	       t = CONSTRAINT(w,i)->plotter.legend_text;
	       if (t != NULL) h += height(t) + w->plotter.legend_spacing;
	  }
     }
     
     y = y2 - ((y2 - y1) - h)/2;
     x = x1 + ((x2 - x1) - AtTextPSWidth(w->plotter.legend_title_text))/2;
     AtTextPSDraw(f, w->plotter.legend_title_text, x,
		  y - AtTextPSAscent(w->plotter.legend_title_text));
     y -= height(w->plotter.legend_title_text) + w->plotter.margin_height;
     
     for(i = 0; i < NUMCHILDREN(w); i++ ) {
	  t = CONSTRAINT(w,i)->plotter.legend_text;
	  if (NTHCHILDISDISPLAYED(w, i) && t != NULL) {
	       h = height(t);
	       AtPlotDrawIconPS(CHILD(w, i), f, x1, y, 16, h);
	       AtTextPSDraw(f, t, x1 + 16 + w->plotter.margin_width,
			    y - AtTextPSAscent(t));
	       y -= h + w->plotter.legend_spacing;
	  }
     }
     
     fprintf(f, "GR\n%%%%EndObject AtPlotter legend\n");
}    
#undef height


void AtPlotterDrawPS(f, w, xll, yll, xur, yur)
FILE *f;
AtPlotterWidget w;
int xll, yll, xur, yur;
{
     int height, width, i;
     int xaxiswidth, x2axiswidth, yaxiswidth, y2axiswidth;
     int x1, y1, x2, y2;
     int titleX, titleY, legendX, legend_width;
     AtScale *xscale, *x2scale, *yscale, *y2scale;
     AtPlotterPart *pp = &w->plotter;
     double min, max;
     AtScale *xs, *ys;
     
     fprintf(f, "%%%% BeginObject: AtPlotter\nGS\n");
     
     x1 = xll;
     y1 = yll;
     x2 = xur;
     y2 = yur;
     
     /* set up clipping */
     /* since this is right before a grestore, we don't need to gsave here. */
     fprintf(f, "%d %d M %d %d L %d %d L %d %d L CP clip newpath\n",
	     x1, y1, x1, y2, x2, y2, x2, y1);
     
     if (pp->show_legend) {
	  legend_width = CalcLegendWidth(w);
	  x2 -= legend_width + pp->margin_width;
	  legendX = x2 + pp->margin_width;
     }
     
     if (pp->title_text != NULL) {
	  titleY = y2 - AtTextPSAscent(pp->title_text);
	  y2 -= AtTextPSAscent(pp->title_text) +
	       AtTextPSDescent(pp->title_text) + pp->margin_height;
     }
     
     xaxiswidth = x2axiswidth = yaxiswidth = y2axiswidth = 0;
     
     if (pp->xaxis && ISDISPLAYED(pp->xaxis))
	  xaxiswidth = AtAxisWidthPS(pp->xaxis);
     if (pp->x2axis && ISDISPLAYED(pp->x2axis))
	  x2axiswidth = AtAxisWidthPS(pp->x2axis);
     if (pp->yaxis && ISDISPLAYED(pp->yaxis))
	  yaxiswidth = AtAxisWidthPS(pp->yaxis);
     if (pp->y2axis && ISDISPLAYED(pp->y2axis))
	  y2axiswidth = AtAxisWidthPS(pp->y2axis);
     
     x2 -= y2axiswidth;
     x1 += yaxiswidth;
     y1 += xaxiswidth;
     y2 -= x2axiswidth;
     
     width = x2 - x1;
     height = y2 - y1;
     
     if (pp->title_text != NULL)  {
	  titleX = x1 + (width - AtTextPSWidth(pp->title_text))/2;
	  AtTextPSDraw(f, pp->title_text, titleX, titleY);
     }
     
     if (pp->show_legend)
	  drawlegend(f, w, legendX, yll, legendX + legend_width, yur);
     
     /* set up scales */
     if (pp->xaxis) {
	  AtAxisGetBounds(pp->xaxis, &min, &max);
	  xscale = AtScaleCreate(min, max, x1, x2,
				 AtAxisGetTransform(pp->xaxis));
     }
     
     if (pp->x2axis) {
	  AtAxisGetBounds(pp->x2axis, &min, &max);
	  x2scale = AtScaleCreate(min, max, x1, x2,
				  AtAxisGetTransform(pp->x2axis));
     }
     
     if (pp->yaxis) {
	  AtAxisGetBounds(pp->yaxis, &min, &max);
	  yscale = AtScaleCreate(min, max, y1, y2,
				 AtAxisGetTransform(pp->yaxis));
     }
     
     if (pp->y2axis) {
	  AtAxisGetBounds(pp->y2axis, &min, &max);
	  y2scale = AtScaleCreate(min, max, y1, y2,
				  AtAxisGetTransform(pp->y2axis));
     }
     
     /* draw each of the plots */
     if (pp->rank_children) {
	  Rank *tmp;
	  for (tmp = pp->ordered_children; tmp; tmp = tmp->next) {
	       if (!ISDISPLAYED(tmp->child)) continue;
	       
	       if ((AtAxisCoreWidget) tmp->child == pp->xaxis) 
		    AtAxisDrawPS((AtAxisCoreWidget) tmp->child, f,
				 xscale, x1, y1, x2, y1, y2 - y1);
	       else if ((AtAxisCoreWidget) tmp->child == pp->x2axis) 
		    AtAxisDrawPS((AtAxisCoreWidget) tmp->child, f, 
				 x2scale, x1, y2, x2, y2, y2 - y1);
	       else if ((AtAxisCoreWidget) tmp->child ==  pp->yaxis) 
		    AtAxisDrawPS((AtAxisCoreWidget) tmp->child, f, 
				 yscale, x1, y1, x1, y2, x2 - x1);
	       else if ((AtAxisCoreWidget) tmp->child == pp->y2axis) 
		    AtAxisDrawPS((AtAxisCoreWidget) tmp->child, f, 
				 y2scale, x2, y1, x2, y2, x2 - x1);
	       else {
		    xs = CONSTRAINTS(tmp->child)->plotter.use_x2_axis ?
			 x2scale : xscale;
		    ys = CONSTRAINTS(tmp->child)->plotter.use_y2_axis ?
			 y2scale : yscale;
		    AtPlotDrawPS(tmp->child, f, xs, ys);
	       }
	  }
     } else {
	  for(i = 0; i < NUMCHILDREN(w); i++) {
	       if (!NTHCHILDISDISPLAYED(w, i)) continue;
	       
	       if ((AtAxisCoreWidget) CHILD(w, i) == pp->xaxis) 
		    AtAxisDrawPS((AtAxisCoreWidget) CHILD(w, i), f, 
				 xscale, x1, y1, x2, y1, y2 - y1);
	       else if ((AtAxisCoreWidget) CHILD(w, i) == pp->x2axis) 
		    AtAxisDrawPS((AtAxisCoreWidget) CHILD(w, i), f, 
				 x2scale, x1, y2, x2, y2, y2 - y1);
	       else if ((AtAxisCoreWidget) CHILD(w, i) == pp->yaxis) 
		    AtAxisDrawPS((AtAxisCoreWidget) CHILD(w, i), f,
				 yscale, x1, y1, x1, y2, x2 - x1);
	       else if ((AtAxisCoreWidget) CHILD(w, i) == pp->y2axis) 
		    AtAxisDrawPS((AtAxisCoreWidget) CHILD(w, i), f,
				 y2scale, x2, y1, x2, y2, x2 - x1);
	       else {
		    xs = CONSTRAINT(w, i)->plotter.use_x2_axis ?
			 x2scale : xscale;
		    ys = CONSTRAINT(w, i)->plotter.use_y2_axis ?
			 y2scale : yscale;
		    AtPlotDrawPS(CHILD(w, i), f, xs, ys);
	       }
	  }
     }
     
     /* finish up */
     fprintf(f, "GR\n%%%%EndObject AtPlotter\n");
}

static char prolog[] =
"/F {findfont exch scalefont setfont} bind def\n\
/S {moveto show} bind def\n\
/L {lineto} bind def\n\
/M {moveto} bind def\n\
/RM { rmoveto } bind def\n\
/RL { rlineto } bind def\n\
/CP { closepath } bind def\n\
/ST { stroke } bind def\n\
/GS { gsave } bind def\n\
/GR { grestore } bind def\n\
/SG { setgray } bind def\n\
/CIR { 4 0 360 arc ST} bind def\n\
/SQR { M 4 4 RM -8 0 RL 0 -8 RL 8 0 RL CP ST } bind def\n\
/TRI { M 0 3.46 RM 4 -8 RL -8 0 RL CP ST } bind def\n\
/CRO { M 0 4 RM 0 -8 RL 4 4 RM -8 0 RL ST } bind def\n\
/STA { M 0 4 RM 0 -8 RL 4 4 RM -8 0 RL\n\
       1.17 2.83 RM 5.66 -5.66 RL 0 5.66 RM -5.66 -5.66 RL ST} bind def\n\
/DIA {M 0 5.66 RM 5.66 -5.66 RL -5.66 -5.66 RL -5.66 5.66 RL CP ST} bind def\n\
/DOT { 1 0 360 arc fill} bind def\n";
    
#include <sys/types.h>
#ifndef VMS
#include <pwd.h>
#endif
     
void AtPlotterGeneratePostscript(filename, w, title, x1, y1, x2, y2, landscape)
char *filename;
AtPlotterWidget w;
char *title;
int x1, y1, x2, y2;
int landscape;
{
     FILE *f;
     char *asctime();
     time_t now = time(NULL);
     
#ifdef VMS
     char *user_name;
#endif
     
     f = fopen(filename, "w+");
     if (f == NULL) {
	  perror("fopen");
	  return;
     }
     
#ifdef VMS
#include <time.h>
     time((time_t *) &now);
#endif
     
     /* do header info */
     fprintf(f,"%%!PS-Adobe-2.0\n");
     fprintf(f,"%%%%Title: %s\n", title);
     fprintf(f,"%%%%Creator: the Athena Tools plotter widget\n");
     fprintf(f,"%%%%CreationDate: %s",asctime(localtime(&now)));
#ifndef VMS
     fprintf(f,"%%%%For: %s\n", getpwuid(getuid())->pw_name);
#else
     fprintf(f, "%%%%For: %s\n", cuserid(user_name));
#endif
     fprintf(f,"%%%%BoundingBox: %d %d %d %d\n", x1, y1, x2, y2);
     fprintf(f,"%%%%Pages: 1\n");
     fprintf(f,"%%%%EndComments\n");
     
     /* do prolog */
     fprintf(f, prolog);
     fprintf(f, "%%%%EndProlog\n");
     
     if (landscape) {
	  
	  fprintf(f, "90 rotate\n");
	  fprintf(f, "0 -612 translate\n");
     }
     /* do the body of the postscript */
     AtPlotterDrawPS(f, w, x1, y1, x2, y2);
     
     /* finish up */
     fprintf(f, "showpage\n");
     
     fclose(f);
     *RCSid = *RCSid; 
}
