bdfload.c revision 6c735b96
16c735b96Schristos/*	$NetBSD: bdfload.c,v 1.16 2022/09/27 18:29:08 christos Exp $	*/
2d5f9d0dbSmacallan
3d5f9d0dbSmacallan/*
4d5f9d0dbSmacallan * Copyright (c) 2018 Michael Lorenz
5d5f9d0dbSmacallan * All rights reserved.
6d5f9d0dbSmacallan *
7d5f9d0dbSmacallan * Redistribution and use in source and binary forms, with or without
8d5f9d0dbSmacallan * modification, are permitted provided that the following conditions
9d5f9d0dbSmacallan * are met:
10d5f9d0dbSmacallan * 1. Redistributions of source code must retain the above copyright
11d5f9d0dbSmacallan *    notice, this list of conditions and the following disclaimer.
12d5f9d0dbSmacallan * 2. Redistributions in binary form must reproduce the above copyright
13d5f9d0dbSmacallan *    notice, this list of conditions and the following disclaimer in the
14d5f9d0dbSmacallan *    documentation and/or other materials provided with the distribution.
15d5f9d0dbSmacallan *
16d5f9d0dbSmacallan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17d5f9d0dbSmacallan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18d5f9d0dbSmacallan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19d5f9d0dbSmacallan * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20d5f9d0dbSmacallan * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21d5f9d0dbSmacallan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22d5f9d0dbSmacallan * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23d5f9d0dbSmacallan * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24d5f9d0dbSmacallan * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25d5f9d0dbSmacallan * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26d5f9d0dbSmacallan */
27d5f9d0dbSmacallan
28d5f9d0dbSmacallan/*
29d5f9d0dbSmacallan * a crude BDF loader for wsdisplay
30d5f9d0dbSmacallan */
31d5f9d0dbSmacallan
32d5f9d0dbSmacallan#include <stdlib.h>
33d5f9d0dbSmacallan#include <stdio.h>
34d5f9d0dbSmacallan#include <fcntl.h>
35d5f9d0dbSmacallan#include <unistd.h>
36d5f9d0dbSmacallan#include <string.h>
37d5f9d0dbSmacallan#include <errno.h>
38d5f9d0dbSmacallan#include <ctype.h>
39d5f9d0dbSmacallan#include <sys/ioctl.h>
40d5f9d0dbSmacallan#include <err.h>
41d5f9d0dbSmacallan
42d5f9d0dbSmacallan#include <dev/wscons/wsconsio.h>
43d5f9d0dbSmacallan
442b154882Suwe/*
452b154882Suwe * wsdisplay_font but with strings embedded and integer fields in
462b154882Suwe * little endian
472b154882Suwe */
482b154882Suwestruct wsfthdr {
492b154882Suwe	char magic[4];		/* "WSFT" */
502b154882Suwe	char name[64];
512b154882Suwe	uint32_t firstchar;
522b154882Suwe	uint32_t numchars;
532b154882Suwe	uint32_t encoding;
542b154882Suwe	uint32_t fontwidth;
552b154882Suwe	uint32_t fontheight;
562b154882Suwe	uint32_t stride;
572b154882Suwe	uint32_t bitorder;
582b154882Suwe	uint32_t byteorder;
592b154882Suwe};
602b154882Suwe
612b154882Suwe
622b154882Suweconst struct encmap {
632b154882Suwe	const char *name;
642b154882Suwe	int encoding;
652b154882Suwe} encmap[] = {
662b154882Suwe	{ "cp437",	WSDISPLAY_FONTENC_IBM },
672b154882Suwe	{ "ibm",	WSDISPLAY_FONTENC_IBM },
682b154882Suwe	{ "iso",	WSDISPLAY_FONTENC_ISO },
69a5a7ac3fSmacallan	{ "iso8859",	WSDISPLAY_FONTENC_ISO },
70a5a7ac3fSmacallan	{ "iso10646",	WSDISPLAY_FONTENC_ISO },
712b154882Suwe	{ "iso-8859-1",	WSDISPLAY_FONTENC_ISO },
722b154882Suwe	{ "iso-8859-2",	WSDISPLAY_FONTENC_ISO2 },
732b154882Suwe	{ "iso-8859-7",	WSDISPLAY_FONTENC_ISO7 },
742b154882Suwe	{ "iso2",	WSDISPLAY_FONTENC_ISO2 },
752b154882Suwe	{ "iso7",	WSDISPLAY_FONTENC_ISO7 },
762b154882Suwe	{ "iso8859-1",	WSDISPLAY_FONTENC_ISO },
772b154882Suwe	{ "iso8859-2",	WSDISPLAY_FONTENC_ISO2 },
782b154882Suwe	{ "iso8859-7",	WSDISPLAY_FONTENC_ISO7 },
792b154882Suwe	{ "koi8-r",	WSDISPLAY_FONTENC_KOI8_R },
802b154882Suwe	{ "koi8r",	WSDISPLAY_FONTENC_KOI8_R },
812b154882Suwe	{ "latin-1",	WSDISPLAY_FONTENC_ISO },
822b154882Suwe	{ "latin-2",	WSDISPLAY_FONTENC_ISO2 },
832b154882Suwe	{ "latin1",	WSDISPLAY_FONTENC_ISO },
842b154882Suwe	{ "latin2",	WSDISPLAY_FONTENC_ISO2 },
852b154882Suwe	{ "pcvt",	WSDISPLAY_FONTENC_PCVT },
862b154882Suwe	{ NULL, -1 }
872b154882Suwe};
882b154882Suwe
892b154882Suweconst char * const encname[] = {
902b154882Suwe#define _ENC(_e) [_e] = #_e
912b154882Suwe	_ENC(WSDISPLAY_FONTENC_ISO),
922b154882Suwe	_ENC(WSDISPLAY_FONTENC_IBM),
932b154882Suwe	_ENC(WSDISPLAY_FONTENC_PCVT),
942b154882Suwe	_ENC(WSDISPLAY_FONTENC_ISO7),
952b154882Suwe	_ENC(WSDISPLAY_FONTENC_ISO2),
962b154882Suwe	_ENC(WSDISPLAY_FONTENC_KOI8_R),
972b154882Suwe};
982b154882Suwe
992b154882Suwe
1002b154882Suweconst char *ofile = NULL;
1012b154882Suweint encoding = -1;
10261348eb3Smacallanint verbose = 0;
103554a5d78Smacallanint dump = 0;
1042d60533cSmacallanint header = 0;
105a5a7ac3fSmacallanint force = 0;
1067d6aaba4Smacallanchar commentbuf[2048] = "";
1077d6aaba4Smacallanint commentptr = 0;
10851bbcd86Smacallanchar fontname[64] = "";
1092b154882Suwe
110554a5d78Smacallanvoid
111554a5d78Smacallandump_line(char *gptr, int stride)
112554a5d78Smacallan{
113554a5d78Smacallan	int i, j, msk, c;
114554a5d78Smacallan
115554a5d78Smacallan	for (i = 0; i < stride; i++) {
116554a5d78Smacallan		c = gptr[i];
117554a5d78Smacallan		msk = 0x80;
118554a5d78Smacallan		for (j = 0; j < 8; j++) {
119554a5d78Smacallan			putchar((c & msk) != 0 ? '#' : ' ');
120554a5d78Smacallan			msk = msk >> 1;
121554a5d78Smacallan		}
122554a5d78Smacallan	}
123ec211365Smacallan	printf("\n");
124554a5d78Smacallan}
125554a5d78Smacallan
1262d60533cSmacallanvoid
1272d60533cSmacallanwrite_wsf(const char *oname, struct wsdisplay_font *f, char *buffer, int buflen)
1282d60533cSmacallan{
1292d60533cSmacallan	struct wsfthdr h;
1302d60533cSmacallan
1312d60533cSmacallan	memset(&h, 0, sizeof(h));
1322d60533cSmacallan	strncpy(h.magic, "WSFT", sizeof(h.magic));
1332d60533cSmacallan	strncpy(h.name, f->name, sizeof(h.name));
1342d60533cSmacallan	h.firstchar = htole32(f->firstchar);
1352d60533cSmacallan	h.numchars = htole32(f->numchars);
1362d60533cSmacallan	h.encoding = htole32(f->encoding);
1372d60533cSmacallan	h.fontwidth = htole32(f->fontwidth);
1382d60533cSmacallan	h.fontheight = htole32(f->fontheight);
1392d60533cSmacallan	h.stride = htole32(f->stride);
1402d60533cSmacallan	h.bitorder = htole32(f->bitorder);
1412d60533cSmacallan	h.byteorder = htole32(f->byteorder);
1422d60533cSmacallan
1432d60533cSmacallan	int wsfd = open(ofile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1442d60533cSmacallan	if (wsfd < 0)
1452d60533cSmacallan		err(EXIT_FAILURE, "%s", ofile);
1462d60533cSmacallan
1472d60533cSmacallan	ssize_t nwritten;
1482d60533cSmacallan	nwritten = write(wsfd, &h, sizeof(h));
1492d60533cSmacallan	if (nwritten < 0)
1502d60533cSmacallan		err(EXIT_FAILURE, "%s", ofile);
1512d60533cSmacallan	if (nwritten != sizeof(h))
1522d60533cSmacallan		errx(EXIT_FAILURE, "%s: partial write", ofile);
1532d60533cSmacallan
1542d60533cSmacallan	nwritten = write(wsfd, buffer, buflen);
1552d60533cSmacallan	if (nwritten < 0)
1562d60533cSmacallan		err(EXIT_FAILURE, "%s", ofile);
1572d60533cSmacallan	if (nwritten != buflen)
1582d60533cSmacallan		errx(EXIT_FAILURE, "%s: partial write", ofile);
1592d60533cSmacallan	close(wsfd);
1602d60533cSmacallan}
1612d60533cSmacallan
1622d60533cSmacallanint
16351bbcd86Smacallanwrite_header(const char *filename, struct wsdisplay_font *f,
1642d60533cSmacallan             char *buffer, int buflen)
1652d60533cSmacallan{
1662d60533cSmacallan	FILE *output;
1677dd7205fSmacallan	int i, j, x, y, idx, pxls, left;
16843d302b1Smacallan	char name[64], c, msk;
1692d60533cSmacallan
1702d60533cSmacallan	/* now output as a header file */
17143d302b1Smacallan	snprintf(name, sizeof(name), "%s_%dx%d", f->name,
1722d60533cSmacallan	    f->fontwidth, f->fontheight);
17343d302b1Smacallan	for (i = 0; i < strlen(name); i++) {
1746c735b96Schristos		if (isblank((unsigned char)name[i]))
1756c735b96Schristos			name[i] = '_';
1762d60533cSmacallan	}
1772d60533cSmacallan	if ((output = fopen(filename, "w")) == NULL) {
1786c735b96Schristos		warn("Can't open output file `%s'", filename);
1792d60533cSmacallan		return -1;
1802d60533cSmacallan	}
1817d6aaba4Smacallan	if (commentptr > 0) {
1827d6aaba4Smacallan		fprintf(output, "/*\n");
1837d6aaba4Smacallan		fputs(commentbuf, output);
1847d6aaba4Smacallan		fprintf(output, "*/\n\n");
1857d6aaba4Smacallan	}
1867d6aaba4Smacallan
18743d302b1Smacallan	fprintf(output, "static u_char %s_data[];\n", name);
1882d60533cSmacallan	fprintf(output, "\n");
18943d302b1Smacallan	fprintf(output, "static struct wsdisplay_font %s = {\n", name);
19051bbcd86Smacallan	fprintf(output, "\t\"%s\",\t\t\t/* typeface name */\n", f->name);
1912d60533cSmacallan	fprintf(output, "\t%d,\t\t\t\t/* firstchar */\n", f->firstchar);
1922d60533cSmacallan	fprintf(output, "\t%d,\t\t\t\t/* numchars */\n", f->numchars);
1932d60533cSmacallan	fprintf(output, "\t%d,\t\t\t\t/* encoding */\n", f->encoding);
1942d60533cSmacallan	fprintf(output, "\t%d,\t\t\t\t/* fontwidth */\n", f->fontwidth);
1952d60533cSmacallan	fprintf(output, "\t%d,\t\t\t\t/* fontheight */\n", f->fontheight);
1962d60533cSmacallan	fprintf(output, "\t%d,\t\t\t\t/* stride */\n", f->stride);
1972d60533cSmacallan	fprintf(output, "\tWSDISPLAY_FONTORDER_L2R,\t/* bit order */\n");
1982d60533cSmacallan	fprintf(output, "\tWSDISPLAY_FONTORDER_L2R,\t/* byte order */\n");
19943d302b1Smacallan	fprintf(output, "\t%s_data\t\t/* data */\n", name);
2002d60533cSmacallan	fprintf(output, "};\n\n");
20143d302b1Smacallan	fprintf(output, "static u_char %s_data[] = {\n", name);
2022d60533cSmacallan	for (i = f->firstchar; i < f->firstchar + f->numchars; i++) {
2032d60533cSmacallan		fprintf(output, "\t/* %d */\n", i);
2042d60533cSmacallan		idx = i * f->stride * f->fontheight;
2052d60533cSmacallan		for (y = 0; y < f->fontheight; y++) {
2062d60533cSmacallan			for (x = 0; x < f->stride; x++) {
2072d60533cSmacallan				fprintf(output, "0x%02x, ",buffer[idx + x]);
2082d60533cSmacallan			}
2092d60533cSmacallan			fprintf(output, "/* ");
2107dd7205fSmacallan			pxls = f->fontwidth;
2112d60533cSmacallan			for (x = 0; x < f->stride; x++) {
2122d60533cSmacallan				c = buffer[idx + x];
2132d60533cSmacallan				msk = 0x80;
2147dd7205fSmacallan				left = pxls > 8 ? 8 : pxls;
2157dd7205fSmacallan				for (j = 0; j < left; j++) {
216426ebb7fSmacallan					fprintf(output, "%s",
217426ebb7fSmacallan					    (c & msk) != 0 ? "[]" : ". ");
2182d60533cSmacallan					msk = msk >> 1;
2192d60533cSmacallan				}
2207dd7205fSmacallan				pxls -= 8;
2212d60533cSmacallan			}
2222d60533cSmacallan			fprintf(output, " */\n");
2232d60533cSmacallan
2242d60533cSmacallan			idx += f->stride;
2252d60533cSmacallan		}
2262d60533cSmacallan	}
2272d60533cSmacallan	fprintf(output, "};\n");
2282d60533cSmacallan	fclose(output);
2292d60533cSmacallan	return 0;
2302d60533cSmacallan}
2312d60533cSmacallan
232d5f9d0dbSmacallanvoid
233d5f9d0dbSmacallaninterpret(FILE *foo)
234d5f9d0dbSmacallan{
23551bbcd86Smacallan	char line[128], *arg, name[64] = "foo", *buffer;
2362b154882Suwe	int buflen = -1;
2376c735b96Schristos	int in_char = 0, current = -1, stride = 0, charsize = 0;
238d5f9d0dbSmacallan	int width, height, x, y, num;
239d5f9d0dbSmacallan	int first = 255, last = 0;
240d5f9d0dbSmacallan	int left, top, lines;
241d5f9d0dbSmacallan	int bl = 255, bt = 255, br = -1, bb = -1;
242d5f9d0dbSmacallan	struct wsdisplay_font f;
2432b154882Suwe	int status;
244d5f9d0dbSmacallan
245d5f9d0dbSmacallan	while (fgets(line, sizeof(line), foo) != NULL) {
2466c735b96Schristos		size_t i = 0, len;
247d5f9d0dbSmacallan		/* separate keyword from parameters */
248d5f9d0dbSmacallan		len = strlen(line);
2496c735b96Schristos		while (!isspace((unsigned char)line[i]) && i < len) i++;
250d5f9d0dbSmacallan		line[i] = 0;
251d5f9d0dbSmacallan		arg = &line[i + 1];
252d5f9d0dbSmacallan		i = 0;
253d5f9d0dbSmacallan		len = strlen(arg);
254d5f9d0dbSmacallan		/* get rid of garbage */
2556c735b96Schristos		while ((!iscntrl((unsigned char)arg[i])) && (arg[i] != 0)) {
256d5f9d0dbSmacallan			i++;
257d5f9d0dbSmacallan		}
258d5f9d0dbSmacallan		arg[i] = 0;
259d5f9d0dbSmacallan		if (strcmp(line, "FAMILY_NAME") == 0) {
260d5f9d0dbSmacallan			/* cut off quotation marks */
2616c735b96Schristos			strlcpy(name, arg + 1, 64);
26261348eb3Smacallan			if (verbose) printf("name: %s\n", name);
2637d6aaba4Smacallan		} else if (strcmp(line, "COMMENT") == 0) {
2647d6aaba4Smacallan			commentptr += snprintf(&commentbuf[commentptr],
2656c735b96Schristos			    sizeof(commentbuf) - commentptr, "%s\n", arg);
266a5a7ac3fSmacallan		} else if (strcmp(line, "SPACING") == 0) {
267a5a7ac3fSmacallan			char spc[16];
268a5a7ac3fSmacallan			int res;
269a5a7ac3fSmacallan			res = sscanf(arg, "%s", spc);
270a5a7ac3fSmacallan			if (res > 0) {
271a5a7ac3fSmacallan				if (verbose) printf("spacing %s\n", spc);
272a5a7ac3fSmacallan				if ((spc[1] == 'P') && (force == 0)) {
2736c735b96Schristos					warnx("This is a proportional font, "
2746c735b96Schristos					   "results are probably not suitable "
2756c735b96Schristos					   "for console use.");
2766c735b96Schristos					errx(EXIT_FAILURE, "Use -f to override "
2776c735b96Schristos					    "if you want to try it anyway.");
278a5a7ac3fSmacallan				}
279a5a7ac3fSmacallan			}
280d5f9d0dbSmacallan		} else if (strcmp(line, "FONTBOUNDINGBOX") == 0) {
281d5f9d0dbSmacallan			int res;
282d5f9d0dbSmacallan			res = sscanf(arg, "%d %d %d %d",
283d5f9d0dbSmacallan					  &width, &height, &x, &y);
284d5f9d0dbSmacallan			stride = (width + 7) >> 3;
28561348eb3Smacallan			if (verbose) printf("box %d x %d\n", width, height);
286d5f9d0dbSmacallan			if (stride > 2) {
2876c735b96Schristos				errx(EXIT_FAILURE,
28861348eb3Smacallan				    "no fonts wider than 16 work for now\n");
289d5f9d0dbSmacallan			}
290d5f9d0dbSmacallan			charsize = height * stride;
291d5f9d0dbSmacallan			buflen = 256 * charsize;
2922b154882Suwe			buffer = calloc(1, buflen);
293d5f9d0dbSmacallan			if (buffer == NULL) {
29461348eb3Smacallan				err(EXIT_FAILURE,
29561348eb3Smacallan				    "failed to allocate %dKB for glyphs\n",
296d5f9d0dbSmacallan				    buflen);
297ce18d260Suwe			}
298d5f9d0dbSmacallan		} else if (strcmp(line, "CHARS") == 0) {
299d5f9d0dbSmacallan			if (sscanf(arg, "%d", &num) == 1)
30061348eb3Smacallan				if (verbose)
30161348eb3Smacallan				    printf("number of characters: %d\n", num);
302d5f9d0dbSmacallan		} else if (strcmp(line, "STARTCHAR") == 0) {
303d5f9d0dbSmacallan			in_char = 1;
304d5f9d0dbSmacallan		} else if (strcmp(line, "ENDCHAR") == 0) {
305d5f9d0dbSmacallan			in_char = 0;
306d5f9d0dbSmacallan			current = -1;
307d5f9d0dbSmacallan		} else if (strcmp(line, "ENCODING") == 0) {
308d5f9d0dbSmacallan			if (sscanf(arg, "%d", &current) == 1) {
309d5f9d0dbSmacallan				if (current >= 0 && current < 256) {
310d5f9d0dbSmacallan					if (current < first) first = current;
311d5f9d0dbSmacallan					if (current > last) last = current;
312554a5d78Smacallan					if (dump) printf("glyph %d\n", current);
313d5f9d0dbSmacallan				}
314d5f9d0dbSmacallan			}
315d5f9d0dbSmacallan		} else if (strcmp(line, "BBX") == 0) {
316d5f9d0dbSmacallan			int cx, cy, cwi, che;
317d5f9d0dbSmacallan			if (sscanf(arg, "%d %d %d %d", &cwi, &che, &cx, &cy)
318d5f9d0dbSmacallan			     == 4) {
319d5f9d0dbSmacallan				left = cx;
320d5f9d0dbSmacallan				lines = che;
321d5f9d0dbSmacallan				top = height + y - che - cy;
322d5f9d0dbSmacallan				if (left < bl) bl = left;
323d5f9d0dbSmacallan				if (top < bt) bt = top;
324d5f9d0dbSmacallan				if ((left + cwi) > br) br = left + cwi;
325d5f9d0dbSmacallan				if ((top + che) > bb) bb = top + che;
3266c735b96Schristos				if (dump && verbose)
3276c735b96Schristos					printf("top %d left %d\n", top, left);
328d5f9d0dbSmacallan			}
329d5f9d0dbSmacallan		} else if (strcmp(line, "BITMAP") == 0) {
330d5f9d0dbSmacallan			int i, j, k, l;
331d5f9d0dbSmacallan			char num[32];
332d5f9d0dbSmacallan			char *gptr = &buffer[charsize * current];
333d5f9d0dbSmacallan			char *bptr = gptr + top;
334d5f9d0dbSmacallan			uint16_t *bptr16 = (uint16_t *)gptr;
335d5f9d0dbSmacallan			bptr16 += top;
336d5f9d0dbSmacallan			/* see if the character is in range */
337d5f9d0dbSmacallan			if ((current < 0) || (current > 255)) continue;
338d5f9d0dbSmacallan			/* now we read & render the character */
339d5f9d0dbSmacallan			for (i = 0; i < lines; i++) {
340d5f9d0dbSmacallan				fgets(num, 32, foo);
341d5f9d0dbSmacallan				sscanf(num, "%x", &l);
342d5f9d0dbSmacallan				if ((stride) == 2 && (strlen(num) < 4))
343d5f9d0dbSmacallan					l = l << 8;
344d5f9d0dbSmacallan				l = l >> left;
345d5f9d0dbSmacallan				if (stride == 1) {
346b9e0dc8cSmacallan					*bptr = l;
347b9e0dc8cSmacallan					bptr++;
348d5f9d0dbSmacallan				} else {
349d5f9d0dbSmacallan					*bptr16 = htobe16(l);
350d5f9d0dbSmacallan					bptr16++;
351ce18d260Suwe				}
352d5f9d0dbSmacallan			}
353554a5d78Smacallan			if (dump) {
354554a5d78Smacallan				gptr = &buffer[charsize * current];
355554a5d78Smacallan				for (i = 0; i < height; i++) {
356554a5d78Smacallan					dump_line(gptr, stride);
357554a5d78Smacallan					gptr += stride;
358554a5d78Smacallan				}
359554a5d78Smacallan			}
360d5f9d0dbSmacallan		}
361d5f9d0dbSmacallan	}
36261348eb3Smacallan	if (verbose) {
36361348eb3Smacallan		printf("range %d to %d\n", first, last);
36461348eb3Smacallan		printf("encoding: %s\n", encname[encoding]);
36561348eb3Smacallan		printf("actual box: %d %d %d %d\n", bl, bt, br, bb);
36661348eb3Smacallan	}
3672b154882Suwe
368d5f9d0dbSmacallan	/* now stuff it into a something wsfont understands */
369d5f9d0dbSmacallan	f.fontwidth = width /*(width + 3) & ~3*/;
370d5f9d0dbSmacallan	f.fontheight = height;
371d5f9d0dbSmacallan	f.firstchar = first;
3722b154882Suwe	f.numchars = last - first + 1;
373d5f9d0dbSmacallan	f.stride = stride;
3742b154882Suwe	f.encoding = encoding;
37551bbcd86Smacallan	if (fontname[0] == 0) {
37651bbcd86Smacallan		f.name = name;
37751bbcd86Smacallan	} else f.name = fontname;
378d5f9d0dbSmacallan	f.bitorder = WSDISPLAY_FONTORDER_L2R;
379d5f9d0dbSmacallan	f.byteorder = WSDISPLAY_FONTORDER_L2R;
380d5f9d0dbSmacallan	f.data = &buffer[first * charsize];
381d5f9d0dbSmacallan
3822b154882Suwe	if (ofile == NULL) {
3832b154882Suwe		int fdev = open("/dev/wsfont", O_RDWR, 0);
3842b154882Suwe		if (fdev < 0)
3852b154882Suwe			err(EXIT_FAILURE, "/dev/wsfont");
3862b154882Suwe		status = ioctl(fdev, WSDISPLAYIO_LDFONT, &f);
3872b154882Suwe		if (status != 0)
3882b154882Suwe			err(EXIT_FAILURE, "WSDISPLAYIO_LDFONT");
3892b154882Suwe		close(fdev);
3902b154882Suwe	}
3912b154882Suwe	else {
3926c735b96Schristos		if (header == 0)
3932d60533cSmacallan			write_wsf(ofile, &f, buffer, buflen);
3946c735b96Schristos		else
39551bbcd86Smacallan			write_header(ofile, &f, buffer, buflen);
3962b154882Suwe	}
397d5f9d0dbSmacallan}
398d5f9d0dbSmacallan
3992b154882Suwe__dead void
4002b154882Suweusage()
4012b154882Suwe{
4026c735b96Schristos	fprintf(stderr, "Usage: %s [-vdhf] [-e encoding] [-N name] "
4036c735b96Schristos	    "[-o ofile.wsf] font.bdf\n", getprogname());
4042b154882Suwe	exit(EXIT_FAILURE);
4052b154882Suwe}
4062b154882Suwe
4072b154882Suweint
408d5f9d0dbSmacallanmain(int argc, char *argv[])
409d5f9d0dbSmacallan{
410d5f9d0dbSmacallan	FILE *foo;
4112b154882Suwe	const char *encname = NULL;
4122b154882Suwe
4132b154882Suwe	int c;
414a5a7ac3fSmacallan	while ((c = getopt(argc, argv, "e:o:N:vdhf")) != -1) {
4152b154882Suwe		switch (c) {
4162b154882Suwe
4172b154882Suwe		/* font encoding */
4182b154882Suwe		case 'e':
4192b154882Suwe			if (encname != NULL)
4202b154882Suwe				usage();
4212b154882Suwe			encname = optarg;
4222b154882Suwe			break;
4232b154882Suwe
4242b154882Suwe		/* output file name */
4252b154882Suwe		case 'o':
4262b154882Suwe			if (ofile != NULL)
4272b154882Suwe				usage();
4282b154882Suwe			ofile = optarg;
4292b154882Suwe			break;
4302b154882Suwe
43161348eb3Smacallan		case 'v':
43261348eb3Smacallan			verbose = 1;
43361348eb3Smacallan			break;
43461348eb3Smacallan
435554a5d78Smacallan		case 'd':
436554a5d78Smacallan			dump = 1;
437554a5d78Smacallan			break;
438554a5d78Smacallan
4392d60533cSmacallan		case 'h':
4402d60533cSmacallan			header = 1;
4412d60533cSmacallan			break;
442a5a7ac3fSmacallan		case 'f':
443a5a7ac3fSmacallan			force = 1;
444a5a7ac3fSmacallan			break;
44551bbcd86Smacallan		case 'N':
44651bbcd86Smacallan			strncpy(fontname, optarg, 64);
44751bbcd86Smacallan			break;
4482b154882Suwe		case '?':	/* FALLTHROUGH */
4492b154882Suwe		default:
4502b154882Suwe			usage();
451d5f9d0dbSmacallan		}
452d5f9d0dbSmacallan	}
4532b154882Suwe
4542b154882Suwe	argc -= optind;
4552b154882Suwe	argv += optind;
4562b154882Suwe
4572b154882Suwe	if (encname == NULL) {
4582b154882Suwe		encoding = WSDISPLAY_FONTENC_ISO;
4592b154882Suwe	}
4602b154882Suwe	else {
4612b154882Suwe		for (const struct encmap *e = encmap; e->name; ++e) {
4622b154882Suwe			if (strcmp(e->name, encname) == 0) {
4632b154882Suwe				encoding = e->encoding;
4642b154882Suwe				break;
4652b154882Suwe			}
4662b154882Suwe		}
4672b154882Suwe	}
4682b154882Suwe
4692b154882Suwe	/* get encoding from the bdf file? */
4702b154882Suwe	if (encoding == -1)
4712b154882Suwe		encoding = WSDISPLAY_FONTENC_ISO;
4722b154882Suwe
4732b154882Suwe	if (argc == 0)
4742b154882Suwe		usage();
4752b154882Suwe
4762b154882Suwe	const char *bdfname = argv[0];
4772b154882Suwe	foo = fopen(bdfname, "r");
4782b154882Suwe	if (foo == NULL)
4792b154882Suwe		err(EXIT_FAILURE, "%s", bdfname);
4802b154882Suwe
4812b154882Suwe	interpret(foo);
4822b154882Suwe	return EXIT_SUCCESS;
483d5f9d0dbSmacallan}
484