bdfload.c revision 7dd7205f
1/*	$NetBSD: bdfload.c,v 1.11 2022/08/23 19:09:15 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;
101int dump = 0;
102int header = 0;
103char commentbuf[2048] = "";
104int commentptr = 0;
105
106void
107dump_line(char *gptr, int stride)
108{
109	int i, j, msk, c;
110
111	for (i = 0; i < stride; i++) {
112		c = gptr[i];
113		msk = 0x80;
114		for (j = 0; j < 8; j++) {
115			putchar((c & msk) != 0 ? '#' : ' ');
116			msk = msk >> 1;
117		}
118	}
119	printf("\n");
120}
121
122void
123write_wsf(const char *oname, struct wsdisplay_font *f, char *buffer, int buflen)
124{
125	struct wsfthdr h;
126
127	memset(&h, 0, sizeof(h));
128	strncpy(h.magic, "WSFT", sizeof(h.magic));
129	strncpy(h.name, f->name, sizeof(h.name));
130	h.firstchar = htole32(f->firstchar);
131	h.numchars = htole32(f->numchars);
132	h.encoding = htole32(f->encoding);
133	h.fontwidth = htole32(f->fontwidth);
134	h.fontheight = htole32(f->fontheight);
135	h.stride = htole32(f->stride);
136	h.bitorder = htole32(f->bitorder);
137	h.byteorder = htole32(f->byteorder);
138
139	int wsfd = open(ofile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
140	if (wsfd < 0)
141		err(EXIT_FAILURE, "%s", ofile);
142
143	ssize_t nwritten;
144	nwritten = write(wsfd, &h, sizeof(h));
145	if (nwritten < 0)
146		err(EXIT_FAILURE, "%s", ofile);
147	if (nwritten != sizeof(h))
148		errx(EXIT_FAILURE, "%s: partial write", ofile);
149
150	nwritten = write(wsfd, buffer, buflen);
151	if (nwritten < 0)
152		err(EXIT_FAILURE, "%s", ofile);
153	if (nwritten != buflen)
154		errx(EXIT_FAILURE, "%s: partial write", ofile);
155	close(wsfd);
156}
157
158int
159write_header(const char *filename, struct wsdisplay_font *f, char *name,
160             char *buffer, int buflen)
161{
162	FILE *output;
163	int i, j, x, y, idx, pxls, left;
164	char fontname[64], c, msk;
165
166	/* now output as a header file */
167	snprintf(fontname, sizeof(fontname), "%s_%dx%d", name,
168	    f->fontwidth, f->fontheight);
169	for (i = 0; i < strlen(fontname); i++) {
170		if (isblank((int)fontname[i]))
171			fontname[i]='_';
172	}
173	if ((output = fopen(filename, "w")) == NULL) {
174		fprintf(stderr, "Can't open output file %s\n", filename);
175		return -1;
176	}
177	if (commentptr > 0) {
178		fprintf(output, "/*\n");
179		fputs(commentbuf, output);
180		fprintf(output, "*/\n\n");
181	}
182
183	fprintf(output, "static u_char %s_data[];\n", fontname);
184	fprintf(output, "\n");
185	fprintf(output, "static struct wsdisplay_font %s = {\n", fontname);
186	fprintf(output, "\t\"%s\",\t\t\t/* typeface name */\n", name);
187	fprintf(output, "\t%d,\t\t\t\t/* firstchar */\n", f->firstchar);
188	fprintf(output, "\t%d,\t\t\t\t/* numchars */\n", f->numchars);
189	fprintf(output, "\t%d,\t\t\t\t/* encoding */\n", f->encoding);
190	fprintf(output, "\t%d,\t\t\t\t/* fontwidth */\n", f->fontwidth);
191	fprintf(output, "\t%d,\t\t\t\t/* fontheight */\n", f->fontheight);
192	fprintf(output, "\t%d,\t\t\t\t/* stride */\n", f->stride);
193	fprintf(output, "\tWSDISPLAY_FONTORDER_L2R,\t/* bit order */\n");
194	fprintf(output, "\tWSDISPLAY_FONTORDER_L2R,\t/* byte order */\n");
195	fprintf(output, "\t%s_data\t\t/* data */\n", fontname);
196	fprintf(output, "};\n\n");
197	fprintf(output, "static u_char %s_data[] = {\n", fontname);
198	for (i = f->firstchar; i < f->firstchar + f->numchars; i++) {
199		fprintf(output, "\t/* %d */\n", i);
200		idx = i * f->stride * f->fontheight;
201		for (y = 0; y < f->fontheight; y++) {
202			for (x = 0; x < f->stride; x++) {
203				fprintf(output, "0x%02x, ",buffer[idx + x]);
204			}
205			fprintf(output, "/* ");
206			pxls = f->fontwidth;
207			for (x = 0; x < f->stride; x++) {
208				c = buffer[idx + x];
209				msk = 0x80;
210				left = pxls > 8 ? 8 : pxls;
211				for (j = 0; j < left; j++) {
212					fprintf(output, "%s",
213					    (c & msk) != 0 ? "[]" : ". ");
214					msk = msk >> 1;
215				}
216				pxls -= 8;
217			}
218			fprintf(output, " */\n");
219
220			idx += f->stride;
221		}
222	}
223	fprintf(output, "};\n");
224	fclose(output);
225	return 0;
226}
227
228void
229interpret(FILE *foo)
230{
231	char line[128], *arg, name[64] = "foop", *buffer;
232	int buflen = -1;
233	int len, in_char = 0, current = -1, stride = 0, charsize = 0;
234	int width, height, x, y, num;
235	int first = 255, last = 0;
236	int left, top, lines;
237	int bl = 255, bt = 255, br = -1, bb = -1;
238	struct wsdisplay_font f;
239	int status;
240
241	while (fgets(line, sizeof(line), foo) != NULL) {
242		int i = 0;
243		/* separate keyword from parameters */
244		len = strlen(line);
245		while (!isspace(line[i]) && (i < len)) i++;
246		line[i] = 0;
247		arg = &line[i + 1];
248		i = 0;
249		len = strlen(arg);
250		/* get rid of garbage */
251		while ((!iscntrl(arg[i])) && (arg[i] != 0)) {
252			i++;
253		}
254		arg[i] = 0;
255		if (strcmp(line, "FAMILY_NAME") == 0) {
256			/* cut off quotation marks */
257			strncpy(name, arg + 1, 64);
258			name[strlen(name) - 1] = 0;
259			if (verbose) printf("name: %s\n", name);
260		} else if (strcmp(line, "COMMENT") == 0) {
261			commentptr += snprintf(&commentbuf[commentptr],
262					      2048 - commentptr,
263					      "%s\n", arg);
264		} else if (strcmp(line, "FONTBOUNDINGBOX") == 0) {
265			int res;
266			res = sscanf(arg, "%d %d %d %d",
267					  &width, &height, &x, &y);
268			stride = (width + 7) >> 3;
269			if (verbose) printf("box %d x %d\n", width, height);
270			if (stride > 2) {
271				err(EXIT_FAILURE,
272				    "no fonts wider than 16 work for now\n");
273			}
274			charsize = height * stride;
275			buflen = 256 * charsize;
276			buffer = calloc(1, buflen);
277			if (buffer == NULL) {
278				err(EXIT_FAILURE,
279				    "failed to allocate %dKB for glyphs\n",
280				    buflen);
281			}
282		} else if (strcmp(line, "CHARS") == 0) {
283			if (sscanf(arg, "%d", &num) == 1)
284				if (verbose)
285				    printf("number of characters: %d\n", num);
286		} else if (strcmp(line, "STARTCHAR") == 0) {
287			in_char = 1;
288		} else if (strcmp(line, "ENDCHAR") == 0) {
289			in_char = 0;
290			current = -1;
291		} else if (strcmp(line, "ENCODING") == 0) {
292			if (sscanf(arg, "%d", &current) == 1) {
293				if (current >= 0 && current < 256) {
294					if (current < first) first = current;
295					if (current > last) last = current;
296					if (dump) printf("glyph %d\n", current);
297				}
298			}
299		} else if (strcmp(line, "BBX") == 0) {
300			int cx, cy, cwi, che;
301			if (sscanf(arg, "%d %d %d %d", &cwi, &che, &cx, &cy)
302			     == 4) {
303				left = cx;
304				lines = che;
305				top = height + y - che - cy;
306				if (left < bl) bl = left;
307				if (top < bt) bt = top;
308				if ((left + cwi) > br) br = left + cwi;
309				if ((top + che) > bb) bb = top + che;
310				if(dump && verbose) printf("top %d left %d\n", top, left);
311			}
312		} else if (strcmp(line, "BITMAP") == 0) {
313			int i, j, k, l;
314			char num[32];
315			char *gptr = &buffer[charsize * current];
316			char *bptr = gptr + top;
317			uint16_t *bptr16 = (uint16_t *)gptr;
318			bptr16 += top;
319			/* see if the character is in range */
320			if ((current < 0) || (current > 255)) continue;
321			/* now we read & render the character */
322			for (i = 0; i < lines; i++) {
323				fgets(num, 32, foo);
324				sscanf(num, "%x", &l);
325				if ((stride) == 2 && (strlen(num) < 4))
326					l = l << 8;
327				l = l >> left;
328				if (stride == 1) {
329					*bptr = l;
330					bptr++;
331				} else {
332					*bptr16 = htobe16(l);
333					bptr16++;
334				}
335			}
336			if (dump) {
337				gptr = &buffer[charsize * current];
338				for (i = 0; i < height; i++) {
339					dump_line(gptr, stride);
340					gptr += stride;
341				}
342			}
343		}
344	}
345	if (verbose) {
346		printf("range %d to %d\n", first, last);
347		printf("encoding: %s\n", encname[encoding]);
348		printf("actual box: %d %d %d %d\n", bl, bt, br, bb);
349	}
350
351	/* now stuff it into a something wsfont understands */
352	f.fontwidth = width /*(width + 3) & ~3*/;
353	f.fontheight = height;
354	f.firstchar = first;
355	f.numchars = last - first + 1;
356	f.stride = stride;
357	f.encoding = encoding;
358	f.name = name;
359	f.bitorder = WSDISPLAY_FONTORDER_L2R;
360	f.byteorder = WSDISPLAY_FONTORDER_L2R;
361	f.data = &buffer[first * charsize];
362
363	if (ofile == NULL) {
364		int fdev = open("/dev/wsfont", O_RDWR, 0);
365		if (fdev < 0)
366			err(EXIT_FAILURE, "/dev/wsfont");
367		status = ioctl(fdev, WSDISPLAYIO_LDFONT, &f);
368		if (status != 0)
369			err(EXIT_FAILURE, "WSDISPLAYIO_LDFONT");
370		close(fdev);
371	}
372	else {
373		if (header == 0) {
374			write_wsf(ofile, &f, buffer, buflen);
375		} else
376			write_header(ofile, &f, name, buffer, buflen);
377	}
378}
379
380__dead void
381usage()
382{
383	fprintf(stderr, "usage: bdfload [-vdh] [-e encoding] [-o ofile.wsf] font.bdf\n");
384	exit(EXIT_FAILURE);
385}
386
387int
388main(int argc, char *argv[])
389{
390	FILE *foo;
391	const char *encname = NULL;
392
393	int c;
394	while ((c = getopt(argc, argv, "e:o:vdh")) != -1) {
395		switch (c) {
396
397		/* font encoding */
398		case 'e':
399			if (encname != NULL)
400				usage();
401			encname = optarg;
402			break;
403
404		/* output file name */
405		case 'o':
406			if (ofile != NULL)
407				usage();
408			ofile = optarg;
409			break;
410
411		case 'v':
412			verbose = 1;
413			break;
414
415		case 'd':
416			dump = 1;
417			break;
418
419		case 'h':
420			header = 1;
421			break;
422
423		case '?':	/* FALLTHROUGH */
424		default:
425			usage();
426		}
427	}
428
429	argc -= optind;
430	argv += optind;
431
432	if (encname == NULL) {
433		encoding = WSDISPLAY_FONTENC_ISO;
434	}
435	else {
436		for (const struct encmap *e = encmap; e->name; ++e) {
437			if (strcmp(e->name, encname) == 0) {
438				encoding = e->encoding;
439				break;
440			}
441		}
442	}
443
444	/* get encoding from the bdf file? */
445	if (encoding == -1)
446		encoding = WSDISPLAY_FONTENC_ISO;
447
448	if (argc == 0)
449		usage();
450
451	const char *bdfname = argv[0];
452	foo = fopen(bdfname, "r");
453	if (foo == NULL)
454		err(EXIT_FAILURE, "%s", bdfname);
455
456	interpret(foo);
457	return EXIT_SUCCESS;
458}
459