loadfont.c 12 KB

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