Skip to content

Commit

Permalink
filter tex: MDL-10197 Quality improvement of TeX-images by using PNG …
Browse files Browse the repository at this point in the history
…format.

It seems some TeX installs use a convert utility that renders poor quality
images when using the GIF format, but render good ones when using PNG. If
all the needed tools are available, let the admin choose the preferred
output format.

Merged from MOODLE_19_STABLE
  • Loading branch information
iarenaza committed Apr 10, 2010
1 parent ba74f51 commit cf4e754
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 17 deletions.
2 changes: 1 addition & 1 deletion filter/tex/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function filter ($text) {
$texcache->timemodified = time();
$DB->insert_record("cache_filters", $texcache, false);
}
$filename = $md5 . ".gif";
$filename = $md5 . ".{$CFG->filter_tex_convertformat}";
$text = str_replace( $matches[0][$i], string_file_picture_tex($filename, $texexp, '', '', $align, $alt), $text);
}
return $text;
Expand Down
6 changes: 6 additions & 0 deletions filter/tex/filtersettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
$items[] = new admin_setting_configexecutable('filter_tex_pathdvips', get_string('pathdvips', 'admin'), '', $default_filter_tex_pathdvips);
$items[] = new admin_setting_configexecutable('filter_tex_pathconvert', get_string('pathconvert', 'admin'), '', $default_filter_tex_pathconvert);

// Even if we offer GIF and PNG formats here, in the update callback we check whether
// all the paths actually point to executables. If they don't, we force the setting
// to GIF, as that's the only format mimeTeX can produce.
$formats = array('gif' => 'GIF', 'png' => 'PNG');
$items[] = new admin_setting_configselect('filter_tex_convertformat', get_string('convertformat', 'admin'), get_string('configconvertformat', 'admin'), 'gif', $formats);

foreach ($items as $item) {
$item->set_updatedcallback('filter_tex_updatedcallback');
$settings->add($item);
Expand Down
18 changes: 10 additions & 8 deletions filter/tex/latex.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ function execute( $command, $log=null ) {
}

/**
* Render TeX string into gif
* Render TeX string into gif/png
* @param string $formula TeX formula
* @param string $filename base of filename for output (no extension)
* @param int $fontsize font size
* @param int $density density value for .ps to .gif conversion
* @param int $density density value for .ps to .gif/.png conversion
* @param string $background background color (e.g, #FFFFFF).
* @param file $log valid open file handle for optional logging (debugging only)
* @return bool true if successful
Expand All @@ -106,7 +106,7 @@ function render( $formula, $filename, $fontsize=12, $density=240, $background=''
$tex = "{$this->temp_dir}/$filename.tex";
$dvi = "{$this->temp_dir}/$filename.dvi";
$ps = "{$this->temp_dir}/$filename.ps";
$gif = "{$this->temp_dir}/$filename.gif";
$img = "{$this->temp_dir}/$filename.{$CFG->filter_tex_convertformat}";

// turn the latex doc into a .tex file in the temp area
$fh = fopen( $tex, 'w' );
Expand All @@ -126,30 +126,32 @@ function render( $formula, $filename, $fontsize=12, $density=240, $background=''
return false;
}

// run convert on document (.ps to .gif)
// run convert on document (.ps to .gif/.png)
if ($background) {
$bg_opt = "-transparent \"$background\""; // Makes transparent background
} else {
$bg_opt = "";
}
$command = "{$CFG->filter_tex_pathconvert} -density $density -trim $bg_opt $ps $gif";
$command = "{$CFG->filter_tex_pathconvert} -density $density -trim $bg_opt $ps $img";
if ($this->execute($command, $log )) {
return false;
}

return $gif;
return $img;
}

/**
* Delete files created in temporary area
* Don't forget to copy the final gif before calling this
* Don't forget to copy the final gif/png before calling this
* @param string $filename file base (no extension)
*/
function clean_up( $filename ) {
global $CFG;

unlink( "{$this->temp_dir}/$filename.tex" );
unlink( "{$this->temp_dir}/$filename.dvi" );
unlink( "{$this->temp_dir}/$filename.ps" );
unlink( "{$this->temp_dir}/$filename.gif" );
unlink( "{$this->temp_dir}/$filename.{$CFG->filter_tex_convertformat}" );
unlink( "{$this->temp_dir}/$filename.aux" );
unlink( "{$this->temp_dir}/$filename.log" );
return;
Expand Down
7 changes: 7 additions & 0 deletions filter/tex/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ function filter_tex_updatedcallback($name) {

$DB->delete_records('cache_filters', array('filter'=>'tex'));
$DB->delete_records('cache_filters', array('filter'=>'algebra'));

if (!(is_file($CFG->filter_tex_pathlatex) && is_executable($CFG->filter_tex_pathlatex) &&
is_file($CFG->filter_tex_pathdvips) && is_executable($CFG->filter_tex_pathdvips) &&
is_file($CFG->filter_tex_pathconvert) && is_executable($CFG->filter_tex_pathconvert))) {
// LaTeX, dvips or convert are not available, and mimetex can only produce GIFs so...
set_config('filter_tex_convertformat', 'gif');
}
}


2 changes: 1 addition & 1 deletion filter/tex/pix.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
}

if (!file_exists($pathname)) {
$md5 = str_replace('.gif','',$image);
$md5 = str_replace(".{$CFG->filter_tex_convertformat}",'',$image);
if ($texcache = $DB->get_record('cache_filters', array('filter'=>'tex', 'md5key'=>$md5))) {
if (!file_exists($CFG->dataroot.'/filter/tex')) {
make_upload_directory('filter/tex');
Expand Down
15 changes: 8 additions & 7 deletions filter/tex/texdebug.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ function tex2image($texexp, $return=false) {
return;
}

$texexp = stripslashes($texexp);
$image = md5($texexp) . ".gif";
$filetype = 'image/gif';
if (!file_exists("$CFG->dataroot/filter/tex")) {
Expand Down Expand Up @@ -211,7 +212,7 @@ function TexOutput($expression, $graphic=false) {
$tex = "$latex->temp_dir/$md5.tex";
$dvi = "$latex->temp_dir/$md5.dvi";
$ps = "$latex->temp_dir/$md5.ps";
$gif = "$latex->temp_dir/$md5.gif";
$img = "$latex->temp_dir/$md5.{$CFG->filter_tex_convertformat}";

// put the expression as a file into the temp area
$expression = html_entity_decode($expression);
Expand All @@ -233,13 +234,13 @@ function TexOutput($expression, $graphic=false) {
$output .= execute($cmd);

// step 3: convert command
$cmd = "$CFG->filter_tex_pathconvert -density 240 -trim $ps $gif ";
$cmd = "$CFG->filter_tex_pathconvert -density 240 -trim $ps $img ";
$output .= execute($cmd);

if (!$graphic) {
echo($output);
} else {
send_file($gif, "$md5.gif");
send_file($img, "$md5.{$CFG->filter_tex_convertformat}");
}
}

Expand Down Expand Up @@ -280,13 +281,13 @@ function slasharguments($texexp) {
target="inlineframe">
<center>
<input type="text" name="tex" size="50"
value="f(x)=\Bigint_{-\infty}^x~e^{-t^2}dt" />
value="f(x)=\int_{-\infty}^x~e^{-t^2}dt" />
</center>
<p>The following tests are available:</p>
<ol>
<li><input type="radio" name="action" value="ShowDB" id="ShowDB" />
<label for="ShowDB">See the cache_filters database entry for this expression (if any).</label></li>
<li><input type="radio" name="DeleteDB" value="DeleteDB" id="DeleteDB" />
<li><input type="radio" name="action" value="DeleteDB" id="DeleteDB" />
<label for="DeleteDB">Delete the cache_filters database entry for this expression (if any).</label></li>
<li><input type="radio" name="action" value="ShowImageMimetex" id="ShowImageMimetex" checked="checked" />
<label for="ShowImageMimetex">Show a graphic image of the algebraic expression rendered with mimetex.</label></li>
Expand All @@ -313,7 +314,7 @@ function slasharguments($texexp) {
processed before. If not, it adds a DB entry for that expression. It then
replaces the TeX expression by an &lt;img src=&quot;.../filter/tex/pix.php...&quot;&gt;
tag. The filter/tex/pix.php script then searches the database to find an
appropriate gif image file for that expression and to create one if it doesn't exist.
appropriate gif/png image file for that expression and to create one if it doesn't exist.
It will then use either the LaTex/Ghostscript renderer (using external executables
on your system) or the bundled Mimetex executable. The full Latex/Ghostscript
renderer produces better results and is tried first.
Expand All @@ -324,7 +325,7 @@ function slasharguments($texexp) {
process this expression. Then the database entry for that expression contains
a bad TeX expression in the rawtext field (usually blank). You can fix this
by clicking on &quot;Delete DB Entry&quot;</li>
<li>The TeX to gif image conversion process does not work.
<li>The TeX to gif/png image conversion process does not work.
If paths are specified in the filter configuation screen for the three
executables these will be tried first. Note that they still must be correctly
installed and have the correct permissions. In particular make sure that you
Expand Down
2 changes: 2 additions & 0 deletions lang/en_utf8/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
$string['configclamactlikevirus'] = 'Treat files like viruses';
$string['configclamdonothing'] = 'Treat files as OK';
$string['configclamfailureonupload'] = 'If you have configured clam to scan uploaded files, but it is configured incorrectly or fails to run for some unknown reason, how should it behave? If you choose \'Treat files like viruses\', they\'ll be moved into the quarantine area, or deleted. If you choose \'Treat files as OK\', the files will be moved to the destination directory like normal. Either way, admins will be alerted that clam has failed. If you choose \'Treat files like viruses\' and for some reason clam fails to run (usually because you have entered an invalid pathtoclam), ALL files that are uploaded will be moved to the given quarantine area, or deleted. Be careful with this setting.';
$string['configconvertformat'] = 'If <i>latex</i>, <i>dvips</i> and <i>convert</i> are available, the images are created using the specified format. If it is not, mimeTeX will be used and it will create GIF images.';
$string['configcookiehttponly'] = 'Enables new PHP 5.2.0 feature - browsers are instructed to send cookie with real http requests only, cookies should not be accessible by scripting languages. This is not supported in all browsers and it may not be fully compatible with current code. It helps to prevent some types of XSS attacks.';
$string['configcookiesecure'] = 'If server is accepting only https connections it is recommended to enable sending of secure cookies. If enabled please make sure that web server is not accepting http:// or set up permanent redirection to https:// address. When <em>wwwroot</em> address does not start with https:// this setting is turned off automatically.';
$string['configcountry'] = 'If you set a country here, then this country will be selected by default on new user accounts. To force users to choose a country, just leave this unset.';
Expand Down Expand Up @@ -328,6 +329,7 @@
$string['confirmed'] = 'Confirmed';
$string['confirminstall'] = 'You are about to install language pack ($a), are you sure?';
$string['confirmdeletecomments'] = 'You are about to delete comments, are you sure?';
$string['convertformat'] = '<i>convert</i> output format';
$string['cookiehttponly'] = 'Only http cookies';
$string['cookiesecure'] = 'Secure cookies only';
$string['country'] = 'Default country';
Expand Down

0 comments on commit cf4e754

Please sign in to comment.