#!/usr/bin/perl

if ($#ARGV<1) {
  print "\
SUMMARY:

  Use xfig to design your fvwm2 vector buttons, and convert them 
  to fvwm2 vector button definitions using this script.

USAGE:

  With this hack you can design fvwm2 vector buttons using xfig
  in the following manner:

  1) Start up xfig, and draw a BLUE square to mark the bounds of 
     your button. The contents of this square will be scaled up
     to the size of the button displayed by fvwm, so all drawing
     should be done inside this blue square. Make shure the color
     is really blue (so rgb:00/00/ff), otherwise the script
     cannot recognize the bounds of your button.

  2) Start drawing POLYGONS (ie. lines, squares etc) inside the 
     blue square: design your button. BLACK lines will eventually
     be displayed in the shadow color, RED lines wil be
     displayed in the hilight color. Lines in other colors (except
     for BLUE) are ignored, and can be used to lign up stuff
     or whatever.

     Notes: 
     
     The results looks best, when you set xfig to work on a grid
     of fixed positions. This way, it's easier to make the button
     symmetrical.

     To rescale the image in your button, simply delete the blue
     square around your drawing, and draw a new, larger or
     smaller one. When running the script again, your drawing
     will now be rescaled, such that the blue square will fit the
     boundary of the button drawn by fvwm.

  3) When you're finished, save the image, and export it as
     encapsulated postscript, eg. as mybutton.eps

  4) Run the script on the eps file as follows:

         eps2vector.pl mybutton.eps 3 > mybutton.butts

     This will produce a file mybutton.butts, containing vector
     button definitions for button number 3. This file can be
     read by fvwm2.  To test the button, open an fvwm2 console
     and type something like:

         read /root/mybutton.butts

     (if the file is located in the directory /root). The button
     will now appear (if all goes well). Or, just cut and paste
     the file into your .fvwm2rc file.

NOTE:

  I tested the script using xfig to generate eps files, but maybe
  it will also work on other eps files. Converting high
  resolution eps images to vector buttons will probably not work,
  though. Versions I used: Xfig 3.2 and Fvwm2.3.12.
     
AUTHOR:\

                  Bugs, comments, suggestions:

  Jos van Riswick, email: josvanr\@xs4all.nl/j.g.a.v.riswick\@tue.nl

                          12-1-2000

  ";

  exit;
}

$xmin=$ymin=100000;
$xmax=$ymax=-100000;

@cm=qw(
  lineto
  newpath
  moveto
  gsave
  closepath
);

open (IN,$ARGV[0]);

$ln=0;
while (<IN>) {

  $fi=$fi.$_; $ln++;
  # get drawing commands
  foreach $i (0 .. $#cm) {
    if ($_=~/{$cm[$i]}/) {
      $l=$_; $l=~s/^\///; $l=~s/ .*\n//;
      $cm{$cm[$i]}=$l;
    }
  }
  # get color black command
  if ($_=~/{0.000 0.000 0.000 srgb}/) {
    $l=$_; $l=~s/^\///; $l=~s/ .*\n//;
    $black=$l;
  }
  # get color blue command
  if ($_=~/{0.000 0.000 1.000 srgb}/) {
    $l=$_; $l=~s/^\///; $l=~s/ .*\n//;
    $blue=$l;
  }
  # get color red command
  if ($_=~/{1.000 0.000 0.000 srgb}/) {
    $l=$_; $l=~s/^\///; $l=~s/ .*\n//;
    $red=$l;
  }
}

@li=split("\n",$fi);

# get polygons and colors
$f=$fi; $f=~s/.*\$F2psBegin//gs; @pf=split("n ",$f);

$l=-1;
foreach $i (1 ..$#pf) {
  if ($pf[$i]=~m/$red/) {
    $pc[$i]=1; $l++;
  } elsif ($pf[$i]=~m/$blue/) {
    $pc[$i]=2;
  } elsif ($pf[$i]=~m/$black/) {
    $pc[$i]=0; $l++;
  } else {
    $pc[$i]=3;
  }

  if ($pc[$i]<=2) {

    if (($pf[$i]=~m/$cm{closepath}/) and ($pc[$i]<2)) {
      $cp[$i]=1;
    }

    $pf[$i]=~s/.*$cm{newpath} //gs;
    $pf[$i]=~s/ $cm{gsave}.*//gs;
    $pf[$i]=~s/ $cm{closepath}.*//gs;
    $pf[$i]=~s/$cm{moveto}/x/gs;
    $pf[$i]=~s/$cm{lineto}/x/gs;
    $pf[$i]=~s/\n//gs;

    @c=split("x",$pf[$i]);
    foreach $ii (0 .. $#c) {
      @cc=split(" ",$c[$ii]);
      if ($pc[$i]<2) {
        # polygons
        $p[$l][$ii][0]=$cc[0];
        $p[$l][$ii][1]=$cc[1];
        $color[$l]=$pc[$i];
      } else {
        # blue 'bounding box'
        if ($cc[0]<$xmin) {$xmin=$cc[0]};
        if ($cc[0]>$xmax) {$xmax=$cc[0]};
        if ($cc[1]<$ymin) {$ymin=$cc[1]};
        if ($cc[1]>$ymax) {$ymax=$cc[1]};
      }
    }

    if ($cp[$i]) {
      $p[$l][$#c+1][0]=$p[$l][0][0];
      $p[$l][$#c+1][1]=$p[$l][0][1];
    }

  }
}

$dx=$xmax-$xmin;
$dy=$ymax-$ymin;
$fx=100/$dx;
$fy=100/$dy;

# convert to percentages and output
print "ButtonStyle $ARGV[1] Simple -- UseTitleStyle\n";
foreach $i (0 .. $l) {
  @pp=@{$p[$i]};
  foreach $ii (0 .. $#pp) {
    ($x,$y)=($pp[$ii][0],$pp[$ii][1]);
    $xa=round($fx*($x-$xmin));
    $ya=round($fy*($y-$ymin));
    if ($ii==0) {
      $n=$#pp+1;
      print "AddButtonStyle $ARGV[1] $n ".$xa."x".$ya."@".$color[$i]." "; 
    } else {
      print $xa."x".$ya."@".$color[$i]." ";
    }
  }
  print "\n";
}

sub round { my $x=$_[0];
  my $ix=int($x);
  if (($x-$ix)<0.5) {
    return $ix;
  } else {
    return $ix+1;
  }
}



