bdfload.c revision 682eda17
1682eda17Smacallan/*	$NetBSD: bdfload.c,v 1.19 2022/10/25 13:36:35 macallan 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] = "";
10916ae1398Smacallanchar *names[256];
1102b154882Suwe
111554a5d78Smacallanvoid
112554a5d78Smacallandump_line(char *gptr, int stride)
113554a5d78Smacallan{
114554a5d78Smacallan	int i, j, msk, c;
115554a5d78Smacallan
116554a5d78Smacallan	for (i = 0; i < stride; i++) {
117554a5d78Smacallan		c = gptr[i];
118554a5d78Smacallan		msk = 0x80;
119554a5d78Smacallan		for (j = 0; j < 8; j++) {
120554a5d78Smacallan			putchar((c & msk) != 0 ? '#' : ' ');
121554a5d78Smacallan			msk = msk >> 1;
122554a5d78Smacallan		}
123554a5d78Smacallan	}
124ec211365Smacallan	printf("\n");
125554a5d78Smacallan}
126554a5d78Smacallan
1272d60533cSmacallanvoid
1282d60533cSmacallanwrite_wsf(const char *oname, struct wsdisplay_font *f, char *buffer, int buflen)
1292d60533cSmacallan{
1302d60533cSmacallan	struct wsfthdr h;
1312d60533cSmacallan
1322d60533cSmacallan	memset(&h, 0, sizeof(h));
1332d60533cSmacallan	strncpy(h.magic, "WSFT", sizeof(h.magic));
1342d60533cSmacallan	strncpy(h.name, f->name, sizeof(h.name));
1352d60533cSmacallan	h.firstchar = htole32(f->firstchar);
1362d60533cSmacallan	h.numchars = htole32(f->numchars);
1372d60533cSmacallan	h.encoding = htole32(f->encoding);
1382d60533cSmacallan	h.fontwidth = htole32(f->fontwidth);
1392d60533cSmacallan	h.fontheight = htole32(f->fontheight);
1402d60533cSmacallan	h.stride = htole32(f->stride);
1412d60533cSmacallan	h.bitorder = htole32(f->bitorder);
1422d60533cSmacallan	h.byteorder = htole32(f->byteorder);
1432d60533cSmacallan
1442d60533cSmacallan	int wsfd = open(ofile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1452d60533cSmacallan	if (wsfd < 0)
1462d60533cSmacallan		err(EXIT_FAILURE, "%s", ofile);
1472d60533cSmacallan
1482d60533cSmacallan	ssize_t nwritten;
1492d60533cSmacallan	nwritten = write(wsfd, &h, sizeof(h));
1502d60533cSmacallan	if (nwritten < 0)
1512d60533cSmacallan		err(EXIT_FAILURE, "%s", ofile);
1522d60533cSmacallan	if (nwritten != sizeof(h))
1532d60533cSmacallan		errx(EXIT_FAILURE, "%s: partial write", ofile);
1542d60533cSmacallan
1552d60533cSmacallan	nwritten = write(wsfd, buffer, buflen);
1562d60533cSmacallan	if (nwritten < 0)
1572d60533cSmacallan		err(EXIT_FAILURE, "%s", ofile);
1582d60533cSmacallan	if (nwritten != buflen)
1592d60533cSmacallan		errx(EXIT_FAILURE, "%s: partial write", ofile);
1602d60533cSmacallan	close(wsfd);
1612d60533cSmacallan}
1622d60533cSmacallan
1632d60533cSmacallanint
16451bbcd86Smacallanwrite_header(const char *filename, struct wsdisplay_font *f,
1652d60533cSmacallan             char *buffer, int buflen)
1662d60533cSmacallan{
1672d60533cSmacallan	FILE *output;
1687dd7205fSmacallan	int i, j, x, y, idx, pxls, left;
16943d302b1Smacallan	char name[64], c, msk;
1702d60533cSmacallan
1712d60533cSmacallan	/* now output as a header file */
17243d302b1Smacallan	snprintf(name, sizeof(name), "%s_%dx%d", f->name,
1732d60533cSmacallan	    f->fontwidth, f->fontheight);
17443d302b1Smacallan	for (i = 0; i < strlen(name); i++) {
1756c735b96Schristos		if (isblank((unsigned char)name[i]))
1766c735b96Schristos			name[i] = '_';
1772d60533cSmacallan	}
1782d60533cSmacallan	if ((output = fopen(filename, "w")) == NULL) {
1796c735b96Schristos		warn("Can't open output file `%s'", filename);
1802d60533cSmacallan		return -1;
1812d60533cSmacallan	}
1827d6aaba4Smacallan	if (commentptr > 0) {
1837d6aaba4Smacallan		fprintf(output, "/*\n");
1847d6aaba4Smacallan		fputs(commentbuf, output);
1857d6aaba4Smacallan		fprintf(output, "*/\n\n");
1867d6aaba4Smacallan	}
1877d6aaba4Smacallan
18843d302b1Smacallan	fprintf(output, "static u_char %s_data[];\n", name);
1892d60533cSmacallan	fprintf(output, "\n");
19043d302b1Smacallan	fprintf(output, "static struct wsdisplay_font %s = {\n", name);
19151bbcd86Smacallan	fprintf(output, "\t\"%s\",\t\t\t/* typeface name */\n", f->name);
1922d60533cSmacallan	fprintf(output, "\t%d,\t\t\t\t/* firstchar */\n", f->firstchar);
1932d60533cSmacallan	fprintf(output, "\t%d,\t\t\t\t/* numchars */\n", f->numchars);
1942d60533cSmacallan	fprintf(output, "\t%d,\t\t\t\t/* encoding */\n", f->encoding);
1952d60533cSmacallan	fprintf(output, "\t%d,\t\t\t\t/* fontwidth */\n", f->fontwidth);
1962d60533cSmacallan	fprintf(output, "\t%d,\t\t\t\t/* fontheight */\n", f->fontheight);
1972d60533cSmacallan	fprintf(output, "\t%d,\t\t\t\t/* stride */\n", f->stride);
1982d60533cSmacallan	fprintf(output, "\tWSDISPLAY_FONTORDER_L2R,\t/* bit order */\n");
1992d60533cSmacallan	fprintf(output, "\tWSDISPLAY_FONTORDER_L2R,\t/* byte order */\n");
20043d302b1Smacallan	fprintf(output, "\t%s_data\t\t/* data */\n", name);
2012d60533cSmacallan	fprintf(output, "};\n\n");
20243d302b1Smacallan	fprintf(output, "static u_char %s_data[] = {\n", name);
2032d60533cSmacallan	for (i = f->firstchar; i < f->firstchar + f->numchars; i++) {
20416ae1398Smacallan		if (names[i] != NULL) {
20516ae1398Smacallan			fprintf(output, "\t/* %d %s */\n", i, names[i]);
20616ae1398Smacallan		} else
20716ae1398Smacallan			fprintf(output, "\t/* %d */\n", i);
2082d60533cSmacallan		idx = i * f->stride * f->fontheight;
2092d60533cSmacallan		for (y = 0; y < f->fontheight; y++) {
2102d60533cSmacallan			for (x = 0; x < f->stride; x++) {
2112d60533cSmacallan				fprintf(output, "0x%02x, ",buffer[idx + x]);
2122d60533cSmacallan			}
2132d60533cSmacallan			fprintf(output, "/* ");
2147dd7205fSmacallan			pxls = f->fontwidth;
2152d60533cSmacallan			for (x = 0; x < f->stride; x++) {
2162d60533cSmacallan				c = buffer[idx + x];
2172d60533cSmacallan				msk = 0x80;
2187dd7205fSmacallan				left = pxls > 8 ? 8 : pxls;
2197dd7205fSmacallan				for (j = 0; j < left; j++) {
220426ebb7fSmacallan					fprintf(output, "%s",
221426ebb7fSmacallan					    (c & msk) != 0 ? "[]" : ". ");
2222d60533cSmacallan					msk = msk >> 1;
2232d60533cSmacallan				}
2247dd7205fSmacallan				pxls -= 8;
2252d60533cSmacallan			}
2262d60533cSmacallan			fprintf(output, " */\n");
2272d60533cSmacallan
2282d60533cSmacallan			idx += f->stride;
2292d60533cSmacallan		}
2302d60533cSmacallan	}
2312d60533cSmacallan	fprintf(output, "};\n");
2322d60533cSmacallan	fclose(output);
2332d60533cSmacallan	return 0;
2342d60533cSmacallan}
2352d60533cSmacallan
236d5f9d0dbSmacallanvoid
237d5f9d0dbSmacallaninterpret(FILE *foo)
238d5f9d0dbSmacallan{
239999c8572Smacallan	char line[128], *arg, name[64] = "foo", *buffer, *cbitmap;
24016ae1398Smacallan	char charname[65], *charnamebuf;
24116ae1398Smacallan	int buflen = -1, charnamebufptr = 0, j;
2426c735b96Schristos	int in_char = 0, current = -1, stride = 0, charsize = 0;
243d5f9d0dbSmacallan	int width, height, x, y, num;
244d5f9d0dbSmacallan	int first = 255, last = 0;
245d5f9d0dbSmacallan	int left, top, lines;
246d5f9d0dbSmacallan	int bl = 255, bt = 255, br = -1, bb = -1;
247d5f9d0dbSmacallan	struct wsdisplay_font f;
2482b154882Suwe	int status;
249d5f9d0dbSmacallan
25016ae1398Smacallan	charnamebuf = malloc(64 * 256);
25116ae1398Smacallan	if (charnamebuf == 0) err(EXIT_FAILURE, "failed to allocate memory\n");
25216ae1398Smacallan	memset(charnamebuf, 0, 64 * 256);
25316ae1398Smacallan	for (j = 0; j < 256; j++) names[j] = NULL;
25416ae1398Smacallan
255d5f9d0dbSmacallan	while (fgets(line, sizeof(line), foo) != NULL) {
2566c735b96Schristos		size_t i = 0, len;
257d5f9d0dbSmacallan		/* separate keyword from parameters */
258d5f9d0dbSmacallan		len = strlen(line);
2596c735b96Schristos		while (!isspace((unsigned char)line[i]) && i < len) i++;
260d5f9d0dbSmacallan		line[i] = 0;
261d5f9d0dbSmacallan		arg = &line[i + 1];
262d5f9d0dbSmacallan		i = 0;
263d5f9d0dbSmacallan		len = strlen(arg);
264d5f9d0dbSmacallan		/* get rid of garbage */
2656c735b96Schristos		while ((!iscntrl((unsigned char)arg[i])) && (arg[i] != 0)) {
266d5f9d0dbSmacallan			i++;
267d5f9d0dbSmacallan		}
268d5f9d0dbSmacallan		arg[i] = 0;
269d5f9d0dbSmacallan		if (strcmp(line, "FAMILY_NAME") == 0) {
270682eda17Smacallan			char *q;
271d5f9d0dbSmacallan			/* cut off quotation marks */
2726c735b96Schristos			strlcpy(name, arg + 1, 64);
273682eda17Smacallan			/* remove trailing " */
274682eda17Smacallan			if ((q = strnstr(name, "\"", 64)) != NULL)
275682eda17Smacallan				*q = 0;
27661348eb3Smacallan			if (verbose) printf("name: %s\n", name);
2777d6aaba4Smacallan		} else if (strcmp(line, "COMMENT") == 0) {
2787d6aaba4Smacallan			commentptr += snprintf(&commentbuf[commentptr],
2796c735b96Schristos			    sizeof(commentbuf) - commentptr, "%s\n", arg);
280a5a7ac3fSmacallan		} else if (strcmp(line, "SPACING") == 0) {
281a5a7ac3fSmacallan			char spc[16];
282a5a7ac3fSmacallan			int res;
283a5a7ac3fSmacallan			res = sscanf(arg, "%s", spc);
284a5a7ac3fSmacallan			if (res > 0) {
285a5a7ac3fSmacallan				if (verbose) printf("spacing %s\n", spc);
286a5a7ac3fSmacallan				if ((spc[1] == 'P') && (force == 0)) {
2876c735b96Schristos					warnx("This is a proportional font, "
2886c735b96Schristos					   "results are probably not suitable "
2896c735b96Schristos					   "for console use.");
2906c735b96Schristos					errx(EXIT_FAILURE, "Use -f to override "
2916c735b96Schristos					    "if you want to try it anyway.");
292a5a7ac3fSmacallan				}
293a5a7ac3fSmacallan			}
294d5f9d0dbSmacallan		} else if (strcmp(line, "FONTBOUNDINGBOX") == 0) {
295d5f9d0dbSmacallan			int res;
296d5f9d0dbSmacallan			res = sscanf(arg, "%d %d %d %d",
297d5f9d0dbSmacallan					  &width, &height, &x, &y);
298d5f9d0dbSmacallan			stride = (width + 7) >> 3;
29961348eb3Smacallan			if (verbose) printf("box %d x %d\n", width, height);
300d5f9d0dbSmacallan			if (stride > 2) {
3016c735b96Schristos				errx(EXIT_FAILURE,
30261348eb3Smacallan				    "no fonts wider than 16 work for now\n");
303d5f9d0dbSmacallan			}
304d5f9d0dbSmacallan			charsize = height * stride;
305999c8572Smacallan			buflen = 257 * charsize;
3062b154882Suwe			buffer = calloc(1, buflen);
307d5f9d0dbSmacallan			if (buffer == NULL) {
30861348eb3Smacallan				err(EXIT_FAILURE,
30961348eb3Smacallan				    "failed to allocate %dKB for glyphs\n",
310d5f9d0dbSmacallan				    buflen);
311ce18d260Suwe			}
312999c8572Smacallan			cbitmap = buffer + 256 * charsize;
313d5f9d0dbSmacallan		} else if (strcmp(line, "CHARS") == 0) {
314d5f9d0dbSmacallan			if (sscanf(arg, "%d", &num) == 1)
31561348eb3Smacallan				if (verbose)
31661348eb3Smacallan				    printf("number of characters: %d\n", num);
317d5f9d0dbSmacallan		} else if (strcmp(line, "STARTCHAR") == 0) {
318d5f9d0dbSmacallan			in_char = 1;
319999c8572Smacallan			if (charsize <= 1) err(EXIT_FAILURE,
320999c8572Smacallan			    "syntax error - no valid FONTBOUNDINGBOX\n");
321999c8572Smacallan			memset(cbitmap, 0, charsize);
32216ae1398Smacallan			strlcpy(charname, arg, 64);
323d5f9d0dbSmacallan		} else if (strcmp(line, "ENDCHAR") == 0) {
324d5f9d0dbSmacallan			in_char = 0;
325999c8572Smacallan			/* only commit the glyph if it's in range */
326999c8572Smacallan			if ((current >= 0) && (current < 256)) {
327999c8572Smacallan				memcpy(&buffer[charsize * current],
328999c8572Smacallan				    cbitmap, charsize);
32916ae1398Smacallan				if ((strlen(charname) > 0) &&
33016ae1398Smacallan				    (charnamebufptr < 255 * 64)) {
33116ae1398Smacallan				    	char *cur;
33216ae1398Smacallan					int len;
33316ae1398Smacallan					/* copy name into buffer, keep a
33416ae1398Smacallan					 * pointer to it for later */
33516ae1398Smacallan					cur = &charnamebuf[charnamebufptr];
33616ae1398Smacallan					len = strlcpy(cur, charname, 64);
33716ae1398Smacallan					charnamebufptr += len + 1;
33816ae1398Smacallan					names[current] = cur;
33916ae1398Smacallan				}
340999c8572Smacallan			}
341d5f9d0dbSmacallan			current = -1;
342d5f9d0dbSmacallan		} else if (strcmp(line, "ENCODING") == 0) {
343d5f9d0dbSmacallan			if (sscanf(arg, "%d", &current) == 1) {
344d5f9d0dbSmacallan				if (current >= 0 && current < 256) {
345d5f9d0dbSmacallan					if (current < first) first = current;
346d5f9d0dbSmacallan					if (current > last) last = current;
347554a5d78Smacallan					if (dump) printf("glyph %d\n", current);
348d5f9d0dbSmacallan				}
349d5f9d0dbSmacallan			}
350d5f9d0dbSmacallan		} else if (strcmp(line, "BBX") == 0) {
351d5f9d0dbSmacallan			int cx, cy, cwi, che;
352d5f9d0dbSmacallan			if (sscanf(arg, "%d %d %d %d", &cwi, &che, &cx, &cy)
353d5f9d0dbSmacallan			     == 4) {
354d5f9d0dbSmacallan				left = cx;
355d5f9d0dbSmacallan				lines = che;
356d5f9d0dbSmacallan				top = height + y - che - cy;
357d5f9d0dbSmacallan				if (left < bl) bl = left;
358d5f9d0dbSmacallan				if (top < bt) bt = top;
359d5f9d0dbSmacallan				if ((left + cwi) > br) br = left + cwi;
360d5f9d0dbSmacallan				if ((top + che) > bb) bb = top + che;
3616c735b96Schristos				if (dump && verbose)
3626c735b96Schristos					printf("top %d left %d\n", top, left);
363d5f9d0dbSmacallan			}
364d5f9d0dbSmacallan		} else if (strcmp(line, "BITMAP") == 0) {
365d5f9d0dbSmacallan			int i, j, k, l;
366d5f9d0dbSmacallan			char num[32];
367999c8572Smacallan			char *gptr = cbitmap;
368d5f9d0dbSmacallan			char *bptr = gptr + top;
369d5f9d0dbSmacallan			uint16_t *bptr16 = (uint16_t *)gptr;
370d5f9d0dbSmacallan			bptr16 += top;
371d5f9d0dbSmacallan			/* see if the character is in range */
372d5f9d0dbSmacallan			if ((current < 0) || (current > 255)) continue;
373d5f9d0dbSmacallan			/* now we read & render the character */
374d5f9d0dbSmacallan			for (i = 0; i < lines; i++) {
375d5f9d0dbSmacallan				fgets(num, 32, foo);
376d5f9d0dbSmacallan				sscanf(num, "%x", &l);
377d5f9d0dbSmacallan				if ((stride) == 2 && (strlen(num) < 4))
378d5f9d0dbSmacallan					l = l << 8;
379d5f9d0dbSmacallan				l = l >> left;
380d5f9d0dbSmacallan				if (stride == 1) {
381b9e0dc8cSmacallan					*bptr = l;
382b9e0dc8cSmacallan					bptr++;
383d5f9d0dbSmacallan				} else {
384d5f9d0dbSmacallan					*bptr16 = htobe16(l);
385d5f9d0dbSmacallan					bptr16++;
386ce18d260Suwe				}
387d5f9d0dbSmacallan			}
388554a5d78Smacallan			if (dump) {
389999c8572Smacallan				gptr = cbitmap;
390554a5d78Smacallan				for (i = 0; i < height; i++) {
391554a5d78Smacallan					dump_line(gptr, stride);
392554a5d78Smacallan					gptr += stride;
393554a5d78Smacallan				}
394554a5d78Smacallan			}
395d5f9d0dbSmacallan		}
396d5f9d0dbSmacallan	}
39761348eb3Smacallan	if (verbose) {
39861348eb3Smacallan		printf("range %d to %d\n", first, last);
39961348eb3Smacallan		printf("encoding: %s\n", encname[encoding]);
40061348eb3Smacallan		printf("actual box: %d %d %d %d\n", bl, bt, br, bb);
40161348eb3Smacallan	}
4022b154882Suwe
403d5f9d0dbSmacallan	/* now stuff it into a something wsfont understands */
404d5f9d0dbSmacallan	f.fontwidth = width /*(width + 3) & ~3*/;
405d5f9d0dbSmacallan	f.fontheight = height;
406d5f9d0dbSmacallan	f.firstchar = first;
4072b154882Suwe	f.numchars = last - first + 1;
408d5f9d0dbSmacallan	f.stride = stride;
4092b154882Suwe	f.encoding = encoding;
41051bbcd86Smacallan	if (fontname[0] == 0) {
41151bbcd86Smacallan		f.name = name;
41251bbcd86Smacallan	} else f.name = fontname;
413d5f9d0dbSmacallan	f.bitorder = WSDISPLAY_FONTORDER_L2R;
414d5f9d0dbSmacallan	f.byteorder = WSDISPLAY_FONTORDER_L2R;
415d5f9d0dbSmacallan	f.data = &buffer[first * charsize];
416d5f9d0dbSmacallan
4172b154882Suwe	if (ofile == NULL) {
4182b154882Suwe		int fdev = open("/dev/wsfont", O_RDWR, 0);
4192b154882Suwe		if (fdev < 0)
4202b154882Suwe			err(EXIT_FAILURE, "/dev/wsfont");
4212b154882Suwe		status = ioctl(fdev, WSDISPLAYIO_LDFONT, &f);
4222b154882Suwe		if (status != 0)
4232b154882Suwe			err(EXIT_FAILURE, "WSDISPLAYIO_LDFONT");
4242b154882Suwe		close(fdev);
4252b154882Suwe	}
4262b154882Suwe	else {
4276c735b96Schristos		if (header == 0)
4282d60533cSmacallan			write_wsf(ofile, &f, buffer, buflen);
4296c735b96Schristos		else
43051bbcd86Smacallan			write_header(ofile, &f, buffer, buflen);
4312b154882Suwe	}
432d5f9d0dbSmacallan}
433d5f9d0dbSmacallan
4342b154882Suwe__dead void
4352b154882Suweusage()
4362b154882Suwe{
4376c735b96Schristos	fprintf(stderr, "Usage: %s [-vdhf] [-e encoding] [-N name] "
4386c735b96Schristos	    "[-o ofile.wsf] font.bdf\n", getprogname());
4392b154882Suwe	exit(EXIT_FAILURE);
4402b154882Suwe}
4412b154882Suwe
4422b154882Suweint
443d5f9d0dbSmacallanmain(int argc, char *argv[])
444d5f9d0dbSmacallan{
445d5f9d0dbSmacallan	FILE *foo;
4462b154882Suwe	const char *encname = NULL;
4472b154882Suwe
4482b154882Suwe	int c;
449a5a7ac3fSmacallan	while ((c = getopt(argc, argv, "e:o:N:vdhf")) != -1) {
4502b154882Suwe		switch (c) {
4512b154882Suwe
4522b154882Suwe		/* font encoding */
4532b154882Suwe		case 'e':
4542b154882Suwe			if (encname != NULL)
4552b154882Suwe				usage();
4562b154882Suwe			encname = optarg;
4572b154882Suwe			break;
4582b154882Suwe
4592b154882Suwe		/* output file name */
4602b154882Suwe		case 'o':
4612b154882Suwe			if (ofile != NULL)
4622b154882Suwe				usage();
4632b154882Suwe			ofile = optarg;
4642b154882Suwe			break;
4652b154882Suwe
46661348eb3Smacallan		case 'v':
46761348eb3Smacallan			verbose = 1;
46861348eb3Smacallan			break;
46961348eb3Smacallan
470554a5d78Smacallan		case 'd':
471554a5d78Smacallan			dump = 1;
472554a5d78Smacallan			break;
473554a5d78Smacallan
4742d60533cSmacallan		case 'h':
4752d60533cSmacallan			header = 1;
4762d60533cSmacallan			break;
477a5a7ac3fSmacallan		case 'f':
478a5a7ac3fSmacallan			force = 1;
479a5a7ac3fSmacallan			break;
48051bbcd86Smacallan		case 'N':
48151bbcd86Smacallan			strncpy(fontname, optarg, 64);
48251bbcd86Smacallan			break;
4832b154882Suwe		case '?':	/* FALLTHROUGH */
4842b154882Suwe		default:
4852b154882Suwe			usage();
486d5f9d0dbSmacallan		}
487d5f9d0dbSmacallan	}
4882b154882Suwe
4892b154882Suwe	argc -= optind;
4902b154882Suwe	argv += optind;
4912b154882Suwe
4922b154882Suwe	if (encname == NULL) {
4932b154882Suwe		encoding = WSDISPLAY_FONTENC_ISO;
4942b154882Suwe	}
4952b154882Suwe	else {
4962b154882Suwe		for (const struct encmap *e = encmap; e->name; ++e) {
4972b154882Suwe			if (strcmp(e->name, encname) == 0) {
4982b154882Suwe				encoding = e->encoding;
4992b154882Suwe				break;
5002b154882Suwe			}
5012b154882Suwe		}
5022b154882Suwe	}
5032b154882Suwe
5042b154882Suwe	/* get encoding from the bdf file? */
5052b154882Suwe	if (encoding == -1)
5062b154882Suwe		encoding = WSDISPLAY_FONTENC_ISO;
5072b154882Suwe
5082b154882Suwe	if (argc == 0)
5092b154882Suwe		usage();
5102b154882Suwe
5112b154882Suwe	const char *bdfname = argv[0];
5122b154882Suwe	foo = fopen(bdfname, "r");
5132b154882Suwe	if (foo == NULL)
5142b154882Suwe		err(EXIT_FAILURE, "%s", bdfname);
5152b154882Suwe
5162b154882Suwe	interpret(foo);
5172b154882Suwe	return EXIT_SUCCESS;
518d5f9d0dbSmacallan}
519