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