1*20200Sdist /*
2*20200Sdist * Copyright (c) 1983 Regents of the University of California.
3*20200Sdist * All rights reserved. The Berkeley software License Agreement
4*20200Sdist * specifies the terms and conditions for redistribution.
5*20200Sdist */
6*20200Sdist
713945Ssam #ifndef lint
8*20200Sdist static char sccsid[] = "@(#)rotate.c 5.1 (Berkeley) 05/15/85";
9*20200Sdist #endif not lint
1013945Ssam
1112085Sralph /*
1212085Sralph * Rotate a Varian/Versatec font.
1312085Sralph */
1412085Sralph
1512085Sralph #include <stdio.h>
1612085Sralph #include <vfont.h>
1712085Sralph #include <sys/types.h>
1812085Sralph #include <sys/stat.h>
1912085Sralph
2012085Sralph char *chp;
2112085Sralph char *sbrk();
2212085Sralph
main(argc,argv)2312085Sralph main(argc,argv)
2412085Sralph char **argv;
2512085Sralph {
2612085Sralph struct header h;
2712085Sralph struct dispatch d[256], nd;
2812085Sralph struct stat stb;
2912085Sralph off_t tell();
3012085Sralph int i,size;
3112085Sralph int beg;
3212085Sralph char scr[2048];
3312085Sralph
3412085Sralph argc--, argv++;
3512085Sralph if (argc > 0) {
3612085Sralph close(0);
3712085Sralph if (open(argv[0], 0) < 0)
3812085Sralph perror(argv[0]), exit(1);
3912085Sralph }
4012085Sralph if (read(0, &h, sizeof(h)) != sizeof(h)) {
4112085Sralph fprintf(stderr, "header read error\n");
4212085Sralph exit(1);
4312085Sralph }
4412085Sralph if (h.magic != 0436) {
4512085Sralph fprintf(stderr, "bad magic number\n");
4612085Sralph exit(1);
4712085Sralph }
4812085Sralph if (read(0, d, sizeof(d)) != sizeof(d)) {
4912085Sralph fprintf(stderr, "dispatch read error\n");
5012085Sralph exit(1);
5112085Sralph }
5212085Sralph fstat(0, &stb);
5312085Sralph size = stb.st_size - tell(0);
5412085Sralph fprintf(stderr, "%d bytes of characters\n", size);
5512085Sralph chp = sbrk(size + 1024);
5612085Sralph read(0, chp, size);
5712085Sralph write(1, &h, sizeof (h));
5812085Sralph write(1, d, sizeof (d));
5912085Sralph beg = tell(1);
6012085Sralph for (i = 0; i < 256; i++)
6112085Sralph if (d[i].nbytes) {
6212085Sralph if (d[i].addr + d[i].nbytes > size) {
6312085Sralph fprintf(stderr, "char %d out of range\n", i);
6412085Sralph continue;
6512085Sralph }
6612085Sralph cvt(&d[i], chp+d[i].addr, &nd, scr);
6712085Sralph d[i] = nd;
6812085Sralph d[i].addr = tell(1) - beg;
6912085Sralph write(1, scr, d[i].nbytes);
7012085Sralph }
7112085Sralph fprintf(stderr, "done, new size %d\n", tell(1) - beg);
7212085Sralph h.size = tell(1) - beg;
7312085Sralph lseek(1, 0, 0);
7412085Sralph write(1, &h, sizeof (h));
7512085Sralph write(1, d, sizeof (d));
7612085Sralph }
7712085Sralph
7812085Sralph cvt(odp, ocp, dp, cp)
7912085Sralph struct dispatch *odp, *dp;
8012085Sralph register char *ocp, *cp;
8112085Sralph {
8212085Sralph int max;
8312085Sralph int bpl;
8412085Sralph int row,byte,bit;
8512085Sralph register char *ep;
8612085Sralph register int bitoff;
8712085Sralph register int bits;
8812085Sralph int extra;
8912085Sralph
9012085Sralph max = (odp->up+odp->down+7)/8;
9112085Sralph extra = max*8 - (odp->down+odp->up);
9212085Sralph dp->down = odp->down;
9312085Sralph dp->up = odp->up;
9412085Sralph dp->left = odp->left;
9512085Sralph dp->right = odp->right;
9612085Sralph dp->nbytes = max*(dp->right+dp->left);
9712085Sralph ep = cp;
9812085Sralph for (byte = 0; byte < dp->nbytes; byte++)
9912085Sralph *ep++ = 0;
10012085Sralph bpl = (dp->right+dp->left+7)/8;
10112085Sralph for (row = 0; row < odp->up+odp->down; row++) {
10212085Sralph for (byte = 0; byte < bpl; byte++) {
10312085Sralph bits = *ocp++;
10412085Sralph for (bit = 0; bit < 8; bit++) {
10512085Sralph if (bits & 0x80) {
10612085Sralph ep = cp + max*(byte*8+bit);
10712085Sralph bitoff = max*8 - row - 1 - extra;
10812085Sralph ep += (bitoff/8);
10912085Sralph *ep |= 0x80 >> (bitoff%8);
11012085Sralph }
11112085Sralph bits <<= 1;
11212085Sralph }
11312085Sralph }
11412085Sralph }
11512085Sralph }
116