]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/TeX2png.php
Remove extra empty lines
[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." .
52             " This is for small text");
53     }
54
55     function getDefaultArguments()
56     {
57         return array('text' => "$$(a + b)^2 = a^2 + 2 ab + b^2$$");
58     }
59
60     function parseArgStr($argstr)
61     {
62         // modified from WikiPlugin.php
63         $arg_p = '\w+';
64         $op_p = '(?:\|\|)?=';
65         $word_p = '\S+';
66         $opt_ws = '\s*';
67         $qq_p = '" ( (?:[^"\\\\]|\\\\.)* ) "';
68         //"<--kludge for brain-dead syntax coloring
69         $q_p = "' ( (?:[^'\\\\]|\\\\.)* ) '";
70         $gt_p = "_\\( $opt_ws $qq_p $opt_ws \\)";
71         $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws (?: $qq_p|$q_p|$gt_p|($word_p))";
72
73         $args = array();
74         $defaults = array();
75
76         while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
77             @ list(, $arg, $op, $qq_val, $q_val, $gt_val, $word_val) = $m;
78             $argstr = substr($argstr, strlen($m[0]));
79
80             // Remove quotes from string values.
81             if ($qq_val)
82                 // we don't remove backslashes in tex formulas
83                 // $val = stripslashes($qq_val);
84                 $val = $qq_val;
85             elseif ($q_val)
86                 $val = stripslashes($q_val); elseif ($gt_val)
87                 $val = _(stripslashes($gt_val)); else
88                 $val = $word_val;
89
90             if ($op == '=') {
91                 $args[$arg] = $val;
92             } else {
93                 // NOTE: This does work for multiple args. Use the
94                 // separator character defined in your webserver
95                 // configuration, usually & or &amp; (See
96                 // http://www.htmlhelp.com/faq/cgifaq.4.html)
97                 // e.g. <plugin RecentChanges days||=1 show_all||=0 show_minor||=0>
98                 // url: RecentChanges?days=1&show_all=1&show_minor=0
99                 assert($op == '||=');
100                 $defaults[$arg] = $val;
101             }
102         }
103
104         if ($argstr) {
105             $this->handle_plugin_args_cruft($argstr, $args);
106         }
107
108         return array($args, $defaults);
109     }
110
111     function createTexFile($texfile, $text)
112     {
113         // this is the small latex file
114         // which contains only the mathematical
115         // expression
116         $fp = fopen($texfile, 'w');
117         $str = "\documentclass{article}\n";
118         $str .= "\usepackage{amsfonts}\n";
119         $str .= "\usepackage{amssymb}\n";
120         // Here tou can add some package in order
121         // to produce more sophisticated output
122         $str .= "\pagestyle{empty}\n";
123         $str .= "\begin{document}\n";
124         $str .= $text . "\n";
125         $str .= "\end{document}";
126         fwrite($fp, $str);
127         fclose($fp);
128         return 0;
129     }
130
131     function createPngFile($imagepath, $imagename)
132     {
133         // to create dvi file from the latex file
134         $commandes = $this->latexbin . " temp.tex;";
135         exec("cd $imagepath;$commandes");
136         // to create png file from the dvi file
137         // there is no option but it is possible
138         // to add one (scale for example)
139         if (file_exists("$imagepath/temp.dvi")) {
140             $commandes = $this->dvipsbin . " temp.dvi -o temp.ps;";
141             $commandes .= $this->pstoimgbin . " -type png -margins 0,0 ";
142             $commandes .= "-crop a -geometry 600x300 ";
143             $commandes .= "-aaliastext -color 1 -scale 1.5 ";
144             $commandes .= "temp.ps -o " . $imagename;
145             exec("cd $imagepath;$commandes");
146             unlink("$imagepath/temp.dvi");
147             unlink("$imagepath/temp.ps");
148         } else
149             echo _(" (syntax error for latex) ");
150         // to clean the directory
151         unlink("$imagepath/temp.tex");
152         unlink("$imagepath/temp.aux");
153         unlink("$imagepath/temp.log");
154         return 0;
155     }
156
157     function isMathExp(&$text)
158     {
159         // this function returns
160         // 0 : text is too long or not a mathematical expression
161         // 1 : text is $xxxxxx$ hence in line
162         // 2 : text is $$xxxx$$ hence centered
163         $last = strlen($text) - 1;
164         if ($last >= 250) {
165             $text = "Too long !";
166             return 0;
167         } elseif ($last <= 1 || strpos($text, '$') != 0) {
168             return 0;
169         } elseif (strpos($text, '$', 1) == $last)
170             return 1; elseif ($last > 3 &&
171             strpos($text, '$', 1) == 1 &&
172             strpos($text, '$', 2) == $last - 1
173         )
174             return 2;
175         return 0;
176     }
177
178     function tex2png($text)
179     {
180         // the name of the png cached file
181         $imagename = md5($text) . ".png";
182         $url = $this->imagepath . "/$imagename";
183
184         if (!file_exists($url)) {
185             if (is_writable($this->imagepath)) {
186                 $texfile = $this->imagepath . "/temp.tex";
187                 $this->createTexFile($texfile, $text);
188                 $this->createPngFile($this->imagepath, $imagename);
189             } else {
190                 $error_html = _("TeX imagepath not writable.");
191                 trigger_error($error_html, E_USER_NOTICE);
192             }
193         }
194
195         // there is always something in the html page
196         // even if the tex directory doesn't exist
197         // or mathematical expression is wrong
198         switch ($this->isMathExp($text)) {
199             case 0: // not a mathematical expression
200                 $html = HTML::tt(array('class' => 'tex',
201                     'style' => 'color:red;'), $text);
202                 break;
203             case 1: // an inlined mathematical expression
204                 $html = HTML::img(array('class' => 'tex',
205                     'src' => $url,
206                     'alt' => $text));
207                 break;
208             case 2: // mathematical expression on separate line
209                 $html = HTML::img(array('class' => 'tex',
210                     'src' => $url,
211                     'alt' => $text));
212                 $html = HTML::div(array('align' => 'center'), $html);
213                 break;
214             default:
215                 break;
216         }
217
218         return $html;
219     }
220
221     function run($dbi, $argstr, &$request, $basepage)
222     {
223         // from text2png.php
224         if ((function_exists('ImageTypes') and (ImageTypes() & IMG_PNG))
225             or function_exists("ImagePNG")
226         ) {
227             // we have gd & png so go ahead.
228             extract($this->getArgs($argstr, $request));
229             return $this->tex2png($text);
230         } else {
231             // we don't have png and/or gd.
232             $error_html = _("Sorry, this version of PHP cannot create PNG image files.");
233             $link = "http://www.php.net/manual/pl/ref.image.php";
234             $error_html .= sprintf(_("See %s"), $link) . ".";
235             trigger_error($error_html, E_USER_NOTICE);
236             return;
237         }
238     }
239 }
240
241 // Local Variables:
242 // mode: php
243 // tab-width: 8
244 // c-basic-offset: 4
245 // c-hanging-comment-ender-p: nil
246 // indent-tabs-mode: nil
247 // End: