| … | |
… | |
| 25 | 'fileFromPath', |
25 | 'fileFromPath', |
| 26 | 'directoryFromPath', |
26 | 'directoryFromPath', |
| 27 | 'createFile', |
27 | 'createFile', |
| 28 | 'createDirectory', |
28 | 'createDirectory', |
| 29 | 'getImageDimmensions', |
29 | 'getImageDimmensions', |
|
|
30 | 'dvipng', |
| 30 | ); |
31 | ); |
| 31 | |
32 | |
|
|
33 | # The above symbols are exported into the caller's namespace. This is usually |
|
|
34 | # done so that they can be shared with a problem's safe compartment via |
|
|
35 | # WeBWorK::PG::Translator. To do this, add the symbol's name to the |
|
|
36 | # %shared_subroutine_hash hash in lib/WeBWorK/PG/Translator.pm. |
| 32 | |
37 | |
| 33 | =head1 Private functions (not methods) used by PGtranslator for file IO. |
38 | =head1 Private functions (not methods) used by PGtranslator for file IO. |
| 34 | |
39 | |
| 35 | =head2 includePGtext |
40 | =head2 includePGtext |
| 36 | |
41 | |
| … | |
… | |
| 199 | my $image = GD::Image->new($imageName); |
204 | my $image = GD::Image->new($imageName); |
| 200 | my ($width, $height) = $image->getBounds(); |
205 | my ($width, $height) = $image->getBounds(); |
| 201 | return ($height, $width); |
206 | return ($height, $width); |
| 202 | } |
207 | } |
| 203 | |
208 | |
|
|
209 | =head2 dvipng |
|
|
210 | |
|
|
211 | dvipng(wd, latex, dvpng, tex, targetPath) |
|
|
212 | |
|
|
213 | $wd, # working directory, for latex and dvipng garbage |
|
|
214 | # (must already exist!) |
|
|
215 | $latex, # path to latex binary |
|
|
216 | $dvipng, # path to dvipng binary |
|
|
217 | $tex, # tex string representing equation |
|
|
218 | $targetPath # location of resulting image file |
|
|
219 | |
|
|
220 | Uses LaTeX and dvipng to convert a LaTeX math expression into a PNG image. |
|
|
221 | |
|
|
222 | =cut |
|
|
223 | |
|
|
224 | sub dvipng($$$$$) { |
|
|
225 | my ( |
|
|
226 | $wd, # working directory, for latex and dvipng garbage |
|
|
227 | # (must already exist!) |
|
|
228 | $latex, # path to latex binary |
|
|
229 | $dvipng, # path to dvipng binary |
|
|
230 | $tex, # tex string representing equation |
|
|
231 | $targetPath # location of resulting image file |
|
|
232 | ) = @_; |
|
|
233 | |
|
|
234 | my $texFile = "$wd/equation.tex"; |
|
|
235 | my $dviFile = "$wd/equation.dvi"; |
|
|
236 | my $dviFile2 = "$wd/equationequation.dvi"; |
|
|
237 | my $dviCall = "equation"; |
|
|
238 | my $pngFile = "$wd/equation1.png"; |
|
|
239 | |
|
|
240 | die "dvipng working directory $wd doesn't exist -- caller should have created it for us!\n" |
|
|
241 | unless -e $wd; |
|
|
242 | |
|
|
243 | # write the tex file |
|
|
244 | local *TEX; |
|
|
245 | open TEX, ">", $texFile; |
|
|
246 | print TEX <<'EOF'; |
|
|
247 | % BEGIN HEADER |
|
|
248 | \batchmode |
|
|
249 | \documentclass[12pt]{article} |
|
|
250 | \usepackage{amsmath,amsfonts,amssymb} |
|
|
251 | \def\gt{>} |
|
|
252 | \def\lt{<} |
|
|
253 | \usepackage[active,textmath,displaymath]{preview} |
|
|
254 | \begin{document} |
|
|
255 | % END HEADER |
|
|
256 | EOF |
|
|
257 | print TEX "\\( \\displaystyle{$tex} \\)\n"; |
|
|
258 | print TEX <<'EOF'; |
|
|
259 | % BEGIN FOOTER |
|
|
260 | \end{document} |
|
|
261 | % END FOOTER |
|
|
262 | EOF |
|
|
263 | close TEX; |
|
|
264 | |
|
|
265 | # call latex |
|
|
266 | system "cd $wd && $latex $texFile"; |
|
|
267 | |
|
|
268 | return 0 unless -e $dviFile; |
|
|
269 | |
|
|
270 | # change the name of the DVI file to get around dvipng's crackheadedness |
|
|
271 | system "/bin/mv", $dviFile, $dviFile2; |
|
|
272 | |
|
|
273 | # call dvipng |
|
|
274 | system "cd $wd && $dvipng $dviCall" and die "dvipng:dvipng failed: $!"; |
|
|
275 | |
|
|
276 | return 0 unless -e $pngFile; |
|
|
277 | |
|
|
278 | system "/bin/mv", $pngFile, $targetPath and die "Failed to mv: $!\n"; |
|
|
279 | } |
|
|
280 | |
| 204 | 1; |
281 | 1; |