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