]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/TexToPng.php
access-restrictions are not implemented
[SourceForge/phpwiki.git] / lib / plugin / TexToPng.php
1 <?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')) {
80     define('TexToPng_debug', false);
81 }
82 if (!defined('TexToPng_antialias')) {
83     define('TexToPng_antialias', false);
84 }
85 if (!defined('TexToPng_transparent')) {
86     define('TexToPng_transparent', false);
87 }
88
89 /*-----------------------------------------------------------------------
90  | WikiPlugin_TexToPng
91  *----------------------------------------------------------------------*/
92
93 require_once 'lib/WikiPluginCached.php';
94
95 class WikiPlugin_TexToPng extends WikiPluginCached
96 {
97     function getPluginType()
98     {
99         return PLUGIN_CACHED_IMG_ONDEMAND;
100     }
101
102     function getName()
103     {
104         return "TexToPng";
105     }
106
107     function getDescription()
108     {
109         return _("Converts TeX to an image. May be used to embed formulas in PhpWiki.");
110     }
111
112     function getDefaultArguments()
113     {
114         return array('tex' => "",
115             'magstep' => TexToPng_magstep,
116             'img' => 'png',
117             'subslash' => 'off',
118             'antialias' => TexToPng_antialias ? 'on' : 'off',
119             'transparent' => TexToPng_transparent ? 'on' : 'off',
120             'center' => 'off');
121     }
122
123     function getImage($dbi, $argarray, $request)
124     {
125         extract($argarray);
126         $this->checkParams($tex, $magstep, $subslash, $antialias, $transparent);
127         return $this->TexToImg($tex, $magstep, $antialias, $transparent);
128     } // run
129
130     function getExpire($dbi, $argarray, $request)
131     {
132         return '0';
133     }
134
135     function getImageType($dbi, $argarray, $request)
136     {
137         extract($argarray);
138         return $img;
139     }
140
141     function getAlt($dbi, $argarray, $request)
142     {
143         extract($argarray);
144         return $tex;
145     }
146
147     function embedImg($url, $dbi, $argarray, $request)
148     {
149         $html = HTML::img(array(
150             'src' => $url,
151             'alt' => htmlspecialchars($this->getAlt($dbi, $argarray, $request))
152         ));
153         if ($argarray['center'] == 'on')
154             return HTML::div(array('style' => 'text-align:center;'), $html);
155         return $html;
156     }
157
158     /* -------------------- error handling ---------------------------- */
159
160     function dbg($out)
161     {
162         // test if verbose debug info is selected
163         if (TexToPng_debug) {
164             $this->complain($out . "\n");
165         } else {
166             if (!$this->_errortext) {
167                 // yeah, I've been told to be quiet, but obviously
168                 // an error occured. So at least complain silently.
169                 $this->complain(' ');
170             }
171         }
172
173     } // dbg
174
175     /* -------------------- parameter handling ------------------------ */
176
177     function helptext()
178     {
179         $aa = TexToPng_antialias ? 'on(default)$|$off' : 'on$|$off(default)';
180         $tp = TexToPng_transparent ? 'on(default)$|$off' : 'on$|$off(default)';
181         $help =
182             '/settabs/+/indent&$<$?plugin /bf{Tex} & [{/tt transparent}] & = "png(default)$|$jpeg$|$gif"& /cr' . "\n" .
183                 '/+&$<$?plugin /bf{TexToPng} & /hfill {/tt tex}           & = "/TeX/  commands"& /cr' . "\n" .
184                 '/+&                         & /hfill [{/tt img}]         & = "png(default)$|$jpeg$|$gif"& /cr' . "\n" .
185                 '/+&                         & /hfill [{/tt magstep}]     & = "0 to 5 (' . TexToPng_magstep . ' default)"& /cr' . "\n" .
186                 '/+&                         & /hfill [{/tt center}]      & = "on$|$off(default)"& /cr' . "\n" .
187                 '/+&                         & /hfill [{/tt subslash}]    & = "on$|$off(default)"& /cr' . "\n" .
188                 '/+&                         & /hfill [{/tt antialias}]   & = "' . $aa . '"& /cr' . "\n" .
189                 '/+&                         & /hfill [{/tt transparent}] & = "' . $tp . '"&?$>$ /cr' . "\n";
190
191         return strtr($help, '/', '\\');
192     } // helptext
193
194     function checkParams(&$tex, &$magstep, $subslash, &$aalias, &$transp)
195     {
196
197         if ($subslash == 'on') {
198             // WORKAROUND for backslashes
199             $tex = strtr($tex, '/', '\\');
200         }
201
202         // ------- check parameters
203         $def = $this->getDefaultArguments();
204
205         if ($tex == '') {
206             $tex = $this->helptext();
207         }
208
209         if ($magstep < 0 || $magstep > 5) {
210             $magstep = $def["magstep"];
211         }
212         // calculate magnification factor
213         $magstep = floor(10 * pow(1.2, $magstep)) / 10;
214
215         $aalias = $aalias != 'off';
216         $transp = $transp != 'off';
217
218     } // checkParams
219
220     /* ------------------ image creation ------------------------------ */
221
222     function execute($cmd, $complainvisibly = false)
223     {
224         exec($cmd, $errortxt, $returnval);
225         $ok = $returnval == 0;
226
227         if (!$ok) {
228             if (!$complainvisibly) {
229                 $this->dbg('Error during execution of ' . $cmd);
230             }
231             ;
232             while (list($key, $value) = each($errortxt)) {
233                 if ($complainvisibly) {
234                     $this->complain($value . "\n");
235                 } else {
236                     $this->dbg($value);
237                 }
238             }
239         }
240         return $ok;
241     } // execute
242
243     /* ---------------------------------------------------------------- */
244
245     function createTexFile($texfile, $texstr)
246     {
247         if ($ok = ($fp = fopen($texfile, 'w')) != 0) {
248             // prepare .tex file
249             $texcommands =
250                 '\nopagenumbers' . "\n" .
251                     '\hoffset=0cm' . "\n" .
252                     '\voffset=0cm' . "\n" .
253                     //    '\hsize=20cm'    . "\n" .
254                     //    '\vsize=10ex'    . "\n" .
255                     $texstr . "\n" .
256                     '\vfill\eject' . "\n" .
257                     '\end' . "\n\n";
258
259             $ok = fwrite($fp, $texcommands);
260             $ok = fclose($fp) && $ok; // close anyway
261         }
262         if (!$ok) {
263             $this->dbg('could not write .tex file: ' . $texstr);
264         }
265         return $ok;
266     } // createTexFile
267
268     /* ---------------------------------------------------------------- */
269
270     function TexToImg($texstr, $scale, $aalias, $transp)
271     {
272         //$cacheparams = $GLOBALS['CacheParams'];
273         $tempfiles = $this->tempnam('TexToPng');
274         $img = 0; // $size = 0;
275
276         // procuce options for pstoimg
277         $options =
278             ($aalias ? '-aaliastext -color 8 ' : '-color 1 ') .
279                 ($transp ? '-transparent ' : '') .
280                 '-scale ' . $scale . ' ' .
281                 '-type png -crop btlr -geometry 600x150 -margins 0,0';
282
283         // rely on intelligent bool interpretation
284         $ok = $tempfiles &&
285             $this->createTexFile($tempfiles . '.tex', $texstr) &&
286             $this->execute('cd ' . $cacheparams['cache_dir'] . '; ' .
287                 "$texbin " . $tempfiles . '.tex', true) &&
288             $this->execute("$dvipsbin -o" . $tempfiles . '.ps ' . $tempfiles . '.dvi') &&
289             $this->execute("$pstoimgbin $options"
290                 . ' -out ' . $tempfiles . '.png ' .
291                 $tempfiles . '.ps') &&
292             file_exists($tempfiles . '.png');
293
294         if ($ok) {
295             if (!($img = ImageCreateFromPNG($tempfiles . '.png'))) {
296                 $this->dbg("Could not open just created image file: $tempfiles");
297                 $ok = false;
298             }
299         }
300
301         // clean up tmpdir; in debug mode only if no error occured
302
303         if (!TexToPng_debug || (TexToPng_debug && $ok)) {
304             if ($tempfiles) {
305                 unlink($tempfiles);
306                 unlink($tempfiles . '.ps');
307                 unlink($tempfiles . '.tex');
308                 //unlink($tempfiles . '.aux');
309                 unlink($tempfiles . '.dvi');
310                 unlink($tempfiles . '.log');
311                 unlink($tempfiles . '.png');
312             }
313         }
314
315         if ($ok) {
316             return $img;
317         }
318         return false;
319     } // TexToImg
320 }
321
322 // Local Variables:
323 // mode: php
324 // tab-width: 8
325 // c-basic-offset: 4
326 // c-hanging-comment-ender-p: nil
327 // indent-tabs-mode: nil
328 // End: