source: trunk/gsdl/bin/script/gimp/title_icon.pl@ 1037

Last change on this file since 1037 was 1037, checked in by sjboddie, 24 years ago

added macro translation and gimp image creation perl scripts

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 11.6 KB
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
48my ($cfg_file, $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);
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 " -image_dir directory directory to create images in [`pwd`]\n";
60 print STDERR " this should be full path to existing directory\n";
61 print STDERR " -imagefile filename of image to embed within new icon\n";
62 print STDERR " -width number width of icon [150]\n";
63 print STDERR " -height number height of icon [44]\n";
64 print STDERR " -imageheight number this is the height of the image if the image contains\n";
65 print STDERR " an image (like collection icons) [110]\n";
66 print STDERR " -stripecolor hex_value color of vertical stripe [$gsdl_green]\n";
67 print STDERR " -stripewidth width of vertical stripe [40]\n";
68 print STDERR " -stripe_alignment alignment of vertical stripe (left or right) [left]\n";
69 print STDERR " -i_transparency number transparency of image within icon (0 > transparent > 100) [60]\n";
70 print STDERR " -text string image text\n";
71 print STDERR " -text_alignment alignment of text (left or right) [left]\n";
72 print STDERR " -filename string filename of resulting image\n";
73 print STDERR " -textspace_x number space in pixels between left/right edge of image and\n";
74 print STDERR " left/right edge of text [3]\n";
75 print STDERR " -textspace_y number space in pixels between top of image and top of\n";
76 print STDERR " text [3]\n";
77 print STDERR " -bgcolor hex_value background color of icon [$gsdl_green]\n";
78 print STDERR " -fontcolor hex_value text color [$black]\n";
79 print STDERR " -fontsize number font point size [17]\n";
80 print STDERR " -minfontsize number minimum point size font will be reduced to fit image [10]\n";
81 print STDERR " -fontname string [lucida]\n";
82 print STDERR " -fontweight string [medium]\n";
83 print STDERR " -fontslant [r]\n";
84 print STDERR " -fontwidth [*]\n";
85 print STDERR " -fontspacing [*]\n\n";
86}
87
88sub reset_options {
89 $image_dir = "./";
90 $imagefile = "";
91 $width = 150;
92 $height = 44;
93 $imageheight = 110;
94 $stripecolor = $gsdl_green;
95 $stripewidth = 40;
96 $stripe_alignment = "left";
97 $i_transparency = 60;
98 $text = "";
99 $text_alignment = "left";
100 $filename = "";
101 $textspace_x = 3;
102 $textspace_y = 3;
103 $bgcolor = $gsdl_green;
104 $fontcolor = $black;
105 $fontsize = 17;
106 $minfontsize = 10;
107 $fontname = "lucida";
108 $fontweight = "medium";
109 $fontslant = "r";
110 $fontwidth = "*";
111 $fontspacing = "*";
112}
113
114sub gsdl_title_icon {
115
116 if (!parsargv::parse(\@ARGV,
117 'cfg_file/.*/', \$cfg_file,
118 'image_dir/.*/./', \$image_dir,
119 'imagefile/.*/', \$imagefile,
120 'width/^\d+$/150', \$width,
121 'height/^\d+$/44', \$height,
122 'imageheight/^\d+$/110', \$imageheight,
123 "stripecolor/#[0-9A-Fa-f]{6}/$gsdl_green", \$stripecolor,
124 'stripewidth/^\d+$/40', \$stripewidth,
125 'stripe_alignment/^(left|right)$/left', \$stripe_alignment,
126 'i_transparency/^\d+$/60', \$i_transparency,
127 'text/.*/', \$text,
128 'text_alignment/^(left|right)$/left', \$text_alignment,
129 'filename/.*', \$filename,
130 'textspace_x/^\d+$/3', \$textspace_x,
131 'textspace_y/^\d+$/3', \$textspace_y,
132 "bgcolor/#[0-9A-Fa-f]{6}/$gsdl_green", \$bgcolor,
133 "fontcolor/#[0-9A-Fa-f]{6}/$black", \$fontcolor,
134 'fontsize/^\d+$/17', \$fontsize,
135 'minfontsize/^\d+$/10', \$minfontsize,
136 'fontname/.*/lucida', \$fontname,
137 'fontweight/.*/medium', \$fontweight,
138 'fontslant/.*/r', \$fontslant,
139 'fontwidth/.*/*', \$fontwidth,
140 'fontspacing/.*/*', \$fontspacing)) {
141 &print_usage();
142 die "title_icon.pl: incorrect options\n";
143 }
144
145 # will create wherever gimp was started up from if we don't do this
146 if ($image_dir eq "./") {
147 $image_dir = `pwd`;
148 chomp $image_dir;
149 }
150
151 if ($cfg_file =~ /\w/) {
152
153 open (CONF, $cfg_file) || die "couldn't open cfg_file $cfg_file\n";
154 while (1) {
155
156 &reset_options ();
157
158 # read image configuration entry
159 my $status = &read_config_entry (CONF);
160 if ($filename !~ /\w/) {
161 if ($status) {last;}
162 else {next;}
163 }
164
165 &produce_image ();
166 if ($status) {last;}
167 }
168
169 close CONF;
170
171 } else {
172
173 &produce_image ();
174
175 }
176}
177
178sub produce_image {
179
180 my $use_image = 0;
181 if ($imagefile =~ /\w/) {
182 if (!-r $imagefile) {
183 print STDERR "WARNING (title_icon.pl): imagefile '$imagefile cannot be ";
184 print STDERR "read - $filename will be created without the use of an image file\n";
185 } else {
186 $use_image = 1;
187 $height = $imageheight;
188 }
189 }
190
191 # create the image
192 my $image = gimp_image_new ($width, $height, RGB_IMAGE);
193
194 # background layer
195 my $backlayer = gimp_layer_new ($image, $width, $height, RGB_IMAGE,
196 "BGLayer", 100, NORMAL_MODE);
197
198 # add the background layer
199 gimp_image_add_layer ($image, $backlayer, 0);
200
201 # set colour of stripe
202 gimp_palette_set_foreground ($stripecolor);
203
204 # clear the background
205 gimp_selection_all ($image);
206 gimp_edit_clear ($image, $backlayer);
207 gimp_selection_none ($image);
208
209 # fill in stripe
210 if ($stripe_alignment eq "left") {
211 gimp_rect_select ($image, 0, 0, $stripewidth, $height, 0, 0, 0);
212 } else {
213 gimp_rect_select ($image, $width-$stripewidth, 0, $stripewidth, $height, 0, 0, 0);
214 }
215 gimp_bucket_fill ($image, $backlayer, FG_BUCKET_FILL, NORMAL_MODE, 100, 0, 1, 0, 0);
216 gimp_selection_none ($image);
217
218 # get image file (image goes on opposite side to stripe)
219 if ($use_image) {
220 my $rimage = gimp_file_load (RUN_NONINTERACTIVE, $imagefile, $imagefile);
221 my $rdraw = gimp_image_active_drawable ($rimage);
222 gimp_scale ($rimage, $rdraw, 1, 0, 0, $width-$stripewidth, $height);
223 gimp_edit_copy ($rimage, $rdraw);
224
225 my $imagelayer = gimp_layer_new ($image, $width, $height, RGB_IMAGE,
226 "ImageLayer", $i_transparency, NORMAL_MODE);
227
228 gimp_image_add_layer ($image, $imagelayer, 0);
229
230 # clear the new layer
231 gimp_selection_all ($image);
232 gimp_edit_clear ($image, $imagelayer);
233 gimp_selection_none ($image);
234
235 my $flayer = gimp_edit_paste ($image, $imagelayer, 1);
236 if ($stripe_alignment eq "left") {
237 gimp_layer_set_offsets($flayer, $stripewidth, 0);
238 gimp_layer_set_offsets($imagelayer, $stripewidth, 0);
239 } else {
240 gimp_layer_set_offsets($flayer, 0, 0);
241 gimp_layer_set_offsets($imagelayer, 0, 0);
242 }
243 }
244
245 # set colour of text
246 gimp_palette_set_foreground ($fontcolor);
247
248 # set the text if there is any
249 my ($textlayer, $textheight, $textwidth);
250 my $fsize = $fontsize;
251 if (length($text)) {
252 $text =~ s/\\n/\n/gi;
253 while (1) {
254 $textlayer = gimp_text ($image, $backlayer, 0, 0, $text, 0, 1, $fsize,
255 PIXELS, "*", $fontname, $fontweight, $fontslant,
256 $fontwidth, $fontspacing);
257
258 # check that text fits within image
259 $textwidth = gimp_drawable_width($textlayer);
260 $textheight = gimp_drawable_height($textlayer);
261 if ((($textwidth + $textspace_x) > $width) ||
262 (($textheight + $textspace_y) > $height)) {
263 if ($fsize < $minfontsize) {
264 die "Error (title_icon.pl): text '$text' doesn't fit on ${width}x${height} image " .
265 "(minimum font size tried: $minfontsize\n";
266 } else {
267 gimp_selection_all ($image);
268 gimp_edit_clear ($image, $textlayer);
269 gimp_selection_none ($image);
270 $fsize --;
271 print STDERR "WARNING (title_icon.pl): '$text' doesn't fit: reducing font size to $fsize\n";
272 }
273 } else {
274 last;
275 }
276 }
277
278 # align text
279 if ($text_alignment eq "left") {
280 gimp_layer_set_offsets ($textlayer, $textspace_x, $textspace_y);
281 } else {
282 gimp_layer_set_offsets ($textlayer, ($width-$textwidth)-$textspace_x, $textspace_y);
283 }
284 }
285
286 # flatten the image
287 my $finishedlayer = gimp_image_flatten ($image);
288
289 if ($filename =~ /\.gif$/i) {
290 # make indexed colour (may need to do this for
291 # other formats as well as gif)
292 gimp_convert_indexed ($image, 0, 256);
293 }
294
295 # save image
296 my $filename = &util::filename_cat ($image_dir, $filename);
297 if ($filename =~ /\.jpe?g$/i) {
298 # gimp_file_save doesn't appear to work properly for jpegs
299 file_jpeg_save (RUN_NONINTERACTIVE, $image, $finishedlayer,
300 $filename, $filename, 0.8, 0, 1);
301 } else {
302 gimp_file_save (RUN_NONINTERACTIVE, $image, $finishedlayer,
303 $filename, $filename);
304 }
305}
306
307# returns 1 if this is the last entry,
308sub read_config_entry {
309 my ($handle) = @_;
310
311 my $line = "";
312 while (defined ($line = <$handle>)) {
313 next unless $line =~ /\w/;
314 my @line = ();
315 if ($line =~ /^\-+/) {return 0;}
316 $line =~ s/^\#.*$//; # remove comments
317 $line =~ s/\cM|\cJ//g; # remove end-of-line characters
318 $line =~ s/^\s+//; # remove initial white space
319 while ($line =~ s/\s*(\"[^\"]*\"|\'[^\']*\'|\S+)\s*//) {
320 if (defined $1) {
321 # remove any enclosing quotes
322 my $entry = $1;
323 $entry =~ s/^([\"\'])(.*)\1$/$2/;
324
325 # substitute any environment variables
326 $entry =~ s/\$(\w+)/$ENV{$1}/g;
327 $entry =~ s/\$\{(\w+)\}/$ENV{$1}/g;
328
329 push (@line, $entry);
330 } else {
331 push (@line, "");
332 }
333 }
334 if (scalar (@line) == 2 && defined ${$line[0]}) {
335 ${$line[0]} = $line[1];
336 }
337 }
338 return 1;
339}
340
341sub query {
342
343 gimp_install_procedure("gsdl_title_icon", "create title icons for gsdl",
344 "", "Stefan Boddie", "Stefan Boddie", "2000-03-10",
345 "<Toolbox>/Xtns/gsdl_title_icon", "*", &PROC_EXTENSION,
346 [[PARAM_INT32, "run_mode", "Interactive, [non-interactive]"]], []);
347}
348
349sub net {
350 gsdl_title_icon;
351}
352
353exit main;
354
Note: See TracBrowser for help on using the repository browser.