Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.9KB

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <libgen.h>
  5. /* This script converts "space-dot" fonts to binary "glyph rows". One byte for
  6. * each row. In a 5x7 font, each glyph thus use 7 bytes.
  7. * Resulting bytes are aligned to the **left** of the byte. Therefore, for
  8. * a 5-bit wide char, ". . ." translates to 0b10101000
  9. * Left-aligned bytes are easier to work with when compositing glyphs.
  10. */
  11. int main(int argc, char **argv)
  12. {
  13. if (argc != 2) {
  14. fprintf(stderr, "Usage: ./fontcompile fpath\n");
  15. return 1;
  16. }
  17. char *fn = basename(argv[1]);
  18. if (!fn) {
  19. return 1;
  20. }
  21. int w = 0;
  22. if ((fn[0] >= '3') && (fn[0] <= '8')) {
  23. w = fn[0] - '0';
  24. }
  25. int h = 0;
  26. if ((fn[2] >= '3') && (fn[2] <= '8')) {
  27. h = fn[2] - '0';
  28. }
  29. if (!w || !h || fn[1] != 'x') {
  30. fprintf(stderr, "Not a font filename: (3-8)x(3-8).txt.\n");
  31. return 1;
  32. }
  33. fprintf(stderr, "Reading a %d x %d font\n", w, h);
  34. FILE *fp = fopen(argv[1], "r");
  35. if (!fp) {
  36. fprintf(stderr, "Can't open %s.\n", argv[1]);
  37. return 1;
  38. }
  39. // We start the binary data with our first char, space, which is not in our
  40. // input but needs to be in our output.
  41. for (int i=0; i<h; i++) {
  42. putchar(0);
  43. }
  44. int lineno = 1;
  45. char buf[0x10];
  46. while (fgets(buf, 0x10, fp)) {
  47. size_t l = strlen(buf);
  48. if (l > w+1) { // +1 because of the newline char.
  49. fprintf(stderr, "Line %d too long.\n", lineno);
  50. fclose(fp);
  51. return 1;
  52. }
  53. // line can be narrower than width. It's padded with spaces.
  54. while (l < w+1) {
  55. buf[l] = ' ';
  56. l++;
  57. }
  58. unsigned char c = 0;
  59. for (int i=0; i<w; i++) {
  60. if (buf[i] == '.') {
  61. c |= (1 << (7-i));
  62. }
  63. }
  64. putchar(c);
  65. lineno++;
  66. }
  67. fclose(fp);
  68. return 0;
  69. }