#include "ps.h"

void printFile(ostream& out){
  out<<"%!PS-Adobe-3.0 EPSF-3.0\n"
     <<"%%BoundingBox: 0 0 595 842\n"
     <<"%%Title: increment.ps\n"
     <<"%%Creator: Ross Andrews\n"
     <<"%%CreationDate: Wed Aug 11 15:54:27 2004\n"
     <<"%%EndComments\n"
     <<"64 dict begin\n"
     <<"/inch {72 mul} def\n"
     <<"/cm {28.34 mul} def\n"
     <<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
     <<"/TENHEIGHT 1.5 cm def \n"
     <<"/NUMTICKS 37 def\n"
     <<"/TICKWIDTH 0.5 cm def\n"
     <<"/BUFFER 0.75 inch def\n"
     <<"/BORDERX 5.5 inch NUMTICKS TICKWIDTH mul 2 div sub BUFFER sub def\n"
     <<"/BORDERY 4.25 inch TENHEIGHT 5 mul sub BUFFER sub def\n"
     <<"/GRIDX BORDERX BUFFER add def\n"
     <<"/GRIDY BORDERY BUFFER add def\n"
     <<"/GS 3 def % standard dimension of the glyphs in points.\n"
     <<"/XLABEL (Number of Racks Selected) def\n"
     <<"/YLABEL (Successful Groups (%)) def\n"
     <<"/TITLE (Increment-Length Resources) def\n"
     <<"/FONTSIZE 10 def\n"
     <<"/TEXTCELL 1.2 inch def\n"
     <<"/NUMBERCELL 0.25 inch def\n"
     <<"<< /PageSize [11 inch 8.5 inch] >> setpagedevice\n"
     <<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
     <<"% Converts a graph point to a location on the page\n"
     <<"% pt pointX loc\n"
     <<"/pointX {\n"
     <<"    TICKWIDTH mul GRIDX add TICKWIDTH 2 div add\n"
     <<"} def\n"
     <<"% Converts a graph point to a location on the page\n"
     <<"% pt pointY loc\n"
     <<"/pointY {\n"
     <<"    TENHEIGHT mul 10 div GRIDY add\n"
     <<"} def\n"
     <<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
     <<"% drawGrid\n"
     <<"% (BORDERX, BORDERY) is the lower-left corner.\n"
     <<"% Draws a rectangle, then a bunch of horizontal lines,\n"
     <<"% then a bunch of tickmarks on the middle line,\n"
     <<"% and some labels. Everything but the lines.\n"
     <<"/drawGrid {\n"
     <<"    16 dict begin\n"
     <<"	% Draw box border around page\n"
     <<"	BORDERX BORDERY moveto\n"
     <<"	0 TENHEIGHT 10 mul BUFFER 2 mul add rlineto\n"
     <<"	NUMTICKS TICKWIDTH mul BUFFER 2 mul add 0 rlineto \n"
     <<"	0 -1 TENHEIGHT 10 mul BUFFER 2 mul add mul rlineto\n"
     <<"	closepath\n"
     <<"	stroke\n"
     <<"	% draw horizontal lines\n"
     <<"	newpath\n"
     <<"	GRIDX GRIDY moveto\n"
     <<"	0 1 10 {\n"
     <<"	    TENHEIGHT mul GRIDY add GRIDX exch moveto\n"
     <<"	    NUMTICKS TICKWIDTH mul 0 rlineto\n"
     <<"	    -1 NUMTICKS TICKWIDTH mul mul 0 rmoveto\n"
     <<"	} for\n"
     <<"	stroke\n"
     <<"	% draw vertical lines\n"
     <<"	newpath\n"
     <<"	GRIDX GRIDY moveto\n"
     <<"	0 TENHEIGHT 10 mul rlineto\n"
     <<"	stroke\n"
     <<"	% draw tickmarks on line 5\n"
     <<"	newpath\n"
     <<"	GRIDX GRIDY 5 TENHEIGHT mul add moveto\n"
     <<"	0 1 NUMTICKS {\n"
     <<"	    0 -0.1 cm rlineto\n"
     <<"	    TICKWIDTH 0.1 cm rmoveto\n"
     <<"	    pop % toss useless control variable.\n"
     <<"	} for\n"
     <<"	stroke\n"
     <<"	% Draw labels on tickmarks\n"
     <<"	newpath\n"
     <<"	/Courier findfont 6 scalefont setfont\n"
     <<"	0 1 NUMTICKS 1 sub {\n"
     <<"	    /num exch def\n"
     <<"	    /numstr num 1 add 5 string cvs def\n"
     <<"	    num TICKWIDTH mul GRIDX add GRIDY 5 TENHEIGHT mul add -0.3 cm add moveto\n"
     <<"	    TICKWIDTH 2 div numstr stringwidth pop 2 div sub 0 rmoveto\n"
     <<"	    numstr show\n"
     <<"	} for\n"
     <<"	% Draw labels on axes\n"
     <<"	/Times-Roman findfont 16 scalefont setfont \n"
     <<"	GRIDX NUMTICKS TICKWIDTH mul 2 div TITLE stringwidth pop 2 div sub add\n"
     <<"	    GRIDY 10 TENHEIGHT mul add 0.5 cm add moveto\n"
     <<"	TITLE show	\n"
     <<"	/Times-Roman findfont 10 scalefont setfont\n"
     <<"	newpath\n"
     <<"	GRIDX NUMTICKS TICKWIDTH mul 2 div XLABEL stringwidth pop 2 div sub add\n"
     <<"	    GRIDY 0.5 cm sub moveto\n"
     <<"	XLABEL show\n"
     <<"	GRIDX 0.3 cm sub GRIDY 5 TENHEIGHT mul add YLABEL stringwidth pop 2 div sub moveto\n"
     <<"	90 rotate\n"
     <<"	YLABEL show\n"
     <<"    end\n"
     <<"} def\n"
     <<"% Draws a line from x to x+(length of points),\n"
     <<"% calling glyph for each point with the page coords\n"
     <<"% of the point on the stack\n"
     <<"% x glyph points drawLine\n"
     <<"/drawLine {\n"
     <<"    10 dict begin\n"
     <<"	/points exch def\n"
     <<"	/x exch def\n"
     <<"	/glyph exch def\n"
     <<"	/numpoints points length def\n"
     <<"	newpath\n"
     <<"	x pointX points 0 get pointY moveto\n"
     <<"	x pointX points 0 get pointY gsave newpath moveto glyph cvx exec grestore\n"
     <<"	1 1 numpoints 1 sub {\n"
     <<"	    /x x 1 add def\n"
     <<"	    /n exch def\n"
     <<"	    x pointX points n get pointY lineto\n"
     <<"	    x pointX points n get pointY gsave newpath moveto glyph cvx exec grestore\n"
     <<"	} for\n"
     <<"	stroke\n"
     <<"    end\n"
     <<"} def\n"
     <<"% Draws a label at x,y with the text lbl\n"
     <<"% x and y are graph points, and are converted automatically to page points.\n"
     <<"% x y lbl drawLabel\n"
     <<"/drawLabel {\n"
     <<"    10 dict begin\n"
     <<"	/lbl exch def\n"
     <<"	/y exch def\n"
     <<"	/x exch def\n"
     <<"	newpath\n"
     <<"	x pointX y pointY moveto\n"
     <<"	0.5 inch 0 rlineto\n"
     <<"	stroke\n"
     <<"	newpath\n"
     <<"	x pointX 0.6 inch add y pointY 3 sub moveto\n"
     <<"	/Times-Roman findfont 8 scalefont setfont\n"
     <<"	lbl show\n"
     <<"    end\n"
     <<"} def\n"

     <<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
     <<"%%% Table functions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
     <<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
     <<"% x y rows columns cellwidth header? title drawTable\n"
     <<"% Draws the outline of a table at the specified (x,y) with rows and columns, adding one extra row for column headers, and centering a title.\n"
     <<"/drawTable {\n"
     <<"10 dict begin\n"
     <<"/title exch def\n"
     <<"/header exch def\n"
     <<"/cellwidth exch def\n"
     <<"/columns exch def\n"
     <<"/rows exch 1 add def\n"
     <<"/y exch def\n"
     <<"/x exch def\n"
     <<"\n"
     <<"rows 1 sub -1 0 { % shading\n"
     <<"/n exch def\n"
     <<"n 2 mod 0 eq n rows 1 sub ne header 0 eq or and\n"
     <<"{\n"
     <<"gsave\n"
     <<"0.8 0.8 0.8 setrgbcolor\n"
     <<"newpath\n"
     <<"n FONTSIZE mul y add x exch moveto\n"
     <<"cellwidth columns mul 0 rlineto\n"
     <<"0 FONTSIZE rlineto\n"
     <<"cellwidth columns mul -1 mul 0 rlineto\n"
     <<"closepath\n"
     <<"fill\n"
     <<"grestore\n"
     <<"} if\n"
     <<"} for\n"
     <<"\n"
     <<"newpath % draw outline\n"
     <<"x y moveto\n"
     <<"2 setlinewidth\n"
     <<"0 FONTSIZE rows mul rlineto\n"
     <<"cellwidth columns mul 0 rlineto\n"
     <<"0 -1 FONTSIZE mul rows mul rlineto\n"
     <<"closepath\n"
     <<"\n"
     <<"header 0 ne\n"
     <<"{\n"
     <<"x y rows 1 sub FONTSIZE mul add moveto\n"
     <<"cellwidth columns mul 0 rlineto    \n"
     <<"} if\n"
     <<"\n"
     <<"stroke\n"
     <<"\n"
     <<"1 setlinewidth\n"
     <<"\n"
     <<"1 1 columns{ % vertical lines\n"
     <<"newpath\n"
     <<"cellwidth mul x add y moveto\n"
     <<"0 rows FONTSIZE mul rlineto\n"
     <<"stroke\n"
     <<"} for\n"
     <<"\n"
     <<"newpath % title\n"
     <<"/Times-Roman findfont FONTSIZE scalefont setfont\n"
     <<"x cellwidth columns mul 2 div add title stringwidth pop 2 div sub\n"
     <<"y FONTSIZE rows mul add 2 add moveto\n"
     <<"title show\n"
     <<"\n"
     <<"end\n"
     <<"} def\n"
     <<"\n"
     <<"% x y header? title columndata tableColumn\n"
     <<"/tableColumn {\n"
     <<"10 dict begin\n"
     <<"/columndata exch def\n"
     <<"/title exch def\n"
     <<"/header exch def\n"
     <<"/y exch def\n"
     <<"/x exch def\n"
     <<"/rows columndata length 1 sub def\n"
     <<"\n"
     <<"0 1 rows{\n"
     <<"/n exch def\n"
     <<"newpath\n"
     <<"rows n sub FONTSIZE mul y add 2 add x 2 add exch moveto\n"
     <<"/Times-Roman findfont FONTSIZE scalefont setfont\n"
     <<"columndata n get show\n"
     <<"} for\n"
     <<"\n"
     <<"newpath\n"
     <<"x 2 add y 2 add rows 1 add FONTSIZE mul add moveto\n"
     <<"/Times-Roman findfont FONTSIZE scalefont setfont\n"
     <<"title show\n"
     <<"\n"
     <<"end\n"
     <<"} def\n"
     <<"\n"
     <<"% x y cellwidth rowdata tableRow\n"
     <<"/tableRow {\n"
     <<"10 dict begin\n"
     <<"/rowdata exch def\n"
     <<"/cellwidth exch def\n"
     <<"/y exch def\n"
     <<"/x exch def\n"
     <<"/columns rowdata length 1 sub def\n"
     <<"\n"
     <<"0 1 columns{\n"
     <<"/n exch def\n"
     <<"newpath\n"
     <<"n cellwidth mul x add 1 add y 1 add moveto\n"
     <<"/Times-Roman findfont FONTSIZE 1 sub scalefont setfont\n"
     <<"rowdata n get show\n"
     <<"} for\n"
     <<"\n"
     <<"end\n"
     <<"} def\n"
     <<"\n"
     <<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
     <<"%%% End table functions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
     <<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
     <<"% glyph functions\n"
     <<"% These draw a glyph centered on currentpoint.\n"
     <<"% They run inside a gsave/grestore sandbox,\n"
     <<"% newpath is already called when they start,\n"
     <<"% and GS is a defined constant to make their\n"
     <<"% sizes uniform.\n"
     <<"/circle { currentpoint GS 0 360 arc fill } def\n"
     <<"/cross {\n"
     <<"    -1 GS mul GS rmoveto\n"
     <<"    2 GS mul -2 GS mul rlineto\n"
     <<"    0 2 GS mul rmoveto -2 GS mul dup rlineto\n"
     <<"    stroke\n"
     <<"} def\n"
     <<"/plus {\n"
     <<"    -1 GS mul 0 rmoveto\n"
     <<"    2 GS mul 0 rlineto\n"
     <<"    -1 GS mul GS rmoveto\n"
     <<"    0 -2 GS mul rlineto\n"
     <<"    stroke\n"
     <<"} def\n"
     <<"/square {\n"
     <<"    -1 GS mul dup rmoveto\n"
     <<"    0 2 GS mul rlineto\n"
     <<"    2 GS mul 0 rlineto\n"
     <<"    0 -2 GS mul rlineto closepath\n"
     <<"    fill\n"
     <<"} def\n"
     <<"/diamond {\n"
     <<"    0 -1 GS mul rmoveto\n"
     <<"    GS GS rlineto\n"
     <<"    -1 GS mul GS rlineto\n"
     <<"    -1 GS mul dup rlineto closepath\n"
     <<"    fill\n"
     <<"} def\n"
     <<"/asterisk {\n"
     <<"    0 45 135{\n"
     <<"	rotate\n"
     <<"	-1 GS mul 0 rmoveto\n"
     <<"	2 GS mul 0 rlineto\n"
     <<"	-1 GS mul 0 rmoveto\n"
     <<"    } for\n"
     <<"    stroke\n"
     <<"} def\n"
     <<"/underscore{\n"
     <<"    -2 GS div 0 rmoveto\n"
     <<"    GS 0 rlineto\n"
     <<"    stroke\n"
     <<"} def\n"
     <<"/triangle{\n"
     <<"    2 2 scale\n"
     <<"    -0.5 GS mul -0.5 GS mul rmoveto\n"
     <<"    -30 rotate 0 GS rlineto\n"
     <<"    -120 rotate 0 GS rlineto closepath\n"
     <<"    fill\n"
     <<"} def\n"
     <<"/smallcircle { 0.7 0.7 scale circle } def\n"
     <<"/smallsquare { 0.7 0.7 scale square } def\n"
     <<"/smalldiamond { 0.7 0.7 scale diamond } def\n"
     <<"/smallasterisk { 0.7 0.7 scale asterisk } def\n"
     <<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
     <<"% Line example:\n"
     <<"%128 0 128 setrgbcolor (circle) 0 [ 100 96.5 88.3 74 66 50 32 13 ] drawLine\n";
}

void printFooter(ostream& out){
  out<<"newpath\n"
     <<"drawGrid\n"
     <<"showpage\n";
}

void printTable(ostream &out,int numpoints,vector<string> sumnames){
  int sumcount=sumnames.size();
  out<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
     <<"%%% Table whatever %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
     <<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
     <<"\n"
     <<"0.5 inch TEXTCELL add %x\n"
     <<" 7 inch 15 "<<sumcount<<" add FONTSIZE mul sub %y\n"
     <<" 14 "<<sumcount<<" add %rows\n"
     <<numpoints<<" %columns\n"
     <<" NUMBERCELL %cellwidth\n"
     <<"0 %header\n"
     <<"() %title\n"
     <<" drawTable\n"
     <<"\n"
     <<"0.5 inch %x\n"
     <<"7 inch 15 "<<sumcount<<" add FONTSIZE mul sub %y\n"
     <<"14 "<<sumcount<<" add %rows\n"
     <<"1 %columns\n"
     <<"TEXTCELL %cellwidth\n"
     <<"0 %header\n"
     <<"() %title\n"
     <<"drawTable\n"
     <<"\n"
     <<"0.5 inch %x\n"
     <<"7 inch 15 "<<sumcount<<" add FONTSIZE mul sub %y\n"
     <<"0 %header\n"
     <<"() %title\n"
     <<"[ %columndata\n"
     <<" (Any exceeded)\n"
     <<" (Crew training)\n"
     <<" (Crewtime)\n"
     <<" (Upmass)\n"
     <<" (Keepalive power)\n"
     <<" (Stowage)\n"
     <<" (Middecks)\n"
     <<" (Energy)\n"
     <<" (Crew-tended energy)\n"
     <<" (Acoustics)\n"
     <<" (C.T. acoustics)\n"
     <<" (MTL)\n"
     <<" (Crew-tended MTL)\n"
     <<" (LTL)\n"
     <<" (Crew-tended LTL)\n";
  for(unsigned int n=0;n!=sumnames.size();n++)
    {
      out<<" ("<<sumnames[n]<<")\n";
    }
  out<<"] tableColumn\n\n";
}

void printTableFooter(ostream &out){
  out<<"\nshowpage\nend\n%%EOF\n";
}
