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$");
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	pad;
52	uint32_t	glyph_count;
53	uint32_t	map_count[4];
54} __packed;
55
56static int
57print_glyphs(struct file_header *fh)
58{
59	unsigned int gbytes, glyph_count, j, k, total;
60	uint8_t *gbuf;
61
62	gbytes = howmany(fh->width, 8) * fh->height;
63	glyph_count = be32toh(fh->glyph_count);
64
65	printf("\nstatic uint8_t font_bytes[%u * %u] = {", glyph_count, gbytes);
66	total = glyph_count * 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 const char *map_names[4] = {
88    "normal", "normal_right", "bold", "bold_right" };
89
90static int
91print_mappings(struct file_header *fh, int map_index)
92{
93	struct file_mapping fm;
94	unsigned int nmappings, i, col = 0;
95
96
97	nmappings = be32toh(fh->map_count[map_index]);
98
99	if (nmappings == 0)
100		return (0);
101
102	printf("\nstatic struct vt_font_map font_mapping_%s[%u] = {",
103	    map_names[map_index], nmappings);
104
105	for (i = 0; i < nmappings; i++) {
106		if (fread(&fm, sizeof fm, 1, stdin) != 1) {
107			perror("mapping");
108			return (1);
109		}
110
111		printf(col == 0 ? "\n\t" : " ");
112		printf("{ 0x%04x, 0x%04x, 0x%02x },",
113		    be32toh(fm.source), be16toh(fm.destination),
114		    be16toh(fm.length));
115		col = (col + 1) % 2;
116	}
117
118	printf("\n};\n");
119
120	return (0);
121}
122
123static int
124print_info(struct file_header *fh)
125{
126	unsigned int i;
127
128	printf(
129	    "\nstruct vt_font vt_font_default = {\n"
130	    "\t.vf_width\t\t= %u,\n"
131	    "\t.vf_height\t\t= %u,\n"
132	    "\t.vf_bytes\t\t= font_bytes,\n",
133	    fh->width, fh->height);
134
135	printf("\t.vf_map\t\t\t= {\n");
136	for (i = 0; i < 4; i++) {
137		if (fh->map_count[i] > 0)
138			printf("\t\t\t\t    font_mapping_%s,\n", map_names[i]);
139		else
140			printf("\t\t\t\t    NULL,\n");
141	}
142	printf("\t\t\t\t  },\n");
143	printf("\t.vf_map_count\t\t= { %u, %u, %u, %u },\n",
144	    be32toh(fh->map_count[0]),
145	    be32toh(fh->map_count[1]),
146	    be32toh(fh->map_count[2]),
147	    be32toh(fh->map_count[3]));
148	printf("\t.vf_refcount\t\t= 1,\n};\n");
149
150	return (0);
151}
152
153int
154main(int argc __unused, char *argv[] __unused)
155{
156	struct file_header fh;
157	unsigned int i;
158
159	if (fread(&fh, sizeof fh, 1, stdin) != 1) {
160		perror("file_header");
161		return (1);
162	}
163
164	if (memcmp(fh.magic, "VFNT0002", 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	for (i = 0; i < 4; i++)
174		if (print_mappings(&fh, i) != 0)
175			return (1);
176	if (print_info(&fh) != 0)
177		return (1);
178
179	return (0);
180}
181