forked from wikimedia/mediawiki-extensions-Math
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MathLatexRender.php
314 lines (273 loc) · 10.3 KB
/
MathLatexRender.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
/**
* Takes LaTeX fragments, sends them to a latex distro for rendering
*
* @author Julian Diaz
* Based on the MathTexvc.php devloped by several contributors, and on the work of
* Jesse B. Dooley for the MathLatex Extension publisehd at http://mathlatex.sourceforge.net
*
* In order to use this render, the following settings are required in LocalSettings.php
*
* wfLoadExtension( 'Math' );
* $wgUseTeX= true;
* $wgLatexOnWindows = true;
* $wgPdfLaTexCommand = "D:\\texlive\bin\win32\pdflatex.exe";
* $wgDvipngCommand = "D:\\texlive\bin\win32\dvipng.exe";
*/
class MathLatexRender extends MathTexvc {
private $equation = '';
// Overides
public function render() {
global $wgMathDirectory;
// test for '%' as the first character in $equation
// If true pass the equation, with no wrapper, to LaTeX.
// If fail, format the equation and pass it to LaTeX.
if( ($this->tex[0] == '%' ) == false ) {
$this->equation = $this->wrapper( $this->tex );
}
$tmpDir = wfTempDir();
$this->setHash(md5(rand()));
$tempFsFile = new TempFSFile( "$tmpDir/{$this->getHash()}.png" );
//$tempFsFile->autocollect(); // destroy file when $tempFsFile leaves scope
// call LaTeXrender
// returns true on success or error string on failure
$render_return = $this->LaTeXrender();
// test for error string
if( is_string( $render_return ) == true ){
$msg = "<span tyle=\"color:red\">Render::render</span> failed<br />\n" .
$render_return . "<br />\n";
wfDebugLog( 'MathLatexRender', $msg );
return $msg;
}
// call DviPNGrender
// returns true on success or error string on failure
$render_return = $this->DviPNGrender();
// test for error string
if( is_string( $render_return ) == true ){
$msg = "<span style=\"color:red\">Render:DviPNGrender</span> failed<br />\n" .
$render_return . "<br />\n";
wfDebugLog( 'MathLatexRender', $msg );
return $msg;
} elseif ( !file_exists( "$tmpDir/{$this->getHash()}.png" ) ) {
return $this->getError( 'math_image_error' );
} elseif ( filesize( "$tmpDir/{$this->getHash()}.png" ) == 0 ) {
return $this->getError( 'math_image_error' );
}
$hashpath = $this->getHashPath(); // final storage directory
$backend = $this->getBackend();
# Create any containers/directories as needed...
if ( !$backend->prepare( [ 'dir' => $hashpath ] )->isOK() ) {
return $this->getError( 'math_output_error' );
}
// Store the file at the final storage path...
// Bug 56769: buffer the writes and do them at the end.
if ( !isset( $wgHooks['ParserAfterParse']['FlushMathBackend'] ) ) {
$backend->mathBufferedWrites = [];
$wgHooks['ParserAfterParse']['FlushMathBackend'] = function () use ( $backend ) {
global $wgHooks;
unset( $wgHooks['ParserAfterParse']['FlushMathBackend'] );
$backend->doQuickOperations( $backend->mathBufferedWrites );
unset( $backend->mathBufferedWrites );
};
}
$backend->mathBufferedWrites[] = [
'op' => 'store',
'src' => "$tmpDir/{$this->getHash()}.png",
'dst' => "$hashpath/{$this->getHash()}.png",
'ref' => $tempFsFile // keep file alive
];
$status_code = copy("$tmpDir/{$this->getHash()}.png","$wgMathDirectory/{$this->getHashSubPath()}/{$this->getHash()}.png");
// $this->cleanTemporaryDirectory();
if (!$status_code) {
$this->_errorcode = 6;
return false;
}
// Get here and everything worked.
return true;
}/**
* private functions
*/
/**
* LaTeXrender
*
* @brief Convert the latex statement to a dvi
*
* @function
* @name LaTeXrender
* @author Jesse B. Dooley
* @date January 8, 2016
* @ingroup Extensions
* @link http://mathlatex.sourceforge.net
*
* Modified by Julian diaz on December 1, 2016. From the Original Versio 1.0
*
*/
private function LaTeXrender( ) {
global $wgPdfLaTexCommand;
$tmpDir = wfTempDir();
$tempFsFileName = $this->getHash();
wfDebugLog( 'MathLatexRender', $tmpDir );
if( chdir ( $tmpDir ) == false ) {
$msg = "<span style=\"color:red\">Render:LaTeXrender chdir</span> failed<br />\n";
wfDebugLog( 'MathLatexRender', $msg );
return $msg;
}
// write out the tex file in $MathTempPath
$file_handle = fopen( $tempFsFileName . ".tex", "w");
// test for a valid filehandle
if( $file_handle == false ) {
$msg = "<span style=\"color:red\">Render:LaTeXrender fopen</span> failed<br />\n";
wfDebugLog( 'MathLatexRender', $msg );
return $msg;
}
// write out the equation file
if( fwrite($file_handle, $this->equation ) == false) {
$msg = "<span style=\"color:red\">Render:LaTeXrender fwrite</span> failed<br />\n";
wfDebugLog( 'MathLatexRender', $msg );
return $msg;
}
// fwrite succeeded, close
if( fclose( $file_handle ) == false ) {
$msg = "<span style=\"color:red\">Render:LaTeXrender fclose</span> failed<br />\n";
wfDebugLog( 'MathLatexRender', $msg );
return $msg;
}
// have the input file.
// assemble the latex call
$cmd = $wgPdfLaTexCommand.
' --fmt=latex ' . // use latex format
'--interaction=nonstopmode ' . // don't stop, no point in it
$tempFsFileName . ".tex"; // source file
$retval = null;
$contents = wfShellExec( $cmd, $retval );
// verify if tex was produced.
if ( file_exists( $tempFsFileName . ".tex" ) == false ) {
$msg = "<span style=\"color:red\">Render:LaTeXrender tex creation</span> failed<br />\n" .
"cmd " . $cmd . "<br />\n".
"retval " . $retval . "<br />\n" .
"result " . $contents . "<br />\n";
wfDebugLog( 'MathLaTeX', $msg );
return $msg;
} else {
return true;
}
} // LaTeXrender
/**
* DviPNGrender
*
* @brief Convert a dvi file to the $MathImageExt image
*
* @function
* @name DviPNGrender
* @author Jesse B. Dooley
* @date January 8, 2016
* @ingroup Extensions
* @link http://mathlatex.sourceforge.net
*
* Modified by Julian diaz on December 1, 2016. From the Original Versio 1.0
*/
private function DviPNGrender() {
global $MathDotsPerInch;
global $wgDvipngCommand;
$tmpDir = wfTempDir();
$tempFsFileName = $this->getHash();
// change current dir to $MathTempPath
if( chdir ( $tmpDir ) == false ) {
$msg = "<span style=\"color:red\">Render:DviPNGrender chdir</span> failed<br />\n";
wfDebugLog( 'MathLaTeX', $msg );
return $msg;
}
// assemble the dvipng call
$cmd = $wgDvipngCommand . // dvipng.exe command
' -bg Transparent ' . // set background to transparent
'--gamma 1.5 ' . // color interpolation
'-D 120' . ' ' . // output resolution
'-T tight ' . // reduce image size to just the equation
'--strict ' . // don't stop, no point in it
$tempFsFileName . '.dvi ' . // input file
'-o ' .
$tempFsFileName . ".png"; // output file
$retval = null;
$contents = wfShellExec( $cmd, $retval );
// verify if png was produced.
if( file_exists( $tempFsFileName . ".png" ) == false ) {
$msg = "<span style=\"color:red\">Render::DviPNGrender png creation</span> failed<br />\n" .
"cmd " . $cmd . "<br />\n" .
"retval " . $retval . "<br />\n" .
"dvipng result " . $contents . "<br />\n";
wfDebugLog( 'MathLaTeX', $msg );
return $msg;
} else {
return true;
}
} // DviPNGrender
/**
* wrapper
*
* @brief Wrap the latex statement in commands for texlive
*
* @function
* @name onParserFirstCallInit
* @version 1.0
* @author Jesse B. Dooley
* @date January 8, 2016
* @ingroup Extensions
* @link http://mathlatex.sourceforge.net
*
* @param string latex statement
* @return string latex statement wrapped
*/
public static function wrapper( $plain_text ) {
return "\\nonstopmode" .
"\n" .
"\\documentclass[12pt]{article}" .
"\n" .
"\\usepackage{mathtools}" . // texlive-collection-latexrecommended
"\n" .
"\\usepackage{lmodern}" .
"\n" .
"\usepackage{amsmath}" . // texlive-collection-latex
"\n" .
"\\usepackage{amsfonts}" .
"\n" .
"\\usepackage{amssymb}" .
"\n" .
"\\usepackage{pst-plot}" . // texlive-collection-pstricks
"\n" .
"\\usepackage{color}" .
"\n" .
"\\pagestyle{empty}" .
"\n".
"\\begin{document}" .
"\n" .
"$$" .
"\n" .
$plain_text .
"\n" .
"$$" .
"\n" .
"\\end{document}\n";
} // wrapper
/**
* Remove Temporary files
*
* Original work from Benjamin Zeiss
*
* Modified by Julian diaz on December 1, 2016.
*/
function cleanTemporaryDirectory() {
$tmpDir = wfTempDir();
$tempFsFileName = $this->getHash();
// change current dir to $MathTempPath
if( chdir ( $tmpDir ) == false ) {
$msg = "<span style=\"color:red\">Render:DviPNGrender chdir</span> failed<br />\n";
wfDebugLog( 'MathLaTeX', $msg );
return $msg;
}
unlink($tempFsFileName.".tex");
unlink($tempFsFileName.".aux");
unlink($tempFsFileName.".log");
unlink($tempFsFileName.".dvi");
unlink($tempFsFileName.".png");
}
}