2 // +---------------------------------------------------------------------+
4 // +---------------------------------------------------------------------+
5 // | This is a WikiPlugin that surrounds tex commands given as parameter |
6 // | with a page description and renders it using several existing |
7 // | engines into a gif, png or jpeg file. |
8 // | TexToPng is usage example for WikiPluginCached. |
10 // | Copyright (C) 2002 Johannes Große (Johannes Große) |
11 // | You may copy this code freely under the conditions of the GPL |
12 // +---------------------------------------------------------------------+
14 /*-----------------------------------------------------------------------
16 *----------------------------------------------------------------------*/
17 // needs (la)tex, dvips, gs, netpbm, libpng
18 // LaTeX2HTML ftp://ftp.dante.de/tex-archive/support/latex2html
19 $texbin = '/usr/bin/tex';
20 $dvipsbin = '/usr/bin/dvips';
21 $pstoimgbin = '/usr/bin/pstoimg';
23 // output mere debug messages (should be set to false in a stable
25 define('TexToPng_debug', false);
27 /*-----------------------------------------------------------------------
29 *----------------------------------------------------------------------*/
31 | use antialias for rendering;
32 | anitalias: blurs, _looks better_, needs twice space, renders slowlier
34 define('TexToPng_antialias', true);
37 | Use transparent background; dont combine with antialias on a dark
38 | background. Seems to have a bug: produces strange effects for some
39 | ps-files (almost non readable,blurred output) even when directly
40 | invoked from shell. So its probably a pstoimg bug.
42 define('TexToPng_transparent', false);
45 | default value for rescaling
46 | allowed range: 0 - 5 (integer)
48 define('TexToPng_magstep', 3);
51 /*-----------------------------------------------------------------------
55 *----------------------------------------------------------------------*/
57 // check boolean constants
59 if (!defined('TexToPng_debug')) { define('TexToPng_debug', false); }
60 if (!defined('TexToPng_antialias')) { define('TexToPng_antialias', false); }
61 if (!defined('TexToPng_transparent')) { define('TexToPng_transparent', false); }
63 /*-----------------------------------------------------------------------
65 *----------------------------------------------------------------------*/
67 require_once "lib/WikiPluginCached.php";
69 class WikiPlugin_TexToPng extends WikiPluginCached
71 /* --------- overwrite virtual or abstract methods ---------------- */
73 function getPluginType() {
74 return PLUGIN_CACHED_IMG_ONDEMAND;
81 function getDescription() {
82 return 'Converts TeX to an image. May be used to embed'
83 . ' formulas in PhpWiki.';
86 function getDefaultArguments() {
87 return array('tex' => "",
88 'magstep' => TexToPng_magstep,
91 'antialias' => TexToPng_antialias ? 'on' : 'off',
92 'transparent' => TexToPng_transparent ? 'on' : 'off',
96 function getImage($dbi, $argarray, $request) {
98 $this->checkParams($tex, $magstep, $subslash, $antialias, $transparent);
99 return $this->TexToImg($tex, $magstep, $antialias, $transparent);
102 function getExpire($dbi, $argarray, $request) {
106 function getImageType($dbi, $argarray, $request) {
111 function getAlt($dbi, $argarray, $request) {
116 function embedImg($url,$dbi,$argarray,$request) {
117 $html = HTML::img( array(
119 'alt' => htmlspecialchars($this->getAlt($dbi,$argarray,$request))
121 if ($argarray['center']=='on')
122 return HTML::div( array('style' => 'text-align:center;'), $html);
126 /* -------------------- error handling ---------------------------- */
128 function dbg( $out ) {
129 // test if verbose debug info is selected
130 if (TexToPng_debug) {
131 $this->complain( $out."\n" );
133 if (!$this->_errortext) {
134 // yeah, I've been told to be quiet, but obviously
135 // an error occured. So at least complain silently.
136 $this->complain(' ');
142 /* -------------------- parameter handling ------------------------ */
144 function helptext() {
145 $aa= TexToPng_antialias ?'on(default)$|$off':'on$|$off(default)';
146 $tp= TexToPng_transparent?'on(default)$|$off':'on$|$off(default)';
148 '/settabs/+/indent&$<$?plugin /bf{Tex} & [{/tt transparent}] & = "png(default)$|$jpeg$|$gif"& /cr'."\n".
149 '/+&$<$?plugin /bf{TexToPng} & /hfill {/tt tex} & = "/TeX/ commands"& /cr'."\n".
150 '/+& & /hfill [{/tt img}] & = "png(default)$|$jpeg$|$gif"& /cr'."\n".
151 '/+& & /hfill [{/tt magstep}] & = "0 to 5 ('.TexToPng_magstep.' default)"& /cr'."\n".
152 '/+& & /hfill [{/tt center}] & = "on$|$off(default)"& /cr'."\n".
153 '/+& & /hfill [{/tt subslash}] & = "on$|$off(default)"& /cr'."\n".
154 '/+& & /hfill [{/tt antialias}] & = "'.$aa.'"& /cr'."\n".
155 '/+& & /hfill [{/tt transparent}] & = "'.$tp.'"&?$>$ /cr'."\n";
157 return strtr($help, '/', '\\' );
161 function checkParams( &$tex, &$magstep, $subslash, &$aalias, &$transp ) {
163 if ($subslash=='on') {
164 // WORKAROUND for backslashes
165 $tex = strtr($tex,'/','\\');
168 // ------- check parameters
169 $def = $this->getDefaultArguments();
171 if ($tex=='') { $tex = $this->helptext(); }
173 if ($magstep < 0 || $magstep > 5 ) { $magstep = $def["magstep"]; }
174 // calculate magnification factor
175 $magstep = floor(10*pow(1.2,$magstep))/10;
177 $aalias = $aalias != 'off';
178 $transp = $transp != 'off';
182 /* ------------------ image creation ------------------------------ */
184 function execute($cmd,$complainvisibly=false) {
185 exec($cmd, $errortxt, $returnval);
186 $ok = $returnval == 0;
189 if (!$complainvisibly) {
190 $this->dbg('Error during execution of '.$cmd );
192 while (list($key,$value)=each($errortxt)) {
193 if ($complainvisibly) {
194 $this->complain( $value."\n" );
196 $this->dbg( $value );
203 /* ---------------------------------------------------------------- */
205 function createTexFile($texfile,$texstr) {
206 if ($ok=($fp=fopen($texfile, 'w'))!=0 ) {
209 '\nopagenumbers' . "\n" .
210 '\hoffset=0cm' . "\n" .
211 '\voffset=0cm' . "\n" .
212 // '\hsize=20cm' . "\n" .
213 // '\vsize=10ex' . "\n" .
215 '\vfill\eject' . "\n" .
218 $ok = fwrite($fp, $texcommands);
219 $ok = fclose($fp) && $ok; // close anyway
222 $this->dbg('could not write .tex file: ' . $texstr);
227 /* ---------------------------------------------------------------- */
229 function TexToImg($texstr, $scale, $aalias, $transp) {
230 $cacheparams = $GLOBALS['CacheParams'];
231 $tempfiles = tempnam($cacheparams['cache_dir'],'TexToPng');
232 $img = 0; // $size = 0;
234 // procuce options for pstoimg
236 ($aalias ? '-aaliastext -color 8 ' : '-color 1 ') .
237 ($transp ? '-transparent ' : '') .
238 '-scale ' . $scale . ' ' .
239 '-type png -crop btlr -geometry 600x150 -margins 0,0';
241 // rely on intelligent bool interpretation
243 $this->createTexFile($tempfiles.'.tex',$texstr) &&
244 $this->execute('cd '.$cacheparams['cache_dir'].'; '.
245 "$texbin ".$tempfiles.'.tex',true) &&
246 $this->execute("$dvipsbin -o".$tempfiles.'.ps '.$tempfiles.'.dvi') &&
247 $this->execute("$pstoimgbin $options"
248 ' -out '.$tempfiles.'.png '.
249 $tempfiles.'.ps' ) &&
250 file_exists( $tempfiles.'.png' );
253 if (!($img = ImageCreateFromPNG( $tempfiles.'.png' ))) {
254 $this->dbg("Could not open just created image file: $tempfiles");
259 // clean up tmpdir; in debug mode only if no error occured
261 if ( !TexToPng_debug || (TexToPng_debug && $ok)) {
264 unlink($tempfiles.'.ps');
265 unlink($tempfiles.'.tex');
266 //unlink($tempfiles.'.aux');
267 unlink($tempfiles.'.dvi');
268 unlink($tempfiles.'.log');
269 unlink($tempfiles.'.png');
278 } // WikiPlugin_TexToPng
287 // c-hanging-comment-ender-p: nil
288 // indent-tabs-mode: nil