2 * Copyright (c) 2009, 2014 The FreeBSD Foundation
4 * This software was developed by Ed Schouten under sponsorship from the
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 #include <sys/types.h>
31 #include <sys/endian.h>
32 #include <sys/fnv_hash.h>
34 #include <sys/param.h>
35 #include <sys/queue.h>
47 #define VFNT_MAXGLYPHS 131072
48 #define VFNT_MAXDIMENSION 128
50 static unsigned int width = 8, wbytes, height = 16;
53 TAILQ_ENTRY(glyph) g_list;
54 SLIST_ENTRY(glyph) g_hash;
59 #define FONTCVT_NHASH 4096
60 TAILQ_HEAD(glyph_list, glyph);
61 static SLIST_HEAD(, glyph) glyph_hash[FONTCVT_NHASH];
62 static struct glyph_list glyphs[VFNT_MAPS] = {
63 TAILQ_HEAD_INITIALIZER(glyphs[0]),
64 TAILQ_HEAD_INITIALIZER(glyphs[1]),
65 TAILQ_HEAD_INITIALIZER(glyphs[2]),
66 TAILQ_HEAD_INITIALIZER(glyphs[3]),
68 static unsigned int glyph_total, glyph_count[4], glyph_unique, glyph_dupe;
71 TAILQ_ENTRY(mapping) m_list;
73 unsigned int m_length;
74 struct glyph *m_glyph;
77 TAILQ_HEAD(mapping_list, mapping);
78 static struct mapping_list maps[VFNT_MAPS] = {
79 TAILQ_HEAD_INITIALIZER(maps[0]),
80 TAILQ_HEAD_INITIALIZER(maps[1]),
81 TAILQ_HEAD_INITIALIZER(maps[2]),
82 TAILQ_HEAD_INITIALIZER(maps[3]),
84 static unsigned int mapping_total, map_count[4], map_folded_count[4],
85 mapping_unique, mapping_dupe;
88 VT_FONT, /* default */
89 VT_C_SOURCE, /* C source for built in fonts */
90 VT_C_COMPRESSED /* C source with compressed font data */
99 * Compressed font glyph list. To be used with boot loader, we need to have
100 * ascii set and box drawing chars.
102 static struct whitelist c_list[] = {
103 { .c = 0, .len = 0 }, /* deault char */
104 { .c = 0x20, .len = 0x5f },
105 { .c = 0x2500, .len = 0 }, /* single frame */
106 { .c = 0x2502, .len = 0 },
107 { .c = 0x250c, .len = 0 },
108 { .c = 0x2510, .len = 0 },
109 { .c = 0x2514, .len = 0 },
110 { .c = 0x2518, .len = 0 },
111 { .c = 0x2550, .len = 1 }, /* double frame */
112 { .c = 0x2554, .len = 0 },
113 { .c = 0x2557, .len = 0 },
114 { .c = 0x255a, .len = 0 },
115 { .c = 0x255d, .len = 0 },
119 * Uncompressed source. For x86 we need cp437 so the vga text mode
120 * can program font into the vga card.
122 static struct whitelist s_list[] = {
123 { .c = 0, .len = 0 }, /* deault char */
124 { .c = 0x20, .len = 0x5f }, /* ascii set */
125 { .c = 0xA0, .len = 0x5f }, /* latin 1 */
126 { .c = 0x0192, .len = 0 },
127 { .c = 0x0332, .len = 0 }, /* composing lower line */
128 { .c = 0x0393, .len = 0 },
129 { .c = 0x0398, .len = 0 },
130 { .c = 0x03A3, .len = 0 },
131 { .c = 0x03A6, .len = 0 },
132 { .c = 0x03A9, .len = 0 },
133 { .c = 0x03B1, .len = 1 },
134 { .c = 0x03B4, .len = 0 },
135 { .c = 0x03C0, .len = 0 },
136 { .c = 0x03C3, .len = 0 },
137 { .c = 0x03C4, .len = 0 },
138 { .c = 0x207F, .len = 0 },
139 { .c = 0x20A7, .len = 0 },
140 { .c = 0x2205, .len = 0 },
141 { .c = 0x220A, .len = 0 },
142 { .c = 0x2219, .len = 1 },
143 { .c = 0x221E, .len = 0 },
144 { .c = 0x2229, .len = 0 },
145 { .c = 0x2248, .len = 0 },
146 { .c = 0x2261, .len = 0 },
147 { .c = 0x2264, .len = 1 },
148 { .c = 0x2310, .len = 0 },
149 { .c = 0x2320, .len = 1 },
150 { .c = 0x2500, .len = 0 },
151 { .c = 0x2502, .len = 0 },
152 { .c = 0x250C, .len = 0 },
153 { .c = 0x2510, .len = 0 },
154 { .c = 0x2514, .len = 0 },
155 { .c = 0x2518, .len = 0 },
156 { .c = 0x251C, .len = 0 },
157 { .c = 0x2524, .len = 0 },
158 { .c = 0x252C, .len = 0 },
159 { .c = 0x2534, .len = 0 },
160 { .c = 0x253C, .len = 0 },
161 { .c = 0x2550, .len = 0x1c },
162 { .c = 0x2580, .len = 0 },
163 { .c = 0x2584, .len = 0 },
164 { .c = 0x2588, .len = 0 },
165 { .c = 0x258C, .len = 0 },
166 { .c = 0x2590, .len = 3 },
167 { .c = 0x25A0, .len = 0 },
170 static bool filter = true;
171 static enum output_format format = VT_FONT;
172 /* Type for write callback. */
173 typedef size_t (*vt_write)(const void *, size_t, size_t, FILE *);
174 static uint8_t *uncompressed;
180 (void)fprintf(stderr, "usage: vtfontcvt "
181 "[-nv] [-f format] [-h height] [-w width]\n"
182 "\t-o output_file normal_font [bold_font]\n");
191 if ((m = calloc(1, size)) == NULL)
192 errx(1, "memory allocation failure");
197 add_mapping(struct glyph *gl, unsigned int c, unsigned int map_idx)
199 struct mapping *mp, *mp_temp;
200 struct mapping_list *ml;
204 mp = xmalloc(sizeof *mp);
210 if (TAILQ_LAST(ml, mapping_list) == NULL ||
211 TAILQ_LAST(ml, mapping_list)->m_char < c) {
212 /* Common case: empty list or new char at end of list. */
213 TAILQ_INSERT_TAIL(ml, mp, m_list);
215 /* Find insertion point for char; cannot be at end. */
216 TAILQ_FOREACH(mp_temp, ml, m_list) {
217 if (mp_temp->m_char >= c) {
218 TAILQ_INSERT_BEFORE(mp_temp, mp, m_list);
224 map_count[map_idx]++;
231 dedup_mapping(unsigned int map_idx)
233 struct mapping *mp_bold, *mp_normal, *mp_temp;
234 unsigned normal_map_idx = map_idx - VFNT_MAP_BOLD;
236 assert(map_idx == VFNT_MAP_BOLD || map_idx == VFNT_MAP_BOLD_RIGHT);
237 mp_normal = TAILQ_FIRST(&maps[normal_map_idx]);
238 TAILQ_FOREACH_SAFE(mp_bold, &maps[map_idx], m_list, mp_temp) {
239 while (mp_normal->m_char < mp_bold->m_char)
240 mp_normal = TAILQ_NEXT(mp_normal, m_list);
241 if (mp_bold->m_char != mp_normal->m_char)
242 errx(1, "Character %u not in normal font!",
244 if (mp_bold->m_glyph != mp_normal->m_glyph)
247 /* No mapping is needed if it's equal to the normal mapping. */
248 TAILQ_REMOVE(&maps[map_idx], mp_bold, m_list);
255 static struct glyph *
256 add_glyph(const uint8_t *bytes, unsigned int map_idx, int fallback)
262 glyph_count[map_idx]++;
264 /* Return existing glyph if we have an identical one. */
265 hash = fnv_32_buf(bytes, wbytes * height, FNV1_32_INIT) % FONTCVT_NHASH;
266 SLIST_FOREACH(gl, &glyph_hash[hash], g_hash) {
267 if (memcmp(gl->g_data, bytes, wbytes * height) == 0) {
273 /* Allocate new glyph. */
274 gl = xmalloc(sizeof *gl);
275 gl->g_data = xmalloc(wbytes * height);
276 memcpy(gl->g_data, bytes, wbytes * height);
278 TAILQ_INSERT_HEAD(&glyphs[map_idx], gl, g_list);
280 TAILQ_INSERT_TAIL(&glyphs[map_idx], gl, g_list);
281 SLIST_INSERT_HEAD(&glyph_hash[hash], gl, g_hash);
284 if (glyph_unique > VFNT_MAXGLYPHS)
285 errx(1, "too many glyphs (%u)", glyph_unique);
290 check_whitelist(unsigned c)
292 struct whitelist *w = NULL;
298 if (format == VT_C_SOURCE) {
300 n = sizeof(s_list) / sizeof(s_list[0]);
302 if (format == VT_C_COMPRESSED) {
304 n = sizeof(c_list) / sizeof(c_list[0]);
308 for (i = 0; i < n; i++) {
309 if (c >= w[i].c && c <= w[i].c + w[i].len)
316 add_char(unsigned curchar, unsigned map_idx, uint8_t *bytes, uint8_t *bytes_r)
320 /* Prevent adding two glyphs for 0xFFFD */
321 if (curchar == 0xFFFD) {
322 if (map_idx < VFNT_MAP_BOLD)
323 gl = add_glyph(bytes, 0, 1);
324 } else if (filter == false || curchar >= 0x20) {
325 gl = add_glyph(bytes, map_idx, 0);
326 if (add_mapping(gl, curchar, map_idx) != 0)
328 if (bytes_r != NULL) {
329 gl = add_glyph(bytes_r, map_idx + 1, 0);
330 if (add_mapping(gl, curchar, map_idx + 1) != 0)
338 * Right-shift glyph row.
341 rshift_row(uint8_t *buf, size_t len, size_t shift)
343 ssize_t i, off_byte = shift / 8;
344 size_t off_bit = shift % 8;
348 for (i = len - 1; i >= 0; i--)
349 buf[i] = (i >= off_byte ? buf[i - off_byte] >> off_bit : 0) |
350 (i > off_byte ? buf[i - off_byte - 1] << (8 - off_bit) : 0);
354 * Split double-width characters into left and right half. Single-width
355 * characters in _left_ only.
358 split_row(uint8_t *left, uint8_t *right, uint8_t *line, size_t w)
362 s = wbytes * 8 - width;
364 memcpy(left, line, wbytes);
365 *(left + wbytes - 1) &= 0xFF << s;
367 if (w > width) { /* Double-width character. */
370 for (i = 0; i < wbytes; i++) {
371 t = *(line + wbytes + i - 1);
373 t |= *(line + wbytes + i) >> s;
376 *(right + wbytes - 1) &= 0xFF << s;
384 if (h <= 0 || h > VFNT_MAXDIMENSION)
385 errx(1, "invalid height %d", h);
392 if (w <= 0 || w > VFNT_MAXDIMENSION)
393 errx(1, "invalid width %d", w);
395 wbytes = howmany(width, 8);
399 parse_bdf(FILE *fp, unsigned int map_idx)
403 uint8_t *line, *bytes, *bytes_r;
404 unsigned int curchar = 0, i, j, linenum = 0, bbwbytes;
405 int bbw, bbh, bbox, bboy; /* Glyph bounding box. */
406 int fbbw = 0, fbbh, fbbox, fbboy; /* Font bounding box. */
407 int dwidth = 0, dwy = 0;
412 * Step 1: Parse FONT logical font descriptor and FONTBOUNDINGBOX
415 while ((ln = fgetln(fp, &length)) != NULL) {
417 ln[length - 1] = '\0';
419 if (strncmp(ln, "FONT ", 5) == 0) {
422 while ((p = strchr(p, '-')) != NULL) {
430 } else if (strncmp(ln, "FONTBOUNDINGBOX ", 16) == 0) {
431 if (sscanf(ln + 16, "%d %d %d %d", &fbbw, &fbbh, &fbbox,
433 errx(1, "invalid FONTBOUNDINGBOX at line %u",
441 errx(1, "broken font header");
442 if (spc != 'c' && spc != 'C')
443 errx(1, "font spacing \"C\" (character cell) required");
445 /* Step 2: Validate DWIDTH (Device Width) of all glyphs. */
446 while ((ln = fgetln(fp, &length)) != NULL) {
448 ln[length - 1] = '\0';
450 if (strncmp(ln, "DWIDTH ", 7) == 0) {
451 if (sscanf(ln + 7, "%d %d", &dwidth, &dwy) != 2)
452 errx(1, "invalid DWIDTH at line %u", linenum);
453 if (dwy != 0 || (dwidth != fbbw && dwidth * 2 != fbbw))
454 errx(1, "bitmap with unsupported DWIDTH %d %d at line %u",
455 dwidth, dwy, linenum);
461 /* Step 3: Restart at the beginning of the file and read glyph data. */
462 dwidth = bbw = bbh = 0;
465 bbwbytes = 0; /* GCC 4.2.1 "may be used uninitialized" workaround. */
466 bytes = xmalloc(wbytes * height);
467 bytes_r = xmalloc(wbytes * height);
468 line = xmalloc(wbytes * 2);
469 while ((ln = fgetln(fp, &length)) != NULL) {
471 ln[length - 1] = '\0';
473 if (strncmp(ln, "ENCODING ", 9) == 0) {
474 curchar = atoi(ln + 9);
475 } else if (strncmp(ln, "DWIDTH ", 7) == 0) {
476 dwidth = atoi(ln + 7);
477 } else if (strncmp(ln, "BBX ", 4) == 0) {
478 if (sscanf(ln + 4, "%d %d %d %d", &bbw, &bbh, &bbox,
480 errx(1, "invalid BBX at line %u", linenum);
481 if (bbw < 1 || bbh < 1 || bbw > fbbw || bbh > fbbh ||
482 bbox < fbbox || bboy < fbboy ||
483 bbh + bboy > fbbh + fbboy)
484 errx(1, "broken bitmap with BBX %d %d %d %d at line %u",
485 bbw, bbh, bbox, bboy, linenum);
486 bbwbytes = howmany(bbw, 8);
487 } else if (strncmp(ln, "BITMAP", 6) == 0 &&
488 (ln[6] == ' ' || ln[6] == '\0')) {
489 if (dwidth == 0 || bbw == 0 || bbh == 0)
490 errx(1, "broken char header at line %u!",
492 memset(bytes, 0, wbytes * height);
493 memset(bytes_r, 0, wbytes * height);
496 * Assume that the next _bbh_ lines are bitmap data.
497 * ENDCHAR is allowed to terminate the bitmap
498 * early but is not otherwise checked; any extra data
501 for (i = (fbbh + fbboy) - (bbh + bboy);
502 i < (unsigned int)((fbbh + fbboy) - bboy); i++) {
503 if ((ln = fgetln(fp, &length)) == NULL)
504 errx(1, "unexpected EOF");
506 ln[length - 1] = '\0';
507 if (strcmp(ln, "ENDCHAR") == 0)
509 if (strlen(ln) < bbwbytes * 2)
510 errx(1, "broken bitmap at line %u",
512 memset(line, 0, wbytes * 2);
513 for (j = 0; j < bbwbytes; j++) {
515 if (sscanf(ln + j * 2, "%2x", &val) ==
518 *(line + j) = (uint8_t)val;
521 rshift_row(line, wbytes * 2, bbox - fbbox);
522 rv = split_row(bytes + i * wbytes,
523 bytes_r + i * wbytes, line, dwidth);
528 if (check_whitelist(curchar) == true) {
529 rv = add_char(curchar, map_idx, bytes,
530 dwidth > (int)width ? bytes_r : NULL);
535 dwidth = bbw = bbh = 0;
547 parse_hex(FILE *fp, unsigned int map_idx)
551 uint8_t *bytes = NULL, *bytes_r = NULL, *line = NULL;
552 unsigned curchar = 0, gwidth, gwbytes, i, j, chars_per_row;
555 while ((ln = fgetln(fp, &length)) != NULL) {
556 ln[length - 1] = '\0';
558 if (strncmp(ln, "# Height: ", 10) == 0) {
560 errx(1, "malformed input: Height tag after font data");
561 set_height(atoi(ln + 10));
562 } else if (strncmp(ln, "# Width: ", 9) == 0) {
564 errx(1, "malformed input: Width tag after font data");
565 set_width(atoi(ln + 9));
566 } else if (sscanf(ln, "%6x:", &curchar) == 1) {
568 bytes = xmalloc(wbytes * height);
569 bytes_r = xmalloc(wbytes * height);
570 line = xmalloc(wbytes * 2);
572 /* ln is guaranteed to have a colon here. */
573 p = strchr(ln, ':') + 1;
574 chars_per_row = strlen(p) / height;
575 if (chars_per_row < wbytes * 2)
577 "malformed input: broken bitmap, character %06x",
580 gwbytes = howmany(gwidth, 8);
581 if (chars_per_row < gwbytes * 2 || gwidth <= 8) {
582 gwidth = width; /* Single-width character. */
586 for (i = 0; i < height; i++) {
587 for (j = 0; j < gwbytes; j++) {
589 if (sscanf(p + j * 2, "%2x", &val) == 0)
591 *(line + j) = (uint8_t)val;
593 rv = split_row(bytes + i * wbytes,
594 bytes_r + i * wbytes, line, gwidth);
600 if (check_whitelist(curchar) == true) {
601 rv = add_char(curchar, map_idx, bytes,
602 gwidth != width ? bytes_r : NULL);
616 parse_file(const char *filename, unsigned int map_idx)
622 fp = fopen(filename, "r");
627 len = strlen(filename);
628 if (len > 4 && strcasecmp(filename + len - 4, ".hex") == 0)
629 rv = parse_hex(fp, map_idx);
631 rv = parse_bdf(fp, map_idx);
640 unsigned int i, idx = 0;
642 for (i = 0; i < VFNT_MAPS; i++)
643 TAILQ_FOREACH(gl, &glyphs[i], g_list)
647 /* Note we only deal with byte stream here. */
649 write_glyph_source(const void *ptr, size_t size, size_t nitems, FILE *stream)
651 const uint8_t *data = ptr;
655 for (i = 0; i < size; i++) {
656 if ((i % wbytes) == 0) {
657 if (fprintf(stream, "\n") < 0)
660 if (fprintf(stream, "0x%02x, ", data[i]) < 0)
663 if (fprintf(stream, "\n") < 0)
669 /* Write to buffer */
671 write_glyph_buf(const void *ptr, size_t size, size_t nitems,
672 FILE *stream __unused)
674 static size_t index = 0;
677 (void) memmove(uncompressed + index, ptr, size);
684 write_glyphs(FILE *fp, vt_write cb)
689 for (i = 0; i < VFNT_MAPS; i++) {
690 TAILQ_FOREACH(gl, &glyphs[i], g_list)
691 if (cb(gl->g_data, wbytes * height, 1, fp) != 1)
698 fold_mappings(unsigned int map_idx)
700 struct mapping_list *ml = &maps[map_idx];
701 struct mapping *mn, *mp, *mbase;
703 mp = mbase = TAILQ_FIRST(ml);
704 for (mp = mbase = TAILQ_FIRST(ml); mp != NULL; mp = mn) {
705 mn = TAILQ_NEXT(mp, m_list);
706 if (mn != NULL && mn->m_char == mp->m_char + 1 &&
707 mn->m_glyph->g_index == mp->m_glyph->g_index + 1)
709 mbase->m_length = mp->m_char - mbase->m_char + 1;
711 map_folded_count[map_idx]++;
716 write_mappings(FILE *fp, unsigned int map_idx)
718 struct mapping_list *ml = &maps[map_idx];
721 unsigned int i = 0, j = 0;
723 TAILQ_FOREACH(mp, ml, m_list) {
725 if (mp->m_length > 0) {
727 fm.vfm_src = htobe32(mp->m_char);
728 fm.vfm_dst = htobe16(mp->m_glyph->g_index);
729 fm.vfm_len = htobe16(mp->m_length - 1);
730 if (fwrite(&fm, sizeof fm, 1, fp) != 1)
739 write_source_mappings(FILE *fp, unsigned int map_idx)
741 struct mapping_list *ml = &maps[map_idx];
743 unsigned int i = 0, j = 0;
745 TAILQ_FOREACH(mp, ml, m_list) {
747 if (mp->m_length > 0) {
749 if (fprintf(fp, "\t{ 0x%08x, 0x%04x, 0x%04x },\n",
750 mp->m_char, mp->m_glyph->g_index,
751 mp->m_length - 1) < 0)
760 write_fnt(const char *filename)
763 struct font_header fh = {
764 .fh_magic = FONT_HEADER_MAGIC,
767 fp = fopen(filename, "wb");
774 fh.fh_height = height;
775 fh.fh_glyph_count = htobe32(glyph_unique);
776 fh.fh_map_count[0] = htobe32(map_folded_count[0]);
777 fh.fh_map_count[1] = htobe32(map_folded_count[1]);
778 fh.fh_map_count[2] = htobe32(map_folded_count[2]);
779 fh.fh_map_count[3] = htobe32(map_folded_count[3]);
780 if (fwrite(&fh, sizeof(fh), 1, fp) != 1) {
786 if (write_glyphs(fp, &fwrite) != 0 ||
787 write_mappings(fp, VFNT_MAP_NORMAL) != 0 ||
788 write_mappings(fp, VFNT_MAP_NORMAL_RIGHT) != 0 ||
789 write_mappings(fp, VFNT_MAP_BOLD) != 0 ||
790 write_mappings(fp, VFNT_MAP_BOLD_RIGHT) != 0) {
801 write_fnt_source(bool lz4, const char *filename)
805 size_t uncompressed_size = wbytes * height * glyph_unique;
806 size_t compressed_size = uncompressed_size;
807 uint8_t *compressed = NULL;
809 fp = fopen(filename, "w");
816 uncompressed = xmalloc(uncompressed_size);
817 compressed = xmalloc(uncompressed_size);
819 if (fprintf(fp, "/* Generated %ux%u console font source. */\n\n",
822 if (fprintf(fp, "#include <sys/types.h>\n") < 0)
824 if (fprintf(fp, "#include <sys/param.h>\n") < 0)
826 if (fprintf(fp, "#include <sys/font.h>\n\n") < 0)
829 /* Write font bytes. */
830 if (fprintf(fp, "static uint8_t FONTDATA_%ux%u[] = {\n",
834 if (write_glyphs(fp, &write_glyph_buf) != 0)
836 compressed_size = lz4_compress(uncompressed, compressed,
837 uncompressed_size, compressed_size, 0);
838 if (write_glyph_source(compressed, compressed_size, 1, fp) != 1)
843 if (write_glyphs(fp, &write_glyph_source) != 0)
846 if (fprintf(fp, "};\n\n") < 0)
849 /* Write font maps. */
850 if (!TAILQ_EMPTY(&maps[VFNT_MAP_NORMAL])) {
851 if (fprintf(fp, "static vfnt_map_t "
852 "FONTMAP_NORMAL_%ux%u[] = {\n", width, height) < 0)
854 if (write_source_mappings(fp, VFNT_MAP_NORMAL) != 0)
856 if (fprintf(fp, "};\n\n") < 0)
859 if (!TAILQ_EMPTY(&maps[VFNT_MAP_NORMAL_RIGHT])) {
860 if (fprintf(fp, "static vfnt_map_t "
861 "FONTMAP_NORMAL_RH_%ux%u[] = {\n", width, height) < 0)
863 if (write_source_mappings(fp, VFNT_MAP_NORMAL_RIGHT) != 0)
865 if (fprintf(fp, "};\n\n") < 0)
868 if (!TAILQ_EMPTY(&maps[VFNT_MAP_BOLD])) {
869 if (fprintf(fp, "static vfnt_map_t "
870 "FONTMAP_BOLD_%ux%u[] = {\n", width, height) < 0)
872 if (write_source_mappings(fp, VFNT_MAP_BOLD) != 0)
874 if (fprintf(fp, "};\n\n") < 0)
877 if (!TAILQ_EMPTY(&maps[VFNT_MAP_BOLD_RIGHT])) {
878 if (fprintf(fp, "static vfnt_map_t "
879 "FONTMAP_BOLD_RH_%ux%u[] = {\n", width, height) < 0)
881 if (write_source_mappings(fp, VFNT_MAP_BOLD_RIGHT) != 0)
883 if (fprintf(fp, "};\n\n") < 0)
887 /* Write struct font. */
888 if (fprintf(fp, "struct vt_font font_%ux%u = {\n",
891 if (fprintf(fp, "\t.vf_map\t= {\n") < 0)
893 if (TAILQ_EMPTY(&maps[VFNT_MAP_NORMAL])) {
894 if (fprintf(fp, "\t\t\tNULL,\n") < 0)
897 if (fprintf(fp, "\t\t\tFONTMAP_NORMAL_%ux%u,\n",
901 if (TAILQ_EMPTY(&maps[VFNT_MAP_NORMAL_RIGHT])) {
902 if (fprintf(fp, "\t\t\tNULL,\n") < 0)
905 if (fprintf(fp, "\t\t\tFONTMAP_NORMAL_RH_%ux%u,\n",
909 if (TAILQ_EMPTY(&maps[VFNT_MAP_BOLD])) {
910 if (fprintf(fp, "\t\t\tNULL,\n") < 0)
913 if (fprintf(fp, "\t\t\tFONTMAP_BOLD_%ux%u,\n",
917 if (TAILQ_EMPTY(&maps[VFNT_MAP_BOLD_RIGHT])) {
918 if (fprintf(fp, "\t\t\tNULL\n") < 0)
921 if (fprintf(fp, "\t\t\tFONTMAP_BOLD_RH_%ux%u\n",
925 if (fprintf(fp, "\t\t},\n") < 0)
928 if (fprintf(fp, "\t.vf_bytes\t= NULL,\n") < 0)
931 if (fprintf(fp, "\t.vf_bytes\t= FONTDATA_%ux%u,\n",
932 width, height) < 0) {
936 if (fprintf(fp, "\t.vf_width\t= %u,\n", width) < 0)
938 if (fprintf(fp, "\t.vf_height\t= %u,\n", height) < 0)
940 if (fprintf(fp, "\t.vf_map_count\t= { %u, %u, %u, %u }\n",
941 map_folded_count[0], map_folded_count[1], map_folded_count[2],
942 map_folded_count[3]) < 0) {
945 if (fprintf(fp, "};\n\n") < 0)
948 /* Write bitmap data. */
949 if (fprintf(fp, "vt_font_bitmap_data_t font_data_%ux%u = {\n",
952 if (fprintf(fp, "\t.vfbd_width\t= %u,\n", width) < 0)
954 if (fprintf(fp, "\t.vfbd_height\t= %u,\n", height) < 0)
957 if (fprintf(fp, "\t.vfbd_compressed_size\t= %zu,\n",
958 compressed_size) < 0) {
961 if (fprintf(fp, "\t.vfbd_uncompressed_size\t= %zu,\n",
962 uncompressed_size) < 0) {
965 if (fprintf(fp, "\t.vfbd_compressed_data\t= FONTDATA_%ux%u,\n",
966 width, height) < 0) {
970 if (fprintf(fp, "\t.vfbd_compressed_size\t= 0,\n") < 0)
972 if (fprintf(fp, "\t.vfbd_uncompressed_size\t= %zu,\n",
973 uncompressed_size) < 0) {
976 if (fprintf(fp, "\t.vfbd_compressed_data\t= NULL,\n") < 0)
979 if (fprintf(fp, "\t.vfbd_font = &font_%ux%u\n", width, height) < 0)
981 if (fprintf(fp, "};\n") < 0)
993 print_font_info(void)
999 "- glyph_total: %6u\n"
1000 "- glyph_normal: %6u\n"
1001 "- glyph_normal_right: %6u\n"
1002 "- glyph_bold: %6u\n"
1003 "- glyph_bold_right: %6u\n"
1004 "- glyph_unique: %6u\n"
1005 "- glyph_dupe: %6u\n"
1006 "- mapping_total: %6u\n"
1007 "- mapping_normal: %6u\n"
1008 "- mapping_normal_folded: %6u\n"
1009 "- mapping_normal_right: %6u\n"
1010 "- mapping_normal_right_folded: %6u\n"
1011 "- mapping_bold: %6u\n"
1012 "- mapping_bold_folded: %6u\n"
1013 "- mapping_bold_right: %6u\n"
1014 "- mapping_bold_right_folded: %6u\n"
1015 "- mapping_unique: %6u\n"
1016 "- mapping_dupe: %6u\n",
1023 glyph_unique, glyph_dupe,
1025 map_count[0], map_folded_count[0],
1026 map_count[1], map_folded_count[1],
1027 map_count[2], map_folded_count[2],
1028 map_count[3], map_folded_count[3],
1029 mapping_unique, mapping_dupe);
1033 main(int argc, char *argv[])
1035 int ch, verbose = 0, rv = 0;
1036 char *outfile = NULL;
1038 assert(sizeof(struct font_header) == 32);
1039 assert(sizeof(vfnt_map_t) == 8);
1041 while ((ch = getopt(argc, argv, "nf:h:vw:o:")) != -1) {
1044 if (strcmp(optarg, "font") == 0)
1046 else if (strcmp(optarg, "source") == 0)
1047 format = VT_C_SOURCE;
1048 else if (strcmp(optarg, "compressed-source") == 0)
1049 format = VT_C_COMPRESSED;
1051 errx(1, "Invalid format: %s", optarg);
1054 height = atoi(optarg);
1066 width = atoi(optarg);
1076 if (outfile == NULL && (argc < 2 || argc > 3))
1079 if (outfile == NULL) {
1080 outfile = argv[argc - 1];
1087 if (parse_file(argv[0], VFNT_MAP_NORMAL) != 0)
1092 if (parse_file(argv[0], VFNT_MAP_BOLD) != 0)
1098 dedup_mapping(VFNT_MAP_BOLD);
1099 dedup_mapping(VFNT_MAP_BOLD_RIGHT);
1107 rv = write_fnt(outfile);
1110 rv = write_fnt_source(false, outfile);
1112 case VT_C_COMPRESSED:
1113 rv = write_fnt_source(true, outfile);