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

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

Added special case code for generating Kazakh images, which is pretty messy. Kazakh is a superset of Russian (with 9 new letters), so none of the KOI8 encodings is suitable. Therefore, the Kazakh strings are mapped to an 8-bit encoding that matches a Kazakh font available at http://www.unesco.kz/ci/projects/greenstone/kazakh_fonts/helv_k.ttf and helv_k1.ttf (bold).

Since Russian is a subset of Kazakh, the same font and encoding can be used to produce the Russian images. However it was decided (for now) to leave the Russian images generated using the KOI8-R encoding and font, since this is a standard.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 16.1 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::unicode2singlebyte(&unicode::utf82unicode($text), "koi8_r");
194 }
195 # special case for Kazakh images (can also handle Russian) - encode to match Kazakh font
196 elsif ($language_symbol eq "kz") { # || $language_symbol eq "ru") {
197 $text = &unicode::unicode2singlebyte(&unicode::utf82unicode($text), "kazakh");
198 }
199
200 # edit image macros
201 $image_macros =~ s/(_httpimg_\/)(?:[^\/\}]*\/)?([^\}]*\.(?:gif|jpe?g|png))/$1$language_symbol\/$2/gs;
202
203 if ($image_type eq "top_nav_button") {
204
205 # generate images
206 my $options = "-text \"$text\" -filenamestem $image_name";
207 # special case for russian images
208 if ($language_symbol eq "ru") {
209 $options .= " -fontsize 10 -height 20 -whitespace -image_dir $image_dir";
210 $options .= " -foundry cronyx -fontname helvetica -fontweight bold";
211 }
212 # special case for Kazakh images (can also handle Russian)
213 elsif ($language_symbol eq "kz") { # || $language_symbol eq "ru") {
214 $options .= " -fontsize 10 -height 20 -whitespace -image_dir $image_dir";
215 $options .= " -foundry 2rebels -fontname \"helv kaz\" -fontweight bold";
216 }
217 else {
218 $options .= " -fontsize 12 -height 20 -whitespace -image_dir $image_dir";
219 }
220 `$ENV{'GSDLHOME'}/bin/script/gimp/flash_button-1.2.pl $options`;
221
222 # get width of new images and edit width macro
223 # my $fullfilename = &util::filename_cat ($image_dir, "${image_name}on.gif");
224 # &process_width_macro ($fullfilename, $image_name, \$image_macros);
225
226 } elsif ($image_type eq "nav_bar_button") {
227
228 # generate on and off images
229 my $options = "-text \"$text\" -filenamestem $image_name";
230 $options .= " -fontsize 17 -height 17 -fixed_width -width 87";
231 $options .= " -image_dir $image_dir";
232 # special case for russian images
233 if ($language_symbol eq "ru") {
234 $options .= " -foundry cronyx -fontname helvetica";
235 }
236 # special case for Kazakh images (can also handle Russian)
237 elsif ($language_symbol eq "kz") { # || $language_symbol eq "ru") {
238 $options .= " -foundry 2rebels -fontname \"helv kaz\"";
239 }
240 `$ENV{'GSDLHOME'}/bin/script/gimp/flash_button-1.2.pl $options`;
241
242 # generate green image
243 $options = "-text \"$text\" -filename ${image_name}gr.gif";
244 $options .= " -image_dir $image_dir";
245 # special case for russian images
246 if ($language_symbol eq "ru") {
247 $options .= " -foundry cronyx -fontname helvetica";
248 }
249 # special case for Kazakh images (can also handle Russian)
250 elsif ($language_symbol eq "kz") { # || $language_symbol eq "ru") {
251 $options .= " -foundry 2rebels -fontname \"helv kaz\"";
252 }
253 `$ENV{'GSDLHOME'}/bin/script/gimp/green_bar-1.2.pl $options`;
254
255 # get width of new images and edit width macro
256 my $fullfilename = &util::filename_cat ($image_dir, "${image_name}on.gif");
257 &process_width_macro ($fullfilename, $image_name, \$image_macros);
258
259 } elsif ($image_type eq "document_button") {
260
261 # generate on and off images
262 my $options = "-text \"$text\" -filenamestem $image_name";
263 $options .= " -fixed_width -whitespace -image_dir $image_dir";
264 # special case for russian images
265 if ($language_symbol eq "ru") {
266 $options .= " -fontsize 8 -foundry cronyx -fontname helvetica";
267 }
268 # special case for Kazakh images (can also handle Russian)
269 elsif ($language_symbol eq "kz") { # || $language_symbol eq "ru") {
270 $options .= " -fontsize 8 -foundry 2rebels -fontname \"helv kaz\"";
271 }
272 `$ENV{'GSDLHOME'}/bin/script/gimp/flash_button-1.2.pl $options`;
273
274 # get width of new images and edit width macro
275 # my $fullfilename = &util::filename_cat ($image_dir, "${image_name}on.gif");
276 # &process_width_macro ($fullfilename, $image_name, \$image_macros);
277
278 } elsif ($image_type eq "collector_bar_button") {
279
280 $text =~ s/\\n/\n/g;
281 if ($text !~ /\n/) {
282 # Format the text so it is centered, one word per line
283 local @textparts = split(/[ \t\n]+/, $text);
284 local $maxlength = 0;
285 for ($i = 0; $i < scalar(@textparts); $i++) {
286 if (length($textparts[$i]) > $maxlength) {
287 $maxlength = length($textparts[$i]);
288 }
289 }
290 $text = "";
291 for ($i = 0; $i < scalar(@textparts); $i++) {
292 if (length($textparts[$i]) < $maxlength) {
293 $text .= ' ' x ((($maxlength - length($textparts[$i])) / 2) + 1);
294 }
295 $text .= $textparts[$i] . "\n";
296 }
297 }
298
299 # generate on and off images (yellow)
300 my $options = "-text \"$text\" -filenamestem yc$image_name -image_dir $image_dir";
301 $options .= " -width 77 -height 26 -fixed_width -whitespace -fontsize 13";
302 # special case for russian images
303 if ($language_symbol eq "ru") {
304 $options .= " -foundry cronyx -fontname helvetica";
305 }
306 # special case for Kazakh images (can also handle Russian)
307 elsif ($language_symbol eq "kz") { # || $language_symbol eq "ru") {
308 $options .= " -foundry 2rebels -fontname \"helv kaz\"";
309 }
310 `$ENV{'GSDLHOME'}/bin/script/gimp/flash_button-1.2.pl $options`;
311
312 # generate on and off images (green)
313 $options = "-text \"$text\" -filenamestem gc$image_name -image_dir $image_dir";
314 $options .= " -width 77 -height 26 -fixed_width -whitespace -fontsize 13";
315 $options .= " -bgcolor \"#96c19b\"";
316 # special case for russian images
317 if ($language_symbol eq "ru") {
318 $options .= " -foundry cronyx -fontname helvetica";
319 }
320 # special case for Kazakh images (can also handle Russian)
321 elsif ($language_symbol eq "kz") { # || $language_symbol eq "ru") {
322 $options .= " -foundry 2rebels -fontname \"helv kaz\"";
323 }
324 `$ENV{'GSDLHOME'}/bin/script/gimp/flash_button-1.2.pl $options`;
325
326 # generate on and off images (grey) - only the light image (off) is used
327 $options = "-text \"$text\" -filenamestem nc$image_name -image_dir $image_dir";
328 $options .= " -width 77 -height 26 -fixed_width -whitespace -fontsize 13";
329 $options .= " -bgcolor \"#7E7E7E\" -fontcolor \"#a0a0a0\"";
330 # special case for russian images
331 if ($language_symbol eq "ru") {
332 $options .= " -foundry cronyx -fontname helvetica";
333 }
334 # special case for Kazakh images (can also handle Russian)
335 elsif ($language_symbol eq "kz") { # || $language_symbol eq "ru") {
336 $options .= " -foundry 2rebels -fontname \"helv kaz\"";
337 }
338 `$ENV{'GSDLHOME'}/bin/script/gimp/flash_button-1.2.pl $options`;
339 # delete the unused dark image
340 unlink(util::filename_cat($image_dir, "nc$image_name" . "on.gif"));
341
342 } elsif ($image_type eq "green_bar_left_aligned") {
343
344 # generate green bar image (we're assuming these bars are always 537
345 # pixels and are never stretched by excess text
346 my $options = "-text \"$text\" -filename ${image_name}.gif -dont_center";
347 $options .= " -width 537 -width_space 15 -image_dir $image_dir";
348 # special case for russian images
349 if ($language_symbol eq "ru") {
350 $options .= " -foundry cronyx -fontname helvetica";
351 }
352 # special case for Kazakh images (can also handle Russian)
353 elsif ($language_symbol eq "kz") { # || $language_symbol eq "ru") {
354 $options .= " -foundry 2rebels -fontname \"helv kaz\"";
355 }
356 `$ENV{'GSDLHOME'}/bin/script/gimp/green_bar-1.2.pl $options`;
357
358 } elsif ($image_type eq "green_title") {
359
360 # read the width if it is specified in $image_macros
361 my ($width) = $image_macros =~ /_width${image_name}x?_\s*[^\{]*\{(\d+)\}/;
362 $width = 200 unless ($width);
363
364 # generate green title image
365 my $options = "-text \"$text\" -filename ${image_name}.gif -image_dir $image_dir";
366 $options .= " -width $width -height 57 -stripe_alignment right -text_alignment right";
367 $options .= " -fontsize 26 -fontweight bold";
368 # special case for russian images
369 if ($language_symbol eq "ru") {
370 $options .= " -foundry cronyx -fontname helvetica";
371 }
372 # special case for Kazakh images (can also handle Russian)
373 elsif ($language_symbol eq "kz") { # || $language_symbol eq "ru") {
374 $options .= " -foundry 2rebels -fontname \"helv kaz\"";
375 }
376 `$ENV{'GSDLHOME'}/bin/script/gimp/title_icon-1.2.pl $options`;
377
378 # get width of resulting image and edit _width..._ macro in $image_macros
379 # (no longer needed since we always resize to the width read from $image_macros.)
380 # my $fullfilename = &util::filename_cat ($image_dir, "${image_name}.gif");
381 # &process_width_macro ($fullfilename, $image_name, \$image_macros);
382
383 } elsif ($image_type eq "hand_made") {
384
385 $hand_made ++;
386
387 } else {
388
389 print STDERR "WARNING (translate.pl): unknown image type found ($image_type)\n";
390
391 }
392
393 return "\n\#\# \"$origtext\" \#\# $image_type \#\# $image_name \#\#$image_macros";
394}
395
396sub process_width_macro {
397 my ($filename, $image_name, $image_macros) = @_;
398
399 my $img_info = &get_img_info ($filename);
400 $$image_macros =~ s/(_width${image_name}x?_\s*(?:\[[^\]]*\])?\s*\{)(\d+)(\})/$1$img_info->{'width'}$3/s;
401}
402
403sub add_language_param {
404 my ($macroname, $paramlist) = @_;
405
406 my $first = 1;
407 if (defined $paramlist) {
408 $paramlist =~ s/^\[//;
409 $paramlist =~ s/\]$//;
410 my @params = split /\,/, $paramlist;
411 $paramlist = "";
412 foreach $param (@params) {
413 # remove any existing language parameter
414 if ($param !~ /^l=/) {
415 $paramlist .= "," unless $first;
416 $paramlist .= $param;
417 $first = 0;
418 }
419 }
420 }
421 $paramlist .= "," unless $first;
422 $paramlist .= "l=" . $language_symbol;
423 return "$macroname [$paramlist] {";
424}
425
426sub get_img_info {
427 my ($imagefile) = @_;
428 my %info = ();
429
430 if (!-r $imagefile) {
431 print STDERR "ERROR (translate.pl): couldn't open $imagefile to get dimensions\n";
432 $info{'width'} = 0;
433 $info{'height'} = 0;
434 } else {
435 my $image = gimp_file_load (RUN_NONINTERACTIVE, $imagefile, $imagefile);
436 $info{'width'} = gimp_image_width ($image);
437 $info{'height'} = gimp_image_height ($image);
438 }
439
440 return \%info;
441}
442
443sub query {
444
445 gimp_install_procedure("gsdl_translate", "translate macro files and create images",
446 "", "Stefan Boddie", "Stefan Boddie", "2000-03-14",
447 "<Toolbox>/Xtns/gsdl_translate", "*", &PROC_EXTENSION,
448 [[PARAM_INT32, "run_mode", "Interactive, [non-interactive]"]], []);
449}
450
451Gimp::on_net { gsdl_translate; };
452exit main;
Note: See TracBrowser for help on using the repository browser.