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