package MF2O;
use strict;
use Font::TFM;

use constant UNITSPEREM => 16000;

sub new {
  my $that = shift;
  my $font = shift || die "No input font";
  my $class = ref($that) || $that;

  my $base = $font;
  $base =~ s/\..*//;
  print "Base: $base\n";

  # TODO: Get this from somewhere.
  my $designsize = 10;

  my $self = bless {
    FONT => $font,
    BASE => $base,
    DESIGNSIZE => $designsize,
  }, $class;
  return $self;
}

sub run {
  my $self = shift;
  my $mag = UNITSPEREM / $self->{DESIGNSIZE};

  my $font = $self->{FONT};
  my $base = $self->{BASE};

  system "mfmp", "\\mode=localfont; \\batchmode; mag=$mag; input $font";
  my $tfm = $self->getTFMObject($mag);

  open SCRIPT, ">$base.pe";
  $self->scriptProlog(*SCRIPT);

  opendir(DIR, ".") or die "Can't read dir: $!";
  my @epsfiles;
  my @mpfiles = readdir(DIR);

  foreach my $file (@mpfiles) {
    -f $file or next;
    $file =~ /^$base.(\d\d*)$/ or next;
    my $pos = $1;
    $self->mpostToEps($file);
    push @epsfiles, "$file.eps";
    print SCRIPT "Print(\"importing $pos\")\n";
    print SCRIPT "Select($pos); Import(\"$file.eps\",0,12)\n";
    print SCRIPT "RemoveOverlap()\n";
    print SCRIPT "SetWidth(" . sprintf("%.0f", $tfm->width(chr($pos))) . ")\n";
    print SCRIPT "AutoHint()\n";
  }

  print SCRIPT "MergeKern(\"$base.tfm\")\n";
  print SCRIPT "Generate(\"$base.pfa\")\n";
  print SCRIPT "Save(\"$base.sfd\")\n";
  print SCRIPT "Print(\"Import script done.\")\n";
  system "fontforge", "-script", "$base.pe";
  $self->cleanUp(\@epsfiles);
  print "Done.\n";
}

sub getTFMObject {
  my $self = shift;
  my $mag = shift;

  system "mf", "\\mode=localfont; \\batchmode; mag=1; input $self->{FONT}";

  $Font::TFM::TEXFONTSDIR = ".";
  my $tfm = new Font::TFM name => $self->{BASE}, multiply => $mag;

  return $tfm;
}

sub scriptProlog {
  my $self = shift;
  my $fh = shift;
  print $fh "New()\n";
  print $fh "ScaleToEm(" . UNITSPEREM . ")\n";
  print $fh "LoadEncodingFile(\"/usr/share/texmf/dvips/base/xl2.enc\")\n";
  print $fh "Reencode(\"adobestandard\")\n";
  print $fh "Print(\"presetting characters\")\n";
  foreach my $pos (0..255) {
    print $fh "Select($pos)\n";
    print $fh "SetWidth(" . sprintf("%.0f", UNITSPEREM / 2) . ")\n";
  }
  print $fh "Reencode(\"xl2encoding\")\n";
  print $fh "Print(\"presetting characters\")\n";
  foreach my $pos (0..255) {
    print $fh "Select($pos)\n";
    print $fh "SetWidth(" . sprintf("%.0f", UNITSPEREM / 2) . ")\n";
  }
}

sub cleanUp {
  my ($self, $files) = @_;
  foreach my $file (@$files) {
    unlink $file;
  }
  unlink "$self->{BASE}.pe";
}

sub mpostToEps {
  my $self = shift;
  my $file = shift;

  my $line;

  open CHAR, "<$file" or die "Can't open input eps: $!";
  open EPS, ">$file.eps" or die "Can't open output eps: $!";
  while (defined ($line = <CHAR>)) {
    $line =~ s/showpage//g;
    $line =~ s/(\d\d*\.\d\d*)/sprintf("%.0f", $1)/eg;
    print EPS $line;
  }
  close EPS;
  unlink $file;
}

package main;

my $mf2o = new MF2O($ARGV[0]);
$mf2o->run();

