xref: /freebsd/tools/tools/vt/mkkfont/mkkfont.c (revision e17f5b1d)
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 #include <sys/font.h>
36 
37 #include <stdint.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 
42 static int
43 print_glyphs(struct font_header *fh)
44 {
45 	unsigned int gbytes, glyph_count, j, k, total;
46 	uint8_t *gbuf;
47 
48 	gbytes = howmany(fh->fh_width, 8) * fh->fh_height;
49 	glyph_count = be32toh(fh->fh_glyph_count);
50 
51 	printf("\nstatic uint8_t font_bytes[%u * %u] = {", glyph_count, gbytes);
52 	total = glyph_count * gbytes;
53 	gbuf = malloc(total);
54 
55 	if (fread(gbuf, total, 1, stdin) != 1) {
56 		perror("glyph");
57 		return (1);
58 	}
59 
60 	for (j = 0; j < total; j += 12) {
61 		for (k = 0; k < 12 && k < total - j; k++) {
62 			printf(k == 0 ? "\n\t" : " ");
63 			printf("0x%02hhx,", gbuf[j + k]);
64 		}
65 	}
66 
67 	free(gbuf);
68 	printf("\n};\n");
69 
70 	return (0);
71 }
72 
73 static const char *map_names[4] = {
74     "normal", "normal_right", "bold", "bold_right" };
75 
76 static int
77 print_mappings(struct font_header *fh, int map_index)
78 {
79 	vfnt_map_t fm;
80 	unsigned int nmappings, i, col = 0;
81 
82 
83 	nmappings = be32toh(fh->fh_map_count[map_index]);
84 
85 	if (nmappings == 0)
86 		return (0);
87 
88 	printf("\nstatic vfnt_map_t font_mapping_%s[%u] = {",
89 	    map_names[map_index], nmappings);
90 
91 	for (i = 0; i < nmappings; i++) {
92 		if (fread(&fm, sizeof fm, 1, stdin) != 1) {
93 			perror("mapping");
94 			return (1);
95 		}
96 
97 		printf(col == 0 ? "\n\t" : " ");
98 		printf("{ 0x%04x, 0x%04x, 0x%02x },",
99 		    be32toh(fm.vfm_src), be16toh(fm.vfm_dst),
100 		    be16toh(fm.vfm_len));
101 		col = (col + 1) % 2;
102 	}
103 
104 	printf("\n};\n");
105 
106 	return (0);
107 }
108 
109 static int
110 print_info(struct font_header *fh)
111 {
112 	unsigned int i;
113 
114 	printf(
115 	    "\nstruct vt_font vt_font_default = {\n"
116 	    "\t.vf_width\t\t= %u,\n"
117 	    "\t.vf_height\t\t= %u,\n"
118 	    "\t.vf_bytes\t\t= font_bytes,\n",
119 	    fh->fh_width, fh->fh_height);
120 
121 	printf("\t.vf_map\t\t\t= {\n");
122 	for (i = 0; i < 4; i++) {
123 		if (fh->fh_map_count[i] > 0)
124 			printf("\t\t\t\t    font_mapping_%s,\n", map_names[i]);
125 		else
126 			printf("\t\t\t\t    NULL,\n");
127 	}
128 	printf("\t\t\t\t  },\n");
129 	printf("\t.vf_map_count\t\t= { %u, %u, %u, %u },\n",
130 	    be32toh(fh->fh_map_count[0]),
131 	    be32toh(fh->fh_map_count[1]),
132 	    be32toh(fh->fh_map_count[2]),
133 	    be32toh(fh->fh_map_count[3]));
134 	printf("\t.vf_refcount\t\t= 1,\n};\n");
135 
136 	return (0);
137 }
138 
139 int
140 main(int argc __unused, char *argv[] __unused)
141 {
142 	struct font_header fh;
143 	unsigned int i;
144 
145 	if (fread(&fh, sizeof fh, 1, stdin) != 1) {
146 		perror("font_header");
147 		return (1);
148 	}
149 
150 	if (memcmp(fh.fh_magic, "VFNT0002", 8) != 0) {
151 		fprintf(stderr, "Bad magic\n");
152 		return (1);
153 	}
154 
155 	printf("#include <dev/vt/vt.h>\n");
156 
157 	if (print_glyphs(&fh) != 0)
158 		return (1);
159 	for (i = 0; i < 4; i++)
160 		if (print_mappings(&fh, i) != 0)
161 			return (1);
162 	if (print_info(&fh) != 0)
163 		return (1);
164 
165 	return (0);
166 }
167