Parent Directory
|
Revision Log
Took it out of debugging mode (where cached images are always ignored).
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 my $refreshMe = 0; 125 if (defined($opts{refresh}) and 126 (($opts{refresh} eq "yes") or ($opts{refresh} == 1))) { 127 $refreshMe = 1; 128 } 129 130 # $refreshMe = 1; # Uncomment for testing 131 my $latexfilenamebase = $self->{tmppath}.$self->{filenamestart}; 132 133 my $sourcePath = $self->{sourceFile}; 134 my $tempFile = "${latexfilenamebase}".$self->{count}.".png"; # last image 135 136 if ($refreshMe or not -e $tempFile 137 or (stat $sourcePath)[9] > (stat $tempFile)[9]) { 138 # image file doesn't exist, or source file is newer then image file 139 # or we just want new images produced 140 141 # my $old_cdir = `pwd`; # cd for running latex 142 # chomp($old_cdir); 143 chdir($self->{tmppath}) || 144 warn "Could not move into temporary directory $self->{tmppath}"; 145 146 if (-e "$latexfilenamebase.tex") { 147 unlink("$latexfilenamebase.tex") || 148 warn "Could not delete old LaTeX file"; 149 } 150 151 local *LATEXME; 152 open(LATEXME,">$latexfilenamebase.tex") || warn "Cannot create temporary tex file"; 153 print LATEXME <<'EOT'; 154 \documentclass[12pt]{article} 155 \nonstopmode 156 \usepackage{amsmath,amsfonts,amssymb} 157 \def\gt{>} 158 \def\lt{<} 159 160 \usepackage[active,textmath,displaymath]{preview} 161 \begin{document} 162 EOT 163 164 my $j; 165 for $j (@{$self->{latexlines}}) { 166 print LATEXME "\n$j\n"; 167 } 168 169 print LATEXME '\end{document}'."\n"; 170 close(LATEXME); 171 172 chmod(0666, "$latexfilenamebase.tex") || warn "Could not change permissions on $latexfilenamebase.tex"; 173 174 my $error_log = '/dev/null'; ## by default do not log error messages 175 $error_log = &Global::getErrorLog if $Global::imageDebugMode; 176 177 $ENV{PATH} .= "$Global::extendedPath"; 178 my $dvipng_res = int($Global::dvipngScaling * 1000+0.5); 179 my $cmdout=""; 180 181 # remove any old files using this name 182 unlink("$self->{filenamestart}.dvi","$self->{filenamestart}.log", 183 "$self->{filenamestart}.aux","missfont.log"); 184 185 $cmdout=system("$Global::externalLatexPath $self->{filenamestart}.tex >>$error_log 2>>$error_log"); 186 warn "$Global::externalLatexPath $self->{filenamestart}.tex >>$error_log 2>>$error_log -- FAILED in ImageGenerator returned $cmdout" if $cmdout; 187 188 $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"); 189 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 !=256; 190 191 unless ($Global::imageDebugMode) { 192 unlink("$self->{filenamestart}.dvi","$self->{filenamestart}.log", 193 "$self->{filenamestart}.tex", 194 "$self->{filenamestart}.aux" 195 ); 196 } 197 # chdir($old_cdir); 198 } 199 } 200 201 1;
| aubreyja at gmail dot com | ViewVC Help |
| Powered by ViewVC 1.0.9 |