Parent Directory
|
Revision Log
Changed recognition of failed dvipng exit code to match version 0.3.
1 #!/usr/local/bin/perl 2 3 ################################################################################ 4 # WeBWorK 5 # 6 # Copyright (c) 1995-2002 University of Rochester 7 # All rights reserved 8 # 9 ################################################################################ 10 11 use lib '.'; use webworkInit; # WeBWorKInitLine 12 13 use strict; 14 15 =head1 NAME 16 17 ImageGenerator.pm 18 19 =head1 SYNPOSIS 20 21 Create an object for holding bits of math for LaTeX, and then to process 22 them later. 23 24 my $imgen = new ImageGenerator; #create a image generator 25 $imgen->initialize(\%envir); # provide basic data in preparation of image collection 26 $imgen->add(string); # add a new LaTeX string to be processed. 27 # Should be in math mode with \( or \[ 28 # It returns the html tag 29 30 $imgen->getCount(); # Returns the number of images which have been added 31 $imgen->tmpurl(); # Returns the beginning of the html path 32 $imgen->render(); # Generates the images. By default, we reuse old 33 # images when reasonable, unless passed a flag 34 # refresh=>'yes' (or 1) 35 36 =head1 DESCRIPTION 37 38 This module defines an object which holds bits of LaTeX math for 39 generating images. 40 41 =cut 42 43 44 package ImageGenerator; 45 46 47 =head2 new 48 Creates the ImageGenerator object. 49 50 =cut 51 52 53 sub new { 54 my $class = shift; 55 my $self = { 56 latexlines => [], 57 count => 0, 58 tmppath => "", 59 tmpURLstart=>"", 60 filenamestart=> "" 61 }; 62 63 bless $self, $class; 64 } 65 66 67 sub initialize { 68 my $self = shift; 69 my $envir = shift; # pointer to problem envirment hash 70 my $problemnum = $envir->{'probNum'}; 71 my $studname = $envir->{'studentLogin'}; 72 my $psvn = $envir->{'psvn'}; 73 my $setname = $envir->{'setNumber'}; 74 my $tmpURLstart = $envir->{'tempURL'}; 75 my $path=main::surePathToTmpFile(main::convertPath("png/$setname/$psvn/foo")); 76 $path =~ s/foo$//; # remove final foo 77 $self->{sourceFile} = $envir->{templateDirectory} . "/" . 78 $envir->{fileName}; 79 $self->{tmpURLstart} = $tmpURLstart."png/$setname/$psvn"; 80 $self->{tmppath}=$path; 81 $self->{filenamestart}="$studname-prob${problemnum}image"; 82 } 83 84 # Add another string to list to be LaTeX'ed 85 # return the tag 86 sub add { 87 my $self = shift; 88 my $newstr = shift; 89 my $tag = $newstr; 90 $self->{count}++; 91 my $tempURL= $self->tmpurl()."$self->{count}.png"; 92 93 if ($tag =~ /^\\\(/) { 94 $tag =~ s|^\\\( *||; 95 $tag =~ s|\\\)$||; 96 $tag = qq!<img src="$tempURL" align="middle" alt="$tag">!; 97 } else { 98 # Displayed math comes in with \[ stuff \]. To get a good 99 # bounding box through preview, we change that to \( \displaystyle{ 100 # stuff } \), and then center the resulting image 101 $tag =~ s|^\\\[ *||; 102 $tag =~ s|\\\]$||; 103 $newstr = '\(\displaystyle{'.$tag.'}\)'; 104 $tag = qq!<div align="center"><img src="$tempURL" align="middle" alt="$tag"></div>!; 105 } 106 push @{$self->{latexlines}}, $newstr; 107 108 return $tag; 109 } 110 111 sub getCount { 112 my $self = shift; 113 return($self->{count}); 114 } 115 116 sub tmpurl { 117 my $self = shift; 118 return("$self->{tmpURLstart}/$self->{filenamestart}"); 119 } 120 121 sub render { 122 my $self = shift; 123 my %opts = @_; 124 125 if($self->{count}==0) {return ;} # Don't run latex if there are no images 126 127 my $refreshMe = 0; 128 if (defined($opts{refresh}) and 129 (($opts{refresh} eq "yes") or ($opts{refresh} == 1))) { 130 $refreshMe = 1; 131 } 132 133 # $refreshMe = 1; # Uncomment for testing 134 my $latexfilenamebase = $self->{tmppath}.$self->{filenamestart}; 135 136 my $sourcePath = $self->{sourceFile}; 137 my $tempFile = "${latexfilenamebase}".$self->{count}.".png"; # last image 138 139 if ($refreshMe or not -e $tempFile 140 or (stat $sourcePath)[9] > (stat $tempFile)[9]) { 141 # image file doesn't exist, or source file is newer then image file 142 # or we just want new images produced 143 144 # my $old_cdir = `pwd`; # cd for running latex 145 # chomp($old_cdir); 146 chdir($self->{tmppath}) || 147 warn "Could not move into temporary directory $self->{tmppath}"; 148 149 if (-e "$latexfilenamebase.tex") { 150 unlink("$latexfilenamebase.tex") || 151 warn "Could not delete old LaTeX file"; 152 } 153 154 local *LATEXME; 155 open(LATEXME,">$latexfilenamebase.tex") || warn "Cannot create temporary tex file"; 156 print LATEXME <<'EOT'; 157 \documentclass[12pt]{article} 158 \nonstopmode 159 \usepackage{amsmath,amsfonts,amssymb} 160 \def\gt{>} 161 \def\lt{<} 162 163 \usepackage[active,textmath,displaymath]{preview} 164 \begin{document} 165 EOT 166 167 my $j; 168 for $j (@{$self->{latexlines}}) { 169 print LATEXME "\n$j\n"; 170 } 171 172 print LATEXME '\end{document}'."\n"; 173 close(LATEXME); 174 175 chmod(0666, "$latexfilenamebase.tex") || warn "Could not change permissions on $latexfilenamebase.tex"; 176 177 my $error_log = '/dev/null'; ## by default do not log error messages 178 $error_log = &Global::getErrorLog if $Global::imageDebugMode; 179 180 $ENV{PATH} .= "$Global::extendedPath"; 181 my $dvipng_res = int($Global::dvipngScaling * 1000+0.5); 182 my $cmdout=""; 183 184 # remove any old files using this name 185 unlink("$self->{filenamestart}.dvi","$self->{filenamestart}.log", 186 "$self->{filenamestart}.aux","missfont.log"); 187 188 $cmdout=system("$Global::externalLatexPath $self->{filenamestart}.tex >>$error_log 2>>$error_log"); 189 warn "$Global::externalLatexPath $self->{filenamestart}.tex >>$error_log 2>>$error_log -- FAILED in ImageGenerator returned $cmdout" if $cmdout; 190 191 $cmdout=system("$Global::externalDvipngPath -x$dvipng_res -bgTransparent -Q$Global::dvipngShrinkFactor -mode $Global::dvipngMode -D$Global::dvipngDPI $self->{filenamestart}.dvi >>$error_log 2>>$error_log"); 192 warn "$Global::externalDvipngPath -x$dvipng_res -bgTransparent -Q$Global::dvipngShrinkFactor -mode $Global::dvipngMode -D$Global::dvipngDPI $self->{filenamestart}.dvi >>$error_log 2>>$error_log -- FAILED in ImageGenerator.pm returned $cmdout" if $cmdout; 193 194 unless ($Global::imageDebugMode) { 195 unlink("$self->{filenamestart}.dvi","$self->{filenamestart}.log", 196 "$self->{filenamestart}.tex", 197 "$self->{filenamestart}.aux" 198 ); 199 } 200 # chdir($old_cdir); 201 } 202 } 203 204 1;
| aubreyja at gmail dot com | ViewVC Help |
| Powered by ViewVC 1.0.9 |