xref: /illumos-gate/usr/src/cmd/vtfontcvt/vtfontcvt.c (revision ebe61a781b04a51e0700be7c93f60479ad63de01)
14b529e40SToomas Soome /*
24b529e40SToomas Soome  * Copyright (c) 2009, 2014 The FreeBSD Foundation
34b529e40SToomas Soome  * All rights reserved.
44b529e40SToomas Soome  *
54b529e40SToomas Soome  * This software was developed by Ed Schouten under sponsorship from the
64b529e40SToomas Soome  * FreeBSD Foundation.
74b529e40SToomas Soome  *
84b529e40SToomas Soome  * Redistribution and use in source and binary forms, with or without
94b529e40SToomas Soome  * modification, are permitted provided that the following conditions
104b529e40SToomas Soome  * are met:
114b529e40SToomas Soome  * 1. Redistributions of source code must retain the above copyright
124b529e40SToomas Soome  *    notice, this list of conditions and the following disclaimer.
134b529e40SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
144b529e40SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
154b529e40SToomas Soome  *    documentation and/or other materials provided with the distribution.
164b529e40SToomas Soome  *
174b529e40SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
184b529e40SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
194b529e40SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
204b529e40SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
214b529e40SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
224b529e40SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
234b529e40SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
244b529e40SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
254b529e40SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
264b529e40SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
274b529e40SToomas Soome  * SUCH DAMAGE.
284b529e40SToomas Soome  */
294b529e40SToomas Soome 
304b529e40SToomas Soome #include <sys/types.h>
314b529e40SToomas Soome #include <endian.h>
324b529e40SToomas Soome #include <sys/param.h>
334b529e40SToomas Soome #include <sys/queue.h>
344b529e40SToomas Soome 
354b529e40SToomas Soome #include <assert.h>
364b529e40SToomas Soome #include <err.h>
374b529e40SToomas Soome #include <stdint.h>
384b529e40SToomas Soome #include <stdio.h>
394b529e40SToomas Soome #include <stdlib.h>
404b529e40SToomas Soome #include <stdbool.h>
414b529e40SToomas Soome #include <string.h>
424b529e40SToomas Soome #include <unistd.h>
434b529e40SToomas Soome #include "fnv_hash.h"
444b529e40SToomas Soome 
454b529e40SToomas Soome #define	VFNT_MAPS 4
464b529e40SToomas Soome #define	VFNT_MAP_NORMAL 0
474b529e40SToomas Soome #define	VFNT_MAP_NORMAL_RH 1
484b529e40SToomas Soome #define	VFNT_MAP_BOLD 2
494b529e40SToomas Soome #define	VFNT_MAP_BOLD_RH 3
504b529e40SToomas Soome 
514b529e40SToomas Soome extern size_t lz4_compress(void *, void *, size_t, size_t, int);
524b529e40SToomas Soome 
534b529e40SToomas Soome static unsigned int width = 8, wbytes, height = 16;
544b529e40SToomas Soome 
554b529e40SToomas Soome struct bbox {
564b529e40SToomas Soome 	unsigned int width;	/* pixels */
574b529e40SToomas Soome 	unsigned int height;
584b529e40SToomas Soome 	int x;			/* lower left corner x */
594b529e40SToomas Soome 	int y;			/* lower left corner y */
604b529e40SToomas Soome };
614b529e40SToomas Soome 
624b529e40SToomas Soome static struct bbox bbox;	/* font bounding box */
634b529e40SToomas Soome static int font_ascent;		/* pixels above baseline */
644b529e40SToomas Soome static int font_descent;	/* pixels below baseline */
654b529e40SToomas Soome static unsigned int default_char = 0xFFFD;
664b529e40SToomas Soome 
674b529e40SToomas Soome struct glyph {
684b529e40SToomas Soome 	TAILQ_ENTRY(glyph)	 g_list;
694b529e40SToomas Soome 	SLIST_ENTRY(glyph)	 g_hash;
704b529e40SToomas Soome 	uint8_t			*g_data;
714b529e40SToomas Soome 	unsigned int		 g_index;
724b529e40SToomas Soome };
734b529e40SToomas Soome 
744b529e40SToomas Soome #define	FONTCVT_NHASH 4096
754b529e40SToomas Soome TAILQ_HEAD(glyph_list, glyph);
764b529e40SToomas Soome static SLIST_HEAD(, glyph) glyph_hash[FONTCVT_NHASH];
774b529e40SToomas Soome static struct glyph_list glyphs[VFNT_MAPS] = {
784b529e40SToomas Soome     TAILQ_HEAD_INITIALIZER(glyphs[0]),
794b529e40SToomas Soome     TAILQ_HEAD_INITIALIZER(glyphs[1]),
804b529e40SToomas Soome     TAILQ_HEAD_INITIALIZER(glyphs[2]),
814b529e40SToomas Soome     TAILQ_HEAD_INITIALIZER(glyphs[3]),
824b529e40SToomas Soome };
834b529e40SToomas Soome static unsigned int glyph_total, glyph_count[4], glyph_unique, glyph_dupe;
844b529e40SToomas Soome 
854b529e40SToomas Soome struct mapping {
864b529e40SToomas Soome 	TAILQ_ENTRY(mapping)	 m_list;
874b529e40SToomas Soome 	unsigned int		 m_char;
884b529e40SToomas Soome 	unsigned int		 m_length;
894b529e40SToomas Soome 	struct glyph		*m_glyph;
904b529e40SToomas Soome };
914b529e40SToomas Soome 
924b529e40SToomas Soome TAILQ_HEAD(mapping_list, mapping);
934b529e40SToomas Soome static struct mapping_list maps[VFNT_MAPS] = {
944b529e40SToomas Soome     TAILQ_HEAD_INITIALIZER(maps[0]),
954b529e40SToomas Soome     TAILQ_HEAD_INITIALIZER(maps[1]),
964b529e40SToomas Soome     TAILQ_HEAD_INITIALIZER(maps[2]),
974b529e40SToomas Soome     TAILQ_HEAD_INITIALIZER(maps[3]),
984b529e40SToomas Soome };
994b529e40SToomas Soome static unsigned int mapping_total, map_count[4], map_folded_count[4],
1004b529e40SToomas Soome     mapping_unique, mapping_dupe;
1014b529e40SToomas Soome 
1024b529e40SToomas Soome enum output_format {
1034b529e40SToomas Soome 	VT_FONT,		/* default */
1044b529e40SToomas Soome 	VT_C_SOURCE,		/* C source for built in fonts */
1054b529e40SToomas Soome 	VT_C_COMPRESSED		/* C source with compressed font data */
1064b529e40SToomas Soome };
1074b529e40SToomas Soome 
1084b529e40SToomas Soome struct whitelist {
1094b529e40SToomas Soome 	uint32_t c;
1104b529e40SToomas Soome 	uint32_t len;
1114b529e40SToomas Soome };
1124b529e40SToomas Soome 
1134b529e40SToomas Soome /*
1144b529e40SToomas Soome  * Compressed font glyph list. To be used with boot loader, we need to have
1154b529e40SToomas Soome  * ascii set and box drawing chars.
1164b529e40SToomas Soome  */
1174b529e40SToomas Soome static struct whitelist c_list[] = {
1184b529e40SToomas Soome 	{ .c = 0, .len = 0 },		/* deault char */
1194b529e40SToomas Soome 	{ .c = 0x20, .len = 0x5f },
1204b529e40SToomas Soome 	{ .c = 0x2500, .len = 0 },	/* single frame */
1214b529e40SToomas Soome 	{ .c = 0x2502, .len = 0 },
1224b529e40SToomas Soome 	{ .c = 0x250c, .len = 0 },
1234b529e40SToomas Soome 	{ .c = 0x2510, .len = 0 },
1244b529e40SToomas Soome 	{ .c = 0x2514, .len = 0 },
1254b529e40SToomas Soome 	{ .c = 0x2518, .len = 0 },
1264b529e40SToomas Soome 	{ .c = 0x2550, .len = 1 },	/* double frame */
1274b529e40SToomas Soome 	{ .c = 0x2554, .len = 0 },
1284b529e40SToomas Soome 	{ .c = 0x2557, .len = 0 },
1294b529e40SToomas Soome 	{ .c = 0x255a, .len = 0 },
1304b529e40SToomas Soome 	{ .c = 0x255d, .len = 0 },
1314b529e40SToomas Soome };
1324b529e40SToomas Soome 
1334b529e40SToomas Soome /*
1344b529e40SToomas Soome  * Uncompressed source. For x86 we need cp437 so the vga text mode
1354b529e40SToomas Soome  * can program font into the vga card.
1364b529e40SToomas Soome  */
1374b529e40SToomas Soome static struct whitelist s_list[] = {
1384b529e40SToomas Soome 	{ .c = 0, .len = 0 },		/* deault char */
1394b529e40SToomas Soome 	{ .c = 0x20, .len = 0x5f },	/* ascii set */
1404b529e40SToomas Soome 	{ .c = 0xA0, .len = 0x5f },	/* latin 1 */
1414b529e40SToomas Soome 	{ .c = 0x0192, .len = 0 },
1424b529e40SToomas Soome 	{ .c = 0x0332, .len = 0 },	/* composing lower line */
1434b529e40SToomas Soome 	{ .c = 0x0393, .len = 0 },
1444b529e40SToomas Soome 	{ .c = 0x0398, .len = 0 },
1454b529e40SToomas Soome 	{ .c = 0x03A3, .len = 0 },
1464b529e40SToomas Soome 	{ .c = 0x03A6, .len = 0 },
1474b529e40SToomas Soome 	{ .c = 0x03A9, .len = 0 },
1484b529e40SToomas Soome 	{ .c = 0x03B1, .len = 1 },
1494b529e40SToomas Soome 	{ .c = 0x03B4, .len = 0 },
1504b529e40SToomas Soome 	{ .c = 0x03C0, .len = 0 },
1514b529e40SToomas Soome 	{ .c = 0x03C3, .len = 0 },
1524b529e40SToomas Soome 	{ .c = 0x03C4, .len = 0 },
1534b529e40SToomas Soome 	{ .c = 0x207F, .len = 0 },
1544b529e40SToomas Soome 	{ .c = 0x20A7, .len = 0 },
1554b529e40SToomas Soome 	{ .c = 0x2205, .len = 0 },
1564b529e40SToomas Soome 	{ .c = 0x220A, .len = 0 },
1574b529e40SToomas Soome 	{ .c = 0x2219, .len = 1 },
1584b529e40SToomas Soome 	{ .c = 0x221E, .len = 0 },
1594b529e40SToomas Soome 	{ .c = 0x2229, .len = 0 },
1604b529e40SToomas Soome 	{ .c = 0x2248, .len = 0 },
1614b529e40SToomas Soome 	{ .c = 0x2261, .len = 0 },
1624b529e40SToomas Soome 	{ .c = 0x2264, .len = 1 },
1634b529e40SToomas Soome 	{ .c = 0x2310, .len = 0 },
1644b529e40SToomas Soome 	{ .c = 0x2320, .len = 1 },
1654b529e40SToomas Soome 	{ .c = 0x2500, .len = 0 },
1664b529e40SToomas Soome 	{ .c = 0x2502, .len = 0 },
1674b529e40SToomas Soome 	{ .c = 0x250C, .len = 0 },
1684b529e40SToomas Soome 	{ .c = 0x2510, .len = 0 },
1694b529e40SToomas Soome 	{ .c = 0x2514, .len = 0 },
1704b529e40SToomas Soome 	{ .c = 0x2518, .len = 0 },
1714b529e40SToomas Soome 	{ .c = 0x251C, .len = 0 },
1724b529e40SToomas Soome 	{ .c = 0x2524, .len = 0 },
1734b529e40SToomas Soome 	{ .c = 0x252C, .len = 0 },
1744b529e40SToomas Soome 	{ .c = 0x2534, .len = 0 },
1754b529e40SToomas Soome 	{ .c = 0x253C, .len = 0 },
1764b529e40SToomas Soome 	{ .c = 0x2550, .len = 0x1c },
1774b529e40SToomas Soome 	{ .c = 0x2580, .len = 0 },
1784b529e40SToomas Soome 	{ .c = 0x2584, .len = 0 },
1794b529e40SToomas Soome 	{ .c = 0x2588, .len = 0 },
1804b529e40SToomas Soome 	{ .c = 0x258C, .len = 0 },
1814b529e40SToomas Soome 	{ .c = 0x2590, .len = 3 },
1824b529e40SToomas Soome 	{ .c = 0x25A0, .len = 0 },
1834b529e40SToomas Soome };
1844b529e40SToomas Soome 
1854b529e40SToomas Soome bool filter = true;
1864b529e40SToomas Soome enum output_format format = VT_FONT;
1874b529e40SToomas Soome /* Type for write callback. */
1884b529e40SToomas Soome typedef size_t (*vt_write)(const void *, size_t, size_t, FILE *);
1894b529e40SToomas Soome uint8_t *uncompressed;
1904b529e40SToomas Soome 
1914b529e40SToomas Soome static void
1924b529e40SToomas Soome usage(void)
1934b529e40SToomas Soome {
1944b529e40SToomas Soome 
1954b529e40SToomas Soome 	(void) fprintf(stderr, "usage:\tvtfontcvt "
1964b529e40SToomas Soome 	    "[-n] [-f font|source|compressed-source] [-w width] "
1974b529e40SToomas Soome 	    "[-h height]\n\t[-v] -o outfile normal.bdf [bold.bdf]\n");
1984b529e40SToomas Soome 	exit(1);
1994b529e40SToomas Soome }
2004b529e40SToomas Soome 
2014b529e40SToomas Soome static void *
2024b529e40SToomas Soome xmalloc(size_t size)
2034b529e40SToomas Soome {
2044b529e40SToomas Soome 	void *m;
2054b529e40SToomas Soome 
2064b529e40SToomas Soome 	if ((m = malloc(size)) == NULL)
2074b529e40SToomas Soome 		errx(1, "memory allocation failure");
2084b529e40SToomas Soome 	return (m);
2094b529e40SToomas Soome }
2104b529e40SToomas Soome 
2114b529e40SToomas Soome static int
2124b529e40SToomas Soome add_mapping(struct glyph *gl, unsigned int c, unsigned int map_idx)
2134b529e40SToomas Soome {
2144b529e40SToomas Soome 	struct mapping *mp, *v;
2154b529e40SToomas Soome 	struct mapping_list *ml;
2164b529e40SToomas Soome 
2174b529e40SToomas Soome 	mapping_total++;
2184b529e40SToomas Soome 
2194b529e40SToomas Soome 	mp = xmalloc(sizeof (*mp));
2204b529e40SToomas Soome 	mp->m_char = c;
2214b529e40SToomas Soome 	mp->m_glyph = gl;
2224b529e40SToomas Soome 	mp->m_length = 0;
2234b529e40SToomas Soome 
2244b529e40SToomas Soome 	ml = &maps[map_idx];
2254b529e40SToomas Soome 	if (TAILQ_LAST(ml, mapping_list) != NULL &&
2264b529e40SToomas Soome 	    TAILQ_LAST(ml, mapping_list)->m_char >= c) {
2274b529e40SToomas Soome 		TAILQ_FOREACH_REVERSE(v, ml, mapping_list, m_list) {
2284b529e40SToomas Soome 			if (v->m_char < c) {
2294b529e40SToomas Soome 				TAILQ_INSERT_AFTER(ml, v, mp, m_list);
2304b529e40SToomas Soome 				break;
2314b529e40SToomas Soome 			} else if (v->m_char == c)
2324b529e40SToomas Soome 				errx(1, "Bad ordering at character %u", c);
2334b529e40SToomas Soome 		}
2344b529e40SToomas Soome 	} else
2354b529e40SToomas Soome 		TAILQ_INSERT_TAIL(ml, mp, m_list);
2364b529e40SToomas Soome 
2374b529e40SToomas Soome 	map_count[map_idx]++;
2384b529e40SToomas Soome 	mapping_unique++;
2394b529e40SToomas Soome 
2404b529e40SToomas Soome 	return (0);
2414b529e40SToomas Soome }
2424b529e40SToomas Soome 
2434b529e40SToomas Soome static int
2444b529e40SToomas Soome dedup_mapping(unsigned int map_idx)
2454b529e40SToomas Soome {
246*ebe61a78SToomas Soome 	struct mapping *tmp, *mp_bold, *mp_normal;
2474b529e40SToomas Soome 	unsigned normal_map_idx = map_idx - VFNT_MAP_BOLD;
2484b529e40SToomas Soome 
2494b529e40SToomas Soome 	assert(map_idx == VFNT_MAP_BOLD || map_idx == VFNT_MAP_BOLD_RH);
2504b529e40SToomas Soome 	mp_normal = TAILQ_FIRST(&maps[normal_map_idx]);
251*ebe61a78SToomas Soome 	TAILQ_FOREACH_SAFE(mp_bold, &maps[map_idx], m_list, tmp) {
2524b529e40SToomas Soome 		while (mp_normal->m_char < mp_bold->m_char)
2534b529e40SToomas Soome 			mp_normal = TAILQ_NEXT(mp_normal, m_list);
2544b529e40SToomas Soome 		if (mp_bold->m_char != mp_normal->m_char)
2554b529e40SToomas Soome 			errx(1, "Character %u not in normal font!",
2564b529e40SToomas Soome 			    mp_bold->m_char);
2574b529e40SToomas Soome 		if (mp_bold->m_glyph != mp_normal->m_glyph)
2584b529e40SToomas Soome 			continue;
2594b529e40SToomas Soome 
2604b529e40SToomas Soome 		/* No mapping is needed if it's equal to the normal mapping. */
2614b529e40SToomas Soome 		TAILQ_REMOVE(&maps[map_idx], mp_bold, m_list);
2624b529e40SToomas Soome 		free(mp_bold);
2634b529e40SToomas Soome 		mapping_dupe++;
2644b529e40SToomas Soome 	}
2654b529e40SToomas Soome 	return (0);
2664b529e40SToomas Soome }
2674b529e40SToomas Soome 
2684b529e40SToomas Soome static struct glyph *
2694b529e40SToomas Soome add_glyph(const uint8_t *bytes, unsigned int map_idx, int fallback)
2704b529e40SToomas Soome {
2714b529e40SToomas Soome 	struct glyph *gl;
2724b529e40SToomas Soome 	int hash;
2734b529e40SToomas Soome 
2744b529e40SToomas Soome 	glyph_total++;
2754b529e40SToomas Soome 	glyph_count[map_idx]++;
2764b529e40SToomas Soome 
2774b529e40SToomas Soome 	hash = fnv_32_buf(bytes, wbytes * height, FNV1_32_INIT) % FONTCVT_NHASH;
2784b529e40SToomas Soome 	SLIST_FOREACH(gl, &glyph_hash[hash], g_hash) {
2794b529e40SToomas Soome 		if (memcmp(gl->g_data, bytes, wbytes * height) == 0) {
2804b529e40SToomas Soome 			glyph_dupe++;
2814b529e40SToomas Soome 			return (gl);
2824b529e40SToomas Soome 		}
2834b529e40SToomas Soome 	}
2844b529e40SToomas Soome 
2854b529e40SToomas Soome 	gl = xmalloc(sizeof (*gl));
2864b529e40SToomas Soome 	gl->g_data = xmalloc(wbytes * height);
2874b529e40SToomas Soome 	memcpy(gl->g_data, bytes, wbytes * height);
2884b529e40SToomas Soome 	if (fallback)
2894b529e40SToomas Soome 		TAILQ_INSERT_HEAD(&glyphs[map_idx], gl, g_list);
2904b529e40SToomas Soome 	else
2914b529e40SToomas Soome 		TAILQ_INSERT_TAIL(&glyphs[map_idx], gl, g_list);
2924b529e40SToomas Soome 	SLIST_INSERT_HEAD(&glyph_hash[hash], gl, g_hash);
2934b529e40SToomas Soome 
2944b529e40SToomas Soome 	glyph_unique++;
2954b529e40SToomas Soome 	return (gl);
2964b529e40SToomas Soome }
2974b529e40SToomas Soome 
2984b529e40SToomas Soome static bool
2994b529e40SToomas Soome check_whitelist(unsigned c)
3004b529e40SToomas Soome {
3014b529e40SToomas Soome 	struct whitelist *w = NULL;
3024b529e40SToomas Soome 	int i, n = 0;
3034b529e40SToomas Soome 
3044b529e40SToomas Soome 	if (filter == false)
3054b529e40SToomas Soome 		return (true);
3064b529e40SToomas Soome 
3074b529e40SToomas Soome 	if (format == VT_C_SOURCE) {
3084b529e40SToomas Soome 		w = s_list;
3094b529e40SToomas Soome 		n = sizeof (s_list) / sizeof (s_list[0]);
3104b529e40SToomas Soome 	}
3114b529e40SToomas Soome 	if (format == VT_C_COMPRESSED) {
3124b529e40SToomas Soome 		w = c_list;
3134b529e40SToomas Soome 		n = sizeof (c_list) / sizeof (c_list[0]);
3144b529e40SToomas Soome 	}
3154b529e40SToomas Soome 	if (w == NULL)
3164b529e40SToomas Soome 		return (true);
3174b529e40SToomas Soome 	for (i = 0; i < n; i++) {
3184b529e40SToomas Soome 		if (c >= w[i].c && c <= w[i].c + w[i].len)
3194b529e40SToomas Soome 			return (true);
3204b529e40SToomas Soome 	}
3214b529e40SToomas Soome 	return (false);
3224b529e40SToomas Soome }
3234b529e40SToomas Soome 
3244b529e40SToomas Soome static int
3254b529e40SToomas Soome add_char(unsigned curchar, unsigned map_idx, uint8_t *bytes, uint8_t *bytes_r)
3264b529e40SToomas Soome {
3274b529e40SToomas Soome 	struct glyph *gl;
3284b529e40SToomas Soome 
3294b529e40SToomas Soome 	/* Prevent adding two glyphs for default_char */
3304b529e40SToomas Soome 	if (curchar == default_char) {
3314b529e40SToomas Soome 		if (map_idx < VFNT_MAP_BOLD)
3324b529e40SToomas Soome 			gl = add_glyph(bytes, 0, 1);
3334b529e40SToomas Soome 	} else if (filter == false || curchar >= 0x20) {
3344b529e40SToomas Soome 		gl = add_glyph(bytes, map_idx, 0);
3354b529e40SToomas Soome 		if (add_mapping(gl, curchar, map_idx) != 0)
3364b529e40SToomas Soome 			return (1);
3374b529e40SToomas Soome 		if (bytes_r != NULL) {
3384b529e40SToomas Soome 			gl = add_glyph(bytes_r, map_idx + 1, 0);
3394b529e40SToomas Soome 			if (add_mapping(gl, curchar,
3404b529e40SToomas Soome 			    map_idx + 1) != 0)
3414b529e40SToomas Soome 				return (1);
3424b529e40SToomas Soome 		}
3434b529e40SToomas Soome 	}
3444b529e40SToomas Soome 	return (0);
3454b529e40SToomas Soome }
3464b529e40SToomas Soome 
3474b529e40SToomas Soome 
3484b529e40SToomas Soome static int
3494b529e40SToomas Soome parse_bitmap_line(uint8_t *left, uint8_t *right, unsigned int line,
3504b529e40SToomas Soome     unsigned int dwidth)
3514b529e40SToomas Soome {
3524b529e40SToomas Soome 	uint8_t *p;
3534b529e40SToomas Soome 	unsigned int i, subline;
3544b529e40SToomas Soome 
3554b529e40SToomas Soome 	if (dwidth != width && dwidth != width * 2)
3564b529e40SToomas Soome 		errx(1, "Bitmap with unsupported width %u!", dwidth);
3574b529e40SToomas Soome 
3584b529e40SToomas Soome 	/* Move pixel data right to simplify splitting double characters. */
3594b529e40SToomas Soome 	line >>= (howmany(dwidth, 8) * 8) - dwidth;
3604b529e40SToomas Soome 
3614b529e40SToomas Soome 	for (i = dwidth / width; i > 0; i--) {
3624b529e40SToomas Soome 		p = (i == 2) ? right : left;
3634b529e40SToomas Soome 
3644b529e40SToomas Soome 		subline = line & ((1 << width) - 1);
3654b529e40SToomas Soome 		subline <<= (howmany(width, 8) * 8) - width;
3664b529e40SToomas Soome 
3674b529e40SToomas Soome 		if (wbytes == 1) {
3684b529e40SToomas Soome 			*p = subline;
3694b529e40SToomas Soome 		} else if (wbytes == 2) {
3704b529e40SToomas Soome 			*p++ = subline >> 8;
3714b529e40SToomas Soome 			*p = subline;
3724b529e40SToomas Soome 		} else {
3734b529e40SToomas Soome 			errx(1, "Unsupported wbytes %u!", wbytes);
3744b529e40SToomas Soome 		}
3754b529e40SToomas Soome 
3764b529e40SToomas Soome 		line >>= width;
3774b529e40SToomas Soome 	}
3784b529e40SToomas Soome 
3794b529e40SToomas Soome 	return (0);
3804b529e40SToomas Soome }
3814b529e40SToomas Soome 
3824b529e40SToomas Soome static int
3834b529e40SToomas Soome parse_bdf(FILE *fp, unsigned int map_idx)
3844b529e40SToomas Soome {
3854b529e40SToomas Soome 	char ln[BUFSIZ];
3864b529e40SToomas Soome 	uint8_t bytes[wbytes * height], bytes_r[wbytes * height];
3874b529e40SToomas Soome 	unsigned int curchar = 0, dwidth = 0, i, line;
3884b529e40SToomas Soome 
3894b529e40SToomas Soome 	memset(bytes, 0, sizeof (bytes));
3904b529e40SToomas Soome 	memset(bytes_r, 0, sizeof (bytes_r));
3914b529e40SToomas Soome 
3924b529e40SToomas Soome 	while (fgets(ln, sizeof (ln), fp) != NULL) {
3934b529e40SToomas Soome 		if (sscanf(ln, "ENCODING %u", &curchar) == 1)
3944b529e40SToomas Soome 			continue;
3954b529e40SToomas Soome 
3964b529e40SToomas Soome 		if (sscanf(ln, "DWIDTH %u", &dwidth) == 1)
3974b529e40SToomas Soome 			continue;
3984b529e40SToomas Soome 
3994b529e40SToomas Soome 		if (strncmp(ln, "BITMAP", 6) == 0) {
4004b529e40SToomas Soome 			for (i = 0; i < height; i++) {
4014b529e40SToomas Soome 				if (fgets(ln, sizeof (ln), fp) == NULL)
4024b529e40SToomas Soome 					errx(1, "Unexpected EOF!");
4034b529e40SToomas Soome 				sscanf(ln, "%x", &line);
4044b529e40SToomas Soome 				if (parse_bitmap_line(bytes + i * wbytes,
4054b529e40SToomas Soome 				    bytes_r + i * wbytes, line, dwidth) != 0)
4064b529e40SToomas Soome 					return (1);
4074b529e40SToomas Soome 			}
4084b529e40SToomas Soome 
4094b529e40SToomas Soome 			if (check_whitelist(curchar) == true) {
4104b529e40SToomas Soome 				if (add_char(curchar, map_idx, bytes,
4114b529e40SToomas Soome 				    dwidth == width * 2 ? bytes_r : NULL) != 0)
4124b529e40SToomas Soome 					return (1);
4134b529e40SToomas Soome 			}
4144b529e40SToomas Soome 		}
4154b529e40SToomas Soome 	}
4164b529e40SToomas Soome 
4174b529e40SToomas Soome 	return (0);
4184b529e40SToomas Soome }
4194b529e40SToomas Soome 
4204b529e40SToomas Soome static void
4214b529e40SToomas Soome set_width(int w)
4224b529e40SToomas Soome {
4234b529e40SToomas Soome 
4244b529e40SToomas Soome 	if (w <= 0 || w > 128)
4254b529e40SToomas Soome 		errx(1, "invalid width %d", w);
4264b529e40SToomas Soome 	width = w;
4274b529e40SToomas Soome 	wbytes = howmany(width, 8);
4284b529e40SToomas Soome }
4294b529e40SToomas Soome 
4304b529e40SToomas Soome static int
4314b529e40SToomas Soome parse_hex(FILE *fp, unsigned int map_idx)
4324b529e40SToomas Soome {
4334b529e40SToomas Soome 	char ln[BUFSIZ], *p;
4344b529e40SToomas Soome 	char fmt_str[8];
4354b529e40SToomas Soome 	uint8_t *bytes = NULL, *bytes_r = NULL;
4364b529e40SToomas Soome 	unsigned curchar = 0, i, line, chars_per_row, dwidth;
4374b529e40SToomas Soome 	int rv = 0;
4384b529e40SToomas Soome 
4394b529e40SToomas Soome 	while (fgets(ln, sizeof (ln), fp) != NULL) {
4404b529e40SToomas Soome 		if (strncmp(ln, "# Height: ", 10) == 0) {
4414b529e40SToomas Soome 			if (bytes != NULL) {
4424b529e40SToomas Soome 				errx(1, "malformed input: Height tag after "
4434b529e40SToomas Soome 				    "font data");
4444b529e40SToomas Soome 			}
4454b529e40SToomas Soome 			height = atoi(ln + 10);
4464b529e40SToomas Soome 		} else if (strncmp(ln, "# Width: ", 9) == 0) {
4474b529e40SToomas Soome 			if (bytes != NULL) {
4484b529e40SToomas Soome 				errx(1, "malformed input: Width tag after "
4494b529e40SToomas Soome 				    "font data");
4504b529e40SToomas Soome 			}
4514b529e40SToomas Soome 			set_width(atoi(ln + 9));
4524b529e40SToomas Soome 		} else if (sscanf(ln, "%6x:", &curchar)) {
4534b529e40SToomas Soome 			if (bytes == NULL) {
4544b529e40SToomas Soome 				bytes = xmalloc(wbytes * height);
4554b529e40SToomas Soome 				bytes_r = xmalloc(wbytes * height);
4564b529e40SToomas Soome 			}
4574b529e40SToomas Soome 			/* ln is guaranteed to have a colon here. */
4584b529e40SToomas Soome 			p = strchr(ln, ':') + 1;
4594b529e40SToomas Soome 			chars_per_row = strlen(p) / height;
4604b529e40SToomas Soome 			dwidth = width;
4614b529e40SToomas Soome 			if (chars_per_row / 2 > (width + 7) / 8)
4624b529e40SToomas Soome 				dwidth *= 2; /* Double-width character. */
4634b529e40SToomas Soome 			snprintf(fmt_str, sizeof (fmt_str), "%%%ux",
4644b529e40SToomas Soome 			    chars_per_row);
4654b529e40SToomas Soome 
4664b529e40SToomas Soome 			for (i = 0; i < height; i++) {
4674b529e40SToomas Soome 				sscanf(p, fmt_str, &line);
4684b529e40SToomas Soome 				p += chars_per_row;
4694b529e40SToomas Soome 				if (parse_bitmap_line(bytes + i * wbytes,
4704b529e40SToomas Soome 				    bytes_r + i * wbytes, line, dwidth) != 0) {
4714b529e40SToomas Soome 					rv = 1;
4724b529e40SToomas Soome 					goto out;
4734b529e40SToomas Soome 				}
4744b529e40SToomas Soome 			}
4754b529e40SToomas Soome 
4764b529e40SToomas Soome 			if (add_char(curchar, map_idx, bytes,
4774b529e40SToomas Soome 			    dwidth == width * 2 ? bytes_r : NULL) != 0) {
4784b529e40SToomas Soome 				rv = 1;
4794b529e40SToomas Soome 				goto out;
4804b529e40SToomas Soome 			}
4814b529e40SToomas Soome 		}
4824b529e40SToomas Soome 	}
4834b529e40SToomas Soome out:
4844b529e40SToomas Soome 	free(bytes);
4854b529e40SToomas Soome 	free(bytes_r);
4864b529e40SToomas Soome 	return (rv);
4874b529e40SToomas Soome }
4884b529e40SToomas Soome 
4894b529e40SToomas Soome /* Read BDF header and set the font data. */
4904b529e40SToomas Soome static int
4914b529e40SToomas Soome parse_bdf_header(FILE *fp)
4924b529e40SToomas Soome {
4934b529e40SToomas Soome 	char ln[BUFSIZ];
4944b529e40SToomas Soome 	char spacing = '\0';	/* Should we assume C if not specified? */
4954b529e40SToomas Soome 	int ret;
4964b529e40SToomas Soome 
4974b529e40SToomas Soome 	while (fgets(ln, sizeof (ln), fp) != NULL) {
4984b529e40SToomas Soome 		ret = sscanf(ln, "FONTBOUNDINGBOX %u %u %d %d",
4994b529e40SToomas Soome 		    &bbox.width, &bbox.height, &bbox.x, &bbox.y);
5004b529e40SToomas Soome 		if (ret == 4)
5014b529e40SToomas Soome 			continue;
5024b529e40SToomas Soome 		ret = sscanf(ln, "FONT_ASCENT %u", &font_ascent);
5034b529e40SToomas Soome 		if (ret == 1)
5044b529e40SToomas Soome 			continue;
5054b529e40SToomas Soome 		ret = sscanf(ln, "FONT_DESCENT %u", &font_descent);
5064b529e40SToomas Soome 		if (ret == 1)
5074b529e40SToomas Soome 			continue;
5084b529e40SToomas Soome 		ret = sscanf(ln, "DEFAULT_CHAR %u", &default_char);
5094b529e40SToomas Soome 		if (ret == 1) {
5104b529e40SToomas Soome 			c_list[0].c = default_char;
5114b529e40SToomas Soome 			s_list[0].c = default_char;
5124b529e40SToomas Soome 			continue;
5134b529e40SToomas Soome 		}
5144b529e40SToomas Soome 		ret = sscanf(ln, "SPACING \"%c\"", &spacing);
5154b529e40SToomas Soome 		if (ret == 1)
5164b529e40SToomas Soome 			continue;
5174b529e40SToomas Soome 		if (strncmp("ENDPROPERTIES", ln, 13) == 0)
5184b529e40SToomas Soome 			break;
5194b529e40SToomas Soome 	}
5204b529e40SToomas Soome 	if (spacing != 'C') {
5214b529e40SToomas Soome 		printf("Spacing '%c' is not supported\n", spacing);
5224b529e40SToomas Soome 		return (1);
5234b529e40SToomas Soome 	}
5244b529e40SToomas Soome 	if (bbox.width == 0)
5254b529e40SToomas Soome 		bbox.width = width;
5264b529e40SToomas Soome 	set_width(bbox.width);
5274b529e40SToomas Soome 
5284b529e40SToomas Soome 	if (bbox.height == 0)
5294b529e40SToomas Soome 		bbox.height = height;
5304b529e40SToomas Soome 	height = bbox.height;
5314b529e40SToomas Soome 	return (0);
5324b529e40SToomas Soome }
5334b529e40SToomas Soome 
5344b529e40SToomas Soome static int
5354b529e40SToomas Soome parse_file(const char *filename, unsigned int map_idx)
5364b529e40SToomas Soome {
5374b529e40SToomas Soome 	FILE *fp;
5384b529e40SToomas Soome 	size_t len;
5394b529e40SToomas Soome 	int rv;
5404b529e40SToomas Soome 
5414b529e40SToomas Soome 	fp = fopen(filename, "r");
5424b529e40SToomas Soome 	if (fp == NULL) {
5434b529e40SToomas Soome 		perror(filename);
5444b529e40SToomas Soome 		return (1);
5454b529e40SToomas Soome 	}
5464b529e40SToomas Soome 	len = strlen(filename);
5474b529e40SToomas Soome 	if (len > 4 && strcasecmp(filename + len - 4, ".hex") == 0) {
5484b529e40SToomas Soome 		rv = parse_hex(fp, map_idx);
5494b529e40SToomas Soome 	} else {
5504b529e40SToomas Soome 		if ((rv = parse_bdf_header(fp)) == 0)
5514b529e40SToomas Soome 			rv = parse_bdf(fp, map_idx);
5524b529e40SToomas Soome 	}
5534b529e40SToomas Soome 	fclose(fp);
5544b529e40SToomas Soome 	return (rv);
5554b529e40SToomas Soome }
5564b529e40SToomas Soome 
5574b529e40SToomas Soome static void
5584b529e40SToomas Soome number_glyphs(void)
5594b529e40SToomas Soome {
5604b529e40SToomas Soome 	struct glyph *gl;
5614b529e40SToomas Soome 	unsigned int i, idx = 0;
5624b529e40SToomas Soome 
5634b529e40SToomas Soome 	for (i = 0; i < VFNT_MAPS; i++)
5644b529e40SToomas Soome 		TAILQ_FOREACH(gl, &glyphs[i], g_list)
5654b529e40SToomas Soome 			gl->g_index = idx++;
5664b529e40SToomas Soome }
5674b529e40SToomas Soome 
5684b529e40SToomas Soome /* Note we only deal with byte stream here. */
5694b529e40SToomas Soome static size_t
5704b529e40SToomas Soome write_glyph_source(const void *ptr, size_t size, size_t nitems, FILE *stream)
5714b529e40SToomas Soome {
5724b529e40SToomas Soome 	const uint8_t *data = ptr;
5734b529e40SToomas Soome 	size_t i;
5744b529e40SToomas Soome 
5754b529e40SToomas Soome 	size *= nitems;
5764b529e40SToomas Soome 	for (i = 0; i < size; i++) {
5774b529e40SToomas Soome 		if ((i % wbytes) == 0) {
5784b529e40SToomas Soome 			if (fprintf(stream, "\n") < 0)
5794b529e40SToomas Soome 				return (0);
5804b529e40SToomas Soome 		}
5814b529e40SToomas Soome 		if (fprintf(stream, "0x%02x, ", data[i]) < 0)
5824b529e40SToomas Soome 			return (0);
5834b529e40SToomas Soome 	}
5844b529e40SToomas Soome 	if (fprintf(stream, "\n") < 0)
5854b529e40SToomas Soome 		nitems = 0;
5864b529e40SToomas Soome 
5874b529e40SToomas Soome 	return (nitems);
5884b529e40SToomas Soome }
5894b529e40SToomas Soome 
5904b529e40SToomas Soome /* Write to buffer */
5914b529e40SToomas Soome static size_t
5924b529e40SToomas Soome write_glyph_buf(const void *ptr, size_t size, size_t nitems, FILE *stream)
5934b529e40SToomas Soome {
5944b529e40SToomas Soome 	static size_t index = 0;
5954b529e40SToomas Soome 
5964b529e40SToomas Soome 	size *= nitems;
5974b529e40SToomas Soome 	(void) memmove(uncompressed + index, ptr, size);
5984b529e40SToomas Soome 	index += size;
5994b529e40SToomas Soome 
6004b529e40SToomas Soome 	return (nitems);
6014b529e40SToomas Soome }
6024b529e40SToomas Soome 
6034b529e40SToomas Soome static int
6044b529e40SToomas Soome write_glyphs(FILE *fp, vt_write cb)
6054b529e40SToomas Soome {
6064b529e40SToomas Soome 	struct glyph *gl;
6074b529e40SToomas Soome 	unsigned int i;
6084b529e40SToomas Soome 
6094b529e40SToomas Soome 	for (i = 0; i < VFNT_MAPS; i++) {
6104b529e40SToomas Soome 		TAILQ_FOREACH(gl, &glyphs[i], g_list)
6114b529e40SToomas Soome 			if (cb(gl->g_data, wbytes * height, 1, fp) != 1)
6124b529e40SToomas Soome 				return (1);
6134b529e40SToomas Soome 	}
6144b529e40SToomas Soome 	return (0);
6154b529e40SToomas Soome }
6164b529e40SToomas Soome 
6174b529e40SToomas Soome static void
6184b529e40SToomas Soome fold_mappings(unsigned int map_idx)
6194b529e40SToomas Soome {
6204b529e40SToomas Soome 	struct mapping_list *ml = &maps[map_idx];
6214b529e40SToomas Soome 	struct mapping *mn, *mp, *mbase;
6224b529e40SToomas Soome 
6234b529e40SToomas Soome 	mp = mbase = TAILQ_FIRST(ml);
6244b529e40SToomas Soome 	for (mp = mbase = TAILQ_FIRST(ml); mp != NULL; mp = mn) {
6254b529e40SToomas Soome 		mn = TAILQ_NEXT(mp, m_list);
6264b529e40SToomas Soome 		if (mn != NULL && mn->m_char == mp->m_char + 1 &&
6274b529e40SToomas Soome 		    mn->m_glyph->g_index == mp->m_glyph->g_index + 1)
6284b529e40SToomas Soome 			continue;
6294b529e40SToomas Soome 		mbase->m_length = mp->m_char - mbase->m_char + 1;
6304b529e40SToomas Soome 		mbase = mp = mn;
6314b529e40SToomas Soome 		map_folded_count[map_idx]++;
6324b529e40SToomas Soome 	}
6334b529e40SToomas Soome }
6344b529e40SToomas Soome 
6354b529e40SToomas Soome struct file_mapping {
6364b529e40SToomas Soome 	uint32_t	source;
6374b529e40SToomas Soome 	uint16_t	destination;
6384b529e40SToomas Soome 	uint16_t	length;
6394b529e40SToomas Soome } __attribute__((packed));
6404b529e40SToomas Soome 
6414b529e40SToomas Soome static int
6424b529e40SToomas Soome write_mappings(FILE *fp, unsigned int map_idx)
6434b529e40SToomas Soome {
6444b529e40SToomas Soome 	struct mapping_list *ml = &maps[map_idx];
6454b529e40SToomas Soome 	struct mapping *mp;
6464b529e40SToomas Soome 	struct file_mapping fm;
6474b529e40SToomas Soome 	unsigned int i = 0, j = 0;
6484b529e40SToomas Soome 
6494b529e40SToomas Soome 	TAILQ_FOREACH(mp, ml, m_list) {
6504b529e40SToomas Soome 		j++;
6514b529e40SToomas Soome 		if (mp->m_length > 0) {
6524b529e40SToomas Soome 			i += mp->m_length;
6534b529e40SToomas Soome 			fm.source = htobe32(mp->m_char);
6544b529e40SToomas Soome 			fm.destination = htobe16(mp->m_glyph->g_index);
6554b529e40SToomas Soome 			fm.length = htobe16(mp->m_length - 1);
6564b529e40SToomas Soome 			if (fwrite(&fm, sizeof (fm), 1, fp) != 1)
6574b529e40SToomas Soome 				return (1);
6584b529e40SToomas Soome 		}
6594b529e40SToomas Soome 	}
6604b529e40SToomas Soome 	assert(i == j);
6614b529e40SToomas Soome 	return (0);
6624b529e40SToomas Soome }
6634b529e40SToomas Soome 
6644b529e40SToomas Soome static int
6654b529e40SToomas Soome write_source_mappings(FILE *fp, unsigned int map_idx)
6664b529e40SToomas Soome {
6674b529e40SToomas Soome 	struct mapping_list *ml = &maps[map_idx];
6684b529e40SToomas Soome 	struct mapping *mp;
6694b529e40SToomas Soome 	unsigned int i = 0, j = 0;
6704b529e40SToomas Soome 
6714b529e40SToomas Soome 	TAILQ_FOREACH(mp, ml, m_list) {
6724b529e40SToomas Soome 		j++;
6734b529e40SToomas Soome 		if (mp->m_length > 0) {
6744b529e40SToomas Soome 			i += mp->m_length;
6754b529e40SToomas Soome 			if (fprintf(fp, "\t{ 0x%08x, 0x%04x, 0x%04x },\n",
6764b529e40SToomas Soome 			    mp->m_char, mp->m_glyph->g_index,
6774b529e40SToomas Soome 			    mp->m_length - 1) < 0)
6784b529e40SToomas Soome 				return (1);
6794b529e40SToomas Soome 		}
6804b529e40SToomas Soome 	}
6814b529e40SToomas Soome 	assert(i == j);
6824b529e40SToomas Soome 	return (0);
6834b529e40SToomas Soome }
6844b529e40SToomas Soome 
6854b529e40SToomas Soome struct file_header {
6864b529e40SToomas Soome 	uint8_t		magic[8];
6874b529e40SToomas Soome 	uint8_t		width;
6884b529e40SToomas Soome 	uint8_t		height;
6894b529e40SToomas Soome 	uint16_t	pad;
6904b529e40SToomas Soome 	uint32_t	glyph_count;
6914b529e40SToomas Soome 	uint32_t	map_count[4];
6924b529e40SToomas Soome } __attribute__((packed));
6934b529e40SToomas Soome 
6944b529e40SToomas Soome static int
6954b529e40SToomas Soome write_fnt(const char *filename)
6964b529e40SToomas Soome {
6974b529e40SToomas Soome 	FILE *fp;
6984b529e40SToomas Soome 	struct file_header fh = {
6994b529e40SToomas Soome 		.magic = "VFNT0002",
7004b529e40SToomas Soome 	};
7014b529e40SToomas Soome 
7024b529e40SToomas Soome 	fp = fopen(filename, "wb");
7034b529e40SToomas Soome 	if (fp == NULL) {
7044b529e40SToomas Soome 		perror(filename);
7054b529e40SToomas Soome 		return (1);
7064b529e40SToomas Soome 	}
7074b529e40SToomas Soome 
7084b529e40SToomas Soome 	fh.width = width;
7094b529e40SToomas Soome 	fh.height = height;
7104b529e40SToomas Soome 	fh.glyph_count = htobe32(glyph_unique);
7114b529e40SToomas Soome 	fh.map_count[0] = htobe32(map_folded_count[0]);
7124b529e40SToomas Soome 	fh.map_count[1] = htobe32(map_folded_count[1]);
7134b529e40SToomas Soome 	fh.map_count[2] = htobe32(map_folded_count[2]);
7144b529e40SToomas Soome 	fh.map_count[3] = htobe32(map_folded_count[3]);
7154b529e40SToomas Soome 	if (fwrite(&fh, sizeof (fh), 1, fp) != 1) {
7164b529e40SToomas Soome 		perror(filename);
7174b529e40SToomas Soome 		fclose(fp);
7184b529e40SToomas Soome 		return (1);
7194b529e40SToomas Soome 	}
7204b529e40SToomas Soome 
7214b529e40SToomas Soome 	if (write_glyphs(fp, &fwrite) != 0 ||
7224b529e40SToomas Soome 	    write_mappings(fp, VFNT_MAP_NORMAL) != 0 ||
7234b529e40SToomas Soome 	    write_mappings(fp, 1) != 0 ||
7244b529e40SToomas Soome 	    write_mappings(fp, VFNT_MAP_BOLD) != 0 ||
7254b529e40SToomas Soome 	    write_mappings(fp, 3) != 0) {
7264b529e40SToomas Soome 		perror(filename);
7274b529e40SToomas Soome 		fclose(fp);
7284b529e40SToomas Soome 		return (1);
7294b529e40SToomas Soome 	}
7304b529e40SToomas Soome 
7314b529e40SToomas Soome 	fclose(fp);
7324b529e40SToomas Soome 	return (0);
7334b529e40SToomas Soome }
7344b529e40SToomas Soome 
7354b529e40SToomas Soome static int
7364b529e40SToomas Soome write_fnt_source(bool lz4, const char *filename)
7374b529e40SToomas Soome {
7384b529e40SToomas Soome 	FILE *fp;
7394b529e40SToomas Soome 	int rv = 1;
7404b529e40SToomas Soome 	size_t uncompressed_size = wbytes * height * glyph_unique;
7414b529e40SToomas Soome 	size_t compressed_size = uncompressed_size;
7424b529e40SToomas Soome 	uint8_t *compressed = NULL;
7434b529e40SToomas Soome 
7444b529e40SToomas Soome 	fp = fopen(filename, "w");
7454b529e40SToomas Soome 	if (fp == NULL) {
7464b529e40SToomas Soome 		perror(filename);
7474b529e40SToomas Soome 		return (1);
7484b529e40SToomas Soome 	}
7494b529e40SToomas Soome 
7504b529e40SToomas Soome 	if (lz4 == true) {
7514b529e40SToomas Soome 		uncompressed = xmalloc(uncompressed_size);
7524b529e40SToomas Soome 		compressed = xmalloc(uncompressed_size);
7534b529e40SToomas Soome 	}
7544b529e40SToomas Soome 	if (fprintf(fp, "/* Generated %ux%u console font source. */\n\n",
7554b529e40SToomas Soome 	    width, height) < 0)
7564b529e40SToomas Soome 		goto done;
7574b529e40SToomas Soome 	if (fprintf(fp, "#include <sys/types.h>\n") < 0)
7584b529e40SToomas Soome 		goto done;
7594b529e40SToomas Soome 	if (fprintf(fp, "#include <sys/param.h>\n") < 0)
7604b529e40SToomas Soome 		goto done;
7614b529e40SToomas Soome 	if (fprintf(fp, "#include <sys/font.h>\n\n") < 0)
7624b529e40SToomas Soome 		goto done;
7634b529e40SToomas Soome 
7644b529e40SToomas Soome 	/* Write font bytes. */
7654b529e40SToomas Soome 	if (fprintf(fp, "static uint8_t FONTDATA_%ux%u[] = {\n",
7664b529e40SToomas Soome 	    width, height) < 0)
7674b529e40SToomas Soome 		goto done;
7684b529e40SToomas Soome 	if (lz4 == true) {
7694b529e40SToomas Soome 		if (write_glyphs(fp, &write_glyph_buf) != 0)
7704b529e40SToomas Soome 			goto done;
7714b529e40SToomas Soome 		compressed_size = lz4_compress(uncompressed, compressed,
7724b529e40SToomas Soome 		    uncompressed_size, compressed_size, 0);
7734b529e40SToomas Soome 		if (write_glyph_source(compressed, compressed_size, 1, fp) != 1)
7744b529e40SToomas Soome 			goto done;
7754b529e40SToomas Soome 		free(uncompressed);
7764b529e40SToomas Soome 		free(compressed);
7774b529e40SToomas Soome 	} else {
7784b529e40SToomas Soome 		if (write_glyphs(fp, &write_glyph_source) != 0)
7794b529e40SToomas Soome 			goto done;
7804b529e40SToomas Soome 	}
7814b529e40SToomas Soome 	if (fprintf(fp, "};\n\n") < 0)
7824b529e40SToomas Soome 		goto done;
7834b529e40SToomas Soome 
7844b529e40SToomas Soome 	/* Write font maps. */
7854b529e40SToomas Soome 	if (!TAILQ_EMPTY(&maps[VFNT_MAP_NORMAL])) {
7864b529e40SToomas Soome 		if (fprintf(fp, "static struct font_map "
7874b529e40SToomas Soome 		    "FONTMAP_NORMAL_%ux%u[] = {\n", width, height) < 0)
7884b529e40SToomas Soome 			goto done;
7894b529e40SToomas Soome 		if (write_source_mappings(fp, VFNT_MAP_NORMAL) != 0)
7904b529e40SToomas Soome 			goto done;
7914b529e40SToomas Soome 		if (fprintf(fp, "};\n\n") < 0)
7924b529e40SToomas Soome 			goto done;
7934b529e40SToomas Soome 	}
7944b529e40SToomas Soome 	if (!TAILQ_EMPTY(&maps[VFNT_MAP_NORMAL_RH])) {
7954b529e40SToomas Soome 		if (fprintf(fp, "static struct font_map "
7964b529e40SToomas Soome 		    "FONTMAP_NORMAL_RH_%ux%u[] = {\n", width, height) < 0)
7974b529e40SToomas Soome 			goto done;
7984b529e40SToomas Soome 		if (write_source_mappings(fp, VFNT_MAP_NORMAL_RH) != 0)
7994b529e40SToomas Soome 			goto done;
8004b529e40SToomas Soome 		if (fprintf(fp, "};\n\n") < 0)
8014b529e40SToomas Soome 			goto done;
8024b529e40SToomas Soome 	}
8034b529e40SToomas Soome 	if (!TAILQ_EMPTY(&maps[VFNT_MAP_BOLD])) {
8044b529e40SToomas Soome 		if (fprintf(fp, "static struct font_map "
8054b529e40SToomas Soome 		    "FONTMAP_BOLD_%ux%u[] = {\n", width, height) < 0)
8064b529e40SToomas Soome 			goto done;
8074b529e40SToomas Soome 		if (write_source_mappings(fp, VFNT_MAP_BOLD) != 0)
8084b529e40SToomas Soome 			goto done;
8094b529e40SToomas Soome 		if (fprintf(fp, "};\n\n") < 0)
8104b529e40SToomas Soome 			goto done;
8114b529e40SToomas Soome 	}
8124b529e40SToomas Soome 	if (!TAILQ_EMPTY(&maps[VFNT_MAP_BOLD_RH])) {
8134b529e40SToomas Soome 		if (fprintf(fp, "static struct font_map "
8144b529e40SToomas Soome 		    "FONTMAP_BOLD_RH_%ux%u[] = {\n", width, height) < 0)
8154b529e40SToomas Soome 			goto done;
8164b529e40SToomas Soome 		if (write_source_mappings(fp, VFNT_MAP_BOLD_RH) != 0)
8174b529e40SToomas Soome 			goto done;
8184b529e40SToomas Soome 		if (fprintf(fp, "};\n\n") < 0)
8194b529e40SToomas Soome 			goto done;
8204b529e40SToomas Soome 	}
8214b529e40SToomas Soome 
8224b529e40SToomas Soome 	/* Write struct font. */
8234b529e40SToomas Soome 	if (fprintf(fp, "struct font font_%ux%u = {\n",
8244b529e40SToomas Soome 	    width, height) < 0)
8254b529e40SToomas Soome 		goto done;
8264b529e40SToomas Soome 	if (fprintf(fp, "\t.vf_map\t= {\n") < 0)
8274b529e40SToomas Soome 		goto done;
8284b529e40SToomas Soome 	if (TAILQ_EMPTY(&maps[VFNT_MAP_NORMAL])) {
8294b529e40SToomas Soome 		if (fprintf(fp, "\t\t\tNULL,\n") < 0)
8304b529e40SToomas Soome 			goto done;
8314b529e40SToomas Soome 	} else {
8324b529e40SToomas Soome 		if (fprintf(fp, "\t\t\tFONTMAP_NORMAL_%ux%u,\n",
8334b529e40SToomas Soome 		    width, height) < 0)
8344b529e40SToomas Soome 			goto done;
8354b529e40SToomas Soome 	}
8364b529e40SToomas Soome 	if (TAILQ_EMPTY(&maps[VFNT_MAP_NORMAL_RH])) {
8374b529e40SToomas Soome 		if (fprintf(fp, "\t\t\tNULL,\n") < 0)
8384b529e40SToomas Soome 			goto done;
8394b529e40SToomas Soome 	} else {
8404b529e40SToomas Soome 		if (fprintf(fp, "\t\t\tFONTMAP_NORMAL_RH_%ux%u,\n",
8414b529e40SToomas Soome 		    width, height) < 0)
8424b529e40SToomas Soome 			goto done;
8434b529e40SToomas Soome 	}
8444b529e40SToomas Soome 	if (TAILQ_EMPTY(&maps[VFNT_MAP_BOLD])) {
8454b529e40SToomas Soome 		if (fprintf(fp, "\t\t\tNULL,\n") < 0)
8464b529e40SToomas Soome 			goto done;
8474b529e40SToomas Soome 	} else {
8484b529e40SToomas Soome 		if (fprintf(fp, "\t\t\tFONTMAP_BOLD_%ux%u,\n",
8494b529e40SToomas Soome 		    width, height) < 0)
8504b529e40SToomas Soome 			goto done;
8514b529e40SToomas Soome 	}
8524b529e40SToomas Soome 	if (TAILQ_EMPTY(&maps[VFNT_MAP_BOLD_RH])) {
8534b529e40SToomas Soome 		if (fprintf(fp, "\t\t\tNULL\n") < 0)
8544b529e40SToomas Soome 			goto done;
8554b529e40SToomas Soome 	} else {
8564b529e40SToomas Soome 		if (fprintf(fp, "\t\t\tFONTMAP_BOLD_RH_%ux%u\n",
8574b529e40SToomas Soome 		    width, height) < 0)
8584b529e40SToomas Soome 			goto done;
8594b529e40SToomas Soome 	}
8604b529e40SToomas Soome 	if (fprintf(fp, "\t\t},\n") < 0)
8614b529e40SToomas Soome 		goto done;
8624b529e40SToomas Soome 	if (lz4 == true) {
8634b529e40SToomas Soome 		if (fprintf(fp, "\t.vf_bytes\t= NULL,\n") < 0)
8644b529e40SToomas Soome 			goto done;
8654b529e40SToomas Soome 	} else {
8664b529e40SToomas Soome 		if (fprintf(fp, "\t.vf_bytes\t= FONTDATA_%ux%u,\n",
8674b529e40SToomas Soome 		    width, height) < 0) {
8684b529e40SToomas Soome 			goto done;
8694b529e40SToomas Soome 		}
8704b529e40SToomas Soome 	}
8714b529e40SToomas Soome 	if (fprintf(fp, "\t.vf_width\t= %u,\n", width) < 0)
8724b529e40SToomas Soome 		goto done;
8734b529e40SToomas Soome 	if (fprintf(fp, "\t.vf_height\t= %u,\n", height) < 0)
8744b529e40SToomas Soome 		goto done;
8754b529e40SToomas Soome 	if (fprintf(fp, "\t.vf_map_count\t= { %u, %u, %u, %u }\n",
8764b529e40SToomas Soome 	    map_folded_count[0], map_folded_count[1], map_folded_count[2],
8774b529e40SToomas Soome 	    map_folded_count[3]) < 0) {
8784b529e40SToomas Soome 		goto done;
8794b529e40SToomas Soome 	}
8804b529e40SToomas Soome 	if (fprintf(fp, "};\n\n") < 0)
8814b529e40SToomas Soome 		goto done;
8824b529e40SToomas Soome 
8834b529e40SToomas Soome 	/* Write bitmap data. */
8844b529e40SToomas Soome 	if (fprintf(fp, "bitmap_data_t font_data_%ux%u = {\n",
8854b529e40SToomas Soome 	    width, height) < 0)
8864b529e40SToomas Soome 		goto done;
8874b529e40SToomas Soome 	if (fprintf(fp, "\t.width\t= %u,\n", width) < 0)
8884b529e40SToomas Soome 		goto done;
8894b529e40SToomas Soome 	if (fprintf(fp, "\t.height\t= %u,\n", height) < 0)
8904b529e40SToomas Soome 		goto done;
8914b529e40SToomas Soome 	if (lz4 == true) {
8924b529e40SToomas Soome 		if (fprintf(fp, "\t.compressed_size\t= %u,\n",
8934b529e40SToomas Soome 		    compressed_size) < 0) {
8944b529e40SToomas Soome 			goto done;
8954b529e40SToomas Soome 		}
8964b529e40SToomas Soome 		if (fprintf(fp, "\t.uncompressed_size\t= %u,\n",
8974b529e40SToomas Soome 		    uncompressed_size) < 0) {
8984b529e40SToomas Soome 			goto done;
8994b529e40SToomas Soome 		}
9004b529e40SToomas Soome 		if (fprintf(fp, "\t.compressed_data\t= FONTDATA_%ux%u,\n",
9014b529e40SToomas Soome 		    width, height) < 0) {
9024b529e40SToomas Soome 			goto done;
9034b529e40SToomas Soome 		}
9044b529e40SToomas Soome 	} else {
9054b529e40SToomas Soome 		if (fprintf(fp, "\t.compressed_size\t= 0,\n") < 0)
9064b529e40SToomas Soome 			goto done;
9074b529e40SToomas Soome 		if (fprintf(fp, "\t.uncompressed_size\t= %u,\n",
9084b529e40SToomas Soome 		    uncompressed_size) < 0) {
9094b529e40SToomas Soome 			goto done;
9104b529e40SToomas Soome 		}
9114b529e40SToomas Soome 		if (fprintf(fp, "\t.compressed_data\t= NULL,\n") < 0)
9124b529e40SToomas Soome 			goto done;
9134b529e40SToomas Soome 	}
9144b529e40SToomas Soome 	if (fprintf(fp, "\t.font = &font_%ux%u\n", width, height) < 0)
9154b529e40SToomas Soome 		goto done;
9164b529e40SToomas Soome 	if (fprintf(fp, "};\n") < 0)
9174b529e40SToomas Soome 		goto done;
9184b529e40SToomas Soome 
9194b529e40SToomas Soome 	rv = 0;
9204b529e40SToomas Soome done:
9214b529e40SToomas Soome 	if (rv != 0)
9224b529e40SToomas Soome 		perror(filename);
9234b529e40SToomas Soome 	fclose(fp);
9244b529e40SToomas Soome 	return (0);
9254b529e40SToomas Soome }
9264b529e40SToomas Soome 
9274b529e40SToomas Soome static void
9284b529e40SToomas Soome print_font_info(void)
9294b529e40SToomas Soome {
9304b529e40SToomas Soome 	printf(
9314b529e40SToomas Soome "Statistics:\n"
9324b529e40SToomas Soome "- glyph_total:                 %6u\n"
9334b529e40SToomas Soome "- glyph_normal:                %6u\n"
9344b529e40SToomas Soome "- glyph_normal_right:          %6u\n"
9354b529e40SToomas Soome "- glyph_bold:                  %6u\n"
9364b529e40SToomas Soome "- glyph_bold_right:            %6u\n"
9374b529e40SToomas Soome "- glyph_unique:                %6u\n"
9384b529e40SToomas Soome "- glyph_dupe:                  %6u\n"
9394b529e40SToomas Soome "- mapping_total:               %6u\n"
9404b529e40SToomas Soome "- mapping_normal:              %6u\n"
9414b529e40SToomas Soome "- mapping_normal_folded:       %6u\n"
9424b529e40SToomas Soome "- mapping_normal_right:        %6u\n"
9434b529e40SToomas Soome "- mapping_normal_right_folded: %6u\n"
9444b529e40SToomas Soome "- mapping_bold:                %6u\n"
9454b529e40SToomas Soome "- mapping_bold_folded:         %6u\n"
9464b529e40SToomas Soome "- mapping_bold_right:          %6u\n"
9474b529e40SToomas Soome "- mapping_bold_right_folded:   %6u\n"
9484b529e40SToomas Soome "- mapping_unique:              %6u\n"
9494b529e40SToomas Soome "- mapping_dupe:                %6u\n",
9504b529e40SToomas Soome 	    glyph_total,
9514b529e40SToomas Soome 	    glyph_count[0],
9524b529e40SToomas Soome 	    glyph_count[1],
9534b529e40SToomas Soome 	    glyph_count[2],
9544b529e40SToomas Soome 	    glyph_count[3],
9554b529e40SToomas Soome 	    glyph_unique, glyph_dupe,
9564b529e40SToomas Soome 	    mapping_total,
9574b529e40SToomas Soome 	    map_count[0], map_folded_count[0],
9584b529e40SToomas Soome 	    map_count[1], map_folded_count[1],
9594b529e40SToomas Soome 	    map_count[2], map_folded_count[2],
9604b529e40SToomas Soome 	    map_count[3], map_folded_count[3],
9614b529e40SToomas Soome 	    mapping_unique, mapping_dupe);
9624b529e40SToomas Soome }
9634b529e40SToomas Soome 
9644b529e40SToomas Soome int
9654b529e40SToomas Soome main(int argc, char *argv[])
9664b529e40SToomas Soome {
9674b529e40SToomas Soome 	int ch, val, verbose = 0, rv = 0;
9684b529e40SToomas Soome 	char *outfile = NULL;
9694b529e40SToomas Soome 
9704b529e40SToomas Soome 	assert(sizeof (struct file_header) == 32);
9714b529e40SToomas Soome 	assert(sizeof (struct file_mapping) == 8);
9724b529e40SToomas Soome 
9734b529e40SToomas Soome 	while ((ch = getopt(argc, argv, "nf:h:vw:o:")) != -1) {
9744b529e40SToomas Soome 		switch (ch) {
9754b529e40SToomas Soome 		case 'f':
9764b529e40SToomas Soome 			if (strcmp(optarg, "font") == 0)
9774b529e40SToomas Soome 				format = VT_FONT;
9784b529e40SToomas Soome 			else if (strcmp(optarg, "source") == 0)
9794b529e40SToomas Soome 				format = VT_C_SOURCE;
9804b529e40SToomas Soome 			else if (strcmp(optarg, "compressed-source") == 0)
9814b529e40SToomas Soome 				format = VT_C_COMPRESSED;
9824b529e40SToomas Soome 			else
9834b529e40SToomas Soome 				errx(1, "Invalid format: %s", optarg);
9844b529e40SToomas Soome 			break;
9854b529e40SToomas Soome 		case 'h':
9864b529e40SToomas Soome 			val = atoi(optarg);
9874b529e40SToomas Soome 			if (val <= 0 || val > 128)
9884b529e40SToomas Soome 				errx(1, "Invalid height %d", val);
9894b529e40SToomas Soome 			height = val;
9904b529e40SToomas Soome 			break;
9914b529e40SToomas Soome 		case 'n':
9924b529e40SToomas Soome 			filter = false;
9934b529e40SToomas Soome 			break;
9944b529e40SToomas Soome 		case 'o':
9954b529e40SToomas Soome 			outfile = optarg;
9964b529e40SToomas Soome 			break;
9974b529e40SToomas Soome 		case 'v':
9984b529e40SToomas Soome 			verbose = 1;
9994b529e40SToomas Soome 			break;
10004b529e40SToomas Soome 		case 'w':
10014b529e40SToomas Soome 			set_width(atoi(optarg));
10024b529e40SToomas Soome 			break;
10034b529e40SToomas Soome 		case '?':
10044b529e40SToomas Soome 		default:
10054b529e40SToomas Soome 			usage();
10064b529e40SToomas Soome 		}
10074b529e40SToomas Soome 	}
10084b529e40SToomas Soome 	argc -= optind;
10094b529e40SToomas Soome 	argv += optind;
10104b529e40SToomas Soome 
10114b529e40SToomas Soome 	if (outfile == NULL || argc < 1 || argc > 2)
10124b529e40SToomas Soome 		usage();
10134b529e40SToomas Soome 
10144b529e40SToomas Soome 	wbytes = howmany(width, 8);
10154b529e40SToomas Soome 
10164b529e40SToomas Soome 	if (parse_file(argv[0], VFNT_MAP_NORMAL) != 0)
10174b529e40SToomas Soome 		return (1);
10184b529e40SToomas Soome 	argc--;
10194b529e40SToomas Soome 	argv++;
10204b529e40SToomas Soome 	if (argc == 1) {
10214b529e40SToomas Soome 		if (parse_file(argv[0], VFNT_MAP_BOLD) != 0)
10224b529e40SToomas Soome 			return (1);
10234b529e40SToomas Soome 		argc--;
10244b529e40SToomas Soome 		argv++;
10254b529e40SToomas Soome 	}
10264b529e40SToomas Soome 	number_glyphs();
10274b529e40SToomas Soome 	dedup_mapping(VFNT_MAP_BOLD);
10284b529e40SToomas Soome 	dedup_mapping(VFNT_MAP_BOLD_RH);
10294b529e40SToomas Soome 	fold_mappings(0);
10304b529e40SToomas Soome 	fold_mappings(1);
10314b529e40SToomas Soome 	fold_mappings(2);
10324b529e40SToomas Soome 	fold_mappings(3);
10334b529e40SToomas Soome 
10344b529e40SToomas Soome 	switch (format) {
10354b529e40SToomas Soome 	case VT_FONT:
10364b529e40SToomas Soome 		rv = write_fnt(outfile);
10374b529e40SToomas Soome 		break;
10384b529e40SToomas Soome 	case VT_C_SOURCE:
10394b529e40SToomas Soome 		rv = write_fnt_source(false, outfile);
10404b529e40SToomas Soome 		break;
10414b529e40SToomas Soome 	case VT_C_COMPRESSED:
10424b529e40SToomas Soome 		rv = write_fnt_source(true, outfile);
10434b529e40SToomas Soome 		break;
10444b529e40SToomas Soome 	}
10454b529e40SToomas Soome 
10464b529e40SToomas Soome 	if (verbose)
10474b529e40SToomas Soome 		print_font_info();
10484b529e40SToomas Soome 
10494b529e40SToomas Soome 	return (rv);
10504b529e40SToomas Soome }
1051