source: trunk/gsdl/bin/script/translate.pl@ 4227

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

First stab at generating collector_bar_buttons. Assumes that the text to go onto the buttons is formatted nicely (in terms of newlines and spaces to center text).

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 13.5 KB
Line 
1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# translate.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# translate.pl takes a translated macro file (filename passed in on command
29# line) and generates any images required by it. Check out english.dm for
30# an example of the format translate.pl expects
31
32# translate.pl uses gimp to generate images so needs gimp installed and set
33# up for scripting with perl
34
35BEGIN {
36 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
37 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
38}
39
40use Gimp qw/:auto :DEFAULT/;
41use parsargv;
42use util;
43use unicode;
44
45# these html entities will be translated correctly when occurring in
46# images. Any entities not in this list will not.
47my %rmap = ('auml' => chr (228),
48 'euml' => chr (235),
49 'iuml' => chr (239),
50 'ouml' => chr (246),
51 'uuml' => chr (252),
52 'Auml' => chr (196),
53 'Euml' => chr (203),
54 'Iuml' => chr (207),
55 'Ouml' => chr (214),
56 'Uuml' => chr (220),
57 'szlig' => chr (223),
58 'aacute' => chr (225),
59 'eacute' => chr (233),
60 'iacute' => chr (237),
61 'oacute' => chr (243),
62 'uacute' => chr (250),
63 'Aacute' => chr (193),
64 'Eacute' => chr (201),
65 'Iacute' => chr (205),
66 'Oacute' => chr (211),
67 'Uacute' => chr (218),
68 'agrave' => chr (224),
69 'egrave' => chr (232),
70 'igrave' => chr (236),
71 'ograve' => chr (242),
72 'ugrave' => chr (249),
73 'Agrave' => chr (192),
74 'Egrave' => chr (200),
75 'Igrave' => chr (204),
76 'Ograve' => chr (210),
77 'Ugrave' => chr (217),
78 'ntilde' => chr (241),
79 'Ntilde' => chr (209),
80 'atilde' => chr (227),
81 'Atilde' => chr (195),
82 'otilde' => chr (245),
83 'Otilde' => chr (213),
84 'ccedil' => chr (231),
85 'Ccedil' => chr (199),
86 'ecirc' => chr (234),
87 'Ecirc' => chr (202),
88 'acirc' => chr (226),
89 'Acirc' => chr (194),
90 );
91
92my $hand_made = 0;
93
94sub print_usage {
95 print STDERR "\n";
96 print STDERR "translate.pl: Uses gimp to generate any images required by a\n";
97 print STDERR " Greenstone macro file.\n\n";
98 print STDERR " usage: $0 [options] macrofile\n\n";
99 print STDERR " options:\n";
100 print STDERR " -save_orig_file edited macrofile will be written to\n";
101 print STDERR " macrofile.new leaving macrofile unchanged\n";
102 print STDERR " -language_symbol ISO abbreviation of language (e.g. German=de,\n";
103 print STDERR " French=fr, Maori=mi)\n";
104 print STDERR " -image_dir directory full path to directory in which to create images\n";
105 print STDERR " (defaults to `pwd`/images)\n\n";
106}
107
108sub gsdl_translate {
109
110 if (!parsargv::parse(\@ARGV,
111 'save_orig_file', \$save_orig_file,
112 'language_symbol/[A-Za-z]{2}', \$language_symbol,
113 'image_dir/.*/images', \$image_dir)) {
114 &print_usage();
115 die "\n";
116 }
117
118 if ($image_dir eq "images") {
119 $image_dir = `pwd`;
120 chomp $image_dir;
121 $image_dir = &util::filename_cat ($image_dir, "images");
122 }
123
124 if (!defined $ARGV[0]) {
125 print STDERR "no macro file supplied\n\n";
126 &print_usage();
127 die "\n";
128 }
129 my $macrofile = $ARGV[0];
130 die "\nmacrofile $macrofile does not exist\n\n" unless -e $macrofile;
131
132 if (!-e $image_dir) {
133 mkdir ($image_dir, 511) || die "\ncouldn't create image_dir $image_dir\n\n";
134 }
135
136 open (INPUT, $macrofile) || die "\ncouldn't open $macrofile for reading\n\n";
137 open (OUTPUT, ">$macrofile.new") || die "\ncouldn't open temporary file $macrofile.new for writing\n\n";
138
139 &parse_file (INPUT, OUTPUT);
140
141 close OUTPUT;
142 close INPUT;
143
144 if (!$save_orig_file) {
145 `mv $macrofile.new $macrofile`;
146 }
147
148 print STDERR "\n\n";
149 print STDERR "translation of macro file $macrofile completed\n";
150 print STDERR "the translated macro file is $macrofile.new\n" if $save_orig_file;
151 print STDERR "\n";
152 if ($hand_made) {
153 print STDERR "$hand_made hand made images were found within $macrofile,\n";
154 print STDERR "these will need to be made by hand (grep $macrofile for 'hand_made'\n\n";
155 }
156 print STDERR "To add your new interface translation to Greenstone you'll need to:\n";
157 print STDERR " 1. Copy your new macro file to your GSDLHOME/macros directory\n";
158 print STDERR " 2. Add your new macro file to the macrofiles list in your\n";
159 print STDERR " GSDLHOME/etc/main.cfg configuration file\n";
160 print STDERR " 3. Copy your newly created images from $image_dir to \n";
161 print STDERR " GSDLHOME/images/$language_symbol/\n\n";
162 print STDERR "Access your new interface language by setting the language (l) cgi\n";
163 print STDERR "argument to '$language_symbol'\n\n";
164
165}
166
167sub parse_file {
168 my ($input, $output) = @_;
169
170 undef $/;
171 my $dmfile = <$input>;
172 $/ = "\n";
173
174 # process all the images
175
176 $dmfile =~ s/(?:^|\n)\#\#\s*\"([^\"]*)\"\s*\#\#\s*([^\s\#]*)\s*\#\#\s*([^\s\#]*)\s*\#\#(.*?)(?=(\n\#|\s*\Z))/&process_image ($1, $2, $3, $4)/esg;
177
178 # add language parameter to each macro
179 $dmfile =~ s/(\n\s*)(_[^_]*_)\s*(\[[^\]]*\])?\s*\{/$1 . &add_language_param ($2, $3)/esg;
180
181 print $output $dmfile;
182}
183
184sub process_image {
185 my ($text, $image_type, $image_name, $image_macros) = @_;
186
187 my $origtext = $text;
188 $text =~ s/&(\d{3,4});/chr($1)/ge;
189 $text =~ s/&([^;]*);/$rmap{$1}/g;
190
191 # special case for russian images - fonts expect text to be koi8-r encoded
192 if ($language_symbol eq "ru") {
193 $text = &unicode::unicode2koi8r(&unicode::utf82unicode($text));
194 }
195
196 # edit image macros
197 $image_macros =~ s/(_httpimg_\/)(?:[^\/\}]*\/)?([^\}]*\.(?:gif|jpe?g|png))/$1$language_symbol\/$2/gs;
198
199 if ($image_type eq "top_nav_button") {
200
201 # generate images
202 my $options = "-text \"$text\" -filenamestem $image_name";
203 # special case for russian images
204 if ($language_symbol eq "ru") {
205 $options .= " -fontsize 10 -height 20 -whitespace -image_dir $image_dir";
206 $options .= " -foundry cronyx -fontname helvetica";
207 } else {
208 $options .= " -fontsize 12 -height 20 -whitespace -image_dir $image_dir";
209 }
210 `$ENV{'GSDLHOME'}/bin/script/gimp/flash_button-1.2.pl $options`;
211
212 # get width of new images and edit width macro
213 # my $fullfilename = &util::filename_cat ($image_dir, "${image_name}on.gif");
214 # &process_width_macro ($fullfilename, $image_name, \$image_macros);
215
216 } elsif ($image_type eq "nav_bar_button") {
217
218 # generate on and off images
219 my $options = "-text \"$text\" -filenamestem $image_name";
220 $options .= " -fontsize 17 -height 17 -fixed_width -width 87";
221 $options .= " -image_dir $image_dir";
222 # special case for russian images
223 if ($language_symbol eq "ru") {
224 $options .= " -foundry cronyx -fontname helvetica";
225 }
226 `$ENV{'GSDLHOME'}/bin/script/gimp/flash_button-1.2.pl $options`;
227
228 # generate green image
229 $options = "-text \"$text\" -filename ${image_name}gr.gif";
230 $options .= " -image_dir $image_dir";
231 # special case for russian images
232 if ($language_symbol eq "ru") {
233 $options .= " -foundry cronyx -fontname helvetica";
234 }
235 `$ENV{'GSDLHOME'}/bin/script/gimp/green_bar-1.2.pl $options`;
236
237 # get width of new images and edit width macro
238 my $fullfilename = &util::filename_cat ($image_dir, "${image_name}on.gif");
239 &process_width_macro ($fullfilename, $image_name, \$image_macros);
240
241 } elsif ($image_type eq "collector_bar_button") {
242
243 # unescape any newline characters in the text
244 $text =~ s/\\n/\n/g;
245
246 # generate on and off images (yellow)
247 my $options = "-text \"$text\" -filenamestem yc$image_name -image_dir $image_dir";
248 $options .= " -width 77 -height 26 -fixed_width -whitespace -fontsize 13";
249 # special case for russian images
250 if ($language_symbol eq "ru") {
251 $options .= " -foundry cronyx -fontname helvetica";
252 }
253 `$ENV{'GSDLHOME'}/bin/script/gimp/flash_button-1.2.pl $options`;
254
255 # generate on and off images (green)
256 $options = "-text \"$text\" -filenamestem gc$image_name -image_dir $image_dir";
257 $options .= " -width 77 -height 26 -fixed_width -whitespace -fontsize 13";
258 $options .= " -bgcolor \"#96c19b\"";
259 # special case for russian images
260 if ($language_symbol eq "ru") {
261 $options .= " -foundry cronyx -fontname helvetica";
262 }
263 `$ENV{'GSDLHOME'}/bin/script/gimp/flash_button-1.2.pl $options`;
264
265 # generate on and off images (grey) - only the light image (off) is used
266 $options = "-text \"$text\" -filenamestem nc$image_name -image_dir $image_dir";
267 $options .= " -width 77 -height 26 -fixed_width -whitespace -fontsize 13";
268 $options .= " -bgcolor \"#7E7E7E\" -fontcolor \"#a0a0a0\"";
269 # special case for russian images
270 if ($language_symbol eq "ru") {
271 $options .= " -foundry cronyx -fontname helvetica";
272 }
273 `$ENV{'GSDLHOME'}/bin/script/gimp/flash_button-1.2.pl $options`;
274
275 # delete the unused dark image
276 if (-e util::filename_cat($image_dir, "nc$image_name" . "on.gif")) {
277 unlink(util::filename_cat($image_dir, "nc$image_name" . "on.gif"));
278 }
279
280 } elsif ($image_type eq "document_button") {
281
282 # generate on and off images
283 my $options = "-text \"$text\" -filenamestem $image_name";
284 $options .= " -fixed_width -whitespace -image_dir $image_dir";
285 if ($language_symbol eq "ru") {
286 $options .= " -fontsize 8 -foundry cronyx -fontname helvetica";
287 }
288 `$ENV{'GSDLHOME'}/bin/script/gimp/flash_button-1.2.pl $options`;
289
290 # get width of new images and edit width macro
291 # my $fullfilename = &util::filename_cat ($image_dir, "${image_name}on.gif");
292 # &process_width_macro ($fullfilename, $image_name, \$image_macros);
293
294 } elsif ($image_type eq "green_bar_left_aligned") {
295
296 # generate green bar image (we're assuming these bars are always 537
297 # pixels and are never stretched by excess text
298 my $options = "-text \"$text\" -filename ${image_name}.gif -dont_center";
299 $options .= " -width 537 -width_space 15 -image_dir $image_dir";
300 if ($language_symbol eq "ru") {
301 $options .= " -foundry cronyx -fontname helvetica";
302 }
303 `$ENV{'GSDLHOME'}/bin/script/gimp/green_bar-1.2.pl $options`;
304
305 } elsif ($image_type eq "green_title") {
306
307 # read the width if it is specified in $image_macros
308 my ($width) = $image_macros =~ /_width${image_name}x?_\s*[^\{]*\{(\d+)\}/;
309 $width = 200 unless ($width);
310
311 # generate green title image
312 my $options = "-text \"$text\" -filename ${image_name}.gif -image_dir $image_dir";
313 $options .= " -width $width -height 57 -stripe_alignment right -text_alignment right";
314 $options .= " -fontsize 26 -fontweight bold";
315 # special case for russian images
316 if ($language_symbol eq "ru") {
317 $options .= " -foundry cronyx -fontname helvetica";
318 }
319 `$ENV{'GSDLHOME'}/bin/script/gimp/title_icon-1.2.pl $options`;
320
321 # get width of resulting image and edit _width..._ macro in $image_macros
322 # (no longer needed since we always resize to the width read from $image_macros.)
323 # my $fullfilename = &util::filename_cat ($image_dir, "${image_name}.gif");
324 # &process_width_macro ($fullfilename, $image_name, \$image_macros);
325
326 } elsif ($image_type eq "hand_made") {
327
328 $hand_made ++;
329
330 } else {
331
332 print STDERR "WARNING (translate.pl): unknown image type found ($image_type)\n";
333
334 }
335
336 return "\n\#\# \"$origtext\" \#\# $image_type \#\# $image_name \#\#$image_macros";
337}
338
339sub process_width_macro {
340 my ($filename, $image_name, $image_macros) = @_;
341
342 my $img_info = &get_img_info ($filename);
343 $$image_macros =~ s/(_width${image_name}x?_\s*(?:\[[^\]]*\])?\s*\{)(\d+)(\})/$1$img_info->{'width'}$3/s;
344}
345
346sub add_language_param {
347 my ($macroname, $paramlist) = @_;
348
349 my $first = 1;
350 if (defined $paramlist) {
351 $paramlist =~ s/^\[//;
352 $paramlist =~ s/\]$//;
353 my @params = split /\,/, $paramlist;
354 $paramlist = "";
355 foreach $param (@params) {
356 # remove any existing language parameter
357 if ($param !~ /^l=/) {
358 $paramlist .= "," unless $first;
359 $paramlist .= $param;
360 $first = 0;
361 }
362 }
363 }
364 $paramlist .= "," unless $first;
365 $paramlist .= "l=" . $language_symbol;
366 return "$macroname [$paramlist] {";
367}
368
369sub get_img_info {
370 my ($imagefile) = @_;
371 my %info = ();
372
373 if (!-r $imagefile) {
374 print STDERR "ERROR (translate.pl): couldn't open $imagefile to get dimensions\n";
375 $info{'width'} = 0;
376 $info{'height'} = 0;
377 } else {
378 my $image = gimp_file_load (RUN_NONINTERACTIVE, $imagefile, $imagefile);
379 $info{'width'} = gimp_image_width ($image);
380 $info{'height'} = gimp_image_height ($image);
381 }
382
383 return \%info;
384}
385
386sub query {
387
388 gimp_install_procedure("gsdl_translate", "translate macro files and create images",
389 "", "Stefan Boddie", "Stefan Boddie", "2000-03-14",
390 "<Toolbox>/Xtns/gsdl_translate", "*", &PROC_EXTENSION,
391 [[PARAM_INT32, "run_mode", "Interactive, [non-interactive]"]], []);
392}
393
394Gimp::on_net { gsdl_translate; };
395exit main;
Note: See TracBrowser for help on using the repository browser.