psf2png.c in git
PSF2PNG(1) FreeBSD General Commands Manual PSF2PNG(1)
NAME
psf2png – PSF2 to PNG renderer
SYNOPSIS
psf2png [-b bg] [-c cols] [-f fg] [-s str] [file]
DESCRIPTION
psf2png renders the PSF2 font file or standard input to PNG on standard
output.
The arguments are as follows:
-b bg Use bg (hexadecimal RGB) as background color. The default
background color is black.
-c cols
Arrange glyphs in cols columns. The default number of columns is
32.
-f fg Use fg (hexadecimal RGB) as foreground color. The default
foreground color is white.
-s str Render glyphs for string str rather than all glyphs.
SEE ALSO
pngo(1), psfed(1)
FreeBSD 12.0-RELEASE-p10 September 28, 2018 FreeBSD 12.0-RELEASE-p10
#include <err.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include "png.h"
int main(int argc, char *argv[]) {
uint32_t cols = 32;
const char *str = NULL;
uint32_t fg = 0xFFFFFF;
uint32_t bg = 0x000000;
int opt;
while (0 < (opt = getopt(argc, argv, "b:c:f:s:"))) {
switch (opt) {
break; case 'b': bg = strtoul(optarg, NULL, 16);
break; case 'c': cols = strtoul(optarg, NULL, 0);
break; case 'f': fg = strtoul(optarg, NULL, 16);
break; case 's': str = optarg;
break; default: return EX_USAGE;
}
}
if (!cols && str) cols = strlen(str);
if (!cols) return EX_USAGE;
const char *path = NULL;
if (optind < argc) path = argv[optind];
FILE *file = path ? fopen(path, "r") : stdin;
if (!file) err(EX_NOINPUT, "%s", path);
if (!path) path = "(stdin)";
struct {
uint32_t magic;
uint32_t version;
uint32_t size;
uint32_t flags;
struct {
uint32_t len;
uint32_t size;
uint32_t height;
uint32_t width;
} glyph;
} header;
size_t len = fread(&header, sizeof(header), 1, file);
if (ferror(file)) err(EX_IOERR, "%s", path);
if (len < 1) errx(EX_DATAERR, "%s: truncated header", path);
uint32_t widthBytes = (header.glyph.width + 7) / 8;
uint8_t glyphs[header.glyph.len][header.glyph.height][widthBytes];
len = fread(glyphs, header.glyph.size, header.glyph.len, file);
if (ferror(file)) err(EX_IOERR, "%s", path);
if (len < header.glyph.len) {
errx(EX_DATAERR, "%s: truncated glyphs", path);
}
fclose(file);
uint32_t count = (str ? strlen(str) : header.glyph.len);
uint32_t width = header.glyph.width * cols;
uint32_t rows = (count + cols - 1) / cols;
uint32_t height = header.glyph.height * rows;
pngHead(stdout, width, height, 8, PNGIndexed);
uint8_t pal[] = {
bg >> 16, bg >> 8, bg,
fg >> 16, fg >> 8, fg,
};
pngPalette(stdout, pal, sizeof(pal));
uint8_t data[height][1 + width];
memset(data, PNGNone, sizeof(data));
for (uint32_t i = 0; i < count; ++i) {
uint32_t row = header.glyph.height * (i / cols);
uint32_t col = 1 + header.glyph.width * (i % cols);
uint32_t g = (str ? str[i] : i);
for (uint32_t y = 0; y < header.glyph.height; ++y) {
for (uint32_t x = 0; x < header.glyph.width; ++x) {
uint8_t bit = glyphs[g][y][x / 8] >> (7 - x % 8) & 1;
data[row + y][col + x] = bit;
}
}
}
pngData(stdout, (uint8_t *)data, sizeof(data));
pngTail(stdout);
}