4 * Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
5 * Copyright (C) 2002 Johannes Große
7 * This file is part of PhpWiki.
9 * PhpWiki is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * PhpWiki is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with PhpWiki; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 // +---------------------------------------------------------------------+
26 // +---------------------------------------------------------------------+
27 // | This is a WikiPlugin that surrounds tex commands given as parameter |
28 // | with a page description and renders it using several existing |
29 // | engines into a gif, png or jpeg file. |
30 // | TexToPng is usage example for WikiPluginCached. |
32 // | You may copy this code freely under the conditions of the GPL |
33 // +---------------------------------------------------------------------+
35 /*-----------------------------------------------------------------------
37 *----------------------------------------------------------------------*/
38 // needs (la)tex, dvips, gs, netpbm, libpng
39 // LaTeX2HTML ftp://ftp.dante.de/tex-archive/support/latex2html
40 $texbin = '/usr/bin/tex';
41 $dvipsbin = '/usr/bin/dvips';
42 $pstoimgbin = '/usr/bin/pstoimg';
44 // output mere debug messages (should be set to false in a stable
46 define('TexToPng_debug', false);
48 /*-----------------------------------------------------------------------
50 *----------------------------------------------------------------------*/
52 | use antialias for rendering;
53 | anitalias: blurs, _looks better_, needs twice space, renders slowlier
55 define('TexToPng_antialias', true);
58 | Use transparent background; dont combine with antialias on a dark
59 | background. Seems to have a bug: produces strange effects for some
60 | ps-files (almost non readable,blurred output) even when directly
61 | invoked from shell. So its probably a pstoimg bug.
63 define('TexToPng_transparent', false);
66 | default value for rescaling
67 | allowed range: 0 - 5 (integer)
69 define('TexToPng_magstep', 3);
72 /*-----------------------------------------------------------------------
76 *----------------------------------------------------------------------*/
78 // check boolean constants
80 if (!defined('TexToPng_debug')) { define('TexToPng_debug', false); }
81 if (!defined('TexToPng_antialias')) { define('TexToPng_antialias', false); }
82 if (!defined('TexToPng_transparent')) { define('TexToPng_transparent', false); }
84 /*-----------------------------------------------------------------------
86 *----------------------------------------------------------------------*/
88 require_once "lib/WikiPluginCached.php";
90 class WikiPlugin_TexToPng extends WikiPluginCached
92 function getPluginType() {
93 return PLUGIN_CACHED_IMG_ONDEMAND;
100 function getDescription() {
101 return _("Converts TeX to an image. May be used to embed formulas in PhpWiki.");
104 function getDefaultArguments() {
105 return array('tex' => "",
106 'magstep' => TexToPng_magstep,
109 'antialias' => TexToPng_antialias ? 'on' : 'off',
110 'transparent' => TexToPng_transparent ? 'on' : 'off',
114 function getImage($dbi, $argarray, $request) {
116 $this->checkParams($tex, $magstep, $subslash, $antialias, $transparent);
117 return $this->TexToImg($tex, $magstep, $antialias, $transparent);
120 function getExpire($dbi, $argarray, $request) {
124 function getImageType($dbi, $argarray, $request) {
129 function getAlt($dbi, $argarray, $request) {
134 function embedImg($url,$dbi,$argarray,$request) {
135 $html = HTML::img( array(
137 'alt' => htmlspecialchars($this->getAlt($dbi,$argarray,$request))
139 if ($argarray['center']=='on')
140 return HTML::div( array('style' => 'text-align:center;'), $html);
144 /* -------------------- error handling ---------------------------- */
146 function dbg( $out ) {
147 // test if verbose debug info is selected
148 if (TexToPng_debug) {
149 $this->complain( $out."\n" );
151 if (!$this->_errortext) {
152 // yeah, I've been told to be quiet, but obviously
153 // an error occured. So at least complain silently.
154 $this->complain(' ');
160 /* -------------------- parameter handling ------------------------ */
162 function helptext() {
163 $aa= TexToPng_antialias ?'on(default)$|$off':'on$|$off(default)';
164 $tp= TexToPng_transparent?'on(default)$|$off':'on$|$off(default)';
166 '/settabs/+/indent&$<$?plugin /bf{Tex} & [{/tt transparent}] & = "png(default)$|$jpeg$|$gif"& /cr'."\n".
167 '/+&$<$?plugin /bf{TexToPng} & /hfill {/tt tex} & = "/TeX/ commands"& /cr'."\n".
168 '/+& & /hfill [{/tt img}] & = "png(default)$|$jpeg$|$gif"& /cr'."\n".
169 '/+& & /hfill [{/tt magstep}] & = "0 to 5 ('.TexToPng_magstep.' default)"& /cr'."\n".
170 '/+& & /hfill [{/tt center}] & = "on$|$off(default)"& /cr'."\n".
171 '/+& & /hfill [{/tt subslash}] & = "on$|$off(default)"& /cr'."\n".
172 '/+& & /hfill [{/tt antialias}] & = "'.$aa.'"& /cr'."\n".
173 '/+& & /hfill [{/tt transparent}] & = "'.$tp.'"&?$>$ /cr'."\n";
175 return strtr($help, '/', '\\' );
179 function checkParams( &$tex, &$magstep, $subslash, &$aalias, &$transp ) {
181 if ($subslash=='on') {
182 // WORKAROUND for backslashes
183 $tex = strtr($tex,'/','\\');
186 // ------- check parameters
187 $def = $this->getDefaultArguments();
189 if ($tex=='') { $tex = $this->helptext(); }
191 if ($magstep < 0 || $magstep > 5 ) { $magstep = $def["magstep"]; }
192 // calculate magnification factor
193 $magstep = floor(10*pow(1.2,$magstep))/10;
195 $aalias = $aalias != 'off';
196 $transp = $transp != 'off';
200 /* ------------------ image creation ------------------------------ */
202 function execute($cmd,$complainvisibly=false) {
203 exec($cmd, $errortxt, $returnval);
204 $ok = $returnval == 0;
207 if (!$complainvisibly) {
208 $this->dbg('Error during execution of '.$cmd );
210 while (list($key,$value)=each($errortxt)) {
211 if ($complainvisibly) {
212 $this->complain( $value."\n" );
214 $this->dbg( $value );
221 /* ---------------------------------------------------------------- */
223 function createTexFile($texfile,$texstr) {
224 if ($ok=($fp=fopen($texfile, 'w'))!=0 ) {
227 '\nopagenumbers' . "\n" .
228 '\hoffset=0cm' . "\n" .
229 '\voffset=0cm' . "\n" .
230 // '\hsize=20cm' . "\n" .
231 // '\vsize=10ex' . "\n" .
233 '\vfill\eject' . "\n" .
236 $ok = fwrite($fp, $texcommands);
237 $ok = fclose($fp) && $ok; // close anyway
240 $this->dbg('could not write .tex file: ' . $texstr);
245 /* ---------------------------------------------------------------- */
247 function TexToImg($texstr, $scale, $aalias, $transp) {
248 //$cacheparams = $GLOBALS['CacheParams'];
249 $tempfiles = $this->tempnam('TexToPng');
250 $img = 0; // $size = 0;
252 // procuce options for pstoimg
254 ($aalias ? '-aaliastext -color 8 ' : '-color 1 ') .
255 ($transp ? '-transparent ' : '') .
256 '-scale ' . $scale . ' ' .
257 '-type png -crop btlr -geometry 600x150 -margins 0,0';
259 // rely on intelligent bool interpretation
261 $this->createTexFile($tempfiles.'.tex',$texstr) &&
262 $this->execute('cd '.$cacheparams['cache_dir'].'; '.
263 "$texbin ".$tempfiles.'.tex',true) &&
264 $this->execute("$dvipsbin -o".$tempfiles.'.ps '.$tempfiles.'.dvi') &&
265 $this->execute("$pstoimgbin $options"
266 .' -out '.$tempfiles.'.png '.
267 $tempfiles.'.ps' ) &&
268 file_exists( $tempfiles.'.png' );
271 if (!($img = ImageCreateFromPNG( $tempfiles.'.png' ))) {
272 $this->dbg("Could not open just created image file: $tempfiles");
277 // clean up tmpdir; in debug mode only if no error occured
279 if ( !TexToPng_debug || (TexToPng_debug && $ok)) {
282 unlink($tempfiles . '.ps');
283 unlink($tempfiles . '.tex');
284 //unlink($tempfiles . '.aux');
285 unlink($tempfiles . '.dvi');
286 unlink($tempfiles . '.log');
287 unlink($tempfiles . '.png');
302 // c-hanging-comment-ender-p: nil
303 // indent-tabs-mode: nil