#!/usr/bin/perl

use Gimp qw(:auto N_);
use Gimp::Fu;

my $fn_base = "/tmp/ttf$$";
my %tmpfiles = (
  "$fn_base.pgm"=>1,
  "$fn_base.tex"=>1,
  "$fn_base.log"=>1,
  "$fn_base.ps"=>1,
  "$fn_base.dvi"=>1);

# Cleanup
sub cleanup {
  foreach my $fn (keys %tmpfiles) {
    unlink $fn;
  }
}

sub xec {
  my $cmd = shift;
#    print STDERR "$cmd\n";
  return `$cmd`;
}

sub exist_in_tex_path {
  my $file = shift;

  return 0 unless length($file);
  return 1 if -e $file;
  foreach my $p (split(/:/, $ENV{TEXINPUTS} . ":/tmp")) {
      return 1 if -e "$p/$file";
  }
  return 0;
}

sub tex_string_to_pgm {
  my($text, $input_file, $string, $ppi, $magstep, $anti_aliasing,
     $output_file) = @_;

  my $scale = sprintf("%.5f", 1/$anti_aliasing);
  my $r = $ppi * $anti_aliasing;
  my $device = "pgmraw";

  chdir "/tmp";
  if (exist_in_tex_path($input_file)) {
    $input .= "\\input $input_file\n";
  }

  open(TEX, ">$fn_base.tex");
  print TEX "\\nopagenumbers\n"
	  . "\\magnification\\magstep$magstep\n"
	  . "\\tolerance=8000\n"
	  . "$input\n"
	  . "$string\n"
	  . "\\bye";
  close(TEX);

  my $res = xec("tex $fn_base.tex");
  # Deal with errors...

  # Make dvips output bounding box
  my $psoutput = xec("dvips -r$r -E -f $fn_base");
  # Deal with errors

  # Shift postscript file to origin
  my @bbox = $psoutput=~ /^%%BoundingBox:\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/m;
  my $w = $bbox[2]-$bbox[0];
  my $h = $bbox[3]-$bbox[1];
  $psoutput=~ s/^%%BoundingBox:.*$/%%BoundingBox: 0 0 $w $h/m;
  $psoutput=~ s/\b1 0 bop/-$bbox[0] -$bbox[1] translate\n$&/m;

  # Output to file in order not to have to use Open2.
  open(PS, ">$fn_base.ps");
  print PS $psoutput;
  close(PS);
  $w= int($w*$r/72+0.5);
  $h= int($h*$r/72+0.5);

  # Use gs to produce a pgmfile
  my $cmd = "gs -g${w}x${h} -r${r} -dNOPAUSE -q -sDEVICE=$device -sOutputFile=- $fn_base.ps quit.ps |";
  $cmd .= "pnmscale $scale |" if $scale != 1;
  chop($cmd);
  $cmd .= "> $output_file";
  print "$cmd\n";
  xec("$cmd");
}

sub grey_file_to_float {
  my($image, $drawable, $fn) = @_;

  # Setup
  gimp_image_undo_group_start($image);
  gimp_context_push();

  # Load the new img
  my $grey_img = gimp_file_load($fn, $fn);

  # Get name of new layer
  my $grey_layer = gimp_image_get_active_layer($grey_img);

  # Create an alpha layer and copy image to alpha layer
  gimp_layer_add_alpha($grey_layer);
  $grey_img->selection_all();
  gimp_edit_copy($grey_layer);
  $mask = gimp_layer_create_mask($grey_layer, 0);
  gimp_layer_add_mask($grey_layer, $mask);
  my $floating_layer = gimp_edit_paste($mask, 0);
  gimp_floating_sel_anchor($floating_layer);
  gimp_invert($mask);
  gimp_context_set_background(gimp_context_get_foreground());
  gimp_edit_fill($grey_layer, BACKGROUND_FILL);
  gimp_layer_remove_mask($grey_layer, 0);

  # Now copy this layer to $img 1
  gimp_edit_copy($grey_layer);
  $floating_layer = gimp_edit_paste($drawable, 0);
  gimp_layer_set_lock_alpha($floating_layer, 1);
  gimp_edit_fill($floating_layer, BACKGROUND_FILL);

  cleanup();

  # Get rid of $grey_img
  gimp_image_delete($grey_img);

  # Restore
  gimp_context_pop();
  gimp_image_undo_group_end($image);

  return undef;
}

podregister {
  tex_string_to_pgm($text, $input_file, $text, $ppi, $magstep, $anti_aliasing,
		    "$fn_base.pgm");
  grey_file_to_float($image, $drawable, "$fn_base.pgm");
  ();
};

exit main();
__END__

=head1 NAME

tex_string_to_float - Turn a TeX-string into floating layer

=head1 SYNOPSIS

<Image>/Filters/Render/TeX String...

=head1 DESCRIPTION

Takes a TeX string as input and creates a floating layer of the rendered
string in the current layer in the foreground color.

=head1 PARAMETERS

     [PF_STRING,  "input_file",     "TeX macro file to input"],
     [PF_STRING,  "text",     "Enter TeX String", "Your \\TeX string here"],
     [PF_VALUE,   "ppi",     "Resolution to render the text in",   "72"],
     [PF_VALUE,   "magstep",     "TeX magstep",   "2"],
     [PF_VALUE,   "anti_aliasing",     "Anti-aliasing factor",   "4"],

=head1 RETURN VALUES

=head1 IMAGE TYPES

*

=head1 AUTHOR

Dov Grobgeld <dov@imagic.weizmann.ac.il>

=head1 DATE

1999-03-16

=head1 LICENSE

(c) Dov Grobgeld

Released under the Artistic License.
