home *** CD-ROM | disk | FTP | other *** search
- /*
- * hippoplotPS.c - Postscript routines for displaying hippo ntuples.
- *
- * Copyright (C) 1991 The Board of Trustees of The Leland Stanford
- * Junior University. All Rights Reserved.
- *
- * $ID$
- *
- * by Paul Rensing, Apr. 8, 1992
- */
-
- #include <string.h>
- #include <stdlib.h>
- #include <math.h>
- #include "hippo.h"
- #include "hippoplotPS.h"
- #include "hippoutil.h"
-
- /* various globals */
-
- GLOB_QUAL const char hippoplotPS_c_rcsid[] =
- "$Id: hippoplotPS.c,v 1.2 1992/04/10 01:37:29 rensing Rel $";
-
- GLOB_QUAL const char hippoplotPS_h_rcsid[] = _HIPPOPLOTPS_H_RCSID_;
-
- #define MAXNPTS_PRINT 500
-
- static struct {
- float xScale;
- float yScale;
- float xMarg;
- float yMarg;
- float xOrig;
- float yOrig;
- float xMax;
- float yMax;
- } data;
- static float bbox[4]; /* bounding box required by DPSDoUserPath */
-
- static FILE *outfile = stdout;
- static int inPage = 0;
- static int nPages = 0;
-
- int initDrvr_PS( FILE *fl )
- {
- outfile = fl;
- fprintf(outfile,"%%!PS-Adobe-2.0\n");
- fprintf(outfile,"%%%%Creator: hippoplotamus\n");
- fprintf(outfile,"%%%%Pages: (atend) 1\n");
- fprintf(outfile,"%%%%BoundingBox: (atend)\n");
- fprintf(outfile,"%%%%EndComments\n");
- fprintf(outfile,"\n\n");
-
- /* NeXT's magic number for fast drawing */
- fprintf(outfile,"0.15 setlinewidth\n");
-
- return 0;
- }
-
- int endDrvr_PS( void )
- {
- endPage_PS();
- fprintf(outfile,"%%%%Pages: %d %d\n",nPages,nPages);
- return 0;
- }
-
-
- int initPlot_PS( rectangle *draw, rectangle *margin, rectangle *user )
- {
- data.xScale = margin->size.width/user->size.width;
- data.yScale = margin->size.height/user->size.height;
- data.xMarg = margin->origin.x;
- data.yMarg = margin->origin.y;
- data.xOrig = user->origin.x;
- data.yOrig = user->origin.y;
- data.xMax = user->origin.x + user->size.width;
- data.yMax = user->origin.y + user->size.height;
-
- /* specify bounding box (add a small margin for safety) */
- bbox[0] = data.xOrig -
- (data.xMarg - draw->origin.x)/data.xScale;
- bbox[1] = data.yOrig -
- (data.yMarg - draw->origin.y)/data.yScale;
- bbox[2] = data.xOrig + user->size.width +
- (draw->size.width + draw->origin.x -
- (data.xMarg+margin->size.width))/data.xScale;
- bbox[3] = data.yOrig + user->size.height +
- (draw->size.height + draw->origin.y -
- (data.yMarg + margin->size.height))/data.yScale;
-
- inPage = 1;
-
- fprintf(outfile,"%%\n%%\n");
-
- return 0;
- }
-
- int endPage_PS( void )
- {
- if (inPage)
- {
- fprintf(outfile,"showpage\n");
- fprintf(outfile,"%%\n%%\n");
-
- /* NeXT's magic number for fast drawing */
- fprintf(outfile,"0.15 setlinewidth\n");
- }
- inPage = 0;
-
- return 0;
- }
-
- int drawLine_PS(float *coords, int nCoords, linestyle_t linest)
- {
- int i;
-
- int repeatCount; /* this number + 32 must be < 255 */
- /* to display and < 127 for printing */
- /* to fit in 1 char */
- float *co;
- int count;
- float ctm_matrix[6] = {1.0/data.xScale, 0.0, 0.0,
- 1.0/data.yScale, 0.0, 0.0 };
-
- fprintf(outfile,"%% drawLine_PS\n");
-
- repeatCount = 92; /* work around apparent bug */
- fprintf(outfile,"gsave ");
-
- /* get into user coords */
- fprintf(outfile,"%f %f translate ",data.xMarg, data.yMarg);
- fprintf(outfile,"%f %f scale ",data.xScale, data.yScale);
- fprintf(outfile,"%f %f translate\n", -data.xOrig, -data.yOrig);
-
- co = coords;
- do
- {
- if (nCoords > repeatCount) count = repeatCount;
- else count = nCoords;
-
- fprintf(outfile,"gsave\nnewpath systemdict begin\n");
- fprintf(outfile,"%f %f moveto\n",*co++,*co++);
- for (i=1; i<count; i++)
- fprintf(outfile,"%f %f lineto\n",*co++, *co++);
- fprintf(outfile,"end\n[%f %f %f %f %f %f] concat\n",
- ctm_matrix[0],ctm_matrix[1],ctm_matrix[2],
- ctm_matrix[3],ctm_matrix[4],ctm_matrix[5]);
- fprintf(outfile,"stroke grestore\n");
- co += 2*(count - 1);
- nCoords -= (count - 1);
- }
- while (nCoords > 1); /* 1 because we always back up 1 point */
- /* to connect the segments */
-
- fprintf(outfile,"grestore\n");
- return 0;
- }
-
-
-
- int drawPoints_PS(float *coords, int nCoords, plotsymbol_t symbol,
- float symbolSize)
- {
-
- int i;
- int filled=0;
- int count;
- int maxnpts;
- float xPointSide, yPointSide;
- float xHalfPointSide, yHalfPointSide;
- float ctm_matrix[6] = {1.0/data.xScale, 0.0, 0.0, 1.0/data.yScale, 0.0, 0.0 };
-
- fprintf(outfile,"%% drawPoints_PS\n");
-
- maxnpts = MAXNPTS_PRINT;
-
- if (symbol == SOLIDSQUARE)
- filled = 1;
-
- if (filled) maxnpts /= 5;
-
- xPointSide = symbolSize / data.xScale;
- yPointSide = symbolSize / data.yScale;
- xHalfPointSide = xPointSide / 2.0;
- yHalfPointSide = yPointSide / 2.0;
-
- fprintf(outfile,"gsave\n");
-
- /* get into user coords */
- fprintf(outfile,"%f %f translate ", data.xMarg, data.yMarg);
- fprintf(outfile,"%f %f scale ", data.xScale, data.yScale);
- fprintf(outfile,"%f %f translate\n", -data.xOrig, -data.yOrig);
-
- if (nCoords > maxnpts) i = maxnpts;
- else i = nCoords;
- do
- {
- if (nCoords > maxnpts) count = maxnpts;
- else count = nCoords;
-
- fprintf(outfile,"gsave\nnewpath systemdict begin\n");
-
- switch (symbol)
- {
- case SQUARE:
- case SOLIDSQUARE:
- for (i = 0; i < count; i++)
- {
- fprintf(outfile,"%f %f moveto\n",
- *coords++ - xHalfPointSide,
- *coords++ - yHalfPointSide);
- fprintf(outfile,"%f %f rlineto\n", +xPointSide, 0.0);
- fprintf(outfile,"%f %f rlineto\n", +0.0, +yPointSide);
- fprintf(outfile,"%f %f rlineto\n", -xPointSide, +0.0);
- fprintf(outfile,"closepath\n");
- }
- break;
-
- case PLUS:
- for (i = 0; i < count; i++)
- {
- fprintf(outfile,"%f %f moveto\n",
- *coords++ - xHalfPointSide,
- *coords++);
- fprintf(outfile,"%f %f rlineto\n", xPointSide, 0.0);
- fprintf(outfile,"%f %f moveto\n", -xHalfPointSide,
- yHalfPointSide);
- fprintf(outfile,"%f %f rlineto\n", 0.0, -yPointSide);
- }
- break;
-
- case TIMES:
- for (i = 0; i < count; i++)
- {
- fprintf(outfile,"%f %f moveto\n",
- *coords++ - xHalfPointSide,
- *coords++ - yHalfPointSide);
- fprintf(outfile,"%f %f rlineto\n", xPointSide, yPointSide);
- fprintf(outfile,"%f %f moveto\n", 0.0, yPointSide);
- fprintf(outfile,"%f %f rlineto\n", xPointSide,
- -yPointSide);
- }
- break;
-
- default:
- break;
- }
- if (filled)
- {
- fprintf(outfile,"end\n");
- fprintf(outfile,"fill grestore\n");
- }
- else
- {
- fprintf(outfile,"end\n[%f %f %f %f %f %f] concat\n",
- ctm_matrix[0],ctm_matrix[1],ctm_matrix[2],
- ctm_matrix[3],ctm_matrix[4],ctm_matrix[5]);
- fprintf(outfile,"stroke grestore\n");
- }
-
- nCoords -= count;
- }
- while (nCoords > 0);
-
- fprintf(outfile,"grestore\n");
-
- return 0;
- }
-
-
- int drawYError_PS(float *coords, float *errorYs, int nCoords)
- {
- #define ERRORCAPSIZE 2.0
-
- int i;
- int count;
- int maxnpts;
- float x, yhi, ylo;
- float halfErrorCap = ERRORCAPSIZE/data.xScale * 0.5;
- float ctm_matrix[6] = {1.0/data.xScale, 0.0, 0.0, 1.0/data.yScale, 0.0, 0.0 };
-
- fprintf(outfile,"%% drawYError_PS\n");
-
- maxnpts = MAXNPTS_PRINT;
-
- if (nCoords > maxnpts) i = maxnpts;
- else i = nCoords;
-
- fprintf(outfile,"gsave\n");
-
- /* get into user coords */
- fprintf(outfile,"%f %f translate ", data.xMarg, data.yMarg);
- fprintf(outfile,"%f %f scale ", data.xScale, data.yScale);
- fprintf(outfile,"%f %f translate\n", -data.xOrig, -data.yOrig);
-
- do
- {
- if (nCoords > maxnpts) count = maxnpts;
- else count = nCoords;
-
- fprintf(outfile,"gsave\nnewpath systemdict begin\n");
- /* fprintf(outfile,"%f %f %f %f setbbox\n",bbox[0],bbox[1],
- bbox[2],bbox[3]);
- */
- for (i = 0; i < count; i++)
- {
- x = *coords++; /* x coord */
- coords++; /* skip over this y coord - not needed */
- yhi = *errorYs++; /* y of high end of error bar */
- ylo = *errorYs++; /* y of low end of error bar */
-
- fprintf(outfile,"%f %f moveto\n",x,ylo);
- fprintf(outfile,"%f %f lineto\n",x,yhi);
- fprintf(outfile,"%f %f moveto\n", x - halfErrorCap, yhi);
- fprintf(outfile,"%f %f lineto\n", x + halfErrorCap, yhi);
- fprintf(outfile,"%f %f moveto\n", x - halfErrorCap, ylo);
- fprintf(outfile,"%f %f lineto\n", x + halfErrorCap, ylo);
- }
-
- fprintf(outfile,"end\n[%f %f %f %f %f %f] concat\n",
- ctm_matrix[0],ctm_matrix[1],ctm_matrix[2],
- ctm_matrix[3],ctm_matrix[4],ctm_matrix[5]);
- fprintf(outfile,"stroke grestore\n");
- nCoords -= count;
- }
- while (nCoords > 0);
-
- fprintf(outfile,"grestore\n");
-
- return 0;
- }
-
-
- int drawXError_PS(float *coords, float *errorXs, int nCoords)
- {
- int i;
- int count;
- int maxnpts;
- float halfErrorCap = ERRORCAPSIZE/data.yScale * 0.5;
- float y, xlo, xhi;
- float ctm_matrix[6] = {1.0/data.xScale, 0.0, 0.0, 1.0/data.yScale, 0.0, 0.0 };
-
- fprintf(outfile,"%% drawXError_PS\n");
-
- maxnpts = MAXNPTS_PRINT;
-
- if (nCoords > maxnpts) i = maxnpts;
- else i = nCoords;
-
- fprintf(outfile,"gsave\n");
-
- /* get into user coords */
- fprintf(outfile,"%f %f translate ", data.xMarg, data.yMarg);
- fprintf(outfile,"%f %f scale ", data.xScale, data.yScale);
- fprintf(outfile,"%f %f translate\n", -data.xOrig, -data.yOrig);
-
- do
- {
- if (nCoords > maxnpts) count = maxnpts;
- else count = nCoords;
-
- fprintf(outfile,"gsave\nnewpath systemdict begin\n");
- /* fprintf(outfile,"%f %f %f %f setbbox\n",bbox[0],bbox[1],
- bbox[2],bbox[3]);
- */
- for (i = 0; i < count; i++)
- {
- coords++; /* skip over this x coord - not needed */
- y = *coords++; /* y coord */
- xhi = *errorXs++; /* x of high end of error bar */
- xlo = *errorXs++; /* x of low end of error bar */
-
- fprintf(outfile,"%f %f moveto\n",xlo,y);
- fprintf(outfile,"%f %f lineto\n",xhi,y);
- fprintf(outfile,"%f %f moveto\n", xlo, y - halfErrorCap);
- fprintf(outfile,"%f %f lineto\n", xlo, y + halfErrorCap);
- fprintf(outfile,"%f %f moveto\n", xhi, y - halfErrorCap);
- fprintf(outfile,"%f %f lineto\n", xhi, y + halfErrorCap);
- }
-
- fprintf(outfile,"end\n[%f %f %f %f %f %f] concat\n",
- ctm_matrix[0],ctm_matrix[1],ctm_matrix[2],
- ctm_matrix[3],ctm_matrix[4],ctm_matrix[5]);
- fprintf(outfile,"stroke grestore\n");
- nCoords -= count;
- }
- while (nCoords > 0);
-
- fprintf(outfile,"grestore\n");
-
- return 0;
- }
-
- int drawYTicks_PS( float *y, int nt, float tickwidth, int side )
- {
- int i;
- float ctm_matrix[6] = {1.0/data.xScale, 0.0, 0.0, 1.0/data.yScale, 0.0, 0.0 };
- float start;
-
- tickwidth /= data.xScale;
-
- fprintf(outfile,"%% drawYTicks_PS\n");
-
- if (side == 0)
- start = data.xOrig;
- else
- start = data.xMax - tickwidth;
-
- fprintf(outfile,"gsave\n");
-
- /* get into user coords */
- fprintf(outfile,"%f %f translate ", data.xMarg, data.yMarg);
- fprintf(outfile,"%f %f scale ", data.xScale, data.yScale);
- fprintf(outfile,"%f %f translate\n",-data.xOrig, -data.yOrig);
-
- fprintf(outfile,"newpath systemdict begin\n");
- /* fprintf(outfile,"%f %f %f %f setbbox\n",bbox[0],bbox[1],
- bbox[2],bbox[3]);
- */
- for (i = 0; i < nt; i++)
- {
- fprintf(outfile,"%f %f moveto ", start, y[i] );
- fprintf(outfile,"%f %f rlineto\n", tickwidth, 0.0 );
- }
- fprintf(outfile,"end\n[%f %f %f %f %f %f] concat\n",
- ctm_matrix[0],ctm_matrix[1],ctm_matrix[2],
- ctm_matrix[3],ctm_matrix[4],ctm_matrix[5]);
- fprintf(outfile,"stroke\n");
-
- fprintf(outfile,"grestore\n");
-
- return 0;
- }
-
- int drawXTicks_PS( float *y, int nt, float tickwidth, int side )
- {
- int i;
- float ctm_matrix[6] = {1.0/data.xScale, 0.0, 0.0, 1.0/data.yScale, 0.0, 0.0 };
- float start;
-
- tickwidth /= data.yScale;
-
- fprintf(outfile,"%% drawXTicks_PS\n");
-
- if (side == 0)
- start = data.yOrig;
- else
- start = data.yMax - tickwidth;
-
- fprintf(outfile,"gsave\n");
-
- /* get into user coords */
- fprintf(outfile,"%f %f translate ", data.xMarg, data.yMarg);
- fprintf(outfile,"%f %f scale ", data.xScale, data.yScale);
- fprintf(outfile,"%f %f translate\n", -data.xOrig, -data.yOrig);
-
- fprintf(outfile,"newpath systemdict begin\n");
- /* fprintf(outfile,"%f %f %f %f setbbox\n",bbox[0],bbox[1],
- bbox[2],bbox[3]);
- */
- for (i = 0; i < nt; i++)
- {
- fprintf(outfile,"%f %f moveto ", y[i], start );
- fprintf(outfile,"%f %f rlineto\n", 0.0, tickwidth );
- }
- fprintf(outfile,"end\n[%f %f %f %f %f %f] concat\n",
- ctm_matrix[0],ctm_matrix[1],ctm_matrix[2],
- ctm_matrix[3],ctm_matrix[4],ctm_matrix[5]);
- fprintf(outfile,"stroke\n");
-
-
- fprintf(outfile,"grestore\n");
-
- return 0;
- }
-
- int drawMag_PS(float x, float y, int mag, float fontSize)
- {
- char str[10];
-
- fprintf(outfile,"%% drawMag_PS\n");
-
- fprintf(outfile,"(Helvetica) %f selectfont\n",fontSize);
- fprintf(outfile,"%f %f moveto\n", x, y );
- fprintf(outfile,"(x10) show");
- fprintf(outfile,"%f %f rmoveto\n", 0.0, 0.5*fontSize );
- sprintf(str,"%d",mag);
- fprintf(outfile,"(%s) show\n", str);
-
- return 0;
- }
-
-
- int drawText_PS(char *string, float x, float y, float fontSize,
- float rotation, char xAlign, char yAlign)
- {
- float xStep = 0.0, yStep = 0.0;
-
- fprintf(outfile,"%% drawText_PS\n");
-
- switch (yAlign)
- {
- case 'C':
- case 'c':
- yStep = 0.4;
- break;
-
- case 'T':
- case 't':
- yStep = 0.8;
- break;
- }
-
- switch (xAlign)
- {
- case 'C':
- case 'c':
- xStep = 0.5;
- break;
- case 'R':
- case 'r':
- xStep = 1.0;
- break;
- }
-
- fprintf(outfile,"gsave\n");
- fprintf(outfile,"(Helvetica) %f selectfont\n", fontSize);
- fprintf(outfile,"%f %f translate %f rotate\n",x,y,rotation);
-
- fprintf(outfile,"(%s) stringwidth pop\n", string);
- fprintf(outfile,"%f neg mul %f moveto\n",xStep,-fontSize*yStep);
- fprintf(outfile,"(%s) show\n",string);
- fprintf(outfile,"grestore\n");
-
- return 0;
- }
-
-
-
- int drawColor2D_PS(int nXBins, int nYBins, rectangle *marginRect,
- float *data, float binMin, float binMax, int useColor )
- {
- int iYBin;
- int totBins = nXBins * nYBins;
- float deltaX = marginRect->size.width / nXBins;
- float deltaY = marginRect->size.height / nYBins;
- float grayScale, nearWhite = 0.9;
- float xCoord, yCoord;
- int i, count;
- int maxnpts = 30;
-
- fprintf(outfile,"%% drawColor2D_PS\n");
-
- /*
- * this routine leaves color in unpredictable state,
- * so we need a gsave/grestore
- */
- fprintf(outfile,"gsave\n");
-
- if (binMin == binMax)
- grayScale = 1.0;
- else
- grayScale = nearWhite / (binMax - binMin);
- xCoord = marginRect->origin.x;
- yCoord = marginRect->origin.y;
- iYBin = 0;
-
- do
- {
- if (totBins > maxnpts) count = maxnpts;
- else count = totBins;
-
- fprintf(outfile,"gsave\nnewpath systemdict begin\n");
- /* fprintf(outfile,"%f %f %f %f setbbox\n",bbox[0],bbox[1],
- bbox[2],bbox[3]);
- */
- for (i = 0; i < count; i++)
- {
- /* bins are arranged as bins[x][y] with y changing fastest */
- if (*data != 0)
- {
- fprintf(outfile,"%f setgray\n", (binMax- *data)*grayScale);
- fprintf(outfile,"%f %f moveto %f %f rlineto\n",
- xCoord, yCoord, deltaX, 0.0 );
- fprintf(outfile,"%f %f rlineto %f %f rlineto closepath\n",
- 0.0, deltaY, -deltaX, 0.0 );
- }
- data++;
- yCoord += deltaY;
- iYBin++;
- if ( iYBin == nYBins)
- {
- iYBin = 0;
- yCoord = marginRect->origin.y;
- xCoord += deltaX;
-
- }
- }
- fprintf(outfile,"end\n");
- fprintf(outfile,"fill grestore\n");
- totBins -= count;
-
- }
- while (totBins > 0);
-
-
- fprintf(outfile,"grestore\n");
- return 0;
- }
-
-
- int drawLego2D_PS(rectangle *marginRect)
- {
- float fontHeight = 0.2 * data.yMarg;
-
- fprintf(outfile,"%% drawLego2D_PS\n");
-
- if (fontHeight > 24.0) fontHeight = 24.0;
-
- drawText_PS("Not implemented yet.",
- marginRect->origin.x + marginRect->size.width*0.5,
- marginRect->origin.y + marginRect->size.height*0.5,
- fontHeight,0.0,'c','c');
- return 0;
- }
-
- int shade_PS(float xlow, float xhigh, float ylow, float yhigh )
- {
- fprintf(outfile,"%% shade_PS\n");
-
- fprintf(outfile,"gsave\n");
- /* PSsetalpha(0.0);*/
- fprintf(outfile,"%f setgray\n", 0.6);
- fprintf(outfile,"newpath systemdict begin\n");
- /* fprintf(outfile,"%f %f %f %f setbbox\n",bbox[0],bbox[1],
- bbox[2],bbox[3]);
- */
- fprintf(outfile,"%f %f moveto %f %f rlineto\n",
- xlow, ylow, xhigh-xlow, 0.0 );
- fprintf(outfile,"%f %f rlineto %f %f rlineto closepath\n",
- 0.0, yhigh-ylow, xlow-xhigh, 0.0 );
- fprintf(outfile,"end\n");
- fprintf(outfile,"fill grestore\n");
-
- return 0;
- }
-
-
-