Here's a simple perl script to convert BDF fonts to C structs:
#!/usr/bin/perl -n
$FONTNAME = "typeface";
sub pad {
my $count = shift;
while($count-- >= 0) { print ", 0x00"; }
}
if (/^FONTBOUNDINGBOX\s+(\d+)\s+(\d+)/) { $HEIGHT = $2; printf("struct glyph {char c; uint8_t d[%d];};\n", $HEIGHT); printf("glyph %s[] = {\n", $FONTNAME); }
elsif (/^STARTCHAR\s+(.)$/) { $INCHAR=1; printf(" '%s', ", $1); }
elsif (/^BITMAP/ && $INCHAR) { $BITMAP=1; }
elsif (/^ENDCHAR/ && $INCHAR) { pad($HEIGHT - $BITMAP); print " },\n"; $BITMAP=0; $INCHAR=0; }
elsif (/^ENDFONT/) { print " '\\0', { 0x00"; pad($HEIGHT-2); print " }\n"; print "};\n"; }
elsif ($BITMAP) { s/(.+)\n$//; printf($BITMAP++ == 1 ? "{ 0x%s" : ", 0x%s", $1); }
I'm not really a perl coder, but it works and produces output such as this (from the X11 5x3 'micro' font):
struct glyph {char c; uint8_t d[5];};
glyph micro_font[] = {
'0', { 0x40, 0xa0, 0xa0, 0xa0, 0x40 },
'1', { 0x40, 0xc0, 0x40, 0x40, 0x40 },
...
'\0', { 0x00, 0x00, 0x00, 0x00, 0x00 }
};
The array is terminated with a null character so that it can be iterated/searched through.
The bitmaps in BDF format are done one byte per line.
It would probably be better to use one byte per column - since fonts are generally higher than they are wide, a 5x3 font could be represented with 3 data bytes instead of 5.
It would also make horizontal scrolling easier, and proportional print possible: store the width of a character and only print that many columns of pixel data.