]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/vtfontcvt/vtfontcvt.c
vtfontcvt: initialize bbwbytes to avoid GCC 4.2.1 uninitialized warning
[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;
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                     sscanf(ln + 4, "%d %d %d %d", &bbw, &bbh, &bbox,
384                      &bboy) == 4) {
385                         if (bbw < 1 || bbh < 1 || bbw > fbbw || bbh > fbbh ||
386                             bbox < fbbox || bboy < fbboy)
387                                 errx(1, "broken bitmap with BBX %d %d %d %d at line %u",
388                                     bbw, bbh, bbox, bboy, linenum);
389                         bbwbytes = howmany(bbw, 8);
390                 } else if (strncmp(ln, "BITMAP", 6) == 0 &&
391                     (ln[6] == ' ' || ln[6] == '\0')) {
392                         if (dwidth == 0 || bbw == 0 || bbh == 0)
393                                 errx(1, "broken char header at line %u!",
394                                     linenum);
395                         memset(bytes, 0, wbytes * height);
396                         memset(bytes_r, 0, wbytes * height);
397
398                         /*
399                          * Assume that the next _bbh_ lines are bitmap data.
400                          * ENDCHAR is allowed to terminate the bitmap
401                          * early but is not otherwise checked; any extra data
402                          * is ignored.
403                          */
404                         for (i = (fbbh + fbboy) - (bbh + bboy);
405                             i < (unsigned int)((fbbh + fbboy) - bboy); i++) {
406                                 if ((ln = fgetln(fp, &length)) == NULL)
407                                         errx(1, "unexpected EOF");
408                                 linenum++;
409                                 ln[length - 1] = '\0';
410                                 if (strcmp(ln, "ENDCHAR") == 0)
411                                         break;
412                                 if (strlen(ln) < bbwbytes * 2)
413                                         errx(1, "broken bitmap at line %u",
414                                             linenum);
415                                 memset(line, 0, wbytes * 2);
416                                 for (j = 0; j < bbwbytes; j++) {
417                                         unsigned int val;
418                                         if (sscanf(ln + j * 2, "%2x", &val) ==
419                                             0)
420                                                 break;
421                                         *(line + j) = (uint8_t)val;
422                                 }
423
424                                 rv = rshift_row(line, wbytes * 2, bbw,
425                                     bbox - fbbox);
426                                 if (rv != 0)
427                                         goto out;
428
429                                 rv = split_row(bytes + i * wbytes,
430                                      bytes_r + i * wbytes, line, dwidth);
431                                 if (rv != 0)
432                                         goto out;
433                         }
434
435                         rv = add_char(curchar, map_idx, bytes,
436                             dwidth > (int)width ? bytes_r : NULL);
437                         if (rv != 0)
438                                 goto out;
439
440                         dwidth = bbw = bbh = 0;
441                 }
442         }
443
444 out:
445         free(bytes);
446         free(bytes_r);
447         free(line);
448         return (rv);
449 }
450
451 static int
452 parse_hex(FILE *fp, unsigned int map_idx)
453 {
454         char *ln, *p;
455         size_t length;
456         uint8_t *bytes = NULL, *bytes_r = NULL, *line = NULL;
457         unsigned curchar = 0, gwidth, gwbytes, i, j, chars_per_row;
458         int rv = 0;
459
460         while ((ln = fgetln(fp, &length)) != NULL) {
461                 ln[length - 1] = '\0';
462
463                 if (strncmp(ln, "# Height: ", 10) == 0) {
464                         if (bytes != NULL)
465                                 errx(1, "malformed input: Height tag after font data");
466                         set_height(atoi(ln + 10));
467                 } else if (strncmp(ln, "# Width: ", 9) == 0) {
468                         if (bytes != NULL)
469                                 errx(1, "malformed input: Width tag after font data");
470                         set_width(atoi(ln + 9));
471                 } else if (sscanf(ln, "%6x:", &curchar)) {
472                         if (bytes == NULL) {
473                                 bytes = xmalloc(wbytes * height);
474                                 bytes_r = xmalloc(wbytes * height);
475                                 line = xmalloc(wbytes * 2);
476                         }
477                         /* ln is guaranteed to have a colon here. */
478                         p = strchr(ln, ':') + 1;
479                         chars_per_row = strlen(p) / height;
480                         if (chars_per_row < wbytes * 2)
481                                 errx(1,
482                                     "malformed input: broken bitmap, character %06x",
483                                     curchar);
484                         gwidth = width * 2;
485                         gwbytes = howmany(width, 8);
486                         if (chars_per_row < gwbytes * 2 || gwidth <= 8) {
487                                 gwidth = width; /* Single-width character. */
488                                 gwbytes = wbytes;
489                         }
490
491                         for (i = 0; i < height; i++) {
492                                 for (j = 0; j < gwbytes; j++) {
493                                         unsigned int val;
494                                         if (sscanf(p + j * 2, "%2x", &val) == 0)
495                                                 break;
496                                         *(line + j) = (uint8_t)val;
497                                 }
498                                 rv = split_row(bytes + i * wbytes,
499                                     bytes_r + i * wbytes, line, gwidth);
500                                 if (rv != 0)
501                                         goto out;
502                                 p += gwbytes * 2;
503                         }
504
505                         rv = add_char(curchar, map_idx, bytes,
506                             gwidth != width ? bytes_r : NULL);
507                         if (rv != 0)
508                                 goto out;
509                 }
510         }
511 out:
512         free(bytes);
513         free(bytes_r);
514         free(line);
515         return (rv);
516 }
517
518 static int
519 parse_file(const char *filename, unsigned int map_idx)
520 {
521         FILE *fp;
522         size_t len;
523         int rv;
524
525         fp = fopen(filename, "r");
526         if (fp == NULL) {
527                 perror(filename);
528                 return (1);
529         }
530         len = strlen(filename);
531         if (len > 4 && strcasecmp(filename + len - 4, ".hex") == 0)
532                 rv = parse_hex(fp, map_idx);
533         else
534                 rv = parse_bdf(fp, map_idx);
535         fclose(fp);
536         return (rv);
537 }
538
539 static void
540 number_glyphs(void)
541 {
542         struct glyph *gl;
543         unsigned int i, idx = 0;
544
545         for (i = 0; i < VFNT_MAPS; i++)
546                 TAILQ_FOREACH(gl, &glyphs[i], g_list)
547                         gl->g_index = idx++;
548 }
549
550 static int
551 write_glyphs(FILE *fp)
552 {
553         struct glyph *gl;
554         unsigned int i;
555
556         for (i = 0; i < VFNT_MAPS; i++) {
557                 TAILQ_FOREACH(gl, &glyphs[i], g_list)
558                         if (fwrite(gl->g_data, wbytes * height, 1, fp) != 1)
559                                 return (1);
560         }
561         return (0);
562 }
563
564 static void
565 fold_mappings(unsigned int map_idx)
566 {
567         struct mapping_list *ml = &maps[map_idx];
568         struct mapping *mn, *mp, *mbase;
569
570         mp = mbase = TAILQ_FIRST(ml);
571         for (mp = mbase = TAILQ_FIRST(ml); mp != NULL; mp = mn) {
572                 mn = TAILQ_NEXT(mp, m_list);
573                 if (mn != NULL && mn->m_char == mp->m_char + 1 &&
574                     mn->m_glyph->g_index == mp->m_glyph->g_index + 1)
575                         continue;
576                 mbase->m_length = mp->m_char - mbase->m_char + 1;
577                 mbase = mp = mn;
578                 map_folded_count[map_idx]++;
579         }
580 }
581
582 struct file_mapping {
583         uint32_t        source;
584         uint16_t        destination;
585         uint16_t        length;
586 } __packed;
587
588 static int
589 write_mappings(FILE *fp, unsigned int map_idx)
590 {
591         struct mapping_list *ml = &maps[map_idx];
592         struct mapping *mp;
593         struct file_mapping fm;
594         unsigned int i = 0, j = 0;
595
596         TAILQ_FOREACH(mp, ml, m_list) {
597                 j++;
598                 if (mp->m_length > 0) {
599                         i += mp->m_length;
600                         fm.source = htobe32(mp->m_char);
601                         fm.destination = htobe16(mp->m_glyph->g_index);
602                         fm.length = htobe16(mp->m_length - 1);
603                         if (fwrite(&fm, sizeof fm, 1, fp) != 1)
604                                 return (1);
605                 }
606         }
607         assert(i == j);
608         return (0);
609 }
610
611 struct file_header {
612         uint8_t         magic[8];
613         uint8_t         width;
614         uint8_t         height;
615         uint16_t        pad;
616         uint32_t        glyph_count;
617         uint32_t        map_count[4];
618 } __packed;
619
620 static int
621 write_fnt(const char *filename)
622 {
623         FILE *fp;
624         struct file_header fh = {
625                 .magic = "VFNT0002",
626         };
627
628         fp = fopen(filename, "wb");
629         if (fp == NULL) {
630                 perror(filename);
631                 return (1);
632         }
633
634         fh.width = width;
635         fh.height = height;
636         fh.glyph_count = htobe32(glyph_unique);
637         fh.map_count[0] = htobe32(map_folded_count[0]);
638         fh.map_count[1] = htobe32(map_folded_count[1]);
639         fh.map_count[2] = htobe32(map_folded_count[2]);
640         fh.map_count[3] = htobe32(map_folded_count[3]);
641         if (fwrite(&fh, sizeof fh, 1, fp) != 1) {
642                 perror(filename);
643                 fclose(fp);
644                 return (1);
645         }
646
647         if (write_glyphs(fp) != 0 ||
648             write_mappings(fp, VFNT_MAP_NORMAL) != 0 ||
649             write_mappings(fp, VFNT_MAP_NORMAL_RH) != 0 ||
650             write_mappings(fp, VFNT_MAP_BOLD) != 0 ||
651             write_mappings(fp, VFNT_MAP_BOLD_RH) != 0) {
652                 perror(filename);
653                 fclose(fp);
654                 return (1);
655         }
656
657         fclose(fp);
658         return (0);
659 }
660
661 static void
662 print_font_info(void)
663 {
664         printf(
665 "Statistics:\n"
666 "- width:                       %6u\n"
667 "- height:                      %6u\n"
668 "- glyph_total:                 %6u\n"
669 "- glyph_normal:                %6u\n"
670 "- glyph_normal_right:          %6u\n"
671 "- glyph_bold:                  %6u\n"
672 "- glyph_bold_right:            %6u\n"
673 "- glyph_unique:                %6u\n"
674 "- glyph_dupe:                  %6u\n"
675 "- mapping_total:               %6u\n"
676 "- mapping_normal:              %6u\n"
677 "- mapping_normal_folded:       %6u\n"
678 "- mapping_normal_right:        %6u\n"
679 "- mapping_normal_right_folded: %6u\n"
680 "- mapping_bold:                %6u\n"
681 "- mapping_bold_folded:         %6u\n"
682 "- mapping_bold_right:          %6u\n"
683 "- mapping_bold_right_folded:   %6u\n"
684 "- mapping_unique:              %6u\n"
685 "- mapping_dupe:                %6u\n",
686             width, height,
687             glyph_total,
688             glyph_count[0],
689             glyph_count[1],
690             glyph_count[2],
691             glyph_count[3],
692             glyph_unique, glyph_dupe,
693             mapping_total,
694             map_count[0], map_folded_count[0],
695             map_count[1], map_folded_count[1],
696             map_count[2], map_folded_count[2],
697             map_count[3], map_folded_count[3],
698             mapping_unique, mapping_dupe);
699 }
700
701 int
702 main(int argc, char *argv[])
703 {
704         int ch, verbose = 0;
705
706         assert(sizeof(struct file_header) == 32);
707         assert(sizeof(struct file_mapping) == 8);
708
709         while ((ch = getopt(argc, argv, "h:vw:")) != -1) {
710                 switch (ch) {
711                 case 'h':
712                         height = atoi(optarg);
713                         break;
714                 case 'v':
715                         verbose = 1;
716                         break;
717                 case 'w':
718                         width = atoi(optarg);
719                         break;
720                 case '?':
721                 default:
722                         usage();
723                 }
724         }
725         argc -= optind;
726         argv += optind;
727
728         if (argc < 2 || argc > 3)
729                 usage();
730
731         set_width(width);
732         set_height(height);
733
734         if (parse_file(argv[0], VFNT_MAP_NORMAL) != 0)
735                 return (1);
736         argc--;
737         argv++;
738         if (argc == 2) {
739                 if (parse_file(argv[0], VFNT_MAP_BOLD) != 0)
740                         return (1);
741                 argc--;
742                 argv++;
743         }
744         number_glyphs();
745         dedup_mapping(VFNT_MAP_BOLD);
746         dedup_mapping(VFNT_MAP_BOLD_RH);
747         fold_mappings(0);
748         fold_mappings(1);
749         fold_mappings(2);
750         fold_mappings(3);
751         if (write_fnt(argv[0]) != 0)
752                 return (1);
753
754         if (verbose)
755                 print_font_info();
756
757         return (0);
758 }