source: other-projects/image-generation/trunk/make-images.pl@ 21019

Last change on this file since 21019 was 13536, checked in by mdewsnip, 16 years ago

Initial revision

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 20.5 KB
Line 
1#!/usr/bin/perl
2
3###########################################################################
4#
5# make-images.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) 2005 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# This script is an alternative to translate.pl that uses ImageMagick
30# instead of Gimp to generate the button images. The advantage of using
31# ImageMagick is that it supports Unicode (and other multibyte encodings).
32#
33# This script requires the following components installed:
34#
35# - A recent version of ImageMagick, and the PerlMagick extensions.
36# (http://www.imagemagick.org)
37#
38# - The FreeType libraries (http://freetype.sourceforge.net)
39#
40# The script reads through the macrofile specified at the command line,
41# and generates images for each of the image macros. It does this by
42# 'pasting' the button text onto a set of pre-generated images, using the
43# annotate.pl script. The background images used, and the arguments to
44# annotate.pl, depend on the type of image (eg. nav_bar_button) being
45# generated.
46#
47###########################################################################
48
49
50BEGIN {
51 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
52 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
53}
54
55
56use parsargv;
57use unicode;
58use util;
59
60
61my $fribidi_path = "/home/mdewsnip/packages/fribidi-0.10.4/bin";
62
63
64# -------------------------------------------------------------------------
65# This script can use any TrueType font (the 'font' argument).
66#
67# The base font size should be a reasonable size for nav_bar_buttons.
68# A value of 15 is likely to be a good starting point for most fonts. The
69# baseline offset value (a fraction) should be set if the text for the
70# language is displayed too high or too low.
71#
72# ARIALBD.TTF font: partial Unicode support, bold
73# font size = 15 (calculated so text_height = 17)
74#
75# ARIALUNI.TTF font: complete Unicode 2.0 support, but no bold
76# font size = 11 (calculated so text_height = 17)
77#
78# Available options:
79#
80# font (default ARIALBD.TTF)
81# base_font_size (default 15)
82# baseline_offset_factor (moves text down) (default 0.0)
83# right_to_left (default false)
84# no_nav_bar_button_text_height_warnings (default false)
85# document_button_font_size
86# document_button_width (default 66)
87# document_button_height (default 30)
88# collector_bar_button_width (default 77)
89# collector_bar_button_height (default 28)
90# home_page_button_width (default 100)
91#
92# -------------------------------------------------------------------------
93%language_specifications =
94 (
95 # Arabic
96 "ar", { 'base_font_size' => "16",
97 'right_to_left' => 1,
98 'no_nav_bar_button_text_height_warnings' => 1 },
99
100 # Bengali
101 "bn", { 'font' => "ARIALUNI.TTF",
102 'base_font_size' => "16",
103 'baseline_offset_factor' => (1/5),
104 'no_nav_bar_button_text_height_warnings' => 1 },
105
106 # Catalan
107 "ca", { 'document_button_width' => "67",
108 'collector_bar_button_width' => "80" },
109
110 # Czech
111 "cs", { 'document_button_width' => "79" },
112
113 # German
114 "de", { 'document_button_width' => "108" },
115
116 # Greek
117 "el", { 'document_button_width' => "100" },
118
119 # English
120 "en", { },
121
122 # Spanish
123 "es", { 'document_button_width' => "67",
124 'collector_bar_button_width' => "106" },
125
126 # Farsi
127 "fa", { 'base_font_size' => "16",
128 'right_to_left' => 1,
129 'no_nav_bar_button_text_height_warnings' => 1 },
130
131 # Finnish
132 "fi", { 'document_button_width' => "81" },
133
134 # French
135 "fr", { 'document_button_width' => "107",
136 'collector_bar_button_width' => "92" },
137
138 # Galician
139 "gl", { 'document_button_width' => "71",
140 'collector_bar_button_width' => "103" },
141
142 # Hebrew
143 "he", { },
144
145 # Hindi -- not tried yet
146 # "hi", { },
147
148 # Croatian
149 "hr", { 'document_button_width' => "87" },
150
151 # Armenian
152 "hy", { 'font' => "ARIALUNI.TTF",
153 'base_font_size' => "18",
154 'baseline_offset_factor' => (1/9),
155 'no_nav_bar_button_text_height_warnings' => 1,
156 'document_button_font_size' => "11",
157 'document_button_width' => "108" },
158
159 # Indonesian
160 "id", { 'document_button_width' => "80" },
161
162 # Italian
163 "it", { 'document_button_width' => "76",
164 'collector_bar_button_width' => "89" },
165
166 # Japanese
167 "ja", { 'font' => "ARIALUNI.TTF",
168 'base_font_size' => "15",
169 'baseline_offset_factor' => (1/5) },
170
171 # Georgian
172 "ka", { 'font' => "ARIALUNI.TTF",
173 'base_font_size' => "15",
174 'baseline_offset_factor' => (1/15),
175 'no_nav_bar_button_text_height_warnings' => 1,
176 'document_button_width' => "100",
177 'collector_bar_button_width' => "87",
178 'collector_bar_button_height' => "45" },
179
180 # Kazakh
181 "kk", { 'document_button_width' => "101",
182 'collector_bar_button_width' => "103",
183 'home_page_button_width' => "106" },
184
185 # Kannada -- not tried yet
186 # "kn", { },
187
188 # Kirghiz
189 "ky", { },
190
191 # Latvian
192 "lv", { },
193
194 # Maori
195 "mi", { 'document_button_width' => "97",
196 'document_button_height' => "42" },
197
198 # Mongolian
199 "mn", { 'document_button_width' => "95",
200 'collector_bar_button_width' => "84",
201 'collector_bar_button_height' => "42",
202 'home_page_button_width' => "109" },
203
204 # Dutch
205 "nl", { 'document_button_width' => "86" },
206
207 # Polish
208 "pl", { 'document_button_width' => "83" },
209
210 # Portuguese (Brazil)
211 "pt-br", { },
212
213 # Portuguese (Portugal)
214 "pt-pt", { },
215
216 # Russian
217 "ru", { 'document_button_width' => "69",
218 'collector_bar_button_width' => "91",
219 'home_page_button_width' => "113" },
220
221 # Serbian (Latin version)
222 "sr", { 'document_button_width' => "87" },
223
224 # Thai -- not supported because Freetype doesn't support combining characters!
225 # "th", { 'font' => "ARIALUNI.TTF" },
226
227 # Turkish
228 "tr", { 'document_button_width' => "75" },
229
230 # Ukrainian
231 "uk", { 'document_button_width' => "81" },
232
233 # Vietnamese
234 "vi", { },
235
236 # Chinese (Simplified)
237 "zh", { 'font' => "ARIALUNI.TTF",
238 'base_font_size' => "15",
239 'baseline_offset_factor' => (4/15) },
240
241 # Chinese (Traditional)
242 "zh-tr", { 'font' => "ARIALUNI.TTF",
243 'base_font_size' => "16",
244 'baseline_offset_factor' => (1/4),
245 'document_button_width' => "67",
246 'collector_bar_button_width' => "103",
247 'home_page_button_width' => "106" },
248
249 );
250
251
252sub print_usage
253{
254 print STDERR "\n";
255 print STDERR "mkimages.pl: Uses ImageMagick to generate images required by a\n";
256 print STDERR " Greenstone macro file.\n\n";
257 print STDERR " usage: $0 [options] macrofile\n\n";
258 print STDERR " options:\n";
259 print STDERR " -language_symbol ISO abbreviation of language (e.g. German=de,\n";
260 print STDERR " French=fr, Maori=mi)\n";
261 print STDERR " -image_dir directory full path to directory in which to create images\n";
262 print STDERR " (defaults to images)\n\n";
263}
264
265
266sub main
267{
268 # Parse command line arguments
269 if (!parsargv::parse(\@ARGV,
270 'verbose', \$verbose,
271 'language_symbol/.*', \$language_symbol,
272 'image_dir/.*/images', \$image_dir)) {
273 &print_usage();
274 die "\n";
275 }
276
277 if (!defined $ARGV[0]) {
278 print STDERR "Error: No macro file supplied\n";
279 &print_usage();
280 die "\n";
281 }
282 my $macrofile = $ARGV[0];
283 die "\nError: Macrofile $macrofile does not exist\n\n" unless -e $macrofile;
284
285 if (!-e $image_dir) {
286 mkdir ($image_dir, 511) || die "\nError: Couldn't create image_dir $image_dir\n\n";
287 }
288
289 if (!$language_specifications{$language_symbol}) {
290 die "\nError: No specification exists for this language.\n\n";
291 }
292
293 open(INPUT, $macrofile) || die "\nError: Couldn't open $macrofile for reading.\n\n";
294 open(OUTPUT, ">$macrofile.new") || die "\nError: Couldn't open temporary file $macrofile.new for writing\n\n";
295
296 &parse_file(INPUT, OUTPUT);
297
298 close(OUTPUT);
299 close(INPUT);
300
301 &util::cp($macrofile, "$macrofile.orig");
302 &util::mv("$macrofile.new", $macrofile);
303}
304
305
306sub parse_file
307{
308 my ($input, $output) = @_;
309
310 undef $/;
311 my $dmfile = <$input>;
312 $/ = "\n";
313
314 # Process all the images in the macrofile
315 $dmfile =~ s/(?:^|\n)\#\#\s*\"([^\"]*)\"\s*\#\#\s*([^\s\#]*)\s*\#\#\s*([^\s\#]*)\s*\#\#(.*?)(?=(\n\#|\s*\Z))/&process_image ($1, $2, $3, $4)/esg;
316
317 # Add the language parameters to each macro
318 $dmfile =~ s/(\n\s*)(_[^_]*_)\s*(\[[^\]]*\])?\s*\{/$1 . &add_language_param ($2, $3)/esg;
319 print $output $dmfile;
320}
321
322
323sub process_image
324{
325 my ($text, $image_type, $image_name, $image_macros) = @_;
326
327 my $font = $language_specifications{$language_symbol}{"font"} || "ARIALBD.TTF";
328 my $base_font_size = $language_specifications{$language_symbol}{"base_font_size"} || "15";
329 my $baseline_offset_factor = $language_specifications{$language_symbol}{"baseline_offset_factor"} || "0.0";
330
331 # Convert all "\n" strings in the text into the newline character
332 my $text_internal = $text;
333 $text_internal =~ s/\\n/\n/g;
334
335 # Most languages have left-to-right text
336 my $h_align_flush = "left";
337
338 # A couple of extra steps are necessary for right-to-left text (Arabic, Farsi)
339 if (defined($language_specifications{$language_symbol}{"right_to_left"})) {
340 # Write the text string out to a file for shape_arabic.pl to work on
341 open(STRING_FILE, ">string.raw");
342 print STRING_FILE $text_internal;
343 close(STRING_FILE);
344
345 # Use shape_arabic.pl to "join" the characters correctly and fribidi to reverse the characters properly
346 $text_internal = `perl -S shape_arabic.pl -file string.raw | $fribidi_path/fribidi`;
347
348 # Remove the extra space fribidi adds before the reversed string
349 $text_internal =~ s/(^|\n)\s*/$1/g;
350
351 # Remove the temporary text file
352 &util::rm("string.raw");
353
354 # Text aligned "flush" will now be at the right
355 $h_align_flush = "right";
356 }
357
358 my $text_unicode = &unicode::utf82unicode($text_internal);
359
360 # Edit image macros
361 unless ($language_symbol eq "en") {
362 $image_macros =~ s/(_httpimg_\/)(?:[^\/\}]*\/)?([^\}]*\.(?:gif|jpe?g|png))/$1$language_symbol\/$2/gs;
363 }
364
365 # Generate top_nav_button images (eg. "preferences")
366 if ($image_type eq "top_nav_button") {
367 my $font_size = $base_font_size;
368
369 my $tnb_options = "-image_width 40 -stretchable_width -image_height 20";
370 $tnb_options .= " -padding_left 2 -padding_right 2";
371 $tnb_options .= " -baseline_offset " . &round($font_size * $baseline_offset_factor);
372 $tnb_options .= " -font $font -font_size $font_size";
373
374 # Generate on and off images
375 &annotate_image("tnb_ylon.gif", $tnb_options, $text_unicode, "${image_name}on.gif");
376 &annotate_image("tnb_ylof.gif", $tnb_options, $text_unicode, "${image_name}of.gif");
377 }
378
379 # Generate nav_bar_button images (eg. "search", classifier buttons)
380 elsif ($image_type eq "nav_bar_button") {
381 my $font_size = $base_font_size;
382
383 my $nbb_options = "-image_width 87 -image_height 17 -stretchable_width";
384 $nbb_options .= " -baseline_offset " . &round($font_size * $baseline_offset_factor);
385 $nbb_options .= " -font $font -font_size $font_size";
386 $nbb_options .= " -no_text_height_warnings" if $language_specifications{$language_symbol}{"no_nav_bar_button_text_height_warnings"};
387
388 # Generate on and off images, and green image
389 &annotate_image("nbb_ylon.gif", $nbb_options, $text_unicode, "${image_name}on.gif");
390 &annotate_image("nbb_ylof.gif", $nbb_options, $text_unicode, "${image_name}of.gif");
391 &annotate_image("nbb_gron.gif", $nbb_options, $text_unicode, "${image_name}gr.gif");
392 }
393
394 # Generate document_button images (eg. "expand text")
395 elsif ($image_type eq "document_button") {
396 my $font_size = $language_specifications{$language_symbol}{"document_button_font_size"} || &round(($base_font_size * 4) / 5);
397
398 my $document_button_width = $language_specifications{$language_symbol}{"document_button_width"} || "66";
399 my $document_button_height = $language_specifications{$language_symbol}{"document_button_height"} || "30";
400
401 my $db_options = "-image_width $document_button_width -image_height $document_button_height";
402 $db_options .= " -baseline_offset " . &round($font_size * $baseline_offset_factor);
403 $db_options .= " -font $font -font_size $font_size";
404
405 # Generate on and off images
406 &annotate_image("db_ylon.gif", $db_options, $text_unicode, "${image_name}on.gif");
407 &annotate_image("db_ylof.gif", $db_options, $text_unicode, "${image_name}of.gif");
408 }
409
410 # Generate collector_bar_button images (eg. "source data")
411 elsif ($image_type eq "collector_bar_button") {
412 my $font_size = &round(($base_font_size * 4) / 5);
413
414 my $collector_bar_button_width = $language_specifications{$language_symbol}{"collector_bar_button_width"} || "77";
415 my $collector_bar_button_height = $language_specifications{$language_symbol}{"collector_bar_button_height"} || "28";
416
417 my $cbb_options = "-image_width $collector_bar_button_width -image_height $collector_bar_button_height";
418 $cbb_options .= " -baseline_offset " . &round($font_size * $baseline_offset_factor);
419 $cbb_options .= " -font $font -font_size $font_size";
420
421 # Generate on and off images (yellow), on and off images (green), and grey image
422 &annotate_image("cbb_ylon.gif", $cbb_options, $text_unicode, "yc${image_name}on.gif");
423 &annotate_image("cbb_ylof.gif", $cbb_options, $text_unicode, "yc${image_name}of.gif");
424 &annotate_image("cbb_gron.gif", $cbb_options, $text_unicode, "gc${image_name}on.gif");
425 &annotate_image("cbb_grof.gif", $cbb_options, $text_unicode, "gc${image_name}of.gif");
426 &annotate_image("cbb_gyof.gif", $cbb_options . " -text_colour \"#a0a0a0\"", $text_unicode, "nc${image_name}of.gif");
427 }
428
429 # Generate green_bar_left_aligned images (eg. "results" bar)
430 elsif ($image_type eq "green_bar_left_aligned") {
431 my $font_size = $base_font_size;
432
433 my $lgb_options = "-image_width 537 -image_height 17";
434 $lgb_options .= " -h_align $h_align_flush -padding_left 15 -padding_right 15";
435 $lgb_options .= " -baseline_offset " . &round($font_size * $baseline_offset_factor);
436 $lgb_options .= " -font $font -font_size $font_size";
437 $lgb_options .= " -no_text_height_warnings" if $language_specifications{$language_symbol}{"no_nav_bar_button_text_height_warnings"};
438
439 # Generate on image only
440 &annotate_image("nbb_gron.gif", $lgb_options, $text_unicode, "${image_name}.gif");
441 }
442
443 # Generate green_title images (eg. "about", classifier titles)
444 elsif ($image_type eq "green_title") {
445 my $font_size = ($base_font_size * 2);
446
447 my $gt_options = "-image_width 200 -image_height 57 -stretchable_width -stretchable_height -note_if_stretched";
448 $gt_options .= " -h_align right -padding_left 5 -padding_right 5 -padding_bottom 5 -v_align top";
449 if ($text_internal =~ /\n/) {
450 $gt_options .= " -baseline_offset " . &round($font_size * $baseline_offset_factor);
451 }
452 $gt_options .= " -font $font -font_size $font_size";
453
454 # Generate green title image
455 &annotate_image("gt.gif", $gt_options, $text_unicode, "${image_name}.gif");
456 }
457
458 # Generate plain collection_icons
459 elsif ($image_type eq "collection_icon") {
460 my $font_size = &round(($base_font_size * 6) / 5);
461
462 my $ci_options = "-image_width 150 -image_height 44 -stretchable_height";
463 $ci_options .= " -h_align $h_align_flush -padding_left 2 -padding_right 2 -v_align top";
464 $ci_options .= " -font $font -font_size $font_size";
465
466 # Generate collection icon (small)
467 &annotate_image("cism.gif", $ci_options, $text_unicode, "${image_name}_sm.gif");
468
469 $font_size = &round(($font_size * 3) / 2);
470
471 $ci_options = "-image_width 225 -image_height 66 -stretchable_height";
472 $ci_options .= " -h_align $h_align_flush -padding_left 4 -padding_right 4 -v_align top";
473 $ci_options .= " -font $font -font_size $font_size";
474
475 # Generate collection icon
476 &annotate_image("cism.gif", $ci_options, $text_unicode, "${image_name}.gif");
477 }
478
479 # Generate documented_example_collection_icons (cog-wheel images)
480 elsif ($image_type eq "documented_example_collection_icon") {
481 my $font_size = &round(($base_font_size * 6) / 5);
482
483 my $dec_options = "-image_width 150 -image_height 88";
484 $dec_options .= " -h_align $h_align_flush -padding_left 1 -padding_right 1 -v_align top";
485 $dec_options .= " -font $font -font_size $font_size";
486
487 # Generate documented example collection icon
488 &annotate_image("dec.gif", $dec_options, $text_unicode, "${image_name}.gif");
489 }
490
491 # Generate toki_collection_icons
492 elsif ($image_type eq "toki_collection_icon") {
493 my $font_size = &round(($base_font_size * 6) / 5);
494
495 my $toki_options = "-image_width 150 -image_height 110";
496 $toki_options .= " -h_align $h_align_flush -padding_left 6 -padding_right 1 -v_align bottom -padding_bottom 20";
497 $toki_options .= " -font $font -font_size $font_size";
498
499 # Generate documented example collection icon
500 &annotate_image("toki.gif", $toki_options, $text_unicode, "${image_name}.gif");
501 }
502
503 # Generate home_page_button images (eg. "administration", "documentation")
504 elsif ($image_type eq "home_page_button") {
505 my $font_size = &round(($base_font_size * 4) / 5);
506
507 my $home_page_button_width = $language_specifications{$language_symbol}{"home_page_button_width"} || "100";
508
509 my $hpb_options = "-image_width $home_page_button_width -image_height 30 -stretchable_height";
510 $hpb_options .= " -baseline_offset " . &round($font_size * $baseline_offset_factor);
511 $hpb_options .= " -v_align center -padding_top 2";
512 $hpb_options .= " -font $font -font_size $font_size";
513
514 # Generate on and off images
515 &annotate_image("hpb_ylon.gif", $hpb_options, $text_unicode, "${image_name}on.gif");
516 &annotate_image("hpb_ylof.gif", $hpb_options, $text_unicode, "${image_name}of.gif");
517 }
518
519 # ...and that's all we know how to generate
520 else {
521 print STDERR "WARNING: Unknown image type found ($image_type)\n";
522 }
523
524 return "\n\#\# \"$text\" \#\# $image_type \#\# $image_name \#\#$image_macros";
525}
526
527
528sub annotate_image
529{
530 my ($background_image, $options, $text_unicode, $output_name) = @_;
531
532 # Convert the text to the form ImageMagick requires
533 my $text_imagick = "";
534 foreach $character (@$text_unicode) {
535 if ($character == 0x0A) {
536 # Newline character
537 $text_imagick .= "\\n";
538 }
539 elsif ($character < 0x80) {
540 # Normal ascii character
541 $text_imagick .= chr($character);
542 }
543 else {
544 # Unicode character (convert to a four digit hexadecimal value)
545 my $hex_val = sprintf("%lx", $character);
546 $text_imagick .= "\\0x" . '0' x (4 - length($hex_val)) . $hex_val;
547 }
548 }
549
550 # Use the annotate.pl script to paste the text onto the background image
551 my $output_file = &util::filename_cat($image_dir, $output_name);
552 my $verbose_option = "";
553 $verbose_option = "-verbose" if $verbose;
554 print "\nRunning: annotate.pl -background_image $background_image $options -text \"$text_imagick\" -output_file $output_file\n" if $verbose;
555 `perl -S annotate.pl -background_image $background_image $options -text \"$text_imagick\" -output_file $output_file $verbose_option`;
556}
557
558
559sub round
560{
561 my ($fp_number) = @_;
562 if ($fp_number < 0.0) {
563 return int($fp_number - 0.5);
564 }
565 else {
566 return int($fp_number + 0.5);
567 }
568}
569
570
571sub add_language_param {
572 my ($macroname, $paramlist) = @_;
573
574 if ($language_symbol eq "en") {
575 return "$macroname {";
576 }
577
578 my $first = 1;
579 if (defined $paramlist) {
580 $paramlist =~ s/^\[//;
581 $paramlist =~ s/\]$//;
582 my @params = split /\,/, $paramlist;
583 $paramlist = "";
584 foreach $param (@params) {
585 # remove any existing language parameter
586 if ($param !~ /^l=/) {
587 $paramlist .= "," unless $first;
588 $paramlist .= $param;
589 $first = 0;
590 }
591 }
592 }
593 $paramlist .= "," unless $first;
594 $paramlist .= "l=" . $language_symbol;
595 return "$macroname [$paramlist] {";
596}
597
598
599&main(@ARGV);
Note: See TracBrowser for help on using the repository browser.