#!/usr/bin/perl

# $Header: /cvsroot/wmi/wmi/ui/oval-buttons.pl,v 1.2 2004/04/12 19:44:01 kovzol Exp $
#
# This script is converted from .scm using the scm2perl
# translator. The .scm file was originally written by
# Arturo Espinosa & Federico Mena Quintero and is modified
# by Zoltan Kovacs for WebMathematics Interactive
# using the web design idea by Gabor Molnar.
#
# Note that script may be buggy. Feedbacks appreciated.
#
# Usage:
# $ ./oval-buttons.pl -text_string "Discrete Mathematics" -active 1
# $ convert out.xcf png:out.png
#
# Consider using a more condensed font in the future, e.g.
# "-*-steelfish-medium-*-*-*-12-*-*-*-*-*-iso8859-2" or better.

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

sub text_width {
   my ($extents) = @_;
   @{$extents}[0];
}

sub text_height {
   my ($extents) = @_;
   @{$extents}[1];
}

sub round_select {
   my ($img, $x, $y, $width, $height, $ratio) = @_;
   do {
      my $diameter = $ratio * $height;
      
      gimp_ellipse_select ($img, $x, $y, $diameter, $height, 0, 0, 0, 0);
      gimp_ellipse_select ($img, ($x + ($width - $diameter)), $y, $diameter, $height, 0, 0, 0, 0);
      gimp_rect_select ($img, ($x + $diameter / 2), $y, ($width - $diameter), $height, 0, 0, 0);
   };
}

sub script_fu_mysite_buttons {
   my ($text, $size, $font, $ul_color, $lr_color, $text_color, $ul_color_high, $lr_color_high, $hlight_color, $xpadding, $ypadding, $bevel, $ratio, $button_width, $fixed_width, $center_text, $active, $output_file) = @_;
   do {
   my $text_length=length($text);
   my $original_length=$text_length;
   my $original_text=$text;
   my $height;
   my $descent;
      
      do {
      
      if ($text_length<$original_length)
       {
        $text = substr($original_text,0,$text_length)."...";
       }
      else 
       {
        $text=$original_text;
       }
   
      my $text_extents = [gimp_text_get_extents_fontname ($text, $size, PIXELS, $font)];
      my $ascent = @{$text_extents}[2];
      $descent = @{cddr ($text_extents)}[1];
      $height = (2 * ($ypadding + $bevel) + ($ascent + $descent));
      my $radius = $ratio * $height / 4;
      
      $label_width = (text_width ($text_extents) - text_width ([gimp_text_get_extents_fontname (" ", $size, PIXELS, $font)]));
      if (!$fixed_width) {
         $width = (2 * ($radius + $xpadding) + $bevel + $label_width);
      } else {
         $width = $button_width;
      }
      $text_length -= 1;
      
      } while ($label_width + 5 > $width);
      
      if (!$center_text) {
         $x_offset = ($xpadding + $radius + $bevel);
      } else {
         $x_offset = ($width - $label_width) / 2;
      }
      $y_offset = ($ypadding + $descent + $bevel);
      if ($active == 1)
        {
          do_mysitebutton ($text, $size, $font, $ul_color, $lr_color, $text_color, $x_offset, $y_offset, $bevel, $ratio, $radius, 0, $width, $height, $label_width, $output_file)
	}
      if ($active == 0)
        {
          do_mysitebutton ($text, $size, $font, $ul_color_high, $lr_color_high, $hlight_color, $x_offset, $y_offset, $bevel, $ratio, $radius, 0, $width, $height, $label_width, $output_file)
	}
   };
}

sub do_mysitebutton {
   my ($text, $size, $font, $ul_color, $lr_color, $text_color, $x_offset, $y_offset, $bevel, $ratio, $radius, $pressed, $width, $height, $label_width, $output_file) = @_;
   do {
      my $old_fg_color = gimp_palette_get_foreground ();
      my $old_bg_color = gimp_palette_get_background ();
      my $img = gimp_image_new ($width, $height, RGB_IMAGE);
      my $bumpmap = gimp_layer_new ($img, $width, $height, RGBA_IMAGE, "Bumpmap", 100, NORMAL_MODE);
      my $gradient = gimp_layer_new ($img, $width, $height, RGBA_IMAGE, "Button", 100, NORMAL_MODE);
      
      gimp_image_undo_disable ($img);
      gimp_image_add_layer ($img, $bumpmap, -1);
      gimp_selection_none ($img);
      gimp_palette_set_background ([0, 0, 0]);
      gimp_edit_fill ($bumpmap, BACKGROUND_FILL);
      round_select ($img, $bevel / 2, $bevel / 2, ($width - $bevel), ($height - $bevel), $ratio);
      gimp_palette_set_background ([255, 255, 255]);
      gimp_edit_fill ($bumpmap, BACKGROUND_FILL);
      gimp_selection_none ($img);
#      plug_in_gauss_rle (1, $img, $bumpmap, $bevel, 1, 1);
      gimp_image_add_layer ($img, $gradient, -1);
      gimp_edit_clear ($gradient);
      round_select ($img, 0, 0, $width, $height, $ratio);
      gimp_palette_set_foreground ($ul_color);
      gimp_palette_set_background ($lr_color);
      gimp_blend ($gradient, FG_BG_RGB_MODE, NORMAL_MODE, GRADIENT_LINEAR, 
      100, 0, REPEAT_NONE, FALSE, FALSE, 0, 0, FALSE,
      0, 0, 0, ($height - 1));
      gimp_selection_none ($img);
#      plug_in_bump_map (1, $img, $gradient, $bumpmap, 135, 45, $bevel, 0, 0, 0, 0, 1, $pressed, 0);
#      $pressed eq 1 ? set_ ($bevel, ($bevel + 1))
#         : die 'cond fell off the end';
      gimp_palette_set_foreground ($text_color);
      do {
         my $textl = gimp_text_fontname ($img, -1, 0, 0, $text, 0, 1, $size, PIXELS, $font);
         
         gimp_layer_set_offsets ($textl, $x_offset, $y_offset);
      };
      gimp_selection_none ($img);
      round_select ($img, 1, 1, ($width - 1), ($height - 1), $ratio);
      gimp_selection_invert ($img);
      gimp_edit_clear ($gradient);
      gimp_image_remove_layer ($img, $bumpmap);
      gimp_image_merge_visible_layers ($img, EXPAND_AS_NECESSARY);
      gimp_selection_none ($img);
      gimp_palette_set_foreground ($old_fg_color);
      gimp_palette_set_background ($old_bg_color);
      gimp_image_undo_enable ($img);
      gimp_display_new ($img);
      gimp_displays_flush ();
      gimp_xcf_save(0,$img,$bumpmap,$output_file,"xcf");
      return $img;
#      gimp_file_save(0,$img,$bumpmap,"o.png","png");
   };
  
  return;
}

register "perl_fu_mysite_buttons",
   "My Site Buttons",
   "My Site Buttons",
   "Zoltan Kovacs, Arturo Espinosa",
   "Zoltan Kovacs, Arturo Espinosa & Federico Mena Quintero",
   "April 12, 2004",
   "<Toolbox>/Xtns/Perl-Fu/Web Page Themes/My Site/Oval Buttons",
   "",
   [
      [PF_STRING,    'text_string',      "Text String", "WMI"],
      [PF_ADJUSTMENT,'font_size_pixels', "Font Size (pixels)", [16, 2, 11, 1, 1, 0, 1]],
      [PF_FONT,      'font',             "Font", "quasiswisscondensedttf"],
      [PF_COLOUR,    'upper_color',      "Upper color", [247, 248, 252]],
      [PF_COLOUR,    'lower_color',      "Lower color", [184, 205, 226]],
      [PF_COLOUR,    'text_color',       "Text color", [0, 0, 0]],
      [PF_COLOUR,    'upper_color_active',"Upper color (active)", [247, 247, 247]],
      [PF_COLOUR,    'lower_color_active',"Lower color (active)", [226, 226, 226]],
      [PF_COLOUR,    'text_color_active',"Text color (active)", [0, 0, 0]],
      [PF_VALUE,     'xpadding',         "xPadding", "4"],
      [PF_VALUE,     'ypadding',         "yPadding", "1"],
      [PF_VALUE,     'bevel_width',      "Bevel width", "2"],
      [PF_VALUE,     'round_ratio',      "Round ratio", "1"],
      [PF_VALUE,     'button_width',     "Button Width", "103"],
      [PF_TOGGLE,    'fixed_button_width',"Fixed Button Width  ", 1],
      [PF_TOGGLE,    'center_text',      "Center Text", 1],
      [PF_TOGGLE,    'active',       	 "Active", 0],
      [PF_STRING,    'output_file',      "Output File", "out.xcf"],
   ],
   \&script_fu_mysite_buttons;

sub cddr {
   my(@x)=@{$_[0]};
   shift(@x); shift(@x);
   @x >1 ? [@x] : $x[0];
}

exit main;
