]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/vtfontcvt/vtfontcvt.c
Add ACPI support for USB driver.
[FreeBSD/FreeBSD.git] / usr.bin / vtfontcvt / vtfontcvt.c
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                     sscanf(ln + 16, "%d %d %d %d", &fbbw, &fbbh, &fbbox,
340                     &fbboy) == 4) {
341                         set_width(fbbw);
342                         set_height(fbbh);
343                         break;
344                 }
345         }
346         if (fbbw == 0)
347                 errx(1, "broken font header");
348         if (spc != 'c' && spc != 'C')
349                 errx(1, "font spacing \"C\" (character cell) required");
350
351         /* Step 2: Validate DWIDTH (Device Width) of all glyphs. */
352         while ((ln = fgetln(fp, &length)) != NULL) {
353                 linenum++;
354                 ln[length - 1] = '\0';
355
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);
361                         if (dwidth < fbbw)
362                                 set_width(dwidth);
363                 }
364         }
365
366         /* Step 3: Restart at the beginning of the file and read glyph data. */
367         dwidth = bbw = bbh = 0;
368         rewind(fp);
369         linenum = 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) {
375                 linenum++;
376                 ln[length - 1] = '\0';
377
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,
384                              &bboy) != 4)
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!",
396                                     linenum);
397                         memset(bytes, 0, wbytes * height);
398                         memset(bytes_r, 0, wbytes * height);
399
400                         /*
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
404                          * is ignored.
405                          */
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");
410                                 linenum++;
411                                 ln[length - 1] = '\0';
412                                 if (strcmp(ln, "ENDCHAR") == 0)
413                                         break;
414                                 if (strlen(ln) < bbwbytes * 2)
415                                         errx(1, "broken bitmap at line %u",
416                                             linenum);
417                                 memset(line, 0, wbytes * 2);
418                                 for (j = 0; j < bbwbytes; j++) {
419                                         unsigned int val;
420                                         if (sscanf(ln + j * 2, "%2x", &val) ==
421                                             0)
422                                                 break;
423                                         *(line + j) = (uint8_t)val;
424                                 }
425
426                                 rv = rshift_row(line, wbytes * 2, bbw,
427                                     bbox - fbbox);
428                                 if (rv != 0)
429                                         goto out;
430
431                                 rv = split_row(bytes + i * wbytes,
432                                      bytes_r + i * wbytes, line, dwidth);
433                                 if (rv != 0)
434                                         goto out;
435                         }
436
437                         rv = add_char(curchar, map_idx, bytes,
438                             dwidth > (int)width ? bytes_r : NULL);
439                         if (rv != 0)
440                                 goto out;
441
442                         dwidth = bbw = bbh = 0;
443                 }
444         }
445
446 out:
447         free(bytes);
448         free(bytes_r);
449         free(line);
450         return (rv);
451 }
452
453 static int
454 parse_hex(FILE *fp, unsigned int map_idx)
455 {
456         char *ln, *p;
457         size_t length;
458         uint8_t *bytes = NULL, *bytes_r = NULL, *line = NULL;
459         unsigned curchar = 0, gwidth, gwbytes, i, j, chars_per_row;
460         int rv = 0;
461
462         while ((ln = fgetln(fp, &length)) != NULL) {
463                 ln[length - 1] = '\0';
464
465                 if (strncmp(ln, "# Height: ", 10) == 0) {
466                         if (bytes != NULL)
467                                 errx(1, "malformed input: Height tag after font data");
468                         set_height(atoi(ln + 10));
469                 } else if (strncmp(ln, "# Width: ", 9) == 0) {
470                         if (bytes != NULL)
471                                 errx(1, "malformed input: Width tag after font data");
472                         set_width(atoi(ln + 9));
473                 } else if (sscanf(ln, "%6x:", &curchar)) {
474                         if (bytes == NULL) {
475                                 bytes = xmalloc(wbytes * height);
476                                 bytes_r = xmalloc(wbytes * height);
477                                 line = xmalloc(wbytes * 2);
478                         }
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)
483                                 errx(1,
484                                     "malformed input: broken bitmap, character %06x",
485                                     curchar);
486                         gwidth = width * 2;
487                         gwbytes = howmany(gwidth, 8);
488                         if (chars_per_row < gwbytes * 2 || gwidth <= 8) {
489                                 gwidth = width; /* Single-width character. */
490                                 gwbytes = wbytes;
491                         }
492
493                         for (i = 0; i < height; i++) {
494                                 for (j = 0; j < gwbytes; j++) {
495                                         unsigned int val;
496                                         if (sscanf(p + j * 2, "%2x", &val) == 0)
497                                                 break;
498                                         *(line + j) = (uint8_t)val;
499                                 }
500                                 rv = split_row(bytes + i * wbytes,
501                                     bytes_r + i * wbytes, line, gwidth);
502                                 if (rv != 0)
503                                         goto out;
504                                 p += gwbytes * 2;
505                         }
506
507                         rv = add_char(curchar, map_idx, bytes,
508                             gwidth != width ? bytes_r : NULL);
509                         if (rv != 0)
510                                 goto out;
511                 }
512         }
513 out:
514         free(bytes);
515         free(bytes_r);
516         free(line);
517         return (rv);
518 }
519
520 static int
521 parse_file(const char *filename, unsigned int map_idx)
522 {
523         FILE *fp;
524         size_t len;
525         int rv;
526
527         fp = fopen(filename, "r");
528         if (fp == NULL) {
529                 perror(filename);
530                 return (1);
531         }
532         len = strlen(filename);
533         if (len > 4 && strcasecmp(filename + len - 4, ".hex") == 0)
534                 rv = parse_hex(fp, map_idx);
535         else
536                 rv = parse_bdf(fp, map_idx);
537         fclose(fp);
538         return (rv);
539 }
540
541 static void
542 number_glyphs(void)
543 {
544         struct glyph *gl;
545         unsigned int i, idx = 0;
546
547         for (i = 0; i < VFNT_MAPS; i++)
548                 TAILQ_FOREACH(gl, &glyphs[i], g_list)
549                         gl->g_index = idx++;
550 }
551
552 static int
553 write_glyphs(FILE *fp)
554 {
555         struct glyph *gl;
556         unsigned int i;
557
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)
561                                 return (1);
562         }
563         return (0);
564 }
565
566 static void
567 fold_mappings(unsigned int map_idx)
568 {
569         struct mapping_list *ml = &maps[map_idx];
570         struct mapping *mn, *mp, *mbase;
571
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)
577                         continue;
578                 mbase->m_length = mp->m_char - mbase->m_char + 1;
579                 mbase = mp = mn;
580                 map_folded_count[map_idx]++;
581         }
582 }
583
584 struct file_mapping {
585         uint32_t        source;
586         uint16_t        destination;
587         uint16_t        length;
588 } __packed;
589
590 static int
591 write_mappings(FILE *fp, unsigned int map_idx)
592 {
593         struct mapping_list *ml = &maps[map_idx];
594         struct mapping *mp;
595         struct file_mapping fm;
596         unsigned int i = 0, j = 0;
597
598         TAILQ_FOREACH(mp, ml, m_list) {
599                 j++;
600                 if (mp->m_length > 0) {
601                         i += mp->m_length;
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)
606                                 return (1);
607                 }
608         }
609         assert(i == j);
610         return (0);
611 }
612
613 struct file_header {
614         uint8_t         magic[8];
615         uint8_t         width;
616         uint8_t         height;
617         uint16_t        pad;
618         uint32_t        glyph_count;
619         uint32_t        map_count[4];
620 } __packed;
621
622 static int
623 write_fnt(const char *filename)
624 {
625         FILE *fp;
626         struct file_header fh = {
627                 .magic = "VFNT0002",
628         };
629
630         fp = fopen(filename, "wb");
631         if (fp == NULL) {
632                 perror(filename);
633                 return (1);
634         }
635
636         fh.width = width;
637         fh.height = height;
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) {
644                 perror(filename);
645                 fclose(fp);
646                 return (1);
647         }
648
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) {
654                 perror(filename);
655                 fclose(fp);
656                 return (1);
657         }
658
659         fclose(fp);
660         return (0);
661 }
662
663 static void
664 print_font_info(void)
665 {
666         printf(
667 "Statistics:\n"
668 "- width:                       %6u\n"
669 "- height:                      %6u\n"
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",
688             width, height,
689             glyph_total,
690             glyph_count[0],
691             glyph_count[1],
692             glyph_count[2],
693             glyph_count[3],
694             glyph_unique, glyph_dupe,
695             mapping_total,
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);
701 }
702
703 int
704 main(int argc, char *argv[])
705 {
706         int ch, verbose = 0;
707
708         assert(sizeof(struct file_header) == 32);
709         assert(sizeof(struct file_mapping) == 8);
710
711         while ((ch = getopt(argc, argv, "h:vw:")) != -1) {
712                 switch (ch) {
713                 case 'h':
714                         height = atoi(optarg);
715                         break;
716                 case 'v':
717                         verbose = 1;
718                         break;
719                 case 'w':
720                         width = atoi(optarg);
721                         break;
722                 case '?':
723                 default:
724                         usage();
725                 }
726         }
727         argc -= optind;
728         argv += optind;
729
730         if (argc < 2 || argc > 3)
731                 usage();
732
733         set_width(width);
734         set_height(height);
735
736         if (parse_file(argv[0], VFNT_MAP_NORMAL) != 0)
737                 return (1);
738         argc--;
739         argv++;
740         if (argc == 2) {
741                 if (parse_file(argv[0], VFNT_MAP_BOLD) != 0)
742                         return (1);
743                 argc--;
744                 argv++;
745         }
746         number_glyphs();
747         dedup_mapping(VFNT_MAP_BOLD);
748         dedup_mapping(VFNT_MAP_BOLD_RH);
749         fold_mappings(0);
750         fold_mappings(1);
751         fold_mappings(2);
752         fold_mappings(3);
753         if (write_fnt(argv[0]) != 0)
754                 return (1);
755
756         if (verbose)
757                 print_font_info();
758
759         return (0);
760 }