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/types.h>
30 #include <sys/endian.h>
31 #include <sys/fnv_hash.h>
33 #include <sys/param.h>
34 #include <sys/queue.h>
46 #define VFNT_MAXGLYPHS 131072
47 #define VFNT_MAXDIMENSION 128
49 static unsigned int width = 8, wbytes, height = 16;
52 TAILQ_ENTRY(glyph) g_list;
53 SLIST_ENTRY(glyph) g_hash;
58 #define FONTCVT_NHASH 4096
59 TAILQ_HEAD(glyph_list, glyph);
60 static SLIST_HEAD(, glyph) glyph_hash[FONTCVT_NHASH];
61 static struct glyph_list glyphs[VFNT_MAPS] = {
62 TAILQ_HEAD_INITIALIZER(glyphs[0]),
63 TAILQ_HEAD_INITIALIZER(glyphs[1]),
64 TAILQ_HEAD_INITIALIZER(glyphs[2]),
65 TAILQ_HEAD_INITIALIZER(glyphs[3]),
67 static unsigned int glyph_total, glyph_count[4], glyph_unique, glyph_dupe;
70 TAILQ_ENTRY(mapping) m_list;
72 unsigned int m_length;
73 struct glyph *m_glyph;
76 TAILQ_HEAD(mapping_list, mapping);
77 static struct mapping_list maps[VFNT_MAPS] = {
78 TAILQ_HEAD_INITIALIZER(maps[0]),
79 TAILQ_HEAD_INITIALIZER(maps[1]),
80 TAILQ_HEAD_INITIALIZER(maps[2]),
81 TAILQ_HEAD_INITIALIZER(maps[3]),
83 static unsigned int mapping_total, map_count[4], map_folded_count[4],
84 mapping_unique, mapping_dupe;
87 VT_FONT, /* default */
88 VT_C_SOURCE, /* C source for built in fonts */
89 VT_C_COMPRESSED /* C source with compressed font data */
98 * Compressed font glyph list. To be used with boot loader, we need to have
99 * ascii set and box drawing chars.
101 static struct whitelist c_list[] = {
102 { .c = 0, .len = 0 }, /* deault char */
103 { .c = 0x20, .len = 0x5f },
104 { .c = 0x2500, .len = 0 }, /* single frame */
105 { .c = 0x2502, .len = 0 },
106 { .c = 0x250c, .len = 0 },
107 { .c = 0x2510, .len = 0 },
108 { .c = 0x2514, .len = 0 },
109 { .c = 0x2518, .len = 0 },
110 { .c = 0x2550, .len = 1 }, /* double frame */
111 { .c = 0x2554, .len = 0 },
112 { .c = 0x2557, .len = 0 },
113 { .c = 0x255a, .len = 0 },
114 { .c = 0x255d, .len = 0 },
118 * Uncompressed source. For x86 we need cp437 so the vga text mode
119 * can program font into the vga card.
121 static struct whitelist s_list[] = {
122 { .c = 0, .len = 0 }, /* deault char */
123 { .c = 0x20, .len = 0x5f }, /* ascii set */
124 { .c = 0xA0, .len = 0x5f }, /* latin 1 */
125 { .c = 0x0192, .len = 0 },
126 { .c = 0x0332, .len = 0 }, /* composing lower line */
127 { .c = 0x0393, .len = 0 },
128 { .c = 0x0398, .len = 0 },
129 { .c = 0x03A3, .len = 0 },
130 { .c = 0x03A6, .len = 0 },
131 { .c = 0x03A9, .len = 0 },
132 { .c = 0x03B1, .len = 1 },
133 { .c = 0x03B4, .len = 0 },
134 { .c = 0x03C0, .len = 0 },
135 { .c = 0x03C3, .len = 0 },
136 { .c = 0x03C4, .len = 0 },
137 { .c = 0x207F, .len = 0 },
138 { .c = 0x20A7, .len = 0 },
139 { .c = 0x2205, .len = 0 },
140 { .c = 0x220A, .len = 0 },
141 { .c = 0x2219, .len = 1 },
142 { .c = 0x221E, .len = 0 },
143 { .c = 0x2229, .len = 0 },
144 { .c = 0x2248, .len = 0 },
145 { .c = 0x2261, .len = 0 },
146 { .c = 0x2264, .len = 1 },
147 { .c = 0x2310, .len = 0 },
148 { .c = 0x2320, .len = 1 },
149 { .c = 0x2500, .len = 0 },
150 { .c = 0x2502, .len = 0 },
151 { .c = 0x250C, .len = 0 },
152 { .c = 0x2510, .len = 0 },
153 { .c = 0x2514, .len = 0 },
154 { .c = 0x2518, .len = 0 },
155 { .c = 0x251C, .len = 0 },
156 { .c = 0x2524, .len = 0 },
157 { .c = 0x252C, .len = 0 },
158 { .c = 0x2534, .len = 0 },
159 { .c = 0x253C, .len = 0 },
160 { .c = 0x2550, .len = 0x1c },
161 { .c = 0x2580, .len = 0 },
162 { .c = 0x2584, .len = 0 },
163 { .c = 0x2588, .len = 0 },
164 { .c = 0x258C, .len = 0 },
165 { .c = 0x2590, .len = 3 },
166 { .c = 0x25A0, .len = 0 },
169 static bool filter = true;
170 static enum output_format format = VT_FONT;
171 /* Type for write callback. */
172 typedef size_t (*vt_write)(const void *, size_t, size_t, FILE *);
173 static uint8_t *uncompressed;
179 (void)fprintf(stderr, "usage: vtfontcvt "
180 "[-nv] [-f format] [-h height] [-w width]\n"
181 "\t-o output_file normal_font [bold_font]\n");
190 if ((m = calloc(1, size)) == NULL)
191 errx(1, "memory allocation failure");
196 add_mapping(struct glyph *gl, unsigned int c, unsigned int map_idx)
198 struct mapping *mp, *mp_temp;
199 struct mapping_list *ml;
203 mp = xmalloc(sizeof *mp);
209 if (TAILQ_LAST(ml, mapping_list) == NULL ||
210 TAILQ_LAST(ml, mapping_list)->m_char < c) {
211 /* Common case: empty list or new char at end of list. */
212 TAILQ_INSERT_TAIL(ml, mp, m_list);
214 /* Find insertion point for char; cannot be at end. */
215 TAILQ_FOREACH(mp_temp, ml, m_list) {
216 if (mp_temp->m_char >= c) {
217 TAILQ_INSERT_BEFORE(mp_temp, mp, m_list);
223 map_count[map_idx]++;
230 dedup_mapping(unsigned int map_idx)
232 struct mapping *mp_bold, *mp_normal, *mp_temp;
233 unsigned normal_map_idx = map_idx - VFNT_MAP_BOLD;
235 assert(map_idx == VFNT_MAP_BOLD || map_idx == VFNT_MAP_BOLD_RIGHT);
236 mp_normal = TAILQ_FIRST(&maps[normal_map_idx]);
237 TAILQ_FOREACH_SAFE(mp_bold, &maps[map_idx], m_list, mp_temp) {
238 while (mp_normal->m_char < mp_bold->m_char)
239 mp_normal = TAILQ_NEXT(mp_normal, m_list);
240 if (mp_bold->m_char != mp_normal->m_char)
241 errx(1, "Character %u not in normal font!",
243 if (mp_bold->m_glyph != mp_normal->m_glyph)
246 /* No mapping is needed if it's equal to the normal mapping. */
247 TAILQ_REMOVE(&maps[map_idx], mp_bold, m_list);
254 static struct glyph *
255 add_glyph(const uint8_t *bytes, unsigned int map_idx, int fallback)
261 glyph_count[map_idx]++;
263 /* Return existing glyph if we have an identical one. */
264 hash = fnv_32_buf(bytes, wbytes * height, FNV1_32_INIT) % FONTCVT_NHASH;
265 SLIST_FOREACH(gl, &glyph_hash[hash], g_hash) {
266 if (memcmp(gl->g_data, bytes, wbytes * height) == 0) {
272 /* Allocate new glyph. */
273 gl = xmalloc(sizeof *gl);
274 gl->g_data = xmalloc(wbytes * height);
275 memcpy(gl->g_data, bytes, wbytes * height);
277 TAILQ_INSERT_HEAD(&glyphs[map_idx], gl, g_list);
279 TAILQ_INSERT_TAIL(&glyphs[map_idx], gl, g_list);
280 SLIST_INSERT_HEAD(&glyph_hash[hash], gl, g_hash);
283 if (glyph_unique > VFNT_MAXGLYPHS)
284 errx(1, "too many glyphs (%u)", glyph_unique);
289 check_whitelist(unsigned c)
291 struct whitelist *w = NULL;
297 if (format == VT_C_SOURCE) {
299 n = sizeof(s_list) / sizeof(s_list[0]);
301 if (format == VT_C_COMPRESSED) {
303 n = sizeof(c_list) / sizeof(c_list[0]);
307 for (i = 0; i < n; i++) {
308 if (c >= w[i].c && c <= w[i].c + w[i].len)
315 add_char(unsigned curchar, unsigned map_idx, uint8_t *bytes, uint8_t *bytes_r)
319 /* Prevent adding two glyphs for 0xFFFD */
320 if (curchar == 0xFFFD) {
321 if (map_idx < VFNT_MAP_BOLD)
322 gl = add_glyph(bytes, 0, 1);
323 } else if (filter == false || curchar >= 0x20) {
324 gl = add_glyph(bytes, map_idx, 0);
325 if (add_mapping(gl, curchar, map_idx) != 0)
327 if (bytes_r != NULL) {
328 gl = add_glyph(bytes_r, map_idx + 1, 0);
329 if (add_mapping(gl, curchar, map_idx + 1) != 0)
337 * Right-shift glyph row.
340 rshift_row(uint8_t *buf, size_t len, size_t shift)
342 ssize_t i, off_byte = shift / 8;
343 size_t off_bit = shift % 8;
347 for (i = len - 1; i >= 0; i--)
348 buf[i] = (i >= off_byte ? buf[i - off_byte] >> off_bit : 0) |
349 (i > off_byte ? buf[i - off_byte - 1] << (8 - off_bit) : 0);
353 * Split double-width characters into left and right half. Single-width
354 * characters in _left_ only.
357 split_row(uint8_t *left, uint8_t *right, uint8_t *line, size_t w)
361 s = wbytes * 8 - width;
363 memcpy(left, line, wbytes);
364 *(left + wbytes - 1) &= 0xFF << s;
366 if (w > width) { /* Double-width character. */
369 for (i = 0; i < wbytes; i++) {
370 t = *(line + wbytes + i - 1);
372 t |= *(line + wbytes + i) >> s;
375 *(right + wbytes - 1) &= 0xFF << s;
383 if (h <= 0 || h > VFNT_MAXDIMENSION)
384 errx(1, "invalid height %d", h);
391 if (w <= 0 || w > VFNT_MAXDIMENSION)
392 errx(1, "invalid width %d", w);
394 wbytes = howmany(width, 8);
398 parse_bdf(FILE *fp, unsigned int map_idx)
402 uint8_t *line, *bytes, *bytes_r;
403 unsigned int curchar = 0, i, j, linenum = 0, bbwbytes;
404 int bbw, bbh, bbox, bboy; /* Glyph bounding box. */
405 int fbbw = 0, fbbh, fbbox, fbboy; /* Font bounding box. */
406 int dwidth = 0, dwy = 0;
411 * Step 1: Parse FONT logical font descriptor and FONTBOUNDINGBOX
414 while ((ln = fgetln(fp, &length)) != NULL) {
416 ln[length - 1] = '\0';
418 if (strncmp(ln, "FONT ", 5) == 0) {
421 while ((p = strchr(p, '-')) != NULL) {
429 } else if (strncmp(ln, "FONTBOUNDINGBOX ", 16) == 0) {
430 if (sscanf(ln + 16, "%d %d %d %d", &fbbw, &fbbh, &fbbox,
432 errx(1, "invalid FONTBOUNDINGBOX at line %u",
440 errx(1, "broken font header");
441 if (spc != 'c' && spc != 'C')
442 errx(1, "font spacing \"C\" (character cell) required");
444 /* Step 2: Validate DWIDTH (Device Width) of all glyphs. */
445 while ((ln = fgetln(fp, &length)) != NULL) {
447 ln[length - 1] = '\0';
449 if (strncmp(ln, "DWIDTH ", 7) == 0) {
450 if (sscanf(ln + 7, "%d %d", &dwidth, &dwy) != 2)
451 errx(1, "invalid DWIDTH at line %u", linenum);
452 if (dwy != 0 || (dwidth != fbbw && dwidth * 2 != fbbw))
453 errx(1, "bitmap with unsupported DWIDTH %d %d at line %u",
454 dwidth, dwy, linenum);
460 /* Step 3: Restart at the beginning of the file and read glyph data. */
461 dwidth = bbw = bbh = 0;
464 bbwbytes = 0; /* GCC 4.2.1 "may be used uninitialized" workaround. */
465 bytes = xmalloc(wbytes * height);
466 bytes_r = xmalloc(wbytes * height);
467 line = xmalloc(wbytes * 2);
468 while ((ln = fgetln(fp, &length)) != NULL) {
470 ln[length - 1] = '\0';
472 if (strncmp(ln, "ENCODING ", 9) == 0) {
473 curchar = atoi(ln + 9);
474 } else if (strncmp(ln, "DWIDTH ", 7) == 0) {
475 dwidth = atoi(ln + 7);
476 } else if (strncmp(ln, "BBX ", 4) == 0) {
477 if (sscanf(ln + 4, "%d %d %d %d", &bbw, &bbh, &bbox,
479 errx(1, "invalid BBX at line %u", linenum);
480 if (bbw < 1 || bbh < 1 || bbw > fbbw || bbh > fbbh ||
481 bbox < fbbox || bboy < fbboy ||
482 bbh + bboy > fbbh + fbboy)
483 errx(1, "broken bitmap with BBX %d %d %d %d at line %u",
484 bbw, bbh, bbox, bboy, linenum);
485 bbwbytes = howmany(bbw, 8);
486 } else if (strncmp(ln, "BITMAP", 6) == 0 &&
487 (ln[6] == ' ' || ln[6] == '\0')) {
488 if (dwidth == 0 || bbw == 0 || bbh == 0)
489 errx(1, "broken char header at line %u!",
491 memset(bytes, 0, wbytes * height);
492 memset(bytes_r, 0, wbytes * height);
495 * Assume that the next _bbh_ lines are bitmap data.
496 * ENDCHAR is allowed to terminate the bitmap
497 * early but is not otherwise checked; any extra data
500 for (i = (fbbh + fbboy) - (bbh + bboy);
501 i < (unsigned int)((fbbh + fbboy) - bboy); i++) {
502 if ((ln = fgetln(fp, &length)) == NULL)
503 errx(1, "unexpected EOF");
505 ln[length - 1] = '\0';
506 if (strcmp(ln, "ENDCHAR") == 0)
508 if (strlen(ln) < bbwbytes * 2)
509 errx(1, "broken bitmap at line %u",
511 memset(line, 0, wbytes * 2);
512 for (j = 0; j < bbwbytes; j++) {
514 if (sscanf(ln + j * 2, "%2x", &val) ==
517 *(line + j) = (uint8_t)val;
520 rshift_row(line, wbytes * 2, bbox - fbbox);
521 rv = split_row(bytes + i * wbytes,
522 bytes_r + i * wbytes, line, dwidth);
527 if (check_whitelist(curchar) == true) {
528 rv = add_char(curchar, map_idx, bytes,
529 dwidth > (int)width ? bytes_r : NULL);
534 dwidth = bbw = bbh = 0;
546 parse_hex(FILE *fp, unsigned int map_idx)
550 uint8_t *bytes = NULL, *bytes_r = NULL, *line = NULL;
551 unsigned curchar = 0, gwidth, gwbytes, i, j, chars_per_row;
554 while ((ln = fgetln(fp, &length)) != NULL) {
555 ln[length - 1] = '\0';
557 if (strncmp(ln, "# Height: ", 10) == 0) {
559 errx(1, "malformed input: Height tag after font data");
560 set_height(atoi(ln + 10));
561 } else if (strncmp(ln, "# Width: ", 9) == 0) {
563 errx(1, "malformed input: Width tag after font data");
564 set_width(atoi(ln + 9));
565 } else if (sscanf(ln, "%6x:", &curchar) == 1) {
567 bytes = xmalloc(wbytes * height);
568 bytes_r = xmalloc(wbytes * height);
569 line = xmalloc(wbytes * 2);
571 /* ln is guaranteed to have a colon here. */
572 p = strchr(ln, ':') + 1;
573 chars_per_row = strlen(p) / height;
574 if (chars_per_row < wbytes * 2)
576 "malformed input: broken bitmap, character %06x",
579 gwbytes = howmany(gwidth, 8);
580 if (chars_per_row < gwbytes * 2 || gwidth <= 8) {
581 gwidth = width; /* Single-width character. */
585 for (i = 0; i < height; i++) {
586 for (j = 0; j < gwbytes; j++) {
588 if (sscanf(p + j * 2, "%2x", &val) == 0)
590 *(line + j) = (uint8_t)val;
592 rv = split_row(bytes + i * wbytes,
593 bytes_r + i * wbytes, line, gwidth);
599 if (check_whitelist(curchar) == true) {
600 rv = add_char(curchar, map_idx, bytes,
601 gwidth != width ? bytes_r : NULL);
615 parse_file(const char *filename, unsigned int map_idx)
621 fp = fopen(filename, "r");
626 len = strlen(filename);
627 if (len > 4 && strcasecmp(filename + len - 4, ".hex") == 0)
628 rv = parse_hex(fp, map_idx);
630 rv = parse_bdf(fp, map_idx);
639 unsigned int i, idx = 0;
641 for (i = 0; i < VFNT_MAPS; i++)
642 TAILQ_FOREACH(gl, &glyphs[i], g_list)
646 /* Note we only deal with byte stream here. */
648 write_glyph_source(const void *ptr, size_t size, size_t nitems, FILE *stream)
650 const uint8_t *data = ptr;
654 for (i = 0; i < size; i++) {
655 if ((i % wbytes) == 0) {
656 if (fprintf(stream, "\n") < 0)
659 if (fprintf(stream, "0x%02x, ", data[i]) < 0)
662 if (fprintf(stream, "\n") < 0)
668 /* Write to buffer */
670 write_glyph_buf(const void *ptr, size_t size, size_t nitems,
671 FILE *stream __unused)
673 static size_t index = 0;
676 (void) memmove(uncompressed + index, ptr, size);
683 write_glyphs(FILE *fp, vt_write cb)
688 for (i = 0; i < VFNT_MAPS; i++) {
689 TAILQ_FOREACH(gl, &glyphs[i], g_list)
690 if (cb(gl->g_data, wbytes * height, 1, fp) != 1)
697 fold_mappings(unsigned int map_idx)
699 struct mapping_list *ml = &maps[map_idx];
700 struct mapping *mn, *mp, *mbase;
702 mp = mbase = TAILQ_FIRST(ml);
703 for (mp = mbase = TAILQ_FIRST(ml); mp != NULL; mp = mn) {
704 mn = TAILQ_NEXT(mp, m_list);
705 if (mn != NULL && mn->m_char == mp->m_char + 1 &&
706 mn->m_glyph->g_index == mp->m_glyph->g_index + 1)
708 mbase->m_length = mp->m_char - mbase->m_char + 1;
710 map_folded_count[map_idx]++;
715 write_mappings(FILE *fp, unsigned int map_idx)
717 struct mapping_list *ml = &maps[map_idx];
720 unsigned int i = 0, j = 0;
722 TAILQ_FOREACH(mp, ml, m_list) {
724 if (mp->m_length > 0) {
726 fm.vfm_src = htobe32(mp->m_char);
727 fm.vfm_dst = htobe16(mp->m_glyph->g_index);
728 fm.vfm_len = htobe16(mp->m_length - 1);
729 if (fwrite(&fm, sizeof fm, 1, fp) != 1)
738 write_source_mappings(FILE *fp, unsigned int map_idx)
740 struct mapping_list *ml = &maps[map_idx];
742 unsigned int i = 0, j = 0;
744 TAILQ_FOREACH(mp, ml, m_list) {
746 if (mp->m_length > 0) {
748 if (fprintf(fp, "\t{ 0x%08x, 0x%04x, 0x%04x },\n",
749 mp->m_char, mp->m_glyph->g_index,
750 mp->m_length - 1) < 0)
759 write_fnt(const char *filename)
762 struct font_header fh = {
763 .fh_magic = FONT_HEADER_MAGIC,
766 fp = fopen(filename, "wb");
773 fh.fh_height = height;
774 fh.fh_glyph_count = htobe32(glyph_unique);
775 fh.fh_map_count[0] = htobe32(map_folded_count[0]);
776 fh.fh_map_count[1] = htobe32(map_folded_count[1]);
777 fh.fh_map_count[2] = htobe32(map_folded_count[2]);
778 fh.fh_map_count[3] = htobe32(map_folded_count[3]);
779 if (fwrite(&fh, sizeof(fh), 1, fp) != 1) {
785 if (write_glyphs(fp, &fwrite) != 0 ||
786 write_mappings(fp, VFNT_MAP_NORMAL) != 0 ||
787 write_mappings(fp, VFNT_MAP_NORMAL_RIGHT) != 0 ||
788 write_mappings(fp, VFNT_MAP_BOLD) != 0 ||
789 write_mappings(fp, VFNT_MAP_BOLD_RIGHT) != 0) {
800 write_fnt_source(bool lz4, const char *filename)
804 size_t uncompressed_size = wbytes * height * glyph_unique;
805 size_t compressed_size = uncompressed_size;
806 uint8_t *compressed = NULL;
808 fp = fopen(filename, "w");
815 uncompressed = xmalloc(uncompressed_size);
816 compressed = xmalloc(uncompressed_size);
818 if (fprintf(fp, "/* Generated %ux%u console font source. */\n\n",
821 if (fprintf(fp, "#include <sys/types.h>\n") < 0)
823 if (fprintf(fp, "#include <sys/param.h>\n") < 0)
825 if (fprintf(fp, "#include <sys/font.h>\n\n") < 0)
828 /* Write font bytes. */
829 if (fprintf(fp, "static uint8_t FONTDATA_%ux%u[] = {\n",
833 if (write_glyphs(fp, &write_glyph_buf) != 0)
835 compressed_size = lz4_compress(uncompressed, compressed,
836 uncompressed_size, compressed_size, 0);
837 if (write_glyph_source(compressed, compressed_size, 1, fp) != 1)
842 if (write_glyphs(fp, &write_glyph_source) != 0)
845 if (fprintf(fp, "};\n\n") < 0)
848 /* Write font maps. */
849 if (!TAILQ_EMPTY(&maps[VFNT_MAP_NORMAL])) {
850 if (fprintf(fp, "static vfnt_map_t "
851 "FONTMAP_NORMAL_%ux%u[] = {\n", width, height) < 0)
853 if (write_source_mappings(fp, VFNT_MAP_NORMAL) != 0)
855 if (fprintf(fp, "};\n\n") < 0)
858 if (!TAILQ_EMPTY(&maps[VFNT_MAP_NORMAL_RIGHT])) {
859 if (fprintf(fp, "static vfnt_map_t "
860 "FONTMAP_NORMAL_RH_%ux%u[] = {\n", width, height) < 0)
862 if (write_source_mappings(fp, VFNT_MAP_NORMAL_RIGHT) != 0)
864 if (fprintf(fp, "};\n\n") < 0)
867 if (!TAILQ_EMPTY(&maps[VFNT_MAP_BOLD])) {
868 if (fprintf(fp, "static vfnt_map_t "
869 "FONTMAP_BOLD_%ux%u[] = {\n", width, height) < 0)
871 if (write_source_mappings(fp, VFNT_MAP_BOLD) != 0)
873 if (fprintf(fp, "};\n\n") < 0)
876 if (!TAILQ_EMPTY(&maps[VFNT_MAP_BOLD_RIGHT])) {
877 if (fprintf(fp, "static vfnt_map_t "
878 "FONTMAP_BOLD_RH_%ux%u[] = {\n", width, height) < 0)
880 if (write_source_mappings(fp, VFNT_MAP_BOLD_RIGHT) != 0)
882 if (fprintf(fp, "};\n\n") < 0)
886 /* Write struct font. */
887 if (fprintf(fp, "struct vt_font font_%ux%u = {\n",
890 if (fprintf(fp, "\t.vf_map\t= {\n") < 0)
892 if (TAILQ_EMPTY(&maps[VFNT_MAP_NORMAL])) {
893 if (fprintf(fp, "\t\t\tNULL,\n") < 0)
896 if (fprintf(fp, "\t\t\tFONTMAP_NORMAL_%ux%u,\n",
900 if (TAILQ_EMPTY(&maps[VFNT_MAP_NORMAL_RIGHT])) {
901 if (fprintf(fp, "\t\t\tNULL,\n") < 0)
904 if (fprintf(fp, "\t\t\tFONTMAP_NORMAL_RH_%ux%u,\n",
908 if (TAILQ_EMPTY(&maps[VFNT_MAP_BOLD])) {
909 if (fprintf(fp, "\t\t\tNULL,\n") < 0)
912 if (fprintf(fp, "\t\t\tFONTMAP_BOLD_%ux%u,\n",
916 if (TAILQ_EMPTY(&maps[VFNT_MAP_BOLD_RIGHT])) {
917 if (fprintf(fp, "\t\t\tNULL\n") < 0)
920 if (fprintf(fp, "\t\t\tFONTMAP_BOLD_RH_%ux%u\n",
924 if (fprintf(fp, "\t\t},\n") < 0)
927 if (fprintf(fp, "\t.vf_bytes\t= NULL,\n") < 0)
930 if (fprintf(fp, "\t.vf_bytes\t= FONTDATA_%ux%u,\n",
931 width, height) < 0) {
935 if (fprintf(fp, "\t.vf_width\t= %u,\n", width) < 0)
937 if (fprintf(fp, "\t.vf_height\t= %u,\n", height) < 0)
939 if (fprintf(fp, "\t.vf_map_count\t= { %u, %u, %u, %u }\n",
940 map_folded_count[0], map_folded_count[1], map_folded_count[2],
941 map_folded_count[3]) < 0) {
944 if (fprintf(fp, "};\n\n") < 0)
947 /* Write bitmap data. */
948 if (fprintf(fp, "vt_font_bitmap_data_t font_data_%ux%u = {\n",
951 if (fprintf(fp, "\t.vfbd_width\t= %u,\n", width) < 0)
953 if (fprintf(fp, "\t.vfbd_height\t= %u,\n", height) < 0)
956 if (fprintf(fp, "\t.vfbd_compressed_size\t= %zu,\n",
957 compressed_size) < 0) {
960 if (fprintf(fp, "\t.vfbd_uncompressed_size\t= %zu,\n",
961 uncompressed_size) < 0) {
964 if (fprintf(fp, "\t.vfbd_compressed_data\t= FONTDATA_%ux%u,\n",
965 width, height) < 0) {
969 if (fprintf(fp, "\t.vfbd_compressed_size\t= 0,\n") < 0)
971 if (fprintf(fp, "\t.vfbd_uncompressed_size\t= %zu,\n",
972 uncompressed_size) < 0) {
975 if (fprintf(fp, "\t.vfbd_compressed_data\t= NULL,\n") < 0)
978 if (fprintf(fp, "\t.vfbd_font = &font_%ux%u\n", width, height) < 0)
980 if (fprintf(fp, "};\n") < 0)
992 print_font_info(void)
998 "- glyph_total: %6u\n"
999 "- glyph_normal: %6u\n"
1000 "- glyph_normal_right: %6u\n"
1001 "- glyph_bold: %6u\n"
1002 "- glyph_bold_right: %6u\n"
1003 "- glyph_unique: %6u\n"
1004 "- glyph_dupe: %6u\n"
1005 "- mapping_total: %6u\n"
1006 "- mapping_normal: %6u\n"
1007 "- mapping_normal_folded: %6u\n"
1008 "- mapping_normal_right: %6u\n"
1009 "- mapping_normal_right_folded: %6u\n"
1010 "- mapping_bold: %6u\n"
1011 "- mapping_bold_folded: %6u\n"
1012 "- mapping_bold_right: %6u\n"
1013 "- mapping_bold_right_folded: %6u\n"
1014 "- mapping_unique: %6u\n"
1015 "- mapping_dupe: %6u\n",
1022 glyph_unique, glyph_dupe,
1024 map_count[0], map_folded_count[0],
1025 map_count[1], map_folded_count[1],
1026 map_count[2], map_folded_count[2],
1027 map_count[3], map_folded_count[3],
1028 mapping_unique, mapping_dupe);
1032 main(int argc, char *argv[])
1034 int ch, verbose = 0, rv = 0;
1035 char *outfile = NULL;
1037 assert(sizeof(struct font_header) == 32);
1038 assert(sizeof(vfnt_map_t) == 8);
1040 while ((ch = getopt(argc, argv, "nf:h:vw:o:")) != -1) {
1043 if (strcmp(optarg, "font") == 0)
1045 else if (strcmp(optarg, "source") == 0)
1046 format = VT_C_SOURCE;
1047 else if (strcmp(optarg, "compressed-source") == 0)
1048 format = VT_C_COMPRESSED;
1050 errx(1, "Invalid format: %s", optarg);
1053 height = atoi(optarg);
1065 width = atoi(optarg);
1075 if (outfile == NULL && (argc < 2 || argc > 3))
1078 if (outfile == NULL) {
1079 outfile = argv[argc - 1];
1086 if (parse_file(argv[0], VFNT_MAP_NORMAL) != 0)
1091 if (parse_file(argv[0], VFNT_MAP_BOLD) != 0)
1097 dedup_mapping(VFNT_MAP_BOLD);
1098 dedup_mapping(VFNT_MAP_BOLD_RIGHT);
1106 rv = write_fnt(outfile);
1109 rv = write_fnt_source(false, outfile);
1111 case VT_C_COMPRESSED:
1112 rv = write_fnt_source(true, outfile);