114683Sslatteng #ifndef lint
214683Sslatteng static char sccsid[] = "@(#)vfontinfo.c	4.3 (Berkeley) 7/16/83";
314683Sslatteng #endif
414683Sslatteng 
514683Sslatteng /* Font Information for VCat-style fonts
614683Sslatteng  *      Andy Hertzfeld  4/79
714683Sslatteng  *
814683Sslatteng  *	Modified to print Ascii chars 1/80 by Mark Horton
914683Sslatteng  *	Zoom option added 5/81 by Steve Stone with tables from Mark Horton.
1014683Sslatteng  *	Message option added 5/31 by Mark Horton
1114683Sslatteng  */
1214683Sslatteng #include <stdio.h>
1314683Sslatteng #include <ctype.h>
1414683Sslatteng #include <vfont.h>
1514683Sslatteng 
1618121Sslatteng #ifndef BITDIR
1718121Sslatteng #define BITDIR "/usr/lib/vfont"
1818121Sslatteng #endif
1918121Sslatteng 
2014683Sslatteng struct header FontHeader;
2114683Sslatteng struct dispatch disptable[256];
2214683Sslatteng 
2314683Sslatteng char	IName[100];
2414683Sslatteng char *	rdchar();
2514683Sslatteng long	fbase;
2614683Sslatteng 
2714683Sslatteng char	defascii[256];
2814683Sslatteng char	*charswanted = defascii;
2914683Sslatteng int	verbose;
3014683Sslatteng char	charbits[4000];
3114683Sslatteng int	H, W, WB, base;
3214683Sslatteng int 	zoom = 1;
3314683Sslatteng 
3414683Sslatteng char msgout[24][80];
3514683Sslatteng int msgflag = 0;
3614683Sslatteng int curline, curcol;	/* cursor, numbered from lower left corner */
3714683Sslatteng int minline=24, maxline=0, maxcol=0;
3814683Sslatteng 
main(argc,argv)3914683Sslatteng main(argc,argv)
4014683Sslatteng int argc;
4114683Sslatteng char **argv;
4214683Sslatteng 
4314683Sslatteng {
4414683Sslatteng 	int FID,i,j;
4514683Sslatteng 
4614683Sslatteng 	while (argc > 1 && argv[1][0] == '-') {
4714683Sslatteng 		switch(argv[1][1]) {
4816073Sslatteng 		case 'z':
4916073Sslatteng 			zoom = argv[1][2] - '0';  /* zoom implies verbose */
5014683Sslatteng 		case 'v':
5114683Sslatteng 			verbose++;
5214683Sslatteng 			break;
5314683Sslatteng 		case 'm':
5414683Sslatteng 			msgflag = 1;
5514683Sslatteng 			zoom = 2;
5614683Sslatteng 			for (i=0; i<24; i++)
5714683Sslatteng 				for (j=0; j<80; j++)
5814683Sslatteng 					msgout[i][j] = ' ';
5914683Sslatteng 			curline = 5; curcol = 0;
6014683Sslatteng 			break;
6114683Sslatteng 		default:
6214683Sslatteng 			printf("Bad flag: %s\n", argv[1]);
6314683Sslatteng 		}
6414683Sslatteng 		argc--; argv++;
6514683Sslatteng 	}
6614683Sslatteng 	if (argc < 2) {
6714683Sslatteng 		fprintf(stderr,"Usage: %s filename", argv[0]);
6814683Sslatteng 		exit(2);
6914683Sslatteng 	}
7014683Sslatteng 
7114683Sslatteng 	for (i=0; i<128; i++)
7214683Sslatteng 		defascii[i] = i;
7314683Sslatteng 	if (argc >= 3)
7414683Sslatteng 		charswanted = argv[2];
7514683Sslatteng 
7618121Sslatteng 	sprintf(IName, "%s/%s", BITDIR, argv[1]);
7714683Sslatteng 	if ((FID = open(argv[1],0)) < 0)
7814683Sslatteng 		if ((FID = open(IName,0)) < 0) {
7914683Sslatteng 			printf("Can't find %s\n",argv[1]);
8014683Sslatteng 			exit(8);
8114683Sslatteng 		};
8214683Sslatteng 
8314683Sslatteng 	if (read(FID,&FontHeader,sizeof FontHeader) != sizeof FontHeader)
8414683Sslatteng 		error("Bad header in Font file.");
8514683Sslatteng 
8614683Sslatteng 	if (read(FID,&disptable[0],sizeof disptable) != sizeof disptable)
8714683Sslatteng 		error("Bad dispatch table in Font file");
8814683Sslatteng 
8914683Sslatteng 	fbase = sizeof FontHeader + sizeof disptable;
9014683Sslatteng 
9114683Sslatteng 	if (FontHeader.magic != 0436)
9214683Sslatteng 		printf("Magic number %o wrong\n", FontHeader.magic);
9314683Sslatteng 	if (!msgflag) {
9414683Sslatteng 		printf("Font %s, ",argv[1]);
9514683Sslatteng 		printf("raster size %d, ",FontHeader.size);
9614683Sslatteng 		printf("max width %d, max height %d, xtend %d\n",
9714683Sslatteng 			FontHeader.maxx, FontHeader.maxy,FontHeader.xtend);
9814683Sslatteng 		printf("\n");
9914683Sslatteng 		for (i = strlen(argv[1]) + 1; i > 0; --i)
10014683Sslatteng 			printf(" ");
10114683Sslatteng 		printf("ASCII     offset    size  left    right   up     down    width \n");
10214683Sslatteng 	}
10314683Sslatteng 
10414683Sslatteng 	for (i=0; i<256; i++) {
10514683Sslatteng 		j = charswanted[i];
10614683Sslatteng 		if (i>0 && j==0)
10714683Sslatteng 			break;
10814683Sslatteng 		if (disptable[j].nbytes != 0) {
10914683Sslatteng 			if (!msgflag)
11014683Sslatteng 				printf("%s  %3o %2s     %4d   %4d   %4d   %4d   %4d   %4d   %5d\n",
11114683Sslatteng 					argv[1],
11214683Sslatteng 					j, rdchar(j),
11314683Sslatteng 					disptable[j].addr,
11414683Sslatteng 					disptable[j].nbytes,
11514683Sslatteng 					disptable[j].left,
11614683Sslatteng 					disptable[j].right,
11714683Sslatteng 					disptable[j].up,
11814683Sslatteng 					disptable[j].down,
11914683Sslatteng 					disptable[j].width);
12014683Sslatteng 			if (verbose || msgflag) {
12114683Sslatteng 				int len = disptable[j].nbytes;
12214683Sslatteng 				int k, l, last;
12314683Sslatteng 
12414683Sslatteng 				lseek(FID, fbase+disptable[j].addr, 0);
12514683Sslatteng 				read(FID, charbits, len);
12614683Sslatteng 				H = (disptable[j].up) + (disptable[j].down);
12714683Sslatteng 				W = (disptable[j].left) + (disptable[j].right);
12814683Sslatteng 				base = disptable[j].up;
129*23844Sslatteng #ifdef sun
130*23844Sslatteng 				WB = ((W+15)/16)*2;
131*23844Sslatteng #else
13214683Sslatteng 				WB = (W+7)/8;
133*23844Sslatteng #endif
13416073Sslatteng 				shozoom();
13516073Sslatteng 				if (msgflag) {
13616073Sslatteng 					k = disptable[j].width;
13716073Sslatteng 					if (zoom == 0) k *= 2;
13816073Sslatteng 					else if (zoom == 2) k /= 2;
13916073Sslatteng 					curcol += k;
14014683Sslatteng 				}
14114683Sslatteng 			}
14214683Sslatteng 		}
14314683Sslatteng 	}
14414683Sslatteng 	if (msgflag) {
14514683Sslatteng 		for (i=maxline; i>=minline; i--) {
14614683Sslatteng 			for (j=0; j<maxcol; j++)
14714683Sslatteng 				putchar(msgout[i][j]);
14814683Sslatteng 			putchar('\n');
14914683Sslatteng 		}
15014683Sslatteng 	}
15117908Sslatteng 	exit(0);
15214683Sslatteng }
15314683Sslatteng 
error(string)15414683Sslatteng error(string)
15514683Sslatteng char *string;
15614683Sslatteng 
15714683Sslatteng {
15814683Sslatteng 	printf("\nvfontinfo: %s\n",string);
15914683Sslatteng 	exit(8);
16014683Sslatteng };
16114683Sslatteng 
rdchar(c)16214683Sslatteng char *rdchar(c)
16314683Sslatteng char c;
16414683Sslatteng {
16514683Sslatteng 	static char ret[3];
16614683Sslatteng 	ret[0] = isprint(c) ? ' ' : '^';
16714683Sslatteng 	ret[1] = isprint(c) ?  c  : c^0100;
16814683Sslatteng 	ret[2] = 0;
16914683Sslatteng 	return (ret);
17014683Sslatteng }
17114683Sslatteng 
17214683Sslatteng int
fbit(row,col)17314683Sslatteng fbit(row, col)
17414683Sslatteng int row, col;
17514683Sslatteng {
17614683Sslatteng 	int thisbyte, thisbit, ret;
17714683Sslatteng 
17814683Sslatteng 	if (row<0 || row>=H || col>=W) return(0);
17914683Sslatteng 	thisbyte = charbits[row*WB + (col>>3)] & 0xff;
18014683Sslatteng 	thisbit = 0x80 >> (col&7);
18114683Sslatteng 	ret = thisbyte & thisbit;
18214683Sslatteng 	return (ret != 0);
18314683Sslatteng }
18414683Sslatteng 
18514683Sslatteng 
18614683Sslatteng /*
18714683Sslatteng The implementation would work like this:
18814683Sslatteng 	zoom level	method
18914683Sslatteng 	0		2 chars/pixel, 1 is "[]", 0 is "  ".
19014683Sslatteng 	1		2 pixels/char 2x1, using " " "," "'" "|"
19114683Sslatteng 	2		8 pixels/char 4x2, using 16x16 table
19214683Sslatteng 	3		32 pixels/char 8x4, mapped into (2)
19314683Sslatteng 	4 and up	similar, mapped into (2)
19414683Sslatteng 
19514683Sslatteng The 16x16 table maps a 4x2 pattern into a printing ascii character which
19614683Sslatteng most closely approximates that pattern, e.g. the pattern
19714683Sslatteng 	|'
19814683Sslatteng 	''
19914683Sslatteng would be represented by the character "[".  I have such a table worked out.
20014683Sslatteng 
20114683Sslatteng Grainer zoom levels would take the rule of reducing it into a smaller bitmap,
20214683Sslatteng or-ing the bits together.  (e.g. level 3 would take a 2x2 chunk and map it
20314683Sslatteng into a single pixel: 0 if all 4 are 0, 1 otherwise.)  These pixels would be
20414683Sslatteng displayed as in 2.
20514683Sslatteng */
20614683Sslatteng 
20714683Sslatteng /*
20814683Sslatteng  * graphtab: a table for rudimentary graphics on ordinary terminals.
20914683Sslatteng  * For each 4x2 bit pattern of the form:
21014683Sslatteng  *	ae
21114683Sslatteng  *	bf
21214683Sslatteng  *	cg
21314683Sslatteng  *	dh
21414683Sslatteng  * form the 4 bit quantities abcd and efgh and get table entry
21514683Sslatteng  *	graphtab[abcd][efgh]
21614683Sslatteng  * to display in that character position.
21714683Sslatteng  *
21814683Sslatteng  * General philosophies: the dh bits are intended for descenders where
21914683Sslatteng  * possible.  Characters with radically different appearance on different
22014683Sslatteng  * terminals (e.g. _ and ^) are avoided.
22114683Sslatteng  *
22214683Sslatteng  * Version 1.0, March 1981, Mark Horton.
22314683Sslatteng  */
22414683Sslatteng 
22514683Sslatteng char tab1[4] = {
22614683Sslatteng 	' ', ',', '\'', '|'
22714683Sslatteng };
22814683Sslatteng 
22914683Sslatteng char graphtab[16][16] = {
23014683Sslatteng ' ', '.', '.', ',', '.', ';', ':', 'j', '\'', ':', ':', ';', '\'', ';', '!', '|',
23114683Sslatteng '.', '.', ':', ',', ';', ';', ';', 'j', '/', ';', ';', ';', 'j', 'j', 'j', 'j',
23214683Sslatteng '.', ',', '~', ',', 'r', '<', 'j', 'q', '/', ';', 'I', ';', '/', '|', 'I', '|',
23314683Sslatteng ',', ',', 'r', 'x', '/', '/', '/', 'd', '/', '/', '/', 'd', '/', '/', '/', 'd',
23414683Sslatteng '.', ':', '\\', ';', '-', '=', 'v', 'q', '\'', ':', '<', '|', '\'', ':', '+', '+',
23514683Sslatteng ';', ';', '>', ';', '=', '=', 'g', 'g', '\'', ':', 'S', 'S', '/', '/', '/', '+',
23614683Sslatteng ':', '\\', '\\', '\\', 'r', '<', 'w', 'q', '/', '<', '6', '4', '/', '/', 'd', '+',
23714683Sslatteng 'l', 'L', '+', 'b', 'y', '[', 'p', 'g', '/', '<', '/', '6', '/', '/', '/', '+',
23814683Sslatteng '`', ':', ':', ';', '`', '\\', '\\', '\\', '"', ':', ':', ';', '`', '\\', 'Y', 'T',
23914683Sslatteng ';', ';', ';', ';', '`', '2', '>', '\\', ':', '=', ';', ';', '?', '?', ']', ']',
24014683Sslatteng ':', ';', ';', ';', '>', '2', '>', '\\', 'F', ';', 'O', ';', '7', '?', ']', '7',
24114683Sslatteng ';', ';', ';', ';', '?', '2', '>', 'b', ';', ';', ';', ';', '?', '?', ']', '#',
24214683Sslatteng '\'', '\\', '\\', '\\', '`', '\\', '\\', '\\', '\'', '\'', '<', '5', '"', '"', 'v', 'q',
24314683Sslatteng ';', '\\', '\\', '\\', '`', '=', '\\', '\\', '\'', '\'', '5', '5', '"', '?', 'g', 'g',
24414683Sslatteng 'I', 'L', 'L', 'L', 'D', '\\', 'b', 'f', 'F', '[', '[', '[', 'P', '?', '#', 'M',
24514683Sslatteng '|', '|', '|', '|', '|', '#', '+', '#', 'T', '[', 'F', 'F', 'P', '?', 'P', 'M'
24614683Sslatteng };
24714683Sslatteng 
24814683Sslatteng 
shozoom()24914683Sslatteng shozoom()
25014683Sslatteng {
25114683Sslatteng 	register i;
25214683Sslatteng 
25314683Sslatteng 	if (zoom == 0)
25414683Sslatteng 		sho0();
25514683Sslatteng 	else if (zoom == 1)
25614683Sslatteng 		sho1();
25714683Sslatteng 	else if (zoom == 2)
25814683Sslatteng 		sho2();
25914683Sslatteng }
26014683Sslatteng 
sho0()26114683Sslatteng sho0()
26214683Sslatteng {
26314683Sslatteng 	register k,l;
26414683Sslatteng 
26514683Sslatteng 	for (k=0; k<H; k++) {
26614683Sslatteng 		for (l=0; l<W; l++)
26714683Sslatteng 			printf("%s", fbit(k,l)?"[]": "  ");
26814683Sslatteng 		printf("\n");
26914683Sslatteng 	}
27014683Sslatteng 	printf("\n");
27114683Sslatteng }
27214683Sslatteng 
sho1()27314683Sslatteng sho1()
27414683Sslatteng {
27514683Sslatteng 	register k,l;
27614683Sslatteng 
27714683Sslatteng 	k = 0;
27814683Sslatteng 	for (k = 0; k <= H; k += 2) {
27914683Sslatteng 		for(l=0;l<W;l++) putchar(tab1[(fbit(k,l) << 1) | fbit(k+1,l)]);
28014683Sslatteng 		putchar('\n');
28114683Sslatteng 	}
28214683Sslatteng 	putchar('\n');
28314683Sslatteng }
28414683Sslatteng 
sho2()28514683Sslatteng sho2()
28614683Sslatteng {
28714683Sslatteng 	register i,j,k,l;
28814683Sslatteng 	int line = curline + (base+3)/4;
28914683Sslatteng 	int col;
29014683Sslatteng 
29114683Sslatteng 	k = base%4;
29214683Sslatteng 	if (k > 0) k -= 4;
29314683Sslatteng 	while (k < H) {
29414683Sslatteng 		l = 0;
29514683Sslatteng 		col = curcol;
29614683Sslatteng 		while (l<W) {
29714683Sslatteng 			i = fbit(k,l)*8 + fbit(k+1,l)*4 +
29814683Sslatteng 			    fbit(k+2,l)*2 + fbit(k+3,l);
29914683Sslatteng 			l++;
30014683Sslatteng 			j = fbit(k,l)*8 + fbit(k+1,l)*4 +
30114683Sslatteng 			    fbit(k+2,l)*2 + fbit(k+3,l);
30214683Sslatteng 
30314683Sslatteng 			if (msgflag) {
30414683Sslatteng 				if (graphtab[i][j] != ' ') {
30514683Sslatteng 					if (line > maxline) maxline = line;
30614683Sslatteng 					if (line < minline) minline = line;
30714683Sslatteng 					if (col > maxcol)   maxcol  = col;
30814683Sslatteng 				}
30914683Sslatteng 				msgout[line][col] = graphtab[i][j];
31014683Sslatteng 			} else
31114683Sslatteng 				printf("%c",graphtab[i][j]);
31214683Sslatteng 			l++;
31314683Sslatteng 			col++;
31414683Sslatteng 		}
31514683Sslatteng 		if (msgflag == 0)
31614683Sslatteng 			printf("\n");
31714683Sslatteng 		k += 4;
31814683Sslatteng 		line--;
31914683Sslatteng 	}
32014683Sslatteng 	if (msgflag == 0)
32114683Sslatteng 		printf("\n");
32214683Sslatteng }
323