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