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 formulas in PhpWiki.");
85 function getDefaultArguments() {
86 return array('tex' => "",
87 'magstep' => TexToPng_magstep,
90 'antialias' => TexToPng_antialias ? 'on' : 'off',
91 'transparent' => TexToPng_transparent ? 'on' : 'off',
95 function getImage($dbi, $argarray, $request) {
97 $this->checkParams($tex, $magstep, $subslash, $antialias, $transparent);
98 return $this->TexToImg($tex, $magstep, $antialias, $transparent);
101 function getExpire($dbi, $argarray, $request) {
105 function getImageType($dbi, $argarray, $request) {
110 function getAlt($dbi, $argarray, $request) {
115 function embedImg($url,$dbi,$argarray,$request) {
116 $html = HTML::img( array(
118 'alt' => htmlspecialchars($this->getAlt($dbi,$argarray,$request))
120 if ($argarray['center']=='on')
121 return HTML::div( array('style' => 'text-align:center;'), $html);
125 /* -------------------- error handling ---------------------------- */
127 function dbg( $out ) {
128 // test if verbose debug info is selected
129 if (TexToPng_debug) {
130 $this->complain( $out."\n" );
132 if (!$this->_errortext) {
133 // yeah, I've been told to be quiet, but obviously
134 // an error occured. So at least complain silently.
135 $this->complain(' ');
141 /* -------------------- parameter handling ------------------------ */
143 function helptext() {
144 $aa= TexToPng_antialias ?'on(default)$|$off':'on$|$off(default)';
145 $tp= TexToPng_transparent?'on(default)$|$off':'on$|$off(default)';
147 '/settabs/+/indent&$<$?plugin /bf{Tex} & [{/tt transparent}] & = "png(default)$|$jpeg$|$gif"& /cr'."\n".
148 '/+&$<$?plugin /bf{TexToPng} & /hfill {/tt tex} & = "/TeX/ commands"& /cr'."\n".
149 '/+& & /hfill [{/tt img}] & = "png(default)$|$jpeg$|$gif"& /cr'."\n".
150 '/+& & /hfill [{/tt magstep}] & = "0 to 5 ('.TexToPng_magstep.' default)"& /cr'."\n".
151 '/+& & /hfill [{/tt center}] & = "on$|$off(default)"& /cr'."\n".
152 '/+& & /hfill [{/tt subslash}] & = "on$|$off(default)"& /cr'."\n".
153 '/+& & /hfill [{/tt antialias}] & = "'.$aa.'"& /cr'."\n".
154 '/+& & /hfill [{/tt transparent}] & = "'.$tp.'"&?$>$ /cr'."\n";
156 return strtr($help, '/', '\\' );
160 function checkParams( &$tex, &$magstep, $subslash, &$aalias, &$transp ) {
162 if ($subslash=='on') {
163 // WORKAROUND for backslashes
164 $tex = strtr($tex,'/','\\');
167 // ------- check parameters
168 $def = $this->getDefaultArguments();
170 if ($tex=='') { $tex = $this->helptext(); }
172 if ($magstep < 0 || $magstep > 5 ) { $magstep = $def["magstep"]; }
173 // calculate magnification factor
174 $magstep = floor(10*pow(1.2,$magstep))/10;
176 $aalias = $aalias != 'off';
177 $transp = $transp != 'off';
181 /* ------------------ image creation ------------------------------ */
183 function execute($cmd,$complainvisibly=false) {
184 exec($cmd, $errortxt, $returnval);
185 $ok = $returnval == 0;
188 if (!$complainvisibly) {
189 $this->dbg('Error during execution of '.$cmd );
191 while (list($key,$value)=each($errortxt)) {
192 if ($complainvisibly) {
193 $this->complain( $value."\n" );
195 $this->dbg( $value );
202 /* ---------------------------------------------------------------- */
204 function createTexFile($texfile,$texstr) {
205 if ($ok=($fp=fopen($texfile, 'w'))!=0 ) {
208 '\nopagenumbers' . "\n" .
209 '\hoffset=0cm' . "\n" .
210 '\voffset=0cm' . "\n" .
211 // '\hsize=20cm' . "\n" .
212 // '\vsize=10ex' . "\n" .
214 '\vfill\eject' . "\n" .
217 $ok = fwrite($fp, $texcommands);
218 $ok = fclose($fp) && $ok; // close anyway
221 $this->dbg('could not write .tex file: ' . $texstr);
226 /* ---------------------------------------------------------------- */
228 function TexToImg($texstr, $scale, $aalias, $transp) {
229 $cacheparams = $GLOBALS['CacheParams'];
230 $tempfiles = tempnam($cacheparams['cache_dir'],'TexToPng');
231 $img = 0; // $size = 0;
233 // procuce options for pstoimg
235 ($aalias ? '-aaliastext -color 8 ' : '-color 1 ') .
236 ($transp ? '-transparent ' : '') .
237 '-scale ' . $scale . ' ' .
238 '-type png -crop btlr -geometry 600x150 -margins 0,0';
240 // rely on intelligent bool interpretation
242 $this->createTexFile($tempfiles.'.tex',$texstr) &&
243 $this->execute('cd '.$cacheparams['cache_dir'].'; '.
244 "$texbin ".$tempfiles.'.tex',true) &&
245 $this->execute("$dvipsbin -o".$tempfiles.'.ps '.$tempfiles.'.dvi') &&
246 $this->execute("$pstoimgbin $options"
247 .' -out '.$tempfiles.'.png '.
248 $tempfiles.'.ps' ) &&
249 file_exists( $tempfiles.'.png' );
252 if (!($img = ImageCreateFromPNG( $tempfiles.'.png' ))) {
253 $this->dbg("Could not open just created image file: $tempfiles");
258 // clean up tmpdir; in debug mode only if no error occured
260 if ( !TexToPng_debug || (TexToPng_debug && $ok)) {
263 unlink($tempfiles.'.ps');
264 unlink($tempfiles.'.tex');
265 //unlink($tempfiles.'.aux');
266 unlink($tempfiles.'.dvi');
267 unlink($tempfiles.'.log');
268 unlink($tempfiles.'.png');
277 } // WikiPlugin_TexToPng
286 // c-hanging-comment-ender-p: nil
287 // indent-tabs-mode: nil