2 * setfont.c - Eugene Crosser & Andries Brouwer
6 * Loads the console font, and possibly the corresponding screen map(s).
7 * We accept two kind of screen maps, one [-m] giving the correspondence
8 * between some arbitrary 8-bit character set currently in use and the
9 * font positions, and the second [-u] giving the correspondence between
10 * font positions and Unicode values.
19 #include <sys/types.h>
21 #include <sys/ioctl.h>
28 #include "loadunimap.h"
30 #include "psffontop.h"
37 static int position_codepage(int iunit);
38 static void saveoldfont(int fd, char *ofil);
39 static void saveoldfontplusunicodemap(int fd, char *Ofil);
40 static void loadnewfont(int fd, char *ifil,
41 int iunit, int hwunit, int no_m, int no_u);
42 static void loadnewfonts(int fd, char **ifiles, int ifilct,
43 int iunit, int hwunit, int no_m, int no_u);
44 extern void saveoldmap(int fd, char *omfil);
45 extern void loadnewmap(int fd, char *mfil);
46 extern void activatemap(void);
47 extern void disactivatemap(void);
52 /* search for the font in these directories (with trailing /) */
53 char *fontdirpath[] = { "", DATADIR "/" FONTDIR "/", 0 };
54 char *fontsuffixes[] = { "", ".psfu", ".psf", ".cp", ".fnt", 0 };
55 /* hide partial fonts a bit - loading a single one is a bad idea */
56 char *partfontdirpath[] = { "", DATADIR "/" FONTDIR "/" PARTIALDIR "/", 0 };
57 char *partfontsuffixes[] = { "", 0 };
60 findfont(char *fnam) {
61 return findfile(fnam, fontdirpath, fontsuffixes);
65 findpartialfont(char *fnam) {
66 return findfile(fnam, partfontdirpath, partfontsuffixes);
73 "Usage: setfont [write-options] [-<N>] [newfont..] [-m consolemap] [-u unicodemap]\n"
74 " write-options (take place before file loading):\n"
75 " -o <filename> Write current font to <filename>\n"
76 " -O <filename> Write current font and unicode map to <filename>\n"
77 " -om <filename> Write current consolemap to <filename>\n"
78 " -ou <filename> Write current unicodemap to <filename>\n"
79 "If no newfont and no -[o|O|om|ou|m|u] option is given,\n"
80 "a default font is loaded:\n"
81 " setfont Load font \"default[.gz]\"\n"
82 " setfont -<N> Load font \"default8x<N>[.gz]\"\n"
83 "The -<N> option selects a font from a codepage that contains three fonts:\n"
84 " setfont -{8|14|16} codepage.cp[.gz] Load 8x<N> font from codepage.cp\n"
85 "Explicitly (with -m or -u) or implicitly (in the fontfile) given mappings\n"
86 "will be loaded and, in the case of consolemaps, activated.\n"
87 " -h<N> (no space) Override font height.\n"
88 " -m <fn> Load console screen map.\n"
89 " -u <fn> Load font unicode map.\n"
90 " -m none Suppress loading and activation of a screen map.\n"
91 " -u none Suppress loading of a unicode map.\n"
93 " -V Print version and exit.\n"
94 "Files are loaded from the current directory or /usr/lib/kbd/*/.\n"
102 main(int argc, char *argv[]) {
103 char *ifiles[MAXIFILES];
104 char *mfil, *ufil, *Ofil, *ofil, *omfil, *oufil;
105 int ifilct = 0, fd, i, iunit, hwunit, no_m, no_u;
108 set_progname(argv[0]);
110 setlocale(LC_ALL, "");
111 bindtextdomain(PACKAGE, LOCALEDIR);
116 ifiles[0] = mfil = ufil = Ofil = ofil = omfil = oufil = 0;
120 for (i = 1; i < argc; i++) {
121 if (!strcmp(argv[i], "-V")) {
122 print_version_and_exit();
123 } else if (!strcmp(argv[i], "-v")) {
125 } else if (!strcmp(argv[i], "-R")) {
127 } else if (!strcmp(argv[i], "-O")) {
128 if (++i == argc || Ofil)
131 } else if (!strcmp(argv[i], "-o")) {
132 if (++i == argc || ofil)
135 } else if (!strcmp(argv[i], "-om")) {
136 if (++i == argc || omfil)
139 } else if (!strcmp(argv[i], "-ou")) {
140 if (++i == argc || oufil)
143 } else if (!strcmp(argv[i], "-m")) {
144 if (++i == argc || mfil)
146 if (!strcmp(argv[i], "none"))
150 } else if (!strcmp(argv[i], "-u")) {
151 if (++i == argc || ufil)
153 if (!strcmp(argv[i], "none"))
157 } else if (!strcmp(argv[i], "-f")) {
159 } else if (!strncmp(argv[i], "-h", 2)) {
160 hwunit = atoi(argv[i]+2);
161 if (hwunit <= 0 || hwunit > 32)
163 } else if (argv[i][0] == '-') {
164 iunit = atoi(argv[i]+1);
165 if(iunit <= 0 || iunit > 32)
168 if (ifilct == MAXIFILES) {
169 fprintf(stderr, _("setfont: too many input files\n"));
172 ifiles[ifilct++] = argv[i];
176 if (ifilct && restore) {
177 fprintf(stderr, _("setfont: cannot both restore from character ROM"
178 " and from file. Font unchanged.\n"));
182 if (!ifilct && !mfil && !ufil &&
183 !Ofil && !ofil && !omfil && !oufil && !restore)
184 /* reset to some default */
185 ifiles[ifilct++] = "";
188 saveoldfontplusunicodemap(fd, Ofil);
191 saveoldfont(fd, ofil);
194 saveoldmap(fd, omfil);
197 saveunicodemap(fd, oufil);
200 loadnewmap(fd, mfil);
212 loadnewfonts(fd, ifiles, ifilct, iunit, hwunit, no_m, no_u);
215 loadunicodemap(fd, ufil);
221 * 0 - do not test, 1 - test and warn, 2 - test and wipe, 3 - refuse
223 static int erase_mode = 1;
226 do_loadfont(int fd, char *inbuf, int width, int height, int hwunit,
227 int fontsize, char *pathname) {
230 int bytewidth = (width+7)/8;
231 int charsize = height * bytewidth;
232 int kcharsize = 32 * bytewidth;
233 int bad_video_erase_char = 0;
235 if (height < 1 || height > 32) {
236 fprintf(stderr, _("Bad character height %d\n"), height);
239 if (width < 1 || width > 32) {
240 fprintf(stderr, _("Bad character width %d\n"), width);
247 buflen = kcharsize * ((fontsize < 128) ? 128 : fontsize);
248 buf = xmalloc(buflen);
249 memset(buf,0,buflen);
251 for (i = 0; i < fontsize; i++)
252 memcpy(buf+(i*kcharsize), inbuf+(i*charsize), charsize);
255 * Due to a kernel bug, font position 32 is used
256 * to erase the screen, regardless of maps loaded.
257 * So, usually this font position should be blank.
260 for (i = 0; i < kcharsize; i++)
261 if (buf[32*kcharsize+i])
262 bad_video_erase_char = 1;
263 if (bad_video_erase_char) {
265 _("%s: font position 32 is nonblank\n"),
271 for (i = 0; i < kcharsize; i++)
272 buf[32*kcharsize+i] = 0;
273 fprintf(stderr, _("%s: wiped it\n"), progname);
277 _("%s: background will look funny\n"),
286 if (height == hwunit && pathname)
287 printf(_("Loading %d-char %dx%d font from file %s\n"),
288 fontsize, width, height, pathname);
289 else if (height == hwunit)
290 printf(_("Loading %d-char %dx%d font\n"),
291 fontsize, width, height);
293 printf(_("Loading %d-char %dx%d (%d) font from file %s\n"),
294 fontsize, width, height, hwunit, pathname);
296 printf(_("Loading %d-char %dx%d (%d) font\n"),
297 fontsize, width, height, hwunit);
300 if (putfont(fd, buf, fontsize, width, hwunit))
305 do_loadtable(int fd, struct unicode_list *uclistheads, int fontsize) {
306 struct unimapdesc ud;
308 int i, ct = 0, maxct;
309 struct unicode_list *ul;
310 struct unicode_seq *us;
313 for (i = 0; i < fontsize; i++) {
314 ul = uclistheads[i].next;
317 if (us && ! us->next)
322 up = xmalloc(maxct * sizeof(struct unipair));
323 for (i = 0; i < fontsize; i++) {
324 ul = uclistheads[i].next;
327 if (us && ! us->next) {
328 up[ct].unicode = us->uc;
336 char *u = _("%s: bug in do_loadtable\n");
337 fprintf(stderr, u, progname);
342 printf(_("Loading Unicode mapping table...\n"));
346 if (loadunimap(fd, NULL, &ud))
351 loadnewfonts(int fd, char **ifiles, int ifilct,
352 int iunit, int hwunit, int no_m, int no_u) {
354 char *ifil, *inbuf, *fontbuf, *bigfontbuf;
355 int inputlth, fontbuflth, fontsize, height, width, bytewidth;
356 int bigfontbuflth, bigfontsize, bigheight, bigwidth;
357 struct unicode_list *uclistheads;
361 loadnewfont(fd, ifiles[0], iunit, hwunit, no_m, no_u);
365 /* several fonts that must be merged */
366 /* We just concatenate the bitmaps - only allow psf fonts */
374 for (i=0; i<ifilct; i++) {
376 if ((fpi = findfont(ifil)) == NULL &&
377 (fpi = findpartialfont(ifil)) == NULL) {
378 fprintf(stderr, _("Cannot open font file %s\n"), ifil);
382 inbuf = fontbuf = NULL;
383 inputlth = fontbuflth = 0;
386 if(readpsffont(fpi, &inbuf, &inputlth, &fontbuf, &fontbuflth,
387 &width, &fontsize, bigfontsize,
388 no_u ? NULL : &uclistheads)) {
389 fprintf(stderr, _("When loading several fonts, all "
390 "must be psf fonts - %s isn't\n"),
394 bytewidth = (width+7) / 8;
395 height = fontbuflth / (bytewidth * fontsize);
397 printf(_("Read %d-char %dx%d font from file %s\n"),
398 fontsize, width, height, pathname);
402 else if (bigheight != height) {
403 fprintf(stderr, _("When loading several fonts, all "
404 "must have the same height\n"));
409 else if (bigwidth != width) {
410 fprintf(stderr, _("When loading several fonts, all "
411 "must have the same width\n"));
415 bigfontsize += fontsize;
416 bigfontbuflth += fontbuflth;
417 bigfontbuf = xrealloc(bigfontbuf, bigfontbuflth);
418 memcpy(bigfontbuf+bigfontbuflth-fontbuflth,
419 fontbuf, fontbuflth);
421 do_loadfont(fd, bigfontbuf, bigwidth, bigheight, hwunit,
424 if (uclistheads && !no_u)
425 do_loadtable(fd, uclistheads, bigfontsize);
429 loadnewfont(int fd, char *ifil, int iunit, int hwunit, int no_m, int no_u) {
432 int height, width, bytewidth, def = 0;
433 char *inbuf, *fontbuf;
434 int inputlth, fontbuflth, fontsize, offset;
435 struct unicode_list *uclistheads;
438 /* try to find some default file */
440 def = 1; /* maybe also load default unimap */
442 if (iunit < 0 || iunit > 32)
445 if ((fpi = findfont(ifil = "default")) == NULL &&
446 (fpi = findfont(ifil = "default8x16")) == NULL &&
447 (fpi = findfont(ifil = "default8x14")) == NULL &&
448 (fpi = findfont(ifil = "default8x8")) == NULL) {
449 fprintf(stderr, _("Cannot find default font\n"));
453 sprintf(defname, "default8x%d", iunit);
454 if ((fpi = findfont(ifil = defname)) == NULL &&
455 (fpi = findfont(ifil = "default")) == NULL) {
456 fprintf(stderr, _("Cannot find %s font\n"), ifil);
461 if ((fpi = findfont(ifil)) == NULL) {
462 fprintf(stderr, _("Cannot open font file %s\n"), ifil);
468 printf(_("Reading font file %s\n"), ifil);
470 inbuf = fontbuf = NULL;
471 inputlth = fontbuflth = fontsize = 0;
474 if(readpsffont(fpi, &inbuf, &inputlth, &fontbuf, &fontbuflth,
475 &width, &fontsize, 0,
476 no_u ? NULL : &uclistheads) == 0) {
477 /* we've got a psf font */
478 bytewidth = (width+7) / 8;
479 height = fontbuflth / (bytewidth * fontsize);
481 do_loadfont(fd, fontbuf, width, height, hwunit,
483 if (uclistheads && !no_u)
484 do_loadtable(fd, uclistheads, fontsize);
486 if (!uclistheads && !no_u && def)
487 loadunicodemap(fd, "def.uni");
492 /* instructions to combine fonts? */
493 { char *combineheader = "# combine partial fonts\n";
494 int chlth = strlen(combineheader);
496 if (inputlth >= chlth && !strncmp(inbuf, combineheader, chlth)) {
497 char *ifiles[MAXIFILES];
500 while(q < inbuf + inputlth) {
502 while (q < inbuf+inputlth && *q != '\n')
504 if (q == inbuf+inputlth) {
506 _("No final newline in combine file\n"));
510 if (ifilct == MAXIFILES) {
512 _("Too many files to combine\n"));
515 ifiles[ifilct++] = p;
518 loadnewfonts(fd, ifiles, ifilct, iunit, hwunit, no_m, no_u);
523 /* file with three code pages? */
524 if (inputlth == 9780) {
525 offset = position_codepage(iunit);
529 } else if (inputlth == 32768) {
530 /* restorefont -w writes a SVGA font to file
531 restorefont -r restores it
532 These fonts have size 32768, for two 512-char fonts.
533 In fact, when BROKEN_GRAPHICS_PROGRAMS is defined,
534 and it always is, there is no default font that is saved,
535 so probably the second half is always garbage. */
536 fprintf(stderr, _("Hmm - a font from restorefont? "
537 "Using the first half.\n"));
538 inputlth = 16384; /* ignore rest */
546 int rem = (inputlth % 256);
547 if (rem == 0 || rem == 40) {
548 /* 0: bare code page bitmap */
549 /* 40: preceded by .cp header */
550 /* we might check some header details */
553 fprintf(stderr, _("Bad input file size\n"));
558 height = inputlth/256;
560 do_loadfont(fd, inbuf+offset, width, height, hwunit, fontsize,
565 position_codepage(int iunit) {
568 /* code page: first 40 bytes, then 8x16 font,
569 then 6 bytes, then 8x14 font,
570 then 6 bytes, then 8x8 font */
574 _("This file contains 3 fonts: 8x8, 8x14 and 8x16."
576 "using an option -8 or -14 or -16 "
577 "which one you want loaded.\n"));
582 offset = 7732; break;
584 offset = 4142; break;
588 fprintf(stderr, _("You asked for font size %d, "
589 "but only 8, 14, 16 are possible here.\n"),
597 do_saveoldfont(int fd, char *ofil, FILE *fpo, int unimap_follows,
598 int *count, int *utf8) {
600 /* this is the max font size the kernel is willing to handle */
601 #define MAXFONTSIZE 65536
602 char buf[MAXFONTSIZE];
604 int i, ct, width, height, bytewidth, charsize, kcharsize;
606 ct = sizeof(buf)/(32*32/8); /* max size 32x32, 8 bits/byte */
607 if (getfont(fd, buf, &ct, &width, &height))
610 /* save as efficiently as possible */
611 bytewidth = (width + 7) / 8;
612 height = font_charheight(buf, ct, width);
613 charsize = height * bytewidth;
614 kcharsize = 32 * bytewidth;
616 /* Do we need a psf header? */
617 /* Yes if ct==512 - otherwise we cannot distinguish
618 a 512-char 8x8 and a 256-char 8x16 font. */
619 #define ALWAYS_PSF_HEADER 1
621 if (ct != 256 || width != 8 || unimap_follows || ALWAYS_PSF_HEADER) {
626 flags |= WPSFH_HASTAB;
627 writepsffontheader (fpo, width, height, ct, &psftype, flags);
629 *utf8 = (psftype == 2);
633 fprintf(stderr, _("Found nothing to save\n"));
635 for (i = 0; i < ct; i++) {
636 if (fwrite(buf+(i*kcharsize), charsize, 1, fpo) != 1) {
637 fprintf(stderr, _("Cannot write font file"));
642 printf(_("Saved %d-char %dx%d font file on %s\n"),
643 ct, width, height, ofil);
652 saveoldfont(int fd, char *ofil) {
655 if((fpo = fopen(ofil, "w")) == NULL) {
659 do_saveoldfont(fd, ofil, fpo, 0, NULL, NULL);
664 saveoldfontplusunicodemap(int fd, char *Ofil) {
669 if((fpo = fopen(Ofil, "w")) == NULL) {
674 do_saveoldfont(fd, Ofil, fpo, 1, &ct, &utf8);
675 appendunicodemap(fd, fpo, ct, utf8);
679 /* Only on the current console? On all allocated consoles? */
680 /* A newly allocated console has NORM_MAP by default -
681 probably it should copy the default from the current console?
682 But what if we want a new one because the current one is messed up? */
683 /* For the moment: only the current console, only the G0 set */
690 disactivatemap(void) {