3 # Generate covers for books to use as cover in ePUBs
5 # This program is copyright 2012, 2017 by Javier Fernandez-Sanguino <jfs@computer.org>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 # For more information please see
22 # http://www.gnu.org/licenses/licenses.html#GPL
28 use File::Path qw(mkpath);
30 my $PROGRAM_NAME = 'gbtoepub';
31 my $USAGE = "$PROGRAM_NAME [options] book-code\n\t--meta=[metadata file]\n\t--xml=[book XML]\n\t--language=[language area of input data (output determined by meta file)]\n\t--font-files=[font-files]\n\t--no-validate\n\t--verbose\n";
33 my $FILENAME_SEPARATOR = '/';
35 my $CONVERT = qx{which convert};
36 my $CP = qx{which cp};
37 my $MV = qx{which mv};
38 my $RM = qx{which rm};
39 my $CHMOD = qx{which chmod};
47 # Check that all the binaries are were want them
50 push @BINARIES, ($CONVERT, $CP, $MV, $RM, $CHMOD);
54 die "$PROGRAM_NAME: Cannot find binary '".$_."'. Please install it.\n";
60 my $OEBPS_DIR = 'OEBPS';
61 my $META_INF_DIR = 'META-INF';
69 my $fontFiles = "common/fonts";
75 ### read command line options
77 while( $#ARGV > -1 ) {
78 my $cmdLineItem = shift @ARGV;
79 if( $cmdLineItem =~ /^--meta=(.+)$/ ) {
82 elsif( $cmdLineItem =~ /^--xml=(.+)$/ ) {
85 elsif( $cmdLineItem =~ /^--language=(.+)$/ ) {
88 elsif( $cmdLineItem =~ /^--verbose/ ) {
91 elsif( $cmdLineItem =~ /^--font-files=(.+)$/ ) {
95 $bookCode = $cmdLineItem;
99 if( $bookCode eq '' ) {
100 die "$PROGRAM_NAME: Unspecified book code\n$USAGE";
102 if( $metaFile eq '' ) { $metaFile = "$language/.publisher/rules/epub"; }
103 if( $bookXML eq '' ) { $bookXML = "$language/xml/$bookCode.xml"; }
106 ### read in metadata file
108 unless( -e $metaFile && -f $metaFile && -r $metaFile ) {
109 die qq{$PROGRAM_NAME: Improper metadata file "$metaFile"\n};
112 open( META, '<', $metaFile ) or
113 die qq{$PROGRAM_NAME: Unable to open metadata file "$metaFile": $!\n};
116 while( my $line = <META> ) {
117 $meta .= $line if $line !~ /^[[:space:]]*#/;
121 ### interpret rules from metadata
122 my $rulesString = '';
123 if( $meta =~ /^[[:space:]]*$bookCode[[:space:]]*{([^}]*)}/sm ) {
127 die "$PROGRAM_NAME: Book code ($bookCode) not found in metadata file or invalid file syntax\n";
130 my @rules = split( /[[:space:]\n]*;[[:space:]\n]*/, $rulesString );
132 foreach my $rule (@rules) {
133 if( $rule =~ /[[:space:]]*([^:]+)[[:space:]]*:[[:space:]]*(.+)$/s ) {
134 $rulesHash{ $1 } = $2;
137 die "$PROGRAM_NAME: Unrecognized rule syntax:\n$rule\n";
141 unless( defined $rulesHash{'book-series'} ) {
142 die "$PROGRAM_NAME: no book series set\n";
145 my $SERIES = get_series($rulesHash{'book-series'}) ;
146 my $SERIES_NUMBER = get_series_number($bookCode);
149 ### create output directories
152 $outPath{'top'} = $rulesHash{'language'} . $FILENAME_SEPARATOR .
153 'epub' . $FILENAME_SEPARATOR .
154 $rulesHash{'book-series'} . $FILENAME_SEPARATOR .
157 $outPath{'meta-inf'} = $outPath{'top'} . $FILENAME_SEPARATOR . $META_INF_DIR;
158 $outPath{'oebps'} = $outPath{'top'} . $FILENAME_SEPARATOR . $OEBPS_DIR;
160 foreach my $directory (keys(%outPath)) {
161 unless( -e $outPath{$directory} && -d $outPath{$directory} ) {
162 mkpath $outPath{$directory}
163 or die "$PROGRAM_NAME: Unknown error creating output directory " .
164 "\"$outPath{$directory}\"\n";
168 ### create content files
173 # Generate the cover image. This can be done in two ways:
174 # 1.- A file is available under the directory of JPEG files for the book
175 # 2.- A file is generated using imagemagick
178 my $coverImage = $outPath{'oebps'} . $FILENAME_SEPARATOR . "cover.jpg";
179 # Cover filename generated by Project Aon
180 my $pa_coverImage = $rulesHash{'language'} . $FILENAME_SEPARATOR . "jpeg" . $FILENAME_SEPARATOR .$rulesHash{'book-series'}. $FILENAME_SEPARATOR .$bookCode . $FILENAME_SEPARATOR . "cover.jpg";
182 if ( -e "$pa_coverImage") {
184 print STDERR "DEBUG: Using cover from $pa_coverImage\n" if $verbose;
185 system "cp $pa_coverImage $coverImage";
188 # Use Imagemagick to generate the cover page
190 print STDERR "DEBUG: Will generate cover with ImageMagick\n" if $verbose;
191 my $TITLE = quote_shell(find_title($bookXML));
192 my $AUTHOR = quote_shell(find_author($bookXML));
193 my $ILLUSTRATOR = quote_shell(find_illustrator($bookXML));
194 my $convert_cmd = "";
196 if ( -e "$fontFiles/SouvenirStd-Demi.otf" && -e "$fontFiles/SouvenirStd-Light.otf" ) {
197 $convert_cmd="$CONVERT -size 600x800 -background white -font $fontFiles/SouvenirStd-Demi.otf -pointsize 32 -fill '#006633' -gravity north caption:\"\" -annotate +0+218 \"$TITLE\" -font $fontFiles/SouvenirStd-Light.otf -pointsize 22 -fill black -annotate +0+304 '$AUTHOR' -annotate +0+333 '$ILLUSTRATOR' $coverImage"
199 print STDERR "WARN: Fontfiles not found, using standard font\n";
200 $convert_cmd="$CONVERT -size 600x800 -background white -pointsize 32 -fill '#006633' -gravity north caption:\"\" -annotate +0+218 \"$TITLE\" -pointsize 22 -fill black -annotate +0+304 '$AUTHOR' -annotate +0+333 '$ILLUSTRATOR' $coverImage";
203 print STDERR "DEBUG: Will run '$convert_cmd'\n" if $verbose;
210 ################################################################################
212 ################################################################################
215 # Determine series long name by the series acronym
218 my $series_name = "";
219 if ($series eq "lw" ) {
220 $series_name = "Lone Wolf";
221 } elsif ($series eq "ls" ) {
222 $series_name = "Lobo Solitario";
223 } elsif ($series eq "gs" ) {
224 $series_name = "Grey Star the Wizard";
225 } elsif ($series eq "fw" ) {
226 $series_name = "Freeway Warrior";
228 print STDERR "WARN: Undefined series. Short name given: '$series'\n";
229 $series_name = "[undefined]";
234 # Determine the series number based on book code
235 sub get_series_number {
237 my $series_number = "";
238 if ( $bookCode =~ /^(\d\d)/ ) {
241 print STDERR "WARN: Undefined series number. Book code is '$bookCode'.\n";
242 $series_number = "xx";
244 return $series_number;
247 # Determine the book title by reading the book meta information
250 my $title = ""; my $line = "";
251 open (BOOK, "head -100 $book | ") || die ("Could not read $book: $!");
252 while ($title eq "" && ( $line = <BOOK> ) ) {
254 if ( $line =~ /<title>(.*?)<\/title>/ ) {
260 if ( $title eq "" ) {
261 print STDERR "WARN: Cannot find title for book '$book'\n";
262 $title = "[Undefined]";
265 return convert_entities($title);
268 # Determine the book author by reading the book meta information
273 open (BOOK, "head -100 $book |") || die ("Could not read $book: $!");
276 while ($author eq "" && ( $line = <BOOK> ) ) {
278 if ( $find_line == 1 && $line =~ /<line>(.*?)<\/line>/ ) {
281 $find_line = 1 if ( $line =~ /<creator class="medium">/ );
282 $find_line = 0 if ( $line =~ /<\/creator>/ );
283 if ( $line =~ /<creator class="author">(.*?)<\/title>/ ) {
289 if ( $author eq "" ) {
290 print STDERR "WARN: Cannot find author for book '$book'\n";
291 $author = "[Undefined]";
298 # Determine the book illustrator by reading the book meta information
299 sub find_illustrator {
301 my $illustrator = "";
303 open (BOOK, "head -100 $book | ") || die ("Could not read $book: $!");
306 while ($illustrator eq "" && ( $line = <BOOK> ) ) {
308 if ( $find_line == 1 && $line =~ /<line>Illustrated by (.*?)<\/line>/ ) {
311 if ( $find_line == 1 && $line =~ /<line>Ilustrado por (.*?)<\/line>/ ) {
314 $find_line = 1 if ( $line =~ /<creator class="medium">/ );
315 $find_line = 0 if ( $line =~ /<\/creator>/ );
316 if ( $line =~ /<creator class="illustrator">(.*?)<\/title>/ ) {
322 if ( $illustrator eq "" ) {
323 print STDERR "WARN: Cannot find illustrator for book '$book'\n";
324 if ( $language eq "en" ) {
325 $illustrator = "[Unknown]";
326 } elsif ( $language eq "es" ) {
327 $illustrator = "[Desconocido]";
330 if ( $language eq "en" ) {
331 $illustrator = "Illustrated by ".$illustrator;
332 } elsif ( $language eq "es" ) {
333 $illustrator = "Illustrado por ".$illustrator;
339 sub convert_entities {
340 # Convert character entities to their correspondent values
343 $text =~ s/\<ch.apos\/\>/'/g;
344 $text =~ s/\<ch.nbsp\/\>/ /g;
345 $text =~ s/\<ch.plusmn\/\>/+-/g;
346 $text =~ s/\<ch.aacute\/\>/á/g;
347 $text =~ s/\<ch.eacute\/\>/é/g;
348 $text =~ s/\<ch.iacute\/\>/í/g;
349 $text =~ s/\<ch.oacute\/\>/ó/g;
350 $text =~ s/\<ch.uacute\/\>/ú/g;
351 $text =~ s/\<ch.ntilde\/\>/ñ/g;
352 $text =~ s/\<ch.Aacute\/\>/Á/g;
353 $text =~ s/\<ch.Eacute\/\>/É/g;
354 $text =~ s/\<ch.Iacute\/\>/Í/g;
355 $text =~ s/\<ch.Oacute\/\>/Ó/g;
356 $text =~ s/\<ch.Uacute\/\>/Ú/g;
357 $text =~ s/\<ch.auml\/\>/ä/g;
358 $text =~ s/\<ch.euml\/\>/ë/g;
359 $text =~ s/\<ch.iuml\/\>/ï/g;
360 $text =~ s/\<ch.ouml\/\>/ö/g;
361 $text =~ s/\<ch.uuml\/\>/ü/g;
362 $text =~ s/\<ch.Ntilde\/\>/Ñ/g;
363 $text =~ s/\<ch.acute\/\>/´/g;
364 $text =~ s/\<ch.iexcl\/\>/¡/g;
365 $text =~ s/\<ch.iquest\/\>/¿/g;
366 $text =~ s/\<ch.laquo\/\>/«/g;
367 $text =~ s/\<ch.raquo\/\>/»/g;
368 $text =~ s/\<ch.ampersand\/\>/&/g;
373 # Quote metacaracters for shell use