Index: usr.bin/vtfontcvt/vtfontcvt.c =================================================================== --- usr.bin/vtfontcvt/vtfontcvt.c +++ usr.bin/vtfontcvt/vtfontcvt.c @@ -47,19 +47,19 @@ #include #include -#define VFNT_MAXGLYPHS 131072 -#define VFNT_MAXDIMENSION 128 +#define VFNT_MAXGLYPHS 131072 +#define VFNT_MAXDIMENSION 128 static unsigned int width = 8, wbytes, height = 16; struct glyph { - TAILQ_ENTRY(glyph) g_list; - SLIST_ENTRY(glyph) g_hash; - uint8_t *g_data; - unsigned int g_index; + TAILQ_ENTRY(glyph) g_list; + SLIST_ENTRY(glyph) g_hash; + uint8_t *g_data; + unsigned int g_index; }; -#define FONTCVT_NHASH 4096 +#define FONTCVT_NHASH 4096 TAILQ_HEAD(glyph_list, glyph); static SLIST_HEAD(, glyph) glyph_hash[FONTCVT_NHASH]; static struct glyph_list glyphs[VFNT_MAPS] = { @@ -71,10 +71,10 @@ static unsigned int glyph_total, glyph_count[4], glyph_unique, glyph_dupe; struct mapping { - TAILQ_ENTRY(mapping) m_list; - unsigned int m_char; - unsigned int m_length; - struct glyph *m_glyph; + TAILQ_ENTRY(mapping) m_list; + unsigned int m_char; + unsigned int m_length; + struct glyph *m_glyph; }; TAILQ_HEAD(mapping_list, mapping); @@ -88,9 +88,9 @@ mapping_unique, mapping_dupe; enum output_format { - VT_FONT, /* default */ - VT_C_SOURCE, /* C source for built in fonts */ - VT_C_COMPRESSED /* C source with compressed font data */ + VT_FONT, /* default */ + VT_C_SOURCE, /* C source for built in fonts */ + VT_C_COMPRESSED /* C source with compressed font data */ }; struct whitelist { @@ -103,19 +103,19 @@ * ascii set and box drawing chars. */ static struct whitelist c_list[] = { - { .c = 0, .len = 0 }, /* deault char */ - { .c = 0x20, .len = 0x5f }, - { .c = 0x2500, .len = 0 }, /* single frame */ - { .c = 0x2502, .len = 0 }, - { .c = 0x250c, .len = 0 }, - { .c = 0x2510, .len = 0 }, - { .c = 0x2514, .len = 0 }, - { .c = 0x2518, .len = 0 }, - { .c = 0x2550, .len = 1 }, /* double frame */ - { .c = 0x2554, .len = 0 }, - { .c = 0x2557, .len = 0 }, - { .c = 0x255a, .len = 0 }, - { .c = 0x255d, .len = 0 }, + { .c = 0, .len = 0 }, /* deault char */ + { .c = 0x20, .len = 0x5f }, + { .c = 0x2500, .len = 0 }, /* single frame */ + { .c = 0x2502, .len = 0 }, + { .c = 0x250c, .len = 0 }, + { .c = 0x2510, .len = 0 }, + { .c = 0x2514, .len = 0 }, + { .c = 0x2518, .len = 0 }, + { .c = 0x2550, .len = 1 },/* double frame */ + { .c = 0x2554, .len = 0 }, + { .c = 0x2557, .len = 0 }, + { .c = 0x255a, .len = 0 }, + { .c = 0x255d, .len = 0 }, }; /* @@ -123,51 +123,51 @@ * can program font into the vga card. */ static struct whitelist s_list[] = { - { .c = 0, .len = 0 }, /* deault char */ - { .c = 0x20, .len = 0x5f }, /* ascii set */ - { .c = 0xA0, .len = 0x5f }, /* latin 1 */ - { .c = 0x0192, .len = 0 }, - { .c = 0x0332, .len = 0 }, /* composing lower line */ - { .c = 0x0393, .len = 0 }, - { .c = 0x0398, .len = 0 }, - { .c = 0x03A3, .len = 0 }, - { .c = 0x03A6, .len = 0 }, - { .c = 0x03A9, .len = 0 }, - { .c = 0x03B1, .len = 1 }, - { .c = 0x03B4, .len = 0 }, - { .c = 0x03C0, .len = 0 }, - { .c = 0x03C3, .len = 0 }, - { .c = 0x03C4, .len = 0 }, - { .c = 0x207F, .len = 0 }, - { .c = 0x20A7, .len = 0 }, - { .c = 0x2205, .len = 0 }, - { .c = 0x220A, .len = 0 }, - { .c = 0x2219, .len = 1 }, - { .c = 0x221E, .len = 0 }, - { .c = 0x2229, .len = 0 }, - { .c = 0x2248, .len = 0 }, - { .c = 0x2261, .len = 0 }, - { .c = 0x2264, .len = 1 }, - { .c = 0x2310, .len = 0 }, - { .c = 0x2320, .len = 1 }, - { .c = 0x2500, .len = 0 }, - { .c = 0x2502, .len = 0 }, - { .c = 0x250C, .len = 0 }, - { .c = 0x2510, .len = 0 }, - { .c = 0x2514, .len = 0 }, - { .c = 0x2518, .len = 0 }, - { .c = 0x251C, .len = 0 }, - { .c = 0x2524, .len = 0 }, - { .c = 0x252C, .len = 0 }, - { .c = 0x2534, .len = 0 }, - { .c = 0x253C, .len = 0 }, - { .c = 0x2550, .len = 0x1c }, - { .c = 0x2580, .len = 0 }, - { .c = 0x2584, .len = 0 }, - { .c = 0x2588, .len = 0 }, - { .c = 0x258C, .len = 0 }, - { .c = 0x2590, .len = 3 }, - { .c = 0x25A0, .len = 0 }, + { .c = 0, .len = 0 }, /* deault char */ + { .c = 0x20, .len = 0x5f }, /* ascii set */ + { .c = 0xA0, .len = 0x5f }, /* latin 1 */ + { .c = 0x0192, .len = 0 }, + { .c = 0x0332, .len = 0 }, /* composing lower line */ + { .c = 0x0393, .len = 0 }, + { .c = 0x0398, .len = 0 }, + { .c = 0x03A3, .len = 0 }, + { .c = 0x03A6, .len = 0 }, + { .c = 0x03A9, .len = 0 }, + { .c = 0x03B1, .len = 1 }, + { .c = 0x03B4, .len = 0 }, + { .c = 0x03C0, .len = 0 }, + { .c = 0x03C3, .len = 0 }, + { .c = 0x03C4, .len = 0 }, + { .c = 0x207F, .len = 0 }, + { .c = 0x20A7, .len = 0 }, + { .c = 0x2205, .len = 0 }, + { .c = 0x220A, .len = 0 }, + { .c = 0x2219, .len = 1 }, + { .c = 0x221E, .len = 0 }, + { .c = 0x2229, .len = 0 }, + { .c = 0x2248, .len = 0 }, + { .c = 0x2261, .len = 0 }, + { .c = 0x2264, .len = 1 }, + { .c = 0x2310, .len = 0 }, + { .c = 0x2320, .len = 1 }, + { .c = 0x2500, .len = 0 }, + { .c = 0x2502, .len = 0 }, + { .c = 0x250C, .len = 0 }, + { .c = 0x2510, .len = 0 }, + { .c = 0x2514, .len = 0 }, + { .c = 0x2518, .len = 0 }, + { .c = 0x251C, .len = 0 }, + { .c = 0x2524, .len = 0 }, + { .c = 0x252C, .len = 0 }, + { .c = 0x2534, .len = 0 }, + { .c = 0x253C, .len = 0 }, + { .c = 0x2550, .len = 0x1c }, + { .c = 0x2580, .len = 0 }, + { .c = 0x2584, .len = 0 }, + { .c = 0x2588, .len = 0 }, + { .c = 0x258C, .len = 0 }, + { .c = 0x2590, .len = 3 }, + { .c = 0x25A0, .len = 0 }, }; static bool filter = true; @@ -179,13 +179,13 @@ static void usage(void) { - (void)fprintf(stderr, "usage: vtfontcvt " "[-n] [-f font|source|compressed-source] [-w width] " "[-h height]\n\t[-v] normal.bdf [bold.bdf] out.fnt\n"); exit(1); } + static void * xmalloc(size_t size) { @@ -196,6 +196,7 @@ return (m); } + static int add_mapping(struct glyph *gl, unsigned int c, unsigned int map_idx) { @@ -216,7 +217,7 @@ TAILQ_INSERT_TAIL(ml, mp, m_list); } else { /* Find insertion point for char; cannot be at end. */ - TAILQ_FOREACH(mp_temp, ml, m_list) { + TAILQ_FOREACH (mp_temp, ml, m_list) { if (mp_temp->m_char >= c) { TAILQ_INSERT_BEFORE(mp_temp, mp, m_list); break; @@ -230,6 +231,7 @@ return (0); } + static int dedup_mapping(unsigned int map_idx) { @@ -238,7 +240,7 @@ assert(map_idx == VFNT_MAP_BOLD || map_idx == VFNT_MAP_BOLD_RIGHT); mp_normal = TAILQ_FIRST(&maps[normal_map_idx]); - TAILQ_FOREACH_SAFE(mp_bold, &maps[map_idx], m_list, mp_temp) { + TAILQ_FOREACH_SAFE (mp_bold, &maps[map_idx], m_list, mp_temp) { while (mp_normal->m_char < mp_bold->m_char) mp_normal = TAILQ_NEXT(mp_normal, m_list); if (mp_bold->m_char != mp_normal->m_char) @@ -255,6 +257,7 @@ return (0); } + static struct glyph * add_glyph(const uint8_t *bytes, unsigned int map_idx, int fallback) { @@ -266,7 +269,7 @@ /* Return existing glyph if we have an identical one. */ hash = fnv_32_buf(bytes, wbytes * height, FNV1_32_INIT) % FONTCVT_NHASH; - SLIST_FOREACH(gl, &glyph_hash[hash], g_hash) { + SLIST_FOREACH (gl, &glyph_hash[hash], g_hash) { if (memcmp(gl->g_data, bytes, wbytes * height) == 0) { glyph_dupe++; return (gl); @@ -289,6 +292,7 @@ return (gl); } + static bool check_whitelist(unsigned c) { @@ -300,14 +304,15 @@ if (format == VT_C_SOURCE) { w = s_list; - n = sizeof (s_list) / sizeof (s_list[0]); + n = sizeof(s_list) / sizeof(s_list[0]); } if (format == VT_C_COMPRESSED) { w = c_list; - n = sizeof (c_list) / sizeof (c_list[0]); + n = sizeof(c_list) / sizeof(c_list[0]); } if (w == NULL) return (true); + for (i = 0; i < n; i++) { if (c >= w[i].c && c <= w[i].c + w[i].len) return (true); @@ -315,6 +320,7 @@ return (false); } + static int add_char(unsigned curchar, unsigned map_idx, uint8_t *bytes, uint8_t *bytes_r) { @@ -328,6 +334,7 @@ gl = add_glyph(bytes, map_idx, 0); if (add_mapping(gl, curchar, map_idx) != 0) return (1); + if (bytes_r != NULL) { gl = add_glyph(bytes_r, map_idx + 1, 0); if (add_mapping(gl, curchar, map_idx + 1) != 0) @@ -337,6 +344,7 @@ return (0); } + /* * Right-shift glyph row. */ @@ -348,11 +356,13 @@ if (shift == 0) return; + for (i = len - 1; i >= 0; i--) buf[i] = (i >= off_byte ? buf[i - off_byte] >> off_bit : 0) | (i > off_byte ? buf[i - off_byte - 1] << (8 - off_bit) : 0); } + /* * Split double-width characters into left and right half. Single-width * characters in _left_ only. @@ -381,6 +391,7 @@ return (0); } + static void set_height(int h) { @@ -389,6 +400,7 @@ height = h; } + static void set_width(int w) { @@ -398,6 +410,7 @@ wbytes = howmany(width, 8); } + static int parse_bdf(FILE *fp, unsigned int map_idx) { @@ -405,8 +418,8 @@ size_t length; uint8_t *line, *bytes, *bytes_r; unsigned int curchar = 0, i, j, linenum = 0, bbwbytes; - int bbw, bbh, bbox, bboy; /* Glyph bounding box. */ - int fbbw = 0, fbbh, fbbox, fbboy; /* Font bounding box. */ + int bbw, bbh, bbox, bboy; /* Glyph bounding box. */ + int fbbw = 0, fbbh, fbbox, fbboy; /* Font bounding box. */ int dwidth = 0, dwy = 0; int rv = -1; char spc = '\0'; @@ -454,7 +467,8 @@ if (sscanf(ln + 7, "%d %d", &dwidth, &dwy) != 2) errx(1, "invalid DWIDTH at line %u", linenum); if (dwy != 0 || (dwidth != fbbw && dwidth * 2 != fbbw)) - errx(1, "bitmap with unsupported DWIDTH %d %d at line %u", + errx(1, + "bitmap with unsupported DWIDTH %d %d at line %u", dwidth, dwy, linenum); if (dwidth < fbbw) set_width(dwidth); @@ -479,12 +493,13 @@ dwidth = atoi(ln + 7); } else if (strncmp(ln, "BBX ", 4) == 0) { if (sscanf(ln + 4, "%d %d %d %d", &bbw, &bbh, &bbox, - &bboy) != 4) + &bboy) != 4) errx(1, "invalid BBX at line %u", linenum); if (bbw < 1 || bbh < 1 || bbw > fbbw || bbh > fbbh || bbox < fbbox || bboy < fbboy || bbh + bboy > fbbh + fbboy) - errx(1, "broken bitmap with BBX %d %d %d %d at line %u", + errx(1, + "broken bitmap with BBX %d %d %d %d at line %u", bbw, bbh, bbox, bboy, linenum); bbwbytes = howmany(bbw, 8); } else if (strncmp(ln, "BITMAP", 6) == 0 && @@ -523,14 +538,14 @@ rshift_row(line, wbytes * 2, bbox - fbbox); rv = split_row(bytes + i * wbytes, - bytes_r + i * wbytes, line, dwidth); + bytes_r + i * wbytes, line, dwidth); if (rv != 0) goto out; } if (check_whitelist(curchar) == true) { rv = add_char(curchar, map_idx, bytes, - dwidth > (int)width ? bytes_r : NULL); + dwidth > (int)width ? bytes_r : NULL); if (rv != 0) goto out; } @@ -546,6 +561,7 @@ return (rv); } + static int parse_hex(FILE *fp, unsigned int map_idx) { @@ -560,11 +576,13 @@ if (strncmp(ln, "# Height: ", 10) == 0) { if (bytes != NULL) - errx(1, "malformed input: Height tag after font data"); + errx(1, + "malformed input: Height tag after font data"); set_height(atoi(ln + 10)); } else if (strncmp(ln, "# Width: ", 9) == 0) { if (bytes != NULL) - errx(1, "malformed input: Width tag after font data"); + errx(1, + "malformed input: Width tag after font data"); set_width(atoi(ln + 9)); } else if (sscanf(ln, "%6x:", &curchar)) { if (bytes == NULL) { @@ -594,7 +612,7 @@ *(line + j) = (uint8_t)val; } rv = split_row(bytes + i * wbytes, - bytes_r + i * wbytes, line, gwidth); + bytes_r + i * wbytes, line, gwidth); if (rv != 0) goto out; p += gwbytes * 2; @@ -602,7 +620,7 @@ if (check_whitelist(curchar) == true) { rv = add_char(curchar, map_idx, bytes, - gwidth != width ? bytes_r : NULL); + gwidth != width ? bytes_r : NULL); if (rv != 0) goto out; } @@ -615,6 +633,7 @@ return (rv); } + static int parse_file(const char *filename, unsigned int map_idx) { @@ -636,6 +655,7 @@ return (rv); } + static void number_glyphs(void) { @@ -643,10 +663,11 @@ unsigned int i, idx = 0; for (i = 0; i < VFNT_MAPS; i++) - TAILQ_FOREACH(gl, &glyphs[i], g_list) + TAILQ_FOREACH (gl, &glyphs[i], g_list) gl->g_index = idx++; } + /* Note we only deal with byte stream here. */ static size_t write_glyph_source(const void *ptr, size_t size, size_t nitems, FILE *stream) @@ -669,6 +690,7 @@ return (nitems); } + /* Write to buffer */ static size_t write_glyph_buf(const void *ptr, size_t size, size_t nitems, @@ -677,12 +699,13 @@ static size_t index = 0; size *= nitems; - (void) memmove(uncompressed + index, ptr, size); + (void)memmove(uncompressed + index, ptr, size); index += size; return (nitems); } + static int write_glyphs(FILE *fp, vt_write cb) { @@ -690,13 +713,14 @@ unsigned int i; for (i = 0; i < VFNT_MAPS; i++) { - TAILQ_FOREACH(gl, &glyphs[i], g_list) + TAILQ_FOREACH (gl, &glyphs[i], g_list) if (cb(gl->g_data, wbytes * height, 1, fp) != 1) return (1); } return (0); } + static void fold_mappings(unsigned int map_idx) { @@ -715,6 +739,7 @@ } } + static int write_mappings(FILE *fp, unsigned int map_idx) { @@ -723,7 +748,7 @@ vfnt_map_t fm; unsigned int i = 0, j = 0; - TAILQ_FOREACH(mp, ml, m_list) { + TAILQ_FOREACH (mp, ml, m_list) { j++; if (mp->m_length > 0) { i += mp->m_length; @@ -738,6 +763,7 @@ return (0); } + static int write_source_mappings(FILE *fp, unsigned int map_idx) { @@ -745,7 +771,7 @@ struct mapping *mp; unsigned int i = 0, j = 0; - TAILQ_FOREACH(mp, ml, m_list) { + TAILQ_FOREACH (mp, ml, m_list) { j++; if (mp->m_length > 0) { i += mp->m_length; @@ -759,12 +785,13 @@ return (0); } + static int write_fnt(const char *filename) { FILE *fp; struct font_header fh = { - .fh_magic = FONT_HEADER_MAGIC, + .fh_magic = FONT_HEADER_MAGIC, }; fp = fopen(filename, "wb"); @@ -800,6 +827,7 @@ return (0); } + static int write_fnt_source(bool lz4, const char *filename) { @@ -837,7 +865,7 @@ if (write_glyphs(fp, &write_glyph_buf) != 0) goto done; compressed_size = lz4_compress(uncompressed, compressed, - uncompressed_size, compressed_size, 0); + uncompressed_size, compressed_size, 0); if (write_glyph_source(compressed, compressed_size, 1, fp) != 1) goto done; free(uncompressed); @@ -847,7 +875,7 @@ goto done; } if (fprintf(fp, "};\n\n") < 0) - goto done; + goto done; /* Write font maps. */ if (!TAILQ_EMPTY(&maps[VFNT_MAP_NORMAL])) { @@ -992,46 +1020,48 @@ return (0); } + static void print_font_info(void) { printf( -"Statistics:\n" -"- width: %6u\n" -"- height: %6u\n" -"- glyph_total: %6u\n" -"- glyph_normal: %6u\n" -"- glyph_normal_right: %6u\n" -"- glyph_bold: %6u\n" -"- glyph_bold_right: %6u\n" -"- glyph_unique: %6u\n" -"- glyph_dupe: %6u\n" -"- mapping_total: %6u\n" -"- mapping_normal: %6u\n" -"- mapping_normal_folded: %6u\n" -"- mapping_normal_right: %6u\n" -"- mapping_normal_right_folded: %6u\n" -"- mapping_bold: %6u\n" -"- mapping_bold_folded: %6u\n" -"- mapping_bold_right: %6u\n" -"- mapping_bold_right_folded: %6u\n" -"- mapping_unique: %6u\n" -"- mapping_dupe: %6u\n", - width, height, - glyph_total, - glyph_count[0], - glyph_count[1], - glyph_count[2], - glyph_count[3], - glyph_unique, glyph_dupe, - mapping_total, - map_count[0], map_folded_count[0], - map_count[1], map_folded_count[1], - map_count[2], map_folded_count[2], - map_count[3], map_folded_count[3], - mapping_unique, mapping_dupe); + "Statistics:\n" + "- width: %6u\n" + "- height: %6u\n" + "- glyph_total: %6u\n" + "- glyph_normal: %6u\n" + "- glyph_normal_right: %6u\n" + "- glyph_bold: %6u\n" + "- glyph_bold_right: %6u\n" + "- glyph_unique: %6u\n" + "- glyph_dupe: %6u\n" + "- mapping_total: %6u\n" + "- mapping_normal: %6u\n" + "- mapping_normal_folded: %6u\n" + "- mapping_normal_right: %6u\n" + "- mapping_normal_right_folded: %6u\n" + "- mapping_bold: %6u\n" + "- mapping_bold_folded: %6u\n" + "- mapping_bold_right: %6u\n" + "- mapping_bold_right_folded: %6u\n" + "- mapping_unique: %6u\n" + "- mapping_dupe: %6u\n", + width, height, + glyph_total, + glyph_count[0], + glyph_count[1], + glyph_count[2], + glyph_count[3], + glyph_unique, glyph_dupe, + mapping_total, + map_count[0], map_folded_count[0], + map_count[1], map_folded_count[1], + map_count[2], map_folded_count[2], + map_count[3], map_folded_count[3], + mapping_unique, mapping_dupe); } + int main(int argc, char *argv[]) { @@ -1089,11 +1119,13 @@ if (parse_file(argv[0], VFNT_MAP_NORMAL) != 0) return (1); + argc--; argv++; if (argc == 1) { if (parse_file(argv[0], VFNT_MAP_BOLD) != 0) return (1); + argc--; argv++; }