bdfload.c revision 61348eb3
1/*	$NetBSD: bdfload.c,v 1.4 2022/08/16 20:27:33 macallan Exp $	*/
2
3/*
4 * Copyright (c) 2018 Michael Lorenz
5 * All rights reserved.
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 ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28/*
29 * a crude BDF loader for wsdisplay
30 */
31
32#include <stdlib.h>
33#include <stdio.h>
34#include <fcntl.h>
35#include <unistd.h>
36#include <string.h>
37#include <errno.h>
38#include <ctype.h>
39#include <sys/ioctl.h>
40#include <err.h>
41
42#include <dev/wscons/wsconsio.h>
43
44/*
45 * wsdisplay_font but with strings embedded and integer fields in
46 * little endian
47 */
48struct wsfthdr {
49	char magic[4];		/* "WSFT" */
50	char name[64];
51	uint32_t firstchar;
52	uint32_t numchars;
53	uint32_t encoding;
54	uint32_t fontwidth;
55	uint32_t fontheight;
56	uint32_t stride;
57	uint32_t bitorder;
58	uint32_t byteorder;
59};
60
61
62const struct encmap {
63	const char *name;
64	int encoding;
65} encmap[] = {
66	{ "cp437",	WSDISPLAY_FONTENC_IBM },
67	{ "ibm",	WSDISPLAY_FONTENC_IBM },
68	{ "iso",	WSDISPLAY_FONTENC_ISO },
69	{ "iso-8859-1",	WSDISPLAY_FONTENC_ISO },
70	{ "iso-8859-2",	WSDISPLAY_FONTENC_ISO2 },
71	{ "iso-8859-7",	WSDISPLAY_FONTENC_ISO7 },
72	{ "iso2",	WSDISPLAY_FONTENC_ISO2 },
73	{ "iso7",	WSDISPLAY_FONTENC_ISO7 },
74	{ "iso8859-1",	WSDISPLAY_FONTENC_ISO },
75	{ "iso8859-2",	WSDISPLAY_FONTENC_ISO2 },
76	{ "iso8859-7",	WSDISPLAY_FONTENC_ISO7 },
77	{ "koi8-r",	WSDISPLAY_FONTENC_KOI8_R },
78	{ "koi8r",	WSDISPLAY_FONTENC_KOI8_R },
79	{ "latin-1",	WSDISPLAY_FONTENC_ISO },
80	{ "latin-2",	WSDISPLAY_FONTENC_ISO2 },
81	{ "latin1",	WSDISPLAY_FONTENC_ISO },
82	{ "latin2",	WSDISPLAY_FONTENC_ISO2 },
83	{ "pcvt",	WSDISPLAY_FONTENC_PCVT },
84	{ NULL, -1 }
85};
86
87const char * const encname[] = {
88#define _ENC(_e) [_e] = #_e
89	_ENC(WSDISPLAY_FONTENC_ISO),
90	_ENC(WSDISPLAY_FONTENC_IBM),
91	_ENC(WSDISPLAY_FONTENC_PCVT),
92	_ENC(WSDISPLAY_FONTENC_ISO7),
93	_ENC(WSDISPLAY_FONTENC_ISO2),
94	_ENC(WSDISPLAY_FONTENC_KOI8_R),
95};
96
97
98const char *ofile = NULL;
99int encoding = -1;
100int verbose = 0;
101
102
103void
104interpret(FILE *foo)
105{
106	char line[128], *arg, name[64] = "foop", *buffer;
107	int buflen = -1;
108	int len, in_char = 0, current = -1, stride = 0, charsize = 0;
109	int width, height, x, y, num;
110	int first = 255, last = 0;
111	int left, top, lines;
112	int bl = 255, bt = 255, br = -1, bb = -1;
113	struct wsdisplay_font f;
114	int status;
115
116	while (fgets(line, sizeof(line), foo) != NULL) {
117		int i = 0;
118		/* separate keyword from parameters */
119		len = strlen(line);
120		while (!isspace(line[i]) && (i < len)) i++;
121		line[i] = 0;
122		arg = &line[i + 1];
123		i = 0;
124		len = strlen(arg);
125		/* get rid of garbage */
126		while ((!iscntrl(arg[i])) && (arg[i] != 0)) {
127			i++;
128		}
129		arg[i] = 0;
130		if (strcmp(line, "FAMILY_NAME") == 0) {
131			/* cut off quotation marks */
132			strncpy(name, arg + 1, 64);
133			name[strlen(name) - 1] = 0;
134			if (verbose) printf("name: %s\n", name);
135		} else if (strcmp(line, "FONTBOUNDINGBOX") == 0) {
136			int res;
137			res = sscanf(arg, "%d %d %d %d",
138					  &width, &height, &x, &y);
139			stride = (width + 7) >> 3;
140			if (verbose) printf("box %d x %d\n", width, height);
141			if (stride > 2) {
142				err(EXIT_FAILURE,
143				    "no fonts wider than 16 work for now\n");
144			}
145			charsize = height * stride;
146			buflen = 256 * charsize;
147			buffer = calloc(1, buflen);
148			if (buffer == NULL) {
149				err(EXIT_FAILURE,
150				    "failed to allocate %dKB for glyphs\n",
151				    buflen);
152			}
153		} else if (strcmp(line, "CHARS") == 0) {
154			if (sscanf(arg, "%d", &num) == 1)
155				if (verbose)
156				    printf("number of characters: %d\n", num);
157		} else if (strcmp(line, "STARTCHAR") == 0) {
158			in_char = 1;
159		} else if (strcmp(line, "ENDCHAR") == 0) {
160			in_char = 0;
161			current = -1;
162		} else if (strcmp(line, "ENCODING") == 0) {
163			if (sscanf(arg, "%d", &current) == 1) {
164				if (current >= 0 && current < 256) {
165					if (current < first) first = current;
166					if (current > last) last = current;
167				}
168			}
169		} else if (strcmp(line, "BBX") == 0) {
170			int cx, cy, cwi, che;
171			if (sscanf(arg, "%d %d %d %d", &cwi, &che, &cx, &cy)
172			     == 4) {
173				left = cx;
174				lines = che;
175				top = height + y - che - cy;
176				if (left < bl) bl = left;
177				if (top < bt) bt = top;
178				if ((left + cwi) > br) br = left + cwi;
179				if ((top + che) > bb) bb = top + che;
180			}
181		} else if (strcmp(line, "BITMAP") == 0) {
182			int i, j, k, l;
183			char num[32];
184			char *gptr = &buffer[charsize * current];
185			char *bptr = gptr + top;
186			uint16_t *bptr16 = (uint16_t *)gptr;
187			bptr16 += top;
188			/* see if the character is in range */
189			if ((current < 0) || (current > 255)) continue;
190			/* now we read & render the character */
191			for (i = 0; i < lines; i++) {
192				fgets(num, 32, foo);
193				sscanf(num, "%x", &l);
194				if ((stride) == 2 && (strlen(num) < 4))
195					l = l << 8;
196				l = l >> left;
197				if (stride == 1) {
198					*gptr = l;
199					gptr++;
200				} else {
201					*bptr16 = htobe16(l);
202					bptr16++;
203				}
204			}
205		}
206	}
207	if (verbose) {
208		printf("range %d to %d\n", first, last);
209		printf("encoding: %s\n", encname[encoding]);
210		printf("actual box: %d %d %d %d\n", bl, bt, br, bb);
211	}
212
213	/* now stuff it into a something wsfont understands */
214	f.fontwidth = width /*(width + 3) & ~3*/;
215	f.fontheight = height;
216	f.firstchar = first;
217	f.numchars = last - first + 1;
218	f.stride = stride;
219	f.encoding = encoding;
220	f.name = name;
221	f.bitorder = WSDISPLAY_FONTORDER_L2R;
222	f.byteorder = WSDISPLAY_FONTORDER_L2R;
223	f.data = &buffer[first * charsize];
224
225	if (ofile == NULL) {
226		int fdev = open("/dev/wsfont", O_RDWR, 0);
227		if (fdev < 0)
228			err(EXIT_FAILURE, "/dev/wsfont");
229		status = ioctl(fdev, WSDISPLAYIO_LDFONT, &f);
230		if (status != 0)
231			err(EXIT_FAILURE, "WSDISPLAYIO_LDFONT");
232		close(fdev);
233	}
234	else {
235		struct wsfthdr h;
236
237		memset(&h, 0, sizeof(h));
238		strncpy(h.magic, "WSFT", sizeof(h.magic));
239		strncpy(h.name, f.name, sizeof(h.name));
240		h.firstchar = htole32(f.firstchar);
241		h.numchars = htole32(f.numchars);
242		h.encoding = htole32(f.encoding);
243		h.fontwidth = htole32(f.fontwidth);
244		h.fontheight = htole32(f.fontheight);
245		h.stride = htole32(f.stride);
246		h.bitorder = htole32(f.bitorder);
247		h.byteorder = htole32(f.byteorder);
248
249		int wsfd = open(ofile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
250		if (wsfd < 0)
251			err(EXIT_FAILURE, "%s", ofile);
252
253		ssize_t nwritten;
254		nwritten = write(wsfd, &h, sizeof(h));
255		if (nwritten < 0)
256			err(EXIT_FAILURE, "%s", ofile);
257		if (nwritten != sizeof(h))
258			errx(EXIT_FAILURE, "%s: partial write", ofile);
259
260		nwritten = write(wsfd, buffer, buflen);
261		if (nwritten < 0)
262			err(EXIT_FAILURE, "%s", ofile);
263		if (nwritten != buflen)
264			errx(EXIT_FAILURE, "%s: partial write", ofile);
265		close(wsfd);
266	}
267}
268
269
270__dead void
271usage()
272{
273	fprintf(stderr, "usage: bdfload [-v] [-e encoding] [-o ofile.wsf] font.bdf\n");
274	exit(EXIT_FAILURE);
275}
276
277int
278main(int argc, char *argv[])
279{
280	FILE *foo;
281	const char *encname = NULL;
282
283	int c;
284	while ((c = getopt(argc, argv, "e:o:v")) != -1) {
285		switch (c) {
286
287		/* font encoding */
288		case 'e':
289			if (encname != NULL)
290				usage();
291			encname = optarg;
292			break;
293
294		/* output file name */
295		case 'o':
296			if (ofile != NULL)
297				usage();
298			ofile = optarg;
299			break;
300
301		case 'v':
302			verbose = 1;
303			break;
304
305		case '?':	/* FALLTHROUGH */
306		default:
307			usage();
308		}
309	}
310
311	argc -= optind;
312	argv += optind;
313
314	if (encname == NULL) {
315		encoding = WSDISPLAY_FONTENC_ISO;
316	}
317	else {
318		for (const struct encmap *e = encmap; e->name; ++e) {
319			if (strcmp(e->name, encname) == 0) {
320				encoding = e->encoding;
321				break;
322			}
323		}
324	}
325
326	/* get encoding from the bdf file? */
327	if (encoding == -1)
328		encoding = WSDISPLAY_FONTENC_ISO;
329
330	if (argc == 0)
331		usage();
332
333	const char *bdfname = argv[0];
334	foo = fopen(bdfname, "r");
335	if (foo == NULL)
336		err(EXIT_FAILURE, "%s", bdfname);
337
338	interpret(foo);
339	return EXIT_SUCCESS;
340}
341