source: main/trunk/greenstone2/bin/script/gimp/title_icon.pl@ 20999

Last change on this file since 20999 was 2236, checked in by sjboddie, 23 years ago

Made a couple of changes to image creation scripts to handle cyrillic
characters

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