aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/tcs/font/gbits.c
blob: 6173907dcffc0ccea83651593e6a4a43c6536b2f (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
#include	<u.h>
#include	<libc.h>
#include	<libg.h>
#include	<bio.h>
#include	"hdr.h"

/*
	this file reads bdf fonts. it is mapping dependent in that the ENCODING
	field is font dependent.
*/

static char *field(Biobuf *, char *);

Bitmap *
greadbits(char *file, int n, long *chars, int size, uchar *bits, int **doneptr)
{
	Bitmap *bm;
	Biobuf *bf;
	char *p, *s;
	long kmin, kmax;
	int i, j, byt;
	int nch;
	long c;
	uchar *b;
	int *done;
	uchar *nbits;
	char buf[1024];	/* big enough for one char! */
	static int dig[256] = {
	['0'] 0, ['1'] 1, ['2'] 2, ['3'] 3, ['4'] 4, ['5'] 5, ['6'] 6, ['7'] 7,
	['8'] 8, ['9'] 9, ['a'] 10, ['b'] 11, ['c'] 12, ['d'] 13, ['e'] 14, ['f'] 15,
	};

	bf = Bopen(file, OREAD);
	if(bf == 0){
		fprint(2, "%s: %s: %r\n", argv0, file);
		exits("bitfile open error");
	}
	done = (int *)malloc(n*sizeof(done[0]));
	if(done == 0){
		fprint(2, "%s: malloc error (%d bytes)\n", argv0, n);
		exits("malloc error");
	}
	*doneptr = done;
	memset(done, 0, n*sizeof(done[0]));
	kmin = 65536;
	kmax = 0;
	for(i = 0; i < n; i++){
		c = chars[i];
		if(kmin > c)
			kmin = c;
		if(kmax < c)
			kmax = c;
	}
	nch = 0;
	byt = size/8;
	for(;;){
		while(s = Brdline(bf, '\n')){
			if(strncmp(s, "STARTCHAR", 9) == 0)
				break;
		}
		if(s == 0)
			break;
		s = field(bf, "ENCODING");
		c = strtol(s, (char **)0, 10);
		c = (c/256 - 0xA0)*100 + (c%256 - 0xA0);
		field(bf, "BITMAP");
		p = buf;
		for(i = 0; i < size; i++){	/* rows */
			if((s = Brdline(bf, '\n')) == 0){
				exits("bad bdf");
			}
			memcpy(p, s, 2*byt);
			p += 2*byt;
		}
		field(bf, "ENDCHAR");
		if((c < kmin) || (c > kmax))
			continue;
		for(i = 0; i < n; i++)
			if(c == chars[i]){
				nch++;
				done[i] = 1;
				p = buf;
				b = bits + i*byt;
				for(i = 0; i < size; i++){	/* rows */
					for(j = 0; j < byt; j++){
						*b++ = (dig[*p]<<4) | dig[p[1]];
						p += 2;
					}
					b += (n-1)*byt;
				}
				break;
			}
	}
	nbits = (uchar *)malloc(nch*byt*size);
	if(nbits == 0){
		fprint(2, "%s: malloc error (%d bytes)\n", argv0, nch*byt);
		exits("malloc error");
	}
	c = 0;
	for(i = 0; i < n; i++)
		if(done[i]){
			for(j = 0; j < size; j++)
				memmove(nbits+c*byt+j*nch*byt, bits+i*byt+j*n*byt, byt);
			c++;
		}
	bm = balloc((Rectangle){(Point){0, 0}, (Point){nch*size, size}}, 0);
	if(bm == 0){
		fprint(2, "%s: balloc failure\n", argv0);
		exits("balloc failure");
	}
	wrbitmap(bm, 0, size, nbits);
	for(i = 0; i < n; i++)
		if(done[i] == 0){
			/*fprint(2, "char 0x%x (%d) not found\n", chars[i], chars[i]);/**/
		}
	return(bm);
}

static char *
field(Biobuf *bf, char *name)
{
	char *s;
	int n;

	n = strlen(name);
	while(s = Brdline(bf, '\n')){
		if(strncmp(s, name, n) == 0)
			return(s+n);
		if(strncmp(s, "ENDCHAR", 7) == 0){
			fprint(2, "%s: char missing %s field\n", argv0, name);
			exits("bad bdf");
		}
	}
	fprint(2, "%s: char missing %s field; unexpected EOF\n", argv0, name);
	exits("bad bdf");
	return(0);
}