The repository formerly known as dotfiles
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.

psf2png.c 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Copyright (C) 2018 Curtis McEnroe <june@causal.agency>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <err.h>
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <sysexits.h>
  22. #include <unistd.h>
  23. #include "png.h"
  24. int main(int argc, char *argv[]) {
  25. uint32_t cols = 32;
  26. const char *str = NULL;
  27. uint32_t fg = 0xFFFFFF;
  28. uint32_t bg = 0x000000;
  29. int opt;
  30. while (0 < (opt = getopt(argc, argv, "b:c:f:s:"))) {
  31. switch (opt) {
  32. break; case 'b': bg = strtoul(optarg, NULL, 16);
  33. break; case 'c': cols = strtoul(optarg, NULL, 0);
  34. break; case 'f': fg = strtoul(optarg, NULL, 16);
  35. break; case 's': str = optarg;
  36. break; default: return EX_USAGE;
  37. }
  38. }
  39. if (!cols && str) cols = strlen(str);
  40. if (!cols) return EX_USAGE;
  41. const char *path = NULL;
  42. if (optind < argc) path = argv[optind];
  43. FILE *file = path ? fopen(path, "r") : stdin;
  44. if (!file) err(EX_NOINPUT, "%s", path);
  45. if (!path) path = "(stdin)";
  46. struct {
  47. uint32_t magic;
  48. uint32_t version;
  49. uint32_t size;
  50. uint32_t flags;
  51. struct {
  52. uint32_t len;
  53. uint32_t size;
  54. uint32_t height;
  55. uint32_t width;
  56. } glyph;
  57. } header;
  58. size_t len = fread(&header, sizeof(header), 1, file);
  59. if (ferror(file)) err(EX_IOERR, "%s", path);
  60. if (len < 1) errx(EX_DATAERR, "%s: truncated header", path);
  61. uint32_t widthBytes = (header.glyph.width + 7) / 8;
  62. uint8_t glyphs[header.glyph.len][header.glyph.height][widthBytes];
  63. len = fread(glyphs, header.glyph.size, header.glyph.len, file);
  64. if (ferror(file)) err(EX_IOERR, "%s", path);
  65. if (len < header.glyph.len) {
  66. errx(EX_DATAERR, "%s: truncated glyphs", path);
  67. }
  68. fclose(file);
  69. uint32_t count = (str ? strlen(str) : header.glyph.len);
  70. uint32_t width = header.glyph.width * cols;
  71. uint32_t rows = (count + cols - 1) / cols;
  72. uint32_t height = header.glyph.height * rows;
  73. pngHead(stdout, width, height, 8, PNGIndexed);
  74. uint8_t pal[] = {
  75. bg >> 16, bg >> 8, bg,
  76. fg >> 16, fg >> 8, fg,
  77. };
  78. pngPalette(stdout, pal, sizeof(pal));
  79. uint8_t data[height][1 + width];
  80. memset(data, PNGNone, sizeof(data));
  81. for (uint32_t i = 0; i < count; ++i) {
  82. uint32_t row = header.glyph.height * (i / cols);
  83. uint32_t col = 1 + header.glyph.width * (i % cols);
  84. uint32_t g = (str ? str[i] : i);
  85. for (uint32_t y = 0; y < header.glyph.height; ++y) {
  86. for (uint32_t x = 0; x < header.glyph.width; ++x) {
  87. uint8_t bit = glyphs[g][y][x / 8] >> (7 - x % 8) & 1;
  88. data[row + y][col + x] = bit;
  89. }
  90. }
  91. }
  92. pngData(stdout, (uint8_t *)data, sizeof(data));
  93. pngTail(stdout);
  94. }