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