source: trunk/gsdl/bin/script/gimp/green_bar-1.2.pl@ 4211

Last change on this file since 4211 was 4211, checked in by mdewsnip, 21 years ago

Improved support for KOI8-R (Russian, some Kazakh) descenders.

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