]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - math/render.ml
MediaWiki 1.17.0
[autoinstalls/mediawiki.git] / math / render.ml
1 (* vim: set sw=8 ts=8 et: *)
2
3 let cmd_dvips tmpprefix = "dvips -q -R -E " ^ tmpprefix ^ ".dvi -f >" ^ tmpprefix ^ ".ps"
4 let cmd_latex tmpprefix = "latex " ^ tmpprefix ^ ".tex >/dev/null"
5
6 (* Putting -transparent white in converts arguments will sort-of give you transperancy *)
7 let cmd_convert tmpprefix finalpath = "convert -quality 100 -density 120 " ^ tmpprefix ^ ".ps " ^ finalpath ^ " >/dev/null 2>/dev/null"
8
9 (* Putting -bg Transparent in dvipng's arguments will give full-alpha transparency *)
10 (* Note that IE have problems with such PNGs and need an additional javascript snippet *)
11 (* Putting -bg transparent in dvipng's arguments will give binary transparency *)
12 let cmd_dvipng tmpprefix finalpath backcolor = "dvipng -bg \'" ^ backcolor ^ "\' -gamma 1.5 -D 120 -T tight --strict " ^ tmpprefix ^ ".dvi -o " ^ finalpath ^ " >/dev/null 2>/dev/null"
13
14 exception ExternalCommandFailure of string
15
16 let render tmppath finalpath outtex md5 backcolor =
17     let tmpprefix0 = (string_of_int (Unix.getpid ()))^"_"^md5 in
18     let tmpprefix = (tmppath^"/"^tmpprefix0) in
19     let unlink_all () =
20       begin
21         (* Commenting this block out will aid in debugging *)
22         Sys.remove (tmpprefix ^ ".dvi");
23         Sys.remove (tmpprefix ^ ".aux");
24         Sys.remove (tmpprefix ^ ".log");
25         Sys.remove (tmpprefix ^ ".tex");
26         if Sys.file_exists (tmpprefix ^ ".ps")
27         then Sys.remove (tmpprefix ^ ".ps");
28       end in
29
30     let f = (Util.open_out_unless_exists (tmpprefix ^ ".tex")) in
31       begin
32         (* Assemble final output in file 'f' *)
33         output_string f (Texutil.get_preface ());
34         output_string f outtex;
35         output_string f (Texutil.get_footer ());
36         close_out f;
37
38         (* TODO: document *)
39         if Util.run_in_other_directory tmppath (cmd_latex tmpprefix0) != 0
40           then (
41             unlink_all (); raise (ExternalCommandFailure "latex")
42         ) else if (Sys.command (cmd_dvipng tmpprefix (finalpath^"/"^md5^".png") backcolor) != 0)
43           then (
44             if (Sys.command (cmd_dvips tmpprefix) != 0)
45               then (
46                 unlink_all ();
47                 raise (ExternalCommandFailure "dvips")
48             ) else if (Sys.command (cmd_convert tmpprefix (finalpath^"/"^md5^".png")) != 0)
49               then ( 
50                 unlink_all ();
51                 raise (ExternalCommandFailure "convert")
52             ) else (
53                 unlink_all ()
54             )
55         ) else (
56             unlink_all ()
57         )
58       end