aboutsummaryrefslogtreecommitdiff
path: root/host/font.c
blob: 8906dfac2358426c08a9903d6ca2c3d69daaf0cf (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
#include "font.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void render_glyph(glyph_t *g, color_t *buf, unsigned int bufwidth, unsigned int offx, unsigned int offy, color_t fg, color_t bg){
	unsigned int bitmap_row_width = g->width/8;
	uint8_t *bitmap = ((uint8_t *)g) + sizeof(glyph_t);
	for(unsigned int y=0; y < g->height; y++){
		long int data = 0;
		for(unsigned int i=0; i<bitmap_row_width; i++){
			data <<= 8;
			data |= bitmap[y*bitmap_row_width+i];
		}
		color_t *p = buf + (offy+y)*bufwidth + offx;
		for(unsigned int x=0; x < g->width; x++){
			color_t c = (data&(1<<(g->width-1))) ? fg : bg;
			*p++ = c;
			data <<= 1;
		}
	}
}

int read_bdf(FILE *f, glyph_t **glyph_table, unsigned int glyph_table_size){
	char* line = 0;
	size_t len = 0;

	glyph_t current_glyph = {-1, -1, -1, -1};
	unsigned int dwidth = -1;
	unsigned int encoding = -1;
	void *glyph_data = 0;

	// Clear glyph table before use
	memset(glyph_table, 0, sizeof(*glyph_table));

	for(;;){
		size_t read = getline(&line, &len, f);
		if(read == -1){
			fprintf(stderr, "Could not read line from font file\n");
			goto error;
		}
		if(read < 2) // Ignore empty lines
			continue;
		line[read-1] = '\0';
		char *endptr;
		char *args = strchr(line, ' ');
		if(args){
			*args = 0;
			args++;
		}

		if(strcmp("ENDFONT", line) == 0){
			break;
		}else if(strcmp("ENCODING", line) == 0){
			if(!args){
				fprintf(stderr, "Invalid ENCODING line: %s %s\n", line, args);
				goto error;
			}

			encoding = strtol(args, &endptr, 10);
			if(args == endptr || *endptr != '\0'){
				fprintf(stderr, "Invalid ENCODING line: %s %s\n", line, args);
				goto error;
			}
			if(encoding > glyph_table_size){
				fprintf(stderr, "Codepoint out of valid range (0-%d): %d", glyph_table_size, encoding);
				goto error;
			}

		}else if(strcmp("BBX", line) == 0){
			if(!args){
				fprintf(stderr, "Invalid ENCODING line: %s %s\n", line, args);
				goto error;
			}

			endptr = 0;
			char *w = strtok_r(args, " ", &endptr);
			char *h = strtok_r(NULL, " ", &endptr);
			char *x = strtok_r(NULL, " ", &endptr);
			char *y = strtok_r(NULL, " ", &endptr);
			if(!w || !h || !x || !y){
				fprintf(stderr, "Invalid BBX line: %s %s\n", line, args);
				goto error;
			}
			current_glyph.width = atoi(w);
			current_glyph.height = atoi(h);
			current_glyph.x = atoi(x);
			current_glyph.y = atoi(y);

		}else if(strcmp("DWIDTH", line) == 0){
			if(!args){
				fprintf(stderr, "Invalid ENCODING line: %s %s\n", line, args);
				goto error;
			}

			endptr = 0;
			char *w = strtok_r(args, " ", &endptr);
			char *h = strtok_r(NULL, " ", &endptr);
			if(!w || !h){
				fprintf(stderr, "Invalid DWIDTH line: %s %s\n", line, args);
				goto error;
			}

			dwidth = strtol(w, &endptr, 10);
			if(w == endptr || *endptr != '\0'){
				fprintf(stderr, "Invalid DWIDTH line: %s %s\n", line, args);
				goto error;
			}

		}else if(strcmp("BITMAP", line) == 0){
			unsigned int row_bytes = dwidth/8;
			glyph_data = malloc(sizeof(glyph_t) + row_bytes * current_glyph.height);
			if(!glyph_data){
				fprintf(stderr, "Cannot malloc() memory.\n");
				goto error;
			}
			uint8_t *bitmap = (uint8_t *)glyph_data + sizeof(glyph_t);

			if(current_glyph.width < 0 || dwidth < 0 || encoding < 0){
				fprintf(stderr, "Invalid glyph: ");
				if(current_glyph.width < 0)
					fprintf(stderr, "No bounding box given.");
				if(dwidth < 0)
					fprintf(stderr, "No data width given.");
				if(encoding < 0)
					fprintf(stderr, "No codepoint given.");
				fprintf(stderr, "\n");
				goto error;
			}

			if(current_glyph.width != dwidth){
				fprintf(stderr, "Invalid glyph: bounding box width != dwidth\n");
				goto error;
			}

			unsigned int i=0;
			while(1){
				read = getline(&line, &len, f);
				if(read == -1){
					fprintf(stderr, "Could not read line from font file\n");
					goto error;
				}
				if(read < 2) // Ignore empty lines
					continue;
				line[read-1] = '\0';
				
				if(strncmp("ENDCHAR", line, read) == 0)
					break;

				if(i >= current_glyph.height){
					fprintf(stderr, "Too many rows of bitmap data in glyph %d: %d > %d\n", encoding, i+1, current_glyph.height);
					goto error;
				}

				//XXX This limits the maximum character width to sizeof(long)*8 (normally 64) piccells.
				long int data = strtol(line, &endptr, 16);
				if(line == endptr || *endptr != '\0'){
					fprintf(stderr, "Invalid bitmap data in glyph %d: %s\n", encoding, line);
					goto error;
				}

				// Right-align data
				data >>= ((read-1)*4 - dwidth);
				// Copy rightmost bytes of data to destination buffer
				for(unsigned int j=0; j<row_bytes; j++){
					bitmap[(i+1)*row_bytes-j-1] = data&0xFF;
					data >>= 8;
				}
				i++;
			}
			memcpy(glyph_data, &current_glyph, sizeof(glyph_t));
			glyph_table[encoding] = glyph_data;

			// Reset things for next iteration
			current_glyph.width = -1;
			current_glyph.height = -1;
			current_glyph.x = -1;
			current_glyph.y = -1;
			dwidth = -1;
			encoding = -1;
		}
	}
	return 0;
error:
	// Free malloc()ed glyphs
	free(glyph_data);
	for(unsigned int i=0; i<sizeof(glyph_table)/sizeof(glyph_t *); i++)
		free(glyph_table[i]);
	return 1;
}