/* $Id: PlotterPS.c,v 1.1 91/05/20 15:15:31 gnb Exp $ */
/* $Source: /export/sources/x/At/Plotter/tmp/RCS/PlotterPS.c,v $ */

/*

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.

*/

/*
 * $Log:	PlotterPS.c,v $
 * Revision 1.1  91/05/20  15:15:31  gnb
 * Initial revision
 * 
 * 
 */

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

#include <X11/Intrinsic.h>

#ifdef _AtDevelopment_
#include "Text.h"
#include "PlotterP.h"
#include "Axis.h"
#include "PlotP.h"
#else
#include <At/Text.h>
#include <At/PlotterP.h>
#include <At/Axis.h>
#include <At/PlotP.h>
#endif

#define NUMCHILDREN(w) (w->composite.num_children)
#define CHILD(w,i) (w->composite.children[i])
#define CONSTRAINT(w,i) ((AtPlotterConstraints)(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 DRAWPS(w,i) (*((AtPlotWidgetClass)CHILD(w,i)->core.widget_class)->plot_class.drawPS)
#define DRAWPSCHILD(cw) (*((AtPlotWidgetClass)((Widget)cw)->core.widget_class)->plot_class.drawPS)
#define LEGENDICON(f,w,i,x,y,width,height) (*((AtPlotWidgetClass)CHILD(w,i)->core.widget_class)->plot_class.draw_iconPS)(f, (AtPlotWidget)CHILD(w,i),x,y,width,height)

#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=FIRST_PUBLIC_CHILD; 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\ngsave\n");
    
    h = height(w->plotter.legend_title_text) + w->plotter.margin_height;
    for(i=FIRST_PUBLIC_CHILD; 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 = FIRST_PUBLIC_CHILD; i < NUMCHILDREN(w); i++ ) {
	t = CONSTRAINT(w,i)->plotter.legend_text;
	if (NTHCHILDISDISPLAYED(w, i) && t != NULL) {
	    h = height(t);
	    LEGENDICON(f,w,i, x1, y, 16, h);
	    AtTextPSDraw(f, t, x1 + 16 + w->plotter.margin_width,
			 y - AtTextPSAscent(t));
	    y -= h + w->plotter.legend_spacing;
	}
    }
    
    fprintf(f, "grestore\n%%%%EndObject\n");
}    
#undef height


void AtPlotterDrawPS(f, w, xll, yll, xur, yur)
     FILE *f;
     AtPlotterWidget w;
     int xll, yll, xur, yur;
{
    int height, width;
    int xaxiswidth, yaxiswidth, y2axiswidth;
    int i;
    int x1, y1, x2, y2;
    int titleX, titleY, legendX, legend_width;
    Boolean floatx, floaty;
    int originx, originy;
    AtScale *xscale, *yscale, *y2scale;
    double xmax, xmin, ymax, ymin, min, max;
    
    fprintf(f, "%%%% BeginObject: AtPlotter\ngsave\n");
    
    x1 = xll;
    y1 = yll;
    x2 = xur;
    y2 = yur;

    if (w->plotter.show_legend) {
      legend_width = CalcLegendWidth(w);
      x2 -= legend_width + w->plotter.margin_width;
      legendX = x2 + w->plotter.margin_width;
    }
    
    if (w->plotter.title_text != NULL) {
	titleY = y2 - AtTextPSAscent(w->plotter.title_text);
	y2 -= AtTextPSAscent(w->plotter.title_text) +
	      AtTextPSDescent(w->plotter.title_text) + w->plotter.margin_height;
    }

    AtAxisGetBounds(w->plotter.xaxis, &xmin, &xmax);
    AtAxisGetBounds(w->plotter.yaxis, &ymin, &ymax);
    
    if ((w->plotter.floating_x) && (!w->plotter.framed_axes) &&
        (ymin < 0.0) &&	(ymax > 0.0))
        floatx = True;
    else
        floatx = False;
    
    if ((w->plotter.floating_y) && (!w->plotter.framed_axes) &&
        (xmin < 0.0) &&	(xmax > 0.0))
        floaty = True;
    else
        floaty = False;

    xaxiswidth = yaxiswidth = y2axiswidth = 0;
    
    if (NTHCHILDISDISPLAYED(w, CHILD_XAXIS))
      xaxiswidth = AtAxisWidthPS(w->plotter.xaxis);
    if (NTHCHILDISDISPLAYED(w, CHILD_YAXIS))
      yaxiswidth = AtAxisWidthPS(w->plotter.yaxis);
    if (NTHCHILDISDISPLAYED(w, CHILD_Y2AXIS))
      y2axiswidth = AtAxisWidthPS(w->plotter.y2axis);

    if (!floaty) x1 += yaxiswidth;
    if (!floatx) y1 += xaxiswidth;
    x2 -= y2axiswidth;
    if (w->plotter.framed_axes) {
      int ydisp = NTHCHILDISDISPLAYED(w, CHILD_YAXIS);
      int y2disp = NTHCHILDISDISPLAYED(w, CHILD_Y2AXIS);
      
      if (ydisp && !y2disp)
	x2 -= AtAxisWidth(w->plotter.yframeaxis);
      if (y2disp && !ydisp)
	x1 += AtAxisWidth(w->plotter.y2frameaxis);
      y1 += AtAxisWidth(w->plotter.xframeaxis);
    }

    width = x2 - x1;
    height = y2 - y1;

    if (w->plotter.title_text != NULL)  {
      titleX = x1 + (width - AtTextPSWidth(w->plotter.title_text))/2;
      AtTextPSDraw(f, w->plotter.title_text, titleX, titleY);
    }

    if (w->plotter.show_legend)
	drawlegend(f, w, legendX, yll, legendX + legend_width, yur);

    if(w->plotter.force_aspect){
      int extraw,extrah;
      if((xmax-xmin)/(ymax-ymin)>(double)width/height){
	double xr = (xmax-xmin)/width;
	int new_height = (ymax-ymin)/xr;
	extrah = height - new_height;
	y1 += extrah/2;
	y2 -= extrah/2;
      }
      else {
	double yr = (ymax-ymin)/height;
	int new_width = (xmax-xmin)/yr;
	extraw = width - new_width;
	x1 += extraw/2;
	x2 -= extraw/2;
      }
    }

    /* set up scales */
    AtAxisGetBounds(w->plotter.xaxis, &min, &max);
    xscale = AtScaleCreate(min, max, x1, x2,
			   AtAxisGetTransform(w->plotter.xaxis));

    AtAxisGetBounds(w->plotter.yaxis, &min, &max);
    yscale = AtScaleCreate(min, max, y1, y2,
			   AtAxisGetTransform(w->plotter.yaxis));

    AtAxisGetBounds(w->plotter.y2axis, &min, &max);
    y2scale = AtScaleCreate(min, max, y1, y2,
			   AtAxisGetTransform(w->plotter.y2axis));

    /* draw the axes */
    if (NTHCHILDISDISPLAYED(w, CHILD_XAXIS)) {
      if (floatx) {
	originy = AtScaleUserToPixel(yscale, 0.0);
	AtAxisDrawPS(f, w->plotter.xaxis, x1, originy, x2, originy, y1, y2);
      }
      else
	AtAxisDrawPS(f, w->plotter.xaxis, x1, y1, x2, y1, y1, y2);
    }
    
    if (NTHCHILDISDISPLAYED(w, CHILD_YAXIS)) {
      if (floaty) {
	originx = AtScaleUserToPixel(xscale, 0.0);
	AtAxisDrawPS(f, w->plotter.yaxis, originx, y1, originx, y2, x1, x2);
      }
      else 
	AtAxisDrawPS(f, w->plotter.yaxis, x1, y1, x1, y2, x1, x2);
    }
    
    if (NTHCHILDISDISPLAYED(w, CHILD_Y2AXIS))
	AtAxisDrawPS(f, w->plotter.y2axis, x2, y1, x2, y2, x1, x2);

    if (w->plotter.framed_axes) {
	AtAxisDrawPS(f, w->plotter.xframeaxis, x1, y2, x2, y2, 0, 0);
	if (!NTHCHILDISDISPLAYED(w, CHILD_Y2AXIS))
	  AtAxisDrawPS(f, w->plotter.yframeaxis, x2, y1, x2, y2, 0, 0);
	if (!NTHCHILDISDISPLAYED(w, CHILD_YAXIS))
	  AtAxisDrawPS(f, w->plotter.y2frameaxis, x1, y1, x1, y2, 0, 0);
    }

    /* 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 clip newpath\n",
	    x1, y1, x1, y2, x2,y2, x2, y1);

    /* draw each of the plots */
    if (w->plotter.rank_children) {
      Rank *tmp = w->plotter.ordered_children;
      while (tmp) {
	if (ISDISPLAYED(tmp->child))
	  if (CONSTRAINTS(tmp->child)->plotter.use_y2_axis)
	    DRAWPSCHILD(tmp->child)(f, (AtPlotWidget)tmp->child, 
				    xscale, y2scale);
	  else
	    DRAWPSCHILD(tmp->child)(f, (AtPlotWidget)tmp->child, 
				    xscale, yscale);
	tmp = tmp->next;
      }
    } else {
      for(i=FIRST_PUBLIC_CHILD; i<NUMCHILDREN(w); i++) {
	if (NTHCHILDISDISPLAYED(w, i))
	  if (CONSTRAINT(w,i)->plotter.use_y2_axis)
	    DRAWPS(w,i)(f, (AtPlotWidget)CHILD(w,i), xscale, y2scale);
	  else
	    DRAWPS(w,i)(f, (AtPlotWidget)CHILD(w,i), xscale, yscale);
      }
    }
    
    /* finish up */
    fprintf(f, "grestore\n%%%%EndObject\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\
/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);
}

/*
 * Local Variables:
 * eval: (set-c-style 'GNU)
 * End:
 */
