1 | #!/usr/bin/perl -w
|
---|
2 |
|
---|
3 | ###########################################################################
|
---|
4 | #
|
---|
5 | # green_bar.pl
|
---|
6 | #
|
---|
7 | # A component of the Greenstone digital library software
|
---|
8 | # from the New Zealand Digital Library Project at the
|
---|
9 | # University of Waikato, New Zealand.
|
---|
10 | #
|
---|
11 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
12 | #
|
---|
13 | # This program is free software; you can redistribute it and/or modify
|
---|
14 | # it under the terms of the GNU General Public License as published by
|
---|
15 | # the Free Software Foundation; either version 2 of the License, or
|
---|
16 | # (at your option) any later version.
|
---|
17 | #
|
---|
18 | # This program is distributed in the hope that it will be useful,
|
---|
19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
21 | # GNU General Public License for more details.
|
---|
22 | #
|
---|
23 | # You should have received a copy of the GNU General Public License
|
---|
24 | # along with this program; if not, write to the Free Software
|
---|
25 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
26 | #
|
---|
27 | ###########################################################################
|
---|
28 |
|
---|
29 | # green_bar.pl
|
---|
30 | #
|
---|
31 | # This script generates the black on green gradient background images
|
---|
32 | # used by Greenstone. these are the icons described in macro files as:
|
---|
33 | # green version of nav_bar_button green_bar_left_aligned
|
---|
34 | #
|
---|
35 | # This version has been rewritten for the GIMP Version 1.2. You
|
---|
36 | # should be able to run it if you install gimp 1.2 with perl support.
|
---|
37 | # In Debian this means running "apt-get install gimp1.2 gimp1.2perl"
|
---|
38 |
|
---|
39 |
|
---|
40 | BEGIN {
|
---|
41 | die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
|
---|
42 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
|
---|
43 | }
|
---|
44 |
|
---|
45 | use Gimp ":auto";
|
---|
46 | use Gimp::Fu;
|
---|
47 |
|
---|
48 | use util;
|
---|
49 | use unicode;
|
---|
50 |
|
---|
51 | # set trace level to watch functions as they are executed
|
---|
52 | #Gimp::set_trace(TRACE_ALL);
|
---|
53 | #Gimp::set_trace(TRACE_CALL);
|
---|
54 |
|
---|
55 | my $gsdl_green = "#96c19b";
|
---|
56 | my $black = "#000000";
|
---|
57 | my $white = "#FFFFFF";
|
---|
58 |
|
---|
59 | my ($current_dir) = `pwd`;
|
---|
60 | chomp($current_dir);
|
---|
61 |
|
---|
62 | my ($cfg_file, $width, $height, $text, $filename, $width_space,
|
---|
63 | $alignment, $fgcolor, $bgcolor, $fontcolor, $fontsize, $foundry, $fontname,
|
---|
64 | $fontweight, $fontslant, $fontwidth, $fontspacing, $image_dir);
|
---|
65 |
|
---|
66 | sub reset_options {
|
---|
67 | $image_dir = "./";
|
---|
68 | $width = 87;
|
---|
69 | $height = 17;
|
---|
70 | $text = "";
|
---|
71 | $filename = "";
|
---|
72 | $width_space = 1;
|
---|
73 | $alignment = "eft";
|
---|
74 | $fgcolor = $white;
|
---|
75 | $bgcolor = $gsdl_green;
|
---|
76 | $fontcolor = $black;
|
---|
77 | $fontsize = 17;
|
---|
78 | $foundry = "*";
|
---|
79 | $fontname = "lucida";
|
---|
80 | $fontweight = "medium";
|
---|
81 | $fontslant = "r";
|
---|
82 | $fontwidth = "*";
|
---|
83 | $fontspacing = "*";
|
---|
84 | }
|
---|
85 |
|
---|
86 | sub gsdl_green_bar {
|
---|
87 |
|
---|
88 | ($text, $filename, $image_dir, $width, $height, $fgcolor, $bgcolor,
|
---|
89 | $fontcolor, $fontsize, $alignment, , $width_space, $foundry,
|
---|
90 | $fontname, $fontweight, $fontslant, $fontwidth, $fontspacing,
|
---|
91 | $cfg_file) = @_;
|
---|
92 |
|
---|
93 | # Read a configuration file
|
---|
94 | if ($cfg_file =~ /\w/) {
|
---|
95 |
|
---|
96 | open (CONF, $cfg_file) || die "couldn't open cfg_file $cfg_file\n";
|
---|
97 | while (1) {
|
---|
98 |
|
---|
99 | &reset_options ();
|
---|
100 |
|
---|
101 | # read image configuration entry
|
---|
102 | my $status = &read_config_entry (CONF);
|
---|
103 | if ($filename !~ /\w/) {
|
---|
104 | if ($status) {last;}
|
---|
105 | else {next;}
|
---|
106 | }
|
---|
107 |
|
---|
108 | &produce_image ();
|
---|
109 | if ($status) {last;}
|
---|
110 | }
|
---|
111 |
|
---|
112 | close CONF;
|
---|
113 |
|
---|
114 | }
|
---|
115 |
|
---|
116 | # Produce an image based onlyon command-line parameters
|
---|
117 | else {
|
---|
118 |
|
---|
119 | &produce_image ();
|
---|
120 |
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | sub produce_image {
|
---|
126 |
|
---|
127 | # Create background image
|
---|
128 | my ($image, $backlayer) = &create_image ();
|
---|
129 |
|
---|
130 | # set the text if there is any
|
---|
131 | if (length($text)) {
|
---|
132 |
|
---|
133 | my $textlayer = gimp_text ($image, $backlayer, 0, 0, $text, 0, 1,
|
---|
134 | $fontsize, PIXELS, $foundry, $fontname, $fontweight,
|
---|
135 | $fontslant, $fontwidth, $fontspacing, "*", "*");
|
---|
136 |
|
---|
137 |
|
---|
138 | my $textwidth = gimp_drawable_width($textlayer);
|
---|
139 | my $textheight = gimp_drawable_height($textlayer);
|
---|
140 |
|
---|
141 | # check that text fits within image
|
---|
142 | if ($textheight > $height) {
|
---|
143 | die "'$text' at fontsize of $fontsize pixels does not fit within image\n" .
|
---|
144 | "$height pixels high. Decrease fontsize or increase image height\n";
|
---|
145 | }
|
---|
146 |
|
---|
147 | my $spacers = $width_space * 2;
|
---|
148 |
|
---|
149 | if ($textwidth > $width) {
|
---|
150 |
|
---|
151 | print STDERR "WARNING (green_bar.pl): '$text' does not fit within $width pixel fixed width ";
|
---|
152 | print STDERR "image. Image width was increased to ",$textwidth + $spacers, " pixels\n";
|
---|
153 |
|
---|
154 | $width = $textwidth + $spacers;
|
---|
155 |
|
---|
156 | # recreate image in new size
|
---|
157 | ($image, $backlayer) = &create_image ();
|
---|
158 | $textlayer = gimp_text ($image, $backlayer, 0, 0, $text, 0, 1,
|
---|
159 | $fontsize, PIXELS, $foundry, $fontname, $fontweight,
|
---|
160 | $fontslant, $fontwidth, $fontspacing, "*", "*");
|
---|
161 |
|
---|
162 | }
|
---|
163 |
|
---|
164 | my $y_offset = ($height-$textheight)-int($fontsize/5);
|
---|
165 |
|
---|
166 | my $descenders = "";
|
---|
167 |
|
---|
168 | # russian descenders (KOI8-R)
|
---|
169 | # $descenders .= chr(0xD2);
|
---|
170 | # $descenders .= chr(0xD5);
|
---|
171 |
|
---|
172 | if ($text =~ /[gjpqyJ$descenders]/) { ## capital J is a descender in lucida font
|
---|
173 | # descenders - put text at bottom of image, otherwise
|
---|
174 | # go for fontsize/5 pixels above bottom. This is kind of hacky
|
---|
175 | # and may need some playing with for different fonts/fontsizes
|
---|
176 | $y_offset = $height-$textheight;
|
---|
177 | }
|
---|
178 |
|
---|
179 | if ($alignment =~ /^l/i) {
|
---|
180 | # align text to the left
|
---|
181 | my $x_offset = $width_space;
|
---|
182 | gimp_layer_set_offsets ($textlayer, $x_offset, $y_offset);
|
---|
183 | } elsif ($alignment =~ /^r/i) {
|
---|
184 | # right alignment
|
---|
185 | gimp_layer_set_offsets ($textlayer, ($width-($textwidth+$width_space)), $y_offset);
|
---|
186 | } else {
|
---|
187 | # center alignment (the default)
|
---|
188 | gimp_layer_set_offsets ($textlayer, ($width-$textwidth)/2, $y_offset);
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | # flatten the image
|
---|
193 | my $finishedlayer = gimp_image_flatten ($image);
|
---|
194 |
|
---|
195 | # make indexed colour
|
---|
196 | if ($filename =~ /\.gif$/i) {
|
---|
197 | # make indexed colour (may need to do this for
|
---|
198 | # other formats as well as gif)
|
---|
199 | gimp_convert_indexed ($image, 0, MAKE_PALETTE, 256, 0, 0, "");
|
---|
200 | }
|
---|
201 |
|
---|
202 | # save image
|
---|
203 | $filename = &util::filename_cat ($image_dir, $filename);
|
---|
204 | gimp_file_save (RUN_NONINTERACTIVE, $image, $finishedlayer, $filename, $filename);
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | sub create_image {
|
---|
209 | # create the image
|
---|
210 | my $image = gimp_image_new ($width, $height, RGB_IMAGE);
|
---|
211 |
|
---|
212 | # background layer
|
---|
213 | my $backlayer = gimp_layer_new ($image, $width, $height, RGB_IMAGE,
|
---|
214 | "BGLayer", 100, NORMAL_MODE);
|
---|
215 |
|
---|
216 | # add the background layer
|
---|
217 | gimp_image_add_layer ($image, $backlayer, 0);
|
---|
218 |
|
---|
219 | # set colour of background
|
---|
220 | gimp_palette_set_foreground ($bgcolor);
|
---|
221 | gimp_palette_set_background ($fgcolor);
|
---|
222 |
|
---|
223 | # clear the background
|
---|
224 | gimp_selection_all ($image);
|
---|
225 | gimp_edit_clear ($backlayer);
|
---|
226 | gimp_selection_none ($image);
|
---|
227 |
|
---|
228 | # create the gradient background
|
---|
229 | gimp_blend ($backlayer, 0, NORMAL_MODE, LINEAR, 70, 0, REPEAT_NONE, 0, 0, 0, 5, $height-3, 5, 0);
|
---|
230 |
|
---|
231 | # set colour of text
|
---|
232 | gimp_palette_set_foreground ($fontcolor);
|
---|
233 |
|
---|
234 | return ($image, $backlayer);
|
---|
235 | }
|
---|
236 |
|
---|
237 | # returns 1 if this is the last entry,
|
---|
238 | sub read_config_entry {
|
---|
239 | my ($handle) = @_;
|
---|
240 |
|
---|
241 | my $line = "";
|
---|
242 | while (defined ($line = <$handle>)) {
|
---|
243 | next unless $line =~ /\w/;
|
---|
244 | my @line = ();
|
---|
245 | if ($line =~ /^\-+/) {return 0;}
|
---|
246 | $line =~ s/^\#.*$//; # remove comments
|
---|
247 | $line =~ s/\cM|\cJ//g; # remove end-of-line characters
|
---|
248 | $line =~ s/^\s+//; # remove initial white space
|
---|
249 | while ($line =~ s/\s*(\"[^\"]*\"|\'[^\']*\'|\S+)\s*//) {
|
---|
250 | if (defined $1) {
|
---|
251 | # remove any enclosing quotes
|
---|
252 | my $entry = $1;
|
---|
253 | $entry =~ s/^([\"\'])(.*)\1$/$2/;
|
---|
254 |
|
---|
255 | # substitute any environment variables
|
---|
256 | $entry =~ s/\$(\w+)/$ENV{$1}/g;
|
---|
257 | $entry =~ s/\$\{(\w+)\}/$ENV{$1}/g;
|
---|
258 |
|
---|
259 | push (@line, $entry);
|
---|
260 | } else {
|
---|
261 | push (@line, "");
|
---|
262 | }
|
---|
263 | }
|
---|
264 | if (scalar (@line) == 2 && defined ${$line[0]}) {
|
---|
265 | ${$line[0]} = $line[1];
|
---|
266 | }
|
---|
267 | }
|
---|
268 | return 1;
|
---|
269 | }
|
---|
270 |
|
---|
271 |
|
---|
272 | register("gsdl_green_bar",
|
---|
273 | "Create green bar icons for gsdl (gimp version 1.2)",
|
---|
274 | "",
|
---|
275 | "Stefan Boddie (1.0) and Gordon Paynter (1.2)",
|
---|
276 | "Copyright 2000 Stefan Boddie and The New Zealand Digital Library Project",
|
---|
277 | "2000-07-05",
|
---|
278 | "<Toolbox>/Xtns/gsdl_green_bar",
|
---|
279 | "*",
|
---|
280 | [
|
---|
281 | [PF_STRING, "text", "Text appearing on bar", "greenstone"],
|
---|
282 |
|
---|
283 | [PF_STRING, "filename", "Output file", "green_bar.png"],
|
---|
284 | [PF_STRING, "image_dir", "Directory to create images in", $current_dir],
|
---|
285 |
|
---|
286 | [PF_INT, "width", "Width of the bar", 640],
|
---|
287 | [PF_INT, "height", "Height of the bar", 17],
|
---|
288 | [PF_STRING, "fgcolor", "Foreground colour (top) of bar", $white],
|
---|
289 | [PF_STRING, "bgcolor", "Background colour (bottom) of bar", $gsdl_green],
|
---|
290 | [PF_STRING, "fontcolor", "Colour of text on bar", $black],
|
---|
291 | [PF_INT, "fontsize", "Font size", 17],
|
---|
292 |
|
---|
293 | [PF_STRING, "alignment", "Alignment of text on the bar (left, center or right)", "left"],
|
---|
294 | [PF_INT, "padding_space", "Space around the text", 5],
|
---|
295 |
|
---|
296 | [PF_STRING, "foundry", "Font foundry", "*"],
|
---|
297 | [PF_STRING, "fontname", "Font name", "lucida"],
|
---|
298 | [PF_STRING, "fontweight", "Font weight", "medium"],
|
---|
299 | [PF_STRING, "fontslant", "Font slant", "r"],
|
---|
300 | [PF_STRING, "fontwidth", "Font width", "*"],
|
---|
301 | [PF_STRING, "fontspacing", "Font spacing", "*"],
|
---|
302 |
|
---|
303 | [PF_STRING, "cfg_file", "Configuration file", ""]
|
---|
304 |
|
---|
305 | ],
|
---|
306 | \&gsdl_green_bar);
|
---|
307 |
|
---|
308 |
|
---|
309 | exit main;
|
---|