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