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

Last change on this file since 2447 was 2299, checked in by sjboddie, 23 years ago

Removed a bunch of _width_ macros from language macro files as they're
no longer used.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 11.9 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;
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.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.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.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 "document_button") {
242
243 # generate on and off images
244 my $options = "-text \"$text\" -filenamestem $image_name";
245 $options .= " -fixed_width -whitespace -image_dir $image_dir";
246 if ($language_symbol eq "ru") {
247 $options .= " -fontsize 8 -foundry cronyx -fontname helvetica";
248 }
249 `$ENV{'GSDLHOME'}/bin/script/gimp/flash_button.pl $options`;
250
251 # get width of new images and edit width macro
252 # my $fullfilename = &util::filename_cat ($image_dir, "${image_name}on.gif");
253 # &process_width_macro ($fullfilename, $image_name, \$image_macros);
254
255 } elsif ($image_type eq "green_bar_left_aligned") {
256
257 # generate green bar image (we're assuming these bars are always 537
258 # pixels and are never stretched by excess text
259 my $options = "-text \"$text\" -filename ${image_name}.gif -dont_center";
260 $options .= " -width 537 -width_space 15 -image_dir $image_dir";
261 if ($language_symbol eq "ru") {
262 $options .= " -foundry cronyx -fontname helvetica";
263 }
264 `$ENV{'GSDLHOME'}/bin/script/gimp/green_bar.pl $options`;
265
266 } elsif ($image_type eq "green_title") {
267
268 # read the width if it is specified in $image_macros
269 my ($width) = $image_macros =~ /_width${image_name}x?_\s*[^\{]*\{(\d+)\}/;
270 $width = 200 unless ($width);
271
272 # generate green title image
273 my $options = "-text \"$text\" -filename ${image_name}.gif -image_dir $image_dir";
274 $options .= " -width $width -height 57 -stripe_alignment right -text_alignment right";
275 $options .= " -fontsize 26 -fontweight bold";
276 # special case for russian images
277 if ($language_symbol eq "ru") {
278 $options .= " -foundry cronyx -fontname helvetica";
279 }
280 `$ENV{'GSDLHOME'}/bin/script/gimp/title_icon.pl $options`;
281
282 # get width of resulting image and edit _width..._ macro in $image_macros
283 # (no longer needed since we always resize to the width read from $image_macros.)
284 # my $fullfilename = &util::filename_cat ($image_dir, "${image_name}.gif");
285 # &process_width_macro ($fullfilename, $image_name, \$image_macros);
286
287 } elsif ($image_type eq "hand_made") {
288
289 $hand_made ++;
290
291 } else {
292
293 print STDERR "WARNING (translate.pl): unknown image type found ($image_type)\n";
294
295 }
296
297 return "\n\#\# \"$origtext\" \#\# $image_type \#\# $image_name \#\#$image_macros";
298}
299
300sub process_width_macro {
301 my ($filename, $image_name, $image_macros) = @_;
302
303 my $img_info = &get_img_info ($filename);
304 $$image_macros =~ s/(_width${image_name}x?_\s*(?:\[[^\]]*\])?\s*\{)(\d+)(\})/$1$img_info->{'width'}$3/s;
305}
306
307sub add_language_param {
308 my ($macroname, $paramlist) = @_;
309
310 my $first = 1;
311 if (defined $paramlist) {
312 $paramlist =~ s/^\[//;
313 $paramlist =~ s/\]$//;
314 my @params = split /\,/, $paramlist;
315 $paramlist = "";
316 foreach $param (@params) {
317 # remove any existing language parameter
318 if ($param !~ /^l=/) {
319 $paramlist .= "," unless $first;
320 $paramlist .= $param;
321 $first = 0;
322 }
323 }
324 }
325 $paramlist .= "," unless $first;
326 $paramlist .= "l=" . $language_symbol;
327 return "$macroname [$paramlist] {";
328}
329
330sub get_img_info {
331 my ($imagefile) = @_;
332 my %info = ();
333
334 if (!-r $imagefile) {
335 print STDERR "ERROR (translate.pl): couldn't open $imagefile to get dimensions\n";
336 $info{'width'} = 0;
337 $info{'height'} = 0;
338 } else {
339 my $image = gimp_file_load (RUN_NONINTERACTIVE, $imagefile, $imagefile);
340 $info{'width'} = gimp_image_width ($image);
341 $info{'height'} = gimp_image_height ($image);
342 }
343
344 return \%info;
345}
346
347sub query {
348
349 gimp_install_procedure("gsdl_translate", "translate macro files and create images",
350 "", "Stefan Boddie", "Stefan Boddie", "2000-03-14",
351 "<Toolbox>/Xtns/gsdl_translate", "*", &PROC_EXTENSION,
352 [[PARAM_INT32, "run_mode", "Interactive, [non-interactive]"]], []);
353}
354
355sub net {
356 gsdl_translate;
357}
358
359exit main;
Note: See TracBrowser for help on using the repository browser.