[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
1 <?php 2 // latex.php 3 // render TeX stuff using latex - this will not work on all platforms 4 // or configurations. Only works on Linux and Mac with appropriate 5 // software installed. 6 // Much of this inspired/copied from Benjamin Zeiss' work 7 8 class latex { 9 10 var $temp_dir; 11 var $error; 12 13 /** 14 * Constructor - create temporary directories and build paths to 15 * external 'helper' binaries. 16 * Other platforms could/should be added 17 */ 18 function latex() { 19 global $CFG; 20 21 // construct directory structure 22 $this->temp_dir = $CFG->tempdir . "/latex"; 23 make_temp_directory('latex'); 24 } 25 26 /** 27 * Accessor function for support_platform field. 28 * @return boolean value of supported_platform 29 */ 30 function supported() { 31 return $this->supported_platform; 32 } 33 34 /** 35 * Turn the bit of TeX into a valid latex document 36 * @param string $forumula the TeX formula 37 * @param int $fontsize the font size 38 * @return string the latex document 39 */ 40 function construct_latex_document( $formula, $fontsize=12 ) { 41 global $CFG; 42 43 $formula = filter_tex_sanitize_formula($formula); 44 45 // $fontsize don't affects to formula's size. $density can change size 46 $doc = "\\documentclass[{$fontsize}pt]{article}\n"; 47 $doc .= get_config('filter_tex', 'latexpreamble'); 48 $doc .= "\\pagestyle{empty}\n"; 49 $doc .= "\\begin{document}\n"; 50 //dlnsk $doc .= "$ {$formula} $\n"; 51 if (preg_match("/^[[:space:]]*\\\\begin\\{(gather|align|alignat|multline).?\\}/i",$formula)) { 52 $doc .= "$formula\n"; 53 } else { 54 $doc .= "$ {$formula} $\n"; 55 } 56 $doc .= "\\end{document}\n"; 57 return $doc; 58 } 59 60 /** 61 * execute an external command, with optional logging 62 * @param string $command command to execute 63 * @param file $log valid open file handle - log info will be written to this file 64 * @return return code from execution of command 65 */ 66 function execute( $command, $log=null ) { 67 $output = array(); 68 exec( $command, $output, $return_code ); 69 if ($log) { 70 fwrite( $log, "COMMAND: $command \n" ); 71 $outputs = implode( "\n", $output ); 72 fwrite( $log, "OUTPUT: $outputs \n" ); 73 fwrite( $log, "RETURN_CODE: $return_code\n " ); 74 } 75 return $return_code; 76 } 77 78 /** 79 * Render TeX string into gif/png 80 * @param string $formula TeX formula 81 * @param string $filename filename for output (including extension) 82 * @param int $fontsize font size 83 * @param int $density density value for .ps to .gif/.png conversion 84 * @param string $background background color (e.g, #FFFFFF). 85 * @param file $log valid open file handle for optional logging (debugging only) 86 * @return bool true if successful 87 */ 88 function render( $formula, $filename, $fontsize=12, $density=240, $background='', $log=null ) { 89 90 global $CFG; 91 92 // quick check - will this work? 93 $pathlatex = get_config('filter_tex', 'pathlatex'); 94 if (empty($pathlatex)) { 95 return false; 96 } 97 $pathlatex = escapeshellarg(trim($pathlatex, " '\"")); 98 99 $doc = $this->construct_latex_document( $formula, $fontsize ); 100 101 // construct some file paths 102 $convertformat = get_config('filter_tex', 'convertformat'); 103 if (!strpos($filename, ".{$convertformat}")) { 104 $convertformat = 'png'; 105 } 106 $filename = str_replace(".{$convertformat}", '', $filename); 107 $tex = "{$this->temp_dir}/$filename.tex"; 108 $dvi = "{$this->temp_dir}/$filename.dvi"; 109 $ps = "{$this->temp_dir}/$filename.ps"; 110 $img = "{$this->temp_dir}/$filename.{$convertformat}"; 111 112 // turn the latex doc into a .tex file in the temp area 113 $fh = fopen( $tex, 'w' ); 114 fputs( $fh, $doc ); 115 fclose( $fh ); 116 117 // run latex on document 118 $command = "$pathlatex --interaction=nonstopmode --halt-on-error $tex"; 119 chdir( $this->temp_dir ); 120 if ($this->execute($command, $log)) { // It allways False on Windows 121 // return false; 122 } 123 124 // run dvips (.dvi to .ps) 125 $pathdvips = escapeshellarg(trim(get_config('filter_tex', 'pathdvips'), " '\"")); 126 $command = "$pathdvips -E $dvi -o $ps"; 127 if ($this->execute($command, $log )) { 128 return false; 129 } 130 131 // Run convert on document (.ps to .gif/.png) or run dvisvgm (.ps to .svg). 132 if ($background) { 133 $bg_opt = "-transparent \"$background\""; // Makes transparent background 134 } else { 135 $bg_opt = ""; 136 } 137 if ($convertformat == 'svg') { 138 $pathdvisvgm = escapeshellarg(trim(get_config('filter_tex', 'pathdvisvgm'), " '\"")); 139 $command = "$pathdvisvgm -E $ps -o $img"; 140 } else { 141 $pathconvert = escapeshellarg(trim(get_config('filter_tex', 'pathconvert'), " '\"")); 142 $command = "$pathconvert -density $density -trim $bg_opt $ps $img"; 143 } 144 if ($this->execute($command, $log )) { 145 return false; 146 } 147 148 return $img; 149 } 150 151 /** 152 * Delete files created in temporary area 153 * Don't forget to copy the final gif/png before calling this 154 * @param string $filename file base (no extension) 155 */ 156 function clean_up( $filename ) { 157 global $CFG; 158 159 unlink( "{$this->temp_dir}/$filename.tex" ); 160 unlink( "{$this->temp_dir}/$filename.dvi" ); 161 unlink( "{$this->temp_dir}/$filename.ps" ); 162 $convertformat = get_config('filter_tex', 'convertformat'); 163 unlink( "{$this->temp_dir}/$filename.{$convertformat}" ); 164 unlink( "{$this->temp_dir}/$filename.aux" ); 165 unlink( "{$this->temp_dir}/$filename.log" ); 166 return; 167 } 168 169 } 170 171 172
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |