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