vtfontcvt.c revision 267035
1219888Sed/*-
2219888Sed * Copyright (c) 2009 The FreeBSD Foundation
3219888Sed * All rights reserved.
4219888Sed *
5219888Sed * This software was developed by Ed Schouten under sponsorship from the
6219888Sed * FreeBSD Foundation.
7219888Sed *
8219888Sed * Redistribution and use in source and binary forms, with or without
9219888Sed * modification, are permitted provided that the following conditions
10219888Sed * are met:
11219888Sed * 1. Redistributions of source code must retain the above copyright
12219888Sed *    notice, this list of conditions and the following disclaimer.
13219888Sed * 2. Redistributions in binary form must reproduce the above copyright
14219888Sed *    notice, this list of conditions and the following disclaimer in the
15219888Sed *    documentation and/or other materials provided with the distribution.
16219888Sed *
17219888Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18219888Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19219888Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20219888Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21219888Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22219888Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23219888Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24219888Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25219888Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26219888Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27219888Sed * SUCH DAMAGE.
28219888Sed */
29219888Sed
30219888Sed#include <sys/cdefs.h>
31219888Sed__FBSDID("$FreeBSD: head/tools/tools/vt/fontcvt/fontcvt.c 267035 2014-06-04 03:02:49Z emaste $");
32219888Sed
33267035Semaste#include <sys/types.h>
34267035Semaste#include <sys/fnv_hash.h>
35219888Sed#include <sys/endian.h>
36219888Sed#include <sys/param.h>
37219888Sed#include <sys/queue.h>
38219888Sed
39219888Sed#include <assert.h>
40219888Sed#include <stdint.h>
41219888Sed#include <stdio.h>
42219888Sed#include <stdlib.h>
43219888Sed#include <string.h>
44267011Semaste#include <unistd.h>
45219888Sed
46259680Semaste#define VFNT_MAPS 4
47259680Semaste#define VFNT_MAP_NORMAL 0
48259680Semaste#define VFNT_MAP_BOLD 2
49259680Semaste
50267011Semastestatic unsigned int width = 8, wbytes, height = 16;
51219888Sed
52219888Sedstruct glyph {
53219888Sed	TAILQ_ENTRY(glyph)	 g_list;
54267035Semaste	SLIST_ENTRY(glyph)	 g_hash;
55219888Sed	uint8_t			*g_data;
56219888Sed	unsigned int		 g_index;
57219888Sed};
58219888Sed
59267035Semaste#define	FONTCVT_NHASH 4096
60259680SemasteTAILQ_HEAD(glyph_list, glyph);
61267035Semastestatic SLIST_HEAD(, glyph) glyph_hash[FONTCVT_NHASH];
62259680Semastestatic struct glyph_list glyphs[VFNT_MAPS] = {
63259680Semaste    TAILQ_HEAD_INITIALIZER(glyphs[0]),
64259680Semaste    TAILQ_HEAD_INITIALIZER(glyphs[1]),
65259680Semaste    TAILQ_HEAD_INITIALIZER(glyphs[2]),
66259680Semaste    TAILQ_HEAD_INITIALIZER(glyphs[3]),
67259680Semaste};
68259680Semastestatic unsigned int glyph_total, glyph_count[4], glyph_unique, glyph_dupe;
69219888Sed
70219888Sedstruct mapping {
71219888Sed	TAILQ_ENTRY(mapping)	 m_list;
72219888Sed	unsigned int		 m_char;
73219888Sed	unsigned int		 m_length;
74219888Sed	struct glyph		*m_glyph;
75219888Sed};
76219888Sed
77219888SedTAILQ_HEAD(mapping_list, mapping);
78259680Semastestatic struct mapping_list maps[VFNT_MAPS] = {
79259680Semaste    TAILQ_HEAD_INITIALIZER(maps[0]),
80259680Semaste    TAILQ_HEAD_INITIALIZER(maps[1]),
81259680Semaste    TAILQ_HEAD_INITIALIZER(maps[2]),
82259680Semaste    TAILQ_HEAD_INITIALIZER(maps[3]),
83259680Semaste};
84259680Semastestatic unsigned int mapping_total, map_count[4], map_folded_count[4],
85259680Semaste    mapping_unique, mapping_dupe;
86219888Sed
87219888Sedstatic void
88219888Sedusage(void)
89219888Sed{
90219888Sed
91219888Sed	fprintf(stderr,
92267012Semaste"usage: fontcvt [-w width] [-h height] normal.bdf [bold.bdf] out.fnt\n");
93219888Sed	exit(1);
94219888Sed}
95219888Sed
96219888Sedstatic int
97259680Semasteadd_mapping(struct glyph *gl, unsigned int c, unsigned int map_idx)
98219888Sed{
99219888Sed	struct mapping *mp;
100219888Sed	struct mapping_list *ml;
101219888Sed
102219888Sed	mapping_total++;
103219888Sed
104259680Semaste	if (map_idx >= VFNT_MAP_BOLD) {
105219888Sed		int found = 0;
106259680Semaste		unsigned normal_map_idx = map_idx - VFNT_MAP_BOLD;
107219888Sed
108259680Semaste		TAILQ_FOREACH(mp, &maps[normal_map_idx], m_list) {
109219888Sed			if (mp->m_char < c)
110219888Sed				continue;
111219888Sed			else if (mp->m_char > c)
112219888Sed				break;
113219888Sed			found = 1;
114219888Sed
115219888Sed			/*
116219888Sed			 * No mapping is needed if it's equal to the
117219888Sed			 * normal mapping.
118219888Sed			 */
119219888Sed			if (mp->m_glyph == gl) {
120219888Sed				mapping_dupe++;
121219888Sed				return (0);
122219888Sed			}
123219888Sed		}
124219888Sed
125219888Sed		if (!found) {
126219888Sed			fprintf(stderr,
127219888Sed			    "Character %u not in normal font!\n", c);
128219888Sed			return (1);
129219888Sed		}
130219888Sed	}
131219888Sed
132219888Sed	mp = malloc(sizeof *mp);
133219888Sed	mp->m_char = c;
134219888Sed	mp->m_glyph = gl;
135219888Sed	mp->m_length = 0;
136219888Sed
137259680Semaste	ml = &maps[map_idx];
138219888Sed	if (TAILQ_LAST(ml, mapping_list) != NULL &&
139219888Sed	    TAILQ_LAST(ml, mapping_list)->m_char >= c) {
140219888Sed		fprintf(stderr, "Bad ordering at character %u\n", c);
141219888Sed		return (1);
142219888Sed	}
143219888Sed	TAILQ_INSERT_TAIL(ml, mp, m_list);
144219888Sed
145259680Semaste	map_count[map_idx]++;
146219888Sed	mapping_unique++;
147219888Sed
148219888Sed	return (0);
149219888Sed}
150219888Sed
151219888Sedstatic struct glyph *
152259680Semasteadd_glyph(const uint8_t *bytes, unsigned int map_idx, int fallback)
153219888Sed{
154219888Sed	struct glyph *gl;
155267035Semaste	int hash;
156219888Sed
157219888Sed	glyph_total++;
158259680Semaste	glyph_count[map_idx]++;
159219888Sed
160267035Semaste	hash = fnv_32_buf(bytes, wbytes * height, FNV1_32_INIT) % FONTCVT_NHASH;
161267035Semaste	SLIST_FOREACH(gl, &glyph_hash[hash], g_hash) {
162267035Semaste		if (memcmp(gl->g_data, bytes, wbytes * height) == 0) {
163267035Semaste			glyph_dupe++;
164267035Semaste			return (gl);
165219888Sed		}
166219888Sed	}
167219888Sed
168219888Sed	gl = malloc(sizeof *gl);
169219888Sed	gl->g_data = malloc(wbytes * height);
170219888Sed	memcpy(gl->g_data, bytes, wbytes * height);
171219888Sed	if (fallback)
172259680Semaste		TAILQ_INSERT_HEAD(&glyphs[map_idx], gl, g_list);
173219888Sed	else
174259680Semaste		TAILQ_INSERT_TAIL(&glyphs[map_idx], gl, g_list);
175267035Semaste	SLIST_INSERT_HEAD(&glyph_hash[hash], gl, g_hash);
176219888Sed
177219888Sed	glyph_unique++;
178219888Sed	return (gl);
179219888Sed}
180219888Sed
181219888Sedstatic int
182259680Semasteparse_bitmap_line(uint8_t *left, uint8_t *right, unsigned int line,
183259680Semaste    unsigned int dwidth)
184219888Sed{
185259680Semaste	uint8_t *p;
186259680Semaste	unsigned int i, subline;
187259680Semaste
188259680Semaste	if (dwidth != width && dwidth != width * 2) {
189259680Semaste		fprintf(stderr,
190259680Semaste		    "Unsupported width %u!\n", dwidth);
191259680Semaste		return (1);
192259680Semaste	}
193259680Semaste
194259680Semaste	/* Move pixel data right to simplify splitting double characters. */
195259680Semaste	line >>= (howmany(dwidth, 8) * 8) - dwidth;
196259680Semaste
197259680Semaste	for (i = dwidth / width; i > 0; i--) {
198259680Semaste		p = (i == 2) ? right : left;
199259680Semaste
200259680Semaste		subline = line & ((1 << width) - 1);
201259680Semaste		subline <<= (howmany(width, 8) * 8) - width;
202259680Semaste
203259680Semaste		if (wbytes == 1) {
204259680Semaste			*p = subline;
205259680Semaste		} else if (wbytes == 2) {
206259680Semaste			*p++ = subline >> 8;
207259680Semaste			*p = subline;
208259680Semaste		} else {
209259680Semaste			fprintf(stderr,
210259680Semaste			    "Unsupported wbytes %u!\n", wbytes);
211259680Semaste			return (1);
212259680Semaste		}
213259680Semaste
214259680Semaste		line >>= width;
215259680Semaste	}
216259680Semaste
217259680Semaste	return (0);
218259680Semaste}
219259680Semaste
220259680Semastestatic int
221259680Semasteparse_bdf(const char *filename, unsigned int map_idx)
222259680Semaste{
223219888Sed	FILE *fp;
224219888Sed	char *ln;
225219888Sed	size_t length;
226259680Semaste	uint8_t bytes[wbytes * height], bytes_r[wbytes * height];
227259680Semaste	unsigned int curchar = 0, dwidth = 0, i, line;
228219888Sed	struct glyph *gl;
229219888Sed
230219888Sed	fp = fopen(filename, "r");
231219888Sed	if (fp == NULL) {
232219888Sed		perror(filename);
233219888Sed		return (1);
234219888Sed	}
235219888Sed
236219888Sed	while ((ln = fgetln(fp, &length)) != NULL) {
237219888Sed		ln[length - 1] = '\0';
238219888Sed
239219888Sed		if (strncmp(ln, "ENCODING ", 9) == 0) {
240219888Sed			curchar = atoi(ln + 9);
241219888Sed		}
242219888Sed
243259680Semaste		if (strncmp(ln, "DWIDTH ", 7) == 0) {
244259680Semaste			dwidth = atoi(ln + 7);
245259680Semaste		}
246259680Semaste
247219888Sed		if (strcmp(ln, "BITMAP") == 0) {
248219888Sed			for (i = 0; i < height; i++) {
249219888Sed				if ((ln = fgetln(fp, &length)) == NULL) {
250219888Sed					fprintf(stderr, "Unexpected EOF!\n");
251219888Sed					return (1);
252219888Sed				}
253219888Sed				ln[length - 1] = '\0';
254219888Sed				sscanf(ln, "%x", &line);
255259680Semaste				if (parse_bitmap_line(bytes + i * wbytes,
256259680Semaste				     bytes_r + i * wbytes, line, dwidth) != 0)
257219888Sed					return (1);
258219888Sed			}
259219888Sed
260219888Sed			/* Prevent adding two glyphs for 0xFFFD */
261219888Sed			if (curchar == 0xFFFD) {
262259680Semaste				if (map_idx < VFNT_MAP_BOLD)
263259680Semaste					gl = add_glyph(bytes, 0, 1);
264219888Sed			} else if (curchar >= 0x20) {
265259680Semaste				gl = add_glyph(bytes, map_idx, 0);
266259680Semaste				if (add_mapping(gl, curchar, map_idx) != 0)
267219888Sed					return (1);
268259680Semaste				if (dwidth == width * 2) {
269259680Semaste					gl = add_glyph(bytes_r, map_idx + 1, 0);
270259680Semaste					if (add_mapping(gl, curchar,
271259680Semaste					    map_idx + 1) != 0)
272259680Semaste						return (1);
273259680Semaste				}
274219888Sed			}
275219888Sed		}
276219888Sed	}
277219888Sed
278219888Sed	return (0);
279219888Sed}
280219888Sed
281219888Sedstatic void
282219888Sednumber_glyphs(void)
283219888Sed{
284219888Sed	struct glyph *gl;
285259680Semaste	unsigned int i, idx = 0;
286219888Sed
287259680Semaste	for (i = 0; i < VFNT_MAPS; i++)
288259680Semaste		TAILQ_FOREACH(gl, &glyphs[i], g_list)
289259680Semaste			gl->g_index = idx++;
290219888Sed}
291219888Sed
292219888Sedstatic void
293219888Sedwrite_glyphs(FILE *fp)
294219888Sed{
295219888Sed	struct glyph *gl;
296259680Semaste	unsigned int i;
297219888Sed
298259680Semaste	for (i = 0; i < VFNT_MAPS; i++) {
299259680Semaste		TAILQ_FOREACH(gl, &glyphs[i], g_list)
300259680Semaste			fwrite(gl->g_data, wbytes * height, 1, fp);
301259680Semaste	}
302219888Sed}
303219888Sed
304219888Sedstatic void
305259680Semastefold_mappings(unsigned int map_idx)
306219888Sed{
307259680Semaste	struct mapping_list *ml = &maps[map_idx];
308219888Sed	struct mapping *mn, *mp, *mbase;
309219888Sed
310219888Sed	mp = mbase = TAILQ_FIRST(ml);
311219888Sed	for (mp = mbase = TAILQ_FIRST(ml); mp != NULL; mp = mn) {
312219888Sed		mn = TAILQ_NEXT(mp, m_list);
313219888Sed		if (mn != NULL && mn->m_char == mp->m_char + 1 &&
314219888Sed		    mn->m_glyph->g_index == mp->m_glyph->g_index + 1)
315219888Sed			continue;
316219888Sed		mbase->m_length = mp->m_char - mbase->m_char + 1;
317219888Sed		mbase = mp = mn;
318259680Semaste		map_folded_count[map_idx]++;
319219888Sed	}
320219888Sed}
321219888Sed
322219888Sedstruct file_mapping {
323219888Sed	uint32_t	source;
324219888Sed	uint16_t	destination;
325219888Sed	uint16_t	length;
326219888Sed} __packed;
327219888Sed
328219888Sedstatic void
329259680Semastewrite_mappings(FILE *fp, unsigned int map_idx)
330219888Sed{
331259680Semaste	struct mapping_list *ml = &maps[map_idx];
332219888Sed	struct mapping *mp;
333219888Sed	struct file_mapping fm;
334219888Sed	unsigned int i = 0, j = 0;
335219888Sed
336219888Sed	TAILQ_FOREACH(mp, ml, m_list) {
337219888Sed		j++;
338219888Sed		if (mp->m_length > 0) {
339219888Sed			i += mp->m_length;
340219888Sed			fm.source = htobe32(mp->m_char);
341219888Sed			fm.destination = htobe16(mp->m_glyph->g_index);
342219888Sed			fm.length = htobe16(mp->m_length - 1);
343219888Sed			fwrite(&fm, sizeof fm, 1, fp);
344219888Sed		}
345219888Sed	}
346219888Sed	assert(i == j);
347219888Sed}
348219888Sed
349219888Sedstruct file_header {
350219888Sed	uint8_t		magic[8];
351219888Sed	uint8_t		width;
352219888Sed	uint8_t		height;
353259680Semaste	uint16_t	pad;
354259680Semaste	uint32_t	glyph_count;
355259680Semaste	uint32_t	map_count[4];
356219888Sed} __packed;
357219888Sed
358219888Sedstatic int
359219888Sedwrite_fnt(const char *filename)
360219888Sed{
361219888Sed	FILE *fp;
362219888Sed	struct file_header fh = {
363259680Semaste		.magic = "VFNT0002",
364219888Sed	};
365219888Sed
366219888Sed	fp = fopen(filename, "wb");
367219888Sed	if (fp == NULL) {
368219888Sed		perror(filename);
369219888Sed		return (1);
370219888Sed	}
371219888Sed
372219888Sed	fh.width = width;
373219888Sed	fh.height = height;
374259680Semaste	fh.glyph_count = htobe32(glyph_unique);
375259680Semaste	fh.map_count[0] = htobe32(map_folded_count[0]);
376259680Semaste	fh.map_count[1] = htobe32(map_folded_count[1]);
377259680Semaste	fh.map_count[2] = htobe32(map_folded_count[2]);
378259680Semaste	fh.map_count[3] = htobe32(map_folded_count[3]);
379219888Sed	fwrite(&fh, sizeof fh, 1, fp);
380219888Sed
381219888Sed	write_glyphs(fp);
382259680Semaste	write_mappings(fp, VFNT_MAP_NORMAL);
383219888Sed	write_mappings(fp, 1);
384259680Semaste	write_mappings(fp, VFNT_MAP_BOLD);
385259680Semaste	write_mappings(fp, 3);
386219888Sed
387219888Sed	return (0);
388219888Sed}
389219888Sed
390219888Sedint
391219888Sedmain(int argc, char *argv[])
392219888Sed{
393267011Semaste	int ch;
394219888Sed
395259680Semaste	assert(sizeof(struct file_header) == 32);
396219888Sed	assert(sizeof(struct file_mapping) == 8);
397219888Sed
398267011Semaste	while ((ch = getopt(argc, argv, "h:w:")) != -1) {
399267011Semaste		switch (ch) {
400267011Semaste		case 'h':
401267011Semaste			height = atoi(optarg);
402267011Semaste			break;
403267011Semaste		case 'w':
404267011Semaste			height = atoi(optarg);
405267011Semaste			break;
406267011Semaste		case '?':
407267011Semaste		default:
408267011Semaste			usage();
409267011Semaste		}
410267011Semaste	}
411267011Semaste	argc -= optind;
412267011Semaste	argv += optind;
413267011Semaste
414267012Semaste	if (argc < 2 || argc > 3)
415219888Sed		usage();
416267011Semaste
417219888Sed	wbytes = howmany(width, 8);
418219888Sed
419267011Semaste	if (parse_bdf(argv[0], VFNT_MAP_NORMAL) != 0)
420219888Sed		return (1);
421267012Semaste	argc--;
422267012Semaste	argv++;
423267012Semaste	if (argc == 2) {
424267012Semaste		if (parse_bdf(argv[0], VFNT_MAP_BOLD) != 0)
425267012Semaste			return (1);
426267012Semaste		argc--;
427267012Semaste		argv++;
428267012Semaste	}
429219888Sed	number_glyphs();
430219888Sed	fold_mappings(0);
431219888Sed	fold_mappings(1);
432259680Semaste	fold_mappings(2);
433259680Semaste	fold_mappings(3);
434267012Semaste	if (write_fnt(argv[0]) != 0)
435219888Sed		return (1);
436219888Sed
437219888Sed	printf(
438219888Sed"Statistics:\n"
439259680Semaste"- glyph_total:                 %5u\n"
440259680Semaste"- glyph_normal:                %5u\n"
441259680Semaste"- glyph_normal_right:          %5u\n"
442259680Semaste"- glyph_bold:                  %5u\n"
443259680Semaste"- glyph_bold_right:            %5u\n"
444259680Semaste"- glyph_unique:                %5u\n"
445259680Semaste"- glyph_dupe:                  %5u\n"
446259680Semaste"- mapping_total:               %5u\n"
447259680Semaste"- mapping_normal:              %5u\n"
448259680Semaste"- mapping_normal_folded:       %5u\n"
449259680Semaste"- mapping_normal_right:        %5u\n"
450259680Semaste"- mapping_normal_right_folded: %5u\n"
451259680Semaste"- mapping_bold:                %5u\n"
452259680Semaste"- mapping_bold_folded:         %5u\n"
453259680Semaste"- mapping_bold_right:          %5u\n"
454259680Semaste"- mapping_bold_right_folded:   %5u\n"
455259680Semaste"- mapping_unique:              %5u\n"
456259680Semaste"- mapping_dupe:                %5u\n",
457219888Sed	    glyph_total,
458259680Semaste	    glyph_count[0],
459259680Semaste	    glyph_count[1],
460259680Semaste	    glyph_count[2],
461259680Semaste	    glyph_count[3],
462219888Sed	    glyph_unique, glyph_dupe,
463219888Sed	    mapping_total,
464259680Semaste	    map_count[0], map_folded_count[0],
465259680Semaste	    map_count[1], map_folded_count[1],
466259680Semaste	    map_count[2], map_folded_count[2],
467259680Semaste	    map_count[3], map_folded_count[3],
468219888Sed	    mapping_unique, mapping_dupe);
469219888Sed
470219888Sed	return (0);
471219888Sed}
472