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

char	Font[20000];		/* contents of font file	*/
int	maxvo, maxho;		/* maximum  offsets		*/
FILE	*fid;

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

	if (argc != 2) {
		fprintf(stderr, "usage: ftconv fontname\n");
		exit(1);
	}
	if ((fid = fopen(argv[1], "r")) == NULL) {
		printf("can't open %s\n", argv[1]);
		exit(1);
	}
	fread(Font, sizeof(Font), 1, fid);
	findmax();
	printf("%d %d\n", 0, FONTSIZE - 1);
	for (i = 0; i < FONTSIZE; i++) fontinfo(i);
	return(0);
}

int findmax()
{
	register struct CharDef *cd;
	register int i, vo, ho;

	maxvo = maxho = 0;
	for (i = 0; i < FONTSIZE; i++) {
		cd = (struct CharDef *) &Font[i * sizeof(struct CharDef)];
		vo = cd->VSize + cd->VAdj;
		if (vo > maxvo) maxvo = vo;
		ho = -cd->HAdj;
		if (ho > maxho) maxho = ho;
	}
}

fontinfo(ch)
register char ch;
{
	register struct CharDef *nhp;
	short h, ho, v, vo;
	register short rl, i;
	register unsigned int rs;
	register unsigned short *rsptr;

	nhp = (struct CharDef *) &Font[ch * sizeof(struct CharDef)];

	rsptr = (unsigned short *) CharRaster(nhp);
	if ((int) rsptr < 0 || (short) RasterSize(nhp) <= 0) {
		printf("%3d: 0 0\n", ch);
		return;
	}

	ho = maxho + nhp->HAdj;
	h = (nhp->HSize + 15) >> 4;	/* # of shorts in raster line	*/
	if (h != 1)
		fprintf(stderr, "Character %c is too wide (%d pixels)\n",
			ch, nhp->HSize);
	v = nhp->VSize;			/* # of raster lines		*/
	vo = maxvo - (nhp->VSize + nhp->VAdj);	/* # of base raster lines */

	printf("%3d:", ch);
	printf(" %d %d", v + vo, nhp->HIncr);

	for (rl = 0; rl < v; rl++) {	/* iterate over raster lines	*/
		rs = 0;
		for (i = 0; i < h; i++)
			rs = (rs<<16) + (*rsptr++<<ho);
		printf(" %d", rs);
	}
	for (rl = 0; rl < vo; rl++)	/* build up to base line */
		printf(" 0");
	putchar('\n');
}
