// Copyright 2005 Thomas R. Davis
//
// This file is part of Pentamino.
//
// Pentamino is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Pentamino is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Pentamino; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#include <stdio.h>
#define WIDTH 12
#define HEIGHT 5

#define EDGE 8

char *color(int i)
{
  switch (i) {
  case 'x': return "0 .6 0";
  case 'i': return "0 0 1";
  case 'u': return "0 1 1";
  case 'z': return "1 0 0";
  case 'n': return "1 0 1";
  case 'v': return "1 1 0";
  case 't': return "0.5 0.5 0";
  case 'w': return "0 0.5 1";
  case 'p': return "1 .8 0.5";
  case 'f': return "0.5 0 0.5";
  case 'y': return "0 0.5 0.5";
  case 'l': return "0.5 1 0.5";
  }
}

void header()
{
  fprintf(stdout, "%%!PS-Adobe-2.0 EPSF-1.2\n");
  fprintf(stdout, "%%%%Creator: PSout\n");
  fprintf(stdout, "%%%%BoundingBox: 0 0 %d %d\n", 612, 792);
  fprintf(stdout, "%%%%EndComments\n");
  fprintf(stdout, "0 setlinewidth\n");
}

void footer()
{
  fprintf(stdout, "showpage\n");
}

int main(int argc, char *argv)
{
  FILE *f;
  char instr[62];
  int i, j, k;
  
  int xt = 22, yt = 51;
  int firstpage = 1;
  
  f = fopen("pent12x5final.txt", "r");
  
  header();
  
  while (1) {
    i = fscanf(f, "%s", instr);
    if (i == EOF) {
      fclose(f);
      return 0;
    }
    if (xt == 22 && yt == 51 && firstpage == 0)
        fprintf(stdout, "showpage\n");
    fprintf(stdout, "gsave\n");
    fprintf(stdout, "%d %d translate\n", xt, yt);
    xt += EDGE*12 + 22;
    firstpage = 0;
    if (xt > 612 - 22 - EDGE*12) {
      xt = 22;
      yt += EDGE*5 + 10;
      if (yt > 792 - 50 - EDGE*5) {
        yt = 51;
      }
    }
    k = 0;
    for (i = 0; i < WIDTH; i++)
      for (j = 0; j < HEIGHT; j++) {
        fprintf(stdout, "%s setrgbcolor\n", color(instr[k]));
        fprintf(stdout, "%d %d moveto\n", i*EDGE, j*EDGE);
        fprintf(stdout, "%d %d lineto\n", (i+1)*EDGE, j*EDGE);
        fprintf(stdout, "%d %d lineto\n", (i+1)*EDGE, (j+1)*EDGE);
        fprintf(stdout, "%d %d lineto\n", i*EDGE, (j+1)*EDGE);
        fprintf(stdout, "closepath fill\n");
        k++;
    }
    k = 0;
    for (i = 0; i < WIDTH; i++)
      for (j = 0; j < HEIGHT; j++) {
        fprintf(stdout, "0 0 0 setrgbcolor\n");
        fprintf(stdout, "%d %d moveto\n", i*EDGE, j*EDGE);
        fprintf(stdout, "%d %d lineto\n", (i+1)*EDGE, j*EDGE);
        fprintf(stdout, "%d %d lineto\n", (i+1)*EDGE, (j+1)*EDGE);
        fprintf(stdout, "%d %d lineto\n", i*EDGE, (j+1)*EDGE);
        fprintf(stdout, "closepath stroke\n");
        k++;
    }
    fprintf(stdout, "grestore\n");
  }
    footer();
    return 0;
}
