3
0

loadfont.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * loadfont.c - Eugene Crosser & Andries Brouwer
  4. *
  5. * Version 0.96bb
  6. *
  7. * Loads the console font, and possibly the corresponding screen map(s).
  8. * (Adapted for busybox by Matej Vela.)
  9. *
  10. * Licensed under GPLv2, see file LICENSE in this source tree.
  11. */
  12. //usage:#define loadfont_trivial_usage
  13. //usage: "< font"
  14. //usage:#define loadfont_full_usage "\n\n"
  15. //usage: "Load a console font from stdin"
  16. /* //usage: "\n -C TTY Affect TTY instead of /dev/tty" */
  17. //usage:
  18. //usage:#define loadfont_example_usage
  19. //usage: "$ loadfont < /etc/i18n/fontname\n"
  20. //usage:
  21. //usage:#define setfont_trivial_usage
  22. //usage: "FONT [-m MAPFILE] [-C TTY]"
  23. //usage:#define setfont_full_usage "\n\n"
  24. //usage: "Load a console font\n"
  25. //usage: "\n -m MAPFILE Load console screen map"
  26. //usage: "\n -C TTY Affect TTY instead of /dev/tty"
  27. //usage:
  28. //usage:#define setfont_example_usage
  29. //usage: "$ setfont -m koi8-r /etc/i18n/fontname\n"
  30. #include "libbb.h"
  31. #include <sys/kd.h>
  32. #ifndef KDFONTOP
  33. # define KDFONTOP 0x4B72
  34. struct console_font_op {
  35. unsigned op; /* KD_FONT_OP_* */
  36. unsigned flags; /* KD_FONT_FLAG_* */
  37. unsigned width, height;
  38. unsigned charcount;
  39. unsigned char *data; /* font data with height fixed to 32 */
  40. };
  41. # define KD_FONT_OP_SET 0 /* Set font */
  42. # define KD_FONT_OP_GET 1 /* Get font */
  43. # define KD_FONT_OP_SET_DEFAULT 2 /* Set font to default, data points to name / NULL */
  44. # define KD_FONT_OP_COPY 3 /* Copy from another console */
  45. # define KD_FONT_FLAG_OLD 0x80000000 /* Invoked via old interface */
  46. # define KD_FONT_FLAG_DONT_RECALC 1 /* Don't call adjust_height() */
  47. /* (Used internally for PIO_FONT support) */
  48. #endif /* KDFONTOP */
  49. enum {
  50. PSF1_MAGIC0 = 0x36,
  51. PSF1_MAGIC1 = 0x04,
  52. PSF1_MODE512 = 0x01,
  53. PSF1_MODEHASTAB = 0x02,
  54. PSF1_MODEHASSEQ = 0x04,
  55. PSF1_MAXMODE = 0x05,
  56. PSF1_STARTSEQ = 0xfffe,
  57. PSF1_SEPARATOR = 0xffff,
  58. };
  59. struct psf1_header {
  60. unsigned char magic[2]; /* Magic number */
  61. unsigned char mode; /* PSF font mode */
  62. unsigned char charsize; /* Character size */
  63. };
  64. #define psf1h(x) ((struct psf1_header*)(x))
  65. #define PSF1_MAGIC_OK(x) ( \
  66. (x)->magic[0] == PSF1_MAGIC0 \
  67. && (x)->magic[1] == PSF1_MAGIC1 \
  68. )
  69. #if ENABLE_FEATURE_LOADFONT_PSF2
  70. enum {
  71. PSF2_MAGIC0 = 0x72,
  72. PSF2_MAGIC1 = 0xb5,
  73. PSF2_MAGIC2 = 0x4a,
  74. PSF2_MAGIC3 = 0x86,
  75. PSF2_HAS_UNICODE_TABLE = 0x01,
  76. PSF2_MAXVERSION = 0,
  77. PSF2_STARTSEQ = 0xfe,
  78. PSF2_SEPARATOR = 0xff
  79. };
  80. struct psf2_header {
  81. unsigned char magic[4];
  82. unsigned int version;
  83. unsigned int headersize; /* offset of bitmaps in file */
  84. unsigned int flags;
  85. unsigned int length; /* number of glyphs */
  86. unsigned int charsize; /* number of bytes for each character */
  87. unsigned int height; /* max dimensions of glyphs */
  88. unsigned int width; /* charsize = height * ((width + 7) / 8) */
  89. };
  90. #define psf2h(x) ((struct psf2_header*)(x))
  91. #define PSF2_MAGIC_OK(x) ( \
  92. (x)->magic[0] == PSF2_MAGIC0 \
  93. && (x)->magic[1] == PSF2_MAGIC1 \
  94. && (x)->magic[2] == PSF2_MAGIC2 \
  95. && (x)->magic[3] == PSF2_MAGIC3 \
  96. )
  97. #endif /* ENABLE_FEATURE_LOADFONT_PSF2 */
  98. static void do_loadfont(int fd, unsigned char *inbuf, int height, int width, int charsize, int fontsize)
  99. {
  100. unsigned char *buf;
  101. int charwidth = 32 * ((width+7)/8);
  102. int i;
  103. if (height < 1 || height > 32 || width < 1 || width > 32)
  104. bb_error_msg_and_die("bad character size %dx%d", height, width);
  105. buf = xzalloc(charwidth * ((fontsize < 128) ? 128 : fontsize));
  106. for (i = 0; i < fontsize; i++)
  107. memcpy(buf + (i*charwidth), inbuf + (i*charsize), charsize);
  108. { /* KDFONTOP */
  109. struct console_font_op cfo;
  110. cfo.op = KD_FONT_OP_SET;
  111. cfo.flags = 0;
  112. cfo.width = width;
  113. cfo.height = height;
  114. cfo.charcount = fontsize;
  115. cfo.data = buf;
  116. xioctl(fd, KDFONTOP, &cfo);
  117. }
  118. free(buf);
  119. }
  120. /*
  121. * Format of the Unicode information:
  122. *
  123. * For each font position <uc>*<seq>*<term>
  124. * where <uc> is a 2-byte little endian Unicode value (PSF1)
  125. * or an UTF-8 coded value (PSF2),
  126. * <seq> = <ss><uc><uc>*, <ss> = psf1 ? 0xFFFE : 0xFE,
  127. * <term> = psf1 ? 0xFFFF : 0xFF.
  128. * and * denotes zero or more occurrences of the preceding item.
  129. *
  130. * Semantics:
  131. * The leading <uc>* part gives Unicode symbols that are all
  132. * represented by this font position. The following sequences
  133. * are sequences of Unicode symbols - probably a symbol
  134. * together with combining accents - also represented by
  135. * this font position.
  136. *
  137. * Example:
  138. * At the font position for a capital A-ring glyph, we
  139. * may have:
  140. * 00C5,212B,FFFE,0041,030A,FFFF
  141. * Some font positions may be described by sequences only,
  142. * namely when there is no precomposed Unicode value for the glyph.
  143. */
  144. #if !ENABLE_FEATURE_LOADFONT_PSF2
  145. #define do_loadtable(fd, inbuf, tailsz, fontsize, psf2) \
  146. do_loadtable(fd, inbuf, tailsz, fontsize)
  147. #endif
  148. static void do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize, int psf2)
  149. {
  150. #if !ENABLE_FEATURE_LOADFONT_PSF2
  151. /* gcc 4.3.1 code size: */
  152. # define psf2 0 /* +0 bytes */
  153. // const int psf2 = 0; /* +8 bytes */
  154. // enum { psf2 = 0 }; /* +13 bytes */
  155. #endif
  156. struct unimapinit advice;
  157. struct unimapdesc ud;
  158. struct unipair *up;
  159. int ct = 0, maxct;
  160. int glyph;
  161. uint16_t unicode;
  162. maxct = tailsz; /* more than enough */
  163. up = xmalloc(maxct * sizeof(*up));
  164. for (glyph = 0; glyph < fontsize; glyph++) {
  165. while (tailsz > 0) {
  166. if (!psf2) { /* PSF1 */
  167. unicode = (((uint16_t) inbuf[1]) << 8) + inbuf[0];
  168. tailsz -= 2;
  169. inbuf += 2;
  170. if (unicode == PSF1_SEPARATOR)
  171. break;
  172. } else { /* PSF2 */
  173. #if ENABLE_FEATURE_LOADFONT_PSF2
  174. --tailsz;
  175. unicode = *inbuf++;
  176. if (unicode == PSF2_SEPARATOR) {
  177. break;
  178. } else if (unicode == PSF2_STARTSEQ) {
  179. bb_error_msg_and_die("unicode sequences not implemented");
  180. } else if (unicode >= 0xC0) {
  181. if (unicode >= 0xFC)
  182. unicode &= 0x01, maxct = 5;
  183. else if (unicode >= 0xF8)
  184. unicode &= 0x03, maxct = 4;
  185. else if (unicode >= 0xF0)
  186. unicode &= 0x07, maxct = 3;
  187. else if (unicode >= 0xE0)
  188. unicode &= 0x0F, maxct = 2;
  189. else
  190. unicode &= 0x1F, maxct = 1;
  191. do {
  192. if (tailsz <= 0 || *inbuf < 0x80 || *inbuf > 0xBF)
  193. bb_error_msg_and_die("illegal UTF-8 character");
  194. --tailsz;
  195. unicode = (unicode << 6) + (*inbuf++ & 0x3F);
  196. } while (--maxct > 0);
  197. } else if (unicode >= 0x80) {
  198. bb_error_msg_and_die("illegal UTF-8 character");
  199. }
  200. #else
  201. return;
  202. #endif
  203. }
  204. up[ct].unicode = unicode;
  205. up[ct].fontpos = glyph;
  206. ct++;
  207. }
  208. }
  209. /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP
  210. * this printf did not work on many kernels */
  211. advice.advised_hashsize = 0;
  212. advice.advised_hashstep = 0;
  213. advice.advised_hashlevel = 0;
  214. xioctl(fd, PIO_UNIMAPCLR, &advice);
  215. ud.entry_ct = ct;
  216. ud.entries = up;
  217. xioctl(fd, PIO_UNIMAP, &ud);
  218. #undef psf2
  219. }
  220. static void do_load(int fd, unsigned char *buffer, size_t len)
  221. {
  222. int height;
  223. int width = 8;
  224. int charsize;
  225. int fontsize = 256;
  226. int has_table = 0;
  227. unsigned char *font = buffer;
  228. unsigned char *table;
  229. if (len >= sizeof(struct psf1_header) && PSF1_MAGIC_OK(psf1h(buffer))) {
  230. if (psf1h(buffer)->mode > PSF1_MAXMODE)
  231. bb_error_msg_and_die("unsupported psf file mode");
  232. if (psf1h(buffer)->mode & PSF1_MODE512)
  233. fontsize = 512;
  234. if (psf1h(buffer)->mode & PSF1_MODEHASTAB)
  235. has_table = 1;
  236. height = charsize = psf1h(buffer)->charsize;
  237. font += sizeof(struct psf1_header);
  238. } else
  239. #if ENABLE_FEATURE_LOADFONT_PSF2
  240. if (len >= sizeof(struct psf2_header) && PSF2_MAGIC_OK(psf2h(buffer))) {
  241. if (psf2h(buffer)->version > PSF2_MAXVERSION)
  242. bb_error_msg_and_die("unsupported psf file version");
  243. fontsize = psf2h(buffer)->length;
  244. if (psf2h(buffer)->flags & PSF2_HAS_UNICODE_TABLE)
  245. has_table = 2;
  246. charsize = psf2h(buffer)->charsize;
  247. height = psf2h(buffer)->height;
  248. width = psf2h(buffer)->width;
  249. font += psf2h(buffer)->headersize;
  250. } else
  251. #endif
  252. #if ENABLE_FEATURE_LOADFONT_RAW
  253. if (len == 9780) { /* file with three code pages? */
  254. charsize = height = 16;
  255. font += 40;
  256. } else if ((len & 0377) == 0) { /* bare font */
  257. charsize = height = len / 256;
  258. } else
  259. #endif
  260. {
  261. bb_error_msg_and_die("input file: bad length or unsupported font type");
  262. }
  263. #if !defined(PIO_FONTX) || defined(__sparc__)
  264. if (fontsize != 256)
  265. bb_error_msg_and_die("only fontsize 256 supported");
  266. #endif
  267. table = font + fontsize * charsize;
  268. buffer += len;
  269. if (table > buffer || (!has_table && table != buffer))
  270. bb_error_msg_and_die("input file: bad length");
  271. do_loadfont(fd, font, height, width, charsize, fontsize);
  272. if (has_table)
  273. do_loadtable(fd, table, buffer - table, fontsize, has_table - 1);
  274. }
  275. #if ENABLE_LOADFONT
  276. int loadfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  277. int loadfont_main(int argc UNUSED_PARAM, char **argv)
  278. {
  279. size_t len;
  280. unsigned char *buffer;
  281. // no arguments allowed!
  282. opt_complementary = "=0";
  283. getopt32(argv, "");
  284. /*
  285. * We used to look at the length of the input file
  286. * with stat(); now that we accept compressed files,
  287. * just read the entire file.
  288. */
  289. len = 32*1024; // can't be larger
  290. buffer = xmalloc_read(STDIN_FILENO, &len);
  291. // xmalloc_open_zipped_read_close(filename, &len);
  292. if (!buffer)
  293. bb_perror_msg_and_die("error reading input font");
  294. do_load(get_console_fd_or_die(), buffer, len);
  295. return EXIT_SUCCESS;
  296. }
  297. #endif
  298. #if ENABLE_SETFONT
  299. /*
  300. kbd-1.12:
  301. setfont [-O font+umap.orig] [-o font.orig] [-om cmap.orig]
  302. [-ou umap.orig] [-N] [font.new ...] [-m cmap] [-u umap] [-C console]
  303. [-hNN] [-v] [-V]
  304. -h NN Override font height
  305. -o file
  306. Save previous font in file
  307. -O file
  308. Save previous font and Unicode map in file
  309. -om file
  310. Store console map in file
  311. -ou file
  312. Save previous Unicode map in file
  313. -m file
  314. Load console map or Unicode console map from file
  315. -u file
  316. Load Unicode table describing the font from file
  317. Example:
  318. # cp866
  319. 0x00-0x7f idem
  320. #
  321. 0x80 U+0410 # CYRILLIC CAPITAL LETTER A
  322. 0x81 U+0411 # CYRILLIC CAPITAL LETTER BE
  323. 0x82 U+0412 # CYRILLIC CAPITAL LETTER VE
  324. -C console
  325. Set the font for the indicated console
  326. -v Verbose
  327. -V Version
  328. */
  329. #if ENABLE_FEATURE_SETFONT_TEXTUAL_MAP
  330. static int ctoi(char *s)
  331. {
  332. if (s[0] == '\'' && s[1] != '\0' && s[2] == '\'' && s[3] == '\0')
  333. return s[1];
  334. // U+ means 0x
  335. if (s[0] == 'U' && s[1] == '+') {
  336. s[0] = '0';
  337. s[1] = 'x';
  338. }
  339. if (!isdigit(s[0]))
  340. return -1;
  341. return xstrtoul(s, 0);
  342. }
  343. #endif
  344. int setfont_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  345. int setfont_main(int argc UNUSED_PARAM, char **argv)
  346. {
  347. size_t len;
  348. unsigned opts;
  349. int fd;
  350. unsigned char *buffer;
  351. char *mapfilename;
  352. const char *tty_name = CURRENT_TTY;
  353. opt_complementary = "=1";
  354. opts = getopt32(argv, "m:C:", &mapfilename, &tty_name);
  355. argv += optind;
  356. fd = xopen_nonblocking(tty_name);
  357. if (sizeof(CONFIG_DEFAULT_SETFONT_DIR) > 1) { // if not ""
  358. if (*argv[0] != '/') {
  359. // goto default fonts location. don't die if doesn't exist
  360. chdir(CONFIG_DEFAULT_SETFONT_DIR "/consolefonts");
  361. }
  362. }
  363. // load font
  364. len = 32*1024; // can't be larger
  365. buffer = xmalloc_open_zipped_read_close(*argv, &len);
  366. if (!buffer)
  367. bb_simple_perror_msg_and_die(*argv);
  368. do_load(fd, buffer, len);
  369. // load the screen map, if any
  370. if (opts & 1) { // -m
  371. unsigned mode = PIO_SCRNMAP;
  372. void *map;
  373. if (sizeof(CONFIG_DEFAULT_SETFONT_DIR) > 1) { // if not ""
  374. if (mapfilename[0] != '/') {
  375. // goto default keymaps location
  376. chdir(CONFIG_DEFAULT_SETFONT_DIR "/consoletrans");
  377. }
  378. }
  379. // fetch keymap
  380. map = xmalloc_open_zipped_read_close(mapfilename, &len);
  381. if (!map)
  382. bb_simple_perror_msg_and_die(mapfilename);
  383. // file size is 256 or 512 bytes? -> assume binary map
  384. if (len == E_TABSZ || len == 2*E_TABSZ) {
  385. if (len == 2*E_TABSZ)
  386. mode = PIO_UNISCRNMAP;
  387. }
  388. #if ENABLE_FEATURE_SETFONT_TEXTUAL_MAP
  389. // assume textual Unicode console maps:
  390. // 0x00 U+0000 # NULL (NUL)
  391. // 0x01 U+0001 # START OF HEADING (SOH)
  392. // 0x02 U+0002 # START OF TEXT (STX)
  393. // 0x03 U+0003 # END OF TEXT (ETX)
  394. else {
  395. int i;
  396. char *token[2];
  397. parser_t *parser;
  398. if (ENABLE_FEATURE_CLEAN_UP)
  399. free(map);
  400. map = xmalloc(E_TABSZ * sizeof(unsigned short));
  401. #define unicodes ((unsigned short *)map)
  402. // fill vanilla map
  403. for (i = 0; i < E_TABSZ; i++)
  404. unicodes[i] = 0xf000 + i;
  405. parser = config_open(mapfilename);
  406. while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL | PARSE_MIN_DIE)) {
  407. // parse code/value pair
  408. int a = ctoi(token[0]);
  409. int b = ctoi(token[1]);
  410. if (a < 0 || a >= E_TABSZ
  411. || b < 0 || b > 65535
  412. ) {
  413. bb_error_msg_and_die("map format");
  414. }
  415. // patch map
  416. unicodes[a] = b;
  417. // unicode character is met?
  418. if (b > 255)
  419. mode = PIO_UNISCRNMAP;
  420. }
  421. if (ENABLE_FEATURE_CLEAN_UP)
  422. config_close(parser);
  423. if (mode != PIO_UNISCRNMAP) {
  424. #define asciis ((unsigned char *)map)
  425. for (i = 0; i < E_TABSZ; i++)
  426. asciis[i] = unicodes[i];
  427. #undef asciis
  428. }
  429. #undef unicodes
  430. }
  431. #endif // ENABLE_FEATURE_SETFONT_TEXTUAL_MAP
  432. // do set screen map
  433. xioctl(fd, mode, map);
  434. if (ENABLE_FEATURE_CLEAN_UP)
  435. free(map);
  436. }
  437. return EXIT_SUCCESS;
  438. }
  439. #endif