/* $Id: AxisPS.c,v 1.1 91/05/20 15:12:29 gnb Exp $ */
/* $Source: /export/sources/x/At/Plotter/tmp/RCS/AxisPS.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:	AxisPS.c,v $
 * Revision 1.1  91/05/20  15:12:29  gnb
 * Initial revision
 * 
 * 
 */

#include <stdio.h>
#include <math.h>    

#ifdef _AtDevelopment_
#include "AxisP.h"
#include "FontFamilyP.h"    
#else
#include <At/AxisP.h>
#include <At/FontFamilyP.h>
#endif

/* 1 point is about 1 pixel on our displays, so for now, tic lengths and
   margin lengths etc. are okay as is.
*/

/*
 * returns the "width" (the height if horizontal) of the passed axis.
 * XXX assumes maxnumwidth is pixels is approximately correct.
 */
int AtAxisWidthPS(w)
     AtAxisObject w;
{
  if (w->axis.vertical) {
    return (w->axis.label_text ? AtTextPSWidth(w->axis.label_text) : 0) +
      (w->axis.draw_numbers && w->axis.numbers_outside ?
       w->axis.max_num_width : 0) +
	 (w->axis.tics_outside ? w->axis.tic_length + 1 : 0) +
	   MIN_MARGIN;
  } else {
    return (w->axis.label_text ? 
	    AtTextPSAscent(w->axis.label_text) +
	    AtTextPSDescent(w->axis.label_text) : 0) +
	      (w->axis.draw_numbers && w->axis.numbers_outside &&
	       w->axis.tics > 0 ?
	       AtTextPSAscent(w->axis.tic_text[0]) +
	       AtTextPSDescent(w->axis.tic_text[0]): 0) +
		 (w->axis.tics_outside ? w->axis.tic_length + 1 : 0) +
		   MIN_MARGIN;
  }
}
			   


void AtAxisDrawPS(f, axis, x1, y1, x2, y2, g1, g2)
     FILE *f;
     AtAxisObject axis;
     int x1, y1, x2, y2, g1, g2;
{
    AtAxisPart *a = &axis->axis;
    AtScale *scale;
    int i;
    short st1, st2, bt1, bt2; /* where to draw small and big tics */
    short tmp;
    double *tics;
    int max_num_width = 0;

    if (a->vertical)
	scale = AtScaleCreate(a->min, a->max, y1*100, y2*100, a->transform);
    else
	scale = AtScaleCreate(a->min, a->max, x1*100, x2*100, a->transform);

    fprintf(f, "%%%% BeginObject: AtAxis\n");
    fprintf(f, "gsave\n");

    /* draw the line */
    fprintf(f, "%d %d M %d %d L\n", x1, y1, x2, y2);    

#define swap(a,b) {tmp = a; a = b; b = tmp;}    
    /* figure out how to draw the tics */
    st1 = st2 = 0;
    bt1 = bt2 = 0;
    if (a->vertical) {
	if (a->tics_outside) {st1 = a->subtic_length; bt1 = a->tic_length;}
	if (a->tics_inside)  {st2 = a->subtic_length; bt2 = a->tic_length;}
	if (a->mirror) { swap(st1, st2); swap(bt1,bt2); }
    } else {
	if (a->tics_outside) {st2 = a->subtic_length; bt2 = a->tic_length;}
	if (a->tics_inside)  {st1 = a->subtic_length; bt1 = a->tic_length;}
	if (a->mirror) { swap(st1, st2); swap(bt1,bt2); }
    }
#undef swap

    tics = (double *) XtMalloc(a->tics * sizeof(double));

    if (a->tics_inside || a->tics_outside) {
      /* draw the tics */
      if (a->vertical) 
	for(i = 0; i < a->tics; i++) {
	  tics[i] = AtScaleUserToPixel(scale, a->tic_values[i]) / 100.0;
	  fprintf(f, "%d %g M %d %g L\n",
		  x1-bt1, tics[i], x1+bt2, tics[i]);
	}
      else
	for(i=0; i<a->tics; i++) {
	  tics[i] = AtScaleUserToPixel(scale, a->tic_values[i]) / 100.0;
	  fprintf(f, "%g %d M %g %d L\n",
		  tics[i], y1+bt1, tics[i], y1-bt2);
	}
      fprintf(f, "stroke\n");
    }
    
    /* draw the subtics */
    if (a->subtic_length > 0 && a->subtics && 
	(a->tics_inside || a->tics_outside)) {
      int j;
      double stv = a->tic_interval / (a->subtics + 1);
      double v;
      
      for (i = 0; i < a->tics; i++) {
	for (j = 1;
	     (v = a->tic_values[i] + j * stv) < a->max &&
	     v < a->tic_values[i+1];
	     j++) {
	  v = AtScaleUserToPixel(scale, v) / 100.0; 
	  if (a->vertical)
	    fprintf(f, "%d %g M %d %g L\n", x1 - st1, v, x1 + st2, v);
	  else
	    fprintf(f, "%g %d M %g %d L\n", v, y1 + st1, v, y1 - st2);
	  }
	}
    }
    fprintf(f, "stroke\n");

    /* draw the grid */
    if (a->draw_grid) {
      fprintf(f, "gsave\n");
      fprintf(f, "0 setlinewidth\n");
      fprintf(f, "[ ] 0 setdash\n");
      for(i = 0; i<a->tics; i++) {
	if (a->vertical) 
	  fprintf(f, "%d %g M %d %g L\n", g1, tics[i], g2, tics[i]);
	else
	  fprintf(f, "%g %d M %g %d L\n", tics[i], g1, tics[i], g2);
      }
      fprintf(f, "stroke\n");
      fprintf(f, "grestore\n");
    }
    

    XtFree((char *)tics);
    
#define ascent(a) ((a)->tics > 0 ? \
		   AtTextPSAscent((a)->tic_text[0]) : 0)
#define descent(a) ((a)->tics > 0 ? \
		   AtTextPSDescent((a)->tic_text[0]) : 0)

    /* draw the numbers */
    fprintf(f, "%d /%s F\n", AtFontPointSize(a->numberFF, a->number_size),
	    AtFontPSName(a->numberFF, AtFontPLAIN));
    max_num_width = 0;
    if (a->draw_numbers) {
	for(i=0; i < a->tics; i++) {
	    int x, y, wid;

	    wid = AtTextPSWidth(a->tic_text[i]);
	    
	    if (wid > max_num_width) max_num_width = wid;
	    if (a->vertical) {
		x = x1;
		y = AtScaleUserToPixel(scale, a->tic_values[i]) / 100;
		if (!a->mirror ^ !!a->numbers_outside) 
		    x += bt2 + 2;
		else
		    x -= bt1 + 2 + wid;
		y -= ascent(a)/2 - descent(a)/2;
	    }
	    else {
		x = AtScaleUserToPixel(scale, a->tic_values[i]) / 100;
		y = y1;
		if (!a->mirror ^ !!a->numbers_outside)
		    y += bt1 + 2 + descent(a);
		else
		    y -= bt2 + 2 + ascent(a);
		if (i == a->tics - 1)
		  /* RHS - right justify it */
		  x -= wid;
		else if (i > 0)
		  /* Middle tics, center them */
		  x -= wid/2;
		else {
		  /* Left hand edge, left justify it. */
		}
	    }
	    AtTextPSDraw(f, a->tic_text[i], x, y); 
	}
    }

    /* draw the label */
    if (a->label_text) {
	int x,y;
	x = (x1 + x2)/2;
	y = (y1 + y2)/2;
	if (a->vertical) {
	    if (a->mirror) 
		x += bt2 + 2 + max_num_width + 2;
	    else
		x -= bt1 + max_num_width + 2 + AtTextPSWidth(a->label_text);
	    y -= AtTextPSAscent(a->label_text) / 2 -
	      AtTextPSDescent(a->label_text) / 2;
	}
	else {
	    if (a->mirror) {
		y += bt1 + 2 +  AtTextPSDescent(a->label_text);
		if (a->draw_numbers)
		    y += 2 + ascent(a) + descent(a);
	    }
	    else {
		y -= bt2 + 3 + AtTextPSAscent(a->label_text);
		if (a->draw_numbers)
		    y -=  2 + ascent(a) + descent(a);
	    }
	    x -= AtTextPSWidth(a->label_text)/2;
	}
	AtTextPSDraw(f, a->label_text, x, y);
    }
    fprintf(f, "grestore\n");
    fprintf(f, "%%%% EndObject\n");
}
#undef ascent
#undef descent

/*
 * Local Variables:
 * eval: (set-c-style 'GNU)
 * End:
 */
