xref: /freebsd/usr.bin/vtfontcvt/vtfontcvt.c (revision 67ca7330cf34a789afbbff9ae7e4cdc4a4917ae3)
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 	hash = fnv_32_buf(bytes, wbytes * height, FNV1_32_INIT) % FONTCVT_NHASH;
180 	SLIST_FOREACH(gl, &glyph_hash[hash], g_hash) {
181 		if (memcmp(gl->g_data, bytes, wbytes * height) == 0) {
182 			glyph_dupe++;
183 			return (gl);
184 		}
185 	}
186 
187 	gl = xmalloc(sizeof *gl);
188 	gl->g_data = xmalloc(wbytes * height);
189 	memcpy(gl->g_data, bytes, wbytes * height);
190 	if (fallback)
191 		TAILQ_INSERT_HEAD(&glyphs[map_idx], gl, g_list);
192 	else
193 		TAILQ_INSERT_TAIL(&glyphs[map_idx], gl, g_list);
194 	SLIST_INSERT_HEAD(&glyph_hash[hash], gl, g_hash);
195 
196 	glyph_unique++;
197 	if (glyph_unique > VFNT_MAXGLYPHS)
198 		errx(1, "too many glyphs (%u)", glyph_unique);
199 	return (gl);
200 }
201 
202 static int
203 add_char(unsigned curchar, unsigned map_idx, uint8_t *bytes, uint8_t *bytes_r)
204 {
205 	struct glyph *gl;
206 
207 	/* Prevent adding two glyphs for 0xFFFD */
208 	if (curchar == 0xFFFD) {
209 		if (map_idx < VFNT_MAP_BOLD)
210 			gl = add_glyph(bytes, 0, 1);
211 	} else if (curchar >= 0x20) {
212 		gl = add_glyph(bytes, map_idx, 0);
213 		if (add_mapping(gl, curchar, map_idx) != 0)
214 			return (1);
215 		if (bytes_r != NULL) {
216 			gl = add_glyph(bytes_r, map_idx + 1, 0);
217 			if (add_mapping(gl, curchar, map_idx + 1) != 0)
218 				return (1);
219 		}
220 	}
221 	return (0);
222 }
223 
224 
225 static int
226 parse_bitmap_line(uint8_t *left, uint8_t *right, unsigned int line,
227     unsigned int dwidth)
228 {
229 	uint8_t *p;
230 	unsigned int i, subline;
231 
232 	if (dwidth != width && dwidth != width * 2)
233 		errx(1, "Bitmap with unsupported width %u!", dwidth);
234 
235 	/* Move pixel data right to simplify splitting double characters. */
236 	line >>= (howmany(dwidth, 8) * 8) - dwidth;
237 
238 	for (i = dwidth / width; i > 0; i--) {
239 		p = (i == 2) ? right : left;
240 
241 		subline = line & ((1 << width) - 1);
242 		subline <<= (howmany(width, 8) * 8) - width;
243 
244 		if (wbytes == 1) {
245 			*p = subline;
246 		} else if (wbytes == 2) {
247 			*p++ = subline >> 8;
248 			*p = subline;
249 		} else {
250 			errx(1, "Unsupported wbytes %u!", wbytes);
251 		}
252 
253 		line >>= width;
254 	}
255 
256 	return (0);
257 }
258 
259 static int
260 parse_bdf(FILE *fp, unsigned int map_idx)
261 {
262 	char *ln;
263 	size_t length;
264 	uint8_t bytes[wbytes * height], bytes_r[wbytes * height];
265 	unsigned int curchar = 0, dwidth = 0, i, line;
266 
267 	while ((ln = fgetln(fp, &length)) != NULL) {
268 		ln[length - 1] = '\0';
269 
270 		if (strncmp(ln, "ENCODING ", 9) == 0) {
271 			curchar = atoi(ln + 9);
272 		}
273 
274 		if (strncmp(ln, "DWIDTH ", 7) == 0) {
275 			dwidth = atoi(ln + 7);
276 		}
277 
278 		if (strncmp(ln, "BITMAP", 6) == 0 &&
279 		    (ln[6] == ' ' || ln[6] == '\0')) {
280 			/*
281 			 * Assume that the next _height_ lines are bitmap
282 			 * data.  ENDCHAR is allowed to terminate the bitmap
283 			 * early but is not otherwise checked; any extra data
284 			 * is ignored.
285 			 */
286 			for (i = 0; i < height; i++) {
287 				if ((ln = fgetln(fp, &length)) == NULL)
288 					errx(1, "Unexpected EOF!");
289 				ln[length - 1] = '\0';
290 				if (strcmp(ln, "ENDCHAR") == 0) {
291 					memset(bytes + i * wbytes, 0,
292 					    (height - i) * wbytes);
293 					memset(bytes_r + i * wbytes, 0,
294 					    (height - i) * wbytes);
295 					break;
296 				}
297 				sscanf(ln, "%x", &line);
298 				if (parse_bitmap_line(bytes + i * wbytes,
299 				     bytes_r + i * wbytes, line, dwidth) != 0)
300 					return (1);
301 			}
302 
303 			if (add_char(curchar, map_idx, bytes,
304 			    dwidth == width * 2 ? bytes_r : NULL) != 0)
305 				return (1);
306 		}
307 	}
308 
309 	return (0);
310 }
311 
312 static void
313 set_height(int h)
314 {
315 	if (h <= 0 || h > VFNT_MAXDIMENSION)
316 		errx(1, "invalid height %d", h);
317 	height = h;
318 }
319 
320 static void
321 set_width(int w)
322 {
323 	if (w <= 0 || w > VFNT_MAXDIMENSION)
324 		errx(1, "invalid width %d", w);
325 	width = w;
326 	wbytes = howmany(width, 8);
327 }
328 
329 static int
330 parse_hex(FILE *fp, unsigned int map_idx)
331 {
332 	char *ln, *p;
333 	char fmt_str[8];
334 	size_t length;
335 	uint8_t *bytes = NULL, *bytes_r = NULL;
336 	unsigned curchar = 0, i, line, chars_per_row, dwidth;
337 	int rv = 0;
338 
339 	while ((ln = fgetln(fp, &length)) != NULL) {
340 		ln[length - 1] = '\0';
341 
342 		if (strncmp(ln, "# Height: ", 10) == 0) {
343 			if (bytes != NULL)
344 				errx(1, "malformed input: Height tag after font data");
345 			set_height(atoi(ln + 10));
346 		} else if (strncmp(ln, "# Width: ", 9) == 0) {
347 			if (bytes != NULL)
348 				errx(1, "malformed input: Width tag after font data");
349 			set_width(atoi(ln + 9));
350 		} else if (sscanf(ln, "%6x:", &curchar)) {
351 			if (bytes == NULL) {
352 				bytes = xmalloc(wbytes * height);
353 				bytes_r = xmalloc(wbytes * height);
354 			}
355 			/* ln is guaranteed to have a colon here. */
356 			p = strchr(ln, ':') + 1;
357 			chars_per_row = strlen(p) / height;
358 			dwidth = width;
359 			if (chars_per_row / 2 > (width + 7) / 8)
360 				dwidth *= 2; /* Double-width character. */
361 			snprintf(fmt_str, sizeof(fmt_str), "%%%ux",
362 			    chars_per_row);
363 
364 			for (i = 0; i < height; i++) {
365 				sscanf(p, fmt_str, &line);
366 				p += chars_per_row;
367 				if (parse_bitmap_line(bytes + i * wbytes,
368 				    bytes_r + i * wbytes, line, dwidth) != 0) {
369 					rv = 1;
370 					goto out;
371 				}
372 			}
373 
374 			if (add_char(curchar, map_idx, bytes,
375 			    dwidth == width * 2 ? bytes_r : NULL) != 0) {
376 				rv = 1;
377 				goto out;
378 			}
379 		}
380 	}
381 out:
382 	free(bytes);
383 	free(bytes_r);
384 	return (rv);
385 }
386 
387 static int
388 parse_file(const char *filename, unsigned int map_idx)
389 {
390 	FILE *fp;
391 	size_t len;
392 	int rv;
393 
394 	fp = fopen(filename, "r");
395 	if (fp == NULL) {
396 		perror(filename);
397 		return (1);
398 	}
399 	len = strlen(filename);
400 	if (len > 4 && strcasecmp(filename + len - 4, ".hex") == 0)
401 		rv = parse_hex(fp, map_idx);
402 	else
403 		rv = parse_bdf(fp, map_idx);
404 	fclose(fp);
405 	return (rv);
406 }
407 
408 static void
409 number_glyphs(void)
410 {
411 	struct glyph *gl;
412 	unsigned int i, idx = 0;
413 
414 	for (i = 0; i < VFNT_MAPS; i++)
415 		TAILQ_FOREACH(gl, &glyphs[i], g_list)
416 			gl->g_index = idx++;
417 }
418 
419 static int
420 write_glyphs(FILE *fp)
421 {
422 	struct glyph *gl;
423 	unsigned int i;
424 
425 	for (i = 0; i < VFNT_MAPS; i++) {
426 		TAILQ_FOREACH(gl, &glyphs[i], g_list)
427 			if (fwrite(gl->g_data, wbytes * height, 1, fp) != 1)
428 				return (1);
429 	}
430 	return (0);
431 }
432 
433 static void
434 fold_mappings(unsigned int map_idx)
435 {
436 	struct mapping_list *ml = &maps[map_idx];
437 	struct mapping *mn, *mp, *mbase;
438 
439 	mp = mbase = TAILQ_FIRST(ml);
440 	for (mp = mbase = TAILQ_FIRST(ml); mp != NULL; mp = mn) {
441 		mn = TAILQ_NEXT(mp, m_list);
442 		if (mn != NULL && mn->m_char == mp->m_char + 1 &&
443 		    mn->m_glyph->g_index == mp->m_glyph->g_index + 1)
444 			continue;
445 		mbase->m_length = mp->m_char - mbase->m_char + 1;
446 		mbase = mp = mn;
447 		map_folded_count[map_idx]++;
448 	}
449 }
450 
451 struct file_mapping {
452 	uint32_t	source;
453 	uint16_t	destination;
454 	uint16_t	length;
455 } __packed;
456 
457 static int
458 write_mappings(FILE *fp, unsigned int map_idx)
459 {
460 	struct mapping_list *ml = &maps[map_idx];
461 	struct mapping *mp;
462 	struct file_mapping fm;
463 	unsigned int i = 0, j = 0;
464 
465 	TAILQ_FOREACH(mp, ml, m_list) {
466 		j++;
467 		if (mp->m_length > 0) {
468 			i += mp->m_length;
469 			fm.source = htobe32(mp->m_char);
470 			fm.destination = htobe16(mp->m_glyph->g_index);
471 			fm.length = htobe16(mp->m_length - 1);
472 			if (fwrite(&fm, sizeof fm, 1, fp) != 1)
473 				return (1);
474 		}
475 	}
476 	assert(i == j);
477 	return (0);
478 }
479 
480 struct file_header {
481 	uint8_t		magic[8];
482 	uint8_t		width;
483 	uint8_t		height;
484 	uint16_t	pad;
485 	uint32_t	glyph_count;
486 	uint32_t	map_count[4];
487 } __packed;
488 
489 static int
490 write_fnt(const char *filename)
491 {
492 	FILE *fp;
493 	struct file_header fh = {
494 		.magic = "VFNT0002",
495 	};
496 
497 	fp = fopen(filename, "wb");
498 	if (fp == NULL) {
499 		perror(filename);
500 		return (1);
501 	}
502 
503 	fh.width = width;
504 	fh.height = height;
505 	fh.glyph_count = htobe32(glyph_unique);
506 	fh.map_count[0] = htobe32(map_folded_count[0]);
507 	fh.map_count[1] = htobe32(map_folded_count[1]);
508 	fh.map_count[2] = htobe32(map_folded_count[2]);
509 	fh.map_count[3] = htobe32(map_folded_count[3]);
510 	if (fwrite(&fh, sizeof fh, 1, fp) != 1) {
511 		perror(filename);
512 		fclose(fp);
513 		return (1);
514 	}
515 
516 	if (write_glyphs(fp) != 0 ||
517 	    write_mappings(fp, VFNT_MAP_NORMAL) != 0 ||
518 	    write_mappings(fp, VFNT_MAP_NORMAL_RH) != 0 ||
519 	    write_mappings(fp, VFNT_MAP_BOLD) != 0 ||
520 	    write_mappings(fp, VFNT_MAP_BOLD_RH) != 0) {
521 		perror(filename);
522 		fclose(fp);
523 		return (1);
524 	}
525 
526 	fclose(fp);
527 	return (0);
528 }
529 
530 static void
531 print_font_info(void)
532 {
533 	printf(
534 "Statistics:\n"
535 "- width:                       %6u\n"
536 "- height:                      %6u\n"
537 "- glyph_total:                 %6u\n"
538 "- glyph_normal:                %6u\n"
539 "- glyph_normal_right:          %6u\n"
540 "- glyph_bold:                  %6u\n"
541 "- glyph_bold_right:            %6u\n"
542 "- glyph_unique:                %6u\n"
543 "- glyph_dupe:                  %6u\n"
544 "- mapping_total:               %6u\n"
545 "- mapping_normal:              %6u\n"
546 "- mapping_normal_folded:       %6u\n"
547 "- mapping_normal_right:        %6u\n"
548 "- mapping_normal_right_folded: %6u\n"
549 "- mapping_bold:                %6u\n"
550 "- mapping_bold_folded:         %6u\n"
551 "- mapping_bold_right:          %6u\n"
552 "- mapping_bold_right_folded:   %6u\n"
553 "- mapping_unique:              %6u\n"
554 "- mapping_dupe:                %6u\n",
555 	    width, height,
556 	    glyph_total,
557 	    glyph_count[0],
558 	    glyph_count[1],
559 	    glyph_count[2],
560 	    glyph_count[3],
561 	    glyph_unique, glyph_dupe,
562 	    mapping_total,
563 	    map_count[0], map_folded_count[0],
564 	    map_count[1], map_folded_count[1],
565 	    map_count[2], map_folded_count[2],
566 	    map_count[3], map_folded_count[3],
567 	    mapping_unique, mapping_dupe);
568 }
569 
570 int
571 main(int argc, char *argv[])
572 {
573 	int ch, verbose = 0;
574 
575 	assert(sizeof(struct file_header) == 32);
576 	assert(sizeof(struct file_mapping) == 8);
577 
578 	while ((ch = getopt(argc, argv, "h:vw:")) != -1) {
579 		switch (ch) {
580 		case 'h':
581 			height = atoi(optarg);
582 			break;
583 		case 'v':
584 			verbose = 1;
585 			break;
586 		case 'w':
587 			width = atoi(optarg);
588 			break;
589 		case '?':
590 		default:
591 			usage();
592 		}
593 	}
594 	argc -= optind;
595 	argv += optind;
596 
597 	if (argc < 2 || argc > 3)
598 		usage();
599 
600 	set_width(width);
601 	set_height(height);
602 
603 	if (parse_file(argv[0], VFNT_MAP_NORMAL) != 0)
604 		return (1);
605 	argc--;
606 	argv++;
607 	if (argc == 2) {
608 		if (parse_file(argv[0], VFNT_MAP_BOLD) != 0)
609 			return (1);
610 		argc--;
611 		argv++;
612 	}
613 	number_glyphs();
614 	dedup_mapping(VFNT_MAP_BOLD);
615 	dedup_mapping(VFNT_MAP_BOLD_RH);
616 	fold_mappings(0);
617 	fold_mappings(1);
618 	fold_mappings(2);
619 	fold_mappings(3);
620 	if (write_fnt(argv[0]) != 0)
621 		return (1);
622 
623 	if (verbose)
624 		print_font_info();
625 
626 	return (0);
627 }
628