xref: /freebsd/usr.bin/vtfontcvt/vtfontcvt.c (revision 20bd59416dcacbd2b776fe49dfa193900f303287)
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.
228  */
229 static void
230 rshift_row(uint8_t *buf, size_t len, size_t shift)
231 {
232 	ssize_t i, off_byte = shift / 8;
233 	size_t off_bit = shift % 8;
234 
235 	if (shift == 0)
236 		return;
237 	for (i = len - 1; i >= 0; i--)
238 		buf[i] = (i >= off_byte ? buf[i - off_byte] >> off_bit : 0) |
239 		    (i > off_byte ? buf[i - off_byte - 1] << (8 - off_bit) : 0);
240 }
241 
242 /*
243  * Split double-width characters into left and right half. Single-width
244  * characters in _left_ only.
245  */
246 static int
247 split_row(uint8_t *left, uint8_t *right, uint8_t *line, size_t w)
248 {
249 	size_t s, i;
250 
251 	s = wbytes * 8 - width;
252 
253 	memcpy(left, line, wbytes);
254 	*(left + wbytes - 1) &= 0xFF << s;
255 
256 	if (w > width) { /* Double-width character. */
257 		uint8_t t;
258 
259 		for (i = 0; i < wbytes; i++) {
260 			t = *(line + wbytes + i - 1);
261 			t <<= 8 - s;
262 			t |= *(line + wbytes + i) >> s;
263 			*(right + i) = t;
264 		}
265 		*(right + wbytes - 1) &= 0xFF << s;
266 	}
267 	return (0);
268 }
269 
270 static void
271 set_height(int h)
272 {
273 	if (h <= 0 || h > VFNT_MAXDIMENSION)
274 		errx(1, "invalid height %d", h);
275 	height = h;
276 }
277 
278 static void
279 set_width(int w)
280 {
281 	if (w <= 0 || w > VFNT_MAXDIMENSION)
282 		errx(1, "invalid width %d", w);
283 	width = w;
284 	wbytes = howmany(width, 8);
285 }
286 
287 static int
288 parse_bdf(FILE *fp, unsigned int map_idx)
289 {
290 	char *line, *ln, *p;
291 	size_t length;
292 	uint8_t *bytes, *bytes_r;
293 	unsigned int curchar = 0, i, j, linenum = 0, bbwbytes;
294 	int bbw, bbh, bbox, bboy;		/* Glyph bounding box. */
295 	int fbbw = 0, fbbh, fbbox, fbboy;	/* Font bounding box. */
296 	int dwidth = 0, dwy = 0;
297 	int rv = -1;
298 	char spc = '\0';
299 
300 	/*
301 	 * Step 1: Parse FONT logical font descriptor and FONTBOUNDINGBOX
302 	 * bounding box.
303 	 */
304 	while ((ln = fgetln(fp, &length)) != NULL) {
305 		linenum++;
306 		ln[length - 1] = '\0';
307 
308 		if (strncmp(ln, "FONT ", 5) == 0) {
309 			p = ln + 5;
310 			i = 0;
311 			while ((p = strchr(p, '-')) != NULL) {
312 				p++;
313 				i++;
314 				if (i == 11) {
315 					spc = *p;
316 					break;
317 				}
318 			}
319 		} else if (strncmp(ln, "FONTBOUNDINGBOX ", 16) == 0) {
320 			if (sscanf(ln + 16, "%d %d %d %d", &fbbw, &fbbh, &fbbox,
321 			    &fbboy) != 4)
322 				errx(1, "invalid FONTBOUNDINGBOX at line %u",
323 				    linenum);
324 			set_width(fbbw);
325 			set_height(fbbh);
326 			break;
327 		}
328 	}
329 	if (fbbw == 0)
330 		errx(1, "broken font header");
331 	if (spc != 'c' && spc != 'C')
332 		errx(1, "font spacing \"C\" (character cell) required");
333 
334 	/* Step 2: Validate DWIDTH (Device Width) of all glyphs. */
335 	while ((ln = fgetln(fp, &length)) != NULL) {
336 		linenum++;
337 		ln[length - 1] = '\0';
338 
339 		if (strncmp(ln, "DWIDTH ", 7) == 0) {
340 			if (sscanf(ln + 7, "%d %d", &dwidth, &dwy) != 2)
341 				errx(1, "invalid DWIDTH at line %u", linenum);
342 			if (dwy != 0 || (dwidth != fbbw && dwidth * 2 != fbbw))
343 				errx(1, "bitmap with unsupported DWIDTH %d %d at line %u",
344 				    dwidth, dwy, linenum);
345 			if (dwidth < fbbw)
346 				set_width(dwidth);
347 		}
348 	}
349 
350 	/* Step 3: Restart at the beginning of the file and read glyph data. */
351 	dwidth = bbw = bbh = 0;
352 	rewind(fp);
353 	linenum = 0;
354 	bbwbytes = 0; /* GCC 4.2.1 "may be used uninitialized" workaround. */
355 	bytes = xmalloc(wbytes * height);
356 	bytes_r = xmalloc(wbytes * height);
357 	line = xmalloc(wbytes * 2);
358 	while ((ln = fgetln(fp, &length)) != NULL) {
359 		linenum++;
360 		ln[length - 1] = '\0';
361 
362 		if (strncmp(ln, "ENCODING ", 9) == 0) {
363 			curchar = atoi(ln + 9);
364 		} else if (strncmp(ln, "DWIDTH ", 7) == 0) {
365 			dwidth = atoi(ln + 7);
366 		} else if (strncmp(ln, "BBX ", 4) == 0) {
367 			if (sscanf(ln + 4, "%d %d %d %d", &bbw, &bbh, &bbox,
368 			     &bboy) != 4)
369 				errx(1, "invalid BBX at line %u", linenum);
370 			if (bbw < 1 || bbh < 1 || bbw > fbbw || bbh > fbbh ||
371 			    bbox < fbbox || bboy < fbboy ||
372 			    bbh + bboy > fbbh + fbboy)
373 				errx(1, "broken bitmap with BBX %d %d %d %d at line %u",
374 				    bbw, bbh, bbox, bboy, linenum);
375 			bbwbytes = howmany(bbw, 8);
376 		} else if (strncmp(ln, "BITMAP", 6) == 0 &&
377 		    (ln[6] == ' ' || ln[6] == '\0')) {
378 			if (dwidth == 0 || bbw == 0 || bbh == 0)
379 				errx(1, "broken char header at line %u!",
380 				    linenum);
381 			memset(bytes, 0, wbytes * height);
382 			memset(bytes_r, 0, wbytes * height);
383 
384 			/*
385 			 * Assume that the next _bbh_ lines are bitmap data.
386 			 * ENDCHAR is allowed to terminate the bitmap
387 			 * early but is not otherwise checked; any extra data
388 			 * is ignored.
389 			 */
390 			for (i = (fbbh + fbboy) - (bbh + bboy);
391 			    i < (unsigned int)((fbbh + fbboy) - bboy); i++) {
392 				if ((ln = fgetln(fp, &length)) == NULL)
393 					errx(1, "unexpected EOF");
394 				linenum++;
395 				ln[length - 1] = '\0';
396 				if (strcmp(ln, "ENDCHAR") == 0)
397 					break;
398 				if (strlen(ln) < bbwbytes * 2)
399 					errx(1, "broken bitmap at line %u",
400 					    linenum);
401 				memset(line, 0, wbytes * 2);
402 				for (j = 0; j < bbwbytes; j++) {
403 					unsigned int val;
404 					if (sscanf(ln + j * 2, "%2x", &val) ==
405 					    0)
406 						break;
407 					*(line + j) = (uint8_t)val;
408 				}
409 
410 				rshift_row(line, wbytes * 2, bbox - fbbox);
411 				rv = split_row(bytes + i * wbytes,
412 				     bytes_r + i * wbytes, line, dwidth);
413 				if (rv != 0)
414 					goto out;
415 			}
416 
417 			rv = add_char(curchar, map_idx, bytes,
418 			    dwidth > (int)width ? bytes_r : NULL);
419 			if (rv != 0)
420 				goto out;
421 
422 			dwidth = bbw = bbh = 0;
423 		}
424 	}
425 
426 out:
427 	free(bytes);
428 	free(bytes_r);
429 	free(line);
430 	return (rv);
431 }
432 
433 static int
434 parse_hex(FILE *fp, unsigned int map_idx)
435 {
436 	char *ln, *p;
437 	size_t length;
438 	uint8_t *bytes = NULL, *bytes_r = NULL, *line = NULL;
439 	unsigned curchar = 0, gwidth, gwbytes, i, j, chars_per_row;
440 	int rv = 0;
441 
442 	while ((ln = fgetln(fp, &length)) != NULL) {
443 		ln[length - 1] = '\0';
444 
445 		if (strncmp(ln, "# Height: ", 10) == 0) {
446 			if (bytes != NULL)
447 				errx(1, "malformed input: Height tag after font data");
448 			set_height(atoi(ln + 10));
449 		} else if (strncmp(ln, "# Width: ", 9) == 0) {
450 			if (bytes != NULL)
451 				errx(1, "malformed input: Width tag after font data");
452 			set_width(atoi(ln + 9));
453 		} else if (sscanf(ln, "%6x:", &curchar)) {
454 			if (bytes == NULL) {
455 				bytes = xmalloc(wbytes * height);
456 				bytes_r = xmalloc(wbytes * height);
457 				line = xmalloc(wbytes * 2);
458 			}
459 			/* ln is guaranteed to have a colon here. */
460 			p = strchr(ln, ':') + 1;
461 			chars_per_row = strlen(p) / height;
462 			if (chars_per_row < wbytes * 2)
463 				errx(1,
464 				    "malformed input: broken bitmap, character %06x",
465 				    curchar);
466 			gwidth = width * 2;
467 			gwbytes = howmany(gwidth, 8);
468 			if (chars_per_row < gwbytes * 2 || gwidth <= 8) {
469 				gwidth = width; /* Single-width character. */
470 				gwbytes = wbytes;
471 			}
472 
473 			for (i = 0; i < height; i++) {
474 				for (j = 0; j < gwbytes; j++) {
475 					unsigned int val;
476 					if (sscanf(p + j * 2, "%2x", &val) == 0)
477 						break;
478 					*(line + j) = (uint8_t)val;
479 				}
480 				rv = split_row(bytes + i * wbytes,
481 				    bytes_r + i * wbytes, line, gwidth);
482 				if (rv != 0)
483 					goto out;
484 				p += gwbytes * 2;
485 			}
486 
487 			rv = add_char(curchar, map_idx, bytes,
488 			    gwidth != width ? bytes_r : NULL);
489 			if (rv != 0)
490 				goto out;
491 		}
492 	}
493 out:
494 	free(bytes);
495 	free(bytes_r);
496 	free(line);
497 	return (rv);
498 }
499 
500 static int
501 parse_file(const char *filename, unsigned int map_idx)
502 {
503 	FILE *fp;
504 	size_t len;
505 	int rv;
506 
507 	fp = fopen(filename, "r");
508 	if (fp == NULL) {
509 		perror(filename);
510 		return (1);
511 	}
512 	len = strlen(filename);
513 	if (len > 4 && strcasecmp(filename + len - 4, ".hex") == 0)
514 		rv = parse_hex(fp, map_idx);
515 	else
516 		rv = parse_bdf(fp, map_idx);
517 	fclose(fp);
518 	return (rv);
519 }
520 
521 static void
522 number_glyphs(void)
523 {
524 	struct glyph *gl;
525 	unsigned int i, idx = 0;
526 
527 	for (i = 0; i < VFNT_MAPS; i++)
528 		TAILQ_FOREACH(gl, &glyphs[i], g_list)
529 			gl->g_index = idx++;
530 }
531 
532 static int
533 write_glyphs(FILE *fp)
534 {
535 	struct glyph *gl;
536 	unsigned int i;
537 
538 	for (i = 0; i < VFNT_MAPS; i++) {
539 		TAILQ_FOREACH(gl, &glyphs[i], g_list)
540 			if (fwrite(gl->g_data, wbytes * height, 1, fp) != 1)
541 				return (1);
542 	}
543 	return (0);
544 }
545 
546 static void
547 fold_mappings(unsigned int map_idx)
548 {
549 	struct mapping_list *ml = &maps[map_idx];
550 	struct mapping *mn, *mp, *mbase;
551 
552 	mp = mbase = TAILQ_FIRST(ml);
553 	for (mp = mbase = TAILQ_FIRST(ml); mp != NULL; mp = mn) {
554 		mn = TAILQ_NEXT(mp, m_list);
555 		if (mn != NULL && mn->m_char == mp->m_char + 1 &&
556 		    mn->m_glyph->g_index == mp->m_glyph->g_index + 1)
557 			continue;
558 		mbase->m_length = mp->m_char - mbase->m_char + 1;
559 		mbase = mp = mn;
560 		map_folded_count[map_idx]++;
561 	}
562 }
563 
564 struct file_mapping {
565 	uint32_t	source;
566 	uint16_t	destination;
567 	uint16_t	length;
568 } __packed;
569 
570 static int
571 write_mappings(FILE *fp, unsigned int map_idx)
572 {
573 	struct mapping_list *ml = &maps[map_idx];
574 	struct mapping *mp;
575 	struct file_mapping fm;
576 	unsigned int i = 0, j = 0;
577 
578 	TAILQ_FOREACH(mp, ml, m_list) {
579 		j++;
580 		if (mp->m_length > 0) {
581 			i += mp->m_length;
582 			fm.source = htobe32(mp->m_char);
583 			fm.destination = htobe16(mp->m_glyph->g_index);
584 			fm.length = htobe16(mp->m_length - 1);
585 			if (fwrite(&fm, sizeof fm, 1, fp) != 1)
586 				return (1);
587 		}
588 	}
589 	assert(i == j);
590 	return (0);
591 }
592 
593 struct file_header {
594 	uint8_t		magic[8];
595 	uint8_t		width;
596 	uint8_t		height;
597 	uint16_t	pad;
598 	uint32_t	glyph_count;
599 	uint32_t	map_count[4];
600 } __packed;
601 
602 static int
603 write_fnt(const char *filename)
604 {
605 	FILE *fp;
606 	struct file_header fh = {
607 		.magic = "VFNT0002",
608 	};
609 
610 	fp = fopen(filename, "wb");
611 	if (fp == NULL) {
612 		perror(filename);
613 		return (1);
614 	}
615 
616 	fh.width = width;
617 	fh.height = height;
618 	fh.glyph_count = htobe32(glyph_unique);
619 	fh.map_count[0] = htobe32(map_folded_count[0]);
620 	fh.map_count[1] = htobe32(map_folded_count[1]);
621 	fh.map_count[2] = htobe32(map_folded_count[2]);
622 	fh.map_count[3] = htobe32(map_folded_count[3]);
623 	if (fwrite(&fh, sizeof fh, 1, fp) != 1) {
624 		perror(filename);
625 		fclose(fp);
626 		return (1);
627 	}
628 
629 	if (write_glyphs(fp) != 0 ||
630 	    write_mappings(fp, VFNT_MAP_NORMAL) != 0 ||
631 	    write_mappings(fp, VFNT_MAP_NORMAL_RH) != 0 ||
632 	    write_mappings(fp, VFNT_MAP_BOLD) != 0 ||
633 	    write_mappings(fp, VFNT_MAP_BOLD_RH) != 0) {
634 		perror(filename);
635 		fclose(fp);
636 		return (1);
637 	}
638 
639 	fclose(fp);
640 	return (0);
641 }
642 
643 static void
644 print_font_info(void)
645 {
646 	printf(
647 "Statistics:\n"
648 "- width:                       %6u\n"
649 "- height:                      %6u\n"
650 "- glyph_total:                 %6u\n"
651 "- glyph_normal:                %6u\n"
652 "- glyph_normal_right:          %6u\n"
653 "- glyph_bold:                  %6u\n"
654 "- glyph_bold_right:            %6u\n"
655 "- glyph_unique:                %6u\n"
656 "- glyph_dupe:                  %6u\n"
657 "- mapping_total:               %6u\n"
658 "- mapping_normal:              %6u\n"
659 "- mapping_normal_folded:       %6u\n"
660 "- mapping_normal_right:        %6u\n"
661 "- mapping_normal_right_folded: %6u\n"
662 "- mapping_bold:                %6u\n"
663 "- mapping_bold_folded:         %6u\n"
664 "- mapping_bold_right:          %6u\n"
665 "- mapping_bold_right_folded:   %6u\n"
666 "- mapping_unique:              %6u\n"
667 "- mapping_dupe:                %6u\n",
668 	    width, height,
669 	    glyph_total,
670 	    glyph_count[0],
671 	    glyph_count[1],
672 	    glyph_count[2],
673 	    glyph_count[3],
674 	    glyph_unique, glyph_dupe,
675 	    mapping_total,
676 	    map_count[0], map_folded_count[0],
677 	    map_count[1], map_folded_count[1],
678 	    map_count[2], map_folded_count[2],
679 	    map_count[3], map_folded_count[3],
680 	    mapping_unique, mapping_dupe);
681 }
682 
683 int
684 main(int argc, char *argv[])
685 {
686 	int ch, verbose = 0;
687 
688 	assert(sizeof(struct file_header) == 32);
689 	assert(sizeof(struct file_mapping) == 8);
690 
691 	while ((ch = getopt(argc, argv, "h:vw:")) != -1) {
692 		switch (ch) {
693 		case 'h':
694 			height = atoi(optarg);
695 			break;
696 		case 'v':
697 			verbose = 1;
698 			break;
699 		case 'w':
700 			width = atoi(optarg);
701 			break;
702 		case '?':
703 		default:
704 			usage();
705 		}
706 	}
707 	argc -= optind;
708 	argv += optind;
709 
710 	if (argc < 2 || argc > 3)
711 		usage();
712 
713 	set_width(width);
714 	set_height(height);
715 
716 	if (parse_file(argv[0], VFNT_MAP_NORMAL) != 0)
717 		return (1);
718 	argc--;
719 	argv++;
720 	if (argc == 2) {
721 		if (parse_file(argv[0], VFNT_MAP_BOLD) != 0)
722 			return (1);
723 		argc--;
724 		argv++;
725 	}
726 	number_glyphs();
727 	dedup_mapping(VFNT_MAP_BOLD);
728 	dedup_mapping(VFNT_MAP_BOLD_RH);
729 	fold_mappings(0);
730 	fold_mappings(1);
731 	fold_mappings(2);
732 	fold_mappings(3);
733 	if (write_fnt(argv[0]) != 0)
734 		return (1);
735 
736 	if (verbose)
737 		print_font_info();
738 
739 	return (0);
740 }
741