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