
/*
	GIER simulator - Get character catalog for plot

	(C) Copyright 2001-2018 by Mogens Kjaer


   This program 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.

   This program 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 this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include "GIER.h"

#define MARKa ONE40
#define MARKb ONE41
#define MARKc ONE40_41
#define MARKS ONE40_41

static GIERword *Buffer;
static FILE *PS;

void test(int t,double X0,double Y0,double H)
{
  long n1,n2,xx,yy,x1,y1,xxx,yyy;
  int i,inpath,jump;
  GIERword AA;
  int ind[2000];
  int *pi;
  void moveto(int x, int y)
  {
    double X,Y;
    if(inpath) fprintf(PS, "stroke\n");
    X=X0+x/6.0*H;
    Y=Y0+y/6.0*H;
    inpath=0;
    fprintf(PS, "%.4f %.4f moveto\n",X/2.54*72.0,Y/2.54*72.0);
  }
  void lineto(int x, int y)
  {
    double X,Y;
    inpath=1;
    X=X0+x/6.0*H;
    Y=Y0+y/6.0*H;
    fprintf(PS, "%.4f %.4f lineto\n",X/2.54*72.0,Y/2.54*72.0);
  }
  AA=Buffer[t];
  printf("AA[0]: %d/%d/%d/%d ",
	    (int) ((Buffer[t]&ONE0_9)>>SHIFT9),
	    (int) ((Buffer[t]&ONE10_19)>>SHIFT19),
	    (int) ((Buffer[t]&ONE20_29)>>SHIFT29),
	    (int) ((Buffer[t]&ONE30_39)>>SHIFT39));
  for(i=37;i>=2;i-=5)
  {
    printf(" %d",(int)((Buffer[t]>>i)&31));
  }
  printf("\n");
  printf("AA[1]: %d/%d/%d/%d ",
	    (int) ((Buffer[t+1]&ONE0_9)>>SHIFT9),
	    (int) ((Buffer[t+1]&ONE10_19)>>SHIFT19),
	    (int) ((Buffer[t+1]&ONE20_29)>>SHIFT29),
	    (int) ((Buffer[t+1]&ONE30_39)>>SHIFT39));
  for(i=37;i>=2;i-=5)
  {
    printf(" %d",(int)((Buffer[t+1]>>i)&31));
  }
  printf("\n");
  n1=(AA>>SHIFT4)&31;
  printf("n1 I: %ld\n",n1);
  y1=(AA>>SHIFT9)&31;
  x1=y1/6;
  y1=y1-x1*6;
  xx=x1;
  yy=y1;
  printf("%ld %ld\n",xx,yy);
  moveto(xx,yy);
  xxx=xx;
  yyy=yy;
  n2=(n1-8)*5;
  printf("n2: %ld\n",n2);
  n1=(n1<=7)?(n1*5):35;
  printf("n1 II: %ld\n",n1);
  printf("n1: %ld n2: %ld\n",n1,n2);
  pi=ind;
  for(i=10;i<=n1;i+=5) *pi++ = i;
  for(i=0;i<=n2;i+=5) *pi++=i;
  *pi++=-1;
  pi=ind;
  jump=0;
  for(;*pi>=0;pi++)
  {
    i=*pi;
    if(i==0) AA=Buffer[t+1];
    yy=(AA>>(41-(i+4)))&31;
    if(yy==31)
    {
      printf("-----------\n");
      jump=1;
      continue;
    }
    xx=yy/6;
    yy=(yy-xx*6)-y1;
    xx=xx-x1;
    if(xx==0 && yy==0) break;
    printf("%ld %ld\n",xx,yy);
    xxx+=xx; yyy+=yy;
    if(jump)
    {
      moveto(xxx,yyy);
      jump=0;
    }
    else
    {
      lineto(xxx,yyy);
    }
    x1+=xx;
    y1+=yy;
  }
  moveto(0,0);
}


int main(int argc, char **argv)
{
  FILE *fp;
  char buf[1024];
  char ch1,ch2;
  char *bp,*cp;
  GIERword c,c2,c50,c100;
  int i,j,start,len;
  double X,Y;

  fp = fopen("plotsymbols.dat", "r");

  if(fp == NULL)
  {
    exit(-1);
  }

  Buffer=(GIERword *) malloc(4096*sizeof(*Buffer));
  for(i=0;i<4096;i++) Buffer[i]=0;
  start=3856;
  len=240;

  while(1)
  {
    bp = fgets(buf, sizeof(buf), fp);
    if(bp == NULL) break;

    cp=strtok(bp, "\t\n");
    if(!cp) continue;

    if(!strncmp(cp, "Write Buffer[", 13))
    {
      sscanf(cp+13, "%d", &i);
      sscanf(cp+21, "%lld", Buffer+i);
    }
    Buffer[i]=Buffer[i]<<2;
  }
  fclose(fp);

  PS=fopen("plotsymbols.ps","w");

  fprintf(PS, "%%!PS-Adobe-3.0\n");
  fprintf(PS, "%.4f setlinewidth\n",0.04/2.54*72.0);
  fprintf(PS, "0 0 0 setrgbcolor\n");


  for(i=0;i<len;i++)
  {
    c=Buffer[i+start];
    printf("%4d/%4d/%4d/%4d",
	      (int) ((c&ONE0_9)>>SHIFT9),
	      (int) ((c&ONE10_19)>>SHIFT19),
	      (int) ((c&ONE20_29)>>SHIFT29),
	      (int) ((c&ONE30_39)>>SHIFT39));
    if(i%2==0)
    {
      printf("   ;  %d",i/2);
    }
    printf("\n");
  }
  for(i=0;i<120;i++)
  {
    printf("\ni: %d\n",i);
    X=5.0+(i%8)*1.5;
    Y=27.0-i/8*1.5;
    test(start+i*2,X,Y,0.6);
  }
  fprintf(PS, "showpage\n");
  fprintf(PS, "%c", 4);
  fclose(PS);
}
