source: branches/New_Config_Format-branch/gsdl/bin/script/gimp/flash_button.pl@ 1279

Last change on this file since 1279 was 1279, checked in by sjboddie, 24 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
File size: 10.7 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]\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 # replace any '\n' occurring in text with carriage return
140 $text =~ s/\\n/\n/gi;
141
142 if ($cfg_file =~ /\w/) {
143
144 open (CONF, $cfg_file) || die "couldn't open cfg_file $cfg_file\n";
145 while (1) {
146
147 &reset_options ();
148
149 # read image configuration entry
150 my $status = &read_config_entry (CONF);
151 if ($filenamestem !~ /\w/) {
152 if ($status) {last;}
153 else {next;}
154 }
155
156 &produce_images (0);
157 if ($status) {last;}
158 }
159
160 close CONF;
161
162 } else {
163
164 &produce_images (0);
165
166 }
167}
168
169sub produce_images {
170 my ($off_img) = @_;
171
172 # create image, set background color etc.
173 my ($image, $backlayer) = &create_image ($off_img);
174
175 # set the text if there is any
176 if (length($text)) {
177
178 my $textlayer = gimp_text ($image, $backlayer, 0, 0, $text, 0, 1,
179 $fontsize, PIXELS, "*", $fontname, $fontweight,
180 $fontslant, $fontwidth, $fontspacing);
181
182
183 my $textwidth = gimp_drawable_width($textlayer);
184 my $textheight = gimp_drawable_height($textlayer);
185
186 # check that text fits within image
187 if ($textheight > $height) {
188 die "'$text' at fontsize of $fontsize pixels does not fit within image\n" .
189 "$height pixels high. Decrease fontsize or increase image height\n";
190 }
191
192 my $spacers = $width_space * 2;
193 $spacers += 2 if $whitespace;
194
195 if (!$fixed_width || $textwidth > $width) {
196
197 if ($fixed_width) {
198 print STDERR "WARNING (flash_button.pl): '$text' does not fit within $width pixel fixed width ";
199 print STDERR "image. Image width was increased to ",$textwidth + $spacers, " pixels\n";
200 }
201
202 $width = $textwidth + $spacers;
203
204 # recreate image in new size
205 ($image, $backlayer) = &create_image ($off_img);
206 $textlayer = gimp_text ($image, $backlayer, 0, 0, $text, 0, 1,
207 $fontsize, PIXELS, "*", $fontname, $fontweight,
208 $fontslant, $fontwidth, $fontspacing);
209
210 }
211
212 my $y_offset = ($height-$textheight)-int($fontsize/5);
213 if ($text =~ /[gjpqy]/) {
214 # descenders - put text at bottom of image, otherwise
215 # go for fontsize/5 pixels above bottom. This is kind of hacky
216 # and may need some playing with for different fonts/fontsizes
217 $y_offset = $height-$textheight;
218 }
219
220 if ($fixed_width) {
221 if ($dont_center) {
222 # don't center text horizontally (start it width_space pixels from left edge)
223 my $x_offset = $width_space;
224 $x_offset += 1 if $whitespace;
225 gimp_layer_set_offsets ($textlayer, $x_offset, $y_offset);
226 } else {
227 gimp_layer_set_offsets ($textlayer, ($width-$textwidth)/2, $y_offset);
228 }
229 } else {
230 gimp_layer_set_offsets ($textlayer, $spacers / 2, $y_offset);
231 }
232 }
233
234 # add pixel of whitespace to each edge if required
235 if ($whitespace) {
236 gimp_rect_select ($image, 0, 0, 1, $height, 0, 0, 0);
237 gimp_edit_clear ($image, $backlayer);
238 gimp_selection_none ($image);
239 gimp_rect_select ($image, $width-1, 0, 1, $height, 0, 0, 0);
240 gimp_edit_clear ($image, $backlayer);
241 gimp_selection_none ($image);
242 }
243
244 # flatten the image
245 my $finishedlayer = gimp_image_flatten ($image);
246
247 # make indexed colour
248 gimp_convert_indexed ($image, 0, 256);
249
250 # save image
251 my $filename = $filenamestem . "on.gif";
252 $filename = $filenamestem . "of.gif" if $off_img;
253 $filename = &util::filename_cat ($image_dir, $filename);
254
255 gimp_file_save (RUN_NONINTERACTIVE, $image, $finishedlayer, $filename, $filename);
256
257 &produce_images (1) unless $off_img;
258}
259
260sub create_image {
261 my ($off_img) = @_;
262
263 # create the image
264 my $image = gimp_image_new ($width, $height, RGB_IMAGE);
265
266 # background layer
267 my $backlayer = gimp_layer_new ($image, $width, $height, RGB_IMAGE,
268 "BGLayer", 100, NORMAL_MODE);
269
270 # add the background layer
271 gimp_image_add_layer ($image, $backlayer, 0);
272
273 # set colour of background
274 gimp_palette_set_foreground ($bgcolor);
275
276 # clear the background
277 gimp_selection_all ($image);
278 gimp_edit_clear ($image, $backlayer);
279 gimp_selection_none ($image);
280
281 # create the gradient background
282 gimp_blend ($image, $backlayer, 0, NORMAL_MODE, LINEAR, 70, 0, REPEAT_NONE, 0, 0, 0, 5, $height-3, 5, 0);
283
284 # adjust lightness of "off" gif
285 gimp_hue_saturation ($image, $backlayer, 0, 0, 100, 0) if $off_img;
286
287 # set colour of text
288 gimp_palette_set_foreground ($fontcolor);
289
290 return ($image, $backlayer);
291}
292
293# returns 1 if this is the last entry,
294sub read_config_entry {
295 my ($handle) = @_;
296
297 my $line = "";
298 while (defined ($line = <$handle>)) {
299 next unless $line =~ /\w/;
300 my @line = ();
301 if ($line =~ /^\-+/) {return 0;}
302 $line =~ s/^\#.*$//; # remove comments
303 $line =~ s/\cM|\cJ//g; # remove end-of-line characters
304 $line =~ s/^\s+//; # remove initial white space
305 while ($line =~ s/\s*(\"[^\"]*\"|\'[^\']*\'|\S+)\s*//) {
306 if (defined $1) {
307 # remove any enclosing quotes
308 my $entry = $1;
309 $entry =~ s/^([\"\'])(.*)\1$/$2/;
310
311 # substitute any environment variables
312 $entry =~ s/\$(\w+)/$ENV{$1}/g;
313 $entry =~ s/\$\{(\w+)\}/$ENV{$1}/g;
314
315 push (@line, $entry);
316 } else {
317 push (@line, "");
318 }
319 }
320 if (scalar (@line) == 2 && defined ${$line[0]}) {
321 ${$line[0]} = $line[1];
322 }
323 }
324 return 1;
325}
326
327sub query {
328
329 gimp_install_procedure("gsdl_flash_button", "create flashy javascript buttons for gsdl",
330 "", "Stefan Boddie", "Stefan Boddie", "2000-03-10",
331 "<Toolbox>/Xtns/gsdl_flash_button", "*", &PROC_EXTENSION,
332 [[PARAM_INT32, "run_mode", "Interactive, [non-interactive]"]], []);
333}
334
335sub net {
336 gsdl_flash_button;
337}
338
339exit main;
Note: See TracBrowser for help on using the repository browser.