]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/TexToPng.php
include [all] Include and file path should be devided with single space. File path...
[SourceForge/phpwiki.git] / lib / plugin / TexToPng.php
1 <?php // -*-php-*-
2
3 /**
4  * Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
5  * Copyright (C) 2002 Johannes Große
6  *
7  * This file is part of PhpWiki.
8  *
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.
13  *
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.
18  *
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.
22  */
23
24 // +---------------------------------------------------------------------+
25 // | TexToPng.php                                                        |
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.                     |
31 // |                                                                     |
32 // | You may copy this code freely under the conditions of the GPL       |
33 // +---------------------------------------------------------------------+
34
35 /*-----------------------------------------------------------------------
36  | CONFIGURATION
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';
43
44 // output mere debug messages (should be set to false in a stable
45 // version)
46    define('TexToPng_debug', false);
47
48 /*-----------------------------------------------------------------------
49  | OPTION DEFAULTS
50  *----------------------------------------------------------------------*/
51 /*----
52  | use antialias for rendering;
53  | anitalias: blurs, _looks better_, needs twice space, renders slowlier
54  |                                                                      */
55    define('TexToPng_antialias', true);
56
57 /*----
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.
62  |                                                                      */
63    define('TexToPng_transparent', false);
64
65 /*----
66  | default value for rescaling
67  | allowed range: 0 - 5 (integer)
68  |                                                                      */
69    define('TexToPng_magstep', 3);
70
71 /*-----------------------------------------------------------------------
72  |
73  |  Source
74  |
75  *----------------------------------------------------------------------*/
76
77 // check boolean constants
78
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); }
82
83 /*-----------------------------------------------------------------------
84  | WikiPlugin_TexToPng
85  *----------------------------------------------------------------------*/
86
87 require_once 'lib/WikiPluginCached.php';
88
89 class WikiPlugin_TexToPng extends WikiPluginCached
90 {
91     function getPluginType() {
92         return PLUGIN_CACHED_IMG_ONDEMAND;
93     }
94
95     function getName() {
96         return "TexToPng";
97     }
98
99     function getDescription() {
100         return _("Converts TeX to an image. May be used to embed formulas in PhpWiki.");
101     }
102
103     function getDefaultArguments() {
104         return array('tex'          => "",
105                      'magstep'      => TexToPng_magstep,
106                      'img'          => 'png',
107                      'subslash'     => 'off',
108                      'antialias'    => TexToPng_antialias   ? 'on' : 'off',
109                      'transparent'  => TexToPng_transparent ? 'on' : 'off',
110                      'center'       => 'off');
111     }
112
113     function getImage($dbi, $argarray, $request) {
114         extract($argarray);
115         $this->checkParams($tex, $magstep, $subslash, $antialias, $transparent);
116         return $this->TexToImg($tex, $magstep, $antialias, $transparent);
117     } // run
118
119     function getExpire($dbi, $argarray, $request) {
120         return '0';
121     }
122
123     function getImageType($dbi, $argarray, $request) {
124         extract($argarray);
125         return $img;
126     }
127
128     function getAlt($dbi, $argarray, $request) {
129         extract($argarray);
130         return $tex;
131     }
132
133     function embedImg($url,$dbi,$argarray,$request) {
134         $html = HTML::img( array(
135             'src'   => $url,
136             'alt'   => htmlspecialchars($this->getAlt($dbi,$argarray,$request))
137             ));
138         if ($argarray['center']=='on')
139             return HTML::div( array('style' => 'text-align:center;'), $html);
140         return $html;
141     }
142
143     /* -------------------- error handling ---------------------------- */
144
145     function dbg( $out ) {
146         // test if verbose debug info is selected
147         if (TexToPng_debug) {
148             $this->complain( $out."\n" );
149         } else {
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(' ');
154             }
155         }
156
157     } // dbg
158
159     /* -------------------- parameter handling ------------------------ */
160
161     function helptext() {
162         $aa= TexToPng_antialias  ?'on(default)$|$off':'on$|$off(default)';
163         $tp= TexToPng_transparent?'on(default)$|$off':'on$|$off(default)';
164         $help =
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";
173
174         return strtr($help, '/', '\\' );
175     } // helptext
176
177
178     function checkParams( &$tex, &$magstep, $subslash, &$aalias, &$transp ) {
179
180         if ($subslash=='on') {
181             // WORKAROUND for backslashes
182             $tex = strtr($tex,'/','\\');
183         }
184
185         // ------- check parameters
186         $def = $this->getDefaultArguments();
187
188         if ($tex=='') { $tex = $this->helptext(); }
189
190         if ($magstep < 0 || $magstep > 5 ) { $magstep = $def["magstep"]; }
191         // calculate magnification factor
192         $magstep = floor(10*pow(1.2,$magstep))/10;
193
194         $aalias = $aalias != 'off';
195         $transp = $transp != 'off';
196
197     } // checkParams
198
199     /* ------------------ image creation ------------------------------ */
200
201     function execute($cmd,$complainvisibly=false) {
202         exec($cmd, $errortxt, $returnval);
203         $ok = $returnval == 0;
204
205         if (!$ok) {
206             if (!$complainvisibly) {
207                  $this->dbg('Error during execution of '.$cmd );
208             };
209             while (list($key,$value)=each($errortxt)) {
210                 if ($complainvisibly) {
211                     $this->complain( $value."\n" );
212                 } else {
213                     $this->dbg( $value );
214                 }
215             }
216         }
217         return $ok;
218     } // execute
219
220     /* ---------------------------------------------------------------- */
221
222     function createTexFile($texfile,$texstr) {
223         if ($ok=($fp=fopen($texfile, 'w'))!=0 ) {
224             // prepare .tex file
225             $texcommands =
226                 '\nopagenumbers'   . "\n" .
227                 '\hoffset=0cm'     . "\n" .
228                 '\voffset=0cm'     . "\n" .
229             //    '\hsize=20cm'    . "\n" .
230             //    '\vsize=10ex'    . "\n" .
231                 $texstr            . "\n" .
232                 '\vfill\eject'     . "\n" .
233                 '\end'             . "\n\n";
234
235             $ok = fwrite($fp, $texcommands);
236             $ok = fclose($fp) && $ok;  // close anyway
237         }
238         if (!$ok) {
239             $this->dbg('could not write .tex file: ' . $texstr);
240         }
241         return $ok;
242     } // createTexFile
243
244     /* ---------------------------------------------------------------- */
245
246     function TexToImg($texstr, $scale, $aalias, $transp) {
247         //$cacheparams = $GLOBALS['CacheParams'];
248         $tempfiles = $this->tempnam('TexToPng');
249         $img = 0; // $size = 0;
250
251         // procuce options for pstoimg
252         $options =
253            ($aalias ? '-aaliastext -color 8 ' : '-color 1 ') .
254            ($transp ? '-transparent ' : '') .
255            '-scale ' . $scale . ' ' .
256            '-type png -crop btlr -geometry 600x150 -margins 0,0';
257
258         // rely on intelligent bool interpretation
259         $ok= $tempfiles &&
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' );
268
269         if ($ok) {
270             if (!($img = ImageCreateFromPNG( $tempfiles.'.png' ))) {
271                 $this->dbg("Could not open just created image file: $tempfiles");
272                 $ok = false;
273             }
274         }
275
276         // clean up tmpdir; in debug mode only if no error occured
277
278         if ( !TexToPng_debug || (TexToPng_debug && $ok))  {
279             if ($tempfiles) {
280                 unlink($tempfiles);
281                 unlink($tempfiles . '.ps');
282                 unlink($tempfiles . '.tex');
283                 //unlink($tempfiles . '.aux');
284                 unlink($tempfiles . '.dvi');
285                 unlink($tempfiles . '.log');
286                 unlink($tempfiles . '.png');
287             }
288         }
289
290         if ($ok) {
291             return $img;
292         }
293         return false;
294     } // TexToImg
295 }
296
297 // Local Variables:
298 // mode: php
299 // tab-width: 8
300 // c-basic-offset: 4
301 // c-hanging-comment-ender-p: nil
302 // indent-tabs-mode: nil
303 // End: