#!/usr/bin/perl

use Gimp;
use Storable;

$fg	= [140,140,250];
$fg2	= [250,250, 90,];
$bg	= [ 90,  0, 90];
$w1	= 0.5;
$blur	= 0.4;
$font	= "-bitstream-blade runner movie font-demi-i-modern-*-*-*-*-*-p-*-iso8859-1";

$BASE	= "/root/www/src";
#$BASE = "/tmp";

# enable example mode... if disabled, it will write out some logos, and not
# wont' display anything.
$example = 0;

$|=1;

Gimp::init;

%info = (
      'example'		=> [50, "$BASE/images/menu"],
      'headline'	=> [20, "$BASE/images/menu"],
      'sub_on'		=> [12, "$BASE/images/menu"],
      'marc'		=> [25, "$BASE/marc/images/menu"],
      'marcmain'	=> [14, "$BASE/marc/images/menu"],
      'marcprojects'	=> [14, "$BASE/marc/images/menu"],
      'marcbackground'	=> [14, "$BASE/marc/images/menu"],
);

if ($example) {
   @strings = (['example',"Example"]);
} else {
   my %depend = %{ retrieve "/root/www/depend" };
   my %logo;
   while(my($k,$v)=each(%depend)) {
      $logo{join ",",@$_}=$_ for @{$v->{L}};
   }
   @strings = values(%logo);
}

for(@strings) {
   my($class,$string)=@$_;
   my($size,$base)=@{$info{$class}};
   print "($class,$size,$base,$string)\n";
   my($w,$h,$a,$d)=Gimp->text_get_extents_fontname($string,$size,PIXELS,$font);
   system("rm -f $base/*.jpg");
   $h+=$d;
   $w{$class}=$w if $w{$class}<$w;
   $h{$class}=$h if $h{$class}<$h;
}

for(@strings) {
   my($class,$string)=@$_;
   my $fn=$string;
   my($size,$base)=@{$info{$class}};
   print "+";
   $fn=~y/-\/?&! A-Z/______a-z/;
   my $img=create_logo (0,$w{$class},$h{$class},$size,$string,"$base/${fn}_off.jpg") if $class !~ /_on$/;
   my $img=create_logo (1,$w{$class},$h{$class},$size,$string,"$base/${fn}_on.jpg");
}

print "\n";

sub create_logo {
   my($on,$w,$h,$p,$t,$fn)=@_;
   my $img = new Image($w+$w1*$h,$h,RGB);
   my $b = new Layer($img,$w+$w1*$h,$h,RGB_IMAGE,'Background',100,NORMAL_MODE);
   Palette->set_background($bg);
   Palette->set_foreground($on ? $fg2 : $fg);
   $b->fill(BG_IMAGE_FILL);
   $b->add_layer(0);

   $img->display_new if $example;

   $t=~y/L/l/;# dumb font bladerunner

   my $t = $img->text_fontname(undef,0,$blur*$h*-.5,$t,$blur*$h,1,$p,PIXELS,$font);
   my $s = $t->copy(0); $s->add_layer(0);
   my $c = $t->copy(0); $c->add_layer(0);

   $t->mblur(0,$blur*$h,0);
   $t->merge_down(EXPAND_AS_NECESSARY);
   $s->gauss_rle(4,1,1);
   $s->set_mode($on ? SUBTRACT_MODE : SUBTRACT_MODE);
   #Gimp::set_trace(TRACE_NAME);

   if ($on) {
      $c->brightness_contrast(100,0);
      $c->set_mode(ADDITION_MODE);
   } else {
      $s->brightness_contrast(100,0);
      $c->brightness_contrast(-50,0);
      $c->set_mode(ADDITION_MODE);
   }

   $img->flatten->file_jpeg_save($fn,$fn,0.70,0,1,1,"",0,1,0,0);
   Gimp->displays_flush if $example;

   $img->clean_all;

   $img->delete unless $example;
}
