#!/usr/bin/perl

# Run like:
#
# gzip -d < /usr/share/fonts/X11/misc/5x7.pcf.gz |\
# pcf2bdf |\
# ./bdf2hexstring.pl > font5x7-bin.txt

use strict;
use warnings;

my @data = ("000000") x 1024;

my $inchar = 0;
my $index;
my $lineno;

while (<>) {
    chomp;
    if (/^FONTBOUNDINGBOX (\d+) (\d+)/) {
	die "need a 5 x 7 font" unless $1 == 5 && $2 == 7;
    }
    if ($inchar == 0 && /^STARTCHAR/) {
	$inchar = 1;
    } elsif ($inchar == 1 && /^ENCODING (\d+)/) {
	$inchar = 2;
	$index = int($1);
	if ($index >= 128) {
	    $inchar = 0;
	    $index = undef;
	}
    } elsif ($inchar == 2 && /^BITMAP/) {
	$inchar = 3;
	$lineno = 0;
    } elsif ($inchar == 3) {
	if (/^ENDCHAR/) {
	    $inchar = 0;
	    $index = undef;
	    $lineno = undef;
	} else {
	    my $hexval = hex($_);
	    my $str = "";
	    for (my $i = 0x80; $i != 0; $i >>= 1) {
		$str .= ($hexval & $i)? '1': '0';
	    }
	    $data[$index * 8 + $lineno] = substr($str, 0, 6);
	    $lineno++;
	}
    }
}

for (my $i = 0; $i < 1024; $i++) {
    print $data[$i] . "\n";
}
