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