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 along
20 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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);
71 /*-----------------------------------------------------------------------
75 *----------------------------------------------------------------------*/
77 // check boolean constants
79 if (!defined('TexToPng_debug')) {
80 define('TexToPng_debug', false);
82 if (!defined('TexToPng_antialias')) {
83 define('TexToPng_antialias', false);
85 if (!defined('TexToPng_transparent')) {
86 define('TexToPng_transparent', false);
89 /*-----------------------------------------------------------------------
91 *----------------------------------------------------------------------*/
93 require_once 'lib/WikiPluginCached.php';
95 class WikiPlugin_TexToPng extends WikiPluginCached
97 function getPluginType()
99 return PLUGIN_CACHED_IMG_ONDEMAND;
102 function getDescription()
104 return _("Converts TeX to an image. May be used to embed formulas in PhpWiki.");
107 function getDefaultArguments()
109 return array('tex' => "",
110 'magstep' => TexToPng_magstep,
113 'antialias' => TexToPng_antialias ? 'on' : 'off',
114 'transparent' => TexToPng_transparent ? 'on' : 'off',
118 function getImage($dbi, $argarray, $request)
121 $this->checkParams($tex, $magstep, $subslash, $antialias, $transparent);
122 return $this->TexToImg($tex, $magstep, $antialias, $transparent);
125 function getExpire($dbi, $argarray, $request)
130 function getImageType($dbi, $argarray, $request)
136 function getAlt($dbi, $argarray, $request)
142 function embedImg($url, $dbi, $argarray, $request)
144 $html = HTML::img(array(
146 'alt' => htmlspecialchars($this->getAlt($dbi, $argarray, $request))
148 if ($argarray['center'] == 'on')
149 return HTML::div(array('style' => 'text-align:center;'), $html);
153 /* -------------------- error handling ---------------------------- */
157 // test if verbose debug info is selected
158 if (TexToPng_debug) {
159 $this->complain($out . "\n");
161 if (!$this->_errortext) {
162 // yeah, I've been told to be quiet, but obviously
163 // an error occured. So at least complain silently.
164 $this->complain(' ');
170 /* -------------------- parameter handling ------------------------ */
174 $aa = TexToPng_antialias ? 'on(default)$|$off' : 'on$|$off(default)';
175 $tp = TexToPng_transparent ? 'on(default)$|$off' : 'on$|$off(default)';
177 '/settabs/+/indent&$<$?plugin /bf{Tex} & [{/tt transparent}] & = "png(default)$|$jpeg$|$gif"& /cr' . "\n" .
178 '/+&$<$?plugin /bf{TexToPng} & /hfill {/tt tex} & = "/TeX/ commands"& /cr' . "\n" .
179 '/+& & /hfill [{/tt img}] & = "png(default)$|$jpeg$|$gif"& /cr' . "\n" .
180 '/+& & /hfill [{/tt magstep}] & = "0 to 5 (' . TexToPng_magstep . ' default)"& /cr' . "\n" .
181 '/+& & /hfill [{/tt center}] & = "on$|$off(default)"& /cr' . "\n" .
182 '/+& & /hfill [{/tt subslash}] & = "on$|$off(default)"& /cr' . "\n" .
183 '/+& & /hfill [{/tt antialias}] & = "' . $aa . '"& /cr' . "\n" .
184 '/+& & /hfill [{/tt transparent}] & = "' . $tp . '"&?$>$ /cr' . "\n";
186 return strtr($help, '/', '\\');
189 function checkParams(&$tex, &$magstep, $subslash, &$aalias, &$transp)
192 if ($subslash == 'on') {
193 // WORKAROUND for backslashes
194 $tex = strtr($tex, '/', '\\');
197 // ------- check parameters
198 $def = $this->getDefaultArguments();
201 $tex = $this->helptext();
204 if ($magstep < 0 || $magstep > 5) {
205 $magstep = $def["magstep"];
207 // calculate magnification factor
208 $magstep = floor(10 * pow(1.2, $magstep)) / 10;
210 $aalias = $aalias != 'off';
211 $transp = $transp != 'off';
215 /* ------------------ image creation ------------------------------ */
217 function execute($cmd, $complainvisibly = false)
219 exec($cmd, $errortxt, $returnval);
220 $ok = $returnval == 0;
223 if (!$complainvisibly) {
224 $this->dbg('Error during execution of ' . $cmd);
227 while (list($key, $value) = each($errortxt)) {
228 if ($complainvisibly) {
229 $this->complain($value . "\n");
238 /* ---------------------------------------------------------------- */
240 function createTexFile($texfile, $texstr)
242 if ($ok = ($fp = fopen($texfile, 'w')) != 0) {
245 '\nopagenumbers' . "\n" .
246 '\hoffset=0cm' . "\n" .
247 '\voffset=0cm' . "\n" .
248 // '\hsize=20cm' . "\n" .
249 // '\vsize=10ex' . "\n" .
251 '\vfill\eject' . "\n" .
254 $ok = fwrite($fp, $texcommands);
255 $ok = fclose($fp) && $ok; // close anyway
258 $this->dbg('could not write .tex file: ' . $texstr);
263 /* ---------------------------------------------------------------- */
265 function TexToImg($texstr, $scale, $aalias, $transp)
267 //$cacheparams = $GLOBALS['CacheParams'];
268 $tempfiles = $this->tempnam('TexToPng');
269 $img = 0; // $size = 0;
271 // procuce options for pstoimg
273 ($aalias ? '-aaliastext -color 8 ' : '-color 1 ') .
274 ($transp ? '-transparent ' : '') .
275 '-scale ' . $scale . ' ' .
276 '-type png -crop btlr -geometry 600x150 -margins 0,0';
278 // rely on intelligent bool interpretation
280 $this->createTexFile($tempfiles . '.tex', $texstr) &&
281 $this->execute('cd ' . $cacheparams['cache_dir'] . '; ' .
282 "$texbin " . $tempfiles . '.tex', true) &&
283 $this->execute("$dvipsbin -o" . $tempfiles . '.ps ' . $tempfiles . '.dvi') &&
284 $this->execute("$pstoimgbin $options"
285 . ' -out ' . $tempfiles . '.png ' .
286 $tempfiles . '.ps') &&
287 file_exists($tempfiles . '.png');
290 if (!($img = ImageCreateFromPNG($tempfiles . '.png'))) {
291 $this->dbg("Could not open just created image file: $tempfiles");
296 // clean up tmpdir; in debug mode only if no error occured
298 if (!TexToPng_debug || (TexToPng_debug && $ok)) {
301 unlink($tempfiles . '.ps');
302 unlink($tempfiles . '.tex');
303 //unlink($tempfiles . '.aux');
304 unlink($tempfiles . '.dvi');
305 unlink($tempfiles . '.log');
306 unlink($tempfiles . '.png');
321 // c-hanging-comment-ender-p: nil
322 // indent-tabs-mode: nil