bdfload.c revision 7d6aaba4
1/* $NetBSD: bdfload.c,v 1.10 2022/08/23 18:28:14 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; 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 for (x = 0; x < f->stride; x++) { 207 c = buffer[idx + x]; 208 msk = 0x80; 209 for (j = 0; j < 8; j++) { 210 fprintf(output, "%s", 211 (c & msk) != 0 ? "[]" : ". "); 212 msk = msk >> 1; 213 } 214 } 215 fprintf(output, " */\n"); 216 217 idx += f->stride; 218 } 219 } 220 fprintf(output, "};\n"); 221 fclose(output); 222 return 0; 223} 224 225void 226interpret(FILE *foo) 227{ 228 char line[128], *arg, name[64] = "foop", *buffer; 229 int buflen = -1; 230 int len, in_char = 0, current = -1, stride = 0, charsize = 0; 231 int width, height, x, y, num; 232 int first = 255, last = 0; 233 int left, top, lines; 234 int bl = 255, bt = 255, br = -1, bb = -1; 235 struct wsdisplay_font f; 236 int status; 237 238 while (fgets(line, sizeof(line), foo) != NULL) { 239 int i = 0; 240 /* separate keyword from parameters */ 241 len = strlen(line); 242 while (!isspace(line[i]) && (i < len)) i++; 243 line[i] = 0; 244 arg = &line[i + 1]; 245 i = 0; 246 len = strlen(arg); 247 /* get rid of garbage */ 248 while ((!iscntrl(arg[i])) && (arg[i] != 0)) { 249 i++; 250 } 251 arg[i] = 0; 252 if (strcmp(line, "FAMILY_NAME") == 0) { 253 /* cut off quotation marks */ 254 strncpy(name, arg + 1, 64); 255 name[strlen(name) - 1] = 0; 256 if (verbose) printf("name: %s\n", name); 257 } else if (strcmp(line, "COMMENT") == 0) { 258 commentptr += snprintf(&commentbuf[commentptr], 259 2048 - commentptr, 260 "%s\n", arg); 261 } else if (strcmp(line, "FONTBOUNDINGBOX") == 0) { 262 int res; 263 res = sscanf(arg, "%d %d %d %d", 264 &width, &height, &x, &y); 265 stride = (width + 7) >> 3; 266 if (verbose) printf("box %d x %d\n", width, height); 267 if (stride > 2) { 268 err(EXIT_FAILURE, 269 "no fonts wider than 16 work for now\n"); 270 } 271 charsize = height * stride; 272 buflen = 256 * charsize; 273 buffer = calloc(1, buflen); 274 if (buffer == NULL) { 275 err(EXIT_FAILURE, 276 "failed to allocate %dKB for glyphs\n", 277 buflen); 278 } 279 } else if (strcmp(line, "CHARS") == 0) { 280 if (sscanf(arg, "%d", &num) == 1) 281 if (verbose) 282 printf("number of characters: %d\n", num); 283 } else if (strcmp(line, "STARTCHAR") == 0) { 284 in_char = 1; 285 } else if (strcmp(line, "ENDCHAR") == 0) { 286 in_char = 0; 287 current = -1; 288 } else if (strcmp(line, "ENCODING") == 0) { 289 if (sscanf(arg, "%d", ¤t) == 1) { 290 if (current >= 0 && current < 256) { 291 if (current < first) first = current; 292 if (current > last) last = current; 293 if (dump) printf("glyph %d\n", current); 294 } 295 } 296 } else if (strcmp(line, "BBX") == 0) { 297 int cx, cy, cwi, che; 298 if (sscanf(arg, "%d %d %d %d", &cwi, &che, &cx, &cy) 299 == 4) { 300 left = cx; 301 lines = che; 302 top = height + y - che - cy; 303 if (left < bl) bl = left; 304 if (top < bt) bt = top; 305 if ((left + cwi) > br) br = left + cwi; 306 if ((top + che) > bb) bb = top + che; 307 if(dump && verbose) printf("top %d left %d\n", top, left); 308 } 309 } else if (strcmp(line, "BITMAP") == 0) { 310 int i, j, k, l; 311 char num[32]; 312 char *gptr = &buffer[charsize * current]; 313 char *bptr = gptr + top; 314 uint16_t *bptr16 = (uint16_t *)gptr; 315 bptr16 += top; 316 /* see if the character is in range */ 317 if ((current < 0) || (current > 255)) continue; 318 /* now we read & render the character */ 319 for (i = 0; i < lines; i++) { 320 fgets(num, 32, foo); 321 sscanf(num, "%x", &l); 322 if ((stride) == 2 && (strlen(num) < 4)) 323 l = l << 8; 324 l = l >> left; 325 if (stride == 1) { 326 *bptr = l; 327 bptr++; 328 } else { 329 *bptr16 = htobe16(l); 330 bptr16++; 331 } 332 } 333 if (dump) { 334 gptr = &buffer[charsize * current]; 335 for (i = 0; i < height; i++) { 336 dump_line(gptr, stride); 337 gptr += stride; 338 } 339 } 340 } 341 } 342 if (verbose) { 343 printf("range %d to %d\n", first, last); 344 printf("encoding: %s\n", encname[encoding]); 345 printf("actual box: %d %d %d %d\n", bl, bt, br, bb); 346 } 347 348 /* now stuff it into a something wsfont understands */ 349 f.fontwidth = width /*(width + 3) & ~3*/; 350 f.fontheight = height; 351 f.firstchar = first; 352 f.numchars = last - first + 1; 353 f.stride = stride; 354 f.encoding = encoding; 355 f.name = name; 356 f.bitorder = WSDISPLAY_FONTORDER_L2R; 357 f.byteorder = WSDISPLAY_FONTORDER_L2R; 358 f.data = &buffer[first * charsize]; 359 360 if (ofile == NULL) { 361 int fdev = open("/dev/wsfont", O_RDWR, 0); 362 if (fdev < 0) 363 err(EXIT_FAILURE, "/dev/wsfont"); 364 status = ioctl(fdev, WSDISPLAYIO_LDFONT, &f); 365 if (status != 0) 366 err(EXIT_FAILURE, "WSDISPLAYIO_LDFONT"); 367 close(fdev); 368 } 369 else { 370 if (header == 0) { 371 write_wsf(ofile, &f, buffer, buflen); 372 } else 373 write_header(ofile, &f, name, buffer, buflen); 374 } 375} 376 377__dead void 378usage() 379{ 380 fprintf(stderr, "usage: bdfload [-vdh] [-e encoding] [-o ofile.wsf] font.bdf\n"); 381 exit(EXIT_FAILURE); 382} 383 384int 385main(int argc, char *argv[]) 386{ 387 FILE *foo; 388 const char *encname = NULL; 389 390 int c; 391 while ((c = getopt(argc, argv, "e:o:vdh")) != -1) { 392 switch (c) { 393 394 /* font encoding */ 395 case 'e': 396 if (encname != NULL) 397 usage(); 398 encname = optarg; 399 break; 400 401 /* output file name */ 402 case 'o': 403 if (ofile != NULL) 404 usage(); 405 ofile = optarg; 406 break; 407 408 case 'v': 409 verbose = 1; 410 break; 411 412 case 'd': 413 dump = 1; 414 break; 415 416 case 'h': 417 header = 1; 418 break; 419 420 case '?': /* FALLTHROUGH */ 421 default: 422 usage(); 423 } 424 } 425 426 argc -= optind; 427 argv += optind; 428 429 if (encname == NULL) { 430 encoding = WSDISPLAY_FONTENC_ISO; 431 } 432 else { 433 for (const struct encmap *e = encmap; e->name; ++e) { 434 if (strcmp(e->name, encname) == 0) { 435 encoding = e->encoding; 436 break; 437 } 438 } 439 } 440 441 /* get encoding from the bdf file? */ 442 if (encoding == -1) 443 encoding = WSDISPLAY_FONTENC_ISO; 444 445 if (argc == 0) 446 usage(); 447 448 const char *bdfname = argv[0]; 449 foo = fopen(bdfname, "r"); 450 if (foo == NULL) 451 err(EXIT_FAILURE, "%s", bdfname); 452 453 interpret(foo); 454 return EXIT_SUCCESS; 455} 456