<?php
// Copyright(C) 2007 fem. All rights reserved.
define( "IMG_DIR", "./mathimg/" );
define( "IMG_EXT", ".png" );

function make_math2_img( $aryargs ) {
   $math = join( $aryargs,"," );
   //remove extra comma at the end.
   $math = rtrim( $math, "," );
   $imgfile = md5( $math ).IMG_EXT;
   if ( !file_exists( IMG_DIR.$imgfile ) ) {
       $tmpfile = IMG_DIR."tmp.tex";
       $tmp = fopen( $tmpfile, "w" );
       fputs( $tmp, '\documentclass[12pt]{jarticle}'."\n" );
       fputs( $tmp, '\usepackage{amsmath,amssymb,bm}'."\n" );
       fputs( $tmp, '\pagestyle{empty}'."\n" );
       fputs( $tmp, '\begin{document}'."\n" );
       fputs( $tmp, '\thispagestyle{empty}'."\n" );
       fputs( $tmp, '\large\['.$math.'\]'."\n" );
       fputs( $tmp, '\end{document}'."\n" );
       fclose( $tmp );
       $oldDir = getcwd();
       chdir( IMG_DIR );
       exec( "platex -halt-on-error tmp.tex" );
       exec( "dvipng -bg 'rgb 1.0 1.0 1.0' -bd 1000 -D 72 -T tight --gamma 1.1 -o tmp.png tmp.dvi" );
       exec( "mv tmp.png ".$imgfile );
       chdir( $oldDir );
   }
   return "<IMG SRC=".IMG_DIR.$imgfile.">";
}

function plugin_math2_convert() {
   $aryargs = func_get_args();
   return make_math2_img( $aryargs );
}

function plugin_math2_inline() {
   $aryargs = func_get_args();
   return make_math2_img( $aryargs );
}
?>
