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