#!/usr/local/bin/perl

#
# console2bdf
# Public domain, written by Pasi Eronen <pe@iki.fi> on November 1997.
#
# Converts a Linux raw console font to X11 BDF font.
#
# Note that this program doesn't understand PSF fonts but you
# can easily convert any font understood by Linux setfont(8)
# by loading it and then outputting it with the "-o" option.
#
# To make the font usable in X11 convert it to PCF with
# bdftopcf (comes with standard X distribution), put it in 
# the correct directory (maybe /usr/lib/X11/fonts/misc/), 
# run "mkfontdir <directory>" and finally "xset fp rehash".
#
# Usage:
# console2bdf fontfile fontname > bdffile
#

# Check arguments
die "Usage: console2bdf fontfile fontname > bdffile\n" if ($#ARGV != 1);
$filename = $ARGV[0];
$fontname = $ARGV[1];

# Check file size (and calculate height)
$size = (-s $filename);
die "$filename: Not found\n" unless ($size > 0);
$height = $size/256;
if ((($height*256) != $size) || ($height < 8) || ($height > 32))
{
    die "Can't determine font height\n";
}

# Open file
open(INPUT, "<$filename");
binmode INPUT;

# Output BDF header
print "COMMENT Generated by console2bdf\n";
print "STARTFONT 2.1\n";
print "FONT $fontname\n";
print "SIZE $height 75 75\n";
print "FONTBOUNDINGBOX 8 $height 0 0\n";
print "STARTPROPERTIES 3\n";
print "FONT_DESCENT 0\n";
print "FONT_ASCENT $height\n";
print "DEFAULT_CHAR 0\n";
print "ENDPROPERTIES\n";
print "CHARS 256\n";

# Convert characters
for ($i=0; ($i < 256); $i++)
{
    print "STARTCHAR C$i\n";
    print "ENCODING $i\n";
    print "SWIDTH 666 0\n";
    print "DWIDTH 8 0\n";
    print "BBX 8 $height 0 0\n";
    print "BITMAP\n";
    for ($line = 0; ($line < $height); $line++)
    {
	read INPUT, $data, 1;
	printf "%02x\n", ord($data)
    }
    print "ENDCHAR\n";
}

# Close files
print "ENDFONT\n";
close(INPUT);
close(OUTPUT);
