| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: bdfload.c,v 1.7 2022/08/16 21:52:00 macallan Exp $ */ | | 1 | /* $NetBSD: bdfload.c,v 1.8 2022/08/23 13:56:04 macallan Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 2018 Michael Lorenz | | 4 | * Copyright (c) 2018 Michael Lorenz |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | | 14 | * documentation and/or other materials provided with the distribution. |
| @@ -89,44 +89,142 @@ const char * const encname[] = { | | | @@ -89,44 +89,142 @@ const char * const encname[] = { |
89 | _ENC(WSDISPLAY_FONTENC_ISO), | | 89 | _ENC(WSDISPLAY_FONTENC_ISO), |
90 | _ENC(WSDISPLAY_FONTENC_IBM), | | 90 | _ENC(WSDISPLAY_FONTENC_IBM), |
91 | _ENC(WSDISPLAY_FONTENC_PCVT), | | 91 | _ENC(WSDISPLAY_FONTENC_PCVT), |
92 | _ENC(WSDISPLAY_FONTENC_ISO7), | | 92 | _ENC(WSDISPLAY_FONTENC_ISO7), |
93 | _ENC(WSDISPLAY_FONTENC_ISO2), | | 93 | _ENC(WSDISPLAY_FONTENC_ISO2), |
94 | _ENC(WSDISPLAY_FONTENC_KOI8_R), | | 94 | _ENC(WSDISPLAY_FONTENC_KOI8_R), |
95 | }; | | 95 | }; |
96 | | | 96 | |
97 | | | 97 | |
98 | const char *ofile = NULL; | | 98 | const char *ofile = NULL; |
99 | int encoding = -1; | | 99 | int encoding = -1; |
100 | int verbose = 0; | | 100 | int verbose = 0; |
101 | int dump = 0; | | 101 | int dump = 0; |
| | | 102 | int header = 0; |
102 | | | 103 | |
103 | void | | 104 | void |
104 | dump_line(char *gptr, int stride) | | 105 | dump_line(char *gptr, int stride) |
105 | { | | 106 | { |
106 | int i, j, msk, c; | | 107 | int i, j, msk, c; |
107 | | | 108 | |
108 | for (i = 0; i < stride; i++) { | | 109 | for (i = 0; i < stride; i++) { |
109 | c = gptr[i]; | | 110 | c = gptr[i]; |
110 | msk = 0x80; | | 111 | msk = 0x80; |
111 | for (j = 0; j < 8; j++) { | | 112 | for (j = 0; j < 8; j++) { |
112 | putchar((c & msk) != 0 ? '#' : ' '); | | 113 | putchar((c & msk) != 0 ? '#' : ' '); |
113 | msk = msk >> 1; | | 114 | msk = msk >> 1; |
114 | } | | 115 | } |
115 | } | | 116 | } |
116 | printf("\n"); | | 117 | printf("\n"); |
117 | } | | 118 | } |
118 | | | 119 | |
119 | void | | 120 | void |
| | | 121 | write_wsf(const char *oname, struct wsdisplay_font *f, char *buffer, int buflen) |
| | | 122 | { |
| | | 123 | struct wsfthdr h; |
| | | 124 | |
| | | 125 | memset(&h, 0, sizeof(h)); |
| | | 126 | strncpy(h.magic, "WSFT", sizeof(h.magic)); |
| | | 127 | strncpy(h.name, f->name, sizeof(h.name)); |
| | | 128 | h.firstchar = htole32(f->firstchar); |
| | | 129 | h.numchars = htole32(f->numchars); |
| | | 130 | h.encoding = htole32(f->encoding); |
| | | 131 | h.fontwidth = htole32(f->fontwidth); |
| | | 132 | h.fontheight = htole32(f->fontheight); |
| | | 133 | h.stride = htole32(f->stride); |
| | | 134 | h.bitorder = htole32(f->bitorder); |
| | | 135 | h.byteorder = htole32(f->byteorder); |
| | | 136 | |
| | | 137 | int wsfd = open(ofile, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| | | 138 | if (wsfd < 0) |
| | | 139 | err(EXIT_FAILURE, "%s", ofile); |
| | | 140 | |
| | | 141 | ssize_t nwritten; |
| | | 142 | nwritten = write(wsfd, &h, sizeof(h)); |
| | | 143 | if (nwritten < 0) |
| | | 144 | err(EXIT_FAILURE, "%s", ofile); |
| | | 145 | if (nwritten != sizeof(h)) |
| | | 146 | errx(EXIT_FAILURE, "%s: partial write", ofile); |
| | | 147 | |
| | | 148 | nwritten = write(wsfd, buffer, buflen); |
| | | 149 | if (nwritten < 0) |
| | | 150 | err(EXIT_FAILURE, "%s", ofile); |
| | | 151 | if (nwritten != buflen) |
| | | 152 | errx(EXIT_FAILURE, "%s: partial write", ofile); |
| | | 153 | close(wsfd); |
| | | 154 | } |
| | | 155 | |
| | | 156 | int |
| | | 157 | write_header(const char *filename, struct wsdisplay_font *f, char *name, |
| | | 158 | char *buffer, int buflen) |
| | | 159 | { |
| | | 160 | FILE *output; |
| | | 161 | int i, j, x, y, idx; |
| | | 162 | char fontname[64], c, msk; |
| | | 163 | |
| | | 164 | /* now output as a header file */ |
| | | 165 | snprintf(fontname, sizeof(fontname), "%s_%dx%d", name, |
| | | 166 | f->fontwidth, f->fontheight); |
| | | 167 | for (i = 0; i < strlen(fontname); i++) { |
| | | 168 | if (isblank((int)fontname[i])) |
| | | 169 | fontname[i]='_'; |
| | | 170 | } |
| | | 171 | if ((output = fopen(filename, "w")) == NULL) { |
| | | 172 | fprintf(stderr, "Can't open output file %s\n", filename); |
| | | 173 | return -1; |
| | | 174 | } |
| | | 175 | fprintf(output, "static u_char %s_data[];\n", fontname); |
| | | 176 | fprintf(output, "\n"); |
| | | 177 | fprintf(output, "static struct wsdisplay_font %s = {\n", fontname); |
| | | 178 | fprintf(output, "\t\"%s\",\t\t\t/* typeface name */\n", name); |
| | | 179 | fprintf(output, "\t%d,\t\t\t\t/* firstchar */\n", f->firstchar); |
| | | 180 | fprintf(output, "\t%d,\t\t\t\t/* numchars */\n", f->numchars); |
| | | 181 | fprintf(output, "\t%d,\t\t\t\t/* encoding */\n", f->encoding); |
| | | 182 | fprintf(output, "\t%d,\t\t\t\t/* fontwidth */\n", f->fontwidth); |
| | | 183 | fprintf(output, "\t%d,\t\t\t\t/* fontheight */\n", f->fontheight); |
| | | 184 | fprintf(output, "\t%d,\t\t\t\t/* stride */\n", f->stride); |
| | | 185 | fprintf(output, "\tWSDISPLAY_FONTORDER_L2R,\t/* bit order */\n"); |
| | | 186 | fprintf(output, "\tWSDISPLAY_FONTORDER_L2R,\t/* byte order */\n"); |
| | | 187 | fprintf(output, "\t%s_data\t\t/* data */\n", fontname); |
| | | 188 | fprintf(output, "};\n\n"); |
| | | 189 | fprintf(output, "static u_char %s_data[] = {\n", fontname); |
| | | 190 | for (i = f->firstchar; i < f->firstchar + f->numchars; i++) { |
| | | 191 | fprintf(output, "\t/* %d */\n", i); |
| | | 192 | idx = i * f->stride * f->fontheight; |
| | | 193 | for (y = 0; y < f->fontheight; y++) { |
| | | 194 | for (x = 0; x < f->stride; x++) { |
| | | 195 | fprintf(output, "0x%02x, ",buffer[idx + x]); |
| | | 196 | } |
| | | 197 | fprintf(output, "/* "); |
| | | 198 | for (x = 0; x < f->stride; x++) { |
| | | 199 | c = buffer[idx + x]; |
| | | 200 | msk = 0x80; |
| | | 201 | for (j = 0; j < 8; j++) { |
| | | 202 | fprintf(output, "%c", |
| | | 203 | (c & msk) != 0 ? '#' : ' '); |
| | | 204 | msk = msk >> 1; |
| | | 205 | } |
| | | 206 | } |
| | | 207 | fprintf(output, " */\n"); |
| | | 208 | |
| | | 209 | idx += f->stride; |
| | | 210 | } |
| | | 211 | } |
| | | 212 | fprintf(output, "};\n"); |
| | | 213 | fclose(output); |
| | | 214 | return 0; |
| | | 215 | } |
| | | 216 | |
| | | 217 | void |
120 | interpret(FILE *foo) | | 218 | interpret(FILE *foo) |
121 | { | | 219 | { |
122 | char line[128], *arg, name[64] = "foop", *buffer; | | 220 | char line[128], *arg, name[64] = "foop", *buffer; |
123 | int buflen = -1; | | 221 | int buflen = -1; |
124 | int len, in_char = 0, current = -1, stride = 0, charsize = 0; | | 222 | int len, in_char = 0, current = -1, stride = 0, charsize = 0; |
125 | int width, height, x, y, num; | | 223 | int width, height, x, y, num; |
126 | int first = 255, last = 0; | | 224 | int first = 255, last = 0; |
127 | int left, top, lines; | | 225 | int left, top, lines; |
128 | int bl = 255, bt = 255, br = -1, bb = -1; | | 226 | int bl = 255, bt = 255, br = -1, bb = -1; |
129 | struct wsdisplay_font f; | | 227 | struct wsdisplay_font f; |
130 | int status; | | 228 | int status; |
131 | | | 229 | |
132 | while (fgets(line, sizeof(line), foo) != NULL) { | | 230 | while (fgets(line, sizeof(line), foo) != NULL) { |
| @@ -247,100 +345,76 @@ interpret(FILE *foo) | | | @@ -247,100 +345,76 @@ interpret(FILE *foo) |
247 | f.byteorder = WSDISPLAY_FONTORDER_L2R; | | 345 | f.byteorder = WSDISPLAY_FONTORDER_L2R; |
248 | f.data = &buffer[first * charsize]; | | 346 | f.data = &buffer[first * charsize]; |
249 | | | 347 | |
250 | if (ofile == NULL) { | | 348 | if (ofile == NULL) { |
251 | int fdev = open("/dev/wsfont", O_RDWR, 0); | | 349 | int fdev = open("/dev/wsfont", O_RDWR, 0); |
252 | if (fdev < 0) | | 350 | if (fdev < 0) |
253 | err(EXIT_FAILURE, "/dev/wsfont"); | | 351 | err(EXIT_FAILURE, "/dev/wsfont"); |
254 | status = ioctl(fdev, WSDISPLAYIO_LDFONT, &f); | | 352 | status = ioctl(fdev, WSDISPLAYIO_LDFONT, &f); |
255 | if (status != 0) | | 353 | if (status != 0) |
256 | err(EXIT_FAILURE, "WSDISPLAYIO_LDFONT"); | | 354 | err(EXIT_FAILURE, "WSDISPLAYIO_LDFONT"); |
257 | close(fdev); | | 355 | close(fdev); |
258 | } | | 356 | } |
259 | else { | | 357 | else { |
260 | struct wsfthdr h; | | 358 | if (header == 0) { |
261 | | | 359 | write_wsf(ofile, &f, buffer, buflen); |
262 | memset(&h, 0, sizeof(h)); | | 360 | } else |
263 | strncpy(h.magic, "WSFT", sizeof(h.magic)); | | 361 | write_header(ofile, &f, name, buffer, buflen); |
264 | strncpy(h.name, f.name, sizeof(h.name)); | | | |
265 | h.firstchar = htole32(f.firstchar); | | | |
266 | h.numchars = htole32(f.numchars); | | | |
267 | h.encoding = htole32(f.encoding); | | | |
268 | h.fontwidth = htole32(f.fontwidth); | | | |
269 | h.fontheight = htole32(f.fontheight); | | | |
270 | h.stride = htole32(f.stride); | | | |
271 | h.bitorder = htole32(f.bitorder); | | | |
272 | h.byteorder = htole32(f.byteorder); | | | |
273 | | | | |
274 | int wsfd = open(ofile, O_WRONLY | O_CREAT | O_TRUNC, 0644); | | | |
275 | if (wsfd < 0) | | | |
276 | err(EXIT_FAILURE, "%s", ofile); | | | |
277 | | | | |
278 | ssize_t nwritten; | | | |
279 | nwritten = write(wsfd, &h, sizeof(h)); | | | |
280 | if (nwritten < 0) | | | |
281 | err(EXIT_FAILURE, "%s", ofile); | | | |
282 | if (nwritten != sizeof(h)) | | | |
283 | errx(EXIT_FAILURE, "%s: partial write", ofile); | | | |
284 | | | | |
285 | nwritten = write(wsfd, buffer, buflen); | | | |
286 | if (nwritten < 0) | | | |
287 | err(EXIT_FAILURE, "%s", ofile); | | | |
288 | if (nwritten != buflen) | | | |
289 | errx(EXIT_FAILURE, "%s: partial write", ofile); | | | |
290 | close(wsfd); | | | |
291 | } | | 362 | } |
292 | } | | 363 | } |
293 | | | 364 | |
294 | | | | |
295 | __dead void | | 365 | __dead void |
296 | usage() | | 366 | usage() |
297 | { | | 367 | { |
298 | fprintf(stderr, "usage: bdfload [-vd] [-e encoding] [-o ofile.wsf] font.bdf\n"); | | 368 | fprintf(stderr, "usage: bdfload [-vdh] [-e encoding] [-o ofile.wsf] font.bdf\n"); |
299 | exit(EXIT_FAILURE); | | 369 | exit(EXIT_FAILURE); |
300 | } | | 370 | } |
301 | | | 371 | |
302 | int | | 372 | int |
303 | main(int argc, char *argv[]) | | 373 | main(int argc, char *argv[]) |
304 | { | | 374 | { |
305 | FILE *foo; | | 375 | FILE *foo; |
306 | const char *encname = NULL; | | 376 | const char *encname = NULL; |
307 | | | 377 | |
308 | int c; | | 378 | int c; |
309 | while ((c = getopt(argc, argv, "e:o:vd")) != -1) { | | 379 | while ((c = getopt(argc, argv, "e:o:vdh")) != -1) { |
310 | switch (c) { | | 380 | switch (c) { |
311 | | | 381 | |
312 | /* font encoding */ | | 382 | /* font encoding */ |
313 | case 'e': | | 383 | case 'e': |
314 | if (encname != NULL) | | 384 | if (encname != NULL) |
315 | usage(); | | 385 | usage(); |
316 | encname = optarg; | | 386 | encname = optarg; |
317 | break; | | 387 | break; |
318 | | | 388 | |
319 | /* output file name */ | | 389 | /* output file name */ |
320 | case 'o': | | 390 | case 'o': |
321 | if (ofile != NULL) | | 391 | if (ofile != NULL) |
322 | usage(); | | 392 | usage(); |
323 | ofile = optarg; | | 393 | ofile = optarg; |
324 | break; | | 394 | break; |
325 | | | 395 | |
326 | case 'v': | | 396 | case 'v': |
327 | verbose = 1; | | 397 | verbose = 1; |
328 | break; | | 398 | break; |
329 | | | 399 | |
330 | case 'd': | | 400 | case 'd': |
331 | dump = 1; | | 401 | dump = 1; |
332 | break; | | 402 | break; |
333 | | | 403 | |
| | | 404 | case 'h': |
| | | 405 | header = 1; |
| | | 406 | break; |
| | | 407 | |
334 | case '?': /* FALLTHROUGH */ | | 408 | case '?': /* FALLTHROUGH */ |
335 | default: | | 409 | default: |
336 | usage(); | | 410 | usage(); |
337 | } | | 411 | } |
338 | } | | 412 | } |
339 | | | 413 | |
340 | argc -= optind; | | 414 | argc -= optind; |
341 | argv += optind; | | 415 | argv += optind; |
342 | | | 416 | |
343 | if (encname == NULL) { | | 417 | if (encname == NULL) { |
344 | encoding = WSDISPLAY_FONTENC_ISO; | | 418 | encoding = WSDISPLAY_FONTENC_ISO; |
345 | } | | 419 | } |
346 | else { | | 420 | else { |