#!/usr/bin/perl

###########################################################################
#
# make-images.pl
#
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# Copyright (C) 2005 New Zealand Digital Library Project
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# --------------------------------------------------------------------------
#
# This script is an alternative to translate.pl that uses ImageMagick
# instead of Gimp to generate the button images. The advantage of using
# ImageMagick is that it supports Unicode (and other multibyte encodings).
#
# This script requires the following components installed:
#
#  - A recent version of ImageMagick, and the PerlMagick extensions.
#      (http://www.imagemagick.org)
#
#  - The FreeType libraries (http://freetype.sourceforge.net)
#
# The script reads through the macrofile specified at the command line,
# and generates images for each of the image macros. It does this by
# 'pasting' the button text onto a set of pre-generated images, using the
# annotate.pl script. The background images used, and the arguments to
# annotate.pl, depend on the type of image (eg. nav_bar_button) being
# generated.
#
###########################################################################


BEGIN {
    die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
}


use parsargv;
use unicode;
use util;


my $fribidi_path = "/home/mdewsnip/packages/fribidi-0.10.4/bin";


# -------------------------------------------------------------------------
#    This script can use any TrueType font (the 'font' argument).
#
#    The base font size should be a reasonable size for nav_bar_buttons.
#  A value of 15 is likely to be a good starting point for most fonts. The
#  baseline offset value (a fraction) should be set if the text for the
#  language is displayed too high or too low.
#
#    ARIALBD.TTF font: partial Unicode support, bold
#      font size = 15 (calculated so text_height = 17)
#
#    ARIALUNI.TTF font: complete Unicode 2.0 support, but no bold
#      font size = 11 (calculated so text_height = 17)
#
#    Available options:
#
#      font                                        (default ARIALBD.TTF)
#      base_font_size                              (default 15)
#      baseline_offset_factor (moves text down)    (default 0.0)
#      right_to_left                               (default false)
#      no_nav_bar_button_text_height_warnings      (default false)
#      document_button_font_size
#      document_button_width                       (default 66)
#      document_button_height                      (default 30)
#      collector_bar_button_width                  (default 77)
#      collector_bar_button_height                 (default 28)
#      home_page_button_width                      (default 100)
#
# -------------------------------------------------------------------------
%language_specifications = 
    (
     # Arabic
     "ar", { 'base_font_size' => "16",
	     'right_to_left' => 1,
	     'no_nav_bar_button_text_height_warnings' => 1 },

     # Bengali
     "bn", { 'font' => "ARIALUNI.TTF",
	     'base_font_size' => "16",
	     'baseline_offset_factor' => (1/5),
	     'no_nav_bar_button_text_height_warnings' => 1 },

     # Catalan
     "ca", { 'document_button_width' => "67",
	     'collector_bar_button_width' => "80" },

     # Czech
     "cs", { 'document_button_width' => "79" },

     # German
     "de", { 'document_button_width' => "108" },

     # Greek
     "el", { 'document_button_width' => "100" },

     # English
     "en", { },

     # Spanish
     "es", { 'document_button_width' => "67",
	     'collector_bar_button_width' => "106" },

     # Farsi
     "fa", { 'base_font_size' => "16",
	     'right_to_left' => 1,
	     'no_nav_bar_button_text_height_warnings' => 1 },

     # Finnish
     "fi", { 'document_button_width' => "81" },

     # French
     "fr", { 'document_button_width' => "107",
	     'collector_bar_button_width' => "92" },

     # Galician
     "gl", { 'document_button_width' => "71",
	     'collector_bar_button_width' => "103" },

     # Hebrew
     "he", { },

     # Hindi -- not tried yet
     # "hi", { },

     # Croatian
     "hr", { 'document_button_width' => "87" },

     # Armenian
     "hy", { 'font' => "ARIALUNI.TTF",
	     'base_font_size' => "18",
	     'baseline_offset_factor' => (1/9),
	     'no_nav_bar_button_text_height_warnings' => 1,
	     'document_button_font_size' => "11",
	     'document_button_width' => "108" },

     # Indonesian
     "id", { 'document_button_width' => "80" },

     # Italian
     "it", { 'document_button_width' => "76",
	     'collector_bar_button_width' => "89" },

     # Japanese
     "ja", { 'font' => "ARIALUNI.TTF",
	     'base_font_size' => "15",
	     'baseline_offset_factor' => (1/5) },

     # Georgian
     "ka", { 'font' => "ARIALUNI.TTF",
	     'base_font_size' => "15",
	     'baseline_offset_factor' => (1/15),
	     'no_nav_bar_button_text_height_warnings' => 1,
	     'document_button_width' => "100",
	     'collector_bar_button_width' => "87",
	     'collector_bar_button_height' => "45" },

     # Kazakh
     "kk", { 'document_button_width' => "101",
	     'collector_bar_button_width' => "103",
	     'home_page_button_width' => "106" },

     # Kannada -- not tried yet
     # "kn", { },

     # Kirghiz
     "ky", { },

     # Latvian
     "lv", { },

     # Maori
     "mi", { 'document_button_width' => "97",
	     'document_button_height' => "42" },

     # Mongolian
     "mn", { 'document_button_width' => "95",
	     'collector_bar_button_width' => "84",
	     'collector_bar_button_height' => "42",
	     'home_page_button_width' => "109" },

     # Dutch
     "nl", { 'document_button_width' => "86" },

     # Polish
     "pl", { 'document_button_width' => "83" },

     # Portuguese (Brazil)
     "pt-br", { },

     # Portuguese (Portugal)
     "pt-pt", { },

     # Russian
     "ru", { 'document_button_width' => "69",
	     'collector_bar_button_width' => "91",
	     'home_page_button_width' => "113" },

     # Serbian (Latin version)
     "sr", { 'document_button_width' => "87" },

     # Thai -- not supported because Freetype doesn't support combining characters!
     # "th", { 'font' => "ARIALUNI.TTF" },

     # Turkish
     "tr", { 'document_button_width' => "75" },

     # Ukrainian
     "uk", { 'document_button_width' => "81" },

     # Vietnamese
     "vi", { },

     # Chinese (Simplified)
     "zh", { 'font' => "ARIALUNI.TTF",
	     'base_font_size' => "15",
	     'baseline_offset_factor' => (4/15) },

     # Chinese (Traditional)
     "zh-tr", { 'font' => "ARIALUNI.TTF",
	        'base_font_size' => "16",
	        'baseline_offset_factor' => (1/4),
	        'document_button_width' => "67",
		'collector_bar_button_width' => "103",
	        'home_page_button_width' => "106" },

     );


sub print_usage
{
    print STDERR "\n";
    print STDERR "mkimages.pl: Uses ImageMagick to generate images required by a\n";
    print STDERR "              Greenstone macro file.\n\n";
    print STDERR "  usage: $0 [options] macrofile\n\n";
    print STDERR "  options:\n";
    print STDERR "   -language_symbol      ISO abbreviation of language (e.g. German=de,\n";
    print STDERR "                         French=fr, Maori=mi)\n";
    print STDERR "   -image_dir directory  full path to directory in which to create images\n";
    print STDERR "                         (defaults to images)\n\n";
}


sub main
{
    # Parse command line arguments
    if (!parsargv::parse(\@ARGV, 
			 'verbose', \$verbose,
			 'language_symbol/.*', \$language_symbol,
			 'image_dir/.*/images', \$image_dir)) {
	&print_usage();
	die "\n";
    }

    if (!defined $ARGV[0]) {
	print STDERR "Error: No macro file supplied\n";
	&print_usage();
	die "\n";
    }
    my $macrofile = $ARGV[0];
    die "\nError: Macrofile $macrofile does not exist\n\n" unless -e $macrofile;

    if (!-e $image_dir) {
	mkdir ($image_dir, 511) || die "\nError: Couldn't create image_dir $image_dir\n\n";
    }

    if (!$language_specifications{$language_symbol}) {
	die "\nError: No specification exists for this language.\n\n";
    }

    open(INPUT, $macrofile) || die "\nError: Couldn't open $macrofile for reading.\n\n";
    open(OUTPUT, ">$macrofile.new") || die "\nError: Couldn't open temporary file $macrofile.new for writing\n\n";

    &parse_file(INPUT, OUTPUT);

    close(OUTPUT);
    close(INPUT);

    &util::cp($macrofile, "$macrofile.orig");
    &util::mv("$macrofile.new", $macrofile);
}


sub parse_file
{
    my ($input, $output) = @_;

    undef $/;
    my $dmfile = <$input>;
    $/ = "\n";

    # Process all the images in the macrofile
    $dmfile =~ s/(?:^|\n)\#\#\s*\"([^\"]*)\"\s*\#\#\s*([^\s\#]*)\s*\#\#\s*([^\s\#]*)\s*\#\#(.*?)(?=(\n\#|\s*\Z))/&process_image ($1, $2, $3, $4)/esg;

    # Add the language parameters to each macro
    $dmfile =~ s/(\n\s*)(_[^_]*_)\s*(\[[^\]]*\])?\s*\{/$1 . &add_language_param ($2, $3)/esg;
    print $output $dmfile;
}


sub process_image
{
    my ($text, $image_type, $image_name, $image_macros) = @_;

    my $font = $language_specifications{$language_symbol}{"font"} || "ARIALBD.TTF";
    my $base_font_size = $language_specifications{$language_symbol}{"base_font_size"} || "15";
    my $baseline_offset_factor = $language_specifications{$language_symbol}{"baseline_offset_factor"} || "0.0";

    # Convert all "\n" strings in the text into the newline character
    my $text_internal = $text;
    $text_internal =~ s/\\n/\n/g;

    # Most languages have left-to-right text
    my $h_align_flush = "left";

    # A couple of extra steps are necessary for right-to-left text (Arabic, Farsi)
    if (defined($language_specifications{$language_symbol}{"right_to_left"})) {
	# Write the text string out to a file for shape_arabic.pl to work on
	open(STRING_FILE, ">string.raw");
	print STRING_FILE $text_internal;
	close(STRING_FILE);

	# Use shape_arabic.pl to "join" the characters correctly and fribidi to reverse the characters properly
	$text_internal = `perl -S shape_arabic.pl -file string.raw | $fribidi_path/fribidi`;

	# Remove the extra space fribidi adds before the reversed string
	$text_internal =~ s/(^|\n)\s*/$1/g;

	# Remove the temporary text file
	&util::rm("string.raw");

	# Text aligned "flush" will now be at the right 
	$h_align_flush = "right";
    }

    my $text_unicode = &unicode::utf82unicode($text_internal);

    # Edit image macros
    unless ($language_symbol eq "en") {
	$image_macros =~ s/(_httpimg_\/)(?:[^\/\}]*\/)?([^\}]*\.(?:gif|jpe?g|png))/$1$language_symbol\/$2/gs;
    }

    # Generate top_nav_button images (eg. "preferences")
    if ($image_type eq "top_nav_button") {
	my $font_size = $base_font_size;

	my $tnb_options = "-image_width 40 -stretchable_width -image_height 20";
	$tnb_options .= " -padding_left 2 -padding_right 2";
	$tnb_options .= " -baseline_offset " . &round($font_size * $baseline_offset_factor);
	$tnb_options .= " -font $font -font_size $font_size";

	# Generate on and off images
	&annotate_image("tnb_ylon.gif", $tnb_options, $text_unicode, "${image_name}on.gif");
	&annotate_image("tnb_ylof.gif", $tnb_options, $text_unicode, "${image_name}of.gif");
    }

    # Generate nav_bar_button images (eg. "search", classifier buttons)
    elsif ($image_type eq "nav_bar_button") {
	my $font_size = $base_font_size;

	my $nbb_options = "-image_width 87 -image_height 17 -stretchable_width";
	$nbb_options .= " -baseline_offset " . &round($font_size * $baseline_offset_factor);
	$nbb_options .= " -font $font -font_size $font_size";
	$nbb_options .= " -no_text_height_warnings" if $language_specifications{$language_symbol}{"no_nav_bar_button_text_height_warnings"};

	# Generate on and off images, and green image
	&annotate_image("nbb_ylon.gif", $nbb_options, $text_unicode, "${image_name}on.gif");
	&annotate_image("nbb_ylof.gif", $nbb_options, $text_unicode, "${image_name}of.gif");
	&annotate_image("nbb_gron.gif", $nbb_options, $text_unicode, "${image_name}gr.gif");
    }

    # Generate document_button images (eg. "expand text")
    elsif ($image_type eq "document_button") {
	my $font_size = $language_specifications{$language_symbol}{"document_button_font_size"} || &round(($base_font_size * 4) / 5);

	my $document_button_width = $language_specifications{$language_symbol}{"document_button_width"} || "66";
	my $document_button_height = $language_specifications{$language_symbol}{"document_button_height"} || "30";

	my $db_options = "-image_width $document_button_width -image_height $document_button_height";
	$db_options .= " -baseline_offset " . &round($font_size * $baseline_offset_factor);
	$db_options .= " -font $font -font_size $font_size"; 

	# Generate on and off images
	&annotate_image("db_ylon.gif", $db_options, $text_unicode, "${image_name}on.gif");
	&annotate_image("db_ylof.gif", $db_options, $text_unicode, "${image_name}of.gif");
    }

    # Generate collector_bar_button images (eg. "source data")
    elsif ($image_type eq "collector_bar_button") {
	my $font_size = &round(($base_font_size * 4) / 5);

	my $collector_bar_button_width = $language_specifications{$language_symbol}{"collector_bar_button_width"} || "77";
	my $collector_bar_button_height = $language_specifications{$language_symbol}{"collector_bar_button_height"} || "28";

	my $cbb_options = "-image_width $collector_bar_button_width -image_height $collector_bar_button_height";
	$cbb_options .= " -baseline_offset " . &round($font_size * $baseline_offset_factor);
	$cbb_options .= " -font $font -font_size $font_size";

	# Generate on and off images (yellow), on and off images (green), and grey image
	&annotate_image("cbb_ylon.gif", $cbb_options, $text_unicode, "yc${image_name}on.gif");
	&annotate_image("cbb_ylof.gif", $cbb_options, $text_unicode, "yc${image_name}of.gif");
	&annotate_image("cbb_gron.gif", $cbb_options, $text_unicode, "gc${image_name}on.gif");
	&annotate_image("cbb_grof.gif", $cbb_options, $text_unicode, "gc${image_name}of.gif");
	&annotate_image("cbb_gyof.gif", $cbb_options . " -text_colour \"#a0a0a0\"", $text_unicode, "nc${image_name}of.gif");
    }

    # Generate green_bar_left_aligned images (eg. "results" bar)
    elsif ($image_type eq "green_bar_left_aligned") {
	my $font_size = $base_font_size;

	my $lgb_options = "-image_width 537 -image_height 17";
	$lgb_options .= " -h_align $h_align_flush -padding_left 15 -padding_right 15";
	$lgb_options .= " -baseline_offset " . &round($font_size * $baseline_offset_factor);
	$lgb_options .= " -font $font -font_size $font_size";
	$lgb_options .= " -no_text_height_warnings" if $language_specifications{$language_symbol}{"no_nav_bar_button_text_height_warnings"};

	# Generate on image only
	&annotate_image("nbb_gron.gif", $lgb_options, $text_unicode, "${image_name}.gif");
    }

    # Generate green_title images (eg. "about", classifier titles)
    elsif ($image_type eq "green_title") {
	my $font_size = ($base_font_size * 2);

	my $gt_options = "-image_width 200 -image_height 57 -stretchable_width -stretchable_height -note_if_stretched";
	$gt_options .= " -h_align right -padding_left 5 -padding_right 5 -padding_bottom 5 -v_align top";
	if ($text_internal =~ /\n/) {
	    $gt_options .= " -baseline_offset " . &round($font_size * $baseline_offset_factor);
	}
	$gt_options .= " -font $font -font_size $font_size";

	# Generate green title image
	&annotate_image("gt.gif", $gt_options, $text_unicode, "${image_name}.gif");
    }

    # Generate plain collection_icons
    elsif ($image_type eq "collection_icon") {
	my $font_size = &round(($base_font_size * 6) / 5);

	my $ci_options = "-image_width 150 -image_height 44 -stretchable_height";
	$ci_options .= " -h_align $h_align_flush -padding_left 2 -padding_right 2 -v_align top";
	$ci_options .= " -font $font -font_size $font_size";

	# Generate collection icon (small)
	&annotate_image("cism.gif", $ci_options, $text_unicode, "${image_name}_sm.gif");

	$font_size = &round(($font_size * 3) / 2);

	$ci_options = "-image_width 225 -image_height 66 -stretchable_height";
	$ci_options .= " -h_align $h_align_flush -padding_left 4 -padding_right 4 -v_align top";
	$ci_options .= " -font $font -font_size $font_size";

	# Generate collection icon
	&annotate_image("cism.gif", $ci_options, $text_unicode, "${image_name}.gif");
    }

    # Generate documented_example_collection_icons (cog-wheel images)
    elsif ($image_type eq "documented_example_collection_icon") {
	my $font_size = &round(($base_font_size * 6) / 5);

	my $dec_options = "-image_width 150 -image_height 88";
	$dec_options .= " -h_align $h_align_flush -padding_left 1 -padding_right 1 -v_align top";
	$dec_options .= " -font $font -font_size $font_size";

	# Generate documented example collection icon
	&annotate_image("dec.gif", $dec_options, $text_unicode, "${image_name}.gif");
    }

    # Generate toki_collection_icons
    elsif ($image_type eq "toki_collection_icon") {
	my $font_size = &round(($base_font_size * 6) / 5);

	my $toki_options = "-image_width 150 -image_height 110";
	$toki_options .= " -h_align $h_align_flush -padding_left 6 -padding_right 1 -v_align bottom -padding_bottom 20";
	$toki_options .= " -font $font -font_size $font_size";

	# Generate documented example collection icon
	&annotate_image("toki.gif", $toki_options, $text_unicode, "${image_name}.gif");
    }

    # Generate home_page_button images (eg. "administration", "documentation")
    elsif ($image_type eq "home_page_button") {
	my $font_size = &round(($base_font_size * 4) / 5);

	my $home_page_button_width = $language_specifications{$language_symbol}{"home_page_button_width"} || "100";

	my $hpb_options = "-image_width $home_page_button_width -image_height 30 -stretchable_height";
	$hpb_options .= " -baseline_offset " . &round($font_size * $baseline_offset_factor);
	$hpb_options .= " -v_align center -padding_top 2";
	$hpb_options .= " -font $font -font_size $font_size";

	# Generate on and off images
	&annotate_image("hpb_ylon.gif", $hpb_options, $text_unicode, "${image_name}on.gif");
	&annotate_image("hpb_ylof.gif", $hpb_options, $text_unicode, "${image_name}of.gif");
    }

    # ...and that's all we know how to generate
    else {
	print STDERR "WARNING: Unknown image type found ($image_type)\n";
    }

    return "\n\#\# \"$text\" \#\# $image_type \#\# $image_name \#\#$image_macros";
}


sub annotate_image
{
    my ($background_image, $options, $text_unicode, $output_name) = @_;

    # Convert the text to the form ImageMagick requires
    my $text_imagick = "";
    foreach $character (@$text_unicode) {
	if ($character == 0x0A) {
	    # Newline character
	    $text_imagick .= "\\n";
	}
	elsif ($character < 0x80) {
	    # Normal ascii character
	    $text_imagick .= chr($character);
	}
	else {
	    # Unicode character (convert to a four digit hexadecimal value)
	    my $hex_val = sprintf("%lx", $character);
	    $text_imagick .= "\\0x" . '0' x (4 - length($hex_val)) . $hex_val;
	}
    }

    # Use the annotate.pl script to paste the text onto the background image
    my $output_file = &util::filename_cat($image_dir, $output_name);
    my $verbose_option = "";
    $verbose_option = "-verbose" if $verbose;
    print "\nRunning: annotate.pl -background_image $background_image $options -text \"$text_imagick\" -output_file $output_file\n" if $verbose;
    `perl -S annotate.pl -background_image $background_image $options -text \"$text_imagick\" -output_file $output_file $verbose_option`;
}


sub round
{
    my ($fp_number) = @_;
    if ($fp_number < 0.0) {
	return int($fp_number - 0.5);
    }
    else {
	return int($fp_number + 0.5);
    }
}


sub add_language_param {
    my ($macroname, $paramlist) = @_;

    if ($language_symbol eq "en") { 
	return "$macroname {";
    }

    my $first = 1;
    if (defined $paramlist) {
	$paramlist =~ s/^\[//;
	$paramlist =~ s/\]$//;
	my @params = split /\,/, $paramlist;
	$paramlist = "";
	foreach $param (@params) {
	    # remove any existing language parameter
	    if ($param !~ /^l=/) {
		$paramlist .= "," unless $first;
		$paramlist .= $param;
		$first = 0;
	    }
	}
    }
    $paramlist .= "," unless $first;
    $paramlist .= "l=" . $language_symbol;
    return "$macroname [$paramlist] {";
}


&main(@ARGV);
