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