xref: /freebsd/usr.bin/vtfontcvt/vtfontcvt.c (revision 86aa9539fef591a363b06a0ebd3aa7a07f4c1579)
1 /*-
2  * Copyright (c) 2009, 2014 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Ed Schouten under sponsorship from the
6  * FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
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>
38 
39 #include <assert.h>
40 #include <err.h>
41 #include <stdint.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 #define VFNT_MAPS 4
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
54 
55 static unsigned int width = 8, wbytes, height = 16;
56 
57 struct glyph {
58 	TAILQ_ENTRY(glyph)	 g_list;
59 	SLIST_ENTRY(glyph)	 g_hash;
60 	uint8_t			*g_data;
61 	unsigned int		 g_index;
62 };
63 
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]),
72 };
73 static unsigned int glyph_total, glyph_count[4], glyph_unique, glyph_dupe;
74 
75 struct mapping {
76 	TAILQ_ENTRY(mapping)	 m_list;
77 	unsigned int		 m_char;
78 	unsigned int		 m_length;
79 	struct glyph		*m_glyph;
80 };
81 
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]),
88 };
89 static unsigned int mapping_total, map_count[4], map_folded_count[4],
90     mapping_unique, mapping_dupe;
91 
92 static void
93 usage(void)
94 {
95 
96 	(void)fprintf(stderr,
97 "usage: vtfontcvt [-w width] [-h height] [-v] normal.bdf [bold.bdf] out.fnt\n");
98 	exit(1);
99 }
100 
101 static void *
102 xmalloc(size_t size)
103 {
104 	void *m;
105 
106 	if ((m = calloc(1, size)) == NULL)
107 		errx(1, "memory allocation failure");
108 	return (m);
109 }
110 
111 static int
112 add_mapping(struct glyph *gl, unsigned int c, unsigned int map_idx)
113 {
114 	struct mapping *mp, *mp_temp;
115 	struct mapping_list *ml;
116 
117 	mapping_total++;
118 
119 	mp = xmalloc(sizeof *mp);
120 	mp->m_char = c;
121 	mp->m_glyph = gl;
122 	mp->m_length = 0;
123 
124 	ml = &maps[map_idx];
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);
129 	} else {
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);
134 				break;
135 			}
136 		}
137 	}
138 
139 	map_count[map_idx]++;
140 	mapping_unique++;
141 
142 	return (0);
143 }
144 
145 static int
146 dedup_mapping(unsigned int map_idx)
147 {
148 	struct mapping *mp_bold, *mp_normal, *mp_temp;
149 	unsigned normal_map_idx = map_idx - VFNT_MAP_BOLD;
150 
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!",
158 			    mp_bold->m_char);
159 		if (mp_bold->m_glyph != mp_normal->m_glyph)
160 			continue;
161 
162 		/* No mapping is needed if it's equal to the normal mapping. */
163 		TAILQ_REMOVE(&maps[map_idx], mp_bold, m_list);
164 		free(mp_bold);
165 		mapping_dupe++;
166 	}
167 	return (0);
168 }
169 
170 static struct glyph *
171 add_glyph(const uint8_t *bytes, unsigned int map_idx, int fallback)
172 {
173 	struct glyph *gl;
174 	int hash;
175 
176 	glyph_total++;
177 	glyph_count[map_idx]++;
178 
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) {
183 			glyph_dupe++;
184 			return (gl);
185 		}
186 	}
187 
188 	/* Allocate new glyph. */
189 	gl = xmalloc(sizeof *gl);
190 	gl->g_data = xmalloc(wbytes * height);
191 	memcpy(gl->g_data, bytes, wbytes * height);
192 	if (fallback)
193 		TAILQ_INSERT_HEAD(&glyphs[map_idx], gl, g_list);
194 	else
195 		TAILQ_INSERT_TAIL(&glyphs[map_idx], gl, g_list);
196 	SLIST_INSERT_HEAD(&glyph_hash[hash], gl, g_hash);
197 
198 	glyph_unique++;
199 	if (glyph_unique > VFNT_MAXGLYPHS)
200 		errx(1, "too many glyphs (%u)", glyph_unique);
201 	return (gl);
202 }
203 
204 static int
205 add_char(unsigned curchar, unsigned map_idx, uint8_t *bytes, uint8_t *bytes_r)
206 {
207 	struct glyph *gl;
208 
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)
216 			return (1);
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)
220 				return (1);
221 		}
222 	}
223 	return (0);
224 }
225 
226 /*
227  * Right-shift glyph row by _shift_ bits. Row _len_ bits wide, _size_ bytes.
228  */
229 static int
230 rshift_row(uint8_t *line, size_t size, size_t len, size_t shift)
231 {
232 	size_t d, s, i;
233 	uint16_t t;
234 
235 	assert(size > 0 && len > 0);
236 	assert(size * 8 >= len);
237 
238 	if (shift == 0)
239 		return (0);
240 
241 	d = shift / 8;
242 	s = 8 - shift % 8;
243 	i = howmany(len, 8);
244 
245 	while (i > 0) {
246 		i--;
247 
248 		t = *(line + i);
249 		*(line + i) = 0;
250 
251 		t <<= s;
252 
253 		if (i + d + 1 < size)
254 			*(line + i + d + 1) |= (uint8_t)t;
255 		if (i + d < size)
256 			*(line + i + d) = t >> 8;
257 	}
258 	return (0);
259 }
260 
261 /*
262  * Split double-width characters into left and right half. Single-width
263  * characters in _left_ only.
264  */
265 static int
266 split_row(uint8_t *left, uint8_t *right, uint8_t *line, size_t w)
267 {
268 	size_t s, i;
269 
270 	s = wbytes * 8 - width;
271 
272 	memcpy(left, line, wbytes);
273 	*(left + wbytes - 1) &= 0xFF << s;
274 
275 	if (w > width) { /* Double-width character. */
276 		uint8_t t;
277 
278 		for (i = 0; i < wbytes; i++) {
279 			t = *(line + wbytes + i - 1);
280 			t <<= 8 - s;
281 			t |= *(line + wbytes + i) >> s;
282 			*(right + i) = t;
283 		}
284 		*(right + wbytes - 1) &= 0xFF << s;
285 	}
286 	return (0);
287 }
288 
289 static void
290 set_height(int h)
291 {
292 	if (h <= 0 || h > VFNT_MAXDIMENSION)
293 		errx(1, "invalid height %d", h);
294 	height = h;
295 }
296 
297 static void
298 set_width(int w)
299 {
300 	if (w <= 0 || w > VFNT_MAXDIMENSION)
301 		errx(1, "invalid width %d", w);
302 	width = w;
303 	wbytes = howmany(width, 8);
304 }
305 
306 static int
307 parse_bdf(FILE *fp, unsigned int map_idx)
308 {
309 	char *line, *ln, *p;
310 	size_t length;
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;
316 	int rv = -1;
317 	char spc = '\0';
318 
319 	/*
320 	 * Step 1: Parse FONT logical font descriptor and FONTBOUNDINGBOX
321 	 * bounding box.
322 	 */
323 	while ((ln = fgetln(fp, &length)) != NULL) {
324 		linenum++;
325 		ln[length - 1] = '\0';
326 
327 		if (strncmp(ln, "FONT ", 5) == 0) {
328 			p = ln + 5;
329 			i = 0;
330 			while ((p = strchr(p, '-')) != NULL) {
331 				p++;
332 				i++;
333 				if (i == 11) {
334 					spc = *p;
335 					break;
336 				}
337 			}
338 		} else if (strncmp(ln, "FONTBOUNDINGBOX ", 16) == 0) {
339 			if (sscanf(ln + 16, "%d %d %d %d", &fbbw, &fbbh, &fbbox,
340 			    &fbboy) != 4)
341 				errx(1, "invalid FONTBOUNDINGBOX at line %u",
342 				    linenum);
343 			set_width(fbbw);
344 			set_height(fbbh);
345 			break;
346 		}
347 	}
348 	if (fbbw == 0)
349 		errx(1, "broken font header");
350 	if (spc != 'c' && spc != 'C')
351 		errx(1, "font spacing \"C\" (character cell) required");
352 
353 	/* Step 2: Validate DWIDTH (Device Width) of all glyphs. */
354 	while ((ln = fgetln(fp, &length)) != NULL) {
355 		linenum++;
356 		ln[length - 1] = '\0';
357 
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);
364 			if (dwidth < fbbw)
365 				set_width(dwidth);
366 		}
367 	}
368 
369 	/* Step 3: Restart at the beginning of the file and read glyph data. */
370 	dwidth = bbw = bbh = 0;
371 	rewind(fp);
372 	linenum = 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) {
378 		linenum++;
379 		ln[length - 1] = '\0';
380 
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,
387 			     &bboy) != 4)
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!",
399 				    linenum);
400 			memset(bytes, 0, wbytes * height);
401 			memset(bytes_r, 0, wbytes * height);
402 
403 			/*
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
407 			 * is ignored.
408 			 */
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");
413 				linenum++;
414 				ln[length - 1] = '\0';
415 				if (strcmp(ln, "ENDCHAR") == 0)
416 					break;
417 				if (strlen(ln) < bbwbytes * 2)
418 					errx(1, "broken bitmap at line %u",
419 					    linenum);
420 				memset(line, 0, wbytes * 2);
421 				for (j = 0; j < bbwbytes; j++) {
422 					unsigned int val;
423 					if (sscanf(ln + j * 2, "%2x", &val) ==
424 					    0)
425 						break;
426 					*(line + j) = (uint8_t)val;
427 				}
428 
429 				rv = rshift_row(line, wbytes * 2, bbw,
430 				    bbox - fbbox);
431 				if (rv != 0)
432 					goto out;
433 
434 				rv = split_row(bytes + i * wbytes,
435 				     bytes_r + i * wbytes, line, dwidth);
436 				if (rv != 0)
437 					goto out;
438 			}
439 
440 			rv = add_char(curchar, map_idx, bytes,
441 			    dwidth > (int)width ? bytes_r : NULL);
442 			if (rv != 0)
443 				goto out;
444 
445 			dwidth = bbw = bbh = 0;
446 		}
447 	}
448 
449 out:
450 	free(bytes);
451 	free(bytes_r);
452 	free(line);
453 	return (rv);
454 }
455 
456 static int
457 parse_hex(FILE *fp, unsigned int map_idx)
458 {
459 	char *ln, *p;
460 	size_t length;
461 	uint8_t *bytes = NULL, *bytes_r = NULL, *line = NULL;
462 	unsigned curchar = 0, gwidth, gwbytes, i, j, chars_per_row;
463 	int rv = 0;
464 
465 	while ((ln = fgetln(fp, &length)) != NULL) {
466 		ln[length - 1] = '\0';
467 
468 		if (strncmp(ln, "# Height: ", 10) == 0) {
469 			if (bytes != NULL)
470 				errx(1, "malformed input: Height tag after font data");
471 			set_height(atoi(ln + 10));
472 		} else if (strncmp(ln, "# Width: ", 9) == 0) {
473 			if (bytes != NULL)
474 				errx(1, "malformed input: Width tag after font data");
475 			set_width(atoi(ln + 9));
476 		} else if (sscanf(ln, "%6x:", &curchar)) {
477 			if (bytes == NULL) {
478 				bytes = xmalloc(wbytes * height);
479 				bytes_r = xmalloc(wbytes * height);
480 				line = xmalloc(wbytes * 2);
481 			}
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)
486 				errx(1,
487 				    "malformed input: broken bitmap, character %06x",
488 				    curchar);
489 			gwidth = width * 2;
490 			gwbytes = howmany(gwidth, 8);
491 			if (chars_per_row < gwbytes * 2 || gwidth <= 8) {
492 				gwidth = width; /* Single-width character. */
493 				gwbytes = wbytes;
494 			}
495 
496 			for (i = 0; i < height; i++) {
497 				for (j = 0; j < gwbytes; j++) {
498 					unsigned int val;
499 					if (sscanf(p + j * 2, "%2x", &val) == 0)
500 						break;
501 					*(line + j) = (uint8_t)val;
502 				}
503 				rv = split_row(bytes + i * wbytes,
504 				    bytes_r + i * wbytes, line, gwidth);
505 				if (rv != 0)
506 					goto out;
507 				p += gwbytes * 2;
508 			}
509 
510 			rv = add_char(curchar, map_idx, bytes,
511 			    gwidth != width ? bytes_r : NULL);
512 			if (rv != 0)
513 				goto out;
514 		}
515 	}
516 out:
517 	free(bytes);
518 	free(bytes_r);
519 	free(line);
520 	return (rv);
521 }
522 
523 static int
524 parse_file(const char *filename, unsigned int map_idx)
525 {
526 	FILE *fp;
527 	size_t len;
528 	int rv;
529 
530 	fp = fopen(filename, "r");
531 	if (fp == NULL) {
532 		perror(filename);
533 		return (1);
534 	}
535 	len = strlen(filename);
536 	if (len > 4 && strcasecmp(filename + len - 4, ".hex") == 0)
537 		rv = parse_hex(fp, map_idx);
538 	else
539 		rv = parse_bdf(fp, map_idx);
540 	fclose(fp);
541 	return (rv);
542 }
543 
544 static void
545 number_glyphs(void)
546 {
547 	struct glyph *gl;
548 	unsigned int i, idx = 0;
549 
550 	for (i = 0; i < VFNT_MAPS; i++)
551 		TAILQ_FOREACH(gl, &glyphs[i], g_list)
552 			gl->g_index = idx++;
553 }
554 
555 static int
556 write_glyphs(FILE *fp)
557 {
558 	struct glyph *gl;
559 	unsigned int i;
560 
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)
564 				return (1);
565 	}
566 	return (0);
567 }
568 
569 static void
570 fold_mappings(unsigned int map_idx)
571 {
572 	struct mapping_list *ml = &maps[map_idx];
573 	struct mapping *mn, *mp, *mbase;
574 
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)
580 			continue;
581 		mbase->m_length = mp->m_char - mbase->m_char + 1;
582 		mbase = mp = mn;
583 		map_folded_count[map_idx]++;
584 	}
585 }
586 
587 struct file_mapping {
588 	uint32_t	source;
589 	uint16_t	destination;
590 	uint16_t	length;
591 } __packed;
592 
593 static int
594 write_mappings(FILE *fp, unsigned int map_idx)
595 {
596 	struct mapping_list *ml = &maps[map_idx];
597 	struct mapping *mp;
598 	struct file_mapping fm;
599 	unsigned int i = 0, j = 0;
600 
601 	TAILQ_FOREACH(mp, ml, m_list) {
602 		j++;
603 		if (mp->m_length > 0) {
604 			i += mp->m_length;
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)
609 				return (1);
610 		}
611 	}
612 	assert(i == j);
613 	return (0);
614 }
615 
616 struct file_header {
617 	uint8_t		magic[8];
618 	uint8_t		width;
619 	uint8_t		height;
620 	uint16_t	pad;
621 	uint32_t	glyph_count;
622 	uint32_t	map_count[4];
623 } __packed;
624 
625 static int
626 write_fnt(const char *filename)
627 {
628 	FILE *fp;
629 	struct file_header fh = {
630 		.magic = "VFNT0002",
631 	};
632 
633 	fp = fopen(filename, "wb");
634 	if (fp == NULL) {
635 		perror(filename);
636 		return (1);
637 	}
638 
639 	fh.width = width;
640 	fh.height = height;
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) {
647 		perror(filename);
648 		fclose(fp);
649 		return (1);
650 	}
651 
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) {
657 		perror(filename);
658 		fclose(fp);
659 		return (1);
660 	}
661 
662 	fclose(fp);
663 	return (0);
664 }
665 
666 static void
667 print_font_info(void)
668 {
669 	printf(
670 "Statistics:\n"
671 "- width:                       %6u\n"
672 "- height:                      %6u\n"
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",
691 	    width, height,
692 	    glyph_total,
693 	    glyph_count[0],
694 	    glyph_count[1],
695 	    glyph_count[2],
696 	    glyph_count[3],
697 	    glyph_unique, glyph_dupe,
698 	    mapping_total,
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);
704 }
705 
706 int
707 main(int argc, char *argv[])
708 {
709 	int ch, verbose = 0;
710 
711 	assert(sizeof(struct file_header) == 32);
712 	assert(sizeof(struct file_mapping) == 8);
713 
714 	while ((ch = getopt(argc, argv, "h:vw:")) != -1) {
715 		switch (ch) {
716 		case 'h':
717 			height = atoi(optarg);
718 			break;
719 		case 'v':
720 			verbose = 1;
721 			break;
722 		case 'w':
723 			width = atoi(optarg);
724 			break;
725 		case '?':
726 		default:
727 			usage();
728 		}
729 	}
730 	argc -= optind;
731 	argv += optind;
732 
733 	if (argc < 2 || argc > 3)
734 		usage();
735 
736 	set_width(width);
737 	set_height(height);
738 
739 	if (parse_file(argv[0], VFNT_MAP_NORMAL) != 0)
740 		return (1);
741 	argc--;
742 	argv++;
743 	if (argc == 2) {
744 		if (parse_file(argv[0], VFNT_MAP_BOLD) != 0)
745 			return (1);
746 		argc--;
747 		argv++;
748 	}
749 	number_glyphs();
750 	dedup_mapping(VFNT_MAP_BOLD);
751 	dedup_mapping(VFNT_MAP_BOLD_RH);
752 	fold_mappings(0);
753 	fold_mappings(1);
754 	fold_mappings(2);
755 	fold_mappings(3);
756 	if (write_fnt(argv[0]) != 0)
757 		return (1);
758 
759 	if (verbose)
760 		print_font_info();
761 
762 	return (0);
763 }
764