]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/vtfontcvt/vtfontcvt.c
MFV r267565:
[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
53 static unsigned int width = 8, wbytes, height = 16;
54
55 struct glyph {
56         TAILQ_ENTRY(glyph)       g_list;
57         SLIST_ENTRY(glyph)       g_hash;
58         uint8_t                 *g_data;
59         unsigned int             g_index;
60 };
61
62 #define FONTCVT_NHASH 4096
63 TAILQ_HEAD(glyph_list, glyph);
64 static SLIST_HEAD(, glyph) glyph_hash[FONTCVT_NHASH];
65 static struct glyph_list glyphs[VFNT_MAPS] = {
66     TAILQ_HEAD_INITIALIZER(glyphs[0]),
67     TAILQ_HEAD_INITIALIZER(glyphs[1]),
68     TAILQ_HEAD_INITIALIZER(glyphs[2]),
69     TAILQ_HEAD_INITIALIZER(glyphs[3]),
70 };
71 static unsigned int glyph_total, glyph_count[4], glyph_unique, glyph_dupe;
72
73 struct mapping {
74         TAILQ_ENTRY(mapping)     m_list;
75         unsigned int             m_char;
76         unsigned int             m_length;
77         struct glyph            *m_glyph;
78 };
79
80 TAILQ_HEAD(mapping_list, mapping);
81 static struct mapping_list maps[VFNT_MAPS] = {
82     TAILQ_HEAD_INITIALIZER(maps[0]),
83     TAILQ_HEAD_INITIALIZER(maps[1]),
84     TAILQ_HEAD_INITIALIZER(maps[2]),
85     TAILQ_HEAD_INITIALIZER(maps[3]),
86 };
87 static unsigned int mapping_total, map_count[4], map_folded_count[4],
88     mapping_unique, mapping_dupe;
89
90 static void
91 usage(void)
92 {
93
94         errx(1,
95 "usage: vtfontcvt [-w width] [-h height] [-v] normal.bdf [bold.bdf] out.fnt\n");
96         exit(1);
97 }
98
99 static int
100 add_mapping(struct glyph *gl, unsigned int c, unsigned int map_idx)
101 {
102         struct mapping *mp;
103         struct mapping_list *ml;
104
105         mapping_total++;
106
107         mp = malloc(sizeof *mp);
108         mp->m_char = c;
109         mp->m_glyph = gl;
110         mp->m_length = 0;
111
112         ml = &maps[map_idx];
113         if (TAILQ_LAST(ml, mapping_list) != NULL &&
114             TAILQ_LAST(ml, mapping_list)->m_char >= c) {
115                 errx(1, "Bad ordering at character %u\n", c);
116                 return (1);
117         }
118         TAILQ_INSERT_TAIL(ml, mp, m_list);
119
120         map_count[map_idx]++;
121         mapping_unique++;
122
123         return (0);
124 }
125
126 static int
127 dedup_mapping(unsigned int map_idx)
128 {
129         struct mapping *mp_bold, *mp_normal, *mp_temp;
130         unsigned normal_map_idx = map_idx - VFNT_MAP_BOLD;
131
132         assert(map_idx == VFNT_MAP_BOLD || map_idx == VFNT_MAP_BOLD_RH);
133         mp_normal = TAILQ_FIRST(&maps[normal_map_idx]);
134         TAILQ_FOREACH_SAFE(mp_bold, &maps[map_idx], m_list, mp_temp) {
135                 while (mp_normal->m_char < mp_bold->m_char)
136                         mp_normal = TAILQ_NEXT(mp_normal, m_list);
137                 if (mp_bold->m_char != mp_normal->m_char) {
138                         errx(1, "Character %u not in normal font!\n",
139                             mp_bold->m_char);
140                         return (1);
141                 }
142                 if (mp_bold->m_glyph != mp_normal->m_glyph)
143                         continue;
144
145                 /* No mapping is needed if it's equal to the normal mapping. */
146                 TAILQ_REMOVE(&maps[map_idx], mp_bold, m_list);
147                 free(mp_bold);
148                 mapping_dupe++;
149         }
150         return (0);
151 }
152
153 static struct glyph *
154 add_glyph(const uint8_t *bytes, unsigned int map_idx, int fallback)
155 {
156         struct glyph *gl;
157         int hash;
158
159         glyph_total++;
160         glyph_count[map_idx]++;
161
162         hash = fnv_32_buf(bytes, wbytes * height, FNV1_32_INIT) % FONTCVT_NHASH;
163         SLIST_FOREACH(gl, &glyph_hash[hash], g_hash) {
164                 if (memcmp(gl->g_data, bytes, wbytes * height) == 0) {
165                         glyph_dupe++;
166                         return (gl);
167                 }
168         }
169
170         gl = malloc(sizeof *gl);
171         gl->g_data = malloc(wbytes * height);
172         memcpy(gl->g_data, bytes, wbytes * height);
173         if (fallback)
174                 TAILQ_INSERT_HEAD(&glyphs[map_idx], gl, g_list);
175         else
176                 TAILQ_INSERT_TAIL(&glyphs[map_idx], gl, g_list);
177         SLIST_INSERT_HEAD(&glyph_hash[hash], gl, g_hash);
178
179         glyph_unique++;
180         return (gl);
181 }
182
183 static int
184 add_char(unsigned curchar, unsigned map_idx, uint8_t *bytes, uint8_t *bytes_r)
185 {
186         struct glyph *gl;
187
188         /* Prevent adding two glyphs for 0xFFFD */
189         if (curchar == 0xFFFD) {
190                 if (map_idx < VFNT_MAP_BOLD)
191                         gl = add_glyph(bytes, 0, 1);
192         } else if (curchar >= 0x20) {
193                 gl = add_glyph(bytes, map_idx, 0);
194                 if (add_mapping(gl, curchar, map_idx) != 0)
195                         return (1);
196                 if (bytes_r != NULL) {
197                         gl = add_glyph(bytes_r, map_idx + 1, 0);
198                         if (add_mapping(gl, curchar,
199                             map_idx + 1) != 0)
200                                 return (1);
201                 }
202         }
203         return (0);
204 }
205
206
207 static int
208 parse_bitmap_line(uint8_t *left, uint8_t *right, unsigned int line,
209     unsigned int dwidth)
210 {
211         uint8_t *p;
212         unsigned int i, subline;
213
214         if (dwidth != width && dwidth != width * 2) {
215                 errx(1,
216                     "Bitmap with unsupported width %u!\n", dwidth);
217                 return (1);
218         }
219
220         /* Move pixel data right to simplify splitting double characters. */
221         line >>= (howmany(dwidth, 8) * 8) - dwidth;
222
223         for (i = dwidth / width; i > 0; i--) {
224                 p = (i == 2) ? right : left;
225
226                 subline = line & ((1 << width) - 1);
227                 subline <<= (howmany(width, 8) * 8) - width;
228
229                 if (wbytes == 1) {
230                         *p = subline;
231                 } else if (wbytes == 2) {
232                         *p++ = subline >> 8;
233                         *p = subline;
234                 } else {
235                         errx(1,
236                             "Unsupported wbytes %u!\n", wbytes);
237                         return (1);
238                 }
239
240                 line >>= width;
241         }
242
243         return (0);
244 }
245
246 static int
247 parse_bdf(FILE *fp, unsigned int map_idx)
248 {
249         char *ln;
250         size_t length;
251         uint8_t bytes[wbytes * height], bytes_r[wbytes * height];
252         unsigned int curchar = 0, dwidth = 0, i, line;
253
254         while ((ln = fgetln(fp, &length)) != NULL) {
255                 ln[length - 1] = '\0';
256
257                 if (strncmp(ln, "ENCODING ", 9) == 0) {
258                         curchar = atoi(ln + 9);
259                 }
260
261                 if (strncmp(ln, "DWIDTH ", 7) == 0) {
262                         dwidth = atoi(ln + 7);
263                 }
264
265                 if (strncmp(ln, "BITMAP", 6) == 0 &&
266                     (ln[6] == ' ' || ln[6] == '\0')) {
267                         for (i = 0; i < height; i++) {
268                                 if ((ln = fgetln(fp, &length)) == NULL) {
269                                         errx(1, "Unexpected EOF!\n");
270                                         return (1);
271                                 }
272                                 ln[length - 1] = '\0';
273                                 sscanf(ln, "%x", &line);
274                                 if (parse_bitmap_line(bytes + i * wbytes,
275                                      bytes_r + i * wbytes, line, dwidth) != 0)
276                                         return (1);
277                         }
278
279                         if (add_char(curchar, map_idx, bytes,
280                             dwidth == width * 2 ? bytes_r : NULL) != 0)
281                                 return (1);
282                 }
283         }
284
285         return (0);
286 }
287
288 static int
289 parse_hex(FILE *fp, unsigned int map_idx)
290 {
291         char *ln, *p;
292         char fmt_str[8];
293         size_t length;
294         uint8_t bytes[wbytes * height], bytes_r[wbytes * height];
295         unsigned curchar = 0, i, line, chars_per_row, dwidth;
296
297         while ((ln = fgetln(fp, &length)) != NULL) {
298                 ln[length - 1] = '\0';
299
300                 if (strncmp(ln, "# Height: ", 10) == 0) {
301                         height = atoi(ln + 10);
302                 } else if (strncmp(ln, "# Width: ", 9) == 0) {
303                         width = atoi(ln + 9);
304                 } else if (sscanf(ln, "%4x:", &curchar)) {
305                         p = ln + 5;
306                         chars_per_row = strlen(p) / height;
307                         dwidth = width;
308                         if (chars_per_row / 2 > width / 8)
309                                 dwidth *= 2; /* Double-width character. */
310                         snprintf(fmt_str, sizeof(fmt_str), "%%%ux",
311                             chars_per_row);
312
313                         for (i = 0; i < height; i++) {
314                                 sscanf(p, fmt_str, &line);
315                                 p += chars_per_row;
316                                 if (parse_bitmap_line(bytes + i * wbytes,
317                                     bytes_r + i * wbytes, line, dwidth) != 0)
318                                         return (1);
319                         }
320
321                         if (add_char(curchar, map_idx, bytes,
322                             dwidth == width * 2 ? bytes_r : NULL) != 0)
323                                 return (1);
324                 }
325         }
326         return (0);
327 }
328
329 static int
330 parse_file(const char *filename, unsigned int map_idx)
331 {
332         FILE *fp;
333         size_t len;
334         int rv;
335
336         fp = fopen(filename, "r");
337         if (fp == NULL) {
338                 perror(filename);
339                 return (1);
340         }
341         len = strlen(filename);
342         if (len > 4 && strcasecmp(filename + len - 4, ".hex") == 0)
343                 rv = parse_hex(fp, map_idx);
344         else
345                 rv = parse_bdf(fp, map_idx);
346         fclose(fp);
347         return (rv);
348 }
349
350 static void
351 number_glyphs(void)
352 {
353         struct glyph *gl;
354         unsigned int i, idx = 0;
355
356         for (i = 0; i < VFNT_MAPS; i++)
357                 TAILQ_FOREACH(gl, &glyphs[i], g_list)
358                         gl->g_index = idx++;
359 }
360
361 static int
362 write_glyphs(FILE *fp)
363 {
364         struct glyph *gl;
365         unsigned int i;
366
367         for (i = 0; i < VFNT_MAPS; i++) {
368                 TAILQ_FOREACH(gl, &glyphs[i], g_list)
369                         if (fwrite(gl->g_data, wbytes * height, 1, fp) != 1)
370                                 return (1);
371         }
372         return (0);
373 }
374
375 static void
376 fold_mappings(unsigned int map_idx)
377 {
378         struct mapping_list *ml = &maps[map_idx];
379         struct mapping *mn, *mp, *mbase;
380
381         mp = mbase = TAILQ_FIRST(ml);
382         for (mp = mbase = TAILQ_FIRST(ml); mp != NULL; mp = mn) {
383                 mn = TAILQ_NEXT(mp, m_list);
384                 if (mn != NULL && mn->m_char == mp->m_char + 1 &&
385                     mn->m_glyph->g_index == mp->m_glyph->g_index + 1)
386                         continue;
387                 mbase->m_length = mp->m_char - mbase->m_char + 1;
388                 mbase = mp = mn;
389                 map_folded_count[map_idx]++;
390         }
391 }
392
393 struct file_mapping {
394         uint32_t        source;
395         uint16_t        destination;
396         uint16_t        length;
397 } __packed;
398
399 static int
400 write_mappings(FILE *fp, unsigned int map_idx)
401 {
402         struct mapping_list *ml = &maps[map_idx];
403         struct mapping *mp;
404         struct file_mapping fm;
405         unsigned int i = 0, j = 0;
406
407         TAILQ_FOREACH(mp, ml, m_list) {
408                 j++;
409                 if (mp->m_length > 0) {
410                         i += mp->m_length;
411                         fm.source = htobe32(mp->m_char);
412                         fm.destination = htobe16(mp->m_glyph->g_index);
413                         fm.length = htobe16(mp->m_length - 1);
414                         if (fwrite(&fm, sizeof fm, 1, fp) != 1)
415                                 return (1);
416                 }
417         }
418         assert(i == j);
419         return (0);
420 }
421
422 struct file_header {
423         uint8_t         magic[8];
424         uint8_t         width;
425         uint8_t         height;
426         uint16_t        pad;
427         uint32_t        glyph_count;
428         uint32_t        map_count[4];
429 } __packed;
430
431 static int
432 write_fnt(const char *filename)
433 {
434         FILE *fp;
435         struct file_header fh = {
436                 .magic = "VFNT0002",
437         };
438
439         fp = fopen(filename, "wb");
440         if (fp == NULL) {
441                 perror(filename);
442                 return (1);
443         }
444
445         fh.width = width;
446         fh.height = height;
447         fh.glyph_count = htobe32(glyph_unique);
448         fh.map_count[0] = htobe32(map_folded_count[0]);
449         fh.map_count[1] = htobe32(map_folded_count[1]);
450         fh.map_count[2] = htobe32(map_folded_count[2]);
451         fh.map_count[3] = htobe32(map_folded_count[3]);
452         if (fwrite(&fh, sizeof fh, 1, fp) != 1) {
453                 perror(filename);
454                 fclose(fp);
455                 return (1);
456         }
457
458         if (write_glyphs(fp) != 0 ||
459             write_mappings(fp, VFNT_MAP_NORMAL) != 0 ||
460             write_mappings(fp, 1) != 0 ||
461             write_mappings(fp, VFNT_MAP_BOLD) != 0 ||
462             write_mappings(fp, 3) != 0) {
463                 perror(filename);
464                 fclose(fp);
465                 return (1);
466         }
467
468         fclose(fp);
469         return (0);
470 }
471
472 static void
473 print_font_info(void)
474 {
475         printf(
476 "Statistics:\n"
477 "- glyph_total:                 %5u\n"
478 "- glyph_normal:                %5u\n"
479 "- glyph_normal_right:          %5u\n"
480 "- glyph_bold:                  %5u\n"
481 "- glyph_bold_right:            %5u\n"
482 "- glyph_unique:                %5u\n"
483 "- glyph_dupe:                  %5u\n"
484 "- mapping_total:               %5u\n"
485 "- mapping_normal:              %5u\n"
486 "- mapping_normal_folded:       %5u\n"
487 "- mapping_normal_right:        %5u\n"
488 "- mapping_normal_right_folded: %5u\n"
489 "- mapping_bold:                %5u\n"
490 "- mapping_bold_folded:         %5u\n"
491 "- mapping_bold_right:          %5u\n"
492 "- mapping_bold_right_folded:   %5u\n"
493 "- mapping_unique:              %5u\n"
494 "- mapping_dupe:                %5u\n",
495             glyph_total,
496             glyph_count[0],
497             glyph_count[1],
498             glyph_count[2],
499             glyph_count[3],
500             glyph_unique, glyph_dupe,
501             mapping_total,
502             map_count[0], map_folded_count[0],
503             map_count[1], map_folded_count[1],
504             map_count[2], map_folded_count[2],
505             map_count[3], map_folded_count[3],
506             mapping_unique, mapping_dupe);
507 }
508
509 int
510 main(int argc, char *argv[])
511 {
512         int ch, val, verbose = 0;
513
514         assert(sizeof(struct file_header) == 32);
515         assert(sizeof(struct file_mapping) == 8);
516
517         while ((ch = getopt(argc, argv, "h:vw:")) != -1) {
518                 switch (ch) {
519                 case 'h':
520                         val = atoi(optarg);
521                         if (val <= 0 || val > 128) {
522                                 errx(1, "Invalid height %d", val);
523                                 return (1);
524                         }
525                         height = val;
526                         break;
527                 case 'v':
528                         verbose = 1;
529                         break;
530                 case 'w':
531                         val = atoi(optarg);
532                         if (val <= 0 || val > 128) {
533                                 errx(1, "Invalid width %d", val);
534                                 return (1);
535                         }
536                         width = val;
537                         break;
538                 case '?':
539                 default:
540                         usage();
541                 }
542         }
543         argc -= optind;
544         argv += optind;
545
546         if (argc < 2 || argc > 3)
547                 usage();
548
549         wbytes = howmany(width, 8);
550
551         if (parse_file(argv[0], VFNT_MAP_NORMAL) != 0)
552                 return (1);
553         argc--;
554         argv++;
555         if (argc == 2) {
556                 if (parse_file(argv[0], VFNT_MAP_BOLD) != 0)
557                         return (1);
558                 argc--;
559                 argv++;
560         }
561         number_glyphs();
562         dedup_mapping(VFNT_MAP_BOLD);
563         dedup_mapping(VFNT_MAP_BOLD_RH);
564         fold_mappings(0);
565         fold_mappings(1);
566         fold_mappings(2);
567         fold_mappings(3);
568         if (write_fnt(argv[0]) != 0)
569                 return (1);
570
571         if (verbose)
572                 print_font_info();
573
574         return (0);
575 }