xref: /freebsd/usr.bin/vtfontcvt/vtfontcvt.c (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 /*-
2  * Copyright (c) 2009, 2014 The FreeBSD Foundation
3  *
4  * This software was developed by Ed Schouten under sponsorship from the
5  * FreeBSD Foundation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/types.h>
33 #include <sys/endian.h>
34 #include <sys/fnv_hash.h>
35 #include <sys/font.h>
36 #include <sys/param.h>
37 #include <sys/queue.h>
38 
39 #include <assert.h>
40 #include <err.h>
41 #include <lz4.h>
42 #include <stdbool.h>
43 #include <stdint.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 
49 #define VFNT_MAXGLYPHS 131072
50 #define VFNT_MAXDIMENSION 128
51 
52 static unsigned int width = 8, wbytes, height = 16;
53 
54 struct glyph {
55 	TAILQ_ENTRY(glyph)	 g_list;
56 	SLIST_ENTRY(glyph)	 g_hash;
57 	uint8_t			*g_data;
58 	unsigned int		 g_index;
59 };
60 
61 #define	FONTCVT_NHASH 4096
62 TAILQ_HEAD(glyph_list, glyph);
63 static SLIST_HEAD(, glyph) glyph_hash[FONTCVT_NHASH];
64 static struct glyph_list glyphs[VFNT_MAPS] = {
65 	TAILQ_HEAD_INITIALIZER(glyphs[0]),
66 	TAILQ_HEAD_INITIALIZER(glyphs[1]),
67 	TAILQ_HEAD_INITIALIZER(glyphs[2]),
68 	TAILQ_HEAD_INITIALIZER(glyphs[3]),
69 };
70 static unsigned int glyph_total, glyph_count[4], glyph_unique, glyph_dupe;
71 
72 struct mapping {
73 	TAILQ_ENTRY(mapping)	 m_list;
74 	unsigned int		 m_char;
75 	unsigned int		 m_length;
76 	struct glyph		*m_glyph;
77 };
78 
79 TAILQ_HEAD(mapping_list, mapping);
80 static struct mapping_list maps[VFNT_MAPS] = {
81 	TAILQ_HEAD_INITIALIZER(maps[0]),
82 	TAILQ_HEAD_INITIALIZER(maps[1]),
83 	TAILQ_HEAD_INITIALIZER(maps[2]),
84 	TAILQ_HEAD_INITIALIZER(maps[3]),
85 };
86 static unsigned int mapping_total, map_count[4], map_folded_count[4],
87     mapping_unique, mapping_dupe;
88 
89 enum output_format {
90 	VT_FONT,		/* default */
91 	VT_C_SOURCE,		/* C source for built in fonts */
92 	VT_C_COMPRESSED		/* C source with compressed font data */
93 };
94 
95 struct whitelist {
96 	uint32_t c;
97 	uint32_t len;
98 };
99 
100 /*
101  * Compressed font glyph list. To be used with boot loader, we need to have
102  * ascii set and box drawing chars.
103  */
104 static struct whitelist c_list[] = {
105 	{ .c = 0, .len = 0 },		/* deault char */
106 	{ .c = 0x20, .len = 0x5f },
107 	{ .c = 0x2500, .len = 0 },	/* single frame */
108 	{ .c = 0x2502, .len = 0 },
109 	{ .c = 0x250c, .len = 0 },
110 	{ .c = 0x2510, .len = 0 },
111 	{ .c = 0x2514, .len = 0 },
112 	{ .c = 0x2518, .len = 0 },
113 	{ .c = 0x2550, .len = 1 },	/* double frame */
114 	{ .c = 0x2554, .len = 0 },
115 	{ .c = 0x2557, .len = 0 },
116 	{ .c = 0x255a, .len = 0 },
117 	{ .c = 0x255d, .len = 0 },
118 };
119 
120 /*
121  * Uncompressed source. For x86 we need cp437 so the vga text mode
122  * can program font into the vga card.
123  */
124 static struct whitelist s_list[] = {
125 	{ .c = 0, .len = 0 },		/* deault char */
126 	{ .c = 0x20, .len = 0x5f },	/* ascii set */
127 	{ .c = 0xA0, .len = 0x5f },	/* latin 1 */
128 	{ .c = 0x0192, .len = 0 },
129 	{ .c = 0x0332, .len = 0 },	/* composing lower line */
130 	{ .c = 0x0393, .len = 0 },
131 	{ .c = 0x0398, .len = 0 },
132 	{ .c = 0x03A3, .len = 0 },
133 	{ .c = 0x03A6, .len = 0 },
134 	{ .c = 0x03A9, .len = 0 },
135 	{ .c = 0x03B1, .len = 1 },
136 	{ .c = 0x03B4, .len = 0 },
137 	{ .c = 0x03C0, .len = 0 },
138 	{ .c = 0x03C3, .len = 0 },
139 	{ .c = 0x03C4, .len = 0 },
140 	{ .c = 0x207F, .len = 0 },
141 	{ .c = 0x20A7, .len = 0 },
142 	{ .c = 0x2205, .len = 0 },
143 	{ .c = 0x220A, .len = 0 },
144 	{ .c = 0x2219, .len = 1 },
145 	{ .c = 0x221E, .len = 0 },
146 	{ .c = 0x2229, .len = 0 },
147 	{ .c = 0x2248, .len = 0 },
148 	{ .c = 0x2261, .len = 0 },
149 	{ .c = 0x2264, .len = 1 },
150 	{ .c = 0x2310, .len = 0 },
151 	{ .c = 0x2320, .len = 1 },
152 	{ .c = 0x2500, .len = 0 },
153 	{ .c = 0x2502, .len = 0 },
154 	{ .c = 0x250C, .len = 0 },
155 	{ .c = 0x2510, .len = 0 },
156 	{ .c = 0x2514, .len = 0 },
157 	{ .c = 0x2518, .len = 0 },
158 	{ .c = 0x251C, .len = 0 },
159 	{ .c = 0x2524, .len = 0 },
160 	{ .c = 0x252C, .len = 0 },
161 	{ .c = 0x2534, .len = 0 },
162 	{ .c = 0x253C, .len = 0 },
163 	{ .c = 0x2550, .len = 0x1c },
164 	{ .c = 0x2580, .len = 0 },
165 	{ .c = 0x2584, .len = 0 },
166 	{ .c = 0x2588, .len = 0 },
167 	{ .c = 0x258C, .len = 0 },
168 	{ .c = 0x2590, .len = 3 },
169 	{ .c = 0x25A0, .len = 0 },
170 };
171 
172 static bool filter = true;
173 static enum output_format format = VT_FONT;
174 /* Type for write callback. */
175 typedef size_t (*vt_write)(const void *, size_t, size_t, FILE *);
176 static uint8_t *uncompressed;
177 
178 static void
179 usage(void)
180 {
181 
182 	(void)fprintf(stderr, "usage: vtfontcvt "
183 	    "[-n] [-f font|source|compressed-source] [-w width] "
184 	    "[-h height]\n\t[-v] normal.bdf [bold.bdf] out.fnt\n");
185 	exit(1);
186 }
187 
188 static void *
189 xmalloc(size_t size)
190 {
191 	void *m;
192 
193 	if ((m = calloc(1, size)) == NULL)
194 		errx(1, "memory allocation failure");
195 	return (m);
196 }
197 
198 static int
199 add_mapping(struct glyph *gl, unsigned int c, unsigned int map_idx)
200 {
201 	struct mapping *mp, *mp_temp;
202 	struct mapping_list *ml;
203 
204 	mapping_total++;
205 
206 	mp = xmalloc(sizeof *mp);
207 	mp->m_char = c;
208 	mp->m_glyph = gl;
209 	mp->m_length = 0;
210 
211 	ml = &maps[map_idx];
212 	if (TAILQ_LAST(ml, mapping_list) == NULL ||
213 	    TAILQ_LAST(ml, mapping_list)->m_char < c) {
214 		/* Common case: empty list or new char at end of list. */
215 		TAILQ_INSERT_TAIL(ml, mp, m_list);
216 	} else {
217 		/* Find insertion point for char; cannot be at end. */
218 		TAILQ_FOREACH(mp_temp, ml, m_list) {
219 			if (mp_temp->m_char >= c) {
220 				TAILQ_INSERT_BEFORE(mp_temp, mp, m_list);
221 				break;
222 			}
223 		}
224 	}
225 
226 	map_count[map_idx]++;
227 	mapping_unique++;
228 
229 	return (0);
230 }
231 
232 static int
233 dedup_mapping(unsigned int map_idx)
234 {
235 	struct mapping *mp_bold, *mp_normal, *mp_temp;
236 	unsigned normal_map_idx = map_idx - VFNT_MAP_BOLD;
237 
238 	assert(map_idx == VFNT_MAP_BOLD || map_idx == VFNT_MAP_BOLD_RIGHT);
239 	mp_normal = TAILQ_FIRST(&maps[normal_map_idx]);
240 	TAILQ_FOREACH_SAFE(mp_bold, &maps[map_idx], m_list, mp_temp) {
241 		while (mp_normal->m_char < mp_bold->m_char)
242 			mp_normal = TAILQ_NEXT(mp_normal, m_list);
243 		if (mp_bold->m_char != mp_normal->m_char)
244 			errx(1, "Character %u not in normal font!",
245 			    mp_bold->m_char);
246 		if (mp_bold->m_glyph != mp_normal->m_glyph)
247 			continue;
248 
249 		/* No mapping is needed if it's equal to the normal mapping. */
250 		TAILQ_REMOVE(&maps[map_idx], mp_bold, m_list);
251 		free(mp_bold);
252 		mapping_dupe++;
253 	}
254 	return (0);
255 }
256 
257 static struct glyph *
258 add_glyph(const uint8_t *bytes, unsigned int map_idx, int fallback)
259 {
260 	struct glyph *gl;
261 	int hash;
262 
263 	glyph_total++;
264 	glyph_count[map_idx]++;
265 
266 	/* Return existing glyph if we have an identical one. */
267 	hash = fnv_32_buf(bytes, wbytes * height, FNV1_32_INIT) % FONTCVT_NHASH;
268 	SLIST_FOREACH(gl, &glyph_hash[hash], g_hash) {
269 		if (memcmp(gl->g_data, bytes, wbytes * height) == 0) {
270 			glyph_dupe++;
271 			return (gl);
272 		}
273 	}
274 
275 	/* Allocate new glyph. */
276 	gl = xmalloc(sizeof *gl);
277 	gl->g_data = xmalloc(wbytes * height);
278 	memcpy(gl->g_data, bytes, wbytes * height);
279 	if (fallback)
280 		TAILQ_INSERT_HEAD(&glyphs[map_idx], gl, g_list);
281 	else
282 		TAILQ_INSERT_TAIL(&glyphs[map_idx], gl, g_list);
283 	SLIST_INSERT_HEAD(&glyph_hash[hash], gl, g_hash);
284 
285 	glyph_unique++;
286 	if (glyph_unique > VFNT_MAXGLYPHS)
287 		errx(1, "too many glyphs (%u)", glyph_unique);
288 	return (gl);
289 }
290 
291 static bool
292 check_whitelist(unsigned c)
293 {
294 	struct whitelist *w = NULL;
295 	int i, n = 0;
296 
297 	if (filter == false)
298 		return (true);
299 
300 	if (format == VT_C_SOURCE) {
301 		w = s_list;
302 		n = sizeof(s_list) / sizeof(s_list[0]);
303 	}
304 	if (format == VT_C_COMPRESSED) {
305 		w = c_list;
306 		n = sizeof(c_list) / sizeof(c_list[0]);
307 	}
308 	if (w == NULL)
309 		return (true);
310 	for (i = 0; i < n; i++) {
311 		if (c >= w[i].c && c <= w[i].c + w[i].len)
312 			return (true);
313 	}
314 	return (false);
315 }
316 
317 static int
318 add_char(unsigned curchar, unsigned map_idx, uint8_t *bytes, uint8_t *bytes_r)
319 {
320 	struct glyph *gl;
321 
322 	/* Prevent adding two glyphs for 0xFFFD */
323 	if (curchar == 0xFFFD) {
324 		if (map_idx < VFNT_MAP_BOLD)
325 			gl = add_glyph(bytes, 0, 1);
326 	} else if (filter == false || curchar >= 0x20) {
327 		gl = add_glyph(bytes, map_idx, 0);
328 		if (add_mapping(gl, curchar, map_idx) != 0)
329 			return (1);
330 		if (bytes_r != NULL) {
331 			gl = add_glyph(bytes_r, map_idx + 1, 0);
332 			if (add_mapping(gl, curchar, map_idx + 1) != 0)
333 				return (1);
334 		}
335 	}
336 	return (0);
337 }
338 
339 /*
340  * Right-shift glyph row.
341  */
342 static void
343 rshift_row(uint8_t *buf, size_t len, size_t shift)
344 {
345 	ssize_t i, off_byte = shift / 8;
346 	size_t off_bit = shift % 8;
347 
348 	if (shift == 0)
349 		return;
350 	for (i = len - 1; i >= 0; i--)
351 		buf[i] = (i >= off_byte ? buf[i - off_byte] >> off_bit : 0) |
352 		    (i > off_byte ? buf[i - off_byte - 1] << (8 - off_bit) : 0);
353 }
354 
355 /*
356  * Split double-width characters into left and right half. Single-width
357  * characters in _left_ only.
358  */
359 static int
360 split_row(uint8_t *left, uint8_t *right, uint8_t *line, size_t w)
361 {
362 	size_t s, i;
363 
364 	s = wbytes * 8 - width;
365 
366 	memcpy(left, line, wbytes);
367 	*(left + wbytes - 1) &= 0xFF << s;
368 
369 	if (w > width) { /* Double-width character. */
370 		uint8_t t;
371 
372 		for (i = 0; i < wbytes; i++) {
373 			t = *(line + wbytes + i - 1);
374 			t <<= 8 - s;
375 			t |= *(line + wbytes + i) >> s;
376 			*(right + i) = t;
377 		}
378 		*(right + wbytes - 1) &= 0xFF << s;
379 	}
380 	return (0);
381 }
382 
383 static void
384 set_height(int h)
385 {
386 	if (h <= 0 || h > VFNT_MAXDIMENSION)
387 		errx(1, "invalid height %d", h);
388 	height = h;
389 }
390 
391 static void
392 set_width(int w)
393 {
394 	if (w <= 0 || w > VFNT_MAXDIMENSION)
395 		errx(1, "invalid width %d", w);
396 	width = w;
397 	wbytes = howmany(width, 8);
398 }
399 
400 static int
401 parse_bdf(FILE *fp, unsigned int map_idx)
402 {
403 	char *ln, *p;
404 	size_t length;
405 	uint8_t *line, *bytes, *bytes_r;
406 	unsigned int curchar = 0, i, j, linenum = 0, bbwbytes;
407 	int bbw, bbh, bbox, bboy;		/* Glyph bounding box. */
408 	int fbbw = 0, fbbh, fbbox, fbboy;	/* Font bounding box. */
409 	int dwidth = 0, dwy = 0;
410 	int rv = -1;
411 	char spc = '\0';
412 
413 	/*
414 	 * Step 1: Parse FONT logical font descriptor and FONTBOUNDINGBOX
415 	 * bounding box.
416 	 */
417 	while ((ln = fgetln(fp, &length)) != NULL) {
418 		linenum++;
419 		ln[length - 1] = '\0';
420 
421 		if (strncmp(ln, "FONT ", 5) == 0) {
422 			p = ln + 5;
423 			i = 0;
424 			while ((p = strchr(p, '-')) != NULL) {
425 				p++;
426 				i++;
427 				if (i == 11) {
428 					spc = *p;
429 					break;
430 				}
431 			}
432 		} else if (strncmp(ln, "FONTBOUNDINGBOX ", 16) == 0) {
433 			if (sscanf(ln + 16, "%d %d %d %d", &fbbw, &fbbh, &fbbox,
434 			    &fbboy) != 4)
435 				errx(1, "invalid FONTBOUNDINGBOX at line %u",
436 				    linenum);
437 			set_width(fbbw);
438 			set_height(fbbh);
439 			break;
440 		}
441 	}
442 	if (fbbw == 0)
443 		errx(1, "broken font header");
444 	if (spc != 'c' && spc != 'C')
445 		errx(1, "font spacing \"C\" (character cell) required");
446 
447 	/* Step 2: Validate DWIDTH (Device Width) of all glyphs. */
448 	while ((ln = fgetln(fp, &length)) != NULL) {
449 		linenum++;
450 		ln[length - 1] = '\0';
451 
452 		if (strncmp(ln, "DWIDTH ", 7) == 0) {
453 			if (sscanf(ln + 7, "%d %d", &dwidth, &dwy) != 2)
454 				errx(1, "invalid DWIDTH at line %u", linenum);
455 			if (dwy != 0 || (dwidth != fbbw && dwidth * 2 != fbbw))
456 				errx(1, "bitmap with unsupported DWIDTH %d %d at line %u",
457 				    dwidth, dwy, linenum);
458 			if (dwidth < fbbw)
459 				set_width(dwidth);
460 		}
461 	}
462 
463 	/* Step 3: Restart at the beginning of the file and read glyph data. */
464 	dwidth = bbw = bbh = 0;
465 	rewind(fp);
466 	linenum = 0;
467 	bbwbytes = 0; /* GCC 4.2.1 "may be used uninitialized" workaround. */
468 	bytes = xmalloc(wbytes * height);
469 	bytes_r = xmalloc(wbytes * height);
470 	line = xmalloc(wbytes * 2);
471 	while ((ln = fgetln(fp, &length)) != NULL) {
472 		linenum++;
473 		ln[length - 1] = '\0';
474 
475 		if (strncmp(ln, "ENCODING ", 9) == 0) {
476 			curchar = atoi(ln + 9);
477 		} else if (strncmp(ln, "DWIDTH ", 7) == 0) {
478 			dwidth = atoi(ln + 7);
479 		} else if (strncmp(ln, "BBX ", 4) == 0) {
480 			if (sscanf(ln + 4, "%d %d %d %d", &bbw, &bbh, &bbox,
481 			     &bboy) != 4)
482 				errx(1, "invalid BBX at line %u", linenum);
483 			if (bbw < 1 || bbh < 1 || bbw > fbbw || bbh > fbbh ||
484 			    bbox < fbbox || bboy < fbboy ||
485 			    bbh + bboy > fbbh + fbboy)
486 				errx(1, "broken bitmap with BBX %d %d %d %d at line %u",
487 				    bbw, bbh, bbox, bboy, linenum);
488 			bbwbytes = howmany(bbw, 8);
489 		} else if (strncmp(ln, "BITMAP", 6) == 0 &&
490 		    (ln[6] == ' ' || ln[6] == '\0')) {
491 			if (dwidth == 0 || bbw == 0 || bbh == 0)
492 				errx(1, "broken char header at line %u!",
493 				    linenum);
494 			memset(bytes, 0, wbytes * height);
495 			memset(bytes_r, 0, wbytes * height);
496 
497 			/*
498 			 * Assume that the next _bbh_ lines are bitmap data.
499 			 * ENDCHAR is allowed to terminate the bitmap
500 			 * early but is not otherwise checked; any extra data
501 			 * is ignored.
502 			 */
503 			for (i = (fbbh + fbboy) - (bbh + bboy);
504 			    i < (unsigned int)((fbbh + fbboy) - bboy); i++) {
505 				if ((ln = fgetln(fp, &length)) == NULL)
506 					errx(1, "unexpected EOF");
507 				linenum++;
508 				ln[length - 1] = '\0';
509 				if (strcmp(ln, "ENDCHAR") == 0)
510 					break;
511 				if (strlen(ln) < bbwbytes * 2)
512 					errx(1, "broken bitmap at line %u",
513 					    linenum);
514 				memset(line, 0, wbytes * 2);
515 				for (j = 0; j < bbwbytes; j++) {
516 					unsigned int val;
517 					if (sscanf(ln + j * 2, "%2x", &val) ==
518 					    0)
519 						break;
520 					*(line + j) = (uint8_t)val;
521 				}
522 
523 				rshift_row(line, wbytes * 2, bbox - fbbox);
524 				rv = split_row(bytes + i * wbytes,
525 				     bytes_r + i * wbytes, line, dwidth);
526 				if (rv != 0)
527 					goto out;
528 			}
529 
530 			if (check_whitelist(curchar) == true) {
531 				rv = add_char(curchar, map_idx, bytes,
532 				    dwidth > (int)width ? bytes_r : NULL);
533 				if (rv != 0)
534 					goto out;
535 			}
536 
537 			dwidth = bbw = bbh = 0;
538 		}
539 	}
540 
541 out:
542 	free(bytes);
543 	free(bytes_r);
544 	free(line);
545 	return (rv);
546 }
547 
548 static int
549 parse_hex(FILE *fp, unsigned int map_idx)
550 {
551 	char *ln, *p;
552 	size_t length;
553 	uint8_t *bytes = NULL, *bytes_r = NULL, *line = NULL;
554 	unsigned curchar = 0, gwidth, gwbytes, i, j, chars_per_row;
555 	int rv = 0;
556 
557 	while ((ln = fgetln(fp, &length)) != NULL) {
558 		ln[length - 1] = '\0';
559 
560 		if (strncmp(ln, "# Height: ", 10) == 0) {
561 			if (bytes != NULL)
562 				errx(1, "malformed input: Height tag after font data");
563 			set_height(atoi(ln + 10));
564 		} else if (strncmp(ln, "# Width: ", 9) == 0) {
565 			if (bytes != NULL)
566 				errx(1, "malformed input: Width tag after font data");
567 			set_width(atoi(ln + 9));
568 		} else if (sscanf(ln, "%6x:", &curchar)) {
569 			if (bytes == NULL) {
570 				bytes = xmalloc(wbytes * height);
571 				bytes_r = xmalloc(wbytes * height);
572 				line = xmalloc(wbytes * 2);
573 			}
574 			/* ln is guaranteed to have a colon here. */
575 			p = strchr(ln, ':') + 1;
576 			chars_per_row = strlen(p) / height;
577 			if (chars_per_row < wbytes * 2)
578 				errx(1,
579 				    "malformed input: broken bitmap, character %06x",
580 				    curchar);
581 			gwidth = width * 2;
582 			gwbytes = howmany(gwidth, 8);
583 			if (chars_per_row < gwbytes * 2 || gwidth <= 8) {
584 				gwidth = width; /* Single-width character. */
585 				gwbytes = wbytes;
586 			}
587 
588 			for (i = 0; i < height; i++) {
589 				for (j = 0; j < gwbytes; j++) {
590 					unsigned int val;
591 					if (sscanf(p + j * 2, "%2x", &val) == 0)
592 						break;
593 					*(line + j) = (uint8_t)val;
594 				}
595 				rv = split_row(bytes + i * wbytes,
596 				    bytes_r + i * wbytes, line, gwidth);
597 				if (rv != 0)
598 					goto out;
599 				p += gwbytes * 2;
600 			}
601 
602 			if (check_whitelist(curchar) == true) {
603 				rv = add_char(curchar, map_idx, bytes,
604 				    gwidth != width ? bytes_r : NULL);
605 				if (rv != 0)
606 					goto out;
607 			}
608 		}
609 	}
610 out:
611 	free(bytes);
612 	free(bytes_r);
613 	free(line);
614 	return (rv);
615 }
616 
617 static int
618 parse_file(const char *filename, unsigned int map_idx)
619 {
620 	FILE *fp;
621 	size_t len;
622 	int rv;
623 
624 	fp = fopen(filename, "r");
625 	if (fp == NULL) {
626 		perror(filename);
627 		return (1);
628 	}
629 	len = strlen(filename);
630 	if (len > 4 && strcasecmp(filename + len - 4, ".hex") == 0)
631 		rv = parse_hex(fp, map_idx);
632 	else
633 		rv = parse_bdf(fp, map_idx);
634 	fclose(fp);
635 	return (rv);
636 }
637 
638 static void
639 number_glyphs(void)
640 {
641 	struct glyph *gl;
642 	unsigned int i, idx = 0;
643 
644 	for (i = 0; i < VFNT_MAPS; i++)
645 		TAILQ_FOREACH(gl, &glyphs[i], g_list)
646 			gl->g_index = idx++;
647 }
648 
649 /* Note we only deal with byte stream here. */
650 static size_t
651 write_glyph_source(const void *ptr, size_t size, size_t nitems, FILE *stream)
652 {
653 	const uint8_t *data = ptr;
654 	size_t i;
655 
656 	size *= nitems;
657 	for (i = 0; i < size; i++) {
658 		if ((i % wbytes) == 0) {
659 			if (fprintf(stream, "\n") < 0)
660 				return (0);
661 		}
662 		if (fprintf(stream, "0x%02x, ", data[i]) < 0)
663 			return (0);
664 	}
665 	if (fprintf(stream, "\n") < 0)
666 		nitems = 0;
667 
668 	return (nitems);
669 }
670 
671 /* Write to buffer */
672 static size_t
673 write_glyph_buf(const void *ptr, size_t size, size_t nitems,
674     FILE *stream __unused)
675 {
676 	static size_t index = 0;
677 
678 	size *= nitems;
679 	(void) memmove(uncompressed + index, ptr, size);
680 	index += size;
681 
682 	return (nitems);
683 }
684 
685 static int
686 write_glyphs(FILE *fp, vt_write cb)
687 {
688 	struct glyph *gl;
689 	unsigned int i;
690 
691 	for (i = 0; i < VFNT_MAPS; i++) {
692 		TAILQ_FOREACH(gl, &glyphs[i], g_list)
693 			if (cb(gl->g_data, wbytes * height, 1, fp) != 1)
694 				return (1);
695 	}
696 	return (0);
697 }
698 
699 static void
700 fold_mappings(unsigned int map_idx)
701 {
702 	struct mapping_list *ml = &maps[map_idx];
703 	struct mapping *mn, *mp, *mbase;
704 
705 	mp = mbase = TAILQ_FIRST(ml);
706 	for (mp = mbase = TAILQ_FIRST(ml); mp != NULL; mp = mn) {
707 		mn = TAILQ_NEXT(mp, m_list);
708 		if (mn != NULL && mn->m_char == mp->m_char + 1 &&
709 		    mn->m_glyph->g_index == mp->m_glyph->g_index + 1)
710 			continue;
711 		mbase->m_length = mp->m_char - mbase->m_char + 1;
712 		mbase = mp = mn;
713 		map_folded_count[map_idx]++;
714 	}
715 }
716 
717 static int
718 write_mappings(FILE *fp, unsigned int map_idx)
719 {
720 	struct mapping_list *ml = &maps[map_idx];
721 	struct mapping *mp;
722 	vfnt_map_t fm;
723 	unsigned int i = 0, j = 0;
724 
725 	TAILQ_FOREACH(mp, ml, m_list) {
726 		j++;
727 		if (mp->m_length > 0) {
728 			i += mp->m_length;
729 			fm.vfm_src = htobe32(mp->m_char);
730 			fm.vfm_dst = htobe16(mp->m_glyph->g_index);
731 			fm.vfm_len = htobe16(mp->m_length - 1);
732 			if (fwrite(&fm, sizeof fm, 1, fp) != 1)
733 				return (1);
734 		}
735 	}
736 	assert(i == j);
737 	return (0);
738 }
739 
740 static int
741 write_source_mappings(FILE *fp, unsigned int map_idx)
742 {
743 	struct mapping_list *ml = &maps[map_idx];
744 	struct mapping *mp;
745 	unsigned int i = 0, j = 0;
746 
747 	TAILQ_FOREACH(mp, ml, m_list) {
748 		j++;
749 		if (mp->m_length > 0) {
750 			i += mp->m_length;
751 			if (fprintf(fp, "\t{ 0x%08x, 0x%04x, 0x%04x },\n",
752 			    mp->m_char, mp->m_glyph->g_index,
753 			    mp->m_length - 1) < 0)
754 				return (1);
755 		}
756 	}
757 	assert(i == j);
758 	return (0);
759 }
760 
761 static int
762 write_fnt(const char *filename)
763 {
764 	FILE *fp;
765 	struct font_header fh = {
766 		.fh_magic = FONT_HEADER_MAGIC,
767 	};
768 
769 	fp = fopen(filename, "wb");
770 	if (fp == NULL) {
771 		perror(filename);
772 		return (1);
773 	}
774 
775 	fh.fh_width = width;
776 	fh.fh_height = height;
777 	fh.fh_glyph_count = htobe32(glyph_unique);
778 	fh.fh_map_count[0] = htobe32(map_folded_count[0]);
779 	fh.fh_map_count[1] = htobe32(map_folded_count[1]);
780 	fh.fh_map_count[2] = htobe32(map_folded_count[2]);
781 	fh.fh_map_count[3] = htobe32(map_folded_count[3]);
782 	if (fwrite(&fh, sizeof(fh), 1, fp) != 1) {
783 		perror(filename);
784 		fclose(fp);
785 		return (1);
786 	}
787 
788 	if (write_glyphs(fp, &fwrite) != 0 ||
789 	    write_mappings(fp, VFNT_MAP_NORMAL) != 0 ||
790 	    write_mappings(fp, VFNT_MAP_NORMAL_RIGHT) != 0 ||
791 	    write_mappings(fp, VFNT_MAP_BOLD) != 0 ||
792 	    write_mappings(fp, VFNT_MAP_BOLD_RIGHT) != 0) {
793 		perror(filename);
794 		fclose(fp);
795 		return (1);
796 	}
797 
798 	fclose(fp);
799 	return (0);
800 }
801 
802 static int
803 write_fnt_source(bool lz4, const char *filename)
804 {
805 	FILE *fp;
806 	int rv = 1;
807 	size_t uncompressed_size = wbytes * height * glyph_unique;
808 	size_t compressed_size = uncompressed_size;
809 	uint8_t *compressed = NULL;
810 
811 	fp = fopen(filename, "w");
812 	if (fp == NULL) {
813 		perror(filename);
814 		return (1);
815 	}
816 
817 	if (lz4 == true) {
818 		uncompressed = xmalloc(uncompressed_size);
819 		compressed = xmalloc(uncompressed_size);
820 	}
821 	if (fprintf(fp, "/* Generated %ux%u console font source. */\n\n",
822 	    width, height) < 0)
823 		goto done;
824 	if (fprintf(fp, "#include <sys/types.h>\n") < 0)
825 		goto done;
826 	if (fprintf(fp, "#include <sys/param.h>\n") < 0)
827 		goto done;
828 	if (fprintf(fp, "#include <sys/font.h>\n\n") < 0)
829 		goto done;
830 
831 	/* Write font bytes. */
832 	if (fprintf(fp, "static uint8_t FONTDATA_%ux%u[] = {\n",
833 	    width, height) < 0)
834 		goto done;
835 	if (lz4 == true) {
836 		if (write_glyphs(fp, &write_glyph_buf) != 0)
837 			goto done;
838 		compressed_size = lz4_compress(uncompressed, compressed,
839 		    uncompressed_size, compressed_size, 0);
840 		if (write_glyph_source(compressed, compressed_size, 1, fp) != 1)
841 			goto done;
842 		free(uncompressed);
843 		free(compressed);
844 	} else {
845 		if (write_glyphs(fp, &write_glyph_source) != 0)
846 			goto done;
847 	}
848 	if (fprintf(fp, "};\n\n") < 0)
849 		goto done;
850 
851 	/* Write font maps. */
852 	if (!TAILQ_EMPTY(&maps[VFNT_MAP_NORMAL])) {
853 		if (fprintf(fp, "static vfnt_map_t "
854 		    "FONTMAP_NORMAL_%ux%u[] = {\n", width, height) < 0)
855 			goto done;
856 		if (write_source_mappings(fp, VFNT_MAP_NORMAL) != 0)
857 			goto done;
858 		if (fprintf(fp, "};\n\n") < 0)
859 			goto done;
860 	}
861 	if (!TAILQ_EMPTY(&maps[VFNT_MAP_NORMAL_RIGHT])) {
862 		if (fprintf(fp, "static vfnt_map_t "
863 		    "FONTMAP_NORMAL_RH_%ux%u[] = {\n", width, height) < 0)
864 			goto done;
865 		if (write_source_mappings(fp, VFNT_MAP_NORMAL_RIGHT) != 0)
866 			goto done;
867 		if (fprintf(fp, "};\n\n") < 0)
868 			goto done;
869 	}
870 	if (!TAILQ_EMPTY(&maps[VFNT_MAP_BOLD])) {
871 		if (fprintf(fp, "static vfnt_map_t "
872 		    "FONTMAP_BOLD_%ux%u[] = {\n", width, height) < 0)
873 			goto done;
874 		if (write_source_mappings(fp, VFNT_MAP_BOLD) != 0)
875 			goto done;
876 		if (fprintf(fp, "};\n\n") < 0)
877 			goto done;
878 	}
879 	if (!TAILQ_EMPTY(&maps[VFNT_MAP_BOLD_RIGHT])) {
880 		if (fprintf(fp, "static vfnt_map_t "
881 		    "FONTMAP_BOLD_RH_%ux%u[] = {\n", width, height) < 0)
882 			goto done;
883 		if (write_source_mappings(fp, VFNT_MAP_BOLD_RIGHT) != 0)
884 			goto done;
885 		if (fprintf(fp, "};\n\n") < 0)
886 			goto done;
887 	}
888 
889 	/* Write struct font. */
890 	if (fprintf(fp, "struct vt_font font_%ux%u = {\n",
891 	    width, height) < 0)
892 		goto done;
893 	if (fprintf(fp, "\t.vf_map\t= {\n") < 0)
894 		goto done;
895 	if (TAILQ_EMPTY(&maps[VFNT_MAP_NORMAL])) {
896 		if (fprintf(fp, "\t\t\tNULL,\n") < 0)
897 			goto done;
898 	} else {
899 		if (fprintf(fp, "\t\t\tFONTMAP_NORMAL_%ux%u,\n",
900 		    width, height) < 0)
901 			goto done;
902 	}
903 	if (TAILQ_EMPTY(&maps[VFNT_MAP_NORMAL_RIGHT])) {
904 		if (fprintf(fp, "\t\t\tNULL,\n") < 0)
905 			goto done;
906 	} else {
907 		if (fprintf(fp, "\t\t\tFONTMAP_NORMAL_RH_%ux%u,\n",
908 		    width, height) < 0)
909 			goto done;
910 	}
911 	if (TAILQ_EMPTY(&maps[VFNT_MAP_BOLD])) {
912 		if (fprintf(fp, "\t\t\tNULL,\n") < 0)
913 			goto done;
914 	} else {
915 		if (fprintf(fp, "\t\t\tFONTMAP_BOLD_%ux%u,\n",
916 		    width, height) < 0)
917 			goto done;
918 	}
919 	if (TAILQ_EMPTY(&maps[VFNT_MAP_BOLD_RIGHT])) {
920 		if (fprintf(fp, "\t\t\tNULL\n") < 0)
921 			goto done;
922 	} else {
923 		if (fprintf(fp, "\t\t\tFONTMAP_BOLD_RH_%ux%u\n",
924 		    width, height) < 0)
925 			goto done;
926 	}
927 	if (fprintf(fp, "\t\t},\n") < 0)
928 		goto done;
929 	if (lz4 == true) {
930 		if (fprintf(fp, "\t.vf_bytes\t= NULL,\n") < 0)
931 			goto done;
932 	} else {
933 		if (fprintf(fp, "\t.vf_bytes\t= FONTDATA_%ux%u,\n",
934 		    width, height) < 0) {
935 			goto done;
936 		}
937 	}
938 	if (fprintf(fp, "\t.vf_width\t= %u,\n", width) < 0)
939 		goto done;
940 	if (fprintf(fp, "\t.vf_height\t= %u,\n", height) < 0)
941 		goto done;
942 	if (fprintf(fp, "\t.vf_map_count\t= { %u, %u, %u, %u }\n",
943 	    map_folded_count[0], map_folded_count[1], map_folded_count[2],
944 	    map_folded_count[3]) < 0) {
945 		goto done;
946 	}
947 	if (fprintf(fp, "};\n\n") < 0)
948 		goto done;
949 
950 	/* Write bitmap data. */
951 	if (fprintf(fp, "vt_font_bitmap_data_t font_data_%ux%u = {\n",
952 	    width, height) < 0)
953 		goto done;
954 	if (fprintf(fp, "\t.vfbd_width\t= %u,\n", width) < 0)
955 		goto done;
956 	if (fprintf(fp, "\t.vfbd_height\t= %u,\n", height) < 0)
957 		goto done;
958 	if (lz4 == true) {
959 		if (fprintf(fp, "\t.vfbd_compressed_size\t= %zu,\n",
960 		    compressed_size) < 0) {
961 			goto done;
962 		}
963 		if (fprintf(fp, "\t.vfbd_uncompressed_size\t= %zu,\n",
964 		    uncompressed_size) < 0) {
965 			goto done;
966 		}
967 		if (fprintf(fp, "\t.vfbd_compressed_data\t= FONTDATA_%ux%u,\n",
968 		    width, height) < 0) {
969 			goto done;
970 		}
971 	} else {
972 		if (fprintf(fp, "\t.vfbd_compressed_size\t= 0,\n") < 0)
973 			goto done;
974 		if (fprintf(fp, "\t.vfbd_uncompressed_size\t= %zu,\n",
975 		    uncompressed_size) < 0) {
976 			goto done;
977 		}
978 		if (fprintf(fp, "\t.vfbd_compressed_data\t= NULL,\n") < 0)
979 			goto done;
980 	}
981 	if (fprintf(fp, "\t.vfbd_font = &font_%ux%u\n", width, height) < 0)
982 		goto done;
983 	if (fprintf(fp, "};\n") < 0)
984 		goto done;
985 
986 	rv = 0;
987 done:
988 	if (rv != 0)
989 		perror(filename);
990 	fclose(fp);
991 	return (0);
992 }
993 
994 static void
995 print_font_info(void)
996 {
997 	printf(
998 "Statistics:\n"
999 "- width:                       %6u\n"
1000 "- height:                      %6u\n"
1001 "- glyph_total:                 %6u\n"
1002 "- glyph_normal:                %6u\n"
1003 "- glyph_normal_right:          %6u\n"
1004 "- glyph_bold:                  %6u\n"
1005 "- glyph_bold_right:            %6u\n"
1006 "- glyph_unique:                %6u\n"
1007 "- glyph_dupe:                  %6u\n"
1008 "- mapping_total:               %6u\n"
1009 "- mapping_normal:              %6u\n"
1010 "- mapping_normal_folded:       %6u\n"
1011 "- mapping_normal_right:        %6u\n"
1012 "- mapping_normal_right_folded: %6u\n"
1013 "- mapping_bold:                %6u\n"
1014 "- mapping_bold_folded:         %6u\n"
1015 "- mapping_bold_right:          %6u\n"
1016 "- mapping_bold_right_folded:   %6u\n"
1017 "- mapping_unique:              %6u\n"
1018 "- mapping_dupe:                %6u\n",
1019 	    width, height,
1020 	    glyph_total,
1021 	    glyph_count[0],
1022 	    glyph_count[1],
1023 	    glyph_count[2],
1024 	    glyph_count[3],
1025 	    glyph_unique, glyph_dupe,
1026 	    mapping_total,
1027 	    map_count[0], map_folded_count[0],
1028 	    map_count[1], map_folded_count[1],
1029 	    map_count[2], map_folded_count[2],
1030 	    map_count[3], map_folded_count[3],
1031 	    mapping_unique, mapping_dupe);
1032 }
1033 
1034 int
1035 main(int argc, char *argv[])
1036 {
1037 	int ch, verbose = 0, rv = 0;
1038 	char *outfile = NULL;
1039 
1040 	assert(sizeof(struct font_header) == 32);
1041 	assert(sizeof(vfnt_map_t) == 8);
1042 
1043 	while ((ch = getopt(argc, argv, "nf:h:vw:o:")) != -1) {
1044 		switch (ch) {
1045 		case 'f':
1046 			if (strcmp(optarg, "font") == 0)
1047 				format = VT_FONT;
1048 			else if (strcmp(optarg, "source") == 0)
1049 				format = VT_C_SOURCE;
1050 			else if (strcmp(optarg, "compressed-source") == 0)
1051 				format = VT_C_COMPRESSED;
1052 			else
1053 				errx(1, "Invalid format: %s", optarg);
1054 			break;
1055 		case 'h':
1056 			height = atoi(optarg);
1057 			break;
1058 		case 'n':
1059 			filter = false;
1060 			break;
1061 		case 'o':
1062 			outfile = optarg;
1063 			break;
1064 		case 'v':
1065 			verbose = 1;
1066 			break;
1067 		case 'w':
1068 			width = atoi(optarg);
1069 			break;
1070 		case '?':
1071 		default:
1072 			usage();
1073 		}
1074 	}
1075 	argc -= optind;
1076 	argv += optind;
1077 
1078 	if (outfile == NULL && (argc < 2 || argc > 3))
1079 		usage();
1080 
1081 	if (outfile == NULL) {
1082 		outfile = argv[argc - 1];
1083 		argc--;
1084 	}
1085 
1086 	set_width(width);
1087 	set_height(height);
1088 
1089 	if (parse_file(argv[0], VFNT_MAP_NORMAL) != 0)
1090 		return (1);
1091 	argc--;
1092 	argv++;
1093 	if (argc == 1) {
1094 		if (parse_file(argv[0], VFNT_MAP_BOLD) != 0)
1095 			return (1);
1096 		argc--;
1097 		argv++;
1098 	}
1099 	number_glyphs();
1100 	dedup_mapping(VFNT_MAP_BOLD);
1101 	dedup_mapping(VFNT_MAP_BOLD_RIGHT);
1102 	fold_mappings(0);
1103 	fold_mappings(1);
1104 	fold_mappings(2);
1105 	fold_mappings(3);
1106 
1107 	switch (format) {
1108 	case VT_FONT:
1109 		rv = write_fnt(outfile);
1110 		break;
1111 	case VT_C_SOURCE:
1112 		rv = write_fnt_source(false, outfile);
1113 		break;
1114 	case VT_C_COMPRESSED:
1115 		rv = write_fnt_source(true, outfile);
1116 		break;
1117 	}
1118 
1119 	if (verbose)
1120 		print_font_info();
1121 
1122 	return (rv);
1123 }
1124