#!/usr/bin/perl
$copy = '(c) 1998 Markus Peuhkuri <puhuri@iki.fi>';
$dist = 'Free distribution allowed, refer GPL (Gnu Public Licence)';

$hdr = q#

/typewritedict 10 dict def

/typewrite % ptsize up down back fwd str -> -
{
typewritedict begin
 /str exch def
 /fwd exch def
 /back exch def
 /down exch def
 /up exch def
 /ptsize exch def

 /fwd fwd ptsize mul  def
 /back back ptsize mul  def
 /down down ptsize mul  def
 /up up ptsize mul  def
 /x fwd back add def
 /y up down add def

 /lastx 0 def
 /lasty 0 def

 { pop pop
   lastx neg lasty neg rmoveto
   frand x mul back sub dup /lastx exch def
     frand y mul down sub dup /lasty exch def rmoveto
  } str kshow
 lastx neg lasty neg rmoveto
end
} def

/frand { % - -> float
 	 % returns random number in interval [0,1]
  rand 2147483647 div
} def
#;

die "Usage: $0 font point up down back fwd [textfile(s)]\n  $copy\n  $dist\n"
    if $#ARGV < 5;

($font, $point, $up, $down, $back, $fwd, @rest) = @ARGV;
for ($i = 0; $i < 6; $i++) {
  shift @ARGV;
}

$topmarg = (297 - 4) / 25.4 * 72;
$botmarg = 4 / 25.4 * 72 + $point * 1.2;;
$leftmarg =  5 / 25.4 * 72;

print '%!PS-Adobe', "\n";
print "% Made with:\n% $copy \n% $dist\n";
print $hdr;
printf "/%s findfont %d scalefont setfont\n", $font, $point;

$pos = $topmarg;
while (<>) {
  chop;
  s/([()\\])/\\$1/g;
  $pos -= $point * 1.2;
  printf "%f %f moveto\n", $leftmarg, $pos;
  printf "%f %f %f %f %f (%s) typewrite\n",
  $point, $up, $down, $back, $fwd, $_;
  $nonplank++;

  if ($pos < $botmarg) {
    $page ++;
    printf "showpage\n%%%%Page: %d %d\n", $page, $page;
    $pos = $topmarg;
    $nonplank = 0;
  }
}

print "showpage\n%%EOF\n" if $nonplank;
#EOF
