#include <stdio.h>
#include <ft.h>

/*****************************************************************************
 	FTINFO -  print out data contained in .ft file.   The -R option causes
 		  the minirasters to be displayed in addition to the character
 		  header data.  The -f option  identifies the .ft  file to  be
 		  examined.
 *****************************************************************************/

#ifdef apm
#define	swapb(s) s
#define	SLEN	23
char	fontdir[] = "";
#endif

/*
#ifndef mc68000
#define	swapb(s)    ((s & 0xFF)<<8) | ((s>>8) & 0xFF)
#define SLEN	    23
char	fontdir[] = "/projects/nunix/Fonts/CRT/";
#endif

#ifdef mc68000
#define	swapb(s) s
#define SLEN	 63
char	fontdir[] = "/Fonts/CRT/";
#endif
*/

struct  CharDef Font[FONTSIZE];	/* character headers for .ft file	*/
char	fname[100];		/* .ft font name			*/
char	rdata[1000];		/* buffer to hold mini-raster data	*/
short	HSize, VSize;		/* scaled Horizonal & Vertical sizes	*/
short	count = -1, RasterS;	/* char count, scaled raster size	*/
char	outrast = 0;		/* 1 for detailed printout		*/
FILE	*fid;

/*****************************************************************************
 	RESTORE - resets the terminal video mode and exits
 *****************************************************************************/
restore()
{
	printf("\033q\n");
	exit();
}

/*****************************************************************************
 	MAIN - main program loop
 *****************************************************************************/
main(argc, argv)
int argc;
char **argv;
{
	register short i; 
	register char *argptr;
	char ch;

	signal(2, restore);
	while (--argc)
	{
		argptr = *++argv;
		if (*argptr == '-') switch (*++argptr)
		{
		case 'R':
			outrast++;
			continue;
		case 'f':
			if (*++argptr != '/')
			{
				strcpy(fname, fontdir);
				strcat(fname, argptr);
			}
			else strcpy(fname, argptr);
			if ((fid = fopen(fname, "r")) == NULL)
			{
				strcat(fname, ".ft");
				if ((fid = fopen(fname, "r")) == NULL)
				{
					printf("can't open %s\n", fname);
					exit();
				}
			}
			fread(Font, sizeof(Font), 1, fid);
			continue;
		default:
			printf("unrecognized option '-%c'\n", *argptr);
			exit();
		}
		else
		{
			if (*argptr != '0') sscanf(argptr, "%c", &ch);
			else sscanf(argptr, "%o", &ch);
			fontinfo(ch);
		}
	}
	if (count < 0) for (i = 0; i < FONTSIZE; i++) fontinfo(i);
}

/*****************************************************************************
 	FONTINFO - prints out data for character 'ch'
 *****************************************************************************/
fontinfo(ch)
register char ch;
{
	register short	*s1;
	register char j;
	register struct CharDef *nhp;

	RasterS = 0;
	nhp = &Font[ch];

	nhp->Raster = swapb(nhp->Raster);
	HSize = nhp->HSize;
	VSize = nhp->VSize;
	if (nhp->Raster > 0) RasterS = VSize * ((HSize + 15) >> 4) * 2;
	if (RasterS == 0) return;

	if (outrast || ((++count % SLEN) == 0)) 
	{
		if (outrast) count = SLEN - 1;
		printf("\n%s\n", fname);
		printf("\033pASCII   CHAR  HSIZE   VSIZE    HADJ");
		printf("    VADJ   HINCR   VINCR"); 
		printf("    MRPTR   SIZE\033q\n");
	}

	printf("0x%x\t'", ch);
	if ((j = ch) < ' ')
	{
		putchar('^');
		j += 64;
	}
	printf("%c'\t%d\t%d\t%d\t%d\t%d\t%d\t", j, nhp->HSize,
		nhp->VSize, nhp->HAdj, nhp->VAdj,
		nhp->HIncr, nhp->VIncr);
	if (nhp->Raster)
		printf("%d\t%d", nhp->Raster, RasterS);
	printf("\n");

	if (outrast) rastinfo(ch);
}

/*****************************************************************************
 	RASTINFO - displays miniraster for character 'ch'
 *****************************************************************************/
rastinfo(ch)
{
	long rrptr, offset;
	short h, v;
	register short *rsptr, rl, i, bit, bitcnt;
	unsigned short rs;

	offset = ((ch + 1) * sizeof(struct CharDef)) - sizeof(short);
	rrptr = Font[ch].Raster;
	h = (HSize + 15) >> 4;		/* # of shorts in raster line	*/
	v = VSize;			/* # of raster lines		*/

	fseek(fid, offset + rrptr, 0);
	fread(rdata, h*v*2, 1, fid);

	putchar('\n');
	rsptr = (short *)rdata;
	for (rl = 0; rl < v; rl++)	/* iterate over raster lines	*/
	{
		bitcnt = 0;
		for (i = 0; i < h; i++)	/* iterate over shorts in line	*/
		{
			rs = swapb(*rsptr); rsptr++;
			for (bit = 0; bit < 16; bit++)	/* over bits	*/
			{
				if (rs & 1) putchar('X'); else putchar('-');
				if (++bitcnt >= HSize) break;
				rs >>= 1;
			}
		}
		putchar('\n');
	}
	printf("\n\n");
}

