root/branches/New_Config_Format-branch/gsdl/bin/script/gimp/title_icon.pl @ 1279

Revision 1279, 12.9 KB (checked in by sjboddie, 20 years ago)

merged changes to trunk into New_Config_Format branch

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# title_icon.pl
6# A component of the Greenstone digital library software
7# from the New Zealand Digital Library Project at the
8# University of Waikato, New Zealand.
9#
10# Copyright (C) 1999 New Zealand Digital Library Project
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25#
26###########################################################################
27
28# title_icon.pl generates all the green_title type icons and
29# collection icons for Greenstone
30
31BEGIN {
32    die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
33    unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
34}
35
36use Gimp;
37use parsargv;
38use util;
39
40# set trace level to watch functions as they are executed
41#Gimp::set_trace(TRACE_ALL);
42#Gimp::set_trace(TRACE_CALL);
43
44my $gsdl_green = "#96c19b";
45my $black = "#000000";
46
47
48local ($cfg_file, $size, $imagefile, $width, $height, $imageheight, $stripecolor, $stripewidth,
49       $stripe_alignment, $i_transparency, $text, $text_alignment, $filename, $textspace_x,
50       $textspace_y, $bgcolor, $fontcolor, $fontsize, $minfontsize, $fontname,
51       $fontweight, $fontslant, $fontwidth, $fontspacing, $image_dir, $dont_wrap);
52
53sub print_usage {
54    print STDERR "\n  usage: $0 [options] macrofile\n\n";
55    print STDERR "  options:\n";
56    print STDERR "   -cfg_file file         configuration file containing one or more\n";
57    print STDERR "                          sets of the following options - use to create\n";
58    print STDERR "                          batches of images\n";
59    print STDERR "   -size number           the overall size ratio of the image (i.e. a size\n";
60    print STDERR "                          of 2 will create an image twice the default size)\n";
61    print STDERR "   -image_dir directory   directory to create images in [`pwd`]\n";
62    print STDERR "                          this should be full path to existing directory\n";
63    print STDERR "   -imagefile             filename of image to embed within new icon\n";
64    print STDERR "   -width number          width of icon [150]\n";
65    print STDERR "   -height number         height of icon [44]\n";
66    print STDERR "   -imageheight number    this is the height of the image if the image contains\n";
67    print STDERR "                          an image (like collection icons) [110]\n";
68    print STDERR "   -stripecolor hex_value color of vertical stripe [$gsdl_green]\n";
69    print STDERR "   -stripewidth           width of vertical stripe [40]\n";
70    print STDERR "   -stripe_alignment      alignment of vertical stripe (left or right) [left]\n";
71    print STDERR "   -i_transparency number transparency of image within icon (0 > transparent > 100) [60]\n";
72    print STDERR "   -text string           image text\n";
73    print STDERR "   -text_alignment        alignment of text (left or right) [left]\n";
74    print STDERR "   -filename string       filename of resulting image\n";
75    print STDERR "   -textspace_x number    space in pixels between left/right edge of image and\n";
76    print STDERR "                          left/right edge of text [3]\n";
77    print STDERR "   -textspace_y number    space in pixels between top of image and top of\n";
78    print STDERR "                          text [3]\n";
79    print STDERR "   -bgcolor hex_value     background color of icon [$gsdl_green]\n";
80    print STDERR "   -fontcolor hex_value   text color [$black]\n";
81    print STDERR "   -fontsize number       font point size [17]\n";
82    print STDERR "   -minfontsize number    minimum point size font will be reduced to fit image [10]\n";
83    print STDERR "   -fontname string       [lucida]\n";
84    print STDERR "   -fontweight string     [medium]\n";
85    print STDERR "   -fontslant             [r]\n";
86    print STDERR "   -fontwidth             [*]\n";
87    print STDERR "   -fontspacing           [*]\n";
88    print STDERR "   -dont_wrap             don't attempt to wrap text\n\n";
89}
90
91sub reset_options {
92    $image_dir = "./";
93    $imagefile = "";
94    $width = int (150 * $size);
95    $height = int (44 * $size);
96    $imageheight = int (110 * $size);
97    $stripecolor = $gsdl_green;
98    $stripewidth = int (40 * $size);
99    $stripe_alignment = "left";
100    $i_transparency = 60;
101    $text = "";
102    $text_alignment = "left";
103    $filename = "";
104    $textspace_x = int (3 * $size);
105    $textspace_y = int (3 * $size);
106    $bgcolor = $gsdl_green;
107    $fontcolor = $black;
108    $fontsize = int (17 * $size);
109    $minfontsize = int (10 * $size);
110    $fontname = "lucida";
111    $fontweight = "medium";
112    $fontslant = "r";
113    $fontwidth = "*";
114    $fontspacing = "*";
115}
116
117sub gsdl_title_icon {
118
119    if (!parsargv::parse(\@ARGV,
120             'cfg_file/.*/', \$cfg_file,
121             'size/\d+/1', \$size,
122             'image_dir/.*/./', \$image_dir,
123             'imagefile/.*/', \$imagefile,
124             'width/^\d+$/150', \$width,
125             'height/^\d+$/44', \$height,
126             'imageheight/^\d+$/110', \$imageheight,
127             "stripecolor/#[0-9A-Fa-f]{6}/$gsdl_green", \$stripecolor,
128             'stripewidth/^\d+$/40', \$stripewidth,
129             'stripe_alignment/^(left|right)$/left', \$stripe_alignment,
130             'i_transparency/^\d+$/60', \$i_transparency,
131             'text/.*/', \$text,
132             'text_alignment/^(left|right)$/left', \$text_alignment,
133             'filename/.*', \$filename,
134             'textspace_x/^\d+$/3', \$textspace_x,
135             'textspace_y/^\d+$/3', \$textspace_y,
136             "bgcolor/#[0-9A-Fa-f]{6}/$gsdl_green", \$bgcolor,
137             "fontcolor/#[0-9A-Fa-f]{6}/$black", \$fontcolor,
138             'fontsize/^\d+$/17', \$fontsize,
139             'minfontsize/^\d+$/10', \$minfontsize,
140             'fontname/.*/lucida', \$fontname,
141             'fontweight/.*/medium', \$fontweight,
142             'fontslant/.*/r', \$fontslant,
143             'fontwidth/.*/*', \$fontwidth,
144             'fontspacing/.*/*', \$fontspacing,
145             'dont_wrap', \$dont_wrap)) {
146    &print_usage();
147    die "title_icon.pl: incorrect options\n";
148    }
149
150    # will create wherever gimp was started up from if we don't do this
151    if ($image_dir eq "./") {
152    $image_dir = `pwd`;
153    chomp $image_dir;
154    }
155
156    if ($cfg_file =~ /\w/) {
157   
158    open (CONF, $cfg_file) || die "couldn't open cfg_file $cfg_file\n";
159    while (1) {
160
161        &reset_options ();
162   
163        # read image configuration entry
164        my $status = &read_config_entry (CONF);
165        if ($filename !~ /\w/) {
166        if ($status) {last;}
167        else {next;}
168        }
169
170        &produce_image ();
171        if ($status) {last;}
172    }
173
174    close CONF;
175
176    } else {
177
178    &produce_image ();
179
180    }
181}
182
183sub produce_image {
184
185    &adjust_args ();
186    &wrap_text () unless $dont_wrap;
187
188    my $use_image = 0;
189    if ($imagefile =~ /\w/) {
190    if (!-r $imagefile) {
191        print STDERR "WARNING (title_icon.pl): imagefile '$imagefile cannot be ";
192        print STDERR "read - $filename will be created without the use of an image file\n";
193    } else {
194        $use_image = 1;
195        $height = $imageheight;
196    }
197    }
198
199    # create the image
200    my $image = gimp_image_new ($width, $height, RGB_IMAGE);
201   
202    # background layer
203    my $backlayer = gimp_layer_new ($image, $width, $height, RGB_IMAGE,
204                    "BGLayer", 100, NORMAL_MODE);
205   
206    # add the background layer
207    gimp_image_add_layer ($image, $backlayer, 0);
208   
209    # set colour of stripe
210    gimp_palette_set_foreground ($stripecolor);
211   
212    # clear the background
213    gimp_selection_all ($image);
214    gimp_edit_clear ($image, $backlayer);
215    gimp_selection_none ($image);
216   
217    # fill in stripe
218    if ($stripe_alignment eq "left") {
219    gimp_rect_select ($image, 0, 0, $stripewidth, $height, 0, 0, 0);
220    } else {
221    gimp_rect_select ($image, $width-$stripewidth, 0, $stripewidth, $height, 0, 0, 0);
222    }
223    gimp_bucket_fill ($image, $backlayer, FG_BUCKET_FILL, NORMAL_MODE, 100, 0, 1, 0, 0);
224    gimp_selection_none ($image);
225   
226    # get image file (image goes on opposite side to stripe)
227    if ($use_image) {
228    my $rimage = gimp_file_load (RUN_NONINTERACTIVE, $imagefile, $imagefile);
229    my $rdraw = gimp_image_active_drawable ($rimage);
230    gimp_scale ($rimage, $rdraw, 1, 0, 0, $width-$stripewidth, $height);
231    gimp_edit_copy ($rimage, $rdraw);
232   
233    my $imagelayer = gimp_layer_new ($image, $width, $height, RGB_IMAGE,
234                     "ImageLayer", $i_transparency, NORMAL_MODE);
235   
236    gimp_image_add_layer ($image, $imagelayer, 0);
237   
238    # clear the new layer
239    gimp_selection_all ($image);
240    gimp_edit_clear ($image, $imagelayer);
241    gimp_selection_none ($image);
242   
243    my $flayer = gimp_edit_paste ($image, $imagelayer, 1);
244    if ($stripe_alignment eq "left") {
245        gimp_layer_set_offsets($flayer, $stripewidth, 0);
246        gimp_layer_set_offsets($imagelayer, $stripewidth, 0);
247    } else {
248        gimp_layer_set_offsets($flayer, 0, 0);
249        gimp_layer_set_offsets($imagelayer, 0, 0);
250    }
251    }
252   
253    # set colour of text
254    gimp_palette_set_foreground ($fontcolor);
255   
256    # set the text if there is any
257    my ($textlayer, $textheight, $textwidth);
258    my $fsize = $fontsize;
259    if (length($text)) {
260    $text =~ s/\\n/\n/gi;
261    while (1) {
262        $textlayer = gimp_text ($image, $backlayer, 0, 0, $text, 0, 1, $fsize,
263                    PIXELS, "*", $fontname, $fontweight, $fontslant,
264                    $fontwidth, $fontspacing);
265       
266        # check that text fits within image
267        $textwidth = gimp_drawable_width($textlayer);
268        $textheight = gimp_drawable_height($textlayer);
269        if ((($textwidth + $textspace_x) > $width) ||
270        (($textheight + $textspace_y) > $height)) {
271        if ($fsize < $minfontsize) {
272            die "Error (title_icon.pl): text '$text' doesn't fit on ${width}x${height} image " .
273            "(minimum font size tried: $minfontsize\n";
274        } else {
275            gimp_selection_all ($image);
276            gimp_edit_clear ($image, $textlayer);
277            gimp_selection_none ($image);
278            $fsize --;
279            print STDERR "WARNING (title_icon.pl): '$text' doesn't fit: reducing font size to $fsize\n";
280        }
281        } else {
282        last;
283        }
284    }
285   
286    # align text
287    if ($text_alignment eq "left") {
288        gimp_layer_set_offsets ($textlayer, $textspace_x, $textspace_y);
289    } else {
290        gimp_layer_set_offsets ($textlayer, ($width-$textwidth)-$textspace_x, $textspace_y);
291    }
292    }
293   
294    # flatten the image
295    my $finishedlayer = gimp_image_flatten ($image);
296   
297    if ($filename =~ /\.gif$/i) {
298    # make indexed colour (may need to do this for
299    # other formats as well as gif)
300    gimp_convert_indexed ($image, 0, 256);
301    }
302   
303    # save image
304    my $filename = &util::filename_cat ($image_dir, $filename);
305    if ($filename =~ /\.jpe?g$/i) {
306    # gimp_file_save doesn't appear to work properly for jpegs
307    file_jpeg_save (RUN_NONINTERACTIVE, $image, $finishedlayer,
308            $filename, $filename, 0.8, 0, 1);
309    } else {
310    gimp_file_save (RUN_NONINTERACTIVE, $image, $finishedlayer,
311            $filename, $filename);
312    }
313}
314
315# returns 1 if this is the last entry,
316sub read_config_entry {
317    my ($handle) = @_;
318
319    my $line = "";
320    while (defined ($line = <$handle>)) {
321    next unless $line =~ /\w/;
322    my @line = ();
323    if ($line =~ /^\-+/) {return 0;}
324    $line =~ s/^\#.*$//;   # remove comments
325    $line =~ s/\cM|\cJ//g; # remove end-of-line characters
326    $line =~ s/^\s+//;     # remove initial white space
327    while ($line =~ s/\s*(\"[^\"]*\"|\'[^\']*\'|\S+)\s*//) {
328        if (defined $1) {
329        # remove any enclosing quotes
330        my $entry = $1;
331        $entry =~ s/^([\"\'])(.*)\1$/$2/;
332
333        # substitute any environment variables
334        $entry =~ s/\$(\w+)/$ENV{$1}/g;
335        $entry =~ s/\$\{(\w+)\}/$ENV{$1}/g;
336       
337        push (@line, $entry);
338        } else {
339        push (@line, "");
340        }
341    }
342    if (scalar (@line) == 2 && defined ${$line[0]}) {
343        ${$line[0]} = $line[1];
344    }
345    }
346    return 1;
347}
348
349# adjust arguments that are effected by the size argument
350sub adjust_args {   
351
352    if ($size != 1) {
353    my @size_args = ('width', 'height', 'imageheight', 'stripewidth',
354             'textspace_x', 'textspace_y', 'fontsize', 'minfontsize');
355    foreach $arg (@size_args) {
356        $$arg = int ($$arg * $size);
357    }
358    }
359}
360
361sub wrap_text {
362
363    # don't wrap text if it already contains carriage returns
364    return if $text =~ /\n/;
365
366    # the following assumes that all words are less than $wrap_length long
367    my $wrap_length = 14;
368
369    my $new_text = "";
370    while (length ($text) >= $wrap_length) {
371    my $line = substr ($text, 0, $wrap_length);
372    $text =~ s/^$line//;
373    $line =~ s/\s([^\s]*)$/\n/;
374    $text = $1 . $text;
375    $new_text .= $line;
376    }
377    $new_text .= $text;
378    $text = $new_text;
379}
380   
381sub query {
382
383  gimp_install_procedure("gsdl_title_icon", "create title icons for gsdl",
384                        "", "Stefan Boddie", "Stefan Boddie", "2000-03-10",
385                        "<Toolbox>/Xtns/gsdl_title_icon", "*", &PROC_EXTENSION,
386                        [[PARAM_INT32, "run_mode", "Interactive, [non-interactive]"]], []);
387}
388
389sub net {
390  gsdl_title_icon;
391}
392
393exit main;
394
Note: See TracBrowser for help on using the browser.