#!/usr/local/bin/perl
# $Id: ps2ascii.pl,v 1.3 1991/12/12 05:07:56 ssd Exp ssd $

# PostScript to ASCII translator
#Updated to fix ligature bug:
#based on message from mkant
# Written by Steven Dick (ssd@engr.ucf.edu)
# with thanks to Olof Runborg for beta testing & suggestions

# Send suggestions/improvements/bugs/etc to ssd@engr.ucf.edu

# This is public domain--although I'd appreciate it if you would
# leave the above notice there.

require join('', $ENV{'PS2ASCII'}, '/translation.pl');

sub chardecode {
  local($ch)=oct($_[0]);
  local($fix);
  if (defined($Encoding{$Font.$ch})){
    $Encoding{$Font.$ch};
  } elsif ($ch == 15) {
     'o'.' ';
  } elsif ($ch == 11) {
     'f'.'f';
  } elsif ($ch == 12) {
     'f'.'i';
  } elsif ($ch == 13) {
     'f'.'l';
  } elsif ($ch == 14) {
     'f'.'f'.'i';
  } else {
    'X'.pack("c",$ch).'X';
  }
}


while (<>){
  chop;
  if (/^showpage$/) { 
    &showpage;
  } elsif (/^X. /) {
    ($junk,$x,$y,$xn,$yn,$text)=split(' ',$_,6);
    if (/^X1 /){
      $text =~ s/^\((.+)\)$/$1/;
      $text =~ s/\\\\/"/g;
      $text =~ s/\\(\d{1,3})/&chardecode($1)/eg;
      $text =~ s/\\n/\n/g;
      $text =~ s/\\r/\r/g;
      $text =~ s/\\t/\t/g;
      $text =~ s/\\b/\b/g;
      $text =~ s/\\f/\f/g;
      $text =~ s/\\\)/)/g;
      $text =~ s/\\\(/(/g;
      $text =~ s/\\\\/\\/g;
    } elsif (/^Xg ([^ ]*)/) {
      $text = $translation{$1};
    }
    &processline($x,$y,$xn,$yn,$text);
  } elsif (/^F ([^ ]+)$/){
    $Font = $1;
  } elsif (($Font,$junk)=/^E ([^ ]+) \[(.+)\]$/){
#    print "found encoding: \n";
    $num = 0;
    foreach $ch ( split(' ',$junk)){
      next if ($ch eq '/.notdef');
      if (defined($translation{$ch})){
	$Encoding{$Font.$num} = $translation{$ch};
      } else {
	print "Unknown character name '$ch' for font $Font\n";
      }
    } continue { $num++; }
#  print " num= $num\n";
  } else {
    print;
    print "\n";
  }
}
# assume text ends with a showpage?

sub byposition {
  $page[$a] <=> $page[$b] ||
  $wherey[$a] <=> $wherey[$b] ||
  $wherex[$a] <=> $wherex[$b];
}

sub showpage
{
  $index--;
#  if ($margset){
#    print "\nshowpage: left=",$lmargin/72," right=",$rmargin/72,"\n";
#  } else {
#    print "\nshowpage: no margins set!\n";
#  }
  $lmargin=0;

  @order = sort byposition 0..$index;

  $scale=7.65;
  $y=$x=0;
  foreach $a (@order) {
    if ($y!=$wherey[$a]){
      if (($wherey[$a] - $y)/10 >= 1) { 
      print "\n" x (($wherey[$a] - $y)/10);
    } else {
      print "\n";
    }
    $y=$wherey[$a];
#      print "\n";
    $x=0;
  }
    $dif = $wherex[$a] - $x - $lmargin;
    $scale = 3.7;
#  $scale = ($xn[$a]-$wherex[$a])/length($text[$a]) if
(length($text[$a])>0 && $xn[$a]!=$wherex[$a]);
    if ($dif>=$scale){
#      print ' ';
      print ' ' x ($dif/$scale);
#    printf "(x%2.3f) ",$dif/$scale;
      $x = $wherex[$a] - $lmargin;
  }
    # addition to make TeX format more nicely.
  if ($dif<$scale && $dif>($scale/4) ){
    print ' ';
    $x = $wherex[$a] - $lmargin;
  }
  $x = $xn[$a] - $lmargin;
  print $text[$a];
}
  &startpage;
  print "\n" x 2;
  print "-" x 77;
  print "\n" x 1;
}

sub processline 
{
  local($x,$y,$xn,$yn,$text)=@_;
#  if ($y<0) {   $y = -$y; }
#  else { warn "$y wasn't negative!\n"; }
  $wherex[$index] = $x;
  $wherey[$index] = $y;
  $xn[$index]=$xn;
  $text[$index]=$text;
#  $twidth{$Font} += $xn-$x;
#  $tlength{$Font} += length($text);
  if (!$margset){
    $lmargin=$rmargin=$x;
    $margset=1;
  } else {
    $lmargin=$x if ($lmargin>$x);
    $rmargin=$xn if ($rmargin<$xn);
  }
  $index++;
}

sub startpage {
  $index=0;
  $margset=0;
  $lmargin=0; $rmargin=8*72;  # set some sane values just in case
  undef @wherex,@wherey,@xn,@text;
}
