source: gsdl/trunk/bin/script/gimp/green_bar.pl@ 19620

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