1 | #!/usr/bin/perl -w
|
---|
2 |
|
---|
3 | ###########################################################################
|
---|
4 | #
|
---|
5 | # title_icon.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-2001 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 | # title_icon.pl
|
---|
30 | #
|
---|
31 | # This script generates collection title icons for greenstone.
|
---|
32 | #
|
---|
33 | # This version has been rewritten for the GIMP Version 1.2. You
|
---|
34 | # should be able to run it if you install gimp 1.2 with perl support.
|
---|
35 | # In Debian this means running "apt-get install gimp1.2 gimp1.2perl"
|
---|
36 |
|
---|
37 | BEGIN {
|
---|
38 | die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
|
---|
39 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
|
---|
40 | }
|
---|
41 |
|
---|
42 | use Gimp ":auto";
|
---|
43 | use Gimp::Fu;
|
---|
44 |
|
---|
45 | use util;
|
---|
46 | use unicode;
|
---|
47 |
|
---|
48 | # set trace level to watch functions as they are executed
|
---|
49 | #Gimp::set_trace(TRACE_ALL);
|
---|
50 | #Gimp::set_trace(TRACE_CALL);
|
---|
51 |
|
---|
52 | my $gsdl_green = "#96c19b";
|
---|
53 | my $black = "#000000";
|
---|
54 | my $white = "#FFFFFF";
|
---|
55 |
|
---|
56 | my ($current_dir) = `pwd`;
|
---|
57 | chomp($current_dir);
|
---|
58 |
|
---|
59 |
|
---|
60 | my ($text, $filename, $image_dir, $size, $width, $height,
|
---|
61 | $stripecolor, $stripewidth, $stripe_alignment, $textspace_x,
|
---|
62 | $textspace_y, $dont_wrap, $imagefile, $imageheight, $i_transparency,
|
---|
63 | $bgcolor, $fontcolor, $fontsize, $minfontsize, $text_alignment,
|
---|
64 | $foundry, $fontname, $fontweight, $fontslant, $fontwidth,
|
---|
65 | $fontspacing, $cfg_file);
|
---|
66 |
|
---|
67 | sub reset_options {
|
---|
68 | $image_dir = "./";
|
---|
69 | $imagefile = "";
|
---|
70 | $width = int (150 * $size);
|
---|
71 | $height = int (44 * $size);
|
---|
72 | $imageheight = int (110 * $size);
|
---|
73 | $stripecolor = $gsdl_green;
|
---|
74 | $stripewidth = int (40 * $size);
|
---|
75 | $stripe_alignment = "left";
|
---|
76 | $i_transparency = 60;
|
---|
77 | $text = "";
|
---|
78 | $text_alignment = "left";
|
---|
79 | $filename = "";
|
---|
80 | $textspace_x = int (3 * $size);
|
---|
81 | $textspace_y = int (3 * $size);
|
---|
82 | $bgcolor = $gsdl_green;
|
---|
83 | $fontcolor = $black;
|
---|
84 | $fontsize = int (17 * $size);
|
---|
85 | $minfontsize = int (10 * $size);
|
---|
86 | $foundry = "*";
|
---|
87 | $fontname = "lucida";
|
---|
88 | $fontweight = "medium";
|
---|
89 | $fontslant = "r";
|
---|
90 | $fontwidth = "*";
|
---|
91 | $fontspacing = "*";
|
---|
92 | }
|
---|
93 |
|
---|
94 | sub gsdl_title_icon {
|
---|
95 |
|
---|
96 | ($text, $filename, $image_dir, $size, $width, $height,
|
---|
97 | $stripecolor, $stripewidth, $stripe_alignment, $textspace_x,
|
---|
98 | $textspace_y, $dont_wrap, $imagefile, $imageheight, $i_transparency,
|
---|
99 | $bgcolor, $fontcolor, $fontsize, $minfontsize, $text_alignment,
|
---|
100 | $foundry, $fontname, $fontweight, $fontslant, $fontwidth,
|
---|
101 | $fontspacing, $cfg_file) = @_;
|
---|
102 |
|
---|
103 | # Create images using a configuration file
|
---|
104 | if ($cfg_file =~ /\w/) {
|
---|
105 |
|
---|
106 | open (CONF, $cfg_file) || die "couldn't open cfg_file $cfg_file\n";
|
---|
107 | while (1) {
|
---|
108 |
|
---|
109 | &reset_options ();
|
---|
110 |
|
---|
111 | # read image configuration entry
|
---|
112 | my $status = &read_config_entry (CONF);
|
---|
113 | if ($filename !~ /\w/) {
|
---|
114 | if ($status) {last;}
|
---|
115 | else {next;}
|
---|
116 | }
|
---|
117 | &produce_image ();
|
---|
118 | if ($status) {last;}
|
---|
119 | }
|
---|
120 | close CONF;
|
---|
121 | }
|
---|
122 |
|
---|
123 | # Create a single image from the parameters we have
|
---|
124 | else {
|
---|
125 | &produce_image ();
|
---|
126 | }
|
---|
127 |
|
---|
128 | }
|
---|
129 |
|
---|
130 | sub produce_image {
|
---|
131 |
|
---|
132 | &adjust_args ();
|
---|
133 | &wrap_text () unless $dont_wrap;
|
---|
134 |
|
---|
135 | my $use_image = 0;
|
---|
136 | if ($imagefile =~ /\w/) {
|
---|
137 | if (!-r $imagefile) {
|
---|
138 | print STDERR "WARNING (title_icon.pl): imagefile '$imagefile cannot be ";
|
---|
139 | print STDERR "read - $filename will be created without the use of an image file\n";
|
---|
140 | } else {
|
---|
141 | $use_image = 1;
|
---|
142 | $height = $imageheight;
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | # create the image
|
---|
147 | my $image = gimp_image_new ($width, $height, RGB_IMAGE);
|
---|
148 |
|
---|
149 | # create and add the background layer
|
---|
150 | my $backlayer = gimp_layer_new ($image, $width, $height, RGB_IMAGE,
|
---|
151 | "BGLayer", 100, NORMAL_MODE);
|
---|
152 | gimp_image_add_layer ($image, $backlayer, 0);
|
---|
153 |
|
---|
154 | # clear the background
|
---|
155 | gimp_selection_all ($image);
|
---|
156 | gimp_edit_clear ($backlayer);
|
---|
157 | gimp_selection_none ($image);
|
---|
158 |
|
---|
159 | # fill in stripe
|
---|
160 | if ($stripewidth > 0) {
|
---|
161 | gimp_palette_set_foreground ($stripecolor);
|
---|
162 | if ($stripe_alignment eq "left") {
|
---|
163 | gimp_rect_select ($image, 0, 0, $stripewidth, $height, 0, 0, 0);
|
---|
164 | } else {
|
---|
165 | gimp_rect_select ($image, $width-$stripewidth, 0, $stripewidth, $height, 0, 0, 0);
|
---|
166 | }
|
---|
167 | gimp_bucket_fill ($backlayer, FG_BUCKET_FILL, NORMAL_MODE, 100, 0, 1, 0, 0);
|
---|
168 | gimp_selection_none ($image);
|
---|
169 | }
|
---|
170 |
|
---|
171 | # get image file (image goes on opposite side to stripe)
|
---|
172 | if ($use_image) {
|
---|
173 | my $rimage = gimp_file_load (RUN_NONINTERACTIVE, $imagefile, $imagefile);
|
---|
174 | my $rdraw = gimp_image_active_drawable ($rimage);
|
---|
175 | gimp_scale ($rdraw, 1, 0, 0, $width-$stripewidth, $height);
|
---|
176 | gimp_edit_copy ($rdraw);
|
---|
177 |
|
---|
178 | # add the background image layer
|
---|
179 | my $imagelayer = gimp_layer_new ($image, $width, $height, RGB_IMAGE,
|
---|
180 | "ImageLayer", $i_transparency, NORMAL_MODE);
|
---|
181 | gimp_image_add_layer ($image, $imagelayer, 0);
|
---|
182 |
|
---|
183 | # clear the new layer
|
---|
184 | gimp_selection_all ($image);
|
---|
185 | gimp_edit_clear ($imagelayer);
|
---|
186 | gimp_selection_none ($image);
|
---|
187 |
|
---|
188 | my $flayer = gimp_edit_paste ($imagelayer, 1);
|
---|
189 | if ($stripe_alignment eq "left") {
|
---|
190 | gimp_layer_set_offsets($flayer, $stripewidth, 0);
|
---|
191 | gimp_layer_set_offsets($imagelayer, $stripewidth, 0);
|
---|
192 | } else {
|
---|
193 | gimp_layer_set_offsets($flayer, 0, 0);
|
---|
194 | gimp_layer_set_offsets($imagelayer, 0, 0);
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | # flatten the image (otherwise the text will be "behind" the image)
|
---|
199 | $backlayer = gimp_image_flatten ($image);
|
---|
200 |
|
---|
201 | # set colour of text
|
---|
202 | gimp_palette_set_foreground ($fontcolor);
|
---|
203 |
|
---|
204 | # set the text if there is any
|
---|
205 | my ($textlayer, $textheight, $textwidth);
|
---|
206 | my $fsize = $fontsize;
|
---|
207 | if (length($text)) {
|
---|
208 | $text =~ s/\\n/\n/gi;
|
---|
209 |
|
---|
210 | while (1) {
|
---|
211 | $textlayer = gimp_text ($image, $backlayer, 0, 0, $text, 0, 1, $fsize,
|
---|
212 | PIXELS, $foundry, $fontname, $fontweight, $fontslant,
|
---|
213 | $fontwidth, $fontspacing, "*", "*");
|
---|
214 |
|
---|
215 | # check that text fits within image
|
---|
216 | $textwidth = gimp_drawable_width($textlayer);
|
---|
217 | $textheight = gimp_drawable_height($textlayer);
|
---|
218 | if ((($textwidth + $textspace_x) > $width) ||
|
---|
219 | (($textheight + $textspace_y) > $height)) {
|
---|
220 | if ($fsize < $minfontsize) {
|
---|
221 | die "Error (title_icon.pl): text '$text' doesn't fit on ${width}x${height} image " .
|
---|
222 | "(minimum font size tried: $minfontsize\n";
|
---|
223 | } else {
|
---|
224 | gimp_selection_all ($image);
|
---|
225 | gimp_edit_clear ($image, $textlayer);
|
---|
226 | gimp_selection_none ($image);
|
---|
227 | $fsize --;
|
---|
228 | print STDERR "WARNING (title_icon.pl): '$text' doesn't fit: reducing font size to $fsize\n";
|
---|
229 | }
|
---|
230 | } else {
|
---|
231 | last;
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 | # align text
|
---|
236 | if ($text_alignment eq "left") {
|
---|
237 | gimp_layer_set_offsets ($textlayer, $textspace_x, $textspace_y);
|
---|
238 | } else {
|
---|
239 | gimp_layer_set_offsets ($textlayer, ($width-$textwidth)-$textspace_x, $textspace_y);
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | # flatten the image
|
---|
244 | my $finishedlayer = gimp_image_flatten ($image);
|
---|
245 |
|
---|
246 | if ($filename =~ /\.gif$/i) {
|
---|
247 | # make indexed colour (may need to do this for
|
---|
248 | # other formats as well as gif)
|
---|
249 | gimp_convert_indexed ($image, 0, 256);
|
---|
250 | }
|
---|
251 |
|
---|
252 | # save image
|
---|
253 | my $filename = &util::filename_cat ($image_dir, $filename);
|
---|
254 | gimp_file_save (RUN_NONINTERACTIVE, $image, $finishedlayer, $filename, $filename);
|
---|
255 | }
|
---|
256 |
|
---|
257 | # returns 1 if this is the last entry,
|
---|
258 | sub read_config_entry {
|
---|
259 | my ($handle) = @_;
|
---|
260 |
|
---|
261 | my $line = "";
|
---|
262 | while (defined ($line = <$handle>)) {
|
---|
263 | next unless $line =~ /\w/;
|
---|
264 | my @line = ();
|
---|
265 | if ($line =~ /^\-+/) {return 0;}
|
---|
266 | $line =~ s/^\#.*$//; # remove comments
|
---|
267 | $line =~ s/\cM|\cJ//g; # remove end-of-line characters
|
---|
268 | $line =~ s/^\s+//; # remove initial white space
|
---|
269 | while ($line =~ s/\s*(\"[^\"]*\"|\'[^\']*\'|\S+)\s*//) {
|
---|
270 | if (defined $1) {
|
---|
271 | # remove any enclosing quotes
|
---|
272 | my $entry = $1;
|
---|
273 | $entry =~ s/^([\"\'])(.*)\1$/$2/;
|
---|
274 |
|
---|
275 | # substitute any environment variables
|
---|
276 | $entry =~ s/\$(\w+)/$ENV{$1}/g;
|
---|
277 | $entry =~ s/\$\{(\w+)\}/$ENV{$1}/g;
|
---|
278 |
|
---|
279 | push (@line, $entry);
|
---|
280 | } else {
|
---|
281 | push (@line, "");
|
---|
282 | }
|
---|
283 | }
|
---|
284 | if (scalar (@line) == 2 && defined ${$line[0]}) {
|
---|
285 | ${$line[0]} = $line[1];
|
---|
286 | }
|
---|
287 | }
|
---|
288 | return 1;
|
---|
289 | }
|
---|
290 |
|
---|
291 | # adjust arguments that are effected by the size argument
|
---|
292 | sub adjust_args {
|
---|
293 |
|
---|
294 | if ($size != 1) {
|
---|
295 | $width *= $size;
|
---|
296 | $height *= $size;
|
---|
297 | $imageheight *= $size;
|
---|
298 | $stripewidth *= $size;
|
---|
299 | $textspace_x *= $size;
|
---|
300 | $textspace_y *= $size;
|
---|
301 | $fontsize *= $size;
|
---|
302 | $minfontsize *= $size;
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | sub wrap_text {
|
---|
307 |
|
---|
308 | # don't wrap text if it already contains carriage returns
|
---|
309 | return if $text =~ /\n/;
|
---|
310 |
|
---|
311 | # the following assumes that all words are less than $wrap_length long
|
---|
312 | my $wrap_length = 14;
|
---|
313 |
|
---|
314 | my $new_text = "";
|
---|
315 | while (length ($text) >= $wrap_length) {
|
---|
316 | my $line = substr ($text, 0, $wrap_length);
|
---|
317 | $text =~ s/^$line//;
|
---|
318 | $line =~ s/\s([^\s]*)$/\n/;
|
---|
319 | $text = $1 . $text;
|
---|
320 | $new_text .= $line;
|
---|
321 | }
|
---|
322 | $new_text .= $text;
|
---|
323 | $text = $new_text;
|
---|
324 | }
|
---|
325 |
|
---|
326 |
|
---|
327 | register("gsdl_title_icon",
|
---|
328 | "Create title icons for gsdl (gimp version 1.2)",
|
---|
329 | "",
|
---|
330 | "Stefan Boddie (1.0) and Gordon Paynter (1.2)",
|
---|
331 | "Copyright 1999-2001 The New Zealand Digital Library Project",
|
---|
332 | "2000-03-10",
|
---|
333 | "<Toolbox>/Xtns/gsdl_title_icon",
|
---|
334 | "*",
|
---|
335 | [
|
---|
336 | [PF_STRING, "text", "Text appearing on icon", "greenstone collection"],
|
---|
337 |
|
---|
338 | [PF_STRING, "filename", "Output file", "title_icon.png"],
|
---|
339 | [PF_STRING, "image_dir", "Directory to create images in", $current_dir],
|
---|
340 |
|
---|
341 | [PF_INT, "size_ratio", "Size factor: a factor of 2 doulbles image size", 1],
|
---|
342 | [PF_INT, "width", "Width of the icon", 150],
|
---|
343 | [PF_INT, "height", "Height of the icon", 44],
|
---|
344 |
|
---|
345 | [PF_STRING, "stripecolor", "Colour of the stripe", $gsdl_green],
|
---|
346 | [PF_INT, "stripewidth", "Width of the stripe", 40],
|
---|
347 | [PF_STRING, "stripealign", "Alignment of stripe (left or right)", "left"],
|
---|
348 |
|
---|
349 | [PF_INT, "text_x_offset", "Distance from left of image to text", 3],
|
---|
350 | [PF_INT, "text_y_offset", "Distance from top of image to text", 3],
|
---|
351 | [PF_STRING, "dont_wrap", "don't attempt to wrap text", ""],
|
---|
352 |
|
---|
353 | [PF_STRING, "imagefile", "Filename of background image", ""],
|
---|
354 | [PF_INT, "imageheight", "Height of background image", 110],
|
---|
355 | [PF_INT, "imagetransp", "Transparency of background image (1-100)", 60],
|
---|
356 |
|
---|
357 | # [PF_STRING, "fgcolor", "Foreground colour (top) of bar", $white],
|
---|
358 | [PF_STRING, "bgcolor", "Background colour (bottom) of bar", $gsdl_green],
|
---|
359 | [PF_STRING, "fontcolor", "Colour of text on bar", $black],
|
---|
360 | [PF_INT, "fontsize", "Font size", 17],
|
---|
361 | [PF_INT, "minfontsize", "Minimum font size if scaling required", 10],
|
---|
362 |
|
---|
363 | [PF_STRING, "alignment", "Alignment of text on the icon (left or right)", "left"],
|
---|
364 |
|
---|
365 | [PF_STRING, "foundry", "Font foundry", "*"],
|
---|
366 | [PF_STRING, "fontname", "Font name", "lucida"],
|
---|
367 | [PF_STRING, "fontweight", "Font weight", "medium"],
|
---|
368 | [PF_STRING, "fontslant", "Font slant", "r"],
|
---|
369 | [PF_STRING, "fontwidth", "Font width", "*"],
|
---|
370 | [PF_STRING, "fontspacing", "Font spacing", "*"],
|
---|
371 |
|
---|
372 | [PF_STRING, "cfg_file", "Configuration file", ""]
|
---|
373 |
|
---|
374 | ],
|
---|
375 | \&gsdl_title_icon);
|
---|
376 |
|
---|
377 |
|
---|
378 | exit main;
|
---|
379 |
|
---|
380 |
|
---|