Bug#7252: Patch to enable dvipng support in math rendering
[lhc/web/wiklou.git] / math / render.ml
1 let cmd_dvips tmpprefix = "dvips -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 = "dvipng -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 =
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 Sys.remove (tmpprefix ^ ".ps");
23 end in
24 let f = (Util.open_out_unless_exists (tmpprefix ^ ".tex")) in
25 begin
26 output_string f (Texutil.get_preface ());
27 output_string f outtex;
28 output_string f (Texutil.get_footer ());
29 close_out f;
30 if Util.run_in_other_directory tmppath (cmd_latex tmpprefix0) != 0
31 then (unlink_all (); raise (ExternalCommandFailure "latex"))
32 else if (Sys.command (cmd_dvipng tmpprefix (finalpath^"/"^md5^".png")) != 0)
33 then (if (Sys.command (cmd_dvips tmpprefix) != 0)
34 then (unlink_all (); raise (ExternalCommandFailure "dvips"))
35 else if (Sys.command (cmd_convert tmpprefix (finalpath^"/"^md5^".png")) != 0)
36 then (unlink_all (); raise (ExternalCommandFailure "convert"))
37 else unlink_all ())
38 else unlink_all ()
39 end