xref: /freebsd/tools/tools/vt/mkkfont/mkkfont.c (revision 5c2bc3db201a4fe8d7911cf816bea104d5dc2138)
1 /*-
2  * Copyright (c) 2009 The FreeBSD Foundation
3  *
4  * This software was developed by Ed Schouten under sponsorship from the
5  * FreeBSD Foundation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include <sys/endian.h>
31 #include <sys/param.h>
32 #include <sys/font.h>
33 
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 static int
40 print_glyphs(struct font_header *fh)
41 {
42 	unsigned int gbytes, glyph_count, j, k, total;
43 	uint8_t *gbuf;
44 
45 	gbytes = howmany(fh->fh_width, 8) * fh->fh_height;
46 	glyph_count = be32toh(fh->fh_glyph_count);
47 
48 	printf("\nstatic uint8_t font_bytes[%u * %u] = {", glyph_count, gbytes);
49 	total = glyph_count * gbytes;
50 	gbuf = malloc(total);
51 
52 	if (fread(gbuf, total, 1, stdin) != 1) {
53 		perror("glyph");
54 		return (1);
55 	}
56 
57 	for (j = 0; j < total; j += 12) {
58 		for (k = 0; k < 12 && k < total - j; k++) {
59 			printf(k == 0 ? "\n\t" : " ");
60 			printf("0x%02hhx,", gbuf[j + k]);
61 		}
62 	}
63 
64 	free(gbuf);
65 	printf("\n};\n");
66 
67 	return (0);
68 }
69 
70 static const char *map_names[4] = {
71     "normal", "normal_right", "bold", "bold_right" };
72 
73 static int
74 print_mappings(struct font_header *fh, int map_index)
75 {
76 	vfnt_map_t fm;
77 	unsigned int nmappings, i, col = 0;
78 
79 
80 	nmappings = be32toh(fh->fh_map_count[map_index]);
81 
82 	if (nmappings == 0)
83 		return (0);
84 
85 	printf("\nstatic vfnt_map_t font_mapping_%s[%u] = {",
86 	    map_names[map_index], nmappings);
87 
88 	for (i = 0; i < nmappings; i++) {
89 		if (fread(&fm, sizeof fm, 1, stdin) != 1) {
90 			perror("mapping");
91 			return (1);
92 		}
93 
94 		printf(col == 0 ? "\n\t" : " ");
95 		printf("{ 0x%04x, 0x%04x, 0x%02x },",
96 		    be32toh(fm.vfm_src), be16toh(fm.vfm_dst),
97 		    be16toh(fm.vfm_len));
98 		col = (col + 1) % 2;
99 	}
100 
101 	printf("\n};\n");
102 
103 	return (0);
104 }
105 
106 static int
107 print_info(struct font_header *fh)
108 {
109 	unsigned int i;
110 
111 	printf(
112 	    "\nstruct vt_font vt_font_default = {\n"
113 	    "\t.vf_width\t\t= %u,\n"
114 	    "\t.vf_height\t\t= %u,\n"
115 	    "\t.vf_bytes\t\t= font_bytes,\n",
116 	    fh->fh_width, fh->fh_height);
117 
118 	printf("\t.vf_map\t\t\t= {\n");
119 	for (i = 0; i < 4; i++) {
120 		if (fh->fh_map_count[i] > 0)
121 			printf("\t\t\t\t    font_mapping_%s,\n", map_names[i]);
122 		else
123 			printf("\t\t\t\t    NULL,\n");
124 	}
125 	printf("\t\t\t\t  },\n");
126 	printf("\t.vf_map_count\t\t= { %u, %u, %u, %u },\n",
127 	    be32toh(fh->fh_map_count[0]),
128 	    be32toh(fh->fh_map_count[1]),
129 	    be32toh(fh->fh_map_count[2]),
130 	    be32toh(fh->fh_map_count[3]));
131 	printf("\t.vf_refcount\t\t= 1,\n};\n");
132 
133 	return (0);
134 }
135 
136 int
137 main(int argc __unused, char *argv[] __unused)
138 {
139 	struct font_header fh;
140 	unsigned int i;
141 
142 	if (fread(&fh, sizeof fh, 1, stdin) != 1) {
143 		perror("font_header");
144 		return (1);
145 	}
146 
147 	if (memcmp(fh.fh_magic, "VFNT0002", 8) != 0) {
148 		fprintf(stderr, "Bad magic\n");
149 		return (1);
150 	}
151 
152 	printf("#include <dev/vt/vt.h>\n");
153 
154 	if (print_glyphs(&fh) != 0)
155 		return (1);
156 	for (i = 0; i < 4; i++)
157 		if (print_mappings(&fh, i) != 0)
158 			return (1);
159 	if (print_info(&fh) != 0)
160 		return (1);
161 
162 	return (0);
163 }
164