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
52 #define VFNT_MAXGLYPHS 131072
53 #define VFNT_MAXDIMENSION 128
55 static unsigned int width = 8, wbytes, height = 16;
58 TAILQ_ENTRY(glyph) g_list;
59 SLIST_ENTRY(glyph) g_hash;
64 #define FONTCVT_NHASH 4096
65 TAILQ_HEAD(glyph_list, glyph);
66 static SLIST_HEAD(, glyph) glyph_hash[FONTCVT_NHASH];
67 static struct glyph_list glyphs[VFNT_MAPS] = {
68 TAILQ_HEAD_INITIALIZER(glyphs[0]),
69 TAILQ_HEAD_INITIALIZER(glyphs[1]),
70 TAILQ_HEAD_INITIALIZER(glyphs[2]),
71 TAILQ_HEAD_INITIALIZER(glyphs[3]),
73 static unsigned int glyph_total, glyph_count[4], glyph_unique, glyph_dupe;
76 TAILQ_ENTRY(mapping) m_list;
78 unsigned int m_length;
79 struct glyph *m_glyph;
82 TAILQ_HEAD(mapping_list, mapping);
83 static struct mapping_list maps[VFNT_MAPS] = {
84 TAILQ_HEAD_INITIALIZER(maps[0]),
85 TAILQ_HEAD_INITIALIZER(maps[1]),
86 TAILQ_HEAD_INITIALIZER(maps[2]),
87 TAILQ_HEAD_INITIALIZER(maps[3]),
89 static unsigned int mapping_total, map_count[4], map_folded_count[4],
90 mapping_unique, mapping_dupe;
97 "usage: vtfontcvt [-w width] [-h height] [-v] normal.bdf [bold.bdf] out.fnt\n");
106 if ((m = calloc(1, size)) == NULL)
107 errx(1, "memory allocation failure");
112 add_mapping(struct glyph *gl, unsigned int c, unsigned int map_idx)
114 struct mapping *mp, *mp_temp;
115 struct mapping_list *ml;
119 mp = xmalloc(sizeof *mp);
125 if (TAILQ_LAST(ml, mapping_list) == NULL ||
126 TAILQ_LAST(ml, mapping_list)->m_char < c) {
127 /* Common case: empty list or new char at end of list. */
128 TAILQ_INSERT_TAIL(ml, mp, m_list);
130 /* Find insertion point for char; cannot be at end. */
131 TAILQ_FOREACH(mp_temp, ml, m_list) {
132 if (mp_temp->m_char >= c) {
133 TAILQ_INSERT_BEFORE(mp_temp, mp, m_list);
139 map_count[map_idx]++;
146 dedup_mapping(unsigned int map_idx)
148 struct mapping *mp_bold, *mp_normal, *mp_temp;
149 unsigned normal_map_idx = map_idx - VFNT_MAP_BOLD;
151 assert(map_idx == VFNT_MAP_BOLD || map_idx == VFNT_MAP_BOLD_RH);
152 mp_normal = TAILQ_FIRST(&maps[normal_map_idx]);
153 TAILQ_FOREACH_SAFE(mp_bold, &maps[map_idx], m_list, mp_temp) {
154 while (mp_normal->m_char < mp_bold->m_char)
155 mp_normal = TAILQ_NEXT(mp_normal, m_list);
156 if (mp_bold->m_char != mp_normal->m_char)
157 errx(1, "Character %u not in normal font!",
159 if (mp_bold->m_glyph != mp_normal->m_glyph)
162 /* No mapping is needed if it's equal to the normal mapping. */
163 TAILQ_REMOVE(&maps[map_idx], mp_bold, m_list);
170 static struct glyph *
171 add_glyph(const uint8_t *bytes, unsigned int map_idx, int fallback)
177 glyph_count[map_idx]++;
179 /* Return existing glyph if we have an identical one. */
180 hash = fnv_32_buf(bytes, wbytes * height, FNV1_32_INIT) % FONTCVT_NHASH;
181 SLIST_FOREACH(gl, &glyph_hash[hash], g_hash) {
182 if (memcmp(gl->g_data, bytes, wbytes * height) == 0) {
188 /* Allocate new glyph. */
189 gl = xmalloc(sizeof *gl);
190 gl->g_data = xmalloc(wbytes * height);
191 memcpy(gl->g_data, bytes, wbytes * height);
193 TAILQ_INSERT_HEAD(&glyphs[map_idx], gl, g_list);
195 TAILQ_INSERT_TAIL(&glyphs[map_idx], gl, g_list);
196 SLIST_INSERT_HEAD(&glyph_hash[hash], gl, g_hash);
199 if (glyph_unique > VFNT_MAXGLYPHS)
200 errx(1, "too many glyphs (%u)", glyph_unique);
205 add_char(unsigned curchar, unsigned map_idx, uint8_t *bytes, uint8_t *bytes_r)
209 /* Prevent adding two glyphs for 0xFFFD */
210 if (curchar == 0xFFFD) {
211 if (map_idx < VFNT_MAP_BOLD)
212 gl = add_glyph(bytes, 0, 1);
213 } else if (curchar >= 0x20) {
214 gl = add_glyph(bytes, map_idx, 0);
215 if (add_mapping(gl, curchar, map_idx) != 0)
217 if (bytes_r != NULL) {
218 gl = add_glyph(bytes_r, map_idx + 1, 0);
219 if (add_mapping(gl, curchar, map_idx + 1) != 0)
227 * Right-shift glyph row.
230 rshift_row(uint8_t *buf, size_t len, size_t shift)
232 ssize_t i, off_byte = shift / 8;
233 size_t off_bit = shift % 8;
237 for (i = len - 1; i >= 0; i--)
238 buf[i] = (i >= off_byte ? buf[i - off_byte] >> off_bit : 0) |
239 (i > off_byte ? buf[i - off_byte - 1] << (8 - off_bit) : 0);
243 * Split double-width characters into left and right half. Single-width
244 * characters in _left_ only.
247 split_row(uint8_t *left, uint8_t *right, uint8_t *line, size_t w)
251 s = wbytes * 8 - width;
253 memcpy(left, line, wbytes);
254 *(left + wbytes - 1) &= 0xFF << s;
256 if (w > width) { /* Double-width character. */
259 for (i = 0; i < wbytes; i++) {
260 t = *(line + wbytes + i - 1);
262 t |= *(line + wbytes + i) >> s;
265 *(right + wbytes - 1) &= 0xFF << s;
273 if (h <= 0 || h > VFNT_MAXDIMENSION)
274 errx(1, "invalid height %d", h);
281 if (w <= 0 || w > VFNT_MAXDIMENSION)
282 errx(1, "invalid width %d", w);
284 wbytes = howmany(width, 8);
288 parse_bdf(FILE *fp, unsigned int map_idx)
292 uint8_t *bytes, *bytes_r;
293 unsigned int curchar = 0, i, j, linenum = 0, bbwbytes;
294 int bbw, bbh, bbox, bboy; /* Glyph bounding box. */
295 int fbbw = 0, fbbh, fbbox, fbboy; /* Font bounding box. */
296 int dwidth = 0, dwy = 0;
301 * Step 1: Parse FONT logical font descriptor and FONTBOUNDINGBOX
304 while ((ln = fgetln(fp, &length)) != NULL) {
306 ln[length - 1] = '\0';
308 if (strncmp(ln, "FONT ", 5) == 0) {
311 while ((p = strchr(p, '-')) != NULL) {
319 } else if (strncmp(ln, "FONTBOUNDINGBOX ", 16) == 0) {
320 if (sscanf(ln + 16, "%d %d %d %d", &fbbw, &fbbh, &fbbox,
322 errx(1, "invalid FONTBOUNDINGBOX at line %u",
330 errx(1, "broken font header");
331 if (spc != 'c' && spc != 'C')
332 errx(1, "font spacing \"C\" (character cell) required");
334 /* Step 2: Validate DWIDTH (Device Width) of all glyphs. */
335 while ((ln = fgetln(fp, &length)) != NULL) {
337 ln[length - 1] = '\0';
339 if (strncmp(ln, "DWIDTH ", 7) == 0) {
340 if (sscanf(ln + 7, "%d %d", &dwidth, &dwy) != 2)
341 errx(1, "invalid DWIDTH at line %u", linenum);
342 if (dwy != 0 || (dwidth != fbbw && dwidth * 2 != fbbw))
343 errx(1, "bitmap with unsupported DWIDTH %d %d at line %u",
344 dwidth, dwy, linenum);
350 /* Step 3: Restart at the beginning of the file and read glyph data. */
351 dwidth = bbw = bbh = 0;
354 bbwbytes = 0; /* GCC 4.2.1 "may be used uninitialized" workaround. */
355 bytes = xmalloc(wbytes * height);
356 bytes_r = xmalloc(wbytes * height);
357 line = xmalloc(wbytes * 2);
358 while ((ln = fgetln(fp, &length)) != NULL) {
360 ln[length - 1] = '\0';
362 if (strncmp(ln, "ENCODING ", 9) == 0) {
363 curchar = atoi(ln + 9);
364 } else if (strncmp(ln, "DWIDTH ", 7) == 0) {
365 dwidth = atoi(ln + 7);
366 } else if (strncmp(ln, "BBX ", 4) == 0) {
367 if (sscanf(ln + 4, "%d %d %d %d", &bbw, &bbh, &bbox,
369 errx(1, "invalid BBX at line %u", linenum);
370 if (bbw < 1 || bbh < 1 || bbw > fbbw || bbh > fbbh ||
371 bbox < fbbox || bboy < fbboy ||
372 bbh + bboy > fbbh + fbboy)
373 errx(1, "broken bitmap with BBX %d %d %d %d at line %u",
374 bbw, bbh, bbox, bboy, linenum);
375 bbwbytes = howmany(bbw, 8);
376 } else if (strncmp(ln, "BITMAP", 6) == 0 &&
377 (ln[6] == ' ' || ln[6] == '\0')) {
378 if (dwidth == 0 || bbw == 0 || bbh == 0)
379 errx(1, "broken char header at line %u!",
381 memset(bytes, 0, wbytes * height);
382 memset(bytes_r, 0, wbytes * height);
385 * Assume that the next _bbh_ lines are bitmap data.
386 * ENDCHAR is allowed to terminate the bitmap
387 * early but is not otherwise checked; any extra data
390 for (i = (fbbh + fbboy) - (bbh + bboy);
391 i < (unsigned int)((fbbh + fbboy) - bboy); i++) {
392 if ((ln = fgetln(fp, &length)) == NULL)
393 errx(1, "unexpected EOF");
395 ln[length - 1] = '\0';
396 if (strcmp(ln, "ENDCHAR") == 0)
398 if (strlen(ln) < bbwbytes * 2)
399 errx(1, "broken bitmap at line %u",
401 memset(line, 0, wbytes * 2);
402 for (j = 0; j < bbwbytes; j++) {
404 if (sscanf(ln + j * 2, "%2x", &val) ==
407 *(line + j) = (uint8_t)val;
410 rshift_row(line, wbytes * 2, bbox - fbbox);
411 rv = split_row(bytes + i * wbytes,
412 bytes_r + i * wbytes, line, dwidth);
417 rv = add_char(curchar, map_idx, bytes,
418 dwidth > (int)width ? bytes_r : NULL);
422 dwidth = bbw = bbh = 0;
434 parse_hex(FILE *fp, unsigned int map_idx)
438 uint8_t *bytes = NULL, *bytes_r = NULL, *line = NULL;
439 unsigned curchar = 0, gwidth, gwbytes, i, j, chars_per_row;
442 while ((ln = fgetln(fp, &length)) != NULL) {
443 ln[length - 1] = '\0';
445 if (strncmp(ln, "# Height: ", 10) == 0) {
447 errx(1, "malformed input: Height tag after font data");
448 set_height(atoi(ln + 10));
449 } else if (strncmp(ln, "# Width: ", 9) == 0) {
451 errx(1, "malformed input: Width tag after font data");
452 set_width(atoi(ln + 9));
453 } else if (sscanf(ln, "%6x:", &curchar)) {
455 bytes = xmalloc(wbytes * height);
456 bytes_r = xmalloc(wbytes * height);
457 line = xmalloc(wbytes * 2);
459 /* ln is guaranteed to have a colon here. */
460 p = strchr(ln, ':') + 1;
461 chars_per_row = strlen(p) / height;
462 if (chars_per_row < wbytes * 2)
464 "malformed input: broken bitmap, character %06x",
467 gwbytes = howmany(gwidth, 8);
468 if (chars_per_row < gwbytes * 2 || gwidth <= 8) {
469 gwidth = width; /* Single-width character. */
473 for (i = 0; i < height; i++) {
474 for (j = 0; j < gwbytes; j++) {
476 if (sscanf(p + j * 2, "%2x", &val) == 0)
478 *(line + j) = (uint8_t)val;
480 rv = split_row(bytes + i * wbytes,
481 bytes_r + i * wbytes, line, gwidth);
487 rv = add_char(curchar, map_idx, bytes,
488 gwidth != width ? bytes_r : NULL);
501 parse_file(const char *filename, unsigned int map_idx)
507 fp = fopen(filename, "r");
512 len = strlen(filename);
513 if (len > 4 && strcasecmp(filename + len - 4, ".hex") == 0)
514 rv = parse_hex(fp, map_idx);
516 rv = parse_bdf(fp, map_idx);
525 unsigned int i, idx = 0;
527 for (i = 0; i < VFNT_MAPS; i++)
528 TAILQ_FOREACH(gl, &glyphs[i], g_list)
533 write_glyphs(FILE *fp)
538 for (i = 0; i < VFNT_MAPS; i++) {
539 TAILQ_FOREACH(gl, &glyphs[i], g_list)
540 if (fwrite(gl->g_data, wbytes * height, 1, fp) != 1)
547 fold_mappings(unsigned int map_idx)
549 struct mapping_list *ml = &maps[map_idx];
550 struct mapping *mn, *mp, *mbase;
552 mp = mbase = TAILQ_FIRST(ml);
553 for (mp = mbase = TAILQ_FIRST(ml); mp != NULL; mp = mn) {
554 mn = TAILQ_NEXT(mp, m_list);
555 if (mn != NULL && mn->m_char == mp->m_char + 1 &&
556 mn->m_glyph->g_index == mp->m_glyph->g_index + 1)
558 mbase->m_length = mp->m_char - mbase->m_char + 1;
560 map_folded_count[map_idx]++;
564 struct file_mapping {
566 uint16_t destination;
571 write_mappings(FILE *fp, unsigned int map_idx)
573 struct mapping_list *ml = &maps[map_idx];
575 struct file_mapping fm;
576 unsigned int i = 0, j = 0;
578 TAILQ_FOREACH(mp, ml, m_list) {
580 if (mp->m_length > 0) {
582 fm.source = htobe32(mp->m_char);
583 fm.destination = htobe16(mp->m_glyph->g_index);
584 fm.length = htobe16(mp->m_length - 1);
585 if (fwrite(&fm, sizeof fm, 1, fp) != 1)
598 uint32_t glyph_count;
599 uint32_t map_count[4];
603 write_fnt(const char *filename)
606 struct file_header fh = {
610 fp = fopen(filename, "wb");
618 fh.glyph_count = htobe32(glyph_unique);
619 fh.map_count[0] = htobe32(map_folded_count[0]);
620 fh.map_count[1] = htobe32(map_folded_count[1]);
621 fh.map_count[2] = htobe32(map_folded_count[2]);
622 fh.map_count[3] = htobe32(map_folded_count[3]);
623 if (fwrite(&fh, sizeof fh, 1, fp) != 1) {
629 if (write_glyphs(fp) != 0 ||
630 write_mappings(fp, VFNT_MAP_NORMAL) != 0 ||
631 write_mappings(fp, VFNT_MAP_NORMAL_RH) != 0 ||
632 write_mappings(fp, VFNT_MAP_BOLD) != 0 ||
633 write_mappings(fp, VFNT_MAP_BOLD_RH) != 0) {
644 print_font_info(void)
650 "- glyph_total: %6u\n"
651 "- glyph_normal: %6u\n"
652 "- glyph_normal_right: %6u\n"
653 "- glyph_bold: %6u\n"
654 "- glyph_bold_right: %6u\n"
655 "- glyph_unique: %6u\n"
656 "- glyph_dupe: %6u\n"
657 "- mapping_total: %6u\n"
658 "- mapping_normal: %6u\n"
659 "- mapping_normal_folded: %6u\n"
660 "- mapping_normal_right: %6u\n"
661 "- mapping_normal_right_folded: %6u\n"
662 "- mapping_bold: %6u\n"
663 "- mapping_bold_folded: %6u\n"
664 "- mapping_bold_right: %6u\n"
665 "- mapping_bold_right_folded: %6u\n"
666 "- mapping_unique: %6u\n"
667 "- mapping_dupe: %6u\n",
674 glyph_unique, glyph_dupe,
676 map_count[0], map_folded_count[0],
677 map_count[1], map_folded_count[1],
678 map_count[2], map_folded_count[2],
679 map_count[3], map_folded_count[3],
680 mapping_unique, mapping_dupe);
684 main(int argc, char *argv[])
688 assert(sizeof(struct file_header) == 32);
689 assert(sizeof(struct file_mapping) == 8);
691 while ((ch = getopt(argc, argv, "h:vw:")) != -1) {
694 height = atoi(optarg);
700 width = atoi(optarg);
710 if (argc < 2 || argc > 3)
716 if (parse_file(argv[0], VFNT_MAP_NORMAL) != 0)
721 if (parse_file(argv[0], VFNT_MAP_BOLD) != 0)
727 dedup_mapping(VFNT_MAP_BOLD);
728 dedup_mapping(VFNT_MAP_BOLD_RH);
733 if (write_fnt(argv[0]) != 0)