aboutsummaryrefslogtreecommitdiffstats
path: root/util/alevt/bdf2xbm.c
blob: 74d238e6f8eda80dc04e6bc65770b3a9e21b8c2a (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
    Simple program to convert a bdf-font to a bitmap.
    The characters are arranged in a 32x8 matrix.
    usage: bdf2xbm [identifier] <bdf >xbm
    Copyright 1998,1999 by E. Toernig (froese@gmx.de)
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdarg.h>

#define not !
#define streq(a,b) (strcmp((a),(b)) == 0)

int lineno;
char *word[64];
int nword;

char *font = "font%dx%d";
int w, h, bpl;
unsigned char *bmap;


static void error(char *fmt, ...)
{
    va_list args;

    va_start(args, fmt);
    fprintf(stderr, "bdf2xbm");
    if (lineno)
	fprintf(stderr, ":%d", lineno);
    fprintf(stderr, ": ");
    vfprintf(stderr, fmt, args);
    fputc('\n', stderr);
    exit(1);
}


static int nextline()
{
    static char buf[256];
    char *p;
    int i;

    do
    {
	nword = 0;
	if (fgets(buf, sizeof(buf), stdin) == 0)
	    return nword;
	lineno++;
	
	p = buf;
	for (;;)
	{
	    while (isspace(*p))
		p++;
	    if (*p == 0)
		break;
	    word[nword++] = p;
	    while (*p && not isspace(*p))
		*p = toupper(*p), p++;
	    if (*p == 0)
		break;
	    *p++ = 0;
	}
    } while (nword == 0);

    for (i = nword; i < 64; ++i)
	word[i] = "";
    return nword;
}


static inline void setbit(int ch, int x, int y)
{

    int yo = ch / 32 * h + y;
    int xo = ch % 32 * w + x;

    bmap[yo * bpl + xo / 8] |= 1 << (xo % 8);
}


static void dobitmap(int ch, int x, int y)
{
    int i, j;

    for (i = 0; i < y; ++i)
    {
	nextline();
	if (nword > 1 || strlen(word[0]) != (x + 7) / 8 * 2)
	    error("bad BITMAP");
	for (j = 0; j < x; ++j)
	{
	    int c = word[0][j / 4];
	    if (c >= '0' && c <= '9')
		c -= '0';
	    else if (c >= 'A' && c <= 'F')
		c -= 'A' - 10;
	    else
		error("bad hexchar in BITMAP");
	    if (c & (8 >> (j % 4)))
		setbit(ch, j, i);
	}
    }
}


static void dochar()
{
    int ch = -1, x = -1,  y = -1;

    while (nextline())
    {
	if (streq(word[0], "ENDCHAR"))
	    return;
	else if (streq(word[0], "ENCODING") && nword == 2)
	{
	    ch = atoi(word[1]);
	    if (ch < 0 || ch > 255)
		error("bad character code %d", ch);
	}
	else if (streq(word[0], "BBX") && nword == 5)
	{
	    x = atoi(word[1]), y = atoi(word[2]);
	    if (x < 1 || x > 64 || y < 1 || y > 64)
		error("bad BBX (%dx%d)", x, y);
	}
	else if (streq(word[0], "BITMAP"))
	{
	    if (x < 0)
		error("missing BBX");
	    if (ch < 0)
		error("missing ENDCODING");
	    dobitmap(ch, x, y);
	}
    }
    error("unexpected EOF (missing ENDCHAR)");
}


static void dofile()
{
    lineno = 0;
    w = h = 0;
    bmap = 0;

    nextline();
    if (nword != 2 || not streq(word[0], "STARTFONT"))
	error("not a bdf-file");

    while (nextline())
    {
	if (streq(word[0], "ENDFONT"))
	    return;
	else if (streq(word[0], "FONTBOUNDINGBOX") && nword == 5)
	{
	    if (bmap)
		error("multiple FONTBOUNDINGBOXes!?!");
	    w = atoi(word[1]), h = atoi(word[2]);
	    if (w < 1 || w > 64 || h < 1 || h > 64)
		error("bad bounding box %dx%d\n", w, h);
	    bpl = (w*32+7)/8; // rounding is unnecessary
	    bmap = calloc(1, bpl * h*8);
	    if (bmap == 0)
		error("out of memory");
	}
	else if (streq(word[0], "STARTCHAR"))
	{
	    if (not bmap)
		error("no FONTBOUNDINGBOX");
	    dochar();
	}
    }
    error("unexpected EOF (missing ENDFONT)");
}


static void writexbm()
{
    char buf[256];
    int i, j;
    unsigned char *p = bmap;

    if (not bmap)
	return;

    sprintf(buf, font, w, h);

    printf("#define %s_width %d\n", buf, 32 * w);
    printf("#define %s_height %d\n", buf, 8 * h);
    printf("static unsigned char %s_bits[] = {\n", buf);
    for (i = 0; i < 16 * h * w / 8; ++i)
    {
	for (j = 0; j < 16; ++j)
	    printf("0x%02x,", *p++);
	printf("\n");
    }
    printf("};\n");
}


int main(int argc, char **argv)
{
    if (argc > 1)
	font = argv[1];
    dofile();
    writexbm();
    exit(0);
}