2 * Copyright (c) 2009 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>
47 #define VFNT_MAP_NORMAL 0
48 #define VFNT_MAP_BOLD 2
50 static unsigned int width = 8, wbytes, height = 16;
53 TAILQ_ENTRY(glyph) g_list;
54 SLIST_ENTRY(glyph) g_hash;
59 #define FONTCVT_NHASH 4096
60 TAILQ_HEAD(glyph_list, glyph);
61 static SLIST_HEAD(, glyph) glyph_hash[FONTCVT_NHASH];
62 static struct glyph_list glyphs[VFNT_MAPS] = {
63 TAILQ_HEAD_INITIALIZER(glyphs[0]),
64 TAILQ_HEAD_INITIALIZER(glyphs[1]),
65 TAILQ_HEAD_INITIALIZER(glyphs[2]),
66 TAILQ_HEAD_INITIALIZER(glyphs[3]),
68 static unsigned int glyph_total, glyph_count[4], glyph_unique, glyph_dupe;
71 TAILQ_ENTRY(mapping) m_list;
73 unsigned int m_length;
74 struct glyph *m_glyph;
77 TAILQ_HEAD(mapping_list, mapping);
78 static struct mapping_list maps[VFNT_MAPS] = {
79 TAILQ_HEAD_INITIALIZER(maps[0]),
80 TAILQ_HEAD_INITIALIZER(maps[1]),
81 TAILQ_HEAD_INITIALIZER(maps[2]),
82 TAILQ_HEAD_INITIALIZER(maps[3]),
84 static unsigned int mapping_total, map_count[4], map_folded_count[4],
85 mapping_unique, mapping_dupe;
92 "usage: fontcvt [-w width] [-h height] normal.bdf [bold.bdf] out.fnt\n");
97 add_mapping(struct glyph *gl, unsigned int c, unsigned int map_idx)
100 struct mapping_list *ml;
104 if (map_idx >= VFNT_MAP_BOLD) {
106 unsigned normal_map_idx = map_idx - VFNT_MAP_BOLD;
108 TAILQ_FOREACH(mp, &maps[normal_map_idx], m_list) {
111 else if (mp->m_char > c)
116 * No mapping is needed if it's equal to the
119 if (mp->m_glyph == gl) {
127 "Character %u not in normal font!\n", c);
132 mp = malloc(sizeof *mp);
138 if (TAILQ_LAST(ml, mapping_list) != NULL &&
139 TAILQ_LAST(ml, mapping_list)->m_char >= c) {
140 fprintf(stderr, "Bad ordering at character %u\n", c);
143 TAILQ_INSERT_TAIL(ml, mp, m_list);
145 map_count[map_idx]++;
151 static struct glyph *
152 add_glyph(const uint8_t *bytes, unsigned int map_idx, int fallback)
158 glyph_count[map_idx]++;
160 hash = fnv_32_buf(bytes, wbytes * height, FNV1_32_INIT) % FONTCVT_NHASH;
161 SLIST_FOREACH(gl, &glyph_hash[hash], g_hash) {
162 if (memcmp(gl->g_data, bytes, wbytes * height) == 0) {
168 gl = malloc(sizeof *gl);
169 gl->g_data = malloc(wbytes * height);
170 memcpy(gl->g_data, bytes, wbytes * height);
172 TAILQ_INSERT_HEAD(&glyphs[map_idx], gl, g_list);
174 TAILQ_INSERT_TAIL(&glyphs[map_idx], gl, g_list);
175 SLIST_INSERT_HEAD(&glyph_hash[hash], gl, g_hash);
182 parse_bitmap_line(uint8_t *left, uint8_t *right, unsigned int line,
186 unsigned int i, subline;
188 if (dwidth != width && dwidth != width * 2) {
190 "Unsupported width %u!\n", dwidth);
194 /* Move pixel data right to simplify splitting double characters. */
195 line >>= (howmany(dwidth, 8) * 8) - dwidth;
197 for (i = dwidth / width; i > 0; i--) {
198 p = (i == 2) ? right : left;
200 subline = line & ((1 << width) - 1);
201 subline <<= (howmany(width, 8) * 8) - width;
205 } else if (wbytes == 2) {
210 "Unsupported wbytes %u!\n", wbytes);
221 parse_bdf(const char *filename, unsigned int map_idx)
226 uint8_t bytes[wbytes * height], bytes_r[wbytes * height];
227 unsigned int curchar = 0, dwidth = 0, i, line;
230 fp = fopen(filename, "r");
236 while ((ln = fgetln(fp, &length)) != NULL) {
237 ln[length - 1] = '\0';
239 if (strncmp(ln, "ENCODING ", 9) == 0) {
240 curchar = atoi(ln + 9);
243 if (strncmp(ln, "DWIDTH ", 7) == 0) {
244 dwidth = atoi(ln + 7);
247 if (strcmp(ln, "BITMAP") == 0) {
248 for (i = 0; i < height; i++) {
249 if ((ln = fgetln(fp, &length)) == NULL) {
250 fprintf(stderr, "Unexpected EOF!\n");
253 ln[length - 1] = '\0';
254 sscanf(ln, "%x", &line);
255 if (parse_bitmap_line(bytes + i * wbytes,
256 bytes_r + i * wbytes, line, dwidth) != 0)
260 /* Prevent adding two glyphs for 0xFFFD */
261 if (curchar == 0xFFFD) {
262 if (map_idx < VFNT_MAP_BOLD)
263 gl = add_glyph(bytes, 0, 1);
264 } else if (curchar >= 0x20) {
265 gl = add_glyph(bytes, map_idx, 0);
266 if (add_mapping(gl, curchar, map_idx) != 0)
268 if (dwidth == width * 2) {
269 gl = add_glyph(bytes_r, map_idx + 1, 0);
270 if (add_mapping(gl, curchar,
285 unsigned int i, idx = 0;
287 for (i = 0; i < VFNT_MAPS; i++)
288 TAILQ_FOREACH(gl, &glyphs[i], g_list)
293 write_glyphs(FILE *fp)
298 for (i = 0; i < VFNT_MAPS; i++) {
299 TAILQ_FOREACH(gl, &glyphs[i], g_list)
300 fwrite(gl->g_data, wbytes * height, 1, fp);
305 fold_mappings(unsigned int map_idx)
307 struct mapping_list *ml = &maps[map_idx];
308 struct mapping *mn, *mp, *mbase;
310 mp = mbase = TAILQ_FIRST(ml);
311 for (mp = mbase = TAILQ_FIRST(ml); mp != NULL; mp = mn) {
312 mn = TAILQ_NEXT(mp, m_list);
313 if (mn != NULL && mn->m_char == mp->m_char + 1 &&
314 mn->m_glyph->g_index == mp->m_glyph->g_index + 1)
316 mbase->m_length = mp->m_char - mbase->m_char + 1;
318 map_folded_count[map_idx]++;
322 struct file_mapping {
324 uint16_t destination;
329 write_mappings(FILE *fp, unsigned int map_idx)
331 struct mapping_list *ml = &maps[map_idx];
333 struct file_mapping fm;
334 unsigned int i = 0, j = 0;
336 TAILQ_FOREACH(mp, ml, m_list) {
338 if (mp->m_length > 0) {
340 fm.source = htobe32(mp->m_char);
341 fm.destination = htobe16(mp->m_glyph->g_index);
342 fm.length = htobe16(mp->m_length - 1);
343 fwrite(&fm, sizeof fm, 1, fp);
354 uint32_t glyph_count;
355 uint32_t map_count[4];
359 write_fnt(const char *filename)
362 struct file_header fh = {
366 fp = fopen(filename, "wb");
374 fh.glyph_count = htobe32(glyph_unique);
375 fh.map_count[0] = htobe32(map_folded_count[0]);
376 fh.map_count[1] = htobe32(map_folded_count[1]);
377 fh.map_count[2] = htobe32(map_folded_count[2]);
378 fh.map_count[3] = htobe32(map_folded_count[3]);
379 fwrite(&fh, sizeof fh, 1, fp);
382 write_mappings(fp, VFNT_MAP_NORMAL);
383 write_mappings(fp, 1);
384 write_mappings(fp, VFNT_MAP_BOLD);
385 write_mappings(fp, 3);
391 main(int argc, char *argv[])
395 assert(sizeof(struct file_header) == 32);
396 assert(sizeof(struct file_mapping) == 8);
398 while ((ch = getopt(argc, argv, "h:w:")) != -1) {
401 height = atoi(optarg);
404 width = atoi(optarg);
414 if (argc < 2 || argc > 3)
417 wbytes = howmany(width, 8);
419 if (parse_bdf(argv[0], VFNT_MAP_NORMAL) != 0)
424 if (parse_bdf(argv[0], VFNT_MAP_BOLD) != 0)
434 if (write_fnt(argv[0]) != 0)
439 "- glyph_total: %5u\n"
440 "- glyph_normal: %5u\n"
441 "- glyph_normal_right: %5u\n"
442 "- glyph_bold: %5u\n"
443 "- glyph_bold_right: %5u\n"
444 "- glyph_unique: %5u\n"
445 "- glyph_dupe: %5u\n"
446 "- mapping_total: %5u\n"
447 "- mapping_normal: %5u\n"
448 "- mapping_normal_folded: %5u\n"
449 "- mapping_normal_right: %5u\n"
450 "- mapping_normal_right_folded: %5u\n"
451 "- mapping_bold: %5u\n"
452 "- mapping_bold_folded: %5u\n"
453 "- mapping_bold_right: %5u\n"
454 "- mapping_bold_right_folded: %5u\n"
455 "- mapping_unique: %5u\n"
456 "- mapping_dupe: %5u\n",
462 glyph_unique, glyph_dupe,
464 map_count[0], map_folded_count[0],
465 map_count[1], map_folded_count[1],
466 map_count[2], map_folded_count[2],
467 map_count[3], map_folded_count[3],
468 mapping_unique, mapping_dupe);