summaryrefslogtreecommitdiff
path: root/dviware/dvi2qms/pxltoqms.c
blob: f6f53adf23aba3fa78958e06b0172d60afe8607c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 *	xtoq - TeX82 font to QMS font.
 *
 *	xtoq infile > outfile
 */

#include <ctype.h>
#include <stdio.h>
#include <local/qfont.h>
#define MIN(x,y) ((x)<(y)? (x): (y))
#define MAX(x,y) ((x)>(y)? (x): (y))
#define RND(x)	((int)((x)+0.5))
#define MAGIC	1001
#define	HIGH	0x80000000

char *progname;
char *filename;
short x_height, x_width;
short x_voffset, x_hoffset;
long x_pointer;
long x_spacing;
long dirbuf[4];
int File;
int maxheight;
unsigned char thischar;
struct q_header qh;
struct q_glyph qg;

char *
unctrl(c)
unsigned char c;
{
	static char hold[3];
	if (c == '\177') return "^?";
	if (c<' ') sprintf(hold,"^%c", c^0100);
	else sprintf(hold,"%c",c);
	return hold;
}

main(argc,argv)
int argc;
char **argv;
{
	int k, vert;
	int i, j, raslen, outraslen;
	long *buf, *bp;
	long trailer[5];
	progname = *argv;
	while (--argc) {
		if ('-'==**++argv) switch (*++*argv) {
		default:
			bomb();
		}
		else break;
	}
	if (!argc) bomb();
	filename = *argv;
	if (--argc>0) bomb();
	File = open(filename,0);
	if (0>File) {
		fprintf(stderr,"%s: cannot read %s\n", progname,filename);
		exit(1);
	}
	lseek(File,-5L*sizeof(long),2);
	read(File,trailer,5*sizeof(long));
	if (MAGIC != trailer[4]) {
		fprintf(stderr,"%s: %s not a pxl file\n", progname,filename);
		exit(1);
	}
/*
 *	comb the directory to determine the QMS font height.
 */
	maxheight = 0;
	lseek(File, trailer[3]*sizeof(long), 0);
	for (thischar=0; thischar<128; thischar++) {
		read(File, (char *)dirbuf, 4*sizeof(long));
		x_height = dirbuf[0] & 0xffff;
		x_pointer = dirbuf[2];
		if (x_pointer) maxheight=MAX(maxheight, x_height);
	}
/*
 *	write the QMS header.
 */
	qh.q_number = 0;
	qh.q_orientation = 'P';
	qh.q_version = '1';
	strncpy(qh.q_name,filename,4);
	qh.q_fheight = maxheight;
	qh.q_baseline = 0;
	qh.q_format = Q_FORMAT2;
	qwriteh(stdout, &qh);
/*
 *	now convert the glyphs.
 */
	for (thischar=0; thischar<128; thischar++) {
		lseek(File, (-5+(-128L + thischar)*4)*sizeof(long), 2);
		read(File, (char *)dirbuf, 4*sizeof(long));
		x_height = dirbuf[0] & 0xffff;
		x_width = (dirbuf[0]>>16) & 0xffff;
		x_voffset = dirbuf[1] & 0xffff;
		x_hoffset = (dirbuf[1]>>16) & 0xffff;
		x_pointer = dirbuf[2];
		x_spacing = dirbuf[3];
		if (!x_pointer && !x_spacing) continue;
		raslen = (x_width+31) / 32;
		if (!x_pointer) continue;	/* DON'T CONVERT SPACES */
/*
 *	allocate buffers.
 */
		qg.q_char = thischar;
		qg.q_spacing =RND(300.*x_spacing*trailer[1]/(1000.*trailer[2]));
		qg.q_width = x_width;
/*
 *	set the height to the glyph height.
 *	the output file will have to be filtered through 'newheight'.
 */
		qg.q_height = x_height;
		qg.q_hoffset = -x_hoffset;
		qg.q_voffset = 37 + x_voffset - x_height;
		outraslen = (qg.q_height + 7) / 8;
		qg.q_bitmap = (unsigned char *)malloc(outraslen*qg.q_width);
		for (k=0; k<outraslen*qg.q_width; k++) qg.q_bitmap[k]=0;
		buf = (long *)malloc((unsigned)x_height*raslen*sizeof(long));
		lseek(File, x_pointer*sizeof(long), 0);
		read(File, (char *)buf, x_height * raslen * sizeof(long));
		bp = buf;
		for (i=0; i<x_height; i++) {
			long dots;
			for (j=0; j<x_width; j+=32) {
				dots = *bp++;
				vert = x_height - 1;
				for (k=0; k<32; k++) {
					if (32*j+k>=qg.q_width) break;
					if (HIGH & dots)
					qg.q_bitmap[(vert-i)/8+
						outraslen*(32*j+k)] |=
						1<<(7-(vert-i)%8);
					dots<<=1;
				}
			}
		}
		qwrite(stdout,&qh,&qg);
		free(buf);
		free(qg.q_bitmap);
	}
	qend(stdout);
	exit(0);
}

bomb()
{
	fprintf(stderr,"usage: %s file\n",progname);
	exit(1);
}
-- 
Col. G. L. Sicherman
...{rocksvax|decvax}!sunybcs!colonel