source: main/trunk/greenstone2/bin/script/translate.pl@ 24371

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

Updated stuff for Thai image generation, using the Microsoft BrowalliaUPC font.

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