package GimpSite::Text;

use strict;

require GimpSite;
require Digest::MD5;
require BlockBin;
use Gimp;

BEGIN {
    Gimp::init($GimpSite::Config{'gimp'}); # connect to GIMP - FIXME - check errors
}

# FIXME - this all sucks, there should be a general solution/interface.

# This one generates a .gif of text and returns the URI, width and height.

sub Render ($) {
    my $text = shift; 

    $text = BlockBin::unescape_html (undef, $text);

    # we want a simple filename but still want to ensure uniqueness
    my $md5 = uc(Digest::MD5::md5_hex($text));

    # make simple filename
    my $file = lc($text);
    $file =~ s/[^\w]//g;
    $file .= "-$md5.gif";

    # where do we put them?
    my $furi = $GimpSite::Config{'imguri'};
    my $fdir = $GimpSite::Config{'imgdir'};

    unless (-f "$fdir/$file") { # generate file!
        my $fontdir = $GimpSite::Config{'fontdir'};
        my $font = 'tt0510m_';                # FIXME - move to Config

        # FIXME - do some locking!

        eval { Gimp->message_get_handler; 1 } ||
            Gimp::init($GimpSite::Config{'gimp'});
            # connect to GIMP - FIXME - check if ok

        my $Itext = new Gimp::Image(520, 61, RGB);
        my $Lbg = $Itext->layer_new(520, 61, RGB_IMAGE, 'Background', 100, NORMAL_MODE);

        $Lbg->add_layer(0);
        Palette->set_foreground([0, 0, 0]);
        Palette->set_background([255, 0, 0]);
        $Lbg->edit_fill(BG_IMAGE_FILL);

        my $Ltext = $Lbg->freetype ( # render text via gimp-freetype
            "$fontdir/$font.ttf",   # font file
            48,                     # font size
            PIXELS,                 # size unit
            1, 0, 0, 1,             # transform matrix
            1,                      # kerning
            1,                      # hinting
            1,                      # antialiasing
            0,                      # bezier outline
            0,                      # spacing
            ' ' . $text,            # text FIXME - hackish
        );

        $Itext->crop (514, 54, 6, 7);
        $Ltext->resize (514, 54, 0, 0);

        my $Lshad = $Ltext->copy(0);
        $Lshad->add_layer(1);

        $Lshad->set_offsets (2, 2);
        $Lshad->gauss_rle (2, 1, 1);
        $Lshad->set_opacity (25);

        $Ltext->edit_copy;
        my $Iheight = new Gimp::Image (514, 54, GRAY);
        my $Lhtemp = $Iheight->layer_new (514, 54, GRAY_IMAGE, 'Background', 100, NORMAL_MODE);
        $Lhtemp->add_layer(0);

        Palette->set_background([255, 255, 255]);
        $Lhtemp->edit_fill(BG_IMAGE_FILL);
        $Lhtemp->edit_paste (0);
        my $Lheight = $Iheight->gimp_image_flatten();
        $Lheight->gauss_rle (3, 1, 1);
        $Lheight->invert;
        $Lheight->levels (VALUE_LUT, 31, 255, 1.5, 0, 255);

        $Ltext->set_preserve_trans (1);
        Palette->set_foreground([255, 191, 169]);
        $Ltext->edit_fill(FG_IMAGE_FILL);

        $Ltext->lighting (
            $Lheight,           # bumpmap drawable
            undef,              # environment map drawable
            1,                  # do bumpmap?
            0,                  # do environment?
            1,                  # bump type (0 = linear, 1 = log ...)
            0,                  # light type (0 = point ...)
            [1.0, 1.0, 0.9],    # light color
            -8.0, -4.0, 8.0,    # light position
            0.0, 0.0, 0.0,      # light direction (unused w/point light)
            0.45,               # material ambient
            0.90,               # material diffuse
            0.40,               # reflection diffuse
            0.10,               # reflection specular
            27.0,               # reflection highlight
            1,                  # antialiasing?
            0,                  # make new image?
            0                   # transparent background?
        );

        Palette->set_background([207, 239, 223]);

        $Itext->remove_layer($Lbg);
        $Itext->merge_visible_layers(0);
        my $Lall = $Itext->get_active_layer;
        $Lall->semiflatten;
        $Lall = $Itext->get_active_layer;
        $Lall->resize (514, 51, 0, 0);

        $Itext->crop (512, 48, 2, 3);
        $Itext->convert_indexed (0, 0, 255, 0, 1, 0);

        ##########

        my $Sdwb = $Itext->get_active_layer;
        $Sdwb->file_gif_save ("$fdir/$file", $file, 1, 0, 0, 0);

        $Iheight->delete ();
        $Itext->delete ();

        # FIXME - do some unlocking!
    }

    return ("$furi/$file", 512, 48);
}

1;

# EOF
