]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/TeX2png.php
return
[SourceForge/phpwiki.git] / lib / plugin / TeX2png.php
1 <?php
2
3 /*
4  * Copyright (C) Copyright 2004 Pierrick Meignen
5  *
6  * This file is part of PhpWiki.
7  *
8  * PhpWiki is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * PhpWiki is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 /**
24  * This is a simple version of the original TexToPng plugin which uses
25  * the powerful plugincached mechanism.
26  * TeX2png uses its own much simplier static cache in images/tex.
27  *
28  * @author: Pierrick Meignen
29  * TODO: use url helpers, windows fixes
30  *       use a better imagepath
31  */
32
33 // needs latex
34 // LaTeX2HTML ftp://ftp.dante.de/tex-archive/support/latex2html
35
36 class WikiPlugin_TeX2png
37     extends WikiPlugin
38 {
39     var $imagepath = 'images/tex';
40     var $latexbin = '/usr/bin/latex';
41     var $dvipsbin = '/usr/bin/dvips';
42     var $pstoimgbin = '/usr/bin/pstoimg';
43
44     function getName()
45     {
46         return _("TeX2png");
47     }
48
49     function getDescription()
50     {
51         return _("Convert Tex mathematicals expressions to cached PNG files. This is for small text.");
52     }
53
54     function getDefaultArguments()
55     {
56         return array('text' => "$$(a + b)^2 = a^2 + 2 ab + b^2$$");
57     }
58
59     function parseArgStr($argstr)
60     {
61         // modified from WikiPlugin.php
62         $arg_p = '\w+';
63         $op_p = '(?:\|\|)?=';
64         $word_p = '\S+';
65         $opt_ws = '\s*';
66         $qq_p = '" ( (?:[^"\\\\]|\\\\.)* ) "';
67         //"<--kludge for brain-dead syntax coloring
68         $q_p = "' ( (?:[^'\\\\]|\\\\.)* ) '";
69         $gt_p = "_\\( $opt_ws $qq_p $opt_ws \\)";
70         $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws (?: $qq_p|$q_p|$gt_p|($word_p))";
71
72         $args = array();
73         $defaults = array();
74
75         while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
76             @ list(, $arg, $op, $qq_val, $q_val, $gt_val, $word_val) = $m;
77             $argstr = substr($argstr, strlen($m[0]));
78
79             // Remove quotes from string values.
80             if ($qq_val)
81                 // we don't remove backslashes in tex formulas
82                 // $val = stripslashes($qq_val);
83                 $val = $qq_val;
84             elseif ($q_val)
85                 $val = stripslashes($q_val); elseif ($gt_val)
86                 $val = _(stripslashes($gt_val)); else
87                 $val = $word_val;
88
89             if ($op == '=') {
90                 $args[$arg] = $val;
91             } else {
92                 // NOTE: This does work for multiple args. Use the
93                 // separator character defined in your webserver
94                 // configuration, usually & or &amp; (See
95                 // http://www.htmlhelp.com/faq/cgifaq.4.html)
96                 // e.g. <plugin RecentChanges days||=1 show_all||=0 show_minor||=0>
97                 // url: RecentChanges?days=1&show_all=1&show_minor=0
98                 assert($op == '||=');
99                 $defaults[$arg] = $val;
100             }
101         }
102
103         if ($argstr) {
104             $this->handle_plugin_args_cruft($argstr, $args);
105         }
106
107         return array($args, $defaults);
108     }
109
110     function createTexFile($texfile, $text)
111     {
112         // this is the small latex file
113         // which contains only the mathematical
114         // expression
115         $fp = fopen($texfile, 'w');
116         $str = "\documentclass{article}\n";
117         $str .= "\usepackage{amsfonts}\n";
118         $str .= "\usepackage{amssymb}\n";
119         // Here tou can add some package in order
120         // to produce more sophisticated output
121         $str .= "\pagestyle{empty}\n";
122         $str .= "\begin{document}\n";
123         $str .= $text . "\n";
124         $str .= "\end{document}";
125         fwrite($fp, $str);
126         fclose($fp);
127         return 0;
128     }
129
130     function createPngFile($imagepath, $imagename)
131     {
132         // to create dvi file from the latex file
133         $commandes = $this->latexbin . " temp.tex;";
134         exec("cd $imagepath;$commandes");
135         // to create png file from the dvi file
136         // there is no option but it is possible
137         // to add one (scale for example)
138         if (file_exists("$imagepath/temp.dvi")) {
139             $commandes = $this->dvipsbin . " temp.dvi -o temp.ps;";
140             $commandes .= $this->pstoimgbin . " -type png -margins 0,0 ";
141             $commandes .= "-crop a -geometry 600x300 ";
142             $commandes .= "-aaliastext -color 1 -scale 1.5 ";
143             $commandes .= "temp.ps -o " . $imagename;
144             exec("cd $imagepath;$commandes");
145             unlink("$imagepath/temp.dvi");
146             unlink("$imagepath/temp.ps");
147         } else
148             echo _(" (syntax error for latex) ");
149         // to clean the directory
150         unlink("$imagepath/temp.tex");
151         unlink("$imagepath/temp.aux");
152         unlink("$imagepath/temp.log");
153         return 0;
154     }
155
156     function isMathExp(&$text)
157     {
158         // this function returns
159         // 0 : text is too long or not a mathematical expression
160         // 1 : text is $xxxxxx$ hence in line
161         // 2 : text is $$xxxx$$ hence centered
162         $last = strlen($text) - 1;
163         if ($last >= 250) {
164             $text = "Too long !";
165             return 0;
166         } elseif ($last <= 1 || strpos($text, '$') != 0) {
167             return 0;
168         } elseif (strpos($text, '$', 1) == $last)
169             return 1; elseif ($last > 3 &&
170             strpos($text, '$', 1) == 1 &&
171             strpos($text, '$', 2) == $last - 1
172         )
173             return 2;
174         return 0;
175     }
176
177     function tex2png($text)
178     {
179         // the name of the png cached file
180         $imagename = md5($text) . ".png";
181         $url = $this->imagepath . "/$imagename";
182
183         if (!file_exists($url)) {
184             if (is_writable($this->imagepath)) {
185                 $texfile = $this->imagepath . "/temp.tex";
186                 $this->createTexFile($texfile, $text);
187                 $this->createPngFile($this->imagepath, $imagename);
188             } else {
189                 $error_html = _("TeX imagepath not writable.");
190                 trigger_error($error_html, E_USER_NOTICE);
191             }
192         }
193
194         // there is always something in the html page
195         // even if the tex directory doesn't exist
196         // or mathematical expression is wrong
197         switch ($this->isMathExp($text)) {
198             case 0: // not a mathematical expression
199                 $html = HTML::tt(array('class' => 'tex',
200                     'style' => 'color:red;'), $text);
201                 break;
202             case 1: // an inlined mathematical expression
203                 $html = HTML::img(array('class' => 'tex',
204                     'src' => $url,
205                     'alt' => $text));
206                 break;
207             case 2: // mathematical expression on separate line
208                 $html = HTML::img(array('class' => 'tex',
209                     'src' => $url,
210                     'alt' => $text));
211                 $html = HTML::div(array('align' => 'center'), $html);
212                 break;
213             default:
214                 break;
215         }
216
217         return $html;
218     }
219
220     function run($dbi, $argstr, &$request, $basepage)
221     {
222         // from text2png.php
223         if ((function_exists('ImageTypes') and (ImageTypes() & IMG_PNG))
224             or function_exists("ImagePNG")
225         ) {
226             // we have gd & png so go ahead.
227             extract($this->getArgs($argstr, $request));
228             return $this->tex2png($text);
229         } else {
230             // we don't have png and/or gd.
231             $error_html = _("Sorry, this version of PHP cannot create PNG image files.");
232             $link = "http://www.php.net/manual/pl/ref.image.php";
233             $error_html .= sprintf(_("See %s"), $link) . ".";
234             trigger_error($error_html, E_USER_NOTICE);
235             return HTML::p($error_html);
236         }
237     }
238 }
239
240 // Local Variables:
241 // mode: php
242 // tab-width: 8
243 // c-basic-offset: 4
244 // c-hanging-comment-ender-p: nil
245 // indent-tabs-mode: nil
246 // End: