Changeset 19628

Show
Ignore:
Timestamp:
27.05.2009 22:04:45 (10 years ago)
Author:
davidb
Message:

Restructuring into supporting functions

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • other-projects/trunk/realistic-books/src/pdf2realbook.pl

    r19253 r19628  
    4545 
    4646 
    47 if ($#ARGV < 0) 
    48 { 
    49     my $full_progname = $0; 
     47sub print_usage 
     48{ 
     49    my ($full_progname) = @_; 
     50 
    5051    my $progname = basename($full_progname); 
    5152 
    5253    print STDERR "\nUsage: $progname pdf_filename\n"; 
    5354    print STDERR "or     perl -S $progname pdf_filename\n\n"; 
    54 } 
    55 else 
    56 { 
    57     my $progname = `pdf2swf --version`; 
    58     if (defined $progname && $progname =~ m/swftools/i) { 
    59     } else { 
    60     die("\nPlease make sure that pdf2swf is located in the same location as this script.\nIf you are in Linux/Macintosh, please install swftools in SWFToolsPackage.\n"); 
    61     } 
    62     my $filename = $ARGV[0]; 
     55 
     56    exit(-1); 
     57} 
     58 
     59sub check_pdf2swf 
     60{ 
     61    my $output = `pdf2swf --version`; 
     62 
     63    if (!defined $output || ($output !~ m/swftools/i)) { 
     64    print STDERR "Unable to find pdf2swf: $!\n"; 
     65    print STDERR "Have you sourced setup.bash (Linux/MacOS)?\n"; 
     66    print STDERR "Have you run     setup.bat  (Windows)?\n"; 
     67    exit(-1); 
     68    } 
     69} 
     70 
     71sub check_file 
     72{ 
     73    my ($filename) = @_; 
     74 
    6375    if (-d $filename) { 
    64     die("$filename is a directory! Please specify the file extension. \n"); 
    65     } 
    66     if (-f $filename) { 
    67     } else { 
    68     die("Cannot find $filename! \n"); 
    69     } 
    70  
    71 ##    print STDERR "Ignore the message called RegOpenKeyEx failed\n"; 
    72  
    73     my($file_root, $fulldir, $file_ext) = fileparse($filename,qr/\.[^.]*/); 
    74  
    75     my $folder_name = catfile($ENV{'REALISTIC_BOOKS_HOME'},"books",$file_root); 
     76    print STDERR "Error: $filename is a directory.\n"; 
     77    print STDERR "       Please specify the PDF file as input.\n"; 
     78    exit(-1); 
     79    } 
     80 
     81    if (!-e $filename) { 
     82    print STDERR "Error: Unable to find $filename\n"; 
     83    exit(-1); 
     84    } 
     85} 
     86 
     87sub copy_model_col 
     88{ 
     89    my ($folder_name) = @_; 
    7690 
    7791    #-- 
     
    92106    copy($src_filename,$dst_filename); 
    93107    } 
    94  
    95     my $html_input = "<HTML>\n<title>$folder_name</title>\n<Description>\n"; 
     108} 
     109 
     110sub pdf2swf_info 
     111{ 
     112    my ($filename) = @_; 
     113 
     114    my $cmd = "pdf2swf --info $filename"; 
     115    if ($ENV{'RBOS'} =~ /^windows$/i) { 
     116    $cmd .= " 2>null"; 
     117    } 
     118    else { 
     119    $cmd .= " 2>/dev/null"; 
     120    } 
     121     
     122    my @output_lines = split(/\n/,`$cmd`); 
     123 
     124    my $last_line = pop(@output_lines); 
     125 
     126    # Watch out for trailing \n 
     127    $last_line = pop(@output_lines) if ($last_line =~ m/^$/); 
     128 
     129    my ($num_pages) = ($last_line =~ m/^page=(\d+)/); 
     130 
     131    return $num_pages; 
     132} 
     133 
     134     
     135sub generate_html 
     136{ 
     137    my ($full_folder_name,$folder_root,$filename) = @_; 
     138 
     139    my $num_pages = pdf2swf_info($filename); 
     140    print STDOUT "Total page: $num_pages\n"; 
     141 
     142    my $html_input = "<HTML>\n<title>$folder_root</title>\n<Description>\n"; 
    96143    $html_input = "$html_input<!-- Please do not remove Filename and PageFolder metadata -->\n"; 
    97     $html_input = "$html_input<Metadata name=\"Filename\">$folder_name</Metadata>\n"; 
    98     $html_input = "$html_input<Metadata name=\"PageFolder\">$folder_name</Metadata>\n"; 
     144    $html_input = "$html_input<Metadata name=\"Filename\">$folder_root</Metadata>\n"; 
     145    $html_input = "$html_input<Metadata name=\"PageFolder\">.</Metadata>\n"; 
    99146    $html_input = "$html_input<Metadata name=\"StartNumbering\">1</Metadata>\n"; 
    100147    $html_input = "$html_input</Description>\n"; 
    101      
    102     my $i = 1; 
    103     my $page_num = $i; 
    104  
    105     while ($i <= $page_num) 
    106     { 
     148 
     149    for (my $i=1; $i<=$num_pages; $i++) { 
     150 
    107151    my $backFile = "Page_$i.swf"; 
    108     my $frontFile = "$folder_name"; 
     152    my $frontFile = "$full_folder_name"; 
    109153    my $output_file = catfile($frontFile,$backFile); 
    110154    my $imgfile = `pdf2swf -p $i -s insertstop -s zoom=100 $filename -o $output_file`; 
    111155     
    112     if ($i == 1) 
    113     { 
    114         if ($imgfile =~ m/Pages:/i) 
    115         { 
    116         my $tmp = $'; 
    117         if ($tmp =~ m/(\d+)/) 
    118         { 
    119             $page_num = $1; 
    120             print STDOUT "Total page: $page_num\n"; 
    121         } 
    122         } 
    123     } 
    124  
    125156    print STDOUT "creating page $i\n"; 
    126157     
    127     #$html_input = "$html_input<newpage totalframes=\"1\" /><!-- filenum=\"$i\" -->\n"; 
    128158    $html_input = "$html_input<newpage filename=\"Page_$i\" />\n"; 
    129     $i = $i + 1; 
    130159    } 
    131160 
    132161    $html_input = "$html_input</HTML>\n"; 
    133      
    134     my $html_file = catfile($folder_name,"pages.htm"); 
     162 
     163    return $html_input; 
     164} 
     165 
     166 
     167sub main 
     168{ 
     169    my ($argc,@argv) = @_; 
     170 
     171    my $full_progname = $0; 
     172 
     173    if ($argc < 0) { 
     174    print_usage($full_progname); 
     175    } 
     176 
     177    check_pdf2swf(); 
     178 
     179    my $filename = $argv[0]; 
     180    check_file($filename); 
     181 
     182    # print STDERR "Ignore the message called RegOpenKeyEx failed\n"; 
     183 
     184    my($file_root, $fulldir, $file_ext) = fileparse($filename,qr/\.[^.]*/); 
     185 
     186    my $full_folder_name = catfile($ENV{'REALISTIC_BOOKS_HOME'},"books",$file_root); 
     187 
     188    copy_model_col($full_folder_name); 
     189 
     190    my $html_input = generate_html($full_folder_name,$file_root,$filename); 
     191 
     192    # Write out HTML file 
     193    my $html_file = catfile($full_folder_name,"pages.htm"); 
    135194    open (PROD, ">$html_file") || die("Error Writing to File: $html_file $!"); 
     195    print STDERR "Creating $html_file\n"; 
     196 
    136197    print PROD $html_input; 
    137198    close (PROD) || die("Error Closing File: $html_file $!"); 
    138     print STDERR "creating $html_file\n"; 
    139  
    140     print STDOUT "Realistic book generated in $folder_name\n"; 
    141  
    142 } 
     199 
     200 
     201    print STDOUT "Realistic book generated in $full_folder_name\n"; 
     202 
     203} 
     204 
     205 
     206&main(scalar(@ARGV),@ARGV);