source: trunk/gsdl/bin/script/gimp/flash_button.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: 10.6 KB
Line 
1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# flash_button.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# flash_button.pl generates all the flashy javascript type
29# buttons used by Greenstone
30# these are the buttons described in macro files as:
31# top_nav_button
32# nav_bar_button
33# document_button
34
35BEGIN {
36 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
37 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
38}
39
40use Gimp;
41use parsargv;
42use util;
43
44# set trace level to watch functions as they are executed
45#Gimp::set_trace(TRACE_ALL);
46#Gimp::set_trace(TRACE_CALL);
47
48my $gsdl_yellow = "#D2B464";
49my $black = "#000000";
50
51my ($cfg_file, $width, $height, $text, $filenamestem, $fixed_width,
52 $width_space, $whitespace, $dont_center, $bgcolor, $fontcolor,
53 $fontsize, $fontname, $fontweight, $fontslant, $fontwidth,
54 $fontspacing, $image_dir);
55
56sub print_usage {
57 print STDERR "\n usage: $0 [options] macrofile\n\n";
58 print STDERR " options:\n";
59 print STDERR " -cfg_file file configuration file containing one or more\n";
60 print STDERR " sets of the following options - use to create\n";
61 print STDERR " batches of images\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 " -width number width of button [65]\n";
65 print STDERR " -height number height of button [30]\n";
66 print STDERR " -text string button text\n";
67 print STDERR " -filenamestem string filename - two images will be produced -\n";
68 print STDERR " filenamestemon.gif and filenamestemof.gif\n";
69 print STDERR " -fixed_width fix width of image - if this isn't set\n";
70 print STDERR " image will be cropped with with width_space\n";
71 print STDERR " space at each side of text\n";
72 print STDERR " -width_space number width in pixels of blank space to leave at left\n";
73 print STDERR " and right edges of text [1]\n";
74 print STDERR " -whitespace add 1 pixel of white space at left and right edges\n";
75 print STDERR " of image\n";
76 print STDERR " -dont_center don't center text horizontally (only applicable if\n";
77 print STDERR " image is fixed width\n";
78 print STDERR " -bgcolor hex_value background color of button [$gsdl_yellow]\n";
79 print STDERR " -fontcolor hex_value text color [$black]\n";
80 print STDERR " -fontsize number font point size [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 $width = 65;
91 $height = 30;
92 $text = "";
93 $filenamestem = "";
94 $fixed_width = 0;
95 $width_space = 1;
96 $whitespace = 0;
97 $dont_center = 0;
98 $bgcolor = $gsdl_yellow;
99 $fontcolor = $black;
100 $fontsize = 10;
101 $fontname = "lucida";
102 $fontweight = "medium";
103 $fontslant = "r";
104 $fontwidth = "*";
105 $fontspacing = "*";
106}
107
108sub gsdl_flash_button {
109
110 if (!parsargv::parse(\@ARGV,
111 'cfg_file/.*/', \$cfg_file,
112 'image_dir/.*/./', \$image_dir,
113 'width/^\d+$/65', \$width,
114 'height/^\d+$/30', \$height,
115 'text/.*/', \$text,
116 'filenamestem/.*', \$filenamestem,
117 'fixed_width', \$fixed_width,
118 'width_space/^\d+$/1', \$width_space,
119 'whitespace', \$whitespace,
120 'dont_center', \$dont_center,
121 "bgcolor/#[0-9A-Fa-f]{6}/$gsdl_yellow", \$bgcolor,
122 "fontcolor/#[0-9A-Fa-f]{6}/$black", \$fontcolor,
123 'fontsize/^\d+$/10', \$fontsize,
124 'fontname/.*/lucida', \$fontname,
125 'fontweight/.*/medium', \$fontweight,
126 'fontslant/.*/r', \$fontslant,
127 'fontwidth/.*/*', \$fontwidth,
128 'fontspacing/.*/*', \$fontspacing)) {
129 &print_usage();
130 die "flash_button.pl: incorrect options\n";
131 }
132
133 # will create wherever gimp was started up from if we don't do this
134 if ($image_dir eq "./") {
135 $image_dir = `pwd`;
136 chomp $image_dir;
137 }
138
139 if ($cfg_file =~ /\w/) {
140
141 open (CONF, $cfg_file) || die "couldn't open cfg_file $cfg_file\n";
142 while (1) {
143
144 &reset_options ();
145
146 # read image configuration entry
147 my $status = &read_config_entry (CONF);
148 if ($filenamestem !~ /\w/) {
149 if ($status) {last;}
150 else {next;}
151 }
152
153 &produce_images (0);
154 if ($status) {last;}
155 }
156
157 close CONF;
158
159 } else {
160
161 &produce_images (0);
162
163 }
164}
165
166sub produce_images {
167 my ($off_img) = @_;
168
169 # create image, set background color etc.
170 my ($image, $backlayer) = &create_image ($off_img);
171
172 # set the text if there is any
173 if (length($text)) {
174
175 my $textlayer = gimp_text ($image, $backlayer, 0, 0, $text, 0, 1,
176 $fontsize, PIXELS, "*", $fontname, $fontweight,
177 $fontslant, $fontwidth, $fontspacing);
178
179
180 my $textwidth = gimp_drawable_width($textlayer);
181 my $textheight = gimp_drawable_height($textlayer);
182
183 # check that text fits within image
184 if ($textheight > $height) {
185 die "'$text' at fontsize of $fontsize pixels does not fit within image\n" .
186 "$height pixels high. Decrease fontsize or increase image height\n";
187 }
188
189 my $spacers = $width_space * 2;
190 $spacers += 2 if $whitespace;
191
192 if (!$fixed_width || $textwidth > $width) {
193
194 if ($fixed_width) {
195 print STDERR "WARNING (flash_button.pl): '$text' does not fit within $width pixel fixed width ";
196 print STDERR "image. Image width was increased to ",$textwidth + $spacers, " pixels\n";
197 }
198
199 $width = $textwidth + $spacers;
200
201 # recreate image in new size
202 ($image, $backlayer) = &create_image ($off_img);
203 $textlayer = gimp_text ($image, $backlayer, 0, 0, $text, 0, 1,
204 $fontsize, PIXELS, "*", $fontname, $fontweight,
205 $fontslant, $fontwidth, $fontspacing);
206
207 }
208
209 my $y_offset = ($height-$textheight)-int($fontsize/5);
210 if ($text =~ /[gjpqy]/) {
211 # descenders - put text at bottom of image, otherwise
212 # go for fontsize/5 pixels above bottom. This is kind of hacky
213 # and may need some playing with for different fonts/fontsizes
214 $y_offset = $height-$textheight;
215 }
216
217 if ($fixed_width) {
218 if ($dont_center) {
219 # don't center text horizontally (start it width_space pixels from left edge)
220 my $x_offset = $width_space;
221 $x_offset += 1 if $whitespace;
222 gimp_layer_set_offsets ($textlayer, $x_offset, $y_offset);
223 } else {
224 gimp_layer_set_offsets ($textlayer, ($width-$textwidth)/2, $y_offset);
225 }
226 } else {
227 gimp_layer_set_offsets ($textlayer, $spacers / 2, $y_offset);
228 }
229 }
230
231 # add pixel of whitespace to each edge if required
232 if ($whitespace) {
233 gimp_rect_select ($image, 0, 0, 1, $height, 0, 0, 0);
234 gimp_edit_clear ($image, $backlayer);
235 gimp_selection_none ($image);
236 gimp_rect_select ($image, $width-1, 0, 1, $height, 0, 0, 0);
237 gimp_edit_clear ($image, $backlayer);
238 gimp_selection_none ($image);
239 }
240
241 # flatten the image
242 my $finishedlayer = gimp_image_flatten ($image);
243
244 # make indexed colour
245 gimp_convert_indexed ($image, 0, 256);
246
247 # save image
248 my $filename = $filenamestem . "on.gif";
249 $filename = $filenamestem . "of.gif" if $off_img;
250 $filename = &util::filename_cat ($image_dir, $filename);
251
252 gimp_file_save (RUN_NONINTERACTIVE, $image, $finishedlayer, $filename, $filename);
253
254 &produce_images (1) unless $off_img;
255}
256
257sub create_image {
258 my ($off_img) = @_;
259
260 # create the image
261 my $image = gimp_image_new ($width, $height, RGB_IMAGE);
262
263 # background layer
264 my $backlayer = gimp_layer_new ($image, $width, $height, RGB_IMAGE,
265 "BGLayer", 100, NORMAL_MODE);
266
267 # add the background layer
268 gimp_image_add_layer ($image, $backlayer, 0);
269
270 # set colour of background
271 gimp_palette_set_foreground ($bgcolor);
272
273 # clear the background
274 gimp_selection_all ($image);
275 gimp_edit_clear ($image, $backlayer);
276 gimp_selection_none ($image);
277
278 # create the gradient background
279 gimp_blend ($image, $backlayer, 0, NORMAL_MODE, LINEAR, 70, 0, REPEAT_NONE, 0, 0, 0, 5, $height-3, 5, 0);
280
281 # adjust lightness of "off" gif
282 gimp_hue_saturation ($image, $backlayer, 0, 0, 100, 0) if $off_img;
283
284 # set colour of text
285 gimp_palette_set_foreground ($fontcolor);
286
287 return ($image, $backlayer);
288}
289
290# returns 1 if this is the last entry,
291sub read_config_entry {
292 my ($handle) = @_;
293
294 my $line = "";
295 while (defined ($line = <$handle>)) {
296 next unless $line =~ /\w/;
297 my @line = ();
298 if ($line =~ /^\-+/) {return 0;}
299 $line =~ s/^\#.*$//; # remove comments
300 $line =~ s/\cM|\cJ//g; # remove end-of-line characters
301 $line =~ s/^\s+//; # remove initial white space
302 while ($line =~ s/\s*(\"[^\"]*\"|\'[^\']*\'|\S+)\s*//) {
303 if (defined $1) {
304 # remove any enclosing quotes
305 my $entry = $1;
306 $entry =~ s/^([\"\'])(.*)\1$/$2/;
307
308 # substitute any environment variables
309 $entry =~ s/\$(\w+)/$ENV{$1}/g;
310 $entry =~ s/\$\{(\w+)\}/$ENV{$1}/g;
311
312 push (@line, $entry);
313 } else {
314 push (@line, "");
315 }
316 }
317 if (scalar (@line) == 2 && defined ${$line[0]}) {
318 ${$line[0]} = $line[1];
319 }
320 }
321 return 1;
322}
323
324sub query {
325
326 gimp_install_procedure("gsdl_flash_button", "create flashy javascript buttons for gsdl",
327 "", "Stefan Boddie", "Stefan Boddie", "2000-03-10",
328 "<Toolbox>/Xtns/gsdl_flash_button", "*", &PROC_EXTENSION,
329 [[PARAM_INT32, "run_mode", "Interactive, [non-interactive]"]], []);
330}
331
332sub net {
333 gsdl_flash_button;
334}
335
336exit main;
Note: See TracBrowser for help on using the repository browser.