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/endian.h>
34 #include <sys/param.h>
35 #include <sys/queue.h>
44 #define VFNT_MAP_NORMAL 0
45 #define VFNT_MAP_BOLD 2
47 static unsigned int width, wbytes, height;
50 TAILQ_ENTRY(glyph) g_list;
55 TAILQ_HEAD(glyph_list, glyph);
56 static struct glyph_list glyphs[VFNT_MAPS] = {
57 TAILQ_HEAD_INITIALIZER(glyphs[0]),
58 TAILQ_HEAD_INITIALIZER(glyphs[1]),
59 TAILQ_HEAD_INITIALIZER(glyphs[2]),
60 TAILQ_HEAD_INITIALIZER(glyphs[3]),
62 static unsigned int glyph_total, glyph_count[4], glyph_unique, glyph_dupe;
65 TAILQ_ENTRY(mapping) m_list;
67 unsigned int m_length;
68 struct glyph *m_glyph;
71 TAILQ_HEAD(mapping_list, mapping);
72 static struct mapping_list maps[VFNT_MAPS] = {
73 TAILQ_HEAD_INITIALIZER(maps[0]),
74 TAILQ_HEAD_INITIALIZER(maps[1]),
75 TAILQ_HEAD_INITIALIZER(maps[2]),
76 TAILQ_HEAD_INITIALIZER(maps[3]),
78 static unsigned int mapping_total, map_count[4], map_folded_count[4],
79 mapping_unique, mapping_dupe;
86 "usage: fontcvt width height normal.bdf bold.bdf out.fnt\n");
91 add_mapping(struct glyph *gl, unsigned int c, unsigned int map_idx)
94 struct mapping_list *ml;
98 if (map_idx >= VFNT_MAP_BOLD) {
100 unsigned normal_map_idx = map_idx - VFNT_MAP_BOLD;
102 TAILQ_FOREACH(mp, &maps[normal_map_idx], m_list) {
105 else if (mp->m_char > c)
110 * No mapping is needed if it's equal to the
113 if (mp->m_glyph == gl) {
121 "Character %u not in normal font!\n", c);
126 mp = malloc(sizeof *mp);
132 if (TAILQ_LAST(ml, mapping_list) != NULL &&
133 TAILQ_LAST(ml, mapping_list)->m_char >= c) {
134 fprintf(stderr, "Bad ordering at character %u\n", c);
137 TAILQ_INSERT_TAIL(ml, mp, m_list);
139 map_count[map_idx]++;
145 static struct glyph *
146 add_glyph(const uint8_t *bytes, unsigned int map_idx, int fallback)
152 glyph_count[map_idx]++;
154 for (i = 0; i < VFNT_MAPS; i++) {
155 TAILQ_FOREACH(gl, &glyphs[i], g_list) {
156 if (memcmp(gl->g_data, bytes, wbytes * height) == 0) {
163 gl = malloc(sizeof *gl);
164 gl->g_data = malloc(wbytes * height);
165 memcpy(gl->g_data, bytes, wbytes * height);
167 TAILQ_INSERT_HEAD(&glyphs[map_idx], gl, g_list);
169 TAILQ_INSERT_TAIL(&glyphs[map_idx], gl, g_list);
176 parse_bitmap_line(uint8_t *left, uint8_t *right, unsigned int line,
180 unsigned int i, subline;
182 if (dwidth != width && dwidth != width * 2) {
184 "Unsupported width %u!\n", dwidth);
188 /* Move pixel data right to simplify splitting double characters. */
189 line >>= (howmany(dwidth, 8) * 8) - dwidth;
191 for (i = dwidth / width; i > 0; i--) {
192 p = (i == 2) ? right : left;
194 subline = line & ((1 << width) - 1);
195 subline <<= (howmany(width, 8) * 8) - width;
199 } else if (wbytes == 2) {
204 "Unsupported wbytes %u!\n", wbytes);
215 parse_bdf(const char *filename, unsigned int map_idx)
220 uint8_t bytes[wbytes * height], bytes_r[wbytes * height];
221 unsigned int curchar = 0, dwidth = 0, i, line;
224 fp = fopen(filename, "r");
230 while ((ln = fgetln(fp, &length)) != NULL) {
231 ln[length - 1] = '\0';
233 if (strncmp(ln, "ENCODING ", 9) == 0) {
234 curchar = atoi(ln + 9);
237 if (strncmp(ln, "DWIDTH ", 7) == 0) {
238 dwidth = atoi(ln + 7);
241 if (strcmp(ln, "BITMAP") == 0) {
242 for (i = 0; i < height; i++) {
243 if ((ln = fgetln(fp, &length)) == NULL) {
244 fprintf(stderr, "Unexpected EOF!\n");
247 ln[length - 1] = '\0';
248 sscanf(ln, "%x", &line);
249 if (parse_bitmap_line(bytes + i * wbytes,
250 bytes_r + i * wbytes, line, dwidth) != 0)
254 /* Prevent adding two glyphs for 0xFFFD */
255 if (curchar == 0xFFFD) {
256 if (map_idx < VFNT_MAP_BOLD)
257 gl = add_glyph(bytes, 0, 1);
258 } else if (curchar >= 0x20) {
259 gl = add_glyph(bytes, map_idx, 0);
260 if (add_mapping(gl, curchar, map_idx) != 0)
262 if (dwidth == width * 2) {
263 gl = add_glyph(bytes_r, map_idx + 1, 0);
264 if (add_mapping(gl, curchar,
279 unsigned int i, idx = 0;
281 for (i = 0; i < VFNT_MAPS; i++)
282 TAILQ_FOREACH(gl, &glyphs[i], g_list)
287 write_glyphs(FILE *fp)
292 for (i = 0; i < VFNT_MAPS; i++) {
293 TAILQ_FOREACH(gl, &glyphs[i], g_list)
294 fwrite(gl->g_data, wbytes * height, 1, fp);
299 fold_mappings(unsigned int map_idx)
301 struct mapping_list *ml = &maps[map_idx];
302 struct mapping *mn, *mp, *mbase;
304 mp = mbase = TAILQ_FIRST(ml);
305 for (mp = mbase = TAILQ_FIRST(ml); mp != NULL; mp = mn) {
306 mn = TAILQ_NEXT(mp, m_list);
307 if (mn != NULL && mn->m_char == mp->m_char + 1 &&
308 mn->m_glyph->g_index == mp->m_glyph->g_index + 1)
310 mbase->m_length = mp->m_char - mbase->m_char + 1;
312 map_folded_count[map_idx]++;
316 struct file_mapping {
318 uint16_t destination;
323 write_mappings(FILE *fp, unsigned int map_idx)
325 struct mapping_list *ml = &maps[map_idx];
327 struct file_mapping fm;
328 unsigned int i = 0, j = 0;
330 TAILQ_FOREACH(mp, ml, m_list) {
332 if (mp->m_length > 0) {
334 fm.source = htobe32(mp->m_char);
335 fm.destination = htobe16(mp->m_glyph->g_index);
336 fm.length = htobe16(mp->m_length - 1);
337 fwrite(&fm, sizeof fm, 1, fp);
348 uint32_t glyph_count;
349 uint32_t map_count[4];
353 write_fnt(const char *filename)
356 struct file_header fh = {
360 fp = fopen(filename, "wb");
368 fh.glyph_count = htobe32(glyph_unique);
369 fh.map_count[0] = htobe32(map_folded_count[0]);
370 fh.map_count[1] = htobe32(map_folded_count[1]);
371 fh.map_count[2] = htobe32(map_folded_count[2]);
372 fh.map_count[3] = htobe32(map_folded_count[3]);
373 fwrite(&fh, sizeof fh, 1, fp);
376 write_mappings(fp, VFNT_MAP_NORMAL);
377 write_mappings(fp, 1);
378 write_mappings(fp, VFNT_MAP_BOLD);
379 write_mappings(fp, 3);
385 main(int argc, char *argv[])
388 assert(sizeof(struct file_header) == 32);
389 assert(sizeof(struct file_mapping) == 8);
394 width = atoi(argv[1]);
395 wbytes = howmany(width, 8);
396 height = atoi(argv[2]);
398 if (parse_bdf(argv[3], VFNT_MAP_NORMAL) != 0)
400 if (parse_bdf(argv[4], VFNT_MAP_BOLD) != 0)
407 if (write_fnt(argv[5]) != 0)
412 "- glyph_total: %5u\n"
413 "- glyph_normal: %5u\n"
414 "- glyph_normal_right: %5u\n"
415 "- glyph_bold: %5u\n"
416 "- glyph_bold_right: %5u\n"
417 "- glyph_unique: %5u\n"
418 "- glyph_dupe: %5u\n"
419 "- mapping_total: %5u\n"
420 "- mapping_normal: %5u\n"
421 "- mapping_normal_folded: %5u\n"
422 "- mapping_normal_right: %5u\n"
423 "- mapping_normal_right_folded: %5u\n"
424 "- mapping_bold: %5u\n"
425 "- mapping_bold_folded: %5u\n"
426 "- mapping_bold_right: %5u\n"
427 "- mapping_bold_right_folded: %5u\n"
428 "- mapping_unique: %5u\n"
429 "- mapping_dupe: %5u\n",
435 glyph_unique, glyph_dupe,
437 map_count[0], map_folded_count[0],
438 map_count[1], map_folded_count[1],
439 map_count[2], map_folded_count[2],
440 map_count[3], map_folded_count[3],
441 mapping_unique, mapping_dupe);