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')) { define('TexToPng_debug', false); }
80 if (!defined('TexToPng_antialias')) { define('TexToPng_antialias', false); }
81 if (!defined('TexToPng_transparent')) { define('TexToPng_transparent', false); }
83 /*-----------------------------------------------------------------------
85 *----------------------------------------------------------------------*/
87 require_once 'lib/WikiPluginCached.php';
89 class WikiPlugin_TexToPng extends WikiPluginCached
91 function getPluginType() {
92 return PLUGIN_CACHED_IMG_ONDEMAND;
99 function getDescription() {
100 return _("Converts TeX to an image. May be used to embed formulas in PhpWiki.");
103 function getDefaultArguments() {
104 return array('tex' => "",
105 'magstep' => TexToPng_magstep,
108 'antialias' => TexToPng_antialias ? 'on' : 'off',
109 'transparent' => TexToPng_transparent ? 'on' : 'off',
113 function getImage($dbi, $argarray, $request) {
115 $this->checkParams($tex, $magstep, $subslash, $antialias, $transparent);
116 return $this->TexToImg($tex, $magstep, $antialias, $transparent);
119 function getExpire($dbi, $argarray, $request) {
123 function getImageType($dbi, $argarray, $request) {
128 function getAlt($dbi, $argarray, $request) {
133 function embedImg($url,$dbi,$argarray,$request) {
134 $html = HTML::img( array(
136 'alt' => htmlspecialchars($this->getAlt($dbi,$argarray,$request))
138 if ($argarray['center']=='on')
139 return HTML::div( array('style' => 'text-align:center;'), $html);
143 /* -------------------- error handling ---------------------------- */
145 function dbg( $out ) {
146 // test if verbose debug info is selected
147 if (TexToPng_debug) {
148 $this->complain( $out."\n" );
150 if (!$this->_errortext) {
151 // yeah, I've been told to be quiet, but obviously
152 // an error occured. So at least complain silently.
153 $this->complain(' ');
159 /* -------------------- parameter handling ------------------------ */
161 function helptext() {
162 $aa= TexToPng_antialias ?'on(default)$|$off':'on$|$off(default)';
163 $tp= TexToPng_transparent?'on(default)$|$off':'on$|$off(default)';
165 '/settabs/+/indent&$<$?plugin /bf{Tex} & [{/tt transparent}] & = "png(default)$|$jpeg$|$gif"& /cr'."\n".
166 '/+&$<$?plugin /bf{TexToPng} & /hfill {/tt tex} & = "/TeX/ commands"& /cr'."\n".
167 '/+& & /hfill [{/tt img}] & = "png(default)$|$jpeg$|$gif"& /cr'."\n".
168 '/+& & /hfill [{/tt magstep}] & = "0 to 5 ('.TexToPng_magstep.' default)"& /cr'."\n".
169 '/+& & /hfill [{/tt center}] & = "on$|$off(default)"& /cr'."\n".
170 '/+& & /hfill [{/tt subslash}] & = "on$|$off(default)"& /cr'."\n".
171 '/+& & /hfill [{/tt antialias}] & = "'.$aa.'"& /cr'."\n".
172 '/+& & /hfill [{/tt transparent}] & = "'.$tp.'"&?$>$ /cr'."\n";
174 return strtr($help, '/', '\\' );
178 function checkParams( &$tex, &$magstep, $subslash, &$aalias, &$transp ) {
180 if ($subslash=='on') {
181 // WORKAROUND for backslashes
182 $tex = strtr($tex,'/','\\');
185 // ------- check parameters
186 $def = $this->getDefaultArguments();
188 if ($tex=='') { $tex = $this->helptext(); }
190 if ($magstep < 0 || $magstep > 5 ) { $magstep = $def["magstep"]; }
191 // calculate magnification factor
192 $magstep = floor(10*pow(1.2,$magstep))/10;
194 $aalias = $aalias != 'off';
195 $transp = $transp != 'off';
199 /* ------------------ image creation ------------------------------ */
201 function execute($cmd,$complainvisibly=false) {
202 exec($cmd, $errortxt, $returnval);
203 $ok = $returnval == 0;
206 if (!$complainvisibly) {
207 $this->dbg('Error during execution of '.$cmd );
209 while (list($key,$value)=each($errortxt)) {
210 if ($complainvisibly) {
211 $this->complain( $value."\n" );
213 $this->dbg( $value );
220 /* ---------------------------------------------------------------- */
222 function createTexFile($texfile,$texstr) {
223 if ($ok=($fp=fopen($texfile, 'w'))!=0 ) {
226 '\nopagenumbers' . "\n" .
227 '\hoffset=0cm' . "\n" .
228 '\voffset=0cm' . "\n" .
229 // '\hsize=20cm' . "\n" .
230 // '\vsize=10ex' . "\n" .
232 '\vfill\eject' . "\n" .
235 $ok = fwrite($fp, $texcommands);
236 $ok = fclose($fp) && $ok; // close anyway
239 $this->dbg('could not write .tex file: ' . $texstr);
244 /* ---------------------------------------------------------------- */
246 function TexToImg($texstr, $scale, $aalias, $transp) {
247 //$cacheparams = $GLOBALS['CacheParams'];
248 $tempfiles = $this->tempnam('TexToPng');
249 $img = 0; // $size = 0;
251 // procuce options for pstoimg
253 ($aalias ? '-aaliastext -color 8 ' : '-color 1 ') .
254 ($transp ? '-transparent ' : '') .
255 '-scale ' . $scale . ' ' .
256 '-type png -crop btlr -geometry 600x150 -margins 0,0';
258 // rely on intelligent bool interpretation
260 $this->createTexFile($tempfiles.'.tex',$texstr) &&
261 $this->execute('cd '.$cacheparams['cache_dir'].'; '.
262 "$texbin ".$tempfiles.'.tex',true) &&
263 $this->execute("$dvipsbin -o".$tempfiles.'.ps '.$tempfiles.'.dvi') &&
264 $this->execute("$pstoimgbin $options"
265 .' -out '.$tempfiles.'.png '.
266 $tempfiles.'.ps' ) &&
267 file_exists( $tempfiles.'.png' );
270 if (!($img = ImageCreateFromPNG( $tempfiles.'.png' ))) {
271 $this->dbg("Could not open just created image file: $tempfiles");
276 // clean up tmpdir; in debug mode only if no error occured
278 if ( !TexToPng_debug || (TexToPng_debug && $ok)) {
281 unlink($tempfiles . '.ps');
282 unlink($tempfiles . '.tex');
283 //unlink($tempfiles . '.aux');
284 unlink($tempfiles . '.dvi');
285 unlink($tempfiles . '.log');
286 unlink($tempfiles . '.png');
301 // c-hanging-comment-ender-p: nil
302 // indent-tabs-mode: nil