xref: /csrg-svn/old/fcvt/fcvt.c (revision 11765)
1*11765Smckusick #ifndef lint
2*11765Smckusick static char sccsid[] = "@(#)fcvt.c	4.1	(Berkeley)	03/29/83";
3*11765Smckusick #endif not lint
4*11765Smckusick 
5*11765Smckusick /*
6*11765Smckusick  * Convert from the SAIL font format to the Unix font format.
7*11765Smckusick  * Usage: fcvt sailfile unixfile
8*11765Smckusick  */
9*11765Smckusick long left(), right();
10*11765Smckusick int sws;	/* sail word size in 36 bit words */
11*11765Smckusick char b[40000], u[2000];
12*11765Smckusick #include <stdio.h>
13*11765Smckusick #include <vfont.h>
14*11765Smckusick 
15*11765Smckusick struct header vheader;
16*11765Smckusick struct dispatch disptable[256];
17*11765Smckusick 
18*11765Smckusick long rightbits[19] = {
19*11765Smckusick 	0,	1,	03,	07,	017,	037,
20*11765Smckusick 	077,	0177,	0377,	0777,	01777,	03777,
21*11765Smckusick 	07777,	017777,	037777,	077777,	0177777,0377777,0777777
22*11765Smckusick };
23*11765Smckusick 
main(argc,argv)24*11765Smckusick main(argc, argv)
25*11765Smckusick char **argv;
26*11765Smckusick {
27*11765Smckusick 	int infd = open(argv[1], 0);
28*11765Smckusick 	int outfd = creat(argv[2], 0666);
29*11765Smckusick 	int n;
30*11765Smckusick 	long lh, rh;
31*11765Smckusick 	int base, nb, ncol, nleft, r, i;
32*11765Smckusick 	int c, p;
33*11765Smckusick 	/* Sail counters and things */
34*11765Smckusick 	int height, maxwidth, baseline;
35*11765Smckusick 	int charwidth, rastwidth, charcode, wordcount;
36*11765Smckusick 	int leftkern, rowsfromtop, datarowcount;
37*11765Smckusick 	/* Unix counters and things */
38*11765Smckusick 	int rastrows, rastcols;
39*11765Smckusick 	int curaddr;
40*11765Smckusick 	int packed;	/* true if sail packed format for this glyph */
41*11765Smckusick 	int nperword;
42*11765Smckusick 
43*11765Smckusick 	if (infd < 0 || outfd < 0) {
44*11765Smckusick 		printf("Usage: fcvt sailfile unixfile\n");
45*11765Smckusick 		exit(1);
46*11765Smckusick 	}
47*11765Smckusick 	n = read(infd, b, sizeof b);
48*11765Smckusick 	sws = 2 * n / 9;
49*11765Smckusick 	if (n == sizeof b) {
50*11765Smckusick 		printf("Font larger than %d bytes - recompile me\n", n);
51*11765Smckusick 		exit(1);
52*11765Smckusick 	}
53*11765Smckusick 	close(infd);
54*11765Smckusick 
55*11765Smckusick 	height = right(0201);
56*11765Smckusick 	maxwidth = right(0202);
57*11765Smckusick 	baseline = right(0203);
58*11765Smckusick 
59*11765Smckusick 	vheader.magic = 0436;
60*11765Smckusick 	/* size gets done later */
61*11765Smckusick 	vheader.maxx = height;
62*11765Smckusick 	vheader.maxy = maxwidth;
63*11765Smckusick 	/* I don't know what xtnd would map to */
64*11765Smckusick 
65*11765Smckusick 	lseek(outfd, (long) sizeof vheader + sizeof disptable, 0);
66*11765Smckusick 	curaddr = 0;
67*11765Smckusick 
68*11765Smckusick 	/* Look at each char */
69*11765Smckusick 	for (c=0; c<0200; c++) {
70*11765Smckusick 		/* Find Sail info */
71*11765Smckusick 		base = right(c);
72*11765Smckusick 		if (base == 0)
73*11765Smckusick 			continue;
74*11765Smckusick 		charwidth = left(c);
75*11765Smckusick 		rastwidth = (left(base) >> 9) & 0777;
76*11765Smckusick 		if (rastwidth == 0)
77*11765Smckusick 			rastwidth = charwidth;
78*11765Smckusick 		charcode = left(base) & 0777;
79*11765Smckusick 		if (charcode != c)
80*11765Smckusick 			printf("bad char code %o(%c) != %o(%c)\n", charcode, charcode, c, c);
81*11765Smckusick 		wordcount = right(base);
82*11765Smckusick 		if (base+wordcount > sws) {
83*11765Smckusick 			printf("Bad range %o-%o > %o glyph %o\n", base, base+wordcount, sws, c);
84*11765Smckusick 			continue;
85*11765Smckusick 		}
86*11765Smckusick 		leftkern = (left(base+1) >> 9) & 0777;
87*11765Smckusick 		rowsfromtop = left(base+1) & 0777;
88*11765Smckusick 		datarowcount = right(base+1);
89*11765Smckusick 
90*11765Smckusick 		rastrows = datarowcount;
91*11765Smckusick 		rastcols = (rastwidth + 35) / 36 * 36;
92*11765Smckusick 
93*11765Smckusick 		/* Unix disptable stuff */
94*11765Smckusick 		disptable[c].addr = curaddr;
95*11765Smckusick 		nb = rastrows * ((rastcols + 7) >> 3);
96*11765Smckusick 		disptable[c].nbytes = nb;
97*11765Smckusick 		curaddr += nb;
98*11765Smckusick 		disptable[c].left = leftkern;
99*11765Smckusick 		disptable[c].right = rastcols - leftkern;
100*11765Smckusick 		disptable[c].up = baseline - rowsfromtop;
101*11765Smckusick 		disptable[c].down = rastrows - disptable[c].up;
102*11765Smckusick 		disptable[c].width = charwidth;
103*11765Smckusick 		packed = (datarowcount > wordcount);
104*11765Smckusick 		nperword = 36 / rastwidth;
105*11765Smckusick 
106*11765Smckusick 		/* Now get the raster rows themselves */
107*11765Smckusick 		p = 0;
108*11765Smckusick 		ncol = rastcols / 36;
109*11765Smckusick 		nleft = ((rastwidth-1) % 36 + 1);
110*11765Smckusick 		base += 2;
111*11765Smckusick 		for (r=0; r<rastrows; r++) {
112*11765Smckusick 			if (!packed) {
113*11765Smckusick 				for (i=0; i<ncol; i++) {
114*11765Smckusick 					lh = left(base); rh = right(base++);
115*11765Smckusick 					/* compensate for garbage in SAIL fonts */
116*11765Smckusick 					if (i == ncol-1) {
117*11765Smckusick 						if (nleft <= 18) {
118*11765Smckusick 							rh = 0;
119*11765Smckusick 							lh &= ~rightbits[18-nleft];
120*11765Smckusick 						} else
121*11765Smckusick 							rh &= ~rightbits[36-nleft];
122*11765Smckusick 					}
123*11765Smckusick 					if (i%2) {
124*11765Smckusick 						u[p-1] |= (lh>>14) & 017;
125*11765Smckusick 						u[p++] = lh >> 6;
126*11765Smckusick 						u[p++] = ((lh&077)<<2) | ((rh>>16)&03);
127*11765Smckusick 						u[p++] = rh >> 8;
128*11765Smckusick 						u[p++] = rh;
129*11765Smckusick 					} else {
130*11765Smckusick 						u[p++] = lh >> 10;
131*11765Smckusick 						u[p++] = lh >> 2;
132*11765Smckusick 						u[p++] = ((lh&03)<<6) | (rh>>12);
133*11765Smckusick 						u[p++] = rh >> 4;
134*11765Smckusick 						u[p++] = (rh & 017) << 4;
135*11765Smckusick 					}
136*11765Smckusick 				}
137*11765Smckusick 			} else {
138*11765Smckusick 				put(r % nperword, rastwidth, left(base+r/nperword), right(base+r/nperword), u+p);
139*11765Smckusick 				p += 5;	/* 5 8 bit bytes per 36 bit word */
140*11765Smckusick 			}
141*11765Smckusick 		}
142*11765Smckusick 		write(outfd, u, p);
143*11765Smckusick 	}
144*11765Smckusick 	lseek(outfd, 0, 0);
145*11765Smckusick 	vheader.size = curaddr;
146*11765Smckusick 	write(outfd, &vheader, sizeof vheader);
147*11765Smckusick 	write(outfd, disptable, sizeof disptable);
148*11765Smckusick 	close(outfd);
149*11765Smckusick 	exit(0);
150*11765Smckusick }
151*11765Smckusick 
152*11765Smckusick /*
153*11765Smckusick  * put a pdp-10 style variable size byte into 8 bit Unix bytes starting
154*11765Smckusick  * at location dest.  The byte is bytesize bits, and is the bytenumth byte
155*11765Smckusick  * in the 36 bit word (lh,,rh).
156*11765Smckusick  */
put(bytenum,bytesize,lh,rh,dest)157*11765Smckusick put(bytenum, bytesize, lh, rh, dest)
158*11765Smckusick int bytenum, bytesize;
159*11765Smckusick long lh, rh;
160*11765Smckusick char *dest;
161*11765Smckusick {
162*11765Smckusick 	register int i;
163*11765Smckusick 
164*11765Smckusick 	for (i=0; i<5; i++)
165*11765Smckusick 		dest[i] = 0;
166*11765Smckusick 	for (i=0; i<bytenum; i++) {
167*11765Smckusick 		lh <<= bytesize;
168*11765Smckusick 		lh |= (rh >> 18-bytesize) & rightbits[bytesize];
169*11765Smckusick 		rh <<= bytesize;
170*11765Smckusick 	}
171*11765Smckusick 	lh &= ~rightbits[18-bytesize];
172*11765Smckusick 	/* We now have the byte we want left justified in lh */
173*11765Smckusick 	lh <<= 14;
174*11765Smckusick 	/* lh is now the byte we want, left justified in 32 bit word */
175*11765Smckusick 	for (i=0; i<bytesize; i += 8) {
176*11765Smckusick 		*dest++ = (lh >> 24) & 0377;
177*11765Smckusick 		lh <<= 8;
178*11765Smckusick 	}
179*11765Smckusick }
180*11765Smckusick 
181*11765Smckusick /*
182*11765Smckusick  * Return the left half (18 bits) of pdp-10 word p.
183*11765Smckusick  */
184*11765Smckusick long
left(p)185*11765Smckusick left(p)
186*11765Smckusick int p;
187*11765Smckusick {
188*11765Smckusick 	register int lp, odd;
189*11765Smckusick 	register long retval;
190*11765Smckusick 
191*11765Smckusick 	odd = p%2;
192*11765Smckusick 	lp = 9*p/2;
193*11765Smckusick 	if (p >= sws) {
194*11765Smckusick 		return(0);
195*11765Smckusick 	}
196*11765Smckusick 	if (odd) {
197*11765Smckusick 		retval  = (b[lp++] & 0017) << 14;
198*11765Smckusick 		retval |= (b[lp++] & 0377) << 6;
199*11765Smckusick 		retval |= (b[lp] >> 2) & 63;
200*11765Smckusick 	} else {
201*11765Smckusick 		retval  = (b[lp++] & 0377) << 10;
202*11765Smckusick 		retval |= (b[lp++] & 0377) << 2;
203*11765Smckusick 		retval |= (b[lp] >> 6) & 3;
204*11765Smckusick 	}
205*11765Smckusick 	return retval;
206*11765Smckusick }
207*11765Smckusick 
208*11765Smckusick /*
209*11765Smckusick  * Return the right half of 36 bit word #p.
210*11765Smckusick  */
211*11765Smckusick long
right(p)212*11765Smckusick right(p)
213*11765Smckusick int p;
214*11765Smckusick {
215*11765Smckusick 	register int lp, odd;
216*11765Smckusick 	register long retval;
217*11765Smckusick 
218*11765Smckusick 	odd = p%2;
219*11765Smckusick 	lp = 9*p/2 + 2;
220*11765Smckusick 	if (p >= sws) {
221*11765Smckusick 		return(0);
222*11765Smckusick 	}
223*11765Smckusick 	if (odd) {
224*11765Smckusick 		retval  = (b[lp++] & 0003) << 16;
225*11765Smckusick 		retval |= (b[lp++] & 0377) << 8;
226*11765Smckusick 		retval |= (b[lp]   & 0377);
227*11765Smckusick 	} else {
228*11765Smckusick 		retval  = (b[lp++] & 0077) << 12;
229*11765Smckusick 		retval |= (b[lp++] & 0377) << 4;
230*11765Smckusick 		retval |= (b[lp] >> 4) & 017;
231*11765Smckusick 	}
232*11765Smckusick 	return retval;
233*11765Smckusick }
234