xref: /csrg-svn/old/vpr/vtools/rotate.c (revision 12085)
1*12085Sralph /*	rotate.c	4.1	83/04/28	*/
2*12085Sralph /*
3*12085Sralph  * Rotate a Varian/Versatec font.
4*12085Sralph  */
5*12085Sralph 
6*12085Sralph #include <stdio.h>
7*12085Sralph #include <vfont.h>
8*12085Sralph #include <sys/types.h>
9*12085Sralph #include <sys/stat.h>
10*12085Sralph 
11*12085Sralph char	*chp;
12*12085Sralph char	*sbrk();
13*12085Sralph 
14*12085Sralph main(argc,argv)
15*12085Sralph char **argv;
16*12085Sralph {
17*12085Sralph 	struct header h;
18*12085Sralph 	struct dispatch d[256], nd;
19*12085Sralph 	struct stat stb;
20*12085Sralph 	off_t tell();
21*12085Sralph 	int i,size;
22*12085Sralph 	int beg;
23*12085Sralph 	char scr[2048];
24*12085Sralph 
25*12085Sralph 	argc--, argv++;
26*12085Sralph 	if (argc > 0) {
27*12085Sralph 		close(0);
28*12085Sralph 		if (open(argv[0], 0) < 0)
29*12085Sralph 			perror(argv[0]), exit(1);
30*12085Sralph 	}
31*12085Sralph 	if (read(0, &h, sizeof(h)) != sizeof(h)) {
32*12085Sralph 		fprintf(stderr, "header read error\n");
33*12085Sralph 		exit(1);
34*12085Sralph 	}
35*12085Sralph 	if (h.magic != 0436) {
36*12085Sralph 		fprintf(stderr, "bad magic number\n");
37*12085Sralph 		exit(1);
38*12085Sralph 	}
39*12085Sralph 	if (read(0, d, sizeof(d)) != sizeof(d)) {
40*12085Sralph 		fprintf(stderr, "dispatch read error\n");
41*12085Sralph 		exit(1);
42*12085Sralph 	}
43*12085Sralph 	fstat(0, &stb);
44*12085Sralph 	size = stb.st_size - tell(0);
45*12085Sralph 	fprintf(stderr, "%d bytes of characters\n", size);
46*12085Sralph 	chp = sbrk(size + 1024);
47*12085Sralph 	read(0, chp, size);
48*12085Sralph 	write(1, &h, sizeof (h));
49*12085Sralph 	write(1, d, sizeof (d));
50*12085Sralph 	beg = tell(1);
51*12085Sralph 	for (i = 0; i < 256; i++)
52*12085Sralph 		if (d[i].nbytes) {
53*12085Sralph 			if (d[i].addr + d[i].nbytes > size) {
54*12085Sralph 				fprintf(stderr, "char %d out of range\n", i);
55*12085Sralph 				continue;
56*12085Sralph 			}
57*12085Sralph 			cvt(&d[i], chp+d[i].addr, &nd, scr);
58*12085Sralph 			d[i] = nd;
59*12085Sralph 			d[i].addr = tell(1) - beg;
60*12085Sralph 			write(1, scr, d[i].nbytes);
61*12085Sralph 		}
62*12085Sralph 	fprintf(stderr, "done, new size %d\n", tell(1) - beg);
63*12085Sralph 	h.size = tell(1) - beg;
64*12085Sralph 	lseek(1, 0, 0);
65*12085Sralph 	write(1, &h, sizeof (h));
66*12085Sralph 	write(1, d, sizeof (d));
67*12085Sralph }
68*12085Sralph 
69*12085Sralph cvt(odp, ocp, dp, cp)
70*12085Sralph 	struct dispatch *odp, *dp;
71*12085Sralph 	register char *ocp, *cp;
72*12085Sralph {
73*12085Sralph 	int max;
74*12085Sralph 	int bpl;
75*12085Sralph 	int row,byte,bit;
76*12085Sralph 	register char *ep;
77*12085Sralph 	register int bitoff;
78*12085Sralph 	register int bits;
79*12085Sralph 	int extra;
80*12085Sralph 
81*12085Sralph 	max = (odp->up+odp->down+7)/8;
82*12085Sralph 	extra = max*8 - (odp->down+odp->up);
83*12085Sralph 	dp->down = odp->down;
84*12085Sralph 	dp->up = odp->up;
85*12085Sralph 	dp->left = odp->left;
86*12085Sralph 	dp->right = odp->right;
87*12085Sralph 	dp->nbytes = max*(dp->right+dp->left);
88*12085Sralph 	ep = cp;
89*12085Sralph 	for (byte = 0; byte < dp->nbytes; byte++)
90*12085Sralph 		*ep++ = 0;
91*12085Sralph 	bpl = (dp->right+dp->left+7)/8;
92*12085Sralph 	for (row = 0; row < odp->up+odp->down; row++) {
93*12085Sralph 		for (byte = 0; byte < bpl; byte++) {
94*12085Sralph 			bits = *ocp++;
95*12085Sralph 			for (bit = 0; bit < 8; bit++) {
96*12085Sralph 				if (bits & 0x80) {
97*12085Sralph 					ep = cp + max*(byte*8+bit);
98*12085Sralph 					bitoff = max*8 - row - 1 - extra;
99*12085Sralph 					ep += (bitoff/8);
100*12085Sralph 					*ep |= 0x80 >> (bitoff%8);
101*12085Sralph 				}
102*12085Sralph 				bits <<= 1;
103*12085Sralph 			}
104*12085Sralph 		}
105*12085Sralph 	}
106*12085Sralph }
107