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>
48 #define VFNT_MAP_NORMAL 0
49 #define VFNT_MAP_NORMAL_RH 1
50 #define VFNT_MAP_BOLD 2
51 #define VFNT_MAP_BOLD_RH 3
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;
95 "usage: vtfontcvt [-w width] [-h height] [-v] normal.bdf [bold.bdf] out.fnt\n");
104 if ((m = malloc(size)) == NULL)
105 errx(1, "memory allocation failure");
110 add_mapping(struct glyph *gl, unsigned int c, unsigned int map_idx)
113 struct mapping_list *ml;
117 mp = xmalloc(sizeof *mp);
123 if (TAILQ_LAST(ml, mapping_list) != NULL &&
124 TAILQ_LAST(ml, mapping_list)->m_char >= c)
125 errx(1, "Bad ordering at character %u", c);
126 TAILQ_INSERT_TAIL(ml, mp, m_list);
128 map_count[map_idx]++;
135 dedup_mapping(unsigned int map_idx)
137 struct mapping *mp_bold, *mp_normal, *mp_temp;
138 unsigned normal_map_idx = map_idx - VFNT_MAP_BOLD;
140 assert(map_idx == VFNT_MAP_BOLD || map_idx == VFNT_MAP_BOLD_RH);
141 mp_normal = TAILQ_FIRST(&maps[normal_map_idx]);
142 TAILQ_FOREACH_SAFE(mp_bold, &maps[map_idx], m_list, mp_temp) {
143 while (mp_normal->m_char < mp_bold->m_char)
144 mp_normal = TAILQ_NEXT(mp_normal, m_list);
145 if (mp_bold->m_char != mp_normal->m_char)
146 errx(1, "Character %u not in normal font!",
148 if (mp_bold->m_glyph != mp_normal->m_glyph)
151 /* No mapping is needed if it's equal to the normal mapping. */
152 TAILQ_REMOVE(&maps[map_idx], mp_bold, m_list);
159 static struct glyph *
160 add_glyph(const uint8_t *bytes, unsigned int map_idx, int fallback)
166 glyph_count[map_idx]++;
168 hash = fnv_32_buf(bytes, wbytes * height, FNV1_32_INIT) % FONTCVT_NHASH;
169 SLIST_FOREACH(gl, &glyph_hash[hash], g_hash) {
170 if (memcmp(gl->g_data, bytes, wbytes * height) == 0) {
176 gl = xmalloc(sizeof *gl);
177 gl->g_data = xmalloc(wbytes * height);
178 memcpy(gl->g_data, bytes, wbytes * height);
180 TAILQ_INSERT_HEAD(&glyphs[map_idx], gl, g_list);
182 TAILQ_INSERT_TAIL(&glyphs[map_idx], gl, g_list);
183 SLIST_INSERT_HEAD(&glyph_hash[hash], gl, g_hash);
190 add_char(unsigned curchar, unsigned map_idx, uint8_t *bytes, uint8_t *bytes_r)
194 /* Prevent adding two glyphs for 0xFFFD */
195 if (curchar == 0xFFFD) {
196 if (map_idx < VFNT_MAP_BOLD)
197 gl = add_glyph(bytes, 0, 1);
198 } else if (curchar >= 0x20) {
199 gl = add_glyph(bytes, map_idx, 0);
200 if (add_mapping(gl, curchar, map_idx) != 0)
202 if (bytes_r != NULL) {
203 gl = add_glyph(bytes_r, map_idx + 1, 0);
204 if (add_mapping(gl, curchar,
214 parse_bitmap_line(uint8_t *left, uint8_t *right, unsigned int line,
218 unsigned int i, subline;
220 if (dwidth != width && dwidth != width * 2)
221 errx(1, "Bitmap with unsupported width %u!", dwidth);
223 /* Move pixel data right to simplify splitting double characters. */
224 line >>= (howmany(dwidth, 8) * 8) - dwidth;
226 for (i = dwidth / width; i > 0; i--) {
227 p = (i == 2) ? right : left;
229 subline = line & ((1 << width) - 1);
230 subline <<= (howmany(width, 8) * 8) - width;
234 } else if (wbytes == 2) {
238 errx(1, "Unsupported wbytes %u!", wbytes);
248 parse_bdf(FILE *fp, unsigned int map_idx)
252 uint8_t bytes[wbytes * height], bytes_r[wbytes * height];
253 unsigned int curchar = 0, dwidth = 0, i, line;
255 while ((ln = fgetln(fp, &length)) != NULL) {
256 ln[length - 1] = '\0';
258 if (strncmp(ln, "ENCODING ", 9) == 0) {
259 curchar = atoi(ln + 9);
262 if (strncmp(ln, "DWIDTH ", 7) == 0) {
263 dwidth = atoi(ln + 7);
266 if (strncmp(ln, "BITMAP", 6) == 0 &&
267 (ln[6] == ' ' || ln[6] == '\0')) {
268 for (i = 0; i < height; i++) {
269 if ((ln = fgetln(fp, &length)) == NULL)
270 errx(1, "Unexpected EOF!");
271 ln[length - 1] = '\0';
272 sscanf(ln, "%x", &line);
273 if (parse_bitmap_line(bytes + i * wbytes,
274 bytes_r + i * wbytes, line, dwidth) != 0)
278 if (add_char(curchar, map_idx, bytes,
279 dwidth == width * 2 ? bytes_r : NULL) != 0)
291 if (w <= 0 || w > 128)
292 errx(1, "invalid width %d", w);
294 wbytes = howmany(width, 8);
298 parse_hex(FILE *fp, unsigned int map_idx)
303 uint8_t *bytes = NULL, *bytes_r = NULL;
304 unsigned curchar = 0, i, line, chars_per_row, dwidth;
307 while ((ln = fgetln(fp, &length)) != NULL) {
308 ln[length - 1] = '\0';
310 if (strncmp(ln, "# Height: ", 10) == 0) {
312 errx(1, "malformed input: Height tag after font data");
313 height = atoi(ln + 10);
314 } else if (strncmp(ln, "# Width: ", 9) == 0) {
316 errx(1, "malformed input: Width tag after font data");
317 set_width(atoi(ln + 9));
318 } else if (sscanf(ln, "%6x:", &curchar)) {
320 bytes = xmalloc(wbytes * height);
321 bytes_r = xmalloc(wbytes * height);
323 /* ln is guaranteed to have a colon here. */
324 p = strchr(ln, ':') + 1;
325 chars_per_row = strlen(p) / height;
327 if (chars_per_row / 2 > (width + 7) / 8)
328 dwidth *= 2; /* Double-width character. */
329 snprintf(fmt_str, sizeof(fmt_str), "%%%ux",
332 for (i = 0; i < height; i++) {
333 sscanf(p, fmt_str, &line);
335 if (parse_bitmap_line(bytes + i * wbytes,
336 bytes_r + i * wbytes, line, dwidth) != 0) {
342 if (add_char(curchar, map_idx, bytes,
343 dwidth == width * 2 ? bytes_r : NULL) != 0) {
356 parse_file(const char *filename, unsigned int map_idx)
362 fp = fopen(filename, "r");
367 len = strlen(filename);
368 if (len > 4 && strcasecmp(filename + len - 4, ".hex") == 0)
369 rv = parse_hex(fp, map_idx);
371 rv = parse_bdf(fp, map_idx);
380 unsigned int i, idx = 0;
382 for (i = 0; i < VFNT_MAPS; i++)
383 TAILQ_FOREACH(gl, &glyphs[i], g_list)
388 write_glyphs(FILE *fp)
393 for (i = 0; i < VFNT_MAPS; i++) {
394 TAILQ_FOREACH(gl, &glyphs[i], g_list)
395 if (fwrite(gl->g_data, wbytes * height, 1, fp) != 1)
402 fold_mappings(unsigned int map_idx)
404 struct mapping_list *ml = &maps[map_idx];
405 struct mapping *mn, *mp, *mbase;
407 mp = mbase = TAILQ_FIRST(ml);
408 for (mp = mbase = TAILQ_FIRST(ml); mp != NULL; mp = mn) {
409 mn = TAILQ_NEXT(mp, m_list);
410 if (mn != NULL && mn->m_char == mp->m_char + 1 &&
411 mn->m_glyph->g_index == mp->m_glyph->g_index + 1)
413 mbase->m_length = mp->m_char - mbase->m_char + 1;
415 map_folded_count[map_idx]++;
419 struct file_mapping {
421 uint16_t destination;
426 write_mappings(FILE *fp, unsigned int map_idx)
428 struct mapping_list *ml = &maps[map_idx];
430 struct file_mapping fm;
431 unsigned int i = 0, j = 0;
433 TAILQ_FOREACH(mp, ml, m_list) {
435 if (mp->m_length > 0) {
437 fm.source = htobe32(mp->m_char);
438 fm.destination = htobe16(mp->m_glyph->g_index);
439 fm.length = htobe16(mp->m_length - 1);
440 if (fwrite(&fm, sizeof fm, 1, fp) != 1)
453 uint32_t glyph_count;
454 uint32_t map_count[4];
458 write_fnt(const char *filename)
461 struct file_header fh = {
465 fp = fopen(filename, "wb");
473 fh.glyph_count = htobe32(glyph_unique);
474 fh.map_count[0] = htobe32(map_folded_count[0]);
475 fh.map_count[1] = htobe32(map_folded_count[1]);
476 fh.map_count[2] = htobe32(map_folded_count[2]);
477 fh.map_count[3] = htobe32(map_folded_count[3]);
478 if (fwrite(&fh, sizeof fh, 1, fp) != 1) {
484 if (write_glyphs(fp) != 0 ||
485 write_mappings(fp, VFNT_MAP_NORMAL) != 0 ||
486 write_mappings(fp, 1) != 0 ||
487 write_mappings(fp, VFNT_MAP_BOLD) != 0 ||
488 write_mappings(fp, 3) != 0) {
499 print_font_info(void)
503 "- glyph_total: %6u\n"
504 "- glyph_normal: %6u\n"
505 "- glyph_normal_right: %6u\n"
506 "- glyph_bold: %6u\n"
507 "- glyph_bold_right: %6u\n"
508 "- glyph_unique: %6u\n"
509 "- glyph_dupe: %6u\n"
510 "- mapping_total: %6u\n"
511 "- mapping_normal: %6u\n"
512 "- mapping_normal_folded: %6u\n"
513 "- mapping_normal_right: %6u\n"
514 "- mapping_normal_right_folded: %6u\n"
515 "- mapping_bold: %6u\n"
516 "- mapping_bold_folded: %6u\n"
517 "- mapping_bold_right: %6u\n"
518 "- mapping_bold_right_folded: %6u\n"
519 "- mapping_unique: %6u\n"
520 "- mapping_dupe: %6u\n",
526 glyph_unique, glyph_dupe,
528 map_count[0], map_folded_count[0],
529 map_count[1], map_folded_count[1],
530 map_count[2], map_folded_count[2],
531 map_count[3], map_folded_count[3],
532 mapping_unique, mapping_dupe);
536 main(int argc, char *argv[])
538 int ch, val, verbose = 0;
540 assert(sizeof(struct file_header) == 32);
541 assert(sizeof(struct file_mapping) == 8);
543 while ((ch = getopt(argc, argv, "h:vw:")) != -1) {
547 if (val <= 0 || val > 128)
548 errx(1, "Invalid height %d", val);
555 set_width(atoi(optarg));
565 if (argc < 2 || argc > 3)
568 wbytes = howmany(width, 8);
570 if (parse_file(argv[0], VFNT_MAP_NORMAL) != 0)
575 if (parse_file(argv[0], VFNT_MAP_BOLD) != 0)
581 dedup_mapping(VFNT_MAP_BOLD);
582 dedup_mapping(VFNT_MAP_BOLD_RH);
587 if (write_fnt(argv[0]) != 0)