#!/usr/bin/perl

eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell

eval 'exec /usr/app/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell

# <sjburges@gimp.org>
# This is adrian and xachs idea - take a rectangluar selection, and select
# font type and string.  Then fill it with whatever size is needed.

# This uses highly non-scientific "for my test image these were ok" 
# approximations - I didn't chart anything or calcualte tradeoffs.  Really
# it doesn't matter until the fine tuning, which takes awhile.

# 7/15/99: added support if the user selects in pixels (assumes 72 dpi - deal)
#          and tuned it a bit more <sjburges@gimp.org>

use Gimp;
use Gimp::Fu;
use Gimp::Util;

# Gimp::set_trace(TRACE_ALL);

$defaultfont = "-*-blippo-heavy-r-normal-*-*-360-*-*-p-*-iso8859-1";
undef $defaultfont;
$calls = 0;

sub growfont {
        ($fontname, $plussize) = @_;
        @fontdesc = split /-/, $fontname;
        $fontdesc[8] eq "*" ?  ($fontdesc[7] += $plussize/72) : ($fontdesc[8]+= $plussize);
        $outname = join "-", @fontdesc;
        #print ("!!!${fontdesc[7]}-${fontdesc[8]}!!!\n");
        $calls ++;
        return $outname;
        }                                                                                                                                                        

register "fit_text",
         "Fit Text - fit text to a selection",
         "Have a rectangular selection, and select the font type and spacing.  It will fill the selection with text as closely as possible.  If no selection is made prior to running, it will fill the entire image.",
         "Seth Burgess",
         "Seth Burgess <sjburges\@gimp.org>",
         "2000-01-29",
         N_"<Image>/Filters/Render/Fit Text...",
         "*",
         [
           [PF_FONT, "font", "What font type to use - size will be ignored", $defaultfont],
           [PF_STRING, "string", "Text String to fill with", "Fit Text"],
            ],
         sub {
    my($img,$layer,$xlfd,$string) =@_;
    ($sel,$x1,$y1,$x2,$y2) = $img->gimp_selection_bounds;
    $width = $x2-$x1;
	$height = $y2-$y1;

	@extents=Gimp->text_get_extents_fontname($string,xlfd_size($xlfd),$xlfd);
	$growsize = ($extents[0]<$width && $extents[1]<$height) ? 288 : -288;
	if ($growsize > 0 ) {
		 while ($extents[0]<$width && $extents[1]<$height) {
				$xlfd = growfont($xlfd,$growsize);
				@extents=Gimp->text_get_extents_fontname($string,xlfd_size($xlfd),$xlfd);
				}
		$xlfd = growfont($xlfd, -$growsize);
		}
	else {
		 while ($extents[0]>$width || $extents[1]>$height) {
				$xlfd = growfont($xlfd,$growsize);
				@extents=Gimp->text_get_extents_fontname($string,xlfd_size($xlfd),$xlfd);
				}
		}

	while ($extents[0]<$width && $extents[1]<$height) {
		$xlfd = growfont($xlfd,144); # precision for the last bit
		@extents=Gimp->text_get_extents_fontname($string,xlfd_size($xlfd),$xlfd);
		}

	$xlfd = growfont($xlfd, -144);

	$tmplay = $layer->text_fontname($x1,$y1,$string,0,1,xlfd_size($xlfd), $xlfd);
	$width2=$tmplay->width;
	$height2=$tmplay->height;

# X returns crap, so fine tune it here.
#	print "$width2, $height2:$width, $height\n";
	while ($width2<$width && $height2<$height) {
		$tmplay->remove;
		$xlfd = growfont($xlfd,288);
		$tmplay=$layer->text_fontname($x1,$y1,$string,0,1,xlfd_size($xlfd), $xlfd);
		$width2=$tmplay->width;
		$height2=$tmplay->height;
		}
        
	while ($width2>$width || $height2>$height) {
		$tmplay->remove;
		$xlfd = growfont($xlfd,-72);
		$tmplay=$layer->text_fontname($x1,$y1,$string,0,1,xlfd_size($xlfd), $xlfd);
		$width2=$tmplay->width;
		$height2=$tmplay->height;
		}
	while ($width2<$width && $height2<$height) {
		$tmplay->remove;
		$xlfd = growfont($xlfd,12);
		$tmplay=$layer->text_fontname($x1,$y1,$string,0,1,xlfd_size($xlfd), $xlfd);
		$width2=$tmplay->width;
		$height2=$tmplay->height;
		}
        $tmplay->remove;
	$xlfd = growfont($xlfd,-12);
	$tmplay=$layer->text_fontname($x1,$y1,$string,0,1,
                         xlfd_size($xlfd), $xlfd);
#   print $calls;
    return();
};
exit main;
