2 rcs_id('$Id: TeX2png.php,v 1.1 2004-06-13 09:35:51 rurban Exp $');
4 Copyright 2004 Pierrick Meignen
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.
12 * @author: Pierrick Meignen
13 * TODO: use url helpers, windows fixes
14 * use a better imagepath
18 // LaTeX2HTML ftp://ftp.dante.de/tex-archive/support/latex2html
20 class WikiPlugin_TeX2png
23 var $imagepath = "images/tex";
24 var $latexbin = '/usr/bin/latex';
25 var $dvipsbin = '/usr/bin/dvips';
26 var $pstoimgbin = '/usr/bin/pstoimg';
32 function getDescription () {
33 return _("Convert Tex mathematicals expressions to cached png files." .
34 " This is for small text");
37 function getVersion() {
38 return preg_replace("/[Revision: $]/", '',"\$Revision: 1.1 $");
41 function getDefaultArguments() {
42 return array('text' => "$$(a + b)^2 = a^2 + 2 ab + b^2$$");
45 function parseArgStr($argstr) {
46 // modified from WikiPlugin.php
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))";
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]));
64 // Remove quotes from string values.
66 // we don't remove backslashes in tex formulas
67 // $val = stripslashes($qq_val);
70 $val = stripslashes($q_val);
72 $val = _(stripslashes($gt_val));
80 // NOTE: This does work for multiple args. Use the
81 // separator character defined in your webserver
82 // configuration, usually & or & (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
87 $defaults[$arg] = $val;
92 $this->handle_plugin_args_cruft($argstr, $args);
95 return array($args, $defaults);
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}";
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");
127 function isMathExp($text) {
128 $last = strlen($text) - 1;
129 if($text[0] != "$" || $text[$last] != "$")
131 else if ($text[1] == "$" && $text[$last - 1] == "$")
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);
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);
147 if (!file_exists($this->imagepath)) {
148 trigger_error(sprintf("Failed to mkdir '%s'.", $this->imagepath), E_USER_WARNING);
152 $imagename = md5($text) . ".png";
153 $url = $this->imagepath . "/" . $imagename;
155 if(!file_exists($url)){
156 $texfile = $this->imagepath . "/temp.tex";
157 $this->createTexFile($texfile, $text);
158 $this->createPngFile($this->imagepath, $imagename);
162 switch($this->isMathExp($text)) {
164 $html = HTML::tt(array('class'=>'tex', 'style'=>'color:red;'),
168 $html = HTML::img(array('class'=>'tex', 'src' => $url, 'alt' => $text));
171 $html = HTML::img(array('class'=>'tex', 'src' => $url, 'alt' => $text));
172 $html = HTML::div(array('align' => 'center'), $html);
181 function run($dbi, $argstr, &$request, $basepage) {
183 if (ImageTypes() & IMG_PNG) {
184 // we have gd & png so go ahead.
185 extract($this->getArgs($argstr, $request));
186 return $this->tex2png($text);
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);