#!/usr/bin/perl 

# Effect taken from http://tigert.gimp.org/gimp/tutorials/beveled_text/
# perl-ified by Seth Burgess <sjburges@gimp.org>

# Programatically, this script is about as dull as they come.  The only 
# exceptions are those of the neat util functions (that aren't all quite
# working btw).  You can follow step by step with the website at 
# http://tigert.gimp.org/gimp/tutorials/beveled_text/

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

N_"/Xtns/Render"; N_"/Xtns/Render/Logos"; # i18n workaround

$defaultcolor1 = [124,10,18];
$defaultcolor2 = [200,19,27];

$path = N_"<Toolbox>/Xtns/Render/Logos/Inner Bevel...";
$shortdesc = "Perform an inner bevel on text";
$longdesc = "This uses tigert's inner bevel method on text, which can be found with his other excellent tutorials at http://tigert.gimp.org/";
$date = "1999-03-23";
$imgtypes = undef;
$author = "Seth Burgess <sjburges\@gimp.org>";

$regname = "inner_bevel";

$author =~ m/^(.*) </;
$authorname = $1;

sub gimp20_text_wh{
   ($text, $fn) = ($_[0], $_[1]);
   Gimp->text_get_extents_fontname($text,(split(/ /,$fn))[-1],PIXELS,$fn)
   }
   

register $regname, $shortdesc, $longdesc, $authorname, $author, $date, $path, $imgtypes,
  [ 
   [PF_FONT, "font", "Font Name","URW Bookman L, Bold 80"],
   [PF_STRING, "text", "Enter your text to be beveled", "INNERBEVEL"],
   [PF_COLOR, "top_color", "Blend to this color", $defaultcolor2],
   [PF_COLOR, "bottom_color", "Blend from this color", $defaultcolor1],
   [PF_SLIDER, "azimuth", "Direction of the shine", 132, [0,255,5]],
   [PF_SLIDER, "shinyness", "How shiny the final image will be",30, [0,90,5]],
   [PF_SLIDER, "depth_shape", "Determines the final shape", 7 , [0,64,32]], 
   [PF_RADIO, "map", "The type of Map to use", 2, [Linear => 0, Spherical => 1, Sinusoidal => 2] ], 
  ],[],
  [
   'gimp-1.1',
  ], sub {

my ($font, $text, $color1, $color2, $azimuth, $elevation, $depth, $maptype) = @_;
# -- step 1 -- 

gimp_palette_set_background($color1);
gimp_palette_set_foreground($color2);

@dims = gimp20_text_wh($text, $font);

$img = gimp_image_new($dims[0]+30, $dims[1]+10, RGB);

# none of the macro's did quite what I was looking for here.  
# i.e. create a text layer on transparent only...

# -- step 2 --
$layertmp = $img->add_new_layer(0,TRANSPARENT_FILL);
$txtlayer = $img->text_fontname(-1, 10, 10, $text, 0, 1, (split / /,$font)[-1], PIXELS, $font);
$dsp = gimp_display_new($img);  # display the image early
$layer = $img->merge_visible_layers(EXPAND_AS_NECESSARY);
@pt1 = ($layer->width * 0.5 -1, 0);
@pt2 = ($layer->width * 0.5 +1, $layer->height);
# -- step 3 --
$layer->set_preserve_trans(1);
$layer->edit_blend(FG_BG_RGB_MODE, NORMAL_MODE, GRADIENT_LINEAR, 100, 0, REPEAT_NONE, 0, 0, 0, 3, 0.20, @pt1, @pt2); 
# -- step 4 -- 
$layer2 = $layer->copy(0);     # Can you override these to have a default? (would be nice)
$img->add_layer($layer2, 0);
# -- step 5 -- 
$layer2->set_preserve_trans(1);
$img->selection_all;
gimp_palette_set_background([1.0,1.0,1.0]);
$layer2->edit_fill(BACKGROUND_FILL);
# -- step 6 -- 
$layer2->set_preserve_trans(0);
$layer2->gauss_rle(6,1,1);     
# -- step 7 --
$layer->plug_in_bump_map($layer2, $azimuth, $elevation, $depth, 0,0,0,0,1,0,$maptype);
# -- step 8 --
$layer2->invert;
$img->lower_layer($layer2);
# -- step 9 --
$layer2->translate(2, 3);

# extra stuff
$img->add_new_layer(2);
$img->gimp_selection_none();

return();
};

exit main;  # <-- This lil' bugger caused me much grief.  

=head1 LICENSE

Copyright Seth Burgess.  
Distrubuted under the same terms as Gimp-Perl.

=cut

