#include "ps.h"

void printFile(ostream& out,int numticks,int neverrows,int partners,int point,string runname){

  int nevercols=1;
  if(neverrows>30)nevercols=2;

  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 "<<numticks<<" def\n"
     <<"/TICKWIDTH 22.5 cm NUMTICKS div def\n"
     <<"0.75 cm TICKWIDTH lt {/TICKWIDTH 0.75 cm def} if\n"
     <<"/BUFFER 0 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 0.5 cm add add def\n"
     <<"/GS 3 def % standard dimension of the glyphs in points.\n"
     <<"/XLABEL () def\n"
     <<"/YLABEL (Percent Selected in Successful Combinations) def\n"
     <<"/TITLE (Rack Selection Success) def\n"
     <<"\n"
     <<"%Second page parameters\n"
     <<"/NEVERROWS "<<neverrows<<" def\n"
     <<"/NEVERCOLUMNS "<<nevercols<<" def\n"
     <<"/PARTNERS "<<partners<<" def\n"
     <<"/RUNNAME ("<<runname<<") def\n"
     <<"/RESOURCELINE (Press Payloads Selected ("<<point<<"):) def\n"
     <<"\n"
     <<"% constants for the second page\n"
     <<"/TEXTCELL 2 inch def\n"
     <<"/NUMBERCELL 1 inch def % Widths of cell types\n"
     <<"/FONTSIZE 12 def\n"
     <<"/PAGEBORDER 0.5 inch def\n"
     <<"/RESOURCESX PAGEBORDER def\n"
     <<"/RESOURCESY 8.5 inch PAGEBORDER sub 11 FONTSIZE mul sub def\n"
     <<"/NEVERX PAGEBORDER def\n"
     <<"/NEVERY RESOURCESY PAGEBORDER sub NEVERROWS 1 add FONTSIZE mul sub def\n"
     <<"/RUNX PAGEBORDER 2 TEXTCELL mul add PAGEBORDER add def\n"
     <<"/RUNY RESOURCESY PAGEBORDER sub 10 FONTSIZE mul sub def\n"
     <<"\n"
     <<"<< /PageSize [11 inch 8.5 inch] >> setpagedevice\n"
     <<"\n"
     <<"\n"
     <<"\n"
     <<"\n"
     <<"\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"
     <<"gsave\n"
     <<"% draw horizontal lines\n"
     <<"newpath\n"
     <<"GRIDX GRIDY moveto\n"
     <<"NUMTICKS TICKWIDTH mul 0 rlineto\n"
     <<"stroke\n"
     <<"\n"
     <<"% draw vertical lines\n"
     <<"newpath\n"
     <<"GRIDX GRIDY moveto\n"
     <<"0 TENHEIGHT 10 mul rlineto\n"
     <<"stroke\n"
     <<"\n"
     <<"% draw tickmarks on baseline\n"
     <<"newpath\n"
     <<"GRIDX GRIDY 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"
     <<"\n"
     <<"% draw tickmarks on y axis\n"
     <<"newpath\n"
     <<"0 1 10 {\n"
     <<"/num exch def\n"
     <<"GRIDX 0.1 cm sub GRIDY num TENHEIGHT mul add moveto\n"
     <<"0.1 cm 0 rlineto\n"
     <<"} for\n"
     <<"stroke\n"
     <<"\n"
     <<"% Draw labels on tickmarks\n"
     <<"newpath\n"
     <<"/Courier findfont 6 scalefont setfont\n"
     <<"0 1 10 {\n"
     <<"/num exch def\n"
     <<"/numstr num 10 mul 3 string cvs def\n"
     <<"GRIDX 0.3 cm sub numstr stringwidth pop sub num TENHEIGHT mul GRIDY add 1.5 sub moveto\n"
     <<"numstr show\n"
     <<"} for\n"
     <<"\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 1 cm sub GRIDY 5 TENHEIGHT mul add YLABEL stringwidth pop 2 div sub moveto\n"
     <<"90 rotate\n"
     <<"YLABEL show\n"
     <<"grestore\n"
     <<"end\n"
     <<"} def\n"
     <<"\n"
     <<"\n"
     <<"\n"
     <<"\n"
     <<"\n"
     <<"\n"
     <<"% index name value drawbar -\n"
     <<"% Draws a bar for the rack \"name\" with value \"value\" at location \"index\", with label\n"
     <<"/drawBar {\n"
     <<"10 dict begin\n"
     <<"gsave\n"
     <<"/value exch def\n"
     <<"/name exch def\n"
     <<"/index exch def\n"
     <<"/height value 10 div TENHEIGHT mul def\n"
     <<"newpath\n"
     <<"GRIDX index TICKWIDTH mul add TICKWIDTH 4 div add GRIDY moveto\n"
     <<"0 height rlineto\n"
     <<"TICKWIDTH 2 div 0 rlineto\n"
     <<"0 -1 height mul rlineto\n"
     <<"closepath\n"
     <<"gsave\n"
     <<"0.6157 0.5451 1 setrgbcolor fill % Hex triplet 9d8bff\n"
     <<"grestore\n"
     <<"stroke\n"
     <<"newpath\n"
     <<"/fontsize TICKWIDTH 2 div def\n"
     <<"/Times-Roman findfont fontsize scalefont setfont\n"
     <<"GRIDX index TICKWIDTH mul add TICKWIDTH 0.5 mul add fontsize 4 div add GRIDY name stringwidth pop sub 0.3 cm sub moveto\n"
     <<"90 rotate\n"
     <<"name show\n"
     <<"grestore\n"
     <<"end\n"
     <<"} def\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"
     <<"\n"
     <<"drawGrid\n"
     <<"\n";
}

void printTables(ostream& out,int size){
  out<<"showpage\n"
     <<"\n"
     <<"\n"
     <<"RESOURCESX RESOURCESY 10 1 TEXTCELL 1 () drawTable % resources table\n"
     <<"RESOURCESX TEXTCELL add RESOURCESY 10 PARTNERS 1 add 1 inch 1 (Available Resource Consumed) drawTable % Resources table again\n"
     <<"\n"
     <<"RESOURCESX RESOURCESY 1 (Resource:) % Resources table labels\n"
     <<"[ RESOURCELINE\n"
     <<"(Crew Training:)\n"
     <<"(Middecks:)\n"
     <<"(Launched Upmass:)\n"
     <<"(Stowage (NASA only):)\n"
     <<"(Keep-Alive Power:)\n"
     <<"(Inc-Length MTL:)\n"
     <<"(Inc-Length Acoustics:)\n"
     <<"(Energy:)\n"
     <<"(Crew Time:) ] tableColumn\n"
     <<"\n"
     <<"NEVERX NEVERY "<<(size-1)<<" NEVERCOLUMNS TEXTCELL 0 (Never Selectable:) drawTable % Never table\n"
     <<"\n"
     <<"RUNX RUNY 8 2 TEXTCELL 0 RUNNAME drawTable % Run table\n"
     <<"\n"
     <<"RUNX RUNY 0 () % Run labels\n"
     <<"[ (Upmass (kg))\n"
     <<"(Att P/Ls (kg))\n"
     <<"(Middecks (MDLs))\n"
     <<"(Crew Time (hrs per week))\n"
     <<"(Crew Trng (hrs))\n"
     <<"(Stowage (racks))\n"
     <<"(Power (kW))\n"
     <<"(K/A Power (kW))\n"
     <<"(CAM Hskpg (kW)) ] tableColumn\n"
     <<"\n"
     <<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
     <<"\n";
}

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