#!/usr/local/bin/perl -w
#
# this perl script allows to rescale an encapsulated IDL postscript file
#
$rotate = 0.;
$scale  = 1.;
$translate = 0;
$forceBB   = 0;
$clip      = 0;
$error     = 0;
#
if ($#ARGV == -1) { $error = 1 ; }
while ($#ARGV >= 0 ) {
  if ($ARGV[0] =~ /^-/) {
    if ($ARGV[0] eq '-r') {
      shift;
      $rotate = $ARGV[0]+0.;
    } elsif ($ARGV[0] eq '-s') {
      shift;
      $scale = $ARGV[0]+0.;
    } elsif ($ARGV[0] eq '-t') {
      $translate = 1;
      @translate = @ARGV[1..2];
      shift; shift; 
    } elsif ($ARGV[0] eq '-c') {
      $clip = 1;
      @clip = @ARGV[1..4];
      shift; shift; shift; shift;
    } elsif ($ARGV[0] eq '-b') {
      $forceBB = 1;
      @fbb = @ARGV[1..4];
      shift; shift; shift; shift;
    } else {
      print STDERR "invalid option '$ARGV[0]'\n";
      $error++;
    }
  } else {
    if ($#ARGV != 0) {
      $error++;
    } else {
      $file  = $ARGV[0];
    }
  }
  shift;
}
if ($error != 0) {
  @USAGE = ("Usage: scale_idleps [options] filename",
            "",
            "       where options are:",
            "",
            " -t t0 t1          translate by (t0, t1) (in points)",
            " -s value          scale  by value       (ratio)",
            " -r value          rotate by value       (in degree)",
            " -c c0 c1 c2 c3    clip to [c0 c1 c2 c3]",
            " -b b0 b1 b2 b3    force the bounding box to [b0 b1 b2 b3]",
            "",
            "",);
  print STDERR join("\n", @USAGE), "\n";
  exit(1);
}
print STDERR "scale_idleps: scaling '$file' by $scale";
if ($translate != 0) {
  print STDERR " but first translating by ",join(", ",@translate);
}
if ($rotate != 0.) {
  print STDERR " and rotating by $rotate [deg]";
}
if ($clip != 0) {
  print STDERR " and clipping [",join(" ",@clip),"]";
}
print STDERR "...\n";
#
open(FILE, "<$file") || die "scale_idleps: could not open $file";
#
while (<FILE>) {
  if      (/^%%BoundingBox: /) {
    #
    if ($forceBB) { 
      @bbs = @fbb;
    } else { 
      chop($_);
      @w = split(/ /, $_);
      @bbs = @w[1..4]; 
    }
    #
    @bbo = @bbs;
    #
    if ($translate) {
      $bbs[0] += $translate[0];
      $bbs[1] += $translate[1];
      $bbs[2] += $translate[0];
      $bbs[3] += $translate[1];
    }
    $bbs[2] *= $scale;
    $bbs[3] *= $scale;
    #
    if ($rotate != 0.) {

      @bbc = @bbs[2..3];
      $bbc[0] /= 2;
      $bbc[1] /= 2;

      $deg2rad = 0.017453293;
      $cr = cos($rotate*$deg2rad);
      $sr = sin($rotate*$deg2rad);

      $x0 =  ($bbs[0]-$bbc[0])*$cr + ($bbs[1]-$bbc[1])*$sr + $bbc[0];
      $y0 = -($bbs[0]-$bbc[0])*$sr + ($bbs[1]-$bbc[1])*$cr + $bbc[1];

      $x1 =  ($bbs[2]-$bbc[0])*$cr + ($bbs[1]-$bbc[1])*$sr + $bbc[0];
      $y1 = -($bbs[2]-$bbc[0])*$sr + ($bbs[1]-$bbc[1])*$cr + $bbc[1];

      $x2 =  ($bbs[2]-$bbc[0])*$cr + ($bbs[3]-$bbc[1])*$sr + $bbc[0];
      $y2 = -($bbs[2]-$bbc[0])*$sr + ($bbs[3]-$bbc[1])*$cr + $bbc[1];

      $x3 =  ($bbs[0]-$bbc[0])*$cr + ($bbs[3]-$bbc[1])*$sr + $bbc[0];
      $y3 = -($bbs[0]-$bbc[0])*$sr + ($bbs[3]-$bbc[1])*$cr + $bbc[1];

      $bbs[0] = $x0;
      if ($x1 < $bbs[0]) {$bbs[0] = $x1;}
      if ($x2 < $bbs[0]) {$bbs[0] = $x2;}
      if ($x3 < $bbs[0]) {$bbs[0] = $x3;}

      $bbs[1] = $y0;
      if ($y1 < $bbs[1]) {$bbs[1] = $y1;}
      if ($y2 < $bbs[1]) {$bbs[1] = $y2;}
      if ($y3 < $bbs[1]) {$bbs[1] = $y3;}

      $bbs[2] = $x0;
      if ($x1 > $bbs[2]) {$bbs[2] = $x1;}
      if ($x2 > $bbs[2]) {$bbs[2] = $x2;}
      if ($x3 > $bbs[2]) {$bbs[2] = $x3;}

      $bbs[3] = $y0;
      if ($y1 > $bbs[3]) {$bbs[3] = $y1;}
      if ($y2 > $bbs[3]) {$bbs[3] = $y2;}
      if ($y3 > $bbs[3]) {$bbs[3] = $y3;}

    }
    #
    for ($i=0; $i<4; $i++) { 
      $bbs[$i] = int($bbs[$i]+.5);
    }
    #
    if ($clip) { 
      print STDERR "w/out clipping:\n";
      print STDERR "  BoundingBox: ",join(" ",@bbo), 
        ' -> ',join(" ",@bbs),"\n";
      print STDERR "after clipping:\n";
      @bbs = @clip; 
    }
    #
    print STDOUT "%%BoundingBox: ",join(" ",@bbs),"\n";
    #
    print STDERR "  BoundingBox: ",join(" ",@bbo), 
      ' -> ',join(" ",@bbs),"\n";
    @bbsi = @bbs;
    @bboi = @bbo;
    for ($i=0; $i<4; $i++) { 
      $bbsi[$i] = int($bbs[$i]*100./72.)/100.;
      $bboi[$i] = int($bbo[$i]*100./72.)/100.;
    }
    print STDERR "           or: ",join(" ",@bboi),
      ' -> ',join(" ",@bbsi)," in\n";
  } elsif (/^%%PageBoundingBox: /) {
#    @w = split(/ /, $_);
#    #@pbbo = @w[1..4];
#    @pbbs = @w[1..4];
#    $pbbs[2] *= $scale;
#    $pbbs[3] *= $scale;
#    print STDOUT "%%PageBoundingBox: ",join(" ",@pbbs),"\n";
    print STDOUT "%%PageBoundingBox: ",join(" ",@bbs),"\n";
  } elsif (/^%%BeginPageSetup/) {
    print STDOUT $_;
    if ($clip != 0) {
      print STDOUT "%%% clipping done here\n";
      print STDOUT "newpath ".
          "$clip[0] $clip[1] moveto ".
          "$clip[2] $clip[1] lineto ".
          "$clip[2] $clip[3] lineto ".
          "$clip[0] $clip[3] lineto closepath clip newpath\n";
     print STDOUT "%%%\n";
    }
    if ($rotate != 0.) {
      print STDOUT "%%% rotation done here\n";
      print STDOUT "$bbc[0] $bbc[1] translate\n".
          "$rotate rotate\n".
              "-$bbc[0] -$bbc[1] translate\n";
      print STDOUT "%%%\n"
        }
  } elsif (/^save \$IDL_DICT / && $scale != 1.) {
    print STDOUT $_;
    print STDOUT "%%% translation done here\n";
    print STDOUT join(" ",@translate),"translate\n";
    print STDOUT "%%% scaling done here\n";
    print STDOUT "$scale dup scale\n";
    print STDOUT "%%%\n"

  } else {
    print STDOUT $_;
  }
}
#
close(FILE);
exit(0);