3 // render TeX stuff using latex - this will not work on all platforms
4 // or configurations. Only works on Linux and Mac with appropriate
6 // Much of this inspired/copied from Benjamin Zeiss' work
14 * Constructor - create temporary directories and build paths to
15 * external 'helper' binaries.
16 * Other platforms could/should be added
21 // construct directory structure
22 $this->temp_dir
= $CFG->dataroot
. "/temp/latex";
23 if (!file_exists("$CFG->dataroot/temp")) {
24 mkdir( "$CFG->dataroot/temp", $CFG->directorypermissions
);
26 if (!file_exists( $this->temp_dir
)) {
27 mkdir( $this->temp_dir
, $CFG->directorypermissions
);
33 * Accessor function for support_platform field.
34 * @return boolean value of supported_platform
36 function supported() {
37 return $this->supported_platform
;
41 * Turn the bit of TeX into a valid latex document
42 * @param string $forumula the TeX formula
43 * @param int $fontsize the font size
44 * @return string the latex document
46 function construct_latex_document( $formula, $fontsize=12 ) {
47 // $fontsize don't affects to formula's size. $density can change size
50 $doc = "\\documentclass[{$fontsize}pt]{article}\n";
51 $doc .= $CFG->filter_tex_latexpreamble
;
52 $doc .= "\\pagestyle{empty}\n";
53 $doc .= "\\begin{document}\n";
54 //dlnsk $doc .= "$ {$formula} $\n";
55 if (preg_match("/^[[:space:]]*\\\\begin\\{(gather|align|alignat|multline).?\\}/i",$formula)) {
58 $doc .= "$ {$formula} $\n";
60 $doc .= "\\end{document}\n";
65 * execute an external command, with optional logging
66 * @param string $command command to execute
67 * @param file $log valid open file handle - log info will be written to this file
68 * @return return code from execution of command
70 function execute( $command, $log=null ) {
72 exec( $command, $output, $return_code );
74 fwrite( $log, "COMMAND: $command \n" );
75 $outputs = implode( "\n", $output );
76 fwrite( $log, "OUTPUT: $outputs \n" );
77 fwrite( $log, "RETURN_CODE: $return_code\n " );
83 * Render TeX string into gif
84 * @param string $formula TeX formula
85 * @param string $filename base of filename for output (no extension)
86 * @param int $fontsize font size
87 * @param int $density density value for .ps to .gif conversion
88 * @param string $background background color (e.g, #FFFFFF).
89 * @param file $log valid open file handle for optional logging (debugging only)
90 * @return bool true if successful
92 function render( $formula, $filename, $fontsize=12, $density=240, $background='', $log=null ) {
96 // quick check - will this work?
97 if (empty($CFG->filter_tex_pathlatex
)) {
101 $doc = $this->construct_latex_document( $formula, $fontsize );
103 // construct some file paths
104 $tex = "{$this->temp_dir}/$filename.tex";
105 $dvi = "{$this->temp_dir}/$filename.dvi";
106 $ps = "{$this->temp_dir}/$filename.ps";
107 $gif = "{$this->temp_dir}/$filename.gif";
109 // turn the latex doc into a .tex file in the temp area
110 $fh = fopen( $tex, 'w' );
114 // run latex on document
115 $command = "{$CFG->filter_tex_pathlatex} --interaction=nonstopmode $tex";
116 chdir( $this->temp_dir
);
117 if ($this->execute($command, $log)) { // It allways False on Windows
121 // run dvips (.dvi to .ps)
122 $command = "{$CFG->filter_tex_pathdvips} -E $dvi -o $ps";
123 if ($this->execute($command, $log )) {
127 // run convert on document (.ps to .gif)
129 $bg_opt = "-transparent \"$background\""; // Makes transparent background
133 $command = "{$CFG->filter_tex_pathconvert} -density $density -trim $bg_opt $ps $gif";
134 if ($this->execute($command, $log )) {
142 * Delete files created in temporary area
143 * Don't forget to copy the final gif before calling this
144 * @param string $filename file base (no extension)
146 function clean_up( $filename ) {
147 unlink( "{$this->temp_dir}/$filename.tex" );
148 unlink( "{$this->temp_dir}/$filename.dvi" );
149 unlink( "{$this->temp_dir}/$filename.ps" );
150 unlink( "{$this->temp_dir}/$filename.gif" );
151 unlink( "{$this->temp_dir}/$filename.aux" );
152 unlink( "{$this->temp_dir}/$filename.log" );