|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // latex.php 00003 // render TeX stuff using latex - this will not work on all platforms 00004 // or configurations. Only works on Linux and Mac with appropriate 00005 // software installed. 00006 // Much of this inspired/copied from Benjamin Zeiss' work 00007 00008 class latex { 00009 00010 var $temp_dir; 00011 var $error; 00012 00018 function latex() { 00019 global $CFG; 00020 00021 // construct directory structure 00022 $this->temp_dir = $CFG->tempdir . "/latex"; 00023 make_temp_directory('latex'); 00024 } 00025 00030 function supported() { 00031 return $this->supported_platform; 00032 } 00033 00040 function construct_latex_document( $formula, $fontsize=12 ) { 00041 global $CFG; 00042 00043 $formula = filter_tex_sanitize_formula($formula); 00044 00045 // $fontsize don't affects to formula's size. $density can change size 00046 $doc = "\\documentclass[{$fontsize}pt]{article}\n"; 00047 $doc .= $CFG->filter_tex_latexpreamble; 00048 $doc .= "\\pagestyle{empty}\n"; 00049 $doc .= "\\begin{document}\n"; 00050 //dlnsk $doc .= "$ {$formula} $\n"; 00051 if (preg_match("/^[[:space:]]*\\\\begin\\{(gather|align|alignat|multline).?\\}/i",$formula)) { 00052 $doc .= "$formula\n"; 00053 } else { 00054 $doc .= "$ {$formula} $\n"; 00055 } 00056 $doc .= "\\end{document}\n"; 00057 return $doc; 00058 } 00059 00066 function execute( $command, $log=null ) { 00067 $output = array(); 00068 exec( $command, $output, $return_code ); 00069 if ($log) { 00070 fwrite( $log, "COMMAND: $command \n" ); 00071 $outputs = implode( "\n", $output ); 00072 fwrite( $log, "OUTPUT: $outputs \n" ); 00073 fwrite( $log, "RETURN_CODE: $return_code\n " ); 00074 } 00075 return $return_code; 00076 } 00077 00088 function render( $formula, $filename, $fontsize=12, $density=240, $background='', $log=null ) { 00089 00090 global $CFG; 00091 00092 // quick check - will this work? 00093 if (empty($CFG->filter_tex_pathlatex)) { 00094 return false; 00095 } 00096 00097 $doc = $this->construct_latex_document( $formula, $fontsize ); 00098 00099 // construct some file paths 00100 $tex = "{$this->temp_dir}/$filename.tex"; 00101 $dvi = "{$this->temp_dir}/$filename.dvi"; 00102 $ps = "{$this->temp_dir}/$filename.ps"; 00103 $img = "{$this->temp_dir}/$filename.{$CFG->filter_tex_convertformat}"; 00104 00105 // turn the latex doc into a .tex file in the temp area 00106 $fh = fopen( $tex, 'w' ); 00107 fputs( $fh, $doc ); 00108 fclose( $fh ); 00109 00110 // run latex on document 00111 $command = "{$CFG->filter_tex_pathlatex} --interaction=nonstopmode $tex"; 00112 chdir( $this->temp_dir ); 00113 if ($this->execute($command, $log)) { // It allways False on Windows 00114 // return false; 00115 } 00116 00117 // run dvips (.dvi to .ps) 00118 $command = "{$CFG->filter_tex_pathdvips} -E $dvi -o $ps"; 00119 if ($this->execute($command, $log )) { 00120 return false; 00121 } 00122 00123 // run convert on document (.ps to .gif/.png) 00124 if ($background) { 00125 $bg_opt = "-transparent \"$background\""; // Makes transparent background 00126 } else { 00127 $bg_opt = ""; 00128 } 00129 $command = "{$CFG->filter_tex_pathconvert} -density $density -trim $bg_opt $ps $img"; 00130 if ($this->execute($command, $log )) { 00131 return false; 00132 } 00133 00134 return $img; 00135 } 00136 00142 function clean_up( $filename ) { 00143 global $CFG; 00144 00145 unlink( "{$this->temp_dir}/$filename.tex" ); 00146 unlink( "{$this->temp_dir}/$filename.dvi" ); 00147 unlink( "{$this->temp_dir}/$filename.ps" ); 00148 unlink( "{$this->temp_dir}/$filename.{$CFG->filter_tex_convertformat}" ); 00149 unlink( "{$this->temp_dir}/$filename.aux" ); 00150 unlink( "{$this->temp_dir}/$filename.log" ); 00151 return; 00152 } 00153 00154 } 00155 00156 00157