bdfload.c revision 61348eb3
161348eb3Smacallan/*	$NetBSD: bdfload.c,v 1.4 2022/08/16 20:27:33 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 },
692b154882Suwe	{ "iso-8859-1",	WSDISPLAY_FONTENC_ISO },
702b154882Suwe	{ "iso-8859-2",	WSDISPLAY_FONTENC_ISO2 },
712b154882Suwe	{ "iso-8859-7",	WSDISPLAY_FONTENC_ISO7 },
722b154882Suwe	{ "iso2",	WSDISPLAY_FONTENC_ISO2 },
732b154882Suwe	{ "iso7",	WSDISPLAY_FONTENC_ISO7 },
742b154882Suwe	{ "iso8859-1",	WSDISPLAY_FONTENC_ISO },
752b154882Suwe	{ "iso8859-2",	WSDISPLAY_FONTENC_ISO2 },
762b154882Suwe	{ "iso8859-7",	WSDISPLAY_FONTENC_ISO7 },
772b154882Suwe	{ "koi8-r",	WSDISPLAY_FONTENC_KOI8_R },
782b154882Suwe	{ "koi8r",	WSDISPLAY_FONTENC_KOI8_R },
792b154882Suwe	{ "latin-1",	WSDISPLAY_FONTENC_ISO },
802b154882Suwe	{ "latin-2",	WSDISPLAY_FONTENC_ISO2 },
812b154882Suwe	{ "latin1",	WSDISPLAY_FONTENC_ISO },
822b154882Suwe	{ "latin2",	WSDISPLAY_FONTENC_ISO2 },
832b154882Suwe	{ "pcvt",	WSDISPLAY_FONTENC_PCVT },
842b154882Suwe	{ NULL, -1 }
852b154882Suwe};
862b154882Suwe
872b154882Suweconst char * const encname[] = {
882b154882Suwe#define _ENC(_e) [_e] = #_e
892b154882Suwe	_ENC(WSDISPLAY_FONTENC_ISO),
902b154882Suwe	_ENC(WSDISPLAY_FONTENC_IBM),
912b154882Suwe	_ENC(WSDISPLAY_FONTENC_PCVT),
922b154882Suwe	_ENC(WSDISPLAY_FONTENC_ISO7),
932b154882Suwe	_ENC(WSDISPLAY_FONTENC_ISO2),
942b154882Suwe	_ENC(WSDISPLAY_FONTENC_KOI8_R),
952b154882Suwe};
962b154882Suwe
972b154882Suwe
982b154882Suweconst char *ofile = NULL;
992b154882Suweint encoding = -1;
10061348eb3Smacallanint verbose = 0;
1012b154882Suwe
1022b154882Suwe
103d5f9d0dbSmacallanvoid
104d5f9d0dbSmacallaninterpret(FILE *foo)
105d5f9d0dbSmacallan{
1062b154882Suwe	char line[128], *arg, name[64] = "foop", *buffer;
1072b154882Suwe	int buflen = -1;
108d5f9d0dbSmacallan	int len, in_char = 0, current = -1, stride = 0, charsize = 0;
109d5f9d0dbSmacallan	int width, height, x, y, num;
110d5f9d0dbSmacallan	int first = 255, last = 0;
111d5f9d0dbSmacallan	int left, top, lines;
112d5f9d0dbSmacallan	int bl = 255, bt = 255, br = -1, bb = -1;
113d5f9d0dbSmacallan	struct wsdisplay_font f;
1142b154882Suwe	int status;
115d5f9d0dbSmacallan
116d5f9d0dbSmacallan	while (fgets(line, sizeof(line), foo) != NULL) {
117d5f9d0dbSmacallan		int i = 0;
118d5f9d0dbSmacallan		/* separate keyword from parameters */
119d5f9d0dbSmacallan		len = strlen(line);
120d5f9d0dbSmacallan		while (!isspace(line[i]) && (i < len)) i++;
121d5f9d0dbSmacallan		line[i] = 0;
122d5f9d0dbSmacallan		arg = &line[i + 1];
123d5f9d0dbSmacallan		i = 0;
124d5f9d0dbSmacallan		len = strlen(arg);
125d5f9d0dbSmacallan		/* get rid of garbage */
126d5f9d0dbSmacallan		while ((!iscntrl(arg[i])) && (arg[i] != 0)) {
127d5f9d0dbSmacallan			i++;
128d5f9d0dbSmacallan		}
129d5f9d0dbSmacallan		arg[i] = 0;
130d5f9d0dbSmacallan		if (strcmp(line, "FAMILY_NAME") == 0) {
131d5f9d0dbSmacallan			/* cut off quotation marks */
132d5f9d0dbSmacallan			strncpy(name, arg + 1, 64);
133d5f9d0dbSmacallan			name[strlen(name) - 1] = 0;
13461348eb3Smacallan			if (verbose) printf("name: %s\n", name);
135d5f9d0dbSmacallan		} else if (strcmp(line, "FONTBOUNDINGBOX") == 0) {
136d5f9d0dbSmacallan			int res;
137d5f9d0dbSmacallan			res = sscanf(arg, "%d %d %d %d",
138d5f9d0dbSmacallan					  &width, &height, &x, &y);
139d5f9d0dbSmacallan			stride = (width + 7) >> 3;
14061348eb3Smacallan			if (verbose) printf("box %d x %d\n", width, height);
141d5f9d0dbSmacallan			if (stride > 2) {
14261348eb3Smacallan				err(EXIT_FAILURE,
14361348eb3Smacallan				    "no fonts wider than 16 work for now\n");
144d5f9d0dbSmacallan			}
145d5f9d0dbSmacallan			charsize = height * stride;
146d5f9d0dbSmacallan			buflen = 256 * charsize;
1472b154882Suwe			buffer = calloc(1, buflen);
148d5f9d0dbSmacallan			if (buffer == NULL) {
14961348eb3Smacallan				err(EXIT_FAILURE,
15061348eb3Smacallan				    "failed to allocate %dKB for glyphs\n",
151d5f9d0dbSmacallan				    buflen);
152ce18d260Suwe			}
153d5f9d0dbSmacallan		} else if (strcmp(line, "CHARS") == 0) {
154d5f9d0dbSmacallan			if (sscanf(arg, "%d", &num) == 1)
15561348eb3Smacallan				if (verbose)
15661348eb3Smacallan				    printf("number of characters: %d\n", num);
157d5f9d0dbSmacallan		} else if (strcmp(line, "STARTCHAR") == 0) {
158d5f9d0dbSmacallan			in_char = 1;
159d5f9d0dbSmacallan		} else if (strcmp(line, "ENDCHAR") == 0) {
160d5f9d0dbSmacallan			in_char = 0;
161d5f9d0dbSmacallan			current = -1;
162d5f9d0dbSmacallan		} else if (strcmp(line, "ENCODING") == 0) {
163d5f9d0dbSmacallan			if (sscanf(arg, "%d", &current) == 1) {
164d5f9d0dbSmacallan				if (current >= 0 && current < 256) {
165d5f9d0dbSmacallan					if (current < first) first = current;
166d5f9d0dbSmacallan					if (current > last) last = current;
167d5f9d0dbSmacallan				}
168d5f9d0dbSmacallan			}
169d5f9d0dbSmacallan		} else if (strcmp(line, "BBX") == 0) {
170d5f9d0dbSmacallan			int cx, cy, cwi, che;
171d5f9d0dbSmacallan			if (sscanf(arg, "%d %d %d %d", &cwi, &che, &cx, &cy)
172d5f9d0dbSmacallan			     == 4) {
173d5f9d0dbSmacallan				left = cx;
174d5f9d0dbSmacallan				lines = che;
175d5f9d0dbSmacallan				top = height + y - che - cy;
176d5f9d0dbSmacallan				if (left < bl) bl = left;
177d5f9d0dbSmacallan				if (top < bt) bt = top;
178d5f9d0dbSmacallan				if ((left + cwi) > br) br = left + cwi;
179d5f9d0dbSmacallan				if ((top + che) > bb) bb = top + che;
180d5f9d0dbSmacallan			}
181d5f9d0dbSmacallan		} else if (strcmp(line, "BITMAP") == 0) {
182d5f9d0dbSmacallan			int i, j, k, l;
183d5f9d0dbSmacallan			char num[32];
184d5f9d0dbSmacallan			char *gptr = &buffer[charsize * current];
185d5f9d0dbSmacallan			char *bptr = gptr + top;
186d5f9d0dbSmacallan			uint16_t *bptr16 = (uint16_t *)gptr;
187d5f9d0dbSmacallan			bptr16 += top;
188d5f9d0dbSmacallan			/* see if the character is in range */
189d5f9d0dbSmacallan			if ((current < 0) || (current > 255)) continue;
190d5f9d0dbSmacallan			/* now we read & render the character */
191d5f9d0dbSmacallan			for (i = 0; i < lines; i++) {
192d5f9d0dbSmacallan				fgets(num, 32, foo);
193d5f9d0dbSmacallan				sscanf(num, "%x", &l);
194d5f9d0dbSmacallan				if ((stride) == 2 && (strlen(num) < 4))
195d5f9d0dbSmacallan					l = l << 8;
196d5f9d0dbSmacallan				l = l >> left;
197d5f9d0dbSmacallan				if (stride == 1) {
198d5f9d0dbSmacallan					*gptr = l;
199d5f9d0dbSmacallan					gptr++;
200d5f9d0dbSmacallan				} else {
201d5f9d0dbSmacallan					*bptr16 = htobe16(l);
202d5f9d0dbSmacallan					bptr16++;
203ce18d260Suwe				}
204d5f9d0dbSmacallan			}
205d5f9d0dbSmacallan		}
206d5f9d0dbSmacallan	}
20761348eb3Smacallan	if (verbose) {
20861348eb3Smacallan		printf("range %d to %d\n", first, last);
20961348eb3Smacallan		printf("encoding: %s\n", encname[encoding]);
21061348eb3Smacallan		printf("actual box: %d %d %d %d\n", bl, bt, br, bb);
21161348eb3Smacallan	}
2122b154882Suwe
213d5f9d0dbSmacallan	/* now stuff it into a something wsfont understands */
214d5f9d0dbSmacallan	f.fontwidth = width /*(width + 3) & ~3*/;
215d5f9d0dbSmacallan	f.fontheight = height;
216d5f9d0dbSmacallan	f.firstchar = first;
2172b154882Suwe	f.numchars = last - first + 1;
218d5f9d0dbSmacallan	f.stride = stride;
2192b154882Suwe	f.encoding = encoding;
220d5f9d0dbSmacallan	f.name = name;
221d5f9d0dbSmacallan	f.bitorder = WSDISPLAY_FONTORDER_L2R;
222d5f9d0dbSmacallan	f.byteorder = WSDISPLAY_FONTORDER_L2R;
223d5f9d0dbSmacallan	f.data = &buffer[first * charsize];
224d5f9d0dbSmacallan
2252b154882Suwe	if (ofile == NULL) {
2262b154882Suwe		int fdev = open("/dev/wsfont", O_RDWR, 0);
2272b154882Suwe		if (fdev < 0)
2282b154882Suwe			err(EXIT_FAILURE, "/dev/wsfont");
2292b154882Suwe		status = ioctl(fdev, WSDISPLAYIO_LDFONT, &f);
2302b154882Suwe		if (status != 0)
2312b154882Suwe			err(EXIT_FAILURE, "WSDISPLAYIO_LDFONT");
2322b154882Suwe		close(fdev);
2332b154882Suwe	}
2342b154882Suwe	else {
2352b154882Suwe		struct wsfthdr h;
2362b154882Suwe
2372b154882Suwe		memset(&h, 0, sizeof(h));
2382b154882Suwe		strncpy(h.magic, "WSFT", sizeof(h.magic));
2392b154882Suwe		strncpy(h.name, f.name, sizeof(h.name));
2402b154882Suwe		h.firstchar = htole32(f.firstchar);
2412b154882Suwe		h.numchars = htole32(f.numchars);
2422b154882Suwe		h.encoding = htole32(f.encoding);
2432b154882Suwe		h.fontwidth = htole32(f.fontwidth);
2442b154882Suwe		h.fontheight = htole32(f.fontheight);
2452b154882Suwe		h.stride = htole32(f.stride);
2462b154882Suwe		h.bitorder = htole32(f.bitorder);
2472b154882Suwe		h.byteorder = htole32(f.byteorder);
2482b154882Suwe
2492b154882Suwe		int wsfd = open(ofile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
2502b154882Suwe		if (wsfd < 0)
2512b154882Suwe			err(EXIT_FAILURE, "%s", ofile);
2522b154882Suwe
2532b154882Suwe		ssize_t nwritten;
2542b154882Suwe		nwritten = write(wsfd, &h, sizeof(h));
2552b154882Suwe		if (nwritten < 0)
2562b154882Suwe			err(EXIT_FAILURE, "%s", ofile);
2572b154882Suwe		if (nwritten != sizeof(h))
2582b154882Suwe			errx(EXIT_FAILURE, "%s: partial write", ofile);
2592b154882Suwe
2602b154882Suwe		nwritten = write(wsfd, buffer, buflen);
2612b154882Suwe		if (nwritten < 0)
2622b154882Suwe			err(EXIT_FAILURE, "%s", ofile);
2632b154882Suwe		if (nwritten != buflen)
2642b154882Suwe			errx(EXIT_FAILURE, "%s: partial write", ofile);
2652b154882Suwe		close(wsfd);
2662b154882Suwe	}
267d5f9d0dbSmacallan}
268d5f9d0dbSmacallan
2692b154882Suwe
2702b154882Suwe__dead void
2712b154882Suweusage()
2722b154882Suwe{
27361348eb3Smacallan	fprintf(stderr, "usage: bdfload [-v] [-e encoding] [-o ofile.wsf] font.bdf\n");
2742b154882Suwe	exit(EXIT_FAILURE);
2752b154882Suwe}
2762b154882Suwe
2772b154882Suweint
278d5f9d0dbSmacallanmain(int argc, char *argv[])
279d5f9d0dbSmacallan{
280d5f9d0dbSmacallan	FILE *foo;
2812b154882Suwe	const char *encname = NULL;
2822b154882Suwe
2832b154882Suwe	int c;
28461348eb3Smacallan	while ((c = getopt(argc, argv, "e:o:v")) != -1) {
2852b154882Suwe		switch (c) {
2862b154882Suwe
2872b154882Suwe		/* font encoding */
2882b154882Suwe		case 'e':
2892b154882Suwe			if (encname != NULL)
2902b154882Suwe				usage();
2912b154882Suwe			encname = optarg;
2922b154882Suwe			break;
2932b154882Suwe
2942b154882Suwe		/* output file name */
2952b154882Suwe		case 'o':
2962b154882Suwe			if (ofile != NULL)
2972b154882Suwe				usage();
2982b154882Suwe			ofile = optarg;
2992b154882Suwe			break;
3002b154882Suwe
30161348eb3Smacallan		case 'v':
30261348eb3Smacallan			verbose = 1;
30361348eb3Smacallan			break;
30461348eb3Smacallan
3052b154882Suwe		case '?':	/* FALLTHROUGH */
3062b154882Suwe		default:
3072b154882Suwe			usage();
308d5f9d0dbSmacallan		}
309d5f9d0dbSmacallan	}
3102b154882Suwe
3112b154882Suwe	argc -= optind;
3122b154882Suwe	argv += optind;
3132b154882Suwe
3142b154882Suwe	if (encname == NULL) {
3152b154882Suwe		encoding = WSDISPLAY_FONTENC_ISO;
3162b154882Suwe	}
3172b154882Suwe	else {
3182b154882Suwe		for (const struct encmap *e = encmap; e->name; ++e) {
3192b154882Suwe			if (strcmp(e->name, encname) == 0) {
3202b154882Suwe				encoding = e->encoding;
3212b154882Suwe				break;
3222b154882Suwe			}
3232b154882Suwe		}
3242b154882Suwe	}
3252b154882Suwe
3262b154882Suwe	/* get encoding from the bdf file? */
3272b154882Suwe	if (encoding == -1)
3282b154882Suwe		encoding = WSDISPLAY_FONTENC_ISO;
3292b154882Suwe
3302b154882Suwe	if (argc == 0)
3312b154882Suwe		usage();
3322b154882Suwe
3332b154882Suwe	const char *bdfname = argv[0];
3342b154882Suwe	foo = fopen(bdfname, "r");
3352b154882Suwe	if (foo == NULL)
3362b154882Suwe		err(EXIT_FAILURE, "%s", bdfname);
3372b154882Suwe
3382b154882Suwe	interpret(foo);
3392b154882Suwe	return EXIT_SUCCESS;
340d5f9d0dbSmacallan}
341