mkkfont.c revision 219888
1/*-
2 * Copyright (c) 2009 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: user/ed/newcons/sys/dev/vt/tools/mkkfont/mkkfont.c 219888 2011-03-22 21:31:31Z ed $");
32
33#include <sys/endian.h>
34#include <sys/param.h>
35
36#include <stdint.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40
41struct file_mapping {
42	uint32_t	source;
43	uint16_t	destination;
44	uint16_t	length;
45} __packed;
46
47struct file_header {
48	uint8_t		magic[8];
49	uint8_t		width;
50	uint8_t		height;
51	uint16_t	nglyphs;
52	uint16_t	nmappings_normal;
53	uint16_t	nmappings_bold;
54} __packed;
55
56static int
57print_glyphs(struct file_header *fh)
58{
59	unsigned int gbytes, nglyphs, j, k, total;
60	uint8_t *gbuf;
61
62	gbytes = howmany(fh->width, 8) * fh->height;
63	nglyphs = be16toh(fh->nglyphs);
64
65	printf("\nstatic uint8_t font_bytes[%u * %u] = {", nglyphs, gbytes);
66	total = nglyphs * gbytes;
67	gbuf = malloc(total);
68
69	if (fread(gbuf, total, 1, stdin) != 1) {
70		perror("glyph");
71		return (1);
72	}
73
74	for (j = 0; j < total; j += 12) {
75		for (k = 0; k < 12 && k < total - j; k++) {
76			printf(k == 0 ? "\n\t" : " ");
77			printf("0x%02hhx,", gbuf[j + k]);
78		}
79	}
80
81	free(gbuf);
82	printf("\n};\n");
83
84	return (0);
85}
86
87static int
88print_mappings(struct file_header *fh, int bold)
89{
90	struct file_mapping fm;
91	const char *name;
92	unsigned int nmappings, i, col = 0;
93
94	if (bold) {
95		nmappings = be16toh(fh->nmappings_bold);
96		name = "bold";
97	} else {
98		nmappings = be16toh(fh->nmappings_normal);
99		name = "normal";
100	}
101
102	if (nmappings == 0)
103		return (0);
104
105	printf("\nstatic struct vt_font_map font_mapping_%s[%u] = {",
106	    name, nmappings);
107
108	for (i = 0; i < nmappings; i++) {
109		if (fread(&fm, sizeof fm, 1, stdin) != 1) {
110			perror("mapping");
111			return (1);
112		}
113
114		printf(col == 0 ? "\n\t" : " ");
115		printf("{ 0x%04x, 0x%04x, 0x%02x },",
116		    be32toh(fm.source), be16toh(fm.destination),
117		    be16toh(fm.length));
118		col = (col + 1) % 2;
119	}
120
121	printf("\n};\n");
122
123	return (0);
124}
125
126static int
127print_info(struct file_header *fh)
128{
129	unsigned int nnormal, nbold;
130
131	printf(
132	    "\nstruct vt_font vt_font_default = {\n"
133	    "\t.vf_width\t\t= %u,\n"
134	    "\t.vf_height\t\t= %u,\n"
135	    "\t.vf_bytes\t\t= font_bytes,\n",
136	    fh->width, fh->height);
137
138	nnormal = be16toh(fh->nmappings_normal);
139	nbold = be16toh(fh->nmappings_bold);
140
141	if (nnormal != 0)
142		printf(
143		    "\t.vf_normal\t\t= font_mapping_normal,\n"
144		    "\t.vf_normal_length\t= %u,\n", nnormal);
145	if (nbold != 0)
146		printf(
147		    "\t.vf_bold\t\t= font_mapping_bold,\n"
148		    "\t.vf_bold_length\t\t= %u,\n", nbold);
149	printf("\t.vf_refcount\t\t= 1,\n};\n");
150
151	return (0);
152}
153
154int
155main(int argc __unused, char *argv[] __unused)
156{
157	struct file_header fh;
158
159	if (fread(&fh, sizeof fh, 1, stdin) != 1) {
160		perror("file_header");
161		return (1);
162	}
163
164	if (memcmp(fh.magic, "VFNT 1.0", 8) != 0) {
165		fprintf(stderr, "Bad magic\n");
166		return (1);
167	}
168
169	printf("#include <dev/vt/vt.h>\n");
170
171	if (print_glyphs(&fh) != 0)
172		return (1);
173	if (print_mappings(&fh, 0) != 0)
174		return (1);
175	if (print_mappings(&fh, 1) != 0)
176		return (1);
177	if (print_info(&fh) != 0)
178		return (1);
179
180	return (0);
181}
182