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 sscanf(ln + 16, "%d %d %d %d", &fbbw, &fbbh, &fbbox,
347 errx(1, "broken font header");
348 if (spc != 'c' && spc != 'C')
349 errx(1, "font spacing \"C\" (character cell) required");
351 /* Step 2: Validate DWIDTH (Device Width) of all glyphs. */
352 while ((ln = fgetln(fp, &length)) != NULL) {
354 ln[length - 1] = '\0';
356 if (strncmp(ln, "DWIDTH ", 7) == 0 &&
357 sscanf(ln + 7, "%d %d", &dwidth, &dwy) == 2) {
358 if (dwy != 0 || (dwidth != fbbw && dwidth * 2 != fbbw))
359 errx(1, "bitmap with unsupported DWIDTH %d %d at line %u",
360 dwidth, dwy, linenum);
366 /* Step 3: Restart at the beginning of the file and read glyph data. */
367 dwidth = bbw = bbh = 0;
370 bbwbytes = 0; /* GCC 4.2.1 "may be used uninitialized" workaround. */
371 bytes = xmalloc(wbytes * height);
372 bytes_r = xmalloc(wbytes * height);
373 line = xmalloc(wbytes * 2);
374 while ((ln = fgetln(fp, &length)) != NULL) {
376 ln[length - 1] = '\0';
378 if (strncmp(ln, "ENCODING ", 9) == 0) {
379 curchar = atoi(ln + 9);
380 } else if (strncmp(ln, "DWIDTH ", 7) == 0) {
381 dwidth = atoi(ln + 7);
382 } else if (strncmp(ln, "BBX ", 4) == 0) {
383 if (sscanf(ln + 4, "%d %d %d %d", &bbw, &bbh, &bbox,
385 errx(1, "invalid BBX at line %u", linenum);
386 if (bbw < 1 || bbh < 1 || bbw > fbbw || bbh > fbbh ||
387 bbox < fbbox || bboy < fbboy ||
388 bbh + bboy > fbbh + fbboy)
389 errx(1, "broken bitmap with BBX %d %d %d %d at line %u",
390 bbw, bbh, bbox, bboy, linenum);
391 bbwbytes = howmany(bbw, 8);
392 } else if (strncmp(ln, "BITMAP", 6) == 0 &&
393 (ln[6] == ' ' || ln[6] == '\0')) {
394 if (dwidth == 0 || bbw == 0 || bbh == 0)
395 errx(1, "broken char header at line %u!",
397 memset(bytes, 0, wbytes * height);
398 memset(bytes_r, 0, wbytes * height);
401 * Assume that the next _bbh_ lines are bitmap data.
402 * ENDCHAR is allowed to terminate the bitmap
403 * early but is not otherwise checked; any extra data
406 for (i = (fbbh + fbboy) - (bbh + bboy);
407 i < (unsigned int)((fbbh + fbboy) - bboy); i++) {
408 if ((ln = fgetln(fp, &length)) == NULL)
409 errx(1, "unexpected EOF");
411 ln[length - 1] = '\0';
412 if (strcmp(ln, "ENDCHAR") == 0)
414 if (strlen(ln) < bbwbytes * 2)
415 errx(1, "broken bitmap at line %u",
417 memset(line, 0, wbytes * 2);
418 for (j = 0; j < bbwbytes; j++) {
420 if (sscanf(ln + j * 2, "%2x", &val) ==
423 *(line + j) = (uint8_t)val;
426 rv = rshift_row(line, wbytes * 2, bbw,
431 rv = split_row(bytes + i * wbytes,
432 bytes_r + i * wbytes, line, dwidth);
437 rv = add_char(curchar, map_idx, bytes,
438 dwidth > (int)width ? bytes_r : NULL);
442 dwidth = bbw = bbh = 0;
454 parse_hex(FILE *fp, unsigned int map_idx)
458 uint8_t *bytes = NULL, *bytes_r = NULL, *line = NULL;
459 unsigned curchar = 0, gwidth, gwbytes, i, j, chars_per_row;
462 while ((ln = fgetln(fp, &length)) != NULL) {
463 ln[length - 1] = '\0';
465 if (strncmp(ln, "# Height: ", 10) == 0) {
467 errx(1, "malformed input: Height tag after font data");
468 set_height(atoi(ln + 10));
469 } else if (strncmp(ln, "# Width: ", 9) == 0) {
471 errx(1, "malformed input: Width tag after font data");
472 set_width(atoi(ln + 9));
473 } else if (sscanf(ln, "%6x:", &curchar)) {
475 bytes = xmalloc(wbytes * height);
476 bytes_r = xmalloc(wbytes * height);
477 line = xmalloc(wbytes * 2);
479 /* ln is guaranteed to have a colon here. */
480 p = strchr(ln, ':') + 1;
481 chars_per_row = strlen(p) / height;
482 if (chars_per_row < wbytes * 2)
484 "malformed input: broken bitmap, character %06x",
487 gwbytes = howmany(gwidth, 8);
488 if (chars_per_row < gwbytes * 2 || gwidth <= 8) {
489 gwidth = width; /* Single-width character. */
493 for (i = 0; i < height; i++) {
494 for (j = 0; j < gwbytes; j++) {
496 if (sscanf(p + j * 2, "%2x", &val) == 0)
498 *(line + j) = (uint8_t)val;
500 rv = split_row(bytes + i * wbytes,
501 bytes_r + i * wbytes, line, gwidth);
507 rv = add_char(curchar, map_idx, bytes,
508 gwidth != width ? bytes_r : NULL);
521 parse_file(const char *filename, unsigned int map_idx)
527 fp = fopen(filename, "r");
532 len = strlen(filename);
533 if (len > 4 && strcasecmp(filename + len - 4, ".hex") == 0)
534 rv = parse_hex(fp, map_idx);
536 rv = parse_bdf(fp, map_idx);
545 unsigned int i, idx = 0;
547 for (i = 0; i < VFNT_MAPS; i++)
548 TAILQ_FOREACH(gl, &glyphs[i], g_list)
553 write_glyphs(FILE *fp)
558 for (i = 0; i < VFNT_MAPS; i++) {
559 TAILQ_FOREACH(gl, &glyphs[i], g_list)
560 if (fwrite(gl->g_data, wbytes * height, 1, fp) != 1)
567 fold_mappings(unsigned int map_idx)
569 struct mapping_list *ml = &maps[map_idx];
570 struct mapping *mn, *mp, *mbase;
572 mp = mbase = TAILQ_FIRST(ml);
573 for (mp = mbase = TAILQ_FIRST(ml); mp != NULL; mp = mn) {
574 mn = TAILQ_NEXT(mp, m_list);
575 if (mn != NULL && mn->m_char == mp->m_char + 1 &&
576 mn->m_glyph->g_index == mp->m_glyph->g_index + 1)
578 mbase->m_length = mp->m_char - mbase->m_char + 1;
580 map_folded_count[map_idx]++;
584 struct file_mapping {
586 uint16_t destination;
591 write_mappings(FILE *fp, unsigned int map_idx)
593 struct mapping_list *ml = &maps[map_idx];
595 struct file_mapping fm;
596 unsigned int i = 0, j = 0;
598 TAILQ_FOREACH(mp, ml, m_list) {
600 if (mp->m_length > 0) {
602 fm.source = htobe32(mp->m_char);
603 fm.destination = htobe16(mp->m_glyph->g_index);
604 fm.length = htobe16(mp->m_length - 1);
605 if (fwrite(&fm, sizeof fm, 1, fp) != 1)
618 uint32_t glyph_count;
619 uint32_t map_count[4];
623 write_fnt(const char *filename)
626 struct file_header fh = {
630 fp = fopen(filename, "wb");
638 fh.glyph_count = htobe32(glyph_unique);
639 fh.map_count[0] = htobe32(map_folded_count[0]);
640 fh.map_count[1] = htobe32(map_folded_count[1]);
641 fh.map_count[2] = htobe32(map_folded_count[2]);
642 fh.map_count[3] = htobe32(map_folded_count[3]);
643 if (fwrite(&fh, sizeof fh, 1, fp) != 1) {
649 if (write_glyphs(fp) != 0 ||
650 write_mappings(fp, VFNT_MAP_NORMAL) != 0 ||
651 write_mappings(fp, VFNT_MAP_NORMAL_RH) != 0 ||
652 write_mappings(fp, VFNT_MAP_BOLD) != 0 ||
653 write_mappings(fp, VFNT_MAP_BOLD_RH) != 0) {
664 print_font_info(void)
670 "- glyph_total: %6u\n"
671 "- glyph_normal: %6u\n"
672 "- glyph_normal_right: %6u\n"
673 "- glyph_bold: %6u\n"
674 "- glyph_bold_right: %6u\n"
675 "- glyph_unique: %6u\n"
676 "- glyph_dupe: %6u\n"
677 "- mapping_total: %6u\n"
678 "- mapping_normal: %6u\n"
679 "- mapping_normal_folded: %6u\n"
680 "- mapping_normal_right: %6u\n"
681 "- mapping_normal_right_folded: %6u\n"
682 "- mapping_bold: %6u\n"
683 "- mapping_bold_folded: %6u\n"
684 "- mapping_bold_right: %6u\n"
685 "- mapping_bold_right_folded: %6u\n"
686 "- mapping_unique: %6u\n"
687 "- mapping_dupe: %6u\n",
694 glyph_unique, glyph_dupe,
696 map_count[0], map_folded_count[0],
697 map_count[1], map_folded_count[1],
698 map_count[2], map_folded_count[2],
699 map_count[3], map_folded_count[3],
700 mapping_unique, mapping_dupe);
704 main(int argc, char *argv[])
708 assert(sizeof(struct file_header) == 32);
709 assert(sizeof(struct file_mapping) == 8);
711 while ((ch = getopt(argc, argv, "h:vw:")) != -1) {
714 height = atoi(optarg);
720 width = atoi(optarg);
730 if (argc < 2 || argc > 3)
736 if (parse_file(argv[0], VFNT_MAP_NORMAL) != 0)
741 if (parse_file(argv[0], VFNT_MAP_BOLD) != 0)
747 dedup_mapping(VFNT_MAP_BOLD);
748 dedup_mapping(VFNT_MAP_BOLD_RH);
753 if (write_fnt(argv[0]) != 0)