]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/TeX2png.php
Simple version of cached TexToPng, by Pierrick Meignen
[SourceForge/phpwiki.git] / lib / plugin / TeX2png.php
1 <?php // -*-php-*-
2 rcs_id('$Id: TeX2png.php,v 1.1 2004-06-13 09:35:51 rurban Exp $');
3 /*
4  Copyright 2004 Pierrick Meignen
5 */
6
7 /**
8  * This is a simple version of the original TexToPng plugin which uses 
9  * the powerful plugincached mechanism.
10  * TeX2png uses its own much simplier static cache in images/tex.
11  *
12  * @author: Pierrick Meignen
13  * TODO: use url helpers, windows fixes
14  *       use a better imagepath
15  */
16
17 // needs latex
18 // LaTeX2HTML ftp://ftp.dante.de/tex-archive/support/latex2html
19
20 class WikiPlugin_TeX2png
21 extends WikiPlugin
22 {
23     var $imagepath = "images/tex";
24     var $latexbin = '/usr/bin/latex';
25     var $dvipsbin = '/usr/bin/dvips';
26     var $pstoimgbin = '/usr/bin/pstoimg';
27
28     function getName () {
29         return _("TeX2png");
30     }
31     
32     function getDescription () {
33         return _("Convert Tex mathematicals expressions to cached png files." .
34                  " This is for small text");
35     }
36     
37     function getVersion() {
38         return preg_replace("/[Revision: $]/", '',"\$Revision: 1.1 $");
39     }
40     
41     function getDefaultArguments() {
42         return array('text' => "$$(a + b)^2 = a^2 + 2 ab + b^2$$");
43     }
44     
45     function parseArgStr($argstr) {
46         // modified from WikiPlugin.php
47         $arg_p = '\w+';
48         $op_p = '(?:\|\|)?=';
49         $word_p = '\S+';
50         $opt_ws = '\s*';
51         $qq_p = '" ( (?:[^"\\\\]|\\\\.)* ) "';
52         //"<--kludge for brain-dead syntax coloring
53         $q_p  = "' ( (?:[^'\\\\]|\\\\.)* ) '";
54         $gt_p = "_\\( $opt_ws $qq_p $opt_ws \\)";
55         $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws (?: $qq_p|$q_p|$gt_p|($word_p))";
56     
57         $args = array();
58         $defaults = array();
59     
60         while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) {
61             @ list(,$arg,$op,$qq_val,$q_val,$gt_val,$word_val) = $m;
62             $argstr = substr($argstr, strlen($m[0]));
63       
64             // Remove quotes from string values.
65             if ($qq_val)
66                 // we don't remove backslashes in tex formulas
67                 // $val = stripslashes($qq_val);
68                 $val = $qq_val;
69             elseif ($q_val)
70                 $val = stripslashes($q_val);
71             elseif ($gt_val)
72                 $val = _(stripslashes($gt_val));
73             else
74                 $val = $word_val;
75             
76             if ($op == '=') {
77                 $args[$arg] = $val;
78             }
79             else {
80                 // NOTE: This does work for multiple args. Use the
81                 // separator character defined in your webserver
82                 // configuration, usually & or &amp; (See
83                 // http://www.htmlhelp.com/faq/cgifaq.4.html)
84                 // e.g. <plugin RecentChanges days||=1 show_all||=0 show_minor||=0>
85                 // url: RecentChanges?days=1&show_all=1&show_minor=0
86                 assert($op == '||=');
87                 $defaults[$arg] = $val;
88             }
89         }
90     
91         if ($argstr) {
92             $this->handle_plugin_args_cruft($argstr, $args);
93         }
94     
95         return array($args, $defaults);
96     }
97
98     function createTexFile($texfile, $text) {
99          $fp = fopen($texfile, 'w');
100          $str = "\documentclass{article}\n";
101          $str .= "\usepackage{amsfonts}\n";
102          $str .= "\usepackage{amssymb}\n";
103          $str .= "\pagestyle{empty}\n";
104          $str .= "\begin{document}\n";
105          $str .= $text . "\n";
106          $str .= "\end{document}";
107          fwrite($fp, $str);
108          fclose($fp);
109          return 0;
110     }
111
112     function createPngFile($imagepath, $imagename) {
113          $commandes = "$latexbin temp.tex; $dvipsbin temp.dvi -o temp.ps;";
114          $commandes .= "$pstoimgbin -type png -margins 0,0 -crop a -geometry 600x300";
115          $commandes .= " -aaliastext -color 1";
116          $options = " -scale 1.5 ";
117          $commandes .= $commandes . $options . "temp.ps -o " . $imagename;
118          exec("cd $imagepath; $commandes");
119          unlink("$imagepath/temp.dvi");
120          unlink("$imagepath/temp.tex");
121          unlink("$imagepath/temp.aux");
122          unlink("$imagepath/temp.log");
123          unlink("$imagepath/temp.ps");
124          return 0;
125     }
126
127     function isMathExp($text) {
128       $last = strlen($text) - 1;
129       if($text[0] != "$" || $text[$last] != "$")
130         return 0;
131       else if ($text[1] == "$" && $text[$last - 1] == "$")
132         return 2;
133       return 1;
134     }
135
136     function tex2png($text) {
137         if($this->isMathExp($text) == 0){       
138             $error_html =_("Sorry, not a full mathematical expression: " . $text);
139             trigger_error($error_html, E_USER_NOTICE);
140         } else {
141             if (!file_exists($this->imagepath)) {
142                 $oldumask = umask(0);
143                 // permissions affected by user the www server is running as
144                 mkdir($this->imagepath, 0777);
145                 umask($oldumask);
146             }
147             if (!file_exists($this->imagepath)) {
148                 trigger_error(sprintf("Failed to mkdir '%s'.", $this->imagepath), E_USER_WARNING);
149                 return '';
150             }
151
152             $imagename = md5($text) . ".png";
153             $url = $this->imagepath . "/" . $imagename;
154     
155             if(!file_exists($url)){
156                 $texfile = $this->imagepath . "/temp.tex";
157                 $this->createTexFile($texfile, $text);     
158                 $this->createPngFile($this->imagepath, $imagename);
159             } 
160         }
161
162         switch($this->isMathExp($text)) {
163         case 0: 
164             $html = HTML::tt(array('class'=>'tex', 'style'=>'color:red;'),
165                              $text); 
166             break;
167         case 1: 
168             $html = HTML::img(array('class'=>'tex', 'src' => $url, 'alt' => $text)); 
169             break;
170         case 2: 
171             $html = HTML::img(array('class'=>'tex', 'src' => $url, 'alt' => $text));
172             $html = HTML::div(array('align' => 'center'), $html); 
173             break;
174         default: 
175             break;
176         }
177
178         return $html;
179     }
180
181     function run($dbi, $argstr, &$request, $basepage) {
182         // from text2png.php
183         if (ImageTypes() & IMG_PNG) {
184             // we have gd & png so go ahead.
185             extract($this->getArgs($argstr, $request));
186             return $this->tex2png($text);
187         } else {
188             // we don't have png and/or gd.
189             $error_html = _("Sorry, this version of PHP cannot create PNG image files.");
190             $link = "http://www.php.net/manual/pl/ref.image.php";
191             $error_html .= sprintf(_("See %s"), $link) .".";
192             trigger_error($error_html, E_USER_NOTICE);
193             return;
194         }
195     }
196 };
197 ?>