2 * Copyright (c) 2009, 2014 The FreeBSD Foundation
5 * This software was developed by Ed Schouten under sponsorship from the
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
34 #include <sys/fnv_hash.h>
35 #include <sys/endian.h>
36 #include <sys/param.h>
37 #include <sys/queue.h>
50 #define VFNT_MAXGLYPHS 131072
51 #define VFNT_MAXDIMENSION 128
53 static unsigned int width = 8, wbytes, height = 16;
56 TAILQ_ENTRY(glyph) g_list;
57 SLIST_ENTRY(glyph) g_hash;
62 #define FONTCVT_NHASH 4096
63 TAILQ_HEAD(glyph_list, glyph);
64 static SLIST_HEAD(, glyph) glyph_hash[FONTCVT_NHASH];
65 static struct glyph_list glyphs[VFNT_MAPS] = {
66 TAILQ_HEAD_INITIALIZER(glyphs[0]),
67 TAILQ_HEAD_INITIALIZER(glyphs[1]),
68 TAILQ_HEAD_INITIALIZER(glyphs[2]),
69 TAILQ_HEAD_INITIALIZER(glyphs[3]),
71 static unsigned int glyph_total, glyph_count[4], glyph_unique, glyph_dupe;
74 TAILQ_ENTRY(mapping) m_list;
76 unsigned int m_length;
77 struct glyph *m_glyph;
80 TAILQ_HEAD(mapping_list, mapping);
81 static struct mapping_list maps[VFNT_MAPS] = {
82 TAILQ_HEAD_INITIALIZER(maps[0]),
83 TAILQ_HEAD_INITIALIZER(maps[1]),
84 TAILQ_HEAD_INITIALIZER(maps[2]),
85 TAILQ_HEAD_INITIALIZER(maps[3]),
87 static unsigned int mapping_total, map_count[4], map_folded_count[4],
88 mapping_unique, mapping_dupe;
91 VT_FONT, /* default */
92 VT_C_SOURCE, /* C source for built in fonts */
93 VT_C_COMPRESSED /* C source with compressed font data */
102 * Compressed font glyph list. To be used with boot loader, we need to have
103 * ascii set and box drawing chars.
105 static struct whitelist c_list[] = {
106 { .c = 0, .len = 0 }, /* deault char */
107 { .c = 0x20, .len = 0x5f },
108 { .c = 0x2500, .len = 0 }, /* single frame */
109 { .c = 0x2502, .len = 0 },
110 { .c = 0x250c, .len = 0 },
111 { .c = 0x2510, .len = 0 },
112 { .c = 0x2514, .len = 0 },
113 { .c = 0x2518, .len = 0 },
114 { .c = 0x2550, .len = 1 }, /* double frame */
115 { .c = 0x2554, .len = 0 },
116 { .c = 0x2557, .len = 0 },
117 { .c = 0x255a, .len = 0 },
118 { .c = 0x255d, .len = 0 },
122 * Uncompressed source. For x86 we need cp437 so the vga text mode
123 * can program font into the vga card.
125 static struct whitelist s_list[] = {
126 { .c = 0, .len = 0 }, /* deault char */
127 { .c = 0x20, .len = 0x5f }, /* ascii set */
128 { .c = 0xA0, .len = 0x5f }, /* latin 1 */
129 { .c = 0x0192, .len = 0 },
130 { .c = 0x0332, .len = 0 }, /* composing lower line */
131 { .c = 0x0393, .len = 0 },
132 { .c = 0x0398, .len = 0 },
133 { .c = 0x03A3, .len = 0 },
134 { .c = 0x03A6, .len = 0 },
135 { .c = 0x03A9, .len = 0 },
136 { .c = 0x03B1, .len = 1 },
137 { .c = 0x03B4, .len = 0 },
138 { .c = 0x03C0, .len = 0 },
139 { .c = 0x03C3, .len = 0 },
140 { .c = 0x03C4, .len = 0 },
141 { .c = 0x207F, .len = 0 },
142 { .c = 0x20A7, .len = 0 },
143 { .c = 0x2205, .len = 0 },
144 { .c = 0x220A, .len = 0 },
145 { .c = 0x2219, .len = 1 },
146 { .c = 0x221E, .len = 0 },
147 { .c = 0x2229, .len = 0 },
148 { .c = 0x2248, .len = 0 },
149 { .c = 0x2261, .len = 0 },
150 { .c = 0x2264, .len = 1 },
151 { .c = 0x2310, .len = 0 },
152 { .c = 0x2320, .len = 1 },
153 { .c = 0x2500, .len = 0 },
154 { .c = 0x2502, .len = 0 },
155 { .c = 0x250C, .len = 0 },
156 { .c = 0x2510, .len = 0 },
157 { .c = 0x2514, .len = 0 },
158 { .c = 0x2518, .len = 0 },
159 { .c = 0x251C, .len = 0 },
160 { .c = 0x2524, .len = 0 },
161 { .c = 0x252C, .len = 0 },
162 { .c = 0x2534, .len = 0 },
163 { .c = 0x253C, .len = 0 },
164 { .c = 0x2550, .len = 0x1c },
165 { .c = 0x2580, .len = 0 },
166 { .c = 0x2584, .len = 0 },
167 { .c = 0x2588, .len = 0 },
168 { .c = 0x258C, .len = 0 },
169 { .c = 0x2590, .len = 3 },
170 { .c = 0x25A0, .len = 0 },
173 static bool filter = true;
174 static enum output_format format = VT_FONT;
175 /* Type for write callback. */
176 typedef size_t (*vt_write)(const void *, size_t, size_t, FILE *);
177 static uint8_t *uncompressed;
183 (void)fprintf(stderr, "usage: vtfontcvt "
184 "[-n] [-f font|source|compressed-source] [-w width] "
185 "[-h height]\n\t[-v] normal.bdf [bold.bdf] out.fnt\n");
194 if ((m = calloc(1, size)) == NULL)
195 errx(1, "memory allocation failure");
200 add_mapping(struct glyph *gl, unsigned int c, unsigned int map_idx)
202 struct mapping *mp, *mp_temp;
203 struct mapping_list *ml;
207 mp = xmalloc(sizeof *mp);
213 if (TAILQ_LAST(ml, mapping_list) == NULL ||
214 TAILQ_LAST(ml, mapping_list)->m_char < c) {
215 /* Common case: empty list or new char at end of list. */
216 TAILQ_INSERT_TAIL(ml, mp, m_list);
218 /* Find insertion point for char; cannot be at end. */
219 TAILQ_FOREACH(mp_temp, ml, m_list) {
220 if (mp_temp->m_char >= c) {
221 TAILQ_INSERT_BEFORE(mp_temp, mp, m_list);
227 map_count[map_idx]++;
234 dedup_mapping(unsigned int map_idx)
236 struct mapping *mp_bold, *mp_normal, *mp_temp;
237 unsigned normal_map_idx = map_idx - VFNT_MAP_BOLD;
239 assert(map_idx == VFNT_MAP_BOLD || map_idx == VFNT_MAP_BOLD_RIGHT);
240 mp_normal = TAILQ_FIRST(&maps[normal_map_idx]);
241 TAILQ_FOREACH_SAFE(mp_bold, &maps[map_idx], m_list, mp_temp) {
242 while (mp_normal->m_char < mp_bold->m_char)
243 mp_normal = TAILQ_NEXT(mp_normal, m_list);
244 if (mp_bold->m_char != mp_normal->m_char)
245 errx(1, "Character %u not in normal font!",
247 if (mp_bold->m_glyph != mp_normal->m_glyph)
250 /* No mapping is needed if it's equal to the normal mapping. */
251 TAILQ_REMOVE(&maps[map_idx], mp_bold, m_list);
258 static struct glyph *
259 add_glyph(const uint8_t *bytes, unsigned int map_idx, int fallback)
265 glyph_count[map_idx]++;
267 /* Return existing glyph if we have an identical one. */
268 hash = fnv_32_buf(bytes, wbytes * height, FNV1_32_INIT) % FONTCVT_NHASH;
269 SLIST_FOREACH(gl, &glyph_hash[hash], g_hash) {
270 if (memcmp(gl->g_data, bytes, wbytes * height) == 0) {
276 /* Allocate new glyph. */
277 gl = xmalloc(sizeof *gl);
278 gl->g_data = xmalloc(wbytes * height);
279 memcpy(gl->g_data, bytes, wbytes * height);
281 TAILQ_INSERT_HEAD(&glyphs[map_idx], gl, g_list);
283 TAILQ_INSERT_TAIL(&glyphs[map_idx], gl, g_list);
284 SLIST_INSERT_HEAD(&glyph_hash[hash], gl, g_hash);
287 if (glyph_unique > VFNT_MAXGLYPHS)
288 errx(1, "too many glyphs (%u)", glyph_unique);
293 check_whitelist(unsigned c)
295 struct whitelist *w = NULL;
301 if (format == VT_C_SOURCE) {
303 n = sizeof (s_list) / sizeof (s_list[0]);
305 if (format == VT_C_COMPRESSED) {
307 n = sizeof (c_list) / sizeof (c_list[0]);
311 for (i = 0; i < n; i++) {
312 if (c >= w[i].c && c <= w[i].c + w[i].len)
319 add_char(unsigned curchar, unsigned map_idx, uint8_t *bytes, uint8_t *bytes_r)
323 /* Prevent adding two glyphs for 0xFFFD */
324 if (curchar == 0xFFFD) {
325 if (map_idx < VFNT_MAP_BOLD)
326 gl = add_glyph(bytes, 0, 1);
327 } else if (filter == false || curchar >= 0x20) {
328 gl = add_glyph(bytes, map_idx, 0);
329 if (add_mapping(gl, curchar, map_idx) != 0)
331 if (bytes_r != NULL) {
332 gl = add_glyph(bytes_r, map_idx + 1, 0);
333 if (add_mapping(gl, curchar, map_idx + 1) != 0)
341 * Right-shift glyph row.
344 rshift_row(uint8_t *buf, size_t len, size_t shift)
346 ssize_t i, off_byte = shift / 8;
347 size_t off_bit = shift % 8;
351 for (i = len - 1; i >= 0; i--)
352 buf[i] = (i >= off_byte ? buf[i - off_byte] >> off_bit : 0) |
353 (i > off_byte ? buf[i - off_byte - 1] << (8 - off_bit) : 0);
357 * Split double-width characters into left and right half. Single-width
358 * characters in _left_ only.
361 split_row(uint8_t *left, uint8_t *right, uint8_t *line, size_t w)
365 s = wbytes * 8 - width;
367 memcpy(left, line, wbytes);
368 *(left + wbytes - 1) &= 0xFF << s;
370 if (w > width) { /* Double-width character. */
373 for (i = 0; i < wbytes; i++) {
374 t = *(line + wbytes + i - 1);
376 t |= *(line + wbytes + i) >> s;
379 *(right + wbytes - 1) &= 0xFF << s;
387 if (h <= 0 || h > VFNT_MAXDIMENSION)
388 errx(1, "invalid height %d", h);
395 if (w <= 0 || w > VFNT_MAXDIMENSION)
396 errx(1, "invalid width %d", w);
398 wbytes = howmany(width, 8);
402 parse_bdf(FILE *fp, unsigned int map_idx)
406 uint8_t *line, *bytes, *bytes_r;
407 unsigned int curchar = 0, i, j, linenum = 0, bbwbytes;
408 int bbw, bbh, bbox, bboy; /* Glyph bounding box. */
409 int fbbw = 0, fbbh, fbbox, fbboy; /* Font bounding box. */
410 int dwidth = 0, dwy = 0;
415 * Step 1: Parse FONT logical font descriptor and FONTBOUNDINGBOX
418 while ((ln = fgetln(fp, &length)) != NULL) {
420 ln[length - 1] = '\0';
422 if (strncmp(ln, "FONT ", 5) == 0) {
425 while ((p = strchr(p, '-')) != NULL) {
433 } else if (strncmp(ln, "FONTBOUNDINGBOX ", 16) == 0) {
434 if (sscanf(ln + 16, "%d %d %d %d", &fbbw, &fbbh, &fbbox,
436 errx(1, "invalid FONTBOUNDINGBOX at line %u",
444 errx(1, "broken font header");
445 if (spc != 'c' && spc != 'C')
446 errx(1, "font spacing \"C\" (character cell) required");
448 /* Step 2: Validate DWIDTH (Device Width) of all glyphs. */
449 while ((ln = fgetln(fp, &length)) != NULL) {
451 ln[length - 1] = '\0';
453 if (strncmp(ln, "DWIDTH ", 7) == 0) {
454 if (sscanf(ln + 7, "%d %d", &dwidth, &dwy) != 2)
455 errx(1, "invalid DWIDTH at line %u", linenum);
456 if (dwy != 0 || (dwidth != fbbw && dwidth * 2 != fbbw))
457 errx(1, "bitmap with unsupported DWIDTH %d %d at line %u",
458 dwidth, dwy, linenum);
464 /* Step 3: Restart at the beginning of the file and read glyph data. */
465 dwidth = bbw = bbh = 0;
468 bbwbytes = 0; /* GCC 4.2.1 "may be used uninitialized" workaround. */
469 bytes = xmalloc(wbytes * height);
470 bytes_r = xmalloc(wbytes * height);
471 line = xmalloc(wbytes * 2);
472 while ((ln = fgetln(fp, &length)) != NULL) {
474 ln[length - 1] = '\0';
476 if (strncmp(ln, "ENCODING ", 9) == 0) {
477 curchar = atoi(ln + 9);
478 } else if (strncmp(ln, "DWIDTH ", 7) == 0) {
479 dwidth = atoi(ln + 7);
480 } else if (strncmp(ln, "BBX ", 4) == 0) {
481 if (sscanf(ln + 4, "%d %d %d %d", &bbw, &bbh, &bbox,
483 errx(1, "invalid BBX at line %u", linenum);
484 if (bbw < 1 || bbh < 1 || bbw > fbbw || bbh > fbbh ||
485 bbox < fbbox || bboy < fbboy ||
486 bbh + bboy > fbbh + fbboy)
487 errx(1, "broken bitmap with BBX %d %d %d %d at line %u",
488 bbw, bbh, bbox, bboy, linenum);
489 bbwbytes = howmany(bbw, 8);
490 } else if (strncmp(ln, "BITMAP", 6) == 0 &&
491 (ln[6] == ' ' || ln[6] == '\0')) {
492 if (dwidth == 0 || bbw == 0 || bbh == 0)
493 errx(1, "broken char header at line %u!",
495 memset(bytes, 0, wbytes * height);
496 memset(bytes_r, 0, wbytes * height);
499 * Assume that the next _bbh_ lines are bitmap data.
500 * ENDCHAR is allowed to terminate the bitmap
501 * early but is not otherwise checked; any extra data
504 for (i = (fbbh + fbboy) - (bbh + bboy);
505 i < (unsigned int)((fbbh + fbboy) - bboy); i++) {
506 if ((ln = fgetln(fp, &length)) == NULL)
507 errx(1, "unexpected EOF");
509 ln[length - 1] = '\0';
510 if (strcmp(ln, "ENDCHAR") == 0)
512 if (strlen(ln) < bbwbytes * 2)
513 errx(1, "broken bitmap at line %u",
515 memset(line, 0, wbytes * 2);
516 for (j = 0; j < bbwbytes; j++) {
518 if (sscanf(ln + j * 2, "%2x", &val) ==
521 *(line + j) = (uint8_t)val;
524 rshift_row(line, wbytes * 2, bbox - fbbox);
525 rv = split_row(bytes + i * wbytes,
526 bytes_r + i * wbytes, line, dwidth);
531 if (check_whitelist(curchar) == true) {
532 rv = add_char(curchar, map_idx, bytes,
533 dwidth > (int)width ? bytes_r : NULL);
538 dwidth = bbw = bbh = 0;
550 parse_hex(FILE *fp, unsigned int map_idx)
554 uint8_t *bytes = NULL, *bytes_r = NULL, *line = NULL;
555 unsigned curchar = 0, gwidth, gwbytes, i, j, chars_per_row;
558 while ((ln = fgetln(fp, &length)) != NULL) {
559 ln[length - 1] = '\0';
561 if (strncmp(ln, "# Height: ", 10) == 0) {
563 errx(1, "malformed input: Height tag after font data");
564 set_height(atoi(ln + 10));
565 } else if (strncmp(ln, "# Width: ", 9) == 0) {
567 errx(1, "malformed input: Width tag after font data");
568 set_width(atoi(ln + 9));
569 } else if (sscanf(ln, "%6x:", &curchar)) {
571 bytes = xmalloc(wbytes * height);
572 bytes_r = xmalloc(wbytes * height);
573 line = xmalloc(wbytes * 2);
575 /* ln is guaranteed to have a colon here. */
576 p = strchr(ln, ':') + 1;
577 chars_per_row = strlen(p) / height;
578 if (chars_per_row < wbytes * 2)
580 "malformed input: broken bitmap, character %06x",
583 gwbytes = howmany(gwidth, 8);
584 if (chars_per_row < gwbytes * 2 || gwidth <= 8) {
585 gwidth = width; /* Single-width character. */
589 for (i = 0; i < height; i++) {
590 for (j = 0; j < gwbytes; j++) {
592 if (sscanf(p + j * 2, "%2x", &val) == 0)
594 *(line + j) = (uint8_t)val;
596 rv = split_row(bytes + i * wbytes,
597 bytes_r + i * wbytes, line, gwidth);
603 if (check_whitelist(curchar) == true) {
604 rv = add_char(curchar, map_idx, bytes,
605 gwidth != width ? bytes_r : NULL);
619 parse_file(const char *filename, unsigned int map_idx)
625 fp = fopen(filename, "r");
630 len = strlen(filename);
631 if (len > 4 && strcasecmp(filename + len - 4, ".hex") == 0)
632 rv = parse_hex(fp, map_idx);
634 rv = parse_bdf(fp, map_idx);
643 unsigned int i, idx = 0;
645 for (i = 0; i < VFNT_MAPS; i++)
646 TAILQ_FOREACH(gl, &glyphs[i], g_list)
650 /* Note we only deal with byte stream here. */
652 write_glyph_source(const void *ptr, size_t size, size_t nitems, FILE *stream)
654 const uint8_t *data = ptr;
658 for (i = 0; i < size; i++) {
659 if ((i % wbytes) == 0) {
660 if (fprintf(stream, "\n") < 0)
663 if (fprintf(stream, "0x%02x, ", data[i]) < 0)
666 if (fprintf(stream, "\n") < 0)
672 /* Write to buffer */
674 write_glyph_buf(const void *ptr, size_t size, size_t nitems,
675 FILE *stream __unused)
677 static size_t index = 0;
680 (void) memmove(uncompressed + index, ptr, size);
687 write_glyphs(FILE *fp, vt_write cb)
692 for (i = 0; i < VFNT_MAPS; i++) {
693 TAILQ_FOREACH(gl, &glyphs[i], g_list)
694 if (cb(gl->g_data, wbytes * height, 1, fp) != 1)
701 fold_mappings(unsigned int map_idx)
703 struct mapping_list *ml = &maps[map_idx];
704 struct mapping *mn, *mp, *mbase;
706 mp = mbase = TAILQ_FIRST(ml);
707 for (mp = mbase = TAILQ_FIRST(ml); mp != NULL; mp = mn) {
708 mn = TAILQ_NEXT(mp, m_list);
709 if (mn != NULL && mn->m_char == mp->m_char + 1 &&
710 mn->m_glyph->g_index == mp->m_glyph->g_index + 1)
712 mbase->m_length = mp->m_char - mbase->m_char + 1;
714 map_folded_count[map_idx]++;
719 write_mappings(FILE *fp, unsigned int map_idx)
721 struct mapping_list *ml = &maps[map_idx];
724 unsigned int i = 0, j = 0;
726 TAILQ_FOREACH(mp, ml, m_list) {
728 if (mp->m_length > 0) {
730 fm.vfm_src = htobe32(mp->m_char);
731 fm.vfm_dst = htobe16(mp->m_glyph->g_index);
732 fm.vfm_len = htobe16(mp->m_length - 1);
733 if (fwrite(&fm, sizeof fm, 1, fp) != 1)
742 write_source_mappings(FILE *fp, unsigned int map_idx)
744 struct mapping_list *ml = &maps[map_idx];
746 unsigned int i = 0, j = 0;
748 TAILQ_FOREACH(mp, ml, m_list) {
750 if (mp->m_length > 0) {
752 if (fprintf(fp, "\t{ 0x%08x, 0x%04x, 0x%04x },\n",
753 mp->m_char, mp->m_glyph->g_index,
754 mp->m_length - 1) < 0)
763 write_fnt(const char *filename)
766 struct font_header fh = {
767 .fh_magic = FONT_HEADER_MAGIC,
770 fp = fopen(filename, "wb");
777 fh.fh_height = height;
778 fh.fh_glyph_count = htobe32(glyph_unique);
779 fh.fh_map_count[0] = htobe32(map_folded_count[0]);
780 fh.fh_map_count[1] = htobe32(map_folded_count[1]);
781 fh.fh_map_count[2] = htobe32(map_folded_count[2]);
782 fh.fh_map_count[3] = htobe32(map_folded_count[3]);
783 if (fwrite(&fh, sizeof(fh), 1, fp) != 1) {
789 if (write_glyphs(fp, &fwrite) != 0 ||
790 write_mappings(fp, VFNT_MAP_NORMAL) != 0 ||
791 write_mappings(fp, VFNT_MAP_NORMAL_RIGHT) != 0 ||
792 write_mappings(fp, VFNT_MAP_BOLD) != 0 ||
793 write_mappings(fp, VFNT_MAP_BOLD_RIGHT) != 0) {
804 write_fnt_source(bool lz4, const char *filename)
808 size_t uncompressed_size = wbytes * height * glyph_unique;
809 size_t compressed_size = uncompressed_size;
810 uint8_t *compressed = NULL;
812 fp = fopen(filename, "w");
819 uncompressed = xmalloc(uncompressed_size);
820 compressed = xmalloc(uncompressed_size);
822 if (fprintf(fp, "/* Generated %ux%u console font source. */\n\n",
825 if (fprintf(fp, "#include <sys/types.h>\n") < 0)
827 if (fprintf(fp, "#include <sys/param.h>\n") < 0)
829 if (fprintf(fp, "#include <sys/font.h>\n\n") < 0)
832 /* Write font bytes. */
833 if (fprintf(fp, "static uint8_t FONTDATA_%ux%u[] = {\n",
837 if (write_glyphs(fp, &write_glyph_buf) != 0)
839 compressed_size = lz4_compress(uncompressed, compressed,
840 uncompressed_size, compressed_size, 0);
841 if (write_glyph_source(compressed, compressed_size, 1, fp) != 1)
846 if (write_glyphs(fp, &write_glyph_source) != 0)
849 if (fprintf(fp, "};\n\n") < 0)
852 /* Write font maps. */
853 if (!TAILQ_EMPTY(&maps[VFNT_MAP_NORMAL])) {
854 if (fprintf(fp, "static vfnt_map_t "
855 "FONTMAP_NORMAL_%ux%u[] = {\n", width, height) < 0)
857 if (write_source_mappings(fp, VFNT_MAP_NORMAL) != 0)
859 if (fprintf(fp, "};\n\n") < 0)
862 if (!TAILQ_EMPTY(&maps[VFNT_MAP_NORMAL_RIGHT])) {
863 if (fprintf(fp, "static vfnt_map_t "
864 "FONTMAP_NORMAL_RH_%ux%u[] = {\n", width, height) < 0)
866 if (write_source_mappings(fp, VFNT_MAP_NORMAL_RIGHT) != 0)
868 if (fprintf(fp, "};\n\n") < 0)
871 if (!TAILQ_EMPTY(&maps[VFNT_MAP_BOLD])) {
872 if (fprintf(fp, "static vfnt_map_t "
873 "FONTMAP_BOLD_%ux%u[] = {\n", width, height) < 0)
875 if (write_source_mappings(fp, VFNT_MAP_BOLD) != 0)
877 if (fprintf(fp, "};\n\n") < 0)
880 if (!TAILQ_EMPTY(&maps[VFNT_MAP_BOLD_RIGHT])) {
881 if (fprintf(fp, "static vfnt_map_t "
882 "FONTMAP_BOLD_RH_%ux%u[] = {\n", width, height) < 0)
884 if (write_source_mappings(fp, VFNT_MAP_BOLD_RIGHT) != 0)
886 if (fprintf(fp, "};\n\n") < 0)
890 /* Write struct font. */
891 if (fprintf(fp, "struct vt_font font_%ux%u = {\n",
894 if (fprintf(fp, "\t.vf_map\t= {\n") < 0)
896 if (TAILQ_EMPTY(&maps[VFNT_MAP_NORMAL])) {
897 if (fprintf(fp, "\t\t\tNULL,\n") < 0)
900 if (fprintf(fp, "\t\t\tFONTMAP_NORMAL_%ux%u,\n",
904 if (TAILQ_EMPTY(&maps[VFNT_MAP_NORMAL_RIGHT])) {
905 if (fprintf(fp, "\t\t\tNULL,\n") < 0)
908 if (fprintf(fp, "\t\t\tFONTMAP_NORMAL_RH_%ux%u,\n",
912 if (TAILQ_EMPTY(&maps[VFNT_MAP_BOLD])) {
913 if (fprintf(fp, "\t\t\tNULL,\n") < 0)
916 if (fprintf(fp, "\t\t\tFONTMAP_BOLD_%ux%u,\n",
920 if (TAILQ_EMPTY(&maps[VFNT_MAP_BOLD_RIGHT])) {
921 if (fprintf(fp, "\t\t\tNULL\n") < 0)
924 if (fprintf(fp, "\t\t\tFONTMAP_BOLD_RH_%ux%u\n",
928 if (fprintf(fp, "\t\t},\n") < 0)
931 if (fprintf(fp, "\t.vf_bytes\t= NULL,\n") < 0)
934 if (fprintf(fp, "\t.vf_bytes\t= FONTDATA_%ux%u,\n",
935 width, height) < 0) {
939 if (fprintf(fp, "\t.vf_width\t= %u,\n", width) < 0)
941 if (fprintf(fp, "\t.vf_height\t= %u,\n", height) < 0)
943 if (fprintf(fp, "\t.vf_map_count\t= { %u, %u, %u, %u }\n",
944 map_folded_count[0], map_folded_count[1], map_folded_count[2],
945 map_folded_count[3]) < 0) {
948 if (fprintf(fp, "};\n\n") < 0)
951 /* Write bitmap data. */
952 if (fprintf(fp, "vt_font_bitmap_data_t font_data_%ux%u = {\n",
955 if (fprintf(fp, "\t.vfbd_width\t= %u,\n", width) < 0)
957 if (fprintf(fp, "\t.vfbd_height\t= %u,\n", height) < 0)
960 if (fprintf(fp, "\t.vfbd_compressed_size\t= %zu,\n",
961 compressed_size) < 0) {
964 if (fprintf(fp, "\t.vfbd_uncompressed_size\t= %zu,\n",
965 uncompressed_size) < 0) {
968 if (fprintf(fp, "\t.vfbd_compressed_data\t= FONTDATA_%ux%u,\n",
969 width, height) < 0) {
973 if (fprintf(fp, "\t.vfbd_compressed_size\t= 0,\n") < 0)
975 if (fprintf(fp, "\t.vfbd_uncompressed_size\t= %zu,\n",
976 uncompressed_size) < 0) {
979 if (fprintf(fp, "\t.vfbd_compressed_data\t= NULL,\n") < 0)
982 if (fprintf(fp, "\t.vfbd_font = &font_%ux%u\n", width, height) < 0)
984 if (fprintf(fp, "};\n") < 0)
996 print_font_info(void)
1002 "- glyph_total: %6u\n"
1003 "- glyph_normal: %6u\n"
1004 "- glyph_normal_right: %6u\n"
1005 "- glyph_bold: %6u\n"
1006 "- glyph_bold_right: %6u\n"
1007 "- glyph_unique: %6u\n"
1008 "- glyph_dupe: %6u\n"
1009 "- mapping_total: %6u\n"
1010 "- mapping_normal: %6u\n"
1011 "- mapping_normal_folded: %6u\n"
1012 "- mapping_normal_right: %6u\n"
1013 "- mapping_normal_right_folded: %6u\n"
1014 "- mapping_bold: %6u\n"
1015 "- mapping_bold_folded: %6u\n"
1016 "- mapping_bold_right: %6u\n"
1017 "- mapping_bold_right_folded: %6u\n"
1018 "- mapping_unique: %6u\n"
1019 "- mapping_dupe: %6u\n",
1026 glyph_unique, glyph_dupe,
1028 map_count[0], map_folded_count[0],
1029 map_count[1], map_folded_count[1],
1030 map_count[2], map_folded_count[2],
1031 map_count[3], map_folded_count[3],
1032 mapping_unique, mapping_dupe);
1036 main(int argc, char *argv[])
1038 int ch, verbose = 0, rv = 0;
1039 char *outfile = NULL;
1041 assert(sizeof(struct font_header) == 32);
1042 assert(sizeof(vfnt_map_t) == 8);
1044 while ((ch = getopt(argc, argv, "nf:h:vw:o:")) != -1) {
1047 if (strcmp(optarg, "font") == 0)
1049 else if (strcmp(optarg, "source") == 0)
1050 format = VT_C_SOURCE;
1051 else if (strcmp(optarg, "compressed-source") == 0)
1052 format = VT_C_COMPRESSED;
1054 errx(1, "Invalid format: %s", optarg);
1057 height = atoi(optarg);
1069 width = atoi(optarg);
1079 if (outfile == NULL && (argc < 2 || argc > 3))
1082 if (outfile == NULL) {
1083 outfile = argv[argc - 1];
1090 if (parse_file(argv[0], VFNT_MAP_NORMAL) != 0)
1095 if (parse_file(argv[0], VFNT_MAP_BOLD) != 0)
1101 dedup_mapping(VFNT_MAP_BOLD);
1102 dedup_mapping(VFNT_MAP_BOLD_RIGHT);
1110 rv = write_fnt(outfile);
1113 rv = write_fnt_source(false, outfile);
1115 case VT_C_COMPRESSED:
1116 rv = write_fnt_source(true, outfile);