Adjusted a comment
[lhc/web/wiklou.git] / math / render.ml
1 let cmd_dvips tmpprefix = "dvips -q -R -E " ^ tmpprefix ^ ".dvi -f >" ^ tmpprefix ^ ".ps"
2 let cmd_latex tmpprefix = "latex " ^ tmpprefix ^ ".tex >/dev/null"
3 (* Putting -transparent white in converts arguments will sort-of give you transperancy *)
4 let cmd_convert tmpprefix finalpath = "convert -quality 100 -density 120 " ^ tmpprefix ^ ".ps " ^ finalpath ^ " >/dev/null 2>/dev/null"
5 (* Putting -bg Transparent in dvipng's arguments will give full-alpha transparency *)
6 (* Note that IE have problems with such PNGs and need an additional javascript snippet *)
7 (* Putting -bg transparent in dvipng's arguments will give binary transparency *)
8 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"
9
10 exception ExternalCommandFailure of string
11
12 let render tmppath finalpath outtex md5 backcolor =
13 let tmpprefix0 = (string_of_int (Unix.getpid ()))^"_"^md5 in
14 let tmpprefix = (tmppath^"/"^tmpprefix0) in
15 let unlink_all () =
16 begin
17 (* Commenting this block out will aid in debugging *)
18 Sys.remove (tmpprefix ^ ".dvi");
19 Sys.remove (tmpprefix ^ ".aux");
20 Sys.remove (tmpprefix ^ ".log");
21 Sys.remove (tmpprefix ^ ".tex");
22 if Sys.file_exists (tmpprefix ^ ".ps")
23 then Sys.remove (tmpprefix ^ ".ps");
24 end in
25 let f = (Util.open_out_unless_exists (tmpprefix ^ ".tex")) in
26 begin
27 output_string f (Texutil.get_preface ());
28 output_string f outtex;
29 output_string f (Texutil.get_footer ());
30 close_out f;
31 if Util.run_in_other_directory tmppath (cmd_latex tmpprefix0) != 0
32 then (unlink_all (); raise (ExternalCommandFailure "latex"))
33 else if (Sys.command (cmd_dvipng tmpprefix (finalpath^"/"^md5^".png") backcolor) != 0)
34 then (if (Sys.command (cmd_dvips tmpprefix) != 0)
35 then (unlink_all (); raise (ExternalCommandFailure "dvips"))
36 else if (Sys.command (cmd_convert tmpprefix (finalpath^"/"^md5^".png")) != 0)
37 then (unlink_all (); raise (ExternalCommandFailure "convert"))
38 else unlink_all ())
39 else unlink_all ()
40 end