#!/usr/bin/perl

use Font::BDF;
use Pod::Usage;
use Getopt::Long;
use File::Basename;

my $data   = "file.dat";
my $length = 24;
my $verbose;
my $help = 0;
my $output = undef;
my $fontname;
my $encoding = "Unicode";
my $suffix = ".sfd";
my $fontforge = "fontforge";

$result = GetOptions('help|?' => \$help,
		     "output=s" => \$output,    
		     "weight=s" => \$weight,
		     "fullname=s" => \$fullname,
		     "familyname=s" => \$familyname,
		     "fontname=s" => \$fontname,
		     "encoding=s" => \$encoding,
		     "ttf" => \$ttf,
	   ) or pod2usage(2);

pod2usage(1) if $help;

if ($ARGV[0]) {
    my ($name, $path, $suffix) = fileparse($ARGV[0], qr{\..*});
    if (!defined $fontname) { 
	$fontname = (length $name) ? "\u$name" : "MyFont";
	$fontname =~ s/[-_](.)/\u$1/g;
	$fontname =~ s/[^a-zA-Z0-9]//g;
    }
    if (!defined $output) { $output = "$path$name.sfd"; $ttfoutput = "$path$fontname.ttf" }
    else { $ttfoutput = $output }
}
else {
    if (!defined $fontname) { $fontname = "MyFont" }
    if (!defined $output) { $output = "font.sfd"; $ttfoutput = "font.ttf" }
    else { $ttfoutput = $output }
}

if ($ttf) {
    $output = "/tmp/$$.sfd";    
}

$scale = 100;
$fudge = 1;

my $f = Font::BDF->new_from_bdf(\*ARGV);
my %p = $f->get_properties;
my $ascent = $p{FONT_ASCENT} * $scale; 
my $descent = $p{FONT_DESCENT} * $scale;

open OUT, ">$output" or die $!;
select(OUT);

my $num_chars = $f->encoded_chars;
my $max_chars = 0;
for my $x ($f->encoded_chars) {
    if ($x + 1 > $max_chars) { $max_chars = $x + 1 }
}

print "SplineFontDB: 1.0\n";

sub print_field {
    my ($name, $value) = @_;
    if (length $value) {
	print "$name: $value\n";
    }
}

print_field("FontName", $fontname);
print_field("FullName", $fullname);
print_field("FamilyName", $familyname);
print_field("Weight", $weight);
print_field("Encoding", $encoding);
print_field("Ascent", $ascent);
print_field("Descent", $descent);
print_field("BeginChars", "$max_chars $num_chars");

for my $enc ($f->encoded_chars) {
my ($name, $offset) = $f->get_char_info ($enc);
my ($hoff, $voff) = @$offset;
$hoff *= $scale;
$voff *= $scale;
my ($pixels) = $f->get_pixels($enc);
print <<EOF;
StartChar: $name
Encoding: $enc $enc
Width: $hoff
Flags:
Fore
EOF
for (keys %$pixels) {
my ($x, $y) =  /^(.+),(.+)$/;
$x *= $scale;
$y *= $scale;
my ($xp, $yp) = ($x + $scale + $fudge, $y + $scale + $fudge);
print <<EOF;
$x $y m 1
$x $yp l 1
$xp $yp l 1
$xp $y l 1
$x $y l 1
EOF
}
print <<EOF;
EndSplineSet
EndChar
EOF
}
print <<EOF;
EndChars
EndSplineFont
EOF

close(OUT);

select(STDOUT);

if ($ttf) {
    system $fontforge, '-c', 'Open($1); RemoveOverlap(); Simplify(); Generate($2);', $output, $ttfoutput;
    unlink $output;
}


__END__


=head1 NAME

bdf2sfd

=head1 SYNOPSIS

bdf2sfd [options] font.bdf

 Options:
           --help               brief help message
           --output file        set output file (defaults to 
                                [input filename].sfd or font.sfd
                                if reading from stdin)
           --fontname fn        set font name
           --familyname fn      set family name
           --fullname fn        set full name
           --encoding e         set encoding      
           --weight w           set weight
           --ttf                generate ttf instead of sfd

=head1 DESCRIPTION

B<bdf2sfd> reads in a X11 bitmap font in BDF format, and outputs a file
in the native B<Fontforge> format, SFD.

=cut

