#!/usr/local/bin/perl
#
# This is mathlabels, Copyright 1992 Silvio Levy
# $Header: /u/levy/RCS/mathlabels.orig,v 1.3 1994/09/19 02:08:15 levy Exp $
#

#
# first argument: file name for labels
# second to fifth arguments: xlo, xhi, ylo, yhi (to fix the aspect ratio)
#   use OK for these arguments if no changes need to be made
#

die "$0 file.lab xlo xhi ylo yho < file_orig.ps > file.ps\n (use OK" 
         . " for the last four args)\n"
  if ($#ARGV != 4);

$filename=$ARGV[0]; shift;
$xlo=$ARGV[0]; shift;
$xhi=$ARGV[0]; shift;
$ylo=$ARGV[0]; shift;
$yhi=$ARGV[0]; shift;

die "Can't open $filename" unless open(labels,">$filename");

#
# get various parameters output by psfix
#
while (<>) {
  print;
  last if (/^MathPictureStart/);
  ($trash,$lmarg)=split(' ') if (/\/Mlmarg/);
  ($trash,$rmarg)=split(' ') if (/\/Mrmarg/);
  ($trash,$bmarg)=split(' ') if (/\/Mbmarg/);
  ($trash,$tmarg)=split(' ') if (/\/Mtmarg/);
  ($trash,$width)=split(' '), $width*=72 if (/\/Mwidth/);
  ($trash,$height)=split(' '), $height*=72 if (/\/Mheight/);
  ($trash,$nodistort)=split(' ') if (/\/Mnodistort/);
}

&doit(0,0,$width,$height);

sub doit {
local($XLO,$YLO,$XHI,$YHI)=@_;   # target bounding box
local($Ax,$Bx,$Ay,$By);

line:
while (<>) {

return if (/^MathSubEnd/);
if (/ MathSubStart/) {
   ($Xlo,$Ylo,$Xhi,$Yhi) = split(' '); 
   print;
   &doit($Xlo*$Ax+$Bx,$Ylo*$Ay+$By,$Xhi*$Ax+$Bx,$Yhi*$Ay+$By);
}

#
# look between the lines
#
# % Scaling calculations
# ...                   [
# ] MathScale
#
# to mimic the action of MathScale
#
  if (/% Scaling calculations/) {
    $collecting=1;
    $npts=-2;
  }
  if ($collecting) {  #[
    if (/] MathScale/) {
#
# do the calculations
#
      $collecting=0;
      $i=0;
      while ($i<$npts-2) {
        print($point[$npts]);
      }
      ($trash,$gxlow,$gylow) = split(' ',$point[$npts-2]);
      $gxlow=$xlo unless ($xlo=~/OK/);
      $gylow=$ylo unless ($ylo=~/OK/);
      print("[ $gxlow $gylow 0 0 ]\n");
      ($trash,$gxhigh,$gyhigh) = split(' ',$point[$npts-1]);
      $gxhigh=$xhi unless ($xhi=~/OK/);
      $gyhigh=$yhi unless ($yhi=~/OK/);
      print("[ $gxhigh $gyhigh 0 0 ]\n");
      ($Ax,$Ay,$Bx,$By) = 
           &findpars($XLO,$YLO,$XHI,$YHI,$gxlow,$gylow,$gxhigh,$gyhigh);
      print;
    } else {
#
# put scaling lines into an array
#
      if ($npts>=0) {
        if (!/Msboxa/ && !/Mrotsboxa/) {
          $point[$npts] = $_;
          $npts++;
        }
      } else {
        print;
        $npts++;
      }
    }
    next line;
  }
#
# issue labeling command
#
  if (/\[\((.*)\)\]  *([^ ]*)  *([^ ]*)  *([^ ]*)  *([^ ]*)  *(.*Mrotshowa|Mshowa)/) {
    $_=$1;
    $xpos=$2*$Ax+$Bx;
    $ypos=$3*$Ay+$By;
    $xrelpos=$4;
    $yrelpos=$5;
    s/\\([\\()])/$1/g;
    printf labels ("\\setlabel{%s}{%f}{%f}{%f}{%f}\n",
         $_,$xpos,$ypos,$xrelpos,$yrelpos);
    next line;
  }
  print;
}
}

sub findpars {
   local($xlow,$ylow,$xhigh,$yhigh,$gxlow,$gylow,$gxhigh,$gyhigh)=@_;
   local ($Ax,$Ay,$Bx,$By);
      $Ax=($xhigh-$xlow)/($gxhigh-$gxlow);
      $Ay=($yhigh-$ylow)/($gyhigh-$gylow);
      if ($nodistort=~/true/) {
        $Ax=$Ay if ($Ax>$Ay);
        $Ay=$Ax if ($Ay>$Ax);
      }
      $Bx=(($xlow+$xhigh)-($gxlow+$gxhigh)*$Ax)/2;
      $By=(($ylow+$yhigh)-($gylow+$gyhigh)*$Ay)/2;
  return($Ax,$Ay,$Bx,$By);
}
