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 by _shift_ bits. Row _len_ bits wide, _size_ bytes.
230 rshift_row(uint8_t *line, size_t size, size_t len, size_t shift)
235 assert(size > 0 && len > 0);
236 assert(size * 8 >= len);
253 if (i + d + 1 < size)
254 *(line + i + d + 1) |= (uint8_t)t;
256 *(line + i + d) = t >> 8;
262 * Split double-width characters into left and right half. Single-width
263 * characters in _left_ only.
266 split_row(uint8_t *left, uint8_t *right, uint8_t *line, size_t w)
270 s = wbytes * 8 - width;
272 memcpy(left, line, wbytes);
273 *(left + wbytes - 1) &= 0xFF << s;
275 if (w > width) { /* Double-width character. */
278 for (i = 0; i < wbytes; i++) {
279 t = *(line + wbytes + i - 1);
281 t |= *(line + wbytes + i) >> s;
284 *(right + wbytes - 1) &= 0xFF << s;
292 if (h <= 0 || h > VFNT_MAXDIMENSION)
293 errx(1, "invalid height %d", h);
300 if (w <= 0 || w > VFNT_MAXDIMENSION)
301 errx(1, "invalid width %d", w);
303 wbytes = howmany(width, 8);
307 parse_bdf(FILE *fp, unsigned int map_idx)
311 uint8_t *bytes, *bytes_r;
312 unsigned int curchar = 0, i, j, linenum = 0, bbwbytes;
313 int bbw, bbh, bbox, bboy; /* Glyph bounding box. */
314 int fbbw = 0, fbbh, fbbox, fbboy; /* Font bounding box. */
315 int dwidth = 0, dwy = 0;
320 * Step 1: Parse FONT logical font descriptor and FONTBOUNDINGBOX
323 while ((ln = fgetln(fp, &length)) != NULL) {
325 ln[length - 1] = '\0';
327 if (strncmp(ln, "FONT ", 5) == 0) {
330 while ((p = strchr(p, '-')) != NULL) {
338 } else if (strncmp(ln, "FONTBOUNDINGBOX ", 16) == 0) {
339 if (sscanf(ln + 16, "%d %d %d %d", &fbbw, &fbbh, &fbbox,
341 errx(1, "invalid FONTBOUNDINGBOX at line %u",
349 errx(1, "broken font header");
350 if (spc != 'c' && spc != 'C')
351 errx(1, "font spacing \"C\" (character cell) required");
353 /* Step 2: Validate DWIDTH (Device Width) of all glyphs. */
354 while ((ln = fgetln(fp, &length)) != NULL) {
356 ln[length - 1] = '\0';
358 if (strncmp(ln, "DWIDTH ", 7) == 0) {
359 if (sscanf(ln + 7, "%d %d", &dwidth, &dwy) != 2)
360 errx(1, "invalid DWIDTH at line %u", linenum);
361 if (dwy != 0 || (dwidth != fbbw && dwidth * 2 != fbbw))
362 errx(1, "bitmap with unsupported DWIDTH %d %d at line %u",
363 dwidth, dwy, linenum);
369 /* Step 3: Restart at the beginning of the file and read glyph data. */
370 dwidth = bbw = bbh = 0;
373 bbwbytes = 0; /* GCC 4.2.1 "may be used uninitialized" workaround. */
374 bytes = xmalloc(wbytes * height);
375 bytes_r = xmalloc(wbytes * height);
376 line = xmalloc(wbytes * 2);
377 while ((ln = fgetln(fp, &length)) != NULL) {
379 ln[length - 1] = '\0';
381 if (strncmp(ln, "ENCODING ", 9) == 0) {
382 curchar = atoi(ln + 9);
383 } else if (strncmp(ln, "DWIDTH ", 7) == 0) {
384 dwidth = atoi(ln + 7);
385 } else if (strncmp(ln, "BBX ", 4) == 0) {
386 if (sscanf(ln + 4, "%d %d %d %d", &bbw, &bbh, &bbox,
388 errx(1, "invalid BBX at line %u", linenum);
389 if (bbw < 1 || bbh < 1 || bbw > fbbw || bbh > fbbh ||
390 bbox < fbbox || bboy < fbboy ||
391 bbh + bboy > fbbh + fbboy)
392 errx(1, "broken bitmap with BBX %d %d %d %d at line %u",
393 bbw, bbh, bbox, bboy, linenum);
394 bbwbytes = howmany(bbw, 8);
395 } else if (strncmp(ln, "BITMAP", 6) == 0 &&
396 (ln[6] == ' ' || ln[6] == '\0')) {
397 if (dwidth == 0 || bbw == 0 || bbh == 0)
398 errx(1, "broken char header at line %u!",
400 memset(bytes, 0, wbytes * height);
401 memset(bytes_r, 0, wbytes * height);
404 * Assume that the next _bbh_ lines are bitmap data.
405 * ENDCHAR is allowed to terminate the bitmap
406 * early but is not otherwise checked; any extra data
409 for (i = (fbbh + fbboy) - (bbh + bboy);
410 i < (unsigned int)((fbbh + fbboy) - bboy); i++) {
411 if ((ln = fgetln(fp, &length)) == NULL)
412 errx(1, "unexpected EOF");
414 ln[length - 1] = '\0';
415 if (strcmp(ln, "ENDCHAR") == 0)
417 if (strlen(ln) < bbwbytes * 2)
418 errx(1, "broken bitmap at line %u",
420 memset(line, 0, wbytes * 2);
421 for (j = 0; j < bbwbytes; j++) {
423 if (sscanf(ln + j * 2, "%2x", &val) ==
426 *(line + j) = (uint8_t)val;
429 rv = rshift_row(line, wbytes * 2, bbw,
434 rv = split_row(bytes + i * wbytes,
435 bytes_r + i * wbytes, line, dwidth);
440 rv = add_char(curchar, map_idx, bytes,
441 dwidth > (int)width ? bytes_r : NULL);
445 dwidth = bbw = bbh = 0;
457 parse_hex(FILE *fp, unsigned int map_idx)
461 uint8_t *bytes = NULL, *bytes_r = NULL, *line = NULL;
462 unsigned curchar = 0, gwidth, gwbytes, i, j, chars_per_row;
465 while ((ln = fgetln(fp, &length)) != NULL) {
466 ln[length - 1] = '\0';
468 if (strncmp(ln, "# Height: ", 10) == 0) {
470 errx(1, "malformed input: Height tag after font data");
471 set_height(atoi(ln + 10));
472 } else if (strncmp(ln, "# Width: ", 9) == 0) {
474 errx(1, "malformed input: Width tag after font data");
475 set_width(atoi(ln + 9));
476 } else if (sscanf(ln, "%6x:", &curchar)) {
478 bytes = xmalloc(wbytes * height);
479 bytes_r = xmalloc(wbytes * height);
480 line = xmalloc(wbytes * 2);
482 /* ln is guaranteed to have a colon here. */
483 p = strchr(ln, ':') + 1;
484 chars_per_row = strlen(p) / height;
485 if (chars_per_row < wbytes * 2)
487 "malformed input: broken bitmap, character %06x",
490 gwbytes = howmany(gwidth, 8);
491 if (chars_per_row < gwbytes * 2 || gwidth <= 8) {
492 gwidth = width; /* Single-width character. */
496 for (i = 0; i < height; i++) {
497 for (j = 0; j < gwbytes; j++) {
499 if (sscanf(p + j * 2, "%2x", &val) == 0)
501 *(line + j) = (uint8_t)val;
503 rv = split_row(bytes + i * wbytes,
504 bytes_r + i * wbytes, line, gwidth);
510 rv = add_char(curchar, map_idx, bytes,
511 gwidth != width ? bytes_r : NULL);
524 parse_file(const char *filename, unsigned int map_idx)
530 fp = fopen(filename, "r");
535 len = strlen(filename);
536 if (len > 4 && strcasecmp(filename + len - 4, ".hex") == 0)
537 rv = parse_hex(fp, map_idx);
539 rv = parse_bdf(fp, map_idx);
548 unsigned int i, idx = 0;
550 for (i = 0; i < VFNT_MAPS; i++)
551 TAILQ_FOREACH(gl, &glyphs[i], g_list)
556 write_glyphs(FILE *fp)
561 for (i = 0; i < VFNT_MAPS; i++) {
562 TAILQ_FOREACH(gl, &glyphs[i], g_list)
563 if (fwrite(gl->g_data, wbytes * height, 1, fp) != 1)
570 fold_mappings(unsigned int map_idx)
572 struct mapping_list *ml = &maps[map_idx];
573 struct mapping *mn, *mp, *mbase;
575 mp = mbase = TAILQ_FIRST(ml);
576 for (mp = mbase = TAILQ_FIRST(ml); mp != NULL; mp = mn) {
577 mn = TAILQ_NEXT(mp, m_list);
578 if (mn != NULL && mn->m_char == mp->m_char + 1 &&
579 mn->m_glyph->g_index == mp->m_glyph->g_index + 1)
581 mbase->m_length = mp->m_char - mbase->m_char + 1;
583 map_folded_count[map_idx]++;
587 struct file_mapping {
589 uint16_t destination;
594 write_mappings(FILE *fp, unsigned int map_idx)
596 struct mapping_list *ml = &maps[map_idx];
598 struct file_mapping fm;
599 unsigned int i = 0, j = 0;
601 TAILQ_FOREACH(mp, ml, m_list) {
603 if (mp->m_length > 0) {
605 fm.source = htobe32(mp->m_char);
606 fm.destination = htobe16(mp->m_glyph->g_index);
607 fm.length = htobe16(mp->m_length - 1);
608 if (fwrite(&fm, sizeof fm, 1, fp) != 1)
621 uint32_t glyph_count;
622 uint32_t map_count[4];
626 write_fnt(const char *filename)
629 struct file_header fh = {
633 fp = fopen(filename, "wb");
641 fh.glyph_count = htobe32(glyph_unique);
642 fh.map_count[0] = htobe32(map_folded_count[0]);
643 fh.map_count[1] = htobe32(map_folded_count[1]);
644 fh.map_count[2] = htobe32(map_folded_count[2]);
645 fh.map_count[3] = htobe32(map_folded_count[3]);
646 if (fwrite(&fh, sizeof fh, 1, fp) != 1) {
652 if (write_glyphs(fp) != 0 ||
653 write_mappings(fp, VFNT_MAP_NORMAL) != 0 ||
654 write_mappings(fp, VFNT_MAP_NORMAL_RH) != 0 ||
655 write_mappings(fp, VFNT_MAP_BOLD) != 0 ||
656 write_mappings(fp, VFNT_MAP_BOLD_RH) != 0) {
667 print_font_info(void)
673 "- glyph_total: %6u\n"
674 "- glyph_normal: %6u\n"
675 "- glyph_normal_right: %6u\n"
676 "- glyph_bold: %6u\n"
677 "- glyph_bold_right: %6u\n"
678 "- glyph_unique: %6u\n"
679 "- glyph_dupe: %6u\n"
680 "- mapping_total: %6u\n"
681 "- mapping_normal: %6u\n"
682 "- mapping_normal_folded: %6u\n"
683 "- mapping_normal_right: %6u\n"
684 "- mapping_normal_right_folded: %6u\n"
685 "- mapping_bold: %6u\n"
686 "- mapping_bold_folded: %6u\n"
687 "- mapping_bold_right: %6u\n"
688 "- mapping_bold_right_folded: %6u\n"
689 "- mapping_unique: %6u\n"
690 "- mapping_dupe: %6u\n",
697 glyph_unique, glyph_dupe,
699 map_count[0], map_folded_count[0],
700 map_count[1], map_folded_count[1],
701 map_count[2], map_folded_count[2],
702 map_count[3], map_folded_count[3],
703 mapping_unique, mapping_dupe);
707 main(int argc, char *argv[])
711 assert(sizeof(struct file_header) == 32);
712 assert(sizeof(struct file_mapping) == 8);
714 while ((ch = getopt(argc, argv, "h:vw:")) != -1) {
717 height = atoi(optarg);
723 width = atoi(optarg);
733 if (argc < 2 || argc > 3)
739 if (parse_file(argv[0], VFNT_MAP_NORMAL) != 0)
744 if (parse_file(argv[0], VFNT_MAP_BOLD) != 0)
750 dedup_mapping(VFNT_MAP_BOLD);
751 dedup_mapping(VFNT_MAP_BOLD_RH);
756 if (write_fnt(argv[0]) != 0)